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