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>
44 #define DEBUGP(format, args...)
47 #define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING" \
48 : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
49 : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT" \
50 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN" \
54 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
56 struct ip_conntrack *ct;
57 struct ip_conntrack_tuple *t;
58 enum ip_conntrack_info ctinfo;
59 enum ip_conntrack_dir dir;
60 unsigned long statusbit;
62 ct = ip_conntrack_get(skb, &ctinfo);
65 dir = CTINFO2DIR(ctinfo);
66 t = &ct->tuplehash[dir].tuple;
68 if (dir == IP_CT_DIR_ORIGINAL)
69 statusbit = IPS_DST_NAT;
71 statusbit = IPS_SRC_NAT;
73 if (ct->status & statusbit) {
74 fl->fl4_dst = t->dst.ip;
75 if (t->dst.protonum == IPPROTO_TCP ||
76 t->dst.protonum == IPPROTO_UDP)
77 fl->fl_ip_dport = t->dst.u.tcp.port;
80 statusbit ^= IPS_NAT_MASK;
82 if (ct->status & statusbit) {
83 fl->fl4_src = t->src.ip;
84 if (t->dst.protonum == IPPROTO_TCP ||
85 t->dst.protonum == IPPROTO_UDP)
86 fl->fl_ip_sport = t->src.u.tcp.port;
92 ip_nat_fn(unsigned int hooknum,
93 struct sk_buff **pskb,
94 const struct net_device *in,
95 const struct net_device *out,
96 int (*okfn)(struct sk_buff *))
98 struct ip_conntrack *ct;
99 enum ip_conntrack_info ctinfo;
100 struct ip_nat_info *info;
101 /* maniptype == SRC for postrouting. */
102 enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
104 /* We never see fragments: conntrack defrags on pre-routing
105 and local-out, and ip_nat_out protects post-routing. */
106 IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
107 & htons(IP_MF|IP_OFFSET)));
109 ct = ip_conntrack_get(*pskb, &ctinfo);
110 /* Can't track? It's not due to stress, or conntrack would
111 have dropped it. Hence it's the user's responsibilty to
112 packet filter it out, or implement conntrack/NAT for that
115 /* Exception: ICMP redirect to new connection (not in
116 hash table yet). We must not let this through, in
117 case we're doing NAT to the same network. */
118 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
119 struct icmphdr _hdr, *hp;
121 hp = skb_header_pointer(*pskb,
122 (*pskb)->nh.iph->ihl*4,
123 sizeof(_hdr), &_hdr);
125 hp->type == ICMP_REDIRECT)
131 /* Don't try to NAT if this packet is not conntracked */
132 if (ct == &ip_conntrack_untracked)
137 case IP_CT_RELATED+IP_CT_IS_REPLY:
138 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
139 if (!ip_nat_icmp_reply_translation(ct, ctinfo,
145 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
147 info = &ct->nat.info;
149 /* Seen it before? This can happen for loopback, retrans,
150 or local packets.. */
151 if (!ip_nat_initialized(ct, maniptype)) {
154 if (unlikely(is_confirmed(ct)))
155 /* NAT module was loaded late */
156 ret = alloc_null_binding_confirmed(ct, info,
158 else if (hooknum == NF_IP_LOCAL_IN)
159 /* LOCAL_IN hook doesn't have a chain! */
160 ret = alloc_null_binding(ct, info, hooknum);
162 ret = ip_nat_rule_find(pskb, hooknum,
166 if (ret != NF_ACCEPT) {
170 DEBUGP("Already setup manip %s for ct %p\n",
171 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
177 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
178 || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
179 info = &ct->nat.info;
183 return ip_nat_packet(ct, ctinfo, hooknum, pskb);
187 ip_nat_in(unsigned int hooknum,
188 struct sk_buff **pskb,
189 const struct net_device *in,
190 const struct net_device *out,
191 int (*okfn)(struct sk_buff *))
194 u_int32_t daddr = (*pskb)->nh.iph->daddr;
196 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
197 if (ret != NF_DROP && ret != NF_STOLEN
198 && daddr != (*pskb)->nh.iph->daddr) {
199 dst_release((*pskb)->dst);
206 ip_nat_out(unsigned int hooknum,
207 struct sk_buff **pskb,
208 const struct net_device *in,
209 const struct net_device *out,
210 int (*okfn)(struct sk_buff *))
213 struct ip_conntrack *ct;
214 enum ip_conntrack_info ctinfo;
218 /* root is playing with raw sockets. */
219 if ((*pskb)->len < sizeof(struct iphdr)
220 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
223 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
225 if (ret != NF_DROP && ret != NF_STOLEN
226 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
227 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
229 if (ct->tuplehash[dir].tuple.src.ip !=
230 ct->tuplehash[!dir].tuple.dst.ip
231 || ct->tuplehash[dir].tuple.src.u.all !=
232 ct->tuplehash[!dir].tuple.dst.u.all
234 return ip_xfrm_me_harder(pskb) == 0 ? ret : NF_DROP;
241 ip_nat_local_fn(unsigned int hooknum,
242 struct sk_buff **pskb,
243 const struct net_device *in,
244 const struct net_device *out,
245 int (*okfn)(struct sk_buff *))
247 struct ip_conntrack *ct;
248 enum ip_conntrack_info ctinfo;
251 /* root is playing with raw sockets. */
252 if ((*pskb)->len < sizeof(struct iphdr)
253 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
256 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
257 if (ret != NF_DROP && ret != NF_STOLEN
258 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
259 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
261 if (ct->tuplehash[dir].tuple.dst.ip !=
262 ct->tuplehash[!dir].tuple.src.ip
264 || ct->tuplehash[dir].tuple.dst.u.all !=
265 ct->tuplehash[!dir].tuple.src.u.all
268 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
274 ip_nat_adjust(unsigned int hooknum,
275 struct sk_buff **pskb,
276 const struct net_device *in,
277 const struct net_device *out,
278 int (*okfn)(struct sk_buff *))
280 struct ip_conntrack *ct;
281 enum ip_conntrack_info ctinfo;
283 ct = ip_conntrack_get(*pskb, &ctinfo);
284 if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
285 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
286 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
292 /* We must be after connection tracking and before packet filtering. */
294 static struct nf_hook_ops ip_nat_ops[] = {
295 /* Before packet filtering, change destination */
298 .owner = THIS_MODULE,
300 .hooknum = NF_IP_PRE_ROUTING,
301 .priority = NF_IP_PRI_NAT_DST,
303 /* After packet filtering, change source */
306 .owner = THIS_MODULE,
308 .hooknum = NF_IP_POST_ROUTING,
309 .priority = NF_IP_PRI_NAT_SRC,
311 /* After conntrack, adjust sequence number */
313 .hook = ip_nat_adjust,
314 .owner = THIS_MODULE,
316 .hooknum = NF_IP_POST_ROUTING,
317 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
319 /* Before packet filtering, change destination */
321 .hook = ip_nat_local_fn,
322 .owner = THIS_MODULE,
324 .hooknum = NF_IP_LOCAL_OUT,
325 .priority = NF_IP_PRI_NAT_DST,
327 /* After packet filtering, change source */
330 .owner = THIS_MODULE,
332 .hooknum = NF_IP_LOCAL_IN,
333 .priority = NF_IP_PRI_NAT_SRC,
335 /* After conntrack, adjust sequence number */
337 .hook = ip_nat_adjust,
338 .owner = THIS_MODULE,
340 .hooknum = NF_IP_LOCAL_IN,
341 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
345 static int __init ip_nat_standalone_init(void)
352 BUG_ON(ip_nat_decode_session != NULL);
353 ip_nat_decode_session = nat_decode_session;
355 ret = ip_nat_rule_init();
357 printk("ip_nat_init: can't setup rules.\n");
358 goto cleanup_decode_session;
360 ret = nf_register_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
362 printk("ip_nat_init: can't register hooks.\n");
363 goto cleanup_rule_init;
368 ip_nat_rule_cleanup();
369 cleanup_decode_session:
371 ip_nat_decode_session = NULL;
377 static void __exit ip_nat_standalone_fini(void)
379 nf_unregister_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
380 ip_nat_rule_cleanup();
382 ip_nat_decode_session = NULL;
387 module_init(ip_nat_standalone_init);
388 module_exit(ip_nat_standalone_fini);
390 MODULE_LICENSE("GPL");