1 /* Amanda extension for IP connection tracking
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>
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.
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>
18 #include <linux/udp.h>
19 #include <linux/netfilter.h>
20 #include <linux/gfp.h>
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>
28 static unsigned int master_timeout __read_mostly = 300;
29 static char *ts_algo = "kmp";
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");
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)");
42 unsigned int (*nf_nat_amanda_hook)(struct sk_buff *skb,
43 enum ip_conntrack_info ctinfo,
45 unsigned int matchoff,
46 unsigned int matchlen,
47 struct nf_conntrack_expect *exp)
49 EXPORT_SYMBOL_GPL(nf_nat_amanda_hook);
63 } search[] __read_mostly = {
86 static int amanda_help(struct sk_buff *skb,
89 enum ip_conntrack_info ctinfo)
92 struct nf_conntrack_expect *exp;
93 struct nf_conntrack_tuple *tuple;
94 unsigned int dataoff, start, stop, off, i;
95 char pbuf[sizeof("65535")], *tmp;
99 typeof(nf_nat_amanda_hook) nf_nat_amanda;
101 /* Only look at packets from the Amanda server */
102 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
105 /* increase the UDP timeout of the master connection as replies from
106 * Amanda clients to the server can be quite delayed */
107 nf_ct_refresh(ct, skb, master_timeout * HZ);
110 dataoff = protoff + sizeof(struct udphdr);
111 if (dataoff >= skb->len) {
112 net_err_ratelimited("amanda_help: skblen = %u\n", skb->len);
116 memset(&ts, 0, sizeof(ts));
117 start = skb_find_text(skb, dataoff, skb->len,
118 search[SEARCH_CONNECT].ts, &ts);
119 if (start == UINT_MAX)
121 start += dataoff + search[SEARCH_CONNECT].len;
123 memset(&ts, 0, sizeof(ts));
124 stop = skb_find_text(skb, start, skb->len,
125 search[SEARCH_NEWLINE].ts, &ts);
126 if (stop == UINT_MAX)
130 for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) {
131 memset(&ts, 0, sizeof(ts));
132 off = skb_find_text(skb, start, stop, search[i].ts, &ts);
135 off += start + search[i].len;
137 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
138 if (skb_copy_bits(skb, off, pbuf, len))
142 port = htons(simple_strtoul(pbuf, &tmp, 10));
144 if (port == 0 || len > 5)
147 exp = nf_ct_expect_alloc(ct);
149 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
153 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
154 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
156 &tuple->src.u3, &tuple->dst.u3,
157 IPPROTO_TCP, NULL, &port);
159 nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook);
160 if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
161 ret = nf_nat_amanda(skb, ctinfo, protoff,
162 off - dataoff, len, exp);
163 else if (nf_ct_expect_related(exp) != 0) {
164 nf_ct_helper_log(skb, ct, "cannot add expectation");
167 nf_ct_expect_put(exp);
174 static const struct nf_conntrack_expect_policy amanda_exp_policy = {
179 static struct nf_conntrack_helper amanda_helper[2] __read_mostly = {
184 .tuple.src.l3num = AF_INET,
185 .tuple.src.u.udp.port = cpu_to_be16(10080),
186 .tuple.dst.protonum = IPPROTO_UDP,
187 .expect_policy = &amanda_exp_policy,
193 .tuple.src.l3num = AF_INET6,
194 .tuple.src.u.udp.port = cpu_to_be16(10080),
195 .tuple.dst.protonum = IPPROTO_UDP,
196 .expect_policy = &amanda_exp_policy,
200 static void __exit nf_conntrack_amanda_fini(void)
204 nf_conntrack_helper_unregister(&amanda_helper[0]);
205 nf_conntrack_helper_unregister(&amanda_helper[1]);
206 for (i = 0; i < ARRAY_SIZE(search); i++)
207 textsearch_destroy(search[i].ts);
210 static int __init nf_conntrack_amanda_init(void)
214 for (i = 0; i < ARRAY_SIZE(search); i++) {
215 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
217 GFP_KERNEL, TS_AUTOLOAD);
218 if (IS_ERR(search[i].ts)) {
219 ret = PTR_ERR(search[i].ts);
223 ret = nf_conntrack_helper_register(&amanda_helper[0]);
226 ret = nf_conntrack_helper_register(&amanda_helper[1]);
232 nf_conntrack_helper_unregister(&amanda_helper[0]);
235 textsearch_destroy(search[i].ts);
240 module_init(nf_conntrack_amanda_init);
241 module_exit(nf_conntrack_amanda_fini);