[client] Pin the file drop port over the tunnel

The file drop server listened on 41421, which falls inside the ephemeral
port range on Linux (32768-60999) and Windows (49152-65535), so any
outbound connection could take it after boot. The receiver then bound a
dynamic port and advertised it over signaling, and the sender waited for
that advertisement before retrying.

That coupled a data plane feature to signaling traffic: once a peer
connection is established there is no reason for another offer or answer
to go out, so a sender could wait out the grace period for an
advertisement that never came.

Move the port to 22042, next to the SSH (22022) and DNS forwarder (22054)
ports and clear of both ephemeral ranges, and keep the tunnel side fixed
the way SSH does. A receiver that cannot bind it falls back to a dynamic
port and redirects 22042 to it with an inbound DNAT rule, so senders
always dial the well known port and never negotiate. NB_FILEDROP_PORT
overrides the local bind only.

The DNAT runs ahead of the filter on every backend (nftables prerouting
at NAT dest priority, iptables nat/PREROUTING, and the userspace filter's
translate-then-redecode path), so the netstack service registry keeps
taking the bound port.

This drops the port registry, the retry that waited on it, and the
signaling plumbing that fed it.
This commit is contained in:
Zoltán Papp
2026-08-25 18:51:21 +02:00
parent 861e0d5609
commit 41906d4a1a
9 changed files with 83 additions and 236 deletions
+64 -13
View File
@@ -4,9 +4,12 @@ import (
"context"
"net"
"net/netip"
"os"
"strconv"
log "github.com/sirupsen/logrus"
firewallManager "github.com/netbirdio/netbird/client/firewall/manager"
"github.com/netbirdio/netbird/client/internal/filedrop"
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
"github.com/netbirdio/netbird/client/internal/peer"
@@ -36,7 +39,7 @@ func (e *Engine) startFileDrop() {
}
wgAddr := e.wgInterface.Address()
addr := netip.AddrPortFrom(wgAddr.IP, filedrop.Port)
addr := netip.AddrPortFrom(wgAddr.IP, fileDropListenPort())
resolver := filedropResolver{status: e.statusRecorder}
netstackNet := e.wgInterface.GetNet()
@@ -47,7 +50,7 @@ func (e *Engine) startFileDrop() {
bound := e.fileDrop.ReceiverPort()
if bound == 0 {
bound = filedrop.Port
bound = addr.Port()
}
e.fileDropPort = bound
@@ -65,24 +68,56 @@ func (e *Engine) startFileDrop() {
}
}
if bound != filedrop.Port {
e.signaler.SetFiledropPort(bound)
}
e.setupFileDropPortRedirection(bound)
e.setFileDropTunnel()
e.fileDropRunning = true
}
// recordFiledropPort stores the file drop port a peer advertised over signaling;
// a value that does not fit a port is treated as the default.
func (e *Engine) recordFiledropPort(peerKey string, port uint32) {
if e.fileDrop == nil {
// setupFileDropPortRedirection keeps the tunnel-side port fixed when the receiver
// could not bind it, so senders always reach the well-known port.
func (e *Engine) setupFileDropPortRedirection(bound uint16) {
if e.firewall == nil || bound == filedrop.Port {
return
}
if port > 65535 {
port = 0
for _, addr := range e.fileDropLocalAddrs() {
if err := e.firewall.AddInboundDNAT(addr, firewallManager.ProtocolTCP, filedrop.Port, bound); err != nil {
log.Warnf("failed to add file drop port redirection on %s: %v", addr, err)
continue
}
log.Infof("file drop port redirection enabled: %s:%d -> %s:%d", addr, filedrop.Port, addr, bound)
}
e.fileDrop.Ports().Set(filedrop.PeerKey(peerKey), uint16(port))
}
func (e *Engine) removeFileDropPortRedirection(bound uint16) {
if e.firewall == nil || bound == 0 || bound == filedrop.Port {
return
}
for _, addr := range e.fileDropLocalAddrs() {
if err := e.firewall.RemoveInboundDNAT(addr, firewallManager.ProtocolTCP, filedrop.Port, bound); err != nil {
log.Warnf("failed to remove file drop port redirection on %s: %v", addr, err)
continue
}
log.Debugf("file drop port redirection removed: %s:%d -> %s:%d", addr, filedrop.Port, addr, bound)
}
}
func (e *Engine) fileDropLocalAddrs() []netip.Addr {
if e.wgInterface == nil {
return nil
}
wgAddr := e.wgInterface.Address()
var addrs []netip.Addr
if wgAddr.IP.IsValid() {
addrs = append(addrs, wgAddr.IP)
}
if wgAddr.IPv6.IsValid() {
addrs = append(addrs, wgAddr.IPv6)
}
return addrs
}
func (e *Engine) setFileDropTunnel() {
@@ -132,7 +167,7 @@ func (e *Engine) stopFileDrop() {
registrar.UnregisterNetstackService(nftypes.TCP, e.fileDropPort)
}
}
e.signaler.SetFiledropPort(0)
e.removeFileDropPortRedirection(e.fileDropPort)
}
if err := e.fileDrop.StopReceiver(); err != nil {
@@ -141,3 +176,19 @@ func (e *Engine) stopFileDrop() {
e.fileDropRunning = false
e.fileDropPort = 0
}
// fileDropListenPort is the port the receiver tries to bind locally; the
// tunnel-side port stays filedrop.Port whatever this resolves to.
func fileDropListenPort() uint16 {
raw := os.Getenv(filedrop.EnvPort)
if raw == "" {
return filedrop.Port
}
port, err := strconv.ParseUint(raw, 10, 16)
if err != nil {
log.Warnf("invalid %s value %q, using %d", filedrop.EnvPort, raw, filedrop.Port)
return filedrop.Port
}
return uint16(port)
}