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