transport

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 27 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RekeyAfterMessages      = (1 << 60)
	RejectAfterMessages     = (1 << 64) - (1 << 13) - 1
	RekeyAfterTime          = time.Second * 120
	RekeyAttemptTime        = time.Second * 90
	RekeyTimeout            = time.Second * 5
	MaxTimerHandshakes      = 90 / 5 /* RekeyAttemptTime / RekeyTimeout */
	RekeyTimeoutJitterMaxMs = 334
	RejectAfterTime         = time.Second * 180
	KeepaliveTimeout        = time.Second * 10
	CookieRefreshTime       = time.Second * 120
	HandshakeInitationRate  = time.Second / 50
	PaddingMultiple         = 16
)
View Source
const (
	MinMessageSize = MessageKeepaliveSize                  // minimum size of transport message (keepalive)
	MaxMessageSize = MaxSegmentSize                        // maximum size of transport message
	MaxContentSize = MaxSegmentSize - MessageTransportSize // maximum size of transport message content
)
View Source
const (
	UnderLoadAfterTime = time.Second // how long does the transport remain under load after detected
	MaxPeers           = 1 << 16     // maximum number of configured peers
)
View Source
const (
	NoiseConstruction = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"
	NoiseIdentifier   = "WireGuard v1 zx2c4 Jason@zx2c4.com"
	NoiseLabelMAC1    = "mac1----"
	NoiseLabelCookie  = "cookie--"
)
View Source
const (
	MessageInitiationType  = 1
	MessageResponseType    = 2
	MessageCookieReplyType = 3
	MessageTransportType   = 4
)
View Source
const (
	MessageInitiationSize      = 148                                           // size of handshake initiation message
	MessageResponseSize        = 92                                            // size of response message
	MessageCookieReplySize     = 64                                            // size of cookie reply message
	MessageTransportHeaderSize = 16                                            // size of data preceding content in transport message
	MessageTransportSize       = MessageTransportHeaderSize + poly1305.TagSize // size of empty transport
	MessageKeepaliveSize       = MessageTransportSize                          // size of keepalive
	MessageHandshakeSize       = MessageInitiationSize                         // size of largest handshake related message
)
View Source
const (
	MessageTransportOffsetReceiver = 4
	MessageTransportOffsetCounter  = 8
	MessageTransportOffsetContent  = 16
)
View Source
const (
	NoisePublicKeySize    = 32
	NoisePrivateKeySize   = 32
	NoisePresharedKeySize = 32
)
View Source
const (
	QueueStagedSize            = conn.IdealBatchSize
	QueueOutboundSize          = 1024
	QueueInboundSize           = 1024
	QueueHandshakeSize         = 1024
	MaxSegmentSize             = (1 << 16) - 1 // largest possible UDP datagram
	PreallocatedBuffersPerPool = 0             // Disable and allow for infinite memory growth
)
View Source
const DefaultMTU = 1420

Variables

View Source
var (
	InitialChainKey [blake2s.Size]byte
	InitialHash     [blake2s.Size]byte
	ZeroNonce       [chacha20poly1305.NonceSize]byte
)

Functions

func HMAC1

func HMAC1(sum *[blake2s.Size]byte, key, in0 []byte)

func HMAC2

func HMAC2(sum *[blake2s.Size]byte, key, in0, in1 []byte)

func KDF1

func KDF1(t0 *[blake2s.Size]byte, key, input []byte)

func KDF2

func KDF2(t0, t1 *[blake2s.Size]byte, key, input []byte)

func KDF3

func KDF3(t0, t1, t2 *[blake2s.Size]byte, key, input []byte)

Types

type CookieChecker

type CookieChecker struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*CookieChecker) CheckMAC1

func (st *CookieChecker) CheckMAC1(msg []byte) bool

func (*CookieChecker) CheckMAC2

func (st *CookieChecker) CheckMAC2(msg, src []byte) bool

func (*CookieChecker) CreateReply

func (st *CookieChecker) CreateReply(
	msg []byte,
	recv uint32,
	src []byte,
) (*MessageCookieReply, error)

func (*CookieChecker) Init

func (st *CookieChecker) Init(pk NoisePublicKey)

type CookieGenerator

type CookieGenerator struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*CookieGenerator) AddMacs

func (st *CookieGenerator) AddMacs(msg []byte)

