bcm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. /*
  2. * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <socketcan-users@lists.berlios.de>
  41. *
  42. */
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/hrtimer.h>
  47. #include <linux/list.h>
  48. #include <linux/proc_fs.h>
  49. #include <linux/seq_file.h>
  50. #include <linux/uio.h>
  51. #include <linux/net.h>
  52. #include <linux/netdevice.h>
  53. #include <linux/socket.h>
  54. #include <linux/if_arp.h>
  55. #include <linux/skbuff.h>
  56. #include <linux/can.h>
  57. #include <linux/can/core.h>
  58. #include <linux/can/bcm.h>
  59. #include <linux/slab.h>
  60. #include <net/sock.h>
  61. #include <net/net_namespace.h>
  62. /*
  63. * To send multiple CAN frame content within TX_SETUP or to filter
  64. * CAN messages with multiplex index within RX_SETUP, the number of
  65. * different filters is limited to 256 due to the one byte index value.
  66. */
  67. #define MAX_NFRAMES 256
  68. /* use of last_frames[index].can_dlc */
  69. #define RX_RECV 0x40 /* received data for this element */
  70. #define RX_THR 0x80 /* element not been sent due to throttle feature */
  71. #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
  72. /* get best masking value for can_rx_register() for a given single can_id */
  73. #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
  74. (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
  75. (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
  76. #define CAN_BCM_VERSION CAN_VERSION
  77. static __initdata const char banner[] = KERN_INFO
  78. "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n";
  79. MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
  80. MODULE_LICENSE("Dual BSD/GPL");
  81. MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  82. MODULE_ALIAS("can-proto-2");
  83. /* easy access to can_frame payload */
  84. static inline u64 GET_U64(const struct can_frame *cp)
  85. {
  86. return *(u64 *)cp->data;
  87. }
  88. struct bcm_op {
  89. struct list_head list;
  90. int ifindex;
  91. canid_t can_id;
  92. u32 flags;
  93. unsigned long frames_abs, frames_filtered;
  94. struct timeval ival1, ival2;
  95. struct hrtimer timer, thrtimer;
  96. struct tasklet_struct tsklet, thrtsklet;
  97. ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
  98. int rx_ifindex;
  99. u32 count;
  100. u32 nframes;
  101. u32 currframe;
  102. struct can_frame *frames;
  103. struct can_frame *last_frames;
  104. struct can_frame sframe;
  105. struct can_frame last_sframe;
  106. struct sock *sk;
  107. struct net_device *rx_reg_dev;
  108. };
  109. static struct proc_dir_entry *proc_dir;
  110. struct bcm_sock {
  111. struct sock sk;
  112. int bound;
  113. int ifindex;
  114. struct notifier_block notifier;
  115. struct list_head rx_ops;
  116. struct list_head tx_ops;
  117. unsigned long dropped_usr_msgs;
  118. struct proc_dir_entry *bcm_proc_read;
  119. char procname [32]; /* inode number in decimal with \0 */
  120. };
  121. static inline struct bcm_sock *bcm_sk(const struct sock *sk)
  122. {
  123. return (struct bcm_sock *)sk;
  124. }
  125. #define CFSIZ sizeof(struct can_frame)
  126. #define OPSIZ sizeof(struct bcm_op)
  127. #define MHSIZ sizeof(struct bcm_msg_head)
  128. /*
  129. * procfs functions
  130. */
  131. static char *bcm_proc_getifname(char *result, int ifindex)
  132. {
  133. struct net_device *dev;
  134. if (!ifindex)
  135. return "any";
  136. rcu_read_lock();
  137. dev = dev_get_by_index_rcu(&init_net, ifindex);
  138. if (dev)
  139. strcpy(result, dev->name);
  140. else
  141. strcpy(result, "???");
  142. rcu_read_unlock();
  143. return result;
  144. }
  145. static int bcm_proc_show(struct seq_file *m, void *v)
  146. {
  147. char ifname[IFNAMSIZ];
  148. struct sock *sk = (struct sock *)m->private;
  149. struct bcm_sock *bo = bcm_sk(sk);
  150. struct bcm_op *op;
  151. seq_printf(m, ">>> socket %pK", sk->sk_socket);
  152. seq_printf(m, " / sk %pK", sk);
  153. seq_printf(m, " / bo %pK", bo);
  154. seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
  155. seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
  156. seq_printf(m, " <<<\n");
  157. list_for_each_entry(op, &bo->rx_ops, list) {
  158. unsigned long reduction;
  159. /* print only active entries & prevent division by zero */
  160. if (!op->frames_abs)
  161. continue;
  162. seq_printf(m, "rx_op: %03X %-5s ",
  163. op->can_id, bcm_proc_getifname(ifname, op->ifindex));
  164. seq_printf(m, "[%u]%c ", op->nframes,
  165. (op->flags & RX_CHECK_DLC)?'d':' ');
  166. if (op->kt_ival1.tv64)
  167. seq_printf(m, "timeo=%lld ",
  168. (long long)
  169. ktime_to_us(op->kt_ival1));
  170. if (op->kt_ival2.tv64)
  171. seq_printf(m, "thr=%lld ",
  172. (long long)
  173. ktime_to_us(op->kt_ival2));
  174. seq_printf(m, "# recv %ld (%ld) => reduction: ",
  175. op->frames_filtered, op->frames_abs);
  176. reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
  177. seq_printf(m, "%s%ld%%\n",
  178. (reduction == 100)?"near ":"", reduction);
  179. }
  180. list_for_each_entry(op, &bo->tx_ops, list) {
  181. seq_printf(m, "tx_op: %03X %s [%u] ",
  182. op->can_id,
  183. bcm_proc_getifname(ifname, op->ifindex),
  184. op->nframes);
  185. if (op->kt_ival1.tv64)
  186. seq_printf(m, "t1=%lld ",
  187. (long long) ktime_to_us(op->kt_ival1));
  188. if (op->kt_ival2.tv64)
  189. seq_printf(m, "t2=%lld ",
  190. (long long) ktime_to_us(op->kt_ival2));
  191. seq_printf(m, "# sent %ld\n", op->frames_abs);
  192. }
  193. seq_putc(m, '\n');
  194. return 0;
  195. }
  196. static int bcm_proc_open(struct inode *inode, struct file *file)
  197. {
  198. return single_open(file, bcm_proc_show, PDE(inode)->data);
  199. }
  200. static const struct file_operations bcm_proc_fops = {
  201. .owner = THIS_MODULE,
  202. .open = bcm_proc_open,
  203. .read = seq_read,
  204. .llseek = seq_lseek,
  205. .release = single_release,
  206. };
  207. /*
  208. * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
  209. * of the given bcm tx op
  210. */
  211. static void bcm_can_tx(struct bcm_op *op)
  212. {
  213. struct sk_buff *skb;
  214. struct net_device *dev;
  215. struct can_frame *cf = &op->frames[op->currframe];
  216. /* no target device? => exit */
  217. if (!op->ifindex)
  218. return;
  219. dev = dev_get_by_index(&init_net, op->ifindex);
  220. if (!dev) {
  221. /* RFC: should this bcm_op remove itself here? */
  222. return;
  223. }
  224. skb = alloc_skb(CFSIZ, gfp_any());
  225. if (!skb)
  226. goto out;
  227. memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
  228. /* send with loopback */
  229. skb->dev = dev;
  230. skb->sk = op->sk;
  231. can_send(skb, 1);
  232. /* update statistics */
  233. op->currframe++;
  234. op->frames_abs++;
  235. /* reached last frame? */
  236. if (op->currframe >= op->nframes)
  237. op->currframe = 0;
  238. out:
  239. dev_put(dev);
  240. }
  241. /*
  242. * bcm_send_to_user - send a BCM message to the userspace
  243. * (consisting of bcm_msg_head + x CAN frames)
  244. */
  245. static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
  246. struct can_frame *frames, int has_timestamp)
  247. {
  248. struct sk_buff *skb;
  249. struct can_frame *firstframe;
  250. struct sockaddr_can *addr;
  251. struct sock *sk = op->sk;
  252. unsigned int datalen = head->nframes * CFSIZ;
  253. int err;
  254. skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
  255. if (!skb)
  256. return;
  257. memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
  258. if (head->nframes) {
  259. /* can_frames starting here */
  260. firstframe = (struct can_frame *)skb_tail_pointer(skb);
  261. memcpy(skb_put(skb, datalen), frames, datalen);
  262. /*
  263. * the BCM uses the can_dlc-element of the can_frame
  264. * structure for internal purposes. This is only
  265. * relevant for updates that are generated by the
  266. * BCM, where nframes is 1
  267. */
  268. if (head->nframes == 1)
  269. firstframe->can_dlc &= BCM_CAN_DLC_MASK;
  270. }
  271. if (has_timestamp) {
  272. /* restore rx timestamp */
  273. skb->tstamp = op->rx_stamp;
  274. }
  275. /*
  276. * Put the datagram to the queue so that bcm_recvmsg() can
  277. * get it from there. We need to pass the interface index to
  278. * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
  279. * containing the interface index.
  280. */
  281. BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
  282. addr = (struct sockaddr_can *)skb->cb;
  283. memset(addr, 0, sizeof(*addr));
  284. addr->can_family = AF_CAN;
  285. addr->can_ifindex = op->rx_ifindex;
  286. err = sock_queue_rcv_skb(sk, skb);
  287. if (err < 0) {
  288. struct bcm_sock *bo = bcm_sk(sk);
  289. kfree_skb(skb);
  290. /* don't care about overflows in this statistic */
  291. bo->dropped_usr_msgs++;
  292. }
  293. }
  294. static void bcm_tx_timeout_tsklet(unsigned long data)
  295. {
  296. struct bcm_op *op = (struct bcm_op *)data;
  297. struct bcm_msg_head msg_head;
  298. if (op->kt_ival1.tv64 && (op->count > 0)) {
  299. op->count--;
  300. if (!op->count && (op->flags & TX_COUNTEVT)) {
  301. /* create notification to user */
  302. msg_head.opcode = TX_EXPIRED;
  303. msg_head.flags = op->flags;
  304. msg_head.count = op->count;
  305. msg_head.ival1 = op->ival1;
  306. msg_head.ival2 = op->ival2;
  307. msg_head.can_id = op->can_id;
  308. msg_head.nframes = 0;
  309. bcm_send_to_user(op, &msg_head, NULL, 0);
  310. }
  311. /* send (next) frame */
  312. bcm_can_tx(op);
  313. hrtimer_start(&op->timer,
  314. ktime_add(ktime_get(), op->kt_ival1),
  315. HRTIMER_MODE_ABS);
  316. } else {
  317. if (op->kt_ival2.tv64) {
  318. /* send (next) frame */
  319. bcm_can_tx(op);
  320. hrtimer_start(&op->timer,
  321. ktime_add(ktime_get(), op->kt_ival2),
  322. HRTIMER_MODE_ABS);
  323. }
  324. }
  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. ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
  797. /* spec: send can_frame when starting timer */
  798. op->flags |= TX_ANNOUNCE;
  799. /* only start timer when having more frames than sent below */
  800. if (op->kt_ival1.tv64 && (op->count > 1)) {
  801. /* op->count-- is done in bcm_tx_timeout_tsklet */
  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. if (op->kt_ival1.tv64 && (op->count > 0))
  811. op->count--;
  812. }
  813. return msg_head->nframes * CFSIZ + MHSIZ;
  814. }
  815. /*
  816. * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
  817. */
  818. static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  819. int ifindex, struct sock *sk)
  820. {
  821. struct bcm_sock *bo = bcm_sk(sk);
  822. struct bcm_op *op;
  823. int do_rx_register;
  824. int err = 0;
  825. if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
  826. /* be robust against wrong usage ... */
  827. msg_head->flags |= RX_FILTER_ID;
  828. /* ignore trailing garbage */
  829. msg_head->nframes = 0;
  830. }
  831. /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
  832. if (msg_head->nframes > MAX_NFRAMES + 1)
  833. return -EINVAL;
  834. if ((msg_head->flags & RX_RTR_FRAME) &&
  835. ((msg_head->nframes != 1) ||
  836. (!(msg_head->can_id & CAN_RTR_FLAG))))
  837. return -EINVAL;
  838. /* check the given can_id */
  839. op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
  840. if (op) {
  841. /* update existing BCM operation */
  842. /*
  843. * Do we need more space for the can_frames than currently
  844. * allocated? -> This is a _really_ unusual use-case and
  845. * therefore (complexity / locking) it is not supported.
  846. */
  847. if (msg_head->nframes > op->nframes)
  848. return -E2BIG;
  849. if (msg_head->nframes) {
  850. /* update can_frames content */
  851. err = memcpy_fromiovec((u8 *)op->frames,
  852. msg->msg_iov,
  853. msg_head->nframes * CFSIZ);
  854. if (err < 0)
  855. return err;
  856. /* clear last_frames to indicate 'nothing received' */
  857. memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
  858. }
  859. op->nframes = msg_head->nframes;
  860. /* Only an update -> do not call can_rx_register() */
  861. do_rx_register = 0;
  862. } else {
  863. /* insert new BCM operation for the given can_id */
  864. op = kzalloc(OPSIZ, GFP_KERNEL);
  865. if (!op)
  866. return -ENOMEM;
  867. op->can_id = msg_head->can_id;
  868. op->nframes = msg_head->nframes;
  869. if (msg_head->nframes > 1) {
  870. /* create array for can_frames and copy the data */
  871. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  872. GFP_KERNEL);
  873. if (!op->frames) {
  874. kfree(op);
  875. return -ENOMEM;
  876. }
  877. /* create and init array for received can_frames */
  878. op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
  879. GFP_KERNEL);
  880. if (!op->last_frames) {
  881. kfree(op->frames);
  882. kfree(op);
  883. return -ENOMEM;
  884. }
  885. } else {
  886. op->frames = &op->sframe;
  887. op->last_frames = &op->last_sframe;
  888. }
  889. if (msg_head->nframes) {
  890. err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
  891. msg_head->nframes * CFSIZ);
  892. if (err < 0) {
  893. if (op->frames != &op->sframe)
  894. kfree(op->frames);
  895. if (op->last_frames != &op->last_sframe)
  896. kfree(op->last_frames);
  897. kfree(op);
  898. return err;
  899. }
  900. }
  901. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  902. op->sk = sk;
  903. op->ifindex = ifindex;
  904. /* initialize uninitialized (kzalloc) structure */
  905. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  906. op->timer.function = bcm_rx_timeout_handler;
  907. /* initialize tasklet for rx timeout notification */
  908. tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
  909. (unsigned long) op);
  910. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  911. op->thrtimer.function = bcm_rx_thr_handler;
  912. /* initialize tasklet for rx throttle handling */
  913. tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
  914. (unsigned long) op);
  915. /* add this bcm_op to the list of the rx_ops */
  916. list_add(&op->list, &bo->rx_ops);
  917. /* call can_rx_register() */
  918. do_rx_register = 1;
  919. } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
  920. /* check flags */
  921. op->flags = msg_head->flags;
  922. if (op->flags & RX_RTR_FRAME) {
  923. /* no timers in RTR-mode */
  924. hrtimer_cancel(&op->thrtimer);
  925. hrtimer_cancel(&op->timer);
  926. /*
  927. * funny feature in RX(!)_SETUP only for RTR-mode:
  928. * copy can_id into frame BUT without RTR-flag to
  929. * prevent a full-load-loopback-test ... ;-]
  930. */
  931. if ((op->flags & TX_CP_CAN_ID) ||
  932. (op->frames[0].can_id == op->can_id))
  933. op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
  934. } else {
  935. if (op->flags & SETTIMER) {
  936. /* set timer value */
  937. op->ival1 = msg_head->ival1;
  938. op->ival2 = msg_head->ival2;
  939. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  940. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  941. /* disable an active timer due to zero value? */
  942. if (!op->kt_ival1.tv64)
  943. hrtimer_cancel(&op->timer);
  944. /*
  945. * In any case cancel the throttle timer, flush
  946. * potentially blocked msgs and reset throttle handling
  947. */
  948. op->kt_lastmsg = ktime_set(0, 0);
  949. hrtimer_cancel(&op->thrtimer);
  950. bcm_rx_thr_flush(op, 1);
  951. }
  952. if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
  953. hrtimer_start(&op->timer, op->kt_ival1,
  954. HRTIMER_MODE_REL);
  955. }
  956. /* now we can register for can_ids, if we added a new bcm_op */
  957. if (do_rx_register) {
  958. if (ifindex) {
  959. struct net_device *dev;
  960. dev = dev_get_by_index(&init_net, ifindex);
  961. if (dev) {
  962. err = can_rx_register(dev, op->can_id,
  963. REGMASK(op->can_id),
  964. bcm_rx_handler, op,
  965. "bcm");
  966. op->rx_reg_dev = dev;
  967. dev_put(dev);
  968. }
  969. } else
  970. err = can_rx_register(NULL, op->can_id,
  971. REGMASK(op->can_id),
  972. bcm_rx_handler, op, "bcm");
  973. if (err) {
  974. /* this bcm rx op is broken -> remove it */
  975. list_del(&op->list);
  976. bcm_remove_op(op);
  977. return err;
  978. }
  979. }
  980. return msg_head->nframes * CFSIZ + MHSIZ;
  981. }
  982. /*
  983. * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
  984. */
  985. static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
  986. {
  987. struct sk_buff *skb;
  988. struct net_device *dev;
  989. int err;
  990. /* we need a real device to send frames */
  991. if (!ifindex)
  992. return -ENODEV;
  993. skb = alloc_skb(CFSIZ, GFP_KERNEL);
  994. if (!skb)
  995. return -ENOMEM;
  996. err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
  997. if (err < 0) {
  998. kfree_skb(skb);
  999. return err;
  1000. }
  1001. dev = dev_get_by_index(&init_net, ifindex);
  1002. if (!dev) {
  1003. kfree_skb(skb);
  1004. return -ENODEV;
  1005. }
  1006. skb->dev = dev;
  1007. skb->sk = sk;
  1008. err = can_send(skb, 1); /* send with loopback */
  1009. dev_put(dev);
  1010. if (err)
  1011. return err;
  1012. return CFSIZ + MHSIZ;
  1013. }
  1014. /*
  1015. * bcm_sendmsg - process BCM commands (opcodes) from the userspace
  1016. */
  1017. static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
  1018. struct msghdr *msg, size_t size)
  1019. {
  1020. struct sock *sk = sock->sk;
  1021. struct bcm_sock *bo = bcm_sk(sk);
  1022. int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
  1023. struct bcm_msg_head msg_head;
  1024. int ret; /* read bytes or error codes as return value */
  1025. if (!bo->bound)
  1026. return -ENOTCONN;
  1027. /* check for valid message length from userspace */
  1028. if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
  1029. return -EINVAL;
  1030. /* check for alternative ifindex for this bcm_op */
  1031. if (!ifindex && msg->msg_name) {
  1032. /* no bound device as default => check msg_name */
  1033. struct sockaddr_can *addr =
  1034. (struct sockaddr_can *)msg->msg_name;
  1035. if (msg->msg_namelen < sizeof(*addr))
  1036. return -EINVAL;
  1037. if (addr->can_family != AF_CAN)
  1038. return -EINVAL;
  1039. /* ifindex from sendto() */
  1040. ifindex = addr->can_ifindex;
  1041. if (ifindex) {
  1042. struct net_device *dev;
  1043. dev = dev_get_by_index(&init_net, ifindex);
  1044. if (!dev)
  1045. return -ENODEV;
  1046. if (dev->type != ARPHRD_CAN) {
  1047. dev_put(dev);
  1048. return -ENODEV;
  1049. }
  1050. dev_put(dev);
  1051. }
  1052. }
  1053. /* read message head information */
  1054. ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
  1055. if (ret < 0)
  1056. return ret;
  1057. lock_sock(sk);
  1058. switch (msg_head.opcode) {
  1059. case TX_SETUP:
  1060. ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
  1061. break;
  1062. case RX_SETUP:
  1063. ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
  1064. break;
  1065. case TX_DELETE:
  1066. if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
  1067. ret = MHSIZ;
  1068. else
  1069. ret = -EINVAL;
  1070. break;
  1071. case RX_DELETE:
  1072. if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
  1073. ret = MHSIZ;
  1074. else
  1075. ret = -EINVAL;
  1076. break;
  1077. case TX_READ:
  1078. /* reuse msg_head for the reply to TX_READ */
  1079. msg_head.opcode = TX_STATUS;
  1080. ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
  1081. break;
  1082. case RX_READ:
  1083. /* reuse msg_head for the reply to RX_READ */
  1084. msg_head.opcode = RX_STATUS;
  1085. ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
  1086. break;
  1087. case TX_SEND:
  1088. /* we need exactly one can_frame behind the msg head */
  1089. if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
  1090. ret = -EINVAL;
  1091. else
  1092. ret = bcm_tx_send(msg, ifindex, sk);
  1093. break;
  1094. default:
  1095. ret = -EINVAL;
  1096. break;
  1097. }
  1098. release_sock(sk);
  1099. return ret;
  1100. }
  1101. /*
  1102. * notification handler for netdevice status changes
  1103. */
  1104. static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
  1105. void *data)
  1106. {
  1107. struct net_device *dev = (struct net_device *)data;
  1108. struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
  1109. struct sock *sk = &bo->sk;
  1110. struct bcm_op *op;
  1111. int notify_enodev = 0;
  1112. if (!net_eq(dev_net(dev), &init_net))
  1113. return NOTIFY_DONE;
  1114. if (dev->type != ARPHRD_CAN)
  1115. return NOTIFY_DONE;
  1116. switch (msg) {
  1117. case NETDEV_UNREGISTER:
  1118. lock_sock(sk);
  1119. /* remove device specific receive entries */
  1120. list_for_each_entry(op, &bo->rx_ops, list)
  1121. if (op->rx_reg_dev == dev)
  1122. bcm_rx_unreg(dev, op);
  1123. /* remove device reference, if this is our bound device */
  1124. if (bo->bound && bo->ifindex == dev->ifindex) {
  1125. bo->bound = 0;
  1126. bo->ifindex = 0;
  1127. notify_enodev = 1;
  1128. }
  1129. release_sock(sk);
  1130. if (notify_enodev) {
  1131. sk->sk_err = ENODEV;
  1132. if (!sock_flag(sk, SOCK_DEAD))
  1133. sk->sk_error_report(sk);
  1134. }
  1135. break;
  1136. case NETDEV_DOWN:
  1137. if (bo->bound && bo->ifindex == dev->ifindex) {
  1138. sk->sk_err = ENETDOWN;
  1139. if (!sock_flag(sk, SOCK_DEAD))
  1140. sk->sk_error_report(sk);
  1141. }
  1142. }
  1143. return NOTIFY_DONE;
  1144. }
  1145. /*
  1146. * initial settings for all BCM sockets to be set at socket creation time
  1147. */
  1148. static int bcm_init(struct sock *sk)
  1149. {
  1150. struct bcm_sock *bo = bcm_sk(sk);
  1151. bo->bound = 0;
  1152. bo->ifindex = 0;
  1153. bo->dropped_usr_msgs = 0;
  1154. bo->bcm_proc_read = NULL;
  1155. INIT_LIST_HEAD(&bo->tx_ops);
  1156. INIT_LIST_HEAD(&bo->rx_ops);
  1157. /* set notifier */
  1158. bo->notifier.notifier_call = bcm_notifier;
  1159. register_netdevice_notifier(&bo->notifier);
  1160. return 0;
  1161. }
  1162. /*
  1163. * standard socket functions
  1164. */
  1165. static int bcm_release(struct socket *sock)
  1166. {
  1167. struct sock *sk = sock->sk;
  1168. struct bcm_sock *bo;
  1169. struct bcm_op *op, *next;
  1170. if (sk == NULL)
  1171. return 0;
  1172. bo = bcm_sk(sk);
  1173. /* remove bcm_ops, timer, rx_unregister(), etc. */
  1174. unregister_netdevice_notifier(&bo->notifier);
  1175. lock_sock(sk);
  1176. list_for_each_entry_safe(op, next, &bo->tx_ops, list)
  1177. bcm_remove_op(op);
  1178. list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
  1179. /*
  1180. * Don't care if we're bound or not (due to netdev problems)
  1181. * can_rx_unregister() is always a save thing to do here.
  1182. */
  1183. if (op->ifindex) {
  1184. /*
  1185. * Only remove subscriptions that had not
  1186. * been removed due to NETDEV_UNREGISTER
  1187. * in bcm_notifier()
  1188. */
  1189. if (op->rx_reg_dev) {
  1190. struct net_device *dev;
  1191. dev = dev_get_by_index(&init_net, op->ifindex);
  1192. if (dev) {
  1193. bcm_rx_unreg(dev, op);
  1194. dev_put(dev);
  1195. }
  1196. }
  1197. } else
  1198. can_rx_unregister(NULL, op->can_id,
  1199. REGMASK(op->can_id),
  1200. bcm_rx_handler, op);
  1201. bcm_remove_op(op);
  1202. }
  1203. /* remove procfs entry */
  1204. if (proc_dir && bo->bcm_proc_read)
  1205. remove_proc_entry(bo->procname, proc_dir);
  1206. /* remove device reference */
  1207. if (bo->bound) {
  1208. bo->bound = 0;
  1209. bo->ifindex = 0;
  1210. }
  1211. sock_orphan(sk);
  1212. sock->sk = NULL;
  1213. release_sock(sk);
  1214. sock_put(sk);
  1215. return 0;
  1216. }
  1217. static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
  1218. int flags)
  1219. {
  1220. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  1221. struct sock *sk = sock->sk;
  1222. struct bcm_sock *bo = bcm_sk(sk);
  1223. if (len < sizeof(*addr))
  1224. return -EINVAL;
  1225. if (bo->bound)
  1226. return -EISCONN;
  1227. /* bind a device to this socket */
  1228. if (addr->can_ifindex) {
  1229. struct net_device *dev;
  1230. dev = dev_get_by_index(&init_net, addr->can_ifindex);
  1231. if (!dev)
  1232. return -ENODEV;
  1233. if (dev->type != ARPHRD_CAN) {
  1234. dev_put(dev);
  1235. return -ENODEV;
  1236. }
  1237. bo->ifindex = dev->ifindex;
  1238. dev_put(dev);
  1239. } else {
  1240. /* no interface reference for ifindex = 0 ('any' CAN device) */
  1241. bo->ifindex = 0;
  1242. }
  1243. bo->bound = 1;
  1244. if (proc_dir) {
  1245. /* unique socket address as filename */
  1246. sprintf(bo->procname, "%lu", sock_i_ino(sk));
  1247. bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
  1248. proc_dir,
  1249. &bcm_proc_fops, sk);
  1250. }
  1251. return 0;
  1252. }
  1253. static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
  1254. struct msghdr *msg, size_t size, int flags)
  1255. {
  1256. struct sock *sk = sock->sk;
  1257. struct sk_buff *skb;
  1258. int error = 0;
  1259. int noblock;
  1260. int err;
  1261. noblock = flags & MSG_DONTWAIT;
  1262. flags &= ~MSG_DONTWAIT;
  1263. skb = skb_recv_datagram(sk, flags, noblock, &error);
  1264. if (!skb)
  1265. return error;
  1266. if (skb->len < size)
  1267. size = skb->len;
  1268. err = memcpy_toiovec(msg->msg_iov, skb->data, size);
  1269. if (err < 0) {
  1270. skb_free_datagram(sk, skb);
  1271. return err;
  1272. }
  1273. sock_recv_ts_and_drops(msg, sk, skb);
  1274. if (msg->msg_name) {
  1275. msg->msg_namelen = sizeof(struct sockaddr_can);
  1276. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  1277. }
  1278. skb_free_datagram(sk, skb);
  1279. return size;
  1280. }
  1281. static const struct proto_ops bcm_ops = {
  1282. .family = PF_CAN,
  1283. .release = bcm_release,
  1284. .bind = sock_no_bind,
  1285. .connect = bcm_connect,
  1286. .socketpair = sock_no_socketpair,
  1287. .accept = sock_no_accept,
  1288. .getname = sock_no_getname,
  1289. .poll = datagram_poll,
  1290. .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
  1291. .listen = sock_no_listen,
  1292. .shutdown = sock_no_shutdown,
  1293. .setsockopt = sock_no_setsockopt,
  1294. .getsockopt = sock_no_getsockopt,
  1295. .sendmsg = bcm_sendmsg,
  1296. .recvmsg = bcm_recvmsg,
  1297. .mmap = sock_no_mmap,
  1298. .sendpage = sock_no_sendpage,
  1299. };
  1300. static struct proto bcm_proto __read_mostly = {
  1301. .name = "CAN_BCM",
  1302. .owner = THIS_MODULE,
  1303. .obj_size = sizeof(struct bcm_sock),
  1304. .init = bcm_init,
  1305. };
  1306. static const struct can_proto bcm_can_proto = {
  1307. .type = SOCK_DGRAM,
  1308. .protocol = CAN_BCM,
  1309. .ops = &bcm_ops,
  1310. .prot = &bcm_proto,
  1311. };
  1312. static int __init bcm_module_init(void)
  1313. {
  1314. int err;
  1315. printk(banner);
  1316. err = can_proto_register(&bcm_can_proto);
  1317. if (err < 0) {
  1318. printk(KERN_ERR "can: registration of bcm protocol failed\n");
  1319. return err;
  1320. }
  1321. /* create /proc/net/can-bcm directory */
  1322. proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
  1323. return 0;
  1324. }
  1325. static void __exit bcm_module_exit(void)
  1326. {
  1327. can_proto_unregister(&bcm_can_proto);
  1328. if (proc_dir)
  1329. proc_net_remove(&init_net, "can-bcm");
  1330. }
  1331. module_init(bcm_module_init);
  1332. module_exit(bcm_module_exit);