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