netfilter: use macros to create module aliases.
[linux-block.git] / net / netfilter / nf_conntrack_amanda.c
CommitLineData
16958900
PM
1/* 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
f229f6ce 5 * (C) 2006 Patrick McHardy <kaber@trash.net>
16958900
PM
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/textsearch.h>
16#include <linux/skbuff.h>
17#include <linux/in.h>
18#include <linux/udp.h>
1863f096 19#include <linux/netfilter.h>
5a0e3ad6 20#include <linux/gfp.h>
16958900
PM
21
22#include <net/netfilter/nf_conntrack.h>
23#include <net/netfilter/nf_conntrack_expect.h>
24#include <net/netfilter/nf_conntrack_ecache.h>
25#include <net/netfilter/nf_conntrack_helper.h>
26#include <linux/netfilter/nf_conntrack_amanda.h>
27
28static unsigned int master_timeout __read_mostly = 300;
29static char *ts_algo = "kmp";
30
31MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
32MODULE_DESCRIPTION("Amanda connection tracking module");
33MODULE_LICENSE("GPL");
34MODULE_ALIAS("ip_conntrack_amanda");
4dc06f96 35MODULE_ALIAS_NFCT_HELPER("amanda");
16958900
PM
36
37module_param(master_timeout, uint, 0600);
38MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
39module_param(ts_algo, charp, 0400);
40MODULE_PARM_DESC(ts_algo, "textsearch algorithm to use (default kmp)");
41
3db05fea 42unsigned int (*nf_nat_amanda_hook)(struct sk_buff *skb,
16958900 43 enum ip_conntrack_info ctinfo,
051966c0 44 unsigned int protoff,
16958900
PM
45 unsigned int matchoff,
46 unsigned int matchlen,
47 struct nf_conntrack_expect *exp)
48 __read_mostly;
49EXPORT_SYMBOL_GPL(nf_nat_amanda_hook);
50
51enum amanda_strings {
52 SEARCH_CONNECT,
53 SEARCH_NEWLINE,
54 SEARCH_DATA,
55 SEARCH_MESG,
56 SEARCH_INDEX,
4283428e 57 SEARCH_STATE,
16958900
PM
58};
59
60static struct {
58c0fb0d 61 const char *string;
16958900
PM
62 size_t len;
63 struct ts_config *ts;
64} search[] __read_mostly = {
65 [SEARCH_CONNECT] = {
66 .string = "CONNECT ",
67 .len = 8,
68 },
69 [SEARCH_NEWLINE] = {
70 .string = "\n",
71 .len = 1,
72 },
73 [SEARCH_DATA] = {
74 .string = "DATA ",
75 .len = 5,
76 },
77 [SEARCH_MESG] = {
78 .string = "MESG ",
79 .len = 5,
80 },
81 [SEARCH_INDEX] = {
82 .string = "INDEX ",
83 .len = 6,
84 },
4283428e
FT
85 [SEARCH_STATE] = {
86 .string = "STATE ",
87 .len = 6,
88 },
16958900
PM
89};
90
3db05fea 91static int amanda_help(struct sk_buff *skb,
16958900
PM
92 unsigned int protoff,
93 struct nf_conn *ct,
94 enum ip_conntrack_info ctinfo)
95{
16958900
PM
96 struct nf_conntrack_expect *exp;
97 struct nf_conntrack_tuple *tuple;
98 unsigned int dataoff, start, stop, off, i;
99 char pbuf[sizeof("65535")], *tmp;
100 u_int16_t len;
101 __be16 port;
16958900
PM
102 int ret = NF_ACCEPT;
103 typeof(nf_nat_amanda_hook) nf_nat_amanda;
104
105 /* Only look at packets from the Amanda server */
106 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
107 return NF_ACCEPT;
108
109 /* increase the UDP timeout of the master connection as replies from
110 * Amanda clients to the server can be quite delayed */
3db05fea 111 nf_ct_refresh(ct, skb, master_timeout * HZ);
16958900
PM
112
113 /* No data? */
114 dataoff = protoff + sizeof(struct udphdr);
3db05fea 115 if (dataoff >= skb->len) {
e87cc472 116 net_err_ratelimited("amanda_help: skblen = %u\n", skb->len);
16958900
PM
117 return NF_ACCEPT;
118 }
119
3db05fea 120 start = skb_find_text(skb, dataoff, skb->len,
059a2440 121 search[SEARCH_CONNECT].ts);
16958900
PM
122 if (start == UINT_MAX)
123 goto out;
124 start += dataoff + search[SEARCH_CONNECT].len;
125
3db05fea 126 stop = skb_find_text(skb, start, skb->len,
059a2440 127 search[SEARCH_NEWLINE].ts);
16958900
PM
128 if (stop == UINT_MAX)
129 goto out;
130 stop += start;
131
4283428e 132 for (i = SEARCH_DATA; i <= SEARCH_STATE; i++) {
059a2440 133 off = skb_find_text(skb, start, stop, search[i].ts);
16958900
PM
134 if (off == UINT_MAX)
135 continue;
136 off += start + search[i].len;
137
138 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
3db05fea 139 if (skb_copy_bits(skb, off, pbuf, len))
16958900
PM
140 break;
141 pbuf[len] = '\0';
142
143 port = htons(simple_strtoul(pbuf, &tmp, 10));
144 len = tmp - pbuf;
145 if (port == 0 || len > 5)
146 break;
147
6823645d 148 exp = nf_ct_expect_alloc(ct);
16958900 149 if (exp == NULL) {
b20ab9cc 150 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
16958900
PM
151 ret = NF_DROP;
152 goto out;
153 }
154 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
5e8fbe2a
PM
155 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
156 nf_ct_l3num(ct),
6002f266 157 &tuple->src.u3, &tuple->dst.u3,
6823645d 158 IPPROTO_TCP, NULL, &port);
16958900
PM
159
160 nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook);
ee6eb966 161 if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
051966c0
PM
162 ret = nf_nat_amanda(skb, ctinfo, protoff,
163 off - dataoff, len, exp);
b20ab9cc
PNA
164 else if (nf_ct_expect_related(exp) != 0) {
165 nf_ct_helper_log(skb, ct, "cannot add expectation");
16958900 166 ret = NF_DROP;
b20ab9cc 167 }
6823645d 168 nf_ct_expect_put(exp);
16958900
PM
169 }
170
171out:
172 return ret;
173}
174
6002f266 175static const struct nf_conntrack_expect_policy amanda_exp_policy = {
4283428e 176 .max_expected = 4,
6002f266
PM
177 .timeout = 180,
178};
179
16958900
PM
180static struct nf_conntrack_helper amanda_helper[2] __read_mostly = {
181 {
182 .name = "amanda",
16958900
PM
183 .me = THIS_MODULE,
184 .help = amanda_help,
185 .tuple.src.l3num = AF_INET,
09640e63 186 .tuple.src.u.udp.port = cpu_to_be16(10080),
16958900 187 .tuple.dst.protonum = IPPROTO_UDP,
6002f266 188 .expect_policy = &amanda_exp_policy,
16958900
PM
189 },
190 {
191 .name = "amanda",
16958900
PM
192 .me = THIS_MODULE,
193 .help = amanda_help,
194 .tuple.src.l3num = AF_INET6,
09640e63 195 .tuple.src.u.udp.port = cpu_to_be16(10080),
16958900 196 .tuple.dst.protonum = IPPROTO_UDP,
6002f266 197 .expect_policy = &amanda_exp_policy,
16958900
PM
198 },
199};
200
201static void __exit nf_conntrack_amanda_fini(void)
202{
203 int i;
204
d53e3fc3
LZ
205 nf_conntrack_helpers_unregister(amanda_helper,
206 ARRAY_SIZE(amanda_helper));
16958900
PM
207 for (i = 0; i < ARRAY_SIZE(search); i++)
208 textsearch_destroy(search[i].ts);
209}
210
211static int __init nf_conntrack_amanda_init(void)
212{
213 int ret, i;
214
dcf67740
FW
215 NF_CT_HELPER_BUILD_BUG_ON(0);
216
16958900
PM
217 for (i = 0; i < ARRAY_SIZE(search); i++) {
218 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
219 search[i].len,
220 GFP_KERNEL, TS_AUTOLOAD);
c764c9ad
AM
221 if (IS_ERR(search[i].ts)) {
222 ret = PTR_ERR(search[i].ts);
16958900 223 goto err1;
c764c9ad 224 }
16958900 225 }
d53e3fc3
LZ
226 ret = nf_conntrack_helpers_register(amanda_helper,
227 ARRAY_SIZE(amanda_helper));
16958900
PM
228 if (ret < 0)
229 goto err1;
16958900
PM
230 return 0;
231
16958900 232err1:
c764c9ad
AM
233 while (--i >= 0)
234 textsearch_destroy(search[i].ts);
235
16958900
PM
236 return ret;
237}
238
239module_init(nf_conntrack_amanda_init);
240module_exit(nf_conntrack_amanda_fini);