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 * 4. 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 30#include <sys/cdefs.h> 31 32__FBSDID("$FreeBSD: stable/11/usr.bin/systat/ip6.c 368931 2021-01-05 20:02:55Z mr $"); 33 34#ifdef lint 35static const char sccsid[] = "@(#)mbufs.c 8.1 (Berkeley) 6/6/93"; 36#endif 37 38/* From: 39 "Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp" 40*/ 41 42#ifdef INET6 43#include <sys/param.h> 44#include <sys/types.h> 45#include <sys/socket.h> 46#include <sys/sysctl.h> 47 48#include <netinet/in.h> 49#include <netinet/in_systm.h> 50#include <netinet/ip.h> 51#include <netinet6/ip6_var.h> 52 53#include <inttypes.h> 54#include <stdlib.h> 55#include <string.h> 56#include <paths.h> 57 58#include "systat.h" 59#include "extern.h" 60#include "mode.h" 61 62static struct ip6stat curstat, initstat, oldstat; 63 64/*- 65--0 1 2 3 4 5 6 7 66--0123456789012345678901234567890123456789012345678901234567890123456789012345 6700 IPv6 Input IPv6 Output 68019999999 total packets received 999999999 total packets sent 69029999999 - too short for header 999999999 - generated locally 70039999999 - too short for data 999999999 - output drops 71049999999 - with invalid version 999999999 output fragments generated 72059999999 total fragments received 999999999 - fragmentation failed 73069999999 - fragments dropped 999999999 destinations unreachable 74079999999 - fragments timed out 999999999 packets output via raw IP 75089999999 - fragments overflown 76099999999 - packets reassembled ok Input next-header histogram 77109999999 packets forwarded 999999999 - destination options 78119999999 - unreachable dests 999999999 - hop-by-hop options 79129999999 - redirects generated 999999999 - IPv4 80139999999 option errors 999999999 - TCP 81149999999 unwanted multicasts 999999999 - UDP 82159999999 delivered to upper layer 999999999 - IPv6 83169999999 bad scope packets 999999999 - routing header 84179999999 address selection failed 999999999 - fragmentation header 8518 999999999 - ICMP6 8619 999999999 - none 87--0123456789012345678901234567890123456789012345678901234567890123456789012345 88--0 1 2 3 4 5 6 7 89*/ 90 91WINDOW * 92openip6(void) 93{ 94 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 95} 96 97void 98closeip6(WINDOW *w) 99{ 100 if (w == NULL) 101 return; 102 wclear(w); 103 wrefresh(w); 104 delwin(w); 105} 106 107void 108labelip6(void) 109{ 110 wmove(wnd, 0, 0); wclrtoeol(wnd); 111#define L(row, str) mvwprintw(wnd, row, 10, str) 112#define R(row, str) mvwprintw(wnd, row, 45, str); 113 L(0, "IPv6 Input"); R(0, "IPv6 Output"); 114 L(1, "total packets received"); R(1, "total packets sent"); 115 L(2, "- too short for header"); R(2, "- generated locally"); 116 L(3, "- too short for data"); R(3, "- output drops"); 117 L(4, "- with invalid version"); R(4, "output fragments generated"); 118 L(5, "total fragments received"); R(5, "- fragmentation failed"); 119 L(6, "- fragments dropped"); R(6, "destinations unreachable"); 120 L(7, "- fragments timed out"); R(7, "packets output via raw IP"); 121 L(8, "- fragments overflown"); 122 L(9, "- packets reassembled ok"); R(9, "Input next-header histogram"); 123 L(10, "packets forwarded"); R(10, " - destination options"); 124 L(11, "- unreachable dests"); R(11, " - hop-by-hop options"); 125 L(12, "- redirects generated"); R(12, " - IPv4"); 126 L(13, "option errors"); R(13, " - TCP"); 127 L(14, "unwanted multicasts"); R(14, " - UDP"); 128 L(15, "delivered to upper layer"); R(15, " - IPv6"); 129 L(16, "bad scope packets"); R(16, " - routing header"); 130 L(17, "address selection failed"); R(17, " - fragmentation header"); 131 R(18, " - ICMP6"); 132 R(19, " - none"); 133#undef L 134#undef R 135} 136 137static void 138domode(struct ip6stat *ret) 139{ 140 const struct ip6stat *sub; 141 int divisor = 1, i; 142 143 switch(currentmode) { 144 case display_RATE: 145 sub = &oldstat; 146 divisor = (delay > 1000000) ? delay / 1000000 : 1; 147 break; 148 case display_DELTA: 149 sub = &oldstat; 150 break; 151 case display_SINCE: 152 sub = &initstat; 153 break; 154 default: 155 *ret = curstat; 156 return; 157 } 158#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor 159 DO(ip6s_total); 160 DO(ip6s_tooshort); 161 DO(ip6s_toosmall); 162 DO(ip6s_fragments); 163 DO(ip6s_fragdropped); 164 DO(ip6s_fragtimeout); 165 DO(ip6s_fragoverflow); 166 DO(ip6s_forward); 167 DO(ip6s_cantforward); 168 DO(ip6s_redirectsent); 169 DO(ip6s_delivered); 170 DO(ip6s_localout); 171 DO(ip6s_odropped); 172 DO(ip6s_reassembled); 173 DO(ip6s_fragmented); 174 DO(ip6s_ofragments); 175 DO(ip6s_cantfrag); 176 DO(ip6s_badoptions); 177 DO(ip6s_noroute); 178 DO(ip6s_badvers); 179 DO(ip6s_rawout); 180 DO(ip6s_notmember); 181 for (i = 0; i < 256; i++) 182 DO(ip6s_nxthist[i]); 183 DO(ip6s_badscope); 184 DO(ip6s_sources_none); 185#undef DO 186} 187 188void 189showip6(void) 190{ 191 struct ip6stat stats; 192 uint64_t totalout; 193 194 domode(&stats); 195 totalout = stats.ip6s_forward + stats.ip6s_localout; 196 197#define DO(stat, row, col) \ 198 mvwprintw(wnd, row, col, "%9"PRIu64, stats.stat) 199 200 DO(ip6s_total, 1, 0); 201 mvwprintw(wnd, 1, 35, "%9"PRIu64, totalout); 202 DO(ip6s_tooshort, 2, 0); 203 DO(ip6s_localout, 2, 35); 204 DO(ip6s_toosmall, 3, 0); 205 DO(ip6s_odropped, 3, 35); 206 DO(ip6s_badvers, 4, 0); 207 DO(ip6s_ofragments, 4, 35); 208 DO(ip6s_fragments, 5, 0); 209 DO(ip6s_cantfrag, 5, 35); 210 DO(ip6s_fragdropped, 6, 0); 211 DO(ip6s_noroute, 6, 35); 212 DO(ip6s_fragtimeout, 7, 0); 213 DO(ip6s_rawout, 7, 35); 214 DO(ip6s_fragoverflow, 8, 0); 215 DO(ip6s_reassembled, 9, 0); 216 DO(ip6s_forward, 10, 0); 217 DO(ip6s_nxthist[IPPROTO_DSTOPTS], 10, 35); 218 DO(ip6s_cantforward, 11, 0); 219 DO(ip6s_nxthist[IPPROTO_HOPOPTS], 11, 35); 220 DO(ip6s_redirectsent, 12, 0); 221 DO(ip6s_nxthist[IPPROTO_IPV4], 12, 35); 222 DO(ip6s_badoptions, 13, 0); 223 DO(ip6s_nxthist[IPPROTO_TCP], 13, 35); 224 DO(ip6s_notmember, 14, 0); 225 DO(ip6s_nxthist[IPPROTO_UDP], 14, 35); 226 DO(ip6s_delivered, 15, 0); 227 DO(ip6s_nxthist[IPPROTO_IPV6], 15, 35); 228 DO(ip6s_badscope, 16, 0); 229 DO(ip6s_nxthist[IPPROTO_ROUTING], 16, 35); 230 DO(ip6s_sources_none, 17, 0); 231 DO(ip6s_nxthist[IPPROTO_FRAGMENT], 17, 35); 232 DO(ip6s_nxthist[IPPROTO_ICMPV6], 18, 35); 233 DO(ip6s_nxthist[IPPROTO_NONE], 19, 35); 234#undef DO 235} 236 237int 238initip6(void) 239{ 240 size_t len; 241 int name[4]; 242 243 name[0] = CTL_NET; 244 name[1] = PF_INET6; 245 name[2] = IPPROTO_IPV6; 246 name[3] = IPV6CTL_STATS; 247 248 len = 0; 249 if (sysctl(name, 4, 0, &len, 0, 0) < 0) { 250 error("sysctl getting ip6stat size failed"); 251 return 0; 252 } 253 if (len > sizeof curstat) { 254 error("ip6stat structure has grown--recompile systat!"); 255 return 0; 256 } 257 if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) { 258 error("sysctl getting ip6stat failed"); 259 return 0; 260 } 261 oldstat = initstat; 262 return 1; 263} 264 265void 266resetip6(void) 267{ 268 size_t len; 269 int name[4]; 270 271 name[0] = CTL_NET; 272 name[1] = PF_INET6; 273 name[2] = IPPROTO_IPV6; 274 name[3] = IPV6CTL_STATS; 275 276 len = sizeof initstat; 277 if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) { 278 error("sysctl getting ipstat failed"); 279 } 280 281 oldstat = initstat; 282} 283 284void 285fetchip6(void) 286{ 287 int name[4]; 288 size_t len; 289 290 oldstat = curstat; 291 name[0] = CTL_NET; 292 name[1] = PF_INET6; 293 name[2] = IPPROTO_IPV6; 294 name[3] = IPV6CTL_STATS; 295 len = sizeof curstat; 296 297 if (sysctl(name, 4, &curstat, &len, 0, 0) < 0) 298 return; 299} 300 301#endif 302