]> rtime.felk.cvut.cz Git - socketcan-devel.git/blobdiff - kernel/2.6/net/can/gw.c
Beautify check for valid CAN netdevices.
[socketcan-devel.git] / kernel / 2.6 / net / can / gw.c
index baef6f11329553b97870539a034024b20b8c0330..d7257ccbcb1439336914242d8991f5e9a4512659 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
  *
- * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
+ * Copyright (c) 2011 Volkswagen Group Electronic Research
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -48,6 +48,7 @@
 #include <linux/list.h>
 #include <linux/spinlock.h>
 #include <linux/rcupdate.h>
+#include <linux/rculist.h>
 #include <linux/net.h>
 #include <linux/netdevice.h>
 #include <linux/if_arp.h>
 #include <socketcan/can/gw.h>
 #include <net/rtnetlink.h>
 #include <net/net_namespace.h>
+#include <net/sock.h>
 
 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
 RCSID("$Id$");
 
-#define CAN_GW_VERSION "20100218"
+#define CAN_GW_VERSION "20101209"
 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 <oliver.hartkopp@volkswagen.de>");
+MODULE_ALIAS("can-gw");
 
-HLIST_HEAD(can_gw_list);
-static DEFINE_SPINLOCK(can_gw_list_lock);
+HLIST_HEAD(cgw_list);
 static struct notifier_block notifier;
 
-static struct kmem_cache *gw_cache __read_mostly;
+static struct kmem_cache *cgw_cache __read_mostly;
+
+/* structure that contains the (on-the-fly) CAN frame modifications */
+struct cf_mod {
+       struct {
+               struct can_frame and;
+               struct can_frame or;
+               struct can_frame xor;
+               struct can_frame set;
+       } modframe;
+       struct {
+               u8 and;
+               u8 or;
+               u8 xor;
+               u8 set;
+       } modtype;
+       void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
+                                         struct cf_mod *mod);
+
+       /* CAN frame checksum calculation after CAN frame modifications */
+       struct {
+               struct cgw_csum_xor xor;
+               struct cgw_csum_crc8 crc8;
+       } csum;
+       struct {
+               void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
+               void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
+       } csumfunc;
+};
 
-#define GW_SK_MAGIC ((void *)(&notifier))
 
 /*
  * So far we just support CAN -> CAN routing and frame modifications.
  *
- * The internal can_can_gw structure contains optional attributes for
+ * The internal can_can_gw structure contains data and attributes for
  * a CAN -> CAN gateway job.
  */
 struct can_can_gw {
        struct can_filter filter;
-       struct {
-               struct can_frame and;
-               struct can_frame or;
-               struct can_frame xor;
-               struct can_frame set;
-       } modframe;
-       void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
-                                         struct can_can_gw *mod);
+       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 gwjob.flags */
-#define CAN_TX_LOOPBACK 0x00000001
-#define CAN_TX_SRC_TSTAMP 0x00000002
+/* 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 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)
+MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
+MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
+MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
+MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
+MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
+MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
+MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
+MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
+MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
+MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
+
+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 cf_mod.
+        */
 
-/* modification functions that are invoked in the hot path in gw_rcv */
-void mod_and_id (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_id &= mod->modframe.and.can_id;
-}
-void mod_and_dlc (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_dlc &= mod->modframe.and.can_dlc;
-}
-void mod_and_data (struct can_frame *cf, struct can_can_gw *mod) {
-       *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data;
+       dst->can_id = src->can_id;
+       dst->can_dlc = src->can_dlc;
+       *(u64 *)dst->data = *(u64 *)src->data;
 }
-void mod_or_id (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_id |= mod->modframe.or.can_id;
-}
-void mod_or_dlc (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_dlc |= mod->modframe.or.can_dlc;
-}
-void mod_or_data (struct can_frame *cf, struct can_can_gw *mod) {
-       *(u64 *)cf->data |= *(u64 *)mod->modframe.or.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;
 }
