netstack

package
v0.0.0-...-dd04f72 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MPL-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Adopted from: github.com/google/gvisor/blob/f33d034/pkg/tcpip/link/fdbased/packet_dispatchers.go

Package netstack provides the implemention of data-link layer endpoints backed by boundary-preserving file descriptors (e.g., TUN devices, seqpacket/datagram sockets).

Adopted from: github.com/google/gvisor/blob/f33d034/pkg/tcpip/link/fdbased/endpoint.go since fdbased isn't built when building for android (it is only built for linux).

Copyright (c) 2023 RethinkDNS and its authors.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

This file incorporates work covered by the following copyright and permission notice:

MIT No Attribution

Copyright 2022 National Technology & Engineering Solutions of Sandia, LLC
(NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Government retains certain rights in this software.

Copyright (c) 2022 RethinkDNS and its authors.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

Copyright (c) 2022 RethinkDNS and its authors.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

Copyright (c) 2022 RethinkDNS and its authors.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

Index

Constants

View Source
const (
	NetworkUnreachable = iota
	HostUnreachable
	ProtocolUnreachable
	PortUnreachable
)
View Source
const SnapLen uint32 = 2048 // in bytes; some sufficient value

Variables

View Source
var BufConfig = []int{128, 256, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}

BufConfig defines the shape of the vectorised view used to read packets from the NIC.

Functions

func FilePcap

func FilePcap(y bool) (ok bool)

func LogPcap

func LogPcap(y bool) (ok bool)

func NewNetstack

func NewNetstack() (s *stack.Stack)

also: github.com/google/gvisor/blob/adbdac747/runsc/boot/loader.go#L1132 github.com/FlowerWrong/tun2socks/blob/1045a49618/cmd/netstack/main.go github.com/zen-of-proxy/go-tun2io/blob/c08b329b8/tun2io/util.go github.com/WireGuard/wireguard-go/blob/42c9af4/tun/netstack/tun.go github.com/telepresenceio/telepresence/pull/2709

func NewTCPForwarder

func NewTCPForwarder(s *stack.Stack, h GTCPConnHandler) *tcp.Forwarder

nic.deliverNetworkPacket -> no existing matching endpoints -> NewTCPForwarder.HandlePacket ref: github.com/google/gvisor/blob/e89e736f1/pkg/tcpip/adapters/gonet/gonet_test.go#L189

func NewUDPForwarder

func NewUDPForwarder(s *stack.Stack, h GUDPConnHandler) *udp.Forwarder

Perhaps udp conns shouldn't be closed as eagerly as its tcp counterpart Netstack's udp conn is apparently a 'connected udp' socket and it goes through a lot of motions, from what I can tell, to support both unconnected and connected udp sockets. This is untested and unconfirmed speculation from us, but unless intra/udp.go refrains from closing this udp conn, we'll never find out I guess. ref: github.com/google/gvisor/blob/be6ffa7/pkg/tcpip/stack/transport_demuxer.go#L590 and: github.com/google/gvisor/blob/be6ffa7/pkg/tcpip/stack/transport_demuxer.go#L75 and: github.com/google/gvisor/blob/be6ffa7/pkg/tcpip/transport/udp/endpoint.go#L903 via: github.com/google/gvisor/blob/be6ffa7/pkg/tcpip/adapters/gonet/gonet.go#L315 fin: github.com/google/gvisor/blob/be6ffa7/pkg/tcpip/transport/udp/endpoint.go#L220 but: github.com/google/gvisor/blob/be6ffa7/pkg/tcpip/transport/udp/endpoint.go#L180

func Route

func Route(s *stack.Stack, l3 string)

func Up

ref: github.com/brewlin/net-protocol/blob/ec64e5f899/internal/endpoint/endpoint.go#L20

Types

type GConnHandler

type GConnHandler interface {
	TCP() GTCPConnHandler         // TCP returns the TCP handler.
	UDP() GUDPConnHandler         // UDP returns the UDP handler.
	ICMP() GICMPHandler           // ICMP returns the ICMP handler.
	CloseConns(csv string) string // CloseConns closes the connections with the given IDs, or all if empty.
	Close() error                 // Close closes TCP, UDP, ICMP handlers and its resources.
}

func NewGConnHandler

func NewGConnHandler(tcp GTCPConnHandler, udp GUDPConnHandler, icmp GICMPHandler) GConnHandler

type GICMPHandler

type GICMPHandler interface {
	// Multi ping handler
	Ping(source, destination netip.AddrPort, msg []byte, pong Pong) bool
	// Single ping handler
	PingOnce(source, destination netip.AddrPort, msg []byte) bool
	// CloseConns closes all connections
	CloseConns([]string) []string
	// End closes the handler and all its connections
	End() error
}

type GTCPConn

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

func MakeGTCPConn

func MakeGTCPConn(req *tcp.ForwarderRequest, src, dst netip.AddrPort) *GTCPConn

func (*GTCPConn) Abort

func (g *GTCPConn) Abort()

Abort aborts the connection by sending a RST segment.

func (GTCPConn) Close

func (g GTCPConn) Close() error

func (*GTCPConn) CloseRead

func (g *GTCPConn) CloseRead() error

func (*GTCPConn) CloseWrite

func (g *GTCPConn) CloseWrite() error

func (*GTCPConn) Connect

func (g *GTCPConn) Connect(rst bool) (open bool, err error)

func (*GTCPConn) LocalAddr

func (g *GTCPConn) LocalAddr() net.Addr

gonet conn local and remote addresses may be nil ref: github.com/tailscale/tailscale/blob/8c5c87be2/wgengine/netstack/netstack.go#L768-L775 and: github.com/google/gvisor/blob/ffabadf0/pkg/tcpip/transport/tcp/endpoint.go#L2759

func (*GTCPConn) Read

func (g *GTCPConn) Read(data []byte) (int, error)

func (*GTCPConn) RemoteAddr

func (g *GTCPConn) RemoteAddr() net.Addr

func (*GTCPConn) SetDeadline

func (g *GTCPConn) SetDeadline(t time.Time) error

func (*GTCPConn) SetReadDeadline

func (g *GTCPConn) SetReadDeadline(t time.Time) error

func (*GTCPConn) SetWriteDeadline

func (g *GTCPConn) SetWriteDeadline(t time.Time) error

func (*GTCPConn) StatefulTeardown

func (g *GTCPConn) StatefulTeardown() (rst bool)

func (*GTCPConn) Write

func (g *GTCPConn) Write(data []byte) (int, error)

type GTCPConnHandler

type GTCPConnHandler interface {
	// Proxy copies data between src and dst.
	Proxy(conn *GTCPConn, src, dst netip.AddrPort) bool
	// CloseConns closes conns by cids, or all conns if cids is empty.
	CloseConns([]string) []string
	// End closes all conns and releases resources.
	End() error
}

type GUDPConn

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

func MakeGUDPConn

func MakeGUDPConn(r *udp.ForwarderRequest, src, dst netip.AddrPort) *GUDPConn

ref: github.com/google/gvisor/blob/e89e736f1/pkg/tcpip/adapters/gonet/gonet_test.go#L373

func (*GUDPConn) Close

func (g *GUDPConn) Close() error

Close closes the connection.

func (*GUDPConn) Connect

func (g *GUDPConn) Connect(fin bool) error

func (*GUDPConn) LocalAddr

func (g *GUDPConn) LocalAddr() (addr net.Addr)

func (*GUDPConn) Read

func (g *GUDPConn) Read(data []byte) (int, error)

func (*GUDPConn) ReadFrom

func (g *GUDPConn) ReadFrom(data []byte) (int, net.Addr, error)

func (*GUDPConn) RemoteAddr

func (g *GUDPConn) RemoteAddr() (addr net.Addr)

func (*GUDPConn) SetDeadline

func (g *GUDPConn) SetDeadline(t time.Time) error

func (*GUDPConn) SetReadDeadline

func (g *GUDPConn) SetReadDeadline(t time.Time) error

func (*GUDPConn) SetWriteDeadline

func (g *GUDPConn) SetWriteDeadline(t time.Time) error

func (*GUDPConn) StatefulTeardown

func (g *GUDPConn) StatefulTeardown() (fin bool)

func (*GUDPConn) Write

func (g *GUDPConn) Write(data []byte) (int, error)

func (*GUDPConn) WriteTo

func (g *GUDPConn) WriteTo(data []byte, addr net.Addr) (int, error)

type GUDPConnHandler

type GUDPConnHandler interface {
	// Proxy proxies data between conn (src) and dst.
	Proxy(conn *GUDPConn, src, dst netip.AddrPort) bool
	// ProxyMux proxies data between conn and multiple destinations.
	ProxyMux(conn *GUDPConn, src netip.AddrPort) bool
	// CloseConns closes conns by ids, or all if ids is empty.
	CloseConns([]string) []string
	// End closes the handler and all its connections.
	End() error
}

type Options

type Options struct {
	// FDs is a set of FDs used to read/write packets.
	FDs []int

	// MTU is the mtu to use for this endpoint.
	MTU uint32

	// EthernetHeader if true, indicates that the endpoint should read/write
	// ethernet frames instead of IP packets.
	EthernetHeader bool

	// Address is the link address for this endpoint. Only used if
	// EthernetHeader is true.
	Address tcpip.LinkAddress

	// SaveRestore if true, indicates that this NIC capability set should
	// include CapabilitySaveRestore
	SaveRestore bool

	// DisconnectOk if true, indicates that this NIC capability set should
	// include CapabilityDisconnectOk.
	DisconnectOk bool

	// TXChecksumOffload if true, indicates that this endpoints capability
	// set should include CapabilityTXChecksumOffload.
	TXChecksumOffload bool

	// RXChecksumOffload if true, indicates that this endpoints capability
	// set should include CapabilityRXChecksumOffload.
	RXChecksumOffload bool

	// If MaxSyscallHeaderBytes is non-zero, it is the maximum number of bytes
	// of struct iovec, msghdr, and mmsghdr that may be passed by each host
	// system call.
	MaxSyscallHeaderBytes int
}

Options specify the details about the fd-based endpoint to be created.

type Pong

type Pong func(reply []byte) error

Pong is a callback function to send a reply to the client

type SeamlessEndpoint

type SeamlessEndpoint interface {
	stack.LinkEndpoint
	Swapper
}

func NewEndpoint

func NewEndpoint(dev, mtu int, sink io.WriteCloser) (ep SeamlessEndpoint, err error)

ref: github.com/google/gvisor/blob/91f58d2cc/pkg/tcpip/sample/tun_tcp_echo/main.go#L102

func NewFdbasedInjectableEndpoint

func NewFdbasedInjectableEndpoint(opts *Options) (SeamlessEndpoint, error)

New creates a new fd-based endpoint.

Makes fd non-blocking, but does not take ownership of fd, which must remain open for the lifetime of the returned endpoint (until after the endpoint has stopped being using and Wait returns).

type Swapper

type Swapper interface {
	// Swap closes existing FDs; uses new fd and mtu.
	Swap(fd, mtu int) error
}

Jump to

Keyboard shortcuts

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