xref: /freebsd/usr.bin/systat/ip6.c (revision f29d8c1abe6a89f8e8e93f57152a36d409d327cd)
1f29d8c1aSDavid Malone /*-
2f29d8c1aSDavid Malone  * Copyright (c) 1980, 1992, 1993
3f29d8c1aSDavid Malone  *	The Regents of the University of California.  All rights reserved.
4f29d8c1aSDavid Malone  *
5f29d8c1aSDavid Malone  * Redistribution and use in source and binary forms, with or without
6f29d8c1aSDavid Malone  * modification, are permitted provided that the following conditions
7f29d8c1aSDavid Malone  * are met:
8f29d8c1aSDavid Malone  * 1. Redistributions of source code must retain the above copyright
9f29d8c1aSDavid Malone  *    notice, this list of conditions and the following disclaimer.
10f29d8c1aSDavid Malone  * 2. Redistributions in binary form must reproduce the above copyright
11f29d8c1aSDavid Malone  *    notice, this list of conditions and the following disclaimer in the
12f29d8c1aSDavid Malone  *    documentation and/or other materials provided with the distribution.
13f29d8c1aSDavid Malone  * 3. All advertising materials mentioning features or use of this software
14f29d8c1aSDavid Malone  *    must display the following acknowledgement:
15f29d8c1aSDavid Malone  *	This product includes software developed by the University of
16f29d8c1aSDavid Malone  *	California, Berkeley and its contributors.
17f29d8c1aSDavid Malone  * 4. Neither the name of the University nor the names of its contributors
18f29d8c1aSDavid Malone  *    may be used to endorse or promote products derived from this software
19f29d8c1aSDavid Malone  *    without specific prior written permission.
20f29d8c1aSDavid Malone  *
21f29d8c1aSDavid Malone  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22f29d8c1aSDavid Malone  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23f29d8c1aSDavid Malone  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24f29d8c1aSDavid Malone  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25f29d8c1aSDavid Malone  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26f29d8c1aSDavid Malone  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27f29d8c1aSDavid Malone  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28f29d8c1aSDavid Malone  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29f29d8c1aSDavid Malone  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30f29d8c1aSDavid Malone  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31f29d8c1aSDavid Malone  * SUCH DAMAGE.
32f29d8c1aSDavid Malone  */
33f29d8c1aSDavid Malone 
34f29d8c1aSDavid Malone #include <sys/cdefs.h>
35f29d8c1aSDavid Malone 
36f29d8c1aSDavid Malone __FBSDID("$FreeBSD$");
37f29d8c1aSDavid Malone 
38f29d8c1aSDavid Malone #ifdef lint
39f29d8c1aSDavid Malone static const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
40f29d8c1aSDavid Malone #endif
41f29d8c1aSDavid Malone 
42f29d8c1aSDavid Malone /* From:
43f29d8c1aSDavid Malone 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
44f29d8c1aSDavid Malone */
45f29d8c1aSDavid Malone 
46f29d8c1aSDavid Malone #ifdef INET6
47f29d8c1aSDavid Malone #include <sys/param.h>
48f29d8c1aSDavid Malone #include <sys/types.h>
49f29d8c1aSDavid Malone #include <sys/socket.h>
50f29d8c1aSDavid Malone #include <sys/sysctl.h>
51f29d8c1aSDavid Malone 
52f29d8c1aSDavid Malone #include <netinet/in.h>
53f29d8c1aSDavid Malone #include <netinet/in_systm.h>
54f29d8c1aSDavid Malone #include <netinet/ip.h>
55f29d8c1aSDavid Malone #include <netinet6/ip6_var.h>
56f29d8c1aSDavid Malone 
57f29d8c1aSDavid Malone #include <stdlib.h>
58f29d8c1aSDavid Malone #include <string.h>
59f29d8c1aSDavid Malone #include <paths.h>
60f29d8c1aSDavid Malone 
61f29d8c1aSDavid Malone #include "systat.h"
62f29d8c1aSDavid Malone #include "extern.h"
63f29d8c1aSDavid Malone #include "mode.h"
64f29d8c1aSDavid Malone 
65f29d8c1aSDavid Malone static struct ip6stat curstat, initstat, oldstat;
66f29d8c1aSDavid Malone 
67f29d8c1aSDavid Malone /*-
68f29d8c1aSDavid Malone --0         1         2         3         4         5         6         7
69f29d8c1aSDavid Malone --0123456789012345678901234567890123456789012345678901234567890123456789012345
70f29d8c1aSDavid Malone 01        IPv6 Input                         IPv6 Output
71f29d8c1aSDavid Malone 029999999 total packets received   999999999 total packets sent
72f29d8c1aSDavid Malone 039999999 - too short for header   999999999 - generated locally
73f29d8c1aSDavid Malone 049999999 - too short for data     999999999 - output drops
74f29d8c1aSDavid Malone 059999999 - with invalid version   999999999 output fragments generated
75f29d8c1aSDavid Malone 069999999 total fragments received 999999999 - fragmentation failed
76f29d8c1aSDavid Malone 079999999 - fragments dropped      999999999 destinations unreachable
77f29d8c1aSDavid Malone 089999999 - fragments timed out    999999999 packets output via raw IP
78f29d8c1aSDavid Malone 099999999 - fragments overflown              Input next-header histogram
79f29d8c1aSDavid Malone 109999999 - packets reassembled ok 999999999  - destination options
80f29d8c1aSDavid Malone 119999999 packets forwarded        999999999  - hop-by-hop options
81f29d8c1aSDavid Malone 129999999 - unreachable dests      999999999  - IPv4
82f29d8c1aSDavid Malone 139999999 - redirects generated    999999999  - TCP
83f29d8c1aSDavid Malone 149999999 option errors            999999999  - UDP
84f29d8c1aSDavid Malone 159999999 unwanted multicasts      999999999  - IPv6
85f29d8c1aSDavid Malone 169999999 delivered to upper layer 999999999  - routing header
86f29d8c1aSDavid Malone 17                                 999999999  - fragmentation header
87f29d8c1aSDavid Malone 189999999 bad scope packets        999999999  - ICMP6
88f29d8c1aSDavid Malone 199999999 address selection failed 999999999  - none
89f29d8c1aSDavid Malone --0123456789012345678901234567890123456789012345678901234567890123456789012345
90f29d8c1aSDavid Malone --0         1         2         3         4         5         6         7
91f29d8c1aSDavid Malone */
92f29d8c1aSDavid Malone 
93f29d8c1aSDavid Malone WINDOW *
94f29d8c1aSDavid Malone openip6(void)
95f29d8c1aSDavid Malone {
96f29d8c1aSDavid Malone 	return (subwin(stdscr, LINES-4-1, 0, 4, 0));
97f29d8c1aSDavid Malone }
98f29d8c1aSDavid Malone 
99f29d8c1aSDavid Malone void
100f29d8c1aSDavid Malone closeip6(w)
101f29d8c1aSDavid Malone 	WINDOW *w;
102f29d8c1aSDavid Malone {
103f29d8c1aSDavid Malone 	if (w == NULL)
104f29d8c1aSDavid Malone 		return;
105f29d8c1aSDavid Malone 	wclear(w);
106f29d8c1aSDavid Malone 	wrefresh(w);
107f29d8c1aSDavid Malone 	delwin(w);
108f29d8c1aSDavid Malone }
109f29d8c1aSDavid Malone 
110f29d8c1aSDavid Malone void
111f29d8c1aSDavid Malone labelip6(void)
112f29d8c1aSDavid Malone {
113f29d8c1aSDavid Malone 	wmove(wnd, 0, 0); wclrtoeol(wnd);
114f29d8c1aSDavid Malone #define L(row, str) mvwprintw(wnd, row, 10, str)
115f29d8c1aSDavid Malone #define R(row, str) mvwprintw(wnd, row, 45, str);
116f29d8c1aSDavid Malone 	L(1, "IPv6 Input");		R(1, "IPv6 Output");
117f29d8c1aSDavid Malone 	L(2, "total packets received");	R(2, "total packets sent");
118f29d8c1aSDavid Malone 	L(3, "- too short for header");	R(3, "- generated locally");
119f29d8c1aSDavid Malone 	L(4, "- too short for data");	R(4, "- output drops");
120f29d8c1aSDavid Malone 	L(5, "- with invalid version");	R(5, "output fragments generated");
121f29d8c1aSDavid Malone 	L(6, "total fragments received"); R(6, "- fragmentation failed");
122f29d8c1aSDavid Malone 	L(7, "- fragments dropped");	R(7, "destinations unreachable");
123f29d8c1aSDavid Malone 	L(8, "- fragments timed out");	R(8, "packets output via raw IP");
124f29d8c1aSDavid Malone 	L(9, "- fragments overflown");	R(9, "Input next-header histogram");
125f29d8c1aSDavid Malone 	L(10, "- packets reassembled ok"); R(10, " - destination options");
126f29d8c1aSDavid Malone 	L(11, "packets forwarded");	R(11, " - hop-by-hop options");
127f29d8c1aSDavid Malone 	L(12, "- unreachable dests");	R(12, " - IPv4");
128f29d8c1aSDavid Malone 	L(13, "- redirects generated");	R(13, " - TCP");
129f29d8c1aSDavid Malone 	L(14, "option errors");		R(14, " - UDP");
130f29d8c1aSDavid Malone 	L(15, "unwanted multicasts");	R(15, " - IPv6");
131f29d8c1aSDavid Malone 	L(16, "delivered to upper layer"); R(16, " - routing header");
132f29d8c1aSDavid Malone 					R(17, " - fragmentation header");
133f29d8c1aSDavid Malone 	L(18, "bad scope packets");	R(18, " - ICMP6");
134f29d8c1aSDavid Malone 	L(19, "address selection failed"); R(19, " - none");
135f29d8c1aSDavid Malone #undef L
136f29d8c1aSDavid Malone #undef R
137f29d8c1aSDavid Malone }
138f29d8c1aSDavid Malone 
139f29d8c1aSDavid Malone static void
140f29d8c1aSDavid Malone domode(struct ip6stat *ret)
141f29d8c1aSDavid Malone {
142f29d8c1aSDavid Malone 	const struct ip6stat *sub;
143f29d8c1aSDavid Malone 	int divisor = 1, i;
144f29d8c1aSDavid Malone 
145f29d8c1aSDavid Malone 	switch(currentmode) {
146f29d8c1aSDavid Malone 	case display_RATE:
147f29d8c1aSDavid Malone 		sub = &oldstat;
148f29d8c1aSDavid Malone 		divisor = naptime;
149f29d8c1aSDavid Malone 		break;
150f29d8c1aSDavid Malone 	case display_DELTA:
151f29d8c1aSDavid Malone 		sub = &oldstat;
152f29d8c1aSDavid Malone 		break;
153f29d8c1aSDavid Malone 	case display_SINCE:
154f29d8c1aSDavid Malone 		sub = &initstat;
155f29d8c1aSDavid Malone 		break;
156f29d8c1aSDavid Malone 	default:
157f29d8c1aSDavid Malone 		*ret = curstat;
158f29d8c1aSDavid Malone 		return;
159f29d8c1aSDavid Malone 	}
160f29d8c1aSDavid Malone #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
161f29d8c1aSDavid Malone 	DO(ip6s_total);
162f29d8c1aSDavid Malone 	DO(ip6s_tooshort);
163f29d8c1aSDavid Malone 	DO(ip6s_toosmall);
164f29d8c1aSDavid Malone 	DO(ip6s_fragments);
165f29d8c1aSDavid Malone 	DO(ip6s_fragdropped);
166f29d8c1aSDavid Malone 	DO(ip6s_fragtimeout);
167f29d8c1aSDavid Malone 	DO(ip6s_fragoverflow);
168f29d8c1aSDavid Malone 	DO(ip6s_forward);
169f29d8c1aSDavid Malone 	DO(ip6s_cantforward);
170f29d8c1aSDavid Malone 	DO(ip6s_redirectsent);
171f29d8c1aSDavid Malone 	DO(ip6s_delivered);
172f29d8c1aSDavid Malone 	DO(ip6s_localout);
173f29d8c1aSDavid Malone 	DO(ip6s_odropped);
174f29d8c1aSDavid Malone 	DO(ip6s_reassembled);
175f29d8c1aSDavid Malone 	DO(ip6s_fragmented);
176f29d8c1aSDavid Malone 	DO(ip6s_ofragments);
177f29d8c1aSDavid Malone 	DO(ip6s_cantfrag);
178f29d8c1aSDavid Malone 	DO(ip6s_badoptions);
179f29d8c1aSDavid Malone 	DO(ip6s_noroute);
180f29d8c1aSDavid Malone 	DO(ip6s_badvers);
181f29d8c1aSDavid Malone 	DO(ip6s_rawout);
182f29d8c1aSDavid Malone 	DO(ip6s_notmember);
183f29d8c1aSDavid Malone 	for (i = 0; i < 256; i++)
184f29d8c1aSDavid Malone 		DO(ip6s_nxthist[i]);
185f29d8c1aSDavid Malone 	DO(ip6s_badscope);
186f29d8c1aSDavid Malone 	DO(ip6s_sources_none);
187f29d8c1aSDavid Malone #undef DO
188f29d8c1aSDavid Malone }
189f29d8c1aSDavid Malone 
190f29d8c1aSDavid Malone void
191f29d8c1aSDavid Malone showip6(void)
192f29d8c1aSDavid Malone {
193f29d8c1aSDavid Malone 	struct ip6stat stats;
194f29d8c1aSDavid Malone 	u_long totalout;
195f29d8c1aSDavid Malone 
196f29d8c1aSDavid Malone 	domode(&stats);
197f29d8c1aSDavid Malone 	totalout = stats.ip6s_forward + stats.ip6s_localout;
198f29d8c1aSDavid Malone 
199f29d8c1aSDavid Malone #define DO(stat, row, col) \
200f29d8c1aSDavid Malone 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
201f29d8c1aSDavid Malone 
202f29d8c1aSDavid Malone 	DO(ip6s_total, 2, 0);
203f29d8c1aSDavid Malone 	mvwprintw(wnd, 2, 35, "%9lu", totalout);
204f29d8c1aSDavid Malone 	DO(ip6s_localout, 3, 35);
205f29d8c1aSDavid Malone 	DO(ip6s_tooshort, 3, 0);
206f29d8c1aSDavid Malone 	DO(ip6s_odropped, 4, 35);
207f29d8c1aSDavid Malone 	DO(ip6s_toosmall, 4, 0);
208f29d8c1aSDavid Malone 	DO(ip6s_ofragments, 5, 35);
209f29d8c1aSDavid Malone 	DO(ip6s_badvers, 5, 0);
210f29d8c1aSDavid Malone 	DO(ip6s_cantfrag, 6, 35);
211f29d8c1aSDavid Malone 	DO(ip6s_fragments, 6, 0);
212f29d8c1aSDavid Malone 	DO(ip6s_noroute, 7, 35);
213f29d8c1aSDavid Malone 	DO(ip6s_fragdropped, 7, 0);
214f29d8c1aSDavid Malone 	DO(ip6s_rawout, 8, 35);
215f29d8c1aSDavid Malone 	DO(ip6s_fragtimeout, 8, 0);
216f29d8c1aSDavid Malone 	DO(ip6s_fragoverflow, 9, 0);
217f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35);
218f29d8c1aSDavid Malone 	DO(ip6s_reassembled, 10, 0);
219f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35);
220f29d8c1aSDavid Malone 	DO(ip6s_forward, 11, 0);
221f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35);
222f29d8c1aSDavid Malone 	DO(ip6s_cantforward, 12, 0);
223f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_TCP], 13, 35);
224f29d8c1aSDavid Malone 	DO(ip6s_redirectsent, 13, 0);
225f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_UDP], 14, 35);
226f29d8c1aSDavid Malone 	DO(ip6s_badoptions, 14, 0);
227f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35);
228f29d8c1aSDavid Malone 	DO(ip6s_notmember, 15, 0);
229f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35);
230f29d8c1aSDavid Malone 	DO(ip6s_delivered, 16, 0);
231f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35);
232f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35);
233f29d8c1aSDavid Malone 	DO(ip6s_badscope, 18, 0);
234f29d8c1aSDavid Malone 	DO(ip6s_nxthist[IPPROTO_NONE], 19, 35);
235f29d8c1aSDavid Malone 	DO(ip6s_sources_none, 19, 0);
236f29d8c1aSDavid Malone #undef DO
237f29d8c1aSDavid Malone }
238f29d8c1aSDavid Malone 
239f29d8c1aSDavid Malone int
240f29d8c1aSDavid Malone initip6(void)
241f29d8c1aSDavid Malone {
242f29d8c1aSDavid Malone 	size_t len;
243f29d8c1aSDavid Malone 	int name[4];
244f29d8c1aSDavid Malone 
245f29d8c1aSDavid Malone 	name[0] = CTL_NET;
246f29d8c1aSDavid Malone 	name[1] = PF_INET6;
247f29d8c1aSDavid Malone 	name[2] = IPPROTO_IPV6;
248f29d8c1aSDavid Malone 	name[3] = IPV6CTL_STATS;
249f29d8c1aSDavid Malone 
250f29d8c1aSDavid Malone 	len = 0;
251f29d8c1aSDavid Malone 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
252f29d8c1aSDavid Malone 		error("sysctl getting ip6stat size failed");
253f29d8c1aSDavid Malone 		return 0;
254f29d8c1aSDavid Malone 	}
255f29d8c1aSDavid Malone 	if (len > sizeof curstat) {
256f29d8c1aSDavid Malone 		error("ip6stat structure has grown--recompile systat!");
257f29d8c1aSDavid Malone 		return 0;
258f29d8c1aSDavid Malone 	}
259f29d8c1aSDavid Malone 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
260f29d8c1aSDavid Malone 		error("sysctl getting ip6stat failed");
261f29d8c1aSDavid Malone 		return 0;
262f29d8c1aSDavid Malone 	}
263f29d8c1aSDavid Malone 	oldstat = initstat;
264f29d8c1aSDavid Malone 	return 1;
265f29d8c1aSDavid Malone }
266f29d8c1aSDavid Malone 
267f29d8c1aSDavid Malone void
268f29d8c1aSDavid Malone resetip6(void)
269f29d8c1aSDavid Malone {
270f29d8c1aSDavid Malone 	size_t len;
271f29d8c1aSDavid Malone 	int name[4];
272f29d8c1aSDavid Malone 
273f29d8c1aSDavid Malone 	name[0] = CTL_NET;
274f29d8c1aSDavid Malone 	name[1] = PF_INET6;
275f29d8c1aSDavid Malone 	name[2] = IPPROTO_IPV6;
276f29d8c1aSDavid Malone 	name[3] = IPV6CTL_STATS;
277f29d8c1aSDavid Malone 
278f29d8c1aSDavid Malone 	len = sizeof initstat;
279f29d8c1aSDavid Malone 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
280f29d8c1aSDavid Malone 		error("sysctl getting ipstat failed");
281f29d8c1aSDavid Malone 	}
282f29d8c1aSDavid Malone 
283f29d8c1aSDavid Malone 	oldstat = initstat;
284f29d8c1aSDavid Malone }
285f29d8c1aSDavid Malone 
286f29d8c1aSDavid Malone void
287f29d8c1aSDavid Malone fetchip6(void)
288f29d8c1aSDavid Malone {
289f29d8c1aSDavid Malone 	int name[4];
290f29d8c1aSDavid Malone 	size_t len;
291f29d8c1aSDavid Malone 
292f29d8c1aSDavid Malone 	oldstat = curstat;
293f29d8c1aSDavid Malone 	name[0] = CTL_NET;
294f29d8c1aSDavid Malone 	name[1] = PF_INET6;
295f29d8c1aSDavid Malone 	name[2] = IPPROTO_IPV6;
296f29d8c1aSDavid Malone 	name[3] = IPV6CTL_STATS;
297f29d8c1aSDavid Malone 	len = sizeof curstat;
298f29d8c1aSDavid Malone 
299f29d8c1aSDavid Malone 	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
300f29d8c1aSDavid Malone 		return;
301f29d8c1aSDavid Malone }
302f29d8c1aSDavid Malone 
303f29d8c1aSDavid Malone #endif
304