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