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