pf_lb.c revision 261023
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: stable/10/sys/netpfil/pf/pf_lb.c 261023 2014-01-22 10:45:16Z glebius $");
39223637Sbz
40223637Sbz#include "opt_pf.h"
41240233Sglebius#include "opt_inet.h"
42240233Sglebius#include "opt_inet6.h"
43223637Sbz
44223637Sbz#include <sys/param.h>
45223637Sbz#include <sys/socket.h>
46223637Sbz#include <sys/sysctl.h>
47223637Sbz
48223637Sbz#include <net/if.h>
49223637Sbz#include <net/pfvar.h>
50223637Sbz#include <net/if_pflog.h>
51240233Sglebius#include <net/pf_mtag.h>
52223637Sbz
53223637Sbz#define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
54223637Sbz
55240233Sglebiusstatic void		 pf_hash(struct pf_addr *, struct pf_addr *,
56223637Sbz			    struct pf_poolhashkey *, sa_family_t);
57240233Sglebiusstatic struct pf_rule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
58223637Sbz			    int, int, struct pfi_kif *,
59223637Sbz			    struct pf_addr *, u_int16_t, struct pf_addr *,
60240641Sglebius			    uint16_t, int, struct pf_anchor_stackframe *);
61255143Sglebiusstatic int pf_get_sport(sa_family_t, uint8_t, struct pf_rule *,
62255143Sglebius    struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
63255143Sglebius    uint16_t *, uint16_t, uint16_t, struct pf_src_node **);
64223637Sbz
65223637Sbz#define mix(a,b,c) \
66223637Sbz	do {					\
67223637Sbz		a -= b; a -= c; a ^= (c >> 13);	\
68223637Sbz		b -= c; b -= a; b ^= (a << 8);	\
69223637Sbz		c -= a; c -= b; c ^= (b >> 13);	\
70223637Sbz		a -= b; a -= c; a ^= (c >> 12);	\
71223637Sbz		b -= c; b -= a; b ^= (a << 16);	\
72223637Sbz		c -= a; c -= b; c ^= (b >> 5);	\
73223637Sbz		a -= b; a -= c; a ^= (c >> 3);	\
74223637Sbz		b -= c; b -= a; b ^= (a << 10);	\
75223637Sbz		c -= a; c -= b; c ^= (b >> 15);	\
76223637Sbz	} while (0)
77223637Sbz
78223637Sbz/*
79223637Sbz * hash function based on bridge_hash in if_bridge.c
80223637Sbz */
81240233Sglebiusstatic void
82223637Sbzpf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
83223637Sbz    struct pf_poolhashkey *key, sa_family_t af)
84223637Sbz{
85223637Sbz	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
86223637Sbz
87223637Sbz	switch (af) {
88223637Sbz#ifdef INET
89223637Sbz	case AF_INET:
90223637Sbz		a += inaddr->addr32[0];
91223637Sbz		b += key->key32[1];
92223637Sbz		mix(a, b, c);
93223637Sbz		hash->addr32[0] = c + key->key32[2];
94223637Sbz		break;
95223637Sbz#endif /* INET */
96223637Sbz#ifdef INET6
97223637Sbz	case AF_INET6:
98223637Sbz		a += inaddr->addr32[0];
99223637Sbz		b += inaddr->addr32[2];
100223637Sbz		mix(a, b, c);
101223637Sbz		hash->addr32[0] = c;
102223637Sbz		a += inaddr->addr32[1];
103223637Sbz		b += inaddr->addr32[3];
104223637Sbz		c += key->key32[1];
105223637Sbz		mix(a, b, c);
106223637Sbz		hash->addr32[1] = c;
107223637Sbz		a += inaddr->addr32[2];
108223637Sbz		b += inaddr->addr32[1];
109223637Sbz		c += key->key32[2];
110223637Sbz		mix(a, b, c);
111223637Sbz		hash->addr32[2] = c;
112223637Sbz		a += inaddr->addr32[3];
113223637Sbz		b += inaddr->addr32[0];
114223637Sbz		c += key->key32[3];
115223637Sbz		mix(a, b, c);
116223637Sbz		hash->addr32[3] = c;
117223637Sbz		break;
118223637Sbz#endif /* INET6 */
119223637Sbz	}
120223637Sbz}
121223637Sbz
122240233Sglebiusstatic struct pf_rule *
123223637Sbzpf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
124223637Sbz    int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
125240641Sglebius    struct pf_addr *daddr, uint16_t dport, int rs_num,
126240641Sglebius    struct pf_anchor_stackframe *anchor_stack)
127223637Sbz{
128223637Sbz	struct pf_rule		*r, *rm = NULL;
129223637Sbz	struct pf_ruleset	*ruleset = NULL;
130223637Sbz	int			 tag = -1;
131223637Sbz	int			 rtableid = -1;
132223637Sbz	int			 asd = 0;
133223637Sbz
134223637Sbz	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
135223637Sbz	while (r && rm == NULL) {
136223637Sbz		struct pf_rule_addr	*src = NULL, *dst = NULL;
137223637Sbz		struct pf_addr_wrap	*xdst = NULL;
138223637Sbz
139223637Sbz		if (r->action == PF_BINAT && direction == PF_IN) {
140223637Sbz			src = &r->dst;
141223637Sbz			if (r->rpool.cur != NULL)
142223637Sbz				xdst = &r->rpool.cur->addr;
143223637Sbz		} else {
144223637Sbz			src = &r->src;
145223637Sbz			dst = &r->dst;
146223637Sbz		}
147223637Sbz
148223637Sbz		r->evaluations++;
149223637Sbz		if (pfi_kif_match(r->kif, kif) == r->ifnot)
150223637Sbz			r = r->skip[PF_SKIP_IFP].ptr;
151223637Sbz		else if (r->direction && r->direction != direction)
152223637Sbz			r = r->skip[PF_SKIP_DIR].ptr;
153223637Sbz		else if (r->af && r->af != pd->af)
154223637Sbz			r = r->skip[PF_SKIP_AF].ptr;
155223637Sbz		else if (r->proto && r->proto != pd->proto)
156223637Sbz			r = r->skip[PF_SKIP_PROTO].ptr;
157223637Sbz		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
158231852Sbz		    src->neg, kif, M_GETFIB(m)))
159223637Sbz			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
160223637Sbz			    PF_SKIP_DST_ADDR].ptr;
161223637Sbz		else if (src->port_op && !pf_match_port(src->port_op,
162223637Sbz		    src->port[0], src->port[1], sport))
163223637Sbz			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
164223637Sbz			    PF_SKIP_DST_PORT].ptr;
165223637Sbz		else if (dst != NULL &&
166231852Sbz		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
167231852Sbz		    M_GETFIB(m)))
168223637Sbz			r = r->skip[PF_SKIP_DST_ADDR].ptr;
169223637Sbz		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
170231852Sbz		    0, NULL, M_GETFIB(m)))
171223637Sbz			r = TAILQ_NEXT(r, entries);
172223637Sbz		else if (dst != NULL && dst->port_op &&
173223637Sbz		    !pf_match_port(dst->port_op, dst->port[0],
174223637Sbz		    dst->port[1], dport))
175223637Sbz			r = r->skip[PF_SKIP_DST_PORT].ptr;
176240233Sglebius		else if (r->match_tag && !pf_match_tag(m, r, &tag,
177240233Sglebius		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
178223637Sbz			r = TAILQ_NEXT(r, entries);
179223637Sbz		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
180223637Sbz		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
181223637Sbz		    off, pd->hdr.tcp), r->os_fingerprint)))
182223637Sbz			r = TAILQ_NEXT(r, entries);
183223637Sbz		else {
184223637Sbz			if (r->tag)
185223637Sbz				tag = r->tag;
186223637Sbz			if (r->rtableid >= 0)
187223637Sbz				rtableid = r->rtableid;
188223637Sbz			if (r->anchor == NULL) {
189223637Sbz				rm = r;
190223637Sbz			} else
191240641Sglebius				pf_step_into_anchor(anchor_stack, &asd,
192240641Sglebius				    &ruleset, rs_num, &r, NULL, NULL);
193223637Sbz		}
194223637Sbz		if (r == NULL)
195240641Sglebius			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
196240641Sglebius			    rs_num, &r, NULL, NULL);
197223637Sbz	}
198240233Sglebius
199240233Sglebius	if (tag > 0 && pf_tag_packet(m, pd, tag))
200223637Sbz		return (NULL);
201240233Sglebius	if (rtableid >= 0)
202240233Sglebius		M_SETFIB(m, rtableid);
203240233Sglebius
204223637Sbz	if (rm != NULL && (rm->action == PF_NONAT ||
205223637Sbz	    rm->action == PF_NORDR || rm->action == PF_NOBINAT))
206223637Sbz		return (NULL);
207223637Sbz	return (rm);
208223637Sbz}
209223637Sbz
210240233Sglebiusstatic int
211223637Sbzpf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
212255143Sglebius    struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
213255143Sglebius    uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
214255143Sglebius    uint16_t high, struct pf_src_node **sn)
215223637Sbz{
216223637Sbz	struct pf_state_key_cmp	key;
217223637Sbz	struct pf_addr		init_addr;
218255143Sglebius	uint16_t		cut;
219223637Sbz
220223637Sbz	bzero(&init_addr, sizeof(init_addr));
221223637Sbz	if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
222223637Sbz		return (1);
223223637Sbz
224223637Sbz	if (proto == IPPROTO_ICMP) {
225223637Sbz		low = 1;
226223637Sbz		high = 65535;
227223637Sbz	}
228223637Sbz
229255143Sglebius	bzero(&key, sizeof(key));
230255143Sglebius	key.af = af;
231255143Sglebius	key.proto = proto;
232255143Sglebius	key.port[0] = dport;
233255143Sglebius	PF_ACPY(&key.addr[0], daddr, key.af);
234255143Sglebius
235223637Sbz	do {
236255143Sglebius		PF_ACPY(&key.addr[1], naddr, key.af);
237223637Sbz
238223637Sbz		/*
239223637Sbz		 * port search; start random, step;
240223637Sbz		 * similar 2 portloop in in_pcbbind
241223637Sbz		 */
242223637Sbz		if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
243255143Sglebius		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
244255143Sglebius			/*
245255143Sglebius			 * XXX bug: icmp states don't use the id on both sides.
246255143Sglebius			 * (traceroute -I through nat)
247255143Sglebius			 */
248255143Sglebius			key.port[1] = sport;
249255143Sglebius			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
250255143Sglebius				*nport = sport;
251223637Sbz				return (0);
252255143Sglebius			}
253223637Sbz		} else if (low == high) {
254255143Sglebius			key.port[1] = htons(low);
255223637Sbz			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
256223637Sbz				*nport = htons(low);
257223637Sbz				return (0);
258223637Sbz			}
259223637Sbz		} else {
260255143Sglebius			uint16_t tmp;
261223637Sbz
262223637Sbz			if (low > high) {
263223637Sbz				tmp = low;
264223637Sbz				low = high;
265223637Sbz				high = tmp;
266223637Sbz			}
267223637Sbz			/* low < high */
268223637Sbz			cut = htonl(arc4random()) % (1 + high - low) + low;
269223637Sbz			/* low <= cut <= high */
270223637Sbz			for (tmp = cut; tmp <= high; ++(tmp)) {
271255143Sglebius				key.port[1] = htons(tmp);
272223637Sbz				if (pf_find_state_all(&key, PF_IN, NULL) ==
273223637Sbz				    NULL) {
274223637Sbz					*nport = htons(tmp);
275223637Sbz					return (0);
276223637Sbz				}
277223637Sbz			}
278223637Sbz			for (tmp = cut - 1; tmp >= low; --(tmp)) {
279255143Sglebius				key.port[1] = htons(tmp);
280223637Sbz				if (pf_find_state_all(&key, PF_IN, NULL) ==
281223637Sbz				    NULL) {
282223637Sbz					*nport = htons(tmp);
283223637Sbz					return (0);
284223637Sbz				}
285223637Sbz			}
286223637Sbz		}
287223637Sbz
288223637Sbz		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
289223637Sbz		case PF_POOL_RANDOM:
290223637Sbz		case PF_POOL_ROUNDROBIN:
291223637Sbz			if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
292223637Sbz				return (1);
293223637Sbz			break;
294223637Sbz		case PF_POOL_NONE:
295223637Sbz		case PF_POOL_SRCHASH:
296223637Sbz		case PF_POOL_BITMASK:
297223637Sbz		default:
298223637Sbz			return (1);
299223637Sbz		}
300223637Sbz	} while (! PF_AEQ(&init_addr, naddr, af) );
301223637Sbz	return (1);					/* none available */
302223637Sbz}
303223637Sbz
304223637Sbzint
305223637Sbzpf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
306223637Sbz    struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
307223637Sbz{
308223637Sbz	struct pf_pool		*rpool = &r->rpool;
309240233Sglebius	struct pf_addr		*raddr = NULL, *rmask = NULL;
310223637Sbz
311223637Sbz	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
312223637Sbz	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
313240233Sglebius		*sn = pf_find_src_node(saddr, r, af, 0);
314223637Sbz		if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
315223637Sbz			PF_ACPY(naddr, &(*sn)->raddr, af);
316223637Sbz			if (V_pf_status.debug >= PF_DEBUG_MISC) {
317223637Sbz				printf("pf_map_addr: src tracking maps ");
318240233Sglebius				pf_print_host(saddr, 0, af);
319223637Sbz				printf(" to ");
320223637Sbz				pf_print_host(naddr, 0, af);
321223637Sbz				printf("\n");
322223637Sbz			}
323223637Sbz			return (0);
324223637Sbz		}
325223637Sbz	}
326223637Sbz
327223637Sbz	if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
328223637Sbz		return (1);
329223637Sbz	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
330223637Sbz		switch (af) {
331223637Sbz#ifdef INET
332223637Sbz		case AF_INET:
333223637Sbz			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
334223637Sbz			    (rpool->opts & PF_POOL_TYPEMASK) !=
335223637Sbz			    PF_POOL_ROUNDROBIN)
336223637Sbz				return (1);
337223637Sbz			 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
338223637Sbz			 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
339223637Sbz			break;
340223637Sbz#endif /* INET */
341223637Sbz#ifdef INET6
342223637Sbz		case AF_INET6:
343223637Sbz			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
344223637Sbz			    (rpool->opts & PF_POOL_TYPEMASK) !=
345223637Sbz			    PF_POOL_ROUNDROBIN)
346223637Sbz				return (1);
347223637Sbz			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
348223637Sbz			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
349223637Sbz			break;
350223637Sbz#endif /* INET6 */
351223637Sbz		}
352223637Sbz	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
353223637Sbz		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
354223637Sbz			return (1); /* unsupported */
355223637Sbz	} else {
356223637Sbz		raddr = &rpool->cur->addr.v.a.addr;
357223637Sbz		rmask = &rpool->cur->addr.v.a.mask;
358223637Sbz	}
359223637Sbz
360223637Sbz	switch (rpool->opts & PF_POOL_TYPEMASK) {
361223637Sbz	case PF_POOL_NONE:
362223637Sbz		PF_ACPY(naddr, raddr, af);
363223637Sbz		break;
364223637Sbz	case PF_POOL_BITMASK:
365223637Sbz		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
366223637Sbz		break;
367223637Sbz	case PF_POOL_RANDOM:
368223637Sbz		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
369223637Sbz			switch (af) {
370223637Sbz#ifdef INET
371223637Sbz			case AF_INET:
372223637Sbz				rpool->counter.addr32[0] = htonl(arc4random());
373223637Sbz				break;
374223637Sbz#endif /* INET */
375223637Sbz#ifdef INET6
376223637Sbz			case AF_INET6:
377223637Sbz				if (rmask->addr32[3] != 0xffffffff)
378223637Sbz					rpool->counter.addr32[3] =
379223637Sbz					    htonl(arc4random());
380223637Sbz				else
381223637Sbz					break;
382223637Sbz				if (rmask->addr32[2] != 0xffffffff)
383223637Sbz					rpool->counter.addr32[2] =
384223637Sbz					    htonl(arc4random());
385223637Sbz				else
386223637Sbz					break;
387223637Sbz				if (rmask->addr32[1] != 0xffffffff)
388223637Sbz					rpool->counter.addr32[1] =
389223637Sbz					    htonl(arc4random());
390223637Sbz				else
391223637Sbz					break;
392223637Sbz				if (rmask->addr32[0] != 0xffffffff)
393223637Sbz					rpool->counter.addr32[0] =
394223637Sbz					    htonl(arc4random());
395223637Sbz				break;
396223637Sbz#endif /* INET6 */
397223637Sbz			}
398223637Sbz			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
399223637Sbz			PF_ACPY(init_addr, naddr, af);
400223637Sbz
401223637Sbz		} else {
402223637Sbz			PF_AINC(&rpool->counter, af);
403223637Sbz			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
404223637Sbz		}
405223637Sbz		break;
406223637Sbz	case PF_POOL_SRCHASH:
407240233Sglebius	    {
408240233Sglebius		unsigned char hash[16];
409240233Sglebius
410223637Sbz		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
411223637Sbz		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
412223637Sbz		break;
413240233Sglebius	    }
414223637Sbz	case PF_POOL_ROUNDROBIN:
415240233Sglebius	    {
416240233Sglebius		struct pf_pooladdr *acur = rpool->cur;
417240233Sglebius
418240233Sglebius		/*
419240233Sglebius		 * XXXGL: in the round-robin case we need to store
420240233Sglebius		 * the round-robin machine state in the rule, thus
421240233Sglebius		 * forwarding thread needs to modify rule.
422240233Sglebius		 *
423240233Sglebius		 * This is done w/o locking, because performance is assumed
424240233Sglebius		 * more important than round-robin precision.
425240233Sglebius		 *
426240233Sglebius		 * In the simpliest case we just update the "rpool->cur"
427240233Sglebius		 * pointer. However, if pool contains tables or dynamic
428240233Sglebius		 * addresses, then "tblidx" is also used to store machine
429240233Sglebius		 * state. Since "tblidx" is int, concurrent access to it can't
430240233Sglebius		 * lead to inconsistence, only to lost of precision.
431240233Sglebius		 *
432240233Sglebius		 * Things get worse, if table contains not hosts, but
433240233Sglebius		 * prefixes. In this case counter also stores machine state,
434240233Sglebius		 * and for IPv6 address, counter can't be updated atomically.
435240233Sglebius		 * Probably, using round-robin on a table containing IPv6
436240233Sglebius		 * prefixes (or even IPv4) would cause a panic.
437240233Sglebius		 */
438240233Sglebius
439223637Sbz		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
440223637Sbz			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
441240233Sglebius			    &rpool->tblidx, &rpool->counter, af))
442223637Sbz				goto get_addr;
443223637Sbz		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
444223637Sbz			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
445240233Sglebius			    &rpool->tblidx, &rpool->counter, af))
446223637Sbz				goto get_addr;
447223637Sbz		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
448223637Sbz			goto get_addr;
449223637Sbz
450223637Sbz	try_next:
451240233Sglebius		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
452223637Sbz			rpool->cur = TAILQ_FIRST(&rpool->list);
453240233Sglebius		else
454240233Sglebius			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
455223637Sbz		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
456223637Sbz			rpool->tblidx = -1;
457223637Sbz			if (pfr_pool_get(rpool->cur->addr.p.tbl,
458240233Sglebius			    &rpool->tblidx, &rpool->counter, af)) {
459223637Sbz				/* table contains no address of type 'af' */
460223637Sbz				if (rpool->cur != acur)
461223637Sbz					goto try_next;
462223637Sbz				return (1);
463223637Sbz			}
464223637Sbz		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
465223637Sbz			rpool->tblidx = -1;
466223637Sbz			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
467240233Sglebius			    &rpool->tblidx, &rpool->counter, af)) {
468223637Sbz				/* table contains no address of type 'af' */
469223637Sbz				if (rpool->cur != acur)
470223637Sbz					goto try_next;
471223637Sbz				return (1);
472223637Sbz			}
473223637Sbz		} else {
474223637Sbz			raddr = &rpool->cur->addr.v.a.addr;
475223637Sbz			rmask = &rpool->cur->addr.v.a.mask;
476223637Sbz			PF_ACPY(&rpool->counter, raddr, af);
477223637Sbz		}
478223637Sbz
479223637Sbz	get_addr:
480223637Sbz		PF_ACPY(naddr, &rpool->counter, af);
481223637Sbz		if (init_addr != NULL && PF_AZERO(init_addr, af))
482223637Sbz			PF_ACPY(init_addr, naddr, af);
483223637Sbz		PF_AINC(&rpool->counter, af);
484223637Sbz		break;
485240233Sglebius	    }
486223637Sbz	}
487223637Sbz	if (*sn != NULL)
488223637Sbz		PF_ACPY(&(*sn)->raddr, naddr, af);
489223637Sbz
490223637Sbz	if (V_pf_status.debug >= PF_DEBUG_MISC &&
491223637Sbz	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
492223637Sbz		printf("pf_map_addr: selected address ");
493223637Sbz		pf_print_host(naddr, 0, af);
494223637Sbz		printf("\n");
495223637Sbz	}
496223637Sbz
497223637Sbz	return (0);
498223637Sbz}
499223637Sbz
500223637Sbzstruct pf_rule *
501223637Sbzpf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
502223637Sbz    struct pfi_kif *kif, struct pf_src_node **sn,
503223637Sbz    struct pf_state_key **skp, struct pf_state_key **nkp,
504223637Sbz    struct pf_addr *saddr, struct pf_addr *daddr,
505240641Sglebius    uint16_t sport, uint16_t dport, struct pf_anchor_stackframe *anchor_stack)
506223637Sbz{
507223637Sbz	struct pf_rule	*r = NULL;
508240233Sglebius	struct pf_addr	*naddr;
509240233Sglebius	uint16_t	*nport;
510223637Sbz
511240233Sglebius	PF_RULES_RASSERT();
512240233Sglebius	KASSERT(*skp == NULL, ("*skp not NULL"));
513240233Sglebius	KASSERT(*nkp == NULL, ("*nkp not NULL"));
514223637Sbz
515223637Sbz	if (direction == PF_OUT) {
516223637Sbz		r = pf_match_translation(pd, m, off, direction, kif, saddr,
517240641Sglebius		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
518223637Sbz		if (r == NULL)
519223637Sbz			r = pf_match_translation(pd, m, off, direction, kif,
520240641Sglebius			    saddr, sport, daddr, dport, PF_RULESET_NAT,
521240641Sglebius			    anchor_stack);
522223637Sbz	} else {
523223637Sbz		r = pf_match_translation(pd, m, off, direction, kif, saddr,
524240641Sglebius		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
525223637Sbz		if (r == NULL)
526223637Sbz			r = pf_match_translation(pd, m, off, direction, kif,
527240641Sglebius			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
528240641Sglebius			    anchor_stack);
529223637Sbz	}
530223637Sbz
531240233Sglebius	if (r == NULL)
532240233Sglebius		return (NULL);
533223637Sbz
534240233Sglebius	switch (r->action) {
535240233Sglebius	case PF_NONAT:
536240233Sglebius	case PF_NOBINAT:
537240233Sglebius	case PF_NORDR:
538240233Sglebius		return (NULL);
539240233Sglebius	}
540223637Sbz
541240233Sglebius	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
542240233Sglebius	if (*skp == NULL)
543240233Sglebius		return (NULL);
544240233Sglebius	*nkp = pf_state_key_clone(*skp);
545240233Sglebius	if (*nkp == NULL) {
546240233Sglebius		uma_zfree(V_pf_state_key_z, skp);
547240233Sglebius		*skp = NULL;
548240233Sglebius		return (NULL);
549240233Sglebius	}
550223637Sbz
551240233Sglebius	/* XXX We only modify one side for now. */
552240233Sglebius	naddr = &(*nkp)->addr[1];
553240233Sglebius	nport = &(*nkp)->port[1];
554240233Sglebius
555240233Sglebius	switch (r->action) {
556240233Sglebius	case PF_NAT:
557255143Sglebius		if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, daddr,
558255143Sglebius		    dport, naddr, nport, r->rpool.proxy_port[0],
559240233Sglebius		    r->rpool.proxy_port[1], sn)) {
560240233Sglebius			DPFPRINTF(PF_DEBUG_MISC,
561240233Sglebius			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
562240233Sglebius			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
563240233Sglebius			goto notrans;
564240233Sglebius		}
565240233Sglebius		break;
566240233Sglebius	case PF_BINAT:
567240233Sglebius		switch (direction) {
568240233Sglebius		case PF_OUT:
569240233Sglebius			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
570240233Sglebius				switch (pd->af) {
571223637Sbz#ifdef INET
572240233Sglebius				case AF_INET:
573240233Sglebius					if (r->rpool.cur->addr.p.dyn->
574240233Sglebius					    pfid_acnt4 < 1)
575240233Sglebius						goto notrans;
576240233Sglebius					PF_POOLMASK(naddr,
577240233Sglebius					    &r->rpool.cur->addr.p.dyn->
578240233Sglebius					    pfid_addr4,
579240233Sglebius					    &r->rpool.cur->addr.p.dyn->
580240233Sglebius					    pfid_mask4, saddr, AF_INET);
581240233Sglebius					break;
582223637Sbz#endif /* INET */
583223637Sbz#ifdef INET6
584240233Sglebius				case AF_INET6:
585240233Sglebius					if (r->rpool.cur->addr.p.dyn->
586240233Sglebius					    pfid_acnt6 < 1)
587240233Sglebius						goto notrans;
588240233Sglebius					PF_POOLMASK(naddr,
589240233Sglebius					    &r->rpool.cur->addr.p.dyn->
590240233Sglebius					    pfid_addr6,
591240233Sglebius					    &r->rpool.cur->addr.p.dyn->
592240233Sglebius					    pfid_mask6, saddr, AF_INET6);
593240233Sglebius					break;
594223637Sbz#endif /* INET6 */
595240233Sglebius				}
596240233Sglebius			} else
597240233Sglebius				PF_POOLMASK(naddr,
598240233Sglebius				    &r->rpool.cur->addr.v.a.addr,
599240233Sglebius				    &r->rpool.cur->addr.v.a.mask, saddr,
600240233Sglebius				    pd->af);
601240233Sglebius			break;
602240233Sglebius		case PF_IN:
603240233Sglebius			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
604240233Sglebius				switch (pd->af) {
605240233Sglebius#ifdef INET
606240233Sglebius				case AF_INET:
607240233Sglebius					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
608240233Sglebius						goto notrans;
609223637Sbz					PF_POOLMASK(naddr,
610240233Sglebius					    &r->src.addr.p.dyn->pfid_addr4,
611240233Sglebius					    &r->src.addr.p.dyn->pfid_mask4,
612240233Sglebius					    daddr, AF_INET);
613240233Sglebius					break;
614223637Sbz#endif /* INET */
615223637Sbz#ifdef INET6
616240233Sglebius				case AF_INET6:
617240233Sglebius					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
618240233Sglebius						goto notrans;
619240233Sglebius					PF_POOLMASK(naddr,
620240233Sglebius					    &r->src.addr.p.dyn->pfid_addr6,
621240233Sglebius					    &r->src.addr.p.dyn->pfid_mask6,
622240233Sglebius					    daddr, AF_INET6);
623240233Sglebius					break;
624223637Sbz#endif /* INET6 */
625240233Sglebius				}
626240233Sglebius			} else
627240233Sglebius				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
628240233Sglebius				    &r->src.addr.v.a.mask, daddr, pd->af);
629223637Sbz			break;
630240233Sglebius		}
631240233Sglebius		break;
632240233Sglebius	case PF_RDR: {
633240233Sglebius		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
634240233Sglebius			goto notrans;
635240233Sglebius		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
636240233Sglebius			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
637240233Sglebius			    daddr, pd->af);
638223637Sbz
639240233Sglebius		if (r->rpool.proxy_port[1]) {
640240233Sglebius			uint32_t	tmp_nport;
641223637Sbz
642240233Sglebius			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
643240233Sglebius			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
644240233Sglebius			    1)) + r->rpool.proxy_port[0];
645223637Sbz
646240233Sglebius			/* Wrap around if necessary. */
647240233Sglebius			if (tmp_nport > 65535)
648240233Sglebius				tmp_nport -= 65535;
649240233Sglebius			*nport = htons((uint16_t)tmp_nport);
650240233Sglebius		} else if (r->rpool.proxy_port[0])
651240233Sglebius			*nport = htons(r->rpool.proxy_port[0]);
652240233Sglebius		break;
653223637Sbz	}
654240233Sglebius	default:
655240233Sglebius		panic("%s: unknown action %u", __func__, r->action);
656240233Sglebius	}
657223637Sbz
658240233Sglebius	/* Return success only if translation really happened. */
659240233Sglebius	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
660240233Sglebius		return (r);
661240233Sglebius
662240233Sglebiusnotrans:
663240233Sglebius	uma_zfree(V_pf_state_key_z, *nkp);
664240233Sglebius	uma_zfree(V_pf_state_key_z, *skp);
665240233Sglebius	*skp = *nkp = NULL;
666261023Sglebius	*sn = NULL;
667240233Sglebius
668240233Sglebius	return (NULL);
669223637Sbz}
670