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