bcm.c 39 KB

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