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