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 <net/checksum.h>
28 #include <linux/netfilter_ipv4/lockhelp.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_PARM(master_timeout, "i");
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[65536];
44 static DECLARE_LOCK(amanda_buffer_lock);
46 static int help(struct sk_buff *skb,
47 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
49 struct ip_conntrack_expect *exp;
50 struct ip_ct_amanda_expect *exp_amanda_info;
51 char *data, *data_limit, *tmp;
52 unsigned int dataoff, i;
55 /* Only look at packets from the Amanda server */
56 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
59 /* increase the UDP timeout of the master connection as replies from
60 * Amanda clients to the server can be quite delayed */
61 ip_ct_refresh(ct, master_timeout * HZ);
64 dataoff = skb->nh.iph->ihl*4 + sizeof(struct udphdr);
65 if (dataoff >= skb->len) {
67 printk("amanda_help: skblen = %u\n", skb->len);
71 LOCK_BH(&amanda_buffer_lock);
72 skb_copy_bits(skb, dataoff, amanda_buffer, skb->len - dataoff);
74 data_limit = amanda_buffer + skb->len - dataoff;
77 /* Search for the CONNECT string */
78 data = strstr(data, "CONNECT ");
81 data += strlen("CONNECT ");
83 /* Only search first line. */
84 if ((tmp = strchr(data, '\n')))
87 for (i = 0; i < ARRAY_SIZE(conns); i++) {
88 char *match = strstr(data, conns[i]);
91 tmp = data = match + strlen(conns[i]);
92 port = simple_strtoul(data, &data, 10);
94 if (port == 0 || len > 5)
97 exp = ip_conntrack_expect_alloc();
101 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
102 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
103 exp->tuple.dst.protonum = IPPROTO_TCP;
104 exp->mask.src.ip = 0xFFFFFFFF;
105 exp->mask.dst.ip = 0xFFFFFFFF;
106 exp->mask.dst.protonum = 0xFFFF;
107 exp->mask.dst.u.tcp.port = 0xFFFF;
109 exp_amanda_info = &exp->help.exp_amanda_info;
110 exp_amanda_info->offset = data - amanda_buffer;
111 exp_amanda_info->port = port;
112 exp_amanda_info->len = len;
114 exp->tuple.dst.u.tcp.port = htons(port);
116 ip_conntrack_expect_related(exp, ct);
120 UNLOCK_BH(&amanda_buffer_lock);
124 static struct ip_conntrack_helper amanda_helper = {
125 .max_expected = ARRAY_SIZE(conns),
127 .flags = IP_CT_HELPER_F_REUSE_EXPECT,
132 .tuple = { .src = { .u = { __constant_htons(10080) } },
133 .dst = { .protonum = IPPROTO_UDP },
135 .mask = { .src = { .u = { 0xFFFF } },
136 .dst = { .protonum = 0xFFFF },
140 static void __exit fini(void)
142 ip_conntrack_helper_unregister(&amanda_helper);
145 static int __init init(void)
147 return ip_conntrack_helper_register(&amanda_helper);
150 PROVIDES_CONNTRACK(amanda);