bcm.c 38 KB

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