bcm.c 39 KB

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