Index: stable/4/usr.bin/netstat/if.c =================================================================== --- stable/4/usr.bin/netstat/if.c (revision 123055) +++ stable/4/usr.bin/netstat/if.c (revision 123056) @@ -1,684 +1,696 @@ /* * Copyright (c) 1983, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint /* static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95"; */ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef NS #include #include #endif #ifdef ISO #include #include #endif #include #include #include #include #include #include #include "netstat.h" #define YES 1 #define NO 0 static void sidewaysintpr (u_int, u_long); static void catchalarm (int); #ifdef INET6 char *netname6 (struct sockaddr_in6 *, struct in6_addr *); static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */ static int bdg_done; #endif /* print bridge statistics */ void bdg_stats(u_long dummy __unused, char *name, int af __unused) { int i; size_t slen ; struct bdg_stats s ; int mib[4] ; slen = sizeof(s); mib[0] = CTL_NET ; mib[1] = PF_LINK ; mib[2] = IFT_ETHER ; mib[3] = PF_BDG ; if (sysctl(mib,4, &s,&slen,NULL,0)==-1) return ; /* no bridging */ #ifdef INET6 if (bdg_done != 0) return; else bdg_done = 1; #endif printf("-- Bridging statistics (%s) --\n", name) ; printf( "Name In Out Forward Drop Bcast Mcast Local Unknown\n"); for (i = 0 ; i < 16 ; i++) { if (s.s[i].name[0]) printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n", s.s[i].name, s.s[i].p_in[(int)BDG_IN], s.s[i].p_in[(int)BDG_OUT], s.s[i].p_in[(int)BDG_FORWARD], s.s[i].p_in[(int)BDG_DROP], s.s[i].p_in[(int)BDG_BCAST], s.s[i].p_in[(int)BDG_MCAST], s.s[i].p_in[(int)BDG_LOCAL], s.s[i].p_in[(int)BDG_UNKNOWN] ); } } /* * Display a formatted value, or a '-' in the same space. */ static void show_stat(const char *fmt, int width, u_long value, short showvalue) { char newfmt[32]; /* Construct the format string */ if (showvalue) { sprintf(newfmt, "%%%d%s", width, fmt); printf(newfmt, value); } else { sprintf(newfmt, "%%%ds", width); printf(newfmt, "-"); } } /* * Print a description of the network interfaces. */ void intpr(int interval, u_long ifnetaddr, void (*pfunc)(char *)) { struct ifnet ifnet; struct ifnethead ifnethead; union { struct ifaddr ifa; struct in_ifaddr in; #ifdef INET6 struct in6_ifaddr in6; #endif struct ipx_ifaddr ipx; #ifdef NS struct ns_ifaddr ns; #endif #ifdef ISO struct iso_ifaddr iso; #endif } ifaddr; u_long ifaddraddr; u_long ifaddrfound; u_long ifnetfound; u_long opackets; u_long ipackets; u_long obytes; u_long ibytes; u_long oerrors; u_long ierrors; u_long collisions; short timer; int drops; struct sockaddr *sa = NULL; char name[32], tname[16]; short network_layer; short link_layer; if (ifnetaddr == 0) { printf("ifnet: symbol not defined\n"); return; } if (interval) { sidewaysintpr((unsigned)interval, ifnetaddr); return; } if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead)) return; ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead); if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) return; if (!pfunc) { - printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s", - "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs"); + if (Wflag) + printf("%-7.7s", "Name"); + else + printf("%-5.5s", "Name"); + printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s", + "Mtu", "Network", "Address", "Ipkts", "Ierrs"); if (bflag) printf(" %10.10s","Ibytes"); printf(" %8.8s %5.5s", "Opkts", "Oerrs"); if (bflag) printf(" %10.10s","Obytes"); printf(" %5s", "Coll"); if (tflag) printf(" %s", "Time"); if (dflag) printf(" %s", "Drop"); putchar('\n'); } ifaddraddr = 0; while (ifnetaddr || ifaddraddr) { struct sockaddr_in *sin; #ifdef INET6 struct sockaddr_in6 *sin6; #endif register char *cp; int n, m; network_layer = 0; link_layer = 0; if (ifaddraddr == 0) { ifnetfound = ifnetaddr; if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) || kread((u_long)ifnet.if_name, tname, 16)) return; tname[15] = '\0'; ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link); snprintf(name, 32, "%s%d", tname, ifnet.if_unit); if (interface != 0 && (strcmp(name, interface) != 0)) continue; cp = index(name, '\0'); if (pfunc) { (*pfunc)(name); continue; } if ((ifnet.if_flags&IFF_UP) == 0) *cp++ = '*'; *cp = '\0'; ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead); } ifaddrfound = ifaddraddr; /* * Get the interface stats. These may get * overriden below on a per-interface basis. */ opackets = ifnet.if_opackets; ipackets = ifnet.if_ipackets; obytes = ifnet.if_obytes; ibytes = ifnet.if_ibytes; oerrors = ifnet.if_oerrors; ierrors = ifnet.if_ierrors; collisions = ifnet.if_collisions; timer = ifnet.if_timer; drops = ifnet.if_snd.ifq_drops; if (ifaddraddr == 0) { - printf("%-5.5s %-5lu ", name, ifnet.if_mtu); + if (Wflag) + printf("%-7.7s", name); + else + printf("%-5.5s", name); + printf(" %5lu ", ifnet.if_mtu); printf("%-13.13s ", "none"); printf("%-15.15s ", "none"); } else { if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) { ifaddraddr = 0; continue; } #define CP(x) ((char *)(x)) cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + CP(&ifaddr); sa = (struct sockaddr *)cp; if (af != AF_UNSPEC && sa->sa_family != af) { ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link); continue; } - printf("%-5.5s %-5lu ", name, ifnet.if_mtu); + if (Wflag) + printf("%-7.7s", name); + else + printf("%-5.5s", name); + printf(" %5lu ", ifnet.if_mtu); switch (sa->sa_family) { case AF_UNSPEC: printf("%-13.13s ", "none"); printf("%-15.15s ", "none"); break; case AF_INET: sin = (struct sockaddr_in *)sa; #ifdef notdef /* can't use inet_makeaddr because kernel * keeps nets unshifted. */ in = inet_makeaddr(ifaddr.in.ia_subnet, INADDR_ANY); printf("%-13.13s ", netname(in.s_addr, ifaddr.in.ia_subnetmask)); #else printf("%-13.13s ", netname(htonl(ifaddr.in.ia_subnet), ifaddr.in.ia_subnetmask)); #endif printf("%-15.15s ", routename(sin->sin_addr.s_addr)); network_layer = 1; break; #ifdef INET6 case AF_INET6: sin6 = (struct sockaddr_in6 *)sa; printf("%-11.11s ", netname6(&ifaddr.in6.ia_addr, &ifaddr.in6.ia_prefixmask.sin6_addr)); printf("%-17.17s ", (char *)inet_ntop(AF_INET6, &sin6->sin6_addr, ntop_buf, sizeof(ntop_buf))); network_layer = 1; break; #endif /*INET6*/ case AF_IPX: { struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa; u_long net; char netnum[10]; *(union ipx_net *) &net = sipx->sipx_addr.x_net; sprintf(netnum, "%lx", (u_long)ntohl(net)); printf("ipx:%-8s ", netnum); /* printf("ipx:%-8s ", netname(net, 0L)); */ printf("%-15s ", ipx_phost((struct sockaddr *)sipx)); } break; case AF_APPLETALK: printf("atalk:%-12.12s ",atalk_print(sa,0x10) ); printf("%-9.9s ",atalk_print(sa,0x0b) ); break; #ifdef NS case AF_NS: { struct sockaddr_ns *sns = (struct sockaddr_ns *)sa; u_long net; char netnum[10]; *(union ns_net *) &net = sns->sns_addr.x_net; sprintf(netnum, "%lxH", ntohl(net)); upHex(netnum); printf("ns:%-8s ", netnum); printf("%-15s ", ns_phost((struct sockaddr *)sns)); } break; #endif case AF_LINK: { struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; char linknum[10]; cp = (char *)LLADDR(sdl); n = sdl->sdl_alen; sprintf(linknum, "", sdl->sdl_index); m = printf("%-11.11s ", linknum); } goto hexprint; default: m = printf("(%d)", sa->sa_family); for (cp = sa->sa_len + (char *)sa; --cp > sa->sa_data && (*cp == 0);) {} n = cp - sa->sa_data + 1; cp = sa->sa_data; hexprint: while (--n >= 0) m += printf("%02x%c", *cp++ & 0xff, n > 0 ? ':' : ' '); m = 30 - m; while (m-- > 0) putchar(' '); link_layer = 1; break; } /* * Fixup the statistics for interfaces that * update stats for their network addresses */ if (network_layer) { opackets = ifaddr.in.ia_ifa.if_opackets; ipackets = ifaddr.in.ia_ifa.if_ipackets; obytes = ifaddr.in.ia_ifa.if_obytes; ibytes = ifaddr.in.ia_ifa.if_ibytes; } ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link); } show_stat("lu", 8, ipackets, link_layer|network_layer); printf(" "); show_stat("lu", 5, ierrors, link_layer); printf(" "); if (bflag) { show_stat("lu", 10, ibytes, link_layer|network_layer); printf(" "); } show_stat("lu", 8, opackets, link_layer|network_layer); printf(" "); show_stat("lu", 5, oerrors, link_layer); printf(" "); if (bflag) { show_stat("lu", 10, obytes, link_layer|network_layer); printf(" "); } show_stat("lu", 5, collisions, link_layer); if (tflag) { printf(" "); show_stat("d", 3, timer, link_layer); } if (dflag) { printf(" "); show_stat("d", 3, drops, link_layer); } putchar('\n'); if (aflag && ifaddrfound) { /* * Print family's multicast addresses */ u_long multiaddr; struct ifmultiaddr ifma; union { struct sockaddr sa; struct sockaddr_in in; #ifdef INET6 struct sockaddr_in6 in6; #endif /* INET6 */ struct sockaddr_dl dl; } msa; const char *fmt; for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first; multiaddr; multiaddr = (u_long)ifma.ifma_link.le_next) { if (kread(multiaddr, (char *)&ifma, sizeof ifma)) break; if (kread((u_long)ifma.ifma_addr, (char *)&msa, sizeof msa)) break; if (msa.sa.sa_family != sa->sa_family) continue; fmt = 0; switch (msa.sa.sa_family) { case AF_INET: fmt = routename(msa.in.sin_addr.s_addr); break; #ifdef INET6 case AF_INET6: printf("%23s %-19.19s(refs: %d)\n", "", inet_ntop(AF_INET6, &msa.in6.sin6_addr, ntop_buf, sizeof(ntop_buf)), ifma.ifma_refcount); break; #endif /* INET6 */ case AF_LINK: switch (msa.dl.sdl_type) { case IFT_ETHER: case IFT_FDDI: fmt = ether_ntoa( (struct ether_addr *) LLADDR(&msa.dl)); break; } break; } if (fmt) printf("%23s %s\n", "", fmt); } } } } struct iftot { SLIST_ENTRY(iftot) chain; char ift_name[16]; /* interface name */ u_long ift_ip; /* input packets */ u_long ift_ie; /* input errors */ u_long ift_op; /* output packets */ u_long ift_oe; /* output errors */ u_long ift_co; /* collisions */ u_int ift_dr; /* drops */ u_long ift_ib; /* input bytes */ u_long ift_ob; /* output bytes */ }; u_char signalled; /* set if alarm goes off "early" */ /* * Print a running summary of interface statistics. * Repeat display every interval seconds, showing statistics * collected over that interval. Assumes that interval is non-zero. * First line printed at top of screen is always cumulative. * XXX - should be rewritten to use ifmib(4). */ static void sidewaysintpr(unsigned interval, u_long off) { struct ifnet ifnet; u_long firstifnet; struct ifnethead ifnethead; struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting; register int line; int oldmask, first; u_long interesting_off; if (kread(off, (char *)&ifnethead, sizeof ifnethead)) return; firstifnet = (u_long)TAILQ_FIRST(&ifnethead); if ((iftot = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); exit(1); } memset(iftot, 0, sizeof(struct iftot)); interesting = NULL; interesting_off = 0; for (off = firstifnet, ip = iftot; off;) { char name[16], tname[16]; if (kread(off, (char *)&ifnet, sizeof ifnet)) break; if (kread((u_long)ifnet.if_name, tname, 16)) break; tname[15] = '\0'; snprintf(name, 16, "%s%d", tname, ifnet.if_unit); if (interface && strcmp(name, interface) == 0) { interesting = ip; interesting_off = off; } snprintf(ip->ift_name, 16, "(%s)", name);; if ((ipn = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); exit(1); } memset(ipn, 0, sizeof(struct iftot)); SLIST_NEXT(ip, chain) = ipn; ip = ipn; off = (u_long)TAILQ_NEXT(&ifnet, if_link); } if ((total = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); exit(1); } memset(total, 0, sizeof(struct iftot)); if ((sum = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); exit(1); } memset(sum, 0, sizeof(struct iftot)); (void)signal(SIGALRM, catchalarm); signalled = NO; (void)alarm(interval); first = 1; banner: printf("%17s %14s %16s", "input", interesting ? interesting->ift_name : "(Total)", "output"); putchar('\n'); printf("%10s %5s %10s %10s %5s %10s %5s", "packets", "errs", "bytes", "packets", "errs", "bytes", "colls"); if (dflag) printf(" %5.5s", "drops"); putchar('\n'); fflush(stdout); line = 0; loop: if (interesting != NULL) { ip = interesting; if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) { printf("???\n"); exit(1); }; if (!first) { printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", ifnet.if_ipackets - ip->ift_ip, ifnet.if_ierrors - ip->ift_ie, ifnet.if_ibytes - ip->ift_ib, ifnet.if_opackets - ip->ift_op, ifnet.if_oerrors - ip->ift_oe, ifnet.if_obytes - ip->ift_ob, ifnet.if_collisions - ip->ift_co); if (dflag) printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr); } ip->ift_ip = ifnet.if_ipackets; ip->ift_ie = ifnet.if_ierrors; ip->ift_ib = ifnet.if_ibytes; ip->ift_op = ifnet.if_opackets; ip->ift_oe = ifnet.if_oerrors; ip->ift_ob = ifnet.if_obytes; ip->ift_co = ifnet.if_collisions; ip->ift_dr = ifnet.if_snd.ifq_drops; } else { sum->ift_ip = 0; sum->ift_ie = 0; sum->ift_ib = 0; sum->ift_op = 0; sum->ift_oe = 0; sum->ift_ob = 0; sum->ift_co = 0; sum->ift_dr = 0; for (off = firstifnet, ip = iftot; off && SLIST_NEXT(ip, chain) != NULL; ip = SLIST_NEXT(ip, chain)) { if (kread(off, (char *)&ifnet, sizeof ifnet)) { off = 0; continue; } sum->ift_ip += ifnet.if_ipackets; sum->ift_ie += ifnet.if_ierrors; sum->ift_ib += ifnet.if_ibytes; sum->ift_op += ifnet.if_opackets; sum->ift_oe += ifnet.if_oerrors; sum->ift_ob += ifnet.if_obytes; sum->ift_co += ifnet.if_collisions; sum->ift_dr += ifnet.if_snd.ifq_drops; off = (u_long)TAILQ_NEXT(&ifnet, if_link); } if (!first) { printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", sum->ift_ip - total->ift_ip, sum->ift_ie - total->ift_ie, sum->ift_ib - total->ift_ib, sum->ift_op - total->ift_op, sum->ift_oe - total->ift_oe, sum->ift_ob - total->ift_ob, sum->ift_co - total->ift_co); if (dflag) printf(" %5u", sum->ift_dr - total->ift_dr); } *total = *sum; } if (!first) putchar('\n'); fflush(stdout); oldmask = sigblock(sigmask(SIGALRM)); if (! signalled) { sigpause(0); } sigsetmask(oldmask); signalled = NO; (void)alarm(interval); line++; first = 0; if (line == 21) goto banner; else goto loop; /*NOTREACHED*/ } /* * Called if an interval expires before sidewaysintpr has completed a loop. * Sets a flag to not wait for the alarm. */ static void catchalarm(int signo __unused) { signalled = YES; } Index: stable/4/usr.bin/netstat/netstat.1 =================================================================== --- stable/4/usr.bin/netstat/netstat.1 (revision 123055) +++ stable/4/usr.bin/netstat/netstat.1 (revision 123056) @@ -1,441 +1,447 @@ .\" Copyright (c) 1983, 1990, 1992, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)netstat.1 8.8 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" .Dd September 7, 2001 .Dt NETSTAT 1 .Os .Sh NAME .Nm netstat .Nd show network status .Sh DESCRIPTION The .Nm command symbolically displays the contents of various network-related data structures. There are a number of output formats, depending on the options for the information presented. .Bl -tag -width indent .It Xo .Bk -words .Nm .Op Fl AaLlnSW .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Display a list of active sockets (protocol control blocks) for each network protocol, for a particular .Ar protocol_family , or for a single .Ar protocol . If .Fl A is also present, show the address of a protocol control block (PCB) associated with a socket; used for debugging. If .Fl a is also present, show the state of all sockets; normally sockets used by server processes are not shown. If .Fl L is also present, show the size of the various listen queues. The first count shows the number of unaccepted connections, the second count shows the amount of unaccepted incomplete connections, and the third count is the maximum number of queued connections. If .Fl S is also present, show network addresses as numbers (as with .Fl n ) but show ports symbolically. .It Xo .Bk -words .Nm .Fl i | I Ar interface .Op Fl abdnt .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Show the state of all network interfaces or a single .Ar interface which have been auto-configured (interfaces statically configured into a system, but not located at boot time are not shown). An asterisk .Pq Dq Li * after an interface name indicates that the interface is .Dq down . If .Fl a is also present, multicast addresses currently in use are shown for each Ethernet interface and for each IP interface address. Multicast addresses are shown on separate lines following the interface address with which they are associated. If .Fl b is also present, show the number of bytes in and out. If .Fl d is also present, show the number of dropped packets. If .Fl t is also present, show the contents of watchdog timers. +If +.Fl W +is also present, print interface names using a wider field size. .It Xo .Bk -words .Nm .Fl w Ar wait .Op Fl I Ar interface .Op Fl d .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc At intervals of .Ar wait seconds, display the information regarding packet traffic on all configured network interfaces or a single .Ar interface . If .Fl d is also present, show the number of dropped packets. .It Xo .Bk -words .Nm .Fl s Op Fl s .Op Fl z .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Display system-wide statistics for each network protocol, for a particular .Ar protocol_family , or for a single .Ar protocol . If .Fl s is repeated, counters with a value of zero are suppressed. If .Fl z is also present, reset statistic counters after displaying them. .It Xo .Bk -words .Nm .Fl i | I Ar interface Fl s .Op Fl f Ar protocol_family | Fl p Ar protocol .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Display per-interface statistics for each network protocol, for a particular .Ar protocol_family , or for a single .Ar protocol . .It Xo .Bk -words .Nm .Fl m .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Show statistics recorded by the memory management routines .Pq Xr mbuf 9 . The network manages a private pool of memory buffers. .It Xo .Bk -words .Nm .Fl r .Op Fl AalnW .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Display the contents of all routing tables, or a routing table for a particular .Ar address_family . If .Fl A is also present, show the contents of the internal Patricia tree structures; used for debugging. If .Fl a is also present, show protocol-cloned routes (routes generated by an .Dv RTF_PRCLONING parent route); normally these routes are not shown. When .Fl W or .Fl l is also present, show the path MTU -for each route. +for each route, +and print interface +names with a wider +field size. .It Xo .Bk -words .Nm .Fl rs .Op Fl s .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Display routing statistics. If .Fl s is repeated, counters with a value of zero are suppressed. .It Xo .Bk -words .Nm .Fl g .Op Fl lW .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Show information related to multicast (group address) routing. By default, show the IP Multicast virtual-interface and routing tables. .It Xo .Bk -words .Nm .Fl gs .Op Fl s .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc Show multicast routing statistics. If .Fl s is repeated, counters with a value of zero are suppressed. .El .Pp Some options have the general meaning: .Bl -tag -width flag .It Fl f Ar address_family , Fl p Ar protocol Limit display to those records of the specified .Ar address_family or a single .Ar protocol . The following address families and protocols are recognized: .Pp .Bl -tag -width ".Cm netgraph , ng Pq Dv AF_NETGRAPH" -compact .It Em Family .Em Protocols .It Cm inet Pq Dv AF_INET .Cm bdg , divert , icmp , igmp , ip , ipsec , pim , tcp , udp .It Cm inet6 Pq Dv AF_INET6 .Cm bdg , icmp6 , ip6 , ipsec6 , rip6 , tcp , udp .It Cm pfkey Pq Dv PF_KEY .Cm pfkey .It Cm atalk Pq Dv AF_APPLETALK .Cm ddp .It Cm netgraph , ng Pq Dv AF_NETGRAPH .Cm ctrl , data .It Cm ipx Pq Dv AF_IPX .Cm ipx , spx .\".It Cm ns Pq Dv AF_NS .\".Cm idp , ns_err , spp .\".It Cm iso Pq Dv AF_ISO .\".Cm clnp , cltp , esis , tp .It Cm unix Pq Dv AF_UNIX .It Cm link Pq Dv AF_LINK .El .Pp The program will complain if .Ar protocol is unknown or if there is no statistics routine for it. .It Fl l The .Fl l option is equivalent to .Fl W . .It Fl M Extract values associated with the name list from the specified core instead of the default .Pa /dev/kmem . .It Fl N Extract the name list from the specified system instead of the default, which is the kernel image the system has booted from. .It Fl n Show network addresses and ports as numbers. Normally .Nm attempts to resolve addresses and ports, and display them symbolically. .It Fl W In certain displays, avoid truncating addresses even if this causes some fields to overflow. .El .Pp The default display, for active sockets, shows the local and remote addresses, send and receive queue sizes (in bytes), protocol, and the internal state of the protocol. Address formats are of the form .Dq host.port or .Dq network.port if a socket's address specifies a network but no specific host address. When known, the host and network addresses are displayed symbolically according to the databases .Xr hosts 5 and .Xr networks 5 , respectively. If a symbolic name for an address is unknown, or if the .Fl n option is specified, the address is printed numerically, according to the address family. For more information regarding the Internet IPv4 .Dq dot format , refer to .Xr inet 3 . Unspecified, or .Dq wildcard , addresses and ports appear as .Dq Li * . .Pp The interface display provides a table of cumulative statistics regarding packets transferred, errors, and collisions. The network addresses of the interface and the maximum transmission unit .Pq Dq mtu are also displayed. .Pp The routing table display indicates the available routes and their status. Each route consists of a destination host or network, and a gateway to use in forwarding packets. The flags field shows a collection of information about the route stored as binary choices. The individual flags are discussed in more detail in the .Xr route 8 and .Xr route 4 manual pages. The mapping between letters and flags is: .Bl -column ".Li W" ".Dv RTF_WASCLONED" .It Li 1 Ta Dv RTF_PROTO1 Ta "Protocol specific routing flag #1" .It Li 2 Ta Dv RTF_PROTO2 Ta "Protocol specific routing flag #2" .It Li 3 Ta Dv RTF_PROTO3 Ta "Protocol specific routing flag #3" .It Li B Ta Dv RTF_BLACKHOLE Ta "Just discard pkts (during updates)" .It Li b Ta Dv RTF_BROADCAST Ta "The route represents a broadcast address" .It Li C Ta Dv RTF_CLONING Ta "Generate new routes on use" .It Li c Ta Dv RTF_PRCLONING Ta "Protocol-specified generate new routes on use" .It Li D Ta Dv RTF_DYNAMIC Ta "Created dynamically (by redirect)" .It Li G Ta Dv RTF_GATEWAY Ta "Destination requires forwarding by intermediary" .It Li H Ta Dv RTF_HOST Ta "Host entry (net otherwise)" .It Li L Ta Dv RTF_LLINFO Ta "Valid protocol to link address translation" .It Li M Ta Dv RTF_MODIFIED Ta "Modified dynamically (by redirect)" .It Li R Ta Dv RTF_REJECT Ta "Host or net unreachable" .It Li S Ta Dv RTF_STATIC Ta "Manually added" .It Li U Ta Dv RTF_UP Ta "Route usable" .It Li W Ta Dv RTF_WASCLONED Ta "Route was generated as a result of cloning" .It Li X Ta Dv RTF_XRESOLVE Ta "External daemon translates proto to link address" .El .Pp Direct routes are created for each interface attached to the local host; the gateway field for such entries shows the address of the outgoing interface. The refcnt field gives the current number of active uses of the route. Connection oriented protocols normally hold on to a single route for the duration of a connection while connectionless protocols obtain a route while sending to the same destination. The use field provides a count of the number of packets sent using that route. The interface entry indicates the network interface utilized for the route. .Pp When .Nm is invoked with the .Fl w option and a .Ar wait interval argument, it displays a running count of statistics related to network interfaces. An obsolescent version of this option used a numeric parameter with no option, and is currently supported for backward compatibility. By default, this display summarizes information for all interfaces. Information for a specific interface may be displayed with the .Fl I option. .Sh SEE ALSO .Xr fstat 1 , .Xr nfsstat 1 , .Xr ps 1 , .Xr sockstat 1 , .Xr inet 4 , .Xr route 4 , .Xr unix 4 , .Xr hosts 5 , .Xr networks 5 , .Xr protocols 5 , .Xr services 5 , .Xr iostat 8 , .Xr route 8 , .Xr trpt 8 , .Xr vmstat 8 , .Xr mbuf 9 .Sh HISTORY The .Nm command appeared in .Bx 4.2 . .Pp IPv6 support was added by WIDE/KAME project. .Sh BUGS The notion of errors is ill-defined. Index: stable/4/usr.bin/netstat/route.c =================================================================== --- stable/4/usr.bin/netstat/route.c (revision 123055) +++ stable/4/usr.bin/netstat/route.c (revision 123056) @@ -1,1214 +1,1214 @@ /* * Copyright (c) 1983, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint #if 0 static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef NS #include #endif #include #include #include #include #include #include #include #include #include #include #include "netstat.h" #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) /* alignment constraint for routing socket */ #define ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) /* * Definitions for showing gateway flags. */ struct bits { u_long b_mask; char b_val; } bits[] = { { RTF_UP, 'U' }, { RTF_GATEWAY, 'G' }, { RTF_HOST, 'H' }, { RTF_REJECT, 'R' }, { RTF_DYNAMIC, 'D' }, { RTF_MODIFIED, 'M' }, { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ { RTF_CLONING, 'C' }, { RTF_XRESOLVE, 'X' }, { RTF_LLINFO, 'L' }, { RTF_STATIC, 'S' }, { RTF_PROTO1, '1' }, { RTF_PROTO2, '2' }, { RTF_WASCLONED,'W' }, { RTF_PRCLONING,'c' }, { RTF_PROTO3, '3' }, { RTF_BLACKHOLE,'B' }, { RTF_BROADCAST,'b' }, { 0 } }; typedef union { long dummy; /* Helps align structure. */ struct sockaddr u_sa; u_short u_data[128]; } sa_u; static sa_u pt_u; int do_rtent = 0; struct rtentry rtentry; struct radix_node rnode; struct radix_mask rmask; struct radix_node_head *rt_tables[AF_MAX+1]; int NewTree = 0; static struct sockaddr *kgetsa (struct sockaddr *); static void size_cols (int ef, struct radix_node *rn); static void size_cols_tree (struct radix_node *rn); static void size_cols_rtentry (struct rtentry *rt); static void p_tree (struct radix_node *); static void p_rtnode (void); static void ntreestuff (void); static void np_rtentry (struct rt_msghdr *); static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int); static const char *fmt_sockaddr (struct sockaddr *sa, struct sockaddr *mask, int flags); static void p_flags (int, char *); static const char *fmt_flags(int f); static void p_rtentry (struct rtentry *); static u_long forgemask (u_long); static void domask (char *, u_long, u_long); /* * Print routing tables. */ void routepr(u_long rtree) { struct radix_node_head *rnh, head; int i; printf("Routing tables\n"); if (Aflag == 0 && NewTree) ntreestuff(); else { if (rtree == 0) { printf("rt_tables: symbol not in namelist\n"); return; } kget(rtree, rt_tables); for (i = 0; i <= AF_MAX; i++) { if ((rnh = rt_tables[i]) == 0) continue; kget(rnh, head); if (i == AF_UNSPEC) { if (Aflag && af == 0) { printf("Netmasks:\n"); p_tree(head.rnh_treetop); } } else if (af == AF_UNSPEC || af == i) { size_cols(i, head.rnh_treetop); pr_family(i); do_rtent = 1; pr_rthdr(i); p_tree(head.rnh_treetop); } } } } /* * Print address family header before a section of the routing table. */ void pr_family(int af) { char *afname; switch (af) { case AF_INET: afname = "Internet"; break; #ifdef INET6 case AF_INET6: afname = "Internet6"; break; #endif /*INET6*/ case AF_IPX: afname = "IPX"; break; #ifdef NS case AF_NS: afname = "XNS"; break; #endif case AF_ISO: afname = "ISO"; break; case AF_APPLETALK: afname = "AppleTalk"; break; case AF_CCITT: afname = "X.25"; break; case AF_NETGRAPH: afname = "Netgraph"; break; default: afname = NULL; break; } if (afname) printf("\n%s:\n", afname); else printf("\nProtocol Family %d:\n", af); } /* column widths; each followed by one space */ #ifndef INET6 #define WID_DST_DEFAULT(af) 18 /* width of destination column */ #define WID_GW_DEFAULT(af) 18 /* width of gateway column */ -#define WID_IF_DEFAULT(af) 6 /* width of netif column */ +#define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */ #else #define WID_DST_DEFAULT(af) \ ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18) #define WID_GW_DEFAULT(af) \ ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18) -#define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : 6) +#define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 : 6)) #endif /*INET6*/ static int wid_dst; static int wid_gw; static int wid_flags; static int wid_refs; static int wid_use; static int wid_mtu; static int wid_if; static int wid_expire; static void size_cols(int ef, struct radix_node *rn) { wid_dst = WID_DST_DEFAULT(ef); wid_gw = WID_GW_DEFAULT(ef); wid_flags = 6; wid_refs = 6; wid_use = 8; wid_mtu = 6; wid_if = WID_IF_DEFAULT(ef); wid_expire = 6; if (Wflag) size_cols_tree(rn); } static void size_cols_tree(struct radix_node *rn) { again: kget(rn, rnode); if (rnode.rn_bit < 0) { if ((rnode.rn_flags & RNF_ROOT) == 0) { kget(rn, rtentry); size_cols_rtentry(&rtentry); } if ((rn = rnode.rn_dupedkey)) goto again; } else { rn = rnode.rn_right; size_cols_tree(rnode.rn_left); size_cols_tree(rn); } } static void size_cols_rtentry(struct rtentry *rt) { static struct ifnet ifnet, *lastif; struct rtentry parent; static char buffer[100]; const char *bp; struct sockaddr *sa; sa_u addr, mask; int len; /* * Don't print protocol-cloned routes unless -a. */ if (rt->rt_flags & RTF_WASCLONED && !aflag) { kget(rt->rt_parent, parent); if (parent.rt_flags & RTF_PRCLONING) return; } bzero(&addr, sizeof(addr)); if ((sa = kgetsa(rt_key(rt)))) bcopy(sa, &addr, sa->sa_len); bzero(&mask, sizeof(mask)); if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) bcopy(sa, &mask, sa->sa_len); bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags); len = strlen(bp); wid_dst = MAX(len, wid_dst); bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST); len = strlen(bp); wid_gw = MAX(len, wid_gw); bp = fmt_flags(rt->rt_flags); len = strlen(bp); wid_flags = MAX(len, wid_flags); if (addr.u_sa.sa_family == AF_INET || Wflag) { len = snprintf(buffer, sizeof(buffer), "%ld", rt->rt_refcnt); wid_refs = MAX(len, wid_refs); len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_use); wid_use = MAX(len, wid_use); if (Wflag && rt->rt_rmx.rmx_mtu != 0) { len = snprintf(buffer, sizeof(buffer), "%lu", rt->rt_rmx.rmx_mtu); wid_mtu = MAX(len, wid_mtu); } } if (rt->rt_ifp) { if (rt->rt_ifp != lastif) { len = snprintf(buffer, sizeof(buffer), "%d", ifnet.if_unit); kget(rt->rt_ifp, ifnet); kread((u_long)ifnet.if_name, buffer, sizeof(buffer)); lastif = rt->rt_ifp; len += strlen(buffer); wid_if = MAX(len, wid_if); } if (rt->rt_rmx.rmx_expire) { time_t expire_time; if ((expire_time = rt->rt_rmx.rmx_expire - time(NULL)) > 0) { snprintf(buffer, sizeof(buffer), "%d", (int)expire_time); wid_expire = MAX(len, wid_expire); } } } } /* * Print header for routing table columns. */ void pr_rthdr(int af) { if (Aflag) printf("%-8.8s ","Address"); if (af == AF_INET || Wflag) { if (Wflag) { printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*.*s %*s\n", wid_dst, wid_dst, "Destination", wid_gw, wid_gw, "Gateway", wid_flags, wid_flags, "Flags", wid_refs, wid_refs, "Refs", wid_use, wid_use, "Use", wid_mtu, wid_mtu, "Mtu", wid_if, wid_if, "Netif", wid_expire, "Expire"); } else { printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n", wid_dst, wid_dst, "Destination", wid_gw, wid_gw, "Gateway", wid_flags, wid_flags, "Flags", wid_refs, wid_refs, "Refs", wid_use, wid_use, "Use", wid_if, wid_if, "Netif", wid_expire, "Expire"); } } else { printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n", wid_dst, wid_dst, "Destination", wid_gw, wid_gw, "Gateway", wid_flags, wid_flags, "Flags", wid_if, wid_if, "Netif", wid_expire, "Expire"); } } static struct sockaddr * kgetsa(struct sockaddr *dst) { kget(dst, pt_u.u_sa); if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); return (&pt_u.u_sa); } static void p_tree(struct radix_node *rn) { again: kget(rn, rnode); if (rnode.rn_bit < 0) { if (Aflag) printf("%-8.8lx ", (u_long)rn); if (rnode.rn_flags & RNF_ROOT) { if (Aflag) printf("(root node)%s", rnode.rn_dupedkey ? " =>\n" : "\n"); } else if (do_rtent) { kget(rn, rtentry); p_rtentry(&rtentry); if (Aflag) p_rtnode(); } else { p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), NULL, 0, 44); putchar('\n'); } if ((rn = rnode.rn_dupedkey)) goto again; } else { if (Aflag && do_rtent) { printf("%-8.8lx ", (u_long)rn); p_rtnode(); } rn = rnode.rn_right; p_tree(rnode.rn_left); p_tree(rn); } } char nbuf[20]; static void p_rtnode(void) { struct radix_mask *rm = rnode.rn_mklist; if (rnode.rn_bit < 0) { if (rnode.rn_mask) { printf("\t mask "); p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), NULL, 0, -1); } else if (rm == 0) return; } else { sprintf(nbuf, "(%d)", rnode.rn_bit); printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right); } while (rm) { kget(rm, rmask); sprintf(nbuf, " %d refs, ", rmask.rm_refs); printf(" mk = %8.8lx {(%d),%s", (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " "); if (rmask.rm_flags & RNF_NORMAL) { struct radix_node rnode_aux; printf(" , "); kget(rmask.rm_leaf, rnode_aux); p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask), NULL, 0, -1); } else p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), NULL, 0, -1); putchar('}'); if ((rm = rmask.rm_mklist)) printf(" ->"); } putchar('\n'); } static void ntreestuff(void) { size_t needed; int mib[6]; char *buf, *next, *lim; register struct rt_msghdr *rtm; mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; mib[3] = 0; mib[4] = NET_RT_DUMP; mib[5] = 0; if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { err(1, "sysctl: net.route.0.0.dump estimate"); } if ((buf = malloc(needed)) == 0) { err(2, "malloc(%lu)", (unsigned long)needed); } if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { err(1, "sysctl: net.route.0.0.dump"); } lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; np_rtentry(rtm); } } static void np_rtentry(struct rt_msghdr *rtm) { register struct sockaddr *sa = (struct sockaddr *)(rtm + 1); #ifdef notdef static int masks_done, banner_printed; #endif static int old_af; int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; #ifdef notdef /* for the moment, netmasks are skipped over */ if (!banner_printed) { printf("Netmasks:\n"); banner_printed = 1; } if (masks_done == 0) { if (rtm->rtm_addrs != RTA_DST ) { masks_done = 1; af = sa->sa_family; } } else #endif af = sa->sa_family; if (af != old_af) { pr_family(af); old_af = af; } if (rtm->rtm_addrs == RTA_DST) p_sockaddr(sa, NULL, 0, 36); else { p_sockaddr(sa, NULL, rtm->rtm_flags, 16); sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa); p_sockaddr(sa, NULL, 0, 18); } p_flags(rtm->rtm_flags & interesting, "%-6.6s "); putchar('\n'); } static void p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width) { const char *cp; cp = fmt_sockaddr(sa, mask, flags); if (width < 0 ) printf("%s ", cp); else { if (numeric_addr) printf("%-*s ", width, cp); else printf("%-*.*s ", width, width, cp); } } static const char * fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags) { static char workbuf[128]; char *cplim; char *cp = workbuf; switch(sa->sa_family) { case AF_INET: { register struct sockaddr_in *sin = (struct sockaddr_in *)sa; if ((sin->sin_addr.s_addr == INADDR_ANY) && mask && ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) ==0L) cp = "default" ; else if (flags & RTF_HOST) cp = routename(sin->sin_addr.s_addr); else if (mask) cp = netname(sin->sin_addr.s_addr, ntohl(((struct sockaddr_in *)mask) ->sin_addr.s_addr)); else cp = netname(sin->sin_addr.s_addr, 0L); break; } #ifdef INET6 case AF_INET6: { struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; struct in6_addr *in6 = &sa6->sin6_addr; /* * XXX: This is a special workaround for KAME kernels. * sin6_scope_id field of SA should be set in the future. */ if (IN6_IS_ADDR_LINKLOCAL(in6) || IN6_IS_ADDR_MC_LINKLOCAL(in6)) { /* XXX: override is ok? */ sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]); *(u_short *)&in6->s6_addr[2] = 0; } if (flags & RTF_HOST) cp = routename6(sa6); else if (mask) cp = netname6(sa6, &((struct sockaddr_in6 *)mask)->sin6_addr); else { cp = netname6(sa6, NULL); } break; } #endif /*INET6*/ case AF_IPX: { struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr; if (ipx_nullnet(satoipx_addr(work))) cp = "default"; else cp = ipx_print(sa); break; } case AF_APPLETALK: { if (!(flags & RTF_HOST) && mask) cp = atalk_print2(sa,mask,9); else cp = atalk_print(sa,11); break; } case AF_NETGRAPH: { printf("%s", ((struct sockaddr_ng *)sa)->sg_data); break; } #ifdef NS case AF_NS: cp = ns_print(sa); break; #endif case AF_LINK: { register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) (void) sprintf(workbuf, "link#%d", sdl->sdl_index); else switch (sdl->sdl_type) { case IFT_ETHER: if (sdl->sdl_alen == ETHER_ADDR_LEN) { cp = ether_ntoa((struct ether_addr *) (sdl->sdl_data + sdl->sdl_nlen)); break; } /* FALLTHROUGH */ default: cp = link_ntoa(sdl); break; } break; } default: { register u_char *s = (u_char *)sa->sa_data, *slim; slim = sa->sa_len + (u_char *) sa; cplim = cp + sizeof(workbuf) - 6; cp += sprintf(cp, "(%d)", sa->sa_family); while (s < slim && cp < cplim) { cp += sprintf(cp, " %02x", *s++); if (s < slim) cp += sprintf(cp, "%02x", *s++); } cp = workbuf; } } return (cp); } static void p_flags(int f, char *format) { printf(format, fmt_flags(f)); } static const char * fmt_flags(int f) { static char name[33]; char *flags; struct bits *p = bits; for (flags = name; p->b_mask; p++) if (p->b_mask & f) *flags++ = p->b_val; *flags = '\0'; return (name); } static void p_rtentry(struct rtentry *rt) { static struct ifnet ifnet, *lastif; struct rtentry parent; static char buffer[128]; static char prettyname[128]; struct sockaddr *sa; sa_u addr, mask; /* * Don't print protocol-cloned routes unless -a. */ if (rt->rt_flags & RTF_WASCLONED && !aflag) { kget(rt->rt_parent, parent); if (parent.rt_flags & RTF_PRCLONING) return; } bzero(&addr, sizeof(addr)); if ((sa = kgetsa(rt_key(rt)))) bcopy(sa, &addr, sa->sa_len); bzero(&mask, sizeof(mask)); if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) bcopy(sa, &mask, sa->sa_len); p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst); p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw); snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags); p_flags(rt->rt_flags, buffer); if (addr.u_sa.sa_family == AF_INET || Wflag) { printf("%*ld %*lu ", wid_refs, rt->rt_refcnt, wid_use, rt->rt_use); if (Wflag) { if (rt->rt_rmx.rmx_mtu != 0) printf("%*lu ", wid_mtu, rt->rt_rmx.rmx_mtu); else printf("%*s ", wid_mtu, ""); } } if (rt->rt_ifp) { if (rt->rt_ifp != lastif) { kget(rt->rt_ifp, ifnet); kread((u_long)ifnet.if_name, buffer, sizeof(buffer)); lastif = rt->rt_ifp; snprintf(prettyname, sizeof(prettyname), "%s%d", buffer, ifnet.if_unit); } printf("%*.*s", wid_if, wid_if, prettyname); if (rt->rt_rmx.rmx_expire) { time_t expire_time; if ((expire_time = rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0) printf(" %*d", wid_expire, (int)expire_time); } if (rt->rt_nodes[0].rn_dupedkey) printf(" =>"); } putchar('\n'); } char * routename(u_long in) { register char *cp; static char line[MAXHOSTNAMELEN]; struct hostent *hp; cp = 0; if (!numeric_addr) { hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), AF_INET); if (hp) { cp = hp->h_name; trimdomain(cp, strlen(cp)); } } if (cp) { strncpy(line, cp, sizeof(line) - 1); line[sizeof(line) - 1] = '\0'; } else { #define C(x) ((x) & 0xff) in = ntohl(in); sprintf(line, "%lu.%lu.%lu.%lu", C(in >> 24), C(in >> 16), C(in >> 8), C(in)); } return (line); } static u_long forgemask(u_long a) { u_long m; if (IN_CLASSA(a)) m = IN_CLASSA_NET; else if (IN_CLASSB(a)) m = IN_CLASSB_NET; else m = IN_CLASSC_NET; return (m); } static void domask(char *dst, u_long addr, u_long mask) { register int b, i; if (!mask || (forgemask(addr) == mask)) { *dst = '\0'; return; } i = 0; for (b = 0; b < 32; b++) if (mask & (1 << b)) { register int bb; i = b; for (bb = b+1; bb < 32; bb++) if (!(mask & (1 << bb))) { i = -1; /* noncontig */ break; } break; } if (i == -1) sprintf(dst, "&0x%lx", mask); else sprintf(dst, "/%d", 32-i); } /* * Return the name of the network whose address is given. * The address is assumed to be that of a net or subnet, not a host. */ char * netname(u_long in, u_long mask) { char *cp = 0; static char line[MAXHOSTNAMELEN]; struct netent *np = 0; u_long dmask; register u_long i; #define NSHIFT(m) ( \ (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ 0) i = ntohl(in); dmask = forgemask(i); if (!numeric_addr && i) { np = getnetbyaddr(i >> NSHIFT(mask), AF_INET); if (np == NULL && mask == 0) np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET); if (np != NULL) { cp = np->n_name; trimdomain(cp, strlen(cp)); } } #undef NSHIFT if (cp != NULL) { strncpy(line, cp, sizeof(line) - 1); line[sizeof(line) - 1] = '\0'; } else { switch (dmask) { case IN_CLASSA_NET: if ((i & IN_CLASSA_HOST) == 0) { sprintf(line, "%lu", C(i >> 24)); break; } /* FALLTHROUGH */ case IN_CLASSB_NET: if ((i & IN_CLASSB_HOST) == 0) { sprintf(line, "%lu.%lu", C(i >> 24), C(i >> 16)); break; } /* FALLTHROUGH */ case IN_CLASSC_NET: if ((i & IN_CLASSC_HOST) == 0) { sprintf(line, "%lu.%lu.%lu", C(i >> 24), C(i >> 16), C(i >> 8)); break; } /* FALLTHROUGH */ default: sprintf(line, "%lu.%lu.%lu.%lu", C(i >> 24), C(i >> 16), C(i >> 8), C(i)); break; } } domask(line + strlen(line), i, mask); return (line); } #ifdef INET6 char * netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) { static char line[MAXHOSTNAMELEN]; u_char *p = (u_char *)mask; u_char *lim; int masklen, illegal = 0, flag = NI_WITHSCOPEID; if (mask) { for (masklen = 0, lim = p + 16; p < lim; p++) { switch (*p) { case 0xff: masklen += 8; break; case 0xfe: masklen += 7; break; case 0xfc: masklen += 6; break; case 0xf8: masklen += 5; break; case 0xf0: masklen += 4; break; case 0xe0: masklen += 3; break; case 0xc0: masklen += 2; break; case 0x80: masklen += 1; break; case 0x00: break; default: illegal ++; break; } } if (illegal) fprintf(stderr, "illegal prefixlen\n"); } else masklen = 128; if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) return("default"); if (numeric_addr) flag |= NI_NUMERICHOST; getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), NULL, 0, flag); if (numeric_addr) sprintf(&line[strlen(line)], "/%d", masklen); return line; } char * routename6(struct sockaddr_in6 *sa6) { static char line[MAXHOSTNAMELEN]; int flag = NI_WITHSCOPEID; /* use local variable for safety */ struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),}; sa6_local.sin6_addr = sa6->sin6_addr; sa6_local.sin6_scope_id = sa6->sin6_scope_id; if (numeric_addr) flag |= NI_NUMERICHOST; getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, line, sizeof(line), NULL, 0, flag); return line; } #endif /*INET6*/ /* * Print routing statistics */ void rt_stats(u_long rtsaddr, u_long rttaddr) { struct rtstat rtstat; int rttrash; if (rtsaddr == 0) { printf("rtstat: symbol not in namelist\n"); return; } if (rttaddr == 0) { printf("rttrash: symbol not in namelist\n"); return; } kread(rtsaddr, (char *)&rtstat, sizeof (rtstat)); kread(rttaddr, (char *)&rttrash, sizeof (rttrash)); printf("routing:\n"); #define p(f, m) if (rtstat.f || sflag <= 1) \ printf(m, rtstat.f, plural(rtstat.f)) p(rts_badredirect, "\t%u bad routing redirect%s\n"); p(rts_dynamic, "\t%u dynamically created route%s\n"); p(rts_newgateway, "\t%u new gateway%s due to redirects\n"); p(rts_unreach, "\t%u destination%s found unreachable\n"); p(rts_wildcard, "\t%u use%s of a wildcard route\n"); #undef p if (rttrash || sflag <= 1) printf("\t%u route%s not in table but not freed\n", rttrash, plural(rttrash)); } char * ipx_print(struct sockaddr *sa) { u_short port; struct servent *sp = 0; char *net = "", *host = ""; register char *p; register u_char *q; struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr; static char mybuf[50]; char cport[10], chost[15], cnet[15]; port = ntohs(work.x_port); if (ipx_nullnet(work) && ipx_nullhost(work)) { if (port) { if (sp) sprintf(mybuf, "*.%s", sp->s_name); else sprintf(mybuf, "*.%x", port); } else sprintf(mybuf, "*.*"); return (mybuf); } if (ipx_wildnet(work)) net = "any"; else if (ipx_nullnet(work)) net = "*"; else { q = work.x_net.c_net; sprintf(cnet, "%02x%02x%02x%02x", q[0], q[1], q[2], q[3]); for (p = cnet; *p == '0' && p < cnet + 8; p++) continue; net = p; } if (ipx_wildhost(work)) host = "any"; else if (ipx_nullhost(work)) host = "*"; else { q = work.x_host.c_host; sprintf(chost, "%02x%02x%02x%02x%02x%02x", q[0], q[1], q[2], q[3], q[4], q[5]); for (p = chost; *p == '0' && p < chost + 12; p++) continue; host = p; } if (port) { if (strcmp(host, "*") == 0) host = ""; if (sp) snprintf(cport, sizeof(cport), "%s%s", *host ? "." : "", sp->s_name); else snprintf(cport, sizeof(cport), "%s%x", *host ? "." : "", port); } else *cport = 0; snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport); return(mybuf); } char * ipx_phost(struct sockaddr *sa) { register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa; struct sockaddr_ipx work; static union ipx_net ipx_zeronet; char *p; struct ipx_addr in; work = *sipx; in = work.sipx_addr; work.sipx_addr.x_port = 0; work.sipx_addr.x_net = ipx_zeronet; p = ipx_print((struct sockaddr *)&work); if (strncmp("*.", p, 2) == 0) p += 2; return(p); } #ifdef NS short ns_nullh[] = {0,0,0}; short ns_bh[] = {-1,-1,-1}; char * ns_print(struct sockaddr *sa) { register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa; struct ns_addr work; union { union ns_net net_e; u_long long_e; } net; u_short port; static char mybuf[50], cport[10], chost[25]; char *host = ""; register char *p; register u_char *q; work = sns->sns_addr; port = ntohs(work.x_port); work.x_port = 0; net.net_e = work.x_net; if (ns_nullhost(work) && net.long_e == 0) { if (port ) { sprintf(mybuf, "*.%xH", port); upHex(mybuf); } else sprintf(mybuf, "*.*"); return (mybuf); } if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) { host = "any"; } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) { host = "*"; } else { q = work.x_host.c_host; sprintf(chost, "%02x%02x%02x%02x%02x%02xH", q[0], q[1], q[2], q[3], q[4], q[5]); for (p = chost; *p == '0' && p < chost + 12; p++) continue; host = p; } if (port) sprintf(cport, ".%xH", htons(port)); else *cport = 0; sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport); upHex(mybuf); return(mybuf); } char * ns_phost(struct sockaddr *sa) { register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa; struct sockaddr_ns work; static union ns_net ns_zeronet; char *p; work = *sns; work.sns_addr.x_port = 0; work.sns_addr.x_net = ns_zeronet; p = ns_print((struct sockaddr *)&work); if (strncmp("0H.", p, 3) == 0) p += 3; return(p); } #endif void upHex(char *p0) { register char *p = p0; for (; *p; p++) switch (*p) { case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': *p += ('A' - 'a'); break; } }