2 * (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4 * (C) 2011 Patrick McHardy <kaber@trash.net>
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.
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/timer.h>
14 #include <linux/skbuff.h>
15 #include <linux/gfp.h>
17 #include <linux/jhash.h>
18 #include <linux/rtnetlink.h>
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_core.h>
22 #include <net/netfilter/nf_nat.h>
23 #include <net/netfilter/nf_nat_l3proto.h>
24 #include <net/netfilter/nf_nat_l4proto.h>
25 #include <net/netfilter/nf_nat_core.h>
26 #include <net/netfilter/nf_nat_helper.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
28 #include <net/netfilter/nf_conntrack_seqadj.h>
29 #include <net/netfilter/nf_conntrack_l3proto.h>
30 #include <net/netfilter/nf_conntrack_zones.h>
31 #include <linux/netfilter/nf_nat.h>
33 static DEFINE_MUTEX(nf_nat_proto_mutex
);
34 static const struct nf_nat_l3proto __rcu
*nf_nat_l3protos
[NFPROTO_NUMPROTO
]
36 static const struct nf_nat_l4proto __rcu
**nf_nat_l4protos
[NFPROTO_NUMPROTO
]
39 struct nf_nat_conn_key
{
40 const struct net
*net
;
41 const struct nf_conntrack_tuple
*tuple
;
42 const struct nf_conntrack_zone
*zone
;
45 static struct rhltable nf_nat_bysource_table
;
47 inline const struct nf_nat_l3proto
*
48 __nf_nat_l3proto_find(u8 family
)
50 return rcu_dereference(nf_nat_l3protos
[family
]);
53 inline const struct nf_nat_l4proto
*
54 __nf_nat_l4proto_find(u8 family
, u8 protonum
)
56 return rcu_dereference(nf_nat_l4protos
[family
][protonum
]);
58 EXPORT_SYMBOL_GPL(__nf_nat_l4proto_find
);
61 static void __nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
)
63 const struct nf_nat_l3proto
*l3proto
;
64 const struct nf_conn
*ct
;
65 enum ip_conntrack_info ctinfo
;
66 enum ip_conntrack_dir dir
;
67 unsigned long statusbit
;
70 ct
= nf_ct_get(skb
, &ctinfo
);
74 family
= nf_ct_l3num(ct
);
75 l3proto
= __nf_nat_l3proto_find(family
);
79 dir
= CTINFO2DIR(ctinfo
);
80 if (dir
== IP_CT_DIR_ORIGINAL
)
81 statusbit
= IPS_DST_NAT
;
83 statusbit
= IPS_SRC_NAT
;
85 l3proto
->decode_session(skb
, ct
, dir
, statusbit
, fl
);
88 int nf_xfrm_me_harder(struct net
*net
, struct sk_buff
*skb
, unsigned int family
)
92 struct dst_entry
*dst
;
95 err
= xfrm_decode_session(skb
, &fl
, family
);
101 dst
= ((struct xfrm_dst
*)dst
)->route
;
104 dst
= xfrm_lookup(net
, dst
, &fl
, skb
->sk
, 0);
109 skb_dst_set(skb
, dst
);
111 /* Change in oif may mean change in hh_len. */
112 hh_len
= skb_dst(skb
)->dev
->hard_header_len
;
113 if (skb_headroom(skb
) < hh_len
&&
114 pskb_expand_head(skb
, hh_len
- skb_headroom(skb
), 0, GFP_ATOMIC
))
118 EXPORT_SYMBOL(nf_xfrm_me_harder
);
119 #endif /* CONFIG_XFRM */
121 static u32
nf_nat_bysource_hash(const void *data
, u32 len
, u32 seed
)
123 const struct nf_conntrack_tuple
*t
;
124 const struct nf_conn
*ct
= data
;
126 t
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
127 /* Original src, to ensure we map it consistently if poss. */
129 seed
^= net_hash_mix(nf_ct_net(ct
));
130 return jhash2((const u32
*)&t
->src
, sizeof(t
->src
) / sizeof(u32
),
131 t
->dst
.protonum
^ seed
);
134 /* Is this tuple already taken? (not by us) */
136 nf_nat_used_tuple(const struct nf_conntrack_tuple
*tuple
,
137 const struct nf_conn
*ignored_conntrack
)
139 /* Conntrack tracking doesn't keep track of outgoing tuples; only
140 * incoming ones. NAT means they don't have a fixed mapping,
141 * so we invert the tuple and look for the incoming reply.
143 * We could keep a separate hash if this proves too slow.
145 struct nf_conntrack_tuple reply
;
147 nf_ct_invert_tuplepr(&reply
, tuple
);
148 return nf_conntrack_tuple_taken(&reply
, ignored_conntrack
);
150 EXPORT_SYMBOL(nf_nat_used_tuple
);
152 /* If we source map this tuple so reply looks like reply_tuple, will
153 * that meet the constraints of range.
155 static int in_range(const struct nf_nat_l3proto
*l3proto
,
156 const struct nf_nat_l4proto
*l4proto
,
157 const struct nf_conntrack_tuple
*tuple
,
158 const struct nf_nat_range
*range
)
160 /* If we are supposed to map IPs, then we must be in the
161 * range specified, otherwise let this drag us onto a new src IP.
163 if (range
->flags
& NF_NAT_RANGE_MAP_IPS
&&
164 !l3proto
->in_range(tuple
, range
))
167 if (!(range
->flags
& NF_NAT_RANGE_PROTO_SPECIFIED
) ||
168 l4proto
->in_range(tuple
, NF_NAT_MANIP_SRC
,
169 &range
->min_proto
, &range
->max_proto
))
176 same_src(const struct nf_conn
*ct
,
177 const struct nf_conntrack_tuple
*tuple
)
179 const struct nf_conntrack_tuple
*t
;
181 t
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
182 return (t
->dst
.protonum
== tuple
->dst
.protonum
&&
183 nf_inet_addr_cmp(&t
->src
.u3
, &tuple
->src
.u3
) &&
184 t
->src
.u
.all
== tuple
->src
.u
.all
);
187 static int nf_nat_bysource_cmp(struct rhashtable_compare_arg
*arg
,
190 const struct nf_nat_conn_key
*key
= arg
->key
;
191 const struct nf_conn
*ct
= obj
;
193 if (!same_src(ct
, key
->tuple
) ||
194 !net_eq(nf_ct_net(ct
), key
->net
) ||
195 !nf_ct_zone_equal(ct
, key
->zone
, IP_CT_DIR_ORIGINAL
))
201 static struct rhashtable_params nf_nat_bysource_params
= {
202 .head_offset
= offsetof(struct nf_conn
, nat_bysource
),
203 .obj_hashfn
= nf_nat_bysource_hash
,
204 .obj_cmpfn
= nf_nat_bysource_cmp
,
209 /* Only called for SRC manip */
211 find_appropriate_src(struct net
*net
,
212 const struct nf_conntrack_zone
*zone
,
213 const struct nf_nat_l3proto
*l3proto
,
214 const struct nf_nat_l4proto
*l4proto
,
215 const struct nf_conntrack_tuple
*tuple
,
216 struct nf_conntrack_tuple
*result
,
217 const struct nf_nat_range
*range
)
219 const struct nf_conn
*ct
;
220 struct nf_nat_conn_key key
= {
225 struct rhlist_head
*hl
;
227 hl
= rhltable_lookup(&nf_nat_bysource_table
, &key
,
228 nf_nat_bysource_params
);
232 ct
= container_of(hl
, typeof(*ct
), nat_bysource
);
234 nf_ct_invert_tuplepr(result
,
235 &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
236 result
->dst
= tuple
->dst
;
238 return in_range(l3proto
, l4proto
, result
, range
);
241 /* For [FUTURE] fragmentation handling, we want the least-used
242 * src-ip/dst-ip/proto triple. Fairness doesn't come into it. Thus
243 * if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports
244 * 1-65535, we don't do pro-rata allocation based on ports; we choose
245 * the ip with the lowest src-ip/dst-ip/proto usage.
248 find_best_ips_proto(const struct nf_conntrack_zone
*zone
,
249 struct nf_conntrack_tuple
*tuple
,
250 const struct nf_nat_range
*range
,
251 const struct nf_conn
*ct
,
252 enum nf_nat_manip_type maniptype
)
254 union nf_inet_addr
*var_ipp
;
257 u32 minip
, maxip
, j
, dist
;
260 /* No IP mapping? Do nothing. */
261 if (!(range
->flags
& NF_NAT_RANGE_MAP_IPS
))
264 if (maniptype
== NF_NAT_MANIP_SRC
)
265 var_ipp
= &tuple
->src
.u3
;
267 var_ipp
= &tuple
->dst
.u3
;
269 /* Fast path: only one choice. */
270 if (nf_inet_addr_cmp(&range
->min_addr
, &range
->max_addr
)) {
271 *var_ipp
= range
->min_addr
;
275 if (nf_ct_l3num(ct
) == NFPROTO_IPV4
)
276 max
= sizeof(var_ipp
->ip
) / sizeof(u32
) - 1;
278 max
= sizeof(var_ipp
->ip6
) / sizeof(u32
) - 1;
280 /* Hashing source and destination IPs gives a fairly even
281 * spread in practice (if there are a small number of IPs
282 * involved, there usually aren't that many connections
283 * anyway). The consistency means that servers see the same
284 * client coming from the same IP (some Internet Banking sites
285 * like this), even across reboots.
287 j
= jhash2((u32
*)&tuple
->src
.u3
, sizeof(tuple
->src
.u3
) / sizeof(u32
),
288 range
->flags
& NF_NAT_RANGE_PERSISTENT
?
289 0 : (__force u32
)tuple
->dst
.u3
.all
[max
] ^ zone
->id
);
292 for (i
= 0; i
<= max
; i
++) {
293 /* If first bytes of the address are at the maximum, use the
294 * distance. Otherwise use the full range.
297 minip
= ntohl((__force __be32
)range
->min_addr
.all
[i
]);
298 maxip
= ntohl((__force __be32
)range
->max_addr
.all
[i
]);
299 dist
= maxip
- minip
+ 1;
305 var_ipp
->all
[i
] = (__force __u32
)
306 htonl(minip
+ reciprocal_scale(j
, dist
));
307 if (var_ipp
->all
[i
] != range
->max_addr
.all
[i
])
310 if (!(range
->flags
& NF_NAT_RANGE_PERSISTENT
))
311 j
^= (__force u32
)tuple
->dst
.u3
.all
[i
];
315 /* Manipulate the tuple into the range given. For NF_INET_POST_ROUTING,
316 * we change the source to map into the range. For NF_INET_PRE_ROUTING
317 * and NF_INET_LOCAL_OUT, we change the destination to map into the
318 * range. It might not be possible to get a unique tuple, but we try.
319 * At worst (or if we race), we will end up with a final duplicate in
320 * __ip_conntrack_confirm and drop the packet. */
322 get_unique_tuple(struct nf_conntrack_tuple
*tuple
,
323 const struct nf_conntrack_tuple
*orig_tuple
,
324 const struct nf_nat_range
*range
,
326 enum nf_nat_manip_type maniptype
)
328 const struct nf_conntrack_zone
*zone
;
329 const struct nf_nat_l3proto
*l3proto
;
330 const struct nf_nat_l4proto
*l4proto
;
331 struct net
*net
= nf_ct_net(ct
);
333 zone
= nf_ct_zone(ct
);
336 l3proto
= __nf_nat_l3proto_find(orig_tuple
->src
.l3num
);
337 l4proto
= __nf_nat_l4proto_find(orig_tuple
->src
.l3num
,
338 orig_tuple
->dst
.protonum
);
340 /* 1) If this srcip/proto/src-proto-part is currently mapped,
341 * and that same mapping gives a unique tuple within the given
344 * This is only required for source (ie. NAT/masq) mappings.
345 * So far, we don't do local source mappings, so multiple
346 * manips not an issue.
348 if (maniptype
== NF_NAT_MANIP_SRC
&&
349 !(range
->flags
& NF_NAT_RANGE_PROTO_RANDOM_ALL
)) {
350 /* try the original tuple first */
351 if (in_range(l3proto
, l4proto
, orig_tuple
, range
)) {
352 if (!nf_nat_used_tuple(orig_tuple
, ct
)) {
353 *tuple
= *orig_tuple
;
356 } else if (find_appropriate_src(net
, zone
, l3proto
, l4proto
,
357 orig_tuple
, tuple
, range
)) {
358 pr_debug("get_unique_tuple: Found current src map\n");
359 if (!nf_nat_used_tuple(tuple
, ct
))
364 /* 2) Select the least-used IP/proto combination in the given range */
365 *tuple
= *orig_tuple
;
366 find_best_ips_proto(zone
, tuple
, range
, ct
, maniptype
);
368 /* 3) The per-protocol part of the manip is made to map into
369 * the range to make a unique tuple.
372 /* Only bother mapping if it's not already in range and unique */
373 if (!(range
->flags
& NF_NAT_RANGE_PROTO_RANDOM_ALL
)) {
374 if (range
->flags
& NF_NAT_RANGE_PROTO_SPECIFIED
) {
375 if (l4proto
->in_range(tuple
, maniptype
,
377 &range
->max_proto
) &&
378 (range
->min_proto
.all
== range
->max_proto
.all
||
379 !nf_nat_used_tuple(tuple
, ct
)))
381 } else if (!nf_nat_used_tuple(tuple
, ct
)) {
386 /* Last change: get protocol to try to obtain unique tuple. */
387 l4proto
->unique_tuple(l3proto
, tuple
, range
, maniptype
, ct
);
392 struct nf_conn_nat
*nf_ct_nat_ext_add(struct nf_conn
*ct
)
394 struct nf_conn_nat
*nat
= nfct_nat(ct
);
398 if (!nf_ct_is_confirmed(ct
))
399 nat
= nf_ct_ext_add(ct
, NF_CT_EXT_NAT
, GFP_ATOMIC
);
403 EXPORT_SYMBOL_GPL(nf_ct_nat_ext_add
);
406 nf_nat_setup_info(struct nf_conn
*ct
,
407 const struct nf_nat_range
*range
,
408 enum nf_nat_manip_type maniptype
)
410 struct nf_conntrack_tuple curr_tuple
, new_tuple
;
412 NF_CT_ASSERT(maniptype
== NF_NAT_MANIP_SRC
||
413 maniptype
== NF_NAT_MANIP_DST
);
414 BUG_ON(nf_nat_initialized(ct
, maniptype
));
416 /* What we've got will look like inverse of reply. Normally
417 * this is what is in the conntrack, except for prior
418 * manipulations (future optimization: if num_manips == 0,
419 * orig_tp = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
421 nf_ct_invert_tuplepr(&curr_tuple
,
422 &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
424 get_unique_tuple(&new_tuple
, &curr_tuple
, range
, ct
, maniptype
);
426 if (!nf_ct_tuple_equal(&new_tuple
, &curr_tuple
)) {
427 struct nf_conntrack_tuple reply
;
429 /* Alter conntrack table so will recognize replies. */
430 nf_ct_invert_tuplepr(&reply
, &new_tuple
);
431 nf_conntrack_alter_reply(ct
, &reply
);
433 /* Non-atomic: we own this at the moment. */
434 if (maniptype
== NF_NAT_MANIP_SRC
)
435 ct
->status
|= IPS_SRC_NAT
;
437 ct
->status
|= IPS_DST_NAT
;
440 if (!nfct_seqadj_ext_add(ct
))
444 if (maniptype
== NF_NAT_MANIP_SRC
) {
445 struct nf_nat_conn_key key
= {
446 .net
= nf_ct_net(ct
),
447 .tuple
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
448 .zone
= nf_ct_zone(ct
),
452 err
= rhltable_insert_key(&nf_nat_bysource_table
,
455 nf_nat_bysource_params
);
461 if (maniptype
== NF_NAT_MANIP_DST
)
462 ct
->status
|= IPS_DST_NAT_DONE
;
464 ct
->status
|= IPS_SRC_NAT_DONE
;
468 EXPORT_SYMBOL(nf_nat_setup_info
);
471 __nf_nat_alloc_null_binding(struct nf_conn
*ct
, enum nf_nat_manip_type manip
)
473 /* Force range to this IP; let proto decide mapping for
474 * per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
475 * Use reply in case it's already been mangled (eg local packet).
477 union nf_inet_addr ip
=
478 (manip
== NF_NAT_MANIP_SRC
?
479 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
:
480 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
);
481 struct nf_nat_range range
= {
482 .flags
= NF_NAT_RANGE_MAP_IPS
,
486 return nf_nat_setup_info(ct
, &range
, manip
);
490 nf_nat_alloc_null_binding(struct nf_conn
*ct
, unsigned int hooknum
)
492 return __nf_nat_alloc_null_binding(ct
, HOOK2MANIP(hooknum
));
494 EXPORT_SYMBOL_GPL(nf_nat_alloc_null_binding
);
496 /* Do packet manipulations according to nf_nat_setup_info. */
497 unsigned int nf_nat_packet(struct nf_conn
*ct
,
498 enum ip_conntrack_info ctinfo
,
499 unsigned int hooknum
,
502 const struct nf_nat_l3proto
*l3proto
;
503 const struct nf_nat_l4proto
*l4proto
;
504 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
505 unsigned long statusbit
;
506 enum nf_nat_manip_type mtype
= HOOK2MANIP(hooknum
);
508 if (mtype
== NF_NAT_MANIP_SRC
)
509 statusbit
= IPS_SRC_NAT
;
511 statusbit
= IPS_DST_NAT
;
513 /* Invert if this is reply dir. */
514 if (dir
== IP_CT_DIR_REPLY
)
515 statusbit
^= IPS_NAT_MASK
;
517 /* Non-atomic: these bits don't change. */
518 if (ct
->status
& statusbit
) {
519 struct nf_conntrack_tuple target
;
521 /* We are aiming to look like inverse of other direction. */
522 nf_ct_invert_tuplepr(&target
, &ct
->tuplehash
[!dir
].tuple
);
524 l3proto
= __nf_nat_l3proto_find(target
.src
.l3num
);
525 l4proto
= __nf_nat_l4proto_find(target
.src
.l3num
,
526 target
.dst
.protonum
);
527 if (!l3proto
->manip_pkt(skb
, 0, l4proto
, &target
, mtype
))
532 EXPORT_SYMBOL_GPL(nf_nat_packet
);
534 struct nf_nat_proto_clean
{
539 /* kill conntracks with affected NAT section */
540 static int nf_nat_proto_remove(struct nf_conn
*i
, void *data
)
542 const struct nf_nat_proto_clean
*clean
= data
;
544 if ((clean
->l3proto
&& nf_ct_l3num(i
) != clean
->l3proto
) ||
545 (clean
->l4proto
&& nf_ct_protonum(i
) != clean
->l4proto
))
548 return i
->status
& IPS_NAT_MASK
? 1 : 0;
551 static int nf_nat_proto_clean(struct nf_conn
*ct
, void *data
)
553 if (nf_nat_proto_remove(ct
, data
))
556 if ((ct
->status
& IPS_SRC_NAT_DONE
) == 0)
559 /* This netns is being destroyed, and conntrack has nat null binding.
560 * Remove it from bysource hash, as the table will be freed soon.
562 * Else, when the conntrack is destoyed, nf_nat_cleanup_conntrack()
563 * will delete entry from already-freed table.
565 ct
->status
&= ~IPS_NAT_DONE_MASK
;
566 rhltable_remove(&nf_nat_bysource_table
, &ct
->nat_bysource
,
567 nf_nat_bysource_params
);
569 /* don't delete conntrack. Although that would make things a lot
570 * simpler, we'd end up flushing all conntracks on nat rmmod.
575 static void nf_nat_l4proto_clean(u8 l3proto
, u8 l4proto
)
577 struct nf_nat_proto_clean clean
= {
585 nf_ct_iterate_cleanup(net
, nf_nat_proto_remove
, &clean
, 0, 0);
589 static void nf_nat_l3proto_clean(u8 l3proto
)
591 struct nf_nat_proto_clean clean
= {
599 nf_ct_iterate_cleanup(net
, nf_nat_proto_remove
, &clean
, 0, 0);
603 /* Protocol registration. */
604 int nf_nat_l4proto_register(u8 l3proto
, const struct nf_nat_l4proto
*l4proto
)
606 const struct nf_nat_l4proto
**l4protos
;
610 mutex_lock(&nf_nat_proto_mutex
);
611 if (nf_nat_l4protos
[l3proto
] == NULL
) {
612 l4protos
= kmalloc(IPPROTO_MAX
* sizeof(struct nf_nat_l4proto
*),
614 if (l4protos
== NULL
) {
619 for (i
= 0; i
< IPPROTO_MAX
; i
++)
620 RCU_INIT_POINTER(l4protos
[i
], &nf_nat_l4proto_unknown
);
622 /* Before making proto_array visible to lockless readers,
623 * we must make sure its content is committed to memory.
627 nf_nat_l4protos
[l3proto
] = l4protos
;
630 if (rcu_dereference_protected(
631 nf_nat_l4protos
[l3proto
][l4proto
->l4proto
],
632 lockdep_is_held(&nf_nat_proto_mutex
)
633 ) != &nf_nat_l4proto_unknown
) {
637 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
][l4proto
->l4proto
], l4proto
);
639 mutex_unlock(&nf_nat_proto_mutex
);
642 EXPORT_SYMBOL_GPL(nf_nat_l4proto_register
);
644 /* No one stores the protocol anywhere; simply delete it. */
645 void nf_nat_l4proto_unregister(u8 l3proto
, const struct nf_nat_l4proto
*l4proto
)
647 mutex_lock(&nf_nat_proto_mutex
);
648 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
][l4proto
->l4proto
],
649 &nf_nat_l4proto_unknown
);
650 mutex_unlock(&nf_nat_proto_mutex
);
653 nf_nat_l4proto_clean(l3proto
, l4proto
->l4proto
);
655 EXPORT_SYMBOL_GPL(nf_nat_l4proto_unregister
);
657 int nf_nat_l3proto_register(const struct nf_nat_l3proto
*l3proto
)
661 err
= nf_ct_l3proto_try_module_get(l3proto
->l3proto
);
665 mutex_lock(&nf_nat_proto_mutex
);
666 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
->l3proto
][IPPROTO_TCP
],
667 &nf_nat_l4proto_tcp
);
668 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
->l3proto
][IPPROTO_UDP
],
669 &nf_nat_l4proto_udp
);
670 #ifdef CONFIG_NF_NAT_PROTO_DCCP
671 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
->l3proto
][IPPROTO_DCCP
],
672 &nf_nat_l4proto_dccp
);
674 #ifdef CONFIG_NF_NAT_PROTO_SCTP
675 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
->l3proto
][IPPROTO_SCTP
],
676 &nf_nat_l4proto_sctp
);
678 #ifdef CONFIG_NF_NAT_PROTO_UDPLITE
679 RCU_INIT_POINTER(nf_nat_l4protos
[l3proto
->l3proto
][IPPROTO_UDPLITE
],
680 &nf_nat_l4proto_udplite
);
682 mutex_unlock(&nf_nat_proto_mutex
);
684 RCU_INIT_POINTER(nf_nat_l3protos
[l3proto
->l3proto
], l3proto
);
687 EXPORT_SYMBOL_GPL(nf_nat_l3proto_register
);
689 void nf_nat_l3proto_unregister(const struct nf_nat_l3proto
*l3proto
)
691 mutex_lock(&nf_nat_proto_mutex
);
692 RCU_INIT_POINTER(nf_nat_l3protos
[l3proto
->l3proto
], NULL
);
693 mutex_unlock(&nf_nat_proto_mutex
);
696 nf_nat_l3proto_clean(l3proto
->l3proto
);
697 nf_ct_l3proto_module_put(l3proto
->l3proto
);
699 EXPORT_SYMBOL_GPL(nf_nat_l3proto_unregister
);
701 /* No one using conntrack by the time this called. */
702 static void nf_nat_cleanup_conntrack(struct nf_conn
*ct
)
704 if (ct
->status
& IPS_SRC_NAT_DONE
)
705 rhltable_remove(&nf_nat_bysource_table
, &ct
->nat_bysource
,
706 nf_nat_bysource_params
);
709 static struct nf_ct_ext_type nat_extend __read_mostly
= {
710 .len
= sizeof(struct nf_conn_nat
),
711 .align
= __alignof__(struct nf_conn_nat
),
712 .destroy
= nf_nat_cleanup_conntrack
,
716 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
718 #include <linux/netfilter/nfnetlink.h>
719 #include <linux/netfilter/nfnetlink_conntrack.h>
721 static const struct nla_policy protonat_nla_policy
[CTA_PROTONAT_MAX
+1] = {
722 [CTA_PROTONAT_PORT_MIN
] = { .type
= NLA_U16
},
723 [CTA_PROTONAT_PORT_MAX
] = { .type
= NLA_U16
},
726 static int nfnetlink_parse_nat_proto(struct nlattr
*attr
,
727 const struct nf_conn
*ct
,
728 struct nf_nat_range
*range
)
730 struct nlattr
*tb
[CTA_PROTONAT_MAX
+1];
731 const struct nf_nat_l4proto
*l4proto
;
734 err
= nla_parse_nested(tb
, CTA_PROTONAT_MAX
, attr
,
735 protonat_nla_policy
, NULL
);
739 l4proto
= __nf_nat_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
740 if (l4proto
->nlattr_to_range
)
741 err
= l4proto
->nlattr_to_range(tb
, range
);
746 static const struct nla_policy nat_nla_policy
[CTA_NAT_MAX
+1] = {
747 [CTA_NAT_V4_MINIP
] = { .type
= NLA_U32
},
748 [CTA_NAT_V4_MAXIP
] = { .type
= NLA_U32
},
749 [CTA_NAT_V6_MINIP
] = { .len
= sizeof(struct in6_addr
) },
750 [CTA_NAT_V6_MAXIP
] = { .len
= sizeof(struct in6_addr
) },
751 [CTA_NAT_PROTO
] = { .type
= NLA_NESTED
},
755 nfnetlink_parse_nat(const struct nlattr
*nat
,
756 const struct nf_conn
*ct
, struct nf_nat_range
*range
,
757 const struct nf_nat_l3proto
*l3proto
)
759 struct nlattr
*tb
[CTA_NAT_MAX
+1];
762 memset(range
, 0, sizeof(*range
));
764 err
= nla_parse_nested(tb
, CTA_NAT_MAX
, nat
, nat_nla_policy
, NULL
);
768 err
= l3proto
->nlattr_to_range(tb
, range
);
772 if (!tb
[CTA_NAT_PROTO
])
775 return nfnetlink_parse_nat_proto(tb
[CTA_NAT_PROTO
], ct
, range
);
778 /* This function is called under rcu_read_lock() */
780 nfnetlink_parse_nat_setup(struct nf_conn
*ct
,
781 enum nf_nat_manip_type manip
,
782 const struct nlattr
*attr
)
784 struct nf_nat_range range
;
785 const struct nf_nat_l3proto
*l3proto
;
788 /* Should not happen, restricted to creating new conntracks
791 if (WARN_ON_ONCE(nf_nat_initialized(ct
, manip
)))
794 /* Make sure that L3 NAT is there by when we call nf_nat_setup_info to
795 * attach the null binding, otherwise this may oops.
797 l3proto
= __nf_nat_l3proto_find(nf_ct_l3num(ct
));
801 /* No NAT information has been passed, allocate the null-binding */
803 return __nf_nat_alloc_null_binding(ct
, manip
) == NF_DROP
? -ENOMEM
: 0;
805 err
= nfnetlink_parse_nat(attr
, ct
, &range
, l3proto
);
809 return nf_nat_setup_info(ct
, &range
, manip
) == NF_DROP
? -ENOMEM
: 0;
813 nfnetlink_parse_nat_setup(struct nf_conn
*ct
,
814 enum nf_nat_manip_type manip
,
815 const struct nlattr
*attr
)
821 static void __net_exit
nf_nat_net_exit(struct net
*net
)
823 struct nf_nat_proto_clean clean
= {};
825 nf_ct_iterate_cleanup(net
, nf_nat_proto_clean
, &clean
, 0, 0);
828 static struct pernet_operations nf_nat_net_ops
= {
829 .exit
= nf_nat_net_exit
,
832 static struct nf_ct_helper_expectfn follow_master_nat
= {
833 .name
= "nat-follow-master",
834 .expectfn
= nf_nat_follow_master
,
837 static int __init
nf_nat_init(void)
841 ret
= rhltable_init(&nf_nat_bysource_table
, &nf_nat_bysource_params
);
845 ret
= nf_ct_extend_register(&nat_extend
);
847 rhltable_destroy(&nf_nat_bysource_table
);
848 printk(KERN_ERR
"nf_nat_core: Unable to register extension\n");
852 ret
= register_pernet_subsys(&nf_nat_net_ops
);
856 nf_ct_helper_expectfn_register(&follow_master_nat
);
858 BUG_ON(nfnetlink_parse_nat_setup_hook
!= NULL
);
859 RCU_INIT_POINTER(nfnetlink_parse_nat_setup_hook
,
860 nfnetlink_parse_nat_setup
);
862 BUG_ON(nf_nat_decode_session_hook
!= NULL
);
863 RCU_INIT_POINTER(nf_nat_decode_session_hook
, __nf_nat_decode_session
);
868 rhltable_destroy(&nf_nat_bysource_table
);
869 nf_ct_extend_unregister(&nat_extend
);
873 static void __exit
nf_nat_cleanup(void)
877 unregister_pernet_subsys(&nf_nat_net_ops
);
878 nf_ct_extend_unregister(&nat_extend
);
879 nf_ct_helper_expectfn_unregister(&follow_master_nat
);
880 RCU_INIT_POINTER(nfnetlink_parse_nat_setup_hook
, NULL
);
882 RCU_INIT_POINTER(nf_nat_decode_session_hook
, NULL
);
886 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
887 kfree(nf_nat_l4protos
[i
]);
889 rhltable_destroy(&nf_nat_bysource_table
);
892 MODULE_LICENSE("GPL");
894 module_init(nf_nat_init
);
895 module_exit(nf_nat_cleanup
);