func (*CookieGenerator) ConsumeReply

func (st *CookieGenerator) ConsumeReply(msg *MessageCookieReply) bool

func (*CookieGenerator) Init

func (st *CookieGenerator) Init(pk NoisePublicKey)

type Handshake

type Handshake struct {
	// contains filtered or unexported fields
}

func (*Handshake) Clear

func (h *Handshake) Clear()

type IndexTable

type IndexTable struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*IndexTable) Delete

func (table *IndexTable) Delete(index uint32)

func (*IndexTable) Init

func (table *IndexTable) Init()

func (*IndexTable) Lookup

func (table *IndexTable) Lookup(id uint32) IndexTableEntry

func (*IndexTable) NewIndexForHandshake

func (table *IndexTable) NewIndexForHandshake(peer *Peer, handshake *Handshake) (uint32, error)

func (*IndexTable) SwapIndexForKeypair

func (table *IndexTable) SwapIndexForKeypair(index uint32, keypair *Keypair)

type IndexTableEntry

type IndexTableEntry struct {
	// contains filtered or unexported fields
}

type Keypair

type Keypair struct {
	// contains filtered or unexported fields
}

type Keypairs

type Keypairs struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*Keypairs) Current

func (kp *Keypairs) Current() *Keypair

type MessageCookieReply

type MessageCookieReply struct {
	Type     uint32
	Receiver uint32
	Nonce    [chacha20poly1305.NonceSizeX]byte
	Cookie   [blake2s.Size128 + poly1305.TagSize]byte
}

type MessageInitiation

type MessageInitiation struct {
	Type      uint32
	Sender    uint32
	Ephemeral NoisePublicKey
	Static    [NoisePublicKeySize + poly1305.TagSize]byte
	Timestamp [tai64n.TimestampSize + poly1305.TagSize]byte
	MAC1      [blake2s.Size128]byte
	MAC2      [blake2s.Size128]byte
}

type MessageResponse

type MessageResponse struct {
	Type      uint32
	Sender    uint32
	Receiver  uint32
	Ephemeral NoisePublicKey
	Empty     [poly1305.TagSize]byte
	MAC1      [blake2s.Size128]byte
	MAC2      [blake2s.Size128]byte
}

type MessageTransport

type MessageTransport struct {
	Type     uint32
	Receiver uint32
	Counter  uint64
	Content  []byte
}

type NoiseNonce

type NoiseNonce uint64 // padded to 12-bytes

type NoisePresharedKey

type NoisePresharedKey [NoisePresharedKeySize]byte

type NoisePrivateKey

type NoisePrivateKey [NoisePrivateKeySize]byte

func NewPrivateKey

func NewPrivateKey() (sk NoisePrivateKey, err error)

func (NoisePrivateKey) Equals

func (key NoisePrivateKey) Equals(tar NoisePrivateKey) bool

func (*NoisePrivateKey) FromString

func (key *NoisePrivateKey) FromString(src string) error

func (NoisePrivateKey) IsZero

func (key NoisePrivateKey) IsZero() bool

func (*NoisePrivateKey) PublicKey

func (sk *NoisePrivateKey) PublicKey() (pk NoisePublicKey)

func (NoisePrivateKey) String

func (key NoisePrivateKey) String() string

type NoisePublicKey

type NoisePublicKey [NoisePublicKeySize]byte

func (NoisePublicKey) Equals

func (key NoisePublicKey) Equals(tar NoisePublicKey) bool

func (*NoisePublicKey) FromString

func (key *NoisePublicKey) FromString(src string) error

func (NoisePublicKey) IsZero

func (key NoisePublicKey) IsZero() bool

func (NoisePublicKey) String

func (key NoisePublicKey) String() string

type Peer

type Peer struct {
	// contains filtered or unexported fields
}

func (*Peer) BeginSymmetricSession

func (peer *Peer) BeginSymmetricSession() error

Derives a new keypair from the current handshake state *

func (*Peer) ExpireCurrentKeypairs

func (peer *Peer) ExpireCurrentKeypairs()

func (*Peer) FlushStagedPackets

func (peer *Peer) FlushStagedPackets()

func (*Peer) NewTimer

func (peer *Peer) NewTimer(expirationFunction func(*Peer)) *Timer

func (*Peer) ReceivedWithKeypair

