2018-11-05 16:40:47 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2018-11-05 17:03:58 +00:00
|
|
|
"fmt"
|
2018-11-05 16:40:47 +00:00
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
core *Core
|
|
|
|
enabled bool
|
|
|
|
ipv4routes []cryptokey_route
|
|
|
|
ipv6routes []cryptokey_route
|
2018-11-05 17:31:10 +00:00
|
|
|
ipv4cache map[address]cryptokey_route
|
|
|
|
ipv6cache map[address]cryptokey_route
|
2018-11-05 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cryptokey_route struct {
|
|
|
|
subnet net.IPNet
|
|
|
|
destination []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptokey) init(core *Core) {
|
|
|
|
c.core = core
|
|
|
|
c.ipv4routes = make([]cryptokey_route, 0)
|
|
|
|
c.ipv6routes = make([]cryptokey_route, 0)
|
2018-11-05 17:31:10 +00:00
|
|
|
c.ipv4cache = make(map[address]cryptokey_route, 0)
|
|
|
|
c.ipv6cache = make(map[address]cryptokey_route, 0)
|
2018-11-05 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptokey) isEnabled() bool {
|
|
|
|
return c.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cryptokey) addRoute(cidr string, dest string) error {
|
|
|
|
// Is the CIDR we've been given valid?
|
2018-11-05 17:03:58 +00:00
|
|
|
ipaddr, ipnet, err := net.ParseCIDR(cidr)
|
2018-11-05 16:40:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the prefix length and size
|
2018-11-05 17:03:58 +00:00
|
|
|
_, prefixsize := ipnet.Mask.Size()
|
2018-11-05 16:40:47 +00:00
|
|
|
|
|
|
|
// Check if the prefix is IPv4 or IPv6
|
|
|
|
if prefixsize == net.IPv6len*8 {
|
2018-11-05 17:03:58 +00:00
|
|
|
// Is the route an Yggdrasil destination?
|
|
|
|
var addr address
|
|
|
|
var snet subnet
|
|
|
|
copy(addr[:], ipaddr)
|
|
|
|
copy(snet[:], ipnet.IP)
|
|
|
|
if addr.isValid() || snet.isValid() {
|
|
|
|
return errors.New("Can't specify Yggdrasil destination as crypto-key route")
|
|
|
|
}
|
|
|
|
// Do we already have a route for this subnet?
|
2018-11-05 16:40:47 +00:00
|
|
|
for _, route := range c.ipv6routes {
|
2018-11-05 17:03:58 +00:00
|
|
|
if route.subnet.String() == ipnet.String() {
|
|
|
|
return errors.New(fmt.Sprintf("Route already exists for %s", cidr))
|
2018-11-05 16:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Decode the public key
|
|
|
|
if boxPubKey, err := hex.DecodeString(dest); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
// Add the new crypto-key route
|
|
|
|
c.ipv6routes = append(c.ipv6routes, cryptokey_route{
|
|
|
|
subnet: *ipnet,
|
|
|
|
destination: boxPubKey,
|
|
|
|
})
|
2018-11-05 17:31:10 +00:00
|
|
|
|
2018-11-05 16:40:47 +00:00
|
|
|
// Sort so most specific routes are first
|
|
|
|
sort.Slice(c.ipv6routes, func(i, j int) bool {
|
|
|
|
im, _ := c.ipv6routes[i].subnet.Mask.Size()
|
|
|
|
jm, _ := c.ipv6routes[j].subnet.Mask.Size()
|
|
|
|
return im > jm
|
|
|
|
})
|
2018-11-05 17:31:10 +00:00
|
|
|
|
|
|
|
// Clear the cache as this route might change future routing
|
|
|
|
// Setting an empty slice keeps the memory whereas nil invokes GC
|
|
|
|
for k := range c.ipv6cache {
|
|
|
|
delete(c.ipv6cache, k)
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:40:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else if prefixsize == net.IPv4len*8 {
|
|
|
|
// IPv4
|
|
|
|
return errors.New("IPv4 not supported at this time")
|
|
|
|
}
|
|
|
|
return errors.New("Unspecified error")
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:31:10 +00:00
|
|
|
func (c *cryptokey) getPublicKeyForAddress(addr address) (boxPubKey, error) {
|
2018-11-05 23:12:26 +00:00
|
|
|
// Check if the address is a valid Yggdrasil address - if so it
|
|
|
|
// is exempt from all CKR checking
|
|
|
|
if addr.isValid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:31:10 +00:00
|
|
|
// Check if there's a cache entry for this addr
|
|
|
|
if route, ok := c.ipv6cache[addr]; ok {
|
|
|
|
var box boxPubKey
|
|
|
|
copy(box[:boxPubKeyLen], route.destination)
|
|
|
|
return box, nil
|
|
|
|
}
|
2018-11-05 16:40:47 +00:00
|
|
|
|
2018-11-05 17:31:10 +00:00
|
|
|
// No cache was found - start by converting the address into a net.IP
|
|
|
|
ip := make(net.IP, 16)
|
|
|
|
copy(ip[:16], addr[:])
|
|
|
|
|
|
|
|
// Check whether it's an IPv4 or an IPv6 address
|
|
|
|
if ip.To4() == nil {
|
|
|
|
// Check if we have a route. At this point c.ipv6routes should be
|
|
|
|
// pre-sorted so that the most specific routes are first
|
2018-11-05 16:40:47 +00:00
|
|
|
for _, route := range c.ipv6routes {
|
2018-11-05 17:31:10 +00:00
|
|
|
// Does this subnet match the given IP?
|
|
|
|
if route.subnet.Contains(ip) {
|
|
|
|
// Cache the entry for future packets to get a faster lookup
|
|
|
|
c.ipv6cache[addr] = route
|
|
|
|
|
|
|
|
// Return the boxPubKey
|
2018-11-05 16:40:47 +00:00
|
|
|
var box boxPubKey
|
|
|
|
copy(box[:boxPubKeyLen], route.destination)
|
|
|
|
return box, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-05 17:31:10 +00:00
|
|
|
// IPv4 isn't supported yet
|
2018-11-05 16:40:47 +00:00
|
|
|
return boxPubKey{}, errors.New("IPv4 not supported at this time")
|
|
|
|
}
|
2018-11-05 17:31:10 +00:00
|
|
|
|
|
|
|
// No route was found if we got to this point
|
|
|
|
return boxPubKey{}, errors.New(fmt.Sprintf("No route to %s", ip.String()))
|
2018-11-05 16:40:47 +00:00
|
|
|
}
|