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) MUST_BE_READ_LOCKED(&ip_nat_lock)
35 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
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" \
58 static inline int call_expect(struct ip_conntrack *master,
59 struct sk_buff **pskb,
61 struct ip_conntrack *ct,
62 struct ip_nat_info *info)
64 return master->nat.info.helper->expect(pskb, hooknum, ct, info);
68 ip_nat_fn(unsigned int hooknum,
69 struct sk_buff **pskb,
70 const struct net_device *in,
71 const struct net_device *out,
72 int (*okfn)(struct sk_buff *))
74 struct ip_conntrack *ct;
75 enum ip_conntrack_info ctinfo;
76 struct ip_nat_info *info;
77 /* maniptype == SRC for postrouting. */
78 enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
80 /* We never see fragments: conntrack defrags on pre-routing
81 and local-out, and ip_nat_out protects post-routing. */
82 IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
83 & htons(IP_MF|IP_OFFSET)));
85 (*pskb)->nfcache |= NFC_UNKNOWN;
87 /* If we had a hardware checksum before, it's now invalid */
88 if ((*pskb)->ip_summed == CHECKSUM_HW)
89 if (skb_checksum_help(pskb, (out == NULL)))
92 ct = ip_conntrack_get(*pskb, &ctinfo);
93 /* Can't track? It's not due to stress, or conntrack would
94 have dropped it. Hence it's the user's responsibilty to
95 packet filter it out, or implement conntrack/NAT for that
98 /* Exception: ICMP redirect to new connection (not in
99 hash table yet). We must not let this through, in
100 case we're doing NAT to the same network. */
101 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
104 if (skb_copy_bits(*pskb, (*pskb)->nh.iph->ihl*4,
105 &hdr, sizeof(hdr)) == 0
106 && hdr.type == ICMP_REDIRECT)
114 case IP_CT_RELATED+IP_CT_IS_REPLY:
115 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
116 if (!icmp_reply_translation(pskb, ct, hooknum,
122 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
124 info = &ct->nat.info;
126 WRITE_LOCK(&ip_nat_lock);
127 /* Seen it before? This can happen for loopback, retrans,
128 or local packets.. */
129 if (!(info->initialized & (1 << maniptype))
130 #ifndef CONFIG_IP_NF_NAT_LOCAL
131 /* If this session has already been confirmed we must not
132 * touch it again even if there is no mapping set up.
133 * Can only happen on local->local traffic with
134 * CONFIG_IP_NF_NAT_LOCAL disabled.
136 && !(ct->status & IPS_CONFIRMED)
142 && master_ct(ct)->nat.info.helper
143 && master_ct(ct)->nat.info.helper->expect) {
144 ret = call_expect(master_ct(ct), pskb,
147 #ifdef CONFIG_IP_NF_NAT_LOCAL
148 /* LOCAL_IN hook doesn't have a chain! */
149 if (hooknum == NF_IP_LOCAL_IN)
150 ret = alloc_null_binding(ct, info,
154 ret = ip_nat_rule_find(pskb, hooknum, in, out,
158 if (ret != NF_ACCEPT) {
159 WRITE_UNLOCK(&ip_nat_lock);
163 DEBUGP("Already setup manip %s for ct %p\n",
164 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
166 WRITE_UNLOCK(&ip_nat_lock);
171 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
172 || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
173 info = &ct->nat.info;
177 return do_bindings(ct, ctinfo, info, hooknum, pskb);
181 ip_nat_out(unsigned int hooknum,
182 struct sk_buff **pskb,
183 const struct net_device *in,
184 const struct net_device *out,
185 int (*okfn)(struct sk_buff *))
187 /* root is playing with raw sockets. */
188 if ((*pskb)->len < sizeof(struct iphdr)
189 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
192 /* We can hit fragment here; forwarded packets get
193 defragmented by connection tracking coming in, then
194 fragmented (grr) by the forward code.
196 In future: If we have nfct != NULL, AND we have NAT
197 initialized, AND there is no helper, then we can do full
198 NAPT on the head, and IP-address-only NAT on the rest.
200 I'm starting to have nightmares about fragments. */
202 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
203 *pskb = ip_ct_gather_frags(*pskb);
209 return ip_nat_fn(hooknum, pskb, in, out, okfn);
212 #ifdef CONFIG_IP_NF_NAT_LOCAL
214 ip_nat_local_fn(unsigned int hooknum,
215 struct sk_buff **pskb,
216 const struct net_device *in,
217 const struct net_device *out,
218 int (*okfn)(struct sk_buff *))
220 u_int32_t saddr, daddr;
223 /* root is playing with raw sockets. */
224 if ((*pskb)->len < sizeof(struct iphdr)
225 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
228 saddr = (*pskb)->nh.iph->saddr;
229 daddr = (*pskb)->nh.iph->daddr;
231 ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
232 if (ret != NF_DROP && ret != NF_STOLEN
233 && ((*pskb)->nh.iph->saddr != saddr
234 || (*pskb)->nh.iph->daddr != daddr))
235 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
240 /* We must be after connection tracking and before packet filtering. */
242 /* Before packet filtering, change destination */
243 static struct nf_hook_ops ip_nat_in_ops = {
245 .owner = THIS_MODULE,
247 .hooknum = NF_IP_PRE_ROUTING,
248 .priority = NF_IP_PRI_NAT_DST,
251 /* After packet filtering, change source */
252 static struct nf_hook_ops ip_nat_out_ops = {
254 .owner = THIS_MODULE,
256 .hooknum = NF_IP_POST_ROUTING,
257 .priority = NF_IP_PRI_NAT_SRC,
260 #ifdef CONFIG_IP_NF_NAT_LOCAL
261 /* Before packet filtering, change destination */
262 static struct nf_hook_ops ip_nat_local_out_ops = {
263 .hook = ip_nat_local_fn,
264 .owner = THIS_MODULE,
266 .hooknum = NF_IP_LOCAL_OUT,
267 .priority = NF_IP_PRI_NAT_DST,
270 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
271 static struct nf_hook_ops ip_nat_local_in_ops = {
273 .owner = THIS_MODULE,
275 .hooknum = NF_IP_LOCAL_IN,
276 .priority = NF_IP_PRI_NAT_SRC,
280 /* Protocol registration. */
281 int ip_nat_protocol_register(struct ip_nat_protocol *proto)
286 WRITE_LOCK(&ip_nat_lock);
287 list_for_each(i, &protos) {
288 if (((struct ip_nat_protocol *)i)->protonum
289 == proto->protonum) {
295 list_prepend(&protos, proto);
297 WRITE_UNLOCK(&ip_nat_lock);
301 /* Noone stores the protocol anywhere; simply delete it. */
302 void ip_nat_protocol_unregister(struct ip_nat_protocol *proto)
304 WRITE_LOCK(&ip_nat_lock);
305 LIST_DELETE(&protos, proto);
306 WRITE_UNLOCK(&ip_nat_lock);
308 /* Someone could be still looking at the proto in a bh. */
312 static int init_or_cleanup(int init)
318 if (!init) goto cleanup;
320 ret = ip_nat_rule_init();
322 printk("ip_nat_init: can't setup rules.\n");
323 goto cleanup_nothing;
327 printk("ip_nat_init: can't setup rules.\n");
328 goto cleanup_rule_init;
330 ret = nf_register_hook(&ip_nat_in_ops);
332 printk("ip_nat_init: can't register in hook.\n");
335 ret = nf_register_hook(&ip_nat_out_ops);
337 printk("ip_nat_init: can't register out hook.\n");
340 #ifdef CONFIG_IP_NF_NAT_LOCAL
341 ret = nf_register_hook(&ip_nat_local_out_ops);
343 printk("ip_nat_init: can't register local out hook.\n");
346 ret = nf_register_hook(&ip_nat_local_in_ops);
348 printk("ip_nat_init: can't register local in hook.\n");
349 goto cleanup_localoutops;
355 #ifdef CONFIG_IP_NF_NAT_LOCAL
356 nf_unregister_hook(&ip_nat_local_in_ops);
358 nf_unregister_hook(&ip_nat_local_out_ops);
361 nf_unregister_hook(&ip_nat_out_ops);
363 nf_unregister_hook(&ip_nat_in_ops);
367 ip_nat_rule_cleanup();
369 MUST_BE_READ_WRITE_UNLOCKED(&ip_nat_lock);
373 static int __init init(void)
375 return init_or_cleanup(1);
378 static void __exit fini(void)
386 EXPORT_SYMBOL(ip_nat_setup_info);
387 EXPORT_SYMBOL(ip_nat_protocol_register);
388 EXPORT_SYMBOL(ip_nat_protocol_unregister);
389 EXPORT_SYMBOL(ip_nat_helper_register);
390 EXPORT_SYMBOL(ip_nat_helper_unregister);
391 EXPORT_SYMBOL(ip_nat_cheat_check);
392 EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
393 EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
394 EXPORT_SYMBOL(ip_nat_used_tuple);
395 MODULE_LICENSE("GPL");