xref: /freebsd/sys/netpfil/pf/pf_lb.c (revision 498cca14836da0408841cca8101e481e32151886)
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 __FBSDID("$FreeBSD$");
413b3a8eb9SGleb Smirnoff 
423b3a8eb9SGleb Smirnoff #include "opt_pf.h"
433b3a8eb9SGleb Smirnoff #include "opt_inet.h"
443b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
453b3a8eb9SGleb Smirnoff 
463b3a8eb9SGleb Smirnoff #include <sys/param.h>
4776039bc8SGleb Smirnoff #include <sys/lock.h>
4876039bc8SGleb Smirnoff #include <sys/mbuf.h>
493b3a8eb9SGleb Smirnoff #include <sys/socket.h>
503b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
513b3a8eb9SGleb Smirnoff 
523b3a8eb9SGleb Smirnoff #include <net/if.h>
5376039bc8SGleb Smirnoff #include <net/vnet.h>
543b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
553b3a8eb9SGleb Smirnoff #include <net/if_pflog.h>
563b3a8eb9SGleb Smirnoff 
573b3a8eb9SGleb Smirnoff #define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
583b3a8eb9SGleb Smirnoff 
593b3a8eb9SGleb Smirnoff static void		 pf_hash(struct pf_addr *, struct pf_addr *,
603b3a8eb9SGleb Smirnoff 			    struct pf_poolhashkey *, sa_family_t);
61e86bddeaSKristof Provost static struct pf_krule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
62320c1116SKristof Provost 			    int, int, struct pfi_kkif *,
633b3a8eb9SGleb Smirnoff 			    struct pf_addr *, u_int16_t, struct pf_addr *,
64e86bddeaSKristof Provost 			    uint16_t, int, struct pf_kanchor_stackframe *);
65e86bddeaSKristof Provost static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *,
668fc6e19cSGleb Smirnoff     struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
6717ad7334SKristof Provost     uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **);
683b3a8eb9SGleb Smirnoff 
693b3a8eb9SGleb Smirnoff #define mix(a,b,c) \
703b3a8eb9SGleb Smirnoff 	do {					\
713b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 13);	\
723b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 8);	\
733b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 13);	\
743b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 12);	\
753b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 16);	\
763b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 5);	\
773b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 3);	\
783b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 10);	\
793b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 15);	\
803b3a8eb9SGleb Smirnoff 	} while (0)
813b3a8eb9SGleb Smirnoff 
823b3a8eb9SGleb Smirnoff /*
833b3a8eb9SGleb Smirnoff  * hash function based on bridge_hash in if_bridge.c
843b3a8eb9SGleb Smirnoff  */
853b3a8eb9SGleb Smirnoff static void
863b3a8eb9SGleb Smirnoff pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
873b3a8eb9SGleb Smirnoff     struct pf_poolhashkey *key, sa_family_t af)
883b3a8eb9SGleb Smirnoff {
893b3a8eb9SGleb Smirnoff 	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
903b3a8eb9SGleb Smirnoff 
913b3a8eb9SGleb Smirnoff 	switch (af) {
923b3a8eb9SGleb Smirnoff #ifdef INET
933b3a8eb9SGleb Smirnoff 	case AF_INET:
943b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
953b3a8eb9SGleb Smirnoff 		b += key->key32[1];
963b3a8eb9SGleb Smirnoff 		mix(a, b, c);
973b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c + key->key32[2];
983b3a8eb9SGleb Smirnoff 		break;
993b3a8eb9SGleb Smirnoff #endif /* INET */
1003b3a8eb9SGleb Smirnoff #ifdef INET6
1013b3a8eb9SGleb Smirnoff 	case AF_INET6:
1023b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
1033b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[2];
1043b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1053b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c;
1063b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[1];
1073b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[3];
1083b3a8eb9SGleb Smirnoff 		c += key->key32[1];
1093b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1103b3a8eb9SGleb Smirnoff 		hash->addr32[1] = c;
1113b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[2];
1123b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[1];
1133b3a8eb9SGleb Smirnoff 		c += key->key32[2];
1143b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1153b3a8eb9SGleb Smirnoff 		hash->addr32[2] = c;
1163b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[3];
1173b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[0];
1183b3a8eb9SGleb Smirnoff 		c += key->key32[3];
1193b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1203b3a8eb9SGleb Smirnoff 		hash->addr32[3] = c;
1213b3a8eb9SGleb Smirnoff 		break;
1223b3a8eb9SGleb Smirnoff #endif /* INET6 */
1233b3a8eb9SGleb Smirnoff 	}
1243b3a8eb9SGleb Smirnoff }
1253b3a8eb9SGleb Smirnoff 
126e86bddeaSKristof Provost static struct pf_krule *
1273b3a8eb9SGleb Smirnoff pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
128320c1116SKristof Provost     int direction, struct pfi_kkif *kif, struct pf_addr *saddr, u_int16_t sport,
1291d6139c0SGleb Smirnoff     struct pf_addr *daddr, uint16_t dport, int rs_num,
130e86bddeaSKristof Provost     struct pf_kanchor_stackframe *anchor_stack)
1313b3a8eb9SGleb Smirnoff {
132e86bddeaSKristof Provost 	struct pf_krule		*r, *rm = NULL;
133e86bddeaSKristof Provost 	struct pf_kruleset	*ruleset = NULL;
1343b3a8eb9SGleb Smirnoff 	int			 tag = -1;
1353b3a8eb9SGleb Smirnoff 	int			 rtableid = -1;
1363b3a8eb9SGleb Smirnoff 	int			 asd = 0;
1373b3a8eb9SGleb Smirnoff 
1383b3a8eb9SGleb Smirnoff 	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1393b3a8eb9SGleb Smirnoff 	while (r && rm == NULL) {
1403b3a8eb9SGleb Smirnoff 		struct pf_rule_addr	*src = NULL, *dst = NULL;
1413b3a8eb9SGleb Smirnoff 		struct pf_addr_wrap	*xdst = NULL;
1423b3a8eb9SGleb Smirnoff 
1433b3a8eb9SGleb Smirnoff 		if (r->action == PF_BINAT && direction == PF_IN) {
1443b3a8eb9SGleb Smirnoff 			src = &r->dst;
1453b3a8eb9SGleb Smirnoff 			if (r->rpool.cur != NULL)
1463b3a8eb9SGleb Smirnoff 				xdst = &r->rpool.cur->addr;
1473b3a8eb9SGleb Smirnoff 		} else {
1483b3a8eb9SGleb Smirnoff 			src = &r->src;
1493b3a8eb9SGleb Smirnoff 			dst = &r->dst;
1503b3a8eb9SGleb Smirnoff 		}
1513b3a8eb9SGleb Smirnoff 
15202cf67ccSMateusz Guzik 		pf_counter_u64_add(&r->evaluations, 1);
153320c1116SKristof Provost 		if (pfi_kkif_match(r->kif, kif) == r->ifnot)
1543b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_IFP].ptr;
1553b3a8eb9SGleb Smirnoff 		else if (r->direction && r->direction != direction)
1563b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DIR].ptr;
1573b3a8eb9SGleb Smirnoff 		else if (r->af && r->af != pd->af)
1583b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_AF].ptr;
1593b3a8eb9SGleb Smirnoff 		else if (r->proto && r->proto != pd->proto)
1603b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_PROTO].ptr;
1613b3a8eb9SGleb Smirnoff 		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
1623b3a8eb9SGleb Smirnoff 		    src->neg, kif, M_GETFIB(m)))
1633b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
1643b3a8eb9SGleb Smirnoff 			    PF_SKIP_DST_ADDR].ptr;
1653b3a8eb9SGleb Smirnoff 		else if (src->port_op && !pf_match_port(src->port_op,
1663b3a8eb9SGleb Smirnoff 		    src->port[0], src->port[1], sport))
1673b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
1683b3a8eb9SGleb Smirnoff 			    PF_SKIP_DST_PORT].ptr;
1693b3a8eb9SGleb Smirnoff 		else if (dst != NULL &&
1703b3a8eb9SGleb Smirnoff 		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
1713b3a8eb9SGleb Smirnoff 		    M_GETFIB(m)))
1723b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
1733b3a8eb9SGleb Smirnoff 		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
1743b3a8eb9SGleb Smirnoff 		    0, NULL, M_GETFIB(m)))
1753b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1763b3a8eb9SGleb Smirnoff 		else if (dst != NULL && dst->port_op &&
1773b3a8eb9SGleb Smirnoff 		    !pf_match_port(dst->port_op, dst->port[0],
1783b3a8eb9SGleb Smirnoff 		    dst->port[1], dport))
1793b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DST_PORT].ptr;
1803b3a8eb9SGleb Smirnoff 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
1813b3a8eb9SGleb Smirnoff 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
1823b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1833b3a8eb9SGleb Smirnoff 		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
1843b3a8eb9SGleb Smirnoff 		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
185d38630f6SKristof Provost 		    off, &pd->hdr.tcp), r->os_fingerprint)))
1863b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1873b3a8eb9SGleb Smirnoff 		else {
1883b3a8eb9SGleb Smirnoff 			if (r->tag)
1893b3a8eb9SGleb Smirnoff 				tag = r->tag;
1903b3a8eb9SGleb Smirnoff 			if (r->rtableid >= 0)
1913b3a8eb9SGleb Smirnoff 				rtableid = r->rtableid;
1923b3a8eb9SGleb Smirnoff 			if (r->anchor == NULL) {
1933b3a8eb9SGleb Smirnoff 				rm = r;
1943b3a8eb9SGleb Smirnoff 			} else
1951d6139c0SGleb Smirnoff 				pf_step_into_anchor(anchor_stack, &asd,
1961d6139c0SGleb Smirnoff 				    &ruleset, rs_num, &r, NULL, NULL);
1973b3a8eb9SGleb Smirnoff 		}
1983b3a8eb9SGleb Smirnoff 		if (r == NULL)
1991d6139c0SGleb Smirnoff 			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
2001d6139c0SGleb Smirnoff 			    rs_num, &r, NULL, NULL);
2013b3a8eb9SGleb Smirnoff 	}
2023b3a8eb9SGleb Smirnoff 
2033b3a8eb9SGleb Smirnoff 	if (tag > 0 && pf_tag_packet(m, pd, tag))
2043b3a8eb9SGleb Smirnoff 		return (NULL);
2053b3a8eb9SGleb Smirnoff 	if (rtableid >= 0)
2063b3a8eb9SGleb Smirnoff 		M_SETFIB(m, rtableid);
2073b3a8eb9SGleb Smirnoff 
2083b3a8eb9SGleb Smirnoff 	if (rm != NULL && (rm->action == PF_NONAT ||
2093b3a8eb9SGleb Smirnoff 	    rm->action == PF_NORDR || rm->action == PF_NOBINAT))
2103b3a8eb9SGleb Smirnoff 		return (NULL);
2113b3a8eb9SGleb Smirnoff 	return (rm);
2123b3a8eb9SGleb Smirnoff }
2133b3a8eb9SGleb Smirnoff 
2143b3a8eb9SGleb Smirnoff static int
215e86bddeaSKristof Provost pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
2168fc6e19cSGleb Smirnoff     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
2178fc6e19cSGleb Smirnoff     uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
21817ad7334SKristof Provost     uint16_t high, struct pf_ksrc_node **sn)
2193b3a8eb9SGleb Smirnoff {
2203b3a8eb9SGleb Smirnoff 	struct pf_state_key_cmp	key;
2213b3a8eb9SGleb Smirnoff 	struct pf_addr		init_addr;
2223b3a8eb9SGleb Smirnoff 
2233b3a8eb9SGleb Smirnoff 	bzero(&init_addr, sizeof(init_addr));
2243b3a8eb9SGleb Smirnoff 	if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
2253b3a8eb9SGleb Smirnoff 		return (1);
2263b3a8eb9SGleb Smirnoff 
2278fc6e19cSGleb Smirnoff 	bzero(&key, sizeof(key));
2283b3a8eb9SGleb Smirnoff 	key.af = af;
2293b3a8eb9SGleb Smirnoff 	key.proto = proto;
2308fc6e19cSGleb Smirnoff 	key.port[0] = dport;
2318fc6e19cSGleb Smirnoff 	PF_ACPY(&key.addr[0], daddr, key.af);
2328fc6e19cSGleb Smirnoff 
2338fc6e19cSGleb Smirnoff 	do {
2348fc6e19cSGleb Smirnoff 		PF_ACPY(&key.addr[1], naddr, key.af);
2353b3a8eb9SGleb Smirnoff 
2363b3a8eb9SGleb Smirnoff 		/*
2373b3a8eb9SGleb Smirnoff 		 * port search; start random, step;
2383b3a8eb9SGleb Smirnoff 		 * similar 2 portloop in in_pcbbind
2393b3a8eb9SGleb Smirnoff 		 */
2403b3a8eb9SGleb Smirnoff 		if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
2418fc6e19cSGleb Smirnoff 		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
2428fc6e19cSGleb Smirnoff 			/*
2438fc6e19cSGleb Smirnoff 			 * XXX bug: icmp states don't use the id on both sides.
2448fc6e19cSGleb Smirnoff 			 * (traceroute -I through nat)
2458fc6e19cSGleb Smirnoff 			 */
2468fc6e19cSGleb Smirnoff 			key.port[1] = sport;
24719d6e29bSMateusz Guzik 			if (!pf_find_state_all_exists(&key, PF_IN)) {
2488fc6e19cSGleb Smirnoff 				*nport = sport;
2493b3a8eb9SGleb Smirnoff 				return (0);
2508fc6e19cSGleb Smirnoff 			}
2513b3a8eb9SGleb Smirnoff 		} else if (low == high) {
2528fc6e19cSGleb Smirnoff 			key.port[1] = htons(low);
25319d6e29bSMateusz Guzik 			if (!pf_find_state_all_exists(&key, PF_IN)) {
2543b3a8eb9SGleb Smirnoff 				*nport = htons(low);
2553b3a8eb9SGleb Smirnoff 				return (0);
2563b3a8eb9SGleb Smirnoff 			}
2573b3a8eb9SGleb Smirnoff 		} else {
2587f3ad018SKristof Provost 			uint32_t tmp;
2597f3ad018SKristof Provost 			uint16_t cut;
2603b3a8eb9SGleb Smirnoff 
2613b3a8eb9SGleb Smirnoff 			if (low > high) {
2623b3a8eb9SGleb Smirnoff 				tmp = low;
2633b3a8eb9SGleb Smirnoff 				low = high;
2643b3a8eb9SGleb Smirnoff 				high = tmp;
2653b3a8eb9SGleb Smirnoff 			}
2663b3a8eb9SGleb Smirnoff 			/* low < high */
267e4e01d9cSGleb Smirnoff 			cut = arc4random() % (1 + high - low) + low;
2683b3a8eb9SGleb Smirnoff 			/* low <= cut <= high */
2697f3ad018SKristof Provost 			for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
2708fc6e19cSGleb Smirnoff 				key.port[1] = htons(tmp);
27119d6e29bSMateusz Guzik 				if (!pf_find_state_all_exists(&key, PF_IN)) {
2723b3a8eb9SGleb Smirnoff 					*nport = htons(tmp);
2733b3a8eb9SGleb Smirnoff 					return (0);
2743b3a8eb9SGleb Smirnoff 				}
2753b3a8eb9SGleb Smirnoff 			}
2767f3ad018SKristof Provost 			tmp = cut;
2777f3ad018SKristof Provost 			for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
2788fc6e19cSGleb Smirnoff 				key.port[1] = htons(tmp);
27919d6e29bSMateusz Guzik 				if (!pf_find_state_all_exists(&key, PF_IN)) {
2803b3a8eb9SGleb Smirnoff 					*nport = htons(tmp);
2813b3a8eb9SGleb Smirnoff 					return (0);
2823b3a8eb9SGleb Smirnoff 				}
2833b3a8eb9SGleb Smirnoff 			}
2843b3a8eb9SGleb Smirnoff 		}
2853b3a8eb9SGleb Smirnoff 
2863b3a8eb9SGleb Smirnoff 		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
2873b3a8eb9SGleb Smirnoff 		case PF_POOL_RANDOM:
2883b3a8eb9SGleb Smirnoff 		case PF_POOL_ROUNDROBIN:
2892b0a4ffaSKristof Provost 			/*
2902b0a4ffaSKristof Provost 			 * pick a different source address since we're out
2912b0a4ffaSKristof Provost 			 * of free port choices for the current one.
2922b0a4ffaSKristof Provost 			 */
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 
3062aa21096SKurosawa Takahiro static int
3072aa21096SKurosawa Takahiro pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
3082aa21096SKurosawa Takahiro     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
3092aa21096SKurosawa Takahiro     uint16_t dport, struct pf_addr *naddr, uint16_t *nport,
3102aa21096SKurosawa Takahiro     struct pf_ksrc_node **sn)
3112aa21096SKurosawa Takahiro {
3122aa21096SKurosawa Takahiro 	uint16_t psmask, low, highmask;
3132aa21096SKurosawa Takahiro 	uint16_t i, ahigh, cut;
3142aa21096SKurosawa Takahiro 	int ashift, psidshift;
3152aa21096SKurosawa Takahiro 
3162aa21096SKurosawa Takahiro 	ashift = 16 - r->rpool.mape.offset;
3172aa21096SKurosawa Takahiro 	psidshift = ashift - r->rpool.mape.psidlen;
3182aa21096SKurosawa Takahiro 	psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1);
3192aa21096SKurosawa Takahiro 	psmask = psmask << psidshift;
3202aa21096SKurosawa Takahiro 	highmask = (1U << psidshift) - 1;
3212aa21096SKurosawa Takahiro 
3222aa21096SKurosawa Takahiro 	ahigh = (1U << r->rpool.mape.offset) - 1;
3232aa21096SKurosawa Takahiro 	cut = arc4random() & ahigh;
3242aa21096SKurosawa Takahiro 	if (cut == 0)
3252aa21096SKurosawa Takahiro 		cut = 1;
3262aa21096SKurosawa Takahiro 
3272aa21096SKurosawa Takahiro 	for (i = cut; i <= ahigh; i++) {
3282aa21096SKurosawa Takahiro 		low = (i << ashift) | psmask;
3292aa21096SKurosawa Takahiro 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
3302aa21096SKurosawa Takahiro 		    naddr, nport, low, low | highmask, sn))
3312aa21096SKurosawa Takahiro 			return (0);
3322aa21096SKurosawa Takahiro 	}
3332aa21096SKurosawa Takahiro 	for (i = cut - 1; i > 0; i--) {
3342aa21096SKurosawa Takahiro 		low = (i << ashift) | psmask;
3352aa21096SKurosawa Takahiro 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
3362aa21096SKurosawa Takahiro 		    naddr, nport, low, low | highmask, sn))
3372aa21096SKurosawa Takahiro 			return (0);
3382aa21096SKurosawa Takahiro 	}
3392aa21096SKurosawa Takahiro 	return (1);
3402aa21096SKurosawa Takahiro }
3412aa21096SKurosawa Takahiro 
3423b3a8eb9SGleb Smirnoff int
343e86bddeaSKristof Provost pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
34417ad7334SKristof Provost     struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_ksrc_node **sn)
3453b3a8eb9SGleb Smirnoff {
346320c1116SKristof Provost 	struct pf_kpool		*rpool = &r->rpool;
3473b3a8eb9SGleb Smirnoff 	struct pf_addr		*raddr = NULL, *rmask = NULL;
3483b3a8eb9SGleb Smirnoff 
349e85343b1SGleb Smirnoff 	/* Try to find a src_node if none was given and this
350e85343b1SGleb Smirnoff 	   is a sticky-address rule. */
3513b3a8eb9SGleb Smirnoff 	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
352e85343b1SGleb Smirnoff 	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
3533b3a8eb9SGleb Smirnoff 		*sn = pf_find_src_node(saddr, r, af, 0);
354e85343b1SGleb Smirnoff 
355e85343b1SGleb Smirnoff 	/* If a src_node was found or explicitly given and it has a non-zero
356e85343b1SGleb Smirnoff 	   route address, use this address. A zeroed address is found if the
357e85343b1SGleb Smirnoff 	   src node was created just a moment ago in pf_create_state and it
358e85343b1SGleb Smirnoff 	   needs to be filled in with routing decision calculated here. */
3593b3a8eb9SGleb Smirnoff 	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
360336683f2SKristof Provost 		/* If the supplied address is the same as the current one we've
361336683f2SKristof Provost 		 * been asked before, so tell the caller that there's no other
362336683f2SKristof Provost 		 * address to be had. */
363336683f2SKristof Provost 		if (PF_AEQ(naddr, &(*sn)->raddr, af))
364336683f2SKristof Provost 			return (1);
365336683f2SKristof Provost 
3663b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &(*sn)->raddr, af);
367*498cca14SKristof Provost 		if (V_pf_status.debug >= PF_DEBUG_NOISY) {
3683b3a8eb9SGleb Smirnoff 			printf("pf_map_addr: src tracking maps ");
3693b3a8eb9SGleb Smirnoff 			pf_print_host(saddr, 0, af);
3703b3a8eb9SGleb Smirnoff 			printf(" to ");
3713b3a8eb9SGleb Smirnoff 			pf_print_host(naddr, 0, af);
3723b3a8eb9SGleb Smirnoff 			printf("\n");
3733b3a8eb9SGleb Smirnoff 		}
3743b3a8eb9SGleb Smirnoff 		return (0);
3753b3a8eb9SGleb Smirnoff 	}
3763b3a8eb9SGleb Smirnoff 
377e85343b1SGleb Smirnoff 	/* Find the route using chosen algorithm. Store the found route
378e85343b1SGleb Smirnoff 	   in src_node if it was given or found. */
3793b3a8eb9SGleb Smirnoff 	if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
3803b3a8eb9SGleb Smirnoff 		return (1);
3813b3a8eb9SGleb Smirnoff 	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
3823b3a8eb9SGleb Smirnoff 		switch (af) {
3833b3a8eb9SGleb Smirnoff #ifdef INET
3843b3a8eb9SGleb Smirnoff 		case AF_INET:
3853b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
3863b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
3873b3a8eb9SGleb Smirnoff 			    PF_POOL_ROUNDROBIN)
3883b3a8eb9SGleb Smirnoff 				return (1);
3893b3a8eb9SGleb Smirnoff 			 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
3903b3a8eb9SGleb Smirnoff 			 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
3913b3a8eb9SGleb Smirnoff 			break;
3923b3a8eb9SGleb Smirnoff #endif /* INET */
3933b3a8eb9SGleb Smirnoff #ifdef INET6
3943b3a8eb9SGleb Smirnoff 		case AF_INET6:
3953b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
3963b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
3973b3a8eb9SGleb Smirnoff 			    PF_POOL_ROUNDROBIN)
3983b3a8eb9SGleb Smirnoff 				return (1);
3993b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
4003b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
4013b3a8eb9SGleb Smirnoff 			break;
4023b3a8eb9SGleb Smirnoff #endif /* INET6 */
4033b3a8eb9SGleb Smirnoff 		}
4043b3a8eb9SGleb Smirnoff 	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
4053b3a8eb9SGleb Smirnoff 		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
4063b3a8eb9SGleb Smirnoff 			return (1); /* unsupported */
4073b3a8eb9SGleb Smirnoff 	} else {
4083b3a8eb9SGleb Smirnoff 		raddr = &rpool->cur->addr.v.a.addr;
4093b3a8eb9SGleb Smirnoff 		rmask = &rpool->cur->addr.v.a.mask;
4103b3a8eb9SGleb Smirnoff 	}
4113b3a8eb9SGleb Smirnoff 
4123b3a8eb9SGleb Smirnoff 	switch (rpool->opts & PF_POOL_TYPEMASK) {
4133b3a8eb9SGleb Smirnoff 	case PF_POOL_NONE:
4143b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, raddr, af);
4153b3a8eb9SGleb Smirnoff 		break;
4163b3a8eb9SGleb Smirnoff 	case PF_POOL_BITMASK:
4173b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
4183b3a8eb9SGleb Smirnoff 		break;
4193b3a8eb9SGleb Smirnoff 	case PF_POOL_RANDOM:
4203b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
4213b3a8eb9SGleb Smirnoff 			switch (af) {
4223b3a8eb9SGleb Smirnoff #ifdef INET
4233b3a8eb9SGleb Smirnoff 			case AF_INET:
4243b3a8eb9SGleb Smirnoff 				rpool->counter.addr32[0] = htonl(arc4random());
4253b3a8eb9SGleb Smirnoff 				break;
4263b3a8eb9SGleb Smirnoff #endif /* INET */
4273b3a8eb9SGleb Smirnoff #ifdef INET6
4283b3a8eb9SGleb Smirnoff 			case AF_INET6:
4293b3a8eb9SGleb Smirnoff 				if (rmask->addr32[3] != 0xffffffff)
4303b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[3] =
4313b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4323b3a8eb9SGleb Smirnoff 				else
4333b3a8eb9SGleb Smirnoff 					break;
4343b3a8eb9SGleb Smirnoff 				if (rmask->addr32[2] != 0xffffffff)
4353b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[2] =
4363b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4373b3a8eb9SGleb Smirnoff 				else
4383b3a8eb9SGleb Smirnoff 					break;
4393b3a8eb9SGleb Smirnoff 				if (rmask->addr32[1] != 0xffffffff)
4403b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[1] =
4413b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4423b3a8eb9SGleb Smirnoff 				else
4433b3a8eb9SGleb Smirnoff 					break;
4443b3a8eb9SGleb Smirnoff 				if (rmask->addr32[0] != 0xffffffff)
4453b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[0] =
4463b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4473b3a8eb9SGleb Smirnoff 				break;
4483b3a8eb9SGleb Smirnoff #endif /* INET6 */
4493b3a8eb9SGleb Smirnoff 			}
4503b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
4513b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
4523b3a8eb9SGleb Smirnoff 
4533b3a8eb9SGleb Smirnoff 		} else {
4543b3a8eb9SGleb Smirnoff 			PF_AINC(&rpool->counter, af);
4553b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
4563b3a8eb9SGleb Smirnoff 		}
4573b3a8eb9SGleb Smirnoff 		break;
4583b3a8eb9SGleb Smirnoff 	case PF_POOL_SRCHASH:
4593b3a8eb9SGleb Smirnoff 	    {
4603b3a8eb9SGleb Smirnoff 		unsigned char hash[16];
4613b3a8eb9SGleb Smirnoff 
4623b3a8eb9SGleb Smirnoff 		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
4633b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
4643b3a8eb9SGleb Smirnoff 		break;
4653b3a8eb9SGleb Smirnoff 	    }
4663b3a8eb9SGleb Smirnoff 	case PF_POOL_ROUNDROBIN:
4673b3a8eb9SGleb Smirnoff 	    {
468320c1116SKristof Provost 		struct pf_kpooladdr *acur = rpool->cur;
4693b3a8eb9SGleb Smirnoff 
4703b3a8eb9SGleb Smirnoff 		/*
4713b3a8eb9SGleb Smirnoff 		 * XXXGL: in the round-robin case we need to store
4723b3a8eb9SGleb Smirnoff 		 * the round-robin machine state in the rule, thus
4733b3a8eb9SGleb Smirnoff 		 * forwarding thread needs to modify rule.
4743b3a8eb9SGleb Smirnoff 		 *
4753b3a8eb9SGleb Smirnoff 		 * This is done w/o locking, because performance is assumed
4763b3a8eb9SGleb Smirnoff 		 * more important than round-robin precision.
4773b3a8eb9SGleb Smirnoff 		 *
4783b3a8eb9SGleb Smirnoff 		 * In the simpliest case we just update the "rpool->cur"
4793b3a8eb9SGleb Smirnoff 		 * pointer. However, if pool contains tables or dynamic
4803b3a8eb9SGleb Smirnoff 		 * addresses, then "tblidx" is also used to store machine
4813b3a8eb9SGleb Smirnoff 		 * state. Since "tblidx" is int, concurrent access to it can't
4823b3a8eb9SGleb Smirnoff 		 * lead to inconsistence, only to lost of precision.
4833b3a8eb9SGleb Smirnoff 		 *
4843b3a8eb9SGleb Smirnoff 		 * Things get worse, if table contains not hosts, but
4853b3a8eb9SGleb Smirnoff 		 * prefixes. In this case counter also stores machine state,
4863b3a8eb9SGleb Smirnoff 		 * and for IPv6 address, counter can't be updated atomically.
4873b3a8eb9SGleb Smirnoff 		 * Probably, using round-robin on a table containing IPv6
4883b3a8eb9SGleb Smirnoff 		 * prefixes (or even IPv4) would cause a panic.
4893b3a8eb9SGleb Smirnoff 		 */
4903b3a8eb9SGleb Smirnoff 
4913b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
4923b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
4933b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af))
4943b3a8eb9SGleb Smirnoff 				goto get_addr;
4953b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
4963b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
4973b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af))
4983b3a8eb9SGleb Smirnoff 				goto get_addr;
4993b3a8eb9SGleb Smirnoff 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
5003b3a8eb9SGleb Smirnoff 			goto get_addr;
5013b3a8eb9SGleb Smirnoff 
5023b3a8eb9SGleb Smirnoff 	try_next:
5033b3a8eb9SGleb Smirnoff 		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
5043b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_FIRST(&rpool->list);
5053b3a8eb9SGleb Smirnoff 		else
5063b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
5073b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
5083b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
5093b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.tbl,
5103b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af)) {
5113b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
5123b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
5133b3a8eb9SGleb Smirnoff 					goto try_next;
5143b3a8eb9SGleb Smirnoff 				return (1);
5153b3a8eb9SGleb Smirnoff 			}
5163b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
5173b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
5183b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
5193b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af)) {
5203b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
5213b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
5223b3a8eb9SGleb Smirnoff 					goto try_next;
5233b3a8eb9SGleb Smirnoff 				return (1);
5243b3a8eb9SGleb Smirnoff 			}
5253b3a8eb9SGleb Smirnoff 		} else {
5263b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.v.a.addr;
5273b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.v.a.mask;
5283b3a8eb9SGleb Smirnoff 			PF_ACPY(&rpool->counter, raddr, af);
5293b3a8eb9SGleb Smirnoff 		}
5303b3a8eb9SGleb Smirnoff 
5313b3a8eb9SGleb Smirnoff 	get_addr:
5323b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &rpool->counter, af);
5333b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af))
5343b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
5353b3a8eb9SGleb Smirnoff 		PF_AINC(&rpool->counter, af);
5363b3a8eb9SGleb Smirnoff 		break;
5373b3a8eb9SGleb Smirnoff 	    }
5383b3a8eb9SGleb Smirnoff 	}
5393b3a8eb9SGleb Smirnoff 	if (*sn != NULL)
5403b3a8eb9SGleb Smirnoff 		PF_ACPY(&(*sn)->raddr, naddr, af);
5413b3a8eb9SGleb Smirnoff 
542*498cca14SKristof Provost 	if (V_pf_status.debug >= PF_DEBUG_NOISY &&
5433b3a8eb9SGleb Smirnoff 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
5443b3a8eb9SGleb Smirnoff 		printf("pf_map_addr: selected address ");
5453b3a8eb9SGleb Smirnoff 		pf_print_host(naddr, 0, af);
5463b3a8eb9SGleb Smirnoff 		printf("\n");
5473b3a8eb9SGleb Smirnoff 	}
5483b3a8eb9SGleb Smirnoff 
5493b3a8eb9SGleb Smirnoff 	return (0);
5503b3a8eb9SGleb Smirnoff }
5513b3a8eb9SGleb Smirnoff 
552e86bddeaSKristof Provost struct pf_krule *
5533b3a8eb9SGleb Smirnoff pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
554320c1116SKristof Provost     struct pfi_kkif *kif, struct pf_ksrc_node **sn,
5553b3a8eb9SGleb Smirnoff     struct pf_state_key **skp, struct pf_state_key **nkp,
5563b3a8eb9SGleb Smirnoff     struct pf_addr *saddr, struct pf_addr *daddr,
557e86bddeaSKristof Provost     uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack)
5583b3a8eb9SGleb Smirnoff {
559e86bddeaSKristof Provost 	struct pf_krule	*r = NULL;
5603b3a8eb9SGleb Smirnoff 	struct pf_addr	*naddr;
5613b3a8eb9SGleb Smirnoff 	uint16_t	*nport;
5622aa21096SKurosawa Takahiro 	uint16_t	 low, high;
5633b3a8eb9SGleb Smirnoff 
5643b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5653b3a8eb9SGleb Smirnoff 	KASSERT(*skp == NULL, ("*skp not NULL"));
5663b3a8eb9SGleb Smirnoff 	KASSERT(*nkp == NULL, ("*nkp not NULL"));
5673b3a8eb9SGleb Smirnoff 
5683b3a8eb9SGleb Smirnoff 	if (direction == PF_OUT) {
5693b3a8eb9SGleb Smirnoff 		r = pf_match_translation(pd, m, off, direction, kif, saddr,
5701d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
5713b3a8eb9SGleb Smirnoff 		if (r == NULL)
5723b3a8eb9SGleb Smirnoff 			r = pf_match_translation(pd, m, off, direction, kif,
5731d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_NAT,
5741d6139c0SGleb Smirnoff 			    anchor_stack);
5753b3a8eb9SGleb Smirnoff 	} else {
5763b3a8eb9SGleb Smirnoff 		r = pf_match_translation(pd, m, off, direction, kif, saddr,
5771d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
5783b3a8eb9SGleb Smirnoff 		if (r == NULL)
5793b3a8eb9SGleb Smirnoff 			r = pf_match_translation(pd, m, off, direction, kif,
5801d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
5811d6139c0SGleb Smirnoff 			    anchor_stack);
5823b3a8eb9SGleb Smirnoff 	}
5833b3a8eb9SGleb Smirnoff 
5843b3a8eb9SGleb Smirnoff 	if (r == NULL)
5853b3a8eb9SGleb Smirnoff 		return (NULL);
5863b3a8eb9SGleb Smirnoff 
5873b3a8eb9SGleb Smirnoff 	switch (r->action) {
5883b3a8eb9SGleb Smirnoff 	case PF_NONAT:
5893b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
5903b3a8eb9SGleb Smirnoff 	case PF_NORDR:
5913b3a8eb9SGleb Smirnoff 		return (NULL);
5923b3a8eb9SGleb Smirnoff 	}
5933b3a8eb9SGleb Smirnoff 
5943b3a8eb9SGleb Smirnoff 	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
5953b3a8eb9SGleb Smirnoff 	if (*skp == NULL)
5963b3a8eb9SGleb Smirnoff 		return (NULL);
5973b3a8eb9SGleb Smirnoff 	*nkp = pf_state_key_clone(*skp);
5983b3a8eb9SGleb Smirnoff 	if (*nkp == NULL) {
59998a9874fSKristof Provost 		uma_zfree(V_pf_state_key_z, *skp);
6003b3a8eb9SGleb Smirnoff 		*skp = NULL;
6013b3a8eb9SGleb Smirnoff 		return (NULL);
6023b3a8eb9SGleb Smirnoff 	}
6033b3a8eb9SGleb Smirnoff 
6043b3a8eb9SGleb Smirnoff 	/* XXX We only modify one side for now. */
6053b3a8eb9SGleb Smirnoff 	naddr = &(*nkp)->addr[1];
6063b3a8eb9SGleb Smirnoff 	nport = &(*nkp)->port[1];
6073b3a8eb9SGleb Smirnoff 
6083b3a8eb9SGleb Smirnoff 	switch (r->action) {
6093b3a8eb9SGleb Smirnoff 	case PF_NAT:
6102aa21096SKurosawa Takahiro 		if (pd->proto == IPPROTO_ICMP) {
6112aa21096SKurosawa Takahiro 			low  = 1;
6122aa21096SKurosawa Takahiro 			high = 65535;
6132aa21096SKurosawa Takahiro 		} else {
6142aa21096SKurosawa Takahiro 			low  = r->rpool.proxy_port[0];
6152aa21096SKurosawa Takahiro 			high = r->rpool.proxy_port[1];
6162aa21096SKurosawa Takahiro 		}
6172aa21096SKurosawa Takahiro 		if (r->rpool.mape.offset > 0) {
6182aa21096SKurosawa Takahiro 			if (pf_get_mape_sport(pd->af, pd->proto, r, saddr,
6192aa21096SKurosawa Takahiro 			    sport, daddr, dport, naddr, nport, sn)) {
6202aa21096SKurosawa Takahiro 				DPFPRINTF(PF_DEBUG_MISC,
6212aa21096SKurosawa Takahiro 				    ("pf: MAP-E port allocation (%u/%u/%u)"
6222aa21096SKurosawa Takahiro 				    " failed\n",
6232aa21096SKurosawa Takahiro 				    r->rpool.mape.offset,
6242aa21096SKurosawa Takahiro 				    r->rpool.mape.psidlen,
6252aa21096SKurosawa Takahiro 				    r->rpool.mape.psid));
6262aa21096SKurosawa Takahiro 				goto notrans;
6272aa21096SKurosawa Takahiro 			}
6282aa21096SKurosawa Takahiro 		} else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport,
6292aa21096SKurosawa Takahiro 		    daddr, dport, naddr, nport, low, high, sn)) {
6303b3a8eb9SGleb Smirnoff 			DPFPRINTF(PF_DEBUG_MISC,
6313b3a8eb9SGleb Smirnoff 			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
6323b3a8eb9SGleb Smirnoff 			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
6333b3a8eb9SGleb Smirnoff 			goto notrans;
6343b3a8eb9SGleb Smirnoff 		}
6353b3a8eb9SGleb Smirnoff 		break;
6363b3a8eb9SGleb Smirnoff 	case PF_BINAT:
6373b3a8eb9SGleb Smirnoff 		switch (direction) {
6383b3a8eb9SGleb Smirnoff 		case PF_OUT:
6393b3a8eb9SGleb Smirnoff 			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
6403b3a8eb9SGleb Smirnoff 				switch (pd->af) {
6413b3a8eb9SGleb Smirnoff #ifdef INET
6423b3a8eb9SGleb Smirnoff 				case AF_INET:
6433b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
6443b3a8eb9SGleb Smirnoff 					    pfid_acnt4 < 1)
6453b3a8eb9SGleb Smirnoff 						goto notrans;
6463b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6473b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6483b3a8eb9SGleb Smirnoff 					    pfid_addr4,
6493b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6503b3a8eb9SGleb Smirnoff 					    pfid_mask4, saddr, AF_INET);
6513b3a8eb9SGleb Smirnoff 					break;
6523b3a8eb9SGleb Smirnoff #endif /* INET */
6533b3a8eb9SGleb Smirnoff #ifdef INET6
6543b3a8eb9SGleb Smirnoff 				case AF_INET6:
6553b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
6563b3a8eb9SGleb Smirnoff 					    pfid_acnt6 < 1)
6573b3a8eb9SGleb Smirnoff 						goto notrans;
6583b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6593b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6603b3a8eb9SGleb Smirnoff 					    pfid_addr6,
6613b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6623b3a8eb9SGleb Smirnoff 					    pfid_mask6, saddr, AF_INET6);
6633b3a8eb9SGleb Smirnoff 					break;
6643b3a8eb9SGleb Smirnoff #endif /* INET6 */
6653b3a8eb9SGleb Smirnoff 				}
6663b3a8eb9SGleb Smirnoff 			} else
6673b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr,
6683b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.addr,
6693b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.mask, saddr,
6703b3a8eb9SGleb Smirnoff 				    pd->af);
6713b3a8eb9SGleb Smirnoff 			break;
6723b3a8eb9SGleb Smirnoff 		case PF_IN:
6733b3a8eb9SGleb Smirnoff 			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
6743b3a8eb9SGleb Smirnoff 				switch (pd->af) {
6753b3a8eb9SGleb Smirnoff #ifdef INET
6763b3a8eb9SGleb Smirnoff 				case AF_INET:
6773b3a8eb9SGleb Smirnoff 					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
6783b3a8eb9SGleb Smirnoff 						goto notrans;
6793b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6803b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr4,
6813b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask4,
6823b3a8eb9SGleb Smirnoff 					    daddr, AF_INET);
6833b3a8eb9SGleb Smirnoff 					break;
6843b3a8eb9SGleb Smirnoff #endif /* INET */
6853b3a8eb9SGleb Smirnoff #ifdef INET6
6863b3a8eb9SGleb Smirnoff 				case AF_INET6:
6873b3a8eb9SGleb Smirnoff 					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
6883b3a8eb9SGleb Smirnoff 						goto notrans;
6893b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6903b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr6,
6913b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask6,
6923b3a8eb9SGleb Smirnoff 					    daddr, AF_INET6);
6933b3a8eb9SGleb Smirnoff 					break;
6943b3a8eb9SGleb Smirnoff #endif /* INET6 */
6953b3a8eb9SGleb Smirnoff 				}
6963b3a8eb9SGleb Smirnoff 			} else
6973b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
6983b3a8eb9SGleb Smirnoff 				    &r->src.addr.v.a.mask, daddr, pd->af);
6993b3a8eb9SGleb Smirnoff 			break;
7003b3a8eb9SGleb Smirnoff 		}
7013b3a8eb9SGleb Smirnoff 		break;
7023b3a8eb9SGleb Smirnoff 	case PF_RDR: {
7033b3a8eb9SGleb Smirnoff 		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
7043b3a8eb9SGleb Smirnoff 			goto notrans;
7053b3a8eb9SGleb Smirnoff 		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
7063b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
7073b3a8eb9SGleb Smirnoff 			    daddr, pd->af);
7083b3a8eb9SGleb Smirnoff 
7093b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[1]) {
7103b3a8eb9SGleb Smirnoff 			uint32_t	tmp_nport;
7113b3a8eb9SGleb Smirnoff 
7123b3a8eb9SGleb Smirnoff 			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
7133b3a8eb9SGleb Smirnoff 			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
7143b3a8eb9SGleb Smirnoff 			    1)) + r->rpool.proxy_port[0];
7153b3a8eb9SGleb Smirnoff 
7163b3a8eb9SGleb Smirnoff 			/* Wrap around if necessary. */
7173b3a8eb9SGleb Smirnoff 			if (tmp_nport > 65535)
7183b3a8eb9SGleb Smirnoff 				tmp_nport -= 65535;
7193b3a8eb9SGleb Smirnoff 			*nport = htons((uint16_t)tmp_nport);
7203b3a8eb9SGleb Smirnoff 		} else if (r->rpool.proxy_port[0])
7213b3a8eb9SGleb Smirnoff 			*nport = htons(r->rpool.proxy_port[0]);
7223b3a8eb9SGleb Smirnoff 		break;
7233b3a8eb9SGleb Smirnoff 	}
7243b3a8eb9SGleb Smirnoff 	default:
7253b3a8eb9SGleb Smirnoff 		panic("%s: unknown action %u", __func__, r->action);
7263b3a8eb9SGleb Smirnoff 	}
7273b3a8eb9SGleb Smirnoff 
7283b3a8eb9SGleb Smirnoff 	/* Return success only if translation really happened. */
7293b3a8eb9SGleb Smirnoff 	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
7303b3a8eb9SGleb Smirnoff 		return (r);
7313b3a8eb9SGleb Smirnoff 
7323b3a8eb9SGleb Smirnoff notrans:
7333b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *nkp);
7343b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *skp);
7353b3a8eb9SGleb Smirnoff 	*skp = *nkp = NULL;
736a830c452SGleb Smirnoff 	*sn = NULL;
7373b3a8eb9SGleb Smirnoff 
7383b3a8eb9SGleb Smirnoff 	return (NULL);
7393b3a8eb9SGleb Smirnoff }
740