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 | |
| 21 | static const struct xt_table nf_nat_ipv4_table = { |
| 22 | .name = "nat", |
| 23 | .valid_hooks = (1 << NF_INET_PRE_ROUTING) | |
| 24 | (1 << NF_INET_POST_ROUTING) | |
| 25 | (1 << NF_INET_LOCAL_OUT) | |
| 26 | (1 << NF_INET_LOCAL_IN), |
| 27 | .me = THIS_MODULE, |
| 28 | .af = NFPROTO_IPV4, |
| 29 | }; |
| 30 | |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 31 | static unsigned int iptable_nat_do_chain(const struct nf_hook_ops *ops, |
| 32 | struct sk_buff *skb, |
David S. Miller | d7cf408 | 2015-04-03 20:51:13 -0400 | [diff] [blame] | 33 | const struct nf_hook_state *state, |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 34 | struct nf_conn *ct) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 35 | { |
| 36 | struct net *net = nf_ct_net(ct); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 37 | |
David S. Miller | 1c491ba | 2015-04-03 20:56:08 -0400 | [diff] [blame] | 38 | return ipt_do_table(skb, ops->hooknum, state, net->ipv4.nat_table); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 41 | static unsigned int iptable_nat_ipv4_fn(const struct nf_hook_ops *ops, |
| 42 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 43 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 44 | { |
David S. Miller | d7cf408 | 2015-04-03 20:51:13 -0400 | [diff] [blame] | 45 | return nf_nat_ipv4_fn(ops, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 48 | static unsigned int iptable_nat_ipv4_in(const struct nf_hook_ops *ops, |
| 49 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 50 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 51 | { |
David S. Miller | d7cf408 | 2015-04-03 20:51:13 -0400 | [diff] [blame] | 52 | return nf_nat_ipv4_in(ops, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 55 | static unsigned int iptable_nat_ipv4_out(const struct nf_hook_ops *ops, |
| 56 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 57 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 58 | { |
David S. Miller | d7cf408 | 2015-04-03 20:51:13 -0400 | [diff] [blame] | 59 | return nf_nat_ipv4_out(ops, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 62 | static unsigned int iptable_nat_ipv4_local_fn(const struct nf_hook_ops *ops, |
| 63 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 64 | const struct nf_hook_state *state) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 65 | { |
David S. Miller | d7cf408 | 2015-04-03 20:51:13 -0400 | [diff] [blame] | 66 | return nf_nat_ipv4_local_fn(ops, skb, state, iptable_nat_do_chain); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = { |
| 70 | /* Before packet filtering, change destination */ |
| 71 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 72 | .hook = iptable_nat_ipv4_in, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 73 | .owner = THIS_MODULE, |
| 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 | .owner = THIS_MODULE, |
| 82 | .pf = NFPROTO_IPV4, |
| 83 | .hooknum = NF_INET_POST_ROUTING, |
| 84 | .priority = NF_IP_PRI_NAT_SRC, |
| 85 | }, |
| 86 | /* Before packet filtering, change destination */ |
| 87 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 88 | .hook = iptable_nat_ipv4_local_fn, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 89 | .owner = THIS_MODULE, |
| 90 | .pf = NFPROTO_IPV4, |
| 91 | .hooknum = NF_INET_LOCAL_OUT, |
| 92 | .priority = NF_IP_PRI_NAT_DST, |
| 93 | }, |
| 94 | /* After packet filtering, change source */ |
| 95 | { |
Pablo Neira Ayuso | 30766f4 | 2014-08-05 20:02:42 +0200 | [diff] [blame] | 96 | .hook = iptable_nat_ipv4_fn, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 97 | .owner = THIS_MODULE, |
| 98 | .pf = NFPROTO_IPV4, |
| 99 | .hooknum = NF_INET_LOCAL_IN, |
| 100 | .priority = NF_IP_PRI_NAT_SRC, |
| 101 | }, |
| 102 | }; |
| 103 | |
| 104 | static int __net_init iptable_nat_net_init(struct net *net) |
| 105 | { |
| 106 | struct ipt_replace *repl; |
| 107 | |
| 108 | repl = ipt_alloc_initial_table(&nf_nat_ipv4_table); |
| 109 | if (repl == NULL) |
| 110 | return -ENOMEM; |
| 111 | net->ipv4.nat_table = ipt_register_table(net, &nf_nat_ipv4_table, repl); |
| 112 | kfree(repl); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 113 | return PTR_ERR_OR_ZERO(net->ipv4.nat_table); |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void __net_exit iptable_nat_net_exit(struct net *net) |
| 117 | { |
| 118 | ipt_unregister_table(net, net->ipv4.nat_table); |
| 119 | } |
| 120 | |
| 121 | static struct pernet_operations iptable_nat_net_ops = { |
| 122 | .init = iptable_nat_net_init, |
| 123 | .exit = iptable_nat_net_exit, |
| 124 | }; |
| 125 | |
| 126 | static int __init iptable_nat_init(void) |
| 127 | { |
| 128 | int err; |
| 129 | |
| 130 | err = register_pernet_subsys(&iptable_nat_net_ops); |
| 131 | if (err < 0) |
| 132 | goto err1; |
| 133 | |
| 134 | err = nf_register_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops)); |
| 135 | if (err < 0) |
| 136 | goto err2; |
| 137 | return 0; |
| 138 | |
| 139 | err2: |
| 140 | unregister_pernet_subsys(&iptable_nat_net_ops); |
| 141 | err1: |
| 142 | return err; |
| 143 | } |
| 144 | |
| 145 | static void __exit iptable_nat_exit(void) |
| 146 | { |
| 147 | nf_unregister_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops)); |
| 148 | unregister_pernet_subsys(&iptable_nat_net_ops); |
| 149 | } |
| 150 | |
| 151 | module_init(iptable_nat_init); |
| 152 | module_exit(iptable_nat_exit); |
| 153 | |
| 154 | MODULE_LICENSE("GPL"); |