bcm.c 39 KB

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