MulticastGroup.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MULTICASTGROUP_HPP
  14. #define ZT_MULTICASTGROUP_HPP
  15. #include <stdint.h>
  16. #include "Constants.hpp"
  17. #include "MAC.hpp"
  18. #include "InetAddress.hpp"
  19. #include "Utils.hpp"
  20. namespace ZeroTier {
  21. /**
  22. * A multicast group composed of a multicast MAC and a 32-bit ADI field
  23. *
  24. * ADI stands for additional distinguishing information. ADI is primarily for
  25. * adding additional information to broadcast (ff:ff:ff:ff:ff:ff) memberships,
  26. * since straight-up broadcast won't scale. Right now it's zero except for
  27. * IPv4 ARP, where it holds the IPv4 address itself to make ARP into a
  28. * selective multicast query that can scale.
  29. *
  30. * In the future we might add some kind of plugin architecture that can add
  31. * ADI for things like mDNS (multicast DNS) to improve the selectivity of
  32. * those protocols.
  33. *
  34. * MulticastGroup behaves as an immutable value object.
  35. */
  36. class MulticastGroup
  37. {
  38. public:
  39. ZT_ALWAYS_INLINE MulticastGroup() :
  40. _mac(),
  41. _adi(0) {}
  42. ZT_ALWAYS_INLINE MulticastGroup(const MAC &m,uint32_t a) :
  43. _mac(m),
  44. _adi(a) {}
  45. /**
  46. * Derive the multicast group used for address resolution (ARP/NDP) for an IP
  47. *
  48. * @param ip IP address (port field is ignored)
  49. * @return Multicast group for ARP/NDP
  50. */
  51. static ZT_ALWAYS_INLINE MulticastGroup deriveMulticastGroupForAddressResolution(const InetAddress &ip)
  52. {
  53. if (ip.isV4()) {
  54. // IPv4 wants broadcast MACs, so we shove the V4 address itself into
  55. // the Multicast Group ADI field. Making V4 ARP work is basically why
  56. // ADI was added, as well as handling other things that want mindless
  57. // Ethernet broadcast to all.
  58. return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
  59. } else if (ip.isV6()) {
  60. // IPv6 is better designed in this respect. We can compute the IPv6
  61. // multicast address directly from the IP address, and it gives us
  62. // 24 bits of uniqueness. Collisions aren't likely to be common enough
  63. // to care about.
  64. const unsigned char *a = (const unsigned char *)ip.rawIpData();
  65. return MulticastGroup(MAC(0x33,0x33,0xff,a[13],a[14],a[15]),0);
  66. }
  67. return MulticastGroup();
  68. }
  69. ZT_ALWAYS_INLINE const MAC &mac() const { return _mac; }
  70. ZT_ALWAYS_INLINE uint32_t adi() const { return _adi; }
  71. ZT_ALWAYS_INLINE unsigned long hashCode() const { return (_mac.hashCode() + (unsigned long)_adi); }
  72. ZT_ALWAYS_INLINE bool operator==(const MulticastGroup &g) const { return ((_mac == g._mac)&&(_adi == g._adi)); }
  73. ZT_ALWAYS_INLINE bool operator!=(const MulticastGroup &g) const { return ((_mac != g._mac)||(_adi != g._adi)); }
  74. ZT_ALWAYS_INLINE bool operator<(const MulticastGroup &g) const
  75. {
  76. if (_mac < g._mac)
  77. return true;
  78. else if (_mac == g._mac)
  79. return (_adi < g._adi);
  80. return false;
  81. }
  82. ZT_ALWAYS_INLINE bool operator>(const MulticastGroup &g) const { return (g < *this); }
  83. ZT_ALWAYS_INLINE bool operator<=(const MulticastGroup &g) const { return !(g < *this); }
  84. ZT_ALWAYS_INLINE bool operator>=(const MulticastGroup &g) const { return !(*this < g); }
  85. /**
  86. * Compute a 32-bit fnv1a hash of a multicast group and a network ID
  87. *
  88. * @param mg Multicast group
  89. * @param nwid Network ID
  90. * @return 32-bit relatively-unique ID
  91. */
  92. static ZT_ALWAYS_INLINE uint32_t id(const MulticastGroup &mg,const uint64_t nwid)
  93. {
  94. const uint32_t fnv1aPrime = 0x01000193;
  95. uint32_t i = 0x811c9dc5;
  96. i = (((uint32_t)(nwid >> 56) & 0xff) ^ i) * fnv1aPrime;
  97. i = (((uint32_t)(nwid >> 48) & 0xff) ^ i) * fnv1aPrime;
  98. i = (((uint32_t)(nwid >> 40) & 0xff) ^ i) * fnv1aPrime;
  99. i = (((uint32_t)(nwid >> 32) & 0xff) ^ i) * fnv1aPrime;
  100. i = (((uint32_t)(nwid >> 24) & 0xff) ^ i) * fnv1aPrime;
  101. i = (((uint32_t)(nwid >> 16) & 0xff) ^ i) * fnv1aPrime;
  102. i = (((uint32_t)(nwid >> 8) & 0xff) ^ i) * fnv1aPrime;
  103. i = (((uint32_t)nwid & 0xff) ^ i) * fnv1aPrime;
  104. const uint64_t mac = mg._mac.toInt();
  105. i = (((uint32_t)(mac >> 56) & 0xff) ^ i) * fnv1aPrime;
  106. i = (((uint32_t)(mac >> 48) & 0xff) ^ i) * fnv1aPrime;
  107. i = (((uint32_t)(mac >> 40) & 0xff) ^ i) * fnv1aPrime;
  108. i = (((uint32_t)(mac >> 32) & 0xff) ^ i) * fnv1aPrime;
  109. i = (((uint32_t)(mac >> 24) & 0xff) ^ i) * fnv1aPrime;
  110. i = (((uint32_t)(mac >> 16) & 0xff) ^ i) * fnv1aPrime;
  111. i = (((uint32_t)(mac >> 8) & 0xff) ^ i) * fnv1aPrime;
  112. i = (((uint32_t)mac & 0xff) ^ i) * fnv1aPrime;
  113. const uint32_t adi = mg._adi;
  114. i = (((adi >> 24) & 0xff) ^ i) * fnv1aPrime;
  115. i = (((adi >> 16) & 0xff) ^ i) * fnv1aPrime;
  116. i = (((adi >> 8) & 0xff) ^ i) * fnv1aPrime;
  117. i = ((adi & 0xff) ^ i) * fnv1aPrime;
  118. return i;
  119. }
  120. private:
  121. MAC _mac;
  122. uint32_t _adi;
  123. };
  124. } // namespace ZeroTier
  125. #endif