func (peer *Peer) ReceivedWithKeypair(receivedKeypair *Keypair) bool

func (*Peer) RoutineSequentialReceiver

func (peer *Peer) RoutineSequentialReceiver(maxBatchSize int)

func (*Peer) RoutineSequentialSender

func (peer *Peer) RoutineSequentialSender(maxBatchSize int)

func (*Peer) SendBuffers

func (peer *Peer) SendBuffers(buffers [][]byte) error

func (*Peer) SendHandshakeInitiation

func (peer *Peer) SendHandshakeInitiation(isRetry bool) error

func (*Peer) SendHandshakeResponse

func (peer *Peer) SendHandshakeResponse() error

func (*Peer) SendKeepalive

func (peer *Peer) SendKeepalive() error

Queues a keepalive if no packets are queued for peer

func (*Peer) SendStagedPackets

func (peer *Peer) SendStagedPackets() error

func (*Peer) SetEndpointFromPacket

func (peer *Peer) SetEndpointFromPacket(endpoint conn.Endpoint)

func (*Peer) SetPresharedKey

func (peer *Peer) SetPresharedKey(psk NoisePresharedKey)

func (*Peer) StagePackets

func (peer *Peer) StagePackets(elems *QueueOutboundElementsContainer)

func (*Peer) Start

func (peer *Peer) Start()

func (*Peer) Stop

func (peer *Peer) Stop()

func (*Peer) String

func (peer *Peer) String() string

func (*Peer) ZeroAndFlushAll

func (peer *Peer) ZeroAndFlushAll()

type QueueHandshakeElement

type QueueHandshakeElement struct {
	// contains filtered or unexported fields
}

type QueueInboundElement

type QueueInboundElement struct {
	// contains filtered or unexported fields
}

type QueueInboundElementsContainer

type QueueInboundElementsContainer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

type QueueOutboundElement

type QueueOutboundElement struct {
	// contains filtered or unexported fields
}

type QueueOutboundElementsContainer

type QueueOutboundElementsContainer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

type SourceSink

type SourceSink interface {
	io.Closer

	// Read one or more packets from the Transport (without any additional headers).
	// On a successful read it returns the number of packets read, and sets
	// packet lengths within the sizes slice. len(sizes) must be >= len(bufs).
	// A nonzero offset can be used to instruct the Transport on where to begin
	// reading into each element of the bufs slice.
	Read(bufs [][]byte, sizes []int, destinations []NoisePublicKey, offset int) (int, error)

	// Write one or more packets to the transport (without any additional headers).
	// On a successful write it returns the number of packets written. A nonzero
	// offset can be used to instruct the Transport on where to begin writing from
	// each packet contained within the bufs slice.
	Write(bufs [][]byte, sources []NoisePublicKey, offset int) (int, error)

	// BatchSize returns the preferred/max number of packets that can be read or
	// written in a single read/write call. BatchSize must not change over the
	// lifetime of a Transport.
	BatchSize() int
}

type Timer

type Timer struct {
	*time.Timer
	// contains filtered or unexported fields
}

A Timer manages time-based aspects of the WireGuard protocol. Timer roughly copies the interface of the Linux kernel's struct timer_list.

func (*Timer) Del

func (timer *Timer) Del()

func (*Timer) DelSync

func (timer *Timer) DelSync()

func (*Timer) IsPending

func (timer *Timer) IsPending() bool

func (*Timer) Mod

func (timer *Timer) Mod(d time.Duration)

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

func NewTransport

func NewTransport(sourceSink SourceSink, bind conn.Bind, logger *slog.Logger) *Transport

func (*Transport) BatchSize

func (transport *Transport) BatchSize() int

BatchSize returns the BatchSize for the transport as a whole which is the max of the bind batch size and the sink batch size. The batch size reported by transport is the size used to construct memory pools, and is the allowed batch size for the lifetime of the transport.

func (*Transport) Bind

func (transport *Transport) Bind() conn.Bind

func (*Transport) BindClose

func (transport *Transport) BindClose() error

func (*Transport) BindUpdate

func (transport *Transport) BindUpdate() error

func (*Transport) Close

func (transport *Transport) Close() error

func (*Transport) ConsumeMessageInitiation

func (transport *Transport) ConsumeMessageInitiation(msg *MessageInitiation) *Peer

func (*Transport) ConsumeMessageResponse

