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