aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/service/ifaceconfig.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-05-16 16:47:28 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-16 17:14:52 +0200
commitd260908423e877c04bf386de6a6f644dcf8ebd70 (patch)
treed1c41aa36d5f6305b03d8274d85d6ab454c682bf /service/ifaceconfig.go
parentservice: print useragent in log (diff)
downloadwireguard-windows-d260908423e877c04bf386de6a6f644dcf8ebd70.tar.xz
wireguard-windows-d260908423e877c04bf386de6a6f644dcf8ebd70.zip
service: silently ignore routes that won't have a gateway
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'service/ifaceconfig.go')
-rw-r--r--service/ifaceconfig.go31
1 files changed, 13 insertions, 18 deletions
diff --git a/service/ifaceconfig.go b/service/ifaceconfig.go
index 6655f263..115e454e 100644
--- a/service/ifaceconfig.go
+++ b/service/ifaceconfig.go
@@ -7,7 +7,6 @@ package service
import (
"bytes"
- "errors"
"log"
"net"
"sort"
@@ -202,12 +201,11 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error {
return err
}
- routeCount := len(conf.Interface.Addresses)
+ estimatedRouteCount := len(conf.Interface.Addresses)
for _, peer := range conf.Peers {
- routeCount += len(peer.AllowedIPs)
+ estimatedRouteCount += len(peer.AllowedIPs)
}
- routes := make([]winipcfg.RouteData, routeCount)
- routeCount = 0
+ routes := make([]winipcfg.RouteData, 0, estimatedRouteCount)
var firstGateway4 *net.IP
var firstGateway6 *net.IP
addresses := make([]*net.IPNet, len(conf.Interface.Addresses))
@@ -220,15 +218,14 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error {
} else if addr.Bits() == 128 && firstGateway6 == nil {
firstGateway6 = &gateway
}
- routes[routeCount] = winipcfg.RouteData{
+ routes = append(routes, winipcfg.RouteData{
Destination: net.IPNet{
IP: gateway,
Mask: ipnet.Mask,
},
NextHop: gateway,
Metric: 0,
- }
- routeCount++
+ })
}
foundDefault4 := false
@@ -236,9 +233,9 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error {
for _, peer := range conf.Peers {
for _, allowedip := range peer.AllowedIPs {
if (allowedip.Bits() == 32 && firstGateway4 == nil) || (allowedip.Bits() == 128 && firstGateway6 == nil) {
- return errors.New("Due to a Windows limitation, one cannot have interface routes without an interface address")
+ continue
}
- routes[routeCount] = winipcfg.RouteData{
+ route := winipcfg.RouteData{
Destination: allowedip.IPNet(),
Metric: 0,
}
@@ -246,14 +243,14 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error {
if allowedip.Cidr == 0 {
foundDefault4 = true
}
- routes[routeCount].NextHop = *firstGateway4
+ route.NextHop = *firstGateway4
} else if allowedip.Bits() == 128 {
if allowedip.Cidr == 0 {
foundDefault6 = true
}
- routes[routeCount].NextHop = *firstGateway6
+ route.NextHop = *firstGateway6
}
- routeCount++
+ routes = append(routes, route)
}
}
@@ -266,8 +263,7 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error {
return err
}
- deduplicatedRoutes := make([]*winipcfg.RouteData, routeCount)
- routeCount = 0
+ deduplicatedRoutes := make([]*winipcfg.RouteData, 0, len(routes))
sort.Slice(routes, func(i, j int) bool {
return routes[i].Metric < routes[j].Metric ||
bytes.Compare(routes[i].NextHop, routes[j].NextHop) == -1 ||
@@ -281,11 +277,10 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error {
bytes.Equal(routes[i].Destination.Mask, routes[i-1].Destination.Mask) {
continue
}
- deduplicatedRoutes[routeCount] = &routes[i]
- routeCount++
+ deduplicatedRoutes = append(deduplicatedRoutes, &routes[i])
}
- err = iface.SetRoutes(deduplicatedRoutes[:routeCount])
+ err = iface.SetRoutes(deduplicatedRoutes)
if err != nil {
return nil
}