TLDR: Is there a way to use "ip route" to add multicast routes for multiple NICs?
We have software that uses two multicast groups to communicate with two different groups of devices on two separate physical networks. With the exception of this application, devices on one network do not need to communicate across our device to communicate with devices on the other network.
To do this the software creates two sockets. Each one is bound to the one of the ip addresses of the separate NICS. That socket is then joined to the multicast group that exists on that network, eg socket 1 is bound to 192.168.0.2 and joined to multicast group 233.255.10.1 while socket 2 is bound to 10.57.31.2 and joined to multicast group 239.255.100.1.
We are currently using a bash script (Linux kernel 3.14.39) to set multicast routes on the two network interfaces using route, eg
route add -net 224.0.0.0 netmask 240.0.0.0 eth0
route add -net 224.0.0.0 netmask 240.0.0.0 eth1
and verified via route -n
Destination Gateway Genmask Flags Metric Ref Use Iface
224.0.0.0 0.0.0.0 240.0.0.0 U 0 0 0 eth0
224.0.0.0 0.0.0.0 240.0.0.0 U 0 0 0 eth1
I recently read that route was deprecated/obsolete and that we should be using ip route instead, eg
ip route add 224.0.0.0/4 dev eth0
ip route add 224.0.0.0/4 dev eth1
Unfortunately, the second call fails with "RTNETLINK answers: File exists" and of course the second route doesn't show up after these calls.
Is there a way to use ip route to add multicast routes to multiple NICs?
I can use /8 as a netmask? eg
ip route add 233.0.0.0/8 dev eth0
and
ip route add 239.0.0.0/8 dev eth1
but this is problematic as the script that does this is not aware of what multicast address is associated with which device and its not always guaranteed to be the same depending on system configuration. Using my first example of route add makes this a non issue.
UPDATES Thanks to an extended discussion with @Ron Maupin, I realized the error was in our code. We were not setting the interface to use for multicasting with IP_MULTICAST_IF. Once I added the setsockopt call to set IP_MULTICAST_IF, I no longer needed to add the routing tables.
struct in_addr multicastInterface = {};
multicastInterface.s_addr = interfaceAddressNetworkOrder;
// Set which outgoing interface to use
int result = setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_IF, (char*)&multicastInterface, sizeof(struct in_addr));