blob: 7b8239c0cd5eed58036362de1ea5caa2de085fa7 [file] [log] [blame]
Patrick McHardy16958902006-12-02 22:08:26 -08001/* Amanda extension for IP connection tracking
2 *
3 * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca>
4 * based on HW's ip_conntrack_irc.c as well as other modules
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/textsearch.h>
15#include <linux/skbuff.h>
16#include <linux/in.h>
17#include <linux/udp.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080018#include <linux/netfilter.h>
Patrick McHardy16958902006-12-02 22:08:26 -080019
20#include <net/netfilter/nf_conntrack.h>
21#include <net/netfilter/nf_conntrack_expect.h>
22#include <net/netfilter/nf_conntrack_ecache.h>
23#include <net/netfilter/nf_conntrack_helper.h>
24#include <linux/netfilter/nf_conntrack_amanda.h>
25
26static unsigned int master_timeout __read_mostly = 300;
27static char *ts_algo = "kmp";
28
29MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
30MODULE_DESCRIPTION("Amanda connection tracking module");
31MODULE_LICENSE("GPL");
32MODULE_ALIAS("ip_conntrack_amanda");
33
34module_param(master_timeout, uint, 0600);
35MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
36module_param(ts_algo, charp, 0400);
37MODULE_PARM_DESC(ts_algo, "textsearch algorithm to use (default kmp)");
38
Herbert Xu3db05fe2007-10-15 00:53:15 -070039unsigned int (*nf_nat_amanda_hook)(struct sk_buff *skb,
Patrick McHardy16958902006-12-02 22:08:26 -080040 enum ip_conntrack_info ctinfo,
41 unsigned int matchoff,
42 unsigned int matchlen,
43 struct nf_conntrack_expect *exp)
44 __read_mostly;
45EXPORT_SYMBOL_GPL(nf_nat_amanda_hook);
46
47enum amanda_strings {
48 SEARCH_CONNECT,
49 SEARCH_NEWLINE,
50 SEARCH_DATA,
51 SEARCH_MESG,
52 SEARCH_INDEX,
53};
54
55static struct {
56 char *string;
57 size_t len;
58 struct ts_config *ts;
59} search[] __read_mostly = {
60 [SEARCH_CONNECT] = {
61 .string = "CONNECT ",
62 .len = 8,
63 },
64 [SEARCH_NEWLINE] = {
65 .string = "\n",
66 .len = 1,
67 },
68 [SEARCH_DATA] = {
69 .string = "DATA ",
70 .len = 5,
71 },
72 [SEARCH_MESG] = {
73 .string = "MESG ",
74 .len = 5,
75 },
76 [SEARCH_INDEX] = {
77 .string = "INDEX ",
78 .len = 6,
79 },
80};
81
Herbert Xu3db05fe2007-10-15 00:53:15 -070082static int amanda_help(struct sk_buff *skb,
Patrick McHardy16958902006-12-02 22:08:26 -080083 unsigned int protoff,
84 struct nf_conn *ct,
85 enum ip_conntrack_info ctinfo)
86{
87 struct ts_state ts;
88 struct nf_conntrack_expect *exp;
89 struct nf_conntrack_tuple *tuple;
90 unsigned int dataoff, start, stop, off, i;
91 char pbuf[sizeof("65535")], *tmp;
92 u_int16_t len;
93 __be16 port;
94 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
95 int ret = NF_ACCEPT;
96 typeof(nf_nat_amanda_hook) nf_nat_amanda;
97
98 /* Only look at packets from the Amanda server */
99 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
100 return NF_ACCEPT;
101
102 /* increase the UDP timeout of the master connection as replies from
103 * Amanda clients to the server can be quite delayed */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700104 nf_ct_refresh(ct, skb, master_timeout * HZ);
Patrick McHardy16958902006-12-02 22:08:26 -0800105
106 /* No data? */
107 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700108 if (dataoff >= skb->len) {
Patrick McHardy16958902006-12-02 22:08:26 -0800109 if (net_ratelimit())
Herbert Xu3db05fe2007-10-15 00:53:15 -0700110 printk("amanda_help: skblen = %u\n", skb->len);
Patrick McHardy16958902006-12-02 22:08:26 -0800111 return NF_ACCEPT;
112 }
113
114 memset(&ts, 0, sizeof(ts));
Herbert Xu3db05fe2007-10-15 00:53:15 -0700115 start = skb_find_text(skb, dataoff, skb->len,
Patrick McHardy16958902006-12-02 22:08:26 -0800116 search[SEARCH_CONNECT].ts, &ts);
117 if (start == UINT_MAX)
118 goto out;
119 start += dataoff + search[SEARCH_CONNECT].len;
120
121 memset(&ts, 0, sizeof(ts));
Herbert Xu3db05fe2007-10-15 00:53:15 -0700122 stop = skb_find_text(skb, start, skb->len,
Patrick McHardy16958902006-12-02 22:08:26 -0800123 search[SEARCH_NEWLINE].ts, &ts);
124 if (stop == UINT_MAX)
125 goto out;
126 stop += start;
127
128 for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) {
129 memset(&ts, 0, sizeof(ts));
Herbert Xu3db05fe2007-10-15 00:53:15 -0700130 off = skb_find_text(skb, start, stop, search[i].ts, &ts);
Patrick McHardy16958902006-12-02 22:08:26 -0800131 if (off == UINT_MAX)
132 continue;
133 off += start + search[i].len;
134
135 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700136 if (skb_copy_bits(skb, off, pbuf, len))
Patrick McHardy16958902006-12-02 22:08:26 -0800137 break;
138 pbuf[len] = '\0';
139
140 port = htons(simple_strtoul(pbuf, &tmp, 10));
141 len = tmp - pbuf;
142 if (port == 0 || len > 5)
143 break;
144
Patrick McHardy68236452007-07-07 22:30:49 -0700145 exp = nf_ct_expect_alloc(ct);
Patrick McHardy16958902006-12-02 22:08:26 -0800146 if (exp == NULL) {
147 ret = NF_DROP;
148 goto out;
149 }
150 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
Patrick McHardy68236452007-07-07 22:30:49 -0700151 nf_ct_expect_init(exp, family, &tuple->src.u3, &tuple->dst.u3,
152 IPPROTO_TCP, NULL, &port);
Patrick McHardy16958902006-12-02 22:08:26 -0800153
154 nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook);
155 if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700156 ret = nf_nat_amanda(skb, ctinfo, off - dataoff,
Patrick McHardy16958902006-12-02 22:08:26 -0800157 len, exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700158 else if (nf_ct_expect_related(exp) != 0)
Patrick McHardy16958902006-12-02 22:08:26 -0800159 ret = NF_DROP;
Patrick McHardy68236452007-07-07 22:30:49 -0700160 nf_ct_expect_put(exp);
Patrick McHardy16958902006-12-02 22:08:26 -0800161 }
162
163out:
164 return ret;
165}
166
167static struct nf_conntrack_helper amanda_helper[2] __read_mostly = {
168 {
169 .name = "amanda",
170 .max_expected = 3,
171 .timeout = 180,
172 .me = THIS_MODULE,
173 .help = amanda_help,
174 .tuple.src.l3num = AF_INET,
175 .tuple.src.u.udp.port = __constant_htons(10080),
176 .tuple.dst.protonum = IPPROTO_UDP,
Patrick McHardy16958902006-12-02 22:08:26 -0800177 },
178 {
179 .name = "amanda",
180 .max_expected = 3,
181 .timeout = 180,
182 .me = THIS_MODULE,
183 .help = amanda_help,
184 .tuple.src.l3num = AF_INET6,
185 .tuple.src.u.udp.port = __constant_htons(10080),
186 .tuple.dst.protonum = IPPROTO_UDP,
Patrick McHardy16958902006-12-02 22:08:26 -0800187 },
188};
189
190static void __exit nf_conntrack_amanda_fini(void)
191{
192 int i;
193
194 nf_conntrack_helper_unregister(&amanda_helper[0]);
195 nf_conntrack_helper_unregister(&amanda_helper[1]);
196 for (i = 0; i < ARRAY_SIZE(search); i++)
197 textsearch_destroy(search[i].ts);
198}
199
200static int __init nf_conntrack_amanda_init(void)
201{
202 int ret, i;
203
Patrick McHardy16958902006-12-02 22:08:26 -0800204 for (i = 0; i < ARRAY_SIZE(search); i++) {
205 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
206 search[i].len,
207 GFP_KERNEL, TS_AUTOLOAD);
Akinobu Mitac764c9a2007-06-05 12:56:53 -0700208 if (IS_ERR(search[i].ts)) {
209 ret = PTR_ERR(search[i].ts);
Patrick McHardy16958902006-12-02 22:08:26 -0800210 goto err1;
Akinobu Mitac764c9a2007-06-05 12:56:53 -0700211 }
Patrick McHardy16958902006-12-02 22:08:26 -0800212 }
213 ret = nf_conntrack_helper_register(&amanda_helper[0]);
214 if (ret < 0)
215 goto err1;
216 ret = nf_conntrack_helper_register(&amanda_helper[1]);
217 if (ret < 0)
218 goto err2;
219 return 0;
220
221err2:
222 nf_conntrack_helper_unregister(&amanda_helper[0]);
223err1:
Akinobu Mitac764c9a2007-06-05 12:56:53 -0700224 while (--i >= 0)
225 textsearch_destroy(search[i].ts);
226
Patrick McHardy16958902006-12-02 22:08:26 -0800227 return ret;
228}
229
230module_init(nf_conntrack_amanda_init);
231module_exit(nf_conntrack_amanda_fini);