xref: /freebsd/usr.bin/systat/icmp.c (revision 2eb4d8dc723da3cf7d735a3226ae49da4c8c5dbc)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef lint
37 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38 #endif
39 
40 /* From:
41 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
42 */
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_icmp.h>
53 #include <netinet/icmp_var.h>
54 
55 #include <stdlib.h>
56 #include <string.h>
57 #include <paths.h>
58 #include "systat.h"
59 #include "extern.h"
60 #include "mode.h"
61 
62 static struct icmpstat icmpstat, initstat, oldstat;
63 
64 /*-
65 --0         1         2         3         4         5         6         7
66 --0123456789012345678901234567890123456789012345678901234567890123456789012345
67 00          ICMP Input                         ICMP Output
68 01999999999 total messages           999999999 total messages
69 02999999999 with bad code            999999999 errors generated
70 03999999999 with bad length          999999999 suppressed - original too short
71 04999999999 with bad checksum        999999999 suppressed - original was ICMP
72 05999999999 with insufficient data   999999999 responses sent
73 06                                   999999999 suppressed - multicast echo
74 07                                   999999999 suppressed - multicast tstamp
75 08
76 09          Input Histogram                    Output Histogram
77 10999999999 echo response            999999999 echo response
78 11999999999 echo request             999999999 echo request
79 12999999999 destination unreachable  999999999 destination unreachable
80 13999999999 redirect                 999999999 redirect
81 14999999999 time-to-live exceeded    999999999 time-to-line exceeded
82 15999999999 parameter problem        999999999 parameter problem
83 16999999999 router advertisement     999999999 router solicitation
84 17
85 18
86 --0123456789012345678901234567890123456789012345678901234567890123456789012345
87 --0         1         2         3         4         5         6         7
88 */
89 
90 WINDOW *
91 openicmp(void)
92 {
93 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
94 }
95 
96 void
97 closeicmp(WINDOW *w)
98 {
99 	if (w == NULL)
100 		return;
101 	wclear(w);
102 	wrefresh(w);
103 	delwin(w);
104 }
105 
106 void
107 labelicmp(void)
108 {
109 	wmove(wnd, 0, 0); wclrtoeol(wnd);
110 #define L(row, str) mvwprintw(wnd, row, 10, str)
111 #define R(row, str) mvwprintw(wnd, row, 45, str);
112 	L(0, "ICMP Input");		R(0, "ICMP Output");
113 	L(1, "total messages");		R(1, "total messages");
114 	L(2, "with bad code");		R(2, "errors generated");
115 	L(3, "with bad length");	R(3, "suppressed - original too short");
116 	L(4, "with bad checksum");	R(4, "suppressed - original was ICMP");
117 	L(5, "with insufficient data");	R(5, "responses sent");
118 					R(6, "suppressed - multicast echo");
119 					R(7, "suppressed - multicast tstamp");
120 	L(9, "Input Histogram");	R(9, "Output Histogram");
121 #define B(row, str) L(row, str); R(row, str)
122 	B(10, "echo response");
123 	B(11, "echo request");
124 	B(12, "destination unreachable");
125 	B(13, "redirect");
126 	B(14, "time-to-live exceeded");
127 	B(15, "parameter problem");
128 	L(16, "router advertisement");	R(16, "router solicitation");
129 #undef L
130 #undef R
131 #undef B
132 }
133 
134 static void
135 domode(struct icmpstat *ret)
136 {
137 	const struct icmpstat *sub;
138 	int i, divisor = 1;
139 
140 	switch(currentmode) {
141 	case display_RATE:
142 		sub = &oldstat;
143 		divisor = (delay > 1000000) ? delay / 1000000 : 1;
144 		break;
145 	case display_DELTA:
146 		sub = &oldstat;
147 		break;
148 	case display_SINCE:
149 		sub = &initstat;
150 		break;
151 	default:
152 		*ret = icmpstat;
153 		return;
154 	}
155 #define DO(stat) ret->stat = (icmpstat.stat - sub->stat) / divisor
156 	DO(icps_error);
157 	DO(icps_oldshort);
158 	DO(icps_oldicmp);
159 	for (i = 0; i <= ICMP_MAXTYPE; i++) {
160 		DO(icps_outhist[i]);
161 	}
162 	DO(icps_badcode);
163 	DO(icps_tooshort);
164 	DO(icps_checksum);
165 	DO(icps_badlen);
166 	DO(icps_reflect);
167 	for (i = 0; i <= ICMP_MAXTYPE; i++) {
168 		DO(icps_inhist[i]);
169 	}
170 	DO(icps_bmcastecho);
171 	DO(icps_bmcasttstamp);
172 #undef DO
173 }
174 
175 void
176 showicmp(void)
177 {
178 	struct icmpstat stats;
179 	u_long totalin, totalout;
180 	int i;
181 
182 	memset(&stats, 0, sizeof stats);
183 	domode(&stats);
184 	for (i = totalin = totalout = 0; i <= ICMP_MAXTYPE; i++) {
185 		totalin += stats.icps_inhist[i];
186 		totalout += stats.icps_outhist[i];
187 	}
188 	totalin += stats.icps_badcode + stats.icps_badlen +
189 		stats.icps_checksum + stats.icps_tooshort;
190 	mvwprintw(wnd, 1, 0, "%9lu", totalin);
191 	mvwprintw(wnd, 1, 35, "%9lu", totalout);
192 
193 #define DO(stat, row, col) \
194 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
195 
196 	DO(icps_badcode, 2, 0);
197 	DO(icps_badlen, 3, 0);
198 	DO(icps_checksum, 4, 0);
199 	DO(icps_tooshort, 5, 0);
200 	DO(icps_error, 2, 35);
201 	DO(icps_oldshort, 3, 35);
202 	DO(icps_oldicmp, 4, 35);
203 	DO(icps_reflect, 5, 35);
204 	DO(icps_bmcastecho, 6, 35);
205 	DO(icps_bmcasttstamp, 7, 35);
206 #define DO2(type, row) DO(icps_inhist[type], row, 0); DO(icps_outhist[type], \
207 							 row, 35)
208 	DO2(ICMP_ECHOREPLY, 10);
209 	DO2(ICMP_ECHO, 11);
210 	DO2(ICMP_UNREACH, 12);
211 	DO2(ICMP_REDIRECT, 13);
212 	DO2(ICMP_TIMXCEED, 14);
213 	DO2(ICMP_PARAMPROB, 15);
214 	DO(icps_inhist[ICMP_ROUTERADVERT], 16, 0);
215 	DO(icps_outhist[ICMP_ROUTERSOLICIT], 16, 35);
216 #undef DO
217 #undef DO2
218 }
219 
220 int
221 initicmp(void)
222 {
223 	size_t len;
224 	int name[4];
225 
226 	name[0] = CTL_NET;
227 	name[1] = PF_INET;
228 	name[2] = IPPROTO_ICMP;
229 	name[3] = ICMPCTL_STATS;
230 
231 	len = 0;
232 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
233 		error("sysctl getting icmpstat size failed");
234 		return 0;
235 	}
236 	if (len > sizeof icmpstat) {
237 		error("icmpstat structure has grown--recompile systat!");
238 		return 0;
239 	}
240 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
241 		error("sysctl getting icmpstat size failed");
242 		return 0;
243 	}
244 	oldstat = initstat;
245 	return 1;
246 }
247 
248 void
249 reseticmp(void)
250 {
251 	size_t len;
252 	int name[4];
253 
254 	name[0] = CTL_NET;
255 	name[1] = PF_INET;
256 	name[2] = IPPROTO_ICMP;
257 	name[3] = ICMPCTL_STATS;
258 
259 	len = sizeof initstat;
260 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
261 		error("sysctl getting icmpstat size failed");
262 	}
263 	oldstat = initstat;
264 }
265 
266 void
267 fetchicmp(void)
268 {
269 	int name[4];
270 	size_t len;
271 
272 	oldstat = icmpstat;
273 	name[0] = CTL_NET;
274 	name[1] = PF_INET;
275 	name[2] = IPPROTO_ICMP;
276 	name[3] = ICMPCTL_STATS;
277 	len = sizeof icmpstat;
278 
279 	if (sysctl(name, 4, &icmpstat, &len, 0, 0) < 0)
280 		return;
281 }
282