How to receive multicast data on a server with multiple domains other than the standard

I have a linux server with two network adapters (eth0 and eth1) and set eth0 to "ip route" by default. Now I would like to receive multicast packets on eth1. I added "224.0.20.0/24 dev eth1 proto static scope link" to the routing table, and I connect as follows:

sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); // port 12345, adress INADDR_ANY bind(sock, &bind_addr, sizeof(bind_addr)); // multicast address 224.0.20.100, interface address 10.13.0.7 (=eth1) setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imreq, sizeof(imreq)); 

According to ip maddr it connects to this group on the right interface, and tshark -i eth1 indicates that I am actually receiving multicast packets.

However, when I call recvfrom(sock) I do not receive any packets. If I set "ip route default" to eth1 (instead of eth0), I get packets through recvfrom. Is this a problem with my code or my network setup, and what is the correct way to do this?

(update) solution: caf hinted that this might be the same problem; really: after doing echo 0 > /proc/sys/net/ipv4/conf/eth1/rp_filter now I can receive multicast packets!

+7
source share
3 answers

caf comment that this is a duplicate of receiving multicast on a server with multiple interfaces (linux) answered this! (And I am posting this as an answer for clarity.) Namely, echo 0 > /proc/sys/net/ipv4/conf/eth1/rp_filter fixes my problem.

+2
source

Try adding a netmask and specifying 10.13.0.7 as a gateway in the routing table entry.

+2
source

That's right if you have two network adapters with a default gw value for only one of them.

Multicast uses unicast routes to determine the path to the source. This means that if the multicast path is different from the unicast path, then the multicast path will be completed. This is a loop prevention mechanism called RPF checking.

In this case, the NIC-bound application was forced to join IGMP, where when the unicast routes were retrieved from another network adapter with a standard gateway. Therefore, the verification failed. So no data.

You do not need to add any static routes. It should work only when rp_filter changes to 0.

+2
source

All Articles