net.c revision 5fa46bc91672ef5737aee6f99763161511566c24
012a352f4b26cfd874db8d06debc495c2303e8b2Bob Halley/*
178f6ad061e54bc5babfca3577f72058fa0797c1Bob Halley * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC")
5c144477062a5df657acee41a82051d38537fd38Tinderbox User * Copyright (C) 1999-2003 Internet Software Consortium.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews */
b897c52f865b2fc4e220e2110b874e59c716456bBob Halley
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User/* $Id$ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#include <config.h>
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#include <sys/types.h>
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#if defined(HAVE_SYS_SYSCTL_H)
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#if defined(HAVE_SYS_PARAM_H)
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence#include <sys/param.h>
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#endif
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#include <sys/sysctl.h>
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#endif
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#include <errno.h>
b897c52f865b2fc4e220e2110b874e59c716456bBob Halley#include <unistd.h>
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#include <isc/log.h>
c7b785510e3f517a0c98c0b6b6e6ad8f359e9e4cMark Andrews#include <isc/msgs.h>
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#include <isc/net.h>
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#include <isc/once.h>
b897c52f865b2fc4e220e2110b874e59c716456bBob Halley#include <isc/strerror.h>
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#include <isc/string.h>
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#include <isc/util.h>
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt/*%
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt * Definitions about UDP port range specification. This is a total mess of
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt * portability variants: some use sysctl (but the sysctl names vary), some use
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt * system-specific interfaces, some have the same interface for IPv4 and IPv6,
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt * some separate them, etc...
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt */
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt/*%
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt * The last resort defaults: use all non well known port space
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt */
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence#ifndef ISC_NET_PORTRANGELOW
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence#define ISC_NET_PORTRANGELOW 1024
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#endif /* ISC_NET_PORTRANGELOW */
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#ifndef ISC_NET_PORTRANGEHIGH
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#define ISC_NET_PORTRANGEHIGH 65535
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#endif /* ISC_NET_PORTRANGEHIGH */
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#ifdef HAVE_SYSCTLBYNAME
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence/*%
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley * sysctl variants
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrence */
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrence#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#define USE_SYSCTL_PORTRANGE
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrence#define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrence#define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
dabea86dac4c01f852b7aea728f73b4f55a89d44Mark Andrews#define SYSCTL_V6PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
dabea86dac4c01f852b7aea728f73b4f55a89d44Mark Andrews#define SYSCTL_V6PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
dabea86dac4c01f852b7aea728f73b4f55a89d44Mark Andrews#endif
12a3ab37fe6556406acdf92fc7c5f198d603ca2eMark Andrews
f5cfcbf2f7906fb59c2b8b9b8fc9c7a75ac44dabMark Andrews#ifdef __NetBSD__
2d46d268ccff30bb50e661b47c6496d23d9156c7Mark Andrews#define USE_SYSCTL_PORTRANGE
2d46d268ccff30bb50e661b47c6496d23d9156c7Mark Andrews#define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.anonportmin"
dabea86dac4c01f852b7aea728f73b4f55a89d44Mark Andrews#define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.anonportmax"
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#define SYSCTL_V6PORTRANGE_LOW "net.inet6.ip6.anonportmin"
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#define SYSCTL_V6PORTRANGE_HIGH "net.inet6.ip6.anonportmax"
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence#endif
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence#else /* !HAVE_SYSCTLBYNAME */
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews
2d9bd38ec3fbbfe920757b8972d94f664a9b354bMark Andrews#ifdef __OpenBSD__
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#define USE_SYSCTL_PORTRANGE
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence#define SYSCTL_V4PORTRANGE_LOW { CTL_NET, PF_INET, IPPROTO_IP, \
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews IPCTL_IPPORT_HIFIRSTAUTO }
8319af16557b81eba3277ee67215285f0823b587Mark Andrews#define SYSCTL_V4PORTRANGE_HIGH { CTL_NET, PF_INET, IPPROTO_IP, \
8319af16557b81eba3277ee67215285f0823b587Mark Andrews IPCTL_IPPORT_HILASTAUTO }
aee5e9cbacd8f88325840b8a498876f4319b0890Mark Andrews/* Same for IPv6 */
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#define SYSCTL_V6PORTRANGE_LOW SYSCTL_V4PORTRANGE_LOW
9e5854255178c04170bc98839282d4cf3fae7443Mark Andrews#define SYSCTL_V6PORTRANGE_HIGH SYSCTL_V4PORTRANGE_HIGH
f7a6d4f915ce622d988916397f313b33ae954afcMark Andrews#endif
f7a6d4f915ce622d988916397f313b33ae954afcMark Andrews
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#endif /* HAVE_SYSCTLBYNAME */
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews#if defined(ISC_PLATFORM_HAVEIPV6)
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence# if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrenceconst struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
c32b87bc54abacf95fb3b063d72b7d1855c1643bMichael Graff# endif
7f9f8c13c5e5e26e0ba2b82c0900d11ecf6269ceMark Andrews
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence# if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
c3e6fbe4b7471d843d015e3f1737b7edb9d0c547Mark Andrewsconst struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
c3e6fbe4b7471d843d015e3f1737b7edb9d0c547Mark Andrews# endif
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrence
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrence# if defined(WANT_IPV6)
12e63bfe1d111ccb57f482b28d56c785cccc7cf7David Lawrencestatic isc_once_t once_ipv6only = ISC_ONCE_INIT;
460b427411b72da26b1836b9424e2e70d65d9394David Lawrence# endif
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews# if defined(ISC_PLATFORM_HAVEIN6PKTINFO)
6376f6189950156cc1488c86f22b19dd4feec11cMark Andrewsstatic isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
5e47b4200ed81b8e18e165fe3a626d9992003db4Mark Andrews# endif
5e47b4200ed81b8e18e165fe3a626d9992003db4Mark Andrews#endif /* ISC_PLATFORM_HAVEIPV6 */
5e47b4200ed81b8e18e165fe3a626d9992003db4Mark Andrews
5e47b4200ed81b8e18e165fe3a626d9992003db4Mark Andrewsstatic isc_once_t once = ISC_ONCE_INIT;
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley
2320f230995995595438a9d9301d84931fd266ceMark Andrewsstatic isc_result_t ipv4_result = ISC_R_NOTFOUND;
c427260a8678f2e99a2337fb95ec98d9c9ee8c05Mark Andrewsstatic isc_result_t ipv6_result = ISC_R_NOTFOUND;
620a452ebe92fff63e85c5930a6e6dc8d9455918Mark Andrewsstatic isc_result_t unix_result = ISC_R_NOTFOUND;
620a452ebe92fff63e85c5930a6e6dc8d9455918Mark Andrewsstatic isc_result_t ipv6only_result = ISC_R_NOTFOUND;
6dcb47e37f9f0cdb94bdabc3fa157ff07983c590Mark Andrewsstatic isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
6dcb47e37f9f0cdb94bdabc3fa157ff07983c590Mark Andrews
43b3337ba58d70ca34f4d91e8c6c5e13a54af690Mark Andrewsstatic isc_result_t
43b3337ba58d70ca34f4d91e8c6c5e13a54af690Mark Andrewstry_proto(int domain) {
f10370fd44f05fecc808d89c01b2d50df2b232f3Mark Andrews int s;
f10370fd44f05fecc808d89c01b2d50df2b232f3Mark Andrews isc_result_t result = ISC_R_SUCCESS;
693d70f96fc2b3c1830580edcc29146afd6a9f61Mark Andrews char strbuf[ISC_STRERRORSIZE];
693d70f96fc2b3c1830580edcc29146afd6a9f61Mark Andrews
693d70f96fc2b3c1830580edcc29146afd6a9f61Mark Andrews s = socket(domain, SOCK_STREAM, 0);
693d70f96fc2b3c1830580edcc29146afd6a9f61Mark Andrews if (s == -1) {
d3347bd01baee56e476982a7443ba18cdbaeebe1Mark Andrews switch (errno) {
d3347bd01baee56e476982a7443ba18cdbaeebe1Mark Andrews#ifdef EAFNOSUPPORT
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley case EAFNOSUPPORT:
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#endif
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#ifdef EPROTONOSUPPORT
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt case EPROTONOSUPPORT:
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#endif
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#ifdef EINVAL
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley case EINVAL:
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley#endif
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley return (ISC_R_NOTFOUND);
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley default:
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews "socket() %s: %s",
0014d6342b0d50ae37126ac16d5bf821d02ffff7David Lawrence isc_msgcat_get(isc_msgcat,
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews ISC_MSGSET_GENERAL,
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews ISC_MSG_FAILED,
0014d6342b0d50ae37126ac16d5bf821d02ffff7David Lawrence "failed"),
a9558a6c63d9c6dbb2f3800b39ccb008652fcde3Mark Andrews strbuf);
a9558a6c63d9c6dbb2f3800b39ccb008652fcde3Mark Andrews return (ISC_R_UNEXPECTED);
a9558a6c63d9c6dbb2f3800b39ccb008652fcde3Mark Andrews }
c651f15b30f1dae5cc2f00878fb5da5b3a35a468Mark Andrews }
c651f15b30f1dae5cc2f00878fb5da5b3a35a468Mark Andrews
0014d6342b0d50ae37126ac16d5bf821d02ffff7David Lawrence#ifdef ISC_PLATFORM_HAVEIPV6
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#ifdef WANT_IPV6
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt if (domain == PF_INET6) {
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley struct sockaddr_in6 sin6;
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley unsigned int len;
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley /*
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley * Check to see if IPv6 is broken, as is common on Linux.
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt */
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley len = sizeof(sin6);
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley if (getsockname(s, (struct sockaddr *)&sin6, (void *)&len) < 0)
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley {
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley "retrieving the address of an IPv6 "
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley "socket from the kernel failed.");
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt "IPv6 is not supported.");
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt result = ISC_R_NOTFOUND;
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley } else {
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley if (len == sizeof(struct sockaddr_in6))
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley result = ISC_R_SUCCESS;
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt else {
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley isc_log_write(isc_lctx,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOGCATEGORY_GENERAL,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOGMODULE_SOCKET,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOG_ERROR,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley "IPv6 structures in kernel and "
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt "user space do not match.");
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley isc_log_write(isc_lctx,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOGCATEGORY_GENERAL,
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley ISC_LOGMODULE_SOCKET,
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt ISC_LOG_ERROR,
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt "IPv6 is not supported.");
18483fce5b9d1e02748bdcb916014cedea654f78Mark Andrews result = ISC_R_NOTFOUND;
af9dbf1ccdd53933aaae9300d13ce0965d39b067Evan Hunt }
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley }
0b72c791466d0807bcf22522b5ddb7da902c2720Bob Halley }
#endif
#endif
#endif
(void)close(s);
return (result);
}
static void
initialize_action(void) {
ipv4_result = try_proto(PF_INET);
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
ipv6_result = try_proto(PF_INET6);
#endif
#endif
#endif
#ifdef ISC_PLATFORM_HAVESYSUNH
unix_result = try_proto(PF_UNIX);
#endif
}
static void
initialize(void) {
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
}
isc_result_t
isc_net_probeipv4(void) {
initialize();
return (ipv4_result);
}
isc_result_t
isc_net_probeipv6(void) {
initialize();
return (ipv6_result);
}
isc_result_t
isc_net_probeunix(void) {
initialize();
return (unix_result);
}
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
static void
try_ipv6only(void) {
#ifdef IPV6_V6ONLY
int s, on;
char strbuf[ISC_STRERRORSIZE];
#endif
isc_result_t result;
result = isc_net_probeipv6();
if (result != ISC_R_SUCCESS) {
ipv6only_result = result;
return;
}
#ifndef IPV6_V6ONLY
ipv6only_result = ISC_R_NOTFOUND;
return;
#else
/* check for TCP sockets */
s = socket(PF_INET6, SOCK_STREAM, 0);
if (s == -1) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"socket() %s: %s",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_FAILED,
"failed"),
strbuf);
ipv6only_result = ISC_R_UNEXPECTED;
return;
}
on = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
ipv6only_result = ISC_R_NOTFOUND;
goto close;
}
close(s);
/* check for UDP sockets */
s = socket(PF_INET6, SOCK_DGRAM, 0);
if (s == -1) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"socket() %s: %s",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_FAILED,
"failed"),
strbuf);
ipv6only_result = ISC_R_UNEXPECTED;
return;
}
on = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
ipv6only_result = ISC_R_NOTFOUND;
goto close;
}
close(s);
ipv6only_result = ISC_R_SUCCESS;
close:
close(s);
return;
#endif /* IPV6_V6ONLY */
}
static void
initialize_ipv6only(void) {
RUNTIME_CHECK(isc_once_do(&once_ipv6only,
try_ipv6only) == ISC_R_SUCCESS);
}
#endif /* WANT_IPV6 */
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
static void
try_ipv6pktinfo(void) {
int s, on;
char strbuf[ISC_STRERRORSIZE];
isc_result_t result;
int optname;
result = isc_net_probeipv6();
if (result != ISC_R_SUCCESS) {
ipv6pktinfo_result = result;
return;
}
/* we only use this for UDP sockets */
s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"socket() %s: %s",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_FAILED,
"failed"),
strbuf);
ipv6pktinfo_result = ISC_R_UNEXPECTED;
return;
}
#ifdef IPV6_RECVPKTINFO
optname = IPV6_RECVPKTINFO;
#else
optname = IPV6_PKTINFO;
#endif
on = 1;
if (setsockopt(s, IPPROTO_IPV6, optname, &on, sizeof(on)) < 0) {
ipv6pktinfo_result = ISC_R_NOTFOUND;
goto close;
}
close(s);
ipv6pktinfo_result = ISC_R_SUCCESS;
close:
close(s);
return;
}
static void
initialize_ipv6pktinfo(void) {
RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
try_ipv6pktinfo) == ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
#endif /* ISC_PLATFORM_HAVEIPV6 */
isc_result_t
isc_net_probe_ipv6only(void) {
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
initialize_ipv6only();
#else
ipv6only_result = ISC_R_NOTFOUND;
#endif
#endif
return (ipv6only_result);
}
isc_result_t
isc_net_probe_ipv6pktinfo(void) {
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
#ifdef WANT_IPV6
initialize_ipv6pktinfo();
#else
ipv6pktinfo_result = ISC_R_NOTFOUND;
#endif
#endif
#endif
return (ipv6pktinfo_result);
}
#if defined(USE_SYSCTL_PORTRANGE)
#if defined(HAVE_SYSCTLBYNAME)
static isc_result_t
getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
int port_low, port_high;
size_t portlen;
const char *sysctlname_lowport, *sysctlname_hiport;
if (af == AF_INET) {
sysctlname_lowport = SYSCTL_V4PORTRANGE_LOW;
sysctlname_hiport = SYSCTL_V4PORTRANGE_HIGH;
} else {
sysctlname_lowport = SYSCTL_V6PORTRANGE_LOW;
sysctlname_hiport = SYSCTL_V6PORTRANGE_HIGH;
}
portlen = sizeof(portlen);
if (sysctlbyname(sysctlname_lowport, &port_low, &portlen,
NULL, 0) < 0) {
return (ISC_R_FAILURE);
}
portlen = sizeof(portlen);
if (sysctlbyname(sysctlname_hiport, &port_high, &portlen,
NULL, 0) < 0) {
return (ISC_R_FAILURE);
}
if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
return (ISC_R_RANGE);
*low = (in_port_t)port_low;
*high = (in_port_t)port_high;
return (ISC_R_SUCCESS);
}
#else /* !HAVE_SYSCTLBYNAME */
static isc_result_t
getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
int mib_lo4[4] = SYSCTL_V4PORTRANGE_LOW;
int mib_hi4[4] = SYSCTL_V4PORTRANGE_HIGH;
int mib_lo6[4] = SYSCTL_V6PORTRANGE_LOW;
int mib_hi6[4] = SYSCTL_V6PORTRANGE_HIGH;
int *mib_lo, *mib_hi, miblen;
int port_low, port_high;
size_t portlen;
if (af == AF_INET) {
mib_lo = mib_lo4;
mib_hi = mib_hi4;
miblen = sizeof(mib_lo4) / sizeof(mib_lo4[0]);
} else {
mib_lo = mib_lo6;
mib_hi = mib_hi6;
miblen = sizeof(mib_lo6) / sizeof(mib_lo6[0]);
}
portlen = sizeof(portlen);
if (sysctl(mib_lo, miblen, &port_low, &portlen, NULL, 0) < 0) {
return (ISC_R_FAILURE);
}
portlen = sizeof(portlen);
if (sysctl(mib_hi, miblen, &port_high, &portlen, NULL, 0) < 0) {
return (ISC_R_FAILURE);
}
if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
return (ISC_R_RANGE);
*low = (in_port_t) port_low;
*high = (in_port_t) port_high;
return (ISC_R_SUCCESS);
}
#endif /* HAVE_SYSCTLBYNAME */
#endif /* USE_SYSCTL_PORTRANGE */
isc_result_t
isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
int result = ISC_R_FAILURE;
REQUIRE(low != NULL && high != NULL);
#if defined(USE_SYSCTL_PORTRANGE)
result = getudpportrange_sysctl(af, low, high);
#else
UNUSED(af);
#endif
if (result != ISC_R_SUCCESS) {
*low = ISC_NET_PORTRANGELOW;
*high = ISC_NET_PORTRANGEHIGH;
}
return (ISC_R_SUCCESS); /* we currently never fail in this function */
}
void
isc_net_disableipv4(void) {
initialize();
if (ipv4_result == ISC_R_SUCCESS)
ipv4_result = ISC_R_DISABLED;
}
void
isc_net_disableipv6(void) {
initialize();
if (ipv6_result == ISC_R_SUCCESS)
ipv6_result = ISC_R_DISABLED;
}
void
isc_net_enableipv4(void) {
initialize();
if (ipv4_result == ISC_R_DISABLED)
ipv4_result = ISC_R_SUCCESS;
}
void
isc_net_enableipv6(void) {
initialize();
if (ipv6_result == ISC_R_DISABLED)
ipv6_result = ISC_R_SUCCESS;
}