]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/gw.c
Added netlink powered CAN gateway functionality with CAN frame modification features.
[socketcan-devel.git] / kernel / 2.6 / net / can / gw.c
1 /*
2  * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
3  *
4  * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
23  *
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.
26  *
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
38  * DAMAGE.
39  *
40  * Send feedback to <socketcan-users@lists.berlios.de>
41  *
42  */
43
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>
60
61 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
62 RCSID("$Id$");
63
64 #define CAN_GW_VERSION "20100218"
65 static __initdata const char banner[] =
66         KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
67
68 MODULE_DESCRIPTION("PF_CAN netlink gateway");
69 MODULE_LICENSE("Dual BSD/GPL");
70 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
71
72 HLIST_HEAD(can_gw_list);
73 static DEFINE_SPINLOCK(can_gw_list_lock);
74 static struct notifier_block notifier;
75
76 static struct kmem_cache *gw_cache __read_mostly;
77
78 #define GW_SK_MAGIC ((void *)(&notifier))
79
80 /*
81  * So far we just support CAN -> CAN routing and frame modifications.
82  *
83  * The internal can_can_gw structure contains optional attributes for
84  * a CAN -> CAN gateway job.
85  */
86 struct can_can_gw {
87         struct can_filter filter;
88         struct {
89                 struct can_frame and;
90                 struct can_frame or;
91                 struct can_frame xor;
92                 struct can_frame set;
93         } modframe;
94         void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
95                                           struct can_can_gw *mod);
96 };
97
98 /* list entry for CAN gateways jobs */
99 struct gw_job {
100         struct hlist_node list;
101         struct rcu_head rcu;
102         struct net_device *src_dev;
103         struct net_device *dst_dev;
104         u32 flags;
105         u32 handled_frames;
106         u32 dropped_frames;
107         union {
108                 struct can_can_gw ccgw;
109                 /* tbc */
110         };
111 };
112
113 /* content of u32 gwjob.flags */
114 #define CAN_TX_LOOPBACK 0x00000001
115
116 /* modification functions that are invoked in the hot path in gw_rcv */
117 void mod_and_id (struct can_frame *cf, struct can_can_gw *mod) {
118         cf->can_id &= mod->modframe.and.can_id;
119 }
120 void mod_and_dlc (struct can_frame *cf, struct can_can_gw *mod) {
121         cf->can_dlc &= mod->modframe.and.can_dlc;
122 }
123 void mod_and_data (struct can_frame *cf, struct can_can_gw *mod) {
124         *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data;
125 }
126 void mod_or_id (struct can_frame *cf, struct can_can_gw *mod) {
127         cf->can_id |= mod->modframe.or.can_id;
128 }
129 void mod_or_dlc (struct can_frame *cf, struct can_can_gw *mod) {
130         cf->can_dlc |= mod->modframe.or.can_dlc;
131 }
132 void mod_or_data (struct can_frame *cf, struct can_can_gw *mod) {
133         *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data;
134 }
135 void mod_xor_id (struct can_frame *cf, struct can_can_gw *mod) {
136         cf->can_id ^= mod->modframe.xor.can_id;
137 }
138 void mod_xor_dlc (struct can_frame *cf, struct can_can_gw *mod) {
139         cf->can_dlc ^= mod->modframe.xor.can_dlc;
140 }
141 void mod_xor_data (struct can_frame *cf, struct can_can_gw *mod) {
142         *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data;
143 }
144 void mod_set_id (struct can_frame *cf, struct can_can_gw *mod) {
145         cf->can_id = mod->modframe.set.can_id;
146 }
147 void mod_set_dlc (struct can_frame *cf, struct can_can_gw *mod) {
148         cf->can_dlc = mod->modframe.set.can_dlc;
149 }
150 void mod_set_data (struct can_frame *cf, struct can_can_gw *mod) {
151         *(u64 *)cf->data = *(u64 *)mod->modframe.set.data;
152 }
153
154
155 /* the receive & process & send function */
156 static void gw_rcv(struct sk_buff *skb, void *data)
157 {
158         struct gw_job *gwj = (struct gw_job *)data;
159         struct can_frame *cf;
160         struct sk_buff *nskb;
161         int modidx = 0;
162
163         /* do not handle already routed frames */
164         if (skb->sk == GW_SK_MAGIC)
165                 return;
166
167         if (!netif_running(gwj->dst_dev)) {
168                 gwj->dropped_frames++;
169                 return;
170         }
171
172         /*
173          * clone the given skb, which has not been done in can_rv()
174          *
175          * When there is at least one modification function activated,
176          * we need to copy the skb as we want to modify skb->data.
177          */
178         if (gwj->ccgw.modfunc[0])
179                 nskb = skb_copy(skb, GFP_ATOMIC);
180         else
181                 nskb = skb_clone(skb, GFP_ATOMIC);
182
183         if (!nskb) {
184                 gwj->dropped_frames++;
185                 return;
186         }
187
188         gwj->handled_frames++;
189
190         /* mark routed frames with a 'special' sk value */
191         nskb->sk = GW_SK_MAGIC;
192         nskb->dev = gwj->dst_dev;
193
194         /* pointer to modifiable CAN frame */
195         cf = (struct can_frame *)nskb->data;
196
197         /* perform preprocessed modification functions if there are any */
198         while (modidx < MAX_MODFUNCTIONS && gwj->ccgw.modfunc[modidx])
199                 (*gwj->ccgw.modfunc[modidx++])(cf, &gwj->ccgw);
200
201         /* send to netdevice */
202         if (can_send(nskb, gwj->flags & CAN_TX_LOOPBACK))
203                 gwj->dropped_frames++;
204 }
205
206 static inline int can_gw_register_filter(struct gw_job *gwj)
207 {
208         return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id,
209                                gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw");
210 }
211
212 static inline void can_gw_unregister_filter(struct gw_job *gwj)
213 {
214         can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id,
215                           gwj->ccgw.filter.can_mask, gw_rcv, gwj);
216 }
217
218 static int gw_notifier(struct notifier_block *nb,
219                         unsigned long msg, void *data)
220 {
221         struct net_device *dev = (struct net_device *)data;
222
223 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
224         if (!net_eq(dev_net(dev), &init_net))
225                 return NOTIFY_DONE;
226 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
227         if (dev->nd_net != &init_net)
228                 return NOTIFY_DONE;
229 #endif
230         if (dev->type != ARPHRD_CAN)
231                 return NOTIFY_DONE;
232
233         if (msg == NETDEV_UNREGISTER) {
234
235                 struct gw_job *gwj = NULL;
236                 struct hlist_node *n, *nx;
237
238                 spin_lock(&can_gw_list_lock);
239
240                 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
241
242                         if (gwj->src_dev == dev || gwj->dst_dev == dev) { 
243                                 hlist_del(&gwj->list);
244                                 can_gw_unregister_filter(gwj);
245                                 kfree(gwj);
246                         }
247                 }
248
249                 spin_unlock(&can_gw_list_lock);
250         }
251
252         return NOTIFY_DONE;
253 }
254
255 /*
256  * Dump information about all ports, in response to GETROUTE
257  */
258 static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
259 {
260         printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
261
262         return 0;
263 }
264
265 static int gw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
266 {
267
268         struct rtcanmsg *r;
269         struct nlattr *tb[CGW_MAX+1];
270         struct gw_job *gwj;
271         u8 buf[CGW_MODATTR_LEN];
272         int modidx = 0;
273         int err = 0;
274
275         printk(KERN_INFO "%s: len %d attrlen %d\n", __FUNCTION__,
276                nlmsg_len(nlh), nlmsg_attrlen(nlh, sizeof(*r)));
277
278         if (nlmsg_len(nlh) < sizeof(*r))
279                 return -EINVAL;
280
281         r = nlmsg_data(nlh);
282         if (r->can_family != AF_CAN)
283                 return -EPFNOSUPPORT;
284
285         gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
286         if (!gwj)
287                 return -ENOMEM;
288
289         gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
290         if (!gwj->src_dev) {
291                 err = -ENODEV;
292                 goto fail;
293         }
294
295         /* for now the source device needs to be a CAN device */
296         if (gwj->src_dev->type != ARPHRD_CAN) {
297                 err = -ENODEV;
298                 goto put_src_fail;
299         }
300
301         gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
302         if (!gwj->dst_dev) {
303                 err = -ENODEV;
304                 goto put_src_fail;
305         }
306
307         /* for now the destination device needs to be a CAN device */
308         if (gwj->dst_dev->type != ARPHRD_CAN) {
309                 err = -ENODEV;
310                 goto put_src_dst_fail;
311         }
312
313         gwj->flags = 0;
314
315         if (r->can_txflags & CAN_GW_TXFLAGS_LOOPBACK)
316                 gwj->flags |= CAN_TX_LOOPBACK;
317
318         memset(&gwj->ccgw, 0, sizeof(gwj->ccgw)); 
319
320         /* check for additional attributes / filters here */
321
322         err = nlmsg_parse(nlh, sizeof(*r), tb, CGW_MAX, NULL);
323         if (err < 0)
324                 goto put_src_dst_fail;
325
326         /* check for can_filter in attributes */
327         if (tb[CGW_FILTER] &&
328             nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
329                 nla_memcpy(&gwj->ccgw.filter, tb[CGW_FILTER],
330                            sizeof(struct can_filter));
331
332         /* check for AND/OR/XOR/SET modifications */
333         if (tb[CGW_MOD_AND] &&
334             nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
335                 nla_memcpy(&buf, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
336
337                 memcpy(&gwj->ccgw.modframe.and, &buf[1],
338                        sizeof(struct can_frame));
339
340                 if (buf[0] & CGW_MOD_ID)
341                         gwj->ccgw.modfunc[modidx++] = mod_and_id;
342
343                 if (buf[0] & CGW_MOD_DLC)
344                         gwj->ccgw.modfunc[modidx++] = mod_and_dlc;
345
346                 if (buf[0] & CGW_MOD_DATA)
347                         gwj->ccgw.modfunc[modidx++] = mod_and_data;
348         }
349
350         if (tb[CGW_MOD_OR] &&
351             nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
352                 nla_memcpy(&buf, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
353
354                 memcpy(&gwj->ccgw.modframe.or, &buf[1],
355                        sizeof(struct can_frame));
356
357                 if (buf[0] & CGW_MOD_ID)
358                         gwj->ccgw.modfunc[modidx++] = mod_or_id;
359
360                 if (buf[0] & CGW_MOD_DLC)
361                         gwj->ccgw.modfunc[modidx++] = mod_or_dlc;
362
363                 if (buf[0] & CGW_MOD_DATA)
364                         gwj->ccgw.modfunc[modidx++] = mod_or_data;
365         }
366
367         if (tb[CGW_MOD_XOR] &&
368             nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
369                 nla_memcpy(&buf, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
370
371                 memcpy(&gwj->ccgw.modframe.xor, &buf[1],
372                        sizeof(struct can_frame));
373
374                 if (buf[0] & CGW_MOD_ID)
375                         gwj->ccgw.modfunc[modidx++] = mod_xor_id;
376
377                 if (buf[0] & CGW_MOD_DLC)
378                         gwj->ccgw.modfunc[modidx++] = mod_xor_dlc;
379
380                 if (buf[0] & CGW_MOD_DATA)
381                         gwj->ccgw.modfunc[modidx++] = mod_xor_data;
382         }
383
384         if (tb[CGW_MOD_SET] &&
385             nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
386                 nla_memcpy(&buf, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
387
388                 memcpy(&gwj->ccgw.modframe.set, &buf[1],
389                        sizeof(struct can_frame));
390
391                 if (buf[0] & CGW_MOD_ID)
392                         gwj->ccgw.modfunc[modidx++] = mod_set_id;
393
394                 if (buf[0] & CGW_MOD_DLC)
395                         gwj->ccgw.modfunc[modidx++] = mod_set_dlc;
396
397                 if (buf[0] & CGW_MOD_DATA)
398                         gwj->ccgw.modfunc[modidx++] = mod_set_data;
399         }
400
401         spin_lock(&can_gw_list_lock);
402
403         err = can_gw_register_filter(gwj);
404         if (!err)
405                 hlist_add_head_rcu(&gwj->list, &can_gw_list);
406
407         spin_unlock(&can_gw_list_lock);
408         
409         dev_put(gwj->src_dev);
410         dev_put(gwj->dst_dev);
411
412         if (!err)
413                 return 0;
414
415 put_src_dst_fail:
416         dev_put(gwj->dst_dev);
417 put_src_fail:
418         dev_put(gwj->src_dev);
419 fail:
420         kmem_cache_free(gw_cache, gwj);
421         return err;
422 }
423
424 static int gw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
425 {
426         printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
427
428         return 0;
429 }
430
431 static __init int gw_module_init(void)
432 {
433         printk(banner);
434
435         gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job),
436                                       0, 0, NULL);
437
438         if (!gw_cache)
439                 return -ENOMEM;
440
441         /* set notifier */
442         notifier.notifier_call = gw_notifier;
443         register_netdevice_notifier(&notifier);
444
445         if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) {
446                 unregister_netdevice_notifier(&notifier);
447                 kmem_cache_destroy(gw_cache);
448                 return -ENOBUFS;
449         }
450
451         /* Only the first call to __rtnl_register can fail */
452         __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL);
453         __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL);
454
455         return 0;
456 }
457
458 static __exit void gw_module_exit(void)
459 {
460         struct gw_job *gwj = NULL;
461         struct hlist_node *n, *nx;
462
463         rtnl_unregister_all(PF_CAN);
464
465         unregister_netdevice_notifier(&notifier);
466
467         spin_lock(&can_gw_list_lock);
468
469         hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
470                 hlist_del(&gwj->list);
471                 can_gw_unregister_filter(gwj);
472                 kfree(gwj);
473         }
474
475         spin_unlock(&can_gw_list_lock);
476
477         rcu_barrier(); /* Wait for completion of call_rcu()'s */
478
479         kmem_cache_destroy(gw_cache);
480 }
481
482 module_init(gw_module_init);
483 module_exit(gw_module_exit);