| 1 |
/* |
| 2 |
* Interface MIB architecture support |
| 3 |
* |
| 4 |
* $Id$ |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-features.h> |
| 8 |
#include <net-snmp/net-snmp-includes.h> |
| 9 |
#include <net-snmp/agent/snmp_agent.h> |
| 10 |
#include <net-snmp/agent/snmp_vars.h> |
| 11 |
#include "interface_private.h" |
| 12 |
|
| 13 |
netsnmp_feature_require(fd_event_manager); |
| 14 |
netsnmp_feature_require(delete_prefix_info); |
| 15 |
netsnmp_feature_require(create_prefix_info); |
| 16 |
netsnmp_feature_child_of(interface_arch_set_admin_status, interface_all); |
| 17 |
|
| 18 |
#ifdef NETSNMP_FEATURE_REQUIRE_INTERFACE_ARCH_SET_ADMIN_STATUS |
| 19 |
netsnmp_feature_require(interface_ioctl_flags_set); |
| 20 |
#endif /* NETSNMP_FEATURE_REQUIRE_INTERFACE_ARCH_SET_ADMIN_STATUS */ |
| 21 |
|
| 22 |
#ifdef HAVE_INTTYPES_H |
| 23 |
#include <inttypes.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifdef HAVE_PCI_LOOKUP_NAME |
| 27 |
#include <pci/pci.h> |
| 28 |
#include <setjmp.h> |
| 29 |
#ifndef PCI_NONRET |
| 30 |
#define PCI_NONRET |
| 31 |
#endif |
| 32 |
static struct pci_access *pci_access; |
| 33 |
|
| 34 |
/* Avoid letting libpci call exit(1) when no PCI bus is available. */ |
| 35 |
static int do_longjmp =0; |
| 36 |
static jmp_buf err_buf; |
| 37 |
PCI_NONRET static void |
| 38 |
netsnmp_pci_error(char *msg, ...) |
| 39 |
{ |
| 40 |
va_list args; |
| 41 |
char *buf; |
| 42 |
int buflen; |
| 43 |
|
| 44 |
va_start(args, msg); |
| 45 |
buflen = strlen("pcilib: ")+strlen(msg)+2; |
| 46 |
buf = malloc(buflen); |
| 47 |
snprintf(buf, buflen, "pcilib: %s\n", msg); |
| 48 |
snmp_vlog(LOG_ERR, buf, args); |
| 49 |
free(buf); |
| 50 |
va_end(args); |
| 51 |
if (do_longjmp) |
| 52 |
longjmp(err_buf, 1); |
| 53 |
else |
| 54 |
exit(1); |
| 55 |
} |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 59 |
#ifdef HAVE_LINUX_ETHTOOL_NEEDS_U64 |
| 60 |
#include <linux/types.h> |
| 61 |
typedef __u64 u64; /* hack, so we may include kernel's ethtool.h */ |
| 62 |
typedef __u32 u32; /* ditto */ |
| 63 |
typedef __u16 u16; /* ditto */ |
| 64 |
typedef __u8 u8; /* ditto */ |
| 65 |
#endif |
| 66 |
#include <linux/ethtool.h> |
| 67 |
#endif /* HAVE_LINUX_ETHTOOL_H */ |
| 68 |
|
| 69 |
#include "mibII/mibII_common.h" |
| 70 |
#include "if-mib/ifTable/ifTable_constants.h" |
| 71 |
#include "ip-mib/data_access/ipaddress_ioctl.h" |
| 72 |
|
| 73 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 74 |
|
| 75 |
#ifdef HAVE_SYS_IOCTL_H |
| 76 |
#include <sys/ioctl.h> |
| 77 |
#else |
| 78 |
#error "linux should have sys/ioctl header" |
| 79 |
#endif |
| 80 |
|
| 81 |
#include <net-snmp/data_access/interface.h> |
| 82 |
#include <net-snmp/data_access/ipaddress.h> |
| 83 |
#include "if-mib/data_access/interface.h" |
| 84 |
#include "mibgroup/util_funcs.h" |
| 85 |
#include "interface_ioctl.h" |
| 86 |
|
| 87 |
#include <sys/types.h> |
| 88 |
#include <sys/stat.h> |
| 89 |
#include <unistd.h> |
| 90 |
#include <errno.h> |
| 91 |
|
| 92 |
#include <linux/sockios.h> |
| 93 |
#include <linux/if_ether.h> |
| 94 |
|
| 95 |
#ifndef IF_NAMESIZE |
| 96 |
#define IF_NAMESIZE 16 |
| 97 |
#endif |
| 98 |
|
| 99 |
#ifndef SIOCGMIIPHY |
| 100 |
#define SIOCGMIIPHY 0x8947 |
| 101 |
#endif |
| 102 |
|
| 103 |
#ifndef SIOCGMIIREG |
| 104 |
#define SIOCGMIIREG 0x8948 |
| 105 |
#endif |
| 106 |
|
| 107 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 108 |
#if defined(HAVE_LINUX_RTNETLINK_H) |
| 109 |
#include <linux/rtnetlink.h> |
| 110 |
#ifdef RTMGRP_IPV6_PREFIX |
| 111 |
#define SUPPORT_PREFIX_FLAGS 1 |
| 112 |
#endif /* RTMGRP_IPV6_PREFIX */ |
| 113 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 114 |
#endif /* NETSNMP_ENABLE_IPV6 */ |
| 115 |
unsigned long long |
| 116 |
netsnmp_linux_interface_get_if_speed(int fd, const char *name, |
| 117 |
unsigned long long defaultspeed); |
| 118 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 119 |
unsigned long long |
| 120 |
netsnmp_linux_interface_get_if_speed_mii(int fd, const char *name, |
| 121 |
unsigned long long defaultspeed); |
| 122 |
#endif |
| 123 |
|
| 124 |
#define PROC_SYS_NET_IPVx_NEIGH_RETRANS_TIME_MS "/proc/sys/net/ipv%d/neigh/%s/retrans_time_ms" |
| 125 |
#define PROC_SYS_NET_IPVx_NEIGH_RETRANS_TIME "/proc/sys/net/ipv%d/neigh/%s/retrans_time" |
| 126 |
#define PROC_SYS_NET_IPVx_BASE_REACHABLE_TIME_MS "/proc/sys/net/ipv%d/neigh/%s/base_reachable_time_ms" |
| 127 |
#define PROC_SYS_NET_IPVx_BASE_REACHABLE_TIME "/proc/sys/net/ipv%d/neigh/%s/base_reachable_time" |
| 128 |
#ifdef SUPPORT_PREFIX_FLAGS |
| 129 |
prefix_cbx *prefix_head_list = NULL; |
| 130 |
netsnmp_prefix_listen_info list_info; |
| 131 |
#define IF_PREFIX_ONLINK 0x01 |
| 132 |
#define IF_PREFIX_AUTOCONF 0x02 |
| 133 |
|
| 134 |
int netsnmp_prefix_listen(void); |
| 135 |
#endif |
| 136 |
|
| 137 |
#ifdef HAVE_PCI_LOOKUP_NAME |
| 138 |
static void init_libpci(void) |
| 139 |
{ |
| 140 |
struct stat stbuf; |
| 141 |
|
| 142 |
/* |
| 143 |
* When snmpd is run inside an OpenVZ container or on a Raspberry Pi system |
| 144 |
* /proc/bus/pci is not available. |
| 145 |
*/ |
| 146 |
if (stat("/proc/bus/pci", &stbuf) < 0) |
| 147 |
return; |
| 148 |
|
| 149 |
pci_access = pci_alloc(); |
| 150 |
if (!pci_access) { |
| 151 |
snmp_log(LOG_ERR, "pcilib: pci_alloc failed\n"); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
pci_access->error = netsnmp_pci_error; |
| 156 |
|
| 157 |
do_longjmp = 1; |
| 158 |
if (setjmp(err_buf)) { |
| 159 |
pci_cleanup(pci_access); |
| 160 |
snmp_log(LOG_ERR, "pcilib: pci_init failed\n"); |
| 161 |
pci_access = NULL; |
| 162 |
} |
| 163 |
else if (pci_access) |
| 164 |
pci_init(pci_access); |
| 165 |
do_longjmp = 0; |
| 166 |
} |
| 167 |
#else |
| 168 |
static void init_libpci(void) |
| 169 |
{ |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
void |
| 174 |
netsnmp_arch_interface_init(void) |
| 175 |
{ |
| 176 |
#ifdef SUPPORT_PREFIX_FLAGS |
| 177 |
list_info.list_head = &prefix_head_list; |
| 178 |
netsnmp_prefix_listen(); |
| 179 |
#endif |
| 180 |
|
| 181 |
init_libpci(); |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* find the ifIndex for an interface name |
| 186 |
* NOTE: The Linux version is not efficient for large numbers of calls. |
| 187 |
* consider using netsnmp_access_interface_ioctl_ifindex_get() |
| 188 |
* for loops which need to look up a lot of indexes. |
| 189 |
* |
| 190 |
* @retval 0 : no index found |
| 191 |
* @retval >0: ifIndex for interface |
| 192 |
*/ |
| 193 |
oid |
| 194 |
netsnmp_arch_interface_index_find(const char *name) |
| 195 |
{ |
| 196 |
return netsnmp_access_interface_ioctl_ifindex_get(-1, name); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/* |
| 201 |
* check for ipv6 addresses |
| 202 |
*/ |
| 203 |
void |
| 204 |
_arch_interface_has_ipv6(oid if_index, u_int *flags, |
| 205 |
netsnmp_container *addr_container) |
| 206 |
{ |
| 207 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 208 |
netsnmp_ipaddress_entry *addr_entry = NULL; |
| 209 |
netsnmp_iterator *addr_it = NULL; |
| 210 |
u_int addr_container_flags = 0; /* must init to 0 */ |
| 211 |
#endif |
| 212 |
|
| 213 |
if (NULL == flags) |
| 214 |
return; |
| 215 |
|
| 216 |
*flags &= ~NETSNMP_INTERFACE_FLAGS_HAS_IPV6; |
| 217 |
|
| 218 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 219 |
/* |
| 220 |
* get ipv6 addresses |
| 221 |
*/ |
| 222 |
if (NULL == addr_container) { |
| 223 |
/* |
| 224 |
* we only care about ipv6, if we need to allocate our own |
| 225 |
* temporary container. set the flags (which we also use later |
| 226 |
* to determine if we need to free the container). |
| 227 |
*/ |
| 228 |
addr_container_flags = NETSNMP_ACCESS_IPADDRESS_LOAD_IPV6_ONLY; |
| 229 |
addr_container = |
| 230 |
netsnmp_access_ipaddress_container_load(NULL, |
| 231 |
addr_container_flags); |
| 232 |
if (NULL == addr_container) { |
| 233 |
DEBUGMSGTL(("access:ifcontainer", |
| 234 |
"couldn't get ip addresses container\n")); |
| 235 |
return; |
| 236 |
} |
| 237 |
} |
| 238 |
else { |
| 239 |
/* |
| 240 |
* addr_container flags must be 0, so we don't release the |
| 241 |
* user's container. |
| 242 |
*/ |
| 243 |
netsnmp_assert(0 == addr_container_flags); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/* |
| 248 |
* get an ipaddress container iterator, and look for ipv6 addrs |
| 249 |
*/ |
| 250 |
addr_it = CONTAINER_ITERATOR(addr_container); |
| 251 |
if (NULL == addr_it) { |
| 252 |
DEBUGMSGTL(("access:ifcontainer", |
| 253 |
"couldn't get ip addresses iterator\n")); |
| 254 |
if (0!=addr_container_flags) |
| 255 |
netsnmp_access_ipaddress_container_free(addr_container, 0); |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
addr_entry = ITERATOR_FIRST(addr_it); |
| 260 |
for( ; addr_entry ; addr_entry = ITERATOR_NEXT(addr_it) ) { |
| 261 |
/* |
| 262 |
* skip non matching indexes and ipv4 addresses |
| 263 |
*/ |
| 264 |
if ((if_index != addr_entry->if_index) || |
| 265 |
(4 == addr_entry->ia_address_len)) |
| 266 |
continue; |
| 267 |
|
| 268 |
/* |
| 269 |
* found one! no need to keep looking, set the flag and bail |
| 270 |
*/ |
| 271 |
*flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV6; |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* make mama proud and clean up after ourselves |
| 277 |
*/ |
| 278 |
ITERATOR_RELEASE(addr_it); |
| 279 |
if (0!=addr_container_flags) |
| 280 |
netsnmp_access_ipaddress_container_free(addr_container, 0); |
| 281 |
#endif |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* @internal |
| 286 |
*/ |
| 287 |
static void |
| 288 |
_arch_interface_flags_v4_get(netsnmp_interface_entry *entry) |
| 289 |
{ |
| 290 |
FILE *fin; |
| 291 |
char line[256]; |
| 292 |
struct stat st; |
| 293 |
unsigned short retrans_time_factor; |
| 294 |
|
| 295 |
/* |
| 296 |
* Check which retransmit time interface is available |
| 297 |
*/ |
| 298 |
snprintf(line, sizeof(line), PROC_SYS_NET_IPVx_NEIGH_RETRANS_TIME_MS, 4, |
| 299 |
entry->name); |
| 300 |
if (stat(line, &st) == 0) { |
| 301 |
retrans_time_factor = 1; |
| 302 |
} else { |
| 303 |
snprintf(line, sizeof(line), PROC_SYS_NET_IPVx_NEIGH_RETRANS_TIME, 4, |
| 304 |
entry->name); |
| 305 |
retrans_time_factor = 10; |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
* get the retransmit time |
| 310 |
*/ |
| 311 |
if (!(fin = fopen(line, "r"))) { |
| 312 |
DEBUGMSGTL(("access:interface", |
| 313 |
"Failed to open %s\n", line)); |
| 314 |
} else { |
| 315 |
if (fgets(line, sizeof(line), fin)) { |
| 316 |
entry->retransmit_v4 = atoi(line) * retrans_time_factor; |
| 317 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V4_RETRANSMIT; |
| 318 |
} |
| 319 |
fclose(fin); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
#ifdef HAVE_PCI_LOOKUP_NAME |
| 324 |
|
| 325 |
/* Get value from sysfs file */ |
| 326 |
static int sysfs_get_id(const char *path, unsigned short *id) |
| 327 |
{ |
| 328 |
FILE *fin; |
| 329 |
int n; |
| 330 |
|
| 331 |
if (!(fin = fopen(path, "r"))) { |
| 332 |
DEBUGMSGTL(("access:interface", |
| 333 |
"Failed to open %s\n", path)); |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
n = fscanf(fin, "%hx", id); |
| 338 |
fclose(fin); |
| 339 |
|
| 340 |
return n == 1; |
| 341 |
} |
| 342 |
|
| 343 |
/* Get interface description for PCI device |
| 344 |
* by using sysfs to find vendor and device |
| 345 |
* then lookup name (-lpci) |
| 346 |
* |
| 347 |
* For software interfaces there is no PCI information |
| 348 |
* so description will not be set. |
| 349 |
*/ |
| 350 |
static void |
| 351 |
_arch_interface_description_get(netsnmp_interface_entry *entry) |
| 352 |
{ |
| 353 |
const char *descr; |
| 354 |
char buf[256]; |
| 355 |
unsigned short vendor_id, device_id; |
| 356 |
|
| 357 |
if (!pci_access) |
| 358 |
return; |
| 359 |
|
| 360 |
snprintf(buf, sizeof(buf), |
| 361 |
"/sys/class/net/%s/device/vendor", entry->name); |
| 362 |
|
| 363 |
if (!sysfs_get_id(buf, &vendor_id)) |
| 364 |
return; |
| 365 |
|
| 366 |
snprintf(buf, sizeof(buf), |
| 367 |
"/sys/class/net/%s/device/device", entry->name); |
| 368 |
|
| 369 |
if (!sysfs_get_id(buf, &device_id)) |
| 370 |
return; |
| 371 |
|
| 372 |
descr = pci_lookup_name(pci_access, buf, sizeof(buf), |
| 373 |
PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, |
| 374 |
vendor_id, device_id, 0, 0); |
| 375 |
if (descr) { |
| 376 |
free(entry->descr); |
| 377 |
entry->descr = strdup(descr); |
| 378 |
} else { |
| 379 |
DEBUGMSGTL(("access:interface", |
| 380 |
"Failed pci_lookup_name vendor=%#hx device=%#hx\n", |
| 381 |
vendor_id, device_id)); |
| 382 |
} |
| 383 |
} |
| 384 |
#endif |
| 385 |
|
| 386 |
|
| 387 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 388 |
/** |
| 389 |
* @internal |
| 390 |
*/ |
| 391 |
static void |
| 392 |
_arch_interface_flags_v6_get(netsnmp_interface_entry *entry) |
| 393 |
{ |
| 394 |
FILE *fin; |
| 395 |
char line[256]; |
| 396 |
struct stat st; |
| 397 |
unsigned short retrans_time_factor; |
| 398 |
unsigned short basereachable_time_ms; |
| 399 |
|
| 400 |
/* |
| 401 |
* Check which retransmit time interface is available |
| 402 |
*/ |
| 403 |
snprintf(line, sizeof(line), PROC_SYS_NET_IPVx_NEIGH_RETRANS_TIME_MS, 6, |
| 404 |
entry->name); |
| 405 |
if (stat(line, &st) == 0) { |
| 406 |
retrans_time_factor = 1; |
| 407 |
} else { |
| 408 |
snprintf(line, sizeof(line), PROC_SYS_NET_IPVx_NEIGH_RETRANS_TIME, 6, |
| 409 |
entry->name); |
| 410 |
retrans_time_factor = 10; |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
* get the retransmit time |
| 415 |
*/ |
| 416 |
if (!(fin = fopen(line, "r"))) { |
| 417 |
DEBUGMSGTL(("access:interface", |
| 418 |
"Failed to open %s\n", line)); |
| 419 |
} |
| 420 |
else { |
| 421 |
if (fgets(line, sizeof(line), fin)) { |
| 422 |
entry->retransmit_v6 = atoi(line) * retrans_time_factor; |
| 423 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V6_RETRANSMIT; |
| 424 |
} |
| 425 |
fclose(fin); |
| 426 |
} |
| 427 |
|
| 428 |
/* |
| 429 |
* get the forwarding status |
| 430 |
*/ |
| 431 |
snprintf(line, sizeof(line), "/proc/sys/net/ipv6/conf/%s/forwarding", |
| 432 |
entry->name); |
| 433 |
if (!(fin = fopen(line, "r"))) { |
| 434 |
DEBUGMSGTL(("access:interface", |
| 435 |
"Failed to open %s\n", line)); |
| 436 |
} |
| 437 |
else { |
| 438 |
if (fgets(line, sizeof(line), fin)) { |
| 439 |
entry->forwarding_v6 = atoi(line); |
| 440 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V6_FORWARDING; |
| 441 |
} |
| 442 |
fclose(fin); |
| 443 |
} |
| 444 |
|
| 445 |
/* |
| 446 |
* Check which base reachable time interface is available. |
| 447 |
*/ |
| 448 |
|
| 449 |
snprintf(line, sizeof(line), PROC_SYS_NET_IPVx_BASE_REACHABLE_TIME_MS, 6, |
| 450 |
entry->name); |
| 451 |
if (stat(line, &st) == 0) { |
| 452 |
basereachable_time_ms = 1; |
| 453 |
} else { |
| 454 |
snprintf(line, sizeof(line), PROC_SYS_NET_IPVx_BASE_REACHABLE_TIME, 6, |
| 455 |
entry->name); |
| 456 |
basereachable_time_ms = 0; |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
* get the reachable time |
| 461 |
*/ |
| 462 |
if (!(fin = fopen(line, "r"))) { |
| 463 |
DEBUGMSGTL(("access:interface", |
| 464 |
"Failed to open %s\n", line)); |
| 465 |
} |
| 466 |
else { |
| 467 |
if (fgets(line, sizeof(line), fin)) { |
| 468 |
if (basereachable_time_ms) { |
| 469 |
entry->reachable_time = atoi(line); /* millisec */ |
| 470 |
} else { |
| 471 |
entry->reachable_time = atoi(line)*1000; /* sec to millisec */ |
| 472 |
} |
| 473 |
|
| 474 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V6_REACHABLE; |
| 475 |
} |
| 476 |
fclose(fin); |
| 477 |
} |
| 478 |
} |
| 479 |
#endif /* NETSNMP_ENABLE_IPV6 */ |
| 480 |
|
| 481 |
/** |
| 482 |
* @internal |
| 483 |
*/ |
| 484 |
static int |
| 485 |
_parse_stats(netsnmp_interface_entry *entry, char *stats, int expected) |
| 486 |
{ |
| 487 |
/* |
| 488 |
* scanline_2_2: |
| 489 |
* [ IN ] |
| 490 |
* byte pkts errs drop fifo frame cmprs mcst | |
| 491 |
* [ OUT ] |
| 492 |
* byte pkts errs drop fifo colls carrier compressed |
| 493 |
*/ |
| 494 |
uintmax_t rec_pkt, rec_oct, rec_err, rec_drop, rec_mcast; |
| 495 |
uintmax_t snd_pkt, snd_oct, snd_err, snd_drop, coll; |
| 496 |
const char *scan_line_2_2 = |
| 497 |
"%" SCNuMAX " %" SCNuMAX " %" SCNuMAX " %" SCNuMAX |
| 498 |
" %*" SCNuMAX " %*" SCNuMAX " %*" SCNuMAX " %" SCNuMAX |
| 499 |
" %" SCNuMAX " %" SCNuMAX " %" SCNuMAX " %" SCNuMAX |
| 500 |
" %*" SCNuMAX " %" SCNuMAX; |
| 501 |
const char *scan_line_2_0 = |
| 502 |
"%" SCNuMAX " %" SCNuMAX " %*" SCNuMAX " %*" SCNuMAX |
| 503 |
" %*" SCNuMAX " %" SCNuMAX " %" SCNuMAX " %*" SCNuMAX |
| 504 |
" %*" SCNuMAX " %" SCNuMAX; |
| 505 |
static const char *scan_line_to_use = NULL; |
| 506 |
int scan_count; |
| 507 |
|
| 508 |
if (10 == expected) |
| 509 |
scan_line_to_use = scan_line_2_2; |
| 510 |
else { |
| 511 |
netsnmp_assert(5 == expected); |
| 512 |
scan_line_to_use = scan_line_2_0; |
| 513 |
} |
| 514 |
|
| 515 |
while (*stats == ' ') |
| 516 |
stats++; |
| 517 |
|
| 518 |
if ((*stats == 'N') && |
| 519 |
(0 == strncmp(stats, "No statistics available", |
| 520 |
strlen("No statistics available")))) |
| 521 |
return -1; |
| 522 |
|
| 523 |
/* |
| 524 |
* Now parse the rest of the line (i.e. starting from 'stats') |
| 525 |
* to extract the relevant statistics, and populate |
| 526 |
* data structure accordingly. |
| 527 |
* Use the entry flags field to indicate which counters are valid |
| 528 |
*/ |
| 529 |
rec_pkt = rec_oct = rec_err = rec_drop = rec_mcast = 0; |
| 530 |
snd_pkt = snd_oct = snd_err = snd_drop = coll = 0; |
| 531 |
if (scan_line_to_use == scan_line_2_2) { |
| 532 |
scan_count = sscanf(stats, scan_line_to_use, |
| 533 |
&rec_oct, &rec_pkt, &rec_err, &rec_drop, &rec_mcast, |
| 534 |
&snd_oct, &snd_pkt, &snd_err, &snd_drop, |
| 535 |
&coll); |
| 536 |
if (scan_count == expected) { |
| 537 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_BYTES; |
| 538 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_DROPS; |
| 539 |
/* |
| 540 |
* 2.4 kernel includes a single multicast (input) counter? |
| 541 |
*/ |
| 542 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_MCAST_PKTS; |
| 543 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_HIGH_SPEED; |
| 544 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_HIGH_BYTES; |
| 545 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_HIGH_PACKETS; |
| 546 |
} |
| 547 |
} else { |
| 548 |
scan_count = sscanf(stats, scan_line_to_use, |
| 549 |
&rec_pkt, &rec_err, |
| 550 |
&snd_pkt, &snd_err, &coll); |
| 551 |
if (scan_count == expected) { |
| 552 |
entry->ns_flags &= ~NETSNMP_INTERFACE_FLAGS_HAS_MCAST_PKTS; |
| 553 |
rec_oct = rec_drop = 0; |
| 554 |
snd_oct = snd_drop = 0; |
| 555 |
} |
| 556 |
} |
| 557 |
if(scan_count != expected) { |
| 558 |
snmp_log(LOG_ERR, |
| 559 |
"error scanning interface data (expected %d, got %d)\n", |
| 560 |
expected, scan_count); |
| 561 |
return scan_count; |
| 562 |
} |
| 563 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_ACTIVE; |
| 564 |
|
| 565 |
/* |
| 566 |
* linux previous to 1.3.~13 may miss transmitted loopback pkts: |
| 567 |
*/ |
| 568 |
if (!strcmp(entry->name, "lo") && rec_pkt > 0 && !snd_pkt) |
| 569 |
snd_pkt = rec_pkt; |
| 570 |
|
| 571 |
/* |
| 572 |
* subtract out multicast packets from rec_pkt before |
| 573 |
* we store it as unicast counter. |
| 574 |
*/ |
| 575 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_CALCULATE_UCAST; |
| 576 |
entry->stats.ibytes.low = rec_oct & 0xffffffff; |
| 577 |
entry->stats.iall.low = rec_pkt & 0xffffffff; |
| 578 |
entry->stats.imcast.low = rec_mcast & 0xffffffff; |
| 579 |
entry->stats.obytes.low = snd_oct & 0xffffffff; |
| 580 |
entry->stats.oucast.low = snd_pkt & 0xffffffff; |
| 581 |
entry->stats.ibytes.high = rec_oct >> 32; |
| 582 |
entry->stats.iall.high = rec_pkt >> 32; |
| 583 |
entry->stats.imcast.high = rec_mcast >> 32; |
| 584 |
entry->stats.obytes.high = snd_oct >> 32; |
| 585 |
entry->stats.oucast.high = snd_pkt >> 32; |
| 586 |
entry->stats.ierrors = rec_err; |
| 587 |
entry->stats.idiscards = rec_drop; |
| 588 |
entry->stats.oerrors = snd_err; |
| 589 |
entry->stats.odiscards = snd_drop; |
| 590 |
entry->stats.collisions = coll; |
| 591 |
|
| 592 |
/* |
| 593 |
* calculated stats. |
| 594 |
* |
| 595 |
* we have imcast, but not ibcast. |
| 596 |
*/ |
| 597 |
entry->stats.inucast = entry->stats.imcast.low + |
| 598 |
entry->stats.ibcast.low; |
| 599 |
entry->stats.onucast = entry->stats.omcast.low + |
| 600 |
entry->stats.obcast.low; |
| 601 |
|
| 602 |
return 0; |
| 603 |
} |
| 604 |
|
| 605 |
/* Guess the IANA network interface type from the network interface name. */ |
| 606 |
static int netsnmp_guess_interface_type(const netsnmp_interface_entry *entry) |
| 607 |
{ |
| 608 |
struct match_if { |
| 609 |
int mi_type; |
| 610 |
const char *mi_name; |
| 611 |
}; |
| 612 |
|
| 613 |
static const struct match_if lmatch_if[] = { |
| 614 |
{IANAIFTYPE_SOFTWARELOOPBACK, "lo"}, |
| 615 |
{IANAIFTYPE_ETHERNETCSMACD, "eth"}, |
| 616 |
{IANAIFTYPE_ETHERNETCSMACD, "vmnet"}, |
| 617 |
{IANAIFTYPE_ISO88025TOKENRING, "tr"}, |
| 618 |
{IANAIFTYPE_FASTETHER, "feth"}, |
| 619 |
{IANAIFTYPE_GIGABITETHERNET,"gig"}, |
| 620 |
{IANAIFTYPE_INFINIBAND,"ib"}, |
| 621 |
{IANAIFTYPE_PPP, "ppp"}, |
| 622 |
{IANAIFTYPE_SLIP, "sl"}, |
| 623 |
{IANAIFTYPE_TUNNEL, "sit"}, |
| 624 |
{IANAIFTYPE_BASICISDN, "ippp"}, |
| 625 |
{IANAIFTYPE_PROPVIRTUAL, "bond"}, /* Bonding driver find fastest slave */ |
| 626 |
{IANAIFTYPE_PROPVIRTUAL, "vad"}, /* ANS driver - ?speed? */ |
| 627 |
{0, NULL} /* end of list */ |
| 628 |
}; |
| 629 |
|
| 630 |
const struct match_if *pm; |
| 631 |
|
| 632 |
for (pm = lmatch_if; pm->mi_name; pm++) { |
| 633 |
const int len = strlen(pm->mi_name); |
| 634 |
|
| 635 |
if (strncmp(entry->name, pm->mi_name, len) == 0) |
| 636 |
return pm->mi_type; |
| 637 |
} |
| 638 |
return IANAIFTYPE_OTHER; |
| 639 |
} |
| 640 |
|
| 641 |
static void netsnmp_derive_interface_id(netsnmp_interface_entry *entry) |
| 642 |
{ |
| 643 |
/* |
| 644 |
* interface identifier is specified based on physaddr and type |
| 645 |
*/ |
| 646 |
switch (entry->type) { |
| 647 |
case IANAIFTYPE_ETHERNETCSMACD: |
| 648 |
case IANAIFTYPE_ETHERNET3MBIT: |
| 649 |
case IANAIFTYPE_FASTETHER: |
| 650 |
case IANAIFTYPE_FASTETHERFX: |
| 651 |
case IANAIFTYPE_GIGABITETHERNET: |
| 652 |
case IANAIFTYPE_FDDI: |
| 653 |
case IANAIFTYPE_ISO88025TOKENRING: |
| 654 |
if (entry->paddr && entry->paddr_len != ETH_ALEN) |
| 655 |
break; |
| 656 |
|
| 657 |
entry->v6_if_id_len = entry->paddr_len + 2; |
| 658 |
memcpy(entry->v6_if_id, entry->paddr, 3); |
| 659 |
memcpy(entry->v6_if_id + 5, entry->paddr + 3, 3); |
| 660 |
entry->v6_if_id[0] ^= 2; |
| 661 |
entry->v6_if_id[3] = 0xFF; |
| 662 |
entry->v6_if_id[4] = 0xFE; |
| 663 |
|
| 664 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V6_IFID; |
| 665 |
break; |
| 666 |
|
| 667 |
case IANAIFTYPE_SOFTWARELOOPBACK: |
| 668 |
entry->v6_if_id_len = 0; |
| 669 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V6_IFID; |
| 670 |
break; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Read network interface information from /proc/net/dev. |
| 676 |
* |
| 677 |
* @param container: Container to store network information in. |
| 678 |
* @param load_flags: One or more NETSNMP_ACCESS_INTERFACE_LOAD_* flags. |
| 679 |
* |
| 680 |
* @retval 0 success |
| 681 |
* @retval -1 no container specified |
| 682 |
* @retval -2 could not open /proc/net/dev |
| 683 |
* @retval -3 could not create entry (probably malloc) |
| 684 |
*/ |
| 685 |
int |
| 686 |
netsnmp_arch_interface_container_load(netsnmp_container* container, |
| 687 |
u_int load_flags) |
| 688 |
{ |
| 689 |
FILE *devin; |
| 690 |
char line[256]; |
| 691 |
static char scan_expected = 0; |
| 692 |
int fd, rc; |
| 693 |
int interfaces = 0; |
| 694 |
struct ifconf ifc; |
| 695 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 696 |
netsnmp_container *addr_container; |
| 697 |
#endif |
| 698 |
|
| 699 |
DEBUGMSGTL(("access:interface:container:arch", "load (flags %x)\n", |
| 700 |
load_flags)); |
| 701 |
|
| 702 |
if (NULL == container) { |
| 703 |
snmp_log(LOG_ERR, "no container specified/found for interface\n"); |
| 704 |
return -1; |
| 705 |
} |
| 706 |
|
| 707 |
if (!(devin = fopen("/proc/net/dev", "r"))) { |
| 708 |
DEBUGMSGTL(("access:interface", |
| 709 |
"Failed to load Interface Table (linux1)\n")); |
| 710 |
snmp_log_perror("interface_linux: cannot open /proc/net/dev"); |
| 711 |
return -2; |
| 712 |
} |
| 713 |
|
| 714 |
/* |
| 715 |
* create socket for ioctls |
| 716 |
*/ |
| 717 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 718 |
if(fd < 0) { |
| 719 |
snmp_log_perror("interface_linux: could not create socket"); |
| 720 |
rc = -2; |
| 721 |
goto close_devin; |
| 722 |
} |
| 723 |
|
| 724 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 725 |
/* |
| 726 |
* get ipv6 addresses |
| 727 |
*/ |
| 728 |
addr_container = netsnmp_access_ipaddress_container_load(NULL, 0); |
| 729 |
#endif |
| 730 |
|
| 731 |
/* |
| 732 |
* Read the first two lines of the file, containing the header |
| 733 |
* This indicates which version of the kernel we're working with, |
| 734 |
* and hence which statistics are actually available. |
| 735 |
* |
| 736 |
* Wes originally suggested parsing the field names in this header |
| 737 |
* to detect the position of individual fields directly, |
| 738 |
* but I suspect this is probably more trouble than it's worth. |
| 739 |
*/ |
| 740 |
NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), devin)); |
| 741 |
NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), devin)); |
| 742 |
|
| 743 |
if( 0 == scan_expected ) { |
| 744 |
if (strstr(line, "compressed")) { |
| 745 |
scan_expected = 10; |
| 746 |
DEBUGMSGTL(("access:interface", |
| 747 |
"using linux 2.2 kernel /proc/net/dev\n")); |
| 748 |
} else { |
| 749 |
scan_expected = 5; |
| 750 |
DEBUGMSGTL(("access:interface", |
| 751 |
"using linux 2.0 kernel /proc/net/dev\n")); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
interfaces = netsnmp_access_ipaddress_ioctl_get_interface_count(fd, &ifc); |
| 756 |
if (interfaces < 0) { |
| 757 |
snmp_log(LOG_ERR,"get interface count failed\n"); |
| 758 |
rc = -2; |
| 759 |
goto free_addr_container; |
| 760 |
} |
| 761 |
netsnmp_assert(NULL != ifc.ifc_buf); |
| 762 |
|
| 763 |
|
| 764 |
/* |
| 765 |
* The rest of the file provides the statistics for each interface. |
| 766 |
* Read in each line in turn, isolate the interface name |
| 767 |
* and retrieve (or create) the corresponding data structure. |
| 768 |
*/ |
| 769 |
while (fgets(line, sizeof(line), devin)) { |
| 770 |
netsnmp_interface_entry *entry = NULL; |
| 771 |
char *stats, *ifstart = line; |
| 772 |
u_int flags; |
| 773 |
oid if_index; |
| 774 |
size_t len; |
| 775 |
|
| 776 |
flags = 0; |
| 777 |
len = strlen(line); |
| 778 |
if (len && line[len - 1] == '\n') |
| 779 |
line[len - 1] = '\0'; |
| 780 |
|
| 781 |
while (*ifstart && *ifstart == ' ') |
| 782 |
ifstart++; |
| 783 |
|
| 784 |
if ((!*ifstart) || ((stats = strrchr(ifstart, ':')) == NULL)) { |
| 785 |
snmp_log(LOG_ERR, |
| 786 |
"interface data format error 1, line ==|%s|\n", line); |
| 787 |
continue; |
| 788 |
} |
| 789 |
if ((scan_expected == 10) && ((stats - line) < 6)) { |
| 790 |
snmp_log(LOG_ERR, |
| 791 |
"interface data format error 2 (%d < 6), line ==|%s|\n", |
| 792 |
(int)(stats - line), line); |
| 793 |
} |
| 794 |
|
| 795 |
DEBUGMSGTL(("9:access:ifcontainer", "processing '%s'\n", ifstart)); |
| 796 |
|
| 797 |
/* |
| 798 |
* get index via ioctl. |
| 799 |
* If we've met this interface before, use the same index. |
| 800 |
* Otherwise find an unused index value and use that. |
| 801 |
*/ |
| 802 |
*stats++ = 0; /* null terminate name */ |
| 803 |
|
| 804 |
if (!netsnmp_access_interface_include(ifstart)) |
| 805 |
continue; |
| 806 |
|
| 807 |
/* we may need to stop tracking ifaces if a max was set */ |
| 808 |
if (netsnmp_access_interface_max_reached(ifstart)) |
| 809 |
continue; |
| 810 |
|
| 811 |
/* |
| 812 |
* set address type flags. |
| 813 |
* the only way I know of to check an interface for |
| 814 |
* ip version is to look for ip addresses. If anyone |
| 815 |
* knows a better way, put it here! |
| 816 |
*/ |
| 817 |
if_index = netsnmp_arch_interface_index_find(ifstart); |
| 818 |
if (if_index == 0) { |
| 819 |
DEBUGMSGTL(("access:interface", "network interface %s is gone", |
| 820 |
ifstart)); |
| 821 |
continue; |
| 822 |
} |
| 823 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 824 |
_arch_interface_has_ipv6(if_index, &flags, addr_container); |
| 825 |
#endif |
| 826 |
netsnmp_access_interface_ioctl_has_ipv4(fd, ifstart, 0, &flags, &ifc); |
| 827 |
|
| 828 |
/* |
| 829 |
* do we only want one address type? |
| 830 |
*/ |
| 831 |
if (((load_flags & NETSNMP_ACCESS_INTERFACE_LOAD_IP4_ONLY) && |
| 832 |
((flags & NETSNMP_INTERFACE_FLAGS_HAS_IPV4) == 0)) || |
| 833 |
((load_flags & NETSNMP_ACCESS_INTERFACE_LOAD_IP6_ONLY) && |
| 834 |
((flags & NETSNMP_INTERFACE_FLAGS_HAS_IPV6) == 0))) { |
| 835 |
DEBUGMSGTL(("9:access:ifcontainer", |
| 836 |
"interface '%s' excluded by ip version\n", |
| 837 |
ifstart)); |
| 838 |
continue; |
| 839 |
} |
| 840 |
|
| 841 |
entry = netsnmp_access_interface_entry_create(ifstart, if_index); |
| 842 |
if(NULL == entry) { |
| 843 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 844 |
netsnmp_access_ipaddress_container_free(addr_container, 0); |
| 845 |
#endif |
| 846 |
rc = -3; |
| 847 |
goto free_ifc; |
| 848 |
} |
| 849 |
entry->ns_flags = flags; /* initial flags; we'll set more later */ |
| 850 |
|
| 851 |
#ifdef HAVE_PCI_LOOKUP_NAME |
| 852 |
_arch_interface_description_get(entry); |
| 853 |
#endif |
| 854 |
|
| 855 |
|
| 856 |
/* |
| 857 |
* use ioctls for some stuff |
| 858 |
* (ignore rc, so we get as much info as possible) |
| 859 |
*/ |
| 860 |
netsnmp_access_interface_ioctl_physaddr_get(fd, entry); |
| 861 |
|
| 862 |
/* |
| 863 |
* physaddr should have set type. make some guesses (based |
| 864 |
* on name) if not. |
| 865 |
*/ |
| 866 |
if (entry->type == 0) |
| 867 |
entry->type = netsnmp_guess_interface_type(entry); |
| 868 |
|
| 869 |
netsnmp_derive_interface_id(entry); |
| 870 |
|
| 871 |
if (IANAIFTYPE_ETHERNETCSMACD == entry->type) { |
| 872 |
unsigned long long speed; |
| 873 |
unsigned long long defaultspeed = NOMINAL_LINK_SPEED; |
| 874 |
if (!(entry->os_flags & IFF_RUNNING)) { |
| 875 |
/* |
| 876 |
* use speed 0 if the if speed cannot be determined *and* the |
| 877 |
* interface is down |
| 878 |
*/ |
| 879 |
defaultspeed = 0; |
| 880 |
} |
| 881 |
speed = netsnmp_linux_interface_get_if_speed(fd, |
| 882 |
entry->name, defaultspeed); |
| 883 |
if (speed > 0xffffffffL) { |
| 884 |
entry->speed = 0xffffffff; |
| 885 |
} else |
| 886 |
entry->speed = speed; |
| 887 |
entry->speed_high = speed / 1000000LL; |
| 888 |
} else { |
| 889 |
netsnmp_access_interface_entry_guess_speed(entry); |
| 890 |
} |
| 891 |
|
| 892 |
netsnmp_access_interface_ioctl_flags_get(fd, entry); |
| 893 |
|
| 894 |
netsnmp_access_interface_ioctl_mtu_get(fd, entry); |
| 895 |
|
| 896 |
/* |
| 897 |
* Zero speed means link problem. |
| 898 |
* - i'm not sure this is always true... |
| 899 |
*/ |
| 900 |
if((entry->speed == 0) && (entry->os_flags & IFF_UP)) { |
| 901 |
entry->os_flags &= ~IFF_RUNNING; |
| 902 |
} |
| 903 |
|
| 904 |
/* |
| 905 |
* check for promiscuous mode. |
| 906 |
* NOTE: there are 2 ways to set promiscuous mode in Linux |
| 907 |
* (kernels later than 2.2.something) - using ioctls and |
| 908 |
* using setsockopt. The ioctl method tested here does not |
| 909 |
* detect if an interface was set using setsockopt. google |
| 910 |
* on IFF_PROMISC and linux to see lots of arguments about it. |
| 911 |
*/ |
| 912 |
if(entry->os_flags & IFF_PROMISC) { |
| 913 |
entry->promiscuous = 1; /* boolean */ |
| 914 |
} |
| 915 |
|
| 916 |
/* |
| 917 |
* hardcoded max packet size |
| 918 |
* (see ip_frag_reasm: if(len > 65535) goto out_oversize;) |
| 919 |
*/ |
| 920 |
entry->reasm_max_v4 = entry->reasm_max_v6 = 65535; |
| 921 |
entry->ns_flags |= |
| 922 |
NETSNMP_INTERFACE_FLAGS_HAS_V4_REASMMAX | |
| 923 |
NETSNMP_INTERFACE_FLAGS_HAS_V6_REASMMAX; |
| 924 |
|
| 925 |
netsnmp_access_interface_entry_overrides(entry); |
| 926 |
|
| 927 |
if (! (load_flags & NETSNMP_ACCESS_INTERFACE_LOAD_NO_STATS)) |
| 928 |
_parse_stats(entry, stats, scan_expected); |
| 929 |
|
| 930 |
if (flags & NETSNMP_INTERFACE_FLAGS_HAS_IPV4) |
| 931 |
_arch_interface_flags_v4_get(entry); |
| 932 |
|
| 933 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 934 |
if (flags & NETSNMP_INTERFACE_FLAGS_HAS_IPV6) |
| 935 |
_arch_interface_flags_v6_get(entry); |
| 936 |
#endif /* NETSNMP_ENABLE_IPV6 */ |
| 937 |
|
| 938 |
/* |
| 939 |
* add to container |
| 940 |
*/ |
| 941 |
if (CONTAINER_INSERT(container, entry) != 0) { |
| 942 |
netsnmp_interface_entry *existing = |
| 943 |
CONTAINER_FIND(container, entry); |
| 944 |
NETSNMP_LOGONCE((LOG_WARNING, |
| 945 |
"Encountered interface with index %" NETSNMP_PRIz "u twice: %s <> %s\n", |
| 946 |
entry->index, existing ? existing->name : "(?)", |
| 947 |
entry->name)); |
| 948 |
netsnmp_access_interface_entry_free(entry); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
rc = 0; |
| 953 |
|
| 954 |
free_ifc: |
| 955 |
free(ifc.ifc_buf); |
| 956 |
|
| 957 |
free_addr_container: |
| 958 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 959 |
netsnmp_access_ipaddress_container_free(addr_container, 0); |
| 960 |
#endif |
| 961 |
|
| 962 |
close(fd); |
| 963 |
|
| 964 |
close_devin: |
| 965 |
fclose(devin); |
| 966 |
|
| 967 |
return rc; |
| 968 |
} |
| 969 |
|
| 970 |
#ifndef NETSNMP_FEATURE_REMOVE_INTERFACE_ARCH_SET_ADMIN_STATUS |
| 971 |
int |
| 972 |
netsnmp_arch_set_admin_status(netsnmp_interface_entry * entry, |
| 973 |
int ifAdminStatus_val) |
| 974 |
{ |
| 975 |
int and_complement; |
| 976 |
|
| 977 |
DEBUGMSGTL(("access:interface:arch", "set_admin_status\n")); |
| 978 |
|
| 979 |
if(IFADMINSTATUS_UP == ifAdminStatus_val) |
| 980 |
and_complement = 0; /* |= */ |
| 981 |
else |
| 982 |
and_complement = 1; /* &= ~ */ |
| 983 |
|
| 984 |
return netsnmp_access_interface_ioctl_flags_set(-1, entry, |
| 985 |
IFF_UP, and_complement); |
| 986 |
} |
| 987 |
#endif /* NETSNMP_FEATURE_REMOVE_INTERFACE_ARCH_SET_ADMIN_STATUS */ |
| 988 |
|
| 989 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 990 |
/** |
| 991 |
* Determines network interface speed from ETHTOOL_GLINKSETTINGS |
| 992 |
* In case of failure revert to obsolete ETHTOOL_GSET |
| 993 |
*/ |
| 994 |
unsigned long long |
| 995 |
netsnmp_linux_interface_get_if_speed(int fd, const char *name, |
| 996 |
unsigned long long defaultspeed) |
| 997 |
{ |
| 998 |
int ret; |
| 999 |
struct netsnmp_linux_link_settings nlls; |
| 1000 |
uint32_t speed = -1; |
| 1001 |
|
| 1002 |
ret = netsnmp_get_link_settings(&nlls, fd, name); |
| 1003 |
if (ret < 0) { |
| 1004 |
DEBUGMSGTL(("mibII/interfaces", "ETHTOOL_GSET on %s failed (%d / %d)\n", |
| 1005 |
name, ret, speed)); |
| 1006 |
return netsnmp_linux_interface_get_if_speed_mii(fd, name, defaultspeed); |
| 1007 |
} |
| 1008 |
speed = nlls.speed; |
| 1009 |
if (speed == 0xffff || speed == 0xffffffffUL /*SPEED_UNKNOWN*/) |
| 1010 |
speed = defaultspeed; |
| 1011 |
/* return in bps */ |
| 1012 |
DEBUGMSGTL(("mibII/interfaces", "ETHTOOL_GSET on %s speed = %#x = %d\n", |
| 1013 |
name, speed, speed)); |
| 1014 |
return speed * 1000LL * 1000LL; |
| 1015 |
} |
| 1016 |
#endif |
| 1017 |
|
| 1018 |
/** |
| 1019 |
* Determines network interface speed from MII |
| 1020 |
*/ |
| 1021 |
unsigned long long |
| 1022 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 1023 |
netsnmp_linux_interface_get_if_speed_mii(int fd, const char *name, |
| 1024 |
unsigned long long defaultspeed) |
| 1025 |
#else |
| 1026 |
netsnmp_linux_interface_get_if_speed(int fd, const char *name, |
| 1027 |
unsigned long long defaultspeed) |
| 1028 |
#endif |
| 1029 |
{ |
| 1030 |
unsigned long long retspeed = defaultspeed; |
| 1031 |
struct ifreq ifr; |
| 1032 |
|
| 1033 |
/* the code is based on mii-diag utility by Donald Becker |
| 1034 |
* see ftp://ftp.scyld.com/pub/diag/mii-diag.c |
| 1035 |
*/ |
| 1036 |
ushort *data = (ushort *)(&ifr.ifr_data); |
| 1037 |
unsigned phy_id; |
| 1038 |
int mii_reg, i; |
| 1039 |
ushort mii_val[32]; |
| 1040 |
ushort bmcr, bmsr, nway_advert, lkpar; |
| 1041 |
const unsigned long long media_speeds[] = {10000000, 10000000, 100000000, 100000000, 10000000, 0}; |
| 1042 |
/* It corresponds to "10baseT", "10baseT-FD", "100baseTx", "100baseTx-FD", "100baseT4", "Flow-control", 0, */ |
| 1043 |
|
| 1044 |
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 1045 |
data[0] = 0; |
| 1046 |
|
| 1047 |
/* |
| 1048 |
* SIOCGMIIPHY has been defined since at least kernel 2.4.10 (Sept 2001). |
| 1049 |
* It's probably safe to drop the interim SIOCDEVPRIVATE handling now! |
| 1050 |
*/ |
| 1051 |
if (ioctl(fd, SIOCGMIIPHY, &ifr) < 0) { |
| 1052 |
DEBUGMSGTL(("mibII/interfaces", "SIOCGMIIPHY on %s failed\n", |
| 1053 |
ifr.ifr_name)); |
| 1054 |
return retspeed; |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* Begin getting mii register values */ |
| 1058 |
phy_id = data[0]; |
| 1059 |
for (mii_reg = 0; mii_reg < 8; mii_reg++){ |
| 1060 |
data[0] = phy_id; |
| 1061 |
data[1] = mii_reg; |
| 1062 |
if(ioctl(fd, SIOCGMIIREG, &ifr) <0){ |
| 1063 |
DEBUGMSGTL(("mibII/interfaces", "SIOCGMIIREG on %s failed\n", ifr.ifr_name)); |
| 1064 |
} |
| 1065 |
mii_val[mii_reg] = data[3]; |
| 1066 |
} |
| 1067 |
/*Parsing of mii values*/ |
| 1068 |
/*Invalid basic mode control register*/ |
| 1069 |
if (mii_val[0] == 0xffff || mii_val[1] == 0x0000) { |
| 1070 |
DEBUGMSGTL(("mibII/interfaces", "No MII transceiver present!.\n")); |
| 1071 |
return retspeed; |
| 1072 |
} |
| 1073 |
/* Descriptive rename. */ |
| 1074 |
bmcr = mii_val[0]; /*basic mode control register*/ |
| 1075 |
bmsr = mii_val[1]; /* basic mode status register*/ |
| 1076 |
nway_advert = mii_val[4]; /* autonegotiation advertisement*/ |
| 1077 |
lkpar = mii_val[5]; /*link partner ability*/ |
| 1078 |
|
| 1079 |
/*Check for link existence, returns 0 if link is absent*/ |
| 1080 |
if ((bmsr & 0x0016) != 0x0004){ |
| 1081 |
DEBUGMSGTL(("mibII/interfaces", "No link...\n")); |
| 1082 |
retspeed = 0; |
| 1083 |
return retspeed; |
| 1084 |
} |
| 1085 |
|
| 1086 |
if(!(bmcr & 0x1000) ){ |
| 1087 |
DEBUGMSGTL(("mibII/interfaces", "Auto-negotiation disabled.\n")); |
| 1088 |
retspeed = bmcr & 0x2000 ? 100000000 : 10000000; |
| 1089 |
return retspeed; |
| 1090 |
} |
| 1091 |
/* Link partner got our advertised abilities */ |
| 1092 |
if (lkpar & 0x4000) { |
| 1093 |
int negotiated = nway_advert & lkpar & 0x3e0; |
| 1094 |
int max_capability = 0; |
| 1095 |
/* Scan for the highest negotiated capability, highest priority |
| 1096 |
(100baseTx-FDX) to lowest (10baseT-HDX). */ |
| 1097 |
int media_priority[] = {8, 9, 7, 6, 5}; /* media_names[i-5] */ |
| 1098 |
for (i = 0; media_priority[i]; i++){ |
| 1099 |
if (negotiated & (1 << media_priority[i])) { |
| 1100 |
max_capability = media_priority[i]; |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
if (max_capability) |
| 1105 |
retspeed = media_speeds[max_capability - 5]; |
| 1106 |
else |
| 1107 |
DEBUGMSGTL(("mibII/interfaces", "No common media type was autonegotiated!\n")); |
| 1108 |
}else if(lkpar & 0x00A0){ |
| 1109 |
retspeed = (lkpar & 0x0080) ? 100000000 : 10000000; |
| 1110 |
} |
| 1111 |
return retspeed; |
| 1112 |
} |
| 1113 |
#ifdef SUPPORT_PREFIX_FLAGS |
| 1114 |
void netsnmp_prefix_process(int fd, void *data); |
| 1115 |
|
| 1116 |
/* Open netlink socket to watch new ipv6 addresses and prefixes. */ |
| 1117 |
int netsnmp_prefix_listen(void) |
| 1118 |
{ |
| 1119 |
struct { |
| 1120 |
struct nlmsghdr n; |
| 1121 |
struct ifinfomsg r; |
| 1122 |
char buf[1024]; |
| 1123 |
} req; |
| 1124 |
|
| 1125 |
struct rtattr *rta; |
| 1126 |
int status; |
| 1127 |
struct sockaddr_nl localaddrinfo; |
| 1128 |
unsigned groups = 0; |
| 1129 |
|
| 1130 |
int fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 1131 |
if (fd < 0) { |
| 1132 |
snmp_log(LOG_ERR, "netsnmp_prefix_listen: Cannot create socket.\n"); |
| 1133 |
return -1; |
| 1134 |
} |
| 1135 |
|
| 1136 |
memset(&localaddrinfo, 0, sizeof(struct sockaddr_nl)); |
| 1137 |
|
| 1138 |
groups |= RTMGRP_IPV6_IFADDR; |
| 1139 |
groups |= RTMGRP_IPV6_PREFIX; |
| 1140 |
localaddrinfo.nl_family = AF_NETLINK; |
| 1141 |
localaddrinfo.nl_groups = groups; |
| 1142 |
|
| 1143 |
if (bind(fd, (struct sockaddr*)&localaddrinfo, sizeof(localaddrinfo)) < 0) { |
| 1144 |
snmp_log(LOG_ERR,"netsnmp_prefix_listen: Bind failed.\n"); |
| 1145 |
close(fd); |
| 1146 |
return -1; |
| 1147 |
} |
| 1148 |
|
| 1149 |
memset(&req, 0, sizeof(req)); |
| 1150 |
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)); |
| 1151 |
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT; |
| 1152 |
req.n.nlmsg_type = RTM_GETLINK; |
| 1153 |
req.r.ifi_family = AF_INET6; |
| 1154 |
rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.n.nlmsg_len)); |
| 1155 |
rta->rta_len = RTA_LENGTH(16); |
| 1156 |
|
| 1157 |
status = send(fd, &req, req.n.nlmsg_len, 0); |
| 1158 |
if (status < 0) { |
| 1159 |
snmp_log(LOG_ERR,"netsnmp_prefix_listen: send failed\n"); |
| 1160 |
close(fd); |
| 1161 |
return -1; |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (register_readfd(fd, netsnmp_prefix_process, NULL) != 0) { |
| 1165 |
snmp_log(LOG_ERR,"netsnmp_prefix_listen: error registering netlink socket\n"); |
| 1166 |
close(fd); |
| 1167 |
return -1; |
| 1168 |
} |
| 1169 |
return 0; |
| 1170 |
} |
| 1171 |
|
| 1172 |
/* Process one incoming netlink packets. |
| 1173 |
* RTM_NEWADDR and RTM_NEWPREFIX usually arrive in separate packets |
| 1174 |
* -> information from these packets must be stored locally and |
| 1175 |
* new prefix is added when information from both packets is complete. |
| 1176 |
*/ |
| 1177 |
void netsnmp_prefix_process(int fd, void *data) |
| 1178 |
{ |
| 1179 |
int status; |
| 1180 |
char buf[16384]; |
| 1181 |
struct nlmsghdr *nlmp; |
| 1182 |
struct rtattr *rtatp; |
| 1183 |
struct ifaddrmsg *ifa; |
| 1184 |
struct prefixmsg *prefix; |
| 1185 |
struct in6_addr *in6p; |
| 1186 |
|
| 1187 |
/* these values must persist between calls */ |
| 1188 |
static char in6pAddr[40]; |
| 1189 |
static int have_addr = 0,have_prefix = 0; |
| 1190 |
static int onlink = 2,autonomous = 2; /*Assume as false*/ |
| 1191 |
|
| 1192 |
int iret; |
| 1193 |
prefix_cbx *new; |
| 1194 |
int len, req_len, length; |
| 1195 |
|
| 1196 |
status = recv(fd, buf, sizeof(buf), 0); |
| 1197 |
if (status < 0) { |
| 1198 |
if (errno == EINTR) |
| 1199 |
return; |
| 1200 |
snmp_log(LOG_ERR,"netsnmp_prefix_listen: Receive failed.\n"); |
| 1201 |
return; |
| 1202 |
} |
| 1203 |
|
| 1204 |
if(status == 0){ |
| 1205 |
DEBUGMSGTL(("access:interface:prefix", "End of File\n")); |
| 1206 |
return; |
| 1207 |
} |
| 1208 |
|
| 1209 |
for(nlmp = (struct nlmsghdr *)buf; status > sizeof(*nlmp);){ |
| 1210 |
len = nlmp->nlmsg_len; |
| 1211 |
req_len = len - sizeof(*nlmp); |
| 1212 |
|
| 1213 |
if (req_len < 0 || len > status) { |
| 1214 |
snmp_log(LOG_ERR,"netsnmp_prefix_listen: Error in length.\n"); |
| 1215 |
return; |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (!NLMSG_OK(nlmp, status)) { |
| 1219 |
DEBUGMSGTL(("access:interface:prefix", "NLMSG not OK\n")); |
| 1220 |
continue; |
| 1221 |
} |
| 1222 |
|
| 1223 |
if (nlmp->nlmsg_type == RTM_NEWADDR || nlmp->nlmsg_type == RTM_DELADDR) { |
| 1224 |
ifa = NLMSG_DATA(nlmp); |
| 1225 |
length = nlmp->nlmsg_len; |
| 1226 |
length -= NLMSG_LENGTH(sizeof(*ifa)); |
| 1227 |
|
| 1228 |
if (length < 0) { |
| 1229 |
DEBUGMSGTL(("access:interface:prefix", "wrong nlmsg length %d\n", length)); |
| 1230 |
continue; |
| 1231 |
} |
| 1232 |
|
| 1233 |
if(!ifa->ifa_flags) { |
| 1234 |
rtatp = IFA_RTA(ifa); |
| 1235 |
while (RTA_OK(rtatp, length)) { |
| 1236 |
if (rtatp->rta_type == IFA_ADDRESS){ |
| 1237 |
in6p = (struct in6_addr *) RTA_DATA(rtatp); |
| 1238 |
if(nlmp->nlmsg_type == RTM_DELADDR) { |
| 1239 |
snprintf(in6pAddr, sizeof(in6pAddr), "%04x%04x%04x%04x%04x%04x%04x%04x", NIP6(*in6p)); |
| 1240 |
have_addr = -1; |
| 1241 |
break; |
| 1242 |
} else { |
| 1243 |
snprintf(in6pAddr, sizeof(in6pAddr), "%04x%04x%04x%04x%04x%04x%04x%04x", NIP6(*in6p)); |
| 1244 |
have_addr = 1; |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
rtatp = RTA_NEXT(rtatp,length); |
| 1249 |
} |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
if(nlmp->nlmsg_type == RTM_NEWPREFIX) { |
| 1254 |
prefix = NLMSG_DATA(nlmp); |
| 1255 |
length = nlmp->nlmsg_len; |
| 1256 |
length -= NLMSG_LENGTH(sizeof(*prefix)); |
| 1257 |
|
| 1258 |
if (length < 0) { |
| 1259 |
DEBUGMSGTL(("access:interface:prefix", "wrong nlmsg length %d\n", length)); |
| 1260 |
continue; |
| 1261 |
} |
| 1262 |
have_prefix = 1; |
| 1263 |
if (prefix->prefix_flags & IF_PREFIX_ONLINK) { |
| 1264 |
onlink = 1; |
| 1265 |
} |
| 1266 |
if (prefix->prefix_flags & IF_PREFIX_AUTOCONF) { |
| 1267 |
autonomous = 1; |
| 1268 |
} |
| 1269 |
} |
| 1270 |
status -= NLMSG_ALIGN(len); |
| 1271 |
nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len)); |
| 1272 |
} |
| 1273 |
|
| 1274 |
if((have_addr == 1) && (have_prefix == 1)){ |
| 1275 |
if(!(new = net_snmp_create_prefix_info (onlink, autonomous, in6pAddr))) |
| 1276 |
DEBUGMSGTL(("access:interface:prefix", "Unable to create prefix info\n")); |
| 1277 |
else { |
| 1278 |
|
| 1279 |
iret = net_snmp_search_update_prefix_info (list_info.list_head, new, 0); |
| 1280 |
if(iret < 0) { |
| 1281 |
DEBUGMSGTL(("access:interface:prefix", "Unable to add/update prefix info\n")); |
| 1282 |
free(new); |
| 1283 |
} |
| 1284 |
if(iret == 2) /*Only when enrty already exists and we are only updating*/ |
| 1285 |
free(new); |
| 1286 |
} |
| 1287 |
have_addr = have_prefix = 0; |
| 1288 |
onlink = autonomous = 2; /*Set to defaults again*/ |
| 1289 |
} else if (have_addr == -1) { |
| 1290 |
iret = net_snmp_delete_prefix_info (list_info.list_head, in6pAddr); |
| 1291 |
if(iret < 0) |
| 1292 |
DEBUGMSGTL(("access:interface:prefix", "Unable to delete the prefix info\n")); |
| 1293 |
if(!iret) |
| 1294 |
DEBUGMSGTL(("access:interface:prefix", "Unable to find the node to delete\n")); |
| 1295 |
have_addr = 0; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
#endif |
| 1299 |
|