| 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 "mibII/mibII_common.h" |
| 10 |
#include "if-mib/ifTable/ifTable_constants.h" |
| 11 |
|
| 12 |
netsnmp_feature_child_of(interface_arch_set_admin_status, interface_all); |
| 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 "linux 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 |
#define starttime (*(const struct timeval*)netsnmp_get_agent_starttime()) |
| 35 |
|
| 36 |
int |
| 37 |
netsnmp_openbsd_interface_get_if_speed(char *name, u_int *speed, u_int *speed_high); |
| 38 |
|
| 39 |
void |
| 40 |
netsnmp_arch_interface_init(void) |
| 41 |
{ |
| 42 |
/* |
| 43 |
* nothing to do |
| 44 |
*/ |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* find the ifIndex for an interface name |
| 49 |
* |
| 50 |
* @retval 0 : no index found |
| 51 |
* @retval >0: ifIndex for interface |
| 52 |
*/ |
| 53 |
oid |
| 54 |
netsnmp_arch_interface_index_find(const char *name) |
| 55 |
{ |
| 56 |
return if_nametoindex(name); |
| 57 |
} |
| 58 |
|
| 59 |
/* sa_len roundup macro. */ |
| 60 |
#define ROUNDUP(a) \ |
| 61 |
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 62 |
|
| 63 |
/* |
| 64 |
* |
| 65 |
* @retval 0 success |
| 66 |
* @retval -1 no container specified |
| 67 |
* @retval -2 could not get interface info |
| 68 |
* @retval -3 could not create entry (probably malloc) |
| 69 |
*/ |
| 70 |
int |
| 71 |
netsnmp_arch_interface_container_load(netsnmp_container* container, |
| 72 |
u_int load_flags) |
| 73 |
{ |
| 74 |
netsnmp_interface_entry *entry = NULL; |
| 75 |
u_char *if_list = NULL, *cp; |
| 76 |
size_t if_list_size = 0; |
| 77 |
struct if_msghdr *ifp; |
| 78 |
int sysctl_oid[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 }; |
| 79 |
struct ifa_msghdr *ifa; |
| 80 |
struct sockaddr *a; |
| 81 |
struct sockaddr_dl *adl; |
| 82 |
int amask; |
| 83 |
char *if_name; |
| 84 |
int flags; |
| 85 |
|
| 86 |
DEBUGMSGTL(("access:interface:container:arch", |
| 87 |
"load (flags %p)\n", load_flags)); |
| 88 |
|
| 89 |
if (NULL == container) { |
| 90 |
snmp_log(LOG_ERR, "no container specified/found for interface\n"); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
|
| 94 |
if (sysctl(sysctl_oid, sizeof(sysctl_oid) / sizeof(int), 0, &if_list_size, 0, 0) == -1) { |
| 95 |
snmp_log(LOG_ERR, "could not get interface info (size)\n"); |
| 96 |
return -2; |
| 97 |
} |
| 98 |
|
| 99 |
if_list = (u_char*)malloc(if_list_size); |
| 100 |
if (if_list == NULL) { |
| 101 |
snmp_log(LOG_ERR, "could not allocate memory for interface info (%u bytes)\n", if_list_size); |
| 102 |
return -3; |
| 103 |
} else { |
| 104 |
DEBUGMSGTL(("access:interface:container:arch", |
| 105 |
"allocated %u bytes for if_list\n", if_list_size)); |
| 106 |
} |
| 107 |
|
| 108 |
if (sysctl(sysctl_oid, sizeof(sysctl_oid) / sizeof(int), if_list, &if_list_size, 0, 0) == -1) { |
| 109 |
snmp_log(LOG_ERR, "could not get interface info\n"); |
| 110 |
free(if_list); |
| 111 |
return -2; |
| 112 |
} |
| 113 |
|
| 114 |
/* 1st pass: create interface entries */ |
| 115 |
for (cp = if_list; cp < if_list + if_list_size; cp += ifp->ifm_msglen) { |
| 116 |
|
| 117 |
ifp = (struct if_msghdr *) cp; |
| 118 |
if_name = NULL; |
| 119 |
flags = 0; |
| 120 |
adl = NULL; |
| 121 |
|
| 122 |
if (ifp->ifm_type != RTM_IFINFO) |
| 123 |
continue; |
| 124 |
|
| 125 |
if (ifp->ifm_addrs & RTA_IFP) { |
| 126 |
a = (struct sockaddr *) (ifp + 1); |
| 127 |
/* if_msghdr is followed by one or more sockaddrs, of which we need only RTA_IFP */ |
| 128 |
/* most of the time RTA_IFP is the first address we get, hence the shortcut */ |
| 129 |
if ((ifp->ifm_addrs & (~RTA_IFP - 1)) != 0) { |
| 130 |
/* skip all addresses up to RTA_IFP. */ |
| 131 |
for (amask = (RTA_IFP >> 1); amask != 0; amask >>= 1) { |
| 132 |
if (ifp->ifm_addrs & amask) { a = (struct sockaddr *) ( ((char *) a) + ROUNDUP(a->sa_len) ); } |
| 133 |
} |
| 134 |
} |
| 135 |
adl = (struct sockaddr_dl *) a; |
| 136 |
if_name = (char *) adl->sdl_data; |
| 137 |
if_name[adl->sdl_nlen] = '\0'; |
| 138 |
} |
| 139 |
if (!(ifp->ifm_addrs & RTA_IFP) || if_name == NULL) { |
| 140 |
snmp_log(LOG_ERR, "ifm_index %u: no interface name in message, skipping\n", ifp->ifm_index); |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
entry = netsnmp_access_interface_entry_create(if_name, ifp->ifm_index); |
| 145 |
if(NULL == entry) { |
| 146 |
free(if_list); |
| 147 |
return -3; |
| 148 |
} |
| 149 |
|
| 150 |
/* get physical address */ |
| 151 |
if (adl != NULL && adl->sdl_alen > 0) { |
| 152 |
entry->paddr_len = adl->sdl_alen; |
| 153 |
entry->paddr = (char*)malloc(entry->paddr_len); |
| 154 |
memcpy(entry->paddr, adl->sdl_data + adl->sdl_nlen, adl->sdl_alen); |
| 155 |
DEBUGMSGTL(("access:interface:container:arch", |
| 156 |
"%s: paddr_len=%d, entry->paddr=%x:%x:%x:%x:%x:%x\n", |
| 157 |
if_name, entry->paddr_len, |
| 158 |
entry->paddr[0], entry->paddr[1], entry->paddr[2], |
| 159 |
entry->paddr[3], entry->paddr[4], entry->paddr[5])); |
| 160 |
} else { |
| 161 |
entry->paddr = (char*)malloc(6); |
| 162 |
entry->paddr_len = 6; |
| 163 |
memset(entry->paddr, 0, 6); |
| 164 |
} |
| 165 |
|
| 166 |
entry->mtu = ifp->ifm_data.ifi_mtu; |
| 167 |
entry->type = ifp->ifm_data.ifi_type; |
| 168 |
|
| 169 |
|
| 170 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IF_FLAGS; |
| 171 |
entry->os_flags = ifp->ifm_flags; |
| 172 |
|
| 173 |
if (ifp->ifm_flags & IFF_UP) { |
| 174 |
entry->admin_status = IFADMINSTATUS_UP; |
| 175 |
#if defined( LINK_STATE_UP ) && defined( LINK_STATE_DOWN ) |
| 176 |
if (ifp->ifm_data.ifi_link_state == LINK_STATE_UP) { |
| 177 |
entry->oper_status = IFOPERSTATUS_UP; |
| 178 |
} else if (ifp->ifm_data.ifi_link_state == LINK_STATE_DOWN) { |
| 179 |
entry->oper_status = IFOPERSTATUS_DOWN; |
| 180 |
} else { |
| 181 |
entry->oper_status = IFOPERSTATUS_UNKNOWN; |
| 182 |
} |
| 183 |
#else |
| 184 |
entry->oper_status = IFOPERSTATUS_UP; |
| 185 |
#endif |
| 186 |
} else { |
| 187 |
entry->admin_status = IFADMINSTATUS_DOWN; |
| 188 |
/* IF-MIB specifically says that ifOperStatus should be down in this case */ |
| 189 |
entry->oper_status = IFOPERSTATUS_DOWN; |
| 190 |
} |
| 191 |
|
| 192 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_V4_REASMMAX; |
| 193 |
entry->reasm_max = IP_MAXPACKET; |
| 194 |
|
| 195 |
/* get counters */ |
| 196 |
entry->stats.ibytes.low = ifp->ifm_data.ifi_ibytes; entry->stats.ibytes.high = 0; |
| 197 |
entry->stats.iucast.low = ifp->ifm_data.ifi_ipackets; entry->stats.iucast.high = 0; |
| 198 |
entry->stats.imcast.low = ifp->ifm_data.ifi_imcasts; entry->stats.imcast.high = 0; |
| 199 |
entry->stats.ierrors = ifp->ifm_data.ifi_ierrors; |
| 200 |
entry->stats.idiscards = ifp->ifm_data.ifi_iqdrops; |
| 201 |
entry->stats.iunknown_protos = ifp->ifm_data.ifi_noproto; |
| 202 |
|
| 203 |
entry->stats.obytes.low = ifp->ifm_data.ifi_obytes; entry->stats.obytes.high = 0; |
| 204 |
entry->stats.oucast.low = ifp->ifm_data.ifi_opackets; entry->stats.oucast.high = 0; |
| 205 |
entry->stats.omcast.low = ifp->ifm_data.ifi_omcasts; entry->stats.omcast.high = 0; |
| 206 |
entry->stats.oerrors = ifp->ifm_data.ifi_oerrors; |
| 207 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_BYTES | |
| 208 |
NETSNMP_INTERFACE_FLAGS_HAS_DROPS | |
| 209 |
NETSNMP_INTERFACE_FLAGS_HAS_MCAST_PKTS; |
| 210 |
|
| 211 |
if (timercmp(&ifp->ifm_data.ifi_lastchange, &starttime, >)) { |
| 212 |
entry->lastchange = (ifp->ifm_data.ifi_lastchange.tv_sec - starttime.tv_sec) * 100; |
| 213 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_LASTCHANGE; |
| 214 |
} else { |
| 215 |
entry->lastchange = 0; |
| 216 |
} |
| 217 |
|
| 218 |
if (ifp->ifm_flags & IFF_PROMISC) entry->promiscuous = 1; |
| 219 |
|
| 220 |
/* try to guess the speed from media type */ |
| 221 |
netsnmp_openbsd_interface_get_if_speed(entry->name, &entry->speed, &entry->speed_high); |
| 222 |
if (entry->speed_high != 0) { |
| 223 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_HIGH_SPEED; |
| 224 |
} else { |
| 225 |
/* or resort to ifi_baudrate */ |
| 226 |
entry->speed = ifp->ifm_data.ifi_baudrate; |
| 227 |
} |
| 228 |
|
| 229 |
netsnmp_access_interface_entry_overrides(entry); |
| 230 |
|
| 231 |
CONTAINER_INSERT(container, entry); |
| 232 |
DEBUGMSGTL(("access:interface:container:arch", |
| 233 |
"created entry %u for %s\n", entry->index, entry->name)); |
| 234 |
} /* for (each interface entry) */ |
| 235 |
|
| 236 |
/* pass 2: walk addresses */ |
| 237 |
for (cp = if_list; cp < if_list + if_list_size; cp += ifa->ifam_msglen) { |
| 238 |
|
| 239 |
ifa = (struct ifa_msghdr *) cp; |
| 240 |
|
| 241 |
if (ifa->ifam_type != RTM_NEWADDR) |
| 242 |
continue; |
| 243 |
|
| 244 |
DEBUGMSGTL(("access:interface:container:arch", |
| 245 |
"received 0x%x in RTM_NEWADDR for ifindex %u\n", |
| 246 |
ifa->ifam_addrs, ifa->ifam_index)); |
| 247 |
|
| 248 |
entry = netsnmp_access_interface_entry_get_by_index(container, ifa->ifam_index); |
| 249 |
if (entry == NULL) { |
| 250 |
snmp_log(LOG_ERR, "address for a nonexistent interface? index=%d", ifa->ifam_index); |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
/* walk the list of addresses received. |
| 255 |
we do not use actual addresses, the sole purpose of this is to set flags */ |
| 256 |
a = (struct sockaddr *) (ifa + 1); |
| 257 |
for (amask = ifa->ifam_addrs; amask != 0; amask >>= 1) { |
| 258 |
if ((amask & 1) != 0) { |
| 259 |
DEBUGMSGTL(("access:interface:container:arch", |
| 260 |
"%s: a=%p, sa_len=%d, sa_family=0x%x\n", |
| 261 |
entry->name, a, a->sa_len, a->sa_family)); |
| 262 |
|
| 263 |
if (a->sa_family == AF_INET) |
| 264 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV4; |
| 265 |
else if (a->sa_family == AF_INET6) |
| 266 |
entry->ns_flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV6; |
| 267 |
|
| 268 |
a = (struct sockaddr *) ( ((char *) a) + ROUNDUP(a->sa_len) ); |
| 269 |
} |
| 270 |
} |
| 271 |
DEBUGMSGTL(("access:interface:container:arch", |
| 272 |
"%s: flags=0x%x\n", entry->name, entry->ns_flags)); |
| 273 |
} |
| 274 |
|
| 275 |
if (if_list != NULL) |
| 276 |
free(if_list); |
| 277 |
|
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* subroutine to translate known media typed to speed. |
| 283 |
* see /usr/include/net/if_media.h for definitions |
| 284 |
*/ |
| 285 |
|
| 286 |
void |
| 287 |
_openbsd_interface_ifmedia_to_speed(int media, u_int *speed, u_int *speed_high) |
| 288 |
{ |
| 289 |
*speed = 0; *speed_high = 0; |
| 290 |
|
| 291 |
switch (IFM_TYPE(media)) { |
| 292 |
case IFM_ETHER: |
| 293 |
switch (IFM_SUBTYPE(media)) { |
| 294 |
case IFM_10_T: |
| 295 |
case IFM_10_2: |
| 296 |
case IFM_10_5: |
| 297 |
case IFM_10_STP: |
| 298 |
case IFM_10_FL: |
| 299 |
*speed = 10000000; *speed_high = 10; break; |
| 300 |
case IFM_100_TX: |
| 301 |
case IFM_100_FX: |
| 302 |
case IFM_100_T4: |
| 303 |
case IFM_100_VG: |
| 304 |
case IFM_100_T2: |
| 305 |
*speed = 100000000; *speed_high = 100; break; |
| 306 |
case IFM_1000_LX: |
| 307 |
case IFM_1000_SX: |
| 308 |
case IFM_1000_CX: |
| 309 |
#ifdef IFM_1000_T |
| 310 |
case IFM_1000_T: |
| 311 |
#endif |
| 312 |
*speed = 1000000000; *speed_high = 1000; break; |
| 313 |
#ifdef IFM_10GBASE_SR |
| 314 |
case IFM_10GBASE_SR: |
| 315 |
case IFM_10GBASE_LR: |
| 316 |
*speed = (u_int) -1; /* 4294967295; */ *speed_high = 10000; break; |
| 317 |
#endif |
| 318 |
} |
| 319 |
break; |
| 320 |
} |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* @retval 0 speed could not be determined (error, unknown media) |
| 326 |
* @retval >0 speed, equal to *speed. |
| 327 |
* sets *speed=2^31 and returns *speed_high=speed/10^6 as required by ifSpeed/ifHighSpeed. |
| 328 |
*/ |
| 329 |
|
| 330 |
int |
| 331 |
netsnmp_openbsd_interface_get_if_speed(char *name, u_int *speed, u_int *speed_high) |
| 332 |
{ |
| 333 |
int s; |
| 334 |
struct ifmediareq ifmr; |
| 335 |
int i; |
| 336 |
uint64_t *media_list; |
| 337 |
u_int t_speed, t_speed_high; |
| 338 |
u_int m_speed, m_speed_high; |
| 339 |
|
| 340 |
*speed = 0; *speed_high = 0; |
| 341 |
|
| 342 |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
memset(&ifmr, 0, sizeof(ifmr)); |
| 347 |
strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); |
| 348 |
|
| 349 |
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0 || ifmr.ifm_count == 0) { |
| 350 |
close(s); |
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
/* |
| 355 |
* try to get speed from current media. |
| 356 |
* if unsuccessful (e.g., interface is down), get a list of capabilities, |
| 357 |
* try each and return maximum speed the interface is capable of. |
| 358 |
*/ |
| 359 |
|
| 360 |
_openbsd_interface_ifmedia_to_speed(ifmr.ifm_current, speed, speed_high); |
| 361 |
|
| 362 |
if (*speed == 0 && |
| 363 |
(media_list = (uint64_t *) malloc(ifmr.ifm_count * sizeof(uint64_t))) != NULL ) { |
| 364 |
|
| 365 |
ifmr.ifm_ulist = media_list; |
| 366 |
|
| 367 |
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == 0) { |
| 368 |
m_speed = 0; m_speed_high = 0; |
| 369 |
for (i = 0; i < ifmr.ifm_count; i++) { |
| 370 |
|
| 371 |
_openbsd_interface_ifmedia_to_speed(media_list[i], &t_speed, &t_speed_high); |
| 372 |
|
| 373 |
if (t_speed_high > m_speed_high || |
| 374 |
(t_speed_high == m_speed_high && t_speed > t_speed)) { |
| 375 |
m_speed_high = t_speed_high; m_speed = t_speed; |
| 376 |
} |
| 377 |
} |
| 378 |
*speed = m_speed; *speed_high = m_speed_high; |
| 379 |
} |
| 380 |
free(media_list); |
| 381 |
} |
| 382 |
|
| 383 |
close(s); |
| 384 |
|
| 385 |
DEBUGMSGTL(("access:interface:container:arch", |
| 386 |
"%s: speed: %u, speed_high: %u\n", |
| 387 |
name, *speed, *speed_high)); |
| 388 |
|
| 389 |
return *speed; |
| 390 |
} |
| 391 |
|
| 392 |
#ifndef NETSNMP_FEATURE_REMOVE_INTERFACE_ARCH_SET_ADMIN_STATUS |
| 393 |
int |
| 394 |
netsnmp_arch_set_admin_status(netsnmp_interface_entry * entry, |
| 395 |
int ifAdminStatus_val) |
| 396 |
{ |
| 397 |
DEBUGMSGTL(("access:interface:arch", "set_admin_status\n")); |
| 398 |
|
| 399 |
/* TODO: implement this call */ |
| 400 |
|
| 401 |
/* not implemented */ |
| 402 |
snmp_log(LOG_ERR, "netsnmp_arch_set_admin_status is not (yet) implemented for FreeBSD.\n"); |
| 403 |
|
| 404 |
return -4; |
| 405 |
} |
| 406 |
#endif /* NETSNMP_FEATURE_REMOVE_INTERFACE_ARCH_SET_ADMIN_STATUS */ |
| 407 |
|