bcm.c 43 KB

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