| 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 |
|
| 11 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 12 |
#include <net-snmp/data_access/ipaddress.h> |
| 13 |
#include <net-snmp/data_access/interface.h> |
| 14 |
|
| 15 |
#include "ip-mib/ipAddressTable/ipAddressTable_constants.h" |
| 16 |
#include "if-mib/data_access/interface_ioctl.h" |
| 17 |
|
| 18 |
#include <errno.h> |
| 19 |
#include <net/if.h> |
| 20 |
#include <sys/ioctl.h> |
| 21 |
|
| 22 |
#include "ipaddress_ioctl.h" |
| 23 |
|
| 24 |
netsnmp_feature_child_of(ipadress_ioctl_entry_copy, ipaddress_common); |
| 25 |
|
| 26 |
static void _print_flags(short flags); |
| 27 |
|
| 28 |
#define LIST_TOKEN "ioctl_extras" |
| 29 |
|
| 30 |
/* |
| 31 |
* get extra structure |
| 32 |
* |
| 33 |
* @returns the extras structure from the entry |
| 34 |
*/ |
| 35 |
_ioctl_extras * |
| 36 |
netsnmp_ioctl_ipaddress_extras_get(netsnmp_ipaddress_entry *entry) |
| 37 |
{ |
| 38 |
if ((NULL == entry) || (NULL == entry->arch_data)) |
| 39 |
return NULL; |
| 40 |
|
| 41 |
return (_ioctl_extras*)netsnmp_get_list_data(entry->arch_data, LIST_TOKEN); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* initialize ioctl extras |
| 46 |
* |
| 47 |
* @returns _ioctl_extras pointer, or NULL on error |
| 48 |
*/ |
| 49 |
_ioctl_extras * |
| 50 |
netsnmp_ioctl_ipaddress_entry_init(netsnmp_ipaddress_entry *entry) |
| 51 |
{ |
| 52 |
netsnmp_data_list *node; |
| 53 |
_ioctl_extras *extras; |
| 54 |
|
| 55 |
if (NULL == entry) |
| 56 |
return NULL; |
| 57 |
|
| 58 |
extras = SNMP_MALLOC_TYPEDEF(_ioctl_extras); |
| 59 |
if (NULL == extras) |
| 60 |
return NULL; |
| 61 |
|
| 62 |
node = netsnmp_create_data_list(LIST_TOKEN, extras, free); |
| 63 |
if (NULL == node) { |
| 64 |
free(extras); |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
netsnmp_data_list_add_node( &entry->arch_data, node ); |
| 69 |
|
| 70 |
return extras; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* cleanup ioctl extras |
| 75 |
*/ |
| 76 |
void |
| 77 |
netsnmp_ioctl_ipaddress_entry_cleanup(netsnmp_ipaddress_entry *entry) |
| 78 |
{ |
| 79 |
if (NULL == entry) { |
| 80 |
netsnmp_assert(NULL != entry); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (NULL == entry->arch_data) { |
| 85 |
netsnmp_assert(NULL != entry->arch_data); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
netsnmp_remove_list_node(&entry->arch_data, LIST_TOKEN); |
| 90 |
} |
| 91 |
|
| 92 |
#ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_IOCTL_ENTRY_COPY |
| 93 |
/** |
| 94 |
* copy ioctl extras |
| 95 |
* |
| 96 |
* @retval 0: success |
| 97 |
* @retval <0: error |
| 98 |
*/ |
| 99 |
int |
| 100 |
netsnmp_ioctl_ipaddress_entry_copy(netsnmp_ipaddress_entry *lhs, |
| 101 |
netsnmp_ipaddress_entry *rhs) |
| 102 |
{ |
| 103 |
_ioctl_extras *lhs_extras, *rhs_extras; |
| 104 |
int rc = SNMP_ERR_NOERROR; |
| 105 |
|
| 106 |
if ((NULL == lhs) || (NULL == rhs)) { |
| 107 |
netsnmp_assert((NULL != lhs) && (NULL != rhs)); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
rhs_extras = netsnmp_ioctl_ipaddress_extras_get(rhs); |
| 112 |
lhs_extras = netsnmp_ioctl_ipaddress_extras_get(lhs); |
| 113 |
if (NULL == rhs_extras) { |
| 114 |
if (NULL != lhs_extras) |
| 115 |
netsnmp_ioctl_ipaddress_entry_cleanup(lhs); |
| 116 |
} |
| 117 |
else { |
| 118 |
if (NULL == lhs_extras) |
| 119 |
lhs_extras = netsnmp_ioctl_ipaddress_entry_init(lhs); |
| 120 |
|
| 121 |
if (NULL != lhs_extras) |
| 122 |
memcpy(lhs_extras, rhs_extras, sizeof(_ioctl_extras)); |
| 123 |
else |
| 124 |
rc = -1; |
| 125 |
} |
| 126 |
|
| 127 |
return rc; |
| 128 |
} |
| 129 |
#endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_IOCTL_ENTRY_COPY */ |
| 130 |
|
| 131 |
/** |
| 132 |
* load ipv4 address via ioctl |
| 133 |
*/ |
| 134 |
int |
| 135 |
_netsnmp_ioctl_ipaddress_container_load_v4(netsnmp_container *container, |
| 136 |
int idx_offset) |
| 137 |
{ |
| 138 |
int i, sd, rc = 0, interfaces = 0; |
| 139 |
struct ifconf ifc; |
| 140 |
struct ifreq *ifrp; |
| 141 |
struct sockaddr save_addr; |
| 142 |
struct sockaddr_in * si; |
| 143 |
struct address_flag_info addr_info; |
| 144 |
in_addr_t ipval; |
| 145 |
_ioctl_extras *extras; |
| 146 |
|
| 147 |
if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 148 |
snmp_log_perror("_netsnmp_ioctl_ipaddress_container_load_v4: could not create socket"); |
| 149 |
return -1; |
| 150 |
} |
| 151 |
|
| 152 |
interfaces = |
| 153 |
netsnmp_access_ipaddress_ioctl_get_interface_count(sd, &ifc); |
| 154 |
if(interfaces < 0) { |
| 155 |
close(sd); |
| 156 |
return -2; |
| 157 |
} |
| 158 |
netsnmp_assert(NULL != ifc.ifc_buf); |
| 159 |
DEBUGMSGTL(("access:ipaddress:container", "processing %d interfaces\n", interfaces)); |
| 160 |
|
| 161 |
ifrp = ifc.ifc_req; |
| 162 |
for(i=0; i < interfaces; ++i, ++ifrp) { |
| 163 |
netsnmp_ipaddress_entry *entry, *bcastentry = NULL; |
| 164 |
|
| 165 |
DEBUGMSGTL(("access:ipaddress:container", |
| 166 |
" interface %d, %s\n", i, ifrp->ifr_name)); |
| 167 |
|
| 168 |
if (AF_INET != ifrp->ifr_addr.sa_family) { |
| 169 |
DEBUGMSGTL(("access:ipaddress:container", |
| 170 |
" skipping %s; non AF_INET family %d\n", |
| 171 |
ifrp->ifr_name, ifrp->ifr_addr.sa_family)); |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
if (!netsnmp_access_interface_include(ifrp->ifr_name)) |
| 176 |
continue; |
| 177 |
|
| 178 |
if (netsnmp_access_interface_max_reached(ifrp->ifr_name)) |
| 179 |
/* we may need to stop tracking ifaces if a max was set */ |
| 180 |
continue; |
| 181 |
/* |
| 182 |
*/ |
| 183 |
entry = netsnmp_access_ipaddress_entry_create(); |
| 184 |
if(NULL == entry) { |
| 185 |
rc = -3; |
| 186 |
break; |
| 187 |
} |
| 188 |
entry->ns_ia_index = ++idx_offset; |
| 189 |
|
| 190 |
/* |
| 191 |
* save if name |
| 192 |
*/ |
| 193 |
extras = netsnmp_ioctl_ipaddress_extras_get(entry); |
| 194 |
memcpy(extras->name, ifrp->ifr_name, sizeof(extras->name)); |
| 195 |
|
| 196 |
/* |
| 197 |
* each time we make an ioctl, we need to specify the address, but |
| 198 |
* it will be overwritten in the call. so we save address here. |
| 199 |
*/ |
| 200 |
save_addr = ifrp->ifr_addr; |
| 201 |
|
| 202 |
/* |
| 203 |
* set indexes |
| 204 |
*/ |
| 205 |
si = (struct sockaddr_in *) &ifrp->ifr_addr; |
| 206 |
entry->ia_address_len = sizeof(si->sin_addr.s_addr); |
| 207 |
ipval = si->sin_addr.s_addr; |
| 208 |
memcpy(entry->ia_address, &si->sin_addr.s_addr, |
| 209 |
entry->ia_address_len); |
| 210 |
|
| 211 |
/* |
| 212 |
* get ifindex |
| 213 |
*/ |
| 214 |
{ |
| 215 |
/* |
| 216 |
* I think that Linux and Solaris both use ':' in the |
| 217 |
* interface name for aliases. When a new arch is added |
| 218 |
* that uses some other indicator, a new function, maybe |
| 219 |
* netsnmp_access_ipaddress_entry_name_alias_check(), will |
| 220 |
* need to be written. |
| 221 |
*/ |
| 222 |
char *ptr = strchr(ifrp->ifr_name, ':'); |
| 223 |
if (NULL != ptr) { |
| 224 |
entry->flags |= NETSNMP_ACCESS_IPADDRESS_ISALIAS; |
| 225 |
*ptr = 0; |
| 226 |
} |
| 227 |
} |
| 228 |
entry->if_index = |
| 229 |
netsnmp_access_interface_ioctl_ifindex_get(sd, ifrp->ifr_name); |
| 230 |
if (0 == entry->if_index) { |
| 231 |
snmp_log(LOG_ERR,"no ifindex found for interface\n"); |
| 232 |
netsnmp_access_ipaddress_entry_free(entry); |
| 233 |
continue; |
| 234 |
} |
| 235 |
|
| 236 |
/* restore the interface name if we modifed it due to unaliasing |
| 237 |
* above |
| 238 |
*/ |
| 239 |
if (entry->flags & NETSNMP_ACCESS_IPADDRESS_ISALIAS) { |
| 240 |
memcpy(ifrp->ifr_name, extras->name, sizeof(extras->name)); |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* get broadcast |
| 245 |
*/ |
| 246 |
memset(&addr_info, 0, sizeof(struct address_flag_info)); |
| 247 |
#if defined (NETSNMP_ENABLE_IPV6) && defined(HAVE_LINUX_RTNETLINK_H) |
| 248 |
addr_info = netsnmp_access_other_info_get(entry->if_index, AF_INET); |
| 249 |
if(addr_info.bcastflg) { |
| 250 |
bcastentry = netsnmp_access_ipaddress_entry_create(); |
| 251 |
if(NULL == bcastentry) { |
| 252 |
rc = -3; |
| 253 |
break; |
| 254 |
} |
| 255 |
bcastentry->if_index = entry->if_index; |
| 256 |
bcastentry->ns_ia_index = ++idx_offset; |
| 257 |
bcastentry->ia_address_len = sizeof(addr_info.addr); |
| 258 |
memcpy(bcastentry->ia_address, &addr_info.addr, |
| 259 |
bcastentry->ia_address_len); |
| 260 |
} |
| 261 |
#endif |
| 262 |
|
| 263 |
/* |
| 264 |
* get netmask |
| 265 |
*/ |
| 266 |
ifrp->ifr_addr = save_addr; |
| 267 |
if (ioctl(sd, SIOCGIFNETMASK, ifrp) < 0) { |
| 268 |
snmp_log(LOG_ERR, |
| 269 |
"error getting netmask for interface %d\n", i); |
| 270 |
netsnmp_access_ipaddress_entry_free(entry); |
| 271 |
continue; |
| 272 |
} |
| 273 |
netsnmp_assert(AF_INET == ifrp->ifr_addr.sa_family); |
| 274 |
si = (struct sockaddr_in *) &ifrp->ifr_addr; |
| 275 |
entry->ia_prefix_len = |
| 276 |
netsnmp_ipaddress_ipv4_prefix_len(si->sin_addr.s_addr); |
| 277 |
if(bcastentry) |
| 278 |
bcastentry->ia_prefix_len = entry->ia_prefix_len; |
| 279 |
|
| 280 |
|
| 281 |
/* |
| 282 |
* get flags |
| 283 |
*/ |
| 284 |
ifrp->ifr_addr = save_addr; |
| 285 |
if (ioctl(sd, SIOCGIFFLAGS, ifrp) < 0) { |
| 286 |
snmp_log(LOG_ERR, |
| 287 |
"error getting if_flags for interface %d\n", i); |
| 288 |
netsnmp_access_ipaddress_entry_free(bcastentry); |
| 289 |
netsnmp_access_ipaddress_entry_free(entry); |
| 290 |
continue; |
| 291 |
} |
| 292 |
extras->flags = ifrp->ifr_flags; |
| 293 |
|
| 294 |
if(bcastentry) |
| 295 |
bcastentry->ia_type = IPADDRESSTYPE_BROADCAST; |
| 296 |
if(addr_info.anycastflg) |
| 297 |
entry->ia_type = IPADDRESSTYPE_ANYCAST; |
| 298 |
else |
| 299 |
entry->ia_type = IPADDRESSTYPE_UNICAST; |
| 300 |
|
| 301 |
/** entry->ia_prefix_oid ? */ |
| 302 |
|
| 303 |
/* |
| 304 |
* per the MIB: |
| 305 |
* In the absence of other information, an IPv4 address is |
| 306 |
* always preferred(1). |
| 307 |
*/ |
| 308 |
entry->ia_status = IPADDRESSSTATUSTC_PREFERRED; |
| 309 |
if(bcastentry) |
| 310 |
bcastentry->ia_status = IPADDRESSSTATUSTC_PREFERRED; |
| 311 |
|
| 312 |
/* |
| 313 |
* can we figure out if an address is from DHCP? |
| 314 |
* use manual until then... |
| 315 |
*/ |
| 316 |
if(IS_APIPA(ipval)) { |
| 317 |
entry->ia_origin = IPADDRESSORIGINTC_RANDOM; |
| 318 |
if(bcastentry) |
| 319 |
bcastentry->ia_origin = IPADDRESSORIGINTC_RANDOM; |
| 320 |
} |
| 321 |
else { |
| 322 |
entry->ia_origin = IPADDRESSORIGINTC_MANUAL; |
| 323 |
if(bcastentry) |
| 324 |
bcastentry->ia_origin = IPADDRESSORIGINTC_MANUAL; |
| 325 |
} |
| 326 |
|
| 327 |
DEBUGIF("access:ipaddress:container") { |
| 328 |
DEBUGMSGT_NC(("access:ipaddress:container", |
| 329 |
" if %d: addr len %d, index 0x%" NETSNMP_PRIo "x\n", |
| 330 |
i, entry->ia_address_len, entry->if_index)); |
| 331 |
if (4 == entry->ia_address_len) |
| 332 |
DEBUGMSGT_NC(("access:ipaddress:container", |
| 333 |
" address %d.%d.%d.%d\n", |
| 334 |
entry->ia_address[0], entry->ia_address[1], |
| 335 |
entry->ia_address[2], entry->ia_address[3])); |
| 336 |
DEBUGMSGT_NC(("access:ipaddress:container", "flags 0x%x\n", |
| 337 |
extras->flags)); |
| 338 |
_print_flags(extras->flags); |
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
/* |
| 343 |
* add entry to container |
| 344 |
*/ |
| 345 |
if(bcastentry){ |
| 346 |
if (CONTAINER_INSERT(container, bcastentry) < 0) { |
| 347 |
DEBUGMSGTL(("access:ipaddress:container","error with ipaddress_entry: insert broadcast entry into container failed.\n")); |
| 348 |
netsnmp_access_ipaddress_entry_free(bcastentry); |
| 349 |
netsnmp_access_ipaddress_entry_free(entry); |
| 350 |
continue; |
| 351 |
} |
| 352 |
bcastentry = NULL; |
| 353 |
} |
| 354 |
|
| 355 |
if (CONTAINER_INSERT(container, entry) < 0) { |
| 356 |
DEBUGMSGTL(("access:ipaddress:container","error with ipaddress_entry: insert into container failed.\n")); |
| 357 |
NETSNMP_LOGONCE((LOG_ERR, "Duplicate IPv4 address detected, some interfaces may not be visible in IP-MIB\n")); |
| 358 |
netsnmp_access_ipaddress_entry_free(entry); |
| 359 |
continue; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/* |
| 364 |
* clean up |
| 365 |
*/ |
| 366 |
free(ifc.ifc_buf); |
| 367 |
close(sd); |
| 368 |
|
| 369 |
/* |
| 370 |
* return number of interfaces seen |
| 371 |
*/ |
| 372 |
if(rc < 0) |
| 373 |
return rc; |
| 374 |
|
| 375 |
return idx_offset; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* find unused alias number |
| 380 |
*/ |
| 381 |
static int |
| 382 |
_next_alias(const char *if_name) |
| 383 |
{ |
| 384 |
int i, j, k, sd, interfaces = 0, len; |
| 385 |
struct ifconf ifc; |
| 386 |
struct ifreq *ifrp; |
| 387 |
char *alias; |
| 388 |
int *alias_list; |
| 389 |
|
| 390 |
if (NULL == if_name) |
| 391 |
return -1; |
| 392 |
len = strlen(if_name); |
| 393 |
|
| 394 |
if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 395 |
snmp_log_perror("_next_alias: could not create socket"); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
|
| 399 |
interfaces = |
| 400 |
netsnmp_access_ipaddress_ioctl_get_interface_count(sd, &ifc); |
| 401 |
if(interfaces < 0) { |
| 402 |
close(sd); |
| 403 |
return -2; |
| 404 |
} |
| 405 |
netsnmp_assert(NULL != ifc.ifc_buf); |
| 406 |
DEBUGMSGTL(("access:ipaddress:container", "processing %d interfaces\n", interfaces)); |
| 407 |
|
| 408 |
alias_list = (int*)malloc(interfaces * sizeof(int)); |
| 409 |
if (NULL == alias_list) { |
| 410 |
close(sd); |
| 411 |
return -2; |
| 412 |
} |
| 413 |
|
| 414 |
ifrp = ifc.ifc_req; |
| 415 |
for(i=0,j=0; i < interfaces; ++i, ++ifrp) { |
| 416 |
|
| 417 |
if (strncmp(ifrp->ifr_name, if_name, len) != 0) |
| 418 |
continue; |
| 419 |
|
| 420 |
DEBUGMSGTL(("access:ipaddress:container", |
| 421 |
" interface %d, %s\n", i, ifrp->ifr_name)); |
| 422 |
|
| 423 |
alias = strchr(ifrp->ifr_name, ':'); |
| 424 |
if (NULL == alias) |
| 425 |
continue; |
| 426 |
|
| 427 |
++alias; /* skip ':' */ |
| 428 |
alias_list[j++] = atoi(alias); |
| 429 |
} |
| 430 |
|
| 431 |
/* |
| 432 |
* clean up |
| 433 |
*/ |
| 434 |
free(ifc.ifc_buf); |
| 435 |
close(sd); |
| 436 |
|
| 437 |
/* |
| 438 |
* return first unused alias |
| 439 |
*/ |
| 440 |
for(i=1; i<=interfaces; ++i) { |
| 441 |
for(k=0;k<j;++k) |
| 442 |
if (alias_list[k] == i) |
| 443 |
break; |
| 444 |
if (k == j) { |
| 445 |
free(alias_list); |
| 446 |
return i; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
free(alias_list); |
| 451 |
return interfaces + 1; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
/** |
| 456 |
* |
| 457 |
* @retval 0 : no error |
| 458 |
* @retval -1 : bad parameter |
| 459 |
* @retval -2 : couldn't create socket |
| 460 |
* @retval -3 : ioctl failed |
| 461 |
*/ |
| 462 |
int |
| 463 |
_netsnmp_ioctl_ipaddress_set_v4(netsnmp_ipaddress_entry * entry) |
| 464 |
{ |
| 465 |
struct ifreq ifrq; |
| 466 |
struct sockaddr_in *sin; |
| 467 |
int rc, fd = -1; |
| 468 |
_ioctl_extras *extras; |
| 469 |
|
| 470 |
if (NULL == entry) |
| 471 |
return -1; |
| 472 |
|
| 473 |
netsnmp_assert(4 == entry->ia_address_len); |
| 474 |
|
| 475 |
extras = netsnmp_ioctl_ipaddress_extras_get(entry); |
| 476 |
if (NULL == extras) |
| 477 |
return -1; |
| 478 |
|
| 479 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 480 |
if(fd < 0) { |
| 481 |
snmp_log_perror("_netsnmp_ioctl_ipaddress_set_v4: couldn't create socket"); |
| 482 |
return -2; |
| 483 |
} |
| 484 |
memset(&ifrq, 0, sizeof(ifrq)); |
| 485 |
|
| 486 |
if ('\0' == extras->name[0]) { |
| 487 |
const char *name = netsnmp_access_interface_name_find(entry->if_index); |
| 488 |
int alias_idx; |
| 489 |
|
| 490 |
if (NULL == name) { |
| 491 |
DEBUGMSGT(("access:ipaddress:set", |
| 492 |
"cant find name for index %" NETSNMP_PRIo "d\n", |
| 493 |
entry->if_index)); |
| 494 |
close(fd); |
| 495 |
return -1; |
| 496 |
} |
| 497 |
|
| 498 |
/* |
| 499 |
* search for unused alias |
| 500 |
*/ |
| 501 |
alias_idx = _next_alias(name); |
| 502 |
snprintf(ifrq.ifr_name, sizeof(ifrq.ifr_name), "%s:%d", |
| 503 |
name, alias_idx); |
| 504 |
} |
| 505 |
else |
| 506 |
strlcpy(ifrq.ifr_name, (char *) extras->name, sizeof(ifrq.ifr_name)); |
| 507 |
|
| 508 |
sin = (struct sockaddr_in*)&ifrq.ifr_addr; |
| 509 |
sin->sin_family = AF_INET; |
| 510 |
memcpy(&sin->sin_addr.s_addr, entry->ia_address, |
| 511 |
entry->ia_address_len); |
| 512 |
|
| 513 |
rc = ioctl(fd, SIOCSIFADDR, &ifrq); |
| 514 |
close(fd); |
| 515 |
if(rc < 0) { |
| 516 |
snmp_log(LOG_ERR,"error setting address\n"); |
| 517 |
return -3; |
| 518 |
} |
| 519 |
|
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* |
| 525 |
* @retval 0 : no error |
| 526 |
* @retval -1 : bad parameter |
| 527 |
* @retval -2 : couldn't create socket |
| 528 |
* @retval -3 : ioctl failed |
| 529 |
*/ |
| 530 |
int |
| 531 |
_netsnmp_ioctl_ipaddress_delete_v4(netsnmp_ipaddress_entry * entry) |
| 532 |
{ |
| 533 |
struct ifreq ifrq; |
| 534 |
int rc, fd = -1; |
| 535 |
_ioctl_extras *extras; |
| 536 |
|
| 537 |
if (NULL == entry) |
| 538 |
return -1; |
| 539 |
|
| 540 |
netsnmp_assert(4 == entry->ia_address_len); |
| 541 |
|
| 542 |
extras = netsnmp_ioctl_ipaddress_extras_get(entry); |
| 543 |
if (NULL == extras) |
| 544 |
return -1; |
| 545 |
|
| 546 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 547 |
if(fd < 0) { |
| 548 |
snmp_log_perror("_netsnmp_ioctl_ipaddress_delete_v4: couldn't create socket"); |
| 549 |
return -2; |
| 550 |
} |
| 551 |
|
| 552 |
memset(&ifrq, 0, sizeof(ifrq)); |
| 553 |
|
| 554 |
strlcpy(ifrq.ifr_name, (char *) extras->name, sizeof(ifrq.ifr_name)); |
| 555 |
|
| 556 |
ifrq.ifr_flags = 0; |
| 557 |
|
| 558 |
rc = ioctl(fd, SIOCSIFFLAGS, &ifrq); |
| 559 |
close(fd); |
| 560 |
if(rc < 0) { |
| 561 |
snmp_log(LOG_ERR,"error deleting address\n"); |
| 562 |
return -3; |
| 563 |
} |
| 564 |
|
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
/** |
| 570 |
* Add/remove IPv6 address using ioctl. |
| 571 |
* @retval 0 : no error |
| 572 |
* @retval -1 : bad parameter |
| 573 |
* @retval -2 : couldn't create socket |
| 574 |
* @retval -3 : ioctl failed |
| 575 |
*/ |
| 576 |
int |
| 577 |
_netsnmp_ioctl_ipaddress_v6(netsnmp_ipaddress_entry * entry, int operation) |
| 578 |
{ |
| 579 |
#ifdef linux |
| 580 |
/* |
| 581 |
* From linux/ipv6.h. It cannot be included because it collides |
| 582 |
* with netinet/in.h |
| 583 |
*/ |
| 584 |
struct in6_ifreq { |
| 585 |
struct in6_addr ifr6_addr; |
| 586 |
uint32_t ifr6_prefixlen; |
| 587 |
int ifr6_ifindex; |
| 588 |
}; |
| 589 |
|
| 590 |
struct in6_ifreq ifrq; |
| 591 |
int rc, fd = -1; |
| 592 |
|
| 593 |
DEBUGMSGT(("access:ipaddress:set", "_netsnmp_ioctl_ipaddress_set_v6 started\n")); |
| 594 |
|
| 595 |
if (NULL == entry) |
| 596 |
return -1; |
| 597 |
|
| 598 |
netsnmp_assert(16 == entry->ia_address_len); |
| 599 |
|
| 600 |
fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 601 |
if(fd < 0) { |
| 602 |
snmp_log_perror("_netsnmp_ioctl_ipaddress_v6: couldn't create socket"); |
| 603 |
return -2; |
| 604 |
} |
| 605 |
memset(&ifrq, 0, sizeof(ifrq)); |
| 606 |
ifrq.ifr6_ifindex = entry->if_index; |
| 607 |
ifrq.ifr6_prefixlen = 64; |
| 608 |
|
| 609 |
memcpy(&ifrq.ifr6_addr, entry->ia_address, |
| 610 |
entry->ia_address_len); |
| 611 |
|
| 612 |
rc = ioctl(fd, operation, &ifrq); |
| 613 |
close(fd); |
| 614 |
if(rc < 0) { |
| 615 |
snmp_log(LOG_ERR,"error setting address: %s(%d)\n", strerror(errno), errno); |
| 616 |
return -3; |
| 617 |
} |
| 618 |
DEBUGMSGT(("access:ipaddress:set", "_netsnmp_ioctl_ipaddress_set_v6 finished\n")); |
| 619 |
return 0; |
| 620 |
#else |
| 621 |
/* we don't support ipv6 on this platform (yet) */ |
| 622 |
return -3; |
| 623 |
#endif |
| 624 |
|
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* |
| 629 |
* @retval 0 : no error |
| 630 |
* @retval -1 : bad parameter |
| 631 |
* @retval -2 : couldn't create socket |
| 632 |
* @retval -3 : ioctl failed |
| 633 |
*/ |
| 634 |
int |
| 635 |
_netsnmp_ioctl_ipaddress_set_v6(netsnmp_ipaddress_entry * entry) |
| 636 |
{ |
| 637 |
return _netsnmp_ioctl_ipaddress_v6(entry, SIOCSIFADDR); |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* |
| 642 |
* @retval 0 : no error |
| 643 |
* @retval -1 : bad parameter |
| 644 |
* @retval -2 : couldn't create socket |
| 645 |
* @retval -3 : ioctl failed |
| 646 |
*/ |
| 647 |
int |
| 648 |
_netsnmp_ioctl_ipaddress_delete_v6(netsnmp_ipaddress_entry * entry) |
| 649 |
{ |
| 650 |
return _netsnmp_ioctl_ipaddress_v6(entry, SIOCDIFADDR); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* get the interface count and populate the ifc_buf |
| 655 |
* |
| 656 |
* Note: the caller assumes responsbility for the ifc->ifc_buf |
| 657 |
* memory, and should free() it when done. |
| 658 |
* |
| 659 |
* @retval -1 : malloc error |
| 660 |
*/ |
| 661 |
int |
| 662 |
netsnmp_access_ipaddress_ioctl_get_interface_count(int sd, struct ifconf * ifc) |
| 663 |
{ |
| 664 |
int lastlen = 0, i; |
| 665 |
struct ifconf ifc_tmp; |
| 666 |
|
| 667 |
if (NULL == ifc) { |
| 668 |
memset(&ifc_tmp, 0x0, sizeof(ifc_tmp)); |
| 669 |
ifc = &ifc_tmp; |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF |
| 674 |
* on some platforms; see W. R. Stevens, ``Unix Network Programming |
| 675 |
* Volume I'', p.435. |
| 676 |
*/ |
| 677 |
|
| 678 |
for (i = 8;; i *= 2) { |
| 679 |
ifc->ifc_buf = (caddr_t)calloc(i, sizeof(struct ifreq)); |
| 680 |
if (NULL == ifc->ifc_buf) { |
| 681 |
snmp_log(LOG_ERR, "could not allocate memory for %d interfaces\n", |
| 682 |
i); |
| 683 |
return -1; |
| 684 |
} |
| 685 |
ifc->ifc_len = i * sizeof(struct ifreq); |
| 686 |
|
| 687 |
if (ioctl(sd, SIOCGIFCONF, (char *) ifc) < 0) { |
| 688 |
if (errno != EINVAL || lastlen != 0) { |
| 689 |
/* |
| 690 |
* Something has gone genuinely wrong. |
| 691 |
*/ |
| 692 |
snmp_log(LOG_ERR, "bad rc from ioctl, errno %d", errno); |
| 693 |
SNMP_FREE(ifc->ifc_buf); |
| 694 |
return -1; |
| 695 |
} |
| 696 |
/* |
| 697 |
* Otherwise, it could just be that the buffer is too small. |
| 698 |
*/ |
| 699 |
} else { |
| 700 |
if (ifc->ifc_len == lastlen) { |
| 701 |
/* |
| 702 |
* The length is the same as the last time; we're done. |
| 703 |
*/ |
| 704 |
break; |
| 705 |
} |
| 706 |
lastlen = ifc->ifc_len; |
| 707 |
} |
| 708 |
free(ifc->ifc_buf); /* no SNMP_FREE, getting ready to reassign */ |
| 709 |
} |
| 710 |
|
| 711 |
if (ifc == &ifc_tmp) |
| 712 |
free(ifc_tmp.ifc_buf); |
| 713 |
|
| 714 |
return ifc->ifc_len / sizeof(struct ifreq); |
| 715 |
} |
| 716 |
|
| 717 |
/** |
| 718 |
*/ |
| 719 |
static void |
| 720 |
_print_flags(short flags) |
| 721 |
{ |
| 722 |
/** Standard interface flags. */ |
| 723 |
struct { |
| 724 |
short flag; |
| 725 |
const char *name; |
| 726 |
} map[] = { |
| 727 |
{ IFF_UP, "interface is up"}, |
| 728 |
{ IFF_BROADCAST, "broadcast address valid"}, |
| 729 |
{ IFF_DEBUG, "turn on debugging"}, |
| 730 |
{ IFF_LOOPBACK, "is a loopback net"}, |
| 731 |
{ IFF_POINTOPOINT, "interface is has p-p link"}, |
| 732 |
{ IFF_NOTRAILERS, "avoid use of trailers"}, |
| 733 |
{ IFF_RUNNING, "resources allocated"}, |
| 734 |
{ IFF_NOARP, "no ARP protocol"}, |
| 735 |
{ IFF_PROMISC, "receive all packets"}, |
| 736 |
{ IFF_ALLMULTI, "receive all multicast packets"}, |
| 737 |
{ IFF_MASTER, "master of a load balancer"}, |
| 738 |
{ IFF_SLAVE, "slave of a load balancer"}, |
| 739 |
{ IFF_MULTICAST, "Supports multicast"}, |
| 740 |
{ IFF_PORTSEL, "can set media type"}, |
| 741 |
{ IFF_AUTOMEDIA, "auto media select active"}, |
| 742 |
}; |
| 743 |
short unknown = flags; |
| 744 |
size_t i; |
| 745 |
|
| 746 |
for(i = 0; i < sizeof(map)/sizeof(map[0]); ++i) |
| 747 |
if(flags & map[i].flag) { |
| 748 |
DEBUGMSGT_NC(("access:ipaddress:container"," %s\n", map[i].name)); |
| 749 |
unknown &= ~map[i].flag; |
| 750 |
} |
| 751 |
|
| 752 |
if(unknown) |
| 753 |
DEBUGMSGT_NC(("access:ipaddress:container"," unknown 0x%x\n", unknown)); |
| 754 |
} |