mirror of
https://github.com/tailscale/tailscale.git
synced 2025-03-01 19:51:58 +00:00
wgengine/netstack: add support for incoming UDP connections
Updates #504 Updates #707 Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
parent
43b30e463c
commit
7325b5a7ba
@ -18,6 +18,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gvisor.dev/gvisor/pkg/tcpip"
|
"gvisor.dev/gvisor/pkg/tcpip"
|
||||||
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
|
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
|
||||||
@ -60,6 +61,7 @@ type Impl struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const nicID = 1
|
const nicID = 1
|
||||||
|
const mtu = 1500
|
||||||
|
|
||||||
// Create creates and populates a new Impl.
|
// Create creates and populates a new Impl.
|
||||||
func Create(logf logger.Logf, tundev *tstun.TUN, e wgengine.Engine, mc *magicsock.Conn) (*Impl, error) {
|
func Create(logf logger.Logf, tundev *tstun.TUN, e wgengine.Engine, mc *magicsock.Conn) (*Impl, error) {
|
||||||
@ -79,7 +81,6 @@ func Create(logf logger.Logf, tundev *tstun.TUN, e wgengine.Engine, mc *magicsoc
|
|||||||
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
||||||
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
||||||
})
|
})
|
||||||
const mtu = 1500
|
|
||||||
linkEP := channel.New(512, mtu, "")
|
linkEP := channel.New(512, mtu, "")
|
||||||
if tcpipProblem := ipstack.CreateNIC(nicID, linkEP); tcpipProblem != nil {
|
if tcpipProblem := ipstack.CreateNIC(nicID, linkEP); tcpipProblem != nil {
|
||||||
return nil, fmt.Errorf("could not create netstack NIC: %v", tcpipProblem)
|
return nil, fmt.Errorf("could not create netstack NIC: %v", tcpipProblem)
|
||||||
@ -390,18 +391,75 @@ func (ns *Impl) acceptUDP(r *udp.ForwarderRequest) {
|
|||||||
ns.logf("Could not create endpoint, exiting")
|
ns.logf("Could not create endpoint, exiting")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
localAddr, err := ep.GetLocalAddress()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
remoteAddr, err := ep.GetRemoteAddress()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
c := gonet.NewUDPConn(ns.ipstack, &wq, ep)
|
c := gonet.NewUDPConn(ns.ipstack, &wq, ep)
|
||||||
go echoUDP(c)
|
go ns.forwardUDP(c, &wq, localAddr, remoteAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func echoUDP(c *gonet.UDPConn) {
|
func (ns *Impl) forwardUDP(client *gonet.UDPConn, wq *waiter.Queue, clientLocalAddr, clientRemoteAddr tcpip.FullAddress) {
|
||||||
buf := make([]byte, 1500)
|
port := clientLocalAddr.Port
|
||||||
for {
|
ns.logf("[v2] netstack: forwarding incoming UDP connection on port %v", port)
|
||||||
n, err := c.Read(buf)
|
backendLocalAddr := &net.UDPAddr{Port: int(clientRemoteAddr.Port)}
|
||||||
|
backendRemoteAddr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: int(port)}
|
||||||
|
backendConn, err := net.ListenUDP("udp4", backendLocalAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
ns.logf("netstack: could not bind local port %v: %v, trying again with random port", clientRemoteAddr.Port, err)
|
||||||
|
backendConn, err = net.ListenUDP("udp4", nil)
|
||||||
|
if err != nil {
|
||||||
|
ns.logf("netstack: could not connect to local UDP server on port %v: %v", port, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
c.Write(buf[:n])
|
|
||||||
}
|
}
|
||||||
c.Close()
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
timer := time.AfterFunc(2*time.Minute, func() {
|
||||||
|
ns.logf("netstack: forwarder UDP connection on port %v closed", port)
|
||||||
|
cancel()
|
||||||
|
client.Close()
|
||||||
|
backendConn.Close()
|
||||||
|
})
|
||||||
|
extend := func() {
|
||||||
|
timer.Reset(2 * time.Minute)
|
||||||
|
}
|
||||||
|
startPacketCopy(ctx, client, &net.UDPAddr{
|
||||||
|
IP: net.ParseIP(clientRemoteAddr.Addr.String()),
|
||||||
|
Port: int(clientRemoteAddr.Port),
|
||||||
|
}, backendConn, ns.logf, extend)
|
||||||
|
startPacketCopy(ctx, backendConn, backendRemoteAddr, client, ns.logf, extend)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func startPacketCopy(ctx context.Context, dst net.PacketConn, dstAddr net.Addr, src net.PacketConn, logf logger.Logf, extend func()) {
|
||||||
|
go func() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
logf("[v2] wrote UDP packet %s -> %s", srcAddr, dstAddr)
|
||||||
|
extend()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user