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