xref: /freebsd/usr.bin/systat/ip6.c (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
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 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef lint
37 static const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38 #endif
39 
40 /* From:
41 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
42 */
43 
44 #ifdef INET6
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet6/ip6_var.h>
54 
55 #include <inttypes.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <paths.h>
59 
60 #include "systat.h"
61 #include "extern.h"
62 #include "mode.h"
63 
64 static struct ip6stat curstat, initstat, oldstat;
65 
66 /*-
67 --0         1         2         3         4         5         6         7
68 --0123456789012345678901234567890123456789012345678901234567890123456789012345
69 00        IPv6 Input                         IPv6 Output
70 019999999 total packets received   999999999 total packets sent
71 029999999 - too short for header   999999999 - generated locally
72 039999999 - too short for data     999999999 - output drops
73 049999999 - with invalid version   999999999 output fragments generated
74 059999999 total fragments received 999999999 - fragmentation failed
75 069999999 - fragments dropped      999999999 destinations unreachable
76 079999999 - fragments timed out    999999999 packets output via raw IP
77 089999999 - fragments overflown
78 099999999 - packets reassembled ok           Input next-header histogram
79 109999999 packets forwarded        999999999  - destination options
80 119999999 - unreachable dests      999999999  - hop-by-hop options
81 129999999 - redirects generated    999999999  - IPv4
82 139999999 option errors            999999999  - TCP
83 149999999 unwanted multicasts      999999999  - UDP
84 159999999 delivered to upper layer 999999999  - IPv6
85 169999999 bad scope packets        999999999  - routing header
86 179999999 address selection failed 999999999  - fragmentation header
87 18                                 999999999  - ICMP6
88 19                                 999999999  - none
89 --0123456789012345678901234567890123456789012345678901234567890123456789012345
90 --0         1         2         3         4         5         6         7
91 */
92 
93 WINDOW *
94 openip6(void)
95 {
96 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
97 }
98 
99 void
100 closeip6(WINDOW *w)
101 {
102 	if (w == NULL)
103 		return;
104 	wclear(w);
105 	wrefresh(w);
106 	delwin(w);
107 }
108 
109 void
110 labelip6(void)
111 {
112 	wmove(wnd, 0, 0); wclrtoeol(wnd);
113 #define L(row, str) mvwprintw(wnd, row, 10, str)
114 #define R(row, str) mvwprintw(wnd, row, 45, str);
115 	L(0, "IPv6 Input");		R(0, "IPv6 Output");
116 	L(1, "total packets received");	R(1, "total packets sent");
117 	L(2, "- too short for header");	R(2, "- generated locally");
118 	L(3, "- too short for data");	R(3, "- output drops");
119 	L(4, "- with invalid version");	R(4, "output fragments generated");
120 	L(5, "total fragments received"); R(5, "- fragmentation failed");
121 	L(6, "- fragments dropped");	R(6, "destinations unreachable");
122 	L(7, "- fragments timed out");	R(7, "packets output via raw IP");
123 	L(8, "- fragments overflown");
124 	L(9, "- atomic fragments"); 	R(9, "Input next-header histogram");
125 	L(10, "- packets reassembled ok"); R(10, " - destination options");
126 	L(11, "packets forwarded");	R(11, " - hop-by-hop options");
127 	L(12, "- unreachable dests");	R(12, " - IPv4");
128 	L(13, "- redirects generated");	R(13, " - TCP");
129 	L(14, "option errors");		R(14, " - UDP");
130 	L(15, "unwanted multicasts");	R(15, " - IPv6");
131 	L(16, "delivered to upper layer"); R(16, " - routing header");
132 	L(17, "bad scope packets");	R(17, " - fragmentation header");
133 	L(18, "address selection failed");R(18, " - ICMP6");
134 					R(19, " - none");
135 #undef L
136 #undef R
137 }
138 
139 static void
140 domode(struct ip6stat *ret)
141 {
142 	const struct ip6stat *sub;
143 	int divisor = 1, i;
144 
145 	switch(currentmode) {
146 	case display_RATE:
147 		sub = &oldstat;
148 		divisor = (delay > 1000000) ? delay / 1000000 : 1;
149 		break;
150 	case display_DELTA:
151 		sub = &oldstat;
152 		break;
153 	case display_SINCE:
154 		sub = &initstat;
155 		break;
156 	default:
157 		*ret = curstat;
158 		return;
159 	}
160 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
161 	DO(ip6s_total);
162 	DO(ip6s_tooshort);
163 	DO(ip6s_toosmall);
164 	DO(ip6s_fragments);
165 	DO(ip6s_fragdropped);
166 	DO(ip6s_fragtimeout);
167 	DO(ip6s_fragoverflow);
168 	DO(ip6s_atomicfrags);
169 	DO(ip6s_forward);
170 	DO(ip6s_cantforward);
171 	DO(ip6s_redirectsent);
172 	DO(ip6s_delivered);
173 	DO(ip6s_localout);
174 	DO(ip6s_odropped);
175 	DO(ip6s_reassembled);
176 	DO(ip6s_fragmented);
177 	DO(ip6s_ofragments);
178 	DO(ip6s_cantfrag);
179 	DO(ip6s_badoptions);
180 	DO(ip6s_noroute);
181 	DO(ip6s_badvers);
182 	DO(ip6s_rawout);
183 	DO(ip6s_notmember);
184 	for (i = 0; i < 256; i++)
185 		DO(ip6s_nxthist[i]);
186 	DO(ip6s_badscope);
187 	DO(ip6s_sources_none);
188 #undef DO
189 }
190 
191 void
192 showip6(void)
193 {
194 	struct ip6stat stats;
195 	uint64_t totalout;
196 
197 	domode(&stats);
198 	totalout = stats.ip6s_forward + stats.ip6s_localout;
199 
200 #define DO(stat, row, col) \
201 	mvwprintw(wnd, row, col, "%9"PRIu64, stats.stat)
202 
203 	DO(ip6s_total, 1, 0);
204 	mvwprintw(wnd, 1, 35, "%9"PRIu64, totalout);
205 	DO(ip6s_tooshort, 2, 0);
206 	DO(ip6s_localout, 2, 35);
207 	DO(ip6s_toosmall, 3, 0);
208 	DO(ip6s_odropped, 3, 35);
209 	DO(ip6s_badvers, 4, 0);
210 	DO(ip6s_ofragments, 4, 35);
211 	DO(ip6s_fragments, 5, 0);
212 	DO(ip6s_cantfrag, 5, 35);
213 	DO(ip6s_fragdropped, 6, 0);
214 	DO(ip6s_noroute, 6, 35);
215 	DO(ip6s_fragtimeout, 7, 0);
216 	DO(ip6s_rawout, 7, 35);
217 	DO(ip6s_fragoverflow, 8, 0);
218 	DO(ip6s_atomicfrags, 9, 0);
219 	DO(ip6s_reassembled, 10, 0);
220 	DO(ip6s_forward, 11, 0);
221 	DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35);
222 	DO(ip6s_cantforward, 12, 0);
223 	DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35);
224 	DO(ip6s_redirectsent, 13, 0);
225 	DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35);
226 	DO(ip6s_badoptions, 14, 0);
227 	DO(ip6s_nxthist[IPPROTO_TCP], 13, 35);
228 	DO(ip6s_notmember, 15, 0);
229 	DO(ip6s_nxthist[IPPROTO_UDP], 14, 35);
230 	DO(ip6s_delivered, 16, 0);
231 	DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35);
232 	DO(ip6s_badscope, 17, 0);
233 	DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35);
234 	DO(ip6s_sources_none, 18, 0);
235 	DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35);
236 	DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35);
237 	DO(ip6s_nxthist[IPPROTO_NONE], 19, 35);
238 #undef DO
239 }
240 
241 int
242 initip6(void)
243 {
244 	size_t len;
245 	int name[4];
246 
247 	name[0] = CTL_NET;
248 	name[1] = PF_INET6;
249 	name[2] = IPPROTO_IPV6;
250 	name[3] = IPV6CTL_STATS;
251 
252 	len = 0;
253 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
254 		error("sysctl getting ip6stat size failed");
255 		return 0;
256 	}
257 	if (len > sizeof curstat) {
258 		error("ip6stat structure has grown--recompile systat!");
259 		return 0;
260 	}
261 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
262 		error("sysctl getting ip6stat failed");
263 		return 0;
264 	}
265 	oldstat = initstat;
266 	return 1;
267 }
268 
269 void
270 resetip6(void)
271 {
272 	size_t len;
273 	int name[4];
274 
275 	name[0] = CTL_NET;
276 	name[1] = PF_INET6;
277 	name[2] = IPPROTO_IPV6;
278 	name[3] = IPV6CTL_STATS;
279 
280 	len = sizeof initstat;
281 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
282 		error("sysctl getting ipstat failed");
283 	}
284 
285 	oldstat = initstat;
286 }
287 
288 void
289 fetchip6(void)
290 {
291 	int name[4];
292 	size_t len;
293 
294 	oldstat = curstat;
295 	name[0] = CTL_NET;
296 	name[1] = PF_INET6;
297 	name[2] = IPPROTO_IPV6;
298 	name[3] = IPV6CTL_STATS;
299 	len = sizeof curstat;
300 
301 	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
302 		return;
303 }
304 
305 #endif
306