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