2020-02-05 22:16:58 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2020-04-30 20:20:09 +00:00
|
|
|
package router
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2020-11-16 17:55:44 +00:00
|
|
|
"runtime"
|
2020-02-05 22:16:58 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2020-09-22 16:13:45 +00:00
|
|
|
"github.com/go-multierror/multierror"
|
2020-02-13 05:11:41 +00:00
|
|
|
ole "github.com/go-ole/go-ole"
|
2020-02-05 22:16:58 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
|
|
|
"golang.org/x/sys/windows"
|
2020-09-26 02:11:05 +00:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2020-09-10 18:40:02 +00:00
|
|
|
"tailscale.com/net/interfaces"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/wgengine/winnet"
|
|
|
|
)
|
|
|
|
|
2020-09-10 18:40:02 +00:00
|
|
|
// monitorDefaultRoutes subscribes to route change events and updates
|
|
|
|
// the Tailscale tunnel interface's MTU to match that of the
|
|
|
|
// underlying default route.
|
|
|
|
//
|
|
|
|
// This is an attempt at making the MTU mostly correct, but in
|
|
|
|
// practice this entire piece of code ends up just using the 1280
|
|
|
|
// value passed in at device construction time. This code might make
|
|
|
|
// the MTU go lower due to very low-MTU IPv4 interfaces.
|
|
|
|
//
|
|
|
|
// TODO: this code is insufficient to control the MTU correctly. The
|
|
|
|
// correct way to do it is per-peer PMTU discovery, and synthesizing
|
|
|
|
// ICMP fragmentation-needed messages within tailscaled. This code may
|
|
|
|
// address a few rare corner cases, but is unlikely to significantly
|
|
|
|
// help with MTU issues compared to a static 1280B implementation.
|
|
|
|
func monitorDefaultRoutes(tun *tun.NativeTun) (*winipcfg.RouteChangeCallback, error) {
|
2020-02-05 22:16:58 +00:00
|
|
|
guid := tun.GUID()
|
2020-09-26 02:11:05 +00:00
|
|
|
ourLuid, err := winipcfg.LUIDFromGUID(&guid)
|
2020-02-05 22:16:58 +00:00
|
|
|
lastMtu := uint32(0)
|
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
return nil, fmt.Errorf("error mapping GUID %v to LUID: %w", guid, err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
doIt := func() error {
|
2020-09-10 18:40:02 +00:00
|
|
|
mtu, err := getDefaultRouteMTU()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
return fmt.Errorf("error getting default route MTU: %w", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-09-10 18:40:02 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
if mtu > 0 && (lastMtu == 0 || lastMtu != mtu) {
|
2020-09-26 02:11:05 +00:00
|
|
|
iface, err := ourLuid.IPInterface(windows.AF_INET)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
return fmt.Errorf("error getting v4 interface: %w", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
iface.NLMTU = mtu - 80
|
2020-08-07 19:06:32 +00:00
|
|
|
// If the TUN device was created with a smaller MTU,
|
|
|
|
// though, such as 1280, we don't want to go bigger than
|
|
|
|
// configured. (See the comment on minimalMTU in the
|
|
|
|
// wgengine package.)
|
2020-09-26 02:11:05 +00:00
|
|
|
if min, err := tun.MTU(); err == nil && min < int(iface.NLMTU) {
|
|
|
|
iface.NLMTU = uint32(min)
|
2020-08-07 19:06:32 +00:00
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
if iface.NLMTU < 576 {
|
|
|
|
iface.NLMTU = 576
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
err = iface.Set()
|
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
return fmt.Errorf("error setting v4 MTU: %w", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
tun.ForceMTU(int(iface.NLMTU))
|
|
|
|
iface, err = ourLuid.IPInterface(windows.AF_INET6)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
if !errors.Is(err, windows.ERROR_NOT_FOUND) {
|
|
|
|
return fmt.Errorf("error getting v6 interface: %w", err)
|
2020-09-11 18:59:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-26 02:11:05 +00:00
|
|
|
iface.NLMTU = mtu - 80
|
|
|
|
if iface.NLMTU < 1280 {
|
|
|
|
iface.NLMTU = 1280
|
2020-09-11 18:59:48 +00:00
|
|
|
}
|
|
|
|
err = iface.Set()
|
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
return fmt.Errorf("error setting v6 MTU: %w", err)
|
2020-09-11 18:59:48 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
lastMtu = mtu
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = doIt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
cb, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
|
2020-02-05 22:16:58 +00:00
|
|
|
//fmt.Printf("MonitorDefaultRoutes: changed: %v\n", route.DestinationPrefix)
|
|
|
|
if route.DestinationPrefix.PrefixLength == 0 {
|
|
|
|
_ = doIt()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cb, nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 18:40:02 +00:00
|
|
|
func getDefaultRouteMTU() (uint32, error) {
|
|
|
|
mtus, err := interfaces.NonTailscaleMTUs()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
routes, err := winipcfg.GetIPForwardTable2(windows.AF_INET)
|
2020-09-10 18:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
best := ^uint32(0)
|
|
|
|
mtu := uint32(0)
|
|
|
|
for _, route := range routes {
|
|
|
|
if route.DestinationPrefix.PrefixLength != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
routeMTU := mtus[route.InterfaceLUID]
|
2020-09-10 18:40:02 +00:00
|
|
|
if routeMTU == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if route.Metric < best {
|
|
|
|
best = route.Metric
|
|
|
|
mtu = routeMTU
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
routes, err = winipcfg.GetIPForwardTable2(windows.AF_INET6)
|
2020-09-10 18:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
best = ^uint32(0)
|
|
|
|
for _, route := range routes {
|
|
|
|
if route.DestinationPrefix.PrefixLength != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-26 02:11:05 +00:00
|
|
|
routeMTU := mtus[route.InterfaceLUID]
|
2020-09-10 18:40:02 +00:00
|
|
|
if routeMTU == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if route.Metric < best {
|
|
|
|
best = route.Metric
|
|
|
|
if routeMTU < mtu {
|
|
|
|
mtu = routeMTU
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mtu, nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:41:28 +00:00
|
|
|
// setPrivateNetwork marks the provided network adapter's category to private.
|
|
|
|
// It returns (false, nil) if the adapter was not found.
|
|
|
|
func setPrivateNetwork(ifcGUID *windows.GUID) (bool, error) {
|
|
|
|
// NLM_NETWORK_CATEGORY values.
|
|
|
|
const (
|
|
|
|
categoryPublic = 0
|
|
|
|
categoryPrivate = 1
|
|
|
|
categoryDomain = 2
|
|
|
|
)
|
2020-11-16 17:55:44 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
// https://github.com/tailscale/tailscale/issues/921#issuecomment-727526807
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2020-09-16 21:41:28 +00:00
|
|
|
var c ole.Connection
|
|
|
|
if err := c.Initialize(); err != nil {
|
2020-02-21 20:22:29 +00:00
|
|
|
return false, fmt.Errorf("c.Initialize: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
defer c.Uninitialize()
|
|
|
|
|
|
|
|
m, err := winnet.NewNetworkListManager(&c)
|
|
|
|
if err != nil {
|
2020-02-21 20:22:29 +00:00
|
|
|
return false, fmt.Errorf("winnet.NewNetworkListManager: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
defer m.Release()
|
|
|
|
|
|
|
|
cl, err := m.GetNetworkConnections()
|
|
|
|
if err != nil {
|
2020-02-21 20:22:29 +00:00
|
|
|
return false, fmt.Errorf("m.GetNetworkConnections: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
defer cl.Release()
|
|
|
|
|
|
|
|
for _, nco := range cl {
|
|
|
|
aid, err := nco.GetAdapterId()
|
|
|
|
if err != nil {
|
2020-02-21 20:22:29 +00:00
|
|
|
return false, fmt.Errorf("nco.GetAdapterId: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
if aid != ifcGUID.String() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := nco.GetNetwork()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("GetNetwork: %v", err)
|
|
|
|
}
|
|
|
|
defer n.Release()
|
|
|
|
|
|
|
|
cat, err := n.GetCategory()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("GetCategory: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:41:28 +00:00
|
|
|
if cat != categoryPrivate {
|
|
|
|
if err := n.SetCategory(categoryPrivate); err != nil {
|
2020-02-05 22:16:58 +00:00
|
|
|
return false, fmt.Errorf("SetCategory: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
// interfaceFromGUID returns IPAdapterAddresses with specified GUID.
|
|
|
|
func interfaceFromGUID(guid *windows.GUID, 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)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if addr.LUID == luid {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("interfaceFromGUID: interface with LUID %v (from GUID %v) not found", luid, guid)
|
|
|
|
}
|
|
|
|
|
2020-05-12 07:08:52 +00:00
|
|
|
func configureInterface(cfg *Config, tun *tun.NativeTun) error {
|
2020-02-05 22:16:58 +00:00
|
|
|
const mtu = 0
|
|
|
|
guid := tun.GUID()
|
2020-09-26 02:11:05 +00:00
|
|
|
iface, err := interfaceFromGUID(&guid,
|
2020-09-21 21:52:52 +00:00
|
|
|
// Issue 474: on early boot, when the network is still
|
|
|
|
// coming up, if the Tailscale service comes up first,
|
|
|
|
// the Tailscale adapter it finds might not have the
|
|
|
|
// IPv4 service available yet? Try this flag:
|
2020-09-26 02:11:05 +00:00
|
|
|
winipcfg.GAAFlagIncludeAllInterfaces,
|
|
|
|
)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// It takes a weirdly long time for Windows to notice the
|
|
|
|
// new interface has come up. Poll periodically until it
|
|
|
|
// does.
|
2020-09-16 21:41:28 +00:00
|
|
|
const tries = 20
|
|
|
|
for i := 0; i < tries; i++ {
|
|
|
|
found, err := setPrivateNetwork(&guid)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-16 21:41:28 +00:00
|
|
|
log.Printf("setPrivateNetwork(try=%d): %v", i, err)
|
|
|
|
} else {
|
|
|
|
if found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("setPrivateNetwork(try=%d): not found", i)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2020-09-16 21:41:28 +00:00
|
|
|
log.Printf("setPrivateNetwork: adapter %v not found after %d tries, giving up", guid, tries)
|
2020-02-05 22:16:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var firstGateway4 *net.IP
|
|
|
|
var firstGateway6 *net.IP
|
2020-05-12 07:08:52 +00:00
|
|
|
addresses := make([]*net.IPNet, len(cfg.LocalAddrs))
|
|
|
|
for i, addr := range cfg.LocalAddrs {
|
2020-02-05 22:16:58 +00:00
|
|
|
ipnet := addr.IPNet()
|
|
|
|
addresses[i] = ipnet
|
|
|
|
gateway := ipnet.IP
|
|
|
|
if addr.IP.Is4() && firstGateway4 == nil {
|
|
|
|
firstGateway4 = &gateway
|
|
|
|
} else if addr.IP.Is6() && firstGateway6 == nil {
|
|
|
|
firstGateway6 = &gateway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:02:58 +00:00
|
|
|
var routes []winipcfg.RouteData
|
2020-02-05 22:16:58 +00:00
|
|
|
foundDefault4 := false
|
|
|
|
foundDefault6 := false
|
2020-05-12 07:08:52 +00:00
|
|
|
for _, route := range cfg.Routes {
|
2020-05-08 01:07:13 +00:00
|
|
|
if (route.IP.Is4() && firstGateway4 == nil) || (route.IP.Is6() && firstGateway6 == nil) {
|
|
|
|
return errors.New("Due to a Windows limitation, one cannot have interface routes without an interface address")
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-05-08 01:07:13 +00:00
|
|
|
ipn := route.IPNet()
|
|
|
|
var gateway net.IP
|
|
|
|
if route.IP.Is4() {
|
|
|
|
gateway = *firstGateway4
|
|
|
|
} else if route.IP.Is6() {
|
|
|
|
gateway = *firstGateway6
|
|
|
|
}
|
|
|
|
r := winipcfg.RouteData{
|
|
|
|
Destination: net.IPNet{
|
|
|
|
IP: ipn.IP.Mask(ipn.Mask),
|
|
|
|
Mask: ipn.Mask,
|
|
|
|
},
|
|
|
|
NextHop: gateway,
|
|
|
|
Metric: 0,
|
|
|
|
}
|
|
|
|
if bytes.Compare(r.Destination.IP, gateway) == 0 {
|
|
|
|
// no need to add a route for the interface's
|
|
|
|
// own IP. The kernel does that for us.
|
|
|
|
// If we try to replace it, we'll fail to
|
|
|
|
// add the route unless NextHop is set, but
|
|
|
|
// then the interface's IP won't be pingable.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if route.IP.Is4() {
|
2020-05-08 05:17:30 +00:00
|
|
|
if route.Bits == 0 {
|
2020-05-08 01:07:13 +00:00
|
|
|
foundDefault4 = true
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-08 01:07:13 +00:00
|
|
|
r.NextHop = *firstGateway4
|
|
|
|
} else if route.IP.Is6() {
|
2020-05-08 05:17:30 +00:00
|
|
|
if route.Bits == 0 {
|
2020-05-08 01:07:13 +00:00
|
|
|
foundDefault6 = true
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-08 01:07:13 +00:00
|
|
|
r.NextHop = *firstGateway6
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-08 01:07:13 +00:00
|
|
|
routes = append(routes, r)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:13:45 +00:00
|
|
|
err = syncAddresses(iface, addresses)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-01 20:24:58 +00:00
|
|
|
sort.Slice(routes, func(i, j int) bool { return routeLess(&routes[i], &routes[j]) })
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
deduplicatedRoutes := []*winipcfg.RouteData{}
|
|
|
|
for i := 0; i < len(routes); i++ {
|
|
|
|
// There's only one way to get to a given IP+Mask, so delete
|
|
|
|
// all matches after the first.
|
|
|
|
if i > 0 &&
|
|
|
|
bytes.Equal(routes[i].Destination.IP, routes[i-1].Destination.IP) &&
|
|
|
|
bytes.Equal(routes[i].Destination.Mask, routes[i-1].Destination.Mask) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deduplicatedRoutes = append(deduplicatedRoutes, &routes[i])
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
// Re-read interface after syncAddresses.
|
|
|
|
iface, err = interfaceFromGUID(&guid,
|
|
|
|
// Issue 474: on early boot, when the network is still
|
|
|
|
// coming up, if the Tailscale service comes up first,
|
|
|
|
// the Tailscale adapter it finds might not have the
|
|
|
|
// IPv4 service available yet? Try this flag:
|
|
|
|
winipcfg.GAAFlagIncludeAllInterfaces,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
var errAcc error
|
2020-09-22 16:13:45 +00:00
|
|
|
err = syncRoutes(iface, deduplicatedRoutes)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil && errAcc == nil {
|
2020-09-01 20:27:42 +00:00
|
|
|
log.Printf("setroutes: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
errAcc = err
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
ipif, err := iface.LUID.IPInterface(windows.AF_INET)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-01 20:27:42 +00:00
|
|
|
log.Printf("getipif: %v", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if foundDefault4 {
|
|
|
|
ipif.UseAutomaticMetric = false
|
|
|
|
ipif.Metric = 0
|
|
|
|
}
|
|
|
|
if mtu > 0 {
|
2020-09-26 02:11:05 +00:00
|
|
|
ipif.NLMTU = uint32(mtu)
|
|
|
|
tun.ForceMTU(int(ipif.NLMTU))
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
err = ipif.Set()
|
|
|
|
if err != nil && errAcc == nil {
|
|
|
|
errAcc = err
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
ipif, err = iface.LUID.IPInterface(windows.AF_INET6)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-09-26 02:11:05 +00:00
|
|
|
if !errors.Is(err, windows.ERROR_NOT_FOUND) {
|
2020-09-11 18:59:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if foundDefault6 {
|
|
|
|
ipif.UseAutomaticMetric = false
|
|
|
|
ipif.Metric = 0
|
|
|
|
}
|
|
|
|
if mtu > 0 {
|
2020-09-26 02:11:05 +00:00
|
|
|
ipif.NLMTU = uint32(mtu)
|
2020-09-11 18:59:48 +00:00
|
|
|
}
|
|
|
|
ipif.DadTransmits = 0
|
|
|
|
ipif.RouterDiscoveryBehavior = winipcfg.RouterDiscoveryDisabled
|
|
|
|
err = ipif.Set()
|
|
|
|
if err != nil && errAcc == nil {
|
|
|
|
errAcc = err
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errAcc
|
|
|
|
}
|
2020-09-01 20:24:58 +00:00
|
|
|
|
|
|
|
// routeLess reports whether ri should sort before rj.
|
|
|
|
// The actual sort order doesn't appear to matter. The caller just
|
|
|
|
// wants them sorted to be able to de-dup.
|
|
|
|
func routeLess(ri, rj *winipcfg.RouteData) bool {
|
|
|
|
if v := bytes.Compare(ri.Destination.IP, rj.Destination.IP); v != 0 {
|
|
|
|
return v == -1
|
|
|
|
}
|
|
|
|
if v := bytes.Compare(ri.Destination.Mask, rj.Destination.Mask); v != 0 {
|
|
|
|
// Narrower masks first
|
|
|
|
return v == 1
|
|
|
|
}
|
|
|
|
if ri.Metric != rj.Metric {
|
|
|
|
// Lower metrics first
|
|
|
|
return ri.Metric < rj.Metric
|
|
|
|
}
|
|
|
|
if v := bytes.Compare(ri.NextHop, rj.NextHop); v != 0 {
|
|
|
|
// No nexthop before non-empty nexthop.
|
|
|
|
return v == -1
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-09-22 16:13:45 +00:00
|
|
|
|
|
|
|
// unwrapIP returns the shortest version of ip.
|
|
|
|
func unwrapIP(ip net.IP) net.IP {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func v4Mask(m net.IPMask) net.IPMask {
|
|
|
|
if len(m) == 16 {
|
|
|
|
return m[12:]
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func netCompare(a, b net.IPNet) int {
|
|
|
|
aip, bip := unwrapIP(a.IP), unwrapIP(b.IP)
|
|
|
|
v := bytes.Compare(aip, bip)
|
|
|
|
if v != 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
amask, bmask := a.Mask, b.Mask
|
|
|
|
if len(aip) == 4 {
|
|
|
|
amask = v4Mask(a.Mask)
|
|
|
|
bmask = v4Mask(b.Mask)
|
|
|
|
}
|
|
|
|
|
|
|
|
// narrower first
|
|
|
|
return -bytes.Compare(amask, bmask)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortNets(a []*net.IPNet) {
|
|
|
|
sort.Slice(a, func(i, j int) bool {
|
|
|
|
return netCompare(*a[i], *a[j]) == -1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// deltaNets returns the changes to turn a into b.
|
|
|
|
func deltaNets(a, b []*net.IPNet) (add, del []*net.IPNet) {
|
|
|
|
add = make([]*net.IPNet, 0, len(b))
|
|
|
|
del = make([]*net.IPNet, 0, len(a))
|
|
|
|
sortNets(a)
|
|
|
|
sortNets(b)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
j := 0
|
|
|
|
for i < len(a) && j < len(b) {
|
|
|
|
switch netCompare(*a[i], *b[j]) {
|
|
|
|
case -1:
|
|
|
|
// a < b, delete
|
|
|
|
del = append(del, a[i])
|
|
|
|
i++
|
|
|
|
case 0:
|
|
|
|
// a == b, no diff
|
|
|
|
i++
|
|
|
|
j++
|
|
|
|
case 1:
|
|
|
|
// a > b, add missing entry
|
|
|
|
add = append(add, b[j])
|
|
|
|
j++
|
|
|
|
default:
|
|
|
|
panic("unexpected compare result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
del = append(del, a[i:]...)
|
|
|
|
add = append(add, b[j:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func excludeIPv6LinkLocal(in []*net.IPNet) (out []*net.IPNet) {
|
|
|
|
out = in[:0]
|
|
|
|
for _, n := range in {
|
|
|
|
if len(n.IP) == 16 && n.IP.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, n)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
// ipAdapterUnicastAddressToIPNet converts windows.IpAdapterUnicastAddress to net.IPNet.
|
|
|
|
func ipAdapterUnicastAddressToIPNet(u *windows.IpAdapterUnicastAddress) *net.IPNet {
|
|
|
|
ip := u.Address.IP()
|
|
|
|
w := 32
|
|
|
|
if ip.To4() == nil {
|
|
|
|
w = 128
|
|
|
|
}
|
|
|
|
return &net.IPNet{
|
|
|
|
IP: ip,
|
|
|
|
Mask: net.CIDRMask(int(u.OnLinkPrefixLength), w),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unicastIPNets returns all unicast net.IPNet for ifc interface.
|
|
|
|
func unicastIPNets(ifc *winipcfg.IPAdapterAddresses) []*net.IPNet {
|
|
|
|
nets := make([]*net.IPNet, 0)
|
|
|
|
for addr := ifc.FirstUnicastAddress; addr != nil; addr = addr.Next {
|
|
|
|
nets = append(nets, ipAdapterUnicastAddressToIPNet(addr))
|
|
|
|
}
|
|
|
|
return nets
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:13:45 +00:00
|
|
|
// syncAddresses incrementally sets the interface's unicast IP addresses,
|
|
|
|
// doing the minimum number of AddAddresses & DeleteAddress calls.
|
|
|
|
// This avoids the full FlushAddresses.
|
|
|
|
//
|
|
|
|
// Any IPv6 link-local addresses are not deleted.
|
2020-09-26 02:11:05 +00:00
|
|
|
func syncAddresses(ifc *winipcfg.IPAdapterAddresses, want []*net.IPNet) error {
|
2020-09-22 16:13:45 +00:00
|
|
|
var erracc error
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
got := unicastIPNets(ifc)
|
2020-09-22 16:13:45 +00:00
|
|
|
add, del := deltaNets(got, want)
|
|
|
|
del = excludeIPv6LinkLocal(del)
|
|
|
|
for _, a := range del {
|
2020-09-26 02:11:05 +00:00
|
|
|
err := ifc.LUID.DeleteIPAddress(*a)
|
2020-09-22 16:13:45 +00:00
|
|
|
if err != nil {
|
|
|
|
erracc = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
for _, a := range add {
|
|
|
|
err := ifc.LUID.AddIPAddress(*a)
|
|
|
|
if err != nil {
|
|
|
|
erracc = err
|
|
|
|
}
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return erracc
|
|
|
|
}
|
|
|
|
|
|
|
|
func routeDataCompare(a, b *winipcfg.RouteData) int {
|
|
|
|
v := bytes.Compare(a.Destination.IP, b.Destination.IP)
|
|
|
|
if v != 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Narrower masks first
|
|
|
|
v = bytes.Compare(a.Destination.Mask, b.Destination.Mask)
|
|
|
|
if v != 0 {
|
|
|
|
return -v
|
|
|
|
}
|
|
|
|
|
|
|
|
// No nexthop before non-empty nexthop
|
|
|
|
v = bytes.Compare(a.NextHop, b.NextHop)
|
|
|
|
if v != 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lower metrics first
|
|
|
|
if a.Metric < b.Metric {
|
|
|
|
return -1
|
|
|
|
} else if a.Metric > b.Metric {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortRouteData(a []*winipcfg.RouteData) {
|
|
|
|
sort.Slice(a, func(i, j int) bool {
|
|
|
|
return routeDataCompare(a[i], a[j]) < 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func deltaRouteData(a, b []*winipcfg.RouteData) (add, del []*winipcfg.RouteData) {
|
|
|
|
add = make([]*winipcfg.RouteData, 0, len(b))
|
|
|
|
del = make([]*winipcfg.RouteData, 0, len(a))
|
|
|
|
sortRouteData(a)
|
|
|
|
sortRouteData(b)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
j := 0
|
|
|
|
for i < len(a) && j < len(b) {
|
|
|
|
switch routeDataCompare(a[i], b[j]) {
|
|
|
|
case -1:
|
|
|
|
// a < b, delete
|
|
|
|
del = append(del, a[i])
|
|
|
|
i++
|
|
|
|
case 0:
|
|
|
|
// a == b, no diff
|
|
|
|
i++
|
|
|
|
j++
|
|
|
|
case 1:
|
|
|
|
// a > b, add missing entry
|
|
|
|
add = append(add, b[j])
|
|
|
|
j++
|
|
|
|
default:
|
|
|
|
panic("unexpected compare result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
del = append(del, a[i:]...)
|
|
|
|
add = append(add, b[j:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:11:05 +00:00
|
|
|
// getInterfaceRoutes returns all the interface's routes.
|
|
|
|
// Corresponds to GetIpForwardTable2 function, but filtered by interface.
|
2020-10-29 21:38:59 +00:00
|
|
|
func getInterfaceRoutes(ifc *winipcfg.IPAdapterAddresses, family winipcfg.AddressFamily) (matches []*winipcfg.MibIPforwardRow2, err error) {
|
2020-09-26 02:11:05 +00:00
|
|
|
routes, err := winipcfg.GetIPForwardTable2(family)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := range routes {
|
|
|
|
if routes[i].InterfaceLUID == ifc.LUID {
|
2020-10-29 21:38:59 +00:00
|
|
|
matches = append(matches, &routes[i])
|
2020-09-26 02:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-29 21:38:59 +00:00
|
|
|
return
|
2020-09-26 02:11:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:13:45 +00:00
|
|
|
// syncRoutes incrementally sets multiples routes on an interface.
|
|
|
|
// This avoids a full ifc.FlushRoutes call.
|
2020-09-26 02:11:05 +00:00
|
|
|
func syncRoutes(ifc *winipcfg.IPAdapterAddresses, want []*winipcfg.RouteData) error {
|
|
|
|
routes, err := getInterfaceRoutes(ifc, windows.AF_INET)
|
2020-09-22 16:13:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
got := make([]*winipcfg.RouteData, 0, len(routes))
|
|
|
|
for _, r := range routes {
|
2020-09-26 02:11:05 +00:00
|
|
|
got = append(got, &winipcfg.RouteData{
|
|
|
|
Destination: r.DestinationPrefix.IPNet(),
|
|
|
|
NextHop: r.NextHop.IP(),
|
|
|
|
Metric: r.Metric,
|
|
|
|
})
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add, del := deltaRouteData(got, want)
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
for _, a := range del {
|
2020-09-26 02:11:05 +00:00
|
|
|
err := ifc.LUID.DeleteRoute(a.Destination, a.NextHop)
|
2020-09-22 16:13:45 +00:00
|
|
|
if err != nil {
|
2020-09-23 21:01:00 +00:00
|
|
|
dstStr := a.Destination.String()
|
|
|
|
if dstStr == "169.254.255.255/32" {
|
|
|
|
// Issue 785. Ignore these routes
|
|
|
|
// failing to delete. Harmless.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
errs = append(errs, fmt.Errorf("deleting route %v: %w", dstStr, err))
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range add {
|
2020-09-26 02:11:05 +00:00
|
|
|
err := ifc.LUID.AddRoute(a.Destination, a.NextHop, a.Metric)
|
2020-09-22 16:13:45 +00:00
|
|
|
if err != nil {
|
2020-09-23 21:01:00 +00:00
|
|
|
errs = append(errs, fmt.Errorf("adding route %v: %w", &a.Destination, err))
|
2020-09-22 16:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multierror.New(errs)
|
|
|
|
}
|