aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-12-16 14:42:03 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-12-16 14:42:56 +0100
commit936a9c1f736ba20dbeb7f4dec37d0983da38b92a (patch)
treee78662ca134c392aaf939ec4450cbaccf360002f
parentversion: bump (diff)
downloadwireguard-windows-936a9c1f736ba20dbeb7f4dec37d0983da38b92a.tar.xz
wireguard-windows-936a9c1f736ba20dbeb7f4dec37d0983da38b92a.zip
tunnel: use real on-link routes
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--tunnel/addressconfig.go31
1 files changed, 10 insertions, 21 deletions
diff --git a/tunnel/addressconfig.go b/tunnel/addressconfig.go
index 643e178e..777c96cd 100644
--- a/tunnel/addressconfig.go
+++ b/tunnel/addressconfig.go
@@ -60,38 +60,27 @@ func cleanupAddressesOnDisconnectedInterfaces(family winipcfg.AddressFamily, add
func configureInterface(family winipcfg.AddressFamily, conf *conf.Config, tun *tun.NativeTun) error {
luid := winipcfg.LUID(tun.LUID())
- estimatedRouteCount := len(conf.Interface.Addresses)
+ estimatedRouteCount := 0
for _, peer := range conf.Peers {
estimatedRouteCount += len(peer.AllowedIPs)
}
routes := make([]winipcfg.RouteData, 0, estimatedRouteCount)
- var firstGateway4 *net.IP
- var firstGateway6 *net.IP
addresses := make([]net.IPNet, len(conf.Interface.Addresses))
+ var haveV4Address, haveV6Address bool
for i, addr := range conf.Interface.Addresses {
- ipnet := addr.IPNet()
- addresses[i] = ipnet
- gateway := ipnet.IP.Mask(ipnet.Mask)
- if addr.Bits() == 32 && firstGateway4 == nil {
- firstGateway4 = &gateway
- } else if addr.Bits() == 128 && firstGateway6 == nil {
- firstGateway6 = &gateway
+ addresses[i] = addr.IPNet()
+ if addr.Bits() == 32 {
+ haveV4Address = true
+ } else if addr.Bits() == 128 {
+ haveV6Address = true
}
- routes = append(routes, winipcfg.RouteData{
- Destination: net.IPNet{
- IP: gateway,
- Mask: ipnet.Mask,
- },
- NextHop: gateway,
- Metric: 0,
- })
}
foundDefault4 := false
foundDefault6 := false
for _, peer := range conf.Peers {
for _, allowedip := range peer.AllowedIPs {
- if (allowedip.Bits() == 32 && firstGateway4 == nil) || (allowedip.Bits() == 128 && firstGateway6 == nil) {
+ if (allowedip.Bits() == 32 && !haveV4Address) || (allowedip.Bits() == 128 && !haveV6Address) {
continue
}
route := winipcfg.RouteData{
@@ -102,12 +91,12 @@ func configureInterface(family winipcfg.AddressFamily, conf *conf.Config, tun *t
if allowedip.Cidr == 0 {
foundDefault4 = true
}
- route.NextHop = *firstGateway4
+ route.NextHop = net.IPv4zero
} else if allowedip.Bits() == 128 {
if allowedip.Cidr == 0 {
foundDefault6 = true
}
- route.NextHop = *firstGateway6
+ route.NextHop = net.IPv6zero
}
routes = append(routes, route)
}