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