| 1 |
/* |
| 2 |
* Interface MIB architecture support |
| 3 |
* |
| 4 |
* $Id:$ |
| 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/defaultrouter.h> |
| 11 |
|
| 12 |
#include "ip-mib/ipDefaultRouterTable/ipDefaultRouterTable.h" |
| 13 |
#include "defaultrouter_private.h" |
| 14 |
|
| 15 |
#include <sys/param.h> |
| 16 |
#include <sys/socket.h> |
| 17 |
#include <sys/sysctl.h> |
| 18 |
#include <net/route.h> |
| 19 |
#include <netinet/in.h> |
| 20 |
#include <arpa/inet.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#if !defined(SA_SIZE) && !defined(RT_ROUNDUP) |
| 25 |
#define RT_ROUNDUP(a) \ |
| 26 |
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 27 |
#endif |
| 28 |
|
| 29 |
/**---------------------------------------------------------------------*/ |
| 30 |
/* |
| 31 |
* local static prototypes |
| 32 |
*/ |
| 33 |
static int _load_defaultrouter_from_sysctl(netsnmp_container *, int); |
| 34 |
|
| 35 |
static int idx_offset; |
| 36 |
|
| 37 |
/* |
| 38 |
* initialize arch specific storage |
| 39 |
* |
| 40 |
* @retval 0: success |
| 41 |
* @retval <0: error |
| 42 |
*/ |
| 43 |
int |
| 44 |
netsnmp_arch_defaultrouter_entry_init(netsnmp_defaultrouter_entry *entry) |
| 45 |
{ |
| 46 |
/* |
| 47 |
* init |
| 48 |
*/ |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* |
| 54 |
* @retval 0 no errors |
| 55 |
* @retval !0 errors |
| 56 |
*/ |
| 57 |
int |
| 58 |
netsnmp_arch_defaultrouter_container_load(netsnmp_container *container, |
| 59 |
u_int load_flags) |
| 60 |
{ |
| 61 |
int err; |
| 62 |
|
| 63 |
err = 0; |
| 64 |
idx_offset = 0; |
| 65 |
|
| 66 |
DEBUGMSGTL(("access:defaultrouter:entry:arch", "load\n")); |
| 67 |
if (NULL == container) { |
| 68 |
snmp_log(LOG_ERR, |
| 69 |
"netsnmp_arch_defaultrouter_container_load: container invalid\n"); |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
err = _load_defaultrouter_from_sysctl(container, AF_INET); |
| 74 |
if (err != 0) { |
| 75 |
u_int flags = NETSNMP_ACCESS_DEFAULTROUTER_FREE_KEEP_CONTAINER; |
| 76 |
netsnmp_access_defaultrouter_container_free(container, flags); |
| 77 |
goto out; |
| 78 |
} |
| 79 |
|
| 80 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 81 |
err = _load_defaultrouter_from_sysctl(container, AF_INET6); |
| 82 |
if (err != 0) { |
| 83 |
u_int flags = NETSNMP_ACCESS_DEFAULTROUTER_FREE_KEEP_CONTAINER; |
| 84 |
netsnmp_access_defaultrouter_container_free(container, flags); |
| 85 |
goto out; |
| 86 |
} |
| 87 |
#endif |
| 88 |
|
| 89 |
out: |
| 90 |
return err; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* |
| 95 |
* @retval 0 no errors |
| 96 |
* @retval !0 errors |
| 97 |
*/ |
| 98 |
static int |
| 99 |
_load_defaultrouter_from_sysctl(netsnmp_container *container, int family) |
| 100 |
{ |
| 101 |
netsnmp_defaultrouter_entry *entry; |
| 102 |
struct rt_msghdr *rtm; |
| 103 |
struct sockaddr *dst_sa, *gw_sa; |
| 104 |
char *buf, *lim, *newbuf, *next; |
| 105 |
char address[NETSNMP_ACCESS_DEFAULTROUTER_BUF_SIZE]; |
| 106 |
int mib[6]; |
| 107 |
size_t address_len, needed; |
| 108 |
int address_type, err, preference, st; |
| 109 |
|
| 110 |
netsnmp_assert(NULL != container); |
| 111 |
|
| 112 |
mib[0] = CTL_NET; |
| 113 |
mib[1] = PF_ROUTE; |
| 114 |
mib[2] = 0; |
| 115 |
mib[3] = family; |
| 116 |
mib[4] = NET_RT_DUMP; |
| 117 |
mib[5] = 0; |
| 118 |
|
| 119 |
err = 0; |
| 120 |
|
| 121 |
buf = newbuf = NULL; |
| 122 |
|
| 123 |
if (family == AF_INET) { |
| 124 |
address_len = 4; |
| 125 |
address_type = INETADDRESSTYPE_IPV4; |
| 126 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 127 |
} else if (family == AF_INET6) { |
| 128 |
address_len = 16; |
| 129 |
address_type = INETADDRESSTYPE_IPV6; |
| 130 |
#endif |
| 131 |
} else { |
| 132 |
err = EINVAL; |
| 133 |
goto out; |
| 134 |
} |
| 135 |
|
| 136 |
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { |
| 137 |
err = errno; |
| 138 |
goto out; |
| 139 |
} |
| 140 |
|
| 141 |
/* Empty arp table. */ |
| 142 |
if (needed == 0) |
| 143 |
goto out; |
| 144 |
|
| 145 |
for (;;) { |
| 146 |
newbuf = realloc(buf, needed); |
| 147 |
if (newbuf == NULL) { |
| 148 |
err = ENOMEM; |
| 149 |
goto out; |
| 150 |
} |
| 151 |
buf = newbuf; |
| 152 |
st = sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &needed, NULL, 0); |
| 153 |
if (st == 0 || errno != ENOMEM) |
| 154 |
break; |
| 155 |
else |
| 156 |
needed += needed / 8; /* XXX: why 8? */ |
| 157 |
} |
| 158 |
if (st == -1) { |
| 159 |
err = errno; |
| 160 |
goto out; |
| 161 |
} |
| 162 |
|
| 163 |
lim = buf + needed; |
| 164 |
for (next = buf; next < lim; next += rtm->rtm_msglen) { |
| 165 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 166 |
struct in6_addr in6addr_any = { } /*IN6ADDR_ANY_INIT*/; |
| 167 |
#endif |
| 168 |
|
| 169 |
rtm = (struct rt_msghdr *)next; |
| 170 |
|
| 171 |
if (!(rtm->rtm_addrs & RTA_GATEWAY)) |
| 172 |
continue; |
| 173 |
|
| 174 |
dst_sa = (struct sockaddr*)(rtm + 1); |
| 175 |
#ifdef SA_SIZE |
| 176 |
gw_sa = (struct sockaddr*)(SA_SIZE(dst_sa) + (char*)dst_sa); |
| 177 |
#else |
| 178 |
gw_sa = (struct sockaddr*)(RT_ROUNDUP(dst_sa->sa_len) + (char*)dst_sa); |
| 179 |
#endif |
| 180 |
|
| 181 |
switch (family) { |
| 182 |
case AF_INET: |
| 183 |
if (((struct sockaddr_in*)dst_sa)->sin_addr.s_addr != INADDR_ANY) |
| 184 |
continue; |
| 185 |
memcpy(address, &((struct sockaddr_in*)gw_sa)->sin_addr.s_addr, |
| 186 |
address_len); |
| 187 |
break; |
| 188 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 189 |
case AF_INET6: |
| 190 |
if (memcmp(((struct sockaddr_in6*)dst_sa)->sin6_addr.s6_addr, |
| 191 |
&in6addr_any, sizeof in6addr_any) != 0) |
| 192 |
continue; /* XXX: need to determine qualifying criteria for |
| 193 |
* default gateways in IPv6. */ |
| 194 |
memcpy(address, &((struct sockaddr_in6*)dst_sa)->sin6_addr.s6_addr, |
| 195 |
address_len); |
| 196 |
break; |
| 197 |
#endif |
| 198 |
default: |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
entry = netsnmp_access_defaultrouter_entry_create(); |
| 203 |
if (NULL == entry) { |
| 204 |
err = ENOMEM; |
| 205 |
break; |
| 206 |
} |
| 207 |
|
| 208 |
/* XXX: this is wrong (hardcoding the router preference to medium). */ |
| 209 |
preference = 0; |
| 210 |
|
| 211 |
entry->ns_dr_index = ++idx_offset; |
| 212 |
entry->dr_addresstype = address_type; |
| 213 |
entry->dr_address_len = address_len; |
| 214 |
memcpy(entry->dr_address, address, sizeof(address)); |
| 215 |
entry->dr_if_index = rtm->rtm_index; |
| 216 |
entry->dr_lifetime = rtm->rtm_rmx.rmx_expire; |
| 217 |
entry->dr_preference = preference; |
| 218 |
|
| 219 |
if (CONTAINER_INSERT(container, entry) < 0) { |
| 220 |
DEBUGMSGTL(("access:arp:container", |
| 221 |
"error with defaultrouter_entry: " |
| 222 |
"insert into container failed.\n")); |
| 223 |
netsnmp_access_defaultrouter_entry_free(entry); |
| 224 |
goto out; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
out: |
| 229 |
free(buf); |
| 230 |
return err; |
| 231 |
} |