xref: /freebsd/usr.bin/systat/ip6.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
1f29d8c1aSDavid Malone /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4f29d8c1aSDavid Malone  * Copyright (c) 1980, 1992, 1993
5f29d8c1aSDavid Malone  *	The Regents of the University of California.  All rights reserved.
6f29d8c1aSDavid Malone  *
7f29d8c1aSDavid Malone  * Redistribution and use in source and binary forms, with or without
8f29d8c1aSDavid Malone  * modification, are permitted provided that the following conditions
9f29d8c1aSDavid Malone  * are met:
10f29d8c1aSDavid Malone  * 1. Redistributions of source code must retain the above copyright
11f29d8c1aSDavid Malone  *    notice, this list of conditions and the following disclaimer.
12f29d8c1aSDavid Malone  * 2. Redistributions in binary form must reproduce the above copyright
13f29d8c1aSDavid Malone  *    notice, this list of conditions and the following disclaimer in the
14f29d8c1aSDavid Malone  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16f29d8c1aSDavid Malone  *    may be used to endorse or promote products derived from this software
17f29d8c1aSDavid Malone  *    without specific prior written permission.
18f29d8c1aSDavid Malone  *
19f29d8c1aSDavid Malone  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20f29d8c1aSDavid Malone  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21f29d8c1aSDavid Malone  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22f29d8c1aSDavid Malone  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23f29d8c1aSDavid Malone  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24f29d8c1aSDavid Malone  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25f29d8c1aSDavid Malone  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26f29d8c1aSDavid Malone  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27f29d8c1aSDavid Malone  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28f29d8c1aSDavid Malone  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29f29d8c1aSDavid Malone  * SUCH DAMAGE.
30f29d8c1aSDavid Malone  */
31f29d8c1aSDavid Malone 
32f29d8c1aSDavid Malone #include <sys/cdefs.h>
33f29d8c1aSDavid Malone 
34f29d8c1aSDavid Malone __FBSDID("$FreeBSD$");
35f29d8c1aSDavid Malone 
36f29d8c1aSDavid Malone #ifdef lint
37f29d8c1aSDavid Malone static const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38f29d8c1aSDavid Malone #endif
39f29d8c1aSDavid Malone 
40f29d8c1aSDavid Malone /* From:
41f29d8c1aSDavid Malone 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
42f29d8c1aSDavid Malone */
43f29d8c1aSDavid Malone 
44f29d8c1aSDavid Malone #ifdef INET6
45f29d8c1aSDavid Malone #include <sys/param.h>
46f29d8c1aSDavid Malone #include <sys/types.h>
47f29d8c1aSDavid Malone #include <sys/socket.h>
48f29d8c1aSDavid Malone #include <sys/sysctl.h>
49f29d8c1aSDavid Malone 
50f29d8c1aSDavid Malone #include <netinet/in.h>
51f29d8c1aSDavid Malone #include <netinet/in_systm.h>
52f29d8c1aSDavid Malone #include <netinet/ip.h>
53f29d8c1aSDavid Malone #include <netinet6/ip6_var.h>
54f29d8c1aSDavid Malone 
55f29d8c1aSDavid Malone #include <stdlib.h>
56f29d8c1aSDavid Malone #include <string.h>
57f29d8c1aSDavid Malone #include <paths.h>
58f29d8c1aSDavid Malone 
59f29d8c1aSDavid Malone #include "systat.h"
60f29d8c1aSDavid Malone #include "extern.h"
61f29d8c1aSDavid Malone #include "mode.h"
62f29d8c1aSDavid Malone 
63f29d8c1aSDavid Malone static struct ip6stat curstat, initstat, oldstat;
64f29d8c1aSDavid Malone 
65f29d8c1aSDavid Malone /*-
66f29d8c1aSDavid Malone --0         1         2         3         4         5         6         7
67f29d8c1aSDavid Malone --0123456789012345678901234567890123456789012345678901234567890123456789012345
688aa22952SBruce Evans 00        IPv6 Input                         IPv6 Output
698aa22952SBruce Evans 019999999 total packets received   999999999 total packets sent
708aa22952SBruce Evans 029999999 - too short for header   999999999 - generated locally
718aa22952SBruce Evans 039999999 - too short for data     999999999 - output drops
728aa22952SBruce Evans 049999999 - with invalid version   999999999 output fragments generated
738aa22952SBruce Evans 059999999 total fragments received 999999999 - fragmentation failed
748aa22952SBruce Evans 069999999 - fragments dropped      999999999 destinations unreachable
758aa22952SBruce Evans 079999999 - fragments timed out    999999999 packets output via raw IP
768aa22952SBruce Evans 089999999 - fragments overflown
778aa22952SBruce Evans 099999999 - packets reassembled ok           Input next-header histogram
788aa22952SBruce Evans 109999999 packets forwarded        999999999  - destination options
798aa22952SBruce Evans 119999999 - unreachable dests      999999999  - hop-by-hop options
808aa22952SBruce Evans 129999999 - redirects generated    999999999  - IPv4
818aa22952SBruce Evans 139999999 option errors            999999999  - TCP
828aa22952SBruce Evans 149999999 unwanted multicasts      999999999  - UDP
838aa22952SBruce Evans 159999999 delivered to upper layer 999999999  - IPv6
848aa22952SBruce Evans 169999999 bad scope packets        999999999  - routing header
858aa22952SBruce Evans 179999999 address selection failed 999999999  - fragmentation header
868aa22952SBruce Evans 18                                 999999999  - ICMP6
878aa22952SBruce Evans 19                                 999999999  - none
88f29d8c1aSDavid Malone --0123456789012345678901234567890123456789012345678901234567890123456789012345
89f29d8c1aSDavid Malone --0         1         2         3         4         5         6         7
90f29d8c1aSDavid Malone */
91f29d8c1aSDavid Malone 
92f29d8c1aSDavid Malone WINDOW *
93f29d8c1aSDavid Malone openip6(void)
94f29d8c1aSDavid Malone {
958aa22952SBruce Evans 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
96f29d8c1aSDavid Malone }
97f29d8c1aSDavid Malone 
98f29d8c1aSDavid Malone void
9993b9f504SXin LI closeip6(WINDOW *w)
100f29d8c1aSDavid Malone {
101f29d8c1aSDavid Malone 	if (w == NULL)
102f29d8c1aSDavid Malone 		return;
103f29d8c1aSDavid Malone 	wclear(w);
104f29d8c1aSDavid Malone 	wrefresh(w);
105f29d8c1aSDavid Malone 	delwin(w);
106f29d8c1aSDavid Malone }
107f29d8c1aSDavid Malone 
108f29d8c1aSDavid Malone void
109f29d8c1aSDavid Malone labelip6(void)
110f29d8c1aSDavid Malone {
111f29d8c1aSDavid Malone 	wmove(wnd, 0, 0); wclrtoeol(wnd);
112f29d8c1aSDavid Malone #define L(row, str) mvwprintw(wnd, row, 10, str)
113f29d8c1aSDavid Malone #define R(row, str) mvwprintw(wnd, row, 45, str);
1148aa22952SBruce Evans 	L(0, "IPv6 Input");		R(0, "IPv6 Output");
1158aa22952SBruce Evans 	L(1, "total packets received");	R(1, "total packets sent");
1168aa22952SBruce Evans 	L(2, "- too short for header");	R(2, "- generated locally");
1178aa22952SBruce Evans 	L(3, "- too short for data");	R(3, "- output drops");
1188aa22952SBruce Evans 	L(4, "- with invalid version");	R(4, "output fragments generated");
1198aa22952SBruce Evans 	L(5, "total fragments received"); R(5, "- fragmentation failed");
1208aa22952SBruce Evans 	L(6, "- fragments dropped");	R(6, "destinations unreachable");
1218aa22952SBruce Evans 	L(7, "- fragments timed out");	R(7, "packets output via raw IP");
1228aa22952SBruce Evans 	L(8, "- fragments overflown");
1238aa22952SBruce Evans 	L(9, "- packets reassembled ok"); R(9, "Input next-header histogram");
1248aa22952SBruce Evans 	L(10, "packets forwarded");	R(10, " - destination options");
1258aa22952SBruce Evans 	L(11, "- unreachable dests");	R(11, " - hop-by-hop options");
1268aa22952SBruce Evans 	L(12, "- redirects generated");	R(12, " - IPv4");
1278aa22952SBruce Evans 	L(13, "option errors");		R(13, " - TCP");
1288aa22952SBruce Evans 	L(14, "unwanted multicasts");	R(14, " - UDP");
1298aa22952SBruce Evans 	L(15, "delivered to upper layer"); R(15, " - IPv6");
1308aa22952SBruce Evans 	L(16, "bad scope packets");	R(16, " - routing header");
1318aa22952SBruce Evans 	L(17, "address selection failed"); R(17, " - fragmentation header");
1328aa22952SBruce Evans 					R(18, " - ICMP6");
1338aa22952SBruce Evans 					R(19, " - none");
134f29d8c1aSDavid Malone #undef L
135f29d8c1aSDavid Malone #undef R
136f29d8c1aSDavid Malone }
137f29d8c1aSDavid Malone 
138f29d8c1aSDavid Malone static void
139f29d8c1aSDavid Malone domode(struct ip6stat *ret)
140f29d8c1aSDavid Malone {
141f29d8c1aSDavid Malone 	const struct ip6stat *sub;
142f29d8c1aSDavid Malone 	int divisor = 1, i;
143f29d8c1aSDavid Malone 
144f29d8c1aSDavid Malone 	switch(currentmode) {
145f29d8c1aSDavid Malone 	case display_RATE:
146f29d8c1aSDavid Malone 		sub = &oldstat;
1478b3daf89SAlexander V. Chernikov 		divisor = (delay > 1000000) ? delay / 1000000 : 1;
148f29d8c1aSDavid Malone 		break;
149f29d8c1aSDavid Malone 	case display_DELTA:
150f29d8c1aSDavid Malone 		sub = &oldstat;
151f29d8c1aSDavid Malone 		break;
152f29d8c1aSDavid Malone 	case display_SINCE:
153f29d8c1aSDavid Malone 		sub = &initstat;
154f29d8c1aSDavid Malone 		break;
155f29d8c1aSDavid Malone 	default:
156f29d8c1aSDavid Malone 		*ret = curstat;
157f29d8c1aSDavid Malone 		return;
158f29d8c1aSDavid Malone 	}
159f29d8c1aSDavid Malone #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
160f29d8c1aSDavid Malone 	DO(ip6s_total);
161f29d8c1aSDavid Malone 	DO(ip6s_tooshort);
162f29d8c1aSDavid Malone 	DO(ip6s_toosmall);
163f29d8c1aSDavid Malone 	DO(ip6s_fragments);
164f29d8c1aSDavid Malone 	DO(ip6s_fragdropped);
165f29d8c1aSDavid Malone 	DO(ip6s_fragtimeout);
166f29d8c1aSDavid Malone 	DO(ip6s_fragoverflow);
167f29d8c1aSDavid Malone 	DO(ip6s_forward);
168f29d8c1aSDavid Malone 	DO(ip6s_cantforward);
169f29d8c1aSDavid Malone 	DO(ip6s_redirectsent);
170f29d8c1aSDavid Malone 	DO(ip6s_delivered);
171f29d8c1aSDavid Malone 	DO(ip6s_localout);
172f29d8c1aSDavid Malone 	DO(ip6s_odropped);
173f29d8c1aSDavid Malone 	DO(ip6s_reassembled);
174f29d8c1aSDavid Malone 	DO(ip6s_fragmented);
175f29d8c1aSDavid Malone 	DO(ip6s_ofragments);
176f29d8c1aSDavid Malone 	DO(ip6s_cantfrag);
177f29d8c1aSDavid Malone 	DO(ip6s_badoptions);
178f29d8c1aSDavid Malone 	DO(ip6s_noroute);
179f29d8c1aSDavid Malone 	DO(ip6s_badvers);
180f29d8c1aSDavid Malone 	DO(ip6s_rawout);
181f29d8c1aSDavid Malone 	DO(ip6s_notmember);
182f29d8c1aSDavid Malone 	for (i = 0; i < 256; i++)
183f29d8c1aSDavid Malone 		DO(ip6s_nxthist[i]);
184f29d8c1aSDavid Malone 	DO(ip6s_badscope);
185f29d8c1aSDavid Malone 	DO(ip6s_sources_none);
186f29d8c1aSDavid Malone #undef DO
187f29d8c1aSDavid Malone }
188f29d8c1aSDavid Malone 
189f29d8c1aSDavid Malone void
190f29d8c1aSDavid Malone showip6(void)
191f29d8c1aSDavid Malone {
192f29d8c1aSDavid Malone 	struct ip6stat stats;
193f29d8c1aSDavid Malone 	u_long totalout;
194f29d8c1aSDavid Malone 
195f29d8c1aSDavid Malone 	domode(&stats);
196f29d8c1aSDavid Malone 	totalout = stats.ip6s_forward + stats.ip6s_localout;
197f29d8c1aSDavid Malone 
198f29d8c1aSDavid Malone #define DO(stat, row, col) \
199f29d8c1aSDavid Malone 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
200f29d8c1aSDavid Malone 
2018aa22952SBruce Evans 	DO(ip6s_total, 1, 0);
2028aa22952SBruce Evans 	mvwprintw(wnd, 1, 35, "%9lu", totalout);
2038aa22952SBruce Evans 	DO(ip6s_tooshort, 2, 0);
2048aa22952SBruce Evans 	DO(ip6s_localout, 2, 35);
2058aa22952SBruce Evans 	DO(ip6s_toosmall, 3, 0);
2068aa22952SBruce Evans 	DO(ip6s_odropped, 3, 35);
2078aa22952SBruce Evans 	DO(ip6s_badvers, 4, 0);
2088aa22952SBruce Evans 	DO(ip6s_ofragments, 4, 35);
2098aa22952SBruce Evans 	DO(ip6s_fragments, 5, 0);
2108aa22952SBruce Evans 	DO(ip6s_cantfrag, 5, 35);
2118aa22952SBruce Evans 	DO(ip6s_fragdropped, 6, 0);
2128aa22952SBruce Evans 	DO(ip6s_noroute, 6, 35);
2138aa22952SBruce Evans 	DO(ip6s_fragtimeout, 7, 0);
2148aa22952SBruce Evans 	DO(ip6s_rawout, 7, 35);
2158aa22952SBruce Evans 	DO(ip6s_fragoverflow, 8, 0);
2168aa22952SBruce Evans 	DO(ip6s_reassembled, 9, 0);
2178aa22952SBruce Evans 	DO(ip6s_forward, 10, 0);
218f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35);
2198aa22952SBruce Evans 	DO(ip6s_cantforward, 11, 0);
220f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35);
2218aa22952SBruce Evans 	DO(ip6s_redirectsent, 12, 0);
222f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35);
2238aa22952SBruce Evans 	DO(ip6s_badoptions, 13, 0);
224f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_TCP], 13, 35);
2258aa22952SBruce Evans 	DO(ip6s_notmember, 14, 0);
226f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_UDP], 14, 35);
2278aa22952SBruce Evans 	DO(ip6s_delivered, 15, 0);
228f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35);
2298aa22952SBruce Evans 	DO(ip6s_badscope, 16, 0);
230f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35);
2318aa22952SBruce Evans 	DO(ip6s_sources_none, 17, 0);
232f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35);
233f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35);
234f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_NONE], 19, 35);
235f29d8c1aSDavid Malone #undef DO
236f29d8c1aSDavid Malone }
237f29d8c1aSDavid Malone 
238f29d8c1aSDavid Malone int
239f29d8c1aSDavid Malone initip6(void)
240f29d8c1aSDavid Malone {
241f29d8c1aSDavid Malone 	size_t len;
242f29d8c1aSDavid Malone 	int name[4];
243f29d8c1aSDavid Malone 
244f29d8c1aSDavid Malone 	name[0] = CTL_NET;
245f29d8c1aSDavid Malone 	name[1] = PF_INET6;
246f29d8c1aSDavid Malone 	name[2] = IPPROTO_IPV6;
247f29d8c1aSDavid Malone 	name[3] = IPV6CTL_STATS;
248f29d8c1aSDavid Malone 
249f29d8c1aSDavid Malone 	len = 0;
250f29d8c1aSDavid Malone 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
251f29d8c1aSDavid Malone 		error("sysctl getting ip6stat size failed");
252f29d8c1aSDavid Malone 		return 0;
253f29d8c1aSDavid Malone 	}
254f29d8c1aSDavid Malone 	if (len > sizeof curstat) {
255f29d8c1aSDavid Malone 		error("ip6stat structure has grown--recompile systat!");
256f29d8c1aSDavid Malone 		return 0;
257f29d8c1aSDavid Malone 	}
258f29d8c1aSDavid Malone 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
259f29d8c1aSDavid Malone 		error("sysctl getting ip6stat failed");
260f29d8c1aSDavid Malone 		return 0;
261f29d8c1aSDavid Malone 	}
262f29d8c1aSDavid Malone 	oldstat = initstat;
263f29d8c1aSDavid Malone 	return 1;
264f29d8c1aSDavid Malone }
265f29d8c1aSDavid Malone 
266f29d8c1aSDavid Malone void
267f29d8c1aSDavid Malone resetip6(void)
268f29d8c1aSDavid Malone {
269f29d8c1aSDavid Malone 	size_t len;
270f29d8c1aSDavid Malone 	int name[4];
271f29d8c1aSDavid Malone 
272f29d8c1aSDavid Malone 	name[0] = CTL_NET;
273f29d8c1aSDavid Malone 	name[1] = PF_INET6;
274f29d8c1aSDavid Malone 	name[2] = IPPROTO_IPV6;
275f29d8c1aSDavid Malone 	name[3] = IPV6CTL_STATS;
276f29d8c1aSDavid Malone 
277f29d8c1aSDavid Malone 	len = sizeof initstat;
278f29d8c1aSDavid Malone 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
279f29d8c1aSDavid Malone 		error("sysctl getting ipstat failed");
280f29d8c1aSDavid Malone 	}
281f29d8c1aSDavid Malone 
282f29d8c1aSDavid Malone 	oldstat = initstat;
283f29d8c1aSDavid Malone }
284f29d8c1aSDavid Malone 
285f29d8c1aSDavid Malone void
286f29d8c1aSDavid Malone fetchip6(void)
287f29d8c1aSDavid Malone {
288f29d8c1aSDavid Malone 	int name[4];
289f29d8c1aSDavid Malone 	size_t len;
290f29d8c1aSDavid Malone 
291f29d8c1aSDavid Malone 	oldstat = curstat;
292f29d8c1aSDavid Malone 	name[0] = CTL_NET;
293f29d8c1aSDavid Malone 	name[1] = PF_INET6;
294f29d8c1aSDavid Malone 	name[2] = IPPROTO_IPV6;
295f29d8c1aSDavid Malone 	name[3] = IPV6CTL_STATS;
296f29d8c1aSDavid Malone 	len = sizeof curstat;
297f29d8c1aSDavid Malone 
298f29d8c1aSDavid Malone 	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
299f29d8c1aSDavid Malone 		return;
300f29d8c1aSDavid Malone }
301f29d8c1aSDavid Malone 
302f29d8c1aSDavid Malone #endif
303