xref: /openbsd-src/sys/net/pf_lb.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: pf_lb.c,v 1.63 2018/12/10 16:48:15 kn Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002 - 2008 Henning Brauer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Effort sponsored in part by the Defense Advanced Research Projects
33  * Agency (DARPA) and Air Force Research Laboratory, Air Force
34  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35  *
36  */
37 
38 #include "bpfilter.h"
39 #include "pflog.h"
40 #include "pfsync.h"
41 #include "pflow.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/filio.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/kernel.h>
50 #include <sys/time.h>
51 #include <sys/pool.h>
52 #include <sys/rwlock.h>
53 #include <sys/syslog.h>
54 #include <sys/stdint.h>
55 
56 #include <crypto/siphash.h>
57 
58 #include <net/if.h>
59 #include <net/bpf.h>
60 #include <net/route.h>
61 
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_icmp.h>
67 #include <netinet/icmp_var.h>
68 #include <netinet/tcp.h>
69 #include <netinet/tcp_seq.h>
70 #include <netinet/tcp_timer.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
73 #include <netinet/if_ether.h>
74 
75 #ifdef INET6
76 #include <netinet/ip6.h>
77 #include <netinet/icmp6.h>
78 #endif /* INET6 */
79 
80 #include <net/pfvar.h>
81 #include <net/pfvar_priv.h>
82 
83 #if NPFLOG > 0
84 #include <net/if_pflog.h>
85 #endif	/* NPFLOG > 0 */
86 
87 #if NPFLOW > 0
88 #include <net/if_pflow.h>
89 #endif	/* NPFLOW > 0 */
90 
91 #if NPFSYNC > 0
92 #include <net/if_pfsync.h>
93 #endif /* NPFSYNC > 0 */
94 
95 u_int64_t		 pf_hash(struct pf_addr *, struct pf_addr *,
96 			    struct pf_poolhashkey *, sa_family_t);
97 int			 pf_get_sport(struct pf_pdesc *, struct pf_rule *,
98 			    struct pf_addr *, u_int16_t *, u_int16_t,
99 			    u_int16_t, struct pf_src_node **);
100 int			 pf_get_transaddr_af(struct pf_rule *,
101 			    struct pf_pdesc *, struct pf_src_node **);
102 int			 pf_map_addr_sticky(sa_family_t, struct pf_rule *,
103 			    struct pf_addr *, struct pf_addr *,
104 			    struct pf_src_node **, struct pf_pool *,
105 			    enum pf_sn_types);
106 
107 u_int64_t
108 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
109     struct pf_poolhashkey *key, sa_family_t af)
110 {
111 	uint64_t res = 0;
112 #ifdef INET6
113 	union {
114 		uint64_t hash64;
115 		uint32_t hash32[2];
116 	} h;
117 #endif	/* INET6 */
118 
119 	switch (af) {
120 	case AF_INET:
121 		res = SipHash24((SIPHASH_KEY *)key,
122 		    &inaddr->addr32[0], sizeof(inaddr->addr32[0]));
123 		hash->addr32[0] = res;
124 		break;
125 #ifdef INET6
126 	case AF_INET6:
127 		res = SipHash24((SIPHASH_KEY *)key, &inaddr->addr32[0],
128 		    4 * sizeof(inaddr->addr32[0]));
129 		h.hash64 = res;
130 		hash->addr32[0] = h.hash32[0];
131 		hash->addr32[1] = h.hash32[1];
132 		/*
133 		 * siphash isn't big enough, but flipping it around is
134 		 * good enough here.
135 		 */
136 		hash->addr32[2] = ~h.hash32[1];
137 		hash->addr32[3] = ~h.hash32[0];
138 		break;
139 #endif /* INET6 */
140 	default:
141 		unhandled_af(af);
142 	}
143 	return (res);
144 }
145 
146 int
147 pf_get_sport(struct pf_pdesc *pd, struct pf_rule *r,
148     struct pf_addr *naddr, u_int16_t *nport, u_int16_t low, u_int16_t high,
149     struct pf_src_node **sn)
150 {
151 	struct pf_state_key_cmp	key;
152 	struct pf_addr		init_addr;
153 	u_int16_t		cut;
154 	int			dir = (pd->dir == PF_IN) ? PF_OUT : PF_IN;
155 	int			sidx = pd->sidx;
156 	int			didx = pd->didx;
157 
158 	memset(&init_addr, 0, sizeof(init_addr));
159 	if (pf_map_addr(pd->naf, r, &pd->nsaddr, naddr, &init_addr, sn, &r->nat,
160 	    PF_SN_NAT))
161 		return (1);
162 
163 	if (pd->proto == IPPROTO_ICMP) {
164 		if (pd->ndport == htons(ICMP_ECHO)) {
165 			low = 1;
166 			high = 65535;
167 		} else
168 			return (0);	/* Don't try to modify non-echo ICMP */
169 	}
170 #ifdef INET6
171 	if (pd->proto == IPPROTO_ICMPV6) {
172 		if (pd->ndport == htons(ICMP6_ECHO_REQUEST)) {
173 			low = 1;
174 			high = 65535;
175 		} else
176 			return (0);	/* Don't try to modify non-echo ICMP */
177 	}
178 #endif /* INET6 */
179 
180 	do {
181 		key.af = pd->naf;
182 		key.proto = pd->proto;
183 		key.rdomain = pd->rdomain;
184 		pf_addrcpy(&key.addr[didx], &pd->ndaddr, key.af);
185 		pf_addrcpy(&key.addr[sidx], naddr, key.af);
186 		key.port[didx] = pd->ndport;
187 
188 		/*
189 		 * port search; start random, step;
190 		 * similar 2 portloop in in_pcbbind
191 		 */
192 		if (!(pd->proto == IPPROTO_TCP || pd->proto == IPPROTO_UDP ||
193 		    pd->proto == IPPROTO_ICMP || pd->proto == IPPROTO_ICMPV6)) {
194 			/* XXX bug: icmp states dont use the id on both
195 			 * XXX sides (traceroute -I through nat) */
196 			key.port[sidx] = pd->nsport;
197 			if (pf_find_state_all(&key, dir, NULL) == NULL) {
198 				*nport = pd->nsport;
199 				return (0);
200 			}
201 		} else if (low == 0 && high == 0) {
202 			key.port[sidx] = pd->nsport;
203 			if (pf_find_state_all(&key, dir, NULL) == NULL) {
204 				*nport = pd->nsport;
205 				return (0);
206 			}
207 		} else if (low == high) {
208 			key.port[sidx] = htons(low);
209 			if (pf_find_state_all(&key, dir, NULL) == NULL) {
210 				*nport = htons(low);
211 				return (0);
212 			}
213 		} else {
214 			u_int32_t tmp;
215 
216 			if (low > high) {
217 				tmp = low;
218 				low = high;
219 				high = tmp;
220 			}
221 			/* low < high */
222 			cut = arc4random_uniform(1 + high - low) + low;
223 			/* low <= cut <= high */
224 			for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
225 				key.port[sidx] = htons(tmp);
226 				if (pf_find_state_all(&key, dir, NULL) ==
227 				    NULL && !in_baddynamic(tmp, pd->proto)) {
228 					*nport = htons(tmp);
229 					return (0);
230 				}
231 			}
232 			tmp = cut;
233 			for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
234 				key.port[sidx] = htons(tmp);
235 				if (pf_find_state_all(&key, dir, NULL) ==
236 				    NULL && !in_baddynamic(tmp, pd->proto)) {
237 					*nport = htons(tmp);
238 					return (0);
239 				}
240 			}
241 		}
242 
243 		switch (r->nat.opts & PF_POOL_TYPEMASK) {
244 		case PF_POOL_RANDOM:
245 		case PF_POOL_ROUNDROBIN:
246 		case PF_POOL_LEASTSTATES:
247 			/*
248 			 * pick a different source address since we're out
249 			 * of free port choices for the current one.
250 			 */
251 			if (pf_map_addr(pd->naf, r, &pd->nsaddr, naddr,
252 			    &init_addr, sn, &r->nat, PF_SN_NAT))
253 				return (1);
254 			break;
255 		case PF_POOL_NONE:
256 		case PF_POOL_SRCHASH:
257 		case PF_POOL_BITMASK:
258 		default:
259 			return (1);
260 		}
261 	} while (! PF_AEQ(&init_addr, naddr, pd->naf) );
262 	return (1);					/* none available */
263 }
264 
265 int
266 pf_map_addr_sticky(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
267     struct pf_addr *naddr, struct pf_src_node **sns, struct pf_pool *rpool,
268     enum pf_sn_types type)
269 {
270 	struct pf_addr		*raddr, *rmask, *cached;
271 	struct pf_state		*s;
272 	struct pf_src_node	 k;
273 	int			 valid;
274 
275 	k.af = af;
276 	k.type = type;
277 	pf_addrcpy(&k.addr, saddr, af);
278 	k.rule.ptr = r;
279 	pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
280 	sns[type] = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
281 	if (sns[type] == NULL)
282 		return (-1);
283 
284 	/* check if the cached entry is still valid */
285 	cached = &(sns[type])->raddr;
286 	valid = 0;
287 	if (PF_AZERO(cached, af)) {
288 		valid = 1;
289 	} else if (rpool->addr.type == PF_ADDR_DYNIFTL) {
290 		if (pfr_kentry_byaddr(rpool->addr.p.dyn->pfid_kt, cached,
291 		    af, 0))
292 			valid = 1;
293 	} else if (rpool->addr.type == PF_ADDR_TABLE) {
294 		if (pfr_kentry_byaddr(rpool->addr.p.tbl, cached, af, 0))
295 			valid = 1;
296 	} else if (rpool->addr.type != PF_ADDR_NOROUTE) {
297 		raddr = &rpool->addr.v.a.addr;
298 		rmask = &rpool->addr.v.a.mask;
299 		valid = pf_match_addr(0, raddr, rmask, cached, af);
300 	}
301 	if (!valid) {
302 		if (pf_status.debug >= LOG_DEBUG) {
303 			log(LOG_DEBUG, "pf: pf_map_addr: "
304 			    "stale src tracking (%u) ", type);
305 			pf_print_host(&k.addr, 0, af);
306 			addlog(" to ");
307 			pf_print_host(cached, 0, af);
308 			addlog("\n");
309 		}
310 		if (sns[type]->states != 0) {
311 			/* XXX expensive */
312 			RB_FOREACH(s, pf_state_tree_id,
313 			   &tree_id)
314 				pf_state_rm_src_node(s,
315 				    sns[type]);
316 		}
317 		sns[type]->expire = 1;
318 		pf_remove_src_node(sns[type]);
319 		sns[type] = NULL;
320 		return (-1);
321 	}
322 	if (!PF_AZERO(cached, af))
323 		pf_addrcpy(naddr, cached, af);
324 	if (pf_status.debug >= LOG_DEBUG) {
325 		log(LOG_DEBUG, "pf: pf_map_addr: "
326 		    "src tracking (%u) maps ", type);
327 		pf_print_host(&k.addr, 0, af);
328 		addlog(" to ");
329 		pf_print_host(naddr, 0, af);
330 		addlog("\n");
331 	}
332 	return (0);
333 }
334 
335 int
336 pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr,
337     struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sns,
338     struct pf_pool *rpool, enum pf_sn_types type)
339 {
340 	unsigned char		 hash[16];
341 	struct pf_addr		 faddr;
342 	struct pf_addr		*raddr = &rpool->addr.v.a.addr;
343 	struct pf_addr		*rmask = &rpool->addr.v.a.mask;
344 	u_int64_t		 states;
345 	u_int16_t		 weight;
346 	u_int64_t		 load;
347 	u_int64_t		 cload;
348 	u_int64_t		 hashidx;
349 	int			 cnt;
350 
351 	if (sns[type] == NULL && rpool->opts & PF_POOL_STICKYADDR &&
352 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE &&
353 	    pf_map_addr_sticky(af, r, saddr, naddr, sns, rpool, type) == 0)
354 		return (0);
355 
356 	if (rpool->addr.type == PF_ADDR_NOROUTE)
357 		return (1);
358 	if (rpool->addr.type == PF_ADDR_DYNIFTL) {
359 		switch (af) {
360 		case AF_INET:
361 			if (rpool->addr.p.dyn->pfid_acnt4 < 1 &&
362 			    !PF_POOL_DYNTYPE(rpool->opts))
363 				return (1);
364 			raddr = &rpool->addr.p.dyn->pfid_addr4;
365 			rmask = &rpool->addr.p.dyn->pfid_mask4;
366 			break;
367 #ifdef INET6
368 		case AF_INET6:
369 			if (rpool->addr.p.dyn->pfid_acnt6 < 1 &&
370 			    !PF_POOL_DYNTYPE(rpool->opts))
371 				return (1);
372 			raddr = &rpool->addr.p.dyn->pfid_addr6;
373 			rmask = &rpool->addr.p.dyn->pfid_mask6;
374 			break;
375 #endif /* INET6 */
376 		default:
377 			unhandled_af(af);
378 		}
379 	} else if (rpool->addr.type == PF_ADDR_TABLE) {
380 		if (!PF_POOL_DYNTYPE(rpool->opts))
381 			return (1); /* unsupported */
382 	} else {
383 		raddr = &rpool->addr.v.a.addr;
384 		rmask = &rpool->addr.v.a.mask;
385 	}
386 
387 	switch (rpool->opts & PF_POOL_TYPEMASK) {
388 	case PF_POOL_NONE:
389 		pf_addrcpy(naddr, raddr, af);
390 		break;
391 	case PF_POOL_BITMASK:
392 		pf_poolmask(naddr, raddr, rmask, saddr, af);
393 		break;
394 	case PF_POOL_RANDOM:
395 		if (rpool->addr.type == PF_ADDR_TABLE) {
396 			cnt = rpool->addr.p.tbl->pfrkt_cnt;
397 			if (cnt == 0)
398 				rpool->tblidx = 0;
399 			else
400 				rpool->tblidx = (int)arc4random_uniform(cnt);
401 			memset(&rpool->counter, 0, sizeof(rpool->counter));
402 			if (pfr_pool_get(rpool, &raddr, &rmask, af))
403 				return (1);
404 			pf_addrcpy(naddr, &rpool->counter, af);
405 		} else if (rpool->addr.type == PF_ADDR_DYNIFTL) {
406 			cnt = rpool->addr.p.dyn->pfid_kt->pfrkt_cnt;
407 			if (cnt == 0)
408 				rpool->tblidx = 0;
409 			else
410 				rpool->tblidx = (int)arc4random_uniform(cnt);
411 			memset(&rpool->counter, 0, sizeof(rpool->counter));
412 			if (pfr_pool_get(rpool, &raddr, &rmask, af))
413 				return (1);
414 			pf_addrcpy(naddr, &rpool->counter, af);
415 		} else if (init_addr != NULL && PF_AZERO(init_addr, af)) {
416 			switch (af) {
417 			case AF_INET:
418 				rpool->counter.addr32[0] = arc4random();
419 				break;
420 #ifdef INET6
421 			case AF_INET6:
422 				if (rmask->addr32[3] != 0xffffffff)
423 					rpool->counter.addr32[3] = arc4random();
424 				else
425 					break;
426 				if (rmask->addr32[2] != 0xffffffff)
427 					rpool->counter.addr32[2] = arc4random();
428 				else
429 					break;
430 				if (rmask->addr32[1] != 0xffffffff)
431 					rpool->counter.addr32[1] = arc4random();
432 				else
433 					break;
434 				if (rmask->addr32[0] != 0xffffffff)
435 					rpool->counter.addr32[0] = arc4random();
436 				break;
437 #endif /* INET6 */
438 			default:
439 				unhandled_af(af);
440 			}
441 			pf_poolmask(naddr, raddr, rmask, &rpool->counter, af);
442 			pf_addrcpy(init_addr, naddr, af);
443 
444 		} else {
445 			pf_addr_inc(&rpool->counter, af);
446 			pf_poolmask(naddr, raddr, rmask, &rpool->counter, af);
447 		}
448 		break;
449 	case PF_POOL_SRCHASH:
450 		hashidx =
451 		    pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
452 		if (rpool->addr.type == PF_ADDR_TABLE) {
453 			cnt = rpool->addr.p.tbl->pfrkt_cnt;
454 			if (cnt == 0)
455 				rpool->tblidx = 0;
456 			else
457 				rpool->tblidx = (int)(hashidx % cnt);
458 			memset(&rpool->counter, 0, sizeof(rpool->counter));
459 			if (pfr_pool_get(rpool, &raddr, &rmask, af))
460 				return (1);
461 			pf_addrcpy(naddr, &rpool->counter, af);
462 		} else if (rpool->addr.type == PF_ADDR_DYNIFTL) {
463 			cnt = rpool->addr.p.dyn->pfid_kt->pfrkt_cnt;
464 			if (cnt == 0)
465 				rpool->tblidx = 0;
466 			else
467 				rpool->tblidx = (int)(hashidx % cnt);
468 			memset(&rpool->counter, 0, sizeof(rpool->counter));
469 			if (pfr_pool_get(rpool, &raddr, &rmask, af))
470 				return (1);
471 			pf_addrcpy(naddr, &rpool->counter, af);
472 		} else {
473 			pf_poolmask(naddr, raddr, rmask,
474 			    (struct pf_addr *)&hash, af);
475 		}
476 		break;
477 	case PF_POOL_ROUNDROBIN:
478 		if (rpool->addr.type == PF_ADDR_TABLE ||
479 		    rpool->addr.type == PF_ADDR_DYNIFTL) {
480 			if (pfr_pool_get(rpool, &raddr, &rmask, af)) {
481 				/*
482 				 * reset counter in case its value
483 				 * has been removed from the pool.
484 				 */
485 				memset(&rpool->counter, 0,
486 				    sizeof(rpool->counter));
487 				if (pfr_pool_get(rpool, &raddr, &rmask, af))
488 					return (1);
489 			}
490 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
491 			return (1);
492 
493 		/* iterate over table if it contains entries which are weighted */
494 		if ((rpool->addr.type == PF_ADDR_TABLE &&
495 		    rpool->addr.p.tbl->pfrkt_refcntcost > 0) ||
496 		    (rpool->addr.type == PF_ADDR_DYNIFTL &&
497 		    rpool->addr.p.dyn->pfid_kt->pfrkt_refcntcost > 0)) {
498 			do {
499 				if (rpool->addr.type == PF_ADDR_TABLE ||
500 				    rpool->addr.type == PF_ADDR_DYNIFTL) {
501 					if (pfr_pool_get(rpool,
502 					    &raddr, &rmask, af))
503 						return (1);
504 				} else {
505 					log(LOG_ERR, "pf: pf_map_addr: "
506 					    "weighted RR failure");
507 					return (1);
508 				}
509 				if (rpool->weight >= rpool->curweight)
510 					break;
511 				pf_addr_inc(&rpool->counter, af);
512 			} while (1);
513 
514 			weight = rpool->weight;
515 		}
516 
517 		pf_addrcpy(naddr, &rpool->counter, af);
518 		if (init_addr != NULL && PF_AZERO(init_addr, af))
519 			pf_addrcpy(init_addr, naddr, af);
520 		pf_addr_inc(&rpool->counter, af);
521 		break;
522 	case PF_POOL_LEASTSTATES:
523 		/* retrieve an address first */
524 		if (rpool->addr.type == PF_ADDR_TABLE ||
525 		    rpool->addr.type == PF_ADDR_DYNIFTL) {
526 			if (pfr_pool_get(rpool, &raddr, &rmask, af)) {
527 				/* see PF_POOL_ROUNDROBIN */
528 				memset(&rpool->counter, 0,
529 				    sizeof(rpool->counter));
530 				if (pfr_pool_get(rpool, &raddr, &rmask, af))
531 					return (1);
532 			}
533 		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
534 			return (1);
535 
536 		states = rpool->states;
537 		weight = rpool->weight;
538 
539 		if ((rpool->addr.type == PF_ADDR_TABLE &&
540 		    rpool->addr.p.tbl->pfrkt_refcntcost > 0) ||
541 		    (rpool->addr.type == PF_ADDR_DYNIFTL &&
542 		    rpool->addr.p.dyn->pfid_kt->pfrkt_refcntcost > 0))
543 			load = ((UINT16_MAX * rpool->states) / rpool->weight);
544 		else
545 			load = states;
546 
547 		pf_addrcpy(&faddr, &rpool->counter, af);
548 
549 		pf_addrcpy(naddr, &rpool->counter, af);
550 		if (init_addr != NULL && PF_AZERO(init_addr, af))
551 			pf_addrcpy(init_addr, naddr, af);
552 
553 		/*
554 		 * iterate *once* over whole table and find destination with
555 		 * least connection
556 		 */
557 		do  {
558 			pf_addr_inc(&rpool->counter, af);
559 			if (rpool->addr.type == PF_ADDR_TABLE ||
560 			    rpool->addr.type == PF_ADDR_DYNIFTL) {
561 				if (pfr_pool_get(rpool, &raddr, &rmask, af))
562 					return (1);
563 			} else if (pf_match_addr(0, raddr, rmask,
564 			    &rpool->counter, af))
565 				return (1);
566 
567 			if ((rpool->addr.type == PF_ADDR_TABLE &&
568 			    rpool->addr.p.tbl->pfrkt_refcntcost > 0) ||
569 			    (rpool->addr.type == PF_ADDR_DYNIFTL &&
570 			    rpool->addr.p.dyn->pfid_kt->pfrkt_refcntcost > 0))
571 				cload = ((UINT16_MAX * rpool->states)
572 					/ rpool->weight);
573 			else
574 				cload = rpool->states;
575 
576 			/* find lc minimum */
577 			if (cload < load) {
578 				states = rpool->states;
579 				weight = rpool->weight;
580 				load = cload;
581 
582 				pf_addrcpy(naddr, &rpool->counter, af);
583 				if (init_addr != NULL &&
584 				    PF_AZERO(init_addr, af))
585 				    pf_addrcpy(init_addr, naddr, af);
586 			}
587 		} while (pf_match_addr(1, &faddr, rmask, &rpool->counter, af) &&
588 		    (states > 0));
589 
590 		if (rpool->addr.type == PF_ADDR_TABLE) {
591 			if (pfr_states_increase(rpool->addr.p.tbl,
592 			    naddr, af) == -1) {
593 				if (pf_status.debug >= LOG_DEBUG) {
594 					log(LOG_DEBUG,"pf: pf_map_addr: "
595 					    "selected address ");
596 					pf_print_host(naddr, 0, af);
597 					addlog(". Failed to increase count!\n");
598 				}
599 				return (1);
600 			}
601 		} else if (rpool->addr.type == PF_ADDR_DYNIFTL) {
602 			if (pfr_states_increase(rpool->addr.p.dyn->pfid_kt,
603 			    naddr, af) == -1) {
604 				if (pf_status.debug >= LOG_DEBUG) {
605 					log(LOG_DEBUG, "pf: pf_map_addr: "
606 					    "selected address ");
607 					pf_print_host(naddr, 0, af);
608 					addlog(". Failed to increase count!\n");
609 				}
610 				return (1);
611 			}
612 		}
613 		break;
614 	}
615 
616 	if (rpool->opts & PF_POOL_STICKYADDR) {
617 		if (sns[type] != NULL) {
618 			pf_remove_src_node(sns[type]);
619 			sns[type] = NULL;
620 		}
621 		if (pf_insert_src_node(&sns[type], r, type, af, saddr, naddr))
622 			return (1);
623 	}
624 
625 	if (pf_status.debug >= LOG_INFO &&
626 	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
627 		log(LOG_INFO, "pf: pf_map_addr: selected address ");
628 		pf_print_host(naddr, 0, af);
629 		if ((rpool->opts & PF_POOL_TYPEMASK) ==
630 		    PF_POOL_LEASTSTATES)
631 			addlog(" with state count %llu", states);
632 		if ((rpool->addr.type == PF_ADDR_TABLE &&
633 		    rpool->addr.p.tbl->pfrkt_refcntcost > 0) ||
634 		    (rpool->addr.type == PF_ADDR_DYNIFTL &&
635 		    rpool->addr.p.dyn->pfid_kt->pfrkt_refcntcost > 0))
636 			addlog(" with weight %u", weight);
637 		addlog("\n");
638 	}
639 
640 	return (0);
641 }
642 
643 int
644 pf_get_transaddr(struct pf_rule *r, struct pf_pdesc *pd,
645     struct pf_src_node **sns, struct pf_rule **nr)
646 {
647 	struct pf_addr	naddr;
648 	u_int16_t	nport;
649 
650 #ifdef INET6
651 	if (pd->af != pd->naf)
652 		return (pf_get_transaddr_af(r, pd, sns));
653 #endif /* INET6 */
654 
655 	if (r->nat.addr.type != PF_ADDR_NONE) {
656 		/* XXX is this right? what if rtable is changed at the same
657 		 * XXX time? where do I need to figure out the sport? */
658 		nport = 0;
659 		if (pf_get_sport(pd, r, &naddr, &nport,
660 		    r->nat.proxy_port[0], r->nat.proxy_port[1], sns)) {
661 			DPFPRINTF(LOG_NOTICE,
662 			    "pf: NAT proxy port allocation (%u-%u) failed",
663 			    r->nat.proxy_port[0],
664 			    r->nat.proxy_port[1]);
665 			return (-1);
666 		}
667 		*nr = r;
668 		pf_addrcpy(&pd->nsaddr, &naddr, pd->af);
669 		pd->nsport = nport;
670 	}
671 	if (r->rdr.addr.type != PF_ADDR_NONE) {
672 		if (pf_map_addr(pd->af, r, &pd->nsaddr, &naddr, NULL, sns,
673 		    &r->rdr, PF_SN_RDR))
674 			return (-1);
675 		if ((r->rdr.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
676 			pf_poolmask(&naddr, &naddr,  &r->rdr.addr.v.a.mask,
677 			    &pd->ndaddr, pd->af);
678 
679 		nport = 0;
680 		if (r->rdr.proxy_port[1]) {
681 			u_int32_t	tmp_nport;
682 
683 			tmp_nport = ((ntohs(pd->ndport) -
684 			    ntohs(r->dst.port[0])) %
685 			    (r->rdr.proxy_port[1] -
686 			    r->rdr.proxy_port[0] + 1)) +
687 			    r->rdr.proxy_port[0];
688 
689 			/* wrap around if necessary */
690 			if (tmp_nport > 65535)
691 				tmp_nport -= 65535;
692 			nport = htons((u_int16_t)tmp_nport);
693 		} else if (r->rdr.proxy_port[0])
694 			nport = htons(r->rdr.proxy_port[0]);
695 		*nr = r;
696 		pf_addrcpy(&pd->ndaddr, &naddr, pd->af);
697 		if (nport)
698 			pd->ndport = nport;
699 	}
700 
701 	return (0);
702 }
703 
704 #ifdef INET6
705 int
706 pf_get_transaddr_af(struct pf_rule *r, struct pf_pdesc *pd,
707     struct pf_src_node **sns)
708 {
709 	struct pf_addr	ndaddr, nsaddr, naddr;
710 	u_int16_t	nport;
711 	int		prefixlen = 96;
712 
713 	if (pf_status.debug >= LOG_INFO) {
714 		log(LOG_INFO, "pf: af-to %s %s, ",
715 		    pd->naf == AF_INET ? "inet" : "inet6",
716 		    r->rdr.addr.type == PF_ADDR_NONE ? "nat" : "rdr");
717 		pf_print_host(&pd->nsaddr, pd->nsport, pd->af);
718 		addlog(" -> ");
719 		pf_print_host(&pd->ndaddr, pd->ndport, pd->af);
720 		addlog("\n");
721 	}
722 
723 	if (r->nat.addr.type == PF_ADDR_NONE)
724 		panic("pf_get_transaddr_af: no nat pool for source address");
725 
726 	/* get source address and port */
727 	nport = 0;
728 	if (pf_get_sport(pd, r, &nsaddr, &nport,
729 	    r->nat.proxy_port[0], r->nat.proxy_port[1], sns)) {
730 		DPFPRINTF(LOG_NOTICE,
731 		    "pf: af-to NAT proxy port allocation (%u-%u) failed",
732 		    r->nat.proxy_port[0],
733 		    r->nat.proxy_port[1]);
734 		return (-1);
735 	}
736 	pd->nsport = nport;
737 
738 	if (pd->proto == IPPROTO_ICMPV6 && pd->naf == AF_INET) {
739 		if (pd->dir == PF_IN) {
740 			pd->ndport = ntohs(pd->ndport);
741 			if (pd->ndport == ICMP6_ECHO_REQUEST)
742 				pd->ndport = ICMP_ECHO;
743 			else if (pd->ndport == ICMP6_ECHO_REPLY)
744 				pd->ndport = ICMP_ECHOREPLY;
745 			pd->ndport = htons(pd->ndport);
746 		} else {
747 			pd->nsport = ntohs(pd->nsport);
748 			if (pd->nsport == ICMP6_ECHO_REQUEST)
749 				pd->nsport = ICMP_ECHO;
750 			else if (pd->nsport == ICMP6_ECHO_REPLY)
751 				pd->nsport = ICMP_ECHOREPLY;
752 			pd->nsport = htons(pd->nsport);
753 		}
754 	} else if (pd->proto == IPPROTO_ICMP && pd->naf == AF_INET6) {
755 		if (pd->dir == PF_IN) {
756 			pd->ndport = ntohs(pd->ndport);
757 			if (pd->ndport == ICMP_ECHO)
758 				pd->ndport = ICMP6_ECHO_REQUEST;
759 			else if (pd->ndport == ICMP_ECHOREPLY)
760 				pd->ndport = ICMP6_ECHO_REPLY;
761 			pd->ndport = htons(pd->ndport);
762 		} else {
763 			pd->nsport = ntohs(pd->nsport);
764 			if (pd->nsport == ICMP_ECHO)
765 				pd->nsport = ICMP6_ECHO_REQUEST;
766 			else if (pd->nsport == ICMP_ECHOREPLY)
767 				pd->nsport = ICMP6_ECHO_REPLY;
768 			pd->nsport = htons(pd->nsport);
769 		}
770 	}
771 
772 	/* get the destination address and port */
773 	if (r->rdr.addr.type != PF_ADDR_NONE) {
774 		if (pf_map_addr(pd->naf, r, &nsaddr, &naddr, NULL, sns,
775 		    &r->rdr, PF_SN_RDR))
776 			return (-1);
777 		if (r->rdr.proxy_port[0])
778 			pd->ndport = htons(r->rdr.proxy_port[0]);
779 
780 		if (pd->naf == AF_INET) {
781 			/* The prefix is the IPv4 rdr address */
782 			prefixlen = in_mask2len((struct in_addr *)
783 			    &r->rdr.addr.v.a.mask);
784 			inet_nat46(pd->naf, &pd->ndaddr,
785 			    &ndaddr, &naddr, prefixlen);
786 		} else {
787 			/* The prefix is the IPv6 rdr address */
788 			prefixlen =
789 			    in6_mask2len((struct in6_addr *)
790 			    &r->rdr.addr.v.a.mask, NULL);
791 			inet_nat64(pd->naf, &pd->ndaddr,
792 			    &ndaddr, &naddr, prefixlen);
793 		}
794 	} else {
795 		if (pd->naf == AF_INET) {
796 			/* The prefix is the IPv6 dst address */
797 			prefixlen =
798 			    in6_mask2len((struct in6_addr *)
799 			    &r->dst.addr.v.a.mask, NULL);
800 			if (prefixlen < 32)
801 				prefixlen = 96;
802 			inet_nat64(pd->naf, &pd->ndaddr,
803 			    &ndaddr, &pd->ndaddr, prefixlen);
804 		} else {
805 			/*
806 			 * The prefix is the IPv6 nat address
807 			 * (that was stored in pd->nsaddr)
808 			 */
809 			prefixlen = in6_mask2len((struct in6_addr *)
810 			    &r->nat.addr.v.a.mask, NULL);
811 			if (prefixlen > 96)
812 				prefixlen = 96;
813 			inet_nat64(pd->naf, &pd->ndaddr,
814 			    &ndaddr, &nsaddr, prefixlen);
815 		}
816 	}
817 
818 	pf_addrcpy(&pd->nsaddr, &nsaddr, pd->naf);
819 	pf_addrcpy(&pd->ndaddr, &ndaddr, pd->naf);
820 
821 	if (pf_status.debug >= LOG_INFO) {
822 		log(LOG_INFO, "pf: af-to %s %s done, prefixlen %d, ",
823 		    pd->naf == AF_INET ? "inet" : "inet6",
824 		    r->rdr.addr.type == PF_ADDR_NONE ? "nat" : "rdr",
825 		    prefixlen);
826 		pf_print_host(&pd->nsaddr, pd->nsport, pd->naf);
827 		addlog(" -> ");
828 		pf_print_host(&pd->ndaddr, pd->ndport, pd->naf);
829 		addlog("\n");
830 	}
831 
832 	return (0);
833 }
834 #endif /* INET6 */
835 
836 int
837 pf_postprocess_addr(struct pf_state *cur)
838 {
839 	struct pf_rule		*nr;
840 	struct pf_state_key	*sks;
841 	struct pf_pool		 rpool;
842 	struct pf_addr		 lookup_addr;
843 	int			 slbcount = -1;
844 
845 	nr = cur->natrule.ptr;
846 
847 	if (nr == NULL)
848 		return (0);
849 
850 	/* decrease counter */
851 
852 	sks = cur->key[PF_SK_STACK];
853 
854 	/* check for outgoing or ingoing balancing */
855 	if (nr->rt == PF_ROUTETO)
856 		lookup_addr = cur->rt_addr;
857 	else if (sks != NULL)
858 		lookup_addr = sks->addr[1];
859 	else {
860 		if (pf_status.debug >= LOG_DEBUG) {
861 			log(LOG_DEBUG, "pf: %s: unable to obtain address",
862 			    __func__);
863 		}
864 		return (1);
865 	}
866 
867 	/* check for appropriate pool */
868 	if (nr->rdr.addr.type != PF_ADDR_NONE)
869 		rpool = nr->rdr;
870 	else if (nr->nat.addr.type != PF_ADDR_NONE)
871 		rpool = nr->nat;
872 	else if (nr->route.addr.type != PF_ADDR_NONE)
873 		rpool = nr->route;
874 	else
875 		return (0);
876 
877 	if (((rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_LEASTSTATES))
878 		return (0);
879 
880 	if (rpool.addr.type == PF_ADDR_TABLE) {
881 		if ((slbcount = pfr_states_decrease(
882 		    rpool.addr.p.tbl,
883 		    &lookup_addr, sks->af)) == -1) {
884 			if (pf_status.debug >= LOG_DEBUG) {
885 				log(LOG_DEBUG, "pf: %s: selected address ",
886 				    __func__);
887 				pf_print_host(&lookup_addr,
888 				    sks->port[0], sks->af);
889 				addlog(". Failed to "
890 				    "decrease count!\n");
891 			}
892 			return (1);
893 		}
894 	} else if (rpool.addr.type == PF_ADDR_DYNIFTL) {
895 		if ((slbcount = pfr_states_decrease(
896 		    rpool.addr.p.dyn->pfid_kt,
897 		    &lookup_addr, sks->af)) == -1) {
898 			if (pf_status.debug >= LOG_DEBUG) {
899 				log(LOG_DEBUG, "pf: %s: selected address ",
900 				    __func__);
901 				pf_print_host(&lookup_addr,
902 				    sks->port[0], sks->af);
903 				addlog(". Failed to "
904 				    "decrease count!\n");
905 			}
906 			return (1);
907 		}
908 	}
909 	if (slbcount > -1) {
910 		if (pf_status.debug >= LOG_INFO) {
911 			log(LOG_INFO, "pf: %s: selected address ", __func__);
912 			pf_print_host(&lookup_addr, sks->port[0],
913 			    sks->af);
914 			addlog(" decreased state count to %u\n",
915 			    slbcount);
916 		}
917 	}
918 	return (0);
919 }
920