bcm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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 [32]; /* inode number in decimal 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 %pK", sk->sk_socket);
  151. seq_printf(m, " / sk %pK", sk);
  152. seq_printf(m, " / bo %pK", 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_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. /* throtteling 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 receiption 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 comparation,
  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 receiption
  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 receiption 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 receiption
  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_fromiovec((u8 *)&op->frames[i],
  715. msg->msg_iov, CFSIZ);
  716. if (op->frames[i].can_dlc > 8)
  717. err = -EINVAL;
  718. if (err < 0)
  719. return err;
  720. if (msg_head->flags & TX_CP_CAN_ID) {
  721. /* copy can_id into frame */
  722. op->frames[i].can_id = msg_head->can_id;
  723. }
  724. }
  725. } else {
  726. /* insert new BCM operation for the given can_id */
  727. op = kzalloc(OPSIZ, GFP_KERNEL);
  728. if (!op)
  729. return -ENOMEM;
  730. op->can_id = msg_head->can_id;
  731. /* create array for can_frames and copy the data */
  732. if (msg_head->nframes > 1) {
  733. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  734. GFP_KERNEL);
  735. if (!op->frames) {
  736. kfree(op);
  737. return -ENOMEM;
  738. }
  739. } else
  740. op->frames = &op->sframe;
  741. for (i = 0; i < msg_head->nframes; i++) {
  742. err = memcpy_fromiovec((u8 *)&op->frames[i],
  743. msg->msg_iov, CFSIZ);
  744. if (op->frames[i].can_dlc > 8)
  745. err = -EINVAL;
  746. if (err < 0) {
  747. if (op->frames != &op->sframe)
  748. kfree(op->frames);
  749. kfree(op);
  750. return err;
  751. }
  752. if (msg_head->flags & TX_CP_CAN_ID) {
  753. /* copy can_id into frame */
  754. op->frames[i].can_id = msg_head->can_id;
  755. }
  756. }
  757. /* tx_ops never compare with previous received messages */
  758. op->last_frames = NULL;
  759. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  760. op->sk = sk;
  761. op->ifindex = ifindex;
  762. /* initialize uninitialized (kzalloc) structure */
  763. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  764. op->timer.function = bcm_tx_timeout_handler;
  765. /* initialize tasklet for tx countevent notification */
  766. tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
  767. (unsigned long) op);
  768. /* currently unused in tx_ops */
  769. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  770. /* add this bcm_op to the list of the tx_ops */
  771. list_add(&op->list, &bo->tx_ops);
  772. } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
  773. if (op->nframes != msg_head->nframes) {
  774. op->nframes = msg_head->nframes;
  775. /* start multiple frame transmission with index 0 */
  776. op->currframe = 0;
  777. }
  778. /* check flags */
  779. op->flags = msg_head->flags;
  780. if (op->flags & TX_RESET_MULTI_IDX) {
  781. /* start multiple frame transmission with index 0 */
  782. op->currframe = 0;
  783. }
  784. if (op->flags & SETTIMER) {
  785. /* set timer values */
  786. op->count = msg_head->count;
  787. op->ival1 = msg_head->ival1;
  788. op->ival2 = msg_head->ival2;
  789. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  790. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  791. /* disable an active timer due to zero values? */
  792. if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
  793. hrtimer_cancel(&op->timer);
  794. }
  795. if (op->flags & STARTTIMER) {
  796. hrtimer_cancel(&op->timer);
  797. /* spec: send can_frame when starting timer */
  798. op->flags |= TX_ANNOUNCE;
  799. }
  800. if (op->flags & TX_ANNOUNCE) {
  801. bcm_can_tx(op);
  802. if (op->count)
  803. op->count--;
  804. }
  805. if (op->flags & STARTTIMER)
  806. bcm_tx_start_timer(op);
  807. return msg_head->nframes * CFSIZ + MHSIZ;
  808. }
  809. /*
  810. * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
  811. */
  812. static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  813. int ifindex, struct sock *sk)
  814. {
  815. struct bcm_sock *bo = bcm_sk(sk);
  816. struct bcm_op *op;
  817. int do_rx_register;
  818. int err = 0;
  819. if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
  820. /* be robust against wrong usage ... */
  821. msg_head->flags |= RX_FILTER_ID;
  822. /* ignore trailing garbage */
  823. msg_head->nframes = 0;
  824. }
  825. /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
  826. if (msg_head->nframes > MAX_NFRAMES + 1)
  827. return -EINVAL;
  828. if ((msg_head->flags & RX_RTR_FRAME) &&
  829. ((msg_head->nframes != 1) ||
  830. (!(msg_head->can_id & CAN_RTR_FLAG))))
  831. return -EINVAL;
  832. /* check the given can_id */
  833. op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
  834. if (op) {
  835. /* update existing BCM operation */
  836. /*
  837. * Do we need more space for the can_frames than currently
  838. * allocated? -> This is a _really_ unusual use-case and
  839. * therefore (complexity / locking) it is not supported.
  840. */
  841. if (msg_head->nframes > op->nframes)
  842. return -E2BIG;
  843. if (msg_head->nframes) {
  844. /* update can_frames content */
  845. err = memcpy_fromiovec((u8 *)op->frames,
  846. msg->msg_iov,
  847. msg_head->nframes * CFSIZ);
  848. if (err < 0)
  849. return err;
  850. /* clear last_frames to indicate 'nothing received' */
  851. memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
  852. }
  853. op->nframes = msg_head->nframes;
  854. /* Only an update -> do not call can_rx_register() */
  855. do_rx_register = 0;
  856. } else {
  857. /* insert new BCM operation for the given can_id */
  858. op = kzalloc(OPSIZ, GFP_KERNEL);
  859. if (!op)
  860. return -ENOMEM;
  861. op->can_id = msg_head->can_id;
  862. op->nframes = msg_head->nframes;
  863. if (msg_head->nframes > 1) {
  864. /* create array for can_frames and copy the data */
  865. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  866. GFP_KERNEL);
  867. if (!op->frames) {
  868. kfree(op);
  869. return -ENOMEM;
  870. }
  871. /* create and init array for received can_frames */
  872. op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
  873. GFP_KERNEL);
  874. if (!op->last_frames) {
  875. kfree(op->frames);
  876. kfree(op);
  877. return -ENOMEM;
  878. }
  879. } else {
  880. op->frames = &op->sframe;
  881. op->last_frames = &op->last_sframe;
  882. }
  883. if (msg_head->nframes) {
  884. err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
  885. msg_head->nframes * CFSIZ);
  886. if (err < 0) {
  887. if (op->frames != &op->sframe)
  888. kfree(op->frames);
  889. if (op->last_frames != &op->last_sframe)
  890. kfree(op->last_frames);
  891. kfree(op);
  892. return err;
  893. }
  894. }
  895. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  896. op->sk = sk;
  897. op->ifindex = ifindex;
  898. /* initialize uninitialized (kzalloc) structure */
  899. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  900. op->timer.function = bcm_rx_timeout_handler;
  901. /* initialize tasklet for rx timeout notification */
  902. tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
  903. (unsigned long) op);
  904. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  905. op->thrtimer.function = bcm_rx_thr_handler;
  906. /* initialize tasklet for rx throttle handling */
  907. tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
  908. (unsigned long) op);
  909. /* add this bcm_op to the list of the rx_ops */
  910. list_add(&op->list, &bo->rx_ops);
  911. /* call can_rx_register() */
  912. do_rx_register = 1;
  913. } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
  914. /* check flags */
  915. op->flags = msg_head->flags;
  916. if (op->flags & RX_RTR_FRAME) {
  917. /* no timers in RTR-mode */
  918. hrtimer_cancel(&op->thrtimer);
  919. hrtimer_cancel(&op->timer);
  920. /*
  921. * funny feature in RX(!)_SETUP only for RTR-mode:
  922. * copy can_id into frame BUT without RTR-flag to
  923. * prevent a full-load-loopback-test ... ;-]
  924. */
  925. if ((op->flags & TX_CP_CAN_ID) ||
  926. (op->frames[0].can_id == op->can_id))
  927. op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
  928. } else {
  929. if (op->flags & SETTIMER) {
  930. /* set timer value */
  931. op->ival1 = msg_head->ival1;
  932. op->ival2 = msg_head->ival2;
  933. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  934. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  935. /* disable an active timer due to zero value? */
  936. if (!op->kt_ival1.tv64)
  937. hrtimer_cancel(&op->timer);
  938. /*
  939. * In any case cancel the throttle timer, flush
  940. * potentially blocked msgs and reset throttle handling
  941. */
  942. op->kt_lastmsg = ktime_set(0, 0);
  943. hrtimer_cancel(&op->thrtimer);
  944. bcm_rx_thr_flush(op, 1);
  945. }
  946. if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
  947. hrtimer_start(&op->timer, op->kt_ival1,
  948. HRTIMER_MODE_REL);
  949. }
  950. /* now we can register for can_ids, if we added a new bcm_op */
  951. if (do_rx_register) {
  952. if (ifindex) {
  953. struct net_device *dev;
  954. dev = dev_get_by_index(&init_net, ifindex);
  955. if (dev) {
  956. err = can_rx_register(dev, op->can_id,
  957. REGMASK(op->can_id),
  958. bcm_rx_handler, op,
  959. "bcm");
  960. op->rx_reg_dev = dev;
  961. dev_put(dev);
  962. }
  963. } else
  964. err = can_rx_register(NULL, op->can_id,
  965. REGMASK(op->can_id),
  966. bcm_rx_handler, op, "bcm");
  967. if (err) {
  968. /* this bcm rx op is broken -> remove it */
  969. list_del(&op->list);
  970. bcm_remove_op(op);
  971. return err;
  972. }
  973. }
  974. return msg_head->nframes * CFSIZ + MHSIZ;
  975. }
  976. /*
  977. * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
  978. */
  979. static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
  980. {
  981. struct sk_buff *skb;
  982. struct net_device *dev;
  983. int err;
  984. /* we need a real device to send frames */
  985. if (!ifindex)
  986. return -ENODEV;
  987. skb = alloc_skb(CFSIZ, GFP_KERNEL);
  988. if (!skb)
  989. return -ENOMEM;
  990. err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, 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. skb->dev = dev;
  1001. skb->sk = sk;
  1002. err = can_send(skb, 1); /* send with loopback */
  1003. dev_put(dev);
  1004. if (err)
  1005. return err;
  1006. return CFSIZ + MHSIZ;
  1007. }
  1008. /*
  1009. * bcm_sendmsg - process BCM commands (opcodes) from the userspace
  1010. */
  1011. static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
  1012. 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. struct sockaddr_can *addr =
  1028. (struct sockaddr_can *)msg->msg_name;
  1029. if (msg->msg_namelen < sizeof(*addr))
  1030. return -EINVAL;
  1031. if (addr->can_family != AF_CAN)
  1032. return -EINVAL;
  1033. /* ifindex from sendto() */
  1034. ifindex = addr->can_ifindex;
  1035. if (ifindex) {
  1036. struct net_device *dev;
  1037. dev = dev_get_by_index(&init_net, ifindex);
  1038. if (!dev)
  1039. return -ENODEV;
  1040. if (dev->type != ARPHRD_CAN) {
  1041. dev_put(dev);
  1042. return -ENODEV;
  1043. }
  1044. dev_put(dev);
  1045. }
  1046. }
  1047. /* read message head information */
  1048. ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
  1049. if (ret < 0)
  1050. return ret;
  1051. lock_sock(sk);
  1052. switch (msg_head.opcode) {
  1053. case TX_SETUP:
  1054. ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
  1055. break;
  1056. case RX_SETUP:
  1057. ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
  1058. break;
  1059. case TX_DELETE:
  1060. if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
  1061. ret = MHSIZ;
  1062. else
  1063. ret = -EINVAL;
  1064. break;
  1065. case RX_DELETE:
  1066. if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
  1067. ret = MHSIZ;
  1068. else
  1069. ret = -EINVAL;
  1070. break;
  1071. case TX_READ:
  1072. /* reuse msg_head for the reply to TX_READ */
  1073. msg_head.opcode = TX_STATUS;
  1074. ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
  1075. break;
  1076. case RX_READ:
  1077. /* reuse msg_head for the reply to RX_READ */
  1078. msg_head.opcode = RX_STATUS;
  1079. ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
  1080. break;
  1081. case TX_SEND:
  1082. /* we need exactly one can_frame behind the msg head */
  1083. if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
  1084. ret = -EINVAL;
  1085. else
  1086. ret = bcm_tx_send(msg, ifindex, sk);
  1087. break;
  1088. default:
  1089. ret = -EINVAL;
  1090. break;
  1091. }
  1092. release_sock(sk);
  1093. return ret;
  1094. }
  1095. /*
  1096. * notification handler for netdevice status changes
  1097. */
  1098. static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
  1099. void *data)
  1100. {
  1101. struct net_device *dev = (struct net_device *)data;
  1102. struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
  1103. struct sock *sk = &bo->sk;
  1104. struct bcm_op *op;
  1105. int notify_enodev = 0;
  1106. if (!net_eq(dev_net(dev), &init_net))
  1107. return NOTIFY_DONE;
  1108. if (dev->type != ARPHRD_CAN)
  1109. return NOTIFY_DONE;
  1110. switch (msg) {
  1111. case NETDEV_UNREGISTER:
  1112. lock_sock(sk);
  1113. /* remove device specific receive entries */
  1114. list_for_each_entry(op, &bo->rx_ops, list)
  1115. if (op->rx_reg_dev == dev)
  1116. bcm_rx_unreg(dev, op);
  1117. /* remove device reference, if this is our bound device */
  1118. if (bo->bound && bo->ifindex == dev->ifindex) {
  1119. bo->bound = 0;
  1120. bo->ifindex = 0;
  1121. notify_enodev = 1;
  1122. }
  1123. release_sock(sk);
  1124. if (notify_enodev) {
  1125. sk->sk_err = ENODEV;
  1126. if (!sock_flag(sk, SOCK_DEAD))
  1127. sk->sk_error_report(sk);
  1128. }
  1129. break;
  1130. case NETDEV_DOWN:
  1131. if (bo->bound && bo->ifindex == dev->ifindex) {
  1132. sk->sk_err = ENETDOWN;
  1133. if (!sock_flag(sk, SOCK_DEAD))
  1134. sk->sk_error_report(sk);
  1135. }
  1136. }
  1137. return NOTIFY_DONE;
  1138. }
  1139. /*
  1140. * initial settings for all BCM sockets to be set at socket creation time
  1141. */
  1142. static int bcm_init(struct sock *sk)
  1143. {
  1144. struct bcm_sock *bo = bcm_sk(sk);
  1145. bo->bound = 0;
  1146. bo->ifindex = 0;
  1147. bo->dropped_usr_msgs = 0;
  1148. bo->bcm_proc_read = NULL;
  1149. INIT_LIST_HEAD(&bo->tx_ops);
  1150. INIT_LIST_HEAD(&bo->rx_ops);
  1151. /* set notifier */
  1152. bo->notifier.notifier_call = bcm_notifier;
  1153. register_netdevice_notifier(&bo->notifier);
  1154. return 0;
  1155. }
  1156. /*
  1157. * standard socket functions
  1158. */
  1159. static int bcm_release(struct socket *sock)
  1160. {
  1161. struct sock *sk = sock->sk;
  1162. struct bcm_sock *bo;
  1163. struct bcm_op *op, *next;
  1164. if (sk == NULL)
  1165. return 0;
  1166. bo = bcm_sk(sk);
  1167. /* remove bcm_ops, timer, rx_unregister(), etc. */
  1168. unregister_netdevice_notifier(&bo->notifier);
  1169. lock_sock(sk);
  1170. list_for_each_entry_safe(op, next, &bo->tx_ops, list)
  1171. bcm_remove_op(op);
  1172. list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
  1173. /*
  1174. * Don't care if we're bound or not (due to netdev problems)
  1175. * can_rx_unregister() is always a save thing to do here.
  1176. */
  1177. if (op->ifindex) {
  1178. /*
  1179. * Only remove subscriptions that had not
  1180. * been removed due to NETDEV_UNREGISTER
  1181. * in bcm_notifier()
  1182. */
  1183. if (op->rx_reg_dev) {
  1184. struct net_device *dev;
  1185. dev = dev_get_by_index(&init_net, op->ifindex);
  1186. if (dev) {
  1187. bcm_rx_unreg(dev, op);
  1188. dev_put(dev);
  1189. }
  1190. }
  1191. } else
  1192. can_rx_unregister(NULL, op->can_id,
  1193. REGMASK(op->can_id),
  1194. bcm_rx_handler, op);
  1195. bcm_remove_op(op);
  1196. }
  1197. /* remove procfs entry */
  1198. if (proc_dir && bo->bcm_proc_read)
  1199. remove_proc_entry(bo->procname, proc_dir);
  1200. /* remove device reference */
  1201. if (bo->bound) {
  1202. bo->bound = 0;
  1203. bo->ifindex = 0;
  1204. }
  1205. sock_orphan(sk);
  1206. sock->sk = NULL;
  1207. release_sock(sk);
  1208. sock_put(sk);
  1209. return 0;
  1210. }
  1211. static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
  1212. int flags)
  1213. {
  1214. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  1215. struct sock *sk = sock->sk;
  1216. struct bcm_sock *bo = bcm_sk(sk);
  1217. if (len < sizeof(*addr))
  1218. return -EINVAL;
  1219. if (bo->bound)
  1220. return -EISCONN;
  1221. /* bind a device to this socket */
  1222. if (addr->can_ifindex) {
  1223. struct net_device *dev;
  1224. dev = dev_get_by_index(&init_net, addr->can_ifindex);
  1225. if (!dev)
  1226. return -ENODEV;
  1227. if (dev->type != ARPHRD_CAN) {
  1228. dev_put(dev);
  1229. return -ENODEV;
  1230. }
  1231. bo->ifindex = dev->ifindex;
  1232. dev_put(dev);
  1233. } else {
  1234. /* no interface reference for ifindex = 0 ('any' CAN device) */
  1235. bo->ifindex = 0;
  1236. }
  1237. bo->bound = 1;
  1238. if (proc_dir) {
  1239. /* unique socket address as filename */
  1240. sprintf(bo->procname, "%lu", sock_i_ino(sk));
  1241. bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
  1242. proc_dir,
  1243. &bcm_proc_fops, sk);
  1244. }
  1245. return 0;
  1246. }
  1247. static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
  1248. struct msghdr *msg, size_t size, int flags)
  1249. {
  1250. struct sock *sk = sock->sk;
  1251. struct sk_buff *skb;
  1252. int error = 0;
  1253. int noblock;
  1254. int err;
  1255. noblock = flags & MSG_DONTWAIT;
  1256. flags &= ~MSG_DONTWAIT;
  1257. skb = skb_recv_datagram(sk, flags, noblock, &error);
  1258. if (!skb)
  1259. return error;
  1260. if (skb->len < size)
  1261. size = skb->len;
  1262. err = memcpy_toiovec(msg->msg_iov, skb->data, size);
  1263. if (err < 0) {
  1264. skb_free_datagram(sk, skb);
  1265. return err;
  1266. }
  1267. sock_recv_ts_and_drops(msg, sk, skb);
  1268. if (msg->msg_name) {
  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. printk(banner);
  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. proc_net_remove(&init_net, "can-bcm");
  1324. }
  1325. module_init(bcm_module_init);
  1326. module_exit(bcm_module_exit);