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