Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
c796597
ML-KEM encapsulate/decapsulate module
riccardomanfrin Jul 21, 2026
a48bb9b
Messages definition
riccardomanfrin Jul 21, 2026
87afc3e
Pure mechanics of manager
riccardomanfrin Jul 21, 2026
92cf08b
Bench key material boilerplate time/allocs
riccardomanfrin Jul 21, 2026
19739b2
Admits possible errors on Encode
riccardomanfrin Jul 21, 2026
311c5f8
Defines event callbacks
riccardomanfrin Jul 21, 2026
e4bed8d
Adds driver to glue together manager and outside world
riccardomanfrin Jul 21, 2026
d8fa34e
Reuse answer, don't calculate again
riccardomanfrin Jul 21, 2026
75dcd57
Models reattempts
riccardomanfrin Jul 21, 2026
fa3c3ad
Manages convergence
riccardomanfrin Jul 22, 2026
0a3dc2b
[squash] isInitial and answered can be inferred without state variables
riccardomanfrin Jul 22, 2026
661926c
Collapse Driver and Manager in one.
riccardomanfrin Jul 23, 2026
f8bb816
Epurate wg refs
riccardomanfrin Jul 23, 2026
2ed0d3b
Assume two transports: initial "signal" (control plane) one (no data …
riccardomanfrin Jul 23, 2026
cec4bd4
Leave signal offer/answer as a pull/push operation not as an actual t…
riccardomanfrin Jul 23, 2026
0aeed6a
Removes confirm. Uses next offer to deliver confirmation/ack of previ…
riccardomanfrin Jul 23, 2026
6a644df
Protocol update
riccardomanfrin Jul 24, 2026
902daf6
Invert order of keys as per draft
riccardomanfrin Jul 24, 2026
30212ca
Adds MLKEM Payload placeholder to client internals
riccardomanfrin Jul 24, 2026
41298ac
Added enabled env var
riccardomanfrin Jul 24, 2026
dd5e943
Adds no-op Transports and callbacks
riccardomanfrin Jul 24, 2026
077d96a
Initializes PQ ML-KEM manager
riccardomanfrin Jul 24, 2026
0ae4622
Adds real callback setter for PSK on ready
riccardomanfrin Jul 24, 2026
40b6a55
Ensure iface is up and with overlay ip assigned to get a valid UDP port
riccardomanfrin Jul 24, 2026
6a42f7d
Communicate the port over the signal exchange
riccardomanfrin Jul 24, 2026
d2db1c7
Adds transport
riccardomanfrin Jul 25, 2026
cda3922
Makes Transport just a UDP socket.
riccardomanfrin Jul 25, 2026
ef29e99
Race fix
riccardomanfrin Jul 25, 2026
539a1f4
Typo
riccardomanfrin Jul 25, 2026
3270fc5
Bit of renaming
riccardomanfrin Jul 25, 2026
bac6af3
Homogeneous logs prefix
riccardomanfrin Jul 25, 2026
56b315d
pqkem: dedicated slog logger via NB_PQ_MLKEM_LOG_LEVEL
riccardomanfrin Jul 26, 2026
555310b
pqkem: carry KEM offer/answer over the signalling exchange
riccardomanfrin Jul 26, 2026
7c276ce
pqkem: apply derived PSK at WG peer-config time (pull) + keep push fo…
riccardomanfrin Jul 26, 2026
e3ab585
pqkem: register data-path endpoint from signalling
riccardomanfrin Jul 27, 2026
1c46b4e
pqkem: clock data-path PSK rotation from WireGuard handshakes
riccardomanfrin Jul 27, 2026
b92b145
Renames SetRemotePort to SetRemoteAddr
riccardomanfrin Jul 27, 2026
562c02f
Adds log tracepoints
riccardomanfrin Jul 28, 2026
b1c0a2b
Don't rotate PQ keys if data path is idle for ~90s (less than a WG ha…
riccardomanfrin Jul 31, 2026
8c8abfb
pqkem: derive PSK with HKDF-SHA256
riccardomanfrin Jul 29, 2026
b1b2cc1
pqkem: rotate PSK in kernel mode instead of skipping
riccardomanfrin Jul 29, 2026
f322be1
pqkem: strict (fail-closed) mode + wire status Quantum resistance
riccardomanfrin Jul 30, 2026
e1d5551
Discriminate initial from rekey failure
riccardomanfrin Jul 30, 2026
d485567
pqkem: recover from persistent rekey failure by re-bootstrapping over…
riccardomanfrin Jul 31, 2026
63d2996
pqkem: concurrency tests (recovery + race)
riccardomanfrin Jul 31, 2026
d988525
Prioritize Kem over RP
riccardomanfrin Aug 4, 2026
bfa3652
Adds test to validate compromised keys are not accepted
riccardomanfrin Aug 4, 2026
202176e
Be more explicit on names that is a fake key to ensure we don't commu…
riccardomanfrin Aug 4, 2026
cb5ee8e
Remove obvious comments; leave only the why of things
riccardomanfrin Aug 4, 2026
5a6de00
Adds PQ connection tests
riccardomanfrin Aug 4, 2026
cfaf4f1
Allow non strict mode
riccardomanfrin Aug 4, 2026
b93e8d6
Skip default port send in signal proto
riccardomanfrin Aug 4, 2026
2b27040
Anticipates PSK before WG does handshake so it finds it to set it
riccardomanfrin Aug 5, 2026
a78d587
Introduces a forced WG handshake on initial MLKEM bootstrap.
riccardomanfrin Aug 6, 2026
3ed4b4f
Revert "Introduces a forced WG handshake on initial MLKEM bootstrap."
riccardomanfrin Aug 6, 2026
4ffa2c7
Introduces a forced imparity on MLKEM bootstrap.
riccardomanfrin Aug 6, 2026
1870b10
[client] pqkem: make signalling bootstrap idempotent through awaiting…
riccardomanfrin Aug 6, 2026
3b61222
[client] pqkem: gate controller re-offer to kick the KEM exactly once
riccardomanfrin Aug 6, 2026
4c6735e
[client] pqkem: don't let the responder's delayed update revert the PSK
riccardomanfrin Aug 7, 2026
82699af
[client] pqkem: split handshaker Listen into per-message handlers
riccardomanfrin Aug 7, 2026
1dc48bc
Addresses CI fixes
riccardomanfrin Aug 7, 2026
11555c3
Addresses CI fixes
riccardomanfrin Aug 7, 2026
8a04fa4
Addresses CI fixes
riccardomanfrin Aug 7, 2026
ecddbf2
Addresses CI fixes
riccardomanfrin Aug 7, 2026
3efd4c9
UDPv4 is sufficient! we always have 100.x overlay. IPv6 is additive
riccardomanfrin Aug 7, 2026
1ee0121
Add comment to explain working boundaries
riccardomanfrin Aug 24, 2026
91f1a72
Adds observability and fixes cross cases
riccardomanfrin Aug 27, 2026
aca6885
[client] pqkem: enforce exchange roles and re-bootstrap stale signal …
riccardomanfrin Aug 28, 2026
9ea3ddc
[client] pqkem: don't drop a responder's offer when the controller ha…
riccardomanfrin Aug 28, 2026
e0523ef
[client] pqkem: make exchange logs discriminate offer/answer, role, t…
riccardomanfrin Sep 1, 2026
1bae44e
Merge branch 'main' into feat-post_quantum_ml_kem
riccardomanfrin Oct 5, 2026
dd07b12
[client] engine: extract Rosenpass and ML-KEM startup from Start
riccardomanfrin Oct 5, 2026
89ec00e
[client] pqkem: address review findings on convergence, strict parsin…
riccardomanfrin Oct 5, 2026
a575a8f
Merge remote-tracking branch 'origin/main' into feat-post_quantum_ml_kem
riccardomanfrin Oct 5, 2026
9af2788
[client] pqkem: correct the convergence-deadline comment
riccardomanfrin Oct 7, 2026
cc2d43e
[client] pqkem: document the real responder callback timing
riccardomanfrin Oct 7, 2026
9041e9a
[client] pqkem: document rekey-failure recovery as host-defined
riccardomanfrin Oct 7, 2026
5341ab0
[client] pqkem: commit the initiator PSK only after Finish succeeds
riccardomanfrin Oct 7, 2026
d0f7e2a
[client] pqkem: route a failed PSK application through convergence re…
riccardomanfrin Oct 7, 2026
1182239
[client] pqkem: carry a generation on PSK callbacks to drop stale app…
riccardomanfrin Oct 7, 2026
ff634c5
[client] pqkem: don't let a superseded rotation offer revert a fresh …
riccardomanfrin Oct 7, 2026
0747fa0
[client] pqkem: signal a responder failure distinctly from "no KEM"
riccardomanfrin Oct 7, 2026
4eba98d
[client] pqkem: drain the data-path receive loop before clearing state
riccardomanfrin Oct 7, 2026
6e0c150
[client] pqkem: drop the unused exchangeCtl.pendingPSK field
riccardomanfrin Oct 9, 2026
85eaf43
[client] pqkem: mention *ErrorMsg in the Decode doc comment
riccardomanfrin Oct 9, 2026
25893d2
[client] status: spell out Rosenpass in the quantum-resistance mechan…
riccardomanfrin Oct 9, 2026
f660e2e
[client] peer: correct the MlkemPort zero-value comment
riccardomanfrin Oct 9, 2026
b04c8ef
[client] pqkem: fix the capability-signal wording in a test comment
riccardomanfrin Oct 9, 2026
2bb557c
[client] pqkem: preserve group scope in the slog-to-logrus handler
riccardomanfrin Oct 9, 2026
50a0bfc
[client] pqkem: signal an offer-build failure with a marker, not an e…
riccardomanfrin Oct 9, 2026
de70516
[client] pqkem: drain in-flight signal handlers on Stop
riccardomanfrin Oct 9, 2026
daf9fba
[client] pqkem: document SignalOffer's error-marker return contract
riccardomanfrin Oct 9, 2026
6cba403
Merge remote-tracking branch 'origin/main' into feat-post_quantum_ml_kem
riccardomanfrin Oct 16, 2026
1152b25
[client] pqkem: run the data-path transport over a net.PacketConn
riccardomanfrin Oct 19, 2026
f669cba
[client] pqkem: bind the data-path transport on the netstack in netst…
riccardomanfrin Oct 19, 2026
2a2f7a9
[client] pqkem: report a peer as quantum resistant once its PSK lands
riccardomanfrin Oct 19, 2026
a73ce11
[client] pqkem: bind the configured pre-shared key into the PQ PSK
riccardomanfrin Oct 19, 2026
b10fc22
[client] pqkem: route PSK commits and data-path sends through helpers
riccardomanfrin Oct 19, 2026
1345107
[client] pqkem: authenticate data-path messages under the shared PSK
riccardomanfrin Oct 19, 2026
c1118f6
[client] build the lazy peer config in one place
riccardomanfrin Oct 19, 2026
3cfc5c8
[client] pqkem: install strict-mode keys on idle lazy peers
riccardomanfrin Oct 19, 2026
5f3f5d5
[client] pqkem: re-bootstrap over Signal only while the peer is conne…
riccardomanfrin Oct 19, 2026
92764d8
[client] pqkem: keep rotating after a rotation times out
riccardomanfrin Oct 19, 2026
e9a622c
[client] pqkem: apply PSK generations to WireGuard in order
riccardomanfrin Oct 19, 2026
d0e8c4e
[client] pqkem: name the post-quantum recovery path for what it does
riccardomanfrin Oct 19, 2026
2853a16
[client] pqkem: answer every responder offer with a fresh KEM bootstrap
riccardomanfrin Oct 20, 2026
aa9141c
[client] pqkem: make weak tests fail on the regressions they name
riccardomanfrin Oct 20, 2026
fb6e955
[client] pqkem: cover the env flags, signal port, offer marker and st…
riccardomanfrin Oct 20, 2026
4eaa57e
[client] pqkem: wake status subscribers when a peer turns quantum res…
riccardomanfrin Oct 21, 2026
ebeebe0
[client] pqkem: drop the previous PSKs on Stop
riccardomanfrin Oct 21, 2026
2c2f684
[client] pqkem: test the controller reoffer through the handshaker
riccardomanfrin Oct 21, 2026
e5764dd
[client] Raise the wasm size limit to 64 MB
riccardomanfrin Oct 21, 2026
50afdff
[client] pqkem: answer an unreadable offer with an error marker
riccardomanfrin Oct 22, 2026
70e6ce1
[client] pqkem: reprogram the latest PSK after a stale WireGuard conf…
riccardomanfrin Oct 22, 2026
95a976d
[client] pqkem: leave a responder offer unanswered during an offer glare
riccardomanfrin Oct 22, 2026
3a6e634
[client] pqkem: resend the in-flight KEM offer during an offer glare
riccardomanfrin Oct 22, 2026
c7971e8
Merge remote-tracking branch 'origin/main' into feat-post_quantum_ml_kem
riccardomanfrin Oct 22, 2026
442b3b4
[client] pqkem: take the netstack type from wgbackend
riccardomanfrin Oct 22, 2026
2069329
[client] pqkem: check the glare resend carries the in-flight offer
riccardomanfrin Oct 22, 2026
b776157
[client] pqkem: follow a peer's ML-KEM capability in both directions
riccardomanfrin Nov 16, 2026
e825ebd
[client] pqkem: warn about a failed key exchange only for a connected…
riccardomanfrin Nov 16, 2026
0677e23
[client] Extract the overlay UDP accept rule from the Rosenpass firew…
riccardomanfrin Nov 16, 2026
4afc71e
[client] pqkem: accept the ML-KEM data-path port in the firewall
riccardomanfrin Nov 16, 2026
7ef0fef
Merge branch 'main' into feat-post_quantum_ml_kem
riccardomanfrin Nov 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions client/internal/conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,7 @@ func (e *ConnMgr) SetExcludeList(ctx context.Context, peerIDs map[string]bool) {
continue
}

lazyPeerCfg := lazyconn.PeerConfig{
PublicKey: peerID,
AllowedIPs: peerConn.WgConfig().AllowedIps,
PeerConnID: peerConn.ConnID(),
Log: peerConn.Log,
}
excludedPeers = append(excludedPeers, lazyPeerCfg)
excludedPeers = append(excludedPeers, newLazyPeerConfig(peerID, peerConn))
}

