NetconEthernetTap.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_NETCONETHERNETTAP_HPP
  28. #define ZT_NETCONETHERNETTAP_HPP
  29. #ifdef ZT_ENABLE_NETCON
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string>
  33. #include <vector>
  34. #include <stdexcept>
  35. #include "../node/Constants.hpp"
  36. #include "../node/MulticastGroup.hpp"
  37. #include "../node/Mutex.hpp"
  38. #include "../node/InetAddress.hpp"
  39. #include "../osdep/Thread.hpp"
  40. #include "../osdep/Phy.hpp"
  41. #include "NetconService.hpp"
  42. #include "NetconUtilities.hpp"
  43. #include "netif/etharp.h"
  44. namespace ZeroTier {
  45. class NetconEthernetTap;
  46. /**
  47. * Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
  48. */
  49. class NetconEthernetTap
  50. {
  51. friend class Phy<NetconEthernetTap *>;
  52. public:
  53. NetconEthernetTap(
  54. const char *homePath,
  55. const MAC &mac,
  56. unsigned int mtu,
  57. unsigned int metric,
  58. uint64_t nwid,
  59. const char *friendlyName,
  60. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  61. void *arg);
  62. ~NetconEthernetTap();
  63. void setEnabled(bool en);
  64. bool enabled() const;
  65. bool addIp(const InetAddress &ip);
  66. bool removeIp(const InetAddress &ip);
  67. std::vector<InetAddress> ips() const;
  68. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  69. std::string deviceName() const;
  70. void setFriendlyName(const char *friendlyName);
  71. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  72. void threadMain()
  73. throw();
  74. LWIPStack *lwipstack;
  75. uint64_t _nwid;
  76. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  77. void *_arg;
  78. private:
  79. // LWIP callbacks
  80. static err_t nc_poll(void* arg, struct tcp_pcb *tpcb);
  81. static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
  82. static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
  83. static void nc_err(void *arg, err_t err);
  84. static void nc_close(struct tcp_pcb *tpcb);
  85. static err_t nc_send(struct tcp_pcb *tpcb);
  86. static err_t nc_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
  87. static err_t nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
  88. // RPC handlers (from NetconIntercept)
  89. void unload_rpc(void *data, pid_t &pid, pid_t &tid, int &rpc_count, char (timestamp[20]), char &cmd, void* &payload);
  90. void handle_bind(PhySocket *sock, void **uptr, struct bind_st *bind_rpc);
  91. void handle_listen(PhySocket *sock, void **uptr, struct listen_st *listen_rpc);
  92. void handle_map_request(PhySocket *sock, void **uptr, unsigned char* buf);
  93. void handle_retval(PhySocket *sock, void **uptr, int rpc_count, int newfd);
  94. TcpConnection * handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc);
  95. void handle_connect(PhySocket *sock, void **uptr, struct connect_st* connect_rpc);
  96. void handle_write(TcpConnection *conn);
  97. int send_return_value(TcpConnection *conn, int retval, int _errno);
  98. int send_return_value(int fd, int retval, int _errno);
  99. void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
  100. void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
  101. void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
  102. void phyOnTcpClose(PhySocket *sock,void **uptr);
  103. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  104. void phyOnTcpWritable(PhySocket *sock,void **uptr);
  105. void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
  106. void phyOnUnixClose(PhySocket *sock,void **uptr);
  107. void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  108. void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable);
  109. ip_addr_t convert_ip(struct sockaddr_in * addr)
  110. {
  111. ip_addr_t conn_addr;
  112. struct sockaddr_in *ipv4 = addr;
  113. short a = ip4_addr1(&(ipv4->sin_addr));
  114. short b = ip4_addr2(&(ipv4->sin_addr));
  115. short c = ip4_addr3(&(ipv4->sin_addr));
  116. short d = ip4_addr4(&(ipv4->sin_addr));
  117. IP4_ADDR(&conn_addr, a,b,c,d);
  118. return conn_addr;
  119. }
  120. // Client helpers
  121. TcpConnection *getConnectionByTheirFD(PhySocket *sock, int fd);
  122. void closeConnection(TcpConnection *conn);
  123. void closeAll();
  124. void closeClient(PhySocket *sock);
  125. void compact_dump();
  126. void dump();
  127. void die(int exret);
  128. Phy<NetconEthernetTap *> _phy;
  129. PhySocket *_unixListenSocket;
  130. std::vector<TcpConnection*> tcp_connections;
  131. std::vector<PhySocket*> rpc_sockets;
  132. std::map<PhySocket*, pid_t> pidmap;
  133. pid_t rpc_counter;
  134. netif interface;
  135. MAC _mac;
  136. Thread _thread;
  137. std::string _homePath;
  138. std::string _dev; // path to Unix domain socket
  139. std::vector<MulticastGroup> _multicastGroups;
  140. Mutex _multicastGroups_m;
  141. std::vector<InetAddress> _ips;
  142. Mutex _ips_m;
  143. unsigned int _mtu;
  144. volatile bool _enabled;
  145. volatile bool _run;
  146. };
  147. /*------------------------------------------------------------------------------
  148. ------------------------ low-level Interface functions -------------------------
  149. ------------------------------------------------------------------------------*/
  150. static err_t low_level_output(struct netif *netif, struct pbuf *p);
  151. static err_t tapif_init(struct netif *netif)
  152. {
  153. // Actual init functionality is in addIp() of tap
  154. return ERR_OK;
  155. }
  156. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  157. {
  158. struct pbuf *q;
  159. char buf[ZT_MAX_MTU+32];
  160. char *bufptr;
  161. int tot_len = 0;
  162. ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
  163. /* initiate transfer(); */
  164. bufptr = buf;
  165. for(q = p; q != NULL; q = q->next) {
  166. /* Send the data from the pbuf to the interface, one pbuf at a
  167. time. The size of the data in each pbuf is kept in the ->len
  168. variable. */
  169. /* send data from(q->payload, q->len); */
  170. memcpy(bufptr, q->payload, q->len);
  171. bufptr += q->len;
  172. tot_len += q->len;
  173. }
  174. // [Send packet to network]
  175. // Split ethernet header and feed into handler
  176. struct eth_hdr *ethhdr;
  177. ethhdr = (struct eth_hdr *)buf;
  178. ZeroTier::MAC src_mac;
  179. ZeroTier::MAC dest_mac;
  180. src_mac.setTo(ethhdr->src.addr, 6);
  181. dest_mac.setTo(ethhdr->dest.addr, 6);
  182. tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,
  183. Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),tot_len - sizeof(struct eth_hdr));
  184. return ERR_OK;
  185. }
  186. } // namespace ZeroTier
  187. #endif // ZT_ENABLE_NETCON
  188. #endif