af_can.c 22 KB

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