Prepare for the new wireguard-go API.

Signed-off-by: Aleksandar Pesic <peske.nis@gmail.com>
pull/1009/head
Aleksandar Pesic 4 years ago
parent 943860fde7
commit 274d32d0aa

@ -39,12 +39,8 @@ import (
// address a few rare corner cases, but is unlikely to significantly // address a few rare corner cases, but is unlikely to significantly
// help with MTU issues compared to a static 1280B implementation. // help with MTU issues compared to a static 1280B implementation.
func monitorDefaultRoutes(tun *tun.NativeTun) (*winipcfg.RouteChangeCallback, error) { func monitorDefaultRoutes(tun *tun.NativeTun) (*winipcfg.RouteChangeCallback, error) {
guid := tun.GUID() ourLuid := winipcfg.LUID(tun.LUID())
ourLuid, err := winipcfg.LUIDFromGUID(&guid)
lastMtu := uint32(0) lastMtu := uint32(0)
if err != nil {
return nil, fmt.Errorf("error mapping GUID %v to LUID: %w", guid, err)
}
doIt := func() error { doIt := func() error {
mtu, err := getDefaultRouteMTU() mtu, err := getDefaultRouteMTU()
if err != nil { if err != nil {
@ -91,7 +87,7 @@ func monitorDefaultRoutes(tun *tun.NativeTun) (*winipcfg.RouteChangeCallback, er
} }
return nil return nil
} }
err = doIt() err := doIt()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -159,7 +155,7 @@ func getDefaultRouteMTU() (uint32, error) {
// setPrivateNetwork marks the provided network adapter's category to private. // setPrivateNetwork marks the provided network adapter's category to private.
// It returns (false, nil) if the adapter was not found. // It returns (false, nil) if the adapter was not found.
func setPrivateNetwork(ifcGUID *windows.GUID) (bool, error) { func setPrivateNetwork(ifcLUID winipcfg.LUID) (bool, error) {
// NLM_NETWORK_CATEGORY values. // NLM_NETWORK_CATEGORY values.
const ( const (
categoryPublic = 0 categoryPublic = 0
@ -167,6 +163,11 @@ func setPrivateNetwork(ifcGUID *windows.GUID) (bool, error) {
categoryDomain = 2 categoryDomain = 2
) )
ifcGUID, err := ifcLUID.GUID()
if err != nil {
return false, fmt.Errorf("ifcLUID.GUID: %v", err)
}
// Lock OS thread when using OLE, which seems to be a requirement // Lock OS thread when using OLE, which seems to be a requirement
// from the Microsoft docs. go-ole doesn't seem to handle it automatically. // from the Microsoft docs. go-ole doesn't seem to handle it automatically.
// https://github.com/tailscale/tailscale/issues/921#issuecomment-727526807 // https://github.com/tailscale/tailscale/issues/921#issuecomment-727526807
@ -222,12 +223,8 @@ func setPrivateNetwork(ifcGUID *windows.GUID) (bool, error) {
return false, nil return false, nil
} }
// interfaceFromGUID returns IPAdapterAddresses with specified GUID. // interfaceFromLUID returns IPAdapterAddresses with specified GUID.
func interfaceFromGUID(guid *windows.GUID, flags winipcfg.GAAFlags) (*winipcfg.IPAdapterAddresses, error) { func interfaceFromLUID(luid winipcfg.LUID, flags winipcfg.GAAFlags) (*winipcfg.IPAdapterAddresses, error) {
luid, err := winipcfg.LUIDFromGUID(guid)
if err != nil {
return nil, err
}
addresses, err := winipcfg.GetAdaptersAddresses(windows.AF_UNSPEC, flags) addresses, err := winipcfg.GetAdaptersAddresses(windows.AF_UNSPEC, flags)
if err != nil { if err != nil {
return nil, err return nil, err
@ -237,13 +234,13 @@ func interfaceFromGUID(guid *windows.GUID, flags winipcfg.GAAFlags) (*winipcfg.I
return addr, nil return addr, nil
} }
} }
return nil, fmt.Errorf("interfaceFromGUID: interface with LUID %v (from GUID %v) not found", luid, guid) return nil, fmt.Errorf("interfaceFromLUID: interface with LUID %v not found", luid)
} }
func configureInterface(cfg *Config, tun *tun.NativeTun) error { func configureInterface(cfg *Config, tun *tun.NativeTun) error {
const mtu = 0 const mtu = 0
guid := tun.GUID() luid := winipcfg.LUID(tun.LUID())
iface, err := interfaceFromGUID(&guid, iface, err := interfaceFromLUID(luid,
// Issue 474: on early boot, when the network is still // Issue 474: on early boot, when the network is still
// coming up, if the Tailscale service comes up first, // coming up, if the Tailscale service comes up first,
// the Tailscale adapter it finds might not have the // the Tailscale adapter it finds might not have the
@ -260,7 +257,7 @@ func configureInterface(cfg *Config, tun *tun.NativeTun) error {
// does. // does.
const tries = 20 const tries = 20
for i := 0; i < tries; i++ { for i := 0; i < tries; i++ {
found, err := setPrivateNetwork(&guid) found, err := setPrivateNetwork(luid)
if err != nil { if err != nil {
log.Printf("setPrivateNetwork(try=%d): %v", i, err) log.Printf("setPrivateNetwork(try=%d): %v", i, err)
} else { } else {
@ -271,7 +268,7 @@ func configureInterface(cfg *Config, tun *tun.NativeTun) error {
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
log.Printf("setPrivateNetwork: adapter %v not found after %d tries, giving up", guid, tries) log.Printf("setPrivateNetwork: adapter LUID %v not found after %d tries, giving up", luid, tries)
}() }()
var firstGateway4 *net.IP var firstGateway4 *net.IP
@ -353,7 +350,7 @@ func configureInterface(cfg *Config, tun *tun.NativeTun) error {
} }
// Re-read interface after syncAddresses. // Re-read interface after syncAddresses.
iface, err = interfaceFromGUID(&guid, iface, err = interfaceFromLUID(luid,
// Issue 474: on early boot, when the network is still // Issue 474: on early boot, when the network is still
// coming up, if the Tailscale service comes up first, // coming up, if the Tailscale service comes up first,
// the Tailscale adapter it finds might not have the // the Tailscale adapter it finds might not have the

@ -37,10 +37,15 @@ func newUserspaceRouter(logf logger.Logf, wgdev *device.Device, tundev tun.Devic
} }
nativeTun := tundev.(*tun.NativeTun) nativeTun := tundev.(*tun.NativeTun)
guid := nativeTun.GUID().String() luid := winipcfg.LUID(nativeTun.LUID())
guid, err := luid.GUID()
if err != nil {
return nil, err
}
mconfig := dns.ManagerConfig{ mconfig := dns.ManagerConfig{
Logf: logf, Logf: logf,
InterfaceName: guid, InterfaceName: guid.String(),
} }
return &winRouter{ return &winRouter{

Loading…
Cancel
Save