NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / nf_nat_ftp.c
blobd1adc7f00191c300805451ab2b220761149e63c0
1 /* FTP extension for TCP NAT alteration. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <net/netfilter/nf_nat.h>
17 #include <net/netfilter/nf_nat_helper.h>
18 #include <net/netfilter/nf_nat_rule.h>
19 #include <net/netfilter/nf_conntrack_helper.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21 #include <linux/netfilter/nf_conntrack_ftp.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
25 MODULE_DESCRIPTION("ftp NAT helper");
26 MODULE_ALIAS("ip_nat_ftp");
28 #if 0
29 #define DEBUGP printk
30 #else
31 #define DEBUGP(format, args...)
32 #endif
34 /* FIXME: Time out? --RR */
36 static int
37 mangle_rfc959_packet(struct sk_buff *skb,
38 __be32 newip,
39 u_int16_t port,
40 unsigned int matchoff,
41 unsigned int matchlen,
42 struct nf_conn *ct,
43 enum ip_conntrack_info ctinfo)
45 char buffer[sizeof("nnn,nnn,nnn,nnn,nnn,nnn")];
47 sprintf(buffer, "%u,%u,%u,%u,%u,%u",
48 NIPQUAD(newip), port>>8, port&0xFF);
50 DEBUGP("calling nf_nat_mangle_tcp_packet\n");
52 return nf_nat_mangle_tcp_packet(skb, ct, ctinfo, matchoff,
53 matchlen, buffer, strlen(buffer));
56 /* |1|132.235.1.2|6275| */
57 static int
58 mangle_eprt_packet(struct sk_buff *skb,
59 __be32 newip,
60 u_int16_t port,
61 unsigned int matchoff,
62 unsigned int matchlen,
63 struct nf_conn *ct,
64 enum ip_conntrack_info ctinfo)
66 char buffer[sizeof("|1|255.255.255.255|65535|")];
68 sprintf(buffer, "|1|%u.%u.%u.%u|%u|", NIPQUAD(newip), port);
70 DEBUGP("calling nf_nat_mangle_tcp_packet\n");
72 return nf_nat_mangle_tcp_packet(skb, ct, ctinfo, matchoff,
73 matchlen, buffer, strlen(buffer));
76 /* |1|132.235.1.2|6275| */
77 static int
78 mangle_epsv_packet(struct sk_buff *skb,
79 __be32 newip,
80 u_int16_t port,
81 unsigned int matchoff,
82 unsigned int matchlen,
83 struct nf_conn *ct,
84 enum ip_conntrack_info ctinfo)
86 char buffer[sizeof("|||65535|")];
88 sprintf(buffer, "|||%u|", port);
90 DEBUGP("calling nf_nat_mangle_tcp_packet\n");
92 return nf_nat_mangle_tcp_packet(skb, ct, ctinfo, matchoff,
93 matchlen, buffer, strlen(buffer));
96 static int (*mangle[])(struct sk_buff *, __be32, u_int16_t,
97 unsigned int, unsigned int, struct nf_conn *,
98 enum ip_conntrack_info)
99 = {
100 [NF_CT_FTP_PORT] = mangle_rfc959_packet,
101 [NF_CT_FTP_PASV] = mangle_rfc959_packet,
102 [NF_CT_FTP_EPRT] = mangle_eprt_packet,
103 [NF_CT_FTP_EPSV] = mangle_epsv_packet
106 /* So, this packet has hit the connection tracking matching code.
107 Mangle it, and change the expectation to match the new version. */
108 static unsigned int nf_nat_ftp(struct sk_buff *skb,
109 enum ip_conntrack_info ctinfo,
110 enum nf_ct_ftp_type type,
111 unsigned int matchoff,
112 unsigned int matchlen,
113 struct nf_conntrack_expect *exp)
115 __be32 newip;
116 u_int16_t port;
117 int dir = CTINFO2DIR(ctinfo);
118 struct nf_conn *ct = exp->master;
120 DEBUGP("FTP_NAT: type %i, off %u len %u\n", type, matchoff, matchlen);
122 /* Connection will come from wherever this packet goes, hence !dir */
123 newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
124 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
125 exp->dir = !dir;
127 /* When you see the packet, we need to NAT it the same as the
128 * this one. */
129 exp->expectfn = nf_nat_follow_master;
131 /* Try to get same port: if not, try to change it. */
132 for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
133 exp->tuple.dst.u.tcp.port = htons(port);
134 if (nf_conntrack_expect_related(exp) == 0)
135 break;
138 if (port == 0)
139 return NF_DROP;
141 if (!mangle[type](skb, newip, port, matchoff, matchlen, ct, ctinfo)) {
142 nf_conntrack_unexpect_related(exp);
143 return NF_DROP;
145 return NF_ACCEPT;
148 static void __exit nf_nat_ftp_fini(void)
150 rcu_assign_pointer(nf_nat_ftp_hook, NULL);
151 synchronize_rcu();
154 static int __init nf_nat_ftp_init(void)
156 BUG_ON(rcu_dereference(nf_nat_ftp_hook));
157 rcu_assign_pointer(nf_nat_ftp_hook, nf_nat_ftp);
158 return 0;
161 /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
162 static int warn_set(const char *val, struct kernel_param *kp)
164 printk(KERN_INFO KBUILD_MODNAME
165 ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
166 return 0;
168 module_param_call(ports, warn_set, NULL, NULL, 0);
170 module_init(nf_nat_ftp_init);
171 module_exit(nf_nat_ftp_fini);