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