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(const struct nf_hook_ops
*ops
,
35 const struct nf_hook_state
*state
,
38 return ip6t_do_table(skb
, ops
->hooknum
, state
,
39 state
->net
->ipv6
.ip6table_nat
);
42 static unsigned int ip6table_nat_fn(const struct nf_hook_ops
*ops
,
44 const struct nf_hook_state
*state
)
46 return nf_nat_ipv6_fn(ops
, skb
, state
, ip6table_nat_do_chain
);
49 static unsigned int ip6table_nat_in(const struct nf_hook_ops
*ops
,
51 const struct nf_hook_state
*state
)
53 return nf_nat_ipv6_in(ops
, skb
, state
, ip6table_nat_do_chain
);
56 static unsigned int ip6table_nat_out(const struct nf_hook_ops
*ops
,
58 const struct nf_hook_state
*state
)
60 return nf_nat_ipv6_out(ops
, skb
, state
, ip6table_nat_do_chain
);
63 static unsigned int ip6table_nat_local_fn(const struct nf_hook_ops
*ops
,
65 const struct nf_hook_state
*state
)
67 return nf_nat_ipv6_local_fn(ops
, skb
, state
, ip6table_nat_do_chain
);
70 static struct nf_hook_ops nf_nat_ipv6_ops
[] __read_mostly
= {
71 /* Before packet filtering, change destination */
73 .hook
= ip6table_nat_in
,
76 .hooknum
= NF_INET_PRE_ROUTING
,
77 .priority
= NF_IP6_PRI_NAT_DST
,
79 /* After packet filtering, change source */
81 .hook
= ip6table_nat_out
,
84 .hooknum
= NF_INET_POST_ROUTING
,
85 .priority
= NF_IP6_PRI_NAT_SRC
,
87 /* Before packet filtering, change destination */
89 .hook
= ip6table_nat_local_fn
,
92 .hooknum
= NF_INET_LOCAL_OUT
,
93 .priority
= NF_IP6_PRI_NAT_DST
,
95 /* After packet filtering, change source */
97 .hook
= ip6table_nat_fn
,
100 .hooknum
= NF_INET_LOCAL_IN
,
101 .priority
= NF_IP6_PRI_NAT_SRC
,
105 static int __net_init
ip6table_nat_net_init(struct net
*net
)
107 struct ip6t_replace
*repl
;
109 repl
= ip6t_alloc_initial_table(&nf_nat_ipv6_table
);
112 net
->ipv6
.ip6table_nat
= ip6t_register_table(net
, &nf_nat_ipv6_table
, repl
);
114 return PTR_ERR_OR_ZERO(net
->ipv6
.ip6table_nat
);
117 static void __net_exit
ip6table_nat_net_exit(struct net
*net
)
119 ip6t_unregister_table(net
, net
->ipv6
.ip6table_nat
);
122 static struct pernet_operations ip6table_nat_net_ops
= {
123 .init
= ip6table_nat_net_init
,
124 .exit
= ip6table_nat_net_exit
,
127 static int __init
ip6table_nat_init(void)
131 err
= register_pernet_subsys(&ip6table_nat_net_ops
);
135 err
= nf_register_hooks(nf_nat_ipv6_ops
, ARRAY_SIZE(nf_nat_ipv6_ops
));
141 unregister_pernet_subsys(&ip6table_nat_net_ops
);
146 static void __exit
ip6table_nat_exit(void)
148 nf_unregister_hooks(nf_nat_ipv6_ops
, ARRAY_SIZE(nf_nat_ipv6_ops
));
149 unregister_pernet_subsys(&ip6table_nat_net_ops
);
152 module_init(ip6table_nat_init
);
153 module_exit(ip6table_nat_exit
);
155 MODULE_LICENSE("GPL");