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