bcm.c 42 KB

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