aboutsummaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorGleb Smirnoff <glebius@FreeBSD.org>2017-03-21 06:39:49 +0000
committerGleb Smirnoff <glebius@FreeBSD.org>2017-03-21 06:39:49 +0000
commitcc65eb4e792a203a8b4a17b0e3af7831e5044eca (patch)
tree969a970e8707f4144afbe92a15b95806217ec748 /usr.bin
parent5a33a2afb3a35949545a572cffa0b8e637446d4a (diff)
downloadsrc-cc65eb4e792a203a8b4a17b0e3af7831e5044eca.tar.gz
src-cc65eb4e792a203a8b4a17b0e3af7831e5044eca.zip
Notes
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/netstat/inet.c170
-rw-r--r--usr.bin/sockstat/sockstat.c53
-rw-r--r--usr.bin/systat/extern.h6
-rw-r--r--usr.bin/systat/netcmds.c10
-rw-r--r--usr.bin/systat/netstat.c65
5 files changed, 90 insertions, 214 deletions
diff --git a/usr.bin/netstat/inet.c b/usr.bin/netstat/inet.c
index 2b05ef2f80d9..40256ada49e1 100644
--- a/usr.bin/netstat/inet.c
+++ b/usr.bin/netstat/inet.c
@@ -91,7 +91,7 @@ static int udp_done, tcp_done, sdp_done;
#endif /* INET6 */
static int
-pcblist_sysctl(int proto, const char *name, char **bufp, int istcp __unused)
+pcblist_sysctl(int proto, const char *name, char **bufp)
{
const char *mibvar;
char *buf;
@@ -181,120 +181,6 @@ sotoxsocket(struct socket *so, struct xsocket *xso)
return (0);
}
-static int
-pcblist_kvm(u_long off, char **bufp, int istcp)
-{
- struct inpcbinfo pcbinfo;
- struct inpcbhead listhead;
- struct inpcb *inp;
- struct xinpcb xi;
- struct xinpgen xig;
- struct xtcpcb xt;
- struct socket so;
- struct xsocket *xso;
- char *buf, *p;
- size_t len;
-
- if (off == 0)
- return (0);
- kread(off, &pcbinfo, sizeof(pcbinfo));
- if (istcp)
- len = 2 * sizeof(xig) +
- (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
- sizeof(struct xtcpcb);
- else
- len = 2 * sizeof(xig) +
- (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
- sizeof(struct xinpcb);
- if ((buf = malloc(len)) == NULL) {
- xo_warnx("malloc %lu bytes", (u_long)len);
- return (0);
- }
- p = buf;
-
-#define COPYOUT(obj, size) do { \
- if (len < (size)) { \
- xo_warnx("buffer size exceeded"); \
- goto fail; \
- } \
- bcopy((obj), p, (size)); \
- len -= (size); \
- p += (size); \
-} while (0)
-
-#define KREAD(off, buf, len) do { \
- if (kread((uintptr_t)(off), (buf), (len)) != 0) \
- goto fail; \
-} while (0)
-
- /* Write out header. */
- xig.xig_len = sizeof xig;
- xig.xig_count = pcbinfo.ipi_count;
- xig.xig_gen = pcbinfo.ipi_gencnt;
- xig.xig_sogen = 0;
- COPYOUT(&xig, sizeof xig);
-
- /* Walk the PCB list. */
- xt.xt_len = sizeof xt;
- xi.xi_len = sizeof xi;
- if (istcp)
- xso = &xt.xt_socket;
- else
- xso = &xi.xi_socket;
- KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
- LIST_FOREACH(inp, &listhead, inp_list) {
- if (istcp) {
- KREAD(inp, &xt.xt_inp, sizeof(*inp));
- inp = &xt.xt_inp;
- } else {
- KREAD(inp, &xi.xi_inp, sizeof(*inp));
- inp = &xi.xi_inp;
- }
-
- if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
- continue;
-
- if (istcp) {
- if (inp->inp_ppcb == NULL)
- bzero(&xt.xt_tp, sizeof xt.xt_tp);
- else if (inp->inp_flags & INP_TIMEWAIT) {
- bzero(&xt.xt_tp, sizeof xt.xt_tp);
- xt.xt_tp.t_state = TCPS_TIME_WAIT;
- } else
- KREAD(inp->inp_ppcb, &xt.xt_tp,
- sizeof xt.xt_tp);
- }
- if (inp->inp_socket) {
- KREAD(inp->inp_socket, &so, sizeof(so));
- if (sotoxsocket(&so, xso) != 0)
- goto fail;
- } else {
- bzero(xso, sizeof(*xso));
- if (istcp)
- xso->xso_protocol = IPPROTO_TCP;
- }
- if (istcp)
- COPYOUT(&xt, sizeof xt);
- else
- COPYOUT(&xi, sizeof xi);
- }
-
- /* Reread the pcbinfo and write out the footer. */
- kread(off, &pcbinfo, sizeof(pcbinfo));
- xig.xig_count = pcbinfo.ipi_count;
- xig.xig_gen = pcbinfo.ipi_gencnt;
- COPYOUT(&xig, sizeof xig);
-
- *bufp = buf;
- return (1);
-
-fail:
- free(buf);
- return (0);
-#undef COPYOUT
-#undef KREAD
-}
-
/*
* Print a summary of connections related to an Internet
* protocol. For TCP, also give state of connection.
@@ -304,15 +190,14 @@ fail:
void
protopr(u_long off, const char *name, int af1, int proto)
{
- int istcp;
static int first = 1;
+ int istcp;
char *buf;
const char *vchar;
- struct tcpcb *tp = NULL;
- struct inpcb *inp;
+ struct xtcpcb *tp;
+ struct xinpcb *inp;
struct xinpgen *xig, *oxig;
struct xsocket *so;
- struct xtcp_timer *timer;
istcp = 0;
switch (proto) {
@@ -341,28 +226,21 @@ protopr(u_long off, const char *name, int af1, int proto)
#endif
break;
}
- if (live) {
- if (!pcblist_sysctl(proto, name, &buf, istcp))
- return;
- } else {
- if (!pcblist_kvm(off, &buf, istcp))
- return;
- }
+
+ if (!pcblist_sysctl(proto, name, &buf))
+ return;
oxig = xig = (struct xinpgen *)buf;
for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
xig->xig_len > sizeof(struct xinpgen);
xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
if (istcp) {
- timer = &((struct xtcpcb *)xig)->xt_timer;
- tp = &((struct xtcpcb *)xig)->xt_tp;
- inp = &((struct xtcpcb *)xig)->xt_inp;
- so = &((struct xtcpcb *)xig)->xt_socket;
+ tp = (struct xtcpcb *)xig;
+ inp = &tp->xt_inp;
} else {
- inp = &((struct xinpcb *)xig)->xi_inp;
- so = &((struct xinpcb *)xig)->xi_socket;
- timer = NULL;
+ inp = (struct xinpcb *)xig;
}
+ so = &inp->xi_socket;
/* Ignore sockets for protocols other than the desired one. */
if (so->xso_protocol != proto)
@@ -574,25 +452,25 @@ protopr(u_long off, const char *name, int af1, int proto)
so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
- if (timer != NULL)
+ if (istcp)
xo_emit(" {:retransmit-timer/%4d.%02d} "
"{:persist-timer/%4d.%02d} "
"{:keepalive-timer/%4d.%02d} "
"{:msl2-timer/%4d.%02d} "
"{:delay-ack-timer/%4d.%02d} "
"{:inactivity-timer/%4d.%02d}",
- timer->tt_rexmt / 1000,
- (timer->tt_rexmt % 1000) / 10,
- timer->tt_persist / 1000,
- (timer->tt_persist % 1000) / 10,
- timer->tt_keep / 1000,
- (timer->tt_keep % 1000) / 10,
- timer->tt_2msl / 1000,
- (timer->tt_2msl % 1000) / 10,
- timer->tt_delack / 1000,
- (timer->tt_delack % 1000) / 10,
- timer->t_rcvtime / 1000,
- (timer->t_rcvtime % 1000) / 10);
+ tp->tt_rexmt / 1000,
+ (tp->tt_rexmt % 1000) / 10,
+ tp->tt_persist / 1000,
+ (tp->tt_persist % 1000) / 10,
+ tp->tt_keep / 1000,
+ (tp->tt_keep % 1000) / 10,
+ tp->tt_2msl / 1000,
+ (tp->tt_2msl % 1000) / 10,
+ tp->tt_delack / 1000,
+ (tp->tt_delack % 1000) / 10,
+ tp->t_rcvtime / 1000,
+ (tp->t_rcvtime % 1000) / 10);
}
if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c
index dea195c387b1..97add95457a8 100644
--- a/usr.bin/sockstat/sockstat.c
+++ b/usr.bin/sockstat/sockstat.c
@@ -562,7 +562,6 @@ gather_inet(int proto)
struct xinpgen *xig, *exig;
struct xinpcb *xip;
struct xtcpcb *xtp;
- struct inpcb *inp;
struct xsocket *so;
struct sock *sock;
struct addr *laddr, *faddr;
@@ -625,54 +624,52 @@ gather_inet(int proto)
xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
if (xig >= exig)
break;
- xip = (struct xinpcb *)xig;
- xtp = (struct xtcpcb *)xig;
switch (proto) {
case IPPROTO_TCP:
+ xtp = (struct xtcpcb *)xig;
+ xip = &xtp->xt_inp;
if (xtp->xt_len != sizeof(*xtp)) {
warnx("struct xtcpcb size mismatch");
goto out;
}
- inp = &xtp->xt_inp;
- so = &xtp->xt_socket;
- protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
+ protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
break;
case IPPROTO_UDP:
case IPPROTO_DIVERT:
+ xip = (struct xinpcb *)xig;
if (xip->xi_len != sizeof(*xip)) {
warnx("struct xinpcb size mismatch");
goto out;
}
- inp = &xip->xi_inp;
- so = &xip->xi_socket;
break;
default:
errx(1, "protocol %d not supported", proto);
}
- if ((inp->inp_vflag & vflag) == 0)
+ so = &xip->xi_socket;
+ if ((xip->inp_vflag & vflag) == 0)
continue;
- if (inp->inp_vflag & INP_IPV4) {
- if ((inp->inp_fport == 0 && !opt_l) ||
- (inp->inp_fport != 0 && !opt_c))
+ if (xip->inp_vflag & INP_IPV4) {
+ if ((xip->inp_fport == 0 && !opt_l) ||
+ (xip->inp_fport != 0 && !opt_c))
continue;
#define __IN_IS_ADDR_LOOPBACK(pina) \
((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
if (opt_L &&
- (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
- __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
+ (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
+ __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
continue;
#undef __IN_IS_ADDR_LOOPBACK
- } else if (inp->inp_vflag & INP_IPV6) {
- if ((inp->inp_fport == 0 && !opt_l) ||
- (inp->inp_fport != 0 && !opt_c))
+ } else if (xip->inp_vflag & INP_IPV6) {
+ if ((xip->inp_fport == 0 && !opt_l) ||
+ (xip->inp_fport != 0 && !opt_c))
continue;
if (opt_L &&
- (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
- IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
+ (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
+ IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
continue;
} else {
if (opt_v)
- warnx("invalid vflag 0x%x", inp->inp_vflag);
+ warnx("invalid vflag 0x%x", xip->inp_vflag);
continue;
}
if ((sock = calloc(1, sizeof(*sock))) == NULL)
@@ -683,26 +680,26 @@ gather_inet(int proto)
err(1, "malloc()");
sock->socket = so->xso_so;
sock->proto = proto;
- if (inp->inp_vflag & INP_IPV4) {
+ if (xip->inp_vflag & INP_IPV4) {
sock->family = AF_INET;
sockaddr(&laddr->address, sock->family,
- &inp->inp_laddr, inp->inp_lport);
+ &xip->inp_laddr, xip->inp_lport);
sockaddr(&faddr->address, sock->family,
- &inp->inp_faddr, inp->inp_fport);
- } else if (inp->inp_vflag & INP_IPV6) {
+ &xip->inp_faddr, xip->inp_fport);
+ } else if (xip->inp_vflag & INP_IPV6) {
sock->family = AF_INET6;
sockaddr(&laddr->address, sock->family,
- &inp->in6p_laddr, inp->inp_lport);
+ &xip->in6p_laddr, xip->inp_lport);
sockaddr(&faddr->address, sock->family,
- &inp->in6p_faddr, inp->inp_fport);
+ &xip->in6p_faddr, xip->inp_fport);
}
laddr->next = NULL;
faddr->next = NULL;
sock->laddr = laddr;
sock->faddr = faddr;
- sock->vflag = inp->inp_vflag;
+ sock->vflag = xip->inp_vflag;
if (proto == IPPROTO_TCP)
- sock->state = xtp->xt_tp.t_state;
+ sock->state = xtp->t_state;
sock->protoname = protoname;
hash = (int)((uintptr_t)sock->socket % HASHSIZE);
sock->next = sockhash[hash];
diff --git a/usr.bin/systat/extern.h b/usr.bin/systat/extern.h
index 1e214df3d08d..5d92f88f4b66 100644
--- a/usr.bin/systat/extern.h
+++ b/usr.bin/systat/extern.h
@@ -56,7 +56,7 @@ extern int protos;
extern int verbose;
extern unsigned int delay;
-struct inpcb;
+struct in_conninfo;
extern struct device_selection *dev_select;
extern long generation;
@@ -67,8 +67,8 @@ extern long select_generation;
extern struct nlist namelist[];
-int checkhost(struct inpcb *);
-int checkport(struct inpcb *);
+int checkhost(struct in_conninfo *);
+int checkport(struct in_conninfo *);
void closeicmp(WINDOW *);
void closeicmp6(WINDOW *);
void closeifstat(WINDOW *);
diff --git a/usr.bin/systat/netcmds.c b/usr.bin/systat/netcmds.c
index 084496804479..f8ff64a35c99 100644
--- a/usr.bin/systat/netcmds.c
+++ b/usr.bin/systat/netcmds.c
@@ -224,13 +224,13 @@ selectport(long port, int onoff)
}
int
-checkport(struct inpcb *inp)
+checkport(struct in_conninfo *inc)
{
struct pitem *p;
if (ports)
for (p = ports; p < ports+nports; p++)
- if (p->port == inp->inp_lport || p->port == inp->inp_fport)
+ if (p->port == inc->inc_lport || p->port == inc->inc_fport)
return (p->onoff);
return (1);
}
@@ -281,14 +281,14 @@ selecthost(struct in_addr *in, int onoff)
}
int
-checkhost(struct inpcb *inp)
+checkhost(struct in_conninfo *inc)
{
struct hitem *p;
if (hosts)
for (p = hosts; p < hosts+nhosts; p++)
- if (p->addr.s_addr == inp->inp_laddr.s_addr ||
- p->addr.s_addr == inp->inp_faddr.s_addr)
+ if (p->addr.s_addr == inc->inc_laddr.s_addr ||
+ p->addr.s_addr == inc->inc_faddr.s_addr)
return (p->onoff);
return (1);
}
diff --git a/usr.bin/systat/netstat.c b/usr.bin/systat/netstat.c
index 0ac2244b25b2..5ab367a0a878 100644
--- a/usr.bin/systat/netstat.c
+++ b/usr.bin/systat/netstat.c
@@ -52,6 +52,7 @@ static const char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
#ifdef INET6
#include <netinet/ip6.h>
#endif
+#define _WANT_INPCB
#include <netinet/in_pcb.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
@@ -62,6 +63,7 @@ static const char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
#define TCPSTATES
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_timer.h>
+#define _WANT_TCPCB
#include <netinet/tcp_var.h>
#include <netinet/tcp_debug.h>
#include <netinet/udp.h>
@@ -76,9 +78,9 @@ static const char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
#include "systat.h"
#include "extern.h"
-static struct netinfo *enter(struct inpcb *, int, const char *);
+static struct netinfo *enter(struct in_conninfo *, uint8_t, int, const char *);
static void enter_kvm(struct inpcb *, struct socket *, int, const char *);
-static void enter_sysctl(struct inpcb *, struct xsocket *, int, const char *);
+static void enter_sysctl(struct xinpcb *, struct xsocket *, int, const char *);
static void fetchnetstat_kvm(void);
static void fetchnetstat_sysctl(void);
static char *inetname(struct sockaddr *);
@@ -212,9 +214,9 @@ again:
}
#endif
}
- if (nhosts && !checkhost(&inpcb))
+ if (nhosts && !checkhost(&inpcb.inp_inc))
continue;
- if (nports && !checkport(&inpcb))
+ if (nports && !checkport(&inpcb.inp_inc))
continue;
if (istcp) {
if (inpcb.inp_flags & INP_TIMEWAIT) {
@@ -245,7 +247,6 @@ fetchnetstat_sysctl(void)
int idx;
struct xinpgen *inpg;
char *cur, *end;
- struct inpcb *inpcb;
struct xinpcb *xip = NULL;
struct xtcpcb *xtp = NULL;
int plen;
@@ -291,37 +292,36 @@ fetchnetstat_sysctl(void)
while (cur + plen <= end) {
if (idx == 0) { /* TCP */
xtp = (struct xtcpcb *)cur;
- inpcb = &xtp->xt_inp;
+ xip = &xtp->xt_inp;
} else {
xip = (struct xinpcb *)cur;
- inpcb = &xip->xi_inp;
}
cur += plen;
if (!aflag) {
- if (inpcb->inp_vflag & INP_IPV4) {
- if (inet_lnaof(inpcb->inp_laddr) ==
+ if (xip->inp_vflag & INP_IPV4) {
+ if (inet_lnaof(xip->inp_laddr) ==
INADDR_ANY)
continue;
}
#ifdef INET6
- else if (inpcb->inp_vflag & INP_IPV6) {
- if (memcmp(&inpcb->in6p_laddr,
+ else if (xip->inp_vflag & INP_IPV6) {
+ if (memcmp(&xip->in6p_laddr,
&in6addr_any, sizeof(in6addr_any))
== 0)
continue;
}
#endif
}
- if (nhosts && !checkhost(inpcb))
+ if (nhosts && !checkhost(&xip->inp_inc))
continue;
- if (nports && !checkport(inpcb))
+ if (nports && !checkport(&xip->inp_inc))
continue;
- if (idx == 0) /* TCP */
- enter_sysctl(inpcb, &xtp->xt_socket,
- xtp->xt_tp.t_state, "tcp");
- else /* UDP */
- enter_sysctl(inpcb, &xip->xi_socket, 0, "udp");
+ if (idx == 0)
+ enter_sysctl(xip, &xip->xi_socket,
+ xtp->t_state, "tcp");
+ else
+ enter_sysctl(xip, &xip->xi_socket, 0, "udp");
}
free(inpg);
}
@@ -332,25 +332,26 @@ enter_kvm(struct inpcb *inp, struct socket *so, int state, const char *proto)
{
struct netinfo *p;
- if ((p = enter(inp, state, proto)) != NULL) {
+ if ((p = enter(&inp->inp_inc, inp->inp_vflag, state, proto)) != NULL) {
p->ni_rcvcc = so->so_rcv.sb_ccc;
p->ni_sndcc = so->so_snd.sb_ccc;
}
}
static void
-enter_sysctl(struct inpcb *inp, struct xsocket *so, int state, const char *proto)
+enter_sysctl(struct xinpcb *xip, struct xsocket *so, int state,
+ const char *proto)
{
struct netinfo *p;
- if ((p = enter(inp, state, proto)) != NULL) {
+ if ((p = enter(&xip->inp_inc, xip->inp_vflag, state, proto)) != NULL) {
p->ni_rcvcc = so->so_rcv.sb_cc;
p->ni_sndcc = so->so_snd.sb_cc;
}
}
static struct netinfo *
-enter(struct inpcb *inp, int state, const char *proto)
+enter(struct in_conninfo *inc, uint8_t vflag, int state, const char *proto)
{
struct netinfo *p;
struct sockaddr_storage lsa, fsa;
@@ -361,32 +362,32 @@ enter(struct inpcb *inp, int state, const char *proto)
memset(&lsa, 0, sizeof(lsa));
memset(&fsa, 0, sizeof(fsa));
- if (inp->inp_vflag & INP_IPV4) {
+ if (vflag & INP_IPV4) {
sa4 = (struct sockaddr_in *)&lsa;
- sa4->sin_addr = inp->inp_laddr;
- sa4->sin_port = inp->inp_lport;
+ sa4->sin_addr = inc->inc_laddr;
+ sa4->sin_port = inc->inc_lport;
sa4->sin_family = AF_INET;
sa4->sin_len = sizeof(struct sockaddr_in);
sa4 = (struct sockaddr_in *)&fsa;
- sa4->sin_addr = inp->inp_faddr;
- sa4->sin_port = inp->inp_fport;
+ sa4->sin_addr = inc->inc_faddr;
+ sa4->sin_port = inc->inc_fport;
sa4->sin_family = AF_INET;
sa4->sin_len = sizeof(struct sockaddr_in);
}
#ifdef INET6
- else if (inp->inp_vflag & INP_IPV6) {
+ else if (vflag & INP_IPV6) {
sa6 = (struct sockaddr_in6 *)&lsa;
- memcpy(&sa6->sin6_addr, &inp->in6p_laddr,
+ memcpy(&sa6->sin6_addr, &inc->inc6_laddr,
sizeof(struct in6_addr));
- sa6->sin6_port = inp->inp_lport;
+ sa6->sin6_port = inc->inc_lport;
sa6->sin6_family = AF_INET6;
sa6->sin6_len = sizeof(struct sockaddr_in6);
sa6 = (struct sockaddr_in6 *)&fsa;
- memcpy(&sa6->sin6_addr, &inp->in6p_faddr,
+ memcpy(&sa6->sin6_addr, &inc->inc6_faddr,
sizeof(struct in6_addr));
- sa6->sin6_port = inp->inp_fport;
+ sa6->sin6_port = inc->inc_fport;
sa6->sin6_family = AF_INET6;
sa6->sin6_len = sizeof(struct sockaddr_in6);
}