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