Index | Thread | Search

From:
Alexandr Nedvedicky <sashan@fastmail.net>
Subject:
Re: pf_lb port range retries [Re: CVS: cvs.openbsd.org: src]
To:
Alexandr Nedvedicky <sashan@cvs.openbsd.org>, tech <tech@openbsd.org>
Date:
Mon, 9 Nov 2026 13:25:48 +0100

Download raw body.

Thread
Hello,

On Mon, Nov 09, 2026 at 11:13:20AM +0000, Stuart Henderson wrote:
</snip>
> > 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.

> >           The packet is dropped and PF_LOCK() is released so other
> > packets waiting for a rule check can be inspected by pf_test().
> 
> This isn't correct. The nat rule is not matched but ruleset traversal
> continues. If the ruleset allows it to pass (common where the nat-to
> is on a match rule), the packet is sent untranslated unless there's
> a subsequent translate rule which matches and has a free port.

    I have not tested the change for 'match ... nat-to' case, but
    if I understand code correct, the packet gets always dropped
    if pf_get_sport() fails. That's how things work (according
    to my understanding of code in pf).

	if number of attempts is reached pf_get_sport() returns 1
	to indicate failure.

	the caller, pf_get_transaddr(), then returns -1 to further
	propagate the error.

	this is intercepted in pf_match_rule() function. The block
	that handles the 'match ... nat-to' rule reads as follows:

    4805            if (r->action == PF_MATCH) {
    4806                    if ((ctx->ri = pool_get(&pf_rule_item_pl,
    4807                        PR_NOWAIT)) == NULL) {
    4808                            REASON_SET(&ctx->reason, PFRES_MEMORY);
    4809                            return (PF_TEST_FAIL);
    4810                    }
    4811                    ctx->ri->r = r;
    4812                    /* order is irrelevant */
    4813                    SLIST_INSERT_HEAD(&ctx->rules, ctx->ri, entry);
    4814                    ctx->ri = NULL;
    4815                    pf_rule_to_actions(r, &ctx->act);
    4816                    if (r->rule_flag & PFRULE_AFTO)
    4817                            ctx->pd->naf = r->naf;
    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                    }

	the code bails out with PF_TEST_FAIL error. the in turn makes
	pf_test_rule() here:

    4979         ruleset = &pf_main_ruleset;                               
    4980         rv = pf_match_rule(&ctx, ruleset);                                
    4981         if (rv == PF_TEST_FAIL || ctx.limiter_drop == 1) {
    4982                 REASON_SET(reason, ctx.reason);
    4983                 goto cleanup;
    4984         }

	to take caleanup path and rule traversal stops. The value of
	local variable 'action' is PF_DROP.  so there should be no difference
	between the way how 'pass ... nat-to' and 'mach ... nat-to' rules are
	handled.

thank you for taking a look.
regards
sashan