| 1 |
/* IPV6 base transport support functions |
| 2 |
* |
| 3 |
* Portions of this file are subject to the following copyright(s). See |
| 4 |
* the Net-SNMP's COPYING file for more details and other copyrights |
| 5 |
* that may apply: |
| 6 |
* |
| 7 |
* Portions of this file are copyrighted by: |
| 8 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 9 |
* Use is subject to license terms specified in the COPYING file |
| 10 |
* distributed with the Net-SNMP package. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <net-snmp/net-snmp-config.h> |
| 14 |
|
| 15 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 16 |
|
| 17 |
#include <net-snmp/types.h> |
| 18 |
#include "snmpIPBaseDomain.h" |
| 19 |
#include <net-snmp/library/snmpIPv6BaseDomain.h> |
| 20 |
#include <net-snmp/library/system.h> |
| 21 |
#include <net-snmp/library/snmp_assert.h> |
| 22 |
|
| 23 |
#include <stddef.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <sys/types.h> |
| 26 |
#include <ctype.h> |
| 27 |
#ifdef HAVE_STDLIB_H |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
#ifdef HAVE_STRING_H |
| 31 |
#include <string.h> |
| 32 |
#else |
| 33 |
#include <strings.h> |
| 34 |
#endif |
| 35 |
#ifdef HAVE_SYS_SOCKET_H |
| 36 |
#include <sys/socket.h> |
| 37 |
#endif |
| 38 |
#ifdef HAVE_NETINET_IN_H |
| 39 |
#include <netinet/in.h> |
| 40 |
#endif |
| 41 |
#ifdef HAVE_ARPA_INET_H |
| 42 |
#include <arpa/inet.h> |
| 43 |
#endif |
| 44 |
#ifdef HAVE_NETDB_H |
| 45 |
#include <netdb.h> |
| 46 |
#endif |
| 47 |
#ifdef HAVE_NET_IF_H |
| 48 |
#include <net/if.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#include <net-snmp/types.h> |
| 52 |
#include <net-snmp/library/snmp_debug.h> |
| 53 |
#include <net-snmp/library/default_store.h> |
| 54 |
#include <net-snmp/library/snmp_logging.h> |
| 55 |
|
| 56 |
#include "inet_ntop.h" |
| 57 |
#include "inet_pton.h" |
| 58 |
|
| 59 |
|
| 60 |
#if defined(WIN32) && !defined(IF_NAMESIZE) |
| 61 |
#define IF_NAMESIZE 12 |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
#if defined(HAVE_WINSOCK_H) && !defined(mingw32) |
| 66 |
static const struct in6_addr in6addr_any; /*IN6ADDR_ANY_INIT*/ |
| 67 |
#endif |
| 68 |
|
| 69 |
|
| 70 |
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID |
| 71 |
static unsigned |
| 72 |
netsnmp_if_nametoindex(const char *ifname) |
| 73 |
{ |
| 74 |
#if defined(WIN32) |
| 75 |
return atoi(ifname); |
| 76 |
#elif defined(HAVE_IF_NAMETOINDEX) |
| 77 |
int res; |
| 78 |
|
| 79 |
res = if_nametoindex(ifname); |
| 80 |
if (res == 0) |
| 81 |
res = atoi(ifname); |
| 82 |
|
| 83 |
return res; |
| 84 |
#else |
| 85 |
return 0; |
| 86 |
#endif |
| 87 |
} |
| 88 |
|
| 89 |
static char * |
| 90 |
netsnmp_if_indextoname(unsigned ifindex, char *ifname) |
| 91 |
{ |
| 92 |
#if defined(WIN32) |
| 93 |
snprintf(ifname, IF_NAMESIZE, "%u", ifindex); |
| 94 |
return ifname; |
| 95 |
#elif defined(HAVE_IF_NAMETOINDEX) |
| 96 |
return if_indextoname(ifindex, ifname); |
| 97 |
#else |
| 98 |
return NULL; |
| 99 |
#endif |
| 100 |
} |
| 101 |
#endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */ |
| 102 |
|
| 103 |
char * |
| 104 |
netsnmp_ipv6_fmtaddr(const char *prefix, netsnmp_transport *t, |
| 105 |
const void *data, int len) |
| 106 |
{ |
| 107 |
const struct sockaddr_in6 *to; |
| 108 |
char scope_id[IF_NAMESIZE + 1] = ""; |
| 109 |
char addr[INET6_ADDRSTRLEN]; |
| 110 |
char *tmp; |
| 111 |
|
| 112 |
DEBUGMSGTL(("netsnmp_ipv6", "fmtaddr: t = %p, data = %p, len = %d\n", t, |
| 113 |
data, len)); |
| 114 |
|
| 115 |
if (t && !data) { |
| 116 |
data = t->data; |
| 117 |
len = t->data_length; |
| 118 |
} |
| 119 |
|
| 120 |
switch (data ? len : 0) { |
| 121 |
case sizeof(struct sockaddr_in6): |
| 122 |
to = data; |
| 123 |
break; |
| 124 |
case sizeof(netsnmp_indexed_addr_pair): { |
| 125 |
const netsnmp_indexed_addr_pair *addr_pair = data; |
| 126 |
|
| 127 |
to = (const struct sockaddr_in6 *)&addr_pair->remote_addr; |
| 128 |
break; |
| 129 |
} |
| 130 |
default: |
| 131 |
netsnmp_assert(0); |
| 132 |
if (asprintf(&tmp, "%s: unknown", prefix) < 0) |
| 133 |
tmp = NULL; |
| 134 |
return tmp; |
| 135 |
} |
| 136 |
|
| 137 |
netsnmp_assert(to->sin6_family == AF_INET6); |
| 138 |
|
| 139 |
if (t && t->flags & NETSNMP_TRANSPORT_FLAG_HOSTNAME) { |
| 140 |
struct hostent *host; |
| 141 |
host = netsnmp_gethostbyaddr(&to->sin6_addr, sizeof(struct in6_addr), AF_INET6); |
| 142 |
return (host ? strdup(host->h_name) : NULL); |
| 143 |
} else { |
| 144 |
#if defined(HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID) |
| 145 |
if (to->sin6_scope_id && |
| 146 |
netsnmp_if_indextoname(to->sin6_scope_id, &scope_id[1])) |
| 147 |
scope_id[0] = '%'; |
| 148 |
#endif |
| 149 |
inet_ntop(AF_INET6, &to->sin6_addr, addr, sizeof(addr)); |
| 150 |
if (asprintf(&tmp, "%s: [%s%s]:%hu", prefix, addr, scope_id, |
| 151 |
ntohs(to->sin6_port)) < 0) |
| 152 |
tmp = NULL; |
| 153 |
} |
| 154 |
return tmp; |
| 155 |
} |
| 156 |
|
| 157 |
void netsnmp_ipv6_get_taddr(struct netsnmp_transport_s *t, void **addr, |
| 158 |
size_t *addr_len) |
| 159 |
{ |
| 160 |
struct sockaddr_in6 *sin6 = t->remote; |
| 161 |
|
| 162 |
netsnmp_assert(t->remote_length == sizeof(*sin6)); |
| 163 |
|
| 164 |
*addr_len = 18; |
| 165 |
if ((*addr = malloc(*addr_len))) { |
| 166 |
unsigned char *p = *addr; |
| 167 |
|
| 168 |
memcpy(p, &sin6->sin6_addr, 16); |
| 169 |
memcpy(p + 16, &sin6->sin6_port, 2); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
int netsnmp_ipv6_ostring_to_sockaddr(struct sockaddr_in6 *sin6, const void *o, |
| 174 |
size_t o_len) |
| 175 |
{ |
| 176 |
const char *p = o; |
| 177 |
|
| 178 |
if (o_len != 18) |
| 179 |
return 0; |
| 180 |
|
| 181 |
memset(sin6, 0, sizeof(*sin6)); |
| 182 |
sin6->sin6_family = AF_INET6; |
| 183 |
memcpy(&sin6->sin6_addr, p + 0, 16); |
| 184 |
memcpy(&sin6->sin6_port, p + 16, 2); |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
static int netsnmp_resolve_v6_hostname(struct in6_addr *addr, |
| 189 |
const char *hostname) |
| 190 |
{ |
| 191 |
#ifdef HAVE_GETADDRINFO |
| 192 |
struct addrinfo hint = { 0 }; |
| 193 |
struct addrinfo *addrs; |
| 194 |
int err; |
| 195 |
|
| 196 |
hint.ai_family = PF_INET6; |
| 197 |
hint.ai_socktype = SOCK_DGRAM; |
| 198 |
err = netsnmp_getaddrinfo(hostname, NULL, &hint, &addrs); |
| 199 |
if (err) |
| 200 |
return 0; |
| 201 |
|
| 202 |
if (addrs) { |
| 203 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "hostname (resolved okay)\n")); |
| 204 |
*addr = ((struct sockaddr_in6 *)addrs->ai_addr)->sin6_addr; |
| 205 |
freeaddrinfo(addrs); |
| 206 |
} else { |
| 207 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "Failed to resolve IPv6 hostname\n")); |
| 208 |
} |
| 209 |
return 1; |
| 210 |
#elif defined(HAVE_GETIPNODEBYNAME) |
| 211 |
struct hostent *hp; |
| 212 |
int err; |
| 213 |
|
| 214 |
hp = getipnodebyname(hostname, AF_INET6, 0, &err); |
| 215 |
if (hp == NULL) { |
| 216 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", |
| 217 |
"hostname (couldn't resolve = %d)\n", err)); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "hostname (resolved okay)\n")); |
| 221 |
memcpy(addr, hp->h_addr, hp->h_length); |
| 222 |
return 1; |
| 223 |
#elif defined(HAVE_GETHOSTBYNAME) |
| 224 |
struct hostent *hp; |
| 225 |
|
| 226 |
hp = netsnmp_gethostbyname(hostname); |
| 227 |
if (hp == NULL) { |
| 228 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", |
| 229 |
"hostname (couldn't resolve)\n")); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
if (hp->h_addrtype != AF_INET6) { |
| 233 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "hostname (not AF_INET6!)\n")); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "hostname (resolved okay)\n")); |
| 237 |
memcpy(addr, hp->h_addr, hp->h_length); |
| 238 |
return 1; |
| 239 |
#else /*HAVE_GETHOSTBYNAME */ |
| 240 |
/* |
| 241 |
* There is no name resolving function available. |
| 242 |
*/ |
| 243 |
snmp_log(LOG_ERR, |
| 244 |
"no getaddrinfo()/getipnodebyname()/gethostbyname()\n"); |
| 245 |
return 0; |
| 246 |
#endif /*HAVE_GETHOSTBYNAME */ |
| 247 |
} |
| 248 |
|
| 249 |
int |
| 250 |
netsnmp_sockaddr_in6_2(struct sockaddr_in6 *addr, |
| 251 |
const char *inpeername, const char *default_target) |
| 252 |
{ |
| 253 |
struct netsnmp_ep ep; |
| 254 |
int ret; |
| 255 |
|
| 256 |
ret = netsnmp_sockaddr_in6_3(&ep, inpeername, default_target); |
| 257 |
if (ret == 0) |
| 258 |
return 0; |
| 259 |
*addr = ep.a.sin6; |
| 260 |
return ret; |
| 261 |
} |
| 262 |
|
| 263 |
int |
| 264 |
netsnmp_sockaddr_in6_3(struct netsnmp_ep *ep, |
| 265 |
const char *inpeername, const char *default_target) |
| 266 |
{ |
| 267 |
struct sockaddr_in6 *addr = &ep->a.sin6; |
| 268 |
struct netsnmp_ep_str ep_str; |
| 269 |
char debug_addr[INET6_ADDRSTRLEN]; |
| 270 |
int port; |
| 271 |
|
| 272 |
if (!ep) |
| 273 |
return 0; |
| 274 |
|
| 275 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", |
| 276 |
"ep %p, peername \"%s\", default_target \"%s\"\n", |
| 277 |
ep, inpeername ? inpeername : "[NIL]", |
| 278 |
default_target ? default_target : "[NIL]")); |
| 279 |
|
| 280 |
memset(ep, 0, sizeof(*ep)); |
| 281 |
addr->sin6_family = AF_INET6; |
| 282 |
addr->sin6_addr = in6addr_any; |
| 283 |
addr->sin6_port = htons(SNMP_PORT); |
| 284 |
|
| 285 |
memset(&ep_str, 0, sizeof(ep_str)); |
| 286 |
port = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, |
| 287 |
NETSNMP_DS_LIB_DEFAULT_PORT); |
| 288 |
if (port != 0) |
| 289 |
snprintf(ep_str.port, sizeof(ep_str.port), "%d", port); |
| 290 |
else if (default_target && |
| 291 |
!netsnmp_parse_ep_str(&ep_str, default_target)) |
| 292 |
snmp_log(LOG_ERR, "Invalid default target %s\n", |
| 293 |
default_target); |
| 294 |
|
| 295 |
if (!inpeername || !netsnmp_parse_ep_str(&ep_str, inpeername)) |
| 296 |
return 0; |
| 297 |
|
| 298 |
if (ep_str.port[0]) |
| 299 |
addr->sin6_port = htons(atoi(ep_str.port)); |
| 300 |
if (ep_str.iface[0]) |
| 301 |
strlcpy(ep->iface, ep_str.iface, sizeof(ep->iface)); |
| 302 |
if (ep_str.addr && ep_str.addr[0]) { |
| 303 |
char *scope_id; |
| 304 |
|
| 305 |
scope_id = strchr(ep_str.addr, '%'); |
| 306 |
if (scope_id) { |
| 307 |
*scope_id = '\0'; |
| 308 |
#if defined(HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID) |
| 309 |
addr->sin6_scope_id = netsnmp_if_nametoindex(scope_id + 1); |
| 310 |
#endif |
| 311 |
} |
| 312 |
if (!inet_pton(AF_INET6, ep_str.addr, &addr->sin6_addr) && |
| 313 |
!netsnmp_resolve_v6_hostname(&addr->sin6_addr, ep_str.addr)) { |
| 314 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "failed to parse %s\n", |
| 315 |
ep_str.addr)); |
| 316 |
free(ep_str.addr); |
| 317 |
return 0; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
DEBUGMSGTL(("netsnmp_sockaddr_in6", "return { AF_INET6, [%s%%%d]:%hu }\n", |
| 322 |
inet_ntop(AF_INET6, &addr->sin6_addr, debug_addr, |
| 323 |
sizeof(debug_addr)), (int)addr->sin6_scope_id, |
| 324 |
ntohs(addr->sin6_port))); |
| 325 |
free(ep_str.addr); |
| 326 |
return 1; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
int |
| 331 |
netsnmp_sockaddr_in6(struct sockaddr_in6 *addr, |
| 332 |
const char *inpeername, int remote_port) |
| 333 |
{ |
| 334 |
char buf[sizeof(remote_port) * 3 + 2]; |
| 335 |
sprintf(buf, ":%u", remote_port); |
| 336 |
return netsnmp_sockaddr_in6_2(addr, inpeername, remote_port ? buf : NULL); |
| 337 |
} |
| 338 |
|
| 339 |
#endif /* NETSNMP_ENABLE_IPV6 */ |