]> Git Repo - linux.git/blob - net/ipv6/netfilter/ip6table_nat.c
selinux: Remove security_ops extern
[linux.git] / net / ipv6 / netfilter / ip6table_nat.c
1 /*
2  * Copyright (c) 2011 Patrick McHardy <[email protected]>
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  * Based on Rusty Russell's IPv4 NAT code. Development of IPv6 NAT
9  * funded by Astaro.
10  */
11
12 #include <linux/module.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter_ipv6.h>
15 #include <linux/netfilter_ipv6/ip6_tables.h>
16 #include <linux/ipv6.h>
17 #include <net/ipv6.h>
18
19 #include <net/netfilter/nf_nat.h>
20 #include <net/netfilter/nf_nat_core.h>
21 #include <net/netfilter/nf_nat_l3proto.h>
22
23 static const struct xt_table nf_nat_ipv6_table = {
24         .name           = "nat",
25         .valid_hooks    = (1 << NF_INET_PRE_ROUTING) |
26                           (1 << NF_INET_POST_ROUTING) |
27                           (1 << NF_INET_LOCAL_OUT) |
28                           (1 << NF_INET_LOCAL_IN),
29         .me             = THIS_MODULE,
30         .af             = NFPROTO_IPV6,
31 };
32
33 static unsigned int alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
34 {
35         /* Force range to this IP; let proto decide mapping for
36          * per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
37          */
38         struct nf_nat_range range;
39
40         range.flags = 0;
41         pr_debug("Allocating NULL binding for %p (%pI6)\n", ct,
42                  HOOK2MANIP(hooknum) == NF_NAT_MANIP_SRC ?
43                  &ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip6 :
44                  &ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip6);
45
46         return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
47 }
48
49 static unsigned int nf_nat_rule_find(struct sk_buff *skb, unsigned int hooknum,
50                                      const struct net_device *in,
51                                      const struct net_device *out,
52                                      struct nf_conn *ct)
53 {
54         struct net *net = nf_ct_net(ct);
55         unsigned int ret;
56
57         ret = ip6t_do_table(skb, hooknum, in, out, net->ipv6.ip6table_nat);
58         if (ret == NF_ACCEPT) {
59                 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
60                         ret = alloc_null_binding(ct, hooknum);
61         }
62         return ret;
63 }
64
65 static unsigned int
66 nf_nat_ipv6_fn(const struct nf_hook_ops *ops,
67                struct sk_buff *skb,
68                const struct net_device *in,
69                const struct net_device *out,
70                int (*okfn)(struct sk_buff *))
71 {
72         struct nf_conn *ct;
73         enum ip_conntrack_info ctinfo;
74         struct nf_conn_nat *nat;
75         enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
76         __be16 frag_off;
77         int hdrlen;
78         u8 nexthdr;
79
80         ct = nf_ct_get(skb, &ctinfo);
81         /* Can't track?  It's not due to stress, or conntrack would
82          * have dropped it.  Hence it's the user's responsibilty to
83          * packet filter it out, or implement conntrack/NAT for that
84          * protocol. 8) --RR
85          */
86         if (!ct)
87                 return NF_ACCEPT;
88
89         /* Don't try to NAT if this packet is not conntracked */
90         if (nf_ct_is_untracked(ct))
91                 return NF_ACCEPT;
92
93         nat = nf_ct_nat_ext_add(ct);
94         if (nat == NULL)
95                 return NF_ACCEPT;
96
97         switch (ctinfo) {
98         case IP_CT_RELATED:
99         case IP_CT_RELATED_REPLY:
100                 nexthdr = ipv6_hdr(skb)->nexthdr;
101                 hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
102                                           &nexthdr, &frag_off);
103
104                 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
105                         if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
106                                                              ops->hooknum,
107                                                              hdrlen))
108                                 return NF_DROP;
109                         else
110                                 return NF_ACCEPT;
111                 }
112                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
113         case IP_CT_NEW:
114                 /* Seen it before?  This can happen for loopback, retrans,
115                  * or local packets.
116                  */
117                 if (!nf_nat_initialized(ct, maniptype)) {
118                         unsigned int ret;
119
120                         ret = nf_nat_rule_find(skb, ops->hooknum, in, out, ct);
121                         if (ret != NF_ACCEPT)
122                                 return ret;
123                 } else {
124                         pr_debug("Already setup manip %s for ct %p\n",
125                                  maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
126                                  ct);
127                         if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
128                                 goto oif_changed;
129                 }
130                 break;
131
132         default:
133                 /* ESTABLISHED */
134                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
135                              ctinfo == IP_CT_ESTABLISHED_REPLY);
136                 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
137                         goto oif_changed;
138         }
139
140         return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
141
142 oif_changed:
143         nf_ct_kill_acct(ct, ctinfo, skb);
144         return NF_DROP;
145 }
146
147 static unsigned int
148 nf_nat_ipv6_in(const struct nf_hook_ops *ops,
149                struct sk_buff *skb,
150                const struct net_device *in,
151                const struct net_device *out,
152                int (*okfn)(struct sk_buff *))
153 {
154         unsigned int ret;
155         struct in6_addr daddr = ipv6_hdr(skb)->daddr;
156
157         ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
158         if (ret != NF_DROP && ret != NF_STOLEN &&
159             ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
160                 skb_dst_drop(skb);
161
162         return ret;
163 }
164
165 static unsigned int
166 nf_nat_ipv6_out(const struct nf_hook_ops *ops,
167                 struct sk_buff *skb,
168                 const struct net_device *in,
169                 const struct net_device *out,
170                 int (*okfn)(struct sk_buff *))
171 {
172 #ifdef CONFIG_XFRM
173         const struct nf_conn *ct;
174         enum ip_conntrack_info ctinfo;
175         int err;
176 #endif
177         unsigned int ret;
178
179         /* root is playing with raw sockets. */
180         if (skb->len < sizeof(struct ipv6hdr))
181                 return NF_ACCEPT;
182
183         ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
184 #ifdef CONFIG_XFRM
185         if (ret != NF_DROP && ret != NF_STOLEN &&
186             !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
187             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
188                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
189
190                 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
191                                       &ct->tuplehash[!dir].tuple.dst.u3) ||
192                     (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
193                      ct->tuplehash[dir].tuple.src.u.all !=
194                      ct->tuplehash[!dir].tuple.dst.u.all)) {
195                         err = nf_xfrm_me_harder(skb, AF_INET6);
196                         if (err < 0)
197                                 ret = NF_DROP_ERR(err);
198                 }
199         }
200 #endif
201         return ret;
202 }
203
204 static unsigned int
205 nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops,
206                      struct sk_buff *skb,
207                      const struct net_device *in,
208                      const struct net_device *out,
209                      int (*okfn)(struct sk_buff *))
210 {
211         const struct nf_conn *ct;
212         enum ip_conntrack_info ctinfo;
213         unsigned int ret;
214         int err;
215
216         /* root is playing with raw sockets. */
217         if (skb->len < sizeof(struct ipv6hdr))
218                 return NF_ACCEPT;
219
220         ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
221         if (ret != NF_DROP && ret != NF_STOLEN &&
222             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
223                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
224
225                 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
226                                       &ct->tuplehash[!dir].tuple.src.u3)) {
227                         err = ip6_route_me_harder(skb);
228                         if (err < 0)
229                                 ret = NF_DROP_ERR(err);
230                 }
231 #ifdef CONFIG_XFRM
232                 else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
233                          ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
234                          ct->tuplehash[dir].tuple.dst.u.all !=
235                          ct->tuplehash[!dir].tuple.src.u.all) {
236                         err = nf_xfrm_me_harder(skb, AF_INET6);
237                         if (err < 0)
238                                 ret = NF_DROP_ERR(err);
239                 }
240 #endif
241         }
242         return ret;
243 }
244
245 static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = {
246         /* Before packet filtering, change destination */
247         {
248                 .hook           = nf_nat_ipv6_in,
249                 .owner          = THIS_MODULE,
250                 .pf             = NFPROTO_IPV6,
251                 .hooknum        = NF_INET_PRE_ROUTING,
252                 .priority       = NF_IP6_PRI_NAT_DST,
253         },
254         /* After packet filtering, change source */
255         {
256                 .hook           = nf_nat_ipv6_out,
257                 .owner          = THIS_MODULE,
258                 .pf             = NFPROTO_IPV6,
259                 .hooknum        = NF_INET_POST_ROUTING,
260                 .priority       = NF_IP6_PRI_NAT_SRC,
261         },
262         /* Before packet filtering, change destination */
263         {
264                 .hook           = nf_nat_ipv6_local_fn,
265                 .owner          = THIS_MODULE,
266                 .pf             = NFPROTO_IPV6,
267                 .hooknum        = NF_INET_LOCAL_OUT,
268                 .priority       = NF_IP6_PRI_NAT_DST,
269         },
270         /* After packet filtering, change source */
271         {
272                 .hook           = nf_nat_ipv6_fn,
273                 .owner          = THIS_MODULE,
274                 .pf             = NFPROTO_IPV6,
275                 .hooknum        = NF_INET_LOCAL_IN,
276                 .priority       = NF_IP6_PRI_NAT_SRC,
277         },
278 };
279
280 static int __net_init ip6table_nat_net_init(struct net *net)
281 {
282         struct ip6t_replace *repl;
283
284         repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
285         if (repl == NULL)
286                 return -ENOMEM;
287         net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl);
288         kfree(repl);
289         return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat);
290 }
291
292 static void __net_exit ip6table_nat_net_exit(struct net *net)
293 {
294         ip6t_unregister_table(net, net->ipv6.ip6table_nat);
295 }
296
297 static struct pernet_operations ip6table_nat_net_ops = {
298         .init   = ip6table_nat_net_init,
299         .exit   = ip6table_nat_net_exit,
300 };
301
302 static int __init ip6table_nat_init(void)
303 {
304         int err;
305
306         err = register_pernet_subsys(&ip6table_nat_net_ops);
307         if (err < 0)
308                 goto err1;
309
310         err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
311         if (err < 0)
312                 goto err2;
313         return 0;
314
315 err2:
316         unregister_pernet_subsys(&ip6table_nat_net_ops);
317 err1:
318         return err;
319 }
320
321 static void __exit ip6table_nat_exit(void)
322 {
323         nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
324         unregister_pernet_subsys(&ip6table_nat_net_ops);
325 }
326
327 module_init(ip6table_nat_init);
328 module_exit(ip6table_nat_exit);
329
330 MODULE_LICENSE("GPL");
This page took 0.050539 seconds and 4 git commands to generate.