if.c revision 1.25
1/* $NetBSD: if.c,v 1.25 1997/10/19 05:49:58 lukem 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#include <sys/cdefs.h> 37#ifndef lint 38#if 0 39static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94"; 40#else 41__RCSID("$NetBSD: if.c,v 1.25 1997/10/19 05:49:58 lukem Exp $"); 42#endif 43#endif /* not lint */ 44 45#include <sys/types.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48 49#include <net/if.h> 50#include <net/if_dl.h> 51#include <net/if_types.h> 52#include <netinet/in.h> 53#include <netinet/in_var.h> 54#include <netns/ns.h> 55#include <netns/ns_if.h> 56#include <netiso/iso.h> 57#include <netiso/iso_var.h> 58#include <arpa/inet.h> 59 60#include <signal.h> 61#include <stdio.h> 62#include <string.h> 63#include <unistd.h> 64 65#include "netstat.h" 66 67#define YES 1 68#define NO 0 69 70static void sidewaysintpr __P((u_int, u_long)); 71static void catchalarm __P((int)); 72 73/* 74 * Print a description of the network interfaces. 75 * NOTE: ifnetaddr is the location of the kernel global "ifnet", 76 * which is a TAILQ_HEAD. 77 */ 78void 79intpr(interval, ifnetaddr) 80 int interval; 81 u_long ifnetaddr; 82{ 83 struct ifnet ifnet; 84 union { 85 struct ifaddr ifa; 86 struct in_ifaddr in; 87 struct ns_ifaddr ns; 88 struct iso_ifaddr iso; 89 } ifaddr; 90 u_long ifaddraddr; 91 struct sockaddr *sa; 92 struct ifnet_head ifhead; /* TAILQ_HEAD */ 93 char name[IFNAMSIZ]; 94 95 if (ifnetaddr == 0) { 96 printf("ifnet: symbol not defined\n"); 97 return; 98 } 99 if (interval) { 100 sidewaysintpr((unsigned)interval, ifnetaddr); 101 return; 102 } 103 104 /* 105 * Find the pointer to the first ifnet structure. Replace 106 * the pointer to the TAILQ_HEAD with the actual pointer 107 * to the first list element. 108 */ 109 if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead)) 110 return; 111 ifnetaddr = (u_long)ifhead.tqh_first; 112 113 printf("%-5.5s %-5.5s %-13.13s %-17.17s %8.8s %5.5s %8.8s %5.5s", 114 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs", 115 "Opkts", "Oerrs"); 116 printf(" %5s", "Coll"); 117 if (tflag) 118 printf(" %s", "Time"); 119 if (dflag) 120 printf(" %s", "Drop"); 121 putchar('\n'); 122 ifaddraddr = 0; 123 while (ifnetaddr || ifaddraddr) { 124 struct sockaddr_in *sin; 125 char *cp; 126 int n, m; 127 128 if (ifaddraddr == 0) { 129 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) 130 return; 131 memmove(name, ifnet.if_xname, IFNAMSIZ); 132 name[IFNAMSIZ - 1] = '\0'; /* sanity */ 133 ifnetaddr = (u_long)ifnet.if_list.tqe_next; 134 if (interface != 0 && strcmp(name, interface) != 0) 135 continue; 136 cp = strchr(name, '\0'); 137 if ((ifnet.if_flags & IFF_UP) == 0) 138 *cp++ = '*'; 139 *cp = '\0'; 140 ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first; 141 } 142 printf("%-5.5s %-5ld ", name, ifnet.if_mtu); 143 if (ifaddraddr == 0) { 144 printf("%-13.13s ", "none"); 145 printf("%-17.17s ", "none"); 146 } else { 147 char hexsep = '.'; /* for hexprint */ 148 const char *hexfmt = "%x%c"; /* for hexprint */ 149 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) { 150 ifaddraddr = 0; 151 continue; 152 } 153#define CP(x) ((char *)(x)) 154 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + 155 CP(&ifaddr); sa = (struct sockaddr *)cp; 156 switch (sa->sa_family) { 157 case AF_UNSPEC: 158 printf("%-13.13s ", "none"); 159 printf("%-17.17s ", "none"); 160 break; 161 case AF_INET: 162 sin = (struct sockaddr_in *)sa; 163#ifdef notdef 164 /* can't use inet_makeaddr because kernel 165 * keeps nets unshifted. 166 */ 167 in = inet_makeaddr(ifaddr.in.ia_subnet, 168 INADDR_ANY); 169 printf("%-13.13s ", netname(in.s_addr, 170 ifaddr.in.ia_subnetmask)); 171#else 172 printf("%-13.13s ", 173 netname(ifaddr.in.ia_subnet, 174 ifaddr.in.ia_subnetmask)); 175#endif 176 printf("%-17.17s ", 177 routename(sin->sin_addr.s_addr)); 178 179 if (aflag) { 180 u_long multiaddr; 181 struct in_multi inm; 182 183 multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first; 184 while (multiaddr != 0) { 185 kread(multiaddr, (char *)&inm, 186 sizeof inm); 187 printf("\n%25s %-17.17s ", "", 188 routename(inm.inm_addr.s_addr)); 189 multiaddr = (u_long)inm.inm_list.le_next; 190 } 191 } 192 break; 193 case AF_APPLETALK: 194 printf("atalk:%-7.7s ", 195 atalk_print(sa,0x10)); 196 printf("%-17.17s ", atalk_print(sa,0x0b)); 197 break; 198 case AF_NS: 199 { 200 struct sockaddr_ns *sns = 201 (struct sockaddr_ns *)sa; 202 u_long net; 203 char netnum[8]; 204 205 *(union ns_net *) &net = sns->sns_addr.x_net; 206 sprintf(netnum, "%xH", (u_int32_t) ntohl(net)); 207 upHex(netnum); 208 printf("ns:%-10s ", netnum); 209 printf("%-17.17s ", 210 ns_phost((struct sockaddr *)sns)); 211 } 212 break; 213 case AF_LINK: 214 { 215 struct sockaddr_dl *sdl = 216 (struct sockaddr_dl *)sa; 217 cp = (char *)LLADDR(sdl); 218 if (sdl->sdl_type == IFT_FDDI 219 || sdl->sdl_type == IFT_ETHER) 220 hexsep = ':', hexfmt = "%02x%c"; 221 n = sdl->sdl_alen; 222 } 223 m = printf("%-13.13s ", "<Link>"); 224 goto hexprint; 225 default: 226 m = printf("(%d)", sa->sa_family); 227 for (cp = sa->sa_len + (char *)sa; 228 --cp > sa->sa_data && (*cp == 0);) {} 229 n = cp - sa->sa_data + 1; 230 cp = sa->sa_data; 231 hexprint: 232 while (--n >= 0) 233 m += printf(hexfmt, *cp++ & 0xff, 234 n > 0 ? hexsep : ' '); 235 m = 32 - m; 236 while (m-- > 0) 237 putchar(' '); 238 break; 239 } 240 ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next; 241 } 242 printf("%8ld %5ld %8ld %5ld %5ld", 243 ifnet.if_ipackets, ifnet.if_ierrors, 244 ifnet.if_opackets, ifnet.if_oerrors, 245 ifnet.if_collisions); 246 if (tflag) 247 printf(" %3d", ifnet.if_timer); 248 if (dflag) 249 printf(" %3d", ifnet.if_snd.ifq_drops); 250 putchar('\n'); 251 } 252} 253 254#define MAXIF 100 255struct iftot { 256 char ift_name[IFNAMSIZ]; /* interface name */ 257 int ift_ip; /* input packets */ 258 int ift_ie; /* input errors */ 259 int ift_op; /* output packets */ 260 int ift_oe; /* output errors */ 261 int ift_co; /* collisions */ 262 int ift_dr; /* drops */ 263} iftot[MAXIF]; 264 265u_char signalled; /* set if alarm goes off "early" */ 266 267/* 268 * Print a running summary of interface statistics. 269 * Repeat display every interval seconds, showing statistics 270 * collected over that interval. Assumes that interval is non-zero. 271 * First line printed at top of screen is always cumulative. 272 */ 273static void 274sidewaysintpr(interval, off) 275 unsigned interval; 276 u_long off; 277{ 278 struct ifnet ifnet; 279 u_long firstifnet; 280 struct iftot *ip, *total; 281 int line; 282 struct iftot *lastif, *sum, *interesting; 283 struct ifnet_head ifhead; /* TAILQ_HEAD */ 284 int oldmask; 285 286 /* 287 * Find the pointer to the first ifnet structure. Replace 288 * the pointer to the TAILQ_HEAD with the actual pointer 289 * to the first list element. 290 */ 291 if (kread(off, (char *)&ifhead, sizeof ifhead)) 292 return; 293 firstifnet = (u_long)ifhead.tqh_first; 294 295 lastif = iftot; 296 sum = iftot + MAXIF - 1; 297 total = sum - 1; 298 interesting = (interface == NULL) ? iftot : NULL; 299 for (off = firstifnet, ip = iftot; off;) { 300 if (kread(off, (char *)&ifnet, sizeof ifnet)) 301 break; 302 memset(ip->ift_name, 0, sizeof(ip->ift_name)); 303 snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname); 304 if (interface && strcmp(ifnet.if_xname, interface) == 0) 305 interesting = ip; 306 ip++; 307 if (ip >= iftot + MAXIF - 2) 308 break; 309 off = (u_long)ifnet.if_list.tqe_next; 310 } 311 if (interesting == NULL) { 312 fprintf(stderr, "%s: %s: unknown interface\n", 313 __progname, interface); 314 exit(1); 315 } 316 lastif = ip; 317 318 (void)signal(SIGALRM, catchalarm); 319 signalled = NO; 320 (void)alarm(interval); 321banner: 322 printf(" input %-6.6s output ", interesting->ift_name); 323 if (lastif - iftot > 0) { 324 if (dflag) 325 printf(" "); 326 printf(" input (Total) output"); 327 } 328 for (ip = iftot; ip < iftot + MAXIF; ip++) { 329 ip->ift_ip = 0; 330 ip->ift_ie = 0; 331 ip->ift_op = 0; 332 ip->ift_oe = 0; 333 ip->ift_co = 0; 334 ip->ift_dr = 0; 335 } 336 putchar('\n'); 337 printf("%8.8s %5.5s %8.8s %5.5s %5.5s ", 338 "packets", "errs", "packets", "errs", "colls"); 339 if (dflag) 340 printf("%5.5s ", "drops"); 341 if (lastif - iftot > 0) 342 printf(" %8.8s %5.5s %8.8s %5.5s %5.5s", 343 "packets", "errs", "packets", "errs", "colls"); 344 if (dflag) 345 printf(" %5.5s", "drops"); 346 putchar('\n'); 347 fflush(stdout); 348 line = 0; 349loop: 350 sum->ift_ip = 0; 351 sum->ift_ie = 0; 352 sum->ift_op = 0; 353 sum->ift_oe = 0; 354 sum->ift_co = 0; 355 sum->ift_dr = 0; 356 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) { 357 if (kread(off, (char *)&ifnet, sizeof ifnet)) { 358 off = 0; 359 continue; 360 } 361 if (ip == interesting) { 362 printf("%8ld %5ld %8ld %5ld %5ld", 363 ifnet.if_ipackets - ip->ift_ip, 364 ifnet.if_ierrors - ip->ift_ie, 365 ifnet.if_opackets - ip->ift_op, 366 ifnet.if_oerrors - ip->ift_oe, 367 ifnet.if_collisions - ip->ift_co); 368 if (dflag) 369 printf(" %5d", 370 ifnet.if_snd.ifq_drops - ip->ift_dr); 371 } 372 ip->ift_ip = ifnet.if_ipackets; 373 ip->ift_ie = ifnet.if_ierrors; 374 ip->ift_op = ifnet.if_opackets; 375 ip->ift_oe = ifnet.if_oerrors; 376 ip->ift_co = ifnet.if_collisions; 377 ip->ift_dr = ifnet.if_snd.ifq_drops; 378 sum->ift_ip += ip->ift_ip; 379 sum->ift_ie += ip->ift_ie; 380 sum->ift_op += ip->ift_op; 381 sum->ift_oe += ip->ift_oe; 382 sum->ift_co += ip->ift_co; 383 sum->ift_dr += ip->ift_dr; 384 off = (u_long)ifnet.if_list.tqe_next; 385 } 386 if (lastif - iftot > 0) { 387 printf(" %8d %5d %8d %5d %5d", 388 sum->ift_ip - total->ift_ip, 389 sum->ift_ie - total->ift_ie, 390 sum->ift_op - total->ift_op, 391 sum->ift_oe - total->ift_oe, 392 sum->ift_co - total->ift_co); 393 if (dflag) 394 printf(" %5d", sum->ift_dr - total->ift_dr); 395 } 396 *total = *sum; 397 putchar('\n'); 398 fflush(stdout); 399 line++; 400 oldmask = sigblock(sigmask(SIGALRM)); 401 if (! signalled) { 402 sigpause(0); 403 } 404 sigsetmask(oldmask); 405 signalled = NO; 406 (void)alarm(interval); 407 if (line == 21) 408 goto banner; 409 goto loop; 410 /*NOTREACHED*/ 411} 412 413/* 414 * Called if an interval expires before sidewaysintpr has completed a loop. 415 * Sets a flag to not wait for the alarm. 416 */ 417static void 418catchalarm(signo) 419 int signo; 420{ 421 signalled = YES; 422} 423