Improve tun activation error messages

This commit is contained in:
Nate Brown 2019-11-23 23:18:03 -08:00
parent 9bd8cd2c11
commit 5cad6a2ce3
1 changed files with 8 additions and 8 deletions

View File

@ -168,43 +168,43 @@ func (c Tun) Activate() error {
// Set the device ip address // Set the device ip address
if err = ioctl(fd, syscall.SIOCSIFADDR, uintptr(unsafe.Pointer(&ifra))); err != nil { if err = ioctl(fd, syscall.SIOCSIFADDR, uintptr(unsafe.Pointer(&ifra))); err != nil {
return err return fmt.Errorf("failed to set tun address: %s", err)
} }
// Set the device network // Set the device network
ifra.Addr.Addr = mask ifra.Addr.Addr = mask
if err = ioctl(fd, syscall.SIOCSIFNETMASK, uintptr(unsafe.Pointer(&ifra))); err != nil { if err = ioctl(fd, syscall.SIOCSIFNETMASK, uintptr(unsafe.Pointer(&ifra))); err != nil {
return err return fmt.Errorf("failed to set tun netmask: %s", err)
} }
// Set the device name // Set the device name
ifrf := ifReq{Name: devName} ifrf := ifReq{Name: devName}
if err = ioctl(fd, syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil { if err = ioctl(fd, syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
return err return fmt.Errorf("failed to set tun device name: %s", err)
} }
// Set the MTU on the device // Set the MTU on the device
ifm := ifreqMTU{Name: devName, MTU: c.MaxMTU} ifm := ifreqMTU{Name: devName, MTU: c.MaxMTU}
if err = ioctl(fd, syscall.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil { if err = ioctl(fd, syscall.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
return err return fmt.Errorf("failed to set tun mtu: %s", err)
} }
// Set the transmit queue length // Set the transmit queue length
ifrq := ifreqQLEN{Name: devName, Value: c.TXQueueLen} ifrq := ifreqQLEN{Name: devName, Value: c.TXQueueLen}
if err = ioctl(fd, syscall.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil { if err = ioctl(fd, syscall.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
return err return fmt.Errorf("failed to set tun tx queue length: %s", err)
} }
// Bring up the interface // Bring up the interface
ifrf.Flags = ifrf.Flags | syscall.IFF_UP ifrf.Flags = ifrf.Flags | syscall.IFF_UP
if err = ioctl(fd, syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil { if err = ioctl(fd, syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
return err return fmt.Errorf("failed to bring the tun device up: %s", err)
} }
// Set the routes // Set the routes
link, err := netlink.LinkByName(c.Device) link, err := netlink.LinkByName(c.Device)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get tun device link: %s", err)
} }
// Default route // Default route
@ -242,7 +242,7 @@ func (c Tun) Activate() error {
// Run the interface // Run the interface
ifrf.Flags = ifrf.Flags | syscall.IFF_UP | syscall.IFF_RUNNING ifrf.Flags = ifrf.Flags | syscall.IFF_UP | syscall.IFF_RUNNING
if err = ioctl(fd, syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil { if err = ioctl(fd, syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
return err return fmt.Errorf("failed to run tun device: %s", err)
} }
return nil return nil