bcm.c 45 KB

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