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