bcm.c 42 KB

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