mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-14 09:52:04 -05:00
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
313 lines
8.0 KiB
Go
313 lines
8.0 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package STUN generates STUN request packets and parses response packets.
|
|
package stun
|
|
|
|
import (
|
|
"bytes"
|
|
crand "crypto/rand"
|
|
"encoding/binary"
|
|
"errors"
|
|
"hash/crc32"
|
|
"net"
|
|
"net/netip"
|
|
)
|
|
|
|
const (
|
|
attrNumSoftware = 0x8022
|
|
attrNumFingerprint = 0x8028
|
|
attrMappedAddress = 0x0001
|
|
attrXorMappedAddress = 0x0020
|
|
// This alternative attribute type is not
|
|
// mentioned in the RFC, but the shift into
|
|
// the "comprehension-optional" range seems
|
|
// like an easy mistake for a server to make.
|
|
// And servers appear to send it.
|
|
attrXorMappedAddressAlt = 0x8020
|
|
|
|
software = "tailnode" // notably: 8 bytes long, so no padding
|
|
bindingRequest = "\x00\x01"
|
|
magicCookie = "\x21\x12\xa4\x42"
|
|
lenFingerprint = 8 // 2+byte header + 2-byte length + 4-byte crc32
|
|
headerLen = 20
|
|
)
|
|
|
|
// TxID is a transaction ID.
|
|
type TxID [12]byte
|
|
|
|
// NewTxID returns a new random TxID.
|
|
func NewTxID() TxID {
|
|
var tx TxID
|
|
if _, err := crand.Read(tx[:]); err != nil {
|
|
panic(err)
|
|
}
|
|
return tx
|
|
}
|
|
|
|
// Request generates a binding request STUN packet.
|
|
// The transaction ID, tID, should be a random sequence of bytes.
|
|
func Request(tID TxID) []byte {
|
|
// STUN header, RFC5389 Section 6.
|
|
const lenAttrSoftware = 4 + len(software)
|
|
b := make([]byte, 0, headerLen+lenAttrSoftware+lenFingerprint)
|
|
b = append(b, bindingRequest...)
|
|
b = appendU16(b, uint16(lenAttrSoftware+lenFingerprint)) // number of bytes following header
|
|
b = append(b, magicCookie...)
|
|
b = append(b, tID[:]...)
|
|
|
|
// Attribute SOFTWARE, RFC5389 Section 15.5.
|
|
b = appendU16(b, attrNumSoftware)
|
|
b = appendU16(b, uint16(len(software)))
|
|
b = append(b, software...)
|
|
|
|
// Attribute FINGERPRINT, RFC5389 Section 15.5.
|
|
fp := fingerPrint(b)
|
|
b = appendU16(b, attrNumFingerprint)
|
|
b = appendU16(b, 4)
|
|
b = appendU32(b, fp)
|
|
|
|
return b
|
|
}
|
|
|
|
func fingerPrint(b []byte) uint32 { return crc32.ChecksumIEEE(b) ^ 0x5354554e }
|
|
|
|
func appendU16(b []byte, v uint16) []byte {
|
|
return append(b, byte(v>>8), byte(v))
|
|
}
|
|
|
|
func appendU32(b []byte, v uint32) []byte {
|
|
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
|
|
}
|
|
|
|
// ParseBindingRequest parses a STUN binding request.
|
|
//
|
|
// It returns an error unless it advertises that it came from
|
|
// Tailscale.
|
|
func ParseBindingRequest(b []byte) (TxID, error) {
|
|
if !Is(b) {
|
|
return TxID{}, ErrNotSTUN
|
|
}
|
|
if string(b[:len(bindingRequest)]) != bindingRequest {
|
|
return TxID{}, ErrNotBindingRequest
|
|
}
|
|
var txID TxID
|
|
copy(txID[:], b[8:8+len(txID)])
|
|
var softwareOK bool
|
|
var lastAttr uint16
|
|
var gotFP uint32
|
|
if err := foreachAttr(b[headerLen:], func(attrType uint16, a []byte) error {
|
|
lastAttr = attrType
|
|
if attrType == attrNumSoftware && string(a) == software {
|
|
softwareOK = true
|
|
}
|
|
if attrType == attrNumFingerprint && len(a) == 4 {
|
|
gotFP = binary.BigEndian.Uint32(a)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return TxID{}, err
|
|
}
|
|
if !softwareOK {
|
|
return TxID{}, ErrWrongSoftware
|
|
}
|
|
if lastAttr != attrNumFingerprint {
|
|
return TxID{}, ErrNoFingerprint
|
|
}
|
|
wantFP := fingerPrint(b[:len(b)-lenFingerprint])
|
|
if gotFP != wantFP {
|
|
return TxID{}, ErrWrongFingerprint
|
|
}
|
|
return txID, nil
|
|
}
|
|
|
|
var (
|
|
ErrNotSTUN = errors.New("response is not a STUN packet")
|
|
ErrNotSuccessResponse = errors.New("STUN packet is not a response")
|
|
ErrMalformedAttrs = errors.New("STUN response has malformed attributes")
|
|
ErrNotBindingRequest = errors.New("STUN request not a binding request")
|
|
ErrWrongSoftware = errors.New("STUN request came from non-Tailscale software")
|
|
ErrNoFingerprint = errors.New("STUN request didn't end in fingerprint")
|
|
ErrWrongFingerprint = errors.New("STUN request had bogus fingerprint")
|
|
)
|
|
|
|
func foreachAttr(b []byte, fn func(attrType uint16, a []byte) error) error {
|
|
for len(b) > 0 {
|
|
if len(b) < 4 {
|
|
return ErrMalformedAttrs
|
|
}
|
|
attrType := binary.BigEndian.Uint16(b[:2])
|
|
attrLen := int(binary.BigEndian.Uint16(b[2:4]))
|
|
attrLenWithPad := (attrLen + 3) &^ 3
|
|
b = b[4:]
|
|
if attrLenWithPad > len(b) {
|
|
return ErrMalformedAttrs
|
|
}
|
|
if err := fn(attrType, b[:attrLen]); err != nil {
|
|
return err
|
|
}
|
|
b = b[attrLenWithPad:]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Response generates a binding response.
|
|
func Response(txID TxID, addrPort netip.AddrPort) []byte {
|
|
addr := addrPort.Addr()
|
|
|
|
var fam byte
|
|
if addr.Is4() {
|
|
fam = 1
|
|
} else if addr.Is6() {
|
|
fam = 2
|
|
} else {
|
|
return nil
|
|
}
|
|
attrsLen := 8 + addr.BitLen()/8
|
|
b := make([]byte, 0, headerLen+attrsLen)
|
|
|
|
// Header
|
|
b = append(b, 0x01, 0x01) // success
|
|
b = appendU16(b, uint16(attrsLen))
|
|
b = append(b, magicCookie...)
|
|
b = append(b, txID[:]...)
|
|
|
|
// Attributes (well, one)
|
|
b = appendU16(b, attrXorMappedAddress)
|
|
b = appendU16(b, uint16(4+addr.BitLen()/8))
|
|
b = append(b,
|
|
0, // unused byte
|
|
fam)
|
|
b = appendU16(b, addrPort.Port()^0x2112) // first half of magicCookie
|
|
ipa := addr.As16()
|
|
for i, o := range ipa[16-addr.BitLen()/8:] {
|
|
if i < 4 {
|
|
b = append(b, o^magicCookie[i])
|
|
} else {
|
|
b = append(b, o^txID[i-len(magicCookie)])
|
|
}
|
|
}
|
|
return b
|
|
}
|
|
|
|
// ParseResponse parses a successful binding response STUN packet.
|
|
// The IP address is extracted from the XOR-MAPPED-ADDRESS attribute.
|
|
func ParseResponse(b []byte) (tID TxID, addr netip.AddrPort, err error) {
|
|
if !Is(b) {
|
|
return tID, netip.AddrPort{}, ErrNotSTUN
|
|
}
|
|
copy(tID[:], b[8:8+len(tID)])
|
|
if b[0] != 0x01 || b[1] != 0x01 {
|
|
return tID, netip.AddrPort{}, ErrNotSuccessResponse
|
|
}
|
|
attrsLen := int(binary.BigEndian.Uint16(b[2:4]))
|
|
b = b[headerLen:] // remove STUN header
|
|
if attrsLen > len(b) {
|
|
return tID, netip.AddrPort{}, ErrMalformedAttrs
|
|
} else if len(b) > attrsLen {
|
|
b = b[:attrsLen] // trim trailing packet bytes
|
|
}
|
|
|
|
var fallbackAddr netip.AddrPort
|
|
|
|
// Read through the attributes.
|
|
// The the addr+port reported by XOR-MAPPED-ADDRESS
|
|
// as the canonical value. If the attribute is not
|
|
// present but the STUN server responds with
|
|
// MAPPED-ADDRESS we fall back to it.
|
|
if err := foreachAttr(b, func(attrType uint16, attr []byte) error {
|
|
switch attrType {
|
|
case attrXorMappedAddress, attrXorMappedAddressAlt:
|
|
ipSlice, port, err := xorMappedAddress(tID, attr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if ip, ok := netip.AddrFromSlice(ipSlice); ok {
|
|
addr = netip.AddrPortFrom(ip.Unmap(), port)
|
|
}
|
|
case attrMappedAddress:
|
|
ipSlice, port, err := mappedAddress(attr)
|
|
if err != nil {
|
|
return ErrMalformedAttrs
|
|
}
|
|
if ip, ok := netip.AddrFromSlice(ipSlice); ok {
|
|
fallbackAddr = netip.AddrPortFrom(ip.Unmap(), port)
|
|
}
|
|
}
|
|
return nil
|
|
|
|
}); err != nil {
|
|
return TxID{}, netip.AddrPort{}, err
|
|
}
|
|
|
|
if addr.IsValid() {
|
|
return tID, addr, nil
|
|
}
|
|
if fallbackAddr.IsValid() {
|
|
return tID, fallbackAddr, nil
|
|
}
|
|
return tID, netip.AddrPort{}, ErrMalformedAttrs
|
|
}
|
|
|
|
func xorMappedAddress(tID TxID, b []byte) (addr []byte, port uint16, err error) {
|
|
// XOR-MAPPED-ADDRESS attribute, RFC5389 Section 15.2
|
|
if len(b) < 4 {
|
|
return nil, 0, ErrMalformedAttrs
|
|
}
|
|
xorPort := binary.BigEndian.Uint16(b[2:4])
|
|
addrField := b[4:]
|
|
port = xorPort ^ 0x2112 // first half of magicCookie
|
|
|
|
addrLen := familyAddrLen(b[1])
|
|
if addrLen == 0 {
|
|
return nil, 0, ErrMalformedAttrs
|
|
}
|
|
if len(addrField) < addrLen {
|
|
return nil, 0, ErrMalformedAttrs
|
|
}
|
|
xorAddr := addrField[:addrLen]
|
|
addr = make([]byte, addrLen)
|
|
for i := range xorAddr {
|
|
if i < len(magicCookie) {
|
|
addr[i] = xorAddr[i] ^ magicCookie[i]
|
|
} else {
|
|
addr[i] = xorAddr[i] ^ tID[i-len(magicCookie)]
|
|
}
|
|
}
|
|
return addr, port, nil
|
|
}
|
|
|
|
func familyAddrLen(fam byte) int {
|
|
switch fam {
|
|
case 0x01: // IPv4
|
|
return net.IPv4len
|
|
case 0x02: // IPv6
|
|
return net.IPv6len
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func mappedAddress(b []byte) (addr []byte, port uint16, err error) {
|
|
if len(b) < 4 {
|
|
return nil, 0, ErrMalformedAttrs
|
|
}
|
|
port = uint16(b[2])<<8 | uint16(b[3])
|
|
addrField := b[4:]
|
|
addrLen := familyAddrLen(b[1])
|
|
if addrLen == 0 {
|
|
return nil, 0, ErrMalformedAttrs
|
|
}
|
|
if len(addrField) < addrLen {
|
|
return nil, 0, ErrMalformedAttrs
|
|
}
|
|
return bytes.Clone(addrField[:addrLen]), port, nil
|
|
}
|
|
|
|
// Is reports whether b is a STUN message.
|
|
func Is(b []byte) bool {
|
|
return len(b) >= headerLen &&
|
|
b[0]&0b11000000 == 0 && // top two bits must be zero
|
|
string(b[4:8]) == magicCookie
|
|
}
|