vserver 1.9.3
[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 static int help(struct sk_buff *skb,
48                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
49 {
50         struct ip_conntrack_expect *exp;
51         struct ip_ct_amanda_expect *exp_amanda_info;
52         char *amp, *data, *data_limit, *tmp;
53         unsigned int dataoff, i;
54         u_int16_t port, len;
55
56         /* Only look at packets from the Amanda server */
57         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
58                 return NF_ACCEPT;
59
60         /* increase the UDP timeout of the master connection as replies from
61          * Amanda clients to the server can be quite delayed */
62         ip_ct_refresh_acct(ct, ctinfo, NULL, master_timeout * HZ);
63
64         /* No data? */
65         dataoff = skb->nh.iph->ihl*4 + sizeof(struct udphdr);
66         if (dataoff >= skb->len) {
67                 if (net_ratelimit())
68                         printk("amanda_help: skblen = %u\n", skb->len);
69                 return NF_ACCEPT;
70         }
71
72         LOCK_BH(&amanda_buffer_lock);
73         amp = skb_header_pointer(skb, dataoff,
74                                  skb->len - dataoff, amanda_buffer);
75         BUG_ON(amp == NULL);
76         data = amp;
77         data_limit = amp + skb->len - dataoff;
78         *data_limit = '\0';
79
80         /* Search for the CONNECT string */
81         data = strstr(data, "CONNECT ");
82         if (!data)
83                 goto out;
84         data += strlen("CONNECT ");
85
86         /* Only search first line. */   
87         if ((tmp = strchr(data, '\n')))
88                 *tmp = '\0';
89
90         for (i = 0; i < ARRAY_SIZE(conns); i++) {
91                 char *match = strstr(data, conns[i]);
92                 if (!match)
93                         continue;
94                 tmp = data = match + strlen(conns[i]);
95                 port = simple_strtoul(data, &data, 10);
96                 len = data - tmp;
97                 if (port == 0 || len > 5)
98                         break;
99
100                 exp = ip_conntrack_expect_alloc();
101                 if (exp == NULL)
102                         goto out;
103
104                 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
105                 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
106                 exp->tuple.dst.protonum = IPPROTO_TCP;
107                 exp->mask.src.ip = 0xFFFFFFFF;
108                 exp->mask.dst.ip = 0xFFFFFFFF;
109                 exp->mask.dst.protonum = 0xFFFF;
110                 exp->mask.dst.u.tcp.port = 0xFFFF;
111
112                 exp_amanda_info = &exp->help.exp_amanda_info;
113                 exp_amanda_info->offset = tmp - amp;
114                 exp_amanda_info->port   = port;
115                 exp_amanda_info->len    = len;
116
117                 exp->tuple.dst.u.tcp.port = htons(port);
118
119                 ip_conntrack_expect_related(exp, ct);
120         }
121
122 out:
123         UNLOCK_BH(&amanda_buffer_lock);
124         return NF_ACCEPT;
125 }
126
127 static struct ip_conntrack_helper amanda_helper = {
128         .max_expected = ARRAY_SIZE(conns),
129         .timeout = 180,
130         .flags = IP_CT_HELPER_F_REUSE_EXPECT,
131         .me = THIS_MODULE,
132         .help = help,
133         .name = "amanda",
134
135         .tuple = { .src = { .u = { __constant_htons(10080) } },
136                    .dst = { .protonum = IPPROTO_UDP },
137         },
138         .mask = { .src = { .u = { 0xFFFF } },
139                  .dst = { .protonum = 0xFFFF },
140         },
141 };
142
143 static void __exit fini(void)
144 {
145         ip_conntrack_helper_unregister(&amanda_helper);
146 }
147
148 static int __init init(void)
149 {
150         return ip_conntrack_helper_register(&amanda_helper);
151 }
152
153 PROVIDES_CONNTRACK(amanda);
154 module_init(init);
155 module_exit(fini);