OneService.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdint.h>
  30. #include <string>
  31. #include <map>
  32. #include <vector>
  33. #include <algorithm>
  34. #include <list>
  35. #include <thread>
  36. #include <mutex>
  37. #include <condition_variable>
  38. #include "../include/ZeroTierOne.h"
  39. #include "../node/Constants.hpp"
  40. #include "../node/Mutex.hpp"
  41. #include "../node/Node.hpp"
  42. #include "../node/Utils.hpp"
  43. #include "../node/InetAddress.hpp"
  44. #include "../node/MAC.hpp"
  45. #include "../node/Identity.hpp"
  46. #include "../node/Salsa20.hpp"
  47. #include "../node/Poly1305.hpp"
  48. #include "../node/SHA512.hpp"
  49. #include "../osdep/Phy.hpp"
  50. #include "../osdep/Thread.hpp"
  51. #include "../osdep/OSUtils.hpp"
  52. #include "../osdep/Http.hpp"
  53. #include "../osdep/PortMapper.hpp"
  54. #include "../osdep/Binder.hpp"
  55. #include "../osdep/ManagedRoute.hpp"
  56. #include "../osdep/BlockingQueue.hpp"
  57. #include "OneService.hpp"
  58. #include "SoftwareUpdater.hpp"
  59. #ifdef __WINDOWS__
  60. #include <WinSock2.h>
  61. #include <Windows.h>
  62. #include <ShlObj.h>
  63. #include <netioapi.h>
  64. #include <iphlpapi.h>
  65. //#include <unistd.h>
  66. #define stat _stat
  67. #else
  68. #include <sys/types.h>
  69. #include <sys/socket.h>
  70. #include <sys/stat.h>
  71. #include <sys/wait.h>
  72. #include <unistd.h>
  73. #include <ifaddrs.h>
  74. #endif
  75. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  76. #include <http_parser.h>
  77. #else
  78. #include "../ext/http-parser/http_parser.h"
  79. #endif
  80. #include "../ext/json/json.hpp"
  81. using json = nlohmann::json;
  82. #include "../controller/EmbeddedNetworkController.hpp"
  83. #include "../controller/RabbitMQ.hpp"
  84. #include "../osdep/EthernetTap.hpp"
  85. #ifdef __WINDOWS__
  86. #include "../osdep/WindowsEthernetTap.hpp"
  87. #endif
  88. #ifndef ZT_SOFTWARE_UPDATE_DEFAULT
  89. #define ZT_SOFTWARE_UPDATE_DEFAULT "disable"
  90. #endif
  91. // Sanity limits for HTTP
  92. #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 64)
  93. #define ZT_MAX_HTTP_CONNECTIONS 65536
  94. // Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and also
  95. // bridged via ZeroTier to the same LAN traffic will (if the OS is sane) prefer WiFi.
  96. #define ZT_IF_METRIC 5000
  97. // How often to check for new multicast subscriptions on a tap device
  98. #define ZT_TAP_CHECK_MULTICAST_INTERVAL 5000
  99. // How often to check for local interface addresses
  100. #define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
  101. // How often local.conf is checked for changes
  102. #define ZT_LOCAL_CONF_FILE_CHECK_INTERVAL 10000
  103. namespace ZeroTier {
  104. namespace {
  105. static const InetAddress NULL_INET_ADDR;
  106. static std::string _trimString(const std::string &s)
  107. {
  108. unsigned long end = (unsigned long)s.length();
  109. while (end) {
  110. char c = s[end - 1];
  111. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  112. --end;
  113. else break;
  114. }
  115. unsigned long start = 0;
  116. while (start < end) {
  117. char c = s[start];
  118. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  119. ++start;
  120. else break;
  121. }
  122. return s.substr(start,end - start);
  123. }
  124. static void _networkToJson(nlohmann::json &nj,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName,const OneService::NetworkSettings &localSettings)
  125. {
  126. char tmp[256];
  127. const char *nstatus = "",*ntype = "";
  128. switch(nc->status) {
  129. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  130. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  131. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  132. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  133. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  134. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  135. }
  136. switch(nc->type) {
  137. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  138. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  139. }
  140. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",nc->nwid);
  141. nj["id"] = tmp;
  142. nj["nwid"] = tmp;
  143. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff));
  144. nj["mac"] = tmp;
  145. nj["name"] = nc->name;
  146. nj["status"] = nstatus;
  147. nj["type"] = ntype;
  148. nj["mtu"] = nc->mtu;
  149. nj["dhcp"] = (bool)(nc->dhcp != 0);
  150. nj["bridge"] = (bool)(nc->bridge != 0);
  151. nj["broadcastEnabled"] = (bool)(nc->broadcastEnabled != 0);
  152. nj["portError"] = nc->portError;
  153. nj["netconfRevision"] = nc->netconfRevision;
  154. nj["portDeviceName"] = portDeviceName;
  155. nj["allowManaged"] = localSettings.allowManaged;
  156. nj["allowGlobal"] = localSettings.allowGlobal;
  157. nj["allowDefault"] = localSettings.allowDefault;
  158. nlohmann::json aa = nlohmann::json::array();
  159. for(unsigned int i=0;i<nc->assignedAddressCount;++i) {
  160. aa.push_back(reinterpret_cast<const InetAddress *>(&(nc->assignedAddresses[i]))->toString(tmp));
  161. }
  162. nj["assignedAddresses"] = aa;
  163. nlohmann::json ra = nlohmann::json::array();
  164. for(unsigned int i=0;i<nc->routeCount;++i) {
  165. nlohmann::json rj;
  166. rj["target"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].target))->toString(tmp);
  167. if (nc->routes[i].via.ss_family == nc->routes[i].target.ss_family)
  168. rj["via"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].via))->toIpString(tmp);
  169. else rj["via"] = nlohmann::json();
  170. rj["flags"] = (int)nc->routes[i].flags;
  171. rj["metric"] = (int)nc->routes[i].metric;
  172. ra.push_back(rj);
  173. }
  174. nj["routes"] = ra;
  175. nlohmann::json mca = nlohmann::json::array();
  176. for(unsigned int i=0;i<nc->multicastSubscriptionCount;++i) {
  177. nlohmann::json m;
  178. m["mac"] = MAC(nc->multicastSubscriptions[i].mac).toString(tmp);
  179. m["adi"] = nc->multicastSubscriptions[i].adi;
  180. mca.push_back(m);
  181. }
  182. nj["multicastSubscriptions"] = mca;
  183. }
  184. static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer)
  185. {
  186. char tmp[256];
  187. const char *prole = "";
  188. switch(peer->role) {
  189. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  190. case ZT_PEER_ROLE_MOON: prole = "MOON"; break;
  191. case ZT_PEER_ROLE_PLANET: prole = "PLANET"; break;
  192. }
  193. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  194. pj["address"] = tmp;
  195. pj["versionMajor"] = peer->versionMajor;
  196. pj["versionMinor"] = peer->versionMinor;
  197. pj["versionRev"] = peer->versionRev;
  198. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev);
  199. pj["version"] = tmp;
  200. pj["latency"] = peer->latency;
  201. pj["role"] = prole;
  202. nlohmann::json pa = nlohmann::json::array();
  203. for(unsigned int i=0;i<peer->pathCount;++i) {
  204. int64_t lastSend = peer->paths[i].lastSend;
  205. int64_t lastReceive = peer->paths[i].lastReceive;
  206. nlohmann::json j;
  207. j["address"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  208. j["lastSend"] = (lastSend < 0) ? 0 : lastSend;
  209. j["lastReceive"] = (lastReceive < 0) ? 0 : lastReceive;
  210. j["trustedPathId"] = peer->paths[i].trustedPathId;
  211. j["active"] = (bool)(peer->paths[i].expired == 0);
  212. j["expired"] = (bool)(peer->paths[i].expired != 0);
  213. j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  214. pa.push_back(j);
  215. }
  216. pj["paths"] = pa;
  217. }
  218. static void _peerAggregateLinkToJson(nlohmann::json &pj,const ZT_Peer *peer)
  219. {
  220. char tmp[256];
  221. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  222. pj["aggregateLinkLatency"] = peer->latency;
  223. nlohmann::json pa = nlohmann::json::array();
  224. for(unsigned int i=0;i<peer->pathCount;++i) {
  225. int64_t lastSend = peer->paths[i].lastSend;
  226. int64_t lastReceive = peer->paths[i].lastReceive;
  227. nlohmann::json j;
  228. j["address"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  229. j["lastSend"] = (lastSend < 0) ? 0 : lastSend;
  230. j["lastReceive"] = (lastReceive < 0) ? 0 : lastReceive;
  231. //j["trustedPathId"] = peer->paths[i].trustedPathId;
  232. //j["active"] = (bool)(peer->paths[i].expired == 0);
  233. //j["expired"] = (bool)(peer->paths[i].expired != 0);
  234. //j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  235. j["latency"] = peer->paths[i].latency;
  236. j["pdv"] = peer->paths[i].packetDelayVariance;
  237. //j["throughputDisturbCoeff"] = peer->paths[i].throughputDisturbCoeff;
  238. //j["packetErrorRatio"] = peer->paths[i].packetErrorRatio;
  239. //j["packetLossRatio"] = peer->paths[i].packetLossRatio;
  240. j["stability"] = peer->paths[i].stability;
  241. j["throughput"] = peer->paths[i].throughput;
  242. //j["maxThroughput"] = peer->paths[i].maxThroughput;
  243. j["allocation"] = peer->paths[i].allocation;
  244. j["ifname"] = peer->paths[i].ifname;
  245. pa.push_back(j);
  246. }
  247. pj["paths"] = pa;
  248. }
  249. class OneServiceImpl;
  250. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf);
  251. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData);
  252. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len);
  253. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen);
  254. static int SnodeWirePacketSendFunction(ZT_Node *node,void *uptr,void *tptr,int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl);
  255. static void SnodeVirtualNetworkFrameFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  256. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr);
  257. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result);
  258. static void StapFrameHandler(void *uptr,void *tptr,uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  259. static int ShttpOnMessageBegin(http_parser *parser);
  260. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  261. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  262. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  263. #else
  264. static int ShttpOnStatus(http_parser *parser);
  265. #endif
  266. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  267. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  268. static int ShttpOnHeadersComplete(http_parser *parser);
  269. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  270. static int ShttpOnMessageComplete(http_parser *parser);
  271. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 1)
  272. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  273. ShttpOnMessageBegin,
  274. ShttpOnUrl,
  275. ShttpOnStatus,
  276. ShttpOnHeaderField,
  277. ShttpOnValue,
  278. ShttpOnHeadersComplete,
  279. ShttpOnBody,
  280. ShttpOnMessageComplete
  281. };
  282. #else
  283. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  284. ShttpOnMessageBegin,
  285. ShttpOnUrl,
  286. ShttpOnHeaderField,
  287. ShttpOnValue,
  288. ShttpOnHeadersComplete,
  289. ShttpOnBody,
  290. ShttpOnMessageComplete
  291. };
  292. #endif
  293. /**
  294. * A TCP connection and related state and buffers
  295. */
  296. struct TcpConnection
  297. {
  298. enum {
  299. TCP_UNCATEGORIZED_INCOMING, // uncategorized incoming connection
  300. TCP_HTTP_INCOMING,
  301. } type;
  302. OneServiceImpl *parent;
  303. PhySocket *sock;
  304. InetAddress remoteAddr;
  305. uint64_t lastReceive;
  306. // Used for inbound HTTP connections
  307. http_parser parser;
  308. unsigned long messageSize;
  309. std::string currentHeaderField;
  310. std::string currentHeaderValue;
  311. std::string url;
  312. std::string status;
  313. std::map< std::string,std::string > headers;
  314. std::string readq;
  315. std::string writeq;
  316. Mutex writeq_m;
  317. };
  318. struct OneServiceIncomingPacket
  319. {
  320. uint64_t now;
  321. int64_t sock;
  322. struct sockaddr_storage from;
  323. unsigned int size;
  324. uint8_t data[ZT_MAX_MTU];
  325. };
  326. class OneServiceImpl : public OneService
  327. {
  328. public:
  329. // begin member variables --------------------------------------------------
  330. const std::string _homePath;
  331. std::string _authToken;
  332. std::string _controllerDbPath;
  333. const std::string _networksPath;
  334. const std::string _moonsPath;
  335. EmbeddedNetworkController *_controller;
  336. Phy<OneServiceImpl *> _phy;
  337. Node *_node;
  338. SoftwareUpdater *_updater;
  339. PhySocket *_localControlSocket4;
  340. PhySocket *_localControlSocket6;
  341. bool _updateAutoApply;
  342. bool _allowSecondaryPort;
  343. unsigned int _multipathMode;
  344. unsigned int _primaryPort;
  345. unsigned int _secondaryPort;
  346. unsigned int _tertiaryPort;
  347. volatile unsigned int _udpPortPickerCounter;
  348. // Local configuration and memo-ized information from it
  349. json _localConfig;
  350. Hashtable< uint64_t,std::vector<InetAddress> > _v4Hints;
  351. Hashtable< uint64_t,std::vector<InetAddress> > _v6Hints;
  352. Hashtable< uint64_t,std::vector<InetAddress> > _v4Blacklists;
  353. Hashtable< uint64_t,std::vector<InetAddress> > _v6Blacklists;
  354. std::vector< InetAddress > _globalV4Blacklist;
  355. std::vector< InetAddress > _globalV6Blacklist;
  356. std::vector< InetAddress > _allowManagementFrom;
  357. std::vector< std::string > _interfacePrefixBlacklist;
  358. Mutex _localConfig_m;
  359. std::vector<InetAddress> explicitBind;
  360. /*
  361. * To attempt to handle NAT/gateway craziness we use three local UDP ports:
  362. *
  363. * [0] is the normal/default port, usually 9993
  364. * [1] is a port derived from our ZeroTier address
  365. * [2] is a port computed from the normal/default for use with uPnP/NAT-PMP mappings
  366. *
  367. * [2] exists because on some gateways trying to do regular NAT-t interferes
  368. * destructively with uPnP port mapping behavior in very weird buggy ways.
  369. * It's only used if uPnP/NAT-PMP is enabled in this build.
  370. */
  371. unsigned int _ports[3];
  372. Binder _binder;
  373. // Time we last received a packet from a global address
  374. uint64_t _lastDirectReceiveFromGlobal;
  375. // Last potential sleep/wake event
  376. uint64_t _lastRestart;
  377. // Deadline for the next background task service function
  378. volatile int64_t _nextBackgroundTaskDeadline;
  379. // Configured networks
  380. struct NetworkState
  381. {
  382. NetworkState() :
  383. tap((EthernetTap *)0)
  384. {
  385. // Real defaults are in network 'up' code in network event handler
  386. settings.allowManaged = true;
  387. settings.allowGlobal = false;
  388. settings.allowDefault = false;
  389. }
  390. std::shared_ptr<EthernetTap> tap;
  391. ZT_VirtualNetworkConfig config; // memcpy() of raw config from core
  392. std::vector<InetAddress> managedIps;
  393. std::list< SharedPtr<ManagedRoute> > managedRoutes;
  394. NetworkSettings settings;
  395. };
  396. std::map<uint64_t,NetworkState> _nets;
  397. Mutex _nets_m;
  398. // Active TCP/IP connections
  399. std::vector< TcpConnection * > _tcpConnections;
  400. Mutex _tcpConnections_m;
  401. // Termination status information
  402. ReasonForTermination _termReason;
  403. std::string _fatalErrorMessage;
  404. Mutex _termReason_m;
  405. // uPnP/NAT-PMP port mapper if enabled
  406. bool _portMappingEnabled; // local.conf settings
  407. #ifdef ZT_USE_MINIUPNPC
  408. PortMapper *_portMapper;
  409. #endif
  410. // Set to false to force service to stop
  411. volatile bool _run;
  412. Mutex _run_m;
  413. MQConfig *_mqc;
  414. // end member variables ----------------------------------------------------
  415. OneServiceImpl(const char *hp,unsigned int port) :
  416. _homePath((hp) ? hp : ".")
  417. ,_controllerDbPath(_homePath + ZT_PATH_SEPARATOR_S "controller.d")
  418. ,_networksPath(_homePath + ZT_PATH_SEPARATOR_S "networks.d")
  419. ,_moonsPath(_homePath + ZT_PATH_SEPARATOR_S "moons.d")
  420. ,_controller((EmbeddedNetworkController *)0)
  421. ,_phy(this,false,true)
  422. ,_node((Node *)0)
  423. ,_updater((SoftwareUpdater *)0)
  424. ,_localControlSocket4((PhySocket *)0)
  425. ,_localControlSocket6((PhySocket *)0)
  426. ,_updateAutoApply(false)
  427. ,_primaryPort(port)
  428. ,_udpPortPickerCounter(0)
  429. ,_lastDirectReceiveFromGlobal(0)
  430. ,_lastRestart(0)
  431. ,_nextBackgroundTaskDeadline(0)
  432. ,_termReason(ONE_STILL_RUNNING)
  433. ,_portMappingEnabled(true)
  434. #ifdef ZT_USE_MINIUPNPC
  435. ,_portMapper((PortMapper *)0)
  436. #endif
  437. ,_run(true)
  438. ,_mqc(NULL)
  439. {
  440. _ports[0] = 0;
  441. _ports[1] = 0;
  442. _ports[2] = 0;
  443. }
  444. virtual ~OneServiceImpl()
  445. {
  446. _binder.closeAll(_phy);
  447. _phy.close(_localControlSocket4);
  448. _phy.close(_localControlSocket6);
  449. #ifdef ZT_USE_MINIUPNPC
  450. delete _portMapper;
  451. #endif
  452. delete _controller;
  453. delete _mqc;
  454. }
  455. virtual ReasonForTermination run()
  456. {
  457. try {
  458. {
  459. const std::string authTokenPath(_homePath + ZT_PATH_SEPARATOR_S "authtoken.secret");
  460. if (!OSUtils::readFile(authTokenPath.c_str(),_authToken)) {
  461. unsigned char foo[24];
  462. Utils::getSecureRandom(foo,sizeof(foo));
  463. _authToken = "";
  464. for(unsigned int i=0;i<sizeof(foo);++i)
  465. _authToken.push_back("abcdefghijklmnopqrstuvwxyz0123456789"[(unsigned long)foo[i] % 36]);
  466. if (!OSUtils::writeFile(authTokenPath.c_str(),_authToken)) {
  467. Mutex::Lock _l(_termReason_m);
  468. _termReason = ONE_UNRECOVERABLE_ERROR;
  469. _fatalErrorMessage = "authtoken.secret could not be written";
  470. return _termReason;
  471. } else {
  472. OSUtils::lockDownFile(authTokenPath.c_str(),false);
  473. }
  474. }
  475. _authToken = _trimString(_authToken);
  476. }
  477. {
  478. struct ZT_Node_Callbacks cb;
  479. cb.version = 0;
  480. cb.stateGetFunction = SnodeStateGetFunction;
  481. cb.statePutFunction = SnodeStatePutFunction;
  482. cb.wirePacketSendFunction = SnodeWirePacketSendFunction;
  483. cb.virtualNetworkFrameFunction = SnodeVirtualNetworkFrameFunction;
  484. cb.virtualNetworkConfigFunction = SnodeVirtualNetworkConfigFunction;
  485. cb.eventCallback = SnodeEventCallback;
  486. cb.pathCheckFunction = SnodePathCheckFunction;
  487. cb.pathLookupFunction = SnodePathLookupFunction;
  488. _node = new Node(this,(void *)0,&cb,OSUtils::now());
  489. }
  490. // local.conf
  491. readLocalSettings();
  492. applyLocalConfig();
  493. // Make sure we can use the primary port, and hunt for one if configured to do so
  494. const int portTrials = (_primaryPort == 0) ? 256 : 1; // if port is 0, pick random
  495. for(int k=0;k<portTrials;++k) {
  496. if (_primaryPort == 0) {
  497. unsigned int randp = 0;
  498. Utils::getSecureRandom(&randp,sizeof(randp));
  499. _primaryPort = 20000 + (randp % 45500);
  500. }
  501. if (_trialBind(_primaryPort)) {
  502. _ports[0] = _primaryPort;
  503. } else {
  504. _primaryPort = 0;
  505. }
  506. }
  507. if (_ports[0] == 0) {
  508. Mutex::Lock _l(_termReason_m);
  509. _termReason = ONE_UNRECOVERABLE_ERROR;
  510. _fatalErrorMessage = "cannot bind to local control interface port";
  511. return _termReason;
  512. }
  513. // Bind TCP control socket to 127.0.0.1 and ::1 as well for loopback TCP control socket queries
  514. {
  515. struct sockaddr_in lo4;
  516. memset(&lo4,0,sizeof(lo4));
  517. lo4.sin_family = AF_INET;
  518. lo4.sin_addr.s_addr = Utils::hton((uint32_t)0x7f000001);
  519. lo4.sin_port = Utils::hton((uint16_t)_ports[0]);
  520. _localControlSocket4 = _phy.tcpListen((const struct sockaddr *)&lo4);
  521. struct sockaddr_in6 lo6;
  522. memset(&lo6,0,sizeof(lo6));
  523. lo6.sin6_family = AF_INET6;
  524. lo6.sin6_addr.s6_addr[15] = 1;
  525. lo6.sin6_port = lo4.sin_port;
  526. _localControlSocket6 = _phy.tcpListen((const struct sockaddr *)&lo6);
  527. }
  528. // Save primary port to a file so CLIs and GUIs can learn it easily
  529. char portstr[64];
  530. OSUtils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]);
  531. OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S "zerotier-one.port").c_str(),std::string(portstr));
  532. // Attempt to bind to a secondary port chosen from our ZeroTier address.
  533. // This exists because there are buggy NATs out there that fail if more
  534. // than one device behind the same NAT tries to use the same internal
  535. // private address port number. Buggy NATs are a running theme.
  536. if (_allowSecondaryPort) {
  537. _ports[1] = (_secondaryPort == 0) ? 20000 + ((unsigned int)_node->address() % 45500) : _secondaryPort;
  538. for(int i=0;;++i) {
  539. if (i > 1000) {
  540. _ports[1] = 0;
  541. break;
  542. } else if (++_ports[1] >= 65536) {
  543. _ports[1] = 20000;
  544. }
  545. if (_trialBind(_ports[1]))
  546. break;
  547. }
  548. }
  549. #ifdef ZT_USE_MINIUPNPC
  550. if (_portMappingEnabled) {
  551. // If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't
  552. // use the other two ports for that because some NATs do really funky
  553. // stuff with ports that are explicitly mapped that breaks things.
  554. if (_ports[1]) {
  555. _ports[2] = (_tertiaryPort == 0) ? _ports[1] : _tertiaryPort;
  556. for(int i=0;;++i) {
  557. if (i > 1000) {
  558. _ports[2] = 0;
  559. break;
  560. } else if (++_ports[2] >= 65536) {
  561. _ports[2] = 20000;
  562. }
  563. if (_trialBind(_ports[2]))
  564. break;
  565. }
  566. if (_ports[2]) {
  567. char uniqueName[64];
  568. OSUtils::ztsnprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]);
  569. _portMapper = new PortMapper(_ports[2],uniqueName);
  570. }
  571. }
  572. }
  573. #endif
  574. // Delete legacy iddb.d if present (cleanup)
  575. OSUtils::rmDashRf((_homePath + ZT_PATH_SEPARATOR_S "iddb.d").c_str());
  576. // Network controller is now enabled by default for desktop and server
  577. _controller = new EmbeddedNetworkController(_node,_homePath.c_str(),_controllerDbPath.c_str(),_ports[0], _mqc);
  578. _node->setNetconfMaster((void *)_controller);
  579. // Join existing networks in networks.d
  580. {
  581. std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str()));
  582. for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
  583. std::size_t dot = f->find_last_of('.');
  584. if ((dot == 16)&&(f->substr(16) == ".conf"))
  585. _node->join(Utils::hexStrToU64(f->substr(0,dot).c_str()),(void *)0,(void *)0);
  586. }
  587. }
  588. // Main I/O loop
  589. _nextBackgroundTaskDeadline = 0;
  590. int64_t clockShouldBe = OSUtils::now();
  591. _lastRestart = clockShouldBe;
  592. int64_t lastTapMulticastGroupCheck = 0;
  593. int64_t lastBindRefresh = 0;
  594. int64_t lastUpdateCheck = clockShouldBe;
  595. int64_t lastMultipathModeUpdate = 0;
  596. int64_t lastCleanedPeersDb = 0;
  597. int64_t lastLocalInterfaceAddressCheck = (clockShouldBe - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to configure and other things time to settle
  598. int64_t lastLocalConfFileCheck = OSUtils::now();
  599. for(;;) {
  600. _run_m.lock();
  601. if (!_run) {
  602. _run_m.unlock();
  603. _termReason_m.lock();
  604. _termReason = ONE_NORMAL_TERMINATION;
  605. _termReason_m.unlock();
  606. break;
  607. } else {
  608. _run_m.unlock();
  609. }
  610. const int64_t now = OSUtils::now();
  611. // Attempt to detect sleep/wake events by detecting delay overruns
  612. bool restarted = false;
  613. if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
  614. _lastRestart = now;
  615. restarted = true;
  616. }
  617. // Check for updates (if enabled)
  618. if ((_updater)&&((now - lastUpdateCheck) > 10000)) {
  619. lastUpdateCheck = now;
  620. if (_updater->check(now) && _updateAutoApply)
  621. _updater->apply();
  622. }
  623. // Reload local.conf if anything changed recently
  624. if ((now - lastLocalConfFileCheck) >= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  625. lastLocalConfFileCheck = now;
  626. struct stat result;
  627. if(stat((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(), &result)==0) {
  628. int64_t mod_time = result.st_mtime * 1000;
  629. if ((now - mod_time) <= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  630. readLocalSettings();
  631. applyLocalConfig();
  632. }
  633. }
  634. }
  635. // Refresh bindings in case device's interfaces have changed, and also sync routes to update any shadow routes (e.g. shadow default)
  636. if (((now - lastBindRefresh) >= (_multipathMode ? ZT_BINDER_REFRESH_PERIOD / 8 : ZT_BINDER_REFRESH_PERIOD))||(restarted)) {
  637. lastBindRefresh = now;
  638. unsigned int p[3];
  639. unsigned int pc = 0;
  640. for(int i=0;i<3;++i) {
  641. if (_ports[i])
  642. p[pc++] = _ports[i];
  643. }
  644. _binder.refresh(_phy,p,pc,explicitBind,*this);
  645. {
  646. Mutex::Lock _l(_nets_m);
  647. for(std::map<uint64_t,NetworkState>::iterator n(_nets.begin());n!=_nets.end();++n) {
  648. if (n->second.tap)
  649. syncManagedStuff(n->second,false,true);
  650. }
  651. }
  652. }
  653. // Update multipath mode (if needed)
  654. if (((now - lastMultipathModeUpdate) >= ZT_BINDER_REFRESH_PERIOD / 8)||(restarted)) {
  655. lastMultipathModeUpdate = now;
  656. _node->setMultipathMode(_multipathMode);
  657. }
  658. // Run background task processor in core if it's time to do so
  659. int64_t dl = _nextBackgroundTaskDeadline;
  660. if (dl <= now) {
  661. _node->processBackgroundTasks((void *)0,now,&_nextBackgroundTaskDeadline);
  662. dl = _nextBackgroundTaskDeadline;
  663. }
  664. // Sync multicast group memberships
  665. if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) {
  666. lastTapMulticastGroupCheck = now;
  667. std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > > mgChanges;
  668. {
  669. Mutex::Lock _l(_nets_m);
  670. mgChanges.reserve(_nets.size() + 1);
  671. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  672. if (n->second.tap) {
  673. mgChanges.push_back(std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > >(n->first,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> >()));
  674. n->second.tap->scanMulticastGroups(mgChanges.back().second.first,mgChanges.back().second.second);
  675. }
  676. }
  677. }
  678. for(std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > >::iterator c(mgChanges.begin());c!=mgChanges.end();++c) {
  679. for(std::vector<MulticastGroup>::iterator m(c->second.first.begin());m!=c->second.first.end();++m)
  680. _node->multicastSubscribe((void *)0,c->first,m->mac().toInt(),m->adi());
  681. for(std::vector<MulticastGroup>::iterator m(c->second.second.begin());m!=c->second.second.end();++m)
  682. _node->multicastUnsubscribe(c->first,m->mac().toInt(),m->adi());
  683. }
  684. }
  685. // Sync information about physical network interfaces
  686. if ((now - lastLocalInterfaceAddressCheck) >= (_multipathMode ? ZT_LOCAL_INTERFACE_CHECK_INTERVAL / 8 : ZT_LOCAL_INTERFACE_CHECK_INTERVAL)) {
  687. lastLocalInterfaceAddressCheck = now;
  688. _node->clearLocalInterfaceAddresses();
  689. #ifdef ZT_USE_MINIUPNPC
  690. if (_portMapper) {
  691. std::vector<InetAddress> mappedAddresses(_portMapper->get());
  692. for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
  693. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
  694. }
  695. #endif
  696. std::vector<InetAddress> boundAddrs(_binder.allBoundLocalInterfaceAddresses());
  697. for(std::vector<InetAddress>::const_iterator i(boundAddrs.begin());i!=boundAddrs.end();++i)
  698. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*i)));
  699. }
  700. // Clean peers.d periodically
  701. if ((now - lastCleanedPeersDb) >= 3600000) {
  702. lastCleanedPeersDb = now;
  703. OSUtils::cleanDirectory((_homePath + ZT_PATH_SEPARATOR_S "peers.d").c_str(),now - 2592000000LL); // delete older than 30 days
  704. }
  705. const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100;
  706. clockShouldBe = now + (uint64_t)delay;
  707. _phy.poll(delay);
  708. }
  709. } catch (std::exception &e) {
  710. Mutex::Lock _l(_termReason_m);
  711. _termReason = ONE_UNRECOVERABLE_ERROR;
  712. _fatalErrorMessage = std::string("unexpected exception in main thread: ")+e.what();
  713. } catch ( ... ) {
  714. Mutex::Lock _l(_termReason_m);
  715. _termReason = ONE_UNRECOVERABLE_ERROR;
  716. _fatalErrorMessage = "unexpected exception in main thread: unknown exception";
  717. }
  718. try {
  719. Mutex::Lock _l(_tcpConnections_m);
  720. while (!_tcpConnections.empty())
  721. _phy.close((*_tcpConnections.begin())->sock);
  722. } catch ( ... ) {}
  723. {
  724. Mutex::Lock _l(_nets_m);
  725. _nets.clear();
  726. }
  727. delete _updater;
  728. _updater = (SoftwareUpdater *)0;
  729. delete _node;
  730. _node = (Node *)0;
  731. return _termReason;
  732. }
  733. void readLocalSettings()
  734. {
  735. // Read local configuration
  736. std::map<InetAddress,ZT_PhysicalPathConfiguration> ppc;
  737. // LEGACY: support old "trustedpaths" flat file
  738. FILE *trustpaths = fopen((_homePath + ZT_PATH_SEPARATOR_S "trustedpaths").c_str(),"r");
  739. if (trustpaths) {
  740. fprintf(stderr,"WARNING: 'trustedpaths' flat file format is deprecated in favor of path definitions in local.conf" ZT_EOL_S);
  741. char buf[1024];
  742. while (fgets(buf,sizeof(buf),trustpaths)) {
  743. int fno = 0;
  744. char *saveptr = (char *)0;
  745. uint64_t trustedPathId = 0;
  746. InetAddress trustedPathNetwork;
  747. for(char *f=Utils::stok(buf,"=\r\n \t",&saveptr);(f);f=Utils::stok((char *)0,"=\r\n \t",&saveptr)) {
  748. if (fno == 0) {
  749. trustedPathId = Utils::hexStrToU64(f);
  750. } else if (fno == 1) {
  751. trustedPathNetwork = InetAddress(f);
  752. } else break;
  753. ++fno;
  754. }
  755. if ( (trustedPathId != 0) && ((trustedPathNetwork.ss_family == AF_INET)||(trustedPathNetwork.ss_family == AF_INET6)) && (trustedPathNetwork.netmaskBits() > 0) ) {
  756. ppc[trustedPathNetwork].trustedPathId = trustedPathId;
  757. ppc[trustedPathNetwork].mtu = 0; // use default
  758. }
  759. }
  760. fclose(trustpaths);
  761. }
  762. // Read local config file
  763. Mutex::Lock _l2(_localConfig_m);
  764. std::string lcbuf;
  765. if (OSUtils::readFile((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(),lcbuf)) {
  766. if (lcbuf.length() > 0) {
  767. try {
  768. _localConfig = OSUtils::jsonParse(lcbuf);
  769. if (!_localConfig.is_object()) {
  770. fprintf(stderr,"ERROR: unable to parse local.conf (root element is not a JSON object)" ZT_EOL_S);
  771. exit(1);
  772. }
  773. } catch ( ... ) {
  774. fprintf(stderr,"ERROR: unable to parse local.conf (invalid JSON)" ZT_EOL_S);
  775. exit(1);
  776. }
  777. }
  778. }
  779. // Get any trusted paths in local.conf (we'll parse the rest of physical[] elsewhere)
  780. json &physical = _localConfig["physical"];
  781. if (physical.is_object()) {
  782. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  783. InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  784. if (net) {
  785. if (phy.value().is_object()) {
  786. uint64_t tpid;
  787. if ((tpid = OSUtils::jsonInt(phy.value()["trustedPathId"],0ULL)) != 0ULL) {
  788. if ((net.ss_family == AF_INET)||(net.ss_family == AF_INET6))
  789. ppc[net].trustedPathId = tpid;
  790. }
  791. ppc[net].mtu = (int)OSUtils::jsonInt(phy.value()["mtu"],0ULL); // 0 means use default
  792. }
  793. }
  794. }
  795. }
  796. json &settings = _localConfig["settings"];
  797. if (settings.is_object()) {
  798. // Allow controller DB path to be put somewhere else
  799. const std::string cdbp(OSUtils::jsonString(settings["controllerDbPath"],""));
  800. if (cdbp.length() > 0)
  801. _controllerDbPath = cdbp;
  802. json &rmq = settings["rabbitmq"];
  803. if (rmq.is_object() && _mqc == NULL) {
  804. fprintf(stderr, "Reading RabbitMQ Config\n");
  805. _mqc = new MQConfig;
  806. _mqc->port = rmq["port"];
  807. _mqc->host = OSUtils::jsonString(rmq["host"], "").c_str();
  808. _mqc->username = OSUtils::jsonString(rmq["username"], "").c_str();
  809. _mqc->password = OSUtils::jsonString(rmq["password"], "").c_str();
  810. }
  811. // Bind to wildcard instead of to specific interfaces (disables full tunnel capability)
  812. json &bind = settings["bind"];
  813. if (bind.is_array()) {
  814. for(unsigned long i=0;i<bind.size();++i) {
  815. const std::string ips(OSUtils::jsonString(bind[i],""));
  816. if (ips.length() > 0) {
  817. InetAddress ip(ips.c_str());
  818. if ((ip.ss_family == AF_INET)||(ip.ss_family == AF_INET6))
  819. explicitBind.push_back(ip);
  820. }
  821. }
  822. }
  823. }
  824. // Set trusted paths if there are any
  825. if (ppc.size() > 0) {
  826. for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::iterator i(ppc.begin());i!=ppc.end();++i)
  827. _node->setPhysicalPathConfiguration(reinterpret_cast<const struct sockaddr_storage *>(&(i->first)),&(i->second));
  828. }
  829. }
  830. virtual ReasonForTermination reasonForTermination() const
  831. {
  832. Mutex::Lock _l(_termReason_m);
  833. return _termReason;
  834. }
  835. virtual std::string fatalErrorMessage() const
  836. {
  837. Mutex::Lock _l(_termReason_m);
  838. return _fatalErrorMessage;
  839. }
  840. virtual std::string portDeviceName(uint64_t nwid) const
  841. {
  842. Mutex::Lock _l(_nets_m);
  843. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  844. if ((n != _nets.end())&&(n->second.tap))
  845. return n->second.tap->deviceName();
  846. else return std::string();
  847. }
  848. #ifdef ZT_SDK
  849. virtual std::string givenHomePath()
  850. {
  851. return _homePath;
  852. }
  853. void getRoutes(uint64_t nwid, void *routeArray, unsigned int *numRoutes)
  854. {
  855. Mutex::Lock _l(_nets_m);
  856. NetworkState &n = _nets[nwid];
  857. *numRoutes = *numRoutes < n.config.routeCount ? *numRoutes : n.config.routeCount;
  858. for(unsigned int i=0; i<*numRoutes; i++) {
  859. ZT_VirtualNetworkRoute *vnr = (ZT_VirtualNetworkRoute*)routeArray;
  860. memcpy(&vnr[i], &(n.config.routes[i]), sizeof(ZT_VirtualNetworkRoute));
  861. }
  862. }
  863. virtual Node *getNode()
  864. {
  865. return _node;
  866. }
  867. #endif // ZT_SDK
  868. virtual void terminate()
  869. {
  870. _run_m.lock();
  871. _run = false;
  872. _run_m.unlock();
  873. _phy.whack();
  874. }
  875. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const
  876. {
  877. Mutex::Lock _l(_nets_m);
  878. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  879. if (n == _nets.end())
  880. return false;
  881. settings = n->second.settings;
  882. return true;
  883. }
  884. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings)
  885. {
  886. Mutex::Lock _l(_nets_m);
  887. std::map<uint64_t,NetworkState>::iterator n(_nets.find(nwid));
  888. if (n == _nets.end())
  889. return false;
  890. n->second.settings = settings;
  891. char nlcpath[4096];
  892. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_networksPath.c_str(),nwid);
  893. FILE *out = fopen(nlcpath,"w");
  894. if (out) {
  895. fprintf(out,"allowManaged=%d\n",(int)n->second.settings.allowManaged);
  896. fprintf(out,"allowGlobal=%d\n",(int)n->second.settings.allowGlobal);
  897. fprintf(out,"allowDefault=%d\n",(int)n->second.settings.allowDefault);
  898. fclose(out);
  899. }
  900. if (n->second.tap)
  901. syncManagedStuff(n->second,true,true);
  902. return true;
  903. }
  904. // =========================================================================
  905. // Internal implementation methods for control plane, route setup, etc.
  906. // =========================================================================
  907. inline unsigned int handleControlPlaneHttpRequest(
  908. const InetAddress &fromAddress,
  909. unsigned int httpMethod,
  910. const std::string &path,
  911. const std::map<std::string,std::string> &headers,
  912. const std::string &body,
  913. std::string &responseBody,
  914. std::string &responseContentType)
  915. {
  916. char tmp[256];
  917. unsigned int scode = 404;
  918. json res;
  919. std::vector<std::string> ps(OSUtils::split(path.c_str(),"/","",""));
  920. std::map<std::string,std::string> urlArgs;
  921. /* Note: this is kind of restricted in what it'll take. It does not support
  922. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  923. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  924. * paths to simply-named resources. */
  925. if (ps.size() > 0) {
  926. std::size_t qpos = ps[ps.size() - 1].find('?');
  927. if (qpos != std::string::npos) {
  928. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  929. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  930. std::vector<std::string> asplit(OSUtils::split(args.c_str(),"&","",""));
  931. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  932. std::size_t eqpos = a->find('=');
  933. if (eqpos == std::string::npos)
  934. urlArgs[*a] = "";
  935. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  936. }
  937. }
  938. } else {
  939. return 404;
  940. }
  941. bool isAuth = false;
  942. {
  943. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  944. if ((ah != headers.end())&&(_authToken == ah->second)) {
  945. isAuth = true;
  946. } else {
  947. ah = urlArgs.find("auth");
  948. if ((ah != urlArgs.end())&&(_authToken == ah->second))
  949. isAuth = true;
  950. }
  951. }
  952. #ifdef __SYNOLOGY__
  953. // Authenticate via Synology's built-in cgi script
  954. if (!isAuth) {
  955. int synotoken_pos = path.find("SynoToken");
  956. int argpos = path.find("?");
  957. if(synotoken_pos != std::string::npos && argpos != std::string::npos) {
  958. std::string cookie = path.substr(argpos+1, synotoken_pos-(argpos+1));
  959. std::string synotoken = path.substr(synotoken_pos);
  960. std::string cookie_val = cookie.substr(cookie.find("=")+1);
  961. std::string synotoken_val = synotoken.substr(synotoken.find("=")+1);
  962. // Set necessary env for auth script
  963. std::map<std::string,std::string>::const_iterator ah2(headers.find("x-forwarded-for"));
  964. setenv("HTTP_COOKIE", cookie_val.c_str(), true);
  965. setenv("HTTP_X_SYNO_TOKEN", synotoken_val.c_str(), true);
  966. setenv("REMOTE_ADDR", ah2->second.c_str(),true);
  967. char user[256], buf[1024];
  968. FILE *fp = NULL;
  969. bzero(user, 256);
  970. fp = popen("/usr/syno/synoman/webman/modules/authenticate.cgi", "r");
  971. if(!fp)
  972. isAuth = false;
  973. else {
  974. bzero(buf, sizeof(buf));
  975. fread(buf, 1024, 1, fp);
  976. if(strlen(buf) > 0) {
  977. snprintf(user, 256, "%s", buf);
  978. isAuth = true;
  979. }
  980. }
  981. pclose(fp);
  982. }
  983. }
  984. #endif
  985. if (httpMethod == HTTP_GET) {
  986. if (isAuth) {
  987. if (ps[0] == "status") {
  988. ZT_NodeStatus status;
  989. _node->status(&status);
  990. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",status.address);
  991. res["address"] = tmp;
  992. res["publicIdentity"] = status.publicIdentity;
  993. res["online"] = (bool)(status.online != 0);
  994. res["versionMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  995. res["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  996. res["versionRev"] = ZEROTIER_ONE_VERSION_REVISION;
  997. res["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  998. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  999. res["version"] = tmp;
  1000. res["clock"] = OSUtils::now();
  1001. {
  1002. Mutex::Lock _l(_localConfig_m);
  1003. res["config"] = _localConfig;
  1004. }
  1005. json &settings = res["config"]["settings"];
  1006. settings["primaryPort"] = OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1007. if (_multipathMode) {
  1008. json &multipathConfig = res["multipath"];
  1009. ZT_PeerList *pl = _node->peers();
  1010. char peerAddrStr[256];
  1011. if (pl) {
  1012. for(unsigned long i=0;i<pl->peerCount;++i) {
  1013. if (pl->peers[i].hadAggregateLink) {
  1014. nlohmann::json pj;
  1015. _peerAggregateLinkToJson(pj,&(pl->peers[i]));
  1016. OSUtils::ztsnprintf(peerAddrStr,sizeof(peerAddrStr),"%.10llx",pl->peers[i].address);
  1017. multipathConfig[peerAddrStr] = (pj);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. #ifdef ZT_USE_MINIUPNPC
  1023. settings["portMappingEnabled"] = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1024. #else
  1025. settings["portMappingEnabled"] = false; // not supported in build
  1026. #endif
  1027. #ifndef ZT_SDK
  1028. settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT);
  1029. settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL);
  1030. #endif
  1031. scode = 200;
  1032. } else if (ps[0] == "network") {
  1033. ZT_VirtualNetworkList *nws = _node->networks();
  1034. if (nws) {
  1035. if (ps.size() == 1) {
  1036. // Return [array] of all networks
  1037. res = nlohmann::json::array();
  1038. for(unsigned long i=0;i<nws->networkCount;++i) {
  1039. OneService::NetworkSettings localSettings;
  1040. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1041. nlohmann::json nj;
  1042. _networkToJson(nj,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1043. res.push_back(nj);
  1044. }
  1045. scode = 200;
  1046. } else if (ps.size() == 2) {
  1047. // Return a single network by ID or 404 if not found
  1048. const uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1049. for(unsigned long i=0;i<nws->networkCount;++i) {
  1050. if (nws->networks[i].nwid == wantnw) {
  1051. OneService::NetworkSettings localSettings;
  1052. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1053. _networkToJson(res,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1054. scode = 200;
  1055. break;
  1056. }
  1057. }
  1058. } else scode = 404;
  1059. _node->freeQueryResult((void *)nws);
  1060. } else scode = 500;
  1061. } else if (ps[0] == "peer") {
  1062. ZT_PeerList *pl = _node->peers();
  1063. if (pl) {
  1064. if (ps.size() == 1) {
  1065. // Return [array] of all peers
  1066. res = nlohmann::json::array();
  1067. for(unsigned long i=0;i<pl->peerCount;++i) {
  1068. nlohmann::json pj;
  1069. _peerToJson(pj,&(pl->peers[i]));
  1070. res.push_back(pj);
  1071. }
  1072. scode = 200;
  1073. } else if (ps.size() == 2) {
  1074. // Return a single peer by ID or 404 if not found
  1075. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  1076. for(unsigned long i=0;i<pl->peerCount;++i) {
  1077. if (pl->peers[i].address == wantp) {
  1078. _peerToJson(res,&(pl->peers[i]));
  1079. scode = 200;
  1080. break;
  1081. }
  1082. }
  1083. } else scode = 404;
  1084. _node->freeQueryResult((void *)pl);
  1085. } else scode = 500;
  1086. } else {
  1087. if (_controller) {
  1088. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1089. } else scode = 404;
  1090. }
  1091. } else scode = 401; // isAuth == false
  1092. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  1093. if (isAuth) {
  1094. if (ps[0] == "network") {
  1095. if (ps.size() == 2) {
  1096. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1097. _node->join(wantnw,(void *)0,(void *)0); // does nothing if we are a member
  1098. ZT_VirtualNetworkList *nws = _node->networks();
  1099. if (nws) {
  1100. for(unsigned long i=0;i<nws->networkCount;++i) {
  1101. if (nws->networks[i].nwid == wantnw) {
  1102. OneService::NetworkSettings localSettings;
  1103. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1104. try {
  1105. json j(OSUtils::jsonParse(body));
  1106. if (j.is_object()) {
  1107. json &allowManaged = j["allowManaged"];
  1108. if (allowManaged.is_boolean()) localSettings.allowManaged = (bool)allowManaged;
  1109. json &allowGlobal = j["allowGlobal"];
  1110. if (allowGlobal.is_boolean()) localSettings.allowGlobal = (bool)allowGlobal;
  1111. json &allowDefault = j["allowDefault"];
  1112. if (allowDefault.is_boolean()) localSettings.allowDefault = (bool)allowDefault;
  1113. }
  1114. } catch ( ... ) {
  1115. // discard invalid JSON
  1116. }
  1117. setNetworkSettings(nws->networks[i].nwid,localSettings);
  1118. _networkToJson(res,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1119. scode = 200;
  1120. break;
  1121. }
  1122. }
  1123. _node->freeQueryResult((void *)nws);
  1124. } else scode = 500;
  1125. } else scode = 404;
  1126. } else {
  1127. if (_controller)
  1128. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1129. else scode = 404;
  1130. }
  1131. } else scode = 401; // isAuth == false
  1132. } else if (httpMethod == HTTP_DELETE) {
  1133. if (isAuth) {
  1134. if (ps[0] == "network") {
  1135. ZT_VirtualNetworkList *nws = _node->networks();
  1136. if (nws) {
  1137. if (ps.size() == 2) {
  1138. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1139. for(unsigned long i=0;i<nws->networkCount;++i) {
  1140. if (nws->networks[i].nwid == wantnw) {
  1141. _node->leave(wantnw,(void **)0,(void *)0);
  1142. res["result"] = true;
  1143. scode = 200;
  1144. break;
  1145. }
  1146. }
  1147. } // else 404
  1148. _node->freeQueryResult((void *)nws);
  1149. } else scode = 500;
  1150. } else {
  1151. if (_controller)
  1152. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1153. else scode = 404;
  1154. }
  1155. } else scode = 401; // isAuth = false
  1156. } else {
  1157. scode = 400;
  1158. }
  1159. if (responseBody.length() == 0) {
  1160. if ((res.is_object())||(res.is_array()))
  1161. responseBody = OSUtils::jsonDump(res);
  1162. else responseBody = "{}";
  1163. responseContentType = "application/json";
  1164. }
  1165. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  1166. // Also double-check isAuth since forbidding this without auth feels safer.
  1167. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  1168. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  1169. if (responseBody.length() > 0)
  1170. responseBody = jsonp->second + "(" + responseBody + ");";
  1171. else responseBody = jsonp->second + "(null);";
  1172. responseContentType = "application/javascript";
  1173. }
  1174. return scode;
  1175. }
  1176. // Must be called after _localConfig is read or modified
  1177. void applyLocalConfig()
  1178. {
  1179. Mutex::Lock _l(_localConfig_m);
  1180. json lc(_localConfig);
  1181. _v4Hints.clear();
  1182. _v6Hints.clear();
  1183. _v4Blacklists.clear();
  1184. _v6Blacklists.clear();
  1185. json &virt = lc["virtual"];
  1186. if (virt.is_object()) {
  1187. for(json::iterator v(virt.begin());v!=virt.end();++v) {
  1188. const std::string nstr = v.key();
  1189. if ((nstr.length() == ZT_ADDRESS_LENGTH_HEX)&&(v.value().is_object())) {
  1190. const Address ztaddr(Utils::hexStrToU64(nstr.c_str()));
  1191. if (ztaddr) {
  1192. const uint64_t ztaddr2 = ztaddr.toInt();
  1193. std::vector<InetAddress> &v4h = _v4Hints[ztaddr2];
  1194. std::vector<InetAddress> &v6h = _v6Hints[ztaddr2];
  1195. std::vector<InetAddress> &v4b = _v4Blacklists[ztaddr2];
  1196. std::vector<InetAddress> &v6b = _v6Blacklists[ztaddr2];
  1197. json &tryAddrs = v.value()["try"];
  1198. if (tryAddrs.is_array()) {
  1199. for(unsigned long i=0;i<tryAddrs.size();++i) {
  1200. const InetAddress ip(OSUtils::jsonString(tryAddrs[i],"").c_str());
  1201. if (ip.ss_family == AF_INET)
  1202. v4h.push_back(ip);
  1203. else if (ip.ss_family == AF_INET6)
  1204. v6h.push_back(ip);
  1205. }
  1206. }
  1207. json &blAddrs = v.value()["blacklist"];
  1208. if (blAddrs.is_array()) {
  1209. for(unsigned long i=0;i<blAddrs.size();++i) {
  1210. const InetAddress ip(OSUtils::jsonString(blAddrs[i],"").c_str());
  1211. if (ip.ss_family == AF_INET)
  1212. v4b.push_back(ip);
  1213. else if (ip.ss_family == AF_INET6)
  1214. v6b.push_back(ip);
  1215. }
  1216. }
  1217. if (v4h.empty()) _v4Hints.erase(ztaddr2);
  1218. if (v6h.empty()) _v6Hints.erase(ztaddr2);
  1219. if (v4b.empty()) _v4Blacklists.erase(ztaddr2);
  1220. if (v6b.empty()) _v6Blacklists.erase(ztaddr2);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. _globalV4Blacklist.clear();
  1226. _globalV6Blacklist.clear();
  1227. json &physical = lc["physical"];
  1228. if (physical.is_object()) {
  1229. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  1230. const InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  1231. if ((net)&&(net.netmaskBits() > 0)) {
  1232. if (phy.value().is_object()) {
  1233. if (OSUtils::jsonBool(phy.value()["blacklist"],false)) {
  1234. if (net.ss_family == AF_INET)
  1235. _globalV4Blacklist.push_back(net);
  1236. else if (net.ss_family == AF_INET6)
  1237. _globalV6Blacklist.push_back(net);
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. _allowManagementFrom.clear();
  1244. _interfacePrefixBlacklist.clear();
  1245. json &settings = lc["settings"];
  1246. _primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1247. _allowSecondaryPort = OSUtils::jsonBool(settings["allowSecondaryPort"],true);
  1248. _secondaryPort = (unsigned int)OSUtils::jsonInt(settings["secondaryPort"],0);
  1249. _tertiaryPort = (unsigned int)OSUtils::jsonInt(settings["tertiaryPort"],0);
  1250. if (_secondaryPort != 0 || _tertiaryPort != 0) {
  1251. fprintf(stderr,"WARNING: using manually-specified ports. This can cause NAT issues." ZT_EOL_S);
  1252. }
  1253. _multipathMode = (unsigned int)OSUtils::jsonInt(settings["multipathMode"],0);
  1254. _portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1255. #ifndef ZT_SDK
  1256. const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT));
  1257. const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false);
  1258. if (((up == "apply")||(up == "download"))||(udist)) {
  1259. if (!_updater)
  1260. _updater = new SoftwareUpdater(*_node,_homePath);
  1261. _updateAutoApply = (up == "apply");
  1262. _updater->setUpdateDistribution(udist);
  1263. _updater->setChannel(OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL));
  1264. } else {
  1265. delete _updater;
  1266. _updater = (SoftwareUpdater *)0;
  1267. _updateAutoApply = false;
  1268. }
  1269. #endif
  1270. json &ignoreIfs = settings["interfacePrefixBlacklist"];
  1271. if (ignoreIfs.is_array()) {
  1272. for(unsigned long i=0;i<ignoreIfs.size();++i) {
  1273. const std::string tmp(OSUtils::jsonString(ignoreIfs[i],""));
  1274. if (tmp.length() > 0)
  1275. _interfacePrefixBlacklist.push_back(tmp);
  1276. }
  1277. }
  1278. json &amf = settings["allowManagementFrom"];
  1279. if (amf.is_array()) {
  1280. for(unsigned long i=0;i<amf.size();++i) {
  1281. const InetAddress nw(OSUtils::jsonString(amf[i],"").c_str());
  1282. if (nw)
  1283. _allowManagementFrom.push_back(nw);
  1284. }
  1285. }
  1286. }
  1287. // Checks if a managed IP or route target is allowed
  1288. bool checkIfManagedIsAllowed(const NetworkState &n,const InetAddress &target)
  1289. {
  1290. if (!n.settings.allowManaged)
  1291. return false;
  1292. if (n.settings.allowManagedWhitelist.size() > 0) {
  1293. bool allowed = false;
  1294. for (InetAddress addr : n.settings.allowManagedWhitelist) {
  1295. if (addr.containsAddress(target) && addr.netmaskBits() <= target.netmaskBits()) {
  1296. allowed = true;
  1297. break;
  1298. }
  1299. }
  1300. if (!allowed) return false;
  1301. }
  1302. if (target.isDefaultRoute())
  1303. return n.settings.allowDefault;
  1304. switch(target.ipScope()) {
  1305. case InetAddress::IP_SCOPE_NONE:
  1306. case InetAddress::IP_SCOPE_MULTICAST:
  1307. case InetAddress::IP_SCOPE_LOOPBACK:
  1308. case InetAddress::IP_SCOPE_LINK_LOCAL:
  1309. return false;
  1310. case InetAddress::IP_SCOPE_GLOBAL:
  1311. return n.settings.allowGlobal;
  1312. default:
  1313. return true;
  1314. }
  1315. }
  1316. // Match only an IP from a vector of IPs -- used in syncManagedStuff()
  1317. bool matchIpOnly(const std::vector<InetAddress> &ips,const InetAddress &ip) const
  1318. {
  1319. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1320. if (i->ipsEqual(ip))
  1321. return true;
  1322. }
  1323. return false;
  1324. }
  1325. // Apply or update managed IPs for a configured network (be sure n.tap exists)
  1326. void syncManagedStuff(NetworkState &n,bool syncIps,bool syncRoutes)
  1327. {
  1328. char ipbuf[64];
  1329. // assumes _nets_m is locked
  1330. if (syncIps) {
  1331. std::vector<InetAddress> newManagedIps;
  1332. newManagedIps.reserve(n.config.assignedAddressCount);
  1333. for(unsigned int i=0;i<n.config.assignedAddressCount;++i) {
  1334. const InetAddress *ii = reinterpret_cast<const InetAddress *>(&(n.config.assignedAddresses[i]));
  1335. if (checkIfManagedIsAllowed(n,*ii))
  1336. newManagedIps.push_back(*ii);
  1337. }
  1338. std::sort(newManagedIps.begin(),newManagedIps.end());
  1339. newManagedIps.erase(std::unique(newManagedIps.begin(),newManagedIps.end()),newManagedIps.end());
  1340. for(std::vector<InetAddress>::iterator ip(n.managedIps.begin());ip!=n.managedIps.end();++ip) {
  1341. if (std::find(newManagedIps.begin(),newManagedIps.end(),*ip) == newManagedIps.end()) {
  1342. if (!n.tap->removeIp(*ip))
  1343. fprintf(stderr,"ERROR: unable to remove ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  1344. }
  1345. }
  1346. #ifdef __SYNOLOGY__
  1347. if (!n.tap->addIpSyn(newManagedIps))
  1348. fprintf(stderr,"ERROR: unable to add ip addresses to ifcfg" ZT_EOL_S);
  1349. #else
  1350. for(std::vector<InetAddress>::iterator ip(newManagedIps.begin());ip!=newManagedIps.end();++ip) {
  1351. if (std::find(n.managedIps.begin(),n.managedIps.end(),*ip) == n.managedIps.end()) {
  1352. if (!n.tap->addIp(*ip))
  1353. fprintf(stderr,"ERROR: unable to add ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  1354. }
  1355. }
  1356. #endif
  1357. n.managedIps.swap(newManagedIps);
  1358. }
  1359. if (syncRoutes) {
  1360. char tapdev[64];
  1361. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  1362. OSUtils::ztsnprintf(tapdev,sizeof(tapdev),"%.16llx",(unsigned long long)((WindowsEthernetTap *)(n.tap.get()))->luid().Value);
  1363. #else
  1364. Utils::scopy(tapdev,sizeof(tapdev),n.tap->deviceName().c_str());
  1365. #endif
  1366. std::vector<InetAddress> myIps(n.tap->ips());
  1367. // Nuke applied routes that are no longer in n.config.routes[] and/or are not allowed
  1368. for(std::list< SharedPtr<ManagedRoute> >::iterator mr(n.managedRoutes.begin());mr!=n.managedRoutes.end();) {
  1369. bool haveRoute = false;
  1370. if ( (checkIfManagedIsAllowed(n,(*mr)->target())) && (((*mr)->via().ss_family != (*mr)->target().ss_family)||(!matchIpOnly(myIps,(*mr)->via()))) ) {
  1371. for(unsigned int i=0;i<n.config.routeCount;++i) {
  1372. const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
  1373. const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
  1374. if ( ((*mr)->target() == *target) && ( ((via->ss_family == target->ss_family)&&((*mr)->via().ipsEqual(*via))) || (strcmp(tapdev,(*mr)->device())==0) ) ) {
  1375. haveRoute = true;
  1376. break;
  1377. }
  1378. }
  1379. }
  1380. if (haveRoute) {
  1381. ++mr;
  1382. } else {
  1383. n.managedRoutes.erase(mr++);
  1384. }
  1385. }
  1386. // Apply routes in n.config.routes[] that we haven't applied yet, and sync those we have in case shadow routes need to change
  1387. for(unsigned int i=0;i<n.config.routeCount;++i) {
  1388. const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
  1389. const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
  1390. const InetAddress *src = NULL;
  1391. for (unsigned int j=0; j<n.config.assignedAddressCount; ++j) {
  1392. const InetAddress *const tmp = reinterpret_cast<const InetAddress *>(&(n.config.assignedAddresses[j]));
  1393. if (target->isV4() && tmp->isV4()) {
  1394. src = reinterpret_cast<InetAddress *>(&(n.config.assignedAddresses[j]));
  1395. break;
  1396. } else if (target->isV6() && tmp->isV6()) {
  1397. src = reinterpret_cast<InetAddress *>(&(n.config.assignedAddresses[j]));
  1398. break;
  1399. }
  1400. }
  1401. if (!src)
  1402. src = &NULL_INET_ADDR;
  1403. if ( (!checkIfManagedIsAllowed(n,*target)) || ((via->ss_family == target->ss_family)&&(matchIpOnly(myIps,*via))) )
  1404. continue;
  1405. bool haveRoute = false;
  1406. // Ignore routes implied by local managed IPs since adding the IP adds the route
  1407. #ifndef __APPLE__
  1408. for(std::vector<InetAddress>::iterator ip(n.managedIps.begin());ip!=n.managedIps.end();++ip) {
  1409. if ((target->netmaskBits() == ip->netmaskBits())&&(target->containsAddress(*ip))) {
  1410. haveRoute = true;
  1411. break;
  1412. }
  1413. }
  1414. #endif
  1415. if (haveRoute)
  1416. continue;
  1417. #ifndef ZT_SDK
  1418. // If we've already applied this route, just sync it and continue
  1419. for(std::list< SharedPtr<ManagedRoute> >::iterator mr(n.managedRoutes.begin());mr!=n.managedRoutes.end();++mr) {
  1420. if ( ((*mr)->target() == *target) && ( ((via->ss_family == target->ss_family)&&((*mr)->via().ipsEqual(*via))) || (tapdev == (*mr)->device()) ) ) {
  1421. haveRoute = true;
  1422. (*mr)->sync();
  1423. break;
  1424. }
  1425. }
  1426. if (haveRoute)
  1427. continue;
  1428. // Add and apply new routes
  1429. n.managedRoutes.push_back(SharedPtr<ManagedRoute>(new ManagedRoute(*target,*via,*src,tapdev)));
  1430. if (!n.managedRoutes.back()->sync())
  1431. n.managedRoutes.pop_back();
  1432. #endif
  1433. }
  1434. }
  1435. }
  1436. // =========================================================================
  1437. // Handlers for Node and Phy<> callbacks
  1438. // =========================================================================
  1439. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len)
  1440. {
  1441. const uint64_t now = OSUtils::now();
  1442. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL))
  1443. _lastDirectReceiveFromGlobal = now;
  1444. const ZT_ResultCode rc = _node->processWirePacket(nullptr,now,reinterpret_cast<int64_t>(sock),reinterpret_cast<const struct sockaddr_storage *>(from),data,len,&_nextBackgroundTaskDeadline);
  1445. if (ZT_ResultCode_isFatal(rc)) {
  1446. char tmp[256];
  1447. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  1448. Mutex::Lock _l(_termReason_m);
  1449. _termReason = ONE_UNRECOVERABLE_ERROR;
  1450. _fatalErrorMessage = tmp;
  1451. this->terminate();
  1452. }
  1453. }
  1454. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  1455. {
  1456. _phy.close(sock,true);
  1457. }
  1458. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
  1459. {
  1460. if (!from) {
  1461. _phy.close(sockN,false);
  1462. return;
  1463. } else {
  1464. #ifdef ZT_SDK
  1465. // Immediately close new local connections. The intention is to prevent the backplane from being accessed when operating as libzt
  1466. if (!allowHttpBackplaneManagement && ((InetAddress*)from)->ipScope() == InetAddress::IP_SCOPE_LOOPBACK) {
  1467. _phy.close(sockN,false);
  1468. return;
  1469. }
  1470. #endif
  1471. TcpConnection *tc = new TcpConnection();
  1472. {
  1473. Mutex::Lock _l(_tcpConnections_m);
  1474. _tcpConnections.push_back(tc);
  1475. }
  1476. tc->type = TcpConnection::TCP_UNCATEGORIZED_INCOMING;
  1477. tc->parent = this;
  1478. tc->sock = sockN;
  1479. tc->remoteAddr = from;
  1480. tc->lastReceive = OSUtils::now();
  1481. http_parser_init(&(tc->parser),HTTP_REQUEST);
  1482. tc->parser.data = (void *)tc;
  1483. tc->messageSize = 0;
  1484. *uptrN = (void *)tc;
  1485. }
  1486. }
  1487. void phyOnTcpClose(PhySocket *sock,void **uptr)
  1488. {
  1489. TcpConnection *tc = (TcpConnection *)*uptr;
  1490. if (tc) {
  1491. {
  1492. Mutex::Lock _l(_tcpConnections_m);
  1493. _tcpConnections.erase(std::remove(_tcpConnections.begin(),_tcpConnections.end(),tc),_tcpConnections.end());
  1494. }
  1495. delete tc;
  1496. }
  1497. }
  1498. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  1499. {
  1500. try {
  1501. if (!len) return; // sanity check, should never happen
  1502. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  1503. tc->lastReceive = OSUtils::now();
  1504. switch(tc->type) {
  1505. case TcpConnection::TCP_UNCATEGORIZED_INCOMING:
  1506. switch(reinterpret_cast<uint8_t *>(data)[0]) {
  1507. // HTTP: GET, PUT, POST, HEAD, DELETE
  1508. case 'G':
  1509. case 'P':
  1510. case 'D':
  1511. case 'H': {
  1512. // This is only allowed from IPs permitted to access the management
  1513. // backplane, which is just 127.0.0.1/::1 unless otherwise configured.
  1514. bool allow;
  1515. {
  1516. Mutex::Lock _l(_localConfig_m);
  1517. if (_allowManagementFrom.size() == 0) {
  1518. allow = (tc->remoteAddr.ipScope() == InetAddress::IP_SCOPE_LOOPBACK);
  1519. } else {
  1520. allow = false;
  1521. for(std::vector<InetAddress>::const_iterator i(_allowManagementFrom.begin());i!=_allowManagementFrom.end();++i) {
  1522. if (i->containsAddress(tc->remoteAddr)) {
  1523. allow = true;
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. }
  1529. if (allow) {
  1530. tc->type = TcpConnection::TCP_HTTP_INCOMING;
  1531. phyOnTcpData(sock,uptr,data,len);
  1532. } else {
  1533. _phy.close(sock);
  1534. }
  1535. } break;
  1536. // Drop unknown protocols
  1537. default:
  1538. _phy.close(sock);
  1539. break;
  1540. }
  1541. return;
  1542. case TcpConnection::TCP_HTTP_INCOMING:
  1543. http_parser_execute(&(tc->parser),&HTTP_PARSER_SETTINGS,(const char *)data,len);
  1544. if ((tc->parser.upgrade)||(tc->parser.http_errno != HPE_OK))
  1545. _phy.close(sock);
  1546. return;
  1547. }
  1548. } catch ( ... ) {
  1549. _phy.close(sock);
  1550. }
  1551. }
  1552. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  1553. {
  1554. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  1555. bool closeit = false;
  1556. {
  1557. Mutex::Lock _l(tc->writeq_m);
  1558. if (tc->writeq.length() > 0) {
  1559. long sent = (long)_phy.streamSend(sock,tc->writeq.data(),(unsigned long)tc->writeq.length(),true);
  1560. if (sent > 0) {
  1561. if ((unsigned long)sent >= (unsigned long)tc->writeq.length()) {
  1562. tc->writeq.clear();
  1563. _phy.setNotifyWritable(sock,false);
  1564. if (tc->type == TcpConnection::TCP_HTTP_INCOMING)
  1565. closeit = true; // HTTP keep alive not supported
  1566. } else {
  1567. tc->writeq.erase(tc->writeq.begin(),tc->writeq.begin() + sent);
  1568. }
  1569. }
  1570. } else {
  1571. _phy.setNotifyWritable(sock,false);
  1572. }
  1573. }
  1574. if (closeit)
  1575. _phy.close(sock);
  1576. }
  1577. inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
  1578. inline void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {}
  1579. inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
  1580. inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  1581. inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {}
  1582. inline int nodeVirtualNetworkConfigFunction(uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwc)
  1583. {
  1584. Mutex::Lock _l(_nets_m);
  1585. NetworkState &n = _nets[nwid];
  1586. switch(op) {
  1587. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
  1588. if (!n.tap) {
  1589. try {
  1590. char friendlyName[128];
  1591. OSUtils::ztsnprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid);
  1592. n.tap = EthernetTap::newInstance(
  1593. nullptr,
  1594. _homePath.c_str(),
  1595. MAC(nwc->mac),
  1596. nwc->mtu,
  1597. (unsigned int)ZT_IF_METRIC,
  1598. nwid,
  1599. friendlyName,
  1600. StapFrameHandler,
  1601. (void *)this);
  1602. *nuptr = (void *)&n;
  1603. char nlcpath[256];
  1604. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  1605. std::string nlcbuf;
  1606. if (OSUtils::readFile(nlcpath,nlcbuf)) {
  1607. Dictionary<4096> nc;
  1608. nc.load(nlcbuf.c_str());
  1609. Buffer<1024> allowManaged;
  1610. if (nc.get("allowManaged", allowManaged) && allowManaged.size() != 0) {
  1611. std::string addresses (allowManaged.begin(), allowManaged.size());
  1612. if (allowManaged.size() <= 5) { // untidy parsing for backward compatibility
  1613. if (allowManaged[0] == '1' || allowManaged[0] == 't' || allowManaged[0] == 'T') {
  1614. n.settings.allowManaged = true;
  1615. } else {
  1616. n.settings.allowManaged = false;
  1617. }
  1618. } else {
  1619. // this should be a list of IP addresses
  1620. n.settings.allowManaged = true;
  1621. size_t pos = 0;
  1622. while (true) {
  1623. size_t nextPos = addresses.find(',', pos);
  1624. std::string address = addresses.substr(pos, (nextPos == std::string::npos ? addresses.size() : nextPos) - pos);
  1625. n.settings.allowManagedWhitelist.push_back(InetAddress(address.c_str()));
  1626. if (nextPos == std::string::npos) break;
  1627. pos = nextPos + 1;
  1628. }
  1629. }
  1630. } else {
  1631. n.settings.allowManaged = true;
  1632. }
  1633. n.settings.allowGlobal = nc.getB("allowGlobal", false);
  1634. n.settings.allowDefault = nc.getB("allowDefault", false);
  1635. }
  1636. } catch (std::exception &exc) {
  1637. #ifdef __WINDOWS__
  1638. FILE *tapFailLog = fopen((_homePath + ZT_PATH_SEPARATOR_S"port_error_log.txt").c_str(),"a");
  1639. if (tapFailLog) {
  1640. fprintf(tapFailLog,"%.16llx: %s" ZT_EOL_S,(unsigned long long)nwid,exc.what());
  1641. fclose(tapFailLog);
  1642. }
  1643. #else
  1644. fprintf(stderr,"ERROR: unable to configure virtual network port: %s" ZT_EOL_S,exc.what());
  1645. #endif
  1646. _nets.erase(nwid);
  1647. return -999;
  1648. } catch ( ... ) {
  1649. return -999; // tap init failed
  1650. }
  1651. }
  1652. // After setting up tap, fall through to CONFIG_UPDATE since we also want to do this...
  1653. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
  1654. memcpy(&(n.config),nwc,sizeof(ZT_VirtualNetworkConfig));
  1655. if (n.tap) { // sanity check
  1656. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  1657. // wait for up to 5 seconds for the WindowsEthernetTap to actually be initialized
  1658. //
  1659. // without WindowsEthernetTap::isInitialized() returning true, the won't actually
  1660. // be online yet and setting managed routes on it will fail.
  1661. const int MAX_SLEEP_COUNT = 500;
  1662. for (int i = 0; !((WindowsEthernetTap *)(n.tap.get()))->isInitialized() && i < MAX_SLEEP_COUNT; i++) {
  1663. Sleep(10);
  1664. }
  1665. #endif
  1666. syncManagedStuff(n,true,true);
  1667. n.tap->setMtu(nwc->mtu);
  1668. } else {
  1669. _nets.erase(nwid);
  1670. return -999; // tap init failed
  1671. }
  1672. break;
  1673. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
  1674. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
  1675. if (n.tap) { // sanity check
  1676. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  1677. std::string winInstanceId(((WindowsEthernetTap *)(n.tap.get()))->instanceId());
  1678. #endif
  1679. *nuptr = (void *)0;
  1680. n.tap.reset();
  1681. _nets.erase(nwid);
  1682. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  1683. if ((op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY)&&(winInstanceId.length() > 0))
  1684. WindowsEthernetTap::deletePersistentTapDevice(winInstanceId.c_str());
  1685. #endif
  1686. if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) {
  1687. char nlcpath[256];
  1688. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  1689. OSUtils::rm(nlcpath);
  1690. }
  1691. } else {
  1692. _nets.erase(nwid);
  1693. }
  1694. break;
  1695. }
  1696. return 0;
  1697. }
  1698. inline void nodeEventCallback(enum ZT_Event event,const void *metaData)
  1699. {
  1700. switch(event) {
  1701. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  1702. Mutex::Lock _l(_termReason_m);
  1703. _termReason = ONE_IDENTITY_COLLISION;
  1704. _fatalErrorMessage = "identity/address collision";
  1705. this->terminate();
  1706. } break;
  1707. case ZT_EVENT_TRACE: {
  1708. if (metaData) {
  1709. ::fprintf(stderr,"%s" ZT_EOL_S,(const char *)metaData);
  1710. ::fflush(stderr);
  1711. }
  1712. } break;
  1713. case ZT_EVENT_USER_MESSAGE: {
  1714. const ZT_UserMessage *um = reinterpret_cast<const ZT_UserMessage *>(metaData);
  1715. if ((um->typeId == ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE)&&(_updater)) {
  1716. _updater->handleSoftwareUpdateUserMessage(um->origin,um->data,um->length);
  1717. }
  1718. } break;
  1719. case ZT_EVENT_REMOTE_TRACE: {
  1720. // TODO
  1721. }
  1722. default:
  1723. break;
  1724. }
  1725. }
  1726. inline void nodeStatePutFunction(enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  1727. {
  1728. char p[1024];
  1729. FILE *f;
  1730. bool secure = false;
  1731. char dirname[1024];
  1732. dirname[0] = 0;
  1733. switch(type) {
  1734. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  1735. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  1736. break;
  1737. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  1738. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  1739. secure = true;
  1740. break;
  1741. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  1742. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "networks.d",_homePath.c_str());
  1743. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.conf",dirname,(unsigned long long)id[0]);
  1744. secure = true;
  1745. break;
  1746. case ZT_STATE_OBJECT_PEER:
  1747. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "peers.d",_homePath.c_str());
  1748. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.10llx.peer",dirname,(unsigned long long)id[0]);
  1749. break;
  1750. default:
  1751. return;
  1752. }
  1753. if ((len >= 0)&&(data)) {
  1754. // Check to see if we've already written this first. This reduces
  1755. // redundant writes and I/O overhead on most platforms and has
  1756. // little effect on others.
  1757. f = fopen(p,"rb");
  1758. if (f) {
  1759. char *const buf = (char *)malloc(len*4);
  1760. if (buf) {
  1761. long l = (long)fread(buf,1,(size_t)(len*4),f);
  1762. fclose(f);
  1763. if ((l == (long)len)&&(memcmp(data,buf,l) == 0)) {
  1764. free(buf);
  1765. return;
  1766. }
  1767. free(buf);
  1768. }
  1769. }
  1770. f = fopen(p,"wb");
  1771. if ((!f)&&(dirname[0])) { // create subdirectory if it does not exist
  1772. OSUtils::mkdir(dirname);
  1773. f = fopen(p,"wb");
  1774. }
  1775. if (f) {
  1776. if (fwrite(data,len,1,f) != 1)
  1777. fprintf(stderr,"WARNING: unable to write to file: %s (I/O error)" ZT_EOL_S,p);
  1778. fclose(f);
  1779. if (secure)
  1780. OSUtils::lockDownFile(p,false);
  1781. } else {
  1782. fprintf(stderr,"WARNING: unable to write to file: %s (unable to open)" ZT_EOL_S,p);
  1783. }
  1784. } else {
  1785. OSUtils::rm(p);
  1786. }
  1787. }
  1788. inline int nodeStateGetFunction(enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  1789. {
  1790. char p[4096];
  1791. switch(type) {
  1792. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  1793. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  1794. break;
  1795. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  1796. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  1797. break;
  1798. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  1799. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf",_homePath.c_str(),(unsigned long long)id[0]);
  1800. break;
  1801. case ZT_STATE_OBJECT_PEER:
  1802. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "peers.d" ZT_PATH_SEPARATOR_S "%.10llx.peer",_homePath.c_str(),(unsigned long long)id[0]);
  1803. break;
  1804. default:
  1805. return -1;
  1806. }
  1807. FILE *f = fopen(p,"rb");
  1808. if (f) {
  1809. int n = (int)fread(data,1,maxlen,f);
  1810. fclose(f);
  1811. if (n >= 0)
  1812. return n;
  1813. }
  1814. return -1;
  1815. }
  1816. inline int nodeWirePacketSendFunction(const int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  1817. {
  1818. if ((localSocket != -1)&&(localSocket != 0)&&(_binder.isUdpSocketValid((PhySocket *)((uintptr_t)localSocket)))) {
  1819. if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),ttl);
  1820. const bool r = _phy.udpSend((PhySocket *)((uintptr_t)localSocket),(const struct sockaddr *)addr,data,len);
  1821. if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),255);
  1822. return ((r) ? 0 : -1);
  1823. } else {
  1824. return ((_binder.udpSendAll(_phy,addr,data,len,ttl)) ? 0 : -1);
  1825. }
  1826. }
  1827. inline void nodeVirtualNetworkFrameFunction(uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  1828. {
  1829. NetworkState *n = reinterpret_cast<NetworkState *>(*nuptr);
  1830. if ((!n)||(!n->tap))
  1831. return;
  1832. n->tap->put(MAC(sourceMac),MAC(destMac),etherType,data,len);
  1833. }
  1834. inline int nodePathCheckFunction(uint64_t ztaddr,const int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  1835. {
  1836. // Make sure we're not trying to do ZeroTier-over-ZeroTier
  1837. {
  1838. Mutex::Lock _l(_nets_m);
  1839. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  1840. if (n->second.tap) {
  1841. std::vector<InetAddress> ips(n->second.tap->ips());
  1842. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1843. if (i->containsAddress(*(reinterpret_cast<const InetAddress *>(remoteAddr)))) {
  1844. return 0;
  1845. }
  1846. }
  1847. }
  1848. }
  1849. }
  1850. /* Note: I do not think we need to scan for overlap with managed routes
  1851. * because of the "route forking" and interface binding that we do. This
  1852. * ensures (we hope) that ZeroTier traffic will still take the physical
  1853. * path even if its managed routes override this for other traffic. Will
  1854. * revisit if we see recursion problems. */
  1855. // Check blacklists
  1856. const Hashtable< uint64_t,std::vector<InetAddress> > *blh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  1857. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  1858. if (remoteAddr->ss_family == AF_INET) {
  1859. blh = &_v4Blacklists;
  1860. gbl = &_globalV4Blacklist;
  1861. } else if (remoteAddr->ss_family == AF_INET6) {
  1862. blh = &_v6Blacklists;
  1863. gbl = &_globalV6Blacklist;
  1864. }
  1865. if (blh) {
  1866. Mutex::Lock _l(_localConfig_m);
  1867. const std::vector<InetAddress> *l = blh->get(ztaddr);
  1868. if (l) {
  1869. for(std::vector<InetAddress>::const_iterator a(l->begin());a!=l->end();++a) {
  1870. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  1871. return 0;
  1872. }
  1873. }
  1874. }
  1875. if (gbl) {
  1876. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  1877. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  1878. return 0;
  1879. }
  1880. }
  1881. return 1;
  1882. }
  1883. inline int nodePathLookupFunction(uint64_t ztaddr,int family,struct sockaddr_storage *result)
  1884. {
  1885. const Hashtable< uint64_t,std::vector<InetAddress> > *lh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  1886. if (family < 0)
  1887. lh = (_node->prng() & 1) ? &_v4Hints : &_v6Hints;
  1888. else if (family == AF_INET)
  1889. lh = &_v4Hints;
  1890. else if (family == AF_INET6)
  1891. lh = &_v6Hints;
  1892. else return 0;
  1893. const std::vector<InetAddress> *l = lh->get(ztaddr);
  1894. if ((l)&&(l->size() > 0)) {
  1895. memcpy(result,&((*l)[(unsigned long)_node->prng() % l->size()]),sizeof(struct sockaddr_storage));
  1896. return 1;
  1897. } else return 0;
  1898. }
  1899. inline void tapFrameHandler(uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  1900. {
  1901. _node->processVirtualNetworkFrame((void *)0,OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&_nextBackgroundTaskDeadline);
  1902. }
  1903. inline void onHttpRequestToServer(TcpConnection *tc)
  1904. {
  1905. char tmpn[4096];
  1906. std::string data;
  1907. std::string contentType("text/plain"); // default if not changed in handleRequest()
  1908. unsigned int scode = 404;
  1909. // Note that we check allowed IP ranges when HTTP connections are first detected in
  1910. // phyOnTcpData(). If we made it here the source IP is okay.
  1911. try {
  1912. scode = handleControlPlaneHttpRequest(tc->remoteAddr,tc->parser.method,tc->url,tc->headers,tc->readq,data,contentType);
  1913. } catch (std::exception &exc) {
  1914. fprintf(stderr,"WARNING: unexpected exception processing control HTTP request: %s" ZT_EOL_S,exc.what());
  1915. scode = 500;
  1916. } catch ( ... ) {
  1917. fprintf(stderr,"WARNING: unexpected exception processing control HTTP request: unknown exception" ZT_EOL_S);
  1918. scode = 500;
  1919. }
  1920. const char *scodestr;
  1921. switch(scode) {
  1922. case 200: scodestr = "OK"; break;
  1923. case 400: scodestr = "Bad Request"; break;
  1924. case 401: scodestr = "Unauthorized"; break;
  1925. case 403: scodestr = "Forbidden"; break;
  1926. case 404: scodestr = "Not Found"; break;
  1927. case 500: scodestr = "Internal Server Error"; break;
  1928. case 501: scodestr = "Not Implemented"; break;
  1929. case 503: scodestr = "Service Unavailable"; break;
  1930. default: scodestr = "Error"; break;
  1931. }
  1932. OSUtils::ztsnprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nContent-Type: %s\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n",
  1933. scode,
  1934. scodestr,
  1935. contentType.c_str(),
  1936. (unsigned long)data.length());
  1937. {
  1938. Mutex::Lock _l(tc->writeq_m);
  1939. tc->writeq = tmpn;
  1940. if (tc->parser.method != HTTP_HEAD)
  1941. tc->writeq.append(data);
  1942. }
  1943. _phy.setNotifyWritable(tc->sock,true);
  1944. }
  1945. inline void onHttpResponseFromClient(TcpConnection *tc)
  1946. {
  1947. _phy.close(tc->sock);
  1948. }
  1949. bool shouldBindInterface(const char *ifname,const InetAddress &ifaddr)
  1950. {
  1951. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  1952. if ((ifname[0] == 'l')&&(ifname[1] == 'o')) return false; // loopback
  1953. if ((ifname[0] == 'z')&&(ifname[1] == 't')) return false; // sanity check: zt#
  1954. if ((ifname[0] == 't')&&(ifname[1] == 'u')&&(ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  1955. if ((ifname[0] == 't')&&(ifname[1] == 'a')&&(ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  1956. #endif
  1957. #ifdef __APPLE__
  1958. if ((ifname[0] == 'f')&&(ifname[1] == 'e')&&(ifname[2] == 't')&&(ifname[3] == 'h')) return false; // ... as is feth#
  1959. if ((ifname[0] == 'l')&&(ifname[1] == 'o')) return false; // loopback
  1960. if ((ifname[0] == 'z')&&(ifname[1] == 't')) return false; // sanity check: zt#
  1961. if ((ifname[0] == 't')&&(ifname[1] == 'u')&&(ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  1962. if ((ifname[0] == 't')&&(ifname[1] == 'a')&&(ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  1963. if ((ifname[0] == 'u')&&(ifname[1] == 't')&&(ifname[2] == 'u')&&(ifname[3] == 'n')) return false; // ... as is utun#
  1964. #endif
  1965. {
  1966. Mutex::Lock _l(_localConfig_m);
  1967. for(std::vector<std::string>::const_iterator p(_interfacePrefixBlacklist.begin());p!=_interfacePrefixBlacklist.end();++p) {
  1968. if (!strncmp(p->c_str(),ifname,p->length()))
  1969. return false;
  1970. }
  1971. }
  1972. {
  1973. // Check global blacklists
  1974. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  1975. if (ifaddr.ss_family == AF_INET) {
  1976. gbl = &_globalV4Blacklist;
  1977. } else if (ifaddr.ss_family == AF_INET6) {
  1978. gbl = &_globalV6Blacklist;
  1979. }
  1980. if (gbl) {
  1981. Mutex::Lock _l(_localConfig_m);
  1982. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  1983. if (a->containsAddress(ifaddr))
  1984. return false;
  1985. }
  1986. }
  1987. }
  1988. {
  1989. Mutex::Lock _l(_nets_m);
  1990. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  1991. if (n->second.tap) {
  1992. std::vector<InetAddress> ips(n->second.tap->ips());
  1993. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1994. if (i->ipsEqual(ifaddr))
  1995. return false;
  1996. }
  1997. }
  1998. }
  1999. }
  2000. return true;
  2001. }
  2002. bool _trialBind(unsigned int port)
  2003. {
  2004. struct sockaddr_in in4;
  2005. struct sockaddr_in6 in6;
  2006. PhySocket *tb;
  2007. memset(&in4,0,sizeof(in4));
  2008. in4.sin_family = AF_INET;
  2009. in4.sin_port = Utils::hton((uint16_t)port);
  2010. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0,0);
  2011. if (tb) {
  2012. _phy.close(tb,false);
  2013. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0);
  2014. if (tb) {
  2015. _phy.close(tb,false);
  2016. return true;
  2017. }
  2018. }
  2019. memset(&in6,0,sizeof(in6));
  2020. in6.sin6_family = AF_INET6;
  2021. in6.sin6_port = Utils::hton((uint16_t)port);
  2022. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0,0);
  2023. if (tb) {
  2024. _phy.close(tb,false);
  2025. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0);
  2026. if (tb) {
  2027. _phy.close(tb,false);
  2028. return true;
  2029. }
  2030. }
  2031. return false;
  2032. }
  2033. };
  2034. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf)
  2035. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkConfigFunction(nwid,nuptr,op,nwconf); }
  2036. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData)
  2037. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeEventCallback(event,metaData); }
  2038. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  2039. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeStatePutFunction(type,id,data,len); }
  2040. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  2041. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeStateGetFunction(type,id,data,maxlen); }
  2042. static int SnodeWirePacketSendFunction(ZT_Node *node,void *uptr,void *tptr,int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  2043. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeWirePacketSendFunction(localSocket,addr,data,len,ttl); }
  2044. static void SnodeVirtualNetworkFrameFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,uint64_t sourceMac,uint64_t destMac,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  2045. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkFrameFunction(nwid,nuptr,sourceMac,destMac,etherType,vlanId,data,len); }
  2046. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  2047. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathCheckFunction(ztaddr,localSocket,remoteAddr); }
  2048. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result)
  2049. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathLookupFunction(ztaddr,family,result); }
  2050. static void StapFrameHandler(void *uptr,void *tptr,uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  2051. { reinterpret_cast<OneServiceImpl *>(uptr)->tapFrameHandler(nwid,from,to,etherType,vlanId,data,len); }
  2052. static int ShttpOnMessageBegin(http_parser *parser)
  2053. {
  2054. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2055. tc->currentHeaderField = "";
  2056. tc->currentHeaderValue = "";
  2057. tc->messageSize = 0;
  2058. tc->url.clear();
  2059. tc->status.clear();
  2060. tc->headers.clear();
  2061. tc->readq.clear();
  2062. return 0;
  2063. }
  2064. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  2065. {
  2066. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2067. tc->messageSize += (unsigned long)length;
  2068. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2069. return -1;
  2070. tc->url.append(ptr,length);
  2071. return 0;
  2072. }
  2073. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  2074. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  2075. #else
  2076. static int ShttpOnStatus(http_parser *parser)
  2077. #endif
  2078. { return 0; }
  2079. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  2080. {
  2081. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2082. tc->messageSize += (unsigned long)length;
  2083. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2084. return -1;
  2085. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length())) {
  2086. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  2087. tc->currentHeaderField = "";
  2088. tc->currentHeaderValue = "";
  2089. }
  2090. for(size_t i=0;i<length;++i)
  2091. tc->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  2092. return 0;
  2093. }
  2094. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length)
  2095. {
  2096. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2097. tc->messageSize += (unsigned long)length;
  2098. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2099. return -1;
  2100. tc->currentHeaderValue.append(ptr,length);
  2101. return 0;
  2102. }
  2103. static int ShttpOnHeadersComplete(http_parser *parser)
  2104. {
  2105. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2106. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length()))
  2107. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  2108. return 0;
  2109. }
  2110. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  2111. {
  2112. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2113. tc->messageSize += (unsigned long)length;
  2114. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2115. return -1;
  2116. tc->readq.append(ptr,length);
  2117. return 0;
  2118. }
  2119. static int ShttpOnMessageComplete(http_parser *parser)
  2120. {
  2121. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2122. if (tc->type == TcpConnection::TCP_HTTP_INCOMING) {
  2123. tc->parent->onHttpRequestToServer(tc);
  2124. } else {
  2125. tc->parent->onHttpResponseFromClient(tc);
  2126. }
  2127. return 0;
  2128. }
  2129. } // anonymous namespace
  2130. std::string OneService::platformDefaultHomePath()
  2131. {
  2132. return OSUtils::platformDefaultHomePath();
  2133. }
  2134. OneService *OneService::newInstance(const char *hp,unsigned int port) { return new OneServiceImpl(hp,port); }
  2135. OneService::~OneService() {}
  2136. } // namespace ZeroTier