1 /* Amanda extension for IP connection tracking, Version 0.2
2 * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca>
3 * based on HW's ip_conntrack_irc.c as well as other modules
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
11 * insmod ip_conntrack_amanda.o [master_timeout=n]
13 * Where master_timeout is the timeout (in seconds) of the master
14 * connection (port 10080). This defaults to 5 minutes but if
15 * your clients take longer than 5 minutes to do their work
16 * before getting back to the Amanda server, you can increase
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/textsearch.h>
24 #include <linux/skbuff.h>
27 #include <linux/udp.h>
29 #include <linux/netfilter.h>
30 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
31 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
33 static unsigned int master_timeout
= 300;
34 static char *ts_algo
= "kmp";
36 MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
37 MODULE_DESCRIPTION("Amanda connection tracking module");
38 MODULE_LICENSE("GPL");
39 module_param(master_timeout
, uint
, 0600);
40 MODULE_PARM_DESC(master_timeout
, "timeout for the master connection");
41 module_param(ts_algo
, charp
, 0400);
42 MODULE_PARM_DESC(ts_algo
, "textsearch algorithm to use (default kmp)");
44 unsigned int (*ip_nat_amanda_hook
)(struct sk_buff
**pskb
,
45 enum ip_conntrack_info ctinfo
,
46 unsigned int matchoff
,
47 unsigned int matchlen
,
48 struct ip_conntrack_expect
*exp
);
49 EXPORT_SYMBOL_GPL(ip_nat_amanda_hook
);
86 static int help(struct sk_buff
**pskb
,
87 struct ip_conntrack
*ct
, enum ip_conntrack_info ctinfo
)
90 struct ip_conntrack_expect
*exp
;
91 unsigned int dataoff
, start
, stop
, off
, i
;
92 char pbuf
[sizeof("65535")], *tmp
;
95 typeof(ip_nat_amanda_hook
) ip_nat_amanda
;
97 /* Only look at packets from the Amanda server */
98 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
101 /* increase the UDP timeout of the master connection as replies from
102 * Amanda clients to the server can be quite delayed */
103 ip_ct_refresh(ct
, *pskb
, master_timeout
* HZ
);
106 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
107 if (dataoff
>= (*pskb
)->len
) {
109 printk("amanda_help: skblen = %u\n", (*pskb
)->len
);
113 memset(&ts
, 0, sizeof(ts
));
114 start
= skb_find_text(*pskb
, dataoff
, (*pskb
)->len
,
115 search
[SEARCH_CONNECT
].ts
, &ts
);
116 if (start
== UINT_MAX
)
118 start
+= dataoff
+ search
[SEARCH_CONNECT
].len
;
120 memset(&ts
, 0, sizeof(ts
));
121 stop
= skb_find_text(*pskb
, start
, (*pskb
)->len
,
122 search
[SEARCH_NEWLINE
].ts
, &ts
);
123 if (stop
== UINT_MAX
)
127 for (i
= SEARCH_DATA
; i
<= SEARCH_INDEX
; i
++) {
128 memset(&ts
, 0, sizeof(ts
));
129 off
= skb_find_text(*pskb
, start
, stop
, search
[i
].ts
, &ts
);
132 off
+= start
+ search
[i
].len
;
134 len
= min_t(unsigned int, sizeof(pbuf
) - 1, stop
- off
);
135 if (skb_copy_bits(*pskb
, off
, pbuf
, len
))
139 port
= simple_strtoul(pbuf
, &tmp
, 10);
141 if (port
== 0 || len
> 5)
144 exp
= ip_conntrack_expect_alloc(ct
);
150 exp
->expectfn
= NULL
;
153 exp
->tuple
.src
.ip
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.ip
;
154 exp
->tuple
.src
.u
.tcp
.port
= 0;
155 exp
->tuple
.dst
.ip
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.ip
;
156 exp
->tuple
.dst
.protonum
= IPPROTO_TCP
;
157 exp
->tuple
.dst
.u
.tcp
.port
= htons(port
);
159 exp
->mask
.src
.ip
= htonl(0xFFFFFFFF);
160 exp
->mask
.src
.u
.tcp
.port
= 0;
161 exp
->mask
.dst
.ip
= htonl(0xFFFFFFFF);
162 exp
->mask
.dst
.protonum
= 0xFF;
163 exp
->mask
.dst
.u
.tcp
.port
= htons(0xFFFF);
165 /* RCU read locked by nf_hook_slow */
166 ip_nat_amanda
= rcu_dereference(ip_nat_amanda_hook
);
168 ret
= ip_nat_amanda(pskb
, ctinfo
, off
- dataoff
,
170 else if (ip_conntrack_expect_related(exp
) != 0)
172 ip_conntrack_expect_put(exp
);
179 static struct ip_conntrack_helper amanda_helper
= {
186 .tuple
= { .src
= { .u
= { .udp
= {.port
= __constant_htons(10080) } } },
187 .dst
= { .protonum
= IPPROTO_UDP
},
189 .mask
= { .src
= { .u
= { 0xFFFF } },
190 .dst
= { .protonum
= 0xFF },
194 static void __exit
ip_conntrack_amanda_fini(void)
198 ip_conntrack_helper_unregister(&amanda_helper
);
199 for (i
= 0; i
< ARRAY_SIZE(search
); i
++)
200 textsearch_destroy(search
[i
].ts
);
203 static int __init
ip_conntrack_amanda_init(void)
208 for (i
= 0; i
< ARRAY_SIZE(search
); i
++) {
209 search
[i
].ts
= textsearch_prepare(ts_algo
, search
[i
].string
,
211 GFP_KERNEL
, TS_AUTOLOAD
);
212 if (search
[i
].ts
== NULL
)
215 ret
= ip_conntrack_helper_register(&amanda_helper
);
221 for (; i
>= 0; i
--) {
223 textsearch_destroy(search
[i
].ts
);
228 module_init(ip_conntrack_amanda_init
);
229 module_exit(ip_conntrack_amanda_fini
);