2 * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
4 * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/types.h>
48 #include <linux/list.h>
49 #include <linux/spinlock.h>
50 #include <linux/rcupdate.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
55 #include <socketcan/can.h>
56 #include <socketcan/can/core.h>
57 #include <socketcan/can/gw.h>
58 #include <net/rtnetlink.h>
59 #include <net/net_namespace.h>
61 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
64 #define CAN_GW_VERSION "20100218"
65 static __initdata const char banner[] =
66 KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
68 MODULE_DESCRIPTION("PF_CAN netlink gateway");
69 MODULE_LICENSE("Dual BSD/GPL");
70 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72 HLIST_HEAD(can_gw_list);
73 static DEFINE_SPINLOCK(can_gw_list_lock);
74 static struct notifier_block notifier;
76 static struct kmem_cache *gw_cache __read_mostly;
78 #define GW_SK_MAGIC ((void *)(¬ifier))
81 * So far we just support CAN -> CAN routing and frame modifications.
83 * The internal can_can_gw structure contains optional attributes for
84 * a CAN -> CAN gateway job.
87 struct can_filter filter;
94 void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
95 struct can_can_gw *mod);
98 /* list entry for CAN gateways jobs */
100 struct hlist_node list;
102 struct net_device *src_dev;
103 struct net_device *dst_dev;
108 struct can_can_gw ccgw;
113 /* content of u32 gwjob.flags */
114 #define CAN_TX_ECHO 0x00000001
115 #define CAN_TX_SRC_TSTAMP 0x00000002
117 /* modification functions that are invoked in the hot path in gw_rcv */
118 void mod_and_id (struct can_frame *cf, struct can_can_gw *mod) {
119 cf->can_id &= mod->modframe.and.can_id;
121 void mod_and_dlc (struct can_frame *cf, struct can_can_gw *mod) {
122 cf->can_dlc &= mod->modframe.and.can_dlc;
124 void mod_and_data (struct can_frame *cf, struct can_can_gw *mod) {
125 *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data;
127 void mod_or_id (struct can_frame *cf, struct can_can_gw *mod) {
128 cf->can_id |= mod->modframe.or.can_id;
130 void mod_or_dlc (struct can_frame *cf, struct can_can_gw *mod) {
131 cf->can_dlc |= mod->modframe.or.can_dlc;
133 void mod_or_data (struct can_frame *cf, struct can_can_gw *mod) {
134 *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data;
136 void mod_xor_id (struct can_frame *cf, struct can_can_gw *mod) {
137 cf->can_id ^= mod->modframe.xor.can_id;
139 void mod_xor_dlc (struct can_frame *cf, struct can_can_gw *mod) {
140 cf->can_dlc ^= mod->modframe.xor.can_dlc;
142 void mod_xor_data (struct can_frame *cf, struct can_can_gw *mod) {
143 *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data;
145 void mod_set_id (struct can_frame *cf, struct can_can_gw *mod) {
146 cf->can_id = mod->modframe.set.can_id;
148 void mod_set_dlc (struct can_frame *cf, struct can_can_gw *mod) {
149 cf->can_dlc = mod->modframe.set.can_dlc;
151 void mod_set_data (struct can_frame *cf, struct can_can_gw *mod) {
152 *(u64 *)cf->data = *(u64 *)mod->modframe.set.data;
155 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
158 * Copy the struct members separately to ensure that no uninitialized
159 * data are copied in the 3 bytes hole of the struct. This is needed
160 * to make easy compares of the data in the struct can_can_gw.
163 dst->can_id = src->can_id;
164 dst->can_dlc = src->can_dlc;
165 *(u64 *)dst->data = *(u64 *)src->data;
168 /* the receive & process & send function */
169 static void gw_rcv(struct sk_buff *skb, void *data)
171 struct gw_job *gwj = (struct gw_job *)data;
172 struct can_frame *cf;
173 struct sk_buff *nskb;
176 /* do not handle already routed frames */
177 if (skb->sk == GW_SK_MAGIC)
180 if (!netif_running(gwj->dst_dev)) {
181 gwj->dropped_frames++;
186 * clone the given skb, which has not been done in can_rcv()
188 * When there is at least one modification function activated,
189 * we need to copy the skb as we want to modify skb->data.
191 if (gwj->ccgw.modfunc[0])
192 nskb = skb_copy(skb, GFP_ATOMIC);
194 nskb = skb_clone(skb, GFP_ATOMIC);
197 gwj->dropped_frames++;
201 /* mark routed frames with a 'special' sk value */
202 nskb->sk = GW_SK_MAGIC;
203 nskb->dev = gwj->dst_dev;
205 /* pointer to modifiable CAN frame */
206 cf = (struct can_frame *)nskb->data;
208 /* perform preprocessed modification functions if there are any */
209 while (modidx < MAX_MODFUNCTIONS && gwj->ccgw.modfunc[modidx])
210 (*gwj->ccgw.modfunc[modidx++])(cf, &gwj->ccgw);
212 /* clear the skb timestamp if not configured the other way */
213 if (!(gwj->flags & CAN_TX_SRC_TSTAMP))
214 nskb->tstamp.tv64 = 0;
216 /* send to netdevice */
217 if (can_send(nskb, gwj->flags & CAN_TX_ECHO))
218 gwj->dropped_frames++;
220 gwj->handled_frames++;
223 static inline int can_gw_register_filter(struct gw_job *gwj)
225 return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id,
226 gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw");
229 static inline void can_gw_unregister_filter(struct gw_job *gwj)
231 can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id,
232 gwj->ccgw.filter.can_mask, gw_rcv, gwj);
235 static int gw_notifier(struct notifier_block *nb,
236 unsigned long msg, void *data)
238 struct net_device *dev = (struct net_device *)data;
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
241 if (!net_eq(dev_net(dev), &init_net))
243 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
244 if (dev->nd_net != &init_net)
247 if (dev->type != ARPHRD_CAN)
250 if (msg == NETDEV_UNREGISTER) {
252 struct gw_job *gwj = NULL;
253 struct hlist_node *n, *nx;
255 spin_lock(&can_gw_list_lock);
257 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
259 if (gwj->src_dev == dev || gwj->dst_dev == dev) {
260 hlist_del(&gwj->list);
261 can_gw_unregister_filter(gwj);
266 spin_unlock(&can_gw_list_lock);
273 * Dump information about all ports, in response to GETROUTE
275 static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
277 printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
282 static int gw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
286 struct nlattr *tb[CGW_MAX+1];
294 } __attribute__((packed)) mb;
296 BUILD_BUG_ON(sizeof(mb) != CGW_MODATTR_LEN);
298 printk(KERN_INFO "%s: len %d attrlen %d\n", __FUNCTION__,
299 nlmsg_len(nlh), nlmsg_attrlen(nlh, sizeof(*r)));
301 if (nlmsg_len(nlh) < sizeof(*r))
305 if (r->can_family != AF_CAN)
306 return -EPFNOSUPPORT;
308 gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
312 gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
318 /* for now the source device needs to be a CAN device */
319 if (gwj->src_dev->type != ARPHRD_CAN) {
324 gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
330 /* for now the destination device needs to be a CAN device */
331 if (gwj->dst_dev->type != ARPHRD_CAN) {
333 goto put_src_dst_fail;
338 if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
339 gwj->flags |= CAN_TX_ECHO;
341 if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
342 gwj->flags |= CAN_TX_SRC_TSTAMP;
344 memset(&gwj->ccgw, 0, sizeof(gwj->ccgw));
346 /* check for additional attributes / filters here */
348 err = nlmsg_parse(nlh, sizeof(*r), tb, CGW_MAX, NULL);
350 goto put_src_dst_fail;
352 /* check for can_filter in attributes */
353 if (tb[CGW_FILTER] &&
354 nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
355 nla_memcpy(&gwj->ccgw.filter, tb[CGW_FILTER],
356 sizeof(struct can_filter));
358 /* check for AND/OR/XOR/SET modifications */
359 if (tb[CGW_MOD_AND] &&
360 nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
361 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
363 canframecpy(&gwj->ccgw.modframe.and, &mb.cf);
365 if (mb.modtype & CGW_MOD_ID)
366 gwj->ccgw.modfunc[modidx++] = mod_and_id;
368 if (mb.modtype & CGW_MOD_DLC)
369 gwj->ccgw.modfunc[modidx++] = mod_and_dlc;
371 if (mb.modtype & CGW_MOD_DATA)
372 gwj->ccgw.modfunc[modidx++] = mod_and_data;
375 if (tb[CGW_MOD_OR] &&
376 nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
377 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
379 canframecpy(&gwj->ccgw.modframe.or, &mb.cf);
381 if (mb.modtype & CGW_MOD_ID)
382 gwj->ccgw.modfunc[modidx++] = mod_or_id;
384 if (mb.modtype & CGW_MOD_DLC)
385 gwj->ccgw.modfunc[modidx++] = mod_or_dlc;
387 if (mb.modtype & CGW_MOD_DATA)
388 gwj->ccgw.modfunc[modidx++] = mod_or_data;
391 if (tb[CGW_MOD_XOR] &&
392 nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
393 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
395 canframecpy(&gwj->ccgw.modframe.xor, &mb.cf);
397 if (mb.modtype & CGW_MOD_ID)
398 gwj->ccgw.modfunc[modidx++] = mod_xor_id;
400 if (mb.modtype & CGW_MOD_DLC)
401 gwj->ccgw.modfunc[modidx++] = mod_xor_dlc;
403 if (mb.modtype & CGW_MOD_DATA)
404 gwj->ccgw.modfunc[modidx++] = mod_xor_data;
407 if (tb[CGW_MOD_SET] &&
408 nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
409 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
411 canframecpy(&gwj->ccgw.modframe.set, &mb.cf);
413 if (mb.modtype & CGW_MOD_ID)
414 gwj->ccgw.modfunc[modidx++] = mod_set_id;
416 if (mb.modtype & CGW_MOD_DLC)
417 gwj->ccgw.modfunc[modidx++] = mod_set_dlc;
419 if (mb.modtype & CGW_MOD_DATA)
420 gwj->ccgw.modfunc[modidx++] = mod_set_data;
423 spin_lock(&can_gw_list_lock);
425 err = can_gw_register_filter(gwj);
427 hlist_add_head_rcu(&gwj->list, &can_gw_list);
429 spin_unlock(&can_gw_list_lock);
431 dev_put(gwj->src_dev);
432 dev_put(gwj->dst_dev);
440 dev_put(gwj->dst_dev);
442 dev_put(gwj->src_dev);
444 kmem_cache_free(gw_cache, gwj);
448 static int gw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
450 printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
455 static __init int gw_module_init(void)
459 gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job),
466 notifier.notifier_call = gw_notifier;
467 register_netdevice_notifier(¬ifier);
469 if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) {
470 unregister_netdevice_notifier(¬ifier);
471 kmem_cache_destroy(gw_cache);
475 /* Only the first call to __rtnl_register can fail */
476 __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL);
477 __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL);
482 static __exit void gw_module_exit(void)
484 struct gw_job *gwj = NULL;
485 struct hlist_node *n, *nx;
487 rtnl_unregister_all(PF_CAN);
489 unregister_netdevice_notifier(¬ifier);
491 spin_lock(&can_gw_list_lock);
493 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
494 hlist_del(&gwj->list);
495 can_gw_unregister_filter(gwj);
499 spin_unlock(&can_gw_list_lock);
501 rcu_barrier(); /* Wait for completion of call_rcu()'s */
503 kmem_cache_destroy(gw_cache);
506 module_init(gw_module_init);
507 module_exit(gw_module_exit);