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