Merge branch 'kbuild/rc-fixes' into kbuild/kconfig
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / netfilter / iptable_nat.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2011 Patrick McHardy <kaber@trash.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter_ipv4.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
14 #include <linux/ip.h>
15 #include <net/ip.h>
16
17 #include <net/netfilter/nf_nat.h>
18 #include <net/netfilter/nf_nat_core.h>
19 #include <net/netfilter/nf_nat_l3proto.h>
20
21 static const struct xt_table nf_nat_ipv4_table = {
22         .name           = "nat",
23         .valid_hooks    = (1 << NF_INET_PRE_ROUTING) |
24                           (1 << NF_INET_POST_ROUTING) |
25                           (1 << NF_INET_LOCAL_OUT) |
26                           (1 << NF_INET_LOCAL_IN),
27         .me             = THIS_MODULE,
28         .af             = NFPROTO_IPV4,
29 };
30
31 static unsigned int alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
32 {
33         /* Force range to this IP; let proto decide mapping for
34          * per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
35          */
36         struct nf_nat_range range;
37
38         range.flags = 0;
39         pr_debug("Allocating NULL binding for %p (%pI4)\n", ct,
40                  HOOK2MANIP(hooknum) == NF_NAT_MANIP_SRC ?
41                  &ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip :
42                  &ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
43
44         return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
45 }
46
47 static unsigned int nf_nat_rule_find(struct sk_buff *skb, unsigned int hooknum,
48                                      const struct net_device *in,
49                                      const struct net_device *out,
50                                      struct nf_conn *ct)
51 {
52         struct net *net = nf_ct_net(ct);
53         unsigned int ret;
54
55         ret = ipt_do_table(skb, hooknum, in, out, net->ipv4.nat_table);
56         if (ret == NF_ACCEPT) {
57                 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
58                         ret = alloc_null_binding(ct, hooknum);
59         }
60         return ret;
61 }
62
63 static unsigned int
64 nf_nat_ipv4_fn(unsigned int hooknum,
65                struct sk_buff *skb,
66                const struct net_device *in,
67                const struct net_device *out,
68                int (*okfn)(struct sk_buff *))
69 {
70         struct nf_conn *ct;
71         enum ip_conntrack_info ctinfo;
72         struct nf_conn_nat *nat;
73         /* maniptype == SRC for postrouting. */
74         enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum);
75
76         /* We never see fragments: conntrack defrags on pre-routing
77          * and local-out, and nf_nat_out protects post-routing.
78          */
79         NF_CT_ASSERT(!ip_is_fragment(ip_hdr(skb)));
80
81         ct = nf_ct_get(skb, &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          */
87         if (!ct)
88                 return NF_ACCEPT;
89
90         /* Don't try to NAT if this packet is not conntracked */
91         if (nf_ct_is_untracked(ct))
92                 return NF_ACCEPT;
93
94         nat = nfct_nat(ct);
95         if (!nat) {
96                 /* NAT module was loaded late. */
97                 if (nf_ct_is_confirmed(ct))
98                         return NF_ACCEPT;
99                 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
100                 if (nat == NULL) {
101                         pr_debug("failed to add NAT extension\n");
102                         return NF_ACCEPT;
103                 }
104         }
105
106         switch (ctinfo) {
107         case IP_CT_RELATED:
108         case IP_CT_RELATED_REPLY:
109                 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
110                         if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
111                                                            hooknum))
112                                 return NF_DROP;
113                         else
114                                 return NF_ACCEPT;
115                 }
116                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
117         case IP_CT_NEW:
118                 /* Seen it before?  This can happen for loopback, retrans,
119                  * or local packets.
120                  */
121                 if (!nf_nat_initialized(ct, maniptype)) {
122                         unsigned int ret;
123
124                         ret = nf_nat_rule_find(skb, hooknum, in, out, ct);
125                         if (ret != NF_ACCEPT)
126                                 return ret;
127                 } else
128                         pr_debug("Already setup manip %s for ct %p\n",
129                                  maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
130                                  ct);
131                 break;
132
133         default:
134                 /* ESTABLISHED */
135                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
136                              ctinfo == IP_CT_ESTABLISHED_REPLY);
137                 if (nf_nat_oif_changed(hooknum, ctinfo, nat, out)) {
138                         nf_ct_kill_acct(ct, ctinfo, skb);
139                         return NF_DROP;
140                 }
141         }
142
143         return nf_nat_packet(ct, ctinfo, hooknum, skb);
144 }
145
146 static unsigned int
147 nf_nat_ipv4_in(unsigned int hooknum,
148                struct sk_buff *skb,
149                const struct net_device *in,
150                const struct net_device *out,
151                int (*okfn)(struct sk_buff *))
152 {
153         unsigned int ret;
154         __be32 daddr = ip_hdr(skb)->daddr;
155
156         ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn);
157         if (ret != NF_DROP && ret != NF_STOLEN &&
158             daddr != ip_hdr(skb)->daddr)
159                 skb_dst_drop(skb);
160
161         return ret;
162 }
163
164 static unsigned int
165 nf_nat_ipv4_out(unsigned int hooknum,
166                 struct sk_buff *skb,
167                 const struct net_device *in,
168                 const struct net_device *out,
169                 int (*okfn)(struct sk_buff *))
170 {
171 #ifdef CONFIG_XFRM
172         const struct nf_conn *ct;
173         enum ip_conntrack_info ctinfo;
174 #endif
175         unsigned int ret;
176
177         /* root is playing with raw sockets. */
178         if (skb->len < sizeof(struct iphdr) ||
179             ip_hdrlen(skb) < sizeof(struct iphdr))
180                 return NF_ACCEPT;
181
182         ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn);
183 #ifdef CONFIG_XFRM
184         if (ret != NF_DROP && ret != NF_STOLEN &&
185             !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
186             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
187                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
188
189                 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
190                      ct->tuplehash[!dir].tuple.dst.u3.ip) ||
191                     (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
192                      ct->tuplehash[dir].tuple.src.u.all !=
193                      ct->tuplehash[!dir].tuple.dst.u.all))
194                         if (nf_xfrm_me_harder(skb, AF_INET) < 0)
195                                 ret = NF_DROP;
196         }
197 #endif
198         return ret;
199 }
200
201 static unsigned int
202 nf_nat_ipv4_local_fn(unsigned int hooknum,
203                      struct sk_buff *skb,
204                      const struct net_device *in,
205                      const struct net_device *out,
206                      int (*okfn)(struct sk_buff *))
207 {
208         const struct nf_conn *ct;
209         enum ip_conntrack_info ctinfo;
210         unsigned int ret;
211
212         /* root is playing with raw sockets. */
213         if (skb->len < sizeof(struct iphdr) ||
214             ip_hdrlen(skb) < sizeof(struct iphdr))
215                 return NF_ACCEPT;
216
217         ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn);
218         if (ret != NF_DROP && ret != NF_STOLEN &&
219             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
220                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
221
222                 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
223                     ct->tuplehash[!dir].tuple.src.u3.ip) {
224                         if (ip_route_me_harder(skb, RTN_UNSPEC))
225                                 ret = NF_DROP;
226                 }
227 #ifdef CONFIG_XFRM
228                 else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
229                          ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
230                          ct->tuplehash[dir].tuple.dst.u.all !=
231                          ct->tuplehash[!dir].tuple.src.u.all)
232                         if (nf_xfrm_me_harder(skb, AF_INET) < 0)
233                                 ret = NF_DROP;
234 #endif
235         }
236         return ret;
237 }
238
239 static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = {
240         /* Before packet filtering, change destination */
241         {
242                 .hook           = nf_nat_ipv4_in,
243                 .owner          = THIS_MODULE,
244                 .pf             = NFPROTO_IPV4,
245                 .hooknum        = NF_INET_PRE_ROUTING,
246                 .priority       = NF_IP_PRI_NAT_DST,
247         },
248         /* After packet filtering, change source */
249         {
250                 .hook           = nf_nat_ipv4_out,
251                 .owner          = THIS_MODULE,
252                 .pf             = NFPROTO_IPV4,
253                 .hooknum        = NF_INET_POST_ROUTING,
254                 .priority       = NF_IP_PRI_NAT_SRC,
255         },
256         /* Before packet filtering, change destination */
257         {
258                 .hook           = nf_nat_ipv4_local_fn,
259                 .owner          = THIS_MODULE,
260                 .pf             = NFPROTO_IPV4,
261                 .hooknum        = NF_INET_LOCAL_OUT,
262                 .priority       = NF_IP_PRI_NAT_DST,
263         },
264         /* After packet filtering, change source */
265         {
266                 .hook           = nf_nat_ipv4_fn,
267                 .owner          = THIS_MODULE,
268                 .pf             = NFPROTO_IPV4,
269                 .hooknum        = NF_INET_LOCAL_IN,
270                 .priority       = NF_IP_PRI_NAT_SRC,
271         },
272 };
273
274 static int __net_init iptable_nat_net_init(struct net *net)
275 {
276         struct ipt_replace *repl;
277
278         repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
279         if (repl == NULL)
280                 return -ENOMEM;
281         net->ipv4.nat_table = ipt_register_table(net, &nf_nat_ipv4_table, repl);
282         kfree(repl);
283         return PTR_RET(net->ipv4.nat_table);
284 }
285
286 static void __net_exit iptable_nat_net_exit(struct net *net)
287 {
288         ipt_unregister_table(net, net->ipv4.nat_table);
289 }
290
291 static struct pernet_operations iptable_nat_net_ops = {
292         .init   = iptable_nat_net_init,
293         .exit   = iptable_nat_net_exit,
294 };
295
296 static int __init iptable_nat_init(void)
297 {
298         int err;
299
300         err = register_pernet_subsys(&iptable_nat_net_ops);
301         if (err < 0)
302                 goto err1;
303
304         err = nf_register_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops));
305         if (err < 0)
306                 goto err2;
307         return 0;
308
309 err2:
310         unregister_pernet_subsys(&iptable_nat_net_ops);
311 err1:
312         return err;
313 }
314
315 static void __exit iptable_nat_exit(void)
316 {
317         nf_unregister_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops));
318         unregister_pernet_subsys(&iptable_nat_net_ops);
319 }
320
321 module_init(iptable_nat_init);
322 module_exit(iptable_nat_exit);
323
324 MODULE_LICENSE("GPL");