bcm.c 41 KB

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