xref: /freebsd/sys/netpfil/pf/pf_lb.c (revision 739731b8ca800540ed45d1ce92726ee5b61a87e5)
1d8aa10ccSGleb Smirnoff /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
53b3a8eb9SGleb Smirnoff  * Copyright (c) 2002 - 2008 Henning Brauer
63b3a8eb9SGleb Smirnoff  * All rights reserved.
73b3a8eb9SGleb Smirnoff  *
83b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
93b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
103b3a8eb9SGleb Smirnoff  * are met:
113b3a8eb9SGleb Smirnoff  *
123b3a8eb9SGleb Smirnoff  *    - Redistributions of source code must retain the above copyright
133b3a8eb9SGleb Smirnoff  *      notice, this list of conditions and the following disclaimer.
143b3a8eb9SGleb Smirnoff  *    - Redistributions in binary form must reproduce the above
153b3a8eb9SGleb Smirnoff  *      copyright notice, this list of conditions and the following
163b3a8eb9SGleb Smirnoff  *      disclaimer in the documentation and/or other materials provided
173b3a8eb9SGleb Smirnoff  *      with the distribution.
183b3a8eb9SGleb Smirnoff  *
193b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
203b3a8eb9SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
213b3a8eb9SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
223b3a8eb9SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
233b3a8eb9SGleb Smirnoff  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
243b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
253b3a8eb9SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
263b3a8eb9SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
273b3a8eb9SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
283b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
293b3a8eb9SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
303b3a8eb9SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
313b3a8eb9SGleb Smirnoff  *
323b3a8eb9SGleb Smirnoff  * Effort sponsored in part by the Defense Advanced Research Projects
333b3a8eb9SGleb Smirnoff  * Agency (DARPA) and Air Force Research Laboratory, Air Force
343b3a8eb9SGleb Smirnoff  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
353b3a8eb9SGleb Smirnoff  *
36d8aa10ccSGleb Smirnoff  *	$OpenBSD: pf_lb.c,v 1.2 2009/02/12 02:13:15 sthen Exp $
373b3a8eb9SGleb Smirnoff  */
383b3a8eb9SGleb Smirnoff 
393b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
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>
473b3a8eb9SGleb Smirnoff #include <sys/socket.h>
483b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
493b3a8eb9SGleb Smirnoff 
503b3a8eb9SGleb Smirnoff #include <net/if.h>
5176039bc8SGleb Smirnoff #include <net/vnet.h>
523b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
533b3a8eb9SGleb Smirnoff #include <net/if_pflog.h>
543b3a8eb9SGleb Smirnoff 
55339a1977SMark Johnston /*
56339a1977SMark Johnston  * Limit the amount of work we do to find a free source port for redirects that
57339a1977SMark Johnston  * introduce a state conflict.
58339a1977SMark Johnston  */
59339a1977SMark Johnston #define	V_pf_rdr_srcport_rewrite_tries	VNET(pf_rdr_srcport_rewrite_tries)
60339a1977SMark Johnston VNET_DEFINE_STATIC(int, pf_rdr_srcport_rewrite_tries) = 16;
61339a1977SMark Johnston 
623b3a8eb9SGleb Smirnoff #define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
633b3a8eb9SGleb Smirnoff 
643b3a8eb9SGleb Smirnoff static void		 pf_hash(struct pf_addr *, struct pf_addr *,
653b3a8eb9SGleb Smirnoff 			    struct pf_poolhashkey *, sa_family_t);
66e86bddeaSKristof Provost static struct pf_krule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
67*739731b8SKristof Provost 			    struct pfi_kkif *, struct pf_addr *, u_int16_t,
68*739731b8SKristof Provost 			    struct pf_addr *, uint16_t, int,
69*739731b8SKristof Provost 			    struct pf_kanchor_stackframe *);
70e86bddeaSKristof Provost static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *,
718fc6e19cSGleb Smirnoff     struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
72390dc369STom Jones     uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **,
73390dc369STom Jones     struct pf_udp_mapping **);
747d381d0aSKristof Provost static bool		 pf_islinklocal(const sa_family_t, const struct pf_addr *);
753b3a8eb9SGleb Smirnoff 
763b3a8eb9SGleb Smirnoff #define mix(a,b,c) \
773b3a8eb9SGleb Smirnoff 	do {					\
783b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 13);	\
793b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 8);	\
803b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 13);	\
813b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 12);	\
823b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 16);	\
833b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 5);	\
843b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 3);	\
853b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 10);	\
863b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 15);	\
873b3a8eb9SGleb Smirnoff 	} while (0)
883b3a8eb9SGleb Smirnoff 
893b3a8eb9SGleb Smirnoff /*
903b3a8eb9SGleb Smirnoff  * hash function based on bridge_hash in if_bridge.c
913b3a8eb9SGleb Smirnoff  */
923b3a8eb9SGleb Smirnoff static void
933b3a8eb9SGleb Smirnoff pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
943b3a8eb9SGleb Smirnoff     struct pf_poolhashkey *key, sa_family_t af)
953b3a8eb9SGleb Smirnoff {
963b3a8eb9SGleb Smirnoff 	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
973b3a8eb9SGleb Smirnoff 
983b3a8eb9SGleb Smirnoff 	switch (af) {
993b3a8eb9SGleb Smirnoff #ifdef INET
1003b3a8eb9SGleb Smirnoff 	case AF_INET:
1013b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
1023b3a8eb9SGleb Smirnoff 		b += key->key32[1];
1033b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1043b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c + key->key32[2];
1053b3a8eb9SGleb Smirnoff 		break;
1063b3a8eb9SGleb Smirnoff #endif /* INET */
1073b3a8eb9SGleb Smirnoff #ifdef INET6
1083b3a8eb9SGleb Smirnoff 	case AF_INET6:
1093b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
1103b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[2];
1113b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1123b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c;
1133b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[1];
1143b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[3];
1153b3a8eb9SGleb Smirnoff 		c += key->key32[1];
1163b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1173b3a8eb9SGleb Smirnoff 		hash->addr32[1] = c;
1183b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[2];
1193b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[1];
1203b3a8eb9SGleb Smirnoff 		c += key->key32[2];
1213b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1223b3a8eb9SGleb Smirnoff 		hash->addr32[2] = c;
1233b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[3];
1243b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[0];
1253b3a8eb9SGleb Smirnoff 		c += key->key32[3];
1263b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1273b3a8eb9SGleb Smirnoff 		hash->addr32[3] = c;
1283b3a8eb9SGleb Smirnoff 		break;
1293b3a8eb9SGleb Smirnoff #endif /* INET6 */
1303b3a8eb9SGleb Smirnoff 	}
1313b3a8eb9SGleb Smirnoff }
1323b3a8eb9SGleb Smirnoff 
133e86bddeaSKristof Provost static struct pf_krule *
134*739731b8SKristof Provost pf_match_translation(struct pf_pdesc *pd, struct mbuf *m,
135f2064dd1SKajetan Staszkiewicz     struct pfi_kkif *kif, struct pf_addr *saddr, u_int16_t sport,
1361d6139c0SGleb Smirnoff     struct pf_addr *daddr, uint16_t dport, int rs_num,
137e86bddeaSKristof Provost     struct pf_kanchor_stackframe *anchor_stack)
1383b3a8eb9SGleb Smirnoff {
139e86bddeaSKristof Provost 	struct pf_krule		*r, *rm = NULL;
140e86bddeaSKristof Provost 	struct pf_kruleset	*ruleset = NULL;
1413b3a8eb9SGleb Smirnoff 	int			 tag = -1;
1423b3a8eb9SGleb Smirnoff 	int			 rtableid = -1;
1433b3a8eb9SGleb Smirnoff 	int			 asd = 0;
1443b3a8eb9SGleb Smirnoff 
1453b3a8eb9SGleb Smirnoff 	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1466b94546aSMateusz Guzik 	while (r != NULL) {
1473b3a8eb9SGleb Smirnoff 		struct pf_rule_addr	*src = NULL, *dst = NULL;
1483b3a8eb9SGleb Smirnoff 		struct pf_addr_wrap	*xdst = NULL;
1493b3a8eb9SGleb Smirnoff 
150f2064dd1SKajetan Staszkiewicz 		if (r->action == PF_BINAT && pd->dir == PF_IN) {
1513b3a8eb9SGleb Smirnoff 			src = &r->dst;
1523b3a8eb9SGleb Smirnoff 			if (r->rpool.cur != NULL)
1533b3a8eb9SGleb Smirnoff 				xdst = &r->rpool.cur->addr;
1543b3a8eb9SGleb Smirnoff 		} else {
1553b3a8eb9SGleb Smirnoff 			src = &r->src;
1563b3a8eb9SGleb Smirnoff 			dst = &r->dst;
1573b3a8eb9SGleb Smirnoff 		}
1583b3a8eb9SGleb Smirnoff 
15902cf67ccSMateusz Guzik 		pf_counter_u64_add(&r->evaluations, 1);
160320c1116SKristof Provost 		if (pfi_kkif_match(r->kif, kif) == r->ifnot)
161e5c64b26SKajetan Staszkiewicz 			r = r->skip[PF_SKIP_IFP];
162f2064dd1SKajetan Staszkiewicz 		else if (r->direction && r->direction != pd->dir)
163e5c64b26SKajetan Staszkiewicz 			r = r->skip[PF_SKIP_DIR];
1643b3a8eb9SGleb Smirnoff 		else if (r->af && r->af != pd->af)
165e5c64b26SKajetan Staszkiewicz 			r = r->skip[PF_SKIP_AF];
1663b3a8eb9SGleb Smirnoff 		else if (r->proto && r->proto != pd->proto)
167e5c64b26SKajetan Staszkiewicz 			r = r->skip[PF_SKIP_PROTO];
1683b3a8eb9SGleb Smirnoff 		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
1693b3a8eb9SGleb Smirnoff 		    src->neg, kif, M_GETFIB(m)))
1703b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
171e5c64b26SKajetan Staszkiewicz 			    PF_SKIP_DST_ADDR];
1723b3a8eb9SGleb Smirnoff 		else if (src->port_op && !pf_match_port(src->port_op,
1733b3a8eb9SGleb Smirnoff 		    src->port[0], src->port[1], sport))
1743b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
175e5c64b26SKajetan Staszkiewicz 			    PF_SKIP_DST_PORT];
1763b3a8eb9SGleb Smirnoff 		else if (dst != NULL &&
1773b3a8eb9SGleb Smirnoff 		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
1783b3a8eb9SGleb Smirnoff 		    M_GETFIB(m)))
179e5c64b26SKajetan Staszkiewicz 			r = r->skip[PF_SKIP_DST_ADDR];
1803b3a8eb9SGleb Smirnoff 		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
1813b3a8eb9SGleb Smirnoff 		    0, NULL, M_GETFIB(m)))
1823b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1833b3a8eb9SGleb Smirnoff 		else if (dst != NULL && dst->port_op &&
1843b3a8eb9SGleb Smirnoff 		    !pf_match_port(dst->port_op, dst->port[0],
1853b3a8eb9SGleb Smirnoff 		    dst->port[1], dport))
186e5c64b26SKajetan Staszkiewicz 			r = r->skip[PF_SKIP_DST_PORT];
1873b3a8eb9SGleb Smirnoff 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
1883b3a8eb9SGleb Smirnoff 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
1893b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1903b3a8eb9SGleb Smirnoff 		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
1913b3a8eb9SGleb Smirnoff 		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
192*739731b8SKristof Provost 		    &pd->hdr.tcp), r->os_fingerprint)))
1933b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1943b3a8eb9SGleb Smirnoff 		else {
1953b3a8eb9SGleb Smirnoff 			if (r->tag)
1963b3a8eb9SGleb Smirnoff 				tag = r->tag;
1973b3a8eb9SGleb Smirnoff 			if (r->rtableid >= 0)
1983b3a8eb9SGleb Smirnoff 				rtableid = r->rtableid;
1993b3a8eb9SGleb Smirnoff 			if (r->anchor == NULL) {
2003b3a8eb9SGleb Smirnoff 				rm = r;
2016b94546aSMateusz Guzik 				if (rm->action == PF_NONAT ||
2026b94546aSMateusz Guzik 				    rm->action == PF_NORDR ||
2036b94546aSMateusz Guzik 				    rm->action == PF_NOBINAT) {
2046b94546aSMateusz Guzik 					rm = NULL;
2056b94546aSMateusz Guzik 				}
2066b94546aSMateusz Guzik 				break;
2073b3a8eb9SGleb Smirnoff 			} else
2081d6139c0SGleb Smirnoff 				pf_step_into_anchor(anchor_stack, &asd,
2091d6139c0SGleb Smirnoff 				    &ruleset, rs_num, &r, NULL, NULL);
2103b3a8eb9SGleb Smirnoff 		}
2113b3a8eb9SGleb Smirnoff 		if (r == NULL)
2121d6139c0SGleb Smirnoff 			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
2131d6139c0SGleb Smirnoff 			    rs_num, &r, NULL, NULL);
2143b3a8eb9SGleb Smirnoff 	}
2153b3a8eb9SGleb Smirnoff 
2163b3a8eb9SGleb Smirnoff 	if (tag > 0 && pf_tag_packet(m, pd, tag))
2173b3a8eb9SGleb Smirnoff 		return (NULL);
2183b3a8eb9SGleb Smirnoff 	if (rtableid >= 0)
2193b3a8eb9SGleb Smirnoff 		M_SETFIB(m, rtableid);
2203b3a8eb9SGleb Smirnoff 
2213b3a8eb9SGleb Smirnoff 	return (rm);
2223b3a8eb9SGleb Smirnoff }
2233b3a8eb9SGleb Smirnoff 
2243b3a8eb9SGleb Smirnoff static int
225e86bddeaSKristof Provost pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
2268fc6e19cSGleb Smirnoff     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
2278fc6e19cSGleb Smirnoff     uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
228390dc369STom Jones     uint16_t high, struct pf_ksrc_node **sn,
229390dc369STom Jones     struct pf_udp_mapping **udp_mapping)
2303b3a8eb9SGleb Smirnoff {
2313b3a8eb9SGleb Smirnoff 	struct pf_state_key_cmp	key;
2323b3a8eb9SGleb Smirnoff 	struct pf_addr		init_addr;
233390dc369STom Jones 	struct pf_srchash	*sh = NULL;
2343b3a8eb9SGleb Smirnoff 
2353b3a8eb9SGleb Smirnoff 	bzero(&init_addr, sizeof(init_addr));
236390dc369STom Jones 
237390dc369STom Jones 	MPASS(*udp_mapping == NULL);
238390dc369STom Jones 
239390dc369STom Jones 	/*
240390dc369STom Jones 	 * If we are UDP and have an existing mapping we can get source port
241390dc369STom Jones 	 * from the mapping. In this case we have to look up the src_node as
242390dc369STom Jones 	 * pf_map_addr would.
243390dc369STom Jones 	 */
244390dc369STom Jones 	if (proto == IPPROTO_UDP && (r->rpool.opts & PF_POOL_ENDPI)) {
245390dc369STom Jones 		struct pf_udp_endpoint_cmp udp_source;
246390dc369STom Jones 
247390dc369STom Jones 		bzero(&udp_source, sizeof(udp_source));
248390dc369STom Jones 		udp_source.af = af;
249390dc369STom Jones 		PF_ACPY(&udp_source.addr, saddr, af);
250390dc369STom Jones 		udp_source.port = sport;
251390dc369STom Jones 		*udp_mapping = pf_udp_mapping_find(&udp_source);
252390dc369STom Jones 		if (*udp_mapping) {
253390dc369STom Jones 			PF_ACPY(naddr, &(*udp_mapping)->endpoints[1].addr, af);
254390dc369STom Jones 			*nport = (*udp_mapping)->endpoints[1].port;
255390dc369STom Jones 			/* Try to find a src_node as per pf_map_addr(). */
256390dc369STom Jones 			if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
257390dc369STom Jones 			    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
258390dc369STom Jones 				*sn = pf_find_src_node(saddr, r, af, &sh, 0);
259390dc369STom Jones 			return (0);
260390dc369STom Jones 		} else {
261390dc369STom Jones 			*udp_mapping = pf_udp_mapping_create(af, saddr, sport, &init_addr, 0);
262390dc369STom Jones 			if (*udp_mapping == NULL)
2633b3a8eb9SGleb Smirnoff 				return (1);
264390dc369STom Jones 		}
265390dc369STom Jones 	}
266390dc369STom Jones 
2678e3d2529SKajetan Staszkiewicz 	if (pf_map_addr_sn(af, r, saddr, naddr, NULL, &init_addr, sn))
268390dc369STom Jones 		goto failed;
2693b3a8eb9SGleb Smirnoff 
270534ee17eSKristof Provost 	if (proto == IPPROTO_ICMP) {
271534ee17eSKristof Provost 		if (*nport == htons(ICMP_ECHO)) {
272534ee17eSKristof Provost 			low = 1;
273534ee17eSKristof Provost 			high = 65535;
274534ee17eSKristof Provost 		} else
275534ee17eSKristof Provost 			return (0);	/* Don't try to modify non-echo ICMP */
276534ee17eSKristof Provost 	}
277534ee17eSKristof Provost #ifdef INET6
278534ee17eSKristof Provost 	if (proto == IPPROTO_ICMPV6) {
279534ee17eSKristof Provost 		if (*nport == htons(ICMP6_ECHO_REQUEST)) {
280534ee17eSKristof Provost 			low = 1;
281534ee17eSKristof Provost 			high = 65535;
282534ee17eSKristof Provost 		} else
283534ee17eSKristof Provost 			return (0);	/* Don't try to modify non-echo ICMP */
284534ee17eSKristof Provost 	}
285534ee17eSKristof Provost #endif /* INET6 */
286534ee17eSKristof Provost 
2878fc6e19cSGleb Smirnoff 	bzero(&key, sizeof(key));
2883b3a8eb9SGleb Smirnoff 	key.af = af;
2893b3a8eb9SGleb Smirnoff 	key.proto = proto;
2908fc6e19cSGleb Smirnoff 	key.port[0] = dport;
2918fc6e19cSGleb Smirnoff 	PF_ACPY(&key.addr[0], daddr, key.af);
2928fc6e19cSGleb Smirnoff 
2938fc6e19cSGleb Smirnoff 	do {
2948fc6e19cSGleb Smirnoff 		PF_ACPY(&key.addr[1], naddr, key.af);
295390dc369STom Jones 		if (*udp_mapping)
296390dc369STom Jones 			PF_ACPY(&(*udp_mapping)->endpoints[1].addr, naddr, af);
2973b3a8eb9SGleb Smirnoff 
2983b3a8eb9SGleb Smirnoff 		/*
2993b3a8eb9SGleb Smirnoff 		 * port search; start random, step;
3003b3a8eb9SGleb Smirnoff 		 * similar 2 portloop in in_pcbbind
3013b3a8eb9SGleb Smirnoff 		 */
3026053adafSKristof Provost 		if (proto == IPPROTO_SCTP) {
3036053adafSKristof Provost 			key.port[1] = sport;
3046053adafSKristof Provost 			if (!pf_find_state_all_exists(&key, PF_IN)) {
3056053adafSKristof Provost 				*nport = sport;
3066053adafSKristof Provost 				return (0);
3076053adafSKristof Provost 			} else {
3086053adafSKristof Provost 				return (1); /* Fail mapping. */
3096053adafSKristof Provost 			}
3106053adafSKristof Provost 		} else if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
3118fc6e19cSGleb Smirnoff 		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
3128fc6e19cSGleb Smirnoff 			/*
3138fc6e19cSGleb Smirnoff 			 * XXX bug: icmp states don't use the id on both sides.
3148fc6e19cSGleb Smirnoff 			 * (traceroute -I through nat)
3158fc6e19cSGleb Smirnoff 			 */
3168fc6e19cSGleb Smirnoff 			key.port[1] = sport;
31719d6e29bSMateusz Guzik 			if (!pf_find_state_all_exists(&key, PF_IN)) {
3188fc6e19cSGleb Smirnoff 				*nport = sport;
3193b3a8eb9SGleb Smirnoff 				return (0);
3208fc6e19cSGleb Smirnoff 			}
3213b3a8eb9SGleb Smirnoff 		} else if (low == high) {
3228fc6e19cSGleb Smirnoff 			key.port[1] = htons(low);
32319d6e29bSMateusz Guzik 			if (!pf_find_state_all_exists(&key, PF_IN)) {
324390dc369STom Jones 				if (*udp_mapping != NULL) {
325390dc369STom Jones 					(*udp_mapping)->endpoints[1].port = htons(low);
326390dc369STom Jones 					if (pf_udp_mapping_insert(*udp_mapping) == 0) {
3273b3a8eb9SGleb Smirnoff 						*nport = htons(low);
3283b3a8eb9SGleb Smirnoff 						return (0);
3293b3a8eb9SGleb Smirnoff 					}
3303b3a8eb9SGleb Smirnoff 				} else {
331390dc369STom Jones 					*nport = htons(low);
332390dc369STom Jones 					return (0);
333390dc369STom Jones 				}
334390dc369STom Jones 			}
335390dc369STom Jones 		} else {
3367f3ad018SKristof Provost 			uint32_t tmp;
3377f3ad018SKristof Provost 			uint16_t cut;
3383b3a8eb9SGleb Smirnoff 
3393b3a8eb9SGleb Smirnoff 			if (low > high) {
3403b3a8eb9SGleb Smirnoff 				tmp = low;
3413b3a8eb9SGleb Smirnoff 				low = high;
3423b3a8eb9SGleb Smirnoff 				high = tmp;
3433b3a8eb9SGleb Smirnoff 			}
3443b3a8eb9SGleb Smirnoff 			/* low < high */
345e4e01d9cSGleb Smirnoff 			cut = arc4random() % (1 + high - low) + low;
3463b3a8eb9SGleb Smirnoff 			/* low <= cut <= high */
3477f3ad018SKristof Provost 			for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
348390dc369STom Jones 				if (*udp_mapping != NULL) {
349390dc369STom Jones 					(*udp_mapping)->endpoints[1].port = htons(tmp);
350390dc369STom Jones 					if (pf_udp_mapping_insert(*udp_mapping) == 0) {
351390dc369STom Jones 						*nport = htons(tmp);
352390dc369STom Jones 						return (0);
353390dc369STom Jones 					}
354390dc369STom Jones 				} else {
3558fc6e19cSGleb Smirnoff 					key.port[1] = htons(tmp);
35619d6e29bSMateusz Guzik 					if (!pf_find_state_all_exists(&key, PF_IN)) {
3573b3a8eb9SGleb Smirnoff 						*nport = htons(tmp);
3583b3a8eb9SGleb Smirnoff 						return (0);
3593b3a8eb9SGleb Smirnoff 					}
3603b3a8eb9SGleb Smirnoff 				}
361390dc369STom Jones 			}
3627f3ad018SKristof Provost 			tmp = cut;
3637f3ad018SKristof Provost 			for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
364390dc369STom Jones 				if (proto == IPPROTO_UDP &&
365390dc369STom Jones 				    (r->rpool.opts & PF_POOL_ENDPI)) {
366390dc369STom Jones 					(*udp_mapping)->endpoints[1].port = htons(tmp);
367390dc369STom Jones 					if (pf_udp_mapping_insert(*udp_mapping) == 0) {
368390dc369STom Jones 						*nport = htons(tmp);
369390dc369STom Jones 						return (0);
370390dc369STom Jones 					}
371390dc369STom Jones 				} else {
3728fc6e19cSGleb Smirnoff 					key.port[1] = htons(tmp);
37319d6e29bSMateusz Guzik 					if (!pf_find_state_all_exists(&key, PF_IN)) {
3743b3a8eb9SGleb Smirnoff 						*nport = htons(tmp);
3753b3a8eb9SGleb Smirnoff 						return (0);
3763b3a8eb9SGleb Smirnoff 					}
3773b3a8eb9SGleb Smirnoff 				}
3783b3a8eb9SGleb Smirnoff 			}
379390dc369STom Jones 		}
3803b3a8eb9SGleb Smirnoff 
3813b3a8eb9SGleb Smirnoff 		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
3823b3a8eb9SGleb Smirnoff 		case PF_POOL_RANDOM:
3833b3a8eb9SGleb Smirnoff 		case PF_POOL_ROUNDROBIN:
3842b0a4ffaSKristof Provost 			/*
3852b0a4ffaSKristof Provost 			 * pick a different source address since we're out
3862b0a4ffaSKristof Provost 			 * of free port choices for the current one.
3872b0a4ffaSKristof Provost 			 */
3888e3d2529SKajetan Staszkiewicz 			if (pf_map_addr_sn(af, r, saddr, naddr, NULL, &init_addr, sn))
3893b3a8eb9SGleb Smirnoff 				return (1);
3903b3a8eb9SGleb Smirnoff 			break;
3913b3a8eb9SGleb Smirnoff 		case PF_POOL_NONE:
3923b3a8eb9SGleb Smirnoff 		case PF_POOL_SRCHASH:
3933b3a8eb9SGleb Smirnoff 		case PF_POOL_BITMASK:
3943b3a8eb9SGleb Smirnoff 		default:
3953b3a8eb9SGleb Smirnoff 			return (1);
3963b3a8eb9SGleb Smirnoff 		}
3973b3a8eb9SGleb Smirnoff 	} while (! PF_AEQ(&init_addr, naddr, af) );
398390dc369STom Jones 
399390dc369STom Jones failed:
400390dc369STom Jones 	uma_zfree(V_pf_udp_mapping_z, *udp_mapping);
401390dc369STom Jones 	*udp_mapping = NULL;
4023b3a8eb9SGleb Smirnoff 	return (1);					/* none available */
4033b3a8eb9SGleb Smirnoff }
4043b3a8eb9SGleb Smirnoff 
4057d381d0aSKristof Provost static bool
4067d381d0aSKristof Provost pf_islinklocal(const sa_family_t af, const struct pf_addr *addr)
4077d381d0aSKristof Provost {
4087d381d0aSKristof Provost 	if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&addr->v6))
4097d381d0aSKristof Provost 		return (true);
4107d381d0aSKristof Provost 	return (false);
4117d381d0aSKristof Provost }
4127d381d0aSKristof Provost 
4132aa21096SKurosawa Takahiro static int
4142aa21096SKurosawa Takahiro pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
4152aa21096SKurosawa Takahiro     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
4162aa21096SKurosawa Takahiro     uint16_t dport, struct pf_addr *naddr, uint16_t *nport,
417390dc369STom Jones     struct pf_ksrc_node **sn, struct pf_udp_mapping **udp_mapping)
4182aa21096SKurosawa Takahiro {
4192aa21096SKurosawa Takahiro 	uint16_t psmask, low, highmask;
4202aa21096SKurosawa Takahiro 	uint16_t i, ahigh, cut;
4212aa21096SKurosawa Takahiro 	int ashift, psidshift;
4222aa21096SKurosawa Takahiro 
4232aa21096SKurosawa Takahiro 	ashift = 16 - r->rpool.mape.offset;
4242aa21096SKurosawa Takahiro 	psidshift = ashift - r->rpool.mape.psidlen;
4252aa21096SKurosawa Takahiro 	psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1);
4262aa21096SKurosawa Takahiro 	psmask = psmask << psidshift;
4272aa21096SKurosawa Takahiro 	highmask = (1U << psidshift) - 1;
4282aa21096SKurosawa Takahiro 
4292aa21096SKurosawa Takahiro 	ahigh = (1U << r->rpool.mape.offset) - 1;
4302aa21096SKurosawa Takahiro 	cut = arc4random() & ahigh;
4312aa21096SKurosawa Takahiro 	if (cut == 0)
4322aa21096SKurosawa Takahiro 		cut = 1;
4332aa21096SKurosawa Takahiro 
4342aa21096SKurosawa Takahiro 	for (i = cut; i <= ahigh; i++) {
4352aa21096SKurosawa Takahiro 		low = (i << ashift) | psmask;
4362aa21096SKurosawa Takahiro 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
437390dc369STom Jones 		    naddr, nport, low, low | highmask, sn, udp_mapping))
4382aa21096SKurosawa Takahiro 			return (0);
4392aa21096SKurosawa Takahiro 	}
4402aa21096SKurosawa Takahiro 	for (i = cut - 1; i > 0; i--) {
4412aa21096SKurosawa Takahiro 		low = (i << ashift) | psmask;
4422aa21096SKurosawa Takahiro 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
443390dc369STom Jones 		    naddr, nport, low, low | highmask, sn, udp_mapping))
4442aa21096SKurosawa Takahiro 			return (0);
4452aa21096SKurosawa Takahiro 	}
4462aa21096SKurosawa Takahiro 	return (1);
4472aa21096SKurosawa Takahiro }
4482aa21096SKurosawa Takahiro 
44916303d2bSKajetan Staszkiewicz u_short
450e86bddeaSKristof Provost pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
4518e3d2529SKajetan Staszkiewicz     struct pf_addr *naddr, struct pfi_kkif **nkif, struct pf_addr *init_addr)
4523b3a8eb9SGleb Smirnoff {
4539569fdddSMark Johnston 	u_short			 reason = PFRES_MATCH;
454320c1116SKristof Provost 	struct pf_kpool		*rpool = &r->rpool;
4553b3a8eb9SGleb Smirnoff 	struct pf_addr		*raddr = NULL, *rmask = NULL;
4563b3a8eb9SGleb Smirnoff 
4575f5e32f1SKristof Provost 	mtx_lock(&rpool->mtx);
458e85343b1SGleb Smirnoff 	/* Find the route using chosen algorithm. Store the found route
459e85343b1SGleb Smirnoff 	   in src_node if it was given or found. */
4605f5e32f1SKristof Provost 	if (rpool->cur->addr.type == PF_ADDR_NOROUTE) {
46116303d2bSKajetan Staszkiewicz 		reason = PFRES_MAPFAILED;
46216303d2bSKajetan Staszkiewicz 		goto done_pool_mtx;
4635f5e32f1SKristof Provost 	}
4643b3a8eb9SGleb Smirnoff 	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
4653b3a8eb9SGleb Smirnoff 		switch (af) {
4663b3a8eb9SGleb Smirnoff #ifdef INET
4673b3a8eb9SGleb Smirnoff 		case AF_INET:
4683b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
4693b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
4705f5e32f1SKristof Provost 			    PF_POOL_ROUNDROBIN) {
47116303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
47216303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
4735f5e32f1SKristof Provost 			}
4743b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
4753b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
4763b3a8eb9SGleb Smirnoff 			break;
4773b3a8eb9SGleb Smirnoff #endif /* INET */
4783b3a8eb9SGleb Smirnoff #ifdef INET6
4793b3a8eb9SGleb Smirnoff 		case AF_INET6:
4803b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
4813b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
4825f5e32f1SKristof Provost 			    PF_POOL_ROUNDROBIN) {
48316303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
48416303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
4855f5e32f1SKristof Provost 			}
4863b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
4873b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
4883b3a8eb9SGleb Smirnoff 			break;
4893b3a8eb9SGleb Smirnoff #endif /* INET6 */
4903b3a8eb9SGleb Smirnoff 		}
4913b3a8eb9SGleb Smirnoff 	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
4925f5e32f1SKristof Provost 		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN) {
49316303d2bSKajetan Staszkiewicz 			reason = PFRES_MAPFAILED;
49416303d2bSKajetan Staszkiewicz 			goto done_pool_mtx; /* unsupported */
4955f5e32f1SKristof Provost 		}
4963b3a8eb9SGleb Smirnoff 	} else {
4973b3a8eb9SGleb Smirnoff 		raddr = &rpool->cur->addr.v.a.addr;
4983b3a8eb9SGleb Smirnoff 		rmask = &rpool->cur->addr.v.a.mask;
4993b3a8eb9SGleb Smirnoff 	}
5003b3a8eb9SGleb Smirnoff 
5013b3a8eb9SGleb Smirnoff 	switch (rpool->opts & PF_POOL_TYPEMASK) {
5023b3a8eb9SGleb Smirnoff 	case PF_POOL_NONE:
5033b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, raddr, af);
5043b3a8eb9SGleb Smirnoff 		break;
5053b3a8eb9SGleb Smirnoff 	case PF_POOL_BITMASK:
5063b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
5073b3a8eb9SGleb Smirnoff 		break;
5083b3a8eb9SGleb Smirnoff 	case PF_POOL_RANDOM:
5093b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
5103b3a8eb9SGleb Smirnoff 			switch (af) {
5113b3a8eb9SGleb Smirnoff #ifdef INET
5123b3a8eb9SGleb Smirnoff 			case AF_INET:
5133b3a8eb9SGleb Smirnoff 				rpool->counter.addr32[0] = htonl(arc4random());
5143b3a8eb9SGleb Smirnoff 				break;
5153b3a8eb9SGleb Smirnoff #endif /* INET */
5163b3a8eb9SGleb Smirnoff #ifdef INET6
5173b3a8eb9SGleb Smirnoff 			case AF_INET6:
5183b3a8eb9SGleb Smirnoff 				if (rmask->addr32[3] != 0xffffffff)
5193b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[3] =
5203b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
5213b3a8eb9SGleb Smirnoff 				else
5223b3a8eb9SGleb Smirnoff 					break;
5233b3a8eb9SGleb Smirnoff 				if (rmask->addr32[2] != 0xffffffff)
5243b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[2] =
5253b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
5263b3a8eb9SGleb Smirnoff 				else
5273b3a8eb9SGleb Smirnoff 					break;
5283b3a8eb9SGleb Smirnoff 				if (rmask->addr32[1] != 0xffffffff)
5293b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[1] =
5303b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
5313b3a8eb9SGleb Smirnoff 				else
5323b3a8eb9SGleb Smirnoff 					break;
5333b3a8eb9SGleb Smirnoff 				if (rmask->addr32[0] != 0xffffffff)
5343b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[0] =
5353b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
5363b3a8eb9SGleb Smirnoff 				break;
5373b3a8eb9SGleb Smirnoff #endif /* INET6 */
5383b3a8eb9SGleb Smirnoff 			}
5393b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
5403b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
5413b3a8eb9SGleb Smirnoff 
5423b3a8eb9SGleb Smirnoff 		} else {
5433b3a8eb9SGleb Smirnoff 			PF_AINC(&rpool->counter, af);
5443b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
5453b3a8eb9SGleb Smirnoff 		}
5463b3a8eb9SGleb Smirnoff 		break;
5473b3a8eb9SGleb Smirnoff 	case PF_POOL_SRCHASH:
5483b3a8eb9SGleb Smirnoff 	    {
5493b3a8eb9SGleb Smirnoff 		unsigned char hash[16];
5503b3a8eb9SGleb Smirnoff 
5513b3a8eb9SGleb Smirnoff 		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
5523b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
5533b3a8eb9SGleb Smirnoff 		break;
5543b3a8eb9SGleb Smirnoff 	    }
5553b3a8eb9SGleb Smirnoff 	case PF_POOL_ROUNDROBIN:
5563b3a8eb9SGleb Smirnoff 	    {
557320c1116SKristof Provost 		struct pf_kpooladdr *acur = rpool->cur;
5583b3a8eb9SGleb Smirnoff 
5593b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
5603b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
5617d381d0aSKristof Provost 			    &rpool->tblidx, &rpool->counter, af, NULL))
5623b3a8eb9SGleb Smirnoff 				goto get_addr;
5633b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
5643b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
5657d381d0aSKristof Provost 			    &rpool->tblidx, &rpool->counter, af, pf_islinklocal))
5663b3a8eb9SGleb Smirnoff 				goto get_addr;
5673b3a8eb9SGleb Smirnoff 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
5683b3a8eb9SGleb Smirnoff 			goto get_addr;
5693b3a8eb9SGleb Smirnoff 
5703b3a8eb9SGleb Smirnoff 	try_next:
5713b3a8eb9SGleb Smirnoff 		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
5723b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_FIRST(&rpool->list);
5733b3a8eb9SGleb Smirnoff 		else
5743b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
5753b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
5763b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
5773b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.tbl,
5787d381d0aSKristof Provost 			    &rpool->tblidx, &rpool->counter, af, NULL)) {
5793b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
5803b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
5813b3a8eb9SGleb Smirnoff 					goto try_next;
58216303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
58316303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
5843b3a8eb9SGleb Smirnoff 			}
5853b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
5863b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
5873b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
5887d381d0aSKristof Provost 			    &rpool->tblidx, &rpool->counter, af, pf_islinklocal)) {
5893b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
5903b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
5913b3a8eb9SGleb Smirnoff 					goto try_next;
59216303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
59316303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
5943b3a8eb9SGleb Smirnoff 			}
5953b3a8eb9SGleb Smirnoff 		} else {
5963b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.v.a.addr;
5973b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.v.a.mask;
5983b3a8eb9SGleb Smirnoff 			PF_ACPY(&rpool->counter, raddr, af);
5993b3a8eb9SGleb Smirnoff 		}
6003b3a8eb9SGleb Smirnoff 
6013b3a8eb9SGleb Smirnoff 	get_addr:
6023b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &rpool->counter, af);
6033b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af))
6043b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
6053b3a8eb9SGleb Smirnoff 		PF_AINC(&rpool->counter, af);
6063b3a8eb9SGleb Smirnoff 		break;
6073b3a8eb9SGleb Smirnoff 	    }
6083b3a8eb9SGleb Smirnoff 	}
609d10de21fSKajetan Staszkiewicz 
610d10de21fSKajetan Staszkiewicz 	if (nkif)
611d10de21fSKajetan Staszkiewicz 		*nkif = rpool->cur->kif;
612d10de21fSKajetan Staszkiewicz 
6138e3d2529SKajetan Staszkiewicz done_pool_mtx:
6148e3d2529SKajetan Staszkiewicz 	mtx_unlock(&rpool->mtx);
6158e3d2529SKajetan Staszkiewicz 
6168e3d2529SKajetan Staszkiewicz 	if (reason) {
6178e3d2529SKajetan Staszkiewicz 		counter_u64_add(V_pf_status.counters[reason], 1);
6188e3d2529SKajetan Staszkiewicz 	}
6198e3d2529SKajetan Staszkiewicz 
6208e3d2529SKajetan Staszkiewicz 	return (reason);
6218e3d2529SKajetan Staszkiewicz }
6228e3d2529SKajetan Staszkiewicz 
6238e3d2529SKajetan Staszkiewicz u_short
6248e3d2529SKajetan Staszkiewicz pf_map_addr_sn(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
6258e3d2529SKajetan Staszkiewicz     struct pf_addr *naddr, struct pfi_kkif **nkif, struct pf_addr *init_addr,
6268e3d2529SKajetan Staszkiewicz     struct pf_ksrc_node **sn)
6278e3d2529SKajetan Staszkiewicz {
6288e3d2529SKajetan Staszkiewicz 	u_short			 reason = 0;
6298e3d2529SKajetan Staszkiewicz 	struct pf_kpool		*rpool = &r->rpool;
6308e3d2529SKajetan Staszkiewicz 	struct pf_srchash	*sh = NULL;
6318e3d2529SKajetan Staszkiewicz 
6328e3d2529SKajetan Staszkiewicz 	/* Try to find a src_node if none was given and this
6338e3d2529SKajetan Staszkiewicz 	   is a sticky-address rule. */
6348e3d2529SKajetan Staszkiewicz 	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
6358e3d2529SKajetan Staszkiewicz 	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
6368e3d2529SKajetan Staszkiewicz 		*sn = pf_find_src_node(saddr, r, af, &sh, false);
6378e3d2529SKajetan Staszkiewicz 
6388e3d2529SKajetan Staszkiewicz 	/* If a src_node was found or explicitly given and it has a non-zero
6398e3d2529SKajetan Staszkiewicz 	   route address, use this address. A zeroed address is found if the
6408e3d2529SKajetan Staszkiewicz 	   src node was created just a moment ago in pf_create_state and it
6418e3d2529SKajetan Staszkiewicz 	   needs to be filled in with routing decision calculated here. */
6428e3d2529SKajetan Staszkiewicz 	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
6438e3d2529SKajetan Staszkiewicz 		/* If the supplied address is the same as the current one we've
6448e3d2529SKajetan Staszkiewicz 		 * been asked before, so tell the caller that there's no other
6458e3d2529SKajetan Staszkiewicz 		 * address to be had. */
6468e3d2529SKajetan Staszkiewicz 		if (PF_AEQ(naddr, &(*sn)->raddr, af)) {
6478e3d2529SKajetan Staszkiewicz 			reason = PFRES_MAPFAILED;
6488e3d2529SKajetan Staszkiewicz 			goto done;
6498e3d2529SKajetan Staszkiewicz 		}
6508e3d2529SKajetan Staszkiewicz 
6518e3d2529SKajetan Staszkiewicz 		PF_ACPY(naddr, &(*sn)->raddr, af);
6528e3d2529SKajetan Staszkiewicz 		if (nkif)
6538e3d2529SKajetan Staszkiewicz 			*nkif = (*sn)->rkif;
6548e3d2529SKajetan Staszkiewicz 		if (V_pf_status.debug >= PF_DEBUG_NOISY) {
6558e3d2529SKajetan Staszkiewicz 			printf("pf_map_addr: src tracking maps ");
6568e3d2529SKajetan Staszkiewicz 			pf_print_host(saddr, 0, af);
6578e3d2529SKajetan Staszkiewicz 			printf(" to ");
6588e3d2529SKajetan Staszkiewicz 			pf_print_host(naddr, 0, af);
6598e3d2529SKajetan Staszkiewicz 			if (nkif)
6608e3d2529SKajetan Staszkiewicz 				printf("@%s", (*nkif)->pfik_name);
6618e3d2529SKajetan Staszkiewicz 			printf("\n");
6628e3d2529SKajetan Staszkiewicz 		}
6638e3d2529SKajetan Staszkiewicz 		goto done;
6648e3d2529SKajetan Staszkiewicz 	}
6658e3d2529SKajetan Staszkiewicz 
6668e3d2529SKajetan Staszkiewicz 	/*
6678e3d2529SKajetan Staszkiewicz 	 * Source node has not been found. Find a new address and store it
6688e3d2529SKajetan Staszkiewicz 	 * in variables given by the caller.
6698e3d2529SKajetan Staszkiewicz 	 */
6708e3d2529SKajetan Staszkiewicz 	if (pf_map_addr(af, r, saddr, naddr, nkif, init_addr) != 0) {
6718e3d2529SKajetan Staszkiewicz 		/* pf_map_addr() sets reason counters on its own */
6728e3d2529SKajetan Staszkiewicz 		goto done;
6738e3d2529SKajetan Staszkiewicz 	}
6748e3d2529SKajetan Staszkiewicz 
675d10de21fSKajetan Staszkiewicz 	if (*sn != NULL) {
6763b3a8eb9SGleb Smirnoff 		PF_ACPY(&(*sn)->raddr, naddr, af);
677d10de21fSKajetan Staszkiewicz 		if (nkif)
678d10de21fSKajetan Staszkiewicz 			(*sn)->rkif = *nkif;
679d10de21fSKajetan Staszkiewicz 	}
6803b3a8eb9SGleb Smirnoff 
681498cca14SKristof Provost 	if (V_pf_status.debug >= PF_DEBUG_NOISY &&
6823b3a8eb9SGleb Smirnoff 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
6833b3a8eb9SGleb Smirnoff 		printf("pf_map_addr: selected address ");
6843b3a8eb9SGleb Smirnoff 		pf_print_host(naddr, 0, af);
685d10de21fSKajetan Staszkiewicz 		if (nkif)
686d10de21fSKajetan Staszkiewicz 			printf("@%s", (*nkif)->pfik_name);
6873b3a8eb9SGleb Smirnoff 		printf("\n");
6883b3a8eb9SGleb Smirnoff 	}
6893b3a8eb9SGleb Smirnoff 
69016303d2bSKajetan Staszkiewicz done:
69116303d2bSKajetan Staszkiewicz 	if (reason) {
69216303d2bSKajetan Staszkiewicz 		counter_u64_add(V_pf_status.counters[reason], 1);
69316303d2bSKajetan Staszkiewicz 	}
69416303d2bSKajetan Staszkiewicz 
69516303d2bSKajetan Staszkiewicz 	return (reason);
6963b3a8eb9SGleb Smirnoff }
6973b3a8eb9SGleb Smirnoff 
6987e65cfc9SMark Johnston u_short
699f2064dd1SKajetan Staszkiewicz pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
700320c1116SKristof Provost     struct pfi_kkif *kif, struct pf_ksrc_node **sn,
7013b3a8eb9SGleb Smirnoff     struct pf_state_key **skp, struct pf_state_key **nkp,
7023b3a8eb9SGleb Smirnoff     struct pf_addr *saddr, struct pf_addr *daddr,
7037e65cfc9SMark Johnston     uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack,
704390dc369STom Jones     struct pf_krule **rp,
705390dc369STom Jones     struct pf_udp_mapping **udp_mapping)
7063b3a8eb9SGleb Smirnoff {
707e86bddeaSKristof Provost 	struct pf_krule	*r = NULL;
7083b3a8eb9SGleb Smirnoff 	struct pf_addr	*naddr;
7099897a669SMark Johnston 	uint16_t	*nportp;
7102aa21096SKurosawa Takahiro 	uint16_t	 low, high;
7117e65cfc9SMark Johnston 	u_short		 reason;
7123b3a8eb9SGleb Smirnoff 
7133b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
7143b3a8eb9SGleb Smirnoff 	KASSERT(*skp == NULL, ("*skp not NULL"));
7153b3a8eb9SGleb Smirnoff 	KASSERT(*nkp == NULL, ("*nkp not NULL"));
7163b3a8eb9SGleb Smirnoff 
7177e65cfc9SMark Johnston 	*rp = NULL;
7187e65cfc9SMark Johnston 
719f2064dd1SKajetan Staszkiewicz 	if (pd->dir == PF_OUT) {
720*739731b8SKristof Provost 		r = pf_match_translation(pd, m, kif, saddr,
7211d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
7223b3a8eb9SGleb Smirnoff 		if (r == NULL)
723*739731b8SKristof Provost 			r = pf_match_translation(pd, m, kif,
7241d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_NAT,
7251d6139c0SGleb Smirnoff 			    anchor_stack);
7263b3a8eb9SGleb Smirnoff 	} else {
727*739731b8SKristof Provost 		r = pf_match_translation(pd, m, kif, saddr,
7281d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
7293b3a8eb9SGleb Smirnoff 		if (r == NULL)
730*739731b8SKristof Provost 			r = pf_match_translation(pd, m, kif,
7311d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
7321d6139c0SGleb Smirnoff 			    anchor_stack);
7333b3a8eb9SGleb Smirnoff 	}
7343b3a8eb9SGleb Smirnoff 
7353b3a8eb9SGleb Smirnoff 	if (r == NULL)
7367e65cfc9SMark Johnston 		return (PFRES_MAX);
7373b3a8eb9SGleb Smirnoff 
7383b3a8eb9SGleb Smirnoff 	switch (r->action) {
7393b3a8eb9SGleb Smirnoff 	case PF_NONAT:
7403b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
7413b3a8eb9SGleb Smirnoff 	case PF_NORDR:
7427e65cfc9SMark Johnston 		return (PFRES_MAX);
7433b3a8eb9SGleb Smirnoff 	}
7443b3a8eb9SGleb Smirnoff 
745*739731b8SKristof Provost 	*skp = pf_state_key_setup(pd, m, saddr, daddr, sport, dport);
7463b3a8eb9SGleb Smirnoff 	if (*skp == NULL)
7477e65cfc9SMark Johnston 		return (PFRES_MEMORY);
7483b3a8eb9SGleb Smirnoff 	*nkp = pf_state_key_clone(*skp);
7493b3a8eb9SGleb Smirnoff 	if (*nkp == NULL) {
75098a9874fSKristof Provost 		uma_zfree(V_pf_state_key_z, *skp);
7513b3a8eb9SGleb Smirnoff 		*skp = NULL;
7527e65cfc9SMark Johnston 		return (PFRES_MEMORY);
7533b3a8eb9SGleb Smirnoff 	}
7543b3a8eb9SGleb Smirnoff 
7553b3a8eb9SGleb Smirnoff 	naddr = &(*nkp)->addr[1];
7569897a669SMark Johnston 	nportp = &(*nkp)->port[1];
7573b3a8eb9SGleb Smirnoff 
7583b3a8eb9SGleb Smirnoff 	switch (r->action) {
7593b3a8eb9SGleb Smirnoff 	case PF_NAT:
7602aa21096SKurosawa Takahiro 		if (pd->proto == IPPROTO_ICMP) {
7612aa21096SKurosawa Takahiro 			low = 1;
7622aa21096SKurosawa Takahiro 			high = 65535;
7632aa21096SKurosawa Takahiro 		} else {
7642aa21096SKurosawa Takahiro 			low  = r->rpool.proxy_port[0];
7652aa21096SKurosawa Takahiro 			high = r->rpool.proxy_port[1];
7662aa21096SKurosawa Takahiro 		}
7672aa21096SKurosawa Takahiro 		if (r->rpool.mape.offset > 0) {
7682aa21096SKurosawa Takahiro 			if (pf_get_mape_sport(pd->af, pd->proto, r, saddr,
769390dc369STom Jones 			    sport, daddr, dport, naddr, nportp, sn, udp_mapping)) {
7702aa21096SKurosawa Takahiro 				DPFPRINTF(PF_DEBUG_MISC,
7712aa21096SKurosawa Takahiro 				    ("pf: MAP-E port allocation (%u/%u/%u)"
7722aa21096SKurosawa Takahiro 				    " failed\n",
7732aa21096SKurosawa Takahiro 				    r->rpool.mape.offset,
7742aa21096SKurosawa Takahiro 				    r->rpool.mape.psidlen,
7752aa21096SKurosawa Takahiro 				    r->rpool.mape.psid));
7767e65cfc9SMark Johnston 				reason = PFRES_MAPFAILED;
7772aa21096SKurosawa Takahiro 				goto notrans;
7782aa21096SKurosawa Takahiro 			}
7792aa21096SKurosawa Takahiro 		} else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport,
780390dc369STom Jones 		    daddr, dport, naddr, nportp, low, high, sn, udp_mapping)) {
7813b3a8eb9SGleb Smirnoff 			DPFPRINTF(PF_DEBUG_MISC,
7823b3a8eb9SGleb Smirnoff 			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
7833b3a8eb9SGleb Smirnoff 			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
7847e65cfc9SMark Johnston 			reason = PFRES_MAPFAILED;
7853b3a8eb9SGleb Smirnoff 			goto notrans;
7863b3a8eb9SGleb Smirnoff 		}
7873b3a8eb9SGleb Smirnoff 		break;
7883b3a8eb9SGleb Smirnoff 	case PF_BINAT:
789f2064dd1SKajetan Staszkiewicz 		switch (pd->dir) {
7903b3a8eb9SGleb Smirnoff 		case PF_OUT:
7913b3a8eb9SGleb Smirnoff 			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
7923b3a8eb9SGleb Smirnoff 				switch (pd->af) {
7933b3a8eb9SGleb Smirnoff #ifdef INET
7943b3a8eb9SGleb Smirnoff 				case AF_INET:
7953b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
7967e65cfc9SMark Johnston 					    pfid_acnt4 < 1) {
7977e65cfc9SMark Johnston 						reason = PFRES_MAPFAILED;
7983b3a8eb9SGleb Smirnoff 						goto notrans;
7997e65cfc9SMark Johnston 					}
8003b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
8013b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
8023b3a8eb9SGleb Smirnoff 					    pfid_addr4,
8033b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
8043b3a8eb9SGleb Smirnoff 					    pfid_mask4, saddr, AF_INET);
8053b3a8eb9SGleb Smirnoff 					break;
8063b3a8eb9SGleb Smirnoff #endif /* INET */
8073b3a8eb9SGleb Smirnoff #ifdef INET6
8083b3a8eb9SGleb Smirnoff 				case AF_INET6:
8093b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
8107e65cfc9SMark Johnston 					    pfid_acnt6 < 1) {
8117e65cfc9SMark Johnston 						reason = PFRES_MAPFAILED;
8123b3a8eb9SGleb Smirnoff 						goto notrans;
8137e65cfc9SMark Johnston 					}
8143b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
8153b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
8163b3a8eb9SGleb Smirnoff 					    pfid_addr6,
8173b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
8183b3a8eb9SGleb Smirnoff 					    pfid_mask6, saddr, AF_INET6);
8193b3a8eb9SGleb Smirnoff 					break;
8203b3a8eb9SGleb Smirnoff #endif /* INET6 */
8213b3a8eb9SGleb Smirnoff 				}
8223b3a8eb9SGleb Smirnoff 			} else
8233b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr,
8243b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.addr,
8253b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.mask, saddr,
8263b3a8eb9SGleb Smirnoff 				    pd->af);
8273b3a8eb9SGleb Smirnoff 			break;
8283b3a8eb9SGleb Smirnoff 		case PF_IN:
8293b3a8eb9SGleb Smirnoff 			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
8303b3a8eb9SGleb Smirnoff 				switch (pd->af) {
8313b3a8eb9SGleb Smirnoff #ifdef INET
8323b3a8eb9SGleb Smirnoff 				case AF_INET:
8337e65cfc9SMark Johnston 					if (r->src.addr.p.dyn->pfid_acnt4 < 1) {
8347e65cfc9SMark Johnston 						reason = PFRES_MAPFAILED;
8353b3a8eb9SGleb Smirnoff 						goto notrans;
8367e65cfc9SMark Johnston 					}
8373b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
8383b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr4,
8393b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask4,
8403b3a8eb9SGleb Smirnoff 					    daddr, AF_INET);
8413b3a8eb9SGleb Smirnoff 					break;
8423b3a8eb9SGleb Smirnoff #endif /* INET */
8433b3a8eb9SGleb Smirnoff #ifdef INET6
8443b3a8eb9SGleb Smirnoff 				case AF_INET6:
8457e65cfc9SMark Johnston 					if (r->src.addr.p.dyn->pfid_acnt6 < 1) {
8467e65cfc9SMark Johnston 						reason = PFRES_MAPFAILED;
8473b3a8eb9SGleb Smirnoff 						goto notrans;
8487e65cfc9SMark Johnston 					}
8493b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
8503b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr6,
8513b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask6,
8523b3a8eb9SGleb Smirnoff 					    daddr, AF_INET6);
8533b3a8eb9SGleb Smirnoff 					break;
8543b3a8eb9SGleb Smirnoff #endif /* INET6 */
8553b3a8eb9SGleb Smirnoff 				}
8563b3a8eb9SGleb Smirnoff 			} else
8573b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
8583b3a8eb9SGleb Smirnoff 				    &r->src.addr.v.a.mask, daddr, pd->af);
8593b3a8eb9SGleb Smirnoff 			break;
8603b3a8eb9SGleb Smirnoff 		}
8613b3a8eb9SGleb Smirnoff 		break;
8623b3a8eb9SGleb Smirnoff 	case PF_RDR: {
8639897a669SMark Johnston 		struct pf_state_key_cmp key;
864339a1977SMark Johnston 		int tries;
8659897a669SMark Johnston 		uint16_t cut, low, high, nport;
8669897a669SMark Johnston 
8678e3d2529SKajetan Staszkiewicz 		reason = pf_map_addr_sn(pd->af, r, saddr, naddr, NULL, NULL, sn);
8687e65cfc9SMark Johnston 		if (reason != 0)
8693b3a8eb9SGleb Smirnoff 			goto notrans;
8703b3a8eb9SGleb Smirnoff 		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
8713b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
8723b3a8eb9SGleb Smirnoff 			    daddr, pd->af);
8733b3a8eb9SGleb Smirnoff 
8746053adafSKristof Provost 		/* Do not change SCTP ports. */
8756053adafSKristof Provost 		if (pd->proto == IPPROTO_SCTP)
8766053adafSKristof Provost 			break;
8776053adafSKristof Provost 
8783b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[1]) {
8793b3a8eb9SGleb Smirnoff 			uint32_t	tmp_nport;
8803b3a8eb9SGleb Smirnoff 
8813b3a8eb9SGleb Smirnoff 			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
8823b3a8eb9SGleb Smirnoff 			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
8833b3a8eb9SGleb Smirnoff 			    1)) + r->rpool.proxy_port[0];
8843b3a8eb9SGleb Smirnoff 
8853b3a8eb9SGleb Smirnoff 			/* Wrap around if necessary. */
8863b3a8eb9SGleb Smirnoff 			if (tmp_nport > 65535)
8873b3a8eb9SGleb Smirnoff 				tmp_nport -= 65535;
8889897a669SMark Johnston 			nport = htons((uint16_t)tmp_nport);
8893b3a8eb9SGleb Smirnoff 		} else if (r->rpool.proxy_port[0])
8909897a669SMark Johnston 			nport = htons(r->rpool.proxy_port[0]);
8919897a669SMark Johnston 		else
8929897a669SMark Johnston 			nport = dport;
8939897a669SMark Johnston 
8949897a669SMark Johnston 		/*
8959897a669SMark Johnston 		 * Update the destination port.
8969897a669SMark Johnston 		 */
8979897a669SMark Johnston 		*nportp = nport;
8989897a669SMark Johnston 
8999897a669SMark Johnston 		/*
9009897a669SMark Johnston 		 * Do we have a source port conflict in the stack state?  Try to
9019897a669SMark Johnston 		 * modulate the source port if so.  Note that this is racy since
9029897a669SMark Johnston 		 * the state lookup may not find any matches here but will once
9039897a669SMark Johnston 		 * pf_create_state() actually instantiates the state.
9049897a669SMark Johnston 		 */
9059897a669SMark Johnston 		bzero(&key, sizeof(key));
9069897a669SMark Johnston 		key.af = pd->af;
9079897a669SMark Johnston 		key.proto = pd->proto;
9089897a669SMark Johnston 		key.port[0] = sport;
9099897a669SMark Johnston 		PF_ACPY(&key.addr[0], saddr, key.af);
9109897a669SMark Johnston 		key.port[1] = nport;
9119897a669SMark Johnston 		PF_ACPY(&key.addr[1], naddr, key.af);
9129897a669SMark Johnston 
9139897a669SMark Johnston 		if (!pf_find_state_all_exists(&key, PF_OUT))
9149897a669SMark Johnston 			break;
9159897a669SMark Johnston 
916339a1977SMark Johnston 		tries = 0;
917339a1977SMark Johnston 
9189897a669SMark Johnston 		low = 50001;	/* XXX-MJ PF_NAT_PROXY_PORT_LOW/HIGH */
9199897a669SMark Johnston 		high = 65535;
9209897a669SMark Johnston 		cut = arc4random() % (1 + high - low) + low;
9219897a669SMark Johnston 		for (uint32_t tmp = cut;
922339a1977SMark Johnston 		    tmp <= high && tmp <= UINT16_MAX &&
923339a1977SMark Johnston 		    tries < V_pf_rdr_srcport_rewrite_tries;
924339a1977SMark Johnston 		    tmp++, tries++) {
9259897a669SMark Johnston 			key.port[0] = htons(tmp);
9269897a669SMark Johnston 			if (!pf_find_state_all_exists(&key, PF_OUT)) {
9279897a669SMark Johnston 				/* Update the source port. */
9289897a669SMark Johnston 				(*nkp)->port[0] = htons(tmp);
9299897a669SMark Johnston 				goto out;
9309897a669SMark Johnston 			}
9319897a669SMark Johnston 		}
932339a1977SMark Johnston 		for (uint32_t tmp = cut - 1;
933339a1977SMark Johnston 		    tmp >= low && tries < V_pf_rdr_srcport_rewrite_tries;
934339a1977SMark Johnston 		    tmp--, tries++) {
9359897a669SMark Johnston 			key.port[0] = htons(tmp);
9369897a669SMark Johnston 			if (!pf_find_state_all_exists(&key, PF_OUT)) {
9379897a669SMark Johnston 				/* Update the source port. */
9389897a669SMark Johnston 				(*nkp)->port[0] = htons(tmp);
9399897a669SMark Johnston 				goto out;
9409897a669SMark Johnston 			}
9419897a669SMark Johnston 		}
9429897a669SMark Johnston 
9439569fdddSMark Johnston 		/*
9449569fdddSMark Johnston 		 * We failed to find a match.  Push on ahead anyway, let
9459569fdddSMark Johnston 		 * pf_state_insert() be the arbiter of whether the state
9469569fdddSMark Johnston 		 * conflict is tolerable.  In particular, with TCP connections
9479569fdddSMark Johnston 		 * the state may be reused if the TCP state is terminal.
9489569fdddSMark Johnston 		 */
9499897a669SMark Johnston 		DPFPRINTF(PF_DEBUG_MISC,
9509897a669SMark Johnston 		    ("pf: RDR source port allocation failed\n"));
9519569fdddSMark Johnston 		break;
9527e65cfc9SMark Johnston 
9539897a669SMark Johnston out:
9549897a669SMark Johnston 		DPFPRINTF(PF_DEBUG_MISC,
9559897a669SMark Johnston 		    ("pf: RDR source port allocation %u->%u\n",
9569897a669SMark Johnston 		    ntohs(sport), ntohs((*nkp)->port[0])));
9573b3a8eb9SGleb Smirnoff 		break;
9583b3a8eb9SGleb Smirnoff 	}
9593b3a8eb9SGleb Smirnoff 	default:
9603b3a8eb9SGleb Smirnoff 		panic("%s: unknown action %u", __func__, r->action);
9613b3a8eb9SGleb Smirnoff 	}
9623b3a8eb9SGleb Smirnoff 
9633b3a8eb9SGleb Smirnoff 	/* Return success only if translation really happened. */
9647e65cfc9SMark Johnston 	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp))) {
9657e65cfc9SMark Johnston 		*rp = r;
9667e65cfc9SMark Johnston 		return (PFRES_MATCH);
9677e65cfc9SMark Johnston 	}
9683b3a8eb9SGleb Smirnoff 
9697e65cfc9SMark Johnston 	reason = PFRES_MAX;
9703b3a8eb9SGleb Smirnoff notrans:
9713b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *nkp);
9723b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *skp);
9733b3a8eb9SGleb Smirnoff 	*skp = *nkp = NULL;
974a830c452SGleb Smirnoff 	*sn = NULL;
9753b3a8eb9SGleb Smirnoff 
9767e65cfc9SMark Johnston 	return (reason);
9773b3a8eb9SGleb Smirnoff }
978