[NETFILTER]: ip_conntrack_expect_related must not free expectation
[firewire-audio.git] / net / ipv4 / netfilter / ip_conntrack_amanda.c
blob01e1b58322a9b07cba23e4d2b678debf873c42d1
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
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.
10 * Module load syntax:
11 * insmod ip_conntrack_amanda.o [master_timeout=n]
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.
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>
29 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
30 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
32 static unsigned int master_timeout = 300;
34 MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
35 MODULE_DESCRIPTION("Amanda connection tracking module");
36 MODULE_LICENSE("GPL");
37 module_param(master_timeout, int, 0600);
38 MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
40 static char *conns[] = { "DATA ", "MESG ", "INDEX " };
42 /* This is slow, but it's simple. --RR */
43 static char amanda_buffer[65536];
44 static DEFINE_SPINLOCK(amanda_buffer_lock);
46 unsigned int (*ip_nat_amanda_hook)(struct sk_buff **pskb,
47 enum ip_conntrack_info ctinfo,
48 unsigned int matchoff,
49 unsigned int matchlen,
50 struct ip_conntrack_expect *exp);
51 EXPORT_SYMBOL_GPL(ip_nat_amanda_hook);
53 static int help(struct sk_buff **pskb,
54 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
56 struct ip_conntrack_expect *exp;
57 char *data, *data_limit, *tmp;
58 unsigned int dataoff, i;
59 u_int16_t port, len;
60 int ret = NF_ACCEPT;
62 /* Only look at packets from the Amanda server */
63 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
64 return NF_ACCEPT;
66 /* increase the UDP timeout of the master connection as replies from
67 * Amanda clients to the server can be quite delayed */
68 ip_ct_refresh_acct(ct, ctinfo, NULL, master_timeout * HZ);
70 /* No data? */
71 dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
72 if (dataoff >= (*pskb)->len) {
73 if (net_ratelimit())
74 printk("amanda_help: skblen = %u\n", (*pskb)->len);
75 return NF_ACCEPT;
78 spin_lock_bh(&amanda_buffer_lock);
79 skb_copy_bits(*pskb, dataoff, amanda_buffer, (*pskb)->len - dataoff);
80 data = amanda_buffer;
81 data_limit = amanda_buffer + (*pskb)->len - dataoff;
82 *data_limit = '\0';
84 /* Search for the CONNECT string */
85 data = strstr(data, "CONNECT ");
86 if (!data)
87 goto out;
88 data += strlen("CONNECT ");
90 /* Only search first line. */
91 if ((tmp = strchr(data, '\n')))
92 *tmp = '\0';
94 for (i = 0; i < ARRAY_SIZE(conns); i++) {
95 char *match = strstr(data, conns[i]);
96 if (!match)
97 continue;
98 tmp = data = match + strlen(conns[i]);
99 port = simple_strtoul(data, &data, 10);
100 len = data - tmp;
101 if (port == 0 || len > 5)
102 break;
104 exp = ip_conntrack_expect_alloc(ct);
105 if (exp == NULL) {
106 ret = NF_DROP;
107 goto out;
110 exp->expectfn = NULL;
112 exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
113 exp->tuple.src.u.tcp.port = 0;
114 exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
115 exp->tuple.dst.protonum = IPPROTO_TCP;
116 exp->tuple.dst.u.tcp.port = htons(port);
118 exp->mask.src.ip = 0xFFFFFFFF;
119 exp->mask.src.u.tcp.port = 0;
120 exp->mask.dst.ip = 0xFFFFFFFF;
121 exp->mask.dst.protonum = 0xFF;
122 exp->mask.dst.u.tcp.port = 0xFFFF;
124 if (ip_nat_amanda_hook)
125 ret = ip_nat_amanda_hook(pskb, ctinfo,
126 tmp - amanda_buffer,
127 len, exp);
128 else if (ip_conntrack_expect_related(exp) != 0)
129 ret = NF_DROP;
130 ip_conntrack_expect_put(exp);
133 out:
134 spin_unlock_bh(&amanda_buffer_lock);
135 return ret;
138 static struct ip_conntrack_helper amanda_helper = {
139 .max_expected = ARRAY_SIZE(conns),
140 .timeout = 180,
141 .me = THIS_MODULE,
142 .help = help,
143 .name = "amanda",
145 .tuple = { .src = { .u = { __constant_htons(10080) } },
146 .dst = { .protonum = IPPROTO_UDP },
148 .mask = { .src = { .u = { 0xFFFF } },
149 .dst = { .protonum = 0xFF },
153 static void __exit fini(void)
155 ip_conntrack_helper_unregister(&amanda_helper);
158 static int __init init(void)
160 return ip_conntrack_helper_register(&amanda_helper);
163 module_init(init);
164 module_exit(fini);