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