Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set afpacket socket in promiscuous mode #564

Open
dawei73 opened this issue Nov 20, 2018 · 2 comments · May be fixed by #1166
Open

Set afpacket socket in promiscuous mode #564

dawei73 opened this issue Nov 20, 2018 · 2 comments · May be fixed by #1166

Comments

@dawei73
Copy link

dawei73 commented Nov 20, 2018

It would be nice to be able to set the socket in promiscuous mode.

perhaps something like this:

func (h *TPacket) SetPromiscMode(ifaceName string) error {
	ifIndex := 0
	// An empty string here means to listen to all interfaces
	if ifaceName != "" {
		iface, err := net.InterfaceByName(ifaceName)
		if err != nil {
			return fmt.Errorf("InterfaceByName: %v", err)
		}
		ifIndex = iface.Index
	}
	var sp C.struct_packet_mreq
	sp.mr_ifindex = C.int(ifIndex)
	sp.mr_type = unix.PACKET_MR_PROMISC
	sp.mr_alen = 0
	sp.mr_address = [8]C.uchar{0,0,0,0,0,0,0,0}
	if err := setsockopt(h.fd, unix.SOL_PACKET, unix.PACKET_ADD_MEMBERSHIP, unsafe.Pointer(&sp), unsafe.Sizeof(sp)); err != nil {
		return fmt.Errorf("setsockopt promisc mode: %v", err)
  }
	return nil
}
@norg
Copy link

norg commented Feb 11, 2020

This would be very helpful as it could help in some scenarios where we don't see any packets unless it's set to promiscuous mode. Is there any plan to include this?

Current workaround is ip link set $INTERFACE promisc on.

@maxatome
Copy link

maxatome commented Apr 26, 2024

Using reflect, so very dirty :)

func setPromiscuous(tp *afpacket.TPacket, ifidx int, on bool) error {
	mreq := unix.PacketMreq{
		Ifindex: int32(ifidx),
		Type:    unix.PACKET_MR_PROMISC,
	}
	opt := unix.PACKET_DROP_MEMBERSHIP
	if on {
		opt = unix.PACKET_ADD_MEMBERSHIP
	}
	fd := int(reflect.ValueOf(tp).Elem().FieldByName("fd").Int())
	return unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, opt, &mreq)
}

ifidx is the interface index.

maxatome added a commit to maxatome/gopacket that referenced this issue Apr 26, 2024
@maxatome maxatome linked a pull request Apr 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants