blob: 7d2bd940291fd47a68977ec86b7ac0952744c432 [file] [log] [blame]
Patrick McHardy58a317f2012-08-26 19:14:12 +02001/*
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
Florian Westphalb9e69e12016-02-25 10:08:36 +010023static int __net_init ip6table_nat_table_init(struct net *net);
24
Patrick McHardy58a317f2012-08-26 19:14:12 +020025static const struct xt_table nf_nat_ipv6_table = {
26 .name = "nat",
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),
31 .me = THIS_MODULE,
32 .af = NFPROTO_IPV6,
Florian Westphalb9e69e12016-02-25 10:08:36 +010033 .table_init = ip6table_nat_table_init,
Patrick McHardy58a317f2012-08-26 19:14:12 +020034};
35
Eric W. Biederman06198b32015-09-18 14:33:06 -050036static unsigned int ip6table_nat_do_chain(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020037 struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -040038 const struct nf_hook_state *state,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020039 struct nf_conn *ct)
Patrick McHardy58a317f2012-08-26 19:14:12 +020040{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -050041 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_nat);
Patrick McHardy58a317f2012-08-26 19:14:12 +020042}
43
Eric W. Biederman06198b32015-09-18 14:33:06 -050044static unsigned int ip6table_nat_fn(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020045 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040046 const struct nf_hook_state *state)
Patrick McHardy58a317f2012-08-26 19:14:12 +020047{
Eric W. Biederman06198b32015-09-18 14:33:06 -050048 return nf_nat_ipv6_fn(priv, skb, state, ip6table_nat_do_chain);
Patrick McHardy58a317f2012-08-26 19:14:12 +020049}
50
Eric W. Biederman06198b32015-09-18 14:33:06 -050051static unsigned int ip6table_nat_in(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020052 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040053 const struct nf_hook_state *state)
Patrick McHardy58a317f2012-08-26 19:14:12 +020054{
Eric W. Biederman06198b32015-09-18 14:33:06 -050055 return nf_nat_ipv6_in(priv, skb, state, ip6table_nat_do_chain);
Patrick McHardy58a317f2012-08-26 19:14:12 +020056}
57
Eric W. Biederman06198b32015-09-18 14:33:06 -050058static unsigned int ip6table_nat_out(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020059 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040060 const struct nf_hook_state *state)
Patrick McHardy58a317f2012-08-26 19:14:12 +020061{
Eric W. Biederman06198b32015-09-18 14:33:06 -050062 return nf_nat_ipv6_out(priv, skb, state, ip6table_nat_do_chain);
Patrick McHardy58a317f2012-08-26 19:14:12 +020063}
64
Eric W. Biederman06198b32015-09-18 14:33:06 -050065static unsigned int ip6table_nat_local_fn(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020066 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040067 const struct nf_hook_state *state)
Patrick McHardy58a317f2012-08-26 19:14:12 +020068{
Eric W. Biederman06198b32015-09-18 14:33:06 -050069 return nf_nat_ipv6_local_fn(priv, skb, state, ip6table_nat_do_chain);
Patrick McHardy58a317f2012-08-26 19:14:12 +020070}
71
72static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = {
73 /* Before packet filtering, change destination */
74 {
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020075 .hook = ip6table_nat_in,
Patrick McHardy58a317f2012-08-26 19:14:12 +020076 .pf = NFPROTO_IPV6,
77 .hooknum = NF_INET_PRE_ROUTING,
78 .priority = NF_IP6_PRI_NAT_DST,
79 },
80 /* After packet filtering, change source */
81 {
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020082 .hook = ip6table_nat_out,
Patrick McHardy58a317f2012-08-26 19:14:12 +020083 .pf = NFPROTO_IPV6,
84 .hooknum = NF_INET_POST_ROUTING,
85 .priority = NF_IP6_PRI_NAT_SRC,
86 },
87 /* Before packet filtering, change destination */
88 {
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020089 .hook = ip6table_nat_local_fn,
Patrick McHardy58a317f2012-08-26 19:14:12 +020090 .pf = NFPROTO_IPV6,
91 .hooknum = NF_INET_LOCAL_OUT,
92 .priority = NF_IP6_PRI_NAT_DST,
93 },
94 /* After packet filtering, change source */
95 {
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +020096 .hook = ip6table_nat_fn,
Patrick McHardy58a317f2012-08-26 19:14:12 +020097 .pf = NFPROTO_IPV6,
98 .hooknum = NF_INET_LOCAL_IN,
99 .priority = NF_IP6_PRI_NAT_SRC,
100 },
101};
102
Florian Westphalb9e69e12016-02-25 10:08:36 +0100103static int __net_init ip6table_nat_table_init(struct net *net)
Patrick McHardy58a317f2012-08-26 19:14:12 +0200104{
105 struct ip6t_replace *repl;
Florian Westphala67dd262016-02-25 10:08:35 +0100106 int ret;
Patrick McHardy58a317f2012-08-26 19:14:12 +0200107
Florian Westphalb9e69e12016-02-25 10:08:36 +0100108 if (net->ipv6.ip6table_nat)
109 return 0;
110
Patrick McHardy58a317f2012-08-26 19:14:12 +0200111 repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
112 if (repl == NULL)
113 return -ENOMEM;
Florian Westphala67dd262016-02-25 10:08:35 +0100114 ret = ip6t_register_table(net, &nf_nat_ipv6_table, repl,
115 nf_nat_ipv6_ops, &net->ipv6.ip6table_nat);
Patrick McHardy58a317f2012-08-26 19:14:12 +0200116 kfree(repl);
Florian Westphala67dd262016-02-25 10:08:35 +0100117 return ret;
Patrick McHardy58a317f2012-08-26 19:14:12 +0200118}
119
120static void __net_exit ip6table_nat_net_exit(struct net *net)
121{
Florian Westphalb9e69e12016-02-25 10:08:36 +0100122 if (!net->ipv6.ip6table_nat)
123 return;
Florian Westphala67dd262016-02-25 10:08:35 +0100124 ip6t_unregister_table(net, net->ipv6.ip6table_nat, nf_nat_ipv6_ops);
Florian Westphalb9e69e12016-02-25 10:08:36 +0100125 net->ipv6.ip6table_nat = NULL;
Patrick McHardy58a317f2012-08-26 19:14:12 +0200126}
127
128static struct pernet_operations ip6table_nat_net_ops = {
Patrick McHardy58a317f2012-08-26 19:14:12 +0200129 .exit = ip6table_nat_net_exit,
130};
131
132static int __init ip6table_nat_init(void)
133{
Florian Westphalb9e69e12016-02-25 10:08:36 +0100134 int ret = register_pernet_subsys(&ip6table_nat_net_ops);
Patrick McHardy58a317f2012-08-26 19:14:12 +0200135
Florian Westphalb9e69e12016-02-25 10:08:36 +0100136 if (ret)
137 return ret;
Patrick McHardy58a317f2012-08-26 19:14:12 +0200138
Florian Westphalb9e69e12016-02-25 10:08:36 +0100139 ret = ip6table_nat_table_init(&init_net);
140 if (ret)
141 unregister_pernet_subsys(&ip6table_nat_net_ops);
142 return ret;
Patrick McHardy58a317f2012-08-26 19:14:12 +0200143}
144
145static void __exit ip6table_nat_exit(void)
146{
Patrick McHardy58a317f2012-08-26 19:14:12 +0200147 unregister_pernet_subsys(&ip6table_nat_net_ops);
148}
149
150module_init(ip6table_nat_init);
151module_exit(ip6table_nat_exit);
152
153MODULE_LICENSE("GPL");