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

Disable and Delete route must affect both exit routes (IPv4 and IPv6) #1428

Merged
merged 2 commits into from
May 7, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Changes

- Fix issue where systemd could not bind to port 80 [#1365](https://github.com/juanfont/headscale/pull/1365)
- Disable (or delete) both exit routes at the same time [#1428](https://github.com/juanfont/headscale/pull/1428)

## 0.22.0 (2023-04-20)

Expand Down
54 changes: 50 additions & 4 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,36 @@ func (h *Headscale) DisableRoute(id uint64) error {
return err
}

route.Enabled = false
route.IsPrimary = false
err = h.db.Save(route).Error
// 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() {
route.Enabled = false
route.IsPrimary = false
err = h.db.Save(route).Error
if err != nil {
return err
}

return h.handlePrimarySubnetFailover()
}

routes, err := h.GetMachineRoutes(&route.Machine)
if err != nil {
return err
}

for i := range routes {
if routes[i].isExitRoute() {
routes[i].Enabled = false
routes[i].IsPrimary = false
err = h.db.Save(&routes[i]).Error
if err != nil {
return err
}
}
}

return h.handlePrimarySubnetFailover()
}

Expand All @@ -122,7 +145,30 @@ func (h *Headscale) DeleteRoute(id uint64) error {
return err
}

if err := h.db.Unscoped().Delete(&route).Error; err != nil {
// 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() {
if err := h.db.Unscoped().Delete(&route).Error; err != nil {
return err
}

return h.handlePrimarySubnetFailover()
}

routes, err := h.GetMachineRoutes(&route.Machine)
if err != nil {
return err
}

routesToDelete := []Route{}
for _, r := range routes {
if r.isExitRoute() {
routesToDelete = append(routesToDelete, r)
}
}

if err := h.db.Unscoped().Delete(&routesToDelete).Error; err != nil {
return err
}

Expand Down
31 changes: 31 additions & 0 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,37 @@ func (s *Suite) TestAllowedIPRoutes(c *check.C) {

c.Assert(foundExitNodeV4, check.Equals, true)
c.Assert(foundExitNodeV6, check.Equals, true)

// Now we disable only one of the exit routes
// and we see if both are disabled
var exitRouteV4 Route
for _, route := range routes {
if route.isExitRoute() && netip.Prefix(route.Prefix) == prefixExitNodeV4 {
exitRouteV4 = route

break
}
}

err = app.DisableRoute(uint64(exitRouteV4.ID))
c.Assert(err, check.IsNil)

enabledRoutes1, err = app.GetEnabledRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 1)

// and now we delete only one of the exit routes
// and we check if both are deleted
routes, err = app.GetMachineRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 4)

err = app.DeleteRoute(uint64(exitRouteV4.ID))
c.Assert(err, check.IsNil)

routes, err = app.GetMachineRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 2)
}

func (s *Suite) TestDeleteRoutes(c *check.C) {
Expand Down