1244769Sglebius/*-
2223637Sbz * Copyright (c) 2001 Daniel Hartmeier
3223637Sbz * Copyright (c) 2002 - 2008 Henning Brauer
4223637Sbz * All rights reserved.
5223637Sbz *
6223637Sbz * Redistribution and use in source and binary forms, with or without
7223637Sbz * modification, are permitted provided that the following conditions
8223637Sbz * are met:
9223637Sbz *
10223637Sbz *    - Redistributions of source code must retain the above copyright
11223637Sbz *      notice, this list of conditions and the following disclaimer.
12223637Sbz *    - Redistributions in binary form must reproduce the above
13223637Sbz *      copyright notice, this list of conditions and the following
14223637Sbz *      disclaimer in the documentation and/or other materials provided
15223637Sbz *      with the distribution.
16223637Sbz *
17223637Sbz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18223637Sbz * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19223637Sbz * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20223637Sbz * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21223637Sbz * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22223637Sbz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23223637Sbz * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24223637Sbz * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25223637Sbz * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26223637Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27223637Sbz * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28223637Sbz * POSSIBILITY OF SUCH DAMAGE.
29223637Sbz *
30223637Sbz * Effort sponsored in part by the Defense Advanced Research Projects
31223637Sbz * Agency (DARPA) and Air Force Research Laboratory, Air Force
32223637Sbz * Materiel Command, USAF, under agreement number F30602-01-2-0537.
33223637Sbz *
34244769Sglebius *	$OpenBSD: pf_lb.c,v 1.2 2009/02/12 02:13:15 sthen Exp $
35223637Sbz */
36223637Sbz
37223637Sbz#include <sys/cdefs.h>
38223637Sbz__FBSDID("$FreeBSD: releng/11.0/sys/netpfil/pf/pf_lb.c 270023 2014-08-15 14:16:08Z glebius $");
39223637Sbz
40223637Sbz#include "opt_pf.h"
41240233Sglebius#include "opt_inet.h"
42240233Sglebius#include "opt_inet6.h"
43223637Sbz
44223637Sbz#include <sys/param.h>
45257176Sglebius#include <sys/lock.h>
46257176Sglebius#include <sys/mbuf.h>
47257176Sglebius#include <sys/rwlock.h>
48223637Sbz#include <sys/socket.h>
49223637Sbz#include <sys/sysctl.h>
50223637Sbz
51223637Sbz#include <net/if.h>
52257176Sglebius#include <net/vnet.h>
53223637Sbz#include <net/pfvar.h>
54223637Sbz#include <net/if_pflog.h>
55223637Sbz
56223637Sbz#define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
57223637Sbz
58240233Sglebiusstatic void		 pf_hash(struct pf_addr *, struct pf_addr *,
59223637Sbz			    struct pf_poolhashkey *, sa_family_t);
60240233Sglebiusstatic struct pf_rule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
61223637Sbz			    int, int, struct pfi_kif *,
62223637Sbz			    struct pf_addr *, u_int16_t, struct pf_addr *,
63240641Sglebius			    uint16_t, int, struct pf_anchor_stackframe *);
64255143Sglebiusstatic int pf_get_sport(sa_family_t, uint8_t, struct pf_rule *,
65255143Sglebius    struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
66255143Sglebius    uint16_t *, uint16_t, uint16_t, struct pf_src_node **);
67223637Sbz
68223637Sbz#define mix(a,b,c) \
69223637Sbz	do {					\
70223637Sbz		a -= b; a -= c; a ^= (c >> 13);	\
71223637Sbz		b -= c; b -= a; b ^= (a << 8);	\
72223637Sbz		c -= a; c -= b; c ^= (b >> 13);	\
73223637Sbz		a -= b; a -= c; a ^= (c >> 12);	\
74223637Sbz		b -= c; b -= a; b ^= (a << 16);	\
75223637Sbz		c -= a; c -= b; c ^= (b >> 5);	\
76223637Sbz		a -= b; a -= c; a ^= (c >> 3);	\
77223637Sbz		b -= c; b -= a; b ^= (a << 10);	\
78223637Sbz		c -= a; c -= b; c ^= (b >> 15);	\
79223637Sbz	} while (0)
80223637Sbz
81223637Sbz/*
82223637Sbz * hash function based on bridge_hash in if_bridge.c
83223637Sbz */
84240233Sglebiusstatic void
85223637Sbzpf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
86223637Sbz    struct pf_poolhashkey *key, sa_family_t af)
87223637Sbz{
88223637Sbz	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
89223637Sbz
90223637Sbz	switch (af) {
91223637Sbz#ifdef INET
92223637Sbz	case AF_INET:
93223637Sbz		a += inaddr->addr32[0];
94223637Sbz		b += key->key32[1];
95223637Sbz		mix(a, b, c);
96223637Sbz		hash->addr32[0] = c + key->key32[2];
97223637Sbz		break;
98223637Sbz#endif /* INET */
99223637Sbz#ifdef INET6
100223637Sbz	case AF_INET6:
101223637Sbz		a += inaddr->addr32[0];
102223637Sbz		b += inaddr->addr32[2];
103223637Sbz		mix(a, b, c);
104223637Sbz		hash->addr32[0] = c;
105223637Sbz		a += inaddr->addr32[1];
106223637Sbz		b += inaddr->addr32[3];
107223637Sbz		c += key->key32[1];
108223637Sbz		mix(a, b, c);
109223637Sbz		hash->addr32[1] = c;
110223637Sbz		a += inaddr->addr32[2];
111223637Sbz		b += inaddr->addr32[1];
112223637Sbz		c += key->key32[2];
113223637Sbz		mix(a, b, c);
114223637Sbz		hash->addr32[2] = c;
115223637Sbz		a += inaddr->addr32[3];
116223637Sbz		b += inaddr->addr32[0];
117223637Sbz		c += key->key32[3];
118223637Sbz		mix(a, b, c);
119223637Sbz		hash->addr32[3] = c;
120223637Sbz		break;
121223637Sbz#endif /* INET6 */
122223637Sbz	}
123223637Sbz}
124223637Sbz
125240233Sglebiusstatic struct pf_rule *
126223637Sbzpf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
127223637Sbz    int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
128240641Sglebius    struct pf_addr *daddr, uint16_t dport, int rs_num,
129240641Sglebius    struct pf_anchor_stackframe *anchor_stack)
130223637Sbz{
131223637Sbz	struct pf_rule		*r, *rm = NULL;
132223637Sbz	struct pf_ruleset	*ruleset = NULL;
133223637Sbz	int			 tag = -1;
134223637Sbz	int			 rtableid = -1;
135223637Sbz	int			 asd = 0;
136223637Sbz
137223637Sbz	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
138223637Sbz	while (r && rm == NULL) {
139223637Sbz		struct pf_rule_addr	*src = NULL, *dst = NULL;
140223637Sbz		struct pf_addr_wrap	*xdst = NULL;
141223637Sbz
142223637Sbz		if (r->action == PF_BINAT && direction == PF_IN) {
143223637Sbz			src = &r->dst;
144223637Sbz			if (r->rpool.cur != NULL)
145223637Sbz				xdst = &r->rpool.cur->addr;
146223637Sbz		} else {
147223637Sbz			src = &r->src;
148223637Sbz			dst = &r->dst;
149223637Sbz		}
150223637Sbz
151223637Sbz		r->evaluations++;
152223637Sbz		if (pfi_kif_match(r->kif, kif) == r->ifnot)
153223637Sbz			r = r->skip[PF_SKIP_IFP].ptr;
154223637Sbz		else if (r->direction && r->direction != direction)
155223637Sbz			r = r->skip[PF_SKIP_DIR].ptr;
156223637Sbz		else if (r->af && r->af != pd->af)
157223637Sbz			r = r->skip[PF_SKIP_AF].ptr;
158223637Sbz		else if (r->proto && r->proto != pd->proto)
159223637Sbz			r = r->skip[PF_SKIP_PROTO].ptr;
160223637Sbz		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
161231852Sbz		    src->neg, kif, M_GETFIB(m)))
162223637Sbz			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
163223637Sbz			    PF_SKIP_DST_ADDR].ptr;
164223637Sbz		else if (src->port_op && !pf_match_port(src->port_op,
165223637Sbz		    src->port[0], src->port[1], sport))
166223637Sbz			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
167223637Sbz			    PF_SKIP_DST_PORT].ptr;
168223637Sbz		else if (dst != NULL &&
169231852Sbz		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
170231852Sbz		    M_GETFIB(m)))
171223637Sbz			r = r->skip[PF_SKIP_DST_ADDR].ptr;
172223637Sbz		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
173231852Sbz		    0, NULL, M_GETFIB(m)))
174223637Sbz			r = TAILQ_NEXT(r, entries);
175223637Sbz		else if (dst != NULL && dst->port_op &&
176223637Sbz		    !pf_match_port(dst->port_op, dst->port[0],
177223637Sbz		    dst->port[1], dport))
178223637Sbz			r = r->skip[PF_SKIP_DST_PORT].ptr;
179240233Sglebius		else if (r->match_tag && !pf_match_tag(m, r, &tag,
180240233Sglebius		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
181223637Sbz			r = TAILQ_NEXT(r, entries);
182223637Sbz		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
183223637Sbz		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
184223637Sbz		    off, pd->hdr.tcp), r->os_fingerprint)))
185223637Sbz			r = TAILQ_NEXT(r, entries);
186223637Sbz		else {
187223637Sbz			if (r->tag)
188223637Sbz				tag = r->tag;
189223637Sbz			if (r->rtableid >= 0)
190223637Sbz				rtableid = r->rtableid;
191223637Sbz			if (r->anchor == NULL) {
192223637Sbz				rm = r;
193223637Sbz			} else
194240641Sglebius				pf_step_into_anchor(anchor_stack, &asd,
195240641Sglebius				    &ruleset, rs_num, &r, NULL, NULL);
196223637Sbz		}
197223637Sbz		if (r == NULL)
198240641Sglebius			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
199240641Sglebius			    rs_num, &r, NULL, NULL);
200223637Sbz	}
201240233Sglebius
202240233Sglebius	if (tag > 0 && pf_tag_packet(m, pd, tag))
203223637Sbz		return (NULL);
204240233Sglebius	if (rtableid >= 0)
205240233Sglebius		M_SETFIB(m, rtableid);
206240233Sglebius
207223637Sbz	if (rm != NULL && (rm->action == PF_NONAT ||
208223637Sbz	    rm->action == PF_NORDR || rm->action == PF_NOBINAT))
209223637Sbz		return (NULL);
210223637Sbz	return (rm);
211223637Sbz}
212223637Sbz
213240233Sglebiusstatic int
214223637Sbzpf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
215255143Sglebius    struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
216255143Sglebius    uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
217255143Sglebius    uint16_t high, struct pf_src_node **sn)
218223637Sbz{
219223637Sbz	struct pf_state_key_cmp	key;
220223637Sbz	struct pf_addr		init_addr;
221223637Sbz
222223637Sbz	bzero(&init_addr, sizeof(init_addr));
223223637Sbz	if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
224223637Sbz		return (1);
225223637Sbz
226264521Sglebius	if (proto == IPPROTO_ICMP) {
227223637Sbz		low = 1;
228223637Sbz		high = 65535;
229223637Sbz	}
230223637Sbz
231255143Sglebius	bzero(&key, sizeof(key));
232255143Sglebius	key.af = af;
233255143Sglebius	key.proto = proto;
234255143Sglebius	key.port[0] = dport;
235255143Sglebius	PF_ACPY(&key.addr[0], daddr, key.af);
236255143Sglebius
237223637Sbz	do {
238255143Sglebius		PF_ACPY(&key.addr[1], naddr, key.af);
239223637Sbz
240223637Sbz		/*
241223637Sbz		 * port search; start random, step;
242223637Sbz		 * similar 2 portloop in in_pcbbind
243223637Sbz		 */
244223637Sbz		if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
245255143Sglebius		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
246255143Sglebius			/*
247255143Sglebius			 * XXX bug: icmp states don't use the id on both sides.
248255143Sglebius			 * (traceroute -I through nat)
249255143Sglebius			 */
250255143Sglebius			key.port[1] = sport;
251255143Sglebius			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
252255143Sglebius				*nport = sport;
253223637Sbz				return (0);
254255143Sglebius			}
255223637Sbz		} else if (low == high) {
256255143Sglebius			key.port[1] = htons(low);
257223637Sbz			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
258223637Sbz				*nport = htons(low);
259223637Sbz				return (0);
260223637Sbz			}
261223637Sbz		} else {
262258133Sglebius			uint16_t tmp, cut;
263223637Sbz
264223637Sbz			if (low > high) {
265223637Sbz				tmp = low;
266223637Sbz				low = high;
267223637Sbz				high = tmp;
268223637Sbz			}
269223637Sbz			/* low < high */
270258133Sglebius			cut = arc4random() % (1 + high - low) + low;
271223637Sbz			/* low <= cut <= high */
272223637Sbz			for (tmp = cut; tmp <= high; ++(tmp)) {
273255143Sglebius				key.port[1] = htons(tmp);
274223637Sbz				if (pf_find_state_all(&key, PF_IN, NULL) ==
275223637Sbz				    NULL) {
276223637Sbz					*nport = htons(tmp);
277223637Sbz					return (0);
278223637Sbz				}
279223637Sbz			}
280223637Sbz			for (tmp = cut - 1; tmp >= low; --(tmp)) {
281255143Sglebius				key.port[1] = htons(tmp);
282223637Sbz				if (pf_find_state_all(&key, PF_IN, NULL) ==
283223637Sbz				    NULL) {
284223637Sbz					*nport = htons(tmp);
285223637Sbz					return (0);
286223637Sbz				}
287223637Sbz			}
288223637Sbz		}
289223637Sbz
290223637Sbz		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
291223637Sbz		case PF_POOL_RANDOM:
292223637Sbz		case PF_POOL_ROUNDROBIN:
293223637Sbz			if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
294223637Sbz				return (1);
295223637Sbz			break;
296223637Sbz		case PF_POOL_NONE:
297223637Sbz		case PF_POOL_SRCHASH:
298223637Sbz		case PF_POOL_BITMASK:
299223637Sbz		default:
300223637Sbz			return (1);
301223637Sbz		}
302223637Sbz	} while (! PF_AEQ(&init_addr, naddr, af) );
303223637Sbz	return (1);					/* none available */
304223637Sbz}
305223637Sbz
306223637Sbzint
307223637Sbzpf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
308223637Sbz    struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
309223637Sbz{
310223637Sbz	struct pf_pool		*rpool = &r->rpool;
311240233Sglebius	struct pf_addr		*raddr = NULL, *rmask = NULL;
312223637Sbz
313270023Sglebius	/* Try to find a src_node if none was given and this
314270023Sglebius	   is a sticky-address rule. */
315223637Sbz	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
316270023Sglebius	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
317240233Sglebius		*sn = pf_find_src_node(saddr, r, af, 0);
318270023Sglebius
319270023Sglebius	/* If a src_node was found or explicitly given and it has a non-zero
320270023Sglebius	   route address, use this address. A zeroed address is found if the
321270023Sglebius	   src node was created just a moment ago in pf_create_state and it
322270023Sglebius	   needs to be filled in with routing decision calculated here. */
323270023Sglebius	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
324270023Sglebius		PF_ACPY(naddr, &(*sn)->raddr, af);
325270023Sglebius		if (V_pf_status.debug >= PF_DEBUG_MISC) {
326270023Sglebius			printf("pf_map_addr: src tracking maps ");
327270023Sglebius			pf_print_host(saddr, 0, af);
328270023Sglebius			printf(" to ");
329270023Sglebius			pf_print_host(naddr, 0, af);
330270023Sglebius			printf("\n");
331223637Sbz		}
332270023Sglebius		return (0);
333223637Sbz	}
334223637Sbz
335270023Sglebius	/* Find the route using chosen algorithm. Store the found route
336270023Sglebius	   in src_node if it was given or found. */
337223637Sbz	if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
338223637Sbz		return (1);
339223637Sbz	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
340223637Sbz		switch (af) {
341223637Sbz#ifdef INET
342223637Sbz		case AF_INET:
343223637Sbz			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
344223637Sbz			    (rpool->opts & PF_POOL_TYPEMASK) !=
345223637Sbz			    PF_POOL_ROUNDROBIN)
346223637Sbz				return (1);
347223637Sbz			 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
348223637Sbz			 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
349223637Sbz			break;
350223637Sbz#endif /* INET */
351223637Sbz#ifdef INET6
352223637Sbz		case AF_INET6:
353223637Sbz			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
354223637Sbz			    (rpool->opts & PF_POOL_TYPEMASK) !=
355223637Sbz			    PF_POOL_ROUNDROBIN)
356223637Sbz				return (1);
357223637Sbz			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
358223637Sbz			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
359223637Sbz			break;
360223637Sbz#endif /* INET6 */
361223637Sbz		}
362223637Sbz	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
363223637Sbz		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
364223637Sbz			return (1); /* unsupported */
365223637Sbz	} else {
366223637Sbz		raddr = &rpool->cur->addr.v.a.addr;
367223637Sbz		rmask = &rpool->cur->addr.v.a.mask;
368223637Sbz	}
369223637Sbz
370223637Sbz	switch (rpool->opts & PF_POOL_TYPEMASK) {
371223637Sbz	case PF_POOL_NONE:
372223637Sbz		PF_ACPY(naddr, raddr, af);
373223637Sbz		break;
374223637Sbz	case PF_POOL_BITMASK:
375223637Sbz		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
376223637Sbz		break;
377223637Sbz	case PF_POOL_RANDOM:
378223637Sbz		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
379223637Sbz			switch (af) {
380223637Sbz#ifdef INET
381223637Sbz			case AF_INET:
382223637Sbz				rpool->counter.addr32[0] = htonl(arc4random());
383223637Sbz				break;
384223637Sbz#endif /* INET */
385223637Sbz#ifdef INET6
386223637Sbz			case AF_INET6:
387223637Sbz				if (rmask->addr32[3] != 0xffffffff)
388223637Sbz					rpool->counter.addr32[3] =
389223637Sbz					    htonl(arc4random());
390223637Sbz				else
391223637Sbz					break;
392223637Sbz				if (rmask->addr32[2] != 0xffffffff)
393223637Sbz					rpool->counter.addr32[2] =
394223637Sbz					    htonl(arc4random());
395223637Sbz				else
396223637Sbz					break;
397223637Sbz				if (rmask->addr32[1] != 0xffffffff)
398223637Sbz					rpool->counter.addr32[1] =
399223637Sbz					    htonl(arc4random());
400223637Sbz				else
401223637Sbz					break;
402223637Sbz				if (rmask->addr32[0] != 0xffffffff)
403223637Sbz					rpool->counter.addr32[0] =
404223637Sbz					    htonl(arc4random());
405223637Sbz				break;
406223637Sbz#endif /* INET6 */
407223637Sbz			}
408223637Sbz			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
409223637Sbz			PF_ACPY(init_addr, naddr, af);
410223637Sbz
411223637Sbz		} else {
412223637Sbz			PF_AINC(&rpool->counter, af);
413223637Sbz			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
414223637Sbz		}
415223637Sbz		break;
416223637Sbz	case PF_POOL_SRCHASH:
417240233Sglebius	    {
418240233Sglebius		unsigned char hash[16];
419240233Sglebius
420223637Sbz		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
421223637Sbz		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
422223637Sbz		break;
423240233Sglebius	    }
424223637Sbz	case PF_POOL_ROUNDROBIN:
425240233Sglebius	    {
426240233Sglebius		struct pf_pooladdr *acur = rpool->cur;
427240233Sglebius
428240233Sglebius		/*
429240233Sglebius		 * XXXGL: in the round-robin case we need to store
430240233Sglebius		 * the round-robin machine state in the rule, thus
431240233Sglebius		 * forwarding thread needs to modify rule.
432240233Sglebius		 *
433240233Sglebius		 * This is done w/o locking, because performance is assumed
434240233Sglebius		 * more important than round-robin precision.
435240233Sglebius		 *
436240233Sglebius		 * In the simpliest case we just update the "rpool->cur"
437240233Sglebius		 * pointer. However, if pool contains tables or dynamic
438240233Sglebius		 * addresses, then "tblidx" is also used to store machine
439240233Sglebius		 * state. Since "tblidx" is int, concurrent access to it can't
440240233Sglebius		 * lead to inconsistence, only to lost of precision.
441240233Sglebius		 *
442240233Sglebius		 * Things get worse, if table contains not hosts, but
443240233Sglebius		 * prefixes. In this case counter also stores machine state,
444240233Sglebius		 * and for IPv6 address, counter can't be updated atomically.
445240233Sglebius		 * Probably, using round-robin on a table containing IPv6
446240233Sglebius		 * prefixes (or even IPv4) would cause a panic.
447240233Sglebius		 */
448240233Sglebius
449223637Sbz		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
450223637Sbz			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
451240233Sglebius			    &rpool->tblidx, &rpool->counter, af))
452223637Sbz				goto get_addr;
453223637Sbz		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
454223637Sbz			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
455240233Sglebius			    &rpool->tblidx, &rpool->counter, af))
456223637Sbz				goto get_addr;
457223637Sbz		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
458223637Sbz			goto get_addr;
459223637Sbz
460223637Sbz	try_next:
461240233Sglebius		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
462223637Sbz			rpool->cur = TAILQ_FIRST(&rpool->list);
463240233Sglebius		else
464240233Sglebius			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
465223637Sbz		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
466223637Sbz			rpool->tblidx = -1;
467223637Sbz			if (pfr_pool_get(rpool->cur->addr.p.tbl,
468240233Sglebius			    &rpool->tblidx, &rpool->counter, af)) {
469223637Sbz				/* table contains no address of type 'af' */
470223637Sbz				if (rpool->cur != acur)
471223637Sbz					goto try_next;
472223637Sbz				return (1);
473223637Sbz			}
474223637Sbz		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
475223637Sbz			rpool->tblidx = -1;
476223637Sbz			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
477240233Sglebius			    &rpool->tblidx, &rpool->counter, af)) {
478223637Sbz				/* table contains no address of type 'af' */
479223637Sbz				if (rpool->cur != acur)
480223637Sbz					goto try_next;
481223637Sbz				return (1);
482223637Sbz			}
483223637Sbz		} else {
484223637Sbz			raddr = &rpool->cur->addr.v.a.addr;
485223637Sbz			rmask = &rpool->cur->addr.v.a.mask;
486223637Sbz			PF_ACPY(&rpool->counter, raddr, af);
487223637Sbz		}
488223637Sbz
489223637Sbz	get_addr:
490223637Sbz		PF_ACPY(naddr, &rpool->counter, af);
491223637Sbz		if (init_addr != NULL && PF_AZERO(init_addr, af))
492223637Sbz			PF_ACPY(init_addr, naddr, af);
493223637Sbz		PF_AINC(&rpool->counter, af);
494223637Sbz		break;
495240233Sglebius	    }
496223637Sbz	}
497223637Sbz	if (*sn != NULL)
498223637Sbz		PF_ACPY(&(*sn)->raddr, naddr, af);
499223637Sbz
500223637Sbz	if (V_pf_status.debug >= PF_DEBUG_MISC &&
501223637Sbz	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
502223637Sbz		printf("pf_map_addr: selected address ");
503223637Sbz		pf_print_host(naddr, 0, af);
504223637Sbz		printf("\n");
505223637Sbz	}
506223637Sbz
507223637Sbz	return (0);
508223637Sbz}
509223637Sbz
510223637Sbzstruct pf_rule *
511223637Sbzpf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
512223637Sbz    struct pfi_kif *kif, struct pf_src_node **sn,
513223637Sbz    struct pf_state_key **skp, struct pf_state_key **nkp,
514223637Sbz    struct pf_addr *saddr, struct pf_addr *daddr,
515240641Sglebius    uint16_t sport, uint16_t dport, struct pf_anchor_stackframe *anchor_stack)
516223637Sbz{
517223637Sbz	struct pf_rule	*r = NULL;
518240233Sglebius	struct pf_addr	*naddr;
519240233Sglebius	uint16_t	*nport;
520223637Sbz
521240233Sglebius	PF_RULES_RASSERT();
522240233Sglebius	KASSERT(*skp == NULL, ("*skp not NULL"));
523240233Sglebius	KASSERT(*nkp == NULL, ("*nkp not NULL"));
524223637Sbz
525223637Sbz	if (direction == PF_OUT) {
526223637Sbz		r = pf_match_translation(pd, m, off, direction, kif, saddr,
527240641Sglebius		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
528223637Sbz		if (r == NULL)
529223637Sbz			r = pf_match_translation(pd, m, off, direction, kif,
530240641Sglebius			    saddr, sport, daddr, dport, PF_RULESET_NAT,
531240641Sglebius			    anchor_stack);
532223637Sbz	} else {
533223637Sbz		r = pf_match_translation(pd, m, off, direction, kif, saddr,
534240641Sglebius		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
535223637Sbz		if (r == NULL)
536223637Sbz			r = pf_match_translation(pd, m, off, direction, kif,
537240641Sglebius			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
538240641Sglebius			    anchor_stack);
539223637Sbz	}
540223637Sbz
541240233Sglebius	if (r == NULL)
542240233Sglebius		return (NULL);
543223637Sbz
544240233Sglebius	switch (r->action) {
545240233Sglebius	case PF_NONAT:
546240233Sglebius	case PF_NOBINAT:
547240233Sglebius	case PF_NORDR:
548240233Sglebius		return (NULL);
549240233Sglebius	}
550223637Sbz
551240233Sglebius	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
552240233Sglebius	if (*skp == NULL)
553240233Sglebius		return (NULL);
554240233Sglebius	*nkp = pf_state_key_clone(*skp);
555240233Sglebius	if (*nkp == NULL) {
556240233Sglebius		uma_zfree(V_pf_state_key_z, skp);
557240233Sglebius		*skp = NULL;
558240233Sglebius		return (NULL);
559240233Sglebius	}
560223637Sbz
561240233Sglebius	/* XXX We only modify one side for now. */
562240233Sglebius	naddr = &(*nkp)->addr[1];
563240233Sglebius	nport = &(*nkp)->port[1];
564240233Sglebius
565240233Sglebius	switch (r->action) {
566240233Sglebius	case PF_NAT:
567255143Sglebius		if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, daddr,
568255143Sglebius		    dport, naddr, nport, r->rpool.proxy_port[0],
569240233Sglebius		    r->rpool.proxy_port[1], sn)) {
570240233Sglebius			DPFPRINTF(PF_DEBUG_MISC,
571240233Sglebius			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
572240233Sglebius			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
573240233Sglebius			goto notrans;
574240233Sglebius		}
575240233Sglebius		break;
576240233Sglebius	case PF_BINAT:
577240233Sglebius		switch (direction) {
578240233Sglebius		case PF_OUT:
579240233Sglebius			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
580240233Sglebius				switch (pd->af) {
581223637Sbz#ifdef INET
582240233Sglebius				case AF_INET:
583240233Sglebius					if (r->rpool.cur->addr.p.dyn->
584240233Sglebius					    pfid_acnt4 < 1)
585240233Sglebius						goto notrans;
586240233Sglebius					PF_POOLMASK(naddr,
587240233Sglebius					    &r->rpool.cur->addr.p.dyn->
588240233Sglebius					    pfid_addr4,
589240233Sglebius					    &r->rpool.cur->addr.p.dyn->
590240233Sglebius					    pfid_mask4, saddr, AF_INET);
591240233Sglebius					break;
592223637Sbz#endif /* INET */
593223637Sbz#ifdef INET6
594240233Sglebius				case AF_INET6:
595240233Sglebius					if (r->rpool.cur->addr.p.dyn->
596240233Sglebius					    pfid_acnt6 < 1)
597240233Sglebius						goto notrans;
598240233Sglebius					PF_POOLMASK(naddr,
599240233Sglebius					    &r->rpool.cur->addr.p.dyn->
600240233Sglebius					    pfid_addr6,
601240233Sglebius					    &r->rpool.cur->addr.p.dyn->
602240233Sglebius					    pfid_mask6, saddr, AF_INET6);
603240233Sglebius					break;
604223637Sbz#endif /* INET6 */
605240233Sglebius				}
606240233Sglebius			} else
607240233Sglebius				PF_POOLMASK(naddr,
608240233Sglebius				    &r->rpool.cur->addr.v.a.addr,
609240233Sglebius				    &r->rpool.cur->addr.v.a.mask, saddr,
610240233Sglebius				    pd->af);
611240233Sglebius			break;
612240233Sglebius		case PF_IN:
613240233Sglebius			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
614240233Sglebius				switch (pd->af) {
615240233Sglebius#ifdef INET
616240233Sglebius				case AF_INET:
617240233Sglebius					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
618240233Sglebius						goto notrans;
619223637Sbz					PF_POOLMASK(naddr,
620240233Sglebius					    &r->src.addr.p.dyn->pfid_addr4,
621240233Sglebius					    &r->src.addr.p.dyn->pfid_mask4,
622240233Sglebius					    daddr, AF_INET);
623240233Sglebius					break;
624223637Sbz#endif /* INET */
625223637Sbz#ifdef INET6
626240233Sglebius				case AF_INET6:
627240233Sglebius					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
628240233Sglebius						goto notrans;
629240233Sglebius					PF_POOLMASK(naddr,
630240233Sglebius					    &r->src.addr.p.dyn->pfid_addr6,
631240233Sglebius					    &r->src.addr.p.dyn->pfid_mask6,
632240233Sglebius					    daddr, AF_INET6);
633240233Sglebius					break;
634223637Sbz#endif /* INET6 */
635240233Sglebius				}
636240233Sglebius			} else
637240233Sglebius				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
638240233Sglebius				    &r->src.addr.v.a.mask, daddr, pd->af);
639223637Sbz			break;
640240233Sglebius		}
641240233Sglebius		break;
642240233Sglebius	case PF_RDR: {
643240233Sglebius		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
644240233Sglebius			goto notrans;
645240233Sglebius		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
646240233Sglebius			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
647240233Sglebius			    daddr, pd->af);
648223637Sbz
649240233Sglebius		if (r->rpool.proxy_port[1]) {
650240233Sglebius			uint32_t	tmp_nport;
651223637Sbz
652240233Sglebius			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
653240233Sglebius			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
654240233Sglebius			    1)) + r->rpool.proxy_port[0];
655223637Sbz
656240233Sglebius			/* Wrap around if necessary. */
657240233Sglebius			if (tmp_nport > 65535)
658240233Sglebius				tmp_nport -= 65535;
659240233Sglebius			*nport = htons((uint16_t)tmp_nport);
660240233Sglebius		} else if (r->rpool.proxy_port[0])
661240233Sglebius			*nport = htons(r->rpool.proxy_port[0]);
662240233Sglebius		break;
663223637Sbz	}
664240233Sglebius	default:
665240233Sglebius		panic("%s: unknown action %u", __func__, r->action);
666240233Sglebius	}
667223637Sbz
668240233Sglebius	/* Return success only if translation really happened. */
669240233Sglebius	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
670240233Sglebius		return (r);
671240233Sglebius
672240233Sglebiusnotrans:
673240233Sglebius	uma_zfree(V_pf_state_key_z, *nkp);
674240233Sglebius	uma_zfree(V_pf_state_key_z, *skp);
675240233Sglebius	*skp = *nkp = NULL;
676260377Sglebius	*sn = NULL;
677240233Sglebius
678240233Sglebius	return (NULL);
679223637Sbz}
680