2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <net/route.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcpip.h>
62 #include <netinet/tcp_seq.h>
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcp_debug.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
81 static struct netinfo *enter(struct in_conninfo *, uint8_t, int, const char *);
82 static void enter_kvm(struct inpcb *, struct socket *, int, const char *);
83 static void enter_sysctl(struct xinpcb *, struct xsocket *, int, const char *);
84 static void fetchnetstat_kvm(void);
85 static void fetchnetstat_sysctl(void);
86 static char *inetname(struct sockaddr *);
87 static void inetprint(struct sockaddr *, const char *);
89 #define streq(a,b) (strcmp(a,b)==0)
90 #define YMAX(w) (getmaxy(w)-2)
97 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
101 TAILQ_ENTRY(netinfo) chain;
102 short ni_line; /* line on screen */
103 short ni_seen; /* 0 when not present in list */
105 #define NIF_LACHG 0x1 /* local address changed */
106 #define NIF_FACHG 0x2 /* foreign address changed */
107 short ni_state; /* tcp state */
108 const char *ni_proto; /* protocol */
109 struct sockaddr_storage ni_lsa; /* local address */
110 struct sockaddr_storage ni_fsa; /* foreign address */
111 u_int ni_rcvcc; /* rcv buffer character count */
112 u_int ni_sndcc; /* snd buffer character count */
115 TAILQ_HEAD(netinfohead, netinfo) netcb = TAILQ_HEAD_INITIALIZER(netcb);
117 static int aflag = 0;
118 static int nflag = 0;
119 static int lastrow = 1;
122 closenetstat(WINDOW *w)
128 TAILQ_FOREACH(p, &netcb, chain) {
129 if (p->ni_line != -1)
140 static const char *miblist[] = {
141 "net.inet.tcp.pcblist",
142 "net.inet.udp.pcblist"
145 static char tcb[] = "tcb", udb[] = "udb";
147 struct nlist namelist[] = {
168 fetchnetstat_sysctl();
172 fetchnetstat_kvm(void)
176 struct inpcbhead head;
183 if (namelist[X_TCB].n_value == 0)
185 TAILQ_FOREACH(p, &netcb, chain)
191 else if (protos&UDP) {
196 error("No protocols to display");
200 KREAD(off, &head, sizeof (struct inpcbhead));
201 LIST_FOREACH(next, &head, inp_list) {
202 KREAD(next, &inpcb, sizeof (inpcb));
205 if (inpcb.inp_vflag & INP_IPV4) {
206 if (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
210 else if (inpcb.inp_vflag & INP_IPV6) {
211 if (memcmp(&inpcb.in6p_laddr,
212 &in6addr_any, sizeof(in6addr_any)) == 0)
217 if (nhosts && !checkhost(&inpcb.inp_inc))
219 if (nports && !checkport(&inpcb.inp_inc))
222 if (inpcb.inp_flags & INP_TIMEWAIT) {
223 bzero(&sockb, sizeof(sockb));
224 enter_kvm(&inpcb, &sockb, TCPS_TIME_WAIT,
227 KREAD(inpcb.inp_socket, &sockb,
229 KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
230 enter_kvm(&inpcb, &sockb, tcpcb.t_state,
234 enter_kvm(&inpcb, &sockb, 0, "udp");
236 if (istcp && (protos&UDP)) {
244 fetchnetstat_sysctl(void)
248 struct xinpgen *inpg;
250 struct xinpcb *xip = NULL;
251 struct xtcpcb *xtp = NULL;
255 TAILQ_FOREACH(p, &netcb, chain)
259 } else if (protos&UDP) {
262 error("No protocols to display");
266 for (;idx < 2; idx++) {
267 if (idx == 1 && !(protos&UDP))
269 inpg = (struct xinpgen *)sysctl_dynread(miblist[idx], &lsz);
271 error("sysctl(%s...) failed", miblist[idx]);
275 * We currently do no require a consistent pcb list.
276 * Try to be robust in case of struct size changes
278 cur = ((char *)inpg) + inpg->xig_len;
279 /* There is also a trailing struct xinpgen */
280 end = ((char *)inpg) + lsz - inpg->xig_len;
285 if (idx == 0) { /* TCP */
286 xtp = (struct xtcpcb *)cur;
289 xip = (struct xinpcb *)cur;
292 while (cur + plen <= end) {
293 if (idx == 0) { /* TCP */
294 xtp = (struct xtcpcb *)cur;
297 xip = (struct xinpcb *)cur;
302 if (xip->inp_vflag & INP_IPV4) {
303 if (inet_lnaof(xip->inp_laddr) ==
308 else if (xip->inp_vflag & INP_IPV6) {
309 if (memcmp(&xip->in6p_laddr,
310 &in6addr_any, sizeof(in6addr_any))
316 if (nhosts && !checkhost(&xip->inp_inc))
318 if (nports && !checkport(&xip->inp_inc))
321 enter_sysctl(xip, &xip->xi_socket,
322 xtp->t_state, "tcp");
324 enter_sysctl(xip, &xip->xi_socket, 0, "udp");
331 enter_kvm(struct inpcb *inp, struct socket *so, int state, const char *proto)
335 if ((p = enter(&inp->inp_inc, inp->inp_vflag, state, proto)) != NULL) {
336 p->ni_rcvcc = so->so_rcv.sb_ccc;
337 p->ni_sndcc = so->so_snd.sb_ccc;
342 enter_sysctl(struct xinpcb *xip, struct xsocket *so, int state,
347 if ((p = enter(&xip->inp_inc, xip->inp_vflag, state, proto)) != NULL) {
348 p->ni_rcvcc = so->so_rcv.sb_cc;
349 p->ni_sndcc = so->so_snd.sb_cc;
353 static struct netinfo *
354 enter(struct in_conninfo *inc, uint8_t vflag, int state, const char *proto)
357 struct sockaddr_storage lsa, fsa;
358 struct sockaddr_in *sa4;
360 struct sockaddr_in6 *sa6;
363 memset(&lsa, 0, sizeof(lsa));
364 memset(&fsa, 0, sizeof(fsa));
365 if (vflag & INP_IPV4) {
366 sa4 = (struct sockaddr_in *)&lsa;
367 sa4->sin_addr = inc->inc_laddr;
368 sa4->sin_port = inc->inc_lport;
369 sa4->sin_family = AF_INET;
370 sa4->sin_len = sizeof(struct sockaddr_in);
372 sa4 = (struct sockaddr_in *)&fsa;
373 sa4->sin_addr = inc->inc_faddr;
374 sa4->sin_port = inc->inc_fport;
375 sa4->sin_family = AF_INET;
376 sa4->sin_len = sizeof(struct sockaddr_in);
379 else if (vflag & INP_IPV6) {
380 sa6 = (struct sockaddr_in6 *)&lsa;
381 memcpy(&sa6->sin6_addr, &inc->inc6_laddr,
382 sizeof(struct in6_addr));
383 sa6->sin6_port = inc->inc_lport;
384 sa6->sin6_family = AF_INET6;
385 sa6->sin6_len = sizeof(struct sockaddr_in6);
387 sa6 = (struct sockaddr_in6 *)&fsa;
388 memcpy(&sa6->sin6_addr, &inc->inc6_faddr,
389 sizeof(struct in6_addr));
390 sa6->sin6_port = inc->inc_fport;
391 sa6->sin6_family = AF_INET6;
392 sa6->sin6_len = sizeof(struct sockaddr_in6);
399 * Only take exact matches, any sockets with
400 * previously unbound addresses will be deleted
401 * below in the display routine because they
402 * will appear as ``not seen'' in the kernel
405 TAILQ_FOREACH(p, &netcb, chain) {
406 if (!streq(proto, p->ni_proto))
408 if (p->ni_lsa.ss_family != lsa.ss_family ||
409 memcmp(&p->ni_lsa, &lsa, lsa.ss_len) != 0)
411 if (p->ni_fsa.ss_family == fsa.ss_family &&
412 memcmp(&p->ni_fsa, &fsa, fsa.ss_len) == 0)
416 if ((p = malloc(sizeof(*p))) == NULL) {
417 error("Out of memory");
420 TAILQ_INSERT_HEAD(&netcb, p, chain);
422 memcpy(&p->ni_lsa, &lsa, lsa.ss_len);
423 memcpy(&p->ni_fsa, &fsa, fsa.ss_len);
424 p->ni_proto = strdup(proto);
425 p->ni_flags = NIF_LACHG|NIF_FACHG;
432 /* column locations */
434 #define FADDR LADDR+23
435 #define PROTO FADDR+23
436 #define RCVCC PROTO+6
437 #define SNDCC RCVCC+7
438 #define STATE SNDCC+7
443 if (use_kvm && namelist[X_TCB].n_type == 0)
445 wmove(wnd, 0, 0); wclrtobot(wnd);
446 mvwaddstr(wnd, 0, LADDR, "Local Address");
447 mvwaddstr(wnd, 0, FADDR, "Foreign Address");
448 mvwaddstr(wnd, 0, PROTO, "Proto");
449 mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
450 mvwaddstr(wnd, 0, SNDCC, "Send-Q");
451 mvwaddstr(wnd, 0, STATE, "(state)");
457 struct netinfo *p, *q;
459 const char *family = "";
462 * First, delete any connections that have gone
463 * away and adjust the position of connections
464 * below to reflect the deleted line.
466 p = TAILQ_FIRST(&netcb);
468 if (p->ni_line == -1 || p->ni_seen) {
469 p = TAILQ_NEXT(p, chain);
472 wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
473 TAILQ_FOREACH(q, &netcb, chain)
474 if (q != p && q->ni_line > p->ni_line) {
476 /* this shouldn't be necessary */
477 q->ni_flags |= NIF_LACHG|NIF_FACHG;
480 q = TAILQ_NEXT(p, chain);
481 TAILQ_REMOVE(&netcb, p, chain);
486 * Update existing connections and add new ones.
488 TAILQ_FOREACH(p, &netcb, chain) {
489 if (p->ni_line == -1) {
491 * Add a new entry if possible.
493 if (lastrow > YMAX(wnd))
495 p->ni_line = lastrow++;
496 p->ni_flags |= NIF_LACHG|NIF_FACHG;
498 if (p->ni_flags & NIF_LACHG) {
499 wmove(wnd, p->ni_line, LADDR);
500 inetprint((struct sockaddr *)&p->ni_lsa, p->ni_proto);
501 p->ni_flags &= ~NIF_LACHG;
503 if (p->ni_flags & NIF_FACHG) {
504 wmove(wnd, p->ni_line, FADDR);
505 inetprint((struct sockaddr *)&p->ni_fsa, p->ni_proto);
506 p->ni_flags &= ~NIF_FACHG;
509 family = (p->ni_lsa.ss_family == AF_INET) ? "4" : "6";
511 snprintf(proto, sizeof(proto), "%s%s", p->ni_proto, family);
512 mvwaddstr(wnd, p->ni_line, PROTO, proto);
513 mvwprintw(wnd, p->ni_line, RCVCC, "%6u", p->ni_rcvcc);
514 mvwprintw(wnd, p->ni_line, SNDCC, "%6u", p->ni_sndcc);
515 if (streq(p->ni_proto, "tcp")) {
516 if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
517 mvwprintw(wnd, p->ni_line, STATE, "%d",
520 mvwaddstr(wnd, p->ni_line, STATE,
521 tcpstates[p->ni_state]);
525 if (lastrow < YMAX(wnd)) {
526 wmove(wnd, lastrow, 0); wclrtobot(wnd);
527 wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd); /* XXX */
532 * Pretty print an Internet address (net address + port).
533 * If the nflag was specified, use numbers instead of names.
536 inetprint(struct sockaddr *sa, const char *proto)
538 struct servent *sp = 0;
542 switch (sa->sa_family) {
544 port = ((struct sockaddr_in *)sa)->sin_port;
548 port = ((struct sockaddr_in6 *)sa)->sin6_port;
555 snprintf(line, sizeof(line), "%.*s.", 16, inetname(sa));
556 cp = strchr(line, '\0');
558 sp = getservbyport(port, proto);
560 snprintf(cp, sizeof(line) - (cp - line), "%.8s",
561 sp ? sp->s_name : "*");
563 snprintf(cp, sizeof(line) - (cp - line), "%d",
564 ntohs((u_short)port));
565 /* pad to full column to clear any garbage */
566 cp = strchr(line, '\0');
567 while (cp - line < 22)
574 * Construct an Internet address representation.
575 * If the nflag has been supplied, give
576 * numeric value, otherwise try for symbolic name.
579 inetname(struct sockaddr *sa)
582 static char line[NI_MAXHOST];
588 if (sa->sa_family == AF_INET6) {
589 if (memcmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
590 &in6addr_any, sizeof(in6addr_any)) == 0)
593 getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
594 nflag ? NI_NUMERICHOST : 0);
599 in = ((struct sockaddr_in *)sa)->sin_addr;
600 if (!nflag && in.s_addr != INADDR_ANY) {
601 int net = inet_netof(in);
602 int lna = inet_lnaof(in);
604 if (lna == INADDR_ANY) {
605 np = getnetbyaddr(net, AF_INET);
610 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
615 if (in.s_addr == INADDR_ANY)
618 snprintf(line, sizeof(line), "%s", cp);
620 in.s_addr = ntohl(in.s_addr);
621 #define C(x) ((x) & 0xff)
622 snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
623 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
629 cmdnetstat(const char *cmd, const char *args)
631 if (prefix(cmd, "all")) {
635 if (prefix(cmd, "numbers") || prefix(cmd, "names")) {
639 new = prefix(cmd, "numbers");
642 TAILQ_FOREACH(p, &netcb, chain) {
643 if (p->ni_line == -1)
645 p->ni_flags |= NIF_LACHG|NIF_FACHG;
650 if (!netcmd(cmd, args))