2023-03-17 10:37:27 +01:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-25 14:16:24 +02:00
|
|
|
"net/netip"
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/transport/v3"
|
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/tun"
|
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/iface/bind"
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
2024-08-27 19:21:14 +02:00
|
|
|
const defaultWindowsGUIDSTring = "{f2f29e61-d91f-4d76-8151-119b20c4bdeb}"
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
type tunDevice struct {
|
2024-01-03 16:06:20 +01:00
|
|
|
name string
|
|
|
|
address WGAddress
|
|
|
|
port int
|
|
|
|
key string
|
|
|
|
mtu int
|
|
|
|
iceBind *bind.ICEBind
|
|
|
|
|
|
|
|
device *device.Device
|
|
|
|
nativeTunDevice *tun.NativeTun
|
|
|
|
wrapper *DeviceWrapper
|
|
|
|
udpMux *bind.UniversalUDPMuxDefault
|
|
|
|
configurer wgConfigurer
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-06-19 12:12:11 +02:00
|
|
|
func newTunDevice(name string, address WGAddress, port int, key string, mtu int, transportNet transport.Net, filterFn bind.FilterFn) wgTunDevice {
|
2023-04-13 17:00:01 +02:00
|
|
|
return &tunDevice{
|
|
|
|
name: name,
|
|
|
|
address: address,
|
2024-01-03 16:06:20 +01:00
|
|
|
port: port,
|
|
|
|
key: key,
|
2023-04-13 17:00:01 +02:00
|
|
|
mtu: mtu,
|
2024-06-19 12:12:11 +02:00
|
|
|
iceBind: bind.NewICEBind(transportNet, filterFn),
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-08-27 19:21:14 +02:00
|
|
|
func getGUID() (windows.GUID, error) {
|
|
|
|
guidString := defaultWindowsGUIDSTring
|
|
|
|
if CustomWindowsGUIDString != "" {
|
|
|
|
guidString = CustomWindowsGUIDString
|
|
|
|
}
|
|
|
|
return windows.GUIDFromString(guidString)
|
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) Create() (wgConfigurer, error) {
|
2024-08-27 19:21:14 +02:00
|
|
|
guid, err := getGUID()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get GUID: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-15 14:45:18 +02:00
|
|
|
log.Info("create tun interface")
|
2024-08-27 19:21:14 +02:00
|
|
|
tunDevice, err := tun.CreateTUNWithRequestedGUID(t.name, &guid, t.mtu)
|
2023-04-13 17:00:01 +02:00
|
|
|
if err != nil {
|
2024-09-02 19:19:14 +02:00
|
|
|
return nil, fmt.Errorf("error creating tun device: %s", err)
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
t.nativeTunDevice = tunDevice.(*tun.NativeTun)
|
|
|
|
t.wrapper = newDeviceWrapper(tunDevice)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// We need to create a wireguard-go device and listen to configuration requests
|
2024-01-03 16:06:20 +01:00
|
|
|
t.device = device.NewDevice(
|
|
|
|
t.wrapper,
|
|
|
|
t.iceBind,
|
2024-07-15 14:45:18 +02:00
|
|
|
device.NewLogger(wgLogLevel(), "[netbird] "),
|
2024-01-03 16:06:20 +01:00
|
|
|
)
|
2023-04-13 17:00:01 +02:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
luid := winipcfg.LUID(t.nativeTunDevice.LUID())
|
2023-05-25 14:16:24 +02:00
|
|
|
|
|
|
|
nbiface, err := luid.IPInterface(windows.AF_INET)
|
|
|
|
if err != nil {
|
2024-01-03 16:06:20 +01:00
|
|
|
t.device.Close()
|
2023-05-25 14:16:24 +02:00
|
|
|
return nil, fmt.Errorf("got error when getting ip interface %s", err)
|
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
nbiface.NLMTU = uint32(t.mtu)
|
2023-05-25 14:16:24 +02:00
|
|
|
|
|
|
|
err = nbiface.Set()
|
|
|
|
if err != nil {
|
2024-01-03 16:06:20 +01:00
|
|
|
t.device.Close()
|
2023-05-25 14:16:24 +02:00
|
|
|
return nil, fmt.Errorf("got error when getting setting the interface mtu: %s", err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
err = t.assignAddr()
|
2023-04-13 17:00:01 +02:00
|
|
|
if err != nil {
|
2024-01-03 16:06:20 +01:00
|
|
|
t.device.Close()
|
2024-09-02 19:19:14 +02:00
|
|
|
return nil, fmt.Errorf("error assigning ip: %s", err)
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
t.configurer = newWGUSPConfigurer(t.device, t.name)
|
|
|
|
err = t.configurer.configureInterface(t.key, t.port)
|
|
|
|
if err != nil {
|
|
|
|
t.device.Close()
|
|
|
|
t.configurer.close()
|
2024-09-02 19:19:14 +02:00
|
|
|
return nil, fmt.Errorf("error configuring interface: %s", err)
|
2024-01-03 16:06:20 +01:00
|
|
|
}
|
|
|
|
return t.configurer, nil
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) Up() (*bind.UniversalUDPMuxDefault, error) {
|
|
|
|
err := t.device.Up()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
udpMux, err := t.iceBind.GetICEMux()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.udpMux = udpMux
|
|
|
|
log.Debugf("device is ready to use: %s", t.name)
|
|
|
|
return udpMux, nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) UpdateAddr(address WGAddress) error {
|
|
|
|
t.address = address
|
|
|
|
return t.assignAddr()
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) Close() error {
|
|
|
|
if t.configurer != nil {
|
|
|
|
t.configurer.close()
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
if t.device != nil {
|
|
|
|
t.device.Close()
|
|
|
|
t.device = nil
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
if t.udpMux != nil {
|
|
|
|
return t.udpMux.Close()
|
2023-04-13 17:00:01 +02:00
|
|
|
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (t *tunDevice) WgAddress() WGAddress {
|
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) DeviceName() string {
|
|
|
|
return t.name
|
|
|
|
}
|
2023-04-13 17:00:01 +02:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) Wrapper() *DeviceWrapper {
|
|
|
|
return t.wrapper
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) getInterfaceGUIDString() (string, error) {
|
|
|
|
if t.nativeTunDevice == nil {
|
2023-03-17 10:37:27 +01:00
|
|
|
return "", fmt.Errorf("interface has not been initialized yet")
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
luid := winipcfg.LUID(t.nativeTunDevice.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
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *tunDevice) assignAddr() error {
|
|
|
|
luid := winipcfg.LUID(t.nativeTunDevice.LUID())
|
|
|
|
log.Debugf("adding address %s to interface: %s", t.address.IP, t.name)
|
|
|
|
return luid.SetIPAddresses([]netip.Prefix{netip.MustParsePrefix(t.address.String())})
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|