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