Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 3 | * (C) 2011 Patrick McHardy <kaber@trash.net> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/netfilter.h> |
| 12 | #include <linux/netfilter_ipv4.h> |
| 13 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 14 | #include <linux/ip.h> |
| 15 | #include <net/ip.h> |
| 16 | |
| 17 | #include <net/netfilter/nf_nat.h> |
| 18 | #include <net/netfilter/nf_nat_core.h> |
| 19 | #include <net/netfilter/nf_nat_l3proto.h> |
| 20 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 21 | static int __net_init iptable_nat_table_init(struct net *net); |
| 22 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 23 | static const struct xt_table nf_nat_ipv4_table = { |
| 24 | .name = "nat", |
| 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), |
| 29 | .me = THIS_MODULE, |
| 30 | .af = NFPROTO_IPV4, |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 31 | .table_init = iptable_nat_table_init, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 34 | static unsigned int iptable_nat_do_chain(void *priv, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 35 | struct sk_buff *skb, |
David S. Miller | d7cf408 | 2015-04-03 20:51:13 -0400 | [diff] [blame] | 36 | const struct nf_hook_state *state, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 37 | struct nf_conn *ct) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 38 | { |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 39 | return ipt_do_table(skb, state, state->net->ipv4.nat_table); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 42 | static unsigned int iptable_nat_ipv4_fn(void *priv, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 43 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 44 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 45 | { |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 46 | return nf_nat_ipv4_fn(priv, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 49 | static unsigned int iptable_nat_ipv4_in(void *priv, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 50 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 51 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 52 | { |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 53 | return nf_nat_ipv4_in(priv, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 56 | static unsigned int iptable_nat_ipv4_out(void *priv, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 57 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 58 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 59 | { |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 60 | return nf_nat_ipv4_out(priv, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 63 | static unsigned int iptable_nat_ipv4_local_fn(void *priv, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 64 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 65 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 66 | { |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 67 | return nf_nat_ipv4_local_fn(priv, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = { |
| 71 | /* Before packet filtering, change destination */ |
| 72 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 73 | .hook = iptable_nat_ipv4_in, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 74 | .pf = NFPROTO_IPV4, |
| 75 | .hooknum = NF_INET_PRE_ROUTING, |
| 76 | .priority = NF_IP_PRI_NAT_DST, |
| 77 | }, |
| 78 | /* After packet filtering, change source */ |
| 79 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 80 | .hook = iptable_nat_ipv4_out, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 81 | .pf = NFPROTO_IPV4, |
| 82 | .hooknum = NF_INET_POST_ROUTING, |
| 83 | .priority = NF_IP_PRI_NAT_SRC, |
| 84 | }, |
| 85 | /* Before packet filtering, change destination */ |
| 86 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 87 | .hook = iptable_nat_ipv4_local_fn, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 88 | .pf = NFPROTO_IPV4, |
| 89 | .hooknum = NF_INET_LOCAL_OUT, |
| 90 | .priority = NF_IP_PRI_NAT_DST, |
| 91 | }, |
| 92 | /* After packet filtering, change source */ |
| 93 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 94 | .hook = iptable_nat_ipv4_fn, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 95 | .pf = NFPROTO_IPV4, |
| 96 | .hooknum = NF_INET_LOCAL_IN, |
| 97 | .priority = NF_IP_PRI_NAT_SRC, |
| 98 | }, |
| 99 | }; |
| 100 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 101 | static int __net_init iptable_nat_table_init(struct net *net) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 102 | { |
| 103 | struct ipt_replace *repl; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 104 | int ret; |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 105 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 106 | if (net->ipv4.nat_table) |
| 107 | return 0; |
| 108 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 109 | repl = ipt_alloc_initial_table(&nf_nat_ipv4_table); |
| 110 | if (repl == NULL) |
| 111 | return -ENOMEM; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 112 | ret = ipt_register_table(net, &nf_nat_ipv4_table, repl, |
| 113 | nf_nat_ipv4_ops, &net->ipv4.nat_table); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 114 | kfree(repl); |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 115 | return ret; |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void __net_exit iptable_nat_net_exit(struct net *net) |
| 119 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 120 | if (!net->ipv4.nat_table) |
| 121 | return; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 122 | ipt_unregister_table(net, net->ipv4.nat_table, nf_nat_ipv4_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 123 | net->ipv4.nat_table = NULL; |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static struct pernet_operations iptable_nat_net_ops = { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 127 | .exit = iptable_nat_net_exit, |
| 128 | }; |
| 129 | |
| 130 | static int __init iptable_nat_init(void) |
| 131 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 132 | int ret = register_pernet_subsys(&iptable_nat_net_ops); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 133 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 134 | if (ret) |
| 135 | return ret; |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 136 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 137 | ret = iptable_nat_table_init(&init_net); |
| 138 | if (ret) |
| 139 | unregister_pernet_subsys(&iptable_nat_net_ops); |
| 140 | return ret; |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void __exit iptable_nat_exit(void) |
| 144 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 145 | unregister_pernet_subsys(&iptable_nat_net_ops); |
| 146 | } |
| 147 | |
| 148 | module_init(iptable_nat_init); |
| 149 | module_exit(iptable_nat_exit); |
| 150 | |
| 151 | MODULE_LICENSE("GPL"); |