[SCTP]: Fix sctp_primitive_ABORT() call in sctp_close().
[linux-2.6/verdex.git] / net / ipv4 / netfilter / ip_nat_standalone.c
blob17de077a790169166533da0a12056012b668f77d
1 /* This file contains all the functions required for the standalone
2 ip_nat module.
4 These are not required by the compatibility layer.
5 */
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
19 * */
21 #include <linux/types.h>
22 #include <linux/icmp.h>
23 #include <linux/ip.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>
29 #include <net/ip.h>
30 #include <net/checksum.h>
31 #include <linux/spinlock.h>
33 #define ASSERT_READ_LOCK(x)
34 #define ASSERT_WRITE_LOCK(x)
36 #include <linux/netfilter_ipv4/ip_nat.h>
37 #include <linux/netfilter_ipv4/ip_nat_rule.h>
38 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
39 #include <linux/netfilter_ipv4/ip_nat_core.h>
40 #include <linux/netfilter_ipv4/ip_nat_helper.h>
41 #include <linux/netfilter_ipv4/ip_tables.h>
42 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
43 #include <linux/netfilter_ipv4/listhelp.h>
45 #if 0
46 #define DEBUGP printk
47 #else
48 #define DEBUGP(format, args...)
49 #endif
51 #define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING" \
52 : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
53 : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT" \
54 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN" \
55 : "*ERROR*")))
57 #ifdef CONFIG_XFRM
58 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
60 struct ip_conntrack *ct;
61 struct ip_conntrack_tuple *t;
62 enum ip_conntrack_info ctinfo;
63 enum ip_conntrack_dir dir;
64 unsigned long statusbit;
66 ct = ip_conntrack_get(skb, &ctinfo);
67 if (ct == NULL)
68 return;
69 dir = CTINFO2DIR(ctinfo);
70 t = &ct->tuplehash[dir].tuple;
72 if (dir == IP_CT_DIR_ORIGINAL)
73 statusbit = IPS_DST_NAT;
74 else
75 statusbit = IPS_SRC_NAT;
77 if (ct->status & statusbit) {
78 fl->fl4_dst = t->dst.ip;
79 if (t->dst.protonum == IPPROTO_TCP ||
80 t->dst.protonum == IPPROTO_UDP)
81 fl->fl_ip_dport = t->dst.u.tcp.port;
84 statusbit ^= IPS_NAT_MASK;
86 if (ct->status & statusbit) {
87 fl->fl4_src = t->src.ip;
88 if (t->dst.protonum == IPPROTO_TCP ||
89 t->dst.protonum == IPPROTO_UDP)
90 fl->fl_ip_sport = t->src.u.tcp.port;
93 #endif
95 static unsigned int
96 ip_nat_fn(unsigned int hooknum,
97 struct sk_buff **pskb,
98 const struct net_device *in,
99 const struct net_device *out,
100 int (*okfn)(struct sk_buff *))
102 struct ip_conntrack *ct;
103 enum ip_conntrack_info ctinfo;
104 struct ip_nat_info *info;
105 /* maniptype == SRC for postrouting. */
106 enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
108 /* We never see fragments: conntrack defrags on pre-routing
109 and local-out, and ip_nat_out protects post-routing. */
110 IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
111 & htons(IP_MF|IP_OFFSET)));
113 /* If we had a hardware checksum before, it's now invalid */
114 if ((*pskb)->ip_summed == CHECKSUM_HW)
115 if (skb_checksum_help(*pskb, (out == NULL)))
116 return NF_DROP;
118 ct = ip_conntrack_get(*pskb, &ctinfo);
119 /* Can't track? It's not due to stress, or conntrack would
120 have dropped it. Hence it's the user's responsibilty to
121 packet filter it out, or implement conntrack/NAT for that
122 protocol. 8) --RR */
123 if (!ct) {
124 /* Exception: ICMP redirect to new connection (not in
125 hash table yet). We must not let this through, in
126 case we're doing NAT to the same network. */
127 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
128 struct icmphdr _hdr, *hp;
130 hp = skb_header_pointer(*pskb,
131 (*pskb)->nh.iph->ihl*4,
132 sizeof(_hdr), &_hdr);
133 if (hp != NULL &&
134 hp->type == ICMP_REDIRECT)
135 return NF_DROP;
137 return NF_ACCEPT;
140 /* Don't try to NAT if this packet is not conntracked */
141 if (ct == &ip_conntrack_untracked)
142 return NF_ACCEPT;
144 switch (ctinfo) {
145 case IP_CT_RELATED:
146 case IP_CT_RELATED+IP_CT_IS_REPLY:
147 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
148 if (!ip_nat_icmp_reply_translation(pskb, ct, maniptype,
149 CTINFO2DIR(ctinfo)))
150 return NF_DROP;
151 else
152 return NF_ACCEPT;
154 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
155 case IP_CT_NEW:
156 info = &ct->nat.info;
158 /* Seen it before? This can happen for loopback, retrans,
159 or local packets.. */
160 if (!ip_nat_initialized(ct, maniptype)) {
161 unsigned int ret;
163 if (unlikely(is_confirmed(ct)))
164 /* NAT module was loaded late */
165 ret = alloc_null_binding_confirmed(ct, info,
166 hooknum);
167 else if (hooknum == NF_IP_LOCAL_IN)
168 /* LOCAL_IN hook doesn't have a chain! */
169 ret = alloc_null_binding(ct, info, hooknum);
170 else
171 ret = ip_nat_rule_find(pskb, hooknum,
172 in, out, ct,
173 info);
175 if (ret != NF_ACCEPT) {
176 return ret;
178 } else
179 DEBUGP("Already setup manip %s for ct %p\n",
180 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
181 ct);
182 break;
184 default:
185 /* ESTABLISHED */
186 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
187 || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
188 info = &ct->nat.info;
191 IP_NF_ASSERT(info);
192 return ip_nat_packet(ct, ctinfo, hooknum, pskb);
195 static unsigned int
196 ip_nat_in(unsigned int hooknum,
197 struct sk_buff **pskb,
198 const struct net_device *in,
199 const struct net_device *out,
200 int (*okfn)(struct sk_buff *))
202 unsigned int ret;
203 u_int32_t daddr = (*pskb)->nh.iph->daddr;
205 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
206 if (ret != NF_DROP && ret != NF_STOLEN
207 && daddr != (*pskb)->nh.iph->daddr) {
208 dst_release((*pskb)->dst);
209 (*pskb)->dst = NULL;
211 return ret;
214 static unsigned int
215 ip_nat_out(unsigned int hooknum,
216 struct sk_buff **pskb,
217 const struct net_device *in,
218 const struct net_device *out,
219 int (*okfn)(struct sk_buff *))
221 #ifdef CONFIG_XFRM
222 struct ip_conntrack *ct;
223 enum ip_conntrack_info ctinfo;
224 #endif
225 unsigned int ret;
227 /* root is playing with raw sockets. */
228 if ((*pskb)->len < sizeof(struct iphdr)
229 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
230 return NF_ACCEPT;
232 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
233 #ifdef CONFIG_XFRM
234 if (ret != NF_DROP && ret != NF_STOLEN
235 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
236 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
238 if (ct->tuplehash[dir].tuple.src.ip !=
239 ct->tuplehash[!dir].tuple.dst.ip
240 || ct->tuplehash[dir].tuple.src.u.all !=
241 ct->tuplehash[!dir].tuple.dst.u.all
243 return ip_xfrm_me_harder(pskb) == 0 ? ret : NF_DROP;
245 #endif
246 return ret;
249 static unsigned int
250 ip_nat_local_fn(unsigned int hooknum,
251 struct sk_buff **pskb,
252 const struct net_device *in,
253 const struct net_device *out,
254 int (*okfn)(struct sk_buff *))
256 struct ip_conntrack *ct;
257 enum ip_conntrack_info ctinfo;
258 unsigned int ret;
260 /* root is playing with raw sockets. */
261 if ((*pskb)->len < sizeof(struct iphdr)
262 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
263 return NF_ACCEPT;
265 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
266 if (ret != NF_DROP && ret != NF_STOLEN
267 && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
268 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
270 if (ct->tuplehash[dir].tuple.dst.ip !=
271 ct->tuplehash[!dir].tuple.src.ip
272 #ifdef CONFIG_XFRM
273 || ct->tuplehash[dir].tuple.dst.u.all !=
274 ct->tuplehash[!dir].tuple.src.u.all
275 #endif
277 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
279 return ret;
282 static unsigned int
283 ip_nat_adjust(unsigned int hooknum,
284 struct sk_buff **pskb,
285 const struct net_device *in,
286 const struct net_device *out,
287 int (*okfn)(struct sk_buff *))
289 struct ip_conntrack *ct;
290 enum ip_conntrack_info ctinfo;
292 ct = ip_conntrack_get(*pskb, &ctinfo);
293 if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
294 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
295 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
296 return NF_DROP;
298 return NF_ACCEPT;
301 /* We must be after connection tracking and before packet filtering. */
303 static struct nf_hook_ops ip_nat_ops[] = {
304 /* Before packet filtering, change destination */
306 .hook = ip_nat_in,
307 .owner = THIS_MODULE,
308 .pf = PF_INET,
309 .hooknum = NF_IP_PRE_ROUTING,
310 .priority = NF_IP_PRI_NAT_DST,
312 /* After packet filtering, change source */
314 .hook = ip_nat_out,
315 .owner = THIS_MODULE,
316 .pf = PF_INET,
317 .hooknum = NF_IP_POST_ROUTING,
318 .priority = NF_IP_PRI_NAT_SRC,
320 /* After conntrack, adjust sequence number */
322 .hook = ip_nat_adjust,
323 .owner = THIS_MODULE,
324 .pf = PF_INET,
325 .hooknum = NF_IP_POST_ROUTING,
326 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
328 /* Before packet filtering, change destination */
330 .hook = ip_nat_local_fn,
331 .owner = THIS_MODULE,
332 .pf = PF_INET,
333 .hooknum = NF_IP_LOCAL_OUT,
334 .priority = NF_IP_PRI_NAT_DST,
336 /* After packet filtering, change source */
338 .hook = ip_nat_fn,
339 .owner = THIS_MODULE,
340 .pf = PF_INET,
341 .hooknum = NF_IP_LOCAL_IN,
342 .priority = NF_IP_PRI_NAT_SRC,
344 /* After conntrack, adjust sequence number */
346 .hook = ip_nat_adjust,
347 .owner = THIS_MODULE,
348 .pf = PF_INET,
349 .hooknum = NF_IP_LOCAL_IN,
350 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
354 static int __init ip_nat_standalone_init(void)
356 int ret = 0;
358 need_conntrack();
360 #ifdef CONFIG_XFRM
361 BUG_ON(ip_nat_decode_session != NULL);
362 ip_nat_decode_session = nat_decode_session;
363 #endif
364 ret = ip_nat_rule_init();
365 if (ret < 0) {
366 printk("ip_nat_init: can't setup rules.\n");
367 goto cleanup_decode_session;
369 ret = nf_register_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
370 if (ret < 0) {
371 printk("ip_nat_init: can't register hooks.\n");
372 goto cleanup_rule_init;
374 return ret;
376 cleanup_rule_init:
377 ip_nat_rule_cleanup();
378 cleanup_decode_session:
379 #ifdef CONFIG_XFRM
380 ip_nat_decode_session = NULL;
381 synchronize_net();
382 #endif
383 return ret;
386 static void __exit ip_nat_standalone_fini(void)
388 nf_unregister_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
389 ip_nat_rule_cleanup();
390 #ifdef CONFIG_XFRM
391 ip_nat_decode_session = NULL;
392 synchronize_net();
393 #endif
396 module_init(ip_nat_standalone_init);
397 module_exit(ip_nat_standalone_fini);
399 MODULE_LICENSE("GPL");