if.c revision 1.18 1 1.18 cgd /* $NetBSD: if.c,v 1.18 1996/06/04 20:22:23 cgd Exp $ */
2 1.13 thorpej
3 1.1 cgd /*
4 1.8 mycroft * Copyright (c) 1983, 1988, 1993
5 1.8 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.13 thorpej #if 0
38 1.13 thorpej static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
39 1.13 thorpej #else
40 1.18 cgd static char *rcsid = "$NetBSD: if.c,v 1.18 1996/06/04 20:22:23 cgd Exp $";
41 1.13 thorpej #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/types.h>
45 1.8 mycroft #include <sys/protosw.h>
46 1.1 cgd #include <sys/socket.h>
47 1.1 cgd
48 1.1 cgd #include <net/if.h>
49 1.1 cgd #include <net/if_dl.h>
50 1.1 cgd #include <netinet/in.h>
51 1.1 cgd #include <netinet/in_var.h>
52 1.1 cgd #include <netns/ns.h>
53 1.1 cgd #include <netns/ns_if.h>
54 1.1 cgd #include <netiso/iso.h>
55 1.1 cgd #include <netiso/iso_var.h>
56 1.8 mycroft #include <arpa/inet.h>
57 1.1 cgd
58 1.8 mycroft #include <signal.h>
59 1.1 cgd #include <stdio.h>
60 1.8 mycroft #include <string.h>
61 1.8 mycroft #include <unistd.h>
62 1.8 mycroft
63 1.8 mycroft #include "netstat.h"
64 1.1 cgd
65 1.1 cgd #define YES 1
66 1.1 cgd #define NO 0
67 1.1 cgd
68 1.8 mycroft static void sidewaysintpr __P((u_int, u_long));
69 1.8 mycroft static void catchalarm __P((int));
70 1.1 cgd
71 1.1 cgd /*
72 1.1 cgd * Print a description of the network interfaces.
73 1.15 thorpej * NOTE: ifnetaddr is the location of the kernel global "ifnet",
74 1.15 thorpej * which is a TAILQ_HEAD.
75 1.1 cgd */
76 1.8 mycroft void
77 1.1 cgd intpr(interval, ifnetaddr)
78 1.1 cgd int interval;
79 1.7 cgd u_long ifnetaddr;
80 1.1 cgd {
81 1.1 cgd struct ifnet ifnet;
82 1.1 cgd union {
83 1.1 cgd struct ifaddr ifa;
84 1.1 cgd struct in_ifaddr in;
85 1.1 cgd struct ns_ifaddr ns;
86 1.1 cgd struct iso_ifaddr iso;
87 1.1 cgd } ifaddr;
88 1.7 cgd u_long ifaddraddr;
89 1.1 cgd struct sockaddr *sa;
90 1.15 thorpej struct ifnet_head ifhead; /* TAILQ_HEAD */
91 1.15 thorpej char name[IFNAMSIZ];
92 1.1 cgd
93 1.1 cgd if (ifnetaddr == 0) {
94 1.1 cgd printf("ifnet: symbol not defined\n");
95 1.1 cgd return;
96 1.1 cgd }
97 1.1 cgd if (interval) {
98 1.1 cgd sidewaysintpr((unsigned)interval, ifnetaddr);
99 1.1 cgd return;
100 1.1 cgd }
101 1.15 thorpej
102 1.15 thorpej /*
103 1.15 thorpej * Find the pointer to the first ifnet structure. Replace
104 1.15 thorpej * the pointer to the TAILQ_HEAD with the actual pointer
105 1.15 thorpej * to the first list element.
106 1.15 thorpej */
107 1.15 thorpej if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
108 1.8 mycroft return;
109 1.15 thorpej ifnetaddr = (u_long)ifhead.tqh_first;
110 1.15 thorpej
111 1.14 jtc printf("%-5.5s %-5.5s %-11.11s %-17.17s %8.8s %5.5s %8.8s %5.5s",
112 1.1 cgd "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
113 1.1 cgd "Opkts", "Oerrs");
114 1.1 cgd printf(" %5s", "Coll");
115 1.1 cgd if (tflag)
116 1.1 cgd printf(" %s", "Time");
117 1.1 cgd if (dflag)
118 1.1 cgd printf(" %s", "Drop");
119 1.1 cgd putchar('\n');
120 1.1 cgd ifaddraddr = 0;
121 1.1 cgd while (ifnetaddr || ifaddraddr) {
122 1.1 cgd struct sockaddr_in *sin;
123 1.1 cgd register char *cp;
124 1.1 cgd int n, m;
125 1.1 cgd
126 1.1 cgd if (ifaddraddr == 0) {
127 1.15 thorpej if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
128 1.8 mycroft return;
129 1.15 thorpej bcopy(ifnet.if_xname, name, IFNAMSIZ);
130 1.15 thorpej name[IFNAMSIZ - 1] = '\0'; /* sanity */
131 1.10 mycroft ifnetaddr = (u_long)ifnet.if_list.tqe_next;
132 1.16 thorpej if (interface != 0 && strcmp(name, interface) != 0)
133 1.1 cgd continue;
134 1.1 cgd cp = index(name, '\0');
135 1.10 mycroft if ((ifnet.if_flags & IFF_UP) == 0)
136 1.1 cgd *cp++ = '*';
137 1.1 cgd *cp = '\0';
138 1.10 mycroft ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
139 1.1 cgd }
140 1.1 cgd printf("%-5.5s %-5d ", name, ifnet.if_mtu);
141 1.1 cgd if (ifaddraddr == 0) {
142 1.1 cgd printf("%-11.11s ", "none");
143 1.1 cgd printf("%-15.15s ", "none");
144 1.1 cgd } else {
145 1.8 mycroft if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
146 1.8 mycroft ifaddraddr = 0;
147 1.8 mycroft continue;
148 1.8 mycroft }
149 1.1 cgd #define CP(x) ((char *)(x))
150 1.8 mycroft cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
151 1.1 cgd CP(&ifaddr); sa = (struct sockaddr *)cp;
152 1.1 cgd switch (sa->sa_family) {
153 1.1 cgd case AF_UNSPEC:
154 1.1 cgd printf("%-11.11s ", "none");
155 1.12 jtc printf("%-17.17s ", "none");
156 1.1 cgd break;
157 1.1 cgd case AF_INET:
158 1.1 cgd sin = (struct sockaddr_in *)sa;
159 1.1 cgd #ifdef notdef
160 1.1 cgd /* can't use inet_makeaddr because kernel
161 1.1 cgd * keeps nets unshifted.
162 1.1 cgd */
163 1.1 cgd in = inet_makeaddr(ifaddr.in.ia_subnet,
164 1.1 cgd INADDR_ANY);
165 1.8 mycroft printf("%-11.11s ", netname(in.s_addr,
166 1.8 mycroft ifaddr.in.ia_subnetmask));
167 1.1 cgd #else
168 1.1 cgd printf("%-11.11s ",
169 1.11 mycroft netname(ifaddr.in.ia_subnet,
170 1.8 mycroft ifaddr.in.ia_subnetmask));
171 1.1 cgd #endif
172 1.12 jtc printf("%-17.17s ",
173 1.8 mycroft routename(sin->sin_addr.s_addr));
174 1.10 mycroft
175 1.10 mycroft if (aflag) {
176 1.10 mycroft u_long multiaddr;
177 1.10 mycroft struct in_multi inm;
178 1.10 mycroft
179 1.10 mycroft multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
180 1.10 mycroft while (multiaddr != 0) {
181 1.10 mycroft kread(multiaddr, (char *)&inm,
182 1.10 mycroft sizeof inm);
183 1.10 mycroft printf("\n%23s %-15.15s ", "",
184 1.10 mycroft routename(inm.inm_addr.s_addr));
185 1.10 mycroft multiaddr = (u_long)inm.inm_list.le_next;
186 1.10 mycroft }
187 1.10 mycroft }
188 1.1 cgd break;
189 1.1 cgd case AF_NS:
190 1.1 cgd {
191 1.1 cgd struct sockaddr_ns *sns =
192 1.1 cgd (struct sockaddr_ns *)sa;
193 1.1 cgd u_long net;
194 1.1 cgd char netnum[8];
195 1.1 cgd
196 1.1 cgd *(union ns_net *) &net = sns->sns_addr.x_net;
197 1.1 cgd sprintf(netnum, "%lxH", ntohl(net));
198 1.1 cgd upHex(netnum);
199 1.1 cgd printf("ns:%-8s ", netnum);
200 1.12 jtc printf("%-17s ",
201 1.8 mycroft ns_phost((struct sockaddr *)sns));
202 1.1 cgd }
203 1.1 cgd break;
204 1.1 cgd case AF_LINK:
205 1.1 cgd {
206 1.1 cgd struct sockaddr_dl *sdl =
207 1.1 cgd (struct sockaddr_dl *)sa;
208 1.1 cgd cp = (char *)LLADDR(sdl);
209 1.1 cgd n = sdl->sdl_alen;
210 1.1 cgd }
211 1.9 mycroft m = printf("%-11.11s ", "<Link>");
212 1.1 cgd goto hexprint;
213 1.1 cgd default:
214 1.1 cgd m = printf("(%d)", sa->sa_family);
215 1.1 cgd for (cp = sa->sa_len + (char *)sa;
216 1.1 cgd --cp > sa->sa_data && (*cp == 0);) {}
217 1.1 cgd n = cp - sa->sa_data + 1;
218 1.1 cgd cp = sa->sa_data;
219 1.1 cgd hexprint:
220 1.1 cgd while (--n >= 0)
221 1.1 cgd m += printf("%x%c", *cp++ & 0xff,
222 1.1 cgd n > 0 ? '.' : ' ');
223 1.12 jtc m = 30 - m;
224 1.1 cgd while (m-- > 0)
225 1.1 cgd putchar(' ');
226 1.1 cgd break;
227 1.1 cgd }
228 1.10 mycroft ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
229 1.1 cgd }
230 1.1 cgd printf("%8d %5d %8d %5d %5d",
231 1.1 cgd ifnet.if_ipackets, ifnet.if_ierrors,
232 1.1 cgd ifnet.if_opackets, ifnet.if_oerrors,
233 1.1 cgd ifnet.if_collisions);
234 1.1 cgd if (tflag)
235 1.1 cgd printf(" %3d", ifnet.if_timer);
236 1.1 cgd if (dflag)
237 1.1 cgd printf(" %3d", ifnet.if_snd.ifq_drops);
238 1.1 cgd putchar('\n');
239 1.1 cgd }
240 1.1 cgd }
241 1.1 cgd
242 1.18 cgd #define MAXIF 100
243 1.1 cgd struct iftot {
244 1.15 thorpej char ift_name[IFNAMSIZ]; /* interface name */
245 1.1 cgd int ift_ip; /* input packets */
246 1.1 cgd int ift_ie; /* input errors */
247 1.1 cgd int ift_op; /* output packets */
248 1.1 cgd int ift_oe; /* output errors */
249 1.1 cgd int ift_co; /* collisions */
250 1.1 cgd int ift_dr; /* drops */
251 1.1 cgd } iftot[MAXIF];
252 1.1 cgd
253 1.1 cgd u_char signalled; /* set if alarm goes off "early" */
254 1.1 cgd
255 1.1 cgd /*
256 1.1 cgd * Print a running summary of interface statistics.
257 1.1 cgd * Repeat display every interval seconds, showing statistics
258 1.1 cgd * collected over that interval. Assumes that interval is non-zero.
259 1.1 cgd * First line printed at top of screen is always cumulative.
260 1.1 cgd */
261 1.8 mycroft static void
262 1.1 cgd sidewaysintpr(interval, off)
263 1.1 cgd unsigned interval;
264 1.7 cgd u_long off;
265 1.1 cgd {
266 1.1 cgd struct ifnet ifnet;
267 1.7 cgd u_long firstifnet;
268 1.1 cgd register struct iftot *ip, *total;
269 1.1 cgd register int line;
270 1.1 cgd struct iftot *lastif, *sum, *interesting;
271 1.15 thorpej struct ifnet_head ifhead; /* TAILQ_HEAD */
272 1.1 cgd int oldmask;
273 1.1 cgd
274 1.15 thorpej /*
275 1.15 thorpej * Find the pointer to the first ifnet structure. Replace
276 1.15 thorpej * the pointer to the TAILQ_HEAD with the actual pointer
277 1.15 thorpej * to the first list element.
278 1.15 thorpej */
279 1.15 thorpej if (kread(off, (char *)&ifhead, sizeof ifhead))
280 1.8 mycroft return;
281 1.15 thorpej firstifnet = (u_long)ifhead.tqh_first;
282 1.15 thorpej
283 1.1 cgd lastif = iftot;
284 1.1 cgd sum = iftot + MAXIF - 1;
285 1.1 cgd total = sum - 1;
286 1.17 cgd interesting = (interface == NULL) ? iftot : NULL;
287 1.1 cgd for (off = firstifnet, ip = iftot; off;) {
288 1.8 mycroft if (kread(off, (char *)&ifnet, sizeof ifnet))
289 1.8 mycroft break;
290 1.1 cgd ip->ift_name[0] = '(';
291 1.15 thorpej bcopy(ifnet.if_xname, ip->ift_name + 1, IFNAMSIZ - 1);
292 1.15 thorpej if (interface &&
293 1.15 thorpej strcmp(ip->ift_name + 1, interface) == 0)
294 1.1 cgd interesting = ip;
295 1.15 thorpej ip->ift_name[IFNAMSIZ - 1] = '\0';
296 1.1 cgd ip++;
297 1.1 cgd if (ip >= iftot + MAXIF - 2)
298 1.1 cgd break;
299 1.10 mycroft off = (u_long)ifnet.if_list.tqe_next;
300 1.17 cgd }
301 1.17 cgd if (interesting == NULL) {
302 1.17 cgd fprintf(stderr, "%s: %s: unknown interface\n",
303 1.17 cgd __progname, interface);
304 1.17 cgd exit(1);
305 1.1 cgd }
306 1.1 cgd lastif = ip;
307 1.1 cgd
308 1.1 cgd (void)signal(SIGALRM, catchalarm);
309 1.1 cgd signalled = NO;
310 1.1 cgd (void)alarm(interval);
311 1.1 cgd banner:
312 1.1 cgd printf(" input %-6.6s output ", interesting->ift_name);
313 1.1 cgd if (lastif - iftot > 0) {
314 1.1 cgd if (dflag)
315 1.1 cgd printf(" ");
316 1.1 cgd printf(" input (Total) output");
317 1.1 cgd }
318 1.1 cgd for (ip = iftot; ip < iftot + MAXIF; ip++) {
319 1.1 cgd ip->ift_ip = 0;
320 1.1 cgd ip->ift_ie = 0;
321 1.1 cgd ip->ift_op = 0;
322 1.1 cgd ip->ift_oe = 0;
323 1.1 cgd ip->ift_co = 0;
324 1.1 cgd ip->ift_dr = 0;
325 1.1 cgd }
326 1.1 cgd putchar('\n');
327 1.1 cgd printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
328 1.1 cgd "packets", "errs", "packets", "errs", "colls");
329 1.1 cgd if (dflag)
330 1.1 cgd printf("%5.5s ", "drops");
331 1.1 cgd if (lastif - iftot > 0)
332 1.1 cgd printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
333 1.1 cgd "packets", "errs", "packets", "errs", "colls");
334 1.1 cgd if (dflag)
335 1.1 cgd printf(" %5.5s", "drops");
336 1.1 cgd putchar('\n');
337 1.1 cgd fflush(stdout);
338 1.1 cgd line = 0;
339 1.1 cgd loop:
340 1.1 cgd sum->ift_ip = 0;
341 1.1 cgd sum->ift_ie = 0;
342 1.1 cgd sum->ift_op = 0;
343 1.1 cgd sum->ift_oe = 0;
344 1.1 cgd sum->ift_co = 0;
345 1.1 cgd sum->ift_dr = 0;
346 1.1 cgd for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
347 1.8 mycroft if (kread(off, (char *)&ifnet, sizeof ifnet)) {
348 1.8 mycroft off = 0;
349 1.8 mycroft continue;
350 1.8 mycroft }
351 1.1 cgd if (ip == interesting) {
352 1.1 cgd printf("%8d %5d %8d %5d %5d",
353 1.1 cgd ifnet.if_ipackets - ip->ift_ip,
354 1.1 cgd ifnet.if_ierrors - ip->ift_ie,
355 1.1 cgd ifnet.if_opackets - ip->ift_op,
356 1.1 cgd ifnet.if_oerrors - ip->ift_oe,
357 1.1 cgd ifnet.if_collisions - ip->ift_co);
358 1.1 cgd if (dflag)
359 1.1 cgd printf(" %5d",
360 1.1 cgd ifnet.if_snd.ifq_drops - ip->ift_dr);
361 1.1 cgd }
362 1.1 cgd ip->ift_ip = ifnet.if_ipackets;
363 1.1 cgd ip->ift_ie = ifnet.if_ierrors;
364 1.1 cgd ip->ift_op = ifnet.if_opackets;
365 1.1 cgd ip->ift_oe = ifnet.if_oerrors;
366 1.1 cgd ip->ift_co = ifnet.if_collisions;
367 1.1 cgd ip->ift_dr = ifnet.if_snd.ifq_drops;
368 1.1 cgd sum->ift_ip += ip->ift_ip;
369 1.1 cgd sum->ift_ie += ip->ift_ie;
370 1.1 cgd sum->ift_op += ip->ift_op;
371 1.1 cgd sum->ift_oe += ip->ift_oe;
372 1.1 cgd sum->ift_co += ip->ift_co;
373 1.1 cgd sum->ift_dr += ip->ift_dr;
374 1.10 mycroft off = (u_long)ifnet.if_list.tqe_next;
375 1.1 cgd }
376 1.1 cgd if (lastif - iftot > 0) {
377 1.1 cgd printf(" %8d %5d %8d %5d %5d",
378 1.1 cgd sum->ift_ip - total->ift_ip,
379 1.1 cgd sum->ift_ie - total->ift_ie,
380 1.1 cgd sum->ift_op - total->ift_op,
381 1.1 cgd sum->ift_oe - total->ift_oe,
382 1.1 cgd sum->ift_co - total->ift_co);
383 1.1 cgd if (dflag)
384 1.1 cgd printf(" %5d", sum->ift_dr - total->ift_dr);
385 1.1 cgd }
386 1.1 cgd *total = *sum;
387 1.1 cgd putchar('\n');
388 1.1 cgd fflush(stdout);
389 1.1 cgd line++;
390 1.1 cgd oldmask = sigblock(sigmask(SIGALRM));
391 1.1 cgd if (! signalled) {
392 1.1 cgd sigpause(0);
393 1.1 cgd }
394 1.1 cgd sigsetmask(oldmask);
395 1.1 cgd signalled = NO;
396 1.1 cgd (void)alarm(interval);
397 1.1 cgd if (line == 21)
398 1.1 cgd goto banner;
399 1.1 cgd goto loop;
400 1.1 cgd /*NOTREACHED*/
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd /*
404 1.1 cgd * Called if an interval expires before sidewaysintpr has completed a loop.
405 1.1 cgd * Sets a flag to not wait for the alarm.
406 1.1 cgd */
407 1.8 mycroft static void
408 1.8 mycroft catchalarm(signo)
409 1.8 mycroft int signo;
410 1.1 cgd {
411 1.1 cgd signalled = YES;
412 1.1 cgd }
413