af_can.c 25 KB

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