LinuxTap, bugfix

This commit is contained in:
KusakabeSi 2021-08-24 13:26:20 +00:00
parent d717d35f64
commit 6b563b5ad5
2 changed files with 48 additions and 4 deletions

View File

@ -176,6 +176,8 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) {
thetap, err = tap.CreateUDPSockTAP(tconfig.Interface, lis, sen) thetap, err = tap.CreateUDPSockTAP(tconfig.Interface, lis, sen)
case "vpp": case "vpp":
thetap, err = tap.CreateVppTAP(tconfig.Interface, tconfig.LogLevel.LogLevel) thetap, err = tap.CreateVppTAP(tconfig.Interface, tconfig.LogLevel.LogLevel)
case "tap":
thetap, err = tap.CreateTAP(tconfig.Interface)
default: default:
return errors.New("Unknow interface type:" + tconfig.Interface.Itype) return errors.New("Unknow interface type:" + tconfig.Interface.Itype)
} }

View File

@ -212,16 +212,53 @@ func (tap *NativeTap) setMacAddr(mac MacAddress) (err error) {
ifreq = struct.pack('16sH6B8x', self.name, AF_UNIX, *macbytes) ifreq = struct.pack('16sH6B8x', self.name, AF_UNIX, *macbytes)
fcntl.ioctl(sockfd, SIOCSIFHWADDR, ifreq) fcntl.ioctl(sockfd, SIOCSIFHWADDR, ifreq)
*/ */
copy(ifr[:16], name) copy(ifr[:unix.IFNAMSIZ], name)
binary.BigEndian.PutUint16(ifr[16:18], unix.AF_UNIX) binary.LittleEndian.PutUint16(ifr[unix.IFNAMSIZ:unix.IFNAMSIZ+2], unix.AF_UNIX)
copy(ifr[18:24], mac[:]) copy(ifr[unix.IFNAMSIZ+2:unix.IFNAMSIZ+8], mac[:])
copy(ifr[24:32], make([]byte, 8))
_, _, err = unix.Syscall( _, _, err = unix.Syscall(
unix.SYS_IOCTL, unix.SYS_IOCTL,
uintptr(fd), uintptr(fd),
uintptr(unix.SIOCSIFHWADDR), uintptr(unix.SIOCSIFHWADDR),
uintptr(unsafe.Pointer(&ifr[0])), 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 return
} }
@ -506,6 +543,11 @@ func CreateTAPFromFile(file *os.File, iconfig config.InterfaceConf) (Device, err
unix.Close(tap.netlinkSock) unix.Close(tap.netlinkSock)
return nil, err return nil, err
} }
err = tap.setUp()
if err != nil {
unix.Close(tap.netlinkSock)
return nil, err
}
return tap, nil return tap, nil
} }