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