xref: /freebsd/sys/netpfil/pf/pf_lb.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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 && rm == 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 			} else
195 				pf_step_into_anchor(anchor_stack, &asd,
196 				    &ruleset, rs_num, &r, NULL, NULL);
197 		}
198 		if (r == NULL)
199 			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
200 			    rs_num, &r, NULL, NULL);
201 	}
202 
203 	if (tag > 0 && pf_tag_packet(m, pd, tag))
204 		return (NULL);
205 	if (rtableid >= 0)
206 		M_SETFIB(m, rtableid);
207 
208 	if (rm != NULL && (rm->action == PF_NONAT ||
209 	    rm->action == PF_NORDR || rm->action == PF_NOBINAT))
210 		return (NULL);
211 	return (rm);
212 }
213 
214 static int
215 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
216     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
217     uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
218     uint16_t high, struct pf_ksrc_node **sn)
219 {
220 	struct pf_state_key_cmp	key;
221 	struct pf_addr		init_addr;
222 
223 	bzero(&init_addr, sizeof(init_addr));
224 	if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
225 		return (1);
226 
227 	bzero(&key, sizeof(key));
228 	key.af = af;
229 	key.proto = proto;
230 	key.port[0] = dport;
231 	PF_ACPY(&key.addr[0], daddr, key.af);
232 
233 	do {
234 		PF_ACPY(&key.addr[1], naddr, key.af);
235 
236 		/*
237 		 * port search; start random, step;
238 		 * similar 2 portloop in in_pcbbind
239 		 */
240 		if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
241 		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
242 			/*
243 			 * XXX bug: icmp states don't use the id on both sides.
244 			 * (traceroute -I through nat)
245 			 */
246 			key.port[1] = sport;
247 			if (!pf_find_state_all_exists(&key, PF_IN)) {
248 				*nport = sport;
249 				return (0);
250 			}
251 		} else if (low == high) {
252 			key.port[1] = htons(low);
253 			if (!pf_find_state_all_exists(&key, PF_IN)) {
254 				*nport = htons(low);
255 				return (0);
256 			}
257 		} else {
258 			uint32_t tmp;
259 			uint16_t cut;
260 
261 			if (low > high) {
262 				tmp = low;
263 				low = high;
264 				high = tmp;
265 			}
266 			/* low < high */
267 			cut = arc4random() % (1 + high - low) + low;
268 			/* low <= cut <= high */
269 			for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
270 				key.port[1] = htons(tmp);
271 				if (!pf_find_state_all_exists(&key, PF_IN)) {
272 					*nport = htons(tmp);
273 					return (0);
274 				}
275 			}
276 			tmp = cut;
277 			for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
278 				key.port[1] = htons(tmp);
279 				if (!pf_find_state_all_exists(&key, PF_IN)) {
280 					*nport = htons(tmp);
281 					return (0);
282 				}
283 			}
284 		}
285 
286 		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
287 		case PF_POOL_RANDOM:
288 		case PF_POOL_ROUNDROBIN:
289 			/*
290 			 * pick a different source address since we're out
291 			 * of free port choices for the current one.
292 			 */
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 static int
307 pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
308     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
309     uint16_t dport, struct pf_addr *naddr, uint16_t *nport,
310     struct pf_ksrc_node **sn)
311 {
312 	uint16_t psmask, low, highmask;
313 	uint16_t i, ahigh, cut;
314 	int ashift, psidshift;
315 
316 	ashift = 16 - r->rpool.mape.offset;
317 	psidshift = ashift - r->rpool.mape.psidlen;
318 	psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1);
319 	psmask = psmask << psidshift;
320 	highmask = (1U << psidshift) - 1;
321 
322 	ahigh = (1U << r->rpool.mape.offset) - 1;
323 	cut = arc4random() & ahigh;
324 	if (cut == 0)
325 		cut = 1;
326 
327 	for (i = cut; i <= ahigh; i++) {
328 		low = (i << ashift) | psmask;
329 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
330 		    naddr, nport, low, low | highmask, sn))
331 			return (0);
332 	}
333 	for (i = cut - 1; i > 0; i--) {
334 		low = (i << ashift) | psmask;
335 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
336 		    naddr, nport, low, low | highmask, sn))
337 			return (0);
338 	}
339 	return (1);
340 }
341 
342 int
343 pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
344     struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_ksrc_node **sn)
345 {
346 	struct pf_kpool		*rpool = &r->rpool;
347 	struct pf_addr		*raddr = NULL, *rmask = NULL;
348 
349 	/* Try to find a src_node if none was given and this
350 	   is a sticky-address rule. */
351 	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
352 	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
353 		*sn = pf_find_src_node(saddr, r, af, 0);
354 
355 	/* If a src_node was found or explicitly given and it has a non-zero
356 	   route address, use this address. A zeroed address is found if the
357 	   src node was created just a moment ago in pf_create_state and it
358 	   needs to be filled in with routing decision calculated here. */
359 	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
360 		/* If the supplied address is the same as the current one we've
361 		 * been asked before, so tell the caller that there's no other
362 		 * address to be had. */
363 		if (PF_AEQ(naddr, &(*sn)->raddr, af))
364 			return (1);
365 
366 		PF_ACPY(naddr, &(*sn)->raddr, af);
367 		if (V_pf_status.debug >= PF_DEBUG_NOISY) {
368 			printf("pf_map_addr: src tracking maps ");
369 			pf_print_host(saddr, 0, af);
370 			printf(" to ");
371 			pf_print_host(naddr, 0, af);
372 			printf("\n");
373 		}
374 		return (0);
375 	}
376 
377 	mtx_lock(&rpool->mtx);
378 	/* Find the route using chosen algorithm. Store the found route
379 	   in src_node if it was given or found. */
380 	if (rpool->cur->addr.type == PF_ADDR_NOROUTE) {
381 		mtx_unlock(&rpool->mtx);
382 		return (1);
383 	}
384 	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
385 		switch (af) {
386 #ifdef INET
387 		case AF_INET:
388 			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
389 			    (rpool->opts & PF_POOL_TYPEMASK) !=
390 			    PF_POOL_ROUNDROBIN) {
391 				mtx_unlock(&rpool->mtx);
392 				return (1);
393 			}
394 			raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
395 			rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
396 			break;
397 #endif /* INET */
398 #ifdef INET6
399 		case AF_INET6:
400 			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
401 			    (rpool->opts & PF_POOL_TYPEMASK) !=
402 			    PF_POOL_ROUNDROBIN) {
403 				mtx_unlock(&rpool->mtx);
404 				return (1);
405 			}
406 			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
407 			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
408 			break;
409 #endif /* INET6 */
410 		}
411 	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
412 		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN) {
413 			mtx_unlock(&rpool->mtx);
414 			return (1); /* unsupported */
415 		}
416 	} else {
417 		raddr = &rpool->cur->addr.v.a.addr;
418 		rmask = &rpool->cur->addr.v.a.mask;
419 	}
420 
421 	switch (rpool->opts & PF_POOL_TYPEMASK) {
422 	case PF_POOL_NONE:
423 		PF_ACPY(naddr, raddr, af);
424 		break;
425 	case PF_POOL_BITMASK:
426 		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
427 		break;
428 	case PF_POOL_RANDOM:
429 		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
430 			switch (af) {
431 #ifdef INET
432 			case AF_INET:
433 				rpool->counter.addr32[0] = htonl(arc4random());
434 				break;
435 #endif /* INET */
436 #ifdef INET6
437 			case AF_INET6:
438 				if (rmask->addr32[3] != 0xffffffff)
439 					rpool->counter.addr32[3] =
440 					    htonl(arc4random());
441 				else
442 					break;
443 				if (rmask->addr32[2] != 0xffffffff)
444 					rpool->counter.addr32[2] =
445 					    htonl(arc4random());
446 				else
447 					break;
448 				if (rmask->addr32[1] != 0xffffffff)
449 					rpool->counter.addr32[1] =
450 					    htonl(arc4random());
451 				else
452 					break;
453 				if (rmask->addr32[0] != 0xffffffff)
454 					rpool->counter.addr32[0] =
455 					    htonl(arc4random());
456 				break;
457 #endif /* INET6 */
458 			}
459 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
460 			PF_ACPY(init_addr, naddr, af);
461 
462 		} else {
463 			PF_AINC(&rpool->counter, af);
464 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
465 		}
466 		break;
467 	case PF_POOL_SRCHASH:
468 	    {
469 		unsigned char hash[16];
470 
471 		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
472 		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
473 		break;
474 	    }
475 	case PF_POOL_ROUNDROBIN:
476 	    {
477 		struct pf_kpooladdr *acur = rpool->cur;
478 
479 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
480 			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
481 			    &rpool->tblidx, &rpool->counter, af))
482 				goto get_addr;
483 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
484 			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
485 			    &rpool->tblidx, &rpool->counter, af))
486 				goto get_addr;
487 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
488 			goto get_addr;
489 
490 	try_next:
491 		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
492 			rpool->cur = TAILQ_FIRST(&rpool->list);
493 		else
494 			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
495 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
496 			rpool->tblidx = -1;
497 			if (pfr_pool_get(rpool->cur->addr.p.tbl,
498 			    &rpool->tblidx, &rpool->counter, af)) {
499 				/* table contains no address of type 'af' */
500 				if (rpool->cur != acur)
501 					goto try_next;
502 				mtx_unlock(&rpool->mtx);
503 				return (1);
504 			}
505 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
506 			rpool->tblidx = -1;
507 			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
508 			    &rpool->tblidx, &rpool->counter, af)) {
509 				/* table contains no address of type 'af' */
510 				if (rpool->cur != acur)
511 					goto try_next;
512 				mtx_unlock(&rpool->mtx);
513 				return (1);
514 			}
515 		} else {
516 			raddr = &rpool->cur->addr.v.a.addr;
517 			rmask = &rpool->cur->addr.v.a.mask;
518 			PF_ACPY(&rpool->counter, raddr, af);
519 		}
520 
521 	get_addr:
522 		PF_ACPY(naddr, &rpool->counter, af);
523 		if (init_addr != NULL && PF_AZERO(init_addr, af))
524 			PF_ACPY(init_addr, naddr, af);
525 		PF_AINC(&rpool->counter, af);
526 		break;
527 	    }
528 	}
529 	if (*sn != NULL)
530 		PF_ACPY(&(*sn)->raddr, naddr, af);
531 
532 	mtx_unlock(&rpool->mtx);
533 
534 	if (V_pf_status.debug >= PF_DEBUG_NOISY &&
535 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
536 		printf("pf_map_addr: selected address ");
537 		pf_print_host(naddr, 0, af);
538 		printf("\n");
539 	}
540 
541 	return (0);
542 }
543 
544 struct pf_krule *
545 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
546     struct pfi_kkif *kif, struct pf_ksrc_node **sn,
547     struct pf_state_key **skp, struct pf_state_key **nkp,
548     struct pf_addr *saddr, struct pf_addr *daddr,
549     uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack)
550 {
551 	struct pf_krule	*r = NULL;
552 	struct pf_addr	*naddr;
553 	uint16_t	*nport;
554 	uint16_t	 low, high;
555 
556 	PF_RULES_RASSERT();
557 	KASSERT(*skp == NULL, ("*skp not NULL"));
558 	KASSERT(*nkp == NULL, ("*nkp not NULL"));
559 
560 	if (direction == PF_OUT) {
561 		r = pf_match_translation(pd, m, off, direction, kif, saddr,
562 		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
563 		if (r == NULL)
564 			r = pf_match_translation(pd, m, off, direction, kif,
565 			    saddr, sport, daddr, dport, PF_RULESET_NAT,
566 			    anchor_stack);
567 	} else {
568 		r = pf_match_translation(pd, m, off, direction, kif, saddr,
569 		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
570 		if (r == NULL)
571 			r = pf_match_translation(pd, m, off, direction, kif,
572 			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
573 			    anchor_stack);
574 	}
575 
576 	if (r == NULL)
577 		return (NULL);
578 
579 	switch (r->action) {
580 	case PF_NONAT:
581 	case PF_NOBINAT:
582 	case PF_NORDR:
583 		return (NULL);
584 	}
585 
586 	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
587 	if (*skp == NULL)
588 		return (NULL);
589 	*nkp = pf_state_key_clone(*skp);
590 	if (*nkp == NULL) {
591 		uma_zfree(V_pf_state_key_z, *skp);
592 		*skp = NULL;
593 		return (NULL);
594 	}
595 
596 	/* XXX We only modify one side for now. */
597 	naddr = &(*nkp)->addr[1];
598 	nport = &(*nkp)->port[1];
599 
600 	switch (r->action) {
601 	case PF_NAT:
602 		if (pd->proto == IPPROTO_ICMP) {
603 			low  = 1;
604 			high = 65535;
605 		} else {
606 			low  = r->rpool.proxy_port[0];
607 			high = r->rpool.proxy_port[1];
608 		}
609 		if (r->rpool.mape.offset > 0) {
610 			if (pf_get_mape_sport(pd->af, pd->proto, r, saddr,
611 			    sport, daddr, dport, naddr, nport, sn)) {
612 				DPFPRINTF(PF_DEBUG_MISC,
613 				    ("pf: MAP-E port allocation (%u/%u/%u)"
614 				    " failed\n",
615 				    r->rpool.mape.offset,
616 				    r->rpool.mape.psidlen,
617 				    r->rpool.mape.psid));
618 				goto notrans;
619 			}
620 		} else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport,
621 		    daddr, dport, naddr, nport, low, high, sn)) {
622 			DPFPRINTF(PF_DEBUG_MISC,
623 			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
624 			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
625 			goto notrans;
626 		}
627 		break;
628 	case PF_BINAT:
629 		switch (direction) {
630 		case PF_OUT:
631 			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
632 				switch (pd->af) {
633 #ifdef INET
634 				case AF_INET:
635 					if (r->rpool.cur->addr.p.dyn->
636 					    pfid_acnt4 < 1)
637 						goto notrans;
638 					PF_POOLMASK(naddr,
639 					    &r->rpool.cur->addr.p.dyn->
640 					    pfid_addr4,
641 					    &r->rpool.cur->addr.p.dyn->
642 					    pfid_mask4, saddr, AF_INET);
643 					break;
644 #endif /* INET */
645 #ifdef INET6
646 				case AF_INET6:
647 					if (r->rpool.cur->addr.p.dyn->
648 					    pfid_acnt6 < 1)
649 						goto notrans;
650 					PF_POOLMASK(naddr,
651 					    &r->rpool.cur->addr.p.dyn->
652 					    pfid_addr6,
653 					    &r->rpool.cur->addr.p.dyn->
654 					    pfid_mask6, saddr, AF_INET6);
655 					break;
656 #endif /* INET6 */
657 				}
658 			} else
659 				PF_POOLMASK(naddr,
660 				    &r->rpool.cur->addr.v.a.addr,
661 				    &r->rpool.cur->addr.v.a.mask, saddr,
662 				    pd->af);
663 			break;
664 		case PF_IN:
665 			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
666 				switch (pd->af) {
667 #ifdef INET
668 				case AF_INET:
669 					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
670 						goto notrans;
671 					PF_POOLMASK(naddr,
672 					    &r->src.addr.p.dyn->pfid_addr4,
673 					    &r->src.addr.p.dyn->pfid_mask4,
674 					    daddr, AF_INET);
675 					break;
676 #endif /* INET */
677 #ifdef INET6
678 				case AF_INET6:
679 					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
680 						goto notrans;
681 					PF_POOLMASK(naddr,
682 					    &r->src.addr.p.dyn->pfid_addr6,
683 					    &r->src.addr.p.dyn->pfid_mask6,
684 					    daddr, AF_INET6);
685 					break;
686 #endif /* INET6 */
687 				}
688 			} else
689 				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
690 				    &r->src.addr.v.a.mask, daddr, pd->af);
691 			break;
692 		}
693 		break;
694 	case PF_RDR: {
695 		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
696 			goto notrans;
697 		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
698 			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
699 			    daddr, pd->af);
700 
701 		if (r->rpool.proxy_port[1]) {
702 			uint32_t	tmp_nport;
703 
704 			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
705 			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
706 			    1)) + r->rpool.proxy_port[0];
707 
708 			/* Wrap around if necessary. */
709 			if (tmp_nport > 65535)
710 				tmp_nport -= 65535;
711 			*nport = htons((uint16_t)tmp_nport);
712 		} else if (r->rpool.proxy_port[0])
713 			*nport = htons(r->rpool.proxy_port[0]);
714 		break;
715 	}
716 	default:
717 		panic("%s: unknown action %u", __func__, r->action);
718 	}
719 
720 	/* Return success only if translation really happened. */
721 	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
722 		return (r);
723 
724 notrans:
725 	uma_zfree(V_pf_state_key_z, *nkp);
726 	uma_zfree(V_pf_state_key_z, *skp);
727 	*skp = *nkp = NULL;
728 	*sn = NULL;
729 
730 	return (NULL);
731 }
732