ifiter_ioctl.c revision a134177ed9f82189504191d90f3ed9e97c2b47cb
/*
* Copyright (C) 1999-2009, 2014-2017 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: ifiter_ioctl.c,v 1.62 2009/01/18 23:48:14 tbox Exp $ */
/*! \file
* \brief
* 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
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
#ifdef __linux
#ifndef IF_NAMESIZE
# ifdef IFNAMSIZ
# define IF_NAMESIZE IFNAMSIZ
# else
# define IF_NAMESIZE 16
# endif
#endif
#endif
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
/*
* 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
return (linux_if_inet6_current(iter));
#else
return (ISC_R_NOMORE);
#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 ISC_PLATFORM_HAVESALEN
#endif
#ifdef ISC_PLATFORM_HAVESALEN
else
#endif
} else {
#ifdef __linux
return (linux_if_inet6_next(iter));
#else
return (ISC_R_NOMORE);
#endif
}
return (ISC_R_SUCCESS);
}
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
static isc_result_t
#ifdef ISC_PLATFORM_HAVESALEN
#endif
#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
}