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.
8 #include <linux/types.h>
9 #include <linux/icmp.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>
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_conntrack_extend.h>
23 #include <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_rule.h>
25 #include <net/netfilter/nf_nat_protocol.h>
26 #include <net/netfilter/nf_nat_core.h>
27 #include <net/netfilter/nf_nat_helper.h>
28 #include <linux/netfilter_ipv4/ip_tables.h>
31 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
33 const struct nf_conn *ct;
34 const struct nf_conntrack_tuple *t;
35 enum ip_conntrack_info ctinfo;
36 enum ip_conntrack_dir dir;
37 unsigned long statusbit;
39 ct = nf_ct_get(skb, &ctinfo);
42 dir = CTINFO2DIR(ctinfo);
43 t = &ct->tuplehash[dir].tuple;
45 if (dir == IP_CT_DIR_ORIGINAL)
46 statusbit = IPS_DST_NAT;
48 statusbit = IPS_SRC_NAT;
50 if (ct->status & statusbit) {
51 fl->fl4_dst = t->dst.u3.ip;
52 if (t->dst.protonum == IPPROTO_TCP ||
53 t->dst.protonum == IPPROTO_UDP ||
54 t->dst.protonum == IPPROTO_UDPLITE ||
55 t->dst.protonum == IPPROTO_DCCP ||
56 t->dst.protonum == IPPROTO_SCTP)
57 fl->fl_ip_dport = t->dst.u.tcp.port;
60 statusbit ^= IPS_NAT_MASK;
62 if (ct->status & statusbit) {
63 fl->fl4_src = t->src.u3.ip;
64 if (t->dst.protonum == IPPROTO_TCP ||
65 t->dst.protonum == IPPROTO_UDP ||
66 t->dst.protonum == IPPROTO_UDPLITE ||
67 t->dst.protonum == IPPROTO_DCCP ||
68 t->dst.protonum == IPPROTO_SCTP)
69 fl->fl_ip_sport = t->src.u.tcp.port;
75 nf_nat_fn(unsigned int hooknum,
77 const struct net_device *in,
78 const struct net_device *out,
79 int (*okfn)(struct sk_buff *))
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
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);
106 hp->type == ICMP_REDIRECT)
112 /* Don't try to NAT if this packet is not conntracked */
113 if (ct == &nf_conntrack_untracked)
118 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
120 pr_debug("failed to add NAT extension\n");
127 case IP_CT_RELATED+IP_CT_IS_REPLY:
128 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
129 if (!nf_nat_icmp_reply_translation(ct, ctinfo,
135 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
138 /* Seen it before? This can happen for loopback, retrans,
139 or local packets.. */
140 if (!nf_nat_initialized(ct, maniptype)) {
143 if (unlikely(nf_ct_is_confirmed(ct)))
144 /* NAT module was loaded late */
145 ret = alloc_null_binding_confirmed(ct, hooknum);
146 else if (hooknum == NF_INET_LOCAL_IN)
147 /* LOCAL_IN hook doesn't have a chain! */
148 ret = alloc_null_binding(ct, hooknum);
150 ret = nf_nat_rule_find(skb, hooknum, in, out,
153 if (ret != NF_ACCEPT) {
157 pr_debug("Already setup manip %s for ct %p\n",
158 maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
164 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
165 ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
168 return nf_nat_packet(ct, ctinfo, hooknum, skb);
172 nf_nat_in(unsigned int hooknum,
174 const struct net_device *in,
175 const struct net_device *out,
176 int (*okfn)(struct sk_buff *))
179 __be32 daddr = ip_hdr(skb)->daddr;
181 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
182 if (ret != NF_DROP && ret != NF_STOLEN &&
183 daddr != ip_hdr(skb)->daddr) {
184 dst_release(skb->dst);
191 nf_nat_out(unsigned int hooknum,
193 const struct net_device *in,
194 const struct net_device *out,
195 int (*okfn)(struct sk_buff *))
198 const struct nf_conn *ct;
199 enum ip_conntrack_info ctinfo;
203 /* root is playing with raw sockets. */
204 if (skb->len < sizeof(struct iphdr) ||
205 ip_hdrlen(skb) < sizeof(struct iphdr))
208 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
210 if (ret != NF_DROP && ret != NF_STOLEN &&
211 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
212 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
214 if (ct->tuplehash[dir].tuple.src.u3.ip !=
215 ct->tuplehash[!dir].tuple.dst.u3.ip
216 || ct->tuplehash[dir].tuple.src.u.all !=
217 ct->tuplehash[!dir].tuple.dst.u.all
219 return ip_xfrm_me_harder(skb) == 0 ? ret : NF_DROP;
226 nf_nat_local_fn(unsigned int hooknum,
228 const struct net_device *in,
229 const struct net_device *out,
230 int (*okfn)(struct sk_buff *))
232 const struct nf_conn *ct;
233 enum ip_conntrack_info ctinfo;
236 /* root is playing with raw sockets. */
237 if (skb->len < sizeof(struct iphdr) ||
238 ip_hdrlen(skb) < sizeof(struct iphdr))
241 ret = nf_nat_fn(hooknum, skb, in, out, okfn);
242 if (ret != NF_DROP && ret != NF_STOLEN &&
243 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
244 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
246 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
247 ct->tuplehash[!dir].tuple.src.u3.ip) {
248 if (ip_route_me_harder(skb, RTN_UNSPEC))
252 else if (ct->tuplehash[dir].tuple.dst.u.all !=
253 ct->tuplehash[!dir].tuple.src.u.all)
254 if (ip_xfrm_me_harder(skb))
262 nf_nat_adjust(unsigned int hooknum,
264 const struct net_device *in,
265 const struct net_device *out,
266 int (*okfn)(struct sk_buff *))
269 enum ip_conntrack_info ctinfo;
271 ct = nf_ct_get(skb, &ctinfo);
272 if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
273 pr_debug("nf_nat_standalone: adjusting sequence number\n");
274 if (!nf_nat_seq_adjust(skb, ct, ctinfo))
280 /* We must be after connection tracking and before packet filtering. */
282 static struct nf_hook_ops nf_nat_ops[] __read_mostly = {
283 /* Before packet filtering, change destination */
286 .owner = THIS_MODULE,
288 .hooknum = NF_INET_PRE_ROUTING,
289 .priority = NF_IP_PRI_NAT_DST,
291 /* After packet filtering, change source */
294 .owner = THIS_MODULE,
296 .hooknum = NF_INET_POST_ROUTING,
297 .priority = NF_IP_PRI_NAT_SRC,
299 /* After conntrack, adjust sequence number */
301 .hook = nf_nat_adjust,
302 .owner = THIS_MODULE,
304 .hooknum = NF_INET_POST_ROUTING,
305 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
307 /* Before packet filtering, change destination */
309 .hook = nf_nat_local_fn,
310 .owner = THIS_MODULE,
312 .hooknum = NF_INET_LOCAL_OUT,
313 .priority = NF_IP_PRI_NAT_DST,
315 /* After packet filtering, change source */
318 .owner = THIS_MODULE,
320 .hooknum = NF_INET_LOCAL_IN,
321 .priority = NF_IP_PRI_NAT_SRC,
323 /* After conntrack, adjust sequence number */
325 .hook = nf_nat_adjust,
326 .owner = THIS_MODULE,
328 .hooknum = NF_INET_LOCAL_IN,
329 .priority = NF_IP_PRI_NAT_SEQ_ADJUST,
333 static int __init nf_nat_standalone_init(void)
337 need_ipv4_conntrack();
340 BUG_ON(ip_nat_decode_session != NULL);
341 rcu_assign_pointer(ip_nat_decode_session, nat_decode_session);
343 ret = nf_nat_rule_init();
345 printk("nf_nat_init: can't setup rules.\n");
346 goto cleanup_decode_session;
348 ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
350 printk("nf_nat_init: can't register hooks.\n");
351 goto cleanup_rule_init;
356 nf_nat_rule_cleanup();
357 cleanup_decode_session:
359 rcu_assign_pointer(ip_nat_decode_session, NULL);
365 static void __exit nf_nat_standalone_fini(void)
367 nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
368 nf_nat_rule_cleanup();
370 rcu_assign_pointer(ip_nat_decode_session, NULL);
373 /* Conntrack caches are unregistered in nf_conntrack_cleanup */
376 module_init(nf_nat_standalone_init);
377 module_exit(nf_nat_standalone_fini);
379 MODULE_LICENSE("GPL");
380 MODULE_ALIAS("ip_nat");