/contrib/bind9/lib/isc/unix/interfaceiter.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 312 lines · 225 code · 34 blank · 53 comment · 46 complexity · 35a5e5c1838957d1f1fc9599f0d84b3c MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: interfaceiter.c,v 1.45 2008/12/01 03:51:47 marka Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <sys/types.h>
  21. #include <sys/ioctl.h>
  22. #ifdef HAVE_SYS_SOCKIO_H
  23. #include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
  24. #endif
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <isc/interfaceiter.h>
  30. #include <isc/log.h>
  31. #include <isc/magic.h>
  32. #include <isc/mem.h>
  33. #include <isc/msgs.h>
  34. #include <isc/net.h>
  35. #include <isc/print.h>
  36. #include <isc/result.h>
  37. #include <isc/strerror.h>
  38. #include <isc/string.h>
  39. #include <isc/types.h>
  40. #include <isc/util.h>
  41. /* Must follow <isc/net.h>. */
  42. #ifdef HAVE_NET_IF6_H
  43. #include <net/if6.h>
  44. #endif
  45. #include <net/if.h>
  46. /* Common utility functions */
  47. /*%
  48. * Extract the network address part from a "struct sockaddr".
  49. * \brief
  50. * The address family is given explicitly
  51. * instead of using src->sa_family, because the latter does not work
  52. * for copying a network mask obtained by SIOCGIFNETMASK (it does
  53. * not have a valid address family).
  54. */
  55. static void
  56. get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
  57. char *ifname)
  58. {
  59. struct sockaddr_in6 *sa6;
  60. #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
  61. !defined(ISC_PLATFORM_HAVESCOPEID)
  62. UNUSED(ifname);
  63. #endif
  64. /* clear any remaining value for safety */
  65. memset(dst, 0, sizeof(*dst));
  66. dst->family = family;
  67. switch (family) {
  68. case AF_INET:
  69. memcpy(&dst->type.in,
  70. &((struct sockaddr_in *) src)->sin_addr,
  71. sizeof(struct in_addr));
  72. break;
  73. case AF_INET6:
  74. sa6 = (struct sockaddr_in6 *)src;
  75. memcpy(&dst->type.in6, &sa6->sin6_addr,
  76. sizeof(struct in6_addr));
  77. #ifdef ISC_PLATFORM_HAVESCOPEID
  78. if (sa6->sin6_scope_id != 0)
  79. isc_netaddr_setzone(dst, sa6->sin6_scope_id);
  80. else {
  81. /*
  82. * BSD variants embed scope zone IDs in the 128bit
  83. * address as a kernel internal form. Unfortunately,
  84. * the embedded IDs are not hidden from applications
  85. * when getting access to them by sysctl or ioctl.
  86. * We convert the internal format to the pure address
  87. * part and the zone ID part.
  88. * Since multicast addresses should not appear here
  89. * and they cannot be distinguished from netmasks,
  90. * we only consider unicast link-local addresses.
  91. */
  92. if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
  93. isc_uint16_t zone16;
  94. memcpy(&zone16, &sa6->sin6_addr.s6_addr[2],
  95. sizeof(zone16));
  96. zone16 = ntohs(zone16);
  97. if (zone16 != 0) {
  98. /* the zone ID is embedded */
  99. isc_netaddr_setzone(dst,
  100. (isc_uint32_t)zone16);
  101. dst->type.in6.s6_addr[2] = 0;
  102. dst->type.in6.s6_addr[3] = 0;
  103. #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
  104. } else if (ifname != NULL) {
  105. unsigned int zone;
  106. /*
  107. * sin6_scope_id is still not provided,
  108. * but the corresponding interface name
  109. * is know. Use the interface ID as
  110. * the link ID.
  111. */
  112. zone = if_nametoindex(ifname);
  113. if (zone != 0) {
  114. isc_netaddr_setzone(dst,
  115. (isc_uint32_t)zone);
  116. }
  117. #endif
  118. }
  119. }
  120. }
  121. #endif
  122. break;
  123. default:
  124. INSIST(0);
  125. break;
  126. }
  127. }
  128. /*
  129. * Include system-dependent code.
  130. */
  131. #ifdef __linux
  132. #define ISC_IF_INET6_SZ \
  133. sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
  134. static isc_result_t linux_if_inet6_next(isc_interfaceiter_t *);
  135. static isc_result_t linux_if_inet6_current(isc_interfaceiter_t *);
  136. static void linux_if_inet6_first(isc_interfaceiter_t *iter);
  137. #endif
  138. #if HAVE_GETIFADDRS
  139. #include "ifiter_getifaddrs.c"
  140. #elif HAVE_IFLIST_SYSCTL
  141. #include "ifiter_sysctl.c"
  142. #else
  143. #include "ifiter_ioctl.c"
  144. #endif
  145. #ifdef __linux
  146. static void
  147. linux_if_inet6_first(isc_interfaceiter_t *iter) {
  148. if (iter->proc != NULL) {
  149. rewind(iter->proc);
  150. (void)linux_if_inet6_next(iter);
  151. } else
  152. iter->valid = ISC_R_NOMORE;
  153. }
  154. static isc_result_t
  155. linux_if_inet6_next(isc_interfaceiter_t *iter) {
  156. if (iter->proc != NULL &&
  157. fgets(iter->entry, sizeof(iter->entry), iter->proc) != NULL)
  158. iter->valid = ISC_R_SUCCESS;
  159. else
  160. iter->valid = ISC_R_NOMORE;
  161. return (iter->valid);
  162. }
  163. static isc_result_t
  164. linux_if_inet6_current(isc_interfaceiter_t *iter) {
  165. char address[33];
  166. char name[IF_NAMESIZE+1];
  167. struct in6_addr addr6;
  168. int ifindex, prefix, flag3, flag4;
  169. int res;
  170. unsigned int i;
  171. if (iter->valid != ISC_R_SUCCESS)
  172. return (iter->valid);
  173. if (iter->proc == NULL) {
  174. isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
  175. ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
  176. "/proc/net/if_inet6:iter->proc == NULL");
  177. return (ISC_R_FAILURE);
  178. }
  179. res = sscanf(iter->entry, "%32[a-f0-9] %x %x %x %x %16s\n",
  180. address, &ifindex, &prefix, &flag3, &flag4, name);
  181. if (res != 6) {
  182. isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
  183. ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
  184. "/proc/net/if_inet6:sscanf() -> %d (expected 6)",
  185. res);
  186. return (ISC_R_FAILURE);
  187. }
  188. if (strlen(address) != 32) {
  189. isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
  190. ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
  191. "/proc/net/if_inet6:strlen(%s) != 32", address);
  192. return (ISC_R_FAILURE);
  193. }
  194. for (i = 0; i < 16; i++) {
  195. unsigned char byte;
  196. static const char hex[] = "0123456789abcdef";
  197. byte = ((strchr(hex, address[i * 2]) - hex) << 4) |
  198. (strchr(hex, address[i * 2 + 1]) - hex);
  199. addr6.s6_addr[i] = byte;
  200. }
  201. iter->current.af = AF_INET6;
  202. iter->current.flags = INTERFACE_F_UP;
  203. isc_netaddr_fromin6(&iter->current.address, &addr6);
  204. if (isc_netaddr_islinklocal(&iter->current.address)) {
  205. isc_netaddr_setzone(&iter->current.address,
  206. (isc_uint32_t)ifindex);
  207. }
  208. for (i = 0; i < 16; i++) {
  209. if (prefix > 8) {
  210. addr6.s6_addr[i] = 0xff;
  211. prefix -= 8;
  212. } else {
  213. addr6.s6_addr[i] = (0xff << (8 - prefix)) & 0xff;
  214. prefix = 0;
  215. }
  216. }
  217. isc_netaddr_fromin6(&iter->current.netmask, &addr6);
  218. strncpy(iter->current.name, name, sizeof(iter->current.name));
  219. return (ISC_R_SUCCESS);
  220. }
  221. #endif
  222. /*
  223. * The remaining code is common to the sysctl and ioctl case.
  224. */
  225. isc_result_t
  226. isc_interfaceiter_current(isc_interfaceiter_t *iter,
  227. isc_interface_t *ifdata)
  228. {
  229. REQUIRE(iter->result == ISC_R_SUCCESS);
  230. memcpy(ifdata, &iter->current, sizeof(*ifdata));
  231. return (ISC_R_SUCCESS);
  232. }
  233. isc_result_t
  234. isc_interfaceiter_first(isc_interfaceiter_t *iter) {
  235. isc_result_t result;
  236. REQUIRE(VALID_IFITER(iter));
  237. internal_first(iter);
  238. for (;;) {
  239. result = internal_current(iter);
  240. if (result != ISC_R_IGNORE)
  241. break;
  242. result = internal_next(iter);
  243. if (result != ISC_R_SUCCESS)
  244. break;
  245. }
  246. iter->result = result;
  247. return (result);
  248. }
  249. isc_result_t
  250. isc_interfaceiter_next(isc_interfaceiter_t *iter) {
  251. isc_result_t result;
  252. REQUIRE(VALID_IFITER(iter));
  253. REQUIRE(iter->result == ISC_R_SUCCESS);
  254. for (;;) {
  255. result = internal_next(iter);
  256. if (result != ISC_R_SUCCESS)
  257. break;
  258. result = internal_current(iter);
  259. if (result != ISC_R_IGNORE)
  260. break;
  261. }
  262. iter->result = result;
  263. return (result);
  264. }
  265. void
  266. isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
  267. {
  268. isc_interfaceiter_t *iter;
  269. REQUIRE(iterp != NULL);
  270. iter = *iterp;
  271. REQUIRE(VALID_IFITER(iter));
  272. internal_destroy(iter);
  273. if (iter->buf != NULL)
  274. isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
  275. iter->magic = 0;
  276. isc_mem_put(iter->mctx, iter, sizeof(*iter));
  277. *iterp = NULL;
  278. }