1 /* $OpenBSD: if.c,v 1.9 1996/08/16 09:38:56 mickey Exp $ */ 2 /* $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94"; 40 #else 41 static char *rcsid = "$OpenBSD: if.c,v 1.9 1996/08/16 09:38:56 mickey 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 <netipx/ipx.h> 57 #include <netipx/ipx_if.h> 58 #include <netiso/iso.h> 59 #include <netiso/iso_var.h> 60 #include <arpa/inet.h> 61 62 #include <signal.h> 63 #include <stdio.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 #include "netstat.h" 68 69 #define YES 1 70 #define NO 0 71 72 static void sidewaysintpr __P((u_int, u_long)); 73 static void catchalarm __P((int)); 74 75 /* 76 * Print a description of the network interfaces. 77 * NOTE: ifnetaddr is the location of the kernel global "ifnet", 78 * which is a TAILQ_HEAD. 79 */ 80 void 81 intpr(interval, ifnetaddr) 82 int interval; 83 u_long ifnetaddr; 84 { 85 struct ifnet ifnet; 86 union { 87 struct ifaddr ifa; 88 struct in_ifaddr in; 89 struct ns_ifaddr ns; 90 struct ipx_ifaddr ipx; 91 struct iso_ifaddr iso; 92 } ifaddr; 93 u_long ifaddraddr; 94 struct sockaddr *sa; 95 struct ifnet_head ifhead; /* TAILQ_HEAD */ 96 char name[IFNAMSIZ]; 97 98 if (ifnetaddr == 0) { 99 printf("ifnet: symbol not defined\n"); 100 return; 101 } 102 if (interval) { 103 sidewaysintpr((unsigned)interval, ifnetaddr); 104 return; 105 } 106 107 /* 108 * Find the pointer to the first ifnet structure. Replace 109 * the pointer to the TAILQ_HEAD with the actual pointer 110 * to the first list element. 111 */ 112 if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead)) 113 return; 114 ifnetaddr = (u_long)ifhead.tqh_first; 115 116 printf("%-5.5s %-5.5s %-11.11s %-17.17s %8.8s %5.5s %8.8s %5.5s", 117 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs", 118 "Opkts", "Oerrs"); 119 printf(" %5s", "Coll"); 120 if (tflag) 121 printf(" %s", "Time"); 122 if (dflag) 123 printf(" %s", "Drop"); 124 putchar('\n'); 125 ifaddraddr = 0; 126 while (ifnetaddr || ifaddraddr) { 127 struct sockaddr_in *sin; 128 register char *cp; 129 int n, m; 130 131 if (ifaddraddr == 0) { 132 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet)) 133 return; 134 bcopy(ifnet.if_xname, name, IFNAMSIZ); 135 name[IFNAMSIZ - 1] = '\0'; /* sanity */ 136 ifnetaddr = (u_long)ifnet.if_list.tqe_next; 137 if (interface != 0 && strcmp(name, interface) != 0) 138 continue; 139 cp = index(name, '\0'); 140 if ((ifnet.if_flags & IFF_UP) == 0) 141 *cp++ = '*'; 142 *cp = '\0'; 143 ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first; 144 } 145 printf("%-5.5s %-5d ", name, ifnet.if_mtu); 146 if (ifaddraddr == 0) { 147 printf("%-11.11s ", "none"); 148 printf("%-15.15s ", "none"); 149 } else { 150 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) { 151 ifaddraddr = 0; 152 continue; 153 } 154 #define CP(x) ((char *)(x)) 155 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + 156 CP(&ifaddr); sa = (struct sockaddr *)cp; 157 switch (sa->sa_family) { 158 case AF_UNSPEC: 159 printf("%-11.11s ", "none"); 160 printf("%-17.17s ", "none"); 161 break; 162 case AF_INET: 163 sin = (struct sockaddr_in *)sa; 164 #ifdef notdef 165 /* can't use inet_makeaddr because kernel 166 * keeps nets unshifted. 167 */ 168 in = inet_makeaddr(ifaddr.in.ia_subnet, 169 INADDR_ANY); 170 printf("%-11.11s ", netname(in.s_addr, 171 ifaddr.in.ia_subnetmask)); 172 #else 173 printf("%-11.11s ", 174 netname(ifaddr.in.ia_subnet, 175 ifaddr.in.ia_subnetmask)); 176 #endif 177 printf("%-17.17s ", 178 routename(sin->sin_addr.s_addr)); 179 180 if (aflag) { 181 u_long multiaddr; 182 struct in_multi inm; 183 184 multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first; 185 while (multiaddr != 0) { 186 kread(multiaddr, (char *)&inm, 187 sizeof inm); 188 printf("\n%23s %-15.15s ", "", 189 routename(inm.inm_addr.s_addr)); 190 multiaddr = (u_long)inm.inm_list.le_next; 191 } 192 } 193 break; 194 case AF_IPX: 195 { 196 struct sockaddr_ipx *sipx = 197 (struct sockaddr_ipx *)sa; 198 u_long net; 199 char netnum[8]; 200 201 *(union ipx_net *) &net = sipx->sipx_addr.ipx_net; 202 sprintf(netnum, "%xH", ntohl(net)); 203 upHex(netnum); 204 printf("ipx:%-8s", netnum); 205 printf("%-17s ", 206 ipx_phost((struct sockaddr *)sipx)); 207 } 208 break; 209 case AF_NS: 210 { 211 struct sockaddr_ns *sns = 212 (struct sockaddr_ns *)sa; 213 u_long net; 214 char netnum[8]; 215 216 *(union ns_net *) &net = sns->sns_addr.x_net; 217 sprintf(netnum, "%xH", ntohl(net)); 218 upHex(netnum); 219 printf("ns:%-8s ", netnum); 220 printf("%-17s ", 221 ns_phost((struct sockaddr *)sns)); 222 } 223 break; 224 case AF_LINK: 225 { 226 struct sockaddr_dl *sdl = 227 (struct sockaddr_dl *)sa; 228 m = printf("%-11.11s ", "<Link>"); 229 if (sdl->sdl_type == IFT_ETHER || 230 sdl->sdl_type == IFT_FDDI) 231 printf("%-17.17s ", 232 ether_ntoa(LLADDR(sdl))); 233 else { 234 cp = (char *)LLADDR(sdl); 235 n = sdl->sdl_alen; 236 goto hexprint; 237 } 238 } 239 break; 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("%x%c", *cp++ & 0xff, 249 n > 0 ? '.' : ' '); 250 m = 30 - m; 251 while (m-- > 0) 252 putchar(' '); 253 break; 254 } 255 ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next; 256 } 257 printf("%8d %5d %8d %5d %5d", 258 ifnet.if_ipackets, ifnet.if_ierrors, 259 ifnet.if_opackets, ifnet.if_oerrors, 260 ifnet.if_collisions); 261 if (tflag) 262 printf(" %3d", ifnet.if_timer); 263 if (dflag) 264 printf(" %3d", ifnet.if_snd.ifq_drops); 265 putchar('\n'); 266 } 267 } 268 269 #define MAXIF 100 270 struct iftot { 271 char ift_name[IFNAMSIZ]; /* interface name */ 272 int ift_ip; /* input packets */ 273 int ift_ie; /* input errors */ 274 int ift_op; /* output packets */ 275 int ift_oe; /* output errors */ 276 int ift_co; /* collisions */ 277 int ift_dr; /* drops */ 278 } iftot[MAXIF]; 279 280 u_char signalled; /* set if alarm goes off "early" */ 281 282 /* 283 * Print a running summary of interface statistics. 284 * Repeat display every interval seconds, showing statistics 285 * collected over that interval. Assumes that interval is non-zero. 286 * First line printed at top of screen is always cumulative. 287 */ 288 static void 289 sidewaysintpr(interval, off) 290 unsigned interval; 291 u_long off; 292 { 293 struct ifnet ifnet; 294 u_long firstifnet; 295 register struct iftot *ip, *total; 296 register int line; 297 struct iftot *lastif, *sum, *interesting; 298 struct ifnet_head ifhead; /* TAILQ_HEAD */ 299 int oldmask; 300 301 /* 302 * Find the pointer to the first ifnet structure. Replace 303 * the pointer to the TAILQ_HEAD with the actual pointer 304 * to the first list element. 305 */ 306 if (kread(off, (char *)&ifhead, sizeof ifhead)) 307 return; 308 firstifnet = (u_long)ifhead.tqh_first; 309 310 lastif = iftot; 311 sum = iftot + MAXIF - 1; 312 total = sum - 1; 313 interesting = (interface == NULL) ? iftot : NULL; 314 for (off = firstifnet, ip = iftot; off;) { 315 if (kread(off, (char *)&ifnet, sizeof ifnet)) 316 break; 317 bzero(ip->ift_name, sizeof(ip->ift_name)); 318 snprintf(ip->ift_name, IFNAMSIZ, "(%s)", ifnet.if_xname); 319 if (interface && strcmp(ifnet.if_xname, interface) == 0) 320 interesting = ip; 321 ip++; 322 if (ip >= iftot + MAXIF - 2) 323 break; 324 off = (u_long)ifnet.if_list.tqe_next; 325 } 326 if (interesting == NULL) { 327 fprintf(stderr, "%s: %s: unknown interface\n", 328 __progname, interface); 329 exit(1); 330 } 331 lastif = ip; 332 333 (void)signal(SIGALRM, catchalarm); 334 signalled = NO; 335 (void)alarm(interval); 336 banner: 337 printf(" input %-6.6s output ", interesting->ift_name); 338 if (lastif - iftot > 0) { 339 if (dflag) 340 printf(" "); 341 printf(" input (Total) output"); 342 } 343 for (ip = iftot; ip < iftot + MAXIF; ip++) { 344 ip->ift_ip = 0; 345 ip->ift_ie = 0; 346 ip->ift_op = 0; 347 ip->ift_oe = 0; 348 ip->ift_co = 0; 349 ip->ift_dr = 0; 350 } 351 putchar('\n'); 352 printf("%8.8s %5.5s %8.8s %5.5s %5.5s ", 353 "packets", "errs", "packets", "errs", "colls"); 354 if (dflag) 355 printf("%5.5s ", "drops"); 356 if (lastif - iftot > 0) 357 printf(" %8.8s %5.5s %8.8s %5.5s %5.5s", 358 "packets", "errs", "packets", "errs", "colls"); 359 if (dflag) 360 printf(" %5.5s", "drops"); 361 putchar('\n'); 362 fflush(stdout); 363 line = 0; 364 loop: 365 sum->ift_ip = 0; 366 sum->ift_ie = 0; 367 sum->ift_op = 0; 368 sum->ift_oe = 0; 369 sum->ift_co = 0; 370 sum->ift_dr = 0; 371 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) { 372 if (kread(off, (char *)&ifnet, sizeof ifnet)) { 373 off = 0; 374 continue; 375 } 376 if (ip == interesting) { 377 printf("%8d %5d %8d %5d %5d", 378 ifnet.if_ipackets - ip->ift_ip, 379 ifnet.if_ierrors - ip->ift_ie, 380 ifnet.if_opackets - ip->ift_op, 381 ifnet.if_oerrors - ip->ift_oe, 382 ifnet.if_collisions - ip->ift_co); 383 if (dflag) 384 printf(" %5d", 385 ifnet.if_snd.ifq_drops - ip->ift_dr); 386 } 387 ip->ift_ip = ifnet.if_ipackets; 388 ip->ift_ie = ifnet.if_ierrors; 389 ip->ift_op = ifnet.if_opackets; 390 ip->ift_oe = ifnet.if_oerrors; 391 ip->ift_co = ifnet.if_collisions; 392 ip->ift_dr = ifnet.if_snd.ifq_drops; 393 sum->ift_ip += ip->ift_ip; 394 sum->ift_ie += ip->ift_ie; 395 sum->ift_op += ip->ift_op; 396 sum->ift_oe += ip->ift_oe; 397 sum->ift_co += ip->ift_co; 398 sum->ift_dr += ip->ift_dr; 399 off = (u_long)ifnet.if_list.tqe_next; 400 } 401 if (lastif - iftot > 0) { 402 printf(" %8d %5d %8d %5d %5d", 403 sum->ift_ip - total->ift_ip, 404 sum->ift_ie - total->ift_ie, 405 sum->ift_op - total->ift_op, 406 sum->ift_oe - total->ift_oe, 407 sum->ift_co - total->ift_co); 408 if (dflag) 409 printf(" %5d", sum->ift_dr - total->ift_dr); 410 } 411 *total = *sum; 412 putchar('\n'); 413 fflush(stdout); 414 line++; 415 oldmask = sigblock(sigmask(SIGALRM)); 416 if (! signalled) { 417 sigpause(0); 418 } 419 sigsetmask(oldmask); 420 signalled = NO; 421 (void)alarm(interval); 422 if (line == 21) 423 goto banner; 424 goto loop; 425 /*NOTREACHED*/ 426 } 427 428 /* 429 * Called if an interval expires before sidewaysintpr has completed a loop. 430 * Sets a flag to not wait for the alarm. 431 */ 432 static void 433 catchalarm(signo) 434 int signo; 435 { 436 signalled = YES; 437 } 438