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 ip_nat_fn(unsigned int hooknum,
60 struct sk_buff **pskb,
61 const struct net_device *in,
62 const struct net_device *out,
63 int (*okfn)(struct sk_buff *))
65 struct ip_conntrack *ct;
66 enum ip_conntrack_info ctinfo;
67 struct ip_nat_info *info;
68 /* maniptype == SRC for postrouting. */
69 enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
71 /* We never see fragments: conntrack defrags on pre-routing
72 and local-out, and ip_nat_out protects post-routing. */
73 IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
74 & htons(IP_MF|IP_OFFSET)));
76 /* If we had a hardware checksum before, it's now invalid */
77 if ((*pskb)->ip_summed == CHECKSUM_HW)
78 if (skb_checksum_help(*pskb, (out == NULL)))
81 ct = ip_conntrack_get(*pskb, &ctinfo);
82 /* Can't track? It's not due to stress, or conntrack would
83 have dropped it. Hence it's the user's responsibilty to
84 packet filter it out, or implement conntrack/NAT for that
87 /* Exception: ICMP redirect to new connection (not in
88 hash table yet). We must not let this through, in
89 case we're doing NAT to the same network. */
90 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
91 struct icmphdr _hdr, *hp;
93 hp = skb_header_pointer(*pskb,
94 (*pskb)->nh.iph->ihl*4,
97 hp->type == ICMP_REDIRECT)
103 /* Don't try to NAT if this packet is not conntracked */
104 if (ct == &ip_conntrack_untracked)
109 case IP_CT_RELATED+IP_CT_IS_REPLY:
110 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
111 if (!ip_nat_icmp_reply_translation(pskb, ct, maniptype,
117 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
119 info = &ct->nat.info;
121 /* Seen it before? This can happen for loopback, retrans,
122 or local packets.. */
123 if (!ip_nat_initialized(ct, maniptype)) {
126 if (unlikely(is_confirmed(ct)))
127 /* NAT module was loaded late */
128 ret = alloc_null_binding_confirmed(ct, info,
130 else if (hooknum == NF_IP_LOCAL_IN)
131 /* LOCAL_IN hook doesn't have a chain! */
132 ret = alloc_null_binding(ct, info, hooknum);
134 ret = ip_nat_rule_find(pskb, hooknum,
138 if (ret != NF_ACCEPT) {
142 DEBUGP("Already setup manip %s for ct %p\n",
143 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
149 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
150 || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
151 info = &ct->nat.info;
155 return ip_nat_packet(ct, ctinfo, hooknum, pskb);
159 ip_nat_in(unsigned int hooknum,
160 struct sk_buff **pskb,
161 const struct net_device *in,
162 const struct net_device *out,
163 int (*okfn)(struct sk_buff *))
165 struct ip_conntrack *ct;
166 enum ip_conntrack_info ctinfo;
169 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
170 if (ret != NF_DROP && ret != NF_STOLEN
171 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
172 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
174 if (ct->tuplehash[dir].tuple.src.ip !=
175 ct->tuplehash[!dir].tuple.dst.ip) {
176 dst_release((*pskb)->dst);
184 ip_nat_out(unsigned int hooknum,
185 struct sk_buff **pskb,
186 const struct net_device *in,
187 const struct net_device *out,
188 int (*okfn)(struct sk_buff *))
190 /* root is playing with raw sockets. */
191 if ((*pskb)->len < sizeof(struct iphdr)
192 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
195 return ip_nat_fn(hooknum, pskb, in, out, okfn);
199 ip_nat_local_fn(unsigned int hooknum,
200 struct sk_buff **pskb,
201 const struct net_device *in,
202 const struct net_device *out,
203 int (*okfn)(struct sk_buff *))
205 struct ip_conntrack *ct;
206 enum ip_conntrack_info ctinfo;
209 /* root is playing with raw sockets. */
210 if ((*pskb)->len < sizeof(struct iphdr)
211 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
214 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
215 if (ret != NF_DROP && ret != NF_STOLEN
216 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
217 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
219 if (ct->tuplehash[dir].tuple.dst.ip !=
220 ct->tuplehash[!dir].tuple.src.ip)
221 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
227 ip_nat_adjust(unsigned int hooknum,
228 struct sk_buff **pskb,
229 const struct net_device *in,
230 const struct net_device *out,
231 int (*okfn)(struct sk_buff *))
233 struct ip_conntrack *ct;
234 enum ip_conntrack_info ctinfo;
236 ct = ip_conntrack_get(*pskb, &ctinfo);
237 if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
238 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
239 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
245 /* We must be after connection tracking and before packet filtering. */
247 /* Before packet filtering, change destination */
248 static struct nf_hook_ops ip_nat_in_ops = {
250 .owner = THIS_MODULE,
252 .hooknum = NF_IP_PRE_ROUTING,
253 .priority = NF_IP_PRI_NAT_DST,
256 /* After packet filtering, change source */
257 static struct nf_hook_ops ip_nat_out_ops = {
259 .owner = THIS_MODULE,
261 .hooknum = NF_IP_POST_ROUTING,
262 .priority = NF_IP_PRI_NAT_SRC,
265 /* After conntrack, adjust sequence number */
266 static struct nf_hook_ops ip_nat_adjust_out_ops = {
267 .hook = ip_nat_adjust,
268 .owner = THIS_MODULE,
270 .hooknum = NF_IP_POST_ROUTING,
271 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
274 /* Before packet filtering, change destination */
275 static struct nf_hook_ops ip_nat_local_out_ops = {
276 .hook = ip_nat_local_fn,
277 .owner = THIS_MODULE,
279 .hooknum = NF_IP_LOCAL_OUT,
280 .priority = NF_IP_PRI_NAT_DST,
283 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
284 static struct nf_hook_ops ip_nat_local_in_ops = {
286 .owner = THIS_MODULE,
288 .hooknum = NF_IP_LOCAL_IN,
289 .priority = NF_IP_PRI_NAT_SRC,
292 /* After conntrack, adjust sequence number */
293 static struct nf_hook_ops ip_nat_adjust_in_ops = {
294 .hook = ip_nat_adjust,
295 .owner = THIS_MODULE,
297 .hooknum = NF_IP_LOCAL_IN,
298 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
302 static int init_or_cleanup(int init)
308 if (!init) goto cleanup;
310 ret = ip_nat_rule_init();
312 printk("ip_nat_init: can't setup rules.\n");
313 goto cleanup_nothing;
315 ret = nf_register_hook(&ip_nat_in_ops);
317 printk("ip_nat_init: can't register in hook.\n");
318 goto cleanup_rule_init;
320 ret = nf_register_hook(&ip_nat_out_ops);
322 printk("ip_nat_init: can't register out hook.\n");
325 ret = nf_register_hook(&ip_nat_adjust_in_ops);
327 printk("ip_nat_init: can't register adjust in hook.\n");
330 ret = nf_register_hook(&ip_nat_adjust_out_ops);
332 printk("ip_nat_init: can't register adjust out hook.\n");
333 goto cleanup_adjustin_ops;
335 ret = nf_register_hook(&ip_nat_local_out_ops);
337 printk("ip_nat_init: can't register local out hook.\n");
338 goto cleanup_adjustout_ops;;
340 ret = nf_register_hook(&ip_nat_local_in_ops);
342 printk("ip_nat_init: can't register local in hook.\n");
343 goto cleanup_localoutops;
348 nf_unregister_hook(&ip_nat_local_in_ops);
350 nf_unregister_hook(&ip_nat_local_out_ops);
351 cleanup_adjustout_ops:
352 nf_unregister_hook(&ip_nat_adjust_out_ops);
353 cleanup_adjustin_ops:
354 nf_unregister_hook(&ip_nat_adjust_in_ops);
356 nf_unregister_hook(&ip_nat_out_ops);
358 nf_unregister_hook(&ip_nat_in_ops);
360 ip_nat_rule_cleanup();
365 static int __init init(void)
367 return init_or_cleanup(1);
370 static void __exit fini(void)
378 MODULE_LICENSE("GPL");