ifiter_ioctl.c revision 91c49d591c79159a965adb9d78c03cc2f334991e
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff/*
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Copyright (C) 1999 Internet Software Consortium.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff *
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Permission to use, copy, modify, and distribute this software for any
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * purpose with or without fee is hereby granted, provided that the above
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * copyright notice and this permission notice appear in all copies.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff *
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * SOFTWARE.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
3c9b0570ed1d506bb0dc02fa5358594b0b57c2b1David Lawrence/*
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Obtain the list of network interfaces using the SIOCGIFCONF ioctl.
ae302c18c296f31c02efb613887f206894caa749David Lawrence * See netintro(4).
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
3c9b0570ed1d506bb0dc02fa5358594b0b57c2b1David Lawrence#define IFITER_MAGIC 0x49464954U /* IFIT. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define VALID_IFITER(t) ((t) != NULL && (t)->magic == IFITER_MAGIC)
ae302c18c296f31c02efb613887f206894caa749David Lawrence
ae302c18c296f31c02efb613887f206894caa749David Lawrencestruct isc_interfaceiter {
ae302c18c296f31c02efb613887f206894caa749David Lawrence unsigned int magic; /* Magic number. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_mem_t *mctx;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff int socket;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff struct ifconf ifc;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff void *buf; /* Buffer for sysctl data. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff unsigned int bufsize; /* Bytes allocated. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff unsigned int pos; /* Current offset in
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff SIOCGIFCONF data */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_interface_t current; /* Current interface data. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_result_t result; /* Last result code. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff};
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff/*
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Size of buffer for SIOCGIFCONF, in bytes. We assume no sane system
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * will have more than a megabyte of interface configuration data.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define IFCONF_BUFSIZE_INITIAL 4096
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define IFCONF_BUFSIZE_MAX 1048576
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graffisc_result_t
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graffisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp)
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff{
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_interfaceiter_t *iter;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_result_t result;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff REQUIRE(mctx != NULL);
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff REQUIRE(iterp != NULL);
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff REQUIRE(*iterp == NULL);
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff iter = isc_mem_get(mctx, sizeof(*iter));
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff if (iter == NULL)
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff return (ISC_R_NOMEMORY);
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff iter->mctx = mctx;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff iter->buf = NULL;
4e21e54a0394d0f87798620a95c1f675c0b0d09cMichael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff /* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff "making interface scan socket: %s",
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff strerror(errno));
4e21e54a0394d0f87798620a95c1f675c0b0d09cMichael Graff result = ISC_R_UNEXPECTED;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff goto socket_failure;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff }
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff /*
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Get the interface configuration, allocating more memory if
* necessary.
*/
iter->bufsize = IFCONF_BUFSIZE_INITIAL;
for (;;) {
iter->buf = isc_mem_get(mctx, iter->bufsize);
if (iter->buf == NULL) {
result = ISC_R_NOMEMORY;
goto alloc_failure;
}
iter->ifc.ifc_len = iter->bufsize;
iter->ifc.ifc_buf = iter->buf;
if (ioctl(iter->socket, SIOCGIFCONF, (char *) &iter->ifc) < 0) {
if (errno != EINVAL) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"get interface configuration: %s",
strerror(errno));
result = ISC_R_UNEXPECTED;
goto ioctl_failure;
}
/* 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.
*/
if (iter->ifc.ifc_len + 2 * sizeof(struct ifreq)
< iter->bufsize)
break;
}
if (iter->bufsize >= IFCONF_BUFSIZE_MAX) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"get interface configuration: "
"maximum buffer size exceeded");
result = ISC_R_UNEXPECTED;
goto ioctl_failure;
}
isc_mem_put(mctx, iter->buf, iter->bufsize);
iter->bufsize *= 2;
}
/*
* A newly created iterator has an undefined position
* until isc_interfaceiter_first() is called.
*/
iter->pos = (unsigned int) -1;
iter->result = ISC_R_FAILURE;
iter->magic = IFITER_MAGIC;
*iterp = iter;
return (ISC_R_SUCCESS);
ioctl_failure:
isc_mem_put(mctx, iter->buf, iter->bufsize);
alloc_failure:
(void) close(iter->socket);
socket_failure:
isc_mem_put(mctx, iter, sizeof *iter);
return (result);
}
/*
* Get information about the current interface to iter->current.
* If successful, return ISC_R_SUCCESS.
* If the interface has an unsupported address family,
* return ISC_R_FAILURE. In case of other failure,
* return ISC_R_UNEXPECTED.
*/
static isc_result_t
internal_current(isc_interfaceiter_t *iter) {
struct ifreq *ifrp;
struct ifreq ifreq;
int family;
REQUIRE(VALID_IFITER(iter));
REQUIRE (iter->pos < (unsigned int) iter->ifc.ifc_len);
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
memcpy(&ifreq, ifrp, sizeof ifreq);
family = ifreq.ifr_addr.sa_family;
if (family != AF_INET) /* XXX IPv6 */
return (ISC_R_FAILURE);
memset(&iter->current, 0, sizeof(iter->current));
INSIST(sizeof(ifreq.ifr_name) <= sizeof(iter->current.name));
memcpy(iter->current.name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
get_addr(family, &iter->current.address, &ifreq.ifr_addr);
/* Get interface flags. */
iter->current.flags = 0;
if (ioctl(iter->socket, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface flags: %s",
ifreq.ifr_name,
strerror(errno));
return (ISC_R_UNEXPECTED);
}
if ((ifreq.ifr_flags & IFF_UP) != 0)
iter->current.flags |= INTERFACE_F_UP;
if ((ifreq.ifr_flags & IFF_POINTOPOINT) != 0)
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
if ((ifreq.ifr_flags & IFF_LOOPBACK) != 0)
iter->current.flags |= INTERFACE_F_LOOPBACK;
/* If the interface is point-to-point, get the destination address. */
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
if (ioctl(iter->socket, SIOCGIFDSTADDR, (char *) &ifreq) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting destination address: %s",
ifreq.ifr_name,
strerror(errno));
return (ISC_R_UNEXPECTED);
}
get_addr(family, &iter->current.dstaddress,
&ifreq.ifr_dstaddr);
}
/* Get the network mask. */
if (ioctl(iter->socket, SIOCGIFNETMASK, (char *) &ifreq) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting netmask: %s",
ifreq.ifr_name,
strerror(errno));
return (ISC_R_UNEXPECTED);
}
get_addr(family, &iter->current.netmask,
&ifreq.ifr_addr);
return (ISC_R_SUCCESS);
}
/*
* 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
internal_next(isc_interfaceiter_t *iter) {
struct ifreq *ifrp;
REQUIRE (iter->pos < (unsigned int) iter->ifc.ifc_len);
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
#ifdef ISC_PLATFORM_HAVESALEN
if (ifrp->ifr_addr.sa_len > sizeof(struct sockaddr))
iter->pos += sizeof(ifrp->ifr_name) + ifrp->ifr_addr.sa_len;
else
#endif
iter->pos += sizeof *ifrp;
if (iter->pos >= (unsigned int) iter->ifc.ifc_len)
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
static void
internal_destroy(isc_interfaceiter_t *iter) {
(void) close(iter->socket);
}