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