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