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