raw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * raw.c - Raw sockets for protocol family CAN
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. */
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/uio.h>
  44. #include <linux/net.h>
  45. #include <linux/slab.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/socket.h>
  48. #include <linux/if_arp.h>
  49. #include <linux/skbuff.h>
  50. #include <linux/can.h>
  51. #include <linux/can/core.h>
  52. #include <linux/can/skb.h>
  53. #include <linux/can/raw.h>
  54. #include <net/sock.h>
  55. #include <net/net_namespace.h>
  56. #define CAN_RAW_VERSION CAN_VERSION
  57. MODULE_DESCRIPTION("PF_CAN raw protocol");
  58. MODULE_LICENSE("Dual BSD/GPL");
  59. MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
  60. MODULE_ALIAS("can-proto-1");
  61. #define MASK_ALL 0
  62. /*
  63. * A raw socket has a list of can_filters attached to it, each receiving
  64. * the CAN frames matching that filter. If the filter list is empty,
  65. * no CAN frames will be received by the socket. The default after
  66. * opening the socket, is to have one filter which receives all frames.
  67. * The filter list is allocated dynamically with the exception of the
  68. * list containing only one item. This common case is optimized by
  69. * storing the single filter in dfilter, to avoid using dynamic memory.
  70. */
  71. struct uniqframe {
  72. int skbcnt;
  73. const struct sk_buff *skb;
  74. unsigned int join_rx_count;
  75. };
  76. struct raw_sock {
  77. struct sock sk;
  78. int bound;
  79. int ifindex;
  80. struct list_head notifier;
  81. int loopback;
  82. int recv_own_msgs;
  83. int fd_frames;
  84. int join_filters;
  85. int count; /* number of active filters */
  86. struct can_filter dfilter; /* default/single filter */
  87. struct can_filter *filter; /* pointer to filter(s) */
  88. can_err_mask_t err_mask;
  89. struct uniqframe __percpu *uniq;
  90. };
  91. static LIST_HEAD(raw_notifier_list);
  92. static DEFINE_SPINLOCK(raw_notifier_lock);
  93. static struct raw_sock *raw_busy_notifier;
  94. /*
  95. * Return pointer to store the extra msg flags for raw_recvmsg().
  96. * We use the space of one unsigned int beyond the 'struct sockaddr_can'
  97. * in skb->cb.
  98. */
  99. static inline unsigned int *raw_flags(struct sk_buff *skb)
  100. {
  101. sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
  102. sizeof(unsigned int));
  103. /* return pointer after struct sockaddr_can */
  104. return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
  105. }
  106. static inline struct raw_sock *raw_sk(const struct sock *sk)
  107. {
  108. return (struct raw_sock *)sk;
  109. }
  110. static void raw_rcv(struct sk_buff *oskb, void *data)
  111. {
  112. struct sock *sk = (struct sock *)data;
  113. struct raw_sock *ro = raw_sk(sk);
  114. struct sockaddr_can *addr;
  115. struct sk_buff *skb;
  116. unsigned int *pflags;
  117. /* check the received tx sock reference */
  118. if (!ro->recv_own_msgs && oskb->sk == sk)
  119. return;
  120. /* do not pass non-CAN2.0 frames to a legacy socket */
  121. if (!ro->fd_frames && oskb->len != CAN_MTU)
  122. return;
  123. /* eliminate multiple filter matches for the same skb */
  124. if (this_cpu_ptr(ro->uniq)->skb == oskb &&
  125. this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
  126. if (ro->join_filters) {
  127. this_cpu_inc(ro->uniq->join_rx_count);
  128. /* drop frame until all enabled filters matched */
  129. if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
  130. return;
  131. } else {
  132. return;
  133. }
  134. } else {
  135. this_cpu_ptr(ro->uniq)->skb = oskb;
  136. this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
  137. this_cpu_ptr(ro->uniq)->join_rx_count = 1;
  138. /* drop first frame to check all enabled filters? */
  139. if (ro->join_filters && ro->count > 1)
  140. return;
  141. }
  142. /* clone the given skb to be able to enqueue it into the rcv queue */
  143. skb = skb_clone(oskb, GFP_ATOMIC);
  144. if (!skb)
  145. return;
  146. /*
  147. * Put the datagram to the queue so that raw_recvmsg() can
  148. * get it from there. We need to pass the interface index to
  149. * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
  150. * containing the interface index.
  151. */
  152. sock_skb_cb_check_size(sizeof(struct sockaddr_can));
  153. addr = (struct sockaddr_can *)skb->cb;
  154. memset(addr, 0, sizeof(*addr));
  155. addr->can_family = AF_CAN;
  156. addr->can_ifindex = skb->dev->ifindex;
  157. /* add CAN specific message flags for raw_recvmsg() */
  158. pflags = raw_flags(skb);
  159. *pflags = 0;
  160. if (oskb->sk)
  161. *pflags |= MSG_DONTROUTE;
  162. if (oskb->sk == sk)
  163. *pflags |= MSG_CONFIRM;
  164. if (sock_queue_rcv_skb(sk, skb) < 0)
  165. kfree_skb(skb);
  166. }
  167. static int raw_enable_filters(struct net *net, struct net_device *dev,
  168. struct sock *sk, struct can_filter *filter,
  169. int count)
  170. {
  171. int err = 0;
  172. int i;
  173. for (i = 0; i < count; i++) {
  174. err = can_rx_register(net, dev, filter[i].can_id,
  175. filter[i].can_mask,
  176. raw_rcv, sk, "raw", sk);
  177. if (err) {
  178. /* clean up successfully registered filters */
  179. while (--i >= 0)
  180. can_rx_unregister(net, dev, filter[i].can_id,
  181. filter[i].can_mask,
  182. raw_rcv, sk);
  183. break;
  184. }
  185. }
  186. return err;
  187. }
  188. static int raw_enable_errfilter(struct net *net, struct net_device *dev,
  189. struct sock *sk, can_err_mask_t err_mask)
  190. {
  191. int err = 0;
  192. if (err_mask)
  193. err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
  194. raw_rcv, sk, "raw", sk);
  195. return err;
  196. }
  197. static void raw_disable_filters(struct net *net, struct net_device *dev,
  198. struct sock *sk, struct can_filter *filter,
  199. int count)
  200. {
  201. int i;
  202. for (i = 0; i < count; i++)
  203. can_rx_unregister(net, dev, filter[i].can_id,
  204. filter[i].can_mask, raw_rcv, sk);
  205. }
  206. static inline void raw_disable_errfilter(struct net *net,
  207. struct net_device *dev,
  208. struct sock *sk,
  209. can_err_mask_t err_mask)
  210. {
  211. if (err_mask)
  212. can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
  213. raw_rcv, sk);
  214. }
  215. static inline void raw_disable_allfilters(struct net *net,
  216. struct net_device *dev,
  217. struct sock *sk)
  218. {
  219. struct raw_sock *ro = raw_sk(sk);
  220. raw_disable_filters(net, dev, sk, ro->filter, ro->count);
  221. raw_disable_errfilter(net, dev, sk, ro->err_mask);
  222. }
  223. static int raw_enable_allfilters(struct net *net, struct net_device *dev,
  224. struct sock *sk)
  225. {
  226. struct raw_sock *ro = raw_sk(sk);
  227. int err;
  228. err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
  229. if (!err) {
  230. err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
  231. if (err)
  232. raw_disable_filters(net, dev, sk, ro->filter,
  233. ro->count);
  234. }
  235. return err;
  236. }
  237. static void raw_notify(struct raw_sock *ro, unsigned long msg,
  238. struct net_device *dev)
  239. {
  240. struct sock *sk = &ro->sk;
  241. if (!net_eq(dev_net(dev), sock_net(sk)))
  242. return;
  243. if (ro->ifindex != dev->ifindex)
  244. return;
  245. switch (msg) {
  246. case NETDEV_UNREGISTER:
  247. lock_sock(sk);
  248. /* remove current filters & unregister */
  249. if (ro->bound)
  250. raw_disable_allfilters(dev_net(dev), dev, sk);
  251. if (ro->count > 1)
  252. kfree(ro->filter);
  253. ro->ifindex = 0;
  254. ro->bound = 0;
  255. ro->count = 0;
  256. release_sock(sk);
  257. sk->sk_err = ENODEV;
  258. if (!sock_flag(sk, SOCK_DEAD))
  259. sk->sk_error_report(sk);
  260. break;
  261. case NETDEV_DOWN:
  262. sk->sk_err = ENETDOWN;
  263. if (!sock_flag(sk, SOCK_DEAD))
  264. sk->sk_error_report(sk);
  265. break;
  266. }
  267. }
  268. static int raw_notifier(struct notifier_block *nb, unsigned long msg,
  269. void *ptr)
  270. {
  271. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  272. if (dev->type != ARPHRD_CAN)
  273. return NOTIFY_DONE;
  274. if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
  275. return NOTIFY_DONE;
  276. if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
  277. return NOTIFY_DONE;
  278. spin_lock(&raw_notifier_lock);
  279. list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
  280. spin_unlock(&raw_notifier_lock);
  281. raw_notify(raw_busy_notifier, msg, dev);
  282. spin_lock(&raw_notifier_lock);
  283. }
  284. raw_busy_notifier = NULL;
  285. spin_unlock(&raw_notifier_lock);
  286. return NOTIFY_DONE;
  287. }
  288. static int raw_init(struct sock *sk)
  289. {
  290. struct raw_sock *ro = raw_sk(sk);
  291. ro->bound = 0;
  292. ro->ifindex = 0;
  293. /* set default filter to single entry dfilter */
  294. ro->dfilter.can_id = 0;
  295. ro->dfilter.can_mask = MASK_ALL;
  296. ro->filter = &ro->dfilter;
  297. ro->count = 1;
  298. /* set default loopback behaviour */
  299. ro->loopback = 1;
  300. ro->recv_own_msgs = 0;
  301. ro->fd_frames = 0;
  302. ro->join_filters = 0;
  303. /* alloc_percpu provides zero'ed memory */
  304. ro->uniq = alloc_percpu(struct uniqframe);
  305. if (unlikely(!ro->uniq))
  306. return -ENOMEM;
  307. /* set notifier */
  308. spin_lock(&raw_notifier_lock);
  309. list_add_tail(&ro->notifier, &raw_notifier_list);
  310. spin_unlock(&raw_notifier_lock);
  311. return 0;
  312. }
  313. static int raw_release(struct socket *sock)
  314. {
  315. struct sock *sk = sock->sk;
  316. struct raw_sock *ro;
  317. if (!sk)
  318. return 0;
  319. ro = raw_sk(sk);
  320. spin_lock(&raw_notifier_lock);
  321. while (raw_busy_notifier == ro) {
  322. spin_unlock(&raw_notifier_lock);
  323. schedule_timeout_uninterruptible(1);
  324. spin_lock(&raw_notifier_lock);
  325. }
  326. list_del(&ro->notifier);
  327. spin_unlock(&raw_notifier_lock);
  328. lock_sock(sk);
  329. /* remove current filters & unregister */
  330. if (ro->bound) {
  331. if (ro->ifindex) {
  332. struct net_device *dev;
  333. dev = dev_get_by_index(sock_net(sk), ro->ifindex);
  334. if (dev) {
  335. raw_disable_allfilters(dev_net(dev), dev, sk);
  336. dev_put(dev);
  337. }
  338. } else
  339. raw_disable_allfilters(sock_net(sk), NULL, sk);
  340. }
  341. if (ro->count > 1)
  342. kfree(ro->filter);
  343. ro->ifindex = 0;
  344. ro->bound = 0;
  345. ro->count = 0;
  346. free_percpu(ro->uniq);
  347. sock_orphan(sk);
  348. sock->sk = NULL;
  349. release_sock(sk);
  350. sock_put(sk);
  351. return 0;
  352. }
  353. static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
  354. {
  355. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  356. struct sock *sk = sock->sk;
  357. struct raw_sock *ro = raw_sk(sk);
  358. int ifindex;
  359. int err = 0;
  360. int notify_enetdown = 0;
  361. if (len < sizeof(*addr))
  362. return -EINVAL;
  363. lock_sock(sk);
  364. if (ro->bound && addr->can_ifindex == ro->ifindex)
  365. goto out;
  366. if (addr->can_ifindex) {
  367. struct net_device *dev;
  368. dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
  369. if (!dev) {
  370. err = -ENODEV;
  371. goto out;
  372. }
  373. if (dev->type != ARPHRD_CAN) {
  374. dev_put(dev);
  375. err = -ENODEV;
  376. goto out;
  377. }
  378. if (!(dev->flags & IFF_UP))
  379. notify_enetdown = 1;
  380. ifindex = dev->ifindex;
  381. /* filters set by default/setsockopt */
  382. err = raw_enable_allfilters(sock_net(sk), dev, sk);
  383. dev_put(dev);
  384. } else {
  385. ifindex = 0;
  386. /* filters set by default/setsockopt */
  387. err = raw_enable_allfilters(sock_net(sk), NULL, sk);
  388. }
  389. if (!err) {
  390. if (ro->bound) {
  391. /* unregister old filters */
  392. if (ro->ifindex) {
  393. struct net_device *dev;
  394. dev = dev_get_by_index(sock_net(sk),
  395. ro->ifindex);
  396. if (dev) {
  397. raw_disable_allfilters(dev_net(dev),
  398. dev, sk);
  399. dev_put(dev);
  400. }
  401. } else
  402. raw_disable_allfilters(sock_net(sk), NULL, sk);
  403. }
  404. ro->ifindex = ifindex;
  405. ro->bound = 1;
  406. }
  407. out:
  408. release_sock(sk);
  409. if (notify_enetdown) {
  410. sk->sk_err = ENETDOWN;
  411. if (!sock_flag(sk, SOCK_DEAD))
  412. sk->sk_error_report(sk);
  413. }
  414. return err;
  415. }
  416. static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
  417. int *len, int peer)
  418. {
  419. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  420. struct sock *sk = sock->sk;
  421. struct raw_sock *ro = raw_sk(sk);
  422. if (peer)
  423. return -EOPNOTSUPP;
  424. memset(addr, 0, sizeof(*addr));
  425. addr->can_family = AF_CAN;
  426. addr->can_ifindex = ro->ifindex;
  427. *len = sizeof(*addr);
  428. return 0;
  429. }
  430. static int raw_setsockopt(struct socket *sock, int level, int optname,
  431. char __user *optval, unsigned int optlen)
  432. {
  433. struct sock *sk = sock->sk;
  434. struct raw_sock *ro = raw_sk(sk);
  435. struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
  436. struct can_filter sfilter; /* single filter */
  437. struct net_device *dev = NULL;
  438. can_err_mask_t err_mask = 0;
  439. int count = 0;
  440. int err = 0;
  441. if (level != SOL_CAN_RAW)
  442. return -EINVAL;
  443. switch (optname) {
  444. case CAN_RAW_FILTER:
  445. if (optlen % sizeof(struct can_filter) != 0)
  446. return -EINVAL;
  447. if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
  448. return -EINVAL;
  449. count = optlen / sizeof(struct can_filter);
  450. if (count > 1) {
  451. /* filter does not fit into dfilter => alloc space */
  452. filter = memdup_user(optval, optlen);
  453. if (IS_ERR(filter))
  454. return PTR_ERR(filter);
  455. } else if (count == 1) {
  456. if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
  457. return -EFAULT;
  458. }
  459. lock_sock(sk);
  460. if (ro->bound && ro->ifindex)
  461. dev = dev_get_by_index(sock_net(sk), ro->ifindex);
  462. if (ro->bound) {
  463. /* (try to) register the new filters */
  464. if (count == 1)
  465. err = raw_enable_filters(sock_net(sk), dev, sk,
  466. &sfilter, 1);
  467. else
  468. err = raw_enable_filters(sock_net(sk), dev, sk,
  469. filter, count);
  470. if (err) {
  471. if (count > 1)
  472. kfree(filter);
  473. goto out_fil;
  474. }
  475. /* remove old filter registrations */
  476. raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
  477. ro->count);
  478. }
  479. /* remove old filter space */
  480. if (ro->count > 1)
  481. kfree(ro->filter);
  482. /* link new filters to the socket */
  483. if (count == 1) {
  484. /* copy filter data for single filter */
  485. ro->dfilter = sfilter;
  486. filter = &ro->dfilter;
  487. }
  488. ro->filter = filter;
  489. ro->count = count;
  490. out_fil:
  491. if (dev)
  492. dev_put(dev);
  493. release_sock(sk);
  494. break;
  495. case CAN_RAW_ERR_FILTER:
  496. if (optlen != sizeof(err_mask))
  497. return -EINVAL;
  498. if (copy_from_user(&err_mask, optval, optlen))
  499. return -EFAULT;
  500. err_mask &= CAN_ERR_MASK;
  501. lock_sock(sk);
  502. if (ro->bound && ro->ifindex)
  503. dev = dev_get_by_index(sock_net(sk), ro->ifindex);
  504. /* remove current error mask */
  505. if (ro->bound) {
  506. /* (try to) register the new err_mask */
  507. err = raw_enable_errfilter(sock_net(sk), dev, sk,
  508. err_mask);
  509. if (err)
  510. goto out_err;
  511. /* remove old err_mask registration */
  512. raw_disable_errfilter(sock_net(sk), dev, sk,
  513. ro->err_mask);
  514. }
  515. /* link new err_mask to the socket */
  516. ro->err_mask = err_mask;
  517. out_err:
  518. if (dev)
  519. dev_put(dev);
  520. release_sock(sk);
  521. break;
  522. case CAN_RAW_LOOPBACK:
  523. if (optlen != sizeof(ro->loopback))
  524. return -EINVAL;
  525. if (copy_from_user(&ro->loopback, optval, optlen))
  526. return -EFAULT;
  527. break;
  528. case CAN_RAW_RECV_OWN_MSGS:
  529. if (optlen != sizeof(ro->recv_own_msgs))
  530. return -EINVAL;
  531. if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
  532. return -EFAULT;
  533. break;
  534. case CAN_RAW_FD_FRAMES:
  535. if (optlen != sizeof(ro->fd_frames))
  536. return -EINVAL;
  537. if (copy_from_user(&ro->fd_frames, optval, optlen))
  538. return -EFAULT;
  539. break;
  540. case CAN_RAW_JOIN_FILTERS:
  541. if (optlen != sizeof(ro->join_filters))
  542. return -EINVAL;
  543. if (copy_from_user(&ro->join_filters, optval, optlen))
  544. return -EFAULT;
  545. break;
  546. default:
  547. return -ENOPROTOOPT;
  548. }
  549. return err;
  550. }
  551. static int raw_getsockopt(struct socket *sock, int level, int optname,
  552. char __user *optval, int __user *optlen)
  553. {
  554. struct sock *sk = sock->sk;
  555. struct raw_sock *ro = raw_sk(sk);
  556. int len;
  557. void *val;
  558. int err = 0;
  559. if (level != SOL_CAN_RAW)
  560. return -EINVAL;
  561. if (get_user(len, optlen))
  562. return -EFAULT;
  563. if (len < 0)
  564. return -EINVAL;
  565. switch (optname) {
  566. case CAN_RAW_FILTER:
  567. lock_sock(sk);
  568. if (ro->count > 0) {
  569. int fsize = ro->count * sizeof(struct can_filter);
  570. if (len > fsize)
  571. len = fsize;
  572. if (copy_to_user(optval, ro->filter, len))
  573. err = -EFAULT;
  574. } else
  575. len = 0;
  576. release_sock(sk);
  577. if (!err)
  578. err = put_user(len, optlen);
  579. return err;
  580. case CAN_RAW_ERR_FILTER:
  581. if (len > sizeof(can_err_mask_t))
  582. len = sizeof(can_err_mask_t);
  583. val = &ro->err_mask;
  584. break;
  585. case CAN_RAW_LOOPBACK:
  586. if (len > sizeof(int))
  587. len = sizeof(int);
  588. val = &ro->loopback;
  589. break;
  590. case CAN_RAW_RECV_OWN_MSGS:
  591. if (len > sizeof(int))
  592. len = sizeof(int);
  593. val = &ro->recv_own_msgs;
  594. break;
  595. case CAN_RAW_FD_FRAMES:
  596. if (len > sizeof(int))
  597. len = sizeof(int);
  598. val = &ro->fd_frames;
  599. break;
  600. case CAN_RAW_JOIN_FILTERS:
  601. if (len > sizeof(int))
  602. len = sizeof(int);
  603. val = &ro->join_filters;
  604. break;
  605. default:
  606. return -ENOPROTOOPT;
  607. }
  608. if (put_user(len, optlen))
  609. return -EFAULT;
  610. if (copy_to_user(optval, val, len))
  611. return -EFAULT;
  612. return 0;
  613. }
  614. static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  615. {
  616. struct sock *sk = sock->sk;
  617. struct raw_sock *ro = raw_sk(sk);
  618. struct sk_buff *skb;
  619. struct net_device *dev;
  620. int ifindex;
  621. int err;
  622. if (msg->msg_name) {
  623. DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
  624. if (msg->msg_namelen < sizeof(*addr))
  625. return -EINVAL;
  626. if (addr->can_family != AF_CAN)
  627. return -EINVAL;
  628. ifindex = addr->can_ifindex;
  629. } else
  630. ifindex = ro->ifindex;
  631. dev = dev_get_by_index(sock_net(sk), ifindex);
  632. if (!dev)
  633. return -ENXIO;
  634. err = -EINVAL;
  635. if (ro->fd_frames && dev->mtu == CANFD_MTU) {
  636. if (unlikely(size != CANFD_MTU && size != CAN_MTU))
  637. goto put_dev;
  638. } else {
  639. if (unlikely(size != CAN_MTU))
  640. goto put_dev;
  641. }
  642. skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
  643. msg->msg_flags & MSG_DONTWAIT, &err);
  644. if (!skb)
  645. goto put_dev;
  646. can_skb_reserve(skb);
  647. can_skb_prv(skb)->ifindex = dev->ifindex;
  648. can_skb_prv(skb)->skbcnt = 0;
  649. err = memcpy_from_msg(skb_put(skb, size), msg, size);
  650. if (err < 0)
  651. goto free_skb;
  652. sock_tx_timestamp(sk, sk->sk_tsflags, &skb_shinfo(skb)->tx_flags);
  653. skb->dev = dev;
  654. skb->sk = sk;
  655. skb->priority = sk->sk_priority;
  656. err = can_send(skb, ro->loopback);
  657. dev_put(dev);
  658. if (err)
  659. goto send_failed;
  660. return size;
  661. free_skb:
  662. kfree_skb(skb);
  663. put_dev:
  664. dev_put(dev);
  665. send_failed:
  666. return err;
  667. }
  668. static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  669. int flags)
  670. {
  671. struct sock *sk = sock->sk;
  672. struct sk_buff *skb;
  673. int err = 0;
  674. int noblock;
  675. noblock = flags & MSG_DONTWAIT;
  676. flags &= ~MSG_DONTWAIT;
  677. skb = skb_recv_datagram(sk, flags, noblock, &err);
  678. if (!skb)
  679. return err;
  680. if (size < skb->len)
  681. msg->msg_flags |= MSG_TRUNC;
  682. else
  683. size = skb->len;
  684. err = memcpy_to_msg(msg, skb->data, size);
  685. if (err < 0) {
  686. skb_free_datagram(sk, skb);
  687. return err;
  688. }
  689. sock_recv_ts_and_drops(msg, sk, skb);
  690. if (msg->msg_name) {
  691. __sockaddr_check_size(sizeof(struct sockaddr_can));
  692. msg->msg_namelen = sizeof(struct sockaddr_can);
  693. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  694. }
  695. /* assign the flags that have been recorded in raw_rcv() */
  696. msg->msg_flags |= *(raw_flags(skb));
  697. skb_free_datagram(sk, skb);
  698. return size;
  699. }
  700. static const struct proto_ops raw_ops = {
  701. .family = PF_CAN,
  702. .release = raw_release,
  703. .bind = raw_bind,
  704. .connect = sock_no_connect,
  705. .socketpair = sock_no_socketpair,
  706. .accept = sock_no_accept,
  707. .getname = raw_getname,
  708. .poll = datagram_poll,
  709. .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
  710. .listen = sock_no_listen,
  711. .shutdown = sock_no_shutdown,
  712. .setsockopt = raw_setsockopt,
  713. .getsockopt = raw_getsockopt,
  714. .sendmsg = raw_sendmsg,
  715. .recvmsg = raw_recvmsg,
  716. .mmap = sock_no_mmap,
  717. .sendpage = sock_no_sendpage,
  718. };
  719. static struct proto raw_proto __read_mostly = {
  720. .name = "CAN_RAW",
  721. .owner = THIS_MODULE,
  722. .obj_size = sizeof(struct raw_sock),
  723. .init = raw_init,
  724. };
  725. static const struct can_proto raw_can_proto = {
  726. .type = SOCK_RAW,
  727. .protocol = CAN_RAW,
  728. .ops = &raw_ops,
  729. .prot = &raw_proto,
  730. };
  731. static struct notifier_block canraw_notifier = {
  732. .notifier_call = raw_notifier
  733. };
  734. static __init int raw_module_init(void)
  735. {
  736. int err;
  737. pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n");
  738. err = can_proto_register(&raw_can_proto);
  739. if (err < 0)
  740. printk(KERN_ERR "can: registration of raw protocol failed\n");
  741. else
  742. register_netdevice_notifier(&canraw_notifier);
  743. return err;
  744. }
  745. static __exit void raw_module_exit(void)
  746. {
  747. can_proto_unregister(&raw_can_proto);
  748. unregister_netdevice_notifier(&canraw_notifier);
  749. }
  750. module_init(raw_module_init);
  751. module_exit(raw_module_exit);