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.17 (Berkeley) 06/18/92"; 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 <arpa/inet.h> 38 #include <netdb.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include "netstat.h" 42 43 struct inpcb inpcb; 44 struct tcpcb tcpcb; 45 struct socket sockb; 46 47 char *inetname __P((struct in_addr *)); 48 void inetprint __P((struct in_addr *, int, char *)); 49 50 /* 51 * Print a summary of connections related to an Internet 52 * protocol. For TCP, also give state of connection. 53 * Listening processes (aflag) are suppressed unless the 54 * -a (all) flag is specified. 55 */ 56 void 57 protopr(off, name) 58 off_t off; 59 char *name; 60 { 61 struct inpcb cb; 62 register struct inpcb *prev, *next; 63 int istcp; 64 static int first = 1; 65 66 if (off == 0) 67 return; 68 istcp = strcmp(name, "tcp") == 0; 69 kread(off, (char *)&cb, sizeof (struct inpcb)); 70 inpcb = cb; 71 prev = (struct inpcb *)off; 72 if (inpcb.inp_next == (struct inpcb *)off) 73 return; 74 while (inpcb.inp_next != (struct inpcb *)off) { 75 next = inpcb.inp_next; 76 kread((off_t)next, (char *)&inpcb, sizeof (inpcb)); 77 if (inpcb.inp_prev != prev) { 78 printf("???\n"); 79 break; 80 } 81 if (!aflag && 82 inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) { 83 prev = next; 84 continue; 85 } 86 kread((off_t)inpcb.inp_socket, (char *)&sockb, sizeof (sockb)); 87 if (istcp) { 88 kread((off_t)inpcb.inp_ppcb, 89 (char *)&tcpcb, sizeof (tcpcb)); 90 } 91 if (first) { 92 printf("Active Internet connections"); 93 if (aflag) 94 printf(" (including servers)"); 95 putchar('\n'); 96 if (Aflag) 97 printf("%-8.8s ", "PCB"); 98 printf(Aflag ? 99 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" : 100 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n", 101 "Proto", "Recv-Q", "Send-Q", 102 "Local Address", "Foreign Address", "(state)"); 103 first = 0; 104 } 105 if (Aflag) 106 if (istcp) 107 printf("%8x ", inpcb.inp_ppcb); 108 else 109 printf("%8x ", next); 110 printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc, 111 sockb.so_snd.sb_cc); 112 inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, name); 113 inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, name); 114 if (istcp) { 115 if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES) 116 printf(" %d", tcpcb.t_state); 117 else 118 printf(" %s", tcpstates[tcpcb.t_state]); 119 } 120 putchar('\n'); 121 prev = next; 122 } 123 } 124 125 /* 126 * Dump TCP statistics structure. 127 */ 128 void 129 tcp_stats(off, name) 130 off_t off; 131 char *name; 132 { 133 struct tcpstat tcpstat; 134 135 if (off == 0) 136 return; 137 printf ("%s:\n", name); 138 kread(off, (char *)&tcpstat, sizeof (tcpstat)); 139 140 #define p(f, m) if (tcpstat.f || sflag <= 1) \ 141 printf(m, tcpstat.f, plural(tcpstat.f)) 142 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \ 143 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 void 198 udp_stats(off, name) 199 off_t off; 200 char *name; 201 { 202 struct udpstat udpstat; 203 204 if (off == 0) 205 return; 206 kread(off, (char *)&udpstat, sizeof (udpstat)); 207 printf("%s:\n", name); 208 #define p(f, m) if (udpstat.f || sflag <= 1) \ 209 printf(m, udpstat.f, plural(udpstat.f)) 210 p(udps_hdrops, "\t%u incomplete header%s\n"); 211 p(udps_badlen, "\t%u bad data length field%s\n"); 212 p(udps_badsum, "\t%u bad checksum%s\n"); 213 p(udps_noport, "\t%u no port%s\n"); 214 p(udps_noportbcast, "\t%u (arrived as bcast) no port%s\n"); 215 #undef p 216 } 217 218 /* 219 * Dump IP statistics structure. 220 */ 221 void 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 kread(off, (char *)&ipstat, sizeof (ipstat)); 231 printf("%s:\n", name); 232 233 #define p(f, m) if (ipstat.f || sflag <= 1) \ 234 printf(m, ipstat.f, plural(ipstat.f)) 235 236 p(ips_total, "\t%u total packet%s received\n"); 237 p(ips_badsum, "\t%u bad header checksum%s\n"); 238 p(ips_tooshort, "\t%u with size smaller than minimum\n"); 239 p(ips_toosmall, "\t%u with data size < data length\n"); 240 p(ips_badhlen, "\t%u with header length < data size\n"); 241 p(ips_badlen, "\t%u with data length < header length\n"); 242 p(ips_fragments, "\t%u fragment%s received\n"); 243 p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n"); 244 p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n"); 245 p(ips_forward, "\t%u packet%s forwarded\n"); 246 p(ips_cantforward, "\t%u packet%s not forwardable\n"); 247 p(ips_redirectsent, "\t%u redirect%s sent\n"); 248 #undef p 249 } 250 251 static char *icmpnames[] = { 252 "echo reply", 253 "#1", 254 "#2", 255 "destination unreachable", 256 "source quench", 257 "routing redirect", 258 "#6", 259 "#7", 260 "echo", 261 "#9", 262 "#10", 263 "time exceeded", 264 "parameter problem", 265 "time stamp", 266 "time stamp reply", 267 "information request", 268 "information request reply", 269 "address mask request", 270 "address mask reply", 271 }; 272 273 /* 274 * Dump ICMP statistics. 275 */ 276 void 277 icmp_stats(off, name) 278 off_t off; 279 char *name; 280 { 281 struct icmpstat icmpstat; 282 register int i, first; 283 284 if (off == 0) 285 return; 286 kread(off, (char *)&icmpstat, sizeof (icmpstat)); 287 printf("%s:\n", name); 288 289 #define p(f, m) if (icmpstat.f || sflag <= 1) \ 290 printf(m, icmpstat.f, plural(icmpstat.f)) 291 292 p(icps_error, "\t%u call%s to icmp_error\n"); 293 p(icps_oldicmp, 294 "\t%u error%s not generated 'cuz old message was icmp\n"); 295 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 296 if (icmpstat.icps_outhist[i] != 0) { 297 if (first) { 298 printf("\tOutput histogram:\n"); 299 first = 0; 300 } 301 printf("\t\t%s: %u\n", icmpnames[i], 302 icmpstat.icps_outhist[i]); 303 } 304 p(icps_badcode, "\t%u message%s with bad code fields\n"); 305 p(icps_tooshort, "\t%u message%s < minimum length\n"); 306 p(icps_checksum, "\t%u bad checksum%s\n"); 307 p(icps_badlen, "\t%u message%s with bad length\n"); 308 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 309 if (icmpstat.icps_inhist[i] != 0) { 310 if (first) { 311 printf("\tInput histogram:\n"); 312 first = 0; 313 } 314 printf("\t\t%s: %u\n", icmpnames[i], 315 icmpstat.icps_inhist[i]); 316 } 317 p(icps_reflect, "\t%u message response%s generated\n"); 318 #undef p 319 } 320 321 /* 322 * Pretty print an Internet address (net address + port). 323 * If the nflag was specified, use numbers instead of names. 324 */ 325 void 326 inetprint(in, port, proto) 327 register struct in_addr *in; 328 int port; 329 char *proto; 330 { 331 struct servent *sp = 0; 332 char line[80], *cp; 333 int width; 334 335 sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in)); 336 cp = index(line, '\0'); 337 if (!nflag && port) 338 sp = getservbyport((int)port, proto); 339 if (sp || port == 0) 340 sprintf(cp, "%.8s", sp ? sp->s_name : "*"); 341 else 342 sprintf(cp, "%d", ntohs((u_short)port)); 343 width = Aflag ? 18 : 22; 344 printf(" %-*.*s", width, width, line); 345 } 346 347 /* 348 * Construct an Internet address representation. 349 * If the nflag has been supplied, give 350 * numeric value, otherwise try for symbolic name. 351 */ 352 char * 353 inetname(inp) 354 struct in_addr *inp; 355 { 356 register char *cp; 357 static char line[50]; 358 struct hostent *hp; 359 struct netent *np; 360 static char domain[MAXHOSTNAMELEN + 1]; 361 static int first = 1; 362 363 if (first && !nflag) { 364 first = 0; 365 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 366 (cp = index(domain, '.'))) 367 (void) strcpy(domain, cp + 1); 368 else 369 domain[0] = 0; 370 } 371 cp = 0; 372 if (!nflag && inp->s_addr != INADDR_ANY) { 373 int net = inet_netof(*inp); 374 int lna = inet_lnaof(*inp); 375 376 if (lna == INADDR_ANY) { 377 np = getnetbyaddr(net, AF_INET); 378 if (np) 379 cp = np->n_name; 380 } 381 if (cp == 0) { 382 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); 383 if (hp) { 384 if ((cp = index(hp->h_name, '.')) && 385 !strcmp(cp + 1, domain)) 386 *cp = 0; 387 cp = hp->h_name; 388 } 389 } 390 } 391 if (inp->s_addr == INADDR_ANY) 392 strcpy(line, "*"); 393 else if (cp) 394 strcpy(line, cp); 395 else { 396 inp->s_addr = ntohl(inp->s_addr); 397 #define C(x) ((x) & 0xff) 398 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24), 399 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); 400 } 401 return (line); 402 } 403