2023-03-17 10:37:27 +01:00
|
|
|
//go:build linux && !android
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
2022-09-14 22:26:11 +02:00
|
|
|
"fmt"
|
2023-02-13 18:34:56 +01:00
|
|
|
"os"
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vishvananda/netlink"
|
2022-01-24 22:45:52 +01:00
|
|
|
)
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) Create() error {
|
2023-04-13 17:00:01 +02:00
|
|
|
if WireGuardModuleIsLoaded() {
|
2023-07-14 21:56:22 +02:00
|
|
|
log.Infof("create tun interface with kernel WireGuard support: %s", c.DeviceName())
|
2023-03-17 10:37:27 +01:00
|
|
|
return c.createWithKernel()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tunModuleIsLoaded() {
|
|
|
|
return fmt.Errorf("couldn't check or load tun module")
|
2021-06-18 13:01:43 +02:00
|
|
|
}
|
2023-07-14 21:56:22 +02:00
|
|
|
log.Infof("create tun interface with userspace WireGuard support: %s", c.DeviceName())
|
2023-03-17 10:37:27 +01:00
|
|
|
var err error
|
|
|
|
c.netInterface, err = c.createWithUserspace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.assignAddr()
|
|
|
|
|
2021-06-18 13:01:43 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// createWithKernel Creates a new WireGuard interface using kernel WireGuard module.
|
2021-06-24 11:02:40 +02:00
|
|
|
// Works for Linux and offers much better network performance
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) createWithKernel() error {
|
2021-06-24 11:02:40 +02:00
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
link := newWGLink(c.name)
|
2021-06-24 11:02:40 +02:00
|
|
|
|
2021-08-26 18:04:19 +02:00
|
|
|
// check if interface exists
|
2023-03-17 10:37:27 +01:00
|
|
|
l, err := netlink.LinkByName(c.name)
|
2021-08-26 18:04:19 +02:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case netlink.LinkNotFoundError:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove if interface exists
|
|
|
|
if l != nil {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = netlink.LinkDel(link)
|
2021-08-26 18:04:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Debugf("adding device: %s", c.name)
|
2022-01-17 14:01:58 +01:00
|
|
|
err = netlink.LinkAdd(link)
|
2021-06-24 11:02:40 +02:00
|
|
|
if os.IsExist(err) {
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Infof("interface %s already exists. Will reuse.", c.name)
|
2021-06-24 11:02:40 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
c.netInterface = link
|
2022-01-17 14:01:58 +01:00
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
err = c.assignAddr()
|
2021-06-24 11:02:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo do a discovery
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Debugf("setting MTU: %d interface: %s", c.mtu, c.name)
|
|
|
|
err = netlink.LinkSetMTU(link, c.mtu)
|
2021-06-24 11:02:40 +02:00
|
|
|
if err != nil {
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Errorf("error setting MTU on interface: %s", c.name)
|
2021-06-24 11:02:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Debugf("bringing up interface: %s", c.name)
|
2022-01-17 14:01:58 +01:00
|
|
|
err = netlink.LinkSetUp(link)
|
2021-06-24 11:02:40 +02:00
|
|
|
if err != nil {
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Errorf("error bringing up interface: %s", c.name)
|
2021-06-24 11:02:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 13:01:43 +02:00
|
|
|
// assignAddr Adds IP address to the tunnel interface
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) assignAddr() error {
|
|
|
|
link := newWGLink(c.name)
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2021-08-26 15:32:05 +02:00
|
|
|
//delete existing addresses
|
2022-01-17 14:01:58 +01:00
|
|
|
list, err := netlink.AddrList(link, 0)
|
2021-08-26 15:32:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(list) > 0 {
|
|
|
|
for _, a := range list {
|
2023-12-04 13:34:06 +01:00
|
|
|
addr := a
|
|
|
|
err = netlink.AddrDel(link, &addr)
|
2021-08-26 15:32:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Debugf("adding address %s to interface: %s", c.address.String(), c.name)
|
|
|
|
addr, _ := netlink.ParseAddr(c.address.String())
|
2022-01-17 14:01:58 +01:00
|
|
|
err = netlink.AddrAdd(link, addr)
|
2021-05-01 12:45:37 +02:00
|
|
|
if os.IsExist(err) {
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Infof("interface %s already has the address: %s", c.name, c.address.String())
|
2021-05-01 12:45:37 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// On linux, the link must be brought up
|
2022-01-17 14:01:58 +01:00
|
|
|
err = netlink.LinkSetUp(link)
|
2021-05-01 12:45:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-06-06 00:40:44 +02:00
|
|
|
|
|
|
|
type wgLink struct {
|
|
|
|
attrs *netlink.LinkAttrs
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
func newWGLink(name string) *wgLink {
|
|
|
|
attrs := netlink.NewLinkAttrs()
|
|
|
|
attrs.Name = name
|
|
|
|
|
|
|
|
return &wgLink{
|
|
|
|
attrs: &attrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 00:40:44 +02:00
|
|
|
// Attrs returns the Wireguard's default attributes
|
2022-01-17 14:01:58 +01:00
|
|
|
func (l *wgLink) Attrs() *netlink.LinkAttrs {
|
|
|
|
return l.attrs
|
2021-06-06 00:40:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the interface type
|
2022-01-17 14:01:58 +01:00
|
|
|
func (l *wgLink) Type() string {
|
2021-06-06 00:40:44 +02:00
|
|
|
return "wireguard"
|
|
|
|
}
|
2021-07-19 15:02:11 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// Close deletes the link interface
|
|
|
|
func (l *wgLink) Close() error {
|
|
|
|
return netlink.LinkDel(l)
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|