diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ec6c931d3..363fe3e481c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,11 @@ - Code reorganisation, a lot of code has moved, please review the following PRs accordingly [#1444](https://github.com/juanfont/headscale/pull/1444) +### 0.22.3-rr (2023-XX-XX) + ### Changes - Set max open and idle connections for postgres +- Allows conflicting subnet ranges across users ## 0.22.3 (2023-05-12) diff --git a/hscontrol/routes.go b/hscontrol/routes.go index 89f9a6941b8..84bd6a18dae 100644 --- a/hscontrol/routes.go +++ b/hscontrol/routes.go @@ -192,15 +192,31 @@ func (h *Headscale) DeleteMachineRoutes(m *Machine) error { // isUniquePrefix returns if there is another machine providing the same route already. func (h *Headscale) isUniquePrefix(route Route) bool { - var count int64 - h.db. - Model(&Route{}). + var routes []Route + err := h.db. + Preload("Machine"). Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?", route.Prefix, route.MachineID, - true, true).Count(&count) + true, true). + Find(&routes).Error + if err != nil { + return true + } + + if len(routes) == 0 { + return true + } + + for _, r := range routes { + // Return false, if there are more than one uniquePrefix for the same + // user. Else, true. This allows having the same prefix for two users. + if route.Machine.UserID == r.Machine.UserID && route.Machine.isOnline() { + return false + } + } - return count == 0 + return true } func (h *Headscale) getPrimaryRoute(prefix netip.Prefix) (*Route, error) {