Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
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 | ||
1da177e4 LT |
29 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> |
30 | #include <linux/netfilter_ipv4/ip_conntrack_amanda.h> | |
31 | ||
32 | static unsigned int master_timeout = 300; | |
33 | ||
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"); | |
39 | ||
40 | static char *conns[] = { "DATA ", "MESG ", "INDEX " }; | |
41 | ||
42 | /* This is slow, but it's simple. --RR */ | |
43 | static char amanda_buffer[65536]; | |
e45b1be8 | 44 | static DEFINE_SPINLOCK(amanda_buffer_lock); |
1da177e4 LT |
45 | |
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); | |
52 | ||
53 | static 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 | ||
e45b1be8 | 78 | spin_lock_bh(&amanda_buffer_lock); |
1da177e4 LT |
79 | 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 | ||
4acdbdbe | 104 | exp = ip_conntrack_expect_alloc(ct); |
1da177e4 LT |
105 | if (exp == NULL) { |
106 | ret = NF_DROP; | |
107 | goto out; | |
108 | } | |
109 | ||
110 | exp->expectfn = NULL; | |
1da177e4 LT |
111 | |
112 | exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip; | |
113 | exp->tuple.src.u.tcp.port = 0; | |
114 | exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip; | |
115 | exp->tuple.dst.protonum = IPPROTO_TCP; | |
116 | exp->tuple.dst.u.tcp.port = htons(port); | |
117 | ||
118 | exp->mask.src.ip = 0xFFFFFFFF; | |
119 | exp->mask.src.u.tcp.port = 0; | |
120 | exp->mask.dst.ip = 0xFFFFFFFF; | |
121 | exp->mask.dst.protonum = 0xFF; | |
122 | exp->mask.dst.u.tcp.port = 0xFFFF; | |
123 | ||
124 | if (ip_nat_amanda_hook) | |
125 | ret = ip_nat_amanda_hook(pskb, ctinfo, | |
126 | tmp - amanda_buffer, | |
127 | len, exp); | |
4acdbdbe | 128 | else if (ip_conntrack_expect_related(exp) != 0) |
1da177e4 | 129 | ret = NF_DROP; |
4acdbdbe | 130 | ip_conntrack_expect_put(exp); |
1da177e4 LT |
131 | } |
132 | ||
133 | out: | |
e45b1be8 | 134 | spin_unlock_bh(&amanda_buffer_lock); |
1da177e4 LT |
135 | return ret; |
136 | } | |
137 | ||
138 | static struct ip_conntrack_helper amanda_helper = { | |
139 | .max_expected = ARRAY_SIZE(conns), | |
140 | .timeout = 180, | |
141 | .me = THIS_MODULE, | |
142 | .help = help, | |
143 | .name = "amanda", | |
144 | ||
145 | .tuple = { .src = { .u = { __constant_htons(10080) } }, | |
146 | .dst = { .protonum = IPPROTO_UDP }, | |
147 | }, | |
148 | .mask = { .src = { .u = { 0xFFFF } }, | |
149 | .dst = { .protonum = 0xFF }, | |
150 | }, | |
151 | }; | |
152 | ||
153 | static void __exit fini(void) | |
154 | { | |
155 | ip_conntrack_helper_unregister(&amanda_helper); | |
156 | } | |
157 | ||
158 | static int __init init(void) | |
159 | { | |
160 | return ip_conntrack_helper_register(&amanda_helper); | |
161 | } | |
162 | ||
163 | module_init(init); | |
164 | module_exit(fini); |