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 gwjob.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 */
125 void mod_and_id (struct can_frame *cf, struct can_can_gw *mod) {
126 cf->can_id &= mod->modframe.and.can_id;
128 void mod_and_dlc (struct can_frame *cf, struct can_can_gw *mod) {
129 cf->can_dlc &= mod->modframe.and.can_dlc;
131 void mod_and_data (struct can_frame *cf, struct can_can_gw *mod) {
132 *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data;
134 void mod_or_id (struct can_frame *cf, struct can_can_gw *mod) {
135 cf->can_id |= mod->modframe.or.can_id;
137 void mod_or_dlc (struct can_frame *cf, struct can_can_gw *mod) {
138 cf->can_dlc |= mod->modframe.or.can_dlc;
140 void mod_or_data (struct can_frame *cf, struct can_can_gw *mod) {
141 *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data;
143 void mod_xor_id (struct can_frame *cf, struct can_can_gw *mod) {
144 cf->can_id ^= mod->modframe.xor.can_id;
146 void mod_xor_dlc (struct can_frame *cf, struct can_can_gw *mod) {
147 cf->can_dlc ^= mod->modframe.xor.can_dlc;
149 void mod_xor_data (struct can_frame *cf, struct can_can_gw *mod) {
150 *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data;
152 void mod_set_id (struct can_frame *cf, struct can_can_gw *mod) {
153 cf->can_id = mod->modframe.set.can_id;
155 void mod_set_dlc (struct can_frame *cf, struct can_can_gw *mod) {
156 cf->can_dlc = mod->modframe.set.can_dlc;
158 void mod_set_data (struct can_frame *cf, struct can_can_gw *mod) {
159 *(u64 *)cf->data = *(u64 *)mod->modframe.set.data;
162 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
165 * Copy the struct members separately to ensure that no uninitialized
166 * data are copied in the 3 bytes hole of the struct. This is needed
167 * to make easy compares of the data in the struct can_can_gw.
170 dst->can_id = src->can_id;
171 dst->can_dlc = src->can_dlc;
172 *(u64 *)dst->data = *(u64 *)src->data;
175 /* the receive & process & send function */
176 static void gw_rcv(struct sk_buff *skb, void *data)
178 struct gw_job *gwj = (struct gw_job *)data;
179 struct can_frame *cf;
180 struct sk_buff *nskb;
183 /* do not handle already routed frames */
184 if (skb->sk == GW_SK_MAGIC)
187 if (!(gwj->dst_dev->flags & IFF_UP)) {
188 gwj->dropped_frames++;
193 * clone the given skb, which has not been done in can_rcv()
195 * When there is at least one modification function activated,
196 * we need to copy the skb as we want to modify skb->data.
198 if (gwj->ccgw.modfunc[0])
199 nskb = skb_copy(skb, GFP_ATOMIC);
201 nskb = skb_clone(skb, GFP_ATOMIC);
204 gwj->dropped_frames++;
208 /* mark routed frames with a 'special' sk value */
209 nskb->sk = GW_SK_MAGIC;
210 nskb->dev = gwj->dst_dev;
212 /* pointer to modifiable CAN frame */
213 cf = (struct can_frame *)nskb->data;
215 /* perform preprocessed modification functions if there are any */
216 while (modidx < MAX_MODFUNCTIONS && gwj->ccgw.modfunc[modidx])
217 (*gwj->ccgw.modfunc[modidx++])(cf, &gwj->ccgw);
219 /* clear the skb timestamp if not configured the other way */
220 if (!(gwj->flags & CAN_TX_SRC_TSTAMP))
221 nskb->tstamp.tv64 = 0;
223 /* send to netdevice */
224 if (can_send(nskb, gwj->flags & CAN_TX_ECHO))
225 gwj->dropped_frames++;
227 gwj->handled_frames++;
230 static inline int can_gw_register_filter(struct gw_job *gwj)
232 return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id,
233 gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw");
236 static inline void can_gw_unregister_filter(struct gw_job *gwj)
238 can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id,
239 gwj->ccgw.filter.can_mask, gw_rcv, gwj);
242 static int gw_notifier(struct notifier_block *nb,
243 unsigned long msg, void *data)
245 struct net_device *dev = (struct net_device *)data;
247 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
248 if (!net_eq(dev_net(dev), &init_net))
250 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
251 if (dev->nd_net != &init_net)
254 if (dev->type != ARPHRD_CAN)
257 if (msg == NETDEV_UNREGISTER) {
259 struct gw_job *gwj = NULL;
260 struct hlist_node *n, *nx;
262 spin_lock(&can_gw_list_lock);
264 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
266 if (gwj->src_dev == dev || gwj->dst_dev == dev) {
267 hlist_del(&gwj->list);
268 can_gw_unregister_filter(gwj);
273 spin_unlock(&can_gw_list_lock);
279 static int gw_put_job(struct sk_buff *skb, struct gw_job *gwj)
284 } __attribute__((packed)) mb;
286 struct rtcanmsg *rtcan;
287 struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
291 rtcan = nlmsg_data(nlh);
292 rtcan->can_family = AF_CAN;
293 rtcan->src_ifindex = gwj->src_dev->ifindex;
294 rtcan->dst_ifindex = gwj->dst_dev->ifindex;
295 rtcan->can_txflags = 0;
297 if (gwj->flags & CAN_TX_ECHO)
298 rtcan->can_txflags |= CAN_GW_TXFLAGS_ECHO;
300 if (gwj->flags & CAN_TX_SRC_TSTAMP)
301 rtcan->can_txflags |= CAN_GW_TXFLAGS_SRC_TSTAMP;
303 /* check non default settings of attributes */
304 if (gwj->handled_frames) {
305 if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
308 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
311 if (gwj->dropped_frames) {
312 if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
315 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
318 if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
319 if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
320 &gwj->ccgw.filter) < 0)
323 nlh->nlmsg_len += NLA_HDRLEN +
324 NLA_ALIGN(sizeof(struct can_filter));
327 if (gwj->ccgw.modtype.and) {
328 memcpy(&mb.cf, &gwj->ccgw.modframe.and, sizeof(mb.cf));
329 mb.modtype = gwj->ccgw.modtype.and;
330 if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
333 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
336 if (gwj->ccgw.modtype.or) {
337 memcpy(&mb.cf, &gwj->ccgw.modframe.or, sizeof(mb.cf));
338 mb.modtype = gwj->ccgw.modtype.or;
339 if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
342 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
345 if (gwj->ccgw.modtype.xor) {
346 memcpy(&mb.cf, &gwj->ccgw.modframe.xor, sizeof(mb.cf));
347 mb.modtype = gwj->ccgw.modtype.xor;
348 if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
351 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
354 if (gwj->ccgw.modtype.set) {
355 memcpy(&mb.cf, &gwj->ccgw.modframe.set, sizeof(mb.cf));
356 mb.modtype = gwj->ccgw.modtype.set;
357 if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
360 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
366 nlmsg_cancel(skb, nlh);
370 /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
371 static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
373 struct gw_job *gwj = NULL;
374 struct hlist_node *n;
379 hlist_for_each_entry_rcu(gwj, n, &can_gw_list, list) {
380 if (idx >= cb->args[0]) {
381 ret = gw_put_job(skb, gwj);
393 /* check for attributes / filters for the CAN->CAN gateway */
394 static int can_can_parse_attr(struct nlmsghdr *nlh, struct can_can_gw *ccgw)
396 struct nlattr *tb[CGW_MAX+1];
403 } __attribute__((packed)) mb;
405 BUILD_BUG_ON(sizeof(mb) != CGW_MODATTR_LEN);
407 memset(ccgw, 0, sizeof(*ccgw));
409 err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
413 /* check for can_filter in attributes */
414 if (tb[CGW_FILTER] &&
415 nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
416 nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
417 sizeof(struct can_filter));
419 /* check for AND/OR/XOR/SET modifications */
420 if (tb[CGW_MOD_AND] &&
421 nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
422 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
424 canframecpy(&ccgw->modframe.and, &mb.cf);
425 ccgw->modtype.and = mb.modtype;
427 if (mb.modtype & CGW_MOD_ID)
428 ccgw->modfunc[modidx++] = mod_and_id;
430 if (mb.modtype & CGW_MOD_DLC)
431 ccgw->modfunc[modidx++] = mod_and_dlc;
433 if (mb.modtype & CGW_MOD_DATA)
434 ccgw->modfunc[modidx++] = mod_and_data;
437 if (tb[CGW_MOD_OR] &&
438 nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
439 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
441 canframecpy(&ccgw->modframe.or, &mb.cf);
442 ccgw->modtype.or = mb.modtype;
444 if (mb.modtype & CGW_MOD_ID)
445 ccgw->modfunc[modidx++] = mod_or_id;
447 if (mb.modtype & CGW_MOD_DLC)
448 ccgw->modfunc[modidx++] = mod_or_dlc;
450 if (mb.modtype & CGW_MOD_DATA)
451 ccgw->modfunc[modidx++] = mod_or_data;
454 if (tb[CGW_MOD_XOR] &&
455 nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
456 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
458 canframecpy(&ccgw->modframe.xor, &mb.cf);
459 ccgw->modtype.xor = mb.modtype;
461 if (mb.modtype & CGW_MOD_ID)
462 ccgw->modfunc[modidx++] = mod_xor_id;
464 if (mb.modtype & CGW_MOD_DLC)
465 ccgw->modfunc[modidx++] = mod_xor_dlc;
467 if (mb.modtype & CGW_MOD_DATA)
468 ccgw->modfunc[modidx++] = mod_xor_data;
471 if (tb[CGW_MOD_SET] &&
472 nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
473 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
475 canframecpy(&ccgw->modframe.set, &mb.cf);
476 ccgw->modtype.set = mb.modtype;
478 if (mb.modtype & CGW_MOD_ID)
479 ccgw->modfunc[modidx++] = mod_set_id;
481 if (mb.modtype & CGW_MOD_DLC)
482 ccgw->modfunc[modidx++] = mod_set_dlc;
484 if (mb.modtype & CGW_MOD_DATA)
485 ccgw->modfunc[modidx++] = mod_set_data;
491 static int gw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
497 if (nlmsg_len(nlh) < sizeof(*r))
501 if (r->can_family != AF_CAN)
502 return -EPFNOSUPPORT;
504 gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
508 gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
514 /* for now the source device needs to be a CAN device */
515 if (gwj->src_dev->type != ARPHRD_CAN) {
520 gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
526 /* for now the destination device needs to be a CAN device */
527 if (gwj->dst_dev->type != ARPHRD_CAN) {
529 goto put_src_dst_fail;
532 gwj->handled_frames = 0;
533 gwj->dropped_frames = 0;
536 if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
537 gwj->flags |= CAN_TX_ECHO;
539 if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
540 gwj->flags |= CAN_TX_SRC_TSTAMP;
542 err = can_can_parse_attr(nlh, &gwj->ccgw);
544 goto put_src_dst_fail;
546 spin_lock(&can_gw_list_lock);
548 err = can_gw_register_filter(gwj);
550 hlist_add_head_rcu(&gwj->list, &can_gw_list);
552 spin_unlock(&can_gw_list_lock);
554 dev_put(gwj->src_dev);
555 dev_put(gwj->dst_dev);
563 dev_put(gwj->dst_dev);
565 dev_put(gwj->src_dev);
567 kmem_cache_free(gw_cache, gwj);
571 static int gw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
573 struct gw_job *gwj = NULL;
574 struct hlist_node *n, *nx;
576 struct can_can_gw ccgw;
580 if (nlmsg_len(nlh) < sizeof(*r))
584 if (r->can_family != AF_CAN)
585 return -EPFNOSUPPORT;
587 if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
588 flags |= CAN_TX_ECHO;
590 if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
591 flags |= CAN_TX_SRC_TSTAMP;
593 err = can_can_parse_attr(nlh, &ccgw);
599 spin_lock(&can_gw_list_lock);
601 /* remove only the first matching entry */
602 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
604 if (gwj->dst_dev->ifindex != r->dst_ifindex)
607 if (gwj->src_dev->ifindex != r->src_ifindex)
610 if (gwj->flags != flags)
613 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
616 hlist_del(&gwj->list);
617 can_gw_unregister_filter(gwj);
623 spin_unlock(&can_gw_list_lock);
628 static __init int gw_module_init(void)
632 gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job),
639 notifier.notifier_call = gw_notifier;
640 register_netdevice_notifier(¬ifier);
642 if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) {
643 unregister_netdevice_notifier(¬ifier);
644 kmem_cache_destroy(gw_cache);
648 /* Only the first call to __rtnl_register can fail */
649 __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL);
650 __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL);
655 static __exit void gw_module_exit(void)
657 struct gw_job *gwj = NULL;
658 struct hlist_node *n, *nx;
660 rtnl_unregister_all(PF_CAN);
662 unregister_netdevice_notifier(¬ifier);
664 spin_lock(&can_gw_list_lock);
666 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
667 hlist_del(&gwj->list);
668 can_gw_unregister_filter(gwj);
672 spin_unlock(&can_gw_list_lock);
674 rcu_barrier(); /* Wait for completion of call_rcu()'s */
676 kmem_cache_destroy(gw_cache);
679 module_init(gw_module_init);
680 module_exit(gw_module_exit);