[NETFILTER]: x_tables: remove duplicate of xt_prefix
[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         typeof(ip_nat_amanda_hook) ip_nat_amanda;
96
97         /* Only look at packets from the Amanda server */
98         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
99                 return NF_ACCEPT;
100
101         /* increase the UDP timeout of the master connection as replies from
102          * Amanda clients to the server can be quite delayed */
103         ip_ct_refresh(ct, *pskb, master_timeout * HZ);
104
105         /* No data? */
106         dataoff = ip_hdrlen(*pskb) + sizeof(struct udphdr);
107         if (dataoff >= (*pskb)->len) {
108                 if (net_ratelimit())
109                         printk("amanda_help: skblen = %u\n", (*pskb)->len);
110                 return NF_ACCEPT;
111         }
112
113         memset(&ts, 0, sizeof(ts));
114         start = skb_find_text(*pskb, dataoff, (*pskb)->len,
115                               search[SEARCH_CONNECT].ts, &ts);
116         if (start == UINT_MAX)
117                 goto out;
118         start += dataoff + search[SEARCH_CONNECT].len;
119
120         memset(&ts, 0, sizeof(ts));
121         stop = skb_find_text(*pskb, start, (*pskb)->len,
122                              search[SEARCH_NEWLINE].ts, &ts);
123         if (stop == UINT_MAX)
124                 goto out;
125         stop += start;
126
127         for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) {
128                 memset(&ts, 0, sizeof(ts));
129                 off = skb_find_text(*pskb, start, stop, search[i].ts, &ts);
130                 if (off == UINT_MAX)
131                         continue;
132                 off += start + search[i].len;
133
134                 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
135                 if (skb_copy_bits(*pskb, off, pbuf, len))
136                         break;
137                 pbuf[len] = '\0';
138
139                 port = simple_strtoul(pbuf, &tmp, 10);
140                 len = tmp - pbuf;
141                 if (port == 0 || len > 5)
142                         break;
143
144                 exp = ip_conntrack_expect_alloc(ct);
145                 if (exp == NULL) {
146                         ret = NF_DROP;
147                         goto out;
148                 }
149
150                 exp->expectfn = NULL;
151                 exp->flags = 0;
152
153                 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
154                 exp->tuple.src.u.tcp.port = 0;
155                 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
156                 exp->tuple.dst.protonum = IPPROTO_TCP;
157                 exp->tuple.dst.u.tcp.port = htons(port);
158
159                 exp->mask.src.ip = htonl(0xFFFFFFFF);
160                 exp->mask.src.u.tcp.port = 0;
161                 exp->mask.dst.ip = htonl(0xFFFFFFFF);
162                 exp->mask.dst.protonum = 0xFF;
163                 exp->mask.dst.u.tcp.port = htons(0xFFFF);
164
165                 /* RCU read locked by nf_hook_slow */
166                 ip_nat_amanda = rcu_dereference(ip_nat_amanda_hook);
167                 if (ip_nat_amanda)
168                         ret = ip_nat_amanda(pskb, ctinfo, off - dataoff,
169                                             len, exp);
170                 else if (ip_conntrack_expect_related(exp) != 0)
171                         ret = NF_DROP;
172                 ip_conntrack_expect_put(exp);
173         }
174
175 out:
176         return ret;
177 }
178
179 static struct ip_conntrack_helper amanda_helper = {
180         .max_expected = 3,
181         .timeout = 180,
182         .me = THIS_MODULE,
183         .help = help,
184         .name = "amanda",
185
186         .tuple = { .src = { .u = { .udp = {.port = __constant_htons(10080) } } },
187                    .dst = { .protonum = IPPROTO_UDP },
188         },
189         .mask = { .src = { .u = { 0xFFFF } },
190                  .dst = { .protonum = 0xFF },
191         },
192 };
193
194 static void __exit ip_conntrack_amanda_fini(void)
195 {
196         int i;
197
198         ip_conntrack_helper_unregister(&amanda_helper);
199         for (i = 0; i < ARRAY_SIZE(search); i++)
200                 textsearch_destroy(search[i].ts);
201 }
202
203 static int __init ip_conntrack_amanda_init(void)
204 {
205         int ret, i;
206
207         ret = -ENOMEM;
208         for (i = 0; i < ARRAY_SIZE(search); i++) {
209                 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
210                                                   search[i].len,
211                                                   GFP_KERNEL, TS_AUTOLOAD);
212                 if (search[i].ts == NULL)
213                         goto err;
214         }
215         ret = ip_conntrack_helper_register(&amanda_helper);
216         if (ret < 0)
217                 goto err;
218         return 0;
219
220 err:
221         for (; i >= 0; i--) {
222                 if (search[i].ts)
223                         textsearch_destroy(search[i].ts);
224         }
225         return ret;
226 }
227
228 module_init(ip_conntrack_amanda_init);
229 module_exit(ip_conntrack_amanda_fini);