Commit | Line | Data |
---|---|---|
58a317f1 PM |
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 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 | ||
2a5538e9 PNA |
33 | static unsigned int ip6table_nat_do_chain(const struct nf_hook_ops *ops, |
34 | struct sk_buff *skb, | |
8fe22382 | 35 | const struct nf_hook_state *state, |
2a5538e9 | 36 | struct nf_conn *ct) |
58a317f1 | 37 | { |
9dff2c96 EB |
38 | return ip6t_do_table(skb, ops->hooknum, state, |
39 | state->net->ipv6.ip6table_nat); | |
58a317f1 PM |
40 | } |
41 | ||
2a5538e9 PNA |
42 | static unsigned int ip6table_nat_fn(const struct nf_hook_ops *ops, |
43 | struct sk_buff *skb, | |
238e54c9 | 44 | const struct nf_hook_state *state) |
58a317f1 | 45 | { |
8fe22382 | 46 | return nf_nat_ipv6_fn(ops, skb, state, ip6table_nat_do_chain); |
58a317f1 PM |
47 | } |
48 | ||
2a5538e9 PNA |
49 | static unsigned int ip6table_nat_in(const struct nf_hook_ops *ops, |
50 | struct sk_buff *skb, | |
238e54c9 | 51 | const struct nf_hook_state *state) |
58a317f1 | 52 | { |
8fe22382 | 53 | return nf_nat_ipv6_in(ops, skb, state, ip6table_nat_do_chain); |
58a317f1 PM |
54 | } |
55 | ||
2a5538e9 PNA |
56 | static unsigned int ip6table_nat_out(const struct nf_hook_ops *ops, |
57 | struct sk_buff *skb, | |
238e54c9 | 58 | const struct nf_hook_state *state) |
58a317f1 | 59 | { |
8fe22382 | 60 | return nf_nat_ipv6_out(ops, skb, state, ip6table_nat_do_chain); |
58a317f1 PM |
61 | } |
62 | ||
2a5538e9 PNA |
63 | static unsigned int ip6table_nat_local_fn(const struct nf_hook_ops *ops, |
64 | struct sk_buff *skb, | |
238e54c9 | 65 | const struct nf_hook_state *state) |
58a317f1 | 66 | { |
8fe22382 | 67 | return nf_nat_ipv6_local_fn(ops, skb, state, ip6table_nat_do_chain); |
58a317f1 PM |
68 | } |
69 | ||
70 | static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = { | |
71 | /* Before packet filtering, change destination */ | |
72 | { | |
2a5538e9 | 73 | .hook = ip6table_nat_in, |
58a317f1 PM |
74 | .owner = THIS_MODULE, |
75 | .pf = NFPROTO_IPV6, | |
76 | .hooknum = NF_INET_PRE_ROUTING, | |
77 | .priority = NF_IP6_PRI_NAT_DST, | |
78 | }, | |
79 | /* After packet filtering, change source */ | |
80 | { | |
2a5538e9 | 81 | .hook = ip6table_nat_out, |
58a317f1 PM |
82 | .owner = THIS_MODULE, |
83 | .pf = NFPROTO_IPV6, | |
84 | .hooknum = NF_INET_POST_ROUTING, | |
85 | .priority = NF_IP6_PRI_NAT_SRC, | |
86 | }, | |
87 | /* Before packet filtering, change destination */ | |
88 | { | |
2a5538e9 | 89 | .hook = ip6table_nat_local_fn, |
58a317f1 PM |
90 | .owner = THIS_MODULE, |
91 | .pf = NFPROTO_IPV6, | |
92 | .hooknum = NF_INET_LOCAL_OUT, | |
93 | .priority = NF_IP6_PRI_NAT_DST, | |
94 | }, | |
95 | /* After packet filtering, change source */ | |
96 | { | |
2a5538e9 | 97 | .hook = ip6table_nat_fn, |
58a317f1 PM |
98 | .owner = THIS_MODULE, |
99 | .pf = NFPROTO_IPV6, | |
100 | .hooknum = NF_INET_LOCAL_IN, | |
101 | .priority = NF_IP6_PRI_NAT_SRC, | |
102 | }, | |
103 | }; | |
104 | ||
105 | static int __net_init ip6table_nat_net_init(struct net *net) | |
106 | { | |
107 | struct ip6t_replace *repl; | |
108 | ||
109 | repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table); | |
110 | if (repl == NULL) | |
111 | return -ENOMEM; | |
112 | net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl); | |
113 | kfree(repl); | |
8c6ffba0 | 114 | return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat); |
58a317f1 PM |
115 | } |
116 | ||
117 | static void __net_exit ip6table_nat_net_exit(struct net *net) | |
118 | { | |
119 | ip6t_unregister_table(net, net->ipv6.ip6table_nat); | |
120 | } | |
121 | ||
122 | static struct pernet_operations ip6table_nat_net_ops = { | |
123 | .init = ip6table_nat_net_init, | |
124 | .exit = ip6table_nat_net_exit, | |
125 | }; | |
126 | ||
127 | static int __init ip6table_nat_init(void) | |
128 | { | |
129 | int err; | |
130 | ||
131 | err = register_pernet_subsys(&ip6table_nat_net_ops); | |
132 | if (err < 0) | |
133 | goto err1; | |
134 | ||
135 | err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops)); | |
136 | if (err < 0) | |
137 | goto err2; | |
138 | return 0; | |
139 | ||
140 | err2: | |
141 | unregister_pernet_subsys(&ip6table_nat_net_ops); | |
142 | err1: | |
143 | return err; | |
144 | } | |
145 | ||
146 | static void __exit ip6table_nat_exit(void) | |
147 | { | |
148 | nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops)); | |
149 | unregister_pernet_subsys(&ip6table_nat_net_ops); | |
150 | } | |
151 | ||
152 | module_init(ip6table_nat_init); | |
153 | module_exit(ip6table_nat_exit); | |
154 | ||
155 | MODULE_LICENSE("GPL"); |