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