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