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; |
| 36 | } nat_initial_table __initdata = { |
| 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 | |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [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, |
| 64 | .lock = RW_LOCK_UNLOCKED, |
| 65 | .me = THIS_MODULE, |
| 66 | .af = AF_INET, |
| 67 | }; |
| 68 | |
| 69 | /* Source NAT */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 70 | static unsigned int ipt_snat_target(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 71 | const struct net_device *in, |
| 72 | const struct net_device *out, |
| 73 | unsigned int hooknum, |
| 74 | const struct xt_target *target, |
| 75 | const void *targinfo) |
| 76 | { |
| 77 | struct nf_conn *ct; |
| 78 | enum ip_conntrack_info ctinfo; |
| 79 | const struct nf_nat_multi_range_compat *mr = targinfo; |
| 80 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 81 | NF_CT_ASSERT(hooknum == NF_INET_POST_ROUTING); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 82 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 83 | ct = nf_ct_get(skb, &ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 84 | |
| 85 | /* Connection must be valid and new. */ |
| 86 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED || |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 87 | ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 88 | NF_CT_ASSERT(out); |
| 89 | |
| 90 | return nf_nat_setup_info(ct, &mr->range[0], hooknum); |
| 91 | } |
| 92 | |
| 93 | /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */ |
| 94 | static void warn_if_extra_mangle(__be32 dstip, __be32 srcip) |
| 95 | { |
| 96 | static int warned = 0; |
| 97 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } }; |
| 98 | struct rtable *rt; |
| 99 | |
| 100 | if (ip_route_output_key(&rt, &fl) != 0) |
| 101 | return; |
| 102 | |
| 103 | if (rt->rt_src != srcip && !warned) { |
| 104 | printk("NAT: no longer support implicit source local NAT\n"); |
| 105 | printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n", |
| 106 | NIPQUAD(srcip), NIPQUAD(dstip)); |
| 107 | warned = 1; |
| 108 | } |
| 109 | ip_rt_put(rt); |
| 110 | } |
| 111 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 112 | static unsigned int ipt_dnat_target(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 113 | const struct net_device *in, |
| 114 | const struct net_device *out, |
| 115 | unsigned int hooknum, |
| 116 | const struct xt_target *target, |
| 117 | const void *targinfo) |
| 118 | { |
| 119 | struct nf_conn *ct; |
| 120 | enum ip_conntrack_info ctinfo; |
| 121 | const struct nf_nat_multi_range_compat *mr = targinfo; |
| 122 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 123 | NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING || |
| 124 | hooknum == NF_INET_LOCAL_OUT); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 125 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 126 | ct = nf_ct_get(skb, &ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 127 | |
| 128 | /* Connection must be valid and new. */ |
| 129 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); |
| 130 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 131 | if (hooknum == NF_INET_LOCAL_OUT && |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 132 | mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 133 | warn_if_extra_mangle(ip_hdr(skb)->daddr, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 134 | mr->range[0].min_ip); |
| 135 | |
| 136 | return nf_nat_setup_info(ct, &mr->range[0], hooknum); |
| 137 | } |
| 138 | |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 139 | static bool ipt_snat_checkentry(const char *tablename, |
| 140 | const void *entry, |
| 141 | const struct xt_target *target, |
| 142 | void *targinfo, |
| 143 | unsigned int hook_mask) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 144 | { |
| 145 | struct nf_nat_multi_range_compat *mr = targinfo; |
| 146 | |
| 147 | /* Must be a valid range */ |
| 148 | if (mr->rangesize != 1) { |
| 149 | printk("SNAT: multiple ranges no longer supported\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 150 | return false; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 151 | } |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 152 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 155 | static bool ipt_dnat_checkentry(const char *tablename, |
| 156 | const void *entry, |
| 157 | const struct xt_target *target, |
| 158 | void *targinfo, |
| 159 | unsigned int hook_mask) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 160 | { |
| 161 | struct nf_nat_multi_range_compat *mr = targinfo; |
| 162 | |
| 163 | /* Must be a valid range */ |
| 164 | if (mr->rangesize != 1) { |
| 165 | printk("DNAT: multiple ranges no longer supported\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 166 | return false; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 167 | } |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 168 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Jan Engelhardt | 170b197 | 2007-07-07 22:17:36 -0700 | [diff] [blame] | 171 | unsigned int |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 172 | alloc_null_binding(struct nf_conn *ct, unsigned int hooknum) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 173 | { |
| 174 | /* Force range to this IP; let proto decide mapping for |
| 175 | per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED). |
| 176 | Use reply in case it's already been mangled (eg local packet). |
| 177 | */ |
| 178 | __be32 ip |
| 179 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC |
| 180 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip |
| 181 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip); |
| 182 | struct nf_nat_range range |
| 183 | = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } }; |
| 184 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 185 | pr_debug("Allocating NULL binding for %p (%u.%u.%u.%u)\n", |
| 186 | ct, NIPQUAD(ip)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 187 | return nf_nat_setup_info(ct, &range, hooknum); |
| 188 | } |
| 189 | |
| 190 | unsigned int |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 191 | alloc_null_binding_confirmed(struct nf_conn *ct, unsigned int hooknum) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 192 | { |
| 193 | __be32 ip |
| 194 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC |
| 195 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip |
| 196 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip); |
Al Viro | a34c458 | 2007-07-26 17:33:19 +0100 | [diff] [blame] | 197 | __be16 all |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 198 | = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC |
| 199 | ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all |
| 200 | : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all); |
| 201 | struct nf_nat_range range |
| 202 | = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } }; |
| 203 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 204 | pr_debug("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n", |
| 205 | ct, NIPQUAD(ip)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 206 | return nf_nat_setup_info(ct, &range, hooknum); |
| 207 | } |
| 208 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 209 | int nf_nat_rule_find(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 210 | unsigned int hooknum, |
| 211 | const struct net_device *in, |
| 212 | const struct net_device *out, |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 213 | struct nf_conn *ct) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 214 | { |
| 215 | int ret; |
| 216 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 217 | ret = ipt_do_table(skb, hooknum, in, out, &nat_table); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 218 | |
| 219 | if (ret == NF_ACCEPT) { |
| 220 | if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum))) |
| 221 | /* NUL mapping */ |
Yasuyuki Kozakai | ba4c7cb | 2007-05-10 14:14:45 -0700 | [diff] [blame] | 222 | ret = alloc_null_binding(ct, hooknum); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 223 | } |
| 224 | return ret; |
| 225 | } |
| 226 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 227 | static struct xt_target ipt_snat_reg __read_mostly = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 228 | .name = "SNAT", |
| 229 | .target = ipt_snat_target, |
| 230 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
| 231 | .table = "nat", |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 232 | .hooks = 1 << NF_INET_POST_ROUTING, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 233 | .checkentry = ipt_snat_checkentry, |
| 234 | .family = AF_INET, |
| 235 | }; |
| 236 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 237 | static struct xt_target ipt_dnat_reg __read_mostly = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 238 | .name = "DNAT", |
| 239 | .target = ipt_dnat_target, |
| 240 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
| 241 | .table = "nat", |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 242 | .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT), |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 243 | .checkentry = ipt_dnat_checkentry, |
| 244 | .family = AF_INET, |
| 245 | }; |
| 246 | |
| 247 | int __init nf_nat_rule_init(void) |
| 248 | { |
| 249 | int ret; |
| 250 | |
| 251 | ret = ipt_register_table(&nat_table, &nat_initial_table.repl); |
| 252 | if (ret != 0) |
| 253 | return ret; |
| 254 | ret = xt_register_target(&ipt_snat_reg); |
| 255 | if (ret != 0) |
| 256 | goto unregister_table; |
| 257 | |
| 258 | ret = xt_register_target(&ipt_dnat_reg); |
| 259 | if (ret != 0) |
| 260 | goto unregister_snat; |
| 261 | |
| 262 | return ret; |
| 263 | |
| 264 | unregister_snat: |
| 265 | xt_unregister_target(&ipt_snat_reg); |
| 266 | unregister_table: |
| 267 | ipt_unregister_table(&nat_table); |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | void nf_nat_rule_cleanup(void) |
| 273 | { |
| 274 | xt_unregister_target(&ipt_dnat_reg); |
| 275 | xt_unregister_target(&ipt_snat_reg); |
| 276 | ipt_unregister_table(&nat_table); |
| 277 | } |