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 const struct xt_table nf_nat_ipv6_table = {
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),
33 static unsigned int ip6table_nat_do_chain(void *priv,
35 const struct nf_hook_state *state,
38 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_nat);
41 static unsigned int ip6table_nat_fn(void *priv,
43 const struct nf_hook_state *state)
45 return nf_nat_ipv6_fn(priv, skb, state, ip6table_nat_do_chain);
48 static unsigned int ip6table_nat_in(void *priv,
50 const struct nf_hook_state *state)
52 return nf_nat_ipv6_in(priv, skb, state, ip6table_nat_do_chain);
55 static unsigned int ip6table_nat_out(void *priv,
57 const struct nf_hook_state *state)
59 return nf_nat_ipv6_out(priv, skb, state, ip6table_nat_do_chain);
62 static unsigned int ip6table_nat_local_fn(void *priv,
64 const struct nf_hook_state *state)
66 return nf_nat_ipv6_local_fn(priv, skb, state, ip6table_nat_do_chain);
69 static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = {
70 /* Before packet filtering, change destination */
72 .hook = ip6table_nat_in,
74 .hooknum = NF_INET_PRE_ROUTING,
75 .priority = NF_IP6_PRI_NAT_DST,
77 /* After packet filtering, change source */
79 .hook = ip6table_nat_out,
81 .hooknum = NF_INET_POST_ROUTING,
82 .priority = NF_IP6_PRI_NAT_SRC,
84 /* Before packet filtering, change destination */
86 .hook = ip6table_nat_local_fn,
88 .hooknum = NF_INET_LOCAL_OUT,
89 .priority = NF_IP6_PRI_NAT_DST,
91 /* After packet filtering, change source */
93 .hook = ip6table_nat_fn,
95 .hooknum = NF_INET_LOCAL_IN,
96 .priority = NF_IP6_PRI_NAT_SRC,
100 static int __net_init ip6table_nat_net_init(struct net *net)
102 struct ip6t_replace *repl;
104 repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
107 net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl);
109 return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat);
112 static void __net_exit ip6table_nat_net_exit(struct net *net)
114 ip6t_unregister_table(net, net->ipv6.ip6table_nat);
117 static struct pernet_operations ip6table_nat_net_ops = {
118 .init = ip6table_nat_net_init,
119 .exit = ip6table_nat_net_exit,
122 static int __init ip6table_nat_init(void)
126 err = register_pernet_subsys(&ip6table_nat_net_ops);
130 err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
136 unregister_pernet_subsys(&ip6table_nat_net_ops);
141 static void __exit ip6table_nat_exit(void)
143 nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
144 unregister_pernet_subsys(&ip6table_nat_net_ops);
147 module_init(ip6table_nat_init);
148 module_exit(ip6table_nat_exit);
150 MODULE_LICENSE("GPL");