15aa3db8cb339fe534097881c2c63b28f78cbc3b
[linux-3.10.git] / net / ipv4 / netfilter / nf_nat_standalone.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3  *
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>
19
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
29 #if 0
30 #define DEBUGP printk
31 #else
32 #define DEBUGP(format, args...)
33 #endif
34
35 #ifdef CONFIG_XFRM
36 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
37 {
38         struct nf_conn *ct;
39         struct nf_conntrack_tuple *t;
40         enum ip_conntrack_info ctinfo;
41         enum ip_conntrack_dir dir;
42         unsigned long statusbit;
43
44         ct = nf_ct_get(skb, &ctinfo);
45         if (ct == NULL)
46                 return;
47         dir = CTINFO2DIR(ctinfo);
48         t = &ct->tuplehash[dir].tuple;
49
50         if (dir == IP_CT_DIR_ORIGINAL)
51                 statusbit = IPS_DST_NAT;
52         else
53                 statusbit = IPS_SRC_NAT;
54
55         if (ct->status & statusbit) {
56                 fl->fl4_dst = t->dst.u3.ip;
57                 if (t->dst.protonum == IPPROTO_TCP ||
58                     t->dst.protonum == IPPROTO_UDP)
59                         fl->fl_ip_dport = t->dst.u.tcp.port;
60         }
61
62         statusbit ^= IPS_NAT_MASK;
63
64         if (ct->status & statusbit) {
65                 fl->fl4_src = t->src.u3.ip;
66                 if (t->dst.protonum == IPPROTO_TCP ||
67                     t->dst.protonum == IPPROTO_UDP)
68                         fl->fl_ip_sport = t->src.u.tcp.port;
69         }
70 }
71 #endif
72
73 static unsigned int
74 nf_nat_fn(unsigned int hooknum,
75           struct sk_buff **pskb,
76           const struct net_device *in,
77           const struct net_device *out,
78           int (*okfn)(struct sk_buff *))
79 {
80         struct nf_conn *ct;
81         enum ip_conntrack_info ctinfo;
82         struct nf_conn_nat *nat;
83         struct nf_nat_info *info;
84         /* maniptype == SRC for postrouting. */
85         enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum);
86
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(!((*pskb)->nh.iph->frag_off
90                        & htons(IP_MF|IP_OFFSET)));
91
92         ct = nf_ct_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
96            protocol. 8) --RR */
97         if (!ct) {
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) {
102                         struct icmphdr _hdr, *hp;
103
104                         hp = skb_header_pointer(*pskb,
105                                                 (*pskb)->nh.iph->ihl*4,
106                                                 sizeof(_hdr), &_hdr);
107                         if (hp != NULL &&
108                             hp->type == ICMP_REDIRECT)
109                                 return NF_DROP;
110                 }
111                 return NF_ACCEPT;
112         }
113
114         /* Don't try to NAT if this packet is not conntracked */
115         if (ct == &nf_conntrack_untracked)
116                 return NF_ACCEPT;
117
118         nat = nfct_nat(ct);
119         if (!nat)
120                 return NF_ACCEPT;
121
122         switch (ctinfo) {
123         case IP_CT_RELATED:
124         case IP_CT_RELATED+IP_CT_IS_REPLY:
125                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
126                         if (!nf_nat_icmp_reply_translation(ct, ctinfo,
127                                                            hooknum, pskb))
128                                 return NF_DROP;
129                         else
130                                 return NF_ACCEPT;
131                 }
132                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
133         case IP_CT_NEW:
134                 info = &nat->info;
135
136                 /* Seen it before?  This can happen for loopback, retrans,
137                    or local packets.. */
138                 if (!nf_nat_initialized(ct, maniptype)) {
139                         unsigned int ret;
140
141                         if (unlikely(nf_ct_is_confirmed(ct)))
142                                 /* NAT module was loaded late */
143                                 ret = alloc_null_binding_confirmed(ct, info,
144                                                                    hooknum);
145                         else if (hooknum == NF_IP_LOCAL_IN)
146                                 /* LOCAL_IN hook doesn't have a chain!  */
147                                 ret = alloc_null_binding(ct, info, hooknum);
148                         else
149                                 ret = nf_nat_rule_find(pskb, hooknum, in, out,
150                                                        ct, info);
151
152                         if (ret != NF_ACCEPT) {
153                                 return ret;
154                         }
155                 } else
156                         DEBUGP("Already setup manip %s for ct %p\n",
157                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
158                                ct);
159                 break;
160
161         default:
162                 /* ESTABLISHED */
163                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
164                              ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
165                 info = &nat->info;
166         }
167
168         NF_CT_ASSERT(info);
169         return nf_nat_packet(ct, ctinfo, hooknum, pskb);
170 }
171
172 static unsigned int
173 nf_nat_in(unsigned int hooknum,
174           struct sk_buff **pskb,
175           const struct net_device *in,
176           const struct net_device *out,
177           int (*okfn)(struct sk_buff *))
178 {
179         unsigned int ret;
180         __be32 daddr = (*pskb)->nh.iph->daddr;
181
182         ret = nf_nat_fn(hooknum, pskb, in, out, okfn);
183         if (ret != NF_DROP && ret != NF_STOLEN &&
184             daddr != (*pskb)->nh.iph->daddr) {
185                 dst_release((*pskb)->dst);
186                 (*pskb)->dst = NULL;
187         }
188         return ret;
189 }
190
191 static unsigned int
192 nf_nat_out(unsigned int hooknum,
193            struct sk_buff **pskb,
194            const struct net_device *in,
195            const struct net_device *out,
196            int (*okfn)(struct sk_buff *))
197 {
198 #ifdef CONFIG_XFRM
199         struct nf_conn *ct;
200         enum ip_conntrack_info ctinfo;
201 #endif
202         unsigned int ret;
203
204         /* root is playing with raw sockets. */
205         if ((*pskb)->len < sizeof(struct iphdr) ||
206             (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
207                 return NF_ACCEPT;
208
209         ret = nf_nat_fn(hooknum, pskb, in, out, okfn);
210 #ifdef CONFIG_XFRM
211         if (ret != NF_DROP && ret != NF_STOLEN &&
212             (ct = nf_ct_get(*pskb, &ctinfo)) != NULL) {
213                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
214
215                 if (ct->tuplehash[dir].tuple.src.u3.ip !=
216                     ct->tuplehash[!dir].tuple.dst.u3.ip
217                     || ct->tuplehash[dir].tuple.src.u.all !=
218                        ct->tuplehash[!dir].tuple.dst.u.all
219                     )
220                         return ip_xfrm_me_harder(pskb) == 0 ? ret : NF_DROP;
221         }
222 #endif
223         return ret;
224 }
225
226 static unsigned int
227 nf_nat_local_fn(unsigned int hooknum,
228                 struct sk_buff **pskb,
229                 const struct net_device *in,
230                 const struct net_device *out,
231                 int (*okfn)(struct sk_buff *))
232 {
233         struct nf_conn *ct;
234         enum ip_conntrack_info ctinfo;
235         unsigned int ret;
236
237         /* root is playing with raw sockets. */
238         if ((*pskb)->len < sizeof(struct iphdr) ||
239             (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
240                 return NF_ACCEPT;
241
242         ret = nf_nat_fn(hooknum, pskb, in, out, okfn);
243         if (ret != NF_DROP && ret != NF_STOLEN &&
244             (ct = nf_ct_get(*pskb, &ctinfo)) != NULL) {
245                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
246
247                 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
248                     ct->tuplehash[!dir].tuple.src.u3.ip) {
249                         if (ip_route_me_harder(pskb, RTN_UNSPEC))
250                                 ret = NF_DROP;
251                 }
252 #ifdef CONFIG_XFRM
253                 else if (ct->tuplehash[dir].tuple.dst.u.all !=
254                          ct->tuplehash[!dir].tuple.src.u.all)
255                         if (ip_xfrm_me_harder(pskb))
256                                 ret = NF_DROP;
257 #endif
258         }
259         return ret;
260 }
261
262 static unsigned int
263 nf_nat_adjust(unsigned int hooknum,
264               struct sk_buff **pskb,
265               const struct net_device *in,
266               const struct net_device *out,
267               int (*okfn)(struct sk_buff *))
268 {
269         struct nf_conn *ct;
270         enum ip_conntrack_info ctinfo;
271
272         ct = nf_ct_get(*pskb, &ctinfo);
273         if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
274                 DEBUGP("nf_nat_standalone: adjusting sequence number\n");
275                 if (!nf_nat_seq_adjust(pskb, ct, ctinfo))
276                         return NF_DROP;
277         }
278         return NF_ACCEPT;
279 }
280
281 /* We must be after connection tracking and before packet filtering. */
282
283 static struct nf_hook_ops nf_nat_ops[] = {
284         /* Before packet filtering, change destination */
285         {
286                 .hook           = nf_nat_in,
287                 .owner          = THIS_MODULE,
288                 .pf             = PF_INET,
289                 .hooknum        = NF_IP_PRE_ROUTING,
290                 .priority       = NF_IP_PRI_NAT_DST,
291         },
292         /* After packet filtering, change source */
293         {
294                 .hook           = nf_nat_out,
295                 .owner          = THIS_MODULE,
296                 .pf             = PF_INET,
297                 .hooknum        = NF_IP_POST_ROUTING,
298                 .priority       = NF_IP_PRI_NAT_SRC,
299         },
300         /* After conntrack, adjust sequence number */
301         {
302                 .hook           = nf_nat_adjust,
303                 .owner          = THIS_MODULE,
304                 .pf             = PF_INET,
305                 .hooknum        = NF_IP_POST_ROUTING,
306                 .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
307         },
308         /* Before packet filtering, change destination */
309         {
310                 .hook           = nf_nat_local_fn,
311                 .owner          = THIS_MODULE,
312                 .pf             = PF_INET,
313                 .hooknum        = NF_IP_LOCAL_OUT,
314                 .priority       = NF_IP_PRI_NAT_DST,
315         },
316         /* After packet filtering, change source */
317         {
318                 .hook           = nf_nat_fn,
319                 .owner          = THIS_MODULE,
320                 .pf             = PF_INET,
321                 .hooknum        = NF_IP_LOCAL_IN,
322                 .priority       = NF_IP_PRI_NAT_SRC,
323         },
324         /* After conntrack, adjust sequence number */
325         {
326                 .hook           = nf_nat_adjust,
327                 .owner          = THIS_MODULE,
328                 .pf             = PF_INET,
329                 .hooknum        = NF_IP_LOCAL_IN,
330                 .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
331         },
332 };
333
334 static int __init nf_nat_standalone_init(void)
335 {
336         int size, ret = 0;
337
338         need_conntrack();
339
340         size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_nat)) +
341                sizeof(struct nf_conn_nat);
342         ret = nf_conntrack_register_cache(NF_CT_F_NAT, "nf_nat:base", size);
343         if (ret < 0) {
344                 printk(KERN_ERR "nf_nat_init: Unable to create slab cache\n");
345                 return ret;
346         }
347
348         size = ALIGN(size, __alignof__(struct nf_conn_help)) +
349                sizeof(struct nf_conn_help);
350         ret = nf_conntrack_register_cache(NF_CT_F_NAT|NF_CT_F_HELP,
351                                           "nf_nat:help", size);
352         if (ret < 0) {
353                 printk(KERN_ERR "nf_nat_init: Unable to create slab cache\n");
354                 goto cleanup_register_cache;
355         }
356 #ifdef CONFIG_XFRM
357         BUG_ON(ip_nat_decode_session != NULL);
358         ip_nat_decode_session = nat_decode_session;
359 #endif
360         ret = nf_nat_rule_init();
361         if (ret < 0) {
362                 printk("nf_nat_init: can't setup rules.\n");
363                 goto cleanup_decode_session;
364         }
365         ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
366         if (ret < 0) {
367                 printk("nf_nat_init: can't register hooks.\n");
368                 goto cleanup_rule_init;
369         }
370         nf_nat_module_is_loaded = 1;
371         return ret;
372
373  cleanup_rule_init:
374         nf_nat_rule_cleanup();
375  cleanup_decode_session:
376 #ifdef CONFIG_XFRM
377         ip_nat_decode_session = NULL;
378         synchronize_net();
379 #endif
380         nf_conntrack_unregister_cache(NF_CT_F_NAT|NF_CT_F_HELP);
381  cleanup_register_cache:
382         nf_conntrack_unregister_cache(NF_CT_F_NAT);
383         return ret;
384 }
385
386 static void __exit nf_nat_standalone_fini(void)
387 {
388         nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
389         nf_nat_rule_cleanup();
390         nf_nat_module_is_loaded = 0;
391 #ifdef CONFIG_XFRM
392         ip_nat_decode_session = NULL;
393         synchronize_net();
394 #endif
395         /* Conntrack caches are unregistered in nf_conntrack_cleanup */
396 }
397
398 module_init(nf_nat_standalone_init);
399 module_exit(nf_nat_standalone_fini);
400
401 MODULE_LICENSE("GPL");
402 MODULE_ALIAS("ip_nat");