allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv4 / netfilter / nf_nat_standalone.c
blobf00aad483d398c3c0ae6d1dc4acece8f78351c49
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8 #include <linux/types.h>
9 #include <linux/icmp.h>
10 #include <linux/ip.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter_ipv4.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <net/ip.h>
17 #include <net/checksum.h>
18 #include <linux/spinlock.h>
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_core.h>
22 #include <net/netfilter/nf_nat.h>
23 #include <net/netfilter/nf_nat_rule.h>
24 #include <net/netfilter/nf_nat_protocol.h>
25 #include <net/netfilter/nf_nat_core.h>
26 #include <net/netfilter/nf_nat_helper.h>
27 #include <linux/netfilter_ipv4/ip_tables.h>
28 #include <linux/netfilter_ipv4/ipt_cone.h>
30 #if 0
31 #define DEBUGP printk
32 #else
33 #define DEBUGP(format, args...)
34 #endif
36 #ifdef CONFIG_XFRM
37 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
39 struct nf_conn *ct;
40 struct nf_conntrack_tuple *t;
41 enum ip_conntrack_info ctinfo;
42 enum ip_conntrack_dir dir;
43 unsigned long statusbit;
45 ct = nf_ct_get(skb, &ctinfo);
46 if (ct == NULL)
47 return;
48 dir = CTINFO2DIR(ctinfo);
49 t = &ct->tuplehash[dir].tuple;
51 if (dir == IP_CT_DIR_ORIGINAL)
52 statusbit = IPS_DST_NAT;
53 else
54 statusbit = IPS_SRC_NAT;
56 if (ct->status & statusbit) {
57 fl->fl4_dst = t->dst.u3.ip;
58 if (t->dst.protonum == IPPROTO_TCP ||
59 t->dst.protonum == IPPROTO_UDP)
60 fl->fl_ip_dport = t->dst.u.tcp.port;
63 statusbit ^= IPS_NAT_MASK;
65 if (ct->status & statusbit) {
66 fl->fl4_src = t->src.u3.ip;
67 if (t->dst.protonum == IPPROTO_TCP ||
68 t->dst.protonum == IPPROTO_UDP)
69 fl->fl_ip_sport = t->src.u.tcp.port;
72 #endif
74 static unsigned int
75 nf_nat_fn(unsigned int hooknum,
76 struct sk_buff *skb,
77 const struct net_device *in,
78 const struct net_device *out,
79 int (*okfn)(struct sk_buff *))
81 struct nf_conn *ct;
82 enum ip_conntrack_info ctinfo;
83 struct nf_conn_nat *nat;
84 /* maniptype == SRC for postrouting. */
85 enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum);
87 /* We never see fragments: conntrack defrags on pre-routing
88 and local-out, and nf_nat_out protects post-routing. */
89 NF_CT_ASSERT(!(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)));
91 ct = nf_ct_get(skb, &ctinfo);
92 /* Can't track? It's not due to stress, or conntrack would
93 have dropped it. Hence it's the user's responsibilty to
94 packet filter it out, or implement conntrack/NAT for that
95 protocol. 8) --RR */
96 if (!ct) {
97 /* Exception: ICMP redirect to new connection (not in
98 hash table yet). We must not let this through, in
99 case we're doing NAT to the same network. */
100 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
101 struct icmphdr _hdr, *hp;
103 hp = skb_header_pointer(skb, ip_hdrlen(skb),
104 sizeof(_hdr), &_hdr);
105 if (hp != NULL &&
106 hp->type == ICMP_REDIRECT)
107 return NF_DROP;
109 return NF_ACCEPT;
112 /* Don't try to NAT if this packet is not conntracked */
113 if (ct == &nf_conntrack_untracked)
114 return NF_ACCEPT;
116 nat = nfct_nat(ct);
117 if (!nat)
118 return NF_ACCEPT;
120 switch (ctinfo) {
121 case IP_CT_RELATED:
122 case IP_CT_RELATED+IP_CT_IS_REPLY:
123 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
124 if (!nf_nat_icmp_reply_translation(ct, ctinfo,
125 hooknum, skb))
126 return NF_DROP;
127 else
128 return NF_ACCEPT;
130 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
131 case IP_CT_NEW:
133 /* Seen it before? This can happen for loopback, retrans,
134 or local packets.. */
135 if (!nf_nat_initialized(ct, maniptype)) {
136 unsigned int ret;
138 if (unlikely(nf_ct_is_confirmed(ct)))
139 /* NAT module was loaded late */
140 ret = alloc_null_binding_confirmed(ct, hooknum);
141 else if (hooknum == NF_IP_LOCAL_IN)
142 /* LOCAL_IN hook doesn't have a chain! */
143 ret = alloc_null_binding(ct, hooknum);
144 else
145 ret = nf_nat_rule_find(skb, hooknum, in, out,
146 ct);
148 if (ret != NF_ACCEPT) {
149 return ret;
152 ipt_cone_place_in_hashes(ct);
154 } else
155 DEBUGP("Already setup manip %s for ct %p\n",
156 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
157 ct);
158 break;
160 default:
161 /* ESTABLISHED */
162 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
163 ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
166 return nf_nat_packet(ct, ctinfo, hooknum, skb);
169 static unsigned int
170 nf_nat_in(unsigned int hooknum,
171 struct sk_buff *skb,
172 const struct net_device *in,
173 const struct net_device *out,
174 int (*okfn)(struct sk_buff *))
176 unsigned int ret;
177 __be32 daddr = ip_hdr(skb)->daddr;
179 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
180 if (ret != NF_DROP && ret != NF_STOLEN &&
181 daddr != ip_hdr(skb)->daddr) {
182 dst_release(skb->dst);
183 skb->dst = NULL;
185 return ret;
188 static unsigned int
189 nf_nat_out(unsigned int hooknum,
190 struct sk_buff *skb,
191 const struct net_device *in,
192 const struct net_device *out,
193 int (*okfn)(struct sk_buff *))
195 #ifdef CONFIG_XFRM
196 struct nf_conn *ct;
197 enum ip_conntrack_info ctinfo;
198 #endif
199 unsigned int ret;
201 /* root is playing with raw sockets. */
202 if (skb->len < sizeof(struct iphdr) ||
203 ip_hdrlen(skb) < sizeof(struct iphdr))
204 return NF_ACCEPT;
206 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
207 #ifdef CONFIG_XFRM
208 if (ret != NF_DROP && ret != NF_STOLEN &&
209 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
210 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
212 if (ct->tuplehash[dir].tuple.src.u3.ip !=
213 ct->tuplehash[!dir].tuple.dst.u3.ip
214 || ct->tuplehash[dir].tuple.src.u.all !=
215 ct->tuplehash[!dir].tuple.dst.u.all
217 return ip_xfrm_me_harder(skb) == 0 ? ret : NF_DROP;
219 #endif
220 return ret;
223 static unsigned int
224 nf_nat_local_fn(unsigned int hooknum,
225 struct sk_buff *skb,
226 const struct net_device *in,
227 const struct net_device *out,
228 int (*okfn)(struct sk_buff *))
230 struct nf_conn *ct;
231 enum ip_conntrack_info ctinfo;
232 unsigned int ret;
234 /* root is playing with raw sockets. */
235 if (skb->len < sizeof(struct iphdr) ||
236 ip_hdrlen(skb) < sizeof(struct iphdr))
237 return NF_ACCEPT;
239 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
240 if (ret != NF_DROP && ret != NF_STOLEN &&
241 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
242 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
244 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
245 ct->tuplehash[!dir].tuple.src.u3.ip) {
246 if (ip_route_me_harder(skb, RTN_UNSPEC))
247 ret = NF_DROP;
249 #ifdef CONFIG_XFRM
250 else if (ct->tuplehash[dir].tuple.dst.u.all !=
251 ct->tuplehash[!dir].tuple.src.u.all)
252 if (ip_xfrm_me_harder(skb))
253 ret = NF_DROP;
254 #endif
256 return ret;
259 static unsigned int
260 nf_nat_adjust(unsigned int hooknum,
261 struct sk_buff *skb,
262 const struct net_device *in,
263 const struct net_device *out,
264 int (*okfn)(struct sk_buff *))
266 struct nf_conn *ct;
267 enum ip_conntrack_info ctinfo;
269 ct = nf_ct_get(skb, &ctinfo);
270 if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
271 DEBUGP("nf_nat_standalone: adjusting sequence number\n");
272 if (!nf_nat_seq_adjust(skb, ct, ctinfo))
273 return NF_DROP;
275 return NF_ACCEPT;
278 /* We must be after connection tracking and before packet filtering. */
280 static struct nf_hook_ops nf_nat_ops[] = {
281 /* Before packet filtering, change destination */
283 .hook = nf_nat_in,
284 .owner = THIS_MODULE,
285 .pf = PF_INET,
286 .hooknum = NF_IP_PRE_ROUTING,
287 .priority = NF_IP_PRI_NAT_DST,
289 /* After packet filtering, change source */
291 .hook = nf_nat_out,
292 .owner = THIS_MODULE,
293 .pf = PF_INET,
294 .hooknum = NF_IP_POST_ROUTING,
295 .priority = NF_IP_PRI_NAT_SRC,
297 /* After conntrack, adjust sequence number */
299 .hook = nf_nat_adjust,
300 .owner = THIS_MODULE,
301 .pf = PF_INET,
302 .hooknum = NF_IP_POST_ROUTING,
303 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
305 /* Before packet filtering, change destination */
307 .hook = nf_nat_local_fn,
308 .owner = THIS_MODULE,
309 .pf = PF_INET,
310 .hooknum = NF_IP_LOCAL_OUT,
311 .priority = NF_IP_PRI_NAT_DST,
313 /* After packet filtering, change source */
315 .hook = nf_nat_fn,
316 .owner = THIS_MODULE,
317 .pf = PF_INET,
318 .hooknum = NF_IP_LOCAL_IN,
319 .priority = NF_IP_PRI_NAT_SRC,
321 /* After conntrack, adjust sequence number */
323 .hook = nf_nat_adjust,
324 .owner = THIS_MODULE,
325 .pf = PF_INET,
326 .hooknum = NF_IP_LOCAL_IN,
327 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
331 static int __init nf_nat_standalone_init(void)
333 int size, ret = 0;
335 need_conntrack();
337 size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_nat)) +
338 sizeof(struct nf_conn_nat);
339 ret = nf_conntrack_register_cache(NF_CT_F_NAT, "nf_nat:base", size);
340 if (ret < 0) {
341 printk(KERN_ERR "nf_nat_init: Unable to create slab cache\n");
342 return ret;
345 size = ALIGN(size, __alignof__(struct nf_conn_help)) +
346 sizeof(struct nf_conn_help);
347 ret = nf_conntrack_register_cache(NF_CT_F_NAT|NF_CT_F_HELP,
348 "nf_nat:help", size);
349 if (ret < 0) {
350 printk(KERN_ERR "nf_nat_init: Unable to create slab cache\n");
351 goto cleanup_register_cache;
353 #ifdef CONFIG_XFRM
354 BUG_ON(ip_nat_decode_session != NULL);
355 ip_nat_decode_session = nat_decode_session;
356 #endif
357 ret = nf_nat_rule_init();
358 if (ret < 0) {
359 printk("nf_nat_init: can't setup rules.\n");
360 goto cleanup_decode_session;
362 ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
363 if (ret < 0) {
364 printk("nf_nat_init: can't register hooks.\n");
365 goto cleanup_rule_init;
367 nf_nat_module_is_loaded = 1;
368 return ret;
370 cleanup_rule_init:
371 nf_nat_rule_cleanup();
372 cleanup_decode_session:
373 #ifdef CONFIG_XFRM
374 ip_nat_decode_session = NULL;
375 synchronize_net();
376 #endif
377 nf_conntrack_unregister_cache(NF_CT_F_NAT|NF_CT_F_HELP);
378 cleanup_register_cache:
379 nf_conntrack_unregister_cache(NF_CT_F_NAT);
380 return ret;
383 static void __exit nf_nat_standalone_fini(void)
385 nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
386 nf_nat_rule_cleanup();
387 nf_nat_module_is_loaded = 0;
388 #ifdef CONFIG_XFRM
389 ip_nat_decode_session = NULL;
390 synchronize_net();
391 #endif
392 /* Conntrack caches are unregistered in nf_conntrack_cleanup */
395 module_init(nf_nat_standalone_init);
396 module_exit(nf_nat_standalone_fini);
398 MODULE_LICENSE("GPL");
399 MODULE_ALIAS("ip_nat");