2 * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
4 * Copyright (c) 2011 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/init.h>
46 #include <linux/types.h>
47 #include <linux/list.h>
48 #include <linux/spinlock.h>
49 #include <linux/rcupdate.h>
50 #include <linux/rculist.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
55 #include <linux/can.h>
56 #include <linux/can/core.h>
57 #include <linux/can/gw.h>
58 #include <net/rtnetlink.h>
59 #include <net/net_namespace.h>
62 #define CAN_GW_VERSION "20101209"
63 static __initdata const char banner[] =
64 KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
66 MODULE_DESCRIPTION("PF_CAN netlink gateway");
67 MODULE_LICENSE("Dual BSD/GPL");
68 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
69 MODULE_ALIAS("can-gw");
72 static struct notifier_block notifier;
74 static struct kmem_cache *cgw_cache __read_mostly;
76 /* structure that contains the (on-the-fly) CAN frame modifications */
90 void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
93 /* CAN frame checksum calculation after CAN frame modifications */
95 struct cgw_csum_xor xor;
96 struct cgw_csum_crc8 crc8;
99 void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
100 void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
106 * So far we just support CAN -> CAN routing and frame modifications.
108 * The internal can_can_gw structure contains data and attributes for
109 * a CAN -> CAN gateway job.
112 struct can_filter filter;
117 /* list entry for CAN gateways jobs */
119 struct hlist_node list;
125 /* CAN frame data source */
126 struct net_device *dev;
129 /* CAN frame data destination */
130 struct net_device *dev;
133 struct can_can_gw ccgw;
140 /* modification functions that are invoked in the hot path in can_can_gw_rcv */
142 #define MODFUNC(func, op) static void func(struct can_frame *cf, \
143 struct cf_mod *mod) { op ; }
145 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
146 MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
147 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
148 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
149 MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
150 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
151 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
152 MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
153 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
154 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
155 MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
156 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
158 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
161 * Copy the struct members separately to ensure that no uninitialized
162 * data are copied in the 3 bytes hole of the struct. This is needed
163 * to make easy compares of the data in the struct cf_mod.
166 dst->can_id = src->can_id;
167 dst->can_dlc = src->can_dlc;
168 *(u64 *)dst->data = *(u64 *)src->data;
171 static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
174 * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
175 * relative to received dlc -1 .. -8 :
176 * e.g. for received dlc = 8
177 * -1 => index = 7 (data[7])
178 * -3 => index = 5 (data[5])
179 * -8 => index = 0 (data[0])
182 if (fr > -9 && fr < 8 &&
190 static inline int calc_idx(int idx, int rx_dlc)
198 static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
200 int from = calc_idx(xor->from_idx, cf->can_dlc);
201 int to = calc_idx(xor->to_idx, cf->can_dlc);
202 int res = calc_idx(xor->result_idx, cf->can_dlc);
203 u8 val = xor->init_xor_val;
206 if (from < 0 || to < 0 || res < 0)
210 for (i = from; i <= to; i++)
213 for (i = from; i >= to; i--)
220 static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
222 u8 val = xor->init_xor_val;
225 for (i = xor->from_idx; i <= xor->to_idx; i++)
228 cf->data[xor->result_idx] = val;
231 static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
233 u8 val = xor->init_xor_val;
236 for (i = xor->from_idx; i >= xor->to_idx; i--)
239 cf->data[xor->result_idx] = val;
242 static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
244 int from = calc_idx(crc8->from_idx, cf->can_dlc);
245 int to = calc_idx(crc8->to_idx, cf->can_dlc);
246 int res = calc_idx(crc8->result_idx, cf->can_dlc);
247 u8 crc = crc8->init_crc_val;
250 if (from < 0 || to < 0 || res < 0)
254 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
255 crc = crc8->crctab[crc^cf->data[i]];
257 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
258 crc = crc8->crctab[crc^cf->data[i]];
261 switch (crc8->profile) {
263 case CGW_CRC8PRF_1U8:
264 crc = crc8->crctab[crc^crc8->profile_data[0]];
267 case CGW_CRC8PRF_16U8:
268 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
271 case CGW_CRC8PRF_SFFID_XOR:
272 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
273 (cf->can_id >> 8 & 0xFF)];
278 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
281 static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
283 u8 crc = crc8->init_crc_val;
286 for (i = crc8->from_idx; i <= crc8->to_idx; i++)
287 crc = crc8->crctab[crc^cf->data[i]];
289 switch (crc8->profile) {
291 case CGW_CRC8PRF_1U8:
292 crc = crc8->crctab[crc^crc8->profile_data[0]];
295 case CGW_CRC8PRF_16U8:
296 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
299 case CGW_CRC8PRF_SFFID_XOR:
300 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
301 (cf->can_id >> 8 & 0xFF)];
305 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
308 static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
310 u8 crc = crc8->init_crc_val;
313 for (i = crc8->from_idx; i >= crc8->to_idx; i--)
314 crc = crc8->crctab[crc^cf->data[i]];
316 switch (crc8->profile) {
318 case CGW_CRC8PRF_1U8:
319 crc = crc8->crctab[crc^crc8->profile_data[0]];
322 case CGW_CRC8PRF_16U8:
323 crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
326 case CGW_CRC8PRF_SFFID_XOR:
327 crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
328 (cf->can_id >> 8 & 0xFF)];
332 cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
335 /* the receive & process & send function */
336 static void can_can_gw_rcv(struct sk_buff *skb, void *data)
338 struct cgw_job *gwj = (struct cgw_job *)data;
339 struct can_frame *cf;
340 struct sk_buff *nskb;
343 /* do not handle already routed frames - see comment below */
344 if (skb_mac_header_was_set(skb))
347 if (!(gwj->dst.dev->flags & IFF_UP)) {
348 gwj->dropped_frames++;
353 * clone the given skb, which has not been done in can_rcv()
355 * When there is at least one modification function activated,
356 * we need to copy the skb as we want to modify skb->data.
358 if (gwj->mod.modfunc[0])
359 nskb = skb_copy(skb, GFP_ATOMIC);
361 nskb = skb_clone(skb, GFP_ATOMIC);
364 gwj->dropped_frames++;
369 * Mark routed frames by setting some mac header length which is
370 * not relevant for the CAN frames located in the skb->data section.
372 * As dev->header_ops is not set in CAN netdevices no one is ever
373 * accessing the various header offsets in the CAN skbuffs anyway.
374 * E.g. using the packet socket to read CAN frames is still working.
376 skb_set_mac_header(nskb, 8);
377 nskb->dev = gwj->dst.dev;
379 /* pointer to modifiable CAN frame */
380 cf = (struct can_frame *)nskb->data;
382 /* perform preprocessed modification functions if there are any */
383 while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
384 (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
386 /* check for checksum updates when the CAN frame has been modified */
388 if (gwj->mod.csumfunc.crc8)
389 (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
391 if (gwj->mod.csumfunc.xor)
392 (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
395 /* clear the skb timestamp if not configured the other way */
396 if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
397 nskb->tstamp.tv64 = 0;
399 /* send to netdevice */
400 if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
401 gwj->dropped_frames++;
403 gwj->handled_frames++;
406 static inline int cgw_register_filter(struct cgw_job *gwj)
408 return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
409 gwj->ccgw.filter.can_mask, can_can_gw_rcv,
413 static inline void cgw_unregister_filter(struct cgw_job *gwj)
415 can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
416 gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
419 static int cgw_notifier(struct notifier_block *nb,
420 unsigned long msg, void *data)
422 struct net_device *dev = (struct net_device *)data;
424 if (!net_eq(dev_net(dev), &init_net))
426 if (dev->type != ARPHRD_CAN)
429 if (msg == NETDEV_UNREGISTER) {
431 struct cgw_job *gwj = NULL;
432 struct hlist_node *n, *nx;
436 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
438 if (gwj->src.dev == dev || gwj->dst.dev == dev) {
439 hlist_del(&gwj->list);
440 cgw_unregister_filter(gwj);
449 static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
451 struct cgw_frame_mod mb;
452 struct rtcanmsg *rtcan;
453 struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
457 rtcan = nlmsg_data(nlh);
458 rtcan->can_family = AF_CAN;
459 rtcan->gwtype = gwj->gwtype;
460 rtcan->flags = gwj->flags;
462 /* add statistics if available */
464 if (gwj->handled_frames) {
465 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
468 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
471 if (gwj->dropped_frames) {
472 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
475 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
478 /* check non default settings of attributes */
480 if (gwj->mod.modtype.and) {
481 memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
482 mb.modtype = gwj->mod.modtype.and;
483 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
486 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
489 if (gwj->mod.modtype.or) {
490 memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
491 mb.modtype = gwj->mod.modtype.or;
492 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
495 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
498 if (gwj->mod.modtype.xor) {
499 memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
500 mb.modtype = gwj->mod.modtype.xor;
501 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
504 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
507 if (gwj->mod.modtype.set) {
508 memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
509 mb.modtype = gwj->mod.modtype.set;
510 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
513 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
516 if (gwj->mod.csumfunc.crc8) {
517 if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
518 &gwj->mod.csum.crc8) < 0)
521 nlh->nlmsg_len += NLA_HDRLEN + \
522 NLA_ALIGN(CGW_CS_CRC8_LEN);
525 if (gwj->mod.csumfunc.xor) {
526 if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
527 &gwj->mod.csum.xor) < 0)
530 nlh->nlmsg_len += NLA_HDRLEN + \
531 NLA_ALIGN(CGW_CS_XOR_LEN);
534 if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
536 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
537 if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
538 &gwj->ccgw.filter) < 0)
541 nlh->nlmsg_len += NLA_HDRLEN +
542 NLA_ALIGN(sizeof(struct can_filter));
545 if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
548 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
550 if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
553 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
559 nlmsg_cancel(skb, nlh);
563 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
564 static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
566 struct cgw_job *gwj = NULL;
567 struct hlist_node *n;
569 int s_idx = cb->args[0];
572 hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
576 if (cgw_put_job(skb, gwj) < 0)
588 /* check for common and gwtype specific attributes */
589 static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
590 u8 gwtype, void *gwtypeattr)
592 struct nlattr *tb[CGW_MAX+1];
593 struct cgw_frame_mod mb;
597 /* initialize modification & checksum data space */
598 memset(mod, 0, sizeof(*mod));
600 err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
604 /* check for AND/OR/XOR/SET modifications */
606 if (tb[CGW_MOD_AND] &&
607 nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
608 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
610 canframecpy(&mod->modframe.and, &mb.cf);
611 mod->modtype.and = mb.modtype;
613 if (mb.modtype & CGW_MOD_ID)
614 mod->modfunc[modidx++] = mod_and_id;
616 if (mb.modtype & CGW_MOD_DLC)
617 mod->modfunc[modidx++] = mod_and_dlc;
619 if (mb.modtype & CGW_MOD_DATA)
620 mod->modfunc[modidx++] = mod_and_data;
623 if (tb[CGW_MOD_OR] &&
624 nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
625 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
627 canframecpy(&mod->modframe.or, &mb.cf);
628 mod->modtype.or = mb.modtype;
630 if (mb.modtype & CGW_MOD_ID)
631 mod->modfunc[modidx++] = mod_or_id;
633 if (mb.modtype & CGW_MOD_DLC)
634 mod->modfunc[modidx++] = mod_or_dlc;
636 if (mb.modtype & CGW_MOD_DATA)
637 mod->modfunc[modidx++] = mod_or_data;
640 if (tb[CGW_MOD_XOR] &&
641 nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
642 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
644 canframecpy(&mod->modframe.xor, &mb.cf);
645 mod->modtype.xor = mb.modtype;
647 if (mb.modtype & CGW_MOD_ID)
648 mod->modfunc[modidx++] = mod_xor_id;
650 if (mb.modtype & CGW_MOD_DLC)
651 mod->modfunc[modidx++] = mod_xor_dlc;
653 if (mb.modtype & CGW_MOD_DATA)
654 mod->modfunc[modidx++] = mod_xor_data;
657 if (tb[CGW_MOD_SET] &&
658 nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
659 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
661 canframecpy(&mod->modframe.set, &mb.cf);
662 mod->modtype.set = mb.modtype;
664 if (mb.modtype & CGW_MOD_ID)
665 mod->modfunc[modidx++] = mod_set_id;
667 if (mb.modtype & CGW_MOD_DLC)
668 mod->modfunc[modidx++] = mod_set_dlc;
670 if (mb.modtype & CGW_MOD_DATA)
671 mod->modfunc[modidx++] = mod_set_data;
674 /* check for checksum operations after CAN frame modifications */
677 if (tb[CGW_CS_CRC8] &&
678 nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
680 struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
681 nla_data(tb[CGW_CS_CRC8]);
683 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
688 nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
692 * select dedicated processing function to reduce
693 * runtime operations in receive hot path.
695 if (c->from_idx < 0 || c->to_idx < 0 ||
697 mod->csumfunc.crc8 = cgw_csum_crc8_rel;
698 else if (c->from_idx <= c->to_idx)
699 mod->csumfunc.crc8 = cgw_csum_crc8_pos;
701 mod->csumfunc.crc8 = cgw_csum_crc8_neg;
704 if (tb[CGW_CS_XOR] &&
705 nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
707 struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
708 nla_data(tb[CGW_CS_XOR]);
710 err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
715 nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
719 * select dedicated processing function to reduce
720 * runtime operations in receive hot path.
722 if (c->from_idx < 0 || c->to_idx < 0 ||
724 mod->csumfunc.xor = cgw_csum_xor_rel;
725 else if (c->from_idx <= c->to_idx)
726 mod->csumfunc.xor = cgw_csum_xor_pos;
728 mod->csumfunc.xor = cgw_csum_xor_neg;
732 if (gwtype == CGW_TYPE_CAN_CAN) {
734 /* check CGW_TYPE_CAN_CAN specific attributes */
736 struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
737 memset(ccgw, 0, sizeof(*ccgw));
739 /* check for can_filter in attributes */
740 if (tb[CGW_FILTER] &&
741 nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
742 nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
743 sizeof(struct can_filter));
747 /* specifying two interfaces is mandatory */
748 if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
751 if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
752 nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
755 if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
756 nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
759 /* both indices set to 0 for flushing all routing entries */
760 if (!ccgw->src_idx && !ccgw->dst_idx)
763 /* only one index set to 0 is an error */
764 if (!ccgw->src_idx || !ccgw->dst_idx)
768 /* add the checks for other gwtypes here */
773 static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
780 if (nlmsg_len(nlh) < sizeof(*r))
784 if (r->can_family != AF_CAN)
785 return -EPFNOSUPPORT;
787 /* so far we only support CAN -> CAN routings */
788 if (r->gwtype != CGW_TYPE_CAN_CAN)
791 gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
795 gwj->handled_frames = 0;
796 gwj->dropped_frames = 0;
797 gwj->flags = r->flags;
798 gwj->gwtype = r->gwtype;
800 err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
806 /* ifindex == 0 is not allowed for job creation */
807 if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
810 gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
815 /* check for CAN netdev not using header_ops - see gw_rcv() */
816 if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
819 gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
824 /* check for CAN netdev not using header_ops - see gw_rcv() */
825 if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
826 goto put_src_dst_out;
830 err = cgw_register_filter(gwj);
832 hlist_add_head_rcu(&gwj->list, &cgw_list);
835 dev_put(gwj->dst.dev);
837 dev_put(gwj->src.dev);
840 kmem_cache_free(cgw_cache, gwj);
845 static void cgw_remove_all_jobs(void)
847 struct cgw_job *gwj = NULL;
848 struct hlist_node *n, *nx;
852 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
853 hlist_del(&gwj->list);
854 cgw_unregister_filter(gwj);
859 static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
861 struct cgw_job *gwj = NULL;
862 struct hlist_node *n, *nx;
865 struct can_can_gw ccgw;
868 if (nlmsg_len(nlh) < sizeof(*r))
872 if (r->can_family != AF_CAN)
873 return -EPFNOSUPPORT;
875 /* so far we only support CAN -> CAN routings */
876 if (r->gwtype != CGW_TYPE_CAN_CAN)
879 err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
883 /* two interface indices both set to 0 => remove all entries */
884 if (!ccgw.src_idx && !ccgw.dst_idx) {
885 cgw_remove_all_jobs();
893 /* remove only the first matching entry */
894 hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
896 if (gwj->flags != r->flags)
899 if (memcmp(&gwj->mod, &mod, sizeof(mod)))
902 /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
903 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
906 hlist_del(&gwj->list);
907 cgw_unregister_filter(gwj);
916 static __init int cgw_module_init(void)
920 cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
927 notifier.notifier_call = cgw_notifier;
928 register_netdevice_notifier(¬ifier);
930 if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
931 unregister_netdevice_notifier(¬ifier);
932 kmem_cache_destroy(cgw_cache);
936 /* Only the first call to __rtnl_register can fail */
937 __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
938 __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
943 static __exit void cgw_module_exit(void)
945 rtnl_unregister_all(PF_CAN);
947 unregister_netdevice_notifier(¬ifier);
950 cgw_remove_all_jobs();
953 rcu_barrier(); /* Wait for completion of call_rcu()'s */
955 kmem_cache_destroy(cgw_cache);
958 module_init(cgw_module_init);
959 module_exit(cgw_module_exit);