af_can.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
  65. MODULE_LICENSE("Dual BSD/GPL");
  66. MODULE_AUTHOR("Urs Thuermann <[email protected]>, "
  67. "Oliver Hartkopp <[email protected]>");
  68. MODULE_ALIAS_NETPROTO(PF_CAN);
  69. static int stats_timer __read_mostly = 1;
  70. module_param(stats_timer, int, S_IRUGO);
  71. MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
  72. /* receive filters subscribed for 'all' CAN devices */
  73. struct dev_rcv_lists can_rx_alldev_list;
  74. static DEFINE_SPINLOCK(can_rcvlists_lock);
  75. static struct kmem_cache *rcv_cache __read_mostly;
  76. /* table of registered CAN protocols */
  77. static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
  78. static DEFINE_MUTEX(proto_tab_lock);
  79. struct timer_list can_stattimer; /* timer for statistics update */
  80. struct s_stats can_stats; /* packet statistics */
  81. struct s_pstats can_pstats; /* receive list statistics */
  82. static atomic_t skbcounter = ATOMIC_INIT(0);
  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. skb_queue_purge(&sk->sk_error_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, kern);
  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->ip_summed = CHECKSUM_UNNECESSARY;
  221. skb_reset_mac_header(skb);
  222. skb_reset_network_header(skb);
  223. skb_reset_transport_header(skb);
  224. if (loop) {
  225. /* local loopback of sent CAN frames */
  226. /* indication for the CAN driver: do loopback */
  227. skb->pkt_type = PACKET_LOOPBACK;
  228. /*
  229. * The reference to the originating sock may be required
  230. * by the receiving socket to check whether the frame is
  231. * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
  232. * Therefore we have to ensure that skb->sk remains the
  233. * reference to the originating sock by restoring skb->sk
  234. * after each skb_clone() or skb_orphan() usage.
  235. */
  236. if (!(skb->dev->flags & IFF_ECHO)) {
  237. /*
  238. * If the interface is not capable to do loopback
  239. * itself, we do it here.
  240. */
  241. newskb = skb_clone(skb, GFP_ATOMIC);
  242. if (!newskb) {
  243. kfree_skb(skb);
  244. return -ENOMEM;
  245. }
  246. can_skb_set_owner(newskb, skb->sk);
  247. newskb->ip_summed = CHECKSUM_UNNECESSARY;
  248. newskb->pkt_type = PACKET_BROADCAST;
  249. }
  250. } else {
  251. /* indication for the CAN driver: no loopback required */
  252. skb->pkt_type = PACKET_HOST;
  253. }
  254. /* send to netdevice */
  255. err = dev_queue_xmit(skb);
  256. if (err > 0)
  257. err = net_xmit_errno(err);
  258. if (err) {
  259. kfree_skb(newskb);
  260. return err;
  261. }
  262. if (newskb)
  263. netif_rx_ni(newskb);
  264. /* update statistics */
  265. can_stats.tx_frames++;
  266. can_stats.tx_frames_delta++;
  267. return 0;
  268. inval_skb:
  269. kfree_skb(skb);
  270. return err;
  271. }
  272. EXPORT_SYMBOL(can_send);
  273. /*
  274. * af_can rx path
  275. */
  276. static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
  277. {
  278. if (!dev)
  279. return &can_rx_alldev_list;
  280. else
  281. return (struct dev_rcv_lists *)dev->ml_priv;
  282. }
  283. /**
  284. * effhash - hash function for 29 bit CAN identifier reduction
  285. * @can_id: 29 bit CAN identifier
  286. *
  287. * Description:
  288. * To reduce the linear traversal in one linked list of _single_ EFF CAN
  289. * frame subscriptions the 29 bit identifier is mapped to 10 bits.
  290. * (see CAN_EFF_RCV_HASH_BITS definition)
  291. *
  292. * Return:
  293. * Hash value from 0x000 - 0x3FF ( enforced by CAN_EFF_RCV_HASH_BITS mask )
  294. */
  295. static unsigned int effhash(canid_t can_id)
  296. {
  297. unsigned int hash;
  298. hash = can_id;
  299. hash ^= can_id >> CAN_EFF_RCV_HASH_BITS;
  300. hash ^= can_id >> (2 * CAN_EFF_RCV_HASH_BITS);
  301. return hash & ((1 << CAN_EFF_RCV_HASH_BITS) - 1);
  302. }
  303. /**
  304. * find_rcv_list - determine optimal filterlist inside device filter struct
  305. * @can_id: pointer to CAN identifier of a given can_filter
  306. * @mask: pointer to CAN mask of a given can_filter
  307. * @d: pointer to the device filter struct
  308. *
  309. * Description:
  310. * Returns the optimal filterlist to reduce the filter handling in the
  311. * receive path. This function is called by service functions that need
  312. * to register or unregister a can_filter in the filter lists.
  313. *
  314. * A filter matches in general, when
  315. *
  316. * <received_can_id> & mask == can_id & mask
  317. *
  318. * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
  319. * relevant bits for the filter.
  320. *
  321. * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
  322. * filter for error messages (CAN_ERR_FLAG bit set in mask). For error msg
  323. * frames there is a special filterlist and a special rx path filter handling.
  324. *
  325. * Return:
  326. * Pointer to optimal filterlist for the given can_id/mask pair.
  327. * Constistency checked mask.
  328. * Reduced can_id to have a preprocessed filter compare value.
  329. */
  330. static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
  331. struct dev_rcv_lists *d)
  332. {
  333. canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
  334. /* filter for error message frames in extra filterlist */
  335. if (*mask & CAN_ERR_FLAG) {
  336. /* clear CAN_ERR_FLAG in filter entry */
  337. *mask &= CAN_ERR_MASK;
  338. return &d->rx[RX_ERR];
  339. }
  340. /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
  341. #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
  342. /* ensure valid values in can_mask for 'SFF only' frame filtering */
  343. if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
  344. *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
  345. /* reduce condition testing at receive time */
  346. *can_id &= *mask;
  347. /* inverse can_id/can_mask filter */
  348. if (inv)
  349. return &d->rx[RX_INV];
  350. /* mask == 0 => no condition testing at receive time */
  351. if (!(*mask))
  352. return &d->rx[RX_ALL];
  353. /* extra filterlists for the subscription of a single non-RTR can_id */
  354. if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
  355. !(*can_id & CAN_RTR_FLAG)) {
  356. if (*can_id & CAN_EFF_FLAG) {
  357. if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS))
  358. return &d->rx_eff[effhash(*can_id)];
  359. } else {
  360. if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
  361. return &d->rx_sff[*can_id];
  362. }
  363. }
  364. /* default: filter via can_id/can_mask */
  365. return &d->rx[RX_FIL];
  366. }
  367. /**
  368. * can_rx_register - subscribe CAN frames from a specific interface
  369. * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
  370. * @can_id: CAN identifier (see description)
  371. * @mask: CAN mask (see description)
  372. * @func: callback function on filter match
  373. * @data: returned parameter for callback function
  374. * @ident: string for calling module identification
  375. * @sk: socket pointer (might be NULL)
  376. *
  377. * Description:
  378. * Invokes the callback function with the received sk_buff and the given
  379. * parameter 'data' on a matching receive filter. A filter matches, when
  380. *
  381. * <received_can_id> & mask == can_id & mask
  382. *
  383. * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
  384. * filter for error message frames (CAN_ERR_FLAG bit set in mask).
  385. *
  386. * The provided pointer to the sk_buff is guaranteed to be valid as long as
  387. * the callback function is running. The callback function must *not* free
  388. * the given sk_buff while processing it's task. When the given sk_buff is
  389. * needed after the end of the callback function it must be cloned inside
  390. * the callback function with skb_clone().
  391. *
  392. * Return:
  393. * 0 on success
  394. * -ENOMEM on missing cache mem to create subscription entry
  395. * -ENODEV unknown device
  396. */
  397. int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
  398. void (*func)(struct sk_buff *, void *), void *data,
  399. char *ident, struct sock *sk)
  400. {
  401. struct receiver *r;
  402. struct hlist_head *rl;
  403. struct dev_rcv_lists *d;
  404. int err = 0;
  405. /* insert new receiver (dev,canid,mask) -> (func,data) */
  406. if (dev && dev->type != ARPHRD_CAN)
  407. return -ENODEV;
  408. r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
  409. if (!r)
  410. return -ENOMEM;
  411. spin_lock(&can_rcvlists_lock);
  412. d = find_dev_rcv_lists(dev);
  413. if (d) {
  414. rl = find_rcv_list(&can_id, &mask, d);
  415. r->can_id = can_id;
  416. r->mask = mask;
  417. r->matches = 0;
  418. r->func = func;
  419. r->data = data;
  420. r->ident = ident;
  421. r->sk = sk;
  422. hlist_add_head_rcu(&r->list, rl);
  423. d->entries++;
  424. can_pstats.rcv_entries++;
  425. if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
  426. can_pstats.rcv_entries_max = can_pstats.rcv_entries;
  427. } else {
  428. kmem_cache_free(rcv_cache, r);
  429. err = -ENODEV;
  430. }
  431. spin_unlock(&can_rcvlists_lock);
  432. return err;
  433. }
  434. EXPORT_SYMBOL(can_rx_register);
  435. /*
  436. * can_rx_delete_receiver - rcu callback for single receiver entry removal
  437. */
  438. static void can_rx_delete_receiver(struct rcu_head *rp)
  439. {
  440. struct receiver *r = container_of(rp, struct receiver, rcu);
  441. struct sock *sk = r->sk;
  442. kmem_cache_free(rcv_cache, r);
  443. if (sk)
  444. sock_put(sk);
  445. }
  446. /**
  447. * can_rx_unregister - unsubscribe CAN frames from a specific interface
  448. * @dev: pointer to netdevice (NULL => unsubscribe from 'all' CAN devices list)
  449. * @can_id: CAN identifier
  450. * @mask: CAN mask
  451. * @func: callback function on filter match
  452. * @data: returned parameter for callback function
  453. *
  454. * Description:
  455. * Removes subscription entry depending on given (subscription) values.
  456. */
  457. void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
  458. void (*func)(struct sk_buff *, void *), void *data)
  459. {
  460. struct receiver *r = NULL;
  461. struct hlist_head *rl;
  462. struct dev_rcv_lists *d;
  463. if (dev && dev->type != ARPHRD_CAN)
  464. return;
  465. spin_lock(&can_rcvlists_lock);
  466. d = find_dev_rcv_lists(dev);
  467. if (!d) {
  468. pr_err("BUG: receive list not found for "
  469. "dev %s, id %03X, mask %03X\n",
  470. DNAME(dev), can_id, mask);
  471. goto out;
  472. }
  473. rl = find_rcv_list(&can_id, &mask, d);
  474. /*
  475. * Search the receiver list for the item to delete. This should
  476. * exist, since no receiver may be unregistered that hasn't
  477. * been registered before.
  478. */
  479. hlist_for_each_entry_rcu(r, rl, list) {
  480. if (r->can_id == can_id && r->mask == mask &&
  481. r->func == func && r->data == data)
  482. break;
  483. }
  484. /*
  485. * Check for bugs in CAN protocol implementations using af_can.c:
  486. * 'r' will be NULL if no matching list item was found for removal.
  487. */
  488. if (!r) {
  489. WARN(1, "BUG: receive list entry not found for dev %s, "
  490. "id %03X, mask %03X\n", DNAME(dev), can_id, mask);
  491. goto out;
  492. }
  493. hlist_del_rcu(&r->list);
  494. d->entries--;
  495. if (can_pstats.rcv_entries > 0)
  496. can_pstats.rcv_entries--;
  497. /* remove device structure requested by NETDEV_UNREGISTER */
  498. if (d->remove_on_zero_entries && !d->entries) {
  499. kfree(d);
  500. dev->ml_priv = NULL;
  501. }
  502. out:
  503. spin_unlock(&can_rcvlists_lock);
  504. /* schedule the receiver item for deletion */
  505. if (r) {
  506. if (r->sk)
  507. sock_hold(r->sk);
  508. call_rcu(&r->rcu, can_rx_delete_receiver);
  509. }
  510. }
  511. EXPORT_SYMBOL(can_rx_unregister);
  512. static inline void deliver(struct sk_buff *skb, struct receiver *r)
  513. {
  514. r->func(skb, r->data);
  515. r->matches++;
  516. }
  517. static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
  518. {
  519. struct receiver *r;
  520. int matches = 0;
  521. struct can_frame *cf = (struct can_frame *)skb->data;
  522. canid_t can_id = cf->can_id;
  523. if (d->entries == 0)
  524. return 0;
  525. if (can_id & CAN_ERR_FLAG) {
  526. /* check for error message frame entries only */
  527. hlist_for_each_entry_rcu(r, &d->rx[RX_ERR], list) {
  528. if (can_id & r->mask) {
  529. deliver(skb, r);
  530. matches++;
  531. }
  532. }
  533. return matches;
  534. }
  535. /* check for unfiltered entries */
  536. hlist_for_each_entry_rcu(r, &d->rx[RX_ALL], list) {
  537. deliver(skb, r);
  538. matches++;
  539. }
  540. /* check for can_id/mask entries */
  541. hlist_for_each_entry_rcu(r, &d->rx[RX_FIL], list) {
  542. if ((can_id & r->mask) == r->can_id) {
  543. deliver(skb, r);
  544. matches++;
  545. }
  546. }
  547. /* check for inverted can_id/mask entries */
  548. hlist_for_each_entry_rcu(r, &d->rx[RX_INV], list) {
  549. if ((can_id & r->mask) != r->can_id) {
  550. deliver(skb, r);
  551. matches++;
  552. }
  553. }
  554. /* check filterlists for single non-RTR can_ids */
  555. if (can_id & CAN_RTR_FLAG)
  556. return matches;
  557. if (can_id & CAN_EFF_FLAG) {
  558. hlist_for_each_entry_rcu(r, &d->rx_eff[effhash(can_id)], list) {
  559. if (r->can_id == can_id) {
  560. deliver(skb, r);
  561. matches++;
  562. }
  563. }
  564. } else {
  565. can_id &= CAN_SFF_MASK;
  566. hlist_for_each_entry_rcu(r, &d->rx_sff[can_id], list) {
  567. deliver(skb, r);
  568. matches++;
  569. }
  570. }
  571. return matches;
  572. }
  573. static void can_receive(struct sk_buff *skb, struct net_device *dev)
  574. {
  575. struct dev_rcv_lists *d;
  576. int matches;
  577. /* update statistics */
  578. can_stats.rx_frames++;
  579. can_stats.rx_frames_delta++;
  580. /* create non-zero unique skb identifier together with *skb */
  581. while (!(can_skb_prv(skb)->skbcnt))
  582. can_skb_prv(skb)->skbcnt = atomic_inc_return(&skbcounter);
  583. rcu_read_lock();
  584. /* deliver the packet to sockets listening on all devices */
  585. matches = can_rcv_filter(&can_rx_alldev_list, skb);
  586. /* find receive list for this device */
  587. d = find_dev_rcv_lists(dev);
  588. if (d)
  589. matches += can_rcv_filter(d, skb);
  590. rcu_read_unlock();
  591. /* consume the skbuff allocated by the netdevice driver */
  592. consume_skb(skb);
  593. if (matches > 0) {
  594. can_stats.matches++;
  595. can_stats.matches_delta++;
  596. }
  597. }
  598. static int can_rcv(struct sk_buff *skb, struct net_device *dev,
  599. struct packet_type *pt, struct net_device *orig_dev)
  600. {
  601. struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  602. if (unlikely(!net_eq(dev_net(dev), &init_net)))
  603. goto drop;
  604. if (unlikely(dev->type != ARPHRD_CAN || skb->len != CAN_MTU ||
  605. cfd->len > CAN_MAX_DLEN)) {
  606. pr_warn_once("PF_CAN: dropped non conform CAN skbuf: dev type %d, len %d, datalen %d\n",
  607. dev->type, skb->len, cfd->len);
  608. goto drop;
  609. }
  610. can_receive(skb, dev);
  611. return NET_RX_SUCCESS;
  612. drop:
  613. kfree_skb(skb);
  614. return NET_RX_DROP;
  615. }
  616. static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
  617. struct packet_type *pt, struct net_device *orig_dev)
  618. {
  619. struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  620. if (unlikely(!net_eq(dev_net(dev), &init_net)))
  621. goto drop;
  622. if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU ||
  623. cfd->len > CANFD_MAX_DLEN)) {
  624. pr_warn_once("PF_CAN: dropped non conform CAN FD skbuf: dev type %d, len %d, datalen %d\n",
  625. dev->type, skb->len, cfd->len);
  626. goto drop;
  627. }
  628. can_receive(skb, dev);
  629. return NET_RX_SUCCESS;
  630. drop:
  631. kfree_skb(skb);
  632. return NET_RX_DROP;
  633. }
  634. /*
  635. * af_can protocol functions
  636. */
  637. /**
  638. * can_proto_register - register CAN transport protocol
  639. * @cp: pointer to CAN protocol structure
  640. *
  641. * Return:
  642. * 0 on success
  643. * -EINVAL invalid (out of range) protocol number
  644. * -EBUSY protocol already in use
  645. * -ENOBUF if proto_register() fails
  646. */
  647. int can_proto_register(const struct can_proto *cp)
  648. {
  649. int proto = cp->protocol;
  650. int err = 0;
  651. if (proto < 0 || proto >= CAN_NPROTO) {
  652. pr_err("can: protocol number %d out of range\n", proto);
  653. return -EINVAL;
  654. }
  655. err = proto_register(cp->prot, 0);
  656. if (err < 0)
  657. return err;
  658. mutex_lock(&proto_tab_lock);
  659. if (proto_tab[proto]) {
  660. pr_err("can: protocol %d already registered\n", proto);
  661. err = -EBUSY;
  662. } else
  663. RCU_INIT_POINTER(proto_tab[proto], cp);
  664. mutex_unlock(&proto_tab_lock);
  665. if (err < 0)
  666. proto_unregister(cp->prot);
  667. return err;
  668. }
  669. EXPORT_SYMBOL(can_proto_register);
  670. /**
  671. * can_proto_unregister - unregister CAN transport protocol
  672. * @cp: pointer to CAN protocol structure
  673. */
  674. void can_proto_unregister(const struct can_proto *cp)
  675. {
  676. int proto = cp->protocol;
  677. mutex_lock(&proto_tab_lock);
  678. BUG_ON(proto_tab[proto] != cp);
  679. RCU_INIT_POINTER(proto_tab[proto], NULL);
  680. mutex_unlock(&proto_tab_lock);
  681. synchronize_rcu();
  682. proto_unregister(cp->prot);
  683. }
  684. EXPORT_SYMBOL(can_proto_unregister);
  685. /*
  686. * af_can notifier to create/remove CAN netdevice specific structs
  687. */
  688. static int can_notifier(struct notifier_block *nb, unsigned long msg,
  689. void *ptr)
  690. {
  691. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  692. struct dev_rcv_lists *d;
  693. if (!net_eq(dev_net(dev), &init_net))
  694. return NOTIFY_DONE;
  695. if (dev->type != ARPHRD_CAN)
  696. return NOTIFY_DONE;
  697. switch (msg) {
  698. case NETDEV_REGISTER:
  699. /* create new dev_rcv_lists for this device */
  700. d = kzalloc(sizeof(*d), GFP_KERNEL);
  701. if (!d)
  702. return NOTIFY_DONE;
  703. BUG_ON(dev->ml_priv);
  704. dev->ml_priv = d;
  705. break;
  706. case NETDEV_UNREGISTER:
  707. spin_lock(&can_rcvlists_lock);
  708. d = dev->ml_priv;
  709. if (d) {
  710. if (d->entries)
  711. d->remove_on_zero_entries = 1;
  712. else {
  713. kfree(d);
  714. dev->ml_priv = NULL;
  715. }
  716. } else
  717. pr_err("can: notifier: receive list not found for dev "
  718. "%s\n", dev->name);
  719. spin_unlock(&can_rcvlists_lock);
  720. break;
  721. }
  722. return NOTIFY_DONE;
  723. }
  724. /*
  725. * af_can module init/exit functions
  726. */
  727. static struct packet_type can_packet __read_mostly = {
  728. .type = cpu_to_be16(ETH_P_CAN),
  729. .func = can_rcv,
  730. };
  731. static struct packet_type canfd_packet __read_mostly = {
  732. .type = cpu_to_be16(ETH_P_CANFD),
  733. .func = canfd_rcv,
  734. };
  735. static const struct net_proto_family can_family_ops = {
  736. .family = PF_CAN,
  737. .create = can_create,
  738. .owner = THIS_MODULE,
  739. };
  740. /* notifier block for netdevice event */
  741. static struct notifier_block can_netdev_notifier __read_mostly = {
  742. .notifier_call = can_notifier,
  743. };
  744. static __init int can_init(void)
  745. {
  746. /* check for correct padding to be able to use the structs similarly */
  747. BUILD_BUG_ON(offsetof(struct can_frame, can_dlc) !=
  748. offsetof(struct canfd_frame, len) ||
  749. offsetof(struct can_frame, data) !=
  750. offsetof(struct canfd_frame, data));
  751. pr_info("can: controller area network core (" CAN_VERSION_STRING ")\n");
  752. memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list));
  753. rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
  754. 0, 0, NULL);
  755. if (!rcv_cache)
  756. return -ENOMEM;
  757. if (IS_ENABLED(CONFIG_PROC_FS)) {
  758. if (stats_timer) {
  759. /* the statistics are updated every second (timer triggered) */
  760. setup_timer(&can_stattimer, can_stat_update, 0);
  761. mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
  762. }
  763. can_init_proc();
  764. }
  765. /* protocol register */
  766. sock_register(&can_family_ops);
  767. register_netdevice_notifier(&can_netdev_notifier);
  768. dev_add_pack(&can_packet);
  769. dev_add_pack(&canfd_packet);
  770. return 0;
  771. }
  772. static __exit void can_exit(void)
  773. {
  774. struct net_device *dev;
  775. if (IS_ENABLED(CONFIG_PROC_FS)) {
  776. if (stats_timer)
  777. del_timer_sync(&can_stattimer);
  778. can_remove_proc();
  779. }
  780. /* protocol unregister */
  781. dev_remove_pack(&canfd_packet);
  782. dev_remove_pack(&can_packet);
  783. unregister_netdevice_notifier(&can_netdev_notifier);
  784. sock_unregister(PF_CAN);
  785. /* remove created dev_rcv_lists from still registered CAN devices */
  786. rcu_read_lock();
  787. for_each_netdev_rcu(&init_net, dev) {
  788. if (dev->type == ARPHRD_CAN && dev->ml_priv) {
  789. struct dev_rcv_lists *d = dev->ml_priv;
  790. BUG_ON(d->entries);
  791. kfree(d);
  792. dev->ml_priv = NULL;
  793. }
  794. }
  795. rcu_read_unlock();
  796. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  797. kmem_cache_destroy(rcv_cache);
  798. }
  799. module_init(can_init);
  800. module_exit(can_exit);