Commit | Line | Data |
---|---|---|
5b1158e9 JK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | ||
9 | /* Everything about the rules for NAT. */ | |
10 | #include <linux/types.h> | |
11 | #include <linux/ip.h> | |
12 | #include <linux/netfilter.h> | |
13 | #include <linux/netfilter_ipv4.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/kmod.h> | |
16 | #include <linux/skbuff.h> | |
17 | #include <linux/proc_fs.h> | |
18 | #include <net/checksum.h> | |
19 | #include <net/route.h> | |
20 | #include <linux/bitops.h> | |
21 | ||
22 | #include <linux/netfilter_ipv4/ip_tables.h> | |
23 | #include <net/netfilter/nf_nat.h> | |
24 | #include <net/netfilter/nf_nat_core.h> | |
25 | #include <net/netfilter/nf_nat_rule.h> | |
26 | ||
6e23ae2a PM |
27 | #define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \ |
28 | (1 << NF_INET_POST_ROUTING) | \ | |
29 | (1 << NF_INET_LOCAL_OUT)) | |
5b1158e9 JK |
30 | |
31 | static struct | |
32 | { | |
33 | struct ipt_replace repl; | |
34 | struct ipt_standard entries[3]; | |
35 | struct ipt_error term; | |
e099a173 | 36 | } nat_initial_table __net_initdata = { |
5b1158e9 JK |
37 | .repl = { |
38 | .name = "nat", | |
39 | .valid_hooks = NAT_VALID_HOOKS, | |
40 | .num_entries = 4, | |
41 | .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error), | |
42 | .hook_entry = { | |
6e23ae2a PM |
43 | [NF_INET_PRE_ROUTING] = 0, |
44 | [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard), | |
45 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 | |
3c2ad469 | 46 | }, |
5b1158e9 | 47 | .underflow = { |
6e23ae2a PM |
48 | [NF_INET_PRE_ROUTING] = 0, |
49 | [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard), | |
50 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 | |
3c2ad469 | 51 | }, |
5b1158e9 JK |
52 | }, |
53 | .entries = { | |
3c2ad469 PM |
54 | IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */ |
55 | IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */ | |
56 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ | |
5b1158e9 | 57 | }, |
3c2ad469 | 58 | .term = IPT_ERROR_INIT, /* ERROR */ |
5b1158e9 JK |
59 | }; |
60 | ||
e099a173 | 61 | static struct xt_table nat_table = { |
5b1158e9 JK |
62 | .name = "nat", |
63 | .valid_hooks = NAT_VALID_HOOKS, | |
fdccecd0 | 64 | .lock = __RW_LOCK_UNLOCKED(__nat_table.lock), |
5b1158e9 JK |
65 | .me = THIS_MODULE, |
66 | .af = AF_INET, | |
67 | }; | |
68 | ||
69 | /* Source NAT */ | |
3db05fea | 70 | static unsigned int ipt_snat_target(struct sk_buff *skb, |
5b1158e9 JK |
71 | const struct net_device *in, |
72 | const struct net_device *out, | |
73 | unsigned int hooknum, | |
74 | const struct xt_target *target, | |
75 | const void *targinfo) | |
76 | { | |
77 | struct nf_conn *ct; | |
78 | enum ip_conntrack_info ctinfo; | |
79 | const struct nf_nat_multi_range_compat *mr = targinfo; | |
80 | ||
6e23ae2a | 81 | NF_CT_ASSERT(hooknum == NF_INET_POST_ROUTING); |
5b1158e9 | 82 | |
3db05fea | 83 | ct = nf_ct_get(skb, &ctinfo); |
5b1158e9 JK |
84 | |
85 | /* Connection must be valid and new. */ | |
86 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED || | |
e905a9ed | 87 | ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)); |
5b1158e9 JK |
88 | NF_CT_ASSERT(out); |
89 | ||
cc01dcbd | 90 | return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC); |
5b1158e9 JK |
91 | } |
92 | ||
93 | /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */ | |
94 | static void warn_if_extra_mangle(__be32 dstip, __be32 srcip) | |
95 | { | |
96 | static int warned = 0; | |
97 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } }; | |
98 | struct rtable *rt; | |
99 | ||
f206351a | 100 | if (ip_route_output_key(&init_net, &rt, &fl) != 0) |
5b1158e9 JK |
101 | return; |
102 | ||
103 | if (rt->rt_src != srcip && !warned) { | |
104 | printk("NAT: no longer support implicit source local NAT\n"); | |
105 | printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n", | |
106 | NIPQUAD(srcip), NIPQUAD(dstip)); | |
107 | warned = 1; | |
108 | } | |
109 | ip_rt_put(rt); | |
110 | } | |
111 | ||
3db05fea | 112 | static unsigned int ipt_dnat_target(struct sk_buff *skb, |
5b1158e9 JK |
113 | const struct net_device *in, |
114 | const struct net_device *out, | |
115 | unsigned int hooknum, | |
116 | const struct xt_target *target, | |
117 | const void *targinfo) | |
118 | { | |
119 | struct nf_conn *ct; | |
120 | enum ip_conntrack_info ctinfo; | |
121 | const struct nf_nat_multi_range_compat *mr = targinfo; | |
122 | ||
6e23ae2a PM |
123 | NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING || |
124 | hooknum == NF_INET_LOCAL_OUT); | |
5b1158e9 | 125 | |
3db05fea | 126 | ct = nf_ct_get(skb, &ctinfo); |
5b1158e9 JK |
127 | |
128 | /* Connection must be valid and new. */ | |
129 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); | |
130 | ||
6e23ae2a | 131 | if (hooknum == NF_INET_LOCAL_OUT && |
5b1158e9 | 132 | mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) |
3db05fea | 133 | warn_if_extra_mangle(ip_hdr(skb)->daddr, |
5b1158e9 JK |
134 | mr->range[0].min_ip); |
135 | ||
cc01dcbd | 136 | return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST); |
5b1158e9 JK |
137 | } |
138 | ||
e1931b78 JE |
139 | static bool ipt_snat_checkentry(const char *tablename, |
140 | const void *entry, | |
141 | const struct xt_target *target, | |
142 | void *targinfo, | |
143 | unsigned int hook_mask) | |
5b1158e9 | 144 | { |
3cf93c96 | 145 | const struct nf_nat_multi_range_compat *mr = targinfo; |
5b1158e9 JK |
146 | |
147 | /* Must be a valid range */ | |
148 | if (mr->rangesize != 1) { | |
149 | printk("SNAT: multiple ranges no longer supported\n"); | |
e1931b78 | 150 | return false; |
5b1158e9 | 151 | } |
e1931b78 | 152 | return true; |
5b1158e9 JK |
153 | } |
154 | ||
e1931b78 JE |
155 | static bool ipt_dnat_checkentry(const char *tablename, |
156 | const void *entry, | |
157 | const struct xt_target *target, | |
158 | void *targinfo, | |
159 | unsigned int hook_mask) | |
5b1158e9 | 160 | { |
3cf93c96 | 161 | const struct nf_nat_multi_range_compat *mr = targinfo; |
5b1158e9 JK |
162 | |
163 | /* Must be a valid range */ | |
164 | if (mr->rangesize != 1) { | |
165 | printk("DNAT: multiple ranges no longer supported\n"); | |
e1931b78 | 166 | return false; |
5b1158e9 | 167 | } |
e1931b78 | 168 | return true; |
5b1158e9 JK |
169 | } |
170 | ||
170b197c | 171 | unsigned int |
ba4c7cba | 172 | alloc_null_binding(struct nf_conn *ct, unsigned int hooknum) |
5b1158e9 JK |
173 | { |
174 | /* Force range to this IP; let proto decide mapping for | |
175 | per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED). | |
176 | Use reply in case it's already been mangled (eg local packet). | |
177 | */ | |
178 | __be32 ip | |
179 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC | |
180 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip | |
181 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip); | |
182 | struct nf_nat_range range | |
183 | = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } }; | |
184 | ||
0d53778e PM |
185 | pr_debug("Allocating NULL binding for %p (%u.%u.%u.%u)\n", |
186 | ct, NIPQUAD(ip)); | |
cc01dcbd | 187 | return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum)); |
5b1158e9 JK |
188 | } |
189 | ||
3db05fea | 190 | int nf_nat_rule_find(struct sk_buff *skb, |
5b1158e9 JK |
191 | unsigned int hooknum, |
192 | const struct net_device *in, | |
193 | const struct net_device *out, | |
ba4c7cba | 194 | struct nf_conn *ct) |
5b1158e9 | 195 | { |
e099a173 | 196 | struct net *net = nf_ct_net(ct); |
5b1158e9 JK |
197 | int ret; |
198 | ||
e099a173 | 199 | ret = ipt_do_table(skb, hooknum, in, out, net->ipv4.nat_table); |
5b1158e9 JK |
200 | |
201 | if (ret == NF_ACCEPT) { | |
202 | if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum))) | |
203 | /* NUL mapping */ | |
ba4c7cba | 204 | ret = alloc_null_binding(ct, hooknum); |
5b1158e9 JK |
205 | } |
206 | return ret; | |
207 | } | |
208 | ||
9f15c530 | 209 | static struct xt_target ipt_snat_reg __read_mostly = { |
5b1158e9 JK |
210 | .name = "SNAT", |
211 | .target = ipt_snat_target, | |
212 | .targetsize = sizeof(struct nf_nat_multi_range_compat), | |
213 | .table = "nat", | |
6e23ae2a | 214 | .hooks = 1 << NF_INET_POST_ROUTING, |
5b1158e9 JK |
215 | .checkentry = ipt_snat_checkentry, |
216 | .family = AF_INET, | |
217 | }; | |
218 | ||
9f15c530 | 219 | static struct xt_target ipt_dnat_reg __read_mostly = { |
5b1158e9 JK |
220 | .name = "DNAT", |
221 | .target = ipt_dnat_target, | |
222 | .targetsize = sizeof(struct nf_nat_multi_range_compat), | |
223 | .table = "nat", | |
6e23ae2a | 224 | .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT), |
5b1158e9 JK |
225 | .checkentry = ipt_dnat_checkentry, |
226 | .family = AF_INET, | |
227 | }; | |
228 | ||
e099a173 AD |
229 | static int __net_init nf_nat_rule_net_init(struct net *net) |
230 | { | |
231 | net->ipv4.nat_table = ipt_register_table(net, &nat_table, | |
232 | &nat_initial_table.repl); | |
233 | if (IS_ERR(net->ipv4.nat_table)) | |
234 | return PTR_ERR(net->ipv4.nat_table); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | static void __net_exit nf_nat_rule_net_exit(struct net *net) | |
239 | { | |
240 | ipt_unregister_table(net->ipv4.nat_table); | |
241 | } | |
242 | ||
243 | static struct pernet_operations nf_nat_rule_net_ops = { | |
244 | .init = nf_nat_rule_net_init, | |
245 | .exit = nf_nat_rule_net_exit, | |
246 | }; | |
247 | ||
5b1158e9 JK |
248 | int __init nf_nat_rule_init(void) |
249 | { | |
250 | int ret; | |
251 | ||
e099a173 AD |
252 | ret = register_pernet_subsys(&nf_nat_rule_net_ops); |
253 | if (ret != 0) | |
254 | goto out; | |
5b1158e9 JK |
255 | ret = xt_register_target(&ipt_snat_reg); |
256 | if (ret != 0) | |
257 | goto unregister_table; | |
258 | ||
259 | ret = xt_register_target(&ipt_dnat_reg); | |
260 | if (ret != 0) | |
261 | goto unregister_snat; | |
262 | ||
263 | return ret; | |
264 | ||
265 | unregister_snat: | |
266 | xt_unregister_target(&ipt_snat_reg); | |
267 | unregister_table: | |
e099a173 AD |
268 | unregister_pernet_subsys(&nf_nat_rule_net_ops); |
269 | out: | |
5b1158e9 JK |
270 | return ret; |
271 | } | |
272 | ||
273 | void nf_nat_rule_cleanup(void) | |
274 | { | |
275 | xt_unregister_target(&ipt_dnat_reg); | |
276 | xt_unregister_target(&ipt_snat_reg); | |
e099a173 | 277 | unregister_pernet_subsys(&nf_nat_rule_net_ops); |
5b1158e9 | 278 | } |