]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/gw.c
Moved kernel parsing of netlink attributes into can_can_parse_attr().
[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_ECHO 0x00000001
115 #define CAN_TX_SRC_TSTAMP 0x00000002
116
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;
120 }
121 void mod_and_dlc (struct can_frame *cf, struct can_can_gw *mod) {
122         cf->can_dlc &= mod->modframe.and.can_dlc;
123 }
124 void mod_and_data (struct can_frame *cf, struct can_can_gw *mod) {
125         *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data;
126 }
127 void mod_or_id (struct can_frame *cf, struct can_can_gw *mod) {
128         cf->can_id |= mod->modframe.or.can_id;
129 }
130 void mod_or_dlc (struct can_frame *cf, struct can_can_gw *mod) {
131         cf->can_dlc |= mod->modframe.or.can_dlc;
132 }
133 void mod_or_data (struct can_frame *cf, struct can_can_gw *mod) {
134         *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data;
135 }
136 void mod_xor_id (struct can_frame *cf, struct can_can_gw *mod) {
137         cf->can_id ^= mod->modframe.xor.can_id;
138 }
139 void mod_xor_dlc (struct can_frame *cf, struct can_can_gw *mod) {
140         cf->can_dlc ^= mod->modframe.xor.can_dlc;
141 }
142 void mod_xor_data (struct can_frame *cf, struct can_can_gw *mod) {
143         *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data;
144 }
145 void mod_set_id (struct can_frame *cf, struct can_can_gw *mod) {
146         cf->can_id = mod->modframe.set.can_id;
147 }
148 void mod_set_dlc (struct can_frame *cf, struct can_can_gw *mod) {
149         cf->can_dlc = mod->modframe.set.can_dlc;
150 }
151 void mod_set_data (struct can_frame *cf, struct can_can_gw *mod) {
152         *(u64 *)cf->data = *(u64 *)mod->modframe.set.data;
153 }
154
155 static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
156 {
157         /*
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.
161          */
162
163         dst->can_id = src->can_id;
164         dst->can_dlc = src->can_dlc;
165         *(u64 *)dst->data = *(u64 *)src->data;
166 }
167
168 /* the receive & process & send function */
169 static void gw_rcv(struct sk_buff *skb, void *data)
170 {
171         struct gw_job *gwj = (struct gw_job *)data;
172         struct can_frame *cf;
173         struct sk_buff *nskb;
174         int modidx = 0;
175
176         /* do not handle already routed frames */
177         if (skb->sk == GW_SK_MAGIC)
178                 return;
179
180         if (!netif_running(gwj->dst_dev)) {
181                 gwj->dropped_frames++;
182                 return;
183         }
184
185         /*
186          * clone the given skb, which has not been done in can_rcv()
187          *
188          * When there is at least one modification function activated,
189          * we need to copy the skb as we want to modify skb->data.
190          */
191         if (gwj->ccgw.modfunc[0])
192                 nskb = skb_copy(skb, GFP_ATOMIC);
193         else
194                 nskb = skb_clone(skb, GFP_ATOMIC);
195
196         if (!nskb) {
197                 gwj->dropped_frames++;
198                 return;
199         }
200
201         /* mark routed frames with a 'special' sk value */
202         nskb->sk = GW_SK_MAGIC;
203         nskb->dev = gwj->dst_dev;
204
205         /* pointer to modifiable CAN frame */
206         cf = (struct can_frame *)nskb->data;
207
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);
211
212         /* clear the skb timestamp if not configured the other way */
213         if (!(gwj->flags & CAN_TX_SRC_TSTAMP))
214                 nskb->tstamp.tv64 = 0;
215
216         /* send to netdevice */
217         if (can_send(nskb, gwj->flags & CAN_TX_ECHO))
218                 gwj->dropped_frames++;
219         else
220                 gwj->handled_frames++;
221 }
222
223 static inline int can_gw_register_filter(struct gw_job *gwj)
224 {
225         return can_rx_register(gwj->src_dev, gwj->ccgw.filter.can_id,
226                                gwj->ccgw.filter.can_mask, gw_rcv, gwj, "gw");
227 }
228
229 static inline void can_gw_unregister_filter(struct gw_job *gwj)
230 {
231         can_rx_unregister(gwj->src_dev, gwj->ccgw.filter.can_id,
232                           gwj->ccgw.filter.can_mask, gw_rcv, gwj);
233 }
234
235 static int gw_notifier(struct notifier_block *nb,
236                         unsigned long msg, void *data)
237 {
238         struct net_device *dev = (struct net_device *)data;
239
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
241         if (!net_eq(dev_net(dev), &init_net))
242                 return NOTIFY_DONE;
243 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
244         if (dev->nd_net != &init_net)
245                 return NOTIFY_DONE;
246 #endif
247         if (dev->type != ARPHRD_CAN)
248                 return NOTIFY_DONE;
249
250         if (msg == NETDEV_UNREGISTER) {
251
252                 struct gw_job *gwj = NULL;
253                 struct hlist_node *n, *nx;
254
255                 spin_lock(&can_gw_list_lock);
256
257                 hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
258
259                         if (gwj->src_dev == dev || gwj->dst_dev == dev) { 
260                                 hlist_del(&gwj->list);
261                                 can_gw_unregister_filter(gwj);
262                                 kfree(gwj);
263                         }
264                 }
265
266                 spin_unlock(&can_gw_list_lock);
267         }
268
269         return NOTIFY_DONE;
270 }
271
272 /*
273  * Dump information about all ports, in response to GETROUTE
274  */
275 static int gw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
276 {
277         printk(KERN_INFO "%s (TODO)\n", __FUNCTION__);
278
279         return 0;
280 }
281
282 /* check for attributes / filters for the CAN->CAN gateway */
283 static int can_can_parse_attr(struct nlmsghdr *nlh, struct can_can_gw *ccgw)
284 {
285         struct nlattr *tb[CGW_MAX+1];
286         int modidx = 0;
287         int err = 0;
288
289         struct {
290                 struct can_frame cf;
291                 u8 modtype;
292         } __attribute__((packed)) mb;
293
294         BUILD_BUG_ON(sizeof(mb) != CGW_MODATTR_LEN);
295
296         memset(ccgw, 0, sizeof(*ccgw)); 
297
298         err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
299         if (err < 0)
300                 return err;
301
302         /* check for can_filter in attributes */
303         if (tb[CGW_FILTER] &&
304             nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
305                 nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
306                            sizeof(struct can_filter));
307
308         /* check for AND/OR/XOR/SET modifications */
309         if (tb[CGW_MOD_AND] &&
310             nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
311                 nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
312
313                 canframecpy(&ccgw->modframe.and, &mb.cf);
314
315                 if (mb.modtype & CGW_MOD_ID)
316                         ccgw->modfunc[modidx++] = mod_and_id;
317
318                 if (mb.modtype & CGW_MOD_DLC)
319                         ccgw->modfunc[modidx++] = mod_and_dlc;
320
321                 if (mb.modtype & CGW_MOD_DATA)
322                         ccgw->modfunc[modidx++] = mod_and_data;
323         }
324
325         if (tb[CGW_MOD_OR] &&
326             nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
327                 nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
328
329                 canframecpy(&ccgw->modframe.or, &mb.cf);
330
331                 if (mb.modtype & CGW_MOD_ID)
332                         ccgw->modfunc[modidx++] = mod_or_id;
333
334                 if (mb.modtype & CGW_MOD_DLC)
335                         ccgw->modfunc[modidx++] = mod_or_dlc;
336
337                 if (mb.modtype & CGW_MOD_DATA)
338                         ccgw->modfunc[modidx++] = mod_or_data;
339         }
340
341         if (tb[CGW_MOD_XOR] &&
342             nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
343                 nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
344
345                 canframecpy(&ccgw->modframe.xor, &mb.cf);
346
347                 if (mb.modtype & CGW_MOD_ID)
348                         ccgw->modfunc[modidx++] = mod_xor_id;
349
350                 if (mb.modtype & CGW_MOD_DLC)
351                         ccgw->modfunc[modidx++] = mod_xor_dlc;
352
353                 if (mb.modtype & CGW_MOD_DATA)
354                         ccgw->modfunc[modidx++] = mod_xor_data;
355         }
356
357         if (tb[CGW_MOD_SET] &&
358             nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
359                 nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
360
361                 canframecpy(&ccgw->modframe.set, &mb.cf);
362
363                 if (mb.modtype & CGW_MOD_ID)
364                         ccgw->modfunc[modidx++] = mod_set_id;
365
366                 if (mb.modtype & CGW_MOD_DLC)
367                         ccgw->modfunc[modidx++] = mod_set_dlc;
368
369                 if (mb.modtype & CGW_MOD_DATA)
370                         ccgw->modfunc[modidx++] = mod_set_data;
371         }
372
373         return 0;
374 }
375
376 static int gw_create_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
377 {
378         struct rtcanmsg *r;
379         struct gw_job *gwj;
380         int err = 0;
381
382         printk(KERN_INFO "%s: len %d attrlen %d\n", __FUNCTION__,
383                nlmsg_len(nlh), nlmsg_attrlen(nlh, sizeof(*r)));
384
385         if (nlmsg_len(nlh) < sizeof(*r))
386                 return -EINVAL;
387
388         r = nlmsg_data(nlh);
389         if (r->can_family != AF_CAN)
390                 return -EPFNOSUPPORT;
391
392         gwj = kmem_cache_alloc(gw_cache, GFP_KERNEL);
393         if (!gwj)
394                 return -ENOMEM;
395
396         gwj->src_dev = dev_get_by_index(&init_net, r->src_ifindex);
397         if (!gwj->src_dev) {
398                 err = -ENODEV;
399                 goto fail;
400         }
401
402         /* for now the source device needs to be a CAN device */
403         if (gwj->src_dev->type != ARPHRD_CAN) {
404                 err = -ENODEV;
405                 goto put_src_fail;
406         }
407
408         gwj->dst_dev = dev_get_by_index(&init_net, r->dst_ifindex);
409         if (!gwj->dst_dev) {
410                 err = -ENODEV;
411                 goto put_src_fail;
412         }
413
414         /* for now the destination device needs to be a CAN device */
415         if (gwj->dst_dev->type != ARPHRD_CAN) {
416                 err = -ENODEV;
417                 goto put_src_dst_fail;
418         }
419
420         gwj->flags = 0;
421
422         if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
423                 gwj->flags |= CAN_TX_ECHO;
424
425         if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
426                 gwj->flags |= CAN_TX_SRC_TSTAMP;
427
428         err = can_can_parse_attr(nlh, &gwj->ccgw);
429         if (err < 0)
430                 goto put_src_dst_fail;
431
432         spin_lock(&can_gw_list_lock);
433
434         err = can_gw_register_filter(gwj);
435         if (!err)
436                 hlist_add_head_rcu(&gwj->list, &can_gw_list);
437
438         spin_unlock(&can_gw_list_lock);
439         
440         dev_put(gwj->src_dev);
441         dev_put(gwj->dst_dev);
442
443         if (err)
444                 goto fail;
445
446         printk(KERN_INFO "%s: added entry %p\n", __FUNCTION__, gwj);
447
448         return 0;
449
450 put_src_dst_fail:
451         dev_put(gwj->dst_dev);
452 put_src_fail:
453         dev_put(gwj->src_dev);
454 fail:
455         kmem_cache_free(gw_cache, gwj);
456         return err;
457 }
458
459 static int gw_remove_job(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
460 {
461         struct gw_job *gwj = NULL;
462         struct hlist_node *n, *nx;
463         struct rtcanmsg *r;
464         struct can_can_gw ccgw;
465         u32 flags = 0;
466         int err = 0;
467
468         printk(KERN_INFO "%s: len %d attrlen %d\n", __FUNCTION__,
469                nlmsg_len(nlh), nlmsg_attrlen(nlh, sizeof(*r)));
470
471         if (nlmsg_len(nlh) < sizeof(*r))
472                 return -EINVAL;
473
474         r = nlmsg_data(nlh);
475         if (r->can_family != AF_CAN)
476                 return -EPFNOSUPPORT;
477
478         if (r->can_txflags & CAN_GW_TXFLAGS_ECHO)
479                 flags |= CAN_TX_ECHO;
480
481         if (r->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
482                 flags |= CAN_TX_SRC_TSTAMP;
483
484         err = can_can_parse_attr(nlh, &ccgw);
485         if (err < 0)
486                 return err;
487
488         err = -EINVAL;
489
490         spin_lock(&can_gw_list_lock);
491
492         /* remove only the first matching entry */
493         hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
494
495                 if (gwj->dst_dev->ifindex != r->dst_ifindex)
496                         continue;
497
498                 if (gwj->src_dev->ifindex != r->src_ifindex)
499                         continue;
500
501                 if (gwj->flags != flags)
502                         continue;
503
504                 if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
505                         continue;
506
507                 hlist_del(&gwj->list);
508                 can_gw_unregister_filter(gwj);
509                 kfree(gwj);
510                 err = 0;
511                 printk(KERN_INFO "%s: removed entry %p\n", __FUNCTION__, gwj);
512                 break;
513         }
514
515         spin_unlock(&can_gw_list_lock);
516         
517         return err;
518 }
519
520 static __init int gw_module_init(void)
521 {
522         printk(banner);
523
524         gw_cache = kmem_cache_create("can_gw", sizeof(struct gw_job),
525                                       0, 0, NULL);
526
527         if (!gw_cache)
528                 return -ENOMEM;
529
530         /* set notifier */
531         notifier.notifier_call = gw_notifier;
532         register_netdevice_notifier(&notifier);
533
534         if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, gw_dump_jobs)) {
535                 unregister_netdevice_notifier(&notifier);
536                 kmem_cache_destroy(gw_cache);
537                 return -ENOBUFS;
538         }
539
540         /* Only the first call to __rtnl_register can fail */
541         __rtnl_register(PF_CAN, RTM_NEWROUTE, gw_create_job, NULL);
542         __rtnl_register(PF_CAN, RTM_DELROUTE, gw_remove_job, NULL);
543
544         return 0;
545 }
546
547 static __exit void gw_module_exit(void)
548 {
549         struct gw_job *gwj = NULL;
550         struct hlist_node *n, *nx;
551
552         rtnl_unregister_all(PF_CAN);
553
554         unregister_netdevice_notifier(&notifier);
555
556         spin_lock(&can_gw_list_lock);
557
558         hlist_for_each_entry_safe(gwj, n, nx, &can_gw_list, list) {
559                 hlist_del(&gwj->list);
560                 can_gw_unregister_filter(gwj);
561                 kfree(gwj);
562         }
563
564         spin_unlock(&can_gw_list_lock);
565
566         rcu_barrier(); /* Wait for completion of call_rcu()'s */
567
568         kmem_cache_destroy(gw_cache);
569 }
570
571 module_init(gw_module_init);
572 module_exit(gw_module_exit);