raw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 notifier_block 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. /*
  92. * Return pointer to store the extra msg flags for raw_recvmsg().
  93. * We use the space of one unsigned int beyond the 'struct sockaddr_can'
  94. * in skb->cb.
  95. */
  96. static inline unsigned int *raw_flags(struct sk_buff *skb)
  97. {
  98. sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
  99. sizeof(unsigned int));
  100. /* return pointer after struct sockaddr_can */
  101. return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
  102. }
  103. static inline struct raw_sock *raw_sk(const struct sock *sk)
  104. {
  105. return (struct raw_sock *)sk;
  106. }
  107. static void raw_rcv(struct sk_buff *oskb, void *data)
  108. {
  109. struct sock *sk = (struct sock *)data;
  110. struct raw_sock *ro = raw_sk(sk);
  111. struct sockaddr_can *addr;
  112. struct sk_buff *skb;
  113. unsigned int *pflags;
  114. /* check the received tx sock reference */
  115. if (!ro->recv_own_msgs && oskb->sk == sk)
  116. return;
  117. /* do not pass non-CAN2.0 frames to a legacy socket */
  118. if (!ro->fd_frames && oskb->len != CAN_MTU)
  119. return;
  120. /* eliminate multiple filter matches for the same skb */
  121. if (this_cpu_ptr(ro->uniq)->skb == oskb &&
  122. this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
  123. if (ro->join_filters) {
  124. this_cpu_inc(ro->uniq->join_rx_count);
  125. /* drop frame until all enabled filters matched */
  126. if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
  127. return;
  128. } else {
  129. return;
  130. }
  131. } else {
  132. this_cpu_ptr(ro->uniq)->skb = oskb;
  133. this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
  134. this_cpu_ptr(ro->uniq)->join_rx_count = 1;
  135. /* drop first frame to check all enabled filters? */
  136. if (ro->join_filters && ro->count > 1)
  137. return;
  138. }
  139. /* clone the given skb to be able to enqueue it into the rcv queue */
  140. skb = skb_clone(oskb, GFP_ATOMIC);
  141. if (!skb)
  142. return;
  143. /*
  144. * Put the datagram to the queue so that raw_recvmsg() can
  145. * get it from there. We need to pass the interface index to
  146. * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
  147. * containing the interface index.
  148. */
  149. sock_skb_cb_check_size(sizeof(struct sockaddr_can));
  150. addr = (struct sockaddr_can *)skb->cb;
  151. memset(addr, 0, sizeof(*addr));
  152. addr->can_family = AF_CAN;
  153. addr->can_ifindex = skb->dev->ifindex;
  154. /* add CAN specific message flags for raw_recvmsg() */
  155. pflags = raw_flags(skb);
  156. *pflags = 0;
  157. if (oskb->sk)
  158. *pflags |= MSG_DONTROUTE;
  159. if (oskb->sk == sk)
  160. *pflags |= MSG_CONFIRM;
  161. if (sock_queue_rcv_skb(sk, skb) < 0)
  162. kfree_skb(skb);
  163. }
  164. static int raw_enable_filters(struct net_device *dev, struct sock *sk,
  165. struct can_filter *filter, int count)
  166. {
  167. int err = 0;
  168. int i;
  169. for (i = 0; i < count; i++) {
  170. err = can_rx_register(dev, filter[i].can_id,
  171. filter[i].can_mask,
  172. raw_rcv, sk, "raw", sk);
  173. if (err) {
  174. /* clean up successfully registered filters */
  175. while (--i >= 0)
  176. can_rx_unregister(dev, filter[i].can_id,
  177. filter[i].can_mask,
  178. raw_rcv, sk);
  179. break;
  180. }
  181. }
  182. return err;
  183. }
  184. static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
  185. can_err_mask_t err_mask)
  186. {
  187. int err = 0;
  188. if (err_mask)
  189. err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
  190. raw_rcv, sk, "raw", sk);
  191. return err;
  192. }
  193. static void raw_disable_filters(struct net_device *dev, struct sock *sk,
  194. struct can_filter *filter, int count)
  195. {
  196. int i;
  197. for (i = 0; i < count; i++)
  198. can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
  199. raw_rcv, sk);
  200. }
  201. static inline void raw_disable_errfilter(struct net_device *dev,
  202. struct sock *sk,
  203. can_err_mask_t err_mask)
  204. {
  205. if (err_mask)
  206. can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
  207. raw_rcv, sk);
  208. }
  209. static inline void raw_disable_allfilters(struct net_device *dev,
  210. struct sock *sk)
  211. {
  212. struct raw_sock *ro = raw_sk(sk);
  213. raw_disable_filters(dev, sk, ro->filter, ro->count);
  214. raw_disable_errfilter(dev, sk, ro->err_mask);
  215. }
  216. static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
  217. {
  218. struct raw_sock *ro = raw_sk(sk);
  219. int err;
  220. err = raw_enable_filters(dev, sk, ro->filter, ro->count);
  221. if (!err) {
  222. err = raw_enable_errfilter(dev, sk, ro->err_mask);
  223. if (err)
  224. raw_disable_filters(dev, sk, ro->filter, ro->count);
  225. }
  226. return err;
  227. }
  228. static int raw_notifier(struct notifier_block *nb,
  229. unsigned long msg, void *ptr)
  230. {
  231. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  232. struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
  233. struct sock *sk = &ro->sk;
  234. if (!net_eq(dev_net(dev), &init_net))
  235. return NOTIFY_DONE;
  236. if (dev->type != ARPHRD_CAN)
  237. return NOTIFY_DONE;
  238. if (ro->ifindex != dev->ifindex)
  239. return NOTIFY_DONE;
  240. switch (msg) {
  241. case NETDEV_UNREGISTER:
  242. lock_sock(sk);
  243. /* remove current filters & unregister */
  244. if (ro->bound)
  245. raw_disable_allfilters(dev, sk);
  246. if (ro->count > 1)
  247. kfree(ro->filter);
  248. ro->ifindex = 0;
  249. ro->bound = 0;
  250. ro->count = 0;
  251. release_sock(sk);
  252. sk->sk_err = ENODEV;
  253. if (!sock_flag(sk, SOCK_DEAD))
  254. sk->sk_error_report(sk);
  255. break;
  256. case NETDEV_DOWN:
  257. sk->sk_err = ENETDOWN;
  258. if (!sock_flag(sk, SOCK_DEAD))
  259. sk->sk_error_report(sk);
  260. break;
  261. }
  262. return NOTIFY_DONE;
  263. }
  264. static int raw_init(struct sock *sk)
  265. {
  266. struct raw_sock *ro = raw_sk(sk);
  267. ro->bound = 0;
  268. ro->ifindex = 0;
  269. /* set default filter to single entry dfilter */
  270. ro->dfilter.can_id = 0;
  271. ro->dfilter.can_mask = MASK_ALL;
  272. ro->filter = &ro->dfilter;
  273. ro->count = 1;
  274. /* set default loopback behaviour */
  275. ro->loopback = 1;
  276. ro->recv_own_msgs = 0;
  277. ro->fd_frames = 0;
  278. ro->join_filters = 0;
  279. /* alloc_percpu provides zero'ed memory */
  280. ro->uniq = alloc_percpu(struct uniqframe);
  281. if (unlikely(!ro->uniq))
  282. return -ENOMEM;
  283. /* set notifier */
  284. ro->notifier.notifier_call = raw_notifier;
  285. register_netdevice_notifier(&ro->notifier);
  286. return 0;
  287. }
  288. static int raw_release(struct socket *sock)
  289. {
  290. struct sock *sk = sock->sk;
  291. struct raw_sock *ro;
  292. if (!sk)
  293. return 0;
  294. ro = raw_sk(sk);
  295. unregister_netdevice_notifier(&ro->notifier);
  296. lock_sock(sk);
  297. /* remove current filters & unregister */
  298. if (ro->bound) {
  299. if (ro->ifindex) {
  300. struct net_device *dev;
  301. dev = dev_get_by_index(&init_net, ro->ifindex);
  302. if (dev) {
  303. raw_disable_allfilters(dev, sk);
  304. dev_put(dev);
  305. }
  306. } else
  307. raw_disable_allfilters(NULL, sk);
  308. }
  309. if (ro->count > 1)
  310. kfree(ro->filter);
  311. ro->ifindex = 0;
  312. ro->bound = 0;
  313. ro->count = 0;
  314. free_percpu(ro->uniq);
  315. sock_orphan(sk);
  316. sock->sk = NULL;
  317. release_sock(sk);
  318. sock_put(sk);
  319. return 0;
  320. }
  321. static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
  322. {
  323. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  324. struct sock *sk = sock->sk;
  325. struct raw_sock *ro = raw_sk(sk);
  326. int ifindex;
  327. int err = 0;
  328. int notify_enetdown = 0;
  329. if (len < sizeof(*addr))
  330. return -EINVAL;
  331. lock_sock(sk);
  332. if (ro->bound && addr->can_ifindex == ro->ifindex)
  333. goto out;
  334. if (addr->can_ifindex) {
  335. struct net_device *dev;
  336. dev = dev_get_by_index(&init_net, addr->can_ifindex);
  337. if (!dev) {
  338. err = -ENODEV;
  339. goto out;
  340. }
  341. if (dev->type != ARPHRD_CAN) {
  342. dev_put(dev);
  343. err = -ENODEV;
  344. goto out;
  345. }
  346. if (!(dev->flags & IFF_UP))
  347. notify_enetdown = 1;
  348. ifindex = dev->ifindex;
  349. /* filters set by default/setsockopt */
  350. err = raw_enable_allfilters(dev, sk);
  351. dev_put(dev);
  352. } else {
  353. ifindex = 0;
  354. /* filters set by default/setsockopt */
  355. err = raw_enable_allfilters(NULL, sk);
  356. }
  357. if (!err) {
  358. if (ro->bound) {
  359. /* unregister old filters */
  360. if (ro->ifindex) {
  361. struct net_device *dev;
  362. dev = dev_get_by_index(&init_net, ro->ifindex);
  363. if (dev) {
  364. raw_disable_allfilters(dev, sk);
  365. dev_put(dev);
  366. }
  367. } else
  368. raw_disable_allfilters(NULL, sk);
  369. }
  370. ro->ifindex = ifindex;
  371. ro->bound = 1;
  372. }
  373. out:
  374. release_sock(sk);
  375. if (notify_enetdown) {
  376. sk->sk_err = ENETDOWN;
  377. if (!sock_flag(sk, SOCK_DEAD))
  378. sk->sk_error_report(sk);
  379. }
  380. return err;
  381. }
  382. static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
  383. int *len, int peer)
  384. {
  385. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  386. struct sock *sk = sock->sk;
  387. struct raw_sock *ro = raw_sk(sk);
  388. if (peer)
  389. return -EOPNOTSUPP;
  390. memset(addr, 0, sizeof(*addr));
  391. addr->can_family = AF_CAN;
  392. addr->can_ifindex = ro->ifindex;
  393. *len = sizeof(*addr);
  394. return 0;
  395. }
  396. static int raw_setsockopt(struct socket *sock, int level, int optname,
  397. char __user *optval, unsigned int optlen)
  398. {
  399. struct sock *sk = sock->sk;
  400. struct raw_sock *ro = raw_sk(sk);
  401. struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
  402. struct can_filter sfilter; /* single filter */
  403. struct net_device *dev = NULL;
  404. can_err_mask_t err_mask = 0;
  405. int count = 0;
  406. int err = 0;
  407. if (level != SOL_CAN_RAW)
  408. return -EINVAL;
  409. switch (optname) {
  410. case CAN_RAW_FILTER:
  411. if (optlen % sizeof(struct can_filter) != 0)
  412. return -EINVAL;
  413. if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
  414. return -EINVAL;
  415. count = optlen / sizeof(struct can_filter);
  416. if (count > 1) {
  417. /* filter does not fit into dfilter => alloc space */
  418. filter = memdup_user(optval, optlen);
  419. if (IS_ERR(filter))
  420. return PTR_ERR(filter);
  421. } else if (count == 1) {
  422. if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
  423. return -EFAULT;
  424. }
  425. lock_sock(sk);
  426. if (ro->bound && ro->ifindex)
  427. dev = dev_get_by_index(&init_net, ro->ifindex);
  428. if (ro->bound) {
  429. /* (try to) register the new filters */
  430. if (count == 1)
  431. err = raw_enable_filters(dev, sk, &sfilter, 1);
  432. else
  433. err = raw_enable_filters(dev, sk, filter,
  434. count);
  435. if (err) {
  436. if (count > 1)
  437. kfree(filter);
  438. goto out_fil;
  439. }
  440. /* remove old filter registrations */
  441. raw_disable_filters(dev, sk, ro->filter, ro->count);
  442. }
  443. /* remove old filter space */
  444. if (ro->count > 1)
  445. kfree(ro->filter);
  446. /* link new filters to the socket */
  447. if (count == 1) {
  448. /* copy filter data for single filter */
  449. ro->dfilter = sfilter;
  450. filter = &ro->dfilter;
  451. }
  452. ro->filter = filter;
  453. ro->count = count;
  454. out_fil:
  455. if (dev)
  456. dev_put(dev);
  457. release_sock(sk);
  458. break;
  459. case CAN_RAW_ERR_FILTER:
  460. if (optlen != sizeof(err_mask))
  461. return -EINVAL;
  462. if (copy_from_user(&err_mask, optval, optlen))
  463. return -EFAULT;
  464. err_mask &= CAN_ERR_MASK;
  465. lock_sock(sk);
  466. if (ro->bound && ro->ifindex)
  467. dev = dev_get_by_index(&init_net, ro->ifindex);
  468. /* remove current error mask */
  469. if (ro->bound) {
  470. /* (try to) register the new err_mask */
  471. err = raw_enable_errfilter(dev, sk, err_mask);
  472. if (err)
  473. goto out_err;
  474. /* remove old err_mask registration */
  475. raw_disable_errfilter(dev, sk, ro->err_mask);
  476. }
  477. /* link new err_mask to the socket */
  478. ro->err_mask = err_mask;
  479. out_err:
  480. if (dev)
  481. dev_put(dev);
  482. release_sock(sk);
  483. break;
  484. case CAN_RAW_LOOPBACK:
  485. if (optlen != sizeof(ro->loopback))
  486. return -EINVAL;
  487. if (copy_from_user(&ro->loopback, optval, optlen))
  488. return -EFAULT;
  489. break;
  490. case CAN_RAW_RECV_OWN_MSGS:
  491. if (optlen != sizeof(ro->recv_own_msgs))
  492. return -EINVAL;
  493. if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
  494. return -EFAULT;
  495. break;
  496. case CAN_RAW_FD_FRAMES:
  497. if (optlen != sizeof(ro->fd_frames))
  498. return -EINVAL;
  499. if (copy_from_user(&ro->fd_frames, optval, optlen))
  500. return -EFAULT;
  501. break;
  502. case CAN_RAW_JOIN_FILTERS:
  503. if (optlen != sizeof(ro->join_filters))
  504. return -EINVAL;
  505. if (copy_from_user(&ro->join_filters, optval, optlen))
  506. return -EFAULT;
  507. break;
  508. default:
  509. return -ENOPROTOOPT;
  510. }
  511. return err;
  512. }
  513. static int raw_getsockopt(struct socket *sock, int level, int optname,
  514. char __user *optval, int __user *optlen)
  515. {
  516. struct sock *sk = sock->sk;
  517. struct raw_sock *ro = raw_sk(sk);
  518. int len;
  519. void *val;
  520. int err = 0;
  521. if (level != SOL_CAN_RAW)
  522. return -EINVAL;
  523. if (get_user(len, optlen))
  524. return -EFAULT;
  525. if (len < 0)
  526. return -EINVAL;
  527. switch (optname) {
  528. case CAN_RAW_FILTER:
  529. lock_sock(sk);
  530. if (ro->count > 0) {
  531. int fsize = ro->count * sizeof(struct can_filter);
  532. if (len > fsize)
  533. len = fsize;
  534. if (copy_to_user(optval, ro->filter, len))
  535. err = -EFAULT;
  536. } else
  537. len = 0;
  538. release_sock(sk);
  539. if (!err)
  540. err = put_user(len, optlen);
  541. return err;
  542. case CAN_RAW_ERR_FILTER:
  543. if (len > sizeof(can_err_mask_t))
  544. len = sizeof(can_err_mask_t);
  545. val = &ro->err_mask;
  546. break;
  547. case CAN_RAW_LOOPBACK:
  548. if (len > sizeof(int))
  549. len = sizeof(int);
  550. val = &ro->loopback;
  551. break;
  552. case CAN_RAW_RECV_OWN_MSGS:
  553. if (len > sizeof(int))
  554. len = sizeof(int);
  555. val = &ro->recv_own_msgs;
  556. break;
  557. case CAN_RAW_FD_FRAMES:
  558. if (len > sizeof(int))
  559. len = sizeof(int);
  560. val = &ro->fd_frames;
  561. break;
  562. case CAN_RAW_JOIN_FILTERS:
  563. if (len > sizeof(int))
  564. len = sizeof(int);
  565. val = &ro->join_filters;
  566. break;
  567. default:
  568. return -ENOPROTOOPT;
  569. }
  570. if (put_user(len, optlen))
  571. return -EFAULT;
  572. if (copy_to_user(optval, val, len))
  573. return -EFAULT;
  574. return 0;
  575. }
  576. static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  577. {
  578. struct sock *sk = sock->sk;
  579. struct raw_sock *ro = raw_sk(sk);
  580. struct sk_buff *skb;
  581. struct net_device *dev;
  582. int ifindex;
  583. int err;
  584. if (msg->msg_name) {
  585. DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
  586. if (msg->msg_namelen < sizeof(*addr))
  587. return -EINVAL;
  588. if (addr->can_family != AF_CAN)
  589. return -EINVAL;
  590. ifindex = addr->can_ifindex;
  591. } else
  592. ifindex = ro->ifindex;
  593. if (ro->fd_frames) {
  594. if (unlikely(size != CANFD_MTU && size != CAN_MTU))
  595. return -EINVAL;
  596. } else {
  597. if (unlikely(size != CAN_MTU))
  598. return -EINVAL;
  599. }
  600. dev = dev_get_by_index(&init_net, ifindex);
  601. if (!dev)
  602. return -ENXIO;
  603. skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
  604. msg->msg_flags & MSG_DONTWAIT, &err);
  605. if (!skb)
  606. goto put_dev;
  607. can_skb_reserve(skb);
  608. can_skb_prv(skb)->ifindex = dev->ifindex;
  609. can_skb_prv(skb)->skbcnt = 0;
  610. err = memcpy_from_msg(skb_put(skb, size), msg, size);
  611. if (err < 0)
  612. goto free_skb;
  613. sock_tx_timestamp(sk, sk->sk_tsflags, &skb_shinfo(skb)->tx_flags);
  614. skb->dev = dev;
  615. skb->sk = sk;
  616. skb->priority = sk->sk_priority;
  617. err = can_send(skb, ro->loopback);
  618. dev_put(dev);
  619. if (err)
  620. goto send_failed;
  621. return size;
  622. free_skb:
  623. kfree_skb(skb);
  624. put_dev:
  625. dev_put(dev);
  626. send_failed:
  627. return err;
  628. }
  629. static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  630. int flags)
  631. {
  632. struct sock *sk = sock->sk;
  633. struct sk_buff *skb;
  634. int err = 0;
  635. int noblock;
  636. noblock = flags & MSG_DONTWAIT;
  637. flags &= ~MSG_DONTWAIT;
  638. skb = skb_recv_datagram(sk, flags, noblock, &err);
  639. if (!skb)
  640. return err;
  641. if (size < skb->len)
  642. msg->msg_flags |= MSG_TRUNC;
  643. else
  644. size = skb->len;
  645. err = memcpy_to_msg(msg, skb->data, size);
  646. if (err < 0) {
  647. skb_free_datagram(sk, skb);
  648. return err;
  649. }
  650. sock_recv_ts_and_drops(msg, sk, skb);
  651. if (msg->msg_name) {
  652. __sockaddr_check_size(sizeof(struct sockaddr_can));
  653. msg->msg_namelen = sizeof(struct sockaddr_can);
  654. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  655. }
  656. /* assign the flags that have been recorded in raw_rcv() */
  657. msg->msg_flags |= *(raw_flags(skb));
  658. skb_free_datagram(sk, skb);
  659. return size;
  660. }
  661. static const struct proto_ops raw_ops = {
  662. .family = PF_CAN,
  663. .release = raw_release,
  664. .bind = raw_bind,
  665. .connect = sock_no_connect,
  666. .socketpair = sock_no_socketpair,
  667. .accept = sock_no_accept,
  668. .getname = raw_getname,
  669. .poll = datagram_poll,
  670. .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
  671. .listen = sock_no_listen,
  672. .shutdown = sock_no_shutdown,
  673. .setsockopt = raw_setsockopt,
  674. .getsockopt = raw_getsockopt,
  675. .sendmsg = raw_sendmsg,
  676. .recvmsg = raw_recvmsg,
  677. .mmap = sock_no_mmap,
  678. .sendpage = sock_no_sendpage,
  679. };
  680. static struct proto raw_proto __read_mostly = {
  681. .name = "CAN_RAW",
  682. .owner = THIS_MODULE,
  683. .obj_size = sizeof(struct raw_sock),
  684. .init = raw_init,
  685. };
  686. static const struct can_proto raw_can_proto = {
  687. .type = SOCK_RAW,
  688. .protocol = CAN_RAW,
  689. .ops = &raw_ops,
  690. .prot = &raw_proto,
  691. };
  692. static __init int raw_module_init(void)
  693. {
  694. int err;
  695. pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n");
  696. err = can_proto_register(&raw_can_proto);
  697. if (err < 0)
  698. printk(KERN_ERR "can: registration of raw protocol failed\n");
  699. return err;
  700. }
  701. static __exit void raw_module_exit(void)
  702. {
  703. can_proto_unregister(&raw_can_proto);
  704. }
  705. module_init(raw_module_init);
  706. module_exit(raw_module_exit);