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$");
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>
51223637Sbz
52223637Sbz#define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
53223637Sbz
54240233Sglebiusstatic void		 pf_hash(struct pf_addr *, struct pf_addr *,
55223637Sbz			    struct pf_poolhashkey *, sa_family_t);
56240233Sglebiusstatic struct pf_rule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
57223637Sbz			    int, int, struct pfi_kif *,
58223637Sbz			    struct pf_addr *, u_int16_t, struct pf_addr *,
59240641Sglebius			    uint16_t, int, struct pf_anchor_stackframe *);
60255143Sglebiusstatic int pf_get_sport(sa_family_t, uint8_t, struct pf_rule *,
61255143Sglebius    struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
62255143Sglebius    uint16_t *, uint16_t, uint16_t, struct pf_src_node **);
63223637Sbz
64223637Sbz#define mix(a,b,c) \
65223637Sbz	do {					\
66223637Sbz		a -= b; a -= c; a ^= (c >> 13);	\
67223637Sbz		b -= c; b -= a; b ^= (a << 8);	\
68223637Sbz		c -= a; c -= b; c ^= (b >> 13);	\
69223637Sbz		a -= b; a -= c; a ^= (c >> 12);	\
70223637Sbz		b -= c; b -= a; b ^= (a << 16);	\
71223637Sbz		c -= a; c -= b; c ^= (b >> 5);	\
72223637Sbz		a -= b; a -= c; a ^= (c >> 3);	\
73223637Sbz		b -= c; b -= a; b ^= (a << 10);	\
74223637Sbz		c -= a; c -= b; c ^= (b >> 15);	\
75223637Sbz	} while (0)
76223637Sbz
77223637Sbz/*
78223637Sbz * hash function based on bridge_hash in if_bridge.c
79223637Sbz */
80240233Sglebiusstatic void
81223637Sbzpf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
82223637Sbz    struct pf_poolhashkey *key, sa_family_t af)
83223637Sbz{
84223637Sbz	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
85223637Sbz
86223637Sbz	switch (af) {
87223637Sbz#ifdef INET
88223637Sbz	case AF_INET:
89223637Sbz		a += inaddr->addr32[0];
90223637Sbz		b += key->key32[1];
91223637Sbz		mix(a, b, c);
92223637Sbz		hash->addr32[0] = c + key->key32[2];
93223637Sbz		break;
94223637Sbz#endif /* INET */
95223637Sbz#ifdef INET6
96223637Sbz	case AF_INET6:
97223637Sbz		a += inaddr->addr32[0];
98223637Sbz		b += inaddr->addr32[2];
99223637Sbz		mix(a, b, c);
100223637Sbz		hash->addr32[0] = c;
101223637Sbz		a += inaddr->addr32[1];
102223637Sbz		b += inaddr->addr32[3];
103223637Sbz		c += key->key32[1];
104223637Sbz		mix(a, b, c);
105223637Sbz		hash->addr32[1] = c;
106223637Sbz		a += inaddr->addr32[2];
107223637Sbz		b += inaddr->addr32[1];
108223637Sbz		c += key->key32[2];
109223637Sbz		mix(a, b, c);
110223637Sbz		hash->addr32[2] = c;
111223637Sbz		a += inaddr->addr32[3];
112223637Sbz		b += inaddr->addr32[0];
113223637Sbz		c += key->key32[3];
114223637Sbz		mix(a, b, c);
115223637Sbz		hash->addr32[3] = c;
116223637Sbz		break;
117223637Sbz#endif /* INET6 */
118223637Sbz	}
119223637Sbz}
120223637Sbz
121240233Sglebiusstatic struct pf_rule *
122223637Sbzpf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
123223637Sbz    int direction, struct pfi_kif *kif, struct pf_addr *saddr, u_int16_t sport,
124240641Sglebius    struct pf_addr *daddr, uint16_t dport, int rs_num,
125240641Sglebius    struct pf_anchor_stackframe *anchor_stack)
126223637Sbz{
127223637Sbz	struct pf_rule		*r, *rm = NULL;
128223637Sbz	struct pf_ruleset	*ruleset = NULL;
129223637Sbz	int			 tag = -1;
130223637Sbz	int			 rtableid = -1;
131223637Sbz	int			 asd = 0;
132223637Sbz
133223637Sbz	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
134223637Sbz	while (r && rm == NULL) {
135223637Sbz		struct pf_rule_addr	*src = NULL, *dst = NULL;
136223637Sbz		struct pf_addr_wrap	*xdst = NULL;
137223637Sbz
138223637Sbz		if (r->action == PF_BINAT && direction == PF_IN) {
139223637Sbz			src = &r->dst;
140223637Sbz			if (r->rpool.cur != NULL)
141223637Sbz				xdst = &r->rpool.cur->addr;
142223637Sbz		} else {
143223637Sbz			src = &r->src;
144223637Sbz			dst = &r->dst;
145223637Sbz		}
146223637Sbz
147223637Sbz		r->evaluations++;
148223637Sbz		if (pfi_kif_match(r->kif, kif) == r->ifnot)
149223637Sbz			r = r->skip[PF_SKIP_IFP].ptr;
150223637Sbz		else if (r->direction && r->direction != direction)
151223637Sbz			r = r->skip[PF_SKIP_DIR].ptr;
152223637Sbz		else if (r->af && r->af != pd->af)
153223637Sbz			r = r->skip[PF_SKIP_AF].ptr;
154223637Sbz		else if (r->proto && r->proto != pd->proto)
155223637Sbz			r = r->skip[PF_SKIP_PROTO].ptr;
156223637Sbz		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
157231852Sbz		    src->neg, kif, M_GETFIB(m)))
158223637Sbz			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
159223637Sbz			    PF_SKIP_DST_ADDR].ptr;
160223637Sbz		else if (src->port_op && !pf_match_port(src->port_op,
161223637Sbz		    src->port[0], src->port[1], sport))
162223637Sbz			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
163223637Sbz			    PF_SKIP_DST_PORT].ptr;
164223637Sbz		else if (dst != NULL &&
165231852Sbz		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
166231852Sbz		    M_GETFIB(m)))
167223637Sbz			r = r->skip[PF_SKIP_DST_ADDR].ptr;
168223637Sbz		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
169231852Sbz		    0, NULL, M_GETFIB(m)))
170223637Sbz			r = TAILQ_NEXT(r, entries);
171223637Sbz		else if (dst != NULL && dst->port_op &&
172223637Sbz		    !pf_match_port(dst->port_op, dst->port[0],
173223637Sbz		    dst->port[1], dport))
174223637Sbz			r = r->skip[PF_SKIP_DST_PORT].ptr;
175240233Sglebius		else if (r->match_tag && !pf_match_tag(m, r, &tag,
176240233Sglebius		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
177223637Sbz			r = TAILQ_NEXT(r, entries);
178223637Sbz		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
179223637Sbz		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
180223637Sbz		    off, pd->hdr.tcp), r->os_fingerprint)))
181223637Sbz			r = TAILQ_NEXT(r, entries);
182223637Sbz		else {
183223637Sbz			if (r->tag)
184223637Sbz				tag = r->tag;
185223637Sbz			if (r->rtableid >= 0)
186223637Sbz				rtableid = r->rtableid;
187223637Sbz			if (r->anchor == NULL) {
188223637Sbz				rm = r;
189223637Sbz			} else
190240641Sglebius				pf_step_into_anchor(anchor_stack, &asd,
191240641Sglebius				    &ruleset, rs_num, &r, NULL, NULL);
192223637Sbz		}
193223637Sbz		if (r == NULL)
194240641Sglebius			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
195240641Sglebius			    rs_num, &r, NULL, NULL);
196223637Sbz	}
197240233Sglebius
198240233Sglebius	if (tag > 0 && pf_tag_packet(m, pd, tag))
199223637Sbz		return (NULL);
200240233Sglebius	if (rtableid >= 0)
201240233Sglebius		M_SETFIB(m, rtableid);
202240233Sglebius
203223637Sbz	if (rm != NULL && (rm->action == PF_NONAT ||
204223637Sbz	    rm->action == PF_NORDR || rm->action == PF_NOBINAT))
205223637Sbz		return (NULL);
206223637Sbz	return (rm);
207223637Sbz}
208223637Sbz
209240233Sglebiusstatic int
210223637Sbzpf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r,
211255143Sglebius    struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
212255143Sglebius    uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
213255143Sglebius    uint16_t high, struct pf_src_node **sn)
214223637Sbz{
215223637Sbz	struct pf_state_key_cmp	key;
216223637Sbz	struct pf_addr		init_addr;
217255143Sglebius	uint16_t		cut;
218223637Sbz
219223637Sbz	bzero(&init_addr, sizeof(init_addr));
220223637Sbz	if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
221223637Sbz		return (1);
222223637Sbz
223223637Sbz	if (proto == IPPROTO_ICMP) {
224223637Sbz		low = 1;
225223637Sbz		high = 65535;
226223637Sbz	}
227223637Sbz
228255143Sglebius	bzero(&key, sizeof(key));
229255143Sglebius	key.af = af;
230255143Sglebius	key.proto = proto;
231255143Sglebius	key.port[0] = dport;
232255143Sglebius	PF_ACPY(&key.addr[0], daddr, key.af);
233255143Sglebius
234223637Sbz	do {
235255143Sglebius		PF_ACPY(&key.addr[1], naddr, key.af);
236223637Sbz
237223637Sbz		/*
238223637Sbz		 * port search; start random, step;
239223637Sbz		 * similar 2 portloop in in_pcbbind
240223637Sbz		 */
241223637Sbz		if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
242255143Sglebius		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
243255143Sglebius			/*
244255143Sglebius			 * XXX bug: icmp states don't use the id on both sides.
245255143Sglebius			 * (traceroute -I through nat)
246255143Sglebius			 */
247255143Sglebius			key.port[1] = sport;
248255143Sglebius			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
249255143Sglebius				*nport = sport;
250223637Sbz				return (0);
251255143Sglebius			}
252223637Sbz		} else if (low == high) {
253255143Sglebius			key.port[1] = htons(low);
254223637Sbz			if (pf_find_state_all(&key, PF_IN, NULL) == NULL) {
255223637Sbz				*nport = htons(low);
256223637Sbz				return (0);
257223637Sbz			}
258223637Sbz		} else {
259255143Sglebius			uint16_t tmp;
260223637Sbz
261223637Sbz			if (low > high) {
262223637Sbz				tmp = low;
263223637Sbz				low = high;
264223637Sbz				high = tmp;
265223637Sbz			}
266223637Sbz			/* low < high */
267223637Sbz			cut = htonl(arc4random()) % (1 + high - low) + low;
268223637Sbz			/* low <= cut <= high */
269223637Sbz			for (tmp = cut; tmp <= high; ++(tmp)) {
270255143Sglebius				key.port[1] = htons(tmp);
271223637Sbz				if (pf_find_state_all(&key, PF_IN, NULL) ==
272223637Sbz				    NULL) {
273223637Sbz					*nport = htons(tmp);
274223637Sbz					return (0);
275223637Sbz				}
276223637Sbz			}
277223637Sbz			for (tmp = cut - 1; tmp >= low; --(tmp)) {
278255143Sglebius				key.port[1] = htons(tmp);
279223637Sbz				if (pf_find_state_all(&key, PF_IN, NULL) ==
280223637Sbz				    NULL) {
281223637Sbz					*nport = htons(tmp);
282223637Sbz					return (0);
283223637Sbz				}
284223637Sbz			}
285223637Sbz		}
286223637Sbz
287223637Sbz		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
288223637Sbz		case PF_POOL_RANDOM:
289223637Sbz		case PF_POOL_ROUNDROBIN:
290223637Sbz			if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
291223637Sbz				return (1);
292223637Sbz			break;
293223637Sbz		case PF_POOL_NONE:
294223637Sbz		case PF_POOL_SRCHASH:
295223637Sbz		case PF_POOL_BITMASK:
296223637Sbz		default:
297223637Sbz			return (1);
298223637Sbz		}
299223637Sbz	} while (! PF_AEQ(&init_addr, naddr, af) );
300223637Sbz	return (1);					/* none available */
301223637Sbz}
302223637Sbz
303223637Sbzint
304223637Sbzpf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
305223637Sbz    struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn)
306223637Sbz{
307223637Sbz	struct pf_pool		*rpool = &r->rpool;
308240233Sglebius	struct pf_addr		*raddr = NULL, *rmask = NULL;
309223637Sbz
310270577Sglebius	/* Try to find a src_node if none was given and this
311270577Sglebius	   is a sticky-address rule. */
312223637Sbz	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
313270577Sglebius	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
314240233Sglebius		*sn = pf_find_src_node(saddr, r, af, 0);
315270577Sglebius
316270577Sglebius	/* If a src_node was found or explicitly given and it has a non-zero
317270577Sglebius	   route address, use this address. A zeroed address is found if the
318270577Sglebius	   src node was created just a moment ago in pf_create_state and it
319270577Sglebius	   needs to be filled in with routing decision calculated here. */
320270577Sglebius	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
321270577Sglebius		PF_ACPY(naddr, &(*sn)->raddr, af);
322270577Sglebius		if (V_pf_status.debug >= PF_DEBUG_MISC) {
323270577Sglebius			printf("pf_map_addr: src tracking maps ");
324270577Sglebius			pf_print_host(saddr, 0, af);
325270577Sglebius			printf(" to ");
326270577Sglebius			pf_print_host(naddr, 0, af);
327270577Sglebius			printf("\n");
328223637Sbz		}
329270577Sglebius		return (0);
330223637Sbz	}
331223637Sbz
332270577Sglebius	/* Find the route using chosen algorithm. Store the found route
333270577Sglebius	   in src_node if it was given or found. */
334223637Sbz	if (rpool->cur->addr.type == PF_ADDR_NOROUTE)
335223637Sbz		return (1);
336223637Sbz	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
337223637Sbz		switch (af) {
338223637Sbz#ifdef INET
339223637Sbz		case AF_INET:
340223637Sbz			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
341223637Sbz			    (rpool->opts & PF_POOL_TYPEMASK) !=
342223637Sbz			    PF_POOL_ROUNDROBIN)
343223637Sbz				return (1);
344223637Sbz			 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
345223637Sbz			 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
346223637Sbz			break;
347223637Sbz#endif /* INET */
348223637Sbz#ifdef INET6
349223637Sbz		case AF_INET6:
350223637Sbz			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
351223637Sbz			    (rpool->opts & PF_POOL_TYPEMASK) !=
352223637Sbz			    PF_POOL_ROUNDROBIN)
353223637Sbz				return (1);
354223637Sbz			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
355223637Sbz			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
356223637Sbz			break;
357223637Sbz#endif /* INET6 */
358223637Sbz		}
359223637Sbz	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
360223637Sbz		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN)
361223637Sbz			return (1); /* unsupported */
362223637Sbz	} else {
363223637Sbz		raddr = &rpool->cur->addr.v.a.addr;
364223637Sbz		rmask = &rpool->cur->addr.v.a.mask;
365223637Sbz	}
366223637Sbz
367223637Sbz	switch (rpool->opts & PF_POOL_TYPEMASK) {
368223637Sbz	case PF_POOL_NONE:
369223637Sbz		PF_ACPY(naddr, raddr, af);
370223637Sbz		break;
371223637Sbz	case PF_POOL_BITMASK:
372223637Sbz		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
373223637Sbz		break;
374223637Sbz	case PF_POOL_RANDOM:
375223637Sbz		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
376223637Sbz			switch (af) {
377223637Sbz#ifdef INET
378223637Sbz			case AF_INET:
379223637Sbz				rpool->counter.addr32[0] = htonl(arc4random());
380223637Sbz				break;
381223637Sbz#endif /* INET */
382223637Sbz#ifdef INET6
383223637Sbz			case AF_INET6:
384223637Sbz				if (rmask->addr32[3] != 0xffffffff)
385223637Sbz					rpool->counter.addr32[3] =
386223637Sbz					    htonl(arc4random());
387223637Sbz				else
388223637Sbz					break;
389223637Sbz				if (rmask->addr32[2] != 0xffffffff)
390223637Sbz					rpool->counter.addr32[2] =
391223637Sbz					    htonl(arc4random());
392223637Sbz				else
393223637Sbz					break;
394223637Sbz				if (rmask->addr32[1] != 0xffffffff)
395223637Sbz					rpool->counter.addr32[1] =
396223637Sbz					    htonl(arc4random());
397223637Sbz				else
398223637Sbz					break;
399223637Sbz				if (rmask->addr32[0] != 0xffffffff)
400223637Sbz					rpool->counter.addr32[0] =
401223637Sbz					    htonl(arc4random());
402223637Sbz				break;
403223637Sbz#endif /* INET6 */
404223637Sbz			}
405223637Sbz			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
406223637Sbz			PF_ACPY(init_addr, naddr, af);
407223637Sbz
408223637Sbz		} else {
409223637Sbz			PF_AINC(&rpool->counter, af);
410223637Sbz			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
411223637Sbz		}
412223637Sbz		break;
413223637Sbz	case PF_POOL_SRCHASH:
414240233Sglebius	    {
415240233Sglebius		unsigned char hash[16];
416240233Sglebius
417223637Sbz		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
418223637Sbz		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
419223637Sbz		break;
420240233Sglebius	    }
421223637Sbz	case PF_POOL_ROUNDROBIN:
422240233Sglebius	    {
423240233Sglebius		struct pf_pooladdr *acur = rpool->cur;
424240233Sglebius
425240233Sglebius		/*
426240233Sglebius		 * XXXGL: in the round-robin case we need to store
427240233Sglebius		 * the round-robin machine state in the rule, thus
428240233Sglebius		 * forwarding thread needs to modify rule.
429240233Sglebius		 *
430240233Sglebius		 * This is done w/o locking, because performance is assumed
431240233Sglebius		 * more important than round-robin precision.
432240233Sglebius		 *
433240233Sglebius		 * In the simpliest case we just update the "rpool->cur"
434240233Sglebius		 * pointer. However, if pool contains tables or dynamic
435240233Sglebius		 * addresses, then "tblidx" is also used to store machine
436240233Sglebius		 * state. Since "tblidx" is int, concurrent access to it can't
437240233Sglebius		 * lead to inconsistence, only to lost of precision.
438240233Sglebius		 *
439240233Sglebius		 * Things get worse, if table contains not hosts, but
440240233Sglebius		 * prefixes. In this case counter also stores machine state,
441240233Sglebius		 * and for IPv6 address, counter can't be updated atomically.
442240233Sglebius		 * Probably, using round-robin on a table containing IPv6
443240233Sglebius		 * prefixes (or even IPv4) would cause a panic.
444240233Sglebius		 */
445240233Sglebius
446223637Sbz		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
447223637Sbz			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
448240233Sglebius			    &rpool->tblidx, &rpool->counter, af))
449223637Sbz				goto get_addr;
450223637Sbz		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
451223637Sbz			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
452240233Sglebius			    &rpool->tblidx, &rpool->counter, af))
453223637Sbz				goto get_addr;
454223637Sbz		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
455223637Sbz			goto get_addr;
456223637Sbz
457223637Sbz	try_next:
458240233Sglebius		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
459223637Sbz			rpool->cur = TAILQ_FIRST(&rpool->list);
460240233Sglebius		else
461240233Sglebius			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
462223637Sbz		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
463223637Sbz			rpool->tblidx = -1;
464223637Sbz			if (pfr_pool_get(rpool->cur->addr.p.tbl,
465240233Sglebius			    &rpool->tblidx, &rpool->counter, af)) {
466223637Sbz				/* table contains no address of type 'af' */
467223637Sbz				if (rpool->cur != acur)
468223637Sbz					goto try_next;
469223637Sbz				return (1);
470223637Sbz			}
471223637Sbz		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
472223637Sbz			rpool->tblidx = -1;
473223637Sbz			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
474240233Sglebius			    &rpool->tblidx, &rpool->counter, af)) {
475223637Sbz				/* table contains no address of type 'af' */
476223637Sbz				if (rpool->cur != acur)
477223637Sbz					goto try_next;
478223637Sbz				return (1);
479223637Sbz			}
480223637Sbz		} else {
481223637Sbz			raddr = &rpool->cur->addr.v.a.addr;
482223637Sbz			rmask = &rpool->cur->addr.v.a.mask;
483223637Sbz			PF_ACPY(&rpool->counter, raddr, af);
484223637Sbz		}
485223637Sbz
486223637Sbz	get_addr:
487223637Sbz		PF_ACPY(naddr, &rpool->counter, af);
488223637Sbz		if (init_addr != NULL && PF_AZERO(init_addr, af))
489223637Sbz			PF_ACPY(init_addr, naddr, af);
490223637Sbz		PF_AINC(&rpool->counter, af);
491223637Sbz		break;
492240233Sglebius	    }
493223637Sbz	}
494223637Sbz	if (*sn != NULL)
495223637Sbz		PF_ACPY(&(*sn)->raddr, naddr, af);
496223637Sbz
497223637Sbz	if (V_pf_status.debug >= PF_DEBUG_MISC &&
498223637Sbz	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
499223637Sbz		printf("pf_map_addr: selected address ");
500223637Sbz		pf_print_host(naddr, 0, af);
501223637Sbz		printf("\n");
502223637Sbz	}
503223637Sbz
504223637Sbz	return (0);
505223637Sbz}
506223637Sbz
507223637Sbzstruct pf_rule *
508223637Sbzpf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
509223637Sbz    struct pfi_kif *kif, struct pf_src_node **sn,
510223637Sbz    struct pf_state_key **skp, struct pf_state_key **nkp,
511223637Sbz    struct pf_addr *saddr, struct pf_addr *daddr,
512240641Sglebius    uint16_t sport, uint16_t dport, struct pf_anchor_stackframe *anchor_stack)
513223637Sbz{
514223637Sbz	struct pf_rule	*r = NULL;
515240233Sglebius	struct pf_addr	*naddr;
516240233Sglebius	uint16_t	*nport;
517223637Sbz
518240233Sglebius	PF_RULES_RASSERT();
519240233Sglebius	KASSERT(*skp == NULL, ("*skp not NULL"));
520240233Sglebius	KASSERT(*nkp == NULL, ("*nkp not NULL"));
521223637Sbz
522223637Sbz	if (direction == PF_OUT) {
523223637Sbz		r = pf_match_translation(pd, m, off, direction, kif, saddr,
524240641Sglebius		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
525223637Sbz		if (r == NULL)
526223637Sbz			r = pf_match_translation(pd, m, off, direction, kif,
527240641Sglebius			    saddr, sport, daddr, dport, PF_RULESET_NAT,
528240641Sglebius			    anchor_stack);
529223637Sbz	} else {
530223637Sbz		r = pf_match_translation(pd, m, off, direction, kif, saddr,
531240641Sglebius		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
532223637Sbz		if (r == NULL)
533223637Sbz			r = pf_match_translation(pd, m, off, direction, kif,
534240641Sglebius			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
535240641Sglebius			    anchor_stack);
536223637Sbz	}
537223637Sbz
538240233Sglebius	if (r == NULL)
539240233Sglebius		return (NULL);
540223637Sbz
541240233Sglebius	switch (r->action) {
542240233Sglebius	case PF_NONAT:
543240233Sglebius	case PF_NOBINAT:
544240233Sglebius	case PF_NORDR:
545240233Sglebius		return (NULL);
546240233Sglebius	}
547223637Sbz
548240233Sglebius	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
549240233Sglebius	if (*skp == NULL)
550240233Sglebius		return (NULL);
551240233Sglebius	*nkp = pf_state_key_clone(*skp);
552240233Sglebius	if (*nkp == NULL) {
553240233Sglebius		uma_zfree(V_pf_state_key_z, skp);
554240233Sglebius		*skp = NULL;
555240233Sglebius		return (NULL);
556240233Sglebius	}
557223637Sbz
558240233Sglebius	/* XXX We only modify one side for now. */
559240233Sglebius	naddr = &(*nkp)->addr[1];
560240233Sglebius	nport = &(*nkp)->port[1];
561240233Sglebius
562240233Sglebius	switch (r->action) {
563240233Sglebius	case PF_NAT:
564255143Sglebius		if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, daddr,
565255143Sglebius		    dport, naddr, nport, r->rpool.proxy_port[0],
566240233Sglebius		    r->rpool.proxy_port[1], sn)) {
567240233Sglebius			DPFPRINTF(PF_DEBUG_MISC,
568240233Sglebius			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
569240233Sglebius			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
570240233Sglebius			goto notrans;
571240233Sglebius		}
572240233Sglebius		break;
573240233Sglebius	case PF_BINAT:
574240233Sglebius		switch (direction) {
575240233Sglebius		case PF_OUT:
576240233Sglebius			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
577240233Sglebius				switch (pd->af) {
578223637Sbz#ifdef INET
579240233Sglebius				case AF_INET:
580240233Sglebius					if (r->rpool.cur->addr.p.dyn->
581240233Sglebius					    pfid_acnt4 < 1)
582240233Sglebius						goto notrans;
583240233Sglebius					PF_POOLMASK(naddr,
584240233Sglebius					    &r->rpool.cur->addr.p.dyn->
585240233Sglebius					    pfid_addr4,
586240233Sglebius					    &r->rpool.cur->addr.p.dyn->
587240233Sglebius					    pfid_mask4, saddr, AF_INET);
588240233Sglebius					break;
589223637Sbz#endif /* INET */
590223637Sbz#ifdef INET6
591240233Sglebius				case AF_INET6:
592240233Sglebius					if (r->rpool.cur->addr.p.dyn->
593240233Sglebius					    pfid_acnt6 < 1)
594240233Sglebius						goto notrans;
595240233Sglebius					PF_POOLMASK(naddr,
596240233Sglebius					    &r->rpool.cur->addr.p.dyn->
597240233Sglebius					    pfid_addr6,
598240233Sglebius					    &r->rpool.cur->addr.p.dyn->
599240233Sglebius					    pfid_mask6, saddr, AF_INET6);
600240233Sglebius					break;
601223637Sbz#endif /* INET6 */
602240233Sglebius				}
603240233Sglebius			} else
604240233Sglebius				PF_POOLMASK(naddr,
605240233Sglebius				    &r->rpool.cur->addr.v.a.addr,
606240233Sglebius				    &r->rpool.cur->addr.v.a.mask, saddr,
607240233Sglebius				    pd->af);
608240233Sglebius			break;
609240233Sglebius		case PF_IN:
610240233Sglebius			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
611240233Sglebius				switch (pd->af) {
612240233Sglebius#ifdef INET
613240233Sglebius				case AF_INET:
614240233Sglebius					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
615240233Sglebius						goto notrans;
616223637Sbz					PF_POOLMASK(naddr,
617240233Sglebius					    &r->src.addr.p.dyn->pfid_addr4,
618240233Sglebius					    &r->src.addr.p.dyn->pfid_mask4,
619240233Sglebius					    daddr, AF_INET);
620240233Sglebius					break;
621223637Sbz#endif /* INET */
622223637Sbz#ifdef INET6
623240233Sglebius				case AF_INET6:
624240233Sglebius					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
625240233Sglebius						goto notrans;
626240233Sglebius					PF_POOLMASK(naddr,
627240233Sglebius					    &r->src.addr.p.dyn->pfid_addr6,
628240233Sglebius					    &r->src.addr.p.dyn->pfid_mask6,
629240233Sglebius					    daddr, AF_INET6);
630240233Sglebius					break;
631223637Sbz#endif /* INET6 */
632240233Sglebius				}
633240233Sglebius			} else
634240233Sglebius				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
635240233Sglebius				    &r->src.addr.v.a.mask, daddr, pd->af);
636223637Sbz			break;
637240233Sglebius		}
638240233Sglebius		break;
639240233Sglebius	case PF_RDR: {
640240233Sglebius		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
641240233Sglebius			goto notrans;
642240233Sglebius		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
643240233Sglebius			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
644240233Sglebius			    daddr, pd->af);
645223637Sbz
646240233Sglebius		if (r->rpool.proxy_port[1]) {
647240233Sglebius			uint32_t	tmp_nport;
648223637Sbz
649240233Sglebius			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
650240233Sglebius			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
651240233Sglebius			    1)) + r->rpool.proxy_port[0];
652223637Sbz
653240233Sglebius			/* Wrap around if necessary. */
654240233Sglebius			if (tmp_nport > 65535)
655240233Sglebius				tmp_nport -= 65535;
656240233Sglebius			*nport = htons((uint16_t)tmp_nport);
657240233Sglebius		} else if (r->rpool.proxy_port[0])
658240233Sglebius			*nport = htons(r->rpool.proxy_port[0]);
659240233Sglebius		break;
660223637Sbz	}
661240233Sglebius	default:
662240233Sglebius		panic("%s: unknown action %u", __func__, r->action);
663240233Sglebius	}
664223637Sbz
665240233Sglebius	/* Return success only if translation really happened. */
666240233Sglebius	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
667240233Sglebius		return (r);
668240233Sglebius
669240233Sglebiusnotrans:
670240233Sglebius	uma_zfree(V_pf_state_key_z, *nkp);
671240233Sglebius	uma_zfree(V_pf_state_key_z, *skp);
672240233Sglebius	*skp = *nkp = NULL;
673261023Sglebius	*sn = NULL;
674240233Sglebius
675240233Sglebius	return (NULL);
676223637Sbz}
677