mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-24 01:14:25 +01:00
subnet support
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
type route struct {
|
||||
mtu int
|
||||
route *net.IPNet
|
||||
via *net.IP
|
||||
}
|
||||
|
||||
func parseRoutes(config *Config, network *net.IPNet) ([]route, error) {
|
||||
@@ -81,6 +82,74 @@ func parseRoutes(config *Config, network *net.IPNet) ([]route, error) {
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
func parseUnsafeRoutes(config *Config, network *net.IPNet) ([]route, error) {
|
||||
var err error
|
||||
|
||||
r := config.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)
|
||||
}
|
||||
|
||||
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: %v", i+1, err)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
r := route{
|
||||
via: &nVia,
|
||||
}
|
||||
|
||||
_, r.route, 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.route) {
|
||||
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.route.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)) {
|
||||
|
||||
Reference in New Issue
Block a user