xref: /freebsd/usr.bin/systat/icmp.c (revision 93b9f50404f9ef0fc00a5ed39ebe09b7cf4086bb)
103e00a72SGarrett Wollman /*-
203e00a72SGarrett Wollman  * Copyright (c) 1980, 1992, 1993
303e00a72SGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
403e00a72SGarrett Wollman  *
503e00a72SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
603e00a72SGarrett Wollman  * modification, are permitted provided that the following conditions
703e00a72SGarrett Wollman  * are met:
803e00a72SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
903e00a72SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1003e00a72SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1103e00a72SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1203e00a72SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1303e00a72SGarrett Wollman  * 3. All advertising materials mentioning features or use of this software
1403e00a72SGarrett Wollman  *    must display the following acknowledgement:
1503e00a72SGarrett Wollman  *	This product includes software developed by the University of
1603e00a72SGarrett Wollman  *	California, Berkeley and its contributors.
1703e00a72SGarrett Wollman  * 4. Neither the name of the University nor the names of its contributors
1803e00a72SGarrett Wollman  *    may be used to endorse or promote products derived from this software
1903e00a72SGarrett Wollman  *    without specific prior written permission.
2003e00a72SGarrett Wollman  *
2103e00a72SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2203e00a72SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2303e00a72SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2403e00a72SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2503e00a72SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2603e00a72SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2703e00a72SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2803e00a72SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2903e00a72SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3003e00a72SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3103e00a72SGarrett Wollman  * SUCH DAMAGE.
3203e00a72SGarrett Wollman  */
3303e00a72SGarrett Wollman 
349ff712b0SMark Murray #include <sys/cdefs.h>
359ff712b0SMark Murray 
369ff712b0SMark Murray __FBSDID("$FreeBSD$");
379ff712b0SMark Murray 
389ff712b0SMark Murray #ifdef lint
3903e00a72SGarrett Wollman static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
409ff712b0SMark Murray #endif
419ff712b0SMark Murray 
429ff712b0SMark Murray /* From:
439ff712b0SMark Murray 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
4403e00a72SGarrett Wollman */
4503e00a72SGarrett Wollman 
4603e00a72SGarrett Wollman #include <sys/param.h>
4703e00a72SGarrett Wollman #include <sys/types.h>
4803e00a72SGarrett Wollman #include <sys/socket.h>
4903e00a72SGarrett Wollman #include <sys/sysctl.h>
5003e00a72SGarrett Wollman 
5103e00a72SGarrett Wollman #include <netinet/in.h>
5203e00a72SGarrett Wollman #include <netinet/in_systm.h>
5303e00a72SGarrett Wollman #include <netinet/ip.h>
5403e00a72SGarrett Wollman #include <netinet/ip_icmp.h>
5503e00a72SGarrett Wollman #include <netinet/icmp_var.h>
5603e00a72SGarrett Wollman 
5703e00a72SGarrett Wollman #include <stdlib.h>
5803e00a72SGarrett Wollman #include <string.h>
5903e00a72SGarrett Wollman #include <paths.h>
6003e00a72SGarrett Wollman #include "systat.h"
6103e00a72SGarrett Wollman #include "extern.h"
6203e00a72SGarrett Wollman #include "mode.h"
6303e00a72SGarrett Wollman 
6403e00a72SGarrett Wollman static struct icmpstat icmpstat, initstat, oldstat;
6503e00a72SGarrett Wollman 
6603e00a72SGarrett Wollman /*-
6703e00a72SGarrett Wollman --0         1         2         3         4         5         6         7
6803e00a72SGarrett Wollman --0123456789012345678901234567890123456789012345678901234567890123456789012345
698aa22952SBruce Evans 00          ICMP Input                         ICMP Output
708aa22952SBruce Evans 01999999999 total messages           999999999 total messages
718aa22952SBruce Evans 02999999999 with bad code            999999999 errors generated
728aa22952SBruce Evans 03999999999 with bad length          999999999 suppressed - original too short
738aa22952SBruce Evans 04999999999 with bad checksum        999999999 suppressed - original was ICMP
748aa22952SBruce Evans 05999999999 with insufficient data   999999999 responses sent
758aa22952SBruce Evans 06                                   999999999 suppressed - multicast echo
768aa22952SBruce Evans 07                                   999999999 suppressed - multicast tstamp
778aa22952SBruce Evans 08
788aa22952SBruce Evans 09          Input Histogram                    Output Histogram
798aa22952SBruce Evans 10999999999 echo response            999999999 echo response
808aa22952SBruce Evans 11999999999 echo request             999999999 echo request
818aa22952SBruce Evans 12999999999 destination unreachable  999999999 destination unreachable
828aa22952SBruce Evans 13999999999 redirect                 999999999 redirect
838aa22952SBruce Evans 14999999999 time-to-live exceeded    999999999 time-to-line exceeded
848aa22952SBruce Evans 15999999999 parameter problem        999999999 parameter problem
858aa22952SBruce Evans 16999999999 router advertisement     999999999 router solicitation
868aa22952SBruce Evans 17
8703e00a72SGarrett Wollman 18
8803e00a72SGarrett Wollman --0123456789012345678901234567890123456789012345678901234567890123456789012345
8903e00a72SGarrett Wollman --0         1         2         3         4         5         6         7
9003e00a72SGarrett Wollman */
9103e00a72SGarrett Wollman 
9203e00a72SGarrett Wollman WINDOW *
9303e00a72SGarrett Wollman openicmp(void)
9403e00a72SGarrett Wollman {
958aa22952SBruce Evans 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
9603e00a72SGarrett Wollman }
9703e00a72SGarrett Wollman 
9803e00a72SGarrett Wollman void
9993b9f504SXin LI closeicmp(WINDOW *w)
10003e00a72SGarrett Wollman {
10103e00a72SGarrett Wollman 	if (w == NULL)
10203e00a72SGarrett Wollman 		return;
10303e00a72SGarrett Wollman 	wclear(w);
10403e00a72SGarrett Wollman 	wrefresh(w);
10503e00a72SGarrett Wollman 	delwin(w);
10603e00a72SGarrett Wollman }
10703e00a72SGarrett Wollman 
10803e00a72SGarrett Wollman void
10903e00a72SGarrett Wollman labelicmp(void)
11003e00a72SGarrett Wollman {
11103e00a72SGarrett Wollman 	wmove(wnd, 0, 0); wclrtoeol(wnd);
11203e00a72SGarrett Wollman #define L(row, str) mvwprintw(wnd, row, 10, str)
11303e00a72SGarrett Wollman #define R(row, str) mvwprintw(wnd, row, 45, str);
1148aa22952SBruce Evans 	L(0, "ICMP Input");		R(0, "ICMP Output");
1158aa22952SBruce Evans 	L(1, "total messages");		R(1, "total messages");
1168aa22952SBruce Evans 	L(2, "with bad code");		R(2, "errors generated");
1178aa22952SBruce Evans 	L(3, "with bad length");	R(3, "suppressed - original too short");
1188aa22952SBruce Evans 	L(4, "with bad checksum");	R(4, "suppressed - original was ICMP");
1198aa22952SBruce Evans 	L(5, "with insufficient data");	R(5, "responses sent");
1208aa22952SBruce Evans 					R(6, "suppressed - multicast echo");
1218aa22952SBruce Evans 					R(7, "suppressed - multicast tstamp");
1228aa22952SBruce Evans 	L(9, "Input Histogram");	R(9, "Output Histogram");
12303e00a72SGarrett Wollman #define B(row, str) L(row, str); R(row, str)
1248aa22952SBruce Evans 	B(10, "echo response");
1258aa22952SBruce Evans 	B(11, "echo request");
1268aa22952SBruce Evans 	B(12, "destination unreachable");
1278aa22952SBruce Evans 	B(13, "redirect");
1288aa22952SBruce Evans 	B(14, "time-to-live exceeded");
1298aa22952SBruce Evans 	B(15, "parameter problem");
1308aa22952SBruce Evans 	L(16, "router advertisement");	R(16, "router solicitation");
13103e00a72SGarrett Wollman #undef L
13203e00a72SGarrett Wollman #undef R
13303e00a72SGarrett Wollman #undef B
13403e00a72SGarrett Wollman }
13503e00a72SGarrett Wollman 
13603e00a72SGarrett Wollman static void
13703e00a72SGarrett Wollman domode(struct icmpstat *ret)
13803e00a72SGarrett Wollman {
13903e00a72SGarrett Wollman 	const struct icmpstat *sub;
14003e00a72SGarrett Wollman 	int i, divisor = 1;
14103e00a72SGarrett Wollman 
14203e00a72SGarrett Wollman 	switch(currentmode) {
14303e00a72SGarrett Wollman 	case display_RATE:
14403e00a72SGarrett Wollman 		sub = &oldstat;
14503e00a72SGarrett Wollman 		divisor = naptime;
14603e00a72SGarrett Wollman 		break;
14703e00a72SGarrett Wollman 	case display_DELTA:
14803e00a72SGarrett Wollman 		sub = &oldstat;
14903e00a72SGarrett Wollman 		break;
15003e00a72SGarrett Wollman 	case display_SINCE:
15103e00a72SGarrett Wollman 		sub = &initstat;
15203e00a72SGarrett Wollman 		break;
15303e00a72SGarrett Wollman 	default:
15403e00a72SGarrett Wollman 		*ret = icmpstat;
15503e00a72SGarrett Wollman 		return;
15603e00a72SGarrett Wollman 	}
15703e00a72SGarrett Wollman #define DO(stat) ret->stat = (icmpstat.stat - sub->stat) / divisor
15803e00a72SGarrett Wollman 	DO(icps_error);
15903e00a72SGarrett Wollman 	DO(icps_oldshort);
16003e00a72SGarrett Wollman 	DO(icps_oldicmp);
16103e00a72SGarrett Wollman 	for (i = 0; i <= ICMP_MAXTYPE; i++) {
16203e00a72SGarrett Wollman 		DO(icps_outhist[i]);
16303e00a72SGarrett Wollman 	}
16403e00a72SGarrett Wollman 	DO(icps_badcode);
16503e00a72SGarrett Wollman 	DO(icps_tooshort);
16603e00a72SGarrett Wollman 	DO(icps_checksum);
16703e00a72SGarrett Wollman 	DO(icps_badlen);
16803e00a72SGarrett Wollman 	DO(icps_reflect);
16903e00a72SGarrett Wollman 	for (i = 0; i <= ICMP_MAXTYPE; i++) {
17003e00a72SGarrett Wollman 		DO(icps_inhist[i]);
17103e00a72SGarrett Wollman 	}
17203e00a72SGarrett Wollman 	DO(icps_bmcastecho);
17303e00a72SGarrett Wollman 	DO(icps_bmcasttstamp);
17403e00a72SGarrett Wollman #undef DO
17503e00a72SGarrett Wollman }
17603e00a72SGarrett Wollman 
17703e00a72SGarrett Wollman void
17803e00a72SGarrett Wollman showicmp(void)
17903e00a72SGarrett Wollman {
18003e00a72SGarrett Wollman 	struct icmpstat stats;
18103e00a72SGarrett Wollman 	u_long totalin, totalout;
18203e00a72SGarrett Wollman 	int i;
18303e00a72SGarrett Wollman 
18403e00a72SGarrett Wollman 	memset(&stats, 0, sizeof stats);
18503e00a72SGarrett Wollman 	domode(&stats);
18603e00a72SGarrett Wollman 	for (i = totalin = totalout = 0; i <= ICMP_MAXTYPE; i++) {
18703e00a72SGarrett Wollman 		totalin += stats.icps_inhist[i];
18803e00a72SGarrett Wollman 		totalout += stats.icps_outhist[i];
18903e00a72SGarrett Wollman 	}
19003e00a72SGarrett Wollman 	totalin += stats.icps_badcode + stats.icps_badlen +
19103e00a72SGarrett Wollman 		stats.icps_checksum + stats.icps_tooshort;
1928aa22952SBruce Evans 	mvwprintw(wnd, 1, 0, "%9lu", totalin);
1938aa22952SBruce Evans 	mvwprintw(wnd, 1, 35, "%9lu", totalout);
19403e00a72SGarrett Wollman 
19503e00a72SGarrett Wollman #define DO(stat, row, col) \
19603e00a72SGarrett Wollman 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
19703e00a72SGarrett Wollman 
1988aa22952SBruce Evans 	DO(icps_badcode, 2, 0);
1998aa22952SBruce Evans 	DO(icps_badlen, 3, 0);
2008aa22952SBruce Evans 	DO(icps_checksum, 4, 0);
2018aa22952SBruce Evans 	DO(icps_tooshort, 5, 0);
2028aa22952SBruce Evans 	DO(icps_error, 2, 35);
2038aa22952SBruce Evans 	DO(icps_oldshort, 3, 35);
2048aa22952SBruce Evans 	DO(icps_oldicmp, 4, 35);
2058aa22952SBruce Evans 	DO(icps_reflect, 5, 35);
2068aa22952SBruce Evans 	DO(icps_bmcastecho, 6, 35);
2078aa22952SBruce Evans 	DO(icps_bmcasttstamp, 7, 35);
20803e00a72SGarrett Wollman #define DO2(type, row) DO(icps_inhist[type], row, 0); DO(icps_outhist[type], \
20903e00a72SGarrett Wollman 							 row, 35)
2108aa22952SBruce Evans 	DO2(ICMP_ECHOREPLY, 10);
2118aa22952SBruce Evans 	DO2(ICMP_ECHO, 11);
2128aa22952SBruce Evans 	DO2(ICMP_UNREACH, 12);
2138aa22952SBruce Evans 	DO2(ICMP_REDIRECT, 13);
2148aa22952SBruce Evans 	DO2(ICMP_TIMXCEED, 14);
2158aa22952SBruce Evans 	DO2(ICMP_PARAMPROB, 15);
2168aa22952SBruce Evans 	DO(icps_inhist[ICMP_ROUTERADVERT], 16, 0);
2178aa22952SBruce Evans 	DO(icps_outhist[ICMP_ROUTERSOLICIT], 16, 35);
21803e00a72SGarrett Wollman #undef DO
21903e00a72SGarrett Wollman #undef DO2
22003e00a72SGarrett Wollman }
22103e00a72SGarrett Wollman 
22203e00a72SGarrett Wollman int
22303e00a72SGarrett Wollman initicmp(void)
22403e00a72SGarrett Wollman {
22503e00a72SGarrett Wollman 	size_t len;
22603e00a72SGarrett Wollman 	int name[4];
22703e00a72SGarrett Wollman 
22803e00a72SGarrett Wollman 	name[0] = CTL_NET;
22903e00a72SGarrett Wollman 	name[1] = PF_INET;
23003e00a72SGarrett Wollman 	name[2] = IPPROTO_ICMP;
23103e00a72SGarrett Wollman 	name[3] = ICMPCTL_STATS;
23203e00a72SGarrett Wollman 
23303e00a72SGarrett Wollman 	len = 0;
23403e00a72SGarrett Wollman 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
23503e00a72SGarrett Wollman 		error("sysctl getting icmpstat size failed");
23603e00a72SGarrett Wollman 		return 0;
23703e00a72SGarrett Wollman 	}
23803e00a72SGarrett Wollman 	if (len > sizeof icmpstat) {
23903e00a72SGarrett Wollman 		error("icmpstat structure has grown--recompile systat!");
24003e00a72SGarrett Wollman 		return 0;
24103e00a72SGarrett Wollman 	}
24203e00a72SGarrett Wollman 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
24303e00a72SGarrett Wollman 		error("sysctl getting icmpstat size failed");
24403e00a72SGarrett Wollman 		return 0;
24503e00a72SGarrett Wollman 	}
24603e00a72SGarrett Wollman 	oldstat = initstat;
24703e00a72SGarrett Wollman 	return 1;
24803e00a72SGarrett Wollman }
24903e00a72SGarrett Wollman 
25003e00a72SGarrett Wollman void
25103e00a72SGarrett Wollman reseticmp(void)
25203e00a72SGarrett Wollman {
25303e00a72SGarrett Wollman 	size_t len;
25403e00a72SGarrett Wollman 	int name[4];
25503e00a72SGarrett Wollman 
25603e00a72SGarrett Wollman 	name[0] = CTL_NET;
25703e00a72SGarrett Wollman 	name[1] = PF_INET;
25803e00a72SGarrett Wollman 	name[2] = IPPROTO_ICMP;
25903e00a72SGarrett Wollman 	name[3] = ICMPCTL_STATS;
26003e00a72SGarrett Wollman 
26103e00a72SGarrett Wollman 	len = sizeof initstat;
26203e00a72SGarrett Wollman 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
26303e00a72SGarrett Wollman 		error("sysctl getting icmpstat size failed");
26403e00a72SGarrett Wollman 	}
26503e00a72SGarrett Wollman 	oldstat = initstat;
26603e00a72SGarrett Wollman }
26703e00a72SGarrett Wollman 
26803e00a72SGarrett Wollman void
26903e00a72SGarrett Wollman fetchicmp(void)
27003e00a72SGarrett Wollman {
27103e00a72SGarrett Wollman 	int name[4];
27203e00a72SGarrett Wollman 	size_t len;
27303e00a72SGarrett Wollman 
27403e00a72SGarrett Wollman 	oldstat = icmpstat;
27503e00a72SGarrett Wollman 	name[0] = CTL_NET;
27603e00a72SGarrett Wollman 	name[1] = PF_INET;
27703e00a72SGarrett Wollman 	name[2] = IPPROTO_ICMP;
27803e00a72SGarrett Wollman 	name[3] = ICMPCTL_STATS;
27903e00a72SGarrett Wollman 	len = sizeof icmpstat;
28003e00a72SGarrett Wollman 
28103e00a72SGarrett Wollman 	if (sysctl(name, 4, &icmpstat, &len, 0, 0) < 0)
28203e00a72SGarrett Wollman 		return;
28303e00a72SGarrett Wollman }
28403e00a72SGarrett Wollman 
285