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