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/config.h>
22 #include <linux/types.h>
23 #include <linux/icmp.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/module.h>
28 #include <linux/skbuff.h>
29 #include <linux/proc_fs.h>
31 #include <net/checksum.h>
32 #include <linux/spinlock.h>
34 #define ASSERT_READ_LOCK(x)
35 #define ASSERT_WRITE_LOCK(x)
37 #include <linux/netfilter_ipv4/ip_nat.h>
38 #include <linux/netfilter_ipv4/ip_nat_rule.h>
39 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_core.h>
41 #include <linux/netfilter_ipv4/ip_nat_helper.h>
42 #include <linux/netfilter_ipv4/ip_tables.h>
43 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
44 #include <linux/netfilter_ipv4/listhelp.h>
49 #define DEBUGP(format, args...)
52 #define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING" \
53 : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
54 : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT" \
55 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN" \
59 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
61 struct ip_conntrack *ct;
62 struct ip_conntrack_tuple *t;
63 enum ip_conntrack_info ctinfo;
64 enum ip_conntrack_dir dir;
65 unsigned long statusbit;
67 ct = ip_conntrack_get(skb, &ctinfo);
70 dir = CTINFO2DIR(ctinfo);
71 t = &ct->tuplehash[dir].tuple;
73 if (dir == IP_CT_DIR_ORIGINAL)
74 statusbit = IPS_DST_NAT;
76 statusbit = IPS_SRC_NAT;
78 if (ct->status & statusbit) {
79 fl->fl4_dst = t->dst.ip;
80 if (t->dst.protonum == IPPROTO_TCP ||
81 t->dst.protonum == IPPROTO_UDP)
82 fl->fl_ip_dport = t->dst.u.tcp.port;
85 statusbit ^= IPS_NAT_MASK;
87 if (ct->status & statusbit) {
88 fl->fl4_src = t->src.ip;
89 if (t->dst.protonum == IPPROTO_TCP ||
90 t->dst.protonum == IPPROTO_UDP)
91 fl->fl_ip_sport = t->src.u.tcp.port;
97 ip_nat_fn(unsigned int hooknum,
98 struct sk_buff **pskb,
99 const struct net_device *in,
100 const struct net_device *out,
101 int (*okfn)(struct sk_buff *))
103 struct ip_conntrack *ct;
104 enum ip_conntrack_info ctinfo;
105 struct ip_nat_info *info;
106 /* maniptype == SRC for postrouting. */
107 enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
109 /* We never see fragments: conntrack defrags on pre-routing
110 and local-out, and ip_nat_out protects post-routing. */
111 IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
112 & htons(IP_MF|IP_OFFSET)));
114 /* If we had a hardware checksum before, it's now invalid */
115 if ((*pskb)->ip_summed == CHECKSUM_HW)
116 if (skb_checksum_help(*pskb, (out == NULL)))
119 ct = ip_conntrack_get(*pskb, &ctinfo);
120 /* Can't track? It's not due to stress, or conntrack would
121 have dropped it. Hence it's the user's responsibilty to
122 packet filter it out, or implement conntrack/NAT for that
125 /* Exception: ICMP redirect to new connection (not in
126 hash table yet). We must not let this through, in
127 case we're doing NAT to the same network. */
128 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
129 struct icmphdr _hdr, *hp;
131 hp = skb_header_pointer(*pskb,
132 (*pskb)->nh.iph->ihl*4,
133 sizeof(_hdr), &_hdr);
135 hp->type == ICMP_REDIRECT)
141 /* Don't try to NAT if this packet is not conntracked */
142 if (ct == &ip_conntrack_untracked)
147 case IP_CT_RELATED+IP_CT_IS_REPLY:
148 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
149 if (!ip_nat_icmp_reply_translation(pskb, ct, maniptype,
155 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
157 info = &ct->nat.info;
159 /* Seen it before? This can happen for loopback, retrans,
160 or local packets.. */
161 if (!ip_nat_initialized(ct, maniptype)) {
164 if (unlikely(is_confirmed(ct)))
165 /* NAT module was loaded late */
166 ret = alloc_null_binding_confirmed(ct, info,
168 else if (hooknum == NF_IP_LOCAL_IN)
169 /* LOCAL_IN hook doesn't have a chain! */
170 ret = alloc_null_binding(ct, info, hooknum);
172 ret = ip_nat_rule_find(pskb, hooknum,
176 if (ret != NF_ACCEPT) {
180 DEBUGP("Already setup manip %s for ct %p\n",
181 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
187 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
188 || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
189 info = &ct->nat.info;
193 return ip_nat_packet(ct, ctinfo, hooknum, pskb);
197 ip_nat_in(unsigned int hooknum,
198 struct sk_buff **pskb,
199 const struct net_device *in,
200 const struct net_device *out,
201 int (*okfn)(struct sk_buff *))
203 struct ip_conntrack *ct;
204 enum ip_conntrack_info ctinfo;
207 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
208 if (ret != NF_DROP && ret != NF_STOLEN
209 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
210 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
212 if (ct->tuplehash[dir].tuple.dst.ip !=
213 ct->tuplehash[!dir].tuple.src.ip) {
214 dst_release((*pskb)->dst);
222 ip_nat_out(unsigned int hooknum,
223 struct sk_buff **pskb,
224 const struct net_device *in,
225 const struct net_device *out,
226 int (*okfn)(struct sk_buff *))
228 struct ip_conntrack *ct;
229 enum ip_conntrack_info ctinfo;
232 /* root is playing with raw sockets. */
233 if ((*pskb)->len < sizeof(struct iphdr)
234 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
237 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
238 if (ret != NF_DROP && ret != NF_STOLEN
239 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
240 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
242 if (ct->tuplehash[dir].tuple.src.ip !=
243 ct->tuplehash[!dir].tuple.dst.ip
245 || ct->tuplehash[dir].tuple.src.u.all !=
246 ct->tuplehash[!dir].tuple.dst.u.all
249 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
255 ip_nat_local_fn(unsigned int hooknum,
256 struct sk_buff **pskb,
257 const struct net_device *in,
258 const struct net_device *out,
259 int (*okfn)(struct sk_buff *))
261 struct ip_conntrack *ct;
262 enum ip_conntrack_info ctinfo;
265 /* root is playing with raw sockets. */
266 if ((*pskb)->len < sizeof(struct iphdr)
267 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
270 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
271 if (ret != NF_DROP && ret != NF_STOLEN
272 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
273 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
275 if (ct->tuplehash[dir].tuple.dst.ip !=
276 ct->tuplehash[!dir].tuple.src.ip
278 || ct->tuplehash[dir].tuple.dst.u.all !=
279 ct->tuplehash[dir].tuple.src.u.all
282 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
288 ip_nat_adjust(unsigned int hooknum,
289 struct sk_buff **pskb,
290 const struct net_device *in,
291 const struct net_device *out,
292 int (*okfn)(struct sk_buff *))
294 struct ip_conntrack *ct;
295 enum ip_conntrack_info ctinfo;
297 ct = ip_conntrack_get(*pskb, &ctinfo);
298 if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
299 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
300 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
306 /* We must be after connection tracking and before packet filtering. */
308 /* Before packet filtering, change destination */
309 static struct nf_hook_ops ip_nat_in_ops = {
311 .owner = THIS_MODULE,
313 .hooknum = NF_IP_PRE_ROUTING,
314 .priority = NF_IP_PRI_NAT_DST,
317 /* After packet filtering, change source */
318 static struct nf_hook_ops ip_nat_out_ops = {
320 .owner = THIS_MODULE,
322 .hooknum = NF_IP_POST_ROUTING,
323 .priority = NF_IP_PRI_NAT_SRC,
326 /* After conntrack, adjust sequence number */
327 static struct nf_hook_ops ip_nat_adjust_out_ops = {
328 .hook = ip_nat_adjust,
329 .owner = THIS_MODULE,
331 .hooknum = NF_IP_POST_ROUTING,
332 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
335 /* Before packet filtering, change destination */
336 static struct nf_hook_ops ip_nat_local_out_ops = {
337 .hook = ip_nat_local_fn,
338 .owner = THIS_MODULE,
340 .hooknum = NF_IP_LOCAL_OUT,
341 .priority = NF_IP_PRI_NAT_DST,
344 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
345 static struct nf_hook_ops ip_nat_local_in_ops = {
347 .owner = THIS_MODULE,
349 .hooknum = NF_IP_LOCAL_IN,
350 .priority = NF_IP_PRI_NAT_SRC,
353 /* After conntrack, adjust sequence number */
354 static struct nf_hook_ops ip_nat_adjust_in_ops = {
355 .hook = ip_nat_adjust,
356 .owner = THIS_MODULE,
358 .hooknum = NF_IP_LOCAL_IN,
359 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
363 static int init_or_cleanup(int init)
369 if (!init) goto cleanup;
372 BUG_ON(ip_nat_decode_session != NULL);
373 ip_nat_decode_session = nat_decode_session;
375 ret = ip_nat_rule_init();
377 printk("ip_nat_init: can't setup rules.\n");
378 goto cleanup_decode_session;
380 ret = nf_register_hook(&ip_nat_in_ops);
382 printk("ip_nat_init: can't register in hook.\n");
383 goto cleanup_rule_init;
385 ret = nf_register_hook(&ip_nat_out_ops);
387 printk("ip_nat_init: can't register out hook.\n");
390 ret = nf_register_hook(&ip_nat_adjust_in_ops);
392 printk("ip_nat_init: can't register adjust in hook.\n");
395 ret = nf_register_hook(&ip_nat_adjust_out_ops);
397 printk("ip_nat_init: can't register adjust out hook.\n");
398 goto cleanup_adjustin_ops;
400 ret = nf_register_hook(&ip_nat_local_out_ops);
402 printk("ip_nat_init: can't register local out hook.\n");
403 goto cleanup_adjustout_ops;;
405 ret = nf_register_hook(&ip_nat_local_in_ops);
407 printk("ip_nat_init: can't register local in hook.\n");
408 goto cleanup_localoutops;
413 nf_unregister_hook(&ip_nat_local_in_ops);
415 nf_unregister_hook(&ip_nat_local_out_ops);
416 cleanup_adjustout_ops:
417 nf_unregister_hook(&ip_nat_adjust_out_ops);
418 cleanup_adjustin_ops:
419 nf_unregister_hook(&ip_nat_adjust_in_ops);
421 nf_unregister_hook(&ip_nat_out_ops);
423 nf_unregister_hook(&ip_nat_in_ops);
425 ip_nat_rule_cleanup();
426 cleanup_decode_session:
428 ip_nat_decode_session = NULL;
434 static int __init init(void)
436 return init_or_cleanup(1);
439 static void __exit fini(void)
447 MODULE_LICENSE("GPL");