if_sysctl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Get interface's address and mask information by sysctl() function.
  3. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "if.h"
  24. #include "sockunion.h"
  25. #include "prefix.h"
  26. #include "connected.h"
  27. #include "memory.h"
  28. #include "ioctl.h"
  29. #include "log.h"
  30. #include "interface.h"
  31. #include "vrf.h"
  32. #include "zebra/rt.h"
  33. #include "zebra/kernel_socket.h"
  34. #include "zebra/rib.h"
  35. void
  36. ifstat_update_sysctl (void)
  37. {
  38. caddr_t ref, buf, end;
  39. size_t bufsiz;
  40. struct if_msghdr *ifm;
  41. struct interface *ifp;
  42. #define MIBSIZ 6
  43. int mib[MIBSIZ] =
  44. {
  45. CTL_NET,
  46. PF_ROUTE,
  47. 0,
  48. 0, /* AF_INET & AF_INET6 */
  49. NET_RT_IFLIST,
  50. 0
  51. };
  52. /* Query buffer size. */
  53. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  54. {
  55. zlog_warn ("sysctl() error by %s", safe_strerror (errno));
  56. return;
  57. }
  58. /* We free this memory at the end of this function. */
  59. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  60. /* Fetch interface informations into allocated buffer. */
  61. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  62. {
  63. zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
  64. return;
  65. }
  66. /* Parse both interfaces and addresses. */
  67. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  68. {
  69. ifm = (struct if_msghdr *) buf;
  70. if (ifm->ifm_type == RTM_IFINFO)
  71. {
  72. ifp = if_lookup_by_index (ifm->ifm_index);
  73. if (ifp)
  74. ifp->stats = ifm->ifm_data;
  75. }
  76. }
  77. /* Free sysctl buffer. */
  78. XFREE (MTYPE_TMP, ref);
  79. return;
  80. }
  81. /* Interface listing up function using sysctl(). */
  82. void
  83. interface_list (struct zebra_vrf *zvrf)
  84. {
  85. caddr_t ref, buf, end;
  86. size_t bufsiz;
  87. struct if_msghdr *ifm;
  88. #define MIBSIZ 6
  89. int mib[MIBSIZ] =
  90. {
  91. CTL_NET,
  92. PF_ROUTE,
  93. 0,
  94. 0, /* AF_INET & AF_INET6 */
  95. NET_RT_IFLIST,
  96. 0
  97. };
  98. if (zvrf->vrf_id != VRF_DEFAULT)
  99. {
  100. zlog_warn ("interface_list: ignore VRF %u", zvrf->vrf_id);
  101. return;
  102. }
  103. /* Query buffer size. */
  104. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  105. {
  106. zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno));
  107. return;
  108. }
  109. /* We free this memory at the end of this function. */
  110. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  111. /* Fetch interface informations into allocated buffer. */
  112. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  113. {
  114. zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
  115. return;
  116. }
  117. /* Parse both interfaces and addresses. */
  118. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  119. {
  120. ifm = (struct if_msghdr *) buf;
  121. switch (ifm->ifm_type)
  122. {
  123. case RTM_IFINFO:
  124. ifm_read (ifm);
  125. break;
  126. case RTM_NEWADDR:
  127. ifam_read ((struct ifa_msghdr *) ifm);
  128. break;
  129. default:
  130. zlog_info ("interfaces_list(): unexpected message type");
  131. XFREE (MTYPE_TMP, ref);
  132. return;
  133. break;
  134. }
  135. }
  136. /* Free sysctl buffer. */
  137. XFREE (MTYPE_TMP, ref);
  138. }