xref: /netbsd/usr.bin/systat/icmp.c (revision bf9ec67e)
1 /*	$NetBSD: icmp.c,v 1.6 2000/12/01 02:19:43 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2000 Andrew Doran <ad@NetBSD.org>
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: icmp.c,v 1.6 2000/12/01 02:19:43 simonb Exp $");
33 #endif /* not lint */
34 
35 #include <sys/param.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip_icmp.h>
41 #include <netinet/icmp_var.h>
42 
43 #include <kvm.h>
44 #include <string.h>
45 
46 #include "systat.h"
47 #include "extern.h"
48 
49 #define LHD(row, str) mvwprintw(wnd, row, 10, str)
50 #define RHD(row, str) mvwprintw(wnd, row, 45, str);
51 #define BD(row, str) LHD(row, str); RHD(row, str)
52 #define SHOW(stat, row, col) \
53     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat)
54 #define SHOW2(type, row) SHOW(icps_inhist[type], row, 0); \
55     SHOW(icps_outhist[type], row, 35)
56 
57 enum update {
58 	UPDATE_TIME,
59 	UPDATE_BOOT,
60 	UPDATE_RUN,
61 };
62 
63 static enum update update = UPDATE_TIME;
64 static struct icmpstat curstat;
65 static struct icmpstat newstat;
66 static struct icmpstat oldstat;
67 
68 static struct nlist namelist[] = {
69 	{ "_icmpstat" },
70 	{ "" }
71 };
72 
73 WINDOW *
74 openicmp(void)
75 {
76 
77 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
78 }
79 
80 void
81 closeicmp(WINDOW *w)
82 {
83 
84 	if (w != NULL) {
85 		wclear(w);
86 		wrefresh(w);
87 		delwin(w);
88 	}
89 }
90 
91 void
92 labelicmp(void)
93 {
94 
95 	wmove(wnd, 0, 0); wclrtoeol(wnd);
96 
97 	mvwprintw(wnd, 1, 0,  "------------ ICMP input -----------");
98 	mvwprintw(wnd, 1, 36, "------------- ICMP output ---------------");
99 
100 	mvwprintw(wnd, 8, 0,  "---------- Input histogram --------");
101 	mvwprintw(wnd, 8, 36, "---------- Output histogram -------------");
102 
103 	LHD(3, "with bad code");
104 	LHD(4, "with bad length");
105 	LHD(5, "with bad checksum");
106 	LHD(6, "with insufficient data");
107 
108 	RHD(3, "errors generated");
109 	RHD(4, "suppressed - original too short");
110 	RHD(5, "suppressed - original was ICMP");
111 	RHD(6, "responses sent");
112 
113 	BD(2, "total messages");
114 	BD(9, "echo response");
115 	BD(10, "echo request");
116 	BD(11, "destination unreachable");
117 	BD(12, "redirect");
118 	BD(13, "time-to-live exceeded");
119 	BD(14, "parameter problem");
120 	LHD(15, "router advertisement");
121 	RHD(15, "router solicitation");
122 }
123 
124 void
125 showicmp(void)
126 {
127 	u_long tin, tout;
128 	int i;
129 
130 	for (i = tin = tout = 0; i <= ICMP_MAXTYPE; i++) {
131 		tin += curstat.icps_inhist[i];
132 		tout += curstat.icps_outhist[i];
133 	}
134 
135 	tin += curstat.icps_badcode + curstat.icps_badlen +
136 	    curstat.icps_checksum + curstat.icps_tooshort;
137 	mvwprintw(wnd, 2, 0, "%9lu", tin);
138 	mvwprintw(wnd, 2, 35, "%9lu", tout);
139 
140 	SHOW(icps_badcode, 3, 0);
141 	SHOW(icps_badlen, 4, 0);
142 	SHOW(icps_checksum, 5, 0);
143 	SHOW(icps_tooshort, 6, 0);
144 	SHOW(icps_error, 3, 35);
145 	SHOW(icps_oldshort, 4, 35);
146 	SHOW(icps_oldicmp, 5, 35);
147 	SHOW(icps_reflect, 6, 35);
148 
149 	SHOW2(ICMP_ECHOREPLY, 9);
150 	SHOW2(ICMP_ECHO, 10);
151 	SHOW2(ICMP_UNREACH, 11);
152 	SHOW2(ICMP_REDIRECT, 12);
153 	SHOW2(ICMP_TIMXCEED, 13);
154 	SHOW2(ICMP_PARAMPROB, 14);
155 	SHOW(icps_inhist[ICMP_ROUTERADVERT], 15, 0);
156 	SHOW(icps_outhist[ICMP_ROUTERSOLICIT], 15, 35);
157 }
158 
159 int
160 initicmp(void)
161 {
162 
163 	if (namelist[0].n_type == 0) {
164 		if (kvm_nlist(kd, namelist)) {
165 			nlisterr(namelist);
166 			return(0);
167 		}
168 		if (namelist[0].n_type == 0) {
169 			error("No namelist");
170 			return(0);
171 		}
172 	}
173 
174 	return (1);
175 }
176 
177 void
178 fetchicmp(void)
179 {
180 	int i;
181 
182 	KREAD((void *)namelist[0].n_value, &newstat, sizeof(newstat));
183 
184 	ADJINETCTR(curstat, oldstat, newstat, icps_badcode);
185 	ADJINETCTR(curstat, oldstat, newstat, icps_badlen);
186 	ADJINETCTR(curstat, oldstat, newstat, icps_checksum);
187 	ADJINETCTR(curstat, oldstat, newstat, icps_tooshort);
188 	ADJINETCTR(curstat, oldstat, newstat, icps_error);
189 	ADJINETCTR(curstat, oldstat, newstat, icps_oldshort);
190 	ADJINETCTR(curstat, oldstat, newstat, icps_oldicmp);
191 	ADJINETCTR(curstat, oldstat, newstat, icps_reflect);
192 
193 	for (i = 0; i <= ICMP_MAXTYPE; i++) {
194 		ADJINETCTR(curstat, oldstat, newstat, icps_inhist[i]);
195 		ADJINETCTR(curstat, oldstat, newstat, icps_outhist[i]);
196 	}
197 
198 	if (update == UPDATE_TIME)
199 		memcpy(&oldstat, &newstat, sizeof(oldstat));
200 }
201 
202 void
203 icmp_boot(char *args)
204 {
205 
206 	memset(&oldstat, 0, sizeof(oldstat));
207 	update = UPDATE_BOOT;
208 }
209 
210 void
211 icmp_run(char *args)
212 {
213 
214 	if (update != UPDATE_RUN) {
215 		memcpy(&oldstat, &newstat, sizeof(oldstat));
216 		update = UPDATE_RUN;
217 	}
218 }
219 
220 void
221 icmp_time(char *args)
222 {
223 
224 	if (update != UPDATE_TIME) {
225 		memcpy(&oldstat, &newstat, sizeof(oldstat));
226 		update = UPDATE_TIME;
227 	}
228 }
229 
230 void
231 icmp_zero(char *args)
232 {
233 
234 	if (update == UPDATE_RUN)
235 		memcpy(&oldstat, &newstat, sizeof(oldstat));
236 }
237