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 #include <linux/netfilter_ipv4/ip_nat.h>
34 #include <linux/netfilter_ipv4/ip_nat_rule.h>
35 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
36 #include <linux/netfilter_ipv4/ip_nat_core.h>
37 #include <linux/netfilter_ipv4/ip_nat_helper.h>
38 #include <linux/netfilter_ipv4/ip_tables.h>
39 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
40 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
45 #define DEBUGP(format, args...)
48 #define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING" \
49 : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
50 : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT" \
51 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN" \
55 static void nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
)
57 struct ip_conntrack
*ct
;
58 struct ip_conntrack_tuple
*t
;
59 enum ip_conntrack_info ctinfo
;
60 enum ip_conntrack_dir dir
;
61 unsigned long statusbit
;
63 ct
= ip_conntrack_get(skb
, &ctinfo
);
66 dir
= CTINFO2DIR(ctinfo
);
67 t
= &ct
->tuplehash
[dir
].tuple
;
69 if (dir
== IP_CT_DIR_ORIGINAL
)
70 statusbit
= IPS_DST_NAT
;
72 statusbit
= IPS_SRC_NAT
;
74 if (ct
->status
& statusbit
) {
75 fl
->fl4_dst
= t
->dst
.ip
;
76 if (t
->dst
.protonum
== IPPROTO_TCP
||
77 t
->dst
.protonum
== IPPROTO_UDP
)
78 fl
->fl_ip_dport
= t
->dst
.u
.tcp
.port
;
81 statusbit
^= IPS_NAT_MASK
;
83 if (ct
->status
& statusbit
) {
84 fl
->fl4_src
= t
->src
.ip
;
85 if (t
->dst
.protonum
== IPPROTO_TCP
||
86 t
->dst
.protonum
== IPPROTO_UDP
)
87 fl
->fl_ip_sport
= t
->src
.u
.tcp
.port
;
93 ip_nat_fn(unsigned int hooknum
,
94 struct sk_buff
**pskb
,
95 const struct net_device
*in
,
96 const struct net_device
*out
,
97 int (*okfn
)(struct sk_buff
*))
99 struct ip_conntrack
*ct
;
100 enum ip_conntrack_info ctinfo
;
101 struct ip_nat_info
*info
;
102 /* maniptype == SRC for postrouting. */
103 enum ip_nat_manip_type maniptype
= HOOK2MANIP(hooknum
);
105 /* We never see fragments: conntrack defrags on pre-routing
106 and local-out, and ip_nat_out protects post-routing. */
107 IP_NF_ASSERT(!((*pskb
)->nh
.iph
->frag_off
108 & htons(IP_MF
|IP_OFFSET
)));
110 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
111 /* Can't track? It's not due to stress, or conntrack would
112 have dropped it. Hence it's the user's responsibilty to
113 packet filter it out, or implement conntrack/NAT for that
116 /* Exception: ICMP redirect to new connection (not in
117 hash table yet). We must not let this through, in
118 case we're doing NAT to the same network. */
119 if ((*pskb
)->nh
.iph
->protocol
== IPPROTO_ICMP
) {
120 struct icmphdr _hdr
, *hp
;
122 hp
= skb_header_pointer(*pskb
,
123 (*pskb
)->nh
.iph
->ihl
*4,
124 sizeof(_hdr
), &_hdr
);
126 hp
->type
== ICMP_REDIRECT
) {
127 if (LOG_INVALID(IPPROTO_ICMP
))
128 nf_log_packet(PF_INET
, 0, *pskb
,
129 NULL
, NULL
, NULL
, "ip_nat_fn: "
130 "untracked ICMP redirect ");
137 /* Don't try to NAT if this packet is not conntracked */
138 if (ct
== &ip_conntrack_untracked
)
143 case IP_CT_RELATED
+IP_CT_IS_REPLY
:
144 if ((*pskb
)->nh
.iph
->protocol
== IPPROTO_ICMP
) {
145 if (!ip_nat_icmp_reply_translation(ct
, ctinfo
,
151 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
153 info
= &ct
->nat
.info
;
155 /* Seen it before? This can happen for loopback, retrans,
156 or local packets.. */
157 if (!ip_nat_initialized(ct
, maniptype
)) {
160 if (unlikely(is_confirmed(ct
)))
161 /* NAT module was loaded late */
162 ret
= alloc_null_binding_confirmed(ct
, info
,
164 else if (hooknum
== NF_IP_LOCAL_IN
)
165 /* LOCAL_IN hook doesn't have a chain! */
166 ret
= alloc_null_binding(ct
, info
, hooknum
);
168 ret
= ip_nat_rule_find(pskb
, hooknum
,
172 if (ret
!= NF_ACCEPT
) {
176 DEBUGP("Already setup manip %s for ct %p\n",
177 maniptype
== IP_NAT_MANIP_SRC
? "SRC" : "DST",
183 IP_NF_ASSERT(ctinfo
== IP_CT_ESTABLISHED
184 || ctinfo
== (IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
));
185 info
= &ct
->nat
.info
;
189 return ip_nat_packet(ct
, ctinfo
, hooknum
, pskb
);
193 ip_nat_in(unsigned int hooknum
,
194 struct sk_buff
**pskb
,
195 const struct net_device
*in
,
196 const struct net_device
*out
,
197 int (*okfn
)(struct sk_buff
*))
200 __be32 daddr
= (*pskb
)->nh
.iph
->daddr
;
202 ret
= ip_nat_fn(hooknum
, pskb
, in
, out
, okfn
);
203 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
204 && daddr
!= (*pskb
)->nh
.iph
->daddr
) {
205 dst_release((*pskb
)->dst
);
212 ip_nat_out(unsigned int hooknum
,
213 struct sk_buff
**pskb
,
214 const struct net_device
*in
,
215 const struct net_device
*out
,
216 int (*okfn
)(struct sk_buff
*))
219 struct ip_conntrack
*ct
;
220 enum ip_conntrack_info ctinfo
;
224 /* root is playing with raw sockets. */
225 if ((*pskb
)->len
< sizeof(struct iphdr
)
226 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
))
229 ret
= ip_nat_fn(hooknum
, pskb
, in
, out
, okfn
);
231 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
232 && (ct
= ip_conntrack_get(*pskb
, &ctinfo
)) != NULL
) {
233 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
235 if (ct
->tuplehash
[dir
].tuple
.src
.ip
!=
236 ct
->tuplehash
[!dir
].tuple
.dst
.ip
237 || ct
->tuplehash
[dir
].tuple
.src
.u
.all
!=
238 ct
->tuplehash
[!dir
].tuple
.dst
.u
.all
240 return ip_xfrm_me_harder(pskb
) == 0 ? ret
: NF_DROP
;
247 ip_nat_local_fn(unsigned int hooknum
,
248 struct sk_buff
**pskb
,
249 const struct net_device
*in
,
250 const struct net_device
*out
,
251 int (*okfn
)(struct sk_buff
*))
253 struct ip_conntrack
*ct
;
254 enum ip_conntrack_info ctinfo
;
257 /* root is playing with raw sockets. */
258 if ((*pskb
)->len
< sizeof(struct iphdr
)
259 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
))
262 ret
= ip_nat_fn(hooknum
, pskb
, in
, out
, okfn
);
263 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
264 && (ct
= ip_conntrack_get(*pskb
, &ctinfo
)) != NULL
) {
265 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
267 if (ct
->tuplehash
[dir
].tuple
.dst
.ip
!=
268 ct
->tuplehash
[!dir
].tuple
.src
.ip
270 || ct
->tuplehash
[dir
].tuple
.dst
.u
.all
!=
271 ct
->tuplehash
[!dir
].tuple
.src
.u
.all
274 if (ip_route_me_harder(pskb
, RTN_UNSPEC
))
281 ip_nat_adjust(unsigned int hooknum
,
282 struct sk_buff
**pskb
,
283 const struct net_device
*in
,
284 const struct net_device
*out
,
285 int (*okfn
)(struct sk_buff
*))
287 struct ip_conntrack
*ct
;
288 enum ip_conntrack_info ctinfo
;
290 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
291 if (ct
&& test_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
)) {
292 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
293 if (!ip_nat_seq_adjust(pskb
, ct
, ctinfo
))
299 /* We must be after connection tracking and before packet filtering. */
301 static struct nf_hook_ops ip_nat_ops
[] = {
302 /* Before packet filtering, change destination */
305 .owner
= THIS_MODULE
,
307 .hooknum
= NF_IP_PRE_ROUTING
,
308 .priority
= NF_IP_PRI_NAT_DST
,
310 /* After packet filtering, change source */
313 .owner
= THIS_MODULE
,
315 .hooknum
= NF_IP_POST_ROUTING
,
316 .priority
= NF_IP_PRI_NAT_SRC
,
318 /* After conntrack, adjust sequence number */
320 .hook
= ip_nat_adjust
,
321 .owner
= THIS_MODULE
,
323 .hooknum
= NF_IP_POST_ROUTING
,
324 .priority
= NF_IP_PRI_NAT_SEQ_ADJUST
,
326 /* Before packet filtering, change destination */
328 .hook
= ip_nat_local_fn
,
329 .owner
= THIS_MODULE
,
331 .hooknum
= NF_IP_LOCAL_OUT
,
332 .priority
= NF_IP_PRI_NAT_DST
,
334 /* After packet filtering, change source */
337 .owner
= THIS_MODULE
,
339 .hooknum
= NF_IP_LOCAL_IN
,
340 .priority
= NF_IP_PRI_NAT_SRC
,
342 /* After conntrack, adjust sequence number */
344 .hook
= ip_nat_adjust
,
345 .owner
= THIS_MODULE
,
347 .hooknum
= NF_IP_LOCAL_IN
,
348 .priority
= NF_IP_PRI_NAT_SEQ_ADJUST
,
352 static int __init
ip_nat_standalone_init(void)
359 BUG_ON(ip_nat_decode_session
!= NULL
);
360 ip_nat_decode_session
= nat_decode_session
;
362 ret
= ip_nat_rule_init();
364 printk("ip_nat_init: can't setup rules.\n");
365 goto cleanup_decode_session
;
367 ret
= nf_register_hooks(ip_nat_ops
, ARRAY_SIZE(ip_nat_ops
));
369 printk("ip_nat_init: can't register hooks.\n");
370 goto cleanup_rule_init
;
375 ip_nat_rule_cleanup();
376 cleanup_decode_session
:
378 ip_nat_decode_session
= NULL
;
384 static void __exit
ip_nat_standalone_fini(void)
386 nf_unregister_hooks(ip_nat_ops
, ARRAY_SIZE(ip_nat_ops
));
387 ip_nat_rule_cleanup();
389 ip_nat_decode_session
= NULL
;
394 module_init(ip_nat_standalone_init
);
395 module_exit(ip_nat_standalone_fini
);
397 MODULE_LICENSE("GPL");