I am trying to use a port from two applications and each of them receives a packet from a different set of IP addresses. For this, I use the options SO_REUSEPORT and SO_ATTACH_REUSEPORT_CBPF. My code is as follows:
parentfd = socket(AF_INET, SOCK_STREAM, 0); if (parentfd < 0) error( "ERROR opening socket"); struct sock_filter code[]={ { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 3, 0x00000800 }, { 0x20, 0, 0, 0x0000001a }, { 0x15, 2, 0, 0xc0a8ff01 }, { 0x6, 0, 0, 0x00000000 }, { 0x6, 0, 0, 0x00040000 }, { 0x6, 0, 0, 0x00000001 }, }; struct sock_fprog bpf = { .len = ARRAY_SIZE(code), .filter = code, }; if (setsockopt(parentfd, SOL_SOCKET, SO_REUSEPORT, (const void *)&optval,sizeof(optval))) error("ERROR setting SO_REUSEPORT"); if (setsockopt(parentfd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, (const void *)&bpf, sizeof(bpf))) error("ERROR setting SO_ATTACH_REUSEPORT_CBPF);
I also have another process that listens on the same port using only the SO_REUSEPORT flag. On a machine with IP 192.168.255.1 I run echo 1234 | ncat 192.168.255.150 1234 echo 1234 | ncat 192.168.255.150 1234 . Based on my filter, I would expect all traffic from this IP address to be received by the second process. However, they are all accepted first. When I change the filter to simple:
struct sock_filter code[]={ { 0x6, 0, 0, 0x00000001 }, };
It works as expected, and all packets are accepted by the second process. Any idea why this might happen?