2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
2024-07-31 09:18:56 -06:00
|
|
|
"net/netip"
|
2021-11-11 15:37:29 -07:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/slackhq/nebula/config"
|
2021-11-12 10:19:28 -07:00
|
|
|
"github.com/slackhq/nebula/util"
|
2021-11-11 15:37:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultMTU = 1300
|
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
// TODO: We may be able to remove routines
|
2024-07-31 09:18:56 -06:00
|
|
|
type DeviceFactory func(c *config.C, l *logrus.Logger, tunCidr netip.Prefix, routines int) (Device, error)
|
2023-11-21 09:50:18 -07:00
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func NewDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr netip.Prefix, routines int) (Device, error) {
|
2021-11-11 15:37:29 -07:00
|
|
|
switch {
|
|
|
|
case c.GetBool("tun.disabled", false):
|
|
|
|
tun := newDisabledTun(tunCidr, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l)
|
|
|
|
return tun, nil
|
|
|
|
|
2023-11-21 09:50:18 -07:00
|
|
|
default:
|
2024-03-28 14:17:28 -06:00
|
|
|
return newTun(c, l, tunCidr, routines > 1)
|
2023-11-21 09:50:18 -07:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 15:37:29 -07:00
|
|
|
|
2023-11-21 09:50:18 -07:00
|
|
|
func NewFdDeviceFromConfig(fd *int) DeviceFactory {
|
2024-07-31 09:18:56 -06:00
|
|
|
return func(c *config.C, l *logrus.Logger, tunCidr netip.Prefix, routines int) (Device, error) {
|
2024-03-28 14:17:28 -06:00
|
|
|
return newTunFromFd(c, l, *fd, tunCidr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func getAllRoutesFromConfig(c *config.C, cidr netip.Prefix, initial bool) (bool, []Route, error) {
|
2024-03-28 14:17:28 -06:00
|
|
|
if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
2023-11-21 09:50:18 -07:00
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
routes, err := parseRoutes(c, cidr)
|
|
|
|
if err != nil {
|
|
|
|
return true, nil, util.NewContextualError("Could not parse tun.routes", nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafeRoutes, err := parseUnsafeRoutes(c, cidr)
|
|
|
|
if err != nil {
|
|
|
|
return true, nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
routes = append(routes, unsafeRoutes...)
|
|
|
|
return true, routes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// findRemovedRoutes will return all routes that are not present in the newRoutes list and would affect the system route table.
|
|
|
|
// Via is not used to evaluate since it does not affect the system route table.
|
|
|
|
func findRemovedRoutes(newRoutes, oldRoutes []Route) []Route {
|
|
|
|
var removed []Route
|
|
|
|
has := func(entry Route) bool {
|
|
|
|
for _, check := range newRoutes {
|
|
|
|
if check.Equal(entry) {
|
|
|
|
return true
|
|
|
|
}
|
2023-11-21 09:50:18 -07:00
|
|
|
}
|
2024-03-28 14:17:28 -06:00
|
|
|
return false
|
|
|
|
}
|
2023-11-21 09:50:18 -07:00
|
|
|
|
2024-03-28 14:17:28 -06:00
|
|
|
for _, oldEntry := range oldRoutes {
|
|
|
|
if !has(oldEntry) {
|
|
|
|
removed = append(removed, oldEntry)
|
|
|
|
}
|
2021-11-11 15:37:29 -07:00
|
|
|
}
|
2024-03-28 14:17:28 -06:00
|
|
|
|
|
|
|
return removed
|
2021-11-11 15:37:29 -07:00
|
|
|
}
|