raw.c 18 KB

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