1 /* 2 * Copyright (c) 1983, 1988, 1993, 1995 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.5 (Berkeley) 05/24/95"; 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_badsyn, "\t%d bad connection attempt%s\n"); 184 p(tcps_connects, "\t%d connection%s established (including accepts)\n"); 185 p2(tcps_closed, tcps_drops, 186 "\t%d connection%s closed (including %d drop%s)\n"); 187 p(tcps_conndrops, "\t%d embryonic connection%s dropped\n"); 188 p2(tcps_rttupdated, tcps_segstimed, 189 "\t%d segment%s updated rtt (of %d attempt%s)\n"); 190 p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n"); 191 p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n"); 192 p(tcps_persisttimeo, "\t%d persist timeout%s\n"); 193 p(tcps_persistdrop, "\t%d connection%s timed out in persist\n"); 194 p(tcps_keeptimeo, "\t%d keepalive timeout%s\n"); 195 p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n"); 196 p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n"); 197 p(tcps_predack, "\t%d correct ACK header prediction%s\n"); 198 p(tcps_preddat, "\t%d correct data packet header prediction%s\n"); 199 p3(tcps_pcbcachemiss, "\t%d PCB cache miss%s\n"); 200 #undef p 201 #undef p2 202 #undef p3 203 } 204 205 /* 206 * Dump UDP statistics structure. 207 */ 208 void 209 udp_stats(off, name) 210 u_long off; 211 char *name; 212 { 213 struct udpstat udpstat; 214 u_long delivered; 215 216 if (off == 0) 217 return; 218 kread(off, (char *)&udpstat, sizeof (udpstat)); 219 printf("%s:\n", name); 220 #define p(f, m) if (udpstat.f || sflag <= 1) \ 221 printf(m, udpstat.f, plural(udpstat.f)) 222 p(udps_ipackets, "\t%u datagram%s received\n"); 223 p(udps_hdrops, "\t%u with incomplete header\n"); 224 p(udps_badlen, "\t%u with bad data length field\n"); 225 p(udps_badsum, "\t%u with bad checksum\n"); 226 p(udps_noport, "\t%u dropped due to no socket\n"); 227 p(udps_noportbcast, "\t%u broadcast/multicast datagram%s dropped due to no socket\n"); 228 p(udps_fullsock, "\t%u dropped due to full socket buffers\n"); 229 delivered = udpstat.udps_ipackets - 230 udpstat.udps_hdrops - 231 udpstat.udps_badlen - 232 udpstat.udps_badsum - 233 udpstat.udps_noport - 234 udpstat.udps_noportbcast - 235 udpstat.udps_fullsock; 236 if (delivered || sflag <= 1) 237 printf("\t%u delivered\n", delivered); 238 p(udps_opackets, "\t%u datagram%s output\n"); 239 #undef p 240 } 241 242 /* 243 * Dump IP statistics structure. 244 */ 245 void 246 ip_stats(off, name) 247 u_long off; 248 char *name; 249 { 250 struct ipstat ipstat; 251 252 if (off == 0) 253 return; 254 kread(off, (char *)&ipstat, sizeof (ipstat)); 255 printf("%s:\n", name); 256 257 #define p(f, m) if (ipstat.f || sflag <= 1) \ 258 printf(m, ipstat.f, plural(ipstat.f)) 259 260 p(ips_total, "\t%u total packet%s received\n"); 261 p(ips_badsum, "\t%u bad header checksum%s\n"); 262 p(ips_toosmall, "\t%u with size smaller than minimum\n"); 263 p(ips_tooshort, "\t%u with data size < data length\n"); 264 p(ips_badhlen, "\t%u with header length < data size\n"); 265 p(ips_badlen, "\t%u with data length < header length\n"); 266 p(ips_badoptions, "\t%u with bad options\n"); 267 p(ips_badvers, "\t%u with incorrect version number\n"); 268 p(ips_fragments, "\t%u fragment%s received\n"); 269 p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n"); 270 p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n"); 271 p(ips_reassembled, "\t%u packet%s reassembled ok\n"); 272 p(ips_delivered, "\t%u packet%s for this host\n"); 273 p(ips_noproto, "\t%u packet%s for unknown/unsupported protocol\n"); 274 p(ips_forward, "\t%u packet%s forwarded\n"); 275 p(ips_cantforward, "\t%u packet%s not forwardable\n"); 276 p(ips_redirectsent, "\t%u redirect%s sent\n"); 277 p(ips_localout, "\t%u packet%s sent from this host\n"); 278 p(ips_rawout, "\t%u packet%s sent with fabricated ip header\n"); 279 p(ips_odropped, "\t%u output packet%s dropped due to no bufs, etc.\n"); 280 p(ips_noroute, "\t%u output packet%s discarded due to no route\n"); 281 p(ips_fragmented, "\t%u output datagram%s fragmented\n"); 282 p(ips_ofragments, "\t%u fragment%s created\n"); 283 p(ips_cantfrag, "\t%u datagram%s that can't be fragmented\n"); 284 #undef p 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 void 313 icmp_stats(off, name) 314 u_long off; 315 char *name; 316 { 317 struct icmpstat icmpstat; 318 register int i, first; 319 320 if (off == 0) 321 return; 322 kread(off, (char *)&icmpstat, sizeof (icmpstat)); 323 printf("%s:\n", name); 324 325 #define p(f, m) if (icmpstat.f || sflag <= 1) \ 326 printf(m, icmpstat.f, plural(icmpstat.f)) 327 328 p(icps_error, "\t%u call%s to icmp_error\n"); 329 p(icps_oldicmp, 330 "\t%u error%s not generated 'cuz old message was icmp\n"); 331 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 332 if (icmpstat.icps_outhist[i] != 0) { 333 if (first) { 334 printf("\tOutput histogram:\n"); 335 first = 0; 336 } 337 printf("\t\t%s: %u\n", icmpnames[i], 338 icmpstat.icps_outhist[i]); 339 } 340 p(icps_badcode, "\t%u message%s with bad code fields\n"); 341 p(icps_tooshort, "\t%u message%s < minimum length\n"); 342 p(icps_checksum, "\t%u bad checksum%s\n"); 343 p(icps_badlen, "\t%u message%s with bad length\n"); 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 p(icps_reflect, "\t%u message response%s generated\n"); 354 #undef p 355 } 356 357 /* 358 * Dump IGMP statistics structure. 359 */ 360 void 361 igmp_stats(off, name) 362 u_long off; 363 char *name; 364 { 365 struct igmpstat igmpstat; 366 367 if (off == 0) 368 return; 369 kread(off, (char *)&igmpstat, sizeof (igmpstat)); 370 printf("%s:\n", name); 371 372 #define p(f, m) if (igmpstat.f || sflag <= 1) \ 373 printf(m, igmpstat.f, plural(igmpstat.f)) 374 #define py(f, m) if (igmpstat.f || sflag <= 1) \ 375 printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y") 376 p(igps_rcv_total, "\t%u message%s received\n"); 377 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n"); 378 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n"); 379 py(igps_rcv_queries, "\t%u membership quer%s received\n"); 380 py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n"); 381 p(igps_rcv_reports, "\t%u membership report%s received\n"); 382 p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n"); 383 p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n"); 384 p(igps_snd_reports, "\t%u membership report%s sent\n"); 385 #undef p 386 #undef py 387 } 388 389 /* 390 * Pretty print an Internet address (net address + port). 391 * If the nflag was specified, use numbers instead of names. 392 */ 393 void 394 inetprint(in, port, proto) 395 register struct in_addr *in; 396 int port; 397 char *proto; 398 { 399 struct servent *sp = 0; 400 char line[80], *cp; 401 int width; 402 403 sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in)); 404 cp = index(line, '\0'); 405 if (!nflag && port) 406 sp = getservbyport((int)port, proto); 407 if (sp || port == 0) 408 sprintf(cp, "%.8s", sp ? sp->s_name : "*"); 409 else 410 sprintf(cp, "%d", ntohs((u_short)port)); 411 width = Aflag ? 18 : 22; 412 printf(" %-*.*s", width, width, line); 413 } 414 415 /* 416 * Construct an Internet address representation. 417 * If the nflag has been supplied, give 418 * numeric value, otherwise try for symbolic name. 419 */ 420 char * 421 inetname(inp) 422 struct in_addr *inp; 423 { 424 register char *cp; 425 static char line[50]; 426 struct hostent *hp; 427 struct netent *np; 428 static char domain[MAXHOSTNAMELEN + 1]; 429 static int first = 1; 430 431 if (first && !nflag) { 432 first = 0; 433 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 434 (cp = index(domain, '.'))) 435 (void) strcpy(domain, cp + 1); 436 else 437 domain[0] = 0; 438 } 439 cp = 0; 440 if (!nflag && inp->s_addr != INADDR_ANY) { 441 int net = inet_netof(*inp); 442 int lna = inet_lnaof(*inp); 443 444 if (lna == INADDR_ANY) { 445 np = getnetbyaddr(net, AF_INET); 446 if (np) 447 cp = np->n_name; 448 } 449 if (cp == 0) { 450 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); 451 if (hp) { 452 if ((cp = index(hp->h_name, '.')) && 453 !strcmp(cp + 1, domain)) 454 *cp = 0; 455 cp = hp->h_name; 456 } 457 } 458 } 459 if (inp->s_addr == INADDR_ANY) 460 strcpy(line, "*"); 461 else if (cp) 462 strcpy(line, cp); 463 else { 464 inp->s_addr = ntohl(inp->s_addr); 465 #define C(x) ((x) & 0xff) 466 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24), 467 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); 468 } 469 return (line); 470 } 471