0
votes

I have opened a raw socket to get all the raw packets:

socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)

When a packet is received on an interface which is member of a LAG or bond, the kernel is sending the packets to the user application 2 times. One for the actual physical interface and another one for the bond interface. How can I restrict the kernel to lift the packet only for the interface which I am interested?

We can achieve it by binding the application interested interface to the socket. But I don't want to create multiple sockets (one for each interface) to avoid scalability issue. Is it possible to bind multiple interface to the raw socket dynamically?

1

1 Answers

2
votes

Use SO_BINDTODEVICE socket option to bind to specific interface:

char *iface = "eth0";
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface, 4);

In this case you'll have to create separate socket for each interface.

Alternative solution would be to use bind(2) with INADDR_ANY.

You do not have a third option.