| 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 <asm/types.h> |
| 16 |
#ifdef HAVE_LINUX_RTNETLINK_H |
| 17 |
#include <linux/netlink.h> |
| 18 |
#include <linux/rtnetlink.h> |
| 19 |
#endif |
| 20 |
#include <sys/socket.h> |
| 21 |
#include <arpa/inet.h> |
| 22 |
#include <errno.h> |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
#define RCVBUF_SIZE 32768 |
| 26 |
#define SNDBUF_SIZE 512 |
| 27 |
|
| 28 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 29 |
#define DR_ADDRSTRLEN INET6_ADDRSTRLEN |
| 30 |
#else |
| 31 |
#define DR_ADDRSTRLEN INET_ADDRSTRLEN |
| 32 |
#endif |
| 33 |
|
| 34 |
/**---------------------------------------------------------------------*/ |
| 35 |
/* |
| 36 |
* local static prototypes |
| 37 |
*/ |
| 38 |
static int _load(netsnmp_container *container); |
| 39 |
|
| 40 |
|
| 41 |
/* |
| 42 |
* initialize arch specific storage |
| 43 |
* |
| 44 |
* @retval 0: success |
| 45 |
* @retval <0: error |
| 46 |
*/ |
| 47 |
int |
| 48 |
netsnmp_arch_defaultrouter_entry_init(netsnmp_defaultrouter_entry *entry) |
| 49 |
{ |
| 50 |
/* |
| 51 |
* init |
| 52 |
*/ |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* |
| 58 |
* @retval 0 no errors |
| 59 |
* @retval !0 errors |
| 60 |
*/ |
| 61 |
int |
| 62 |
netsnmp_arch_defaultrouter_container_load(netsnmp_container *container, |
| 63 |
u_int load_flags) |
| 64 |
{ |
| 65 |
int rc = 0; |
| 66 |
|
| 67 |
DEBUGMSGTL(("access:defaultrouter:entry:arch", "load (linux)\n")); |
| 68 |
|
| 69 |
rc = _load(container); |
| 70 |
if (rc < 0) { |
| 71 |
u_int flags = NETSNMP_ACCESS_DEFAULTROUTER_FREE_KEEP_CONTAINER; |
| 72 |
netsnmp_access_defaultrouter_container_free(container, flags); |
| 73 |
} |
| 74 |
|
| 75 |
return rc; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* |
| 80 |
* @retval 0 no errors |
| 81 |
* @retval !0 errors |
| 82 |
*/ |
| 83 |
static int |
| 84 |
_load(netsnmp_container *container) |
| 85 |
{ |
| 86 |
#ifndef HAVE_LIBNL3 |
| 87 |
DEBUGMSGTL(("access:defaultrouter", |
| 88 |
"cannot get default router information" |
| 89 |
"as netlink socket is not available\n")); |
| 90 |
return -1; |
| 91 |
#else |
| 92 |
int rc = 0; |
| 93 |
int idx_offset = 0; |
| 94 |
netsnmp_defaultrouter_entry *entry; |
| 95 |
int nlsk; |
| 96 |
struct sockaddr_nl addr; |
| 97 |
int rcvbuf_size = RCVBUF_SIZE; |
| 98 |
union { |
| 99 |
struct nlmsghdr hdr; |
| 100 |
unsigned char rcvbuf[RCVBUF_SIZE]; |
| 101 |
} rcvbuf_union; |
| 102 |
union { |
| 103 |
struct nlmsghdr hdr; |
| 104 |
unsigned char sndbuf[SNDBUF_SIZE]; |
| 105 |
} sndbuf_union; |
| 106 |
unsigned char *const rcvbuf = rcvbuf_union.rcvbuf; |
| 107 |
unsigned char *const sndbuf = sndbuf_union.sndbuf; |
| 108 |
struct nlmsghdr *hdr; |
| 109 |
struct rtmsg *rthdr; |
| 110 |
int count; |
| 111 |
int end_of_message = 0; |
| 112 |
|
| 113 |
netsnmp_assert(NULL != container); |
| 114 |
|
| 115 |
/* |
| 116 |
* Open a netlink socket |
| 117 |
*/ |
| 118 |
nlsk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 119 |
if (nlsk < 0) { |
| 120 |
snmp_log(LOG_ERR, "Could not open netlink socket : %s\n", |
| 121 |
strerror(errno)); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
if (setsockopt(nlsk, SOL_SOCKET, SO_RCVBUF, |
| 126 |
&rcvbuf_size, sizeof(rcvbuf_size)) < 0) { |
| 127 |
snmp_log(LOG_ERR, "Could not open netlink socket : %s\n", |
| 128 |
strerror(errno)); |
| 129 |
close(nlsk); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
memset(&addr, '\0', sizeof(struct sockaddr_nl)); |
| 134 |
addr.nl_family = AF_NETLINK; |
| 135 |
|
| 136 |
memset(sndbuf, '\0', SNDBUF_SIZE); |
| 137 |
hdr = &sndbuf_union.hdr; |
| 138 |
hdr->nlmsg_type = RTM_GETROUTE; |
| 139 |
hdr->nlmsg_pid = getpid(); |
| 140 |
hdr->nlmsg_seq = 0; |
| 141 |
hdr->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
| 142 |
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 143 |
rthdr = (struct rtmsg *)NLMSG_DATA(hdr); |
| 144 |
rthdr->rtm_table = RT_TABLE_MAIN; |
| 145 |
|
| 146 |
/* |
| 147 |
* Send a request to the kernel to dump the routing table to us |
| 148 |
*/ |
| 149 |
count = sendto(nlsk, sndbuf, hdr->nlmsg_len, 0, |
| 150 |
(struct sockaddr *)&addr, sizeof(struct sockaddr_nl)); |
| 151 |
if (count < 0) { |
| 152 |
snmp_log(LOG_ERR, "unable to send netlink message to kernel : %s\n", |
| 153 |
strerror(errno)); |
| 154 |
close(nlsk); |
| 155 |
return -2; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Now listen for response |
| 160 |
*/ |
| 161 |
do { |
| 162 |
struct nlmsghdr *nlmhp; |
| 163 |
struct rtmsg *rtmp; |
| 164 |
struct rtattr *rtap; |
| 165 |
socklen_t sock_len; |
| 166 |
int rtcount; |
| 167 |
|
| 168 |
memset(rcvbuf, '\0', RCVBUF_SIZE); |
| 169 |
sock_len = sizeof(struct sockaddr_nl); |
| 170 |
|
| 171 |
/* |
| 172 |
* Get the message |
| 173 |
*/ |
| 174 |
count = recvfrom(nlsk, rcvbuf, RCVBUF_SIZE, 0, |
| 175 |
(struct sockaddr *)&addr, &sock_len); |
| 176 |
if (count < 0) { |
| 177 |
snmp_log(LOG_ERR, "unable to receive netlink messages: %s\n", |
| 178 |
strerror(errno)); |
| 179 |
rc = -1; |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Walk all of the returned messages |
| 185 |
*/ |
| 186 |
nlmhp = &rcvbuf_union.hdr; |
| 187 |
while (NLMSG_OK(nlmhp, count)) { |
| 188 |
u_char addresstype; |
| 189 |
char address[NETSNMP_ACCESS_DEFAULTROUTER_BUF_SIZE + 1]; |
| 190 |
size_t address_len = 0; |
| 191 |
int if_index = -1; |
| 192 |
u_long lifetime = 0; |
| 193 |
int preference = -3; |
| 194 |
|
| 195 |
/* |
| 196 |
* Make sure the message is ok |
| 197 |
*/ |
| 198 |
if (nlmhp->nlmsg_type == NLMSG_ERROR) { |
| 199 |
snmp_log(LOG_ERR, "kernel produced nlmsg err\n"); |
| 200 |
rc = -1; |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* End of message, we're done |
| 206 |
*/ |
| 207 |
if (nlmhp->nlmsg_type & NLMSG_DONE) { |
| 208 |
end_of_message = 1; |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* Get the pointer to the rtmsg struct |
| 214 |
*/ |
| 215 |
rtmp = NLMSG_DATA(nlmhp); |
| 216 |
|
| 217 |
/* |
| 218 |
* zero length destination is a default route |
| 219 |
*/ |
| 220 |
if (rtmp->rtm_dst_len != 0) |
| 221 |
goto next_nlmsghdr; |
| 222 |
|
| 223 |
/* |
| 224 |
* Start scanning the attributes for needed info |
| 225 |
*/ |
| 226 |
if (rtmp->rtm_family == AF_INET) { |
| 227 |
addresstype = INETADDRESSTYPE_IPV4; |
| 228 |
lifetime = IPDEFAULTROUTERLIFETIME_MAX; /* infinity */ |
| 229 |
} |
| 230 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 231 |
else if (rtmp->rtm_family == AF_INET6) { |
| 232 |
addresstype = INETADDRESSTYPE_IPV6; |
| 233 |
/* router lifetime for IPv6 is retrieved by RTA_CACHEINFO */ |
| 234 |
lifetime = 0; |
| 235 |
} |
| 236 |
#endif |
| 237 |
else |
| 238 |
goto next_nlmsghdr; /* skip, we don't care about this route */ |
| 239 |
|
| 240 |
preference = 0; /* preference is medium(0) for now */ |
| 241 |
|
| 242 |
rtap = RTM_RTA(rtmp); |
| 243 |
rtcount = RTM_PAYLOAD(nlmhp); |
| 244 |
while (RTA_OK(rtap, rtcount)) { |
| 245 |
switch (rtap->rta_type) { |
| 246 |
case RTA_OIF: |
| 247 |
if_index = *(int *)(RTA_DATA(rtap)); |
| 248 |
break; |
| 249 |
|
| 250 |
case RTA_GATEWAY: |
| 251 |
address_len = RTA_PAYLOAD(rtap); |
| 252 |
memset(address, '\0', sizeof(address)); |
| 253 |
memcpy(address, RTA_DATA(rtap), address_len); |
| 254 |
break; |
| 255 |
|
| 256 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 257 |
case RTA_CACHEINFO: |
| 258 |
{ |
| 259 |
struct rta_cacheinfo *rtci = RTA_DATA(rtap); |
| 260 |
long hz = sysconf(_SC_CLK_TCK); |
| 261 |
if ((rtmp->rtm_flags & RTM_F_CLONED) || |
| 262 |
(rtci && rtci->rta_expires)) { |
| 263 |
lifetime = rtci->rta_expires / hz; |
| 264 |
} |
| 265 |
} |
| 266 |
break; |
| 267 |
#endif |
| 268 |
|
| 269 |
default: |
| 270 |
break; |
| 271 |
} /* switch */ |
| 272 |
|
| 273 |
rtap = RTA_NEXT(rtap, rtcount); |
| 274 |
} /* while RTA_OK(rtap) */ |
| 275 |
|
| 276 |
/* clip the calculated lifetime if necessary */ |
| 277 |
if (lifetime > IPDEFAULTROUTERLIFETIME_MAX) { |
| 278 |
lifetime = IPDEFAULTROUTERLIFETIME_MAX; |
| 279 |
} |
| 280 |
|
| 281 |
if (address_len != 0 && if_index != -1 && |
| 282 |
lifetime != 0 && preference != -3 ) { |
| 283 |
DEBUGIF("access:defaultrouter") { |
| 284 |
char addr_str[DR_ADDRSTRLEN]; |
| 285 |
memset(addr_str, '\0', DR_ADDRSTRLEN); |
| 286 |
|
| 287 |
if (rtmp->rtm_family == AF_INET) |
| 288 |
inet_ntop(AF_INET, address, addr_str, DR_ADDRSTRLEN); |
| 289 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 290 |
else |
| 291 |
inet_ntop(AF_INET6, address, addr_str, DR_ADDRSTRLEN); |
| 292 |
#endif |
| 293 |
DEBUGMSGTL(("access:defaultrouter", |
| 294 |
"found default route: %s if_index %d " |
| 295 |
"lifetime %lu preference %d\n", |
| 296 |
addr_str, if_index, lifetime, preference)); |
| 297 |
} |
| 298 |
|
| 299 |
entry = netsnmp_access_defaultrouter_entry_create(); |
| 300 |
if (NULL == entry) { |
| 301 |
rc = -3; |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
entry->ns_dr_index = ++idx_offset; |
| 306 |
entry->dr_addresstype = addresstype; |
| 307 |
entry->dr_address_len = address_len; |
| 308 |
memcpy(entry->dr_address, address, |
| 309 |
NETSNMP_ACCESS_DEFAULTROUTER_BUF_SIZE); |
| 310 |
entry->dr_if_index = if_index; |
| 311 |
entry->dr_lifetime = lifetime; |
| 312 |
entry->dr_preference = preference; |
| 313 |
|
| 314 |
if (CONTAINER_INSERT(container, entry) < 0) |
| 315 |
{ |
| 316 |
DEBUGMSGTL(("access:arp:container", |
| 317 |
"error with defaultrouter_entry: " |
| 318 |
"insert into container failed.\n")); |
| 319 |
netsnmp_access_defaultrouter_entry_free(entry); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
next_nlmsghdr: |
| 324 |
nlmhp = NLMSG_NEXT(nlmhp, count); |
| 325 |
} /* while NLMSG_OK(nlmhp) */ |
| 326 |
|
| 327 |
if (rc < 0) |
| 328 |
break; |
| 329 |
|
| 330 |
} while (!end_of_message); |
| 331 |
|
| 332 |
close(nlsk); |
| 333 |
return rc; |
| 334 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 335 |
} |