Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* NAT for netfilter; shared with compatibility layer. */ |
2 | ||
3 | /* (C) 1999-2001 Paul `Rusty' Russell | |
4 | * (C) 2002-2004 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/types.h> | |
13 | #include <linux/timer.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/netfilter_ipv4.h> | |
16 | #include <linux/vmalloc.h> | |
17 | #include <net/checksum.h> | |
18 | #include <net/icmp.h> | |
19 | #include <net/ip.h> | |
20 | #include <net/tcp.h> /* For tcp_prot in getorigdst */ | |
21 | #include <linux/icmp.h> | |
22 | #include <linux/udp.h> | |
23 | #include <linux/jhash.h> | |
24 | ||
1da177e4 LT |
25 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
26 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> | |
27 | #include <linux/netfilter_ipv4/ip_conntrack_protocol.h> | |
28 | #include <linux/netfilter_ipv4/ip_nat.h> | |
29 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> | |
30 | #include <linux/netfilter_ipv4/ip_nat_core.h> | |
31 | #include <linux/netfilter_ipv4/ip_nat_helper.h> | |
32 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> | |
1da177e4 LT |
33 | |
34 | #if 0 | |
35 | #define DEBUGP printk | |
36 | #else | |
37 | #define DEBUGP(format, args...) | |
38 | #endif | |
39 | ||
e45b1be8 | 40 | DEFINE_RWLOCK(ip_nat_lock); |
1da177e4 LT |
41 | |
42 | /* Calculated at init based on memory size */ | |
43 | static unsigned int ip_nat_htable_size; | |
44 | ||
45 | static struct list_head *bysource; | |
080774a2 HW |
46 | |
47 | #define MAX_IP_NAT_PROTO 256 | |
d127e94a | 48 | static struct ip_nat_protocol *ip_nat_protos[MAX_IP_NAT_PROTO]; |
1da177e4 | 49 | |
080774a2 HW |
50 | static inline struct ip_nat_protocol * |
51 | __ip_nat_proto_find(u_int8_t protonum) | |
52 | { | |
a441dfdb | 53 | return rcu_dereference(ip_nat_protos[protonum]); |
080774a2 HW |
54 | } |
55 | ||
56 | struct ip_nat_protocol * | |
57 | ip_nat_proto_find_get(u_int8_t protonum) | |
58 | { | |
59 | struct ip_nat_protocol *p; | |
60 | ||
a441dfdb | 61 | rcu_read_lock(); |
080774a2 | 62 | p = __ip_nat_proto_find(protonum); |
d2a7bb71 HW |
63 | if (!try_module_get(p->me)) |
64 | p = &ip_nat_unknown_protocol; | |
a441dfdb | 65 | rcu_read_unlock(); |
080774a2 HW |
66 | |
67 | return p; | |
68 | } | |
188bab3a | 69 | EXPORT_SYMBOL_GPL(ip_nat_proto_find_get); |
080774a2 HW |
70 | |
71 | void | |
72 | ip_nat_proto_put(struct ip_nat_protocol *p) | |
73 | { | |
74 | module_put(p->me); | |
75 | } | |
188bab3a | 76 | EXPORT_SYMBOL_GPL(ip_nat_proto_put); |
1da177e4 LT |
77 | |
78 | /* We keep an extra hash for each conntrack, for fast searching. */ | |
79 | static inline unsigned int | |
80 | hash_by_src(const struct ip_conntrack_tuple *tuple) | |
81 | { | |
82 | /* Original src, to ensure we map it consistently if poss. */ | |
a76b11dd | 83 | return jhash_3words((__force u32)tuple->src.ip, tuple->src.u.all, |
1da177e4 LT |
84 | tuple->dst.protonum, 0) % ip_nat_htable_size; |
85 | } | |
86 | ||
87 | /* Noone using conntrack by the time this called. */ | |
88 | static void ip_nat_cleanup_conntrack(struct ip_conntrack *conn) | |
89 | { | |
90 | if (!(conn->status & IPS_NAT_DONE_MASK)) | |
91 | return; | |
92 | ||
e45b1be8 | 93 | write_lock_bh(&ip_nat_lock); |
1da177e4 | 94 | list_del(&conn->nat.info.bysource); |
e45b1be8 | 95 | write_unlock_bh(&ip_nat_lock); |
1da177e4 LT |
96 | } |
97 | ||
1da177e4 LT |
98 | /* Is this tuple already taken? (not by us) */ |
99 | int | |
100 | ip_nat_used_tuple(const struct ip_conntrack_tuple *tuple, | |
101 | const struct ip_conntrack *ignored_conntrack) | |
102 | { | |
103 | /* Conntrack tracking doesn't keep track of outgoing tuples; only | |
104 | incoming ones. NAT means they don't have a fixed mapping, | |
105 | so we invert the tuple and look for the incoming reply. | |
106 | ||
107 | We could keep a separate hash if this proves too slow. */ | |
108 | struct ip_conntrack_tuple reply; | |
109 | ||
110 | invert_tuplepr(&reply, tuple); | |
111 | return ip_conntrack_tuple_taken(&reply, ignored_conntrack); | |
112 | } | |
188bab3a | 113 | EXPORT_SYMBOL(ip_nat_used_tuple); |
1da177e4 LT |
114 | |
115 | /* If we source map this tuple so reply looks like reply_tuple, will | |
116 | * that meet the constraints of range. */ | |
117 | static int | |
118 | in_range(const struct ip_conntrack_tuple *tuple, | |
119 | const struct ip_nat_range *range) | |
120 | { | |
a441dfdb PM |
121 | struct ip_nat_protocol *proto; |
122 | int ret = 0; | |
1da177e4 LT |
123 | |
124 | /* If we are supposed to map IPs, then we must be in the | |
125 | range specified, otherwise let this drag us onto a new src IP. */ | |
126 | if (range->flags & IP_NAT_RANGE_MAP_IPS) { | |
127 | if (ntohl(tuple->src.ip) < ntohl(range->min_ip) | |
128 | || ntohl(tuple->src.ip) > ntohl(range->max_ip)) | |
129 | return 0; | |
130 | } | |
131 | ||
a441dfdb PM |
132 | rcu_read_lock(); |
133 | proto = __ip_nat_proto_find(tuple->dst.protonum); | |
1da177e4 LT |
134 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) |
135 | || proto->in_range(tuple, IP_NAT_MANIP_SRC, | |
136 | &range->min, &range->max)) | |
a441dfdb PM |
137 | ret = 1; |
138 | rcu_read_unlock(); | |
1da177e4 | 139 | |
a441dfdb | 140 | return ret; |
1da177e4 LT |
141 | } |
142 | ||
143 | static inline int | |
144 | same_src(const struct ip_conntrack *ct, | |
145 | const struct ip_conntrack_tuple *tuple) | |
146 | { | |
147 | return (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum | |
148 | == tuple->dst.protonum | |
149 | && ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip | |
150 | == tuple->src.ip | |
151 | && ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all | |
152 | == tuple->src.u.all); | |
153 | } | |
154 | ||
155 | /* Only called for SRC manip */ | |
156 | static int | |
157 | find_appropriate_src(const struct ip_conntrack_tuple *tuple, | |
158 | struct ip_conntrack_tuple *result, | |
159 | const struct ip_nat_range *range) | |
160 | { | |
161 | unsigned int h = hash_by_src(tuple); | |
162 | struct ip_conntrack *ct; | |
163 | ||
e45b1be8 | 164 | read_lock_bh(&ip_nat_lock); |
1da177e4 LT |
165 | list_for_each_entry(ct, &bysource[h], nat.info.bysource) { |
166 | if (same_src(ct, tuple)) { | |
167 | /* Copy source part from reply tuple. */ | |
168 | invert_tuplepr(result, | |
169 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); | |
170 | result->dst = tuple->dst; | |
171 | ||
172 | if (in_range(result, range)) { | |
e45b1be8 | 173 | read_unlock_bh(&ip_nat_lock); |
1da177e4 LT |
174 | return 1; |
175 | } | |
176 | } | |
177 | } | |
e45b1be8 | 178 | read_unlock_bh(&ip_nat_lock); |
1da177e4 LT |
179 | return 0; |
180 | } | |
181 | ||
182 | /* For [FUTURE] fragmentation handling, we want the least-used | |
183 | src-ip/dst-ip/proto triple. Fairness doesn't come into it. Thus | |
184 | if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports | |
185 | 1-65535, we don't do pro-rata allocation based on ports; we choose | |
186 | the ip with the lowest src-ip/dst-ip/proto usage. | |
187 | */ | |
188 | static void | |
189 | find_best_ips_proto(struct ip_conntrack_tuple *tuple, | |
190 | const struct ip_nat_range *range, | |
191 | const struct ip_conntrack *conntrack, | |
192 | enum ip_nat_manip_type maniptype) | |
193 | { | |
a76b11dd | 194 | __be32 *var_ipp; |
1da177e4 LT |
195 | /* Host order */ |
196 | u_int32_t minip, maxip, j; | |
197 | ||
198 | /* No IP mapping? Do nothing. */ | |
199 | if (!(range->flags & IP_NAT_RANGE_MAP_IPS)) | |
200 | return; | |
201 | ||
202 | if (maniptype == IP_NAT_MANIP_SRC) | |
203 | var_ipp = &tuple->src.ip; | |
204 | else | |
205 | var_ipp = &tuple->dst.ip; | |
206 | ||
207 | /* Fast path: only one choice. */ | |
208 | if (range->min_ip == range->max_ip) { | |
209 | *var_ipp = range->min_ip; | |
210 | return; | |
211 | } | |
212 | ||
213 | /* Hashing source and destination IPs gives a fairly even | |
214 | * spread in practice (if there are a small number of IPs | |
215 | * involved, there usually aren't that many connections | |
216 | * anyway). The consistency means that servers see the same | |
217 | * client coming from the same IP (some Internet Banking sites | |
218 | * like this), even across reboots. */ | |
219 | minip = ntohl(range->min_ip); | |
220 | maxip = ntohl(range->max_ip); | |
a76b11dd | 221 | j = jhash_2words((__force u32)tuple->src.ip, (__force u32)tuple->dst.ip, 0); |
1da177e4 LT |
222 | *var_ipp = htonl(minip + j % (maxip - minip + 1)); |
223 | } | |
224 | ||
225 | /* Manipulate the tuple into the range given. For NF_IP_POST_ROUTING, | |
226 | * we change the source to map into the range. For NF_IP_PRE_ROUTING | |
227 | * and NF_IP_LOCAL_OUT, we change the destination to map into the | |
228 | * range. It might not be possible to get a unique tuple, but we try. | |
229 | * At worst (or if we race), we will end up with a final duplicate in | |
230 | * __ip_conntrack_confirm and drop the packet. */ | |
231 | static void | |
232 | get_unique_tuple(struct ip_conntrack_tuple *tuple, | |
233 | const struct ip_conntrack_tuple *orig_tuple, | |
234 | const struct ip_nat_range *range, | |
235 | struct ip_conntrack *conntrack, | |
236 | enum ip_nat_manip_type maniptype) | |
237 | { | |
080774a2 | 238 | struct ip_nat_protocol *proto; |
1da177e4 LT |
239 | |
240 | /* 1) If this srcip/proto/src-proto-part is currently mapped, | |
241 | and that same mapping gives a unique tuple within the given | |
242 | range, use that. | |
243 | ||
244 | This is only required for source (ie. NAT/masq) mappings. | |
245 | So far, we don't do local source mappings, so multiple | |
246 | manips not an issue. */ | |
247 | if (maniptype == IP_NAT_MANIP_SRC) { | |
248 | if (find_appropriate_src(orig_tuple, tuple, range)) { | |
249 | DEBUGP("get_unique_tuple: Found current src map\n"); | |
41f4689a EL |
250 | if (!(range->flags & IP_NAT_RANGE_PROTO_RANDOM)) |
251 | if (!ip_nat_used_tuple(tuple, conntrack)) | |
252 | return; | |
1da177e4 LT |
253 | } |
254 | } | |
255 | ||
256 | /* 2) Select the least-used IP/proto combination in the given | |
257 | range. */ | |
258 | *tuple = *orig_tuple; | |
259 | find_best_ips_proto(tuple, range, conntrack, maniptype); | |
260 | ||
261 | /* 3) The per-protocol part of the manip is made to map into | |
262 | the range to make a unique tuple. */ | |
263 | ||
a441dfdb PM |
264 | rcu_read_lock(); |
265 | proto = __ip_nat_proto_find(orig_tuple->dst.protonum); | |
080774a2 | 266 | |
41f4689a EL |
267 | /* Change protocol info to have some randomization */ |
268 | if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) { | |
269 | proto->unique_tuple(tuple, range, maniptype, conntrack); | |
a441dfdb | 270 | goto out; |
41f4689a EL |
271 | } |
272 | ||
1da177e4 LT |
273 | /* Only bother mapping if it's not already in range and unique */ |
274 | if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) | |
275 | || proto->in_range(tuple, maniptype, &range->min, &range->max)) | |
a441dfdb PM |
276 | && !ip_nat_used_tuple(tuple, conntrack)) |
277 | goto out; | |
1da177e4 LT |
278 | |
279 | /* Last change: get protocol to try to obtain unique tuple. */ | |
280 | proto->unique_tuple(tuple, range, maniptype, conntrack); | |
a441dfdb PM |
281 | out: |
282 | rcu_read_unlock(); | |
1da177e4 LT |
283 | } |
284 | ||
285 | unsigned int | |
286 | ip_nat_setup_info(struct ip_conntrack *conntrack, | |
287 | const struct ip_nat_range *range, | |
288 | unsigned int hooknum) | |
289 | { | |
290 | struct ip_conntrack_tuple curr_tuple, new_tuple; | |
291 | struct ip_nat_info *info = &conntrack->nat.info; | |
292 | int have_to_hash = !(conntrack->status & IPS_NAT_DONE_MASK); | |
293 | enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum); | |
294 | ||
295 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING | |
296 | || hooknum == NF_IP_POST_ROUTING | |
297 | || hooknum == NF_IP_LOCAL_IN | |
298 | || hooknum == NF_IP_LOCAL_OUT); | |
299 | BUG_ON(ip_nat_initialized(conntrack, maniptype)); | |
300 | ||
301 | /* What we've got will look like inverse of reply. Normally | |
302 | this is what is in the conntrack, except for prior | |
303 | manipulations (future optimization: if num_manips == 0, | |
304 | orig_tp = | |
305 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple) */ | |
306 | invert_tuplepr(&curr_tuple, | |
307 | &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple); | |
308 | ||
309 | get_unique_tuple(&new_tuple, &curr_tuple, range, conntrack, maniptype); | |
310 | ||
311 | if (!ip_ct_tuple_equal(&new_tuple, &curr_tuple)) { | |
312 | struct ip_conntrack_tuple reply; | |
313 | ||
314 | /* Alter conntrack table so will recognize replies. */ | |
315 | invert_tuplepr(&reply, &new_tuple); | |
316 | ip_conntrack_alter_reply(conntrack, &reply); | |
317 | ||
318 | /* Non-atomic: we own this at the moment. */ | |
319 | if (maniptype == IP_NAT_MANIP_SRC) | |
320 | conntrack->status |= IPS_SRC_NAT; | |
321 | else | |
322 | conntrack->status |= IPS_DST_NAT; | |
323 | } | |
324 | ||
325 | /* Place in source hash if this is the first time. */ | |
326 | if (have_to_hash) { | |
327 | unsigned int srchash | |
328 | = hash_by_src(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL] | |
329 | .tuple); | |
e45b1be8 | 330 | write_lock_bh(&ip_nat_lock); |
1da177e4 | 331 | list_add(&info->bysource, &bysource[srchash]); |
e45b1be8 | 332 | write_unlock_bh(&ip_nat_lock); |
1da177e4 LT |
333 | } |
334 | ||
335 | /* It's done. */ | |
336 | if (maniptype == IP_NAT_MANIP_DST) | |
337 | set_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status); | |
338 | else | |
339 | set_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status); | |
340 | ||
341 | return NF_ACCEPT; | |
342 | } | |
188bab3a | 343 | EXPORT_SYMBOL(ip_nat_setup_info); |
1da177e4 LT |
344 | |
345 | /* Returns true if succeeded. */ | |
346 | static int | |
347 | manip_pkt(u_int16_t proto, | |
348 | struct sk_buff **pskb, | |
349 | unsigned int iphdroff, | |
350 | const struct ip_conntrack_tuple *target, | |
351 | enum ip_nat_manip_type maniptype) | |
352 | { | |
353 | struct iphdr *iph; | |
080774a2 | 354 | struct ip_nat_protocol *p; |
1da177e4 | 355 | |
089af26c | 356 | if (!skb_make_writable(pskb, iphdroff + sizeof(*iph))) |
1da177e4 LT |
357 | return 0; |
358 | ||
359 | iph = (void *)(*pskb)->data + iphdroff; | |
360 | ||
361 | /* Manipulate protcol part. */ | |
a441dfdb PM |
362 | |
363 | /* rcu_read_lock()ed by nf_hook_slow */ | |
364 | p = __ip_nat_proto_find(proto); | |
365 | if (!p->manip_pkt(pskb, iphdroff, target, maniptype)) | |
1da177e4 LT |
366 | return 0; |
367 | ||
368 | iph = (void *)(*pskb)->data + iphdroff; | |
369 | ||
370 | if (maniptype == IP_NAT_MANIP_SRC) { | |
43bc0ca7 | 371 | nf_csum_replace4(&iph->check, iph->saddr, target->src.ip); |
1da177e4 LT |
372 | iph->saddr = target->src.ip; |
373 | } else { | |
43bc0ca7 | 374 | nf_csum_replace4(&iph->check, iph->daddr, target->dst.ip); |
1da177e4 LT |
375 | iph->daddr = target->dst.ip; |
376 | } | |
377 | return 1; | |
378 | } | |
379 | ||
380 | /* Do packet manipulations according to ip_nat_setup_info. */ | |
188bab3a HW |
381 | unsigned int ip_nat_packet(struct ip_conntrack *ct, |
382 | enum ip_conntrack_info ctinfo, | |
383 | unsigned int hooknum, | |
384 | struct sk_buff **pskb) | |
1da177e4 LT |
385 | { |
386 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); | |
387 | unsigned long statusbit; | |
388 | enum ip_nat_manip_type mtype = HOOK2MANIP(hooknum); | |
389 | ||
1da177e4 LT |
390 | if (mtype == IP_NAT_MANIP_SRC) |
391 | statusbit = IPS_SRC_NAT; | |
392 | else | |
393 | statusbit = IPS_DST_NAT; | |
394 | ||
395 | /* Invert if this is reply dir. */ | |
396 | if (dir == IP_CT_DIR_REPLY) | |
397 | statusbit ^= IPS_NAT_MASK; | |
398 | ||
399 | /* Non-atomic: these bits don't change. */ | |
400 | if (ct->status & statusbit) { | |
401 | struct ip_conntrack_tuple target; | |
402 | ||
403 | /* We are aiming to look like inverse of other direction. */ | |
404 | invert_tuplepr(&target, &ct->tuplehash[!dir].tuple); | |
405 | ||
406 | if (!manip_pkt(target.dst.protonum, pskb, 0, &target, mtype)) | |
407 | return NF_DROP; | |
408 | } | |
409 | return NF_ACCEPT; | |
410 | } | |
188bab3a | 411 | EXPORT_SYMBOL_GPL(ip_nat_packet); |
1da177e4 LT |
412 | |
413 | /* Dir is direction ICMP is coming from (opposite to packet it contains) */ | |
4cf411de PM |
414 | int ip_nat_icmp_reply_translation(struct ip_conntrack *ct, |
415 | enum ip_conntrack_info ctinfo, | |
416 | unsigned int hooknum, | |
417 | struct sk_buff **pskb) | |
1da177e4 LT |
418 | { |
419 | struct { | |
420 | struct icmphdr icmp; | |
421 | struct iphdr ip; | |
422 | } *inside; | |
642d628b | 423 | struct ip_conntrack_protocol *proto; |
1da177e4 | 424 | struct ip_conntrack_tuple inner, target; |
c9bdd4b5 | 425 | int hdrlen = ip_hdrlen(*pskb); |
4cf411de | 426 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
bc6e14b6 | 427 | unsigned long statusbit; |
4cf411de | 428 | enum ip_nat_manip_type manip = HOOK2MANIP(hooknum); |
1da177e4 | 429 | |
089af26c | 430 | if (!skb_make_writable(pskb, hdrlen + sizeof(*inside))) |
1da177e4 LT |
431 | return 0; |
432 | ||
c9bdd4b5 | 433 | inside = (void *)(*pskb)->data + ip_hdrlen(*pskb); |
1da177e4 LT |
434 | |
435 | /* We're actually going to mangle it beyond trivial checksum | |
436 | adjustment, so make sure the current checksum is correct. */ | |
4cf411de PM |
437 | if (nf_ip_checksum(*pskb, hooknum, hdrlen, 0)) |
438 | return 0; | |
1da177e4 LT |
439 | |
440 | /* Must be RELATED */ | |
441 | IP_NF_ASSERT((*pskb)->nfctinfo == IP_CT_RELATED || | |
442 | (*pskb)->nfctinfo == IP_CT_RELATED+IP_CT_IS_REPLY); | |
443 | ||
444 | /* Redirects on non-null nats must be dropped, else they'll | |
e905a9ed YH |
445 | start talking to each other without our translation, and be |
446 | confused... --RR */ | |
1da177e4 LT |
447 | if (inside->icmp.type == ICMP_REDIRECT) { |
448 | /* If NAT isn't finished, assume it and drop. */ | |
449 | if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK) | |
450 | return 0; | |
451 | ||
452 | if (ct->status & IPS_NAT_MASK) | |
453 | return 0; | |
454 | } | |
455 | ||
456 | DEBUGP("icmp_reply_translation: translating error %p manp %u dir %s\n", | |
457 | *pskb, manip, dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY"); | |
458 | ||
642d628b PM |
459 | /* rcu_read_lock()ed by nf_hook_slow */ |
460 | proto = __ip_conntrack_proto_find(inside->ip.protocol); | |
c9bdd4b5 | 461 | if (!ip_ct_get_tuple(&inside->ip, *pskb, ip_hdrlen(*pskb) + |
e905a9ed | 462 | sizeof(struct icmphdr) + inside->ip.ihl*4, |
642d628b | 463 | &inner, proto)) |
1da177e4 LT |
464 | return 0; |
465 | ||
466 | /* Change inner back to look like incoming packet. We do the | |
467 | opposite manip on this hook to normal, because it might not | |
468 | pass all hooks (locally-generated ICMP). Consider incoming | |
469 | packet: PREROUTING (DST manip), routing produces ICMP, goes | |
470 | through POSTROUTING (which must correct the DST manip). */ | |
471 | if (!manip_pkt(inside->ip.protocol, pskb, | |
c9bdd4b5 | 472 | ip_hdrlen(*pskb) + sizeof(inside->icmp), |
1da177e4 LT |
473 | &ct->tuplehash[!dir].tuple, |
474 | !manip)) | |
475 | return 0; | |
476 | ||
4cf411de PM |
477 | if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) { |
478 | /* Reloading "inside" here since manip_pkt inner. */ | |
c9bdd4b5 | 479 | inside = (void *)(*pskb)->data + ip_hdrlen(*pskb); |
4cf411de PM |
480 | inside->icmp.checksum = 0; |
481 | inside->icmp.checksum = csum_fold(skb_checksum(*pskb, hdrlen, | |
482 | (*pskb)->len - hdrlen, | |
483 | 0)); | |
484 | } | |
1da177e4 LT |
485 | |
486 | /* Change outer to look the reply to an incoming packet | |
487 | * (proto 0 means don't invert per-proto part). */ | |
bc6e14b6 PM |
488 | if (manip == IP_NAT_MANIP_SRC) |
489 | statusbit = IPS_SRC_NAT; | |
490 | else | |
491 | statusbit = IPS_DST_NAT; | |
1da177e4 | 492 | |
bc6e14b6 PM |
493 | /* Invert if this is reply dir. */ |
494 | if (dir == IP_CT_DIR_REPLY) | |
495 | statusbit ^= IPS_NAT_MASK; | |
1da177e4 | 496 | |
bc6e14b6 | 497 | if (ct->status & statusbit) { |
1da177e4 LT |
498 | invert_tuplepr(&target, &ct->tuplehash[!dir].tuple); |
499 | if (!manip_pkt(0, pskb, 0, &target, manip)) | |
500 | return 0; | |
501 | } | |
502 | ||
503 | return 1; | |
504 | } | |
188bab3a | 505 | EXPORT_SYMBOL_GPL(ip_nat_icmp_reply_translation); |
1da177e4 LT |
506 | |
507 | /* Protocol registration. */ | |
508 | int ip_nat_protocol_register(struct ip_nat_protocol *proto) | |
509 | { | |
510 | int ret = 0; | |
511 | ||
e45b1be8 | 512 | write_lock_bh(&ip_nat_lock); |
1da177e4 LT |
513 | if (ip_nat_protos[proto->protonum] != &ip_nat_unknown_protocol) { |
514 | ret = -EBUSY; | |
515 | goto out; | |
516 | } | |
a441dfdb | 517 | rcu_assign_pointer(ip_nat_protos[proto->protonum], proto); |
1da177e4 | 518 | out: |
e45b1be8 | 519 | write_unlock_bh(&ip_nat_lock); |
1da177e4 LT |
520 | return ret; |
521 | } | |
188bab3a | 522 | EXPORT_SYMBOL(ip_nat_protocol_register); |
1da177e4 LT |
523 | |
524 | /* Noone stores the protocol anywhere; simply delete it. */ | |
525 | void ip_nat_protocol_unregister(struct ip_nat_protocol *proto) | |
526 | { | |
e45b1be8 | 527 | write_lock_bh(&ip_nat_lock); |
a441dfdb PM |
528 | rcu_assign_pointer(ip_nat_protos[proto->protonum], |
529 | &ip_nat_unknown_protocol); | |
e45b1be8 | 530 | write_unlock_bh(&ip_nat_lock); |
a441dfdb | 531 | synchronize_rcu(); |
1da177e4 | 532 | } |
188bab3a | 533 | EXPORT_SYMBOL(ip_nat_protocol_unregister); |
1da177e4 | 534 | |
080774a2 HW |
535 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
536 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) | |
537 | int | |
e905a9ed | 538 | ip_nat_port_range_to_nfattr(struct sk_buff *skb, |
080774a2 HW |
539 | const struct ip_nat_range *range) |
540 | { | |
a76b11dd | 541 | NFA_PUT(skb, CTA_PROTONAT_PORT_MIN, sizeof(__be16), |
080774a2 | 542 | &range->min.tcp.port); |
a76b11dd | 543 | NFA_PUT(skb, CTA_PROTONAT_PORT_MAX, sizeof(__be16), |
080774a2 HW |
544 | &range->max.tcp.port); |
545 | ||
546 | return 0; | |
547 | ||
548 | nfattr_failure: | |
549 | return -1; | |
550 | } | |
551 | ||
552 | int | |
553 | ip_nat_port_nfattr_to_range(struct nfattr *tb[], struct ip_nat_range *range) | |
554 | { | |
555 | int ret = 0; | |
e905a9ed | 556 | |
080774a2 HW |
557 | /* we have to return whether we actually parsed something or not */ |
558 | ||
559 | if (tb[CTA_PROTONAT_PORT_MIN-1]) { | |
560 | ret = 1; | |
e905a9ed | 561 | range->min.tcp.port = |
a76b11dd | 562 | *(__be16 *)NFA_DATA(tb[CTA_PROTONAT_PORT_MIN-1]); |
080774a2 | 563 | } |
e905a9ed | 564 | |
080774a2 | 565 | if (!tb[CTA_PROTONAT_PORT_MAX-1]) { |
e905a9ed | 566 | if (ret) |
080774a2 HW |
567 | range->max.tcp.port = range->min.tcp.port; |
568 | } else { | |
569 | ret = 1; | |
e905a9ed | 570 | range->max.tcp.port = |
a76b11dd | 571 | *(__be16 *)NFA_DATA(tb[CTA_PROTONAT_PORT_MAX-1]); |
080774a2 HW |
572 | } |
573 | ||
574 | return ret; | |
575 | } | |
8922bc93 HW |
576 | EXPORT_SYMBOL_GPL(ip_nat_port_nfattr_to_range); |
577 | EXPORT_SYMBOL_GPL(ip_nat_port_range_to_nfattr); | |
080774a2 HW |
578 | #endif |
579 | ||
188bab3a | 580 | static int __init ip_nat_init(void) |
1da177e4 LT |
581 | { |
582 | size_t i; | |
583 | ||
584 | /* Leave them the same for the moment. */ | |
585 | ip_nat_htable_size = ip_conntrack_htable_size; | |
586 | ||
587 | /* One vmalloc for both hash tables */ | |
588 | bysource = vmalloc(sizeof(struct list_head) * ip_nat_htable_size); | |
589 | if (!bysource) | |
590 | return -ENOMEM; | |
591 | ||
592 | /* Sew in builtin protocols. */ | |
e45b1be8 | 593 | write_lock_bh(&ip_nat_lock); |
1da177e4 | 594 | for (i = 0; i < MAX_IP_NAT_PROTO; i++) |
a441dfdb PM |
595 | rcu_assign_pointer(ip_nat_protos[i], &ip_nat_unknown_protocol); |
596 | rcu_assign_pointer(ip_nat_protos[IPPROTO_TCP], &ip_nat_protocol_tcp); | |
597 | rcu_assign_pointer(ip_nat_protos[IPPROTO_UDP], &ip_nat_protocol_udp); | |
598 | rcu_assign_pointer(ip_nat_protos[IPPROTO_ICMP], &ip_nat_protocol_icmp); | |
e45b1be8 | 599 | write_unlock_bh(&ip_nat_lock); |
1da177e4 LT |
600 | |
601 | for (i = 0; i < ip_nat_htable_size; i++) { | |
602 | INIT_LIST_HEAD(&bysource[i]); | |
603 | } | |
604 | ||
605 | /* FIXME: Man, this is a hack. <SIGH> */ | |
6b48a7d0 PM |
606 | IP_NF_ASSERT(rcu_dereference(ip_conntrack_destroyed) == NULL); |
607 | rcu_assign_pointer(ip_conntrack_destroyed, ip_nat_cleanup_conntrack); | |
1da177e4 LT |
608 | |
609 | /* Initialize fake conntrack so that NAT will skip it */ | |
610 | ip_conntrack_untracked.status |= IPS_NAT_DONE_MASK; | |
611 | return 0; | |
612 | } | |
613 | ||
614 | /* Clear NAT section of all conntracks, in case we're loaded again. */ | |
615 | static int clean_nat(struct ip_conntrack *i, void *data) | |
616 | { | |
617 | memset(&i->nat, 0, sizeof(i->nat)); | |
618 | i->status &= ~(IPS_NAT_MASK | IPS_NAT_DONE_MASK | IPS_SEQ_ADJUST); | |
619 | return 0; | |
620 | } | |
621 | ||
188bab3a | 622 | static void __exit ip_nat_cleanup(void) |
1da177e4 LT |
623 | { |
624 | ip_ct_iterate_cleanup(&clean_nat, NULL); | |
6b48a7d0 PM |
625 | rcu_assign_pointer(ip_conntrack_destroyed, NULL); |
626 | synchronize_rcu(); | |
1da177e4 LT |
627 | vfree(bysource); |
628 | } | |
188bab3a HW |
629 | |
630 | MODULE_LICENSE("GPL"); | |
631 | ||
632 | module_init(ip_nat_init); | |
633 | module_exit(ip_nat_cleanup); |