2019-05-17 22:09:20 +01:00
|
|
|
package tuntap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sort"
|
2019-05-20 21:45:33 +01:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2019-05-17 22:09:20 +01:00
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This module implements crypto-key routing, similar to Wireguard, where we
|
|
|
|
// allow traffic for non-Yggdrasil ranges to be routed over Yggdrasil.
|
|
|
|
|
|
|
|
type cryptokey struct {
|
2019-05-20 21:45:33 +01:00
|
|
|
tun *TunAdapter
|
|
|
|
enabled atomic.Value // bool
|
2019-08-19 10:28:30 +01:00
|
|
|
ipv4remotes []cryptokey_route
|
|
|
|
ipv6remotes []cryptokey_route
|
2019-05-20 21:45:33 +01:00
|
|
|
ipv4cache map[address.Address]cryptokey_route
|
|
|
|
ipv6cache map[address.Address]cryptokey_route
|
2019-08-19 10:28:30 +01:00
|
|
|
ipv4locals []net.IPNet
|
|
|
|
ipv6locals []net.IPNet
|
2019-08-20 09:38:46 +01:00
|
|
|
mutexremotes sync.RWMutex
|
2019-05-20 21:45:33 +01:00
|
|
|
mutexcaches sync.RWMutex
|
2019-08-20 09:38:46 +01:00
|
|
|
mutexlocals sync.RWMutex
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type cryptokey_route struct {
|
|
|
|
subnet net.IPNet
|
|
|
|
destination crypto.BoxPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise crypto-key routing. This must be done before any other CKR calls.
|
|
|
|
func (c *cryptokey) init(tun *TunAdapter) {
|
|
|
|
c.tun = tun
|
2019-08-28 19:31:04 +01:00
|
|
|
c.configure()
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:31:04 +01:00
|
|
|
// Configure the CKR routes. This should only ever be ran by the TUN/TAP actor.
|
|
|
|
func (c *cryptokey) configure() {
|
2019-07-27 15:00:09 +01:00
|
|
|
current := c.tun.config.GetCurrent()
|
2019-05-17 22:09:20 +01:00
|
|
|
|
|
|
|
// Set enabled/disabled state
|
2019-07-06 20:08:32 +01:00
|
|
|
c.setEnabled(current.TunnelRouting.Enable)
|
2019-05-17 22:09:20 +01:00
|
|
|
|
|
|
|
// Clear out existing routes
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexremotes.Lock()
|
2019-08-19 10:28:30 +01:00
|
|
|
c.ipv6remotes = make([]cryptokey_route, 0)
|
|
|
|
c.ipv4remotes = make([]cryptokey_route, 0)
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexremotes.Unlock()
|
2019-05-17 22:09:20 +01:00
|
|
|
|
|
|
|
// Add IPv6 routes
|
2019-08-19 10:28:30 +01:00
|
|
|
for ipv6, pubkey := range current.TunnelRouting.IPv6RemoteSubnets {
|
|
|
|
if err := c.addRemoteSubnet(ipv6, pubkey); err != nil {
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Errorln("Error adding CKR IPv6 remote subnet:", err)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add IPv4 routes
|
2019-08-19 10:28:30 +01:00
|
|
|
for ipv4, pubkey := range current.TunnelRouting.IPv4RemoteSubnets {
|
|
|
|
if err := c.addRemoteSubnet(ipv4, pubkey); err != nil {
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Errorln("Error adding CKR IPv4 remote subnet:", err)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out existing sources
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexlocals.Lock()
|
2019-08-19 10:28:30 +01:00
|
|
|
c.ipv6locals = make([]net.IPNet, 0)
|
|
|
|
c.ipv4locals = make([]net.IPNet, 0)
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexlocals.Unlock()
|
2019-05-17 22:09:20 +01:00
|
|
|
|
|
|
|
// Add IPv6 sources
|
2019-08-19 10:28:30 +01:00
|
|
|
c.ipv6locals = make([]net.IPNet, 0)
|
|
|
|
for _, source := range current.TunnelRouting.IPv6LocalSubnets {
|
|
|
|
if err := c.addLocalSubnet(source); err != nil {
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Errorln("Error adding CKR IPv6 local subnet:", err)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add IPv4 sources
|
2019-08-19 10:28:30 +01:00
|
|
|
c.ipv4locals = make([]net.IPNet, 0)
|
|
|
|
for _, source := range current.TunnelRouting.IPv4LocalSubnets {
|
|
|
|
if err := c.addLocalSubnet(source); err != nil {
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Errorln("Error adding CKR IPv4 local subnet:", err)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe the caches
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.Lock()
|
2019-05-17 22:09:20 +01:00
|
|
|
c.ipv4cache = make(map[address.Address]cryptokey_route, 0)
|
|
|
|
c.ipv6cache = make(map[address.Address]cryptokey_route, 0)
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.Unlock()
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable or disable crypto-key routing.
|
|
|
|
func (c *cryptokey) setEnabled(enabled bool) {
|
2019-05-30 17:33:59 +01:00
|
|
|
c.enabled.Store(enabled)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if crypto-key routing is enabled.
|
|
|
|
func (c *cryptokey) isEnabled() bool {
|
2019-05-30 17:33:59 +01:00
|
|
|
enabled, ok := c.enabled.Load().(bool)
|
|
|
|
return ok && enabled
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the given address (with the address length specified in bytes)
|
|
|
|
// matches either the current node's address, the node's routed subnet or the
|
2019-08-19 10:28:30 +01:00
|
|
|
// list of subnets specified in ipv4locals/ipv6locals.
|
|
|
|
func (c *cryptokey) isValidLocalAddress(addr address.Address, addrlen int) bool {
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexlocals.RLock()
|
|
|
|
defer c.mutexlocals.RUnlock()
|
2019-05-17 22:09:20 +01:00
|
|
|
// Does it match a configured CKR source?
|
|
|
|
if c.isEnabled() {
|
2019-08-20 18:10:08 -05:00
|
|
|
ip := net.IP(addr[:addrlen])
|
2019-05-17 22:09:20 +01:00
|
|
|
// Build our references to the routing sources
|
|
|
|
var routingsources *[]net.IPNet
|
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if addrlen == net.IPv6len {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingsources = &c.ipv6locals
|
2019-05-17 22:09:20 +01:00
|
|
|
} else if addrlen == net.IPv4len {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingsources = &c.ipv4locals
|
2019-05-17 22:09:20 +01:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subnet := range *routingsources {
|
|
|
|
if subnet.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't match any of the above
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a source subnet, which allows traffic with these source addresses to
|
|
|
|
// be tunnelled using crypto-key routing.
|
2019-08-19 10:28:30 +01:00
|
|
|
func (c *cryptokey) addLocalSubnet(cidr string) error {
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexlocals.Lock()
|
|
|
|
defer c.mutexlocals.Unlock()
|
2019-05-20 21:45:33 +01:00
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// Is the CIDR we've been given valid?
|
|
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the prefix length and size
|
|
|
|
_, prefixsize := ipnet.Mask.Size()
|
|
|
|
|
|
|
|
// Build our references to the routing sources
|
|
|
|
var routingsources *[]net.IPNet
|
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if prefixsize == net.IPv6len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingsources = &c.ipv6locals
|
2019-05-17 22:09:20 +01:00
|
|
|
} else if prefixsize == net.IPv4len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingsources = &c.ipv4locals
|
2019-05-17 22:09:20 +01:00
|
|
|
} else {
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("unexpected prefix size")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we already have this CIDR
|
|
|
|
for _, subnet := range *routingsources {
|
|
|
|
if subnet.String() == ipnet.String() {
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("local subnet already configured")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the source subnet
|
|
|
|
*routingsources = append(*routingsources, *ipnet)
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Infoln("Added CKR local subnet", cidr)
|
2019-05-17 22:09:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a destination route for the given CIDR to be tunnelled to the node
|
|
|
|
// with the given BoxPubKey.
|
2019-08-19 10:28:30 +01:00
|
|
|
func (c *cryptokey) addRemoteSubnet(cidr string, dest string) error {
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexremotes.Lock()
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.Lock()
|
2019-08-20 09:38:46 +01:00
|
|
|
defer c.mutexremotes.Unlock()
|
2019-05-20 21:45:33 +01:00
|
|
|
defer c.mutexcaches.Unlock()
|
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// Is the CIDR we've been given valid?
|
|
|
|
ipaddr, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the prefix length and size
|
|
|
|
_, prefixsize := ipnet.Mask.Size()
|
|
|
|
|
|
|
|
// Build our references to the routing table and cache
|
|
|
|
var routingtable *[]cryptokey_route
|
|
|
|
var routingcache *map[address.Address]cryptokey_route
|
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if prefixsize == net.IPv6len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingtable = &c.ipv6remotes
|
2019-05-17 22:09:20 +01:00
|
|
|
routingcache = &c.ipv6cache
|
|
|
|
} else if prefixsize == net.IPv4len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingtable = &c.ipv4remotes
|
2019-05-17 22:09:20 +01:00
|
|
|
routingcache = &c.ipv4cache
|
|
|
|
} else {
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("unexpected prefix size")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is the route an Yggdrasil destination?
|
|
|
|
var addr address.Address
|
|
|
|
var snet address.Subnet
|
|
|
|
copy(addr[:], ipaddr)
|
|
|
|
copy(snet[:], ipnet.IP)
|
|
|
|
if addr.IsValid() || snet.IsValid() {
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("can't specify Yggdrasil destination as crypto-key route")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
// Do we already have a route for this subnet?
|
|
|
|
for _, route := range *routingtable {
|
|
|
|
if route.subnet.String() == ipnet.String() {
|
2019-08-28 19:31:04 +01:00
|
|
|
return fmt.Errorf("remote subnet already exists for %s", cidr)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Decode the public key
|
|
|
|
if bpk, err := hex.DecodeString(dest); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(bpk) != crypto.BoxPubKeyLen {
|
2019-08-28 19:31:04 +01:00
|
|
|
return fmt.Errorf("incorrect key length for %s", dest)
|
2019-05-17 22:09:20 +01:00
|
|
|
} else {
|
|
|
|
// Add the new crypto-key route
|
|
|
|
var key crypto.BoxPubKey
|
|
|
|
copy(key[:], bpk)
|
|
|
|
*routingtable = append(*routingtable, cryptokey_route{
|
|
|
|
subnet: *ipnet,
|
|
|
|
destination: key,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Sort so most specific routes are first
|
|
|
|
sort.Slice(*routingtable, func(i, j int) bool {
|
|
|
|
im, _ := (*routingtable)[i].subnet.Mask.Size()
|
|
|
|
jm, _ := (*routingtable)[j].subnet.Mask.Size()
|
|
|
|
return im > jm
|
|
|
|
})
|
|
|
|
|
|
|
|
// Clear the cache as this route might change future routing
|
|
|
|
// Setting an empty slice keeps the memory whereas nil invokes GC
|
|
|
|
for k := range *routingcache {
|
|
|
|
delete(*routingcache, k)
|
|
|
|
}
|
|
|
|
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Infoln("Added CKR remote subnet", cidr)
|
2019-05-17 22:09:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks up the most specific route for the given address (with the address
|
|
|
|
// length specified in bytes) from the crypto-key routing table. An error is
|
|
|
|
// returned if the address is not suitable or no route was found.
|
|
|
|
func (c *cryptokey) getPublicKeyForAddress(addr address.Address, addrlen int) (crypto.BoxPubKey, error) {
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.RLock()
|
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// Check if the address is a valid Yggdrasil address - if so it
|
|
|
|
// is exempt from all CKR checking
|
|
|
|
if addr.IsValid() {
|
2019-08-28 19:31:04 +01:00
|
|
|
return crypto.BoxPubKey{}, errors.New("cannot look up CKR for Yggdrasil addresses")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build our references to the routing table and cache
|
|
|
|
var routingtable *[]cryptokey_route
|
|
|
|
var routingcache *map[address.Address]cryptokey_route
|
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if addrlen == net.IPv6len {
|
|
|
|
routingcache = &c.ipv6cache
|
|
|
|
} else if addrlen == net.IPv4len {
|
|
|
|
routingcache = &c.ipv4cache
|
|
|
|
} else {
|
2019-08-28 19:31:04 +01:00
|
|
|
return crypto.BoxPubKey{}, errors.New("unexpected prefix size")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there's a cache entry for this addr
|
|
|
|
if route, ok := (*routingcache)[addr]; ok {
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.RUnlock()
|
2019-05-17 22:09:20 +01:00
|
|
|
return route.destination, nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.RUnlock()
|
|
|
|
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexremotes.RLock()
|
|
|
|
defer c.mutexremotes.RUnlock()
|
2019-05-20 21:45:33 +01:00
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if addrlen == net.IPv6len {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingtable = &c.ipv6remotes
|
2019-05-20 21:45:33 +01:00
|
|
|
} else if addrlen == net.IPv4len {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingtable = &c.ipv4remotes
|
2019-05-20 21:45:33 +01:00
|
|
|
} else {
|
2019-08-28 19:31:04 +01:00
|
|
|
return crypto.BoxPubKey{}, errors.New("unexpected prefix size")
|
2019-05-20 21:45:33 +01:00
|
|
|
}
|
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// No cache was found - start by converting the address into a net.IP
|
|
|
|
ip := make(net.IP, addrlen)
|
|
|
|
copy(ip[:addrlen], addr[:])
|
|
|
|
|
2019-08-19 10:28:30 +01:00
|
|
|
// Check if we have a route. At this point c.ipv6remotes should be
|
2019-05-17 22:09:20 +01:00
|
|
|
// pre-sorted so that the most specific routes are first
|
|
|
|
for _, route := range *routingtable {
|
|
|
|
// Does this subnet match the given IP?
|
|
|
|
if route.subnet.Contains(ip) {
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.Lock()
|
|
|
|
defer c.mutexcaches.Unlock()
|
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// Check if the routing cache is above a certain size, if it is evict
|
|
|
|
// a random entry so we can make room for this one. We take advantage
|
|
|
|
// of the fact that the iteration order is random here
|
|
|
|
for k := range *routingcache {
|
|
|
|
if len(*routingcache) < 1024 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
delete(*routingcache, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the entry for future packets to get a faster lookup
|
|
|
|
(*routingcache)[addr] = route
|
|
|
|
|
|
|
|
// Return the boxPubKey
|
|
|
|
return route.destination, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No route was found if we got to this point
|
2019-08-20 09:38:46 +01:00
|
|
|
return crypto.BoxPubKey{}, fmt.Errorf("no route to %s", ip.String())
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a source subnet, which allows traffic with these source addresses to
|
|
|
|
// be tunnelled using crypto-key routing.
|
2019-08-19 10:28:30 +01:00
|
|
|
func (c *cryptokey) removeLocalSubnet(cidr string) error {
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexlocals.Lock()
|
|
|
|
defer c.mutexlocals.Unlock()
|
2019-05-20 21:45:33 +01:00
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// Is the CIDR we've been given valid?
|
|
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the prefix length and size
|
|
|
|
_, prefixsize := ipnet.Mask.Size()
|
|
|
|
|
|
|
|
// Build our references to the routing sources
|
|
|
|
var routingsources *[]net.IPNet
|
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if prefixsize == net.IPv6len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingsources = &c.ipv6locals
|
2019-05-17 22:09:20 +01:00
|
|
|
} else if prefixsize == net.IPv4len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingsources = &c.ipv4locals
|
2019-05-17 22:09:20 +01:00
|
|
|
} else {
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("unexpected prefix size")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we already have this CIDR
|
|
|
|
for idx, subnet := range *routingsources {
|
|
|
|
if subnet.String() == ipnet.String() {
|
|
|
|
*routingsources = append((*routingsources)[:idx], (*routingsources)[idx+1:]...)
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Infoln("Removed CKR local subnet", cidr)
|
2019-05-17 22:09:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("local subnet not found")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a destination route for the given CIDR to be tunnelled to the node
|
|
|
|
// with the given BoxPubKey.
|
2019-08-19 10:28:30 +01:00
|
|
|
func (c *cryptokey) removeRemoteSubnet(cidr string, dest string) error {
|
2019-08-20 09:38:46 +01:00
|
|
|
c.mutexremotes.Lock()
|
2019-05-20 21:45:33 +01:00
|
|
|
c.mutexcaches.Lock()
|
2019-08-20 09:38:46 +01:00
|
|
|
defer c.mutexremotes.Unlock()
|
2019-05-20 21:45:33 +01:00
|
|
|
defer c.mutexcaches.Unlock()
|
|
|
|
|
2019-05-17 22:09:20 +01:00
|
|
|
// Is the CIDR we've been given valid?
|
|
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the prefix length and size
|
|
|
|
_, prefixsize := ipnet.Mask.Size()
|
|
|
|
|
|
|
|
// Build our references to the routing table and cache
|
|
|
|
var routingtable *[]cryptokey_route
|
|
|
|
var routingcache *map[address.Address]cryptokey_route
|
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if prefixsize == net.IPv6len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingtable = &c.ipv6remotes
|
2019-05-17 22:09:20 +01:00
|
|
|
routingcache = &c.ipv6cache
|
|
|
|
} else if prefixsize == net.IPv4len*8 {
|
2019-08-19 10:28:30 +01:00
|
|
|
routingtable = &c.ipv4remotes
|
2019-05-17 22:09:20 +01:00
|
|
|
routingcache = &c.ipv4cache
|
|
|
|
} else {
|
2019-08-28 19:31:04 +01:00
|
|
|
return errors.New("unexpected prefix size")
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the public key
|
|
|
|
bpk, err := hex.DecodeString(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(bpk) != crypto.BoxPubKeyLen {
|
2019-08-28 19:31:04 +01:00
|
|
|
return fmt.Errorf("incorrect key length for %s", dest)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|
|
|
|
netStr := ipnet.String()
|
|
|
|
|
|
|
|
for idx, route := range *routingtable {
|
|
|
|
if bytes.Equal(route.destination[:], bpk) && route.subnet.String() == netStr {
|
|
|
|
*routingtable = append((*routingtable)[:idx], (*routingtable)[idx+1:]...)
|
|
|
|
for k := range *routingcache {
|
|
|
|
delete(*routingcache, k)
|
|
|
|
}
|
2019-08-28 19:31:04 +01:00
|
|
|
c.tun.log.Infof("Removed CKR remote subnet %s via %s\n", cidr, dest)
|
2019-05-17 22:09:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 19:31:04 +01:00
|
|
|
return fmt.Errorf("route does not exists for %s", cidr)
|
2019-05-17 22:09:20 +01:00
|
|
|
}
|