bcm.c 41 KB

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