hwaddr.h 6.0 KB

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