2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <net/route.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
56 #include <netinet/ip6.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/icmp_var.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcpip.h>
65 #include <netinet/tcp_seq.h>
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/tcp_debug.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
84 static struct netinfo *enter(struct in_conninfo *, uint8_t, int, const char *);
85 static void enter_kvm(struct inpcb *, struct socket *, int, const char *);
86 static void enter_sysctl(struct xinpcb *, struct xsocket *, int, const char *);
87 static void fetchnetstat_kvm(void);
88 static void fetchnetstat_sysctl(void);
89 static char *inetname(struct sockaddr *);
90 static void inetprint(struct sockaddr *, const char *);
92 #define streq(a,b) (strcmp(a,b)==0)
93 #define YMAX(w) (getmaxy(w)-2)
100 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
104 TAILQ_ENTRY(netinfo) chain;
105 short ni_line; /* line on screen */
106 short ni_seen; /* 0 when not present in list */
108 #define NIF_LACHG 0x1 /* local address changed */
109 #define NIF_FACHG 0x2 /* foreign address changed */
110 short ni_state; /* tcp state */
111 const char *ni_proto; /* protocol */
112 struct sockaddr_storage ni_lsa; /* local address */
113 struct sockaddr_storage ni_fsa; /* foreign address */
114 u_int ni_rcvcc; /* rcv buffer character count */
115 u_int ni_sndcc; /* snd buffer character count */
118 static TAILQ_HEAD(netinfohead, netinfo) netcb = TAILQ_HEAD_INITIALIZER(netcb);
120 static int aflag = 0;
121 static int nflag = 0;
122 static int lastrow = 1;
125 closenetstat(WINDOW *w)
131 TAILQ_FOREACH(p, &netcb, chain) {
132 if (p->ni_line != -1)
143 static const char *miblist[] = {
144 "net.inet.tcp.pcblist",
145 "net.inet.udp.pcblist"
148 static char tcb[] = "tcb", udb[] = "udb";
150 struct nlist namelist[] = {
171 fetchnetstat_sysctl();
175 fetchnetstat_kvm(void)
179 struct inpcbhead head;
186 if (namelist[X_TCB].n_value == 0)
188 TAILQ_FOREACH(p, &netcb, chain)
194 else if (protos&UDP) {
199 error("No protocols to display");
203 KREAD(off, &head, sizeof (struct inpcbhead));
204 LIST_FOREACH(next, &head, inp_list) {
205 KREAD(next, &inpcb, sizeof (inpcb));
208 if (inpcb.inp_vflag & INP_IPV4) {
209 if (inpcb.inp_laddr.s_addr == INADDR_ANY)
213 else if (inpcb.inp_vflag & INP_IPV6) {
214 if (memcmp(&inpcb.in6p_laddr,
215 &in6addr_any, sizeof(in6addr_any)) == 0)
220 if (nhosts && !checkhost(&inpcb.inp_inc))
222 if (nports && !checkport(&inpcb.inp_inc))
225 if (inpcb.inp_flags & INP_TIMEWAIT) {
226 bzero(&sockb, sizeof(sockb));
227 enter_kvm(&inpcb, &sockb, TCPS_TIME_WAIT,
230 KREAD(inpcb.inp_socket, &sockb,
232 KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
233 enter_kvm(&inpcb, &sockb, tcpcb.t_state,
237 enter_kvm(&inpcb, &sockb, 0, "udp");
239 if (istcp && (protos&UDP)) {
247 fetchnetstat_sysctl(void)
251 struct xinpgen *inpg;
253 struct xinpcb *xip = NULL;
254 struct xtcpcb *xtp = NULL;
258 TAILQ_FOREACH(p, &netcb, chain)
262 } else if (protos&UDP) {
265 error("No protocols to display");
269 for (;idx < 2; idx++) {
270 if (idx == 1 && !(protos&UDP))
272 inpg = (struct xinpgen *)sysctl_dynread(miblist[idx], &lsz);
274 error("sysctl(%s...) failed", miblist[idx]);
278 * We currently do no require a consistent pcb list.
279 * Try to be robust in case of struct size changes
281 cur = ((char *)inpg) + inpg->xig_len;
282 /* There is also a trailing struct xinpgen */
283 end = ((char *)inpg) + lsz - inpg->xig_len;
288 if (idx == 0) { /* TCP */
289 xtp = (struct xtcpcb *)cur;
292 xip = (struct xinpcb *)cur;
295 while (cur + plen <= end) {
296 if (idx == 0) { /* TCP */
297 xtp = (struct xtcpcb *)cur;
300 xip = (struct xinpcb *)cur;
305 if (xip->inp_vflag & INP_IPV4) {
306 if (xip->inp_laddr.s_addr == INADDR_ANY)
310 else if (xip->inp_vflag & INP_IPV6) {
311 if (memcmp(&xip->in6p_laddr,
312 &in6addr_any, sizeof(in6addr_any))
318 if (nhosts && !checkhost(&xip->inp_inc))
320 if (nports && !checkport(&xip->inp_inc))
323 enter_sysctl(xip, &xip->xi_socket,
324 xtp->t_state, "tcp");
326 enter_sysctl(xip, &xip->xi_socket, 0, "udp");
333 enter_kvm(struct inpcb *inp, struct socket *so, int state, const char *proto)
337 if ((p = enter(&inp->inp_inc, inp->inp_vflag, state, proto)) != NULL) {
338 p->ni_rcvcc = so->so_rcv.sb_ccc;
339 p->ni_sndcc = so->so_snd.sb_ccc;
344 enter_sysctl(struct xinpcb *xip, struct xsocket *so, int state,
349 if ((p = enter(&xip->inp_inc, xip->inp_vflag, state, proto)) != NULL) {
350 p->ni_rcvcc = so->so_rcv.sb_cc;
351 p->ni_sndcc = so->so_snd.sb_cc;
355 static struct netinfo *
356 enter(struct in_conninfo *inc, uint8_t vflag, int state, const char *proto)
359 struct sockaddr_storage lsa, fsa;
360 struct sockaddr_in *sa4;
362 struct sockaddr_in6 *sa6;
365 memset(&lsa, 0, sizeof(lsa));
366 memset(&fsa, 0, sizeof(fsa));
367 if (vflag & INP_IPV4) {
368 sa4 = (struct sockaddr_in *)&lsa;
369 sa4->sin_addr = inc->inc_laddr;
370 sa4->sin_port = inc->inc_lport;
371 sa4->sin_family = AF_INET;
372 sa4->sin_len = sizeof(struct sockaddr_in);
374 sa4 = (struct sockaddr_in *)&fsa;
375 sa4->sin_addr = inc->inc_faddr;
376 sa4->sin_port = inc->inc_fport;
377 sa4->sin_family = AF_INET;
378 sa4->sin_len = sizeof(struct sockaddr_in);
381 else if (vflag & INP_IPV6) {
382 sa6 = (struct sockaddr_in6 *)&lsa;
383 memcpy(&sa6->sin6_addr, &inc->inc6_laddr,
384 sizeof(struct in6_addr));
385 sa6->sin6_port = inc->inc_lport;
386 sa6->sin6_family = AF_INET6;
387 sa6->sin6_len = sizeof(struct sockaddr_in6);
389 sa6 = (struct sockaddr_in6 *)&fsa;
390 memcpy(&sa6->sin6_addr, &inc->inc6_faddr,
391 sizeof(struct in6_addr));
392 sa6->sin6_port = inc->inc_fport;
393 sa6->sin6_family = AF_INET6;
394 sa6->sin6_len = sizeof(struct sockaddr_in6);
401 * Only take exact matches, any sockets with
402 * previously unbound addresses will be deleted
403 * below in the display routine because they
404 * will appear as ``not seen'' in the kernel
407 TAILQ_FOREACH(p, &netcb, chain) {
408 if (!streq(proto, p->ni_proto))
410 if (p->ni_lsa.ss_family != lsa.ss_family ||
411 memcmp(&p->ni_lsa, &lsa, lsa.ss_len) != 0)
413 if (p->ni_fsa.ss_family == fsa.ss_family &&
414 memcmp(&p->ni_fsa, &fsa, fsa.ss_len) == 0)
418 if ((p = malloc(sizeof(*p))) == NULL) {
419 error("Out of memory");
422 TAILQ_INSERT_HEAD(&netcb, p, chain);
424 memcpy(&p->ni_lsa, &lsa, lsa.ss_len);
425 memcpy(&p->ni_fsa, &fsa, fsa.ss_len);
426 p->ni_proto = strdup(proto);
427 p->ni_flags = NIF_LACHG|NIF_FACHG;
434 /* column locations */
436 #define FADDR LADDR+23
437 #define PROTO FADDR+23
438 #define RCVCC PROTO+6
439 #define SNDCC RCVCC+7
440 #define STATE SNDCC+7
445 if (use_kvm && namelist[X_TCB].n_type == 0)
447 wmove(wnd, 0, 0); wclrtobot(wnd);
448 mvwaddstr(wnd, 0, LADDR, "Local Address");
449 mvwaddstr(wnd, 0, FADDR, "Foreign Address");
450 mvwaddstr(wnd, 0, PROTO, "Proto");
451 mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
452 mvwaddstr(wnd, 0, SNDCC, "Send-Q");
453 mvwaddstr(wnd, 0, STATE, "(state)");
459 struct netinfo *p, *q;
461 const char *family = "";
464 * First, delete any connections that have gone
465 * away and adjust the position of connections
466 * below to reflect the deleted line.
468 p = TAILQ_FIRST(&netcb);
470 if (p->ni_line == -1 || p->ni_seen) {
471 p = TAILQ_NEXT(p, chain);
474 wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
475 TAILQ_FOREACH(q, &netcb, chain)
476 if (q != p && q->ni_line > p->ni_line) {
478 /* this shouldn't be necessary */
479 q->ni_flags |= NIF_LACHG|NIF_FACHG;
482 q = TAILQ_NEXT(p, chain);
483 TAILQ_REMOVE(&netcb, p, chain);
488 * Update existing connections and add new ones.
490 TAILQ_FOREACH(p, &netcb, chain) {
491 if (p->ni_line == -1) {
493 * Add a new entry if possible.
495 if (lastrow > YMAX(wnd))
497 p->ni_line = lastrow++;
498 p->ni_flags |= NIF_LACHG|NIF_FACHG;
500 if (p->ni_flags & NIF_LACHG) {
501 wmove(wnd, p->ni_line, LADDR);
502 inetprint((struct sockaddr *)&p->ni_lsa, p->ni_proto);
503 p->ni_flags &= ~NIF_LACHG;
505 if (p->ni_flags & NIF_FACHG) {
506 wmove(wnd, p->ni_line, FADDR);
507 inetprint((struct sockaddr *)&p->ni_fsa, p->ni_proto);
508 p->ni_flags &= ~NIF_FACHG;
511 family = (p->ni_lsa.ss_family == AF_INET) ? "4" : "6";
513 snprintf(proto, sizeof(proto), "%s%s", p->ni_proto, family);
514 mvwaddstr(wnd, p->ni_line, PROTO, proto);
515 mvwprintw(wnd, p->ni_line, RCVCC, "%6u", p->ni_rcvcc);
516 mvwprintw(wnd, p->ni_line, SNDCC, "%6u", p->ni_sndcc);
517 if (streq(p->ni_proto, "tcp")) {
518 if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
519 mvwprintw(wnd, p->ni_line, STATE, "%d",
522 mvwaddstr(wnd, p->ni_line, STATE,
523 tcpstates[p->ni_state]);
527 if (lastrow < YMAX(wnd)) {
528 wmove(wnd, lastrow, 0); wclrtobot(wnd);
529 wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd); /* XXX */
534 * Pretty print an Internet address (net address + port).
535 * If the nflag was specified, use numbers instead of names.
538 inetprint(struct sockaddr *sa, const char *proto)
540 struct servent *sp = 0;
544 switch (sa->sa_family) {
546 port = ((struct sockaddr_in *)sa)->sin_port;
550 port = ((struct sockaddr_in6 *)sa)->sin6_port;
557 snprintf(line, sizeof(line), "%.*s.", 16, inetname(sa));
558 cp = strchr(line, '\0');
560 sp = getservbyport(port, proto);
562 snprintf(cp, sizeof(line) - (cp - line), "%.8s",
563 sp ? sp->s_name : "*");
565 snprintf(cp, sizeof(line) - (cp - line), "%d",
566 ntohs((u_short)port));
567 /* pad to full column to clear any garbage */
568 cp = strchr(line, '\0');
569 while (cp - line < 22)
576 * Construct an Internet address representation.
577 * If the nflag has been supplied, give
578 * numeric value, otherwise try for symbolic name.
581 inetname(struct sockaddr *sa)
584 static char line[NI_MAXHOST];
589 if (sa->sa_family == AF_INET6) {
590 if (memcmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
591 &in6addr_any, sizeof(in6addr_any)) == 0)
594 getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
595 nflag ? NI_NUMERICHOST : 0);
600 in = ((struct sockaddr_in *)sa)->sin_addr;
601 if (!nflag && in.s_addr != INADDR_ANY) {
602 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
606 if (in.s_addr == INADDR_ANY)
609 snprintf(line, sizeof(line), "%s", cp);
611 in.s_addr = ntohl(in.s_addr);
612 #define C(x) ((x) & 0xff)
613 snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
614 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
620 cmdnetstat(const char *cmd, const char *args)
622 if (prefix(cmd, "all")) {
626 if (prefix(cmd, "numbers") || prefix(cmd, "names")) {
630 new = prefix(cmd, "numbers");
633 TAILQ_FOREACH(p, &netcb, chain) {
634 if (p->ni_line == -1)
636 p->ni_flags |= NIF_LACHG|NIF_FACHG;
641 if (!netcmd(cmd, args))