af_can.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * af_can.c - Protocol family CAN core module
  3. * (used by different CAN protocol modules)
  4. *
  5. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of Volkswagen nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * Alternatively, provided that this notice is retained in full, this
  21. * software may be distributed under the terms of the GNU General
  22. * Public License ("GPL") version 2, in which case the provisions of the
  23. * GPL apply INSTEAD OF those given above.
  24. *
  25. * The provided data structures and external interfaces from this code
  26. * are not restricted to be used by modules with a GPL compatible license.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39. * DAMAGE.
  40. *
  41. */
  42. #include <linux/module.h>
  43. #include <linux/init.h>
  44. #include <linux/kmod.h>
  45. #include <linux/slab.h>
  46. #include <linux/list.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/rcupdate.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/net.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/socket.h>
  53. #include <linux/if_ether.h>
  54. #include <linux/if_arp.h>
  55. #include <linux/skbuff.h>
  56. #include <linux/can.h>
  57. #include <linux/can/core.h>
  58. #include <linux/ratelimit.h>
  59. #include <net/net_namespace.h>
  60. #include <net/sock.h>
  61. #include "af_can.h"
  62. static __initdata const char banner[] = KERN_INFO
  63. "can: controller area network core (" CAN_VERSION_STRING ")\n";
  64. MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
  65. MODULE_LICENSE("Dual BSD/GPL");
  66. MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
  67. "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  68. MODULE_ALIAS_NETPROTO(PF_CAN);
  69. static int stats_timer __read_mostly = 1;
  70. module_param(stats_timer, int, S_IRUGO);
  71. MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
  72. /* receive filters subscribed for 'all' CAN devices */
  73. struct dev_rcv_lists can_rx_alldev_list;
  74. static DEFINE_SPINLOCK(can_rcvlists_lock);
  75. static struct kmem_cache *rcv_cache __read_mostly;
  76. /* table of registered CAN protocols */
  77. static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
  78. static DEFINE_MUTEX(proto_tab_lock);
  79. struct timer_list can_stattimer; /* timer for statistics update */
  80. struct s_stats can_stats; /* packet statistics */
  81. struct s_pstats can_pstats; /* receive list statistics */
  82. /*
  83. * af_can socket functions
  84. */
  85. int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  86. {
  87. struct sock *sk = sock->sk;
  88. switch (cmd) {
  89. case SIOCGSTAMP:
  90. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  91. default:
  92. return -ENOIOCTLCMD;
  93. }
  94. }
  95. EXPORT_SYMBOL(can_ioctl);
  96. static void can_sock_destruct(struct sock *sk)
  97. {
  98. skb_queue_purge(&sk->sk_receive_queue);
  99. }
  100. static const struct can_proto *can_get_proto(int protocol)
  101. {
  102. const struct can_proto *cp;
  103. rcu_read_lock();
  104. cp = rcu_dereference(proto_tab[protocol]);
  105. if (cp && !try_module_get(cp->prot->owner))
  106. cp = NULL;
  107. rcu_read_unlock();
  108. return cp;
  109. }
  110. static inline void can_put_proto(const struct can_proto *cp)
  111. {
  112. module_put(cp->prot->owner);
  113. }
  114. static int can_create(struct net *net, struct socket *sock, int protocol,
  115. int kern)
  116. {
  117. struct sock *sk;
  118. const struct can_proto *cp;
  119. int err = 0;
  120. sock->state = SS_UNCONNECTED;
  121. if (protocol < 0 || protocol >= CAN_NPROTO)
  122. return -EINVAL;
  123. if (!net_eq(net, &init_net))
  124. return -EAFNOSUPPORT;
  125. cp = can_get_proto(protocol);
  126. #ifdef CONFIG_MODULES
  127. if (!cp) {
  128. /* try to load protocol module if kernel is modular */
  129. err = request_module("can-proto-%d", protocol);
  130. /*
  131. * In case of error we only print a message but don't
  132. * return the error code immediately. Below we will
  133. * return -EPROTONOSUPPORT
  134. */
  135. if (err)
  136. printk_ratelimited(KERN_ERR "can: request_module "
  137. "(can-proto-%d) failed.\n", protocol);
  138. cp = can_get_proto(protocol);
  139. }
  140. #endif
  141. /* check for available protocol and correct usage */
  142. if (!cp)
  143. return -EPROTONOSUPPORT;
  144. if (cp->type != sock->type) {
  145. err = -EPROTOTYPE;
  146. goto errout;
  147. }
  148. sock->ops = cp->ops;
  149. sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
  150. if (!sk) {
  151. err = -ENOMEM;
  152. goto errout;
  153. }
  154. sock_init_data(sock, sk);
  155. sk->sk_destruct = can_sock_destruct;
  156. if (sk->sk_prot->init)
  157. err = sk->sk_prot->init(sk);
  158. if (err) {
  159. /* release sk on errors */
  160. sock_orphan(sk);
  161. sock_put(sk);
  162. }
  163. errout:
  164. can_put_proto(cp);
  165. return err;
  166. }
  167. /*
  168. * af_can tx path
  169. */
  170. /**
  171. * can_send - transmit a CAN frame (optional with local loopback)
  172. * @skb: pointer to socket buffer with CAN frame in data section
  173. * @loop: loopback for listeners on local CAN sockets (recommended default!)
  174. *
  175. * Due to the loopback this routine must not be called from hardirq context.
  176. *
  177. * Return:
  178. * 0 on success
  179. * -ENETDOWN when the selected interface is down
  180. * -ENOBUFS on full driver queue (see net_xmit_errno())
  181. * -ENOMEM when local loopback failed at calling skb_clone()
  182. * -EPERM when trying to send on a non-CAN interface
  183. * -EINVAL when the skb->data does not contain a valid CAN frame
  184. */
  185. int can_send(struct sk_buff *skb, int loop)
  186. {
  187. struct sk_buff *newskb = NULL;
  188. struct can_frame *cf = (struct can_frame *)skb->data;
  189. int err;
  190. if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
  191. kfree_skb(skb);
  192. return -EINVAL;
  193. }
  194. if (skb->dev->type != ARPHRD_CAN) {
  195. kfree_skb(skb);
  196. return -EPERM;
  197. }
  198. if (!(skb->dev->flags & IFF_UP)) {
  199. kfree_skb(skb);
  200. return -ENETDOWN;
  201. }
  202. skb->protocol = htons(ETH_P_CAN);
  203. skb_reset_network_header(skb);
  204. skb_reset_transport_header(skb);
  205. if (loop) {
  206. /* local loopback of sent CAN frames */
  207. /* indication for the CAN driver: do loopback */
  208. skb->pkt_type = PACKET_LOOPBACK;
  209. /*
  210. * The reference to the originating sock may be required
  211. * by the receiving socket to check whether the frame is
  212. * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
  213. * Therefore we have to ensure that skb->sk remains the
  214. * reference to the originating sock by restoring skb->sk
  215. * after each skb_clone() or skb_orphan() usage.
  216. */
  217. if (!(skb->dev->flags & IFF_ECHO)) {
  218. /*
  219. * If the interface is not capable to do loopback
  220. * itself, we do it here.
  221. */
  222. newskb = skb_clone(skb, GFP_ATOMIC);
  223. if (!newskb) {
  224. kfree_skb(skb);
  225. return -ENOMEM;
  226. }
  227. newskb->sk = skb->sk;
  228. newskb->ip_summed = CHECKSUM_UNNECESSARY;
  229. newskb->pkt_type = PACKET_BROADCAST;
  230. }
  231. } else {
  232. /* indication for the CAN driver: no loopback required */
  233. skb->pkt_type = PACKET_HOST;
  234. }
  235. /* send to netdevice */
  236. err = dev_queue_xmit(skb);
  237. if (err > 0)
  238. err = net_xmit_errno(err);
  239. if (err) {
  240. kfree_skb(newskb);
  241. return err;
  242. }
  243. if (newskb)
  244. netif_rx_ni(newskb);
  245. /* update statistics */
  246. can_stats.tx_frames++;
  247. can_stats.tx_frames_delta++;
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(can_send);
  251. /*
  252. * af_can rx path
  253. */
  254. static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
  255. {
  256. if (!dev)
  257. return &can_rx_alldev_list;
  258. else
  259. return (struct dev_rcv_lists *)dev->ml_priv;
  260. }
  261. /**
  262. * find_rcv_list - determine optimal filterlist inside device filter struct
  263. * @can_id: pointer to CAN identifier of a given can_filter
  264. * @mask: pointer to CAN mask of a given can_filter
  265. * @d: pointer to the device filter struct
  266. *
  267. * Description:
  268. * Returns the optimal filterlist to reduce the filter handling in the
  269. * receive path. This function is called by service functions that need
  270. * to register or unregister a can_filter in the filter lists.
  271. *
  272. * A filter matches in general, when
  273. *
  274. * <received_can_id> & mask == can_id & mask
  275. *
  276. * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
  277. * relevant bits for the filter.
  278. *
  279. * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
  280. * filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
  281. * there is a special filterlist and a special rx path filter handling.
  282. *
  283. * Return:
  284. * Pointer to optimal filterlist for the given can_id/mask pair.
  285. * Constistency checked mask.
  286. * Reduced can_id to have a preprocessed filter compare value.
  287. */
  288. static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
  289. struct dev_rcv_lists *d)
  290. {
  291. canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
  292. /* filter for error frames in extra filterlist */
  293. if (*mask & CAN_ERR_FLAG) {
  294. /* clear CAN_ERR_FLAG in filter entry */
  295. *mask &= CAN_ERR_MASK;
  296. return &d->rx[RX_ERR];
  297. }
  298. /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
  299. #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
  300. /* ensure valid values in can_mask for 'SFF only' frame filtering */
  301. if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
  302. *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
  303. /* reduce condition testing at receive time */
  304. *can_id &= *mask;
  305. /* inverse can_id/can_mask filter */
  306. if (inv)
  307. return &d->rx[RX_INV];
  308. /* mask == 0 => no condition testing at receive time */
  309. if (!(*mask))
  310. return &d->rx[RX_ALL];
  311. /* extra filterlists for the subscription of a single non-RTR can_id */
  312. if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
  313. !(*can_id & CAN_RTR_FLAG)) {
  314. if (*can_id & CAN_EFF_FLAG) {
  315. if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
  316. /* RFC: a future use-case for hash-tables? */
  317. return &d->rx[RX_EFF];
  318. }
  319. } else {
  320. if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
  321. return &d->rx_sff[*can_id];
  322. }
  323. }
  324. /* default: filter via can_id/can_mask */
  325. return &d->rx[RX_FIL];
  326. }
  327. /**
  328. * can_rx_register - subscribe CAN frames from a specific interface
  329. * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
  330. * @can_id: CAN identifier (see description)
  331. * @mask: CAN mask (see description)
  332. * @func: callback function on filter match
  333. * @data: returned parameter for callback function
  334. * @ident: string for calling module indentification
  335. *
  336. * Description:
  337. * Invokes the callback function with the received sk_buff and the given
  338. * parameter 'data' on a matching receive filter. A filter matches, when
  339. *
  340. * <received_can_id> & mask == can_id & mask
  341. *
  342. * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
  343. * filter for error frames (CAN_ERR_FLAG bit set in mask).
  344. *
  345. * The provided pointer to the sk_buff is guaranteed to be valid as long as
  346. * the callback function is running. The callback function must *not* free
  347. * the given sk_buff while processing it's task. When the given sk_buff is
  348. * needed after the end of the callback function it must be cloned inside
  349. * the callback function with skb_clone().
  350. *
  351. * Return:
  352. * 0 on success
  353. * -ENOMEM on missing cache mem to create subscription entry
  354. * -ENODEV unknown device
  355. */
  356. int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
  357. void (*func)(struct sk_buff *, void *), void *data,
  358. char *ident)
  359. {
  360. struct receiver *r;
  361. struct hlist_head *rl;
  362. struct dev_rcv_lists *d;
  363. int err = 0;
  364. /* insert new receiver (dev,canid,mask) -> (func,data) */
  365. if (dev && dev->type != ARPHRD_CAN)
  366. return -ENODEV;
  367. r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
  368. if (!r)
  369. return -ENOMEM;
  370. spin_lock(&can_rcvlists_lock);
  371. d = find_dev_rcv_lists(dev);
  372. if (d) {
  373. rl = find_rcv_list(&can_id, &mask, d);
  374. r->can_id = can_id;
  375. r->mask = mask;
  376. r->matches = 0;
  377. r->func = func;
  378. r->data = data;
  379. r->ident = ident;
  380. hlist_add_head_rcu(&r->list, rl);
  381. d->entries++;
  382. can_pstats.rcv_entries++;
  383. if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
  384. can_pstats.rcv_entries_max = can_pstats.rcv_entries;
  385. } else {
  386. kmem_cache_free(rcv_cache, r);
  387. err = -ENODEV;
  388. }
  389. spin_unlock(&can_rcvlists_lock);
  390. return err;
  391. }
  392. EXPORT_SYMBOL(can_rx_register);
  393. /*
  394. * can_rx_delete_receiver - rcu callback for single receiver entry removal
  395. */
  396. static void can_rx_delete_receiver(struct rcu_head *rp)
  397. {
  398. struct receiver *r = container_of(rp, struct receiver, rcu);
  399. kmem_cache_free(rcv_cache, r);
  400. }
  401. /**
  402. * can_rx_unregister - unsubscribe CAN frames from a specific interface
  403. * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
  404. * @can_id: CAN identifier
  405. * @mask: CAN mask
  406. * @func: callback function on filter match
  407. * @data: returned parameter for callback function
  408. *
  409. * Description:
  410. * Removes subscription entry depending on given (subscription) values.
  411. */
  412. void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
  413. void (*func)(struct sk_buff *, void *), void *data)
  414. {
  415. struct receiver *r = NULL;
  416. struct hlist_head *rl;
  417. struct hlist_node *next;
  418. struct dev_rcv_lists *d;
  419. if (dev && dev->type != ARPHRD_CAN)
  420. return;
  421. spin_lock(&can_rcvlists_lock);
  422. d = find_dev_rcv_lists(dev);
  423. if (!d) {
  424. printk(KERN_ERR "BUG: receive list not found for "
  425. "dev %s, id %03X, mask %03X\n",
  426. DNAME(dev), can_id, mask);
  427. goto out;
  428. }
  429. rl = find_rcv_list(&can_id, &mask, d);
  430. /*
  431. * Search the receiver list for the item to delete. This should
  432. * exist, since no receiver may be unregistered that hasn't
  433. * been registered before.
  434. */
  435. hlist_for_each_entry_rcu(r, next, rl, list) {
  436. if (r->can_id == can_id && r->mask == mask &&
  437. r->func == func && r->data == data)
  438. break;
  439. }
  440. /*
  441. * Check for bugs in CAN protocol implementations:
  442. * If no matching list item was found, the list cursor variable next
  443. * will be NULL, while r will point to the last item of the list.
  444. */
  445. if (!next) {
  446. printk(KERN_ERR "BUG: receive list entry not found for "
  447. "dev %s, id %03X, mask %03X\n",
  448. DNAME(dev), can_id, mask);
  449. r = NULL;
  450. goto out;
  451. }
  452. hlist_del_rcu(&r->list);
  453. d->entries--;
  454. if (can_pstats.rcv_entries > 0)
  455. can_pstats.rcv_entries--;
  456. /* remove device structure requested by NETDEV_UNREGISTER */
  457. if (d->remove_on_zero_entries && !d->entries) {
  458. kfree(d);
  459. dev->ml_priv = NULL;
  460. }
  461. out:
  462. spin_unlock(&can_rcvlists_lock);
  463. /* schedule the receiver item for deletion */
  464. if (r)
  465. call_rcu(&r->rcu, can_rx_delete_receiver);
  466. }
  467. EXPORT_SYMBOL(can_rx_unregister);
  468. static inline void deliver(struct sk_buff *skb, struct receiver *r)
  469. {
  470. r->func(skb, r->data);
  471. r->matches++;
  472. }
  473. static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
  474. {
  475. struct receiver *r;
  476. struct hlist_node *n;
  477. int matches = 0;
  478. struct can_frame *cf = (struct can_frame *)skb->data;
  479. canid_t can_id = cf->can_id;
  480. if (d->entries == 0)
  481. return 0;
  482. if (can_id & CAN_ERR_FLAG) {
  483. /* check for error frame entries only */
  484. hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
  485. if (can_id & r->mask) {
  486. deliver(skb, r);
  487. matches++;
  488. }
  489. }
  490. return matches;
  491. }
  492. /* check for unfiltered entries */
  493. hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
  494. deliver(skb, r);
  495. matches++;
  496. }
  497. /* check for can_id/mask entries */
  498. hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
  499. if ((can_id & r->mask) == r->can_id) {
  500. deliver(skb, r);
  501. matches++;
  502. }
  503. }
  504. /* check for inverted can_id/mask entries */
  505. hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
  506. if ((can_id & r->mask) != r->can_id) {
  507. deliver(skb, r);
  508. matches++;
  509. }
  510. }
  511. /* check filterlists for single non-RTR can_ids */
  512. if (can_id & CAN_RTR_FLAG)
  513. return matches;
  514. if (can_id & CAN_EFF_FLAG) {
  515. hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
  516. if (r->can_id == can_id) {
  517. deliver(skb, r);
  518. matches++;
  519. }
  520. }
  521. } else {
  522. can_id &= CAN_SFF_MASK;
  523. hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
  524. deliver(skb, r);
  525. matches++;
  526. }
  527. }
  528. return matches;
  529. }
  530. static int can_rcv(struct sk_buff *skb, struct net_device *dev,
  531. struct packet_type *pt, struct net_device *orig_dev)
  532. {
  533. struct dev_rcv_lists *d;
  534. struct can_frame *cf = (struct can_frame *)skb->data;
  535. int matches;
  536. if (!net_eq(dev_net(dev), &init_net))
  537. goto drop;
  538. if (WARN_ONCE(dev->type != ARPHRD_CAN ||
  539. skb->len != sizeof(struct can_frame) ||
  540. cf->can_dlc > 8,
  541. "PF_CAN: dropped non conform skbuf: "
  542. "dev type %d, len %d, can_dlc %d\n",
  543. dev->type, skb->len, cf->can_dlc))
  544. goto drop;
  545. /* update statistics */
  546. can_stats.rx_frames++;
  547. can_stats.rx_frames_delta++;
  548. rcu_read_lock();
  549. /* deliver the packet to sockets listening on all devices */
  550. matches = can_rcv_filter(&can_rx_alldev_list, skb);
  551. /* find receive list for this device */
  552. d = find_dev_rcv_lists(dev);
  553. if (d)
  554. matches += can_rcv_filter(d, skb);
  555. rcu_read_unlock();
  556. /* consume the skbuff allocated by the netdevice driver */
  557. consume_skb(skb);
  558. if (matches > 0) {
  559. can_stats.matches++;
  560. can_stats.matches_delta++;
  561. }
  562. return NET_RX_SUCCESS;
  563. drop:
  564. kfree_skb(skb);
  565. return NET_RX_DROP;
  566. }
  567. /*
  568. * af_can protocol functions
  569. */
  570. /**
  571. * can_proto_register - register CAN transport protocol
  572. * @cp: pointer to CAN protocol structure
  573. *
  574. * Return:
  575. * 0 on success
  576. * -EINVAL invalid (out of range) protocol number
  577. * -EBUSY protocol already in use
  578. * -ENOBUF if proto_register() fails
  579. */
  580. int can_proto_register(const struct can_proto *cp)
  581. {
  582. int proto = cp->protocol;
  583. int err = 0;
  584. if (proto < 0 || proto >= CAN_NPROTO) {
  585. printk(KERN_ERR "can: protocol number %d out of range\n",
  586. proto);
  587. return -EINVAL;
  588. }
  589. err = proto_register(cp->prot, 0);
  590. if (err < 0)
  591. return err;
  592. mutex_lock(&proto_tab_lock);
  593. if (proto_tab[proto]) {
  594. printk(KERN_ERR "can: protocol %d already registered\n",
  595. proto);
  596. err = -EBUSY;
  597. } else
  598. RCU_INIT_POINTER(proto_tab[proto], cp);
  599. mutex_unlock(&proto_tab_lock);
  600. if (err < 0)
  601. proto_unregister(cp->prot);
  602. return err;
  603. }
  604. EXPORT_SYMBOL(can_proto_register);
  605. /**
  606. * can_proto_unregister - unregister CAN transport protocol
  607. * @cp: pointer to CAN protocol structure
  608. */
  609. void can_proto_unregister(const struct can_proto *cp)
  610. {
  611. int proto = cp->protocol;
  612. mutex_lock(&proto_tab_lock);
  613. BUG_ON(proto_tab[proto] != cp);
  614. RCU_INIT_POINTER(proto_tab[proto], NULL);
  615. mutex_unlock(&proto_tab_lock);
  616. synchronize_rcu();
  617. proto_unregister(cp->prot);
  618. }
  619. EXPORT_SYMBOL(can_proto_unregister);
  620. /*
  621. * af_can notifier to create/remove CAN netdevice specific structs
  622. */
  623. static int can_notifier(struct notifier_block *nb, unsigned long msg,
  624. void *data)
  625. {
  626. struct net_device *dev = (struct net_device *)data;
  627. struct dev_rcv_lists *d;
  628. if (!net_eq(dev_net(dev), &init_net))
  629. return NOTIFY_DONE;
  630. if (dev->type != ARPHRD_CAN)
  631. return NOTIFY_DONE;
  632. switch (msg) {
  633. case NETDEV_REGISTER:
  634. /* create new dev_rcv_lists for this device */
  635. d = kzalloc(sizeof(*d), GFP_KERNEL);
  636. if (!d) {
  637. printk(KERN_ERR
  638. "can: allocation of receive list failed\n");
  639. return NOTIFY_DONE;
  640. }
  641. BUG_ON(dev->ml_priv);
  642. dev->ml_priv = d;
  643. break;
  644. case NETDEV_UNREGISTER:
  645. spin_lock(&can_rcvlists_lock);
  646. d = dev->ml_priv;
  647. if (d) {
  648. if (d->entries)
  649. d->remove_on_zero_entries = 1;
  650. else {
  651. kfree(d);
  652. dev->ml_priv = NULL;
  653. }
  654. } else
  655. printk(KERN_ERR "can: notifier: receive list not "
  656. "found for dev %s\n", dev->name);
  657. spin_unlock(&can_rcvlists_lock);
  658. break;
  659. }
  660. return NOTIFY_DONE;
  661. }
  662. /*
  663. * af_can module init/exit functions
  664. */
  665. static struct packet_type can_packet __read_mostly = {
  666. .type = cpu_to_be16(ETH_P_CAN),
  667. .dev = NULL,
  668. .func = can_rcv,
  669. };
  670. static const struct net_proto_family can_family_ops = {
  671. .family = PF_CAN,
  672. .create = can_create,
  673. .owner = THIS_MODULE,
  674. };
  675. /* notifier block for netdevice event */
  676. static struct notifier_block can_netdev_notifier __read_mostly = {
  677. .notifier_call = can_notifier,
  678. };
  679. static __init int can_init(void)
  680. {
  681. printk(banner);
  682. memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list));
  683. rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
  684. 0, 0, NULL);
  685. if (!rcv_cache)
  686. return -ENOMEM;
  687. if (stats_timer) {
  688. /* the statistics are updated every second (timer triggered) */
  689. setup_timer(&can_stattimer, can_stat_update, 0);
  690. mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
  691. } else
  692. can_stattimer.function = NULL;
  693. can_init_proc();
  694. /* protocol register */
  695. sock_register(&can_family_ops);
  696. register_netdevice_notifier(&can_netdev_notifier);
  697. dev_add_pack(&can_packet);
  698. return 0;
  699. }
  700. static __exit void can_exit(void)
  701. {
  702. struct net_device *dev;
  703. if (stats_timer)
  704. del_timer_sync(&can_stattimer);
  705. can_remove_proc();
  706. /* protocol unregister */
  707. dev_remove_pack(&can_packet);
  708. unregister_netdevice_notifier(&can_netdev_notifier);
  709. sock_unregister(PF_CAN);
  710. /* remove created dev_rcv_lists from still registered CAN devices */
  711. rcu_read_lock();
  712. for_each_netdev_rcu(&init_net, dev) {
  713. if (dev->type == ARPHRD_CAN && dev->ml_priv){
  714. struct dev_rcv_lists *d = dev->ml_priv;
  715. BUG_ON(d->entries);
  716. kfree(d);
  717. dev->ml_priv = NULL;
  718. }
  719. }
  720. rcu_read_unlock();
  721. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  722. kmem_cache_destroy(rcv_cache);
  723. }
  724. module_init(can_init);
  725. module_exit(can_exit);