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