bcm.c 39 KB

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