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