blob: 57a26cc90c9fada2be251d6718f8b814e059beea [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
Patrick McHardyf229f6c2013-04-06 15:24:29 +02005 * (C) 2006 Patrick McHardy <kaber@trash.net>
Patrick McHardy16958902006-12-02 22:08:26 -08006 *
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>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080019#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Patrick McHardy16958902006-12-02 22:08:26 -080021
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");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010035MODULE_ALIAS_NFCT_HELPER("amanda");
Patrick McHardy16958902006-12-02 22:08:26 -080036
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
Herbert Xu3db05fe2007-10-15 00:53:15 -070042unsigned int (*nf_nat_amanda_hook)(struct sk_buff *skb,
Patrick McHardy16958902006-12-02 22:08:26 -080043 enum ip_conntrack_info ctinfo,
Patrick McHardy051966c2012-08-26 19:14:04 +020044 unsigned int protoff,
Patrick McHardy16958902006-12-02 22:08:26 -080045 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,
57};
58
59static struct {
Jan Engelhardt58c0fb02008-04-14 11:15:42 +020060 const char *string;
Patrick McHardy16958902006-12-02 22:08:26 -080061 size_t len;
62 struct ts_config *ts;
63} search[] __read_mostly = {
64 [SEARCH_CONNECT] = {
65 .string = "CONNECT ",
66 .len = 8,
67 },
68 [SEARCH_NEWLINE] = {
69 .string = "\n",
70 .len = 1,
71 },
72 [SEARCH_DATA] = {
73 .string = "DATA ",
74 .len = 5,
75 },
76 [SEARCH_MESG] = {
77 .string = "MESG ",
78 .len = 5,
79 },
80 [SEARCH_INDEX] = {
81 .string = "INDEX ",
82 .len = 6,
83 },
84};
85
Herbert Xu3db05fe2007-10-15 00:53:15 -070086static int amanda_help(struct sk_buff *skb,
Patrick McHardy16958902006-12-02 22:08:26 -080087 unsigned int protoff,
88 struct nf_conn *ct,
89 enum ip_conntrack_info ctinfo)
90{
Patrick McHardy16958902006-12-02 22:08:26 -080091 struct nf_conntrack_expect *exp;
92 struct nf_conntrack_tuple *tuple;
93 unsigned int dataoff, start, stop, off, i;
94 char pbuf[sizeof("65535")], *tmp;
95 u_int16_t len;
96 __be16 port;
Patrick McHardy16958902006-12-02 22:08:26 -080097 int ret = NF_ACCEPT;
98 typeof(nf_nat_amanda_hook) nf_nat_amanda;
99
100 /* Only look at packets from the Amanda server */
101 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
102 return NF_ACCEPT;
103
104 /* increase the UDP timeout of the master connection as replies from
105 * Amanda clients to the server can be quite delayed */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700106 nf_ct_refresh(ct, skb, master_timeout * HZ);
Patrick McHardy16958902006-12-02 22:08:26 -0800107
108 /* No data? */
109 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700110 if (dataoff >= skb->len) {
Joe Perchese87cc472012-05-13 21:56:26 +0000111 net_err_ratelimited("amanda_help: skblen = %u\n", skb->len);
Patrick McHardy16958902006-12-02 22:08:26 -0800112 return NF_ACCEPT;
113 }
114
Herbert Xu3db05fe2007-10-15 00:53:15 -0700115 start = skb_find_text(skb, dataoff, skb->len,
Bojan Prtvar059a2442015-02-22 11:46:35 +0100116 search[SEARCH_CONNECT].ts);
Patrick McHardy16958902006-12-02 22:08:26 -0800117 if (start == UINT_MAX)
118 goto out;
119 start += dataoff + search[SEARCH_CONNECT].len;
120
Herbert Xu3db05fe2007-10-15 00:53:15 -0700121 stop = skb_find_text(skb, start, skb->len,
Bojan Prtvar059a2442015-02-22 11:46:35 +0100122 search[SEARCH_NEWLINE].ts);
Patrick McHardy16958902006-12-02 22:08:26 -0800123 if (stop == UINT_MAX)
124 goto out;
125 stop += start;
126
127 for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) {
Bojan Prtvar059a2442015-02-22 11:46:35 +0100128 off = skb_find_text(skb, start, stop, search[i].ts);
Patrick McHardy16958902006-12-02 22:08:26 -0800129 if (off == UINT_MAX)
130 continue;
131 off += start + search[i].len;
132
133 len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700134 if (skb_copy_bits(skb, off, pbuf, len))
Patrick McHardy16958902006-12-02 22:08:26 -0800135 break;
136 pbuf[len] = '\0';
137
138 port = htons(simple_strtoul(pbuf, &tmp, 10));
139 len = tmp - pbuf;
140 if (port == 0 || len > 5)
141 break;
142
Patrick McHardy68236452007-07-07 22:30:49 -0700143 exp = nf_ct_expect_alloc(ct);
Patrick McHardy16958902006-12-02 22:08:26 -0800144 if (exp == NULL) {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100145 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Patrick McHardy16958902006-12-02 22:08:26 -0800146 ret = NF_DROP;
147 goto out;
148 }
149 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200150 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
151 nf_ct_l3num(ct),
Patrick McHardy6002f2662008-03-25 20:09:15 -0700152 &tuple->src.u3, &tuple->dst.u3,
Patrick McHardy68236452007-07-07 22:30:49 -0700153 IPPROTO_TCP, NULL, &port);
Patrick McHardy16958902006-12-02 22:08:26 -0800154
155 nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook);
Patrick McHardyee6eb962012-08-26 19:14:22 +0200156 if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
Patrick McHardy051966c2012-08-26 19:14:04 +0200157 ret = nf_nat_amanda(skb, ctinfo, protoff,
158 off - dataoff, len, exp);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100159 else if (nf_ct_expect_related(exp) != 0) {
160 nf_ct_helper_log(skb, ct, "cannot add expectation");
Patrick McHardy16958902006-12-02 22:08:26 -0800161 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100162 }
Patrick McHardy68236452007-07-07 22:30:49 -0700163 nf_ct_expect_put(exp);
Patrick McHardy16958902006-12-02 22:08:26 -0800164 }
165
166out:
167 return ret;
168}
169
Patrick McHardy6002f2662008-03-25 20:09:15 -0700170static const struct nf_conntrack_expect_policy amanda_exp_policy = {
171 .max_expected = 3,
172 .timeout = 180,
173};
174
Patrick McHardy16958902006-12-02 22:08:26 -0800175static struct nf_conntrack_helper amanda_helper[2] __read_mostly = {
176 {
177 .name = "amanda",
Patrick McHardy16958902006-12-02 22:08:26 -0800178 .me = THIS_MODULE,
179 .help = amanda_help,
180 .tuple.src.l3num = AF_INET,
Harvey Harrison09640e62009-02-01 00:45:17 -0800181 .tuple.src.u.udp.port = cpu_to_be16(10080),
Patrick McHardy16958902006-12-02 22:08:26 -0800182 .tuple.dst.protonum = IPPROTO_UDP,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700183 .expect_policy = &amanda_exp_policy,
Patrick McHardy16958902006-12-02 22:08:26 -0800184 },
185 {
186 .name = "amanda",
Patrick McHardy16958902006-12-02 22:08:26 -0800187 .me = THIS_MODULE,
188 .help = amanda_help,
189 .tuple.src.l3num = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800190 .tuple.src.u.udp.port = cpu_to_be16(10080),
Patrick McHardy16958902006-12-02 22:08:26 -0800191 .tuple.dst.protonum = IPPROTO_UDP,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700192 .expect_policy = &amanda_exp_policy,
Patrick McHardy16958902006-12-02 22:08:26 -0800193 },
194};
195
196static void __exit nf_conntrack_amanda_fini(void)
197{
198 int i;
199
200 nf_conntrack_helper_unregister(&amanda_helper[0]);
201 nf_conntrack_helper_unregister(&amanda_helper[1]);
202 for (i = 0; i < ARRAY_SIZE(search); i++)
203 textsearch_destroy(search[i].ts);
204}
205
206static int __init nf_conntrack_amanda_init(void)
207{
208 int ret, i;
209
Patrick McHardy16958902006-12-02 22:08:26 -0800210 for (i = 0; i < ARRAY_SIZE(search); i++) {
211 search[i].ts = textsearch_prepare(ts_algo, search[i].string,
212 search[i].len,
213 GFP_KERNEL, TS_AUTOLOAD);
Akinobu Mitac764c9a2007-06-05 12:56:53 -0700214 if (IS_ERR(search[i].ts)) {
215 ret = PTR_ERR(search[i].ts);
Patrick McHardy16958902006-12-02 22:08:26 -0800216 goto err1;
Akinobu Mitac764c9a2007-06-05 12:56:53 -0700217 }
Patrick McHardy16958902006-12-02 22:08:26 -0800218 }
219 ret = nf_conntrack_helper_register(&amanda_helper[0]);
220 if (ret < 0)
221 goto err1;
222 ret = nf_conntrack_helper_register(&amanda_helper[1]);
223 if (ret < 0)
224 goto err2;
225 return 0;
226
227err2:
228 nf_conntrack_helper_unregister(&amanda_helper[0]);
229err1:
Akinobu Mitac764c9a2007-06-05 12:56:53 -0700230 while (--i >= 0)
231 textsearch_destroy(search[i].ts);
232
Patrick McHardy16958902006-12-02 22:08:26 -0800233 return ret;
234}
235
236module_init(nf_conntrack_amanda_init);
237module_exit(nf_conntrack_amanda_fini);