NetconEthernetTap.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "../osdep/Arp.hpp"
  42. #include "NetconService.hpp"
  43. #include "NetconUtilities.hpp"
  44. #include "netif/etharp.h"
  45. struct tapif {
  46. struct eth_addr *ethaddr;
  47. /* Add whatever per-interface state that is needed here. */
  48. int fd;
  49. };
  50. namespace ZeroTier {
  51. class NetconEthernetTap;
  52. /**
  53. * Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
  54. */
  55. class NetconEthernetTap
  56. {
  57. friend class Phy<NetconEthernetTap *>;
  58. public:
  59. NetconEthernetTap(
  60. const char *homePath,
  61. const MAC &mac,
  62. unsigned int mtu,
  63. unsigned int metric,
  64. uint64_t nwid,
  65. const char *friendlyName,
  66. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  67. void *arg);
  68. ~NetconEthernetTap();
  69. void setEnabled(bool en);
  70. bool enabled() const;
  71. bool addIp(const InetAddress &ip);
  72. bool removeIp(const InetAddress &ip);
  73. std::vector<InetAddress> ips() const;
  74. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  75. std::string deviceName() const;
  76. void setFriendlyName(const char *friendlyName);
  77. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  78. void threadMain()
  79. throw();
  80. LWIPStack *lwipstack;
  81. uint64_t _nwid;
  82. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  83. void *_arg;
  84. private:
  85. // LWIP callbacks
  86. static err_t nc_poll(void* arg, struct tcp_pcb *tpcb);
  87. static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
  88. static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
  89. static void nc_err(void *arg, err_t err);
  90. static void nc_close(struct tcp_pcb *tpcb);
  91. static err_t nc_send(struct tcp_pcb *tpcb);
  92. static err_t nc_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
  93. static err_t nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
  94. // RPC handlers (from NetconIntercept)
  95. void handle_bind(NetconClient *client, struct bind_st *bind_rpc);
  96. void handle_listen(NetconClient *client, struct listen_st *listen_rpc);
  97. void handle_retval(NetconClient *client, unsigned char* buf);
  98. void handle_socket(NetconClient *client, struct socket_st* socket_rpc);
  99. void handle_connect(NetconClient *client, struct connect_st* connect_rpc);
  100. void handle_write(NetconConnection *c);
  101. void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
  102. void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
  103. void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
  104. void phyOnTcpClose(PhySocket *sock,void **uptr);
  105. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  106. void phyOnTcpWritable(PhySocket *sock,void **uptr);
  107. void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
  108. void phyOnUnixClose(PhySocket *sock,void **uptr);
  109. void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  110. void phyOnUnixWritable(PhySocket *sock,void **uptr);
  111. void phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr);
  112. void phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n);
  113. void phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr);
  114. int send_return_value(NetconClient *client, int retval);
  115. ip_addr_t convert_ip(struct sockaddr_in * addr)
  116. {
  117. ip_addr_t conn_addr;
  118. struct sockaddr_in *ipv4 = addr;
  119. short a = ip4_addr1(&(ipv4->sin_addr));
  120. short b = ip4_addr2(&(ipv4->sin_addr));
  121. short c = ip4_addr3(&(ipv4->sin_addr));
  122. short d = ip4_addr4(&(ipv4->sin_addr));
  123. IP4_ADDR(&conn_addr, a,b,c,d);
  124. return conn_addr;
  125. }
  126. // Client helpers
  127. NetconConnection *getConnectionByThisFD(int fd);
  128. NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb);
  129. NetconClient *getClientByPCB(struct tcp_pcb *pcb);
  130. void closeClient(NetconClient *client);
  131. void closeConnection(NetconConnection *conn);
  132. Phy<NetconEthernetTap *> _phy;
  133. PhySocket *_unixListenSocket;
  134. std::vector<NetconClient*> clients;
  135. netif interface;
  136. MAC _mac;
  137. Thread _thread;
  138. std::string _homePath;
  139. std::string _dev; // path to Unix domain socket
  140. Arp _arp;
  141. Mutex _arp_m;
  142. std::vector<MulticastGroup> _multicastGroups;
  143. Mutex _multicastGroups_m;
  144. std::vector<InetAddress> _ips;
  145. Mutex _ips_m;
  146. unsigned int _mtu;
  147. volatile bool _enabled;
  148. volatile bool _run;
  149. };
  150. /*------------------------------------------------------------------------------
  151. ------------------------ low-level Interface functions -------------------------
  152. ------------------------------------------------------------------------------*/
  153. static void low_level_init(struct netif *netif)
  154. {
  155. }
  156. static err_t tapif_init(struct netif *netif)
  157. {
  158. return ERR_OK;
  159. }
  160. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  161. {
  162. struct pbuf *q;
  163. char buf[1514];
  164. char *bufptr;
  165. //struct tapif *tapif;
  166. int tot_len = 0;
  167. //tapif = (struct tapif *)netif->state;
  168. ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
  169. #if 0
  170. if(((double)rand()/(double)RAND_MAX) < 0.2) {
  171. printf("drop output\n");
  172. return ERR_OK;
  173. }
  174. #endif
  175. /* initiate transfer(); */
  176. bufptr = &buf[0];
  177. for(q = p; q != NULL; q = q->next) {
  178. /* Send the data from the pbuf to the interface, one pbuf at a
  179. time. The size of the data in each pbuf is kept in the ->len
  180. variable. */
  181. /* send data from(q->payload, q->len); */
  182. memcpy(bufptr, q->payload, q->len);
  183. bufptr += q->len;
  184. tot_len += q->len;
  185. }
  186. // signal that packet should be sent();
  187. // Split ethernet header and feed into handler
  188. struct eth_hdr *ethhdr;
  189. ethhdr = (struct eth_hdr *)p->payload;
  190. ZeroTier::MAC src_mac;
  191. ZeroTier::MAC dest_mac;
  192. src_mac.setTo(ethhdr->src.addr, 6);
  193. dest_mac.setTo(ethhdr->dest.addr, 6);
  194. tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,ZT_ETHERTYPE_IPV4,0,buf,p->tot_len);
  195. return ERR_OK;
  196. }
  197. } // namespace ZeroTier
  198. #endif // ZT_ENABLE_NETCON
  199. #endif