Linux - How to Join Multicast Group
Joining a multicast group from Linux is easy ( sort of ). I’ve done minimal testing so use this information at your own risk.
To join a multicast group on Linux, just start listening on a UDP port using a multicast IP address like this:
mreceive -g 224.2.2.2 -p 2222
For more details about how to use this tool, keep reading.
Enabling / Disabling on an Interface
Assuming your interface is named ‘eno2’ you can use the following.
Check if multicast is enabled on an interface. If it is enabled you will see the word “MULTICAST” otherwise you won’t.
ifconfig eth0 | grep -i multi
Enable multicast on an interface:
ifconfig eno2 multicast
Disable multicast on an interface:
ifconfig eno2 -multicast
IP Ranges and Groups
Ranges:
224.0.0.0 – 239.255.255.255 | Class D addresses are used for multicast |
224.0.0.0 - 224.0.0.255 | Reserved for local use, never forwarded |
239.0.0.0 - 239.255.255.255 | Reserved for administrative scoping |
Groups:
224.0.0.1 | all hosts group |
224.0.0.2 | all multicast routers group |
224.0.0.4 | all DVMRP routers |
224.0.0.5 | all OSPF routers |
224.0.013 | all PIM routers |
- 224.0.0.1 all hosts join this at start up if they are multicast capable.
To find all hosts with multicast enabled, run:
ping 224.0.0.1
Setting Up Multicast on Linux
I believe this will give you a multicast router. You may need to enable routing in the kernel. I haven’t tested this.
Enable multicast on the interface like this:
ifconfig eno2 multicast
Add a route for a class D network:
route add -net 224.0.0.0/8 dev eno2
Sniff multicast traffic on this interface. If you see any packets it is working:
tcpdump -i eno2 ip multicast
The following will show multicast addresses:
ip maddress show
Testing
There is a nice tool for testing multicast. You can find it on GitHub here:
You can pull down the zip file here:
-
Unzip it, enter the directory, and run “make” to build it.
-
Once built it will give you the “msend” and “mreceive” tools that can be run directly from that directory.
-
Open two terminals.
On the first terminal, run the following command. This will start listening on port 2222. It also joins the multicast group 224.2.2.2.
./mreceive -g 224.2.2.2 -p 2222
On the second terminal, run the following command:
ip maddress show | grep 224.2.2.2
It should show the IP of 224.2.2.2 indicating that it has joined that group.
Now start sending packets from the second terminal like this:
./msend -g 224.2.2.2 -p 2222 -t 8 -text "test"
You should see these coming through and being output on the first terminal.
- Type [ctrl] - c on both terminals to stop.
Once you’ve done this you can verify that it is no longer a member of the group like this:
ip maddress show | grep 224.2.2.2
You should see that it now displays nothing indicating that it has left the group. As soon as the receive command stops, it will leave the group.
More
It is worth noting that ssmping is a tool that some people use to test multicast networks. I don’t have a ton of background with this tool but some people find it useful.