| 1 |
|
| 2 |
<html><head> |
| 3 |
<title>IP Multicasting with Socat</title> |
| 4 |
<link rel="stylesheet" type="text/css" href="dest-unreach.css"> |
| 5 |
</head> |
| 6 |
|
| 7 |
<body> |
| 8 |
|
| 9 |
<h1>IP Multicasting with Socat</h1> |
| 10 |
|
| 11 |
<h2>Introduction</h2> |
| 12 |
<p> |
| 13 |
Multicasting (and broadcasting which is also discussed in this article) |
| 14 |
provides a means to direct a single packet to more than one host. Special |
| 15 |
addresses are defined for this purpose and are handled specially by network |
| 16 |
adapters, networking hardware, and IP stacks. |
| 17 |
</p> |
| 18 |
<p> |
| 19 |
IPv4 specifications provide broadcasting and multicasting; IPv6 provides |
| 20 |
multicasting but replaces broadcasting by special multicast modes. UNIX domain |
| 21 |
sockets do not know broadcasting or multicasting. |
| 22 |
</p> |
| 23 |
<p> |
| 24 |
The following examples use UDP/IPv4 only. However, they can easily be |
| 25 |
adapted for raw IPv4 sockets. IPv6 multicasting has not yet been successfully |
| 26 |
used with socat; please contact the author if you have positive experiences or |
| 27 |
ideas that go beyond <tt>IPV6_ADD_MEMBERSHIP</tt>. |
| 28 |
</p> |
| 29 |
<p> |
| 30 |
All multicast examples presented in this document use multicast address |
| 31 |
224.1.0.1; it can be replaced by any valid IPv4 multicast address (except |
| 32 |
<a href="#ALLSYSTEMS">all-systems</a>). |
| 33 |
</p> |
| 34 |
<p> |
| 35 |
We assume a local network with address 192.168.10.0 and mask 255.255.255.0; an |
| 36 |
eventual "client" has 192.168.10.1, example "server" and example peer have |
| 37 |
192.168.10.2 in all examples. Change these addresses and mask to your own |
| 38 |
requirements. |
| 39 |
</p> |
| 40 |
<p> |
| 41 |
All the following examples work bidirectionally except when otherwise noticed. |
| 42 |
For "clients" we just use <tt>STDIO</tt>, and for "servers" we use <tt>EXEC:hostname</tt> which |
| 43 |
ignores its input but shows us which host the reply comes from. Replace these |
| 44 |
socat addresses with what is appropriate for your needs (e.g. shell script |
| 45 |
invocations). Port 6666 can be replaced with any other port (but for ports < |
| 46 |
1024 root privilege might be required). |
| 47 |
</p> |
| 48 |
<p> |
| 49 |
Different kinds of broadcast addresses exist: 255.255.255.255 is local network |
| 50 |
only; for the IPv4 network 192.168.10.0/24 the "official" broadcast address |
| 51 |
is 192.168.10.255; the network address 192.168.10.0 is also interpreted as |
| 52 |
broadcast by some hosts. The two latter forms are routed by gateways. In the |
| 53 |
following examples we only use broadcast address 192.168.10.255. |
| 54 |
</p> |
| 55 |
|
| 56 |
<h2>Example 1: Multicast client and servers</h2> |
| 57 |
|
| 58 |
<p>This example builds something like a "supervisor" or "client" that |
| 59 |
communicates with a set of "servers". The supervisor may send packets to the |
| 60 |
multicast address, and the servers may send response packets. Note that the |
| 61 |
servers would also respond to other clients' requests.</p> |
| 62 |
|
| 63 |
<p>Multicast server:</p> |
| 64 |
<div class="shell">socat \ |
| 65 |
UDP4-RECVFROM:6666,ip-add-membership=224.1.0.1:192.168.10.2,fork \ |
| 66 |
EXEC:hostname |
| 67 |
</div> |
| 68 |
<p> |
| 69 |
This command receives multicast packets addressed to 224.1.0.1 and forks a |
| 70 |
child process for each. The child processes may each send one or more reply |
| 71 |
packets back to the particular sender. 192.168.10.2 means the address of the |
| 72 |
interface where multicasts should be received. |
| 73 |
Run this command on a number of hosts, and they will all respond in |
| 74 |
parallel.</p> |
| 75 |
|
| 76 |
<p>Multicast client:</p> |
| 77 |
|
| 78 |
<span class="shell">socat \ |
| 79 |
STDIO \ |
| 80 |
UDP4-DATAGRAM:224.1.0.1:6666,range=192.168.10.0/24 |
| 81 |
</span> |
| 82 |
<p> |
| 83 |
This process transfers data from stdin to the multicast address, and transfers |
| 84 |
packets received from the local network to stdout. It does not matter in which |
| 85 |
direction the first data is passed. |
| 86 |
A packet from the network is accepted by the IP stack for our socket if: |
| 87 |
<ul> |
| 88 |
<li>it is an incoming UDP/IPv4 packet</li> |
| 89 |
<li>its target port matches the local port assigned to the socket (random)</li> |
| 90 |
<li>its target address matches one of the hosts local addresses or the any-host |
| 91 |
multicast address</li> |
| 92 |
</ul> |
| 93 |
Of these packets, socat handles only those matching the following criteria: |
| 94 |
<ul> |
| 95 |
<li>the source address is within the given range</li> |
| 96 |
<li>the source port is 6666</li> |
| 97 |
</ul> |
| 98 |
</p> |
| 99 |
|
| 100 |
|
| 101 |
<h2>Example 2: Broadcast client and servers</h2> |
| 102 |
|
| 103 |
<p>Broadcast server:</p> |
| 104 |
|
| 105 |
<span class="shell">socat \ |
| 106 |
UDP4-RECVFROM:6666,broadcast,fork \ |
| 107 |
EXEC:hostname |
| 108 |
</span> |
| 109 |
<p> |
| 110 |
This command receives packets addressed to a local broadcast address and forks |
| 111 |
a child process for each. The child processes may each send one or more reply |
| 112 |
packets back to the particular sender. |
| 113 |
Run this command on a number of hosts, and they will all respond in |
| 114 |
parallel.</p> |
| 115 |
|
| 116 |
<p>Broadcast client:</p> |
| 117 |
|
| 118 |
<span class="shell">socat \ |
| 119 |
STDIO \ |
| 120 |
UDP4-DATAGRAM:192.168.10.255:6666,broadcast,range=192.168.10.0/24 |
| 121 |
</span> |
| 122 |
<p> |
| 123 |
This process transfers data from stdin to the broadcast address, and transfers |
| 124 |
packets received from the local network to stdout. It does not matter in which |
| 125 |
direction the first data is passed. |
| 126 |
A packet from the network is accepted by the IP stack for our socket if: |
| 127 |
<ul> |
| 128 |
<li>it is an incoming UDP/IPv4 packet</li> |
| 129 |
<li>its target port matches the local port assigned to the socket (6666)</li> |
| 130 |
<li>its target address matches one of the hosts local addresses or the any-host |
| 131 |
multicast address, or a local broadcast address</li> |
| 132 |
</ul> |
| 133 |
Of these packets, socat handles only those matching the following criteria: |
| 134 |
<ul> |
| 135 |
<li>the source address is within the given range</li> |
| 136 |
<li>the source port is 6666</li> |
| 137 |
</ul> |
| 138 |
</p> |
| 139 |
<p>The <tt>broadcast</tt> option is only required for sending or receiving |
| 140 |
local broadcasts.</p> |
| 141 |
|
| 142 |
<h2>Example 3: Multicast peers</h2> |
| 143 |
|
| 144 |
<p>It is possible to combine multicast sender and receiver in one socat |
| 145 |
address. This allows to start processes on different hosts on the local network |
| 146 |
that will communicate symmetrically, so each process can send messages that are |
| 147 |
received by all the other ones.</p> |
| 148 |
|
| 149 |
<span class="shell">socat \ |
| 150 |
STDIO \ |
| 151 |
UDP4-DATAGRAM:224.1.0.1:6666,bind=:6666,range=192.168.10.0/24,ip-add-membership=224.1.0.1:192.168.10.2 |
| 152 |
</span> |
| 153 |
<p> |
| 154 |
This command is valid for host 192.168.10.2; adapt this address to the |
| 155 |
particular interface addresses of the hosts. |
| 156 |
</p> |
| 157 |
<p> |
| 158 |
Starting this process opens a socket on port 6666 that will receive packets |
| 159 |
directed to multicast address 224.1.0.1. Only packets with matching source |
| 160 |
address and source port 6666 will be handled though. When this process sends |
| 161 |
data to the network the packets will be addressed to 224.1.0.1:6666 and have a |
| 162 |
source address of 192.168.10.2:6666, matching the accept criteria of the peers |
| 163 |
on the local network. |
| 164 |
</p> |
| 165 |
|
| 166 |
<p>Note: this command receives the packets it just has sent; add option |
| 167 |
<tt>ip-multicast-loop=0</tt> if this in undesired.</p> |
| 168 |
|
| 169 |
<h2>Example 4: Broadcast peers</h2> |
| 170 |
|
| 171 |
<p>Just as with multicast, it is possible to combine broadcast sender and |
| 172 |
receiver in one socat address.</p> |
| 173 |
|
| 174 |
<span class="shell">socat \ |
| 175 |
STDIO \ |
| 176 |
UDP4-DATAGRAM:255.255.255.255:6666,bind=:6666,range=192.168.10.0/24,broadcast |
| 177 |
</span> |
| 178 |
<p> |
| 179 |
Starting this process opens a socket on port 6666 that will receive packets |
| 180 |
directed to a local broadcast addresses. Only packets with matching source |
| 181 |
address and source port 6666 will be handled though. When this process sends |
| 182 |
data to the network the packets will be addressed to 255.255.255.255:6666 and |
| 183 |
have a source address of 192.168.10.2:6666, matching the accept criteria of |
| 184 |
the peers on the local network. |
| 185 |
</p> |
| 186 |
|
| 187 |
<p>Note: this command receives the packets it just has sent; there does not |
| 188 |
seem to exist a simple way to prevent this.</p> |
| 189 |
|
| 190 |
|
| 191 |
<h2>Troubleshooting</h2> |
| 192 |
|
| 193 |
<p> |
| 194 |
If you do not get an error message during operation, but the packets do not |
| 195 |
reach the target processes, use <tt>tcpdump</tt> to see if the packets have the |
| 196 |
correct source and destination addresses and ports, and if they leave and enter |
| 197 |
the hosts as expected. |
| 198 |
</p> |
| 199 |
<p> |
| 200 |
The following subsections discuss some typical sources of trouble. |
| 201 |
</p> |
| 202 |
|
| 203 |
<h3>IP filters</h3> |
| 204 |
<p> |
| 205 |
If you do not succeed in receiving multicast or broadcast packets, check if |
| 206 |
iptables are activated on the receiving or sending host. They might be |
| 207 |
configured to disallow this traffic. |
| 208 |
</p> |
| 209 |
|
| 210 |
<h3>Do not bind()</h3> |
| 211 |
<p> |
| 212 |
When using multicast communications, you should not bind the sockets to a |
| 213 |
specific IP address. It seems that the (Linux) IP stack compares the |
| 214 |
destination address with the bind address, not taking care of the multicast |
| 215 |
property of the incoming packet. |
| 216 |
</p> |
| 217 |
|
| 218 |
<h3>Routing</h3> |
| 219 |
<p> |
| 220 |
When you receive an error like:</p> |
| 221 |
<span class="error"> |
| 222 |
... E sendto(3, 0x80c2e44, 4, 0, AF=2 224.1.0.1:6666, 16): Network is unreachable |
| 223 |
</span> |
| 224 |
<p>you have a routing problem. The (Linux) IP stack seems to handle multicast |
| 225 |
addresses just like unicast addresses when determining their route (interface |
| 226 |
and gateway), i.e. the routing table needs an entry that somehow matches the |
| 227 |
target address. </p> |
| 228 |
<p> |
| 229 |
For the same reason, multicast packets will probably leave your host on the |
| 230 |
interface with the default route if it is specified.</p> |
| 231 |
<p> |
| 232 |
Set a multicast/broadcast route with the following command (Linux):</p> |
| 233 |
<span class="shell">route add -net 224.0.0.0/3 gw 192.168.10.2 |
| 234 |
</span> |
| 235 |
|
| 236 |
<a name="ALLSYSTEMS"></a> |
| 237 |
<h3>ALL-SYSTEMS multicast address</h3> |
| 238 |
<p> |
| 239 |
<tt>224.0.0.1</tt> is the all-systems multicast address: all |
| 240 |
datagram sockets appear to be automatically member of this group on all |
| 241 |
interfaces. This membership cannot be dropped on Linux (you need iptables to |
| 242 |
filter packets). |
| 243 |
</p> |
| 244 |
|
| 245 |
|
| 246 |
<h2>(In)Security</h2> |
| 247 |
|
| 248 |
<p>When you use the above examples you should understand that all datagram |
| 249 |
sockets without exception accept all packets that are directly addressed to |
| 250 |
them; |
| 251 |
the multi- and broadcast receiving features are just extensions to this |
| 252 |
functionality. socat currently has no means to handle incoming packets |
| 253 |
differently whether they are addressed to unicast, multicast, or broadcast |
| 254 |
addresses. However, for EXEC'd scripts socat can provide this info in |
| 255 |
environment variables. |
| 256 |
</p> |
| 257 |
|
| 258 |
<p>Authentication or encryption are not available.</p> |
| 259 |
|
| 260 |
<p>It is very easy to fake the source address of UDP (or raw IP) packets. You |
| 261 |
should understand whether your network is protected from address spoofing |
| 262 |
attacks.</p> |
| 263 |
|
| 264 |
<p>Broadcast and multicast traffic can trivially be received by <em>any</em> |
| 265 |
host on the local network.</p> |
| 266 |
|
| 267 |
|
| 268 |
<h2>History</h2> |
| 269 |
|
| 270 |
Starting with version 1.5.0, socat provides a set of address types that |
| 271 |
allow various operations on datagram oriented sockets: |
| 272 |
<dl> |
| 273 |
<dt>SENDTO</dt><dd>send packets to a remote socket and receive packet from this |
| 274 |
remote socket only</dd> |
| 275 |
<dt>RECV</dt><dd>receive all packets that arrive on the local socket, but do |
| 276 |
not reply</dd> |
| 277 |
<dt>RECVFROM</dt><dd>receive all packets that arrive on the local socket, and |
| 278 |
reply using child processes</dd> |
| 279 |
</dl> |
| 280 |
|
| 281 |
<p> |
| 282 |
These modes already enable several different client/server oriented operations. |
| 283 |
Moreover, the SENDTO addresses can send to multicast and broadcast addresses |
| 284 |
(the latter requires the <tt>broadcast</tt> option though). RECV and RECVFROM |
| 285 |
also would accept packets addressed to a local broadcast address (with option |
| 286 |
<tt>broadcast</tt>) or the all-systems multicast address. |
| 287 |
</p> |
| 288 |
|
| 289 |
<p> |
| 290 |
These address types had, however, two major caveats: |
| 291 |
<ul> |
| 292 |
<li>Missing control of multicast group membership in the RECV and RECVFROM |
| 293 |
addresses</li> |
| 294 |
<li>The SENDTO address would never accept a reply to a broadcast or multicast |
| 295 |
addressed packet because the source address of incoming replies would not match |
| 296 |
the target address of the sent packet. |
| 297 |
</ul> |
| 298 |
</p> |
| 299 |
|
| 300 |
<h3>New Features in socat 1.6.0</h3> |
| 301 |
|
| 302 |
<p> |
| 303 |
socat version 1.6.0 addresses these problems and provides a new more generic |
| 304 |
datagram address type (*-DATAGRAM) and the new address option IP-ADD-MEMBERSHIP. |
| 305 |
</p> |
| 306 |
|
| 307 |
<p> |
| 308 |
Please note that the new features could not be successfully tested on IPv6; |
| 309 |
these sections thus apply to IPv4 only. |
| 310 |
</p> |
| 311 |
|
| 312 |
<h3>New Features in socat 1.7.0</h3> |
| 313 |
|
| 314 |
<p> |
| 315 |
socat version 1.7.0 helps to find more information about incoming packets in |
| 316 |
environment variables that can be used in scripts or programs invoked by |
| 317 |
socat. The option <tt>ip-pktinfo</tt> (on non-BSD systems) |
| 318 |
or <tt>ip-recvdstaddr</tt> (on BSD systems) is required to get basic |
| 319 |
information about incoming packets. |
| 320 |
</p> |
| 321 |
|
| 322 |
<p> |
| 323 |
Example: Start a receiver of the following form (tried on Linux): |
| 324 |
</p> |
| 325 |
|
| 326 |
<span class="shell">socat -u \ |
| 327 |
UDP-RECVFROM:8888,reuseaddr,ip-add-membership=224.1.0.1:192.168.10.2,ip-pktinfo,fork \ |
| 328 |
SYSTEM:export |
| 329 |
</span> |
| 330 |
|
| 331 |
<p> |
| 332 |
Then send a multicast packet from the client: |
| 333 |
</p> |
| 334 |
|
| 335 |
<span class="shell">echo | \ |
| 336 |
socat -u \ |
| 337 |
STDIO \ |
| 338 |
UDP-DATAGRAM:224.1.0.1:8888 |
| 339 |
</span> |
| 340 |
|
| 341 |
<p> |
| 342 |
On the server the following text should appear (only interesting lines shown): |
| 343 |
</p> |
| 344 |
|
| 345 |
<pre> |
| 346 |
export SOCAT_IP_DSTADDR="224.1.0.1" |
| 347 |
export SOCAT_IP_IF="eth0" |
| 348 |
export SOCAT_IP_LOCADDR="192.168.10.2" |
| 349 |
export SOCAT_PEERADDR="192.168.10.1" |
| 350 |
export SOCAT_PEERPORT="41159" |
| 351 |
</pre> |
| 352 |
|
| 353 |
<p> |
| 354 |
<tt>SOCAT_IP_IF</tt> shows the interface where the packet entered the server; |
| 355 |
<tt>SOCAT_IP_LOCADDR</tt> shows the IP address of this interface; |
| 356 |
<tt>SOCAT_IP_DSTADDR</tt> shows the target address of the packet; |
| 357 |
<tt>SOCAT_PEERADDR</tt> and <tt>SOCAT_PEERPORT</tt> are the client socket |
| 358 |
values. |
| 359 |
</p> |
| 360 |
|
| 361 |
<h2>More info about socat datagrams</h2> |
| 362 |
|
| 363 |
<h3>Links regarding this tutorial</h3> |
| 364 |
<a href="socat.html#ADDRESS_UDP4_DATAGRAM">address UDP4-DATAGRAM</a><br> |
| 365 |
<a href="socat.html#ADDRESS_UDP4_RECVFROM">address UDP4-RECVFROM</a><br> |
| 366 |
<a href="socat.html#OPTION_RANGE">option range</a><br> |
| 367 |
<a href="socat.html#OPTION_SO_BROADCAST">option broadcast</a><br> |
| 368 |
<a href="socat.html#OPTION_IP_ADD_MEMBERSHIP">option ip-add-membership</a><br> |
| 369 |
<a href="socat.html#OPTION_FORK">option fork</a><br> |
| 370 |
<a href="socat.html#OPTION_BIND">option bind</a><br> |
| 371 |
|
| 372 |
<h3>Other datagram addresses</h3> |
| 373 |
<a href="socat.html#ADDRESS_UDP4_RECV">address UDP4-RECV</a>: pure datagram receiver<br> |
| 374 |
<a href="socat.html#ADDRESS_UDP4_SENDTO">address UDP4-SENDTO</a>: communicate |
| 375 |
with one peer address<br> |
| 376 |
<a href="socat.html#ADDRESS_UDP4_LISTEN">address UDP4-LISTEN</a>: pseudo stream server<br> |
| 377 |
<a href="socat.html#ADDRESS_UDP4_CONNECT">address UDP4-CONNECT</a>: pseudo stream client<br> |
| 378 |
|
| 379 |
<h3>Related socat option groups</h3> |
| 380 |
<a href="socat.html#GROUP_IP">IP options</a><br> |
| 381 |
<a href="socat.html#GROUP_SOCKET">socket options</a><br> |
| 382 |
<a href="socat.html#GROUP_FD">file descriptor options</a><br> |
| 383 |
<a href="socat.html#GROUP_RANGE">range options</a><br> |
| 384 |
<a href="socat.html#GROUP_CHILD">child process options</a><br> |
| 385 |
|
| 386 |
|
| 387 |
<h2>References</h2> |
| 388 |
<a href="http://www.dest-unreach.org/socat">socat home page</a><br> |
| 389 |
<a href="socat.html">socat man page</a><br> |
| 390 |
<a href="http://en.wikipedia.org/wiki/Multicast">multicasting on Wikipedia</a><br> |
| 391 |
<a href="http://en.wikipedia.org/wiki/Broadcast_address">broadcasting on Wikipedia</a><br> |
| 392 |
|
| 393 |
<p> |
| 394 |
<small>This document was last modified in May 2009.</small><br> |
| 395 |
<small>Copyright: Gerhard Rieger 2007-2009</small><br> |
| 396 |
<small>License: <a href="http://www.fsf.org/licensing/licenses/fdl.html">GNU Free Documentation License (FDL)</a></small> |
| 397 |
</p> |
| 398 |
|
| 399 |
</body> |
| 400 |
</html> |