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