Debian Codegraph @

ifconfig/system/bsd.c

raw
1 /* bsd.c -- BSD specific code for ifconfig
2 Copyright (C) 2001-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include <unistd.h>
22 #include "../ifconfig.h"
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <net/if.h>
26 #include <net/if_types.h>
27 #include <net/if_dl.h>
28 #include <net/if_media.h>
29 #ifdef HAVE_NETINET_ETHER_H
30 # include <netinet/ether.h>
31 #endif
32 #include <netinet/if_ether.h>
33 #include <ifaddrs.h>
34
35 #include <attribute.h>
36
37
38 /* Output format stuff. */
39
40 const char *system_default_format = "unix";
41
42
43 /* Argument parsing stuff. */
44
45 const char *system_help = "\
46 NAME [ADDR [DSTADDR]] [broadcast BRDADDR] [netmask MASK] " "[metric N] [mtu N] [up|down]";
47
48 struct argp_child system_argp_child;
49
50 int
51 system_parse_opt (struct ifconfig **ifp MAYBE_UNUSED,
52 char option MAYBE_UNUSED, char *optarg MAYBE_UNUSED)
53 {
54 return 0;
55 }
56
57 int
58 system_parse_opt_rest (struct ifconfig **ifp, int argc, char *argv[])
59 {
60 int i = 0, mask, rev;
61 enum
62 {
63 EXPECT_NOTHING,
64 EXPECT_COMMAND,
65 EXPECT_AF,
66 EXPECT_BROADCAST,
67 EXPECT_NETMASK,
68 EXPECT_METRIC,
69 EXPECT_MTU
70 } expect = EXPECT_COMMAND;
71
72 *ifp = parse_opt_new_ifs (argv[0]);
73
74 while (++i < argc)
75 {
76 switch (expect)
77 {
78 case EXPECT_BROADCAST:
79 parse_opt_set_brdaddr (*ifp, argv[i]);
80 break;
81
82 case EXPECT_NETMASK:
83 parse_opt_set_netmask (*ifp, argv[i]);
84 break;
85
86 case EXPECT_MTU:
87 parse_opt_set_mtu (*ifp, argv[i]);
88 break;
89
90 case EXPECT_METRIC:
91 parse_opt_set_metric (*ifp, argv[i]);
92 break;
93
94 case EXPECT_COMMAND:
95 expect = EXPECT_AF; /* Applicable at creation. */
96 if (!strcmp (argv[i], "create"))
97 {
98 error (0, 0, "interface creation is not supported");
99 return 0;
100 }
101 else if (!strcmp (argv[i], "destroy"))
102 {
103 error (0, 0, "interface destruction is not supported");
104 return 0;
105 }
106 break;
107
108 case EXPECT_AF:
109 case EXPECT_NOTHING:
110 break;
111 }
112
113 if (expect == EXPECT_AF) /* Address selection is single shot. */
114 {
115 expect = EXPECT_NOTHING;
116 if (!strcmp (argv[i], "inet"))
117 continue;
118 else if (!strcmp (argv[i], "inet6"))
119 {
120 error (0, 0, "%s is not a supported address family", argv[i]);
121 return 0;
122 }
123 }
124
125 if (expect != EXPECT_NOTHING)
126 expect = EXPECT_NOTHING;
127 else if (!strcmp (argv[i], "broadcast"))
128 expect = EXPECT_BROADCAST;
129 else if (!strcmp (argv[i], "netmask"))
130 expect = EXPECT_NETMASK;
131 else if (!strcmp (argv[i], "metric"))
132 expect = EXPECT_METRIC;
133 else if (!strcmp (argv[i], "mtu"))
134 expect = EXPECT_MTU;
135 else if (!strcmp (argv[i], "up"))
136 parse_opt_set_flag (*ifp, IFF_UP | IFF_RUNNING, 0);
137 else if (!strcmp (argv[i], "down"))
138 parse_opt_set_flag (*ifp, IFF_UP, 1);
139 else if (((mask = if_nameztoflag (argv[i], &rev))
140 & ~IU_IFF_CANTCHANGE) != 0)
141 parse_opt_set_flag (*ifp, mask, rev);
142 else
143 {
144 /* Also alias, -alias. */
145 if (!((*ifp)->valid & IF_VALID_ADDR))
146 parse_opt_set_address (*ifp, argv[i]);
147 else if (!((*ifp)->valid & IF_VALID_DSTADDR))
148 parse_opt_set_dstaddr (*ifp, argv[i]);
149 }
150 }
151
152 switch (expect)
153 {
154 case EXPECT_BROADCAST:
155 error (0, 0, "option `broadcast' requires an argument");
156 break;
157
158 case EXPECT_NETMASK:
159 error (0, 0, "option `netmask' requires an argument");
160 break;
161
162 case EXPECT_METRIC:
163 error (0, 0, "option `metric' requires an argument");
164 break;
165
166 case EXPECT_MTU:
167 error (0, 0, "option `mtu' requires an argument");
168 break;
169
170 case EXPECT_AF: /* dummy */
171 case EXPECT_COMMAND: /* dummy */
172 case EXPECT_NOTHING:
173 return 1;
174 }
175 return 0;
176 }
177
178 int
179 system_preconfigure (int sfd MAYBE_UNUSED, struct ifreq *ifr MAYBE_UNUSED)
180 {
181 return 0;
182 }
183
184 int
185 system_configure (int sfd MAYBE_UNUSED,
186 struct ifreq *ifr MAYBE_UNUSED,
187 struct system_ifconfig *ifs MAYBE_UNUSED)
188 {
189 return 0;
190 }
191
192
193 /* System hooks. */
194 static struct ifaddrs *ifp = NULL;
195
196 #ifdef SIOCGIFMEDIA
197 struct ifmediareq ifm;
198 #endif /* SIOCGIFMEDIA */
199
200 #define ESTABLISH_IFADDRS \
201 if (!ifp) \
202 getifaddrs (&ifp);
203
204 void
205 system_fh_ifstat_query (format_data_t form, int argc, char *argv[])
206 {
207 /* Flush an existing interface list, thus renewing statistics. */
208 if (ifp)
209 {
210 freeifaddrs (ifp);
211 ifp = NULL;
212 }
213
214 select_arg (form, argc, argv, getifaddrs (&ifp) ? 1 : 0);
215 }
216
217 struct if_nameindex *(*system_if_nameindex) (void) = if_nameindex;
218
219 static void
220 print_hwaddr_ether (format_data_t form, unsigned char *data)
221 {
222 put_string (form, ether_ntoa ((struct ether_addr *) data));
223 had_output = 1;
224 }
225
226 struct ift_symbol
227 {
228 const char *name;
229 const char *title;
230 int value;
231 void (*print_hwaddr) (format_data_t form, unsigned char *data);
232 } ift_symbols[] = {
233 #ifdef IFT_ETHER /* Ethernet CSMA/CD */
234 # ifdef ETHERNAME
235 {"ETHER", ETHERNAME, IFT_ETHER, print_hwaddr_ether},
236 # else
237 {"ETHER", "ether", IFT_ETHER, print_hwaddr_ether},
238 # endif/* !ETHERNAME */
239 #endif /* IFT_ETHER */
240 #ifdef IFT_GIF /* Generic tunnel (gif) */
241 {"IPGIF", "IPIP tunnel", IFT_GIF, NULL},
242 #endif
243 #ifdef IFT_FAITH /* IPv6-to-IPv4 TCP relay capture */
244 {"FAITH", "TCP relay capture", IFT_FAITH, NULL},
245 #endif
246 #ifdef IFT_LOOP /* Local loopback */
247 {"LOOPBACK", "Local loopback", IFT_LOOP, NULL},
248 #endif
249 #ifdef IFT_PFLOG /* Packet filter logging */
250 {"PFLOG", "Packet filter logger", IFT_PFLOG, NULL},
251 #endif
252 #ifdef IFT_PFSYNC /* Packet filter state synching */
253 {"PFSYNC", "Packet filter state synching", IFT_PFSYNC, NULL},
254 #endif
255 #ifdef IFT_PPP /* Point-to-Point serial protocol */
256 {"PPP", "Point-to-Point over serial", IFT_PPP, NULL},
257 #endif
258 #ifdef IFT_SLIP /* IP over generic TTY */
259 {"SLIP", "Serial line IP", IFT_SLIP, NULL},
260 #endif
261 #ifdef IFT_TUNNEL /* Encapsulation (gre) */
262 {"IPGRE", "GRE over IP", IFT_TUNNEL, NULL},
263 #endif
264 {NULL, NULL, 0, NULL}
265 };
266
267 static struct ift_symbol *
268 ift_findvalue (int value)
269 {
270 struct ift_symbol *ift = ift_symbols;
271
272 while (ift->name != NULL)
273 {
274 if (ift->value == value)
275 break;
276 ift++;
277 }
278
279 if (ift->name)
280 return ift;
281 else
282 return NULL;
283 }
284
285 void
286 system_fh_brdaddr_query (format_data_t form, int argc, char *argv[])
287 {
288 ESTABLISH_IFADDRS if (!ifp)
289 select_arg (form, argc, argv, 1);
290 else
291 {
292 int missing = 1;
293 struct ifaddrs *fp;
294
295 for (fp = ifp; fp; fp = fp->ifa_next)
296 {
297 if (fp->ifa_addr->sa_family != AF_INET ||
298 strcmp (fp->ifa_name, form->ifr->ifr_name))
299 continue;
300
301 if ((fp->ifa_flags & IFF_BROADCAST) && fp->ifa_broadaddr)
302 missing = 0;
303 break;
304 }
305 select_arg (form, argc, argv, missing);
306 }
307 }
308
309 void
310 system_fh_brdaddr (format_data_t form, int argc, char *argv[])
311 {
312 ESTABLISH_IFADDRS if (!ifp)
313 put_string (form, "(unknown)");
314 else
315 {
316 int missing = 1;
317 struct ifaddrs *fp;
318
319 for (fp = ifp; fp; fp = fp->ifa_next)
320 {
321 if (fp->ifa_addr->sa_family != AF_INET ||
322 strcmp (fp->ifa_name, form->ifr->ifr_name))
323 continue;
324
325 if ((fp->ifa_flags & IFF_BROADCAST) && fp->ifa_broadaddr)
326 {
327 missing = 0;
328 put_addr (form, argc, argv, fp->ifa_broadaddr);
329 }
330 break;
331 }
332 if (missing)
333 put_string (form, "(unknown)");
334 }
335 }
336
337 void
338 system_fh_hwaddr_query (format_data_t form, int argc, char *argv[])
339 {
340 ESTABLISH_IFADDRS if (!ifp)
341 select_arg (form, argc, argv, 1);
342 else
343 {
344 int missing = 1;
345 struct ifaddrs *fp;
346
347 for (fp = ifp; fp; fp = fp->ifa_next)
348 {
349 struct sockaddr_dl *dl;
350
351 if (fp->ifa_addr->sa_family != AF_LINK ||
352 strcmp (fp->ifa_name, form->ifr->ifr_name))
353 continue;
354
355 dl = (struct sockaddr_dl *) fp->ifa_addr;
356 if (dl && (dl->sdl_len > 0))
357 {
358 struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
359
360 if (ift && ift->print_hwaddr)
361 missing = 0;
362 }
363 break;
364 }
365
366 select_arg (form, argc, argv, missing);
367 }
368 }
369
370 void
371 system_fh_hwaddr (format_data_t form, int argc MAYBE_UNUSED,
372 MAYBE_UNUSED char *argv[])
373 {
374 ESTABLISH_IFADDRS if (!ifp)
375 put_string (form, "(hwaddr unknown)");
376 else
377 {
378 int missing = 1;
379 struct ifaddrs *fp;
380
381 for (fp = ifp; fp; fp = fp->ifa_next)
382 {
383 struct sockaddr_dl *dl;
384
385 if (fp->ifa_addr->sa_family != AF_LINK ||
386 strcmp (fp->ifa_name, form->ifr->ifr_name))
387 continue;
388
389 dl = (struct sockaddr_dl *) fp->ifa_addr;
390 if (dl && (dl->sdl_len > 0))
391 {
392 struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
393
394 if (ift && ift->print_hwaddr)
395 {
396 missing = 0;
397 ift->print_hwaddr (form, (unsigned char *) LLADDR (dl));
398 }
399 }
400 break;
401 }
402 if (missing)
403 put_string (form, "(hwaddr unknown)");
404 }
405 }
406
407 void
408 system_fh_hwtype_query (format_data_t form, int argc, char *argv[])
409 {
410 int missing = 1;
411
412 ESTABLISH_IFADDRS if (ifp)
413 {
414 struct ifaddrs *fp;
415 struct sockaddr_dl *dl = NULL;
416
417 for (fp = ifp; fp; fp = fp->ifa_next)
418 {
419 if (fp->ifa_addr->sa_family != AF_LINK ||
420 strcmp (fp->ifa_name, form->ifr->ifr_name))
421 continue;
422
423 dl = (struct sockaddr_dl *) fp->ifa_addr;
424 if (dl && (dl->sdl_len > 0))
425 {
426 struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
427
428 if (ift && ift->title)
429 missing = 0;
430 }
431 break;
432 }
433 }
434
435 select_arg (form, argc, argv, missing);
436 }
437
438 void
439 system_fh_hwtype (format_data_t form, int argc MAYBE_UNUSED,
440 MAYBE_UNUSED char *argv[])
441 {
442 int found = 0;
443
444 ESTABLISH_IFADDRS if (ifp)
445 {
446 struct ifaddrs *fp;
447 struct sockaddr_dl *dl = NULL;
448
449 for (fp = ifp; fp; fp = fp->ifa_next)
450 {
451 if (fp->ifa_addr->sa_family != AF_LINK ||
452 strcmp (fp->ifa_name, form->ifr->ifr_name))
453 continue;
454
455 dl = (struct sockaddr_dl *) fp->ifa_addr;
456 if (dl && (dl->sdl_len > 0))
457 {
458 struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
459
460 if (ift && ift->title)
461 {
462 found = 1;
463 put_string (form, ift->title);
464 }
465 }
466 break;
467 }
468 }
469
470 if (!found)
471 put_string (form, "(unknown hwtype)");
472 }
473
474 /* Lookup structures provided by the system, each decoding
475 * to clear text the meaning of media and status flags.
476 *
477 * The naming is different in NetBSD and in FreeBSD.
478 */
479 static const struct ifmedia_description media_descr[] = IFM_TYPE_DESCRIPTIONS;
480
481 #ifdef IFM_SUBTYPE_DESCRIPTIONS
482 static const struct ifmedia_description subtype_descr[] =
483 IFM_SUBTYPE_DESCRIPTIONS;
484 #endif
485
486 #ifdef IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
487 static const struct ifmedia_description ethernet_descr[] =
488 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
489 #endif
490
491 #ifdef IFM_SUBTYPE_SHARED_DESCRIPTIONS
492 static const struct ifmedia_description subtype_shared_descr[] =
493 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
494 #endif
495
496 static const struct ifmedia_description option_descr[] =
497 #ifdef IFM_OPTION_DESCRIPTIONS
498 IFM_OPTION_DESCRIPTIONS;
499 #elif defined IFM_SHARED_OPTION_DESCRIPTIONS
500 IFM_SHARED_OPTION_DESCRIPTIONS;
501 #else
502 { {0, NULL}, };
503 #endif
504
505
506 #ifdef IFM_STATUS_DESCRIPTIONS
507 static const struct ifmedia_status_description status_descr[] =
508 IFM_STATUS_DESCRIPTIONS;
509 #endif
510
511 void
512 system_fh_media_query (format_data_t form, int argc, char *argv[])
513 {
514 #ifdef SIOCGIFMEDIA
515 memset (&ifm, 0, sizeof (ifm));
516 strncpy (ifm.ifm_name, form->ifr->ifr_name, sizeof (ifm.ifm_name));
517
518 if (ioctl (form->sfd, SIOCGIFMEDIA, &ifm) >= 0)
519 select_arg (form, argc, argv, 0);
520 else
521 #endif /* SIOCGIFMEDIA */
522 select_arg (form, argc, argv, 1);
523 }
524
525 void
526 system_fh_media (format_data_t form, int argc MAYBE_UNUSED,
527 MAYBE_UNUSED char *argv[])
528 {
529 #ifdef SIOCGIFMEDIA
530 memset (&ifm, 0, sizeof (ifm));
531 strncpy (ifm.ifm_name, form->ifr->ifr_name, sizeof (ifm.ifm_name));
532 if (ioctl (form->sfd, SIOCGIFMEDIA, &ifm) >= 0)
533 {
534 const char *medium = NULL, *options = NULL;
535 const char *subtype = NULL, *active_subtype = NULL;
536 const struct ifmedia_description *item;
537
538 /* Determine media type of adapter. */
539 for (item = media_descr; item->ifmt_word; item++)
540 if (item->ifmt_word == IFM_TYPE (ifm.ifm_current))
541 {
542 medium = item->ifmt_string;
543 break;
544 }
545
546 # ifdef IFM_SUBTYPE_DESCRIPTIONS
547 for (item = subtype_descr; item->ifmt_word || item->ifmt_string; item++)
548 {
549 if (item->ifmt_word == IFM_SUBTYPE (ifm.ifm_current) && !subtype)
550 subtype = item->ifmt_string;
551
552 if (item->ifmt_word == (ifm.ifm_active & (IFM_NMASK | IFM_TMASK))
553 && !active_subtype)
554 active_subtype = item->ifmt_string;
555 }
556 # else/* !IFM_SUBTYPE_DESCRIPTIONS */
557 /* Systems like FreeBSD with sub-types split
558 * into shared and media specific structures.
559 *
560 * Sub-type specific media labels.
561 */
562 switch (IFM_TYPE (ifm.ifm_current))
563 {
564 case IFM_ETHER:
565 for (item = ethernet_descr; item->ifmt_word; item++)
566 {
567 /* Configured sub-type. */
568 if (item->ifmt_word == IFM_SUBTYPE (ifm.ifm_current))
569 subtype = item->ifmt_string;
570
571 /* Active subtype. */
572 if (item->ifmt_word == IFM_SUBTYPE (ifm.ifm_active))
573 active_subtype = item->ifmt_string;
574 }
575 ;
576 default:
577 ;
578 }
579
580 /* Shared media sub-types. Only works if the previous
581 * loops did not assign any string value.
582 *
583 * One valid instance of 'ifmt_word' is naught,
584 * so loop condition needs care.
585 */
586 for (item = subtype_shared_descr; item->ifmt_word || item->ifmt_string;
587 item++)
588 {
589 if (item->ifmt_word == IFM_SUBTYPE (ifm.ifm_current))
590 subtype = item->ifmt_string;
591
592 if (item->ifmt_word == IFM_SUBTYPE (ifm.ifm_active))
593 active_subtype = item->ifmt_string;
594 }
595 # endif/* !IFM_SUBTYPE_DESCRIPTIONS */
596
597 /* Negotiated mode of operation. */
598 for (item = option_descr; item->ifmt_word; item++)
599 if (item->ifmt_word == (ifm.ifm_active & IFM_GMASK) && !options)
600 options = item->ifmt_string;
601
602 /* Print the gathered information. */
603 if (options)
604 printf ("%s %s (%s <%s>)", medium, subtype, active_subtype, options);
605 else
606 printf ("%s %s (%s)", medium, subtype,
607 active_subtype ? active_subtype : "none");
608
609 had_output++;
610 }
611 else
612 #endif /* SIOCGIFMEDIA */
613 put_string (form, "(not known)");
614 }
615
616 void
617 system_fh_netmask_query (format_data_t form, int argc, char *argv[])
618 {
619 ESTABLISH_IFADDRS if (!ifp)
620 select_arg (form, argc, argv, 1);
621 else
622 {
623 int missing = 1;
624 struct ifaddrs *fp;
625
626 for (fp = ifp; fp; fp = fp->ifa_next)
627 {
628 if (fp->ifa_addr->sa_family != AF_INET ||
629 strcmp (fp->ifa_name, form->ifr->ifr_name))
630 continue;
631
632 if (fp->ifa_netmask)
633 missing = 0;
634 break;
635 }
636 select_arg (form, argc, argv, missing);
637 }
638 }
639
640 void
641 system_fh_netmask (format_data_t form, int argc, char *argv[])
642 {
643 ESTABLISH_IFADDRS if (!ifp)
644 put_string (form, "(unknown)");
645 else
646 {
647 int missing = 1;
648 struct ifaddrs *fp;
649
650 for (fp = ifp; fp; fp = fp->ifa_next)
651 {
652 if (fp->ifa_addr->sa_family != AF_INET ||
653 strcmp (fp->ifa_name, form->ifr->ifr_name))
654 continue;
655
656 if (fp->ifa_netmask)
657 {
658 missing = 0;
659 put_addr (form, argc, argv, fp->ifa_netmask);
660 }
661 break;
662 }
663 if (missing)
664 put_string (form, "(netmask unknown)");
665 }
666 }
667
668 void
669 system_fh_status_query (format_data_t form, int argc, char *argv[])
670 {
671 system_fh_media_query (form, argc, argv);
672 }
673
674 void
675 system_fh_status (format_data_t form, int argc, char *argv[])
676 {
677 #ifdef SIOCGIFMEDIA
678 memset (&ifm, 0, sizeof (ifm));
679 strncpy (ifm.ifm_name, form->ifr->ifr_name, sizeof (ifm.ifm_name));
680 if (ioctl (form->sfd, SIOCGIFMEDIA, &ifm) >= 0)
681 {
682 # ifdef IFM_STATUS_DESCRIPTIONS
683 const struct ifmedia_status_description *item;
684
685 for (item = status_descr; item->ifms_type; item++)
686 if (item->ifms_type == IFM_TYPE (ifm.ifm_current))
687 break;
688
689 if (item->ifms_type)
690 put_string (form, IFM_STATUS_DESC (item, ifm.ifm_status));
691 else
692 put_int (form, argc, argv, ifm.ifm_status);
693 # else/* !IFM_STATUS_DESCRIPTIONS */
694 if (ifm.ifm_status & IFM_ACTIVE)
695 put_string (form, "active");
696 else
697 put_string (form, "no carrier");
698 # endif/* IFM_STATUS_DESCRIPTIONS */
699 }
700 else
701 #endif /* SIOCGIFMEDIA */
702 put_string (form, "(not known)");
703 }
704
705 void
706 system_fh_tunnel_query (format_data_t form, int argc, char *argv[])
707 {
708 #if defined SIOCGIFPSRCADDR && defined SIOCGIFPDSTADDR
709 if (ioctl (form->sfd, SIOCGIFPSRCADDR, form->ifr) >= 0)
710 select_arg (form, argc, argv, 0);
711 else
712 #endif /* SIOCGIFPSRCADDR && SIOCGIFPDSTADDR */
713 select_arg (form, argc, argv, 1);
714 }
715
716 void
717 system_fh_tundst (format_data_t form, int argc, char *argv[])
718 {
719 #ifdef SIOCGIFPDSTADDR
720 if (ioctl (form->sfd, SIOCGIFPDSTADDR, form->ifr) >= 0)
721 put_addr (form, argc, argv, &form->ifr->ifr_addr);
722 else
723 put_string (form, "(no phydst)");
724 #endif /* SIOCGIFPDSTADDR */
725 }
726
727 void
728 system_fh_tunsrc (format_data_t form, int argc, char *argv[])
729 {
730 #ifdef SIOCGIFPSRCADDR
731 if (ioctl (form->sfd, SIOCGIFPSRCADDR, form->ifr) >= 0)
732 put_addr (form, argc, argv, &form->ifr->ifr_addr);
733 else
734 put_string (form, "(no physrc)");
735 #endif /* SIOCGIFPSRCADDR */
736 }
737
738 static struct if_data *
739 get_if_data_by_name (format_data_t form)
740 {
741 struct ifaddrs *fp;
742 struct if_data *data = NULL;
743
744 ESTABLISH_IFADDRS if (!ifp)
745 return NULL;
746
747 for (fp = ifp; fp; fp = fp->ifa_next)
748 {
749 /* The choice of AF_LINK is the only portable alternative.
750 * Then IP, ARP etcera are included in all counters.
751 */
752 if (fp->ifa_addr->sa_family != AF_LINK ||
753 strcmp (fp->ifa_name, form->ifr->ifr_name))
754 continue;
755
756 data = (struct if_data *) fp->ifa_data;
757 break;
758 }
759
760 return data;
761 }
762
763 void
764 system_fh_missing_stat (format_data_t form, int argc, char *argv[])
765 {
766 /* FIXME: Would a dash be a better answer? */
767 put_ulong (form, argc, argv, 0);
768 }
769
770 #define _IU_DECLARE2(fld, udata) \
771 void \
772 _IU_CAT2 (system_fh_, fld) (format_data_t form, int argc, char *argv[]) \
773 { \
774 struct if_data *data = get_if_data_by_name (form); \
775 if (data) \
776 put_ulong (form, argc, argv, data->_IU_CAT2(ifi_, udata)); \
777 else \
778 put_string (form, "(" #fld " unknown)"); \
779 }
780
781 _IU_DECLARE2 (rx_bytes, ibytes)
782 _IU_DECLARE2 (rx_dropped, iqdrops)
783 _IU_DECLARE2 (rx_errors, ierrors)
784 _IU_DECLARE2 (rx_packets, ipackets)
785 _IU_DECLARE2 (tx_bytes, obytes)
786 _IU_DECLARE2 (tx_errors, oerrors)
787 _IU_DECLARE2 (tx_packets, opackets) _IU_DECLARE2 (collisions, collisions)
788 void
789 system_fh_tx_dropped (format_data_t form, int argc, char *argv[])
790 {
791 #ifdef _IFI_OQDROPS
792 struct if_data *data = get_if_data_by_name (form);
793
794 if (data)
795 put_ulong (form, argc, argv, data->ifi_oqdrops);
796 else
797 put_string (form, "(txerrors unknown)");
798 #else /* !_IFI_OQDROPS */
799 /* FIXME: Would a dash be a better answer? */
800 put_ulong (form, argc, argv, 0);
801 #endif
802 }