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