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