Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable both exit routes at the same time #1159

Merged
merged 2 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,8 @@ func (h *Headscale) IsRoutesEnabled(machine *Machine, routeStr string) bool {
return false
}

// EnableRoutes enables new routes based on a list of new routes.
func (h *Headscale) EnableRoutes(machine *Machine, routeStrs ...string) error {
// enableRoutes enables new routes based on a list of new routes.
func (h *Headscale) enableRoutes(machine *Machine, routeStrs ...string) error {
newRoutes := make([]netip.Prefix, len(routeStrs))
for index, routeStr := range routeStrs {
route, err := netip.ParsePrefix(routeStr)
Expand Down
9 changes: 8 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ func (h *Headscale) EnableRoute(id uint64) error {
return err
}

return h.EnableRoutes(&route.Machine, netip.Prefix(route.Prefix).String())
// Tailscale requires both IPv4 and IPv6 exit routes to
// be enabled at the same time, as per
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
if route.isExitRoute() {
return h.enableRoutes(&route.Machine, ExitRouteV4.String(), ExitRouteV6.String())
}

return h.enableRoutes(&route.Machine, netip.Prefix(route.Prefix).String())
}

func (h *Headscale) DisableRoute(id uint64) error {
Expand Down
42 changes: 24 additions & 18 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (s *Suite) TestGetRoutes(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(len(advertisedRoutes), check.Equals, 1)

err = app.EnableRoutes(&machine, "192.168.0.0/24")
err = app.enableRoutes(&machine, "192.168.0.0/24")
c.Assert(err, check.NotNil)

err = app.EnableRoutes(&machine, "10.0.0.0/24")
err = app.enableRoutes(&machine, "10.0.0.0/24")
c.Assert(err, check.IsNil)
}

Expand Down Expand Up @@ -102,25 +102,25 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(len(noEnabledRoutes), check.Equals, 0)

err = app.EnableRoutes(&machine, "192.168.0.0/24")
err = app.enableRoutes(&machine, "192.168.0.0/24")
c.Assert(err, check.NotNil)

err = app.EnableRoutes(&machine, "10.0.0.0/24")
err = app.enableRoutes(&machine, "10.0.0.0/24")
c.Assert(err, check.IsNil)

enabledRoutes, err := app.GetEnabledRoutes(&machine)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes), check.Equals, 1)

// Adding it twice will just let it pass through
err = app.EnableRoutes(&machine, "10.0.0.0/24")
err = app.enableRoutes(&machine, "10.0.0.0/24")
c.Assert(err, check.IsNil)

enableRoutesAfterDoubleApply, err := app.GetEnabledRoutes(&machine)
c.Assert(err, check.IsNil)
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)

err = app.EnableRoutes(&machine, "150.0.10.0/25")
err = app.enableRoutes(&machine, "150.0.10.0/25")
c.Assert(err, check.IsNil)

enabledRoutesWithAdditionalRoute, err := app.GetEnabledRoutes(&machine)
Expand Down Expand Up @@ -167,10 +167,10 @@ func (s *Suite) TestIsUniquePrefix(c *check.C) {
err = app.processMachineRoutes(&machine1)
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, route.String())
err = app.enableRoutes(&machine1, route.String())
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, route2.String())
err = app.enableRoutes(&machine1, route2.String())
c.Assert(err, check.IsNil)

hostInfo2 := tailcfg.Hostinfo{
Expand All @@ -192,7 +192,7 @@ func (s *Suite) TestIsUniquePrefix(c *check.C) {
err = app.processMachineRoutes(&machine2)
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine2, route2.String())
err = app.enableRoutes(&machine2, route2.String())
c.Assert(err, check.IsNil)

enabledRoutes1, err := app.GetEnabledRoutes(&machine1)
Expand Down Expand Up @@ -254,10 +254,10 @@ func (s *Suite) TestSubnetFailover(c *check.C) {
err = app.processMachineRoutes(&machine1)
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, prefix.String())
err = app.enableRoutes(&machine1, prefix.String())
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, prefix2.String())
err = app.enableRoutes(&machine1, prefix2.String())
c.Assert(err, check.IsNil)

err = app.handlePrimarySubnetFailover()
Expand Down Expand Up @@ -291,7 +291,7 @@ func (s *Suite) TestSubnetFailover(c *check.C) {
err = app.processMachineRoutes(&machine2)
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine2, prefix2.String())
err = app.enableRoutes(&machine2, prefix2.String())
c.Assert(err, check.IsNil)

err = app.handlePrimarySubnetFailover()
Expand Down Expand Up @@ -339,7 +339,7 @@ func (s *Suite) TestSubnetFailover(c *check.C) {
err = app.processMachineRoutes(&machine2)
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine2, prefix.String())
err = app.enableRoutes(&machine2, prefix.String())
c.Assert(err, check.IsNil)

err = app.handlePrimarySubnetFailover()
Expand Down Expand Up @@ -413,18 +413,24 @@ func (s *Suite) TestAllowedIPRoutes(c *check.C) {
err = app.processMachineRoutes(&machine1)
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, prefix.String())
err = app.enableRoutes(&machine1, prefix.String())
c.Assert(err, check.IsNil)

// We do not enable this one on purpose to test that it is not enabled
// err = app.EnableRoutes(&machine1, prefix2.String())
// err = app.enableRoutes(&machine1, prefix2.String())
// c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, prefixExitNodeV4.String())
routes, err := app.GetMachineRoutes(&machine1)
c.Assert(err, check.IsNil)
for _, route := range routes {
if route.isExitRoute() {
err = app.EnableRoute(uint64(route.ID))
c.Assert(err, check.IsNil)

err = app.EnableRoutes(&machine1, prefixExitNodeV6.String())
c.Assert(err, check.IsNil)
// We only enable one exit route, so we can test that both are enabled
break
}
}

err = app.handlePrimarySubnetFailover()
c.Assert(err, check.IsNil)
Expand Down