[IP]: Introduce ip_hdrlen()
[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, ip_hdrlen(*pskb),
105                                                 sizeof(_hdr), &_hdr);
106                         if (hp != NULL &&
107                             hp->type == ICMP_REDIRECT)
108                                 return NF_DROP;
109                 }
110                 return NF_ACCEPT;
111         }
112
113         /* Don't try to NAT if this packet is not conntracked */
114         if (ct == &nf_conntrack_untracked)
115                 return NF_ACCEPT;
116
117         nat = nfct_nat(ct);
118         if (!nat)
119                 return NF_ACCEPT;
120
121         switch (ctinfo) {
122         case IP_CT_RELATED:
123         case IP_CT_RELATED+IP_CT_IS_REPLY:
124                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
125                         if (!nf_nat_icmp_reply_translation(ct, ctinfo,
126                                                            hooknum, pskb))
127                                 return NF_DROP;
128                         else
129                                 return NF_ACCEPT;
130                 }
131                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
132         case IP_CT_NEW:
133                 info = &nat->info;
134
135                 /* Seen it before?  This can happen for loopback, retrans,
136                    or local packets.. */
137                 if (!nf_nat_initialized(ct, maniptype)) {
138                         unsigned int ret;
139
140                         if (unlikely(nf_ct_is_confirmed(ct)))
141                                 /* NAT module was loaded late */
142                                 ret = alloc_null_binding_confirmed(ct, info,
143                                                                    hooknum);
144                         else if (hooknum == NF_IP_LOCAL_IN)
145                                 /* LOCAL_IN hook doesn't have a chain!  */
146                                 ret = alloc_null_binding(ct, info, hooknum);
147                         else
148                                 ret = nf_nat_rule_find(pskb, hooknum, in, out,
149                                                        ct, info);
150
151                         if (ret != NF_ACCEPT) {
152                                 return ret;
153                         }
154                 } else
155                         DEBUGP("Already setup manip %s for ct %p\n",
156                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
157                                ct);
158                 break;
159
160         default:
161                 /* ESTABLISHED */
162                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
163                              ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
164                 info = &nat->info;
165         }
166
167         NF_CT_ASSERT(info);
168         return nf_nat_packet(ct, ctinfo, hooknum, pskb);
169 }
170
171 static unsigned int
172 nf_nat_in(unsigned int hooknum,
173           struct sk_buff **pskb,
174           const struct net_device *in,
175           const struct net_device *out,
176           int (*okfn)(struct sk_buff *))
177 {
178         unsigned int ret;
179         __be32 daddr = (*pskb)->nh.iph->daddr;
180
181         ret = nf_nat_fn(hooknum, pskb, in, out, okfn);
182         if (ret != NF_DROP && ret != NF_STOLEN &&
183             daddr != (*pskb)->nh.iph->daddr) {
184                 dst_release((*pskb)->dst);
185                 (*pskb)->dst = NULL;
186         }
187         return ret;
188 }
189
190 static unsigned int
191 nf_nat_out(unsigned int hooknum,
192            struct sk_buff **pskb,
193            const struct net_device *in,
194            const struct net_device *out,
195            int (*okfn)(struct sk_buff *))
196 {
197 #ifdef CONFIG_XFRM
198         struct nf_conn *ct;
199         enum ip_conntrack_info ctinfo;
200 #endif
201         unsigned int ret;
202
203         /* root is playing with raw sockets. */
204         if ((*pskb)->len < sizeof(struct iphdr) ||
205             ip_hdrlen(*pskb) < sizeof(struct iphdr))
206                 return NF_ACCEPT;
207
208         ret = nf_nat_fn(hooknum, pskb, in, out, okfn);
209 #ifdef CONFIG_XFRM
210         if (ret != NF_DROP && ret != NF_STOLEN &&
211             (ct = nf_ct_get(*pskb, &ctinfo)) != NULL) {
212                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
213
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
218                     )
219                         return ip_xfrm_me_harder(pskb) == 0 ? ret : NF_DROP;
220         }
221 #endif
222         return ret;
223 }
224
225 static unsigned int
226 nf_nat_local_fn(unsigned int hooknum,
227                 struct sk_buff **pskb,
228                 const struct net_device *in,
229                 const struct net_device *out,
230                 int (*okfn)(struct sk_buff *))
231 {
232         struct nf_conn *ct;
233         enum ip_conntrack_info ctinfo;
234         unsigned int ret;
235
236         /* root is playing with raw sockets. */
237         if ((*pskb)->len < sizeof(struct iphdr) ||
238             ip_hdrlen(*pskb) < sizeof(struct iphdr))
239                 return NF_ACCEPT;
240
241         ret = nf_nat_fn(hooknum, pskb, in, out, okfn);
242         if (ret != NF_DROP && ret != NF_STOLEN &&
243             (ct = nf_ct_get(*pskb, &ctinfo)) != NULL) {
244                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
245
246                 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
247                     ct->tuplehash[!dir].tuple.src.u3.ip) {
248                         if (ip_route_me_harder(pskb, RTN_UNSPEC))
249                                 ret = NF_DROP;
250                 }
251 #ifdef CONFIG_XFRM
252                 else if (ct->tuplehash[dir].tuple.dst.u.all !=
253                          ct->tuplehash[!dir].tuple.src.u.all)
254                         if (ip_xfrm_me_harder(pskb))
255                                 ret = NF_DROP;
256 #endif
257         }
258         return ret;
259 }
260
261 static unsigned int
262 nf_nat_adjust(unsigned int hooknum,
263               struct sk_buff **pskb,
264               const struct net_device *in,
265               const struct net_device *out,
266               int (*okfn)(struct sk_buff *))
267 {
268         struct nf_conn *ct;
269         enum ip_conntrack_info ctinfo;
270
271         ct = nf_ct_get(*pskb, &ctinfo);
272         if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
273                 DEBUGP("nf_nat_standalone: adjusting sequence number\n");
274                 if (!nf_nat_seq_adjust(pskb, ct, ctinfo))
275                         return NF_DROP;
276         }
277         return NF_ACCEPT;
278 }
279
280 /* We must be after connection tracking and before packet filtering. */
281
282 static struct nf_hook_ops nf_nat_ops[] = {
283         /* Before packet filtering, change destination */
284         {
285                 .hook           = nf_nat_in,
286                 .owner          = THIS_MODULE,
287                 .pf             = PF_INET,
288                 .hooknum        = NF_IP_PRE_ROUTING,
289                 .priority       = NF_IP_PRI_NAT_DST,
290         },
291         /* After packet filtering, change source */
292         {
293                 .hook           = nf_nat_out,
294                 .owner          = THIS_MODULE,
295                 .pf             = PF_INET,
296                 .hooknum        = NF_IP_POST_ROUTING,
297                 .priority       = NF_IP_PRI_NAT_SRC,
298         },
299         /* After conntrack, adjust sequence number */
300         {
301                 .hook           = nf_nat_adjust,
302                 .owner          = THIS_MODULE,
303                 .pf             = PF_INET,
304                 .hooknum        = NF_IP_POST_ROUTING,
305                 .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
306         },
307         /* Before packet filtering, change destination */
308         {
309                 .hook           = nf_nat_local_fn,
310                 .owner          = THIS_MODULE,
311                 .pf             = PF_INET,
312                 .hooknum        = NF_IP_LOCAL_OUT,
313                 .priority       = NF_IP_PRI_NAT_DST,
314         },
315         /* After packet filtering, change source */
316         {
317                 .hook           = nf_nat_fn,
318                 .owner          = THIS_MODULE,
319                 .pf             = PF_INET,
320                 .hooknum        = NF_IP_LOCAL_IN,
321                 .priority       = NF_IP_PRI_NAT_SRC,
322         },
323         /* After conntrack, adjust sequence number */
324         {
325                 .hook           = nf_nat_adjust,
326                 .owner          = THIS_MODULE,
327                 .pf             = PF_INET,
328                 .hooknum        = NF_IP_LOCAL_IN,
329                 .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
330         },
331 };
332
333 static int __init nf_nat_standalone_init(void)
334 {
335         int size, ret = 0;
336
337         need_conntrack();
338
339         size = ALIGN(sizeof(struct nf_conn), __alignof__(struct nf_conn_nat)) +
340                sizeof(struct nf_conn_nat);
341         ret = nf_conntrack_register_cache(NF_CT_F_NAT, "nf_nat:base", size);
342         if (ret < 0) {
343                 printk(KERN_ERR "nf_nat_init: Unable to create slab cache\n");
344                 return ret;
345         }
346
347         size = ALIGN(size, __alignof__(struct nf_conn_help)) +
348                sizeof(struct nf_conn_help);
349         ret = nf_conntrack_register_cache(NF_CT_F_NAT|NF_CT_F_HELP,
350                                           "nf_nat:help", size);
351         if (ret < 0) {
352                 printk(KERN_ERR "nf_nat_init: Unable to create slab cache\n");
353                 goto cleanup_register_cache;
354         }
355 #ifdef CONFIG_XFRM
356         BUG_ON(ip_nat_decode_session != NULL);
357         ip_nat_decode_session = nat_decode_session;
358 #endif
359         ret = nf_nat_rule_init();
360         if (ret < 0) {
361                 printk("nf_nat_init: can't setup rules.\n");
362                 goto cleanup_decode_session;
363         }
364         ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
365         if (ret < 0) {
366                 printk("nf_nat_init: can't register hooks.\n");
367                 goto cleanup_rule_init;
368         }
369         nf_nat_module_is_loaded = 1;
370         return ret;
371
372  cleanup_rule_init:
373         nf_nat_rule_cleanup();
374  cleanup_decode_session:
375 #ifdef CONFIG_XFRM
376         ip_nat_decode_session = NULL;
377         synchronize_net();
378 #endif
379         nf_conntrack_unregister_cache(NF_CT_F_NAT|NF_CT_F_HELP);
380  cleanup_register_cache:
381         nf_conntrack_unregister_cache(NF_CT_F_NAT);
382         return ret;
383 }
384
385 static void __exit nf_nat_standalone_fini(void)
386 {
387         nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops));
388         nf_nat_rule_cleanup();
389         nf_nat_module_is_loaded = 0;
390 #ifdef CONFIG_XFRM
391         ip_nat_decode_session = NULL;
392         synchronize_net();
393 #endif
394         /* Conntrack caches are unregistered in nf_conntrack_cleanup */
395 }
396
397 module_init(nf_nat_standalone_init);
398 module_exit(nf_nat_standalone_fini);
399
400 MODULE_LICENSE("GPL");
401 MODULE_ALIAS("ip_nat");