bcm.c 38 KB

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