| 1 |
/* |
| 2 |
* Interface MIB architecture support |
| 3 |
* |
| 4 |
* Based on patch 1362403, submited by Rojer |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
#include <net-snmp/net-snmp-config.h> |
| 9 |
#include <net-snmp/net-snmp-features.h> |
| 10 |
#include <net-snmp/net-snmp-includes.h> |
| 11 |
#include "mibII/mibII_common.h" |
| 12 |
#include "if-mib/ifTable/ifTable_constants.h" |
| 13 |
|
| 14 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 15 |
|
| 16 |
#ifdef HAVE_SYS_IOCTL_H |
| 17 |
#include <sys/ioctl.h> |
| 18 |
#else |
| 19 |
#error "BSD should have sys/ioctl header" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <net-snmp/data_access/interface.h> |
| 23 |
#include <net-snmp/data_access/ipaddress.h> |
| 24 |
#include "if-mib/data_access/interface.h" |
| 25 |
#include "interface_private.h" |
| 26 |
|
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <net/if.h> |
| 30 |
#include <net/if_dl.h> |
| 31 |
#include <net/if_types.h> |
| 32 |
#include <net/if_media.h> |
| 33 |
|
| 34 |
netsnmp_feature_child_of(interface_arch_set_admin_status, interface_all); |
| 35 |
|
| 36 |
/* |
| 37 |
* account for minor differences between FreeBSD and OpenBSD. |
| 38 |
* If this gets unruly, split stuff out into the respective |
| 39 |
* files (interface_*BDS.c). |
| 40 |
*/ |
| 41 |
/* |
| 42 |
* FreeBSD has 2 promiscuous flags: kernel/user; check either |
| 43 |
* http://unix.derkeiler.com/Mailing-Lists/FreeBSD/net/2004-09/0289.html |
| 44 |
* which says: |
| 45 |
* The first flag (IFF_PROMISC) is the one that the kernel code uses and |
| 46 |
* sets on an interface's ifp structure. The second one is the one that |
| 47 |
* comes from user space programs and is sent to the routine ifhwioctl() |
| 48 |
* to set the first flag. |
| 49 |
*/ |
| 50 |
#ifdef IFF_PPROMISC |
| 51 |
# define ARCH_PROMISC_FLAG (IFF_PPROMISC|IFF_PROMISC) |
| 52 |
#else |
| 53 |
# define ARCH_PROMISC_FLAG IFF_PROMISC |
| 54 |
#endif |
| 55 |
|
| 56 |
#define starttime (*(const struct timeval*)netsnmp_get_agent_starttime()) |
| 57 |
|
| 58 |
/* sa_len roundup macro. */ |
| 59 |
#define ROUNDUP(a) \ |
| 60 |
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 61 |
|
| 62 |
void |
| 63 |
netsnmp_arch_interface_init(void) |
| 64 |
{ |
| 65 |
/* |
| 66 |
* nothing to do |
| 67 |
*/ |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
* find the ifIndex for an interface name |
| 72 |
* |
| 73 |
* @retval 0 : no index found |
| 74 |
* @retval >0: ifIndex for interface |
| 75 |
*/ |
| 76 |
oid |
| 77 |
netsnmp_arch_interface_index_find(const char *name) |
| 78 |
{ |
| 79 |
return if_nametoindex(name); |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* subroutine to translate known media typed to speed. |
| 84 |
* see /usr/include/net/if_media.h for definitions |
| 85 |
*/ |
| 86 |
|
| 87 |
void |
| 88 |
netsnmp_sysctl_ifmedia_to_speed(int media, u_int *speed, |
| 89 |
u_int *speed_high) |
| 90 |
{ |
| 91 |
*speed = 0; |
| 92 |
*speed_high = 0; |
| 93 |
|
| 94 |
switch (IFM_TYPE(media)) { |
| 95 |
case IFM_ETHER: |
| 96 |
switch (IFM_SUBTYPE(media)) { |
| 97 |
case IFM_10_T: |
| 98 |
case IFM_10_2: |
| 99 |
case IFM_10_5: |
| 100 |
case IFM_10_STP: |
| 101 |
case IFM_10_FL: |
| 102 |
*speed = 10000000; |
| 103 |
*speed_high = 10; break; |
| 104 |
case IFM_100_TX: |
| 105 |
case IFM_100_FX: |
| 106 |
case IFM_100_T4: |
| 107 |
case IFM_100_VG: |
| 108 |
case IFM_100_T2: |
| 109 |
#ifdef IFM_100_T |
| 110 |
case IFM_100_T: |
| 111 |
#endif |
| 112 |
*speed = 100000000; |
| 113 |
*speed_high = 100; break; |
| 114 |
case IFM_1000_LX: |
| 115 |
case IFM_1000_CX: |
| 116 |
#ifdef IFM_1000_T |
| 117 |
case IFM_1000_T: |
| 118 |
#endif |
| 119 |
#ifdef IFM_1000_SX |
| 120 |
case IFM_1000_SX: |
| 121 |
#endif |
| 122 |
#ifdef IFM_1000_KX |
| 123 |
case IFM_1000_KX: |
| 124 |
#endif |
| 125 |
#ifdef IFM_1000_SGMII |
| 126 |
case IFM_1000_SGMII: |
| 127 |
#endif |
| 128 |
#ifdef IFM_1000_CX_SGMII |
| 129 |
case IFM_1000_CX_SGMII: |
| 130 |
#endif |
| 131 |
*speed = 1000000000; |
| 132 |
*speed_high = 1000; break; |
| 133 |
#ifdef IFM_10GBASE_SR |
| 134 |
case IFM_10GBASE_SR: |
| 135 |
case IFM_10GBASE_LR: |
| 136 |
*speed = (u_int) -1; /* 4294967295; */ |
| 137 |
*speed_high = 10000; break; |
| 138 |
#endif |
| 139 |
#if defined(freebsd3) |
| 140 |
#ifdef IFM_10G_LR |
| 141 |
case IFM_10G_LR: |
| 142 |
#ifdef IFM_10G_SR |
| 143 |
case IFM_10G_SR: |
| 144 |
#endif |
| 145 |
#ifdef IFM_10G_CX4 |
| 146 |
case IFM_10G_CX4: |
| 147 |
#endif |
| 148 |
#ifdef IFM_10G_TWINAX |
| 149 |
case IFM_10G_TWINAX: |
| 150 |
#endif |
| 151 |
#ifdef IFM_10G_TWINAX_LONG |
| 152 |
case IFM_10G_TWINAX_LONG: |
| 153 |
#endif |
| 154 |
#ifdef IFM_10G_LRM |
| 155 |
case IFM_10G_LRM: |
| 156 |
#endif |
| 157 |
#ifdef IFM_10G_T |
| 158 |
case IFM_10G_T: |
| 159 |
#endif |
| 160 |
#ifdef IFM_10G_KX4 |
| 161 |
case IFM_10G_KX4: |
| 162 |
#endif |
| 163 |
#ifdef IFM_10G_KR |
| 164 |
case IFM_10G_KR: |
| 165 |
#endif |
| 166 |
#ifdef IFM_10G_CR1 |
| 167 |
case IFM_10G_CR1: |
| 168 |
#endif |
| 169 |
#ifdef IFM_10G_SFI |
| 170 |
case IFM_10G_SFI: |
| 171 |
#endif |
| 172 |
#ifdef IFM_10G_ER |
| 173 |
case IFM_10G_ER: |
| 174 |
#endif |
| 175 |
#ifdef IFM_10G_AOC |
| 176 |
case IFM_10G_AOC: |
| 177 |
#endif |
| 178 |
*speed = (u_int) -1; /* 4294967295; */ |
| 179 |
*speed_high = 10000; break; |
| 180 |
#endif /* IFM_10G_LR */ |
| 181 |
#ifdef IFM_20G_KR2 |
| 182 |
case IFM_20G_KR2: |
| 183 |
*speed = (u_int) -1; /* 4294967295; */ |
| 184 |
*speed_high = 20000; break; |
| 185 |
#endif /* IFM_20G_KR2 */ |
| 186 |
#ifdef IFM_25G_PCIE |
| 187 |
case IFM_25G_PCIE: |
| 188 |
#ifdef IFM_25G_CR |
| 189 |
case IFM_25G_CR: |
| 190 |
#endif |
| 191 |
#ifdef IFM_25G_KR |
| 192 |
case IFM_25G_KR: |
| 193 |
#endif |
| 194 |
#ifdef IFM_25G_SR |
| 195 |
case IFM_25G_SR: |
| 196 |
#endif |
| 197 |
#ifdef IFM_25G_LR |
| 198 |
case IFM_25G_LR: |
| 199 |
#endif |
| 200 |
#ifdef IFM_25G_ACC |
| 201 |
case IFM_25G_ACC: |
| 202 |
#endif |
| 203 |
#ifdef IFM_25G_AOC |
| 204 |
case IFM_25G_AOC: |
| 205 |
#endif |
| 206 |
*speed = (u_int) -1; /* 4294967295; */ |
| 207 |
*speed_high = 25000; break; |
| 208 |
#endif /* IFM_25G_PCIE */ |
| 209 |
#ifdef IFM_40G_CR4 |
| 210 |
case IFM_40G_CR4: |
| 211 |
#ifdef IFM_40G_SR4 |
| 212 |
case IFM_40G_SR4: |
| 213 |
#endif |
| 214 |
#ifdef IFM_40G_LR4 |
| 215 |
case IFM_40G_LR4: |
| 216 |
#endif |
| 217 |
#ifdef IFM_40G_XLPPI |
| 218 |
case IFM_40G_XLPPI: |
| 219 |
#endif |
| 220 |
#ifdef IFM_40G_KR4 |
| 221 |
case IFM_40G_KR4: |
| 222 |
#endif |
| 223 |
*speed = (u_int) -1; /* 4294967295; */ |
| 224 |
*speed_high = 40000; break; |
| 225 |
#endif /* IFM_40G_CR4 */ |
| 226 |
#ifdef IFM_50G_PCIE |
| 227 |
case IFM_50G_PCIE: |
| 228 |
#ifdef IFM_50G_CR2 |
| 229 |
case IFM_50G_CR2: |
| 230 |
#endif |
| 231 |
#ifdef IFM_50G_KR2 |
| 232 |
case IFM_50G_KR2: |
| 233 |
#endif |
| 234 |
*speed = (u_int) -1; /* 4294967295; */ |
| 235 |
*speed_high = 50000; break; |
| 236 |
#endif /* IFM_50G_PCIE */ |
| 237 |
#ifdef IFM_100G_CR4 |
| 238 |
case IFM_100G_CR4: |
| 239 |
#ifdef IFM_100G_SR4 |
| 240 |
case IFM_100G_SR4: |
| 241 |
#endif |
| 242 |
#ifdef IFM_100G_KR4 |
| 243 |
case IFM_100G_KR4: |
| 244 |
#endif |
| 245 |
#ifdef IFM_100G_LR4 |
| 246 |
case IFM_100G_LR4: |
| 247 |
#endif |
| 248 |
*speed = (u_int) -1; /* 4294967295; */ |
| 249 |
*speed_high = 100000; break; |
| 250 |
#endif /* IFM_100G_CR4 */ |
| 251 |
#endif /* freebsd3 */ |
| 252 |
} |
| 253 |
break; |
| 254 |
case IFM_IEEE80211: |
| 255 |
switch (IFM_SUBTYPE(media)) { |
| 256 |
case IFM_IEEE80211_FH1: |
| 257 |
case IFM_IEEE80211_DS1: |
| 258 |
*speed = 1000000; |
| 259 |
*speed_high = 1; |
| 260 |
break; |
| 261 |
case IFM_IEEE80211_FH2: |
| 262 |
case IFM_IEEE80211_DS2: |
| 263 |
*speed = 2000000; |
| 264 |
*speed_high = 2; |
| 265 |
break; |
| 266 |
case IFM_IEEE80211_DS5: |
| 267 |
*speed = 5500000; |
| 268 |
*speed_high = 5; |
| 269 |
break; |
| 270 |
case IFM_IEEE80211_DS11: |
| 271 |
*speed = 11000000; |
| 272 |
*speed_high = 11; |
| 273 |
break; |
| 274 |
case IFM_IEEE80211_DS22: |
| 275 |
*speed = 22000000; |
| 276 |
*speed_high = 22; |
| 277 |
break; |
| 278 |
#ifdef IFM_IEEE80211_OFDM6 |
| 279 |
case IFM_IEEE80211_OFDM6: |
| 280 |
*speed = 6000000; |
| 281 |
*speed_high = 6; |
| 282 |
break; |
| 283 |
case IFM_IEEE80211_OFDM9: |
| 284 |
*speed = 9000000; |
| 285 |
*speed_high = 9; |
| 286 |
break; |
| 287 |
case IFM_IEEE80211_OFDM12: |
| 288 |
*speed = 12000000; |
| 289 |
*speed_high = 12; |
| 290 |
break; |
| 291 |
case IFM_IEEE80211_OFDM18: |
| 292 |
*speed = 18000000; |
| 293 |
*speed_high = 18; |
| 294 |
break; |
| 295 |
case IFM_IEEE80211_OFDM24: |
| 296 |
*speed = 24000000; |
| 297 |
*speed_high = 24; |
| 298 |
break; |
| 299 |
case IFM_IEEE80211_OFDM36: |
| 300 |
*speed = 36000000; |
| 301 |
*speed_high = 36; |
| 302 |
break; |
| 303 |
case IFM_IEEE80211_OFDM48: |
| 304 |
*speed = 48000000; |
| 305 |
*speed_high = 48; |
| 306 |
break; |
| 307 |
case IFM_IEEE80211_OFDM54: |
| 308 |
*speed = 54000000; |
| 309 |
*speed_high = 54; |
| 310 |
break; |
| 311 |
case IFM_IEEE80211_OFDM72: |
| 312 |
*speed = 72000000; |
| 313 |
*speed_high = 72; |
| 314 |
break; |
| 315 |
#endif |
| 316 |
} |
| 317 |
break; |
| 318 |
#if defined(IFM_TOKEN) |
| 319 |
case IFM_TOKEN: |
| 320 |
switch (IFM_SUBTYPE(media)) { |
| 321 |
case IFM_TOK_STP4: |
| 322 |
case IFM_TOK_UTP4: |
| 323 |
*speed = 4000000; |
| 324 |
*speed_high = 4; |
| 325 |
break; |
| 326 |
case IFM_TOK_STP16: |
| 327 |
case IFM_TOK_UTP16: |
| 328 |
*speed = 16000000; |
| 329 |
*speed_high = 16; |
| 330 |
break; |
| 331 |
#if defined(IFM_TOK_STP100) /* guessing if you've got one, you've got the other.. */ |
| 332 |
case IFM_TOK_STP100: |
| 333 |
case IFM_TOK_UTP100: |
| 334 |
*speed = 100000000; |
| 335 |
*speed_high = 100; |
| 336 |
break; |
| 337 |
#endif /* IFM_TOK_STP100 */ |
| 338 |
} |
| 339 |
break; |
| 340 |
#endif /* IFM_TOKEN */ |
| 341 |
#ifdef IFM_ATM |
| 342 |
case IFM_ATM: |
| 343 |
switch (IFM_SUBTYPE(media)) { |
| 344 |
case IFM_ATM_MM_155: |
| 345 |
case IFM_ATM_SM_155: |
| 346 |
*speed = 155000000; |
| 347 |
*speed_high = 155; |
| 348 |
break; |
| 349 |
case IFM_ATM_MM_622: |
| 350 |
case IFM_ATM_SM_622: |
| 351 |
*speed = 622000000; |
| 352 |
*speed_high = 622; |
| 353 |
break; |
| 354 |
} |
| 355 |
#endif |
| 356 |
} |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
/* |
| 361 |
* @retval 0 speed could not be determined (error, unknown media) |
| 362 |
* @retval >0 speed, equal to *speed. |
| 363 |
* sets *speed=2^31 and returns *speed_high=speed/10^6 as required |
| 364 |
* by ifSpeed/ifHighSpeed. |
| 365 |
*/ |
| 366 |
|
| 367 |
int |
| 368 |
netsnmp_sysctl_get_if_speed(char *name, u_int *speed, |
| 369 |
u_int *speed_high) |
| 370 |
{ |
| 371 |
int s; |
| 372 |
struct ifmediareq ifmr; |
| 373 |
#if defined(OpenBSD) && OpenBSD >= 201605 |
| 374 |
uint64_t *media_list; |
| 375 |
#else |
| 376 |
int *media_list; |
| 377 |
#endif |
| 378 |
int i; |
| 379 |
u_int t_speed, t_speed_high; |
| 380 |
u_int m_speed, m_speed_high; |
| 381 |
|
| 382 |
*speed = 0; |
| 383 |
*speed_high = 0; |
| 384 |
|
| 385 |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
memset(&ifmr, 0, sizeof(ifmr)); |
| 390 |
strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); |
| 391 |
|
| 392 |
DEBUGMSGTL(("access:interface:container:sysctl"," speed in\n")); |
| 393 |
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0 || |
| 394 |
ifmr.ifm_count == 0) { |
| 395 |
|
| 396 |
close(s); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
/* |
| 401 |
* try to get speed from current media. |
| 402 |
* if unsuccessful (e.g., interface is down), get a list of capabilities, |
| 403 |
* try each and return maximum speed the interface is capable of. |
| 404 |
*/ |
| 405 |
|
| 406 |
netsnmp_sysctl_ifmedia_to_speed(ifmr.ifm_current, speed, speed_high); |
| 407 |
|
| 408 |
if (*speed == 0 && |
| 409 |
(media_list = malloc(ifmr.ifm_count * sizeof(*media_list))) != NULL ) { |
| 410 |
|
| 411 |
ifmr.ifm_ulist = media_list; |
| 412 |
|
| 413 |
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == 0) { |
| 414 |
m_speed = 0; |
| 415 |
m_speed_high = 0; |
| 416 |
for (i = 0; i < ifmr.ifm_count; i++) { |
| 417 |
|
| 418 |
netsnmp_sysctl_ifmedia_to_speed(media_list[i], &t_speed, |
| 419 |
&t_speed_high); |
| 420 |
if (t_speed_high > m_speed_high || |
| 421 |
(t_speed_high == m_speed_high && t_speed > m_speed)) { |
| 422 |
m_speed_high = t_speed_high; |
| 423 |
m_speed = t_speed; |
| 424 |
} |
| 425 |
} |
| 426 |
*speed = m_speed; |
| 427 |
*speed_high = m_speed_high; |
| 428 |
} |
| 429 |
free(media_list); |
| 430 |
} |
| 431 |
|
| 432 |
close(s); |
| 433 |
|
| 434 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 435 |
"%s: speed: %u, speed_high: %u\n", |
| 436 |
name, *speed, *speed_high)); |
| 437 |
|
| 438 |
return *speed; |
| 439 |
} |
| 440 |
|
| 441 |
static void set_counter(struct counter64 *c, uint64_t v) |
| 442 |
{ |
| 443 |
c->low = (uint32_t)(v); |
| 444 |
c->high = (v) >> 32; |
| 445 |
} |
| 446 |
|
| 447 |
/* |
| 448 |
* |
| 449 |
* @retval 0 success |
| 450 |
* @retval -1 no container specified |
| 451 |
* @retval -2 could not get interface info |
| 452 |
* @retval -3 could not create entry (probably malloc) |
| 453 |
*/ |
| 454 |
int |
| 455 |
netsnmp_arch_interface_container_load(netsnmp_container* container, |
| 456 |
u_int load_flags) |
| 457 |
{ |
| 458 |
netsnmp_interface_entry *entry = NULL; |
| 459 |
u_char *if_list = NULL, *cp; |
| 460 |
size_t if_list_size = 0; |
| 461 |
struct if_msghdr *ifp; |
| 462 |
int sysctl_oid[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 }; |
| 463 |
struct ifa_msghdr *ifa; |
| 464 |
struct sockaddr *a; |
| 465 |
struct sockaddr_dl *adl; |
| 466 |
int amask; |
| 467 |
char *if_name; |
| 468 |
#ifdef HAVE_STRUCT_IFNET_IF_LASTCHANGE_TV_NSEC |
| 469 |
struct timespec startspec; |
| 470 |
|
| 471 |
TIMEVAL_TO_TIMESPEC(&starttime, &startspec); |
| 472 |
#endif |
| 473 |
|
| 474 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 475 |
"load (flags %u)\n", load_flags)); |
| 476 |
|
| 477 |
if (NULL == container) { |
| 478 |
snmp_log(LOG_ERR, "no container specified/found for interface\n"); |
| 479 |
return -1; |
| 480 |
} |
| 481 |
|
| 482 |
if (sysctl(sysctl_oid, sizeof(sysctl_oid)/sizeof(int), 0, |
| 483 |
&if_list_size, 0, 0) == -1) { |
| 484 |
snmp_log(LOG_ERR, "could not get interface info (size)\n"); |
| 485 |
return -2; |
| 486 |
} |
| 487 |
|
| 488 |
if_list = (u_char*)malloc(if_list_size); |
| 489 |
if (if_list == NULL) { |
| 490 |
snmp_log(LOG_ERR, "could not allocate memory for interface info " |
| 491 |
"(%u bytes)\n", (unsigned) if_list_size); |
| 492 |
return -3; |
| 493 |
} else { |
| 494 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 495 |
"allocated %u bytes for if_list\n", |
| 496 |
(unsigned) if_list_size)); |
| 497 |
} |
| 498 |
|
| 499 |
if (sysctl(sysctl_oid, sizeof(sysctl_oid)/sizeof(int), if_list, |
| 500 |
&if_list_size, 0, 0) == -1) { |
| 501 |
snmp_log(LOG_ERR, "could not get interface info\n"); |
| 502 |
free(if_list); |
| 503 |
return -2; |
| 504 |
} |
| 505 |
|
| 506 |
/* 1st pass: create interface entries */ |
| 507 |
for (cp = if_list; cp < if_list + if_list_size; cp += ifp->ifm_msglen) { |
| 508 |
|
| 509 |
ifp = (struct if_msghdr *) cp; |
| 510 |
if_name = NULL; |
| 511 |
adl = NULL; |
| 512 |
|
| 513 |
if (ifp->ifm_type != RTM_IFINFO) |
| 514 |
continue; |
| 515 |
|
| 516 |
if (ifp->ifm_addrs & RTA_IFP) { |
| 517 |
a = (struct sockaddr *) (ifp + 1); |
| 518 |
/* |
| 519 |
* if_msghdr is followed by one or more sockaddrs, of which we |
| 520 |
* need only RTA_IFP. most of the time RTA_IFP is the first |
| 521 |
* address we get, hence the shortcut. |
| 522 |
*/ |
| 523 |
if ((ifp->ifm_addrs & (~RTA_IFP - 1)) != 0) { |
| 524 |
/* skip all addresses up to RTA_IFP. */ |
| 525 |
for (amask = (RTA_IFP >> 1); amask != 0; amask >>= 1) { |
| 526 |
if (ifp->ifm_addrs & amask) |
| 527 |
a = (struct sockaddr *) |
| 528 |
( ((char *) a) + ROUNDUP(a->sa_len) ); |
| 529 |
} |
| 530 |
} |
| 531 |
adl = (struct sockaddr_dl *) a; |
| 532 |
if_name = malloc(adl->sdl_nlen + 1); |
| 533 |
if (if_name) { |
| 534 |
memcpy(if_name, adl->sdl_data, adl->sdl_nlen); |
| 535 |
if_name[adl->sdl_nlen] = '\0'; |
| 536 |
} |
| 537 |
} |
| 538 |
if (!(ifp->ifm_addrs & RTA_IFP) || if_name == NULL) { |
| 539 |
snmp_log(LOG_ERR, "ifm_index %u: no interface name in message, " |
| 540 |
"skipping\n", ifp->ifm_index); |
| 541 |
continue; |
| 542 |
} |
| 543 |
|
| 544 |
entry = netsnmp_access_interface_entry_create(if_name, ifp->ifm_index); |
| 545 |
free(if_name); |
| 546 |
if(NULL == entry) { |
| 547 |
free(if_list); |
| 548 |
return -3; |
| 549 |
} |
| 550 |
|
| 551 |
/* get physical address */ |
| 552 |
if (adl != NULL && adl->sdl_alen > 0) { |
| 553 |
entry->paddr_len = adl->sdl_alen; |
| 554 |
entry->paddr = (char*)malloc(entry->paddr_len); |
| 555 |
memcpy(entry->paddr, adl->sdl_data + adl->sdl_nlen, adl->sdl_alen); |
| 556 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 557 |
"%s: paddr_len=%d, entry->paddr=%x:%x:%x:%x:%x:%x\n", |
| 558 |
if_name, entry->paddr_len, |
| 559 |
entry->paddr[0], entry->paddr[1], entry->paddr[2], |
| 560 |
entry->paddr[3], entry->paddr[4], entry->paddr[5])); |
| 561 |
} else { |
| 562 |
entry->paddr = (char*)malloc(6); |
| 563 |
entry->paddr_len = 6; |
| 564 |
memset(entry->paddr, 0, 6); |
| 565 |
} |
| 566 |
|
| 567 |
entry->mtu = ifp->ifm_data.ifi_mtu; |
| 568 |
entry->type = ifp->ifm_data.ifi_type; |
| 569 |
|
| 570 |
|
| 571 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IF_FLAGS; |
| 572 |
entry->os_flags = ifp->ifm_flags; |
| 573 |
|
| 574 |
if (ifp->ifm_flags & IFF_UP) { |
| 575 |
entry->admin_status = IFADMINSTATUS_UP; |
| 576 |
#if defined( LINK_STATE_UP ) && defined( LINK_STATE_DOWN ) |
| 577 |
if (ifp->ifm_data.ifi_link_state == LINK_STATE_UP) { |
| 578 |
entry->oper_status = IFOPERSTATUS_UP; |
| 579 |
} else if (ifp->ifm_data.ifi_link_state == LINK_STATE_DOWN) { |
| 580 |
entry->oper_status = IFOPERSTATUS_DOWN; |
| 581 |
} else |
| 582 |
#endif |
| 583 |
{ |
| 584 |
/* |
| 585 |
* link state is unknown, which is not very useful to report. |
| 586 |
* use running state instead. |
| 587 |
*/ |
| 588 |
entry->oper_status = ifp->ifm_flags & IFF_RUNNING ? 1 : 2; |
| 589 |
} |
| 590 |
} else { |
| 591 |
entry->admin_status = IFADMINSTATUS_DOWN; |
| 592 |
/* |
| 593 |
* IF-MIB specifically says that ifOperStatus should be down in |
| 594 |
* this case |
| 595 |
*/ |
| 596 |
entry->oper_status = IFOPERSTATUS_DOWN; |
| 597 |
} |
| 598 |
|
| 599 |
entry->reasm_max_v4 = entry->reasm_max_v6 = IP_MAXPACKET; |
| 600 |
entry->ns_flags |= |
| 601 |
NETSNMP_INTERFACE_FLAGS_HAS_V4_REASMMAX | |
| 602 |
NETSNMP_INTERFACE_FLAGS_HAS_V6_REASMMAX; |
| 603 |
|
| 604 |
/* get counters */ |
| 605 |
set_counter(&entry->stats.ibytes, ifp->ifm_data.ifi_ibytes); |
| 606 |
set_counter(&entry->stats.iucast, ifp->ifm_data.ifi_ipackets); |
| 607 |
set_counter(&entry->stats.imcast, ifp->ifm_data.ifi_imcasts); |
| 608 |
entry->stats.ierrors = ifp->ifm_data.ifi_ierrors; |
| 609 |
entry->stats.idiscards = ifp->ifm_data.ifi_iqdrops; |
| 610 |
entry->stats.iunknown_protos = ifp->ifm_data.ifi_noproto; |
| 611 |
|
| 612 |
set_counter(&entry->stats.obytes, ifp->ifm_data.ifi_obytes); |
| 613 |
set_counter(&entry->stats.oucast, ifp->ifm_data.ifi_opackets); |
| 614 |
set_counter(&entry->stats.omcast, ifp->ifm_data.ifi_omcasts); |
| 615 |
entry->stats.oerrors = ifp->ifm_data.ifi_oerrors; |
| 616 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_BYTES | |
| 617 |
NETSNMP_INTERFACE_FLAGS_HAS_DROPS | |
| 618 |
NETSNMP_INTERFACE_FLAGS_HAS_MCAST_PKTS; |
| 619 |
|
| 620 |
#ifdef HAVE_STRUCT_IFNET_IF_LASTCHANGE_TV_NSEC |
| 621 |
if (timespeccmp(&ifp->ifm_data.ifi_lastchange, &startspec, >)) { |
| 622 |
#else |
| 623 |
if (timercmp(&ifp->ifm_data.ifi_lastchange, &starttime, >)) { |
| 624 |
#endif |
| 625 |
entry->lastchange = (ifp->ifm_data.ifi_lastchange.tv_sec - |
| 626 |
starttime.tv_sec) * 100; |
| 627 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_LASTCHANGE; |
| 628 |
} else { |
| 629 |
entry->lastchange = 0; |
| 630 |
} |
| 631 |
|
| 632 |
if (ifp->ifm_flags & ARCH_PROMISC_FLAG) |
| 633 |
entry->promiscuous = 1; |
| 634 |
|
| 635 |
/* try to guess the speed from media type */ |
| 636 |
netsnmp_sysctl_get_if_speed(entry->name, &entry->speed, |
| 637 |
&entry->speed_high); |
| 638 |
if (entry->speed_high != 0) { |
| 639 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_HIGH_SPEED; |
| 640 |
} else { |
| 641 |
/* or resort to ifi_baudrate */ |
| 642 |
entry->speed = ifp->ifm_data.ifi_baudrate; |
| 643 |
} |
| 644 |
|
| 645 |
netsnmp_access_interface_entry_overrides(entry); |
| 646 |
|
| 647 |
CONTAINER_INSERT(container, entry); |
| 648 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 649 |
"created entry %d for %s\n", (int)entry->index, entry->name)); |
| 650 |
} /* for (each interface entry) */ |
| 651 |
|
| 652 |
/* pass 2: walk addresses */ |
| 653 |
for (cp = if_list; cp < if_list + if_list_size; cp += ifa->ifam_msglen) { |
| 654 |
|
| 655 |
ifa = (struct ifa_msghdr *) cp; |
| 656 |
|
| 657 |
if (ifa->ifam_type != RTM_NEWADDR) |
| 658 |
continue; |
| 659 |
|
| 660 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 661 |
"received 0x%x in RTM_NEWADDR for ifindex %u\n", |
| 662 |
ifa->ifam_addrs, ifa->ifam_index)); |
| 663 |
|
| 664 |
entry = netsnmp_access_interface_entry_get_by_index(container, |
| 665 |
ifa->ifam_index); |
| 666 |
if (entry == NULL) { |
| 667 |
snmp_log(LOG_ERR, "address for a nonexistent interface? index=%d", |
| 668 |
ifa->ifam_index); |
| 669 |
continue; |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* walk the list of addresses received. we do not use actual |
| 674 |
* addresses, the sole purpose of this is to set flags |
| 675 |
*/ |
| 676 |
a = (struct sockaddr *) (ifa + 1); |
| 677 |
for (amask = ifa->ifam_addrs; amask != 0; amask >>= 1) { |
| 678 |
if ((amask & 1) != 0) { |
| 679 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 680 |
"%s: a=%p, sa_len=%d, sa_family=0x%x\n", |
| 681 |
entry->name, a, a->sa_len, a->sa_family)); |
| 682 |
|
| 683 |
if (a->sa_family == AF_INET) |
| 684 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV4; |
| 685 |
else if (a->sa_family == AF_INET6) |
| 686 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV6; |
| 687 |
|
| 688 |
a = (struct sockaddr *) ( ((char *) a) + ROUNDUP(a->sa_len) ); |
| 689 |
} |
| 690 |
} |
| 691 |
DEBUGMSGTL(("access:interface:container:sysctl", |
| 692 |
"%s: flags=0x%x\n", entry->name, entry->ns_flags)); |
| 693 |
} |
| 694 |
|
| 695 |
if (if_list != NULL) |
| 696 |
free(if_list); |
| 697 |
|
| 698 |
return 0; |
| 699 |
} |
| 700 |
|
| 701 |
#ifndef NETSNMP_FEATURE_REMOVE_INTERFACE_ARCH_SET_ADMIN_STATUS |
| 702 |
int |
| 703 |
netsnmp_arch_set_admin_status(netsnmp_interface_entry * entry, |
| 704 |
int ifAdminStatus_val) |
| 705 |
{ |
| 706 |
DEBUGMSGTL(("access:interface:arch", "set_admin_status\n")); |
| 707 |
|
| 708 |
/* TODO: implement this call */ |
| 709 |
|
| 710 |
/* not implemented */ |
| 711 |
snmp_log(LOG_ERR, "netsnmp_arch_set_admin_status not (yet) implemented " |
| 712 |
"for BSD sysctl.\n"); |
| 713 |
|
| 714 |
return -4; |
| 715 |
} |
| 716 |
#endif /* NETSNMP_FEATURE_REMOVE_INTERFACE_ARCH_SET_ADMIN_STATUS */ |