2 * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
4 * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
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
40 * Send feedback to <socketcan-users@lists.berlios.de>
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>
63 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
66 #define CAN_GW_VERSION "20101205"
67 static __initdata const char banner[] =
68 KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
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");
76 static struct notifier_block notifier;
78 static struct kmem_cache *cgw_cache __read_mostly;
80 static struct sock gw_dummy_sk;
82 /* structure that contains the (on-the-fly) CAN frame modifications */
96 void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
99 /* CAN frame checksum calculation after CAN frame modifications */
101 struct cgw_csum_xor xor;
102 struct cgw_csum_crc8 crc8;
105 void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
106 void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
112 * So far we just support CAN -> CAN routing and frame modifications.
114 * The internal can_can_gw structure contains data and attributes for
115 * a CAN -> CAN gateway job.
118 struct can_filter filter;
123 /* list entry for CAN gateways jobs */
125 struct hlist_node list;
131 /* CAN frame data source */
132 struct net_device *dev;
135 /* CAN frame data destination */
136 struct net_device *dev;
139 struct can_can_gw ccgw;
146 /* modification functions that are invoked in the hot path in can_can_gw_rcv */
148 #define MODFUNC(func, op) static void func (struct can_frame *cf, \
149 struct cf_mod *mod) { op ; }
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)
164 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
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.
172 dst->can_id = src->can_id;
173 dst->can_dlc = src->can_dlc;
174 *(u64 *)dst->data = *(u64 *)src->data;
177 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
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])
188 if (fr > -9 && fr < 8 &&
196 static inline int calc_idx(int idx, int rx_dlc)
204 static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
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;
212 if (from < 0 || to < 0 || res < 0)
216 for (i = from; i <= to; i++)
219 for (i = from; i >= to; i--)
226 static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
228 u8 val = xor->init_xor_val;
231 for (i = xor->from_idx; i <= xor->to_idx; i++)
234 cf->data[xor->result_idx] = val;
237 static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
239 u8 val = xor->init_xor_val;
242 for (i = xor->from_idx; i >= xor->to_idx; i--)
245 cf->data[xor->result_idx] = val;
248 static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
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;
256 if (from < 0 || to < 0 || res < 0)
260 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
261 crc = crc8->crctab[crc^cf->data[i]];
263 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
264 crc = crc8->crctab[crc^cf->data[i]];
267 switch (crc8->profile) {
269 case CGW_CRC8PRF_1U8:
270 crc = crc8->crctab[crc^crc8->profile_data[0]];
273 case CGW_CRC8PRF_16U8:
274 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
277 case CGW_CRC8PRF_SFFID_XOR:
278 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
279 (cf->can_id >> 8 & 0xFF)];
284 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
287 static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
289 u8 crc = crc8->init_crc_val;
292 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
293 crc = crc8->crctab[crc^cf->data[i]];
295 switch (crc8->profile) {
297 case CGW_CRC8PRF_1U8:
298 crc = crc8->crctab[crc^crc8->profile_data[0]];
301 case CGW_CRC8PRF_16U8:
302 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
305 case CGW_CRC8PRF_SFFID_XOR:
306 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
307 (cf->can_id >> 8 & 0xFF)];
311 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
314 static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
316 u8 crc = crc8->init_crc_val;
319 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
320 crc = crc8->crctab[crc^cf->data[i]];
322 switch (crc8->profile) {
324 case CGW_CRC8PRF_1U8:
325 crc = crc8->crctab[crc^crc8->profile_data[0]];
328 case CGW_CRC8PRF_16U8:
329 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
332 case CGW_CRC8PRF_SFFID_XOR:
333 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
334 (cf->can_id >> 8 & 0xFF)];
338 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
341 /* the receive & process & send function */
342 static void can_can_gw_rcv(struct sk_buff *skb, void *data)
344 struct cgw_job *gwj = (struct cgw_job *)data;
345 struct can_frame *cf;
346 struct sk_buff *nskb;
349 /* do not handle already routed frames */
350 if (skb->sk == &gw_dummy_sk)
353 if (!(gwj->dst.dev->flags & IFF_UP)) {
354 gwj->dropped_frames++;
359 * clone the given skb, which has not been done in can_rcv()
361 * When there is at least one modification function activated,
362 * we need to copy the skb as we want to modify skb->data.
364 if (gwj->mod.modfunc[0])
365 nskb = skb_copy(skb, GFP_ATOMIC);
367 nskb = skb_clone(skb, GFP_ATOMIC);
370 gwj->dropped_frames++;
374 /* mark routed frames with a 'special' sk value */
375 nskb->sk = &gw_dummy_sk;
376 nskb->dev = gwj->dst.dev;
378 /* pointer to modifiable CAN frame */
379 cf = (struct can_frame *)nskb->data;
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);
385 /* check for checksum updates when the CAN frame has been modified */
387 if (gwj->mod.csumfunc.crc8)
388 (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
390 if (gwj->mod.csumfunc.xor)
391 (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
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;
398 /* send to netdevice */
399 if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
400 gwj->dropped_frames++;
402 gwj->handled_frames++;
405 static inline int cgw_register_filter(struct cgw_job *gwj)
407 return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
408 gwj->ccgw.filter.can_mask, can_can_gw_rcv,
412 static inline void cgw_unregister_filter(struct cgw_job *gwj)
414 can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
415 gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
418 static int cgw_notifier(struct notifier_block *nb,
419 unsigned long msg, void *data)
421 struct net_device *dev = (struct net_device *)data;
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
424 if (!net_eq(dev_net(dev), &init_net))
426 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
427 if (dev->nd_net != &init_net)
430 if (dev->type != ARPHRD_CAN)
433 if (msg == NETDEV_UNREGISTER) {
435 struct cgw_job *gwj = NULL;
436 struct hlist_node *n, *nx;
440 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
442 if (gwj->src.dev == dev || gwj->dst.dev == dev) {
443 hlist_del(&gwj->list);
444 cgw_unregister_filter(gwj);
453 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
455 struct cgw_frame_mod mb;
456 struct rtcanmsg *rtcan;
457 struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
461 rtcan = nlmsg_data(nlh);
462 rtcan->can_family = AF_CAN;
463 rtcan->gwtype = gwj->gwtype;
464 rtcan->flags = gwj->flags;
466 /* add statistics if available */
468 if (gwj->handled_frames) {
469 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
472 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
475 if (gwj->dropped_frames) {
476 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
479 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
482 /* check non default settings of attributes */
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)
490 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
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)
499 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
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)
508 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
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)
517 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
520 if (gwj->mod.csumfunc.crc8) {
521 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
522 &gwj->mod.csum.crc8) < 0)
525 nlh->nlmsg_len += NLA_HDRLEN + \
526 NLA_ALIGN(CGW_CS_CRC8_LEN);
529 if (gwj->mod.csumfunc.xor) {
530 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
531 &gwj->mod.csum.xor) < 0)
534 nlh->nlmsg_len += NLA_HDRLEN + \
535 NLA_ALIGN(CGW_CS_XOR_LEN);
538 if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
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)
545 nlh->nlmsg_len += NLA_HDRLEN +
546 NLA_ALIGN(sizeof(struct can_filter));
549 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
552 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
554 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
557 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
563 nlmsg_cancel(skb, nlh);
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)
570 struct cgw_job *gwj = NULL;
571 struct hlist_node *n;
573 int s_idx = cb->args[0];
576 hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
580 if (cgw_put_job(skb, gwj) < 0)
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)
596 struct nlattr *tb[CGW_MAX+1];
597 struct cgw_frame_mod mb;
601 /* initialize modification & checksum data space */
602 memset(mod, 0, sizeof(*mod));
604 err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
608 /* check for AND/OR/XOR/SET modifications */
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);
614 canframecpy(&mod->modframe.and, &mb.cf);
615 mod->modtype.and = mb.modtype;
617 if (mb.modtype & CGW_MOD_ID)
618 mod->modfunc[modidx++] = mod_and_id;
620 if (mb.modtype & CGW_MOD_DLC)
621 mod->modfunc[modidx++] = mod_and_dlc;
623 if (mb.modtype & CGW_MOD_DATA)
624 mod->modfunc[modidx++] = mod_and_data;
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);
631 canframecpy(&mod->modframe.or, &mb.cf);
632 mod->modtype.or = mb.modtype;
634 if (mb.modtype & CGW_MOD_ID)
635 mod->modfunc[modidx++] = mod_or_id;
637 if (mb.modtype & CGW_MOD_DLC)
638 mod->modfunc[modidx++] = mod_or_dlc;
640 if (mb.modtype & CGW_MOD_DATA)
641 mod->modfunc[modidx++] = mod_or_data;
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);
648 canframecpy(&mod->modframe.xor, &mb.cf);
649 mod->modtype.xor = mb.modtype;
651 if (mb.modtype & CGW_MOD_ID)
652 mod->modfunc[modidx++] = mod_xor_id;
654 if (mb.modtype & CGW_MOD_DLC)
655 mod->modfunc[modidx++] = mod_xor_dlc;
657 if (mb.modtype & CGW_MOD_DATA)
658 mod->modfunc[modidx++] = mod_xor_data;
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);
665 canframecpy(&mod->modframe.set, &mb.cf);
666 mod->modtype.set = mb.modtype;
668 if (mb.modtype & CGW_MOD_ID)
669 mod->modfunc[modidx++] = mod_set_id;
671 if (mb.modtype & CGW_MOD_DLC)
672 mod->modfunc[modidx++] = mod_set_dlc;
674 if (mb.modtype & CGW_MOD_DATA)
675 mod->modfunc[modidx++] = mod_set_data;
678 /* check for checksum operations after CAN frame modifications */
681 if (tb[CGW_CS_CRC8] &&
682 nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
684 struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
685 nla_data(tb[CGW_CS_CRC8]);
687 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
692 nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
696 * select dedicated processing function to reduce
697 * runtime operations in receive hot path.
699 if (c->from_idx < 0 || c->to_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;
705 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
708 if (tb[CGW_CS_XOR] &&
709 nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
711 struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
712 nla_data(tb[CGW_CS_XOR]);
714 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
719 nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
723 * select dedicated processing function to reduce
724 * runtime operations in receive hot path.
726 if (c->from_idx < 0 || c->to_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;
732 mod->csumfunc.xor = cgw_csum_xor_neg;
736 if (gwtype == CGW_TYPE_CAN_CAN) {
738 /* check CGW_TYPE_CAN_CAN specific attributes */
740 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
741 memset(ccgw, 0, sizeof(*ccgw));
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));
751 /* specifying two interfaces is mandatory */
752 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
755 if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
756 nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
759 if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
760 nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
763 /* both indices set to 0 for flushing all routing entries */
764 if (!ccgw->src_idx && !ccgw->dst_idx)
767 /* only one index set to 0 is an error */
768 if (!ccgw->src_idx || !ccgw->dst_idx)
772 /* add the checks for other gwtypes here */
777 static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
784 if (nlmsg_len(nlh) < sizeof(*r))
788 if (r->can_family != AF_CAN)
789 return -EPFNOSUPPORT;
791 /* so far we only support CAN -> CAN routings */
792 if (r->gwtype != CGW_TYPE_CAN_CAN)
795 gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
799 gwj->handled_frames = 0;
800 gwj->dropped_frames = 0;
801 gwj->flags = r->flags;
802 gwj->gwtype = r->gwtype;
804 err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
810 /* ifindex == 0 is not allowed for job creation */
811 if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
814 gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
819 if (gwj->src.dev->type != ARPHRD_CAN)
822 gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
827 if (gwj->dst.dev->type != ARPHRD_CAN)
828 goto put_src_dst_out;
832 err = cgw_register_filter(gwj);
834 hlist_add_head_rcu(&gwj->list, &cgw_list);
837 dev_put(gwj->dst.dev);
839 dev_put(gwj->src.dev);
842 kmem_cache_free(cgw_cache, gwj);
847 static void cgw_remove_all_jobs(void)
849 struct cgw_job *gwj = NULL;
850 struct hlist_node *n, *nx;
854 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
855 hlist_del(&gwj->list);
856 cgw_unregister_filter(gwj);
861 static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
863 struct cgw_job *gwj = NULL;
864 struct hlist_node *n, *nx;
867 struct can_can_gw ccgw;
870 if (nlmsg_len(nlh) < sizeof(*r))
874 if (r->can_family != AF_CAN)
875 return -EPFNOSUPPORT;
877 /* so far we only support CAN -> CAN routings */
878 if (r->gwtype != CGW_TYPE_CAN_CAN)
881 err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
885 /* two interface indices both set to 0 => remove all entries */
886 if (!ccgw.src_idx && !ccgw.dst_idx) {
887 cgw_remove_all_jobs();
895 /* remove only the first matching entry */
896 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
898 if (gwj->flags != r->flags)
901 if (memcmp(&gwj->mod, &mod, sizeof(mod)))
904 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
905 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
908 hlist_del(&gwj->list);
909 cgw_unregister_filter(gwj);
918 static __init int cgw_module_init(void)
922 cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
929 notifier.notifier_call = cgw_notifier;
930 register_netdevice_notifier(¬ifier);
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);
937 if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs)) {
938 unregister_netdevice_notifier(¬ifier);
939 kmem_cache_destroy(cgw_cache);
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);
950 static __exit void cgw_module_exit(void)
952 rtnl_unregister_all(PF_CAN);
954 unregister_netdevice_notifier(¬ifier);
957 cgw_remove_all_jobs();
960 rcu_barrier(); /* Wait for completion of call_rcu()'s */
962 kmem_cache_destroy(cgw_cache);
965 module_init(cgw_module_init);
966 module_exit(cgw_module_exit);