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