UDP packets arriving on wrong sockets
Subsystems (2)
Hi,
I posted this on stackoverflow yesterday but I'm reposting it here since it got
no response. Original post: https://stackoverflow.com/questions/51630337/udp-packets-arriving-on-wrong-sockets-on-linux
I have two UDP sockets bound to the same address and connected to addresses A and B. I have two more UDP sockets bound to A and B and not connected.
This is what my /proc/net/udp looks like (trimmed for readability):
sl local_address rem_address
3937: 0100007F:DD9C 0300007F:9910
3937: 0100007F:DD9C 0200007F:907D
16962: 0200007F:907D 00000000:0000
19157: 0300007F:9910 00000000:0000According to connect(2): "If the socket sockfd is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, *and the only address from which datagrams are received*."
For some reason, my connected sockets are receiving packets that were destined for each other. eg: The UDP socket connected to A sends a message to A, A then sends a reply back. The UDP socket connected to B sends a message to B, B then sends a reply back. But the reply from A arrives at the socket connected to B and the reply from B arrives at the socket connected to A.
Why on earth would this be happening? Note that it happens randomly - sometimes the replies arrive at the correct sockets and sometimes they don't. Is there any way to prevent this or any situation under which connect() is supposed to not work?
Any help explaining this would be hugely appreciated :)
- Andrew
Quoted 26 lines · Andrew Cann
Hi,
I posted this on stackoverflow yesterday but I'm reposting it here since it got
no response. Original post: https://stackoverflow.com/questions/51630337/udp-packets-arriving-on-wrong-sockets-on-linuxI have two UDP sockets bound to the same address and connected to addresses A and B. I have two more UDP sockets bound to A and B and not connected.
This is what my
/proc/net/udplooks like (trimmed for readability):sl local_address rem_address 3937: 0100007F:DD9C 0300007F:9910 3937: 0100007F:DD9C 0200007F:907D 16962: 0200007F:907D 00000000:0000 19157: 0300007F:9910 00000000:0000According to connect(2): "If the socket sockfd is of type
SOCK_DGRAM, then addr is the address to which datagrams are sent by default, *and the only address from which datagrams are received*."For some reason, my connected sockets are receiving packets that were destined for each other. eg: The UDP socket connected to A sends a message to A, A then sends a reply back. The UDP socket connected to B sends a message to B, B then sends a reply back. But the reply from A arrives at the socket connected to B and the reply from B arrives at the socket connected to A.
Quoted 6 lines
Why on earth would this be happening? Note that it happens randomly - sometimes the replies arrive at the correct sockets and sometimes they don't. Is there any way to prevent this or any situation under which
connect()is supposed to not work?Any help explaining this would be hugely appreciated :)
Hi Andrew
Well, you should first give much more details, as there are thousands of different UDP stacks out there.
Documentation/admin-guide/reporting-bugs.rst
...
[4.1.] Kernel version (from /proc/version):
...
Ideally you could give us a C reproducer, so that we can run it ourselves and fix the kernel bug if there is one.
This C reproducer could be part of an official patch, adding a test in tools/testing/selftests/net
Thanks !
Quoted 4 lines · Eric Dumazet
Ideally you could give us a C reproducer, so that we can run it ourselves and fix the kernel bug if there is one.
This C reproducer could be part of an official patch, adding a test in
tools/testing/selftests/net
Alternatively a test in Python would be accepted ;)
Quoted 9 lines · Eric Dumazet
Quoted 5 lines · Andrew Cann
Hi,
I posted this on stackoverflow yesterday but I'm reposting it here since it got
no response. Original post: https://stackoverflow.com/questions/51630337/udp-packets-arriving-on-wrong-sockets-on-linux
Quoted 8 lines
Quoted 8 lines
I have two UDP sockets bound to the same address and connected to addresses A and B. I have two more UDP sockets bound to A and B and not connected.
This is what my
/proc/net/udplooks like (trimmed for readability):sl local_address rem_address 3937: 0100007F:DD9C 0300007F:9910 3937: 0100007F:DD9C 0200007F:907D
You have two sockets bound to the same address and port? Is this using SO_REUSEPORT?
Quoted 13 lines · Willem de Bruijn
Quoted 12 lines · Eric Dumazet
Quoted 8 lines · Andrew Cann
Hi,
I posted this on stackoverflow yesterday but I'm reposting it here since it got
no response. Original post: https://stackoverflow.com/questions/51630337/udp-packets-arriving-on-wrong-sockets-on-linuxI have two UDP sockets bound to the same address and connected to addresses A and B. I have two more UDP sockets bound to A and B and not connected.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
This is what my
/proc/net/udplooks like (trimmed for readability):sl local_address rem_address 3937: 0100007F:DD9C 0300007F:9910 3937: 0100007F:DD9C 0200007F:907DYou have two sockets bound to the same address and port? Is this using
SO_REUSEPORT?
Yeah, time to add listen() and accept() support for UDP :)
Quoted 2 lines · Willem de Bruijn
You have two sockets bound to the same address and port? Is this using
SO_REUSEPORT?
Yes, this is using SO_REUSEPORT.
My colleague wrote a python reproducer for this here:
https://gist.github.com/povilasb/53f1c802dbc2aca36a0ffa5b4cb95536
If you run server.py, then client.py, you should see packets arriving at opposite sockets about half the time. My kernel version is NixOS 4.14.51, he tested on two different machines - a debian 4.9.0 and fedora 28 4.17.2. We can reproduce this on all kernels we tested.
Thanks in advance for any help you can give :)
- Andrew
Quoted 6 lines · Andrew Cann
Quoted 2 lines · Willem de Bruijn
You have two sockets bound to the same address and port? Is this using
SO_REUSEPORT?Yes, this is using
SO_REUSEPORT.
Then this is working as intended.
Without SO_REUSEPORT it would not be possible to bind two sockets to the same address and port. See documentation, e.g., at
https://lwn.net/Articles/542629/
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Andrew Cann
Quoted 2 lines · Willem de Bruijn
You have two sockets bound to the same address and port? Is this using
SO_REUSEPORT?
Quoted 7 lines
Quoted 1 line
Yes, this is using
SO_REUSEPORT.Then this is working as intended.
Without
SO_REUSEPORTit would not be possible to bind two sockets to the same address and port. See documentation, e.g., at
https://lwn.net/Articles/542629/
The man page for connect clearly states that a connected UDP socket should only receive datagrams from the address that it is connected to. This isn't the behaviour I'm seeing. That's the issue.