-void mod_xor_id (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_id ^= mod->modframe.xor.can_id;
+
+static inline int calc_idx(int idx, int rx_dlc)
+{
+       if (idx < 0)
+               return rx_dlc + idx;
+       else
+               return idx;
 }
-void mod_xor_dlc (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_dlc ^= mod->modframe.xor.can_dlc;
+
+static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
+{
+       int from = calc_idx(xor->from_idx, cf->can_dlc);
+       int to = calc_idx(xor->to_idx, cf->can_dlc);
+       int res = calc_idx(xor->result_idx, cf->can_dlc);
+       u8 val = xor->init_xor_val;
+       int i;
+
+       if (from < 0 || to < 0 || res < 0)
+               return;
+
+       if (from <= to) {
+               for (i = from; i <= to; i++)
+                       val ^= cf->data[i];
+       } else {
+               for (i = from; i >= to; i--)
+                       val ^= cf->data[i];
+       }
+
+       cf->data[res] = val;
 }
-void mod_xor_data (struct can_frame *cf, struct can_can_gw *mod) {
-       *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data;
+
+static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
+{
+       u8 val = xor->init_xor_val;
+       int i;
+
+       for (i = xor->from_idx; i <= xor->to_idx; i++)
+               val ^= cf->data[i];
+
+       cf->data[xor->result_idx] = val;
 }
-void mod_set_id (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_id = mod->modframe.set.can_id;
+
+static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
+{
+       u8 val = xor->init_xor_val;
+       int i;
+
+       for (i = xor->from_idx; i >= xor->to_idx; i--)
+               val ^= cf->data[i];
+
+       cf->data[xor->result_idx] = val;
 }
-void mod_set_dlc (struct can_frame *cf, struct can_can_gw *mod) {
-       cf->can_dlc = mod->modframe.set.can_dlc;
+
+static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
+{
+       int from = calc_idx(crc8->from_idx, cf->can_dlc);
+       int to = calc_idx(crc8->to_idx, cf->can_dlc);
+       int res = calc_idx(crc8->result_idx, cf->can_dlc);
+       u8 crc = crc8->init_crc_val;
+       int i;
+
+       if (from < 0 || to < 0 || res < 0)
+               return;
+
+       if (from <= to) {
+               for (i = crc8->from_idx; i <= crc8->to_idx; i++)
+                       crc = crc8->crctab[crc^cf->data[i]];
+       } else {
+               for (i = crc8->from_idx; i >= crc8->to_idx; i--)
+                       crc = crc8->crctab[crc^cf->data[i]];
+       }
+
+       switch (crc8->profile) {
+
+       case CGW_CRC8PRF_1U8:
+               crc = crc8->crctab[crc^crc8->profile_data[0]];
+               break;
+
+       case  CGW_CRC8PRF_16U8:
+               crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
+               break;
+
+       case CGW_CRC8PRF_SFFID_XOR:
+               crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
+                                  (cf->can_id >> 8 & 0xFF)];
+               break;
+
+       }
+
+       cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
 }
-void mod_set_data (struct can_frame *cf, struct can_can_gw *mod) {
-       *(u64 *)cf->data = *(u64 *)mod->modframe.set.data;
+
+static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
+{
+       u8 crc = crc8->init_crc_val;
+       int i;
+
+       for (i = crc8->from_idx; i <= crc8->to_idx; i++)
+               crc = crc8->crctab[crc^cf->data[i]];
+
+       switch (crc8->profile) {
+
+       case CGW_CRC8PRF_1U8:
+               crc = crc8->crctab[crc^crc8->profile_data[0]];
+               break;
+
+       case  CGW_CRC8PRF_16U8:
+               crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
+               break;
+
+       case CGW_CRC8PRF_SFFID_XOR:
+               crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
+                                  (cf->can_id >> 8 & 0xFF)];
+               break;
+       }
+
+       cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
 }
 
+static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
+{
+       u8 crc = crc8->init_crc_val;
+       int i;
+
+       for (i = crc8->from_idx; i >= crc8->to_idx; i--)
+               crc = crc8->crctab[crc^cf->data[i]];
+
+       switch (crc8->profile) {
+
+       case CGW_CRC8PRF_1U8:
+               crc = crc8->crctab[crc^crc8->profile_data[0]];
+               break;
+
+       case  CGW_CRC8PRF_16U8:
+               crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
+               break;
+
+       case CGW_CRC8PRF_SFFID_XOR:
+               crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
+                                  (cf->can_id >> 8 & 0xFF)];
+               break;
+       }
+
+       cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
+}
 
 /* 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)
+       /* do not handle already routed frames - see comment below */
+       if (skb_mac_header_was_set(skb))
                return;
 
-       if (!netif_running(gwj->dst_dev)) {
+       if (!(gwj->dst.dev->flags & IFF_UP)) {
                gwj->dropped_frames++;
                return;
        }
@@ -176,7 +359,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,41 +369,58 @@ static void gw_rcv(struct sk_buff *skb, void *data)
                return;
        }
 
-       /* mark routed frames with a 'special' sk value */
-       nskb->sk = GW_SK_MAGIC;
-       nskb->dev = gwj->dst_dev;
+       /*
+        * Mark routed frames by setting some mac header length which is
+        * not relevant for the CAN frames located in the skb->data section.
+        *
+        * As dev->header_ops is not set in CAN netdevices no one is ever
+        * accessing the various header offsets in the CAN skbuffs anyway.
+        * E.g. using the packet socket to read CAN frames is still working.
+        */
+       skb_set_mac_header(nskb, 8);
+       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.csumfunc.crc8)
+                       (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
+
+               if (gwj->mod.csumfunc.xor)
+                       (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
+       }
 
        /* 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_LOOPBACK))
+       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;
@@ -237,257 +437,532 @@ 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);
+               ASSERT_RTNL();
 
-               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);
        }
 
        return NOTIFY_DONE;
 }
 
-/*
- * Dump information about all ports, in response to GETROUTE
- */
-static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
-{
-       printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
-
-       return 0;
-}
-
-static int gw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
+static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
 {
+       struct cgw_frame_mod mb;
+       struct rtcanmsg *rtcan;
+       struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
+       if (!nlh)
+               return -EMSGSIZE;
+
+       rtcan = nlmsg_data(nlh);
+       rtcan->can_family = AF_CAN;
+       rtcan->gwtype = gwj->gwtype;
+       rtcan->flags = gwj->flags;
+
+       /* add statistics if available */
+
+       if (gwj->handled_frames) {
+               if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
+                       goto cancel;
+               else
+                       nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
+       }
 
-       struct rtcanmsg *r;
-       struct nlattr *tb[CGW_MAX+1];
-       struct gw_job *gwj;
-       u8 buf[CGW_MODATTR_LEN];
-       int modidx = 0;
-       int err = 0;
+       if (gwj->dropped_frames) {
+               if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
+                       goto cancel;
+               else
+                       nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
+       }
 
-       printk(KERN_INFO "%s: len %d attrlen %d\n", __FUNCTION__,
-              nlmsg_len(nlh), nlmsg_attrlen(nlh, sizeof(*r)));
+       /* check non default settings of attributes */
 
-       if (nlmsg_len(nlh) < sizeof(*r))
-                return -EINVAL;
+       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));
+       }
 
-        r = nlmsg_data(nlh);
-        if (r->can_family != AF_CAN)
-                return -EPFNOSUPPORT;
+       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));
+       }
 
