xref: /dragonfly/usr.bin/systat/icmp6.c (revision 7bc7e232)
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  * @(#)mbufs.c     8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/systat/icmp6.c,v 1.3 2006/04/30 04:47:23 bde Exp $
35  * $DragonFly: src/usr.bin/systat/icmp6.c,v 1.1 2007/05/31 11:38:36 hasso Exp $
36  */
37 
38 #include <sys/cdefs.h>
39 
40 #ifdef INET6
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/icmp6.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 icmp6stat icmp6stat, initstat, oldstat;
57 
58 /*-
59 --0         1         2         3         4         5         6         7
60 --0123456789012345678901234567890123456789012345678901234567890123456789012345
61 00          ICMPv6 Input                       ICMPv6 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
68 07          Input Histogram                    Output Histogram
69 08999999999 echo response            999999999 echo response
70 09999999999 echo request             999999999 echo request
71 10999999999 destination unreachable  999999999 destination unreachable
72 11999999999 redirect                 999999999 redirect
73 12999999999 time-to-live exceeded    999999999 time-to-line exceeded
74 13999999999 parameter problem        999999999 parameter problem
75 14999999999 neighbor solicitation    999999999 neighbor solicitation
76 15999999999 neighbor advertisment    999999999 neighbor advertisment
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 openicmp6(void)
86 {
87 	return (subwin(stdscr, LINES-4-1, 0, 4, 0));
88 }
89 
90 void
91 closeicmp6(w)
92 	WINDOW *w;
93 {
94 	if (w == NULL)
95 		return;
96 	wclear(w);
97 	wrefresh(w);
98 	delwin(w);
99 }
100 
101 void
102 labelicmp6(void)
103 {
104 	wmove(wnd, 0, 0); wclrtoeol(wnd);
105 #define L(row, str) mvwprintw(wnd, row, 10, str)
106 #define R(row, str) mvwprintw(wnd, row, 45, str);
107 	L(1, "ICMPv6 Input");		R(1, "ICMPv6 Output");
108 	L(2, "total messages");		R(2, "total messages");
109 	L(3, "with bad code");		R(3, "errors generated");
110 	L(4, "with bad length");	R(4, "suppressed - original too short");
111 	L(5, "with bad checksum");	R(5, "suppressed - original was ICMP");
112 	L(6, "with insufficient data");	R(6, "responses sent");
113 
114 	L(8, "Input Histogram");	R(8, "Output Histogram");
115 #define B(row, str) L(row, str); R(row, str)
116 	B(9, "echo response");
117 	B(10, "echo request");
118 	B(11, "destination unreachable");
119 	B(12, "redirect");
120 	B(13, "time-to-live exceeded");
121 	B(14, "parameter problem");
122 	B(15, "neighbor solicitation");
123 	B(16, "neighbor advertisment");
124 	L(17, "router advertisement");	R(17, "router solicitation");
125 #undef L
126 #undef R
127 #undef B
128 }
129 
130 static void
131 domode(struct icmp6stat *ret)
132 {
133 	const struct icmp6stat *sub;
134 	int i, divisor = 1;
135 
136 	switch(currentmode) {
137 	case display_RATE:
138 		sub = &oldstat;
139 		divisor = naptime;
140 		break;
141 	case display_DELTA:
142 		sub = &oldstat;
143 		break;
144 	case display_SINCE:
145 		sub = &initstat;
146 		break;
147 	default:
148 		*ret = icmp6stat;
149 		return;
150 	}
151 #define DO(stat) ret->stat = (icmp6stat.stat - sub->stat) / divisor
152 	DO(icp6s_error);
153 	DO(icp6s_tooshort);
154 	DO(icp6s_canterror);
155 	for (i = 0; i <= ICMP6_MAXTYPE; i++) {
156 		DO(icp6s_outhist[i]);
157 	}
158 	DO(icp6s_badcode);
159 	DO(icp6s_tooshort);
160 	DO(icp6s_checksum);
161 	DO(icp6s_badlen);
162 	DO(icp6s_reflect);
163 	for (i = 0; i <= ICMP6_MAXTYPE; i++) {
164 		DO(icp6s_inhist[i]);
165 	}
166 #undef DO
167 }
168 
169 void
170 showicmp6(void)
171 {
172 	struct icmp6stat 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 <= ICMP6_MAXTYPE; i++) {
179 		totalin += stats.icp6s_inhist[i];
180 		totalout += stats.icp6s_outhist[i];
181 	}
182 	totalin += stats.icp6s_badcode + stats.icp6s_badlen +
183 		stats.icp6s_checksum + stats.icp6s_tooshort;
184 	mvwprintw(wnd, 2, 0, "%9lu", totalin);
185 	mvwprintw(wnd, 2, 35, "%9lu", totalout);
186 
187 #define DO(stat, row, col) \
188 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
189 
190 	DO(icp6s_badcode, 3, 0);
191 	DO(icp6s_badlen, 4, 0);
192 	DO(icp6s_checksum, 5, 0);
193 	DO(icp6s_tooshort, 6, 0);
194 	DO(icp6s_error, 3, 35);
195 	DO(icp6s_tooshort, 4, 35);
196 	DO(icp6s_canterror, 5, 35);
197 	DO(icp6s_reflect, 6, 35);
198 #define DO2(type, row) DO(icp6s_inhist[type], row, 0); DO(icp6s_outhist[type], \
199 							 row, 35)
200 	DO2(ICMP6_ECHO_REPLY, 9);
201 	DO2(ICMP6_ECHO_REQUEST, 10);
202 	DO2(ICMP6_DST_UNREACH, 11);
203 	DO2(ND_REDIRECT, 12);
204 	DO2(ICMP6_TIME_EXCEEDED, 13);
205 	DO2(ICMP6_PARAM_PROB, 14);
206 	DO2(ND_NEIGHBOR_SOLICIT, 15);
207 	DO2(ND_NEIGHBOR_ADVERT, 16);
208 	DO(icp6s_inhist[ND_ROUTER_SOLICIT], 17, 0);
209 	DO(icp6s_outhist[ND_ROUTER_ADVERT], 17, 35);
210 #undef DO
211 #undef DO2
212 }
213 
214 int
215 initicmp6(void)
216 {
217 	size_t len;
218 	int name[4];
219 
220 	name[0] = CTL_NET;
221 	name[1] = PF_INET6;
222 	name[2] = IPPROTO_ICMPV6;
223 	name[3] = ICMPV6CTL_STATS;
224 
225 	len = 0;
226 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
227 		error("sysctl getting icmp6stat size failed");
228 		return 0;
229 	}
230 	if (len > sizeof icmp6stat) {
231 		error("icmp6stat structure has grown--recompile systat!");
232 		return 0;
233 	}
234 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
235 		error("sysctl getting icmp6stat size failed");
236 		return 0;
237 	}
238 	oldstat = initstat;
239 	return 1;
240 }
241 
242 void
243 reseticmp6(void)
244 {
245 	size_t len;
246 	int name[4];
247 
248 	name[0] = CTL_NET;
249 	name[1] = PF_INET6;
250 	name[2] = IPPROTO_ICMPV6;
251 	name[3] = ICMPV6CTL_STATS;
252 
253 	len = sizeof initstat;
254 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
255 		error("sysctl getting icmp6stat size failed");
256 	}
257 	oldstat = initstat;
258 }
259 
260 void
261 fetchicmp6(void)
262 {
263 	int name[4];
264 	size_t len;
265 
266 	oldstat = icmp6stat;
267 	name[0] = CTL_NET;
268 	name[1] = PF_INET6;
269 	name[2] = IPPROTO_ICMPV6;
270 	name[3] = ICMPV6CTL_STATS;
271 	len = sizeof icmp6stat;
272 
273 	if (sysctl(name, 4, &icmp6stat, &len, 0, 0) < 0)
274 		return;
275 }
276 
277 #endif
278