nebula/overlay/route.go

237 lines
5.0 KiB
Go
Raw Normal View History

2021-11-11 15:37:29 -07:00
package overlay
2019-11-19 10:00:20 -07:00
import (
"fmt"
"math"
2019-11-19 10:00:20 -07:00
"net"
"runtime"
2019-11-19 10:00:20 -07:00
"strconv"
"github.com/slackhq/nebula/cidr"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/iputil"
2019-11-19 10:00:20 -07:00
)
2021-11-11 15:37:29 -07:00
type Route struct {
MTU int
Metric int
Cidr *net.IPNet
Via *iputil.VpnIp
2019-11-19 10:00:20 -07:00
}
func makeRouteTree(routes []Route, allowMTU bool) (*cidr.Tree4, error) {
routeTree := cidr.NewTree4()
for _, r := range routes {
if !allowMTU && r.MTU > 0 {
return nil, fmt.Errorf("route MTU is not supported in %s", runtime.GOOS)
}
if r.Via != nil {
routeTree.AddCIDR(r.Cidr, *r.Via)
}
}
return routeTree, nil
}
func parseRoutes(c *config.C, network *net.IPNet) ([]Route, error) {
2019-11-19 10:00:20 -07:00
var err error
r := c.Get("tun.routes")
2019-11-19 10:00:20 -07:00
if r == nil {
2021-11-11 15:37:29 -07:00
return []Route{}, nil
2019-11-19 10:00:20 -07:00
}
rawRoutes, ok := r.([]interface{})
if !ok {
return nil, fmt.Errorf("tun.routes is not an array")
}
if len(rawRoutes) < 1 {
2021-11-11 15:37:29 -07:00
return []Route{}, nil
2019-11-19 10:00:20 -07:00
}
2021-11-11 15:37:29 -07:00
routes := make([]Route, len(rawRoutes))
2019-11-19 10:00:20 -07:00
for i, r := range rawRoutes {
m, ok := r.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("entry %v in tun.routes is invalid", i+1)
}
rMtu, ok := m["mtu"]
if !ok {
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not present", i+1)
}
mtu, ok := rMtu.(int)
if !ok {
mtu, err = strconv.Atoi(rMtu.(string))
if err != nil {
return nil, fmt.Errorf("entry %v.mtu in tun.routes is not an integer: %v", i+1, err)
}
}
if mtu < 500 {
return nil, fmt.Errorf("entry %v.mtu in tun.routes is below 500: %v", i+1, mtu)
}
rRoute, ok := m["route"]
if !ok {
return nil, fmt.Errorf("entry %v.route in tun.routes is not present", i+1)
}
2021-11-11 15:37:29 -07:00
r := Route{
MTU: mtu,
2019-11-19 10:00:20 -07:00
}
2021-11-11 15:37:29 -07:00
_, r.Cidr, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
2019-11-19 10:00:20 -07:00
if err != nil {
return nil, fmt.Errorf("entry %v.route in tun.routes failed to parse: %v", i+1, err)
}
2021-11-11 15:37:29 -07:00
if !ipWithin(network, r.Cidr) {
2019-11-19 10:00:20 -07:00
return nil, fmt.Errorf(
"entry %v.route in tun.routes is not contained within the network attached to the certificate; route: %v, network: %v",
i+1,
2021-11-11 15:37:29 -07:00
r.Cidr.String(),
2019-11-19 10:00:20 -07:00
network.String(),
)
}
routes[i] = r
}
return routes, nil
}
func parseUnsafeRoutes(c *config.C, network *net.IPNet) ([]Route, error) {
2019-12-12 09:34:17 -07:00
var err error
r := c.Get("tun.unsafe_routes")
2019-12-12 09:34:17 -07:00
if r == nil {
2021-11-11 15:37:29 -07:00
return []Route{}, nil
2019-12-12 09:34:17 -07:00
}
rawRoutes, ok := r.([]interface{})
if !ok {
return nil, fmt.Errorf("tun.unsafe_routes is not an array")
}
if len(rawRoutes) < 1 {
2021-11-11 15:37:29 -07:00
return []Route{}, nil
2019-12-12 09:34:17 -07:00
}
2021-11-11 15:37:29 -07:00
routes := make([]Route, len(rawRoutes))
2019-12-12 09:34:17 -07:00
for i, r := range rawRoutes {
m, ok := r.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("entry %v in tun.unsafe_routes is invalid", i+1)
}
2019-12-12 11:01:46 -07:00
rMtu, ok := m["mtu"]
if !ok {
2021-11-11 15:37:29 -07:00
rMtu = c.GetInt("tun.mtu", DefaultMTU)
2019-12-12 11:01:46 -07:00
}
mtu, ok := rMtu.(int)
if !ok {
mtu, err = strconv.Atoi(rMtu.(string))
if err != nil {
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is not an integer: %v", i+1, err)
}
}
if mtu < 500 {
return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is below 500: %v", i+1, mtu)
}
rMetric, ok := m["metric"]
if !ok {
rMetric = 0
}
metric, ok := rMetric.(int)
if !ok {
_, err = strconv.ParseInt(rMetric.(string), 10, 32)
if err != nil {
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not an integer: %v", i+1, err)
}
}
if metric < 0 || metric > math.MaxInt32 {
return nil, fmt.Errorf("entry %v.metric in tun.unsafe_routes is not in range (0-%d) : %v", i+1, math.MaxInt32, metric)
}
2019-12-12 09:34:17 -07:00
rVia, ok := m["via"]
if !ok {
return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not present", i+1)
}
via, ok := rVia.(string)
if !ok {
2020-04-06 12:33:30 -06:00
return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not a string: found %T", i+1, rVia)
2019-12-12 09:34:17 -07:00
}
nVia := net.ParseIP(via)
if nVia == nil {
return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes failed to parse address: %v", i+1, via)
}
rRoute, ok := m["route"]
if !ok {
return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes is not present", i+1)
}
viaVpnIp := iputil.Ip2VpnIp(nVia)
2021-11-11 15:37:29 -07:00
r := Route{
Via: &viaVpnIp,
2021-11-11 15:37:29 -07:00
MTU: mtu,
Metric: metric,
2019-12-12 09:34:17 -07:00
}
2021-11-11 15:37:29 -07:00
_, r.Cidr, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
2019-12-12 09:34:17 -07:00
if err != nil {
return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes failed to parse: %v", i+1, err)
}
2021-11-11 15:37:29 -07:00
if ipWithin(network, r.Cidr) {
2019-12-12 09:34:17 -07:00
return nil, fmt.Errorf(
"entry %v.route in tun.unsafe_routes is contained within the network attached to the certificate; route: %v, network: %v",
i+1,
2021-11-11 15:37:29 -07:00
r.Cidr.String(),
2019-12-12 09:34:17 -07:00
network.String(),
)
}
routes[i] = r
}
return routes, nil
}
2019-11-19 10:00:20 -07:00
func ipWithin(o *net.IPNet, i *net.IPNet) bool {
// Make sure o contains the lowest form of i
if !o.Contains(i.IP.Mask(i.Mask)) {
return false
}
// Find the max ip in i
ip4 := i.IP.To4()
if ip4 == nil {
return false
}
last := make(net.IP, len(ip4))
copy(last, ip4)
for x := range ip4 {
last[x] |= ^i.Mask[x]
}
// Make sure o contains the max
if !o.Contains(last) {
return false
}
return true
}