bcm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <socketcan-users@lists.berlios.de>
  41. *
  42. */
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/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_start_timer(struct bcm_op *op)
  295. {
  296. if (op->kt_ival1.tv64 && op->count)
  297. hrtimer_start(&op->timer,
  298. ktime_add(ktime_get(), op->kt_ival1),
  299. HRTIMER_MODE_ABS);
  300. else if (op->kt_ival2.tv64)
  301. hrtimer_start(&op->timer,
  302. ktime_add(ktime_get(), op->kt_ival2),
  303. HRTIMER_MODE_ABS);
  304. }
  305. static void bcm_tx_timeout_tsklet(unsigned long data)
  306. {
  307. struct bcm_op *op = (struct bcm_op *)data;
  308. struct bcm_msg_head msg_head;
  309. if (op->kt_ival1.tv64 && (op->count > 0)) {
  310. op->count--;
  311. if (!op->count && (op->flags & TX_COUNTEVT)) {
  312. /* create notification to user */
  313. msg_head.opcode = TX_EXPIRED;
  314. msg_head.flags = op->flags;
  315. msg_head.count = op->count;
  316. msg_head.ival1 = op->ival1;
  317. msg_head.ival2 = op->ival2;
  318. msg_head.can_id = op->can_id;
  319. msg_head.nframes = 0;
  320. bcm_send_to_user(op, &msg_head, NULL, 0);
  321. }
  322. bcm_can_tx(op);
  323. } else if (op->kt_ival2.tv64)
  324. bcm_can_tx(op);
  325. bcm_tx_start_timer(op);
  326. }
  327. /*
  328. * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
  329. */
  330. static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
  331. {
  332. struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
  333. tasklet_schedule(&op->tsklet);
  334. return HRTIMER_NORESTART;
  335. }
  336. /*
  337. * bcm_rx_changed - create a RX_CHANGED notification due to changed content
  338. */
  339. static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
  340. {
  341. struct bcm_msg_head head;
  342. /* update statistics */
  343. op->frames_filtered++;
  344. /* prevent statistics overflow */
  345. if (op->frames_filtered > ULONG_MAX/100)
  346. op->frames_filtered = op->frames_abs = 0;
  347. /* this element is not throttled anymore */
  348. data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
  349. head.opcode = RX_CHANGED;
  350. head.flags = op->flags;
  351. head.count = op->count;
  352. head.ival1 = op->ival1;
  353. head.ival2 = op->ival2;
  354. head.can_id = op->can_id;
  355. head.nframes = 1;
  356. bcm_send_to_user(op, &head, data, 1);
  357. }
  358. /*
  359. * bcm_rx_update_and_send - process a detected relevant receive content change
  360. * 1. update the last received data
  361. * 2. send a notification to the user (if possible)
  362. */
  363. static void bcm_rx_update_and_send(struct bcm_op *op,
  364. struct can_frame *lastdata,
  365. const struct can_frame *rxdata)
  366. {
  367. memcpy(lastdata, rxdata, CFSIZ);
  368. /* mark as used and throttled by default */
  369. lastdata->can_dlc |= (RX_RECV|RX_THR);
  370. /* throtteling mode inactive ? */
  371. if (!op->kt_ival2.tv64) {
  372. /* send RX_CHANGED to the user immediately */
  373. bcm_rx_changed(op, lastdata);
  374. return;
  375. }
  376. /* with active throttling timer we are just done here */
  377. if (hrtimer_active(&op->thrtimer))
  378. return;
  379. /* first receiption with enabled throttling mode */
  380. if (!op->kt_lastmsg.tv64)
  381. goto rx_changed_settime;
  382. /* got a second frame inside a potential throttle period? */
  383. if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
  384. ktime_to_us(op->kt_ival2)) {
  385. /* do not send the saved data - only start throttle timer */
  386. hrtimer_start(&op->thrtimer,
  387. ktime_add(op->kt_lastmsg, op->kt_ival2),
  388. HRTIMER_MODE_ABS);
  389. return;
  390. }
  391. /* the gap was that big, that throttling was not needed here */
  392. rx_changed_settime:
  393. bcm_rx_changed(op, lastdata);
  394. op->kt_lastmsg = ktime_get();
  395. }
  396. /*
  397. * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
  398. * received data stored in op->last_frames[]
  399. */
  400. static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
  401. const struct can_frame *rxdata)
  402. {
  403. /*
  404. * no one uses the MSBs of can_dlc for comparation,
  405. * so we use it here to detect the first time of reception
  406. */
  407. if (!(op->last_frames[index].can_dlc & RX_RECV)) {
  408. /* received data for the first time => send update to user */
  409. bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
  410. return;
  411. }
  412. /* do a real check in can_frame data section */
  413. if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
  414. (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
  415. bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
  416. return;
  417. }
  418. if (op->flags & RX_CHECK_DLC) {
  419. /* do a real check in can_frame dlc */
  420. if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
  421. BCM_CAN_DLC_MASK)) {
  422. bcm_rx_update_and_send(op, &op->last_frames[index],
  423. rxdata);
  424. return;
  425. }
  426. }
  427. }
  428. /*
  429. * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
  430. */
  431. static void bcm_rx_starttimer(struct bcm_op *op)
  432. {
  433. if (op->flags & RX_NO_AUTOTIMER)
  434. return;
  435. if (op->kt_ival1.tv64)
  436. hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
  437. }
  438. static void bcm_rx_timeout_tsklet(unsigned long data)
  439. {
  440. struct bcm_op *op = (struct bcm_op *)data;
  441. struct bcm_msg_head msg_head;
  442. /* create notification to user */
  443. msg_head.opcode = RX_TIMEOUT;
  444. msg_head.flags = op->flags;
  445. msg_head.count = op->count;
  446. msg_head.ival1 = op->ival1;
  447. msg_head.ival2 = op->ival2;
  448. msg_head.can_id = op->can_id;
  449. msg_head.nframes = 0;
  450. bcm_send_to_user(op, &msg_head, NULL, 0);
  451. }
  452. /*
  453. * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
  454. */
  455. static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
  456. {
  457. struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
  458. /* schedule before NET_RX_SOFTIRQ */
  459. tasklet_hi_schedule(&op->tsklet);
  460. /* no restart of the timer is done here! */
  461. /* if user wants to be informed, when cyclic CAN-Messages come back */
  462. if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
  463. /* clear received can_frames to indicate 'nothing received' */
  464. memset(op->last_frames, 0, op->nframes * CFSIZ);
  465. }
  466. return HRTIMER_NORESTART;
  467. }
  468. /*
  469. * bcm_rx_do_flush - helper for bcm_rx_thr_flush
  470. */
  471. static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
  472. unsigned int index)
  473. {
  474. if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
  475. if (update)
  476. bcm_rx_changed(op, &op->last_frames[index]);
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. /*
  482. * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
  483. *
  484. * update == 0 : just check if throttled data is available (any irq context)
  485. * update == 1 : check and send throttled data to userspace (soft_irq context)
  486. */
  487. static int bcm_rx_thr_flush(struct bcm_op *op, int update)
  488. {
  489. int updated = 0;
  490. if (op->nframes > 1) {
  491. unsigned int i;
  492. /* for MUX filter we start at index 1 */
  493. for (i = 1; i < op->nframes; i++)
  494. updated += bcm_rx_do_flush(op, update, i);
  495. } else {
  496. /* for RX_FILTER_ID and simple filter */
  497. updated += bcm_rx_do_flush(op, update, 0);
  498. }
  499. return updated;
  500. }
  501. static void bcm_rx_thr_tsklet(unsigned long data)
  502. {
  503. struct bcm_op *op = (struct bcm_op *)data;
  504. /* push the changed data to the userspace */
  505. bcm_rx_thr_flush(op, 1);
  506. }
  507. /*
  508. * bcm_rx_thr_handler - the time for blocked content updates is over now:
  509. * Check for throttled data and send it to the userspace
  510. */
  511. static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
  512. {
  513. struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
  514. tasklet_schedule(&op->thrtsklet);
  515. if (bcm_rx_thr_flush(op, 0)) {
  516. hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
  517. return HRTIMER_RESTART;
  518. } else {
  519. /* rearm throttle handling */
  520. op->kt_lastmsg = ktime_set(0, 0);
  521. return HRTIMER_NORESTART;
  522. }
  523. }
  524. /*
  525. * bcm_rx_handler - handle a CAN frame receiption
  526. */
  527. static void bcm_rx_handler(struct sk_buff *skb, void *data)
  528. {
  529. struct bcm_op *op = (struct bcm_op *)data;
  530. const struct can_frame *rxframe = (struct can_frame *)skb->data;
  531. unsigned int i;
  532. /* disable timeout */
  533. hrtimer_cancel(&op->timer);
  534. if (op->can_id != rxframe->can_id)
  535. return;
  536. /* save rx timestamp */
  537. op->rx_stamp = skb->tstamp;
  538. /* save originator for recvfrom() */
  539. op->rx_ifindex = skb->dev->ifindex;
  540. /* update statistics */
  541. op->frames_abs++;
  542. if (op->flags & RX_RTR_FRAME) {
  543. /* send reply for RTR-request (placed in op->frames[0]) */
  544. bcm_can_tx(op);
  545. return;
  546. }
  547. if (op->flags & RX_FILTER_ID) {
  548. /* the easiest case */
  549. bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
  550. goto rx_starttimer;
  551. }
  552. if (op->nframes == 1) {
  553. /* simple compare with index 0 */
  554. bcm_rx_cmp_to_index(op, 0, rxframe);
  555. goto rx_starttimer;
  556. }
  557. if (op->nframes > 1) {
  558. /*
  559. * multiplex compare
  560. *
  561. * find the first multiplex mask that fits.
  562. * Remark: The MUX-mask is stored in index 0
  563. */
  564. for (i = 1; i < op->nframes; i++) {
  565. if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
  566. (GET_U64(&op->frames[0]) &
  567. GET_U64(&op->frames[i]))) {
  568. bcm_rx_cmp_to_index(op, i, rxframe);
  569. break;
  570. }
  571. }
  572. }
  573. rx_starttimer:
  574. bcm_rx_starttimer(op);
  575. }
  576. /*
  577. * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
  578. */
  579. static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
  580. int ifindex)
  581. {
  582. struct bcm_op *op;
  583. list_for_each_entry(op, ops, list) {
  584. if ((op->can_id == can_id) && (op->ifindex == ifindex))
  585. return op;
  586. }
  587. return NULL;
  588. }
  589. static void bcm_remove_op(struct bcm_op *op)
  590. {
  591. hrtimer_cancel(&op->timer);
  592. hrtimer_cancel(&op->thrtimer);
  593. if (op->tsklet.func)
  594. tasklet_kill(&op->tsklet);
  595. if (op->thrtsklet.func)
  596. tasklet_kill(&op->thrtsklet);
  597. if ((op->frames) && (op->frames != &op->sframe))
  598. kfree(op->frames);
  599. if ((op->last_frames) && (op->last_frames != &op->last_sframe))
  600. kfree(op->last_frames);
  601. kfree(op);
  602. }
  603. static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
  604. {
  605. if (op->rx_reg_dev == dev) {
  606. can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
  607. bcm_rx_handler, op);
  608. /* mark as removed subscription */
  609. op->rx_reg_dev = NULL;
  610. } else
  611. printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
  612. "mismatch %p %p\n", op->rx_reg_dev, dev);
  613. }
  614. /*
  615. * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
  616. */
  617. static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
  618. {
  619. struct bcm_op *op, *n;
  620. list_for_each_entry_safe(op, n, ops, list) {
  621. if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
  622. /*
  623. * Don't care if we're bound or not (due to netdev
  624. * problems) can_rx_unregister() is always a save
  625. * thing to do here.
  626. */
  627. if (op->ifindex) {
  628. /*
  629. * Only remove subscriptions that had not
  630. * been removed due to NETDEV_UNREGISTER
  631. * in bcm_notifier()
  632. */
  633. if (op->rx_reg_dev) {
  634. struct net_device *dev;
  635. dev = dev_get_by_index(&init_net,
  636. op->ifindex);
  637. if (dev) {
  638. bcm_rx_unreg(dev, op);
  639. dev_put(dev);
  640. }
  641. }
  642. } else
  643. can_rx_unregister(NULL, op->can_id,
  644. REGMASK(op->can_id),
  645. bcm_rx_handler, op);
  646. list_del(&op->list);
  647. bcm_remove_op(op);
  648. return 1; /* done */
  649. }
  650. }
  651. return 0; /* not found */
  652. }
  653. /*
  654. * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
  655. */
  656. static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
  657. {
  658. struct bcm_op *op, *n;
  659. list_for_each_entry_safe(op, n, ops, list) {
  660. if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
  661. list_del(&op->list);
  662. bcm_remove_op(op);
  663. return 1; /* done */
  664. }
  665. }
  666. return 0; /* not found */
  667. }
  668. /*
  669. * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
  670. */
  671. static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
  672. int ifindex)
  673. {
  674. struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
  675. if (!op)
  676. return -EINVAL;
  677. /* put current values into msg_head */
  678. msg_head->flags = op->flags;
  679. msg_head->count = op->count;
  680. msg_head->ival1 = op->ival1;
  681. msg_head->ival2 = op->ival2;
  682. msg_head->nframes = op->nframes;
  683. bcm_send_to_user(op, msg_head, op->frames, 0);
  684. return MHSIZ;
  685. }
  686. /*
  687. * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
  688. */
  689. static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  690. int ifindex, struct sock *sk)
  691. {
  692. struct bcm_sock *bo = bcm_sk(sk);
  693. struct bcm_op *op;
  694. unsigned int i;
  695. int err;
  696. /* we need a real device to send frames */
  697. if (!ifindex)
  698. return -ENODEV;
  699. /* check nframes boundaries - we need at least one can_frame */
  700. if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
  701. return -EINVAL;
  702. /* check the given can_id */
  703. op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
  704. if (op) {
  705. /* update existing BCM operation */
  706. /*
  707. * Do we need more space for the can_frames than currently
  708. * allocated? -> This is a _really_ unusual use-case and
  709. * therefore (complexity / locking) it is not supported.
  710. */
  711. if (msg_head->nframes > op->nframes)
  712. return -E2BIG;
  713. /* update can_frames content */
  714. for (i = 0; i < msg_head->nframes; i++) {
  715. err = memcpy_fromiovec((u8 *)&op->frames[i],
  716. msg->msg_iov, CFSIZ);
  717. if (op->frames[i].can_dlc > 8)
  718. err = -EINVAL;
  719. if (err < 0)
  720. return err;
  721. if (msg_head->flags & TX_CP_CAN_ID) {
  722. /* copy can_id into frame */
  723. op->frames[i].can_id = msg_head->can_id;
  724. }
  725. }
  726. } else {
  727. /* insert new BCM operation for the given can_id */
  728. op = kzalloc(OPSIZ, GFP_KERNEL);
  729. if (!op)
  730. return -ENOMEM;
  731. op->can_id = msg_head->can_id;
  732. /* create array for can_frames and copy the data */
  733. if (msg_head->nframes > 1) {
  734. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  735. GFP_KERNEL);
  736. if (!op->frames) {
  737. kfree(op);
  738. return -ENOMEM;
  739. }
  740. } else
  741. op->frames = &op->sframe;
  742. for (i = 0; i < msg_head->nframes; i++) {
  743. err = memcpy_fromiovec((u8 *)&op->frames[i],
  744. msg->msg_iov, CFSIZ);
  745. if (op->frames[i].can_dlc > 8)
  746. err = -EINVAL;
  747. if (err < 0) {
  748. if (op->frames != &op->sframe)
  749. kfree(op->frames);
  750. kfree(op);
  751. return err;
  752. }
  753. if (msg_head->flags & TX_CP_CAN_ID) {
  754. /* copy can_id into frame */
  755. op->frames[i].can_id = msg_head->can_id;
  756. }
  757. }
  758. /* tx_ops never compare with previous received messages */
  759. op->last_frames = NULL;
  760. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  761. op->sk = sk;
  762. op->ifindex = ifindex;
  763. /* initialize uninitialized (kzalloc) structure */
  764. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  765. op->timer.function = bcm_tx_timeout_handler;
  766. /* initialize tasklet for tx countevent notification */
  767. tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
  768. (unsigned long) op);
  769. /* currently unused in tx_ops */
  770. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  771. /* add this bcm_op to the list of the tx_ops */
  772. list_add(&op->list, &bo->tx_ops);
  773. } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
  774. if (op->nframes != msg_head->nframes) {
  775. op->nframes = msg_head->nframes;
  776. /* start multiple frame transmission with index 0 */
  777. op->currframe = 0;
  778. }
  779. /* check flags */
  780. op->flags = msg_head->flags;
  781. if (op->flags & TX_RESET_MULTI_IDX) {
  782. /* start multiple frame transmission with index 0 */
  783. op->currframe = 0;
  784. }
  785. if (op->flags & SETTIMER) {
  786. /* set timer values */
  787. op->count = msg_head->count;
  788. op->ival1 = msg_head->ival1;
  789. op->ival2 = msg_head->ival2;
  790. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  791. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  792. /* disable an active timer due to zero values? */
  793. if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
  794. hrtimer_cancel(&op->timer);
  795. }
  796. if (op->flags & STARTTIMER) {
  797. hrtimer_cancel(&op->timer);
  798. /* spec: send can_frame when starting timer */
  799. op->flags |= TX_ANNOUNCE;
  800. }
  801. if (op->flags & TX_ANNOUNCE) {
  802. bcm_can_tx(op);
  803. if (op->count)
  804. op->count--;
  805. }
  806. if (op->flags & STARTTIMER)
  807. bcm_tx_start_timer(op);
  808. return msg_head->nframes * CFSIZ + MHSIZ;
  809. }
  810. /*
  811. * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
  812. */
  813. static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  814. int ifindex, struct sock *sk)
  815. {
  816. struct bcm_sock *bo = bcm_sk(sk);
  817. struct bcm_op *op;
  818. int do_rx_register;
  819. int err = 0;
  820. if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
  821. /* be robust against wrong usage ... */
  822. msg_head->flags |= RX_FILTER_ID;
  823. /* ignore trailing garbage */
  824. msg_head->nframes = 0;
  825. }
  826. /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
  827. if (msg_head->nframes > MAX_NFRAMES + 1)
  828. return -EINVAL;
  829. if ((msg_head->flags & RX_RTR_FRAME) &&
  830. ((msg_head->nframes != 1) ||
  831. (!(msg_head->can_id & CAN_RTR_FLAG))))
  832. return -EINVAL;
  833. /* check the given can_id */
  834. op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
  835. if (op) {
  836. /* update existing BCM operation */
  837. /*
  838. * Do we need more space for the can_frames than currently
  839. * allocated? -> This is a _really_ unusual use-case and
  840. * therefore (complexity / locking) it is not supported.
  841. */
  842. if (msg_head->nframes > op->nframes)
  843. return -E2BIG;
  844. if (msg_head->nframes) {
  845. /* update can_frames content */
  846. err = memcpy_fromiovec((u8 *)op->frames,
  847. msg->msg_iov,
  848. msg_head->nframes * CFSIZ);
  849. if (err < 0)
  850. return err;
  851. /* clear last_frames to indicate 'nothing received' */
  852. memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
  853. }
  854. op->nframes = msg_head->nframes;
  855. /* Only an update -> do not call can_rx_register() */
  856. do_rx_register = 0;
  857. } else {
  858. /* insert new BCM operation for the given can_id */
  859. op = kzalloc(OPSIZ, GFP_KERNEL);
  860. if (!op)
  861. return -ENOMEM;
  862. op->can_id = msg_head->can_id;
  863. op->nframes = msg_head->nframes;
  864. if (msg_head->nframes > 1) {
  865. /* create array for can_frames and copy the data */
  866. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  867. GFP_KERNEL);
  868. if (!op->frames) {
  869. kfree(op);
  870. return -ENOMEM;
  871. }
  872. /* create and init array for received can_frames */
  873. op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
  874. GFP_KERNEL);
  875. if (!op->last_frames) {
  876. kfree(op->frames);
  877. kfree(op);
  878. return -ENOMEM;
  879. }
  880. } else {
  881. op->frames = &op->sframe;
  882. op->last_frames = &op->last_sframe;
  883. }
  884. if (msg_head->nframes) {
  885. err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
  886. msg_head->nframes * CFSIZ);
  887. if (err < 0) {
  888. if (op->frames != &op->sframe)
  889. kfree(op->frames);
  890. if (op->last_frames != &op->last_sframe)
  891. kfree(op->last_frames);
  892. kfree(op);
  893. return err;
  894. }
  895. }
  896. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  897. op->sk = sk;
  898. op->ifindex = ifindex;
  899. /* initialize uninitialized (kzalloc) structure */
  900. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  901. op->timer.function = bcm_rx_timeout_handler;
  902. /* initialize tasklet for rx timeout notification */
  903. tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
  904. (unsigned long) op);
  905. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  906. op->thrtimer.function = bcm_rx_thr_handler;
  907. /* initialize tasklet for rx throttle handling */
  908. tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
  909. (unsigned long) op);
  910. /* add this bcm_op to the list of the rx_ops */
  911. list_add(&op->list, &bo->rx_ops);
  912. /* call can_rx_register() */
  913. do_rx_register = 1;
  914. } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
  915. /* check flags */
  916. op->flags = msg_head->flags;
  917. if (op->flags & RX_RTR_FRAME) {
  918. /* no timers in RTR-mode */
  919. hrtimer_cancel(&op->thrtimer);
  920. hrtimer_cancel(&op->timer);
  921. /*
  922. * funny feature in RX(!)_SETUP only for RTR-mode:
  923. * copy can_id into frame BUT without RTR-flag to
  924. * prevent a full-load-loopback-test ... ;-]
  925. */
  926. if ((op->flags & TX_CP_CAN_ID) ||
  927. (op->frames[0].can_id == op->can_id))
  928. op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
  929. } else {
  930. if (op->flags & SETTIMER) {
  931. /* set timer value */
  932. op->ival1 = msg_head->ival1;
  933. op->ival2 = msg_head->ival2;
  934. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  935. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  936. /* disable an active timer due to zero value? */
  937. if (!op->kt_ival1.tv64)
  938. hrtimer_cancel(&op->timer);
  939. /*
  940. * In any case cancel the throttle timer, flush
  941. * potentially blocked msgs and reset throttle handling
  942. */
  943. op->kt_lastmsg = ktime_set(0, 0);
  944. hrtimer_cancel(&op->thrtimer);
  945. bcm_rx_thr_flush(op, 1);
  946. }
  947. if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
  948. hrtimer_start(&op->timer, op->kt_ival1,
  949. HRTIMER_MODE_REL);
  950. }
  951. /* now we can register for can_ids, if we added a new bcm_op */
  952. if (do_rx_register) {
  953. if (ifindex) {
  954. struct net_device *dev;
  955. dev = dev_get_by_index(&init_net, ifindex);
  956. if (dev) {
  957. err = can_rx_register(dev, op->can_id,
  958. REGMASK(op->can_id),
  959. bcm_rx_handler, op,
  960. "bcm");
  961. op->rx_reg_dev = dev;
  962. dev_put(dev);
  963. }
  964. } else
  965. err = can_rx_register(NULL, op->can_id,
  966. REGMASK(op->can_id),
  967. bcm_rx_handler, op, "bcm");
  968. if (err) {
  969. /* this bcm rx op is broken -> remove it */
  970. list_del(&op->list);
  971. bcm_remove_op(op);
  972. return err;
  973. }
  974. }
  975. return msg_head->nframes * CFSIZ + MHSIZ;
  976. }
  977. /*
  978. * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
  979. */
  980. static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
  981. {
  982. struct sk_buff *skb;
  983. struct net_device *dev;
  984. int err;
  985. /* we need a real device to send frames */
  986. if (!ifindex)
  987. return -ENODEV;
  988. skb = alloc_skb(CFSIZ, GFP_KERNEL);
  989. if (!skb)
  990. return -ENOMEM;
  991. err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
  992. if (err < 0) {
  993. kfree_skb(skb);
  994. return err;
  995. }
  996. dev = dev_get_by_index(&init_net, ifindex);
  997. if (!dev) {
  998. kfree_skb(skb);
  999. return -ENODEV;
  1000. }
  1001. skb->dev = dev;
  1002. skb->sk = sk;
  1003. err = can_send(skb, 1); /* send with loopback */
  1004. dev_put(dev);
  1005. if (err)
  1006. return err;
  1007. return CFSIZ + MHSIZ;
  1008. }
  1009. /*
  1010. * bcm_sendmsg - process BCM commands (opcodes) from the userspace
  1011. */
  1012. static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
  1013. struct msghdr *msg, size_t size)
  1014. {
  1015. struct sock *sk = sock->sk;
  1016. struct bcm_sock *bo = bcm_sk(sk);
  1017. int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
  1018. struct bcm_msg_head msg_head;
  1019. int ret; /* read bytes or error codes as return value */
  1020. if (!bo->bound)
  1021. return -ENOTCONN;
  1022. /* check for valid message length from userspace */
  1023. if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
  1024. return -EINVAL;
  1025. /* check for alternative ifindex for this bcm_op */
  1026. if (!ifindex && msg->msg_name) {
  1027. /* no bound device as default => check msg_name */
  1028. struct sockaddr_can *addr =
  1029. (struct sockaddr_can *)msg->msg_name;
  1030. if (msg->msg_namelen < sizeof(*addr))
  1031. return -EINVAL;
  1032. if (addr->can_family != AF_CAN)
  1033. return -EINVAL;
  1034. /* ifindex from sendto() */
  1035. ifindex = addr->can_ifindex;
  1036. if (ifindex) {
  1037. struct net_device *dev;
  1038. dev = dev_get_by_index(&init_net, ifindex);
  1039. if (!dev)
  1040. return -ENODEV;
  1041. if (dev->type != ARPHRD_CAN) {
  1042. dev_put(dev);
  1043. return -ENODEV;
  1044. }
  1045. dev_put(dev);
  1046. }
  1047. }
  1048. /* read message head information */
  1049. ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
  1050. if (ret < 0)
  1051. return ret;
  1052. lock_sock(sk);
  1053. switch (msg_head.opcode) {
  1054. case TX_SETUP:
  1055. ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
  1056. break;
  1057. case RX_SETUP:
  1058. ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
  1059. break;
  1060. case TX_DELETE:
  1061. if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
  1062. ret = MHSIZ;
  1063. else
  1064. ret = -EINVAL;
  1065. break;
  1066. case RX_DELETE:
  1067. if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
  1068. ret = MHSIZ;
  1069. else
  1070. ret = -EINVAL;
  1071. break;
  1072. case TX_READ:
  1073. /* reuse msg_head for the reply to TX_READ */
  1074. msg_head.opcode = TX_STATUS;
  1075. ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
  1076. break;
  1077. case RX_READ:
  1078. /* reuse msg_head for the reply to RX_READ */
  1079. msg_head.opcode = RX_STATUS;
  1080. ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
  1081. break;
  1082. case TX_SEND:
  1083. /* we need exactly one can_frame behind the msg head */
  1084. if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
  1085. ret = -EINVAL;
  1086. else
  1087. ret = bcm_tx_send(msg, ifindex, sk);
  1088. break;
  1089. default:
  1090. ret = -EINVAL;
  1091. break;
  1092. }
  1093. release_sock(sk);
  1094. return ret;
  1095. }
  1096. /*
  1097. * notification handler for netdevice status changes
  1098. */
  1099. static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
  1100. void *data)
  1101. {
  1102. struct net_device *dev = (struct net_device *)data;
  1103. struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
  1104. struct sock *sk = &bo->sk;
  1105. struct bcm_op *op;
  1106. int notify_enodev = 0;
  1107. if (!net_eq(dev_net(dev), &init_net))
  1108. return NOTIFY_DONE;
  1109. if (dev->type != ARPHRD_CAN)
  1110. return NOTIFY_DONE;
  1111. switch (msg) {
  1112. case NETDEV_UNREGISTER:
  1113. lock_sock(sk);
  1114. /* remove device specific receive entries */
  1115. list_for_each_entry(op, &bo->rx_ops, list)
  1116. if (op->rx_reg_dev == dev)
  1117. bcm_rx_unreg(dev, op);
  1118. /* remove device reference, if this is our bound device */
  1119. if (bo->bound && bo->ifindex == dev->ifindex) {
  1120. bo->bound = 0;
  1121. bo->ifindex = 0;
  1122. notify_enodev = 1;
  1123. }
  1124. release_sock(sk);
  1125. if (notify_enodev) {
  1126. sk->sk_err = ENODEV;
  1127. if (!sock_flag(sk, SOCK_DEAD))
  1128. sk->sk_error_report(sk);
  1129. }
  1130. break;
  1131. case NETDEV_DOWN:
  1132. if (bo->bound && bo->ifindex == dev->ifindex) {
  1133. sk->sk_err = ENETDOWN;
  1134. if (!sock_flag(sk, SOCK_DEAD))
  1135. sk->sk_error_report(sk);
  1136. }
  1137. }
  1138. return NOTIFY_DONE;
  1139. }
  1140. /*
  1141. * initial settings for all BCM sockets to be set at socket creation time
  1142. */
  1143. static int bcm_init(struct sock *sk)
  1144. {
  1145. struct bcm_sock *bo = bcm_sk(sk);
  1146. bo->bound = 0;
  1147. bo->ifindex = 0;
  1148. bo->dropped_usr_msgs = 0;
  1149. bo->bcm_proc_read = NULL;
  1150. INIT_LIST_HEAD(&bo->tx_ops);
  1151. INIT_LIST_HEAD(&bo->rx_ops);
  1152. /* set notifier */
  1153. bo->notifier.notifier_call = bcm_notifier;
  1154. register_netdevice_notifier(&bo->notifier);
  1155. return 0;
  1156. }
  1157. /*
  1158. * standard socket functions
  1159. */
  1160. static int bcm_release(struct socket *sock)
  1161. {
  1162. struct sock *sk = sock->sk;
  1163. struct bcm_sock *bo;
  1164. struct bcm_op *op, *next;
  1165. if (sk == NULL)
  1166. return 0;
  1167. bo = bcm_sk(sk);
  1168. /* remove bcm_ops, timer, rx_unregister(), etc. */
  1169. unregister_netdevice_notifier(&bo->notifier);
  1170. lock_sock(sk);
  1171. list_for_each_entry_safe(op, next, &bo->tx_ops, list)
  1172. bcm_remove_op(op);
  1173. list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
  1174. /*
  1175. * Don't care if we're bound or not (due to netdev problems)
  1176. * can_rx_unregister() is always a save thing to do here.
  1177. */
  1178. if (op->ifindex) {
  1179. /*
  1180. * Only remove subscriptions that had not
  1181. * been removed due to NETDEV_UNREGISTER
  1182. * in bcm_notifier()
  1183. */
  1184. if (op->rx_reg_dev) {
  1185. struct net_device *dev;
  1186. dev = dev_get_by_index(&init_net, op->ifindex);
  1187. if (dev) {
  1188. bcm_rx_unreg(dev, op);
  1189. dev_put(dev);
  1190. }
  1191. }
  1192. } else
  1193. can_rx_unregister(NULL, op->can_id,
  1194. REGMASK(op->can_id),
  1195. bcm_rx_handler, op);
  1196. bcm_remove_op(op);
  1197. }
  1198. /* remove procfs entry */
  1199. if (proc_dir && bo->bcm_proc_read)
  1200. remove_proc_entry(bo->procname, proc_dir);
  1201. /* remove device reference */
  1202. if (bo->bound) {
  1203. bo->bound = 0;
  1204. bo->ifindex = 0;
  1205. }
  1206. sock_orphan(sk);
  1207. sock->sk = NULL;
  1208. release_sock(sk);
  1209. sock_put(sk);
  1210. return 0;
  1211. }
  1212. static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
  1213. int flags)
  1214. {
  1215. struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
  1216. struct sock *sk = sock->sk;
  1217. struct bcm_sock *bo = bcm_sk(sk);
  1218. if (len < sizeof(*addr))
  1219. return -EINVAL;
  1220. if (bo->bound)
  1221. return -EISCONN;
  1222. /* bind a device to this socket */
  1223. if (addr->can_ifindex) {
  1224. struct net_device *dev;
  1225. dev = dev_get_by_index(&init_net, addr->can_ifindex);
  1226. if (!dev)
  1227. return -ENODEV;
  1228. if (dev->type != ARPHRD_CAN) {
  1229. dev_put(dev);
  1230. return -ENODEV;
  1231. }
  1232. bo->ifindex = dev->ifindex;
  1233. dev_put(dev);
  1234. } else {
  1235. /* no interface reference for ifindex = 0 ('any' CAN device) */
  1236. bo->ifindex = 0;
  1237. }
  1238. bo->bound = 1;
  1239. if (proc_dir) {
  1240. /* unique socket address as filename */
  1241. sprintf(bo->procname, "%lu", sock_i_ino(sk));
  1242. bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
  1243. proc_dir,
  1244. &bcm_proc_fops, sk);
  1245. }
  1246. return 0;
  1247. }
  1248. static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
  1249. struct msghdr *msg, size_t size, int flags)
  1250. {
  1251. struct sock *sk = sock->sk;
  1252. struct sk_buff *skb;
  1253. int error = 0;
  1254. int noblock;
  1255. int err;
  1256. noblock = flags & MSG_DONTWAIT;
  1257. flags &= ~MSG_DONTWAIT;
  1258. skb = skb_recv_datagram(sk, flags, noblock, &error);
  1259. if (!skb)
  1260. return error;
  1261. if (skb->len < size)
  1262. size = skb->len;
  1263. err = memcpy_toiovec(msg->msg_iov, skb->data, size);
  1264. if (err < 0) {
  1265. skb_free_datagram(sk, skb);
  1266. return err;
  1267. }
  1268. sock_recv_ts_and_drops(msg, sk, skb);
  1269. if (msg->msg_name) {
  1270. msg->msg_namelen = sizeof(struct sockaddr_can);
  1271. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  1272. }
  1273. skb_free_datagram(sk, skb);
  1274. return size;
  1275. }
  1276. static const struct proto_ops bcm_ops = {
  1277. .family = PF_CAN,
  1278. .release = bcm_release,
  1279. .bind = sock_no_bind,
  1280. .connect = bcm_connect,
  1281. .socketpair = sock_no_socketpair,
  1282. .accept = sock_no_accept,
  1283. .getname = sock_no_getname,
  1284. .poll = datagram_poll,
  1285. .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
  1286. .listen = sock_no_listen,
  1287. .shutdown = sock_no_shutdown,
  1288. .setsockopt = sock_no_setsockopt,
  1289. .getsockopt = sock_no_getsockopt,
  1290. .sendmsg = bcm_sendmsg,
  1291. .recvmsg = bcm_recvmsg,
  1292. .mmap = sock_no_mmap,
  1293. .sendpage = sock_no_sendpage,
  1294. };
  1295. static struct proto bcm_proto __read_mostly = {
  1296. .name = "CAN_BCM",
  1297. .owner = THIS_MODULE,
  1298. .obj_size = sizeof(struct bcm_sock),
  1299. .init = bcm_init,
  1300. };
  1301. static const struct can_proto bcm_can_proto = {
  1302. .type = SOCK_DGRAM,
  1303. .protocol = CAN_BCM,
  1304. .ops = &bcm_ops,
  1305. .prot = &bcm_proto,
  1306. };
  1307. static int __init bcm_module_init(void)
  1308. {
  1309. int err;
  1310. printk(banner);
  1311. err = can_proto_register(&bcm_can_proto);
  1312. if (err < 0) {
  1313. printk(KERN_ERR "can: registration of bcm protocol failed\n");
  1314. return err;
  1315. }
  1316. /* create /proc/net/can-bcm directory */
  1317. proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
  1318. return 0;
  1319. }
  1320. static void __exit bcm_module_exit(void)
  1321. {
  1322. can_proto_unregister(&bcm_can_proto);
  1323. if (proc_dir)
  1324. proc_net_remove(&init_net, "can-bcm");
  1325. }
  1326. module_init(bcm_module_init);
  1327. module_exit(bcm_module_exit);