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
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.
11 * insmod ip_conntrack_amanda.o [master_timeout=n]
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
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>
27 #include <linux/udp.h>
29 #include <linux/netfilter.h>
30 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
31 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
33 static unsigned int master_timeout = 300;
34 static char *ts_algo = "kmp";
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)");
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);
86 static int help(struct sk_buff **pskb,
87 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
90 struct ip_conntrack_expect *exp;
91 unsigned int dataoff, start, stop, off, i;
92 char pbuf[sizeof("65535")], *tmp;
96 /* Only look at packets from the Amanda server */
97 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
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);
105 dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
106 if (dataoff >= (*pskb)->len) {
108 printk("amanda_help: skblen = %u\n", (*pskb)->len);
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)
117 start += dataoff + search[SEARCH_CONNECT].len;
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)
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);
131 off += start + search[i].len;
133 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
134 if (skb_copy_bits(*pskb, off, pbuf, len))
138 port = simple_strtoul(pbuf, &tmp, 10);
140 if (port == 0 || len > 5)
143 exp = ip_conntrack_expect_alloc(ct);
149 exp->expectfn = NULL;
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);
158 exp->mask.src.ip = htonl(0xFFFFFFFF);
159 exp->mask.src.u.tcp.port = 0;
160 exp->mask.dst.ip = htonl(0xFFFFFFFF);
161 exp->mask.dst.protonum = 0xFF;
162 exp->mask.dst.u.tcp.port = htons(0xFFFF);
164 if (ip_nat_amanda_hook)
165 ret = ip_nat_amanda_hook(pskb, ctinfo, off - dataoff,
167 else if (ip_conntrack_expect_related(exp) != 0)
169 ip_conntrack_expect_put(exp);
176 static struct ip_conntrack_helper amanda_helper = {
183 .tuple = { .src = { .u = { __constant_htons(10080) } },
184 .dst = { .protonum = IPPROTO_UDP },
186 .mask = { .src = { .u = { 0xFFFF } },
187 .dst = { .protonum = 0xFF },
191 static void __exit ip_conntrack_amanda_fini(void)
195 ip_conntrack_helper_unregister(&amanda_helper);
196 for (i = 0; i < ARRAY_SIZE(search); i++)
197 textsearch_destroy(search[i].ts);
200 static int __init ip_conntrack_amanda_init(void)
205 for (i = 0; i < ARRAY_SIZE(search); i++) {
206 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
208 GFP_KERNEL, TS_AUTOLOAD);
209 if (search[i].ts == NULL)
212 ret = ip_conntrack_helper_register(&amanda_helper);
218 for (; i >= 0; i--) {
220 textsearch_destroy(search[i].ts);
225 module_init(ip_conntrack_amanda_init);
226 module_exit(ip_conntrack_amanda_fini);