]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/bcm-prior-2-6-22.c
can bcm: fix tx_setup off-by-one errors
[socketcan-devel.git] / kernel / 2.6 / net / can / bcm-prior-2-6-22.c
1 /*
2  * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <socketcan-users@lists.berlios.de>
41  *
42  */
43
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/uio.h>
50 #include <linux/net.h>
51 #include <linux/netdevice.h>
52 #include <linux/socket.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
55 #include <socketcan/can.h>
56 #include <socketcan/can/core.h>
57 #include <socketcan/can/bcm.h>
58 #include <net/sock.h>
59 #include "compat.h"
60
61 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
62 RCSID("$Id$");
63
64 /* use of last_frames[index].can_dlc */
65 #define RX_RECV    0x40 /* received data for this element */
66 #define RX_THR     0x80 /* element not been sent due to throttle feature */
67 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
68
69 /* get best masking value for can_rx_register() for a given single can_id */
70 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
71                      (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
72                      (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
73
74 #define CAN_BCM_VERSION CAN_VERSION
75 static __initdata const char banner[] = KERN_INFO
76         "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
77
78 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
79 MODULE_LICENSE("Dual BSD/GPL");
80 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
81 MODULE_ALIAS("can-proto-2");
82
83 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
84 #error This code only supports Kernel versions _below_ 2.6.22
85 #error For 2.6.22+ Kernels please use bcm.c instead of bcm-prior-2-6-22.c
86 #endif
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 timer_list timer, thrtimer;
102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
103         struct skb_timeval rx_stamp;
104 #else
105         struct timeval rx_stamp;
106 #endif
107         unsigned long j_ival1, j_ival2, j_lastmsg;
108         int rx_ifindex;
109         int count;
110         int nframes;
111         int currframe;
112         struct can_frame *frames;
113         struct can_frame *last_frames;
114         struct can_frame sframe;
115         struct can_frame last_sframe;
116         struct sock *sk;
117         struct net_device *rx_reg_dev;
118 };
119
120 static struct proc_dir_entry *proc_dir;
121
122 struct bcm_sock {
123 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
124         struct sock sk;
125 #else
126         struct sock *sk;
127 #endif
128         int bound;
129         int ifindex;
130         struct notifier_block notifier;
131         struct list_head rx_ops;
132         struct list_head tx_ops;
133         unsigned long dropped_usr_msgs;
134         struct proc_dir_entry *bcm_proc_read;
135         char procname [9]; /* pointer printed in ASCII with \0 */
136 };
137
138 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
139 {
140 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
141         return (struct bcm_sock *)sk;
142 #else
143         return (struct bcm_sock *)sk->sk_protinfo;
144 #endif
145 }
146
147 #define CFSIZ sizeof(struct can_frame)
148 #define OPSIZ sizeof(struct bcm_op)
149 #define MHSIZ sizeof(struct bcm_msg_head)
150
151 /*
152  * rounded_tv2jif - calculate jiffies from timeval including optional up
153  * @tv: pointer to timeval
154  *
155  * Description:
156  * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
157  * function is intentionally more relaxed on precise timer ticks to get
158  * exact one jiffy for requested 1000us on a 1000HZ machine.
159  * This code is to be removed when upgrading to kernel hrtimer.
160  *
161  * Return:
162  *  calculated jiffies (max: ULONG_MAX)
163  */
164 static unsigned long rounded_tv2jif(const struct timeval *tv)
165 {
166         unsigned long sec  = tv->tv_sec;
167         unsigned long usec = tv->tv_usec;
168         unsigned long jif;
169
170         if (sec > ULONG_MAX / HZ)
171                 return ULONG_MAX;
172
173         /* round up to get at least the requested time */
174         usec += 1000000 / HZ - 1;
175
176         jif  = usec / (1000000 / HZ);
177
178         if (sec * HZ > ULONG_MAX - jif)
179                 return ULONG_MAX;
180
181         return jif + sec * HZ;
182 }
183
184 /*
185  * procfs functions
186  */
187 static char *bcm_proc_getifname(int ifindex)
188 {
189         struct net_device *dev;
190
191         if (!ifindex)
192                 return "any";
193
194         /* no usage counting */
195         dev = __dev_get_by_index(&init_net, ifindex);
196         if (dev)
197                 return dev->name;
198
199         return "???";
200 }
201
202 static int bcm_read_proc(char *page, char **start, off_t off,
203                          int count, int *eof, void *data)
204 {
205         int len = 0;
206         struct sock *sk = (struct sock *)data;
207         struct bcm_sock *bo = bcm_sk(sk);
208         struct bcm_op *op;
209
210         len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
211                         sk->sk_socket);
212         len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
213         len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
214         len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
215                         bo->dropped_usr_msgs);
216         len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
217                         bcm_proc_getifname(bo->ifindex));
218         len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
219
220         list_for_each_entry(op, &bo->rx_ops, list) {
221
222                 unsigned long reduction;
223
224                 /* print only active entries & prevent division by zero */
225                 if (!op->frames_abs)
226                         continue;
227
228                 len += snprintf(page + len, PAGE_SIZE - len,
229                                 "rx_op: %03X %-5s ",
230                                 op->can_id, bcm_proc_getifname(op->ifindex));
231                 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
232                                 op->nframes,
233                                 (op->flags & RX_CHECK_DLC)?'d':' ');
234                 if (op->j_ival1)
235                         len += snprintf(page + len, PAGE_SIZE - len,
236                                         "timeo=%ld ", op->j_ival1);
237
238                 if (op->j_ival2)
239                         len += snprintf(page + len, PAGE_SIZE - len,
240                                         "thr=%ld ", op->j_ival2);
241
242                 len += snprintf(page + len, PAGE_SIZE - len,
243                                 "# recv %ld (%ld) => reduction: ",
244                                 op->frames_filtered, op->frames_abs);
245
246                 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
247
248                 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
249                                 (reduction == 100)?"near ":"", reduction);
250
251                 if (len > PAGE_SIZE - 200) {
252                         /* mark output cut off */
253                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
254                         break;
255                 }
256         }
257
258         list_for_each_entry(op, &bo->tx_ops, list) {
259
260                 len += snprintf(page + len, PAGE_SIZE - len,
261                                 "tx_op: %03X %s [%d] ",
262                                 op->can_id, bcm_proc_getifname(op->ifindex),
263                                 op->nframes);
264
265                 if (op->j_ival1)
266                         len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
267                                         op->j_ival1);
268
269                 if (op->j_ival2)
270                         len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
271                                         op->j_ival2);
272
273                 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
274                                 op->frames_abs);
275
276                 if (len > PAGE_SIZE - 100) {
277                         /* mark output cut off */
278                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
279                         break;
280                 }
281         }
282
283         len += snprintf(page + len, PAGE_SIZE - len, "\n");
284
285         *eof = 1;
286         return len;
287 }
288
289 /*
290  * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
291  *              of the given bcm tx op
292  */
293 static void bcm_can_tx(struct bcm_op *op)
294 {
295         struct sk_buff *skb;
296         struct net_device *dev;
297         struct can_frame *cf = &op->frames[op->currframe];
298
299         /* no target device? => exit */
300         if (!op->ifindex)
301                 return;
302
303         dev = dev_get_by_index(&init_net, op->ifindex);
304         if (!dev) {
305                 /* RFC: should this bcm_op remove itself here? */
306                 return;
307         }
308
309         skb = alloc_skb(CFSIZ, gfp_any());
310         if (!skb)
311                 goto out;
312
313         memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
314
315         /* send with loopback */
316         skb->dev = dev;
317         skb->sk = op->sk;
318         can_send(skb, 1);
319
320         /* update statistics */
321         op->currframe++;
322         op->frames_abs++;
323
324         /* reached last frame? */
325         if (op->currframe >= op->nframes)
326                 op->currframe = 0;
327  out:
328         dev_put(dev);
329 }
330
331 /*
332  * bcm_send_to_user - send a BCM message to the userspace
333  *                    (consisting of bcm_msg_head + x CAN frames)
334  */
335 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
336                              struct can_frame *frames, int has_timestamp)
337 {
338         struct sk_buff *skb;
339         struct can_frame *firstframe;
340         struct sockaddr_can *addr;
341         struct sock *sk = op->sk;
342         int datalen = head->nframes * CFSIZ;
343         int err;
344
345         skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
346         if (!skb)
347                 return;
348
349         memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
350
351         if (head->nframes) {
352                 /* can_frames starting here */
353                 firstframe = (struct can_frame *)skb->tail;
354
355                 memcpy(skb_put(skb, datalen), frames, datalen);
356
357                 /*
358                  * the BCM uses the can_dlc-element of the can_frame
359                  * structure for internal purposes. This is only
360                  * relevant for updates that are generated by the
361                  * BCM, where nframes is 1
362                  */
363                 if (head->nframes == 1)
364                         firstframe->can_dlc &= BCM_CAN_DLC_MASK;
365         }
366
367         if (has_timestamp) {
368                 /* restore rx timestamp */
369                 skb->tstamp = op->rx_stamp;
370         }
371
372         /*
373          *  Put the datagram to the queue so that bcm_recvmsg() can
374          *  get it from there.  We need to pass the interface index to
375          *  bcm_recvmsg().  We pass a whole struct sockaddr_can in skb->cb
376          *  containing the interface index.
377          */
378
379         BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
380         addr = (struct sockaddr_can *)skb->cb;
381         memset(addr, 0, sizeof(*addr));
382         addr->can_family  = AF_CAN;
383         addr->can_ifindex = op->rx_ifindex;
384
385         err = sock_queue_rcv_skb(sk, skb);
386         if (err < 0) {
387                 struct bcm_sock *bo = bcm_sk(sk);
388
389                 kfree_skb(skb);
390                 /* don't care about overflows in this statistic */
391                 bo->dropped_usr_msgs++;
392         }
393 }
394
395 /*
396  * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
397  */
398 static void bcm_tx_timeout_handler(unsigned long data)
399 {
400         struct bcm_op *op = (struct bcm_op *)data;
401         struct bcm_msg_head msg_head;
402
403         if (op->j_ival1 && (op->count > 0)) {
404
405                 op->count--;
406                 if (!op->count && (op->flags & TX_COUNTEVT)) {
407
408                         /* create notification to user */
409                         msg_head.opcode  = TX_EXPIRED;
410                         msg_head.flags   = op->flags;
411                         msg_head.count   = op->count;
412                         msg_head.ival1   = op->ival1;
413                         msg_head.ival2   = op->ival2;
414                         msg_head.can_id  = op->can_id;
415                         msg_head.nframes = 0;
416
417                         bcm_send_to_user(op, &msg_head, NULL, 0);
418                 }
419
420                 /* send (next) frame */
421                 bcm_can_tx(op);
422                 mod_timer(&op->timer, jiffies + op->j_ival1);
423
424         } else {
425                 if (op->j_ival2) {
426
427                         /* send (next) frame */
428                         bcm_can_tx(op);
429                         mod_timer(&op->timer, jiffies + op->j_ival2);
430                 }
431         }
432 }
433
434 /*
435  * bcm_rx_changed - create a RX_CHANGED notification due to changed content
436  */
437 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
438 {
439         struct bcm_msg_head head;
440
441         op->j_lastmsg = jiffies;
442
443         /* update statistics */
444         op->frames_filtered++;
445
446         /* prevent statistics overflow */
447         if (op->frames_filtered > ULONG_MAX/100)
448                 op->frames_filtered = op->frames_abs = 0;
449
450         /* this element is not throttled anymore */
451         data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
452
453         head.opcode  = RX_CHANGED;
454         head.flags   = op->flags;
455         head.count   = op->count;
456         head.ival1   = op->ival1;
457         head.ival2   = op->ival2;
458         head.can_id  = op->can_id;
459         head.nframes = 1;
460
461         bcm_send_to_user(op, &head, data, 1);
462 }
463
464 /*
465  * bcm_rx_update_and_send - process a detected relevant receive content change
466  *                          1. update the last received data
467  *                          2. send a notification to the user (if possible)
468  */
469 static void bcm_rx_update_and_send(struct bcm_op *op,
470                                    struct can_frame *lastdata,
471                                    const struct can_frame *rxdata)
472 {
473         unsigned long nexttx = op->j_lastmsg + op->j_ival2;
474
475         memcpy(lastdata, rxdata, CFSIZ);
476
477         /* mark as used and throttled by default */
478         lastdata->can_dlc |= (RX_RECV|RX_THR);
479
480         /* throttle bcm_rx_changed ? */
481         if ((op->thrtimer.expires) ||
482             ((op->j_ival2) && (nexttx > jiffies))) {
483                 /* we are already waiting OR we have to start waiting */
484
485                 if (!(op->thrtimer.expires)) {
486                         /* start the timer only the first time */
487                         mod_timer(&op->thrtimer, nexttx);
488                 }
489
490         } else {
491                 /* send RX_CHANGED to the user immediately */
492                 bcm_rx_changed(op, lastdata);
493         }
494 }
495
496 /*
497  * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
498  *                       received data stored in op->last_frames[]
499  */
500 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
501                                 const struct can_frame *rxdata)
502 {
503         /*
504          * no one uses the MSBs of can_dlc for comparation,
505          * so we use it here to detect the first time of reception
506          */
507
508         if (!(op->last_frames[index].can_dlc & RX_RECV)) {
509                 /* received data for the first time => send update to user */
510                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
511                 return;
512         }
513
514         /* do a real check in can_frame data section */
515
516         if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
517             (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
518                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
519                 return;
520         }
521
522         if (op->flags & RX_CHECK_DLC) {
523                 /* do a real check in can_frame dlc */
524                 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
525                                         BCM_CAN_DLC_MASK)) {
526                         bcm_rx_update_and_send(op, &op->last_frames[index],
527                                                rxdata);
528                         return;
529                 }
530         }
531 }
532
533 /*
534  * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
535  */
536 static void bcm_rx_starttimer(struct bcm_op *op)
537 {
538         if (op->flags & RX_NO_AUTOTIMER)
539                 return;
540
541         if (op->j_ival1)
542                 mod_timer(&op->timer, jiffies + op->j_ival1);
543 }
544
545 /*
546  * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
547  */
548 static void bcm_rx_timeout_handler(unsigned long data)
549 {
550         struct bcm_op *op = (struct bcm_op *)data;
551         struct bcm_msg_head msg_head;
552
553         /* create notification to user */
554         msg_head.opcode  = RX_TIMEOUT;
555         msg_head.flags   = op->flags;
556         msg_head.count   = op->count;
557         msg_head.ival1   = op->ival1;
558         msg_head.ival2   = op->ival2;
559         msg_head.can_id  = op->can_id;
560         msg_head.nframes = 0;
561
562         bcm_send_to_user(op, &msg_head, NULL, 0);
563
564         /* no restart of the timer is done here! */
565
566         /* if user wants to be informed, when cyclic CAN-Messages come back */
567         if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
568                 /* clear received can_frames to indicate 'nothing received' */
569                 memset(op->last_frames, 0, op->nframes * CFSIZ);
570         }
571 }
572
573 /*
574  * bcm_rx_do_flush - helper for bcm_rx_thr_flush
575  */
576 static inline int bcm_rx_do_flush(struct bcm_op *op, int index)
577 {
578         if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
579                 bcm_rx_changed(op, &op->last_frames[index]);
580                 return 1;
581         }
582         return 0;
583 }
584
585 /*
586  * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
587  */
588 static int bcm_rx_thr_flush(struct bcm_op *op)
589 {
590         int updated = 0;
591
592         if (op->nframes > 1) {
593                 int i;
594
595                 /* for MUX filter we start at index 1 */
596                 for (i = 1; i < op->nframes; i++)
597                         updated += bcm_rx_do_flush(op, i);
598
599         } else {
600                 /* for RX_FILTER_ID and simple filter */
601                 updated += bcm_rx_do_flush(op, 0);
602         }
603
604         return updated;
605 }
606
607 /*
608  * bcm_rx_thr_handler - the time for blocked content updates is over now:
609  *                      Check for throttled data and send it to the userspace
610  */
611 static void bcm_rx_thr_handler(unsigned long data)
612 {
613         struct bcm_op *op = (struct bcm_op *)data;
614
615         if (bcm_rx_thr_flush(op))
616                 mod_timer(&op->thrtimer, jiffies + op->j_ival2);
617         else {
618                 /* mark disabled / consumed timer */
619                 op->thrtimer.expires = 0;
620         }
621 }
622
623 /*
624  * bcm_rx_handler - handle a CAN frame receiption
625  */
626 static void bcm_rx_handler(struct sk_buff *skb, void *data)
627 {
628         struct bcm_op *op = (struct bcm_op *)data;
629         const struct can_frame *rxframe = (struct can_frame *)skb->data;
630         int i;
631
632         /* disable timeout */
633         del_timer(&op->timer);
634
635         if (op->can_id != rxframe->can_id)
636                 return;
637
638         /* save rx timestamp */
639         op->rx_stamp = skb->tstamp;
640         /* save originator for recvfrom() */
641         op->rx_ifindex = skb->dev->ifindex;
642         /* update statistics */
643         op->frames_abs++;
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                 goto rx_starttimer;
655         }
656
657         if (op->nframes == 1) {
658                 /* simple compare with index 0 */
659                 bcm_rx_cmp_to_index(op, 0, rxframe);
660                 goto rx_starttimer;
661         }
662
663         if (op->nframes > 1) {
664                 /*
665                  * multiplex compare
666                  *
667                  * find the first multiplex mask that fits.
668                  * Remark: The MUX-mask is stored in index 0
669                  */
670
671                 for (i = 1; i < op->nframes; i++) {
672                         if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
673                             (GET_U64(&op->frames[0]) &
674                              GET_U64(&op->frames[i]))) {
675                                 bcm_rx_cmp_to_index(op, i, rxframe);
676                                 break;
677                         }
678                 }
679         }
680
681 rx_starttimer:
682         bcm_rx_starttimer(op);
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         del_timer(&op->timer);
704         del_timer(&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                 setup_timer(&op->timer, bcm_tx_timeout_handler,
914                             (unsigned long)op);
915
916                 /* currently unused in tx_ops */
917                 init_timer(&op->thrtimer);
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->j_ival1 = rounded_tv2jif(&msg_head->ival1);
945                 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
946
947                 /* disable an active timer due to zero values? */
948                 if (!op->j_ival1 && !op->j_ival2)
949                         del_timer(&op->timer);
950         }
951
952         if ((op->flags & STARTTIMER) &&
953             ((op->j_ival1 && op->count) || op->j_ival2)) {
954
955                 /* spec: send can_frame when starting timer */
956                 op->flags |= TX_ANNOUNCE;
957
958                 /* only start timer when having more frames than sent below */
959                 if (op->j_ival1 && (op->count > 1)) {
960                         /* op->count-- is done in bcm_tx_timeout_handler */
961                         mod_timer(&op->timer, jiffies + op->j_ival1);
962                 } else
963                         mod_timer(&op->timer, jiffies + op->j_ival2);
964         }
965
966         if (op->flags & TX_ANNOUNCE) {
967                 bcm_can_tx(op);
968                 if (op->j_ival1 && (op->count > 0))
969                         op->count--;
970         }
971
972         return msg_head->nframes * CFSIZ + MHSIZ;
973 }
974
975 /*
976  * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
977  */
978 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
979                         int ifindex, struct sock *sk)
980 {
981         struct bcm_sock *bo = bcm_sk(sk);
982         struct bcm_op *op;
983         int do_rx_register;
984         int err = 0;
985
986         if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
987                 /* be robust against wrong usage ... */
988                 msg_head->flags |= RX_FILTER_ID;
989                 /* ignore trailing garbage */
990                 msg_head->nframes = 0;
991         }
992
993         if ((msg_head->flags & RX_RTR_FRAME) &&
994             ((msg_head->nframes != 1) ||
995              (!(msg_head->can_id & CAN_RTR_FLAG))))
996                 return -EINVAL;
997
998         /* check the given can_id */
999         op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1000         if (op) {
1001                 /* update existing BCM operation */
1002
1003                 /*
1004                  * Do we need more space for the can_frames than currently
1005                  * allocated? -> This is a _really_ unusual use-case and
1006                  * therefore (complexity / locking) it is not supported.
1007                  */
1008                 if (msg_head->nframes > op->nframes)
1009                         return -E2BIG;
1010
1011                 if (msg_head->nframes) {
1012                         /* update can_frames content */
1013                         err = memcpy_fromiovec((u8 *)op->frames,
1014                                                msg->msg_iov,
1015                                                msg_head->nframes * CFSIZ);
1016                         if (err < 0)
1017                                 return err;
1018
1019                         /* clear last_frames to indicate 'nothing received' */
1020                         memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1021                 }
1022
1023                 op->nframes = msg_head->nframes;
1024
1025                 /* Only an update -> do not call can_rx_register() */
1026                 do_rx_register = 0;
1027
1028         } else {
1029                 /* insert new BCM operation for the given can_id */
1030                 op = kzalloc(OPSIZ, GFP_KERNEL);
1031                 if (!op)
1032                         return -ENOMEM;
1033
1034                 op->can_id    = msg_head->can_id;
1035                 op->nframes   = msg_head->nframes;
1036
1037                 if (msg_head->nframes > 1) {
1038                         /* create array for can_frames and copy the data */
1039                         op->frames = kmalloc(msg_head->nframes * CFSIZ,
1040                                              GFP_KERNEL);
1041                         if (!op->frames) {
1042                                 kfree(op);
1043                                 return -ENOMEM;
1044                         }
1045
1046                         /* create and init array for received can_frames */
1047                         op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1048                                                   GFP_KERNEL);
1049                         if (!op->last_frames) {
1050                                 kfree(op->frames);
1051                                 kfree(op);
1052                                 return -ENOMEM;
1053                         }
1054
1055                 } else {
1056                         op->frames = &op->sframe;
1057                         op->last_frames = &op->last_sframe;
1058                 }
1059
1060                 if (msg_head->nframes) {
1061                         err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1062                                                msg_head->nframes * CFSIZ);
1063                         if (err < 0) {
1064                                 if (op->frames != &op->sframe)
1065                                         kfree(op->frames);
1066                                 if (op->last_frames != &op->last_sframe)
1067                                         kfree(op->last_frames);
1068                                 kfree(op);
1069                                 return err;
1070                         }
1071                 }
1072
1073                 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1074                 op->sk = sk;
1075                 op->ifindex = ifindex;
1076
1077                 /* initialize uninitialized (kzalloc) structure */
1078                 setup_timer(&op->timer, bcm_rx_timeout_handler,
1079                             (unsigned long)op);
1080
1081                 /* init throttle timer for RX_CHANGED */
1082                 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1083                             (unsigned long)op);
1084
1085                 /* mark disabled timer */
1086                 op->thrtimer.expires = 0;
1087
1088                 /* add this bcm_op to the list of the rx_ops */
1089                 list_add(&op->list, &bo->rx_ops);
1090
1091                 /* call can_rx_register() */
1092                 do_rx_register = 1;
1093
1094         } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1095
1096         /* check flags */
1097         op->flags = msg_head->flags;
1098
1099         if (op->flags & RX_RTR_FRAME) {
1100
1101                 /* no timers in RTR-mode */
1102                 del_timer(&op->thrtimer);
1103                 del_timer(&op->timer);
1104
1105                 /*
1106                  * funny feature in RX(!)_SETUP only for RTR-mode:
1107                  * copy can_id into frame BUT without RTR-flag to
1108                  * prevent a full-load-loopback-test ... ;-]
1109                  */
1110                 if ((op->flags & TX_CP_CAN_ID) ||
1111                     (op->frames[0].can_id == op->can_id))
1112                         op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1113
1114         } else {
1115                 if (op->flags & SETTIMER) {
1116
1117                         /* set timer value */
1118                         op->ival1 = msg_head->ival1;
1119                         op->ival2 = msg_head->ival2;
1120                         op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1121                         op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1122
1123                         /* disable an active timer due to zero value? */
1124                         if (!op->j_ival1)
1125                                 del_timer(&op->timer);
1126
1127                         /*
1128                          * In any case cancel the throttle timer, flush
1129                          * potentially blocked msgs and reset throttle handling
1130                          */
1131                         del_timer(&op->thrtimer);
1132                         bcm_rx_thr_flush(op);
1133                         op->thrtimer.expires = 0;
1134                 }
1135
1136                 if ((op->flags & STARTTIMER) && op->j_ival1)
1137                         mod_timer(&op->timer, jiffies + op->j_ival1);
1138         }
1139
1140         /* now we can register for can_ids, if we added a new bcm_op */
1141         if (do_rx_register) {
1142                 if (ifindex) {
1143                         struct net_device *dev;
1144
1145                         dev = dev_get_by_index(&init_net, ifindex);
1146                         if (dev) {
1147                                 err = can_rx_register(dev, op->can_id,
1148                                                       REGMASK(op->can_id),
1149                                                       bcm_rx_handler, op,
1150                                                       "bcm");
1151
1152                                 op->rx_reg_dev = dev;
1153                                 dev_put(dev);
1154                         }
1155
1156                 } else
1157                         err = can_rx_register(NULL, op->can_id,
1158                                               REGMASK(op->can_id),
1159                                               bcm_rx_handler, op, "bcm");
1160                 if (err) {
1161                         /* this bcm rx op is broken -> remove it */
1162                         list_del(&op->list);
1163                         bcm_remove_op(op);
1164                         return err;
1165                 }
1166         }
1167
1168         return msg_head->nframes * CFSIZ + MHSIZ;
1169 }
1170
1171 /*
1172  * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1173  */
1174 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1175 {
1176         struct sk_buff *skb;
1177         struct net_device *dev;
1178         int err;
1179
1180         /* we need a real device to send frames */
1181         if (!ifindex)
1182                 return -ENODEV;
1183
1184         skb = alloc_skb(CFSIZ, GFP_KERNEL);
1185
1186         if (!skb)
1187                 return -ENOMEM;
1188
1189         err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1190         if (err < 0) {
1191                 kfree_skb(skb);
1192                 return err;
1193         }
1194
1195         dev = dev_get_by_index(&init_net, ifindex);
1196         if (!dev) {
1197                 kfree_skb(skb);
1198                 return -ENODEV;
1199         }
1200
1201         skb->dev = dev;
1202         skb->sk  = sk;
1203         err = can_send(skb, 1); /* send with loopback */
1204         dev_put(dev);
1205
1206         if (err)
1207                 return err;
1208
1209         return CFSIZ + MHSIZ;
1210 }
1211
1212 /*
1213  * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1214  */
1215 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1216                        struct msghdr *msg, size_t size)
1217 {
1218         struct sock *sk = sock->sk;
1219         struct bcm_sock *bo = bcm_sk(sk);
1220         int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1221         struct bcm_msg_head msg_head;
1222         int ret; /* read bytes or error codes as return value */
1223
1224         if (!bo->bound)
1225                 return -ENOTCONN;
1226
1227         /* check for valid message length from userspace */
1228         if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1229                 return -EINVAL;
1230
1231         /* check for alternative ifindex for this bcm_op */
1232
1233         if (!ifindex && msg->msg_name) {
1234                 /* no bound device as default => check msg_name */
1235                 struct sockaddr_can *addr =
1236                         (struct sockaddr_can *)msg->msg_name;
1237
1238                 if (addr->can_family != AF_CAN)
1239                         return -EINVAL;
1240
1241                 /* ifindex from sendto() */
1242                 ifindex = addr->can_ifindex;
1243
1244                 if (ifindex) {
1245                         struct net_device *dev;
1246
1247                         dev = dev_get_by_index(&init_net, ifindex);
1248                         if (!dev)
1249                                 return -ENODEV;
1250
1251                         if (dev->type != ARPHRD_CAN) {
1252                                 dev_put(dev);
1253                                 return -ENODEV;
1254                         }
1255
1256                         dev_put(dev);
1257                 }
1258         }
1259
1260         /* read message head information */
1261
1262         ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1263         if (ret < 0)
1264                 return ret;
1265
1266         lock_sock(sk);
1267
1268         switch (msg_head.opcode) {
1269
1270         case TX_SETUP:
1271                 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1272                 break;
1273
1274         case RX_SETUP:
1275                 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1276                 break;
1277
1278         case TX_DELETE:
1279                 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1280                         ret = MHSIZ;
1281                 else
1282                         ret = -EINVAL;
1283                 break;
1284
1285         case RX_DELETE:
1286                 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1287                         ret = MHSIZ;
1288                 else
1289                         ret = -EINVAL;
1290                 break;
1291
1292         case TX_READ:
1293                 /* reuse msg_head for the reply to TX_READ */
1294                 msg_head.opcode  = TX_STATUS;
1295                 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1296                 break;
1297
1298         case RX_READ:
1299                 /* reuse msg_head for the reply to RX_READ */
1300                 msg_head.opcode  = RX_STATUS;
1301                 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1302                 break;
1303
1304         case TX_SEND:
1305                 /* we need exactly one can_frame behind the msg head */
1306                 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1307                         ret = -EINVAL;
1308                 else
1309                         ret = bcm_tx_send(msg, ifindex, sk);
1310                 break;
1311
1312         default:
1313                 ret = -EINVAL;
1314                 break;
1315         }
1316
1317         release_sock(sk);
1318
1319         return ret;
1320 }
1321
1322 /*
1323  * notification handler for netdevice status changes
1324  */
1325 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1326                         void *data)
1327 {
1328         struct net_device *dev = (struct net_device *)data;
1329         struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1330 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1331         struct sock *sk = &bo->sk;
1332 #else
1333         struct sock *sk = bo->sk;
1334 #endif
1335         struct bcm_op *op;
1336         int notify_enodev = 0;
1337
1338         if (dev->type != ARPHRD_CAN)
1339                 return NOTIFY_DONE;
1340
1341         switch (msg) {
1342
1343         case NETDEV_UNREGISTER:
1344                 lock_sock(sk);
1345
1346                 /* remove device specific receive entries */
1347                 list_for_each_entry(op, &bo->rx_ops, list)
1348                         if (op->rx_reg_dev == dev)
1349                                 bcm_rx_unreg(dev, op);
1350
1351                 /* remove device reference, if this is our bound device */
1352                 if (bo->bound && bo->ifindex == dev->ifindex) {
1353                         bo->bound   = 0;
1354                         bo->ifindex = 0;
1355                         notify_enodev = 1;
1356                 }
1357
1358                 release_sock(sk);
1359
1360                 if (notify_enodev) {
1361                         sk->sk_err = ENODEV;
1362                         if (!sock_flag(sk, SOCK_DEAD))
1363                                 sk->sk_error_report(sk);
1364                 }
1365                 break;
1366
1367         case NETDEV_DOWN:
1368                 if (bo->bound && bo->ifindex == dev->ifindex) {
1369                         sk->sk_err = ENETDOWN;
1370                         if (!sock_flag(sk, SOCK_DEAD))
1371                                 sk->sk_error_report(sk);
1372                 }
1373         }
1374
1375         return NOTIFY_DONE;
1376 }
1377
1378 /*
1379  * initial settings for all BCM sockets to be set at socket creation time
1380  */
1381 static int bcm_init(struct sock *sk)
1382 {
1383         struct bcm_sock *bo = bcm_sk(sk);
1384
1385 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1386         bo->sk               = sk;
1387 #endif
1388         bo->bound            = 0;
1389         bo->ifindex          = 0;
1390         bo->dropped_usr_msgs = 0;
1391         bo->bcm_proc_read    = NULL;
1392
1393         INIT_LIST_HEAD(&bo->tx_ops);
1394         INIT_LIST_HEAD(&bo->rx_ops);
1395
1396         /* set notifier */
1397         bo->notifier.notifier_call = bcm_notifier;
1398
1399         register_netdevice_notifier(&bo->notifier);
1400
1401         return 0;
1402 }
1403
1404 /*
1405  * standard socket functions
1406  */
1407 static int bcm_release(struct socket *sock)
1408 {
1409         struct sock *sk = sock->sk;
1410         struct bcm_sock *bo = bcm_sk(sk);
1411         struct bcm_op *op, *next;
1412
1413         /* remove bcm_ops, timer, rx_unregister(), etc. */
1414
1415         unregister_netdevice_notifier(&bo->notifier);
1416
1417         lock_sock(sk);
1418
1419         list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1420                 bcm_remove_op(op);
1421
1422         list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1423                 /*
1424                  * Don't care if we're bound or not (due to netdev problems)
1425                  * can_rx_unregister() is always a save thing to do here.
1426                  */
1427                 if (op->ifindex) {
1428                         /*
1429                          * Only remove subscriptions that had not
1430                          * been removed due to NETDEV_UNREGISTER
1431                          * in bcm_notifier()
1432                          */
1433                         if (op->rx_reg_dev) {
1434                                 struct net_device *dev;
1435
1436                                 dev = dev_get_by_index(&init_net, op->ifindex);
1437                                 if (dev) {
1438                                         bcm_rx_unreg(dev, op);
1439                                         dev_put(dev);
1440                                 }
1441                         }
1442                 } else
1443                         can_rx_unregister(NULL, op->can_id,
1444                                           REGMASK(op->can_id),
1445                                           bcm_rx_handler, op);
1446
1447                 bcm_remove_op(op);
1448         }
1449
1450         /* remove procfs entry */
1451         if (proc_dir && bo->bcm_proc_read)
1452                 remove_proc_entry(bo->procname, proc_dir);
1453
1454         /* remove device reference */
1455         if (bo->bound) {
1456                 bo->bound   = 0;
1457                 bo->ifindex = 0;
1458         }
1459
1460         sock_orphan(sk);
1461         sock->sk = NULL;
1462
1463         release_sock(sk);
1464         sock_put(sk);
1465
1466         return 0;
1467 }
1468
1469 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1470                        int flags)
1471 {
1472         struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1473         struct sock *sk = sock->sk;
1474         struct bcm_sock *bo = bcm_sk(sk);
1475
1476         if (bo->bound)
1477                 return -EISCONN;
1478
1479         /* bind a device to this socket */
1480         if (addr->can_ifindex) {
1481                 struct net_device *dev;
1482
1483                 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1484                 if (!dev)
1485                         return -ENODEV;
1486
1487                 if (dev->type != ARPHRD_CAN) {
1488                         dev_put(dev);
1489                         return -ENODEV;
1490                 }
1491
1492                 bo->ifindex = dev->ifindex;
1493                 dev_put(dev);
1494
1495         } else {
1496                 /* no interface reference for ifindex = 0 ('any' CAN device) */
1497                 bo->ifindex = 0;
1498         }
1499
1500         bo->bound = 1;
1501
1502         if (proc_dir) {
1503                 /* unique socket address as filename */
1504                 sprintf(bo->procname, "%p", sock);
1505                 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1506                                                            proc_dir,
1507                                                            bcm_read_proc, sk);
1508         }
1509
1510         return 0;
1511 }
1512
1513 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1514                        struct msghdr *msg, size_t size, int flags)
1515 {
1516         struct sock *sk = sock->sk;
1517         struct sk_buff *skb;
1518         int error = 0;
1519         int noblock;
1520         int err;
1521
1522         noblock =  flags & MSG_DONTWAIT;
1523         flags   &= ~MSG_DONTWAIT;
1524         skb = skb_recv_datagram(sk, flags, noblock, &error);
1525         if (!skb)
1526                 return error;
1527
1528         if (skb->len < size)
1529                 size = skb->len;
1530
1531         err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1532         if (err < 0) {
1533                 skb_free_datagram(sk, skb);
1534                 return err;
1535         }
1536
1537         sock_recv_timestamp(msg, sk, skb);
1538
1539         if (msg->msg_name) {
1540                 msg->msg_namelen = sizeof(struct sockaddr_can);
1541                 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1542         }
1543
1544         skb_free_datagram(sk, skb);
1545
1546         return size;
1547 }
1548
1549 static const struct proto_ops bcm_ops = {
1550         .family        = PF_CAN,
1551         .release       = bcm_release,
1552         .bind          = sock_no_bind,
1553         .connect       = bcm_connect,
1554         .socketpair    = sock_no_socketpair,
1555         .accept        = sock_no_accept,
1556         .getname       = sock_no_getname,
1557         .poll          = datagram_poll,
1558         .ioctl         = can_ioctl,     /* use can_ioctl() from af_can.c */
1559         .listen        = sock_no_listen,
1560         .shutdown      = sock_no_shutdown,
1561         .setsockopt    = sock_no_setsockopt,
1562         .getsockopt    = sock_no_getsockopt,
1563         .sendmsg       = bcm_sendmsg,
1564         .recvmsg       = bcm_recvmsg,
1565         .mmap          = sock_no_mmap,
1566         .sendpage      = sock_no_sendpage,
1567 };
1568
1569 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1570 static struct proto bcm_proto __read_mostly = {
1571         .name       = "CAN_BCM",
1572         .owner      = THIS_MODULE,
1573         .obj_size   = sizeof(struct bcm_sock),
1574         .init       = bcm_init,
1575 };
1576
1577 static const struct can_proto bcm_can_proto = {
1578         .type       = SOCK_DGRAM,
1579         .protocol   = CAN_BCM,
1580         .capability = -1,
1581         .ops        = &bcm_ops,
1582         .prot       = &bcm_proto,
1583 };
1584 #else
1585 static const struct can_proto bcm_can_proto = {
1586         .type       = SOCK_DGRAM,
1587         .protocol   = CAN_BCM,
1588         .capability = -1,
1589         .ops        = &bcm_ops,
1590         .owner      = THIS_MODULE,
1591         .obj_size   = sizeof(struct bcm_sock),
1592         .init       = bcm_init,
1593 };
1594 #endif
1595
1596 static int __init bcm_module_init(void)
1597 {
1598         int err;
1599
1600         printk(banner);
1601
1602         err = can_proto_register(&bcm_can_proto);
1603         if (err < 0) {
1604                 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1605                 return err;
1606         }
1607
1608         /* create /proc/net/can-bcm directory */
1609         proc_dir = proc_mkdir("can-bcm", proc_net);
1610
1611         if (proc_dir)
1612                 proc_dir->owner = THIS_MODULE;
1613
1614         return 0;
1615 }
1616
1617 static void __exit bcm_module_exit(void)
1618 {
1619         can_proto_unregister(&bcm_can_proto);
1620
1621         if (proc_dir)
1622                 proc_net_remove("can-bcm");
1623 }
1624
1625 module_init(bcm_module_init);
1626 module_exit(bcm_module_exit);