if.c revision 1.30
1/* $NetBSD: if.c,v 1.30 1999/01/11 12:31:53 mrg 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.30 1999/01/11 12:31:53 mrg 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 if (bflag) { 114 printf("%-5.5s %-5.5s %-13.13s %-17.17s " 115 "%10.10s %10.10s", 116 "Name", "Mtu", "Network", "Address", 117 "Ibytes", "Obytes"); 118 } else { 119 printf("%-5.5s %-5.5s %-13.13s %-17.17s " 120 "%8.8s %5.5s %8.8s %5.5s", 121 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs", 122 "Opkts", "Oerrs"); 123 printf(" %5s", "Coll"); 124 } 125 if (tflag) 126 printf(" %s", "Time"); 127 if (dflag) 128 printf(" %s", "Drop"); 129 putchar('\n'); 130 ifaddraddr = 0; 131 while (ifnetaddr || ifaddraddr) { 132 struct sockaddr_in *sin; 133 char *cp; 134 int n, m; 135 136 if (ifaddraddr == 0) { 137 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) 138 return; 139 memmove(name, ifnet.if_xname, IFNAMSIZ); 140 name[IFNAMSIZ - 1] = '\0'; /* sanity */ 141 ifnetaddr = (u_long)ifnet.if_list.tqe_next; 142 if (interface != 0 && strcmp(name, interface) != 0) 143 continue; 144 cp = strchr(name, '\0'); 145 if ((ifnet.if_flags & IFF_UP) == 0) 146 *cp++ = '*'; 147 *cp = '\0'; 148 ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first; 149 } 150 printf("%-5.5s %-5lu ", name, ifnet.if_mtu); 151 if (ifaddraddr == 0) { 152 printf("%-13.13s ", "none"); 153 printf("%-17.17s ", "none"); 154 } else { 155 char hexsep = '.'; /* for hexprint */ 156 const char *hexfmt = "%x%c"; /* for hexprint */ 157 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) { 158 ifaddraddr = 0; 159 continue; 160 } 161#define CP(x) ((char *)(x)) 162 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + 163 CP(&ifaddr); sa = (struct sockaddr *)cp; 164 switch (sa->sa_family) { 165 case AF_UNSPEC: 166 printf("%-13.13s ", "none"); 167 printf("%-17.17s ", "none"); 168 break; 169 case AF_INET: 170 sin = (struct sockaddr_in *)sa; 171#ifdef notdef 172 /* 173 * can't use inet_makeaddr because kernel 174 * keeps nets unshifted. 175 */ 176 in = inet_makeaddr(ifaddr.in.ia_subnet, 177 INADDR_ANY); 178 printf("%-13.13s ", netname(in.s_addr, 179 ifaddr.in.ia_subnetmask)); 180#else 181 printf("%-13.13s ", 182 netname(ifaddr.in.ia_subnet, 183 ifaddr.in.ia_subnetmask)); 184#endif 185 printf("%-17.17s ", 186 routename(sin->sin_addr.s_addr)); 187 188 if (aflag) { 189 u_long multiaddr; 190 struct in_multi inm; 191 192 multiaddr = (u_long) 193 ifaddr.in.ia_multiaddrs.lh_first; 194 while (multiaddr != 0) { 195 kread(multiaddr, (char *)&inm, 196 sizeof inm); 197 printf("\n%25s %-17.17s ", "", 198 routename( 199 inm.inm_addr.s_addr)); 200 multiaddr = 201 (u_long)inm.inm_list.le_next; 202 } 203 } 204 break; 205#ifndef SMALL 206 case AF_APPLETALK: 207 printf("atalk:%-7.7s ", 208 atalk_print(sa,0x10)); 209 printf("%-17.17s ", atalk_print(sa,0x0b)); 210 break; 211 case AF_NS: 212 { 213 struct sockaddr_ns *sns = 214 (struct sockaddr_ns *)sa; 215 u_long net; 216 char netnum[8]; 217 218 *(union ns_net *)&net = sns->sns_addr.x_net; 219 (void)sprintf(netnum, "%xH", 220 (u_int32_t)ntohl(net)); 221 upHex(netnum); 222 printf("ns:%-10s ", netnum); 223 printf("%-17.17s ", 224 ns_phost((struct sockaddr *)sns)); 225 } 226 break; 227#endif 228 case AF_LINK: 229 { 230 struct sockaddr_dl *sdl = 231 (struct sockaddr_dl *)sa; 232 cp = (char *)LLADDR(sdl); 233 if (sdl->sdl_type == IFT_FDDI 234 || sdl->sdl_type == IFT_ETHER) 235 hexsep = ':', hexfmt = "%02x%c"; 236 n = sdl->sdl_alen; 237 } 238 m = printf("%-13.13s ", "<Link>"); 239 goto hexprint; 240 default: 241 m = printf("(%d)", sa->sa_family); 242 for (cp = sa->sa_len + (char *)sa; 243 --cp > sa->sa_data && (*cp == 0);) {} 244 n = cp - sa->sa_data + 1; 245 cp = sa->sa_data; 246 hexprint: 247 while (--n >= 0) 248 m += printf(hexfmt, *cp++ & 0xff, 249 n > 0 ? hexsep : ' '); 250 m = 32 - m; 251 while (m-- > 0) 252 putchar(' '); 253 break; 254 } 255 ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next; 256 } 257 if (bflag) { 258 printf("%10lu %10lu", 259 ifnet.if_ibytes, ifnet.if_obytes); 260 } else { 261 printf("%8lu %5lu %8lu %5lu %5lu", 262 ifnet.if_ipackets, ifnet.if_ierrors, 263 ifnet.if_opackets, ifnet.if_oerrors, 264 ifnet.if_collisions); 265 } 266 if (tflag) 267 printf(" %3d", ifnet.if_timer); 268 if (dflag) 269 printf(" %3d", ifnet.if_snd.ifq_drops); 270 putchar('\n'); 271 } 272} 273 274#define MAXIF 100 275struct iftot { 276 char ift_name[IFNAMSIZ]; /* interface name */ 277 u_long ift_ip; /* input packets */ 278 u_long ift_ib; /* input bytes */ 279 u_long ift_ie; /* input errors */ 280 u_long ift_op; /* output packets */ 281 u_long ift_ob; /* output bytes */ 282 u_long ift_oe; /* output errors */ 283 u_long ift_co; /* collisions */ 284 int ift_dr; /* drops */ 285} iftot[MAXIF]; 286 287u_char signalled; /* set if alarm goes off "early" */ 288 289/* 290 * Print a running summary of interface statistics. 291 * Repeat display every interval seconds, showing statistics 292 * collected over that interval. Assumes that interval is non-zero. 293 * First line printed at top of screen is always cumulative. 294 */ 295static void 296sidewaysintpr(interval, off) 297 unsigned interval; 298 u_long off; 299{ 300 struct ifnet ifnet; 301 u_long firstifnet; 302 struct iftot *ip, *total; 303 int line; 304 struct iftot *lastif, *sum, *interesting; 305 struct ifnet_head ifhead; /* TAILQ_HEAD */ 306 int oldmask; 307 308 /* 309 * Find the pointer to the first ifnet structure. Replace 310 * the pointer to the TAILQ_HEAD with the actual pointer 311 * to the first list element. 312 */ 313 if (kread(off, (char *)&ifhead, sizeof ifhead)) 314 return; 315 firstifnet = (u_long)ifhead.tqh_first; 316 317 lastif = iftot; 318 sum = iftot + MAXIF - 1; 319 total = sum - 1; 320 interesting = (interface == NULL) ? iftot : NULL; 321 for (off = firstifnet, ip = iftot; off;) { 322 if (kread(off, (char *)&ifnet, sizeof ifnet)) 323 break; 324 memset(ip->ift_name, 0, sizeof(ip->ift_name)); 325 snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname); 326 if (interface && strcmp(ifnet.if_xname, interface) == 0) 327 interesting = ip; 328 ip++; 329 if (ip >= iftot + MAXIF - 2) 330 break; 331 off = (u_long)ifnet.if_list.tqe_next; 332 } 333 if (interesting == NULL) { 334 fprintf(stderr, "%s: %s: unknown interface\n", 335 __progname, interface); 336 exit(1); 337 } 338 lastif = ip; 339 340 (void)signal(SIGALRM, catchalarm); 341 signalled = NO; 342 (void)alarm(interval); 343banner: 344 printf(" input %-6.6s output ", interesting->ift_name); 345 if (lastif - iftot > 0) { 346 if (dflag) 347 printf(" "); 348 printf(" input (Total) output"); 349 } 350 for (ip = iftot; ip < iftot + MAXIF; ip++) { 351 ip->ift_ip = 0; 352 ip->ift_ib = 0; 353 ip->ift_ie = 0; 354 ip->ift_op = 0; 355 ip->ift_ob = 0; 356 ip->ift_oe = 0; 357 ip->ift_co = 0; 358 ip->ift_dr = 0; 359 } 360 putchar('\n'); 361 if (bflag) { 362 printf("%10.10s %8.8s %10.10s %5.5s", 363 "bytes", " ", "bytes", " "); 364 } else { 365 printf("%8.8s %5.5s %8.8s %5.5s %5.5s ", 366 "packets", "errs", "packets", "errs", "colls"); 367 } 368 if (dflag) 369 printf("%5.5s ", "drops"); 370 if (lastif - iftot > 0) { 371 if (bflag) { 372 printf("%10.10s %8.8s %10.10s %5.5s", 373 "bytes", " ", "bytes", " "); 374 } else { 375 printf("%8.8s %5.5s %8.8s %5.5s %5.5s ", 376 "packets", "errs", "packets", "errs", "colls"); 377 } 378 } 379 if (dflag) 380 printf(" %5.5s", "drops"); 381 putchar('\n'); 382 fflush(stdout); 383 line = 0; 384loop: 385 sum->ift_ip = 0; 386 sum->ift_ib = 0; 387 sum->ift_ie = 0; 388 sum->ift_op = 0; 389 sum->ift_ob = 0; 390 sum->ift_oe = 0; 391 sum->ift_co = 0; 392 sum->ift_dr = 0; 393 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) { 394 if (kread(off, (char *)&ifnet, sizeof ifnet)) { 395 off = 0; 396 continue; 397 } 398 if (ip == interesting) { 399 if (bflag) { 400 printf("%10lu %8.8s %10lu %5.5s", 401 ifnet.if_ibytes - ip->ift_ib, " ", 402 ifnet.if_obytes - ip->ift_ob, " "); 403 } else { 404 printf("%8lu %5lu %8lu %5lu %5lu", 405 ifnet.if_ipackets - ip->ift_ip, 406 ifnet.if_ierrors - ip->ift_ie, 407 ifnet.if_opackets - ip->ift_op, 408 ifnet.if_oerrors - ip->ift_oe, 409 ifnet.if_collisions - ip->ift_co); 410 } 411 if (dflag) 412 printf(" %5d", 413 ifnet.if_snd.ifq_drops - ip->ift_dr); 414 } 415 ip->ift_ip = ifnet.if_ipackets; 416 ip->ift_ib = ifnet.if_ibytes; 417 ip->ift_ie = ifnet.if_ierrors; 418 ip->ift_op = ifnet.if_opackets; 419 ip->ift_ob = ifnet.if_obytes; 420 ip->ift_oe = ifnet.if_oerrors; 421 ip->ift_co = ifnet.if_collisions; 422 ip->ift_dr = ifnet.if_snd.ifq_drops; 423 sum->ift_ip += ip->ift_ip; 424 sum->ift_ib += ip->ift_ib; 425 sum->ift_ie += ip->ift_ie; 426 sum->ift_op += ip->ift_op; 427 sum->ift_ob += ip->ift_ob; 428 sum->ift_oe += ip->ift_oe; 429 sum->ift_co += ip->ift_co; 430 sum->ift_dr += ip->ift_dr; 431 off = (u_long)ifnet.if_list.tqe_next; 432 } 433 if (lastif - iftot > 0) { 434 if (bflag) { 435 printf(" %10lu %8.8s %10lu %5.5s", 436 sum->ift_ib - total->ift_ib, " ", 437 sum->ift_ob - total->ift_ob, " "); 438 } else { 439 printf(" %8lu %5lu %8lu %5lu %5lu", 440 sum->ift_ip - total->ift_ip, 441 sum->ift_ie - total->ift_ie, 442 sum->ift_op - total->ift_op, 443 sum->ift_oe - total->ift_oe, 444 sum->ift_co - total->ift_co); 445 } 446 if (dflag) 447 printf(" %5d", sum->ift_dr - total->ift_dr); 448 } 449 *total = *sum; 450 putchar('\n'); 451 fflush(stdout); 452 line++; 453 oldmask = sigblock(sigmask(SIGALRM)); 454 if (! signalled) { 455 sigpause(0); 456 } 457 sigsetmask(oldmask); 458 signalled = NO; 459 (void)alarm(interval); 460 if (line == 21) 461 goto banner; 462 goto loop; 463 /*NOTREACHED*/ 464} 465 466/* 467 * Called if an interval expires before sidewaysintpr has completed a loop. 468 * Sets a flag to not wait for the alarm. 469 */ 470static void 471catchalarm(signo) 472 int signo; 473{ 474 475 signalled = YES; 476} 477