2019-11-07 17:13:05 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2021-01-28 17:52:15 +01:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2019-11-07 17:13:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Package conn implements WireGuard's network connections.
|
|
|
|
package conn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
Open(port uint16) (actualPort uint16, err error)
|
|
|
|
|
|
|
|
// Close closes the Bind listener.
|
|
|
|
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
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
// ReceiveIPv6 reads an IPv6 UDP packet into b. It reports the number of bytes read,
|
|
|
|
// n, the packet source address ep, and any error.
|
2021-01-26 03:52:42 +01:00
|
|
|
ReceiveIPv6(b []byte) (n int, ep Endpoint, err error)
|
2019-11-07 17:13:05 +01:00
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
// ReceiveIPv4 reads an IPv4 UDP packet into b. It reports the number of bytes read,
|
|
|
|
// n, the packet source address ep, and any error.
|
2019-11-07 17:13:05 +01:00
|
|
|
ReceiveIPv4(b []byte) (n int, ep Endpoint, err error)
|
|
|
|
|
|
|
|
// Send writes a packet b to address ep.
|
|
|
|
Send(b []byte, ep Endpoint) error
|
|
|
|
|
2021-02-22 02:01:50 +01:00
|
|
|
// ParseEndpoint creates a new endpoint from a string.
|
|
|
|
ParseEndpoint(s string) (Endpoint, error)
|
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
|
|
|
|
DstIP() net.IP
|
|
|
|
SrcIP() net.IP
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEndpoint(s string) (*net.UDPAddr, error) {
|
|
|
|
// ensure that the host is an IP address
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if i := strings.LastIndexByte(host, '%'); i > 0 && strings.IndexByte(host, ':') >= 0 {
|
|
|
|
// Remove the scope, if any. ResolveUDPAddr below will use it, but here we're just
|
|
|
|
// trying to make sure with a small sanity test that this is a real IP address and
|
|
|
|
// not something that's likely to incur DNS lookups.
|
|
|
|
host = host[:i]
|
|
|
|
}
|
|
|
|
if ip := net.ParseIP(host); ip == nil {
|
|
|
|
return nil, errors.New("Failed to parse IP address: " + host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse address and port
|
|
|
|
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ip4 := addr.IP.To4()
|
|
|
|
if ip4 != nil {
|
|
|
|
addr.IP = ip4
|
|
|
|
}
|
|
|
|
return addr, err
|
|
|
|
}
|
2021-02-22 02:01:50 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
ErrBindAlreadyOpen = errors.New("bind is already open")
|
|
|
|
ErrWrongEndpointType = errors.New("endpoint type does not correspond with bind type")
|
|
|
|
)
|