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.
11 #include <linux/module.h>
12 #include <linux/moduleparam.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");
31 #define DEBUGP(format, args...)
34 /* FIXME: Time out? --RR */
37 mangle_rfc959_packet(struct sk_buff
**pskb
,
40 unsigned int matchoff
,
41 unsigned int matchlen
,
43 enum ip_conntrack_info ctinfo
,
46 char buffer
[sizeof("nnn,nnn,nnn,nnn,nnn,nnn")];
48 sprintf(buffer
, "%u,%u,%u,%u,%u,%u",
49 NIPQUAD(newip
), port
>>8, port
&0xFF);
51 DEBUGP("calling nf_nat_mangle_tcp_packet\n");
53 *seq
+= strlen(buffer
) - matchlen
;
54 return nf_nat_mangle_tcp_packet(pskb
, ct
, ctinfo
, matchoff
,
55 matchlen
, buffer
, strlen(buffer
));
58 /* |1|132.235.1.2|6275| */
60 mangle_eprt_packet(struct sk_buff
**pskb
,
63 unsigned int matchoff
,
64 unsigned int matchlen
,
66 enum ip_conntrack_info ctinfo
,
69 char buffer
[sizeof("|1|255.255.255.255|65535|")];
71 sprintf(buffer
, "|1|%u.%u.%u.%u|%u|", NIPQUAD(newip
), port
);
73 DEBUGP("calling nf_nat_mangle_tcp_packet\n");
75 *seq
+= strlen(buffer
) - matchlen
;
76 return nf_nat_mangle_tcp_packet(pskb
, ct
, ctinfo
, matchoff
,
77 matchlen
, buffer
, strlen(buffer
));
80 /* |1|132.235.1.2|6275| */
82 mangle_epsv_packet(struct sk_buff
**pskb
,
85 unsigned int matchoff
,
86 unsigned int matchlen
,
88 enum ip_conntrack_info ctinfo
,
91 char buffer
[sizeof("|||65535|")];
93 sprintf(buffer
, "|||%u|", port
);
95 DEBUGP("calling nf_nat_mangle_tcp_packet\n");
97 *seq
+= strlen(buffer
) - matchlen
;
98 return nf_nat_mangle_tcp_packet(pskb
, ct
, ctinfo
, matchoff
,
99 matchlen
, buffer
, strlen(buffer
));
102 static int (*mangle
[])(struct sk_buff
**, __be32
, u_int16_t
,
103 unsigned int, unsigned int, struct nf_conn
*,
104 enum ip_conntrack_info
, u32
*seq
)
106 [NF_CT_FTP_PORT
] = mangle_rfc959_packet
,
107 [NF_CT_FTP_PASV
] = mangle_rfc959_packet
,
108 [NF_CT_FTP_EPRT
] = mangle_eprt_packet
,
109 [NF_CT_FTP_EPSV
] = mangle_epsv_packet
112 /* So, this packet has hit the connection tracking matching code.
113 Mangle it, and change the expectation to match the new version. */
114 static unsigned int nf_nat_ftp(struct sk_buff
**pskb
,
115 enum ip_conntrack_info ctinfo
,
116 enum nf_ct_ftp_type type
,
117 unsigned int matchoff
,
118 unsigned int matchlen
,
119 struct nf_conntrack_expect
*exp
,
124 int dir
= CTINFO2DIR(ctinfo
);
125 struct nf_conn
*ct
= exp
->master
;
127 DEBUGP("FTP_NAT: type %i, off %u len %u\n", type
, matchoff
, matchlen
);
129 /* Connection will come from wherever this packet goes, hence !dir */
130 newip
= ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
;
131 exp
->saved_proto
.tcp
.port
= exp
->tuple
.dst
.u
.tcp
.port
;
134 /* When you see the packet, we need to NAT it the same as the
136 exp
->expectfn
= nf_nat_follow_master
;
138 /* Try to get same port: if not, try to change it. */
139 for (port
= ntohs(exp
->saved_proto
.tcp
.port
); port
!= 0; port
++) {
140 exp
->tuple
.dst
.u
.tcp
.port
= htons(port
);
141 if (nf_conntrack_expect_related(exp
) == 0)
148 if (!mangle
[type
](pskb
, newip
, port
, matchoff
, matchlen
, ct
, ctinfo
,
150 nf_conntrack_unexpect_related(exp
);
156 static void __exit
nf_nat_ftp_fini(void)
158 rcu_assign_pointer(nf_nat_ftp_hook
, NULL
);
162 static int __init
nf_nat_ftp_init(void)
164 BUG_ON(rcu_dereference(nf_nat_ftp_hook
));
165 rcu_assign_pointer(nf_nat_ftp_hook
, nf_nat_ftp
);
169 /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
170 static int warn_set(const char *val
, struct kernel_param
*kp
)
172 printk(KERN_INFO KBUILD_MODNAME
173 ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
176 module_param_call(ports
, warn_set
, NULL
, NULL
, 0);
178 module_init(nf_nat_ftp_init
);
179 module_exit(nf_nat_ftp_fini
);