]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/gw.c
skb->sk is used in dev_pick_tx() which is called from dev_queue_xmit(). If
[socketcan-devel.git] / kernel / 2.6 / 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 #include <net/sock.h>
62
63 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
64 RCSID("$Id$");
65
66 #define CAN_GW_VERSION "20101205"
67 static __initdata const char banner[] =
68         KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
69
70 MODULE_DESCRIPTION("PF_CAN netlink gateway");
71 MODULE_LICENSE("Dual BSD/GPL");
72 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73 MODULE_ALIAS("can-gw");
74
75 HLIST_HEAD(cgw_list);
76 static struct notifier_block notifier;
77
78 static struct kmem_cache *cgw_cache __read_mostly;
79
80 static struct sock gw_dummy_sk;
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 == &gw_dummy_sk)
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 = &gw_dummy_sk;
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                 ASSERT_RTNL();
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
450         return NOTIFY_DONE;
451 }
452
453 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
454 {
455         struct cgw_frame_mod mb;
456         struct rtcanmsg *rtcan;
457         struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
458         if (!nlh)
459                 return -EMSGSIZE;
460
461         rtcan = nlmsg_data(nlh);
462         rtcan->can_family = AF_CAN;
463         rtcan->gwtype = gwj->gwtype;
464         rtcan->flags = gwj->flags;
465
466         /* add statistics if available */
467
468         if (gwj->handled_frames) {
469                 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
470                         goto cancel;
471                 else
472                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
473         }
474
475         if (gwj->dropped_frames) {
476                 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
477                         goto cancel;
478                 else
479                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
480         }
481
482         /* check non default settings of attributes */
483
484         if (gwj->mod.modtype.and) {
485                 memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
486                 mb.modtype = gwj->mod.modtype.and;
487                 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
488                         goto cancel;
489                 else
490                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
491         }
492
493         if (gwj->mod.modtype.or) {
494                 memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
495                 mb.modtype = gwj->mod.modtype.or;
496                 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
497                         goto cancel;
498                 else
499                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
500         }
501
502         if (gwj->mod.modtype.xor) {
503                 memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
504                 mb.modtype = gwj->mod.modtype.xor;
505                 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
506                         goto cancel;
507                 else
508                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
509         }
510
511         if (gwj->mod.modtype.set) {
512                 memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
513                 mb.modtype = gwj->mod.modtype.set;
514                 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
515                         goto cancel;
516                 else
517                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
518         }
519
520         if (gwj->mod.csumfunc.crc8) {
521                 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
522                             &gwj->mod.csum.crc8) < 0)
523                         goto cancel;
524                 else
525                         nlh->nlmsg_len += NLA_HDRLEN + \
526                                 NLA_ALIGN(CGW_CS_CRC8_LEN);
527         }
528
529         if (gwj->mod.csumfunc.xor) {
530                 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
531                             &gwj->mod.csum.xor) < 0)
532                         goto cancel;
533                 else
534                         nlh->nlmsg_len += NLA_HDRLEN + \
535                                 NLA_ALIGN(CGW_CS_XOR_LEN);
536         }
537
538         if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
539
540                 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
541                         if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
542                                     &gwj->ccgw.filter) < 0)
543                                 goto cancel;
544                         else
545                                 nlh->nlmsg_len += NLA_HDRLEN +
546                                         NLA_ALIGN(sizeof(struct can_filter));
547                 }
548
549                 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
550                         goto cancel;
551                 else
552                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
553
554                 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
555                         goto cancel;
556                 else
557                         nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
558         }
559
560         return skb->len;
561
562 cancel:
563         nlmsg_cancel(skb, nlh);
564         return -EMSGSIZE;
565 }
566
567 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
568 static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
569 {
570         struct cgw_job *gwj = NULL;
571         struct hlist_node *n;
572         int idx = 0;
573         int s_idx = cb->args[0];
574
575         rcu_read_lock();
576         hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
577                 if (idx < s_idx)
578                         goto cont;
579
580                 if (cgw_put_job(skb, gwj) < 0)
581                         break;
582 cont:
583                 idx++;
584         }
585         rcu_read_unlock();
586
587         cb->args[0] = idx;
588
589         return skb->len;
590 }
591
592 /* check for common and gwtype specific attributes */
593 static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod, 
594                           u8 gwtype, void *gwtypeattr)
595 {
596         struct nlattr *tb[CGW_MAX+1];
597         struct cgw_frame_mod mb;
598         int modidx = 0;
599         int err = 0;
600
601         /* initialize modification & checksum data space */
602         memset(mod, 0, sizeof(*mod)); 
603
604         err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
605         if (err < 0)
606                 return err;
607
608         /* check for AND/OR/XOR/SET modifications */
609
610         if (tb[CGW_MOD_AND] &&
611             nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
612                 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
613
614                 canframecpy(&mod->modframe.and, &mb.cf);
615                 mod->modtype.and = mb.modtype;
616
617                 if (mb.modtype & CGW_MOD_ID)
618                         mod->modfunc[modidx++] = mod_and_id;
619
620                 if (mb.modtype & CGW_MOD_DLC)
621                         mod->modfunc[modidx++] = mod_and_dlc;
622
623                 if (mb.modtype & CGW_MOD_DATA)
624                         mod->modfunc[modidx++] = mod_and_data;
625         }
626
627         if (tb[CGW_MOD_OR] &&
628             nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
629                 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
630
631                 canframecpy(&mod->modframe.or, &mb.cf);
632                 mod->modtype.or = mb.modtype;
633
634                 if (mb.modtype & CGW_MOD_ID)
635                         mod->modfunc[modidx++] = mod_or_id;
636
637                 if (mb.modtype & CGW_MOD_DLC)
638                         mod->modfunc[modidx++] = mod_or_dlc;
639
640                 if (mb.modtype & CGW_MOD_DATA)
641                         mod->modfunc[modidx++] = mod_or_data;
642         }
643
644         if (tb[CGW_MOD_XOR] &&
645             nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
646                 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
647
648                 canframecpy(&mod->modframe.xor, &mb.cf);
649                 mod->modtype.xor = mb.modtype;
650
651                 if (mb.modtype & CGW_MOD_ID)
652                         mod->modfunc[modidx++] = mod_xor_id;
653
654                 if (mb.modtype & CGW_MOD_DLC)
655                         mod->modfunc[modidx++] = mod_xor_dlc;
656
657                 if (mb.modtype & CGW_MOD_DATA)
658                         mod->modfunc[modidx++] = mod_xor_data;
659         }
660
661         if (tb[CGW_MOD_SET] &&
662             nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
663                 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
664
665                 canframecpy(&mod->modframe.set, &mb.cf);
666                 mod->modtype.set = mb.modtype;
667
668                 if (mb.modtype & CGW_MOD_ID)
669                         mod->modfunc[modidx++] = mod_set_id;
670
671                 if (mb.modtype & CGW_MOD_DLC)
672                         mod->modfunc[modidx++] = mod_set_dlc;
673
674                 if (mb.modtype & CGW_MOD_DATA)
675                         mod->modfunc[modidx++] = mod_set_data;
676         }
677
678         /* check for checksum operations after CAN frame modifications */
679         if (modidx) {
680
681                 if (tb[CGW_CS_CRC8] &&
682                     nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
683
684                         struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
685                                 nla_data(tb[CGW_CS_CRC8]);
686
687                         err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
688                                                  c->result_idx);
689                         if (err)
690                                 return err;
691
692                         nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
693                                    CGW_CS_CRC8_LEN);
694
695                         /*
696                          * select dedicated processing function to reduce
697                          * runtime operations in receive hot path.
698                          */
699                         if (c->from_idx < 0 || c->to_idx < 0 ||
700                             c->result_idx < 0)
701                                 mod->csumfunc.crc8 = cgw_csum_crc8_rel;
702                         else if (c->from_idx <= c->to_idx)
703                                 mod->csumfunc.crc8 = cgw_csum_crc8_pos;
704                         else
705                                 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
706                 }
707
708                 if (tb[CGW_CS_XOR] &&
709                     nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
710
711                         struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
712                                 nla_data(tb[CGW_CS_XOR]);
713
714                         err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
715                                                  c->result_idx);
716                         if (err)
717                                 return err;
718
719                         nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
720                                    CGW_CS_XOR_LEN);
721
722                         /*
723                          * select dedicated processing function to reduce
724                          * runtime operations in receive hot path.
725                          */
726                         if (c->from_idx < 0 || c->to_idx < 0 ||
727                             c->result_idx < 0)
728                                 mod->csumfunc.xor = cgw_csum_xor_rel;
729                         else if (c->from_idx <= c->to_idx)
730                                 mod->csumfunc.xor = cgw_csum_xor_pos;
731                         else
732                                 mod->csumfunc.xor = cgw_csum_xor_neg;
733                 }
734         }
735
736         if (gwtype == CGW_TYPE_CAN_CAN) {
737
738                 /* check CGW_TYPE_CAN_CAN specific attributes */
739
740                 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
741                 memset(ccgw, 0, sizeof(*ccgw)); 
742
743                 /* check for can_filter in attributes */
744                 if (tb[CGW_FILTER] &&
745                     nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
746                         nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
747                                    sizeof(struct can_filter));
748
749                 err = -ENODEV;
750
751                 /* specifying two interfaces is mandatory */
752                 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
753                         return err;
754
755                 if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
756                         nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
757                                    sizeof(u32));
758
759                 if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
760                         nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
761                                    sizeof(u32));
762
763                 /* both indices set to 0 for flushing all routing entries */
764                 if (!ccgw->src_idx && !ccgw->dst_idx)
765                         return 0;
766
767                 /* only one index set to 0 is an error */
768                 if (!ccgw->src_idx || !ccgw->dst_idx)
769                         return err;
770         }
771
772         /* add the checks for other gwtypes here */
773
774         return 0;
775 }
776
777 static int cgw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh,
778                           void *arg)
779 {
780         struct rtcanmsg *r;
781         struct cgw_job *gwj;
782         int err = 0;
783
784         if (nlmsg_len(nlh) < sizeof(*r))
785                 return -EINVAL;
786
787         r = nlmsg_data(nlh);
788         if (r->can_family != AF_CAN)
789                 return -EPFNOSUPPORT;
790
791         /* so far we only support CAN -> CAN routings */
792         if (r->gwtype != CGW_TYPE_CAN_CAN)
793                 return -EINVAL;
794
795         gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
796         if (!gwj)
797                 return -ENOMEM;
798
799         gwj->handled_frames = 0;
800         gwj->dropped_frames = 0;
801         gwj->flags = r->flags;
802         gwj->gwtype = r->gwtype;
803
804         err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
805         if (err < 0)
806                 goto out;
807
808         err = -ENODEV;
809
810         /* ifindex == 0 is not allowed for job creation */
811         if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
812                 goto out;
813
814         gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
815
816         if (!gwj->src.dev)
817                 goto out;
818
819         if (gwj->src.dev->type != ARPHRD_CAN)
820                 goto put_src_out;
821
822         gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
823
824         if (!gwj->dst.dev)
825                 goto put_src_out;
826
827         if (gwj->dst.dev->type != ARPHRD_CAN)
828                 goto put_src_dst_out;
829                 
830         ASSERT_RTNL();
831
832         err = cgw_register_filter(gwj);
833         if (!err)
834                 hlist_add_head_rcu(&gwj->list, &cgw_list);
835
836 put_src_dst_out:
837         dev_put(gwj->dst.dev);
838 put_src_out:
839         dev_put(gwj->src.dev);
840 out:
841         if (err)
842                 kmem_cache_free(cgw_cache, gwj);
843
844         return err;
845 }
846
847 static void cgw_remove_all_jobs(void)
848 {
849         struct cgw_job *gwj = NULL;
850         struct hlist_node *n, *nx;
851
852         ASSERT_RTNL();
853
854         hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
855                 hlist_del(&gwj->list);
856                 cgw_unregister_filter(gwj);
857                 kfree(gwj);
858         }
859 }
860
861 static int cgw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
862 {
863         struct cgw_job *gwj = NULL;
864         struct hlist_node *n, *nx;
865         struct rtcanmsg *r;
866         struct cf_mod mod;
867         struct can_can_gw ccgw;
868         int err = 0;
869
870         if (nlmsg_len(nlh) < sizeof(*r))
871                 return -EINVAL;
872
873         r = nlmsg_data(nlh);
874         if (r->can_family != AF_CAN)
875                 return -EPFNOSUPPORT;
876
877         /* so far we only support CAN -> CAN routings */
878         if (r->gwtype != CGW_TYPE_CAN_CAN)
879                 return -EINVAL;
880
881         err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
882         if (err < 0)
883                 return err;
884
885         /* two interface indices both set to 0 => remove all entries */
886         if (!ccgw.src_idx && !ccgw.dst_idx) {
887                 cgw_remove_all_jobs();
888                 return 0;
889         }
890
891         err = -EINVAL;
892
893         ASSERT_RTNL();
894
895         /* remove only the first matching entry */
896         hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
897
898                 if (gwj->flags != r->flags)
899                         continue;
900
901                 if (memcmp(&gwj->mod, &mod, sizeof(mod)))
902                         continue;
903
904                 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
905                 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
906                         continue;
907
908                 hlist_del(&gwj->list);
909                 cgw_unregister_filter(gwj);
910                 kfree(gwj);
911                 err = 0;
912                 break;
913         }
914
915         return err;
916 }
917
918 static __init int cgw_module_init(void)
919 {
920         printk(banner);
921
922         cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
923                                       0, 0, NULL);
924
925         if (!cgw_cache)
926                 return -ENOMEM;
927
928         /* set notifier */
929         notifier.notifier_call = cgw_notifier;
930         register_netdevice_notifier(&notifier);
931
932 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
933         /* initialize struct for dev_pick_tx() */
934         sk_tx_queue_clear(&gw_dummy_sk);
935 #endif
936
937         if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs)) {
938                 unregister_netdevice_notifier(&notifier);
939                 kmem_cache_destroy(cgw_cache);
940                 return -ENOBUFS;
941         }
942
943         /* Only the first call to __rtnl_register can fail */
944         __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL);
945         __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL);
946
947         return 0;
948 }
949
950 static __exit void cgw_module_exit(void)
951 {
952         rtnl_unregister_all(PF_CAN);
953
954         unregister_netdevice_notifier(&notifier);
955
956         rtnl_lock();
957         cgw_remove_all_jobs();
958         rtnl_unlock();
959
960         rcu_barrier(); /* Wait for completion of call_rcu()'s */
961
962         kmem_cache_destroy(cgw_cache);
963 }
964
965 module_init(cgw_module_init);
966 module_exit(cgw_module_exit);