-       gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
-       if (!gwj)
-               return -ENOMEM;
+       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));
+       }
 
-       gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
-       if (!gwj->src_dev) {
-               err = -ENODEV;
-               goto fail;
+       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));
        }
 
-       /* for now the source device needs to be a CAN device */
-       if (gwj->src_dev->type != ARPHRD_CAN) {
-               err = -ENODEV;
-               goto put_src_fail;
+       if (gwj->mod.csumfunc.crc8) {
+               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);
        }
 
-       gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
-       if (!gwj->dst_dev) {
-               err = -ENODEV;
-               goto put_src_fail;
+       if (gwj->mod.csumfunc.xor) {
+               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);
        }
 
-       /* 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;
+       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));
        }
 
-       gwj->flags = 0;
+       return skb->len;
 
-       if (r->can_txflags & CAN_GW_TXFLAGS_LOOPBACK)
-               gwj->flags |= CAN_TX_LOOPBACK;
+cancel:
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
+}
 
-       if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
-               gwj->flags |= CAN_TX_SRC_TSTAMP;
+/* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
+static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       struct cgw_job *gwj = NULL;
+       struct hlist_node *n;
+       int idx = 0;
+       int s_idx = cb->args[0];
+
+       rcu_read_lock();
+       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();
 
-       memset(&gwj->ccgw, 0, sizeof(gwj->ccgw)); 
+       cb->args[0] = idx;
 
-       /* check for additional attributes / filters here */
+       return skb->len;
+}
 