added := e.lazyConnMgr.ExcludePeer(excludedPeers)
Expand All @@ -188,6 +182,17 @@ func (e *ConnMgr) SetExcludeList(ctx context.Context, peerIDs map[string]bool) {
}
}

// newLazyPeerConfig describes a peer connection to the lazy connection manager.
func newLazyPeerConfig(peerKey string, conn *peer.Conn) lazyconn.PeerConfig {
return lazyconn.PeerConfig{
PublicKey: peerKey,
AllowedIPs: conn.WgConfig().AllowedIps,
PeerConnID: conn.ConnID(),
Log: conn.Log,
PresharedKey: conn.IdlePresharedKey,
}
}

// AddPeerConn registers a peer connection. permanent requests an always-active connection
// (the peer belongs to the exclude set: a forwarder, or a peer that is not lazy by policy).
// Non-permanent peers are handed to the lazy manager. The subsequent SetExcludeList call
Expand All @@ -212,13 +217,7 @@ func (e *ConnMgr) AddPeerConn(ctx context.Context, peerKey string, conn *peer.Co
return
}

lazyPeerCfg := lazyconn.PeerConfig{
PublicKey: peerKey,
AllowedIPs: conn.WgConfig().AllowedIps,
PeerConnID: conn.ConnID(),
Log: conn.Log,
}
excluded, err := e.lazyConnMgr.AddPeer(lazyPeerCfg)
excluded, err := e.lazyConnMgr.AddPeer(newLazyPeerConfig(peerKey, conn))
if err != nil {
conn.Log.Errorf("failed to add peer to lazyconn manager: %v", err)
if err := conn.Open(ctx); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions client/internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/netbirdio/netbird/client/anonymize"
"github.com/netbirdio/netbird/client/configs"
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/client/internal/pqkem"
"github.com/netbirdio/netbird/client/internal/profilemanager"
"github.com/netbirdio/netbird/client/internal/updater/installer"
nbstatus "github.com/netbirdio/netbird/client/status"
Expand Down Expand Up @@ -961,6 +962,9 @@ func (g *BundleGenerator) addCommonConfigFields(configContent *strings.Builder)
configContent.WriteString(fmt.Sprintf("DisableIPv6Discovery: %v\n", g.internalConfig.DisableIPv6Discovery))
configContent.WriteString(fmt.Sprintf("RosenpassEnabled: %v\n", g.internalConfig.RosenpassEnabled))
configContent.WriteString(fmt.Sprintf("RosenpassPermissive: %v\n", g.internalConfig.RosenpassPermissive))
// ML-KEM (Rosenpass alternative) is env-driven, not part of the config, so read it here.
configContent.WriteString(fmt.Sprintf("MLKEMEnabled: %v\n", pqkem.Enabled()))
configContent.WriteString(fmt.Sprintf("MLKEMStrict: %v\n", pqkem.Strict()))
if g.internalConfig.ServerSSHAllowed != nil {
configContent.WriteString(fmt.Sprintf("ServerSSHAllowed: %v\n", *g.internalConfig.ServerSSHAllowed))
}
Expand Down
150 changes: 126 additions & 24 deletions client/internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"net"
"net/netip"
Expand Down Expand Up @@ -52,6 +53,7 @@ import (
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
"github.com/netbirdio/netbird/client/internal/peerstore"
"github.com/netbirdio/netbird/client/internal/portforward"
"github.com/netbirdio/netbird/client/internal/pqkem"
"github.com/netbirdio/netbird/client/internal/profilemanager"
"github.com/netbirdio/netbird/client/internal/relay"
"github.com/netbirdio/netbird/client/internal/rosenpass"
Expand Down Expand Up @@ -214,6 +216,13 @@ type Engine struct {
// rpManager is a Rosenpass manager
rpManager *rosenpass.Manager

// pqkemManager runs the ML-KEM post-quantum PSK exchange (gated by NB_ENABLE_PQ_MLKEM).
// It owns the data-path transport and peer endpoint routing.
pqkemManager *pqkem.Manager
// pqPSKReapplier restores the latest PQ PSK after a conn configures WireGuard with a
// stale one. Set together with pqkemManager.
pqPSKReapplier *pqPSKReapplier

// syncMsgMux is used to guarantee sequential Management Service message processing
syncMsgMux *sync.Mutex

Expand Down Expand Up @@ -555,6 +564,83 @@ func waitWithContext(ctx context.Context, wg *sync.WaitGroup) error {
}
}

// startRosenpassManager brings up Rosenpass when it is enabled and ML-KEM (which takes
// precedence) is not, since the two post-quantum mechanisms are mutually exclusive.
func (e *Engine) startRosenpassManager(publicKey wgtypes.Key) error {
if e.config.RosenpassEnabled && pqkem.Enabled() {
log.Warnf("rosenpass and ML-KEM post-quantum are mutually exclusive; ML-KEM is enabled, so rosenpass is disabled")
}
if !e.config.RosenpassEnabled || pqkem.Enabled() {
return nil
}

log.Infof("rosenpass is enabled")
if e.config.RosenpassPermissive {
log.Infof("running rosenpass in permissive mode")
} else {
log.Infof("running rosenpass in strict mode")
}

var err error
e.rpManager, err = rosenpass.NewManager(e.config.PreSharedKey, e.config.WgIfaceName, publicKey)
if err != nil {
return fmt.Errorf("create rosenpass manager: %w", err)
}
if err := e.rpManager.Run(); err != nil {
return fmt.Errorf("run rosenpass manager: %w", err)
}
return nil
}

// startPQKEMManager brings up the ML-KEM post-quantum PSK exchange when NB_ENABLE_PQ_MLKEM
// is set. It binds the dedicated UDP transport on the WG overlay IP, so the interface must
// already be up. In strict mode a bind failure is fatal (fail closed); in opportunistic
// mode it is logged and the exchange stays disabled.
func (e *Engine) startPQKEMManager(publicKey wgtypes.Key) error {
if !pqkem.Enabled() {
return nil
}
tr, err := newPQTransport(e.config.WgAddr.IP, e.wgInterface.GetNet())
if err != nil {
if pqkem.Strict() {
return fmt.Errorf("pqkem: strict mode enabled but transport bind failed: %w", err)
}
log.Errorf("pqkem: transport bind failed, exchange disabled: %v", err)
return nil
}
applied := newAppliedGenerations()
cbHandler := pqCallbackHandler{
wg: e.wgInterface,
status: e.statusRecorder,
applied: applied,
// On a persistent rekey failure, re-bootstrap the KEM over Signal: a fresh
// signalling offer starts a new exchange that overwrites the stalled PSK on both
// sides, recovering from a data-path desync.
recoverPQKeyOverSignal: func(remoteKey string) {
if conn, ok := e.peerStore.PeerConn(remoteKey); ok {
conn.RecoverPQKeyOverSignal()
}
},
}
var opts []pqkem.Option
if e.config.PreSharedKey != nil {
// The configured key promises that only peers holding it can communicate. The PQ
// PSK replaces it in WireGuard, so it has to carry that guarantee itself.
opts = append(opts, pqkem.WithPresharedSecret(e.config.PreSharedKey[:]))
}
e.pqkemManager = pqkem.NewManager(pqkem.LocalID(publicKey.String()), cbHandler, pqkem.NewLogger(), opts...)
// Shares the apply lock with the rotation push, so a reapply and a push are ordered.
e.pqPSKReapplier = &pqPSKReapplier{psks: e.pqkemManager, wg: e.wgInterface, applied: applied}
e.pqkemManager.Start(tr)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rosenpass gets an inbound UDP accept rule in initFirewall, guarded by e.rpManager != nil. ML-KEM disables Rosenpass, so nothing opens this port and rotation offers can be dropped by the peer ACLs. Needs a check under a restrictive policy, then the same accept rule for the PQ port.

// Here rather than in initFirewall: the firewall comes up before the interface, and the
// transport can only bind once the interface is up.
if e.firewall != nil {
e.allowOverlayUDPPort("ML-KEM", uint16(e.pqkemManager.LocalPort()))
}
log.Infof("pqkem: enabled (udp port %d on overlay %s)", e.pqkemManager.LocalPort(), e.config.WgAddr.IP)
return nil
}

// Start creates a new WireGuard tunnel interface and listens to events from Signal and Management services
// Connections to remote peers are not established here.
// However, they will be established once an event with a list of peers to connect to will be received from Management Service
Expand Down Expand Up @@ -604,20 +690,8 @@ func (e *Engine) Start(netbirdConfig *mgmProto.NetbirdConfig, mgmtURL *url.URL)
publicKey := e.config.WgPrivateKey.PublicKey()
e.flowManager = netflow.NewManager(e.wgInterface, publicKey[:], e.statusRecorder)

if e.config.RosenpassEnabled {
log.Infof("rosenpass is enabled")
if e.config.RosenpassPermissive {
log.Infof("running rosenpass in permissive mode")
} else {
log.Infof("running rosenpass in strict mode")
}
e.rpManager, err = rosenpass.NewManager(e.config.PreSharedKey, e.config.WgIfaceName, publicKey)
if err != nil {
return fmt.Errorf("create rosenpass manager: %w", err)
}
if err := e.rpManager.Run(); err != nil {
return fmt.Errorf("run rosenpass manager: %w", err)
}
if err := e.startRosenpassManager(publicKey); err != nil {
return err
}
e.stateManager.Start()

Expand Down Expand Up @@ -696,6 +770,10 @@ func (e *Engine) Start(netbirdConfig *mgmProto.NetbirdConfig, mgmtURL *url.URL)
e.rpManager.SetInterface(e.wgInterface)
}

if err := e.startPQKEMManager(publicKey); err != nil {
return err
}

// if inbound conns are blocked there is no need to create the ACL manager
if e.firewall != nil && !e.config.BlockInbound {
e.acl = acl.NewDefaultManager(e.firewall)
Expand Down Expand Up @@ -795,14 +873,19 @@ func (e *Engine) initFirewall() error {
e.blockLanAccess()
}

if e.rpManager == nil || !e.config.RosenpassEnabled {
return nil
if e.rpManager != nil && e.config.RosenpassEnabled {
e.allowOverlayUDPPort("rosenpass", uint16(e.rpManager.GetAddress().Port))
}

rosenpassPort := e.rpManager.GetAddress().Port
port := firewallManager.Port{Values: []uint16{uint16(rosenpassPort)}}
return nil
}

// allowOverlayUDPPort accepts inbound UDP from any overlay peer on a port a local
// service listens on, so the peer ACLs do not drop it. Failure is logged, not fatal.
func (e *Engine) allowOverlayUDPPort(service string, portValue uint16) {
port := firewallManager.Port{Values: []uint16{portValue}}

// IPv4-only: rosenpass peers connect via AllowedIps[0] which is always v4.
// IPv4-only: the peers reach the service via AllowedIps[0], which is always v4.
if _, err := e.firewall.AddFilterRule(
nil,
[]netip.Prefix{netip.PrefixFrom(netip.IPv4Unspecified(), 0)},
Expand All @@ -812,13 +895,11 @@ func (e *Engine) initFirewall() error {
&port,
firewallManager.ActionAccept,
); err != nil {
log.Errorf("failed to allow rosenpass interface traffic: %v", err)
return nil
log.Errorf("failed to allow %s interface traffic: %v", service, err)
return
}

log.Infof("rosenpass interface traffic allowed on port %d", rosenpassPort)

return nil
log.Infof("%s interface traffic allowed on port %d", service, portValue)
}

func (e *Engine) blockLanAccess() {
Expand Down Expand Up @@ -946,6 +1027,10 @@ func (e *Engine) removePeer(peerKey string) error {

e.connMgr.RemovePeerConn(peerKey)

if e.pqkemManager != nil {
e.pqkemManager.RemovePeer(pqkem.RemoteID(peerKey))
}

err := e.statusRecorder.RemovePeer(peerKey)
if err != nil {
log.Warnf("received error when removing peer %s from status recorder: %v", peerKey, err)
Expand Down Expand Up @@ -2022,6 +2107,10 @@ func (e *Engine) createPeerConn(pubKey string, allowedIPs []netip.Prefix, agentV
ICEConfig: e.createICEConfig(),
NetMgr: e.netMgr,
}
if e.pqkemManager != nil {
config.PQ = pqHandshaker{mgr: e.pqkemManager, reapplier: e.pqPSKReapplier, status: e.statusRecorder}
config.PQStrict = pqkem.Strict()
}

serviceDependencies := peer.ServiceDependencies{
StatusRecorder: e.statusRecorder,
Expand Down Expand Up @@ -2209,6 +2298,10 @@ func (e *Engine) close() {
_ = e.rpManager.Close()
}

if e.pqkemManager != nil {
e.pqkemManager.Stop()
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := e.portForwardManager.GracefullyStop(ctx); err != nil {
Expand Down Expand Up @@ -2956,6 +3049,13 @@ func convertToOfferAnswer(msg *sProto.Message) (*peer.OfferAnswer, error) {

relayIP := decodeRelayIP(msg.GetBody().GetRelayServerIP())

// Ports are uint16 internally; the proto widens them to uint32, so validate the
// range before narrowing (a value that does not fit is a malformed message).
mlkemPort := msg.GetBody().GetMlkemPort()
if mlkemPort > math.MaxUint16 {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
return nil, fmt.Errorf("invalid ML-KEM port %d in signalling message", mlkemPort)
}

offerAnswer := peer.OfferAnswer{
IceCredentials: peer.IceCredentials{
UFrag: remoteCred.UFrag,
Expand All @@ -2965,6 +3065,8 @@ func convertToOfferAnswer(msg *sProto.Message) (*peer.OfferAnswer, error) {
Version: msg.GetBody().GetNetBirdVersion(),
RosenpassPubKey: rosenpassPubKey,
RosenpassAddr: rosenpassAddr,
MlkemPayload: msg.GetBody().GetMlkemPayload(),
MlkemPort: uint16(mlkemPort),
RelaySrvAddress: msg.GetBody().GetRelayServerAddress(),
RelaySrvIP: relayIP,
SessionID: sessionID,
Expand Down
Loading
Loading