bcm.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
  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/init.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/hrtimer.h>
  46. #include <linux/list.h>
  47. #include <linux/proc_fs.h>
  48. #include <linux/seq_file.h>
  49. #include <linux/uio.h>
  50. #include <linux/net.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/socket.h>
  53. #include <linux/if_arp.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/can.h>
  56. #include <linux/can/core.h>
  57. #include <linux/can/skb.h>
  58. #include <linux/can/bcm.h>
  59. #include <linux/slab.h>
  60. #include <linux/spinlock.h>
  61. #include <net/sock.h>
  62. #include <net/net_namespace.h>
  63. /*
  64. * To send multiple CAN frame content within TX_SETUP or to filter
  65. * CAN messages with multiplex index within RX_SETUP, the number of
  66. * different filters is limited to 256 due to the one byte index value.
  67. */
  68. #define MAX_NFRAMES 256
  69. /* limit timers to 400 days for sending/timeouts */
  70. #define BCM_TIMER_SEC_MAX (400 * 24 * 60 * 60)
  71. /* use of last_frames[index].flags */
  72. #define RX_LOCAL 0x10 /* frame was created on the local host */
  73. #define RX_OWN 0x20 /* frame was sent via the socket it was received on */
  74. #define RX_RECV 0x40 /* received data for this element */
  75. #define RX_THR 0x80 /* element not been sent due to throttle feature */
  76. #define BCM_CAN_FLAGS_MASK 0x0F /* to clean private flags after usage */
  77. /* get best masking value for can_rx_register() for a given single can_id */
  78. #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
  79. (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
  80. (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
  81. MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
  82. MODULE_LICENSE("Dual BSD/GPL");
  83. MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  84. MODULE_ALIAS("can-proto-2");
  85. #define BCM_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
  86. /*
  87. * easy access to the first 64 bit of can(fd)_frame payload. cp->data is
  88. * 64 bit aligned so the offset has to be multiples of 8 which is ensured
  89. * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler().
  90. */
  91. static inline u64 get_u64(const struct canfd_frame *cp, int offset)
  92. {
  93. return *(u64 *)(cp->data + offset);
  94. }
  95. struct bcm_op {
  96. struct list_head list;
  97. struct rcu_head rcu;
  98. int ifindex;
  99. canid_t can_id;
  100. u32 flags;
  101. unsigned long frames_abs, frames_filtered;
  102. struct bcm_timeval ival1, ival2;
  103. struct hrtimer timer, thrtimer;
  104. ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
  105. int rx_ifindex;
  106. int cfsiz;
  107. u32 count;
  108. u32 nframes;
  109. u32 currframe;
  110. /* void pointers to arrays of struct can[fd]_frame */
  111. void *frames;
  112. void *last_frames;
  113. struct canfd_frame sframe;
  114. struct canfd_frame last_sframe;
  115. struct sock *sk;
  116. struct net_device *rx_reg_dev;
  117. spinlock_t bcm_tx_lock; /* protect currframe/count in runtime updates */
  118. };
  119. struct bcm_sock {
  120. struct sock sk;
  121. int bound;
  122. int ifindex;
  123. struct list_head notifier;
  124. struct list_head rx_ops;
  125. struct list_head tx_ops;
  126. unsigned long dropped_usr_msgs;
  127. struct proc_dir_entry *bcm_proc_read;
  128. char procname [32]; /* inode number in decimal with \0 */
  129. };
  130. static LIST_HEAD(bcm_notifier_list);
  131. static DEFINE_SPINLOCK(bcm_notifier_lock);
  132. static struct bcm_sock *bcm_busy_notifier;
  133. /* Return pointer to store the extra msg flags for bcm_recvmsg().
  134. * We use the space of one unsigned int beyond the 'struct sockaddr_can'
  135. * in skb->cb.
  136. */
  137. static inline unsigned int *bcm_flags(struct sk_buff *skb)
  138. {
  139. /* return pointer after struct sockaddr_can */
  140. return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
  141. }
  142. static inline struct bcm_sock *bcm_sk(const struct sock *sk)
  143. {
  144. return (struct bcm_sock *)sk;
  145. }
  146. static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
  147. {
  148. return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
  149. }
  150. /* check limitations for timeval provided by user */
  151. static bool bcm_is_invalid_tv(struct bcm_msg_head *msg_head)
  152. {
  153. if ((msg_head->ival1.tv_sec < 0) ||
  154. (msg_head->ival1.tv_sec > BCM_TIMER_SEC_MAX) ||
  155. (msg_head->ival1.tv_usec < 0) ||
  156. (msg_head->ival1.tv_usec >= USEC_PER_SEC) ||
  157. (msg_head->ival2.tv_sec < 0) ||
  158. (msg_head->ival2.tv_sec > BCM_TIMER_SEC_MAX) ||
  159. (msg_head->ival2.tv_usec < 0) ||
  160. (msg_head->ival2.tv_usec >= USEC_PER_SEC))
  161. return true;
  162. return false;
  163. }
  164. #define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
  165. #define OPSIZ sizeof(struct bcm_op)
  166. #define MHSIZ sizeof(struct bcm_msg_head)
  167. /*
  168. * procfs functions
  169. */
  170. #if IS_ENABLED(CONFIG_PROC_FS)
  171. static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
  172. {
  173. struct net_device *dev;
  174. if (!ifindex)
  175. return "any";
  176. rcu_read_lock();
  177. dev = dev_get_by_index_rcu(net, ifindex);
  178. if (dev)
  179. strcpy(result, dev->name);
  180. else
  181. strcpy(result, "???");
  182. rcu_read_unlock();
  183. return result;
  184. }
  185. static int bcm_proc_show(struct seq_file *m, void *v)
  186. {
  187. char ifname[IFNAMSIZ];
  188. struct net *net = m->private;
  189. struct sock *sk = (struct sock *)pde_data(m->file->f_inode);
  190. struct bcm_sock *bo = bcm_sk(sk);
  191. struct bcm_op *op;
  192. seq_printf(m, ">>> socket %pK", sk->sk_socket);
  193. seq_printf(m, " / sk %pK", sk);
  194. seq_printf(m, " / bo %pK", bo);
  195. seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
  196. seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
  197. seq_printf(m, " <<<\n");
  198. rcu_read_lock();
  199. list_for_each_entry_rcu(op, &bo->rx_ops, list) {
  200. unsigned long reduction;
  201. /* print only active entries & prevent division by zero */
  202. if (!op->frames_abs)
  203. continue;
  204. seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
  205. bcm_proc_getifname(net, ifname, op->ifindex));
  206. if (op->flags & CAN_FD_FRAME)
  207. seq_printf(m, "(%u)", op->nframes);
  208. else
  209. seq_printf(m, "[%u]", op->nframes);
  210. seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' ');
  211. if (op->kt_ival1)
  212. seq_printf(m, "timeo=%lld ",
  213. (long long)ktime_to_us(op->kt_ival1));
  214. if (op->kt_ival2)
  215. seq_printf(m, "thr=%lld ",
  216. (long long)ktime_to_us(op->kt_ival2));
  217. seq_printf(m, "# recv %ld (%ld) => reduction: ",
  218. op->frames_filtered, op->frames_abs);
  219. reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
  220. seq_printf(m, "%s%ld%%\n",
  221. (reduction == 100) ? "near " : "", reduction);
  222. }
  223. list_for_each_entry(op, &bo->tx_ops, list) {
  224. seq_printf(m, "tx_op: %03X %s ", op->can_id,
  225. bcm_proc_getifname(net, ifname, op->ifindex));
  226. if (op->flags & CAN_FD_FRAME)
  227. seq_printf(m, "(%u) ", op->nframes);
  228. else
  229. seq_printf(m, "[%u] ", op->nframes);
  230. if (op->kt_ival1)
  231. seq_printf(m, "t1=%lld ",
  232. (long long)ktime_to_us(op->kt_ival1));
  233. if (op->kt_ival2)
  234. seq_printf(m, "t2=%lld ",
  235. (long long)ktime_to_us(op->kt_ival2));
  236. seq_printf(m, "# sent %ld\n", op->frames_abs);
  237. }
  238. seq_putc(m, '\n');
  239. rcu_read_unlock();
  240. return 0;
  241. }
  242. #endif /* CONFIG_PROC_FS */
  243. /*
  244. * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
  245. * of the given bcm tx op
  246. */
  247. static void bcm_can_tx(struct bcm_op *op)
  248. {
  249. struct sk_buff *skb;
  250. struct net_device *dev;
  251. struct canfd_frame *cf;
  252. int err;
  253. /* no target device? => exit */
  254. if (!op->ifindex)
  255. return;
  256. /* read currframe under lock protection */
  257. spin_lock_bh(&op->bcm_tx_lock);
  258. cf = op->frames + op->cfsiz * op->currframe;
  259. spin_unlock_bh(&op->bcm_tx_lock);
  260. dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
  261. if (!dev) {
  262. /* RFC: should this bcm_op remove itself here? */
  263. return;
  264. }
  265. skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
  266. if (!skb)
  267. goto out;
  268. can_skb_reserve(skb);
  269. can_skb_prv(skb)->ifindex = dev->ifindex;
  270. can_skb_prv(skb)->skbcnt = 0;
  271. skb_put_data(skb, cf, op->cfsiz);
  272. /* send with loopback */
  273. skb->dev = dev;
  274. can_skb_set_owner(skb, op->sk);
  275. err = can_send(skb, 1);
  276. /* update currframe and count under lock protection */
  277. spin_lock_bh(&op->bcm_tx_lock);
  278. if (!err)
  279. op->frames_abs++;
  280. op->currframe++;
  281. /* reached last frame? */
  282. if (op->currframe >= op->nframes)
  283. op->currframe = 0;
  284. if (op->count > 0)
  285. op->count--;
  286. spin_unlock_bh(&op->bcm_tx_lock);
  287. out:
  288. dev_put(dev);
  289. }
  290. /*
  291. * bcm_send_to_user - send a BCM message to the userspace
  292. * (consisting of bcm_msg_head + x CAN frames)
  293. */
  294. static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
  295. struct canfd_frame *frames, int has_timestamp)
  296. {
  297. struct sk_buff *skb;
  298. struct canfd_frame *firstframe;
  299. struct sockaddr_can *addr;
  300. struct sock *sk = op->sk;
  301. unsigned int datalen = head->nframes * op->cfsiz;
  302. int err;
  303. unsigned int *pflags;
  304. skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
  305. if (!skb)
  306. return;
  307. skb_put_data(skb, head, sizeof(*head));
  308. /* ensure space for sockaddr_can and msg flags */
  309. sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
  310. sizeof(unsigned int));
  311. /* initialize msg flags */
  312. pflags = bcm_flags(skb);
  313. *pflags = 0;
  314. if (head->nframes) {
  315. /* CAN frames starting here */
  316. firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
  317. skb_put_data(skb, frames, datalen);
  318. /*
  319. * the BCM uses the flags-element of the canfd_frame
  320. * structure for internal purposes. This is only
  321. * relevant for updates that are generated by the
  322. * BCM, where nframes is 1
  323. */
  324. if (head->nframes == 1) {
  325. if (firstframe->flags & RX_LOCAL)
  326. *pflags |= MSG_DONTROUTE;
  327. if (firstframe->flags & RX_OWN)
  328. *pflags |= MSG_CONFIRM;
  329. firstframe->flags &= BCM_CAN_FLAGS_MASK;
  330. }
  331. }
  332. if (has_timestamp) {
  333. /* restore rx timestamp */
  334. skb->tstamp = op->rx_stamp;
  335. }
  336. /*
  337. * Put the datagram to the queue so that bcm_recvmsg() can
  338. * get it from there. We need to pass the interface index to
  339. * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
  340. * containing the interface index.
  341. */
  342. addr = (struct sockaddr_can *)skb->cb;
  343. memset(addr, 0, sizeof(*addr));
  344. addr->can_family = AF_CAN;
  345. addr->can_ifindex = op->rx_ifindex;
  346. err = sock_queue_rcv_skb(sk, skb);
  347. if (err < 0) {
  348. struct bcm_sock *bo = bcm_sk(sk);
  349. kfree_skb(skb);
  350. /* don't care about overflows in this statistic */
  351. bo->dropped_usr_msgs++;
  352. }
  353. }
  354. static bool bcm_tx_set_expiry(struct bcm_op *op, struct hrtimer *hrt)
  355. {
  356. ktime_t ival;
  357. if (op->kt_ival1 && op->count)
  358. ival = op->kt_ival1;
  359. else if (op->kt_ival2)
  360. ival = op->kt_ival2;
  361. else
  362. return false;
  363. hrtimer_set_expires(hrt, ktime_add(ktime_get(), ival));
  364. return true;
  365. }
  366. static void bcm_tx_start_timer(struct bcm_op *op)
  367. {
  368. if (bcm_tx_set_expiry(op, &op->timer))
  369. hrtimer_start_expires(&op->timer, HRTIMER_MODE_ABS_SOFT);
  370. }
  371. /* bcm_tx_timeout_handler - performs cyclic CAN frame transmissions */
  372. static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
  373. {
  374. struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
  375. struct bcm_msg_head msg_head;
  376. if (op->kt_ival1 && (op->count > 0)) {
  377. bcm_can_tx(op);
  378. if (!op->count && (op->flags & TX_COUNTEVT)) {
  379. /* create notification to user */
  380. memset(&msg_head, 0, sizeof(msg_head));
  381. msg_head.opcode = TX_EXPIRED;
  382. msg_head.flags = op->flags;
  383. msg_head.count = op->count;
  384. msg_head.ival1 = op->ival1;
  385. msg_head.ival2 = op->ival2;
  386. msg_head.can_id = op->can_id;
  387. msg_head.nframes = 0;
  388. bcm_send_to_user(op, &msg_head, NULL, 0);
  389. }
  390. } else if (op->kt_ival2) {
  391. bcm_can_tx(op);
  392. }
  393. return bcm_tx_set_expiry(op, &op->timer) ?
  394. HRTIMER_RESTART : HRTIMER_NORESTART;
  395. }
  396. /*
  397. * bcm_rx_changed - create a RX_CHANGED notification due to changed content
  398. */
  399. static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
  400. {
  401. struct bcm_msg_head head;
  402. /* update statistics */
  403. op->frames_filtered++;
  404. /* prevent statistics overflow */
  405. if (op->frames_filtered > ULONG_MAX/100)
  406. op->frames_filtered = op->frames_abs = 0;
  407. /* this element is not throttled anymore */
  408. data->flags &= ~RX_THR;
  409. memset(&head, 0, sizeof(head));
  410. head.opcode = RX_CHANGED;
  411. head.flags = op->flags;
  412. head.count = op->count;
  413. head.ival1 = op->ival1;
  414. head.ival2 = op->ival2;
  415. head.can_id = op->can_id;
  416. head.nframes = 1;
  417. bcm_send_to_user(op, &head, data, 1);
  418. }
  419. /*
  420. * bcm_rx_update_and_send - process a detected relevant receive content change
  421. * 1. update the last received data
  422. * 2. send a notification to the user (if possible)
  423. */
  424. static void bcm_rx_update_and_send(struct bcm_op *op,
  425. struct canfd_frame *lastdata,
  426. const struct canfd_frame *rxdata,
  427. unsigned char traffic_flags)
  428. {
  429. memcpy(lastdata, rxdata, op->cfsiz);
  430. /* mark as used and throttled by default */
  431. lastdata->flags |= (RX_RECV|RX_THR);
  432. /* add own/local/remote traffic flags */
  433. lastdata->flags |= traffic_flags;
  434. /* throttling mode inactive ? */
  435. if (!op->kt_ival2) {
  436. /* send RX_CHANGED to the user immediately */
  437. bcm_rx_changed(op, lastdata);
  438. return;
  439. }
  440. /* with active throttling timer we are just done here */
  441. if (hrtimer_active(&op->thrtimer))
  442. return;
  443. /* first reception with enabled throttling mode */
  444. if (!op->kt_lastmsg)
  445. goto rx_changed_settime;
  446. /* got a second frame inside a potential throttle period? */
  447. if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
  448. ktime_to_us(op->kt_ival2)) {
  449. /* do not send the saved data - only start throttle timer */
  450. hrtimer_start(&op->thrtimer,
  451. ktime_add(op->kt_lastmsg, op->kt_ival2),
  452. HRTIMER_MODE_ABS_SOFT);
  453. return;
  454. }
  455. /* the gap was that big, that throttling was not needed here */
  456. rx_changed_settime:
  457. bcm_rx_changed(op, lastdata);
  458. op->kt_lastmsg = ktime_get();
  459. }
  460. /*
  461. * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
  462. * received data stored in op->last_frames[]
  463. */
  464. static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
  465. const struct canfd_frame *rxdata,
  466. unsigned char traffic_flags)
  467. {
  468. struct canfd_frame *cf = op->frames + op->cfsiz * index;
  469. struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
  470. int i;
  471. /*
  472. * no one uses the MSBs of flags for comparison,
  473. * so we use it here to detect the first time of reception
  474. */
  475. if (!(lcf->flags & RX_RECV)) {
  476. /* received data for the first time => send update to user */
  477. bcm_rx_update_and_send(op, lcf, rxdata, traffic_flags);
  478. return;
  479. }
  480. /* do a real check in CAN frame data section */
  481. for (i = 0; i < rxdata->len; i += 8) {
  482. if ((get_u64(cf, i) & get_u64(rxdata, i)) !=
  483. (get_u64(cf, i) & get_u64(lcf, i))) {
  484. bcm_rx_update_and_send(op, lcf, rxdata, traffic_flags);
  485. return;
  486. }
  487. }
  488. if (op->flags & RX_CHECK_DLC) {
  489. /* do a real check in CAN frame length */
  490. if (rxdata->len != lcf->len) {
  491. bcm_rx_update_and_send(op, lcf, rxdata, traffic_flags);
  492. return;
  493. }
  494. }
  495. }
  496. /*
  497. * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
  498. */
  499. static void bcm_rx_starttimer(struct bcm_op *op)
  500. {
  501. if (op->flags & RX_NO_AUTOTIMER)
  502. return;
  503. if (op->kt_ival1)
  504. hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL_SOFT);
  505. }
  506. /* bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out */
  507. static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
  508. {
  509. struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
  510. struct bcm_msg_head msg_head;
  511. /* if user wants to be informed, when cyclic CAN-Messages come back */
  512. if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
  513. /* clear received CAN frames to indicate 'nothing received' */
  514. memset(op->last_frames, 0, op->nframes * op->cfsiz);
  515. }
  516. /* create notification to user */
  517. memset(&msg_head, 0, sizeof(msg_head));
  518. msg_head.opcode = RX_TIMEOUT;
  519. msg_head.flags = op->flags;
  520. msg_head.count = op->count;
  521. msg_head.ival1 = op->ival1;
  522. msg_head.ival2 = op->ival2;
  523. msg_head.can_id = op->can_id;
  524. msg_head.nframes = 0;
  525. bcm_send_to_user(op, &msg_head, NULL, 0);
  526. return HRTIMER_NORESTART;
  527. }
  528. /*
  529. * bcm_rx_do_flush - helper for bcm_rx_thr_flush
  530. */
  531. static inline int bcm_rx_do_flush(struct bcm_op *op, unsigned int index)
  532. {
  533. struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
  534. if ((op->last_frames) && (lcf->flags & RX_THR)) {
  535. bcm_rx_changed(op, lcf);
  536. return 1;
  537. }
  538. return 0;
  539. }
  540. /*
  541. * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
  542. */
  543. static int bcm_rx_thr_flush(struct bcm_op *op)
  544. {
  545. int updated = 0;
  546. if (op->nframes > 1) {
  547. unsigned int i;
  548. /* for MUX filter we start at index 1 */
  549. for (i = 1; i < op->nframes; i++)
  550. updated += bcm_rx_do_flush(op, i);
  551. } else {
  552. /* for RX_FILTER_ID and simple filter */
  553. updated += bcm_rx_do_flush(op, 0);
  554. }
  555. return updated;
  556. }
  557. /*
  558. * bcm_rx_thr_handler - the time for blocked content updates is over now:
  559. * Check for throttled data and send it to the userspace
  560. */
  561. static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
  562. {
  563. struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
  564. if (bcm_rx_thr_flush(op)) {
  565. hrtimer_forward_now(hrtimer, op->kt_ival2);
  566. return HRTIMER_RESTART;
  567. } else {
  568. /* rearm throttle handling */
  569. op->kt_lastmsg = 0;
  570. return HRTIMER_NORESTART;
  571. }
  572. }
  573. /*
  574. * bcm_rx_handler - handle a CAN frame reception
  575. */
  576. static void bcm_rx_handler(struct sk_buff *skb, void *data)
  577. {
  578. struct bcm_op *op = (struct bcm_op *)data;
  579. const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
  580. unsigned int i;
  581. unsigned char traffic_flags;
  582. if (op->can_id != rxframe->can_id)
  583. return;
  584. /* make sure to handle the correct frame type (CAN / CAN FD) */
  585. if (op->flags & CAN_FD_FRAME) {
  586. if (!can_is_canfd_skb(skb))
  587. return;
  588. } else {
  589. if (!can_is_can_skb(skb))
  590. return;
  591. }
  592. /* disable timeout */
  593. hrtimer_cancel(&op->timer);
  594. /* save rx timestamp */
  595. op->rx_stamp = skb->tstamp;
  596. /* save originator for recvfrom() */
  597. op->rx_ifindex = skb->dev->ifindex;
  598. /* update statistics */
  599. op->frames_abs++;
  600. if (op->flags & RX_RTR_FRAME) {
  601. /* send reply for RTR-request (placed in op->frames[0]) */
  602. bcm_can_tx(op);
  603. return;
  604. }
  605. /* compute flags to distinguish between own/local/remote CAN traffic */
  606. traffic_flags = 0;
  607. if (skb->sk) {
  608. traffic_flags |= RX_LOCAL;
  609. if (skb->sk == op->sk)
  610. traffic_flags |= RX_OWN;
  611. }
  612. if (op->flags & RX_FILTER_ID) {
  613. /* the easiest case */
  614. bcm_rx_update_and_send(op, op->last_frames, rxframe,
  615. traffic_flags);
  616. goto rx_starttimer;
  617. }
  618. if (op->nframes == 1) {
  619. /* simple compare with index 0 */
  620. bcm_rx_cmp_to_index(op, 0, rxframe, traffic_flags);
  621. goto rx_starttimer;
  622. }
  623. if (op->nframes > 1) {
  624. /*
  625. * multiplex compare
  626. *
  627. * find the first multiplex mask that fits.
  628. * Remark: The MUX-mask is stored in index 0 - but only the
  629. * first 64 bits of the frame data[] are relevant (CAN FD)
  630. */
  631. for (i = 1; i < op->nframes; i++) {
  632. if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
  633. (get_u64(op->frames, 0) &
  634. get_u64(op->frames + op->cfsiz * i, 0))) {
  635. bcm_rx_cmp_to_index(op, i, rxframe,
  636. traffic_flags);
  637. break;
  638. }
  639. }
  640. }
  641. rx_starttimer:
  642. bcm_rx_starttimer(op);
  643. }
  644. /*
  645. * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
  646. */
  647. static struct bcm_op *bcm_find_op(struct list_head *ops,
  648. struct bcm_msg_head *mh, int ifindex)
  649. {
  650. struct bcm_op *op;
  651. list_for_each_entry(op, ops, list) {
  652. if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
  653. (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
  654. return op;
  655. }
  656. return NULL;
  657. }
  658. static void bcm_free_op_rcu(struct rcu_head *rcu_head)
  659. {
  660. struct bcm_op *op = container_of(rcu_head, struct bcm_op, rcu);
  661. if ((op->frames) && (op->frames != &op->sframe))
  662. kfree(op->frames);
  663. if ((op->last_frames) && (op->last_frames != &op->last_sframe))
  664. kfree(op->last_frames);
  665. kfree(op);
  666. }
  667. static void bcm_remove_op(struct bcm_op *op)
  668. {
  669. hrtimer_cancel(&op->timer);
  670. hrtimer_cancel(&op->thrtimer);
  671. call_rcu(&op->rcu, bcm_free_op_rcu);
  672. }
  673. static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
  674. {
  675. if (op->rx_reg_dev == dev) {
  676. can_rx_unregister(dev_net(dev), dev, op->can_id,
  677. REGMASK(op->can_id), bcm_rx_handler, op);
  678. /* mark as removed subscription */
  679. op->rx_reg_dev = NULL;
  680. } else
  681. printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
  682. "mismatch %p %p\n", op->rx_reg_dev, dev);
  683. }
  684. /*
  685. * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
  686. */
  687. static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
  688. int ifindex)
  689. {
  690. struct bcm_op *op, *n;
  691. list_for_each_entry_safe(op, n, ops, list) {
  692. if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
  693. (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
  694. /* disable automatic timer on frame reception */
  695. op->flags |= RX_NO_AUTOTIMER;
  696. /*
  697. * Don't care if we're bound or not (due to netdev
  698. * problems) can_rx_unregister() is always a save
  699. * thing to do here.
  700. */
  701. if (op->ifindex) {
  702. /*
  703. * Only remove subscriptions that had not
  704. * been removed due to NETDEV_UNREGISTER
  705. * in bcm_notifier()
  706. */
  707. if (op->rx_reg_dev) {
  708. struct net_device *dev;
  709. dev = dev_get_by_index(sock_net(op->sk),
  710. op->ifindex);
  711. if (dev) {
  712. bcm_rx_unreg(dev, op);
  713. dev_put(dev);
  714. }
  715. }
  716. } else
  717. can_rx_unregister(sock_net(op->sk), NULL,
  718. op->can_id,
  719. REGMASK(op->can_id),
  720. bcm_rx_handler, op);
  721. list_del_rcu(&op->list);
  722. bcm_remove_op(op);
  723. return 1; /* done */
  724. }
  725. }
  726. return 0; /* not found */
  727. }
  728. /*
  729. * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
  730. */
  731. static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
  732. int ifindex)
  733. {
  734. struct bcm_op *op, *n;
  735. list_for_each_entry_safe(op, n, ops, list) {
  736. if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
  737. (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
  738. list_del_rcu(&op->list);
  739. bcm_remove_op(op);
  740. return 1; /* done */
  741. }
  742. }
  743. return 0; /* not found */
  744. }
  745. /*
  746. * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
  747. */
  748. static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
  749. int ifindex)
  750. {
  751. struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
  752. if (!op)
  753. return -EINVAL;
  754. /* put current values into msg_head */
  755. msg_head->flags = op->flags;
  756. msg_head->count = op->count;
  757. msg_head->ival1 = op->ival1;
  758. msg_head->ival2 = op->ival2;
  759. msg_head->nframes = op->nframes;
  760. bcm_send_to_user(op, msg_head, op->frames, 0);
  761. return MHSIZ;
  762. }
  763. /*
  764. * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
  765. */
  766. static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  767. int ifindex, struct sock *sk)
  768. {
  769. struct bcm_sock *bo = bcm_sk(sk);
  770. struct bcm_op *op;
  771. struct canfd_frame *cf;
  772. unsigned int i;
  773. int err;
  774. /* we need a real device to send frames */
  775. if (!ifindex)
  776. return -ENODEV;
  777. /* check nframes boundaries - we need at least one CAN frame */
  778. if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
  779. return -EINVAL;
  780. /* check timeval limitations */
  781. if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
  782. return -EINVAL;
  783. /* check the given can_id */
  784. op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
  785. if (op) {
  786. /* update existing BCM operation */
  787. /*
  788. * Do we need more space for the CAN frames than currently
  789. * allocated? -> This is a _really_ unusual use-case and
  790. * therefore (complexity / locking) it is not supported.
  791. */
  792. if (msg_head->nframes > op->nframes)
  793. return -E2BIG;
  794. /* update CAN frames content */
  795. for (i = 0; i < msg_head->nframes; i++) {
  796. cf = op->frames + op->cfsiz * i;
  797. err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
  798. if (op->flags & CAN_FD_FRAME) {
  799. if (cf->len > 64)
  800. err = -EINVAL;
  801. } else {
  802. if (cf->len > 8)
  803. err = -EINVAL;
  804. }
  805. if (err < 0)
  806. return err;
  807. if (msg_head->flags & TX_CP_CAN_ID) {
  808. /* copy can_id into frame */
  809. cf->can_id = msg_head->can_id;
  810. }
  811. }
  812. op->flags = msg_head->flags;
  813. /* only lock for unlikely count/nframes/currframe changes */
  814. if (op->nframes != msg_head->nframes ||
  815. op->flags & TX_RESET_MULTI_IDX ||
  816. op->flags & SETTIMER) {
  817. spin_lock_bh(&op->bcm_tx_lock);
  818. if (op->nframes != msg_head->nframes ||
  819. op->flags & TX_RESET_MULTI_IDX) {
  820. /* potentially update changed nframes */
  821. op->nframes = msg_head->nframes;
  822. /* restart multiple frame transmission */
  823. op->currframe = 0;
  824. }
  825. if (op->flags & SETTIMER)
  826. op->count = msg_head->count;
  827. spin_unlock_bh(&op->bcm_tx_lock);
  828. }
  829. } else {
  830. /* insert new BCM operation for the given can_id */
  831. op = kzalloc(OPSIZ, GFP_KERNEL);
  832. if (!op)
  833. return -ENOMEM;
  834. spin_lock_init(&op->bcm_tx_lock);
  835. op->can_id = msg_head->can_id;
  836. op->cfsiz = CFSIZ(msg_head->flags);
  837. op->flags = msg_head->flags;
  838. op->nframes = msg_head->nframes;
  839. if (op->flags & SETTIMER)
  840. op->count = msg_head->count;
  841. /* create array for CAN frames and copy the data */
  842. if (msg_head->nframes > 1) {
  843. op->frames = kmalloc_array(msg_head->nframes,
  844. op->cfsiz,
  845. GFP_KERNEL);
  846. if (!op->frames) {
  847. kfree(op);
  848. return -ENOMEM;
  849. }
  850. } else
  851. op->frames = &op->sframe;
  852. for (i = 0; i < msg_head->nframes; i++) {
  853. cf = op->frames + op->cfsiz * i;
  854. err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
  855. if (err < 0)
  856. goto free_op;
  857. if (op->flags & CAN_FD_FRAME) {
  858. if (cf->len > 64)
  859. err = -EINVAL;
  860. } else {
  861. if (cf->len > 8)
  862. err = -EINVAL;
  863. }
  864. if (err < 0)
  865. goto free_op;
  866. if (msg_head->flags & TX_CP_CAN_ID) {
  867. /* copy can_id into frame */
  868. cf->can_id = msg_head->can_id;
  869. }
  870. }
  871. /* tx_ops never compare with previous received messages */
  872. op->last_frames = NULL;
  873. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  874. op->sk = sk;
  875. op->ifindex = ifindex;
  876. /* initialize uninitialized (kzalloc) structure */
  877. hrtimer_init(&op->timer, CLOCK_MONOTONIC,
  878. HRTIMER_MODE_REL_SOFT);
  879. op->timer.function = bcm_tx_timeout_handler;
  880. /* currently unused in tx_ops */
  881. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC,
  882. HRTIMER_MODE_REL_SOFT);
  883. /* add this bcm_op to the list of the tx_ops */
  884. list_add(&op->list, &bo->tx_ops);
  885. } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
  886. if (op->flags & SETTIMER) {
  887. /* set timer values */
  888. op->ival1 = msg_head->ival1;
  889. op->ival2 = msg_head->ival2;
  890. op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
  891. op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
  892. /* disable an active timer due to zero values? */
  893. if (!op->kt_ival1 && !op->kt_ival2)
  894. hrtimer_cancel(&op->timer);
  895. }
  896. if (op->flags & STARTTIMER) {
  897. hrtimer_cancel(&op->timer);
  898. /* spec: send CAN frame when starting timer */
  899. op->flags |= TX_ANNOUNCE;
  900. }
  901. if (op->flags & TX_ANNOUNCE)
  902. bcm_can_tx(op);
  903. if (op->flags & STARTTIMER)
  904. bcm_tx_start_timer(op);
  905. return msg_head->nframes * op->cfsiz + MHSIZ;
  906. free_op:
  907. if (op->frames != &op->sframe)
  908. kfree(op->frames);
  909. kfree(op);
  910. return err;
  911. }
  912. /*
  913. * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
  914. */
  915. static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  916. int ifindex, struct sock *sk)
  917. {
  918. struct bcm_sock *bo = bcm_sk(sk);
  919. struct bcm_op *op;
  920. int do_rx_register;
  921. int err = 0;
  922. if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
  923. /* be robust against wrong usage ... */
  924. msg_head->flags |= RX_FILTER_ID;
  925. /* ignore trailing garbage */
  926. msg_head->nframes = 0;
  927. }
  928. /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
  929. if (msg_head->nframes > MAX_NFRAMES + 1)
  930. return -EINVAL;
  931. if ((msg_head->flags & RX_RTR_FRAME) &&
  932. ((msg_head->nframes != 1) ||
  933. (!(msg_head->can_id & CAN_RTR_FLAG))))
  934. return -EINVAL;
  935. /* check timeval limitations */
  936. if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
  937. return -EINVAL;
  938. /* check the given can_id */
  939. op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
  940. if (op) {
  941. /* update existing BCM operation */
  942. /*
  943. * Do we need more space for the CAN frames than currently
  944. * allocated? -> This is a _really_ unusual use-case and
  945. * therefore (complexity / locking) it is not supported.
  946. */
  947. if (msg_head->nframes > op->nframes)
  948. return -E2BIG;
  949. if (msg_head->nframes) {
  950. /* update CAN frames content */
  951. err = memcpy_from_msg(op->frames, msg,
  952. msg_head->nframes * op->cfsiz);
  953. if (err < 0)
  954. return err;
  955. /* clear last_frames to indicate 'nothing received' */
  956. memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
  957. }
  958. op->nframes = msg_head->nframes;
  959. op->flags = msg_head->flags;
  960. /* Only an update -> do not call can_rx_register() */
  961. do_rx_register = 0;
  962. } else {
  963. /* insert new BCM operation for the given can_id */
  964. op = kzalloc(OPSIZ, GFP_KERNEL);
  965. if (!op)
  966. return -ENOMEM;
  967. op->can_id = msg_head->can_id;
  968. op->nframes = msg_head->nframes;
  969. op->cfsiz = CFSIZ(msg_head->flags);
  970. op->flags = msg_head->flags;
  971. if (msg_head->nframes > 1) {
  972. /* create array for CAN frames and copy the data */
  973. op->frames = kmalloc_array(msg_head->nframes,
  974. op->cfsiz,
  975. GFP_KERNEL);
  976. if (!op->frames) {
  977. kfree(op);
  978. return -ENOMEM;
  979. }
  980. /* create and init array for received CAN frames */
  981. op->last_frames = kcalloc(msg_head->nframes,
  982. op->cfsiz,
  983. GFP_KERNEL);
  984. if (!op->last_frames) {
  985. kfree(op->frames);
  986. kfree(op);
  987. return -ENOMEM;
  988. }
  989. } else {
  990. op->frames = &op->sframe;
  991. op->last_frames = &op->last_sframe;
  992. }
  993. if (msg_head->nframes) {
  994. err = memcpy_from_msg(op->frames, msg,
  995. msg_head->nframes * op->cfsiz);
  996. if (err < 0) {
  997. if (op->frames != &op->sframe)
  998. kfree(op->frames);
  999. if (op->last_frames != &op->last_sframe)
  1000. kfree(op->last_frames);
  1001. kfree(op);
  1002. return err;
  1003. }
  1004. }
  1005. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  1006. op->sk = sk;
  1007. op->ifindex = ifindex;
  1008. /* ifindex for timeout events w/o previous frame reception */
  1009. op->rx_ifindex = ifindex;
  1010. /* initialize uninitialized (kzalloc) structure */
  1011. hrtimer_init(&op->timer, CLOCK_MONOTONIC,
  1012. HRTIMER_MODE_REL_SOFT);
  1013. op->timer.function = bcm_rx_timeout_handler;
  1014. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC,
  1015. HRTIMER_MODE_REL_SOFT);
  1016. op->thrtimer.function = bcm_rx_thr_handler;
  1017. /* add this bcm_op to the list of the rx_ops */
  1018. list_add(&op->list, &bo->rx_ops);
  1019. /* call can_rx_register() */
  1020. do_rx_register = 1;
  1021. } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
  1022. /* check flags */
  1023. if (op->flags & RX_RTR_FRAME) {
  1024. struct canfd_frame *frame0 = op->frames;
  1025. /* no timers in RTR-mode */
  1026. hrtimer_cancel(&op->thrtimer);
  1027. hrtimer_cancel(&op->timer);
  1028. /*
  1029. * funny feature in RX(!)_SETUP only for RTR-mode:
  1030. * copy can_id into frame BUT without RTR-flag to
  1031. * prevent a full-load-loopback-test ... ;-]
  1032. */
  1033. if ((op->flags & TX_CP_CAN_ID) ||
  1034. (frame0->can_id == op->can_id))
  1035. frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
  1036. } else {
  1037. if (op->flags & SETTIMER) {
  1038. /* set timer value */
  1039. op->ival1 = msg_head->ival1;
  1040. op->ival2 = msg_head->ival2;
  1041. op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
  1042. op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
  1043. /* disable an active timer due to zero value? */
  1044. if (!op->kt_ival1)
  1045. hrtimer_cancel(&op->timer);
  1046. /*
  1047. * In any case cancel the throttle timer, flush
  1048. * potentially blocked msgs and reset throttle handling
  1049. */
  1050. op->kt_lastmsg = 0;
  1051. hrtimer_cancel(&op->thrtimer);
  1052. bcm_rx_thr_flush(op);
  1053. }
  1054. if ((op->flags & STARTTIMER) && op->kt_ival1)
  1055. hrtimer_start(&op->timer, op->kt_ival1,
  1056. HRTIMER_MODE_REL_SOFT);
  1057. }
  1058. /* now we can register for can_ids, if we added a new bcm_op */
  1059. if (do_rx_register) {
  1060. if (ifindex) {
  1061. struct net_device *dev;
  1062. dev = dev_get_by_index(sock_net(sk), ifindex);
  1063. if (dev) {
  1064. err = can_rx_register(sock_net(sk), dev,
  1065. op->can_id,
  1066. REGMASK(op->can_id),
  1067. bcm_rx_handler, op,
  1068. "bcm", sk);
  1069. op->rx_reg_dev = dev;
  1070. dev_put(dev);
  1071. }
  1072. } else
  1073. err = can_rx_register(sock_net(sk), NULL, op->can_id,
  1074. REGMASK(op->can_id),
  1075. bcm_rx_handler, op, "bcm", sk);
  1076. if (err) {
  1077. /* this bcm rx op is broken -> remove it */
  1078. list_del_rcu(&op->list);
  1079. bcm_remove_op(op);
  1080. return err;
  1081. }
  1082. }
  1083. return msg_head->nframes * op->cfsiz + MHSIZ;
  1084. }
  1085. /*
  1086. * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
  1087. */
  1088. static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
  1089. int cfsiz)
  1090. {
  1091. struct sk_buff *skb;
  1092. struct net_device *dev;
  1093. int err;
  1094. /* we need a real device to send frames */
  1095. if (!ifindex)
  1096. return -ENODEV;
  1097. skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
  1098. if (!skb)
  1099. return -ENOMEM;
  1100. can_skb_reserve(skb);
  1101. err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
  1102. if (err < 0) {
  1103. kfree_skb(skb);
  1104. return err;
  1105. }
  1106. dev = dev_get_by_index(sock_net(sk), ifindex);
  1107. if (!dev) {
  1108. kfree_skb(skb);
  1109. return -ENODEV;
  1110. }
  1111. can_skb_prv(skb)->ifindex = dev->ifindex;
  1112. can_skb_prv(skb)->skbcnt = 0;
  1113. skb->dev = dev;
  1114. can_skb_set_owner(skb, sk);
  1115. err = can_send(skb, 1); /* send with loopback */
  1116. dev_put(dev);
  1117. if (err)
  1118. return err;
  1119. return cfsiz + MHSIZ;
  1120. }
  1121. /*
  1122. * bcm_sendmsg - process BCM commands (opcodes) from the userspace
  1123. */
  1124. static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  1125. {
  1126. struct sock *sk = sock->sk;
  1127. struct bcm_sock *bo = bcm_sk(sk);
  1128. int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
  1129. struct bcm_msg_head msg_head;
  1130. int cfsiz;
  1131. int ret; /* read bytes or error codes as return value */
  1132. if (!bo->bound)
  1133. return -ENOTCONN;
  1134. /* check for valid message length from userspace */
  1135. if (size < MHSIZ)
  1136. return -EINVAL;
  1137. /* read message head information */
  1138. ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
  1139. if (ret < 0)
  1140. return ret;
  1141. cfsiz = CFSIZ(msg_head.flags);
  1142. if ((size - MHSIZ) % cfsiz)
  1143. return -EINVAL;
  1144. /* check for alternative ifindex for this bcm_op */
  1145. if (!ifindex && msg->msg_name) {
  1146. /* no bound device as default => check msg_name */
  1147. DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
  1148. if (msg->msg_namelen < BCM_MIN_NAMELEN)
  1149. return -EINVAL;
  1150. if (addr->can_family != AF_CAN)
  1151. return -EINVAL;
  1152. /* ifindex from sendto() */
  1153. ifindex = addr->can_ifindex;
  1154. if (ifindex) {
  1155. struct net_device *dev;
  1156. dev = dev_get_by_index(sock_net(sk), ifindex);
  1157. if (!dev)
  1158. return -ENODEV;
  1159. if (dev->type != ARPHRD_CAN) {
  1160. dev_put(dev);
  1161. return -ENODEV;
  1162. }
  1163. dev_put(dev);
  1164. }
  1165. }
  1166. lock_sock(sk);
  1167. switch (msg_head.opcode) {
  1168. case TX_SETUP:
  1169. ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
  1170. break;
  1171. case RX_SETUP:
  1172. ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
  1173. break;
  1174. case TX_DELETE:
  1175. if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
  1176. ret = MHSIZ;
  1177. else
  1178. ret = -EINVAL;
  1179. break;
  1180. case RX_DELETE:
  1181. if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
  1182. ret = MHSIZ;
  1183. else
  1184. ret = -EINVAL;
  1185. break;
  1186. case TX_READ:
  1187. /* reuse msg_head for the reply to TX_READ */
  1188. msg_head.opcode = TX_STATUS;
  1189. ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
  1190. break;
  1191. case RX_READ:
  1192. /* reuse msg_head for the reply to RX_READ */
  1193. msg_head.opcode = RX_STATUS;
  1194. ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
  1195. break;
  1196. case TX_SEND:
  1197. /* we need exactly one CAN frame behind the msg head */
  1198. if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
  1199. ret = -EINVAL;
  1200. else
  1201. ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
  1202. break;
  1203. default:
  1204. ret = -EINVAL;
  1205. break;
  1206. }
  1207. release_sock(sk);
  1208. return ret;
  1209. }
  1210. /*
  1211. * notification handler for netdevice status changes
  1212. */
  1213. static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
  1214. struct net_device *dev)
  1215. {
  1216. struct sock *sk = &bo->sk;
  1217. struct bcm_op *op;
  1218. int notify_enodev = 0;
  1219. if (!net_eq(dev_net(dev), sock_net(sk)))
  1220. return;
  1221. switch (msg) {
  1222. case NETDEV_UNREGISTER:
  1223. lock_sock(sk);
  1224. /* remove device specific receive entries */
  1225. list_for_each_entry(op, &bo->rx_ops, list)
  1226. if (op->rx_reg_dev == dev)
  1227. bcm_rx_unreg(dev, op);
  1228. /* remove device reference, if this is our bound device */
  1229. if (bo->bound && bo->ifindex == dev->ifindex) {
  1230. #if IS_ENABLED(CONFIG_PROC_FS)
  1231. if (sock_net(sk)->can.bcmproc_dir && bo->bcm_proc_read) {
  1232. remove_proc_entry(bo->procname, sock_net(sk)->can.bcmproc_dir);
  1233. bo->bcm_proc_read = NULL;
  1234. }
  1235. #endif
  1236. bo->bound = 0;
  1237. bo->ifindex = 0;
  1238. notify_enodev = 1;
  1239. }
  1240. release_sock(sk);
  1241. if (notify_enodev) {
  1242. sk->sk_err = ENODEV;
  1243. if (!sock_flag(sk, SOCK_DEAD))
  1244. sk_error_report(sk);
  1245. }
  1246. break;
  1247. case NETDEV_DOWN:
  1248. if (bo->bound && bo->ifindex == dev->ifindex) {
  1249. sk->sk_err = ENETDOWN;
  1250. if (!sock_flag(sk, SOCK_DEAD))
  1251. sk_error_report(sk);
  1252. }
  1253. }
  1254. }
  1255. static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
  1256. void *ptr)
  1257. {
  1258. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1259. if (dev->type != ARPHRD_CAN)
  1260. return NOTIFY_DONE;
  1261. if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
  1262. return NOTIFY_DONE;
  1263. if (unlikely(bcm_busy_notifier)) /* Check for reentrant bug. */
  1264. return NOTIFY_DONE;
  1265. spin_lock(&bcm_notifier_lock);
  1266. list_for_each_entry(bcm_busy_notifier, &bcm_notifier_list, notifier) {
  1267. spin_unlock(&bcm_notifier_lock);
  1268. bcm_notify(bcm_busy_notifier, msg, dev);
  1269. spin_lock(&bcm_notifier_lock);
  1270. }
  1271. bcm_busy_notifier = NULL;
  1272. spin_unlock(&bcm_notifier_lock);
  1273. return NOTIFY_DONE;
  1274. }
  1275. /*
  1276. * initial settings for all BCM sockets to be set at socket creation time
  1277. */
  1278. static int bcm_init(struct sock *sk)
  1279. {
  1280. struct bcm_sock *bo = bcm_sk(sk);
  1281. bo->bound = 0;
  1282. bo->ifindex = 0;
  1283. bo->dropped_usr_msgs = 0;
  1284. bo->bcm_proc_read = NULL;
  1285. INIT_LIST_HEAD(&bo->tx_ops);
  1286. INIT_LIST_HEAD(&bo->rx_ops);
  1287. /* set notifier */
  1288. spin_lock(&bcm_notifier_lock);
  1289. list_add_tail(&bo->notifier, &bcm_notifier_list);
  1290. spin_unlock(&bcm_notifier_lock);
  1291. return 0;
  1292. }
  1293. /*
  1294. * standard socket functions
  1295. */
  1296. static int bcm_release(struct socket *sock)
  1297. {
  1298. struct sock *sk = sock->sk;
  1299. struct net *net;
  1300. struct bcm_sock *bo;
  1301. struct bcm_op *op, *next;
  1302. if (!sk)
  1303. return 0;
  1304. net = sock_net(sk);
  1305. bo = bcm_sk(sk);
  1306. /* remove bcm_ops, timer, rx_unregister(), etc. */
  1307. spin_lock(&bcm_notifier_lock);
  1308. while (bcm_busy_notifier == bo) {
  1309. spin_unlock(&bcm_notifier_lock);
  1310. schedule_timeout_uninterruptible(1);
  1311. spin_lock(&bcm_notifier_lock);
  1312. }
  1313. list_del(&bo->notifier);
  1314. spin_unlock(&bcm_notifier_lock);
  1315. lock_sock(sk);
  1316. #if IS_ENABLED(CONFIG_PROC_FS)
  1317. /* remove procfs entry */
  1318. if (net->can.bcmproc_dir && bo->bcm_proc_read)
  1319. remove_proc_entry(bo->procname, net->can.bcmproc_dir);
  1320. #endif /* CONFIG_PROC_FS */
  1321. list_for_each_entry_safe(op, next, &bo->tx_ops, list)
  1322. bcm_remove_op(op);
  1323. list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
  1324. /*
  1325. * Don't care if we're bound or not (due to netdev problems)
  1326. * can_rx_unregister() is always a save thing to do here.
  1327. */
  1328. if (op->ifindex) {
  1329. /*
  1330. * Only remove subscriptions that had not
  1331. * been removed due to NETDEV_UNREGISTER
  1332. * in bcm_notifier()
  1333. */
  1334. if (op->rx_reg_dev) {
  1335. struct net_device *dev;
  1336. dev = dev_get_by_index(net, op->ifindex);
  1337. if (dev) {
  1338. bcm_rx_unreg(dev, op);
  1339. dev_put(dev);
  1340. }
  1341. }
  1342. } else
  1343. can_rx_unregister(net, NULL, op->can_id,
  1344. REGMASK(op->can_id),
  1345. bcm_rx_handler, op);
  1346. }
  1347. synchronize_rcu();
  1348. list_for_each_entry_safe(op, next, &bo->rx_ops, list)
  1349. bcm_remove_op(op);
  1350. /* remove device reference */
  1351. if (bo->bound) {
  1352. bo->bound = 0;
  1353. bo->ifindex = 0;
  1354. }
  1355. sock_orphan(sk);
  1356. sock->sk = NULL;
  1357. release_sock(sk);
  1358. sock_put(sk);
  1359. return 0;
  1360. }
  1361. static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
  1362. int flags)
  1363. {
  1364. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  1365. struct sock *sk = sock->sk;
  1366. struct bcm_sock *bo = bcm_sk(sk);
  1367. struct net *net = sock_net(sk);
  1368. int ret = 0;
  1369. if (len < BCM_MIN_NAMELEN)
  1370. return -EINVAL;
  1371. lock_sock(sk);
  1372. if (bo->bound) {
  1373. ret = -EISCONN;
  1374. goto fail;
  1375. }
  1376. /* bind a device to this socket */
  1377. if (addr->can_ifindex) {
  1378. struct net_device *dev;
  1379. dev = dev_get_by_index(net, addr->can_ifindex);
  1380. if (!dev) {
  1381. ret = -ENODEV;
  1382. goto fail;
  1383. }
  1384. if (dev->type != ARPHRD_CAN) {
  1385. dev_put(dev);
  1386. ret = -ENODEV;
  1387. goto fail;
  1388. }
  1389. bo->ifindex = dev->ifindex;
  1390. dev_put(dev);
  1391. } else {
  1392. /* no interface reference for ifindex = 0 ('any' CAN device) */
  1393. bo->ifindex = 0;
  1394. }
  1395. #if IS_ENABLED(CONFIG_PROC_FS)
  1396. if (net->can.bcmproc_dir) {
  1397. /* unique socket address as filename */
  1398. sprintf(bo->procname, "%lu", sock_i_ino(sk));
  1399. bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644,
  1400. net->can.bcmproc_dir,
  1401. bcm_proc_show, sk);
  1402. if (!bo->bcm_proc_read) {
  1403. ret = -ENOMEM;
  1404. goto fail;
  1405. }
  1406. }
  1407. #endif /* CONFIG_PROC_FS */
  1408. bo->bound = 1;
  1409. fail:
  1410. release_sock(sk);
  1411. return ret;
  1412. }
  1413. static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  1414. int flags)
  1415. {
  1416. struct sock *sk = sock->sk;
  1417. struct sk_buff *skb;
  1418. int error = 0;
  1419. int err;
  1420. skb = skb_recv_datagram(sk, flags, &error);
  1421. if (!skb)
  1422. return error;
  1423. if (skb->len < size)
  1424. size = skb->len;
  1425. err = memcpy_to_msg(msg, skb->data, size);
  1426. if (err < 0) {
  1427. skb_free_datagram(sk, skb);
  1428. return err;
  1429. }
  1430. sock_recv_cmsgs(msg, sk, skb);
  1431. if (msg->msg_name) {
  1432. __sockaddr_check_size(BCM_MIN_NAMELEN);
  1433. msg->msg_namelen = BCM_MIN_NAMELEN;
  1434. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  1435. }
  1436. /* assign the flags that have been recorded in bcm_send_to_user() */
  1437. msg->msg_flags |= *(bcm_flags(skb));
  1438. skb_free_datagram(sk, skb);
  1439. return size;
  1440. }
  1441. static int bcm_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
  1442. unsigned long arg)
  1443. {
  1444. /* no ioctls for socket layer -> hand it down to NIC layer */
  1445. return -ENOIOCTLCMD;
  1446. }
  1447. static const struct proto_ops bcm_ops = {
  1448. .family = PF_CAN,
  1449. .release = bcm_release,
  1450. .bind = sock_no_bind,
  1451. .connect = bcm_connect,
  1452. .socketpair = sock_no_socketpair,
  1453. .accept = sock_no_accept,
  1454. .getname = sock_no_getname,
  1455. .poll = datagram_poll,
  1456. .ioctl = bcm_sock_no_ioctlcmd,
  1457. .gettstamp = sock_gettstamp,
  1458. .listen = sock_no_listen,
  1459. .shutdown = sock_no_shutdown,
  1460. .sendmsg = bcm_sendmsg,
  1461. .recvmsg = bcm_recvmsg,
  1462. .mmap = sock_no_mmap,
  1463. };
  1464. static struct proto bcm_proto __read_mostly = {
  1465. .name = "CAN_BCM",
  1466. .owner = THIS_MODULE,
  1467. .obj_size = sizeof(struct bcm_sock),
  1468. .init = bcm_init,
  1469. };
  1470. static const struct can_proto bcm_can_proto = {
  1471. .type = SOCK_DGRAM,
  1472. .protocol = CAN_BCM,
  1473. .ops = &bcm_ops,
  1474. .prot = &bcm_proto,
  1475. };
  1476. static int canbcm_pernet_init(struct net *net)
  1477. {
  1478. #if IS_ENABLED(CONFIG_PROC_FS)
  1479. /* create /proc/net/can-bcm directory */
  1480. net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net);
  1481. #endif /* CONFIG_PROC_FS */
  1482. return 0;
  1483. }
  1484. static void canbcm_pernet_exit(struct net *net)
  1485. {
  1486. #if IS_ENABLED(CONFIG_PROC_FS)
  1487. /* remove /proc/net/can-bcm directory */
  1488. if (net->can.bcmproc_dir)
  1489. remove_proc_entry("can-bcm", net->proc_net);
  1490. #endif /* CONFIG_PROC_FS */
  1491. }
  1492. static struct pernet_operations canbcm_pernet_ops __read_mostly = {
  1493. .init = canbcm_pernet_init,
  1494. .exit = canbcm_pernet_exit,
  1495. };
  1496. static struct notifier_block canbcm_notifier = {
  1497. .notifier_call = bcm_notifier
  1498. };
  1499. static int __init bcm_module_init(void)
  1500. {
  1501. int err;
  1502. pr_info("can: broadcast manager protocol\n");
  1503. err = register_pernet_subsys(&canbcm_pernet_ops);
  1504. if (err)
  1505. return err;
  1506. err = register_netdevice_notifier(&canbcm_notifier);
  1507. if (err)
  1508. goto register_notifier_failed;
  1509. err = can_proto_register(&bcm_can_proto);
  1510. if (err < 0) {
  1511. printk(KERN_ERR "can: registration of bcm protocol failed\n");
  1512. goto register_proto_failed;
  1513. }
  1514. return 0;
  1515. register_proto_failed:
  1516. unregister_netdevice_notifier(&canbcm_notifier);
  1517. register_notifier_failed:
  1518. unregister_pernet_subsys(&canbcm_pernet_ops);
  1519. return err;
  1520. }
  1521. static void __exit bcm_module_exit(void)
  1522. {
  1523. can_proto_unregister(&bcm_can_proto);
  1524. unregister_netdevice_notifier(&canbcm_notifier);
  1525. unregister_pernet_subsys(&canbcm_pernet_ops);
  1526. }
  1527. module_init(bcm_module_init);
  1528. module_exit(bcm_module_exit);