xref: /freebsd/usr.bin/systat/ip.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef lint
37 static const char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38 #endif
39 
40 /* From:
41 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
42 */
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/udp.h>
54 #include <netinet/udp_var.h>
55 
56 #include <inttypes.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <paths.h>
60 
61 #include "systat.h"
62 #include "extern.h"
63 #include "mode.h"
64 
65 struct stat {
66 	struct ipstat i;
67 	struct udpstat u;
68 };
69 
70 static struct stat curstat, initstat, oldstat;
71 
72 /*-
73 --0         1         2         3         4         5         6         7
74 --0123456789012345678901234567890123456789012345678901234567890123456789012345
75 00          IP Input                           IP Output
76 01999999999 total packets received   999999999 total packets sent
77 02999999999 - with bad checksums     999999999 - generated locally
78 03999999999 - too short for header   999999999 - output drops
79 04999999999 - too short for data     999999999 output fragments generated
80 05999999999 - with invalid hlen      999999999 - fragmentation failed
81 06999999999 - with invalid length    999999999 destinations unreachable
82 07999999999 - with invalid version   999999999 packets output via raw IP
83 08999999999 - jumbograms
84 09999999999 total fragments received           UDP Statistics
85 10999999999 - fragments dropped      999999999 total input packets
86 11999999999 - fragments timed out    999999999 - too short for header
87 12999999999 - packets reassembled ok 999999999 - invalid checksum
88 13999999999 packets forwarded        999999999 - no checksum
89 14999999999 - unreachable dests      999999999 - invalid length
90 15999999999 - redirects generated    999999999 - no socket for dest port
91 16999999999 option errors            999999999 - no socket for broadcast
92 17999999999 unwanted multicasts      999999999 - socket buffer full
93 18999999999 delivered to upper layer 999999999 total output packets
94 --0123456789012345678901234567890123456789012345678901234567890123456789012345
95 --0         1         2         3         4         5         6         7
96 */
97 
98 WINDOW *
99 openip(void)
100 {
101 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
102 }
103 
104 void
105 closeip(WINDOW *w)
106 {
107 	if (w == NULL)
108 		return;
109 	wclear(w);
110 	wrefresh(w);
111 	delwin(w);
112 }
113 
114 void
115 labelip(void)
116 {
117 	wmove(wnd, 0, 0); wclrtoeol(wnd);
118 #define L(row, str) mvwprintw(wnd, row, 10, str)
119 #define R(row, str) mvwprintw(wnd, row, 45, str);
120 	L(0, "IP Input");		R(0, "IP Output");
121 	L(1, "total packets received");	R(1, "total packets sent");
122 	L(2, "- with bad checksums");	R(2, "- generated locally");
123 	L(3, "- too short for header");	R(3, "- output drops");
124 	L(4, "- too short for data");	R(4, "output fragments generated");
125 	L(5, "- with invalid hlen");	R(5, "- fragmentation failed");
126 	L(6, "- with invalid length");	R(6, "destinations unreachable");
127 	L(7, "- with invalid version");	R(7, "packets output via raw IP");
128 	L(8, "- jumbograms");
129 	L(9, "total fragments received");	R(9, "UDP Statistics");
130 	L(10, "- fragments dropped");	R(10, "total input packets");
131 	L(11, "- fragments timed out");	R(11, "- too short for header");
132 	L(12, "- packets reassembled ok");	R(12, "- invalid checksum");
133 	L(13, "packets forwarded");	R(13, "- no checksum");
134 	L(14, "- unreachable dests");	R(14, "- invalid length");
135 	L(15, "- redirects generated");	R(15, "- no socket for dest port");
136 	L(16, "option errors");		R(16, "- no socket for broadcast");
137 	L(17, "unwanted multicasts");	R(17, "- socket buffer full");
138 	L(18, "delivered to upper layer");	R(18, "total output packets");
139 #undef L
140 #undef R
141 }
142 
143 static void
144 domode(struct stat *ret)
145 {
146 	const struct stat *sub;
147 	int divisor = 1;
148 
149 	switch(currentmode) {
150 	case display_RATE:
151 		sub = &oldstat;
152 		divisor = (delay > 1000000) ? delay / 1000000 : 1;
153 		break;
154 	case display_DELTA:
155 		sub = &oldstat;
156 		break;
157 	case display_SINCE:
158 		sub = &initstat;
159 		break;
160 	default:
161 		*ret = curstat;
162 		return;
163 	}
164 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
165 	DO(i.ips_total);
166 	DO(i.ips_badsum);
167 	DO(i.ips_tooshort);
168 	DO(i.ips_toosmall);
169 	DO(i.ips_badhlen);
170 	DO(i.ips_badlen);
171 	DO(i.ips_fragments);
172 	DO(i.ips_fragdropped);
173 	DO(i.ips_fragtimeout);
174 	DO(i.ips_forward);
175 	DO(i.ips_cantforward);
176 	DO(i.ips_redirectsent);
177 	DO(i.ips_noproto);
178 	DO(i.ips_delivered);
179 	DO(i.ips_localout);
180 	DO(i.ips_odropped);
181 	DO(i.ips_reassembled);
182 	DO(i.ips_fragmented);
183 	DO(i.ips_ofragments);
184 	DO(i.ips_cantfrag);
185 	DO(i.ips_badoptions);
186 	DO(i.ips_noroute);
187 	DO(i.ips_badvers);
188 	DO(i.ips_rawout);
189 	DO(i.ips_toolong);
190 	DO(i.ips_notmember);
191 	DO(u.udps_ipackets);
192 	DO(u.udps_hdrops);
193 	DO(u.udps_badsum);
194 	DO(u.udps_nosum);
195 	DO(u.udps_badlen);
196 	DO(u.udps_noport);
197 	DO(u.udps_noportbcast);
198 	DO(u.udps_fullsock);
199 	DO(u.udps_opackets);
200 #undef DO
201 }
202 
203 void
204 showip(void)
205 {
206 	struct stat stats;
207 	uint64_t totalout;
208 
209 	domode(&stats);
210 	totalout = stats.i.ips_forward + stats.i.ips_localout;
211 
212 #define DO(stat, row, col) \
213 	mvwprintw(wnd, row, col, "%9"PRIu64, stats.stat)
214 
215 	DO(i.ips_total, 1, 0);
216 	mvwprintw(wnd, 1, 35, "%9"PRIu64, totalout);
217 	DO(i.ips_badsum, 2, 0);
218 	DO(i.ips_localout, 2, 35);
219 	DO(i.ips_tooshort, 3, 0);
220 	DO(i.ips_odropped, 3, 35);
221 	DO(i.ips_toosmall, 4, 0);
222 	DO(i.ips_ofragments, 4, 35);
223 	DO(i.ips_badhlen, 5, 0);
224 	DO(i.ips_cantfrag, 5, 35);
225 	DO(i.ips_badlen, 6, 0);
226 	DO(i.ips_noroute, 6, 35);
227 	DO(i.ips_badvers, 7, 0);
228 	DO(i.ips_rawout, 7, 35);
229 	DO(i.ips_toolong, 8, 0);
230 	DO(i.ips_fragments, 9, 0);
231 	DO(i.ips_fragdropped, 10, 0);
232 	DO(u.udps_ipackets, 10, 35);
233 	DO(i.ips_fragtimeout, 11, 0);
234 	DO(u.udps_hdrops, 11, 35);
235 	DO(i.ips_reassembled, 12, 0);
236 	DO(u.udps_badsum, 12, 35);
237 	DO(i.ips_forward, 13, 0);
238 	DO(u.udps_nosum, 13, 35);
239 	DO(i.ips_cantforward, 14, 0);
240 	DO(u.udps_badlen, 14, 35);
241 	DO(i.ips_redirectsent, 15, 0);
242 	DO(u.udps_noport, 15, 35);
243 	DO(i.ips_badoptions, 16, 0);
244 	DO(u.udps_noportbcast, 16, 35);
245 	DO(i.ips_notmember, 17, 0);
246 	DO(u.udps_fullsock, 17, 35);
247 	DO(i.ips_delivered, 18, 0);
248 	DO(u.udps_opackets, 18, 35);
249 #undef DO
250 }
251 
252 int
253 initip(void)
254 {
255 	size_t len;
256 	int name[4];
257 
258 	name[0] = CTL_NET;
259 	name[1] = PF_INET;
260 	name[2] = IPPROTO_IP;
261 	name[3] = IPCTL_STATS;
262 
263 	len = 0;
264 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
265 		error("sysctl getting ipstat size failed");
266 		return 0;
267 	}
268 	if (len > sizeof curstat.i) {
269 		error("ipstat structure has grown--recompile systat!");
270 		return 0;
271 	}
272 	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
273 		error("sysctl getting ipstat failed");
274 		return 0;
275 	}
276 	name[2] = IPPROTO_UDP;
277 	name[3] = UDPCTL_STATS;
278 
279 	len = 0;
280 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
281 		error("sysctl getting udpstat size failed");
282 		return 0;
283 	}
284 	if (len > sizeof curstat.u) {
285 		error("ipstat structure has grown--recompile systat!");
286 		return 0;
287 	}
288 	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
289 		error("sysctl getting udpstat failed");
290 		return 0;
291 	}
292 	oldstat = initstat;
293 	return 1;
294 }
295 
296 void
297 resetip(void)
298 {
299 	size_t len;
300 	int name[4];
301 
302 	name[0] = CTL_NET;
303 	name[1] = PF_INET;
304 	name[2] = IPPROTO_IP;
305 	name[3] = IPCTL_STATS;
306 
307 	len = sizeof initstat.i;
308 	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
309 		error("sysctl getting ipstat failed");
310 	}
311 	name[2] = IPPROTO_UDP;
312 	name[3] = UDPCTL_STATS;
313 
314 	len = sizeof initstat.u;
315 	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
316 		error("sysctl getting udpstat failed");
317 	}
318 	oldstat = initstat;
319 }
320 
321 void
322 fetchip(void)
323 {
324 	int name[4];
325 	size_t len;
326 
327 	oldstat = curstat;
328 	name[0] = CTL_NET;
329 	name[1] = PF_INET;
330 	name[2] = IPPROTO_IP;
331 	name[3] = IPCTL_STATS;
332 	len = sizeof curstat.i;
333 
334 	if (sysctl(name, 4, &curstat.i, &len, 0, 0) < 0)
335 		return;
336 	name[2] = IPPROTO_UDP;
337 	name[3] = UDPCTL_STATS;
338 	len = sizeof curstat.u;
339 
340 	if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0)
341 		return;
342 }
343