net.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 1999-2005, 2007, 2008, 2012-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id$ */
#include <config.h>
#if defined(HAVE_SYS_SYSCTL_H)
#if defined(HAVE_SYS_PARAM_H)
#endif
#endif
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <isc/strerror.h>
#ifndef ISC_SOCKADDR_LEN_T
#define ISC_SOCKADDR_LEN_T unsigned int
#endif
/*%
* Definitions about UDP port range specification. This is a total mess of
* portability variants: some use sysctl (but the sysctl names vary), some use
* system-specific interfaces, some have the same interface for IPv4 and IPv6,
* some separate them, etc...
*/
/*%
* The last resort defaults: use all non well known port space
*/
#ifndef ISC_NET_PORTRANGELOW
#define ISC_NET_PORTRANGELOW 1024
#endif /* ISC_NET_PORTRANGELOW */
#ifndef ISC_NET_PORTRANGEHIGH
#define ISC_NET_PORTRANGEHIGH 65535
#endif /* ISC_NET_PORTRANGEHIGH */
#ifdef HAVE_SYSCTLBYNAME
/*%
* sysctl variants
*/
#define USE_SYSCTL_PORTRANGE
#define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
#define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
#define SYSCTL_V6PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
#define SYSCTL_V6PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
#endif
#ifdef __NetBSD__
#define USE_SYSCTL_PORTRANGE
#define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.anonportmin"
#define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.anonportmax"
#define SYSCTL_V6PORTRANGE_LOW "net.inet6.ip6.anonportmin"
#define SYSCTL_V6PORTRANGE_HIGH "net.inet6.ip6.anonportmax"
#endif
#else /* !HAVE_SYSCTLBYNAME */
#ifdef __OpenBSD__
#define USE_SYSCTL_PORTRANGE
/* Same for IPv6 */
#endif
#endif /* HAVE_SYSCTLBYNAME */
#if defined(ISC_PLATFORM_HAVEIPV6)
# if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
# endif
# if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
# endif
# if defined(WANT_IPV6)
# if defined(ISC_PLATFORM_HAVEIN6PKTINFO)
# endif
# endif /* WANT_IPV6 */
#endif /* ISC_PLATFORM_HAVEIPV6 */
#ifndef ISC_CMSG_IP_TOS
#ifdef __APPLE__
#define ISC_CMSG_IP_TOS 0 /* As of 10.8.2. */
#else /* ! __APPLE__ */
#define ISC_CMSG_IP_TOS 1
#endif /* ! __APPLE__ */
#endif /* ! ISC_CMSG_IP_TOS */
static unsigned int dscp_result = 0;
static isc_result_t
int s;
char strbuf[ISC_STRERRORSIZE];
if (s == -1) {
switch (errno) {
#ifdef EAFNOSUPPORT
case EAFNOSUPPORT:
#endif
#ifdef EPROTONOSUPPORT
case EPROTONOSUPPORT:
#endif
#ifdef EINVAL
case EINVAL:
#endif
return (ISC_R_NOTFOUND);
default:
"socket() %s: %s",
"failed"),
strbuf);
return (ISC_R_UNEXPECTED);
}
}
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
struct sockaddr_in6 sin6;
unsigned int len;
/*
* Check to see if IPv6 is broken, as is common on Linux.
*/
{
"retrieving the address of an IPv6 "
"socket from the kernel failed.");
"IPv6 is not supported.");
} else {
if (len == sizeof(struct sockaddr_in6))
else {
"IPv6 structures in kernel and "
"user space do not match.");
"IPv6 is not supported.");
}
}
}
#endif
#endif
#endif
(void)close(s);
return (result);
}
static void
initialize_action(void) {
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
#endif
#endif
#endif
#ifdef ISC_PLATFORM_HAVESYSUNH
#endif
}
static void
initialize(void) {
}
isc_net_probeipv4(void) {
initialize();
return (ipv4_result);
}
isc_net_probeipv6(void) {
initialize();
return (ipv6_result);
}
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
result = isc_net_probeipv6();
if (result != ISC_R_SUCCESS) {
return;
}
#ifndef IPV6_V6ONLY
return;
#else
/* check for TCP sockets */
if (s == -1) {
"socket() %s: %s",
"failed"),
strbuf);
return;
}
on = 1;
goto close;
}
close(s);
/* check for UDP sockets */
if (s == -1) {
"socket() %s: %s",
"failed"),
strbuf);
return;
}
on = 1;
goto close;
}
close(s);
return;
#endif /* IPV6_V6ONLY */
}
static void
initialize_ipv6only(void) {
try_ipv6only) == ISC_R_SUCCESS);
}
#endif /* WANT_IPV6 */
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
#ifdef WANT_IPV6
static void
try_ipv6pktinfo(void) {
int s, on;
char strbuf[ISC_STRERRORSIZE];
int optname;
result = isc_net_probeipv6();
if (result != ISC_R_SUCCESS) {
return;
}
/* we only use this for UDP sockets */
if (s == -1) {
"socket() %s: %s",
"failed"),
strbuf);
return;
}
#ifdef IPV6_RECVPKTINFO
#else
#endif
on = 1;
goto close;
}
close(s);
return;
}
static void
initialize_ipv6pktinfo(void) {
try_ipv6pktinfo) == ISC_R_SUCCESS);
}
#endif /* WANT_IPV6 */
#endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
#endif /* ISC_PLATFORM_HAVEIPV6 */
isc_net_probe_ipv6only(void) {
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
#else
#endif
#endif
return (ipv6only_result);
}
isc_net_probe_ipv6pktinfo(void) {
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
#ifdef WANT_IPV6
#else
#endif
#endif
#endif
return (ipv6pktinfo_result);
}
#if ISC_CMSG_IP_TOS || \
static inline ISC_SOCKADDR_LEN_T
#ifdef CMSG_LEN
#else
/*
* Cast NULL so that any pointer arithmetic performed by CMSG_DATA
* is correct.
*/
#endif
}
static inline ISC_SOCKADDR_LEN_T
#ifdef CMSG_SPACE
return (CMSG_SPACE(len));
#else
/*
* XXX: The buffer length is an ad-hoc value, but should be enough
* in a practical sense.
*/
else
return (0);
#endif
}
#ifdef ISC_NET_BSD44MSGHDR
/*
* Make a fd non-blocking.
*/
static isc_result_t
make_nonblock(int fd) {
int ret;
int flags;
char strbuf[ISC_STRERRORSIZE];
#ifdef USE_FIONBIO_IOCTL
int on = 1;
#else
flags |= PORT_NONBLOCK;
#endif
if (ret == -1) {
#ifdef USE_FIONBIO_IOCTL
"ioctl(%d, FIONBIO, &on): %s", fd,
#else
#endif
strbuf);
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
static isc_boolean_t
char strbuf[ISC_STRERRORSIZE];
struct sockaddr_storage ss;
union {
struct cmsghdr h;
unsigned char b[256];
} control;
int dscp = 46;
char buf[1] = { 0 };
"bind: %s", strbuf);
return (ISC_FALSE);
}
"getsockname: %s", strbuf);
return (ISC_FALSE);
}
msg.msg_controllen = 0;
switch (type) {
#ifdef IP_TOS
case IP_TOS:
break;
#endif
#ifdef IPV6_TCLASS
case IPV6_TCLASS:
break;
#endif
default:
INSIST(0);
}
switch (errno) {
#ifdef ENOPROTOOPT
case ENOPROTOOPT:
#endif
#ifdef EOPNOTSUPP
case EOPNOTSUPP:
#endif
case EINVAL:
break;
default:
}
if (debug != ISC_LOG_NOTICE) {
"sendmsg: %s", strbuf);
} else {
"sendmsg() %s: %s",
"failed"),
strbuf);
}
return (ISC_FALSE);
}
/*
* Make sure the message actually got sent.
*/
result = make_nonblock(s);
msg.msg_controllen = 0;
return (ISC_FALSE);
return (ISC_TRUE);
}
#endif
#endif
static void
try_dscp_v4(void) {
#ifdef IP_TOS
char strbuf[ISC_STRERRORSIZE];
int s, dscp = 0, n;
#ifdef IP_RECVTOS
int on = 1;
#endif /* IP_RECVTOS */
#ifdef AI_NUMERICHOST
#else
#endif
"getaddrinfo(127.0.0.1): %s", gai_strerror(n));
return;
}
if (s == -1) {
"socket: %s", strbuf);
return;
}
#ifdef IP_RECVTOS
on = 1;
#endif /* IP_RECVTOS */
#ifdef ISC_NET_BSD44MSGHDR
#if ISC_CMSG_IP_TOS
#endif /* ISC_CMSG_IP_TOS */
#endif /* ISC_NET_BSD44MSGHDR */
close(s);
#endif /* IP_TOS */
}
static void
try_dscp_v6(void) {
#ifdef ISC_PLATFORM_HAVEIPV6
#ifdef WANT_IPV6
#ifdef IPV6_TCLASS
char strbuf[ISC_STRERRORSIZE];
int s, dscp = 0, n;
#if defined(IPV6_RECVTCLASS)
int on = 1;
#endif /* IPV6_RECVTCLASS */
#ifdef AI_NUMERICHOST
#else
#endif
"getaddrinfo(::1): %s", gai_strerror(n));
return;
}
if (s == -1) {
"socket: %s", strbuf);
return;
}
#ifdef IPV6_RECVTCLASS
on = 1;
#endif /* IPV6_RECVTCLASS */
#ifdef ISC_NET_BSD44MSGHDR
#endif /* ISC_NET_BSD44MSGHDR */
close(s);
#endif /* IPV6_TCLASS */
#endif /* WANT_IPV6 */
#endif /* ISC_PLATFORM_HAVEIPV6 */
}
static void
try_dscp(void) {
try_dscp_v4();
try_dscp_v6();
}
static void
initialize_dscp(void) {
}
unsigned int
isc_net_probedscp(void) {
return (dscp_result);
}
#if defined(USE_SYSCTL_PORTRANGE)
#if defined(HAVE_SYSCTLBYNAME)
static isc_result_t
const char *sysctlname_lowport, *sysctlname_hiport;
} else {
}
NULL, 0) < 0) {
return (ISC_R_FAILURE);
}
NULL, 0) < 0) {
return (ISC_R_FAILURE);
}
return (ISC_R_RANGE);
return (ISC_R_SUCCESS);
}
#else /* !HAVE_SYSCTLBYNAME */
static isc_result_t
} else {
}
return (ISC_R_FAILURE);
}
return (ISC_R_FAILURE);
}
return (ISC_R_RANGE);
return (ISC_R_SUCCESS);
}
#endif /* HAVE_SYSCTLBYNAME */
#endif /* USE_SYSCTL_PORTRANGE */
int result = ISC_R_FAILURE;
#if !defined(USE_SYSCTL_PORTRANGE) && defined(__linux)
#endif
#if defined(USE_SYSCTL_PORTRANGE)
/*
* Linux local ports are address family agnostic.
*/
int n;
unsigned int l, h;
if (n == 2 && (l & ~0xffff) == 0 && (h & ~0xffff) == 0) {
*low = l;
*high = h;
}
}
#else
#endif
if (result != ISC_R_SUCCESS) {
}
return (ISC_R_SUCCESS); /* we currently never fail in this function */
}
void
isc_net_disableipv4(void) {
initialize();
if (ipv4_result == ISC_R_SUCCESS)
}
void
isc_net_disableipv6(void) {
initialize();
if (ipv6_result == ISC_R_SUCCESS)
}
void
isc_net_enableipv4(void) {
initialize();
if (ipv4_result == ISC_R_DISABLED)
}
void
isc_net_enableipv6(void) {
initialize();
if (ipv6_result == ISC_R_DISABLED)
}