af_can.c 24 KB

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