bcm.c 39 KB

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