Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / net / netfilter / nf_conntrack_slp.c
1 /*
2  *      NetBIOS name service broadcast connection tracking helper
3  *
4  *      (c) 2007 Jiri Bohac <jbohac@suse.cz>
5  *      (c) 2005 Patrick McHardy <kaber@trash.net>
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12 /*
13  *      This helper tracks locally originating NetBIOS name service
14  *      requests by issuing permanent expectations (valid until
15  *      timing out) matching all reply connections from the
16  *      destination network. The only NetBIOS specific thing is
17  *      actually the port number.
18  */
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
24 #include <linux/inetdevice.h>
25 #include <linux/if_addr.h>
26 #include <linux/in.h>
27 #include <linux/ip.h>
28 #include <linux/netfilter.h>
29 #include <net/route.h>
30
31 #include <net/netfilter/nf_conntrack.h>
32 #include <net/netfilter/nf_conntrack_helper.h>
33 #include <net/netfilter/nf_conntrack_expect.h>
34
35 #define SLP_PORT        427
36
37 MODULE_AUTHOR("Jiri Bohac <jbohac@suse.cz>");
38 MODULE_DESCRIPTION("SLP broadcast connection tracking helper");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("ip_conntrack_slp");
41
42 static unsigned int timeout __read_mostly = 3;
43 module_param(timeout, uint, 0400);
44 MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds");
45
46 static int help(struct sk_buff *skb, unsigned int protoff,
47                 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
48 {
49         struct nf_conntrack_expect *exp;
50         struct rtable *rt = skb_rtable(skb);
51         struct in_device *in_dev;
52         __be32 mask = 0;
53         __be32 src = 0;
54
55         /* we're only interested in locally generated packets */
56         if (skb->sk == NULL)
57                 goto out;
58         if (rt == NULL || !(rt->rt_flags & (RTCF_MULTICAST|RTCF_BROADCAST)))
59                 goto out;
60         if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
61                 goto out;
62
63         rcu_read_lock();
64         in_dev = __in_dev_get_rcu(rt->dst.dev);
65         if (in_dev != NULL) {
66                 for_primary_ifa(in_dev) {
67                         /* this is a hack as slp uses multicast we can't match
68                          * the destination address to some broadcast address. So
69                          * just take the first one. Better would be to install
70                          * expectations for all addresses */
71                         mask = ifa->ifa_mask;
72                         src = ifa->ifa_broadcast;
73                         break;
74                 } endfor_ifa(in_dev);
75         }
76         rcu_read_unlock();
77
78         if (mask == 0 || src == 0)
79                 goto out;
80
81         exp = nf_ct_expect_alloc(ct);
82         if (exp == NULL)
83                 goto out;
84
85         exp->tuple                = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
86         exp->tuple.src.u3.ip      = src;
87         exp->tuple.src.u.udp.port = htons(SLP_PORT);
88
89         exp->mask.src.u3.ip       = mask;
90         exp->mask.src.u.udp.port  = htons(0xFFFF);
91
92         exp->expectfn             = NULL;
93         exp->flags                = NF_CT_EXPECT_PERMANENT;
94         exp->class                = NF_CT_EXPECT_CLASS_DEFAULT;
95         exp->helper               = NULL;
96
97         nf_ct_expect_related(exp);
98         nf_ct_expect_put(exp);
99
100         nf_ct_refresh(ct, skb, timeout * HZ);
101 out:
102         return NF_ACCEPT;
103 }
104
105 static struct nf_conntrack_expect_policy exp_policy = {
106         .max_expected   = 1,
107 };
108
109 static struct nf_conntrack_helper helper __read_mostly = {
110         .name                   = "slp",
111         .tuple.src.l3num        = AF_INET,
112         .tuple.src.u.udp.port   = __constant_htons(SLP_PORT),
113         .tuple.dst.protonum     = IPPROTO_UDP,
114         .me                     = THIS_MODULE,
115         .help                   = help,
116         .expect_policy          = &exp_policy,
117 };
118
119 static int __init nf_conntrack_slp_init(void)
120 {
121         exp_policy.timeout = timeout;
122         return nf_conntrack_helper_register(&helper);
123 }
124
125 static void __exit nf_conntrack_slp_fini(void)
126 {
127         nf_conntrack_helper_unregister(&helper);
128 }
129
130 module_init(nf_conntrack_slp_init);
131 module_exit(nf_conntrack_slp_fini);