af_can.c 23 KB

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