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