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