af_can.c 24 KB

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