1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Daniel Hartmeier 5 * Copyright (c) 2002 - 2008 Henning Brauer 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer in the documentation and/or other materials provided 17 * with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * Effort sponsored in part by the Defense Advanced Research Projects 33 * Agency (DARPA) and Air Force Research Laboratory, Air Force 34 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 35 * 36 * $OpenBSD: pf_lb.c,v 1.2 2009/02/12 02:13:15 sthen Exp $ 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_pf.h" 43 #include "opt_inet.h" 44 #include "opt_inet6.h" 45 46 #include <sys/param.h> 47 #include <sys/lock.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/sysctl.h> 51 52 #include <net/if.h> 53 #include <net/vnet.h> 54 #include <net/pfvar.h> 55 #include <net/if_pflog.h> 56 57 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x 58 59 static void pf_hash(struct pf_addr *, struct pf_addr *, 60 struct pf_poolhashkey *, sa_family_t); 61 static struct pf_krule *pf_match_translation(struct pf_pdesc *, struct mbuf *, 62 int, int, struct pfi_kkif *, 63 struct pf_addr *, u_int16_t, struct pf_addr *, 64 uint16_t, int, struct pf_kanchor_stackframe *); 65 static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *, 66 struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *, 67 uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **); 68 69 #define mix(a,b,c) \ 70 do { \ 71 a -= b; a -= c; a ^= (c >> 13); \ 72 b -= c; b -= a; b ^= (a << 8); \ 73 c -= a; c -= b; c ^= (b >> 13); \ 74 a -= b; a -= c; a ^= (c >> 12); \ 75 b -= c; b -= a; b ^= (a << 16); \ 76 c -= a; c -= b; c ^= (b >> 5); \ 77 a -= b; a -= c; a ^= (c >> 3); \ 78 b -= c; b -= a; b ^= (a << 10); \ 79 c -= a; c -= b; c ^= (b >> 15); \ 80 } while (0) 81 82 /* 83 * hash function based on bridge_hash in if_bridge.c 84 */ 85 static void 86 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash, 87 struct pf_poolhashkey *key, sa_family_t af) 88 { 89 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0]; 90 91 switch (af) { 92 #ifdef INET 93 case AF_INET: 94 a += inaddr->addr32[0]; 95 b += key->key32[1]; 96 mix(a, b, c); 97 hash->addr32[0] = c + key->key32[2]; 98 break; 99 #endif /* INET */ 100 #ifdef INET6 101 case AF_INET6: 102 a += inaddr->addr32[0]; 103 b += inaddr->addr32[2]; 104 mix(a, b, c); 105 hash->addr32[0] = c; 106 a += inaddr->addr32[1]; 107 b += inaddr->addr32[3]; 108 c += key->key32[1]; 109 mix(a, b, c); 110 hash->addr32[1] = c; 111 a += inaddr->addr32[2]; 112 b += inaddr->addr32[1]; 113 c += key->key32[2]; 114 mix(a, b, c); 115 hash->addr32[2] = c; 116 a += inaddr->addr32[3]; 117 b += inaddr->addr32[0]; 118 c += key->key32[3]; 119 mix(a, b, c); 120 hash->addr32[3] = c; 121 break; 122 #endif /* INET6 */ 123 } 124 } 125 126 static struct pf_krule * 127 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off, 128 int direction, struct pfi_kkif *kif, struct pf_addr *saddr, u_int16_t sport, 129 struct pf_addr *daddr, uint16_t dport, int rs_num, 130 struct pf_kanchor_stackframe *anchor_stack) 131 { 132 struct pf_krule *r, *rm = NULL; 133 struct pf_kruleset *ruleset = NULL; 134 int tag = -1; 135 int rtableid = -1; 136 int asd = 0; 137 138 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr); 139 while (r != NULL) { 140 struct pf_rule_addr *src = NULL, *dst = NULL; 141 struct pf_addr_wrap *xdst = NULL; 142 143 if (r->action == PF_BINAT && direction == PF_IN) { 144 src = &r->dst; 145 if (r->rpool.cur != NULL) 146 xdst = &r->rpool.cur->addr; 147 } else { 148 src = &r->src; 149 dst = &r->dst; 150 } 151 152 pf_counter_u64_add(&r->evaluations, 1); 153 if (pfi_kkif_match(r->kif, kif) == r->ifnot) 154 r = r->skip[PF_SKIP_IFP].ptr; 155 else if (r->direction && r->direction != direction) 156 r = r->skip[PF_SKIP_DIR].ptr; 157 else if (r->af && r->af != pd->af) 158 r = r->skip[PF_SKIP_AF].ptr; 159 else if (r->proto && r->proto != pd->proto) 160 r = r->skip[PF_SKIP_PROTO].ptr; 161 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af, 162 src->neg, kif, M_GETFIB(m))) 163 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR : 164 PF_SKIP_DST_ADDR].ptr; 165 else if (src->port_op && !pf_match_port(src->port_op, 166 src->port[0], src->port[1], sport)) 167 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT : 168 PF_SKIP_DST_PORT].ptr; 169 else if (dst != NULL && 170 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL, 171 M_GETFIB(m))) 172 r = r->skip[PF_SKIP_DST_ADDR].ptr; 173 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af, 174 0, NULL, M_GETFIB(m))) 175 r = TAILQ_NEXT(r, entries); 176 else if (dst != NULL && dst->port_op && 177 !pf_match_port(dst->port_op, dst->port[0], 178 dst->port[1], dport)) 179 r = r->skip[PF_SKIP_DST_PORT].ptr; 180 else if (r->match_tag && !pf_match_tag(m, r, &tag, 181 pd->pf_mtag ? pd->pf_mtag->tag : 0)) 182 r = TAILQ_NEXT(r, entries); 183 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto != 184 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m, 185 off, &pd->hdr.tcp), r->os_fingerprint))) 186 r = TAILQ_NEXT(r, entries); 187 else { 188 if (r->tag) 189 tag = r->tag; 190 if (r->rtableid >= 0) 191 rtableid = r->rtableid; 192 if (r->anchor == NULL) { 193 rm = r; 194 if (rm->action == PF_NONAT || 195 rm->action == PF_NORDR || 196 rm->action == PF_NOBINAT) { 197 rm = NULL; 198 } 199 break; 200 } else 201 pf_step_into_anchor(anchor_stack, &asd, 202 &ruleset, rs_num, &r, NULL, NULL); 203 } 204 if (r == NULL) 205 pf_step_out_of_anchor(anchor_stack, &asd, &ruleset, 206 rs_num, &r, NULL, NULL); 207 } 208 209 if (tag > 0 && pf_tag_packet(m, pd, tag)) 210 return (NULL); 211 if (rtableid >= 0) 212 M_SETFIB(m, rtableid); 213 214 return (rm); 215 } 216 217 static int 218 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r, 219 struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr, 220 uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low, 221 uint16_t high, struct pf_ksrc_node **sn) 222 { 223 struct pf_state_key_cmp key; 224 struct pf_addr init_addr; 225 226 bzero(&init_addr, sizeof(init_addr)); 227 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn)) 228 return (1); 229 230 bzero(&key, sizeof(key)); 231 key.af = af; 232 key.proto = proto; 233 key.port[0] = dport; 234 PF_ACPY(&key.addr[0], daddr, key.af); 235 236 do { 237 PF_ACPY(&key.addr[1], naddr, key.af); 238 239 /* 240 * port search; start random, step; 241 * similar 2 portloop in in_pcbbind 242 */ 243 if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP || 244 proto == IPPROTO_ICMP) || (low == 0 && high == 0)) { 245 /* 246 * XXX bug: icmp states don't use the id on both sides. 247 * (traceroute -I through nat) 248 */ 249 key.port[1] = sport; 250 if (!pf_find_state_all_exists(&key, PF_IN)) { 251 *nport = sport; 252 return (0); 253 } 254 } else if (low == high) { 255 key.port[1] = htons(low); 256 if (!pf_find_state_all_exists(&key, PF_IN)) { 257 *nport = htons(low); 258 return (0); 259 } 260 } else { 261 uint32_t tmp; 262 uint16_t cut; 263 264 if (low > high) { 265 tmp = low; 266 low = high; 267 high = tmp; 268 } 269 /* low < high */ 270 cut = arc4random() % (1 + high - low) + low; 271 /* low <= cut <= high */ 272 for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) { 273 key.port[1] = htons(tmp); 274 if (!pf_find_state_all_exists(&key, PF_IN)) { 275 *nport = htons(tmp); 276 return (0); 277 } 278 } 279 tmp = cut; 280 for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) { 281 key.port[1] = htons(tmp); 282 if (!pf_find_state_all_exists(&key, PF_IN)) { 283 *nport = htons(tmp); 284 return (0); 285 } 286 } 287 } 288 289 switch (r->rpool.opts & PF_POOL_TYPEMASK) { 290 case PF_POOL_RANDOM: 291 case PF_POOL_ROUNDROBIN: 292 /* 293 * pick a different source address since we're out 294 * of free port choices for the current one. 295 */ 296 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn)) 297 return (1); 298 break; 299 case PF_POOL_NONE: 300 case PF_POOL_SRCHASH: 301 case PF_POOL_BITMASK: 302 default: 303 return (1); 304 } 305 } while (! PF_AEQ(&init_addr, naddr, af) ); 306 return (1); /* none available */ 307 } 308 309 static int 310 pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r, 311 struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr, 312 uint16_t dport, struct pf_addr *naddr, uint16_t *nport, 313 struct pf_ksrc_node **sn) 314 { 315 uint16_t psmask, low, highmask; 316 uint16_t i, ahigh, cut; 317 int ashift, psidshift; 318 319 ashift = 16 - r->rpool.mape.offset; 320 psidshift = ashift - r->rpool.mape.psidlen; 321 psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1); 322 psmask = psmask << psidshift; 323 highmask = (1U << psidshift) - 1; 324 325 ahigh = (1U << r->rpool.mape.offset) - 1; 326 cut = arc4random() & ahigh; 327 if (cut == 0) 328 cut = 1; 329 330 for (i = cut; i <= ahigh; i++) { 331 low = (i << ashift) | psmask; 332 if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport, 333 naddr, nport, low, low | highmask, sn)) 334 return (0); 335 } 336 for (i = cut - 1; i > 0; i--) { 337 low = (i << ashift) | psmask; 338 if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport, 339 naddr, nport, low, low | highmask, sn)) 340 return (0); 341 } 342 return (1); 343 } 344 345 int 346 pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr, 347 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_ksrc_node **sn) 348 { 349 struct pf_kpool *rpool = &r->rpool; 350 struct pf_addr *raddr = NULL, *rmask = NULL; 351 struct pf_srchash *sh = NULL; 352 353 /* Try to find a src_node if none was given and this 354 is a sticky-address rule. */ 355 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR && 356 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) 357 *sn = pf_find_src_node(saddr, r, af, &sh, false); 358 359 /* If a src_node was found or explicitly given and it has a non-zero 360 route address, use this address. A zeroed address is found if the 361 src node was created just a moment ago in pf_create_state and it 362 needs to be filled in with routing decision calculated here. */ 363 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) { 364 /* If the supplied address is the same as the current one we've 365 * been asked before, so tell the caller that there's no other 366 * address to be had. */ 367 if (PF_AEQ(naddr, &(*sn)->raddr, af)) 368 return (1); 369 370 PF_ACPY(naddr, &(*sn)->raddr, af); 371 if (V_pf_status.debug >= PF_DEBUG_NOISY) { 372 printf("pf_map_addr: src tracking maps "); 373 pf_print_host(saddr, 0, af); 374 printf(" to "); 375 pf_print_host(naddr, 0, af); 376 printf("\n"); 377 } 378 return (0); 379 } 380 381 mtx_lock(&rpool->mtx); 382 /* Find the route using chosen algorithm. Store the found route 383 in src_node if it was given or found. */ 384 if (rpool->cur->addr.type == PF_ADDR_NOROUTE) { 385 mtx_unlock(&rpool->mtx); 386 return (1); 387 } 388 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) { 389 switch (af) { 390 #ifdef INET 391 case AF_INET: 392 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 && 393 (rpool->opts & PF_POOL_TYPEMASK) != 394 PF_POOL_ROUNDROBIN) { 395 mtx_unlock(&rpool->mtx); 396 return (1); 397 } 398 raddr = &rpool->cur->addr.p.dyn->pfid_addr4; 399 rmask = &rpool->cur->addr.p.dyn->pfid_mask4; 400 break; 401 #endif /* INET */ 402 #ifdef INET6 403 case AF_INET6: 404 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 && 405 (rpool->opts & PF_POOL_TYPEMASK) != 406 PF_POOL_ROUNDROBIN) { 407 mtx_unlock(&rpool->mtx); 408 return (1); 409 } 410 raddr = &rpool->cur->addr.p.dyn->pfid_addr6; 411 rmask = &rpool->cur->addr.p.dyn->pfid_mask6; 412 break; 413 #endif /* INET6 */ 414 } 415 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) { 416 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN) { 417 mtx_unlock(&rpool->mtx); 418 return (1); /* unsupported */ 419 } 420 } else { 421 raddr = &rpool->cur->addr.v.a.addr; 422 rmask = &rpool->cur->addr.v.a.mask; 423 } 424 425 switch (rpool->opts & PF_POOL_TYPEMASK) { 426 case PF_POOL_NONE: 427 PF_ACPY(naddr, raddr, af); 428 break; 429 case PF_POOL_BITMASK: 430 PF_POOLMASK(naddr, raddr, rmask, saddr, af); 431 break; 432 case PF_POOL_RANDOM: 433 if (init_addr != NULL && PF_AZERO(init_addr, af)) { 434 switch (af) { 435 #ifdef INET 436 case AF_INET: 437 rpool->counter.addr32[0] = htonl(arc4random()); 438 break; 439 #endif /* INET */ 440 #ifdef INET6 441 case AF_INET6: 442 if (rmask->addr32[3] != 0xffffffff) 443 rpool->counter.addr32[3] = 444 htonl(arc4random()); 445 else 446 break; 447 if (rmask->addr32[2] != 0xffffffff) 448 rpool->counter.addr32[2] = 449 htonl(arc4random()); 450 else 451 break; 452 if (rmask->addr32[1] != 0xffffffff) 453 rpool->counter.addr32[1] = 454 htonl(arc4random()); 455 else 456 break; 457 if (rmask->addr32[0] != 0xffffffff) 458 rpool->counter.addr32[0] = 459 htonl(arc4random()); 460 break; 461 #endif /* INET6 */ 462 } 463 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af); 464 PF_ACPY(init_addr, naddr, af); 465 466 } else { 467 PF_AINC(&rpool->counter, af); 468 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af); 469 } 470 break; 471 case PF_POOL_SRCHASH: 472 { 473 unsigned char hash[16]; 474 475 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af); 476 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af); 477 break; 478 } 479 case PF_POOL_ROUNDROBIN: 480 { 481 struct pf_kpooladdr *acur = rpool->cur; 482 483 if (rpool->cur->addr.type == PF_ADDR_TABLE) { 484 if (!pfr_pool_get(rpool->cur->addr.p.tbl, 485 &rpool->tblidx, &rpool->counter, af)) 486 goto get_addr; 487 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) { 488 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt, 489 &rpool->tblidx, &rpool->counter, af)) 490 goto get_addr; 491 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af)) 492 goto get_addr; 493 494 try_next: 495 if (TAILQ_NEXT(rpool->cur, entries) == NULL) 496 rpool->cur = TAILQ_FIRST(&rpool->list); 497 else 498 rpool->cur = TAILQ_NEXT(rpool->cur, entries); 499 if (rpool->cur->addr.type == PF_ADDR_TABLE) { 500 rpool->tblidx = -1; 501 if (pfr_pool_get(rpool->cur->addr.p.tbl, 502 &rpool->tblidx, &rpool->counter, af)) { 503 /* table contains no address of type 'af' */ 504 if (rpool->cur != acur) 505 goto try_next; 506 mtx_unlock(&rpool->mtx); 507 return (1); 508 } 509 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) { 510 rpool->tblidx = -1; 511 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt, 512 &rpool->tblidx, &rpool->counter, af)) { 513 /* table contains no address of type 'af' */ 514 if (rpool->cur != acur) 515 goto try_next; 516 mtx_unlock(&rpool->mtx); 517 return (1); 518 } 519 } else { 520 raddr = &rpool->cur->addr.v.a.addr; 521 rmask = &rpool->cur->addr.v.a.mask; 522 PF_ACPY(&rpool->counter, raddr, af); 523 } 524 525 get_addr: 526 PF_ACPY(naddr, &rpool->counter, af); 527 if (init_addr != NULL && PF_AZERO(init_addr, af)) 528 PF_ACPY(init_addr, naddr, af); 529 PF_AINC(&rpool->counter, af); 530 break; 531 } 532 } 533 if (*sn != NULL) 534 PF_ACPY(&(*sn)->raddr, naddr, af); 535 536 mtx_unlock(&rpool->mtx); 537 538 if (V_pf_status.debug >= PF_DEBUG_NOISY && 539 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) { 540 printf("pf_map_addr: selected address "); 541 pf_print_host(naddr, 0, af); 542 printf("\n"); 543 } 544 545 return (0); 546 } 547 548 struct pf_krule * 549 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction, 550 struct pfi_kkif *kif, struct pf_ksrc_node **sn, 551 struct pf_state_key **skp, struct pf_state_key **nkp, 552 struct pf_addr *saddr, struct pf_addr *daddr, 553 uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack) 554 { 555 struct pf_krule *r = NULL; 556 struct pf_addr *naddr; 557 uint16_t *nport; 558 uint16_t low, high; 559 560 PF_RULES_RASSERT(); 561 KASSERT(*skp == NULL, ("*skp not NULL")); 562 KASSERT(*nkp == NULL, ("*nkp not NULL")); 563 564 if (direction == PF_OUT) { 565 r = pf_match_translation(pd, m, off, direction, kif, saddr, 566 sport, daddr, dport, PF_RULESET_BINAT, anchor_stack); 567 if (r == NULL) 568 r = pf_match_translation(pd, m, off, direction, kif, 569 saddr, sport, daddr, dport, PF_RULESET_NAT, 570 anchor_stack); 571 } else { 572 r = pf_match_translation(pd, m, off, direction, kif, saddr, 573 sport, daddr, dport, PF_RULESET_RDR, anchor_stack); 574 if (r == NULL) 575 r = pf_match_translation(pd, m, off, direction, kif, 576 saddr, sport, daddr, dport, PF_RULESET_BINAT, 577 anchor_stack); 578 } 579 580 if (r == NULL) 581 return (NULL); 582 583 switch (r->action) { 584 case PF_NONAT: 585 case PF_NOBINAT: 586 case PF_NORDR: 587 return (NULL); 588 } 589 590 *skp = pf_state_key_setup(pd, saddr, daddr, sport, dport); 591 if (*skp == NULL) 592 return (NULL); 593 *nkp = pf_state_key_clone(*skp); 594 if (*nkp == NULL) { 595 uma_zfree(V_pf_state_key_z, *skp); 596 *skp = NULL; 597 return (NULL); 598 } 599 600 /* XXX We only modify one side for now. */ 601 naddr = &(*nkp)->addr[1]; 602 nport = &(*nkp)->port[1]; 603 604 switch (r->action) { 605 case PF_NAT: 606 if (pd->proto == IPPROTO_ICMP) { 607 low = 1; 608 high = 65535; 609 } else { 610 low = r->rpool.proxy_port[0]; 611 high = r->rpool.proxy_port[1]; 612 } 613 if (r->rpool.mape.offset > 0) { 614 if (pf_get_mape_sport(pd->af, pd->proto, r, saddr, 615 sport, daddr, dport, naddr, nport, sn)) { 616 DPFPRINTF(PF_DEBUG_MISC, 617 ("pf: MAP-E port allocation (%u/%u/%u)" 618 " failed\n", 619 r->rpool.mape.offset, 620 r->rpool.mape.psidlen, 621 r->rpool.mape.psid)); 622 goto notrans; 623 } 624 } else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, 625 daddr, dport, naddr, nport, low, high, sn)) { 626 DPFPRINTF(PF_DEBUG_MISC, 627 ("pf: NAT proxy port allocation (%u-%u) failed\n", 628 r->rpool.proxy_port[0], r->rpool.proxy_port[1])); 629 goto notrans; 630 } 631 break; 632 case PF_BINAT: 633 switch (direction) { 634 case PF_OUT: 635 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){ 636 switch (pd->af) { 637 #ifdef INET 638 case AF_INET: 639 if (r->rpool.cur->addr.p.dyn-> 640 pfid_acnt4 < 1) 641 goto notrans; 642 PF_POOLMASK(naddr, 643 &r->rpool.cur->addr.p.dyn-> 644 pfid_addr4, 645 &r->rpool.cur->addr.p.dyn-> 646 pfid_mask4, saddr, AF_INET); 647 break; 648 #endif /* INET */ 649 #ifdef INET6 650 case AF_INET6: 651 if (r->rpool.cur->addr.p.dyn-> 652 pfid_acnt6 < 1) 653 goto notrans; 654 PF_POOLMASK(naddr, 655 &r->rpool.cur->addr.p.dyn-> 656 pfid_addr6, 657 &r->rpool.cur->addr.p.dyn-> 658 pfid_mask6, saddr, AF_INET6); 659 break; 660 #endif /* INET6 */ 661 } 662 } else 663 PF_POOLMASK(naddr, 664 &r->rpool.cur->addr.v.a.addr, 665 &r->rpool.cur->addr.v.a.mask, saddr, 666 pd->af); 667 break; 668 case PF_IN: 669 if (r->src.addr.type == PF_ADDR_DYNIFTL) { 670 switch (pd->af) { 671 #ifdef INET 672 case AF_INET: 673 if (r->src.addr.p.dyn-> pfid_acnt4 < 1) 674 goto notrans; 675 PF_POOLMASK(naddr, 676 &r->src.addr.p.dyn->pfid_addr4, 677 &r->src.addr.p.dyn->pfid_mask4, 678 daddr, AF_INET); 679 break; 680 #endif /* INET */ 681 #ifdef INET6 682 case AF_INET6: 683 if (r->src.addr.p.dyn->pfid_acnt6 < 1) 684 goto notrans; 685 PF_POOLMASK(naddr, 686 &r->src.addr.p.dyn->pfid_addr6, 687 &r->src.addr.p.dyn->pfid_mask6, 688 daddr, AF_INET6); 689 break; 690 #endif /* INET6 */ 691 } 692 } else 693 PF_POOLMASK(naddr, &r->src.addr.v.a.addr, 694 &r->src.addr.v.a.mask, daddr, pd->af); 695 break; 696 } 697 break; 698 case PF_RDR: { 699 if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn)) 700 goto notrans; 701 if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK) 702 PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask, 703 daddr, pd->af); 704 705 if (r->rpool.proxy_port[1]) { 706 uint32_t tmp_nport; 707 708 tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) % 709 (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] + 710 1)) + r->rpool.proxy_port[0]; 711 712 /* Wrap around if necessary. */ 713 if (tmp_nport > 65535) 714 tmp_nport -= 65535; 715 *nport = htons((uint16_t)tmp_nport); 716 } else if (r->rpool.proxy_port[0]) 717 *nport = htons(r->rpool.proxy_port[0]); 718 break; 719 } 720 default: 721 panic("%s: unknown action %u", __func__, r->action); 722 } 723 724 /* Return success only if translation really happened. */ 725 if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp))) 726 return (r); 727 728 notrans: 729 uma_zfree(V_pf_state_key_z, *nkp); 730 uma_zfree(V_pf_state_key_z, *skp); 731 *skp = *nkp = NULL; 732 *sn = NULL; 733 734 return (NULL); 735 } 736