bcm.c 39 KB

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