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 "20101209"
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 /* structure that contains the (on-the-fly) CAN frame modifications */
94 void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
97 /* CAN frame checksum calculation after CAN frame modifications */
99 struct cgw_csum_xor xor;
100 struct cgw_csum_crc8 crc8;
103 void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
104 void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
110 * So far we just support CAN -> CAN routing and frame modifications.
112 * The internal can_can_gw structure contains data and attributes for
113 * a CAN -> CAN gateway job.
116 struct can_filter filter;
121 /* list entry for CAN gateways jobs */
123 struct hlist_node list;
129 /* CAN frame data source */
130 struct net_device *dev;
133 /* CAN frame data destination */
134 struct net_device *dev;
137 struct can_can_gw ccgw;
144 /* modification functions that are invoked in the hot path in can_can_gw_rcv */
146 #define MODFUNC(func, op) static void func (struct can_frame *cf, \
147 struct cf_mod *mod) { op ; }
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)
162 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
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.
170 dst->can_id = src->can_id;
171 dst->can_dlc = src->can_dlc;
172 *(u64 *)dst->data = *(u64 *)src->data;
175 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
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])
186 if (fr > -9 && fr < 8 &&
194 static inline int calc_idx(int idx, int rx_dlc)
202 static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
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;
210 if (from < 0 || to < 0 || res < 0)
214 for (i = from; i <= to; i++)
217 for (i = from; i >= to; i--)
224 static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
226 u8 val = xor->init_xor_val;
229 for (i = xor->from_idx; i <= xor->to_idx; i++)
232 cf->data[xor->result_idx] = val;
235 static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
237 u8 val = xor->init_xor_val;
240 for (i = xor->from_idx; i >= xor->to_idx; i--)
243 cf->data[xor->result_idx] = val;
246 static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
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;
254 if (from < 0 || to < 0 || res < 0)
258 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
259 crc = crc8->crctab[crc^cf->data[i]];
261 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
262 crc = crc8->crctab[crc^cf->data[i]];
265 switch (crc8->profile) {
267 case CGW_CRC8PRF_1U8:
268 crc = crc8->crctab[crc^crc8->profile_data[0]];
271 case CGW_CRC8PRF_16U8:
272 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
275 case CGW_CRC8PRF_SFFID_XOR:
276 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
277 (cf->can_id >> 8 & 0xFF)];
282 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
285 static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
287 u8 crc = crc8->init_crc_val;
290 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
291 crc = crc8->crctab[crc^cf->data[i]];
293 switch (crc8->profile) {
295 case CGW_CRC8PRF_1U8:
296 crc = crc8->crctab[crc^crc8->profile_data[0]];
299 case CGW_CRC8PRF_16U8:
300 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
303 case CGW_CRC8PRF_SFFID_XOR:
304 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
305 (cf->can_id >> 8 & 0xFF)];
309 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
312 static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
314 u8 crc = crc8->init_crc_val;
317 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
318 crc = crc8->crctab[crc^cf->data[i]];
320 switch (crc8->profile) {
322 case CGW_CRC8PRF_1U8:
323 crc = crc8->crctab[crc^crc8->profile_data[0]];
326 case CGW_CRC8PRF_16U8:
327 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
330 case CGW_CRC8PRF_SFFID_XOR:
331 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
332 (cf->can_id >> 8 & 0xFF)];
336 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
339 /* the receive & process & send function */
340 static void can_can_gw_rcv(struct sk_buff *skb, void *data)
342 struct cgw_job *gwj = (struct cgw_job *)data;
343 struct can_frame *cf;
344 struct sk_buff *nskb;
347 /* do not handle already routed frames - see comment below */
348 if (skb_mac_header_was_set(skb))
351 if (!(gwj->dst.dev->flags & IFF_UP)) {
352 gwj->dropped_frames++;
357 * clone the given skb, which has not been done in can_rcv()
359 * When there is at least one modification function activated,
360 * we need to copy the skb as we want to modify skb->data.
362 if (gwj->mod.modfunc[0])
363 nskb = skb_copy(skb, GFP_ATOMIC);
365 nskb = skb_clone(skb, GFP_ATOMIC);
368 gwj->dropped_frames++;
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.
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.
380 skb_set_mac_header(nskb, 8);
381 nskb->dev = gwj->dst.dev;
383 /* pointer to modifiable CAN frame */
384 cf = (struct can_frame *)nskb->data;
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);
390 /* check for checksum updates when the CAN frame has been modified */
392 if (gwj->mod.csumfunc.crc8)
393 (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
395 if (gwj->mod.csumfunc.xor)
396 (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
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;
403 /* send to netdevice */
404 if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
405 gwj->dropped_frames++;
407 gwj->handled_frames++;
410 static inline int cgw_register_filter(struct cgw_job *gwj)
412 return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
413 gwj->ccgw.filter.can_mask, can_can_gw_rcv,
417 static inline void cgw_unregister_filter(struct cgw_job *gwj)
419 can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
420 gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
423 static int cgw_notifier(struct notifier_block *nb,
424 unsigned long msg, void *data)
426 struct net_device *dev = (struct net_device *)data;
428 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
429 if (!net_eq(dev_net(dev), &init_net))
431 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
432 if (dev->nd_net != &init_net)
435 if (dev->type != ARPHRD_CAN)
438 if (msg == NETDEV_UNREGISTER) {
440 struct cgw_job *gwj = NULL;
441 struct hlist_node *n, *nx;
445 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
447 if (gwj->src.dev == dev || gwj->dst.dev == dev) {
448 hlist_del(&gwj->list);
449 cgw_unregister_filter(gwj);
458 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
460 struct cgw_frame_mod mb;
461 struct rtcanmsg *rtcan;
462 struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
466 rtcan = nlmsg_data(nlh);
467 rtcan->can_family = AF_CAN;
468 rtcan->gwtype = gwj->gwtype;
469 rtcan->flags = gwj->flags;
471 /* add statistics if available */
473 if (gwj->handled_frames) {
474 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
477 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
480 if (gwj->dropped_frames) {
481 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
484 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
487 /* check non default settings of attributes */
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)
495 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
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)
504 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
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)
513 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
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)
522 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
525 if (gwj->mod.csumfunc.crc8) {
526 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
527 &gwj->mod.csum.crc8) < 0)
530 nlh->nlmsg_len += NLA_HDRLEN + \
531 NLA_ALIGN(CGW_CS_CRC8_LEN);
534 if (gwj->mod.csumfunc.xor) {
535 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
536 &gwj->mod.csum.xor) < 0)
539 nlh->nlmsg_len += NLA_HDRLEN + \
540 NLA_ALIGN(CGW_CS_XOR_LEN);
543 if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
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)
550 nlh->nlmsg_len += NLA_HDRLEN +
551 NLA_ALIGN(sizeof(struct can_filter));
554 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
557 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
559 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
562 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
568 nlmsg_cancel(skb, nlh);
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)
575 struct cgw_job *gwj = NULL;
576 struct hlist_node *n;
578 int s_idx = cb->args[0];
581 hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
585 if (cgw_put_job(skb, gwj) < 0)
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)
601 struct nlattr *tb[CGW_MAX+1];
602 struct cgw_frame_mod mb;
606 /* initialize modification & checksum data space */
607 memset(mod, 0, sizeof(*mod));
609 err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
613 /* check for AND/OR/XOR/SET modifications */
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);
619 canframecpy(&mod->modframe.and, &mb.cf);
620 mod->modtype.and = mb.modtype;
622 if (mb.modtype & CGW_MOD_ID)
623 mod->modfunc[modidx++] = mod_and_id;
625 if (mb.modtype & CGW_MOD_DLC)
626 mod->modfunc[modidx++] = mod_and_dlc;
628 if (mb.modtype & CGW_MOD_DATA)
629 mod->modfunc[modidx++] = mod_and_data;
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);
636 canframecpy(&mod->modframe.or, &mb.cf);
637 mod->modtype.or = mb.modtype;
639 if (mb.modtype & CGW_MOD_ID)
640 mod->modfunc[modidx++] = mod_or_id;
642 if (mb.modtype & CGW_MOD_DLC)
643 mod->modfunc[modidx++] = mod_or_dlc;
645 if (mb.modtype & CGW_MOD_DATA)
646 mod->modfunc[modidx++] = mod_or_data;
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);
653 canframecpy(&mod->modframe.xor, &mb.cf);
654 mod->modtype.xor = mb.modtype;
656 if (mb.modtype & CGW_MOD_ID)
657 mod->modfunc[modidx++] = mod_xor_id;
659 if (mb.modtype & CGW_MOD_DLC)
660 mod->modfunc[modidx++] = mod_xor_dlc;
662 if (mb.modtype & CGW_MOD_DATA)
663 mod->modfunc[modidx++] = mod_xor_data;
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);
670 canframecpy(&mod->modframe.set, &mb.cf);
671 mod->modtype.set = mb.modtype;
673 if (mb.modtype & CGW_MOD_ID)
674 mod->modfunc[modidx++] = mod_set_id;
676 if (mb.modtype & CGW_MOD_DLC)
677 mod->modfunc[modidx++] = mod_set_dlc;
679 if (mb.modtype & CGW_MOD_DATA)
680 mod->modfunc[modidx++] = mod_set_data;
683 /* check for checksum operations after CAN frame modifications */
686 if (tb[CGW_CS_CRC8] &&
687 nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
689 struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
690 nla_data(tb[CGW_CS_CRC8]);
692 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
697 nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
701 * select dedicated processing function to reduce
702 * runtime operations in receive hot path.
704 if (c->from_idx < 0 || c->to_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;
710 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
713 if (tb[CGW_CS_XOR] &&
714 nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
716 struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
717 nla_data(tb[CGW_CS_XOR]);
719 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
724 nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
728 * select dedicated processing function to reduce
729 * runtime operations in receive hot path.
731 if (c->from_idx < 0 || c->to_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;
737 mod->csumfunc.xor = cgw_csum_xor_neg;
741 if (gwtype == CGW_TYPE_CAN_CAN) {
743 /* check CGW_TYPE_CAN_CAN specific attributes */
745 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
746 memset(ccgw, 0, sizeof(*ccgw));
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));
756 /* specifying two interfaces is mandatory */
757 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
760 if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
761 nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
764 if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
765 nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
768 /* both indices set to 0 for flushing all routing entries */
769 if (!ccgw->src_idx && !ccgw->dst_idx)
772 /* only one index set to 0 is an error */
773 if (!ccgw->src_idx || !ccgw->dst_idx)
777 /* add the checks for other gwtypes here */
782 static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
789 if (nlmsg_len(nlh) < sizeof(*r))
793 if (r->can_family != AF_CAN)
794 return -EPFNOSUPPORT;
796 /* so far we only support CAN -> CAN routings */
797 if (r->gwtype != CGW_TYPE_CAN_CAN)
800 gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
804 gwj->handled_frames = 0;
805 gwj->dropped_frames = 0;
806 gwj->flags = r->flags;
807 gwj->gwtype = r->gwtype;
809 err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
815 /* ifindex == 0 is not allowed for job creation */
816 if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
819 gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
824 if (gwj->src.dev->type != ARPHRD_CAN)
827 gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
832 if (gwj->dst.dev->type != ARPHRD_CAN)
833 goto put_src_dst_out;
837 err = cgw_register_filter(gwj);
839 hlist_add_head_rcu(&gwj->list, &cgw_list);
842 dev_put(gwj->dst.dev);
844 dev_put(gwj->src.dev);
847 kmem_cache_free(cgw_cache, gwj);
852 static void cgw_remove_all_jobs(void)
854 struct cgw_job *gwj = NULL;
855 struct hlist_node *n, *nx;
859 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
860 hlist_del(&gwj->list);
861 cgw_unregister_filter(gwj);
866 static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
868 struct cgw_job *gwj = NULL;
869 struct hlist_node *n, *nx;
872 struct can_can_gw ccgw;
875 if (nlmsg_len(nlh) < sizeof(*r))
879 if (r->can_family != AF_CAN)
880 return -EPFNOSUPPORT;
882 /* so far we only support CAN -> CAN routings */
883 if (r->gwtype != CGW_TYPE_CAN_CAN)
886 err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
890 /* two interface indices both set to 0 => remove all entries */
891 if (!ccgw.src_idx && !ccgw.dst_idx) {
892 cgw_remove_all_jobs();
900 /* remove only the first matching entry */
901 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
903 if (gwj->flags != r->flags)
906 if (memcmp(&gwj->mod, &mod, sizeof(mod)))
909 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
910 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
913 hlist_del(&gwj->list);
914 cgw_unregister_filter(gwj);
923 static __init int cgw_module_init(void)
927 cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
934 notifier.notifier_call = cgw_notifier;
935 register_netdevice_notifier(¬ifier);
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);