EthernetTap.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * Put a frame, making it available to the OS for processing
  103. *
  104. * @param from MAC address from which frame originated
  105. * @param to MAC address of destination (typically MAC of tap itself)
  106. * @param etherType Ethernet protocol ID
  107. * @param data Frame payload
  108. * @param len Length of frame
  109. */
  110. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
  111. /**
  112. * @return OS-specific device or connection name (e.g. zt0, tap0, etc.)
  113. */
  114. virtual std::string deviceName() const = 0;
  115. /**
  116. * Change this device's user-visible name (if supported)
  117. *
  118. * @param friendlyName New name
  119. */
  120. virtual void setFriendlyName(const char *friendlyName) = 0;
  121. /**
  122. * Fill or modify a set to contain multicast groups for this device
  123. *
  124. * This populates a set or, if already populated, modifies it to contain
  125. * only multicast groups in which this device is interested.
  126. *
  127. * This neither includes nor removes the broadcast (ff:ff:ff:ff:ff:ff / 0)
  128. * group.
  129. *
  130. * @param groups Set to modify in place
  131. * @return True if set was changed since last call
  132. */
  133. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups) = 0;
  134. /**
  135. * Inject a packet as if it was sent by the host, if supported
  136. *
  137. * This is for testing and is typically not supported by real TAP devices.
  138. * It's implemented by TestEthernetTap in testnet.
  139. *
  140. * @param from Source MAC
  141. * @param to Destination MAC
  142. * @param etherType Ethernet frame type
  143. * @param data Packet data
  144. * @param len Packet length
  145. * @return False if not supported or packet too large
  146. */
  147. virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
  148. protected:
  149. const char *_implName;
  150. MAC _mac;
  151. unsigned int _mtu;
  152. unsigned int _metric;
  153. };
  154. } // namespace ZeroTier
  155. #endif