bcm.c 42 KB

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