Page MenuHomeFreeBSD

sockstat: Surround explicit IPv6 addresses with brackets
ClosedPublic

Authored by michaelo on Dec 26 2025, 6:26 PM.
Tags
None
Referenced Files
F161235699: D54375.id169904.diff
Thu, Jul 2, 12:09 AM
F161228686: D54375.id168626.diff
Wed, Jul 1, 10:32 PM
Unknown Object (File)
Thu, Jun 18, 1:44 PM
Unknown Object (File)
Thu, Jun 4, 3:29 PM
Unknown Object (File)
May 28 2026, 11:07 AM
Unknown Object (File)
May 28 2026, 6:26 AM
Unknown Object (File)
May 28 2026, 4:27 AM
Unknown Object (File)
May 27 2026, 11:14 PM

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Have a patch locally for <=14 since code is different.
16-CURRENT:

# ./sockstat  -6
USER     COMMAND     PID FD PROTO LOCAL ADDRESS         FOREIGN ADDRESS
osipovmi sshd-sessi 2216  7 tcp6  [fe80::1%lo0]:22      [fe80::1%lo0]:52293
root     sshd-sessi 2213  7 tcp6  [fe80::1%lo0]:22      [fe80::1%lo0]:52293
osipovmi ssh        2212  3 tcp6  [fe80::1%lo0]:52293   [fe80::1%lo0]:22
root     sshd       1310  6 tcp6  *:22                  *:*
root     ntpd       1233 21 udp6  [::1]:123             *:*
root     ntpd       1233 22 udp6  [fe80::1%lo0]:123     *:*
root     rpc.statd  1061  4 udp6  *:934                 *:*
root     rpc.statd  1061  5 tcp6  *:934                 *:*
root     rpcbind    1059  8 udp6  *:111                 *:*
root     rpcbind    1059  9 udp6  *:868                 *:*
root     rpcbind    1059 10 tcp6  *:111                 *:*
root     rpcbind    1059 16 udp6  *:*                   *:*
??       ??           ?? ?? tcp6  *:887                 *:*
??       ??           ?? ?? udp6  *:931                 *:*
??       ??           ?? ?? udp6  *:652                 *:*

13-STABLE:

$ ./sockstat -6
USER     COMMAND    PID   FD PROTO  LOCAL ADDRESS         FOREIGN ADDRESS
osipovmi nc         33276 3  tcp6   [fe80::1%lo0]:6000    *:*
root     master     69090 14 tcp6   *:25                  *:*
postgres postgres   1948  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1947  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1932  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1931  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1930  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1929  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1928  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1927  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1926  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1925  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1924  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1923  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1922  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1921  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1920  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1919  9  tcp6   [::1]:10051           *:*
postgres postgres   1918  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1917  9  tcp6   [::1]:10051           *:*
postgres postgres   1916  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1915  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1914  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1913  9  tcp6   [::1]:10051           *:*
postgres postgres   1912  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1911  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1910  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1909  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1908  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1907  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1906  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1905  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1904  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1903  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1902  9  tcp6   [::1]:10051           *:*
postgres postgres   1901  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1900  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1899  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1898  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1896  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1895  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1894  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1893  9  tcp6   [::1]:10051           *:*
postgres postgres   1891  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1889  9  tcp6   [::1]:10051           *:*
postgres postgres   1887  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1885  9  tcp6   [::1]:10051           *:*
postgres postgres   1884  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1883  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1882  9  tcp6   [::1]:10051           *:*
postgres postgres   1881  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1880  9  tcp6   [::1]:10051           *:*
postgres postgres   1879  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1877  9  tcp6   [::1]:10051           *:*
postgres postgres   1876  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1875  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1873  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1872  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1870  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1869  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1868  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1867  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1866  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1865  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1864  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1863  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1862  9  tcp6   [::1]:10051           *:*
postgres postgres   1861  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1860  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1859  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1858  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1857  9  tcp6   [::1]:10051           *:*
postgres postgres   1820  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1818  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_ser 1817  9  tcp6   [::1]:10051           *:*
zabbix   zabbix_ser 1816  9  tcp6   [::1]:10051           *:*
postgres postgres   1801  9  udp6   [::1]:51742           [::1]:51742
root     sshd       1782  7  tcp6   *:22                  *:*
root     nfsd       1737  6  tcp6   *:2049                *:*
zabbix   zabbix_ser 1729  9  tcp6   [::1]:10051           *:*
root     mountd     1698  7  udp6   *:958                 *:*
root     mountd     1698  8  tcp6   *:958                 *:*
postgres postgres   1691  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1690  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1689  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1688  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1687  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1686  9  udp6   [::1]:51742           [::1]:51742
postgres postgres   1684  6  tcp6   [::1]:5432            *:*
postgres postgres   1684  9  udp6   [::1]:51742           [::1]:51742
zabbix   zabbix_age 1681  5  tcp6   *:10050               *:*
zabbix   zabbix_age 1680  5  tcp6   *:10050               *:*
zabbix   zabbix_age 1679  5  tcp6   *:10050               *:*
zabbix   zabbix_age 1678  5  tcp6   *:10050               *:*
zabbix   zabbix_age 1677  5  tcp6   *:10050               *:*
zabbix   zabbix_age 1668  5  tcp6   *:10050               *:*
root     ntpd       1660  21 udp6   [::1]:123             *:*
root     ntpd       1660  22 udp6   [fe80::1%lo0]:123     *:*
root     rpc.statd  1582  4  udp6   *:871                 *:*
root     rpc.statd  1582  5  tcp6   *:871                 *:*
root     rpcbind    1570  6  udp6   *:111                 *:*
root     rpcbind    1570  7  udp6   *:740                 *:*
root     rpcbind    1570  8  tcp6   *:111                 *:*
root     rpcbind    1570  13 udp6   *:*                   *:*
?        ?          ?     ?  udp6   *:2049                *:*
?        ?          ?     ?  tcp6   *:801                 *:*
?        ?          ?     ?  udp6   *:605                 *:*
?        ?          ?     ?  udp6   *:841                 *:*

