Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 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 | |
| 9 | /* Everything about the rules for NAT. */ |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/ip.h> |
| 12 | #include <linux/netfilter.h> |
| 13 | #include <linux/netfilter_ipv4.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kmod.h> |
| 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/proc_fs.h> |
| 18 | #include <net/checksum.h> |
| 19 | #include <net/route.h> |
| 20 | #include <linux/bitops.h> |
| 21 | |
| 22 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 23 | #include <net/netfilter/nf_nat.h> |
| 24 | #include <net/netfilter/nf_nat_core.h> |
| 25 | #include <net/netfilter/nf_nat_rule.h> |
| 26 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 27 | #define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \ |
| 28 | (1 << NF_INET_POST_ROUTING) | \ |
| 29 | (1 << NF_INET_LOCAL_OUT)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 30 | |
| 31 | static struct |
| 32 | { |
| 33 | struct ipt_replace repl; |
| 34 | struct ipt_standard entries[3]; |
| 35 | struct ipt_error term; |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 36 | } nat_initial_table __net_initdata = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 37 | .repl = { |
| 38 | .name = "nat", |
| 39 | .valid_hooks = NAT_VALID_HOOKS, |
| 40 | .num_entries = 4, |
| 41 | .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error), |
| 42 | .hook_entry = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 43 | [NF_INET_PRE_ROUTING] = 0, |
| 44 | [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard), |
| 45 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 46 | }, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 47 | .underflow = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 48 | [NF_INET_PRE_ROUTING] = 0, |
| 49 | [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard), |
| 50 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 51 | }, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 52 | }, |
| 53 | .entries = { |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 54 | IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */ |
| 55 | IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */ |
| 56 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 57 | }, |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 58 | .term = IPT_ERROR_INIT, /* ERROR */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 61 | static struct xt_table nat_table = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 62 | .name = "nat", |
| 63 | .valid_hooks = NAT_VALID_HOOKS, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 64 | .me = THIS_MODULE, |
| 65 | .af = AF_INET, |
| 66 | }; |
| 67 | |
| 68 | /* Source NAT */ |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 69 | static unsigned int |
| 70 | ipt_snat_target(struct sk_buff *skb, const struct xt_target_param *par) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 71 | { |
| 72 | struct nf_conn *ct; |
| 73 | enum ip_conntrack_info ctinfo; |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 74 | const struct nf_nat_multi_range_compat *mr = par->targinfo; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 75 | |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 76 | NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 77 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 78 | ct = nf_ct_get(skb, &ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 79 | |
| 80 | /* Connection must be valid and new. */ |
| 81 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED || |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 82 | ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)); |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 83 | NF_CT_ASSERT(par->out != NULL); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 84 | |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 85 | return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 88 | static unsigned int |
| 89 | ipt_dnat_target(struct sk_buff *skb, const struct xt_target_param *par) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 90 | { |
| 91 | struct nf_conn *ct; |
| 92 | enum ip_conntrack_info ctinfo; |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 93 | const struct nf_nat_multi_range_compat *mr = par->targinfo; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 94 | |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 95 | NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING || |
| 96 | par->hooknum == NF_INET_LOCAL_OUT); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 97 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 98 | ct = nf_ct_get(skb, &ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 99 | |
| 100 | /* Connection must be valid and new. */ |
| 101 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); |
| 102 | |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 103 | return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 106 | static bool ipt_snat_checkentry(const struct xt_tgchk_param *par) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 107 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 108 | const struct nf_nat_multi_range_compat *mr = par->targinfo; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 109 | |
| 110 | /* Must be a valid range */ |
| 111 | if (mr->rangesize != 1) { |
| 112 | printk("SNAT: multiple ranges no longer supported\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 113 | return false; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 114 | } |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 115 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 118 | static bool ipt_dnat_checkentry(const struct xt_tgchk_param *par) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 119 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 120 | const struct nf_nat_multi_range_compat *mr = par->targinfo; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 121 | |
| 122 | /* Must be a valid range */ |
| 123 | if (mr->rangesize != 1) { |
| 124 | printk("DNAT: multiple ranges no longer supported\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 125 | return false; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 126 | } |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 127 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Jan Engelhardt | 170b197 | 2007-07-07 22:17:36 -0700 | [diff] [blame] | 130 | unsigned int |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 131 | alloc_null_binding(struct nf_conn *ct, unsigned int hooknum) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 132 | { |
| 133 | /* Force range to this IP; let proto decide mapping for |
| 134 | per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED). |
| 135 | Use reply in case it's already been mangled (eg local packet). |
| 136 | */ |
| 137 | __be32 ip |
| 138 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC |
| 139 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip |
| 140 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip); |
| 141 | struct nf_nat_range range |
| 142 | = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } }; |
| 143 | |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 144 | pr_debug("Allocating NULL binding for %p (%pI4)\n", ct, &ip); |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 145 | return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 148 | int nf_nat_rule_find(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 149 | unsigned int hooknum, |
| 150 | const struct net_device *in, |
| 151 | const struct net_device *out, |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 152 | struct nf_conn *ct) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 153 | { |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 154 | struct net *net = nf_ct_net(ct); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 155 | int ret; |
| 156 | |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 157 | ret = ipt_do_table(skb, hooknum, in, out, net->ipv4.nat_table); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 158 | |
| 159 | if (ret == NF_ACCEPT) { |
| 160 | if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum))) |
| 161 | /* NUL mapping */ |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 162 | ret = alloc_null_binding(ct, hooknum); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 163 | } |
| 164 | return ret; |
| 165 | } |
| 166 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 167 | static struct xt_target ipt_snat_reg __read_mostly = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 168 | .name = "SNAT", |
| 169 | .target = ipt_snat_target, |
| 170 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
| 171 | .table = "nat", |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 172 | .hooks = 1 << NF_INET_POST_ROUTING, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 173 | .checkentry = ipt_snat_checkentry, |
| 174 | .family = AF_INET, |
| 175 | }; |
| 176 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 177 | static struct xt_target ipt_dnat_reg __read_mostly = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 178 | .name = "DNAT", |
| 179 | .target = ipt_dnat_target, |
| 180 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
| 181 | .table = "nat", |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 182 | .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT), |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 183 | .checkentry = ipt_dnat_checkentry, |
| 184 | .family = AF_INET, |
| 185 | }; |
| 186 | |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 187 | static int __net_init nf_nat_rule_net_init(struct net *net) |
| 188 | { |
| 189 | net->ipv4.nat_table = ipt_register_table(net, &nat_table, |
| 190 | &nat_initial_table.repl); |
| 191 | if (IS_ERR(net->ipv4.nat_table)) |
| 192 | return PTR_ERR(net->ipv4.nat_table); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void __net_exit nf_nat_rule_net_exit(struct net *net) |
| 197 | { |
| 198 | ipt_unregister_table(net->ipv4.nat_table); |
| 199 | } |
| 200 | |
| 201 | static struct pernet_operations nf_nat_rule_net_ops = { |
| 202 | .init = nf_nat_rule_net_init, |
| 203 | .exit = nf_nat_rule_net_exit, |
| 204 | }; |
| 205 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 206 | int __init nf_nat_rule_init(void) |
| 207 | { |
| 208 | int ret; |
| 209 | |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 210 | ret = register_pernet_subsys(&nf_nat_rule_net_ops); |
| 211 | if (ret != 0) |
| 212 | goto out; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 213 | ret = xt_register_target(&ipt_snat_reg); |
| 214 | if (ret != 0) |
| 215 | goto unregister_table; |
| 216 | |
| 217 | ret = xt_register_target(&ipt_dnat_reg); |
| 218 | if (ret != 0) |
| 219 | goto unregister_snat; |
| 220 | |
| 221 | return ret; |
| 222 | |
| 223 | unregister_snat: |
| 224 | xt_unregister_target(&ipt_snat_reg); |
| 225 | unregister_table: |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 226 | unregister_pernet_subsys(&nf_nat_rule_net_ops); |
| 227 | out: |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | void nf_nat_rule_cleanup(void) |
| 232 | { |
| 233 | xt_unregister_target(&ipt_dnat_reg); |
| 234 | xt_unregister_target(&ipt_snat_reg); |
Alexey Dobriyan | e099a17 | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 235 | unregister_pernet_subsys(&nf_nat_rule_net_ops); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 236 | } |