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/ip.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#include <sys/param.h> 43#include <sys/types.h> 44#include <sys/socket.h> 45#include <sys/sysctl.h> 46 47#include <netinet/in.h> 48#include <netinet/in_systm.h> 49#include <netinet/ip.h> 50#include <netinet/ip_var.h> 51#include <netinet/udp.h> 52#include <netinet/udp_var.h> 53 54#include <inttypes.h> 55#include <stdlib.h> 56#include <string.h> 57#include <paths.h> 58 59#include "systat.h" 60#include "extern.h" 61#include "mode.h" 62 63struct stat { 64 struct ipstat i; 65 struct udpstat u; 66}; 67 68static struct stat curstat, initstat, oldstat; 69 70/*- 71--0 1 2 3 4 5 6 7 72--0123456789012345678901234567890123456789012345678901234567890123456789012345 7300 IP Input IP Output 7401999999999 total packets received 999999999 total packets sent 7502999999999 - with bad checksums 999999999 - generated locally 7603999999999 - too short for header 999999999 - output drops 7704999999999 - too short for data 999999999 output fragments generated 7805999999999 - with invalid hlen 999999999 - fragmentation failed 7906999999999 - with invalid length 999999999 destinations unreachable 8007999999999 - with invalid version 999999999 packets output via raw IP 8108999999999 - jumbograms 8209999999999 total fragments received UDP Statistics 8310999999999 - fragments dropped 999999999 total input packets 8411999999999 - fragments timed out 999999999 - too short for header 8512999999999 - packets reassembled ok 999999999 - invalid checksum 8613999999999 packets forwarded 999999999 - no checksum 8714999999999 - unreachable dests 999999999 - invalid length 8815999999999 - redirects generated 999999999 - no socket for dest port 8916999999999 option errors 999999999 - no socket for broadcast 9017999999999 unwanted multicasts 999999999 - socket buffer full 9118999999999 delivered to upper layer 999999999 total output packets 92--0123456789012345678901234567890123456789012345678901234567890123456789012345 93--0 1 2 3 4 5 6 7 94*/ 95 96WINDOW * 97openip(void) 98{ 99 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 100} 101 102void 103closeip(WINDOW *w) 104{ 105 if (w == NULL) 106 return; 107 wclear(w); 108 wrefresh(w); 109 delwin(w); 110} 111 112void 113labelip(void) 114{ 115 wmove(wnd, 0, 0); wclrtoeol(wnd); 116#define L(row, str) mvwprintw(wnd, row, 10, str) 117#define R(row, str) mvwprintw(wnd, row, 45, str); 118 L(0, "IP Input"); R(0, "IP Output"); 119 L(1, "total packets received"); R(1, "total packets sent"); 120 L(2, "- with bad checksums"); R(2, "- generated locally"); 121 L(3, "- too short for header"); R(3, "- output drops"); 122 L(4, "- too short for data"); R(4, "output fragments generated"); 123 L(5, "- with invalid hlen"); R(5, "- fragmentation failed"); 124 L(6, "- with invalid length"); R(6, "destinations unreachable"); 125 L(7, "- with invalid version"); R(7, "packets output via raw IP"); 126 L(8, "- jumbograms"); 127 L(9, "total fragments received"); R(9, "UDP Statistics"); 128 L(10, "- fragments dropped"); R(10, "total input packets"); 129 L(11, "- fragments timed out"); R(11, "- too short for header"); 130 L(12, "- packets reassembled ok"); R(12, "- invalid checksum"); 131 L(13, "packets forwarded"); R(13, "- no checksum"); 132 L(14, "- unreachable dests"); R(14, "- invalid length"); 133 L(15, "- redirects generated"); R(15, "- no socket for dest port"); 134 L(16, "option errors"); R(16, "- no socket for broadcast"); 135 L(17, "unwanted multicasts"); R(17, "- socket buffer full"); 136 L(18, "delivered to upper layer"); R(18, "total output packets"); 137#undef L 138#undef R 139} 140 141static void 142domode(struct stat *ret) 143{ 144 const struct stat *sub; 145 int divisor = 1; 146 147 switch(currentmode) { 148 case display_RATE: 149 sub = &oldstat; 150 divisor = (delay > 1000000) ? delay / 1000000 : 1; 151 break; 152 case display_DELTA: 153 sub = &oldstat; 154 break; 155 case display_SINCE: 156 sub = &initstat; 157 break; 158 default: 159 *ret = curstat; 160 return; 161 } 162#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor 163 DO(i.ips_total); 164 DO(i.ips_badsum); 165 DO(i.ips_tooshort); 166 DO(i.ips_toosmall); 167 DO(i.ips_badhlen); 168 DO(i.ips_badlen); 169 DO(i.ips_fragments); 170 DO(i.ips_fragdropped); 171 DO(i.ips_fragtimeout); 172 DO(i.ips_forward); 173 DO(i.ips_cantforward); 174 DO(i.ips_redirectsent); 175 DO(i.ips_noproto); 176 DO(i.ips_delivered); 177 DO(i.ips_localout); 178 DO(i.ips_odropped); 179 DO(i.ips_reassembled); 180 DO(i.ips_fragmented); 181 DO(i.ips_ofragments); 182 DO(i.ips_cantfrag); 183 DO(i.ips_badoptions); 184 DO(i.ips_noroute); 185 DO(i.ips_badvers); 186 DO(i.ips_rawout); 187 DO(i.ips_toolong); 188 DO(i.ips_notmember); 189 DO(u.udps_ipackets); 190 DO(u.udps_hdrops); 191 DO(u.udps_badsum); 192 DO(u.udps_nosum); 193 DO(u.udps_badlen); 194 DO(u.udps_noport); 195 DO(u.udps_noportbcast); 196 DO(u.udps_fullsock); 197 DO(u.udps_opackets); 198#undef DO 199} 200 201void 202showip(void) 203{ 204 struct stat stats; 205 uint64_t totalout; 206 207 domode(&stats); 208 totalout = stats.i.ips_forward + stats.i.ips_localout; 209 210#define DO(stat, row, col) \ 211 mvwprintw(wnd, row, col, "%9"PRIu64, stats.stat) 212 213 DO(i.ips_total, 1, 0); 214 mvwprintw(wnd, 1, 35, "%9"PRIu64, totalout); 215 DO(i.ips_badsum, 2, 0); 216 DO(i.ips_localout, 2, 35); 217 DO(i.ips_tooshort, 3, 0); 218 DO(i.ips_odropped, 3, 35); 219 DO(i.ips_toosmall, 4, 0); 220 DO(i.ips_ofragments, 4, 35); 221 DO(i.ips_badhlen, 5, 0); 222 DO(i.ips_cantfrag, 5, 35); 223 DO(i.ips_badlen, 6, 0); 224 DO(i.ips_noroute, 6, 35); 225 DO(i.ips_badvers, 7, 0); 226 DO(i.ips_rawout, 7, 35); 227 DO(i.ips_toolong, 8, 0); 228 DO(i.ips_fragments, 9, 0); 229 DO(i.ips_fragdropped, 10, 0); 230 DO(u.udps_ipackets, 10, 35); 231 DO(i.ips_fragtimeout, 11, 0); 232 DO(u.udps_hdrops, 11, 35); 233 DO(i.ips_reassembled, 12, 0); 234 DO(u.udps_badsum, 12, 35); 235 DO(i.ips_forward, 13, 0); 236 DO(u.udps_nosum, 13, 35); 237 DO(i.ips_cantforward, 14, 0); 238 DO(u.udps_badlen, 14, 35); 239 DO(i.ips_redirectsent, 15, 0); 240 DO(u.udps_noport, 15, 35); 241 DO(i.ips_badoptions, 16, 0); 242 DO(u.udps_noportbcast, 16, 35); 243 DO(i.ips_notmember, 17, 0); 244 DO(u.udps_fullsock, 17, 35); 245 DO(i.ips_delivered, 18, 0); 246 DO(u.udps_opackets, 18, 35); 247#undef DO 248} 249 250int 251initip(void) 252{ 253 size_t len; 254 int name[4]; 255 256 name[0] = CTL_NET; 257 name[1] = PF_INET; 258 name[2] = IPPROTO_IP; 259 name[3] = IPCTL_STATS; 260 261 len = 0; 262 if (sysctl(name, 4, 0, &len, 0, 0) < 0) { 263 error("sysctl getting ipstat size failed"); 264 return 0; 265 } 266 if (len > sizeof curstat.i) { 267 error("ipstat structure has grown--recompile systat!"); 268 return 0; 269 } 270 if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) { 271 error("sysctl getting ipstat failed"); 272 return 0; 273 } 274 name[2] = IPPROTO_UDP; 275 name[3] = UDPCTL_STATS; 276 277 len = 0; 278 if (sysctl(name, 4, 0, &len, 0, 0) < 0) { 279 error("sysctl getting udpstat size failed"); 280 return 0; 281 } 282 if (len > sizeof curstat.u) { 283 error("ipstat structure has grown--recompile systat!"); 284 return 0; 285 } 286 if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) { 287 error("sysctl getting udpstat failed"); 288 return 0; 289 } 290 oldstat = initstat; 291 return 1; 292} 293 294void 295resetip(void) 296{ 297 size_t len; 298 int name[4]; 299 300 name[0] = CTL_NET; 301 name[1] = PF_INET; 302 name[2] = IPPROTO_IP; 303 name[3] = IPCTL_STATS; 304 305 len = sizeof initstat.i; 306 if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) { 307 error("sysctl getting ipstat failed"); 308 } 309 name[2] = IPPROTO_UDP; 310 name[3] = UDPCTL_STATS; 311 312 len = sizeof initstat.u; 313 if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) { 314 error("sysctl getting udpstat failed"); 315 } 316 oldstat = initstat; 317} 318 319void 320fetchip(void) 321{ 322 int name[4]; 323 size_t len; 324 325 oldstat = curstat; 326 name[0] = CTL_NET; 327 name[1] = PF_INET; 328 name[2] = IPPROTO_IP; 329 name[3] = IPCTL_STATS; 330 len = sizeof curstat.i; 331 332 if (sysctl(name, 4, &curstat.i, &len, 0, 0) < 0) 333 return; 334 name[2] = IPPROTO_UDP; 335 name[3] = UDPCTL_STATS; 336 len = sizeof curstat.u; 337 338 if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0) 339 return; 340} 341