• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/net/ipv4/netfilter/
1/* FTP extension for TCP NAT alteration. */
2
3/* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 *
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 */
10
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>
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
25MODULE_DESCRIPTION("ftp NAT helper");
26MODULE_ALIAS("ip_nat_ftp");
27
28
29static int nf_nat_ftp_fmt_cmd(enum nf_ct_ftp_type type,
30			      char *buffer, size_t buflen,
31			      __be32 addr, u16 port)
32{
33	switch (type) {
34	case NF_CT_FTP_PORT:
35	case NF_CT_FTP_PASV:
36		return snprintf(buffer, buflen, "%u,%u,%u,%u,%u,%u",
37				((unsigned char *)&addr)[0],
38				((unsigned char *)&addr)[1],
39				((unsigned char *)&addr)[2],
40				((unsigned char *)&addr)[3],
41				port >> 8,
42				port & 0xFF);
43	case NF_CT_FTP_EPRT:
44		return snprintf(buffer, buflen, "|1|%pI4|%u|", &addr, port);
45	case NF_CT_FTP_EPSV:
46		return snprintf(buffer, buflen, "|||%u|", port);
47	}
48
49	return 0;
50}
51
52/* So, this packet has hit the connection tracking matching code.
53   Mangle it, and change the expectation to match the new version. */
54static unsigned int nf_nat_ftp(struct sk_buff *skb,
55			       enum ip_conntrack_info ctinfo,
56			       enum nf_ct_ftp_type type,
57			       unsigned int matchoff,
58			       unsigned int matchlen,
59			       struct nf_conntrack_expect *exp)
60{
61	__be32 newip;
62	u_int16_t port;
63	int dir = CTINFO2DIR(ctinfo);
64	struct nf_conn *ct = exp->master;
65	char buffer[sizeof("|1|255.255.255.255|65535|")];
66	unsigned int buflen;
67
68	pr_debug("FTP_NAT: type %i, off %u len %u\n", type, matchoff, matchlen);
69
70	/* Connection will come from wherever this packet goes, hence !dir */
71	newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
72	exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
73	exp->dir = !dir;
74
75	/* When you see the packet, we need to NAT it the same as the
76	 * this one. */
77	exp->expectfn = nf_nat_follow_master;
78
79	/* Try to get same port: if not, try to change it. */
80	for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
81		exp->tuple.dst.u.tcp.port = htons(port);
82		if (nf_ct_expect_related(exp) == 0)
83			break;
84	}
85
86	if (port == 0)
87		return NF_DROP;
88
89	buflen = nf_nat_ftp_fmt_cmd(type, buffer, sizeof(buffer), newip, port);
90	if (!buflen)
91		goto out;
92
93	pr_debug("calling nf_nat_mangle_tcp_packet\n");
94
95	if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, matchoff,
96				      matchlen, buffer, buflen))
97		goto out;
98
99	return NF_ACCEPT;
100
101out:
102	nf_ct_unexpect_related(exp);
103	return NF_DROP;
104}
105
106static void __exit nf_nat_ftp_fini(void)
107{
108	rcu_assign_pointer(nf_nat_ftp_hook, NULL);
109	synchronize_rcu();
110}
111
112static int __init nf_nat_ftp_init(void)
113{
114	BUG_ON(nf_nat_ftp_hook != NULL);
115	rcu_assign_pointer(nf_nat_ftp_hook, nf_nat_ftp);
116	return 0;
117}
118
119/* Prior to 2.6.11, we had a ports param.  No longer, but don't break users. */
120static int warn_set(const char *val, struct kernel_param *kp)
121{
122	printk(KERN_INFO KBUILD_MODNAME
123	       ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
124	return 0;
125}
126module_param_call(ports, warn_set, NULL, NULL, 0);
127
128module_init(nf_nat_ftp_init);
129module_exit(nf_nat_ftp_fini);
130