Skip to content

Commit

Permalink
No longer set SIOCGIFFLAGS, update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Kioubit committed Jan 7, 2022
1 parent ede44e8 commit a7eb52c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## Installing & Updating

1) Download the latest release from the releases page and move the binary to the ``/usr/local/bin/`` directory under the filename ``pndpd``.
1) Download the latest release from the [releases page](https://github.com/Kioubit/pndpd/releases) and move the binary to the ``/usr/local/bin/`` directory under the filename ``pndpd``.
2) Allow executing the file by running ``chmod +x /usr/local/bin/pndpd``
3) **For systemd users:** Install the service unit file
````
Expand Down
54 changes: 33 additions & 21 deletions pndp/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,46 @@ type iflags struct {
flags uint16
}

func setPromisc(fd int, iface string, enable bool) {
func setPromisc(fd int, iface string, enable bool, withInterfaceFlags 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)))
if ep != 0 {
panic(ep)
}

if enable {
ifl.flags |= uint16(syscall.IFF_PROMISC)
} else {
ifl.flags &^= uint16(syscall.IFF_PROMISC)
}
// -------------------------- Interface flags --------------------------
if withInterfaceFlags {
tFD, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_DGRAM, 0)
if err != nil {
panic(err)
}

_, _, ep = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if ep != 0 {
panic(ep)
var ifl iflags
copy(ifl.name[:], []byte(iface))
_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tFD), syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if ep != 0 {
panic(ep)
}

if enable {
ifl.flags |= uint16(syscall.IFF_PROMISC)
} else {
ifl.flags &^= uint16(syscall.IFF_PROMISC)
}

_, _, ep = syscall.Syscall(syscall.SYS_IOCTL, uintptr(tFD), syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifl)))
if ep != 0 {
panic(ep)
}

_ = syscall.Close(tFD)
}
// ---------------------------------------------------------------------

// Also set Sockopt to promisc
intf, err := net.InterfaceByName(iface)
// -------------------------- Socket Options ---------------------------
iFace, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
}

mreq := unix.PacketMreq{
Ifindex: int32(intf.Index),
mReq := unix.PacketMreq{
Ifindex: int32(iFace.Index),
Type: unix.PACKET_MR_PROMISC,
}

Expand All @@ -76,9 +88,9 @@ func setPromisc(fd int, iface string, enable bool) {
opt = unix.PACKET_DROP_MEMBERSHIP
}

err = unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, opt, &mreq)
err = unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, opt, &mReq)
if err != nil {
panic(err)
}

// ---------------------------------------------------------------------
}
4 changes: 2 additions & 2 deletions pndp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func listen(iface string, responder chan *ndpRequest, requestType ndpType, stopW
}
go func() {
<-stopChan
setPromisc(fd, iface, false)
setPromisc(fd, iface, false, false)
_ = syscall.Close(fd)
stopWG.Done() // syscall.read does not release when the file descriptor is closed
}()
Expand All @@ -51,7 +51,7 @@ func listen(iface string, responder chan *ndpRequest, requestType ndpType, stopW
panic(err.Error())
}

setPromisc(fd, iface, true)
setPromisc(fd, iface, true, false)

var protocolNo uint32
if requestType == ndp_SOL {
Expand Down

0 comments on commit a7eb52c

Please sign in to comment.