Merge branch 'vendor/LESS'
[dragonfly.git] / usr.bin / systat / ip6.c
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  * $DragonFly: src/usr.bin/systat/ip6.c,v 1.2 2008/10/16 01:52:33 swildner Exp $
36  */
37
38 #include <sys/cdefs.h>
39
40 #ifdef INET6
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
45
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet6/ip6_var.h>
50
51 #include <stdlib.h>
52 #include <string.h>
53 #include <paths.h>
54
55 #include "systat.h"
56 #include "extern.h"
57 #include "mode.h"
58
59 static struct ip6stat curstat, initstat, oldstat;
60
61 /*-
62 --0         1         2         3         4         5         6         7
63 --0123456789012345678901234567890123456789012345678901234567890123456789012345
64 00        IPv6 Input                         IPv6 Output
65 019999999 total packets received   999999999 total packets sent
66 029999999 - too short for header   999999999 - generated locally
67 039999999 - too short for data     999999999 - output drops
68 049999999 - with invalid version   999999999 output fragments generated
69 059999999 total fragments received 999999999 - fragmentation failed
70 069999999 - fragments dropped      999999999 destinations unreachable
71 079999999 - fragments timed out    999999999 packets output via raw IP
72 089999999 - fragments overflown
73 099999999 - packets reassembled ok           Input next-header histogram
74 109999999 packets forwarded        999999999  - destination options
75 119999999 - unreachable dests      999999999  - hop-by-hop options
76 129999999 - redirects generated    999999999  - IPv4
77 139999999 option errors            999999999  - TCP
78 149999999 unwanted multicasts      999999999  - UDP
79 159999999 delivered to upper layer 999999999  - IPv6
80 169999999 bad scope packets        999999999  - routing header
81 179999999 address selection failed 999999999  - fragmentation header
82 18                                 999999999  - ICMP6
83 19                                 999999999  - none
84 --0123456789012345678901234567890123456789012345678901234567890123456789012345
85 --0         1         2         3         4         5         6         7
86 */
87
88 WINDOW *
89 openip6(void)
90 {
91         return (subwin(stdscr, LINES-4-1, 0, 4, 0));
92 }
93
94 void
95 closeip6(WINDOW *w)
96 {
97         if (w == NULL)
98                 return;
99         wclear(w);
100         wrefresh(w);
101         delwin(w);
102 }
103
104 void
105 labelip6(void)
106 {
107         wmove(wnd, 0, 0); wclrtoeol(wnd);
108 #define L(row, str) mvwprintw(wnd, row, 10, str)
109 #define R(row, str) mvwprintw(wnd, row, 45, str);
110         L(1, "IPv6 Input");             R(1, "IPv6 Output");
111         L(2, "total packets received"); R(2, "total packets sent");
112         L(3, "- too short for header"); R(3, "- generated locally");
113         L(4, "- too short for data");   R(4, "- output drops");
114         L(5, "- with invalid version"); R(5, "output fragments generated");
115         L(6, "total fragments received"); R(6, "- fragmentation failed");
116         L(7, "- fragments dropped");    R(7, "destinations unreachable");
117         L(8, "- fragments timed out");  R(8, "packets output via raw IP");
118         L(9, "- fragments overflown");
119         L(10, "- packets reassembled ok"); R(10, "Input next-header histogram");
120         L(11, "packets forwarded");     R(11, " - destination options");
121         L(12, "- unreachable dests");   R(12, " - hop-by-hop options");
122         L(13, "- redirects generated"); R(13, " - IPv4");
123         L(14, "option errors");         R(14, " - TCP");
124         L(15, "unwanted multicasts");   R(15, " - UDP");
125         L(16, "delivered to upper layer"); R(16, " - IPv6");
126         L(17, "bad scope packets");     R(17, " - routing header");
127         L(18, "address selection failed"); R(18, " - fragmentation header");
128                                         R(19, " - ICMP6");
129                                         R(20, " - none");
130 #undef L
131 #undef R
132 }
133
134 static void
135 domode(struct ip6stat *ret)
136 {
137         const struct ip6stat *sub;
138         int divisor = 1, i;
139
140         switch(currentmode) {
141         case display_RATE:
142                 sub = &oldstat;
143                 divisor = naptime;
144                 break;
145         case display_DELTA:
146                 sub = &oldstat;
147                 break;
148         case display_SINCE:
149                 sub = &initstat;
150                 break;
151         default:
152                 *ret = curstat;
153                 return;
154         }
155 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
156         DO(ip6s_total);
157         DO(ip6s_tooshort);
158         DO(ip6s_toosmall);
159         DO(ip6s_fragments);
160         DO(ip6s_fragdropped);
161         DO(ip6s_fragtimeout);
162         DO(ip6s_fragoverflow);
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
185 void
186 showip6(void)
187 {
188         struct ip6stat stats;
189         u_long 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, "%9lu", stats.stat)
196
197         DO(ip6s_total, 2, 0);
198         mvwprintw(wnd, 2, 35, "%9lu", totalout);
199         DO(ip6s_tooshort, 3, 0);
200         DO(ip6s_localout, 3, 35);
201         DO(ip6s_toosmall, 4, 0);
202         DO(ip6s_odropped, 4, 35);
203         DO(ip6s_badvers, 5, 0);
204         DO(ip6s_ofragments, 5, 35);
205         DO(ip6s_fragments, 6, 0);
206         DO(ip6s_cantfrag, 6, 35);
207         DO(ip6s_fragdropped, 7, 0);
208         DO(ip6s_noroute, 7, 35);
209         DO(ip6s_fragtimeout, 8, 0);
210         DO(ip6s_rawout, 8, 35);
211         DO(ip6s_fragoverflow, 9, 0);
212         DO(ip6s_reassembled, 10, 0);
213         DO(ip6s_forward, 11, 0);
214         DO(ip6s_nxthist[IPPROTO_DSTOPTS], 11, 35);
215         DO(ip6s_cantforward, 12, 0);
216         DO(ip6s_nxthist[IPPROTO_HOPOPTS], 12, 35);
217         DO(ip6s_redirectsent, 13, 0);
218         DO(ip6s_nxthist[IPPROTO_IPV4], 13, 35);
219         DO(ip6s_badoptions, 14, 0);
220         DO(ip6s_nxthist[IPPROTO_TCP], 14, 35);
221         DO(ip6s_notmember, 15, 0);
222         DO(ip6s_nxthist[IPPROTO_UDP], 15, 35);
223         DO(ip6s_delivered, 16, 0);
224         DO(ip6s_nxthist[IPPROTO_IPV6], 16, 35);
225         DO(ip6s_badscope, 17, 0);
226         DO(ip6s_nxthist[IPPROTO_ROUTING], 17, 35);
227         DO(ip6s_sources_none, 18, 0);
228         DO(ip6s_nxthist[IPPROTO_FRAGMENT], 18, 35);
229         DO(ip6s_nxthist[IPPROTO_ICMPV6], 19, 35);
230         DO(ip6s_nxthist[IPPROTO_NONE], 20, 35);
231 #undef DO
232 }
233
234 int
235 initip6(void)
236 {
237         size_t len;
238         int name[4];
239
240         name[0] = CTL_NET;
241         name[1] = PF_INET6;
242         name[2] = IPPROTO_IPV6;
243         name[3] = IPV6CTL_STATS;
244
245         len = 0;
246         if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
247                 error("sysctl getting ip6stat size failed");
248                 return 0;
249         }
250         if (len > sizeof curstat) {
251                 error("ip6stat structure has grown--recompile systat!");
252                 return 0;
253         }
254         if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
255                 error("sysctl getting ip6stat failed");
256                 return 0;
257         }
258         oldstat = initstat;
259         return 1;
260 }
261
262 void
263 resetip6(void)
264 {
265         size_t len;
266         int name[4];
267
268         name[0] = CTL_NET;
269         name[1] = PF_INET6;
270         name[2] = IPPROTO_IPV6;
271         name[3] = IPV6CTL_STATS;
272
273         len = sizeof initstat;
274         if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
275                 error("sysctl getting ipstat failed");
276         }
277
278         oldstat = initstat;
279 }
280
281 void
282 fetchip6(void)
283 {
284         int name[4];
285         size_t len;
286
287         oldstat = curstat;
288         name[0] = CTL_NET;
289         name[1] = PF_INET6;
290         name[2] = IPPROTO_IPV6;
291         name[3] = IPV6CTL_STATS;
292         len = sizeof curstat;
293
294         if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
295                 return;
296 }
297
298 #endif