bcm.c 39 KB

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