2020-05-07 10:25:58 +02:00
|
|
|
package wg
|
2019-03-25 01:02:10 +01:00
|
|
|
|
|
|
|
import (
|
2019-03-28 20:27:08 +01:00
|
|
|
"hash/fnv"
|
2019-03-25 01:02:10 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
"github.com/costela/wesher/common"
|
2019-07-12 11:08:14 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
// State holds the configured state of a Wesher Wireguard interface
|
|
|
|
type State struct {
|
2019-03-25 01:02:10 +01:00
|
|
|
iface string
|
2019-07-12 11:08:14 +02:00
|
|
|
client *wgctrl.Client
|
|
|
|
OverlayAddr net.IPNet
|
2019-03-25 01:02:10 +01:00
|
|
|
Port int
|
2019-07-12 11:08:14 +02:00
|
|
|
PrivKey wgtypes.Key
|
|
|
|
PubKey wgtypes.Key
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
// New creates a new Wesher Wireguard state
|
2020-05-07 14:33:29 +02:00
|
|
|
// The Wireguard keys are generated for every new interface
|
|
|
|
// The interface must later be setup using SetUpInterface
|
2020-05-12 19:10:25 +02:00
|
|
|
func New(iface string, port int, ipnet *net.IPNet, name string) (*State, *common.Node, error) {
|
2019-07-12 11:08:14 +02:00
|
|
|
client, err := wgctrl.New()
|
|
|
|
if err != nil {
|
2020-05-12 19:10:25 +02:00
|
|
|
return nil, nil, errors.Wrap(err, "could not instantiate wireguard client")
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 11:08:14 +02:00
|
|
|
privKey, err := wgtypes.GeneratePrivateKey()
|
2019-03-25 01:02:10 +01:00
|
|
|
if err != nil {
|
2020-05-12 19:10:25 +02:00
|
|
|
return nil, nil, err
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
pubKey := privKey.PublicKey()
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
state := State{
|
2019-03-25 01:02:10 +01:00
|
|
|
iface: iface,
|
2019-07-12 11:08:14 +02:00
|
|
|
client: client,
|
2019-03-25 01:02:10 +01:00
|
|
|
Port: port,
|
|
|
|
PrivKey: privKey,
|
|
|
|
PubKey: pubKey,
|
|
|
|
}
|
2020-05-10 18:36:21 +02:00
|
|
|
state.assignOverlayAddr(ipnet, name)
|
|
|
|
|
2020-05-12 19:10:25 +02:00
|
|
|
node := &common.Node{}
|
|
|
|
node.OverlayAddr = state.OverlayAddr
|
|
|
|
node.PubKey = state.PubKey.String()
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-12 19:10:25 +02:00
|
|
|
return &state, node, nil
|
2020-05-10 18:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// assignOverlayAddr assigns a new address to the interface
|
2020-05-07 14:33:29 +02:00
|
|
|
// The address is assigned inside the provided network and depends on the
|
|
|
|
// provided name deterministically
|
|
|
|
// Currently, the address is assigned by hashing the name and mapping that
|
|
|
|
// hash in the target network space
|
2020-05-10 18:36:21 +02:00
|
|
|
func (s *State) assignOverlayAddr(ipnet *net.IPNet, name string) {
|
2019-03-25 01:02:10 +01:00
|
|
|
// TODO: this is way too brittle and opaque
|
|
|
|
bits, size := ipnet.Mask.Size()
|
2019-03-29 22:15:17 +01:00
|
|
|
ip := make([]byte, len(ipnet.IP))
|
2019-03-28 20:27:08 +01:00
|
|
|
copy(ip, []byte(ipnet.IP))
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2019-03-28 20:27:08 +01:00
|
|
|
h := fnv.New128a()
|
2019-03-25 01:02:10 +01:00
|
|
|
h.Write([]byte(name))
|
|
|
|
hb := h.Sum(nil)
|
|
|
|
|
2019-03-28 20:27:08 +01:00
|
|
|
for i := 1; i <= (size-bits)/8; i++ {
|
|
|
|
ip[len(ip)-i] = hb[len(hb)-i]
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
2019-03-28 20:27:08 +01:00
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
s.OverlayAddr = net.IPNet{
|
2019-07-12 11:08:14 +02:00
|
|
|
IP: net.IP(ip),
|
|
|
|
Mask: net.CIDRMask(size, size), // either /32 or /128, depending if ipv4 or ipv6
|
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:33:29 +02:00
|
|
|
// DownInterface shuts down the associated network interface
|
2020-05-07 14:40:22 +02:00
|
|
|
func (s *State) DownInterface() error {
|
|
|
|
if _, err := s.client.Device(s.iface); err != nil {
|
2019-07-12 11:08:14 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil // device already gone; noop
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2020-05-07 14:40:22 +02:00
|
|
|
link, err := netlink.LinkByName(s.iface)
|
2019-03-25 01:02:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
return netlink.LinkDel(link)
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 16:38:28 +02:00
|
|
|
// SetUpInterface creates and sets up the associated network interface
|
2020-05-18 10:42:32 +02:00
|
|
|
func (s *State) SetUpInterface(nodes []common.Node, routedNet *net.IPNet) error {
|
2020-05-07 14:40:22 +02:00
|
|
|
if err := netlink.LinkAdd(&wireguard{LinkAttrs: netlink.LinkAttrs{Name: s.iface}}); err != nil && !os.IsExist(err) {
|
|
|
|
return errors.Wrapf(err, "could not create interface %s", s.iface)
|
2019-07-12 11:08:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
peerCfgs, err := s.nodesToPeerConfigs(nodes)
|
2019-07-12 11:08:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error converting received node information to wireguard format")
|
|
|
|
}
|
2020-05-07 14:40:22 +02:00
|
|
|
if err := s.client.ConfigureDevice(s.iface, wgtypes.Config{
|
|
|
|
PrivateKey: &s.PrivKey,
|
|
|
|
ListenPort: &s.Port,
|
2019-07-12 11:08:14 +02:00
|
|
|
ReplacePeers: true,
|
|
|
|
Peers: peerCfgs,
|
2019-07-29 19:20:25 +02:00
|
|
|
}); err != nil {
|
2020-05-07 14:40:22 +02:00
|
|
|
return errors.Wrapf(err, "could not set wireguard configuration for %s", s.iface)
|
2019-07-29 19:20:25 +02:00
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
link, err := netlink.LinkByName(s.iface)
|
2019-07-12 11:08:14 +02:00
|
|
|
if err != nil {
|
2020-05-07 14:40:22 +02:00
|
|
|
return errors.Wrapf(err, "could not get link information for %s", s.iface)
|
2019-07-12 11:08:14 +02:00
|
|
|
}
|
2019-07-29 19:20:25 +02:00
|
|
|
if err := netlink.AddrReplace(link, &netlink.Addr{
|
2020-05-07 14:40:22 +02:00
|
|
|
IPNet: &s.OverlayAddr,
|
2019-07-29 19:20:25 +02:00
|
|
|
}); err != nil {
|
2020-05-07 14:40:22 +02:00
|
|
|
return errors.Wrapf(err, "could not set address for %s", s.iface)
|
2019-07-29 19:20:25 +02:00
|
|
|
}
|
|
|
|
// TODO: make MTU configurable?
|
|
|
|
if err := netlink.LinkSetMTU(link, 1420); err != nil {
|
2020-05-07 14:40:22 +02:00
|
|
|
return errors.Wrapf(err, "could not set MTU for %s", s.iface)
|
2019-07-29 19:20:25 +02:00
|
|
|
}
|
|
|
|
if err := netlink.LinkSetUp(link); err != nil {
|
2020-05-07 14:40:22 +02:00
|
|
|
return errors.Wrapf(err, "could not enable interface %s", s.iface)
|
2019-07-29 19:20:25 +02:00
|
|
|
}
|
2020-05-18 10:42:32 +02:00
|
|
|
|
|
|
|
// first compute routes
|
|
|
|
currentRoutes, err := netlink.RouteList(link, netlink.FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not update the routing table for %s", s.iface)
|
|
|
|
}
|
|
|
|
routes := make([]netlink.Route, 0)
|
|
|
|
for index, node := range nodes {
|
|
|
|
// dev route
|
|
|
|
routes = append(routes, netlink.Route{
|
2019-07-12 11:08:14 +02:00
|
|
|
LinkIndex: link.Attrs().Index,
|
2020-05-18 10:42:32 +02:00
|
|
|
Dst: &nodes[index].OverlayAddr,
|
2019-07-12 11:08:14 +02:00
|
|
|
Scope: netlink.SCOPE_LINK,
|
|
|
|
})
|
2020-05-18 10:42:32 +02:00
|
|
|
// via routes
|
|
|
|
for _, route := range node.Routes {
|
2020-05-18 10:49:32 +02:00
|
|
|
if !routedNet.Contains(route.IP) {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-18 10:42:32 +02:00
|
|
|
routes = append(routes, netlink.Route{
|
|
|
|
LinkIndex: link.Attrs().Index,
|
|
|
|
Dst: &route,
|
|
|
|
Gw: node.OverlayAddr.IP,
|
|
|
|
Scope: netlink.SCOPE_SITE,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// then actually update the routing table
|
|
|
|
for _, route := range routes {
|
|
|
|
match := matchRoute(currentRoutes, route)
|
|
|
|
if match == nil {
|
|
|
|
netlink.RouteAdd(&route)
|
|
|
|
} else if match.Gw.String() != route.Gw.String() {
|
|
|
|
netlink.RouteReplace(&route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, route := range routes {
|
|
|
|
// only delete a reoute if it is a site scope route that belongs to the routed net, mainly to
|
|
|
|
// avoid deleting otherwise manually set routes
|
|
|
|
if matchRoute(currentRoutes, route) == nil && route.Scope == netlink.SCOPE_LINK && routedNet.Contains(route.Dst.IP) {
|
|
|
|
netlink.RouteDel(&route)
|
|
|
|
}
|
2019-03-27 22:48:54 +01:00
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2019-07-12 11:08:14 +02:00
|
|
|
return nil
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
func (s *State) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, error) {
|
2019-07-12 11:08:14 +02:00
|
|
|
peerCfgs := make([]wgtypes.PeerConfig, len(nodes))
|
|
|
|
for i, node := range nodes {
|
|
|
|
pubKey, err := wgtypes.ParseKey(node.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peerCfgs[i] = wgtypes.PeerConfig{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
ReplaceAllowedIPs: true,
|
|
|
|
Endpoint: &net.UDPAddr{
|
|
|
|
IP: node.Addr,
|
2020-05-07 14:40:22 +02:00
|
|
|
Port: s.Port,
|
2019-07-12 11:08:14 +02:00
|
|
|
},
|
2020-05-18 10:42:32 +02:00
|
|
|
AllowedIPs: append([]net.IPNet{node.OverlayAddr}, node.Routes...),
|
2019-07-12 11:08:14 +02:00
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
return peerCfgs, nil
|
|
|
|
}
|
2020-05-18 10:42:32 +02:00
|
|
|
|
|
|
|
func matchRoute(set []netlink.Route, needle netlink.Route) *netlink.Route {
|
|
|
|
// routes are considered equal if they overlap and have the same prefix length
|
|
|
|
prefixn, _ := needle.Dst.Mask.Size()
|
|
|
|
for _, route := range set {
|
|
|
|
prefixr, _ := route.Dst.Mask.Size()
|
|
|
|
if prefixn == prefixr && route.Dst.Contains(needle.Dst.IP) {
|
|
|
|
return &route
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|