Deleted Added
sdiff udiff text old ( 53068 ) new ( 54263 )
full compact
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/*
36static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
37*/
38static const char rcsid[] =
39 "$FreeBSD: head/usr.bin/netstat/if.c 53068 1999-11-09 22:22:36Z dfr $";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/protosw.h>
44#include <sys/socket.h>
45#include <sys/sysctl.h>
46#include <sys/time.h>
47
48#include <net/if.h>
49#include <net/if_var.h>
50#include <net/if_dl.h>
51#include <net/if_types.h>
52#include <net/bridge.h>
53#include <net/ethernet.h>
54#include <netinet/in.h>
55#include <netinet/in_var.h>
56#include <netipx/ipx.h>
57#include <netipx/ipx_if.h>
58#ifdef NS
59#include <netns/ns.h>
60#include <netns/ns_if.h>
61#endif
62#ifdef ISO
63#include <netiso/iso.h>
64#include <netiso/iso_var.h>
65#endif
66#include <arpa/inet.h>
67
68#include <signal.h>
69#include <stdio.h>
70#include <string.h>
71#include <unistd.h>
72
73#include "netstat.h"
74
75#define YES 1
76#define NO 0
77
78static void sidewaysintpr __P((u_int, u_long));
79static void catchalarm __P((int));
80
81void
82bdg_stats(u_long dummy, char *name) /* print bridge statistics */
83{
84 int i;
85 size_t slen ;
86 struct bdg_stats s ;
87 int mib[4] ;
88
89 slen = sizeof(s);
90
91 mib[0] = CTL_NET ;
92 mib[1] = PF_LINK ;
93 mib[2] = IFT_ETHER ;
94 mib[3] = PF_BDG ;
95 if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
96 return ; /* no bridging */
97 printf("-- Bridging statistics (%s) --\n", name) ;
98 printf(
99"Name In Out Forward Drop Bcast Mcast Local Unknown\n");
100 for (i = 0 ; i < 16 ; i++) {
101 if (s.s[i].name[0])
102 printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
103 s.s[i].name,
104 s.s[i].p_in[(int)BDG_IN],
105 s.s[i].p_in[(int)BDG_OUT],
106 s.s[i].p_in[(int)BDG_FORWARD],
107 s.s[i].p_in[(int)BDG_DROP],
108 s.s[i].p_in[(int)BDG_BCAST],
109 s.s[i].p_in[(int)BDG_MCAST],
110 s.s[i].p_in[(int)BDG_LOCAL],
111 s.s[i].p_in[(int)BDG_UNKNOWN] );
112 }
113}
114
115/*
116 * Print a description of the network interfaces.
117 */
118void
119intpr(interval, ifnetaddr)
120 int interval;
121 u_long ifnetaddr;
122{
123 struct ifnet ifnet;
124 struct ifnethead ifnethead;
125 union {
126 struct ifaddr ifa;
127 struct in_ifaddr in;
128 struct ipx_ifaddr ipx;
129#ifdef NS
130 struct ns_ifaddr ns;
131#endif
132#ifdef ISO
133 struct iso_ifaddr iso;
134#endif
135 } ifaddr;
136 u_long ifaddraddr;
137 u_long ifaddrfound;
138 u_long ifnetfound;
139 struct sockaddr *sa = NULL;
140 char name[32], tname[16];
141
142 if (ifnetaddr == 0) {
143 printf("ifnet: symbol not defined\n");
144 return;
145 }
146 if (interval) {
147 sidewaysintpr((unsigned)interval, ifnetaddr);
148 return;
149 }
150 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
151 return;
152 ifnetaddr = (u_long)ifnethead.tqh_first;
153 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
154 return;
155
156 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
157 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
158 if (bflag)
159 printf(" %10.10s","Ibytes");
160 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
161 if (bflag)
162 printf(" %10.10s","Obytes");
163 printf(" %5s", "Coll");
164 if (tflag)
165 printf(" %s", "Time");
166 if (dflag)
167 printf(" %s", "Drop");
168 putchar('\n');
169 ifaddraddr = 0;
170 while (ifnetaddr || ifaddraddr) {
171 struct sockaddr_in *sin;
172 register char *cp;
173 int n, m;
174
175 if (ifaddraddr == 0) {
176 ifnetfound = ifnetaddr;
177 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
178 kread((u_long)ifnet.if_name, tname, 16))
179 return;
180 tname[15] = '\0';
181 ifnetaddr = (u_long)ifnet.if_link.tqe_next;
182 snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
183 if (interface != 0 && (strcmp(name, interface) != 0))
184 continue;
185 cp = index(name, '\0');
186 if ((ifnet.if_flags&IFF_UP) == 0)
187 *cp++ = '*';
188 *cp = '\0';
189 ifaddraddr = (u_long)ifnet.if_addrhead.tqh_first;
190 }
191 printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
192 ifaddrfound = ifaddraddr;
193 if (ifaddraddr == 0) {
194 printf("%-13.13s ", "none");
195 printf("%-15.15s ", "none");
196 } else {
197 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
198 ifaddraddr = 0;
199 continue;
200 }
201#define CP(x) ((char *)(x))
202 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
203 CP(&ifaddr);
204 sa = (struct sockaddr *)cp;
205 switch (sa->sa_family) {
206 case AF_UNSPEC:
207 printf("%-13.13s ", "none");
208 printf("%-15.15s ", "none");
209 break;
210 case AF_INET:
211 sin = (struct sockaddr_in *)sa;
212#ifdef notdef
213 /* can't use inet_makeaddr because kernel
214 * keeps nets unshifted.
215 */
216 in = inet_makeaddr(ifaddr.in.ia_subnet,
217 INADDR_ANY);
218 printf("%-13.13s ", netname(in.s_addr,
219 ifaddr.in.ia_subnetmask));
220#else
221 printf("%-13.13s ",
222 netname(htonl(ifaddr.in.ia_subnet),
223 ifaddr.in.ia_subnetmask));
224#endif
225 printf("%-15.15s ",
226 routename(sin->sin_addr.s_addr));
227 break;
228 case AF_IPX:
229 {
230 struct sockaddr_ipx *sipx =
231 (struct sockaddr_ipx *)sa;
232 u_long net;
233 char netnum[10];
234
235 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
236 sprintf(netnum, "%lx", (u_long)ntohl(net));
237 printf("ipx:%-8s ", netnum);
238/* printf("ipx:%-8s ", netname(net, 0L)); */
239 printf("%-15s ",
240 ipx_phost((struct sockaddr *)sipx));
241 }
242 break;
243
244 case AF_APPLETALK:
245 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
246 printf("%-9.9s ",atalk_print(sa,0x0b) );
247 break;
248#ifdef NS
249 case AF_NS:
250 {
251 struct sockaddr_ns *sns =
252 (struct sockaddr_ns *)sa;
253 u_long net;
254 char netnum[10];
255
256 *(union ns_net *) &net = sns->sns_addr.x_net;
257 sprintf(netnum, "%lxH", ntohl(net));
258 upHex(netnum);
259 printf("ns:%-8s ", netnum);
260 printf("%-15s ",
261 ns_phost((struct sockaddr *)sns));
262 }
263 break;
264#endif
265 case AF_LINK:
266 {
267 struct sockaddr_dl *sdl =
268 (struct sockaddr_dl *)sa;
269 cp = (char *)LLADDR(sdl);
270 n = sdl->sdl_alen;
271 }
272 m = printf("%-11.11s ", "<Link>");
273 goto hexprint;
274 default:
275 m = printf("(%d)", sa->sa_family);
276 for (cp = sa->sa_len + (char *)sa;
277 --cp > sa->sa_data && (*cp == 0);) {}
278 n = cp - sa->sa_data + 1;
279 cp = sa->sa_data;
280 hexprint:
281 while (--n >= 0)
282 m += printf("%02x%c", *cp++ & 0xff,
283 n > 0 ? '.' : ' ');
284 m = 30 - m;
285 while (m-- > 0)
286 putchar(' ');
287 break;
288 }
289 ifaddraddr = (u_long)ifaddr.ifa.ifa_link.tqe_next;
290 }
291 printf("%8lu %5lu ",
292 ifnet.if_ipackets, ifnet.if_ierrors);
293 if (bflag)
294 printf("%10lu ", ifnet.if_ibytes);
295 printf("%8lu %5lu ",
296 ifnet.if_opackets, ifnet.if_oerrors);
297 if (bflag)
298 printf("%10lu ", ifnet.if_obytes);
299 printf("%5lu", ifnet.if_collisions);
300 if (tflag)
301 printf(" %3d", ifnet.if_timer);
302 if (dflag)
303 printf(" %3d", ifnet.if_snd.ifq_drops);
304 putchar('\n');
305 if (aflag && ifaddrfound) {
306 /*
307 * Print family's multicast addresses
308 */
309 u_long multiaddr;
310 struct ifmultiaddr ifma;
311 union {
312 struct sockaddr sa;
313 struct sockaddr_in in;
314 struct sockaddr_dl dl;
315 } msa;
316 const char *fmt;
317
318 for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
319 multiaddr;
320 multiaddr = (u_long)ifma.ifma_link.le_next) {
321 if (kread(multiaddr, (char *)&ifma,
322 sizeof ifma))
323 break;
324 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
325 sizeof msa))
326 break;
327 if (msa.sa.sa_family != sa->sa_family)
328 continue;
329
330 fmt = 0;
331 switch (msa.sa.sa_family) {
332 case AF_INET:
333 fmt = routename(msa.in.sin_addr.s_addr);
334 break;
335
336 case AF_LINK:
337 switch (ifnet.if_type) {
338 case IFT_ETHER:
339 case IFT_FDDI:
340 fmt = ether_ntoa(
341 (struct ether_addr *)
342 LLADDR(&msa.dl));
343 break;
344 }
345 break;
346 }
347 if (fmt)
348 printf("%23s %s\n", "", fmt);
349 }
350 }
351 }
352}
353
354#define MAXIF 10
355struct iftot {
356 char ift_name[16]; /* interface name */
357 u_long ift_ip; /* input packets */
358 u_long ift_ie; /* input errors */
359 u_long ift_op; /* output packets */
360 u_long ift_oe; /* output errors */
361 u_long ift_co; /* collisions */
362 u_int ift_dr; /* drops */
363 u_long ift_ib; /* input bytes */
364 u_long ift_ob; /* output bytes */
365} iftot[MAXIF];
366
367u_char signalled; /* set if alarm goes off "early" */
368
369/*
370 * Print a running summary of interface statistics.
371 * Repeat display every interval seconds, showing statistics
372 * collected over that interval. Assumes that interval is non-zero.
373 * First line printed at top of screen is always cumulative.
374 * XXX - should be rewritten to use ifmib(4).
375 */
376static void
377sidewaysintpr(interval, off)
378 unsigned interval;
379 u_long off;
380{
381 struct ifnet ifnet;
382 u_long firstifnet;
383 struct ifnethead ifnethead;
384 register struct iftot *ip, *total;
385 register int line;
386 struct iftot *lastif, *sum, *interesting;
387 int oldmask, first;
388 u_long interesting_off;
389
390 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
391 return;
392 firstifnet = (u_long)ifnethead.tqh_first;
393
394 lastif = iftot;
395 sum = iftot + MAXIF - 1;
396 total = sum - 1;
397 interesting = NULL;
398 interesting_off = 0;
399 for (off = firstifnet, ip = iftot; off;) {
400 char name[16], tname[16];
401
402 if (kread(off, (char *)&ifnet, sizeof ifnet))
403 break;
404 if (kread((u_long)ifnet.if_name, tname, 16))
405 break;
406 tname[15] = '\0';
407 snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
408 if (interface && strcmp(name, interface) == 0) {
409 interesting = ip;
410 interesting_off = off;
411 }
412 snprintf(ip->ift_name, 16, "(%s)", name);;
413 ip++;
414 if (ip >= iftot + MAXIF - 2)
415 break;
416 off = (u_long) ifnet.if_link.tqe_next;
417 }
418 lastif = ip;
419
420 (void)signal(SIGALRM, catchalarm);
421 signalled = NO;
422 (void)alarm(interval);
423 for (ip = iftot; ip < iftot + MAXIF; ip++) {
424 ip->ift_ip = 0;
425 ip->ift_ie = 0;
426 ip->ift_ib = 0;
427 ip->ift_op = 0;
428 ip->ift_oe = 0;
429 ip->ift_ob = 0;
430 ip->ift_co = 0;
431 ip->ift_dr = 0;
432 }
433 first = 1;
434banner:
435 printf("%17s %14s %16s", "input",
436 interesting ? interesting->ift_name : "(Total)", "output");
437 putchar('\n');
438 printf("%10s %5s %10s %10s %5s %10s %5s",
439 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
440 if (dflag)
441 printf(" %5.5s", "drops");
442 putchar('\n');
443 fflush(stdout);
444 line = 0;
445loop:
446 if (interesting != NULL) {
447 ip = interesting;
448 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
449 printf("???\n");
450 exit(1);
451 };
452 if (!first) {
453 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
454 ifnet.if_ipackets - ip->ift_ip,
455 ifnet.if_ierrors - ip->ift_ie,
456 ifnet.if_ibytes - ip->ift_ib,
457 ifnet.if_opackets - ip->ift_op,
458 ifnet.if_oerrors - ip->ift_oe,
459 ifnet.if_obytes - ip->ift_ob,
460 ifnet.if_collisions - ip->ift_co);
461 if (dflag)
462 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
463 }
464 ip->ift_ip = ifnet.if_ipackets;
465 ip->ift_ie = ifnet.if_ierrors;
466 ip->ift_ib = ifnet.if_ibytes;
467 ip->ift_op = ifnet.if_opackets;
468 ip->ift_oe = ifnet.if_oerrors;
469 ip->ift_ob = ifnet.if_obytes;
470 ip->ift_co = ifnet.if_collisions;
471 ip->ift_dr = ifnet.if_snd.ifq_drops;
472 } else {
473 sum->ift_ip = 0;
474 sum->ift_ie = 0;
475 sum->ift_ib = 0;
476 sum->ift_op = 0;
477 sum->ift_oe = 0;
478 sum->ift_ob = 0;
479 sum->ift_co = 0;
480 sum->ift_dr = 0;
481 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
482 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
483 off = 0;
484 continue;
485 }
486 sum->ift_ip += ifnet.if_ipackets;
487 sum->ift_ie += ifnet.if_ierrors;
488 sum->ift_ib += ifnet.if_ibytes;
489 sum->ift_op += ifnet.if_opackets;
490 sum->ift_oe += ifnet.if_oerrors;
491 sum->ift_ob += ifnet.if_obytes;
492 sum->ift_co += ifnet.if_collisions;
493 sum->ift_dr += ifnet.if_snd.ifq_drops;
494 off = (u_long) ifnet.if_link.tqe_next;
495 }
496 if (!first) {
497 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
498 sum->ift_ip - total->ift_ip,
499 sum->ift_ie - total->ift_ie,
500 sum->ift_ib - total->ift_ib,
501 sum->ift_op - total->ift_op,
502 sum->ift_oe - total->ift_oe,
503 sum->ift_ob - total->ift_ob,
504 sum->ift_co - total->ift_co);
505 if (dflag)
506 printf(" %5u", sum->ift_dr - total->ift_dr);
507 }
508 *total = *sum;
509 }
510 if (!first)
511 putchar('\n');
512 fflush(stdout);
513 oldmask = sigblock(sigmask(SIGALRM));
514 if (! signalled) {
515 sigpause(0);
516 }
517 sigsetmask(oldmask);
518 signalled = NO;
519 (void)alarm(interval);
520 line++;
521 first = 0;
522 if (line == 21)
523 goto banner;
524 else
525 goto loop;
526 /*NOTREACHED*/
527}
528
529/*
530 * Called if an interval expires before sidewaysintpr has completed a loop.
531 * Sets a flag to not wait for the alarm.
532 */
533static void
534catchalarm(signo)
535 int signo;
536{
537 signalled = YES;
538}