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
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/netfilter.h>
25 #include <linux/moduleparam.h>
26 #include <net/checksum.h>
29 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
30 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
32 static unsigned int master_timeout = 300;
34 MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
35 MODULE_DESCRIPTION("Amanda connection tracking module");
36 MODULE_LICENSE("GPL");
37 module_param(master_timeout, int, 0600);
38 MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
40 static char *conns[] = { "DATA ", "MESG ", "INDEX " };
42 /* This is slow, but it's simple. --RR */
43 static char *amanda_buffer;
44 static DEFINE_SPINLOCK(amanda_buffer_lock);
46 unsigned int (*ip_nat_amanda_hook)(struct sk_buff **pskb,
47 enum ip_conntrack_info ctinfo,
48 unsigned int matchoff,
49 unsigned int matchlen,
50 struct ip_conntrack_expect *exp);
51 EXPORT_SYMBOL_GPL(ip_nat_amanda_hook);
53 static int help(struct sk_buff **pskb,
54 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
56 struct ip_conntrack_expect *exp;
57 char *data, *data_limit, *tmp;
58 unsigned int dataoff, i;
62 /* Only look at packets from the Amanda server */
63 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
66 /* increase the UDP timeout of the master connection as replies from
67 * Amanda clients to the server can be quite delayed */
68 ip_ct_refresh_acct(ct, ctinfo, NULL, master_timeout * HZ);
71 dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
72 if (dataoff >= (*pskb)->len) {
74 printk("amanda_help: skblen = %u\n", (*pskb)->len);
78 spin_lock_bh(&amanda_buffer_lock);
79 skb_copy_bits(*pskb, dataoff, amanda_buffer, (*pskb)->len - dataoff);
81 data_limit = amanda_buffer + (*pskb)->len - dataoff;
84 /* Search for the CONNECT string */
85 data = strstr(data, "CONNECT ");
88 data += strlen("CONNECT ");
90 /* Only search first line. */
91 if ((tmp = strchr(data, '\n')))
94 for (i = 0; i < ARRAY_SIZE(conns); i++) {
95 char *match = strstr(data, conns[i]);
98 tmp = data = match + strlen(conns[i]);
99 port = simple_strtoul(data, &data, 10);
101 if (port == 0 || len > 5)
104 exp = ip_conntrack_expect_alloc(ct);
110 exp->expectfn = NULL;
113 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
114 exp->tuple.src.u.tcp.port = 0;
115 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
116 exp->tuple.dst.protonum = IPPROTO_TCP;
117 exp->tuple.dst.u.tcp.port = htons(port);
119 exp->mask.src.ip = 0xFFFFFFFF;
120 exp->mask.src.u.tcp.port = 0;
121 exp->mask.dst.ip = 0xFFFFFFFF;
122 exp->mask.dst.protonum = 0xFF;
123 exp->mask.dst.u.tcp.port = 0xFFFF;
125 if (ip_nat_amanda_hook)
126 ret = ip_nat_amanda_hook(pskb, ctinfo,
129 else if (ip_conntrack_expect_related(exp) != 0)
131 ip_conntrack_expect_put(exp);
135 spin_unlock_bh(&amanda_buffer_lock);
139 static struct ip_conntrack_helper amanda_helper = {
140 .max_expected = ARRAY_SIZE(conns),
146 .tuple = { .src = { .u = { __constant_htons(10080) } },
147 .dst = { .protonum = IPPROTO_UDP },
149 .mask = { .src = { .u = { 0xFFFF } },
150 .dst = { .protonum = 0xFF },
154 static void __exit fini(void)
156 ip_conntrack_helper_unregister(&amanda_helper);
157 kfree(amanda_buffer);
160 static int __init init(void)
164 amanda_buffer = kmalloc(65536, GFP_KERNEL);
168 ret = ip_conntrack_helper_register(&amanda_helper);
170 kfree(amanda_buffer);