-       err = nlmsg_parse(nlh, sizeof(*r), tb, CGW_MAX, NULL);
-       if (err < 0)
-               goto put_src_dst_fail;
+/* 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;
 
-       /* check for can_filter in attributes */
-       if (tb[CGW_FILTER] &&
-           nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
-               nla_memcpy(&gwj->ccgw.filter, tb[CGW_FILTER],
-                          sizeof(struct can_filter));
+       /* initialize modification & checksum data space */
+       memset(mod, 0, sizeof(*mod));
+
+       err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
+       if (err < 0)
+               return err;
 
        /* check for AND/OR/XOR/SET modifications */
+
        if (tb[CGW_MOD_AND] &&
            nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
-               nla_memcpy(&buf, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
+               nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
 
-               memcpy(&gwj->ccgw.modframe.and, buf,
-                      sizeof(struct can_frame));
+               canframecpy(&mod->modframe.and, &mb.cf);
+               mod->modtype.and = mb.modtype;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_ID)
-                       gwj->ccgw.modfunc[modidx++] = mod_and_id;
+               if (mb.modtype & CGW_MOD_ID)
+                       mod->modfunc[modidx++] = mod_and_id;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DLC)
-                       gwj->ccgw.modfunc[modidx++] = mod_and_dlc;
+               if (mb.modtype & CGW_MOD_DLC)
+                       mod->modfunc[modidx++] = mod_and_dlc;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DATA)
-                       gwj->ccgw.modfunc[modidx++] = mod_and_data;
+               if (mb.modtype & CGW_MOD_DATA)
+                       mod->modfunc[modidx++] = mod_and_data;
        }
 
        if (tb[CGW_MOD_OR] &&
            nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
-               nla_memcpy(&buf, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
+               nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
 
-               memcpy(&gwj->ccgw.modframe.or, buf,
-                      sizeof(struct can_frame));
+               canframecpy(&mod->modframe.or, &mb.cf);
+               mod->modtype.or = mb.modtype;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_ID)
-                       gwj->ccgw.modfunc[modidx++] = mod_or_id;
+               if (mb.modtype & CGW_MOD_ID)
+                       mod->modfunc[modidx++] = mod_or_id;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DLC)
-                       gwj->ccgw.modfunc[modidx++] = mod_or_dlc;
+               if (mb.modtype & CGW_MOD_DLC)
+                       mod->modfunc[modidx++] = mod_or_dlc;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DATA)
-                       gwj->ccgw.modfunc[modidx++] = mod_or_data;
+               if (mb.modtype & CGW_MOD_DATA)
+                       mod->modfunc[modidx++] = mod_or_data;
        }
 
        if (tb[CGW_MOD_XOR] &&
            nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
-               nla_memcpy(&buf, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
+               nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
 
-               memcpy(&gwj->ccgw.modframe.xor, buf,
-                      sizeof(struct can_frame));
+               canframecpy(&mod->modframe.xor, &mb.cf);
+               mod->modtype.xor = mb.modtype;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_ID)
-                       gwj->ccgw.modfunc[modidx++] = mod_xor_id;
+               if (mb.modtype & CGW_MOD_ID)
+                       mod->modfunc[modidx++] = mod_xor_id;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DLC)
-                       gwj->ccgw.modfunc[modidx++] = mod_xor_dlc;
+               if (mb.modtype & CGW_MOD_DLC)
+                       mod->modfunc[modidx++] = mod_xor_dlc;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DATA)
-                       gwj->ccgw.modfunc[modidx++] = mod_xor_data;
+               if (mb.modtype & CGW_MOD_DATA)
+                       mod->modfunc[modidx++] = mod_xor_data;
        }
 
        if (tb[CGW_MOD_SET] &&
            nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
-               nla_memcpy(&buf, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
+               nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
 
-               memcpy(&gwj->ccgw.modframe.set, buf,
-                      sizeof(struct can_frame));
+               canframecpy(&mod->modframe.set, &mb.cf);
+               mod->modtype.set = mb.modtype;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_ID)
-                       gwj->ccgw.modfunc[modidx++] = mod_set_id;
+               if (mb.modtype & CGW_MOD_ID)
+                       mod->modfunc[modidx++] = mod_set_id;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DLC)
-                       gwj->ccgw.modfunc[modidx++] = mod_set_dlc;
+               if (mb.modtype & CGW_MOD_DLC)
+                       mod->modfunc[modidx++] = mod_set_dlc;
 
