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