mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-02-09 13:59:33 +01:00
main - fix default routing being deleted
Default route keeps fluctuating on configuration update.
This commit is contained in:
parent
a5074a536e
commit
1789d203f6
@ -37,7 +37,7 @@ type WgConfiguration struct {
|
|||||||
// service for IPDiscoverability
|
// service for IPDiscoverability
|
||||||
IPDiscovery *IPDiscovery `yaml:"ipDiscovery" validate:"required,eq=public|eq=dns"`
|
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: 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
|
// AdvertiseDefaultRoute: specifies whether or not this route should advertise a default route
|
||||||
// for all nodes to route their packets to
|
// for all nodes to route their packets to
|
||||||
AdvertiseDefaultRoute *bool `yaml:"advertiseDefaults"`
|
AdvertiseDefaultRoute *bool `yaml:"advertiseDefaults"`
|
||||||
|
@ -225,8 +225,11 @@ type Route struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r1 Route) equal(r2 Route) bool {
|
func (r1 Route) equal(r2 Route) bool {
|
||||||
|
mask1Ones, _ := r1.Destination.Mask.Size()
|
||||||
|
mask2Ones, _ := r2.Destination.Mask.Size()
|
||||||
|
|
||||||
return r1.Gateway.String() == r2.Gateway.String() &&
|
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
|
// 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 {
|
shouldExclude := func(r Route) bool {
|
||||||
for _, route := range exclude {
|
for _, route := range exclude {
|
||||||
if route.equal(r) {
|
if r.equal(route) {
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if family == unix.AF_INET && route.Destination.IP.To4() == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if family == unix.AF_INET6 && route.Destination.IP.To16() == nil {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,10 +134,7 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
|||||||
|
|
||||||
for _, route := range node.GetRoutes() {
|
for _, route := range node.GetRoutes() {
|
||||||
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
||||||
v6Default, _, _ := net.ParseCIDR("::/0")
|
if prefix.IP.Equal(net.IPv6zero) && *meshProvider.GetConfiguration().AdvertiseDefaultRoute {
|
||||||
v4Default, _, _ := net.ParseCIDR("0.0.0.0/0")
|
|
||||||
|
|
||||||
if (prefix.IP.Equal(v6Default) || prefix.IP.Equal(v4Default)) && *meshProvider.GetConfiguration().AdvertiseDefaultRoute {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -510,7 +510,7 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager {
|
|||||||
m.RouteManager = params.RouteManager
|
m.RouteManager = params.RouteManager
|
||||||
|
|
||||||
if m.RouteManager == nil {
|
if m.RouteManager == nil {
|
||||||
m.RouteManager = NewRouteManager(m, ¶ms.Conf)
|
m.RouteManager = NewRouteManager(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.CommandRunner == nil {
|
if params.CommandRunner == nil {
|
||||||
|
@ -3,7 +3,6 @@ package mesh
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
|
||||||
"github.com/tim-beatham/wgmesh/pkg/ip"
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||||
)
|
)
|
||||||
@ -14,7 +13,6 @@ type RouteManager interface {
|
|||||||
|
|
||||||
type RouteManagerImpl struct {
|
type RouteManagerImpl struct {
|
||||||
meshManager MeshManager
|
meshManager MeshManager
|
||||||
conf *conf.DaemonConfiguration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RouteManagerImpl) UpdateRoutes() error {
|
func (r *RouteManagerImpl) UpdateRoutes() error {
|
||||||
@ -32,23 +30,25 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
|||||||
routes[mesh1.GetMeshId()] = make([]Route, 0)
|
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))
|
routeMap, err := mesh1.GetRoutes(NodeID(self))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
for _, mesh2 := range meshes {
|
||||||
routeValues, ok := routes[mesh2.GetMeshId()]
|
routeValues, ok := routes[mesh2.GetMeshId()]
|
||||||
|
|
||||||
@ -75,8 +75,9 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
|||||||
return s == mesh2.GetMeshId()
|
return s == mesh2.GetMeshId()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that the route does not see it's own IP
|
// Remove any potential routing loops
|
||||||
return !r.GetDestination().IP.Equal(mesh2IpNet.IP) && !lib.Contains(r.GetPath()[1:], pathNotMesh)
|
return !r.GetDestination().IP.Equal(mesh2IpNet.IP) &&
|
||||||
|
!lib.Contains(r.GetPath()[1:], pathNotMesh)
|
||||||
})
|
})
|
||||||
|
|
||||||
routes[mesh2.GetMeshId()] = routeValues
|
routes[mesh2.GetMeshId()] = routeValues
|
||||||
@ -106,6 +107,6 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouteManager(m MeshManager, conf *conf.DaemonConfiguration) RouteManager {
|
func NewRouteManager(m MeshManager) RouteManager {
|
||||||
return &RouteManagerImpl{meshManager: m, conf: conf}
|
return &RouteManagerImpl{meshManager: m}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user