-               if (buf[sizeof(struct can_frame)] & CGW_MOD_DATA)
-                       gwj->ccgw.modfunc[modidx++] = mod_set_data;
+               if (mb.modtype & CGW_MOD_DATA)
+                       mod->modfunc[modidx++] = mod_set_data;
        }
 
-       spin_lock(&can_gw_list_lock);
+       /* check for checksum operations after CAN frame modifications */
+       if (modidx) {
+
+               if (tb[CGW_CS_CRC8] &&
+                   nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
+
+                       struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
+                               nla_data(tb[CGW_CS_CRC8]);
+
+                       err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
+                                                c->result_idx);
+                       if (err)
+                               return err;
+
+                       nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
+                                  CGW_CS_CRC8_LEN);
+
+                       /*
+                        * select dedicated processing function to reduce
+                        * runtime operations in receive hot path.
+                        */
+                       if (c->from_idx < 0 || c->to_idx < 0 ||
+                           c->result_idx < 0)
+                               mod->csumfunc.crc8 = cgw_csum_crc8_rel;
+                       else if (c->from_idx <= c->to_idx)
+                               mod->csumfunc.crc8 = cgw_csum_crc8_pos;
+                       else
+                               mod->csumfunc.crc8 = cgw_csum_crc8_neg;
+               }
 
-       err = can_gw_register_filter(gwj);
-       if (!err)
-               hlist_add_head_rcu(&gwj->list, &can_gw_list);
+               if (tb[CGW_CS_XOR] &&
+                   nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
+
+                       struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
+                               nla_data(tb[CGW_CS_XOR]);
+
+                       err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
+                                                c->result_idx);
+                       if (err)
+                               return err;
+
+                       nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
+                                  CGW_CS_XOR_LEN);
+
+                       /*
+                        * select dedicated processing function to reduce
+                        * runtime operations in receive hot path.
+                        */
+                       if (c->from_idx < 0 || c->to_idx < 0 ||
+                           c->result_idx < 0)
+                               mod->csumfunc.xor = cgw_csum_xor_rel;
+                       else if (c->from_idx <= c->to_idx)
+                               mod->csumfunc.xor = cgw_csum_xor_pos;
+                       else
+                               mod->csumfunc.xor = cgw_csum_xor_neg;
+               }
+       }
 
