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