inet.c revision 1.22 1 /* $NetBSD: inet.c,v 1.22 1997/07/23 21:31:27 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94";
39 #else
40 static char *rcsid = "$NetBSD: inet.c,v 1.22 1997/07/23 21:31:27 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/mbuf.h>
49 #include <sys/protosw.h>
50
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_icmp.h>
57 #include <netinet/icmp_var.h>
58 #include <netinet/igmp_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcpip.h>
62 #include <netinet/tcp_seq.h>
63 #define TCPSTATES
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_timer.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/tcp_debug.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70
71 #include <arpa/inet.h>
72 #include <netdb.h>
73 #include <stdio.h>
74 #include <string.h>
75 #include <unistd.h>
76 #include "netstat.h"
77
78 struct inpcb inpcb;
79 struct tcpcb tcpcb;
80 struct socket sockb;
81
82 char *inetname __P((struct in_addr *));
83 void inetprint __P((struct in_addr *, int, char *));
84
85 /*
86 * Print a summary of connections related to an Internet
87 * protocol. For TCP, also give state of connection.
88 * Listening processes (aflag) are suppressed unless the
89 * -a (all) flag is specified.
90 */
91 void
92 protopr(off, name)
93 u_long off;
94 char *name;
95 {
96 struct inpcbtable table;
97 register struct inpcb *head, *next, *prev;
98 struct inpcb inpcb;
99 int istcp;
100 static int first = 1;
101
102 if (off == 0)
103 return;
104 istcp = strcmp(name, "tcp") == 0;
105 kread(off, (char *)&table, sizeof table);
106 prev = head =
107 (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
108 next = table.inpt_queue.cqh_first;
109
110 while (next != head) {
111 kread((u_long)next, (char *)&inpcb, sizeof inpcb);
112 if (inpcb.inp_queue.cqe_prev != prev) {
113 printf("???\n");
114 break;
115 }
116 prev = next;
117 next = inpcb.inp_queue.cqe_next;
118
119 if (!aflag &&
120 inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
121 continue;
122 kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
123 if (istcp) {
124 kread((u_long)inpcb.inp_ppcb,
125 (char *)&tcpcb, sizeof (tcpcb));
126 }
127 if (first) {
128 printf("Active Internet connections");
129 if (aflag)
130 printf(" (including servers)");
131 putchar('\n');
132 if (Aflag)
133 printf("%-8.8s ", "PCB");
134 printf(Aflag ?
135 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
136 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
137 "Proto", "Recv-Q", "Send-Q",
138 "Local Address", "Foreign Address", "(state)");
139 first = 0;
140 }
141 if (Aflag)
142 if (istcp)
143 printf("%8lx ", (u_long) inpcb.inp_ppcb);
144 else
145 printf("%8lx ", (u_long) prev);
146 printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
147 sockb.so_snd.sb_cc);
148 inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, name);
149 inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, name);
150 if (istcp) {
151 if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
152 printf(" %d", tcpcb.t_state);
153 else
154 printf(" %s", tcpstates[tcpcb.t_state]);
155 }
156 putchar('\n');
157 }
158 }
159
160 /*
161 * Dump TCP statistics structure.
162 */
163 void
164 tcp_stats(off, name)
165 u_long off;
166 char *name;
167 {
168 struct tcpstat tcpstat;
169
170 if (off == 0)
171 return;
172 printf ("%s:\n", name);
173 kread(off, (char *)&tcpstat, sizeof (tcpstat));
174
175 #define ps(f, m) if (tcpstat.f || sflag <= 1) \
176 printf(m, tcpstat.f)
177 #define p(f, m) if (tcpstat.f || sflag <= 1) \
178 printf(m, tcpstat.f, plural(tcpstat.f))
179 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
180 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
181 #define p2s(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
182 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
183 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
184 printf(m, tcpstat.f, plurales(tcpstat.f))
185
186 p(tcps_sndtotal, "\t%lu packet%s sent\n");
187 p2(tcps_sndpack,tcps_sndbyte,
188 "\t\t%lu data packet%s (%lu byte%s)\n");
189 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
190 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
191 p2s(tcps_sndacks, tcps_delack,
192 "\t\t%lu ack-only packet%s (%lu delayed)\n");
193 p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
194 p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
195 p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
196 p(tcps_sndctrl, "\t\t%lu control packet%s\n");
197 p(tcps_rcvtotal, "\t%lu packet%s received\n");
198 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%lu ack%s (for %lu byte%s)\n");
199 p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
200 p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
201 p2(tcps_rcvpack, tcps_rcvbyte,
202 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
203 p2(tcps_rcvduppack, tcps_rcvdupbyte,
204 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
205 p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
206 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
207 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
208 p2(tcps_rcvoopack, tcps_rcvoobyte,
209 "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
210 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
211 "\t\t%lu packet%s (%lu byte%s) of data after window\n");
212 p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
213 p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
214 p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
215 p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
216 p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
217 ps(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
218 p(tcps_connattempt, "\t%lu connection request%s\n");
219 p(tcps_accepts, "\t%lu connection accept%s\n");
220 p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
221 p2(tcps_closed, tcps_drops,
222 "\t%lu connection%s closed (including %lu drop%s)\n");
223 p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
224 p2(tcps_rttupdated, tcps_segstimed,
225 "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
226 p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
227 p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
228 p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
229 p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
230 p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
231 p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
232 p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
233 p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
234 p3(tcps_pcbhashmiss, "\t%lu PCB hash miss%s\n");
235 ps(tcps_noport, "\t%lu dropped due to no socket\n");
236
237 p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
238 ps(tcps_sc_added, "\t%lu SYN cache entries added\n");
239 p(tcps_sc_collisions, "\t\t%lu hash collision%s\n");
240 ps(tcps_sc_completed, "\t\t%lu completed\n");
241 ps(tcps_sc_aborted, "\t\t%lu aborted (no space to build PCB)\n");
242 ps(tcps_sc_timed_out, "\t\t%lu timed out\n");
243 ps(tcps_sc_overflowed, "\t\t%lu dropped due to overflow\n");
244 ps(tcps_sc_bucketoverflow, "\t\t%lu dropped due to bucket overflow\n");
245 ps(tcps_sc_reset, "\t\t%lu dropped due to RST\n");
246 ps(tcps_sc_unreach, "\t\t%lu dropped due to ICMP unreachable\n");
247 p(tcps_sc_dupesyn, "\t%lu duplicate SYN%s received for entries already in the cache\n");
248 p(tcps_sc_dropped, "\t%lu SYN%s dropped (no route or no space)\n");
249
250 #undef p
251 #undef ps
252 #undef p2
253 #undef p2s
254 #undef p3
255 }
256
257 /*
258 * Dump UDP statistics structure.
259 */
260 void
261 udp_stats(off, name)
262 u_long off;
263 char *name;
264 {
265 struct udpstat udpstat;
266 u_long delivered;
267
268 if (off == 0)
269 return;
270 printf("%s:\n", name);
271 kread(off, (char *)&udpstat, sizeof (udpstat));
272
273 #define ps(f, m) if (udpstat.f || sflag <= 1) \
274 printf(m, udpstat.f)
275 #define p(f, m) if (udpstat.f || sflag <= 1) \
276 printf(m, udpstat.f, plural(udpstat.f))
277 #define p3(f, m) if (udpstat.f || sflag <= 1) \
278 printf(m, udpstat.f, plurales(udpstat.f))
279
280 p(udps_ipackets, "\t%lu datagram%s received\n");
281 ps(udps_hdrops, "\t%lu with incomplete header\n");
282 ps(udps_badlen, "\t%lu with bad data length field\n");
283 ps(udps_badsum, "\t%lu with bad checksum\n");
284 ps(udps_noport, "\t%lu dropped due to no socket\n");
285 p(udps_noportbcast, "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
286 ps(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
287 delivered = udpstat.udps_ipackets -
288 udpstat.udps_hdrops -
289 udpstat.udps_badlen -
290 udpstat.udps_badsum -
291 udpstat.udps_noport -
292 udpstat.udps_noportbcast -
293 udpstat.udps_fullsock;
294 if (delivered || sflag <= 1)
295 printf("\t%lu delivered\n", delivered);
296 p3(udps_pcbhashmiss, "\t%lu PCB hash miss%s\n");
297 p(udps_opackets, "\t%lu datagram%s output\n");
298
299 #undef ps
300 #undef p
301 #undef p3
302 }
303
304 /*
305 * Dump IP statistics structure.
306 */
307 void
308 ip_stats(off, name)
309 u_long off;
310 char *name;
311 {
312 struct ipstat ipstat;
313
314 if (off == 0)
315 return;
316 kread(off, (char *)&ipstat, sizeof (ipstat));
317 printf("%s:\n", name);
318
319 #define ps(f, m) if (ipstat.f || sflag <= 1) \
320 printf(m, ipstat.f)
321 #define p(f, m) if (ipstat.f || sflag <= 1) \
322 printf(m, ipstat.f, plural(ipstat.f))
323
324 p(ips_total, "\t%lu total packet%s received\n");
325 p(ips_badsum, "\t%lu bad header checksum%s\n");
326 ps(ips_toosmall, "\t%lu with size smaller than minimum\n");
327 ps(ips_tooshort, "\t%lu with data size < data length\n");
328 ps(ips_toolong, "\t%lu with length > max ip packet size\n");
329 ps(ips_badhlen, "\t%lu with header length < data size\n");
330 ps(ips_badlen, "\t%lu with data length < header length\n");
331 ps(ips_badoptions, "\t%lu with bad options\n");
332 ps(ips_badvers, "\t%lu with incorrect version number\n");
333 p(ips_fragments, "\t%lu fragment%s received\n");
334 p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
335 p(ips_badfrags, "\t%lu malformed fragment%s dropped\n");
336 p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
337 p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
338 p(ips_delivered, "\t%lu packet%s for this host\n");
339 p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
340 p(ips_forward, "\t%lu packet%s forwarded\n");
341 p(ips_cantforward, "\t%lu packet%s not forwardable\n");
342 p(ips_redirectsent, "\t%lu redirect%s sent\n");
343 p(ips_localout, "\t%lu packet%s sent from this host\n");
344 p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
345 p(ips_odropped, "\t%lu output packet%s dropped due to no bufs, etc.\n");
346 p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
347 p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
348 p(ips_ofragments, "\t%lu fragment%s created\n");
349 p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
350 #undef ps
351 #undef p
352 }
353
354 static char *icmpnames[] = {
355 "echo reply",
356 "#1",
357 "#2",
358 "destination unreachable",
359 "source quench",
360 "routing redirect",
361 "#6",
362 "#7",
363 "echo",
364 "#9",
365 "#10",
366 "time exceeded",
367 "parameter problem",
368 "time stamp",
369 "time stamp reply",
370 "information request",
371 "information request reply",
372 "address mask request",
373 "address mask reply",
374 };
375
376 /*
377 * Dump ICMP statistics.
378 */
379 void
380 icmp_stats(off, name)
381 u_long off;
382 char *name;
383 {
384 struct icmpstat icmpstat;
385 register int i, first;
386
387 if (off == 0)
388 return;
389 kread(off, (char *)&icmpstat, sizeof (icmpstat));
390 printf("%s:\n", name);
391
392 #define p(f, m) if (icmpstat.f || sflag <= 1) \
393 printf(m, icmpstat.f, plural(icmpstat.f))
394
395 p(icps_error, "\t%lu call%s to icmp_error\n");
396 p(icps_oldicmp,
397 "\t%lu error%s not generated because old message was icmp\n");
398 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
399 if (icmpstat.icps_outhist[i] != 0) {
400 if (first) {
401 printf("\tOutput histogram:\n");
402 first = 0;
403 }
404 printf("\t\t%s: %lu\n", icmpnames[i],
405 icmpstat.icps_outhist[i]);
406 }
407 p(icps_badcode, "\t%lu message%s with bad code fields\n");
408 p(icps_tooshort, "\t%lu message%s < minimum length\n");
409 p(icps_checksum, "\t%lu bad checksum%s\n");
410 p(icps_badlen, "\t%lu message%s with bad length\n");
411 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
412 if (icmpstat.icps_inhist[i] != 0) {
413 if (first) {
414 printf("\tInput histogram:\n");
415 first = 0;
416 }
417 printf("\t\t%s: %lu\n", icmpnames[i],
418 icmpstat.icps_inhist[i]);
419 }
420 p(icps_reflect, "\t%lu message response%s generated\n");
421 #undef p
422 }
423
424 /*
425 * Dump IGMP statistics structure.
426 */
427 void
428 igmp_stats(off, name)
429 u_long off;
430 char *name;
431 {
432 struct igmpstat igmpstat;
433
434 if (off == 0)
435 return;
436 kread(off, (char *)&igmpstat, sizeof (igmpstat));
437 printf("%s:\n", name);
438
439 #define p(f, m) if (igmpstat.f || sflag <= 1) \
440 printf(m, igmpstat.f, plural(igmpstat.f))
441 #define py(f, m) if (igmpstat.f || sflag <= 1) \
442 printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
443 p(igps_rcv_total, "\t%lu message%s received\n");
444 p(igps_rcv_tooshort, "\t%lu message%s received with too few bytes\n");
445 p(igps_rcv_badsum, "\t%lu message%s received with bad checksum\n");
446 py(igps_rcv_queries, "\t%lu membership quer%s received\n");
447 py(igps_rcv_badqueries, "\t%lu membership quer%s received with invalid field(s)\n");
448 p(igps_rcv_reports, "\t%lu membership report%s received\n");
449 p(igps_rcv_badreports, "\t%lu membership report%s received with invalid field(s)\n");
450 p(igps_rcv_ourreports, "\t%lu membership report%s received for groups to which we belong\n");
451 p(igps_snd_reports, "\t%lu membership report%s sent\n");
452 #undef p
453 #undef py
454 }
455
456 /*
457 * Pretty print an Internet address (net address + port).
458 * If the nflag was specified, use numbers instead of names.
459 */
460 void
461 inetprint(in, port, proto)
462 register struct in_addr *in;
463 int port;
464 char *proto;
465 {
466 struct servent *sp = 0;
467 char line[80], *cp;
468 int width;
469
470 sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in));
471 cp = index(line, '\0');
472 if (!nflag && port)
473 sp = getservbyport((int)port, proto);
474 if (sp || port == 0)
475 sprintf(cp, "%.8s", sp ? sp->s_name : "*");
476 else
477 sprintf(cp, "%u", ntohs((u_short)port));
478 width = Aflag ? 18 : 22;
479 printf(" %-*.*s", width, width, line);
480 }
481
482 /*
483 * Construct an Internet address representation.
484 * If the nflag has been supplied, give
485 * numeric value, otherwise try for symbolic name.
486 */
487 char *
488 inetname(inp)
489 struct in_addr *inp;
490 {
491 register char *cp;
492 static char line[50];
493 struct hostent *hp;
494 struct netent *np;
495 static char domain[MAXHOSTNAMELEN + 1];
496 static int first = 1;
497
498 if (first && !nflag) {
499 first = 0;
500 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
501 (cp = index(domain, '.')))
502 (void) strcpy(domain, cp + 1);
503 else
504 domain[0] = 0;
505 }
506 cp = 0;
507 if (!nflag && inp->s_addr != INADDR_ANY) {
508 int net = inet_netof(*inp);
509 int lna = inet_lnaof(*inp);
510
511 if (lna == INADDR_ANY) {
512 np = getnetbyaddr(net, AF_INET);
513 if (np)
514 cp = np->n_name;
515 }
516 if (cp == 0) {
517 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
518 if (hp) {
519 if ((cp = index(hp->h_name, '.')) &&
520 !strcmp(cp + 1, domain))
521 *cp = 0;
522 cp = hp->h_name;
523 }
524 }
525 }
526 if (inp->s_addr == INADDR_ANY)
527 strcpy(line, "*");
528 else if (cp)
529 strcpy(line, cp);
530 else {
531 inp->s_addr = ntohl(inp->s_addr);
532 #define C(x) ((x) & 0xff)
533 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
534 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
535 }
536 return (line);
537 }
538