2023-03-17 10:37:27 +01:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-05-25 14:16:24 +02:00
|
|
|
"net/netip"
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
"github.com/pion/transport/v2"
|
2023-03-17 10:37:27 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-05-25 14:16:24 +02:00
|
|
|
"golang.org/x/sys/windows"
|
2023-04-13 17:00:01 +02:00
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/iface/bind"
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type tunDevice struct {
|
|
|
|
name string
|
|
|
|
address WGAddress
|
|
|
|
netInterface NetInterface
|
2023-04-13 17:00:01 +02:00
|
|
|
iceBind *bind.ICEBind
|
|
|
|
mtu int
|
|
|
|
uapi net.Listener
|
2023-05-29 16:00:18 +02:00
|
|
|
wrapper *DeviceWrapper
|
2023-04-13 17:00:01 +02:00
|
|
|
close chan struct{}
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
func newTunDevice(name string, address WGAddress, mtu int, transportNet transport.Net) *tunDevice {
|
|
|
|
return &tunDevice{
|
|
|
|
name: name,
|
|
|
|
address: address,
|
|
|
|
mtu: mtu,
|
|
|
|
iceBind: bind.NewICEBind(transportNet),
|
|
|
|
close: make(chan struct{}),
|
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) Create() error {
|
|
|
|
var err error
|
2023-04-13 17:00:01 +02:00
|
|
|
c.netInterface, err = c.createWithUserspace()
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.assignAddr()
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// createWithUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
|
|
|
|
func (c *tunDevice) createWithUserspace() (NetInterface, error) {
|
|
|
|
tunIface, err := tun.CreateTUN(c.name, c.mtu)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
c.wrapper = newDeviceWrapper(tunIface)
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// We need to create a wireguard-go device and listen to configuration requests
|
|
|
|
tunDev := device.NewDevice(tunIface, c.iceBind, device.NewLogger(device.LogLevelSilent, "[netbird] "))
|
|
|
|
err = tunDev.Up()
|
|
|
|
if err != nil {
|
|
|
|
_ = tunIface.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-25 14:16:24 +02:00
|
|
|
luid := winipcfg.LUID(tunIface.(*tun.NativeTun).LUID())
|
|
|
|
|
|
|
|
nbiface, err := luid.IPInterface(windows.AF_INET)
|
|
|
|
if err != nil {
|
|
|
|
_ = tunIface.Close()
|
|
|
|
return nil, fmt.Errorf("got error when getting ip interface %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nbiface.NLMTU = uint32(c.mtu)
|
|
|
|
|
|
|
|
err = nbiface.Set()
|
|
|
|
if err != nil {
|
|
|
|
_ = tunIface.Close()
|
|
|
|
return nil, fmt.Errorf("got error when getting setting the interface mtu: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
c.uapi, err = c.getUAPI(c.name)
|
|
|
|
if err != nil {
|
|
|
|
_ = tunIface.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.close:
|
|
|
|
log.Debugf("exit uapi.Accept()")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
uapiConn, uapiErr := c.uapi.Accept()
|
|
|
|
if uapiErr != nil {
|
|
|
|
log.Traceln("uapi Accept failed with error: ", uapiErr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
tunDev.IpcHandle(uapiConn)
|
|
|
|
log.Debugf("exit tunDevice.IpcHandle")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Debugln("UAPI listener started")
|
|
|
|
return tunIface, nil
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) UpdateAddr(address WGAddress) error {
|
|
|
|
c.address = address
|
|
|
|
return c.assignAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) WgAddress() WGAddress {
|
|
|
|
return c.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) DeviceName() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) Close() error {
|
2023-04-13 17:00:01 +02:00
|
|
|
select {
|
|
|
|
case c.close <- struct{}{}:
|
|
|
|
default:
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
var err1, err2 error
|
|
|
|
if c.netInterface != nil {
|
|
|
|
err1 = c.netInterface.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.uapi != nil {
|
|
|
|
err2 = c.uapi.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
|
|
|
|
return err2
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) getInterfaceGUIDString() (string, error) {
|
|
|
|
if c.netInterface == nil {
|
|
|
|
return "", fmt.Errorf("interface has not been initialized yet")
|
|
|
|
}
|
2023-04-13 17:00:01 +02:00
|
|
|
windowsDevice := c.netInterface.(*tun.NativeTun)
|
|
|
|
luid := winipcfg.LUID(windowsDevice.LUID())
|
2023-03-17 10:37:27 +01:00
|
|
|
guid, err := luid.GUID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return guid.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
|
|
|
func (c *tunDevice) assignAddr() error {
|
2023-04-13 17:00:01 +02:00
|
|
|
tunDev := c.netInterface.(*tun.NativeTun)
|
|
|
|
luid := winipcfg.LUID(tunDev.LUID())
|
2023-03-17 10:37:27 +01:00
|
|
|
log.Debugf("adding address %s to interface: %s", c.address.IP, c.name)
|
2023-05-25 14:16:24 +02:00
|
|
|
return luid.SetIPAddresses([]netip.Prefix{netip.MustParsePrefix(c.address.String())})
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// getUAPI returns a Listener
|
|
|
|
func (c *tunDevice) getUAPI(iface string) (net.Listener, error) {
|
|
|
|
return ipc.UAPIListen(iface)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|