ifiter_ioctl.c revision 71839e2a424a904a60429a380ee7685464022e4b
/*
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: ifiter_ioctl.c,v 1.46 2004/05/06 03:16:07 marka Exp $ */
/*
* Obtain the list of network interfaces using the SIOCGLIFCONF ioctl.
* See netintro(4).
*/
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
#define LIFCONF if_laddrconf
#else
#define ISC_HAVE_LIFC_FAMILY 1
#define ISC_HAVE_LIFC_FLAGS 1
#endif
#ifdef ISC_PLATFORM_HAVEIF_LADDRREQ
#define lifr_dstaddr iflr_dstaddr
#define lifr_flags iflr_flags
#define LIFREQ if_laddrreq
#else
#endif
#endif
#define ISC_IF_INET6_SZ \
sizeof("00000000000000000000000000000001 01 80 10 80 lo\n")
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
int mode;
int socket;
void *buf; /* Buffer for sysctl data. */
unsigned int bufsize; /* Bytes allocated. */
unsigned int pos; /* Current offset in
SIOCGIFCONF data */
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
int socket6;
void *buf6; /* Buffer for sysctl data. */
unsigned int bufsize6; /* Bytes allocated. */
unsigned int pos6; /* Current offset in
SIOCGLIFCONF data */
#endif
#ifdef HAVE_TRUCLUSTER
int clua_context; /* Cluster alias context */
#endif
#ifdef __linux
char entry[ISC_IF_INET6_SZ];
#endif
};
#ifdef HAVE_TRUCLUSTER
#endif
/*
* Size of buffer for SIOCGLIFCONF, in bytes. We assume no sane system
* will have more than a megabyte of interface configuration data.
*/
#define IFCONF_BUFSIZE_INITIAL 4096
#define IFCONF_BUFSIZE_MAX 1048576
static isc_result_t
char strbuf[ISC_STRERRORSIZE];
for (;;) {
return (ISC_R_NOMEMORY);
/*
* conversion". It comes from its own macro definition,
* and is really hard to shut up.
*/
== -1) {
"get interface "
"configuration: %s"),
strbuf);
goto unexpected;
}
/*
* EINVAL. Retry with a bigger buffer.
*/
} else {
/*
* The ioctl succeeded.
* Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small
* to fit all the interfaces in. If
* ifc.lifc_len is too near to the end of the
* buffer we will grow it just in case and
* retry.
*/
break;
}
"get interface "
"configuration: "
"maximum buffer "
"size exceeded"));
goto unexpected;
}
}
return (ISC_R_SUCCESS);
return (ISC_R_UNEXPECTED);
}
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
static isc_result_t
char strbuf[ISC_STRERRORSIZE];
for (;;) {
return (ISC_R_NOMEMORY);
#ifdef ISC_HAVE_LIFC_FAMILY
#endif
#ifdef ISC_HAVE_LIFC_FLAGS
#endif
/*
* conversion". It comes from its own macro definition,
* and is really hard to shut up.
*/
== -1) {
#ifdef __hpux
/*
* IPv6 interface scanning is not available on all
* kernels w/ IPv6 sockets.
*/
ISC_LOG_DEBUG(1),
"get interface "
"configuration: %s"),
strbuf);
goto cleanup;
}
#endif
"get interface "
"configuration: %s"),
strbuf);
goto cleanup;
}
/*
* EINVAL. Retry with a bigger buffer.
*/
} else {
/*
* The ioctl succeeded.
* Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small
* to fit all the interfaces in. If
* ifc.ifc_len is too near to the end of the
* buffer we will grow it just in case and
* retry.
*/
break;
}
"get interface "
"configuration: "
"maximum buffer "
"size exceeded"));
goto cleanup;
}
}
return (ISC_R_SUCCESS);
return (result);
}
#endif
char strbuf[ISC_STRERRORSIZE];
return (ISC_R_NOMEMORY);
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
#endif
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
result = isc_net_probeipv6();
if (result == ISC_R_SUCCESS) {
/*
* Create an unbound datagram socket to do the SIOCGLIFCONF
* SIOCGLIFCONF to get IPv6 addresses.
*/
"making interface "
"scan socket: %s"),
strbuf);
goto socket6_failure;
}
goto ioctl6_failure;
}
#endif
"making interface "
"scan socket: %s"),
strbuf);
goto socket_failure;
}
if (result != ISC_R_SUCCESS)
goto ioctl_failure;
/*
* A newly created iterator has an undefined position
* until isc_interfaceiter_first() is called.
*/
#ifdef HAVE_TRUCLUSTER
#endif
#ifdef __linux
#endif
return (ISC_R_SUCCESS);
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
#endif
return (result);
}
#ifdef HAVE_TRUCLUSTER
static void
}
static isc_result_t
return (ISC_R_IGNORE);
return (ISC_R_SUCCESS);
}
#endif
#ifdef __linux
static isc_result_t
else
}
static void
(void)linux_if_inet6_next(iter);
} else
}
static isc_result_t
char address[33];
int res;
unsigned int i;
return (ISC_R_FAILURE);
if (res != 6)
return (ISC_R_FAILURE);
return (ISC_R_FAILURE);
for (i = 0; i < 16; i++) {
unsigned char byte;
static const char hex[] = "0123456789abcdef";
}
}
for (i = 0; i < 16; i++) {
if (prefix > 8) {
prefix -= 8;
} else {
prefix = 0;
}
}
return (ISC_R_SUCCESS);
}
#endif
/*
* Get information about the current interface to iter->current.
* If successful, return ISC_R_SUCCESS.
* If the interface has an unsupported address family, or if
* some operation on it fails, return ISC_R_IGNORE to make
* the higher-level iterator code ignore it.
*/
static isc_result_t
int family;
char strbuf[ISC_STRERRORSIZE];
#if !defined(ISC_PLATFORM_HAVEIF_LADDRREQ) && defined(SIOCGLIFADDR)
#else
char sabuf[256];
#endif
#ifdef __linux
#endif
#ifdef __linux
if (result != ISC_R_NOMORE)
return (result);
#endif
#if defined(ISC_PLATFORM_HAVEIPV6)
#else
#endif
return (ISC_R_IGNORE);
/*
* If the interface does not have a address ignore it.
*/
switch (family) {
case AF_INET:
return (ISC_R_IGNORE);
break;
case AF_INET6:
sizeof(in6addr_any)) == 0)
return (ISC_R_IGNORE);
break;
}
/*
* Get interface flags.
*/
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
"%s: getting interface flags: %s",
return (ISC_R_IGNORE);
}
#ifdef IFF_POINTOPOINT
#endif
goto inet;
#if !defined(ISC_PLATFORM_HAVEIF_LADDRREQ) && defined(SIOCGLIFADDR)
"%s: getting interface address: %s",
return (ISC_R_IGNORE);
}
#else
"prefix length for %s is unknown "
"(assume 128)"), sabuf);
prefixlen = 128;
#endif
/*
* Netmask already zeroed.
*/
for (i = 0; i < 16; i++) {
if (prefixlen > 8) {
bits = 0;
prefixlen -= 8;
} else {
prefixlen = 0;
}
}
return (ISC_R_SUCCESS);
inet:
return (ISC_R_IGNORE);
#ifdef IFF_POINTOPOINT
/*
* If the interface is point-to-point, get the destination address.
*/
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
< 0) {
"%s: getting "
"destination address: %s"),
return (ISC_R_IGNORE);
}
}
#endif
/*
* Get the network mask.
*/
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
"%s: getting netmask: %s"),
return (ISC_R_IGNORE);
}
return (ISC_R_SUCCESS);
}
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
static isc_result_t
int family;
char strbuf[ISC_STRERRORSIZE];
int fd;
#ifdef ISC_PLATFORM_HAVEIPV6
#else
#endif
return (ISC_R_IGNORE);
/*
* If the interface does not have a address ignore it.
*/
switch (family) {
case AF_INET:
return (ISC_R_IGNORE);
break;
case AF_INET6:
sizeof(in6addr_any)) == 0)
return (ISC_R_IGNORE);
break;
}
/*
* Get interface flags.
*/
else
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
"%s: getting interface flags: %s",
return (ISC_R_IGNORE);
}
#ifdef IFF_POINTOPOINT
#endif
#ifdef IFF_POINTOPOINT
/*
* If the interface is point-to-point, get the destination address.
*/
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
< 0) {
"%s: getting "
"destination address: %s"),
return (ISC_R_IGNORE);
}
}
#endif
/*
* Get the network mask. Netmask already zeroed.
*/
#ifdef lifr_addrlen
/*
* Special case: if the system provides lifr_addrlen member, the
* netmask of an IPv6 address can be derived from the length, since
* an IPv6 address always has a contiguous mask.
*/
int i, bits;
(~0 << bits) & 0xff;
}
return (ISC_R_SUCCESS);
}
#endif
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
"%s: getting netmask: %s"),
return (ISC_R_IGNORE);
}
return (ISC_R_SUCCESS);
}
#endif
static isc_result_t
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
}
#endif
#ifdef HAVE_TRUCLUSTER
return(internal_current_clusteralias(iter));
#endif
return (internal_current4(iter));
}
/*
* Step the iterator to the next interface. Unlike
* isc_interfaceiter_next(), this may leave the iterator
* positioned on an interface that will ultimately
* be ignored. Return ISC_R_NOMORE if there are no more
* interfaces, otherwise ISC_R_SUCCESS.
*/
static isc_result_t
#ifdef __linux
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);
#endif
#ifdef ISC_PLATFORM_HAVESALEN
else
#endif
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
static isc_result_t
#ifdef ISC_PLATFORM_HAVESALEN
else
#endif
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
#endif
static isc_result_t
#ifdef HAVE_TRUCLUSTER
int clua_result;
#endif
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
return (ISC_R_SUCCESS);
}
}
#endif
#ifdef HAVE_TRUCLUSTER
&iter->clua_context);
if (clua_result != CLUA_SUCCESS)
return (ISC_R_SUCCESS);
}
#endif
return (internal_next4(iter));
}
static void
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
}
#endif
#ifdef __linux
#endif
}
static
#ifdef HAVE_TRUCLUSTER
int clua_result;
#endif
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
#endif
#ifdef HAVE_TRUCLUSTER
iter->clua_context = 0;
&iter->clua_context);
#endif
#ifdef __linux
#endif
}