bcm.c 38 KB

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