1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
5 * Based on Rusty Russell's IPv4 NAT code. Development of IPv6 NAT
9 #include <linux/module.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter_ipv6.h>
12 #include <linux/netfilter_ipv6/ip6_tables.h>
13 #include <linux/ipv6.h>
16 #include <net/netfilter/nf_nat.h>
18 static int __net_init ip6table_nat_table_init(struct net *net);
20 static const struct xt_table nf_nat_ipv6_table = {
22 .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
23 (1 << NF_INET_POST_ROUTING) |
24 (1 << NF_INET_LOCAL_OUT) |
25 (1 << NF_INET_LOCAL_IN),
28 .table_init = ip6table_nat_table_init,
31 static unsigned int ip6table_nat_do_chain(void *priv,
33 const struct nf_hook_state *state)
35 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_nat);
38 static const struct nf_hook_ops nf_nat_ipv6_ops[] = {
40 .hook = ip6table_nat_do_chain,
42 .hooknum = NF_INET_PRE_ROUTING,
43 .priority = NF_IP6_PRI_NAT_DST,
46 .hook = ip6table_nat_do_chain,
48 .hooknum = NF_INET_POST_ROUTING,
49 .priority = NF_IP6_PRI_NAT_SRC,
52 .hook = ip6table_nat_do_chain,
54 .hooknum = NF_INET_LOCAL_OUT,
55 .priority = NF_IP6_PRI_NAT_DST,
58 .hook = ip6table_nat_do_chain,
60 .hooknum = NF_INET_LOCAL_IN,
61 .priority = NF_IP6_PRI_NAT_SRC,
65 static int ip6t_nat_register_lookups(struct net *net)
69 for (i = 0; i < ARRAY_SIZE(nf_nat_ipv6_ops); i++) {
70 ret = nf_nat_ipv6_register_fn(net, &nf_nat_ipv6_ops[i]);
73 nf_nat_ipv6_unregister_fn(net, &nf_nat_ipv6_ops[--i]);
82 static void ip6t_nat_unregister_lookups(struct net *net)
86 for (i = 0; i < ARRAY_SIZE(nf_nat_ipv6_ops); i++)
87 nf_nat_ipv6_unregister_fn(net, &nf_nat_ipv6_ops[i]);
90 static int __net_init ip6table_nat_table_init(struct net *net)
92 struct ip6t_replace *repl;
95 if (net->ipv6.ip6table_nat)
98 repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
101 ret = ip6t_register_table(net, &nf_nat_ipv6_table, repl,
102 NULL, &net->ipv6.ip6table_nat);
108 ret = ip6t_nat_register_lookups(net);
110 ip6t_unregister_table(net, net->ipv6.ip6table_nat, NULL);
111 net->ipv6.ip6table_nat = NULL;
117 static void __net_exit ip6table_nat_net_pre_exit(struct net *net)
119 if (net->ipv6.ip6table_nat)
120 ip6t_nat_unregister_lookups(net);
123 static void __net_exit ip6table_nat_net_exit(struct net *net)
125 if (!net->ipv6.ip6table_nat)
127 ip6t_unregister_table_exit(net, net->ipv6.ip6table_nat);
128 net->ipv6.ip6table_nat = NULL;
131 static struct pernet_operations ip6table_nat_net_ops = {
132 .pre_exit = ip6table_nat_net_pre_exit,
133 .exit = ip6table_nat_net_exit,
136 static int __init ip6table_nat_init(void)
138 int ret = register_pernet_subsys(&ip6table_nat_net_ops);
143 ret = ip6table_nat_table_init(&init_net);
145 unregister_pernet_subsys(&ip6table_nat_net_ops);
149 static void __exit ip6table_nat_exit(void)
151 unregister_pernet_subsys(&ip6table_nat_net_ops);
154 module_init(ip6table_nat_init);
155 module_exit(ip6table_nat_exit);
157 MODULE_LICENSE("GPL");