multicast cleanup

This commit is contained in:
jdl
2026-06-12 14:08:41 +02:00
parent 991fcffa68
commit 6856727985
5 changed files with 27 additions and 176 deletions

View File

@@ -1,57 +1,46 @@
package peer
import (
"encoding/binary"
"net/netip"
"golang.org/x/crypto/nacl/sign"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"vppn/peer/multicast"
)
func (a *App) onMulticastDiscovery(e MulticastEvent) {
func (a *App) onMulticastDiscovery(pkt multicast.Packet) {
if a.isPublic {
return
}
// Peek at the VPN IP byte to find the sender peer before verifying.
// nacl/sign prepends a 64-byte signature, so payload starts at offset sign.Overhead.
if len(e.signed) != mcSignedBeaconLen {
return
}
netAddr := a.vpnNet.Addr().As4()
octets := netAddr
octets[3] = e.signed[sign.Overhead]
// Locate the sender peer by its VPN IP (final octet carried in the beacon).
octets := a.vpnNet.Addr().As4()
octets[3] = pkt.PeerIP
vpnIP := netip.AddrFrom4(octets)
peer, ok := a.peersByIP[vpnIP]
if !ok {
if !ok || peer.IsPublic || peer.State == StateDirect {
return
}
if peer.IsPublic || peer.State == StateDirect {
// Authenticate the beacon against the peer's known sign key.
if !pkt.Verify(a.mcVerifyBuf, &peer.SignPubKey) {
return
}
payload, ok := sign.Open(nil, e.signed, &peer.SignPubKey)
if !ok {
// The beacon is authentic but must also advertise the WG key the hub gave
// us for this peer; otherwise it's inconsistent — drop it.
if wgtypes.Key(pkt.WGPubKey) != peer.PubKey() {
return
}
// payload: [1 VPN IP byte][32 WG pubkey][2 WG port][8 timestamp]
// (timestamp freshness is gated in the reader before this point).
wgPubKey, err := wgtypes.NewKey(payload[1:33])
if err != nil || wgPubKey != peer.PubKey() {
return
}
wgPort := binary.BigEndian.Uint16(payload[33:35])
endpoint := netip.AddrPortFrom(e.src, wgPort)
endpoint := netip.AddrPortFrom(pkt.Src, pkt.WGPort)
if !endpoint.IsValid() {
return
}
var v4, v6 netip.AddrPort
if e.src.Is4() {
if pkt.Src.Is4() {
v4 = endpoint
} else {
v6 = endpoint