From ede44e84262a4e56f4cedd77a9d678254b60b7fe Mon Sep 17 00:00:00 2001 From: Kioubit Date: Tue, 4 Jan 2022 17:37:52 -0500 Subject: [PATCH] Set PACKET_ADD_MEMBERSHIP --- pndp/interface.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pndp/interface.go b/pndp/interface.go index 46b9bb4..fc1b0f9 100644 --- a/pndp/interface.go +++ b/pndp/interface.go @@ -3,6 +3,7 @@ package pndp import ( "golang.org/x/net/bpf" "golang.org/x/sys/unix" + "net" "syscall" "unsafe" ) @@ -38,6 +39,7 @@ type iflags struct { } func setPromisc(fd int, iface string, enable bool) { + //TODO re-test ALLMULTI var ifl iflags copy(ifl.name[:], []byte(iface)) _, _, ep := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl))) @@ -55,4 +57,28 @@ func setPromisc(fd int, iface string, enable bool) { if ep != 0 { panic(ep) } + + // Also set Sockopt to promisc + intf, err := net.InterfaceByName(iface) + if err != nil { + panic(err.Error()) + } + + mreq := unix.PacketMreq{ + Ifindex: int32(intf.Index), + Type: unix.PACKET_MR_PROMISC, + } + + var opt int + if enable { + opt = unix.PACKET_ADD_MEMBERSHIP + } else { + opt = unix.PACKET_DROP_MEMBERSHIP + } + + err = unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, opt, &mreq) + if err != nil { + panic(err) + } + }