2021-11-11 15:37:29 -07:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
2023-11-21 09:50:18 -07:00
|
|
|
type DeviceFactory func(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error)
|
|
|
|
|
|
|
|
func NewDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
|
2021-11-12 10:19:28 -07:00
|
|
|
routes, err := parseRoutes(c, tunCidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.NewContextualError("Could not parse tun.routes", nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafeRoutes, err := parseUnsafeRoutes(c, tunCidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
|
|
|
|
}
|
|
|
|
routes = append(routes, unsafeRoutes...)
|
|
|
|
|
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:
|
|
|
|
return newTun(
|
2021-11-11 15:37:29 -07:00
|
|
|
l,
|
2023-11-21 09:50:18 -07:00
|
|
|
c.GetString("tun.dev", ""),
|
2021-11-11 15:37:29 -07:00
|
|
|
tunCidr,
|
|
|
|
c.GetInt("tun.mtu", DefaultMTU),
|
|
|
|
routes,
|
|
|
|
c.GetInt("tun.tx_queue", 500),
|
2023-11-21 09:50:18 -07:00
|
|
|
routines > 1,
|
2023-05-09 09:36:55 -06:00
|
|
|
c.GetBool("tun.use_system_route_table", false),
|
2021-11-11 15:37:29 -07:00
|
|
|
)
|
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 {
|
|
|
|
return func(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
|
|
|
|
routes, err := parseRoutes(c, tunCidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.NewContextualError("Could not parse tun.routes", nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafeRoutes, err := parseUnsafeRoutes(c, tunCidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
|
|
|
|
}
|
|
|
|
routes = append(routes, unsafeRoutes...)
|
|
|
|
return newTunFromFd(
|
2021-11-11 15:37:29 -07:00
|
|
|
l,
|
2023-11-21 09:50:18 -07:00
|
|
|
*fd,
|
2021-11-11 15:37:29 -07:00
|
|
|
tunCidr,
|
|
|
|
c.GetInt("tun.mtu", DefaultMTU),
|
|
|
|
routes,
|
|
|
|
c.GetInt("tun.tx_queue", 500),
|
2023-05-09 09:36:55 -06:00
|
|
|
c.GetBool("tun.use_system_route_table", false),
|
2021-11-11 15:37:29 -07:00
|
|
|
)
|
2023-11-21 09:50:18 -07:00
|
|
|
|
2021-11-11 15:37:29 -07:00
|
|
|
}
|
|
|
|
}
|