bcm.c 43 KB

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