bcm.c 41 KB

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