xref: /openbsd-src/usr.bin/netstat/inet.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
1 /*	$OpenBSD: inet.c,v 1.3 1996/06/26 05:37:21 deraadt Exp $	*/
2 /*	$NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)inet.c	8.4 (Berkeley) 4/20/94";
40 #else
41 static char *rcsid = "$OpenBSD: inet.c,v 1.3 1996/06/26 05:37:21 deraadt Exp $";
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/mbuf.h>
50 #include <sys/protosw.h>
51 
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/igmp_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/tcp.h>
62 #include <netinet/tcpip.h>
63 #include <netinet/tcp_seq.h>
64 #define TCPSTATES
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcp_debug.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
71 
72 #include <arpa/inet.h>
73 #include <netdb.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include "netstat.h"
78 
79 struct	inpcb inpcb;
80 struct	tcpcb tcpcb;
81 struct	socket sockb;
82 
83 char	*inetname __P((struct in_addr *));
84 void	inetprint __P((struct in_addr *, int, char *));
85 
86 /*
87  * Print a summary of connections related to an Internet
88  * protocol.  For TCP, also give state of connection.
89  * Listening processes (aflag) are suppressed unless the
90  * -a (all) flag is specified.
91  */
92 void
93 protopr(off, name)
94 	u_long off;
95 	char *name;
96 {
97 	struct inpcbtable table;
98 	register struct inpcb *head, *next, *prev;
99 	struct inpcb inpcb;
100 	int istcp;
101 	static int first = 1;
102 
103 	if (off == 0)
104 		return;
105 	istcp = strcmp(name, "tcp") == 0;
106 	kread(off, (char *)&table, sizeof table);
107 	prev = head =
108 	    (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
109 	next = table.inpt_queue.cqh_first;
110 
111 	while (next != head) {
112 		kread((u_long)next, (char *)&inpcb, sizeof inpcb);
113 		if (inpcb.inp_queue.cqe_prev != prev) {
114 			printf("???\n");
115 			break;
116 		}
117 		prev = next;
118 		next = inpcb.inp_queue.cqe_next;
119 
120 		if (!aflag &&
121 		    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
122 			continue;
123 		kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
124 		if (istcp) {
125 			kread((u_long)inpcb.inp_ppcb,
126 			    (char *)&tcpcb, sizeof (tcpcb));
127 		}
128 		if (first) {
129 			printf("Active Internet connections");
130 			if (aflag)
131 				printf(" (including servers)");
132 			putchar('\n');
133 			if (Aflag)
134 				printf("%-8.8s ", "PCB");
135 			printf(Aflag ?
136 				"%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
137 				"%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
138 				"Proto", "Recv-Q", "Send-Q",
139 				"Local Address", "Foreign Address", "(state)");
140 			first = 0;
141 		}
142 		if (Aflag)
143 			if (istcp)
144 				printf("%8x ", inpcb.inp_ppcb);
145 			else
146 				printf("%8x ", prev);
147 		printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
148 			sockb.so_snd.sb_cc);
149 		inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, name);
150 		inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, name);
151 		if (istcp) {
152 			if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
153 				printf(" %d", tcpcb.t_state);
154 			else
155 				printf(" %s", tcpstates[tcpcb.t_state]);
156 		}
157 		putchar('\n');
158 	}
159 }
160 
161 /*
162  * Dump TCP statistics structure.
163  */
164 void
165 tcp_stats(off, name)
166 	u_long off;
167 	char *name;
168 {
169 	struct tcpstat tcpstat;
170 
171 	if (off == 0)
172 		return;
173 	printf ("%s:\n", name);
174 	kread(off, (char *)&tcpstat, sizeof (tcpstat));
175 
176 #define	p(f, m) if (tcpstat.f || sflag <= 1) \
177     printf(m, tcpstat.f, plural(tcpstat.f))
178 #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
179     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
180 #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
181     printf(m, tcpstat.f, plurales(tcpstat.f))
182 
183 	p(tcps_sndtotal, "\t%d packet%s sent\n");
184 	p2(tcps_sndpack,tcps_sndbyte,
185 		"\t\t%d data packet%s (%d byte%s)\n");
186 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
187 		"\t\t%d data packet%s (%d byte%s) retransmitted\n");
188 	p2(tcps_sndacks, tcps_delack,
189 		"\t\t%d ack-only packet%s (%d delayed)\n");
190 	p(tcps_sndurg, "\t\t%d URG only packet%s\n");
191 	p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
192 	p(tcps_sndwinup, "\t\t%d window update packet%s\n");
193 	p(tcps_sndctrl, "\t\t%d control packet%s\n");
194 	p(tcps_rcvtotal, "\t%d packet%s received\n");
195 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
196 	p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
197 	p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
198 	p2(tcps_rcvpack, tcps_rcvbyte,
199 		"\t\t%d packet%s (%d byte%s) received in-sequence\n");
200 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
201 		"\t\t%d completely duplicate packet%s (%d byte%s)\n");
202 	p(tcps_pawsdrop, "\t\t%d old duplicate packet%s\n");
203 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
204 		"\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
205 	p2(tcps_rcvoopack, tcps_rcvoobyte,
206 		"\t\t%d out-of-order packet%s (%d byte%s)\n");
207 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
208 		"\t\t%d packet%s (%d byte%s) of data after window\n");
209 	p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
210 	p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
211 	p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
212 	p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
213 	p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
214 	p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
215 	p(tcps_connattempt, "\t%d connection request%s\n");
216 	p(tcps_accepts, "\t%d connection accept%s\n");
217 	p(tcps_connects, "\t%d connection%s established (including accepts)\n");
218 	p2(tcps_closed, tcps_drops,
219 		"\t%d connection%s closed (including %d drop%s)\n");
220 	p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
221 	p2(tcps_rttupdated, tcps_segstimed,
222 		"\t%d segment%s updated rtt (of %d attempt%s)\n");
223 	p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
224 	p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
225 	p(tcps_persisttimeo, "\t%d persist timeout%s\n");
226 	p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
227 	p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
228 	p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
229 	p(tcps_predack, "\t%d correct ACK header prediction%s\n");
230 	p(tcps_preddat, "\t%d correct data packet header prediction%s\n");
231 	p3(tcps_pcbhashmiss, "\t%d PCB cache miss%s\n");
232 #undef p
233 #undef p2
234 #undef p3
235 }
236 
237 /*
238  * Dump UDP statistics structure.
239  */
240 void
241 udp_stats(off, name)
242 	u_long off;
243 	char *name;
244 {
245 	struct udpstat udpstat;
246 	u_long delivered;
247 
248 	if (off == 0)
249 		return;
250 	kread(off, (char *)&udpstat, sizeof (udpstat));
251 	printf("%s:\n", name);
252 #define	p(f, m) if (udpstat.f || sflag <= 1) \
253     printf(m, udpstat.f, plural(udpstat.f))
254 	p(udps_ipackets, "\t%u datagram%s received\n");
255 	p(udps_hdrops, "\t%u with incomplete header\n");
256 	p(udps_badlen, "\t%u with bad data length field\n");
257 	p(udps_badsum, "\t%u with bad checksum\n");
258 	p(udps_noport, "\t%u dropped due to no socket\n");
259 	p(udps_noportbcast, "\t%u broadcast/multicast datagram%s dropped due to no socket\n");
260 	p(udps_fullsock, "\t%u dropped due to full socket buffers\n");
261 	delivered = udpstat.udps_ipackets -
262 		    udpstat.udps_hdrops -
263 		    udpstat.udps_badlen -
264 		    udpstat.udps_badsum -
265 		    udpstat.udps_noport -
266 		    udpstat.udps_noportbcast -
267 		    udpstat.udps_fullsock;
268 	if (delivered || sflag <= 1)
269 		printf("\t%u delivered\n", delivered);
270 	p(udps_opackets, "\t%u datagram%s output\n");
271 #undef p
272 }
273 
274 /*
275  * Dump IP statistics structure.
276  */
277 void
278 ip_stats(off, name)
279 	u_long off;
280 	char *name;
281 {
282 	struct ipstat ipstat;
283 
284 	if (off == 0)
285 		return;
286 	kread(off, (char *)&ipstat, sizeof (ipstat));
287 	printf("%s:\n", name);
288 
289 #define	p(f, m) if (ipstat.f || sflag <= 1) \
290     printf(m, ipstat.f, plural(ipstat.f))
291 
292 	p(ips_total, "\t%u total packet%s received\n");
293 	p(ips_badsum, "\t%u bad header checksum%s\n");
294 	p(ips_toosmall, "\t%u with size smaller than minimum\n");
295 	p(ips_tooshort, "\t%u with data size < data length\n");
296 	p(ips_badhlen, "\t%u with header length < data size\n");
297 	p(ips_badlen, "\t%u with data length < header length\n");
298 	p(ips_badoptions, "\t%u with bad options\n");
299 	p(ips_badvers, "\t%u with incorrect version number\n");
300 	p(ips_fragments, "\t%u fragment%s received\n");
301 	p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n");
302 	p(ips_badfrags, "\t%u malformed fragment%s dropped\n");
303 	p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n");
304 	p(ips_reassembled, "\t%u packet%s reassembled ok\n");
305 	p(ips_delivered, "\t%u packet%s for this host\n");
306 	p(ips_noproto, "\t%u packet%s for unknown/unsupported protocol\n");
307 	p(ips_forward, "\t%u packet%s forwarded\n");
308 	p(ips_cantforward, "\t%u packet%s not forwardable\n");
309 	p(ips_redirectsent, "\t%u redirect%s sent\n");
310 	p(ips_localout, "\t%u packet%s sent from this host\n");
311 	p(ips_rawout, "\t%u packet%s sent with fabricated ip header\n");
312 	p(ips_odropped, "\t%u output packet%s dropped due to no bufs, etc.\n");
313 	p(ips_noroute, "\t%u output packet%s discarded due to no route\n");
314 	p(ips_fragmented, "\t%u output datagram%s fragmented\n");
315 	p(ips_ofragments, "\t%u fragment%s created\n");
316 	p(ips_cantfrag, "\t%u datagram%s that can't be fragmented\n");
317 #undef p
318 }
319 
320 static	char *icmpnames[] = {
321 	"echo reply",
322 	"#1",
323 	"#2",
324 	"destination unreachable",
325 	"source quench",
326 	"routing redirect",
327 	"#6",
328 	"#7",
329 	"echo",
330 	"#9",
331 	"#10",
332 	"time exceeded",
333 	"parameter problem",
334 	"time stamp",
335 	"time stamp reply",
336 	"information request",
337 	"information request reply",
338 	"address mask request",
339 	"address mask reply",
340 };
341 
342 /*
343  * Dump ICMP statistics.
344  */
345 void
346 icmp_stats(off, name)
347 	u_long off;
348 	char *name;
349 {
350 	struct icmpstat icmpstat;
351 	register int i, first;
352 
353 	if (off == 0)
354 		return;
355 	kread(off, (char *)&icmpstat, sizeof (icmpstat));
356 	printf("%s:\n", name);
357 
358 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
359     printf(m, icmpstat.f, plural(icmpstat.f))
360 
361 	p(icps_error, "\t%u call%s to icmp_error\n");
362 	p(icps_oldicmp,
363 	    "\t%u error%s not generated 'cuz old message was icmp\n");
364 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
365 		if (icmpstat.icps_outhist[i] != 0) {
366 			if (first) {
367 				printf("\tOutput histogram:\n");
368 				first = 0;
369 			}
370 			printf("\t\t%s: %u\n", icmpnames[i],
371 				icmpstat.icps_outhist[i]);
372 		}
373 	p(icps_badcode, "\t%u message%s with bad code fields\n");
374 	p(icps_tooshort, "\t%u message%s < minimum length\n");
375 	p(icps_checksum, "\t%u bad checksum%s\n");
376 	p(icps_badlen, "\t%u message%s with bad length\n");
377 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
378 		if (icmpstat.icps_inhist[i] != 0) {
379 			if (first) {
380 				printf("\tInput histogram:\n");
381 				first = 0;
382 			}
383 			printf("\t\t%s: %u\n", icmpnames[i],
384 				icmpstat.icps_inhist[i]);
385 		}
386 	p(icps_reflect, "\t%u message response%s generated\n");
387 #undef p
388 }
389 
390 /*
391  * Dump IGMP statistics structure.
392  */
393 void
394 igmp_stats(off, name)
395 	u_long off;
396 	char *name;
397 {
398 	struct igmpstat igmpstat;
399 
400 	if (off == 0)
401 		return;
402 	kread(off, (char *)&igmpstat, sizeof (igmpstat));
403 	printf("%s:\n", name);
404 
405 #define	p(f, m) if (igmpstat.f || sflag <= 1) \
406     printf(m, igmpstat.f, plural(igmpstat.f))
407 #define	py(f, m) if (igmpstat.f || sflag <= 1) \
408     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
409 	p(igps_rcv_total, "\t%u message%s received\n");
410         p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
411         p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
412         py(igps_rcv_queries, "\t%u membership quer%s received\n");
413         py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
414         p(igps_rcv_reports, "\t%u membership report%s received\n");
415         p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
416         p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
417         p(igps_snd_reports, "\t%u membership report%s sent\n");
418 #undef p
419 #undef py
420 }
421 
422 /*
423  * Pretty print an Internet address (net address + port).
424  * If the nflag was specified, use numbers instead of names.
425  */
426 void
427 inetprint(in, port, proto)
428 	register struct in_addr *in;
429 	int port;
430 	char *proto;
431 {
432 	struct servent *sp = 0;
433 	char line[80], *cp;
434 	int width;
435 
436 	sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in));
437 	cp = index(line, '\0');
438 	if (!nflag && port)
439 		sp = getservbyport((int)port, proto);
440 	if (sp || port == 0)
441 		sprintf(cp, "%.8s", sp ? sp->s_name : "*");
442 	else
443 		sprintf(cp, "%d", ntohs((u_short)port));
444 	width = Aflag ? 18 : 22;
445 	printf(" %-*.*s", width, width, line);
446 }
447 
448 /*
449  * Construct an Internet address representation.
450  * If the nflag has been supplied, give
451  * numeric value, otherwise try for symbolic name.
452  */
453 char *
454 inetname(inp)
455 	struct in_addr *inp;
456 {
457 	register char *cp;
458 	static char line[50];
459 	struct hostent *hp;
460 	struct netent *np;
461 	static char domain[MAXHOSTNAMELEN + 1];
462 	static int first = 1;
463 
464 	if (first && !nflag) {
465 		first = 0;
466 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
467 		    (cp = index(domain, '.')))
468 			(void) strcpy(domain, cp + 1);
469 		else
470 			domain[0] = 0;
471 	}
472 	cp = 0;
473 	if (!nflag && inp->s_addr != INADDR_ANY) {
474 		int net = inet_netof(*inp);
475 		int lna = inet_lnaof(*inp);
476 
477 		if (lna == INADDR_ANY) {
478 			np = getnetbyaddr(net, AF_INET);
479 			if (np)
480 				cp = np->n_name;
481 		}
482 		if (cp == 0) {
483 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
484 			if (hp) {
485 				if ((cp = index(hp->h_name, '.')) &&
486 				    !strcmp(cp + 1, domain))
487 					*cp = 0;
488 				cp = hp->h_name;
489 			}
490 		}
491 	}
492 	if (inp->s_addr == INADDR_ANY)
493 		strcpy(line, "*");
494 	else if (cp)
495 		strcpy(line, cp);
496 	else {
497 		inp->s_addr = ntohl(inp->s_addr);
498 #define C(x)	((x) & 0xff)
499 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
500 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
501 	}
502 	return (line);
503 }
504