[NETFILTER]: Handle NAT module load race
[linux-3.10.git] / net / ipv4 / netfilter / ip_nat_standalone.c
1 /* This file contains all the functions required for the standalone
2    ip_nat module.
3
4    These are not required by the compatibility layer.
5 */
6
7 /* (C) 1999-2001 Paul `Rusty' Russell
8  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
9  *
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.
13  */
14
15 /*
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  * */
20
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/icmp.h>
24 #include <linux/ip.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>
30 #include <net/ip.h>
31 #include <net/checksum.h>
32 #include <linux/spinlock.h>
33
34 #define ASSERT_READ_LOCK(x)
35 #define ASSERT_WRITE_LOCK(x)
36
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>
45
46 #if 0
47 #define DEBUGP printk
48 #else
49 #define DEBUGP(format, args...)
50 #endif
51
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"  \
56                                     : "*ERROR*")))
57
58 static unsigned int
59 ip_nat_fn(unsigned int hooknum,
60           struct sk_buff **pskb,
61           const struct net_device *in,
62           const struct net_device *out,
63           int (*okfn)(struct sk_buff *))
64 {
65         struct ip_conntrack *ct;
66         enum ip_conntrack_info ctinfo;
67         struct ip_nat_info *info;
68         /* maniptype == SRC for postrouting. */
69         enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
70
71         /* We never see fragments: conntrack defrags on pre-routing
72            and local-out, and ip_nat_out protects post-routing. */
73         IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
74                        & htons(IP_MF|IP_OFFSET)));
75
76         /* If we had a hardware checksum before, it's now invalid */
77         if ((*pskb)->ip_summed == CHECKSUM_HW)
78                 if (skb_checksum_help(*pskb, (out == NULL)))
79                         return NF_DROP;
80
81         ct = ip_conntrack_get(*pskb, &ctinfo);
82         /* Can't track?  It's not due to stress, or conntrack would
83            have dropped it.  Hence it's the user's responsibilty to
84            packet filter it out, or implement conntrack/NAT for that
85            protocol. 8) --RR */
86         if (!ct) {
87                 /* Exception: ICMP redirect to new connection (not in
88                    hash table yet).  We must not let this through, in
89                    case we're doing NAT to the same network. */
90                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
91                         struct icmphdr _hdr, *hp;
92
93                         hp = skb_header_pointer(*pskb,
94                                                 (*pskb)->nh.iph->ihl*4,
95                                                 sizeof(_hdr), &_hdr);
96                         if (hp != NULL &&
97                             hp->type == ICMP_REDIRECT)
98                                 return NF_DROP;
99                 }
100                 return NF_ACCEPT;
101         }
102
103         /* Don't try to NAT if this packet is not conntracked */
104         if (ct == &ip_conntrack_untracked)
105                 return NF_ACCEPT;
106
107         switch (ctinfo) {
108         case IP_CT_RELATED:
109         case IP_CT_RELATED+IP_CT_IS_REPLY:
110                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
111                         if (!icmp_reply_translation(pskb, ct, maniptype,
112                                                     CTINFO2DIR(ctinfo)))
113                                 return NF_DROP;
114                         else
115                                 return NF_ACCEPT;
116                 }
117                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
118         case IP_CT_NEW:
119                 info = &ct->nat.info;
120
121                 /* Seen it before?  This can happen for loopback, retrans,
122                    or local packets.. */
123                 if (!ip_nat_initialized(ct, maniptype)) {
124                         unsigned int ret;
125
126                         if (unlikely(is_confirmed(ct)))
127                                 /* NAT module was loaded late */
128                                 ret = alloc_null_binding_confirmed(ct, info,
129                                                                    hooknum);
130                         else if (hooknum == NF_IP_LOCAL_IN)
131                                 /* LOCAL_IN hook doesn't have a chain!  */
132                                 ret = alloc_null_binding(ct, info, hooknum);
133                         else
134                                 ret = ip_nat_rule_find(pskb, hooknum,
135                                                        in, out, ct,
136                                                        info);
137
138                         if (ret != NF_ACCEPT) {
139                                 return ret;
140                         }
141                 } else
142                         DEBUGP("Already setup manip %s for ct %p\n",
143                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
144                                ct);
145                 break;
146
147         default:
148                 /* ESTABLISHED */
149                 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
150                              || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
151                 info = &ct->nat.info;
152         }
153
154         IP_NF_ASSERT(info);
155         return nat_packet(ct, ctinfo, hooknum, pskb);
156 }
157
158 static unsigned int
159 ip_nat_in(unsigned int hooknum,
160           struct sk_buff **pskb,
161           const struct net_device *in,
162           const struct net_device *out,
163           int (*okfn)(struct sk_buff *))
164 {
165         u_int32_t saddr, daddr;
166         unsigned int ret;
167
168         saddr = (*pskb)->nh.iph->saddr;
169         daddr = (*pskb)->nh.iph->daddr;
170
171         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
172         if (ret != NF_DROP && ret != NF_STOLEN
173             && ((*pskb)->nh.iph->saddr != saddr
174                 || (*pskb)->nh.iph->daddr != daddr)) {
175                 dst_release((*pskb)->dst);
176                 (*pskb)->dst = NULL;
177         }
178         return ret;
179 }
180
181 static unsigned int
182 ip_nat_out(unsigned int hooknum,
183            struct sk_buff **pskb,
184            const struct net_device *in,
185            const struct net_device *out,
186            int (*okfn)(struct sk_buff *))
187 {
188         /* root is playing with raw sockets. */
189         if ((*pskb)->len < sizeof(struct iphdr)
190             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
191                 return NF_ACCEPT;
192
193         /* We can hit fragment here; forwarded packets get
194            defragmented by connection tracking coming in, then
195            fragmented (grr) by the forward code.
196
197            In future: If we have nfct != NULL, AND we have NAT
198            initialized, AND there is no helper, then we can do full
199            NAPT on the head, and IP-address-only NAT on the rest.
200
201            I'm starting to have nightmares about fragments.  */
202
203         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
204                 *pskb = ip_ct_gather_frags(*pskb, IP_DEFRAG_NAT_OUT);
205
206                 if (!*pskb)
207                         return NF_STOLEN;
208         }
209
210         return ip_nat_fn(hooknum, pskb, in, out, okfn);
211 }
212
213 static unsigned int
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 *))
219 {
220         u_int32_t saddr, daddr;
221         unsigned int ret;
222
223         /* root is playing with raw sockets. */
224         if ((*pskb)->len < sizeof(struct iphdr)
225             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
226                 return NF_ACCEPT;
227
228         saddr = (*pskb)->nh.iph->saddr;
229         daddr = (*pskb)->nh.iph->daddr;
230
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;
236         return ret;
237 }
238
239 static unsigned int
240 ip_nat_adjust(unsigned int hooknum,
241               struct sk_buff **pskb,
242               const struct net_device *in,
243               const struct net_device *out,
244               int (*okfn)(struct sk_buff *))
245 {
246         struct ip_conntrack *ct;
247         enum ip_conntrack_info ctinfo;
248
249         ct = ip_conntrack_get(*pskb, &ctinfo);
250         if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
251                 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
252                 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
253                         return NF_DROP;
254         }
255         return NF_ACCEPT;
256 }
257
258 /* We must be after connection tracking and before packet filtering. */
259
260 /* Before packet filtering, change destination */
261 static struct nf_hook_ops ip_nat_in_ops = {
262         .hook           = ip_nat_in,
263         .owner          = THIS_MODULE,
264         .pf             = PF_INET,
265         .hooknum        = NF_IP_PRE_ROUTING,
266         .priority       = NF_IP_PRI_NAT_DST,
267 };
268
269 /* After packet filtering, change source */
270 static struct nf_hook_ops ip_nat_out_ops = {
271         .hook           = ip_nat_out,
272         .owner          = THIS_MODULE,
273         .pf             = PF_INET,
274         .hooknum        = NF_IP_POST_ROUTING,
275         .priority       = NF_IP_PRI_NAT_SRC,
276 };
277
278 /* After conntrack, adjust sequence number */
279 static struct nf_hook_ops ip_nat_adjust_out_ops = {
280         .hook           = ip_nat_adjust,
281         .owner          = THIS_MODULE,
282         .pf             = PF_INET,
283         .hooknum        = NF_IP_POST_ROUTING,
284         .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
285 };
286
287 /* Before packet filtering, change destination */
288 static struct nf_hook_ops ip_nat_local_out_ops = {
289         .hook           = ip_nat_local_fn,
290         .owner          = THIS_MODULE,
291         .pf             = PF_INET,
292         .hooknum        = NF_IP_LOCAL_OUT,
293         .priority       = NF_IP_PRI_NAT_DST,
294 };
295
296 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
297 static struct nf_hook_ops ip_nat_local_in_ops = {
298         .hook           = ip_nat_fn,
299         .owner          = THIS_MODULE,
300         .pf             = PF_INET,
301         .hooknum        = NF_IP_LOCAL_IN,
302         .priority       = NF_IP_PRI_NAT_SRC,
303 };
304
305 /* After conntrack, adjust sequence number */
306 static struct nf_hook_ops ip_nat_adjust_in_ops = {
307         .hook           = ip_nat_adjust,
308         .owner          = THIS_MODULE,
309         .pf             = PF_INET,
310         .hooknum        = NF_IP_LOCAL_IN,
311         .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
312 };
313
314
315 static int init_or_cleanup(int init)
316 {
317         int ret = 0;
318
319         need_ip_conntrack();
320
321         if (!init) goto cleanup;
322
323         ret = ip_nat_rule_init();
324         if (ret < 0) {
325                 printk("ip_nat_init: can't setup rules.\n");
326                 goto cleanup_nothing;
327         }
328         ret = ip_nat_init();
329         if (ret < 0) {
330                 printk("ip_nat_init: can't setup rules.\n");
331                 goto cleanup_rule_init;
332         }
333         ret = nf_register_hook(&ip_nat_in_ops);
334         if (ret < 0) {
335                 printk("ip_nat_init: can't register in hook.\n");
336                 goto cleanup_nat;
337         }
338         ret = nf_register_hook(&ip_nat_out_ops);
339         if (ret < 0) {
340                 printk("ip_nat_init: can't register out hook.\n");
341                 goto cleanup_inops;
342         }
343         ret = nf_register_hook(&ip_nat_adjust_in_ops);
344         if (ret < 0) {
345                 printk("ip_nat_init: can't register adjust in hook.\n");
346                 goto cleanup_outops;
347         }
348         ret = nf_register_hook(&ip_nat_adjust_out_ops);
349         if (ret < 0) {
350                 printk("ip_nat_init: can't register adjust out hook.\n");
351                 goto cleanup_adjustin_ops;
352         }
353         ret = nf_register_hook(&ip_nat_local_out_ops);
354         if (ret < 0) {
355                 printk("ip_nat_init: can't register local out hook.\n");
356                 goto cleanup_adjustout_ops;;
357         }
358         ret = nf_register_hook(&ip_nat_local_in_ops);
359         if (ret < 0) {
360                 printk("ip_nat_init: can't register local in hook.\n");
361                 goto cleanup_localoutops;
362         }
363         return ret;
364
365  cleanup:
366         nf_unregister_hook(&ip_nat_local_in_ops);
367  cleanup_localoutops:
368         nf_unregister_hook(&ip_nat_local_out_ops);
369  cleanup_adjustout_ops:
370         nf_unregister_hook(&ip_nat_adjust_out_ops);
371  cleanup_adjustin_ops:
372         nf_unregister_hook(&ip_nat_adjust_in_ops);
373  cleanup_outops:
374         nf_unregister_hook(&ip_nat_out_ops);
375  cleanup_inops:
376         nf_unregister_hook(&ip_nat_in_ops);
377  cleanup_nat:
378         ip_nat_cleanup();
379  cleanup_rule_init:
380         ip_nat_rule_cleanup();
381  cleanup_nothing:
382         return ret;
383 }
384
385 static int __init init(void)
386 {
387         return init_or_cleanup(1);
388 }
389
390 static void __exit fini(void)
391 {
392         init_or_cleanup(0);
393 }
394
395 module_init(init);
396 module_exit(fini);
397
398 EXPORT_SYMBOL(ip_nat_setup_info);
399 EXPORT_SYMBOL(ip_nat_protocol_register);
400 EXPORT_SYMBOL(ip_nat_protocol_unregister);
401 EXPORT_SYMBOL_GPL(ip_nat_proto_find_get);
402 EXPORT_SYMBOL_GPL(ip_nat_proto_put);
403 EXPORT_SYMBOL(ip_nat_cheat_check);
404 EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
405 EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
406 EXPORT_SYMBOL(ip_nat_used_tuple);
407 EXPORT_SYMBOL(ip_nat_follow_master);
408 MODULE_LICENSE("GPL");