|
|
@@ -2,6 +2,7 @@ package nebula
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "io"
|
|
|
"log/slog"
|
|
|
"net/netip"
|
|
|
|
|
|
@@ -9,10 +10,16 @@ import (
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
"github.com/slackhq/nebula/iputil"
|
|
|
"github.com/slackhq/nebula/noiseutil"
|
|
|
+ "github.com/slackhq/nebula/overlay/batch"
|
|
|
+ "github.com/slackhq/nebula/overlay/tio"
|
|
|
"github.com/slackhq/nebula/routing"
|
|
|
)
|
|
|
|
|
|
-func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet, nb, out []byte, q int, localCache firewall.ConntrackCache) {
|
|
|
+func (f *Interface) consumeInsidePacket(pkt tio.Packet, fwPacket *firewall.Packet, nb []byte, sendBatch batch.TxBatcher, rejectBuf []byte, q int, localCache firewall.ConntrackCache) {
|
|
|
+ // borrowed: pkt.Bytes is owned by the originating tio.Queue and is
|
|
|
+ // only valid until the next Read on that queue. If you must keep
|
|
|
+ // the packet, use pkt.Clone() to detach it
|
|
|
+ packet := pkt.Bytes
|
|
|
err := newPacket(packet, false, fwPacket)
|
|
|
if err != nil {
|
|
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
|
@@ -37,7 +44,10 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|
|
// routes packets from the Nebula addr to the Nebula addr through the Nebula
|
|
|
// TUN device.
|
|
|
if immediatelyForwardToSelf {
|
|
|
- _, err := f.readers[q].Write(packet)
|
|
|
+ err := tio.SegmentSuperpacket(pkt, func(seg []byte) error {
|
|
|
+ _, werr := f.readers[q].Write(seg)
|
|
|
+ return werr
|
|
|
+ })
|
|
|
if err != nil {
|
|
|
f.l.Error("Failed to forward to tun", "error", err)
|
|
|
}
|
|
|
@@ -53,11 +63,23 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|
|
}
|
|
|
|
|
|
hostinfo, ready := f.getOrHandshakeConsiderRouting(fwPacket, func(hh *HandshakeHostInfo) {
|
|
|
- hh.cachePacket(f.l, header.Message, 0, packet, f.sendMessageNow, f.cachedPacketMetrics)
|
|
|
+ // borrowed: SegmentSuperpacket builds each segment in the kernel-supplied pkt
|
|
|
+ // bytes underneath. cachePacket explicitly copies its argument (handshake_manager.go cachePacket),
|
|
|
+ // so retaining segments past the loop is safe.
|
|
|
+ err := tio.SegmentSuperpacket(pkt, func(seg []byte) error {
|
|
|
+ hh.cachePacket(f.l, header.Message, 0, seg, f.sendMessageNow, f.cachedPacketMetrics)
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil && f.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
|
+ f.l.Debug("Failed to segment superpacket for handshake cache",
|
|
|
+ "error", err,
|
|
|
+ "vpnAddr", fwPacket.RemoteAddr,
|
|
|
+ )
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
if hostinfo == nil {
|
|
|
- f.rejectInside(packet, out, q)
|
|
|
+ f.rejectInside(packet, rejectBuf, q)
|
|
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
|
f.l.Debug("dropping outbound packet, vpnAddr not in our vpn networks or in unsafe networks",
|
|
|
"vpnAddr", fwPacket.RemoteAddr,
|
|
|
@@ -73,10 +95,9 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|
|
|
|
|
dropReason := f.firewall.Drop(*fwPacket, false, hostinfo, f.pki.GetCAPool(), localCache)
|
|
|
if dropReason == nil {
|
|
|
- f.sendNoMetrics(header.Message, 0, hostinfo.ConnectionState, hostinfo, netip.AddrPort{}, packet, nb, out, q)
|
|
|
-
|
|
|
+ f.sendInsideMessage(hostinfo, pkt, nb, sendBatch)
|
|
|
} else {
|
|
|
- f.rejectInside(packet, out, q)
|
|
|
+ f.rejectInside(packet, rejectBuf, q)
|
|
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
|
hostinfo.logger(f.l).Debug("dropping outbound packet",
|
|
|
"fwPacket", fwPacket,
|
|
|
@@ -86,6 +107,124 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *firewall.Packet
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (f *Interface) sendInsideEncrypt(hostinfo *HostInfo, ci *ConnectionState, seg, scratch, nb []byte) []byte {
|
|
|
+ if noiseutil.EncryptLockNeeded {
|
|
|
+ ci.writeLock.Lock()
|
|
|
+ }
|
|
|
+ c := ci.messageCounter.Add(1)
|
|
|
+
|
|
|
+ out := header.Encode(scratch, header.Version, header.Message, 0, hostinfo.remoteIndexId, c)
|
|
|
+ f.connectionManager.Out(hostinfo)
|
|
|
+
|
|
|
+ out, encErr := ci.eKey.EncryptDanger(out, out, seg, c, nb)
|
|
|
+ if noiseutil.EncryptLockNeeded {
|
|
|
+ ci.writeLock.Unlock()
|
|
|
+ }
|
|
|
+ if encErr != nil {
|
|
|
+ hostinfo.logger(f.l).Error("Failed to encrypt outgoing packet",
|
|
|
+ "error", encErr,
|
|
|
+ "udpAddr", hostinfo.remote,
|
|
|
+ "counter", c,
|
|
|
+ )
|
|
|
+ // Skip this segment; the rest of the superpacket can still
|
|
|
+ // go out — TCP will retransmit anything we drop here.
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ return out
|
|
|
+}
|
|
|
+
|
|
|
+// sendInsideMessage encrypts a firewall-approved inside packet (or every
|
|
|
+// segment of a TSO/USO superpacket) into the caller's batch slot for
|
|
|
+// later sendmmsg flush. Segmentation is fused with encryption here so the
|
|
|
+// kernel-supplied superpacket bytes never get written into a separate
|
|
|
+// scratch arena: SegmentSuperpacket builds each segment's plaintext in
|
|
|
+// segScratch[:segLen] in turn, and we encrypt directly into a fresh
|
|
|
+// SendBatch slot.
|
|
|
+func (f *Interface) sendInsideMessage(hostinfo *HostInfo, pkt tio.Packet, nb []byte, sendBatch batch.TxBatcher) {
|
|
|
+ ci := hostinfo.ConnectionState
|
|
|
+ if ci.eKey == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if hostinfo.lastRebindCount != f.rebindCount {
|
|
|
+ //NOTE: there is an update hole if a tunnel isn't used and exactly 256 rebinds occur before the tunnel is
|
|
|
+ // finally used again. This tunnel would eventually be torn down and recreated if this action didn't help.
|
|
|
+ f.lightHouse.QueryServer(hostinfo.vpnAddrs[0])
|
|
|
+ hostinfo.lastRebindCount = f.rebindCount
|
|
|
+ if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
|
|
+ hostinfo.logger(f.l).Debug("Lighthouse update triggered for punch due to rebind counter",
|
|
|
+ "vpnAddrs", hostinfo.vpnAddrs,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if !hostinfo.remote.IsValid() { //the relay path
|
|
|
+ //first, find our relay hostinfo:
|
|
|
+ var relayHostInfo *HostInfo
|
|
|
+ var relay *Relay
|
|
|
+ var err error
|
|
|
+ for _, relayIP := range hostinfo.relayState.CopyRelayIps() {
|
|
|
+ relayHostInfo, relay, err = f.hostMap.QueryVpnAddrsRelayFor(hostinfo.vpnAddrs, relayIP)
|
|
|
+ if err != nil {
|
|
|
+ hostinfo.relayState.DeleteRelay(relayIP)
|
|
|
+ hostinfo.logger(f.l).Info("sendNoMetrics failed to find HostInfo",
|
|
|
+ "relay", relayIP,
|
|
|
+ "error", err,
|
|
|
+ )
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if relayHostInfo == nil || relay == nil {
|
|
|
+ //failure already logged
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err = tio.SegmentSuperpacket(pkt, func(seg []byte) error {
|
|
|
+ //relay header + header + plaintext + AEAD tag (16 bytes for both AES-GCM and ChaCha20-Poly1305) + relay tag
|
|
|
+ scratch := sendBatch.Reserve(header.Len + header.Len + len(seg) + 16 + 16)
|
|
|
+
|
|
|
+ innerPacket := f.sendInsideEncrypt(hostinfo, ci, seg, scratch[header.Len:], nb)
|
|
|
+ if innerPacket == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ //now we need to do a relay-encrypt:
|
|
|
+ toSend, err := f.prepareSendVia(relayHostInfo, relay, innerPacket, nb, scratch, true)
|
|
|
+ if err != nil {
|
|
|
+ //already logged
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ sendBatch.Commit(toSend, relayHostInfo.remote, 0)
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ hostinfo.logger(f.l).Error("Failed to segment superpacket for relay send", "error", err)
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err := tio.SegmentSuperpacket(pkt, func(seg []byte) error {
|
|
|
+ // header + plaintext + AEAD tag (16 bytes for both AES-GCM and ChaCha20-Poly1305)
|
|
|
+ scratch := sendBatch.Reserve(header.Len + len(seg) + 16)
|
|
|
+
|
|
|
+ out := f.sendInsideEncrypt(hostinfo, ci, seg, scratch, nb)
|
|
|
+ if out == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ sendBatch.Commit(out, hostinfo.remote, 0)
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ hostinfo.logger(f.l).Error("Failed to segment superpacket for send",
|
|
|
+ "error", err,
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (f *Interface) rejectInside(packet []byte, out []byte, q int) {
|
|
|
if !f.firewall.InSendReject {
|
|
|
return
|
|
|
@@ -275,21 +414,13 @@ func (f *Interface) sendTo(t header.MessageType, st header.MessageSubType, ci *C
|
|
|
f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out, 0)
|
|
|
}
|
|
|
|
|
|
-// SendVia sends a payload through a Relay tunnel. No authentication or encryption is done
|
|
|
-// to the payload for the ultimate target host, making this a useful method for sending
|
|
|
-// handshake messages to peers through relay tunnels.
|
|
|
-// via is the HostInfo through which the message is relayed.
|
|
|
-// ad is the plaintext data to authenticate, but not encrypt
|
|
|
-// nb is a buffer used to store the nonce value, re-used for performance reasons.
|
|
|
-// out is a buffer used to store the result of the Encrypt operation
|
|
|
-// q indicates which writer to use to send the packet.
|
|
|
-func (f *Interface) SendVia(via *HostInfo,
|
|
|
+func (f *Interface) prepareSendVia(via *HostInfo,
|
|
|
relay *Relay,
|
|
|
ad,
|
|
|
nb,
|
|
|
out []byte,
|
|
|
nocopy bool,
|
|
|
-) {
|
|
|
+) ([]byte, error) {
|
|
|
if noiseutil.EncryptLockNeeded {
|
|
|
// NOTE: for goboring AESGCMTLS we need to lock because of the nonce check
|
|
|
via.ConnectionState.writeLock.Lock()
|
|
|
@@ -311,7 +442,7 @@ func (f *Interface) SendVia(via *HostInfo,
|
|
|
"headerLen", len(out),
|
|
|
"cipherOverhead", via.ConnectionState.eKey.Overhead(),
|
|
|
)
|
|
|
- return
|
|
|
+ return nil, io.ErrShortBuffer
|
|
|
}
|
|
|
|
|
|
// The header bytes are written to the 'out' slice; Grow the slice to hold the header and associated data payload.
|
|
|
@@ -331,13 +462,36 @@ func (f *Interface) SendVia(via *HostInfo,
|
|
|
}
|
|
|
if err != nil {
|
|
|
via.logger(f.l).Info("Failed to EncryptDanger in sendVia", "error", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ f.connectionManager.RelayUsed(relay.LocalIndex)
|
|
|
+ return out, nil
|
|
|
+}
|
|
|
+
|
|
|
+// SendVia sends a payload through a Relay tunnel. No authentication or encryption is done
|
|
|
+// to the payload for the ultimate target host, making this a useful method for sending
|
|
|
+// handshake messages to peers through relay tunnels.
|
|
|
+// via is the HostInfo through which the message is relayed.
|
|
|
+// ad is the plaintext data to authenticate, but not encrypt
|
|
|
+// nb is a buffer used to store the nonce value, re-used for performance reasons.
|
|
|
+// out is a buffer used to store the result of the Encrypt operation
|
|
|
+// q indicates which writer to use to send the packet.
|
|
|
+func (f *Interface) SendVia(via *HostInfo,
|
|
|
+ relay *Relay,
|
|
|
+ ad,
|
|
|
+ nb,
|
|
|
+ out []byte,
|
|
|
+ nocopy bool,
|
|
|
+) {
|
|
|
+ toSend, err := f.prepareSendVia(via, relay, ad, nb, out, nocopy)
|
|
|
+ if err != nil {
|
|
|
+ via.logger(f.l).Info("Failed to prepareSendVia", "error", err)
|
|
|
return
|
|
|
}
|
|
|
- err = f.writers[0].WriteTo(out, via.remote)
|
|
|
+ err = f.writers[0].WriteTo(toSend, via.remote)
|
|
|
if err != nil {
|
|
|
via.logger(f.l).Info("Failed to WriteTo in sendVia", "error", err)
|
|
|
}
|
|
|
- f.connectionManager.RelayUsed(relay.LocalIndex)
|
|
|
}
|
|
|
|
|
|
func (f *Interface) sendNoMetrics(t header.MessageType, st header.MessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote netip.AddrPort, p, nb, out []byte, q int) {
|