bcm.c 39 KB

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