From 00ca312b2a63ab27a1155a3b1cb45d41a06f7657 Mon Sep 17 00:00:00 2001 From: Jack Doan Date: Wed, 10 Jul 2024 15:47:32 -0400 Subject: [PATCH] make ipv6 address assignment work with tun_linux.go --- overlay/tun_linux.go | 56 +++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/overlay/tun_linux.go b/overlay/tun_linux.go index 0e7e20d41..658e23f92 100644 --- a/overlay/tun_linux.go +++ b/overlay/tun_linux.go @@ -46,12 +46,24 @@ type ifReq struct { pad [8]byte } -type ifreqAddr struct { - Name [16]byte - Addr unix.RawSockaddrInet4 - pad [8]byte +type ifReqIdx struct { + Name [16]byte + IfIndex uint32 + pad [6]byte } +type in6IfReq struct { + Addr [16]byte + PrefixLen uint32 + IfIndex uint32 +} + +//type ifreqAddr struct { +// Name [16]byte +// Addr unix.RawSockaddrInet4 +// pad [8]byte +//} + type ifreqMTU struct { Name [16]byte MTU int32 @@ -272,15 +284,14 @@ func (t *tun) Activate() error { t.watchRoutes() } - var addr, mask [4]byte + var mask [4]byte //TODO: IPV6-WORK - addr = t.cidr.Addr().As4() tmask := net.CIDRMask(t.cidr.Bits(), 32) copy(mask[:], tmask) s, err := unix.Socket( - unix.AF_INET, + unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_IP, ) @@ -289,29 +300,26 @@ func (t *tun) Activate() error { } t.ioctlFd = uintptr(s) - ifra := ifreqAddr{ - Name: devName, - Addr: unix.RawSockaddrInet4{ - Family: unix.AF_INET, - Addr: addr, - }, + // Set the device name + ifrf := ifReq{Name: devName} + if err = ioctl(t.ioctlFd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil { + return fmt.Errorf("failed to set tun device name: %s", err) } - // Set the device ip address - if err = ioctl(t.ioctlFd, unix.SIOCSIFADDR, uintptr(unsafe.Pointer(&ifra))); err != nil { - return fmt.Errorf("failed to set tun address: %s", err) + idxReq := ifReqIdx{Name: devName} + if err = ioctl(t.ioctlFd, unix.SIOCGIFINDEX, uintptr(unsafe.Pointer(&idxReq))); err != nil { + return fmt.Errorf("get tun index: %s", err) } - // Set the device network - ifra.Addr.Addr = mask - if err = ioctl(t.ioctlFd, unix.SIOCSIFNETMASK, uintptr(unsafe.Pointer(&ifra))); err != nil { - return fmt.Errorf("failed to set tun netmask: %s", err) + ifra6 := in6IfReq{ + Addr: t.cidr.Addr().As16(), + PrefixLen: uint32(t.cidr.Bits()), + IfIndex: idxReq.IfIndex, } - // Set the device name - ifrf := ifReq{Name: devName} - if err = ioctl(t.ioctlFd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil { - return fmt.Errorf("failed to set tun device name: %s", err) + // Set the device ip address + if err = ioctl(t.ioctlFd, unix.SIOCSIFADDR, uintptr(unsafe.Pointer(&ifra6))); err != nil { + return fmt.Errorf("failed to set tun address: %s", err) } // Setup our default MTU