bcm.c 39 KB

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