upgrade to linux 2.6.10-1.12_FC2
[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 *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         skb_copy_bits(skb, dataoff, amanda_buffer, skb->len - dataoff);
74         data = amanda_buffer;
75         data_limit = amanda_buffer + skb->len - dataoff;
76         *data_limit = '\0';
77
78         /* Search for the CONNECT string */
79         data = strstr(data, "CONNECT ");
80         if (!data)
81                 goto out;
82         data += strlen("CONNECT ");
83
84         /* Only search first line. */   
85         if ((tmp = strchr(data, '\n')))
86                 *tmp = '\0';
87
88         for (i = 0; i < ARRAY_SIZE(conns); i++) {
89                 char *match = strstr(data, conns[i]);
90                 if (!match)
91                         continue;
92                 tmp = data = match + strlen(conns[i]);
93                 port = simple_strtoul(data, &data, 10);
94                 len = data - tmp;
95                 if (port == 0 || len > 5)
96                         break;
97
98                 exp = ip_conntrack_expect_alloc();
99                 if (exp == NULL)
100                         goto out;
101
102                 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
103                 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
104                 exp->tuple.dst.protonum = IPPROTO_TCP;
105                 exp->mask.src.ip = 0xFFFFFFFF;
106                 exp->mask.dst.ip = 0xFFFFFFFF;
107                 exp->mask.dst.protonum = 0xFFFF;
108                 exp->mask.dst.u.tcp.port = 0xFFFF;
109
110                 exp_amanda_info = &exp->help.exp_amanda_info;
111                 exp_amanda_info->offset = tmp - amanda_buffer;
112                 exp_amanda_info->port   = port;
113                 exp_amanda_info->len    = len;
114
115                 exp->tuple.dst.u.tcp.port = htons(port);
116
117                 ip_conntrack_expect_related(exp, ct);
118         }
119
120 out:
121         UNLOCK_BH(&amanda_buffer_lock);
122         return NF_ACCEPT;
123 }
124
125 static struct ip_conntrack_helper amanda_helper = {
126         .max_expected = ARRAY_SIZE(conns),
127         .timeout = 180,
128         .flags = IP_CT_HELPER_F_REUSE_EXPECT,
129         .me = THIS_MODULE,
130         .help = help,
131         .name = "amanda",
132
133         .tuple = { .src = { .u = { __constant_htons(10080) } },
134                    .dst = { .protonum = IPPROTO_UDP },
135         },
136         .mask = { .src = { .u = { 0xFFFF } },
137                  .dst = { .protonum = 0xFFFF },
138         },
139 };
140
141 static void __exit fini(void)
142 {
143         ip_conntrack_helper_unregister(&amanda_helper);
144 }
145
146 static int __init init(void)
147 {
148         return ip_conntrack_helper_register(&amanda_helper);
149 }
150
151 PROVIDES_CONNTRACK(amanda);
152 module_init(init);
153 module_exit(fini);