From: Stuart Henderson Subject: pf_lb port range retries [Re: CVS: cvs.openbsd.org: src] To: Alexandr Nedvedicky Cc: tech Date: Mon, 9 Nov 2026 11:13:20 +0000 On 2026/11/09 03:56, Alexandr Nedvedicky wrote: > CVSROOT: /cvs > Module name: src > Changes by: sashan@cvs.openbsd.org 2026/11/09 03:56:59 > > Modified files: > sys/net : pf_lb.c > > Log message: > pf(4): depleted source port pool for NAT starves other packets > > The function pf_get_sport() in current selects the source port > number for network address translation (a.k.a. NAT). It basically > chooses a random source port and uses pf_find_state_all() > to check if the chosen port number is already in use for a particular NAT > address. If the port is already in use, pf_get_sport() selects > another port number and retries. This process continues as long > as the chosen port number collides with an existing address:port tuple. > The problem is that all this happens under the protection of > PF_LOCK(). This effectively stops pf(4) rule evaluation > for other packets that enter the firewall and have no state. > > 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. > 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. If people bump into this, increasing the nat port range should help (at least to an extent). Default range is 50001:65535, defined in pfctl_parser.h PF_NAT_PROXY_PORT_LOW / PF_NAT_PROXY_PORT_HIGH, but it can be set per-rule. If overriding, try to avoid starving the net.inet.ip.porthi* range to prevent affecting connections from the machine itself too badly. Something like 30000:60000 would likely be a better choice than e.g. 40000:65535.