2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 ** @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 * Copyright (c) 1983, 1988, 1993, 1995
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
64 static const char rcsid
[] =
65 "$Id: inet.c,v 1.9 2006/04/04 04:36:27 lindak Exp $";
68 #include <sys/param.h>
69 #include <sys/queue.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sysctl.h>
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
79 #include <netinet/ip6.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/ip_icmp.h>
83 #include <netinet/icmp_var.h>
84 #include <netinet/igmp_var.h>
85 #include <netinet/ip_var.h>
86 #include <netinet/tcp.h>
87 #include <netinet/tcpip.h>
88 #include <netinet/tcp_seq.h>
90 #include <netinet/tcp_fsm.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet/udp.h>
93 #include <netinet/udp_var.h>
95 #include <arpa/inet.h>
106 #define ROUNDUP64(a) \
107 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint64_t) - 1))) : sizeof(uint64_t))
108 #define ADVANCE64(x, n) (((char *)x) += ROUNDUP64(n))
110 char *inetname (struct in_addr
*);
111 void inetprint (struct in_addr
*, int, char *, int);
113 extern void inet6print (struct in6_addr
*, int, char *, int);
114 static int udp_done
, tcp_done
;
118 typedef struct __table_private table_t
;
120 extern table_t
*_nc_table_new(uint32_t n
);
121 extern void _nc_table_free(table_t
*tin
);
123 extern void _nc_table_insert(table_t
*t
, const char *key
, void *datum
);
124 extern void *_nc_table_find(table_t
*t
, const char *key
);
125 extern void _nc_table_delete(table_t
*t
, const char *key
);
127 static table_t
*_serv_cache
= NULL
;
130 * Read and cache all known services
136 char *key
, *name
, *test
;
138 if (_serv_cache
!= NULL
) return;
140 _serv_cache
= _nc_table_new(8192);
143 while (NULL
!= (s
= getservent()))
145 if (s
->s_name
== NULL
) continue;
147 asprintf(&key
, "%hu/%s", (unsigned short)ntohs(s
->s_port
), s
->s_proto
);
148 name
= strdup(s
->s_name
);
149 test
= _nc_table_find(_serv_cache
, key
);
150 if (test
== NULL
) _nc_table_insert(_serv_cache
, key
, name
);
160 _nc_table_free(_serv_cache
);
165 _serv_cache_getservbyport(int port
, char *proto
)
167 static struct servent s
;
173 memset(&s
, 0, sizeof(struct servent
));
174 asprintf(&key
, "%u/%s", port
, (proto
== NULL
) ? "udp" : proto
);
176 s
.s_name
= _nc_table_find(_serv_cache
, key
);
178 if (s
.s_name
== NULL
) return NULL
;
186 #endif /* SRVCACHE */
189 * Print a summary of connections related to an Internet
190 * protocol. For TCP, also give state of connection.
191 * Listening processes (aflag) are suppressed unless the
192 * -a (all) flag is specified.
196 u_int32_t xgn_len
; /* length of this structure */
197 u_int32_t xgn_kind
; /* number of PCBs at this time */
200 #define ALL_XGN_KIND_INP (XSO_SOCKET | XSO_RCVBUF | XSO_SNDBUF | XSO_STATS | XSO_INPCB)
201 #define ALL_XGN_KIND_TCP (ALL_XGN_KIND_INP | XSO_TCPCB)
204 protopr(uint32_t proto
, /* for sysctl version we pass proto # */
208 static int first
= 1;
211 struct xinpgen
*xig
, *oxig
;
214 struct xtcpcb_n
*tp
= NULL
;
215 struct xinpcb_n
*inp
= NULL
;
216 struct xsocket_n
*so
= NULL
;
217 struct xsockbuf_n
*so_rcv
= NULL
;
218 struct xsockbuf_n
*so_snd
= NULL
;
219 struct xsockstat_n
*so_stat
= NULL
;
232 mibvar
= "net.inet.tcp.pcblist_n";
241 mibvar
= "net.inet.udp.pcblist_n";
244 mibvar
= "net.inet.divert.pcblist_n";
247 mibvar
= "net.inet.raw.pcblist_n";
251 if (sysctlbyname(mibvar
, 0, &len
, 0, 0) < 0) {
253 warn("sysctl: %s", mibvar
);
256 if ((buf
= malloc(len
)) == 0) {
257 warn("malloc %lu bytes", (u_long
)len
);
260 if (sysctlbyname(mibvar
, buf
, &len
, 0, 0) < 0) {
261 warn("sysctl: %s", mibvar
);
267 * Bail-out to avoid logic error in the loop below when
268 * there is in fact no more control block to process
270 if (len
<= sizeof(struct xinpgen
)) {
275 oxig
= xig
= (struct xinpgen
*)buf
;
276 for (next
= buf
+ ROUNDUP64(xig
->xig_len
); next
< buf
+ len
; next
+= ROUNDUP64(xgn
->xgn_len
)) {
278 xgn
= (struct xgen_n
*)next
;
279 if (xgn
->xgn_len
<= sizeof(struct xinpgen
))
282 if ((which
& xgn
->xgn_kind
) == 0) {
283 which
|= xgn
->xgn_kind
;
284 switch (xgn
->xgn_kind
) {
286 so
= (struct xsocket_n
*)xgn
;
289 so_rcv
= (struct xsockbuf_n
*)xgn
;
292 so_snd
= (struct xsockbuf_n
*)xgn
;
295 so_stat
= (struct xsockstat_n
*)xgn
;
298 inp
= (struct xinpcb_n
*)xgn
;
301 tp
= (struct xtcpcb_n
*)xgn
;
304 printf("unexpected kind %d\n", xgn
->xgn_kind
);
308 printf("got %d twice\n", xgn
->xgn_kind
);
311 if ((istcp
&& which
!= ALL_XGN_KIND_TCP
) || (!istcp
&& which
!= ALL_XGN_KIND_INP
))
315 /* Ignore sockets for protocols other than the desired one. */
316 if (so
->xso_protocol
!= (int)proto
)
319 /* Ignore PCBs which were freed during copyout. */
320 if (inp
->inp_gencnt
> oxig
->xig_gen
)
323 if ((af
== AF_INET
&& (inp
->inp_vflag
& INP_IPV4
) == 0)
325 || (af
== AF_INET6
&& (inp
->inp_vflag
& INP_IPV6
) == 0)
327 || (af
== AF_UNSPEC
&& ((inp
->inp_vflag
& INP_IPV4
) == 0
337 * Local address is not an indication of listening socket or
338 * server sockey but just rather the socket has been bound.
339 * That why many UDP sockets were not displayed in the original code.
341 if (!aflag
&& istcp
&& tp
->t_state
<= TCPS_LISTEN
)
344 if (Lflag
&& !so
->so_qlimit
)
349 printf("Active Internet connections");
351 printf(" (including servers)");
354 "Current listen queue sizes (qlen/incqlen/maxqlen)");
357 #if !TARGET_OS_EMBEDDED
358 printf("%-16.16s ", "Socket");
360 printf("%-8.8s ", "Socket");
362 printf("%-9.9s", "Flowhash");
365 printf("%-14.14s %-22.22s\n",
366 "Listen", "Local Address");
368 printf((Aflag
&& !Wflag
) ?
369 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %-11.11s" :
370 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %-11.11s",
371 "Proto", "Recv-Q", "Send-Q",
372 "Local Address", "Foreign Address",
375 printf(" %10.10s %10.10s", "rxbytes", "txbytes");
377 printf(" %7.7s[%1d] %7.7s[%1d]", "rxbytes", prioflag
, "txbytes", prioflag
);
384 #if !TARGET_OS_EMBEDDED
385 printf("%16lx ", (u_long
)inp
->inp_ppcb
);
387 printf("%8lx ", (u_long
)inp
->inp_ppcb
);
391 #if !TARGET_OS_EMBEDDED
392 printf("%16lx ", (u_long
)so
->so_pcb
);
394 printf("%8lx ", (u_long
)so
->so_pcb
);
396 printf("%8x ", inp
->inp_flowhash
);
401 snprintf(buf
, 15, "%d/%d/%d", so
->so_qlen
,
402 so
->so_incqlen
, so
->so_qlimit
);
403 printf("%-14.14s ", buf
);
409 if ((inp
->inp_vflag
& INP_IPV6
) != 0)
410 vchar
= ((inp
->inp_vflag
& INP_IPV4
) != 0)
414 vchar
= ((inp
->inp_vflag
& INP_IPV4
) != 0)
417 printf("%-3.3s%-2.2s %6u %6u ", name
, vchar
,
422 if (inp
->inp_vflag
& INP_IPV4
) {
423 inetprint(&inp
->inp_laddr
, (int)inp
->inp_lport
,
426 inetprint(&inp
->inp_faddr
,
427 (int)inp
->inp_fport
, name
, 1);
430 else if (inp
->inp_vflag
& INP_IPV6
) {
431 inet6print(&inp
->in6p_laddr
,
432 (int)inp
->inp_lport
, name
, 1);
434 inet6print(&inp
->in6p_faddr
,
435 (int)inp
->inp_fport
, name
, 1);
436 } /* else nothing printed now */
438 } else if (inp
->inp_flags
& INP_ANONPORT
) {
439 if (inp
->inp_vflag
& INP_IPV4
) {
440 inetprint(&inp
->inp_laddr
, (int)inp
->inp_lport
,
443 inetprint(&inp
->inp_faddr
,
444 (int)inp
->inp_fport
, name
, 0);
447 else if (inp
->inp_vflag
& INP_IPV6
) {
448 inet6print(&inp
->in6p_laddr
,
449 (int)inp
->inp_lport
, name
, 1);
451 inet6print(&inp
->in6p_faddr
,
452 (int)inp
->inp_fport
, name
, 0);
453 } /* else nothing printed now */
456 if (inp
->inp_vflag
& INP_IPV4
) {
457 inetprint(&inp
->inp_laddr
, (int)inp
->inp_lport
,
460 inetprint(&inp
->inp_faddr
,
461 (int)inp
->inp_fport
, name
,
466 else if (inp
->inp_vflag
& INP_IPV6
) {
467 inet6print(&inp
->in6p_laddr
,
468 (int)inp
->inp_lport
, name
, 0);
470 inet6print(&inp
->in6p_faddr
,
471 (int)inp
->inp_fport
, name
,
474 } /* else nothing printed now */
477 if (istcp
&& !Lflag
) {
478 if (tp
->t_state
< 0 || tp
->t_state
>= TCP_NSTATES
)
479 printf("%-11d", tp
->t_state
);
481 printf("%-11s", tcpstates
[tp
->t_state
]);
482 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
483 /* Show T/TCP `hidden state' */
484 if (tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
))
486 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
490 printf("%-11s", " ");
493 u_int64_t rxbytes
= 0;
494 u_int64_t txbytes
= 0;
496 for (i
= 0; i
< SO_TC_MAX
; i
++) {
497 rxbytes
+= so_stat
->xst_tc_stats
[i
].rxbytes
;
498 txbytes
+= so_stat
->xst_tc_stats
[i
].txbytes
;
501 printf(" %10llu %10llu", rxbytes
, txbytes
);
504 printf(" %10llu %10llu",
505 prioflag
< SO_TC_MAX
? so_stat
->xst_tc_stats
[prioflag
].rxbytes
: 0,
506 prioflag
< SO_TC_MAX
? so_stat
->xst_tc_stats
[prioflag
].txbytes
: 0);
510 if (xig
!= oxig
&& xig
->xig_gen
!= oxig
->xig_gen
) {
511 if (oxig
->xig_count
> xig
->xig_count
) {
512 printf("Some %s sockets may have been deleted.\n",
514 } else if (oxig
->xig_count
< xig
->xig_count
) {
515 printf("Some %s sockets may have been created.\n",
518 printf("Some %s sockets may have been created or deleted",
526 * Dump TCP statistics structure.
529 tcp_stats(uint32_t off
, char *name
, int af
)
531 static struct tcpstat ptcpstat
;
532 struct tcpstat tcpstat
;
533 size_t len
= sizeof tcpstat
;
535 if (sysctlbyname("net.inet.tcp.stats", &tcpstat
, &len
, 0, 0) < 0) {
536 warn("sysctl: net.inet.tcp.stats");
541 if (tcp_done
!= 0 && interval
== 0)
547 printf ("%s:\n", name
);
549 #define TCPDIFF(f) (tcpstat.f - ptcpstat.f)
550 #define p(f, m) if (TCPDIFF(f) || sflag <= 1) \
551 printf(m, TCPDIFF(f), plural(TCPDIFF(f)))
552 #define p1a(f, m) if (TCPDIFF(f) || sflag <= 1) \
553 printf(m, TCPDIFF(f))
554 #define p2(f1, f2, m) if (TCPDIFF(f1) || TCPDIFF(f2) || sflag <= 1) \
555 printf(m, TCPDIFF(f1), plural(TCPDIFF(f1)), TCPDIFF(f2), plural(TCPDIFF(f2)))
556 #define p2a(f1, f2, m) if (TCPDIFF(f1) || TCPDIFF(f2) || sflag <= 1) \
557 printf(m, TCPDIFF(f1), plural(TCPDIFF(f1)), TCPDIFF(f2))
558 #define p3(f, m) if (TCPDIFF(f) || sflag <= 1) \
559 printf(m, TCPDIFF(f), plurales(TCPDIFF(f)))
561 p(tcps_sndtotal
, "\t%u packet%s sent\n");
562 p2(tcps_sndpack
,tcps_sndbyte
,
563 "\t\t%u data packet%s (%u byte%s)\n");
564 p2(tcps_sndrexmitpack
, tcps_sndrexmitbyte
,
565 "\t\t%u data packet%s (%u byte%s) retransmitted\n");
566 p(tcps_mturesent
, "\t\t%u resend%s initiated by MTU discovery\n");
567 p2a(tcps_sndacks
, tcps_delack
,
568 "\t\t%u ack-only packet%s (%u delayed)\n");
569 p(tcps_sndurg
, "\t\t%u URG only packet%s\n");
570 p(tcps_sndprobe
, "\t\t%u window probe packet%s\n");
571 p(tcps_sndwinup
, "\t\t%u window update packet%s\n");
572 p(tcps_sndctrl
, "\t\t%u control packet%s\n");
573 p(tcps_fcholdpacket
, "\t\t%u data packet%s sent after flow control\n");
574 p(tcps_rcvtotal
, "\t%u packet%s received\n");
575 p2(tcps_rcvackpack
, tcps_rcvackbyte
, "\t\t%u ack%s (for %u byte%s)\n");
576 p(tcps_rcvdupack
, "\t\t%u duplicate ack%s\n");
577 p(tcps_rcvacktoomuch
, "\t\t%u ack%s for unsent data\n");
578 p2(tcps_rcvpack
, tcps_rcvbyte
,
579 "\t\t%u packet%s (%u byte%s) received in-sequence\n");
580 p2(tcps_rcvduppack
, tcps_rcvdupbyte
,
581 "\t\t%u completely duplicate packet%s (%u byte%s)\n");
582 p(tcps_pawsdrop
, "\t\t%u old duplicate packet%s\n");
583 p2(tcps_rcvpartduppack
, tcps_rcvpartdupbyte
,
584 "\t\t%u packet%s with some dup. data (%u byte%s duped)\n");
585 p2(tcps_rcvoopack
, tcps_rcvoobyte
,
586 "\t\t%u out-of-order packet%s (%u byte%s)\n");
587 p2(tcps_rcvpackafterwin
, tcps_rcvbyteafterwin
,
588 "\t\t%u packet%s (%u byte%s) of data after window\n");
589 p(tcps_rcvwinprobe
, "\t\t%u window probe%s\n");
590 p(tcps_rcvwinupd
, "\t\t%u window update packet%s\n");
591 p(tcps_rcvafterclose
, "\t\t%u packet%s received after close\n");
592 p(tcps_badrst
, "\t\t%u bad reset%s\n");
593 p(tcps_rcvbadsum
, "\t\t%u discarded for bad checksum%s\n");
594 p(tcps_rcvbadoff
, "\t\t%u discarded for bad header offset field%s\n");
595 p1a(tcps_rcvshort
, "\t\t%u discarded because packet too short\n");
596 p(tcps_connattempt
, "\t%u connection request%s\n");
597 p(tcps_accepts
, "\t%u connection accept%s\n");
598 p(tcps_badsyn
, "\t%u bad connection attempt%s\n");
599 p(tcps_listendrop
, "\t%u listen queue overflow%s\n");
600 p(tcps_connects
, "\t%u connection%s established (including accepts)\n");
601 p2(tcps_closed
, tcps_drops
,
602 "\t%u connection%s closed (including %u drop%s)\n");
603 p(tcps_cachedrtt
, "\t\t%u connection%s updated cached RTT on close\n");
605 "\t\t%u connection%s updated cached RTT variance on close\n");
606 p(tcps_cachedssthresh
,
607 "\t\t%u connection%s updated cached ssthresh on close\n");
608 p(tcps_conndrops
, "\t%u embryonic connection%s dropped\n");
609 p2(tcps_rttupdated
, tcps_segstimed
,
610 "\t%u segment%s updated rtt (of %u attempt%s)\n");
611 p(tcps_rexmttimeo
, "\t%u retransmit timeout%s\n");
612 p(tcps_timeoutdrop
, "\t\t%u connection%s dropped by rexmit timeout\n");
613 p(tcps_rxtfindrop
, "\t\t%u connection%s dropped after retransmitting FIN\n");
614 p(tcps_persisttimeo
, "\t%u persist timeout%s\n");
615 p(tcps_persistdrop
, "\t\t%u connection%s dropped by persist timeout\n");
616 p(tcps_keeptimeo
, "\t%u keepalive timeout%s\n");
617 p(tcps_keepprobe
, "\t\t%u keepalive probe%s sent\n");
618 p(tcps_keepdrops
, "\t\t%u connection%s dropped by keepalive\n");
619 p(tcps_predack
, "\t%u correct ACK header prediction%s\n");
620 p(tcps_preddat
, "\t%u correct data packet header prediction%s\n");
622 /* TCP_MAX_SACK indicates the header has the SACK structures */
623 p(tcps_sack_recovery_episode
, "\t%u SACK recovery episode%s\n");
625 "\t%u segment rexmit%s in SACK recovery episodes\n");
626 p(tcps_sack_rexmit_bytes
,
627 "\t%u byte rexmit%s in SACK recovery episodes\n");
628 p(tcps_sack_rcv_blocks
,
629 "\t%u SACK option%s (SACK blocks) received\n");
630 p(tcps_sack_send_blocks
, "\t%u SACK option%s (SACK blocks) sent\n");
631 p1a(tcps_sack_sboverflow
, "\t%u SACK scoreboard overflow\n");
632 #endif /* TCP_MAX_SACK */
635 bcopy(&tcpstat
, &ptcpstat
, len
);
646 * Dump UDP statistics structure.
649 udp_stats(uint32_t off
, char *name
, int af
)
651 static struct udpstat pudpstat
;
652 struct udpstat udpstat
;
653 size_t len
= sizeof udpstat
;
656 if (sysctlbyname("net.inet.udp.stats", &udpstat
, &len
, 0, 0) < 0) {
657 warn("sysctl: net.inet.udp.stats");
662 if (udp_done
!= 0 && interval
== 0)
668 printf("%s:\n", name
);
670 #define UDPDIFF(f) (udpstat.f - pudpstat.f)
671 #define p(f, m) if (UDPDIFF(f) || sflag <= 1) \
672 printf(m, UDPDIFF(f), plural(UDPDIFF(f)))
673 #define p1a(f, m) if (UDPDIFF(f) || sflag <= 1) \
674 printf(m, UDPDIFF(f))
675 p(udps_ipackets
, "\t%u datagram%s received\n");
676 p1a(udps_hdrops
, "\t%u with incomplete header\n");
677 p1a(udps_badlen
, "\t%u with bad data length field\n");
678 p1a(udps_badsum
, "\t%u with bad checksum\n");
679 p1a(udps_noport
, "\t%u dropped due to no socket\n");
681 "\t%u broadcast/multicast datagram%s dropped due to no socket\n");
682 p1a(udps_fullsock
, "\t%u dropped due to full socket buffers\n");
683 p1a(udpps_pcbhashmiss
, "\t%u not for hashed pcb\n");
684 delivered
= UDPDIFF(udps_ipackets
) -
685 UDPDIFF(udps_hdrops
) -
686 UDPDIFF(udps_badlen
) -
687 UDPDIFF(udps_badsum
) -
688 UDPDIFF(udps_noport
) -
689 UDPDIFF(udps_noportbcast
) -
690 UDPDIFF(udps_fullsock
);
691 if (delivered
|| sflag
<= 1)
692 printf("\t%u delivered\n", delivered
);
693 p(udps_opackets
, "\t%u datagram%s output\n");
696 bcopy(&udpstat
, &pudpstat
, len
);
704 * Dump IP statistics structure.
707 ip_stats(uint32_t off
, char *name
, int af
)
709 static struct ipstat pipstat
;
710 struct ipstat ipstat
;
711 size_t len
= sizeof ipstat
;
713 if (sysctlbyname("net.inet.ip.stats", &ipstat
, &len
, 0, 0) < 0) {
714 warn("sysctl: net.inet.ip.stats");
718 printf("%s:\n", name
);
720 #define IPDIFF(f) (ipstat.f - pipstat.f)
721 #define p(f, m) if (IPDIFF(f) || sflag <= 1) \
722 printf(m, IPDIFF(f), plural(IPDIFF(f)))
723 #define p1a(f, m) if (IPDIFF(f) || sflag <= 1) \
726 p(ips_total
, "\t%u total packet%s received\n");
727 p(ips_badsum
, "\t%u bad header checksum%s\n");
728 p1a(ips_toosmall
, "\t%u with size smaller than minimum\n");
729 p1a(ips_tooshort
, "\t%u with data size < data length\n");
730 p1a(ips_toolong
, "\t%u with ip length > max ip packet size\n");
731 p1a(ips_badhlen
, "\t%u with header length < data size\n");
732 p1a(ips_badlen
, "\t%u with data length < header length\n");
733 p1a(ips_badoptions
, "\t%u with bad options\n");
734 p1a(ips_badvers
, "\t%u with incorrect version number\n");
735 p(ips_fragments
, "\t%u fragment%s received\n");
736 p(ips_fragdropped
, "\t%u fragment%s dropped (dup or out of space)\n");
737 p(ips_fragtimeout
, "\t%u fragment%s dropped after timeout\n");
738 p(ips_reassembled
, "\t%u packet%s reassembled ok\n");
739 p(ips_delivered
, "\t%u packet%s for this host\n");
740 p(ips_noproto
, "\t%u packet%s for unknown/unsupported protocol\n");
741 p(ips_forward
, "\t%u packet%s forwarded");
742 p(ips_fastforward
, " (%u packet%s fast forwarded)");
743 if (IPDIFF(ips_forward
) || sflag
<= 1)
745 p(ips_cantforward
, "\t%u packet%s not forwardable\n");
747 "\t%u packet%s received for unknown multicast group\n");
748 p(ips_redirectsent
, "\t%u redirect%s sent\n");
749 p(ips_localout
, "\t%u packet%s sent from this host\n");
750 p(ips_rawout
, "\t%u packet%s sent with fabricated ip header\n");
752 "\t%u output packet%s dropped due to no bufs, etc.\n");
753 p(ips_noroute
, "\t%u output packet%s discarded due to no route\n");
754 p(ips_fragmented
, "\t%u output datagram%s fragmented\n");
755 p(ips_ofragments
, "\t%u fragment%s created\n");
756 p(ips_cantfrag
, "\t%u datagram%s that can't be fragmented\n");
757 p(ips_nogif
, "\t%u tunneling packet%s that can't find gif\n");
758 p(ips_badaddr
, "\t%u datagram%s with bad address in header\n");
759 p(ips_pktdropcntrl
, "\t%u packet%s dropped due to no bufs for control data\n");
762 bcopy(&ipstat
, &pipstat
, len
);
769 static char *icmpnames
[] = {
773 "destination unreachable",
779 "router advertisement",
780 "router solicitation",
785 "information request",
786 "information request reply",
787 "address mask request",
788 "address mask reply",
792 * Dump ICMP statistics.
795 icmp_stats(uint32_t off
, char *name
, int af
)
797 static struct icmpstat picmpstat
;
798 struct icmpstat icmpstat
;
800 int mib
[4]; /* CTL_NET + PF_INET + IPPROTO_ICMP + req */
805 mib
[2] = IPPROTO_ICMP
;
806 mib
[3] = ICMPCTL_STATS
;
808 len
= sizeof icmpstat
;
809 memset(&icmpstat
, 0, len
);
810 if (sysctl(mib
, 4, &icmpstat
, &len
, (void *)0, 0) < 0)
811 return; /* XXX should complain, but not traditional */
813 printf("%s:\n", name
);
815 #define ICMPDIFF(f) (icmpstat.f - picmpstat.f)
816 #define p(f, m) if (ICMPDIFF(f) || sflag <= 1) \
817 printf(m, ICMPDIFF(f), plural(ICMPDIFF(f)))
818 #define p1a(f, m) if (ICMPDIFF(f) || sflag <= 1) \
819 printf(m, ICMPDIFF(f))
821 p(icps_error
, "\t%u call%s to icmp_error\n");
823 "\t%u error%s not generated 'cuz old message was icmp\n");
824 for (first
= 1, i
= 0; i
< ICMP_MAXTYPE
+ 1; i
++)
825 if (ICMPDIFF(icps_outhist
[i
]) != 0) {
827 printf("\tOutput histogram:\n");
830 printf("\t\t%s: %u\n", icmpnames
[i
],
831 ICMPDIFF(icps_outhist
[i
]));
833 p(icps_badcode
, "\t%u message%s with bad code fields\n");
834 p(icps_tooshort
, "\t%u message%s < minimum length\n");
835 p(icps_checksum
, "\t%u bad checksum%s\n");
836 p(icps_badlen
, "\t%u message%s with bad length\n");
837 p1a(icps_bmcastecho
, "\t%u multicast echo requests ignored\n");
838 p1a(icps_bmcasttstamp
, "\t%u multicast timestamp requests ignored\n");
839 for (first
= 1, i
= 0; i
< ICMP_MAXTYPE
+ 1; i
++)
840 if (ICMPDIFF(icps_inhist
[i
]) != 0) {
842 printf("\tInput histogram:\n");
845 printf("\t\t%s: %u\n", icmpnames
[i
],
846 ICMPDIFF(icps_inhist
[i
]));
848 p(icps_reflect
, "\t%u message response%s generated\n");
853 mib
[3] = ICMPCTL_MASKREPL
;
855 if (sysctl(mib
, 4, &i
, &len
, (void *)0, 0) < 0)
857 printf("\tICMP address mask responses are %sabled\n",
861 bcopy(&icmpstat
, &picmpstat
, sizeof (icmpstat
));
865 * Dump IGMP statistics structure.
868 igmp_stats(uint32_t off
, char *name
, int af
)
870 static struct igmpstat_v3 pigmpstat
;
871 struct igmpstat_v3 igmpstat
;
872 size_t len
= sizeof igmpstat
;
874 if (sysctlbyname("net.inet.igmp.v3stats", &igmpstat
, &len
, 0, 0) < 0) {
875 warn("sysctl: net.inet.igmp.v3stats");
879 if (igmpstat
.igps_version
!= IGPS_VERSION_3
) {
880 warnx("%s: version mismatch (%d != %d)", __func__
,
881 igmpstat
.igps_version
, IGPS_VERSION_3
);
883 if (igmpstat
.igps_len
!= IGPS_VERSION3_LEN
) {
884 warnx("%s: size mismatch (%d != %d)", __func__
,
885 igmpstat
.igps_len
, IGPS_VERSION3_LEN
);
888 printf("%s:\n", name
);
890 #define IGMPDIFF(f) ((uintmax_t)(igmpstat.f - pigmpstat.f))
891 #define p64(f, m) if (IGMPDIFF(f) || sflag <= 1) \
892 printf(m, IGMPDIFF(f), plural(IGMPDIFF(f)))
893 #define py64(f, m) if (IGMPDIFF(f) || sflag <= 1) \
894 printf(m, IGMPDIFF(f), IGMPDIFF(f) != 1 ? "ies" : "y")
896 p64(igps_rcv_total
, "\t%ju message%s received\n");
897 p64(igps_rcv_tooshort
, "\t%ju message%s received with too few bytes\n");
898 p64(igps_rcv_badttl
, "\t%ju message%s received with wrong TTL\n");
899 p64(igps_rcv_badsum
, "\t%ju message%s received with bad checksum\n");
900 py64(igps_rcv_v1v2_queries
, "\t%ju V1/V2 membership quer%s received\n");
901 py64(igps_rcv_v3_queries
, "\t%ju V3 membership quer%s received\n");
902 py64(igps_rcv_badqueries
,
903 "\t%ju membership quer%s received with invalid field(s)\n");
904 py64(igps_rcv_gen_queries
, "\t%ju general quer%s received\n");
905 py64(igps_rcv_group_queries
, "\t%ju group quer%s received\n");
906 py64(igps_rcv_gsr_queries
, "\t%ju group-source quer%s received\n");
907 py64(igps_drop_gsr_queries
, "\t%ju group-source quer%s dropped\n");
908 p64(igps_rcv_reports
, "\t%ju membership report%s received\n");
909 p64(igps_rcv_badreports
,
910 "\t%ju membership report%s received with invalid field(s)\n");
911 p64(igps_rcv_ourreports
,
912 "\t%ju membership report%s received for groups to which we belong\n");
913 p64(igps_rcv_nora
, "\t%ju V3 report%s received without Router Alert\n");
914 p64(igps_snd_reports
, "\t%ju membership report%s sent\n");
917 bcopy(&igmpstat
, &pigmpstat
, len
);
925 * Pretty print an Internet address (net address + port).
928 inetprint(struct in_addr
*in
, int port
, char *proto
, int numeric_port
)
930 struct servent
*sp
= 0;
935 snprintf(line
, sizeof(line
), "%s.", inetname(in
));
937 snprintf(line
, sizeof(line
), "%.*s.", (Aflag
&& !numeric_port
) ? 12 : 16, inetname(in
));
938 cp
= index(line
, '\0');
939 if (!numeric_port
&& port
)
940 #ifdef _SERVICE_CACHE_
941 sp
= _serv_cache_getservbyport(port
, proto
);
943 sp
= getservbyport((int)port
, proto
);
946 snprintf(cp
, sizeof(line
) - (cp
- line
), "%.15s ", sp
? sp
->s_name
: "*");
948 snprintf(cp
, sizeof(line
) - (cp
- line
), "%d ", ntohs((u_short
)port
));
949 width
= (Aflag
&& !Wflag
) ? 18 : 22;
951 printf("%-*s ", width
, line
);
953 printf("%-*.*s ", width
, width
, line
);
957 * Construct an Internet address representation.
958 * If the nflag has been supplied, give
959 * numeric value, otherwise try for symbolic name.
962 inetname(struct in_addr
*inp
)
965 static char line
[MAXHOSTNAMELEN
];
970 if (!nflag
&& inp
->s_addr
!= INADDR_ANY
) {
971 int net
= inet_netof(*inp
);
972 int lna
= inet_lnaof(*inp
);
974 if (lna
== INADDR_ANY
) {
975 np
= getnetbyaddr(net
, AF_INET
);
980 hp
= gethostbyaddr((char *)inp
, sizeof (*inp
), AF_INET
);
983 //### trimdomain(cp, strlen(cp));
987 if (inp
->s_addr
== INADDR_ANY
)
988 strlcpy(line
, "*", sizeof(line
));
990 strncpy(line
, cp
, sizeof(line
) - 1);
991 line
[sizeof(line
) - 1] = '\0';
993 inp
->s_addr
= ntohl(inp
->s_addr
);
994 #define C(x) ((u_int)((x) & 0xff))
995 snprintf(line
, sizeof(line
), "%u.%u.%u.%u", C(inp
->s_addr
>> 24),
996 C(inp
->s_addr
>> 16), C(inp
->s_addr
>> 8), C(inp
->s_addr
));