2 * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
4 * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/types.h>
48 #include <linux/list.h>
49 #include <linux/spinlock.h>
50 #include <linux/rcupdate.h>
51 #include <linux/rculist.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <socketcan/can.h>
57 #include <socketcan/can/core.h>
58 #include <socketcan/can/gw.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
62 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
65 #define CAN_GW_VERSION "20100222"
66 static __initdata const char banner[] =
67 KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
69 MODULE_DESCRIPTION("PF_CAN netlink gateway");
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73 HLIST_HEAD(can_gw_list);
74 static DEFINE_SPINLOCK(can_gw_list_lock);
75 static struct notifier_block notifier;
77 static struct kmem_cache *gw_cache __read_mostly;
79 #define GW_SK_MAGIC ((void *)(¬ifier))
82 * So far we just support CAN -> CAN routing and frame modifications.
84 * The internal can_can_gw structure contains optional attributes for
85 * a CAN -> CAN gateway job.
88 struct can_filter filter;
101 void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
102 struct can_can_gw *mod);
105 /* list entry for CAN gateways jobs */
107 struct hlist_node list;
109 struct net_device *src_dev;
110 struct net_device *dst_dev;
115 struct can_can_gw ccgw;
120 /* content of u32 gw_job.flags */
121 #define CAN_TX_ECHO 0x00000001
122 #define CAN_TX_SRC_TSTAMP 0x00000002
124 /* modification functions that are invoked in the hot path in gw_rcv */
126 #define MODFUNC(func, op) static void func (struct can_frame *cf, \
127 struct can_can_gw *mod) { op ; }
129 MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
130 MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
131 MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
132 MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
133 MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
134 MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
135 MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
136 MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
137 MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
138 MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
139 MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
140 MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
142 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
145 * Copy the struct members separately to ensure that no uninitialized
146 * data are copied in the 3 bytes hole of the struct. This is needed
147 * to make easy compares of the data in the struct can_can_gw.
150 dst->can_id = src->can_id;
151 dst->can_dlc = src->can_dlc;
152 *(u64 *)dst->data = *(u64 *)src->data;
155 /* the receive & process & send function */
156 static void gw_rcv(struct sk_buff *skb, void *data)
158 struct gw_job *gwj = (struct gw_job *)data;
159 struct can_frame *cf;
160 struct sk_buff *nskb;
163 /* do not handle already routed frames */
164 if (skb->sk == GW_SK_MAGIC)
167 if (!(gwj->dst_dev->flags & IFF_UP)) {
168 gwj->dropped_frames++;
173 * clone the given skb, which has not been done in can_rcv()
175 * When there is at least one modification function activated,
176 * we need to copy the skb as we want to modify skb->data.
178 if (gwj->ccgw.modfunc[0])
179 nskb = skb_copy(skb, GFP_ATOMIC);
181 nskb = skb_clone(skb, GFP_ATOMIC);
184 gwj->dropped_frames++;
188 /* mark routed frames with a 'special' sk value */
189 nskb->sk = GW_SK_MAGIC;
190 nskb->dev = gwj->dst_dev;
192 /* pointer to modifiable CAN frame */
193 cf = (struct can_frame *)nskb->data;
195 /* perform preprocessed modification functions if there are any */
196 while (modidx < MAX_MODFUNCTIONS && gwj->ccgw.modfunc[modidx])
197 (*gwj->ccgw.modfunc[modidx++])(cf, &gwj->ccgw);
199 /* clear the skb timestamp if not configured the other way */
200 if (!(gwj->flags & CAN_TX_SRC_TSTAMP))
201 nskb->tstamp.tv64 = 0;
203 /* send to netdevice */
204 if (can_send(nskb, gwj->flags & CAN_TX_ECHO))
205 gwj->dropped_frames++;
207 gwj->handled_frames++;
210 static inline int can_gw_register_filter(struct gw_job *gwj)
212 return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id,
213 gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw");
216 static inline void can_gw_unregister_filter(struct gw_job *gwj)
218 can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id,
219 gwj->ccgw.filter.can_mask, gw_rcv, gwj);
222 static int gw_notifier(struct notifier_block *nb,
223 unsigned long msg, void *data)
225 struct net_device *dev = (struct net_device *)data;
227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
228 if (!net_eq(dev_net(dev), &init_net))
230 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
231 if (dev->nd_net != &init_net)
234 if (dev->type != ARPHRD_CAN)
237 if (msg == NETDEV_UNREGISTER) {
239 struct gw_job *gwj = NULL;
240 struct hlist_node *n, *nx;
242 spin_lock(&can_gw_list_lock);
244 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
246 if (gwj->src_dev == dev || gwj->dst_dev == dev) {
247 hlist_del(&gwj->list);
248 can_gw_unregister_filter(gwj);
253 spin_unlock(&can_gw_list_lock);
259 static int gw_put_job(struct sk_buff *skb, struct gw_job *gwj)
264 } __attribute__((packed)) mb;
266 struct rtcanmsg *rtcan;
267 struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
271 rtcan = nlmsg_data(nlh);
272 rtcan->can_family = AF_CAN;
273 rtcan->src_ifindex = gwj->src_dev->ifindex;
274 rtcan->dst_ifindex = gwj->dst_dev->ifindex;
275 rtcan->can_txflags = 0;
277 if (gwj->flags & CAN_TX_ECHO)
278 rtcan->can_txflags |= CAN_GW_TXFLAGS_ECHO;
280 if (gwj->flags & CAN_TX_SRC_TSTAMP)
281 rtcan->can_txflags |= CAN_GW_TXFLAGS_SRC_TSTAMP;
283 /* check non default settings of attributes */
284 if (gwj->handled_frames) {
285 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
288 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
291 if (gwj->dropped_frames) {
292 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
295 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
298 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
299 if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
300 &gwj->ccgw.filter) < 0)
303 nlh->nlmsg_len += NLA_HDRLEN +
304 NLA_ALIGN(sizeof(struct can_filter));
307 if (gwj->ccgw.modtype.and) {
308 memcpy(&mb.cf, &gwj->ccgw.modframe.and, sizeof(mb.cf));
309 mb.modtype = gwj->ccgw.modtype.and;
310 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
313 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
316 if (gwj->ccgw.modtype.or) {
317 memcpy(&mb.cf, &gwj->ccgw.modframe.or, sizeof(mb.cf));
318 mb.modtype = gwj->ccgw.modtype.or;
319 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
322 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
325 if (gwj->ccgw.modtype.xor) {
326 memcpy(&mb.cf, &gwj->ccgw.modframe.xor, sizeof(mb.cf));
327 mb.modtype = gwj->ccgw.modtype.xor;
328 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
331 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
334 if (gwj->ccgw.modtype.set) {
335 memcpy(&mb.cf, &gwj->ccgw.modframe.set, sizeof(mb.cf));
336 mb.modtype = gwj->ccgw.modtype.set;
337 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
340 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
346 nlmsg_cancel(skb, nlh);
350 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
351 static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
353 struct gw_job *gwj = NULL;
354 struct hlist_node *n;
359 hlist_for_each_entry_rcu(gwj, n, &can_gw_list, list) {
360 if (idx >= cb->args[0]) {
361 ret = gw_put_job(skb, gwj);
373 /* check for attributes / filters for the CAN->CAN gateway */
374 static int can_can_parse_attr(struct nlmsghdr *nlh, struct can_can_gw *ccgw)
376 struct nlattr *tb[CGW_MAX+1];
383 } __attribute__((packed)) mb;
385 BUILD_BUG_ON(sizeof(mb) != CGW_MODATTR_LEN);
387 memset(ccgw, 0, sizeof(*ccgw));
389 err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
393 /* check for can_filter in attributes */
394 if (tb[CGW_FILTER] &&
395 nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
396 nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
397 sizeof(struct can_filter));
399 /* check for AND/OR/XOR/SET modifications */
400 if (tb[CGW_MOD_AND] &&
401 nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
402 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
404 canframecpy(&ccgw->modframe.and, &mb.cf);
405 ccgw->modtype.and = mb.modtype;
407 if (mb.modtype & CGW_MOD_ID)
408 ccgw->modfunc[modidx++] = mod_and_id;
410 if (mb.modtype & CGW_MOD_DLC)
411 ccgw->modfunc[modidx++] = mod_and_dlc;
413 if (mb.modtype & CGW_MOD_DATA)
414 ccgw->modfunc[modidx++] = mod_and_data;
417 if (tb[CGW_MOD_OR] &&
418 nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
419 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
421 canframecpy(&ccgw->modframe.or, &mb.cf);
422 ccgw->modtype.or = mb.modtype;
424 if (mb.modtype & CGW_MOD_ID)
425 ccgw->modfunc[modidx++] = mod_or_id;
427 if (mb.modtype & CGW_MOD_DLC)
428 ccgw->modfunc[modidx++] = mod_or_dlc;
430 if (mb.modtype & CGW_MOD_DATA)
431 ccgw->modfunc[modidx++] = mod_or_data;
434 if (tb[CGW_MOD_XOR] &&
435 nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
436 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
438 canframecpy(&ccgw->modframe.xor, &mb.cf);
439 ccgw->modtype.xor = mb.modtype;
441 if (mb.modtype & CGW_MOD_ID)
442 ccgw->modfunc[modidx++] = mod_xor_id;
444 if (mb.modtype & CGW_MOD_DLC)
445 ccgw->modfunc[modidx++] = mod_xor_dlc;
447 if (mb.modtype & CGW_MOD_DATA)
448 ccgw->modfunc[modidx++] = mod_xor_data;
451 if (tb[CGW_MOD_SET] &&
452 nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
453 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
455 canframecpy(&ccgw->modframe.set, &mb.cf);
456 ccgw->modtype.set = mb.modtype;
458 if (mb.modtype & CGW_MOD_ID)
459 ccgw->modfunc[modidx++] = mod_set_id;
461 if (mb.modtype & CGW_MOD_DLC)
462 ccgw->modfunc[modidx++] = mod_set_dlc;
464 if (mb.modtype & CGW_MOD_DATA)
465 ccgw->modfunc[modidx++] = mod_set_data;
471 static int gw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
477 if (nlmsg_len(nlh) < sizeof(*r))
481 if (r->can_family != AF_CAN)
482 return -EPFNOSUPPORT;
484 gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
488 gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
494 /* for now the source device needs to be a CAN device */
495 if (gwj->src_dev->type != ARPHRD_CAN) {
500 gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
506 /* for now the destination device needs to be a CAN device */
507 if (gwj->dst_dev->type != ARPHRD_CAN) {
509 goto put_src_dst_fail;
512 gwj->handled_frames = 0;
513 gwj->dropped_frames = 0;
516 if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
517 gwj->flags |= CAN_TX_ECHO;
519 if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
520 gwj->flags |= CAN_TX_SRC_TSTAMP;
522 err = can_can_parse_attr(nlh, &gwj->ccgw);
524 goto put_src_dst_fail;
526 spin_lock(&can_gw_list_lock);
528 err = can_gw_register_filter(gwj);
530 hlist_add_head_rcu(&gwj->list, &can_gw_list);
532 spin_unlock(&can_gw_list_lock);
534 dev_put(gwj->src_dev);
535 dev_put(gwj->dst_dev);
543 dev_put(gwj->dst_dev);
545 dev_put(gwj->src_dev);
547 kmem_cache_free(gw_cache, gwj);
551 static void gw_remove_all_jobs(void)
553 struct gw_job *gwj = NULL;
554 struct hlist_node *n, *nx;
556 spin_lock(&can_gw_list_lock);
558 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
559 hlist_del(&gwj->list);
560 can_gw_unregister_filter(gwj);
564 spin_unlock(&can_gw_list_lock);
567 static int gw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
569 struct gw_job *gwj = NULL;
570 struct hlist_node *n, *nx;
572 struct can_can_gw ccgw;
576 if (nlmsg_len(nlh) < sizeof(*r))
580 if (r->can_family != AF_CAN)
581 return -EPFNOSUPPORT;
583 /* if_index set to 0 => remove all entries */
584 if (!r->src_ifindex && !r->dst_ifindex) {
585 gw_remove_all_jobs();
589 if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
590 flags |= CAN_TX_ECHO;
592 if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
593 flags |= CAN_TX_SRC_TSTAMP;
595 err = can_can_parse_attr(nlh, &ccgw);
601 spin_lock(&can_gw_list_lock);
603 /* remove only the first matching entry */
604 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
606 if (gwj->dst_dev->ifindex != r->dst_ifindex)
609 if (gwj->src_dev->ifindex != r->src_ifindex)
612 if (gwj->flags != flags)
615 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
618 hlist_del(&gwj->list);
619 can_gw_unregister_filter(gwj);
625 spin_unlock(&can_gw_list_lock);
630 static __init int gw_module_init(void)
634 gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job),
641 notifier.notifier_call = gw_notifier;
642 register_netdevice_notifier(¬ifier);
644 if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) {
645 unregister_netdevice_notifier(¬ifier);
646 kmem_cache_destroy(gw_cache);
650 /* Only the first call to __rtnl_register can fail */
651 __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL);
652 __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL);
657 static __exit void gw_module_exit(void)
659 rtnl_unregister_all(PF_CAN);
661 unregister_netdevice_notifier(¬ifier);
663 gw_remove_all_jobs();
665 rcu_barrier(); /* Wait for completion of call_rcu()'s */
667 kmem_cache_destroy(gw_cache);
670 module_init(gw_module_init);
671 module_exit(gw_module_exit);