hwaddr.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 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. #ifndef HWADDR_H
  15. #define HWADDR_H
  16. #include <vector>
  17. #include <stdint.h>
  18. #include <stddef.h>
  19. #include <dhcp/dhcp4.h>
  20. #include <boost/shared_ptr.hpp>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief Hardware type that represents information from DHCPv4 packet
  24. struct HWAddr {
  25. public:
  26. /// @brief Size of an ethernet hardware address.
  27. static const size_t ETHERNET_HWADDR_LEN = 6;
  28. /// @brief Maximum size of a hardware address.
  29. static const size_t MAX_HWADDR_LEN = 20;
  30. /// @defgroup hw_sources Specifies where a given MAC/hardware address was
  31. /// obtained.
  32. ///
  33. /// @brief The list covers all possible MAC/hw address sources.
  34. ///
  35. /// @{
  36. /// Not really a type, only used in getMAC() calls.
  37. static const uint32_t HWADDR_SOURCE_ANY;
  38. /// Used when actual origin is not known, e.g. when reading from a
  39. /// lease database that didn't store that information.
  40. static const uint32_t HWADDR_SOURCE_UNKNOWN;
  41. /// Obtained first hand from raw socket (100% reliable).
  42. static const uint32_t HWADDR_SOURCE_RAW;
  43. /// Extracted from DUID-LL or DUID-LLT (not 100% reliable as the client
  44. /// can send fake DUID).
  45. static const uint32_t HWADDR_SOURCE_DUID;
  46. /// Extracted from IPv6 link-local address. Not 100% reliable, as the
  47. /// client can use different IID other than EUI-64, e.g. Windows supports
  48. /// RFC4941 and uses random values instead of EUI-64.
  49. static const uint32_t HWADDR_SOURCE_IPV6_LINK_LOCAL;
  50. /// Get it from RFC6939 option. (A relay agent can insert client link layer
  51. /// address option). Note that a skilled attacker can fake that by sending
  52. /// his request relayed, so the legitimate relay will think it's a second
  53. /// relay.
  54. static const uint32_t HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION;
  55. /// A relay can insert remote-id. In some deployments it contains a MAC
  56. /// address (RFC4649).
  57. static const uint32_t HWADDR_SOURCE_REMOTE_ID;
  58. /// A relay can insert a subscriber-id option. In some deployments it
  59. /// contains a MAC address (RFC4580).
  60. static const uint32_t HWADDR_SOURCE_SUBSCRIBER_ID;
  61. /// A CMTS (acting as DHCP relay agent) that supports DOCSIS standard
  62. /// can insert DOCSIS options that contain client's MAC address.
  63. /// This specific option is suboption 1026 in vendor-class option with
  64. /// vendor-id=4491. Client in this context would be a cable modem.
  65. static const uint32_t HWADDR_SOURCE_DOCSIS_CMTS;
  66. /// A cable modem (acting as DHCP client) that supports DOCSIS standard
  67. /// can insert DOCSIS options that contain client's MAC address.
  68. /// This specific option is suboption 36 in vendor-class option with
  69. /// vendor-id=4491.
  70. static const uint32_t HWADDR_SOURCE_DOCSIS_MODEM;
  71. /// @}
  72. /// @brief default constructor
  73. HWAddr();
  74. /// @brief constructor, based on C-style pointer and length
  75. /// @param hwaddr pointer to hardware address
  76. /// @param len length of the address pointed by hwaddr
  77. /// @param htype hardware type
  78. HWAddr(const uint8_t* hwaddr, size_t len, uint16_t htype);
  79. /// @brief constructor, based on C++ vector<uint8_t>
  80. /// @param hwaddr const reference to hardware address
  81. /// @param htype hardware type
  82. HWAddr(const std::vector<uint8_t>& hwaddr, uint16_t htype);
  83. // Vector that keeps the actual hardware address
  84. std::vector<uint8_t> hwaddr_;
  85. /// Hardware type
  86. ///
  87. /// @note It used to be uint8_t as used in DHCPv4. However, since we're
  88. /// expanding MAC addresses support to DHCPv6 that uses hw_type as
  89. /// 16 bits, we need to be able to store that wider format.
  90. uint16_t htype_;
  91. /// @brief Hardware address source
  92. ///
  93. /// This variable specifies how the hardware address was obtained.
  94. /// @todo This is a stub implementation. Proper implementation will move
  95. /// constants from Pkt::HWADDR_SOURCE_* here. Currently always initialized
  96. /// to zero.
  97. uint32_t source_;
  98. /// @brief Returns textual representation of a hardware address
  99. /// (e.g. 00:01:02:03:04:05)
  100. ///
  101. /// @param include_htype Boolean value which controls whether the hardware
  102. /// type is included in the returned string (true), or not (false).
  103. ///
  104. /// @return Hardware address in the textual format.
  105. std::string toText(bool include_htype = true) const;
  106. /// @brief Creates instance of the hardware address from textual format.
  107. ///
  108. /// This function parses HW address specified as text and creates the
  109. /// corresponding @c HWAddr instance. The hexadecimal digits representing
  110. /// individual bytes of the hardware address should be separated with
  111. /// colons. Typically, two digits per byte are used. However, this function
  112. /// allows for 1 digit per HW address byte. In this case, the digit is
  113. /// prepended with '0' during conversion to binary value.
  114. ///
  115. /// This function can be used to perform a reverse operation to the
  116. /// @c HWAddr::toText(false).
  117. ///
  118. /// The instance created by this function sets HTYPE_ETHER as a hardware
  119. /// type.
  120. ///
  121. /// @param text HW address in the textual format.
  122. /// @param htype Hardware type.
  123. ///
  124. /// @return Instance of the HW address created from text.
  125. static HWAddr fromText(const std::string& text,
  126. const uint16_t htype = HTYPE_ETHER);
  127. /// @brief Compares two hardware addresses for equality
  128. bool operator==(const HWAddr& other) const;
  129. /// @brief Compares two hardware addresses for inequality
  130. bool operator!=(const HWAddr& other) const;
  131. };
  132. /// @brief Shared pointer to a hardware address structure
  133. typedef boost::shared_ptr<HWAddr> HWAddrPtr;
  134. }; // end of isc::dhcp namespace
  135. }; // end of isc namespace
  136. #endif // HWADDR_H