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