iface_mgr_bsd.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #if defined(OS_BSD)
  8. #include <dhcp/iface_mgr.h>
  9. #include <dhcp/iface_mgr_error_handler.h>
  10. #include <dhcp/pkt_filter_bpf.h>
  11. #include <dhcp/pkt_filter_inet.h>
  12. #include <exceptions/exceptions.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <net/if_dl.h>
  16. #include <net/if.h>
  17. #include <ifaddrs.h>
  18. using namespace std;
  19. using namespace isc;
  20. using namespace isc::asiolink;
  21. using namespace isc::dhcp;
  22. namespace isc {
  23. namespace dhcp {
  24. /// This is a BSD specific interface detection method.
  25. void
  26. IfaceMgr::detectIfaces() {
  27. struct ifaddrs* iflist = 0;// The whole interface list
  28. struct ifaddrs* ifptr = 0; // The interface we're processing now
  29. // Gets list of ifaddrs struct
  30. if(getifaddrs(&iflist) != 0) {
  31. isc_throw(Unexpected, "Network interfaces detection failed.");
  32. }
  33. typedef map<string, IfacePtr> IfaceLst;
  34. IfaceLst::iterator iface_iter;
  35. IfaceLst ifaces;
  36. // First lookup for getting interfaces ...
  37. for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
  38. const char * ifname = ifptr->ifa_name;
  39. uint ifindex = 0;
  40. if (!(ifindex = if_nametoindex(ifname))) {
  41. // Interface name does not have corresponding index ...
  42. freeifaddrs(iflist);
  43. isc_throw(Unexpected, "Interface " << ifname << " has no index");
  44. }
  45. if ((iface_iter = ifaces.find(ifname)) != ifaces.end()) {
  46. continue;
  47. }
  48. IfacePtr iface(new Iface(ifname, ifindex));
  49. iface->setFlags(ifptr->ifa_flags);
  50. ifaces.insert(pair<string, IfacePtr>(ifname, iface));
  51. }
  52. // Second lookup to get MAC and IP addresses
  53. for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
  54. if ((iface_iter = ifaces.find(ifptr->ifa_name)) == ifaces.end()) {
  55. continue;
  56. }
  57. // Common byte pointer for following data
  58. const uint8_t * ptr = 0;
  59. if(ifptr->ifa_addr->sa_family == AF_LINK) {
  60. // HWAddr
  61. struct sockaddr_dl * ldata =
  62. reinterpret_cast<struct sockaddr_dl *>(ifptr->ifa_addr);
  63. ptr = reinterpret_cast<uint8_t *>(LLADDR(ldata));
  64. iface_iter->second->setHWType(ldata->sdl_type);
  65. iface_iter->second->setMac(ptr, ldata->sdl_alen);
  66. } else if(ifptr->ifa_addr->sa_family == AF_INET6) {
  67. // IPv6 Addr
  68. struct sockaddr_in6 * adata =
  69. reinterpret_cast<struct sockaddr_in6 *>(ifptr->ifa_addr);
  70. ptr = reinterpret_cast<uint8_t *>(&adata->sin6_addr);
  71. IOAddress a = IOAddress::fromBytes(AF_INET6, ptr);
  72. iface_iter->second->addAddress(a);
  73. } else {
  74. // IPv4 Addr
  75. struct sockaddr_in * adata =
  76. reinterpret_cast<struct sockaddr_in *>(ifptr->ifa_addr);
  77. ptr = reinterpret_cast<uint8_t *>(&adata->sin_addr);
  78. IOAddress a = IOAddress::fromBytes(AF_INET, ptr);
  79. iface_iter->second->addAddress(a);
  80. }
  81. }
  82. freeifaddrs(iflist);
  83. // Interfaces registering
  84. for(IfaceLst::const_iterator iface_iter = ifaces.begin();
  85. iface_iter != ifaces.end(); ++iface_iter) {
  86. addInterface(iface_iter->second);
  87. }
  88. }
  89. /// @brief sets flag_*_ fields
  90. ///
  91. /// Like Linux version, os specific flags
  92. ///
  93. /// @params flags
  94. void Iface::setFlags(uint64_t flags) {
  95. flags_ = flags;
  96. flag_loopback_ = flags & IFF_LOOPBACK;
  97. flag_up_ = flags & IFF_UP;
  98. flag_running_ = flags & IFF_RUNNING;
  99. flag_multicast_ = flags & IFF_MULTICAST;
  100. flag_broadcast_ = flags & IFF_BROADCAST;
  101. }
  102. void
  103. IfaceMgr::setMatchingPacketFilter(const bool direct_response_desired) {
  104. // If direct response is desired we have to use BPF. If the direct
  105. // response is not desired we use datagram socket supported by the
  106. // PktFilterInet class. Note however that on BSD systems binding the
  107. // datagram socket to the device is not supported and the server would
  108. // have no means to determine on which interface the packet has been
  109. // received. Hence, it is discouraged to use PktFilterInet for the
  110. // server.
  111. if (direct_response_desired) {
  112. setPacketFilter(PktFilterPtr(new PktFilterBPF()));
  113. } else {
  114. setPacketFilter(PktFilterPtr(new PktFilterInet()));
  115. }
  116. }
  117. bool
  118. IfaceMgr::openMulticastSocket(Iface& iface,
  119. const isc::asiolink::IOAddress& addr,
  120. const uint16_t port,
  121. IfaceMgrErrorMsgCallback error_handler) {
  122. try {
  123. // This should open a socket, bind it to link-local address
  124. // and join multicast group.
  125. openSocket(iface.getName(), addr, port, iface.flag_multicast_);
  126. } catch (const Exception& ex) {
  127. IFACEMGR_ERROR(SocketConfigError, error_handler,
  128. "Failed to open link-local socket on "
  129. " interface " << iface.getName() << ": "
  130. << ex.what());
  131. return (false);
  132. }
  133. return (true);
  134. }
  135. int
  136. IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, uint16_t port,
  137. const bool join_multicast) {
  138. // On BSD, we bind the socket to in6addr_any and join multicast group
  139. // to receive multicast traffic. So, if the multicast is requested,
  140. // replace the address specified by the caller with the "unspecified"
  141. // address.
  142. IOAddress actual_address = join_multicast ? IOAddress("::") : addr;
  143. SocketInfo info = packet_filter6_->openSocket(iface, actual_address, port,
  144. join_multicast);
  145. iface.addSocket(info);
  146. return (info.sockfd_);
  147. }
  148. } // end of isc::dhcp namespace
  149. } // end of dhcp namespace
  150. #endif