iface_mgr_sun.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2011, 2013-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #if defined(OS_SUN)
  16. #include <dhcp/iface_mgr.h>
  17. #include <dhcp/iface_mgr_error_handler.h>
  18. #include <dhcp/pkt_filter_inet.h>
  19. #include <exceptions/exceptions.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <net/if_dl.h>
  23. #include <net/if.h>
  24. #include <ifaddrs.h>
  25. using namespace std;
  26. using namespace isc;
  27. using namespace isc::asiolink;
  28. using namespace isc::dhcp;
  29. namespace isc {
  30. namespace dhcp {
  31. /// This is a Solaris specific interface detection code. It works on Solaris 11
  32. /// only, as earlier versions did not support getifaddrs() API.
  33. void
  34. IfaceMgr::detectIfaces() {
  35. struct ifaddrs * iflist = 0, * ifptr = 0;
  36. // Gets list of ifaddrs struct
  37. if(getifaddrs(& iflist) != 0) {
  38. isc_throw(Unexpected, "Network interfaces detection failed.");
  39. }
  40. typedef std::map<string, Iface> ifaceLst;
  41. ifaceLst::iterator iface_iter;
  42. ifaceLst ifaces;
  43. // First lookup for getting interfaces ...
  44. for(ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
  45. const char * ifname = ifptr->ifa_name;
  46. uint ifindex = 0;
  47. if (!(ifindex = if_nametoindex(ifname))) {
  48. // Interface name does not have corresponding index ...
  49. freeifaddrs(iflist);
  50. isc_throw(Unexpected, "Interface " << ifname << " has no index");
  51. }
  52. iface_iter = ifaces.find(ifname);
  53. if (iface_iter != ifaces.end()) {
  54. continue;
  55. }
  56. Iface iface(ifname, ifindex);
  57. iface.setFlags(ifptr->ifa_flags);
  58. ifaces.insert(pair<string, Iface>(ifname, iface));
  59. }
  60. // Second lookup to get MAC and IP addresses
  61. for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
  62. iface_iter = ifaces.find(ifptr->ifa_name);
  63. if (iface_iter == ifaces.end()) {
  64. continue;
  65. }
  66. // Common byte pointer for following data
  67. const uint8_t * ptr = 0;
  68. if (ifptr->ifa_addr->sa_family == AF_LINK) {
  69. // HWAddr
  70. struct sockaddr_dl * ldata =
  71. reinterpret_cast<struct sockaddr_dl *>(ifptr->ifa_addr);
  72. ptr = reinterpret_cast<uint8_t *>(LLADDR(ldata));
  73. iface_iter->second.setHWType(ldata->sdl_type);
  74. iface_iter->second.setMac(ptr, ldata->sdl_alen);
  75. } else if (ifptr->ifa_addr->sa_family == AF_INET6) {
  76. // IPv6 Addr
  77. struct sockaddr_in6 * adata =
  78. reinterpret_cast<struct sockaddr_in6 *>(ifptr->ifa_addr);
  79. ptr = reinterpret_cast<uint8_t *>(& adata->sin6_addr);
  80. IOAddress a = IOAddress::fromBytes(AF_INET6, ptr);
  81. iface_iter->second.addAddress(a);
  82. } else {
  83. // IPv4 Addr
  84. struct sockaddr_in * adata =
  85. reinterpret_cast<struct sockaddr_in *>(ifptr->ifa_addr);
  86. ptr = reinterpret_cast<uint8_t *>(& adata->sin_addr);
  87. IOAddress a = IOAddress::fromBytes(AF_INET, ptr);
  88. iface_iter->second.addAddress(a);
  89. }
  90. }
  91. freeifaddrs(iflist);
  92. // Interfaces registering
  93. for (ifaceLst::const_iterator iface_iter = ifaces.begin();
  94. iface_iter != ifaces.end(); ++iface_iter) {
  95. ifaces_.push_back(iface_iter->second);
  96. }
  97. }
  98. /// @brief sets flag_*_ fields
  99. ///
  100. /// Like Linux version, os specific flags
  101. ///
  102. /// @params flags
  103. void Iface::setFlags(uint64_t flags) {
  104. flags_ = flags;
  105. flag_loopback_ = flags & IFF_LOOPBACK;
  106. flag_up_ = flags & IFF_UP;
  107. flag_running_ = flags & IFF_RUNNING;
  108. flag_multicast_ = flags & IFF_MULTICAST;
  109. flag_broadcast_ = flags & IFF_BROADCAST;
  110. }
  111. void IfaceMgr::os_send4(struct msghdr& /*m*/,
  112. boost::scoped_array<char>& /*control_buf*/,
  113. size_t /*control_buf_len*/,
  114. const Pkt4Ptr& /*pkt*/) {
  115. // @todo: Are there any specific actions required before sending IPv4 packet
  116. // on Solaris based systems? See iface_mgr_linux.cc
  117. // for working Linux implementation.
  118. }
  119. bool IfaceMgr::os_receive4(struct msghdr& /*m*/, Pkt4Ptr& /*pkt*/) {
  120. // @todo: Are there any specific actions required before receiving IPv4 packet
  121. // on BSDs? See iface_mgr_linux.cc for working Linux implementation.
  122. return (true); // pretend that we have everything set up for reception.
  123. }
  124. void
  125. IfaceMgr::setMatchingPacketFilter(const bool /* direct_response_desired */) {
  126. // @todo Currently we ignore the preference to use direct traffic
  127. // because it hasn't been implemented for Solaris.
  128. setPacketFilter(PktFilterPtr(new PktFilterInet()));
  129. }
  130. bool
  131. IfaceMgr::openMulticastSocket(Iface& iface,
  132. const isc::asiolink::IOAddress& addr,
  133. const uint16_t port,
  134. IfaceMgrErrorMsgCallback error_handler) {
  135. try {
  136. // This should open a socket, bound it to link-local address
  137. // and join multicast group.
  138. openSocket(iface.getName(), addr, port,
  139. iface.flag_multicast_);
  140. } catch (const Exception& ex) {
  141. IFACEMGR_ERROR(SocketConfigError, error_handler,
  142. "Failed to open link-local socket on "
  143. " interface " << iface.getName() << ": "
  144. << ex.what());
  145. return (false);
  146. }
  147. return (true);
  148. }
  149. int
  150. IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, uint16_t port,
  151. const bool join_multicast) {
  152. IOAddress actual_address = join_multicast ? IOAddress("::") : addr;
  153. SocketInfo info = packet_filter6_->openSocket(iface, actual_address, port,
  154. join_multicast);
  155. iface.addSocket(info);
  156. return (info.sockfd_);
  157. }
  158. } // end of isc::dhcp namespace
  159. } // end of dhcp namespace
  160. #endif