2019-11-07 17:13:05 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2022-09-20 17:21:32 +02:00
|
|
|
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
|
2019-11-07 17:13:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Package conn implements WireGuard's network connections.
|
|
|
|
package conn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-04-10 01:21:35 +02:00
|
|
|
"fmt"
|
2022-03-17 00:09:48 +01:00
|
|
|
"net/netip"
|
2021-04-10 01:21:35 +02:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2019-11-07 17:13:05 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-03-02 23:48:02 +01:00
|
|
|
const (
|
2023-03-03 00:08:28 +01:00
|
|
|
DefaultBatchSize = 128 // maximum number of packets handled per read and write
|
2023-03-02 23:48:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// A ReceiveFunc receives at least one packet from the network and writes them
|
|
|
|
// into packets. On a successful read it returns the number of elements of
|
|
|
|
// sizes, packets, and endpoints that should be evaluated. Some elements of
|
|
|
|
// sizes may be zero, and callers should ignore them. Callers must pass a sizes
|
|
|
|
// and eps slice with a length greater than or equal to the length of packets.
|
|
|
|
// These lengths must not exceed the length of the associated Bind.BatchSize().
|
|
|
|
type ReceiveFunc func(packets [][]byte, sizes []int, eps []Endpoint) (n int, err error)
|
2021-03-31 22:55:18 +02:00
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
// A Bind listens on a port for both IPv6 and IPv4 UDP traffic.
|
2020-06-22 02:40:59 +02:00
|
|
|
//
|
|
|
|
// A Bind interface may also be a PeekLookAtSocketFd or BindSocketToInterface,
|
|
|
|
// depending on the platform-specific implementation.
|
2019-11-07 17:13:05 +01:00
|
|
|
type Bind interface {
|
2021-02-22 02:01:50 +01:00
|
|
|
// Open puts the Bind into a listening state on a given port and reports the actual
|
|
|
|
// port that it bound to. Passing zero results in a random selection.
|
2021-03-31 22:55:18 +02:00
|
|
|
// fns is the set of functions that will be called to receive packets.
|
|
|
|
Open(port uint16) (fns []ReceiveFunc, actualPort uint16, err error)
|
2021-02-22 02:01:50 +01:00
|
|
|
|
|
|
|
// Close closes the Bind listener.
|
2021-03-31 22:55:18 +02:00
|
|
|
// All fns returned by Open must return net.ErrClosed after a call to Close.
|
2021-02-22 02:01:50 +01:00
|
|
|
Close() error
|
2019-11-07 17:13:05 +01:00
|
|
|
|
|
|
|
// SetMark sets the mark for each packet sent through this Bind.
|
|
|
|
// This mark is passed to the kernel as the socket option SO_MARK.
|
|
|
|
SetMark(mark uint32) error
|
|
|
|
|
2023-03-02 23:48:02 +01:00
|
|
|
// Send writes one or more packets in buffs to address ep. The length of
|
|
|
|
// buffs must not exceed BatchSize().
|
|
|
|
Send(buffs [][]byte, ep Endpoint) error
|
2019-11-07 17:13:05 +01:00
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
// ParseEndpoint creates a new endpoint from a string.
|
|
|
|
ParseEndpoint(s string) (Endpoint, error)
|
2023-03-02 23:48:02 +01:00
|
|
|
|
|
|
|
// BatchSize is the number of buffers expected to be passed to
|
|
|
|
// the ReceiveFuncs, and the maximum expected to be passed to SendBatch.
|
|
|
|
BatchSize() int
|
2019-11-07 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 09:24:06 +02:00
|
|
|
// BindSocketToInterface is implemented by Bind objects that support being
|
2020-06-22 02:40:59 +02:00
|
|
|
// tied to a single network interface. Used by wireguard-windows.
|
2020-06-07 09:24:06 +02:00
|
|
|
type BindSocketToInterface interface {
|
|
|
|
BindSocketToInterface4(interfaceIndex uint32, blackhole bool) error
|
|
|
|
BindSocketToInterface6(interfaceIndex uint32, blackhole bool) error
|
2019-11-07 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 09:41:08 +02:00
|
|
|
// PeekLookAtSocketFd is implemented by Bind objects that support having their
|
2020-06-22 02:40:59 +02:00
|
|
|
// file descriptor peeked at. Used by wireguard-android.
|
2020-06-07 09:41:08 +02:00
|
|
|
type PeekLookAtSocketFd interface {
|
|
|
|
PeekLookAtSocketFd4() (fd int, err error)
|
|
|
|
PeekLookAtSocketFd6() (fd int, err error)
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
// An Endpoint maintains the source/destination caching for a peer.
|
|
|
|
//
|
2021-02-22 02:01:50 +01:00
|
|
|
// dst: the remote address of a peer ("endpoint" in uapi terminology)
|
|
|
|
// src: the local address from which datagrams originate going to the peer
|
2019-11-07 17:13:05 +01:00
|
|
|
type Endpoint interface {
|
|
|
|
ClearSrc() // clears the source address
|
|
|
|
SrcToString() string // returns the local source address (ip:port)
|
|
|
|
DstToString() string // returns the destination address (ip:port)
|
|
|
|
DstToBytes() []byte // used for mac2 cookie calculations
|
2021-11-05 01:52:54 +01:00
|
|
|
DstIP() netip.Addr
|
|
|
|
SrcIP() netip.Addr
|
2019-11-07 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:21:35 +02:00
|
|
|
var (
|
|
|
|
ErrBindAlreadyOpen = errors.New("bind is already open")
|
|
|
|
ErrWrongEndpointType = errors.New("endpoint type does not correspond with bind type")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (fn ReceiveFunc) PrettyName() string {
|
|
|
|
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
|
|
|
|
// 0. cheese/taco.beansIPv6.func12.func21218-fm
|
|
|
|
name = strings.TrimSuffix(name, "-fm")
|
|
|
|
// 1. cheese/taco.beansIPv6.func12.func21218
|
|
|
|
if idx := strings.LastIndexByte(name, '/'); idx != -1 {
|
|
|
|
name = name[idx+1:]
|
|
|
|
// 2. taco.beansIPv6.func12.func21218
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
var idx int
|
|
|
|
for idx = len(name) - 1; idx >= 0; idx-- {
|
|
|
|
if name[idx] < '0' || name[idx] > '9' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == len(name)-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
const dotFunc = ".func"
|
|
|
|
if !strings.HasSuffix(name[:idx+1], dotFunc) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name = name[:idx+1-len(dotFunc)]
|
|
|
|
// 3. taco.beansIPv6.func12
|
|
|
|
// 4. taco.beansIPv6
|
|
|
|
}
|
|
|
|
if idx := strings.LastIndexByte(name, '.'); idx != -1 {
|
|
|
|
name = name[idx+1:]
|
|
|
|
// 5. beansIPv6
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Sprintf("%p", fn)
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(name, "IPv4") {
|
|
|
|
return "v4"
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(name, "IPv6") {
|
|
|
|
return "v6"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|