bcm.c 39 KB

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