| 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 "ip-mib/ipAddressPrefixTable/ipAddressPrefixTable_constants.h" |
| 17 |
#include "mibgroup/util_funcs.h" |
| 18 |
#include "../../if-mib/data_access/interface_private.h" |
| 19 |
|
| 20 |
#include <errno.h> |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
|
| 24 |
netsnmp_feature_require(prefix_info); |
| 25 |
netsnmp_feature_require(find_prefix_info); |
| 26 |
|
| 27 |
netsnmp_feature_child_of(ipaddress_arch_entry_copy, ipaddress_common); |
| 28 |
|
| 29 |
#ifdef NETSNMP_FEATURE_REQUIRE_IPADDRESS_ARCH_ENTRY_COPY |
| 30 |
netsnmp_feature_require(ipaddress_ioctl_entry_copy); |
| 31 |
#endif /* NETSNMP_FEATURE_REQUIRE_IPADDRESS_ARCH_ENTRY_COPY */ |
| 32 |
|
| 33 |
#include <linux/types.h> |
| 34 |
#include <asm/types.h> |
| 35 |
#if defined(HAVE_LINUX_RTNETLINK_H) |
| 36 |
#include <linux/netlink.h> |
| 37 |
#include <linux/rtnetlink.h> |
| 38 |
#ifdef RTMGRP_IPV6_PREFIX |
| 39 |
#define SUPPORT_PREFIX_FLAGS 1 |
| 40 |
#endif /* RTMGRP_IPV6_PREFIX */ |
| 41 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 42 |
|
| 43 |
#include "ipaddress.h" |
| 44 |
#include "ipaddress_ioctl.h" |
| 45 |
#include "ipaddress_private.h" |
| 46 |
|
| 47 |
int _load_v6(netsnmp_container *container, int idx_offset); |
| 48 |
|
| 49 |
#ifdef HAVE_LINUX_RTNETLINK_H |
| 50 |
int |
| 51 |
netsnmp_access_ipaddress_extra_prefix_info(int index, |
| 52 |
u_long *preferedlt, |
| 53 |
u_long *validlt, |
| 54 |
char *addr); |
| 55 |
#endif |
| 56 |
|
| 57 |
/* |
| 58 |
* initialize arch specific storage |
| 59 |
* |
| 60 |
* @retval 0: success |
| 61 |
* @retval <0: error |
| 62 |
*/ |
| 63 |
int |
| 64 |
netsnmp_arch_ipaddress_entry_init(netsnmp_ipaddress_entry *entry) |
| 65 |
{ |
| 66 |
/* |
| 67 |
* init ipv4 stuff |
| 68 |
*/ |
| 69 |
if (NULL == netsnmp_ioctl_ipaddress_entry_init(entry)) |
| 70 |
return -1; |
| 71 |
|
| 72 |
/* |
| 73 |
* init ipv6 stuff |
| 74 |
* so far, we can just share the ipv4 stuff, so nothing to do |
| 75 |
*/ |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* cleanup arch specific storage |
| 82 |
*/ |
| 83 |
void |
| 84 |
netsnmp_arch_ipaddress_entry_cleanup(netsnmp_ipaddress_entry *entry) |
| 85 |
{ |
| 86 |
/* |
| 87 |
* cleanup ipv4 stuff |
| 88 |
*/ |
| 89 |
netsnmp_ioctl_ipaddress_entry_cleanup(entry); |
| 90 |
|
| 91 |
/* |
| 92 |
* cleanup ipv6 stuff |
| 93 |
* so far, we can just share the ipv4 stuff, so nothing to do |
| 94 |
*/ |
| 95 |
} |
| 96 |
|
| 97 |
#ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_ARCH_ENTRY_COPY |
| 98 |
/* |
| 99 |
* copy arch specific storage |
| 100 |
*/ |
| 101 |
int |
| 102 |
netsnmp_arch_ipaddress_entry_copy(netsnmp_ipaddress_entry *lhs, |
| 103 |
netsnmp_ipaddress_entry *rhs) |
| 104 |
{ |
| 105 |
int rc; |
| 106 |
|
| 107 |
/* |
| 108 |
* copy ipv4 stuff |
| 109 |
*/ |
| 110 |
rc = netsnmp_ioctl_ipaddress_entry_copy(lhs, rhs); |
| 111 |
if (rc) |
| 112 |
return rc; |
| 113 |
|
| 114 |
/* |
| 115 |
* copy ipv6 stuff |
| 116 |
* so far, we can just share the ipv4 stuff, so nothing to do |
| 117 |
*/ |
| 118 |
|
| 119 |
return rc; |
| 120 |
} |
| 121 |
#endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_ARCH_ENTRY_COPY */ |
| 122 |
|
| 123 |
/* |
| 124 |
* create a new entry |
| 125 |
*/ |
| 126 |
int |
| 127 |
netsnmp_arch_ipaddress_create(netsnmp_ipaddress_entry *entry) |
| 128 |
{ |
| 129 |
if (NULL == entry) |
| 130 |
return -1; |
| 131 |
|
| 132 |
if (4 == entry->ia_address_len) { |
| 133 |
return _netsnmp_ioctl_ipaddress_set_v4(entry); |
| 134 |
} else if (16 == entry->ia_address_len) { |
| 135 |
return _netsnmp_ioctl_ipaddress_set_v6(entry); |
| 136 |
} else { |
| 137 |
DEBUGMSGT(("access:ipaddress:create", "wrong length of IP address\n")); |
| 138 |
return -2; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/* |
| 143 |
* create a new entry |
| 144 |
*/ |
| 145 |
int |
| 146 |
netsnmp_arch_ipaddress_delete(netsnmp_ipaddress_entry *entry) |
| 147 |
{ |
| 148 |
if (NULL == entry) |
| 149 |
return -1; |
| 150 |
|
| 151 |
if (4 == entry->ia_address_len) { |
| 152 |
return _netsnmp_ioctl_ipaddress_delete_v4(entry); |
| 153 |
} else if (16 == entry->ia_address_len) { |
| 154 |
return _netsnmp_ioctl_ipaddress_delete_v6(entry); |
| 155 |
} else { |
| 156 |
DEBUGMSGT(("access:ipaddress:create", "only ipv4 supported\n")); |
| 157 |
return -2; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* |
| 163 |
* @retval 0 no errors |
| 164 |
* @retval !0 errors |
| 165 |
*/ |
| 166 |
int |
| 167 |
netsnmp_arch_ipaddress_container_load(netsnmp_container *container, |
| 168 |
u_int load_flags) |
| 169 |
{ |
| 170 |
int rc = 0, idx_offset = 0; |
| 171 |
|
| 172 |
if (0 == (load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_IPV6_ONLY)) { |
| 173 |
rc = _netsnmp_ioctl_ipaddress_container_load_v4(container, idx_offset); |
| 174 |
if(rc < 0) { |
| 175 |
u_int flags = NETSNMP_ACCESS_IPADDRESS_FREE_KEEP_CONTAINER; |
| 176 |
netsnmp_access_ipaddress_container_free(container, flags); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 181 |
|
| 182 |
if (0 == (load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_IPV4_ONLY)) { |
| 183 |
if (rc < 0) |
| 184 |
rc = 0; |
| 185 |
|
| 186 |
idx_offset = rc; |
| 187 |
|
| 188 |
/* |
| 189 |
* load ipv6, ignoring errors if file not found |
| 190 |
*/ |
| 191 |
rc = _load_v6(container, idx_offset); |
| 192 |
if (-2 == rc) |
| 193 |
rc = 0; |
| 194 |
else if(rc < 0) { |
| 195 |
u_int flags = NETSNMP_ACCESS_IPADDRESS_FREE_KEEP_CONTAINER; |
| 196 |
netsnmp_access_ipaddress_container_free(container, flags); |
| 197 |
} |
| 198 |
} |
| 199 |
#endif |
| 200 |
|
| 201 |
/* |
| 202 |
* return no errors (0) if we found any interfaces |
| 203 |
*/ |
| 204 |
if(rc > 0) |
| 205 |
rc = 0; |
| 206 |
|
| 207 |
return rc; |
| 208 |
} |
| 209 |
|
| 210 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 211 |
/** |
| 212 |
*/ |
| 213 |
int |
| 214 |
_load_v6(netsnmp_container *container, int idx_offset) |
| 215 |
{ |
| 216 |
#ifndef HAVE_LINUX_RTNETLINK_H |
| 217 |
DEBUGMSGTL(("access:ipaddress:container", |
| 218 |
"cannot get ip address information" |
| 219 |
"as netlink socket is not available\n")); |
| 220 |
return -1; |
| 221 |
#else /* HAVE_LINUX_RTNETLINK_H */ |
| 222 |
FILE *in; |
| 223 |
char line[80], addr[40]; |
| 224 |
char if_name[IFNAMSIZ+1];/* +1 for '\0' because of the ugly sscanf below */ |
| 225 |
u_char *buf; |
| 226 |
int if_index, pfx_len, scope, flags, rc = 0; |
| 227 |
size_t in_len, out_len; |
| 228 |
netsnmp_ipaddress_entry *entry; |
| 229 |
_ioctl_extras *extras; |
| 230 |
struct address_flag_info addr_info; |
| 231 |
|
| 232 |
netsnmp_assert(NULL != container); |
| 233 |
|
| 234 |
#define PROCFILE "/proc/net/if_inet6" |
| 235 |
if (!(in = fopen(PROCFILE, "r"))) { |
| 236 |
/* |
| 237 |
* If opening /proc/net/if_inet6 fails, only complain if that file |
| 238 |
* does not exist. If it exists but if it is not readable, do not |
| 239 |
* log an error message because this probably is the result of IPv6 |
| 240 |
* support having been disabled intentionally. |
| 241 |
*/ |
| 242 |
struct stat st; |
| 243 |
|
| 244 |
if (stat(PROCFILE, &st) == 0) { |
| 245 |
NETSNMP_LOGONCE((LOG_ERR, |
| 246 |
"ipaddress_linux: could not open " PROCFILE)); |
| 247 |
} |
| 248 |
return -2; |
| 249 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* address index prefix_len scope status if_name |
| 253 |
*/ |
| 254 |
while (fgets(line, sizeof(line), in)) { |
| 255 |
/* |
| 256 |
* fe800000000000000200e8fffe5b5c93 05 40 20 80 eth0 |
| 257 |
* A D P S F I |
| 258 |
* A: address |
| 259 |
* D: device number |
| 260 |
* P: prefix len |
| 261 |
* S: scope (see include/net/ipv6.h, net/ipv6/addrconf.c) |
| 262 |
* F: flags (see include/linux/rtnetlink.h, net/ipv6/addrconf.c) |
| 263 |
* I: interface |
| 264 |
*/ |
| 265 |
rc = sscanf(line, "%39s %08x %08x %04x %02x %" SNMP_MACRO_VAL_TO_STR(IFNAMSIZ) "s\n", |
| 266 |
addr, &if_index, &pfx_len, &scope, &flags, if_name); |
| 267 |
if( 6 != rc ) { |
| 268 |
snmp_log(LOG_ERR, PROCFILE " data format error (%d!=6), line ==|%s|\n", |
| 269 |
rc, line); |
| 270 |
continue; |
| 271 |
} |
| 272 |
DEBUGMSGTL(("access:ipaddress:container", |
| 273 |
"addr %s, index %d, pfx %d, scope %d, flags 0x%X, name %s\n", |
| 274 |
addr, if_index, pfx_len, scope, flags, if_name)); |
| 275 |
|
| 276 |
if (!netsnmp_access_interface_include(if_name)) |
| 277 |
continue; |
| 278 |
|
| 279 |
if (netsnmp_access_interface_max_reached(if_name)) |
| 280 |
/* we may need to stop tracking ifaces if a max was set */ |
| 281 |
continue; |
| 282 |
/* |
| 283 |
*/ |
| 284 |
entry = netsnmp_access_ipaddress_entry_create(); |
| 285 |
if(NULL == entry) { |
| 286 |
rc = -3; |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
in_len = entry->ia_address_len = sizeof(entry->ia_address); |
| 291 |
netsnmp_assert(16 == in_len); |
| 292 |
out_len = 0; |
| 293 |
entry->flags = flags; |
| 294 |
buf = entry->ia_address; |
| 295 |
if(1 != netsnmp_hex_to_binary(&buf, &in_len, |
| 296 |
&out_len, 0, addr, ":")) { |
| 297 |
snmp_log(LOG_ERR,"error parsing '%s', skipping\n", addr); |
| 298 |
netsnmp_access_ipaddress_entry_free(entry); |
| 299 |
continue; |
| 300 |
} |
| 301 |
netsnmp_assert(16 == out_len); |
| 302 |
entry->ia_address_len = out_len; |
| 303 |
|
| 304 |
entry->ns_ia_index = ++idx_offset; |
| 305 |
|
| 306 |
/* |
| 307 |
* save if name |
| 308 |
*/ |
| 309 |
extras = netsnmp_ioctl_ipaddress_extras_get(entry); |
| 310 |
memcpy(extras->name, if_name, sizeof(extras->name)); |
| 311 |
extras->flags = flags; |
| 312 |
|
| 313 |
/* |
| 314 |
* yyy-rks: optimization: create a socket outside the loop and use |
| 315 |
* netsnmp_access_interface_ioctl_ifindex_get() here, since |
| 316 |
* netsnmp_access_interface_index_find will open/close a socket |
| 317 |
* every time it is called. |
| 318 |
*/ |
| 319 |
entry->if_index = netsnmp_access_interface_index_find(if_name); |
| 320 |
memset(&addr_info, 0, sizeof(struct address_flag_info)); |
| 321 |
addr_info = netsnmp_access_other_info_get(entry->if_index, AF_INET6); |
| 322 |
|
| 323 |
/* |
| 324 |
#define IPADDRESSSTATUSTC_PREFERRED 1 |
| 325 |
#define IPADDRESSSTATUSTC_DEPRECATED 2 |
| 326 |
#define IPADDRESSSTATUSTC_INVALID 3 |
| 327 |
#define IPADDRESSSTATUSTC_INACCESSIBLE 4 |
| 328 |
#define IPADDRESSSTATUSTC_UNKNOWN 5 |
| 329 |
#define IPADDRESSSTATUSTC_TENTATIVE 6 |
| 330 |
#define IPADDRESSSTATUSTC_DUPLICATE 7 |
| 331 |
*/ |
| 332 |
if((flags & IFA_F_PERMANENT) || (!flags)) |
| 333 |
entry->ia_status = IPADDRESSSTATUSTC_PREFERRED; /* ?? */ |
| 334 |
#ifdef IFA_F_TEMPORARY |
| 335 |
else if(flags & IFA_F_TEMPORARY) |
| 336 |
entry->ia_status = IPADDRESSSTATUSTC_PREFERRED; /* ?? */ |
| 337 |
#endif |
| 338 |
else if(flags & IFA_F_DEPRECATED) |
| 339 |
entry->ia_status = IPADDRESSSTATUSTC_DEPRECATED; |
| 340 |
else if(flags & IFA_F_TENTATIVE) |
| 341 |
entry->ia_status = IPADDRESSSTATUSTC_TENTATIVE; |
| 342 |
else { |
| 343 |
entry->ia_status = IPADDRESSSTATUSTC_UNKNOWN; |
| 344 |
DEBUGMSGTL(("access:ipaddress:ipv6", |
| 345 |
"unknown flags 0x%x\n", flags)); |
| 346 |
} |
| 347 |
|
| 348 |
/* |
| 349 |
* if it's not multi, it must be uni. |
| 350 |
* (an ipv6 address is never broadcast) |
| 351 |
*/ |
| 352 |
if(addr_info.anycastflg) |
| 353 |
entry->ia_type = IPADDRESSTYPE_ANYCAST; |
| 354 |
else |
| 355 |
entry->ia_type = IPADDRESSTYPE_UNICAST; |
| 356 |
|
| 357 |
|
| 358 |
entry->ia_prefix_len = pfx_len; |
| 359 |
|
| 360 |
/* |
| 361 |
* can we figure out if an address is from DHCP? |
| 362 |
* use manual until then... |
| 363 |
* |
| 364 |
*#define IPADDRESSORIGINTC_OTHER 1 |
| 365 |
*#define IPADDRESSORIGINTC_MANUAL 2 |
| 366 |
*#define IPADDRESSORIGINTC_DHCP 4 |
| 367 |
*#define IPADDRESSORIGINTC_LINKLAYER 5 |
| 368 |
*#define IPADDRESSORIGINTC_RANDOM 6 |
| 369 |
* |
| 370 |
* are 'local' address assigned by link layer?? |
| 371 |
*/ |
| 372 |
if (!flags) |
| 373 |
entry->ia_origin = IPADDRESSORIGINTC_LINKLAYER; |
| 374 |
#ifdef IFA_F_TEMPORARY |
| 375 |
else if (flags & IFA_F_TEMPORARY) |
| 376 |
entry->ia_origin = IPADDRESSORIGINTC_RANDOM; |
| 377 |
#endif |
| 378 |
else if (IN6_IS_ADDR_LINKLOCAL(entry->ia_address)) |
| 379 |
entry->ia_origin = IPADDRESSORIGINTC_LINKLAYER; |
| 380 |
else |
| 381 |
entry->ia_origin = IPADDRESSORIGINTC_MANUAL; |
| 382 |
|
| 383 |
if(entry->ia_origin == IPADDRESSORIGINTC_LINKLAYER) |
| 384 |
entry->ia_storagetype = STORAGETYPE_PERMANENT; |
| 385 |
|
| 386 |
/* xxx-rks: what can we do with scope? */ |
| 387 |
#ifdef HAVE_LINUX_RTNETLINK_H |
| 388 |
if(netsnmp_access_ipaddress_extra_prefix_info(entry->if_index, &entry->ia_prefered_lifetime |
| 389 |
,&entry->ia_valid_lifetime, addr) < 0){ |
| 390 |
DEBUGMSGTL(("access:ipaddress:container", "unable to fetch extra prefix info\n")); |
| 391 |
} |
| 392 |
#else |
| 393 |
entry->ia_prefered_lifetime = 0; |
| 394 |
entry->ia_valid_lifetime = 0; |
| 395 |
#endif |
| 396 |
#ifdef SUPPORT_PREFIX_FLAGS |
| 397 |
{ |
| 398 |
prefix_cbx prefix_val; |
| 399 |
memset(&prefix_val, 0, sizeof(prefix_cbx)); |
| 400 |
if(net_snmp_find_prefix_info(&prefix_head_list, addr, &prefix_val) < 0) { |
| 401 |
DEBUGMSGTL(("access:ipaddress:container", "unable to find info\n")); |
| 402 |
entry->ia_onlink_flag = 1; /*Set by default as true*/ |
| 403 |
entry->ia_autonomous_flag = 2; /*Set by default as false*/ |
| 404 |
|
| 405 |
} else { |
| 406 |
entry->ia_onlink_flag = prefix_val.ipAddressPrefixOnLinkFlag; |
| 407 |
entry->ia_autonomous_flag = prefix_val.ipAddressPrefixAutonomousFlag; |
| 408 |
} |
| 409 |
} |
| 410 |
#else |
| 411 |
entry->ia_onlink_flag = 1; /*Set by default as true*/ |
| 412 |
entry->ia_autonomous_flag = 2; /*Set by default as false*/ |
| 413 |
#endif |
| 414 |
|
| 415 |
/* |
| 416 |
* add entry to container |
| 417 |
*/ |
| 418 |
if (CONTAINER_INSERT(container, entry) < 0) { |
| 419 |
DEBUGMSGTL(("access:ipaddress:container","error with ipaddress_entry: insert into container failed.\n")); |
| 420 |
netsnmp_access_ipaddress_entry_free(entry); |
| 421 |
continue; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
fclose(in); |
| 426 |
|
| 427 |
if(rc<0) |
| 428 |
return rc; |
| 429 |
|
| 430 |
return idx_offset; |
| 431 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 432 |
} |
| 433 |
|
| 434 |
#ifdef HAVE_LINUX_RTNETLINK_H |
| 435 |
struct address_flag_info |
| 436 |
netsnmp_access_other_info_get(int index, int family) |
| 437 |
{ |
| 438 |
struct { |
| 439 |
struct nlmsghdr n; |
| 440 |
struct ifaddrmsg r; |
| 441 |
char buf[1024]; |
| 442 |
} req; |
| 443 |
struct address_flag_info addr; |
| 444 |
struct rtattr *rta; |
| 445 |
int status; |
| 446 |
char buf[16384]; |
| 447 |
struct nlmsghdr *nlmp; |
| 448 |
struct ifaddrmsg *rtmp; |
| 449 |
struct rtattr *rtatp; |
| 450 |
int rtattrlen; |
| 451 |
int sd; |
| 452 |
|
| 453 |
memset(&addr, 0, sizeof(struct address_flag_info)); |
| 454 |
sd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 455 |
if(sd < 0) { |
| 456 |
snmp_log_perror("ipaddress_linux: could not open netlink socket"); |
| 457 |
return addr; |
| 458 |
} |
| 459 |
|
| 460 |
memset(&req, 0, sizeof(req)); |
| 461 |
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 462 |
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT; |
| 463 |
req.n.nlmsg_type = RTM_GETADDR; |
| 464 |
req.r.ifa_family = family; |
| 465 |
rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.n.nlmsg_len)); |
| 466 |
if(family == AF_INET) |
| 467 |
rta->rta_len = RTA_LENGTH(4); |
| 468 |
else |
| 469 |
rta->rta_len = RTA_LENGTH(16); |
| 470 |
|
| 471 |
status = send(sd, &req, req.n.nlmsg_len, 0); |
| 472 |
if (status < 0) { |
| 473 |
snmp_log_perror("ipadress_linux: could not send netlink request"); |
| 474 |
goto out; |
| 475 |
} |
| 476 |
|
| 477 |
status = recv(sd, buf, sizeof(buf), 0); |
| 478 |
if (status < 0) { |
| 479 |
snmp_log_perror("ipadress_linux: could not receive netlink request"); |
| 480 |
goto out; |
| 481 |
} |
| 482 |
|
| 483 |
if(status == 0) { |
| 484 |
snmp_log (LOG_ERR, "ipadress_linux: nothing to read\n"); |
| 485 |
goto out; |
| 486 |
} |
| 487 |
|
| 488 |
for(nlmp = (struct nlmsghdr *)buf; status > sizeof(*nlmp);) { |
| 489 |
int len = nlmp->nlmsg_len; |
| 490 |
int req_len = len - sizeof(*nlmp); |
| 491 |
|
| 492 |
if (req_len < 0 || len > status) { |
| 493 |
snmp_log (LOG_ERR, "invalid netlink message\n"); |
| 494 |
goto out; |
| 495 |
} |
| 496 |
|
| 497 |
if (!NLMSG_OK(nlmp, status)) { |
| 498 |
snmp_log (LOG_ERR, "invalid NLMSG message\n"); |
| 499 |
goto out; |
| 500 |
} |
| 501 |
rtmp = (struct ifaddrmsg *)NLMSG_DATA(nlmp); |
| 502 |
rtatp = (struct rtattr *)IFA_RTA(rtmp); |
| 503 |
rtattrlen = IFA_PAYLOAD(nlmp); |
| 504 |
if(index == rtmp->ifa_index){ |
| 505 |
for (; RTA_OK(rtatp, rtattrlen); rtatp = RTA_NEXT(rtatp, rtattrlen)) { |
| 506 |
if(rtatp->rta_type == IFA_BROADCAST){ |
| 507 |
addr.addr = ((struct in_addr *)RTA_DATA(rtatp))->s_addr; |
| 508 |
addr.bcastflg = 1; |
| 509 |
} |
| 510 |
if(rtatp->rta_type == IFA_ANYCAST){ |
| 511 |
addr.addr = ((struct in_addr *)RTA_DATA(rtatp))->s_addr; |
| 512 |
addr.anycastflg = 1; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
status -= NLMSG_ALIGN(len); |
| 517 |
nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len)); |
| 518 |
} |
| 519 |
out: |
| 520 |
close(sd); |
| 521 |
return addr; |
| 522 |
} |
| 523 |
|
| 524 |
int |
| 525 |
netsnmp_access_ipaddress_extra_prefix_info(int index, u_long *preferedlt, |
| 526 |
u_long *validlt, char *addr) |
| 527 |
{ |
| 528 |
|
| 529 |
struct { |
| 530 |
struct nlmsghdr nlhdr; |
| 531 |
struct ifaddrmsg ifaceinfo; |
| 532 |
char buf[1024]; |
| 533 |
} req; |
| 534 |
|
| 535 |
struct rtattr *rta; |
| 536 |
int status; |
| 537 |
char buf[16384]; |
| 538 |
char tmpaddr[40]; |
| 539 |
struct nlmsghdr *nlmp; |
| 540 |
struct ifaddrmsg *rtmp; |
| 541 |
struct rtattr *rtatp; |
| 542 |
struct ifa_cacheinfo *cache_info; |
| 543 |
struct in6_addr *in6p; |
| 544 |
int rtattrlen; |
| 545 |
int sd; |
| 546 |
int reqaddr = 0; |
| 547 |
int ret = -1; |
| 548 |
|
| 549 |
sd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 550 |
if(sd < 0) { |
| 551 |
snmp_log(LOG_ERR, "could not open netlink socket\n"); |
| 552 |
return -1; |
| 553 |
} |
| 554 |
memset(&req, 0, sizeof(req)); |
| 555 |
req.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 556 |
req.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT; |
| 557 |
req.nlhdr.nlmsg_type = RTM_GETADDR; |
| 558 |
req.ifaceinfo.ifa_family = AF_INET6; |
| 559 |
rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.nlhdr.nlmsg_len)); |
| 560 |
rta->rta_len = RTA_LENGTH(16); /*For ipv6*/ |
| 561 |
|
| 562 |
status = send (sd, &req, req.nlhdr.nlmsg_len, 0); |
| 563 |
if (status < 0) { |
| 564 |
snmp_log(LOG_ERR, "could not send netlink request\n"); |
| 565 |
goto out; |
| 566 |
} |
| 567 |
status = recv (sd, buf, sizeof(buf), 0); |
| 568 |
if (status < 0) { |
| 569 |
snmp_log (LOG_ERR, "could not recieve netlink request\n"); |
| 570 |
goto out; |
| 571 |
} |
| 572 |
if (status == 0) { |
| 573 |
snmp_log (LOG_ERR, "nothing to read\n"); |
| 574 |
goto out; |
| 575 |
} |
| 576 |
for (nlmp = (struct nlmsghdr *)buf; status > sizeof(*nlmp); ){ |
| 577 |
|
| 578 |
int len = nlmp->nlmsg_len; |
| 579 |
int req_len = len - sizeof(*nlmp); |
| 580 |
|
| 581 |
if (req_len < 0 || len > status) { |
| 582 |
snmp_log (LOG_ERR, "invalid netlink message\n"); |
| 583 |
goto out; |
| 584 |
} |
| 585 |
|
| 586 |
if (!NLMSG_OK (nlmp, status)) { |
| 587 |
snmp_log (LOG_ERR, "invalid NLMSG message\n"); |
| 588 |
goto out; |
| 589 |
} |
| 590 |
rtmp = (struct ifaddrmsg *)NLMSG_DATA(nlmp); |
| 591 |
rtatp = (struct rtattr *)IFA_RTA(rtmp); |
| 592 |
rtattrlen = IFA_PAYLOAD(nlmp); |
| 593 |
if(index == rtmp->ifa_index) { |
| 594 |
for (; RTA_OK(rtatp, rtattrlen); rtatp = RTA_NEXT(rtatp, rtattrlen)) { |
| 595 |
if(rtatp->rta_type == IFA_ADDRESS) { |
| 596 |
in6p = (struct in6_addr *)RTA_DATA(rtatp); |
| 597 |
sprintf(tmpaddr, "%04x%04x%04x%04x%04x%04x%04x%04x", NIP6(*in6p)); |
| 598 |
if(!strcmp(tmpaddr ,addr)) |
| 599 |
reqaddr = 1; |
| 600 |
} |
| 601 |
if(rtatp->rta_type == IFA_CACHEINFO) { |
| 602 |
cache_info = (struct ifa_cacheinfo *)RTA_DATA(rtatp); |
| 603 |
if(reqaddr) { |
| 604 |
reqaddr = 0; |
| 605 |
*validlt = cache_info->ifa_valid; |
| 606 |
*preferedlt = cache_info->ifa_prefered; |
| 607 |
} |
| 608 |
|
| 609 |
} |
| 610 |
|
| 611 |
} |
| 612 |
} |
| 613 |
status -= NLMSG_ALIGN(len); |
| 614 |
nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len)); |
| 615 |
} |
| 616 |
ret = 0; |
| 617 |
|
| 618 |
out: |
| 619 |
close(sd); |
| 620 |
return ret; |
| 621 |
} |
| 622 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 623 |
#endif /* defined(NETSNMP_ENABLE_IPV6) */ |