bcm.c 37 KB

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