]> asedeno.scripts.mit.edu Git - linux.git/blob - net/ipv6/netfilter/ip6table_nat.c
Merge ath-next from git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/ath.git
[linux.git] / net / ipv6 / netfilter / ip6table_nat.c
1 /*
2  * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
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 int __net_init ip6table_nat_table_init(struct net *net);
24
25 static const struct xt_table nf_nat_ipv6_table = {
26         .name           = "nat",
27         .valid_hooks    = (1 << NF_INET_PRE_ROUTING) |
28                           (1 << NF_INET_POST_ROUTING) |
29                           (1 << NF_INET_LOCAL_OUT) |
30                           (1 << NF_INET_LOCAL_IN),
31         .me             = THIS_MODULE,
32         .af             = NFPROTO_IPV6,
33         .table_init     = ip6table_nat_table_init,
34 };
35
36 static unsigned int ip6table_nat_do_chain(void *priv,
37                                           struct sk_buff *skb,
38                                           const struct nf_hook_state *state)
39 {
40         return ip6t_do_table(skb, state, state->net->ipv6.ip6table_nat);
41 }
42
43 static unsigned int ip6table_nat_fn(void *priv,
44                                     struct sk_buff *skb,
45                                     const struct nf_hook_state *state)
46 {
47         return nf_nat_ipv6_fn(priv, skb, state, ip6table_nat_do_chain);
48 }
49
50 static unsigned int ip6table_nat_in(void *priv,
51                                     struct sk_buff *skb,
52                                     const struct nf_hook_state *state)
53 {
54         return nf_nat_ipv6_in(priv, skb, state, ip6table_nat_do_chain);
55 }
56
57 static unsigned int ip6table_nat_out(void *priv,
58                                      struct sk_buff *skb,
59                                      const struct nf_hook_state *state)
60 {
61         return nf_nat_ipv6_out(priv, skb, state, ip6table_nat_do_chain);
62 }
63
64 static unsigned int ip6table_nat_local_fn(void *priv,
65                                           struct sk_buff *skb,
66                                           const struct nf_hook_state *state)
67 {
68         return nf_nat_ipv6_local_fn(priv, skb, state, ip6table_nat_do_chain);
69 }
70
71 static const struct nf_hook_ops nf_nat_ipv6_ops[] = {
72         /* Before packet filtering, change destination */
73         {
74                 .hook           = ip6table_nat_in,
75                 .pf             = NFPROTO_IPV6,
76                 .nat_hook       = true,
77                 .hooknum        = NF_INET_PRE_ROUTING,
78                 .priority       = NF_IP6_PRI_NAT_DST,
79         },
80         /* After packet filtering, change source */
81         {
82                 .hook           = ip6table_nat_out,
83                 .pf             = NFPROTO_IPV6,
84                 .nat_hook       = true,
85                 .hooknum        = NF_INET_POST_ROUTING,
86                 .priority       = NF_IP6_PRI_NAT_SRC,
87         },
88         /* Before packet filtering, change destination */
89         {
90                 .hook           = ip6table_nat_local_fn,
91                 .pf             = NFPROTO_IPV6,
92                 .nat_hook       = true,
93                 .hooknum        = NF_INET_LOCAL_OUT,
94                 .priority       = NF_IP6_PRI_NAT_DST,
95         },
96         /* After packet filtering, change source */
97         {
98                 .hook           = ip6table_nat_fn,
99                 .nat_hook       = true,
100                 .pf             = NFPROTO_IPV6,
101                 .hooknum        = NF_INET_LOCAL_IN,
102                 .priority       = NF_IP6_PRI_NAT_SRC,
103         },
104 };
105
106 static int __net_init ip6table_nat_table_init(struct net *net)
107 {
108         struct ip6t_replace *repl;
109         int ret;
110
111         if (net->ipv6.ip6table_nat)
112                 return 0;
113
114         repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
115         if (repl == NULL)
116                 return -ENOMEM;
117         ret = ip6t_register_table(net, &nf_nat_ipv6_table, repl,
118                                   nf_nat_ipv6_ops, &net->ipv6.ip6table_nat);
119         kfree(repl);
120         return ret;
121 }
122
123 static void __net_exit ip6table_nat_net_exit(struct net *net)
124 {
125         if (!net->ipv6.ip6table_nat)
126                 return;
127         ip6t_unregister_table(net, net->ipv6.ip6table_nat, nf_nat_ipv6_ops);
128         net->ipv6.ip6table_nat = NULL;
129 }
130
131 static struct pernet_operations ip6table_nat_net_ops = {
132         .exit   = ip6table_nat_net_exit,
133 };
134
135 static int __init ip6table_nat_init(void)
136 {
137         int ret = register_pernet_subsys(&ip6table_nat_net_ops);
138
139         if (ret)
140                 return ret;
141
142         ret = ip6table_nat_table_init(&init_net);
143         if (ret)
144                 unregister_pernet_subsys(&ip6table_nat_net_ops);
145         return ret;
146 }
147
148 static void __exit ip6table_nat_exit(void)
149 {
150         unregister_pernet_subsys(&ip6table_nat_net_ops);
151 }
152
153 module_init(ip6table_nat_init);
154 module_exit(ip6table_nat_exit);
155
156 MODULE_LICENSE("GPL");