blob: a78a320eee082802eebb99d0b468eda935160934 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
4 *
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.
9 *
10 * Module load syntax:
11 * insmod ip_conntrack_amanda.o [master_timeout=n]
12 *
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
17 * this value.
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/netfilter.h>
24#include <linux/ip.h>
25#include <linux/moduleparam.h>
26#include <net/checksum.h>
27#include <net/udp.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
30#include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
31
32static unsigned int master_timeout = 300;
33
34MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
35MODULE_DESCRIPTION("Amanda connection tracking module");
36MODULE_LICENSE("GPL");
37module_param(master_timeout, int, 0600);
38MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
39
40static char *conns[] = { "DATA ", "MESG ", "INDEX " };
41
42/* This is slow, but it's simple. --RR */
43static char amanda_buffer[65536];
Patrick McHardye45b1be2005-06-21 14:01:30 -070044static DEFINE_SPINLOCK(amanda_buffer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46unsigned 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);
51EXPORT_SYMBOL_GPL(ip_nat_amanda_hook);
52
53static int help(struct sk_buff **pskb,
54 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
55{
56 struct ip_conntrack_expect *exp;
57 char *data, *data_limit, *tmp;
58 unsigned int dataoff, i;
59 u_int16_t port, len;
60 int ret = NF_ACCEPT;
61
62 /* Only look at packets from the Amanda server */
63 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
64 return NF_ACCEPT;
65
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);
69
70 /* No data? */
71 dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
72 if (dataoff >= (*pskb)->len) {
73 if (net_ratelimit())
74 printk("amanda_help: skblen = %u\n", (*pskb)->len);
75 return NF_ACCEPT;
76 }
77
Patrick McHardye45b1be2005-06-21 14:01:30 -070078 spin_lock_bh(&amanda_buffer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 skb_copy_bits(*pskb, dataoff, amanda_buffer, (*pskb)->len - dataoff);
80 data = amanda_buffer;
81 data_limit = amanda_buffer + (*pskb)->len - dataoff;
82 *data_limit = '\0';
83
84 /* Search for the CONNECT string */
85 data = strstr(data, "CONNECT ");
86 if (!data)
87 goto out;
88 data += strlen("CONNECT ");
89
90 /* Only search first line. */
91 if ((tmp = strchr(data, '\n')))
92 *tmp = '\0';
93
94 for (i = 0; i < ARRAY_SIZE(conns); i++) {
95 char *match = strstr(data, conns[i]);
96 if (!match)
97 continue;
98 tmp = data = match + strlen(conns[i]);
99 port = simple_strtoul(data, &data, 10);
100 len = data - tmp;
101 if (port == 0 || len > 5)
102 break;
103
104 exp = ip_conntrack_expect_alloc();
105 if (exp == NULL) {
106 ret = NF_DROP;
107 goto out;
108 }
109
110 exp->expectfn = NULL;
111 exp->master = ct;
112
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);
118
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;
124
125 if (ip_nat_amanda_hook)
126 ret = ip_nat_amanda_hook(pskb, ctinfo,
127 tmp - amanda_buffer,
128 len, exp);
129 else if (ip_conntrack_expect_related(exp) != 0) {
130 ip_conntrack_expect_free(exp);
131 ret = NF_DROP;
132 }
133 }
134
135out:
Patrick McHardye45b1be2005-06-21 14:01:30 -0700136 spin_unlock_bh(&amanda_buffer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return ret;
138}
139
140static struct ip_conntrack_helper amanda_helper = {
141 .max_expected = ARRAY_SIZE(conns),
142 .timeout = 180,
143 .me = THIS_MODULE,
144 .help = help,
145 .name = "amanda",
146
147 .tuple = { .src = { .u = { __constant_htons(10080) } },
148 .dst = { .protonum = IPPROTO_UDP },
149 },
150 .mask = { .src = { .u = { 0xFFFF } },
151 .dst = { .protonum = 0xFF },
152 },
153};
154
155static void __exit fini(void)
156{
157 ip_conntrack_helper_unregister(&amanda_helper);
158}
159
160static int __init init(void)
161{
162 return ip_conntrack_helper_register(&amanda_helper);
163}
164
165module_init(init);
166module_exit(fini);