An address can be in use, but not shown by lsof
, ss
or netstat
once bind
has been used on a SOCK_STREAM
socket, but before the named socket has been set the LISTEN
state.
This was found with a test performed using AlmaLinux 8.6 with a 4.18.0-372.19.1.el8_6.x86_64
Kernel.
The source for the test program is in bind_local.c
Start the test program, specifying an IPv6 link-local address and port number (10000) to bind to:
[mr_halfword@haswell-alma ibv_message_passing]$ ibv_message_passing_c_project/bin/debug/bind_local/bind_local -6 fe80::207:43ff:fe15:2298%4 -p 10000 -l
fd 3 bound to fe80::207:43ff:fe15:2298 scope-id 4 port 10000
Press enter to listen on port
At the above port a SOCK_STREAM
socket has been created, bind
called and getsockname
used to get the socket name which is displayed (i.e. the address the socket has been bound to).
The socket file-descriptor the test program has bound is shown as socket 398999
:
[mr_halfword@haswell-alma ~]$ ls -l /proc/`pgrep bind_local`/fd
total 0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 0 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 1 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 2 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 3 -> 'socket:[398999]'
In this state attempting to use nc
to listen on port 10000 fails with Address already in use, but neither lsof
not ss
show the address:
[mr_halfword@haswell-alma ~]$ nc -l 10000
Ncat: bind to :::10000: Address already in use. QUITTING.
[mr_halfword@haswell-alma ~]$ sudo lsof -i -n -P | grep 10000
[mr_halfword@haswell-alma ~]$ sudo ss -nlp | grep 10000
[mr_halfword@haswell-alma ~]$
Cause the test program to call listen
on the bound socket, by pressing return:
Press return to exit
Now that the bound socket is in the LISTEN
state attempting to use nc
to listen on port 10000 fails with Address already in use, but now lsof
and ss
are showing the address and which program is using the address:
[mr_halfword@haswell-alma ~]$ sudo lsof -i -n -P | grep 10000
bind_loca 16929 mr_halfword 3u IPv6 398999 0t0 TCP [fe80::207:43ff:fe15:2298]:10000 (LISTEN)
[mr_halfword@haswell-alma ~]$ sudo ss -nlp | grep 10000
tcp LISTEN 0 1 [fe80::207:43ff:fe15:2298]%enp1s0f4d1:10000 [::]:* users:(("bind_local",pid=16929,fd=3))
I haven't yet tried looking at the Linux Kernel source code to determine if a SOCK_STREAM
socket which has been bound to an address by being named, but left in that state, has any user space method which can locate the program using the address.
The reason the above was investigating how the iwpmd
iWARP Port Mapper Daemon was claiming TCP ports, for which was unable to find a way to list the claimed TCP ports.