af_can.c 21 KB

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