af_can.c 24 KB

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