bcm.c 39 KB

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