xref: /freebsd/usr.bin/systat/icmp6.c (revision dba6dd177bdee890cf445fbe21a5dccefd5de18e)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifdef lint
39 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 
42 /* From:
43 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
44 */
45 
46 #ifdef INET6
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/icmp6.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 icmp6stat icmp6stat, initstat, oldstat;
63 
64 /*-
65 --0         1         2         3         4         5         6         7
66 --0123456789012345678901234567890123456789012345678901234567890123456789012345
67 01          ICMPv6 Input                       ICMPv6 Output
68 02999999999 total messages           999999999 total messages
69 03999999999 with bad code            999999999 errors generated
70 04999999999 with bad length          999999999 suppressed - original too short
71 05999999999 with bad checksum        999999999 suppressed - original was ICMP
72 06999999999 with insufficient data   999999999 responses sent
73 07
74 08          Input Histogram                    Output Histogram
75 09999999999 echo response            999999999 echo response
76 10999999999 echo request             999999999 echo request
77 11999999999 destination unreachable  999999999 destination unreachable
78 12999999999 redirect                 999999999 redirect
79 13999999999 time-to-live exceeded    999999999 time-to-line exceeded
80 14999999999 parameter problem        999999999 parameter problem
81 15999999999 neighbor solicitation    999999999 neighbor solicitation
82 16999999999 neighbor advertisment    999999999 neighbor advertisment
83 17999999999 router advertisement     999999999 router solicitation
84 18
85 19
86 --0123456789012345678901234567890123456789012345678901234567890123456789012345
87 --0         1         2         3         4         5         6         7
88 */
89 
90 WINDOW *
91 openicmp6(void)
92 {
93 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
94 }
95 
96 void
97 closeicmp6(w)
98 	WINDOW *w;
99 {
100 	if (w == NULL)
101 		return;
102 	wclear(w);
103 	wrefresh(w);
104 	delwin(w);
105 }
106 
107 void
108 labelicmp6(void)
109 {
110 	wmove(wnd, 0, 0); wclrtoeol(wnd);
111 #define L(row, str) mvwprintw(wnd, row, 10, str)
112 #define R(row, str) mvwprintw(wnd, row, 45, str);
113 	L(1, "ICMPv6 Input");		R(1, "ICMPv6 Output");
114 	L(2, "total messages");		R(2, "total messages");
115 	L(3, "with bad code");		R(3, "errors generated");
116 	L(4, "with bad length");	R(4, "suppressed - original too short");
117 	L(5, "with bad checksum");	R(5, "suppressed - original was ICMP");
118 	L(6, "with insufficient data");	R(6, "responses sent");
119 
120 	L(8, "Input Histogram");	R(8, "Output Histogram");
121 #define B(row, str) L(row, str); R(row, str)
122 	B(9, "echo response");
123 	B(10, "echo request");
124 	B(11, "destination unreachable");
125 	B(12, "redirect");
126 	B(13, "time-to-live exceeded");
127 	B(14, "parameter problem");
128 	B(15, "neighbor solicitation");
129 	B(16, "neighbor advertisment");
130 	L(17, "router advertisement");	R(17, "router solicitation");
131 #undef L
132 #undef R
133 #undef B
134 }
135 
136 static void
137 domode(struct icmp6stat *ret)
138 {
139 	const struct icmp6stat *sub;
140 	int i, divisor = 1;
141 
142 	switch(currentmode) {
143 	case display_RATE:
144 		sub = &oldstat;
145 		divisor = naptime;
146 		break;
147 	case display_DELTA:
148 		sub = &oldstat;
149 		break;
150 	case display_SINCE:
151 		sub = &initstat;
152 		break;
153 	default:
154 		*ret = icmp6stat;
155 		return;
156 	}
157 #define DO(stat) ret->stat = (icmp6stat.stat - sub->stat) / divisor
158 	DO(icp6s_error);
159 	DO(icp6s_tooshort);
160 	DO(icp6s_canterror);
161 	for (i = 0; i <= ICMP6_MAXTYPE; i++) {
162 		DO(icp6s_outhist[i]);
163 	}
164 	DO(icp6s_badcode);
165 	DO(icp6s_tooshort);
166 	DO(icp6s_checksum);
167 	DO(icp6s_badlen);
168 	DO(icp6s_reflect);
169 	for (i = 0; i <= ICMP6_MAXTYPE; i++) {
170 		DO(icp6s_inhist[i]);
171 	}
172 #undef DO
173 }
174 
175 void
176 showicmp6(void)
177 {
178 	struct icmp6stat 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 <= ICMP6_MAXTYPE; i++) {
185 		totalin += stats.icp6s_inhist[i];
186 		totalout += stats.icp6s_outhist[i];
187 	}
188 	totalin += stats.icp6s_badcode + stats.icp6s_badlen +
189 		stats.icp6s_checksum + stats.icp6s_tooshort;
190 	mvwprintw(wnd, 2, 0, "%9lu", totalin);
191 	mvwprintw(wnd, 2, 35, "%9lu", totalout);
192 
193 #define DO(stat, row, col) \
194 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
195 
196 	DO(icp6s_badcode, 3, 0);
197 	DO(icp6s_badlen, 4, 0);
198 	DO(icp6s_checksum, 5, 0);
199 	DO(icp6s_tooshort, 6, 0);
200 	DO(icp6s_error, 3, 35);
201 	DO(icp6s_tooshort, 4, 35);
202 	DO(icp6s_canterror, 5, 35);
203 	DO(icp6s_reflect, 6, 35);
204 #define DO2(type, row) DO(icp6s_inhist[type], row, 0); DO(icp6s_outhist[type], \
205 							 row, 35)
206 	DO2(ICMP6_ECHO_REPLY, 9);
207 	DO2(ICMP6_ECHO_REQUEST, 10);
208 	DO2(ICMP6_DST_UNREACH, 11);
209 	DO2(ND_REDIRECT, 12);
210 	DO2(ICMP6_TIME_EXCEEDED, 13);
211 	DO2(ICMP6_PARAM_PROB, 14);
212 	DO2(ND_NEIGHBOR_SOLICIT, 15);
213 	DO2(ND_NEIGHBOR_ADVERT, 16);
214 	DO(icp6s_inhist[ND_ROUTER_SOLICIT], 17, 0);
215 	DO(icp6s_outhist[ND_ROUTER_ADVERT], 17, 35);
216 #undef DO
217 #undef DO2
218 }
219 
220 int
221 initicmp6(void)
222 {
223 	size_t len;
224 	int name[4];
225 
226 	name[0] = CTL_NET;
227 	name[1] = PF_INET6;
228 	name[2] = IPPROTO_ICMPV6;
229 	name[3] = ICMPV6CTL_STATS;
230 
231 	len = 0;
232 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
233 		error("sysctl getting icmp6stat size failed");
234 		return 0;
235 	}
236 	if (len > sizeof icmp6stat) {
237 		error("icmp6stat structure has grown--recompile systat!");
238 		return 0;
239 	}
240 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
241 		error("sysctl getting icmp6stat size failed");
242 		return 0;
243 	}
244 	oldstat = initstat;
245 	return 1;
246 }
247 
248 void
249 reseticmp6(void)
250 {
251 	size_t len;
252 	int name[4];
253 
254 	name[0] = CTL_NET;
255 	name[1] = PF_INET6;
256 	name[2] = IPPROTO_ICMPV6;
257 	name[3] = ICMPV6CTL_STATS;
258 
259 	len = sizeof initstat;
260 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
261 		error("sysctl getting icmp6stat size failed");
262 	}
263 	oldstat = initstat;
264 }
265 
266 void
267 fetchicmp6(void)
268 {
269 	int name[4];
270 	size_t len;
271 
272 	oldstat = icmp6stat;
273 	name[0] = CTL_NET;
274 	name[1] = PF_INET6;
275 	name[2] = IPPROTO_ICMPV6;
276 	name[3] = ICMPV6CTL_STATS;
277 	len = sizeof icmp6stat;
278 
279 	if (sysctl(name, 4, &icmp6stat, &len, 0, 0) < 0)
280 		return;
281 }
282 
283 #endif
284