ip6.c revision 240605
162143Sarchie/*-
262143Sarchie * Copyright (c) 1980, 1992, 1993
362143Sarchie *	The Regents of the University of California.  All rights reserved.
462143Sarchie *
562143Sarchie * Redistribution and use in source and binary forms, with or without
662143Sarchie * modification, are permitted provided that the following conditions
762143Sarchie * are met:
862143Sarchie * 1. Redistributions of source code must retain the above copyright
962143Sarchie *    notice, this list of conditions and the following disclaimer.
1062143Sarchie * 2. Redistributions in binary form must reproduce the above copyright
1162143Sarchie *    notice, this list of conditions and the following disclaimer in the
1262143Sarchie *    documentation and/or other materials provided with the distribution.
1362143Sarchie * 4. Neither the name of the University nor the names of its contributors
1462143Sarchie *    may be used to endorse or promote products derived from this software
1562143Sarchie *    without specific prior written permission.
1662143Sarchie *
1762143Sarchie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1862143Sarchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1962143Sarchie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2062143Sarchie * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2162143Sarchie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2262143Sarchie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2362143Sarchie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2462143Sarchie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2562143Sarchie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2662143Sarchie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2762143Sarchie * SUCH DAMAGE.
2862143Sarchie */
2962143Sarchie
3062143Sarchie#include <sys/cdefs.h>
3162143Sarchie
3262143Sarchie__FBSDID("$FreeBSD: head/usr.bin/systat/ip6.c 240605 2012-09-17 13:36:47Z melifaro $");
3362143Sarchie
3462143Sarchie#ifdef lint
3562143Sarchiestatic const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
3662143Sarchie#endif
3762143Sarchie
3862143Sarchie/* From:
3962143Sarchie	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
4062143Sarchie*/
4162143Sarchie
4262143Sarchie#ifdef INET6
4362143Sarchie#include <sys/param.h>
4462143Sarchie#include <sys/types.h>
4562143Sarchie#include <sys/socket.h>
4662143Sarchie#include <sys/sysctl.h>
4762143Sarchie
4862143Sarchie#include <netinet/in.h>
4962143Sarchie#include <netinet/in_systm.h>
5062143Sarchie#include <netinet/ip.h>
5162143Sarchie#include <netinet6/ip6_var.h>
5262143Sarchie
5362143Sarchie#include <stdlib.h>
5462143Sarchie#include <string.h>
5562143Sarchie#include <paths.h>
5662143Sarchie
5762143Sarchie#include "systat.h"
5862143Sarchie#include "extern.h"
5962143Sarchie#include "mode.h"
6062143Sarchie
6162143Sarchiestatic struct ip6stat curstat, initstat, oldstat;
6262143Sarchie
6362143Sarchie/*-
6462143Sarchie--0         1         2         3         4         5         6         7
6562143Sarchie--0123456789012345678901234567890123456789012345678901234567890123456789012345
6662143Sarchie00        IPv6 Input                         IPv6 Output
6762678Sjulian019999999 total packets received   999999999 total packets sent
6862143Sarchie029999999 - too short for header   999999999 - generated locally
6962143Sarchie039999999 - too short for data     999999999 - output drops
7062143Sarchie049999999 - with invalid version   999999999 output fragments generated
7162143Sarchie059999999 total fragments received 999999999 - fragmentation failed
7262143Sarchie069999999 - fragments dropped      999999999 destinations unreachable
7362143Sarchie079999999 - fragments timed out    999999999 packets output via raw IP
7462143Sarchie089999999 - fragments overflown
7562143Sarchie099999999 - packets reassembled ok           Input next-header histogram
7664358Sarchie109999999 packets forwarded        999999999  - destination options
7764358Sarchie119999999 - unreachable dests      999999999  - hop-by-hop options
7862143Sarchie129999999 - redirects generated    999999999  - IPv4
7962143Sarchie139999999 option errors            999999999  - TCP
8062143Sarchie149999999 unwanted multicasts      999999999  - UDP
8162143Sarchie159999999 delivered to upper layer 999999999  - IPv6
8262143Sarchie169999999 bad scope packets        999999999  - routing header
8362143Sarchie179999999 address selection failed 999999999  - fragmentation header
8462143Sarchie18                                 999999999  - ICMP6
8562143Sarchie19                                 999999999  - none
8662143Sarchie--0123456789012345678901234567890123456789012345678901234567890123456789012345
8762143Sarchie--0         1         2         3         4         5         6         7
8862143Sarchie*/
8962143Sarchie
9062143SarchieWINDOW *
9162143Sarchieopenip6(void)
9262143Sarchie{
9362143Sarchie	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
9462143Sarchie}
9562143Sarchie
9662143Sarchievoid
9762143Sarchiecloseip6(WINDOW *w)
9862143Sarchie{
9962143Sarchie	if (w == NULL)
10062143Sarchie		return;
10162143Sarchie	wclear(w);
10262143Sarchie	wrefresh(w);
10369922Sjulian	delwin(w);
10462143Sarchie}
10562143Sarchie
10662143Sarchievoid
10762143Sarchielabelip6(void)
10864653Sarchie{
10964653Sarchie	wmove(wnd, 0, 0); wclrtoeol(wnd);
11064653Sarchie#define L(row, str) mvwprintw(wnd, row, 10, str)
11164653Sarchie#define R(row, str) mvwprintw(wnd, row, 45, str);
11264653Sarchie	L(0, "IPv6 Input");		R(0, "IPv6 Output");
11364653Sarchie	L(1, "total packets received");	R(1, "total packets sent");
11464653Sarchie	L(2, "- too short for header");	R(2, "- generated locally");
11564653Sarchie	L(3, "- too short for data");	R(3, "- output drops");
11664653Sarchie	L(4, "- with invalid version");	R(4, "output fragments generated");
11764653Sarchie	L(5, "total fragments received"); R(5, "- fragmentation failed");
11864653Sarchie	L(6, "- fragments dropped");	R(6, "destinations unreachable");
11964358Sarchie	L(7, "- fragments timed out");	R(7, "packets output via raw IP");
12064358Sarchie	L(8, "- fragments overflown");
12162143Sarchie	L(9, "- packets reassembled ok"); R(9, "Input next-header histogram");
12262143Sarchie	L(10, "packets forwarded");	R(10, " - destination options");
12362143Sarchie	L(11, "- unreachable dests");	R(11, " - hop-by-hop options");
12462143Sarchie	L(12, "- redirects generated");	R(12, " - IPv4");
12562143Sarchie	L(13, "option errors");		R(13, " - TCP");
12662143Sarchie	L(14, "unwanted multicasts");	R(14, " - UDP");
12762143Sarchie	L(15, "delivered to upper layer"); R(15, " - IPv6");
12862143Sarchie	L(16, "bad scope packets");	R(16, " - routing header");
12962143Sarchie	L(17, "address selection failed"); R(17, " - fragmentation header");
13062143Sarchie					R(18, " - ICMP6");
13162143Sarchie					R(19, " - none");
13262143Sarchie#undef L
13362143Sarchie#undef R
13462143Sarchie}
13562143Sarchie
13662143Sarchiestatic void
13764358Sarchiedomode(struct ip6stat *ret)
13864358Sarchie{
13964358Sarchie	const struct ip6stat *sub;
14064358Sarchie	int divisor = 1, i;
14164358Sarchie
14264358Sarchie	switch(currentmode) {
14364358Sarchie	case display_RATE:
14464358Sarchie		sub = &oldstat;
14564358Sarchie		divisor = (delay > 1000000) ? delay / 1000000 : 1;
14664653Sarchie		break;
14764653Sarchie	case display_DELTA:
14864653Sarchie		sub = &oldstat;
14964653Sarchie		break;
15064653Sarchie	case display_SINCE:
15164653Sarchie		sub = &initstat;
15264653Sarchie		break;
15364653Sarchie	default:
15464653Sarchie		*ret = curstat;
15564653Sarchie		return;
15664653Sarchie	}
15764653Sarchie#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
15864653Sarchie	DO(ip6s_total);
15964653Sarchie	DO(ip6s_tooshort);
16064358Sarchie	DO(ip6s_toosmall);
16164358Sarchie	DO(ip6s_fragments);
16264358Sarchie	DO(ip6s_fragdropped);
16364358Sarchie	DO(ip6s_fragtimeout);
16464358Sarchie	DO(ip6s_fragoverflow);
16564358Sarchie	DO(ip6s_forward);
16664358Sarchie	DO(ip6s_cantforward);
16764653Sarchie	DO(ip6s_redirectsent);
16864653Sarchie	DO(ip6s_delivered);
16964653Sarchie	DO(ip6s_localout);
17064653Sarchie	DO(ip6s_odropped);
17164653Sarchie	DO(ip6s_reassembled);
17264653Sarchie	DO(ip6s_fragmented);
17364653Sarchie	DO(ip6s_ofragments);
17464358Sarchie	DO(ip6s_cantfrag);
17564358Sarchie	DO(ip6s_badoptions);
17664358Sarchie	DO(ip6s_noroute);
17764358Sarchie	DO(ip6s_badvers);
17864358Sarchie	DO(ip6s_rawout);
17962143Sarchie	DO(ip6s_notmember);
18062143Sarchie	for (i = 0; i < 256; i++)
18162143Sarchie		DO(ip6s_nxthist[i]);
18262143Sarchie	DO(ip6s_badscope);
18362143Sarchie	DO(ip6s_sources_none);
18462143Sarchie#undef DO
18562143Sarchie}
18662143Sarchie
18762143Sarchievoid
18862143Sarchieshowip6(void)
18962143Sarchie{
19062143Sarchie	struct ip6stat stats;
19169922Sjulian	u_long totalout;
19262143Sarchie
19362143Sarchie	domode(&stats);
19462143Sarchie	totalout = stats.ip6s_forward + stats.ip6s_localout;
19562143Sarchie
19662143Sarchie#define DO(stat, row, col) \
19762143Sarchie	mvwprintw(wnd, row, col, "%9lu", stats.stat)
19862143Sarchie
19962143Sarchie	DO(ip6s_total, 1, 0);
20062143Sarchie	mvwprintw(wnd, 1, 35, "%9lu", totalout);
20162143Sarchie	DO(ip6s_tooshort, 2, 0);
20262143Sarchie	DO(ip6s_localout, 2, 35);
20362143Sarchie	DO(ip6s_toosmall, 3, 0);
20462143Sarchie	DO(ip6s_odropped, 3, 35);
20562143Sarchie	DO(ip6s_badvers, 4, 0);
20662143Sarchie	DO(ip6s_ofragments, 4, 35);
20762143Sarchie	DO(ip6s_fragments, 5, 0);
20862143Sarchie	DO(ip6s_cantfrag, 5, 35);
20962143Sarchie	DO(ip6s_fragdropped, 6, 0);
21062143Sarchie	DO(ip6s_noroute, 6, 35);
21162143Sarchie	DO(ip6s_fragtimeout, 7, 0);
21262143Sarchie	DO(ip6s_rawout, 7, 35);
21362143Sarchie	DO(ip6s_fragoverflow, 8, 0);
21462143Sarchie	DO(ip6s_reassembled, 9, 0);
21562143Sarchie	DO(ip6s_forward, 10, 0);
21662143Sarchie	DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35);
21762143Sarchie	DO(ip6s_cantforward, 11, 0);
21862143Sarchie	DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35);
21962143Sarchie	DO(ip6s_redirectsent, 12, 0);
22062143Sarchie	DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35);
22162143Sarchie	DO(ip6s_badoptions, 13, 0);
22262143Sarchie	DO(ip6s_nxthist[IPPROTO_TCP], 13, 35);
22362143Sarchie	DO(ip6s_notmember, 14, 0);
22462143Sarchie	DO(ip6s_nxthist[IPPROTO_UDP], 14, 35);
22562143Sarchie	DO(ip6s_delivered, 15, 0);
22662143Sarchie	DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35);
22762143Sarchie	DO(ip6s_badscope, 16, 0);
22862143Sarchie	DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35);
22962143Sarchie	DO(ip6s_sources_none, 17, 0);
23062143Sarchie	DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35);
23162143Sarchie	DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35);
23262143Sarchie	DO(ip6s_nxthist[IPPROTO_NONE], 19, 35);
23362143Sarchie#undef DO
23462143Sarchie}
23562143Sarchie
23662143Sarchieint
23762143Sarchieinitip6(void)
23862143Sarchie{
23962143Sarchie	size_t len;
24062143Sarchie	int name[4];
24162143Sarchie
24262143Sarchie	name[0] = CTL_NET;
24362143Sarchie	name[1] = PF_INET6;
24462143Sarchie	name[2] = IPPROTO_IPV6;
24569922Sjulian	name[3] = IPV6CTL_STATS;
24662143Sarchie
24762143Sarchie	len = 0;
24862143Sarchie	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
24962143Sarchie		error("sysctl getting ip6stat size failed");
25062143Sarchie		return 0;
25162143Sarchie	}
25262143Sarchie	if (len > sizeof curstat) {
25362143Sarchie		error("ip6stat structure has grown--recompile systat!");
25462143Sarchie		return 0;
25562143Sarchie	}
25662143Sarchie	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
25762143Sarchie		error("sysctl getting ip6stat failed");
25862143Sarchie		return 0;
25962143Sarchie	}
26062143Sarchie	oldstat = initstat;
26162143Sarchie	return 1;
26269922Sjulian}
26362143Sarchie
26462143Sarchievoid
26562143Sarchieresetip6(void)
26662143Sarchie{
26762143Sarchie	size_t len;
26862143Sarchie	int name[4];
26962143Sarchie
27062143Sarchie	name[0] = CTL_NET;
27162143Sarchie	name[1] = PF_INET6;
27262143Sarchie	name[2] = IPPROTO_IPV6;
27362143Sarchie	name[3] = IPV6CTL_STATS;
27462143Sarchie
27562143Sarchie	len = sizeof initstat;
27662143Sarchie	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
27762143Sarchie		error("sysctl getting ipstat failed");
27862143Sarchie	}
27962143Sarchie
28062143Sarchie	oldstat = initstat;
28162143Sarchie}
28262143Sarchie
28369922Sjulianvoid
28462143Sarchiefetchip6(void)
28562143Sarchie{
28662143Sarchie	int name[4];
28762143Sarchie	size_t len;
28862143Sarchie
28962143Sarchie	oldstat = curstat;
29062143Sarchie	name[0] = CTL_NET;
29162143Sarchie	name[1] = PF_INET6;
29262143Sarchie	name[2] = IPPROTO_IPV6;
29362143Sarchie	name[3] = IPV6CTL_STATS;
29462143Sarchie	len = sizeof curstat;
29562143Sarchie
29662143Sarchie	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
29762143Sarchie		return;
29862143Sarchie}
29962143Sarchie
30062143Sarchie#endif
30162143Sarchie