func (transport *Transport) ConsumeMessageResponse(msg *MessageResponse) *Peer

func (*Transport) CreateMessageInitiation

func (transport *Transport) CreateMessageInitiation(peer *Peer) (*MessageInitiation, error)

func (*Transport) CreateMessageResponse

func (transport *Transport) CreateMessageResponse(peer *Peer) (*MessageResponse, error)

func (*Transport) DeleteKeypair

func (transport *Transport) DeleteKeypair(key *Keypair)

func (*Transport) Down

func (transport *Transport) Down() error

func (*Transport) GetInboundElement

func (transport *Transport) GetInboundElement() *QueueInboundElement

func (*Transport) GetInboundElementsContainer

func (transport *Transport) GetInboundElementsContainer() *QueueInboundElementsContainer

func (*Transport) GetMessageBuffer

func (transport *Transport) GetMessageBuffer() *[MaxMessageSize]byte

func (*Transport) GetOutboundElement

func (transport *Transport) GetOutboundElement() *QueueOutboundElement

func (*Transport) GetOutboundElementsContainer

func (transport *Transport) GetOutboundElementsContainer() *QueueOutboundElementsContainer

func (*Transport) IsUnderLoad

func (transport *Transport) IsUnderLoad() bool

func (*Transport) LookupPeer

func (transport *Transport) LookupPeer(pk NoisePublicKey) *Peer

func (*Transport) NewOutboundElement

func (transport *Transport) NewOutboundElement() *QueueOutboundElement

func (*Transport) NewPeer

func (transport *Transport) NewPeer(pk NoisePublicKey) (*Peer, error)

func (*Transport) PopulatePools

func (transport *Transport) PopulatePools()

func (*Transport) PutInboundElement

func (transport *Transport) PutInboundElement(elem *QueueInboundElement)

func (*Transport) PutInboundElementsContainer

func (transport *Transport) PutInboundElementsContainer(c *QueueInboundElementsContainer)

func (*Transport) PutMessageBuffer

func (transport *Transport) PutMessageBuffer(msg *[MaxMessageSize]byte)

func (*Transport) PutOutboundElement

func (transport *Transport) PutOutboundElement(elem *QueueOutboundElement)

func (*Transport) PutOutboundElementsContainer

func (transport *Transport) PutOutboundElementsContainer(c *QueueOutboundElementsContainer)

func (*Transport) RemoveAllPeers

func (transport *Transport) RemoveAllPeers()

func (*Transport) RemovePeer

func (transport *Transport) RemovePeer(key NoisePublicKey)

func (*Transport) RoutineDecryption

func (transport *Transport) RoutineDecryption(id int)

func (*Transport) RoutineEncryption

func (transport *Transport) RoutineEncryption(id int)

Encrypts the elements in the queue * and marks them for sequential consumption (by releasing the mutex) * * Obs. One instance per core

func (*Transport) RoutineHandshake

func (transport *Transport) RoutineHandshake(id int)

Handles incoming packets related to handshake.

func (*Transport) RoutineReadFromSourceSink

func (transport *Transport) RoutineReadFromSourceSink()

func (*Transport) RoutineReceiveIncoming

func (transport *Transport) RoutineReceiveIncoming(maxBatchSize int, recv conn.ReceiveFunc)

Receives incoming datagrams for the transport * * Every time the bind is updated a new routine is started for * IPv4 and IPv6 (separately)

func (*Transport) SendHandshakeCookie

func (transport *Transport) SendHandshakeCookie(initiatingElem *QueueHandshakeElement) error

func (*Transport) SendKeepalivesToPeersWithCurrentKeypair

func (transport *Transport) SendKeepalivesToPeersWithCurrentKeypair()

func (*Transport) SetPrivateKey

func (transport *Transport) SetPrivateKey(sk NoisePrivateKey)

func (*Transport) Up

func (transport *Transport) Up() error

func (*Transport) UpdatePort

func (transport *Transport) UpdatePort(port uint16) error

func (*Transport) Wait

func (transport *Transport) Wait() chan struct{}

type WaitPool

type WaitPool struct {
	// contains filtered or unexported fields
}

func NewWaitPool

func NewWaitPool(max uint32, new func() any) *WaitPool

func (*WaitPool) Get

func (p *WaitPool) Get() any

func (*WaitPool) Put

func (p *WaitPool) Put(x any)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL