From e0a6dca7d4415392124efe34c110b8c699935b7f Mon Sep 17 00:00:00 2001 From: chain710 Date: Fri, 6 Jan 2023 10:26:07 +0800 Subject: [PATCH] In NativeTun.configure, inet addr may already exist, so AddrAdd=>AddrReplace - add more error info to help diagnose --- tun_linux.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tun_linux.go b/tun_linux.go index 31cce3d..f8e6686 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -70,7 +70,7 @@ func init() { func open(name string) (int, error) { fd, err := unix.Open(controlPath, unix.O_RDWR, 0) if err != nil { - return -1, err + return -1, E.Cause(err, "open:", controlPath) } var ifr struct { @@ -84,12 +84,12 @@ func open(name string) (int, error) { _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), unix.TUNSETIFF, uintptr(unsafe.Pointer(&ifr))) if errno != 0 { unix.Close(fd) - return -1, errno + return -1, E.Cause(errno, "ioctl") } if err = unix.SetNonblock(fd, true); err != nil { unix.Close(fd) - return -1, err + return -1, E.Cause(err, "set nonblock") } return fd, nil @@ -107,25 +107,25 @@ func (t *NativeTun) configure(tunLink netlink.Link) error { if len(t.options.Inet4Address) > 0 { for _, address := range t.options.Inet4Address { addr4, _ := netlink.ParseAddr(address.String()) - err = netlink.AddrAdd(tunLink, addr4) + err = netlink.AddrReplace(tunLink, addr4) if err != nil { - return err + return E.Cause(err, "add inet4 addr:", address.String()) } } } if len(t.options.Inet6Address) > 0 { for _, address := range t.options.Inet6Address { addr6, _ := netlink.ParseAddr(address.String()) - err = netlink.AddrAdd(tunLink, addr6) + err = netlink.AddrReplace(tunLink, addr6) if err != nil { - return err + return E.Cause(err, "add inet6 addr:", address.String()) } } } err = netlink.LinkSetUp(tunLink) if err != nil { - return err + return E.Cause(err, "link setup") } if t.options.TableIndex == 0 { @@ -141,7 +141,7 @@ func (t *NativeTun) configure(tunLink netlink.Link) error { err = t.setRoute(tunLink) if err != nil { _ = t.unsetRoute0(tunLink) - return err + return E.Cause(err, "set route") } err = t.unsetRules() @@ -151,7 +151,7 @@ func (t *NativeTun) configure(tunLink netlink.Link) error { err = t.setRules() if err != nil { _ = t.unsetRules() - return err + return E.Cause(err, "set rules") } if t.options.AutoRoute && runtime.GOOS == "android" {