2021-01-12 20:03:41 +00:00
|
|
|
// Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
// TSMP is our ICMP-like "Tailscale Message Protocol" for signaling
|
|
|
|
// Tailscale-specific messages between nodes. It uses IP protocol 99
|
|
|
|
// (reserved for "any private encryption scheme") within
|
|
|
|
// Wireguard's normal encryption between peers and never hits the host
|
|
|
|
// network stack.
|
|
|
|
|
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/flowtrack"
|
2021-03-20 04:05:51 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2021-01-12 20:03:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TailscaleRejectedHeader is a TSMP message that says that one
|
|
|
|
// Tailscale node has rejected the connection from another. Unlike a
|
|
|
|
// TCP RST, this includes a reason.
|
|
|
|
//
|
2021-02-02 23:05:51 +00:00
|
|
|
// On the wire, after the IP header, it's currently 7 or 8 bytes:
|
2021-01-12 20:03:41 +00:00
|
|
|
// * '!'
|
|
|
|
// * IPProto byte (IANA protocol number: TCP or UDP)
|
|
|
|
// * 'A' or 'S' (RejectedDueToACLs, RejectedDueToShieldsUp)
|
|
|
|
// * srcPort big endian uint16
|
|
|
|
// * dstPort big endian uint16
|
2021-02-02 23:05:51 +00:00
|
|
|
// * [optional] byte of flag bits:
|
|
|
|
// lowest bit (0x1): MaybeBroken
|
2021-01-12 20:03:41 +00:00
|
|
|
//
|
|
|
|
// In the future it might also accept 16 byte IP flow src/dst IPs
|
|
|
|
// after the header, if they're different than the IP-level ones.
|
|
|
|
type TailscaleRejectedHeader struct {
|
|
|
|
IPSrc netaddr.IP // IPv4 or IPv6 header's src IP
|
|
|
|
IPDst netaddr.IP // IPv4 or IPv6 header's dst IP
|
|
|
|
Src netaddr.IPPort // rejected flow's src
|
|
|
|
Dst netaddr.IPPort // rejected flow's dst
|
2021-03-20 04:05:51 +00:00
|
|
|
Proto ipproto.Proto // proto that was rejected (TCP or UDP)
|
2021-01-12 20:03:41 +00:00
|
|
|
Reason TailscaleRejectReason // why the connection was rejected
|
2021-02-02 23:05:51 +00:00
|
|
|
|
|
|
|
// MaybeBroken is whether the rejection is non-terminal (the
|
|
|
|
// client should not fail immediately). This is sent by a
|
|
|
|
// target when it's not sure whether it's totally broken, but
|
|
|
|
// it might be. For example, the target tailscaled might think
|
|
|
|
// its host firewall or IP forwarding aren't configured
|
|
|
|
// properly, but tailscaled might be wrong (not having enough
|
|
|
|
// visibility into what the OS is doing). When true, the
|
|
|
|
// message is simply an FYI as a potential reason to use for
|
|
|
|
// later when the pendopen connection tracking timer expires.
|
|
|
|
MaybeBroken bool
|
2021-01-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 23:05:51 +00:00
|
|
|
const rejectFlagBitMaybeBroken = 0x1
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
func (rh TailscaleRejectedHeader) Flow() flowtrack.Tuple {
|
2021-03-20 04:05:51 +00:00
|
|
|
return flowtrack.Tuple{Proto: rh.Proto, Src: rh.Src, Dst: rh.Dst}
|
2021-01-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rh TailscaleRejectedHeader) String() string {
|
|
|
|
return fmt.Sprintf("TSMP-reject-flow{%s %s > %s}: %s", rh.Proto, rh.Src, rh.Dst, rh.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TSMPType uint8
|
|
|
|
|
|
|
|
const (
|
2021-02-02 23:05:51 +00:00
|
|
|
// TSMPTypeRejectedConn is the type byte for a TailscaleRejectedHeader.
|
2021-01-12 20:03:41 +00:00
|
|
|
TSMPTypeRejectedConn TSMPType = '!'
|
2021-03-23 22:16:15 +00:00
|
|
|
|
|
|
|
// TSMPTypePing is the type byte for a TailscalePingRequest.
|
|
|
|
TSMPTypePing TSMPType = 'p'
|
|
|
|
|
|
|
|
// TSMPTypePong is the type byte for a TailscalePongResponse.
|
|
|
|
TSMPTypePong TSMPType = 'o'
|
2021-01-12 20:03:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TailscaleRejectReason byte
|
|
|
|
|
2021-02-02 23:05:51 +00:00
|
|
|
// IsZero reports whether r is the zero value, representing no rejection.
|
|
|
|
func (r TailscaleRejectReason) IsZero() bool { return r == TailscaleRejectReasonNone }
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
const (
|
2021-02-02 23:05:51 +00:00
|
|
|
// TailscaleRejectReasonNone is the TailscaleRejectReason zero value.
|
|
|
|
TailscaleRejectReasonNone TailscaleRejectReason = 0
|
|
|
|
|
|
|
|
// RejectedDueToACLs means that the host rejected the connection due to ACLs.
|
|
|
|
RejectedDueToACLs TailscaleRejectReason = 'A'
|
|
|
|
|
|
|
|
// RejectedDueToShieldsUp means that the host rejected the connection due to shields being up.
|
2021-01-12 20:03:41 +00:00
|
|
|
RejectedDueToShieldsUp TailscaleRejectReason = 'S'
|
2021-02-02 23:05:51 +00:00
|
|
|
|
|
|
|
// RejectedDueToIPForwarding means that the relay node's IP
|
|
|
|
// forwarding is disabled.
|
|
|
|
RejectedDueToIPForwarding TailscaleRejectReason = 'F'
|
|
|
|
|
|
|
|
// RejectedDueToHostFirewall means that the target host's
|
|
|
|
// firewall is blocking the traffic.
|
|
|
|
RejectedDueToHostFirewall TailscaleRejectReason = 'W'
|
2021-01-12 20:03:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r TailscaleRejectReason) String() string {
|
|
|
|
switch r {
|
|
|
|
case RejectedDueToACLs:
|
|
|
|
return "acl"
|
|
|
|
case RejectedDueToShieldsUp:
|
|
|
|
return "shields"
|
2021-02-02 23:05:51 +00:00
|
|
|
case RejectedDueToIPForwarding:
|
|
|
|
return "host-ip-forwarding-unavailable"
|
|
|
|
case RejectedDueToHostFirewall:
|
|
|
|
return "host-firewall"
|
2021-01-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("0x%02x", byte(r))
|
|
|
|
}
|
|
|
|
|
2021-02-02 23:05:51 +00:00
|
|
|
func (h TailscaleRejectedHeader) hasFlags() bool {
|
|
|
|
return h.MaybeBroken // the only one currently
|
|
|
|
}
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
func (h TailscaleRejectedHeader) Len() int {
|
2021-02-02 23:05:51 +00:00
|
|
|
v := 1 + // TSMPType byte
|
2021-01-12 20:03:41 +00:00
|
|
|
1 + // IPProto byte
|
|
|
|
1 + // TailscaleRejectReason byte
|
|
|
|
2*2 // 2 uint16 ports
|
2021-02-02 23:05:51 +00:00
|
|
|
if h.IPSrc.Is4() {
|
|
|
|
v += ip4HeaderLength
|
|
|
|
} else if h.IPSrc.Is6() {
|
|
|
|
v += ip6HeaderLength
|
|
|
|
}
|
|
|
|
if h.hasFlags() {
|
|
|
|
v++
|
|
|
|
}
|
|
|
|
return v
|
2021-01-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h TailscaleRejectedHeader) Marshal(buf []byte) error {
|
|
|
|
if len(buf) < h.Len() {
|
|
|
|
return errSmallBuffer
|
|
|
|
}
|
|
|
|
if len(buf) > maxPacketLength {
|
|
|
|
return errLargePacket
|
|
|
|
}
|
|
|
|
if h.Src.IP.Is4() {
|
|
|
|
iph := IP4Header{
|
2021-03-21 04:45:47 +00:00
|
|
|
IPProto: ipproto.TSMP,
|
2021-01-12 20:03:41 +00:00
|
|
|
Src: h.IPSrc,
|
|
|
|
Dst: h.IPDst,
|
|
|
|
}
|
|
|
|
iph.Marshal(buf)
|
|
|
|
buf = buf[ip4HeaderLength:]
|
|
|
|
} else if h.Src.IP.Is6() {
|
|
|
|
iph := IP6Header{
|
2021-03-21 04:45:47 +00:00
|
|
|
IPProto: ipproto.TSMP,
|
2021-01-12 20:03:41 +00:00
|
|
|
Src: h.IPSrc,
|
|
|
|
Dst: h.IPDst,
|
|
|
|
}
|
|
|
|
iph.Marshal(buf)
|
|
|
|
buf = buf[ip6HeaderLength:]
|
|
|
|
} else {
|
|
|
|
return errors.New("bogus src IP")
|
|
|
|
}
|
|
|
|
buf[0] = byte(TSMPTypeRejectedConn)
|
|
|
|
buf[1] = byte(h.Proto)
|
|
|
|
buf[2] = byte(h.Reason)
|
|
|
|
binary.BigEndian.PutUint16(buf[3:5], h.Src.Port)
|
|
|
|
binary.BigEndian.PutUint16(buf[5:7], h.Dst.Port)
|
2021-02-02 23:05:51 +00:00
|
|
|
|
|
|
|
if h.hasFlags() {
|
|
|
|
var flags byte
|
|
|
|
if h.MaybeBroken {
|
|
|
|
flags |= rejectFlagBitMaybeBroken
|
|
|
|
}
|
|
|
|
buf[7] = flags
|
|
|
|
}
|
2021-01-12 20:03:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsTailscaleRejectedHeader parses pp as an incoming rejection
|
|
|
|
// connection TSMP message.
|
|
|
|
//
|
|
|
|
// ok reports whether pp was a valid TSMP rejection packet.
|
|
|
|
func (pp *Parsed) AsTailscaleRejectedHeader() (h TailscaleRejectedHeader, ok bool) {
|
|
|
|
p := pp.Payload()
|
|
|
|
if len(p) < 7 || p[0] != byte(TSMPTypeRejectedConn) {
|
|
|
|
return
|
|
|
|
}
|
2021-02-02 23:05:51 +00:00
|
|
|
h = TailscaleRejectedHeader{
|
2021-03-20 04:05:51 +00:00
|
|
|
Proto: ipproto.Proto(p[1]),
|
2021-01-12 20:03:41 +00:00
|
|
|
Reason: TailscaleRejectReason(p[2]),
|
|
|
|
IPSrc: pp.Src.IP,
|
|
|
|
IPDst: pp.Dst.IP,
|
|
|
|
Src: netaddr.IPPort{IP: pp.Dst.IP, Port: binary.BigEndian.Uint16(p[3:5])},
|
|
|
|
Dst: netaddr.IPPort{IP: pp.Src.IP, Port: binary.BigEndian.Uint16(p[5:7])},
|
2021-02-02 23:05:51 +00:00
|
|
|
}
|
|
|
|
if len(p) > 7 {
|
|
|
|
flags := p[7]
|
|
|
|
h.MaybeBroken = (flags & rejectFlagBitMaybeBroken) != 0
|
|
|
|
}
|
|
|
|
return h, true
|
2021-01-12 20:03:41 +00:00
|
|
|
}
|
2021-03-23 22:16:15 +00:00
|
|
|
|
|
|
|
// TSMPPingRequest is a TSMP message that's like an ICMP ping request.
|
|
|
|
//
|
|
|
|
// On the wire, after the IP header, it's currently 9 bytes:
|
|
|
|
// * 'p' (TSMPTypePing)
|
|
|
|
// * 8 opaque ping bytes to copy back in the response
|
|
|
|
type TSMPPingRequest struct {
|
|
|
|
Data [8]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *Parsed) AsTSMPPing() (h TSMPPingRequest, ok bool) {
|
|
|
|
if pp.IPProto != ipproto.TSMP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := pp.Payload()
|
|
|
|
if len(p) < 9 || p[0] != byte(TSMPTypePing) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
copy(h.Data[:], p[1:])
|
|
|
|
return h, true
|
|
|
|
}
|
|
|
|
|
|
|
|
type TSMPPongReply struct {
|
2021-03-29 22:17:05 +00:00
|
|
|
IPHeader Header
|
|
|
|
Data [8]byte
|
|
|
|
PeerAPIPort uint16
|
2021-03-23 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 22:17:05 +00:00
|
|
|
// AsTSMPPong returns pp as a TSMPPongReply and whether it is one.
|
|
|
|
// The pong.IPHeader field is not populated.
|
|
|
|
func (pp *Parsed) AsTSMPPong() (pong TSMPPongReply, ok bool) {
|
2021-03-23 22:16:15 +00:00
|
|
|
if pp.IPProto != ipproto.TSMP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := pp.Payload()
|
|
|
|
if len(p) < 9 || p[0] != byte(TSMPTypePong) {
|
|
|
|
return
|
|
|
|
}
|
2021-03-29 22:17:05 +00:00
|
|
|
copy(pong.Data[:], p[1:])
|
|
|
|
if len(p) >= 11 {
|
|
|
|
pong.PeerAPIPort = binary.BigEndian.Uint16(p[9:])
|
|
|
|
}
|
|
|
|
return pong, true
|
2021-03-23 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h TSMPPongReply) Len() int {
|
2021-03-29 22:17:05 +00:00
|
|
|
return h.IPHeader.Len() + 11
|
2021-03-23 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h TSMPPongReply) Marshal(buf []byte) error {
|
|
|
|
if len(buf) < h.Len() {
|
|
|
|
return errSmallBuffer
|
|
|
|
}
|
|
|
|
if err := h.IPHeader.Marshal(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf = buf[h.IPHeader.Len():]
|
|
|
|
buf[0] = byte(TSMPTypePong)
|
|
|
|
copy(buf[1:], h.Data[:])
|
2021-03-29 22:17:05 +00:00
|
|
|
binary.BigEndian.PutUint16(buf[9:11], h.PeerAPIPort)
|
2021-03-23 22:16:15 +00:00
|
|
|
return nil
|
|
|
|
}
|