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