xref: /original-bsd/usr.bin/netstat/inet.c (revision c3e32dec)
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.1 (Berkeley) 06/06/93";
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 
147 	p(tcps_sndtotal, "\t%d packet%s sent\n");
148 	p2(tcps_sndpack,tcps_sndbyte,
149 		"\t\t%d data packet%s (%d byte%s)\n");
150 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
151 		"\t\t%d data packet%s (%d byte%s) retransmitted\n");
152 	p2(tcps_sndacks, tcps_delack,
153 		"\t\t%d ack-only packet%s (%d delayed)\n");
154 	p(tcps_sndurg, "\t\t%d URG only packet%s\n");
155 	p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
156 	p(tcps_sndwinup, "\t\t%d window update packet%s\n");
157 	p(tcps_sndctrl, "\t\t%d control packet%s\n");
158 	p(tcps_rcvtotal, "\t%d packet%s received\n");
159 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
160 	p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
161 	p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
162 	p2(tcps_rcvpack, tcps_rcvbyte,
163 		"\t\t%d packet%s (%d byte%s) received in-sequence\n");
164 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
165 		"\t\t%d completely duplicate packet%s (%d byte%s)\n");
166 	p(tcps_pawsdrop, "\t\t%d old duplicate packet%s\n");
167 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
168 		"\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
169 	p2(tcps_rcvoopack, tcps_rcvoobyte,
170 		"\t\t%d out-of-order packet%s (%d byte%s)\n");
171 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
172 		"\t\t%d packet%s (%d byte%s) of data after window\n");
173 	p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
174 	p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
175 	p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
176 	p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
177 	p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
178 	p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
179 	p(tcps_connattempt, "\t%d connection request%s\n");
180 	p(tcps_accepts, "\t%d connection accept%s\n");
181 	p(tcps_connects, "\t%d connection%s established (including accepts)\n");
182 	p2(tcps_closed, tcps_drops,
183 		"\t%d connection%s closed (including %d drop%s)\n");
184 	p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
185 	p2(tcps_rttupdated, tcps_segstimed,
186 		"\t%d segment%s updated rtt (of %d attempt%s)\n");
187 	p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
188 	p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
189 	p(tcps_persisttimeo, "\t%d persist timeout%s\n");
190 	p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
191 	p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
192 	p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
193 #undef p
194 #undef p2
195 }
196 
197 /*
198  * Dump UDP statistics structure.
199  */
200 void
201 udp_stats(off, name)
202 	u_long off;
203 	char *name;
204 {
205 	struct udpstat udpstat;
206 	u_long delivered;
207 
208 	if (off == 0)
209 		return;
210 	kread(off, (char *)&udpstat, sizeof (udpstat));
211 	printf("%s:\n", name);
212 #define	p(f, m) if (udpstat.f || sflag <= 1) \
213     printf(m, udpstat.f, plural(udpstat.f))
214 	p(udps_ipackets, "\t%u datagram%s received\n");
215 	p(udps_hdrops, "\t%u with incomplete header\n");
216 	p(udps_badlen, "\t%u with bad data length field\n");
217 	p(udps_badsum, "\t%u with bad checksum\n");
218 	p(udps_noport, "\t%u dropped due to no socket\n");
219 	p(udps_noportbcast, "\t%u broadcast/multicast datagram%s dropped due to no socket\n");
220 	p(udps_fullsock, "\t%u dropped due to full socket buffers\n");
221 	delivered = udpstat.udps_ipackets -
222 		    udpstat.udps_hdrops -
223 		    udpstat.udps_badlen -
224 		    udpstat.udps_badsum -
225 		    udpstat.udps_noport -
226 		    udpstat.udps_noportbcast -
227 		    udpstat.udps_fullsock;
228 	if (delivered || sflag <= 1)
229 		printf("\t%u delivered\n", delivered);
230 	p(udps_opackets, "\t%u datagram%s output\n");
231 #undef p
232 }
233 
234 /*
235  * Dump IP statistics structure.
236  */
237 void
238 ip_stats(off, name)
239 	u_long off;
240 	char *name;
241 {
242 	struct ipstat ipstat;
243 
244 	if (off == 0)
245 		return;
246 	kread(off, (char *)&ipstat, sizeof (ipstat));
247 	printf("%s:\n", name);
248 
249 #define	p(f, m) if (ipstat.f || sflag <= 1) \
250     printf(m, ipstat.f, plural(ipstat.f))
251 
252 	p(ips_total, "\t%u total packet%s received\n");
253 	p(ips_badsum, "\t%u bad header checksum%s\n");
254 	p(ips_tooshort, "\t%u with size smaller than minimum\n");
255 	p(ips_toosmall, "\t%u with data size < data length\n");
256 	p(ips_badhlen, "\t%u with header length < data size\n");
257 	p(ips_badlen, "\t%u with data length < header length\n");
258 	p(ips_badoptions, "\t%u with bad options\n");
259 	p(ips_badvers, "\t%u with incorrect version number\n");
260 	p(ips_fragments, "\t%u fragment%s received\n");
261 	p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n");
262 	p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n");
263 	p(ips_reassembled, "\t%u packet%s reassembled ok\n");
264 	p(ips_delivered, "\t%u packet%s for this host\n");
265 	p(ips_noproto, "\t%u packet%s for unknown/unsupported protocol\n");
266 	p(ips_forward, "\t%u packet%s forwarded\n");
267 	p(ips_cantforward, "\t%u packet%s not forwardable\n");
268 	p(ips_redirectsent, "\t%u redirect%s sent\n");
269 	p(ips_localout, "\t%u packet%s sent from this host\n");
270 	p(ips_rawout, "\t%u packet%s sent with fabricated ip header\n");
271 	p(ips_odropped, "\t%u output packet%s dropped due to no bufs, etc.\n");
272 	p(ips_noroute, "\t%u output packet%s discarded due to no route\n");
273 	p(ips_fragmented, "\t%u output datagram%s fragmented\n");
274 	p(ips_ofragments, "\t%u fragment%s created\n");
275 	p(ips_cantfrag, "\t%u datagram%s that can't be fragmented\n");
276 #undef p
277 }
278 
279 static	char *icmpnames[] = {
280 	"echo reply",
281 	"#1",
282 	"#2",
283 	"destination unreachable",
284 	"source quench",
285 	"routing redirect",
286 	"#6",
287 	"#7",
288 	"echo",
289 	"#9",
290 	"#10",
291 	"time exceeded",
292 	"parameter problem",
293 	"time stamp",
294 	"time stamp reply",
295 	"information request",
296 	"information request reply",
297 	"address mask request",
298 	"address mask reply",
299 };
300 
301 /*
302  * Dump ICMP statistics.
303  */
304 void
305 icmp_stats(off, name)
306 	u_long off;
307 	char *name;
308 {
309 	struct icmpstat icmpstat;
310 	register int i, first;
311 
312 	if (off == 0)
313 		return;
314 	kread(off, (char *)&icmpstat, sizeof (icmpstat));
315 	printf("%s:\n", name);
316 
317 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
318     printf(m, icmpstat.f, plural(icmpstat.f))
319 
320 	p(icps_error, "\t%u call%s to icmp_error\n");
321 	p(icps_oldicmp,
322 	    "\t%u error%s not generated 'cuz old message was icmp\n");
323 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
324 		if (icmpstat.icps_outhist[i] != 0) {
325 			if (first) {
326 				printf("\tOutput histogram:\n");
327 				first = 0;
328 			}
329 			printf("\t\t%s: %u\n", icmpnames[i],
330 				icmpstat.icps_outhist[i]);
331 		}
332 	p(icps_badcode, "\t%u message%s with bad code fields\n");
333 	p(icps_tooshort, "\t%u message%s < minimum length\n");
334 	p(icps_checksum, "\t%u bad checksum%s\n");
335 	p(icps_badlen, "\t%u message%s with bad length\n");
336 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
337 		if (icmpstat.icps_inhist[i] != 0) {
338 			if (first) {
339 				printf("\tInput histogram:\n");
340 				first = 0;
341 			}
342 			printf("\t\t%s: %u\n", icmpnames[i],
343 				icmpstat.icps_inhist[i]);
344 		}
345 	p(icps_reflect, "\t%u message response%s generated\n");
346 #undef p
347 }
348 
349 /*
350  * Dump IGMP statistics structure.
351  */
352 void
353 igmp_stats(off, name)
354 	u_long off;
355 	char *name;
356 {
357 	struct igmpstat igmpstat;
358 
359 	if (off == 0)
360 		return;
361 	kread(off, (char *)&igmpstat, sizeof (igmpstat));
362 	printf("%s:\n", name);
363 
364 #define	p(f, m) if (igmpstat.f || sflag <= 1) \
365     printf(m, igmpstat.f, plural(igmpstat.f))
366 #define	py(f, m) if (igmpstat.f || sflag <= 1) \
367     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
368 	p(igps_rcv_total, "\t%u message%s received\n");
369         p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
370         p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
371         py(igps_rcv_queries, "\t%u membership quer%s received\n");
372         py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
373         p(igps_rcv_reports, "\t%u membership report%s received\n");
374         p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
375         p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
376         p(igps_snd_reports, "\t%u membership report%s sent\n");
377 #undef p
378 #undef py
379 }
380 
381 /*
382  * Pretty print an Internet address (net address + port).
383  * If the nflag was specified, use numbers instead of names.
384  */
385 void
386 inetprint(in, port, proto)
387 	register struct in_addr *in;
388 	int port;
389 	char *proto;
390 {
391 	struct servent *sp = 0;
392 	char line[80], *cp;
393 	int width;
394 
395 	sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in));
396 	cp = index(line, '\0');
397 	if (!nflag && port)
398 		sp = getservbyport((int)port, proto);
399 	if (sp || port == 0)
400 		sprintf(cp, "%.8s", sp ? sp->s_name : "*");
401 	else
402 		sprintf(cp, "%d", ntohs((u_short)port));
403 	width = Aflag ? 18 : 22;
404 	printf(" %-*.*s", width, width, line);
405 }
406 
407 /*
408  * Construct an Internet address representation.
409  * If the nflag has been supplied, give
410  * numeric value, otherwise try for symbolic name.
411  */
412 char *
413 inetname(inp)
414 	struct in_addr *inp;
415 {
416 	register char *cp;
417 	static char line[50];
418 	struct hostent *hp;
419 	struct netent *np;
420 	static char domain[MAXHOSTNAMELEN + 1];
421 	static int first = 1;
422 
423 	if (first && !nflag) {
424 		first = 0;
425 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
426 		    (cp = index(domain, '.')))
427 			(void) strcpy(domain, cp + 1);
428 		else
429 			domain[0] = 0;
430 	}
431 	cp = 0;
432 	if (!nflag && inp->s_addr != INADDR_ANY) {
433 		int net = inet_netof(*inp);
434 		int lna = inet_lnaof(*inp);
435 
436 		if (lna == INADDR_ANY) {
437 			np = getnetbyaddr(net, AF_INET);
438 			if (np)
439 				cp = np->n_name;
440 		}
441 		if (cp == 0) {
442 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
443 			if (hp) {
444 				if ((cp = index(hp->h_name, '.')) &&
445 				    !strcmp(cp + 1, domain))
446 					*cp = 0;
447 				cp = hp->h_name;
448 			}
449 		}
450 	}
451 	if (inp->s_addr == INADDR_ANY)
452 		strcpy(line, "*");
453 	else if (cp)
454 		strcpy(line, cp);
455 	else {
456 		inp->s_addr = ntohl(inp->s_addr);
457 #define C(x)	((x) & 0xff)
458 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
459 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
460 	}
461 	return (line);
462 }
463