Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* NAT for netfilter; shared with compatibility layer. */ |
| 2 | |
| 3 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 4 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/timer.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <net/checksum.h> |
| 17 | #include <net/icmp.h> |
| 18 | #include <net/ip.h> |
| 19 | #include <net/tcp.h> /* For tcp_prot in getorigdst */ |
| 20 | #include <linux/icmp.h> |
| 21 | #include <linux/udp.h> |
| 22 | #include <linux/jhash.h> |
| 23 | |
| 24 | #include <linux/netfilter_ipv4.h> |
| 25 | #include <net/netfilter/nf_conntrack.h> |
| 26 | #include <net/netfilter/nf_conntrack_core.h> |
| 27 | #include <net/netfilter/nf_nat.h> |
| 28 | #include <net/netfilter/nf_nat_protocol.h> |
| 29 | #include <net/netfilter/nf_nat_core.h> |
| 30 | #include <net/netfilter/nf_nat_helper.h> |
| 31 | #include <net/netfilter/nf_conntrack_helper.h> |
| 32 | #include <net/netfilter/nf_conntrack_l3proto.h> |
| 33 | #include <net/netfilter/nf_conntrack_l4proto.h> |
| 34 | |
| 35 | #if 0 |
| 36 | #define DEBUGP printk |
| 37 | #else |
| 38 | #define DEBUGP(format, args...) |
| 39 | #endif |
| 40 | |
| 41 | static DEFINE_RWLOCK(nf_nat_lock); |
| 42 | |
| 43 | static struct nf_conntrack_l3proto *l3proto = NULL; |
| 44 | |
| 45 | /* Calculated at init based on memory size */ |
| 46 | static unsigned int nf_nat_htable_size; |
| 47 | |
| 48 | static struct list_head *bysource; |
| 49 | |
| 50 | #define MAX_IP_NAT_PROTO 256 |
| 51 | static struct nf_nat_protocol *nf_nat_protos[MAX_IP_NAT_PROTO]; |
| 52 | |
| 53 | static inline struct nf_nat_protocol * |
| 54 | __nf_nat_proto_find(u_int8_t protonum) |
| 55 | { |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 56 | return rcu_dereference(nf_nat_protos[protonum]); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | struct nf_nat_protocol * |
| 60 | nf_nat_proto_find_get(u_int8_t protonum) |
| 61 | { |
| 62 | struct nf_nat_protocol *p; |
| 63 | |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 64 | rcu_read_lock(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 65 | p = __nf_nat_proto_find(protonum); |
| 66 | if (!try_module_get(p->me)) |
| 67 | p = &nf_nat_unknown_protocol; |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 68 | rcu_read_unlock(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 69 | |
| 70 | return p; |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(nf_nat_proto_find_get); |
| 73 | |
| 74 | void |
| 75 | nf_nat_proto_put(struct nf_nat_protocol *p) |
| 76 | { |
| 77 | module_put(p->me); |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(nf_nat_proto_put); |
| 80 | |
| 81 | /* We keep an extra hash for each conntrack, for fast searching. */ |
| 82 | static inline unsigned int |
| 83 | hash_by_src(const struct nf_conntrack_tuple *tuple) |
| 84 | { |
| 85 | /* Original src, to ensure we map it consistently if poss. */ |
| 86 | return jhash_3words((__force u32)tuple->src.u3.ip, tuple->src.u.all, |
| 87 | tuple->dst.protonum, 0) % nf_nat_htable_size; |
| 88 | } |
| 89 | |
| 90 | /* Noone using conntrack by the time this called. */ |
| 91 | static void nf_nat_cleanup_conntrack(struct nf_conn *conn) |
| 92 | { |
| 93 | struct nf_conn_nat *nat; |
| 94 | if (!(conn->status & IPS_NAT_DONE_MASK)) |
| 95 | return; |
| 96 | |
| 97 | nat = nfct_nat(conn); |
| 98 | write_lock_bh(&nf_nat_lock); |
| 99 | list_del(&nat->info.bysource); |
| 100 | write_unlock_bh(&nf_nat_lock); |
| 101 | } |
| 102 | |
| 103 | /* Is this tuple already taken? (not by us) */ |
| 104 | int |
| 105 | nf_nat_used_tuple(const struct nf_conntrack_tuple *tuple, |
| 106 | const struct nf_conn *ignored_conntrack) |
| 107 | { |
| 108 | /* Conntrack tracking doesn't keep track of outgoing tuples; only |
| 109 | incoming ones. NAT means they don't have a fixed mapping, |
| 110 | so we invert the tuple and look for the incoming reply. |
| 111 | |
| 112 | We could keep a separate hash if this proves too slow. */ |
| 113 | struct nf_conntrack_tuple reply; |
| 114 | |
| 115 | nf_ct_invert_tuplepr(&reply, tuple); |
| 116 | return nf_conntrack_tuple_taken(&reply, ignored_conntrack); |
| 117 | } |
| 118 | EXPORT_SYMBOL(nf_nat_used_tuple); |
| 119 | |
| 120 | /* If we source map this tuple so reply looks like reply_tuple, will |
| 121 | * that meet the constraints of range. */ |
| 122 | static int |
| 123 | in_range(const struct nf_conntrack_tuple *tuple, |
| 124 | const struct nf_nat_range *range) |
| 125 | { |
| 126 | struct nf_nat_protocol *proto; |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 127 | int ret = 0; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 128 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 129 | /* If we are supposed to map IPs, then we must be in the |
| 130 | range specified, otherwise let this drag us onto a new src IP. */ |
| 131 | if (range->flags & IP_NAT_RANGE_MAP_IPS) { |
| 132 | if (ntohl(tuple->src.u3.ip) < ntohl(range->min_ip) || |
| 133 | ntohl(tuple->src.u3.ip) > ntohl(range->max_ip)) |
| 134 | return 0; |
| 135 | } |
| 136 | |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 137 | rcu_read_lock(); |
| 138 | proto = __nf_nat_proto_find(tuple->dst.protonum); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 139 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) || |
| 140 | proto->in_range(tuple, IP_NAT_MANIP_SRC, |
| 141 | &range->min, &range->max)) |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 142 | ret = 1; |
| 143 | rcu_read_unlock(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 144 | |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 145 | return ret; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static inline int |
| 149 | same_src(const struct nf_conn *ct, |
| 150 | const struct nf_conntrack_tuple *tuple) |
| 151 | { |
| 152 | const struct nf_conntrack_tuple *t; |
| 153 | |
| 154 | t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
| 155 | return (t->dst.protonum == tuple->dst.protonum && |
| 156 | t->src.u3.ip == tuple->src.u3.ip && |
| 157 | t->src.u.all == tuple->src.u.all); |
| 158 | } |
| 159 | |
| 160 | /* Only called for SRC manip */ |
| 161 | static int |
| 162 | find_appropriate_src(const struct nf_conntrack_tuple *tuple, |
| 163 | struct nf_conntrack_tuple *result, |
| 164 | const struct nf_nat_range *range) |
| 165 | { |
| 166 | unsigned int h = hash_by_src(tuple); |
| 167 | struct nf_conn_nat *nat; |
| 168 | struct nf_conn *ct; |
| 169 | |
| 170 | read_lock_bh(&nf_nat_lock); |
| 171 | list_for_each_entry(nat, &bysource[h], info.bysource) { |
| 172 | ct = (struct nf_conn *)((char *)nat - offsetof(struct nf_conn, data)); |
| 173 | if (same_src(ct, tuple)) { |
| 174 | /* Copy source part from reply tuple. */ |
| 175 | nf_ct_invert_tuplepr(result, |
| 176 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 177 | result->dst = tuple->dst; |
| 178 | |
| 179 | if (in_range(result, range)) { |
| 180 | read_unlock_bh(&nf_nat_lock); |
| 181 | return 1; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | read_unlock_bh(&nf_nat_lock); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* For [FUTURE] fragmentation handling, we want the least-used |
| 190 | src-ip/dst-ip/proto triple. Fairness doesn't come into it. Thus |
| 191 | if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports |
| 192 | 1-65535, we don't do pro-rata allocation based on ports; we choose |
| 193 | the ip with the lowest src-ip/dst-ip/proto usage. |
| 194 | */ |
| 195 | static void |
| 196 | find_best_ips_proto(struct nf_conntrack_tuple *tuple, |
| 197 | const struct nf_nat_range *range, |
| 198 | const struct nf_conn *ct, |
| 199 | enum nf_nat_manip_type maniptype) |
| 200 | { |
| 201 | __be32 *var_ipp; |
| 202 | /* Host order */ |
| 203 | u_int32_t minip, maxip, j; |
| 204 | |
| 205 | /* No IP mapping? Do nothing. */ |
| 206 | if (!(range->flags & IP_NAT_RANGE_MAP_IPS)) |
| 207 | return; |
| 208 | |
| 209 | if (maniptype == IP_NAT_MANIP_SRC) |
| 210 | var_ipp = &tuple->src.u3.ip; |
| 211 | else |
| 212 | var_ipp = &tuple->dst.u3.ip; |
| 213 | |
| 214 | /* Fast path: only one choice. */ |
| 215 | if (range->min_ip == range->max_ip) { |
| 216 | *var_ipp = range->min_ip; |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | /* Hashing source and destination IPs gives a fairly even |
| 221 | * spread in practice (if there are a small number of IPs |
| 222 | * involved, there usually aren't that many connections |
| 223 | * anyway). The consistency means that servers see the same |
| 224 | * client coming from the same IP (some Internet Banking sites |
| 225 | * like this), even across reboots. */ |
| 226 | minip = ntohl(range->min_ip); |
| 227 | maxip = ntohl(range->max_ip); |
| 228 | j = jhash_2words((__force u32)tuple->src.u3.ip, |
| 229 | (__force u32)tuple->dst.u3.ip, 0); |
| 230 | *var_ipp = htonl(minip + j % (maxip - minip + 1)); |
| 231 | } |
| 232 | |
| 233 | /* Manipulate the tuple into the range given. For NF_IP_POST_ROUTING, |
| 234 | * we change the source to map into the range. For NF_IP_PRE_ROUTING |
| 235 | * and NF_IP_LOCAL_OUT, we change the destination to map into the |
| 236 | * range. It might not be possible to get a unique tuple, but we try. |
| 237 | * At worst (or if we race), we will end up with a final duplicate in |
| 238 | * __ip_conntrack_confirm and drop the packet. */ |
| 239 | static void |
| 240 | get_unique_tuple(struct nf_conntrack_tuple *tuple, |
| 241 | const struct nf_conntrack_tuple *orig_tuple, |
| 242 | const struct nf_nat_range *range, |
| 243 | struct nf_conn *ct, |
| 244 | enum nf_nat_manip_type maniptype) |
| 245 | { |
| 246 | struct nf_nat_protocol *proto; |
| 247 | |
| 248 | /* 1) If this srcip/proto/src-proto-part is currently mapped, |
| 249 | and that same mapping gives a unique tuple within the given |
| 250 | range, use that. |
| 251 | |
| 252 | This is only required for source (ie. NAT/masq) mappings. |
| 253 | So far, we don't do local source mappings, so multiple |
| 254 | manips not an issue. */ |
| 255 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 256 | if (find_appropriate_src(orig_tuple, tuple, range)) { |
| 257 | DEBUGP("get_unique_tuple: Found current src map\n"); |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 258 | if (!(range->flags & IP_NAT_RANGE_PROTO_RANDOM)) |
| 259 | if (!nf_nat_used_tuple(tuple, ct)) |
| 260 | return; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | /* 2) Select the least-used IP/proto combination in the given |
| 265 | range. */ |
| 266 | *tuple = *orig_tuple; |
| 267 | find_best_ips_proto(tuple, range, ct, maniptype); |
| 268 | |
| 269 | /* 3) The per-protocol part of the manip is made to map into |
| 270 | the range to make a unique tuple. */ |
| 271 | |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 272 | rcu_read_lock(); |
| 273 | proto = __nf_nat_proto_find(orig_tuple->dst.protonum); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 274 | |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 275 | /* Change protocol info to have some randomization */ |
| 276 | if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) { |
| 277 | proto->unique_tuple(tuple, range, maniptype, ct); |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 278 | goto out; |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 281 | /* Only bother mapping if it's not already in range and unique */ |
| 282 | if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) || |
| 283 | proto->in_range(tuple, maniptype, &range->min, &range->max)) && |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 284 | !nf_nat_used_tuple(tuple, ct)) |
| 285 | goto out; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 286 | |
| 287 | /* Last change: get protocol to try to obtain unique tuple. */ |
| 288 | proto->unique_tuple(tuple, range, maniptype, ct); |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 289 | out: |
| 290 | rcu_read_unlock(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | unsigned int |
| 294 | nf_nat_setup_info(struct nf_conn *ct, |
| 295 | const struct nf_nat_range *range, |
| 296 | unsigned int hooknum) |
| 297 | { |
| 298 | struct nf_conntrack_tuple curr_tuple, new_tuple; |
| 299 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 300 | struct nf_nat_info *info = &nat->info; |
| 301 | int have_to_hash = !(ct->status & IPS_NAT_DONE_MASK); |
| 302 | enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum); |
| 303 | |
| 304 | NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING || |
| 305 | hooknum == NF_IP_POST_ROUTING || |
| 306 | hooknum == NF_IP_LOCAL_IN || |
| 307 | hooknum == NF_IP_LOCAL_OUT); |
| 308 | BUG_ON(nf_nat_initialized(ct, maniptype)); |
| 309 | |
| 310 | /* What we've got will look like inverse of reply. Normally |
| 311 | this is what is in the conntrack, except for prior |
| 312 | manipulations (future optimization: if num_manips == 0, |
| 313 | orig_tp = |
| 314 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple) */ |
| 315 | nf_ct_invert_tuplepr(&curr_tuple, |
| 316 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 317 | |
| 318 | get_unique_tuple(&new_tuple, &curr_tuple, range, ct, maniptype); |
| 319 | |
| 320 | if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) { |
| 321 | struct nf_conntrack_tuple reply; |
| 322 | |
| 323 | /* Alter conntrack table so will recognize replies. */ |
| 324 | nf_ct_invert_tuplepr(&reply, &new_tuple); |
| 325 | nf_conntrack_alter_reply(ct, &reply); |
| 326 | |
| 327 | /* Non-atomic: we own this at the moment. */ |
| 328 | if (maniptype == IP_NAT_MANIP_SRC) |
| 329 | ct->status |= IPS_SRC_NAT; |
| 330 | else |
| 331 | ct->status |= IPS_DST_NAT; |
| 332 | } |
| 333 | |
| 334 | /* Place in source hash if this is the first time. */ |
| 335 | if (have_to_hash) { |
| 336 | unsigned int srchash; |
| 337 | |
| 338 | srchash = hash_by_src(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
| 339 | write_lock_bh(&nf_nat_lock); |
| 340 | list_add(&info->bysource, &bysource[srchash]); |
| 341 | write_unlock_bh(&nf_nat_lock); |
| 342 | } |
| 343 | |
| 344 | /* It's done. */ |
| 345 | if (maniptype == IP_NAT_MANIP_DST) |
| 346 | set_bit(IPS_DST_NAT_DONE_BIT, &ct->status); |
| 347 | else |
| 348 | set_bit(IPS_SRC_NAT_DONE_BIT, &ct->status); |
| 349 | |
| 350 | return NF_ACCEPT; |
| 351 | } |
| 352 | EXPORT_SYMBOL(nf_nat_setup_info); |
| 353 | |
| 354 | /* Returns true if succeeded. */ |
| 355 | static int |
| 356 | manip_pkt(u_int16_t proto, |
| 357 | struct sk_buff **pskb, |
| 358 | unsigned int iphdroff, |
| 359 | const struct nf_conntrack_tuple *target, |
| 360 | enum nf_nat_manip_type maniptype) |
| 361 | { |
| 362 | struct iphdr *iph; |
| 363 | struct nf_nat_protocol *p; |
| 364 | |
| 365 | if (!skb_make_writable(pskb, iphdroff + sizeof(*iph))) |
| 366 | return 0; |
| 367 | |
| 368 | iph = (void *)(*pskb)->data + iphdroff; |
| 369 | |
| 370 | /* Manipulate protcol part. */ |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 371 | |
| 372 | /* rcu_read_lock()ed by nf_hook_slow */ |
| 373 | p = __nf_nat_proto_find(proto); |
| 374 | if (!p->manip_pkt(pskb, iphdroff, target, maniptype)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 375 | return 0; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 376 | |
| 377 | iph = (void *)(*pskb)->data + iphdroff; |
| 378 | |
| 379 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 380 | nf_csum_replace4(&iph->check, iph->saddr, target->src.u3.ip); |
| 381 | iph->saddr = target->src.u3.ip; |
| 382 | } else { |
| 383 | nf_csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip); |
| 384 | iph->daddr = target->dst.u3.ip; |
| 385 | } |
| 386 | return 1; |
| 387 | } |
| 388 | |
| 389 | /* Do packet manipulations according to nf_nat_setup_info. */ |
| 390 | unsigned int nf_nat_packet(struct nf_conn *ct, |
| 391 | enum ip_conntrack_info ctinfo, |
| 392 | unsigned int hooknum, |
| 393 | struct sk_buff **pskb) |
| 394 | { |
| 395 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
| 396 | unsigned long statusbit; |
| 397 | enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum); |
| 398 | |
| 399 | if (mtype == IP_NAT_MANIP_SRC) |
| 400 | statusbit = IPS_SRC_NAT; |
| 401 | else |
| 402 | statusbit = IPS_DST_NAT; |
| 403 | |
| 404 | /* Invert if this is reply dir. */ |
| 405 | if (dir == IP_CT_DIR_REPLY) |
| 406 | statusbit ^= IPS_NAT_MASK; |
| 407 | |
| 408 | /* Non-atomic: these bits don't change. */ |
| 409 | if (ct->status & statusbit) { |
| 410 | struct nf_conntrack_tuple target; |
| 411 | |
| 412 | /* We are aiming to look like inverse of other direction. */ |
| 413 | nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple); |
| 414 | |
| 415 | if (!manip_pkt(target.dst.protonum, pskb, 0, &target, mtype)) |
| 416 | return NF_DROP; |
| 417 | } |
| 418 | return NF_ACCEPT; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(nf_nat_packet); |
| 421 | |
| 422 | /* Dir is direction ICMP is coming from (opposite to packet it contains) */ |
| 423 | int nf_nat_icmp_reply_translation(struct nf_conn *ct, |
| 424 | enum ip_conntrack_info ctinfo, |
| 425 | unsigned int hooknum, |
| 426 | struct sk_buff **pskb) |
| 427 | { |
| 428 | struct { |
| 429 | struct icmphdr icmp; |
| 430 | struct iphdr ip; |
| 431 | } *inside; |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 432 | struct nf_conntrack_l4proto *l4proto; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 433 | struct nf_conntrack_tuple inner, target; |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame^] | 434 | int hdrlen = ip_hdrlen(*pskb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 435 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
| 436 | unsigned long statusbit; |
| 437 | enum nf_nat_manip_type manip = HOOK2MANIP(hooknum); |
| 438 | |
| 439 | if (!skb_make_writable(pskb, hdrlen + sizeof(*inside))) |
| 440 | return 0; |
| 441 | |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame^] | 442 | inside = (void *)(*pskb)->data + ip_hdrlen(*pskb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 443 | |
| 444 | /* We're actually going to mangle it beyond trivial checksum |
| 445 | adjustment, so make sure the current checksum is correct. */ |
| 446 | if (nf_ip_checksum(*pskb, hooknum, hdrlen, 0)) |
| 447 | return 0; |
| 448 | |
| 449 | /* Must be RELATED */ |
| 450 | NF_CT_ASSERT((*pskb)->nfctinfo == IP_CT_RELATED || |
| 451 | (*pskb)->nfctinfo == IP_CT_RELATED+IP_CT_IS_REPLY); |
| 452 | |
| 453 | /* Redirects on non-null nats must be dropped, else they'll |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 454 | start talking to each other without our translation, and be |
| 455 | confused... --RR */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 456 | if (inside->icmp.type == ICMP_REDIRECT) { |
| 457 | /* If NAT isn't finished, assume it and drop. */ |
| 458 | if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK) |
| 459 | return 0; |
| 460 | |
| 461 | if (ct->status & IPS_NAT_MASK) |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | DEBUGP("icmp_reply_translation: translating error %p manp %u dir %s\n", |
| 466 | *pskb, manip, dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY"); |
| 467 | |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 468 | /* rcu_read_lock()ed by nf_hook_slow */ |
| 469 | l4proto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol); |
| 470 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 471 | if (!nf_ct_get_tuple(*pskb, |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame^] | 472 | ip_hdrlen(*pskb) + sizeof(struct icmphdr), |
| 473 | (ip_hdrlen(*pskb) + |
| 474 | sizeof(struct icmphdr) + inside->ip.ihl * 4), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 475 | (u_int16_t)AF_INET, |
| 476 | inside->ip.protocol, |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 477 | &inner, l3proto, l4proto)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 478 | return 0; |
| 479 | |
| 480 | /* Change inner back to look like incoming packet. We do the |
| 481 | opposite manip on this hook to normal, because it might not |
| 482 | pass all hooks (locally-generated ICMP). Consider incoming |
| 483 | packet: PREROUTING (DST manip), routing produces ICMP, goes |
| 484 | through POSTROUTING (which must correct the DST manip). */ |
| 485 | if (!manip_pkt(inside->ip.protocol, pskb, |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame^] | 486 | ip_hdrlen(*pskb) + sizeof(inside->icmp), |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 487 | &ct->tuplehash[!dir].tuple, |
| 488 | !manip)) |
| 489 | return 0; |
| 490 | |
| 491 | if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) { |
| 492 | /* Reloading "inside" here since manip_pkt inner. */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame^] | 493 | inside = (void *)(*pskb)->data + ip_hdrlen(*pskb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 494 | inside->icmp.checksum = 0; |
| 495 | inside->icmp.checksum = |
| 496 | csum_fold(skb_checksum(*pskb, hdrlen, |
| 497 | (*pskb)->len - hdrlen, 0)); |
| 498 | } |
| 499 | |
| 500 | /* Change outer to look the reply to an incoming packet |
| 501 | * (proto 0 means don't invert per-proto part). */ |
| 502 | if (manip == IP_NAT_MANIP_SRC) |
| 503 | statusbit = IPS_SRC_NAT; |
| 504 | else |
| 505 | statusbit = IPS_DST_NAT; |
| 506 | |
| 507 | /* Invert if this is reply dir. */ |
| 508 | if (dir == IP_CT_DIR_REPLY) |
| 509 | statusbit ^= IPS_NAT_MASK; |
| 510 | |
| 511 | if (ct->status & statusbit) { |
| 512 | nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple); |
| 513 | if (!manip_pkt(0, pskb, 0, &target, manip)) |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | return 1; |
| 518 | } |
| 519 | EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation); |
| 520 | |
| 521 | /* Protocol registration. */ |
| 522 | int nf_nat_protocol_register(struct nf_nat_protocol *proto) |
| 523 | { |
| 524 | int ret = 0; |
| 525 | |
| 526 | write_lock_bh(&nf_nat_lock); |
| 527 | if (nf_nat_protos[proto->protonum] != &nf_nat_unknown_protocol) { |
| 528 | ret = -EBUSY; |
| 529 | goto out; |
| 530 | } |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 531 | rcu_assign_pointer(nf_nat_protos[proto->protonum], proto); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 532 | out: |
| 533 | write_unlock_bh(&nf_nat_lock); |
| 534 | return ret; |
| 535 | } |
| 536 | EXPORT_SYMBOL(nf_nat_protocol_register); |
| 537 | |
| 538 | /* Noone stores the protocol anywhere; simply delete it. */ |
| 539 | void nf_nat_protocol_unregister(struct nf_nat_protocol *proto) |
| 540 | { |
| 541 | write_lock_bh(&nf_nat_lock); |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 542 | rcu_assign_pointer(nf_nat_protos[proto->protonum], |
| 543 | &nf_nat_unknown_protocol); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 544 | write_unlock_bh(&nf_nat_lock); |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 545 | synchronize_rcu(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 546 | } |
| 547 | EXPORT_SYMBOL(nf_nat_protocol_unregister); |
| 548 | |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 549 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 550 | int |
| 551 | nf_nat_port_range_to_nfattr(struct sk_buff *skb, |
| 552 | const struct nf_nat_range *range) |
| 553 | { |
| 554 | NFA_PUT(skb, CTA_PROTONAT_PORT_MIN, sizeof(__be16), |
| 555 | &range->min.tcp.port); |
| 556 | NFA_PUT(skb, CTA_PROTONAT_PORT_MAX, sizeof(__be16), |
| 557 | &range->max.tcp.port); |
| 558 | |
| 559 | return 0; |
| 560 | |
| 561 | nfattr_failure: |
| 562 | return -1; |
| 563 | } |
| 564 | EXPORT_SYMBOL_GPL(nf_nat_port_nfattr_to_range); |
| 565 | |
| 566 | int |
| 567 | nf_nat_port_nfattr_to_range(struct nfattr *tb[], struct nf_nat_range *range) |
| 568 | { |
| 569 | int ret = 0; |
| 570 | |
| 571 | /* we have to return whether we actually parsed something or not */ |
| 572 | |
| 573 | if (tb[CTA_PROTONAT_PORT_MIN-1]) { |
| 574 | ret = 1; |
| 575 | range->min.tcp.port = |
| 576 | *(__be16 *)NFA_DATA(tb[CTA_PROTONAT_PORT_MIN-1]); |
| 577 | } |
| 578 | |
| 579 | if (!tb[CTA_PROTONAT_PORT_MAX-1]) { |
| 580 | if (ret) |
| 581 | range->max.tcp.port = range->min.tcp.port; |
| 582 | } else { |
| 583 | ret = 1; |
| 584 | range->max.tcp.port = |
| 585 | *(__be16 *)NFA_DATA(tb[CTA_PROTONAT_PORT_MAX-1]); |
| 586 | } |
| 587 | |
| 588 | return ret; |
| 589 | } |
| 590 | EXPORT_SYMBOL_GPL(nf_nat_port_range_to_nfattr); |
| 591 | #endif |
| 592 | |
| 593 | static int __init nf_nat_init(void) |
| 594 | { |
| 595 | size_t i; |
| 596 | |
| 597 | /* Leave them the same for the moment. */ |
| 598 | nf_nat_htable_size = nf_conntrack_htable_size; |
| 599 | |
| 600 | /* One vmalloc for both hash tables */ |
| 601 | bysource = vmalloc(sizeof(struct list_head) * nf_nat_htable_size); |
| 602 | if (!bysource) |
| 603 | return -ENOMEM; |
| 604 | |
| 605 | /* Sew in builtin protocols. */ |
| 606 | write_lock_bh(&nf_nat_lock); |
| 607 | for (i = 0; i < MAX_IP_NAT_PROTO; i++) |
Patrick McHardy | e22a054 | 2007-02-12 11:12:26 -0800 | [diff] [blame] | 608 | rcu_assign_pointer(nf_nat_protos[i], &nf_nat_unknown_protocol); |
| 609 | rcu_assign_pointer(nf_nat_protos[IPPROTO_TCP], &nf_nat_protocol_tcp); |
| 610 | rcu_assign_pointer(nf_nat_protos[IPPROTO_UDP], &nf_nat_protocol_udp); |
| 611 | rcu_assign_pointer(nf_nat_protos[IPPROTO_ICMP], &nf_nat_protocol_icmp); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 612 | write_unlock_bh(&nf_nat_lock); |
| 613 | |
| 614 | for (i = 0; i < nf_nat_htable_size; i++) { |
| 615 | INIT_LIST_HEAD(&bysource[i]); |
| 616 | } |
| 617 | |
| 618 | /* FIXME: Man, this is a hack. <SIGH> */ |
Patrick McHardy | 982d9a9 | 2007-02-12 11:14:11 -0800 | [diff] [blame] | 619 | NF_CT_ASSERT(rcu_dereference(nf_conntrack_destroyed) == NULL); |
| 620 | rcu_assign_pointer(nf_conntrack_destroyed, nf_nat_cleanup_conntrack); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 621 | |
| 622 | /* Initialize fake conntrack so that NAT will skip it */ |
| 623 | nf_conntrack_untracked.status |= IPS_NAT_DONE_MASK; |
| 624 | |
| 625 | l3proto = nf_ct_l3proto_find_get((u_int16_t)AF_INET); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /* Clear NAT section of all conntracks, in case we're loaded again. */ |
| 630 | static int clean_nat(struct nf_conn *i, void *data) |
| 631 | { |
| 632 | struct nf_conn_nat *nat = nfct_nat(i); |
| 633 | |
| 634 | if (!nat) |
| 635 | return 0; |
| 636 | memset(nat, 0, sizeof(nat)); |
| 637 | i->status &= ~(IPS_NAT_MASK | IPS_NAT_DONE_MASK | IPS_SEQ_ADJUST); |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static void __exit nf_nat_cleanup(void) |
| 642 | { |
| 643 | nf_ct_iterate_cleanup(&clean_nat, NULL); |
Patrick McHardy | 982d9a9 | 2007-02-12 11:14:11 -0800 | [diff] [blame] | 644 | rcu_assign_pointer(nf_conntrack_destroyed, NULL); |
| 645 | synchronize_rcu(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 646 | vfree(bysource); |
| 647 | nf_ct_l3proto_put(l3proto); |
| 648 | } |
| 649 | |
| 650 | MODULE_LICENSE("GPL"); |
| 651 | |
| 652 | module_init(nf_nat_init); |
| 653 | module_exit(nf_nat_cleanup); |