lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20240418174437.26662-1-kuniyu@amazon.com>
Date: Thu, 18 Apr 2024 10:44:37 -0700
From: Kuniyuki Iwashima <kuniyu@...zon.com>
To: <bob.mcmahon@...adcom.com>
CC: <netdev@...r.kernel.org>, <kuniyu@...zon.com>
Subject: Re: UDP recvfrom/recv question on connected/unconnected sockets

From: Bob McMahon <bob.mcmahon@...adcom.com>
Date: Thu, 18 Apr 2024 10:07:51 -0700
> Hi All,
> 
> I have a question about the OS routing UDP packets to threads and connect
> vs unconnected sockets. Same src/dst IP and same dst port, different src
> port.
> 
> If there are two UDP sockets listening on the same port, each serviced by
> its own thread and they both hang a recvfrom() or recv() (for the connected
> socket,) will the OS route packets only to the thread with a connected
> socket vs the thread with th unconnected socket? If not, what will happen?

Connected sockets receive packets matching 4-tuple, and unconnected sockets
receive packets that no connected socket matches.

  $ python3
  >>> from socket import *
  >>> 
  >>> s1 = socket(AF_INET, SOCK_DGRAM)
  >>> s1.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  >>> s1.bind(('0', 0))
  >>> 
  >>> s2 = socket(AF_INET, SOCK_DGRAM)
  >>> s2.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  >>> s2.bind(s1.getsockname())
  >>> 
  >>> s1.connect(('10.0.0.53', 8000))
  >>> s1
  <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('10.0.0.53', 28947), raddr=('10.0.0.53', 8000)>
  >>> s2
  <socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('0.0.0.0', 28947)>
  >>> 
  >>> s3 = socket(AF_INET, SOCK_DGRAM)
  >>> s3.bind(('10.0.0.53', 8000))
  >>> 
  >>> s4 = socket(AF_INET, SOCK_DGRAM)
  >>> s4.bind(('10.0.0.53', 8080))
  >>> 
  >>> s3.sendto(b'hello', s1.getsockname())
  5
  >>> s4.sendto(b'world', s1.getsockname())
  5
  >>> 
  >>> s1.recv(10)
  b'hello'
  >>> s2.recv(10)
  b'world'

Then, if you create a new unconnected socket, the old unconnected socket will
no longer receive packets until the new one is close()d.

  >>> s5 = socket(AF_INET, SOCK_DGRAM)
  >>> s5.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  >>> s5.bind(s1.getsockname())
  >>> 
  >>> s4.sendto(b'test', s1.getsockname())
  4
  >>> s2.recv(10)
  ^CTraceback (most recent call last):
    File "<stdin>", line 1, in <module>
  KeyboardInterrupt
  >>> s5.recv(10)
  b'test'

SO_REUSEADDR allows the newer socket to take over the port from the old
ones.

If you want to blance the loads across unconnected sockets, use SO_REUSPORT
instead.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