| 1 |
/* linux.c -- Linux 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 |
/* Written by Marcus Brinkmann. */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#include <sys/stat.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <xalloc.h> |
| 27 |
|
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#if STDC_HEADERS |
| 33 |
# include <stdlib.h> |
| 34 |
#else |
| 35 |
# ifndef HAVE_STRCHR |
| 36 |
# define strchr index |
| 37 |
# define strrchr rindex |
| 38 |
# endif |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <sys/ioctl.h> |
| 42 |
#include <net/if.h> |
| 43 |
#include <net/if_arp.h> |
| 44 |
#ifdef HAVE_NETINET_ETHER_H |
| 45 |
# include <netinet/ether.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#include <read-file.h> |
| 49 |
#include <attribute.h> |
| 50 |
|
| 51 |
#include "../ifconfig.h" |
| 52 |
|
| 53 |
|
| 54 |
/* ARPHRD stuff. */ |
| 55 |
|
| 56 |
static void |
| 57 |
print_hwaddr_ether (format_data_t form MAYBE_UNUSED, unsigned char *data) |
| 58 |
{ |
| 59 |
*column += printf ("%02X:%02X:%02X:%02X:%02X:%02X", |
| 60 |
data[0], data[1], data[2], data[3], data[4], data[5]); |
| 61 |
had_output = 1; |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
print_hwaddr_arcnet (format_data_t form MAYBE_UNUSED, unsigned char *data) |
| 66 |
{ |
| 67 |
*column += printf ("%02X", data[0]); |
| 68 |
had_output = 1; |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
print_hwaddr_dlci (format_data_t form MAYBE_UNUSED, unsigned char *data) |
| 73 |
{ |
| 74 |
*column += printf ("%i", *((short *) data)); |
| 75 |
had_output = 1; |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
print_hwaddr_ax25 (format_data_t form, unsigned char *data) |
| 80 |
{ |
| 81 |
int i = 0; |
| 82 |
while (i < 6 && (data[i] >> 1) != ' ') |
| 83 |
{ |
| 84 |
put_char (form, (data[i] >> 1)); |
| 85 |
i++; |
| 86 |
} |
| 87 |
i = (data[6] & 0x1E) >> 1; |
| 88 |
if (i) |
| 89 |
{ |
| 90 |
*column += printf ("-%i", i); |
| 91 |
had_output = 1; |
| 92 |
} |
| 93 |
#undef mangle |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
print_hwaddr_irda (format_data_t form MAYBE_UNUSED, unsigned char *data) |
| 98 |
{ |
| 99 |
*column += printf ("%02X:%02X:%02X:%02X", |
| 100 |
data[3], data[2], data[1], data[0]); |
| 101 |
had_output = 1; |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
print_hwaddr_rose (format_data_t form MAYBE_UNUSED, unsigned char *data) |
| 106 |
{ |
| 107 |
*column += printf ("%02X%02X%02X%02X%02X", |
| 108 |
data[0], data[1], data[2], data[3], data[4]); |
| 109 |
had_output = 1; |
| 110 |
} |
| 111 |
|
| 112 |
struct arphrd_symbol |
| 113 |
{ |
| 114 |
const char *name; |
| 115 |
const char *title; |
| 116 |
int value; |
| 117 |
void (*print_hwaddr) (format_data_t form, unsigned char *data); |
| 118 |
} arphrd_symbols[] = { |
| 119 |
/* ARP protocol HARDWARE identifiers. */ |
| 120 |
#ifdef ARPHRD_NETROM /* From KA9Q: NET/ROM pseudo. */ |
| 121 |
{"NETROM", "AMPR NET/ROM", ARPHRD_NETROM, print_hwaddr_ax25}, |
| 122 |
#endif |
| 123 |
#ifdef ARPHRD_ETHER /* Ethernet 10/100Mbps. */ |
| 124 |
{"ETHER", "Ethernet", ARPHRD_ETHER, print_hwaddr_ether}, |
| 125 |
#endif |
| 126 |
#ifdef ARPHRD_EETHER /* Experimental Ethernet. */ |
| 127 |
{"EETHER", "Experimental Etherner", ARPHRD_EETHER, NULL}, |
| 128 |
#endif |
| 129 |
#ifdef ARPHRD_AX25 /* AX.25 Level 2. */ |
| 130 |
{"AX25", "AMPR AX.25", ARPHRD_AX25, print_hwaddr_ax25}, |
| 131 |
#endif |
| 132 |
#ifdef ARPHRD_PRONET /* PROnet token ring. */ |
| 133 |
{"PRONET", "PROnet token ring", ARPHRD_PRONET, NULL}, |
| 134 |
#endif |
| 135 |
#ifdef ARPHRD_CHAOS /* Chaosnet. */ |
| 136 |
{"CHAOS", "Chaosnet", ARPHRD_CHAOS, NULL}, |
| 137 |
#endif |
| 138 |
#ifdef ARPHRD_IEEE802 /* IEEE 802.2 Ethernet/TR/TB. */ |
| 139 |
{"IEEE802", "16/4 Mbps Token Ring", ARPHRD_IEEE802, print_hwaddr_ether}, |
| 140 |
#endif |
| 141 |
#ifdef ARPHRD_ARCNET /* ARCnet. */ |
| 142 |
{"ARCNET", "ARCnet", ARPHRD_ARCNET, print_hwaddr_arcnet}, |
| 143 |
#endif |
| 144 |
#ifdef ARPHRD_APPLETLK /* APPLEtalk. */ |
| 145 |
{"APPLETLK", "Appletalk", ARPHRD_APPLETLK, NULL}, |
| 146 |
#endif |
| 147 |
#ifdef ARPHRD_DLCI /* Frame Relay DLCI. */ |
| 148 |
{"DLCI", "Frame Relay DLCI", ARPHRD_DLCI, print_hwaddr_dlci}, |
| 149 |
#endif |
| 150 |
#ifdef ARPHRD_ATM /* ATM. */ |
| 151 |
{"ATM", "ATM", ARPHRD_ATM, NULL}, |
| 152 |
#endif |
| 153 |
#ifdef ARPHRD_METRICOM /* Metricom STRIP (new IANA id). */ |
| 154 |
{"METRICOM", "Metricom STRIP", ARPHRD_METRICOM, NULL}, |
| 155 |
#endif |
| 156 |
/* Dummy types for non ARP hardware. */ |
| 157 |
#ifdef ARPHRD_SLIP |
| 158 |
{"SLIP", "Serial Line IP", ARPHRD_SLIP, NULL}, |
| 159 |
#endif |
| 160 |
#ifdef ARPHRD_CSLIP |
| 161 |
{"CSLIP", "VJ Serial Line IP", ARPHRD_CSLIP, NULL}, |
| 162 |
#endif |
| 163 |
#ifdef ARPHRD_SLIP6 |
| 164 |
{"SLIP6", "6-bit Serial Line IP", ARPHRD_SLIP6, NULL}, |
| 165 |
#endif |
| 166 |
#ifdef ARPHRD_CSLIP6 |
| 167 |
{"CSLIP6", "VJ 6-bit Serial Line IP", ARPHRD_CSLIP6, NULL}, |
| 168 |
#endif |
| 169 |
#ifdef ARPHRD_RSRVD /* Notional KISS type. */ |
| 170 |
{"SLIP", "Notional KISS type", ARPHRD_SLIP, NULL}, |
| 171 |
#endif |
| 172 |
#ifdef ARPHRD_ADAPT |
| 173 |
{"ADAPT", "Adaptive Serial Line IP", ARPHRD_ADAPT, NULL}, |
| 174 |
#endif |
| 175 |
#ifdef ARPHRD_ROSE |
| 176 |
{"ROSE", "AMPR ROSE", ARPHRD_ROSE, print_hwaddr_rose}, |
| 177 |
#endif |
| 178 |
#ifdef ARPHRD_X25 /* CCITT X.25. */ |
| 179 |
{"X25", "CCITT X.25", ARPHRD_X25, NULL}, |
| 180 |
#endif |
| 181 |
#ifdef ARPHRD_HWX25 /* Boards with X.25 in firmware. */ |
| 182 |
{"HWX25", "CCITT X.25 in firmware", ARPHRD_HWX25, NULL}, |
| 183 |
#endif |
| 184 |
#ifdef ARPHRD_PPP |
| 185 |
{"PPP", "Point-to-Point Protocol", ARPHRD_PPP, NULL}, |
| 186 |
#endif |
| 187 |
#ifdef ARPHRD_HDLC /* (Cisco) HDLC. */ |
| 188 |
{"HDLC", "(Cisco)-HDLC", ARPHRD_HDLC, NULL}, |
| 189 |
#endif |
| 190 |
#ifdef ARPHRD_LAPB /* LAPB. */ |
| 191 |
{"LAPB", "LAPB", ARPHRD_LAPB, NULL}, |
| 192 |
#endif |
| 193 |
#ifdef ARPHRD_DDCMP /* Digital's DDCMP. */ |
| 194 |
{"DDCMP", "DDCMP", ARPHRD_DDCMP, NULL}, |
| 195 |
#endif |
| 196 |
#ifdef ARPHRD_TUNNEL /* IPIP tunnel. */ |
| 197 |
{"TUNNEL", "IPIP Tunnel", ARPHRD_TUNNEL, NULL}, |
| 198 |
#endif |
| 199 |
#ifdef ARPHRD_TUNNEL6 /* IPIP6 tunnel. */ |
| 200 |
{"TUNNEL", "IPIP6 Tunnel", ARPHRD_TUNNEL6, NULL}, |
| 201 |
#endif |
| 202 |
#ifdef ARPHRD_FRAD /* Frame Relay Access Device. */ |
| 203 |
{"FRAD", "Frame Relay Access Device", ARPHRD_FRAD, NULL}, |
| 204 |
#endif |
| 205 |
#ifdef ARPHRD_SKIP /* SKIP vif. */ |
| 206 |
{"SKIP", "SKIP vif", ARPHRD_SKIP, NULL}, |
| 207 |
#endif |
| 208 |
#ifdef ARPHRD_LOOPBACK /* Loopback device. */ |
| 209 |
{"LOOPBACK", "Local Loopback", ARPHRD_LOOPBACK, NULL}, |
| 210 |
#endif |
| 211 |
#ifdef ARPHRD_LOCALTALK /* Localtalk device. */ |
| 212 |
{"LOCALTALK", "Localtalk", ARPHRD_LOCALTALK, NULL}, |
| 213 |
#endif |
| 214 |
#ifdef ARPHRD_FDDI /* Fiber Distributed Data Interface. */ |
| 215 |
{"FDDI", "Fiber Distributed Data Interface", ARPHRD_FDDI, NULL}, |
| 216 |
#endif |
| 217 |
#ifdef ARPHRD_BIF /* AP1000 BIF. */ |
| 218 |
{"BIF", "AP1000 BIF", ARPHRD_BIF, NULL}, |
| 219 |
#endif |
| 220 |
#ifdef ARPHRD_SIT /* sit0 device - IPv6-in-IPv4. */ |
| 221 |
{"SIT", "IPv6-in-IPv4", ARPHRD_SIT, NULL}, |
| 222 |
#endif |
| 223 |
#ifdef ARPHRD_IPDDP /* IP-in-DDP tunnel. */ |
| 224 |
{"IPDDP", "IP-in-DDP", ARPHRD_IPDDP, NULL}, |
| 225 |
#endif |
| 226 |
#ifdef ARPHRD_IPGRE /* GRE over IP. */ |
| 227 |
{"IPGRE", "GRE over IP", ARPHRD_IPGRE, NULL}, |
| 228 |
#endif |
| 229 |
#ifdef ARPHRD_PIMREG /* PIMSM register interface. */ |
| 230 |
{"PIMREG", "PIMSM register", ARPHRD_PIMREG, NULL}, |
| 231 |
#endif |
| 232 |
#ifdef ARPHRD_HIPPI /* High Performance Parallel I'face. */ |
| 233 |
{"HIPPI", "HIPPI", ARPHRD_HIPPI, print_hwaddr_ether}, |
| 234 |
#endif |
| 235 |
#ifdef ARPHRD_ASH /* (Nexus Electronics) Ash. */ |
| 236 |
{"ASH", "Ash", ARPHRD_ASH, NULL}, |
| 237 |
#endif |
| 238 |
#ifdef ARPHRD_ECONET /* Acorn Econet. */ |
| 239 |
{"ECONET", "Econet", ARPHRD_ECONET, NULL}, |
| 240 |
#endif |
| 241 |
#ifdef ARPHRD_IRDA /* Linux-IrDA. */ |
| 242 |
{"IRDA", "IrLap", ARPHRD_IRDA, print_hwaddr_irda}, |
| 243 |
#endif |
| 244 |
#ifdef ARPHRD_FCPP /* Point to point fibrechanel. */ |
| 245 |
{"FCPP", "FCPP", ARPHRD_FCPP, NULL}, |
| 246 |
#endif |
| 247 |
#ifdef ARPHRD_FCAL /* Fibrechanel arbitrated loop. */ |
| 248 |
{"FCAL", "FCAL", ARPHRD_FCAL, NULL}, |
| 249 |
#endif |
| 250 |
#ifdef ARPHRD_FCPL /* Fibrechanel public loop. */ |
| 251 |
{"FCPL", "FCPL", ARPHRD_FCPL, NULL}, |
| 252 |
#endif |
| 253 |
#ifdef ARPHRD_FCPFABRIC /* Fibrechanel fabric. */ |
| 254 |
{"FCFABRIC", "FCFABRIC", ARPHRD_FCPFABRIC, NULL}, |
| 255 |
#endif |
| 256 |
#ifdef ARPHRD_IEEE802_TR /* Magic type ident for TR. */ |
| 257 |
{"IEEE802_TR", "16/4 Mbps Token Ring (New)", ARPHRD_IEEE802_TR, |
| 258 |
print_hwaddr_ether}, |
| 259 |
#endif |
| 260 |
#ifdef ARPHRD_VOID |
| 261 |
{"VOID", "Void (nothing is known)", ARPHRD_VOID, NULL}, |
| 262 |
#endif |
| 263 |
}; |
| 264 |
|
| 265 |
struct arphrd_symbol * |
| 266 |
arphrd_findvalue (int value) |
| 267 |
{ |
| 268 |
struct arphrd_symbol *arp = arphrd_symbols; |
| 269 |
while (arp->name != NULL) |
| 270 |
{ |
| 271 |
if (arp->value == value) |
| 272 |
break; |
| 273 |
arp++; |
| 274 |
} |
| 275 |
if (arp->name) |
| 276 |
return arp; |
| 277 |
else |
| 278 |
return NULL; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
struct pnd_stats |
| 283 |
{ |
| 284 |
struct pnd_stats *next; |
| 285 |
char *name; |
| 286 |
unsigned long long rx_packets; /* total packets received */ |
| 287 |
unsigned long long tx_packets; /* total packets transmitted */ |
| 288 |
unsigned long long rx_bytes; /* total bytes received */ |
| 289 |
unsigned long long tx_bytes; /* total bytes transmitted */ |
| 290 |
unsigned long rx_errors; /* packets receive errors */ |
| 291 |
unsigned long tx_errors; /* packet transmit errors */ |
| 292 |
unsigned long rx_dropped; /* input packets dropped */ |
| 293 |
unsigned long tx_dropped; /* transmit packets dropped */ |
| 294 |
unsigned long rx_multicast; /* multicast packets received */ |
| 295 |
unsigned long rx_compressed; /* compressed packets received */ |
| 296 |
unsigned long tx_compressed; /* compressed packets transmitted */ |
| 297 |
unsigned long collisions; |
| 298 |
|
| 299 |
/* detailed rx_errors: */ |
| 300 |
unsigned long rx_length_errors; |
| 301 |
unsigned long rx_over_errors; /* receiver ring buffer overflow */ |
| 302 |
unsigned long rx_crc_errors; /* received packets with crc error */ |
| 303 |
unsigned long rx_frame_errors; /* received frame alignment errors */ |
| 304 |
unsigned long rx_fifo_errors; /* recveiver fifo overruns */ |
| 305 |
unsigned long rx_missed_errors; /* receiver missed packets */ |
| 306 |
/* detailed tx_errors */ |
| 307 |
unsigned long tx_aborted_errors; |
| 308 |
unsigned long tx_carrier_errors; |
| 309 |
unsigned long tx_fifo_errors; |
| 310 |
unsigned long tx_heartbeat_errors; |
| 311 |
unsigned long tx_window_errors; |
| 312 |
}; |
| 313 |
|
| 314 |
struct pnd_stats *stats_head, *stats_tail; |
| 315 |
static int pnd_stats_init = 0; |
| 316 |
|
| 317 |
static int |
| 318 |
procnet_parse_fields_v1 (char *buf, struct pnd_stats *stats) |
| 319 |
{ |
| 320 |
int n = sscanf (buf, |
| 321 |
"%llu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu", |
| 322 |
&stats->rx_packets, |
| 323 |
&stats->rx_errors, |
| 324 |
&stats->rx_dropped, |
| 325 |
&stats->rx_fifo_errors, |
| 326 |
&stats->rx_frame_errors, |
| 327 |
&stats->tx_packets, |
| 328 |
&stats->tx_errors, |
| 329 |
&stats->tx_dropped, |
| 330 |
&stats->tx_fifo_errors, |
| 331 |
&stats->collisions, |
| 332 |
&stats->tx_carrier_errors); |
| 333 |
stats->rx_bytes = 0; |
| 334 |
stats->tx_bytes = 0; |
| 335 |
stats->rx_multicast = 0; |
| 336 |
return n == 11; |
| 337 |
} |
| 338 |
|
| 339 |
static int |
| 340 |
procnet_parse_fields_v2 (char *buf, struct pnd_stats *stats) |
| 341 |
{ |
| 342 |
int n = sscanf (buf, |
| 343 |
"%llu %llu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu", |
| 344 |
&stats->rx_bytes, |
| 345 |
&stats->rx_packets, |
| 346 |
&stats->rx_errors, |
| 347 |
&stats->rx_dropped, |
| 348 |
&stats->rx_fifo_errors, |
| 349 |
&stats->rx_frame_errors, |
| 350 |
&stats->tx_bytes, |
| 351 |
&stats->tx_packets, |
| 352 |
&stats->tx_errors, |
| 353 |
&stats->tx_dropped, |
| 354 |
&stats->tx_fifo_errors, |
| 355 |
&stats->collisions, |
| 356 |
&stats->tx_carrier_errors); |
| 357 |
stats->rx_multicast = 0; |
| 358 |
return n == 13; |
| 359 |
} |
| 360 |
|
| 361 |
static int |
| 362 |
procnet_parse_fields_v3 (char *buf, struct pnd_stats *stats) |
| 363 |
{ |
| 364 |
int n = sscanf (buf, |
| 365 |
"%llu %llu %lu %lu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu %lu", |
| 366 |
&stats->rx_bytes, |
| 367 |
&stats->rx_packets, |
| 368 |
&stats->rx_errors, |
| 369 |
&stats->rx_dropped, |
| 370 |
&stats->rx_fifo_errors, |
| 371 |
&stats->rx_frame_errors, |
| 372 |
&stats->rx_compressed, |
| 373 |
&stats->rx_multicast, |
| 374 |
&stats->tx_bytes, |
| 375 |
&stats->tx_packets, |
| 376 |
&stats->tx_errors, |
| 377 |
&stats->tx_dropped, |
| 378 |
&stats->tx_fifo_errors, |
| 379 |
&stats->collisions, |
| 380 |
&stats->tx_carrier_errors, |
| 381 |
&stats->tx_compressed); |
| 382 |
return n == 16; |
| 383 |
} |
| 384 |
|
| 385 |
static int (*pnd_parser[]) (char *, struct pnd_stats *) = { |
| 386 |
procnet_parse_fields_v1, |
| 387 |
procnet_parse_fields_v2, |
| 388 |
procnet_parse_fields_v3 |
| 389 |
}; |
| 390 |
|
| 391 |
static int |
| 392 |
pnd_version (char *buf) |
| 393 |
{ |
| 394 |
if (strstr (buf, "compressed")) |
| 395 |
return 2; |
| 396 |
if (strstr (buf, "bytes")) |
| 397 |
return 1; |
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
static void |
| 402 |
pnd_read (void) |
| 403 |
{ |
| 404 |
FILE *fp; |
| 405 |
char *buf = NULL; |
| 406 |
size_t bufsize = 0; |
| 407 |
int v; |
| 408 |
|
| 409 |
fp = fopen (PATH_PROCNET_DEV, "r"); |
| 410 |
if (!fp) |
| 411 |
{ |
| 412 |
error (0, errno, "cannot open %s", PATH_PROCNET_DEV); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
/* Skip header lines */ |
| 417 |
if (getline (&buf, &bufsize, fp) < 0 || getline (&buf, &bufsize, fp) < 0) |
| 418 |
{ |
| 419 |
error (0, errno, "malformed %s", PATH_PROCNET_DEV); |
| 420 |
fclose (fp); |
| 421 |
free (buf); |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
v = pnd_version (buf); |
| 426 |
|
| 427 |
while (getline (&buf, &bufsize, fp) > 0) |
| 428 |
{ |
| 429 |
struct pnd_stats *stats = xzalloc (sizeof (*stats)); |
| 430 |
char *p = buf, *q; |
| 431 |
size_t len; |
| 432 |
|
| 433 |
while (*p |
| 434 |
&& isascii (*(unsigned char *) p) |
| 435 |
&& isspace (*(unsigned char *) p)) |
| 436 |
p++; |
| 437 |
q = strchr (p, ':'); |
| 438 |
if (!q) |
| 439 |
{ |
| 440 |
error (0, errno, "malformed %s", PATH_PROCNET_DEV); |
| 441 |
free (stats); |
| 442 |
break; |
| 443 |
} |
| 444 |
len = q - p; |
| 445 |
stats->name = xmalloc (len + 1); |
| 446 |
memcpy (stats->name, p, len); |
| 447 |
stats->name[len] = 0; |
| 448 |
if (pnd_parser[v] (q + 1, stats) == 0) |
| 449 |
{ |
| 450 |
error (0, errno, "malformed %s", PATH_PROCNET_DEV); |
| 451 |
free (stats); |
| 452 |
break; |
| 453 |
} |
| 454 |
stats->next = NULL; |
| 455 |
if (stats_tail) |
| 456 |
stats_tail->next = stats; |
| 457 |
else |
| 458 |
stats_head = stats; |
| 459 |
stats_tail = stats; |
| 460 |
} |
| 461 |
|
| 462 |
fclose (fp); |
| 463 |
free (buf); |
| 464 |
} |
| 465 |
|
| 466 |
struct pnd_stats * |
| 467 |
pnd_stats_locate (const char *name) |
| 468 |
{ |
| 469 |
struct pnd_stats *stats; |
| 470 |
if (!pnd_stats_init) |
| 471 |
{ |
| 472 |
pnd_read (); |
| 473 |
pnd_stats_init = 1; |
| 474 |
} |
| 475 |
for (stats = stats_head; stats; stats = stats->next) |
| 476 |
if (strcmp (stats->name, name) == 0) |
| 477 |
break; |
| 478 |
return stats; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
/* Output format stuff. */ |
| 484 |
|
| 485 |
const char *system_default_format = "net-tools"; |
| 486 |
|
| 487 |
void |
| 488 |
system_fh_ifstat_query (format_data_t form, int argc, char *argv[]) |
| 489 |
{ |
| 490 |
select_arg (form, argc, argv, |
| 491 |
pnd_stats_locate (form->ifr->ifr_name) ? 0 : 1); |
| 492 |
} |
| 493 |
|
| 494 |
#define _IU_DECLARE(fld) \ |
| 495 |
void \ |
| 496 |
_IU_CAT2 (system_fh_,fld) (format_data_t form, int argc, char *argv[]) \ |
| 497 |
{ \ |
| 498 |
struct pnd_stats *stats = pnd_stats_locate (form->ifr->ifr_name); \ |
| 499 |
if (!stats) \ |
| 500 |
put_string (form, "(" #fld " unknown)"); \ |
| 501 |
else \ |
| 502 |
put_ulong (form, argc, argv, stats->fld); \ |
| 503 |
} |
| 504 |
|
| 505 |
_IU_DECLARE (rx_packets) |
| 506 |
_IU_DECLARE (tx_packets) |
| 507 |
_IU_DECLARE (rx_bytes) |
| 508 |
_IU_DECLARE (tx_bytes) |
| 509 |
_IU_DECLARE (rx_errors) |
| 510 |
_IU_DECLARE (tx_errors) |
| 511 |
_IU_DECLARE (rx_dropped) |
| 512 |
_IU_DECLARE (tx_dropped) |
| 513 |
_IU_DECLARE (rx_multicast) |
| 514 |
_IU_DECLARE (rx_compressed) |
| 515 |
_IU_DECLARE (tx_compressed) |
| 516 |
_IU_DECLARE (collisions) |
| 517 |
_IU_DECLARE (rx_length_errors) |
| 518 |
_IU_DECLARE (rx_over_errors) |
| 519 |
_IU_DECLARE (rx_crc_errors) |
| 520 |
_IU_DECLARE (rx_frame_errors) |
| 521 |
_IU_DECLARE (rx_fifo_errors) |
| 522 |
_IU_DECLARE (rx_missed_errors) |
| 523 |
_IU_DECLARE (tx_aborted_errors) |
| 524 |
_IU_DECLARE (tx_carrier_errors) |
| 525 |
_IU_DECLARE (tx_fifo_errors) |
| 526 |
_IU_DECLARE (tx_heartbeat_errors) _IU_DECLARE (tx_window_errors) |
| 527 |
void |
| 528 |
system_fh_hwaddr_query (format_data_t form, int argc, char *argv[]) |
| 529 |
{ |
| 530 |
#ifdef SIOCGIFHWADDR |
| 531 |
struct arphrd_symbol *arp; |
| 532 |
|
| 533 |
if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0) |
| 534 |
select_arg (form, argc, argv, 1); |
| 535 |
|
| 536 |
arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family); |
| 537 |
select_arg (form, argc, argv, (arp && arp->print_hwaddr) ? 0 : 1); |
| 538 |
#else |
| 539 |
select_arg (form, argc, argv, 1); |
| 540 |
#endif |
| 541 |
} |
| 542 |
|
| 543 |
void |
| 544 |
system_fh_hwaddr (format_data_t form, int argc MAYBE_UNUSED, |
| 545 |
MAYBE_UNUSED char *argv[]) |
| 546 |
{ |
| 547 |
#ifdef SIOCGIFHWADDR |
| 548 |
if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0) |
| 549 |
error (EXIT_FAILURE, errno, |
| 550 |
"SIOCGIFHWADDR failed for interface `%s'", form->ifr->ifr_name); |
| 551 |
else |
| 552 |
{ |
| 553 |
struct arphrd_symbol *arp; |
| 554 |
|
| 555 |
arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family); |
| 556 |
if (arp && arp->print_hwaddr) |
| 557 |
arp->print_hwaddr (form, |
| 558 |
(unsigned char *) form->ifr->ifr_hwaddr.sa_data); |
| 559 |
else |
| 560 |
put_string (form, "(hwaddr unknown)"); |
| 561 |
} |
| 562 |
#else |
| 563 |
*column += printf ("(not available)"); |
| 564 |
had_output = 1; |
| 565 |
#endif |
| 566 |
} |
| 567 |
|
| 568 |
void |
| 569 |
system_fh_hwtype_query (format_data_t form, int argc, char *argv[]) |
| 570 |
{ |
| 571 |
#ifdef SIOCGIFHWADDR |
| 572 |
if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) >= 0) |
| 573 |
select_arg (form, argc, argv, 0); |
| 574 |
else |
| 575 |
#endif |
| 576 |
select_arg (form, argc, argv, 1); |
| 577 |
} |
| 578 |
|
| 579 |
void |
| 580 |
system_fh_hwtype (format_data_t form, int argc MAYBE_UNUSED, |
| 581 |
MAYBE_UNUSED char *argv[]) |
| 582 |
{ |
| 583 |
#ifdef SIOCGIFHWADDR |
| 584 |
if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0) |
| 585 |
error (EXIT_FAILURE, errno, |
| 586 |
"SIOCGIFHWADDR failed for interface `%s'", form->ifr->ifr_name); |
| 587 |
else |
| 588 |
{ |
| 589 |
struct arphrd_symbol *arp; |
| 590 |
|
| 591 |
arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family); |
| 592 |
if (arp) |
| 593 |
put_string (form, arp->title); |
| 594 |
else |
| 595 |
put_string (form, "(hwtype unknown)"); |
| 596 |
} |
| 597 |
#else |
| 598 |
*column += printf ("(not available)"); |
| 599 |
had_output = 1; |
| 600 |
#endif |
| 601 |
} |
| 602 |
|
| 603 |
/* Accept every value of metric as printable. The net-tools' |
| 604 |
* implementation of ifconfig displays metric 0 as `1', so we |
| 605 |
* aim at the same thing, even though all other unices disagree. |
| 606 |
*/ |
| 607 |
void |
| 608 |
system_fh_metric_query (format_data_t form, int argc, char *argv[]) |
| 609 |
{ |
| 610 |
#ifdef SIOCGIFMETRIC |
| 611 |
if (ioctl (form->sfd, SIOCGIFMETRIC, form->ifr) >= 0) |
| 612 |
select_arg (form, argc, argv, 0); |
| 613 |
else |
| 614 |
#endif |
| 615 |
select_arg (form, argc, argv, 1); |
| 616 |
} |
| 617 |
|
| 618 |
void |
| 619 |
system_fh_metric (format_data_t form, int argc, char *argv[]) |
| 620 |
{ |
| 621 |
#ifdef SIOCGIFMETRIC |
| 622 |
if (ioctl (form->sfd, SIOCGIFMETRIC, form->ifr) < 0) |
| 623 |
error (EXIT_FAILURE, errno, |
| 624 |
"SIOCGIFMETRIC failed for interface `%s'", form->ifr->ifr_name); |
| 625 |
else |
| 626 |
put_int (form, argc, argv, |
| 627 |
form->ifr->ifr_metric ? form->ifr->ifr_metric : 1); |
| 628 |
#else |
| 629 |
*column += printf ("(not available)"); |
| 630 |
had_output = 1; |
| 631 |
#endif |
| 632 |
} |
| 633 |
|
| 634 |
void |
| 635 |
system_fh_txqlen_query (format_data_t form, int argc, char *argv[]) |
| 636 |
{ |
| 637 |
#ifdef SIOCGIFTXQLEN |
| 638 |
if (ioctl (form->sfd, SIOCGIFTXQLEN, form->ifr) >= 0) |
| 639 |
select_arg (form, argc, argv, (form->ifr->ifr_qlen >= 0) ? 0 : 1); |
| 640 |
else |
| 641 |
#endif |
| 642 |
select_arg (form, argc, argv, 1); |
| 643 |
} |
| 644 |
|
| 645 |
void |
| 646 |
system_fh_txqlen (format_data_t form, int argc, char *argv[]) |
| 647 |
{ |
| 648 |
#ifdef SIOCGIFTXQLEN |
| 649 |
if (ioctl (form->sfd, SIOCGIFTXQLEN, form->ifr) < 0) |
| 650 |
error (EXIT_FAILURE, errno, |
| 651 |
"SIOCGIFTXQLEN failed for interface `%s'", form->ifr->ifr_name); |
| 652 |
else |
| 653 |
put_int (form, argc, argv, form->ifr->ifr_qlen); |
| 654 |
#else |
| 655 |
*column += printf ("(not available)"); |
| 656 |
had_output = 1; |
| 657 |
#endif |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
/* Argument parsing stuff. */ |
| 662 |
|
| 663 |
const char *system_help = "\ |
| 664 |
NAME [ADDR] [broadcast BRDADDR]\ |
| 665 |
[pointopoint|dstaddr DSTADDR] [netmask MASK]\ |
| 666 |
[ether|hwaddr|lladdr MACADDR]\ |
| 667 |
[metric N] [mtu N] [txqueuelen N] [up|down] [FLAGS]"; |
| 668 |
|
| 669 |
void |
| 670 |
system_parse_opt_set_txqlen (struct ifconfig *ifp, char *arg) |
| 671 |
{ |
| 672 |
char *end; |
| 673 |
|
| 674 |
if (!ifp) |
| 675 |
error (EXIT_FAILURE, 0, "no interface specified for txqlen `%s'", arg); |
| 676 |
|
| 677 |
if (!(ifp->valid & IF_VALID_SYSTEM)) |
| 678 |
{ |
| 679 |
ifp->system = malloc (sizeof (struct system_ifconfig)); |
| 680 |
if (!ifp->system) |
| 681 |
error (EXIT_FAILURE, errno, |
| 682 |
"can't get memory for system interface configuration"); |
| 683 |
ifp->system->valid = 0; |
| 684 |
ifp->valid |= IF_VALID_SYSTEM; |
| 685 |
} |
| 686 |
if (ifp->system->valid & IF_VALID_TXQLEN) |
| 687 |
error (EXIT_FAILURE, 0, |
| 688 |
"only one txqlen allowed for interface `%s'", ifp->name); |
| 689 |
ifp->system->txqlen = strtol (arg, &end, 0); |
| 690 |
if (*arg == '\0' || *end != '\0') |
| 691 |
error (EXIT_FAILURE, 0, |
| 692 |
"txqlen value `%s' for interface `%s' is not a number", |
| 693 |
arg, ifp->name); |
| 694 |
ifp->system->valid |= IF_VALID_TXQLEN; |
| 695 |
} |
| 696 |
|
| 697 |
static struct argp_option linux_argp_options[] = { |
| 698 |
{"txqlen", 'T', "N", 0, |
| 699 |
"set transmit queue length to N", 0}, |
| 700 |
{NULL, 0, NULL, 0, NULL, 0} |
| 701 |
}; |
| 702 |
|
| 703 |
static error_t |
| 704 |
linux_argp_parser (int key, char *arg, struct argp_state *state) |
| 705 |
{ |
| 706 |
struct ifconfig *ifp = *(struct ifconfig **) state->input; |
| 707 |
switch (key) |
| 708 |
{ |
| 709 |
case 'T': /* txqlen */ |
| 710 |
system_parse_opt_set_txqlen (ifp, arg); |
| 711 |
break; |
| 712 |
|
| 713 |
default: |
| 714 |
return ARGP_ERR_UNKNOWN; |
| 715 |
} |
| 716 |
return 0; |
| 717 |
} |
| 718 |
|
| 719 |
static struct argp linux_argp = |
| 720 |
{ linux_argp_options, linux_argp_parser, NULL, NULL, NULL, NULL, NULL }; |
| 721 |
|
| 722 |
struct argp_child system_argp_child = { |
| 723 |
&linux_argp, |
| 724 |
0, |
| 725 |
"Linux-specific options", |
| 726 |
0 |
| 727 |
}; |
| 728 |
|
| 729 |
|
| 730 |
int |
| 731 |
system_parse_opt (struct ifconfig **ifpp, char option, char *arg) |
| 732 |
{ |
| 733 |
struct ifconfig *ifp = *ifpp; |
| 734 |
|
| 735 |
switch (option) |
| 736 |
{ |
| 737 |
case 'T': /* txqlen */ |
| 738 |
system_parse_opt_set_txqlen (ifp, arg); |
| 739 |
break; |
| 740 |
|
| 741 |
default: |
| 742 |
return 0; |
| 743 |
} |
| 744 |
return 1; |
| 745 |
} |
| 746 |
|
| 747 |
int |
| 748 |
system_parse_opt_rest (struct ifconfig **ifp, int argc, char *argv[]) |
| 749 |
{ |
| 750 |
int i = 0; |
| 751 |
enum |
| 752 |
{ |
| 753 |
EXPECT_AF, |
| 754 |
EXPECT_NOTHING, |
| 755 |
EXPECT_BROADCAST, |
| 756 |
EXPECT_DSTADDR, |
| 757 |
EXPECT_NETMASK, |
| 758 |
EXPECT_HWADDR, |
| 759 |
EXPECT_MTU, |
| 760 |
EXPECT_METRIC, |
| 761 |
EXPECT_TXQLEN, |
| 762 |
} expect = EXPECT_AF; |
| 763 |
int mask, rev; |
| 764 |
|
| 765 |
*ifp = parse_opt_new_ifs (argv[0]); |
| 766 |
|
| 767 |
while (++i < argc) |
| 768 |
{ |
| 769 |
switch (expect) |
| 770 |
{ |
| 771 |
case EXPECT_BROADCAST: |
| 772 |
parse_opt_set_brdaddr (*ifp, argv[i]); |
| 773 |
break; |
| 774 |
|
| 775 |
case EXPECT_DSTADDR: |
| 776 |
parse_opt_set_point_to_point (*ifp, argv[i]); |
| 777 |
break; |
| 778 |
|
| 779 |
case EXPECT_NETMASK: |
| 780 |
parse_opt_set_netmask (*ifp, argv[i]); |
| 781 |
break; |
| 782 |
|
| 783 |
case EXPECT_HWADDR: |
| 784 |
parse_opt_set_hwaddr (*ifp, argv[i]); |
| 785 |
break; |
| 786 |
|
| 787 |
case EXPECT_MTU: |
| 788 |
parse_opt_set_mtu (*ifp, argv[i]); |
| 789 |
break; |
| 790 |
|
| 791 |
case EXPECT_METRIC: |
| 792 |
parse_opt_set_metric (*ifp, argv[i]); |
| 793 |
break; |
| 794 |
|
| 795 |
case EXPECT_TXQLEN: |
| 796 |
system_parse_opt_set_txqlen (*ifp, argv[i]); |
| 797 |
break; |
| 798 |
|
| 799 |
case EXPECT_AF: |
| 800 |
expect = EXPECT_NOTHING; |
| 801 |
if (!strcmp (argv[i], "inet")) |
| 802 |
continue; |
| 803 |
else if (!strcmp (argv[i], "inet6")) |
| 804 |
{ |
| 805 |
error (0, 0, "%s is not a supported address family", argv[i]); |
| 806 |
return 0; |
| 807 |
} |
| 808 |
break; |
| 809 |
|
| 810 |
case EXPECT_NOTHING: |
| 811 |
break; |
| 812 |
} |
| 813 |
|
| 814 |
if (expect != EXPECT_NOTHING) |
| 815 |
expect = EXPECT_NOTHING; |
| 816 |
else if (!strcmp (argv[i], "broadcast")) |
| 817 |
expect = EXPECT_BROADCAST; |
| 818 |
else if (!strcmp (argv[i], "dstaddr") |
| 819 |
|| !strcmp (argv[i], "pointopoint")) |
| 820 |
expect = EXPECT_DSTADDR; |
| 821 |
else if (!strcmp (argv[i], "netmask")) |
| 822 |
expect = EXPECT_NETMASK; |
| 823 |
else if (!strcmp (argv[i], "ether") |
| 824 |
|| !strcmp (argv[i], "hwaddr") || !strcmp (argv[i], "lladdr")) |
| 825 |
expect = EXPECT_HWADDR; |
| 826 |
else if (!strcmp (argv[i], "metric")) |
| 827 |
expect = EXPECT_METRIC; |
| 828 |
else if (!strcmp (argv[i], "mtu")) |
| 829 |
expect = EXPECT_MTU; |
| 830 |
else if (!strcmp (argv[i], "txqueuelen")) |
| 831 |
expect = EXPECT_TXQLEN; |
| 832 |
else if (!strcmp (argv[i], "up")) |
| 833 |
parse_opt_set_flag (*ifp, IFF_UP | IFF_RUNNING, 0); |
| 834 |
else if (!strcmp (argv[i], "down")) |
| 835 |
parse_opt_set_flag (*ifp, IFF_UP, 1); |
| 836 |
else if (((mask = if_nameztoflag (argv[i], &rev)) |
| 837 |
& ~IU_IFF_CANTCHANGE) != 0) |
| 838 |
parse_opt_set_flag (*ifp, mask, rev); |
| 839 |
else |
| 840 |
parse_opt_set_address (*ifp, argv[i]); |
| 841 |
} |
| 842 |
|
| 843 |
switch (expect) |
| 844 |
{ |
| 845 |
case EXPECT_BROADCAST: |
| 846 |
error (0, 0, "option `broadcast' requires an argument"); |
| 847 |
break; |
| 848 |
|
| 849 |
case EXPECT_DSTADDR: |
| 850 |
error (0, 0, "option `pointopoint' (`dstaddr') requires an argument"); |
| 851 |
break; |
| 852 |
|
| 853 |
case EXPECT_NETMASK: |
| 854 |
error (0, 0, "option `netmask' requires an argument"); |
| 855 |
break; |
| 856 |
|
| 857 |
case EXPECT_HWADDR: |
| 858 |
error (0, 0, "option `hwaddr' requires an argument"); |
| 859 |
break; |
| 860 |
|
| 861 |
case EXPECT_METRIC: |
| 862 |
error (0, 0, "option `metric' requires an argument"); |
| 863 |
break; |
| 864 |
|
| 865 |
case EXPECT_MTU: |
| 866 |
error (0, 0, "option `mtu' requires an argument"); |
| 867 |
break; |
| 868 |
|
| 869 |
case EXPECT_TXQLEN: |
| 870 |
error (0, 0, "option `txqueuelen' requires an argument"); |
| 871 |
break; |
| 872 |
|
| 873 |
case EXPECT_AF: |
| 874 |
case EXPECT_NOTHING: |
| 875 |
return 1; |
| 876 |
} |
| 877 |
return 0; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
int |
| 882 |
system_preconfigure (int sfd MAYBE_UNUSED, struct ifreq *ifr MAYBE_UNUSED) |
| 883 |
{ |
| 884 |
return 0; |
| 885 |
} |
| 886 |
|
| 887 |
int |
| 888 |
system_configure (int sfd, struct ifreq *ifr, struct system_ifconfig *ifs) |
| 889 |
{ |
| 890 |
if (ifs->valid & IF_VALID_TXQLEN) |
| 891 |
{ |
| 892 |
#ifndef SIOCSIFTXQLEN |
| 893 |
error (0, 0, "don't know how to set the txqlen on this system"); |
| 894 |
return -1; |
| 895 |
#else |
| 896 |
int err = 0; |
| 897 |
|
| 898 |
ifr->ifr_qlen = ifs->txqlen; |
| 899 |
err = ioctl (sfd, SIOCSIFTXQLEN, ifr); |
| 900 |
if (err < 0) |
| 901 |
error (0, errno, "SIOCSIFTXQLEN failed"); |
| 902 |
if (verbose) |
| 903 |
printf ("Set txqlen value of `%s' to `%i'.\n", |
| 904 |
ifr->ifr_name, ifr->ifr_qlen); |
| 905 |
#endif |
| 906 |
} |
| 907 |
return 0; |
| 908 |
} |
| 909 |
|
| 910 |
static struct if_nameindex * |
| 911 |
linux_if_nameindex (void) |
| 912 |
{ |
| 913 |
char *content, *it; |
| 914 |
size_t length, index; |
| 915 |
struct if_nameindex *idx = NULL; |
| 916 |
int fd; |
| 917 |
|
| 918 |
fd = socket (AF_INET, SOCK_DGRAM, 0); |
| 919 |
if (fd < 0) |
| 920 |
return NULL; |
| 921 |
|
| 922 |
/* Read a public text file. */ |
| 923 |
content = read_file (PATH_PROCNET_DEV, 0, &length); |
| 924 |
if (content == NULL) |
| 925 |
return NULL; |
| 926 |
|
| 927 |
/* Count how many interfaces we have. */ |
| 928 |
{ |
| 929 |
size_t n = 0; |
| 930 |
it = content; |
| 931 |
do |
| 932 |
{ |
| 933 |
it = memchr (it + 1, ':', length - (it - content)); |
| 934 |
n++; |
| 935 |
} |
| 936 |
while (it); |
| 937 |
|
| 938 |
idx = malloc (n * sizeof (*idx)); |
| 939 |
if (idx == NULL) |
| 940 |
{ |
| 941 |
int saved_errno = errno; |
| 942 |
close (fd); |
| 943 |
free (content); |
| 944 |
errno = saved_errno; |
| 945 |
return NULL; |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
for (it = memchr (content, ':', length), index = 0; it; |
| 950 |
it = memchr (it, ':', length - (it - content)), index++) |
| 951 |
{ |
| 952 |
char *start = it - 1; |
| 953 |
*it = '\0'; |
| 954 |
|
| 955 |
while (*start != ' ' && *start != '\n') |
| 956 |
start--; |
| 957 |
|
| 958 |
idx[index].if_name = strdup (start + 1); |
| 959 |
idx[index].if_index = index + 1; |
| 960 |
|
| 961 |
#if defined SIOCGIFINDEX |
| 962 |
{ |
| 963 |
struct ifreq cur; |
| 964 |
strcpy (cur.ifr_name, idx[index].if_name); |
| 965 |
cur.ifr_index = -1; |
| 966 |
if (ioctl (fd, SIOCGIFINDEX, &cur) >= 0) |
| 967 |
idx[index].if_index = cur.ifr_index; |
| 968 |
} |
| 969 |
#endif |
| 970 |
|
| 971 |
if (idx[index].if_name == NULL) |
| 972 |
{ |
| 973 |
int saved_errno = errno; |
| 974 |
close (fd); |
| 975 |
free (content); |
| 976 |
errno = saved_errno; |
| 977 |
return NULL; |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
idx[index].if_index = 0; |
| 982 |
idx[index].if_name = NULL; |
| 983 |
|
| 984 |
free (content); |
| 985 |
return idx; |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
/* System hooks. */ |
| 991 |
|
| 992 |
struct if_nameindex *(*system_if_nameindex) (void) = linux_if_nameindex; |