From c839012580cdf84a46121dc453a73811dd86d688 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 17 Jan 2019 23:06:59 +0000 Subject: [PATCH] Fix source address selection --- src/yggdrasil/tcp.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index ca9ea09b..78e39efc 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -245,15 +245,27 @@ func (iface *tcpInterface) call(saddr string, socksaddr *string, sintf string) { if err != nil { continue } - if (src.To4() != nil) == (dst.IP.To4() != nil) { - if addrindex == len(addrs)-1 || src.IsGlobalUnicast() { - dialer.LocalAddr = &net.TCPAddr{ - IP: src, - Port: 0, - Zone: sintf, - } - break + if src.Equal(dst.IP) { + continue + } + if !src.IsGlobalUnicast() && !src.IsLinkLocalUnicast() { + continue + } + bothglobal := src.IsGlobalUnicast() == dst.IP.IsGlobalUnicast() + bothlinklocal := src.IsLinkLocalUnicast() == dst.IP.IsLinkLocalUnicast() + if !bothglobal && !bothlinklocal { + continue + } + if (src.To4() != nil) != (dst.IP.To4() != nil) { + continue + } + if bothglobal || bothlinklocal || addrindex == len(addrs)-1 { + dialer.LocalAddr = &net.TCPAddr{ + IP: src, + Port: 0, + Zone: sintf, } + break } } if dialer.LocalAddr == nil {