bcm.c 42 KB

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