Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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/in.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/netfilter.h>
25 #include <linux/ip.h>
26 #include <linux/moduleparam.h>
27 #include <linux/udp.h>
28 #include <net/checksum.h>
29 #include <net/udp.h>
30
31 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
32 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
33
34 static unsigned int master_timeout = 300;
35
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
42 static const char *conns[] = { "DATA ", "MESG ", "INDEX " };
43
44 /* This is slow, but it's simple. --RR */
45 static char *amanda_buffer;
46 static DEFINE_SPINLOCK(amanda_buffer_lock);
47
48 unsigned int (*ip_nat_amanda_hook)(struct sk_buff **pskb,
49                                    enum ip_conntrack_info ctinfo,
50                                    unsigned int matchoff,
51                                    unsigned int matchlen,
52                                    struct ip_conntrack_expect *exp);
53 EXPORT_SYMBOL_GPL(ip_nat_amanda_hook);
54
55 static int help(struct sk_buff **pskb,
56                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
57 {
58         struct ip_conntrack_expect *exp;
59         char *data, *data_limit, *tmp;
60         unsigned int dataoff, i;
61         u_int16_t port, len;
62         int ret = NF_ACCEPT;
63
64         /* Only look at packets from the Amanda server */
65         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
66                 return NF_ACCEPT;
67
68         /* increase the UDP timeout of the master connection as replies from
69          * Amanda clients to the server can be quite delayed */
70         ip_ct_refresh(ct, *pskb, master_timeout * HZ);
71
72         /* No data? */
73         dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
74         if (dataoff >= (*pskb)->len) {
75                 if (net_ratelimit())
76                         printk("amanda_help: skblen = %u\n", (*pskb)->len);
77                 return NF_ACCEPT;
78         }
79
80         spin_lock_bh(&amanda_buffer_lock);
81         skb_copy_bits(*pskb, dataoff, amanda_buffer, (*pskb)->len - dataoff);
82         data = amanda_buffer;
83         data_limit = amanda_buffer + (*pskb)->len - dataoff;
84         *data_limit = '\0';
85
86         /* Search for the CONNECT string */
87         data = strstr(data, "CONNECT ");
88         if (!data)
89                 goto out;
90         data += strlen("CONNECT ");
91
92         /* Only search first line. */   
93         if ((tmp = strchr(data, '\n')))
94                 *tmp = '\0';
95
96         for (i = 0; i < ARRAY_SIZE(conns); i++) {
97                 char *match = strstr(data, conns[i]);
98                 if (!match)
99                         continue;
100                 tmp = data = match + strlen(conns[i]);
101                 port = simple_strtoul(data, &data, 10);
102                 len = data - tmp;
103                 if (port == 0 || len > 5)
104                         break;
105
106                 exp = ip_conntrack_expect_alloc(ct);
107                 if (exp == NULL) {
108                         ret = NF_DROP;
109                         goto out;
110                 }
111
112                 exp->expectfn = NULL;
113                 exp->flags = 0;
114
115                 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
116                 exp->tuple.src.u.tcp.port = 0;
117                 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
118                 exp->tuple.dst.protonum = IPPROTO_TCP;
119                 exp->tuple.dst.u.tcp.port = htons(port);
120
121                 exp->mask.src.ip = 0xFFFFFFFF;
122                 exp->mask.src.u.tcp.port = 0;
123                 exp->mask.dst.ip = 0xFFFFFFFF;
124                 exp->mask.dst.protonum = 0xFF;
125                 exp->mask.dst.u.tcp.port = 0xFFFF;
126
127                 if (ip_nat_amanda_hook)
128                         ret = ip_nat_amanda_hook(pskb, ctinfo,
129                                                  tmp - amanda_buffer,
130                                                  len, exp);
131                 else if (ip_conntrack_expect_related(exp) != 0)
132                         ret = NF_DROP;
133                 ip_conntrack_expect_put(exp);
134         }
135
136 out:
137         spin_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 ip_conntrack_amanda_fini(void)
157 {
158         ip_conntrack_helper_unregister(&amanda_helper);
159         kfree(amanda_buffer);
160 }
161
162 static int __init ip_conntrack_amanda_init(void)
163 {
164         int ret;
165
166         amanda_buffer = kmalloc(65536, GFP_KERNEL);
167         if (!amanda_buffer)
168                 return -ENOMEM;
169
170         ret = ip_conntrack_helper_register(&amanda_helper);
171         if (ret < 0) {
172                 kfree(amanda_buffer);
173                 return ret;
174         }
175         return 0;
176
177
178 }
179
180 module_init(ip_conntrack_amanda_init);
181 module_exit(ip_conntrack_amanda_fini);