xref: /freebsd/usr.bin/systat/ip.c (revision aa339f1d5df9e38f36a34eb522355c4eebcae6c4)
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 
33 
34 /* From:
35 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
36 */
37 
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/udp.h>
48 #include <netinet/udp_var.h>
49 
50 #include <inttypes.h>
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 struct stat {
60 	struct ipstat i;
61 	struct udpstat u;
62 };
63 
64 static struct stat curstat, initstat, oldstat;
65 
66 /*-
67 --0         1         2         3         4         5         6         7
68 --0123456789012345678901234567890123456789012345678901234567890123456789012345
69 00          IP Input                           IP Output
70 01999999999 total packets received   999999999 total packets sent
71 02999999999 - with bad checksums     999999999 - generated locally
72 03999999999 - too short for header   999999999 - output drops
73 04999999999 - too short for data     999999999 output fragments generated
74 05999999999 - with invalid hlen      999999999 - fragmentation failed
75 06999999999 - with invalid length    999999999 destinations unreachable
76 07999999999 - with invalid version   999999999 packets output via raw IP
77 08999999999 - jumbograms
78 09999999999 total fragments received           UDP Statistics
79 10999999999 - fragments dropped      999999999 total input packets
80 11999999999 - fragments timed out    999999999 - too short for header
81 12999999999 - packets reassembled ok 999999999 - invalid checksum
82 13999999999 packets forwarded        999999999 - no checksum
83 14999999999 - unreachable dests      999999999 - invalid length
84 15999999999 - redirects generated    999999999 - no socket for dest port
85 16999999999 option errors            999999999 - no socket for broadcast
86 17999999999 unwanted multicasts      999999999 - socket buffer full
87 18999999999 delivered to upper layer 999999999 total output packets
88 --0123456789012345678901234567890123456789012345678901234567890123456789012345
89 --0         1         2         3         4         5         6         7
90 */
91 
92 WINDOW *
93 openip(void)
94 {
95 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
96 }
97 
98 void
99 closeip(WINDOW *w)
100 {
101 	if (w == NULL)
102 		return;
103 	wclear(w);
104 	wrefresh(w);
105 	delwin(w);
106 }
107 
108 void
109 labelip(void)
110 {
111 	wmove(wnd, 0, 0); wclrtoeol(wnd);
112 #define L(row, str) mvwprintw(wnd, row, 10, str)
113 #define R(row, str) mvwprintw(wnd, row, 45, str);
114 	L(0, "IP Input");		R(0, "IP Output");
115 	L(1, "total packets received");	R(1, "total packets sent");
116 	L(2, "- with bad checksums");	R(2, "- generated locally");
117 	L(3, "- too short for header");	R(3, "- output drops");
118 	L(4, "- too short for data");	R(4, "output fragments generated");
119 	L(5, "- with invalid hlen");	R(5, "- fragmentation failed");
120 	L(6, "- with invalid length");	R(6, "destinations unreachable");
121 	L(7, "- with invalid version");	R(7, "packets output via raw IP");
122 	L(8, "- jumbograms");
123 	L(9, "total fragments received");	R(9, "UDP Statistics");
124 	L(10, "- fragments dropped");	R(10, "total input packets");
125 	L(11, "- fragments timed out");	R(11, "- too short for header");
126 	L(12, "- packets reassembled ok");	R(12, "- invalid checksum");
127 	L(13, "packets forwarded");	R(13, "- no checksum");
128 	L(14, "- unreachable dests");	R(14, "- invalid length");
129 	L(15, "- redirects generated");	R(15, "- no socket for dest port");
130 	L(16, "option errors");		R(16, "- no socket for broadcast");
131 	L(17, "unwanted multicasts");	R(17, "- socket buffer full");
132 	L(18, "delivered to upper layer");	R(18, "total output packets");
133 #undef L
134 #undef R
135 }
136 
137 static void
138 domode(struct stat *ret)
139 {
140 	const struct stat *sub;
141 	int divisor = 1;
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(i.ips_total);
160 	DO(i.ips_badsum);
161 	DO(i.ips_tooshort);
162 	DO(i.ips_toosmall);
163 	DO(i.ips_badhlen);
164 	DO(i.ips_badlen);
165 	DO(i.ips_fragments);
166 	DO(i.ips_fragdropped);
167 	DO(i.ips_fragtimeout);
168 	DO(i.ips_forward);
169 	DO(i.ips_cantforward);
170 	DO(i.ips_redirectsent);
171 	DO(i.ips_noproto);
172 	DO(i.ips_delivered);
173 	DO(i.ips_localout);
174 	DO(i.ips_odropped);
175 	DO(i.ips_reassembled);
176 	DO(i.ips_fragmented);
177 	DO(i.ips_ofragments);
178 	DO(i.ips_cantfrag);
179 	DO(i.ips_badoptions);
180 	DO(i.ips_noroute);
181 	DO(i.ips_badvers);
182 	DO(i.ips_rawout);
183 	DO(i.ips_toolong);
184 	DO(i.ips_notmember);
185 	DO(u.udps_ipackets);
186 	DO(u.udps_hdrops);
187 	DO(u.udps_badsum);
188 	DO(u.udps_nosum);
189 	DO(u.udps_badlen);
190 	DO(u.udps_noport);
191 	DO(u.udps_noportbcast);
192 	DO(u.udps_fullsock);
193 	DO(u.udps_opackets);
194 #undef DO
195 }
196 
197 void
198 showip(void)
199 {
200 	struct stat stats;
201 	uint64_t totalout;
202 
203 	domode(&stats);
204 	totalout = stats.i.ips_forward + stats.i.ips_localout;
205 
206 #define DO(stat, row, col) \
207 	mvwprintw(wnd, row, col, "%9"PRIu64, stats.stat)
208 
209 	DO(i.ips_total, 1, 0);
210 	mvwprintw(wnd, 1, 35, "%9"PRIu64, totalout);
211 	DO(i.ips_badsum, 2, 0);
212 	DO(i.ips_localout, 2, 35);
213 	DO(i.ips_tooshort, 3, 0);
214 	DO(i.ips_odropped, 3, 35);
215 	DO(i.ips_toosmall, 4, 0);
216 	DO(i.ips_ofragments, 4, 35);
217 	DO(i.ips_badhlen, 5, 0);
218 	DO(i.ips_cantfrag, 5, 35);
219 	DO(i.ips_badlen, 6, 0);
220 	DO(i.ips_noroute, 6, 35);
221 	DO(i.ips_badvers, 7, 0);
222 	DO(i.ips_rawout, 7, 35);
223 	DO(i.ips_toolong, 8, 0);
224 	DO(i.ips_fragments, 9, 0);
225 	DO(i.ips_fragdropped, 10, 0);
226 	DO(u.udps_ipackets, 10, 35);
227 	DO(i.ips_fragtimeout, 11, 0);
228 	DO(u.udps_hdrops, 11, 35);
229 	DO(i.ips_reassembled, 12, 0);
230 	DO(u.udps_badsum, 12, 35);
231 	DO(i.ips_forward, 13, 0);
232 	DO(u.udps_nosum, 13, 35);
233 	DO(i.ips_cantforward, 14, 0);
234 	DO(u.udps_badlen, 14, 35);
235 	DO(i.ips_redirectsent, 15, 0);
236 	DO(u.udps_noport, 15, 35);
237 	DO(i.ips_badoptions, 16, 0);
238 	DO(u.udps_noportbcast, 16, 35);
239 	DO(i.ips_notmember, 17, 0);
240 	DO(u.udps_fullsock, 17, 35);
241 	DO(i.ips_delivered, 18, 0);
242 	DO(u.udps_opackets, 18, 35);
243 #undef DO
244 }
245 
246 int
247 initip(void)
248 {
249 	size_t len;
250 	int name[4];
251 
252 	name[0] = CTL_NET;
253 	name[1] = PF_INET;
254 	name[2] = IPPROTO_IP;
255 	name[3] = IPCTL_STATS;
256 
257 	len = 0;
258 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
259 		error("sysctl getting ipstat size failed");
260 		return 0;
261 	}
262 	if (len > sizeof curstat.i) {
263 		error("ipstat structure has grown--recompile systat!");
264 		return 0;
265 	}
266 	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
267 		error("sysctl getting ipstat failed");
268 		return 0;
269 	}
270 	name[2] = IPPROTO_UDP;
271 	name[3] = UDPCTL_STATS;
272 
273 	len = 0;
274 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
275 		error("sysctl getting udpstat size failed");
276 		return 0;
277 	}
278 	if (len > sizeof curstat.u) {
279 		error("ipstat structure has grown--recompile systat!");
280 		return 0;
281 	}
282 	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
283 		error("sysctl getting udpstat failed");
284 		return 0;
285 	}
286 	oldstat = initstat;
287 	return 1;
288 }
289 
290 void
291 resetip(void)
292 {
293 	size_t len;
294 	int name[4];
295 
296 	name[0] = CTL_NET;
297 	name[1] = PF_INET;
298 	name[2] = IPPROTO_IP;
299 	name[3] = IPCTL_STATS;
300 
301 	len = sizeof initstat.i;
302 	if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
303 		error("sysctl getting ipstat failed");
304 	}
305 	name[2] = IPPROTO_UDP;
306 	name[3] = UDPCTL_STATS;
307 
308 	len = sizeof initstat.u;
309 	if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
310 		error("sysctl getting udpstat failed");
311 	}
312 	oldstat = initstat;
313 }
314 
315 void
316 fetchip(void)
317 {
318 	int name[4];
319 	size_t len;
320 
321 	oldstat = curstat;
322 	name[0] = CTL_NET;
323 	name[1] = PF_INET;
324 	name[2] = IPPROTO_IP;
325 	name[3] = IPCTL_STATS;
326 	len = sizeof curstat.i;
327 
328 	if (sysctl(name, 4, &curstat.i, &len, 0, 0) < 0)
329 		return;
330 	name[2] = IPPROTO_UDP;
331 	name[3] = UDPCTL_STATS;
332 	len = sizeof curstat.u;
333 
334 	if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0)
335 		return;
336 }
337