xref: /freebsd/sys/netpfil/pf/pf_lb.c (revision e85343b1a5ff17afe460541c8bffaaa16e5c0962)
1d8aa10ccSGleb Smirnoff /*-
23b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
33b3a8eb9SGleb Smirnoff  * Copyright (c) 2002 - 2008 Henning Brauer
43b3a8eb9SGleb Smirnoff  * All rights reserved.
53b3a8eb9SGleb Smirnoff  *
63b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
73b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
83b3a8eb9SGleb Smirnoff  * are met:
93b3a8eb9SGleb Smirnoff  *
103b3a8eb9SGleb Smirnoff  *    - Redistributions of source code must retain the above copyright
113b3a8eb9SGleb Smirnoff  *      notice, this list of conditions and the following disclaimer.
123b3a8eb9SGleb Smirnoff  *    - Redistributions in binary form must reproduce the above
133b3a8eb9SGleb Smirnoff  *      copyright notice, this list of conditions and the following
143b3a8eb9SGleb Smirnoff  *      disclaimer in the documentation and/or other materials provided
153b3a8eb9SGleb Smirnoff  *      with the distribution.
163b3a8eb9SGleb Smirnoff  *
173b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
183b3a8eb9SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
193b3a8eb9SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
203b3a8eb9SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
213b3a8eb9SGleb Smirnoff  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
223b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
233b3a8eb9SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
243b3a8eb9SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
253b3a8eb9SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
273b3a8eb9SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
283b3a8eb9SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
293b3a8eb9SGleb Smirnoff  *
303b3a8eb9SGleb Smirnoff  * Effort sponsored in part by the Defense Advanced Research Projects
313b3a8eb9SGleb Smirnoff  * Agency (DARPA) and Air Force Research Laboratory, Air Force
323b3a8eb9SGleb Smirnoff  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
333b3a8eb9SGleb Smirnoff  *
34d8aa10ccSGleb Smirnoff  *	$OpenBSD: pf_lb.c,v 1.2 2009/02/12 02:13:15 sthen Exp $
353b3a8eb9SGleb Smirnoff  */
363b3a8eb9SGleb Smirnoff 
373b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
383b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$");
393b3a8eb9SGleb Smirnoff 
403b3a8eb9SGleb Smirnoff #include "opt_pf.h"
413b3a8eb9SGleb Smirnoff #include "opt_inet.h"
423b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
433b3a8eb9SGleb Smirnoff 
443b3a8eb9SGleb Smirnoff #include <sys/param.h>
4576039bc8SGleb Smirnoff #include <sys/lock.h>
4676039bc8SGleb Smirnoff #include <sys/mbuf.h>
4776039bc8SGleb Smirnoff #include <sys/rwlock.h>
483b3a8eb9SGleb Smirnoff #include <sys/socket.h>
493b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
503b3a8eb9SGleb Smirnoff 
513b3a8eb9SGleb Smirnoff #include <net/if.h>
5276039bc8SGleb Smirnoff #include <net/vnet.h>
533b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
543b3a8eb9SGleb Smirnoff #include <net/if_pflog.h>
553b3a8eb9SGleb Smirnoff 
563b3a8eb9SGleb Smirnoff #define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
573b3a8eb9SGleb Smirnoff 
583b3a8eb9SGleb Smirnoff static void		 pf_hash(struct pf_addr *, struct pf_addr *,
593b3a8eb9SGleb Smirnoff 			    struct pf_poolhashkey *, sa_family_t);
603b3a8eb9SGleb Smirnoff static struct pf_rule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
613b3a8eb9SGleb Smirnoff 			    int, int, struct pfi_kif *,
623b3a8eb9SGleb Smirnoff 			    struct pf_addr *, u_int16_t, struct pf_addr *,
631d6139c0SGleb Smirnoff 			    uint16_t, int, struct pf_anchor_stackframe *);
648fc6e19cSGleb Smirnoff static int pf_get_sport(sa_family_t, uint8_t, struct pf_rule *,
658fc6e19cSGleb Smirnoff     struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
668fc6e19cSGleb Smirnoff     uint16_t *, uint16_t, uint16_t, struct pf_src_node **);
673b3a8eb9SGleb Smirnoff 
683b3a8eb9SGleb Smirnoff #define mix(a,b,c) \
693b3a8eb9SGleb Smirnoff 	do {					\
703b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 13);	\
713b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 8);	\
723b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 13);	\
733b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 12);	\
743b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 16);	\
753b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 5);	\
763b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 3);	\
773b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 10);	\
783b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 15);	\
793b3a8eb9SGleb Smirnoff 	} while (0)
803b3a8eb9SGleb Smirnoff 
813b3a8eb9SGleb Smirnoff /*
823b3a8eb9SGleb Smirnoff  * hash function based on bridge_hash in if_bridge.c
833b3a8eb9SGleb Smirnoff  */
843b3a8eb9SGleb Smirnoff static void
853b3a8eb9SGleb Smirnoff pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
863b3a8eb9SGleb Smirnoff     struct pf_poolhashkey *key, sa_family_t af)
873b3a8eb9SGleb Smirnoff {
883b3a8eb9SGleb Smirnoff 	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
893b3a8eb9SGleb Smirnoff 
903b3a8eb9SGleb Smirnoff 	switch (af) {
913b3a8eb9SGleb Smirnoff #ifdef INET
923b3a8eb9SGleb Smirnoff 	case AF_INET:
933b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
943b3a8eb9SGleb Smirnoff 		b += key->key32[1];
953b3a8eb9SGleb Smirnoff 		mix(a, b, c);
963b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c + key->key32[2];
973b3a8eb9SGleb Smirnoff 		break;
983b3a8eb9SGleb Smirnoff #endif /* INET */
993b3a8eb9SGleb Smirnoff #ifdef INET6
1003b3a8eb9SGleb Smirnoff 	case AF_INET6:
1013b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
1023b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[2];
1033b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1043b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c;
1053b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[1];
1063b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[3];
1073b3a8eb9SGleb Smirnoff 		c += key->key32[1];
1083b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1093b3a8eb9SGleb Smirnoff 		hash->addr32[1] = c;
1103b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[2];
1113b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[1];
1123b3a8eb9SGleb Smirnoff 		c += key->key32[2];
1133b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1143b3a8eb9SGleb Smirnoff 		hash->addr32[2] = c;
1153b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[3];
1163b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[0];
1173b3a8eb9SGleb Smirnoff 		c += key->key32[3];
1183b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1193b3a8eb9SGleb Smirnoff 		hash->addr32[3] = c;
1203b3a8eb9SGleb Smirnoff 		break;
1213b3a8eb9SGleb Smirnoff #endif /* INET6 */
1223b3a8eb9SGleb Smirnoff 	}
1233b3a8eb9SGleb Smirnoff }
1243b3a8eb9SGleb Smirnoff 
1253b3a8eb9SGleb Smirnoff static struct pf_rule *
1263b3a8eb9SGleb Smirnoff pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
1273b3a8eb9SGleb Smirnoff     int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
1281d6139c0SGleb Smirnoff     struct pf_addr *daddr, uint16_t dport, int rs_num,
1291d6139c0SGleb Smirnoff     struct pf_anchor_stackframe *anchor_stack)
1303b3a8eb9SGleb Smirnoff {
1313b3a8eb9SGleb Smirnoff 	struct pf_rule		*r, *rm = NULL;
1323b3a8eb9SGleb Smirnoff 	struct pf_ruleset	*ruleset = NULL;
1333b3a8eb9SGleb Smirnoff 	int			 tag = -1;
1343b3a8eb9SGleb Smirnoff 	int			 rtableid = -1;
1353b3a8eb9SGleb Smirnoff 	int			 asd = 0;
1363b3a8eb9SGleb Smirnoff 
1373b3a8eb9SGleb Smirnoff 	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1383b3a8eb9SGleb Smirnoff 	while (r && rm == NULL) {
1393b3a8eb9SGleb Smirnoff 		struct pf_rule_addr	*src = NULL, *dst = NULL;
1403b3a8eb9SGleb Smirnoff 		struct pf_addr_wrap	*xdst = NULL;
1413b3a8eb9SGleb Smirnoff 
1423b3a8eb9SGleb Smirnoff 		if (r->action == PF_BINAT && direction == PF_IN) {
1433b3a8eb9SGleb Smirnoff 			src = &r->dst;
1443b3a8eb9SGleb Smirnoff 			if (r->rpool.cur != NULL)
1453b3a8eb9SGleb Smirnoff 				xdst = &r->rpool.cur->addr;
1463b3a8eb9SGleb Smirnoff 		} else {
1473b3a8eb9SGleb Smirnoff 			src = &r->src;
1483b3a8eb9SGleb Smirnoff 			dst = &r->dst;
1493b3a8eb9SGleb Smirnoff 		}
1503b3a8eb9SGleb Smirnoff 
1513b3a8eb9SGleb Smirnoff 		r->evaluations++;
1523b3a8eb9SGleb Smirnoff 		if (pfi_kif_match(r->kif, kif) == r->ifnot)
1533b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_IFP].ptr;
1543b3a8eb9SGleb Smirnoff 		else if (r->direction && r->direction != direction)
1553b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DIR].ptr;
1563b3a8eb9SGleb Smirnoff 		else if (r->af && r->af != pd->af)
1573b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_AF].ptr;
1583b3a8eb9SGleb Smirnoff 		else if (r->proto && r->proto != pd->proto)
1593b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_PROTO].ptr;
1603b3a8eb9SGleb Smirnoff 		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
1613b3a8eb9SGleb Smirnoff 		    src->neg, kif, M_GETFIB(m)))
1623b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
1633b3a8eb9SGleb Smirnoff 			    PF_SKIP_DST_ADDR].ptr;
1643b3a8eb9SGleb Smirnoff 		else if (src->port_op && !pf_match_port(src->port_op,
1653b3a8eb9SGleb Smirnoff 		    src->port[0], src->port[1], sport))
1663b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
1673b3a8eb9SGleb Smirnoff 			    PF_SKIP_DST_PORT].ptr;
1683b3a8eb9SGleb Smirnoff 		else if (dst != NULL &&
1693b3a8eb9SGleb Smirnoff 		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
1703b3a8eb9SGleb Smirnoff 		    M_GETFIB(m)))
1713b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
1723b3a8eb9SGleb Smirnoff 		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
1733b3a8eb9SGleb Smirnoff 		    0, NULL, M_GETFIB(m)))
1743b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1753b3a8eb9SGleb Smirnoff 		else if (dst != NULL && dst->port_op &&
1763b3a8eb9SGleb Smirnoff 		    !pf_match_port(dst->port_op, dst->port[0],
1773b3a8eb9SGleb Smirnoff 		    dst->port[1], dport))
1783b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DST_PORT].ptr;
1793b3a8eb9SGleb Smirnoff 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
1803b3a8eb9SGleb Smirnoff 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
1813b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1823b3a8eb9SGleb Smirnoff 		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
1833b3a8eb9SGleb Smirnoff 		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
1843b3a8eb9SGleb Smirnoff 		    off, pd->hdr.tcp), r->os_fingerprint)))
1853b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1863b3a8eb9SGleb Smirnoff 		else {
1873b3a8eb9SGleb Smirnoff 			if (r->tag)
1883b3a8eb9SGleb Smirnoff 				tag = r->tag;
1893b3a8eb9SGleb Smirnoff 			if (r->rtableid >= 0)
1903b3a8eb9SGleb Smirnoff 				rtableid = r->rtableid;
1913b3a8eb9SGleb Smirnoff 			if (r->anchor == NULL) {
1923b3a8eb9SGleb Smirnoff 				rm = r;
1933b3a8eb9SGleb Smirnoff 			} else
1941d6139c0SGleb Smirnoff 				pf_step_into_anchor(anchor_stack, &asd,
1951d6139c0SGleb Smirnoff 				    &ruleset, rs_num, &r, NULL, NULL);
1963b3a8eb9SGleb Smirnoff 		}
1973b3a8eb9SGleb Smirnoff 		if (r == NULL)
1981d6139c0SGleb Smirnoff 			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
1991d6139c0SGleb Smirnoff 			    rs_num, &r, NULL, NULL);
2003b3a8eb9SGleb Smirnoff 	}
2013b3a8eb9SGleb Smirnoff 
2023b3a8eb9SGleb Smirnoff 	if (tag > 0 && pf_tag_packet(m, pd, tag))
2033b3a8eb9SGleb Smirnoff 		return (NULL);
2043b3a8eb9SGleb Smirnoff 	if (rtableid >= 0)
2053b3a8eb9SGleb Smirnoff 		M_SETFIB(m, rtableid);
2063b3a8eb9SGleb Smirnoff 
2073b3a8eb9SGleb Smirnoff 	if (rm != NULL && (rm->action == PF_NONAT ||
2083b3a8eb9SGleb Smirnoff 	    rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2093b3a8eb9SGleb Smirnoff 		return (NULL);
2103b3a8eb9SGleb Smirnoff 	return (rm);
2113b3a8eb9SGleb Smirnoff }
2123b3a8eb9SGleb Smirnoff 
2133b3a8eb9SGleb Smirnoff static int
2143b3a8eb9SGleb Smirnoff pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
2158fc6e19cSGleb Smirnoff     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
2168fc6e19cSGleb Smirnoff     uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
2178fc6e19cSGleb Smirnoff     uint16_t high, struct pf_src_node **sn)
2183b3a8eb9SGleb Smirnoff {
2193b3a8eb9SGleb Smirnoff 	struct pf_state_key_cmp	key;
2203b3a8eb9SGleb Smirnoff 	struct pf_addr		init_addr;
2213b3a8eb9SGleb Smirnoff 
2223b3a8eb9SGleb Smirnoff 	bzero(&init_addr, sizeof(init_addr));
2233b3a8eb9SGleb Smirnoff 	if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2243b3a8eb9SGleb Smirnoff 		return (1);
2253b3a8eb9SGleb Smirnoff 
22679bde95fSGleb Smirnoff 	if (proto == IPPROTO_ICMP) {
2273b3a8eb9SGleb Smirnoff 		low = 1;
2283b3a8eb9SGleb Smirnoff 		high = 65535;
2293b3a8eb9SGleb Smirnoff 	}
2303b3a8eb9SGleb Smirnoff 
2318fc6e19cSGleb Smirnoff 	bzero(&key, sizeof(key));
2323b3a8eb9SGleb Smirnoff 	key.af = af;
2333b3a8eb9SGleb Smirnoff 	key.proto = proto;
2348fc6e19cSGleb Smirnoff 	key.port[0] = dport;
2358fc6e19cSGleb Smirnoff 	PF_ACPY(&key.addr[0], daddr, key.af);
2368fc6e19cSGleb Smirnoff 
2378fc6e19cSGleb Smirnoff 	do {
2388fc6e19cSGleb Smirnoff 		PF_ACPY(&key.addr[1], naddr, key.af);
2393b3a8eb9SGleb Smirnoff 
2403b3a8eb9SGleb Smirnoff 		/*
2413b3a8eb9SGleb Smirnoff 		 * port search; start random, step;
2423b3a8eb9SGleb Smirnoff 		 * similar 2 portloop in in_pcbbind
2433b3a8eb9SGleb Smirnoff 		 */
2443b3a8eb9SGleb Smirnoff 		if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
2458fc6e19cSGleb Smirnoff 		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
2468fc6e19cSGleb Smirnoff 			/*
2478fc6e19cSGleb Smirnoff 			 * XXX bug: icmp states don't use the id on both sides.
2488fc6e19cSGleb Smirnoff 			 * (traceroute -I through nat)
2498fc6e19cSGleb Smirnoff 			 */
2508fc6e19cSGleb Smirnoff 			key.port[1] = sport;
2518fc6e19cSGleb Smirnoff 			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
2528fc6e19cSGleb Smirnoff 				*nport = sport;
2533b3a8eb9SGleb Smirnoff 				return (0);
2548fc6e19cSGleb Smirnoff 			}
2553b3a8eb9SGleb Smirnoff 		} else if (low == high) {
2568fc6e19cSGleb Smirnoff 			key.port[1] = htons(low);
2573b3a8eb9SGleb Smirnoff 			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
2583b3a8eb9SGleb Smirnoff 				*nport = htons(low);
2593b3a8eb9SGleb Smirnoff 				return (0);
2603b3a8eb9SGleb Smirnoff 			}
2613b3a8eb9SGleb Smirnoff 		} else {
262e4e01d9cSGleb Smirnoff 			uint16_t tmp, cut;
2633b3a8eb9SGleb Smirnoff 
2643b3a8eb9SGleb Smirnoff 			if (low > high) {
2653b3a8eb9SGleb Smirnoff 				tmp = low;
2663b3a8eb9SGleb Smirnoff 				low = high;
2673b3a8eb9SGleb Smirnoff 				high = tmp;
2683b3a8eb9SGleb Smirnoff 			}
2693b3a8eb9SGleb Smirnoff 			/* low < high */
270e4e01d9cSGleb Smirnoff 			cut = arc4random() % (1 + high - low) + low;
2713b3a8eb9SGleb Smirnoff 			/* low <= cut <= high */
2723b3a8eb9SGleb Smirnoff 			for (tmp = cut; tmp <= high; ++(tmp)) {
2738fc6e19cSGleb Smirnoff 				key.port[1] = htons(tmp);
2743b3a8eb9SGleb Smirnoff 				if (pf_find_state_all(&key, PF_IN, NULL) ==
2753b3a8eb9SGleb Smirnoff 				    NULL) {
2763b3a8eb9SGleb Smirnoff 					*nport = htons(tmp);
2773b3a8eb9SGleb Smirnoff 					return (0);
2783b3a8eb9SGleb Smirnoff 				}
2793b3a8eb9SGleb Smirnoff 			}
2803b3a8eb9SGleb Smirnoff 			for (tmp = cut - 1; tmp >= low; --(tmp)) {
2818fc6e19cSGleb Smirnoff 				key.port[1] = htons(tmp);
2823b3a8eb9SGleb Smirnoff 				if (pf_find_state_all(&key, PF_IN, NULL) ==
2833b3a8eb9SGleb Smirnoff 				    NULL) {
2843b3a8eb9SGleb Smirnoff 					*nport = htons(tmp);
2853b3a8eb9SGleb Smirnoff 					return (0);
2863b3a8eb9SGleb Smirnoff 				}
2873b3a8eb9SGleb Smirnoff 			}
2883b3a8eb9SGleb Smirnoff 		}
2893b3a8eb9SGleb Smirnoff 
2903b3a8eb9SGleb Smirnoff 		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
2913b3a8eb9SGleb Smirnoff 		case PF_POOL_RANDOM:
2923b3a8eb9SGleb Smirnoff 		case PF_POOL_ROUNDROBIN:
2933b3a8eb9SGleb Smirnoff 			if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2943b3a8eb9SGleb Smirnoff 				return (1);
2953b3a8eb9SGleb Smirnoff 			break;
2963b3a8eb9SGleb Smirnoff 		case PF_POOL_NONE:
2973b3a8eb9SGleb Smirnoff 		case PF_POOL_SRCHASH:
2983b3a8eb9SGleb Smirnoff 		case PF_POOL_BITMASK:
2993b3a8eb9SGleb Smirnoff 		default:
3003b3a8eb9SGleb Smirnoff 			return (1);
3013b3a8eb9SGleb Smirnoff 		}
3023b3a8eb9SGleb Smirnoff 	} while (! PF_AEQ(&init_addr, naddr, af) );
3033b3a8eb9SGleb Smirnoff 	return (1);					/* none available */
3043b3a8eb9SGleb Smirnoff }
3053b3a8eb9SGleb Smirnoff 
3063b3a8eb9SGleb Smirnoff int
3073b3a8eb9SGleb Smirnoff pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
3083b3a8eb9SGleb Smirnoff     struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
3093b3a8eb9SGleb Smirnoff {
3103b3a8eb9SGleb Smirnoff 	struct pf_pool		*rpool = &r->rpool;
3113b3a8eb9SGleb Smirnoff 	struct pf_addr		*raddr = NULL, *rmask = NULL;
3123b3a8eb9SGleb Smirnoff 
313*e85343b1SGleb Smirnoff 	/* Try to find a src_node if none was given and this
314*e85343b1SGleb Smirnoff 	   is a sticky-address rule. */
3153b3a8eb9SGleb Smirnoff 	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
316*e85343b1SGleb Smirnoff 	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
3173b3a8eb9SGleb Smirnoff 		*sn = pf_find_src_node(saddr, r, af, 0);
318*e85343b1SGleb Smirnoff 
319*e85343b1SGleb Smirnoff 	/* If a src_node was found or explicitly given and it has a non-zero
320*e85343b1SGleb Smirnoff 	   route address, use this address. A zeroed address is found if the
321*e85343b1SGleb Smirnoff 	   src node was created just a moment ago in pf_create_state and it
322*e85343b1SGleb Smirnoff 	   needs to be filled in with routing decision calculated here. */
3233b3a8eb9SGleb Smirnoff 	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
3243b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &(*sn)->raddr, af);
3253b3a8eb9SGleb Smirnoff 		if (V_pf_status.debug >= PF_DEBUG_MISC) {
3263b3a8eb9SGleb Smirnoff 			printf("pf_map_addr: src tracking maps ");
3273b3a8eb9SGleb Smirnoff 			pf_print_host(saddr, 0, af);
3283b3a8eb9SGleb Smirnoff 			printf(" to ");
3293b3a8eb9SGleb Smirnoff 			pf_print_host(naddr, 0, af);
3303b3a8eb9SGleb Smirnoff 			printf("\n");
3313b3a8eb9SGleb Smirnoff 		}
3323b3a8eb9SGleb Smirnoff 		return (0);
3333b3a8eb9SGleb Smirnoff 	}
3343b3a8eb9SGleb Smirnoff 
335*e85343b1SGleb Smirnoff 	/* Find the route using chosen algorithm. Store the found route
336*e85343b1SGleb Smirnoff 	   in src_node if it was given or found. */
3373b3a8eb9SGleb Smirnoff 	if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
3383b3a8eb9SGleb Smirnoff 		return (1);
3393b3a8eb9SGleb Smirnoff 	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
3403b3a8eb9SGleb Smirnoff 		switch (af) {
3413b3a8eb9SGleb Smirnoff #ifdef INET
3423b3a8eb9SGleb Smirnoff 		case AF_INET:
3433b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
3443b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
3453b3a8eb9SGleb Smirnoff 			    PF_POOL_ROUNDROBIN)
3463b3a8eb9SGleb Smirnoff 				return (1);
3473b3a8eb9SGleb Smirnoff 			 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
3483b3a8eb9SGleb Smirnoff 			 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
3493b3a8eb9SGleb Smirnoff 			break;
3503b3a8eb9SGleb Smirnoff #endif /* INET */
3513b3a8eb9SGleb Smirnoff #ifdef INET6
3523b3a8eb9SGleb Smirnoff 		case AF_INET6:
3533b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
3543b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
3553b3a8eb9SGleb Smirnoff 			    PF_POOL_ROUNDROBIN)
3563b3a8eb9SGleb Smirnoff 				return (1);
3573b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
3583b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
3593b3a8eb9SGleb Smirnoff 			break;
3603b3a8eb9SGleb Smirnoff #endif /* INET6 */
3613b3a8eb9SGleb Smirnoff 		}
3623b3a8eb9SGleb Smirnoff 	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
3633b3a8eb9SGleb Smirnoff 		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
3643b3a8eb9SGleb Smirnoff 			return (1); /* unsupported */
3653b3a8eb9SGleb Smirnoff 	} else {
3663b3a8eb9SGleb Smirnoff 		raddr = &rpool->cur->addr.v.a.addr;
3673b3a8eb9SGleb Smirnoff 		rmask = &rpool->cur->addr.v.a.mask;
3683b3a8eb9SGleb Smirnoff 	}
3693b3a8eb9SGleb Smirnoff 
3703b3a8eb9SGleb Smirnoff 	switch (rpool->opts & PF_POOL_TYPEMASK) {
3713b3a8eb9SGleb Smirnoff 	case PF_POOL_NONE:
3723b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, raddr, af);
3733b3a8eb9SGleb Smirnoff 		break;
3743b3a8eb9SGleb Smirnoff 	case PF_POOL_BITMASK:
3753b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
3763b3a8eb9SGleb Smirnoff 		break;
3773b3a8eb9SGleb Smirnoff 	case PF_POOL_RANDOM:
3783b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
3793b3a8eb9SGleb Smirnoff 			switch (af) {
3803b3a8eb9SGleb Smirnoff #ifdef INET
3813b3a8eb9SGleb Smirnoff 			case AF_INET:
3823b3a8eb9SGleb Smirnoff 				rpool->counter.addr32[0] = htonl(arc4random());
3833b3a8eb9SGleb Smirnoff 				break;
3843b3a8eb9SGleb Smirnoff #endif /* INET */
3853b3a8eb9SGleb Smirnoff #ifdef INET6
3863b3a8eb9SGleb Smirnoff 			case AF_INET6:
3873b3a8eb9SGleb Smirnoff 				if (rmask->addr32[3] != 0xffffffff)
3883b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[3] =
3893b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
3903b3a8eb9SGleb Smirnoff 				else
3913b3a8eb9SGleb Smirnoff 					break;
3923b3a8eb9SGleb Smirnoff 				if (rmask->addr32[2] != 0xffffffff)
3933b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[2] =
3943b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
3953b3a8eb9SGleb Smirnoff 				else
3963b3a8eb9SGleb Smirnoff 					break;
3973b3a8eb9SGleb Smirnoff 				if (rmask->addr32[1] != 0xffffffff)
3983b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[1] =
3993b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4003b3a8eb9SGleb Smirnoff 				else
4013b3a8eb9SGleb Smirnoff 					break;
4023b3a8eb9SGleb Smirnoff 				if (rmask->addr32[0] != 0xffffffff)
4033b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[0] =
4043b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4053b3a8eb9SGleb Smirnoff 				break;
4063b3a8eb9SGleb Smirnoff #endif /* INET6 */
4073b3a8eb9SGleb Smirnoff 			}
4083b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
4093b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
4103b3a8eb9SGleb Smirnoff 
4113b3a8eb9SGleb Smirnoff 		} else {
4123b3a8eb9SGleb Smirnoff 			PF_AINC(&rpool->counter, af);
4133b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
4143b3a8eb9SGleb Smirnoff 		}
4153b3a8eb9SGleb Smirnoff 		break;
4163b3a8eb9SGleb Smirnoff 	case PF_POOL_SRCHASH:
4173b3a8eb9SGleb Smirnoff 	    {
4183b3a8eb9SGleb Smirnoff 		unsigned char hash[16];
4193b3a8eb9SGleb Smirnoff 
4203b3a8eb9SGleb Smirnoff 		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
4213b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
4223b3a8eb9SGleb Smirnoff 		break;
4233b3a8eb9SGleb Smirnoff 	    }
4243b3a8eb9SGleb Smirnoff 	case PF_POOL_ROUNDROBIN:
4253b3a8eb9SGleb Smirnoff 	    {
4263b3a8eb9SGleb Smirnoff 		struct pf_pooladdr *acur = rpool->cur;
4273b3a8eb9SGleb Smirnoff 
4283b3a8eb9SGleb Smirnoff 		/*
4293b3a8eb9SGleb Smirnoff 		 * XXXGL: in the round-robin case we need to store
4303b3a8eb9SGleb Smirnoff 		 * the round-robin machine state in the rule, thus
4313b3a8eb9SGleb Smirnoff 		 * forwarding thread needs to modify rule.
4323b3a8eb9SGleb Smirnoff 		 *
4333b3a8eb9SGleb Smirnoff 		 * This is done w/o locking, because performance is assumed
4343b3a8eb9SGleb Smirnoff 		 * more important than round-robin precision.
4353b3a8eb9SGleb Smirnoff 		 *
4363b3a8eb9SGleb Smirnoff 		 * In the simpliest case we just update the "rpool->cur"
4373b3a8eb9SGleb Smirnoff 		 * pointer. However, if pool contains tables or dynamic
4383b3a8eb9SGleb Smirnoff 		 * addresses, then "tblidx" is also used to store machine
4393b3a8eb9SGleb Smirnoff 		 * state. Since "tblidx" is int, concurrent access to it can't
4403b3a8eb9SGleb Smirnoff 		 * lead to inconsistence, only to lost of precision.
4413b3a8eb9SGleb Smirnoff 		 *
4423b3a8eb9SGleb Smirnoff 		 * Things get worse, if table contains not hosts, but
4433b3a8eb9SGleb Smirnoff 		 * prefixes. In this case counter also stores machine state,
4443b3a8eb9SGleb Smirnoff 		 * and for IPv6 address, counter can't be updated atomically.
4453b3a8eb9SGleb Smirnoff 		 * Probably, using round-robin on a table containing IPv6
4463b3a8eb9SGleb Smirnoff 		 * prefixes (or even IPv4) would cause a panic.
4473b3a8eb9SGleb Smirnoff 		 */
4483b3a8eb9SGleb Smirnoff 
4493b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
4503b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
4513b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af))
4523b3a8eb9SGleb Smirnoff 				goto get_addr;
4533b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
4543b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
4553b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af))
4563b3a8eb9SGleb Smirnoff 				goto get_addr;
4573b3a8eb9SGleb Smirnoff 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
4583b3a8eb9SGleb Smirnoff 			goto get_addr;
4593b3a8eb9SGleb Smirnoff 
4603b3a8eb9SGleb Smirnoff 	try_next:
4613b3a8eb9SGleb Smirnoff 		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
4623b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_FIRST(&rpool->list);
4633b3a8eb9SGleb Smirnoff 		else
4643b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
4653b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
4663b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
4673b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.tbl,
4683b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af)) {
4693b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
4703b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
4713b3a8eb9SGleb Smirnoff 					goto try_next;
4723b3a8eb9SGleb Smirnoff 				return (1);
4733b3a8eb9SGleb Smirnoff 			}
4743b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
4753b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
4763b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
4773b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af)) {
4783b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
4793b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
4803b3a8eb9SGleb Smirnoff 					goto try_next;
4813b3a8eb9SGleb Smirnoff 				return (1);
4823b3a8eb9SGleb Smirnoff 			}
4833b3a8eb9SGleb Smirnoff 		} else {
4843b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.v.a.addr;
4853b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.v.a.mask;
4863b3a8eb9SGleb Smirnoff 			PF_ACPY(&rpool->counter, raddr, af);
4873b3a8eb9SGleb Smirnoff 		}
4883b3a8eb9SGleb Smirnoff 
4893b3a8eb9SGleb Smirnoff 	get_addr:
4903b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &rpool->counter, af);
4913b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af))
4923b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
4933b3a8eb9SGleb Smirnoff 		PF_AINC(&rpool->counter, af);
4943b3a8eb9SGleb Smirnoff 		break;
4953b3a8eb9SGleb Smirnoff 	    }
4963b3a8eb9SGleb Smirnoff 	}
4973b3a8eb9SGleb Smirnoff 	if (*sn != NULL)
4983b3a8eb9SGleb Smirnoff 		PF_ACPY(&(*sn)->raddr, naddr, af);
4993b3a8eb9SGleb Smirnoff 
5003b3a8eb9SGleb Smirnoff 	if (V_pf_status.debug >= PF_DEBUG_MISC &&
5013b3a8eb9SGleb Smirnoff 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
5023b3a8eb9SGleb Smirnoff 		printf("pf_map_addr: selected address ");
5033b3a8eb9SGleb Smirnoff 		pf_print_host(naddr, 0, af);
5043b3a8eb9SGleb Smirnoff 		printf("\n");
5053b3a8eb9SGleb Smirnoff 	}
5063b3a8eb9SGleb Smirnoff 
5073b3a8eb9SGleb Smirnoff 	return (0);
5083b3a8eb9SGleb Smirnoff }
5093b3a8eb9SGleb Smirnoff 
5103b3a8eb9SGleb Smirnoff struct pf_rule *
5113b3a8eb9SGleb Smirnoff pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
5123b3a8eb9SGleb Smirnoff     struct pfi_kif *kif, struct pf_src_node **sn,
5133b3a8eb9SGleb Smirnoff     struct pf_state_key **skp, struct pf_state_key **nkp,
5143b3a8eb9SGleb Smirnoff     struct pf_addr *saddr, struct pf_addr *daddr,
5151d6139c0SGleb Smirnoff     uint16_t sport, uint16_t dport, struct pf_anchor_stackframe *anchor_stack)
5163b3a8eb9SGleb Smirnoff {
5173b3a8eb9SGleb Smirnoff 	struct pf_rule	*r = NULL;
5183b3a8eb9SGleb Smirnoff 	struct pf_addr	*naddr;
5193b3a8eb9SGleb Smirnoff 	uint16_t	*nport;
5203b3a8eb9SGleb Smirnoff 
5213b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5223b3a8eb9SGleb Smirnoff 	KASSERT(*skp == NULL, ("*skp not NULL"));
5233b3a8eb9SGleb Smirnoff 	KASSERT(*nkp == NULL, ("*nkp not NULL"));
5243b3a8eb9SGleb Smirnoff 
5253b3a8eb9SGleb Smirnoff 	if (direction == PF_OUT) {
5263b3a8eb9SGleb Smirnoff 		r = pf_match_translation(pd, m, off, direction, kif, saddr,
5271d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
5283b3a8eb9SGleb Smirnoff 		if (r == NULL)
5293b3a8eb9SGleb Smirnoff 			r = pf_match_translation(pd, m, off, direction, kif,
5301d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_NAT,
5311d6139c0SGleb Smirnoff 			    anchor_stack);
5323b3a8eb9SGleb Smirnoff 	} else {
5333b3a8eb9SGleb Smirnoff 		r = pf_match_translation(pd, m, off, direction, kif, saddr,
5341d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
5353b3a8eb9SGleb Smirnoff 		if (r == NULL)
5363b3a8eb9SGleb Smirnoff 			r = pf_match_translation(pd, m, off, direction, kif,
5371d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
5381d6139c0SGleb Smirnoff 			    anchor_stack);
5393b3a8eb9SGleb Smirnoff 	}
5403b3a8eb9SGleb Smirnoff 
5413b3a8eb9SGleb Smirnoff 	if (r == NULL)
5423b3a8eb9SGleb Smirnoff 		return (NULL);
5433b3a8eb9SGleb Smirnoff 
5443b3a8eb9SGleb Smirnoff 	switch (r->action) {
5453b3a8eb9SGleb Smirnoff 	case PF_NONAT:
5463b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
5473b3a8eb9SGleb Smirnoff 	case PF_NORDR:
5483b3a8eb9SGleb Smirnoff 		return (NULL);
5493b3a8eb9SGleb Smirnoff 	}
5503b3a8eb9SGleb Smirnoff 
5513b3a8eb9SGleb Smirnoff 	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
5523b3a8eb9SGleb Smirnoff 	if (*skp == NULL)
5533b3a8eb9SGleb Smirnoff 		return (NULL);
5543b3a8eb9SGleb Smirnoff 	*nkp = pf_state_key_clone(*skp);
5553b3a8eb9SGleb Smirnoff 	if (*nkp == NULL) {
5563b3a8eb9SGleb Smirnoff 		uma_zfree(V_pf_state_key_z, skp);
5573b3a8eb9SGleb Smirnoff 		*skp = NULL;
5583b3a8eb9SGleb Smirnoff 		return (NULL);
5593b3a8eb9SGleb Smirnoff 	}
5603b3a8eb9SGleb Smirnoff 
5613b3a8eb9SGleb Smirnoff 	/* XXX We only modify one side for now. */
5623b3a8eb9SGleb Smirnoff 	naddr = &(*nkp)->addr[1];
5633b3a8eb9SGleb Smirnoff 	nport = &(*nkp)->port[1];
5643b3a8eb9SGleb Smirnoff 
5653b3a8eb9SGleb Smirnoff 	switch (r->action) {
5663b3a8eb9SGleb Smirnoff 	case PF_NAT:
5678fc6e19cSGleb Smirnoff 		if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, daddr,
5688fc6e19cSGleb Smirnoff 		    dport, naddr, nport, r->rpool.proxy_port[0],
5693b3a8eb9SGleb Smirnoff 		    r->rpool.proxy_port[1], sn)) {
5703b3a8eb9SGleb Smirnoff 			DPFPRINTF(PF_DEBUG_MISC,
5713b3a8eb9SGleb Smirnoff 			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
5723b3a8eb9SGleb Smirnoff 			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
5733b3a8eb9SGleb Smirnoff 			goto notrans;
5743b3a8eb9SGleb Smirnoff 		}
5753b3a8eb9SGleb Smirnoff 		break;
5763b3a8eb9SGleb Smirnoff 	case PF_BINAT:
5773b3a8eb9SGleb Smirnoff 		switch (direction) {
5783b3a8eb9SGleb Smirnoff 		case PF_OUT:
5793b3a8eb9SGleb Smirnoff 			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
5803b3a8eb9SGleb Smirnoff 				switch (pd->af) {
5813b3a8eb9SGleb Smirnoff #ifdef INET
5823b3a8eb9SGleb Smirnoff 				case AF_INET:
5833b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
5843b3a8eb9SGleb Smirnoff 					    pfid_acnt4 < 1)
5853b3a8eb9SGleb Smirnoff 						goto notrans;
5863b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
5873b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
5883b3a8eb9SGleb Smirnoff 					    pfid_addr4,
5893b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
5903b3a8eb9SGleb Smirnoff 					    pfid_mask4, saddr, AF_INET);
5913b3a8eb9SGleb Smirnoff 					break;
5923b3a8eb9SGleb Smirnoff #endif /* INET */
5933b3a8eb9SGleb Smirnoff #ifdef INET6
5943b3a8eb9SGleb Smirnoff 				case AF_INET6:
5953b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
5963b3a8eb9SGleb Smirnoff 					    pfid_acnt6 < 1)
5973b3a8eb9SGleb Smirnoff 						goto notrans;
5983b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
5993b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6003b3a8eb9SGleb Smirnoff 					    pfid_addr6,
6013b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6023b3a8eb9SGleb Smirnoff 					    pfid_mask6, saddr, AF_INET6);
6033b3a8eb9SGleb Smirnoff 					break;
6043b3a8eb9SGleb Smirnoff #endif /* INET6 */
6053b3a8eb9SGleb Smirnoff 				}
6063b3a8eb9SGleb Smirnoff 			} else
6073b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr,
6083b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.addr,
6093b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.mask, saddr,
6103b3a8eb9SGleb Smirnoff 				    pd->af);
6113b3a8eb9SGleb Smirnoff 			break;
6123b3a8eb9SGleb Smirnoff 		case PF_IN:
6133b3a8eb9SGleb Smirnoff 			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
6143b3a8eb9SGleb Smirnoff 				switch (pd->af) {
6153b3a8eb9SGleb Smirnoff #ifdef INET
6163b3a8eb9SGleb Smirnoff 				case AF_INET:
6173b3a8eb9SGleb Smirnoff 					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
6183b3a8eb9SGleb Smirnoff 						goto notrans;
6193b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6203b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr4,
6213b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask4,
6223b3a8eb9SGleb Smirnoff 					    daddr, AF_INET);
6233b3a8eb9SGleb Smirnoff 					break;
6243b3a8eb9SGleb Smirnoff #endif /* INET */
6253b3a8eb9SGleb Smirnoff #ifdef INET6
6263b3a8eb9SGleb Smirnoff 				case AF_INET6:
6273b3a8eb9SGleb Smirnoff 					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
6283b3a8eb9SGleb Smirnoff 						goto notrans;
6293b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6303b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr6,
6313b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask6,
6323b3a8eb9SGleb Smirnoff 					    daddr, AF_INET6);
6333b3a8eb9SGleb Smirnoff 					break;
6343b3a8eb9SGleb Smirnoff #endif /* INET6 */
6353b3a8eb9SGleb Smirnoff 				}
6363b3a8eb9SGleb Smirnoff 			} else
6373b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
6383b3a8eb9SGleb Smirnoff 				    &r->src.addr.v.a.mask, daddr, pd->af);
6393b3a8eb9SGleb Smirnoff 			break;
6403b3a8eb9SGleb Smirnoff 		}
6413b3a8eb9SGleb Smirnoff 		break;
6423b3a8eb9SGleb Smirnoff 	case PF_RDR: {
6433b3a8eb9SGleb Smirnoff 		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
6443b3a8eb9SGleb Smirnoff 			goto notrans;
6453b3a8eb9SGleb Smirnoff 		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
6463b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
6473b3a8eb9SGleb Smirnoff 			    daddr, pd->af);
6483b3a8eb9SGleb Smirnoff 
6493b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[1]) {
6503b3a8eb9SGleb Smirnoff 			uint32_t	tmp_nport;
6513b3a8eb9SGleb Smirnoff 
6523b3a8eb9SGleb Smirnoff 			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
6533b3a8eb9SGleb Smirnoff 			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
6543b3a8eb9SGleb Smirnoff 			    1)) + r->rpool.proxy_port[0];
6553b3a8eb9SGleb Smirnoff 
6563b3a8eb9SGleb Smirnoff 			/* Wrap around if necessary. */
6573b3a8eb9SGleb Smirnoff 			if (tmp_nport > 65535)
6583b3a8eb9SGleb Smirnoff 				tmp_nport -= 65535;
6593b3a8eb9SGleb Smirnoff 			*nport = htons((uint16_t)tmp_nport);
6603b3a8eb9SGleb Smirnoff 		} else if (r->rpool.proxy_port[0])
6613b3a8eb9SGleb Smirnoff 			*nport = htons(r->rpool.proxy_port[0]);
6623b3a8eb9SGleb Smirnoff 		break;
6633b3a8eb9SGleb Smirnoff 	}
6643b3a8eb9SGleb Smirnoff 	default:
6653b3a8eb9SGleb Smirnoff 		panic("%s: unknown action %u", __func__, r->action);
6663b3a8eb9SGleb Smirnoff 	}
6673b3a8eb9SGleb Smirnoff 
6683b3a8eb9SGleb Smirnoff 	/* Return success only if translation really happened. */
6693b3a8eb9SGleb Smirnoff 	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
6703b3a8eb9SGleb Smirnoff 		return (r);
6713b3a8eb9SGleb Smirnoff 
6723b3a8eb9SGleb Smirnoff notrans:
6733b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *nkp);
6743b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *skp);
6753b3a8eb9SGleb Smirnoff 	*skp = *nkp = NULL;
676a830c452SGleb Smirnoff 	*sn = NULL;
6773b3a8eb9SGleb Smirnoff 
6783b3a8eb9SGleb Smirnoff 	return (NULL);
6793b3a8eb9SGleb Smirnoff }
680