-       spin_unlock(&can_gw_list_lock);
-       
-       dev_put(gwj->src_dev);
-       dev_put(gwj->dst_dev);
+       if (gwtype == CGW_TYPE_CAN_CAN) {
 
-       if (err)
-               goto fail;
+               /* 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 cgw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh,
+                         void *arg)
+{
+       struct rtcanmsg *r;
+       struct cgw_job *gwj;
+       int err = 0;
+
+       if (nlmsg_len(nlh) < sizeof(*r))
+               return -EINVAL;
+
+       r = nlmsg_data(nlh);
+       if (r->can_family != AF_CAN)
+               return -EPFNOSUPPORT;
+
+       /* 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->handled_frames = 0;
+       gwj->dropped_frames = 0;
+       gwj->flags = r->flags;
+       gwj->gwtype = r->gwtype;
+
+       err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
+       if (err < 0)
+               goto out;
+
+       err = -ENODEV;
+
+       /* ifindex == 0 is not allowed for job creation */
+       if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
+               goto out;
+
+       gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
+
+       if (!gwj->src.dev)
+               goto out;
+
+       /* check for CAN netdev not using header_ops - see gw_rcv() */
+       if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
+               goto put_src_out;
+
+       gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
+
+       if (!gwj->dst.dev)
+               goto put_src_out;
+
+       /* check for CAN netdev not using header_ops - see gw_rcv() */
+       if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
+               goto put_src_dst_out;
+
+       ASSERT_RTNL();
+
+       err = cgw_register_filter(gwj);
+       if (!err)
+               hlist_add_head_rcu(&gwj->list, &cgw_list);
+
+put_src_dst_out:
+       dev_put(gwj->dst.dev);
+put_src_out:
+       dev_put(gwj->src.dev);
+out:
+       if (err)
+               kmem_cache_free(cgw_cache, gwj);
 
-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 int gw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
+static void cgw_remove_all_jobs(void)
 {
-       printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
+       struct cgw_job *gwj = NULL;
+       struct hlist_node *n, *nx;
 
-       return 0;
+       ASSERT_RTNL();
+
+       hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
+               hlist_del(&gwj->list);
+               cgw_unregister_filter(gwj);
+               kfree(gwj);
+       }
 }
 
-static __init int gw_module_init(void)
+static int cgw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
+{
+       struct cgw_job *gwj = NULL;
+       struct hlist_node *n, *nx;
+       struct rtcanmsg *r;
+       struct cf_mod mod;
+       struct can_can_gw ccgw;
+       int err = 0;
+
+       if (nlmsg_len(nlh) < sizeof(*r))
+               return -EINVAL;
+
+       r = nlmsg_data(nlh);
+       if (r->can_family != AF_CAN)
+               return -EPFNOSUPPORT;
+
+       /* so far we only support CAN -> CAN routings */
+       if (r->gwtype != CGW_TYPE_CAN_CAN)
+               return -EINVAL;
+
+       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;
+
+       ASSERT_RTNL();
+
+       /* remove only the first matching entry */
+       hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
+
+               if (gwj->flags != r->flags)
+                       continue;
+
+               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);
+               cgw_unregister_filter(gwj);
+               kfree(gwj);
+               err = 0;
+               break;
+       }
+
+       return err;
+}
+
+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(&notifier);
 
-       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(&notifier);
-               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)
 {
-       struct gw_job *gwj = NULL;
-       struct hlist_node *n, *nx;
-
        rtnl_unregister_all(PF_CAN);
 
        unregister_netdevice_notifier(&notifier);
 
-       spin_lock(&can_gw_list_lock);
-
-       hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
-               hlist_del(&gwj->list);
-               can_gw_unregister_filter(gwj);
-               kfree(gwj);
-       }
-
-       spin_unlock(&can_gw_list_lock);
+       rtnl_lock();
+       cgw_remove_all_jobs();
+       rtnl_unlock();
 
        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);