]>
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 | .owner = THIS_MODULE, |
74 | .pf = NFPROTO_IPV6, | |
75 | .hooknum = NF_INET_PRE_ROUTING, | |
76 | .priority = NF_IP6_PRI_NAT_DST, | |
77 | }, | |
78 | /* After packet filtering, change source */ | |
79 | { | |
2a5538e9 | 80 | .hook = ip6table_nat_out, |
58a317f1 PM |
81 | .owner = THIS_MODULE, |
82 | .pf = NFPROTO_IPV6, | |
83 | .hooknum = NF_INET_POST_ROUTING, | |
84 | .priority = NF_IP6_PRI_NAT_SRC, | |
85 | }, | |
86 | /* Before packet filtering, change destination */ | |
87 | { | |
2a5538e9 | 88 | .hook = ip6table_nat_local_fn, |
58a317f1 PM |
89 | .owner = THIS_MODULE, |
90 | .pf = NFPROTO_IPV6, | |
91 | .hooknum = NF_INET_LOCAL_OUT, | |
92 | .priority = NF_IP6_PRI_NAT_DST, | |
93 | }, | |
94 | /* After packet filtering, change source */ | |
95 | { | |
2a5538e9 | 96 | .hook = ip6table_nat_fn, |
58a317f1 PM |
97 | .owner = THIS_MODULE, |
98 | .pf = NFPROTO_IPV6, | |
99 | .hooknum = NF_INET_LOCAL_IN, | |
100 | .priority = NF_IP6_PRI_NAT_SRC, | |
101 | }, | |
102 | }; | |
103 | ||
104 | static int __net_init ip6table_nat_net_init(struct net *net) | |
105 | { | |
106 | struct ip6t_replace *repl; | |
107 | ||
108 | repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table); | |
109 | if (repl == NULL) | |
110 | return -ENOMEM; | |
111 | net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl); | |
112 | kfree(repl); | |
8c6ffba0 | 113 | return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat); |
58a317f1 PM |
114 | } |
115 | ||
116 | static void __net_exit ip6table_nat_net_exit(struct net *net) | |
117 | { | |
118 | ip6t_unregister_table(net, net->ipv6.ip6table_nat); | |
119 | } | |
120 | ||
121 | static struct pernet_operations ip6table_nat_net_ops = { | |
122 | .init = ip6table_nat_net_init, | |
123 | .exit = ip6table_nat_net_exit, | |
124 | }; | |
125 | ||
126 | static int __init ip6table_nat_init(void) | |
127 | { | |
128 | int err; | |
129 | ||
130 | err = register_pernet_subsys(&ip6table_nat_net_ops); | |
131 | if (err < 0) | |
132 | goto err1; | |
133 | ||
134 | err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops)); | |
135 | if (err < 0) | |
136 | goto err2; | |
137 | return 0; | |
138 | ||
139 | err2: | |
140 | unregister_pernet_subsys(&ip6table_nat_net_ops); | |
141 | err1: | |
142 | return err; | |
143 | } | |
144 | ||
145 | static void __exit ip6table_nat_exit(void) | |
146 | { | |
147 | nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops)); | |
148 | unregister_pernet_subsys(&ip6table_nat_net_ops); | |
149 | } | |
150 | ||
151 | module_init(ip6table_nat_init); | |
152 | module_exit(ip6table_nat_exit); | |
153 | ||
154 | MODULE_LICENSE("GPL"); |