2 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
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.
8 * Based on Rusty Russell's IPv4 NAT code. Development of IPv6 NAT
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>
19 #include <net/netfilter/nf_nat.h>
20 #include <net/netfilter/nf_nat_core.h>
21 #include <net/netfilter/nf_nat_l3proto.h>
23 static int __net_init ip6table_nat_table_init(struct net *net);
25 static const struct xt_table nf_nat_ipv6_table = {
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),
33 .table_init = ip6table_nat_table_init,
36 static unsigned int ip6table_nat_do_chain(void *priv,
38 const struct nf_hook_state *state)
40 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_nat);
43 static const struct nf_hook_ops nf_nat_ipv6_ops[] = {
45 .hook = ip6table_nat_do_chain,
47 .hooknum = NF_INET_PRE_ROUTING,
48 .priority = NF_IP6_PRI_NAT_DST,
51 .hook = ip6table_nat_do_chain,
53 .hooknum = NF_INET_POST_ROUTING,
54 .priority = NF_IP6_PRI_NAT_SRC,
57 .hook = ip6table_nat_do_chain,
59 .hooknum = NF_INET_LOCAL_OUT,
60 .priority = NF_IP6_PRI_NAT_DST,
63 .hook = ip6table_nat_do_chain,
65 .hooknum = NF_INET_LOCAL_IN,
66 .priority = NF_IP6_PRI_NAT_SRC,
70 static int ip6t_nat_register_lookups(struct net *net)
74 for (i = 0; i < ARRAY_SIZE(nf_nat_ipv6_ops); i++) {
75 ret = nf_nat_l3proto_ipv6_register_fn(net, &nf_nat_ipv6_ops[i]);
78 nf_nat_l3proto_ipv6_unregister_fn(net, &nf_nat_ipv6_ops[--i]);
87 static void ip6t_nat_unregister_lookups(struct net *net)
91 for (i = 0; i < ARRAY_SIZE(nf_nat_ipv6_ops); i++)
92 nf_nat_l3proto_ipv6_unregister_fn(net, &nf_nat_ipv6_ops[i]);
95 static int __net_init ip6table_nat_table_init(struct net *net)
97 struct ip6t_replace *repl;
100 if (net->ipv6.ip6table_nat)
103 repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
106 ret = ip6t_register_table(net, &nf_nat_ipv6_table, repl,
107 NULL, &net->ipv6.ip6table_nat);
113 ret = ip6t_nat_register_lookups(net);
115 ip6t_unregister_table(net, net->ipv6.ip6table_nat, NULL);
116 net->ipv6.ip6table_nat = NULL;
122 static void __net_exit ip6table_nat_net_exit(struct net *net)
124 if (!net->ipv6.ip6table_nat)
126 ip6t_nat_unregister_lookups(net);
127 ip6t_unregister_table(net, net->ipv6.ip6table_nat, NULL);
128 net->ipv6.ip6table_nat = NULL;
131 static struct pernet_operations ip6table_nat_net_ops = {
132 .exit = ip6table_nat_net_exit,
135 static int __init ip6table_nat_init(void)
137 int ret = register_pernet_subsys(&ip6table_nat_net_ops);
142 ret = ip6table_nat_table_init(&init_net);
144 unregister_pernet_subsys(&ip6table_nat_net_ops);
148 static void __exit ip6table_nat_exit(void)
150 unregister_pernet_subsys(&ip6table_nat_net_ops);
153 module_init(ip6table_nat_init);
154 module_exit(ip6table_nat_exit);
156 MODULE_LICENSE("GPL");