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 | ||
27 | #if 0 | |
28 | #define DEBUGP printk | |
29 | #else | |
30 | #define DEBUGP(format, args...) | |
31 | #endif | |
32 | ||
33 | #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT)) | |
34 | ||
35 | static struct | |
36 | { | |
37 | struct ipt_replace repl; | |
38 | struct ipt_standard entries[3]; | |
39 | struct ipt_error term; | |
40 | } nat_initial_table __initdata = { | |
41 | .repl = { | |
42 | .name = "nat", | |
43 | .valid_hooks = NAT_VALID_HOOKS, | |
44 | .num_entries = 4, | |
45 | .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error), | |
46 | .hook_entry = { | |
47 | [NF_IP_PRE_ROUTING] = 0, | |
48 | [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard), | |
3c2ad469 PM |
49 | [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 |
50 | }, | |
5b1158e9 JK |
51 | .underflow = { |
52 | [NF_IP_PRE_ROUTING] = 0, | |
53 | [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard), | |
3c2ad469 PM |
54 | [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 |
55 | }, | |
5b1158e9 JK |
56 | }, |
57 | .entries = { | |
3c2ad469 PM |
58 | IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */ |
59 | IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */ | |
60 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ | |
5b1158e9 | 61 | }, |
3c2ad469 | 62 | .term = IPT_ERROR_INIT, /* ERROR */ |
5b1158e9 JK |
63 | }; |
64 | ||
e60a13e0 | 65 | static struct xt_table nat_table = { |
5b1158e9 JK |
66 | .name = "nat", |
67 | .valid_hooks = NAT_VALID_HOOKS, | |
68 | .lock = RW_LOCK_UNLOCKED, | |
69 | .me = THIS_MODULE, | |
70 | .af = AF_INET, | |
71 | }; | |
72 | ||
73 | /* Source NAT */ | |
74 | static unsigned int ipt_snat_target(struct sk_buff **pskb, | |
75 | const struct net_device *in, | |
76 | const struct net_device *out, | |
77 | unsigned int hooknum, | |
78 | const struct xt_target *target, | |
79 | const void *targinfo) | |
80 | { | |
81 | struct nf_conn *ct; | |
82 | enum ip_conntrack_info ctinfo; | |
83 | const struct nf_nat_multi_range_compat *mr = targinfo; | |
84 | ||
85 | NF_CT_ASSERT(hooknum == NF_IP_POST_ROUTING); | |
86 | ||
87 | ct = nf_ct_get(*pskb, &ctinfo); | |
88 | ||
89 | /* Connection must be valid and new. */ | |
90 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED || | |
e905a9ed | 91 | ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)); |
5b1158e9 JK |
92 | NF_CT_ASSERT(out); |
93 | ||
94 | return nf_nat_setup_info(ct, &mr->range[0], hooknum); | |
95 | } | |
96 | ||
97 | /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */ | |
98 | static void warn_if_extra_mangle(__be32 dstip, __be32 srcip) | |
99 | { | |
100 | static int warned = 0; | |
101 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } }; | |
102 | struct rtable *rt; | |
103 | ||
104 | if (ip_route_output_key(&rt, &fl) != 0) | |
105 | return; | |
106 | ||
107 | if (rt->rt_src != srcip && !warned) { | |
108 | printk("NAT: no longer support implicit source local NAT\n"); | |
109 | printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n", | |
110 | NIPQUAD(srcip), NIPQUAD(dstip)); | |
111 | warned = 1; | |
112 | } | |
113 | ip_rt_put(rt); | |
114 | } | |
115 | ||
116 | static unsigned int ipt_dnat_target(struct sk_buff **pskb, | |
117 | const struct net_device *in, | |
118 | const struct net_device *out, | |
119 | unsigned int hooknum, | |
120 | const struct xt_target *target, | |
121 | const void *targinfo) | |
122 | { | |
123 | struct nf_conn *ct; | |
124 | enum ip_conntrack_info ctinfo; | |
125 | const struct nf_nat_multi_range_compat *mr = targinfo; | |
126 | ||
127 | NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING || | |
128 | hooknum == NF_IP_LOCAL_OUT); | |
129 | ||
130 | ct = nf_ct_get(*pskb, &ctinfo); | |
131 | ||
132 | /* Connection must be valid and new. */ | |
133 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); | |
134 | ||
135 | if (hooknum == NF_IP_LOCAL_OUT && | |
136 | mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) | |
eddc9ec5 | 137 | warn_if_extra_mangle(ip_hdr(*pskb)->daddr, |
5b1158e9 JK |
138 | mr->range[0].min_ip); |
139 | ||
140 | return nf_nat_setup_info(ct, &mr->range[0], hooknum); | |
141 | } | |
142 | ||
143 | static int ipt_snat_checkentry(const char *tablename, | |
144 | const void *entry, | |
145 | const struct xt_target *target, | |
146 | void *targinfo, | |
147 | unsigned int hook_mask) | |
148 | { | |
149 | struct nf_nat_multi_range_compat *mr = targinfo; | |
150 | ||
151 | /* Must be a valid range */ | |
152 | if (mr->rangesize != 1) { | |
153 | printk("SNAT: multiple ranges no longer supported\n"); | |
154 | return 0; | |
155 | } | |
156 | return 1; | |
157 | } | |
158 | ||
159 | static int ipt_dnat_checkentry(const char *tablename, | |
160 | const void *entry, | |
161 | const struct xt_target *target, | |
162 | void *targinfo, | |
163 | unsigned int hook_mask) | |
164 | { | |
165 | struct nf_nat_multi_range_compat *mr = targinfo; | |
166 | ||
167 | /* Must be a valid range */ | |
168 | if (mr->rangesize != 1) { | |
169 | printk("DNAT: multiple ranges no longer supported\n"); | |
170 | return 0; | |
171 | } | |
172 | return 1; | |
173 | } | |
174 | ||
175 | inline unsigned int | |
176 | alloc_null_binding(struct nf_conn *ct, | |
177 | struct nf_nat_info *info, | |
178 | unsigned int hooknum) | |
179 | { | |
180 | /* Force range to this IP; let proto decide mapping for | |
181 | per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED). | |
182 | Use reply in case it's already been mangled (eg local packet). | |
183 | */ | |
184 | __be32 ip | |
185 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC | |
186 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip | |
187 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip); | |
188 | struct nf_nat_range range | |
189 | = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } }; | |
190 | ||
191 | DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", | |
192 | ct, NIPQUAD(ip)); | |
193 | return nf_nat_setup_info(ct, &range, hooknum); | |
194 | } | |
195 | ||
196 | unsigned int | |
197 | alloc_null_binding_confirmed(struct nf_conn *ct, | |
e905a9ed YH |
198 | struct nf_nat_info *info, |
199 | unsigned int hooknum) | |
5b1158e9 JK |
200 | { |
201 | __be32 ip | |
202 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC | |
203 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip | |
204 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip); | |
205 | u_int16_t all | |
206 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC | |
207 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all | |
208 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all); | |
209 | struct nf_nat_range range | |
210 | = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } }; | |
211 | ||
212 | DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n", | |
213 | ct, NIPQUAD(ip)); | |
214 | return nf_nat_setup_info(ct, &range, hooknum); | |
215 | } | |
216 | ||
217 | int nf_nat_rule_find(struct sk_buff **pskb, | |
218 | unsigned int hooknum, | |
219 | const struct net_device *in, | |
220 | const struct net_device *out, | |
221 | struct nf_conn *ct, | |
222 | struct nf_nat_info *info) | |
223 | { | |
224 | int ret; | |
225 | ||
226 | ret = ipt_do_table(pskb, hooknum, in, out, &nat_table); | |
227 | ||
228 | if (ret == NF_ACCEPT) { | |
229 | if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum))) | |
230 | /* NUL mapping */ | |
231 | ret = alloc_null_binding(ct, info, hooknum); | |
232 | } | |
233 | return ret; | |
234 | } | |
235 | ||
6709dbbb | 236 | static struct xt_target ipt_snat_reg = { |
5b1158e9 JK |
237 | .name = "SNAT", |
238 | .target = ipt_snat_target, | |
239 | .targetsize = sizeof(struct nf_nat_multi_range_compat), | |
240 | .table = "nat", | |
241 | .hooks = 1 << NF_IP_POST_ROUTING, | |
242 | .checkentry = ipt_snat_checkentry, | |
243 | .family = AF_INET, | |
244 | }; | |
245 | ||
246 | static struct xt_target ipt_dnat_reg = { | |
247 | .name = "DNAT", | |
248 | .target = ipt_dnat_target, | |
249 | .targetsize = sizeof(struct nf_nat_multi_range_compat), | |
250 | .table = "nat", | |
251 | .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT), | |
252 | .checkentry = ipt_dnat_checkentry, | |
253 | .family = AF_INET, | |
254 | }; | |
255 | ||
256 | int __init nf_nat_rule_init(void) | |
257 | { | |
258 | int ret; | |
259 | ||
260 | ret = ipt_register_table(&nat_table, &nat_initial_table.repl); | |
261 | if (ret != 0) | |
262 | return ret; | |
263 | ret = xt_register_target(&ipt_snat_reg); | |
264 | if (ret != 0) | |
265 | goto unregister_table; | |
266 | ||
267 | ret = xt_register_target(&ipt_dnat_reg); | |
268 | if (ret != 0) | |
269 | goto unregister_snat; | |
270 | ||
271 | return ret; | |
272 | ||
273 | unregister_snat: | |
274 | xt_unregister_target(&ipt_snat_reg); | |
275 | unregister_table: | |
276 | ipt_unregister_table(&nat_table); | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
281 | void nf_nat_rule_cleanup(void) | |
282 | { | |
283 | xt_unregister_target(&ipt_dnat_reg); | |
284 | xt_unregister_target(&ipt_snat_reg); | |
285 | ipt_unregister_table(&nat_table); | |
286 | } |