vserver 1.9.5.x5
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_amanda.c
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
4  *
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.
9  *
10  *      Module load syntax:
11  *      insmod ip_conntrack_amanda.o [master_timeout=n]
12  *      
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
17  *      this value.
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/netfilter.h>
24 #include <linux/ip.h>
25 #include <linux/moduleparam.h>
26 #include <net/checksum.h>
27 #include <net/udp.h>
28
29 #include <linux/netfilter_ipv4/lockhelp.h>
30 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
31 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
32
33 static unsigned int master_timeout = 300;
34
35 MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
36 MODULE_DESCRIPTION("Amanda connection tracking module");
37 MODULE_LICENSE("GPL");
38 module_param(master_timeout, int, 0600);
39 MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
40
41 static char *conns[] = { "DATA ", "MESG ", "INDEX " };
42
43 /* This is slow, but it's simple. --RR */
44 static char amanda_buffer[65536];
45 static DECLARE_LOCK(amanda_buffer_lock);
46
47 unsigned int (*ip_nat_amanda_hook)(struct sk_buff **pskb,
48                                    enum ip_conntrack_info ctinfo,
49                                    unsigned int matchoff,
50                                    unsigned int matchlen,
51                                    struct ip_conntrack_expect *exp);
52 EXPORT_SYMBOL_GPL(ip_nat_amanda_hook);
53
54 static int help(struct sk_buff **pskb,
55                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
56 {
57         struct ip_conntrack_expect *exp;
58         char *data, *data_limit, *tmp;
59         unsigned int dataoff, i;
60         u_int16_t port, len;
61         int ret = NF_ACCEPT;
62
63         /* Only look at packets from the Amanda server */
64         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
65                 return NF_ACCEPT;
66
67         /* increase the UDP timeout of the master connection as replies from
68          * Amanda clients to the server can be quite delayed */
69         ip_ct_refresh_acct(ct, ctinfo, NULL, master_timeout * HZ);
70
71         /* No data? */
72         dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
73         if (dataoff >= (*pskb)->len) {
74                 if (net_ratelimit())
75                         printk("amanda_help: skblen = %u\n", (*pskb)->len);
76                 return NF_ACCEPT;
77         }
78
79         LOCK_BH(&amanda_buffer_lock);
80         skb_copy_bits(*pskb, dataoff, amanda_buffer, (*pskb)->len - dataoff);
81         data = amanda_buffer;
82         data_limit = amanda_buffer + (*pskb)->len - dataoff;
83         *data_limit = '\0';
84
85         /* Search for the CONNECT string */
86         data = strstr(data, "CONNECT ");
87         if (!data)
88                 goto out;
89         data += strlen("CONNECT ");
90
91         /* Only search first line. */   
92         if ((tmp = strchr(data, '\n')))
93                 *tmp = '\0';
94
95         for (i = 0; i < ARRAY_SIZE(conns); i++) {
96                 char *match = strstr(data, conns[i]);
97                 if (!match)
98                         continue;
99                 tmp = data = match + strlen(conns[i]);
100                 port = simple_strtoul(data, &data, 10);
101                 len = data - tmp;
102                 if (port == 0 || len > 5)
103                         break;
104
105                 exp = ip_conntrack_expect_alloc();
106                 if (exp == NULL) {
107                         ret = NF_DROP;
108                         goto out;
109                 }
110
111                 exp->expectfn = NULL;
112                 exp->master = ct;
113
114                 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
115                 exp->tuple.src.u.tcp.port = 0;
116                 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
117                 exp->tuple.dst.protonum = IPPROTO_TCP;
118                 exp->tuple.dst.u.tcp.port = htons(port);
119
120                 exp->mask.src.ip = 0xFFFFFFFF;
121                 exp->mask.src.u.tcp.port = 0;
122                 exp->mask.dst.ip = 0xFFFFFFFF;
123                 exp->mask.dst.protonum = 0xFF;
124                 exp->mask.dst.u.tcp.port = 0xFFFF;
125
126                 if (ip_nat_amanda_hook)
127                         ret = ip_nat_amanda_hook(pskb, ctinfo,
128                                                  tmp - amanda_buffer,
129                                                  len, exp);
130                 else if (ip_conntrack_expect_related(exp) != 0) {
131                         ip_conntrack_expect_free(exp);
132                         ret = NF_DROP;
133                 }
134         }
135
136 out:
137         UNLOCK_BH(&amanda_buffer_lock);
138         return ret;
139 }
140
141 static struct ip_conntrack_helper amanda_helper = {
142         .max_expected = ARRAY_SIZE(conns),
143         .timeout = 180,
144         .me = THIS_MODULE,
145         .help = help,
146         .name = "amanda",
147
148         .tuple = { .src = { .u = { __constant_htons(10080) } },
149                    .dst = { .protonum = IPPROTO_UDP },
150         },
151         .mask = { .src = { .u = { 0xFFFF } },
152                  .dst = { .protonum = 0xFF },
153         },
154 };
155
156 static void __exit fini(void)
157 {
158         ip_conntrack_helper_unregister(&amanda_helper);
159 }
160
161 static int __init init(void)
162 {
163         return ip_conntrack_helper_register(&amanda_helper);
164 }
165
166 module_init(init);
167 module_exit(fini);