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