xref: /freebsd/sys/netpfil/pf/pf_lb.c (revision 9897a66923a3e79c22fcbd4bc80afae9eb9f277c)
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 
553b3a8eb9SGleb Smirnoff #define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
563b3a8eb9SGleb Smirnoff 
573b3a8eb9SGleb Smirnoff static void		 pf_hash(struct pf_addr *, struct pf_addr *,
583b3a8eb9SGleb Smirnoff 			    struct pf_poolhashkey *, sa_family_t);
59e86bddeaSKristof Provost static struct pf_krule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
60f2064dd1SKajetan Staszkiewicz 			    int, struct pfi_kkif *,
613b3a8eb9SGleb Smirnoff 			    struct pf_addr *, u_int16_t, struct pf_addr *,
62e86bddeaSKristof Provost 			    uint16_t, int, struct pf_kanchor_stackframe *);
63e86bddeaSKristof Provost static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *,
648fc6e19cSGleb Smirnoff     struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
6517ad7334SKristof Provost     uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **);
663b3a8eb9SGleb Smirnoff 
673b3a8eb9SGleb Smirnoff #define mix(a,b,c) \
683b3a8eb9SGleb Smirnoff 	do {					\
693b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 13);	\
703b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 8);	\
713b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 13);	\
723b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 12);	\
733b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 16);	\
743b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 5);	\
753b3a8eb9SGleb Smirnoff 		a -= b; a -= c; a ^= (c >> 3);	\
763b3a8eb9SGleb Smirnoff 		b -= c; b -= a; b ^= (a << 10);	\
773b3a8eb9SGleb Smirnoff 		c -= a; c -= b; c ^= (b >> 15);	\
783b3a8eb9SGleb Smirnoff 	} while (0)
793b3a8eb9SGleb Smirnoff 
803b3a8eb9SGleb Smirnoff /*
813b3a8eb9SGleb Smirnoff  * hash function based on bridge_hash in if_bridge.c
823b3a8eb9SGleb Smirnoff  */
833b3a8eb9SGleb Smirnoff static void
843b3a8eb9SGleb Smirnoff pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
853b3a8eb9SGleb Smirnoff     struct pf_poolhashkey *key, sa_family_t af)
863b3a8eb9SGleb Smirnoff {
873b3a8eb9SGleb Smirnoff 	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
883b3a8eb9SGleb Smirnoff 
893b3a8eb9SGleb Smirnoff 	switch (af) {
903b3a8eb9SGleb Smirnoff #ifdef INET
913b3a8eb9SGleb Smirnoff 	case AF_INET:
923b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
933b3a8eb9SGleb Smirnoff 		b += key->key32[1];
943b3a8eb9SGleb Smirnoff 		mix(a, b, c);
953b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c + key->key32[2];
963b3a8eb9SGleb Smirnoff 		break;
973b3a8eb9SGleb Smirnoff #endif /* INET */
983b3a8eb9SGleb Smirnoff #ifdef INET6
993b3a8eb9SGleb Smirnoff 	case AF_INET6:
1003b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[0];
1013b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[2];
1023b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1033b3a8eb9SGleb Smirnoff 		hash->addr32[0] = c;
1043b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[1];
1053b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[3];
1063b3a8eb9SGleb Smirnoff 		c += key->key32[1];
1073b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1083b3a8eb9SGleb Smirnoff 		hash->addr32[1] = c;
1093b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[2];
1103b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[1];
1113b3a8eb9SGleb Smirnoff 		c += key->key32[2];
1123b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1133b3a8eb9SGleb Smirnoff 		hash->addr32[2] = c;
1143b3a8eb9SGleb Smirnoff 		a += inaddr->addr32[3];
1153b3a8eb9SGleb Smirnoff 		b += inaddr->addr32[0];
1163b3a8eb9SGleb Smirnoff 		c += key->key32[3];
1173b3a8eb9SGleb Smirnoff 		mix(a, b, c);
1183b3a8eb9SGleb Smirnoff 		hash->addr32[3] = c;
1193b3a8eb9SGleb Smirnoff 		break;
1203b3a8eb9SGleb Smirnoff #endif /* INET6 */
1213b3a8eb9SGleb Smirnoff 	}
1223b3a8eb9SGleb Smirnoff }
1233b3a8eb9SGleb Smirnoff 
124e86bddeaSKristof Provost static struct pf_krule *
1253b3a8eb9SGleb Smirnoff pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
126f2064dd1SKajetan Staszkiewicz     struct pfi_kkif *kif, struct pf_addr *saddr, u_int16_t sport,
1271d6139c0SGleb Smirnoff     struct pf_addr *daddr, uint16_t dport, int rs_num,
128e86bddeaSKristof Provost     struct pf_kanchor_stackframe *anchor_stack)
1293b3a8eb9SGleb Smirnoff {
130e86bddeaSKristof Provost 	struct pf_krule		*r, *rm = NULL;
131e86bddeaSKristof Provost 	struct pf_kruleset	*ruleset = NULL;
1323b3a8eb9SGleb Smirnoff 	int			 tag = -1;
1333b3a8eb9SGleb Smirnoff 	int			 rtableid = -1;
1343b3a8eb9SGleb Smirnoff 	int			 asd = 0;
1353b3a8eb9SGleb Smirnoff 
1363b3a8eb9SGleb Smirnoff 	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
1376b94546aSMateusz Guzik 	while (r != NULL) {
1383b3a8eb9SGleb Smirnoff 		struct pf_rule_addr	*src = NULL, *dst = NULL;
1393b3a8eb9SGleb Smirnoff 		struct pf_addr_wrap	*xdst = NULL;
1403b3a8eb9SGleb Smirnoff 
141f2064dd1SKajetan Staszkiewicz 		if (r->action == PF_BINAT && pd->dir == PF_IN) {
1423b3a8eb9SGleb Smirnoff 			src = &r->dst;
1433b3a8eb9SGleb Smirnoff 			if (r->rpool.cur != NULL)
1443b3a8eb9SGleb Smirnoff 				xdst = &r->rpool.cur->addr;
1453b3a8eb9SGleb Smirnoff 		} else {
1463b3a8eb9SGleb Smirnoff 			src = &r->src;
1473b3a8eb9SGleb Smirnoff 			dst = &r->dst;
1483b3a8eb9SGleb Smirnoff 		}
1493b3a8eb9SGleb Smirnoff 
15002cf67ccSMateusz Guzik 		pf_counter_u64_add(&r->evaluations, 1);
151320c1116SKristof Provost 		if (pfi_kkif_match(r->kif, kif) == r->ifnot)
1523b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_IFP].ptr;
153f2064dd1SKajetan Staszkiewicz 		else if (r->direction && r->direction != pd->dir)
1543b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DIR].ptr;
1553b3a8eb9SGleb Smirnoff 		else if (r->af && r->af != pd->af)
1563b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_AF].ptr;
1573b3a8eb9SGleb Smirnoff 		else if (r->proto && r->proto != pd->proto)
1583b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_PROTO].ptr;
1593b3a8eb9SGleb Smirnoff 		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
1603b3a8eb9SGleb Smirnoff 		    src->neg, kif, M_GETFIB(m)))
1613b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
1623b3a8eb9SGleb Smirnoff 			    PF_SKIP_DST_ADDR].ptr;
1633b3a8eb9SGleb Smirnoff 		else if (src->port_op && !pf_match_port(src->port_op,
1643b3a8eb9SGleb Smirnoff 		    src->port[0], src->port[1], sport))
1653b3a8eb9SGleb Smirnoff 			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
1663b3a8eb9SGleb Smirnoff 			    PF_SKIP_DST_PORT].ptr;
1673b3a8eb9SGleb Smirnoff 		else if (dst != NULL &&
1683b3a8eb9SGleb Smirnoff 		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
1693b3a8eb9SGleb Smirnoff 		    M_GETFIB(m)))
1703b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DST_ADDR].ptr;
1713b3a8eb9SGleb Smirnoff 		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
1723b3a8eb9SGleb Smirnoff 		    0, NULL, M_GETFIB(m)))
1733b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1743b3a8eb9SGleb Smirnoff 		else if (dst != NULL && dst->port_op &&
1753b3a8eb9SGleb Smirnoff 		    !pf_match_port(dst->port_op, dst->port[0],
1763b3a8eb9SGleb Smirnoff 		    dst->port[1], dport))
1773b3a8eb9SGleb Smirnoff 			r = r->skip[PF_SKIP_DST_PORT].ptr;
1783b3a8eb9SGleb Smirnoff 		else if (r->match_tag && !pf_match_tag(m, r, &tag,
1793b3a8eb9SGleb Smirnoff 		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
1803b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1813b3a8eb9SGleb Smirnoff 		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
1823b3a8eb9SGleb Smirnoff 		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
183d38630f6SKristof Provost 		    off, &pd->hdr.tcp), r->os_fingerprint)))
1843b3a8eb9SGleb Smirnoff 			r = TAILQ_NEXT(r, entries);
1853b3a8eb9SGleb Smirnoff 		else {
1863b3a8eb9SGleb Smirnoff 			if (r->tag)
1873b3a8eb9SGleb Smirnoff 				tag = r->tag;
1883b3a8eb9SGleb Smirnoff 			if (r->rtableid >= 0)
1893b3a8eb9SGleb Smirnoff 				rtableid = r->rtableid;
1903b3a8eb9SGleb Smirnoff 			if (r->anchor == NULL) {
1913b3a8eb9SGleb Smirnoff 				rm = r;
1926b94546aSMateusz Guzik 				if (rm->action == PF_NONAT ||
1936b94546aSMateusz Guzik 				    rm->action == PF_NORDR ||
1946b94546aSMateusz Guzik 				    rm->action == PF_NOBINAT) {
1956b94546aSMateusz Guzik 					rm = NULL;
1966b94546aSMateusz Guzik 				}
1976b94546aSMateusz Guzik 				break;
1983b3a8eb9SGleb Smirnoff 			} else
1991d6139c0SGleb Smirnoff 				pf_step_into_anchor(anchor_stack, &asd,
2001d6139c0SGleb Smirnoff 				    &ruleset, rs_num, &r, NULL, NULL);
2013b3a8eb9SGleb Smirnoff 		}
2023b3a8eb9SGleb Smirnoff 		if (r == NULL)
2031d6139c0SGleb Smirnoff 			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
2041d6139c0SGleb Smirnoff 			    rs_num, &r, NULL, NULL);
2053b3a8eb9SGleb Smirnoff 	}
2063b3a8eb9SGleb Smirnoff 
2073b3a8eb9SGleb Smirnoff 	if (tag > 0 && pf_tag_packet(m, pd, tag))
2083b3a8eb9SGleb Smirnoff 		return (NULL);
2093b3a8eb9SGleb Smirnoff 	if (rtableid >= 0)
2103b3a8eb9SGleb Smirnoff 		M_SETFIB(m, rtableid);
2113b3a8eb9SGleb Smirnoff 
2123b3a8eb9SGleb Smirnoff 	return (rm);
2133b3a8eb9SGleb Smirnoff }
2143b3a8eb9SGleb Smirnoff 
2153b3a8eb9SGleb Smirnoff static int
216e86bddeaSKristof Provost pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
2178fc6e19cSGleb Smirnoff     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
2188fc6e19cSGleb Smirnoff     uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
21917ad7334SKristof Provost     uint16_t high, struct pf_ksrc_node **sn)
2203b3a8eb9SGleb Smirnoff {
2213b3a8eb9SGleb Smirnoff 	struct pf_state_key_cmp	key;
2223b3a8eb9SGleb Smirnoff 	struct pf_addr		init_addr;
2233b3a8eb9SGleb Smirnoff 
2243b3a8eb9SGleb Smirnoff 	bzero(&init_addr, sizeof(init_addr));
225d10de21fSKajetan Staszkiewicz 	if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn))
2263b3a8eb9SGleb Smirnoff 		return (1);
2273b3a8eb9SGleb Smirnoff 
228534ee17eSKristof Provost 	if (proto == IPPROTO_ICMP) {
229534ee17eSKristof Provost 		if (*nport == htons(ICMP_ECHO)) {
230534ee17eSKristof Provost 			low = 1;
231534ee17eSKristof Provost 			high = 65535;
232534ee17eSKristof Provost 		} else
233534ee17eSKristof Provost 			return (0);	/* Don't try to modify non-echo ICMP */
234534ee17eSKristof Provost 	}
235534ee17eSKristof Provost #ifdef INET6
236534ee17eSKristof Provost 	if (proto == IPPROTO_ICMPV6) {
237534ee17eSKristof Provost 		if (*nport == htons(ICMP6_ECHO_REQUEST)) {
238534ee17eSKristof Provost 			low = 1;
239534ee17eSKristof Provost 			high = 65535;
240534ee17eSKristof Provost 		} else
241534ee17eSKristof Provost 			return (0);	/* Don't try to modify non-echo ICMP */
242534ee17eSKristof Provost 	}
243534ee17eSKristof Provost #endif /* INET6 */
244534ee17eSKristof Provost 
2458fc6e19cSGleb Smirnoff 	bzero(&key, sizeof(key));
2463b3a8eb9SGleb Smirnoff 	key.af = af;
2473b3a8eb9SGleb Smirnoff 	key.proto = proto;
2488fc6e19cSGleb Smirnoff 	key.port[0] = dport;
2498fc6e19cSGleb Smirnoff 	PF_ACPY(&key.addr[0], daddr, key.af);
2508fc6e19cSGleb Smirnoff 
2518fc6e19cSGleb Smirnoff 	do {
2528fc6e19cSGleb Smirnoff 		PF_ACPY(&key.addr[1], naddr, key.af);
2533b3a8eb9SGleb Smirnoff 
2543b3a8eb9SGleb Smirnoff 		/*
2553b3a8eb9SGleb Smirnoff 		 * port search; start random, step;
2563b3a8eb9SGleb Smirnoff 		 * similar 2 portloop in in_pcbbind
2573b3a8eb9SGleb Smirnoff 		 */
2586053adafSKristof Provost 		if (proto == IPPROTO_SCTP) {
2596053adafSKristof Provost 			key.port[1] = sport;
2606053adafSKristof Provost 			if (!pf_find_state_all_exists(&key, PF_IN)) {
2616053adafSKristof Provost 				*nport = sport;
2626053adafSKristof Provost 				return (0);
2636053adafSKristof Provost 			} else {
2646053adafSKristof Provost 				return (1); /* Fail mapping. */
2656053adafSKristof Provost 			}
2666053adafSKristof Provost 		} else if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
2678fc6e19cSGleb Smirnoff 		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
2688fc6e19cSGleb Smirnoff 			/*
2698fc6e19cSGleb Smirnoff 			 * XXX bug: icmp states don't use the id on both sides.
2708fc6e19cSGleb Smirnoff 			 * (traceroute -I through nat)
2718fc6e19cSGleb Smirnoff 			 */
2728fc6e19cSGleb Smirnoff 			key.port[1] = sport;
27319d6e29bSMateusz Guzik 			if (!pf_find_state_all_exists(&key, PF_IN)) {
2748fc6e19cSGleb Smirnoff 				*nport = sport;
2753b3a8eb9SGleb Smirnoff 				return (0);
2768fc6e19cSGleb Smirnoff 			}
2773b3a8eb9SGleb Smirnoff 		} else if (low == high) {
2788fc6e19cSGleb Smirnoff 			key.port[1] = htons(low);
27919d6e29bSMateusz Guzik 			if (!pf_find_state_all_exists(&key, PF_IN)) {
2803b3a8eb9SGleb Smirnoff 				*nport = htons(low);
2813b3a8eb9SGleb Smirnoff 				return (0);
2823b3a8eb9SGleb Smirnoff 			}
2833b3a8eb9SGleb Smirnoff 		} else {
2847f3ad018SKristof Provost 			uint32_t tmp;
2857f3ad018SKristof Provost 			uint16_t cut;
2863b3a8eb9SGleb Smirnoff 
2873b3a8eb9SGleb Smirnoff 			if (low > high) {
2883b3a8eb9SGleb Smirnoff 				tmp = low;
2893b3a8eb9SGleb Smirnoff 				low = high;
2903b3a8eb9SGleb Smirnoff 				high = tmp;
2913b3a8eb9SGleb Smirnoff 			}
2923b3a8eb9SGleb Smirnoff 			/* low < high */
293e4e01d9cSGleb Smirnoff 			cut = arc4random() % (1 + high - low) + low;
2943b3a8eb9SGleb Smirnoff 			/* low <= cut <= high */
2957f3ad018SKristof Provost 			for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
2968fc6e19cSGleb Smirnoff 				key.port[1] = htons(tmp);
29719d6e29bSMateusz Guzik 				if (!pf_find_state_all_exists(&key, PF_IN)) {
2983b3a8eb9SGleb Smirnoff 					*nport = htons(tmp);
2993b3a8eb9SGleb Smirnoff 					return (0);
3003b3a8eb9SGleb Smirnoff 				}
3013b3a8eb9SGleb Smirnoff 			}
3027f3ad018SKristof Provost 			tmp = cut;
3037f3ad018SKristof Provost 			for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
3048fc6e19cSGleb Smirnoff 				key.port[1] = htons(tmp);
30519d6e29bSMateusz Guzik 				if (!pf_find_state_all_exists(&key, PF_IN)) {
3063b3a8eb9SGleb Smirnoff 					*nport = htons(tmp);
3073b3a8eb9SGleb Smirnoff 					return (0);
3083b3a8eb9SGleb Smirnoff 				}
3093b3a8eb9SGleb Smirnoff 			}
3103b3a8eb9SGleb Smirnoff 		}
3113b3a8eb9SGleb Smirnoff 
3123b3a8eb9SGleb Smirnoff 		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
3133b3a8eb9SGleb Smirnoff 		case PF_POOL_RANDOM:
3143b3a8eb9SGleb Smirnoff 		case PF_POOL_ROUNDROBIN:
3152b0a4ffaSKristof Provost 			/*
3162b0a4ffaSKristof Provost 			 * pick a different source address since we're out
3172b0a4ffaSKristof Provost 			 * of free port choices for the current one.
3182b0a4ffaSKristof Provost 			 */
319d10de21fSKajetan Staszkiewicz 			if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn))
3203b3a8eb9SGleb Smirnoff 				return (1);
3213b3a8eb9SGleb Smirnoff 			break;
3223b3a8eb9SGleb Smirnoff 		case PF_POOL_NONE:
3233b3a8eb9SGleb Smirnoff 		case PF_POOL_SRCHASH:
3243b3a8eb9SGleb Smirnoff 		case PF_POOL_BITMASK:
3253b3a8eb9SGleb Smirnoff 		default:
3263b3a8eb9SGleb Smirnoff 			return (1);
3273b3a8eb9SGleb Smirnoff 		}
3283b3a8eb9SGleb Smirnoff 	} while (! PF_AEQ(&init_addr, naddr, af) );
3293b3a8eb9SGleb Smirnoff 	return (1);					/* none available */
3303b3a8eb9SGleb Smirnoff }
3313b3a8eb9SGleb Smirnoff 
3322aa21096SKurosawa Takahiro static int
3332aa21096SKurosawa Takahiro pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
3342aa21096SKurosawa Takahiro     struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
3352aa21096SKurosawa Takahiro     uint16_t dport, struct pf_addr *naddr, uint16_t *nport,
3362aa21096SKurosawa Takahiro     struct pf_ksrc_node **sn)
3372aa21096SKurosawa Takahiro {
3382aa21096SKurosawa Takahiro 	uint16_t psmask, low, highmask;
3392aa21096SKurosawa Takahiro 	uint16_t i, ahigh, cut;
3402aa21096SKurosawa Takahiro 	int ashift, psidshift;
3412aa21096SKurosawa Takahiro 
3422aa21096SKurosawa Takahiro 	ashift = 16 - r->rpool.mape.offset;
3432aa21096SKurosawa Takahiro 	psidshift = ashift - r->rpool.mape.psidlen;
3442aa21096SKurosawa Takahiro 	psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1);
3452aa21096SKurosawa Takahiro 	psmask = psmask << psidshift;
3462aa21096SKurosawa Takahiro 	highmask = (1U << psidshift) - 1;
3472aa21096SKurosawa Takahiro 
3482aa21096SKurosawa Takahiro 	ahigh = (1U << r->rpool.mape.offset) - 1;
3492aa21096SKurosawa Takahiro 	cut = arc4random() & ahigh;
3502aa21096SKurosawa Takahiro 	if (cut == 0)
3512aa21096SKurosawa Takahiro 		cut = 1;
3522aa21096SKurosawa Takahiro 
3532aa21096SKurosawa Takahiro 	for (i = cut; i <= ahigh; i++) {
3542aa21096SKurosawa Takahiro 		low = (i << ashift) | psmask;
3552aa21096SKurosawa Takahiro 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
3562aa21096SKurosawa Takahiro 		    naddr, nport, low, low | highmask, sn))
3572aa21096SKurosawa Takahiro 			return (0);
3582aa21096SKurosawa Takahiro 	}
3592aa21096SKurosawa Takahiro 	for (i = cut - 1; i > 0; i--) {
3602aa21096SKurosawa Takahiro 		low = (i << ashift) | psmask;
3612aa21096SKurosawa Takahiro 		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
3622aa21096SKurosawa Takahiro 		    naddr, nport, low, low | highmask, sn))
3632aa21096SKurosawa Takahiro 			return (0);
3642aa21096SKurosawa Takahiro 	}
3652aa21096SKurosawa Takahiro 	return (1);
3662aa21096SKurosawa Takahiro }
3672aa21096SKurosawa Takahiro 
36816303d2bSKajetan Staszkiewicz u_short
369e86bddeaSKristof Provost pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
370d10de21fSKajetan Staszkiewicz     struct pf_addr *naddr, struct pfi_kkif **nkif, struct pf_addr *init_addr,
371d10de21fSKajetan Staszkiewicz     struct pf_ksrc_node **sn)
3723b3a8eb9SGleb Smirnoff {
37316303d2bSKajetan Staszkiewicz 	u_short			 reason = 0;
374320c1116SKristof Provost 	struct pf_kpool		*rpool = &r->rpool;
3753b3a8eb9SGleb Smirnoff 	struct pf_addr		*raddr = NULL, *rmask = NULL;
376db0a2bfdSKajetan Staszkiewicz 	struct pf_srchash	*sh = NULL;
3773b3a8eb9SGleb Smirnoff 
378e85343b1SGleb Smirnoff 	/* Try to find a src_node if none was given and this
379e85343b1SGleb Smirnoff 	   is a sticky-address rule. */
3803b3a8eb9SGleb Smirnoff 	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
381e85343b1SGleb Smirnoff 	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
382db0a2bfdSKajetan Staszkiewicz 		*sn = pf_find_src_node(saddr, r, af, &sh, false);
383e85343b1SGleb Smirnoff 
384e85343b1SGleb Smirnoff 	/* If a src_node was found or explicitly given and it has a non-zero
385e85343b1SGleb Smirnoff 	   route address, use this address. A zeroed address is found if the
386e85343b1SGleb Smirnoff 	   src node was created just a moment ago in pf_create_state and it
387e85343b1SGleb Smirnoff 	   needs to be filled in with routing decision calculated here. */
3883b3a8eb9SGleb Smirnoff 	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
389336683f2SKristof Provost 		/* If the supplied address is the same as the current one we've
390336683f2SKristof Provost 		 * been asked before, so tell the caller that there's no other
391336683f2SKristof Provost 		 * address to be had. */
39216303d2bSKajetan Staszkiewicz 		if (PF_AEQ(naddr, &(*sn)->raddr, af)) {
39316303d2bSKajetan Staszkiewicz 			reason = PFRES_MAPFAILED;
39416303d2bSKajetan Staszkiewicz 			goto done;
39516303d2bSKajetan Staszkiewicz 		}
396336683f2SKristof Provost 
3973b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &(*sn)->raddr, af);
398d10de21fSKajetan Staszkiewicz 		if (nkif)
399d10de21fSKajetan Staszkiewicz 			*nkif = (*sn)->rkif;
400498cca14SKristof Provost 		if (V_pf_status.debug >= PF_DEBUG_NOISY) {
4013b3a8eb9SGleb Smirnoff 			printf("pf_map_addr: src tracking maps ");
4023b3a8eb9SGleb Smirnoff 			pf_print_host(saddr, 0, af);
4033b3a8eb9SGleb Smirnoff 			printf(" to ");
4043b3a8eb9SGleb Smirnoff 			pf_print_host(naddr, 0, af);
405d10de21fSKajetan Staszkiewicz 			if (nkif)
406d10de21fSKajetan Staszkiewicz 				printf("@%s", (*nkif)->pfik_name);
4073b3a8eb9SGleb Smirnoff 			printf("\n");
4083b3a8eb9SGleb Smirnoff 		}
40916303d2bSKajetan Staszkiewicz 		goto done;
4103b3a8eb9SGleb Smirnoff 	}
4113b3a8eb9SGleb Smirnoff 
4125f5e32f1SKristof Provost 	mtx_lock(&rpool->mtx);
413e85343b1SGleb Smirnoff 	/* Find the route using chosen algorithm. Store the found route
414e85343b1SGleb Smirnoff 	   in src_node if it was given or found. */
4155f5e32f1SKristof Provost 	if (rpool->cur->addr.type == PF_ADDR_NOROUTE) {
41616303d2bSKajetan Staszkiewicz 		reason = PFRES_MAPFAILED;
41716303d2bSKajetan Staszkiewicz 		goto done_pool_mtx;
4185f5e32f1SKristof Provost 	}
4193b3a8eb9SGleb Smirnoff 	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
4203b3a8eb9SGleb Smirnoff 		switch (af) {
4213b3a8eb9SGleb Smirnoff #ifdef INET
4223b3a8eb9SGleb Smirnoff 		case AF_INET:
4233b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
4243b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
4255f5e32f1SKristof Provost 			    PF_POOL_ROUNDROBIN) {
42616303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
42716303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
4285f5e32f1SKristof Provost 			}
4293b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
4303b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
4313b3a8eb9SGleb Smirnoff 			break;
4323b3a8eb9SGleb Smirnoff #endif /* INET */
4333b3a8eb9SGleb Smirnoff #ifdef INET6
4343b3a8eb9SGleb Smirnoff 		case AF_INET6:
4353b3a8eb9SGleb Smirnoff 			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
4363b3a8eb9SGleb Smirnoff 			    (rpool->opts & PF_POOL_TYPEMASK) !=
4375f5e32f1SKristof Provost 			    PF_POOL_ROUNDROBIN) {
43816303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
43916303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
4405f5e32f1SKristof Provost 			}
4413b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
4423b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
4433b3a8eb9SGleb Smirnoff 			break;
4443b3a8eb9SGleb Smirnoff #endif /* INET6 */
4453b3a8eb9SGleb Smirnoff 		}
4463b3a8eb9SGleb Smirnoff 	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
4475f5e32f1SKristof Provost 		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN) {
44816303d2bSKajetan Staszkiewicz 			reason = PFRES_MAPFAILED;
44916303d2bSKajetan Staszkiewicz 			goto done_pool_mtx; /* unsupported */
4505f5e32f1SKristof Provost 		}
4513b3a8eb9SGleb Smirnoff 	} else {
4523b3a8eb9SGleb Smirnoff 		raddr = &rpool->cur->addr.v.a.addr;
4533b3a8eb9SGleb Smirnoff 		rmask = &rpool->cur->addr.v.a.mask;
4543b3a8eb9SGleb Smirnoff 	}
4553b3a8eb9SGleb Smirnoff 
4563b3a8eb9SGleb Smirnoff 	switch (rpool->opts & PF_POOL_TYPEMASK) {
4573b3a8eb9SGleb Smirnoff 	case PF_POOL_NONE:
4583b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, raddr, af);
4593b3a8eb9SGleb Smirnoff 		break;
4603b3a8eb9SGleb Smirnoff 	case PF_POOL_BITMASK:
4613b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
4623b3a8eb9SGleb Smirnoff 		break;
4633b3a8eb9SGleb Smirnoff 	case PF_POOL_RANDOM:
4643b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
4653b3a8eb9SGleb Smirnoff 			switch (af) {
4663b3a8eb9SGleb Smirnoff #ifdef INET
4673b3a8eb9SGleb Smirnoff 			case AF_INET:
4683b3a8eb9SGleb Smirnoff 				rpool->counter.addr32[0] = htonl(arc4random());
4693b3a8eb9SGleb Smirnoff 				break;
4703b3a8eb9SGleb Smirnoff #endif /* INET */
4713b3a8eb9SGleb Smirnoff #ifdef INET6
4723b3a8eb9SGleb Smirnoff 			case AF_INET6:
4733b3a8eb9SGleb Smirnoff 				if (rmask->addr32[3] != 0xffffffff)
4743b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[3] =
4753b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4763b3a8eb9SGleb Smirnoff 				else
4773b3a8eb9SGleb Smirnoff 					break;
4783b3a8eb9SGleb Smirnoff 				if (rmask->addr32[2] != 0xffffffff)
4793b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[2] =
4803b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4813b3a8eb9SGleb Smirnoff 				else
4823b3a8eb9SGleb Smirnoff 					break;
4833b3a8eb9SGleb Smirnoff 				if (rmask->addr32[1] != 0xffffffff)
4843b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[1] =
4853b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4863b3a8eb9SGleb Smirnoff 				else
4873b3a8eb9SGleb Smirnoff 					break;
4883b3a8eb9SGleb Smirnoff 				if (rmask->addr32[0] != 0xffffffff)
4893b3a8eb9SGleb Smirnoff 					rpool->counter.addr32[0] =
4903b3a8eb9SGleb Smirnoff 					    htonl(arc4random());
4913b3a8eb9SGleb Smirnoff 				break;
4923b3a8eb9SGleb Smirnoff #endif /* INET6 */
4933b3a8eb9SGleb Smirnoff 			}
4943b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
4953b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
4963b3a8eb9SGleb Smirnoff 
4973b3a8eb9SGleb Smirnoff 		} else {
4983b3a8eb9SGleb Smirnoff 			PF_AINC(&rpool->counter, af);
4993b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
5003b3a8eb9SGleb Smirnoff 		}
5013b3a8eb9SGleb Smirnoff 		break;
5023b3a8eb9SGleb Smirnoff 	case PF_POOL_SRCHASH:
5033b3a8eb9SGleb Smirnoff 	    {
5043b3a8eb9SGleb Smirnoff 		unsigned char hash[16];
5053b3a8eb9SGleb Smirnoff 
5063b3a8eb9SGleb Smirnoff 		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
5073b3a8eb9SGleb Smirnoff 		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
5083b3a8eb9SGleb Smirnoff 		break;
5093b3a8eb9SGleb Smirnoff 	    }
5103b3a8eb9SGleb Smirnoff 	case PF_POOL_ROUNDROBIN:
5113b3a8eb9SGleb Smirnoff 	    {
512320c1116SKristof Provost 		struct pf_kpooladdr *acur = rpool->cur;
5133b3a8eb9SGleb Smirnoff 
5143b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
5153b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
5163b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af))
5173b3a8eb9SGleb Smirnoff 				goto get_addr;
5183b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
5193b3a8eb9SGleb Smirnoff 			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
5203b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af))
5213b3a8eb9SGleb Smirnoff 				goto get_addr;
5223b3a8eb9SGleb Smirnoff 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
5233b3a8eb9SGleb Smirnoff 			goto get_addr;
5243b3a8eb9SGleb Smirnoff 
5253b3a8eb9SGleb Smirnoff 	try_next:
5263b3a8eb9SGleb Smirnoff 		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
5273b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_FIRST(&rpool->list);
5283b3a8eb9SGleb Smirnoff 		else
5293b3a8eb9SGleb Smirnoff 			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
5303b3a8eb9SGleb Smirnoff 		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
5313b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
5323b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.tbl,
5333b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af)) {
5343b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
5353b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
5363b3a8eb9SGleb Smirnoff 					goto try_next;
53716303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
53816303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
5393b3a8eb9SGleb Smirnoff 			}
5403b3a8eb9SGleb Smirnoff 		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
5413b3a8eb9SGleb Smirnoff 			rpool->tblidx = -1;
5423b3a8eb9SGleb Smirnoff 			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
5433b3a8eb9SGleb Smirnoff 			    &rpool->tblidx, &rpool->counter, af)) {
5443b3a8eb9SGleb Smirnoff 				/* table contains no address of type 'af' */
5453b3a8eb9SGleb Smirnoff 				if (rpool->cur != acur)
5463b3a8eb9SGleb Smirnoff 					goto try_next;
54716303d2bSKajetan Staszkiewicz 				reason = PFRES_MAPFAILED;
54816303d2bSKajetan Staszkiewicz 				goto done_pool_mtx;
5493b3a8eb9SGleb Smirnoff 			}
5503b3a8eb9SGleb Smirnoff 		} else {
5513b3a8eb9SGleb Smirnoff 			raddr = &rpool->cur->addr.v.a.addr;
5523b3a8eb9SGleb Smirnoff 			rmask = &rpool->cur->addr.v.a.mask;
5533b3a8eb9SGleb Smirnoff 			PF_ACPY(&rpool->counter, raddr, af);
5543b3a8eb9SGleb Smirnoff 		}
5553b3a8eb9SGleb Smirnoff 
5563b3a8eb9SGleb Smirnoff 	get_addr:
5573b3a8eb9SGleb Smirnoff 		PF_ACPY(naddr, &rpool->counter, af);
5583b3a8eb9SGleb Smirnoff 		if (init_addr != NULL && PF_AZERO(init_addr, af))
5593b3a8eb9SGleb Smirnoff 			PF_ACPY(init_addr, naddr, af);
5603b3a8eb9SGleb Smirnoff 		PF_AINC(&rpool->counter, af);
5613b3a8eb9SGleb Smirnoff 		break;
5623b3a8eb9SGleb Smirnoff 	    }
5633b3a8eb9SGleb Smirnoff 	}
564d10de21fSKajetan Staszkiewicz 
565d10de21fSKajetan Staszkiewicz 	if (nkif)
566d10de21fSKajetan Staszkiewicz 		*nkif = rpool->cur->kif;
567d10de21fSKajetan Staszkiewicz 
568d10de21fSKajetan Staszkiewicz 	if (*sn != NULL) {
5693b3a8eb9SGleb Smirnoff 		PF_ACPY(&(*sn)->raddr, naddr, af);
570d10de21fSKajetan Staszkiewicz 		if (nkif)
571d10de21fSKajetan Staszkiewicz 			(*sn)->rkif = *nkif;
572d10de21fSKajetan Staszkiewicz 	}
5733b3a8eb9SGleb Smirnoff 
574498cca14SKristof Provost 	if (V_pf_status.debug >= PF_DEBUG_NOISY &&
5753b3a8eb9SGleb Smirnoff 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
5763b3a8eb9SGleb Smirnoff 		printf("pf_map_addr: selected address ");
5773b3a8eb9SGleb Smirnoff 		pf_print_host(naddr, 0, af);
578d10de21fSKajetan Staszkiewicz 		if (nkif)
579d10de21fSKajetan Staszkiewicz 			printf("@%s", (*nkif)->pfik_name);
5803b3a8eb9SGleb Smirnoff 		printf("\n");
5813b3a8eb9SGleb Smirnoff 	}
5823b3a8eb9SGleb Smirnoff 
58316303d2bSKajetan Staszkiewicz done_pool_mtx:
58416303d2bSKajetan Staszkiewicz 	mtx_unlock(&rpool->mtx);
58516303d2bSKajetan Staszkiewicz 
58616303d2bSKajetan Staszkiewicz done:
58716303d2bSKajetan Staszkiewicz 	if (reason) {
58816303d2bSKajetan Staszkiewicz 		counter_u64_add(V_pf_status.counters[reason], 1);
58916303d2bSKajetan Staszkiewicz 	}
59016303d2bSKajetan Staszkiewicz 
59116303d2bSKajetan Staszkiewicz 	return (reason);
5923b3a8eb9SGleb Smirnoff }
5933b3a8eb9SGleb Smirnoff 
594e86bddeaSKristof Provost struct pf_krule *
595f2064dd1SKajetan Staszkiewicz pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
596320c1116SKristof Provost     struct pfi_kkif *kif, struct pf_ksrc_node **sn,
5973b3a8eb9SGleb Smirnoff     struct pf_state_key **skp, struct pf_state_key **nkp,
5983b3a8eb9SGleb Smirnoff     struct pf_addr *saddr, struct pf_addr *daddr,
599e86bddeaSKristof Provost     uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack)
6003b3a8eb9SGleb Smirnoff {
601e86bddeaSKristof Provost 	struct pf_krule	*r = NULL;
6023b3a8eb9SGleb Smirnoff 	struct pf_addr	*naddr;
603*9897a669SMark Johnston 	uint16_t	*nportp;
6042aa21096SKurosawa Takahiro 	uint16_t	 low, high;
6053b3a8eb9SGleb Smirnoff 
6063b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
6073b3a8eb9SGleb Smirnoff 	KASSERT(*skp == NULL, ("*skp not NULL"));
6083b3a8eb9SGleb Smirnoff 	KASSERT(*nkp == NULL, ("*nkp not NULL"));
6093b3a8eb9SGleb Smirnoff 
610f2064dd1SKajetan Staszkiewicz 	if (pd->dir == PF_OUT) {
611f2064dd1SKajetan Staszkiewicz 		r = pf_match_translation(pd, m, off, kif, saddr,
6121d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
6133b3a8eb9SGleb Smirnoff 		if (r == NULL)
614f2064dd1SKajetan Staszkiewicz 			r = pf_match_translation(pd, m, off, kif,
6151d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_NAT,
6161d6139c0SGleb Smirnoff 			    anchor_stack);
6173b3a8eb9SGleb Smirnoff 	} else {
618f2064dd1SKajetan Staszkiewicz 		r = pf_match_translation(pd, m, off, kif, saddr,
6191d6139c0SGleb Smirnoff 		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
6203b3a8eb9SGleb Smirnoff 		if (r == NULL)
621f2064dd1SKajetan Staszkiewicz 			r = pf_match_translation(pd, m, off, kif,
6221d6139c0SGleb Smirnoff 			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
6231d6139c0SGleb Smirnoff 			    anchor_stack);
6243b3a8eb9SGleb Smirnoff 	}
6253b3a8eb9SGleb Smirnoff 
6263b3a8eb9SGleb Smirnoff 	if (r == NULL)
6273b3a8eb9SGleb Smirnoff 		return (NULL);
6283b3a8eb9SGleb Smirnoff 
6293b3a8eb9SGleb Smirnoff 	switch (r->action) {
6303b3a8eb9SGleb Smirnoff 	case PF_NONAT:
6313b3a8eb9SGleb Smirnoff 	case PF_NOBINAT:
6323b3a8eb9SGleb Smirnoff 	case PF_NORDR:
6333b3a8eb9SGleb Smirnoff 		return (NULL);
6343b3a8eb9SGleb Smirnoff 	}
6353b3a8eb9SGleb Smirnoff 
6363b3a8eb9SGleb Smirnoff 	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
6373b3a8eb9SGleb Smirnoff 	if (*skp == NULL)
6383b3a8eb9SGleb Smirnoff 		return (NULL);
6393b3a8eb9SGleb Smirnoff 	*nkp = pf_state_key_clone(*skp);
6403b3a8eb9SGleb Smirnoff 	if (*nkp == NULL) {
64198a9874fSKristof Provost 		uma_zfree(V_pf_state_key_z, *skp);
6423b3a8eb9SGleb Smirnoff 		*skp = NULL;
6433b3a8eb9SGleb Smirnoff 		return (NULL);
6443b3a8eb9SGleb Smirnoff 	}
6453b3a8eb9SGleb Smirnoff 
6463b3a8eb9SGleb Smirnoff 	naddr = &(*nkp)->addr[1];
647*9897a669SMark Johnston 	nportp = &(*nkp)->port[1];
6483b3a8eb9SGleb Smirnoff 
6493b3a8eb9SGleb Smirnoff 	switch (r->action) {
6503b3a8eb9SGleb Smirnoff 	case PF_NAT:
6512aa21096SKurosawa Takahiro 		if (pd->proto == IPPROTO_ICMP) {
6522aa21096SKurosawa Takahiro 			low = 1;
6532aa21096SKurosawa Takahiro 			high = 65535;
6542aa21096SKurosawa Takahiro 		} else {
6552aa21096SKurosawa Takahiro 			low  = r->rpool.proxy_port[0];
6562aa21096SKurosawa Takahiro 			high = r->rpool.proxy_port[1];
6572aa21096SKurosawa Takahiro 		}
6582aa21096SKurosawa Takahiro 		if (r->rpool.mape.offset > 0) {
6592aa21096SKurosawa Takahiro 			if (pf_get_mape_sport(pd->af, pd->proto, r, saddr,
660*9897a669SMark Johnston 			    sport, daddr, dport, naddr, nportp, sn)) {
6612aa21096SKurosawa Takahiro 				DPFPRINTF(PF_DEBUG_MISC,
6622aa21096SKurosawa Takahiro 				    ("pf: MAP-E port allocation (%u/%u/%u)"
6632aa21096SKurosawa Takahiro 				    " failed\n",
6642aa21096SKurosawa Takahiro 				    r->rpool.mape.offset,
6652aa21096SKurosawa Takahiro 				    r->rpool.mape.psidlen,
6662aa21096SKurosawa Takahiro 				    r->rpool.mape.psid));
6672aa21096SKurosawa Takahiro 				goto notrans;
6682aa21096SKurosawa Takahiro 			}
6692aa21096SKurosawa Takahiro 		} else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport,
670*9897a669SMark Johnston 		    daddr, dport, naddr, nportp, low, high, sn)) {
6713b3a8eb9SGleb Smirnoff 			DPFPRINTF(PF_DEBUG_MISC,
6723b3a8eb9SGleb Smirnoff 			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
6733b3a8eb9SGleb Smirnoff 			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
6743b3a8eb9SGleb Smirnoff 			goto notrans;
6753b3a8eb9SGleb Smirnoff 		}
6763b3a8eb9SGleb Smirnoff 		break;
6773b3a8eb9SGleb Smirnoff 	case PF_BINAT:
678f2064dd1SKajetan Staszkiewicz 		switch (pd->dir) {
6793b3a8eb9SGleb Smirnoff 		case PF_OUT:
6803b3a8eb9SGleb Smirnoff 			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
6813b3a8eb9SGleb Smirnoff 				switch (pd->af) {
6823b3a8eb9SGleb Smirnoff #ifdef INET
6833b3a8eb9SGleb Smirnoff 				case AF_INET:
6843b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
6853b3a8eb9SGleb Smirnoff 					    pfid_acnt4 < 1)
6863b3a8eb9SGleb Smirnoff 						goto notrans;
6873b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
6883b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6893b3a8eb9SGleb Smirnoff 					    pfid_addr4,
6903b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
6913b3a8eb9SGleb Smirnoff 					    pfid_mask4, saddr, AF_INET);
6923b3a8eb9SGleb Smirnoff 					break;
6933b3a8eb9SGleb Smirnoff #endif /* INET */
6943b3a8eb9SGleb Smirnoff #ifdef INET6
6953b3a8eb9SGleb Smirnoff 				case AF_INET6:
6963b3a8eb9SGleb Smirnoff 					if (r->rpool.cur->addr.p.dyn->
6973b3a8eb9SGleb Smirnoff 					    pfid_acnt6 < 1)
6983b3a8eb9SGleb Smirnoff 						goto notrans;
6993b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
7003b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
7013b3a8eb9SGleb Smirnoff 					    pfid_addr6,
7023b3a8eb9SGleb Smirnoff 					    &r->rpool.cur->addr.p.dyn->
7033b3a8eb9SGleb Smirnoff 					    pfid_mask6, saddr, AF_INET6);
7043b3a8eb9SGleb Smirnoff 					break;
7053b3a8eb9SGleb Smirnoff #endif /* INET6 */
7063b3a8eb9SGleb Smirnoff 				}
7073b3a8eb9SGleb Smirnoff 			} else
7083b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr,
7093b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.addr,
7103b3a8eb9SGleb Smirnoff 				    &r->rpool.cur->addr.v.a.mask, saddr,
7113b3a8eb9SGleb Smirnoff 				    pd->af);
7123b3a8eb9SGleb Smirnoff 			break;
7133b3a8eb9SGleb Smirnoff 		case PF_IN:
7143b3a8eb9SGleb Smirnoff 			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
7153b3a8eb9SGleb Smirnoff 				switch (pd->af) {
7163b3a8eb9SGleb Smirnoff #ifdef INET
7173b3a8eb9SGleb Smirnoff 				case AF_INET:
7183b3a8eb9SGleb Smirnoff 					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
7193b3a8eb9SGleb Smirnoff 						goto notrans;
7203b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
7213b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr4,
7223b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask4,
7233b3a8eb9SGleb Smirnoff 					    daddr, AF_INET);
7243b3a8eb9SGleb Smirnoff 					break;
7253b3a8eb9SGleb Smirnoff #endif /* INET */
7263b3a8eb9SGleb Smirnoff #ifdef INET6
7273b3a8eb9SGleb Smirnoff 				case AF_INET6:
7283b3a8eb9SGleb Smirnoff 					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
7293b3a8eb9SGleb Smirnoff 						goto notrans;
7303b3a8eb9SGleb Smirnoff 					PF_POOLMASK(naddr,
7313b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_addr6,
7323b3a8eb9SGleb Smirnoff 					    &r->src.addr.p.dyn->pfid_mask6,
7333b3a8eb9SGleb Smirnoff 					    daddr, AF_INET6);
7343b3a8eb9SGleb Smirnoff 					break;
7353b3a8eb9SGleb Smirnoff #endif /* INET6 */
7363b3a8eb9SGleb Smirnoff 				}
7373b3a8eb9SGleb Smirnoff 			} else
7383b3a8eb9SGleb Smirnoff 				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
7393b3a8eb9SGleb Smirnoff 				    &r->src.addr.v.a.mask, daddr, pd->af);
7403b3a8eb9SGleb Smirnoff 			break;
7413b3a8eb9SGleb Smirnoff 		}
7423b3a8eb9SGleb Smirnoff 		break;
7433b3a8eb9SGleb Smirnoff 	case PF_RDR: {
744*9897a669SMark Johnston 		struct pf_state_key_cmp key;
745*9897a669SMark Johnston 		uint16_t cut, low, high, nport;
746*9897a669SMark Johnston 
747d10de21fSKajetan Staszkiewicz 		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, NULL, sn))
7483b3a8eb9SGleb Smirnoff 			goto notrans;
7493b3a8eb9SGleb Smirnoff 		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
7503b3a8eb9SGleb Smirnoff 			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
7513b3a8eb9SGleb Smirnoff 			    daddr, pd->af);
7523b3a8eb9SGleb Smirnoff 
7536053adafSKristof Provost 		/* Do not change SCTP ports. */
7546053adafSKristof Provost 		if (pd->proto == IPPROTO_SCTP)
7556053adafSKristof Provost 			break;
7566053adafSKristof Provost 
7573b3a8eb9SGleb Smirnoff 		if (r->rpool.proxy_port[1]) {
7583b3a8eb9SGleb Smirnoff 			uint32_t	tmp_nport;
7593b3a8eb9SGleb Smirnoff 
7603b3a8eb9SGleb Smirnoff 			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
7613b3a8eb9SGleb Smirnoff 			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
7623b3a8eb9SGleb Smirnoff 			    1)) + r->rpool.proxy_port[0];
7633b3a8eb9SGleb Smirnoff 
7643b3a8eb9SGleb Smirnoff 			/* Wrap around if necessary. */
7653b3a8eb9SGleb Smirnoff 			if (tmp_nport > 65535)
7663b3a8eb9SGleb Smirnoff 				tmp_nport -= 65535;
767*9897a669SMark Johnston 			nport = htons((uint16_t)tmp_nport);
7683b3a8eb9SGleb Smirnoff 		} else if (r->rpool.proxy_port[0])
769*9897a669SMark Johnston 			nport = htons(r->rpool.proxy_port[0]);
770*9897a669SMark Johnston 		else
771*9897a669SMark Johnston 			nport = dport;
772*9897a669SMark Johnston 
773*9897a669SMark Johnston 		/*
774*9897a669SMark Johnston 		 * Update the destination port.
775*9897a669SMark Johnston 		 */
776*9897a669SMark Johnston 		*nportp = nport;
777*9897a669SMark Johnston 
778*9897a669SMark Johnston 		/*
779*9897a669SMark Johnston 		 * Do we have a source port conflict in the stack state?  Try to
780*9897a669SMark Johnston 		 * modulate the source port if so.  Note that this is racy since
781*9897a669SMark Johnston 		 * the state lookup may not find any matches here but will once
782*9897a669SMark Johnston 		 * pf_create_state() actually instantiates the state.
783*9897a669SMark Johnston 		 */
784*9897a669SMark Johnston 		bzero(&key, sizeof(key));
785*9897a669SMark Johnston 		key.af = pd->af;
786*9897a669SMark Johnston 		key.proto = pd->proto;
787*9897a669SMark Johnston 		key.port[0] = sport;
788*9897a669SMark Johnston 		PF_ACPY(&key.addr[0], saddr, key.af);
789*9897a669SMark Johnston 		key.port[1] = nport;
790*9897a669SMark Johnston 		PF_ACPY(&key.addr[1], naddr, key.af);
791*9897a669SMark Johnston 
792*9897a669SMark Johnston 		if (!pf_find_state_all_exists(&key, PF_OUT))
793*9897a669SMark Johnston 			break;
794*9897a669SMark Johnston 
795*9897a669SMark Johnston 		low = 50001;	/* XXX-MJ PF_NAT_PROXY_PORT_LOW/HIGH */
796*9897a669SMark Johnston 		high = 65535;
797*9897a669SMark Johnston 		cut = arc4random() % (1 + high - low) + low;
798*9897a669SMark Johnston 		for (uint32_t tmp = cut;
799*9897a669SMark Johnston 		    tmp <= high && tmp <= UINT16_MAX; tmp++) {
800*9897a669SMark Johnston 			key.port[0] = htons(tmp);
801*9897a669SMark Johnston 			if (!pf_find_state_all_exists(&key, PF_OUT)) {
802*9897a669SMark Johnston 				/* Update the source port. */
803*9897a669SMark Johnston 				(*nkp)->port[0] = htons(tmp);
804*9897a669SMark Johnston 				goto out;
805*9897a669SMark Johnston 			}
806*9897a669SMark Johnston 		}
807*9897a669SMark Johnston 		for (uint32_t tmp = cut - 1; tmp >= low; tmp--) {
808*9897a669SMark Johnston 			key.port[0] = htons(tmp);
809*9897a669SMark Johnston 			if (!pf_find_state_all_exists(&key, PF_OUT)) {
810*9897a669SMark Johnston 				/* Update the source port. */
811*9897a669SMark Johnston 				(*nkp)->port[0] = htons(tmp);
812*9897a669SMark Johnston 				goto out;
813*9897a669SMark Johnston 			}
814*9897a669SMark Johnston 		}
815*9897a669SMark Johnston 
816*9897a669SMark Johnston 		DPFPRINTF(PF_DEBUG_MISC,
817*9897a669SMark Johnston 		    ("pf: RDR source port allocation failed\n"));
818*9897a669SMark Johnston 		if (0) {
819*9897a669SMark Johnston out:
820*9897a669SMark Johnston 			DPFPRINTF(PF_DEBUG_MISC,
821*9897a669SMark Johnston 			    ("pf: RDR source port allocation %u->%u\n",
822*9897a669SMark Johnston 			    ntohs(sport), ntohs((*nkp)->port[0])));
823*9897a669SMark Johnston 		}
8243b3a8eb9SGleb Smirnoff 		break;
8253b3a8eb9SGleb Smirnoff 	}
8263b3a8eb9SGleb Smirnoff 	default:
8273b3a8eb9SGleb Smirnoff 		panic("%s: unknown action %u", __func__, r->action);
8283b3a8eb9SGleb Smirnoff 	}
8293b3a8eb9SGleb Smirnoff 
8303b3a8eb9SGleb Smirnoff 	/* Return success only if translation really happened. */
8313b3a8eb9SGleb Smirnoff 	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
8323b3a8eb9SGleb Smirnoff 		return (r);
8333b3a8eb9SGleb Smirnoff 
8343b3a8eb9SGleb Smirnoff notrans:
8353b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *nkp);
8363b3a8eb9SGleb Smirnoff 	uma_zfree(V_pf_state_key_z, *skp);
8373b3a8eb9SGleb Smirnoff 	*skp = *nkp = NULL;
838a830c452SGleb Smirnoff 	*sn = NULL;
8393b3a8eb9SGleb Smirnoff 
8403b3a8eb9SGleb Smirnoff 	return (NULL);
8413b3a8eb9SGleb Smirnoff }
842