aboutsummaryrefslogtreecommitdiffstats
path: root/tun
diff options
context:
space:
mode:
authorruokeqx <ruokeqx@gmail.com>2025-01-02 20:28:33 +0800
committerJason A. Donenfeld <Jason@zx2c4.com>2025-05-05 15:10:08 +0200
commitbc30fee374479c9a327285f77a06136d29884c07 (patch)
tree8175b639374cebde9e6683f29636cd816995412f /tun
parenttun: use add-with-carry in checksumNoFold() (diff)
downloadwireguard-go-bc30fee374479c9a327285f77a06136d29884c07.tar.xz
wireguard-go-bc30fee374479c9a327285f77a06136d29884c07.zip
tun: darwin: fetch flags and mtu from if_msghdr directly
Signed-off-by: ruokeqx <ruokeqx@gmail.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tun')
-rw-r--r--tun/tun_darwin.go34
1 files changed, 9 insertions, 25 deletions
diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go
index 407b6f2..341afe3 100644
--- a/tun/tun_darwin.go
+++ b/tun/tun_darwin.go
@@ -6,14 +6,12 @@
package tun
import (
- "errors"
"fmt"
"io"
"net"
"os"
"sync"
"syscall"
- "time"
"unsafe"
"golang.org/x/sys/unix"
@@ -30,18 +28,6 @@ type NativeTun struct {
closeOnce sync.Once
}
-func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
- for i := 0; i < 20; i++ {
- iface, err = net.InterfaceByIndex(index)
- if err != nil && errors.Is(err, unix.ENOMEM) {
- time.Sleep(time.Duration(i) * time.Second / 3)
- continue
- }
- return iface, err
- }
- return nil, err
-}
-
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
var (
statusUp bool
@@ -62,26 +48,22 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
return
}
- if n < 14 {
+ if n < 28 {
continue
}
- if data[3 /* type */] != unix.RTM_IFINFO {
+ if data[3 /* ifm_type */] != unix.RTM_IFINFO {
continue
}
- ifindex := int(*(*uint16)(unsafe.Pointer(&data[12 /* ifindex */])))
+ ifindex := int(*(*uint16)(unsafe.Pointer(&data[12 /* ifm_index */])))
if ifindex != tunIfindex {
continue
}
- iface, err := retryInterfaceByIndex(ifindex)
- if err != nil {
- tun.errors <- err
- return
- }
+ flags := int(*(*uint32)(unsafe.Pointer(&data[8 /* ifm_flags */])))
// Up / Down event
- up := (iface.Flags & net.FlagUp) != 0
+ up := (flags & syscall.IFF_UP) != 0
if up != statusUp && up {
tun.events <- EventUp
}
@@ -90,11 +72,13 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
}
statusUp = up
+ mtu := int(*(*uint32)(unsafe.Pointer(&data[24 /* ifm_data.ifi_mtu */])))
+
// MTU changes
- if iface.MTU != statusMTU {
+ if mtu != statusMTU {
tun.events <- EventMTUUpdate
}
- statusMTU = iface.MTU
+ statusMTU = mtu
}
}