2021-05-01 12:45:37 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-06-18 13:01:43 +02:00
|
|
|
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
|
|
|
// Will reuse an existing one.
|
|
|
|
func Create(iface string, address string) error {
|
2021-05-01 12:45:37 +02:00
|
|
|
attrs := netlink.NewLinkAttrs()
|
2021-06-18 13:01:43 +02:00
|
|
|
attrs.Name = iface
|
|
|
|
|
|
|
|
link := wgLink{
|
|
|
|
attrs: &attrs,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("adding device: %s", iface)
|
|
|
|
err := netlink.LinkAdd(&link)
|
|
|
|
if os.IsExist(err) {
|
|
|
|
log.Infof("interface %s already exists. Will reuse.", iface)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("adding address %s to interface: %s", address, iface)
|
|
|
|
addr, _ := netlink.ParseAddr(address)
|
|
|
|
err = netlink.AddrAdd(&link, addr)
|
|
|
|
if os.IsExist(err) {
|
|
|
|
log.Infof("interface %s already has the address: %s", iface, address)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = assignAddr(address, iface)
|
2021-06-07 00:35:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2021-06-18 13:01:43 +02:00
|
|
|
// todo do a discovery
|
|
|
|
log.Debugf("setting MTU: %s", iface)
|
|
|
|
err = netlink.LinkSetMTU(&link, defaultMTU)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error setting MTU on interface: %s", iface)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("bringing up interface: %s", iface)
|
|
|
|
err = netlink.LinkSetUp(&link)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error bringing up interface: %s", iface)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assignAddr Adds IP address to the tunnel interface
|
|
|
|
func assignAddr(address, name string) error {
|
|
|
|
var err error
|
|
|
|
attrs := netlink.NewLinkAttrs()
|
|
|
|
attrs.Name = name
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
link := wgLink{
|
|
|
|
attrs: &attrs,
|
|
|
|
}
|
|
|
|
|
2021-06-06 00:40:44 +02:00
|
|
|
log.Debugf("adding address %s to interface: %s", address, attrs.Name)
|
2021-05-01 12:45:37 +02:00
|
|
|
addr, _ := netlink.ParseAddr(address)
|
2021-06-06 00:40:44 +02:00
|
|
|
err = netlink.AddrAdd(&link, addr)
|
2021-05-01 12:45:37 +02:00
|
|
|
if os.IsExist(err) {
|
2021-06-06 00:40:44 +02:00
|
|
|
log.Infof("interface %s already has the address: %s", attrs.Name, address)
|
2021-05-01 12:45:37 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// On linux, the link must be brought up
|
|
|
|
err = netlink.LinkSetUp(&link)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-06 00:40:44 +02:00
|
|
|
|
|
|
|
type wgLink struct {
|
|
|
|
attrs *netlink.LinkAttrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attrs returns the Wireguard's default attributes
|
|
|
|
func (w *wgLink) Attrs() *netlink.LinkAttrs {
|
|
|
|
return w.attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the interface type
|
|
|
|
func (w *wgLink) Type() string {
|
|
|
|
return "wireguard"
|
|
|
|
}
|