0

I am writing a program for Linux that should receive UDP broadcast packets from the specified port on any of the network interfaces that exist in the system.

However, if the system has multiple network interfaces of the same subnet address, the packets are accepted only by the first interface.

For example, if eth0 has 192.168.225.107 and eth1 has 192.168.225.108, packets accepted only from eth0.

NIC's are attached to different physical networks. According to the tcpdump, the packets are present in both networks.

Code (error checking skipped):

sock = socket(PF_INET, SOCK_DGRAM, 0);
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &on, sizeof(on));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *) &s, sizeof(s));
bzero(&serv_addr_ip, sizeof(serv_addr_ip));
serv_addr_ip.sin_family = AF_INET;
serv_addr_ip.sin_port = htons(port);
serv_addr_ip.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr = (struct sockaddr *) &serv_addr_ip;
addr_len = sizeof(struct sockaddr_in);
bind(sock, serv_addr, addr_len);

while (1) {
    if ((chars = recvfrom(sock, var.buf, MSG_MAX, 0, serv_addr, &addr_len)) < 0) {
        ...
    }
...
}
6
  • 1
    If eth0 and eth1 are in the same subnet, doesn't that mean that you would get each broadcast packet twice? Might that be the reason why it is filtered out and only delivered to one adapter?
    – ypnos
    Aug 27, 2014 at 12:54
  • Here is a small trick. NIC addresses belong to the same subnet, but they are physically connected to different networks, which has different packets. Aug 27, 2014 at 12:59
  • Did you try having eth0 configured for a different subnet? Did you try if eth1 works when eth0 is down?
    – ypnos
    Aug 27, 2014 at 13:31
  • You can't bind to several NICs. You can bind to INADDR_ANY, as you are doing. So what's your question?
    – user207421
    Aug 27, 2014 at 20:17
  • Yes, then I down eth0, packets from eth1 is OK. My question is: why, although I use the INADDR_ANY, I see packets only from the address, which is assigned to the first network card. Aug 29, 2014 at 8:59

1 Answer 1

1

If I understand you correctly, you have two NICs, connected to two physical networks (i.e. network cables, hubs), with each having a separate IP address from the same subnet address range?

The short answer is that your network configuration is wrong. If they really are separate physical networks, then they should have different subnet addresses. It depends on what you mean with separate physical networks, separate hardware? You can NOT have two separate subnets with the same subnet address. Thats why I am saying your network configuration is wrong.

However, The impression I get is that you are trying to bridge the two networks, so that the two NICs belong to the same subnet (not separate). Well, then you should bridge them. You bridge the two NICs together and assign ONE IP address to the bridge. Then you will be able to receive your packets on both NICs.

In linux:

brctl addbr br0
ifconfig eth0 0.0.0.0 down
ifconfig eth1 0.0.0.0 down
brctl addif br0 eth0
brctl addif br0 eth1
ifconfig eth0 up
ifconfig eth1 up
ifconfig br0 up
ifconfig br0 192.168.225.107 (or 192.168.225.108, whatever you prefer)
1
  • Thanks, that's a great idea. It works on the test machine, but unfortunately I can't change the network configuration on the target platform. Aug 29, 2014 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.