bcm.c 45 KB

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