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 /*static char sccsid[] = "from: @(#)inet.c 5.15 (Berkeley) 6/18/90";*/ 36 static char rcsid[] = "$Id: inet.c,v 1.5 1993/08/01 18:10:52 mycroft 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/ip_var.h> 53 #include <netinet/tcp.h> 54 #include <netinet/tcpip.h> 55 #include <netinet/tcp_seq.h> 56 #define TCPSTATES 57 #include <netinet/tcp_fsm.h> 58 #include <netinet/tcp_timer.h> 59 #include <netinet/tcp_var.h> 60 #include <netinet/tcp_debug.h> 61 #include <netinet/udp.h> 62 #include <netinet/udp_var.h> 63 64 #include <netdb.h> 65 66 #include <stdio.h> 67 #include <string.h> 68 69 struct inpcb inpcb; 70 struct tcpcb tcpcb; 71 struct socket sockb; 72 extern int Aflag; 73 extern int aflag; 74 extern int nflag; 75 extern char *plural(); 76 77 char *inetname(); 78 79 /* 80 * Print a summary of connections related to an Internet 81 * protocol. For TCP, also give state of connection. 82 * Listening processes (aflag) are suppressed unless the 83 * -a (all) flag is specified. 84 */ 85 protopr(off, name) 86 off_t off; 87 char *name; 88 { 89 struct inpcb cb; 90 register struct inpcb *prev, *next; 91 int istcp; 92 static int first = 1; 93 94 if (off == 0) 95 return; 96 istcp = strcmp(name, "tcp") == 0; 97 kvm_read(off, (char *)&cb, sizeof (struct inpcb)); 98 inpcb = cb; 99 prev = (struct inpcb *)off; 100 if (inpcb.inp_next == (struct inpcb *)off) 101 return; 102 while (inpcb.inp_next != (struct inpcb *)off) { 103 104 next = inpcb.inp_next; 105 kvm_read((off_t)next, (char *)&inpcb, sizeof (inpcb)); 106 if (inpcb.inp_prev != prev) { 107 printf("???\n"); 108 break; 109 } 110 if (!aflag && 111 inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) { 112 prev = next; 113 continue; 114 } 115 kvm_read((off_t)inpcb.inp_socket, 116 (char *)&sockb, sizeof (sockb)); 117 if (istcp) { 118 kvm_read((off_t)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, inpcb.inp_lport, name); 143 inetprint(&inpcb.inp_faddr, 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 tcp_stats(off, name) 159 off_t off; 160 char *name; 161 { 162 struct tcpstat tcpstat; 163 164 if (off == 0) 165 return; 166 printf ("%s:\n", name); 167 kvm_read(off, (char *)&tcpstat, sizeof (tcpstat)); 168 169 #define p(f, m) printf(m, tcpstat.f, plural(tcpstat.f)) 170 #define p2(f1, f2, m) printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2)) 171 172 p(tcps_sndtotal, "\t%d packet%s sent\n"); 173 p2(tcps_sndpack,tcps_sndbyte, 174 "\t\t%d data packet%s (%d byte%s)\n"); 175 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, 176 "\t\t%d data packet%s (%d byte%s) retransmitted\n"); 177 p2(tcps_sndacks, tcps_delack, 178 "\t\t%d ack-only packet%s (%d delayed)\n"); 179 p(tcps_sndurg, "\t\t%d URG only packet%s\n"); 180 p(tcps_sndprobe, "\t\t%d window probe packet%s\n"); 181 p(tcps_sndwinup, "\t\t%d window update packet%s\n"); 182 p(tcps_sndctrl, "\t\t%d control packet%s\n"); 183 p(tcps_rcvtotal, "\t%d packet%s received\n"); 184 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n"); 185 p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n"); 186 p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n"); 187 p2(tcps_rcvpack, tcps_rcvbyte, 188 "\t\t%d packet%s (%d byte%s) received in-sequence\n"); 189 p2(tcps_rcvduppack, tcps_rcvdupbyte, 190 "\t\t%d completely duplicate packet%s (%d byte%s)\n"); 191 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, 192 "\t\t%d packet%s with some dup. data (%d byte%s duped)\n"); 193 p2(tcps_rcvoopack, tcps_rcvoobyte, 194 "\t\t%d out-of-order packet%s (%d byte%s)\n"); 195 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, 196 "\t\t%d packet%s (%d byte%s) of data after window\n"); 197 p(tcps_rcvwinprobe, "\t\t%d window probe%s\n"); 198 p(tcps_rcvwinupd, "\t\t%d window update packet%s\n"); 199 p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n"); 200 p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n"); 201 p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n"); 202 p(tcps_rcvshort, "\t\t%d discarded because packet too short\n"); 203 p(tcps_connattempt, "\t%d connection request%s\n"); 204 p(tcps_accepts, "\t%d connection accept%s\n"); 205 p(tcps_connects, "\t%d connection%s established (including accepts)\n"); 206 p2(tcps_closed, tcps_drops, 207 "\t%d connection%s closed (including %d drop%s)\n"); 208 p(tcps_conndrops, "\t%d embryonic connection%s dropped\n"); 209 p2(tcps_rttupdated, tcps_segstimed, 210 "\t%d segment%s updated rtt (of %d attempt%s)\n"); 211 p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n"); 212 p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n"); 213 p(tcps_persisttimeo, "\t%d persist timeout%s\n"); 214 p(tcps_keeptimeo, "\t%d keepalive timeout%s\n"); 215 p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n"); 216 p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n"); 217 #undef p 218 #undef p2 219 } 220 221 /* 222 * Dump UDP statistics structure. 223 */ 224 udp_stats(off, name) 225 off_t off; 226 char *name; 227 { 228 struct udpstat udpstat; 229 230 if (off == 0) 231 return; 232 kvm_read(off, (char *)&udpstat, sizeof (udpstat)); 233 printf("%s:\n\t%u packet%s sent\n", name, 234 udpstat.udps_opackets, plural(udpstat.udps_opackets)); 235 printf("\t%u packet%s received\n", 236 udpstat.udps_ipackets, plural(udpstat.udps_ipackets)); 237 printf("\t%u incomplete header%s\n", 238 udpstat.udps_hdrops, plural(udpstat.udps_hdrops)); 239 printf("\t%u bad data length field%s\n", 240 udpstat.udps_badlen, plural(udpstat.udps_badlen)); 241 printf("\t%u bad checksum%s\n", 242 udpstat.udps_badsum, plural(udpstat.udps_badsum)); 243 printf("\t%u packet%s received on unbound ports\n", 244 udpstat.udps_noport, plural(udpstat.udps_noport)); 245 printf("\t%u packet%s received (arrived as bcast) on unbound ports\n", 246 udpstat.udps_noportbcast, plural(udpstat.udps_noportbcast)); 247 printf("\t%u packet%s dropped, socket full\n", 248 udpstat.udps_fullsock, plural(udpstat.udps_fullsock)); 249 printf("\t%u packet%s missed pcb cache\n", 250 udpstat.udpps_pcbcachemiss, plural(udpstat.udpps_pcbcachemiss)); 251 } 252 253 /* 254 * Dump IP statistics structure. 255 */ 256 ip_stats(off, name) 257 off_t off; 258 char *name; 259 { 260 struct ipstat ipstat; 261 262 if (off == 0) 263 return; 264 kvm_read(off, (char *)&ipstat, sizeof (ipstat)); 265 printf("%s:\n\t%u total packets received\n", name, 266 ipstat.ips_total); 267 printf("\t%u bad header checksum%s\n", 268 ipstat.ips_badsum, plural(ipstat.ips_badsum)); 269 printf("\t%u with size smaller than minimum\n", ipstat.ips_tooshort); 270 printf("\t%u with data size < data length\n", ipstat.ips_toosmall); 271 printf("\t%u with header length < data size\n", ipstat.ips_badhlen); 272 printf("\t%u with data length < header length\n", ipstat.ips_badlen); 273 printf("\t%u fragment%s received\n", 274 ipstat.ips_fragments, plural(ipstat.ips_fragments)); 275 printf("\t%u fragment%s dropped (dup or out of space)\n", 276 ipstat.ips_fragdropped, plural(ipstat.ips_fragdropped)); 277 printf("\t%u fragment%s dropped after timeout\n", 278 ipstat.ips_fragtimeout, plural(ipstat.ips_fragtimeout)); 279 printf("\t%u packet%s forwarded\n", 280 ipstat.ips_forward, plural(ipstat.ips_forward)); 281 printf("\t%u packet%s not forwardable\n", 282 ipstat.ips_cantforward, plural(ipstat.ips_cantforward)); 283 printf("\t%u redirect%s sent\n", 284 ipstat.ips_redirectsent, plural(ipstat.ips_redirectsent)); 285 } 286 287 static char *icmpnames[] = { 288 "echo reply", 289 "#1", 290 "#2", 291 "destination unreachable", 292 "source quench", 293 "routing redirect", 294 "#6", 295 "#7", 296 "echo", 297 "#9", 298 "#10", 299 "time exceeded", 300 "parameter problem", 301 "time stamp", 302 "time stamp reply", 303 "information request", 304 "information request reply", 305 "address mask request", 306 "address mask reply", 307 }; 308 309 /* 310 * Dump ICMP statistics. 311 */ 312 icmp_stats(off, name) 313 off_t off; 314 char *name; 315 { 316 struct icmpstat icmpstat; 317 register int i, first; 318 319 if (off == 0) 320 return; 321 kvm_read(off, (char *)&icmpstat, sizeof (icmpstat)); 322 printf("%s:\n\t%u call%s to icmp_error\n", name, 323 icmpstat.icps_error, plural(icmpstat.icps_error)); 324 printf("\t%u error%s not generated 'cuz old message was icmp\n", 325 icmpstat.icps_oldicmp, plural(icmpstat.icps_oldicmp)); 326 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 327 if (icmpstat.icps_outhist[i] != 0) { 328 if (first) { 329 printf("\tOutput histogram:\n"); 330 first = 0; 331 } 332 printf("\t\t%s: %u\n", icmpnames[i], 333 icmpstat.icps_outhist[i]); 334 } 335 printf("\t%u message%s with bad code fields\n", 336 icmpstat.icps_badcode, plural(icmpstat.icps_badcode)); 337 printf("\t%u message%s < minimum length\n", 338 icmpstat.icps_tooshort, plural(icmpstat.icps_tooshort)); 339 printf("\t%u bad checksum%s\n", 340 icmpstat.icps_checksum, plural(icmpstat.icps_checksum)); 341 printf("\t%u message%s with bad length\n", 342 icmpstat.icps_badlen, plural(icmpstat.icps_badlen)); 343 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 344 if (icmpstat.icps_inhist[i] != 0) { 345 if (first) { 346 printf("\tInput histogram:\n"); 347 first = 0; 348 } 349 printf("\t\t%s: %u\n", icmpnames[i], 350 icmpstat.icps_inhist[i]); 351 } 352 printf("\t%u message response%s generated\n", 353 icmpstat.icps_reflect, plural(icmpstat.icps_reflect)); 354 } 355 356 /* 357 * Pretty print an Internet address (net address + port). 358 * If the nflag was specified, use numbers instead of names. 359 */ 360 inetprint(in, port, proto) 361 register struct in_addr *in; 362 u_short port; 363 char *proto; 364 { 365 struct servent *sp = 0; 366 char line[80], *cp, *index(); 367 int width; 368 369 sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(*in)); 370 cp = index(line, '\0'); 371 if (!nflag && port) 372 sp = getservbyport((int)port, proto); 373 if (sp || port == 0) 374 sprintf(cp, "%.8s", sp ? sp->s_name : "*"); 375 else 376 sprintf(cp, "%d", ntohs((u_short)port)); 377 width = Aflag ? 18 : 22; 378 printf(" %-*.*s", width, width, line); 379 } 380 381 /* 382 * Construct an Internet address representation. 383 * If the nflag has been supplied, give 384 * numeric value, otherwise try for symbolic name. 385 */ 386 char * 387 inetname(in) 388 struct in_addr in; 389 { 390 register char *cp; 391 static char line[50]; 392 struct hostent *hp; 393 struct netent *np; 394 static char domain[MAXHOSTNAMELEN + 1]; 395 static int first = 1; 396 397 if (first && !nflag) { 398 first = 0; 399 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 400 (cp = index(domain, '.'))) 401 (void) strcpy(domain, cp + 1); 402 else 403 domain[0] = 0; 404 } 405 cp = 0; 406 if (!nflag && in.s_addr != INADDR_ANY) { 407 int net = inet_netof(in); 408 int lna = inet_lnaof(in); 409 410 if (lna == INADDR_ANY) { 411 np = getnetbyaddr(net, AF_INET); 412 if (np) 413 cp = np->n_name; 414 } 415 if (cp == 0) { 416 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET); 417 if (hp) { 418 if ((cp = index(hp->h_name, '.')) && 419 !strcmp(cp + 1, domain)) 420 *cp = 0; 421 cp = hp->h_name; 422 } 423 } 424 } 425 if (in.s_addr == INADDR_ANY) 426 strcpy(line, "*"); 427 else if (cp) 428 strcpy(line, cp); 429 else { 430 in.s_addr = ntohl(in.s_addr); 431 #define C(x) ((x) & 0xff) 432 sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), 433 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr)); 434 } 435 return (line); 436 } 437