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