ip6.c revision 158161
150724Scg/*-
253413Sroger * Copyright (c) 1980, 1992, 1993
353413Sroger *	The Regents of the University of California.  All rights reserved.
450724Scg *
553413Sroger * Redistribution and use in source and binary forms, with or without
650724Scg * modification, are permitted provided that the following conditions
750724Scg * are met:
850724Scg * 1. Redistributions of source code must retain the above copyright
950724Scg *    notice, this list of conditions and the following disclaimer.
1050724Scg * 2. Redistributions in binary form must reproduce the above copyright
1150724Scg *    notice, this list of conditions and the following disclaimer in the
1250724Scg *    documentation and/or other materials provided with the distribution.
1350724Scg * 3. All advertising materials mentioning features or use of this software
1450724Scg *    must display the following acknowledgement:
1550724Scg *	This product includes software developed by the University of
1650724Scg *	California, Berkeley and its contributors.
1750724Scg * 4. Neither the name of the University nor the names of its contributors
1850724Scg *    may be used to endorse or promote products derived from this software
1950724Scg *    without specific prior written permission.
2050724Scg *
2150724Scg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2250724Scg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350724Scg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450724Scg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2550724Scg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2650724Scg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2750724Scg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2850724Scg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2950724Scg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3050724Scg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3150724Scg * SUCH DAMAGE.
3250724Scg */
3350724Scg
3450724Scg#include <sys/cdefs.h>
3550724Scg
3650724Scg__FBSDID("$FreeBSD: head/usr.bin/systat/ip6.c 158161 2006-04-30 04:47:23Z bde $");
3750724Scg
3850724Scg#ifdef lint
3950724Scgstatic const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
4050724Scg#endif
4150733Speter
4250724Scg/* From:
4350724Scg	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
4453413Sroger*/
4553413Sroger
4653413Sroger#ifdef INET6
4754831Scg#include <sys/param.h>
4854831Scg#include <sys/types.h>
4953413Sroger#include <sys/socket.h>
5053413Sroger#include <sys/sysctl.h>
5153413Sroger
5253413Sroger#include <netinet/in.h>
5353465Scg#include <netinet/in_systm.h>
5453465Scg#include <netinet/ip.h>
5553465Scg#include <netinet6/ip6_var.h>
5650724Scg
5750724Scg#include <stdlib.h>
5850724Scg#include <string.h>
5950724Scg#include <paths.h>
6053413Sroger
6153413Sroger#include "systat.h"
6270134Scg#include "extern.h"
6370134Scg#include "mode.h"
6453413Sroger
6553413Srogerstatic struct ip6stat curstat, initstat, oldstat;
6653413Sroger
6750724Scg/*-
6850724Scg--0         1         2         3         4         5         6         7
6950724Scg--0123456789012345678901234567890123456789012345678901234567890123456789012345
7050724Scg00        IPv6 Input                         IPv6 Output
7153413Sroger019999999 total packets received   999999999 total packets sent
7256154Speter029999999 - too short for header   999999999 - generated locally
7376086Scg039999999 - too short for data     999999999 - output drops
7450724Scg049999999 - with invalid version   999999999 output fragments generated
7576086Scg059999999 total fragments received 999999999 - fragmentation failed
7676086Scg069999999 - fragments dropped      999999999 destinations unreachable
7776086Scg079999999 - fragments timed out    999999999 packets output via raw IP
7876086Scg089999999 - fragments overflown
7976086Scg099999999 - packets reassembled ok           Input next-header histogram
8076086Scg109999999 packets forwarded        999999999  - destination options
8176086Scg119999999 - unreachable dests      999999999  - hop-by-hop options
8276086Scg129999999 - redirects generated    999999999  - IPv4
8376086Scg139999999 option errors            999999999  - TCP
8476086Scg149999999 unwanted multicasts      999999999  - UDP
8576086Scg159999999 delivered to upper layer 999999999  - IPv6
8659019Scg169999999 bad scope packets        999999999  - routing header
8759019Scg179999999 address selection failed 999999999  - fragmentation header
8850724Scg18                                 999999999  - ICMP6
8950724Scg19                                 999999999  - none
9050724Scg--0123456789012345678901234567890123456789012345678901234567890123456789012345
9155209Scg--0         1         2         3         4         5         6         7
9250724Scg*/
9374763Scg
9474763ScgWINDOW *
9554831Scgopenip6(void)
9670321Scg{
9755209Scg	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
9850724Scg}
9955209Scg
10050724Scgvoid
10150724Scgcloseip6(w)
10250724Scg	WINDOW *w;
10350724Scg{
10465644Scg	if (w == NULL)
10565644Scg		return;
10665644Scg	wclear(w);
10765644Scg	wrefresh(w);
10859019Scg	delwin(w);
10954831Scg}
11050724Scg
11150724Scgvoid
11250724Scglabelip6(void)
11350724Scg{
11455209Scg	wmove(wnd, 0, 0); wclrtoeol(wnd);
11550724Scg#define L(row, str) mvwprintw(wnd, row, 10, str)
11650724Scg#define R(row, str) mvwprintw(wnd, row, 45, str);
11754831Scg	L(0, "IPv6 Input");		R(0, "IPv6 Output");
11853413Sroger	L(1, "total packets received");	R(1, "total packets sent");
11954831Scg	L(2, "- too short for header");	R(2, "- generated locally");
12050724Scg	L(3, "- too short for data");	R(3, "- output drops");
12155209Scg	L(4, "- with invalid version");	R(4, "output fragments generated");
12255209Scg	L(5, "total fragments received"); R(5, "- fragmentation failed");
12355209Scg	L(6, "- fragments dropped");	R(6, "destinations unreachable");
12455209Scg	L(7, "- fragments timed out");	R(7, "packets output via raw IP");
12571505Scg	L(8, "- fragments overflown");
12654831Scg	L(9, "- packets reassembled ok"); R(9, "Input next-header histogram");
12754831Scg	L(10, "packets forwarded");	R(10, " - destination options");
12850724Scg	L(11, "- unreachable dests");	R(11, " - hop-by-hop options");
12964881Scg	L(12, "- redirects generated");	R(12, " - IPv4");
13064881Scg	L(13, "option errors");		R(13, " - TCP");
13164881Scg	L(14, "unwanted multicasts");	R(14, " - UDP");
13264881Scg	L(15, "delivered to upper layer"); R(15, " - IPv6");
13364881Scg	L(16, "bad scope packets");	R(16, " - routing header");
13464881Scg	L(17, "address selection failed"); R(17, " - fragmentation header");
13550724Scg					R(18, " - ICMP6");
13674763Scg					R(19, " - none");
13750724Scg#undef L
13864881Scg#undef R
13964881Scg}
14064881Scg
14164881Scgstatic void
14264881Scgdomode(struct ip6stat *ret)
14364881Scg{
14450724Scg	const struct ip6stat *sub;
14574763Scg	int divisor = 1, i;
14650724Scg
14750724Scg	switch(currentmode) {
14850724Scg	case display_RATE:
14950724Scg		sub = &oldstat;
15050724Scg		divisor = naptime;
15150724Scg		break;
15250724Scg	case display_DELTA:
15350724Scg		sub = &oldstat;
15450724Scg		break;
15550724Scg	case display_SINCE:
15650724Scg		sub = &initstat;
15750724Scg		break;
15850724Scg	default:
15950724Scg		*ret = curstat;
16050724Scg		return;
16150724Scg	}
16250724Scg#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16350724Scg	DO(ip6s_total);
16454831Scg	DO(ip6s_tooshort);
16554831Scg	DO(ip6s_toosmall);
16650724Scg	DO(ip6s_fragments);
16770134Scg	DO(ip6s_fragdropped);
16870134Scg	DO(ip6s_fragtimeout);
16970134Scg	DO(ip6s_fragoverflow);
17050724Scg	DO(ip6s_forward);
17174763Scg	DO(ip6s_cantforward);
17250724Scg	DO(ip6s_redirectsent);
17350724Scg	DO(ip6s_delivered);
17450724Scg	DO(ip6s_localout);
17550724Scg	DO(ip6s_odropped);
17650724Scg	DO(ip6s_reassembled);
17750724Scg	DO(ip6s_fragmented);
17850724Scg	DO(ip6s_ofragments);
17950724Scg	DO(ip6s_cantfrag);
18050724Scg	DO(ip6s_badoptions);
18150724Scg	DO(ip6s_noroute);
18250724Scg	DO(ip6s_badvers);
18350724Scg	DO(ip6s_rawout);
18450724Scg	DO(ip6s_notmember);
18550724Scg	for (i = 0; i < 256; i++)
18650724Scg		DO(ip6s_nxthist[i]);
18750724Scg	DO(ip6s_badscope);
18874763Scg	DO(ip6s_sources_none);
18950724Scg#undef DO
19050724Scg}
19150724Scg
19250724Scgvoid
19350724Scgshowip6(void)
19450724Scg{
19550724Scg	struct ip6stat stats;
19650724Scg	u_long totalout;
19750724Scg
19850724Scg	domode(&stats);
19950724Scg	totalout = stats.ip6s_forward + stats.ip6s_localout;
20050724Scg
20150724Scg#define DO(stat, row, col) \
20254831Scg	mvwprintw(wnd, row, col, "%9lu", stats.stat)
20350724Scg
20454831Scg	DO(ip6s_total, 1, 0);
20550724Scg	mvwprintw(wnd, 1, 35, "%9lu", totalout);
20650724Scg	DO(ip6s_tooshort, 2, 0);
20750724Scg	DO(ip6s_localout, 2, 35);
20850724Scg	DO(ip6s_toosmall, 3, 0);
20974763Scg	DO(ip6s_odropped, 3, 35);
21050724Scg	DO(ip6s_badvers, 4, 0);
21150724Scg	DO(ip6s_ofragments, 4, 35);
21250724Scg	DO(ip6s_fragments, 5, 0);
21350724Scg	DO(ip6s_cantfrag, 5, 35);
21450724Scg	DO(ip6s_fragdropped, 6, 0);
21550724Scg	DO(ip6s_noroute, 6, 35);
21650724Scg	DO(ip6s_fragtimeout, 7, 0);
21750724Scg	DO(ip6s_rawout, 7, 35);
21854831Scg	DO(ip6s_fragoverflow, 8, 0);
21954831Scg	DO(ip6s_reassembled, 9, 0);
22054831Scg	DO(ip6s_forward, 10, 0);
22154831Scg	DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35);
22254831Scg	DO(ip6s_cantforward, 11, 0);
22354831Scg	DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35);
22450724Scg	DO(ip6s_redirectsent, 12, 0);
22550724Scg	DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35);
22650724Scg	DO(ip6s_badoptions, 13, 0);
22770134Scg	DO(ip6s_nxthist[IPPROTO_TCP], 13, 35);
22870134Scg	DO(ip6s_notmember, 14, 0);
22970134Scg	DO(ip6s_nxthist[IPPROTO_UDP], 14, 35);
23070134Scg	DO(ip6s_delivered, 15, 0);
23170134Scg	DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35);
23270134Scg	DO(ip6s_badscope, 16, 0);
23370134Scg	DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35);
23470134Scg	DO(ip6s_sources_none, 17, 0);
23570134Scg	DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35);
23670134Scg	DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35);
23750724Scg	DO(ip6s_nxthist[IPPROTO_NONE], 19, 35);
23854831Scg#undef DO
23950724Scg}
24050724Scg
24150724Scgint
24250724Scginitip6(void)
24350724Scg{
24450724Scg	size_t len;
24550724Scg	int name[4];
24650724Scg
24750724Scg	name[0] = CTL_NET;
24850724Scg	name[1] = PF_INET6;
24950724Scg	name[2] = IPPROTO_IPV6;
25050724Scg	name[3] = IPV6CTL_STATS;
25154831Scg
25250724Scg	len = 0;
25350724Scg	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
25450724Scg		error("sysctl getting ip6stat size failed");
25550724Scg		return 0;
25650724Scg	}
25750724Scg	if (len > sizeof curstat) {
25850724Scg		error("ip6stat structure has grown--recompile systat!");
25974763Scg		return 0;
26050724Scg	}
26150724Scg	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
26250724Scg		error("sysctl getting ip6stat failed");
26350724Scg		return 0;
26450724Scg	}
26550724Scg	oldstat = initstat;
26650724Scg	return 1;
26770321Scg}
26870321Scg
26954831Scgvoid
27070321Scgresetip6(void)
27150724Scg{
27250724Scg	size_t len;
27350724Scg	int name[4];
27450724Scg
27570134Scg	name[0] = CTL_NET;
27650724Scg	name[1] = PF_INET6;
27750724Scg	name[2] = IPPROTO_IPV6;
27850724Scg	name[3] = IPV6CTL_STATS;
27950724Scg
28050724Scg	len = sizeof initstat;
28170321Scg	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
28270321Scg		error("sysctl getting ipstat failed");
28370321Scg	}
28450724Scg
28570321Scg	oldstat = initstat;
28670321Scg}
28770321Scg
28850724Scgvoid
28950724Scgfetchip6(void)
29050724Scg{
29150724Scg	int name[4];
29250724Scg	size_t len;
29350724Scg
29470134Scg	oldstat = curstat;
29550724Scg	name[0] = CTL_NET;
29650724Scg	name[1] = PF_INET6;
29750724Scg	name[2] = IPPROTO_IPV6;
29850724Scg	name[3] = IPV6CTL_STATS;
29950724Scg	len = sizeof curstat;
30050724Scg
30150724Scg	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
30250724Scg		return;
30350724Scg}
30450724Scg
30550724Scg#endif
30650724Scg