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