8

when I created two udp sockets and I bind them to INADDR_ANY and same port number. but one of them joined a multicast group. but both of them can receive data from the same multicast group, even one of sockets didn't join to the multicast group.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    @Michael read the [socket man page](http://www.kernel.org/doc/man-pages/online/pages/man7/socket.7.html) and search for SO_REUSEADDR – hek2mgl Dec 16 '12 at 16:18
  • @Micheal when using SO_REUSEADDR with UDP it allows to bind multiple socket to the same address and port. [read](http://developerweb.net/viewtopic.php?id=3908). But after reading this thread I would say it is more a side behaviour. It would have been better to point you to `SO_REUSEPORT` – hek2mgl Dec 16 '12 at 18:20
  • @Michael If you don't know what SO_REUSEADDR does, you shouldn't really be posting on the topic of port reuse at all. TIME_WAIT is a TCP state and this is a UDP question. The citation you linked to is hardly an authoritative source. – user207421 Dec 17 '12 at 11:45

2 Answers2

4

The linux kernel doesn't track the state of IGMP joins. IGMP is a router protocol. Sending an IGMP join to a multicast group just tells the router that it should forward packets to a given address and port. Note that routers must be capable of talking IGMP.

This means although you used setsockopt() to join the multicast group, the membership isn't tracked per socket by the kernel as you may expect. The kernel just sends an IGMP join packet to the router. You can verify this using wireshark or whatever.

As the kernel doesn't track the IGMP state of a socket, incoming traffic on that address and port is just 'regular' traffic for the kernel.

So if you bound both sockets to the same address and port and then have sent an IGMP join using that address and port, it is the expected behaviour that packets will be available on both sockets.

BTW: Why do you need two sockets bound to the same address and port?

UPDATE: Following the explanation of @Ambroz Bizjak (thanks) it isn't true that the Linux kernel doesn't track the state of IGMP joins. It does. But it don't uses this information to decide which packets should be forwarded to which socket, if multiple sockets are bound to the same address and port.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 3
    This is incorrect. The kernel does track the state of IGMP joins, per-socket. It has to, so it can properly implement the IGMP protocol (e.g. responding to IGMP General Queries and IGMP Group-Specific Queries). However it *doesn't* use this information to filter incoming packets. – Ambroz Bizjak Dec 17 '12 at 10:11
  • 2
    And why does it need to keep track *per-socket* you might ask. So that when the last socket joined to a specific group is closed, it can send an IGMP Leave Group message, and cease replying to queries for that group. – Ambroz Bizjak Dec 17 '12 at 10:13
  • @Ambroz Bizjak Oops. I didn't know about that. You should answer the question. – hek2mgl Dec 17 '12 at 12:46
  • 1
    @AmbrozBizjak That's all very well but the evidence here is that it doesn't use that information when delivering multicasts. – user207421 Dec 17 '12 at 21:13
  • @EJP Isn't that exactly what I said in my first comment? "However it doesn't use this information to filter incoming packets." I was just correcting this answer which wrongly claims "The linux kernel doesn't track the state of IGMP joins". – Ambroz Bizjak Dec 17 '12 at 21:39
4

The kernel simply doesn't filter incoming multicast packets based on which multicast groups a socket is a member of. If you don't add a socket to a group, it may still receive multicasts to a group if other sockets on the same system are members. (I'm not sure what happens if a multicast arrives but no sockets are members. You can test if you like.)

Note that the kernel in fact does track group ownership, per-socket even. It has to, or it couldn't properly implement the client side of the IGMP protocol. For instance, the kernel needs to reply to various kinds of IGMP queries from the router (where it is asked what groups the host is joined to), and also so it knows to send a Leave Group message when there are no more sockets joined to a specific group.

Ambroz Bizjak
  • 7,809
  • 1
  • 38
  • 49