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 | ||
9b5b5cff | 40 | static const char *conns[] = { "DATA ", "MESG ", "INDEX " }; |
1da177e4 LT |
41 | |
42 | /* This is slow, but it's simple. --RR */ | |
2669d63d | 43 | static char *amanda_buffer; |
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 */ | |
1dfbab59 | 68 | ip_ct_refresh(ct, *pskb, master_timeout * HZ); |
1da177e4 LT |
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; | |
2248bcfc | 111 | exp->flags = 0; |
1da177e4 LT |
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); | |
4acdbdbe | 129 | else if (ip_conntrack_expect_related(exp) != 0) |
1da177e4 | 130 | ret = NF_DROP; |
4acdbdbe | 131 | ip_conntrack_expect_put(exp); |
1da177e4 LT |
132 | } |
133 | ||
134 | out: | |
e45b1be8 | 135 | spin_unlock_bh(&amanda_buffer_lock); |
1da177e4 LT |
136 | return ret; |
137 | } | |
138 | ||
139 | static struct ip_conntrack_helper amanda_helper = { | |
140 | .max_expected = ARRAY_SIZE(conns), | |
141 | .timeout = 180, | |
142 | .me = THIS_MODULE, | |
143 | .help = help, | |
144 | .name = "amanda", | |
145 | ||
146 | .tuple = { .src = { .u = { __constant_htons(10080) } }, | |
147 | .dst = { .protonum = IPPROTO_UDP }, | |
148 | }, | |
149 | .mask = { .src = { .u = { 0xFFFF } }, | |
150 | .dst = { .protonum = 0xFF }, | |
151 | }, | |
152 | }; | |
153 | ||
154 | static void __exit fini(void) | |
155 | { | |
156 | ip_conntrack_helper_unregister(&amanda_helper); | |
2669d63d | 157 | kfree(amanda_buffer); |
1da177e4 LT |
158 | } |
159 | ||
160 | static int __init init(void) | |
161 | { | |
2669d63d HW |
162 | int ret; |
163 | ||
164 | amanda_buffer = kmalloc(65536, GFP_KERNEL); | |
165 | if (!amanda_buffer) | |
166 | return -ENOMEM; | |
167 | ||
168 | ret = ip_conntrack_helper_register(&amanda_helper); | |
169 | if (ret < 0) { | |
170 | kfree(amanda_buffer); | |
171 | return ret; | |
172 | } | |
173 | return 0; | |
174 | ||
175 | ||
1da177e4 LT |
176 | } |
177 | ||
178 | module_init(init); | |
179 | module_exit(fini); |