Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / net / netfilter / nf_conntrack_amanda.c
1 /* Amanda extension for IP connection tracking
2  *
3  * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca>
4  * based on HW's ip_conntrack_irc.c as well as other modules
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/textsearch.h>
15 #include <linux/skbuff.h>
16 #include <linux/in.h>
17 #include <linux/udp.h>
18 #include <linux/netfilter.h>
19
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_expect.h>
22 #include <net/netfilter/nf_conntrack_ecache.h>
23 #include <net/netfilter/nf_conntrack_helper.h>
24 #include <linux/netfilter/nf_conntrack_amanda.h>
25
26 static unsigned int master_timeout __read_mostly = 300;
27 static char *ts_algo = "kmp";
28
29 MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
30 MODULE_DESCRIPTION("Amanda connection tracking module");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("ip_conntrack_amanda");
33
34 module_param(master_timeout, uint, 0600);
35 MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
36 module_param(ts_algo, charp, 0400);
37 MODULE_PARM_DESC(ts_algo, "textsearch algorithm to use (default kmp)");
38
39 unsigned int (*nf_nat_amanda_hook)(struct sk_buff **pskb,
40                                    enum ip_conntrack_info ctinfo,
41                                    unsigned int matchoff,
42                                    unsigned int matchlen,
43                                    struct nf_conntrack_expect *exp)
44                                    __read_mostly;
45 EXPORT_SYMBOL_GPL(nf_nat_amanda_hook);
46
47 enum amanda_strings {
48         SEARCH_CONNECT,
49         SEARCH_NEWLINE,
50         SEARCH_DATA,
51         SEARCH_MESG,
52         SEARCH_INDEX,
53 };
54
55 static struct {
56         char                    *string;
57         size_t                  len;
58         struct ts_config        *ts;
59 } search[] __read_mostly = {
60         [SEARCH_CONNECT] = {
61                 .string = "CONNECT ",
62                 .len    = 8,
63         },
64         [SEARCH_NEWLINE] = {
65                 .string = "\n",
66                 .len    = 1,
67         },
68         [SEARCH_DATA] = {
69                 .string = "DATA ",
70                 .len    = 5,
71         },
72         [SEARCH_MESG] = {
73                 .string = "MESG ",
74                 .len    = 5,
75         },
76         [SEARCH_INDEX] = {
77                 .string = "INDEX ",
78                 .len    = 6,
79         },
80 };
81
82 static int amanda_help(struct sk_buff **pskb,
83                        unsigned int protoff,
84                        struct nf_conn *ct,
85                        enum ip_conntrack_info ctinfo)
86 {
87         struct ts_state ts;
88         struct nf_conntrack_expect *exp;
89         struct nf_conntrack_tuple *tuple;
90         unsigned int dataoff, start, stop, off, i;
91         char pbuf[sizeof("65535")], *tmp;
92         u_int16_t len;
93         __be16 port;
94         int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
95         int ret = NF_ACCEPT;
96         typeof(nf_nat_amanda_hook) nf_nat_amanda;
97
98         /* Only look at packets from the Amanda server */
99         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
100                 return NF_ACCEPT;
101
102         /* increase the UDP timeout of the master connection as replies from
103          * Amanda clients to the server can be quite delayed */
104         nf_ct_refresh(ct, *pskb, master_timeout * HZ);
105
106         /* No data? */
107         dataoff = protoff + sizeof(struct udphdr);
108         if (dataoff >= (*pskb)->len) {
109                 if (net_ratelimit())
110                         printk("amanda_help: skblen = %u\n", (*pskb)->len);
111                 return NF_ACCEPT;
112         }
113
114         memset(&ts, 0, sizeof(ts));
115         start = skb_find_text(*pskb, dataoff, (*pskb)->len,
116                               search[SEARCH_CONNECT].ts, &ts);
117         if (start == UINT_MAX)
118                 goto out;
119         start += dataoff + search[SEARCH_CONNECT].len;
120
121         memset(&ts, 0, sizeof(ts));
122         stop = skb_find_text(*pskb, start, (*pskb)->len,
123                              search[SEARCH_NEWLINE].ts, &ts);
124         if (stop == UINT_MAX)
125                 goto out;
126         stop += start;
127
128         for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) {
129                 memset(&ts, 0, sizeof(ts));
130                 off = skb_find_text(*pskb, start, stop, search[i].ts, &ts);
131                 if (off == UINT_MAX)
132                         continue;
133                 off += start + search[i].len;
134
135                 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
136                 if (skb_copy_bits(*pskb, off, pbuf, len))
137                         break;
138                 pbuf[len] = '\0';
139
140                 port = htons(simple_strtoul(pbuf, &tmp, 10));
141                 len = tmp - pbuf;
142                 if (port == 0 || len > 5)
143                         break;
144
145                 exp = nf_conntrack_expect_alloc(ct);
146                 if (exp == NULL) {
147                         ret = NF_DROP;
148                         goto out;
149                 }
150                 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
151                 nf_conntrack_expect_init(exp, family,
152                                          &tuple->src.u3, &tuple->dst.u3,
153                                          IPPROTO_TCP, NULL, &port);
154
155                 nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook);
156                 if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
157                         ret = nf_nat_amanda(pskb, ctinfo, off - dataoff,
158                                             len, exp);
159                 else if (nf_conntrack_expect_related(exp) != 0)
160                         ret = NF_DROP;
161                 nf_conntrack_expect_put(exp);
162         }
163
164 out:
165         return ret;
166 }
167
168 static struct nf_conntrack_helper amanda_helper[2] __read_mostly = {
169         {
170                 .name                   = "amanda",
171                 .max_expected           = 3,
172                 .timeout                = 180,
173                 .me                     = THIS_MODULE,
174                 .help                   = amanda_help,
175                 .tuple.src.l3num        = AF_INET,
176                 .tuple.src.u.udp.port   = __constant_htons(10080),
177                 .tuple.dst.protonum     = IPPROTO_UDP,
178                 .mask.src.l3num         = 0xFFFF,
179                 .mask.src.u.udp.port    = __constant_htons(0xFFFF),
180                 .mask.dst.protonum      = 0xFF,
181         },
182         {
183                 .name                   = "amanda",
184                 .max_expected           = 3,
185                 .timeout                = 180,
186                 .me                     = THIS_MODULE,
187                 .help                   = amanda_help,
188                 .tuple.src.l3num        = AF_INET6,
189                 .tuple.src.u.udp.port   = __constant_htons(10080),
190                 .tuple.dst.protonum     = IPPROTO_UDP,
191                 .mask.src.l3num         = 0xFFFF,
192                 .mask.src.u.udp.port    = __constant_htons(0xFFFF),
193                 .mask.dst.protonum      = 0xFF,
194         },
195 };
196
197 static void __exit nf_conntrack_amanda_fini(void)
198 {
199         int i;
200
201         nf_conntrack_helper_unregister(&amanda_helper[0]);
202         nf_conntrack_helper_unregister(&amanda_helper[1]);
203         for (i = 0; i < ARRAY_SIZE(search); i++)
204                 textsearch_destroy(search[i].ts);
205 }
206
207 static int __init nf_conntrack_amanda_init(void)
208 {
209         int ret, i;
210
211         ret = -ENOMEM;
212         for (i = 0; i < ARRAY_SIZE(search); i++) {
213                 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
214                                                   search[i].len,
215                                                   GFP_KERNEL, TS_AUTOLOAD);
216                 if (search[i].ts == NULL)
217                         goto err1;
218         }
219         ret = nf_conntrack_helper_register(&amanda_helper[0]);
220         if (ret < 0)
221                 goto err1;
222         ret = nf_conntrack_helper_register(&amanda_helper[1]);
223         if (ret < 0)
224                 goto err2;
225         return 0;
226
227 err2:
228         nf_conntrack_helper_unregister(&amanda_helper[0]);
229 err1:
230         for (; i >= 0; i--) {
231                 if (search[i].ts)
232                         textsearch_destroy(search[i].ts);
233         }
234         return ret;
235 }
236
237 module_init(nf_conntrack_amanda_init);
238 module_exit(nf_conntrack_amanda_fini);