bcm.c 38 KB

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