1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 /* 36 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95"; 37 */ 38 static const char rcsid[] = 39 "$Id$"; 40 #endif /* not lint */ 41 42 #include <sys/types.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/time.h> 46 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/if_dl.h> 50 #include <net/if_types.h> 51 #include <netinet/in.h> 52 #include <netinet/in_var.h> 53 #define KERNEL 1 54 #include <netinet/if_ether.h> 55 #undef KERNEL 56 #include <netipx/ipx.h> 57 #include <netipx/ipx_if.h> 58 #ifdef NS 59 #include <netns/ns.h> 60 #include <netns/ns_if.h> 61 #endif 62 #ifdef ISO 63 #include <netiso/iso.h> 64 #include <netiso/iso_var.h> 65 #endif 66 #include <arpa/inet.h> 67 68 #include <signal.h> 69 #include <stdio.h> 70 #include <string.h> 71 #include <unistd.h> 72 73 #include "netstat.h" 74 75 #define YES 1 76 #define NO 0 77 78 static void sidewaysintpr __P((u_int, u_long)); 79 static void catchalarm __P((int)); 80 81 /* 82 * Print a description of the network interfaces. 83 */ 84 void 85 intpr(interval, ifnetaddr) 86 int interval; 87 u_long ifnetaddr; 88 { 89 struct ifnet ifnet; 90 struct ifnethead ifnethead; 91 union { 92 struct ifaddr ifa; 93 struct in_ifaddr in; 94 struct ipx_ifaddr ipx; 95 #ifdef NS 96 struct ns_ifaddr ns; 97 #endif 98 #ifdef ISO 99 struct iso_ifaddr iso; 100 #endif 101 } ifaddr; 102 u_long ifaddraddr; 103 u_long ifaddrfound; 104 u_long ifnetfound; 105 struct sockaddr *sa; 106 char name[32], tname[16]; 107 108 if (ifnetaddr == 0) { 109 printf("ifnet: symbol not defined\n"); 110 return; 111 } 112 if (interval) { 113 sidewaysintpr((unsigned)interval, ifnetaddr); 114 return; 115 } 116 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead)) 117 return; 118 ifnetaddr = (u_long)ifnethead.tqh_first; 119 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) 120 return; 121 122 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s", 123 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs"); 124 if (bflag) 125 printf(" %10.10s","Ibytes"); 126 printf(" %8.8s %5.5s", "Opkts", "Oerrs"); 127 if (bflag) 128 printf(" %10.10s","Obytes"); 129 printf(" %5s", "Coll"); 130 if (tflag) 131 printf(" %s", "Time"); 132 if (dflag) 133 printf(" %s", "Drop"); 134 putchar('\n'); 135 ifaddraddr = 0; 136 while (ifnetaddr || ifaddraddr) { 137 struct sockaddr_in *sin; 138 register char *cp; 139 int n, m; 140 141 if (ifaddraddr == 0) { 142 ifnetfound = ifnetaddr; 143 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) || 144 kread((u_long)ifnet.if_name, tname, 16)) 145 return; 146 tname[15] = '\0'; 147 ifnetaddr = (u_long)ifnet.if_link.tqe_next; 148 snprintf(name, 32, "%s%d", tname, ifnet.if_unit); 149 if (interface != 0 && (strcmp(name, interface) != 0)) 150 continue; 151 cp = index(name, '\0'); 152 if ((ifnet.if_flags&IFF_UP) == 0) 153 *cp++ = '*'; 154 *cp = '\0'; 155 ifaddraddr = (u_long)ifnet.if_addrhead.tqh_first; 156 } 157 printf("%-5.5s %-5lu ", name, ifnet.if_mtu); 158 ifaddrfound = ifaddraddr; 159 if (ifaddraddr == 0) { 160 printf("%-13.13s ", "none"); 161 printf("%-15.15s ", "none"); 162 } else { 163 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) { 164 ifaddraddr = 0; 165 continue; 166 } 167 #define CP(x) ((char *)(x)) 168 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + 169 CP(&ifaddr); sa = (struct sockaddr *)cp; 170 switch (sa->sa_family) { 171 case AF_UNSPEC: 172 printf("%-13.13s ", "none"); 173 printf("%-15.15s ", "none"); 174 break; 175 case AF_INET: 176 sin = (struct sockaddr_in *)sa; 177 #ifdef notdef 178 /* can't use inet_makeaddr because kernel 179 * keeps nets unshifted. 180 */ 181 in = inet_makeaddr(ifaddr.in.ia_subnet, 182 INADDR_ANY); 183 printf("%-13.13s ", netname(in.s_addr, 184 ifaddr.in.ia_subnetmask)); 185 #else 186 printf("%-13.13s ", 187 netname(htonl(ifaddr.in.ia_subnet), 188 ifaddr.in.ia_subnetmask)); 189 #endif 190 printf("%-15.15s ", 191 routename(sin->sin_addr.s_addr)); 192 break; 193 case AF_IPX: 194 { 195 struct sockaddr_ipx *sipx = 196 (struct sockaddr_ipx *)sa; 197 u_long net; 198 char netnum[10]; 199 200 *(union ipx_net *) &net = sipx->sipx_addr.x_net; 201 sprintf(netnum, "%lx", ntohl(net)); 202 printf("ipx:%-8s ", netnum); 203 /* printf("ipx:%-8s ", netname(net, 0L)); */ 204 printf("%-15s ", 205 ipx_phost((struct sockaddr *)sipx)); 206 } 207 break; 208 209 case AF_APPLETALK: 210 printf("atalk:%-12.12s ",atalk_print(sa,0x10) ); 211 printf("%-9.9s ",atalk_print(sa,0x0b) ); 212 break; 213 #ifdef NS 214 case AF_NS: 215 { 216 struct sockaddr_ns *sns = 217 (struct sockaddr_ns *)sa; 218 u_long net; 219 char netnum[10]; 220 221 *(union ns_net *) &net = sns->sns_addr.x_net; 222 sprintf(netnum, "%lxH", ntohl(net)); 223 upHex(netnum); 224 printf("ns:%-8s ", netnum); 225 printf("%-15s ", 226 ns_phost((struct sockaddr *)sns)); 227 } 228 break; 229 #endif 230 case AF_LINK: 231 { 232 struct sockaddr_dl *sdl = 233 (struct sockaddr_dl *)sa; 234 cp = (char *)LLADDR(sdl); 235 n = sdl->sdl_alen; 236 } 237 m = printf("%-11.11s ", "<Link>"); 238 goto hexprint; 239 default: 240 m = printf("(%d)", sa->sa_family); 241 for (cp = sa->sa_len + (char *)sa; 242 --cp > sa->sa_data && (*cp == 0);) {} 243 n = cp - sa->sa_data + 1; 244 cp = sa->sa_data; 245 hexprint: 246 while (--n >= 0) 247 m += printf("%02x%c", *cp++ & 0xff, 248 n > 0 ? '.' : ' '); 249 m = 30 - m; 250 while (m-- > 0) 251 putchar(' '); 252 break; 253 } 254 ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next; 255 } 256 printf("%8lu %5lu ", 257 ifnet.if_ipackets, ifnet.if_ierrors); 258 if (bflag) 259 printf("%10lu ", ifnet.if_ibytes); 260 printf("%8lu %5lu ", 261 ifnet.if_opackets, ifnet.if_oerrors); 262 if (bflag) 263 printf("%10lu ", ifnet.if_obytes); 264 printf("%5lu", ifnet.if_collisions); 265 if (tflag) 266 printf(" %3d", ifnet.if_timer); 267 if (dflag) 268 printf(" %3d", ifnet.if_snd.ifq_drops); 269 putchar('\n'); 270 if (aflag && ifaddrfound) { 271 /* 272 * Print family's multicast addresses 273 */ 274 switch (sa->sa_family) { 275 case AF_INET: 276 { 277 u_long multiaddr; 278 struct in_multi inm; 279 280 multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first; 281 while (multiaddr != 0) { 282 kread(multiaddr, (char *)&inm, 283 sizeof inm); 284 multiaddr = (u_long)inm.inm_entry.le_next; 285 printf("%23s %s\n", "", 286 routename(inm.inm_addr.s_addr)); 287 } 288 break; 289 } 290 case AF_LINK: 291 switch (ifnet.if_type) { 292 case IFT_ETHER: 293 case IFT_FDDI: /*XXX*/ 294 { 295 off_t multiaddr; 296 struct arpcom ac; 297 struct ether_multi enm; 298 299 kread(ifnetfound, (char *)&ac, sizeof ac); 300 multiaddr = (u_long)ac.ac_multiaddrs; 301 while (multiaddr != 0) { 302 kread(multiaddr, (char *)&enm, 303 sizeof enm); 304 multiaddr = (u_long)enm.enm_next; 305 printf("%23s %s", "", 306 ether_ntoa(&enm.enm_addrlo)); 307 if (bcmp(&enm.enm_addrlo, 308 &enm.enm_addrhi, 6) != 0) 309 printf(" to %s", 310 ether_ntoa(&enm.enm_addrhi)); 311 printf("\n"); 312 } 313 break; 314 } 315 default: 316 break; 317 } 318 default: 319 break; 320 } 321 } 322 } 323 } 324 325 #define MAXIF 10 326 struct iftot { 327 char ift_name[16]; /* interface name */ 328 u_int ift_ip; /* input packets */ 329 u_int ift_ie; /* input errors */ 330 u_int ift_op; /* output packets */ 331 u_int ift_oe; /* output errors */ 332 u_int ift_co; /* collisions */ 333 u_int ift_dr; /* drops */ 334 u_int ift_ib; /* input bytes */ 335 u_int ift_ob; /* output bytes */ 336 } iftot[MAXIF]; 337 338 u_char signalled; /* set if alarm goes off "early" */ 339 340 /* 341 * Print a running summary of interface statistics. 342 * Repeat display every interval seconds, showing statistics 343 * collected over that interval. Assumes that interval is non-zero. 344 * First line printed at top of screen is always cumulative. 345 * XXX - should be rewritten to use ifmib(4). 346 */ 347 static void 348 sidewaysintpr(interval, off) 349 unsigned interval; 350 u_long off; 351 { 352 struct ifnet ifnet; 353 u_long firstifnet; 354 struct ifnethead ifnethead; 355 register struct iftot *ip, *total; 356 register int line; 357 struct iftot *lastif, *sum, *interesting; 358 int oldmask, first; 359 u_long interesting_off; 360 361 if (kread(off, (char *)&ifnethead, sizeof ifnethead)) 362 return; 363 firstifnet = (u_long)ifnethead.tqh_first; 364 365 lastif = iftot; 366 sum = iftot + MAXIF - 1; 367 total = sum - 1; 368 interesting = NULL; 369 interesting_off = 0; 370 for (off = firstifnet, ip = iftot; off;) { 371 char name[16], tname[16]; 372 373 if (kread(off, (char *)&ifnet, sizeof ifnet)) 374 break; 375 if (kread((u_long)ifnet.if_name, tname, 16)) 376 break; 377 tname[15] = '\0'; 378 snprintf(name, 16, "%s%d", tname, ifnet.if_unit); 379 if (interface && strcmp(name, interface) == 0) { 380 interesting = ip; 381 interesting_off = off; 382 } 383 snprintf(ip->ift_name, 16, "(%s)", name);; 384 ip++; 385 if (ip >= iftot + MAXIF - 2) 386 break; 387 off = (u_long) ifnet.if_link.tqe_next; 388 } 389 lastif = ip; 390 391 (void)signal(SIGALRM, catchalarm); 392 signalled = NO; 393 (void)alarm(interval); 394 for (ip = iftot; ip < iftot + MAXIF; ip++) { 395 ip->ift_ip = 0; 396 ip->ift_ie = 0; 397 ip->ift_ib = 0; 398 ip->ift_op = 0; 399 ip->ift_oe = 0; 400 ip->ift_ob = 0; 401 ip->ift_co = 0; 402 ip->ift_dr = 0; 403 } 404 first = 1; 405 banner: 406 printf("%17s %14s %16s", "input", 407 interesting ? interesting->ift_name : "(Total)", "output"); 408 putchar('\n'); 409 printf("%10s %5s %10s %10s %5s %10s %5s", 410 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls"); 411 if (dflag) 412 printf(" %5.5s", "drops"); 413 putchar('\n'); 414 fflush(stdout); 415 line = 0; 416 loop: 417 if (interesting != NULL) { 418 ip = interesting; 419 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) { 420 printf("???\n"); 421 exit(1); 422 }; 423 if (!first) { 424 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", 425 ifnet.if_ipackets - ip->ift_ip, 426 ifnet.if_ierrors - ip->ift_ie, 427 ifnet.if_ibytes - ip->ift_ib, 428 ifnet.if_opackets - ip->ift_op, 429 ifnet.if_oerrors - ip->ift_oe, 430 ifnet.if_obytes - ip->ift_ob, 431 ifnet.if_collisions - ip->ift_co); 432 if (dflag) 433 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr); 434 } 435 ip->ift_ip = ifnet.if_ipackets; 436 ip->ift_ie = ifnet.if_ierrors; 437 ip->ift_ib = ifnet.if_ibytes; 438 ip->ift_op = ifnet.if_opackets; 439 ip->ift_oe = ifnet.if_oerrors; 440 ip->ift_ob = ifnet.if_obytes; 441 ip->ift_co = ifnet.if_collisions; 442 ip->ift_dr = ifnet.if_snd.ifq_drops; 443 } else { 444 sum->ift_ip = 0; 445 sum->ift_ie = 0; 446 sum->ift_ib = 0; 447 sum->ift_op = 0; 448 sum->ift_oe = 0; 449 sum->ift_ob = 0; 450 sum->ift_co = 0; 451 sum->ift_dr = 0; 452 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) { 453 if (kread(off, (char *)&ifnet, sizeof ifnet)) { 454 off = 0; 455 continue; 456 } 457 sum->ift_ip += ifnet.if_ipackets; 458 sum->ift_ie += ifnet.if_ierrors; 459 sum->ift_ib += ifnet.if_ibytes; 460 sum->ift_op += ifnet.if_opackets; 461 sum->ift_oe += ifnet.if_oerrors; 462 sum->ift_ob += ifnet.if_obytes; 463 sum->ift_co += ifnet.if_collisions; 464 sum->ift_dr += ifnet.if_snd.ifq_drops; 465 off = (u_long) ifnet.if_link.tqe_next; 466 } 467 if (!first) { 468 printf("%10u %5u %10u %10u %5u %10u %5u", 469 sum->ift_ip - total->ift_ip, 470 sum->ift_ie - total->ift_ie, 471 sum->ift_ib - total->ift_ib, 472 sum->ift_op - total->ift_op, 473 sum->ift_oe - total->ift_oe, 474 sum->ift_ob - total->ift_ob, 475 sum->ift_co - total->ift_co); 476 if (dflag) 477 printf(" %5u", sum->ift_dr - total->ift_dr); 478 } 479 *total = *sum; 480 } 481 if (!first) 482 putchar('\n'); 483 fflush(stdout); 484 oldmask = sigblock(sigmask(SIGALRM)); 485 if (! signalled) { 486 sigpause(0); 487 } 488 sigsetmask(oldmask); 489 signalled = NO; 490 (void)alarm(interval); 491 line++; 492 first = 0; 493 if (line == 21) 494 goto banner; 495 else 496 goto loop; 497 /*NOTREACHED*/ 498 } 499 500 /* 501 * Called if an interval expires before sidewaysintpr has completed a loop. 502 * Sets a flag to not wait for the alarm. 503 */ 504 static void 505 catchalarm(signo) 506 int signo; 507 { 508 signalled = YES; 509 } 510