1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 *	$OpenBSD: pf_lb.c,v 1.2 2009/02/12 02:13:15 sthen Exp $
37 */
38
39#include <sys/cdefs.h>
40#include "opt_pf.h"
41#include "opt_inet.h"
42#include "opt_inet6.h"
43
44#include <sys/param.h>
45#include <sys/lock.h>
46#include <sys/mbuf.h>
47#include <sys/socket.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
51#include <net/vnet.h>
52#include <net/pfvar.h>
53#include <net/if_pflog.h>
54
55#define DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
56
57static void		 pf_hash(struct pf_addr *, struct pf_addr *,
58			    struct pf_poolhashkey *, sa_family_t);
59static struct pf_krule	*pf_match_translation(struct pf_pdesc *, struct mbuf *,
60			    int, struct pfi_kkif *,
61			    struct pf_addr *, u_int16_t, struct pf_addr *,
62			    uint16_t, int, struct pf_kanchor_stackframe *);
63static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *,
64    struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
65    uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **);
66
67#define mix(a,b,c) \
68	do {					\
69		a -= b; a -= c; a ^= (c >> 13);	\
70		b -= c; b -= a; b ^= (a << 8);	\
71		c -= a; c -= b; c ^= (b >> 13);	\
72		a -= b; a -= c; a ^= (c >> 12);	\
73		b -= c; b -= a; b ^= (a << 16);	\
74		c -= a; c -= b; c ^= (b >> 5);	\
75		a -= b; a -= c; a ^= (c >> 3);	\
76		b -= c; b -= a; b ^= (a << 10);	\
77		c -= a; c -= b; c ^= (b >> 15);	\
78	} while (0)
79
80/*
81 * hash function based on bridge_hash in if_bridge.c
82 */
83static void
84pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
85    struct pf_poolhashkey *key, sa_family_t af)
86{
87	u_int32_t	a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
88
89	switch (af) {
90#ifdef INET
91	case AF_INET:
92		a += inaddr->addr32[0];
93		b += key->key32[1];
94		mix(a, b, c);
95		hash->addr32[0] = c + key->key32[2];
96		break;
97#endif /* INET */
98#ifdef INET6
99	case AF_INET6:
100		a += inaddr->addr32[0];
101		b += inaddr->addr32[2];
102		mix(a, b, c);
103		hash->addr32[0] = c;
104		a += inaddr->addr32[1];
105		b += inaddr->addr32[3];
106		c += key->key32[1];
107		mix(a, b, c);
108		hash->addr32[1] = c;
109		a += inaddr->addr32[2];
110		b += inaddr->addr32[1];
111		c += key->key32[2];
112		mix(a, b, c);
113		hash->addr32[2] = c;
114		a += inaddr->addr32[3];
115		b += inaddr->addr32[0];
116		c += key->key32[3];
117		mix(a, b, c);
118		hash->addr32[3] = c;
119		break;
120#endif /* INET6 */
121	}
122}
123
124static struct pf_krule *
125pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
126    struct pfi_kkif *kif, struct pf_addr *saddr, u_int16_t sport,
127    struct pf_addr *daddr, uint16_t dport, int rs_num,
128    struct pf_kanchor_stackframe *anchor_stack)
129{
130	struct pf_krule		*r, *rm = NULL;
131	struct pf_kruleset	*ruleset = NULL;
132	int			 tag = -1;
133	int			 rtableid = -1;
134	int			 asd = 0;
135
136	r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
137	while (r != NULL) {
138		struct pf_rule_addr	*src = NULL, *dst = NULL;
139		struct pf_addr_wrap	*xdst = NULL;
140
141		if (r->action == PF_BINAT && pd->dir == PF_IN) {
142			src = &r->dst;
143			if (r->rpool.cur != NULL)
144				xdst = &r->rpool.cur->addr;
145		} else {
146			src = &r->src;
147			dst = &r->dst;
148		}
149
150		pf_counter_u64_add(&r->evaluations, 1);
151		if (pfi_kkif_match(r->kif, kif) == r->ifnot)
152			r = r->skip[PF_SKIP_IFP].ptr;
153		else if (r->direction && r->direction != pd->dir)
154			r = r->skip[PF_SKIP_DIR].ptr;
155		else if (r->af && r->af != pd->af)
156			r = r->skip[PF_SKIP_AF].ptr;
157		else if (r->proto && r->proto != pd->proto)
158			r = r->skip[PF_SKIP_PROTO].ptr;
159		else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
160		    src->neg, kif, M_GETFIB(m)))
161			r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
162			    PF_SKIP_DST_ADDR].ptr;
163		else if (src->port_op && !pf_match_port(src->port_op,
164		    src->port[0], src->port[1], sport))
165			r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
166			    PF_SKIP_DST_PORT].ptr;
167		else if (dst != NULL &&
168		    PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
169		    M_GETFIB(m)))
170			r = r->skip[PF_SKIP_DST_ADDR].ptr;
171		else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
172		    0, NULL, M_GETFIB(m)))
173			r = TAILQ_NEXT(r, entries);
174		else if (dst != NULL && dst->port_op &&
175		    !pf_match_port(dst->port_op, dst->port[0],
176		    dst->port[1], dport))
177			r = r->skip[PF_SKIP_DST_PORT].ptr;
178		else if (r->match_tag && !pf_match_tag(m, r, &tag,
179		    pd->pf_mtag ? pd->pf_mtag->tag : 0))
180			r = TAILQ_NEXT(r, entries);
181		else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
182		    IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
183		    off, &pd->hdr.tcp), r->os_fingerprint)))
184			r = TAILQ_NEXT(r, entries);
185		else {
186			if (r->tag)
187				tag = r->tag;
188			if (r->rtableid >= 0)
189				rtableid = r->rtableid;
190			if (r->anchor == NULL) {
191				rm = r;
192				if (rm->action == PF_NONAT ||
193				    rm->action == PF_NORDR ||
194				    rm->action == PF_NOBINAT) {
195					rm = NULL;
196				}
197				break;
198			} else
199				pf_step_into_anchor(anchor_stack, &asd,
200				    &ruleset, rs_num, &r, NULL, NULL);
201		}
202		if (r == NULL)
203			pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
204			    rs_num, &r, NULL, NULL);
205	}
206
207	if (tag > 0 && pf_tag_packet(m, pd, tag))
208		return (NULL);
209	if (rtableid >= 0)
210		M_SETFIB(m, rtableid);
211
212	return (rm);
213}
214
215static int
216pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
217    struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
218    uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
219    uint16_t high, struct pf_ksrc_node **sn)
220{
221	struct pf_state_key_cmp	key;
222	struct pf_addr		init_addr;
223
224	bzero(&init_addr, sizeof(init_addr));
225	if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn))
226		return (1);
227
228	bzero(&key, sizeof(key));
229	key.af = af;
230	key.proto = proto;
231	key.port[0] = dport;
232	PF_ACPY(&key.addr[0], daddr, key.af);
233
234	do {
235		PF_ACPY(&key.addr[1], naddr, key.af);
236
237		/*
238		 * port search; start random, step;
239		 * similar 2 portloop in in_pcbbind
240		 */
241		if (proto == IPPROTO_SCTP) {
242			key.port[1] = sport;
243			if (!pf_find_state_all_exists(&key, PF_IN)) {
244				*nport = sport;
245				return (0);
246			} else {
247				return (1); /* Fail mapping. */
248			}
249		} else if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
250		    proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
251			/*
252			 * XXX bug: icmp states don't use the id on both sides.
253			 * (traceroute -I through nat)
254			 */
255			key.port[1] = sport;
256			if (!pf_find_state_all_exists(&key, PF_IN)) {
257				*nport = sport;
258				return (0);
259			}
260		} else if (low == high) {
261			key.port[1] = htons(low);
262			if (!pf_find_state_all_exists(&key, PF_IN)) {
263				*nport = htons(low);
264				return (0);
265			}
266		} else {
267			uint32_t tmp;
268			uint16_t cut;
269
270			if (low > high) {
271				tmp = low;
272				low = high;
273				high = tmp;
274			}
275			/* low < high */
276			cut = arc4random() % (1 + high - low) + low;
277			/* low <= cut <= high */
278			for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
279				key.port[1] = htons(tmp);
280				if (!pf_find_state_all_exists(&key, PF_IN)) {
281					*nport = htons(tmp);
282					return (0);
283				}
284			}
285			tmp = cut;
286			for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
287				key.port[1] = htons(tmp);
288				if (!pf_find_state_all_exists(&key, PF_IN)) {
289					*nport = htons(tmp);
290					return (0);
291				}
292			}
293		}
294
295		switch (r->rpool.opts & PF_POOL_TYPEMASK) {
296		case PF_POOL_RANDOM:
297		case PF_POOL_ROUNDROBIN:
298			/*
299			 * pick a different source address since we're out
300			 * of free port choices for the current one.
301			 */
302			if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn))
303				return (1);
304			break;
305		case PF_POOL_NONE:
306		case PF_POOL_SRCHASH:
307		case PF_POOL_BITMASK:
308		default:
309			return (1);
310		}
311	} while (! PF_AEQ(&init_addr, naddr, af) );
312	return (1);					/* none available */
313}
314
315static int
316pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
317    struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
318    uint16_t dport, struct pf_addr *naddr, uint16_t *nport,
319    struct pf_ksrc_node **sn)
320{
321	uint16_t psmask, low, highmask;
322	uint16_t i, ahigh, cut;
323	int ashift, psidshift;
324
325	ashift = 16 - r->rpool.mape.offset;
326	psidshift = ashift - r->rpool.mape.psidlen;
327	psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1);
328	psmask = psmask << psidshift;
329	highmask = (1U << psidshift) - 1;
330
331	ahigh = (1U << r->rpool.mape.offset) - 1;
332	cut = arc4random() & ahigh;
333	if (cut == 0)
334		cut = 1;
335
336	for (i = cut; i <= ahigh; i++) {
337		low = (i << ashift) | psmask;
338		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
339		    naddr, nport, low, low | highmask, sn))
340			return (0);
341	}
342	for (i = cut - 1; i > 0; i--) {
343		low = (i << ashift) | psmask;
344		if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
345		    naddr, nport, low, low | highmask, sn))
346			return (0);
347	}
348	return (1);
349}
350
351u_short
352pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
353    struct pf_addr *naddr, struct pfi_kkif **nkif, struct pf_addr *init_addr,
354    struct pf_ksrc_node **sn)
355{
356	u_short			 reason = 0;
357	struct pf_kpool		*rpool = &r->rpool;
358	struct pf_addr		*raddr = NULL, *rmask = NULL;
359	struct pf_srchash	*sh = NULL;
360
361	/* Try to find a src_node if none was given and this
362	   is a sticky-address rule. */
363	if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
364	    (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
365		*sn = pf_find_src_node(saddr, r, af, &sh, false);
366
367	/* If a src_node was found or explicitly given and it has a non-zero
368	   route address, use this address. A zeroed address is found if the
369	   src node was created just a moment ago in pf_create_state and it
370	   needs to be filled in with routing decision calculated here. */
371	if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
372		/* If the supplied address is the same as the current one we've
373		 * been asked before, so tell the caller that there's no other
374		 * address to be had. */
375		if (PF_AEQ(naddr, &(*sn)->raddr, af)) {
376			reason = PFRES_MAPFAILED;
377			goto done;
378		}
379
380		PF_ACPY(naddr, &(*sn)->raddr, af);
381		if (nkif)
382			*nkif = (*sn)->rkif;
383		if (V_pf_status.debug >= PF_DEBUG_NOISY) {
384			printf("pf_map_addr: src tracking maps ");
385			pf_print_host(saddr, 0, af);
386			printf(" to ");
387			pf_print_host(naddr, 0, af);
388			if (nkif)
389				printf("@%s", (*nkif)->pfik_name);
390			printf("\n");
391		}
392		goto done;
393	}
394
395	mtx_lock(&rpool->mtx);
396	/* Find the route using chosen algorithm. Store the found route
397	   in src_node if it was given or found. */
398	if (rpool->cur->addr.type == PF_ADDR_NOROUTE) {
399		reason = PFRES_MAPFAILED;
400		goto done_pool_mtx;
401	}
402	if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
403		switch (af) {
404#ifdef INET
405		case AF_INET:
406			if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
407			    (rpool->opts & PF_POOL_TYPEMASK) !=
408			    PF_POOL_ROUNDROBIN) {
409				reason = PFRES_MAPFAILED;
410				goto done_pool_mtx;
411			}
412			raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
413			rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
414			break;
415#endif /* INET */
416#ifdef INET6
417		case AF_INET6:
418			if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
419			    (rpool->opts & PF_POOL_TYPEMASK) !=
420			    PF_POOL_ROUNDROBIN) {
421				reason = PFRES_MAPFAILED;
422				goto done_pool_mtx;
423			}
424			raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
425			rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
426			break;
427#endif /* INET6 */
428		}
429	} else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
430		if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN) {
431			reason = PFRES_MAPFAILED;
432			goto done_pool_mtx; /* unsupported */
433		}
434	} else {
435		raddr = &rpool->cur->addr.v.a.addr;
436		rmask = &rpool->cur->addr.v.a.mask;
437	}
438
439	switch (rpool->opts & PF_POOL_TYPEMASK) {
440	case PF_POOL_NONE:
441		PF_ACPY(naddr, raddr, af);
442		break;
443	case PF_POOL_BITMASK:
444		PF_POOLMASK(naddr, raddr, rmask, saddr, af);
445		break;
446	case PF_POOL_RANDOM:
447		if (init_addr != NULL && PF_AZERO(init_addr, af)) {
448			switch (af) {
449#ifdef INET
450			case AF_INET:
451				rpool->counter.addr32[0] = htonl(arc4random());
452				break;
453#endif /* INET */
454#ifdef INET6
455			case AF_INET6:
456				if (rmask->addr32[3] != 0xffffffff)
457					rpool->counter.addr32[3] =
458					    htonl(arc4random());
459				else
460					break;
461				if (rmask->addr32[2] != 0xffffffff)
462					rpool->counter.addr32[2] =
463					    htonl(arc4random());
464				else
465					break;
466				if (rmask->addr32[1] != 0xffffffff)
467					rpool->counter.addr32[1] =
468					    htonl(arc4random());
469				else
470					break;
471				if (rmask->addr32[0] != 0xffffffff)
472					rpool->counter.addr32[0] =
473					    htonl(arc4random());
474				break;
475#endif /* INET6 */
476			}
477			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
478			PF_ACPY(init_addr, naddr, af);
479
480		} else {
481			PF_AINC(&rpool->counter, af);
482			PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
483		}
484		break;
485	case PF_POOL_SRCHASH:
486	    {
487		unsigned char hash[16];
488
489		pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
490		PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
491		break;
492	    }
493	case PF_POOL_ROUNDROBIN:
494	    {
495		struct pf_kpooladdr *acur = rpool->cur;
496
497		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
498			if (!pfr_pool_get(rpool->cur->addr.p.tbl,
499			    &rpool->tblidx, &rpool->counter, af))
500				goto get_addr;
501		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
502			if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
503			    &rpool->tblidx, &rpool->counter, af))
504				goto get_addr;
505		} else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
506			goto get_addr;
507
508	try_next:
509		if (TAILQ_NEXT(rpool->cur, entries) == NULL)
510			rpool->cur = TAILQ_FIRST(&rpool->list);
511		else
512			rpool->cur = TAILQ_NEXT(rpool->cur, entries);
513		if (rpool->cur->addr.type == PF_ADDR_TABLE) {
514			rpool->tblidx = -1;
515			if (pfr_pool_get(rpool->cur->addr.p.tbl,
516			    &rpool->tblidx, &rpool->counter, af)) {
517				/* table contains no address of type 'af' */
518				if (rpool->cur != acur)
519					goto try_next;
520				reason = PFRES_MAPFAILED;
521				goto done_pool_mtx;
522			}
523		} else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
524			rpool->tblidx = -1;
525			if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
526			    &rpool->tblidx, &rpool->counter, af)) {
527				/* table contains no address of type 'af' */
528				if (rpool->cur != acur)
529					goto try_next;
530				reason = PFRES_MAPFAILED;
531				goto done_pool_mtx;
532			}
533		} else {
534			raddr = &rpool->cur->addr.v.a.addr;
535			rmask = &rpool->cur->addr.v.a.mask;
536			PF_ACPY(&rpool->counter, raddr, af);
537		}
538
539	get_addr:
540		PF_ACPY(naddr, &rpool->counter, af);
541		if (init_addr != NULL && PF_AZERO(init_addr, af))
542			PF_ACPY(init_addr, naddr, af);
543		PF_AINC(&rpool->counter, af);
544		break;
545	    }
546	}
547
548	if (nkif)
549		*nkif = rpool->cur->kif;
550
551	if (*sn != NULL) {
552		PF_ACPY(&(*sn)->raddr, naddr, af);
553		if (nkif)
554			(*sn)->rkif = *nkif;
555	}
556
557	if (V_pf_status.debug >= PF_DEBUG_NOISY &&
558	    (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
559		printf("pf_map_addr: selected address ");
560		pf_print_host(naddr, 0, af);
561		if (nkif)
562			printf("@%s", (*nkif)->pfik_name);
563		printf("\n");
564	}
565
566done_pool_mtx:
567	mtx_unlock(&rpool->mtx);
568
569done:
570	if (reason) {
571		counter_u64_add(V_pf_status.counters[reason], 1);
572	}
573
574	return (reason);
575}
576
577struct pf_krule *
578pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
579    struct pfi_kkif *kif, struct pf_ksrc_node **sn,
580    struct pf_state_key **skp, struct pf_state_key **nkp,
581    struct pf_addr *saddr, struct pf_addr *daddr,
582    uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack)
583{
584	struct pf_krule	*r = NULL;
585	struct pf_addr	*naddr;
586	uint16_t	*nport;
587	uint16_t	 low, high;
588
589	PF_RULES_RASSERT();
590	KASSERT(*skp == NULL, ("*skp not NULL"));
591	KASSERT(*nkp == NULL, ("*nkp not NULL"));
592
593	if (pd->dir == PF_OUT) {
594		r = pf_match_translation(pd, m, off, kif, saddr,
595		    sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
596		if (r == NULL)
597			r = pf_match_translation(pd, m, off, kif,
598			    saddr, sport, daddr, dport, PF_RULESET_NAT,
599			    anchor_stack);
600	} else {
601		r = pf_match_translation(pd, m, off, kif, saddr,
602		    sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
603		if (r == NULL)
604			r = pf_match_translation(pd, m, off, kif,
605			    saddr, sport, daddr, dport, PF_RULESET_BINAT,
606			    anchor_stack);
607	}
608
609	if (r == NULL)
610		return (NULL);
611
612	switch (r->action) {
613	case PF_NONAT:
614	case PF_NOBINAT:
615	case PF_NORDR:
616		return (NULL);
617	}
618
619	*skp = pf_state_key_setup(pd, saddr, daddr, sport, dport);
620	if (*skp == NULL)
621		return (NULL);
622	*nkp = pf_state_key_clone(*skp);
623	if (*nkp == NULL) {
624		uma_zfree(V_pf_state_key_z, *skp);
625		*skp = NULL;
626		return (NULL);
627	}
628
629	/* XXX We only modify one side for now. */
630	naddr = &(*nkp)->addr[1];
631	nport = &(*nkp)->port[1];
632
633	switch (r->action) {
634	case PF_NAT:
635		if (pd->proto == IPPROTO_ICMP) {
636			low  = 1;
637			high = 65535;
638		} else {
639			low  = r->rpool.proxy_port[0];
640			high = r->rpool.proxy_port[1];
641		}
642		if (r->rpool.mape.offset > 0) {
643			if (pf_get_mape_sport(pd->af, pd->proto, r, saddr,
644			    sport, daddr, dport, naddr, nport, sn)) {
645				DPFPRINTF(PF_DEBUG_MISC,
646				    ("pf: MAP-E port allocation (%u/%u/%u)"
647				    " failed\n",
648				    r->rpool.mape.offset,
649				    r->rpool.mape.psidlen,
650				    r->rpool.mape.psid));
651				goto notrans;
652			}
653		} else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport,
654		    daddr, dport, naddr, nport, low, high, sn)) {
655			DPFPRINTF(PF_DEBUG_MISC,
656			    ("pf: NAT proxy port allocation (%u-%u) failed\n",
657			    r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
658			goto notrans;
659		}
660		break;
661	case PF_BINAT:
662		switch (pd->dir) {
663		case PF_OUT:
664			if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
665				switch (pd->af) {
666#ifdef INET
667				case AF_INET:
668					if (r->rpool.cur->addr.p.dyn->
669					    pfid_acnt4 < 1)
670						goto notrans;
671					PF_POOLMASK(naddr,
672					    &r->rpool.cur->addr.p.dyn->
673					    pfid_addr4,
674					    &r->rpool.cur->addr.p.dyn->
675					    pfid_mask4, saddr, AF_INET);
676					break;
677#endif /* INET */
678#ifdef INET6
679				case AF_INET6:
680					if (r->rpool.cur->addr.p.dyn->
681					    pfid_acnt6 < 1)
682						goto notrans;
683					PF_POOLMASK(naddr,
684					    &r->rpool.cur->addr.p.dyn->
685					    pfid_addr6,
686					    &r->rpool.cur->addr.p.dyn->
687					    pfid_mask6, saddr, AF_INET6);
688					break;
689#endif /* INET6 */
690				}
691			} else
692				PF_POOLMASK(naddr,
693				    &r->rpool.cur->addr.v.a.addr,
694				    &r->rpool.cur->addr.v.a.mask, saddr,
695				    pd->af);
696			break;
697		case PF_IN:
698			if (r->src.addr.type == PF_ADDR_DYNIFTL) {
699				switch (pd->af) {
700#ifdef INET
701				case AF_INET:
702					if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
703						goto notrans;
704					PF_POOLMASK(naddr,
705					    &r->src.addr.p.dyn->pfid_addr4,
706					    &r->src.addr.p.dyn->pfid_mask4,
707					    daddr, AF_INET);
708					break;
709#endif /* INET */
710#ifdef INET6
711				case AF_INET6:
712					if (r->src.addr.p.dyn->pfid_acnt6 < 1)
713						goto notrans;
714					PF_POOLMASK(naddr,
715					    &r->src.addr.p.dyn->pfid_addr6,
716					    &r->src.addr.p.dyn->pfid_mask6,
717					    daddr, AF_INET6);
718					break;
719#endif /* INET6 */
720				}
721			} else
722				PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
723				    &r->src.addr.v.a.mask, daddr, pd->af);
724			break;
725		}
726		break;
727	case PF_RDR: {
728		if (pf_map_addr(pd->af, r, saddr, naddr, NULL, NULL, sn))
729			goto notrans;
730		if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
731			PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
732			    daddr, pd->af);
733
734		/* Do not change SCTP ports. */
735		if (pd->proto == IPPROTO_SCTP)
736			break;
737
738		if (r->rpool.proxy_port[1]) {
739			uint32_t	tmp_nport;
740
741			tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
742			    (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
743			    1)) + r->rpool.proxy_port[0];
744
745			/* Wrap around if necessary. */
746			if (tmp_nport > 65535)
747				tmp_nport -= 65535;
748			*nport = htons((uint16_t)tmp_nport);
749		} else if (r->rpool.proxy_port[0])
750			*nport = htons(r->rpool.proxy_port[0]);
751		break;
752	}
753	default:
754		panic("%s: unknown action %u", __func__, r->action);
755	}
756
757	/* Return success only if translation really happened. */
758	if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
759		return (r);
760
761notrans:
762	uma_zfree(V_pf_state_key_z, *nkp);
763	uma_zfree(V_pf_state_key_z, *skp);
764	*skp = *nkp = NULL;
765	*sn = NULL;
766
767	return (NULL);
768}
769