From ac25702eaf13bb2243f02130ae271ff600f75209 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 1 Apr 2019 12:00:33 +0200 Subject: wintun: rename device using undocumented API that netsh.exe uses --- tun/wintun/netshell/netshell_windows.go | 32 ++++++++++++++++++++++++++++++++ tun/wintun/wintun_windows.go | 10 +++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tun/wintun/netshell/netshell_windows.go (limited to 'tun') diff --git a/tun/wintun/netshell/netshell_windows.go b/tun/wintun/netshell/netshell_windows.go new file mode 100644 index 0000000..cb03252 --- /dev/null +++ b/tun/wintun/netshell/netshell_windows.go @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package netshell + +import ( + "syscall" + "unsafe" + + "golang.org/x/sys/windows" +) + +var ( + modnetshell = windows.NewLazySystemDLL("netshell.dll") + procHrRenameConnection = modnetshell.NewProc("HrRenameConnection") +) + +func HrRenameConnection(guid *windows.GUID, newName *uint16) (err error) { + err = procHrRenameConnection.Find() + if err != nil { + // Missing from servercore, so we can't presume it's always there. + return + } + + ret, _, _ := syscall.Syscall(procHrRenameConnection.Addr(), 2, uintptr(unsafe.Pointer(guid)), uintptr(unsafe.Pointer(newName)), 0) + if ret != 0 { + err = syscall.Errno(ret) + } + return +} diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index 77e83a0..8e09c41 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -8,6 +8,7 @@ package wintun import ( "errors" "fmt" + "golang.zx2c4.com/wireguard/tun/wintun/netshell" "strings" "syscall" "time" @@ -449,13 +450,20 @@ func (wintun *Wintun) GetInterfaceName() (string, error) { // SetInterfaceName sets network interface name. // func (wintun *Wintun) SetInterfaceName(ifname string) error { + // We open the registry key before calling HrRename, because the registry open will wait until the key exists. key, err := registryOpenKeyRetry(registry.LOCAL_MACHINE, wintun.GetNetRegKeyName(), registry.SET_VALUE) if err != nil { return errors.New("Network-specific registry key open failed: " + err.Error()) } defer key.Close() - // Set the interface name. + // We have to tell the various runtime COM services about the new name too. We ignore the + // error because netshell isn't available on servercore. + // TODO: netsh.exe falls back to NciSetConnection in this case. If somebody complains, maybe + // we should do the same. + _ = netshell.HrRenameConnection(&wintun.CfgInstanceID, windows.StringToUTF16Ptr(ifname)) + + // Set the interface name. The above line should have done this too, but in case it failed, we force it. return key.SetStringValue("Name", ifname) } -- cgit v1.2.3-59-g8ed1b