Project

General

Profile

Actions

Bug #13867

closed

netstat shows duplicated data for UDP source and dest addresses

Added by Jason King about 1 month ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
cmd - userland programs
Start date:
Due date:
% Done:

100%

Estimated time:
Difficulty:
Bite-size
Tags:
Gerrit CR:

Description

While testing the fix for #13839, I noticed that the UDP output looked wrong:

UDP: IPv4
   Local Address        Remote Address      User    Pid      Command       State
-------------------- -------------------- -------- ------ -------------- ----------
...
127.0.0.1.56731      127.0.0.1.56731      root        734 xxxxd       Connected
127.0.0.1.40535      127.0.0.1.40535      root        609 yyyyd        Connected
...

Note that the src and dest IP+port are identical.

The reason is that when printing UDP sockets, the same buffer is reused when formatting the local and remote connection information (unlike TCP which uses a separate buffer for local and remote):

static boolean_t
udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first,
    const mib2_transportMLPEntry_t *attr, const mib2_socketInfoEntry_t *sie)
{
        char    *leadin;
        char    lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
                        /* hostname + portname */
...
        if (asprintf(&leadin,
            UDP_V4_LOCAL_F " " UDP_V4_REMOTE_F " ",
            pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp",
            lname, sizeof (lname)),
            ude->udpEntryInfo.ue_state == MIB2_UDP_connected ?
            pr_ap(ude->udpEntryInfo.ue_RemoteAddress,
            ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
            "") == -1) {
                fatal(1, "Out of memory");
        }
...

Note that lname is used both for the remote address and local address. Compare that to TCP:

static boolean_t
tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first,
    const mib2_transportMLPEntry_t *attr, const mib2_socketInfoEntry_t *sie)
{
        /*
         * lname and fname below are for the hostname as well as the portname
         * There is no limit on portname length so we assume MAXHOSTNAMELEN
         * as the limit
         */
        char    lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
        char    fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
...
               (void) printf(
                    TCP_V4_LOCAL_F " " TCP_V4_REMOTE_F " " 
                    TCP_V4_SWIND_F " " TCP_V4_SENDQ_F " " 
                    TCP_V4_RWIND_F " " TCP_V4_RECVQ_F " %s\n",
                    pr_ap(tp->tcpConnLocalAddress,
                    tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
                    pr_ap(tp->tcpConnRemAddress,
                    tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
                    tp->tcpConnEntryInfo.ce_swnd,
                    (sq >= 0) ? sq : 0,
                    tp->tcpConnEntryInfo.ce_rwnd,
                    (rq >= 0) ? rq : 0,
                    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));

The IPv6 versions of these respective functions have a similar form. The fix is to use a separate buffer for the remote address when printing UDP sockets.

Actions #1

Updated by Electric Monk about 1 month ago

  • Gerrit CR set to 1560
Actions #2

Updated by Jason King about 1 month ago

With this change applied, the netstat UDP output, no longer showed duplicated information for the local and remote address. Additionally, the remote addresses (and ports) now reflected what was expected.

Actions #3

Updated by Electric Monk about 1 month ago

  • Status changed from New to Closed
  • % Done changed from 0 to 100

git commit cdfd7f6baf7b308aedaeb7a7f4a89a4dccd9313e

commit  cdfd7f6baf7b308aedaeb7a7f4a89a4dccd9313e
Author: Jason King <jason.brian.king@gmail.com>
Date:   2021-06-21T18:14:21.000Z

    13867 netstat shows duplicated data for UDP source and dest addresses
    Reviewed by: C Fraire <cfraire@me.com>
    Reviewed by: Vitaliy Gusev <gusev.vitaliy@icloud.com>
    Approved by: Dan McDonald <danmcd@joyent.com>

Actions

Also available in: Atom PDF