[NETFILTER]: nf_nat: use RCU for bysource hash
[linux-2.6/libata-dev.git] / net / ipv4 / netfilter / nf_nat_core.c
blobc7a985fa30ba4812e2b3c67361384205eaa31fd0
1 /* NAT for netfilter; shared with compatibility layer. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
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 */
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/timer.h>
14 #include <linux/skbuff.h>
15 #include <net/checksum.h>
16 #include <net/icmp.h>
17 #include <net/ip.h>
18 #include <net/tcp.h> /* For tcp_prot in getorigdst */
19 #include <linux/icmp.h>
20 #include <linux/udp.h>
21 #include <linux/jhash.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_nat.h>
27 #include <net/netfilter/nf_nat_protocol.h>
28 #include <net/netfilter/nf_nat_core.h>
29 #include <net/netfilter/nf_nat_helper.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <net/netfilter/nf_conntrack_l3proto.h>
32 #include <net/netfilter/nf_conntrack_l4proto.h>
34 static DEFINE_RWLOCK(nf_nat_lock);
36 static struct nf_conntrack_l3proto *l3proto __read_mostly;
38 /* Calculated at init based on memory size */
39 static unsigned int nf_nat_htable_size __read_mostly;
40 static int nf_nat_vmalloced;
42 static struct hlist_head *bysource __read_mostly;
44 #define MAX_IP_NAT_PROTO 256
45 static const struct nf_nat_protocol *nf_nat_protos[MAX_IP_NAT_PROTO]
46 __read_mostly;
48 static inline const struct nf_nat_protocol *
49 __nf_nat_proto_find(u_int8_t protonum)
51 return rcu_dereference(nf_nat_protos[protonum]);
54 const struct nf_nat_protocol *
55 nf_nat_proto_find_get(u_int8_t protonum)
57 const struct nf_nat_protocol *p;
59 rcu_read_lock();
60 p = __nf_nat_proto_find(protonum);
61 if (!try_module_get(p->me))
62 p = &nf_nat_unknown_protocol;
63 rcu_read_unlock();
65 return p;
67 EXPORT_SYMBOL_GPL(nf_nat_proto_find_get);
69 void
70 nf_nat_proto_put(const struct nf_nat_protocol *p)
72 module_put(p->me);
74 EXPORT_SYMBOL_GPL(nf_nat_proto_put);
76 /* We keep an extra hash for each conntrack, for fast searching. */
77 static inline unsigned int
78 hash_by_src(const struct nf_conntrack_tuple *tuple)
80 unsigned int hash;
82 /* Original src, to ensure we map it consistently if poss. */
83 hash = jhash_3words((__force u32)tuple->src.u3.ip,
84 (__force u32)tuple->src.u.all,
85 tuple->dst.protonum, 0);
86 return ((u64)hash * nf_nat_htable_size) >> 32;
89 /* Is this tuple already taken? (not by us) */
90 int
91 nf_nat_used_tuple(const struct nf_conntrack_tuple *tuple,
92 const struct nf_conn *ignored_conntrack)
94 /* Conntrack tracking doesn't keep track of outgoing tuples; only
95 incoming ones. NAT means they don't have a fixed mapping,
96 so we invert the tuple and look for the incoming reply.
98 We could keep a separate hash if this proves too slow. */
99 struct nf_conntrack_tuple reply;
101 nf_ct_invert_tuplepr(&reply, tuple);
102 return nf_conntrack_tuple_taken(&reply, ignored_conntrack);
104 EXPORT_SYMBOL(nf_nat_used_tuple);
106 /* If we source map this tuple so reply looks like reply_tuple, will
107 * that meet the constraints of range. */
108 static int
109 in_range(const struct nf_conntrack_tuple *tuple,
110 const struct nf_nat_range *range)
112 const struct nf_nat_protocol *proto;
113 int ret = 0;
115 /* If we are supposed to map IPs, then we must be in the
116 range specified, otherwise let this drag us onto a new src IP. */
117 if (range->flags & IP_NAT_RANGE_MAP_IPS) {
118 if (ntohl(tuple->src.u3.ip) < ntohl(range->min_ip) ||
119 ntohl(tuple->src.u3.ip) > ntohl(range->max_ip))
120 return 0;
123 rcu_read_lock();
124 proto = __nf_nat_proto_find(tuple->dst.protonum);
125 if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) ||
126 proto->in_range(tuple, IP_NAT_MANIP_SRC,
127 &range->min, &range->max))
128 ret = 1;
129 rcu_read_unlock();
131 return ret;
134 static inline int
135 same_src(const struct nf_conn *ct,
136 const struct nf_conntrack_tuple *tuple)
138 const struct nf_conntrack_tuple *t;
140 t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
141 return (t->dst.protonum == tuple->dst.protonum &&
142 t->src.u3.ip == tuple->src.u3.ip &&
143 t->src.u.all == tuple->src.u.all);
146 /* Only called for SRC manip */
147 static int
148 find_appropriate_src(const struct nf_conntrack_tuple *tuple,
149 struct nf_conntrack_tuple *result,
150 const struct nf_nat_range *range)
152 unsigned int h = hash_by_src(tuple);
153 struct nf_conn_nat *nat;
154 struct nf_conn *ct;
155 struct hlist_node *n;
157 rcu_read_lock();
158 hlist_for_each_entry_rcu(nat, n, &bysource[h], bysource) {
159 ct = nat->ct;
160 if (same_src(ct, tuple)) {
161 /* Copy source part from reply tuple. */
162 nf_ct_invert_tuplepr(result,
163 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
164 result->dst = tuple->dst;
166 if (in_range(result, range)) {
167 rcu_read_unlock();
168 return 1;
172 rcu_read_unlock();
173 return 0;
176 /* For [FUTURE] fragmentation handling, we want the least-used
177 src-ip/dst-ip/proto triple. Fairness doesn't come into it. Thus
178 if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports
179 1-65535, we don't do pro-rata allocation based on ports; we choose
180 the ip with the lowest src-ip/dst-ip/proto usage.
182 static void
183 find_best_ips_proto(struct nf_conntrack_tuple *tuple,
184 const struct nf_nat_range *range,
185 const struct nf_conn *ct,
186 enum nf_nat_manip_type maniptype)
188 __be32 *var_ipp;
189 /* Host order */
190 u_int32_t minip, maxip, j;
192 /* No IP mapping? Do nothing. */
193 if (!(range->flags & IP_NAT_RANGE_MAP_IPS))
194 return;
196 if (maniptype == IP_NAT_MANIP_SRC)
197 var_ipp = &tuple->src.u3.ip;
198 else
199 var_ipp = &tuple->dst.u3.ip;
201 /* Fast path: only one choice. */
202 if (range->min_ip == range->max_ip) {
203 *var_ipp = range->min_ip;
204 return;
207 /* Hashing source and destination IPs gives a fairly even
208 * spread in practice (if there are a small number of IPs
209 * involved, there usually aren't that many connections
210 * anyway). The consistency means that servers see the same
211 * client coming from the same IP (some Internet Banking sites
212 * like this), even across reboots. */
213 minip = ntohl(range->min_ip);
214 maxip = ntohl(range->max_ip);
215 j = jhash_2words((__force u32)tuple->src.u3.ip,
216 (__force u32)tuple->dst.u3.ip, 0);
217 j = ((u64)j * (maxip - minip + 1)) >> 32;
218 *var_ipp = htonl(minip + j);
221 /* Manipulate the tuple into the range given. For NF_INET_POST_ROUTING,
222 * we change the source to map into the range. For NF_INET_PRE_ROUTING
223 * and NF_INET_LOCAL_OUT, we change the destination to map into the
224 * range. It might not be possible to get a unique tuple, but we try.
225 * At worst (or if we race), we will end up with a final duplicate in
226 * __ip_conntrack_confirm and drop the packet. */
227 static void
228 get_unique_tuple(struct nf_conntrack_tuple *tuple,
229 const struct nf_conntrack_tuple *orig_tuple,
230 const struct nf_nat_range *range,
231 struct nf_conn *ct,
232 enum nf_nat_manip_type maniptype)
234 const struct nf_nat_protocol *proto;
236 /* 1) If this srcip/proto/src-proto-part is currently mapped,
237 and that same mapping gives a unique tuple within the given
238 range, use that.
240 This is only required for source (ie. NAT/masq) mappings.
241 So far, we don't do local source mappings, so multiple
242 manips not an issue. */
243 if (maniptype == IP_NAT_MANIP_SRC) {
244 if (find_appropriate_src(orig_tuple, tuple, range)) {
245 pr_debug("get_unique_tuple: Found current src map\n");
246 if (!(range->flags & IP_NAT_RANGE_PROTO_RANDOM))
247 if (!nf_nat_used_tuple(tuple, ct))
248 return;
252 /* 2) Select the least-used IP/proto combination in the given
253 range. */
254 *tuple = *orig_tuple;
255 find_best_ips_proto(tuple, range, ct, maniptype);
257 /* 3) The per-protocol part of the manip is made to map into
258 the range to make a unique tuple. */
260 rcu_read_lock();
261 proto = __nf_nat_proto_find(orig_tuple->dst.protonum);
263 /* Change protocol info to have some randomization */
264 if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) {
265 proto->unique_tuple(tuple, range, maniptype, ct);
266 goto out;
269 /* Only bother mapping if it's not already in range and unique */
270 if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) ||
271 proto->in_range(tuple, maniptype, &range->min, &range->max)) &&
272 !nf_nat_used_tuple(tuple, ct))
273 goto out;
275 /* Last change: get protocol to try to obtain unique tuple. */
276 proto->unique_tuple(tuple, range, maniptype, ct);
277 out:
278 rcu_read_unlock();
281 unsigned int
282 nf_nat_setup_info(struct nf_conn *ct,
283 const struct nf_nat_range *range,
284 enum nf_nat_manip_type maniptype)
286 struct nf_conntrack_tuple curr_tuple, new_tuple;
287 struct nf_conn_nat *nat;
288 int have_to_hash = !(ct->status & IPS_NAT_DONE_MASK);
290 /* nat helper or nfctnetlink also setup binding */
291 nat = nfct_nat(ct);
292 if (!nat) {
293 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
294 if (nat == NULL) {
295 pr_debug("failed to add NAT extension\n");
296 return NF_ACCEPT;
300 NF_CT_ASSERT(maniptype == IP_NAT_MANIP_SRC ||
301 maniptype == IP_NAT_MANIP_DST);
302 BUG_ON(nf_nat_initialized(ct, maniptype));
304 /* What we've got will look like inverse of reply. Normally
305 this is what is in the conntrack, except for prior
306 manipulations (future optimization: if num_manips == 0,
307 orig_tp =
308 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple) */
309 nf_ct_invert_tuplepr(&curr_tuple,
310 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
312 get_unique_tuple(&new_tuple, &curr_tuple, range, ct, maniptype);
314 if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) {
315 struct nf_conntrack_tuple reply;
317 /* Alter conntrack table so will recognize replies. */
318 nf_ct_invert_tuplepr(&reply, &new_tuple);
319 nf_conntrack_alter_reply(ct, &reply);
321 /* Non-atomic: we own this at the moment. */
322 if (maniptype == IP_NAT_MANIP_SRC)
323 ct->status |= IPS_SRC_NAT;
324 else
325 ct->status |= IPS_DST_NAT;
328 /* Place in source hash if this is the first time. */
329 if (have_to_hash) {
330 unsigned int srchash;
332 srchash = hash_by_src(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
333 write_lock_bh(&nf_nat_lock);
334 /* nf_conntrack_alter_reply might re-allocate exntension aera */
335 nat = nfct_nat(ct);
336 nat->ct = ct;
337 hlist_add_head_rcu(&nat->bysource, &bysource[srchash]);
338 write_unlock_bh(&nf_nat_lock);
341 /* It's done. */
342 if (maniptype == IP_NAT_MANIP_DST)
343 set_bit(IPS_DST_NAT_DONE_BIT, &ct->status);
344 else
345 set_bit(IPS_SRC_NAT_DONE_BIT, &ct->status);
347 return NF_ACCEPT;
349 EXPORT_SYMBOL(nf_nat_setup_info);
351 /* Returns true if succeeded. */
352 static int
353 manip_pkt(u_int16_t proto,
354 struct sk_buff *skb,
355 unsigned int iphdroff,
356 const struct nf_conntrack_tuple *target,
357 enum nf_nat_manip_type maniptype)
359 struct iphdr *iph;
360 const struct nf_nat_protocol *p;
362 if (!skb_make_writable(skb, iphdroff + sizeof(*iph)))
363 return 0;
365 iph = (void *)skb->data + iphdroff;
367 /* Manipulate protcol part. */
369 /* rcu_read_lock()ed by nf_hook_slow */
370 p = __nf_nat_proto_find(proto);
371 if (!p->manip_pkt(skb, iphdroff, target, maniptype))
372 return 0;
374 iph = (void *)skb->data + iphdroff;
376 if (maniptype == IP_NAT_MANIP_SRC) {
377 csum_replace4(&iph->check, iph->saddr, target->src.u3.ip);
378 iph->saddr = target->src.u3.ip;
379 } else {
380 csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip);
381 iph->daddr = target->dst.u3.ip;
383 return 1;
386 /* Do packet manipulations according to nf_nat_setup_info. */
387 unsigned int nf_nat_packet(struct nf_conn *ct,
388 enum ip_conntrack_info ctinfo,
389 unsigned int hooknum,
390 struct sk_buff *skb)
392 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
393 unsigned long statusbit;
394 enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum);
396 if (mtype == IP_NAT_MANIP_SRC)
397 statusbit = IPS_SRC_NAT;
398 else
399 statusbit = IPS_DST_NAT;
401 /* Invert if this is reply dir. */
402 if (dir == IP_CT_DIR_REPLY)
403 statusbit ^= IPS_NAT_MASK;
405 /* Non-atomic: these bits don't change. */
406 if (ct->status & statusbit) {
407 struct nf_conntrack_tuple target;
409 /* We are aiming to look like inverse of other direction. */
410 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
412 if (!manip_pkt(target.dst.protonum, skb, 0, &target, mtype))
413 return NF_DROP;
415 return NF_ACCEPT;
417 EXPORT_SYMBOL_GPL(nf_nat_packet);
419 /* Dir is direction ICMP is coming from (opposite to packet it contains) */
420 int nf_nat_icmp_reply_translation(struct nf_conn *ct,
421 enum ip_conntrack_info ctinfo,
422 unsigned int hooknum,
423 struct sk_buff *skb)
425 struct {
426 struct icmphdr icmp;
427 struct iphdr ip;
428 } *inside;
429 struct nf_conntrack_l4proto *l4proto;
430 struct nf_conntrack_tuple inner, target;
431 int hdrlen = ip_hdrlen(skb);
432 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
433 unsigned long statusbit;
434 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
436 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
437 return 0;
439 inside = (void *)skb->data + ip_hdrlen(skb);
441 /* We're actually going to mangle it beyond trivial checksum
442 adjustment, so make sure the current checksum is correct. */
443 if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
444 return 0;
446 /* Must be RELATED */
447 NF_CT_ASSERT(skb->nfctinfo == IP_CT_RELATED ||
448 skb->nfctinfo == IP_CT_RELATED+IP_CT_IS_REPLY);
450 /* Redirects on non-null nats must be dropped, else they'll
451 start talking to each other without our translation, and be
452 confused... --RR */
453 if (inside->icmp.type == ICMP_REDIRECT) {
454 /* If NAT isn't finished, assume it and drop. */
455 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
456 return 0;
458 if (ct->status & IPS_NAT_MASK)
459 return 0;
462 pr_debug("icmp_reply_translation: translating error %p manip %u "
463 "dir %s\n", skb, manip,
464 dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY");
466 /* rcu_read_lock()ed by nf_hook_slow */
467 l4proto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
469 if (!nf_ct_get_tuple(skb,
470 ip_hdrlen(skb) + sizeof(struct icmphdr),
471 (ip_hdrlen(skb) +
472 sizeof(struct icmphdr) + inside->ip.ihl * 4),
473 (u_int16_t)AF_INET,
474 inside->ip.protocol,
475 &inner, l3proto, l4proto))
476 return 0;
478 /* Change inner back to look like incoming packet. We do the
479 opposite manip on this hook to normal, because it might not
480 pass all hooks (locally-generated ICMP). Consider incoming
481 packet: PREROUTING (DST manip), routing produces ICMP, goes
482 through POSTROUTING (which must correct the DST manip). */
483 if (!manip_pkt(inside->ip.protocol, skb,
484 ip_hdrlen(skb) + sizeof(inside->icmp),
485 &ct->tuplehash[!dir].tuple,
486 !manip))
487 return 0;
489 if (skb->ip_summed != CHECKSUM_PARTIAL) {
490 /* Reloading "inside" here since manip_pkt inner. */
491 inside = (void *)skb->data + ip_hdrlen(skb);
492 inside->icmp.checksum = 0;
493 inside->icmp.checksum =
494 csum_fold(skb_checksum(skb, hdrlen,
495 skb->len - hdrlen, 0));
498 /* Change outer to look the reply to an incoming packet
499 * (proto 0 means don't invert per-proto part). */
500 if (manip == IP_NAT_MANIP_SRC)
501 statusbit = IPS_SRC_NAT;
502 else
503 statusbit = IPS_DST_NAT;
505 /* Invert if this is reply dir. */
506 if (dir == IP_CT_DIR_REPLY)
507 statusbit ^= IPS_NAT_MASK;
509 if (ct->status & statusbit) {
510 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
511 if (!manip_pkt(0, skb, 0, &target, manip))
512 return 0;
515 return 1;
517 EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
519 /* Protocol registration. */
520 int nf_nat_protocol_register(const struct nf_nat_protocol *proto)
522 int ret = 0;
524 write_lock_bh(&nf_nat_lock);
525 if (nf_nat_protos[proto->protonum] != &nf_nat_unknown_protocol) {
526 ret = -EBUSY;
527 goto out;
529 rcu_assign_pointer(nf_nat_protos[proto->protonum], proto);
530 out:
531 write_unlock_bh(&nf_nat_lock);
532 return ret;
534 EXPORT_SYMBOL(nf_nat_protocol_register);
536 /* Noone stores the protocol anywhere; simply delete it. */
537 void nf_nat_protocol_unregister(const struct nf_nat_protocol *proto)
539 write_lock_bh(&nf_nat_lock);
540 rcu_assign_pointer(nf_nat_protos[proto->protonum],
541 &nf_nat_unknown_protocol);
542 write_unlock_bh(&nf_nat_lock);
543 synchronize_rcu();
545 EXPORT_SYMBOL(nf_nat_protocol_unregister);
547 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
549 nf_nat_port_range_to_nlattr(struct sk_buff *skb,
550 const struct nf_nat_range *range)
552 NLA_PUT_BE16(skb, CTA_PROTONAT_PORT_MIN, range->min.tcp.port);
553 NLA_PUT_BE16(skb, CTA_PROTONAT_PORT_MAX, range->max.tcp.port);
555 return 0;
557 nla_put_failure:
558 return -1;
560 EXPORT_SYMBOL_GPL(nf_nat_port_nlattr_to_range);
563 nf_nat_port_nlattr_to_range(struct nlattr *tb[], struct nf_nat_range *range)
565 int ret = 0;
567 /* we have to return whether we actually parsed something or not */
569 if (tb[CTA_PROTONAT_PORT_MIN]) {
570 ret = 1;
571 range->min.tcp.port = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
574 if (!tb[CTA_PROTONAT_PORT_MAX]) {
575 if (ret)
576 range->max.tcp.port = range->min.tcp.port;
577 } else {
578 ret = 1;
579 range->max.tcp.port = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
582 return ret;
584 EXPORT_SYMBOL_GPL(nf_nat_port_range_to_nlattr);
585 #endif
587 /* Noone using conntrack by the time this called. */
588 static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
590 struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
592 if (nat == NULL || nat->ct == NULL)
593 return;
595 NF_CT_ASSERT(nat->ct->status & IPS_NAT_DONE_MASK);
597 write_lock_bh(&nf_nat_lock);
598 hlist_del_rcu(&nat->bysource);
599 nat->ct = NULL;
600 write_unlock_bh(&nf_nat_lock);
603 static void nf_nat_move_storage(struct nf_conn *conntrack, void *old)
605 struct nf_conn_nat *new_nat = nf_ct_ext_find(conntrack, NF_CT_EXT_NAT);
606 struct nf_conn_nat *old_nat = (struct nf_conn_nat *)old;
607 struct nf_conn *ct = old_nat->ct;
609 if (!ct || !(ct->status & IPS_NAT_DONE_MASK))
610 return;
612 write_lock_bh(&nf_nat_lock);
613 hlist_replace_rcu(&old_nat->bysource, &new_nat->bysource);
614 new_nat->ct = ct;
615 write_unlock_bh(&nf_nat_lock);
618 static struct nf_ct_ext_type nat_extend __read_mostly = {
619 .len = sizeof(struct nf_conn_nat),
620 .align = __alignof__(struct nf_conn_nat),
621 .destroy = nf_nat_cleanup_conntrack,
622 .move = nf_nat_move_storage,
623 .id = NF_CT_EXT_NAT,
624 .flags = NF_CT_EXT_F_PREALLOC,
627 static int __init nf_nat_init(void)
629 size_t i;
630 int ret;
632 ret = nf_ct_extend_register(&nat_extend);
633 if (ret < 0) {
634 printk(KERN_ERR "nf_nat_core: Unable to register extension\n");
635 return ret;
638 /* Leave them the same for the moment. */
639 nf_nat_htable_size = nf_conntrack_htable_size;
641 bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size,
642 &nf_nat_vmalloced);
643 if (!bysource) {
644 ret = -ENOMEM;
645 goto cleanup_extend;
648 /* Sew in builtin protocols. */
649 write_lock_bh(&nf_nat_lock);
650 for (i = 0; i < MAX_IP_NAT_PROTO; i++)
651 rcu_assign_pointer(nf_nat_protos[i], &nf_nat_unknown_protocol);
652 rcu_assign_pointer(nf_nat_protos[IPPROTO_TCP], &nf_nat_protocol_tcp);
653 rcu_assign_pointer(nf_nat_protos[IPPROTO_UDP], &nf_nat_protocol_udp);
654 rcu_assign_pointer(nf_nat_protos[IPPROTO_ICMP], &nf_nat_protocol_icmp);
655 write_unlock_bh(&nf_nat_lock);
657 /* Initialize fake conntrack so that NAT will skip it */
658 nf_conntrack_untracked.status |= IPS_NAT_DONE_MASK;
660 l3proto = nf_ct_l3proto_find_get((u_int16_t)AF_INET);
661 return 0;
663 cleanup_extend:
664 nf_ct_extend_unregister(&nat_extend);
665 return ret;
668 /* Clear NAT section of all conntracks, in case we're loaded again. */
669 static int clean_nat(struct nf_conn *i, void *data)
671 struct nf_conn_nat *nat = nfct_nat(i);
673 if (!nat)
674 return 0;
675 memset(nat, 0, sizeof(*nat));
676 i->status &= ~(IPS_NAT_MASK | IPS_NAT_DONE_MASK | IPS_SEQ_ADJUST);
677 return 0;
680 static void __exit nf_nat_cleanup(void)
682 nf_ct_iterate_cleanup(&clean_nat, NULL);
683 synchronize_rcu();
684 nf_ct_free_hashtable(bysource, nf_nat_vmalloced, nf_nat_htable_size);
685 nf_ct_l3proto_put(l3proto);
686 nf_ct_extend_unregister(&nat_extend);
689 MODULE_LICENSE("GPL");
691 module_init(nf_nat_init);
692 module_exit(nf_nat_cleanup);