af_can.c 22 KB

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