]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/bcm.c
Omit unneeded skb_clone() calls.
[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                 return;
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                 return;
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_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_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_starttimer:
709         bcm_rx_starttimer(op);
710 }
711
712 /*
713  * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
714  */
715 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
716                                   int ifindex)
717 {
718         struct bcm_op *op;
719
720         list_for_each_entry(op, ops, list) {
721                 if ((op->can_id == can_id) && (op->ifindex == ifindex))
722                         return op;
723         }
724
725         return NULL;
726 }
727
728 static void bcm_remove_op(struct bcm_op *op)
729 {
730         hrtimer_cancel(&op->timer);
731         hrtimer_cancel(&op->thrtimer);
732
733         if (op->tsklet.func)
734                 tasklet_kill(&op->tsklet);
735
736         if (op->thrtsklet.func)
737                 tasklet_kill(&op->thrtsklet);
738
739         if ((op->frames) && (op->frames != &op->sframe))
740                 kfree(op->frames);
741
742         if ((op->last_frames) && (op->last_frames != &op->last_sframe))
743                 kfree(op->last_frames);
744
745         kfree(op);
746
747         return;
748 }
749
750 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
751 {
752         if (op->rx_reg_dev == dev) {
753                 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
754                                   bcm_rx_handler, op);
755
756                 /* mark as removed subscription */
757                 op->rx_reg_dev = NULL;
758         } else
759                 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
760                        "mismatch %p %p\n", op->rx_reg_dev, dev);
761 }
762
763 /*
764  * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
765  */
766 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
767 {
768         struct bcm_op *op, *n;
769
770         list_for_each_entry_safe(op, n, ops, list) {
771                 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
772
773                         /*
774                          * Don't care if we're bound or not (due to netdev
775                          * problems) can_rx_unregister() is always a save
776                          * thing to do here.
777                          */
778                         if (op->ifindex) {
779                                 /*
780                                  * Only remove subscriptions that had not
781                                  * been removed due to NETDEV_UNREGISTER
782                                  * in bcm_notifier()
783                                  */
784                                 if (op->rx_reg_dev) {
785                                         struct net_device *dev;
786
787                                         dev = dev_get_by_index(&init_net,
788                                                                op->ifindex);
789                                         if (dev) {
790                                                 bcm_rx_unreg(dev, op);
791                                                 dev_put(dev);
792                                         }
793                                 }
794                         } else
795                                 can_rx_unregister(NULL, op->can_id,
796                                                   REGMASK(op->can_id),
797                                                   bcm_rx_handler, op);
798
799                         list_del(&op->list);
800                         bcm_remove_op(op);
801                         return 1; /* done */
802                 }
803         }
804
805         return 0; /* not found */
806 }
807
808 /*
809  * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
810  */
811 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
812 {
813         struct bcm_op *op, *n;
814
815         list_for_each_entry_safe(op, n, ops, list) {
816                 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
817                         list_del(&op->list);
818                         bcm_remove_op(op);
819                         return 1; /* done */
820                 }
821         }
822
823         return 0; /* not found */
824 }
825
826 /*
827  * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
828  */
829 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
830                        int ifindex)
831 {
832         struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
833
834         if (!op)
835                 return -EINVAL;
836
837         /* put current values into msg_head */
838         msg_head->flags   = op->flags;
839         msg_head->count   = op->count;
840         msg_head->ival1   = op->ival1;
841         msg_head->ival2   = op->ival2;
842         msg_head->nframes = op->nframes;
843
844         bcm_send_to_user(op, msg_head, op->frames, 0);
845
846         return MHSIZ;
847 }
848
849 /*
850  * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
851  */
852 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
853                         int ifindex, struct sock *sk)
854 {
855         struct bcm_sock *bo = bcm_sk(sk);
856         struct bcm_op *op;
857         int i, err;
858
859         /* we need a real device to send frames */
860         if (!ifindex)
861                 return -ENODEV;
862
863         /* we need at least one can_frame */
864         if (msg_head->nframes < 1)
865                 return -EINVAL;
866
867         /* check the given can_id */
868         op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
869
870         if (op) {
871                 /* update existing BCM operation */
872
873                 /*
874                  * Do we need more space for the can_frames than currently
875                  * allocated? -> This is a _really_ unusual use-case and
876                  * therefore (complexity / locking) it is not supported.
877                  */
878                 if (msg_head->nframes > op->nframes)
879                         return -E2BIG;
880
881                 /* update can_frames content */
882                 for (i = 0; i < msg_head->nframes; i++) {
883                         err = memcpy_fromiovec((u8 *)&op->frames[i],
884                                                msg->msg_iov, CFSIZ);
885
886                         if (op->frames[i].can_dlc > 8)
887                                 err = -EINVAL;
888
889                         if (err < 0)
890                                 return err;
891
892                         if (msg_head->flags & TX_CP_CAN_ID) {
893                                 /* copy can_id into frame */
894                                 op->frames[i].can_id = msg_head->can_id;
895                         }
896                 }
897
898         } else {
899                 /* insert new BCM operation for the given can_id */
900
901                 op = kzalloc(OPSIZ, GFP_KERNEL);
902                 if (!op)
903                         return -ENOMEM;
904
905                 op->can_id    = msg_head->can_id;
906
907                 /* create array for can_frames and copy the data */
908                 if (msg_head->nframes > 1) {
909                         op->frames = kmalloc(msg_head->nframes * CFSIZ,
910                                              GFP_KERNEL);
911                         if (!op->frames) {
912                                 kfree(op);
913                                 return -ENOMEM;
914                         }
915                 } else
916                         op->frames = &op->sframe;
917
918                 for (i = 0; i < msg_head->nframes; i++) {
919                         err = memcpy_fromiovec((u8 *)&op->frames[i],
920                                                msg->msg_iov, CFSIZ);
921
922                         if (op->frames[i].can_dlc > 8)
923                                 err = -EINVAL;
924
925                         if (err < 0) {
926                                 if (op->frames != &op->sframe)
927                                         kfree(op->frames);
928                                 kfree(op);
929                                 return err;
930                         }
931
932                         if (msg_head->flags & TX_CP_CAN_ID) {
933                                 /* copy can_id into frame */
934                                 op->frames[i].can_id = msg_head->can_id;
935                         }
936                 }
937
938                 /* tx_ops never compare with previous received messages */
939                 op->last_frames = NULL;
940
941                 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
942                 op->sk = sk;
943                 op->ifindex = ifindex;
944
945                 /* initialize uninitialized (kzalloc) structure */
946                 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
947                 op->timer.function = bcm_tx_timeout_handler;
948
949                 /* initialize tasklet for tx countevent notification */
950                 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
951                              (unsigned long) op);
952
953                 /* currently unused in tx_ops */
954                 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
955
956                 /* add this bcm_op to the list of the tx_ops */
957                 list_add(&op->list, &bo->tx_ops);
958
959         } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
960
961         if (op->nframes != msg_head->nframes) {
962                 op->nframes   = msg_head->nframes;
963                 /* start multiple frame transmission with index 0 */
964                 op->currframe = 0;
965         }
966
967         /* check flags */
968
969         op->flags = msg_head->flags;
970
971         if (op->flags & TX_RESET_MULTI_IDX) {
972                 /* start multiple frame transmission with index 0 */
973                 op->currframe = 0;
974         }
975
976         if (op->flags & SETTIMER) {
977                 /* set timer values */
978                 op->count = msg_head->count;
979                 op->ival1 = msg_head->ival1;
980                 op->ival2 = msg_head->ival2;
981                 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
982                 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
983
984                 /* disable an active timer due to zero values? */
985                 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
986                         hrtimer_cancel(&op->timer);
987         }
988
989         if ((op->flags & STARTTIMER) &&
990             ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
991
992                 /* spec: send can_frame when starting timer */
993                 op->flags |= TX_ANNOUNCE;
994
995                 if (op->kt_ival1.tv64 && (op->count > 0)) {
996                         /* op->count-- is done in bcm_tx_timeout_handler */
997                         hrtimer_start(&op->timer, op->kt_ival1,
998                                       HRTIMER_MODE_REL);
999                 } else
1000                         hrtimer_start(&op->timer, op->kt_ival2,
1001                                       HRTIMER_MODE_REL);
1002         }
1003
1004         if (op->flags & TX_ANNOUNCE)
1005                 bcm_can_tx(op);
1006
1007         return msg_head->nframes * CFSIZ + MHSIZ;
1008 }
1009
1010 /*
1011  * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1012  */
1013 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1014                         int ifindex, struct sock *sk)
1015 {
1016         struct bcm_sock *bo = bcm_sk(sk);
1017         struct bcm_op *op;
1018         int do_rx_register;
1019         int err = 0;
1020
1021         if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1022                 /* be robust against wrong usage ... */
1023                 msg_head->flags |= RX_FILTER_ID;
1024                 /* ignore trailing garbage */
1025                 msg_head->nframes = 0;
1026         }
1027
1028         if ((msg_head->flags & RX_RTR_FRAME) &&
1029             ((msg_head->nframes != 1) ||
1030              (!(msg_head->can_id & CAN_RTR_FLAG))))
1031                 return -EINVAL;
1032
1033         /* check the given can_id */
1034         op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1035         if (op) {
1036                 /* update existing BCM operation */
1037
1038                 /*
1039                  * Do we need more space for the can_frames than currently
1040                  * allocated? -> This is a _really_ unusual use-case and
1041                  * therefore (complexity / locking) it is not supported.
1042                  */
1043                 if (msg_head->nframes > op->nframes)
1044                         return -E2BIG;
1045
1046                 if (msg_head->nframes) {
1047                         /* update can_frames content */
1048                         err = memcpy_fromiovec((u8 *)op->frames,
1049                                                msg->msg_iov,
1050                                                msg_head->nframes * CFSIZ);
1051                         if (err < 0)
1052                                 return err;
1053
1054                         /* clear last_frames to indicate 'nothing received' */
1055                         memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1056                 }
1057
1058                 op->nframes = msg_head->nframes;
1059
1060                 /* Only an update -> do not call can_rx_register() */
1061                 do_rx_register = 0;
1062
1063         } else {
1064                 /* insert new BCM operation for the given can_id */
1065                 op = kzalloc(OPSIZ, GFP_KERNEL);
1066                 if (!op)
1067                         return -ENOMEM;
1068
1069                 op->can_id    = msg_head->can_id;
1070                 op->nframes   = msg_head->nframes;
1071
1072                 if (msg_head->nframes > 1) {
1073                         /* create array for can_frames and copy the data */
1074                         op->frames = kmalloc(msg_head->nframes * CFSIZ,
1075                                              GFP_KERNEL);
1076                         if (!op->frames) {
1077                                 kfree(op);
1078                                 return -ENOMEM;
1079                         }
1080
1081                         /* create and init array for received can_frames */
1082                         op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1083                                                   GFP_KERNEL);
1084                         if (!op->last_frames) {
1085                                 kfree(op->frames);
1086                                 kfree(op);
1087                                 return -ENOMEM;
1088                         }
1089
1090                 } else {
1091                         op->frames = &op->sframe;
1092                         op->last_frames = &op->last_sframe;
1093                 }
1094
1095                 if (msg_head->nframes) {
1096                         err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1097                                                msg_head->nframes * CFSIZ);
1098                         if (err < 0) {
1099                                 if (op->frames != &op->sframe)
1100                                         kfree(op->frames);
1101                                 if (op->last_frames != &op->last_sframe)
1102                                         kfree(op->last_frames);
1103                                 kfree(op);
1104                                 return err;
1105                         }
1106                 }
1107
1108                 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1109                 op->sk = sk;
1110                 op->ifindex = ifindex;
1111
1112                 /* initialize uninitialized (kzalloc) structure */
1113                 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1114                 op->timer.function = bcm_rx_timeout_handler;
1115
1116                 /* initialize tasklet for rx timeout notification */
1117                 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1118                              (unsigned long) op);
1119
1120                 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1121                 op->thrtimer.function = bcm_rx_thr_handler;
1122
1123                 /* initialize tasklet for rx throttle handling */
1124                 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1125                              (unsigned long) op);
1126
1127                 /* add this bcm_op to the list of the rx_ops */
1128                 list_add(&op->list, &bo->rx_ops);
1129
1130                 /* call can_rx_register() */
1131                 do_rx_register = 1;
1132
1133         } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1134
1135         /* check flags */
1136         op->flags = msg_head->flags;
1137
1138         if (op->flags & RX_RTR_FRAME) {
1139
1140                 /* no timers in RTR-mode */
1141                 hrtimer_cancel(&op->thrtimer);
1142                 hrtimer_cancel(&op->timer);
1143
1144                 /*
1145                  * funny feature in RX(!)_SETUP only for RTR-mode:
1146                  * copy can_id into frame BUT without RTR-flag to
1147                  * prevent a full-load-loopback-test ... ;-]
1148                  */
1149                 if ((op->flags & TX_CP_CAN_ID) ||
1150                     (op->frames[0].can_id == op->can_id))
1151                         op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1152
1153         } else {
1154                 if (op->flags & SETTIMER) {
1155
1156                         /* set timer value */
1157                         op->ival1 = msg_head->ival1;
1158                         op->ival2 = msg_head->ival2;
1159                         op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1160                         op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1161
1162                         /* disable an active timer due to zero value? */
1163                         if (!op->kt_ival1.tv64)
1164                                 hrtimer_cancel(&op->timer);
1165
1166                         /*
1167                          * In any case cancel the throttle timer, flush
1168                          * potentially blocked msgs and reset throttle handling
1169                          */
1170                         op->kt_lastmsg = ktime_set(0, 0);
1171                         hrtimer_cancel(&op->thrtimer);
1172                         bcm_rx_thr_flush(op, 1);
1173                 }
1174
1175                 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1176                         hrtimer_start(&op->timer, op->kt_ival1,
1177                                       HRTIMER_MODE_REL);
1178         }
1179
1180         /* now we can register for can_ids, if we added a new bcm_op */
1181         if (do_rx_register) {
1182                 if (ifindex) {
1183                         struct net_device *dev;
1184
1185                         dev = dev_get_by_index(&init_net, ifindex);
1186                         if (dev) {
1187                                 err = can_rx_register(dev, op->can_id,
1188                                                       REGMASK(op->can_id),
1189                                                       bcm_rx_handler, op,
1190                                                       "bcm");
1191
1192                                 op->rx_reg_dev = dev;
1193                                 dev_put(dev);
1194                         }
1195
1196                 } else
1197                         err = can_rx_register(NULL, op->can_id,
1198                                               REGMASK(op->can_id),
1199                                               bcm_rx_handler, op, "bcm");
1200                 if (err) {
1201                         /* this bcm rx op is broken -> remove it */
1202                         list_del(&op->list);
1203                         bcm_remove_op(op);
1204                         return err;
1205                 }
1206         }
1207
1208         return msg_head->nframes * CFSIZ + MHSIZ;
1209 }
1210
1211 /*
1212  * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1213  */
1214 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1215 {
1216         struct sk_buff *skb;
1217         struct net_device *dev;
1218         int err;
1219
1220         /* we need a real device to send frames */
1221         if (!ifindex)
1222                 return -ENODEV;
1223
1224         skb = alloc_skb(CFSIZ, GFP_KERNEL);
1225
1226         if (!skb)
1227                 return -ENOMEM;
1228
1229         err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1230         if (err < 0) {
1231                 kfree_skb(skb);
1232                 return err;
1233         }
1234
1235         dev = dev_get_by_index(&init_net, ifindex);
1236         if (!dev) {
1237                 kfree_skb(skb);
1238                 return -ENODEV;
1239         }
1240
1241         skb->dev = dev;
1242         skb->sk  = sk;
1243         err = can_send(skb, 1); /* send with loopback */
1244         dev_put(dev);
1245
1246         if (err)
1247                 return err;
1248
1249         return CFSIZ + MHSIZ;
1250 }
1251
1252 /*
1253  * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1254  */
1255 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1256                        struct msghdr *msg, size_t size)
1257 {
1258         struct sock *sk = sock->sk;
1259         struct bcm_sock *bo = bcm_sk(sk);
1260         int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1261         struct bcm_msg_head msg_head;
1262         int ret; /* read bytes or error codes as return value */
1263
1264         if (!bo->bound)
1265                 return -ENOTCONN;
1266
1267         /* check for valid message length from userspace */
1268         if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1269                 return -EINVAL;
1270
1271         /* check for alternative ifindex for this bcm_op */
1272
1273         if (!ifindex && msg->msg_name) {
1274                 /* no bound device as default => check msg_name */
1275                 struct sockaddr_can *addr =
1276                         (struct sockaddr_can *)msg->msg_name;
1277
1278                 if (addr->can_family != AF_CAN)
1279                         return -EINVAL;
1280
1281                 /* ifindex from sendto() */
1282                 ifindex = addr->can_ifindex;
1283
1284                 if (ifindex) {
1285                         struct net_device *dev;
1286
1287                         dev = dev_get_by_index(&init_net, ifindex);
1288                         if (!dev)
1289                                 return -ENODEV;
1290
1291                         if (dev->type != ARPHRD_CAN) {
1292                                 dev_put(dev);
1293                                 return -ENODEV;
1294                         }
1295
1296                         dev_put(dev);
1297                 }
1298         }
1299
1300         /* read message head information */
1301
1302         ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1303         if (ret < 0)
1304                 return ret;
1305
1306         lock_sock(sk);
1307
1308         switch (msg_head.opcode) {
1309
1310         case TX_SETUP:
1311                 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1312                 break;
1313
1314         case RX_SETUP:
1315                 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1316                 break;
1317
1318         case TX_DELETE:
1319                 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1320                         ret = MHSIZ;
1321                 else
1322                         ret = -EINVAL;
1323                 break;
1324
1325         case RX_DELETE:
1326                 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1327                         ret = MHSIZ;
1328                 else
1329                         ret = -EINVAL;
1330                 break;
1331
1332         case TX_READ:
1333                 /* reuse msg_head for the reply to TX_READ */
1334                 msg_head.opcode  = TX_STATUS;
1335                 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1336                 break;
1337
1338         case RX_READ:
1339                 /* reuse msg_head for the reply to RX_READ */
1340                 msg_head.opcode  = RX_STATUS;
1341                 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1342                 break;
1343
1344         case TX_SEND:
1345                 /* we need exactly one can_frame behind the msg head */
1346                 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1347                         ret = -EINVAL;
1348                 else
1349                         ret = bcm_tx_send(msg, ifindex, sk);
1350                 break;
1351
1352         default:
1353                 ret = -EINVAL;
1354                 break;
1355         }
1356
1357         release_sock(sk);
1358
1359         return ret;
1360 }
1361
1362 /*
1363  * notification handler for netdevice status changes
1364  */
1365 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1366                         void *data)
1367 {
1368         struct net_device *dev = (struct net_device *)data;
1369         struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1370         struct sock *sk = &bo->sk;
1371         struct bcm_op *op;
1372         int notify_enodev = 0;
1373
1374 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1375         if (!net_eq(dev_net(dev), &init_net))
1376                 return NOTIFY_DONE;
1377 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1378         if (dev->nd_net != &init_net)
1379                 return NOTIFY_DONE;
1380 #endif
1381
1382         if (dev->type != ARPHRD_CAN)
1383                 return NOTIFY_DONE;
1384
1385         switch (msg) {
1386
1387         case NETDEV_UNREGISTER:
1388                 lock_sock(sk);
1389
1390                 /* remove device specific receive entries */
1391                 list_for_each_entry(op, &bo->rx_ops, list)
1392                         if (op->rx_reg_dev == dev)
1393                                 bcm_rx_unreg(dev, op);
1394
1395                 /* remove device reference, if this is our bound device */
1396                 if (bo->bound && bo->ifindex == dev->ifindex) {
1397                         bo->bound   = 0;
1398                         bo->ifindex = 0;
1399                         notify_enodev = 1;
1400                 }
1401
1402                 release_sock(sk);
1403
1404                 if (notify_enodev) {
1405                         sk->sk_err = ENODEV;
1406                         if (!sock_flag(sk, SOCK_DEAD))
1407                                 sk->sk_error_report(sk);
1408                 }
1409                 break;
1410
1411         case NETDEV_DOWN:
1412                 if (bo->bound && bo->ifindex == dev->ifindex) {
1413                         sk->sk_err = ENETDOWN;
1414                         if (!sock_flag(sk, SOCK_DEAD))
1415                                 sk->sk_error_report(sk);
1416                 }
1417         }
1418
1419         return NOTIFY_DONE;
1420 }
1421
1422 /*
1423  * initial settings for all BCM sockets to be set at socket creation time
1424  */
1425 static int bcm_init(struct sock *sk)
1426 {
1427         struct bcm_sock *bo = bcm_sk(sk);
1428
1429         bo->bound            = 0;
1430         bo->ifindex          = 0;
1431         bo->dropped_usr_msgs = 0;
1432         bo->bcm_proc_read    = NULL;
1433
1434         INIT_LIST_HEAD(&bo->tx_ops);
1435         INIT_LIST_HEAD(&bo->rx_ops);
1436
1437         /* set notifier */
1438         bo->notifier.notifier_call = bcm_notifier;
1439
1440         register_netdevice_notifier(&bo->notifier);
1441
1442         return 0;
1443 }
1444
1445 /*
1446  * standard socket functions
1447  */
1448 static int bcm_release(struct socket *sock)
1449 {
1450         struct sock *sk = sock->sk;
1451         struct bcm_sock *bo = bcm_sk(sk);
1452         struct bcm_op *op, *next;
1453
1454         /* remove bcm_ops, timer, rx_unregister(), etc. */
1455
1456         unregister_netdevice_notifier(&bo->notifier);
1457
1458         lock_sock(sk);
1459
1460         list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1461                 bcm_remove_op(op);
1462
1463         list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1464                 /*
1465                  * Don't care if we're bound or not (due to netdev problems)
1466                  * can_rx_unregister() is always a save thing to do here.
1467                  */
1468                 if (op->ifindex) {
1469                         /*
1470                          * Only remove subscriptions that had not
1471                          * been removed due to NETDEV_UNREGISTER
1472                          * in bcm_notifier()
1473                          */
1474                         if (op->rx_reg_dev) {
1475                                 struct net_device *dev;
1476
1477                                 dev = dev_get_by_index(&init_net, op->ifindex);
1478                                 if (dev) {
1479                                         bcm_rx_unreg(dev, op);
1480                                         dev_put(dev);
1481                                 }
1482                         }
1483                 } else
1484                         can_rx_unregister(NULL, op->can_id,
1485                                           REGMASK(op->can_id),
1486                                           bcm_rx_handler, op);
1487
1488                 bcm_remove_op(op);
1489         }
1490
1491         /* remove procfs entry */
1492         if (proc_dir && bo->bcm_proc_read)
1493                 remove_proc_entry(bo->procname, proc_dir);
1494
1495         /* remove device reference */
1496         if (bo->bound) {
1497                 bo->bound   = 0;
1498                 bo->ifindex = 0;
1499         }
1500
1501         release_sock(sk);
1502         sock_put(sk);
1503
1504         return 0;
1505 }
1506
1507 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1508                        int flags)
1509 {
1510         struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1511         struct sock *sk = sock->sk;
1512         struct bcm_sock *bo = bcm_sk(sk);
1513
1514         if (bo->bound)
1515                 return -EISCONN;
1516
1517         /* bind a device to this socket */
1518         if (addr->can_ifindex) {
1519                 struct net_device *dev;
1520
1521                 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1522                 if (!dev)
1523                         return -ENODEV;
1524
1525                 if (dev->type != ARPHRD_CAN) {
1526                         dev_put(dev);
1527                         return -ENODEV;
1528                 }
1529
1530                 bo->ifindex = dev->ifindex;
1531                 dev_put(dev);
1532
1533         } else {
1534                 /* no interface reference for ifindex = 0 ('any' CAN device) */
1535                 bo->ifindex = 0;
1536         }
1537
1538         bo->bound = 1;
1539
1540         if (proc_dir) {
1541                 /* unique socket address as filename */
1542                 sprintf(bo->procname, "%p", sock);
1543                 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1544                                                            proc_dir,
1545                                                            bcm_read_proc, sk);
1546         }
1547
1548         return 0;
1549 }
1550
1551 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1552                        struct msghdr *msg, size_t size, int flags)
1553 {
1554         struct sock *sk = sock->sk;
1555         struct sk_buff *skb;
1556         int error = 0;
1557         int noblock;
1558         int err;
1559
1560         noblock =  flags & MSG_DONTWAIT;
1561         flags   &= ~MSG_DONTWAIT;
1562         skb = skb_recv_datagram(sk, flags, noblock, &error);
1563         if (!skb)
1564                 return error;
1565
1566         if (skb->len < size)
1567                 size = skb->len;
1568
1569         err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1570         if (err < 0) {
1571                 skb_free_datagram(sk, skb);
1572                 return err;
1573         }
1574
1575         sock_recv_timestamp(msg, sk, skb);
1576
1577         if (msg->msg_name) {
1578                 msg->msg_namelen = sizeof(struct sockaddr_can);
1579                 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1580         }
1581
1582         skb_free_datagram(sk, skb);
1583
1584         return size;
1585 }
1586
1587 static struct proto_ops bcm_ops __read_mostly = {
1588         .family        = PF_CAN,
1589         .release       = bcm_release,
1590         .bind          = sock_no_bind,
1591         .connect       = bcm_connect,
1592         .socketpair    = sock_no_socketpair,
1593         .accept        = sock_no_accept,
1594         .getname       = sock_no_getname,
1595         .poll          = datagram_poll,
1596         .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
1597         .listen        = sock_no_listen,
1598         .shutdown      = sock_no_shutdown,
1599         .setsockopt    = sock_no_setsockopt,
1600         .getsockopt    = sock_no_getsockopt,
1601         .sendmsg       = bcm_sendmsg,
1602         .recvmsg       = bcm_recvmsg,
1603         .mmap          = sock_no_mmap,
1604         .sendpage      = sock_no_sendpage,
1605 };
1606
1607 static struct proto bcm_proto __read_mostly = {
1608         .name       = "CAN_BCM",
1609         .owner      = THIS_MODULE,
1610         .obj_size   = sizeof(struct bcm_sock),
1611         .init       = bcm_init,
1612 };
1613
1614 static struct can_proto bcm_can_proto __read_mostly = {
1615         .type       = SOCK_DGRAM,
1616         .protocol   = CAN_BCM,
1617         .capability = -1,
1618         .ops        = &bcm_ops,
1619         .prot       = &bcm_proto,
1620 };
1621
1622 static int __init bcm_module_init(void)
1623 {
1624         int err;
1625
1626         printk(banner);
1627
1628         err = can_proto_register(&bcm_can_proto);
1629         if (err < 0) {
1630                 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1631                 return err;
1632         }
1633
1634         /* create /proc/net/can-bcm directory */
1635 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1636         proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1637 #else
1638         proc_dir = proc_mkdir("can-bcm", proc_net);
1639 #endif
1640
1641         if (proc_dir)
1642                 proc_dir->owner = THIS_MODULE;
1643
1644         return 0;
1645 }
1646
1647 static void __exit bcm_module_exit(void)
1648 {
1649         can_proto_unregister(&bcm_can_proto);
1650
1651         if (proc_dir)
1652 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1653                 proc_net_remove(&init_net, "can-bcm");
1654 #else
1655                 proc_net_remove("can-bcm");
1656 #endif
1657 }
1658
1659 module_init(bcm_module_init);
1660 module_exit(bcm_module_exit);