ip6.c revision 118318
1156090Sdeischen/*-
2156103Sdelphij * Copyright (c) 1980, 1992, 1993
3156090Sdeischen *	The Regents of the University of California.  All rights reserved.
4156090Sdeischen *
5156090Sdeischen * Redistribution and use in source and binary forms, with or without
6156090Sdeischen * modification, are permitted provided that the following conditions
7156090Sdeischen * are met:
8156090Sdeischen * 1. Redistributions of source code must retain the above copyright
9156090Sdeischen *    notice, this list of conditions and the following disclaimer.
10156090Sdeischen * 2. Redistributions in binary form must reproduce the above copyright
11156090Sdeischen *    notice, this list of conditions and the following disclaimer in the
12156090Sdeischen *    documentation and/or other materials provided with the distribution.
13156090Sdeischen * 3. All advertising materials mentioning features or use of this software
14156090Sdeischen *    must display the following acknowledgement:
15156090Sdeischen *	This product includes software developed by the University of
16156090Sdeischen *	California, Berkeley and its contributors.
17156090Sdeischen * 4. Neither the name of the University nor the names of its contributors
18156090Sdeischen *    may be used to endorse or promote products derived from this software
19156090Sdeischen *    without specific prior written permission.
20156090Sdeischen *
21156090Sdeischen * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22156090Sdeischen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23156090Sdeischen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24156090Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25156090Sdeischen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26156090Sdeischen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27156090Sdeischen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28156090Sdeischen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29156090Sdeischen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30156090Sdeischen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31156090Sdeischen * SUCH DAMAGE.
32156090Sdeischen */
33156090Sdeischen
34156090Sdeischen#include <sys/cdefs.h>
35156090Sdeischen
36156090Sdeischen__FBSDID("$FreeBSD: head/usr.bin/systat/ip6.c 118318 2003-08-01 20:28:20Z dwmalone $");
37156090Sdeischen
38156090Sdeischen#ifdef lint
39156090Sdeischenstatic const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
40156090Sdeischen#endif
41156090Sdeischen
42156090Sdeischen/* From:
43156090Sdeischen	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
44156090Sdeischen*/
45156090Sdeischen
46156090Sdeischen#ifdef INET6
47156090Sdeischen#include <sys/param.h>
48156090Sdeischen#include <sys/types.h>
49156090Sdeischen#include <sys/socket.h>
50156090Sdeischen#include <sys/sysctl.h>
51156090Sdeischen
52156090Sdeischen#include <netinet/in.h>
53156090Sdeischen#include <netinet/in_systm.h>
54156090Sdeischen#include <netinet/ip.h>
55156090Sdeischen#include <netinet6/ip6_var.h>
56156090Sdeischen
57156090Sdeischen#include <stdlib.h>
58156090Sdeischen#include <string.h>
59156090Sdeischen#include <paths.h>
60156090Sdeischen
61156090Sdeischen#include "systat.h"
62156090Sdeischen#include "extern.h"
63156090Sdeischen#include "mode.h"
64156090Sdeischen
65156090Sdeischenstatic struct ip6stat curstat, initstat, oldstat;
66
67/*-
68--0         1         2         3         4         5         6         7
69--0123456789012345678901234567890123456789012345678901234567890123456789012345
7001        IPv6 Input                         IPv6 Output
71029999999 total packets received   999999999 total packets sent
72039999999 - too short for header   999999999 - generated locally
73049999999 - too short for data     999999999 - output drops
74059999999 - with invalid version   999999999 output fragments generated
75069999999 total fragments received 999999999 - fragmentation failed
76079999999 - fragments dropped      999999999 destinations unreachable
77089999999 - fragments timed out    999999999 packets output via raw IP
78099999999 - fragments overflown              Input next-header histogram
79109999999 - packets reassembled ok 999999999  - destination options
80119999999 packets forwarded        999999999  - hop-by-hop options
81129999999 - unreachable dests      999999999  - IPv4
82139999999 - redirects generated    999999999  - TCP
83149999999 option errors            999999999  - UDP
84159999999 unwanted multicasts      999999999  - IPv6
85169999999 delivered to upper layer 999999999  - routing header
8617                                 999999999  - fragmentation header
87189999999 bad scope packets        999999999  - ICMP6
88199999999 address selection failed 999999999  - none
89--0123456789012345678901234567890123456789012345678901234567890123456789012345
90--0         1         2         3         4         5         6         7
91*/
92
93WINDOW *
94openip6(void)
95{
96	return (subwin(stdscr, LINES-4-1, 0, 4, 0));
97}
98
99void
100closeip6(w)
101	WINDOW *w;
102{
103	if (w == NULL)
104		return;
105	wclear(w);
106	wrefresh(w);
107	delwin(w);
108}
109
110void
111labelip6(void)
112{
113	wmove(wnd, 0, 0); wclrtoeol(wnd);
114#define L(row, str) mvwprintw(wnd, row, 10, str)
115#define R(row, str) mvwprintw(wnd, row, 45, str);
116	L(1, "IPv6 Input");		R(1, "IPv6 Output");
117	L(2, "total packets received");	R(2, "total packets sent");
118	L(3, "- too short for header");	R(3, "- generated locally");
119	L(4, "- too short for data");	R(4, "- output drops");
120	L(5, "- with invalid version");	R(5, "output fragments generated");
121	L(6, "total fragments received"); R(6, "- fragmentation failed");
122	L(7, "- fragments dropped");	R(7, "destinations unreachable");
123	L(8, "- fragments timed out");	R(8, "packets output via raw IP");
124	L(9, "- fragments overflown");	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					R(17, " - fragmentation header");
133	L(18, "bad scope packets");	R(18, " - ICMP6");
134	L(19, "address selection failed"); R(19, " - none");
135#undef L
136#undef R
137}
138
139static void
140domode(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 = naptime;
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_forward);
169	DO(ip6s_cantforward);
170	DO(ip6s_redirectsent);
171	DO(ip6s_delivered);
172	DO(ip6s_localout);
173	DO(ip6s_odropped);
174	DO(ip6s_reassembled);
175	DO(ip6s_fragmented);
176	DO(ip6s_ofragments);
177	DO(ip6s_cantfrag);
178	DO(ip6s_badoptions);
179	DO(ip6s_noroute);
180	DO(ip6s_badvers);
181	DO(ip6s_rawout);
182	DO(ip6s_notmember);
183	for (i = 0; i < 256; i++)
184		DO(ip6s_nxthist[i]);
185	DO(ip6s_badscope);
186	DO(ip6s_sources_none);
187#undef DO
188}
189
190void
191showip6(void)
192{
193	struct ip6stat stats;
194	u_long totalout;
195
196	domode(&stats);
197	totalout = stats.ip6s_forward + stats.ip6s_localout;
198
199#define DO(stat, row, col) \
200	mvwprintw(wnd, row, col, "%9lu", stats.stat)
201
202	DO(ip6s_total, 2, 0);
203	mvwprintw(wnd, 2, 35, "%9lu", totalout);
204	DO(ip6s_localout, 3, 35);
205	DO(ip6s_tooshort, 3, 0);
206	DO(ip6s_odropped, 4, 35);
207	DO(ip6s_toosmall, 4, 0);
208	DO(ip6s_ofragments, 5, 35);
209	DO(ip6s_badvers, 5, 0);
210	DO(ip6s_cantfrag, 6, 35);
211	DO(ip6s_fragments, 6, 0);
212	DO(ip6s_noroute, 7, 35);
213	DO(ip6s_fragdropped, 7, 0);
214	DO(ip6s_rawout, 8, 35);
215	DO(ip6s_fragtimeout, 8, 0);
216	DO(ip6s_fragoverflow, 9, 0);
217	DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35);
218	DO(ip6s_reassembled, 10, 0);
219	DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35);
220	DO(ip6s_forward, 11, 0);
221	DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35);
222	DO(ip6s_cantforward, 12, 0);
223	DO(ip6s_nxthist[IPPROTO_TCP], 13, 35);
224	DO(ip6s_redirectsent, 13, 0);
225	DO(ip6s_nxthist[IPPROTO_UDP], 14, 35);
226	DO(ip6s_badoptions, 14, 0);
227	DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35);
228	DO(ip6s_notmember, 15, 0);
229	DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35);
230	DO(ip6s_delivered, 16, 0);
231	DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35);
232	DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35);
233	DO(ip6s_badscope, 18, 0);
234	DO(ip6s_nxthist[IPPROTO_NONE], 19, 35);
235	DO(ip6s_sources_none, 19, 0);
236#undef DO
237}
238
239int
240initip6(void)
241{
242	size_t len;
243	int name[4];
244
245	name[0] = CTL_NET;
246	name[1] = PF_INET6;
247	name[2] = IPPROTO_IPV6;
248	name[3] = IPV6CTL_STATS;
249
250	len = 0;
251	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
252		error("sysctl getting ip6stat size failed");
253		return 0;
254	}
255	if (len > sizeof curstat) {
256		error("ip6stat structure has grown--recompile systat!");
257		return 0;
258	}
259	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
260		error("sysctl getting ip6stat failed");
261		return 0;
262	}
263	oldstat = initstat;
264	return 1;
265}
266
267void
268resetip6(void)
269{
270	size_t len;
271	int name[4];
272
273	name[0] = CTL_NET;
274	name[1] = PF_INET6;
275	name[2] = IPPROTO_IPV6;
276	name[3] = IPV6CTL_STATS;
277
278	len = sizeof initstat;
279	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
280		error("sysctl getting ipstat failed");
281	}
282
283	oldstat = initstat;
284}
285
286void
287fetchip6(void)
288{
289	int name[4];
290	size_t len;
291
292	oldstat = curstat;
293	name[0] = CTL_NET;
294	name[1] = PF_INET6;
295	name[2] = IPPROTO_IPV6;
296	name[3] = IPV6CTL_STATS;
297	len = sizeof curstat;
298
299	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
300		return;
301}
302
303#endif
304