| 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 |
|
| 19 |
#include <errno.h> |
| 20 |
#include <sys/ioctl.h> |
| 21 |
#include <arpa/inet.h> |
| 22 |
|
| 23 |
netsnmp_feature_require(prefix_info); |
| 24 |
netsnmp_feature_require(find_prefix_info); |
| 25 |
|
| 26 |
netsnmp_feature_child_of(ipaddress_arch_entry_copy, ipaddress_common); |
| 27 |
|
| 28 |
#ifdef NETSNMP_FEATURE_REQUIRE_IPADDRESS_ARCH_ENTRY_COPY |
| 29 |
netsnmp_feature_require(ipaddress_ioctl_entry_copy); |
| 30 |
#endif /* NETSNMP_FEATURE_REQUIRE_IPADDRESS_ARCH_ENTRY_COPY */ |
| 31 |
|
| 32 |
#include "ipaddress_ioctl.h" |
| 33 |
#include "ipaddress_private.h" |
| 34 |
#include "if-mib/data_access/interface_private.h" |
| 35 |
|
| 36 |
#define ROUNDUP(a) \ |
| 37 |
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 38 |
|
| 39 |
/* |
| 40 |
* initialize arch specific storage |
| 41 |
* |
| 42 |
* @retval 0: success |
| 43 |
* @retval <0: error |
| 44 |
*/ |
| 45 |
int |
| 46 |
netsnmp_arch_ipaddress_entry_init(netsnmp_ipaddress_entry *entry) |
| 47 |
{ |
| 48 |
/* |
| 49 |
* init ipv4 stuff |
| 50 |
*/ |
| 51 |
/* if (NULL == netsnmp_ioctl_ipaddress_entry_init(entry)) */ |
| 52 |
/* return -1; */ |
| 53 |
|
| 54 |
/* |
| 55 |
* init ipv6 stuff |
| 56 |
* so far, we can just share the ipv4 stuff, so nothing to do |
| 57 |
*/ |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
* cleanup arch specific storage |
| 64 |
*/ |
| 65 |
void |
| 66 |
netsnmp_arch_ipaddress_entry_cleanup(netsnmp_ipaddress_entry *entry) |
| 67 |
{ |
| 68 |
/* |
| 69 |
* cleanup ipv4 stuff |
| 70 |
*/ |
| 71 |
/*netsnmp_ioctl_ipaddress_entry_cleanup(entry); */ |
| 72 |
|
| 73 |
/* |
| 74 |
* cleanup ipv6 stuff |
| 75 |
* so far, we can just share the ipv4 stuff, so nothing to do |
| 76 |
*/ |
| 77 |
} |
| 78 |
|
| 79 |
#ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_ARCH_ENTRY_COPY |
| 80 |
/* |
| 81 |
* copy arch specific storage |
| 82 |
*/ |
| 83 |
int |
| 84 |
netsnmp_arch_ipaddress_entry_copy(netsnmp_ipaddress_entry *lhs, |
| 85 |
netsnmp_ipaddress_entry *rhs) |
| 86 |
{ |
| 87 |
int rc; |
| 88 |
|
| 89 |
rc = 0; |
| 90 |
|
| 91 |
/* |
| 92 |
* copy ipv4 stuff |
| 93 |
*/ |
| 94 |
/*rc = netsnmp_ioctl_ipaddress_entry_copy(lhs, rhs); */ |
| 95 |
if (rc) |
| 96 |
return rc; |
| 97 |
|
| 98 |
/* |
| 99 |
* copy ipv6 stuff |
| 100 |
* so far, we can just share the ipv4 stuff, so nothing to do |
| 101 |
*/ |
| 102 |
|
| 103 |
return rc; |
| 104 |
} |
| 105 |
#endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_ARCH_ENTRY_COPY */ |
| 106 |
|
| 107 |
/* |
| 108 |
* create a new entry |
| 109 |
*/ |
| 110 |
int |
| 111 |
netsnmp_arch_ipaddress_create(netsnmp_ipaddress_entry *entry) |
| 112 |
{ |
| 113 |
if (NULL == entry) |
| 114 |
return -1; |
| 115 |
|
| 116 |
if (4 == entry->ia_address_len) { |
| 117 |
return -1; |
| 118 |
} else if (16 == entry->ia_address_len) { |
| 119 |
return -1; |
| 120 |
} else { |
| 121 |
DEBUGMSGT(("access:ipaddress:create", "wrong length of IP address\n")); |
| 122 |
return -2; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/* |
| 127 |
* create a new entry |
| 128 |
*/ |
| 129 |
int |
| 130 |
netsnmp_arch_ipaddress_delete(netsnmp_ipaddress_entry *entry) |
| 131 |
{ |
| 132 |
if (NULL == entry) |
| 133 |
return -1; |
| 134 |
|
| 135 |
if (4 == entry->ia_address_len) { |
| 136 |
return -2; |
| 137 |
} else if (16 == entry->ia_address_len) { |
| 138 |
return -3; |
| 139 |
} else { |
| 140 |
DEBUGMSGT(("access:ipaddress:create", "only ipv4 supported\n")); |
| 141 |
return -2; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* |
| 147 |
* @retval 0 no errors |
| 148 |
* @retval !0 errors |
| 149 |
*/ |
| 150 |
int |
| 151 |
netsnmp_arch_ipaddress_container_load(netsnmp_container *container, |
| 152 |
u_int load_flags) |
| 153 |
{ |
| 154 |
netsnmp_ipaddress_entry *entry = NULL; |
| 155 |
u_char *if_list = NULL, *cp; |
| 156 |
size_t if_list_size = 0; |
| 157 |
int sysctl_oid[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 }; |
| 158 |
struct ifa_msghdr *ifa; |
| 159 |
struct sockaddr *a; |
| 160 |
int amask; |
| 161 |
int idx_offset = 0; |
| 162 |
|
| 163 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 164 |
"load (flags %u)\n", load_flags)); |
| 165 |
|
| 166 |
if (NULL == container) { |
| 167 |
snmp_log(LOG_ERR, "no container specified/found for interface\n"); |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
if (sysctl(sysctl_oid, sizeof(sysctl_oid)/sizeof(int), 0, |
| 172 |
&if_list_size, 0, 0) == -1) { |
| 173 |
snmp_log(LOG_ERR, "could not get interface info (size)\n"); |
| 174 |
return -2; |
| 175 |
} |
| 176 |
|
| 177 |
if_list = (u_char*)malloc(if_list_size); |
| 178 |
if (if_list == NULL) { |
| 179 |
snmp_log(LOG_ERR, "could not allocate memory for interface info " |
| 180 |
"(%u bytes)\n", (unsigned) if_list_size); |
| 181 |
return -3; |
| 182 |
} else { |
| 183 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 184 |
"allocated %u bytes for if_list\n", |
| 185 |
(unsigned) if_list_size)); |
| 186 |
} |
| 187 |
|
| 188 |
if (sysctl(sysctl_oid, sizeof(sysctl_oid)/sizeof(int), if_list, |
| 189 |
&if_list_size, 0, 0) == -1) { |
| 190 |
snmp_log(LOG_ERR, "could not get interface info\n"); |
| 191 |
free(if_list); |
| 192 |
return -2; |
| 193 |
} |
| 194 |
|
| 195 |
/* pass 2: walk addresses */ |
| 196 |
for (cp = if_list; cp < if_list + if_list_size; cp += ifa->ifam_msglen) { |
| 197 |
int rtax; |
| 198 |
|
| 199 |
ifa = (struct ifa_msghdr *) cp; |
| 200 |
|
| 201 |
if (ifa->ifam_type != RTM_NEWADDR) |
| 202 |
continue; |
| 203 |
|
| 204 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 205 |
"received 0x%x in RTM_NEWADDR for ifindex %u\n", |
| 206 |
ifa->ifam_addrs, ifa->ifam_index)); |
| 207 |
|
| 208 |
entry = netsnmp_access_ipaddress_entry_create(); |
| 209 |
if (entry == NULL) |
| 210 |
return -3; |
| 211 |
|
| 212 |
a = (struct sockaddr *) (ifa + 1); |
| 213 |
entry->ia_status = IPADDRESSSTATUSTC_UNKNOWN; |
| 214 |
entry->ia_origin = IPADDRESSORIGINTC_OTHER; |
| 215 |
entry->ia_address_len = 0; |
| 216 |
for (amask = ifa->ifam_addrs, rtax = 0; amask != 0; amask >>= 1, rtax++) { |
| 217 |
if ((amask & 1) != 0) { |
| 218 |
entry->ns_ia_index = ++idx_offset; |
| 219 |
entry->if_index = ifa->ifam_index; |
| 220 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 221 |
"%d: a=%p, sa_len=%d, sa_family=0x%x\n", |
| 222 |
(int)entry->if_index, a, a->sa_len, a->sa_family)); |
| 223 |
|
| 224 |
if (a->sa_family == AF_INET || a->sa_family == 0) { |
| 225 |
struct sockaddr_in *a4 = (struct sockaddr_in *)a; |
| 226 |
char str[128]; |
| 227 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 228 |
"IPv4 addr %s\n", inet_ntop(AF_INET, &a4->sin_addr.s_addr, str, 128))); |
| 229 |
if (rtax == RTAX_IFA) { |
| 230 |
entry->ia_address_len = 4; |
| 231 |
memcpy(entry->ia_address, &a4->sin_addr.s_addr, entry->ia_address_len); |
| 232 |
} |
| 233 |
else if (rtax == RTAX_NETMASK) |
| 234 |
entry->ia_prefix_len = netsnmp_ipaddress_ipv4_prefix_len(a4->sin_addr.s_addr); |
| 235 |
} |
| 236 |
else if (a->sa_family == AF_INET6) { |
| 237 |
struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)a; |
| 238 |
char str[128]; |
| 239 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 240 |
"IPv6 addr %s\n", inet_ntop(AF_INET6, &a6->sin6_addr.s6_addr, str, 128))); |
| 241 |
if (rtax == RTAX_IFA) { |
| 242 |
entry->ia_address_len = 16; |
| 243 |
memcpy(entry->ia_address, &a6->sin6_addr, entry->ia_address_len); |
| 244 |
} |
| 245 |
else if (rtax == RTAX_NETMASK) { |
| 246 |
entry->ia_prefix_len = netsnmp_ipaddress_ipv6_prefix_len(a6->sin6_addr); |
| 247 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 248 |
"prefix_len=%d\n", entry->ia_prefix_len)); |
| 249 |
} |
| 250 |
} |
| 251 |
a = (struct sockaddr *) ( ((char *) a) + ROUNDUP(a->sa_len) ); |
| 252 |
} |
| 253 |
} |
| 254 |
if (entry->ia_address_len == 0) { |
| 255 |
DEBUGMSGTL(("access:ipaddress:container:sysctl", |
| 256 |
"entry skipped\n")); |
| 257 |
netsnmp_access_ipaddress_entry_free(entry); |
| 258 |
} |
| 259 |
else if (CONTAINER_INSERT(container, entry) < 0) { |
| 260 |
DEBUGMSGTL(("access:ipaddress:container","error with ipaddress_entry: insert into container failed.\n")); |
| 261 |
netsnmp_access_ipaddress_entry_free(entry); |
| 262 |
continue; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (if_list != NULL) |
| 267 |
free(if_list); |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |