xref: /netbsd-src/usr.bin/systat/ip6.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ip6.c,v 1.15 2008/04/10 17:16:39 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2000 Andrew Doran <ad@NetBSD.org>
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: ip6.c,v 1.15 2008/04/10 17:16:39 thorpej Exp $");
33 #endif /* not lint */
34 
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip6.h>
41 #include <netinet6/ip6_var.h>
42 
43 #include <string.h>
44 
45 #include "systat.h"
46 #include "extern.h"
47 
48 #define LHD(row, str)		mvwprintw(wnd, row, 10, str)
49 #define RHD(row, str)		mvwprintw(wnd, row, 45, str);
50 #define SHOW(stat, row, col) \
51     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat[stat])
52 
53 enum update {
54 	UPDATE_TIME,
55 	UPDATE_BOOT,
56 	UPDATE_RUN,
57 };
58 
59 static enum update update = UPDATE_TIME;
60 static uint64_t curstat[IP6_NSTATS];
61 static uint64_t newstat[IP6_NSTATS];
62 static uint64_t oldstat[IP6_NSTATS];
63 
64 static struct nlist namelist[] = {
65 	{ .n_name = "_ip6stat" },
66 	{ .n_name = NULL }
67 };
68 
69 WINDOW *
70 openip6(void)
71 {
72 
73 	return (subwin(stdscr, -1, 0, 5, 0));
74 }
75 
76 void
77 closeip6(WINDOW *w)
78 {
79 
80 	if (w != NULL) {
81 		wclear(w);
82 		wrefresh(w);
83 		delwin(w);
84 	}
85 }
86 
87 void
88 labelip6(void)
89 {
90 
91 	wmove(wnd, 0, 0); wclrtoeol(wnd);
92 
93 	LHD(0,	"total packet received");
94 	LHD(1,	"  smaller than minimum");
95 	LHD(2,	"  data size < data length");
96 	LHD(3,	"  bad options");
97 	LHD(4,	"  incorrect version no");
98 	LHD(5,	"  headers not continuous");
99 	LHD(6,	"  packet for this host");
100 	LHD(7,	"  multicast we don't join");
101 	LHD(8,	"  too many headers");
102 	LHD(9,	"  tunneled packet w/o gif");
103 
104 	LHD(11,	"  fragment received");
105 	LHD(12,	"  fragment dropped");
106 	LHD(13,	"  fragment timeout");
107 	LHD(14,	"  fragment exceeded limit");
108 	LHD(15,	"  packet reassembled ok");
109 
110 #if 0
111 	LHD(17,	"one mbuf");
112 	LHD(18,	"one ext mbuf");
113 	LHD(19,	"two or more ext mbuf");
114 	LHD(20,	"two or more mbuf");
115 #endif
116 
117 	RHD(0,	"packet forwarded");
118 	RHD(1,	"  packet not forwardable");
119 	RHD(2,	"  redirect sent");
120 
121 	RHD(4,	"packet sent from this host");
122 	RHD(5,	"  fabricated ip header");
123 	RHD(6,	"  dropped output (no bufs)");
124 	RHD(7,	"  dropped output (no route)");
125 	RHD(8,	"  output datagram fragmented");
126 	RHD(9,	"  fragment created");
127 	RHD(10,	"  can't be fragmented");
128 
129 	RHD(12,	"violated scope rules");
130 }
131 
132 void
133 showip6(void)
134 {
135 #if 0
136 	u_quad_t m2m;
137 	int i;
138 
139 	m2m = 0;
140 	for (i = 0; i < 32; i++) {
141 		m2m += curstat[IP6_STAT_M2M + i];
142 	}
143 #endif
144 
145 	SHOW(IP6_STAT_TOTAL, 0, 0);
146 	SHOW(IP6_STAT_TOOSMALL, 1, 0);
147 	SHOW(IP6_STAT_TOOSHORT, 2, 0);
148 	SHOW(IP6_STAT_BADOPTIONS, 3, 0);
149 	SHOW(IP6_STAT_BADVERS, 4, 0);
150 	SHOW(IP6_STAT_EXTHDRTOOLONG, 5, 0);
151 	SHOW(IP6_STAT_DELIVERED, 6, 0);
152 	SHOW(IP6_STAT_NOTMEMBER, 7, 0);
153 	SHOW(IP6_STAT_TOOMANYHDR, 8, 0);
154 	SHOW(IP6_STAT_NOGIF, 9, 0);
155 
156 	SHOW(IP6_STAT_FRAGMENTS, 11, 0);
157 	SHOW(IP6_STAT_FRAGDROPPED, 12, 0);
158 	SHOW(IP6_STAT_FRAGTIMEOUT, 13, 0);
159 	SHOW(IP6_STAT_FRAGOVERFLOW, 14, 0);
160 	SHOW(IP6_STAT_REASSEMBLED, 15, 0);
161 
162 #if 0
163 	SHOW(IP6_STAT_M1, 17, 0);
164 	SHOW(IP6_STAT_MEXT1, 18, 0);
165 	SHOW(IP6_STAT_MEXT2M, 19, 0);
166 	mvwprintw(wnd, 20, 0, "%9llu", (unsigned long long)m2m);
167 #endif
168 
169 	SHOW(IP6_STAT_FORWARD, 0, 35);
170 	SHOW(IP6_STAT_CANTFORWARD, 1, 35);
171 	SHOW(IP6_STAT_REDIRECTSENT, 2, 35);
172 
173 	SHOW(IP6_STAT_LOCALOUT, 4, 35);
174 	SHOW(IP6_STAT_RAWOUT, 5, 35);
175 	SHOW(IP6_STAT_ODROPPED, 6, 35);
176 	SHOW(IP6_STAT_NOROUTE, 7, 35);
177 	SHOW(IP6_STAT_FRAGMENTED, 8, 35);
178 	SHOW(IP6_STAT_OFRAGMENTS, 9, 35);
179 	SHOW(IP6_STAT_CANTFRAG, 10, 35);
180 
181 	SHOW(IP6_STAT_BADSCOPE, 12, 35);
182 }
183 
184 int
185 initip6(void)
186 {
187 	int n;
188 
189 	if (! use_sysctl) {
190 		if (namelist[0].n_type == 0) {
191 			n = kvm_nlist(kd, namelist);
192 			if (n < 0) {
193 				nlisterr(namelist);
194 				return(0);
195 			} else if (n == sizeof(namelist) / sizeof(namelist[0]) - 1) {
196 				error("No namelist");
197 				return(0);
198 			}
199 		}
200 	}
201 	return 1;
202 }
203 
204 void
205 fetchip6(void)
206 {
207 	int i;
208 
209 	if (use_sysctl) {
210 		size_t size = sizeof(newstat);
211 
212 		if (sysctlbyname("net.inet6.ip6.stats", newstat, &size,
213 				 NULL, 0) == -1)
214 			return;
215 	} else {
216 		KREAD((void *)namelist[0].n_value, newstat, sizeof(newstat));
217 	}
218 
219 	for (i = 0; i < IP6_NSTATS; i++)
220 		xADJINETCTR(curstat, oldstat, newstat, i);
221 
222 	if (update == UPDATE_TIME)
223 		memcpy(oldstat, newstat, sizeof(oldstat));
224 }
225 
226 void
227 ip6_boot(char *args)
228 {
229 
230 	memset(oldstat, 0, sizeof(oldstat));
231 	update = UPDATE_BOOT;
232 }
233 
234 void
235 ip6_run(char *args)
236 {
237 
238 	if (update != UPDATE_RUN) {
239 		memcpy(oldstat, newstat, sizeof(oldstat));
240 		update = UPDATE_RUN;
241 	}
242 }
243 
244 void
245 ip6_time(char *args)
246 {
247 
248 	if (update != UPDATE_TIME) {
249 		memcpy(oldstat, newstat, sizeof(oldstat));
250 		update = UPDATE_TIME;
251 	}
252 }
253 
254 void
255 ip6_zero(char *args)
256 {
257 
258 	if (update == UPDATE_RUN)
259 		memcpy(oldstat, newstat, sizeof(oldstat));
260 }
261