Would it make sense to use the same way as netstat for consistency? This would mean to use . instead of : and no brackets.

Would it make sense to use the same way as netstat for consistency? This would mean to use . instead of : and no brackets.

I honestly must say that I never understood why netstat uses a dot to separate both components while the rest of the world uses colon. I wouldn't apply netstat's logic here.

This revision is now accepted and ready to land.Dec 26 2025, 8:29 PM

I'd also say des@ should have a look as he's the author of sockstat. For me, I'm all in for this proposed change.

des requested changes to this revision.Dec 29 2025, 12:14 PM

I did indeed write sockstat(8) but I did not write (most of) formataddr(), and I certainly would not have approved it if asked to review it.

usr.bin/sockstat/main.c
928–929
929–931
950–951
952
966
972

I would prefer a solution that ensures addrstr already contains brackets at this point.

973–974
974–975
This revision now requires changes to proceed.Dec 29 2025, 12:14 PM

@des, thanks for the review. I don't want to modify code which I didn't touch. This can be done in a subsequent change.

usr.bin/sockstat/main.c
972

Are you sure because the address itself does not require square brackets, it is only required if it is not * or a port is present? I can easily connect to ssh fe80::1%lo0 if I don't need to the port. The brackets aren't part of the address.

michaelo marked an inline comment as not done.Dec 29 2025, 5:01 PM
michaelo added inline comments.
usr.bin/sockstat/main.c
972

Moreover, if addrstr is modified *before* is_xo_style_encoding is evaluated then {:address/%s} will contain incorrectly bracketed data.

michaelo marked an inline comment as not done.Dec 29 2025, 5:08 PM
michaelo added inline comments.
usr.bin/sockstat/main.c
972

Reference: https://datatracker.ietf.org/doc/html/rfc5952#section-6

It truly only applies when a port is in play.

usr.bin/sockstat/main.c
972

You can trivially

a) increase the size of the buffer by 2
b) determine if brackets are going to be needed before calling cap_getnameinfo()
c) if so, make addrstr point to the second byte of the buffer instead of the first
d) add brackets to the buffer after xo_emit()

usr.bin/sockstat/main.c
972

a) NI_MAXHOST is 1025, this is more room than necessary.
b) sure
c) + d) this adds two more if clauses and the code worse to read through pointer magic:

if (need_brackets) {
    size_t len = strlen(addrstr);
    addrbuf[0] = '[';
    addrstr[len] = ']';
    addrstr[len + 1] = '\0';
    addrstr = addrbuf;
}

You consider this an improvement?

michaelo marked 6 inline comments as done and 2 inline comments as done.Jan 11 2026, 8:45 PM

Address style(9) fixes

@des Please take another look. Do you still want to see the pointer gymnastics?

This revision is now accepted and ready to land.Jan 31 2026, 5:58 PM

Backporting this change to FreeBSD 14.4 has caused downtime for me because it broke my service health checks which relied on the exact output format ($addr:$port instead of [$addr]:$port) and there was no notice in the release notes that the output format has changed.

Backporting this change to FreeBSD 14.4 has caused downtime for me because it broke my service health checks which relied on the exact output format ($addr:$port instead of [$addr]:$port) and there was no notice in the release notes that the output format has changed.

Sorry but we don't usually include minor bugfixes in the release notes, and [addr]:port has been the correct syntax for IPv6 addresses for 25+ years now.