EthernetTap.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ETHERNETTAP_HPP
  28. #define ZT_ETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string>
  32. #include <set>
  33. #include "Constants.hpp"
  34. #include "MAC.hpp"
  35. #include "InetAddress.hpp"
  36. #include "Buffer.hpp"
  37. #include "MulticastGroup.hpp"
  38. #include "NonCopyable.hpp"
  39. namespace ZeroTier {
  40. /**
  41. * Base class for Ethernet tap device implementations
  42. */
  43. class EthernetTap : NonCopyable
  44. {
  45. protected:
  46. EthernetTap(const char *cn,const MAC &m,unsigned int mt,unsigned int met) :
  47. _implName(cn),
  48. _mac(m),
  49. _mtu(mt),
  50. _metric(met) {}
  51. public:
  52. virtual ~EthernetTap() {}
  53. /**
  54. * @return Implementation class name (e.g. UnixEthernetTap)
  55. */
  56. inline const char *implementationName() const { return _implName; }
  57. /**
  58. * Sets whether device is 'up'
  59. *
  60. * This may do nothing on some platforms.
  61. *
  62. * @param en Is device enabled?
  63. */
  64. virtual void setEnabled(bool en) = 0;
  65. /**
  66. * @return Is device 'up'?
  67. */
  68. virtual bool enabled() const = 0;
  69. /**
  70. * @return MAC address of this interface
  71. */
  72. inline const MAC &mac() const throw() { return _mac; }
  73. /**
  74. * @return MTU of this interface
  75. */
  76. inline unsigned int mtu() const throw() { return _mtu; }
  77. /**
  78. * @return Interface metric
  79. */
  80. inline unsigned int metric() const throw() { return _metric; }
  81. /**
  82. * Add an IP to this interface
  83. *
  84. * @param ip IP and netmask (netmask stored in port field)
  85. * @return True if IP added successfully
  86. */
  87. virtual bool addIP(const InetAddress &ip) = 0;
  88. /**
  89. * Remove an IP from this interface
  90. *
  91. * Link-local IP addresses may not be able to be removed, depending on platform and type.
  92. *
  93. * @param ip IP and netmask (netmask stored in port field)
  94. * @return True if IP removed successfully
  95. */
  96. virtual bool removeIP(const InetAddress &ip) = 0;
  97. /**
  98. * @return All IP addresses (V4 and V6) assigned to this interface (including link-local)
  99. */
  100. virtual std::set<InetAddress> ips() const = 0;
  101. /**
  102. * Set this tap's IP addresses to exactly this set of IPs
  103. *
  104. * New IPs are created. Any IP that overlaps with the network of an IP in
  105. * this list is removed, but other IPs are left intact.
  106. *
  107. * @param ips IP addresses with netmask in port field
  108. */
  109. inline void setIps(const std::set<InetAddress> &allIps)
  110. {
  111. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  112. addIP(*i);
  113. std::set<InetAddress> myIps(ips());
  114. #ifdef __APPLE__
  115. bool haveV6LinkLocal = false;
  116. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  117. if (i->isLinkLocal()) {
  118. if (i->isV6())
  119. haveV6LinkLocal = true;
  120. } else if (!allIps.count(*i)) {
  121. for(std::set<InetAddress>::const_iterator i2(allIps.begin());i2!=allIps.end();++i2) {
  122. if (i->sameNetworkAs(*i2)) {
  123. removeIP(*i);
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. if (!haveV6LinkLocal)
  130. addIP(InetAddress::makeIpv6LinkLocal(_mac));
  131. #else
  132. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  133. if ((!i->isLinkLocal())&&(!allIps.count(*i))) {
  134. for(std::set<InetAddress>::const_iterator i2(allIps.begin());i2!=allIps.end();++i2) {
  135. if (i->sameNetworkAs(*i2)) {
  136. removeIP(*i);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. #endif
  143. }
  144. /**
  145. * Put a frame, making it available to the OS for processing
  146. *
  147. * @param from MAC address from which frame originated
  148. * @param to MAC address of destination (typically MAC of tap itself)
  149. * @param etherType Ethernet protocol ID
  150. * @param data Frame payload
  151. * @param len Length of frame
  152. */
  153. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
  154. /**
  155. * @return OS-specific device or connection name (e.g. zt0, tap0, etc.)
  156. */
  157. virtual std::string deviceName() const = 0;
  158. /**
  159. * Change this device's user-visible name (if supported)
  160. *
  161. * @param friendlyName New name
  162. */
  163. virtual void setFriendlyName(const char *friendlyName) = 0;
  164. /**
  165. * Fill or modify a set to contain multicast groups for this device
  166. *
  167. * This populates a set or, if already populated, modifies it to contain
  168. * only multicast groups in which this device is interested.
  169. *
  170. * This neither includes nor removes the broadcast (ff:ff:ff:ff:ff:ff / 0)
  171. * group.
  172. *
  173. * @param groups Set to modify in place
  174. * @return True if set was changed since last call
  175. */
  176. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups) = 0;
  177. /**
  178. * Should this tap device get a pseudo-default-route?
  179. *
  180. * Some platforms (cough Windows) want all "real" network devices to have a
  181. * routing table entry for default, even if it's got a high metric and is
  182. * never used and goes nowhere. If this returns true, the underlying node
  183. * code will use RoutingTable to create one if no default route is
  184. * otherwise defined.
  185. *
  186. * Base class default returns false. Override to return true if needed.
  187. *
  188. * @return True if pseudo-default-route should always exist
  189. */
  190. virtual bool createPseudoDefaultRoute() const { return false; }
  191. protected:
  192. const char *_implName;
  193. MAC _mac;
  194. unsigned int _mtu;
  195. unsigned int _metric;
  196. };
  197. } // namespace ZeroTier
  198. #endif