2020-06-30 04:54:34 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package disco contains the discovery message types.
|
|
|
|
//
|
|
|
|
// A discovery message is:
|
|
|
|
//
|
|
|
|
// Header:
|
2022-08-02 16:33:46 +00:00
|
|
|
//
|
|
|
|
// magic [6]byte // “TS💬” (0x54 53 f0 9f 92 ac)
|
|
|
|
// senderDiscoPub [32]byte // nacl public key
|
|
|
|
// nonce [24]byte
|
2020-06-30 04:54:34 +00:00
|
|
|
//
|
|
|
|
// The recipient then decrypts the bytes following (the nacl secretbox)
|
|
|
|
// and then the inner payload structure is:
|
|
|
|
//
|
2022-09-25 18:29:55 +00:00
|
|
|
// messageType byte (the MessageType constants below)
|
|
|
|
// messageVersion byte (0 for now; but always ignore bytes at the end)
|
|
|
|
// message-payload [...]byte
|
2020-06-30 04:54:34 +00:00
|
|
|
package disco
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2020-06-30 04:54:34 +00:00
|
|
|
|
2021-10-30 00:16:20 +00:00
|
|
|
"go4.org/mem"
|
|
|
|
"tailscale.com/types/key"
|
2020-06-30 04:54:34 +00:00
|
|
|
)
|
|
|
|
|
2020-07-01 19:56:17 +00:00
|
|
|
// Magic is the 6 byte header of all discovery messages.
|
|
|
|
const Magic = "TS💬" // 6 bytes: 0x54 53 f0 9f 92 ac
|
|
|
|
|
2020-07-16 19:58:35 +00:00
|
|
|
const keyLen = 32
|
|
|
|
|
2020-07-01 19:56:17 +00:00
|
|
|
// NonceLen is the length of the nonces used by nacl secretboxes.
|
|
|
|
const NonceLen = 24
|
|
|
|
|
2020-06-30 04:54:34 +00:00
|
|
|
type MessageType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypePing = MessageType(0x01)
|
|
|
|
TypePong = MessageType(0x02)
|
|
|
|
TypeCallMeMaybe = MessageType(0x03)
|
|
|
|
)
|
|
|
|
|
|
|
|
const v0 = byte(0)
|
|
|
|
|
|
|
|
var errShort = errors.New("short message")
|
|
|
|
|
2020-07-16 19:58:35 +00:00
|
|
|
// LooksLikeDiscoWrapper reports whether p looks like it's a packet
|
|
|
|
// containing an encrypted disco message.
|
|
|
|
func LooksLikeDiscoWrapper(p []byte) bool {
|
|
|
|
if len(p) < len(Magic)+keyLen+NonceLen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return string(p[:len(Magic)]) == Magic
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:21:40 +00:00
|
|
|
// Source returns the slice of p that represents the
|
|
|
|
// disco public key source, and whether p looks like
|
|
|
|
// a disco message.
|
|
|
|
func Source(p []byte) (src []byte, ok bool) {
|
|
|
|
if !LooksLikeDiscoWrapper(p) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return p[len(Magic):][:keyLen], true
|
|
|
|
}
|
|
|
|
|
2020-06-30 04:54:34 +00:00
|
|
|
// Parse parses the encrypted part of the message from inside the
|
|
|
|
// nacl secretbox.
|
|
|
|
func Parse(p []byte) (Message, error) {
|
|
|
|
if len(p) < 2 {
|
|
|
|
return nil, errShort
|
|
|
|
}
|
|
|
|
t, ver, p := MessageType(p[0]), p[1], p[2:]
|
|
|
|
switch t {
|
|
|
|
case TypePing:
|
|
|
|
return parsePing(ver, p)
|
|
|
|
case TypePong:
|
|
|
|
return parsePong(ver, p)
|
|
|
|
case TypeCallMeMaybe:
|
2021-01-20 19:39:42 +00:00
|
|
|
return parseCallMeMaybe(ver, p)
|
2020-06-30 04:54:34 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown message type 0x%02x", byte(t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message a discovery message.
|
|
|
|
type Message interface {
|
|
|
|
// AppendMarshal appends the message's marshaled representation.
|
|
|
|
AppendMarshal([]byte) []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendMsgHeader appends two bytes (for t and ver) and then also
|
|
|
|
// dataLen bytes to b, returning the appended slice in all. The
|
|
|
|
// returned data slice is a subslice of all with just dataLen bytes of
|
|
|
|
// where the caller will fill in the data.
|
|
|
|
func appendMsgHeader(b []byte, t MessageType, ver uint8, dataLen int) (all, data []byte) {
|
|
|
|
// TODO: optimize this?
|
|
|
|
all = append(b, make([]byte, dataLen+2)...)
|
|
|
|
all[len(b)] = byte(t)
|
|
|
|
all[len(b)+1] = ver
|
|
|
|
data = all[len(b)+2:]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type Ping struct {
|
2021-10-16 21:55:26 +00:00
|
|
|
// TxID is a random client-generated per-ping transaction ID.
|
2020-06-30 04:54:34 +00:00
|
|
|
TxID [12]byte
|
2021-10-16 21:55:26 +00:00
|
|
|
|
2021-10-18 17:50:13 +00:00
|
|
|
// NodeKey is allegedly the ping sender's wireguard public key.
|
|
|
|
// Old clients (~1.16.0 and earlier) don't send this field.
|
|
|
|
// It shouldn't be trusted by itself, but can be combined with
|
|
|
|
// netmap data to reduce the discokey:nodekey relation from 1:N to
|
|
|
|
// 1:1.
|
2021-10-30 00:16:20 +00:00
|
|
|
NodeKey key.NodePublic
|
2020-06-30 04:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Ping) AppendMarshal(b []byte) []byte {
|
2021-10-16 21:55:26 +00:00
|
|
|
dataLen := 12
|
|
|
|
hasKey := !m.NodeKey.IsZero()
|
|
|
|
if hasKey {
|
2021-10-30 00:35:51 +00:00
|
|
|
dataLen += key.NodePublicRawLen
|
2021-10-16 21:55:26 +00:00
|
|
|
}
|
|
|
|
ret, d := appendMsgHeader(b, TypePing, v0, dataLen)
|
|
|
|
n := copy(d, m.TxID[:])
|
|
|
|
if hasKey {
|
2021-10-30 00:16:20 +00:00
|
|
|
m.NodeKey.AppendTo(d[:n])
|
2021-10-16 21:55:26 +00:00
|
|
|
}
|
2020-06-30 04:54:34 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePing(ver uint8, p []byte) (m *Ping, err error) {
|
|
|
|
if len(p) < 12 {
|
|
|
|
return nil, errShort
|
|
|
|
}
|
|
|
|
m = new(Ping)
|
2021-10-16 21:55:26 +00:00
|
|
|
p = p[copy(m.TxID[:], p):]
|
2021-10-30 00:16:20 +00:00
|
|
|
// Deliberately lax on longer-than-expected messages, for future
|
|
|
|
// compatibility.
|
2021-10-30 00:35:51 +00:00
|
|
|
if len(p) >= key.NodePublicRawLen {
|
|
|
|
m.NodeKey = key.NodePublicFromRaw32(mem.B(p[:key.NodePublicRawLen]))
|
2021-10-16 21:55:26 +00:00
|
|
|
}
|
2020-06-30 04:54:34 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallMeMaybe is a message sent only over DERP to request that the recipient try
|
|
|
|
// to open up a magicsock path back to the sender.
|
|
|
|
//
|
|
|
|
// The sender should've already sent UDP packets to the peer to open
|
|
|
|
// up the stateful firewall mappings inbound.
|
|
|
|
//
|
|
|
|
// The recipient may choose to not open a path back, if it's already
|
|
|
|
// happy with its path. But usually it will.
|
2021-01-20 19:39:42 +00:00
|
|
|
type CallMeMaybe struct {
|
|
|
|
// MyNumber is what the peer believes its endpoints are.
|
2021-01-20 20:41:25 +00:00
|
|
|
//
|
|
|
|
// Prior to Tailscale 1.4, the endpoints were exchanged purely
|
|
|
|
// between nodes and the control server.
|
|
|
|
//
|
|
|
|
// Starting with Tailscale 1.4, clients advertise their endpoints.
|
|
|
|
// Older clients won't use this, but newer clients should
|
|
|
|
// use any endpoints in here that aren't included from control.
|
|
|
|
//
|
|
|
|
// Control might have sent stale endpoints if the client was idle
|
|
|
|
// before contacting us. In that case, the client likely did a STUN
|
|
|
|
// request immediately before sending the CallMeMaybe to recreate
|
|
|
|
// their NAT port mapping, and that new good endpoint is included
|
|
|
|
// in this field, but might not yet be in control's endpoints.
|
|
|
|
// (And in the future, control will stop distributing endpoints
|
|
|
|
// when clients are suitably new.)
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
MyNumber []netip.AddrPort
|
2021-01-20 19:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const epLength = 16 + 2 // 16 byte IP address + 2 byte port
|
2020-06-30 04:54:34 +00:00
|
|
|
|
2021-01-20 19:39:42 +00:00
|
|
|
func (m *CallMeMaybe) AppendMarshal(b []byte) []byte {
|
|
|
|
ret, p := appendMsgHeader(b, TypeCallMeMaybe, v0, epLength*len(m.MyNumber))
|
|
|
|
for _, ipp := range m.MyNumber {
|
2022-07-25 03:08:42 +00:00
|
|
|
a := ipp.Addr().As16()
|
2021-01-20 19:39:42 +00:00
|
|
|
copy(p[:], a[:])
|
2021-05-15 01:07:28 +00:00
|
|
|
binary.BigEndian.PutUint16(p[16:], ipp.Port())
|
2021-01-20 19:39:42 +00:00
|
|
|
p = p[epLength:]
|
|
|
|
}
|
2020-06-30 04:54:34 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:39:42 +00:00
|
|
|
func parseCallMeMaybe(ver uint8, p []byte) (m *CallMeMaybe, err error) {
|
|
|
|
m = new(CallMeMaybe)
|
|
|
|
if len(p)%epLength != 0 || ver != 0 || len(p) == 0 {
|
|
|
|
return m, nil
|
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
m.MyNumber = make([]netip.AddrPort, 0, len(p)/epLength)
|
2021-01-20 19:39:42 +00:00
|
|
|
for len(p) > 0 {
|
|
|
|
var a [16]byte
|
|
|
|
copy(a[:], p)
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
m.MyNumber = append(m.MyNumber, netip.AddrPortFrom(
|
2022-08-02 20:38:11 +00:00
|
|
|
netip.AddrFrom16(a).Unmap(),
|
2021-05-15 01:07:28 +00:00
|
|
|
binary.BigEndian.Uint16(p[16:18])))
|
2021-01-20 19:39:42 +00:00
|
|
|
p = p[epLength:]
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 04:54:34 +00:00
|
|
|
// Pong is a response a Ping.
|
|
|
|
//
|
|
|
|
// It includes the sender's source IP + port, so it's effectively a
|
|
|
|
// STUN response.
|
|
|
|
type Pong struct {
|
|
|
|
TxID [12]byte
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Src netip.AddrPort // 18 bytes (16+2) on the wire; v4-mapped ipv6 for IPv4
|
2020-06-30 04:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pongLen = 12 + 16 + 2
|
|
|
|
|
|
|
|
func (m *Pong) AppendMarshal(b []byte) []byte {
|
|
|
|
ret, d := appendMsgHeader(b, TypePong, v0, pongLen)
|
|
|
|
d = d[copy(d, m.TxID[:]):]
|
2022-07-25 03:08:42 +00:00
|
|
|
ip16 := m.Src.Addr().As16()
|
2020-06-30 04:54:34 +00:00
|
|
|
d = d[copy(d, ip16[:]):]
|
2021-05-15 01:07:28 +00:00
|
|
|
binary.BigEndian.PutUint16(d, m.Src.Port())
|
2020-06-30 04:54:34 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePong(ver uint8, p []byte) (m *Pong, err error) {
|
|
|
|
if len(p) < pongLen {
|
|
|
|
return nil, errShort
|
|
|
|
}
|
|
|
|
m = new(Pong)
|
|
|
|
copy(m.TxID[:], p)
|
|
|
|
p = p[12:]
|
|
|
|
|
2022-08-02 20:38:11 +00:00
|
|
|
srcIP, _ := netip.AddrFromSlice(net.IP(p[:16]))
|
2020-06-30 04:54:34 +00:00
|
|
|
p = p[16:]
|
2021-05-15 01:07:28 +00:00
|
|
|
port := binary.BigEndian.Uint16(p)
|
2022-08-02 20:38:11 +00:00
|
|
|
m.Src = netip.AddrPortFrom(srcIP.Unmap(), port)
|
2020-06-30 04:54:34 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
2020-07-02 17:48:13 +00:00
|
|
|
|
|
|
|
// MessageSummary returns a short summary of m for logging purposes.
|
|
|
|
func MessageSummary(m Message) string {
|
|
|
|
switch m := m.(type) {
|
|
|
|
case *Ping:
|
2020-07-03 18:06:33 +00:00
|
|
|
return fmt.Sprintf("ping tx=%x", m.TxID[:6])
|
2020-07-02 17:48:13 +00:00
|
|
|
case *Pong:
|
2020-07-03 18:06:33 +00:00
|
|
|
return fmt.Sprintf("pong tx=%x", m.TxID[:6])
|
2021-01-20 19:39:42 +00:00
|
|
|
case *CallMeMaybe:
|
2020-07-02 17:52:23 +00:00
|
|
|
return "call-me-maybe"
|
2020-07-02 17:48:13 +00:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%#v", m)
|
|
|
|
}
|
|
|
|
}
|