if.c revision 1.104 1 1.104 msaitoh /* $NetBSD: if.c,v 1.104 2022/09/12 02:25:44 msaitoh 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.55 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.25 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.13 thorpej #if 0
35 1.13 thorpej static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
36 1.13 thorpej #else
37 1.104 msaitoh __RCSID("$NetBSD: if.c,v 1.104 2022/09/12 02:25:44 msaitoh Exp $");
38 1.13 thorpej #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.64 elad #include <sys/param.h>
42 1.1 cgd #include <sys/types.h>
43 1.8 mycroft #include <sys/protosw.h>
44 1.1 cgd #include <sys/socket.h>
45 1.57 ragge #include <sys/time.h>
46 1.64 elad #include <sys/sysctl.h>
47 1.82 mrg #include <sys/ioctl.h>
48 1.1 cgd
49 1.1 cgd #include <net/if.h>
50 1.1 cgd #include <net/if_dl.h>
51 1.20 thorpej #include <net/if_types.h>
52 1.64 elad #include <net/route.h>
53 1.1 cgd #include <netinet/in.h>
54 1.1 cgd #include <netinet/in_var.h>
55 1.8 mycroft #include <arpa/inet.h>
56 1.1 cgd
57 1.59 rpaulo #include <kvm.h>
58 1.8 mycroft #include <signal.h>
59 1.1 cgd #include <stdio.h>
60 1.42 matt #include <stdlib.h>
61 1.8 mycroft #include <string.h>
62 1.8 mycroft #include <unistd.h>
63 1.34 itojun #include <netdb.h>
64 1.64 elad #include <err.h>
65 1.8 mycroft
66 1.8 mycroft #include "netstat.h"
67 1.80 christos #include "rtutil.h"
68 1.70 pooka #include "prog_ops.h"
69 1.1 cgd
70 1.64 elad #define MAXIF 100
71 1.64 elad
72 1.68 pooka #define HUMBUF_SIZE 7
73 1.68 pooka
74 1.64 elad struct iftot {
75 1.64 elad char ift_name[IFNAMSIZ]; /* interface name */
76 1.64 elad u_quad_t ift_ip; /* input packets */
77 1.64 elad u_quad_t ift_ib; /* input bytes */
78 1.64 elad u_quad_t ift_ie; /* input errors */
79 1.101 msaitoh u_quad_t ift_iq; /* input drops */
80 1.64 elad u_quad_t ift_op; /* output packets */
81 1.64 elad u_quad_t ift_ob; /* output bytes */
82 1.64 elad u_quad_t ift_oe; /* output errors */
83 1.101 msaitoh u_quad_t ift_oq; /* output drops */
84 1.64 elad u_quad_t ift_co; /* collisions */
85 1.101 msaitoh };
86 1.101 msaitoh
87 1.101 msaitoh struct if_data_ext {
88 1.101 msaitoh uint64_t ifi_oqdrops;
89 1.64 elad };
90 1.64 elad
91 1.82 mrg static void set_lines(void);
92 1.91 ozaki static void print_addr(const int, struct sockaddr *, struct sockaddr **,
93 1.101 msaitoh struct if_data *, struct ifnet *, struct if_data_ext *);
94 1.64 elad static void sidewaysintpr(u_int, u_long);
95 1.64 elad
96 1.64 elad static void iftot_banner(struct iftot *);
97 1.64 elad static void iftot_print_sum(struct iftot *, struct iftot *);
98 1.64 elad static void iftot_print(struct iftot *, struct iftot *);
99 1.1 cgd
100 1.87 christos static void catchalarm(int);
101 1.64 elad static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
102 1.64 elad static void fetchifs(void);
103 1.64 elad
104 1.101 msaitoh static int if_data_ext_get(const char *, struct if_data_ext *);
105 1.64 elad static void intpr_sysctl(void);
106 1.64 elad static void intpr_kvm(u_long, void (*)(const char *));
107 1.64 elad
108 1.64 elad struct iftot iftot[MAXIF], ip_cur, ip_old, sum_cur, sum_old;
109 1.90 dholland static sig_atomic_t signalled; /* set when alarm goes off */
110 1.32 itojun
111 1.82 mrg static unsigned redraw_lines = 21;
112 1.82 mrg
113 1.82 mrg static void
114 1.82 mrg set_lines(void)
115 1.82 mrg {
116 1.82 mrg static bool first = true;
117 1.82 mrg struct ttysize ts;
118 1.82 mrg
119 1.82 mrg if (!first)
120 1.82 mrg return;
121 1.82 mrg first = false;
122 1.82 mrg if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1 && ts.ts_lines)
123 1.82 mrg redraw_lines = ts.ts_lines - 3;
124 1.82 mrg }
125 1.82 mrg
126 1.82 mrg
127 1.1 cgd /*
128 1.1 cgd * Print a description of the network interfaces.
129 1.15 thorpej * NOTE: ifnetaddr is the location of the kernel global "ifnet",
130 1.15 thorpej * which is a TAILQ_HEAD.
131 1.1 cgd */
132 1.8 mycroft void
133 1.74 matt intpr(int interval, u_long ifnetaddr, void (*pfunc)(const char *))
134 1.1 cgd {
135 1.64 elad
136 1.64 elad if (interval) {
137 1.64 elad sidewaysintpr((unsigned)interval, ifnetaddr);
138 1.64 elad return;
139 1.64 elad }
140 1.64 elad
141 1.99 msaitoh if (use_sysctl)
142 1.64 elad intpr_sysctl();
143 1.99 msaitoh else
144 1.64 elad intpr_kvm(ifnetaddr, pfunc);
145 1.64 elad }
146 1.64 elad
147 1.64 elad static void
148 1.64 elad intpr_header(void)
149 1.64 elad {
150 1.64 elad
151 1.83 christos if (!sflag && !pflag) {
152 1.64 elad if (bflag) {
153 1.64 elad printf("%-5.5s %-5.5s %-13.13s %-17.17s "
154 1.98 msaitoh "%10.10s %10.10s",
155 1.98 msaitoh "Name", "Mtu", "Network", "Address",
156 1.98 msaitoh "Ibytes", "Obytes");
157 1.64 elad } else {
158 1.64 elad printf("%-5.5s %-5.5s %-13.13s %-17.17s "
159 1.95 msaitoh "%8.8s %5.5s",
160 1.95 msaitoh "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
161 1.95 msaitoh if (dflag)
162 1.95 msaitoh printf(" %6.6s", "Idrops");
163 1.95 msaitoh printf(" %8.8s %5.5s %5.5s",
164 1.95 msaitoh "Opkts", "Oerrs", "Colls");
165 1.64 elad }
166 1.95 msaitoh if (dflag)
167 1.95 msaitoh printf(" %6.6s", "Odrops");
168 1.64 elad if (tflag)
169 1.64 elad printf(" %4.4s", "Time");
170 1.64 elad putchar('\n');
171 1.64 elad }
172 1.64 elad }
173 1.64 elad
174 1.101 msaitoh int
175 1.101 msaitoh if_data_ext_get(const char *ifname, struct if_data_ext *dext)
176 1.101 msaitoh {
177 1.101 msaitoh char namebuf[1024];
178 1.101 msaitoh size_t len;
179 1.101 msaitoh int drops;
180 1.101 msaitoh
181 1.101 msaitoh /* For sysctl */
182 1.101 msaitoh snprintf(namebuf, sizeof(namebuf),
183 1.101 msaitoh "net.interfaces.%s.sndq.drops", ifname);
184 1.101 msaitoh len = sizeof(drops);
185 1.101 msaitoh if (sysctlbyname(namebuf, &drops, &len, NULL, 0)
186 1.101 msaitoh == -1) {
187 1.101 msaitoh warnx("'%s' not found", namebuf);
188 1.101 msaitoh dext->ifi_oqdrops = 0;
189 1.101 msaitoh return -1;
190 1.101 msaitoh } else
191 1.101 msaitoh dext->ifi_oqdrops = drops;
192 1.101 msaitoh
193 1.101 msaitoh return 0;
194 1.101 msaitoh }
195 1.101 msaitoh
196 1.64 elad static void
197 1.64 elad intpr_sysctl(void)
198 1.64 elad {
199 1.64 elad struct if_msghdr *ifm;
200 1.64 elad int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
201 1.93 mrg static char *buf = NULL;
202 1.93 mrg static size_t olen;
203 1.93 mrg char *next, *lim, *cp;
204 1.64 elad struct rt_msghdr *rtm;
205 1.64 elad struct ifa_msghdr *ifam;
206 1.64 elad struct if_data *ifd = NULL;
207 1.64 elad struct sockaddr *sa, *rti_info[RTAX_MAX];
208 1.64 elad struct sockaddr_dl *sdl;
209 1.64 elad uint64_t total = 0;
210 1.64 elad size_t len;
211 1.84 christos int did = 1, rtax = 0, n;
212 1.64 elad char name[IFNAMSIZ + 1]; /* + 1 for `*' */
213 1.101 msaitoh char origname[IFNAMSIZ]; /* without `*' */
214 1.91 ozaki int ifindex = 0;
215 1.64 elad
216 1.70 pooka if (prog_sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
217 1.64 elad err(1, "sysctl");
218 1.93 mrg if (len > olen) {
219 1.93 mrg free(buf);
220 1.93 mrg if ((buf = malloc(len)) == NULL)
221 1.93 mrg err(1, NULL);
222 1.93 mrg olen = len;
223 1.93 mrg }
224 1.70 pooka if (prog_sysctl(mib, 6, buf, &len, NULL, 0) == -1)
225 1.64 elad err(1, "sysctl");
226 1.64 elad
227 1.64 elad intpr_header();
228 1.64 elad
229 1.64 elad lim = buf + len;
230 1.64 elad for (next = buf; next < lim; next += rtm->rtm_msglen) {
231 1.101 msaitoh struct if_data_ext dext;
232 1.101 msaitoh
233 1.64 elad rtm = (struct rt_msghdr *)next;
234 1.64 elad if (rtm->rtm_version != RTM_VERSION)
235 1.64 elad continue;
236 1.64 elad switch (rtm->rtm_type) {
237 1.64 elad case RTM_IFINFO:
238 1.64 elad total = 0;
239 1.64 elad ifm = (struct if_msghdr *)next;
240 1.64 elad ifd = &ifm->ifm_data;
241 1.64 elad
242 1.64 elad sa = (struct sockaddr *)(ifm + 1);
243 1.64 elad get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
244 1.64 elad
245 1.64 elad sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
246 1.99 msaitoh if (sdl == NULL || sdl->sdl_family != AF_LINK)
247 1.64 elad continue;
248 1.99 msaitoh
249 1.64 elad bzero(name, sizeof(name));
250 1.64 elad if (sdl->sdl_nlen >= IFNAMSIZ)
251 1.64 elad memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
252 1.98 msaitoh else if (sdl->sdl_nlen > 0)
253 1.64 elad memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
254 1.64 elad
255 1.97 msaitoh if (interface != NULL && strcmp(name, interface) != 0)
256 1.64 elad continue;
257 1.64 elad
258 1.91 ozaki ifindex = sdl->sdl_index;
259 1.91 ozaki
260 1.101 msaitoh /* Keep the original name */
261 1.101 msaitoh strcpy(origname, name);
262 1.101 msaitoh
263 1.101 msaitoh /* Mark inactive interfaces with a '*' */
264 1.64 elad cp = strchr(name, '\0');
265 1.101 msaitoh if ((ifm->ifm_flags & IFF_UP) == 0) {
266 1.64 elad *cp++ = '*';
267 1.101 msaitoh *cp = '\0';
268 1.101 msaitoh }
269 1.64 elad
270 1.64 elad if (qflag) {
271 1.64 elad total = ifd->ifi_ibytes + ifd->ifi_obytes +
272 1.64 elad ifd->ifi_ipackets + ifd->ifi_ierrors +
273 1.64 elad ifd->ifi_opackets + ifd->ifi_oerrors +
274 1.64 elad ifd->ifi_collisions;
275 1.64 elad if (dflag)
276 1.88 christos total += ifd->ifi_iqdrops;
277 1.64 elad if (total == 0)
278 1.64 elad continue;
279 1.64 elad }
280 1.84 christos /* Skip the first one */
281 1.84 christos if (did) {
282 1.84 christos did = 0;
283 1.84 christos continue;
284 1.84 christos }
285 1.84 christos rtax = RTAX_IFP;
286 1.64 elad break;
287 1.64 elad case RTM_NEWADDR:
288 1.64 elad if (qflag && total == 0)
289 1.64 elad continue;
290 1.97 msaitoh if (interface != NULL && strcmp(name, interface) != 0)
291 1.64 elad continue;
292 1.64 elad ifam = (struct ifa_msghdr *)next;
293 1.64 elad if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
294 1.64 elad RTA_BRD)) == 0)
295 1.64 elad break;
296 1.64 elad
297 1.64 elad sa = (struct sockaddr *)(ifam + 1);
298 1.64 elad
299 1.64 elad get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
300 1.84 christos rtax = RTAX_IFA;
301 1.84 christos did = 1;
302 1.64 elad break;
303 1.84 christos default:
304 1.84 christos continue;
305 1.64 elad }
306 1.84 christos if (vflag)
307 1.84 christos n = strlen(name) < 5 ? 5 : strlen(name);
308 1.84 christos else
309 1.84 christos n = 5;
310 1.84 christos
311 1.84 christos printf("%-*.*s %-5" PRIu64 " ", n, n, name, ifd->ifi_mtu);
312 1.102 msaitoh if (dflag)
313 1.101 msaitoh if_data_ext_get(origname, &dext);
314 1.101 msaitoh
315 1.101 msaitoh print_addr(ifindex, rti_info[rtax], rti_info, ifd,
316 1.101 msaitoh NULL, dflag ? &dext : NULL);
317 1.64 elad }
318 1.64 elad }
319 1.64 elad
320 1.73 christos union ifaddr_u {
321 1.73 christos struct ifaddr ifa;
322 1.73 christos struct in_ifaddr in;
323 1.73 christos #ifdef INET6
324 1.73 christos struct in6_ifaddr in6;
325 1.73 christos #endif /* INET6 */
326 1.73 christos };
327 1.73 christos
328 1.64 elad static void
329 1.100 msaitoh ifname_to_ifdata(int s, const char *ifname, struct if_data * const ifd)
330 1.96 thorpej {
331 1.100 msaitoh struct ifdatareq ifdr;
332 1.96 thorpej
333 1.96 thorpej memset(ifd, 0, sizeof(*ifd));
334 1.100 msaitoh strlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
335 1.100 msaitoh if (ioctl(s, SIOCGIFDATA, &ifdr) != 0)
336 1.100 msaitoh return;
337 1.100 msaitoh memcpy(ifd, &ifdr.ifdr_data, sizeof(ifdr.ifdr_data));
338 1.96 thorpej }
339 1.96 thorpej
340 1.96 thorpej static void
341 1.64 elad intpr_kvm(u_long ifnetaddr, void (*pfunc)(const char *))
342 1.64 elad {
343 1.1 cgd struct ifnet ifnet;
344 1.96 thorpej struct if_data ifd;
345 1.73 christos union ifaddr_u ifaddr;
346 1.7 cgd u_long ifaddraddr;
347 1.15 thorpej struct ifnet_head ifhead; /* TAILQ_HEAD */
348 1.43 enami char name[IFNAMSIZ + 1]; /* + 1 for `*' */
349 1.100 msaitoh int s;
350 1.1 cgd
351 1.1 cgd if (ifnetaddr == 0) {
352 1.1 cgd printf("ifnet: symbol not defined\n");
353 1.1 cgd return;
354 1.1 cgd }
355 1.15 thorpej
356 1.15 thorpej /*
357 1.15 thorpej * Find the pointer to the first ifnet structure. Replace
358 1.15 thorpej * the pointer to the TAILQ_HEAD with the actual pointer
359 1.15 thorpej * to the first list element.
360 1.15 thorpej */
361 1.15 thorpej if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
362 1.8 mycroft return;
363 1.15 thorpej ifnetaddr = (u_long)ifhead.tqh_first;
364 1.15 thorpej
365 1.100 msaitoh if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
366 1.100 msaitoh return;
367 1.100 msaitoh
368 1.64 elad intpr_header();
369 1.64 elad
370 1.1 cgd ifaddraddr = 0;
371 1.1 cgd while (ifnetaddr || ifaddraddr) {
372 1.25 lukem char *cp;
373 1.64 elad int n;
374 1.1 cgd
375 1.1 cgd if (ifaddraddr == 0) {
376 1.15 thorpej if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
377 1.8 mycroft return;
378 1.25 lukem memmove(name, ifnet.if_xname, IFNAMSIZ);
379 1.15 thorpej name[IFNAMSIZ - 1] = '\0'; /* sanity */
380 1.10 mycroft ifnetaddr = (u_long)ifnet.if_list.tqe_next;
381 1.97 msaitoh if (interface != NULL && strcmp(name, interface) != 0)
382 1.1 cgd continue;
383 1.25 lukem cp = strchr(name, '\0');
384 1.34 itojun
385 1.34 itojun if (pfunc) {
386 1.34 itojun (*pfunc)(name);
387 1.34 itojun continue;
388 1.34 itojun }
389 1.34 itojun
390 1.10 mycroft if ((ifnet.if_flags & IFF_UP) == 0)
391 1.1 cgd *cp++ = '*';
392 1.1 cgd *cp = '\0';
393 1.10 mycroft ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
394 1.1 cgd }
395 1.41 itojun if (vflag)
396 1.41 itojun n = strlen(name) < 5 ? 5 : strlen(name);
397 1.41 itojun else
398 1.41 itojun n = 5;
399 1.41 itojun printf("%-*.*s %-5llu ", n, n, name,
400 1.33 bouyer (unsigned long long)ifnet.if_mtu);
401 1.1 cgd if (ifaddraddr == 0) {
402 1.21 christos printf("%-13.13s ", "none");
403 1.23 mikel printf("%-17.17s ", "none");
404 1.1 cgd } else {
405 1.64 elad struct sockaddr *sa;
406 1.64 elad
407 1.98 msaitoh if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr))
408 1.98 msaitoh {
409 1.8 mycroft ifaddraddr = 0;
410 1.8 mycroft continue;
411 1.8 mycroft }
412 1.1 cgd #define CP(x) ((char *)(x))
413 1.8 mycroft cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
414 1.56 itojun CP(&ifaddr);
415 1.56 itojun sa = (struct sockaddr *)cp;
416 1.100 msaitoh ifname_to_ifdata(s, name, &ifd);
417 1.91 ozaki print_addr(ifnet.if_index, sa, (void *)&ifaddr,
418 1.101 msaitoh &ifd, &ifnet, NULL);
419 1.64 elad }
420 1.64 elad ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
421 1.64 elad }
422 1.100 msaitoh close(s);
423 1.64 elad }
424 1.64 elad
425 1.64 elad static void
426 1.91 ozaki mc_print(const int ifindex, const size_t ias, const char *oid, int *mcast_oids,
427 1.86 christos void (*pr)(const void *))
428 1.84 christos {
429 1.85 christos uint8_t *mcast_addrs, *p;
430 1.86 christos const size_t incr = 2 * ias + sizeof(uint32_t);
431 1.85 christos size_t len;
432 1.85 christos
433 1.85 christos if (mcast_oids[0] == 0) {
434 1.86 christos size_t oidlen = 4;
435 1.86 christos if (sysctlnametomib(oid, mcast_oids, &oidlen) == -1) {
436 1.86 christos warnx("'%s' not found", oid);
437 1.85 christos return;
438 1.85 christos }
439 1.85 christos if (oidlen != 3) {
440 1.86 christos warnx("Wrong OID path for '%s'", oid);
441 1.85 christos return;
442 1.85 christos }
443 1.85 christos }
444 1.86 christos
445 1.86 christos if (mcast_oids[3] == ifindex)
446 1.86 christos return;
447 1.85 christos mcast_oids[3] = ifindex;
448 1.85 christos
449 1.85 christos mcast_addrs = asysctl(mcast_oids, 4, &len);
450 1.85 christos if (mcast_addrs == NULL && len != 0) {
451 1.86 christos warn("failed to read '%s'", oid);
452 1.85 christos return;
453 1.85 christos }
454 1.85 christos if (len) {
455 1.85 christos p = mcast_addrs;
456 1.85 christos while (len >= incr) {
457 1.86 christos (*pr)((p + ias));
458 1.85 christos p += incr;
459 1.85 christos len -= incr;
460 1.85 christos }
461 1.85 christos }
462 1.85 christos free(mcast_addrs);
463 1.85 christos }
464 1.85 christos
465 1.86 christos #ifdef INET6
466 1.86 christos static void
467 1.86 christos ia6_print(const struct in6_addr *ia)
468 1.86 christos {
469 1.86 christos struct sockaddr_in6 as6;
470 1.86 christos char hbuf[NI_MAXHOST]; /* for getnameinfo() */
471 1.86 christos int n;
472 1.86 christos
473 1.86 christos memset(&as6, 0, sizeof(as6));
474 1.86 christos as6.sin6_len = sizeof(struct sockaddr_in6);
475 1.86 christos as6.sin6_family = AF_INET6;
476 1.86 christos as6.sin6_addr = *ia;
477 1.86 christos inet6_getscopeid(&as6, INET6_IS_ADDR_MC_LINKLOCAL);
478 1.86 christos if (getnameinfo((struct sockaddr *)&as6, as6.sin6_len, hbuf,
479 1.86 christos sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
480 1.86 christos strlcpy(hbuf, "??", sizeof(hbuf));
481 1.86 christos }
482 1.86 christos if (vflag)
483 1.86 christos n = strlen(hbuf) < 17 ? 17 : strlen(hbuf);
484 1.86 christos else
485 1.86 christos n = 17;
486 1.86 christos printf("\n%25s %-*.*s ", "", n, n, hbuf);
487 1.86 christos }
488 1.86 christos
489 1.86 christos static void
490 1.91 ozaki mc6_print(const int ifindex)
491 1.86 christos {
492 1.86 christos static int mcast_oids[4];
493 1.86 christos
494 1.91 ozaki mc_print(ifindex, sizeof(struct in6_addr), "net.inet6.multicast",
495 1.86 christos mcast_oids, (void (*)(const void *))ia6_print);
496 1.86 christos }
497 1.86 christos #endif
498 1.86 christos
499 1.85 christos static void
500 1.85 christos ia4_print(const struct in_addr *ia)
501 1.85 christos {
502 1.85 christos printf("\n%25s %-17.17s ", "", routename4(ia->s_addr, nflag));
503 1.85 christos }
504 1.85 christos
505 1.85 christos static void
506 1.91 ozaki mc4_print(const int ifindex)
507 1.85 christos {
508 1.85 christos static int mcast_oids[4];
509 1.85 christos
510 1.91 ozaki mc_print(ifindex, sizeof(struct in_addr), "net.inet.multicast",
511 1.86 christos mcast_oids, (void (*)(const void *))ia4_print);
512 1.85 christos }
513 1.85 christos
514 1.85 christos static void
515 1.101 msaitoh print_addr(const int ifindex, struct sockaddr *sa, struct sockaddr **rtinfo,
516 1.101 msaitoh struct if_data *ifd, struct ifnet *ifnet, struct if_data_ext *dext)
517 1.64 elad {
518 1.64 elad char hexsep = '.'; /* for hexprint */
519 1.64 elad static const char hexfmt[] = "%02x%c"; /* for hexprint */
520 1.64 elad char hbuf[NI_MAXHOST]; /* for getnameinfo() */
521 1.64 elad #ifdef INET6
522 1.64 elad const int niflag = NI_NUMERICHOST;
523 1.67 plunky struct sockaddr_in6 *sin6, *netmask6;
524 1.64 elad #endif
525 1.81 christos struct sockaddr_in netmask;
526 1.64 elad struct sockaddr_in *sin;
527 1.64 elad char *cp;
528 1.64 elad int n, m;
529 1.64 elad
530 1.64 elad switch (sa->sa_family) {
531 1.64 elad case AF_UNSPEC:
532 1.64 elad printf("%-13.13s ", "none");
533 1.64 elad printf("%-17.17s ", "none");
534 1.64 elad break;
535 1.64 elad case AF_INET:
536 1.64 elad sin = (struct sockaddr_in *)sa;
537 1.64 elad if (use_sysctl) {
538 1.98 msaitoh netmask =
539 1.98 msaitoh *((struct sockaddr_in *)rtinfo[RTAX_NETMASK]);
540 1.64 elad } else {
541 1.64 elad struct in_ifaddr *ifaddr_in = (void *)rtinfo;
542 1.81 christos netmask.sin_addr.s_addr = ifaddr_in->ia_subnetmask;
543 1.64 elad }
544 1.81 christos cp = netname4(sin, &netmask, nflag);
545 1.64 elad if (vflag)
546 1.64 elad n = strlen(cp) < 13 ? 13 : strlen(cp);
547 1.64 elad else
548 1.64 elad n = 13;
549 1.64 elad printf("%-*.*s ", n, n, cp);
550 1.80 christos cp = routename4(sin->sin_addr.s_addr, nflag);
551 1.64 elad if (vflag)
552 1.64 elad n = strlen(cp) < 17 ? 17 : strlen(cp);
553 1.64 elad else
554 1.64 elad n = 17;
555 1.64 elad printf("%-*.*s ", n, n, cp);
556 1.64 elad
557 1.84 christos if (!aflag)
558 1.84 christos break;
559 1.84 christos if (ifnet) {
560 1.64 elad u_long multiaddr;
561 1.64 elad struct in_multi inm;
562 1.73 christos union ifaddr_u *ifaddr = (union ifaddr_u *)rtinfo;
563 1.64 elad
564 1.85 christos multiaddr = (u_long)ifaddr->in.ia_multiaddrs.lh_first;
565 1.64 elad while (multiaddr != 0) {
566 1.85 christos kread(multiaddr, (char *)&inm, sizeof inm);
567 1.85 christos ia4_print(&inm.inm_addr);
568 1.85 christos multiaddr = (u_long)inm.inm_list.le_next;
569 1.64 elad }
570 1.99 msaitoh } else
571 1.91 ozaki mc4_print(ifindex);
572 1.64 elad break;
573 1.32 itojun #ifdef INET6
574 1.64 elad case AF_INET6:
575 1.64 elad sin6 = (struct sockaddr_in6 *)sa;
576 1.79 christos inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
577 1.54 itojun #ifdef __KAME__
578 1.78 christos if (!vflag)
579 1.78 christos sin6->sin6_scope_id = 0;
580 1.34 itojun #endif
581 1.64 elad
582 1.99 msaitoh if (use_sysctl)
583 1.64 elad netmask6 = (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK];
584 1.99 msaitoh else {
585 1.64 elad struct in6_ifaddr *ifaddr_in6 = (void *)rtinfo;
586 1.64 elad netmask6 = &ifaddr_in6->ia_prefixmask;
587 1.64 elad }
588 1.64 elad
589 1.80 christos cp = netname6(sin6, netmask6, nflag);
590 1.64 elad if (vflag)
591 1.64 elad n = strlen(cp) < 13 ? 13 : strlen(cp);
592 1.64 elad else
593 1.64 elad n = 13;
594 1.64 elad printf("%-*.*s ", n, n, cp);
595 1.64 elad if (getnameinfo((struct sockaddr *)sin6,
596 1.64 elad sin6->sin6_len,
597 1.64 elad hbuf, sizeof(hbuf), NULL, 0,
598 1.64 elad niflag) != 0) {
599 1.64 elad strlcpy(hbuf, "?", sizeof(hbuf));
600 1.64 elad }
601 1.64 elad cp = hbuf;
602 1.64 elad if (vflag)
603 1.64 elad n = strlen(cp) < 17 ? 17 : strlen(cp);
604 1.64 elad else
605 1.64 elad n = 17;
606 1.64 elad printf("%-*.*s ", n, n, cp);
607 1.64 elad
608 1.98 msaitoh if (!aflag)
609 1.84 christos break;
610 1.84 christos if (ifnet) {
611 1.64 elad u_long multiaddr;
612 1.64 elad struct in6_multi inm;
613 1.73 christos union ifaddr_u *ifaddr = (union ifaddr_u *)rtinfo;
614 1.98 msaitoh
615 1.94 ozaki multiaddr = (u_long)ifaddr->in6._ia6_multiaddrs.lh_first;
616 1.64 elad while (multiaddr != 0) {
617 1.84 christos kread(multiaddr, (char *)&inm, sizeof inm);
618 1.84 christos ia6_print(&inm.in6m_addr);
619 1.84 christos multiaddr = (u_long)inm.in6m_entry.le_next;
620 1.1 cgd }
621 1.99 msaitoh } else
622 1.91 ozaki mc6_print(ifindex);
623 1.64 elad break;
624 1.64 elad #endif /*INET6*/
625 1.64 elad #ifndef SMALL
626 1.64 elad case AF_APPLETALK:
627 1.98 msaitoh printf("atalk:%-7.7s ", atalk_print(sa, 0x10));
628 1.98 msaitoh printf("%-17.17s ", atalk_print(sa, 0x0b));
629 1.64 elad break;
630 1.64 elad #endif
631 1.64 elad case AF_LINK:
632 1.64 elad printf("%-13.13s ", "<Link>");
633 1.64 elad if (getnameinfo(sa, sa->sa_len,
634 1.64 elad hbuf, sizeof(hbuf), NULL, 0,
635 1.64 elad NI_NUMERICHOST) != 0) {
636 1.64 elad strlcpy(hbuf, "?", sizeof(hbuf));
637 1.26 kml }
638 1.64 elad cp = hbuf;
639 1.64 elad if (vflag)
640 1.64 elad n = strlen(cp) < 17 ? 17 : strlen(cp);
641 1.64 elad else
642 1.64 elad n = 17;
643 1.64 elad printf("%-*.*s ", n, n, cp);
644 1.64 elad break;
645 1.64 elad
646 1.64 elad default:
647 1.64 elad m = printf("(%d)", sa->sa_family);
648 1.64 elad for (cp = sa->sa_len + (char *)sa;
649 1.64 elad --cp > sa->sa_data && (*cp == 0);) {}
650 1.64 elad n = cp - sa->sa_data + 1;
651 1.64 elad cp = sa->sa_data;
652 1.64 elad
653 1.64 elad while (--n >= 0)
654 1.64 elad m += printf(hexfmt, *cp++ & 0xff,
655 1.64 elad n > 0 ? hexsep : ' ');
656 1.64 elad m = 32 - m;
657 1.64 elad while (m-- > 0)
658 1.64 elad putchar(' ');
659 1.64 elad break;
660 1.64 elad }
661 1.64 elad
662 1.64 elad if (bflag) {
663 1.68 pooka char humbuf[HUMBUF_SIZE];
664 1.68 pooka
665 1.68 pooka if (hflag && humanize_number(humbuf, sizeof(humbuf),
666 1.68 pooka ifd->ifi_ibytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
667 1.68 pooka printf("%10s ", humbuf);
668 1.68 pooka else
669 1.68 pooka printf("%10llu ", (unsigned long long)ifd->ifi_ibytes);
670 1.68 pooka
671 1.68 pooka if (hflag && humanize_number(humbuf, sizeof(humbuf),
672 1.68 pooka ifd->ifi_obytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
673 1.68 pooka printf("%10s", humbuf);
674 1.68 pooka else
675 1.68 pooka printf("%10llu", (unsigned long long)ifd->ifi_obytes);
676 1.64 elad } else {
677 1.95 msaitoh printf("%8llu %5llu",
678 1.64 elad (unsigned long long)ifd->ifi_ipackets,
679 1.95 msaitoh (unsigned long long)ifd->ifi_ierrors);
680 1.95 msaitoh if (dflag)
681 1.95 msaitoh printf(" %6" PRIu64, ifd->ifi_iqdrops);
682 1.95 msaitoh printf(" %8llu %5llu %5llu",
683 1.64 elad (unsigned long long)ifd->ifi_opackets,
684 1.64 elad (unsigned long long)ifd->ifi_oerrors,
685 1.64 elad (unsigned long long)ifd->ifi_collisions);
686 1.1 cgd }
687 1.95 msaitoh if (dflag)
688 1.95 msaitoh printf(" %6lld", ifnet ?
689 1.101 msaitoh (unsigned long long)ifnet->if_snd.ifq_drops :
690 1.101 msaitoh dext->ifi_oqdrops);
691 1.64 elad if (tflag)
692 1.73 christos printf(" %4d", ifnet ? ifnet->if_timer : 0);
693 1.64 elad putchar('\n');
694 1.64 elad }
695 1.64 elad
696 1.64 elad static void
697 1.64 elad iftot_banner(struct iftot *ift)
698 1.64 elad {
699 1.64 elad if (bflag)
700 1.64 elad printf("%7.7s in %8.8s %6.6s out %5.5s",
701 1.64 elad ift->ift_name, " ",
702 1.64 elad ift->ift_name, " ");
703 1.64 elad else
704 1.64 elad printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
705 1.64 elad ift->ift_name, " ",
706 1.64 elad ift->ift_name, " ", " ");
707 1.64 elad if (dflag)
708 1.64 elad printf(" %5.5s", " ");
709 1.64 elad
710 1.64 elad if (bflag)
711 1.64 elad printf(" %7.7s in %8.8s %6.6s out %5.5s",
712 1.64 elad "total", " ", "total", " ");
713 1.64 elad else
714 1.64 elad printf(" %5.5s in %5.5s%5.5s out %5.5s %5.5s",
715 1.64 elad "total", " ", "total", " ", " ");
716 1.64 elad if (dflag)
717 1.64 elad printf(" %5.5s", " ");
718 1.64 elad putchar('\n');
719 1.64 elad if (bflag)
720 1.64 elad printf("%10.10s %8.8s %10.10s %5.5s",
721 1.64 elad "bytes", " ", "bytes", " ");
722 1.64 elad else
723 1.64 elad printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
724 1.64 elad "packets", "errs", "packets", "errs", "colls");
725 1.64 elad if (dflag)
726 1.64 elad printf(" %5.5s", "drops");
727 1.64 elad
728 1.64 elad if (bflag)
729 1.64 elad printf(" %10.10s %8.8s %10.10s %5.5s",
730 1.64 elad "bytes", " ", "bytes", " ");
731 1.64 elad else
732 1.64 elad printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
733 1.64 elad "packets", "errs", "packets", "errs", "colls");
734 1.64 elad if (dflag)
735 1.64 elad printf(" %5.5s", "drops");
736 1.64 elad putchar('\n');
737 1.64 elad fflush(stdout);
738 1.1 cgd }
739 1.1 cgd
740 1.64 elad static void
741 1.64 elad iftot_print(struct iftot *cur, struct iftot *old)
742 1.64 elad {
743 1.64 elad if (bflag)
744 1.75 msaitoh printf("%10" PRIu64 " %8.8s %10" PRIu64 " %5.5s",
745 1.66 pgoyette cur->ift_ib - old->ift_ib, " ",
746 1.66 pgoyette cur->ift_ob - old->ift_ob, " ");
747 1.64 elad else
748 1.75 msaitoh printf("%8" PRIu64 " %5" PRIu64 " %8" PRIu64 " %5" PRIu64 " %5" PRIu64,
749 1.66 pgoyette cur->ift_ip - old->ift_ip,
750 1.66 pgoyette cur->ift_ie - old->ift_ie,
751 1.66 pgoyette cur->ift_op - old->ift_op,
752 1.66 pgoyette cur->ift_oe - old->ift_oe,
753 1.66 pgoyette cur->ift_co - old->ift_co);
754 1.64 elad if (dflag)
755 1.101 msaitoh printf(" %5" PRIu64, cur->ift_oq - old->ift_oq);
756 1.64 elad }
757 1.64 elad
758 1.64 elad static void
759 1.64 elad iftot_print_sum(struct iftot *cur, struct iftot *old)
760 1.64 elad {
761 1.64 elad if (bflag)
762 1.75 msaitoh printf(" %10" PRIu64 " %8.8s %10" PRIu64 " %5.5s",
763 1.66 pgoyette cur->ift_ib - old->ift_ib, " ",
764 1.66 pgoyette cur->ift_ob - old->ift_ob, " ");
765 1.64 elad else
766 1.75 msaitoh printf(" %8" PRIu64 " %5" PRIu64 " %8" PRIu64 " %5" PRIu64 " %5" PRIu64,
767 1.66 pgoyette cur->ift_ip - old->ift_ip,
768 1.66 pgoyette cur->ift_ie - old->ift_ie,
769 1.66 pgoyette cur->ift_op - old->ift_op,
770 1.66 pgoyette cur->ift_oe - old->ift_oe,
771 1.66 pgoyette cur->ift_co - old->ift_co);
772 1.64 elad
773 1.64 elad if (dflag)
774 1.101 msaitoh printf(" %5" PRIu64, cur->ift_oq - old->ift_oq);
775 1.64 elad }
776 1.64 elad
777 1.72 joerg __dead static void
778 1.64 elad sidewaysintpr_sysctl(unsigned interval)
779 1.64 elad {
780 1.90 dholland struct itimerval it;
781 1.64 elad sigset_t emptyset;
782 1.90 dholland sigset_t noalrm;
783 1.82 mrg unsigned line;
784 1.82 mrg
785 1.82 mrg set_lines();
786 1.64 elad
787 1.64 elad fetchifs();
788 1.64 elad if (ip_cur.ift_name[0] == '\0') {
789 1.64 elad fprintf(stderr, "%s: %s: unknown interface\n",
790 1.64 elad getprogname(), interface);
791 1.64 elad exit(1);
792 1.64 elad }
793 1.64 elad
794 1.90 dholland sigemptyset(&emptyset);
795 1.90 dholland sigemptyset(&noalrm);
796 1.90 dholland sigaddset(&noalrm, SIGALRM);
797 1.90 dholland sigprocmask(SIG_SETMASK, &noalrm, NULL);
798 1.90 dholland
799 1.90 dholland signalled = 0;
800 1.64 elad (void)signal(SIGALRM, catchalarm);
801 1.90 dholland
802 1.90 dholland it.it_interval.tv_sec = it.it_value.tv_sec = interval;
803 1.90 dholland it.it_interval.tv_usec = it.it_value.tv_usec = 0;
804 1.90 dholland setitimer(ITIMER_REAL, &it, NULL);
805 1.90 dholland
806 1.64 elad banner:
807 1.64 elad iftot_banner(&ip_cur);
808 1.64 elad
809 1.64 elad line = 0;
810 1.64 elad bzero(&ip_old, sizeof(ip_old));
811 1.64 elad bzero(&sum_old, sizeof(sum_old));
812 1.64 elad loop:
813 1.64 elad bzero(&sum_cur, sizeof(sum_cur));
814 1.64 elad
815 1.64 elad fetchifs();
816 1.64 elad
817 1.64 elad iftot_print(&ip_cur, &ip_old);
818 1.64 elad
819 1.64 elad ip_old = ip_cur;
820 1.64 elad
821 1.64 elad iftot_print_sum(&sum_cur, &sum_old);
822 1.64 elad
823 1.64 elad sum_old = sum_cur;
824 1.1 cgd
825 1.64 elad putchar('\n');
826 1.64 elad fflush(stdout);
827 1.64 elad line++;
828 1.98 msaitoh if (signalled == 0)
829 1.64 elad sigsuspend(&emptyset);
830 1.98 msaitoh
831 1.64 elad signalled = 0;
832 1.82 mrg if (line == redraw_lines)
833 1.64 elad goto banner;
834 1.64 elad goto loop;
835 1.64 elad /*NOTREACHED*/
836 1.64 elad }
837 1.1 cgd
838 1.8 mycroft static void
839 1.64 elad sidewaysintpr_kvm(unsigned interval, u_long off)
840 1.1 cgd {
841 1.57 ragge struct itimerval it;
842 1.90 dholland sigset_t emptyset;
843 1.90 dholland sigset_t noalrm;
844 1.1 cgd struct ifnet ifnet;
845 1.96 thorpej struct if_data ifd;
846 1.7 cgd u_long firstifnet;
847 1.25 lukem struct iftot *ip, *total;
848 1.82 mrg unsigned line;
849 1.1 cgd struct iftot *lastif, *sum, *interesting;
850 1.15 thorpej struct ifnet_head ifhead; /* TAILQ_HEAD */
851 1.100 msaitoh int s;
852 1.1 cgd
853 1.82 mrg set_lines();
854 1.82 mrg
855 1.15 thorpej /*
856 1.15 thorpej * Find the pointer to the first ifnet structure. Replace
857 1.15 thorpej * the pointer to the TAILQ_HEAD with the actual pointer
858 1.15 thorpej * to the first list element.
859 1.15 thorpej */
860 1.15 thorpej if (kread(off, (char *)&ifhead, sizeof ifhead))
861 1.8 mycroft return;
862 1.15 thorpej firstifnet = (u_long)ifhead.tqh_first;
863 1.15 thorpej
864 1.100 msaitoh if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
865 1.100 msaitoh return;
866 1.100 msaitoh
867 1.1 cgd lastif = iftot;
868 1.1 cgd sum = iftot + MAXIF - 1;
869 1.1 cgd total = sum - 1;
870 1.17 cgd interesting = (interface == NULL) ? iftot : NULL;
871 1.1 cgd for (off = firstifnet, ip = iftot; off;) {
872 1.8 mycroft if (kread(off, (char *)&ifnet, sizeof ifnet))
873 1.8 mycroft break;
874 1.25 lukem memset(ip->ift_name, 0, sizeof(ip->ift_name));
875 1.31 mycroft snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
876 1.19 thorpej if (interface && strcmp(ifnet.if_xname, interface) == 0)
877 1.1 cgd interesting = ip;
878 1.1 cgd ip++;
879 1.1 cgd if (ip >= iftot + MAXIF - 2)
880 1.1 cgd break;
881 1.10 mycroft off = (u_long)ifnet.if_list.tqe_next;
882 1.17 cgd }
883 1.17 cgd if (interesting == NULL) {
884 1.17 cgd fprintf(stderr, "%s: %s: unknown interface\n",
885 1.47 cgd getprogname(), interface);
886 1.17 cgd exit(1);
887 1.1 cgd }
888 1.1 cgd lastif = ip;
889 1.1 cgd
890 1.90 dholland sigemptyset(&emptyset);
891 1.90 dholland sigemptyset(&noalrm);
892 1.90 dholland sigaddset(&noalrm, SIGALRM);
893 1.90 dholland sigprocmask(SIG_SETMASK, &noalrm, NULL);
894 1.90 dholland
895 1.90 dholland signalled = 0;
896 1.1 cgd (void)signal(SIGALRM, catchalarm);
897 1.57 ragge
898 1.57 ragge it.it_interval.tv_sec = it.it_value.tv_sec = interval;
899 1.57 ragge it.it_interval.tv_usec = it.it_value.tv_usec = 0;
900 1.57 ragge setitimer(ITIMER_REAL, &it, NULL);
901 1.57 ragge
902 1.1 cgd banner:
903 1.31 mycroft if (bflag)
904 1.31 mycroft printf("%7.7s in %8.8s %6.6s out %5.5s",
905 1.31 mycroft interesting->ift_name, " ",
906 1.31 mycroft interesting->ift_name, " ");
907 1.31 mycroft else
908 1.31 mycroft printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
909 1.31 mycroft interesting->ift_name, " ",
910 1.31 mycroft interesting->ift_name, " ", " ");
911 1.31 mycroft if (dflag)
912 1.31 mycroft printf(" %5.5s", " ");
913 1.1 cgd if (lastif - iftot > 0) {
914 1.31 mycroft if (bflag)
915 1.31 mycroft printf(" %7.7s in %8.8s %6.6s out %5.5s",
916 1.31 mycroft "total", " ", "total", " ");
917 1.31 mycroft else
918 1.31 mycroft printf(" %5.5s in %5.5s%5.5s out %5.5s %5.5s",
919 1.31 mycroft "total", " ", "total", " ", " ");
920 1.1 cgd if (dflag)
921 1.31 mycroft printf(" %5.5s", " ");
922 1.1 cgd }
923 1.1 cgd for (ip = iftot; ip < iftot + MAXIF; ip++) {
924 1.1 cgd ip->ift_ip = 0;
925 1.26 kml ip->ift_ib = 0;
926 1.1 cgd ip->ift_ie = 0;
927 1.104 msaitoh ip->ift_iq = 0;
928 1.1 cgd ip->ift_op = 0;
929 1.26 kml ip->ift_ob = 0;
930 1.1 cgd ip->ift_oe = 0;
931 1.104 msaitoh ip->ift_oq = 0;
932 1.1 cgd ip->ift_co = 0;
933 1.1 cgd }
934 1.1 cgd putchar('\n');
935 1.31 mycroft if (bflag)
936 1.26 kml printf("%10.10s %8.8s %10.10s %5.5s",
937 1.31 mycroft "bytes", " ", "bytes", " ");
938 1.31 mycroft else
939 1.31 mycroft printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
940 1.31 mycroft "packets", "errs", "packets", "errs", "colls");
941 1.1 cgd if (dflag)
942 1.31 mycroft printf(" %5.5s", "drops");
943 1.29 ross if (lastif - iftot > 0) {
944 1.31 mycroft if (bflag)
945 1.31 mycroft printf(" %10.10s %8.8s %10.10s %5.5s",
946 1.31 mycroft "bytes", " ", "bytes", " ");
947 1.31 mycroft else
948 1.31 mycroft printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
949 1.31 mycroft "packets", "errs", "packets", "errs", "colls");
950 1.31 mycroft if (dflag)
951 1.31 mycroft printf(" %5.5s", "drops");
952 1.29 ross }
953 1.1 cgd putchar('\n');
954 1.1 cgd fflush(stdout);
955 1.1 cgd line = 0;
956 1.1 cgd loop:
957 1.1 cgd sum->ift_ip = 0;
958 1.26 kml sum->ift_ib = 0;
959 1.1 cgd sum->ift_ie = 0;
960 1.104 msaitoh sum->ift_iq = 0;
961 1.1 cgd sum->ift_op = 0;
962 1.26 kml sum->ift_ob = 0;
963 1.1 cgd sum->ift_oe = 0;
964 1.104 msaitoh sum->ift_oq = 0;
965 1.1 cgd sum->ift_co = 0;
966 1.1 cgd for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
967 1.8 mycroft if (kread(off, (char *)&ifnet, sizeof ifnet)) {
968 1.8 mycroft off = 0;
969 1.8 mycroft continue;
970 1.8 mycroft }
971 1.100 msaitoh ifname_to_ifdata(s, ip->ift_name, &ifd);
972 1.1 cgd if (ip == interesting) {
973 1.26 kml if (bflag) {
974 1.68 pooka char humbuf[HUMBUF_SIZE];
975 1.68 pooka
976 1.68 pooka if (hflag && humanize_number(humbuf,
977 1.68 pooka sizeof(humbuf),
978 1.96 thorpej ifd.ifi_ibytes - ip->ift_ib, "",
979 1.68 pooka HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
980 1.68 pooka printf("%10s %8.8s ", humbuf, " ");
981 1.68 pooka else
982 1.98 msaitoh printf("%10llu %8.8s ",
983 1.68 pooka (unsigned long long)
984 1.96 thorpej (ifd.ifi_ibytes-ip->ift_ib), " ");
985 1.68 pooka
986 1.68 pooka if (hflag && humanize_number(humbuf,
987 1.68 pooka sizeof(humbuf),
988 1.96 thorpej ifd.ifi_obytes - ip->ift_ob, "",
989 1.68 pooka HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
990 1.68 pooka printf("%10s %5.5s", humbuf, " ");
991 1.68 pooka else
992 1.98 msaitoh printf("%10llu %5.5s",
993 1.68 pooka (unsigned long long)
994 1.96 thorpej (ifd.ifi_obytes-ip->ift_ob), " ");
995 1.26 kml } else {
996 1.33 bouyer printf("%8llu %5llu %8llu %5llu %5llu",
997 1.33 bouyer (unsigned long long)
998 1.96 thorpej (ifd.ifi_ipackets - ip->ift_ip),
999 1.33 bouyer (unsigned long long)
1000 1.96 thorpej (ifd.ifi_ierrors - ip->ift_ie),
1001 1.33 bouyer (unsigned long long)
1002 1.96 thorpej (ifd.ifi_opackets - ip->ift_op),
1003 1.33 bouyer (unsigned long long)
1004 1.96 thorpej (ifd.ifi_oerrors - ip->ift_oe),
1005 1.33 bouyer (unsigned long long)
1006 1.96 thorpej (ifd.ifi_collisions - ip->ift_co));
1007 1.26 kml }
1008 1.1 cgd if (dflag)
1009 1.89 christos printf(" %5" PRIu64,
1010 1.101 msaitoh ifnet.if_snd.ifq_drops - ip->ift_oq);
1011 1.1 cgd }
1012 1.96 thorpej ip->ift_ip = ifd.ifi_ipackets;
1013 1.96 thorpej ip->ift_ib = ifd.ifi_ibytes;
1014 1.96 thorpej ip->ift_ie = ifd.ifi_ierrors;
1015 1.96 thorpej ip->ift_op = ifd.ifi_opackets;
1016 1.96 thorpej ip->ift_ob = ifd.ifi_obytes;
1017 1.96 thorpej ip->ift_oe = ifd.ifi_oerrors;
1018 1.96 thorpej ip->ift_co = ifd.ifi_collisions;
1019 1.101 msaitoh ip->ift_oq = ifnet.if_snd.ifq_drops;
1020 1.1 cgd sum->ift_ip += ip->ift_ip;
1021 1.26 kml sum->ift_ib += ip->ift_ib;
1022 1.1 cgd sum->ift_ie += ip->ift_ie;
1023 1.1 cgd sum->ift_op += ip->ift_op;
1024 1.26 kml sum->ift_ob += ip->ift_ob;
1025 1.1 cgd sum->ift_oe += ip->ift_oe;
1026 1.1 cgd sum->ift_co += ip->ift_co;
1027 1.101 msaitoh sum->ift_oq += ip->ift_oq;
1028 1.10 mycroft off = (u_long)ifnet.if_list.tqe_next;
1029 1.1 cgd }
1030 1.1 cgd if (lastif - iftot > 0) {
1031 1.26 kml if (bflag) {
1032 1.68 pooka char humbuf[HUMBUF_SIZE];
1033 1.68 pooka
1034 1.68 pooka if (hflag && humanize_number(humbuf,
1035 1.68 pooka sizeof(humbuf), sum->ift_ib - total->ift_ib, "",
1036 1.68 pooka HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
1037 1.69 enami printf(" %10s %8.8s ", humbuf, " ");
1038 1.68 pooka else
1039 1.98 msaitoh printf(" %10llu %8.8s ",
1040 1.68 pooka (unsigned long long)
1041 1.68 pooka (sum->ift_ib - total->ift_ib), " ");
1042 1.68 pooka
1043 1.68 pooka if (hflag && humanize_number(humbuf,
1044 1.68 pooka sizeof(humbuf), sum->ift_ob - total->ift_ob, "",
1045 1.68 pooka HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
1046 1.68 pooka printf("%10s %5.5s", humbuf, " ");
1047 1.68 pooka else
1048 1.98 msaitoh printf("%10llu %5.5s",
1049 1.68 pooka (unsigned long long)
1050 1.68 pooka (sum->ift_ob - total->ift_ob), " ");
1051 1.26 kml } else {
1052 1.33 bouyer printf(" %8llu %5llu %8llu %5llu %5llu",
1053 1.33 bouyer (unsigned long long)
1054 1.33 bouyer (sum->ift_ip - total->ift_ip),
1055 1.33 bouyer (unsigned long long)
1056 1.33 bouyer (sum->ift_ie - total->ift_ie),
1057 1.33 bouyer (unsigned long long)
1058 1.33 bouyer (sum->ift_op - total->ift_op),
1059 1.33 bouyer (unsigned long long)
1060 1.33 bouyer (sum->ift_oe - total->ift_oe),
1061 1.33 bouyer (unsigned long long)
1062 1.33 bouyer (sum->ift_co - total->ift_co));
1063 1.26 kml }
1064 1.1 cgd if (dflag)
1065 1.33 bouyer printf(" %5llu",
1066 1.101 msaitoh (unsigned long long)(sum->ift_oq - total->ift_oq));
1067 1.1 cgd }
1068 1.1 cgd *total = *sum;
1069 1.1 cgd putchar('\n');
1070 1.1 cgd fflush(stdout);
1071 1.1 cgd line++;
1072 1.98 msaitoh if (signalled == 0)
1073 1.90 dholland sigsuspend(&emptyset);
1074 1.98 msaitoh
1075 1.90 dholland signalled = 0;
1076 1.82 mrg if (line == redraw_lines)
1077 1.1 cgd goto banner;
1078 1.1 cgd goto loop;
1079 1.1 cgd /*NOTREACHED*/
1080 1.1 cgd }
1081 1.1 cgd
1082 1.1 cgd /*
1083 1.64 elad * Print a running summary of interface statistics.
1084 1.64 elad * Repeat display every interval seconds, showing statistics
1085 1.64 elad * collected over that interval. Assumes that interval is non-zero.
1086 1.64 elad * First line printed at top of screen is always cumulative.
1087 1.64 elad */
1088 1.64 elad static void
1089 1.74 matt sidewaysintpr(unsigned int interval, u_long off)
1090 1.64 elad {
1091 1.64 elad
1092 1.98 msaitoh if (use_sysctl)
1093 1.64 elad sidewaysintpr_sysctl(interval);
1094 1.98 msaitoh else
1095 1.64 elad sidewaysintpr_kvm(interval, off);
1096 1.64 elad }
1097 1.64 elad
1098 1.64 elad /*
1099 1.1 cgd * Called if an interval expires before sidewaysintpr has completed a loop.
1100 1.1 cgd * Sets a flag to not wait for the alarm.
1101 1.1 cgd */
1102 1.8 mycroft static void
1103 1.74 matt catchalarm(int signo)
1104 1.1 cgd {
1105 1.28 mrg
1106 1.64 elad signalled = true;
1107 1.64 elad }
1108 1.64 elad
1109 1.64 elad static void
1110 1.64 elad get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
1111 1.98 msaitoh {
1112 1.64 elad int i;
1113 1.64 elad
1114 1.64 elad for (i = 0; i < RTAX_MAX; i++) {
1115 1.64 elad if (addrs & (1 << i)) {
1116 1.64 elad rti_info[i] = sa;
1117 1.71 martin sa = (struct sockaddr *)((char *)(sa) +
1118 1.71 martin RT_ROUNDUP(sa->sa_len));
1119 1.98 msaitoh } else
1120 1.64 elad rti_info[i] = NULL;
1121 1.64 elad }
1122 1.64 elad }
1123 1.64 elad
1124 1.64 elad static void
1125 1.64 elad fetchifs(void)
1126 1.64 elad {
1127 1.64 elad struct if_msghdr *ifm;
1128 1.64 elad int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
1129 1.64 elad struct rt_msghdr *rtm;
1130 1.64 elad struct if_data *ifd = NULL;
1131 1.64 elad struct sockaddr *sa, *rti_info[RTAX_MAX];
1132 1.64 elad struct sockaddr_dl *sdl;
1133 1.93 mrg static char *buf = NULL;
1134 1.93 mrg static size_t olen;
1135 1.101 msaitoh struct if_data_ext dext;
1136 1.93 mrg char *next, *lim;
1137 1.64 elad char name[IFNAMSIZ];
1138 1.64 elad size_t len;
1139 1.64 elad
1140 1.70 pooka if (prog_sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
1141 1.64 elad err(1, "sysctl");
1142 1.93 mrg if (len > olen) {
1143 1.93 mrg free(buf);
1144 1.93 mrg if ((buf = malloc(len)) == NULL)
1145 1.93 mrg err(1, NULL);
1146 1.93 mrg olen = len;
1147 1.93 mrg }
1148 1.70 pooka if (prog_sysctl(mib, 6, buf, &len, NULL, 0) == -1)
1149 1.64 elad err(1, "sysctl");
1150 1.64 elad
1151 1.101 msaitoh memset(&dext, 0, sizeof(dext));
1152 1.64 elad lim = buf + len;
1153 1.64 elad for (next = buf; next < lim; next += rtm->rtm_msglen) {
1154 1.64 elad rtm = (struct rt_msghdr *)next;
1155 1.64 elad if (rtm->rtm_version != RTM_VERSION)
1156 1.64 elad continue;
1157 1.64 elad switch (rtm->rtm_type) {
1158 1.64 elad case RTM_IFINFO:
1159 1.64 elad ifm = (struct if_msghdr *)next;
1160 1.64 elad ifd = &ifm->ifm_data;
1161 1.64 elad
1162 1.64 elad sa = (struct sockaddr *)(ifm + 1);
1163 1.64 elad get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
1164 1.64 elad
1165 1.64 elad sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
1166 1.64 elad if (sdl == NULL || sdl->sdl_family != AF_LINK)
1167 1.64 elad continue;
1168 1.64 elad bzero(name, sizeof(name));
1169 1.64 elad if (sdl->sdl_nlen >= IFNAMSIZ)
1170 1.64 elad memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
1171 1.98 msaitoh else if (sdl->sdl_nlen > 0)
1172 1.64 elad memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
1173 1.64 elad
1174 1.101 msaitoh if_data_ext_get(name, &dext);
1175 1.101 msaitoh
1176 1.97 msaitoh if (interface != NULL && !strcmp(name, interface)) {
1177 1.64 elad strlcpy(ip_cur.ift_name, name,
1178 1.64 elad sizeof(ip_cur.ift_name));
1179 1.64 elad ip_cur.ift_ip = ifd->ifi_ipackets;
1180 1.64 elad ip_cur.ift_ib = ifd->ifi_ibytes;
1181 1.64 elad ip_cur.ift_ie = ifd->ifi_ierrors;
1182 1.64 elad ip_cur.ift_op = ifd->ifi_opackets;
1183 1.64 elad ip_cur.ift_ob = ifd->ifi_obytes;
1184 1.64 elad ip_cur.ift_oe = ifd->ifi_oerrors;
1185 1.64 elad ip_cur.ift_co = ifd->ifi_collisions;
1186 1.101 msaitoh ip_cur.ift_iq = ifd->ifi_iqdrops;
1187 1.103 msaitoh ip_cur.ift_oq = dext.ifi_oqdrops;
1188 1.64 elad }
1189 1.64 elad
1190 1.64 elad sum_cur.ift_ip += ifd->ifi_ipackets;
1191 1.64 elad sum_cur.ift_ib += ifd->ifi_ibytes;
1192 1.64 elad sum_cur.ift_ie += ifd->ifi_ierrors;
1193 1.64 elad sum_cur.ift_op += ifd->ifi_opackets;
1194 1.64 elad sum_cur.ift_ob += ifd->ifi_obytes;
1195 1.64 elad sum_cur.ift_oe += ifd->ifi_oerrors;
1196 1.64 elad sum_cur.ift_co += ifd->ifi_collisions;
1197 1.101 msaitoh sum_cur.ift_iq += ifd->ifi_iqdrops;
1198 1.101 msaitoh sum_cur.ift_oq += dext.ifi_oqdrops;
1199 1.64 elad break;
1200 1.64 elad }
1201 1.64 elad }
1202 1.64 elad if (interface == NULL) {
1203 1.64 elad strlcpy(ip_cur.ift_name, name,
1204 1.64 elad sizeof(ip_cur.ift_name));
1205 1.64 elad ip_cur.ift_ip = ifd->ifi_ipackets;
1206 1.64 elad ip_cur.ift_ib = ifd->ifi_ibytes;
1207 1.64 elad ip_cur.ift_ie = ifd->ifi_ierrors;
1208 1.64 elad ip_cur.ift_op = ifd->ifi_opackets;
1209 1.64 elad ip_cur.ift_ob = ifd->ifi_obytes;
1210 1.64 elad ip_cur.ift_oe = ifd->ifi_oerrors;
1211 1.64 elad ip_cur.ift_co = ifd->ifi_collisions;
1212 1.101 msaitoh ip_cur.ift_iq = ifd->ifi_iqdrops;
1213 1.101 msaitoh ip_cur.ift_oq = dext.ifi_oqdrops;
1214 1.64 elad }
1215 1.1 cgd }
1216