From 4d00594a509c547cf49929ff54819415bfd54683 Mon Sep 17 00:00:00 2001 From: hartkopp Date: Sat, 10 Apr 2010 16:38:48 +0000 Subject: [PATCH] Complete rework of CAN netlink gateway. Major changes: - rework internal structures to prepare routings and modifications of CAN traffic also to non-CAN interfaces - fix reading of netlink messages in cangw.c (added RTCAN_RTA / RTCAN_PAYLOAD macros) - rework reading of gw-job lists in cgw_dump_jobs() - rename of functions and API definitions to have a common namespace cgw_ - added infrastructure to perform crc8 and xor checksums in CAN frame data[] TODO: - add and test functionality for crc8 and xor checksums in CAN frame data[] - add help text for crc8 and xor checksums in CAN frame data[] git-svn-id: svn://svn.berlios.de//socketcan/trunk@1165 030b6a49-0b11-0410-94ab-b0dab22257f2 --- can-utils/cangw.c | 350 +++++++++++---- kernel/2.6/include/socketcan/can/gw.h | 62 ++- kernel/2.6/net/can/gw.c | 588 ++++++++++++++++---------- 3 files changed, 671 insertions(+), 329 deletions(-) diff --git a/can-utils/cangw.c b/can-utils/cangw.c index dd81003..d8757e3 100644 --- a/can-utils/cangw.c +++ b/can-utils/cangw.c @@ -72,6 +72,9 @@ struct modattr { } __attribute__((packed)); +#define RTCAN_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtcanmsg)))) +#define RTCAN_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct rtcanmsg)) + /* some netlink helpers stolen from iproute2 package */ #define NLMSG_TAIL(nmsg) \ ((struct rtattr *)(((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) @@ -128,6 +131,26 @@ void printmod(const char *type, const void *data) printf(" "); } +void print_cs_xor(struct cgw_csum_xor *cs_xor) +{ + printf("-x %d:%d:%d:%02X ", + cs_xor->from_idx, cs_xor->to_idx, + cs_xor->result_idx, cs_xor->prefix_value); +} + +void print_cs_crc8(struct cgw_csum_crc8 *cs_crc8) +{ + int i; + + printf("-c %d:%d:%d:", + cs_crc8->from_idx, cs_crc8->to_idx, + cs_crc8->result_idx); + + for (i = 0; i < 256; i++) + printf("%02X", cs_crc8->crctab[i]); + + printf(" "); +} void print_usage(char *prg) { @@ -160,11 +183,20 @@ void print_usage(char *prg) fprintf(stderr, "\n"); } -int parse_mod(char *optarg, struct modattr *modmsg) +int b64hex(char *asc, unsigned char *bin, int len) { - char *ptr, *nptr; int i; + for (i = 0; i < len; i++) { + if (!sscanf(asc+(i*2), "%2hhx", bin+i)) + return 1; + } + return 0; +} + +int parse_mod(char *optarg, struct modattr *modmsg) +{ + char *ptr, *nptr; char hexdata[17] = {0}; ptr = optarg; @@ -223,14 +255,172 @@ int parse_mod(char *optarg, struct modattr *modmsg) if (strlen(hexdata) != 16) return 6; - for (i = 0; i < 8; i++) { - if (!sscanf(&hexdata[i*2], "%2hhx", &modmsg->cf.data[i])) - return 7; - } + if (b64hex(hexdata, &modmsg->cf.data[0], 8)) + return 7; return 0; /* ok */ } +int parse_rtlist(char *prgname, unsigned char *rxbuf, int len) +{ + char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */ + struct rtcanmsg *rtc; + struct rtattr *rta; + struct nlmsghdr *nlh; + unsigned int src_ifindex = 0; + unsigned int dst_ifindex = 0; + __u32 handled, dropped; + int rtlen; + + + nlh = (struct nlmsghdr *)rxbuf; + + while (1) { + if (!NLMSG_OK(nlh, len)) + return 0; + + if (nlh->nlmsg_type == NLMSG_ERROR) { + printf("NLMSG_ERROR\n"); + return 1; + } + + if (nlh->nlmsg_type == NLMSG_DONE) { + //printf("NLMSG_DONE\n"); + return 1; + } + + rtc = (struct rtcanmsg *)NLMSG_DATA(nlh); + if (rtc->can_family != AF_CAN) { + printf("received msg from unknown family %d\n", rtc->can_family); + return -EINVAL; + } + + if (rtc->gwtype != CGW_TYPE_CAN_CAN) { + printf("received msg with unknown gwtype %d\n", rtc->gwtype); + return -EINVAL; + } + + /* + * print list in a representation that + * can be used directly for start scripts. + * + * To order the mandatory and optional parameters in the + * output string, the NLMSG is parsed twice. + */ + + handled = 0; + dropped = 0; + src_ifindex = 0; + dst_ifindex = 0; + + printf("%s -A ", basename(prgname)); + + /* first parse for mandatory options */ + rta = (struct rtattr *) RTCAN_RTA(rtc); + rtlen = RTCAN_PAYLOAD(nlh); + for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen)) + { + //printf("(A-%d)", rta->rta_type); + switch(rta->rta_type) { + + case CGW_FILTER: + case CGW_MOD_AND: + case CGW_MOD_OR: + case CGW_MOD_XOR: + case CGW_MOD_SET: + case CGW_CS_XOR: + case CGW_CS_CRC8: + break; + + case CGW_SRC_IF: + src_ifindex = *(__u32 *)RTA_DATA(rta); + break; + + case CGW_DST_IF: + dst_ifindex = *(__u32 *)RTA_DATA(rta); + break; + + case CGW_HANDLED: + handled = *(__u32 *)RTA_DATA(rta); + break; + + case CGW_DROPPED: + dropped = *(__u32 *)RTA_DATA(rta); + break; + + default: + printf("Unknown attribute %d!", rta->rta_type); + return -EINVAL; + break; + } + } + + + printf("-s %s ", if_indextoname(src_ifindex, ifname)); + printf("-d %s ", if_indextoname(dst_ifindex, ifname)); + + if (rtc->flags & CGW_FLAGS_CAN_ECHO) + printf("-e "); + + if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP) + printf("-t "); + + /* second parse for mod attributes */ + rta = (struct rtattr *) RTCAN_RTA(rtc); + rtlen = RTCAN_PAYLOAD(nlh); + for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen)) + { + //printf("(B-%d)", rta->rta_type); + switch(rta->rta_type) { + + case CGW_FILTER: + printfilter(RTA_DATA(rta)); + break; + + case CGW_MOD_AND: + printmod("AND", RTA_DATA(rta)); + break; + + case CGW_MOD_OR: + printmod("OR", RTA_DATA(rta)); + break; + + case CGW_MOD_XOR: + printmod("XOR", RTA_DATA(rta)); + break; + + case CGW_MOD_SET: + printmod("SET", RTA_DATA(rta)); + break; + + case CGW_CS_XOR: + print_cs_xor((struct cgw_csum_xor *)RTA_DATA(rta)); + break; + + case CGW_CS_CRC8: + print_cs_crc8((struct cgw_csum_crc8 *)RTA_DATA(rta)); + break; + + case CGW_SRC_IF: + case CGW_DST_IF: + case CGW_HANDLED: + case CGW_DROPPED: + break; + + default: + printf("Unknown attribute %d!", rta->rta_type); + return -EINVAL; + break; + } + } + + printf("# %d handled %d dropped\n", handled, dropped); /* end of entry */ + + /* jump to next NLMSG in the given buffer */ + nlh = NLMSG_NEXT(nlh, len); + } +} + int main(int argc, char **argv) { int s; @@ -241,34 +431,38 @@ int main(int argc, char **argv) int cmd = UNSPEC; int have_filter = 0; + int have_cs_xor = 0; + int have_cs_crc8 = 0; struct { struct nlmsghdr nh; struct rtcanmsg rtcan; - char buf[200]; + char buf[600]; } req; - char rxbuf[8192]; /* netlink receive buffer */ - char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */ + unsigned char rxbuf[8192]; /* netlink receive buffer */ struct nlmsghdr *nlh; struct nlmsgerr *rte; - struct rtcanmsg *rtc; - struct rtattr *rta; - __u32 handled, dropped; - int rtlen; + unsigned int src_ifindex = 0; + unsigned int dst_ifindex = 0; + __u16 flags = 0; int len; struct can_filter filter; struct sockaddr_nl nladdr; + struct cgw_csum_xor cs_xor; + struct cgw_csum_crc8 cs_crc8; + char crc8tab[513] = {0}; + struct modattr modmsg[CGW_MOD_FUNCS]; int modidx = 0; int i; memset(&req, 0, sizeof(req)); - while ((opt = getopt(argc, argv, "ADFLs:d:tef:m:?")) != -1) { + while ((opt = getopt(argc, argv, "ADFLs:d:tef:c:x:m:?")) != -1) { switch (opt) { case 'A': @@ -292,19 +486,19 @@ int main(int argc, char **argv) break; case 's': - req.rtcan.src_ifindex = if_nametoindex(optarg); + src_ifindex = if_nametoindex(optarg); break; case 'd': - req.rtcan.dst_ifindex = if_nametoindex(optarg); + dst_ifindex = if_nametoindex(optarg); break; case 't': - req.rtcan.can_txflags |= CAN_GW_TXFLAGS_SRC_TSTAMP; + flags |= CGW_FLAGS_CAN_SRC_TSTAMP; break; case 'e': - req.rtcan.can_txflags |= CAN_GW_TXFLAGS_ECHO; + flags |= CGW_FLAGS_CAN_ECHO; break; case 'f': @@ -318,6 +512,30 @@ int main(int argc, char **argv) } break; + case 'x': + if (sscanf(optarg, "%hhd:%hhd:%hhd:%hhx", + &cs_xor.from_idx, &cs_xor.to_idx, + &cs_xor.result_idx, &cs_xor.prefix_value) == 4) { + have_cs_xor = 1; + } else { + printf("Bad XOR checksum definition '%s'.\n", optarg); + exit(1); + } + break; + + case 'c': + if ((sscanf(optarg, "%hhd:%hhd:%hhd:%512s", + &cs_crc8.from_idx, &cs_crc8.to_idx, + &cs_crc8.result_idx, crc8tab) == 4) && + (strlen(crc8tab) == 512) && + (b64hex(crc8tab, (unsigned char *)&cs_crc8.crctab, 256) == 0)) { + have_cs_crc8 = 1; + } else { + printf("Bad CRC8 checksum definition '%s'.\n", optarg); + exit(1); + } + break; + case 'm': /* may be triggered by each of the CGW_MOD_FUNCS functions */ if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) { @@ -345,7 +563,7 @@ int main(int argc, char **argv) } if ((cmd == ADD || cmd == DEL) && - ((!req.rtcan.src_ifindex) || (!req.rtcan.dst_ifindex))) { + ((!src_ifindex) || (!dst_ifindex))) { print_usage(basename(argv[0])); exit(1); } @@ -368,8 +586,8 @@ int main(int argc, char **argv) req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; req.nh.nlmsg_type = RTM_DELROUTE; /* if_index set to 0 => remove all entries */ - req.rtcan.src_ifindex = 0; - req.rtcan.dst_ifindex = 0; + src_ifindex = 0; + dst_ifindex = 0; break; case LIST: @@ -387,12 +605,23 @@ int main(int argc, char **argv) req.nh.nlmsg_seq = 0; req.rtcan.can_family = AF_CAN; + req.rtcan.gwtype = CGW_TYPE_CAN_CAN; + req.rtcan.flags = flags; + + addattr_l(&req.nh, sizeof(req), CGW_SRC_IF, &src_ifindex, sizeof(src_ifindex)); + addattr_l(&req.nh, sizeof(req), CGW_DST_IF, &dst_ifindex, sizeof(dst_ifindex)); /* add new attributes here */ if (have_filter) addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter)); + if (have_cs_xor) + addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor)); + + if (have_cs_crc8) + addattr_l(&req.nh, sizeof(req), CGW_CS_CRC8, &cs_crc8, sizeof(cs_crc8)); + /* * a better example code * modmsg.modtype = CGW_MOD_ID; @@ -451,78 +680,17 @@ int main(int argc, char **argv) perror("netlink recv"); return len; } - nlh = (struct nlmsghdr *)rxbuf; - if (nlh->nlmsg_type == NLMSG_DONE) - break; - - rtc = (struct rtcanmsg *)NLMSG_DATA(nlh); - if (rtc->can_family != AF_CAN) { - printf("received msg from unknown family %d\n", rtc->can_family); - return -EINVAL; - } +#if 0 + printf("received msg len %d\n", len); - /* - * print list in a representation that - * can be used directly for start scripts - */ + for (i = 0; i < len; i++) + printf("%02X ", rxbuf[i]); - printf("%s -A ", basename(argv[0])); - printf("-s %s ", if_indextoname(rtc->src_ifindex, ifname)); - printf("-d %s ", if_indextoname(rtc->dst_ifindex, ifname)); - - if (rtc->can_txflags & CAN_GW_TXFLAGS_ECHO) - printf("-e "); - - if (rtc->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP) - printf("-t "); - - /* check for attributes */ - - handled = 0; - dropped = 0; - - rta = (struct rtattr *) RTM_RTA(rtc); - rtlen = RTM_PAYLOAD(nlh); - for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen)) - { - switch(rta->rta_type) { - - case CGW_FILTER: - printfilter(RTA_DATA(rta)); - break; - - case CGW_MOD_AND: - printmod("AND", RTA_DATA(rta)); - break; - - case CGW_MOD_OR: - printmod("OR", RTA_DATA(rta)); - break; - - case CGW_MOD_XOR: - printmod("XOR", RTA_DATA(rta)); - break; - - case CGW_MOD_SET: - printmod("SET", RTA_DATA(rta)); - break; - - case CGW_HANDLED: - handled = *(__u32 *)RTA_DATA(rta); - break; - - case CGW_DROPPED: - dropped = *(__u32 *)RTA_DATA(rta); - break; - - default: - printf("Unknown attribute %d!", rta->rta_type); - return -EINVAL; - break; - } - } - - printf("# %d handled %d dropped\n", handled, dropped); /* end of entry */ + printf("\n"); +#endif + /* leave on errors or NLMSG_DONE */ + if (parse_rtlist(argv[0], rxbuf, len)) + break; } } diff --git a/kernel/2.6/include/socketcan/can/gw.h b/kernel/2.6/include/socketcan/can/gw.h index 5a963ed..f8ead84 100644 --- a/kernel/2.6/include/socketcan/can/gw.h +++ b/kernel/2.6/include/socketcan/can/gw.h @@ -20,30 +20,41 @@ struct rtcanmsg { __u8 can_family; - __u8 can_txflags; - __u16 pad; - __u32 src_ifindex; - __u32 dst_ifindex; + __u8 gwtype; + __u16 flags; }; -#define CAN_GW_TXFLAGS_ECHO 0x01 -#define CAN_GW_TXFLAGS_SRC_TSTAMP 0x02 +/* CAN gateway types */ +enum { + CGW_TYPE_UNSPEC, + CGW_TYPE_CAN_CAN, /* CAN->CAN routing */ + __CGW_TYPE_MAX +}; + +#define CGW_TYPE_MAX (__CGW_TYPE_MAX - 1) /* CAN rtnetlink attribute definitions */ enum { CGW_UNSPEC, - CGW_FILTER, /* specify struct can_filter on source CAN device */ CGW_MOD_AND, /* CAN frame modification binary AND */ CGW_MOD_OR, /* CAN frame modification binary OR */ CGW_MOD_XOR, /* CAN frame modification binary XOR */ CGW_MOD_SET, /* CAN frame modification set alternate values */ + CGW_CS_XOR, /* set data[] XOR checksum into data[index] */ + CGW_CS_CRC8, /* set data[] CRC8 checksum into data[index] */ CGW_HANDLED, /* number of handled CAN frames */ CGW_DROPPED, /* number of dropped CAN frames */ + CGW_SRC_IF, /* ifindex of source network interface */ + CGW_DST_IF, /* ifindex of destination network interface */ + CGW_FILTER, /* specify struct can_filter on source CAN device */ __CGW_MAX }; #define CGW_MAX (__CGW_MAX - 1) +#define CGW_FLAGS_CAN_ECHO 0x01 +#define CGW_FLAGS_CAN_SRC_TSTAMP 0x02 + #define CGW_MOD_FUNCS 4 /* AND OR XOR SET */ /* CAN frame elements that are affected by curr. 3 CAN frame modifications */ @@ -55,11 +66,38 @@ enum { #define MAX_MODFUNCTIONS (CGW_MOD_FUNCS * CGW_FRAME_MODS) -#define CGW_MODATTR_LEN (sizeof(struct can_frame) + 1) +struct cgw_frame_mod { + struct can_frame cf; + __u8 modtype; +} __attribute__((packed)); + +#define CGW_MODATTR_LEN sizeof(struct cgw_frame_mod) + +struct cgw_csum_xor { + __s8 from_idx; + __s8 to_idx; + __s8 result_idx; + __u8 prefix_value; +} __attribute__ ((packed)); + +struct cgw_csum_crc8 { + __s8 from_idx; + __s8 to_idx; + __s8 result_idx; + __u8 crctab[256]; +} __attribute__ ((packed)); + +/* length of checksum operation parameters. idx = index in CAN frame data[] */ +#define CGW_CS_XOR_LEN sizeof(struct cgw_csum_xor) +#define CGW_CS_CRC8_LEN sizeof(struct cgw_csum_crc8) /* * CAN rtnetlink attribute contents in detail * + * CGW_XXX_IF (length 4 bytes): + * Sets an interface index for source/destination network interfaces. + * For the CAN->CAN gwtype the indices of _two_ CAN interfaces are mandatory. + * * CGW_FILTER (length 8 bytes): * Sets a CAN receive filter for the gateway job specified by the * struct can_filter described in include/linux/can.h @@ -71,6 +109,14 @@ enum { * data used as operator * affected CAN frame elements * + * CGW_CS_XOR (length 4 bytes): + * Set a simple XOR checksum starting with the initial prefix-value into + * data[result-idx] using data[start-idx] .. data[end-idx] + * + * CGW_CS_CRC8 (length 259 bytes): + * Set a CRC8 value into data[result-idx] using a given 256 byte CRC8 table and + * a defined input data[start-idx] .. data[end-idx] + * * Remark: The attribute data is a linear buffer. Beware of sending structs! */ diff --git a/kernel/2.6/net/can/gw.c b/kernel/2.6/net/can/gw.c index 895fce7..33d8502 100644 --- a/kernel/2.6/net/can/gw.c +++ b/kernel/2.6/net/can/gw.c @@ -62,30 +62,26 @@ #include /* for RCSID. Removed by mkpatch script */ RCSID("$Id$"); -#define CAN_GW_VERSION "20100222" +#define CAN_GW_VERSION "20100410" static __initdata const char banner[] = KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n"; MODULE_DESCRIPTION("PF_CAN netlink gateway"); MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("Oliver Hartkopp "); +MODULE_ALIAS("can-gw"); -HLIST_HEAD(can_gw_list); -static DEFINE_SPINLOCK(can_gw_list_lock); +HLIST_HEAD(cgw_list); +static DEFINE_SPINLOCK(cgw_list_lock); static struct notifier_block notifier; -static struct kmem_cache *gw_cache __read_mostly; +static struct kmem_cache *cgw_cache __read_mostly; -#define GW_SK_MAGIC ((void *)(¬ifier)) +#define CGW_SK_MAGIC ((void *)(¬ifier)) +#define CGW_CS_DISABLED 42 -/* - * So far we just support CAN -> CAN routing and frame modifications. - * - * The internal can_can_gw structure contains optional attributes for - * a CAN -> CAN gateway job. - */ -struct can_can_gw { - struct can_filter filter; +/* structure that contains the (on-the-fly) CAN frame modifications */ +struct cf_mod { struct { struct can_frame and; struct can_frame or; @@ -99,32 +95,55 @@ struct can_can_gw { u8 set; } modtype; void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf, - struct can_can_gw *mod); + struct cf_mod *mod); + + /* CAN frame checksum calculation after CAN frame modifications */ + struct { + struct cgw_csum_xor xor; + struct cgw_csum_crc8 crc8; + } csum; +}; + + +/* + * So far we just support CAN -> CAN routing and frame modifications. + * + * The internal can_can_gw structure contains data and attributes for + * a CAN -> CAN gateway job. + */ +struct can_can_gw { + struct can_filter filter; + int src_idx; + int dst_idx; }; /* list entry for CAN gateways jobs */ -struct gw_job { +struct cgw_job { struct hlist_node list; struct rcu_head rcu; - struct net_device *src_dev; - struct net_device *dst_dev; - u32 flags; u32 handled_frames; u32 dropped_frames; + struct cf_mod mod; + union { + /* CAN frame data source */ + struct net_device *dev; + } src; + union { + /* CAN frame data destination */ + struct net_device *dev; + } dst; union { struct can_can_gw ccgw; /* tbc */ }; + u8 gwtype; + u16 flags; }; -/* content of u32 gw_job.flags */ -#define CAN_TX_ECHO 0x00000001 -#define CAN_TX_SRC_TSTAMP 0x00000002 - -/* modification functions that are invoked in the hot path in gw_rcv */ +/* modification functions that are invoked in the hot path in can_can_gw_rcv */ #define MODFUNC(func, op) static void func (struct can_frame *cf, \ - struct can_can_gw *mod) { op ; } + struct cf_mod *mod) { op ; } MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id) MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc) @@ -144,7 +163,7 @@ static inline void canframecpy(struct can_frame *dst, struct can_frame *src) /* * Copy the struct members separately to ensure that no uninitialized * data are copied in the 3 bytes hole of the struct. This is needed - * to make easy compares of the data in the struct can_can_gw. + * to make easy compares of the data in the struct cf_mod. */ dst->can_id = src->can_id; @@ -152,19 +171,48 @@ static inline void canframecpy(struct can_frame *dst, struct can_frame *src) *(u64 *)dst->data = *(u64 *)src->data; } +static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re) +{ + /* + * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0] + * relative to received dlc -1 .. -8 : + * e.g. for received dlc = 8 + * -1 => index = 7 (data[7]) + * -3 => index = 5 (data[5]) + * -8 => index = 0 (data[0]) + */ + + if (fr > -9 && fr < 8 && + to > -9 && to < 8 && + re > -9 && re < 8) + return 0; + else + return -EINVAL; +} + +static void cgw_csum_do_xor(struct can_frame *cf, struct cgw_csum_xor *xor) +{ + /* TODO: perform checksum update */ +} + +static void cgw_csum_do_crc8(struct can_frame *cf, struct cgw_csum_crc8 *crc8) +{ + /* TODO: perform checksum update */ +} + /* the receive & process & send function */ -static void gw_rcv(struct sk_buff *skb, void *data) +static void can_can_gw_rcv(struct sk_buff *skb, void *data) { - struct gw_job *gwj = (struct gw_job *)data; + struct cgw_job *gwj = (struct cgw_job *)data; struct can_frame *cf; struct sk_buff *nskb; int modidx = 0; /* do not handle already routed frames */ - if (skb->sk == GW_SK_MAGIC) + if (skb->sk == CGW_SK_MAGIC) return; - if (!(gwj->dst_dev->flags & IFF_UP)) { + if (!(gwj->dst.dev->flags & IFF_UP)) { gwj->dropped_frames++; return; } @@ -175,7 +223,7 @@ static void gw_rcv(struct sk_buff *skb, void *data) * When there is at least one modification function activated, * we need to copy the skb as we want to modify skb->data. */ - if (gwj->ccgw.modfunc[0]) + if (gwj->mod.modfunc[0]) nskb = skb_copy(skb, GFP_ATOMIC); else nskb = skb_clone(skb, GFP_ATOMIC); @@ -186,40 +234,50 @@ static void gw_rcv(struct sk_buff *skb, void *data) } /* mark routed frames with a 'special' sk value */ - nskb->sk = GW_SK_MAGIC; - nskb->dev = gwj->dst_dev; + nskb->sk = CGW_SK_MAGIC; + nskb->dev = gwj->dst.dev; /* pointer to modifiable CAN frame */ cf = (struct can_frame *)nskb->data; /* perform preprocessed modification functions if there are any */ - while (modidx < MAX_MODFUNCTIONS && gwj->ccgw.modfunc[modidx]) - (*gwj->ccgw.modfunc[modidx++])(cf, &gwj->ccgw); + while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx]) + (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod); + + /* check for checksum updates when the CAN frame has been modified */ + if (modidx) { + if (gwj->mod.csum.xor.from_idx != CGW_CS_DISABLED) + cgw_csum_do_xor(cf, &gwj->mod.csum.xor); + + if (gwj->mod.csum.crc8.from_idx != CGW_CS_DISABLED) + cgw_csum_do_crc8(cf, &gwj->mod.csum.crc8); + } /* clear the skb timestamp if not configured the other way */ - if (!(gwj->flags & CAN_TX_SRC_TSTAMP)) + if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP)) nskb->tstamp.tv64 = 0; /* send to netdevice */ - if (can_send(nskb, gwj->flags & CAN_TX_ECHO)) + if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO)) gwj->dropped_frames++; else gwj->handled_frames++; } -static inline int can_gw_register_filter(struct gw_job *gwj) +static inline int cgw_register_filter(struct cgw_job *gwj) { - return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id, - gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw"); + return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id, + gwj->ccgw.filter.can_mask, can_can_gw_rcv, + gwj, "gw"); } -static inline void can_gw_unregister_filter(struct gw_job *gwj) +static inline void cgw_unregister_filter(struct cgw_job *gwj) { - can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id, - gwj->ccgw.filter.can_mask, gw_rcv, gwj); + can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id, + gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj); } -static int gw_notifier(struct notifier_block *nb, +static int cgw_notifier(struct notifier_block *nb, unsigned long msg, void *data) { struct net_device *dev = (struct net_device *)data; @@ -236,33 +294,29 @@ static int gw_notifier(struct notifier_block *nb, if (msg == NETDEV_UNREGISTER) { - struct gw_job *gwj = NULL; + struct cgw_job *gwj = NULL; struct hlist_node *n, *nx; - spin_lock(&can_gw_list_lock); + spin_lock(&cgw_list_lock); - hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) { + hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) { - if (gwj->src_dev == dev || gwj->dst_dev == dev) { + if (gwj->src.dev == dev || gwj->dst.dev == dev) { hlist_del(&gwj->list); - can_gw_unregister_filter(gwj); + cgw_unregister_filter(gwj); kfree(gwj); } } - spin_unlock(&can_gw_list_lock); + spin_unlock(&cgw_list_lock); } return NOTIFY_DONE; } -static int gw_put_job(struct sk_buff *skb, struct gw_job *gwj) +static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj) { - struct { - struct can_frame cf; - u8 modtype; - } __attribute__((packed)) mb; - + struct cgw_frame_mod mb; struct rtcanmsg *rtcan; struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0); if (!nlh) @@ -270,17 +324,11 @@ static int gw_put_job(struct sk_buff *skb, struct gw_job *gwj) rtcan = nlmsg_data(nlh); rtcan->can_family = AF_CAN; - rtcan->src_ifindex = gwj->src_dev->ifindex; - rtcan->dst_ifindex = gwj->dst_dev->ifindex; - rtcan->can_txflags = 0; - - if (gwj->flags & CAN_TX_ECHO) - rtcan->can_txflags |= CAN_GW_TXFLAGS_ECHO; + rtcan->gwtype = gwj->gwtype; + rtcan->flags = gwj->flags; - if (gwj->flags & CAN_TX_SRC_TSTAMP) - rtcan->can_txflags |= CAN_GW_TXFLAGS_SRC_TSTAMP; + /* add statistics if available */ - /* check non default settings of attributes */ if (gwj->handled_frames) { if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0) goto cancel; @@ -295,51 +343,84 @@ static int gw_put_job(struct sk_buff *skb, struct gw_job *gwj) nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32)); } - if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) { - if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter), - &gwj->ccgw.filter) < 0) - goto cancel; - else - nlh->nlmsg_len += NLA_HDRLEN + - NLA_ALIGN(sizeof(struct can_filter)); - } + /* check non default settings of attributes */ - if (gwj->ccgw.modtype.and) { - memcpy(&mb.cf, &gwj->ccgw.modframe.and, sizeof(mb.cf)); - mb.modtype = gwj->ccgw.modtype.and; + if (gwj->mod.modtype.and) { + memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf)); + mb.modtype = gwj->mod.modtype.and; if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0) goto cancel; else nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb)); } - if (gwj->ccgw.modtype.or) { - memcpy(&mb.cf, &gwj->ccgw.modframe.or, sizeof(mb.cf)); - mb.modtype = gwj->ccgw.modtype.or; + if (gwj->mod.modtype.or) { + memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf)); + mb.modtype = gwj->mod.modtype.or; if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0) goto cancel; else nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb)); } - if (gwj->ccgw.modtype.xor) { - memcpy(&mb.cf, &gwj->ccgw.modframe.xor, sizeof(mb.cf)); - mb.modtype = gwj->ccgw.modtype.xor; + if (gwj->mod.modtype.xor) { + memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf)); + mb.modtype = gwj->mod.modtype.xor; if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0) goto cancel; else nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb)); } - if (gwj->ccgw.modtype.set) { - memcpy(&mb.cf, &gwj->ccgw.modframe.set, sizeof(mb.cf)); - mb.modtype = gwj->ccgw.modtype.set; + if (gwj->mod.modtype.set) { + memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf)); + mb.modtype = gwj->mod.modtype.set; if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0) goto cancel; else nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb)); } + if (gwj->mod.csum.xor.from_idx != CGW_CS_DISABLED) { + if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN, + &gwj->mod.csum.xor) < 0) + goto cancel; + else + nlh->nlmsg_len += NLA_HDRLEN + \ + NLA_ALIGN(CGW_CS_XOR_LEN); + } + + if (gwj->mod.csum.crc8.from_idx != CGW_CS_DISABLED) { + if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN, + &gwj->mod.csum.crc8) < 0) + goto cancel; + else + nlh->nlmsg_len += NLA_HDRLEN + \ + NLA_ALIGN(CGW_CS_CRC8_LEN); + } + + if (gwj->gwtype == CGW_TYPE_CAN_CAN) { + + if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) { + if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter), + &gwj->ccgw.filter) < 0) + goto cancel; + else + nlh->nlmsg_len += NLA_HDRLEN + + NLA_ALIGN(sizeof(struct can_filter)); + } + + if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0) + goto cancel; + else + nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32)); + + if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0) + goto cancel; + else + nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32)); + } + return skb->len; cancel: @@ -348,324 +429,371 @@ cancel: } /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */ -static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb) +static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb) { - struct gw_job *gwj = NULL; + struct cgw_job *gwj = NULL; struct hlist_node *n; int idx = 0; - int ret = 0; + int s_idx = cb->args[0]; rcu_read_lock(); - hlist_for_each_entry_rcu(gwj, n, &can_gw_list, list) { - if (idx >= cb->args[0]) { - ret = gw_put_job(skb, gwj); - if (ret > 0) - cb->args[0]++; + hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) { + if (idx < s_idx) + goto cont; + + if (cgw_put_job(skb, gwj) < 0) break; - } +cont: idx++; } rcu_read_unlock(); - return ret; + cb->args[0] = idx; + + return skb->len; } -/* check for attributes / filters for the CAN->CAN gateway */ -static int can_can_parse_attr(struct nlmsghdr *nlh, struct can_can_gw *ccgw) +/* check for common and gwtype specific attributes */ +static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod, + u8 gwtype, void *gwtypeattr) { struct nlattr *tb[CGW_MAX+1]; + struct cgw_frame_mod mb; int modidx = 0; int err = 0; - struct { - struct can_frame cf; - u8 modtype; - } __attribute__((packed)) mb; - - BUILD_BUG_ON(sizeof(mb) != CGW_MODATTR_LEN); - - memset(ccgw, 0, sizeof(*ccgw)); + /* initialize modification & checksum data space */ + memset(mod, 0, sizeof(*mod)); + mod->csum.xor.from_idx = CGW_CS_DISABLED; + mod->csum.crc8.from_idx = CGW_CS_DISABLED; err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL); if (err < 0) return err; - /* check for can_filter in attributes */ - if (tb[CGW_FILTER] && - nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter)) - nla_memcpy(&ccgw->filter, tb[CGW_FILTER], - sizeof(struct can_filter)); - /* check for AND/OR/XOR/SET modifications */ + if (tb[CGW_MOD_AND] && nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) { nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN); - canframecpy(&ccgw->modframe.and, &mb.cf); - ccgw->modtype.and = mb.modtype; + canframecpy(&mod->modframe.and, &mb.cf); + mod->modtype.and = mb.modtype; if (mb.modtype & CGW_MOD_ID) - ccgw->modfunc[modidx++] = mod_and_id; + mod->modfunc[modidx++] = mod_and_id; if (mb.modtype & CGW_MOD_DLC) - ccgw->modfunc[modidx++] = mod_and_dlc; + mod->modfunc[modidx++] = mod_and_dlc; if (mb.modtype & CGW_MOD_DATA) - ccgw->modfunc[modidx++] = mod_and_data; + mod->modfunc[modidx++] = mod_and_data; } if (tb[CGW_MOD_OR] && nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) { nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN); - canframecpy(&ccgw->modframe.or, &mb.cf); - ccgw->modtype.or = mb.modtype; + canframecpy(&mod->modframe.or, &mb.cf); + mod->modtype.or = mb.modtype; if (mb.modtype & CGW_MOD_ID) - ccgw->modfunc[modidx++] = mod_or_id; + mod->modfunc[modidx++] = mod_or_id; if (mb.modtype & CGW_MOD_DLC) - ccgw->modfunc[modidx++] = mod_or_dlc; + mod->modfunc[modidx++] = mod_or_dlc; if (mb.modtype & CGW_MOD_DATA) - ccgw->modfunc[modidx++] = mod_or_data; + mod->modfunc[modidx++] = mod_or_data; } if (tb[CGW_MOD_XOR] && nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) { nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN); - canframecpy(&ccgw->modframe.xor, &mb.cf); - ccgw->modtype.xor = mb.modtype; + canframecpy(&mod->modframe.xor, &mb.cf); + mod->modtype.xor = mb.modtype; if (mb.modtype & CGW_MOD_ID) - ccgw->modfunc[modidx++] = mod_xor_id; + mod->modfunc[modidx++] = mod_xor_id; if (mb.modtype & CGW_MOD_DLC) - ccgw->modfunc[modidx++] = mod_xor_dlc; + mod->modfunc[modidx++] = mod_xor_dlc; if (mb.modtype & CGW_MOD_DATA) - ccgw->modfunc[modidx++] = mod_xor_data; + mod->modfunc[modidx++] = mod_xor_data; } if (tb[CGW_MOD_SET] && nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) { nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN); - canframecpy(&ccgw->modframe.set, &mb.cf); - ccgw->modtype.set = mb.modtype; + canframecpy(&mod->modframe.set, &mb.cf); + mod->modtype.set = mb.modtype; if (mb.modtype & CGW_MOD_ID) - ccgw->modfunc[modidx++] = mod_set_id; + mod->modfunc[modidx++] = mod_set_id; if (mb.modtype & CGW_MOD_DLC) - ccgw->modfunc[modidx++] = mod_set_dlc; + mod->modfunc[modidx++] = mod_set_dlc; if (mb.modtype & CGW_MOD_DATA) - ccgw->modfunc[modidx++] = mod_set_data; + mod->modfunc[modidx++] = mod_set_data; + } + + /* check for checksum operations after CAN frame modifications */ + if (modidx) { + + if (tb[CGW_CS_XOR] && + nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) { + nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR], + CGW_CS_XOR_LEN); + err = cgw_chk_csum_parms(mod->csum.xor.from_idx, + mod->csum.xor.to_idx, + mod->csum.xor.result_idx); + if (err) + return err; + } + + if (tb[CGW_CS_CRC8] && + nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) { + nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8], + CGW_CS_CRC8_LEN); + err = cgw_chk_csum_parms(mod->csum.crc8.from_idx, + mod->csum.crc8.to_idx, + mod->csum.crc8.result_idx); + if (err) + return err; + } + } + + if (gwtype == CGW_TYPE_CAN_CAN) { + + /* check CGW_TYPE_CAN_CAN specific attributes */ + + struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr; + memset(ccgw, 0, sizeof(*ccgw)); + + /* check for can_filter in attributes */ + if (tb[CGW_FILTER] && + nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter)) + nla_memcpy(&ccgw->filter, tb[CGW_FILTER], + sizeof(struct can_filter)); + + err = -ENODEV; + + /* specifying two interfaces is mandatory */ + if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF]) + return err; + + if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32)) + nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF], + sizeof(u32)); + + if (nla_len(tb[CGW_DST_IF]) == sizeof(u32)) + nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF], + sizeof(u32)); + + /* both indices set to 0 for flushing all routing entries */ + if (!ccgw->src_idx && !ccgw->dst_idx) + return 0; + + /* only one index set to 0 is an error */ + if (!ccgw->src_idx || !ccgw->dst_idx) + return err; } + /* add the checks for other gwtypes here */ + return 0; } -static int gw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) +static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, + void *arg) { struct rtcanmsg *r; - struct gw_job *gwj; + struct cgw_job *gwj; int err = 0; if (nlmsg_len(nlh) < sizeof(*r)) - return -EINVAL; + return -EINVAL; - r = nlmsg_data(nlh); - if (r->can_family != AF_CAN) - return -EPFNOSUPPORT; + r = nlmsg_data(nlh); + if (r->can_family != AF_CAN) + return -EPFNOSUPPORT; - gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL); + /* so far we only support CAN -> CAN routings */ + if (r->gwtype != CGW_TYPE_CAN_CAN) + return -EINVAL; + + gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL); if (!gwj) return -ENOMEM; - gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex); - if (!gwj->src_dev) { - err = -ENODEV; - goto fail; - } + gwj->handled_frames = 0; + gwj->dropped_frames = 0; + gwj->flags = r->flags; + gwj->gwtype = r->gwtype; - /* for now the source device needs to be a CAN device */ - if (gwj->src_dev->type != ARPHRD_CAN) { - err = -ENODEV; - goto put_src_fail; - } + err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw); + if (err < 0) + goto out; - gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex); - if (!gwj->dst_dev) { - err = -ENODEV; - goto put_src_fail; - } + err = -ENODEV; - /* for now the destination device needs to be a CAN device */ - if (gwj->dst_dev->type != ARPHRD_CAN) { - err = -ENODEV; - goto put_src_dst_fail; - } + /* ifindex == 0 is not allowed for job creation */ + if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx) + goto out; - gwj->handled_frames = 0; - gwj->dropped_frames = 0; - gwj->flags = 0; + gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx); - if (r->can_txflags & CAN_GW_TXFLAGS_ECHO) - gwj->flags |= CAN_TX_ECHO; + if (!gwj->src.dev) + goto out; - if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP) - gwj->flags |= CAN_TX_SRC_TSTAMP; + if (gwj->src.dev->type != ARPHRD_CAN) + goto put_src_out; - err = can_can_parse_attr(nlh, &gwj->ccgw); - if (err < 0) - goto put_src_dst_fail; + gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx); - spin_lock(&can_gw_list_lock); + if (!gwj->dst.dev) + goto put_src_out; - err = can_gw_register_filter(gwj); + if (gwj->dst.dev->type != ARPHRD_CAN) + goto put_src_dst_out; + + spin_lock(&cgw_list_lock); + + err = cgw_register_filter(gwj); if (!err) - hlist_add_head_rcu(&gwj->list, &can_gw_list); + hlist_add_head_rcu(&gwj->list, &cgw_list); - spin_unlock(&can_gw_list_lock); + spin_unlock(&cgw_list_lock); - dev_put(gwj->src_dev); - dev_put(gwj->dst_dev); - +put_src_dst_out: + dev_put(gwj->dst.dev); +put_src_out: + dev_put(gwj->src.dev); +out: if (err) - goto fail; + kmem_cache_free(cgw_cache, gwj); - return 0; - -put_src_dst_fail: - dev_put(gwj->dst_dev); -put_src_fail: - dev_put(gwj->src_dev); -fail: - kmem_cache_free(gw_cache, gwj); return err; } -static void gw_remove_all_jobs(void) +static void cgw_remove_all_jobs(void) { - struct gw_job *gwj = NULL; + struct cgw_job *gwj = NULL; struct hlist_node *n, *nx; - spin_lock(&can_gw_list_lock); + spin_lock(&cgw_list_lock); - hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) { + hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) { hlist_del(&gwj->list); - can_gw_unregister_filter(gwj); + cgw_unregister_filter(gwj); kfree(gwj); } - spin_unlock(&can_gw_list_lock); + spin_unlock(&cgw_list_lock); } -static int gw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) +static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) { - struct gw_job *gwj = NULL; + struct cgw_job *gwj = NULL; struct hlist_node *n, *nx; struct rtcanmsg *r; + struct cf_mod mod; struct can_can_gw ccgw; - u32 flags = 0; int err = 0; if (nlmsg_len(nlh) < sizeof(*r)) - return -EINVAL; + return -EINVAL; - r = nlmsg_data(nlh); - if (r->can_family != AF_CAN) - return -EPFNOSUPPORT; + r = nlmsg_data(nlh); + if (r->can_family != AF_CAN) + return -EPFNOSUPPORT; - /* if_index set to 0 => remove all entries */ - if (!r->src_ifindex && !r->dst_ifindex) { - gw_remove_all_jobs(); - return 0; - } + /* so far we only support CAN -> CAN routings */ + if (r->gwtype != CGW_TYPE_CAN_CAN) + return -EINVAL; - if (r->can_txflags & CAN_GW_TXFLAGS_ECHO) - flags |= CAN_TX_ECHO; - - if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP) - flags |= CAN_TX_SRC_TSTAMP; - - err = can_can_parse_attr(nlh, &ccgw); + err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw); if (err < 0) return err; + /* two interface indices both set to 0 => remove all entries */ + if (!ccgw.src_idx && !ccgw.dst_idx) { + cgw_remove_all_jobs(); + return 0; + } + err = -EINVAL; - spin_lock(&can_gw_list_lock); + spin_lock(&cgw_list_lock); /* remove only the first matching entry */ - hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) { - - if (gwj->dst_dev->ifindex != r->dst_ifindex) - continue; + hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) { - if (gwj->src_dev->ifindex != r->src_ifindex) + if (gwj->flags != r->flags) continue; - if (gwj->flags != flags) + if (memcmp(&gwj->mod, &mod, sizeof(mod))) continue; + /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */ if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw))) continue; hlist_del(&gwj->list); - can_gw_unregister_filter(gwj); + cgw_unregister_filter(gwj); kfree(gwj); err = 0; break; } - spin_unlock(&can_gw_list_lock); + spin_unlock(&cgw_list_lock); return err; } -static __init int gw_module_init(void) +static __init int cgw_module_init(void) { printk(banner); - gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job), + cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job), 0, 0, NULL); - if (!gw_cache) + if (!cgw_cache) return -ENOMEM; /* set notifier */ - notifier.notifier_call = gw_notifier; + notifier.notifier_call = cgw_notifier; register_netdevice_notifier(¬ifier); - if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) { + if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs)) { unregister_netdevice_notifier(¬ifier); - kmem_cache_destroy(gw_cache); + kmem_cache_destroy(cgw_cache); return -ENOBUFS; } /* Only the first call to __rtnl_register can fail */ - __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL); - __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL); + __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL); + __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL); return 0; } -static __exit void gw_module_exit(void) +static __exit void cgw_module_exit(void) { rtnl_unregister_all(PF_CAN); unregister_netdevice_notifier(¬ifier); - gw_remove_all_jobs(); + cgw_remove_all_jobs(); rcu_barrier(); /* Wait for completion of call_rcu()'s */ - kmem_cache_destroy(gw_cache); + kmem_cache_destroy(cgw_cache); } -module_init(gw_module_init); -module_exit(gw_module_exit); +module_init(cgw_module_init); +module_exit(cgw_module_exit); -- 2.39.2