af_can.c 23 KB

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