nf_nat_rule.c 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* (C) 1999-2001 Paul `Rusty' Russell
 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/* Everything about the rules for NAT. */
#include <linux/types.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <net/checksum.h>
#include <net/route.h>
#include <linux/bitops.h>

#include <linux/netfilter_ipv4/ip_tables.h>
#include <net/netfilter/nf_nat.h>
#include <net/netfilter/nf_nat_core.h>
#include <net/netfilter/nf_nat_rule.h>

27 28 29
#define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
			 (1 << NF_INET_POST_ROUTING) | \
			 (1 << NF_INET_LOCAL_OUT))
30 31 32 33 34 35 36 37 38 39 40 41 42

static struct
{
	struct ipt_replace repl;
	struct ipt_standard entries[3];
	struct ipt_error term;
} nat_initial_table __initdata = {
	.repl = {
		.name = "nat",
		.valid_hooks = NAT_VALID_HOOKS,
		.num_entries = 4,
		.size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
		.hook_entry = {
43 44 45
			[NF_INET_PRE_ROUTING] = 0,
			[NF_INET_POST_ROUTING] = sizeof(struct ipt_standard),
			[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
46
		},
47
		.underflow = {
48 49 50
			[NF_INET_PRE_ROUTING] = 0,
			[NF_INET_POST_ROUTING] = sizeof(struct ipt_standard),
			[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
51
		},
52 53
	},
	.entries = {
54 55 56
		IPT_STANDARD_INIT(NF_ACCEPT),	/* PRE_ROUTING */
		IPT_STANDARD_INIT(NF_ACCEPT),	/* POST_ROUTING */
		IPT_STANDARD_INIT(NF_ACCEPT),	/* LOCAL_OUT */
57
	},
58
	.term = IPT_ERROR_INIT,			/* ERROR */
59 60
};

61
static struct xt_table nat_table = {
62 63 64 65 66 67 68 69
	.name		= "nat",
	.valid_hooks	= NAT_VALID_HOOKS,
	.lock		= RW_LOCK_UNLOCKED,
	.me		= THIS_MODULE,
	.af		= AF_INET,
};

/* Source NAT */
70
static unsigned int ipt_snat_target(struct sk_buff *skb,
71 72 73 74 75 76 77 78 79 80
				    const struct net_device *in,
				    const struct net_device *out,
				    unsigned int hooknum,
				    const struct xt_target *target,
				    const void *targinfo)
{
	struct nf_conn *ct;
	enum ip_conntrack_info ctinfo;
	const struct nf_nat_multi_range_compat *mr = targinfo;

81
	NF_CT_ASSERT(hooknum == NF_INET_POST_ROUTING);
82

83
	ct = nf_ct_get(skb, &ctinfo);
84 85 86

	/* Connection must be valid and new. */
	NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
87
			    ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
88 89
	NF_CT_ASSERT(out);

90
	return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
91 92 93 94 95 96 97 98 99
}

/* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
{
	static int warned = 0;
	struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
	struct rtable *rt;

100
	if (ip_route_output_key(&init_net, &rt, &fl) != 0)
101 102 103 104 105 106 107 108 109 110 111
		return;

	if (rt->rt_src != srcip && !warned) {
		printk("NAT: no longer support implicit source local NAT\n");
		printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
		       NIPQUAD(srcip), NIPQUAD(dstip));
		warned = 1;
	}
	ip_rt_put(rt);
}

112
static unsigned int ipt_dnat_target(struct sk_buff *skb,
113 114 115 116 117 118 119 120 121 122
				    const struct net_device *in,
				    const struct net_device *out,
				    unsigned int hooknum,
				    const struct xt_target *target,
				    const void *targinfo)
{
	struct nf_conn *ct;
	enum ip_conntrack_info ctinfo;
	const struct nf_nat_multi_range_compat *mr = targinfo;

123 124
	NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING ||
		     hooknum == NF_INET_LOCAL_OUT);
125

126
	ct = nf_ct_get(skb, &ctinfo);
127 128 129 130

	/* Connection must be valid and new. */
	NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));

131
	if (hooknum == NF_INET_LOCAL_OUT &&
132
	    mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
133
		warn_if_extra_mangle(ip_hdr(skb)->daddr,
134 135
				     mr->range[0].min_ip);

136
	return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST);
137 138
}

139 140 141 142 143
static bool ipt_snat_checkentry(const char *tablename,
				const void *entry,
				const struct xt_target *target,
				void *targinfo,
				unsigned int hook_mask)
144 145 146 147 148 149
{
	struct nf_nat_multi_range_compat *mr = targinfo;

	/* Must be a valid range */
	if (mr->rangesize != 1) {
		printk("SNAT: multiple ranges no longer supported\n");
150
		return false;
151
	}
152
	return true;
153 154
}

155 156 157 158 159
static bool ipt_dnat_checkentry(const char *tablename,
				const void *entry,
				const struct xt_target *target,
				void *targinfo,
				unsigned int hook_mask)
160 161 162 163 164 165
{
	struct nf_nat_multi_range_compat *mr = targinfo;

	/* Must be a valid range */
	if (mr->rangesize != 1) {
		printk("DNAT: multiple ranges no longer supported\n");
166
		return false;
167
	}
168
	return true;
169 170
}

171
unsigned int
172
alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
173 174 175 176 177 178 179 180 181 182 183 184
{
	/* Force range to this IP; let proto decide mapping for
	   per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
	   Use reply in case it's already been mangled (eg local packet).
	*/
	__be32 ip
		= (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
		   ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
		   : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
	struct nf_nat_range range
		= { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };

185 186
	pr_debug("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
		 ct, NIPQUAD(ip));
187
	return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
188 189 190
}

unsigned int
191
alloc_null_binding_confirmed(struct nf_conn *ct, unsigned int hooknum)
192 193 194 195 196
{
	__be32 ip
		= (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
		   ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
		   : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
A
Al Viro 已提交
197
	__be16 all
198 199 200 201 202 203
		= (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
		   ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
		   : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
	struct nf_nat_range range
		= { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };

204 205
	pr_debug("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
		 ct, NIPQUAD(ip));
206
	return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
207 208
}

209
int nf_nat_rule_find(struct sk_buff *skb,
210 211 212
		     unsigned int hooknum,
		     const struct net_device *in,
		     const struct net_device *out,
213
		     struct nf_conn *ct)
214 215 216
{
	int ret;

217
	ret = ipt_do_table(skb, hooknum, in, out, &nat_table);
218 219 220 221

	if (ret == NF_ACCEPT) {
		if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
			/* NUL mapping */
222
			ret = alloc_null_binding(ct, hooknum);
223 224 225 226
	}
	return ret;
}

227
static struct xt_target ipt_snat_reg __read_mostly = {
228 229 230 231
	.name		= "SNAT",
	.target		= ipt_snat_target,
	.targetsize	= sizeof(struct nf_nat_multi_range_compat),
	.table		= "nat",
232
	.hooks		= 1 << NF_INET_POST_ROUTING,
233 234 235 236
	.checkentry	= ipt_snat_checkentry,
	.family		= AF_INET,
};

237
static struct xt_target ipt_dnat_reg __read_mostly = {
238 239 240 241
	.name		= "DNAT",
	.target		= ipt_dnat_target,
	.targetsize	= sizeof(struct nf_nat_multi_range_compat),
	.table		= "nat",
242
	.hooks		= (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	.checkentry	= ipt_dnat_checkentry,
	.family		= AF_INET,
};

int __init nf_nat_rule_init(void)
{
	int ret;

	ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
	if (ret != 0)
		return ret;
	ret = xt_register_target(&ipt_snat_reg);
	if (ret != 0)
		goto unregister_table;

	ret = xt_register_target(&ipt_dnat_reg);
	if (ret != 0)
		goto unregister_snat;

	return ret;

 unregister_snat:
	xt_unregister_target(&ipt_snat_reg);
 unregister_table:
	ipt_unregister_table(&nat_table);

	return ret;
}

void nf_nat_rule_cleanup(void)
{
	xt_unregister_target(&ipt_dnat_reg);
	xt_unregister_target(&ipt_snat_reg);
	ipt_unregister_table(&nat_table);
}