1
0
forked from extern/smegmesh

main - fix default routing being deleted

Default route keeps fluctuating on configuration
update.
This commit is contained in:
Tim Beatham 2023-12-10 23:35:00 +00:00
parent a5074a536e
commit 1789d203f6
5 changed files with 27 additions and 33 deletions

View File

@ -37,7 +37,7 @@ type WgConfiguration struct {
// service for IPDiscoverability
IPDiscovery *IPDiscovery `yaml:"ipDiscovery" validate:"required,eq=public|eq=dns"`
// AdvertiseRoutes: specifies whether the node can act as a router routing packets between meshes
AdvertiseRoutes *bool `yaml:"advertiseRoutes"`
AdvertiseRoutes *bool `yaml:"advertiseRoute"`
// AdvertiseDefaultRoute: specifies whether or not this route should advertise a default route
// for all nodes to route their packets to
AdvertiseDefaultRoute *bool `yaml:"advertiseDefaults"`

View File

@ -225,8 +225,11 @@ type Route struct {
}
func (r1 Route) equal(r2 Route) bool {
mask1Ones, _ := r1.Destination.Mask.Size()
mask2Ones, _ := r2.Destination.Mask.Size()
return r1.Gateway.String() == r2.Gateway.String() &&
r1.Destination.String() == r2.Destination.String()
(mask1Ones == 0 && mask2Ones == 0 || r1.Destination.IP.Equal(r2.Destination.IP))
}
// DeleteRoutes deletes all routes not in exclude
@ -257,18 +260,11 @@ func (c *RtNetlinkConfig) DeleteRoutes(ifName string, family uint8, exclude ...R
shouldExclude := func(r Route) bool {
for _, route := range exclude {
if route.equal(r) {
return false
}
if family == unix.AF_INET && route.Destination.IP.To4() == nil {
return false
}
if family == unix.AF_INET6 && route.Destination.IP.To16() == nil {
if r.equal(route) {
return false
}
}
return true
}

View File

@ -134,10 +134,7 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
for _, route := range node.GetRoutes() {
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
v6Default, _, _ := net.ParseCIDR("::/0")
v4Default, _, _ := net.ParseCIDR("0.0.0.0/0")
if (prefix.IP.Equal(v6Default) || prefix.IP.Equal(v4Default)) && *meshProvider.GetConfiguration().AdvertiseDefaultRoute {
if prefix.IP.Equal(net.IPv6zero) && *meshProvider.GetConfiguration().AdvertiseDefaultRoute {
return true
}

View File

@ -510,7 +510,7 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager {
m.RouteManager = params.RouteManager
if m.RouteManager == nil {
m.RouteManager = NewRouteManager(m, &params.Conf)
m.RouteManager = NewRouteManager(m)
}
if params.CommandRunner == nil {

View File

@ -3,7 +3,6 @@ package mesh
import (
"net"
"github.com/tim-beatham/wgmesh/pkg/conf"
"github.com/tim-beatham/wgmesh/pkg/ip"
"github.com/tim-beatham/wgmesh/pkg/lib"
)
@ -14,7 +13,6 @@ type RouteManager interface {
type RouteManagerImpl struct {
meshManager MeshManager
conf *conf.DaemonConfiguration
}
func (r *RouteManagerImpl) UpdateRoutes() error {
@ -32,23 +30,25 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
routes[mesh1.GetMeshId()] = make([]Route, 0)
}
if *mesh1.GetConfiguration().AdvertiseDefaultRoute {
_, ipv6Default, _ := net.ParseCIDR("::/0")
defaultRoute := &RouteStub{
Destination: ipv6Default,
HopCount: 0,
Path: []string{mesh1.GetMeshId()},
}
mesh1.AddRoutes(NodeID(self), defaultRoute)
routes[mesh1.GetMeshId()] = append(routes[mesh1.GetMeshId()], defaultRoute)
}
routeMap, err := mesh1.GetRoutes(NodeID(self))
if err != nil {
return err
}
if *r.conf.BaseConfiguration.AdvertiseDefaultRoute {
_, ipv6Default, _ := net.ParseCIDR("::/0")
mesh1.AddRoutes(NodeID(self),
&RouteStub{
Destination: ipv6Default,
HopCount: 0,
Path: make([]string, 0),
})
}
for _, mesh2 := range meshes {
routeValues, ok := routes[mesh2.GetMeshId()]
@ -75,8 +75,9 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
return s == mesh2.GetMeshId()
}
// Ensure that the route does not see it's own IP
return !r.GetDestination().IP.Equal(mesh2IpNet.IP) && !lib.Contains(r.GetPath()[1:], pathNotMesh)
// Remove any potential routing loops
return !r.GetDestination().IP.Equal(mesh2IpNet.IP) &&
!lib.Contains(r.GetPath()[1:], pathNotMesh)
})
routes[mesh2.GetMeshId()] = routeValues
@ -106,6 +107,6 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
return nil
}
func NewRouteManager(m MeshManager, conf *conf.DaemonConfiguration) RouteManager {
return &RouteManagerImpl{meshManager: m, conf: conf}
func NewRouteManager(m MeshManager) RouteManager {
return &RouteManagerImpl{meshManager: m}
}