af_can.c 21 KB

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