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
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/netfilter.h>
26 #include <linux/moduleparam.h>
27 #include <linux/udp.h>
28 #include <net/checksum.h>
31 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
32 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
34 static unsigned int master_timeout = 300;
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");
42 static const char *conns[] = { "DATA ", "MESG ", "INDEX " };
44 /* This is slow, but it's simple. --RR */
45 static char *amanda_buffer;
46 static DEFINE_SPINLOCK(amanda_buffer_lock);
48 unsigned int (*ip_nat_amanda_hook)(struct sk_buff **pskb,
49 enum ip_conntrack_info ctinfo,
50 unsigned int matchoff,
51 unsigned int matchlen,
52 struct ip_conntrack_expect *exp);
53 EXPORT_SYMBOL_GPL(ip_nat_amanda_hook);
55 static int help(struct sk_buff **pskb,
56 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
58 struct ip_conntrack_expect *exp;
59 char *data, *data_limit, *tmp;
60 unsigned int dataoff, i;
64 /* Only look at packets from the Amanda server */
65 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
68 /* increase the UDP timeout of the master connection as replies from
69 * Amanda clients to the server can be quite delayed */
70 ip_ct_refresh(ct, *pskb, master_timeout * HZ);
73 dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
74 if (dataoff >= (*pskb)->len) {
76 printk("amanda_help: skblen = %u\n", (*pskb)->len);
80 spin_lock_bh(&amanda_buffer_lock);
81 skb_copy_bits(*pskb, dataoff, amanda_buffer, (*pskb)->len - dataoff);
83 data_limit = amanda_buffer + (*pskb)->len - dataoff;
86 /* Search for the CONNECT string */
87 data = strstr(data, "CONNECT ");
90 data += strlen("CONNECT ");
92 /* Only search first line. */
93 if ((tmp = strchr(data, '\n')))
96 for (i = 0; i < ARRAY_SIZE(conns); i++) {
97 char *match = strstr(data, conns[i]);
100 tmp = data = match + strlen(conns[i]);
101 port = simple_strtoul(data, &data, 10);
103 if (port == 0 || len > 5)
106 exp = ip_conntrack_expect_alloc(ct);
112 exp->expectfn = NULL;
115 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
116 exp->tuple.src.u.tcp.port = 0;
117 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
118 exp->tuple.dst.protonum = IPPROTO_TCP;
119 exp->tuple.dst.u.tcp.port = htons(port);
121 exp->mask.src.ip = 0xFFFFFFFF;
122 exp->mask.src.u.tcp.port = 0;
123 exp->mask.dst.ip = 0xFFFFFFFF;
124 exp->mask.dst.protonum = 0xFF;
125 exp->mask.dst.u.tcp.port = 0xFFFF;
127 if (ip_nat_amanda_hook)
128 ret = ip_nat_amanda_hook(pskb, ctinfo,
131 else if (ip_conntrack_expect_related(exp) != 0)
133 ip_conntrack_expect_put(exp);
137 spin_unlock_bh(&amanda_buffer_lock);
141 static struct ip_conntrack_helper amanda_helper = {
142 .max_expected = ARRAY_SIZE(conns),
148 .tuple = { .src = { .u = { __constant_htons(10080) } },
149 .dst = { .protonum = IPPROTO_UDP },
151 .mask = { .src = { .u = { 0xFFFF } },
152 .dst = { .protonum = 0xFF },
156 static void __exit fini(void)
158 ip_conntrack_helper_unregister(&amanda_helper);
159 kfree(amanda_buffer);
162 static int __init init(void)
166 amanda_buffer = kmalloc(65536, GFP_KERNEL);
170 ret = ip_conntrack_helper_register(&amanda_helper);
172 kfree(amanda_buffer);