1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2009-2015 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2013 Intel, Inc. All rights reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights
* reserved.
* Copyright (c) 2018 Triad National Security, LLC. All rights
* reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_NET_IF_H
# include <net/if.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#ifdef HAVE_IFADDRS_H
# include <ifaddrs.h>
#endif
#include "opal/constants.h"
#include "opal/mca/threads/tsd.h"
#include "opal/runtime/opal.h"
#include "opal/runtime/opal_params.h"
#include "opal/util/argv.h"
#include "opal/util/net.h"
#include "opal/util/output.h"
#include "opal/util/show_help.h"
/* this function doesn't depend on sockaddr_h */
bool opal_net_isaddr(const char *name)
{
struct addrinfo hint, *res = NULL;
/* initialize the hint */
memset(&hint, '\0', sizeof hint);
/* indicate that we don't know the family */
hint.ai_family = PF_UNSPEC;
hint.ai_flags = AI_NUMERICHOST;
if (0 != getaddrinfo(name, NULL, &hint, &res)) {
/* the input wasn't a recognizable address */
return false;
}
/* we don't care what family - all we care is that
* it is indeed an address
*/
freeaddrinfo(res);
return true;
}
#ifdef HAVE_STRUCT_SOCKADDR_IN
typedef struct private_ipv4_t {
in_addr_t addr;
uint32_t netmask_bits;
} private_ipv4_t;
static private_ipv4_t *private_ipv4 = NULL;
# if OPAL_ENABLE_IPV6
static opal_tsd_tracked_key_t *hostname_tsd_key = NULL;
static void hostname_cleanup(void *value)
{
if (NULL != value)
free(value);
}
static char *get_hostname_buffer(void)
{
void *buffer;
int ret;
ret = opal_tsd_tracked_key_get(hostname_tsd_key, &buffer);
if (OPAL_SUCCESS != ret)
return NULL;
if (NULL == buffer) {
buffer = (void *) malloc((NI_MAXHOST + 1) * sizeof(char));
ret = opal_tsd_tracked_key_set(hostname_tsd_key, buffer);
}
return (char *) buffer;
}
# endif
/**
* Finalize the network helper subsystem
*
* Finalize the network helper subsystem. Should be called exactly
* once for any process that will use any function in the network
* helper subsystem.
*
* @retval OPAL_SUCCESS Success
*/
static void opal_net_finalize(void)
{
# if OPAL_ENABLE_IPV6
if (NULL != hostname_tsd_key) {
OBJ_RELEASE(hostname_tsd_key);
hostname_tsd_key = NULL;
}
# endif
free(private_ipv4);
private_ipv4 = NULL;
}
int opal_net_init(void)
{
char **args, *arg;
uint32_t a, b, c, d, bits, addr;
int i, count, found_bad = 0;
args = opal_argv_split(opal_net_private_ipv4, ';');
if (NULL != args) {
count = opal_argv_count(args);
private_ipv4 = (private_ipv4_t *) malloc((count + 1) * sizeof(private_ipv4_t));
if (NULL == private_ipv4) {
opal_output(0, "Unable to allocate memory for the private addresses array");
opal_argv_free(args);
goto do_local_init;
}
for (i = 0; i < count; i++) {
arg = args[i];
(void) sscanf(arg, "%u.%u.%u.%u/%u", &a, &b, &c, &d, &bits);
if ((a > 255) || (b > 255) || (c > 255) || (d > 255) || (bits > 32)) {
if (0 == found_bad) {
opal_show_help("help-opal-util.txt", "malformed net_private_ipv4", true,
args[i]);
found_bad = 1;
}
continue;
}
addr = (a << 24) | (b << 16) | (c << 8) | d;
private_ipv4[i].addr = htonl(addr);
private_ipv4[i].netmask_bits = bits;
}
private_ipv4[i].addr = 0;
private_ipv4[i].netmask_bits = 0;
opal_argv_free(args);
}
opal_finalize_register_cleanup(opal_net_finalize);
do_local_init:
# if OPAL_ENABLE_IPV6
hostname_tsd_key = OBJ_NEW(opal_tsd_tracked_key_t);
opal_tsd_tracked_key_set_destructor(hostname_tsd_key, hostname_cleanup);
# endif
return OPAL_SUCCESS;
}
/* convert a CIDR prefixlen to netmask (in network byte order) */
uint32_t opal_net_prefix2netmask(uint32_t prefixlen)
{
return htonl(((1u << prefixlen) - 1u) << (32 - prefixlen));
}
bool opal_net_islocalhost(const struct sockaddr *addr)
{
switch (addr->sa_family) {
case AF_INET: {
const struct sockaddr_in *inaddr = (struct sockaddr_in *) addr;
/* if it's in the 127. domain, it shouldn't be routed
(0x7f == 127) */
if (0x7F000000 == (0x7F000000 & ntohl(inaddr->sin_addr.s_addr))) {
return true;
}
return false;
} break;
# if OPAL_ENABLE_IPV6
case AF_INET6: {
const struct sockaddr_in6 *inaddr = (struct sockaddr_in6 *) addr;
if (IN6_IS_ADDR_LOOPBACK(&inaddr->sin6_addr)) {
return true; /* Bug, FIXME: check for 127.0.0.1/8 */
}
return false;
} break;
# endif
default:
opal_output(0, "unhandled sa_family %d passed to opal_net_islocalhost", addr->sa_family);
return false;
break;
}
}
bool opal_net_samenetwork(const struct sockaddr *addr1, const struct sockaddr *addr2, uint32_t plen)
{
uint32_t prefixlen;
if (addr1->sa_family != addr2->sa_family) {
return false; /* address families must be equal */
}
switch (addr1->sa_family) {
case AF_INET: {
if (0 == plen) {
prefixlen = 32;
} else {
prefixlen = plen;
}
struct sockaddr_in inaddr1, inaddr2;
/* Use temporary variables and memcpy's so that we don't
run into bus errors on Solaris/SPARC */
memcpy(&inaddr1, addr1, sizeof(inaddr1));
memcpy(&inaddr2, addr2, sizeof(inaddr2));
uint32_t netmask = opal_net_prefix2netmask(prefixlen);
if ((inaddr1.sin_addr.s_addr & netmask) == (inaddr2.sin_addr.s_addr & netmask)) {
return true;
}
return false;
} break;
# if OPAL_ENABLE_IPV6
case AF_INET6: {
struct sockaddr_in6 inaddr1, inaddr2;
/* Use temporary variables and memcpy's so that we don't
run into bus errors on Solaris/SPARC */
memcpy(&inaddr1, addr1, sizeof(inaddr1));
memcpy(&inaddr2, addr2, sizeof(inaddr2));
struct in6_addr *a6_1 = (struct in6_addr *) &inaddr1.sin6_addr;
struct in6_addr *a6_2 = (struct in6_addr *) &inaddr2.sin6_addr;
if (0 == plen) {
prefixlen = 64;
} else {
prefixlen = plen;
}
if (64 == prefixlen) {
/* prefixlen is always /64, any other case would be routing.
Compare the first eight bytes (64 bits) and hope that
endianness is not an issue on any system as long as
addresses are always stored in network byte order.
*/
if (((const uint32_t *) (a6_1))[0] == ((const uint32_t *) (a6_2))[0]
&& ((const uint32_t *) (a6_1))[1] == ((const uint32_t *) (a6_2))[1]) {
return true;
}
}
return false;
} break;
# endif
default:
opal_output(0, "unhandled sa_family %d passed to opal_samenetwork", addr1->sa_family);
}
return false;
}
/**
* Returns true if the given address is a public IPv4 address.
*/
bool opal_net_addr_isipv4public(const struct sockaddr *addr)
{
switch (addr->sa_family) {
# if OPAL_ENABLE_IPV6
case AF_INET6:
return false;
# endif
case AF_INET: {
const struct sockaddr_in *inaddr = (struct sockaddr_in *) addr;
int i;
if (NULL == private_ipv4) {
return true;
}
for (i = 0; private_ipv4[i].addr != 0; i++) {
if (private_ipv4[i].addr
== (inaddr->sin_addr.s_addr
& opal_net_prefix2netmask(private_ipv4[i].netmask_bits))) {
return false;
}
}
}
return true;
default:
opal_output(0, "unhandled sa_family %d passed to opal_net_addr_isipv4public\n",
addr->sa_family);
}
return false;
}
bool opal_net_addr_isipv6linklocal(const struct sockaddr *addr)
{
# if OPAL_ENABLE_IPV6
struct sockaddr_in6 if_addr;
# endif
switch (addr->sa_family) {
# if OPAL_ENABLE_IPV6
case AF_INET6:
if_addr.sin6_family = AF_INET6;
if (1 != inet_pton(AF_INET6, "fe80::0000", &if_addr.sin6_addr)) {
return false;
}
return opal_net_samenetwork(addr, (struct sockaddr *) &if_addr, 64);
# endif
case AF_INET:
return false;
default:
opal_output(0, "unhandled sa_family %d passed to opal_net_addr_isipv6linklocal\n",
addr->sa_family);
}
return false;
}
char *opal_net_get_hostname(const struct sockaddr *addr)
{
# if OPAL_ENABLE_IPV6
char *name = get_hostname_buffer();
int error;
socklen_t addrlen;
char *p;
if (NULL == name) {
opal_output(0, "opal_sockaddr2str: malloc() failed\n");
return NULL;
}
OPAL_DEBUG_ZERO(*name);
switch (addr->sa_family) {
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
# if defined(__NetBSD__)
/* hotfix for netbsd: on my netbsd machine, getnameinfo
returns an unknown error code. */
if (NULL
== inet_ntop(AF_INET6, &((struct sockaddr_in6 *) addr)->sin6_addr, name, NI_MAXHOST)) {
opal_output(0, "opal_sockaddr2str failed with error code %d", errno);
return NULL;
}
return name;
# else
addrlen = sizeof(struct sockaddr_in6);
# endif
break;
default:
return NULL;
}
error = getnameinfo(addr, addrlen, name, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (error) {
int err = errno;
opal_output(0, "opal_sockaddr2str failed:%s (return code %i)\n", gai_strerror(err), error);
return NULL;
}
/* strip any trailing % data as it isn't pertinent */
if (NULL != (p = strrchr(name, '%'))) {
*p = '\0';
}
return name;
# else
return inet_ntoa(((struct sockaddr_in *) addr)->sin_addr);
# endif
}
int opal_net_get_port(const struct sockaddr *addr)
{
switch (addr->sa_family) {
case AF_INET:
return ntohs(((struct sockaddr_in *) addr)->sin_port);
break;
# if OPAL_ENABLE_IPV6
case AF_INET6:
return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
break;
# endif
}
return -1;
}
#else /* HAVE_STRUCT_SOCKADDR_IN */
int opal_net_init()
{
return OPAL_SUCCESS;
}
int opal_net_finalize()
{
return OPAL_SUCCESS;
}
uint32_t opal_net_prefix2netmask(uint32_t prefixlen)
{
return 0;
}
bool opal_net_islocalhost(const struct sockaddr *addr)
{
return false;
}
bool opal_net_samenetwork(const struct sockaddr *addr1, const struct sockaddr *addr2,
uint32_t prefixlen)
{
return false;
}
bool opal_net_addr_isipv4public(const struct sockaddr *addr)
{
return false;
}
char *opal_net_get_hostname(const struct sockaddr *addr)
{
return NULL;
}
int opal_net_get_port(const struct sockaddr *addr)
{
return -1;
}
#endif /* HAVE_STRUCT_SOCKADDR_IN */
|