xref: /freebsd/usr.bin/systat/ip.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
12938fb78SGarrett Wollman /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
42938fb78SGarrett Wollman  * Copyright (c) 1980, 1992, 1993
52938fb78SGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
62938fb78SGarrett Wollman  *
72938fb78SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
82938fb78SGarrett Wollman  * modification, are permitted provided that the following conditions
92938fb78SGarrett Wollman  * are met:
102938fb78SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
112938fb78SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
122938fb78SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
132938fb78SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
142938fb78SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
162938fb78SGarrett Wollman  *    may be used to endorse or promote products derived from this software
172938fb78SGarrett Wollman  *    without specific prior written permission.
182938fb78SGarrett Wollman  *
192938fb78SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202938fb78SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212938fb78SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222938fb78SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232938fb78SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242938fb78SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252938fb78SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262938fb78SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272938fb78SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282938fb78SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292938fb78SGarrett Wollman  * SUCH DAMAGE.
302938fb78SGarrett Wollman  */
312938fb78SGarrett Wollman 
329ff712b0SMark Murray 
339ff712b0SMark Murray 
342938fb78SGarrett Wollman /* From:
359ff712b0SMark Murray 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
362938fb78SGarrett Wollman */
372938fb78SGarrett Wollman 
382938fb78SGarrett Wollman #include <sys/param.h>
392938fb78SGarrett Wollman #include <sys/types.h>
402938fb78SGarrett Wollman #include <sys/socket.h>
412938fb78SGarrett Wollman #include <sys/sysctl.h>
422938fb78SGarrett Wollman 
432938fb78SGarrett Wollman #include <netinet/in.h>
442938fb78SGarrett Wollman #include <netinet/in_systm.h>
452938fb78SGarrett Wollman #include <netinet/ip.h>
462938fb78SGarrett Wollman #include <netinet/ip_var.h>
472938fb78SGarrett Wollman #include <netinet/udp.h>
482938fb78SGarrett Wollman #include <netinet/udp_var.h>
492938fb78SGarrett Wollman 
50*970bdbf5SMichael Tuexen #include <inttypes.h>
51821df508SXin LI #include <stdlib.h>
52821df508SXin LI #include <string.h>
53821df508SXin LI #include <paths.h>
54821df508SXin LI 
552938fb78SGarrett Wollman #include "systat.h"
562938fb78SGarrett Wollman #include "extern.h"
572938fb78SGarrett Wollman #include "mode.h"
582938fb78SGarrett Wollman 
592938fb78SGarrett Wollman struct stat {
602938fb78SGarrett Wollman 	struct ipstat i;
612938fb78SGarrett Wollman 	struct udpstat u;
622938fb78SGarrett Wollman };
632938fb78SGarrett Wollman 
642938fb78SGarrett Wollman static struct stat curstat, initstat, oldstat;
652938fb78SGarrett Wollman 
662938fb78SGarrett Wollman /*-
672938fb78SGarrett Wollman --0         1         2         3         4         5         6         7
682938fb78SGarrett Wollman --0123456789012345678901234567890123456789012345678901234567890123456789012345
698aa22952SBruce Evans 00          IP Input                           IP Output
708aa22952SBruce Evans 01999999999 total packets received   999999999 total packets sent
718aa22952SBruce Evans 02999999999 - with bad checksums     999999999 - generated locally
728aa22952SBruce Evans 03999999999 - too short for header   999999999 - output drops
738aa22952SBruce Evans 04999999999 - too short for data     999999999 output fragments generated
748aa22952SBruce Evans 05999999999 - with invalid hlen      999999999 - fragmentation failed
758aa22952SBruce Evans 06999999999 - with invalid length    999999999 destinations unreachable
768aa22952SBruce Evans 07999999999 - with invalid version   999999999 packets output via raw IP
778aa22952SBruce Evans 08999999999 - jumbograms
788aa22952SBruce Evans 09999999999 total fragments received           UDP Statistics
798aa22952SBruce Evans 10999999999 - fragments dropped      999999999 total input packets
808aa22952SBruce Evans 11999999999 - fragments timed out    999999999 - too short for header
818aa22952SBruce Evans 12999999999 - packets reassembled ok 999999999 - invalid checksum
828aa22952SBruce Evans 13999999999 packets forwarded        999999999 - no checksum
838aa22952SBruce Evans 14999999999 - unreachable dests      999999999 - invalid length
848aa22952SBruce Evans 15999999999 - redirects generated    999999999 - no socket for dest port
858aa22952SBruce Evans 16999999999 option errors            999999999 - no socket for broadcast
868aa22952SBruce Evans 17999999999 unwanted multicasts      999999999 - socket buffer full
878aa22952SBruce Evans 18999999999 delivered to upper layer 999999999 total output packets
882938fb78SGarrett Wollman --0123456789012345678901234567890123456789012345678901234567890123456789012345
892938fb78SGarrett Wollman --0         1         2         3         4         5         6         7
902938fb78SGarrett Wollman */
912938fb78SGarrett Wollman 
922938fb78SGarrett Wollman WINDOW *
openip(void)932938fb78SGarrett Wollman openip(void)
942938fb78SGarrett Wollman {
958aa22952SBruce Evans 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
962938fb78SGarrett Wollman }
972938fb78SGarrett Wollman 
982938fb78SGarrett Wollman void
closeip(WINDOW * w)9993b9f504SXin LI closeip(WINDOW *w)
1002938fb78SGarrett Wollman {
1012938fb78SGarrett Wollman 	if (w == NULL)
1022938fb78SGarrett Wollman 		return;
1032938fb78SGarrett Wollman 	wclear(w);
1042938fb78SGarrett Wollman 	wrefresh(w);
1052938fb78SGarrett Wollman 	delwin(w);
1062938fb78SGarrett Wollman }
1072938fb78SGarrett Wollman 
1082938fb78SGarrett Wollman void
labelip(void)1092938fb78SGarrett Wollman labelip(void)
1102938fb78SGarrett Wollman {
1112938fb78SGarrett Wollman 	wmove(wnd, 0, 0); wclrtoeol(wnd);
1122938fb78SGarrett Wollman #define L(row, str) mvwprintw(wnd, row, 10, str)
1132938fb78SGarrett Wollman #define R(row, str) mvwprintw(wnd, row, 45, str);
1148aa22952SBruce Evans 	L(0, "IP Input");		R(0, "IP Output");
1158aa22952SBruce Evans 	L(1, "total packets received");	R(1, "total packets sent");
1168aa22952SBruce Evans 	L(2, "- with bad checksums");	R(2, "- generated locally");
1178aa22952SBruce Evans 	L(3, "- too short for header");	R(3, "- output drops");
1188aa22952SBruce Evans 	L(4, "- too short for data");	R(4, "output fragments generated");
1198aa22952SBruce Evans 	L(5, "- with invalid hlen");	R(5, "- fragmentation failed");
1208aa22952SBruce Evans 	L(6, "- with invalid length");	R(6, "destinations unreachable");
1218aa22952SBruce Evans 	L(7, "- with invalid version");	R(7, "packets output via raw IP");
1228aa22952SBruce Evans 	L(8, "- jumbograms");
1238aa22952SBruce Evans 	L(9, "total fragments received");	R(9, "UDP Statistics");
1248aa22952SBruce Evans 	L(10, "- fragments dropped");	R(10, "total input packets");
1258aa22952SBruce Evans 	L(11, "- fragments timed out");	R(11, "- too short for header");
1268aa22952SBruce Evans 	L(12, "- packets reassembled ok");	R(12, "- invalid checksum");
1278aa22952SBruce Evans 	L(13, "packets forwarded");	R(13, "- no checksum");
1288aa22952SBruce Evans 	L(14, "- unreachable dests");	R(14, "- invalid length");
1298aa22952SBruce Evans 	L(15, "- redirects generated");	R(15, "- no socket for dest port");
1308aa22952SBruce Evans 	L(16, "option errors");		R(16, "- no socket for broadcast");
1318aa22952SBruce Evans 	L(17, "unwanted multicasts");	R(17, "- socket buffer full");
1328aa22952SBruce Evans 	L(18, "delivered to upper layer");	R(18, "total output packets");
1332938fb78SGarrett Wollman #undef L
1342938fb78SGarrett Wollman #undef R
1352938fb78SGarrett Wollman }
1362938fb78SGarrett Wollman 
1372938fb78SGarrett Wollman static void
domode(struct stat * ret)1382938fb78SGarrett Wollman domode(struct stat *ret)
1392938fb78SGarrett Wollman {
1402938fb78SGarrett Wollman 	const struct stat *sub;
141d22889b8SDavid E. O'Brien 	int divisor = 1;
1422938fb78SGarrett Wollman 
1432938fb78SGarrett Wollman 	switch(currentmode) {
1442938fb78SGarrett Wollman 	case display_RATE:
1452938fb78SGarrett Wollman 		sub = &oldstat;
1468b3daf89SAlexander V. Chernikov 		divisor = (delay > 1000000) ? delay / 1000000 : 1;
1472938fb78SGarrett Wollman 		break;
1482938fb78SGarrett Wollman 	case display_DELTA:
1492938fb78SGarrett Wollman 		sub = &oldstat;
1502938fb78SGarrett Wollman 		break;
1512938fb78SGarrett Wollman 	case display_SINCE:
1522938fb78SGarrett Wollman 		sub = &initstat;
1532938fb78SGarrett Wollman 		break;
1542938fb78SGarrett Wollman 	default:
1552938fb78SGarrett Wollman 		*ret = curstat;
1562938fb78SGarrett Wollman 		return;
1572938fb78SGarrett Wollman 	}
1582938fb78SGarrett Wollman #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
1592938fb78SGarrett Wollman 	DO(i.ips_total);
1602938fb78SGarrett Wollman 	DO(i.ips_badsum);
1612938fb78SGarrett Wollman 	DO(i.ips_tooshort);
1622938fb78SGarrett Wollman 	DO(i.ips_toosmall);
1632938fb78SGarrett Wollman 	DO(i.ips_badhlen);
1642938fb78SGarrett Wollman 	DO(i.ips_badlen);
1652938fb78SGarrett Wollman 	DO(i.ips_fragments);
1662938fb78SGarrett Wollman 	DO(i.ips_fragdropped);
1672938fb78SGarrett Wollman 	DO(i.ips_fragtimeout);
1682938fb78SGarrett Wollman 	DO(i.ips_forward);
1692938fb78SGarrett Wollman 	DO(i.ips_cantforward);
1702938fb78SGarrett Wollman 	DO(i.ips_redirectsent);
1712938fb78SGarrett Wollman 	DO(i.ips_noproto);
1722938fb78SGarrett Wollman 	DO(i.ips_delivered);
1732938fb78SGarrett Wollman 	DO(i.ips_localout);
1742938fb78SGarrett Wollman 	DO(i.ips_odropped);
1752938fb78SGarrett Wollman 	DO(i.ips_reassembled);
1762938fb78SGarrett Wollman 	DO(i.ips_fragmented);
1772938fb78SGarrett Wollman 	DO(i.ips_ofragments);
1782938fb78SGarrett Wollman 	DO(i.ips_cantfrag);
1792938fb78SGarrett Wollman 	DO(i.ips_badoptions);
1802938fb78SGarrett Wollman 	DO(i.ips_noroute);
1812938fb78SGarrett Wollman 	DO(i.ips_badvers);
1822938fb78SGarrett Wollman 	DO(i.ips_rawout);
1832938fb78SGarrett Wollman 	DO(i.ips_toolong);
1842938fb78SGarrett Wollman 	DO(i.ips_notmember);
1852938fb78SGarrett Wollman 	DO(u.udps_ipackets);
1862938fb78SGarrett Wollman 	DO(u.udps_hdrops);
1872938fb78SGarrett Wollman 	DO(u.udps_badsum);
188fb9aaba0SRuslan Ermilov 	DO(u.udps_nosum);
1892938fb78SGarrett Wollman 	DO(u.udps_badlen);
1902938fb78SGarrett Wollman 	DO(u.udps_noport);
1912938fb78SGarrett Wollman 	DO(u.udps_noportbcast);
1922938fb78SGarrett Wollman 	DO(u.udps_fullsock);
1932938fb78SGarrett Wollman 	DO(u.udps_opackets);
1942938fb78SGarrett Wollman #undef DO
1952938fb78SGarrett Wollman }
1962938fb78SGarrett Wollman 
1972938fb78SGarrett Wollman void
showip(void)1982938fb78SGarrett Wollman showip(void)
1992938fb78SGarrett Wollman {
2002938fb78SGarrett Wollman 	struct stat stats;
201*970bdbf5SMichael Tuexen 	uint64_t totalout;
2022938fb78SGarrett Wollman 
2032938fb78SGarrett Wollman 	domode(&stats);
2042938fb78SGarrett Wollman 	totalout = stats.i.ips_forward + stats.i.ips_localout;
2052938fb78SGarrett Wollman 
2062938fb78SGarrett Wollman #define DO(stat, row, col) \
207*970bdbf5SMichael Tuexen 	mvwprintw(wnd, row, col, "%9"PRIu64, stats.stat)
2082938fb78SGarrett Wollman 
2098aa22952SBruce Evans 	DO(i.ips_total, 1, 0);
210*970bdbf5SMichael Tuexen 	mvwprintw(wnd, 1, 35, "%9"PRIu64, totalout);
2118aa22952SBruce Evans 	DO(i.ips_badsum, 2, 0);
2128aa22952SBruce Evans 	DO(i.ips_localout, 2, 35);
2138aa22952SBruce Evans 	DO(i.ips_tooshort, 3, 0);
2148aa22952SBruce Evans 	DO(i.ips_odropped, 3, 35);
2158aa22952SBruce Evans 	DO(i.ips_toosmall, 4, 0);
2168aa22952SBruce Evans 	DO(i.ips_ofragments, 4, 35);
2178aa22952SBruce Evans 	DO(i.ips_badhlen, 5, 0);
2188aa22952SBruce Evans 	DO(i.ips_cantfrag, 5, 35);
2198aa22952SBruce Evans 	DO(i.ips_badlen, 6, 0);
2208aa22952SBruce Evans 	DO(i.ips_noroute, 6, 35);
2218aa22952SBruce Evans 	DO(i.ips_badvers, 7, 0);
2228aa22952SBruce Evans 	DO(i.ips_rawout, 7, 35);
2238aa22952SBruce Evans 	DO(i.ips_toolong, 8, 0);
2248aa22952SBruce Evans 	DO(i.ips_fragments, 9, 0);
2258aa22952SBruce Evans 	DO(i.ips_fragdropped, 10, 0);
2268aa22952SBruce Evans 	DO(u.udps_ipackets, 10, 35);
2278aa22952SBruce Evans 	DO(i.ips_fragtimeout, 11, 0);
2288aa22952SBruce Evans 	DO(u.udps_hdrops, 11, 35);
2298aa22952SBruce Evans 	DO(i.ips_reassembled, 12, 0);
2308aa22952SBruce Evans 	DO(u.udps_badsum, 12, 35);
2318aa22952SBruce Evans 	DO(i.ips_forward, 13, 0);
2328aa22952SBruce Evans 	DO(u.udps_nosum, 13, 35);
2338aa22952SBruce Evans 	DO(i.ips_cantforward, 14, 0);
2348aa22952SBruce Evans 	DO(u.udps_badlen, 14, 35);
2358aa22952SBruce Evans 	DO(i.ips_redirectsent, 15, 0);
2368aa22952SBruce Evans 	DO(u.udps_noport, 15, 35);
2378aa22952SBruce Evans 	DO(i.ips_badoptions, 16, 0);
2388aa22952SBruce Evans 	DO(u.udps_noportbcast, 16, 35);
2398aa22952SBruce Evans 	DO(i.ips_notmember, 17, 0);
2408aa22952SBruce Evans 	DO(u.udps_fullsock, 17, 35);
2418aa22952SBruce Evans 	DO(i.ips_delivered, 18, 0);
2428aa22952SBruce Evans 	DO(u.udps_opackets, 18, 35);
2432938fb78SGarrett Wollman #undef DO
2442938fb78SGarrett Wollman }
2452938fb78SGarrett Wollman 
2462938fb78SGarrett Wollman int
initip(void)2472938fb78SGarrett Wollman initip(void)
2482938fb78SGarrett Wollman {
2492938fb78SGarrett Wollman 	size_t len;
2502938fb78SGarrett Wollman 	int name[4];
2512938fb78SGarrett Wollman 
2522938fb78SGarrett Wollman 	name[0] = CTL_NET;
2532938fb78SGarrett Wollman 	name[1] = PF_INET;
2542938fb78SGarrett Wollman 	name[2] = IPPROTO_IP;
2552938fb78SGarrett Wollman 	name[3] = IPCTL_STATS;
2562938fb78SGarrett Wollman 
2572938fb78SGarrett Wollman 	len = 0;
2582938fb78SGarrett Wollman 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
2592938fb78SGarrett Wollman 		error("sysctl getting ipstat size failed");
2602938fb78SGarrett Wollman 		return 0;
2612938fb78SGarrett Wollman 	}
2622938fb78SGarrett Wollman 	if (len > sizeof curstat.i) {
2632938fb78SGarrett Wollman 		error("ipstat structure has grown--recompile systat!");
2642938fb78SGarrett Wollman 		return 0;
2652938fb78SGarrett Wollman 	}
2662938fb78SGarrett Wollman 	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
2672938fb78SGarrett Wollman 		error("sysctl getting ipstat failed");
2682938fb78SGarrett Wollman 		return 0;
2692938fb78SGarrett Wollman 	}
2702938fb78SGarrett Wollman 	name[2] = IPPROTO_UDP;
2712938fb78SGarrett Wollman 	name[3] = UDPCTL_STATS;
2722938fb78SGarrett Wollman 
2732938fb78SGarrett Wollman 	len = 0;
2742938fb78SGarrett Wollman 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
2752938fb78SGarrett Wollman 		error("sysctl getting udpstat size failed");
2762938fb78SGarrett Wollman 		return 0;
2772938fb78SGarrett Wollman 	}
2782938fb78SGarrett Wollman 	if (len > sizeof curstat.u) {
2792938fb78SGarrett Wollman 		error("ipstat structure has grown--recompile systat!");
2802938fb78SGarrett Wollman 		return 0;
2812938fb78SGarrett Wollman 	}
2822938fb78SGarrett Wollman 	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
2832938fb78SGarrett Wollman 		error("sysctl getting udpstat failed");
2842938fb78SGarrett Wollman 		return 0;
2852938fb78SGarrett Wollman 	}
2862938fb78SGarrett Wollman 	oldstat = initstat;
2872938fb78SGarrett Wollman 	return 1;
2882938fb78SGarrett Wollman }
2892938fb78SGarrett Wollman 
2902938fb78SGarrett Wollman void
resetip(void)2912938fb78SGarrett Wollman resetip(void)
2922938fb78SGarrett Wollman {
2932938fb78SGarrett Wollman 	size_t len;
2942938fb78SGarrett Wollman 	int name[4];
2952938fb78SGarrett Wollman 
2962938fb78SGarrett Wollman 	name[0] = CTL_NET;
2972938fb78SGarrett Wollman 	name[1] = PF_INET;
2982938fb78SGarrett Wollman 	name[2] = IPPROTO_IP;
2992938fb78SGarrett Wollman 	name[3] = IPCTL_STATS;
3002938fb78SGarrett Wollman 
3012938fb78SGarrett Wollman 	len = sizeof initstat.i;
3022938fb78SGarrett Wollman 	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
3032938fb78SGarrett Wollman 		error("sysctl getting ipstat failed");
3042938fb78SGarrett Wollman 	}
3052938fb78SGarrett Wollman 	name[2] = IPPROTO_UDP;
3062938fb78SGarrett Wollman 	name[3] = UDPCTL_STATS;
3072938fb78SGarrett Wollman 
3082938fb78SGarrett Wollman 	len = sizeof initstat.u;
3092938fb78SGarrett Wollman 	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
3102938fb78SGarrett Wollman 		error("sysctl getting udpstat failed");
3112938fb78SGarrett Wollman 	}
3122938fb78SGarrett Wollman 	oldstat = initstat;
3132938fb78SGarrett Wollman }
3142938fb78SGarrett Wollman 
3152938fb78SGarrett Wollman void
fetchip(void)3162938fb78SGarrett Wollman fetchip(void)
3172938fb78SGarrett Wollman {
3182938fb78SGarrett Wollman 	int name[4];
3192938fb78SGarrett Wollman 	size_t len;
3202938fb78SGarrett Wollman 
3212938fb78SGarrett Wollman 	oldstat = curstat;
3222938fb78SGarrett Wollman 	name[0] = CTL_NET;
3232938fb78SGarrett Wollman 	name[1] = PF_INET;
3242938fb78SGarrett Wollman 	name[2] = IPPROTO_IP;
3252938fb78SGarrett Wollman 	name[3] = IPCTL_STATS;
3262938fb78SGarrett Wollman 	len = sizeof curstat.i;
3272938fb78SGarrett Wollman 
3282938fb78SGarrett Wollman 	if (sysctl(name, 4, &curstat.i, &len, 0, 0) < 0)
3292938fb78SGarrett Wollman 		return;
3302938fb78SGarrett Wollman 	name[2] = IPPROTO_UDP;
3312938fb78SGarrett Wollman 	name[3] = UDPCTL_STATS;
3322938fb78SGarrett Wollman 	len = sizeof curstat.u;
3332938fb78SGarrett Wollman 
3342938fb78SGarrett Wollman 	if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0)
3352938fb78SGarrett Wollman 		return;
3362938fb78SGarrett Wollman }
337