1 /* Amanda extension for IP connection tracking
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
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.
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>
17 #include <linux/udp.h>
18 #include <linux/netfilter.h>
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>
26 static unsigned int master_timeout __read_mostly
= 300;
27 static char *ts_algo
= "kmp";
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");
34 module_param(master_timeout
, uint
, 0600);
35 MODULE_PARM_DESC(master_timeout
, "timeout for the master connection");
36 module_param(ts_algo
, charp
, 0400);
37 MODULE_PARM_DESC(ts_algo
, "textsearch algorithm to use (default kmp)");
39 unsigned int (*nf_nat_amanda_hook
)(struct sk_buff
*skb
,
40 enum ip_conntrack_info ctinfo
,
41 unsigned int matchoff
,
42 unsigned int matchlen
,
43 struct nf_conntrack_expect
*exp
)
45 EXPORT_SYMBOL_GPL(nf_nat_amanda_hook
);
59 } search
[] __read_mostly
= {
82 static int amanda_help(struct sk_buff
*skb
,
85 enum ip_conntrack_info ctinfo
)
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
;
94 int family
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
;
96 typeof(nf_nat_amanda_hook
) nf_nat_amanda
;
98 /* Only look at packets from the Amanda server */
99 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
102 /* increase the UDP timeout of the master connection as replies from
103 * Amanda clients to the server can be quite delayed */
104 nf_ct_refresh(ct
, skb
, master_timeout
* HZ
);
107 dataoff
= protoff
+ sizeof(struct udphdr
);
108 if (dataoff
>= skb
->len
) {
110 printk("amanda_help: skblen = %u\n", skb
->len
);
114 memset(&ts
, 0, sizeof(ts
));
115 start
= skb_find_text(skb
, dataoff
, skb
->len
,
116 search
[SEARCH_CONNECT
].ts
, &ts
);
117 if (start
== UINT_MAX
)
119 start
+= dataoff
+ search
[SEARCH_CONNECT
].len
;
121 memset(&ts
, 0, sizeof(ts
));
122 stop
= skb_find_text(skb
, start
, skb
->len
,
123 search
[SEARCH_NEWLINE
].ts
, &ts
);
124 if (stop
== UINT_MAX
)
128 for (i
= SEARCH_DATA
; i
<= SEARCH_INDEX
; i
++) {
129 memset(&ts
, 0, sizeof(ts
));
130 off
= skb_find_text(skb
, start
, stop
, search
[i
].ts
, &ts
);
133 off
+= start
+ search
[i
].len
;
135 len
= min_t(unsigned int, sizeof(pbuf
) - 1, stop
- off
);
136 if (skb_copy_bits(skb
, off
, pbuf
, len
))
140 port
= htons(simple_strtoul(pbuf
, &tmp
, 10));
142 if (port
== 0 || len
> 5)
145 exp
= nf_ct_expect_alloc(ct
);
150 tuple
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
151 nf_ct_expect_init(exp
, family
, &tuple
->src
.u3
, &tuple
->dst
.u3
,
152 IPPROTO_TCP
, NULL
, &port
);
154 nf_nat_amanda
= rcu_dereference(nf_nat_amanda_hook
);
155 if (nf_nat_amanda
&& ct
->status
& IPS_NAT_MASK
)
156 ret
= nf_nat_amanda(skb
, ctinfo
, off
- dataoff
,
158 else if (nf_ct_expect_related(exp
) != 0)
160 nf_ct_expect_put(exp
);
167 static struct nf_conntrack_helper amanda_helper
[2] __read_mostly
= {
174 .tuple
.src
.l3num
= AF_INET
,
175 .tuple
.src
.u
.udp
.port
= __constant_htons(10080),
176 .tuple
.dst
.protonum
= IPPROTO_UDP
,
184 .tuple
.src
.l3num
= AF_INET6
,
185 .tuple
.src
.u
.udp
.port
= __constant_htons(10080),
186 .tuple
.dst
.protonum
= IPPROTO_UDP
,
190 static void __exit
nf_conntrack_amanda_fini(void)
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
);
200 static int __init
nf_conntrack_amanda_init(void)
204 for (i
= 0; i
< ARRAY_SIZE(search
); i
++) {
205 search
[i
].ts
= textsearch_prepare(ts_algo
, search
[i
].string
,
207 GFP_KERNEL
, TS_AUTOLOAD
);
208 if (IS_ERR(search
[i
].ts
)) {
209 ret
= PTR_ERR(search
[i
].ts
);
213 ret
= nf_conntrack_helper_register(&amanda_helper
[0]);
216 ret
= nf_conntrack_helper_register(&amanda_helper
[1]);
222 nf_conntrack_helper_unregister(&amanda_helper
[0]);
225 textsearch_destroy(search
[i
].ts
);
230 module_init(nf_conntrack_amanda_init
);
231 module_exit(nf_conntrack_amanda_fini
);