Skip to content

Instantly share code, notes, and snippets.

@genneko
Last active April 15, 2019 08:09
Show Gist options
  • Save genneko/303ede6a2dde5ad58936ef571a71f024 to your computer and use it in GitHub Desktop.
Save genneko/303ede6a2dde5ad58936ef571a71f024 to your computer and use it in GitHub Desktop.
FreeBSD patches for wireguard-go and wg-quick
diff --git a/main.go b/main.go
index a3a04b8..e3fa1d5 100644
--- a/main.go
+++ b/main.go
@@ -286,6 +286,9 @@ func main() {
}
// clean up
+ var numcpu = runtime.GOMAXPROCS(1)
+ fmt.Fprintln(os.Stderr, "W CPU = ", numcpu)
+ fmt.Fprintln(os.Stderr, "W CPU = ", runtime.NumCPU())
uapi.Close()
device.Close()
diff --git a/tun/tun_freebsd.go b/tun/tun_freebsd.go
index 9c16183..42de8e9 100644
--- a/tun/tun_freebsd.go
+++ b/tun/tun_freebsd.go
@@ -263,40 +263,43 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
return nil, fmt.Errorf("error %s", errno.Error())
}
- // Rename tun interface
+ if name != assignedName {
+ // Rename tun interface
+
+ // Open control socket
+ confd, err := unix.Socket(
+ unix.AF_INET,
+ unix.SOCK_DGRAM,
+ 0,
+ )
- // Open control socket
- confd, err := unix.Socket(
- unix.AF_INET,
- unix.SOCK_DGRAM,
- 0,
- )
+ if err != nil {
+ return nil, err
+ }
- if err != nil {
- return nil, err
- }
+ defer unix.Close(confd)
- defer unix.Close(confd)
+ // set up struct for iface rename
+ var newnp [unix.IFNAMSIZ]byte
+ copy(newnp[:], name)
- // set up struct for iface rename
- var newnp [unix.IFNAMSIZ]byte
- copy(newnp[:], name)
+ var ifr ifreq_ptr
+ copy(ifr.Name[:], assignedName)
+ ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
- var ifr ifreq_ptr
- copy(ifr.Name[:], assignedName)
- ifr.Data = uintptr(unsafe.Pointer(&newnp[0]))
+ //do actual ioctl to rename iface
+ _, _, errno = unix.Syscall(
+ unix.SYS_IOCTL,
+ uintptr(confd),
+ uintptr(unix.SIOCSIFNAME),
+ uintptr(unsafe.Pointer(&ifr)),
+ )
+ if errno != 0 {
+ tunFile.Close()
+ tunDestroy(name)
+ return nil, fmt.Errorf("failed to rename %s to %s: %s", assignedName, name, errno.Error())
+ }
- //do actual ioctl to rename iface
- _, _, errno = unix.Syscall(
- unix.SYS_IOCTL,
- uintptr(confd),
- uintptr(unix.SIOCSIFNAME),
- uintptr(unsafe.Pointer(&ifr)),
- )
- if errno != 0 {
- tunFile.Close()
- tunDestroy(name)
- return nil, fmt.Errorf("failed to rename %s to %s: %s", assignedName, name, errno.Error())
}
return CreateTUNFromFile(tunFile, mtu)
diff --git a/src/tools/wg-quick/freebsd.bash b/src/tools/wg-quick/freebsd.bash
index 93f1a3b..9ce3be2 100755
--- a/src/tools/wg-quick/freebsd.bash
+++ b/src/tools/wg-quick/freebsd.bash
@@ -55,7 +55,12 @@ make_temp() {
}
clean_temp() {
- [[ -n $CLEANUP_TMPDIR ]] && rm -rf "$CLEANUP_TMPDIR"
+ if [ -n $CLEANUP_TMPDIR ]; then
+ if [ -f "$CLEANUP_TMPDIR/monitor.pid" ]; then
+ kill $(cat $CLEANUP_TMPDIR/monitor.pid)
+ fi
+ rm -rf "$CLEANUP_TMPDIR"
+ fi
}
parse_options() {
@@ -271,12 +276,14 @@ monitor_daemon() {
# in response to incoming packets, and then call set_endpoint_direct_route
# then too. That function should be able to gracefully cleanup if the
# endpoints change.
+ exec 59< <(exec route -n monitor 2>/dev/null)
+ echo $! > $CLEANUP_TMPDIR/monitor.pid
while read -r event; do
[[ $event == RTM_* ]] || continue
ifconfig "$INTERFACE" >/dev/null 2>&1 || break
[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
# TODO: set the mtu as well, but only if up
- done < <(route -n monitor)) & disown
+ done <&59) & disown
}
HAVE_SET_DNS=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment