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 <crypto/siphash/siphash.h>
51
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/vnet.h>
55 #include <net/pfvar.h>
56 #include <net/if_pflog.h>
57
58 #ifdef INET
59 #include <netinet/in_var.h>
60 #endif /* INET */
61
62 #ifdef INET6
63 #include <netinet6/in6_var.h>
64 #endif /* INET6 */
65
66
67 /*
68 * Limit the amount of work we do to find a free source port for redirects that
69 * introduce a state conflict.
70 */
71 #define V_pf_rdr_srcport_rewrite_tries VNET(pf_rdr_srcport_rewrite_tries)
72 VNET_DEFINE_STATIC(int, pf_rdr_srcport_rewrite_tries) = 16;
73
74 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
75
76 static uint64_t pf_hash(struct pf_addr *, struct pf_addr *,
77 struct pf_poolhashkey *, sa_family_t);
78 struct pf_krule *pf_match_translation(int, struct pf_test_ctx *);
79 static enum pf_test_status pf_step_into_translation_anchor(int, struct pf_test_ctx *,
80 struct pf_krule *);
81 static int pf_get_sport(struct pf_pdesc *, struct pf_krule *,
82 struct pf_addr *, uint16_t *, uint16_t, uint16_t,
83 struct pf_ksrc_node **, struct pf_srchash **,
84 struct pf_kpool *, struct pf_udp_mapping **,
85 pf_sn_types_t);
86 static bool pf_islinklocal(const sa_family_t, const struct pf_addr *);
87
88 static uint64_t
pf_hash(struct pf_addr * inaddr,struct pf_addr * hash,struct pf_poolhashkey * key,sa_family_t af)89 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
90 struct pf_poolhashkey *key, sa_family_t af)
91 {
92 SIPHASH_CTX ctx;
93 #ifdef INET6
94 union {
95 uint64_t hash64;
96 uint32_t hash32[2];
97 } h;
98 #endif /* INET6 */
99 uint64_t res = 0;
100
101 _Static_assert(sizeof(*key) >= SIPHASH_KEY_LENGTH, "");
102
103 switch (af) {
104 #ifdef INET
105 case AF_INET:
106 res = SipHash24(&ctx, (const uint8_t *)key,
107 &inaddr->addr32[0], sizeof(inaddr->addr32[0]));
108 hash->addr32[0] = res;
109 break;
110 #endif /* INET */
111 #ifdef INET6
112 case AF_INET6:
113 res = SipHash24(&ctx, (const uint8_t *)key,
114 &inaddr->addr32[0], 4 * sizeof(inaddr->addr32[0]));
115 h.hash64 = res;
116 hash->addr32[0] = h.hash32[0];
117 hash->addr32[1] = h.hash32[1];
118 /*
119 * siphash isn't big enough, but flipping it around is
120 * good enough here.
121 */
122 hash->addr32[2] = ~h.hash32[1];
123 hash->addr32[3] = ~h.hash32[0];
124 break;
125 #endif /* INET6 */
126 default:
127 unhandled_af(af);
128 }
129 return (res);
130 }
131
132 #define PF_TEST_ATTRIB(t, a) \
133 if (t) { \
134 r = a; \
135 continue; \
136 } else do { \
137 } while (0)
138
139 static enum pf_test_status
pf_match_translation_rule(int rs_num,struct pf_test_ctx * ctx,struct pf_kruleset * ruleset)140 pf_match_translation_rule(int rs_num, struct pf_test_ctx *ctx, struct pf_kruleset *ruleset)
141 {
142 struct pf_krule *r;
143 struct pf_pdesc *pd = ctx->pd;
144 int rtableid = -1;
145
146 r = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
147 while (r != NULL) {
148 struct pf_rule_addr *src = NULL, *dst = NULL;
149 struct pf_addr_wrap *xdst = NULL;
150
151 if (r->action == PF_BINAT && pd->dir == PF_IN) {
152 src = &r->dst;
153 if (r->rdr.cur != NULL)
154 xdst = &r->rdr.cur->addr;
155 } else {
156 src = &r->src;
157 dst = &r->dst;
158 }
159
160 pf_counter_u64_add(&r->evaluations, 1);
161 PF_TEST_ATTRIB(pfi_kkif_match(r->kif, pd->kif) == r->ifnot,
162 r->skip[PF_SKIP_IFP]);
163 PF_TEST_ATTRIB(r->direction && r->direction != pd->dir,
164 r->skip[PF_SKIP_DIR]);
165 PF_TEST_ATTRIB(r->af && r->af != pd->af,
166 r->skip[PF_SKIP_AF]);
167 PF_TEST_ATTRIB(r->proto && r->proto != pd->proto,
168 r->skip[PF_SKIP_PROTO]);
169 PF_TEST_ATTRIB(PF_MISMATCHAW(&src->addr, &pd->nsaddr, pd->af,
170 src->neg, pd->kif, M_GETFIB(pd->m)),
171 r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
172 PF_SKIP_DST_ADDR]);
173 PF_TEST_ATTRIB(src->port_op && !pf_match_port(src->port_op,
174 src->port[0], src->port[1], pd->nsport),
175 r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
176 PF_SKIP_DST_PORT]);
177 PF_TEST_ATTRIB(dst != NULL &&
178 PF_MISMATCHAW(&dst->addr, &pd->ndaddr, pd->af, dst->neg, NULL,
179 M_GETFIB(pd->m)),
180 r->skip[PF_SKIP_DST_ADDR]);
181 PF_TEST_ATTRIB(xdst != NULL && PF_MISMATCHAW(xdst, &pd->ndaddr, pd->af,
182 0, NULL, M_GETFIB(pd->m)),
183 TAILQ_NEXT(r, entries));
184 PF_TEST_ATTRIB(dst != NULL && dst->port_op &&
185 !pf_match_port(dst->port_op, dst->port[0],
186 dst->port[1], pd->ndport),
187 r->skip[PF_SKIP_DST_PORT]);
188 PF_TEST_ATTRIB(r->match_tag && !pf_match_tag(pd->m, r, &ctx->tag,
189 pd->pf_mtag ? pd->pf_mtag->tag : 0),
190 TAILQ_NEXT(r, entries));
191 PF_TEST_ATTRIB(r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
192 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd,
193 &pd->hdr.tcp), r->os_fingerprint)),
194 TAILQ_NEXT(r, entries));
195 if (r->tag)
196 ctx->tag = r->tag;
197 if (r->rtableid >= 0)
198 rtableid = r->rtableid;
199 if (r->anchor == NULL) {
200 if (r->action == PF_NONAT ||
201 r->action == PF_NORDR ||
202 r->action == PF_NOBINAT) {
203 *ctx->rm = NULL;
204 } else {
205 /*
206 * found matching r
207 */
208 ctx->tr = r;
209 /*
210 * anchor, with ruleset, where r belongs to
211 */
212 *ctx->am = ctx->a;
213 /*
214 * ruleset where r belongs to
215 */
216 *ctx->rsm = ruleset;
217 /*
218 * ruleset, where anchor belongs to.
219 */
220 ctx->arsm = ctx->aruleset;
221 }
222 } else {
223 ctx->a = r; /* remember anchor */
224 ctx->aruleset = ruleset; /* and its ruleset */
225 if (pf_step_into_translation_anchor(rs_num, ctx,
226 r) != PF_TEST_OK) {
227 break;
228 }
229 }
230 r = TAILQ_NEXT(r, entries);
231 }
232
233 if (ctx->tag > 0 && pf_tag_packet(pd, ctx->tag))
234 return (PF_TEST_FAIL);
235 if (rtableid >= 0)
236 M_SETFIB(pd->m, rtableid);
237
238 return (PF_TEST_OK);
239 }
240
241 static enum pf_test_status
pf_step_into_translation_anchor(int rs_num,struct pf_test_ctx * ctx,struct pf_krule * r)242 pf_step_into_translation_anchor(int rs_num, struct pf_test_ctx *ctx, struct pf_krule *r)
243 {
244 enum pf_test_status rv;
245
246 PF_RULES_RASSERT();
247
248 if (ctx->depth >= PF_ANCHOR_STACK_MAX) {
249 printf("%s: anchor stack overflow on %s\n",
250 __func__, r->anchor->name);
251 return (PF_TEST_FAIL);
252 }
253
254 ctx->depth++;
255
256 if (r->anchor_wildcard) {
257 struct pf_kanchor *child;
258 rv = PF_TEST_OK;
259 RB_FOREACH(child, pf_kanchor_node, &r->anchor->children) {
260 rv = pf_match_translation_rule(rs_num, ctx, &child->ruleset);
261 if ((rv == PF_TEST_QUICK) || (rv == PF_TEST_FAIL)) {
262 /*
263 * we either hit a rule qith quick action
264 * (more likely), or hit some runtime
265 * error (e.g. pool_get() faillure).
266 */
267 break;
268 }
269 }
270 } else {
271 rv = pf_match_translation_rule(rs_num, ctx, &r->anchor->ruleset);
272 }
273
274 ctx->depth--;
275
276 return (rv);
277 }
278
279 struct pf_krule *
pf_match_translation(int rs_num,struct pf_test_ctx * ctx)280 pf_match_translation(int rs_num, struct pf_test_ctx *ctx)
281 {
282 enum pf_test_status rv;
283
284 MPASS(ctx->depth == 0);
285 rv = pf_match_translation_rule(rs_num, ctx, &pf_main_ruleset);
286 MPASS(ctx->depth == 0);
287 if (rv != PF_TEST_OK)
288 return (NULL);
289
290 return (ctx->tr);
291 }
292
293 static int
pf_get_sport(struct pf_pdesc * pd,struct pf_krule * r,struct pf_addr * naddr,uint16_t * nport,uint16_t low,uint16_t high,struct pf_ksrc_node ** sn,struct pf_srchash ** sh,struct pf_kpool * rpool,struct pf_udp_mapping ** udp_mapping,pf_sn_types_t sn_type)294 pf_get_sport(struct pf_pdesc *pd, struct pf_krule *r,
295 struct pf_addr *naddr, uint16_t *nport, uint16_t low,
296 uint16_t high, struct pf_ksrc_node **sn,
297 struct pf_srchash **sh, struct pf_kpool *rpool,
298 struct pf_udp_mapping **udp_mapping, pf_sn_types_t sn_type)
299 {
300 struct pf_state_key_cmp key;
301 struct pf_addr init_addr;
302 int dir = (pd->dir == PF_IN) ? PF_OUT : PF_IN;
303 int sidx = pd->sidx;
304 int didx = pd->didx;
305
306 bzero(&init_addr, sizeof(init_addr));
307
308 if (udp_mapping) {
309 MPASS(*udp_mapping == NULL);
310 }
311
312 /*
313 * If we are UDP and have an existing mapping we can get source port
314 * from the mapping. In this case we have to look up the src_node as
315 * pf_map_addr would.
316 */
317 if (pd->proto == IPPROTO_UDP && (rpool->opts & PF_POOL_ENDPI)) {
318 struct pf_udp_endpoint_cmp udp_source;
319
320 bzero(&udp_source, sizeof(udp_source));
321 udp_source.af = pd->af;
322 PF_ACPY(&udp_source.addr, &pd->nsaddr, pd->af);
323 udp_source.port = pd->nsport;
324 if (udp_mapping) {
325 *udp_mapping = pf_udp_mapping_find(&udp_source);
326 if (*udp_mapping) {
327 PF_ACPY(naddr, &(*udp_mapping)->endpoints[1].addr, pd->af);
328 *nport = (*udp_mapping)->endpoints[1].port;
329 /* Try to find a src_node as per pf_map_addr(). */
330 if (*sn == NULL && rpool->opts & PF_POOL_STICKYADDR &&
331 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
332 *sn = pf_find_src_node(&pd->nsaddr, r,
333 pd->af, sh, sn_type, false);
334 if (*sn != NULL)
335 PF_SRC_NODE_UNLOCK(*sn);
336 return (0);
337 } else {
338 *udp_mapping = pf_udp_mapping_create(pd->af, &pd->nsaddr,
339 pd->nsport, &init_addr, 0);
340 if (*udp_mapping == NULL)
341 return (1);
342 }
343 }
344 }
345
346 if (pf_map_addr_sn(pd->naf, r, &pd->nsaddr, naddr, NULL, &init_addr,
347 sn, sh, rpool, sn_type))
348 goto failed;
349
350 if (pd->proto == IPPROTO_ICMP) {
351 if (pd->ndport == htons(ICMP_ECHO)) {
352 low = 1;
353 high = 65535;
354 } else
355 return (0); /* Don't try to modify non-echo ICMP */
356 }
357 #ifdef INET6
358 if (pd->proto == IPPROTO_ICMPV6) {
359 if (pd->ndport == htons(ICMP6_ECHO_REQUEST)) {
360 low = 1;
361 high = 65535;
362 } else
363 return (0); /* Don't try to modify non-echo ICMP */
364 }
365 #endif /* INET6 */
366
367 bzero(&key, sizeof(key));
368 key.af = pd->naf;
369 key.proto = pd->proto;
370
371 do {
372 PF_ACPY(&key.addr[didx], &pd->ndaddr, key.af);
373 PF_ACPY(&key.addr[sidx], naddr, key.af);
374 key.port[didx] = pd->ndport;
375
376 if (udp_mapping && *udp_mapping)
377 PF_ACPY(&(*udp_mapping)->endpoints[1].addr, naddr, pd->af);
378
379 /*
380 * port search; start random, step;
381 * similar 2 portloop in in_pcbbind
382 */
383 if (pd->proto == IPPROTO_SCTP) {
384 key.port[sidx] = pd->nsport;
385 if (!pf_find_state_all_exists(&key, dir)) {
386 *nport = pd->nsport;
387 return (0);
388 } else {
389 return (1); /* Fail mapping. */
390 }
391 } else if (!(pd->proto == IPPROTO_TCP || pd->proto == IPPROTO_UDP ||
392 pd->proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
393 /*
394 * XXX bug: icmp states don't use the id on both sides.
395 * (traceroute -I through nat)
396 */
397 key.port[sidx] = pd->nsport;
398 if (!pf_find_state_all_exists(&key, dir)) {
399 *nport = pd->nsport;
400 return (0);
401 }
402 } else if (low == high) {
403 key.port[sidx] = htons(low);
404 if (!pf_find_state_all_exists(&key, dir)) {
405 if (udp_mapping && *udp_mapping != NULL) {
406 (*udp_mapping)->endpoints[1].port = htons(low);
407 if (pf_udp_mapping_insert(*udp_mapping) == 0) {
408 *nport = htons(low);
409 return (0);
410 }
411 } else {
412 *nport = htons(low);
413 return (0);
414 }
415 }
416 } else {
417 uint32_t tmp;
418 uint16_t cut;
419
420 if (low > high) {
421 tmp = low;
422 low = high;
423 high = tmp;
424 }
425 /* low < high */
426 cut = arc4random() % (1 + high - low) + low;
427 /* low <= cut <= high */
428 for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
429 if (udp_mapping && *udp_mapping != NULL) {
430 (*udp_mapping)->endpoints[sidx].port = htons(tmp);
431 if (pf_udp_mapping_insert(*udp_mapping) == 0) {
432 *nport = htons(tmp);
433 return (0);
434 }
435 } else {
436 key.port[sidx] = htons(tmp);
437 if (!pf_find_state_all_exists(&key, dir)) {
438 *nport = htons(tmp);
439 return (0);
440 }
441 }
442 }
443 tmp = cut;
444 for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
445 if (pd->proto == IPPROTO_UDP &&
446 (rpool->opts & PF_POOL_ENDPI &&
447 udp_mapping != NULL)) {
448 (*udp_mapping)->endpoints[1].port = htons(tmp);
449 if (pf_udp_mapping_insert(*udp_mapping) == 0) {
450 *nport = htons(tmp);
451 return (0);
452 }
453 } else {
454 key.port[sidx] = htons(tmp);
455 if (!pf_find_state_all_exists(&key, dir)) {
456 *nport = htons(tmp);
457 return (0);
458 }
459 }
460 }
461 }
462
463 switch (rpool->opts & PF_POOL_TYPEMASK) {
464 case PF_POOL_RANDOM:
465 case PF_POOL_ROUNDROBIN:
466 /*
467 * pick a different source address since we're out
468 * of free port choices for the current one.
469 */
470 (*sn) = NULL;
471 if (pf_map_addr_sn(pd->naf, r, &pd->nsaddr, naddr, NULL,
472 &init_addr, sn, sh, rpool, sn_type))
473 return (1);
474 break;
475 case PF_POOL_NONE:
476 case PF_POOL_SRCHASH:
477 case PF_POOL_BITMASK:
478 default:
479 return (1);
480 }
481 } while (! PF_AEQ(&init_addr, naddr, pd->naf) );
482
483 failed:
484 if (udp_mapping) {
485 uma_zfree(V_pf_udp_mapping_z, *udp_mapping);
486 *udp_mapping = NULL;
487 }
488
489 return (1); /* none available */
490 }
491
492 static bool
pf_islinklocal(const sa_family_t af,const struct pf_addr * addr)493 pf_islinklocal(const sa_family_t af, const struct pf_addr *addr)
494 {
495 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&addr->v6))
496 return (true);
497 return (false);
498 }
499
500 static int
pf_get_mape_sport(struct pf_pdesc * pd,struct pf_krule * r,struct pf_addr * naddr,uint16_t * nport,struct pf_ksrc_node ** sn,struct pf_srchash ** sh,struct pf_udp_mapping ** udp_mapping,struct pf_kpool * rpool)501 pf_get_mape_sport(struct pf_pdesc *pd, struct pf_krule *r,
502 struct pf_addr *naddr, uint16_t *nport,
503 struct pf_ksrc_node **sn, struct pf_srchash **sh,
504 struct pf_udp_mapping **udp_mapping, struct pf_kpool *rpool)
505 {
506 uint16_t psmask, low, highmask;
507 uint16_t i, ahigh, cut;
508 int ashift, psidshift;
509
510 ashift = 16 - rpool->mape.offset;
511 psidshift = ashift - rpool->mape.psidlen;
512 psmask = rpool->mape.psid & ((1U << rpool->mape.psidlen) - 1);
513 psmask = psmask << psidshift;
514 highmask = (1U << psidshift) - 1;
515
516 ahigh = (1U << rpool->mape.offset) - 1;
517 cut = arc4random() & ahigh;
518 if (cut == 0)
519 cut = 1;
520
521 for (i = cut; i <= ahigh; i++) {
522 low = (i << ashift) | psmask;
523 if (!pf_get_sport(pd, r,
524 naddr, nport, low, low | highmask, sn, sh, rpool,
525 udp_mapping, PF_SN_NAT))
526 return (0);
527 }
528 for (i = cut - 1; i > 0; i--) {
529 low = (i << ashift) | psmask;
530 if (!pf_get_sport(pd, r,
531 naddr, nport, low, low | highmask, sn, sh, rpool,
532 udp_mapping, PF_SN_NAT))
533 return (0);
534 }
535 return (1);
536 }
537
538 u_short
pf_map_addr(sa_family_t af,struct pf_krule * r,struct pf_addr * saddr,struct pf_addr * naddr,struct pfi_kkif ** nkif,struct pf_addr * init_addr,struct pf_kpool * rpool)539 pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
540 struct pf_addr *naddr, struct pfi_kkif **nkif, struct pf_addr *init_addr,
541 struct pf_kpool *rpool)
542 {
543 u_short reason = PFRES_MATCH;
544 struct pf_addr *raddr = NULL, *rmask = NULL;
545 uint64_t hashidx;
546 int cnt;
547
548 mtx_lock(&rpool->mtx);
549 /* Find the route using chosen algorithm. Store the found route
550 in src_node if it was given or found. */
551 if (rpool->cur->addr.type == PF_ADDR_NOROUTE) {
552 reason = PFRES_MAPFAILED;
553 goto done_pool_mtx;
554 }
555 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
556 switch (af) {
557 #ifdef INET
558 case AF_INET:
559 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
560 !PF_POOL_DYNTYPE(rpool->opts)) {
561 reason = PFRES_MAPFAILED;
562 goto done_pool_mtx;
563 }
564 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
565 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
566 break;
567 #endif /* INET */
568 #ifdef INET6
569 case AF_INET6:
570 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
571 !PF_POOL_DYNTYPE(rpool->opts)) {
572 reason = PFRES_MAPFAILED;
573 goto done_pool_mtx;
574 }
575 raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
576 rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
577 break;
578 #endif /* INET6 */
579 default:
580 unhandled_af(af);
581 }
582 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
583 if (!PF_POOL_DYNTYPE(rpool->opts)) {
584 reason = PFRES_MAPFAILED;
585 goto done_pool_mtx; /* unsupported */
586 }
587 } else {
588 raddr = &rpool->cur->addr.v.a.addr;
589 rmask = &rpool->cur->addr.v.a.mask;
590 }
591
592 switch (rpool->opts & PF_POOL_TYPEMASK) {
593 case PF_POOL_NONE:
594 PF_ACPY(naddr, raddr, af);
595 break;
596 case PF_POOL_BITMASK:
597 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
598 break;
599 case PF_POOL_RANDOM:
600 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
601 cnt = rpool->cur->addr.p.tbl->pfrkt_cnt;
602 if (cnt == 0)
603 rpool->tblidx = 0;
604 else
605 rpool->tblidx = (int)arc4random_uniform(cnt);
606 memset(&rpool->counter, 0, sizeof(rpool->counter));
607 if (pfr_pool_get(rpool->cur->addr.p.tbl,
608 &rpool->tblidx, &rpool->counter, af, NULL)) {
609 reason = PFRES_MAPFAILED;
610 goto done_pool_mtx; /* unsupported */
611 }
612 PF_ACPY(naddr, &rpool->counter, af);
613 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
614 cnt = rpool->cur->addr.p.dyn->pfid_kt->pfrkt_cnt;
615 if (cnt == 0)
616 rpool->tblidx = 0;
617 else
618 rpool->tblidx = (int)arc4random_uniform(cnt);
619 memset(&rpool->counter, 0, sizeof(rpool->counter));
620 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
621 &rpool->tblidx, &rpool->counter, af,
622 pf_islinklocal)) {
623 reason = PFRES_MAPFAILED;
624 goto done_pool_mtx; /* unsupported */
625 }
626 PF_ACPY(naddr, &rpool->counter, af);
627 } else if (init_addr != NULL && PF_AZERO(init_addr, af)) {
628 switch (af) {
629 #ifdef INET
630 case AF_INET:
631 rpool->counter.addr32[0] = arc4random();
632 break;
633 #endif /* INET */
634 #ifdef INET6
635 case AF_INET6:
636 if (rmask->addr32[3] != 0xffffffff)
637 rpool->counter.addr32[3] =
638 arc4random();
639 else
640 break;
641 if (rmask->addr32[2] != 0xffffffff)
642 rpool->counter.addr32[2] =
643 arc4random();
644 else
645 break;
646 if (rmask->addr32[1] != 0xffffffff)
647 rpool->counter.addr32[1] =
648 arc4random();
649 else
650 break;
651 if (rmask->addr32[0] != 0xffffffff)
652 rpool->counter.addr32[0] =
653 arc4random();
654 break;
655 #endif /* INET6 */
656 }
657 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
658 PF_ACPY(init_addr, naddr, af);
659
660 } else {
661 PF_AINC(&rpool->counter, af);
662 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
663 }
664 break;
665 case PF_POOL_SRCHASH:
666 {
667 unsigned char hash[16];
668
669 hashidx =
670 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
671 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
672 cnt = rpool->cur->addr.p.tbl->pfrkt_cnt;
673 if (cnt == 0)
674 rpool->tblidx = 0;
675 else
676 rpool->tblidx = (int)(hashidx % cnt);
677 memset(&rpool->counter, 0, sizeof(rpool->counter));
678 if (pfr_pool_get(rpool->cur->addr.p.tbl,
679 &rpool->tblidx, &rpool->counter, af, NULL)) {
680 reason = PFRES_MAPFAILED;
681 goto done_pool_mtx; /* unsupported */
682 }
683 PF_ACPY(naddr, &rpool->counter, af);
684 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
685 cnt = rpool->cur->addr.p.dyn->pfid_kt->pfrkt_cnt;
686 if (cnt == 0)
687 rpool->tblidx = 0;
688 else
689 rpool->tblidx = (int)(hashidx % cnt);
690 memset(&rpool->counter, 0, sizeof(rpool->counter));
691 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
692 &rpool->tblidx, &rpool->counter, af,
693 pf_islinklocal)) {
694 reason = PFRES_MAPFAILED;
695 goto done_pool_mtx; /* unsupported */
696 }
697 PF_ACPY(naddr, &rpool->counter, af);
698 } else {
699 PF_POOLMASK(naddr, raddr, rmask,
700 (struct pf_addr *)&hash, af);
701 }
702 break;
703 }
704 case PF_POOL_ROUNDROBIN:
705 {
706 struct pf_kpooladdr *acur = rpool->cur;
707
708 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
709 if (!pfr_pool_get(rpool->cur->addr.p.tbl,
710 &rpool->tblidx, &rpool->counter, af, NULL))
711 goto get_addr;
712 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
713 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
714 &rpool->tblidx, &rpool->counter, af, pf_islinklocal))
715 goto get_addr;
716 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
717 goto get_addr;
718
719 try_next:
720 if (TAILQ_NEXT(rpool->cur, entries) == NULL)
721 rpool->cur = TAILQ_FIRST(&rpool->list);
722 else
723 rpool->cur = TAILQ_NEXT(rpool->cur, entries);
724 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
725 if (pfr_pool_get(rpool->cur->addr.p.tbl,
726 &rpool->tblidx, &rpool->counter, af, NULL)) {
727 /* table contains no address of type 'af' */
728 if (rpool->cur != acur)
729 goto try_next;
730 reason = PFRES_MAPFAILED;
731 goto done_pool_mtx;
732 }
733 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
734 rpool->tblidx = -1;
735 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
736 &rpool->tblidx, &rpool->counter, af, pf_islinklocal)) {
737 /* table contains no address of type 'af' */
738 if (rpool->cur != acur)
739 goto try_next;
740 reason = PFRES_MAPFAILED;
741 goto done_pool_mtx;
742 }
743 } else {
744 raddr = &rpool->cur->addr.v.a.addr;
745 rmask = &rpool->cur->addr.v.a.mask;
746 PF_ACPY(&rpool->counter, raddr, af);
747 }
748
749 get_addr:
750 PF_ACPY(naddr, &rpool->counter, af);
751 if (init_addr != NULL && PF_AZERO(init_addr, af))
752 PF_ACPY(init_addr, naddr, af);
753 PF_AINC(&rpool->counter, af);
754 break;
755 }
756 }
757
758 if (nkif)
759 *nkif = rpool->cur->kif;
760
761 done_pool_mtx:
762 mtx_unlock(&rpool->mtx);
763
764 if (reason) {
765 counter_u64_add(V_pf_status.counters[reason], 1);
766 }
767
768 return (reason);
769 }
770
771 u_short
pf_map_addr_sn(sa_family_t af,struct pf_krule * r,struct pf_addr * saddr,struct pf_addr * naddr,struct pfi_kkif ** nkif,struct pf_addr * init_addr,struct pf_ksrc_node ** sn,struct pf_srchash ** sh,struct pf_kpool * rpool,pf_sn_types_t sn_type)772 pf_map_addr_sn(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
773 struct pf_addr *naddr, struct pfi_kkif **nkif, struct pf_addr *init_addr,
774 struct pf_ksrc_node **sn, struct pf_srchash **sh, struct pf_kpool *rpool,
775 pf_sn_types_t sn_type)
776 {
777 u_short reason = 0;
778
779 KASSERT(*sn == NULL, ("*sn not NULL"));
780
781 /*
782 * If this is a sticky-address rule, try to find an existing src_node.
783 * Request the sh to be unlocked if sn was not found, as we never
784 * insert a new sn when parsing the ruleset.
785 */
786 if (rpool->opts & PF_POOL_STICKYADDR &&
787 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
788 *sn = pf_find_src_node(saddr, r, af, sh, sn_type, false);
789
790 if (*sn != NULL) {
791 PF_SRC_NODE_LOCK_ASSERT(*sn);
792
793 /* If the supplied address is the same as the current one we've
794 * been asked before, so tell the caller that there's no other
795 * address to be had. */
796 if (PF_AEQ(naddr, &(*sn)->raddr, af)) {
797 reason = PFRES_MAPFAILED;
798 goto done;
799 }
800
801 PF_ACPY(naddr, &(*sn)->raddr, af);
802 if (nkif)
803 *nkif = (*sn)->rkif;
804 if (V_pf_status.debug >= PF_DEBUG_NOISY) {
805 printf("pf_map_addr: src tracking maps ");
806 pf_print_host(saddr, 0, af);
807 printf(" to ");
808 pf_print_host(naddr, 0, af);
809 if (nkif)
810 printf("@%s", (*nkif)->pfik_name);
811 printf("\n");
812 }
813 goto done;
814 }
815
816 /*
817 * Source node has not been found. Find a new address and store it
818 * in variables given by the caller.
819 */
820 if (pf_map_addr(af, r, saddr, naddr, nkif, init_addr, rpool) != 0) {
821 /* pf_map_addr() sets reason counters on its own */
822 goto done;
823 }
824
825 if (V_pf_status.debug >= PF_DEBUG_NOISY &&
826 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
827 printf("pf_map_addr: selected address ");
828 pf_print_host(naddr, 0, af);
829 if (nkif)
830 printf("@%s", (*nkif)->pfik_name);
831 printf("\n");
832 }
833
834 done:
835 if ((*sn) != NULL)
836 PF_SRC_NODE_UNLOCK(*sn);
837
838 if (reason) {
839 counter_u64_add(V_pf_status.counters[reason], 1);
840 }
841
842 return (reason);
843 }
844
845 u_short
pf_get_translation(struct pf_test_ctx * ctx)846 pf_get_translation(struct pf_test_ctx *ctx)
847 {
848 struct pf_krule *r = NULL;
849 u_short transerror;
850
851 PF_RULES_RASSERT();
852 KASSERT(ctx->sk == NULL, ("*skp not NULL"));
853 KASSERT(ctx->nk == NULL, ("*nkp not NULL"));
854
855 ctx->nr = NULL;
856
857 if (ctx->pd->dir == PF_OUT) {
858 r = pf_match_translation(PF_RULESET_BINAT, ctx);
859 if (r == NULL)
860 r = pf_match_translation(PF_RULESET_NAT, ctx);
861 } else {
862 r = pf_match_translation(PF_RULESET_RDR, ctx);
863 if (r == NULL)
864 r = pf_match_translation(PF_RULESET_BINAT, ctx);
865 }
866
867 if (r == NULL)
868 return (PFRES_MAX);
869
870 switch (r->action) {
871 case PF_NONAT:
872 case PF_NOBINAT:
873 case PF_NORDR:
874 return (PFRES_MAX);
875 }
876
877 transerror = pf_get_transaddr(ctx, r, r->action, &(r->rdr));
878 if (transerror == PFRES_MATCH)
879 ctx->nr = r;
880
881 return (transerror);
882 }
883
884 u_short
pf_get_transaddr(struct pf_test_ctx * ctx,struct pf_krule * r,uint8_t nat_action,struct pf_kpool * rpool)885 pf_get_transaddr(struct pf_test_ctx *ctx, struct pf_krule *r,
886 uint8_t nat_action, struct pf_kpool *rpool)
887 {
888 struct pf_pdesc *pd = ctx->pd;
889 struct pf_addr *naddr;
890 struct pf_ksrc_node *sn = NULL;
891 struct pf_srchash *sh = NULL;
892 uint16_t *nportp;
893 uint16_t low, high;
894 u_short reason;
895
896 PF_RULES_RASSERT();
897 KASSERT(r != NULL, ("r is NULL"));
898 KASSERT(!(r->rule_flag & PFRULE_AFTO), ("AFTO rule"));
899
900 if (ctx->sk == NULL && ctx->nk == NULL) {
901 if (pf_state_key_setup(pd, pd->nsport, pd->ndport, &ctx->sk,
902 &ctx->nk))
903 return (PFRES_MEMORY);
904 }
905
906 naddr = &ctx->nk->addr[1];
907 nportp = &ctx->nk->port[1];
908
909 switch (nat_action) {
910 case PF_NAT:
911 if (pd->proto == IPPROTO_ICMP) {
912 low = 1;
913 high = 65535;
914 } else {
915 low = rpool->proxy_port[0];
916 high = rpool->proxy_port[1];
917 }
918 if (rpool->mape.offset > 0) {
919 if (pf_get_mape_sport(pd, r, naddr, nportp, &sn,
920 &sh, &ctx->udp_mapping, rpool)) {
921 DPFPRINTF(PF_DEBUG_MISC,
922 ("pf: MAP-E port allocation (%u/%u/%u)"
923 " failed\n",
924 rpool->mape.offset,
925 rpool->mape.psidlen,
926 rpool->mape.psid));
927 reason = PFRES_MAPFAILED;
928 goto notrans;
929 }
930 } else if (pf_get_sport(pd, r, naddr, nportp, low, high, &sn,
931 &sh, rpool, &ctx->udp_mapping, PF_SN_NAT)) {
932 DPFPRINTF(PF_DEBUG_MISC,
933 ("pf: NAT proxy port allocation (%u-%u) failed\n",
934 rpool->proxy_port[0], rpool->proxy_port[1]));
935 reason = PFRES_MAPFAILED;
936 goto notrans;
937 }
938 break;
939 case PF_BINAT:
940 switch (pd->dir) {
941 case PF_OUT:
942 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL){
943 switch (pd->af) {
944 #ifdef INET
945 case AF_INET:
946 if (rpool->cur->addr.p.dyn->
947 pfid_acnt4 < 1) {
948 reason = PFRES_MAPFAILED;
949 goto notrans;
950 }
951 PF_POOLMASK(naddr,
952 &rpool->cur->addr.p.dyn->pfid_addr4,
953 &rpool->cur->addr.p.dyn->pfid_mask4,
954 &pd->nsaddr, AF_INET);
955 break;
956 #endif /* INET */
957 #ifdef INET6
958 case AF_INET6:
959 if (rpool->cur->addr.p.dyn->
960 pfid_acnt6 < 1) {
961 reason = PFRES_MAPFAILED;
962 goto notrans;
963 }
964 PF_POOLMASK(naddr,
965 &rpool->cur->addr.p.dyn->pfid_addr6,
966 &rpool->cur->addr.p.dyn->pfid_mask6,
967 &pd->nsaddr, AF_INET6);
968 break;
969 #endif /* INET6 */
970 }
971 } else
972 PF_POOLMASK(naddr,
973 &rpool->cur->addr.v.a.addr,
974 &rpool->cur->addr.v.a.mask, &pd->nsaddr,
975 pd->af);
976 break;
977 case PF_IN:
978 if (r->src.addr.type == PF_ADDR_DYNIFTL) {
979 switch (pd->af) {
980 #ifdef INET
981 case AF_INET:
982 if (r->src.addr.p.dyn->pfid_acnt4 < 1) {
983 reason = PFRES_MAPFAILED;
984 goto notrans;
985 }
986 PF_POOLMASK(naddr,
987 &r->src.addr.p.dyn->pfid_addr4,
988 &r->src.addr.p.dyn->pfid_mask4,
989 &pd->ndaddr, AF_INET);
990 break;
991 #endif /* INET */
992 #ifdef INET6
993 case AF_INET6:
994 if (r->src.addr.p.dyn->pfid_acnt6 < 1) {
995 reason = PFRES_MAPFAILED;
996 goto notrans;
997 }
998 PF_POOLMASK(naddr,
999 &r->src.addr.p.dyn->pfid_addr6,
1000 &r->src.addr.p.dyn->pfid_mask6,
1001 &pd->ndaddr, AF_INET6);
1002 break;
1003 #endif /* INET6 */
1004 }
1005 } else
1006 PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
1007 &r->src.addr.v.a.mask, &pd->ndaddr, pd->af);
1008 break;
1009 }
1010 break;
1011 case PF_RDR: {
1012 struct pf_state_key_cmp key;
1013 int tries;
1014 uint16_t cut, low, high, nport;
1015
1016 reason = pf_map_addr_sn(pd->af, r, &pd->nsaddr, naddr, NULL,
1017 NULL, &sn, &sh, rpool, PF_SN_NAT);
1018 if (reason != 0)
1019 goto notrans;
1020 if ((rpool->opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
1021 PF_POOLMASK(naddr, naddr, &rpool->cur->addr.v.a.mask,
1022 &pd->ndaddr, pd->af);
1023
1024 /* Do not change SCTP ports. */
1025 if (pd->proto == IPPROTO_SCTP)
1026 break;
1027
1028 if (rpool->proxy_port[1]) {
1029 uint32_t tmp_nport;
1030
1031 tmp_nport = ((ntohs(pd->ndport) - ntohs(r->dst.port[0])) %
1032 (rpool->proxy_port[1] - rpool->proxy_port[0] +
1033 1)) + rpool->proxy_port[0];
1034
1035 /* Wrap around if necessary. */
1036 if (tmp_nport > 65535)
1037 tmp_nport -= 65535;
1038 nport = htons((uint16_t)tmp_nport);
1039 } else if (rpool->proxy_port[0])
1040 nport = htons(rpool->proxy_port[0]);
1041 else
1042 nport = pd->ndport;
1043
1044 /*
1045 * Update the destination port.
1046 */
1047 *nportp = nport;
1048
1049 /*
1050 * Do we have a source port conflict in the stack state? Try to
1051 * modulate the source port if so. Note that this is racy since
1052 * the state lookup may not find any matches here but will once
1053 * pf_create_state() actually instantiates the state.
1054 */
1055 bzero(&key, sizeof(key));
1056 key.af = pd->af;
1057 key.proto = pd->proto;
1058 key.port[0] = pd->nsport;
1059 PF_ACPY(&key.addr[0], &pd->nsaddr, key.af);
1060 key.port[1] = nport;
1061 PF_ACPY(&key.addr[1], naddr, key.af);
1062
1063 if (!pf_find_state_all_exists(&key, PF_OUT))
1064 break;
1065
1066 tries = 0;
1067
1068 low = 50001; /* XXX-MJ PF_NAT_PROXY_PORT_LOW/HIGH */
1069 high = 65535;
1070 cut = arc4random() % (1 + high - low) + low;
1071 for (uint32_t tmp = cut;
1072 tmp <= high && tmp <= UINT16_MAX &&
1073 tries < V_pf_rdr_srcport_rewrite_tries;
1074 tmp++, tries++) {
1075 key.port[0] = htons(tmp);
1076 if (!pf_find_state_all_exists(&key, PF_OUT)) {
1077 /* Update the source port. */
1078 ctx->nk->port[0] = htons(tmp);
1079 goto out;
1080 }
1081 }
1082 for (uint32_t tmp = cut - 1;
1083 tmp >= low && tries < V_pf_rdr_srcport_rewrite_tries;
1084 tmp--, tries++) {
1085 key.port[0] = htons(tmp);
1086 if (!pf_find_state_all_exists(&key, PF_OUT)) {
1087 /* Update the source port. */
1088 ctx->nk->port[0] = htons(tmp);
1089 goto out;
1090 }
1091 }
1092
1093 /*
1094 * We failed to find a match. Push on ahead anyway, let
1095 * pf_state_insert() be the arbiter of whether the state
1096 * conflict is tolerable. In particular, with TCP connections
1097 * the state may be reused if the TCP state is terminal.
1098 */
1099 DPFPRINTF(PF_DEBUG_MISC,
1100 ("pf: RDR source port allocation failed\n"));
1101 break;
1102
1103 out:
1104 DPFPRINTF(PF_DEBUG_MISC,
1105 ("pf: RDR source port allocation %u->%u\n",
1106 ntohs(pd->nsport), ntohs(ctx->nk->port[0])));
1107 break;
1108 }
1109 default:
1110 panic("%s: unknown action %u", __func__, r->action);
1111 }
1112
1113 /* Return success only if translation really happened. */
1114 if (bcmp(ctx->sk, ctx->nk, sizeof(struct pf_state_key_cmp))) {
1115 return (PFRES_MATCH);
1116 }
1117
1118 reason = PFRES_MAX;
1119 notrans:
1120 uma_zfree(V_pf_state_key_z, ctx->nk);
1121 uma_zfree(V_pf_state_key_z, ctx->sk);
1122 ctx->sk = ctx->nk = NULL;
1123
1124 return (reason);
1125 }
1126
1127 int
pf_get_transaddr_af(struct pf_krule * r,struct pf_pdesc * pd)1128 pf_get_transaddr_af(struct pf_krule *r, struct pf_pdesc *pd)
1129 {
1130 #if defined(INET) && defined(INET6)
1131 struct pf_addr ndaddr, nsaddr, naddr;
1132 u_int16_t nport = 0;
1133 int prefixlen = 96;
1134 struct pf_srchash *sh = NULL;
1135 struct pf_ksrc_node *sns = NULL;
1136
1137 bzero(&nsaddr, sizeof(nsaddr));
1138 bzero(&ndaddr, sizeof(ndaddr));
1139
1140 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1141 printf("pf: af-to %s %s, ",
1142 pd->naf == AF_INET ? "inet" : "inet6",
1143 TAILQ_EMPTY(&r->rdr.list) ? "nat" : "rdr");
1144 pf_print_host(&pd->nsaddr, pd->nsport, pd->af);
1145 printf(" -> ");
1146 pf_print_host(&pd->ndaddr, pd->ndport, pd->af);
1147 printf("\n");
1148 }
1149
1150 if (TAILQ_EMPTY(&r->nat.list))
1151 panic("pf_get_transaddr_af: no nat pool for source address");
1152
1153 /* get source address and port */
1154 if (pf_get_sport(pd, r, &nsaddr, &nport,
1155 r->nat.proxy_port[0], r->nat.proxy_port[1], &sns, &sh, &r->nat,
1156 NULL, PF_SN_NAT)) {
1157 DPFPRINTF(PF_DEBUG_MISC,
1158 ("pf: af-to NAT proxy port allocation (%u-%u) failed",
1159 r->nat.proxy_port[0], r->nat.proxy_port[1]));
1160 return (-1);
1161 }
1162
1163 if (pd->proto == IPPROTO_ICMPV6 && pd->naf == AF_INET) {
1164 pd->ndport = ntohs(pd->ndport);
1165 if (pd->ndport == ICMP6_ECHO_REQUEST)
1166 pd->ndport = ICMP_ECHO;
1167 else if (pd->ndport == ICMP6_ECHO_REPLY)
1168 pd->ndport = ICMP_ECHOREPLY;
1169 pd->ndport = htons(pd->ndport);
1170 } else if (pd->proto == IPPROTO_ICMP && pd->naf == AF_INET6) {
1171 pd->nsport = ntohs(pd->nsport);
1172 if (pd->ndport == ICMP_ECHO)
1173 pd->ndport = ICMP6_ECHO_REQUEST;
1174 else if (pd->ndport == ICMP_ECHOREPLY)
1175 pd->ndport = ICMP6_ECHO_REPLY;
1176 pd->nsport = htons(pd->nsport);
1177 }
1178
1179 /* get the destination address and port */
1180 if (! TAILQ_EMPTY(&r->rdr.list)) {
1181 if (pf_map_addr_sn(pd->naf, r, &nsaddr, &naddr, NULL, NULL,
1182 &sns, NULL, &r->rdr, PF_SN_NAT))
1183 return (-1);
1184 if (r->rdr.proxy_port[0])
1185 pd->ndport = htons(r->rdr.proxy_port[0]);
1186
1187 if (pd->naf == AF_INET) {
1188 /* The prefix is the IPv4 rdr address */
1189 prefixlen = in_mask2len(
1190 (struct in_addr *)&r->rdr.cur->addr.v.a.mask);
1191 inet_nat46(pd->naf, &pd->ndaddr, &ndaddr, &naddr,
1192 prefixlen);
1193 } else {
1194 /* The prefix is the IPv6 rdr address */
1195 prefixlen = in6_mask2len(
1196 (struct in6_addr *)&r->rdr.cur->addr.v.a.mask, NULL);
1197 inet_nat64(pd->naf, &pd->ndaddr, &ndaddr, &naddr,
1198 prefixlen);
1199 }
1200 } else {
1201 if (pd->naf == AF_INET) {
1202 /* The prefix is the IPv6 dst address */
1203 prefixlen = in6_mask2len(
1204 (struct in6_addr *)&r->dst.addr.v.a.mask, NULL);
1205 if (prefixlen < 32)
1206 prefixlen = 96;
1207 inet_nat64(pd->naf, &pd->ndaddr, &ndaddr, &pd->ndaddr,
1208 prefixlen);
1209 } else {
1210 /*
1211 * The prefix is the IPv6 nat address
1212 * (that was stored in pd->nsaddr)
1213 */
1214 prefixlen = in6_mask2len(
1215 (struct in6_addr *)&r->nat.cur->addr.v.a.mask, NULL);
1216 if (prefixlen > 96)
1217 prefixlen = 96;
1218 inet_nat64(pd->naf, &pd->ndaddr, &ndaddr, &nsaddr,
1219 prefixlen);
1220 }
1221 }
1222
1223 PF_ACPY(&pd->nsaddr, &nsaddr, pd->naf);
1224 PF_ACPY(&pd->ndaddr, &ndaddr, pd->naf);
1225
1226 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1227 printf("pf: af-to %s done, prefixlen %d, ",
1228 pd->naf == AF_INET ? "inet" : "inet6",
1229 prefixlen);
1230 pf_print_host(&pd->nsaddr, pd->nsport, pd->naf);
1231 printf(" -> ");
1232 pf_print_host(&pd->ndaddr, pd->ndport, pd->naf);
1233 printf("\n");
1234 }
1235
1236 return (0);
1237 #else
1238 return (-1);
1239 #endif
1240 }
1241