2020-09-03 22:45:41 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package netstack wires up gVisor's netstack into Tailscale.
|
|
|
|
package netstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-25 19:18:16 +00:00
|
|
|
"io"
|
2020-09-03 22:45:41 +00:00
|
|
|
"log"
|
2021-02-26 21:39:48 +00:00
|
|
|
"net"
|
2021-02-26 22:06:00 +00:00
|
|
|
"strconv"
|
2020-09-03 22:45:41 +00:00
|
|
|
"strings"
|
2021-02-26 22:06:00 +00:00
|
|
|
"sync"
|
2021-05-05 19:32:46 +00:00
|
|
|
"sync/atomic"
|
2021-03-08 18:22:35 +00:00
|
|
|
"time"
|
2020-09-03 22:45:41 +00:00
|
|
|
|
|
|
|
"inet.af/netaddr"
|
2021-03-18 02:34:21 +00:00
|
|
|
"inet.af/netstack/tcpip"
|
|
|
|
"inet.af/netstack/tcpip/adapters/gonet"
|
|
|
|
"inet.af/netstack/tcpip/buffer"
|
|
|
|
"inet.af/netstack/tcpip/header"
|
|
|
|
"inet.af/netstack/tcpip/link/channel"
|
|
|
|
"inet.af/netstack/tcpip/network/ipv4"
|
|
|
|
"inet.af/netstack/tcpip/network/ipv6"
|
|
|
|
"inet.af/netstack/tcpip/stack"
|
|
|
|
"inet.af/netstack/tcpip/transport/icmp"
|
|
|
|
"inet.af/netstack/tcpip/transport/tcp"
|
|
|
|
"inet.af/netstack/tcpip/transport/udp"
|
|
|
|
"inet.af/netstack/waiter"
|
2020-09-03 22:45:41 +00:00
|
|
|
"tailscale.com/net/packet"
|
2021-03-29 18:33:05 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2021-03-27 05:14:08 +00:00
|
|
|
"tailscale.com/net/tstun"
|
2020-09-03 22:45:41 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2021-02-26 22:06:00 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2020-09-03 22:45:41 +00:00
|
|
|
"tailscale.com/wgengine"
|
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
"tailscale.com/wgengine/magicsock"
|
|
|
|
)
|
|
|
|
|
2021-03-03 18:37:01 +00:00
|
|
|
const debugNetstack = false
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
// Impl contains the state for the netstack implementation,
|
|
|
|
// and implements wgengine.FakeImpl to act as a userspace network
|
|
|
|
// stack when Tailscale is running in fake mode.
|
|
|
|
type Impl struct {
|
2021-05-14 15:53:55 +00:00
|
|
|
// ForwardTCPIn, if non-nil, handles forwarding an inbound TCP
|
|
|
|
// connection.
|
|
|
|
// TODO(bradfitz): provide mechanism for tsnet to reject a
|
|
|
|
// port other than accepting it and closing it.
|
|
|
|
ForwardTCPIn func(c net.Conn, port uint16)
|
|
|
|
|
2021-04-01 16:35:41 +00:00
|
|
|
ipstack *stack.Stack
|
|
|
|
linkEP *channel.Endpoint
|
|
|
|
tundev *tstun.Wrapper
|
|
|
|
e wgengine.Engine
|
|
|
|
mc *magicsock.Conn
|
|
|
|
logf logger.Logf
|
|
|
|
onlySubnets bool // whether we only want to handle subnet relaying
|
2021-02-26 22:06:00 +00:00
|
|
|
|
2021-05-05 19:32:46 +00:00
|
|
|
// atomicIsLocalIPFunc holds a func that reports whether an IP
|
|
|
|
// is a local (non-subnet) Tailscale IP address of this
|
|
|
|
// machine. It's always a non-nil func. It's changed on netmap
|
|
|
|
// updates.
|
|
|
|
atomicIsLocalIPFunc atomic.Value // of func(netaddr.IP) bool
|
|
|
|
|
2021-02-26 22:06:00 +00:00
|
|
|
mu sync.Mutex
|
2021-03-01 18:08:53 +00:00
|
|
|
dns DNSMap
|
2021-03-29 18:33:05 +00:00
|
|
|
// connsOpenBySubnetIP keeps track of number of connections open
|
|
|
|
// for each subnet IP temporarily registered on netstack for active
|
|
|
|
// TCP connections, so they can be unregistered when connections are
|
|
|
|
// closed.
|
|
|
|
connsOpenBySubnetIP map[netaddr.IP]int
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nicID = 1
|
2021-03-08 18:22:35 +00:00
|
|
|
const mtu = 1500
|
2021-02-25 19:18:16 +00:00
|
|
|
|
|
|
|
// Create creates and populates a new Impl.
|
2021-04-01 16:35:41 +00:00
|
|
|
func Create(logf logger.Logf, tundev *tstun.Wrapper, e wgengine.Engine, mc *magicsock.Conn, onlySubnets bool) (*Impl, error) {
|
2020-09-03 22:45:41 +00:00
|
|
|
if mc == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil magicsock.Conn")
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
if tundev == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil tundev")
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
if logf == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil logger")
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
if e == nil {
|
2021-02-25 19:18:16 +00:00
|
|
|
return nil, errors.New("nil Engine")
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
ipstack := stack.New(stack.Options{
|
2021-02-25 19:18:16 +00:00
|
|
|
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
|
|
|
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
2020-09-03 22:45:41 +00:00
|
|
|
})
|
|
|
|
linkEP := channel.New(512, mtu, "")
|
2021-02-25 19:18:16 +00:00
|
|
|
if tcpipProblem := ipstack.CreateNIC(nicID, linkEP); tcpipProblem != nil {
|
|
|
|
return nil, fmt.Errorf("could not create netstack NIC: %v", tcpipProblem)
|
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
// By default the netstack NIC will only accept packets for the IPs
|
|
|
|
// registered to it. Since in some cases we dynamically register IPs
|
|
|
|
// based on the packets that arrive, the NIC needs to accept all
|
|
|
|
// incoming packets. The NIC won't receive anything it isn't meant to
|
|
|
|
// since Wireguard will only send us packets that are meant for us.
|
|
|
|
ipstack.SetPromiscuousMode(nicID, true)
|
2021-02-25 19:18:16 +00:00
|
|
|
// Add IPv4 and IPv6 default routes, so all incoming packets from the Tailscale side
|
|
|
|
// are handled by the one fake NIC we use.
|
|
|
|
ipv4Subnet, _ := tcpip.NewSubnet(tcpip.Address(strings.Repeat("\x00", 4)), tcpip.AddressMask(strings.Repeat("\x00", 4)))
|
|
|
|
ipv6Subnet, _ := tcpip.NewSubnet(tcpip.Address(strings.Repeat("\x00", 16)), tcpip.AddressMask(strings.Repeat("\x00", 16)))
|
|
|
|
ipstack.SetRouteTable([]tcpip.Route{
|
|
|
|
{
|
|
|
|
Destination: ipv4Subnet,
|
|
|
|
NIC: nicID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Destination: ipv6Subnet,
|
|
|
|
NIC: nicID,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ns := &Impl{
|
2021-03-29 18:33:05 +00:00
|
|
|
logf: logf,
|
|
|
|
ipstack: ipstack,
|
|
|
|
linkEP: linkEP,
|
|
|
|
tundev: tundev,
|
|
|
|
e: e,
|
|
|
|
mc: mc,
|
|
|
|
connsOpenBySubnetIP: make(map[netaddr.IP]int),
|
2021-04-01 16:35:41 +00:00
|
|
|
onlySubnets: onlySubnets,
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-05-05 19:32:46 +00:00
|
|
|
ns.atomicIsLocalIPFunc.Store(tsaddr.NewContainsIPFunc(nil))
|
2021-02-25 19:18:16 +00:00
|
|
|
return ns, nil
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:38:13 +00:00
|
|
|
// wrapProtoHandler returns protocol handler h wrapped in a version
|
|
|
|
// that dynamically reconfigures ns's subnet addresses as needed for
|
|
|
|
// outbound traffic.
|
|
|
|
func (ns *Impl) wrapProtoHandler(h func(stack.TransportEndpointID, *stack.PacketBuffer) bool) func(stack.TransportEndpointID, *stack.PacketBuffer) bool {
|
|
|
|
return func(tei stack.TransportEndpointID, pb *stack.PacketBuffer) bool {
|
|
|
|
addr := tei.LocalAddress
|
|
|
|
ip, ok := netaddr.FromStdIP(net.IP(addr))
|
|
|
|
if !ok {
|
|
|
|
ns.logf("netstack: could not parse local address for incoming connection")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !ns.isLocalIP(ip) {
|
|
|
|
ns.addSubnetAddress(ip)
|
|
|
|
}
|
|
|
|
return h(tei, pb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
// Start sets up all the handlers so netstack can start working. Implements
|
|
|
|
// wgengine.FakeImpl.
|
|
|
|
func (ns *Impl) Start() error {
|
|
|
|
ns.e.AddNetworkMapCallback(ns.updateIPs)
|
|
|
|
// size = 0 means use default buffer size
|
|
|
|
const tcpReceiveBufferSize = 0
|
|
|
|
const maxInFlightConnectionAttempts = 16
|
|
|
|
tcpFwd := tcp.NewForwarder(ns.ipstack, tcpReceiveBufferSize, maxInFlightConnectionAttempts, ns.acceptTCP)
|
|
|
|
udpFwd := udp.NewForwarder(ns.ipstack, ns.acceptUDP)
|
2021-07-21 15:38:13 +00:00
|
|
|
ns.ipstack.SetTransportProtocolHandler(tcp.ProtocolNumber, ns.wrapProtoHandler(tcpFwd.HandlePacket))
|
|
|
|
ns.ipstack.SetTransportProtocolHandler(udp.ProtocolNumber, ns.wrapProtoHandler(udpFwd.HandlePacket))
|
2021-02-25 19:18:16 +00:00
|
|
|
go ns.injectOutbound()
|
|
|
|
ns.tundev.PostFilterIn = ns.injectInbound
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:08:53 +00:00
|
|
|
// DNSMap maps MagicDNS names (both base + FQDN) to their first IP.
|
|
|
|
// It should not be mutated once created.
|
|
|
|
type DNSMap map[string]netaddr.IP
|
|
|
|
|
|
|
|
func DNSMapFromNetworkMap(nm *netmap.NetworkMap) DNSMap {
|
|
|
|
ret := make(DNSMap)
|
2021-02-26 22:06:00 +00:00
|
|
|
suffix := nm.MagicDNSSuffix()
|
2021-06-28 22:16:37 +00:00
|
|
|
have4 := false
|
2021-02-26 22:06:00 +00:00
|
|
|
if nm.Name != "" && len(nm.Addresses) > 0 {
|
2021-05-15 01:07:28 +00:00
|
|
|
ip := nm.Addresses[0].IP()
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[strings.TrimRight(nm.Name, ".")] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
if dnsname.HasSuffix(nm.Name, suffix) {
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[dnsname.TrimSuffix(nm.Name, suffix)] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
2021-06-28 22:16:37 +00:00
|
|
|
for _, a := range nm.Addresses {
|
|
|
|
if a.IP().Is4() {
|
|
|
|
have4 = true
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
for _, p := range nm.Peers {
|
2021-06-28 22:16:37 +00:00
|
|
|
if p.Name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, a := range p.Addresses {
|
|
|
|
ip := a.IP()
|
|
|
|
if ip.Is4() && !have4 {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[strings.TrimRight(p.Name, ".")] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
if dnsname.HasSuffix(p.Name, suffix) {
|
2021-03-01 18:08:53 +00:00
|
|
|
ret[dnsname.TrimSuffix(p.Name, suffix)] = ip
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
2021-06-28 22:16:37 +00:00
|
|
|
break
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 18:08:53 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Impl) updateDNS(nm *netmap.NetworkMap) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
defer ns.mu.Unlock()
|
|
|
|
ns.dns = DNSMapFromNetworkMap(nm)
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 15:38:13 +00:00
|
|
|
func (ns *Impl) addSubnetAddress(ip netaddr.IP) {
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.mu.Lock()
|
|
|
|
ns.connsOpenBySubnetIP[ip]++
|
|
|
|
needAdd := ns.connsOpenBySubnetIP[ip] == 1
|
|
|
|
ns.mu.Unlock()
|
|
|
|
// Only register address into netstack for first concurrent connection.
|
|
|
|
if needAdd {
|
2021-07-21 15:38:13 +00:00
|
|
|
var pn tcpip.NetworkProtocolNumber
|
|
|
|
if ip.Is4() {
|
|
|
|
pn = ipv4.ProtocolNumber
|
|
|
|
} else if ip.Is6() {
|
|
|
|
pn = ipv6.ProtocolNumber
|
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.ipstack.AddAddress(nicID, pn, tcpip.Address(ip.IPAddr().IP))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Impl) removeSubnetAddress(ip netaddr.IP) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
defer ns.mu.Unlock()
|
|
|
|
ns.connsOpenBySubnetIP[ip]--
|
|
|
|
// Only unregister address from netstack after last concurrent connection.
|
|
|
|
if ns.connsOpenBySubnetIP[ip] == 0 {
|
|
|
|
ns.ipstack.RemoveAddress(nicID, tcpip.Address(ip.IPAddr().IP))
|
|
|
|
delete(ns.connsOpenBySubnetIP, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipPrefixToAddressWithPrefix(ipp netaddr.IPPrefix) tcpip.AddressWithPrefix {
|
|
|
|
return tcpip.AddressWithPrefix{
|
2021-05-15 01:07:28 +00:00
|
|
|
Address: tcpip.Address(ipp.IP().IPAddr().IP),
|
|
|
|
PrefixLen: int(ipp.Bits()),
|
2021-03-29 18:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) updateIPs(nm *netmap.NetworkMap) {
|
2021-05-05 19:32:46 +00:00
|
|
|
ns.atomicIsLocalIPFunc.Store(tsaddr.NewContainsIPFunc(nm.Addresses))
|
2021-02-26 22:06:00 +00:00
|
|
|
ns.updateDNS(nm)
|
|
|
|
|
2021-03-29 18:33:05 +00:00
|
|
|
oldIPs := make(map[tcpip.AddressWithPrefix]bool)
|
|
|
|
for _, protocolAddr := range ns.ipstack.AllAddresses()[nicID] {
|
|
|
|
oldIPs[protocolAddr.AddressWithPrefix] = true
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
newIPs := make(map[tcpip.AddressWithPrefix]bool)
|
2021-04-01 16:35:41 +00:00
|
|
|
|
|
|
|
isAddr := map[netaddr.IPPrefix]bool{}
|
|
|
|
for _, ipp := range nm.SelfNode.Addresses {
|
|
|
|
isAddr[ipp] = true
|
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
for _, ipp := range nm.SelfNode.AllowedIPs {
|
2021-04-01 16:35:41 +00:00
|
|
|
if ns.onlySubnets && isAddr[ipp] {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
newIPs[ipPrefixToAddressWithPrefix(ipp)] = true
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:33:05 +00:00
|
|
|
ipsToBeAdded := make(map[tcpip.AddressWithPrefix]bool)
|
|
|
|
for ipp := range newIPs {
|
|
|
|
if !oldIPs[ipp] {
|
|
|
|
ipsToBeAdded[ipp] = true
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
ipsToBeRemoved := make(map[tcpip.AddressWithPrefix]bool)
|
2021-02-25 19:18:16 +00:00
|
|
|
for ip := range oldIPs {
|
|
|
|
if !newIPs[ip] {
|
|
|
|
ipsToBeRemoved[ip] = true
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.mu.Lock()
|
|
|
|
for ip := range ns.connsOpenBySubnetIP {
|
|
|
|
ipp := tcpip.Address(ip.IPAddr().IP).WithPrefix()
|
|
|
|
delete(ipsToBeRemoved, ipp)
|
|
|
|
}
|
|
|
|
ns.mu.Unlock()
|
2021-01-15 14:16:28 +00:00
|
|
|
|
2021-03-29 18:33:05 +00:00
|
|
|
for ipp := range ipsToBeRemoved {
|
|
|
|
err := ns.ipstack.RemoveAddress(nicID, ipp.Address)
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("netstack: could not deregister IP %s: %v", ipp, err)
|
2021-02-25 19:18:16 +00:00
|
|
|
} else {
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("[v2] netstack: deregistered IP %s", ipp)
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
for ipp := range ipsToBeAdded {
|
2021-03-18 02:34:21 +00:00
|
|
|
var err tcpip.Error
|
2021-03-29 18:33:05 +00:00
|
|
|
if ipp.Address.To4() == "" {
|
|
|
|
err = ns.ipstack.AddAddressWithPrefix(nicID, ipv6.ProtocolNumber, ipp)
|
2021-02-25 19:18:16 +00:00
|
|
|
} else {
|
2021-03-29 18:33:05 +00:00
|
|
|
err = ns.ipstack.AddAddressWithPrefix(nicID, ipv4.ProtocolNumber, ipp)
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("netstack: could not register IP %s: %v", ipp, err)
|
2021-02-25 19:18:16 +00:00
|
|
|
} else {
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("[v2] netstack: registered IP %s", ipp)
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:08:53 +00:00
|
|
|
// Resolve resolves addr into an IP:port using first the MagicDNS contents
|
|
|
|
// of m, else using the system resolver.
|
|
|
|
func (m DNSMap) Resolve(ctx context.Context, addr string) (netaddr.IPPort, error) {
|
2021-02-26 22:06:00 +00:00
|
|
|
ipp, pippErr := netaddr.ParseIPPort(addr)
|
|
|
|
if pippErr == nil {
|
|
|
|
return ipp, nil
|
|
|
|
}
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
// addr is malformed.
|
|
|
|
return netaddr.IPPort{}, err
|
|
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
|
|
// The host part of addr was an IP, so the netaddr.ParseIPPort above should've
|
|
|
|
// passed. Must've been a bad port number. Return the original error.
|
|
|
|
return netaddr.IPPort{}, pippErr
|
|
|
|
}
|
|
|
|
port16, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IPPort{}, fmt.Errorf("invalid port in address %q", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host is not an IP, so assume it's a DNS name.
|
|
|
|
|
|
|
|
// Try MagicDNS first, else otherwise a real DNS lookup.
|
2021-03-01 18:08:53 +00:00
|
|
|
ip := m[host]
|
2021-02-26 22:06:00 +00:00
|
|
|
if !ip.IsZero() {
|
2021-05-15 01:07:28 +00:00
|
|
|
return netaddr.IPPortFrom(ip, uint16(port16)), nil
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 17:21:01 +00:00
|
|
|
// No MagicDNS name so try real DNS.
|
2021-02-26 22:06:00 +00:00
|
|
|
var r net.Resolver
|
|
|
|
ips, err := r.LookupIP(ctx, "ip", host)
|
|
|
|
if err != nil {
|
|
|
|
return netaddr.IPPort{}, err
|
|
|
|
}
|
|
|
|
if len(ips) == 0 {
|
|
|
|
return netaddr.IPPort{}, fmt.Errorf("DNS lookup returned no results for %q", host)
|
|
|
|
}
|
|
|
|
ip, _ = netaddr.FromStdIP(ips[0])
|
2021-05-15 01:07:28 +00:00
|
|
|
return netaddr.IPPortFrom(ip, uint16(port16)), nil
|
2021-02-26 22:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 18:08:53 +00:00
|
|
|
func (ns *Impl) DialContextTCP(ctx context.Context, addr string) (*gonet.TCPConn, error) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
dnsMap := ns.dns
|
|
|
|
ns.mu.Unlock()
|
|
|
|
|
|
|
|
remoteIPPort, err := dnsMap.Resolve(ctx, addr)
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
2021-02-26 22:06:00 +00:00
|
|
|
return nil, err
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
remoteAddress := tcpip.FullAddress{
|
|
|
|
NIC: nicID,
|
2021-05-15 01:07:28 +00:00
|
|
|
Addr: tcpip.Address(remoteIPPort.IP().IPAddr().IP),
|
|
|
|
Port: remoteIPPort.Port(),
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
var ipType tcpip.NetworkProtocolNumber
|
2021-05-15 01:07:28 +00:00
|
|
|
if remoteIPPort.IP().Is4() {
|
2021-02-25 19:18:16 +00:00
|
|
|
ipType = ipv4.ProtocolNumber
|
|
|
|
} else {
|
|
|
|
ipType = ipv6.ProtocolNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return gonet.DialContextTCP(ctx, ns.ipstack, remoteAddress, ipType)
|
|
|
|
}
|
|
|
|
|
2021-06-24 05:09:31 +00:00
|
|
|
func (ns *Impl) DialContextUDP(ctx context.Context, addr string) (*gonet.UDPConn, error) {
|
|
|
|
ns.mu.Lock()
|
|
|
|
dnsMap := ns.dns
|
|
|
|
ns.mu.Unlock()
|
|
|
|
|
|
|
|
remoteIPPort, err := dnsMap.Resolve(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
remoteAddress := &tcpip.FullAddress{
|
|
|
|
NIC: nicID,
|
|
|
|
Addr: tcpip.Address(remoteIPPort.IP().IPAddr().IP),
|
|
|
|
Port: remoteIPPort.Port(),
|
|
|
|
}
|
|
|
|
var ipType tcpip.NetworkProtocolNumber
|
|
|
|
if remoteIPPort.IP().Is4() {
|
|
|
|
ipType = ipv4.ProtocolNumber
|
|
|
|
} else {
|
|
|
|
ipType = ipv6.ProtocolNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return gonet.DialUDP(ns.ipstack, nil, remoteAddress, ipType)
|
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) injectOutbound() {
|
|
|
|
for {
|
|
|
|
packetInfo, ok := ns.linkEP.ReadContext(context.Background())
|
|
|
|
if !ok {
|
|
|
|
ns.logf("[v2] ReadContext-for-write = ok=false")
|
|
|
|
continue
|
2021-01-15 14:16:28 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
pkt := packetInfo.Pkt
|
|
|
|
hdrNetwork := pkt.NetworkHeader()
|
|
|
|
hdrTransport := pkt.TransportHeader()
|
2020-09-03 22:45:41 +00:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
full := make([]byte, 0, pkt.Size())
|
|
|
|
full = append(full, hdrNetwork.View()...)
|
|
|
|
full = append(full, hdrTransport.View()...)
|
2021-03-18 02:34:21 +00:00
|
|
|
full = append(full, pkt.Data().AsRange().AsView()...)
|
2021-03-03 18:37:01 +00:00
|
|
|
if debugNetstack {
|
|
|
|
ns.logf("[v2] packet Write out: % x", full)
|
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
if err := ns.tundev.InjectOutbound(full); err != nil {
|
|
|
|
log.Printf("netstack inject outbound: %v", err)
|
2020-09-03 22:45:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:32:46 +00:00
|
|
|
// isLocalIP reports whether ip is a Tailscale IP assigned to this
|
|
|
|
// node directly (but not a subnet-routed IP).
|
|
|
|
func (ns *Impl) isLocalIP(ip netaddr.IP) bool {
|
|
|
|
return ns.atomicIsLocalIPFunc.Load().(func(netaddr.IP) bool)(ip)
|
|
|
|
}
|
|
|
|
|
2021-03-27 06:13:20 +00:00
|
|
|
func (ns *Impl) injectInbound(p *packet.Parsed, t *tstun.Wrapper) filter.Response {
|
2021-05-15 01:07:28 +00:00
|
|
|
if ns.onlySubnets && ns.isLocalIP(p.Dst.IP()) {
|
2021-05-05 19:32:46 +00:00
|
|
|
// In hybrid ("only subnets") mode, bail out early if
|
|
|
|
// the traffic is destined for an actual Tailscale
|
|
|
|
// address. The real host OS interface will handle it.
|
|
|
|
return filter.Accept
|
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
var pn tcpip.NetworkProtocolNumber
|
|
|
|
switch p.IPVersion {
|
|
|
|
case 4:
|
|
|
|
pn = header.IPv4ProtocolNumber
|
|
|
|
case 6:
|
|
|
|
pn = header.IPv6ProtocolNumber
|
|
|
|
}
|
2021-03-03 18:37:01 +00:00
|
|
|
if debugNetstack {
|
|
|
|
ns.logf("[v2] packet in (from %v): % x", p.Src, p.Buffer())
|
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
vv := buffer.View(append([]byte(nil), p.Buffer()...)).ToVectorisedView()
|
|
|
|
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
|
|
|
Data: vv,
|
2020-09-03 22:45:41 +00:00
|
|
|
})
|
2021-02-25 19:18:16 +00:00
|
|
|
ns.linkEP.InjectInbound(pn, packetBuf)
|
2021-05-06 03:42:07 +00:00
|
|
|
|
|
|
|
// We've now delivered this to netstack, so we're done.
|
|
|
|
// Instead of returning a filter.Accept here (which would also
|
|
|
|
// potentially deliver it to the host OS), and instead of
|
|
|
|
// filter.Drop (which would log about rejected traffic),
|
|
|
|
// instead return filter.DropSilently which just quietly stops
|
|
|
|
// processing it in the tstun TUN wrapper.
|
|
|
|
return filter.DropSilently
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2020-09-03 22:45:41 +00:00
|
|
|
|
2021-08-25 21:39:09 +00:00
|
|
|
func netaddrIPFromNetstackIP(s tcpip.Address) netaddr.IP {
|
|
|
|
switch len(s) {
|
|
|
|
case 4:
|
|
|
|
return netaddr.IPv4(s[0], s[1], s[2], s[3])
|
|
|
|
case 16:
|
|
|
|
var a [16]byte
|
|
|
|
copy(a[:], s)
|
|
|
|
return netaddr.IPFrom16(a)
|
|
|
|
}
|
|
|
|
return netaddr.IP{}
|
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) acceptTCP(r *tcp.ForwarderRequest) {
|
2021-04-21 18:50:48 +00:00
|
|
|
reqDetails := r.ID()
|
2021-03-03 18:37:01 +00:00
|
|
|
if debugNetstack {
|
2021-04-21 18:50:48 +00:00
|
|
|
ns.logf("[v2] TCP ForwarderRequest: %s", stringifyTEI(reqDetails))
|
2021-03-03 18:37:01 +00:00
|
|
|
}
|
2021-08-25 21:39:09 +00:00
|
|
|
clientRemoteIP := netaddrIPFromNetstackIP(reqDetails.RemoteAddress)
|
|
|
|
if !clientRemoteIP.IsValid() {
|
|
|
|
ns.logf("invalid RemoteAddress in TCP ForwarderRequest: %s", stringifyTEI(reqDetails))
|
|
|
|
r.Complete(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dialIP := netaddrIPFromNetstackIP(reqDetails.LocalAddress)
|
|
|
|
isTailscaleIP := tsaddr.IsTailscaleIP(dialIP)
|
2021-03-29 18:33:05 +00:00
|
|
|
defer func() {
|
|
|
|
if !isTailscaleIP {
|
|
|
|
// if this is a subnet IP, we added this in before the TCP handshake
|
|
|
|
// so netstack is happy TCP-handshaking as a subnet IP
|
2021-08-25 21:39:09 +00:00
|
|
|
ns.removeSubnetAddress(dialIP)
|
2021-03-29 18:33:05 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-02-25 19:18:16 +00:00
|
|
|
var wq waiter.Queue
|
|
|
|
ep, err := r.CreateEndpoint(&wq)
|
|
|
|
if err != nil {
|
|
|
|
r.Complete(true)
|
|
|
|
return
|
|
|
|
}
|
2021-05-14 15:53:55 +00:00
|
|
|
r.Complete(false)
|
2021-08-25 21:39:09 +00:00
|
|
|
|
2021-08-27 18:10:56 +00:00
|
|
|
// The ForwarderRequest.CreateEndpoint above asynchronously
|
|
|
|
// starts the TCP handshake. Note that the gonet.TCPConn
|
|
|
|
// methods c.RemoteAddr() and c.LocalAddr() will return nil
|
|
|
|
// until the handshake actually completes. But we have the
|
|
|
|
// remote address in reqDetails instead, so we don't use
|
|
|
|
// gonet.TCPConn.RemoteAddr. The byte copies in both
|
|
|
|
// directions to/from the gonet.TCPConn in forwardTCP will
|
|
|
|
// block until the TCP handshake is complete.
|
2021-05-14 15:53:55 +00:00
|
|
|
c := gonet.NewTCPConn(&wq, ep)
|
2021-08-25 21:39:09 +00:00
|
|
|
|
2021-05-14 15:53:55 +00:00
|
|
|
if ns.ForwardTCPIn != nil {
|
|
|
|
ns.ForwardTCPIn(c, reqDetails.LocalPort)
|
|
|
|
return
|
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
if isTailscaleIP {
|
2021-08-25 21:39:09 +00:00
|
|
|
dialIP = netaddr.IPv4(127, 0, 0, 1)
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2021-08-25 21:39:09 +00:00
|
|
|
dialAddr := netaddr.IPPortFrom(dialIP, uint16(reqDetails.LocalPort))
|
|
|
|
ns.forwardTCP(c, clientRemoteIP, &wq, dialAddr)
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
2020-09-03 22:45:41 +00:00
|
|
|
|
2021-08-25 21:39:09 +00:00
|
|
|
func (ns *Impl) forwardTCP(client *gonet.TCPConn, clientRemoteIP netaddr.IP, wq *waiter.Queue, dialAddr netaddr.IPPort) {
|
2021-02-25 19:18:16 +00:00
|
|
|
defer client.Close()
|
2021-08-25 21:39:09 +00:00
|
|
|
dialAddrStr := dialAddr.String()
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("[v2] netstack: forwarding incoming connection to %s", dialAddrStr)
|
2021-08-25 21:39:09 +00:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
waitEntry, notifyCh := waiter.NewChannelEntry(nil)
|
|
|
|
wq.EventRegister(&waitEntry, waiter.EventHUp)
|
|
|
|
defer wq.EventUnregister(&waitEntry)
|
|
|
|
done := make(chan bool)
|
|
|
|
// netstack doesn't close the notification channel automatically if there was no
|
|
|
|
// hup signal, so we close done after we're done to not leak the goroutine below.
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-notifyCh:
|
|
|
|
case <-done:
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
cancel()
|
2020-09-03 22:45:41 +00:00
|
|
|
}()
|
2021-03-03 16:17:14 +00:00
|
|
|
var stdDialer net.Dialer
|
2021-03-29 18:33:05 +00:00
|
|
|
server, err := stdDialer.DialContext(ctx, "tcp", dialAddrStr)
|
2021-02-25 19:18:16 +00:00
|
|
|
if err != nil {
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("netstack: could not connect to local server at %s: %v", dialAddrStr, err)
|
2021-02-25 19:18:16 +00:00
|
|
|
return
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
defer server.Close()
|
2021-03-15 21:59:35 +00:00
|
|
|
backendLocalAddr := server.LocalAddr().(*net.TCPAddr)
|
|
|
|
backendLocalIPPort, _ := netaddr.FromStdAddr(backendLocalAddr.IP, backendLocalAddr.Port, backendLocalAddr.Zone)
|
|
|
|
ns.e.RegisterIPPortIdentity(backendLocalIPPort, clientRemoteIP)
|
|
|
|
defer ns.e.UnregisterIPPortIdentity(backendLocalIPPort)
|
2021-03-02 20:14:29 +00:00
|
|
|
connClosed := make(chan error, 2)
|
2021-02-25 19:18:16 +00:00
|
|
|
go func() {
|
2021-03-02 20:14:29 +00:00
|
|
|
_, err := io.Copy(server, client)
|
|
|
|
connClosed <- err
|
2021-02-25 19:18:16 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
2021-03-02 20:14:29 +00:00
|
|
|
_, err := io.Copy(client, server)
|
|
|
|
connClosed <- err
|
2021-02-25 19:18:16 +00:00
|
|
|
}()
|
2021-03-02 20:14:29 +00:00
|
|
|
err = <-connClosed
|
|
|
|
if err != nil {
|
|
|
|
ns.logf("proxy connection closed with error: %v", err)
|
|
|
|
}
|
2021-03-29 18:33:05 +00:00
|
|
|
ns.logf("[v2] netstack: forwarder connection to %s closed", dialAddrStr)
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
func (ns *Impl) acceptUDP(r *udp.ForwarderRequest) {
|
2021-07-21 15:38:13 +00:00
|
|
|
sess := r.ID()
|
2021-04-21 18:50:48 +00:00
|
|
|
if debugNetstack {
|
2021-07-21 15:38:13 +00:00
|
|
|
ns.logf("[v2] UDP ForwarderRequest: %v", stringifyTEI(sess))
|
2021-04-21 18:50:48 +00:00
|
|
|
}
|
2021-02-25 19:18:16 +00:00
|
|
|
var wq waiter.Queue
|
|
|
|
ep, err := r.CreateEndpoint(&wq)
|
|
|
|
if err != nil {
|
2021-04-21 19:10:28 +00:00
|
|
|
ns.logf("acceptUDP: could not create endpoint: %v", err)
|
2021-02-25 19:18:16 +00:00
|
|
|
return
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
2021-07-21 15:38:13 +00:00
|
|
|
dstAddr, ok := ipPortOfNetstackAddr(sess.LocalAddress, sess.LocalPort)
|
|
|
|
if !ok {
|
2021-03-08 18:22:35 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-21 15:38:13 +00:00
|
|
|
srcAddr, ok := ipPortOfNetstackAddr(sess.RemoteAddress, sess.RemotePort)
|
|
|
|
if !ok {
|
2021-03-08 18:22:35 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-21 15:38:13 +00:00
|
|
|
|
2021-02-25 19:18:16 +00:00
|
|
|
c := gonet.NewUDPConn(ns.ipstack, &wq, ep)
|
2021-07-21 15:38:13 +00:00
|
|
|
go ns.forwardUDP(c, &wq, srcAddr, dstAddr)
|
2021-02-25 19:18:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 15:38:13 +00:00
|
|
|
// forwardUDP proxies between client (with addr clientAddr) and dstAddr.
|
|
|
|
//
|
|
|
|
// dstAddr may be either a local Tailscale IP, in which we case we proxy to
|
|
|
|
// 127.0.0.1, or any other IP (from an advertised subnet), in which case we
|
|
|
|
// proxy to it directly.
|
|
|
|
func (ns *Impl) forwardUDP(client *gonet.UDPConn, wq *waiter.Queue, clientAddr, dstAddr netaddr.IPPort) {
|
|
|
|
port, srcPort := dstAddr.Port(), clientAddr.Port()
|
2021-03-08 18:22:35 +00:00
|
|
|
ns.logf("[v2] netstack: forwarding incoming UDP connection on port %v", port)
|
2021-07-21 15:38:13 +00:00
|
|
|
|
|
|
|
var backendListenAddr *net.UDPAddr
|
|
|
|
var backendRemoteAddr *net.UDPAddr
|
|
|
|
isLocal := ns.isLocalIP(dstAddr.IP())
|
|
|
|
if isLocal {
|
|
|
|
backendRemoteAddr = &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: int(port)}
|
|
|
|
backendListenAddr = &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: int(srcPort)}
|
|
|
|
} else {
|
|
|
|
backendRemoteAddr = dstAddr.UDPAddr()
|
|
|
|
if dstAddr.IP().Is4() {
|
|
|
|
backendListenAddr = &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: int(srcPort)}
|
|
|
|
} else {
|
|
|
|
backendListenAddr = &net.UDPAddr{IP: net.ParseIP("::"), Port: int(srcPort)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
backendConn, err := net.ListenUDP("udp", backendListenAddr)
|
2021-03-08 18:22:35 +00:00
|
|
|
if err != nil {
|
2021-07-21 15:38:13 +00:00
|
|
|
ns.logf("netstack: could not bind local port %v: %v, trying again with random port", backendListenAddr.Port, err)
|
2021-03-15 21:59:35 +00:00
|
|
|
backendListenAddr.Port = 0
|
2021-07-21 15:38:13 +00:00
|
|
|
backendConn, err = net.ListenUDP("udp", backendListenAddr)
|
2020-09-03 22:45:41 +00:00
|
|
|
if err != nil {
|
2021-07-21 15:38:13 +00:00
|
|
|
ns.logf("netstack: could not create UDP socket, preventing forwarding to %v: %v", dstAddr, err)
|
2021-03-08 18:22:35 +00:00
|
|
|
return
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 21:59:35 +00:00
|
|
|
backendLocalAddr := backendConn.LocalAddr().(*net.UDPAddr)
|
|
|
|
backendLocalIPPort, ok := netaddr.FromStdAddr(backendListenAddr.IP, backendLocalAddr.Port, backendLocalAddr.Zone)
|
|
|
|
if !ok {
|
|
|
|
ns.logf("could not get backend local IP:port from %v:%v", backendLocalAddr.IP, backendLocalAddr.Port)
|
|
|
|
}
|
2021-07-21 15:38:13 +00:00
|
|
|
if isLocal {
|
|
|
|
ns.e.RegisterIPPortIdentity(backendLocalIPPort, dstAddr.IP())
|
|
|
|
}
|
2021-03-08 18:22:35 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-07-21 15:38:13 +00:00
|
|
|
|
|
|
|
idleTimeout := 2 * time.Minute
|
|
|
|
if port == 53 {
|
|
|
|
// Make DNS packet copies time out much sooner.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz): make DNS queries over UDP forwarding even
|
|
|
|
// cheaper by adding an additional idleTimeout post-DNS-reply.
|
|
|
|
// For instance, after the DNS response goes back out, then only
|
|
|
|
// wait a few seconds (or zero, really)
|
|
|
|
idleTimeout = 30 * time.Second
|
|
|
|
}
|
|
|
|
timer := time.AfterFunc(idleTimeout, func() {
|
|
|
|
if isLocal {
|
|
|
|
ns.e.UnregisterIPPortIdentity(backendLocalIPPort)
|
|
|
|
}
|
|
|
|
ns.logf("netstack: UDP session between %s and %s timed out", backendListenAddr, backendRemoteAddr)
|
2021-03-08 18:22:35 +00:00
|
|
|
cancel()
|
|
|
|
client.Close()
|
|
|
|
backendConn.Close()
|
|
|
|
})
|
|
|
|
extend := func() {
|
2021-07-21 15:38:13 +00:00
|
|
|
timer.Reset(idleTimeout)
|
2021-03-08 18:22:35 +00:00
|
|
|
}
|
2021-07-21 15:38:13 +00:00
|
|
|
startPacketCopy(ctx, cancel, client, clientAddr.UDPAddr(), backendConn, ns.logf, extend)
|
2021-03-08 18:43:01 +00:00
|
|
|
startPacketCopy(ctx, cancel, backendConn, backendRemoteAddr, client, ns.logf, extend)
|
2021-07-21 15:38:13 +00:00
|
|
|
if isLocal {
|
|
|
|
// Wait for the copies to be done before decrementing the
|
|
|
|
// subnet address count to potentially remove the route.
|
|
|
|
<-ctx.Done()
|
|
|
|
ns.removeSubnetAddress(dstAddr.IP())
|
|
|
|
}
|
2021-03-08 18:22:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 18:43:01 +00:00
|
|
|
func startPacketCopy(ctx context.Context, cancel context.CancelFunc, dst net.PacketConn, dstAddr net.Addr, src net.PacketConn, logf logger.Logf, extend func()) {
|
2021-07-21 15:38:13 +00:00
|
|
|
if debugNetstack {
|
|
|
|
logf("[v2] netstack: startPacketCopy to %v (%T) from %T", dstAddr, dst, src)
|
|
|
|
}
|
2021-03-08 18:22:35 +00:00
|
|
|
go func() {
|
2021-03-08 18:43:01 +00:00
|
|
|
defer cancel() // tear down the other direction's copy
|
2021-03-08 18:22:35 +00:00
|
|
|
pkt := make([]byte, mtu)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
n, srcAddr, err := src.ReadFrom(pkt)
|
|
|
|
if err != nil {
|
|
|
|
if ctx.Err() == nil {
|
|
|
|
logf("read packet from %s failed: %v", srcAddr, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = dst.WriteTo(pkt[:n], dstAddr)
|
|
|
|
if err != nil {
|
|
|
|
if ctx.Err() == nil {
|
|
|
|
logf("write packet to %s failed: %v", dstAddr, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-03-08 18:43:01 +00:00
|
|
|
if debugNetstack {
|
|
|
|
logf("[v2] wrote UDP packet %s -> %s", srcAddr, dstAddr)
|
|
|
|
}
|
2021-03-08 18:22:35 +00:00
|
|
|
extend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-09-03 22:45:41 +00:00
|
|
|
}
|
2021-04-21 18:50:48 +00:00
|
|
|
|
|
|
|
func stringifyTEI(tei stack.TransportEndpointID) string {
|
|
|
|
localHostPort := net.JoinHostPort(tei.LocalAddress.String(), strconv.Itoa(int(tei.LocalPort)))
|
|
|
|
remoteHostPort := net.JoinHostPort(tei.RemoteAddress.String(), strconv.Itoa(int(tei.RemotePort)))
|
|
|
|
return fmt.Sprintf("%s -> %s", remoteHostPort, localHostPort)
|
|
|
|
}
|
2021-07-21 15:38:13 +00:00
|
|
|
|
|
|
|
func ipPortOfNetstackAddr(a tcpip.Address, port uint16) (ipp netaddr.IPPort, ok bool) {
|
|
|
|
return netaddr.FromStdAddr(net.IP(a), int(port), "") // TODO(bradfitz): can do without allocs
|
|
|
|
}
|