From: Alexandr Nedvedicky Subject: Re: pf_lb port range retries [Re: CVS: cvs.openbsd.org: src] To: Alexandr Nedvedicky , tech Date: Mon, 9 Nov 2026 15:14:04 +0100 Hello, On Mon, Nov 09, 2026 at 01:25:48PM +0100, Alexandr Nedvedicky wrote: > Hello, > > On Mon, Nov 09, 2026 at 11:13:20AM +0000, Stuart Henderson wrote: > > > > The fix introduces a limit on retries. The limit is set to 128. > > > If no free port number is found within 128 attempts, the function > > > gives up. > > > > I think we need to watch for issues with this, it seems like it will > > be bad on systems with a lot of machines behind a nat (though it is > > true that they will already be locking a lot..) > > > > There is a DPFPRINTF for port allocation failure for every new state, > > but it's LOG_NOTICE which is not enabled by default (and I think it > > ends up pretty noisy if you do enable it). I don't think it's > > included in any pfctl -si counter. > > > > I agree adding a counter is good idea. I will prepare the diff shortly. > I've just started to look at this. If the pf_get_sport() allocation fails the pf bumps nat error counter here: 4818 if (pf_get_transaddr(r, ctx->pd, ctx->sns, 4819 &ctx->nr) == -1) { 4820 REASON_SET(&ctx->reason, 4821 PFRES_TRANSLATE); 4822 return (PF_TEST_FAIL); 4823 } 'pfctl -si' reports that 'translate' counter. so there is some insight already. do you think it makes sense to introduce yet another counter just for NAT port allocation failure? perhaps there should be two counters then, like translate-addr translate-port reported. the diff below does exactly that: pf# pfctl -si Status: Enabled for 0 days 00:00:51 Debug: err State Table Total Rate current entries 96 half-open tcp 0 searches 470 9.2/s inserts 111 2.2/s removals 15 0.3/s Counters match 138 2.7/s bad-offset 0 0.0/s fragment 0 0.0/s short 5 0.1/s normalize 0 0.0/s memory 0 0.0/s bad-timestamp 0 0.0/s congestion 0 0.0/s ip-option 0 0.0/s proto-cksum 0 0.0/s state-mismatch 0 0.0/s state-insert 0 0.0/s state-limit 0 0.0/s src-limit 0 0.0/s synproxy 0 0.0/s translate-addr 0 0.0/s translate-port 10 0.2/s no-route 0 0.0/s let me know if you find that useful, so I will chase OKs to get it committed. thanks and regards sashan --------8<---------------8<---------------8<------------------8<-------- diff --git a/sys/net/pf.c b/sys/net/pf.c index 8b778626093..0e7e694388f 100644 --- a/sys/net/pf.c +++ b/sys/net/pf.c @@ -4484,6 +4484,7 @@ pf_match_rule(struct pf_test_ctx *ctx, struct pf_ruleset *ruleset) struct pf_rule *r; struct pf_anchor *child = NULL; int target; + int nat_error; pf_anchor_stack_init(); enter_ruleset: @@ -4815,11 +4816,22 @@ enter_ruleset: pf_rule_to_actions(r, &ctx->act); if (r->rule_flag & PFRULE_AFTO) ctx->pd->naf = r->naf; - if (pf_get_transaddr(r, ctx->pd, ctx->sns, - &ctx->nr) == -1) { + nat_error = pf_get_transaddr(r, ctx->pd, + ctx->sns, &ctx->nr); + switch (nat_error) { + case -1: REASON_SET(&ctx->reason, - PFRES_TRANSLATE); + PFRES_NATADDR); return (PF_TEST_FAIL); + case -2: + REASON_SET(&ctx->reason, + PFRES_NATPORT); + return (PF_TEST_FAIL); + case 0: + break; + default: + panic("%s unkown error %d", __func__, + nat_error); } #if NPFLOG > 0 if (r->log) { @@ -4927,6 +4939,7 @@ pf_test_rule(struct pf_pdesc *pd, struct pf_rule **rm, struct pf_state **sm, int action = PF_DROP; struct pf_test_ctx ctx; int rv; + int nat_error; PF_ASSERT_LOCKED(); @@ -4992,9 +5005,18 @@ pf_test_rule(struct pf_pdesc *pd, struct pf_rule **rm, struct pf_state **sm, pf_rule_to_actions(r, &ctx.act); if (r->rule_flag & PFRULE_AFTO) pd->naf = r->naf; - if (pf_get_transaddr(r, pd, ctx.sns, &ctx.nr) == -1) { - REASON_SET(&ctx.reason, PFRES_TRANSLATE); + nat_error = pf_get_transaddr(r, pd, ctx.sns, &ctx.nr); + switch (nat_error) { + case -1: + REASON_SET(&ctx.reason, PFRES_NATADDR); goto cleanup; + case -2: + REASON_SET(&ctx.reason, PFRES_NATPORT); + goto cleanup; + case 0: + break; + default: + panic("%s unkown error %d", __func__, nat_error); } REASON_SET(&ctx.reason, PFRES_MATCH); diff --git a/sys/net/pf_lb.c b/sys/net/pf_lb.c index ba2f925c8f4..160e65bae56 100644 --- a/sys/net/pf_lb.c +++ b/sys/net/pf_lb.c @@ -723,7 +723,7 @@ pf_get_transaddr(struct pf_rule *r, struct pf_pdesc *pd, "pf: NAT proxy port allocation (%u-%u) failed", r->nat.proxy_port[0], r->nat.proxy_port[1]); - return (-1); + return (-2); } /* decrease least-connection state counter of the previous */ if ((*nr) != NULL && (*nr)->nat.addr.type != PF_ADDR_NONE && diff --git a/sys/net/pfvar.h b/sys/net/pfvar.h index 750fe0ef144..975ae6a45d9 100644 --- a/sys/net/pfvar.h +++ b/sys/net/pfvar.h @@ -1112,9 +1112,10 @@ enum pfi_kif_refs { #define PFRES_MAXSTATES 12 /* State limit */ #define PFRES_SRCLIMIT 13 /* Source node/conn limit */ #define PFRES_SYNPROXY 14 /* SYN proxy */ -#define PFRES_TRANSLATE 15 /* No translation address available */ -#define PFRES_NOROUTE 16 /* No route found for PBR action */ -#define PFRES_MAX 17 /* total+1 */ +#define PFRES_NATADDR 15 /* No translation address available */ +#define PFRES_NATPORT 16 /* No translation port available */ +#define PFRES_NOROUTE 17 /* No route found for PBR action */ +#define PFRES_MAX 18 /* total+1 */ #define PFRES_NAMES { \ "match", \ @@ -1132,7 +1133,8 @@ enum pfi_kif_refs { "state-limit", \ "src-limit", \ "synproxy", \ - "translate", \ + "translate-addr", \ + "translate-port", \ "no-route", \ NULL \ }