129839Swollman/*-
229839Swollman * Copyright (c) 1980, 1992, 1993
329839Swollman *	The Regents of the University of California.  All rights reserved.
429839Swollman *
529839Swollman * Redistribution and use in source and binary forms, with or without
629839Swollman * modification, are permitted provided that the following conditions
729839Swollman * are met:
829839Swollman * 1. Redistributions of source code must retain the above copyright
929839Swollman *    notice, this list of conditions and the following disclaimer.
1029839Swollman * 2. Redistributions in binary form must reproduce the above copyright
1129839Swollman *    notice, this list of conditions and the following disclaimer in the
1229839Swollman *    documentation and/or other materials provided with the distribution.
1329839Swollman * 4. Neither the name of the University nor the names of its contributors
1429839Swollman *    may be used to endorse or promote products derived from this software
1529839Swollman *    without specific prior written permission.
1629839Swollman *
1729839Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1829839Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1929839Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2029839Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2129839Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2229839Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2329839Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2429839Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2529839Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2629839Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2729839Swollman * SUCH DAMAGE.
2829839Swollman */
2929839Swollman
3087715Smarkm#include <sys/cdefs.h>
3187715Smarkm
3287715Smarkm__FBSDID("$FreeBSD$");
3387715Smarkm
3487715Smarkm#ifdef lint
3587715Smarkmstatic const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
3687715Smarkm#endif
3787715Smarkm
3829839Swollman/* From:
3987715Smarkm	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
4029839Swollman*/
4129839Swollman
4229839Swollman#include <sys/param.h>
4329839Swollman#include <sys/types.h>
4429839Swollman#include <sys/socket.h>
4529839Swollman#include <sys/sysctl.h>
4629839Swollman
4729839Swollman#include <netinet/in.h>
4829839Swollman#include <netinet/in_systm.h>
4929839Swollman#include <netinet/ip.h>
5029839Swollman#include <netinet/ip_var.h>
5129839Swollman#include <netinet/udp.h>
5229839Swollman#include <netinet/udp_var.h>
5329839Swollman
54200462Sdelphij#include <stdlib.h>
55200462Sdelphij#include <string.h>
56200462Sdelphij#include <paths.h>
57200462Sdelphij
5829839Swollman#include "systat.h"
5929839Swollman#include "extern.h"
6029839Swollman#include "mode.h"
6129839Swollman
6229839Swollmanstruct stat {
6329839Swollman	struct ipstat i;
6429839Swollman	struct udpstat u;
6529839Swollman};
6629839Swollman
6729839Swollmanstatic struct stat curstat, initstat, oldstat;
6829839Swollman
6929839Swollman/*-
7029839Swollman--0         1         2         3         4         5         6         7
7129839Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
72158160Sbde00          IP Input                           IP Output
73158160Sbde01999999999 total packets received   999999999 total packets sent
74158160Sbde02999999999 - with bad checksums     999999999 - generated locally
75158160Sbde03999999999 - too short for header   999999999 - output drops
76158160Sbde04999999999 - too short for data     999999999 output fragments generated
77158160Sbde05999999999 - with invalid hlen      999999999 - fragmentation failed
78158160Sbde06999999999 - with invalid length    999999999 destinations unreachable
79158160Sbde07999999999 - with invalid version   999999999 packets output via raw IP
80158160Sbde08999999999 - jumbograms
81158160Sbde09999999999 total fragments received           UDP Statistics
82158160Sbde10999999999 - fragments dropped      999999999 total input packets
83158160Sbde11999999999 - fragments timed out    999999999 - too short for header
84158160Sbde12999999999 - packets reassembled ok 999999999 - invalid checksum
85158160Sbde13999999999 packets forwarded        999999999 - no checksum
86158160Sbde14999999999 - unreachable dests      999999999 - invalid length
87158160Sbde15999999999 - redirects generated    999999999 - no socket for dest port
88158160Sbde16999999999 option errors            999999999 - no socket for broadcast
89158160Sbde17999999999 unwanted multicasts      999999999 - socket buffer full
90158160Sbde18999999999 delivered to upper layer 999999999 total output packets
9129839Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
9229839Swollman--0         1         2         3         4         5         6         7
9329839Swollman*/
9429839Swollman
9529839SwollmanWINDOW *
9629839Swollmanopenip(void)
9729839Swollman{
98158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
9929839Swollman}
10029839Swollman
10129839Swollmanvoid
102175387Sdelphijcloseip(WINDOW *w)
10329839Swollman{
10429839Swollman	if (w == NULL)
10529839Swollman		return;
10629839Swollman	wclear(w);
10729839Swollman	wrefresh(w);
10829839Swollman	delwin(w);
10929839Swollman}
11029839Swollman
11129839Swollmanvoid
11229839Swollmanlabelip(void)
11329839Swollman{
11429839Swollman	wmove(wnd, 0, 0); wclrtoeol(wnd);
11529839Swollman#define L(row, str) mvwprintw(wnd, row, 10, str)
11629839Swollman#define R(row, str) mvwprintw(wnd, row, 45, str);
117158160Sbde	L(0, "IP Input");		R(0, "IP Output");
118158160Sbde	L(1, "total packets received");	R(1, "total packets sent");
119158160Sbde	L(2, "- with bad checksums");	R(2, "- generated locally");
120158160Sbde	L(3, "- too short for header");	R(3, "- output drops");
121158160Sbde	L(4, "- too short for data");	R(4, "output fragments generated");
122158160Sbde	L(5, "- with invalid hlen");	R(5, "- fragmentation failed");
123158160Sbde	L(6, "- with invalid length");	R(6, "destinations unreachable");
124158160Sbde	L(7, "- with invalid version");	R(7, "packets output via raw IP");
125158160Sbde	L(8, "- jumbograms");
126158160Sbde	L(9, "total fragments received");	R(9, "UDP Statistics");
127158160Sbde	L(10, "- fragments dropped");	R(10, "total input packets");
128158160Sbde	L(11, "- fragments timed out");	R(11, "- too short for header");
129158160Sbde	L(12, "- packets reassembled ok");	R(12, "- invalid checksum");
130158160Sbde	L(13, "packets forwarded");	R(13, "- no checksum");
131158160Sbde	L(14, "- unreachable dests");	R(14, "- invalid length");
132158160Sbde	L(15, "- redirects generated");	R(15, "- no socket for dest port");
133158160Sbde	L(16, "option errors");		R(16, "- no socket for broadcast");
134158160Sbde	L(17, "unwanted multicasts");	R(17, "- socket buffer full");
135158160Sbde	L(18, "delivered to upper layer");	R(18, "total output packets");
13629839Swollman#undef L
13729839Swollman#undef R
13829839Swollman}
13929839Swollman
14029839Swollmanstatic void
14129839Swollmandomode(struct stat *ret)
14229839Swollman{
14329839Swollman	const struct stat *sub;
14440060Sobrien	int divisor = 1;
14529839Swollman
14629839Swollman	switch(currentmode) {
14729839Swollman	case display_RATE:
14829839Swollman		sub = &oldstat;
149240605Smelifaro		divisor = (delay > 1000000) ? delay / 1000000 : 1;
15029839Swollman		break;
15129839Swollman	case display_DELTA:
15229839Swollman		sub = &oldstat;
15329839Swollman		break;
15429839Swollman	case display_SINCE:
15529839Swollman		sub = &initstat;
15629839Swollman		break;
15729839Swollman	default:
15829839Swollman		*ret = curstat;
15929839Swollman		return;
16029839Swollman	}
16129839Swollman#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16229839Swollman	DO(i.ips_total);
16329839Swollman	DO(i.ips_badsum);
16429839Swollman	DO(i.ips_tooshort);
16529839Swollman	DO(i.ips_toosmall);
16629839Swollman	DO(i.ips_badhlen);
16729839Swollman	DO(i.ips_badlen);
16829839Swollman	DO(i.ips_fragments);
16929839Swollman	DO(i.ips_fragdropped);
17029839Swollman	DO(i.ips_fragtimeout);
17129839Swollman	DO(i.ips_forward);
17229839Swollman	DO(i.ips_cantforward);
17329839Swollman	DO(i.ips_redirectsent);
17429839Swollman	DO(i.ips_noproto);
17529839Swollman	DO(i.ips_delivered);
17629839Swollman	DO(i.ips_localout);
17729839Swollman	DO(i.ips_odropped);
17829839Swollman	DO(i.ips_reassembled);
17929839Swollman	DO(i.ips_fragmented);
18029839Swollman	DO(i.ips_ofragments);
18129839Swollman	DO(i.ips_cantfrag);
18229839Swollman	DO(i.ips_badoptions);
18329839Swollman	DO(i.ips_noroute);
18429839Swollman	DO(i.ips_badvers);
18529839Swollman	DO(i.ips_rawout);
18629839Swollman	DO(i.ips_toolong);
18729839Swollman	DO(i.ips_notmember);
18829839Swollman	DO(u.udps_ipackets);
18929839Swollman	DO(u.udps_hdrops);
19029839Swollman	DO(u.udps_badsum);
19174209Sru	DO(u.udps_nosum);
19229839Swollman	DO(u.udps_badlen);
19329839Swollman	DO(u.udps_noport);
19429839Swollman	DO(u.udps_noportbcast);
19529839Swollman	DO(u.udps_fullsock);
19629839Swollman	DO(u.udps_opackets);
19729839Swollman#undef DO
19829839Swollman}
199158161Sbde
20029839Swollmanvoid
20129839Swollmanshowip(void)
20229839Swollman{
20329839Swollman	struct stat stats;
20429839Swollman	u_long totalout;
20529839Swollman
20629839Swollman	domode(&stats);
20729839Swollman	totalout = stats.i.ips_forward + stats.i.ips_localout;
20829839Swollman
20929839Swollman#define DO(stat, row, col) \
21029839Swollman	mvwprintw(wnd, row, col, "%9lu", stats.stat)
21129839Swollman
212158160Sbde	DO(i.ips_total, 1, 0);
213158160Sbde	mvwprintw(wnd, 1, 35, "%9lu", totalout);
214158160Sbde	DO(i.ips_badsum, 2, 0);
215158160Sbde	DO(i.ips_localout, 2, 35);
216158160Sbde	DO(i.ips_tooshort, 3, 0);
217158160Sbde	DO(i.ips_odropped, 3, 35);
218158160Sbde	DO(i.ips_toosmall, 4, 0);
219158160Sbde	DO(i.ips_ofragments, 4, 35);
220158160Sbde	DO(i.ips_badhlen, 5, 0);
221158160Sbde	DO(i.ips_cantfrag, 5, 35);
222158160Sbde	DO(i.ips_badlen, 6, 0);
223158160Sbde	DO(i.ips_noroute, 6, 35);
224158160Sbde	DO(i.ips_badvers, 7, 0);
225158160Sbde	DO(i.ips_rawout, 7, 35);
226158160Sbde	DO(i.ips_toolong, 8, 0);
227158160Sbde	DO(i.ips_fragments, 9, 0);
228158160Sbde	DO(i.ips_fragdropped, 10, 0);
229158160Sbde	DO(u.udps_ipackets, 10, 35);
230158160Sbde	DO(i.ips_fragtimeout, 11, 0);
231158160Sbde	DO(u.udps_hdrops, 11, 35);
232158160Sbde	DO(i.ips_reassembled, 12, 0);
233158160Sbde	DO(u.udps_badsum, 12, 35);
234158160Sbde	DO(i.ips_forward, 13, 0);
235158160Sbde	DO(u.udps_nosum, 13, 35);
236158160Sbde	DO(i.ips_cantforward, 14, 0);
237158160Sbde	DO(u.udps_badlen, 14, 35);
238158160Sbde	DO(i.ips_redirectsent, 15, 0);
239158160Sbde	DO(u.udps_noport, 15, 35);
240158160Sbde	DO(i.ips_badoptions, 16, 0);
241158160Sbde	DO(u.udps_noportbcast, 16, 35);
242158160Sbde	DO(i.ips_notmember, 17, 0);
243158160Sbde	DO(u.udps_fullsock, 17, 35);
244158160Sbde	DO(i.ips_delivered, 18, 0);
245158160Sbde	DO(u.udps_opackets, 18, 35);
24629839Swollman#undef DO
24729839Swollman}
24829839Swollman
24929839Swollmanint
25029839Swollmaninitip(void)
25129839Swollman{
25229839Swollman	size_t len;
25329839Swollman	int name[4];
25429839Swollman
25529839Swollman	name[0] = CTL_NET;
25629839Swollman	name[1] = PF_INET;
25729839Swollman	name[2] = IPPROTO_IP;
25829839Swollman	name[3] = IPCTL_STATS;
25929839Swollman
26029839Swollman	len = 0;
26129839Swollman	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
26229839Swollman		error("sysctl getting ipstat size failed");
26329839Swollman		return 0;
26429839Swollman	}
26529839Swollman	if (len > sizeof curstat.i) {
26629839Swollman		error("ipstat structure has grown--recompile systat!");
26729839Swollman		return 0;
26829839Swollman	}
26929839Swollman	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
27029839Swollman		error("sysctl getting ipstat failed");
27129839Swollman		return 0;
27229839Swollman	}
27329839Swollman	name[2] = IPPROTO_UDP;
27429839Swollman	name[3] = UDPCTL_STATS;
27529839Swollman
27629839Swollman	len = 0;
27729839Swollman	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
27829839Swollman		error("sysctl getting udpstat size failed");
27929839Swollman		return 0;
28029839Swollman	}
28129839Swollman	if (len > sizeof curstat.u) {
28229839Swollman		error("ipstat structure has grown--recompile systat!");
28329839Swollman		return 0;
28429839Swollman	}
28529839Swollman	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
28629839Swollman		error("sysctl getting udpstat failed");
28729839Swollman		return 0;
28829839Swollman	}
28929839Swollman	oldstat = initstat;
29029839Swollman	return 1;
29129839Swollman}
29229839Swollman
29329839Swollmanvoid
29429839Swollmanresetip(void)
29529839Swollman{
29629839Swollman	size_t len;
29729839Swollman	int name[4];
29829839Swollman
29929839Swollman	name[0] = CTL_NET;
30029839Swollman	name[1] = PF_INET;
30129839Swollman	name[2] = IPPROTO_IP;
30229839Swollman	name[3] = IPCTL_STATS;
30329839Swollman
30429839Swollman	len = sizeof initstat.i;
30529839Swollman	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
30629839Swollman		error("sysctl getting ipstat failed");
30729839Swollman	}
30829839Swollman	name[2] = IPPROTO_UDP;
30929839Swollman	name[3] = UDPCTL_STATS;
31029839Swollman
31129839Swollman	len = sizeof initstat.u;
31229839Swollman	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
31329839Swollman		error("sysctl getting udpstat failed");
31429839Swollman	}
31529839Swollman	oldstat = initstat;
31629839Swollman}
31729839Swollman
31829839Swollmanvoid
31929839Swollmanfetchip(void)
32029839Swollman{
32129839Swollman	int name[4];
32229839Swollman	size_t len;
32329839Swollman
32429839Swollman	oldstat = curstat;
32529839Swollman	name[0] = CTL_NET;
32629839Swollman	name[1] = PF_INET;
32729839Swollman	name[2] = IPPROTO_IP;
32829839Swollman	name[3] = IPCTL_STATS;
32929839Swollman	len = sizeof curstat.i;
33029839Swollman
33129839Swollman	if (sysctl(name, 4, &curstat.i, &len, 0, 0) < 0)
33229839Swollman		return;
33329839Swollman	name[2] = IPPROTO_UDP;
33429839Swollman	name[3] = UDPCTL_STATS;
33529839Swollman	len = sizeof curstat.u;
33629839Swollman
33729839Swollman	if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0)
33829839Swollman		return;
33929839Swollman}
340