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