| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 | package overlayimport (	"fmt"	"math"	"net"	"runtime"	"strconv"	"github.com/sirupsen/logrus"	"github.com/slackhq/nebula/cidr"	"github.com/slackhq/nebula/config"	"github.com/slackhq/nebula/iputil")type Route struct {	MTU     int	Metric  int	Cidr    *net.IPNet	Via     *iputil.VpnIp	Install bool}func makeRouteTree(l *logrus.Logger, routes []Route, allowMTU bool) (*cidr.Tree4, error) {	routeTree := cidr.NewTree4()	for _, r := range routes {		if !allowMTU && r.MTU > 0 {			l.WithField("route", r).Warnf("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) {	var err error	r := c.Get("tun.routes")	if r == nil {		return []Route{}, nil	}	rawRoutes, ok := r.([]interface{})	if !ok {		return nil, fmt.Errorf("tun.routes is not an array")	}	if len(rawRoutes) < 1 {		return []Route{}, nil	}	routes := make([]Route, len(rawRoutes))	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)		}		r := Route{			Install: true,			MTU:     mtu,		}		_, r.Cidr, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))		if err != nil {			return nil, fmt.Errorf("entry %v.route in tun.routes failed to parse: %v", i+1, err)		}		if !ipWithin(network, r.Cidr) {			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,				r.Cidr.String(),				network.String(),			)		}		routes[i] = r	}	return routes, nil}func parseUnsafeRoutes(c *config.C, network *net.IPNet) ([]Route, error) {	var err error	r := c.Get("tun.unsafe_routes")	if r == nil {		return []Route{}, nil	}	rawRoutes, ok := r.([]interface{})	if !ok {		return nil, fmt.Errorf("tun.unsafe_routes is not an array")	}	if len(rawRoutes) < 1 {		return []Route{}, nil	}	routes := make([]Route, len(rawRoutes))	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)		}		var mtu int		if rMtu, ok := m["mtu"]; ok {			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 != 0 && 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)		}		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 {			return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not a string: found %T", i+1, rVia)		}		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)		install := true		rInstall, ok := m["install"]		if ok {			install, err = strconv.ParseBool(fmt.Sprintf("%v", rInstall))			if err != nil {				return nil, fmt.Errorf("entry %v.install in tun.unsafe_routes is not a boolean: %v", i+1, err)			}		}		r := Route{			Via:     &viaVpnIp,			MTU:     mtu,			Metric:  metric,			Install: install,		}		_, r.Cidr, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))		if err != nil {			return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes failed to parse: %v", i+1, err)		}		if ipWithin(network, r.Cidr) {			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,				r.Cidr.String(),				network.String(),			)		}		routes[i] = r	}	return routes, nil}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}
 |