]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - net/can/gw.c
Include CAN gateway from Socketcan SVN rev. 1220
[lisovros/linux_canprio.git] / net / can / gw.c
1 /*
2  * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
3  *
4  * Copyright (c) 2002-2010 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/types.h>
48 #include <linux/list.h>
49 #include <linux/spinlock.h>
50 #include <linux/rcupdate.h>
51 #include <linux/rculist.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <socketcan/can.h>
57 #include <socketcan/can/core.h>
58 #include <socketcan/can/gw.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
61
62 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
63 RCSID("$Id$");
64
65 #define CAN_GW_VERSION "20100412"
66 static __initdata const char banner[] =
67         KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
68
69 MODULE_DESCRIPTION("PF_CAN netlink gateway");
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72 MODULE_ALIAS("can-gw");
73
74 HLIST_HEAD(cgw_list);
75 static DEFINE_SPINLOCK(cgw_list_lock);
76 static struct notifier_block notifier;
77
78 static struct kmem_cache *cgw_cache __read_mostly;
79
80 #define CGW_SK_MAGIC ((void *)(&notifier))
81
82 /* structure that contains the (on-the-fly) CAN frame modifications */
83 struct cf_mod {
84         struct {
85                 struct can_frame and;
86                 struct can_frame or;
87                 struct can_frame xor;
88                 struct can_frame set;
89         } modframe;
90         struct {
91                 u8 and;
92                 u8 or;
93                 u8 xor;
94                 u8 set;
95         } modtype;
96         void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
97                                           struct cf_mod *mod);
98
99         /* CAN frame checksum calculation after CAN frame modifications */
100         struct {
101                 struct cgw_csum_xor xor;
102                 struct cgw_csum_crc8 crc8;
103         } csum;
104         struct {
105                 void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
106                 void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
107         } csumfunc;
108 };
109
110
111 /*
112  * So far we just support CAN -> CAN routing and frame modifications.
113  *
114  * The internal can_can_gw structure contains data and attributes for
115  * a CAN -> CAN gateway job.
116  */
117 struct can_can_gw {
118         struct can_filter filter;
119         int src_idx;
120         int dst_idx;
121 };
122
123 /* list entry for CAN gateways jobs */
124 struct cgw_job {
125         struct hlist_node list;
126         struct rcu_head rcu;
127         u32 handled_frames;
128         u32 dropped_frames;
129         struct cf_mod mod;
130         union {
131                 /* CAN frame data source */
132                 struct net_device *dev;
133         } src;
134         union {
135                 /* CAN frame data destination */
136                 struct net_device *dev;
137         } dst;
138         union {
139                 struct can_can_gw ccgw;
140                 /* tbc */
141         };
142         u8 gwtype;
143         u16 flags;
144 };
145
146 /* modification functions that are invoked in the hot path in can_can_gw_rcv */
147
148 #define MODFUNC(func, op) static void func (struct can_frame *cf, \
149                                             struct cf_mod *mod) { op ; }
150
151 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
152 MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
153 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
154 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
155 MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
156 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
157 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
158 MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
159 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
160 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
161 MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
162 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
163
164 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
165 {
166         /*
167          * Copy the struct members separately to ensure that no uninitialized
168          * data are copied in the 3 bytes hole of the struct. This is needed
169          * to make easy compares of the data in the struct cf_mod.
170          */
171
172         dst->can_id = src->can_id;
173         dst->can_dlc = src->can_dlc;
174         *(u64 *)dst->data = *(u64 *)src->data;
175 }
176
177 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
178 {
179         /* 
180          * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
181          * relative to received dlc -1 .. -8 :
182          * e.g. for received dlc = 8 
183          * -1 => index = 7 (data[7])
184          * -3 => index = 5 (data[5])
185          * -8 => index = 0 (data[0])
186          */
187
188         if (fr > -9 && fr < 8 &&
189             to > -9 && to < 8 &&
190             re > -9 && re < 8)
191                 return 0;
192         else
193                 return -EINVAL;
194
195
196 static inline int calc_idx(int idx, int rx_dlc)
197 {
198         if (idx < 0)
199                 return rx_dlc + idx;
200         else
201                 return idx;
202 }
203
204 static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
205 {
206         int from = calc_idx(xor->from_idx, cf->can_dlc);
207         int to = calc_idx(xor->to_idx, cf->can_dlc);
208         int res = calc_idx(xor->result_idx, cf->can_dlc);
209         u8 val = xor->init_xor_val;
210         int i;
211
212         if (from < 0 || to < 0 || res < 0)
213                 return;
214
215         if (from <= to) {
216                 for (i = from; i <= to; i++)
217                         val ^= cf->data[i]; 
218         } else {
219                 for (i = from; i >= to; i--)
220                         val ^= cf->data[i]; 
221         }
222
223         cf->data[res] = val;
224 }
225
226 static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
227 {
228         u8 val = xor->init_xor_val;
229         int i;
230
231         for (i = xor->from_idx; i <= xor->to_idx; i++)
232                 val ^= cf->data[i];
233
234         cf->data[xor->result_idx] = val;
235 }
236
237 static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
238 {
239         u8 val = xor->init_xor_val;
240         int i;
241
242         for (i = xor->from_idx; i >= xor->to_idx; i--)
243                 val ^= cf->data[i];
244
245         cf->data[xor->result_idx] = val;
246 }
247
248 static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
249 {
250         int from = calc_idx(crc8->from_idx, cf->can_dlc);
251         int to = calc_idx(crc8->to_idx, cf->can_dlc);
252         int res = calc_idx(crc8->result_idx, cf->can_dlc);
253         u8 crc = crc8->init_crc_val;
254         int i;
255
256         if (from < 0 || to < 0 || res < 0)
257                 return;
258
259         if (from <= to) {
260                 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
261                         crc = crc8->crctab[crc^cf->data[i]];
262         } else {
263                 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
264                         crc = crc8->crctab[crc^cf->data[i]];
265         }
266
267         switch (crc8->profile) {
268
269         case CGW_CRC8PRF_1U8:
270                 crc = crc8->crctab[crc^crc8->profile_data[0]];
271                 break;
272
273         case  CGW_CRC8PRF_16U8:
274                 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
275                 break;
276
277         case CGW_CRC8PRF_SFFID_XOR:
278                 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
279                                    (cf->can_id >> 8 & 0xFF)];
280                 break;
281
282         }
283
284         cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
285 }
286
287 static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
288 {
289         u8 crc = crc8->init_crc_val;
290         int i;
291
292         for (i = crc8->from_idx; i <= crc8->to_idx; i++)
293                 crc = crc8->crctab[crc^cf->data[i]];
294
295         switch (crc8->profile) {
296
297         case CGW_CRC8PRF_1U8:
298                 crc = crc8->crctab[crc^crc8->profile_data[0]];
299                 break;
300
301         case  CGW_CRC8PRF_16U8:
302                 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
303                 break;
304
305         case CGW_CRC8PRF_SFFID_XOR:
306                 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
307                                    (cf->can_id >> 8 & 0xFF)];
308                 break;
309         }
310
311         cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
312 }
313
314 static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
315 {
316         u8 crc = crc8->init_crc_val;
317         int i;
318
319         for (i = crc8->from_idx; i >= crc8->to_idx; i--)
320                 crc = crc8->crctab[crc^cf->data[i]];
321
322         switch (crc8->profile) {
323
324         case CGW_CRC8PRF_1U8:
325                 crc = crc8->crctab[crc^crc8->profile_data[0]];
326                 break;
327
328         case  CGW_CRC8PRF_16U8:
329                 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
330                 break;
331
332         case CGW_CRC8PRF_SFFID_XOR:
333                 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
334                                    (cf->can_id >> 8 & 0xFF)];
335                 break;
336         }
337
338         cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
339 }
340
341 /* the receive & process & send function */
342 static void can_can_gw_rcv(struct sk_buff *skb, void *data)
343 {
344         struct cgw_job *gwj = (struct cgw_job *)data;
345         struct can_frame *cf;
346         struct sk_buff *nskb;
347         int modidx = 0;
348
349         /* do not handle already routed frames */
350         if (skb->sk == CGW_SK_MAGIC)
351                 return;
352
353         if (!(gwj->dst.dev->flags & IFF_UP)) {
354                 gwj->dropped_frames++;
355                 return;
356         }
357
358         /*
359          * clone the given skb, which has not been done in can_rcv()
360          *
361          * When there is at least one modification function activated,
362          * we need to copy the skb as we want to modify skb->data.
363          */
364         if (gwj->mod.modfunc[0])
365                 nskb = skb_copy(skb, GFP_ATOMIC);
366         else
367                 nskb = skb_clone(skb, GFP_ATOMIC);
368
369         if (!nskb) {
370                 gwj->dropped_frames++;
371                 return;
372         }
373
374         /* mark routed frames with a 'special' sk value */
375         nskb->sk = CGW_SK_MAGIC;
376         nskb->dev = gwj->dst.dev;
377
378         /* pointer to modifiable CAN frame */
379         cf = (struct can_frame *)nskb->data;
380
381         /* perform preprocessed modification functions if there are any */
382         while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
383                 (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
384
385         /* check for checksum updates when the CAN frame has been modified */
386         if (modidx) {
387                 if (gwj->mod.csumfunc.crc8)
388                         (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
389
390                 if (gwj->mod.csumfunc.xor)
391                         (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
392         }
393
394         /* clear the skb timestamp if not configured the other way */
395         if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
396                 nskb->tstamp.tv64 = 0;
397
398         /* send to netdevice */
399         if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
400                 gwj->dropped_frames++;
401         else
402                 gwj->handled_frames++;
403 }
404
405 static inline int cgw_register_filter(struct cgw_job *gwj)
406 {
407         return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
408                                gwj->ccgw.filter.can_mask, can_can_gw_rcv,
409                                gwj, "gw");
410 }
411
412 static inline void cgw_unregister_filter(struct cgw_job *gwj)
413 {
414         can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
415                           gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
416 }
417
418 static int cgw_notifier(struct notifier_block *nb,
419                         unsigned long msg, void *data)
420 {
421         struct net_device *dev = (struct net_device *)data;
422
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
424         if (!net_eq(dev_net(dev), &init_net))
425                 return NOTIFY_DONE;
426 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
427         if (dev->nd_net != &init_net)
428                 return NOTIFY_DONE;
429 #endif
430         if (dev->type != ARPHRD_CAN)
431                 return NOTIFY_DONE;
432
433         if (msg == NETDEV_UNREGISTER) {
434
435                 struct cgw_job *gwj = NULL;
436                 struct hlist_node *n, *nx;
437
438                 spin_lock(&cgw_list_lock);
439
440                 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
441
442                         if (gwj->src.dev == dev || gwj->dst.dev == dev) { 
443                                 hlist_del(&gwj->list);
444                                 cgw_unregister_filter(gwj);
445                                 kfree(gwj);
446                         }
447                 }
448
449                 spin_unlock(&cgw_list_lock);
450         }
451
452         return NOTIFY_DONE;
453 }
454
455 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
456 {
457         struct cgw_frame_mod mb;
458         struct rtcanmsg *rtcan;
459         struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
460         if (!nlh)
461                 return -EMSGSIZE;
462
463         rtcan = nlmsg_data(nlh);
464         rtcan->can_family = AF_CAN;
465         rtcan->gwtype = gwj->gwtype;
466         rtcan->flags = gwj->flags;
467
468         /* add statistics if available */
469
470         if (gwj->handled_frames) {
471                 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
472                         goto cancel;
473                 else
474                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
475         }
476
477         if (gwj->dropped_frames) {
478                 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
479                         goto cancel;
480                 else
481                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
482         }
483
484         /* check non default settings of attributes */
485
486         if (gwj->mod.modtype.and) {
487                 memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
488                 mb.modtype = gwj->mod.modtype.and;
489                 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
490                         goto cancel;
491                 else
492                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
493         }
494
495         if (gwj->mod.modtype.or) {
496                 memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
497                 mb.modtype = gwj->mod.modtype.or;
498                 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
499                         goto cancel;
500                 else
501                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
502         }
503
504         if (gwj->mod.modtype.xor) {
505                 memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
506                 mb.modtype = gwj->mod.modtype.xor;
507                 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
508                         goto cancel;
509                 else
510                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
511         }
512
513         if (gwj->mod.modtype.set) {
514                 memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
515                 mb.modtype = gwj->mod.modtype.set;
516                 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
517                         goto cancel;
518                 else
519                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
520         }
521
522         if (gwj->mod.csumfunc.crc8) {
523                 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
524                             &gwj->mod.csum.crc8) < 0)
525                         goto cancel;
526                 else
527                         nlh->nlmsg_len += NLA_HDRLEN + \
528                                 NLA_ALIGN(CGW_CS_CRC8_LEN);
529         }
530
531         if (gwj->mod.csumfunc.xor) {
532                 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
533                             &gwj->mod.csum.xor) < 0)
534                         goto cancel;
535                 else
536                         nlh->nlmsg_len += NLA_HDRLEN + \
537                                 NLA_ALIGN(CGW_CS_XOR_LEN);
538         }
539
540         if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
541
542                 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
543                         if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
544                                     &gwj->ccgw.filter) < 0)
545                                 goto cancel;
546                         else
547                                 nlh->nlmsg_len += NLA_HDRLEN +
548                                         NLA_ALIGN(sizeof(struct can_filter));
549                 }
550
551                 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
552                         goto cancel;
553                 else
554                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
555
556                 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
557                         goto cancel;
558                 else
559                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
560         }
561
562         return skb->len;
563
564 cancel:
565         nlmsg_cancel(skb, nlh);
566         return -EMSGSIZE;
567 }
568
569 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
570 static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
571 {
572         struct cgw_job *gwj = NULL;
573         struct hlist_node *n;
574         int idx = 0;
575         int s_idx = cb->args[0];
576
577         rcu_read_lock();
578         hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
579                 if (idx < s_idx)
580                         goto cont;
581
582                 if (cgw_put_job(skb, gwj) < 0)
583                         break;
584 cont:
585                 idx++;
586         }
587         rcu_read_unlock();
588
589         cb->args[0] = idx;
590
591         return skb->len;
592 }
593
594 /* check for common and gwtype specific attributes */
595 static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod, 
596                           u8 gwtype, void *gwtypeattr)
597 {
598         struct nlattr *tb[CGW_MAX+1];
599         struct cgw_frame_mod mb;
600         int modidx = 0;
601         int err = 0;
602
603         /* initialize modification & checksum data space */
604         memset(mod, 0, sizeof(*mod)); 
605
606         err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
607         if (err < 0)
608                 return err;
609
610         /* check for AND/OR/XOR/SET modifications */
611
612         if (tb[CGW_MOD_AND] &&
613             nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
614                 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
615
616                 canframecpy(&mod->modframe.and, &mb.cf);
617                 mod->modtype.and = mb.modtype;
618
619                 if (mb.modtype & CGW_MOD_ID)
620                         mod->modfunc[modidx++] = mod_and_id;
621
622                 if (mb.modtype & CGW_MOD_DLC)
623                         mod->modfunc[modidx++] = mod_and_dlc;
624
625                 if (mb.modtype & CGW_MOD_DATA)
626                         mod->modfunc[modidx++] = mod_and_data;
627         }
628
629         if (tb[CGW_MOD_OR] &&
630             nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
631                 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
632
633                 canframecpy(&mod->modframe.or, &mb.cf);
634                 mod->modtype.or = mb.modtype;
635
636                 if (mb.modtype & CGW_MOD_ID)
637                         mod->modfunc[modidx++] = mod_or_id;
638
639                 if (mb.modtype & CGW_MOD_DLC)
640                         mod->modfunc[modidx++] = mod_or_dlc;
641
642                 if (mb.modtype & CGW_MOD_DATA)
643                         mod->modfunc[modidx++] = mod_or_data;
644         }
645
646         if (tb[CGW_MOD_XOR] &&
647             nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
648                 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
649
650                 canframecpy(&mod->modframe.xor, &mb.cf);
651                 mod->modtype.xor = mb.modtype;
652
653                 if (mb.modtype & CGW_MOD_ID)
654                         mod->modfunc[modidx++] = mod_xor_id;
655
656                 if (mb.modtype & CGW_MOD_DLC)
657                         mod->modfunc[modidx++] = mod_xor_dlc;
658
659                 if (mb.modtype & CGW_MOD_DATA)
660                         mod->modfunc[modidx++] = mod_xor_data;
661         }
662
663         if (tb[CGW_MOD_SET] &&
664             nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
665                 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
666
667                 canframecpy(&mod->modframe.set, &mb.cf);
668                 mod->modtype.set = mb.modtype;
669
670                 if (mb.modtype & CGW_MOD_ID)
671                         mod->modfunc[modidx++] = mod_set_id;
672
673                 if (mb.modtype & CGW_MOD_DLC)
674                         mod->modfunc[modidx++] = mod_set_dlc;
675
676                 if (mb.modtype & CGW_MOD_DATA)
677                         mod->modfunc[modidx++] = mod_set_data;
678         }
679
680         /* check for checksum operations after CAN frame modifications */
681         if (modidx) {
682
683                 if (tb[CGW_CS_CRC8] &&
684                     nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
685
686                         struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
687                                 nla_data(tb[CGW_CS_CRC8]);
688
689                         err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
690                                                  c->result_idx);
691                         if (err)
692                                 return err;
693
694                         nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
695                                    CGW_CS_CRC8_LEN);
696
697                         /*
698                          * select dedicated processing function to reduce
699                          * runtime operations in receive hot path.
700                          */
701                         if (c->from_idx < 0 || c->to_idx < 0 ||
702                             c->result_idx < 0)
703                                 mod->csumfunc.crc8 = cgw_csum_crc8_rel;
704                         else if (c->from_idx <= c->to_idx)
705                                 mod->csumfunc.crc8 = cgw_csum_crc8_pos;
706                         else
707                                 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
708                 }
709
710                 if (tb[CGW_CS_XOR] &&
711                     nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
712
713                         struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
714                                 nla_data(tb[CGW_CS_XOR]);
715
716                         err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
717                                                  c->result_idx);
718                         if (err)
719                                 return err;
720
721                         nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
722                                    CGW_CS_XOR_LEN);
723
724                         /*
725                          * select dedicated processing function to reduce
726                          * runtime operations in receive hot path.
727                          */
728                         if (c->from_idx < 0 || c->to_idx < 0 ||
729                             c->result_idx < 0)
730                                 mod->csumfunc.xor = cgw_csum_xor_rel;
731                         else if (c->from_idx <= c->to_idx)
732                                 mod->csumfunc.xor = cgw_csum_xor_pos;
733                         else
734                                 mod->csumfunc.xor = cgw_csum_xor_neg;
735                 }
736         }
737
738         if (gwtype == CGW_TYPE_CAN_CAN) {
739
740                 /* check CGW_TYPE_CAN_CAN specific attributes */
741
742                 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
743                 memset(ccgw, 0, sizeof(*ccgw)); 
744
745                 /* check for can_filter in attributes */
746                 if (tb[CGW_FILTER] &&
747                     nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
748                         nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
749                                    sizeof(struct can_filter));
750
751                 err = -ENODEV;
752
753                 /* specifying two interfaces is mandatory */
754                 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
755                         return err;
756
757                 if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
758                         nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
759                                    sizeof(u32));
760
761                 if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
762                         nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
763                                    sizeof(u32));
764
765                 /* both indices set to 0 for flushing all routing entries */
766                 if (!ccgw->src_idx && !ccgw->dst_idx)
767                         return 0;
768
769                 /* only one index set to 0 is an error */
770                 if (!ccgw->src_idx || !ccgw->dst_idx)
771                         return err;
772         }
773
774         /* add the checks for other gwtypes here */
775
776         return 0;
777 }
778
779 static int cgw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh,
780                           void *arg)
781 {
782         struct rtcanmsg *r;
783         struct cgw_job *gwj;
784         int err = 0;
785
786         if (nlmsg_len(nlh) < sizeof(*r))
787                 return -EINVAL;
788
789         r = nlmsg_data(nlh);
790         if (r->can_family != AF_CAN)
791                 return -EPFNOSUPPORT;
792
793         /* so far we only support CAN -> CAN routings */
794         if (r->gwtype != CGW_TYPE_CAN_CAN)
795                 return -EINVAL;
796
797         gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
798         if (!gwj)
799                 return -ENOMEM;
800
801         gwj->handled_frames = 0;
802         gwj->dropped_frames = 0;
803         gwj->flags = r->flags;
804         gwj->gwtype = r->gwtype;
805
806         err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
807         if (err < 0)
808                 goto out;
809
810         err = -ENODEV;
811
812         /* ifindex == 0 is not allowed for job creation */
813         if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
814                 goto out;
815
816         gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
817
818         if (!gwj->src.dev)
819                 goto out;
820
821         if (gwj->src.dev->type != ARPHRD_CAN)
822                 goto put_src_out;
823
824         gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
825
826         if (!gwj->dst.dev)
827                 goto put_src_out;
828
829         if (gwj->dst.dev->type != ARPHRD_CAN)
830                 goto put_src_dst_out;
831                 
832         spin_lock(&cgw_list_lock);
833
834         err = cgw_register_filter(gwj);
835         if (!err)
836                 hlist_add_head_rcu(&gwj->list, &cgw_list);
837
838         spin_unlock(&cgw_list_lock);
839         
840 put_src_dst_out:
841         dev_put(gwj->dst.dev);
842 put_src_out:
843         dev_put(gwj->src.dev);
844 out:
845         if (err)
846                 kmem_cache_free(cgw_cache, gwj);
847
848         return err;
849 }
850
851 static void cgw_remove_all_jobs(void)
852 {
853         struct cgw_job *gwj = NULL;
854         struct hlist_node *n, *nx;
855
856         spin_lock(&cgw_list_lock);
857
858         hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
859                 hlist_del(&gwj->list);
860                 cgw_unregister_filter(gwj);
861                 kfree(gwj);
862         }
863
864         spin_unlock(&cgw_list_lock);
865 }
866
867 static int cgw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
868 {
869         struct cgw_job *gwj = NULL;
870         struct hlist_node *n, *nx;
871         struct rtcanmsg *r;
872         struct cf_mod mod;
873         struct can_can_gw ccgw;
874         int err = 0;
875
876         if (nlmsg_len(nlh) < sizeof(*r))
877                 return -EINVAL;
878
879         r = nlmsg_data(nlh);
880         if (r->can_family != AF_CAN)
881                 return -EPFNOSUPPORT;
882
883         /* so far we only support CAN -> CAN routings */
884         if (r->gwtype != CGW_TYPE_CAN_CAN)
885                 return -EINVAL;
886
887         err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
888         if (err < 0)
889                 return err;
890
891         /* two interface indices both set to 0 => remove all entries */
892         if (!ccgw.src_idx && !ccgw.dst_idx) {
893                 cgw_remove_all_jobs();
894                 return 0;
895         }
896
897         err = -EINVAL;
898
899         spin_lock(&cgw_list_lock);
900
901         /* remove only the first matching entry */
902         hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
903
904                 if (gwj->flags != r->flags)
905                         continue;
906
907                 if (memcmp(&gwj->mod, &mod, sizeof(mod)))
908                         continue;
909
910                 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
911                 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
912                         continue;
913
914                 hlist_del(&gwj->list);
915                 cgw_unregister_filter(gwj);
916                 kfree(gwj);
917                 err = 0;
918                 break;
919         }
920
921         spin_unlock(&cgw_list_lock);
922         
923         return err;
924 }
925
926 static __init int cgw_module_init(void)
927 {
928         printk(banner);
929
930         cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
931                                       0, 0, NULL);
932
933         if (!cgw_cache)
934                 return -ENOMEM;
935
936         /* set notifier */
937         notifier.notifier_call = cgw_notifier;
938         register_netdevice_notifier(&notifier);
939
940         if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs)) {
941                 unregister_netdevice_notifier(&notifier);
942                 kmem_cache_destroy(cgw_cache);
943                 return -ENOBUFS;
944         }
945
946         /* Only the first call to __rtnl_register can fail */
947         __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL);
948         __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL);
949
950         return 0;
951 }
952
953 static __exit void cgw_module_exit(void)
954 {
955         rtnl_unregister_all(PF_CAN);
956
957         unregister_netdevice_notifier(&notifier);
958
959         cgw_remove_all_jobs();
960
961         rcu_barrier(); /* Wait for completion of call_rcu()'s */
962
963         kmem_cache_destroy(cgw_cache);
964 }
965
966 module_init(cgw_module_init);
967 module_exit(cgw_module_exit);