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

https://bitbucket.org/freebsd/freebsd-head/ · C · 302 lines · 196 code · 49 blank · 57 comment · 40 complexity · 67909a6c018687301a96ae43b8754cc8 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 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: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp $ */
  18. /*! \file
  19. * \brief
  20. * Obtain the list of network interfaces using sysctl.
  21. * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
  22. * and 19.16.
  23. */
  24. #include <sys/param.h>
  25. #include <sys/sysctl.h>
  26. #include <net/route.h>
  27. #include <net/if_dl.h>
  28. /* XXX what about Alpha? */
  29. #ifdef sgi
  30. #define ROUNDUP(a) ((a) > 0 ? \
  31. (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
  32. sizeof(__uint64_t))
  33. #else
  34. #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
  35. : sizeof(long))
  36. #endif
  37. #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S')
  38. #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
  39. struct isc_interfaceiter {
  40. unsigned int magic; /* Magic number. */
  41. isc_mem_t *mctx;
  42. void *buf; /* Buffer for sysctl data. */
  43. unsigned int bufsize; /* Bytes allocated. */
  44. unsigned int bufused; /* Bytes used. */
  45. unsigned int pos; /* Current offset in
  46. sysctl data. */
  47. isc_interface_t current; /* Current interface data. */
  48. isc_result_t result; /* Last result code. */
  49. };
  50. static int mib[6] = {
  51. CTL_NET,
  52. PF_ROUTE,
  53. 0,
  54. 0, /* Any address family. */
  55. NET_RT_IFLIST,
  56. 0 /* Flags. */
  57. };
  58. isc_result_t
  59. isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
  60. isc_interfaceiter_t *iter;
  61. isc_result_t result;
  62. size_t bufsize;
  63. size_t bufused;
  64. char strbuf[ISC_STRERRORSIZE];
  65. REQUIRE(mctx != NULL);
  66. REQUIRE(iterp != NULL);
  67. REQUIRE(*iterp == NULL);
  68. iter = isc_mem_get(mctx, sizeof(*iter));
  69. if (iter == NULL)
  70. return (ISC_R_NOMEMORY);
  71. iter->mctx = mctx;
  72. iter->buf = 0;
  73. /*
  74. * Determine the amount of memory needed.
  75. */
  76. bufsize = 0;
  77. if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
  78. isc__strerror(errno, strbuf, sizeof(strbuf));
  79. UNEXPECTED_ERROR(__FILE__, __LINE__,
  80. isc_msgcat_get(isc_msgcat,
  81. ISC_MSGSET_IFITERSYSCTL,
  82. ISC_MSG_GETIFLISTSIZE,
  83. "getting interface "
  84. "list size: sysctl: %s"),
  85. strbuf);
  86. result = ISC_R_UNEXPECTED;
  87. goto failure;
  88. }
  89. iter->bufsize = bufsize;
  90. iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
  91. if (iter->buf == NULL) {
  92. result = ISC_R_NOMEMORY;
  93. goto failure;
  94. }
  95. bufused = bufsize;
  96. if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
  97. isc__strerror(errno, strbuf, sizeof(strbuf));
  98. UNEXPECTED_ERROR(__FILE__, __LINE__,
  99. isc_msgcat_get(isc_msgcat,
  100. ISC_MSGSET_IFITERSYSCTL,
  101. ISC_MSG_GETIFLIST,
  102. "getting interface list: "
  103. "sysctl: %s"),
  104. strbuf);
  105. result = ISC_R_UNEXPECTED;
  106. goto failure;
  107. }
  108. iter->bufused = bufused;
  109. INSIST(iter->bufused <= iter->bufsize);
  110. /*
  111. * A newly created iterator has an undefined position
  112. * until isc_interfaceiter_first() is called.
  113. */
  114. iter->pos = (unsigned int) -1;
  115. iter->result = ISC_R_FAILURE;
  116. iter->magic = IFITER_MAGIC;
  117. *iterp = iter;
  118. return (ISC_R_SUCCESS);
  119. failure:
  120. if (iter->buf != NULL)
  121. isc_mem_put(mctx, iter->buf, iter->bufsize);
  122. isc_mem_put(mctx, iter, sizeof(*iter));
  123. return (result);
  124. }
  125. /*
  126. * Get information about the current interface to iter->current.
  127. * If successful, return ISC_R_SUCCESS.
  128. * If the interface has an unsupported address family,
  129. * return ISC_R_IGNORE. In case of other failure,
  130. * return ISC_R_UNEXPECTED.
  131. */
  132. static isc_result_t
  133. internal_current(isc_interfaceiter_t *iter) {
  134. struct ifa_msghdr *ifam, *ifam_end;
  135. REQUIRE(VALID_IFITER(iter));
  136. REQUIRE (iter->pos < (unsigned int) iter->bufused);
  137. ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
  138. ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
  139. if (ifam->ifam_type == RTM_IFINFO) {
  140. struct if_msghdr *ifm = (struct if_msghdr *) ifam;
  141. struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
  142. unsigned int namelen;
  143. memset(&iter->current, 0, sizeof(iter->current));
  144. namelen = sdl->sdl_nlen;
  145. if (namelen > sizeof(iter->current.name) - 1)
  146. namelen = sizeof(iter->current.name) - 1;
  147. memset(iter->current.name, 0, sizeof(iter->current.name));
  148. memcpy(iter->current.name, sdl->sdl_data, namelen);
  149. iter->current.flags = 0;
  150. if ((ifam->ifam_flags & IFF_UP) != 0)
  151. iter->current.flags |= INTERFACE_F_UP;
  152. if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
  153. iter->current.flags |= INTERFACE_F_POINTTOPOINT;
  154. if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
  155. iter->current.flags |= INTERFACE_F_LOOPBACK;
  156. /*
  157. * This is not an interface address.
  158. * Force another iteration.
  159. */
  160. return (ISC_R_IGNORE);
  161. } else if (ifam->ifam_type == RTM_NEWADDR) {
  162. int i;
  163. int family;
  164. struct sockaddr *mask_sa = NULL;
  165. struct sockaddr *addr_sa = NULL;
  166. struct sockaddr *dst_sa = NULL;
  167. struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
  168. family = sa->sa_family;
  169. for (i = 0; i < RTAX_MAX; i++)
  170. {
  171. if ((ifam->ifam_addrs & (1 << i)) == 0)
  172. continue;
  173. INSIST(sa < (struct sockaddr *) ifam_end);
  174. switch (i) {
  175. case RTAX_NETMASK: /* Netmask */
  176. mask_sa = sa;
  177. break;
  178. case RTAX_IFA: /* Interface address */
  179. addr_sa = sa;
  180. break;
  181. case RTAX_BRD: /* Broadcast or destination address */
  182. dst_sa = sa;
  183. break;
  184. }
  185. #ifdef ISC_PLATFORM_HAVESALEN
  186. sa = (struct sockaddr *)((char*)(sa)
  187. + ROUNDUP(sa->sa_len));
  188. #else
  189. #ifdef sgi
  190. /*
  191. * Do as the contributed SGI code does.
  192. */
  193. sa = (struct sockaddr *)((char*)(sa)
  194. + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
  195. #else
  196. /* XXX untested. */
  197. sa = (struct sockaddr *)((char*)(sa)
  198. + ROUNDUP(sizeof(struct sockaddr)));
  199. #endif
  200. #endif
  201. }
  202. if (addr_sa == NULL)
  203. return (ISC_R_IGNORE);
  204. family = addr_sa->sa_family;
  205. if (family != AF_INET && family != AF_INET6)
  206. return (ISC_R_IGNORE);
  207. iter->current.af = family;
  208. get_addr(family, &iter->current.address, addr_sa,
  209. iter->current.name);
  210. if (mask_sa != NULL)
  211. get_addr(family, &iter->current.netmask, mask_sa,
  212. iter->current.name);
  213. if (dst_sa != NULL &&
  214. (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
  215. get_addr(family, &iter->current.dstaddress, dst_sa,
  216. iter->current.name);
  217. return (ISC_R_SUCCESS);
  218. } else {
  219. printf(isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL,
  220. ISC_MSG_UNEXPECTEDTYPE,
  221. "warning: unexpected interface list "
  222. "message type\n"));
  223. return (ISC_R_IGNORE);
  224. }
  225. }
  226. /*
  227. * Step the iterator to the next interface. Unlike
  228. * isc_interfaceiter_next(), this may leave the iterator
  229. * positioned on an interface that will ultimately
  230. * be ignored. Return ISC_R_NOMORE if there are no more
  231. * interfaces, otherwise ISC_R_SUCCESS.
  232. */
  233. static isc_result_t
  234. internal_next(isc_interfaceiter_t *iter) {
  235. struct ifa_msghdr *ifam;
  236. REQUIRE (iter->pos < (unsigned int) iter->bufused);
  237. ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
  238. iter->pos += ifam->ifam_msglen;
  239. if (iter->pos >= iter->bufused)
  240. return (ISC_R_NOMORE);
  241. return (ISC_R_SUCCESS);
  242. }
  243. static void
  244. internal_destroy(isc_interfaceiter_t *iter) {
  245. UNUSED(iter); /* Unused. */
  246. /*
  247. * Do nothing.
  248. */
  249. }
  250. static
  251. void internal_first(isc_interfaceiter_t *iter) {
  252. iter->pos = 0;
  253. }