1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
|
/*
* Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and/or 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.60.120.2 2009/01/18 23:47:41 tbox Exp $ */
/*! \file
* \brief
* Obtain the list of network interfaces using the SIOCGLIFCONF ioctl.
* See netintro(4).
*/
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
#ifdef ISC_PLATFORM_HAVEIF_LADDRCONF
#define lifc_len iflc_len
#define lifc_buf iflc_buf
#define lifc_req iflc_req
#define LIFCONF if_laddrconf
#else
#define ISC_HAVE_LIFC_FAMILY 1
#define ISC_HAVE_LIFC_FLAGS 1
#define LIFCONF lifconf
#endif
#ifdef ISC_PLATFORM_HAVEIF_LADDRREQ
#define lifr_addr iflr_addr
#define lifr_name iflr_name
#define lifr_dstaddr iflr_dstaddr
#define lifr_broadaddr iflr_broadaddr
#define lifr_flags iflr_flags
#define lifr_index iflr_index
#define ss_family sa_family
#define LIFREQ if_laddrreq
#else
#define LIFREQ lifreq
#endif
#endif
#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'T')
#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
isc_mem_t *mctx;
int mode;
int socket;
struct ifconf ifc;
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;
struct LIFCONF lifc;
void *buf6; /* Buffer for sysctl data. */
unsigned int bufsize6; /* Bytes allocated. */
unsigned int pos6; /* Current offset in
SIOCGLIFCONF data */
isc_result_t result6; /* Last result code. */
isc_boolean_t first6;
#endif
#ifdef HAVE_TRUCLUSTER
int clua_context; /* Cluster alias context */
isc_boolean_t clua_done;
struct sockaddr clua_sa;
#endif
#ifdef __linux
FILE * proc;
char entry[ISC_IF_INET6_SZ];
isc_result_t valid;
#endif
isc_interface_t current; /* Current interface data. */
isc_result_t result; /* Last result code. */
};
#ifdef HAVE_TRUCLUSTER
#include <clua/clua.h>
#include <sys/socket.h>
#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
getbuf4(isc_interfaceiter_t *iter) {
char strbuf[ISC_STRERRORSIZE];
iter->bufsize = IFCONF_BUFSIZE_INITIAL;
for (;;) {
iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
if (iter->buf == NULL)
return (ISC_R_NOMEMORY);
memset(&iter->ifc.ifc_len, 0, sizeof(iter->ifc.ifc_len));
iter->ifc.ifc_len = iter->bufsize;
iter->ifc.ifc_buf = iter->buf;
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion". It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFCONF, (char *)&iter->ifc)
== -1) {
if (errno != EINVAL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETIFCONFIG,
"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.
*/
if (iter->ifc.ifc_len + 2 * sizeof(struct ifreq)
< iter->bufsize)
break;
}
if (iter->bufsize >= IFCONF_BUFSIZE_MAX) {
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_BUFFERMAX,
"get interface "
"configuration: "
"maximum buffer "
"size exceeded"));
goto unexpected;
}
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->bufsize *= 2;
}
return (ISC_R_SUCCESS);
unexpected:
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->buf = NULL;
return (ISC_R_UNEXPECTED);
}
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
static isc_result_t
getbuf6(isc_interfaceiter_t *iter) {
char strbuf[ISC_STRERRORSIZE];
isc_result_t result;
iter->bufsize6 = IFCONF_BUFSIZE_INITIAL;
for (;;) {
iter->buf6 = isc_mem_get(iter->mctx, iter->bufsize6);
if (iter->buf6 == NULL)
return (ISC_R_NOMEMORY);
memset(&iter->lifc, 0, sizeof(iter->lifc));
#ifdef ISC_HAVE_LIFC_FAMILY
iter->lifc.lifc_family = AF_INET6;
#endif
#ifdef ISC_HAVE_LIFC_FLAGS
iter->lifc.lifc_flags = 0;
#endif
iter->lifc.lifc_len = iter->bufsize6;
iter->lifc.lifc_buf = iter->buf6;
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion". It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket6, SIOCGLIFCONF, (char *)&iter->lifc)
== -1) {
#ifdef __hpux
/*
* IPv6 interface scanning is not available on all
* kernels w/ IPv6 sockets.
*/
if (errno == ENOENT) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_INTERFACE,
ISC_LOG_DEBUG(1),
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETIFCONFIG,
"get interface "
"configuration: %s"),
strbuf);
result = ISC_R_FAILURE;
goto cleanup;
}
#endif
if (errno != EINVAL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETIFCONFIG,
"get interface "
"configuration: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
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.
*/
if (iter->lifc.lifc_len + 2 * sizeof(struct LIFREQ)
< iter->bufsize6)
break;
}
if (iter->bufsize6 >= IFCONF_BUFSIZE_MAX) {
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_BUFFERMAX,
"get interface "
"configuration: "
"maximum buffer "
"size exceeded"));
result = ISC_R_UNEXPECTED;
goto cleanup;
}
isc_mem_put(iter->mctx, iter->buf6, iter->bufsize6);
iter->bufsize6 *= 2;
}
if (iter->lifc.lifc_len != 0)
iter->mode = 6;
return (ISC_R_SUCCESS);
cleanup:
isc_mem_put(iter->mctx, iter->buf6, iter->bufsize6);
iter->buf6 = NULL;
return (result);
}
#endif
isc_result_t
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
isc_interfaceiter_t *iter;
isc_result_t result;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(mctx != NULL);
REQUIRE(iterp != NULL);
REQUIRE(*iterp == NULL);
iter = isc_mem_get(mctx, sizeof(*iter));
if (iter == NULL)
return (ISC_R_NOMEMORY);
iter->mctx = mctx;
iter->mode = 4;
iter->buf = NULL;
iter->pos = (unsigned int) -1;
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
iter->buf6 = NULL;
iter->pos6 = (unsigned int) -1;
iter->result6 = ISC_R_NOMORE;
iter->socket6 = -1;
iter->first6 = ISC_FALSE;
#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
* ioctl on. HP/UX requires an AF_INET6 socket for
* SIOCGLIFCONF to get IPv6 addresses.
*/
if ((iter->socket6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_MAKESCANSOCKET,
"making interface "
"scan socket: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
goto socket6_failure;
}
result = iter->result6 = getbuf6(iter);
if (result != ISC_R_NOTIMPLEMENTED && result != ISC_R_SUCCESS)
goto ioctl6_failure;
}
#endif
if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_MAKESCANSOCKET,
"making interface "
"scan socket: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
goto socket_failure;
}
result = getbuf4(iter);
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
iter->clua_context = -1;
iter->clua_done = ISC_TRUE;
#endif
#ifdef __linux
iter->proc = fopen("/proc/net/if_inet6", "r");
iter->valid = ISC_R_FAILURE;
#endif
iter->result = ISC_R_FAILURE;
iter->magic = IFITER_MAGIC;
*iterp = iter;
return (ISC_R_SUCCESS);
ioctl_failure:
if (iter->buf != NULL)
isc_mem_put(mctx, iter->buf, iter->bufsize);
(void) close(iter->socket);
socket_failure:
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
if (iter->buf6 != NULL)
isc_mem_put(mctx, iter->buf6, iter->bufsize6);
ioctl6_failure:
if (iter->socket6 != -1)
(void) close(iter->socket6);
socket6_failure:
#endif
isc_mem_put(mctx, iter, sizeof(*iter));
return (result);
}
#ifdef HAVE_TRUCLUSTER
static void
get_inaddr(isc_netaddr_t *dst, struct in_addr *src) {
dst->family = AF_INET;
memcpy(&dst->type.in, src, sizeof(struct in_addr));
}
static isc_result_t
internal_current_clusteralias(isc_interfaceiter_t *iter) {
struct clua_info ci;
if (clua_getaliasinfo(&iter->clua_sa, &ci) != CLUA_SUCCESS)
return (ISC_R_IGNORE);
memset(&iter->current, 0, sizeof(iter->current));
iter->current.af = iter->clua_sa.sa_family;
memset(iter->current.name, 0, sizeof(iter->current.name));
sprintf(iter->current.name, "clua%d", ci.aliasid);
iter->current.flags = INTERFACE_F_UP;
get_inaddr(&iter->current.address, &ci.addr);
get_inaddr(&iter->current.netmask, &ci.netmask);
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
internal_current4(isc_interfaceiter_t *iter) {
struct ifreq *ifrp;
struct ifreq ifreq;
int family;
char strbuf[ISC_STRERRORSIZE];
#if !defined(ISC_PLATFORM_HAVEIF_LADDRREQ) && defined(SIOCGLIFADDR)
struct lifreq lifreq;
#else
char sabuf[256];
#endif
int i, bits, prefixlen;
REQUIRE(VALID_IFITER(iter));
if (iter->ifc.ifc_len == 0 ||
iter->pos == (unsigned int)iter->ifc.ifc_len) {
#ifdef __linux
return (linux_if_inet6_current(iter));
#else
return (ISC_R_NOMORE);
#endif
}
INSIST( iter->pos < (unsigned int) iter->ifc.ifc_len);
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
memset(&ifreq, 0, sizeof(ifreq));
memcpy(&ifreq, ifrp, sizeof(ifreq));
family = ifreq.ifr_addr.sa_family;
#if defined(ISC_PLATFORM_HAVEIPV6)
if (family != AF_INET && family != AF_INET6)
#else
if (family != AF_INET)
#endif
return (ISC_R_IGNORE);
memset(&iter->current, 0, sizeof(iter->current));
iter->current.af = family;
INSIST(sizeof(ifreq.ifr_name) <= sizeof(iter->current.name));
memset(iter->current.name, 0, sizeof(iter->current.name));
memcpy(iter->current.name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
get_addr(family, &iter->current.address,
(struct sockaddr *)&ifrp->ifr_addr, ifreq.ifr_name);
/*
* If the interface does not have a address ignore it.
*/
switch (family) {
case AF_INET:
if (iter->current.address.type.in.s_addr == htonl(INADDR_ANY))
return (ISC_R_IGNORE);
break;
case AF_INET6:
if (memcmp(&iter->current.address.type.in6, &in6addr_any,
sizeof(in6addr_any)) == 0)
return (ISC_R_IGNORE);
break;
}
/*
* Get interface flags.
*/
iter->current.flags = 0;
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface flags: %s",
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
if ((ifreq.ifr_flags & IFF_UP) != 0)
iter->current.flags |= INTERFACE_F_UP;
#ifdef IFF_POINTOPOINT
if ((ifreq.ifr_flags & IFF_POINTOPOINT) != 0)
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
#endif
if ((ifreq.ifr_flags & IFF_LOOPBACK) != 0)
iter->current.flags |= INTERFACE_F_LOOPBACK;
if ((ifreq.ifr_flags & IFF_BROADCAST) != 0)
iter->current.flags |= INTERFACE_F_BROADCAST;
#ifdef IFF_MULTICAST
if ((ifreq.ifr_flags & IFF_MULTICAST) != 0)
iter->current.flags |= INTERFACE_F_MULTICAST;
#endif
if (family == AF_INET)
goto inet;
#if !defined(ISC_PLATFORM_HAVEIF_LADDRREQ) && defined(SIOCGLIFADDR)
memset(&lifreq, 0, sizeof(lifreq));
memcpy(lifreq.lifr_name, iter->current.name, sizeof(lifreq.lifr_name));
memcpy(&lifreq.lifr_addr, &iter->current.address.type.in6,
sizeof(iter->current.address.type.in6));
if (ioctl(iter->socket, SIOCGLIFADDR, &lifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface address: %s",
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
prefixlen = lifreq.lifr_addrlen;
#else
isc_netaddr_format(&iter->current.address, sabuf, sizeof(sabuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_INTERFACE,
ISC_LOG_INFO,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETIFCONFIG,
"prefix length for %s is unknown "
"(assume 128)"), sabuf);
prefixlen = 128;
#endif
/*
* Netmask already zeroed.
*/
iter->current.netmask.family = family;
for (i = 0; i < 16; i++) {
if (prefixlen > 8) {
bits = 0;
prefixlen -= 8;
} else {
bits = 8 - prefixlen;
prefixlen = 0;
}
iter->current.netmask.type.in6.s6_addr[i] = (~0 << bits) & 0xff;
}
return (ISC_R_SUCCESS);
inet:
if (family != AF_INET)
return (ISC_R_IGNORE);
#ifdef IFF_POINTOPOINT
/*
* If the interface is point-to-point, get the destination address.
*/
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFDSTADDR, (char *)&ifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETDESTADDR,
"%s: getting "
"destination address: %s"),
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.dstaddress,
(struct sockaddr *)&ifreq.ifr_dstaddr, ifreq.ifr_name);
}
#endif
if ((iter->current.flags & INTERFACE_F_BROADCAST) != 0) {
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFBRDADDR, (char *)&ifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETBCSTADDR,
"%s: getting "
"broadcast address: %s"),
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.broadcast,
(struct sockaddr *)&ifreq.ifr_broadaddr, ifreq.ifr_name);
}
/*
* Get the network mask.
*/
memset(&ifreq, 0, sizeof(ifreq));
memcpy(&ifreq, ifrp, sizeof(ifreq));
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETNETMASK,
"%s: getting netmask: %s"),
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.netmask,
(struct sockaddr *)&ifreq.ifr_addr, ifreq.ifr_name);
return (ISC_R_SUCCESS);
}
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
static isc_result_t
internal_current6(isc_interfaceiter_t *iter) {
struct LIFREQ *ifrp;
struct LIFREQ lifreq;
int family;
char strbuf[ISC_STRERRORSIZE];
int fd;
REQUIRE(VALID_IFITER(iter));
if (iter->result6 != ISC_R_SUCCESS)
return (iter->result6);
REQUIRE(iter->pos6 < (unsigned int) iter->lifc.lifc_len);
ifrp = (struct LIFREQ *)((char *) iter->lifc.lifc_req + iter->pos6);
memset(&lifreq, 0, sizeof(lifreq));
memcpy(&lifreq, ifrp, sizeof(lifreq));
family = lifreq.lifr_addr.ss_family;
#ifdef ISC_PLATFORM_HAVEIPV6
if (family != AF_INET && family != AF_INET6)
#else
if (family != AF_INET)
#endif
return (ISC_R_IGNORE);
memset(&iter->current, 0, sizeof(iter->current));
iter->current.af = family;
INSIST(sizeof(lifreq.lifr_name) <= sizeof(iter->current.name));
memset(iter->current.name, 0, sizeof(iter->current.name));
memcpy(iter->current.name, lifreq.lifr_name, sizeof(lifreq.lifr_name));
get_addr(family, &iter->current.address,
(struct sockaddr *)&lifreq.lifr_addr, lifreq.lifr_name);
iter->current.ifindex = lifreq.lifr_index;
if (isc_netaddr_islinklocal(&iter->current.address))
isc_netaddr_setzone(&iter->current.address,
(isc_uint32_t)lifreq.lifr_index);
/*
* If the interface does not have a address ignore it.
*/
switch (family) {
case AF_INET:
if (iter->current.address.type.in.s_addr == htonl(INADDR_ANY))
return (ISC_R_IGNORE);
break;
case AF_INET6:
if (memcmp(&iter->current.address.type.in6, &in6addr_any,
sizeof(in6addr_any)) == 0)
return (ISC_R_IGNORE);
break;
}
/*
* Get interface flags.
*/
iter->current.flags = 0;
if (family == AF_INET6)
fd = iter->socket6;
else
fd = iter->socket;
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(fd, SIOCGLIFFLAGS, (char *) &lifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface flags: %s",
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
if ((lifreq.lifr_flags & IFF_UP) != 0)
iter->current.flags |= INTERFACE_F_UP;
#ifdef IFF_POINTOPOINT
if ((lifreq.lifr_flags & IFF_POINTOPOINT) != 0)
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
#endif
if ((lifreq.lifr_flags & IFF_LOOPBACK) != 0)
iter->current.flags |= INTERFACE_F_LOOPBACK;
if ((lifreq.lifr_flags & IFF_BROADCAST) != 0) {
iter->current.flags |= INTERFACE_F_BROADCAST;
}
#ifdef IFF_MULTICAST
if ((lifreq.lifr_flags & IFF_MULTICAST) != 0) {
iter->current.flags |= INTERFACE_F_MULTICAST;
}
#endif
#ifdef IFF_POINTOPOINT
/*
* If the interface is point-to-point, get the destination address.
*/
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(fd, SIOCGLIFDSTADDR, (char *)&lifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETDESTADDR,
"%s: getting "
"destination address: %s"),
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.dstaddress,
(struct sockaddr *)&lifreq.lifr_dstaddr,
lifreq.lifr_name);
}
#endif
#ifdef SIOCGLIFBRDADDR
if ((iter->current.flags & INTERFACE_F_BROADCAST) != 0) {
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGLIFBRDADDR, (char *)&lifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETBCSTADDR,
"%s: getting "
"broadcast address: %s"),
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.broadcast,
(struct sockaddr *)&lifreq.lifr_broadaddr,
lifreq.lifr_name);
}
#endif /* SIOCGLIFBRDADDR */
/*
* Get the network mask. Netmask already zeroed.
*/
memset(&lifreq, 0, sizeof(lifreq));
memcpy(&lifreq, ifrp, sizeof(lifreq));
#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.
*/
if (family == AF_INET6) {
int i, bits;
iter->current.netmask.family = family;
for (i = 0; i < lifreq.lifr_addrlen; i += 8) {
bits = lifreq.lifr_addrlen - i;
bits = (bits < 8) ? (8 - bits) : 0;
iter->current.netmask.type.in6.s6_addr[i / 8] =
(~0 << bits) & 0xff;
}
return (ISC_R_SUCCESS);
}
#endif
/*
* Ignore the HP/UX warning about "integer overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(fd, SIOCGLIFNETMASK, (char *)&lifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETNETMASK,
"%s: getting netmask: %s"),
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.netmask,
(struct sockaddr *)&lifreq.lifr_addr, lifreq.lifr_name);
return (ISC_R_SUCCESS);
}
#endif
static isc_result_t
internal_current(isc_interfaceiter_t *iter) {
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
if (iter->mode == 6) {
iter->result6 = internal_current6(iter);
if (iter->result6 != ISC_R_NOMORE)
return (iter->result6);
}
#endif
#ifdef HAVE_TRUCLUSTER
if (!iter->clua_done)
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
internal_next4(isc_interfaceiter_t *iter) {
#ifdef ISC_PLATFORM_HAVESALEN
struct ifreq *ifrp;
#endif
if (iter->pos < (unsigned int) iter->ifc.ifc_len) {
#ifdef ISC_PLATFORM_HAVESALEN
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
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(struct ifreq);
} else {
INSIST(iter->pos == (unsigned int) iter->ifc.ifc_len);
#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
internal_next6(isc_interfaceiter_t *iter) {
#ifdef ISC_PLATFORM_HAVESALEN
struct LIFREQ *ifrp;
#endif
if (iter->result6 != ISC_R_SUCCESS && iter->result6 != ISC_R_IGNORE)
return (iter->result6);
REQUIRE(iter->pos6 < (unsigned int) iter->lifc.lifc_len);
#ifdef ISC_PLATFORM_HAVESALEN
ifrp = (struct LIFREQ *)((char *) iter->lifc.lifc_req + iter->pos6);
if (ifrp->lifr_addr.sa_len > sizeof(struct sockaddr))
iter->pos6 += sizeof(ifrp->lifr_name) + ifrp->lifr_addr.sa_len;
else
#endif
iter->pos6 += sizeof(struct LIFREQ);
if (iter->pos6 >= (unsigned int) iter->lifc.lifc_len)
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
#endif
static isc_result_t
internal_next(isc_interfaceiter_t *iter) {
#ifdef HAVE_TRUCLUSTER
int clua_result;
#endif
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
if (iter->mode == 6) {
iter->result6 = internal_next6(iter);
if (iter->result6 != ISC_R_NOMORE)
return (iter->result6);
if (iter->first6) {
iter->first6 = ISC_FALSE;
return (ISC_R_SUCCESS);
}
}
#endif
#ifdef HAVE_TRUCLUSTER
if (!iter->clua_done) {
clua_result = clua_getaliasaddress(&iter->clua_sa,
&iter->clua_context);
if (clua_result != CLUA_SUCCESS)
iter->clua_done = ISC_TRUE;
return (ISC_R_SUCCESS);
}
#endif
return (internal_next4(iter));
}
static void
internal_destroy(isc_interfaceiter_t *iter) {
(void) close(iter->socket);
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
if (iter->socket6 != -1)
(void) close(iter->socket6);
if (iter->buf6 != NULL) {
isc_mem_put(iter->mctx, iter->buf6, iter->bufsize6);
}
#endif
#ifdef __linux
if (iter->proc != NULL)
fclose(iter->proc);
#endif
}
static
void internal_first(isc_interfaceiter_t *iter) {
#ifdef HAVE_TRUCLUSTER
int clua_result;
#endif
iter->pos = 0;
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
iter->pos6 = 0;
if (iter->result6 == ISC_R_NOMORE)
iter->result6 = ISC_R_SUCCESS;
iter->first6 = ISC_TRUE;
#endif
#ifdef HAVE_TRUCLUSTER
iter->clua_context = 0;
clua_result = clua_getaliasaddress(&iter->clua_sa,
&iter->clua_context);
iter->clua_done = ISC_TF(clua_result != CLUA_SUCCESS);
#endif
#ifdef __linux
linux_if_inet6_first(iter);
#endif
}
|