bcm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. */
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/hrtimer.h>
  45. #include <linux/list.h>
  46. #include <linux/proc_fs.h>
  47. #include <linux/seq_file.h>
  48. #include <linux/uio.h>
  49. #include <linux/net.h>
  50. #include <linux/netdevice.h>
  51. #include <linux/socket.h>
  52. #include <linux/if_arp.h>
  53. #include <linux/skbuff.h>
  54. #include <linux/can.h>
  55. #include <linux/can/core.h>
  56. #include <linux/can/skb.h>
  57. #include <linux/can/bcm.h>
  58. #include <linux/slab.h>
  59. #include <net/sock.h>
  60. #include <net/net_namespace.h>
  61. /*
  62. * To send multiple CAN frame content within TX_SETUP or to filter
  63. * CAN messages with multiplex index within RX_SETUP, the number of
  64. * different filters is limited to 256 due to the one byte index value.
  65. */
  66. #define MAX_NFRAMES 256
  67. /* use of last_frames[index].can_dlc */
  68. #define RX_RECV 0x40 /* received data for this element */
  69. #define RX_THR 0x80 /* element not been sent due to throttle feature */
  70. #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
  71. /* get best masking value for can_rx_register() for a given single can_id */
  72. #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
  73. (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
  74. (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
  75. #define CAN_BCM_VERSION CAN_VERSION
  76. MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
  77. MODULE_LICENSE("Dual BSD/GPL");
  78. MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  79. MODULE_ALIAS("can-proto-2");
  80. /* easy access to can_frame payload */
  81. static inline u64 GET_U64(const struct can_frame *cp)
  82. {
  83. return *(u64 *)cp->data;
  84. }
  85. struct bcm_op {
  86. struct list_head list;
  87. int ifindex;
  88. canid_t can_id;
  89. u32 flags;
  90. unsigned long frames_abs, frames_filtered;
  91. struct timeval ival1, ival2;
  92. struct hrtimer timer, thrtimer;
  93. struct tasklet_struct tsklet, thrtsklet;
  94. ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
  95. int rx_ifindex;
  96. u32 count;
  97. u32 nframes;
  98. u32 currframe;
  99. struct can_frame *frames;
  100. struct can_frame *last_frames;
  101. struct can_frame sframe;
  102. struct can_frame last_sframe;
  103. struct sock *sk;
  104. struct net_device *rx_reg_dev;
  105. };
  106. static struct proc_dir_entry *proc_dir;
  107. struct bcm_sock {
  108. struct sock sk;
  109. int bound;
  110. int ifindex;
  111. struct notifier_block notifier;
  112. struct list_head rx_ops;
  113. struct list_head tx_ops;
  114. unsigned long dropped_usr_msgs;
  115. struct proc_dir_entry *bcm_proc_read;
  116. char procname [32]; /* inode number in decimal with \0 */
  117. };
  118. static inline struct bcm_sock *bcm_sk(const struct sock *sk)
  119. {
  120. return (struct bcm_sock *)sk;
  121. }
  122. #define CFSIZ sizeof(struct can_frame)
  123. #define OPSIZ sizeof(struct bcm_op)
  124. #define MHSIZ sizeof(struct bcm_msg_head)
  125. /*
  126. * procfs functions
  127. */
  128. static char *bcm_proc_getifname(char *result, int ifindex)
  129. {
  130. struct net_device *dev;
  131. if (!ifindex)
  132. return "any";
  133. rcu_read_lock();
  134. dev = dev_get_by_index_rcu(&init_net, ifindex);
  135. if (dev)
  136. strcpy(result, dev->name);
  137. else
  138. strcpy(result, "???");
  139. rcu_read_unlock();
  140. return result;
  141. }
  142. static int bcm_proc_show(struct seq_file *m, void *v)
  143. {
  144. char ifname[IFNAMSIZ];
  145. struct sock *sk = (struct sock *)m->private;
  146. struct bcm_sock *bo = bcm_sk(sk);
  147. struct bcm_op *op;
  148. seq_printf(m, ">>> socket %pK", sk->sk_socket);
  149. seq_printf(m, " / sk %pK", sk);
  150. seq_printf(m, " / bo %pK", bo);
  151. seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
  152. seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
  153. seq_printf(m, " <<<\n");
  154. list_for_each_entry(op, &bo->rx_ops, list) {
  155. unsigned long reduction;
  156. /* print only active entries & prevent division by zero */
  157. if (!op->frames_abs)
  158. continue;
  159. seq_printf(m, "rx_op: %03X %-5s ",
  160. op->can_id, bcm_proc_getifname(ifname, op->ifindex));
  161. seq_printf(m, "[%u]%c ", op->nframes,
  162. (op->flags & RX_CHECK_DLC)?'d':' ');
  163. if (op->kt_ival1.tv64)
  164. seq_printf(m, "timeo=%lld ",
  165. (long long)
  166. ktime_to_us(op->kt_ival1));
  167. if (op->kt_ival2.tv64)
  168. seq_printf(m, "thr=%lld ",
  169. (long long)
  170. ktime_to_us(op->kt_ival2));
  171. seq_printf(m, "# recv %ld (%ld) => reduction: ",
  172. op->frames_filtered, op->frames_abs);
  173. reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
  174. seq_printf(m, "%s%ld%%\n",
  175. (reduction == 100)?"near ":"", reduction);
  176. }
  177. list_for_each_entry(op, &bo->tx_ops, list) {
  178. seq_printf(m, "tx_op: %03X %s [%u] ",
  179. op->can_id,
  180. bcm_proc_getifname(ifname, op->ifindex),
  181. op->nframes);
  182. if (op->kt_ival1.tv64)
  183. seq_printf(m, "t1=%lld ",
  184. (long long) ktime_to_us(op->kt_ival1));
  185. if (op->kt_ival2.tv64)
  186. seq_printf(m, "t2=%lld ",
  187. (long long) ktime_to_us(op->kt_ival2));
  188. seq_printf(m, "# sent %ld\n", op->frames_abs);
  189. }
  190. seq_putc(m, '\n');
  191. return 0;
  192. }
  193. static int bcm_proc_open(struct inode *inode, struct file *file)
  194. {
  195. return single_open(file, bcm_proc_show, PDE_DATA(inode));
  196. }
  197. static const struct file_operations bcm_proc_fops = {
  198. .owner = THIS_MODULE,
  199. .open = bcm_proc_open,
  200. .read = seq_read,
  201. .llseek = seq_lseek,
  202. .release = single_release,
  203. };
  204. /*
  205. * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
  206. * of the given bcm tx op
  207. */
  208. static void bcm_can_tx(struct bcm_op *op)
  209. {
  210. struct sk_buff *skb;
  211. struct net_device *dev;
  212. struct can_frame *cf = &op->frames[op->currframe];
  213. /* no target device? => exit */
  214. if (!op->ifindex)
  215. return;
  216. dev = dev_get_by_index(&init_net, op->ifindex);
  217. if (!dev) {
  218. /* RFC: should this bcm_op remove itself here? */
  219. return;
  220. }
  221. skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), gfp_any());
  222. if (!skb)
  223. goto out;
  224. can_skb_reserve(skb);
  225. can_skb_prv(skb)->ifindex = dev->ifindex;
  226. can_skb_prv(skb)->skbcnt = 0;
  227. memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
  228. /* send with loopback */
  229. skb->dev = dev;
  230. can_skb_set_owner(skb, 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. sock_skb_cb_check_size(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. /* throttling 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 reception 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 comparison,
  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 reception
  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 reception 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 reception
  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_from_msg((u8 *)&op->frames[i], msg, CFSIZ);
  716. if (op->frames[i].can_dlc > 8)
  717. err = -EINVAL;
  718. if (err < 0)
  719. return err;
  720. if (msg_head->flags & TX_CP_CAN_ID) {
  721. /* copy can_id into frame */
  722. op->frames[i].can_id = msg_head->can_id;
  723. }
  724. }
  725. } else {
  726. /* insert new BCM operation for the given can_id */
  727. op = kzalloc(OPSIZ, GFP_KERNEL);
  728. if (!op)
  729. return -ENOMEM;
  730. op->can_id = msg_head->can_id;
  731. /* create array for can_frames and copy the data */
  732. if (msg_head->nframes > 1) {
  733. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  734. GFP_KERNEL);
  735. if (!op->frames) {
  736. kfree(op);
  737. return -ENOMEM;
  738. }
  739. } else
  740. op->frames = &op->sframe;
  741. for (i = 0; i < msg_head->nframes; i++) {
  742. err = memcpy_from_msg((u8 *)&op->frames[i], msg, CFSIZ);
  743. if (op->frames[i].can_dlc > 8)
  744. err = -EINVAL;
  745. if (err < 0) {
  746. if (op->frames != &op->sframe)
  747. kfree(op->frames);
  748. kfree(op);
  749. return err;
  750. }
  751. if (msg_head->flags & TX_CP_CAN_ID) {
  752. /* copy can_id into frame */
  753. op->frames[i].can_id = msg_head->can_id;
  754. }
  755. }
  756. /* tx_ops never compare with previous received messages */
  757. op->last_frames = NULL;
  758. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  759. op->sk = sk;
  760. op->ifindex = ifindex;
  761. /* initialize uninitialized (kzalloc) structure */
  762. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  763. op->timer.function = bcm_tx_timeout_handler;
  764. /* initialize tasklet for tx countevent notification */
  765. tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
  766. (unsigned long) op);
  767. /* currently unused in tx_ops */
  768. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  769. /* add this bcm_op to the list of the tx_ops */
  770. list_add(&op->list, &bo->tx_ops);
  771. } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
  772. if (op->nframes != msg_head->nframes) {
  773. op->nframes = msg_head->nframes;
  774. /* start multiple frame transmission with index 0 */
  775. op->currframe = 0;
  776. }
  777. /* check flags */
  778. op->flags = msg_head->flags;
  779. if (op->flags & TX_RESET_MULTI_IDX) {
  780. /* start multiple frame transmission with index 0 */
  781. op->currframe = 0;
  782. }
  783. if (op->flags & SETTIMER) {
  784. /* set timer values */
  785. op->count = msg_head->count;
  786. op->ival1 = msg_head->ival1;
  787. op->ival2 = msg_head->ival2;
  788. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  789. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  790. /* disable an active timer due to zero values? */
  791. if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
  792. hrtimer_cancel(&op->timer);
  793. }
  794. if (op->flags & STARTTIMER) {
  795. hrtimer_cancel(&op->timer);
  796. /* spec: send can_frame when starting timer */
  797. op->flags |= TX_ANNOUNCE;
  798. }
  799. if (op->flags & TX_ANNOUNCE) {
  800. bcm_can_tx(op);
  801. if (op->count)
  802. op->count--;
  803. }
  804. if (op->flags & STARTTIMER)
  805. bcm_tx_start_timer(op);
  806. return msg_head->nframes * CFSIZ + MHSIZ;
  807. }
  808. /*
  809. * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
  810. */
  811. static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
  812. int ifindex, struct sock *sk)
  813. {
  814. struct bcm_sock *bo = bcm_sk(sk);
  815. struct bcm_op *op;
  816. int do_rx_register;
  817. int err = 0;
  818. if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
  819. /* be robust against wrong usage ... */
  820. msg_head->flags |= RX_FILTER_ID;
  821. /* ignore trailing garbage */
  822. msg_head->nframes = 0;
  823. }
  824. /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
  825. if (msg_head->nframes > MAX_NFRAMES + 1)
  826. return -EINVAL;
  827. if ((msg_head->flags & RX_RTR_FRAME) &&
  828. ((msg_head->nframes != 1) ||
  829. (!(msg_head->can_id & CAN_RTR_FLAG))))
  830. return -EINVAL;
  831. /* check the given can_id */
  832. op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
  833. if (op) {
  834. /* update existing BCM operation */
  835. /*
  836. * Do we need more space for the can_frames than currently
  837. * allocated? -> This is a _really_ unusual use-case and
  838. * therefore (complexity / locking) it is not supported.
  839. */
  840. if (msg_head->nframes > op->nframes)
  841. return -E2BIG;
  842. if (msg_head->nframes) {
  843. /* update can_frames content */
  844. err = memcpy_from_msg((u8 *)op->frames, msg,
  845. msg_head->nframes * CFSIZ);
  846. if (err < 0)
  847. return err;
  848. /* clear last_frames to indicate 'nothing received' */
  849. memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
  850. }
  851. op->nframes = msg_head->nframes;
  852. /* Only an update -> do not call can_rx_register() */
  853. do_rx_register = 0;
  854. } else {
  855. /* insert new BCM operation for the given can_id */
  856. op = kzalloc(OPSIZ, GFP_KERNEL);
  857. if (!op)
  858. return -ENOMEM;
  859. op->can_id = msg_head->can_id;
  860. op->nframes = msg_head->nframes;
  861. if (msg_head->nframes > 1) {
  862. /* create array for can_frames and copy the data */
  863. op->frames = kmalloc(msg_head->nframes * CFSIZ,
  864. GFP_KERNEL);
  865. if (!op->frames) {
  866. kfree(op);
  867. return -ENOMEM;
  868. }
  869. /* create and init array for received can_frames */
  870. op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
  871. GFP_KERNEL);
  872. if (!op->last_frames) {
  873. kfree(op->frames);
  874. kfree(op);
  875. return -ENOMEM;
  876. }
  877. } else {
  878. op->frames = &op->sframe;
  879. op->last_frames = &op->last_sframe;
  880. }
  881. if (msg_head->nframes) {
  882. err = memcpy_from_msg((u8 *)op->frames, msg,
  883. msg_head->nframes * CFSIZ);
  884. if (err < 0) {
  885. if (op->frames != &op->sframe)
  886. kfree(op->frames);
  887. if (op->last_frames != &op->last_sframe)
  888. kfree(op->last_frames);
  889. kfree(op);
  890. return err;
  891. }
  892. }
  893. /* bcm_can_tx / bcm_tx_timeout_handler needs this */
  894. op->sk = sk;
  895. op->ifindex = ifindex;
  896. /* ifindex for timeout events w/o previous frame reception */
  897. op->rx_ifindex = ifindex;
  898. /* initialize uninitialized (kzalloc) structure */
  899. hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  900. op->timer.function = bcm_rx_timeout_handler;
  901. /* initialize tasklet for rx timeout notification */
  902. tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
  903. (unsigned long) op);
  904. hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  905. op->thrtimer.function = bcm_rx_thr_handler;
  906. /* initialize tasklet for rx throttle handling */
  907. tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
  908. (unsigned long) op);
  909. /* add this bcm_op to the list of the rx_ops */
  910. list_add(&op->list, &bo->rx_ops);
  911. /* call can_rx_register() */
  912. do_rx_register = 1;
  913. } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
  914. /* check flags */
  915. op->flags = msg_head->flags;
  916. if (op->flags & RX_RTR_FRAME) {
  917. /* no timers in RTR-mode */
  918. hrtimer_cancel(&op->thrtimer);
  919. hrtimer_cancel(&op->timer);
  920. /*
  921. * funny feature in RX(!)_SETUP only for RTR-mode:
  922. * copy can_id into frame BUT without RTR-flag to
  923. * prevent a full-load-loopback-test ... ;-]
  924. */
  925. if ((op->flags & TX_CP_CAN_ID) ||
  926. (op->frames[0].can_id == op->can_id))
  927. op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
  928. } else {
  929. if (op->flags & SETTIMER) {
  930. /* set timer value */
  931. op->ival1 = msg_head->ival1;
  932. op->ival2 = msg_head->ival2;
  933. op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
  934. op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
  935. /* disable an active timer due to zero value? */
  936. if (!op->kt_ival1.tv64)
  937. hrtimer_cancel(&op->timer);
  938. /*
  939. * In any case cancel the throttle timer, flush
  940. * potentially blocked msgs and reset throttle handling
  941. */
  942. op->kt_lastmsg = ktime_set(0, 0);
  943. hrtimer_cancel(&op->thrtimer);
  944. bcm_rx_thr_flush(op, 1);
  945. }
  946. if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
  947. hrtimer_start(&op->timer, op->kt_ival1,
  948. HRTIMER_MODE_REL);
  949. }
  950. /* now we can register for can_ids, if we added a new bcm_op */
  951. if (do_rx_register) {
  952. if (ifindex) {
  953. struct net_device *dev;
  954. dev = dev_get_by_index(&init_net, ifindex);
  955. if (dev) {
  956. err = can_rx_register(dev, op->can_id,
  957. REGMASK(op->can_id),
  958. bcm_rx_handler, op,
  959. "bcm");
  960. op->rx_reg_dev = dev;
  961. dev_put(dev);
  962. }
  963. } else
  964. err = can_rx_register(NULL, op->can_id,
  965. REGMASK(op->can_id),
  966. bcm_rx_handler, op, "bcm");
  967. if (err) {
  968. /* this bcm rx op is broken -> remove it */
  969. list_del(&op->list);
  970. bcm_remove_op(op);
  971. return err;
  972. }
  973. }
  974. return msg_head->nframes * CFSIZ + MHSIZ;
  975. }
  976. /*
  977. * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
  978. */
  979. static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
  980. {
  981. struct sk_buff *skb;
  982. struct net_device *dev;
  983. int err;
  984. /* we need a real device to send frames */
  985. if (!ifindex)
  986. return -ENODEV;
  987. skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), GFP_KERNEL);
  988. if (!skb)
  989. return -ENOMEM;
  990. can_skb_reserve(skb);
  991. err = memcpy_from_msg(skb_put(skb, CFSIZ), msg, 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. can_skb_prv(skb)->ifindex = dev->ifindex;
  1002. can_skb_prv(skb)->skbcnt = 0;
  1003. skb->dev = dev;
  1004. can_skb_set_owner(skb, sk);
  1005. err = can_send(skb, 1); /* send with loopback */
  1006. dev_put(dev);
  1007. if (err)
  1008. return err;
  1009. return CFSIZ + MHSIZ;
  1010. }
  1011. /*
  1012. * bcm_sendmsg - process BCM commands (opcodes) from the userspace
  1013. */
  1014. static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  1015. {
  1016. struct sock *sk = sock->sk;
  1017. struct bcm_sock *bo = bcm_sk(sk);
  1018. int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
  1019. struct bcm_msg_head msg_head;
  1020. int ret; /* read bytes or error codes as return value */
  1021. if (!bo->bound)
  1022. return -ENOTCONN;
  1023. /* check for valid message length from userspace */
  1024. if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
  1025. return -EINVAL;
  1026. /* check for alternative ifindex for this bcm_op */
  1027. if (!ifindex && msg->msg_name) {
  1028. /* no bound device as default => check msg_name */
  1029. DECLARE_SOCKADDR(struct sockaddr_can *, addr, 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_from_msg((u8 *)&msg_head, msg, 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 *ptr)
  1101. {
  1102. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  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 socket *sock, struct msghdr *msg, size_t size,
  1249. 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_to_msg(msg, 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. __sockaddr_check_size(sizeof(struct sockaddr_can));
  1271. msg->msg_namelen = sizeof(struct sockaddr_can);
  1272. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  1273. }
  1274. skb_free_datagram(sk, skb);
  1275. return size;
  1276. }
  1277. static const struct proto_ops bcm_ops = {
  1278. .family = PF_CAN,
  1279. .release = bcm_release,
  1280. .bind = sock_no_bind,
  1281. .connect = bcm_connect,
  1282. .socketpair = sock_no_socketpair,
  1283. .accept = sock_no_accept,
  1284. .getname = sock_no_getname,
  1285. .poll = datagram_poll,
  1286. .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
  1287. .listen = sock_no_listen,
  1288. .shutdown = sock_no_shutdown,
  1289. .setsockopt = sock_no_setsockopt,
  1290. .getsockopt = sock_no_getsockopt,
  1291. .sendmsg = bcm_sendmsg,
  1292. .recvmsg = bcm_recvmsg,
  1293. .mmap = sock_no_mmap,
  1294. .sendpage = sock_no_sendpage,
  1295. };
  1296. static struct proto bcm_proto __read_mostly = {
  1297. .name = "CAN_BCM",
  1298. .owner = THIS_MODULE,
  1299. .obj_size = sizeof(struct bcm_sock),
  1300. .init = bcm_init,
  1301. };
  1302. static const struct can_proto bcm_can_proto = {
  1303. .type = SOCK_DGRAM,
  1304. .protocol = CAN_BCM,
  1305. .ops = &bcm_ops,
  1306. .prot = &bcm_proto,
  1307. };
  1308. static int __init bcm_module_init(void)
  1309. {
  1310. int err;
  1311. pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
  1312. err = can_proto_register(&bcm_can_proto);
  1313. if (err < 0) {
  1314. printk(KERN_ERR "can: registration of bcm protocol failed\n");
  1315. return err;
  1316. }
  1317. /* create /proc/net/can-bcm directory */
  1318. proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
  1319. return 0;
  1320. }
  1321. static void __exit bcm_module_exit(void)
  1322. {
  1323. can_proto_unregister(&bcm_can_proto);
  1324. if (proc_dir)
  1325. remove_proc_entry("can-bcm", init_net.proc_net);
  1326. }
  1327. module_init(bcm_module_init);
  1328. module_exit(bcm_module_exit);