| 1 |
/* |
| 2 |
* Interface MIB architecture support |
| 3 |
* |
| 4 |
* $Id: ipv6scopezone_linux.c 14170 2007-04-29 02:22:12Z varun_c $ |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
|
| 9 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 10 |
#include <net-snmp/data_access/scopezone.h> |
| 11 |
#include "ipv6scopezone_private.h" |
| 12 |
|
| 13 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 14 |
static int _scopezone_v6(netsnmp_container* container, int idx_offset); |
| 15 |
#endif |
| 16 |
|
| 17 |
/* |
| 18 |
* |
| 19 |
* @retval 0 success |
| 20 |
* @retval -1 no container specified |
| 21 |
* @retval -2 could not open file |
| 22 |
* @retval -3 could not create entry (probably malloc) |
| 23 |
* @retval -4 file format error |
| 24 |
*/ |
| 25 |
int |
| 26 |
netsnmp_access_scopezone_container_arch_load(netsnmp_container* container, |
| 27 |
u_int load_flags) |
| 28 |
{ |
| 29 |
int rc1 = 0; |
| 30 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 31 |
int idx_offset = 0; |
| 32 |
|
| 33 |
if (NULL == container) { |
| 34 |
snmp_log(LOG_ERR, "no container specified/found for access_scopezone_\n"); |
| 35 |
return -1; |
| 36 |
} |
| 37 |
|
| 38 |
rc1 = _scopezone_v6(container, idx_offset); |
| 39 |
#endif |
| 40 |
if(rc1 > 0) |
| 41 |
rc1 = 0; |
| 42 |
return rc1; |
| 43 |
} |
| 44 |
|
| 45 |
#if defined (NETSNMP_ENABLE_IPV6) |
| 46 |
|
| 47 |
/* scope identifiers, from kernel - include/net/ipv6.h */ |
| 48 |
#define IPV6_ADDR_LOOPBACK 0x0010U |
| 49 |
#define IPV6_ADDR_LINKLOCAL 0x0020U |
| 50 |
#define IPV6_ADDR_SITELOCAL 0x0040U |
| 51 |
|
| 52 |
static int |
| 53 |
_scopezone_v6(netsnmp_container* container, int idx_offset) |
| 54 |
{ |
| 55 |
|
| 56 |
/* |
| 57 |
* On Linux, we support only link-local scope zones. |
| 58 |
* Each interface, which has link-local address, gets unique scope |
| 59 |
* zone index. |
| 60 |
*/ |
| 61 |
FILE *in; |
| 62 |
char line[80], addr[40]; |
| 63 |
int if_index, pfx_len, scope, flags, rc = 0; |
| 64 |
int last_if_index = -1; |
| 65 |
netsnmp_v6scopezone_entry *entry; |
| 66 |
|
| 67 |
netsnmp_assert(NULL != container); |
| 68 |
|
| 69 |
#define PROCFILE "/proc/net/if_inet6" |
| 70 |
if (!(in = fopen(PROCFILE, "r"))) { |
| 71 |
DEBUGMSGTL(("access:scopezone:container","could not open " PROCFILE "\n")); |
| 72 |
return -2; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* address index prefix_len scope status if_name |
| 77 |
*/ |
| 78 |
while (fgets(line, sizeof(line), in)) { |
| 79 |
/* |
| 80 |
* fe800000000000000200e8fffe5b5c93 05 40 20 80 eth0 |
| 81 |
* A D P S F I |
| 82 |
* A: address |
| 83 |
* D: device number |
| 84 |
* P: prefix len |
| 85 |
* S: scope (see include/net/ipv6.h, net/ipv6/addrconf.c) |
| 86 |
* F: flags (see include/linux/rtnetlink.h, net/ipv6/addrconf.c) |
| 87 |
* I: interface |
| 88 |
*/ |
| 89 |
rc = sscanf(line, "%39s %04x %02x %02x %02x\n", |
| 90 |
addr, &if_index, &pfx_len, &scope, &flags); |
| 91 |
if( 5 != rc ) { |
| 92 |
snmp_log(LOG_ERR, PROCFILE " data format error (%d!=5), line ==|%s|\n", |
| 93 |
rc, line); |
| 94 |
continue; |
| 95 |
} |
| 96 |
DEBUGMSGTL(("access:scopezone:container", |
| 97 |
"addr %s, index %d, pfx %d, scope %d, flags 0x%X\n", |
| 98 |
addr, if_index, pfx_len, scope, flags)); |
| 99 |
|
| 100 |
if (if_index < 0) { |
| 101 |
DEBUGMSGTL(("access:scopezone:container", |
| 102 |
"Invalid interface index %d\n", if_index)); |
| 103 |
continue; |
| 104 |
} |
| 105 |
if (! (scope & IPV6_ADDR_LINKLOCAL)) { |
| 106 |
DEBUGMSGTL(("access:scopezone:container", |
| 107 |
"The address is not link-local, skipping\n")); |
| 108 |
continue; |
| 109 |
} |
| 110 |
/* |
| 111 |
* Check that the interface was not inserted before, just in case |
| 112 |
* one interface has two or more link-local addresses. |
| 113 |
*/ |
| 114 |
if (last_if_index == if_index) { |
| 115 |
DEBUGMSGTL(("access:scopezone:container", |
| 116 |
"The interface was already inserted, skipping\n")); |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
last_if_index = if_index; |
| 121 |
entry = netsnmp_access_scopezone_entry_create(); |
| 122 |
if(NULL == entry) { |
| 123 |
rc = -3; |
| 124 |
break; |
| 125 |
} |
| 126 |
entry->ns_scopezone_index = ++idx_offset; |
| 127 |
entry->index = if_index; |
| 128 |
entry->scopezone_linklocal = if_index; |
| 129 |
|
| 130 |
CONTAINER_INSERT(container, entry); |
| 131 |
} |
| 132 |
fclose(in); |
| 133 |
if(rc<0) |
| 134 |
return rc; |
| 135 |
|
| 136 |
return idx_offset; |
| 137 |
} |
| 138 |
#endif |