bcm.c 41 KB

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