From 6b563b5ad5b7d82198d181b42651d582d60948ec Mon Sep 17 00:00:00 2001 From: KusakabeSi Date: Tue, 24 Aug 2021 13:26:20 +0000 Subject: [PATCH] LinuxTap, bugfix --- main_edge.go | 2 ++ tap/tap_linux.go | 50 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/main_edge.go b/main_edge.go index 382d3ee..9313396 100644 --- a/main_edge.go +++ b/main_edge.go @@ -176,6 +176,8 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) { thetap, err = tap.CreateUDPSockTAP(tconfig.Interface, lis, sen) case "vpp": thetap, err = tap.CreateVppTAP(tconfig.Interface, tconfig.LogLevel.LogLevel) + case "tap": + thetap, err = tap.CreateTAP(tconfig.Interface) default: return errors.New("Unknow interface type:" + tconfig.Interface.Itype) } diff --git a/tap/tap_linux.go b/tap/tap_linux.go index 6937800..7dde574 100644 --- a/tap/tap_linux.go +++ b/tap/tap_linux.go @@ -212,16 +212,53 @@ func (tap *NativeTap) setMacAddr(mac MacAddress) (err error) { ifreq = struct.pack('16sH6B8x', self.name, AF_UNIX, *macbytes) fcntl.ioctl(sockfd, SIOCSIFHWADDR, ifreq) */ - copy(ifr[:16], name) - binary.BigEndian.PutUint16(ifr[16:18], unix.AF_UNIX) - copy(ifr[18:24], mac[:]) - copy(ifr[24:32], make([]byte, 8)) + copy(ifr[:unix.IFNAMSIZ], name) + binary.LittleEndian.PutUint16(ifr[unix.IFNAMSIZ:unix.IFNAMSIZ+2], unix.AF_UNIX) + copy(ifr[unix.IFNAMSIZ+2:unix.IFNAMSIZ+8], mac[:]) _, _, err = unix.Syscall( unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFHWADDR), uintptr(unsafe.Pointer(&ifr[0])), ) + if err.(syscall.Errno) == syscall.Errno(0) { + err = nil + } + return +} + +func (tap *NativeTap) setUp() (err error) { + fd, err := unix.Socket( + unix.AF_INET, + unix.SOCK_DGRAM, + 0, + ) + if err != nil { + return err + } + defer unix.Close(fd) + var ifr [ifReqSize]byte + name, err := tap.Name() + if err != nil { + return err + } + /* + ifreq = struct.pack('16sH6B8x', self.name, AF_UNIX, *macbytes) + fcntl.ioctl(sockfd, SIOCSIFHWADDR, ifreq) + */ + flags := uint16(unix.IFF_UP) + copy(ifr[:unix.IFNAMSIZ], name) + binary.LittleEndian.PutUint16(ifr[unix.IFNAMSIZ:unix.IFNAMSIZ+2], unix.AF_UNIX) + binary.LittleEndian.PutUint16(ifr[unix.IFNAMSIZ+2:unix.IFNAMSIZ+4], flags) + _, _, err = unix.Syscall( + unix.SYS_IOCTL, + uintptr(fd), + uintptr(unix.SIOCSIFFLAGS), + uintptr(unsafe.Pointer(&ifr[0])), + ) + if err.(syscall.Errno) == syscall.Errno(0) { + err = nil + } return } @@ -506,6 +543,11 @@ func CreateTAPFromFile(file *os.File, iconfig config.InterfaceConf) (Device, err unix.Close(tap.netlinkSock) return nil, err } + err = tap.setUp() + if err != nil { + unix.Close(tap.netlinkSock) + return nil, err + } return tap, nil }