1 /* This file contains all the functions required for the standalone
4 These are not required by the compatibility layer.
7 /* (C) 1999-2001 Paul `Rusty' Russell
8 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 * 23 Apr 2001: Harald Welte <laforge@gnumonks.org>
17 * - new API and handling of conntrack/nat helpers
18 * - now capable of multiple expectations for one master
21 #include <linux/types.h>
22 #include <linux/icmp.h>
24 #include <linux/netfilter.h>
25 #include <linux/netfilter_ipv4.h>
26 #include <linux/module.h>
27 #include <linux/skbuff.h>
28 #include <linux/proc_fs.h>
30 #include <net/checksum.h>
31 #include <linux/spinlock.h>
33 #define ASSERT_READ_LOCK(x)
34 #define ASSERT_WRITE_LOCK(x)
36 #include <linux/netfilter_ipv4/ip_nat.h>
37 #include <linux/netfilter_ipv4/ip_nat_rule.h>
38 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
39 #include <linux/netfilter_ipv4/ip_nat_core.h>
40 #include <linux/netfilter_ipv4/ip_nat_helper.h>
41 #include <linux/netfilter_ipv4/ip_tables.h>
42 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
43 #include <linux/netfilter_ipv4/listhelp.h>
48 #define DEBUGP(format, args...)
51 #define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING" \
52 : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
53 : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT" \
54 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN" \
58 static void nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
)
60 struct ip_conntrack
*ct
;
61 struct ip_conntrack_tuple
*t
;
62 enum ip_conntrack_info ctinfo
;
63 enum ip_conntrack_dir dir
;
64 unsigned long statusbit
;
66 ct
= ip_conntrack_get(skb
, &ctinfo
);
69 dir
= CTINFO2DIR(ctinfo
);
70 t
= &ct
->tuplehash
[dir
].tuple
;
72 if (dir
== IP_CT_DIR_ORIGINAL
)
73 statusbit
= IPS_DST_NAT
;
75 statusbit
= IPS_SRC_NAT
;
77 if (ct
->status
& statusbit
) {
78 fl
->fl4_dst
= t
->dst
.ip
;
79 if (t
->dst
.protonum
== IPPROTO_TCP
||
80 t
->dst
.protonum
== IPPROTO_UDP
)
81 fl
->fl_ip_dport
= t
->dst
.u
.tcp
.port
;
84 statusbit
^= IPS_NAT_MASK
;
86 if (ct
->status
& statusbit
) {
87 fl
->fl4_src
= t
->src
.ip
;
88 if (t
->dst
.protonum
== IPPROTO_TCP
||
89 t
->dst
.protonum
== IPPROTO_UDP
)
90 fl
->fl_ip_sport
= t
->src
.u
.tcp
.port
;
96 ip_nat_fn(unsigned int hooknum
,
97 struct sk_buff
**pskb
,
98 const struct net_device
*in
,
99 const struct net_device
*out
,
100 int (*okfn
)(struct sk_buff
*))
102 struct ip_conntrack
*ct
;
103 enum ip_conntrack_info ctinfo
;
104 struct ip_nat_info
*info
;
105 /* maniptype == SRC for postrouting. */
106 enum ip_nat_manip_type maniptype
= HOOK2MANIP(hooknum
);
108 /* We never see fragments: conntrack defrags on pre-routing
109 and local-out, and ip_nat_out protects post-routing. */
110 IP_NF_ASSERT(!((*pskb
)->nh
.iph
->frag_off
111 & htons(IP_MF
|IP_OFFSET
)));
113 /* If we had a hardware checksum before, it's now invalid */
114 if ((*pskb
)->ip_summed
== CHECKSUM_HW
)
115 if (skb_checksum_help(*pskb
, (out
== NULL
)))
118 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
119 /* Can't track? It's not due to stress, or conntrack would
120 have dropped it. Hence it's the user's responsibilty to
121 packet filter it out, or implement conntrack/NAT for that
124 /* Exception: ICMP redirect to new connection (not in
125 hash table yet). We must not let this through, in
126 case we're doing NAT to the same network. */
127 if ((*pskb
)->nh
.iph
->protocol
== IPPROTO_ICMP
) {
128 struct icmphdr _hdr
, *hp
;
130 hp
= skb_header_pointer(*pskb
,
131 (*pskb
)->nh
.iph
->ihl
*4,
132 sizeof(_hdr
), &_hdr
);
134 hp
->type
== ICMP_REDIRECT
)
140 /* Don't try to NAT if this packet is not conntracked */
141 if (ct
== &ip_conntrack_untracked
)
146 case IP_CT_RELATED
+IP_CT_IS_REPLY
:
147 if ((*pskb
)->nh
.iph
->protocol
== IPPROTO_ICMP
) {
148 if (!ip_nat_icmp_reply_translation(pskb
, ct
, maniptype
,
154 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
156 info
= &ct
->nat
.info
;
158 /* Seen it before? This can happen for loopback, retrans,
159 or local packets.. */
160 if (!ip_nat_initialized(ct
, maniptype
)) {
163 if (unlikely(is_confirmed(ct
)))
164 /* NAT module was loaded late */
165 ret
= alloc_null_binding_confirmed(ct
, info
,
167 else if (hooknum
== NF_IP_LOCAL_IN
)
168 /* LOCAL_IN hook doesn't have a chain! */
169 ret
= alloc_null_binding(ct
, info
, hooknum
);
171 ret
= ip_nat_rule_find(pskb
, hooknum
,
175 if (ret
!= NF_ACCEPT
) {
179 DEBUGP("Already setup manip %s for ct %p\n",
180 maniptype
== IP_NAT_MANIP_SRC
? "SRC" : "DST",
186 IP_NF_ASSERT(ctinfo
== IP_CT_ESTABLISHED
187 || ctinfo
== (IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
));
188 info
= &ct
->nat
.info
;
192 return ip_nat_packet(ct
, ctinfo
, hooknum
, pskb
);
196 ip_nat_in(unsigned int hooknum
,
197 struct sk_buff
**pskb
,
198 const struct net_device
*in
,
199 const struct net_device
*out
,
200 int (*okfn
)(struct sk_buff
*))
203 u_int32_t daddr
= (*pskb
)->nh
.iph
->daddr
;
205 ret
= ip_nat_fn(hooknum
, pskb
, in
, out
, okfn
);
206 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
207 && daddr
!= (*pskb
)->nh
.iph
->daddr
) {
208 dst_release((*pskb
)->dst
);
215 ip_nat_out(unsigned int hooknum
,
216 struct sk_buff
**pskb
,
217 const struct net_device
*in
,
218 const struct net_device
*out
,
219 int (*okfn
)(struct sk_buff
*))
222 struct ip_conntrack
*ct
;
223 enum ip_conntrack_info ctinfo
;
227 /* root is playing with raw sockets. */
228 if ((*pskb
)->len
< sizeof(struct iphdr
)
229 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
))
232 ret
= ip_nat_fn(hooknum
, pskb
, in
, out
, okfn
);
234 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
235 && (ct
= ip_conntrack_get(*pskb
, &ctinfo
)) != NULL
) {
236 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
238 if (ct
->tuplehash
[dir
].tuple
.src
.ip
!=
239 ct
->tuplehash
[!dir
].tuple
.dst
.ip
240 || ct
->tuplehash
[dir
].tuple
.src
.u
.all
!=
241 ct
->tuplehash
[!dir
].tuple
.dst
.u
.all
243 return ip_xfrm_me_harder(pskb
) == 0 ? ret
: NF_DROP
;
250 ip_nat_local_fn(unsigned int hooknum
,
251 struct sk_buff
**pskb
,
252 const struct net_device
*in
,
253 const struct net_device
*out
,
254 int (*okfn
)(struct sk_buff
*))
256 struct ip_conntrack
*ct
;
257 enum ip_conntrack_info ctinfo
;
260 /* root is playing with raw sockets. */
261 if ((*pskb
)->len
< sizeof(struct iphdr
)
262 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
))
265 ret
= ip_nat_fn(hooknum
, pskb
, in
, out
, okfn
);
266 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
267 && (ct
= ip_conntrack_get(*pskb
, &ctinfo
)) != NULL
) {
268 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
270 if (ct
->tuplehash
[dir
].tuple
.dst
.ip
!=
271 ct
->tuplehash
[!dir
].tuple
.src
.ip
273 || ct
->tuplehash
[dir
].tuple
.dst
.u
.all
!=
274 ct
->tuplehash
[!dir
].tuple
.src
.u
.all
277 return ip_route_me_harder(pskb
) == 0 ? ret
: NF_DROP
;
283 ip_nat_adjust(unsigned int hooknum
,
284 struct sk_buff
**pskb
,
285 const struct net_device
*in
,
286 const struct net_device
*out
,
287 int (*okfn
)(struct sk_buff
*))
289 struct ip_conntrack
*ct
;
290 enum ip_conntrack_info ctinfo
;
292 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
293 if (ct
&& test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
)) {
294 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
295 if (!ip_nat_seq_adjust(pskb
, ct
, ctinfo
))
301 /* We must be after connection tracking and before packet filtering. */
303 static struct nf_hook_ops ip_nat_ops
[] = {
304 /* Before packet filtering, change destination */
307 .owner
= THIS_MODULE
,
309 .hooknum
= NF_IP_PRE_ROUTING
,
310 .priority
= NF_IP_PRI_NAT_DST
,
312 /* After packet filtering, change source */
315 .owner
= THIS_MODULE
,
317 .hooknum
= NF_IP_POST_ROUTING
,
318 .priority
= NF_IP_PRI_NAT_SRC
,
320 /* After conntrack, adjust sequence number */
322 .hook
= ip_nat_adjust
,
323 .owner
= THIS_MODULE
,
325 .hooknum
= NF_IP_POST_ROUTING
,
326 .priority
= NF_IP_PRI_NAT_SEQ_ADJUST
,
328 /* Before packet filtering, change destination */
330 .hook
= ip_nat_local_fn
,
331 .owner
= THIS_MODULE
,
333 .hooknum
= NF_IP_LOCAL_OUT
,
334 .priority
= NF_IP_PRI_NAT_DST
,
336 /* After packet filtering, change source */
339 .owner
= THIS_MODULE
,
341 .hooknum
= NF_IP_LOCAL_IN
,
342 .priority
= NF_IP_PRI_NAT_SRC
,
344 /* After conntrack, adjust sequence number */
346 .hook
= ip_nat_adjust
,
347 .owner
= THIS_MODULE
,
349 .hooknum
= NF_IP_LOCAL_IN
,
350 .priority
= NF_IP_PRI_NAT_SEQ_ADJUST
,
354 static int __init
ip_nat_standalone_init(void)
361 BUG_ON(ip_nat_decode_session
!= NULL
);
362 ip_nat_decode_session
= nat_decode_session
;
364 ret
= ip_nat_rule_init();
366 printk("ip_nat_init: can't setup rules.\n");
367 goto cleanup_decode_session
;
369 ret
= nf_register_hooks(ip_nat_ops
, ARRAY_SIZE(ip_nat_ops
));
371 printk("ip_nat_init: can't register hooks.\n");
372 goto cleanup_rule_init
;
377 ip_nat_rule_cleanup();
378 cleanup_decode_session
:
380 ip_nat_decode_session
= NULL
;
386 static void __exit
ip_nat_standalone_fini(void)
388 nf_unregister_hooks(ip_nat_ops
, ARRAY_SIZE(ip_nat_ops
));
389 ip_nat_rule_cleanup();
391 ip_nat_decode_session
= NULL
;
396 module_init(ip_nat_standalone_init
);
397 module_exit(ip_nat_standalone_fini
);
399 MODULE_LICENSE("GPL");