OneService.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdint.h>
  17. #include <string>
  18. #include <map>
  19. #include <vector>
  20. #include <algorithm>
  21. #include <list>
  22. #include <thread>
  23. #include <mutex>
  24. #include <condition_variable>
  25. #include "../version.h"
  26. #include "../include/ZeroTierOne.h"
  27. #include "../node/Constants.hpp"
  28. #include "../node/Mutex.hpp"
  29. #include "../node/Node.hpp"
  30. #include "../node/Utils.hpp"
  31. #include "../node/InetAddress.hpp"
  32. #include "../node/MAC.hpp"
  33. #include "../node/Identity.hpp"
  34. #include "../node/World.hpp"
  35. #include "../node/Salsa20.hpp"
  36. #include "../node/Poly1305.hpp"
  37. #include "../node/SHA512.hpp"
  38. #include "../node/Bond.hpp"
  39. #include "../node/Peer.hpp"
  40. #include "../osdep/Phy.hpp"
  41. #include "../osdep/OSUtils.hpp"
  42. #include "../osdep/Http.hpp"
  43. #include "../osdep/PortMapper.hpp"
  44. #include "../osdep/Binder.hpp"
  45. #include "../osdep/ManagedRoute.hpp"
  46. #include "../osdep/BlockingQueue.hpp"
  47. #include "../osdep/Link.hpp"
  48. #include "OneService.hpp"
  49. #include "SoftwareUpdater.hpp"
  50. #ifdef __WINDOWS__
  51. #include <WinSock2.h>
  52. #include <Windows.h>
  53. #include <ShlObj.h>
  54. #include <netioapi.h>
  55. #include <iphlpapi.h>
  56. //#include <unistd.h>
  57. #define stat _stat
  58. #else
  59. #include <sys/types.h>
  60. #include <sys/socket.h>
  61. #include <sys/stat.h>
  62. #include <sys/wait.h>
  63. #include <unistd.h>
  64. #include <ifaddrs.h>
  65. #endif
  66. #ifdef __APPLE__
  67. #include "../osdep/MacDNSHelper.hpp"
  68. #elif defined(__WINDOWS__)
  69. #include "../osdep/WinDNSHelper.hpp"
  70. #endif
  71. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  72. #include <http_parser.h>
  73. #else
  74. #include "../ext/http-parser/http_parser.h"
  75. #endif
  76. #if ZT_VAULT_SUPPORT
  77. extern "C" {
  78. #include <curl/curl.h>
  79. }
  80. #endif
  81. #include "../ext/json/json.hpp"
  82. using json = nlohmann::json;
  83. #include "../controller/EmbeddedNetworkController.hpp"
  84. #include "../controller/PostgreSQL.hpp"
  85. #include "../controller/Redis.hpp"
  86. #include "../osdep/EthernetTap.hpp"
  87. #ifdef __WINDOWS__
  88. #include "../osdep/WindowsEthernetTap.hpp"
  89. #endif
  90. #ifndef ZT_SOFTWARE_UPDATE_DEFAULT
  91. #define ZT_SOFTWARE_UPDATE_DEFAULT "disable"
  92. #endif
  93. // Sanity limits for HTTP
  94. #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 64)
  95. #define ZT_MAX_HTTP_CONNECTIONS 65536
  96. // Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and also
  97. // bridged via ZeroTier to the same LAN traffic will (if the OS is sane) prefer WiFi.
  98. #define ZT_IF_METRIC 5000
  99. // How often to check for new multicast subscriptions on a tap device
  100. #define ZT_TAP_CHECK_MULTICAST_INTERVAL 5000
  101. // TCP fallback relay (run by ZeroTier, Inc. -- this will eventually go away)
  102. #ifndef ZT_SDK
  103. #define ZT_TCP_FALLBACK_RELAY "204.80.128.1/443"
  104. #endif
  105. // Frequency at which we re-resolve the TCP fallback relay
  106. #define ZT_TCP_FALLBACK_RERESOLVE_DELAY 86400000
  107. // Attempt to engage TCP fallback after this many ms of no reply to packets sent to global-scope IPs
  108. #define ZT_TCP_FALLBACK_AFTER 60000
  109. // How often to check for local interface addresses
  110. #define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
  111. // Maximum write buffer size for outgoing TCP connections (sanity limit)
  112. #define ZT_TCP_MAX_WRITEQ_SIZE 33554432
  113. // TCP activity timeout
  114. #define ZT_TCP_ACTIVITY_TIMEOUT 60000
  115. #if ZT_VAULT_SUPPORT
  116. size_t curlResponseWrite(void *ptr, size_t size, size_t nmemb, std::string *data)
  117. {
  118. data->append((char*)ptr, size * nmemb);
  119. return size * nmemb;
  120. }
  121. #endif
  122. namespace ZeroTier {
  123. namespace {
  124. static const InetAddress NULL_INET_ADDR;
  125. // Fake TLS hello for TCP tunnel outgoing connections (TUNNELED mode)
  126. static const char ZT_TCP_TUNNEL_HELLO[9] = { 0x17,0x03,0x03,0x00,0x04,(char)ZEROTIER_ONE_VERSION_MAJOR,(char)ZEROTIER_ONE_VERSION_MINOR,(char)((ZEROTIER_ONE_VERSION_REVISION >> 8) & 0xff),(char)(ZEROTIER_ONE_VERSION_REVISION & 0xff) };
  127. static std::string _trimString(const std::string &s)
  128. {
  129. unsigned long end = (unsigned long)s.length();
  130. while (end) {
  131. char c = s[end - 1];
  132. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  133. --end;
  134. else break;
  135. }
  136. unsigned long start = 0;
  137. while (start < end) {
  138. char c = s[start];
  139. if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t'))
  140. ++start;
  141. else break;
  142. }
  143. return s.substr(start,end - start);
  144. }
  145. static void _networkToJson(nlohmann::json &nj,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName,const OneService::NetworkSettings &localSettings)
  146. {
  147. char tmp[256];
  148. const char *nstatus = "",*ntype = "";
  149. switch(nc->status) {
  150. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  151. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  152. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  153. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  154. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  155. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  156. }
  157. switch(nc->type) {
  158. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  159. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  160. }
  161. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",nc->nwid);
  162. nj["id"] = tmp;
  163. nj["nwid"] = tmp;
  164. 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));
  165. nj["mac"] = tmp;
  166. nj["name"] = nc->name;
  167. nj["status"] = nstatus;
  168. nj["type"] = ntype;
  169. nj["mtu"] = nc->mtu;
  170. nj["dhcp"] = (bool)(nc->dhcp != 0);
  171. nj["bridge"] = (bool)(nc->bridge != 0);
  172. nj["broadcastEnabled"] = (bool)(nc->broadcastEnabled != 0);
  173. nj["portError"] = nc->portError;
  174. nj["netconfRevision"] = nc->netconfRevision;
  175. nj["portDeviceName"] = portDeviceName;
  176. nj["allowManaged"] = localSettings.allowManaged;
  177. nj["allowGlobal"] = localSettings.allowGlobal;
  178. nj["allowDefault"] = localSettings.allowDefault;
  179. nj["allowDNS"] = localSettings.allowDNS;
  180. nlohmann::json aa = nlohmann::json::array();
  181. for(unsigned int i=0;i<nc->assignedAddressCount;++i) {
  182. aa.push_back(reinterpret_cast<const InetAddress *>(&(nc->assignedAddresses[i]))->toString(tmp));
  183. }
  184. nj["assignedAddresses"] = aa;
  185. nlohmann::json ra = nlohmann::json::array();
  186. for(unsigned int i=0;i<nc->routeCount;++i) {
  187. nlohmann::json rj;
  188. rj["target"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].target))->toString(tmp);
  189. if (nc->routes[i].via.ss_family == nc->routes[i].target.ss_family)
  190. rj["via"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].via))->toIpString(tmp);
  191. else rj["via"] = nlohmann::json();
  192. rj["flags"] = (int)nc->routes[i].flags;
  193. rj["metric"] = (int)nc->routes[i].metric;
  194. ra.push_back(rj);
  195. }
  196. nj["routes"] = ra;
  197. nlohmann::json mca = nlohmann::json::array();
  198. for(unsigned int i=0;i<nc->multicastSubscriptionCount;++i) {
  199. nlohmann::json m;
  200. m["mac"] = MAC(nc->multicastSubscriptions[i].mac).toString(tmp);
  201. m["adi"] = nc->multicastSubscriptions[i].adi;
  202. mca.push_back(m);
  203. }
  204. nj["multicastSubscriptions"] = mca;
  205. nlohmann::json m;
  206. m["domain"] = nc->dns.domain;
  207. m["servers"] = nlohmann::json::array();
  208. for(int j=0;j<ZT_MAX_DNS_SERVERS;++j) {
  209. InetAddress a(nc->dns.server_addr[j]);
  210. if (a.isV4() || a.isV6()) {
  211. char buf[256];
  212. m["servers"].push_back(a.toIpString(buf));
  213. }
  214. }
  215. nj["dns"] = m;
  216. }
  217. static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer)
  218. {
  219. char tmp[256];
  220. const char *prole = "";
  221. switch(peer->role) {
  222. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  223. case ZT_PEER_ROLE_MOON: prole = "MOON"; break;
  224. case ZT_PEER_ROLE_PLANET: prole = "PLANET"; break;
  225. }
  226. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  227. pj["address"] = tmp;
  228. pj["versionMajor"] = peer->versionMajor;
  229. pj["versionMinor"] = peer->versionMinor;
  230. pj["versionRev"] = peer->versionRev;
  231. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev);
  232. pj["version"] = tmp;
  233. pj["latency"] = peer->latency;
  234. pj["role"] = prole;
  235. pj["isBonded"] = peer->isBonded;
  236. pj["bondingPolicy"] = peer->bondingPolicy;
  237. pj["isHealthy"] = peer->isHealthy;
  238. pj["numAliveLinks"] = peer->numAliveLinks;
  239. pj["numTotalLinks"] = peer->numTotalLinks;
  240. nlohmann::json pa = nlohmann::json::array();
  241. for(unsigned int i=0;i<peer->pathCount;++i) {
  242. int64_t lastSend = peer->paths[i].lastSend;
  243. int64_t lastReceive = peer->paths[i].lastReceive;
  244. nlohmann::json j;
  245. j["address"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  246. j["lastSend"] = (lastSend < 0) ? 0 : lastSend;
  247. j["lastReceive"] = (lastReceive < 0) ? 0 : lastReceive;
  248. j["trustedPathId"] = peer->paths[i].trustedPathId;
  249. j["active"] = (bool)(peer->paths[i].expired == 0);
  250. j["expired"] = (bool)(peer->paths[i].expired != 0);
  251. j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  252. pa.push_back(j);
  253. }
  254. pj["paths"] = pa;
  255. }
  256. static void _peerBondToJson(nlohmann::json &pj,const ZT_Peer *peer)
  257. {
  258. char tmp[256];
  259. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  260. //pj["aggregateLinkLatency"] = peer->latency;
  261. std::string policyStr = BondController::getPolicyStrByCode(peer->bondingPolicy);
  262. pj["policy"] = policyStr;
  263. nlohmann::json pa = nlohmann::json::array();
  264. for(unsigned int i=0;i<peer->pathCount;++i) {
  265. int64_t lastSend = peer->paths[i].lastSend;
  266. int64_t lastReceive = peer->paths[i].lastReceive;
  267. nlohmann::json j;
  268. j["ifname"] = std::string(peer->paths[i].ifname);
  269. j["path"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString(tmp);
  270. j["lastTX"] = (lastSend < 0) ? 0 : lastSend;
  271. j["lastRX"] = (lastReceive < 0) ? 0 : lastReceive;
  272. j["lat"] = peer->paths[i].latencyMean;
  273. j["pdv"] = peer->paths[i].latencyVariance;
  274. //j["trustedPathId"] = peer->paths[i].trustedPathId;
  275. //j["active"] = (bool)(peer->paths[i].expired == 0);
  276. //j["expired"] = (bool)(peer->paths[i].expired != 0);
  277. //j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  278. //j["ltm"] = peer->paths[i].latencyMax;
  279. //j["plr"] = peer->paths[i].packetLossRatio;
  280. //j["per"] = peer->paths[i].packetErrorRatio;
  281. //j["thr"] = peer->paths[i].throughputMean;
  282. //j["thm"] = peer->paths[i].throughputMax;
  283. //j["thv"] = peer->paths[i].throughputVariance;
  284. //j["avl"] = peer->paths[i].availability;
  285. //j["age"] = peer->paths[i].age;
  286. //j["alloc"] = peer->paths[i].allocation;
  287. //j["ifname"] = peer->paths[i].ifname;
  288. pa.push_back(j);
  289. }
  290. pj["links"] = pa;
  291. }
  292. static void _moonToJson(nlohmann::json &mj,const World &world)
  293. {
  294. char tmp[4096];
  295. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",world.id());
  296. mj["id"] = tmp;
  297. mj["timestamp"] = world.timestamp();
  298. mj["signature"] = Utils::hex(world.signature().data,ZT_C25519_SIGNATURE_LEN,tmp);
  299. mj["updatesMustBeSignedBy"] = Utils::hex(world.updatesMustBeSignedBy().data,ZT_C25519_PUBLIC_KEY_LEN,tmp);
  300. nlohmann::json ra = nlohmann::json::array();
  301. for(std::vector<World::Root>::const_iterator r(world.roots().begin());r!=world.roots().end();++r) {
  302. nlohmann::json rj;
  303. rj["identity"] = r->identity.toString(false,tmp);
  304. nlohmann::json eps = nlohmann::json::array();
  305. for(std::vector<InetAddress>::const_iterator a(r->stableEndpoints.begin());a!=r->stableEndpoints.end();++a)
  306. eps.push_back(a->toString(tmp));
  307. rj["stableEndpoints"] = eps;
  308. ra.push_back(rj);
  309. }
  310. mj["roots"] = ra;
  311. mj["waiting"] = false;
  312. }
  313. class OneServiceImpl;
  314. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf);
  315. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData);
  316. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len);
  317. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen);
  318. 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);
  319. 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);
  320. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr);
  321. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result);
  322. 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);
  323. static int ShttpOnMessageBegin(http_parser *parser);
  324. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  325. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  326. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  327. #else
  328. static int ShttpOnStatus(http_parser *parser);
  329. #endif
  330. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  331. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  332. static int ShttpOnHeadersComplete(http_parser *parser);
  333. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  334. static int ShttpOnMessageComplete(http_parser *parser);
  335. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 1)
  336. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  337. ShttpOnMessageBegin,
  338. ShttpOnUrl,
  339. ShttpOnStatus,
  340. ShttpOnHeaderField,
  341. ShttpOnValue,
  342. ShttpOnHeadersComplete,
  343. ShttpOnBody,
  344. ShttpOnMessageComplete
  345. };
  346. #else
  347. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  348. ShttpOnMessageBegin,
  349. ShttpOnUrl,
  350. ShttpOnHeaderField,
  351. ShttpOnValue,
  352. ShttpOnHeadersComplete,
  353. ShttpOnBody,
  354. ShttpOnMessageComplete
  355. };
  356. #endif
  357. /**
  358. * A TCP connection and related state and buffers
  359. */
  360. struct TcpConnection
  361. {
  362. enum {
  363. TCP_UNCATEGORIZED_INCOMING, // uncategorized incoming connection
  364. TCP_HTTP_INCOMING,
  365. TCP_HTTP_OUTGOING,
  366. TCP_TUNNEL_OUTGOING // TUNNELED mode proxy outbound connection
  367. } type;
  368. OneServiceImpl *parent;
  369. PhySocket *sock;
  370. InetAddress remoteAddr;
  371. uint64_t lastReceive;
  372. // Used for inbound HTTP connections
  373. http_parser parser;
  374. unsigned long messageSize;
  375. std::string currentHeaderField;
  376. std::string currentHeaderValue;
  377. std::string url;
  378. std::string status;
  379. std::map< std::string,std::string > headers;
  380. std::string readq;
  381. std::string writeq;
  382. Mutex writeq_m;
  383. };
  384. struct OneServiceIncomingPacket
  385. {
  386. uint64_t now;
  387. int64_t sock;
  388. struct sockaddr_storage from;
  389. unsigned int size;
  390. uint8_t data[ZT_MAX_MTU];
  391. };
  392. class OneServiceImpl : public OneService
  393. {
  394. public:
  395. // begin member variables --------------------------------------------------
  396. const std::string _homePath;
  397. std::string _authToken;
  398. std::string _controllerDbPath;
  399. const std::string _networksPath;
  400. const std::string _moonsPath;
  401. EmbeddedNetworkController *_controller;
  402. Phy<OneServiceImpl *> _phy;
  403. Node *_node;
  404. SoftwareUpdater *_updater;
  405. PhySocket *_localControlSocket4;
  406. PhySocket *_localControlSocket6;
  407. bool _updateAutoApply;
  408. bool _allowTcpFallbackRelay;
  409. bool _allowSecondaryPort;
  410. unsigned int _primaryPort;
  411. unsigned int _secondaryPort;
  412. unsigned int _tertiaryPort;
  413. volatile unsigned int _udpPortPickerCounter;
  414. // Local configuration and memo-ized information from it
  415. json _localConfig;
  416. Hashtable< uint64_t,std::vector<InetAddress> > _v4Hints;
  417. Hashtable< uint64_t,std::vector<InetAddress> > _v6Hints;
  418. Hashtable< uint64_t,std::vector<InetAddress> > _v4Blacklists;
  419. Hashtable< uint64_t,std::vector<InetAddress> > _v6Blacklists;
  420. std::vector< InetAddress > _globalV4Blacklist;
  421. std::vector< InetAddress > _globalV6Blacklist;
  422. std::vector< InetAddress > _allowManagementFrom;
  423. std::vector< std::string > _interfacePrefixBlacklist;
  424. Mutex _localConfig_m;
  425. std::vector<InetAddress> explicitBind;
  426. /*
  427. * To attempt to handle NAT/gateway craziness we use three local UDP ports:
  428. *
  429. * [0] is the normal/default port, usually 9993
  430. * [1] is a port derived from our ZeroTier address
  431. * [2] is a port computed from the normal/default for use with uPnP/NAT-PMP mappings
  432. *
  433. * [2] exists because on some gateways trying to do regular NAT-t interferes
  434. * destructively with uPnP port mapping behavior in very weird buggy ways.
  435. * It's only used if uPnP/NAT-PMP is enabled in this build.
  436. */
  437. unsigned int _ports[3];
  438. Binder _binder;
  439. // Time we last received a packet from a global address
  440. uint64_t _lastDirectReceiveFromGlobal;
  441. #ifdef ZT_TCP_FALLBACK_RELAY
  442. uint64_t _lastSendToGlobalV4;
  443. #endif
  444. // Last potential sleep/wake event
  445. uint64_t _lastRestart;
  446. // Deadline for the next background task service function
  447. volatile int64_t _nextBackgroundTaskDeadline;
  448. // Configured networks
  449. struct NetworkState
  450. {
  451. NetworkState() :
  452. tap((EthernetTap *)0)
  453. {
  454. // Real defaults are in network 'up' code in network event handler
  455. settings.allowManaged = true;
  456. settings.allowGlobal = false;
  457. settings.allowDefault = false;
  458. settings.allowDNS = false;
  459. memset(&config, 0, sizeof(ZT_VirtualNetworkConfig));
  460. }
  461. std::shared_ptr<EthernetTap> tap;
  462. ZT_VirtualNetworkConfig config; // memcpy() of raw config from core
  463. std::vector<InetAddress> managedIps;
  464. std::map< InetAddress, SharedPtr<ManagedRoute> > managedRoutes;
  465. NetworkSettings settings;
  466. };
  467. std::map<uint64_t,NetworkState> _nets;
  468. Mutex _nets_m;
  469. // Active TCP/IP connections
  470. std::vector< TcpConnection * > _tcpConnections;
  471. Mutex _tcpConnections_m;
  472. TcpConnection *_tcpFallbackTunnel;
  473. // Termination status information
  474. ReasonForTermination _termReason;
  475. std::string _fatalErrorMessage;
  476. Mutex _termReason_m;
  477. // uPnP/NAT-PMP port mapper if enabled
  478. bool _portMappingEnabled; // local.conf settings
  479. #ifdef ZT_USE_MINIUPNPC
  480. PortMapper *_portMapper;
  481. #endif
  482. // HashiCorp Vault Settings
  483. #if ZT_VAULT_SUPPORT
  484. bool _vaultEnabled;
  485. std::string _vaultURL;
  486. std::string _vaultToken;
  487. std::string _vaultPath; // defaults to cubbyhole/zerotier/identity.secret for per-access key storage
  488. #endif
  489. // Set to false to force service to stop
  490. volatile bool _run;
  491. Mutex _run_m;
  492. RedisConfig *_rc;
  493. // end member variables ----------------------------------------------------
  494. OneServiceImpl(const char *hp,unsigned int port) :
  495. _homePath((hp) ? hp : ".")
  496. ,_controllerDbPath(_homePath + ZT_PATH_SEPARATOR_S "controller.d")
  497. ,_networksPath(_homePath + ZT_PATH_SEPARATOR_S "networks.d")
  498. ,_moonsPath(_homePath + ZT_PATH_SEPARATOR_S "moons.d")
  499. ,_controller((EmbeddedNetworkController *)0)
  500. ,_phy(this,false,true)
  501. ,_node((Node *)0)
  502. ,_updater((SoftwareUpdater *)0)
  503. ,_localControlSocket4((PhySocket *)0)
  504. ,_localControlSocket6((PhySocket *)0)
  505. ,_updateAutoApply(false)
  506. ,_primaryPort(port)
  507. ,_udpPortPickerCounter(0)
  508. ,_lastDirectReceiveFromGlobal(0)
  509. #ifdef ZT_TCP_FALLBACK_RELAY
  510. ,_lastSendToGlobalV4(0)
  511. #endif
  512. ,_lastRestart(0)
  513. ,_nextBackgroundTaskDeadline(0)
  514. ,_tcpFallbackTunnel((TcpConnection *)0)
  515. ,_termReason(ONE_STILL_RUNNING)
  516. ,_portMappingEnabled(true)
  517. #ifdef ZT_USE_MINIUPNPC
  518. ,_portMapper((PortMapper *)0)
  519. #endif
  520. #ifdef ZT_VAULT_SUPPORT
  521. ,_vaultEnabled(false)
  522. ,_vaultURL()
  523. ,_vaultToken()
  524. ,_vaultPath("cubbyhole/zerotier")
  525. #endif
  526. ,_run(true)
  527. ,_rc(NULL)
  528. {
  529. _ports[0] = 0;
  530. _ports[1] = 0;
  531. _ports[2] = 0;
  532. #if ZT_VAULT_SUPPORT
  533. curl_global_init(CURL_GLOBAL_DEFAULT);
  534. #endif
  535. }
  536. virtual ~OneServiceImpl()
  537. {
  538. _binder.closeAll(_phy);
  539. _phy.close(_localControlSocket4);
  540. _phy.close(_localControlSocket6);
  541. #if ZT_VAULT_SUPPORT
  542. curl_global_cleanup();
  543. #endif
  544. #ifdef ZT_USE_MINIUPNPC
  545. delete _portMapper;
  546. #endif
  547. delete _controller;
  548. delete _rc;
  549. }
  550. virtual ReasonForTermination run()
  551. {
  552. try {
  553. {
  554. const std::string authTokenPath(_homePath + ZT_PATH_SEPARATOR_S "authtoken.secret");
  555. if (!OSUtils::readFile(authTokenPath.c_str(),_authToken)) {
  556. unsigned char foo[24];
  557. Utils::getSecureRandom(foo,sizeof(foo));
  558. _authToken = "";
  559. for(unsigned int i=0;i<sizeof(foo);++i)
  560. _authToken.push_back("abcdefghijklmnopqrstuvwxyz0123456789"[(unsigned long)foo[i] % 36]);
  561. if (!OSUtils::writeFile(authTokenPath.c_str(),_authToken)) {
  562. Mutex::Lock _l(_termReason_m);
  563. _termReason = ONE_UNRECOVERABLE_ERROR;
  564. _fatalErrorMessage = "authtoken.secret could not be written";
  565. return _termReason;
  566. } else {
  567. OSUtils::lockDownFile(authTokenPath.c_str(),false);
  568. }
  569. }
  570. _authToken = _trimString(_authToken);
  571. }
  572. {
  573. struct ZT_Node_Callbacks cb;
  574. cb.version = 0;
  575. cb.stateGetFunction = SnodeStateGetFunction;
  576. cb.statePutFunction = SnodeStatePutFunction;
  577. cb.wirePacketSendFunction = SnodeWirePacketSendFunction;
  578. cb.virtualNetworkFrameFunction = SnodeVirtualNetworkFrameFunction;
  579. cb.virtualNetworkConfigFunction = SnodeVirtualNetworkConfigFunction;
  580. cb.eventCallback = SnodeEventCallback;
  581. cb.pathCheckFunction = SnodePathCheckFunction;
  582. cb.pathLookupFunction = SnodePathLookupFunction;
  583. _node = new Node(this,(void *)0,&cb,OSUtils::now());
  584. }
  585. // local.conf
  586. readLocalSettings();
  587. applyLocalConfig();
  588. // Make sure we can use the primary port, and hunt for one if configured to do so
  589. const int portTrials = (_primaryPort == 0) ? 256 : 1; // if port is 0, pick random
  590. for(int k=0;k<portTrials;++k) {
  591. if (_primaryPort == 0) {
  592. unsigned int randp = 0;
  593. Utils::getSecureRandom(&randp,sizeof(randp));
  594. _primaryPort = 20000 + (randp % 45500);
  595. }
  596. if (_trialBind(_primaryPort)) {
  597. _ports[0] = _primaryPort;
  598. } else {
  599. _primaryPort = 0;
  600. }
  601. }
  602. if (_ports[0] == 0) {
  603. Mutex::Lock _l(_termReason_m);
  604. _termReason = ONE_UNRECOVERABLE_ERROR;
  605. _fatalErrorMessage = "cannot bind to local control interface port";
  606. return _termReason;
  607. }
  608. // Bind TCP control socket to 127.0.0.1 and ::1 as well for loopback TCP control socket queries
  609. {
  610. struct sockaddr_in lo4;
  611. memset(&lo4,0,sizeof(lo4));
  612. lo4.sin_family = AF_INET;
  613. lo4.sin_addr.s_addr = Utils::hton((uint32_t)0x7f000001);
  614. lo4.sin_port = Utils::hton((uint16_t)_ports[0]);
  615. _localControlSocket4 = _phy.tcpListen((const struct sockaddr *)&lo4);
  616. struct sockaddr_in6 lo6;
  617. memset(&lo6,0,sizeof(lo6));
  618. lo6.sin6_family = AF_INET6;
  619. lo6.sin6_addr.s6_addr[15] = 1;
  620. lo6.sin6_port = lo4.sin_port;
  621. _localControlSocket6 = _phy.tcpListen((const struct sockaddr *)&lo6);
  622. }
  623. // Save primary port to a file so CLIs and GUIs can learn it easily
  624. char portstr[64];
  625. OSUtils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]);
  626. OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S "zerotier-one.port").c_str(),std::string(portstr));
  627. // Attempt to bind to a secondary port chosen from our ZeroTier address.
  628. // This exists because there are buggy NATs out there that fail if more
  629. // than one device behind the same NAT tries to use the same internal
  630. // private address port number. Buggy NATs are a running theme.
  631. if (_allowSecondaryPort) {
  632. if (_secondaryPort) {
  633. _ports[1] = _secondaryPort;
  634. } else {
  635. _ports[1] = 20000 + ((unsigned int)_node->address() % 45500);
  636. for(int i=0;;++i) {
  637. if (i > 1000) {
  638. _ports[1] = 0;
  639. break;
  640. } else if (++_ports[1] >= 65536) {
  641. _ports[1] = 20000;
  642. }
  643. if (_trialBind(_ports[1]))
  644. break;
  645. }
  646. }
  647. }
  648. #ifdef ZT_USE_MINIUPNPC
  649. if (_portMappingEnabled) {
  650. // If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't
  651. // use the other two ports for that because some NATs do really funky
  652. // stuff with ports that are explicitly mapped that breaks things.
  653. if (_ports[1]) {
  654. if (_tertiaryPort) {
  655. _ports[2] = _tertiaryPort;
  656. } else {
  657. _ports[2] = _ports[1];
  658. for(int i=0;;++i) {
  659. if (i > 1000) {
  660. _ports[2] = 0;
  661. break;
  662. } else if (++_ports[2] >= 65536) {
  663. _ports[2] = 20000;
  664. }
  665. if (_trialBind(_ports[2]))
  666. break;
  667. }
  668. if (_ports[2]) {
  669. char uniqueName[64];
  670. OSUtils::ztsnprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]);
  671. _portMapper = new PortMapper(_ports[2],uniqueName);
  672. }
  673. }
  674. }
  675. }
  676. #endif
  677. // Delete legacy iddb.d if present (cleanup)
  678. OSUtils::rmDashRf((_homePath + ZT_PATH_SEPARATOR_S "iddb.d").c_str());
  679. // Network controller is now enabled by default for desktop and server
  680. _controller = new EmbeddedNetworkController(_node,_homePath.c_str(),_controllerDbPath.c_str(),_ports[0], _rc);
  681. _node->setNetconfMaster((void *)_controller);
  682. // Join existing networks in networks.d
  683. {
  684. std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str()));
  685. for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
  686. std::size_t dot = f->find_last_of('.');
  687. if ((dot == 16)&&(f->substr(16) == ".conf"))
  688. _node->join(Utils::hexStrToU64(f->substr(0,dot).c_str()),(void *)0,(void *)0);
  689. }
  690. }
  691. // Orbit existing moons in moons.d
  692. {
  693. std::vector<std::string> moonsDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "moons.d").c_str()));
  694. for(std::vector<std::string>::iterator f(moonsDotD.begin());f!=moonsDotD.end();++f) {
  695. std::size_t dot = f->find_last_of('.');
  696. if ((dot == 16)&&(f->substr(16) == ".moon"))
  697. _node->orbit((void *)0,Utils::hexStrToU64(f->substr(0,dot).c_str()),0);
  698. }
  699. }
  700. // Main I/O loop
  701. _nextBackgroundTaskDeadline = 0;
  702. int64_t clockShouldBe = OSUtils::now();
  703. _lastRestart = clockShouldBe;
  704. int64_t lastTapMulticastGroupCheck = 0;
  705. int64_t lastBindRefresh = 0;
  706. int64_t lastUpdateCheck = clockShouldBe;
  707. int64_t lastCleanedPeersDb = 0;
  708. 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
  709. int64_t lastLocalConfFileCheck = OSUtils::now();
  710. for(;;) {
  711. _run_m.lock();
  712. if (!_run) {
  713. _run_m.unlock();
  714. _termReason_m.lock();
  715. _termReason = ONE_NORMAL_TERMINATION;
  716. _termReason_m.unlock();
  717. break;
  718. } else {
  719. _run_m.unlock();
  720. }
  721. const int64_t now = OSUtils::now();
  722. // Attempt to detect sleep/wake events by detecting delay overruns
  723. bool restarted = false;
  724. if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
  725. _lastRestart = now;
  726. restarted = true;
  727. }
  728. // Check for updates (if enabled)
  729. if ((_updater)&&((now - lastUpdateCheck) > 10000)) {
  730. lastUpdateCheck = now;
  731. if (_updater->check(now) && _updateAutoApply)
  732. _updater->apply();
  733. }
  734. // Reload local.conf if anything changed recently
  735. if ((now - lastLocalConfFileCheck) >= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  736. lastLocalConfFileCheck = now;
  737. struct stat result;
  738. if(stat((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(), &result)==0) {
  739. int64_t mod_time = result.st_mtime * 1000;
  740. if ((now - mod_time) <= ZT_LOCAL_CONF_FILE_CHECK_INTERVAL) {
  741. readLocalSettings();
  742. applyLocalConfig();
  743. }
  744. }
  745. }
  746. // Refresh bindings in case device's interfaces have changed, and also sync routes to update any shadow routes (e.g. shadow default)
  747. if (((now - lastBindRefresh) >= (_node->bondController()->inUse() ? ZT_BINDER_REFRESH_PERIOD / 4 : ZT_BINDER_REFRESH_PERIOD))||(restarted)) {
  748. lastBindRefresh = now;
  749. unsigned int p[3];
  750. unsigned int pc = 0;
  751. for(int i=0;i<3;++i) {
  752. if (_ports[i])
  753. p[pc++] = _ports[i];
  754. }
  755. _binder.refresh(_phy,p,pc,explicitBind,*this);
  756. {
  757. Mutex::Lock _l(_nets_m);
  758. for(std::map<uint64_t,NetworkState>::iterator n(_nets.begin());n!=_nets.end();++n) {
  759. if (n->second.tap)
  760. syncManagedStuff(n->second,false,true,false);
  761. }
  762. }
  763. }
  764. // Run background task processor in core if it's time to do so
  765. int64_t dl = _nextBackgroundTaskDeadline;
  766. if (dl <= now) {
  767. _node->processBackgroundTasks((void *)0,now,&_nextBackgroundTaskDeadline);
  768. dl = _nextBackgroundTaskDeadline;
  769. }
  770. // Close TCP fallback tunnel if we have direct UDP
  771. if ((_tcpFallbackTunnel)&&((now - _lastDirectReceiveFromGlobal) < (ZT_TCP_FALLBACK_AFTER / 2)))
  772. _phy.close(_tcpFallbackTunnel->sock);
  773. // Sync multicast group memberships
  774. if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) {
  775. lastTapMulticastGroupCheck = now;
  776. std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > > mgChanges;
  777. {
  778. Mutex::Lock _l(_nets_m);
  779. mgChanges.reserve(_nets.size() + 1);
  780. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  781. if (n->second.tap) {
  782. 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> >()));
  783. n->second.tap->scanMulticastGroups(mgChanges.back().second.first,mgChanges.back().second.second);
  784. }
  785. }
  786. }
  787. for(std::vector< std::pair< uint64_t,std::pair< std::vector<MulticastGroup>,std::vector<MulticastGroup> > > >::iterator c(mgChanges.begin());c!=mgChanges.end();++c) {
  788. for(std::vector<MulticastGroup>::iterator m(c->second.first.begin());m!=c->second.first.end();++m)
  789. _node->multicastSubscribe((void *)0,c->first,m->mac().toInt(),m->adi());
  790. for(std::vector<MulticastGroup>::iterator m(c->second.second.begin());m!=c->second.second.end();++m)
  791. _node->multicastUnsubscribe(c->first,m->mac().toInt(),m->adi());
  792. }
  793. }
  794. // Sync information about physical network interfaces
  795. if ((now - lastLocalInterfaceAddressCheck) >= (_node->bondController()->inUse() ? ZT_LOCAL_INTERFACE_CHECK_INTERVAL / 8 : ZT_LOCAL_INTERFACE_CHECK_INTERVAL)) {
  796. lastLocalInterfaceAddressCheck = now;
  797. _node->clearLocalInterfaceAddresses();
  798. #ifdef ZT_USE_MINIUPNPC
  799. if (_portMapper) {
  800. std::vector<InetAddress> mappedAddresses(_portMapper->get());
  801. for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
  802. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
  803. }
  804. #endif
  805. std::vector<InetAddress> boundAddrs(_binder.allBoundLocalInterfaceAddresses());
  806. for(std::vector<InetAddress>::const_iterator i(boundAddrs.begin());i!=boundAddrs.end();++i) {
  807. _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*i)));
  808. }
  809. }
  810. // Clean peers.d periodically
  811. if ((now - lastCleanedPeersDb) >= 3600000) {
  812. lastCleanedPeersDb = now;
  813. OSUtils::cleanDirectory((_homePath + ZT_PATH_SEPARATOR_S "peers.d").c_str(),now - 2592000000LL); // delete older than 30 days
  814. }
  815. const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 500;
  816. clockShouldBe = now + (int64_t)delay;
  817. _phy.poll(delay);
  818. }
  819. } catch (std::exception &e) {
  820. Mutex::Lock _l(_termReason_m);
  821. _termReason = ONE_UNRECOVERABLE_ERROR;
  822. _fatalErrorMessage = std::string("unexpected exception in main thread: ")+e.what();
  823. } catch ( ... ) {
  824. Mutex::Lock _l(_termReason_m);
  825. _termReason = ONE_UNRECOVERABLE_ERROR;
  826. _fatalErrorMessage = "unexpected exception in main thread: unknown exception";
  827. }
  828. try {
  829. Mutex::Lock _l(_tcpConnections_m);
  830. while (!_tcpConnections.empty())
  831. _phy.close((*_tcpConnections.begin())->sock);
  832. } catch ( ... ) {}
  833. {
  834. Mutex::Lock _l(_nets_m);
  835. _nets.clear();
  836. }
  837. delete _updater;
  838. _updater = (SoftwareUpdater *)0;
  839. delete _node;
  840. _node = (Node *)0;
  841. return _termReason;
  842. }
  843. void readLocalSettings()
  844. {
  845. // Read local configuration
  846. std::map<InetAddress,ZT_PhysicalPathConfiguration> ppc;
  847. // LEGACY: support old "trustedpaths" flat file
  848. FILE *trustpaths = fopen((_homePath + ZT_PATH_SEPARATOR_S "trustedpaths").c_str(),"r");
  849. if (trustpaths) {
  850. fprintf(stderr,"WARNING: 'trustedpaths' flat file format is deprecated in favor of path definitions in local.conf" ZT_EOL_S);
  851. char buf[1024];
  852. while (fgets(buf,sizeof(buf),trustpaths)) {
  853. int fno = 0;
  854. char *saveptr = (char *)0;
  855. uint64_t trustedPathId = 0;
  856. InetAddress trustedPathNetwork;
  857. for(char *f=Utils::stok(buf,"=\r\n \t",&saveptr);(f);f=Utils::stok((char *)0,"=\r\n \t",&saveptr)) {
  858. if (fno == 0) {
  859. trustedPathId = Utils::hexStrToU64(f);
  860. } else if (fno == 1) {
  861. trustedPathNetwork = InetAddress(f);
  862. } else break;
  863. ++fno;
  864. }
  865. if ( (trustedPathId != 0) && ((trustedPathNetwork.ss_family == AF_INET)||(trustedPathNetwork.ss_family == AF_INET6)) && (trustedPathNetwork.netmaskBits() > 0) ) {
  866. ppc[trustedPathNetwork].trustedPathId = trustedPathId;
  867. ppc[trustedPathNetwork].mtu = 0; // use default
  868. }
  869. }
  870. fclose(trustpaths);
  871. }
  872. // Read local config file
  873. Mutex::Lock _l2(_localConfig_m);
  874. std::string lcbuf;
  875. if (OSUtils::readFile((_homePath + ZT_PATH_SEPARATOR_S "local.conf").c_str(),lcbuf)) {
  876. if (lcbuf.length() > 0) {
  877. try {
  878. _localConfig = OSUtils::jsonParse(lcbuf);
  879. if (!_localConfig.is_object()) {
  880. fprintf(stderr,"ERROR: unable to parse local.conf (root element is not a JSON object)" ZT_EOL_S);
  881. exit(1);
  882. }
  883. } catch ( ... ) {
  884. fprintf(stderr,"ERROR: unable to parse local.conf (invalid JSON)" ZT_EOL_S);
  885. exit(1);
  886. }
  887. }
  888. }
  889. // Get any trusted paths in local.conf (we'll parse the rest of physical[] elsewhere)
  890. json &physical = _localConfig["physical"];
  891. if (physical.is_object()) {
  892. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  893. InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  894. if (net) {
  895. if (phy.value().is_object()) {
  896. uint64_t tpid;
  897. if ((tpid = OSUtils::jsonInt(phy.value()["trustedPathId"],0ULL)) != 0ULL) {
  898. if ((net.ss_family == AF_INET)||(net.ss_family == AF_INET6))
  899. ppc[net].trustedPathId = tpid;
  900. }
  901. ppc[net].mtu = (int)OSUtils::jsonInt(phy.value()["mtu"],0ULL); // 0 means use default
  902. }
  903. }
  904. }
  905. }
  906. json &settings = _localConfig["settings"];
  907. if (settings.is_object()) {
  908. // Allow controller DB path to be put somewhere else
  909. const std::string cdbp(OSUtils::jsonString(settings["controllerDbPath"],""));
  910. if (cdbp.length() > 0)
  911. _controllerDbPath = cdbp;
  912. #ifdef ZT_CONTROLLER_USE_LIBPQ
  913. // TODO: Redis config
  914. json &redis = settings["redis"];
  915. if (redis.is_object() && _rc == NULL) {
  916. _rc = new RedisConfig;
  917. _rc->hostname = OSUtils::jsonString(redis["hostname"],"");
  918. _rc->port = redis["port"];
  919. _rc->password = OSUtils::jsonString(redis["password"],"");
  920. _rc->clusterMode = OSUtils::jsonBool(redis["clusterMode"], false);
  921. }
  922. #endif
  923. // Bind to wildcard instead of to specific interfaces (disables full tunnel capability)
  924. json &bind = settings["bind"];
  925. if (bind.is_array()) {
  926. for(unsigned long i=0;i<bind.size();++i) {
  927. const std::string ips(OSUtils::jsonString(bind[i],""));
  928. if (ips.length() > 0) {
  929. InetAddress ip(ips.c_str());
  930. if ((ip.ss_family == AF_INET)||(ip.ss_family == AF_INET6))
  931. explicitBind.push_back(ip);
  932. }
  933. }
  934. }
  935. }
  936. // Set trusted paths if there are any
  937. if (ppc.size() > 0) {
  938. for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::iterator i(ppc.begin());i!=ppc.end();++i)
  939. _node->setPhysicalPathConfiguration(reinterpret_cast<const struct sockaddr_storage *>(&(i->first)),&(i->second));
  940. }
  941. }
  942. virtual ReasonForTermination reasonForTermination() const
  943. {
  944. Mutex::Lock _l(_termReason_m);
  945. return _termReason;
  946. }
  947. virtual std::string fatalErrorMessage() const
  948. {
  949. Mutex::Lock _l(_termReason_m);
  950. return _fatalErrorMessage;
  951. }
  952. virtual std::string portDeviceName(uint64_t nwid) const
  953. {
  954. Mutex::Lock _l(_nets_m);
  955. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  956. if ((n != _nets.end())&&(n->second.tap))
  957. return n->second.tap->deviceName();
  958. else return std::string();
  959. }
  960. #ifdef ZT_SDK
  961. virtual std::string givenHomePath()
  962. {
  963. return _homePath;
  964. }
  965. void getRoutes(uint64_t nwid, void *routeArray, unsigned int *numRoutes)
  966. {
  967. Mutex::Lock _l(_nets_m);
  968. NetworkState &n = _nets[nwid];
  969. *numRoutes = *numRoutes < n.config.routeCount ? *numRoutes : n.config.routeCount;
  970. for(unsigned int i=0; i<*numRoutes; i++) {
  971. ZT_VirtualNetworkRoute *vnr = (ZT_VirtualNetworkRoute*)routeArray;
  972. memcpy(&vnr[i], &(n.config.routes[i]), sizeof(ZT_VirtualNetworkRoute));
  973. }
  974. }
  975. virtual Node *getNode()
  976. {
  977. return _node;
  978. }
  979. #endif // ZT_SDK
  980. virtual void terminate()
  981. {
  982. _run_m.lock();
  983. _run = false;
  984. _run_m.unlock();
  985. _phy.whack();
  986. }
  987. virtual bool getNetworkSettings(const uint64_t nwid,NetworkSettings &settings) const
  988. {
  989. Mutex::Lock _l(_nets_m);
  990. std::map<uint64_t,NetworkState>::const_iterator n(_nets.find(nwid));
  991. if (n == _nets.end())
  992. return false;
  993. settings = n->second.settings;
  994. return true;
  995. }
  996. virtual bool setNetworkSettings(const uint64_t nwid,const NetworkSettings &settings)
  997. {
  998. Mutex::Lock _l(_nets_m);
  999. std::map<uint64_t,NetworkState>::iterator n(_nets.find(nwid));
  1000. if (n == _nets.end())
  1001. return false;
  1002. n->second.settings = settings;
  1003. char nlcpath[4096];
  1004. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_networksPath.c_str(),nwid);
  1005. FILE *out = fopen(nlcpath,"w");
  1006. if (out) {
  1007. fprintf(out,"allowManaged=%d\n",(int)n->second.settings.allowManaged);
  1008. fprintf(out,"allowGlobal=%d\n",(int)n->second.settings.allowGlobal);
  1009. fprintf(out,"allowDefault=%d\n",(int)n->second.settings.allowDefault);
  1010. fprintf(out,"allowDNS=%d\n",(int)n->second.settings.allowDNS);
  1011. fclose(out);
  1012. }
  1013. if (n->second.tap)
  1014. syncManagedStuff(n->second,true,true,true);
  1015. return true;
  1016. }
  1017. // =========================================================================
  1018. // Internal implementation methods for control plane, route setup, etc.
  1019. // =========================================================================
  1020. inline unsigned int handleControlPlaneHttpRequest(
  1021. const InetAddress &fromAddress,
  1022. unsigned int httpMethod,
  1023. const std::string &path,
  1024. const std::map<std::string,std::string> &headers,
  1025. const std::string &body,
  1026. std::string &responseBody,
  1027. std::string &responseContentType)
  1028. {
  1029. char tmp[256];
  1030. unsigned int scode = 404;
  1031. json res;
  1032. std::vector<std::string> ps(OSUtils::split(path.c_str(),"/","",""));
  1033. std::map<std::string,std::string> urlArgs;
  1034. /* Note: this is kind of restricted in what it'll take. It does not support
  1035. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  1036. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  1037. * paths to simply-named resources. */
  1038. if (ps.size() > 0) {
  1039. std::size_t qpos = ps[ps.size() - 1].find('?');
  1040. if (qpos != std::string::npos) {
  1041. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  1042. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  1043. std::vector<std::string> asplit(OSUtils::split(args.c_str(),"&","",""));
  1044. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  1045. std::size_t eqpos = a->find('=');
  1046. if (eqpos == std::string::npos)
  1047. urlArgs[*a] = "";
  1048. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  1049. }
  1050. }
  1051. } else {
  1052. return 404;
  1053. }
  1054. bool isAuth = false;
  1055. {
  1056. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  1057. if ((ah != headers.end())&&(_authToken == ah->second)) {
  1058. isAuth = true;
  1059. } else {
  1060. ah = urlArgs.find("auth");
  1061. if ((ah != urlArgs.end())&&(_authToken == ah->second))
  1062. isAuth = true;
  1063. }
  1064. }
  1065. #ifdef __SYNOLOGY__
  1066. // Authenticate via Synology's built-in cgi script
  1067. if (!isAuth) {
  1068. int synotoken_pos = path.find("SynoToken");
  1069. int argpos = path.find("?");
  1070. if(synotoken_pos != std::string::npos && argpos != std::string::npos) {
  1071. std::string cookie = path.substr(argpos+1, synotoken_pos-(argpos+1));
  1072. std::string synotoken = path.substr(synotoken_pos);
  1073. std::string cookie_val = cookie.substr(cookie.find("=")+1);
  1074. std::string synotoken_val = synotoken.substr(synotoken.find("=")+1);
  1075. // Set necessary env for auth script
  1076. std::map<std::string,std::string>::const_iterator ah2(headers.find("x-forwarded-for"));
  1077. setenv("HTTP_COOKIE", cookie_val.c_str(), true);
  1078. setenv("HTTP_X_SYNO_TOKEN", synotoken_val.c_str(), true);
  1079. setenv("REMOTE_ADDR", ah2->second.c_str(),true);
  1080. char user[256], buf[1024];
  1081. FILE *fp = NULL;
  1082. bzero(user, 256);
  1083. fp = popen("/usr/syno/synoman/webman/modules/authenticate.cgi", "r");
  1084. if(!fp)
  1085. isAuth = false;
  1086. else {
  1087. bzero(buf, sizeof(buf));
  1088. fread(buf, 1024, 1, fp);
  1089. if(strlen(buf) > 0) {
  1090. snprintf(user, 256, "%s", buf);
  1091. isAuth = true;
  1092. }
  1093. }
  1094. pclose(fp);
  1095. }
  1096. }
  1097. #endif
  1098. if (httpMethod == HTTP_GET) {
  1099. if (isAuth) {
  1100. if (ps[0] == "status") {
  1101. ZT_NodeStatus status;
  1102. _node->status(&status);
  1103. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",status.address);
  1104. res["address"] = tmp;
  1105. res["publicIdentity"] = status.publicIdentity;
  1106. res["online"] = (bool)(status.online != 0);
  1107. res["tcpFallbackActive"] = (_tcpFallbackTunnel != (TcpConnection *)0);
  1108. res["versionMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  1109. res["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  1110. res["versionRev"] = ZEROTIER_ONE_VERSION_REVISION;
  1111. res["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  1112. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  1113. res["version"] = tmp;
  1114. res["clock"] = OSUtils::now();
  1115. {
  1116. Mutex::Lock _l(_localConfig_m);
  1117. res["config"] = _localConfig;
  1118. }
  1119. json &settings = res["config"]["settings"];
  1120. settings["primaryPort"] = OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1121. settings["allowTcpFallbackRelay"] = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],_allowTcpFallbackRelay);
  1122. if (_node->bondController()->inUse()) {
  1123. json &multipathConfig = res["bonds"];
  1124. ZT_PeerList *pl = _node->peers();
  1125. char peerAddrStr[256];
  1126. if (pl) {
  1127. for(unsigned long i=0;i<pl->peerCount;++i) {
  1128. if (pl->peers[i].isBonded) {
  1129. nlohmann::json pj;
  1130. _peerBondToJson(pj,&(pl->peers[i]));
  1131. OSUtils::ztsnprintf(peerAddrStr,sizeof(peerAddrStr),"%.10llx",pl->peers[i].address);
  1132. multipathConfig[peerAddrStr] = (pj);
  1133. }
  1134. }
  1135. }
  1136. }
  1137. #ifdef ZT_USE_MINIUPNPC
  1138. settings["portMappingEnabled"] = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1139. #else
  1140. settings["portMappingEnabled"] = false; // not supported in build
  1141. #endif
  1142. #ifndef ZT_SDK
  1143. settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT);
  1144. settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL);
  1145. #endif
  1146. const World planet(_node->planet());
  1147. res["planetWorldId"] = planet.id();
  1148. res["planetWorldTimestamp"] = planet.timestamp();
  1149. scode = 200;
  1150. } else if (ps[0] == "moon") {
  1151. std::vector<World> moons(_node->moons());
  1152. if (ps.size() == 1) {
  1153. // Return [array] of all moons
  1154. res = json::array();
  1155. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1156. json mj;
  1157. _moonToJson(mj,*m);
  1158. res.push_back(mj);
  1159. }
  1160. scode = 200;
  1161. } else {
  1162. // Return a single moon by ID
  1163. const uint64_t id = Utils::hexStrToU64(ps[1].c_str());
  1164. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1165. if (m->id() == id) {
  1166. _moonToJson(res,*m);
  1167. scode = 200;
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. } else if (ps[0] == "network") {
  1173. ZT_VirtualNetworkList *nws = _node->networks();
  1174. if (nws) {
  1175. if (ps.size() == 1) {
  1176. // Return [array] of all networks
  1177. res = nlohmann::json::array();
  1178. for(unsigned long i=0;i<nws->networkCount;++i) {
  1179. OneService::NetworkSettings localSettings;
  1180. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1181. nlohmann::json nj;
  1182. _networkToJson(nj,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1183. res.push_back(nj);
  1184. }
  1185. scode = 200;
  1186. } else if (ps.size() == 2) {
  1187. // Return a single network by ID or 404 if not found
  1188. const uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1189. for(unsigned long i=0;i<nws->networkCount;++i) {
  1190. if (nws->networks[i].nwid == wantnw) {
  1191. OneService::NetworkSettings localSettings;
  1192. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1193. _networkToJson(res,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1194. scode = 200;
  1195. break;
  1196. }
  1197. }
  1198. } else scode = 404;
  1199. _node->freeQueryResult((void *)nws);
  1200. } else scode = 500;
  1201. } else if (ps[0] == "peer") {
  1202. ZT_PeerList *pl = _node->peers();
  1203. if (pl) {
  1204. if (ps.size() == 1) {
  1205. // Return [array] of all peers
  1206. res = nlohmann::json::array();
  1207. for(unsigned long i=0;i<pl->peerCount;++i) {
  1208. nlohmann::json pj;
  1209. _peerToJson(pj,&(pl->peers[i]));
  1210. res.push_back(pj);
  1211. }
  1212. scode = 200;
  1213. } else if (ps.size() == 2) {
  1214. // Return a single peer by ID or 404 if not found
  1215. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  1216. for(unsigned long i=0;i<pl->peerCount;++i) {
  1217. if (pl->peers[i].address == wantp) {
  1218. _peerToJson(res,&(pl->peers[i]));
  1219. scode = 200;
  1220. break;
  1221. }
  1222. }
  1223. } else scode = 404;
  1224. _node->freeQueryResult((void *)pl);
  1225. } else scode = 500;
  1226. } else if (ps[0] == "bonds") {
  1227. ZT_PeerList *pl = _node->peers();
  1228. if (pl) {
  1229. if (ps.size() == 1) {
  1230. // Return [array] of all peers
  1231. res = nlohmann::json::array();
  1232. for(unsigned long i=0;i<pl->peerCount;++i) {
  1233. nlohmann::json pj;
  1234. _peerToJson(pj,&(pl->peers[i]));
  1235. res.push_back(pj);
  1236. }
  1237. scode = 200;
  1238. } else if (ps.size() == 2) {
  1239. // Return a single peer by ID or 404 if not found
  1240. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  1241. for(unsigned long i=0;i<pl->peerCount;++i) {
  1242. if (pl->peers[i].address == wantp) {
  1243. _peerToJson(res,&(pl->peers[i]));
  1244. scode = 200;
  1245. break;
  1246. }
  1247. }
  1248. } else scode = 404;
  1249. _node->freeQueryResult((void *)pl);
  1250. } else scode = 500;
  1251. } else {
  1252. if (_controller) {
  1253. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1254. } else scode = 404;
  1255. }
  1256. } else scode = 401; // isAuth == false
  1257. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  1258. if (isAuth) {
  1259. if (ps[0] == "moon") {
  1260. if (ps.size() == 2) {
  1261. uint64_t seed = 0;
  1262. try {
  1263. json j(OSUtils::jsonParse(body));
  1264. if (j.is_object()) {
  1265. seed = Utils::hexStrToU64(OSUtils::jsonString(j["seed"],"0").c_str());
  1266. }
  1267. } catch ( ... ) {
  1268. // discard invalid JSON
  1269. }
  1270. std::vector<World> moons(_node->moons());
  1271. const uint64_t id = Utils::hexStrToU64(ps[1].c_str());
  1272. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  1273. if (m->id() == id) {
  1274. _moonToJson(res,*m);
  1275. scode = 200;
  1276. break;
  1277. }
  1278. }
  1279. if ((scode != 200)&&(seed != 0)) {
  1280. char tmp[64];
  1281. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",id);
  1282. res["id"] = tmp;
  1283. res["roots"] = json::array();
  1284. res["timestamp"] = 0;
  1285. res["signature"] = json();
  1286. res["updatesMustBeSignedBy"] = json();
  1287. res["waiting"] = true;
  1288. _node->orbit((void *)0,id,seed);
  1289. scode = 200;
  1290. }
  1291. } else scode = 404;
  1292. } else if (ps[0] == "network") {
  1293. if (ps.size() == 2) {
  1294. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1295. _node->join(wantnw,(void *)0,(void *)0); // does nothing if we are a member
  1296. ZT_VirtualNetworkList *nws = _node->networks();
  1297. if (nws) {
  1298. for(unsigned long i=0;i<nws->networkCount;++i) {
  1299. if (nws->networks[i].nwid == wantnw) {
  1300. OneService::NetworkSettings localSettings;
  1301. getNetworkSettings(nws->networks[i].nwid,localSettings);
  1302. try {
  1303. json j(OSUtils::jsonParse(body));
  1304. if (j.is_object()) {
  1305. json &allowManaged = j["allowManaged"];
  1306. if (allowManaged.is_boolean()) localSettings.allowManaged = (bool)allowManaged;
  1307. json &allowGlobal = j["allowGlobal"];
  1308. if (allowGlobal.is_boolean()) localSettings.allowGlobal = (bool)allowGlobal;
  1309. json &allowDefault = j["allowDefault"];
  1310. if (allowDefault.is_boolean()) localSettings.allowDefault = (bool)allowDefault;
  1311. json &allowDNS = j["allowDNS"];
  1312. if (allowDNS.is_boolean()) localSettings.allowDNS = (bool)allowDNS;
  1313. }
  1314. } catch ( ... ) {
  1315. // discard invalid JSON
  1316. }
  1317. setNetworkSettings(nws->networks[i].nwid,localSettings);
  1318. _networkToJson(res,&(nws->networks[i]),portDeviceName(nws->networks[i].nwid),localSettings);
  1319. scode = 200;
  1320. break;
  1321. }
  1322. }
  1323. _node->freeQueryResult((void *)nws);
  1324. } else scode = 500;
  1325. } else scode = 404;
  1326. } else {
  1327. if (_controller)
  1328. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1329. else scode = 404;
  1330. }
  1331. } else scode = 401; // isAuth == false
  1332. } else if (httpMethod == HTTP_DELETE) {
  1333. if (isAuth) {
  1334. if (ps[0] == "moon") {
  1335. if (ps.size() == 2) {
  1336. _node->deorbit((void *)0,Utils::hexStrToU64(ps[1].c_str()));
  1337. res["result"] = true;
  1338. scode = 200;
  1339. } // else 404
  1340. } else if (ps[0] == "network") {
  1341. ZT_VirtualNetworkList *nws = _node->networks();
  1342. if (nws) {
  1343. if (ps.size() == 2) {
  1344. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  1345. for(unsigned long i=0;i<nws->networkCount;++i) {
  1346. if (nws->networks[i].nwid == wantnw) {
  1347. _node->leave(wantnw,(void **)0,(void *)0);
  1348. res["result"] = true;
  1349. scode = 200;
  1350. break;
  1351. }
  1352. }
  1353. } // else 404
  1354. _node->freeQueryResult((void *)nws);
  1355. } else scode = 500;
  1356. } else {
  1357. if (_controller)
  1358. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  1359. else scode = 404;
  1360. }
  1361. } else scode = 401; // isAuth = false
  1362. } else {
  1363. scode = 400;
  1364. }
  1365. if (responseBody.length() == 0) {
  1366. if ((res.is_object())||(res.is_array()))
  1367. responseBody = OSUtils::jsonDump(res);
  1368. else responseBody = "{}";
  1369. responseContentType = "application/json";
  1370. }
  1371. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  1372. // Also double-check isAuth since forbidding this without auth feels safer.
  1373. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  1374. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  1375. if (responseBody.length() > 0)
  1376. responseBody = jsonp->second + "(" + responseBody + ");";
  1377. else responseBody = jsonp->second + "(null);";
  1378. responseContentType = "application/javascript";
  1379. }
  1380. return scode;
  1381. }
  1382. // Must be called after _localConfig is read or modified
  1383. void applyLocalConfig()
  1384. {
  1385. Mutex::Lock _l(_localConfig_m);
  1386. json lc(_localConfig);
  1387. _v4Hints.clear();
  1388. _v6Hints.clear();
  1389. _v4Blacklists.clear();
  1390. _v6Blacklists.clear();
  1391. json &virt = lc["virtual"];
  1392. if (virt.is_object()) {
  1393. for(json::iterator v(virt.begin());v!=virt.end();++v) {
  1394. const std::string nstr = v.key();
  1395. if ((nstr.length() == ZT_ADDRESS_LENGTH_HEX)&&(v.value().is_object())) {
  1396. const Address ztaddr(Utils::hexStrToU64(nstr.c_str()));
  1397. if (ztaddr) {
  1398. const uint64_t ztaddr2 = ztaddr.toInt();
  1399. std::vector<InetAddress> &v4h = _v4Hints[ztaddr2];
  1400. std::vector<InetAddress> &v6h = _v6Hints[ztaddr2];
  1401. std::vector<InetAddress> &v4b = _v4Blacklists[ztaddr2];
  1402. std::vector<InetAddress> &v6b = _v6Blacklists[ztaddr2];
  1403. json &tryAddrs = v.value()["try"];
  1404. if (tryAddrs.is_array()) {
  1405. for(unsigned long i=0;i<tryAddrs.size();++i) {
  1406. const InetAddress ip(OSUtils::jsonString(tryAddrs[i],"").c_str());
  1407. if (ip.ss_family == AF_INET)
  1408. v4h.push_back(ip);
  1409. else if (ip.ss_family == AF_INET6)
  1410. v6h.push_back(ip);
  1411. }
  1412. }
  1413. json &blAddrs = v.value()["blacklist"];
  1414. if (blAddrs.is_array()) {
  1415. for(unsigned long i=0;i<blAddrs.size();++i) {
  1416. const InetAddress ip(OSUtils::jsonString(blAddrs[i],"").c_str());
  1417. if (ip.ss_family == AF_INET)
  1418. v4b.push_back(ip);
  1419. else if (ip.ss_family == AF_INET6)
  1420. v6b.push_back(ip);
  1421. }
  1422. }
  1423. if (v4h.empty()) _v4Hints.erase(ztaddr2);
  1424. if (v6h.empty()) _v6Hints.erase(ztaddr2);
  1425. if (v4b.empty()) _v4Blacklists.erase(ztaddr2);
  1426. if (v6b.empty()) _v6Blacklists.erase(ztaddr2);
  1427. }
  1428. }
  1429. }
  1430. }
  1431. _globalV4Blacklist.clear();
  1432. _globalV6Blacklist.clear();
  1433. json &physical = lc["physical"];
  1434. if (physical.is_object()) {
  1435. for(json::iterator phy(physical.begin());phy!=physical.end();++phy) {
  1436. const InetAddress net(OSUtils::jsonString(phy.key(),"").c_str());
  1437. if ((net)&&(net.netmaskBits() > 0)) {
  1438. if (phy.value().is_object()) {
  1439. if (OSUtils::jsonBool(phy.value()["blacklist"],false)) {
  1440. if (net.ss_family == AF_INET)
  1441. _globalV4Blacklist.push_back(net);
  1442. else if (net.ss_family == AF_INET6)
  1443. _globalV6Blacklist.push_back(net);
  1444. }
  1445. }
  1446. }
  1447. }
  1448. }
  1449. _allowManagementFrom.clear();
  1450. _interfacePrefixBlacklist.clear();
  1451. json &settings = lc["settings"];
  1452. if (!_node->bondController()->inUse()) {
  1453. // defaultBondingPolicy
  1454. std::string defaultBondingPolicyStr(OSUtils::jsonString(settings["defaultBondingPolicy"],""));
  1455. int defaultBondingPolicy = _node->bondController()->getPolicyCodeByStr(defaultBondingPolicyStr);
  1456. _node->bondController()->setBondingLayerDefaultPolicy(defaultBondingPolicy);
  1457. _node->bondController()->setBondingLayerDefaultPolicyStr(defaultBondingPolicyStr); // Used if custom policy
  1458. // Custom Policies
  1459. json &customBondingPolicies = settings["policies"];
  1460. for (json::iterator policyItr = customBondingPolicies.begin(); policyItr != customBondingPolicies.end();++policyItr) {
  1461. // Custom Policy
  1462. std::string customPolicyStr(policyItr.key());
  1463. json &customPolicy = policyItr.value();
  1464. std::string basePolicyStr(OSUtils::jsonString(customPolicy["basePolicy"],""));
  1465. if (_node->bondController()->getPolicyCodeByStr(basePolicyStr) == ZT_BONDING_POLICY_NONE) {
  1466. fprintf(stderr, "error: custom policy (%s) is invalid, unknown base policy (%s).\n",
  1467. customPolicyStr.c_str(), basePolicyStr.c_str());
  1468. continue;
  1469. } if (_node->bondController()->getPolicyCodeByStr(customPolicyStr) != ZT_BONDING_POLICY_NONE) {
  1470. fprintf(stderr, "error: custom policy (%s) will be ignored, cannot use standard policy names for custom policies.\n",
  1471. customPolicyStr.c_str());
  1472. continue;
  1473. }
  1474. // New bond, used as a copy template for new instances
  1475. SharedPtr<Bond> newTemplateBond = new Bond(NULL, basePolicyStr, customPolicyStr, SharedPtr<Peer>());
  1476. // Acceptable ranges
  1477. newTemplateBond->setMaxAcceptableLatency(OSUtils::jsonInt(customPolicy["maxAcceptableLatency"],-1));
  1478. newTemplateBond->setMaxAcceptableMeanLatency(OSUtils::jsonInt(customPolicy["maxAcceptableMeanLatency"],-1));
  1479. newTemplateBond->setMaxAcceptablePacketDelayVariance(OSUtils::jsonInt(customPolicy["maxAcceptablePacketDelayVariance"],-1));
  1480. newTemplateBond->setMaxAcceptablePacketLossRatio((float)OSUtils::jsonDouble(customPolicy["maxAcceptablePacketLossRatio"],-1));
  1481. newTemplateBond->setMaxAcceptablePacketErrorRatio((float)OSUtils::jsonDouble(customPolicy["maxAcceptablePacketErrorRatio"],-1));
  1482. newTemplateBond->setMinAcceptableAllocation((float)OSUtils::jsonDouble(customPolicy["minAcceptableAllocation"],0));
  1483. // Quality weights
  1484. json &qualityWeights = customPolicy["qualityWeights"];
  1485. if (qualityWeights.size() == ZT_QOS_WEIGHT_SIZE) { // TODO: Generalize this
  1486. float weights[ZT_QOS_WEIGHT_SIZE];
  1487. weights[ZT_QOS_LAT_IDX] = (float)OSUtils::jsonDouble(qualityWeights["lat"],0.0);
  1488. weights[ZT_QOS_LTM_IDX] = (float)OSUtils::jsonDouble(qualityWeights["ltm"],0.0);
  1489. weights[ZT_QOS_PDV_IDX] = (float)OSUtils::jsonDouble(qualityWeights["pdv"],0.0);
  1490. weights[ZT_QOS_PLR_IDX] = (float)OSUtils::jsonDouble(qualityWeights["plr"],0.0);
  1491. weights[ZT_QOS_PER_IDX] = (float)OSUtils::jsonDouble(qualityWeights["per"],0.0);
  1492. weights[ZT_QOS_THR_IDX] = (float)OSUtils::jsonDouble(qualityWeights["thr"],0.0);
  1493. weights[ZT_QOS_THM_IDX] = (float)OSUtils::jsonDouble(qualityWeights["thm"],0.0);
  1494. weights[ZT_QOS_THV_IDX] = (float)OSUtils::jsonDouble(qualityWeights["thv"],0.0);
  1495. newTemplateBond->setUserQualityWeights(weights,ZT_QOS_WEIGHT_SIZE);
  1496. }
  1497. // Bond-specific properties
  1498. newTemplateBond->setUpDelay(OSUtils::jsonInt(customPolicy["upDelay"],-1));
  1499. newTemplateBond->setDownDelay(OSUtils::jsonInt(customPolicy["downDelay"],-1));
  1500. newTemplateBond->setFlowRebalanceStrategy(OSUtils::jsonInt(customPolicy["flowRebalanceStrategy"],(uint64_t)0));
  1501. newTemplateBond->setFailoverInterval(OSUtils::jsonInt(customPolicy["failoverInterval"],(uint64_t)0));
  1502. newTemplateBond->setPacketsPerLink(OSUtils::jsonInt(customPolicy["packetsPerLink"],-1));
  1503. std::string linkMonitorStrategyStr(OSUtils::jsonString(customPolicy["linkMonitorStrategy"],""));
  1504. uint8_t linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_DEFAULT;
  1505. if (linkMonitorStrategyStr == "passive") { linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_PASSIVE; }
  1506. if (linkMonitorStrategyStr == "active") { linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_ACTIVE; }
  1507. if (linkMonitorStrategyStr == "dynamic") { linkMonitorStrategy = ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_DYNAMIC; }
  1508. newTemplateBond->setLinkMonitorStrategy(linkMonitorStrategy);
  1509. // Policy-Specific link set
  1510. json &links = customPolicy["links"];
  1511. for (json::iterator linkItr = links.begin(); linkItr != links.end();++linkItr) {
  1512. std::string linkNameStr(linkItr.key());
  1513. json &link = linkItr.value();
  1514. bool enabled = OSUtils::jsonInt(link["enabled"],true);
  1515. uint32_t speed = OSUtils::jsonInt(link["speed"],0);
  1516. float alloc = (float)OSUtils::jsonDouble(link["alloc"],0);
  1517. if (speed && alloc) {
  1518. fprintf(stderr, "error: cannot specify both speed (%d) and alloc (%f) for link (%s), pick one, link disabled.\n",
  1519. speed, alloc, linkNameStr.c_str());
  1520. enabled = false;
  1521. }
  1522. uint32_t upDelay = OSUtils::jsonInt(link["upDelay"],-1);
  1523. uint32_t downDelay = OSUtils::jsonInt(link["downDelay"],-1);
  1524. uint8_t ipvPref = OSUtils::jsonInt(link["ipvPref"],0);
  1525. uint32_t linkMonitorInterval = OSUtils::jsonInt(link["monitorInterval"],(uint64_t)0);
  1526. std::string failoverToStr(OSUtils::jsonString(link["failoverTo"],""));
  1527. // Mode
  1528. std::string linkModeStr(OSUtils::jsonString(link["mode"],"spare"));
  1529. uint8_t linkMode = ZT_MULTIPATH_SLAVE_MODE_SPARE;
  1530. if (linkModeStr == "primary") { linkMode = ZT_MULTIPATH_SLAVE_MODE_PRIMARY; }
  1531. if (linkModeStr == "spare") { linkMode = ZT_MULTIPATH_SLAVE_MODE_SPARE; }
  1532. // ipvPref
  1533. if ((ipvPref != 0) && (ipvPref != 4) && (ipvPref != 6) && (ipvPref != 46) && (ipvPref != 64)) {
  1534. fprintf(stderr, "error: invalid ipvPref value (%d), link disabled.\n", ipvPref);
  1535. enabled = false;
  1536. }
  1537. if (linkMode == ZT_MULTIPATH_SLAVE_MODE_SPARE && failoverToStr.length()) {
  1538. fprintf(stderr, "error: cannot specify failover links for spares, link disabled.\n");
  1539. failoverToStr = "";
  1540. enabled = false;
  1541. }
  1542. _node->bondController()->addCustomLink(customPolicyStr, new Link(linkNameStr,ipvPref,speed,linkMonitorInterval,upDelay,downDelay,enabled,linkMode,failoverToStr,alloc));
  1543. }
  1544. std::string linkSelectMethodStr(OSUtils::jsonString(customPolicy["activeReselect"],"optimize"));
  1545. if (linkSelectMethodStr == "always") {
  1546. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_ALWAYS);
  1547. }
  1548. if (linkSelectMethodStr == "better") {
  1549. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_BETTER);
  1550. }
  1551. if (linkSelectMethodStr == "failure") {
  1552. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_FAILURE);
  1553. }
  1554. if (linkSelectMethodStr == "optimize") {
  1555. newTemplateBond->setLinkSelectMethod(ZT_MULTIPATH_RESELECTION_POLICY_OPTIMIZE);
  1556. }
  1557. if (newTemplateBond->getLinkSelectMethod() < 0 || newTemplateBond->getLinkSelectMethod() > 3) {
  1558. fprintf(stderr, "warning: invalid value (%s) for linkSelectMethod, assuming mode: always\n", linkSelectMethodStr.c_str());
  1559. }
  1560. /*
  1561. newBond->setPolicy(_node->bondController()->getPolicyCodeByStr(basePolicyStr));
  1562. newBond->setFlowHashing((bool)OSUtils::jsonInt(userSpecifiedBondingPolicies[i]["allowFlowHashing"],(bool)allowFlowHashing));
  1563. newBond->setBondMonitorInterval((unsigned int)OSUtils::jsonInt(userSpecifiedBondingPolicies[i]["monitorInterval"],(uint64_t)0));
  1564. newBond->setAllowPathNegotiation((bool)OSUtils::jsonInt(userSpecifiedBondingPolicies[i]["allowPathNegotiation"],(bool)false));
  1565. */
  1566. if (!_node->bondController()->addCustomPolicy(newTemplateBond)) {
  1567. fprintf(stderr, "error: a custom policy of this name (%s) already exists.\n", customPolicyStr.c_str());
  1568. }
  1569. }
  1570. // Peer-specific bonding
  1571. json &peerSpecificBonds = settings["peerSpecificBonds"];
  1572. for (json::iterator peerItr = peerSpecificBonds.begin(); peerItr != peerSpecificBonds.end();++peerItr) {
  1573. _node->bondController()->assignBondingPolicyToPeer(std::stoull(peerItr.key(),0,16), peerItr.value());
  1574. }
  1575. // Check settings
  1576. if (defaultBondingPolicyStr.length() && !defaultBondingPolicy && !_node->bondController()->inUse()) {
  1577. fprintf(stderr, "error: unknown policy (%s) specified by defaultBondingPolicy, link disabled.\n", defaultBondingPolicyStr.c_str());
  1578. }
  1579. }
  1580. // bondingPolicy cannot be used with allowTcpFallbackRelay
  1581. _allowTcpFallbackRelay = OSUtils::jsonBool(settings["allowTcpFallbackRelay"],true) && !(_node->bondController()->inUse());
  1582. _primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
  1583. _allowSecondaryPort = OSUtils::jsonBool(settings["allowSecondaryPort"],true);
  1584. _secondaryPort = (unsigned int)OSUtils::jsonInt(settings["secondaryPort"],0);
  1585. _tertiaryPort = (unsigned int)OSUtils::jsonInt(settings["tertiaryPort"],0);
  1586. if (_secondaryPort != 0 || _tertiaryPort != 0) {
  1587. fprintf(stderr,"WARNING: using manually-specified ports. This can cause NAT issues." ZT_EOL_S);
  1588. }
  1589. _portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true);
  1590. #ifndef ZT_SDK
  1591. const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT));
  1592. const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false);
  1593. if (((up == "apply")||(up == "download"))||(udist)) {
  1594. if (!_updater)
  1595. _updater = new SoftwareUpdater(*_node,_homePath);
  1596. _updateAutoApply = (up == "apply");
  1597. _updater->setUpdateDistribution(udist);
  1598. _updater->setChannel(OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL));
  1599. } else {
  1600. delete _updater;
  1601. _updater = (SoftwareUpdater *)0;
  1602. _updateAutoApply = false;
  1603. }
  1604. #endif
  1605. json &ignoreIfs = settings["interfacePrefixBlacklist"];
  1606. if (ignoreIfs.is_array()) {
  1607. for(unsigned long i=0;i<ignoreIfs.size();++i) {
  1608. const std::string tmp(OSUtils::jsonString(ignoreIfs[i],""));
  1609. if (tmp.length() > 0)
  1610. _interfacePrefixBlacklist.push_back(tmp);
  1611. }
  1612. }
  1613. json &amf = settings["allowManagementFrom"];
  1614. if (amf.is_array()) {
  1615. for(unsigned long i=0;i<amf.size();++i) {
  1616. const InetAddress nw(OSUtils::jsonString(amf[i],"").c_str());
  1617. if (nw)
  1618. _allowManagementFrom.push_back(nw);
  1619. }
  1620. }
  1621. }
  1622. #if ZT_VAULT_SUPPORT
  1623. json &vault = settings["vault"];
  1624. if (vault.is_object()) {
  1625. const std::string url(OSUtils::jsonString(vault["vaultURL"], "").c_str());
  1626. if (!url.empty()) {
  1627. _vaultURL = url;
  1628. }
  1629. const std::string token(OSUtils::jsonString(vault["vaultToken"], "").c_str());
  1630. if (!token.empty()) {
  1631. _vaultToken = token;
  1632. }
  1633. const std::string path(OSUtils::jsonString(vault["vaultPath"], "").c_str());
  1634. if (!path.empty()) {
  1635. _vaultPath = path;
  1636. }
  1637. }
  1638. // also check environment variables for values. Environment variables
  1639. // will override local.conf variables
  1640. const std::string envURL(getenv("VAULT_ADDR"));
  1641. if (!envURL.empty()) {
  1642. _vaultURL = envURL;
  1643. }
  1644. const std::string envToken(getenv("VAULT_TOKEN"));
  1645. if (!envToken.empty()) {
  1646. _vaultToken = envToken;
  1647. }
  1648. const std::string envPath(getenv("VAULT_PATH"));
  1649. if (!envPath.empty()) {
  1650. _vaultPath = envPath;
  1651. }
  1652. if (!_vaultURL.empty() && !_vaultToken.empty()) {
  1653. _vaultEnabled = true;
  1654. }
  1655. #endif
  1656. // Checks if a managed IP or route target is allowed
  1657. bool checkIfManagedIsAllowed(const NetworkState &n,const InetAddress &target)
  1658. {
  1659. if (!n.settings.allowManaged)
  1660. return false;
  1661. if (n.settings.allowManagedWhitelist.size() > 0) {
  1662. bool allowed = false;
  1663. for (InetAddress addr : n.settings.allowManagedWhitelist) {
  1664. if (addr.containsAddress(target) && addr.netmaskBits() <= target.netmaskBits()) {
  1665. allowed = true;
  1666. break;
  1667. }
  1668. }
  1669. if (!allowed) return false;
  1670. }
  1671. if (target.isDefaultRoute())
  1672. return n.settings.allowDefault;
  1673. switch(target.ipScope()) {
  1674. case InetAddress::IP_SCOPE_NONE:
  1675. case InetAddress::IP_SCOPE_MULTICAST:
  1676. case InetAddress::IP_SCOPE_LOOPBACK:
  1677. case InetAddress::IP_SCOPE_LINK_LOCAL:
  1678. return false;
  1679. case InetAddress::IP_SCOPE_GLOBAL:
  1680. return n.settings.allowGlobal;
  1681. default:
  1682. return true;
  1683. }
  1684. }
  1685. // Match only an IP from a vector of IPs -- used in syncManagedStuff()
  1686. inline bool matchIpOnly(const std::set<InetAddress> &ips,const InetAddress &ip) const
  1687. {
  1688. for(std::set<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  1689. if (i->ipsEqual(ip))
  1690. return true;
  1691. }
  1692. return false;
  1693. }
  1694. // Apply or update managed IPs for a configured network (be sure n.tap exists)
  1695. void syncManagedStuff(NetworkState &n,bool syncIps,bool syncRoutes, bool syncDns)
  1696. {
  1697. char ipbuf[64];
  1698. // assumes _nets_m is locked
  1699. if (syncIps) {
  1700. std::vector<InetAddress> newManagedIps;
  1701. newManagedIps.reserve(n.config.assignedAddressCount);
  1702. for(unsigned int i=0;i<n.config.assignedAddressCount;++i) {
  1703. const InetAddress *ii = reinterpret_cast<const InetAddress *>(&(n.config.assignedAddresses[i]));
  1704. if (checkIfManagedIsAllowed(n,*ii))
  1705. newManagedIps.push_back(*ii);
  1706. }
  1707. std::sort(newManagedIps.begin(),newManagedIps.end());
  1708. newManagedIps.erase(std::unique(newManagedIps.begin(),newManagedIps.end()),newManagedIps.end());
  1709. for(std::vector<InetAddress>::iterator ip(n.managedIps.begin());ip!=n.managedIps.end();++ip) {
  1710. if (std::find(newManagedIps.begin(),newManagedIps.end(),*ip) == newManagedIps.end()) {
  1711. if (!n.tap->removeIp(*ip))
  1712. fprintf(stderr,"ERROR: unable to remove ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  1713. }
  1714. }
  1715. #ifdef __SYNOLOGY__
  1716. if (!n.tap->addIpSyn(newManagedIps))
  1717. fprintf(stderr,"ERROR: unable to add ip addresses to ifcfg" ZT_EOL_S);
  1718. #else
  1719. for(std::vector<InetAddress>::iterator ip(newManagedIps.begin());ip!=newManagedIps.end();++ip) {
  1720. if (std::find(n.managedIps.begin(),n.managedIps.end(),*ip) == n.managedIps.end()) {
  1721. if (!n.tap->addIp(*ip))
  1722. fprintf(stderr,"ERROR: unable to add ip address %s" ZT_EOL_S, ip->toString(ipbuf));
  1723. }
  1724. }
  1725. #endif
  1726. n.managedIps.swap(newManagedIps);
  1727. }
  1728. if (syncRoutes) {
  1729. // Get tap device name (use LUID in hex on Windows) and IP addresses.
  1730. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  1731. char tapdevbuf[64];
  1732. OSUtils::ztsnprintf(tapdevbuf,sizeof(tapdevbuf),"%.16llx",(unsigned long long)((WindowsEthernetTap *)(n.tap.get()))->luid().Value);
  1733. std::string tapdev(tapdevbuf);
  1734. #else
  1735. std::string tapdev(n.tap->deviceName());
  1736. #endif
  1737. std::vector<InetAddress> tapIps(n.tap->ips());
  1738. std::set<InetAddress> myIps(tapIps.begin(), tapIps.end());
  1739. for(unsigned int i=0;i<n.config.assignedAddressCount;++i)
  1740. myIps.insert(InetAddress(n.config.assignedAddresses[i]));
  1741. std::set<InetAddress> haveRouteTargets;
  1742. for(unsigned int i=0;i<n.config.routeCount;++i) {
  1743. const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
  1744. const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
  1745. // Make sure we are allowed to set this managed route, and that 'via' is not our IP. The latter
  1746. // avoids setting routes via the router on the router.
  1747. if ( (!checkIfManagedIsAllowed(n,*target)) || ((via->ss_family == target->ss_family)&&(matchIpOnly(myIps,*via))) )
  1748. continue;
  1749. // Find an IP on the interface that can be a source IP, abort if no IPs assigned.
  1750. const InetAddress *src = nullptr;
  1751. unsigned int mostMatchingPrefixBits = 0;
  1752. for(std::set<InetAddress>::const_iterator i(myIps.begin());i!=myIps.end();++i) {
  1753. const unsigned int matchingPrefixBits = i->matchingPrefixBits(*target);
  1754. if (matchingPrefixBits >= mostMatchingPrefixBits) {
  1755. mostMatchingPrefixBits = matchingPrefixBits;
  1756. src = &(*i);
  1757. }
  1758. }
  1759. if (!src)
  1760. continue;
  1761. // Ignore routes implied by local managed IPs since adding the IP adds the route.
  1762. // Apple on the other hand seems to need this at least on some versions.
  1763. #ifndef __APPLE__
  1764. bool haveRoute = false;
  1765. for(std::vector<InetAddress>::iterator ip(n.managedIps.begin());ip!=n.managedIps.end();++ip) {
  1766. if ((target->netmaskBits() == ip->netmaskBits())&&(target->containsAddress(*ip))) {
  1767. haveRoute = true;
  1768. break;
  1769. }
  1770. }
  1771. if (haveRoute)
  1772. continue;
  1773. #endif
  1774. haveRouteTargets.insert(*target);
  1775. #ifndef ZT_SDK
  1776. SharedPtr<ManagedRoute> &mr = n.managedRoutes[*target];
  1777. if (!mr)
  1778. mr.set(new ManagedRoute(*target, *via, *src, tapdev.c_str()));
  1779. #endif
  1780. }
  1781. for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes.begin());r!=n.managedRoutes.end();) {
  1782. if (haveRouteTargets.find(r->first) == haveRouteTargets.end())
  1783. n.managedRoutes.erase(r++);
  1784. else ++r;
  1785. }
  1786. // Sync device-local managed routes first, then indirect results. That way
  1787. // we don't get destination unreachable for routes that are via things
  1788. // that do not yet have routes in the system.
  1789. for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes.begin());r!=n.managedRoutes.end();++r) {
  1790. if (!r->second->via())
  1791. r->second->sync();
  1792. }
  1793. for(std::map< InetAddress, SharedPtr<ManagedRoute> >::iterator r(n.managedRoutes.begin());r!=n.managedRoutes.end();++r) {
  1794. if (r->second->via())
  1795. r->second->sync();
  1796. }
  1797. }
  1798. if (syncDns) {
  1799. if (n.settings.allowDNS) {
  1800. if (strlen(n.config.dns.domain) != 0) {
  1801. std::vector<InetAddress> servers;
  1802. for (int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  1803. InetAddress a(n.config.dns.server_addr[j]);
  1804. if (a.isV4() || a.isV6()) {
  1805. servers.push_back(a);
  1806. }
  1807. }
  1808. n.tap->setDns(n.config.dns.domain, servers);
  1809. }
  1810. } else {
  1811. #ifdef __APPLE__
  1812. MacDNSHelper::removeDNS(n.config.nwid);
  1813. #elif defined(__WINDOWS__)
  1814. WinDNSHelper::removeDNS(n.config.nwid);
  1815. #endif
  1816. }
  1817. }
  1818. }
  1819. // =========================================================================
  1820. // Handlers for Node and Phy<> callbacks
  1821. // =========================================================================
  1822. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len)
  1823. {
  1824. const uint64_t now = OSUtils::now();
  1825. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL))
  1826. _lastDirectReceiveFromGlobal = now;
  1827. const ZT_ResultCode rc = _node->processWirePacket(nullptr,now,reinterpret_cast<int64_t>(sock),reinterpret_cast<const struct sockaddr_storage *>(from),data,len,&_nextBackgroundTaskDeadline);
  1828. if (ZT_ResultCode_isFatal(rc)) {
  1829. char tmp[256];
  1830. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  1831. Mutex::Lock _l(_termReason_m);
  1832. _termReason = ONE_UNRECOVERABLE_ERROR;
  1833. _fatalErrorMessage = tmp;
  1834. this->terminate();
  1835. }
  1836. }
  1837. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  1838. {
  1839. if (!success) {
  1840. phyOnTcpClose(sock,uptr);
  1841. return;
  1842. }
  1843. TcpConnection *const tc = reinterpret_cast<TcpConnection *>(*uptr);
  1844. if (!tc) { // sanity check
  1845. _phy.close(sock,true);
  1846. return;
  1847. }
  1848. tc->sock = sock;
  1849. if (tc->type == TcpConnection::TCP_TUNNEL_OUTGOING) {
  1850. if (_tcpFallbackTunnel)
  1851. _phy.close(_tcpFallbackTunnel->sock);
  1852. _tcpFallbackTunnel = tc;
  1853. _phy.streamSend(sock,ZT_TCP_TUNNEL_HELLO,sizeof(ZT_TCP_TUNNEL_HELLO));
  1854. } else {
  1855. _phy.close(sock,true);
  1856. }
  1857. }
  1858. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
  1859. {
  1860. if (!from) {
  1861. _phy.close(sockN,false);
  1862. return;
  1863. } else {
  1864. #ifdef ZT_SDK
  1865. // Immediately close new local connections. The intention is to prevent the backplane from being accessed when operating as libzt
  1866. if (!allowHttpBackplaneManagement && ((InetAddress*)from)->ipScope() == InetAddress::IP_SCOPE_LOOPBACK) {
  1867. _phy.close(sockN,false);
  1868. return;
  1869. }
  1870. #endif
  1871. TcpConnection *tc = new TcpConnection();
  1872. {
  1873. Mutex::Lock _l(_tcpConnections_m);
  1874. _tcpConnections.push_back(tc);
  1875. }
  1876. tc->type = TcpConnection::TCP_UNCATEGORIZED_INCOMING;
  1877. tc->parent = this;
  1878. tc->sock = sockN;
  1879. tc->remoteAddr = from;
  1880. tc->lastReceive = OSUtils::now();
  1881. http_parser_init(&(tc->parser),HTTP_REQUEST);
  1882. tc->parser.data = (void *)tc;
  1883. tc->messageSize = 0;
  1884. *uptrN = (void *)tc;
  1885. }
  1886. }
  1887. void phyOnTcpClose(PhySocket *sock,void **uptr)
  1888. {
  1889. TcpConnection *tc = (TcpConnection *)*uptr;
  1890. if (tc) {
  1891. if (tc == _tcpFallbackTunnel) {
  1892. _tcpFallbackTunnel = (TcpConnection *)0;
  1893. }
  1894. {
  1895. Mutex::Lock _l(_tcpConnections_m);
  1896. _tcpConnections.erase(std::remove(_tcpConnections.begin(),_tcpConnections.end(),tc),_tcpConnections.end());
  1897. }
  1898. delete tc;
  1899. }
  1900. }
  1901. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  1902. {
  1903. try {
  1904. if (!len) return; // sanity check, should never happen
  1905. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  1906. tc->lastReceive = OSUtils::now();
  1907. switch(tc->type) {
  1908. case TcpConnection::TCP_UNCATEGORIZED_INCOMING:
  1909. switch(reinterpret_cast<uint8_t *>(data)[0]) {
  1910. // HTTP: GET, PUT, POST, HEAD, DELETE
  1911. case 'G':
  1912. case 'P':
  1913. case 'D':
  1914. case 'H': {
  1915. // This is only allowed from IPs permitted to access the management
  1916. // backplane, which is just 127.0.0.1/::1 unless otherwise configured.
  1917. bool allow;
  1918. {
  1919. Mutex::Lock _l(_localConfig_m);
  1920. if (_allowManagementFrom.size() == 0) {
  1921. allow = (tc->remoteAddr.ipScope() == InetAddress::IP_SCOPE_LOOPBACK);
  1922. } else {
  1923. allow = false;
  1924. for(std::vector<InetAddress>::const_iterator i(_allowManagementFrom.begin());i!=_allowManagementFrom.end();++i) {
  1925. if (i->containsAddress(tc->remoteAddr)) {
  1926. allow = true;
  1927. break;
  1928. }
  1929. }
  1930. }
  1931. }
  1932. if (allow) {
  1933. tc->type = TcpConnection::TCP_HTTP_INCOMING;
  1934. phyOnTcpData(sock,uptr,data,len);
  1935. } else {
  1936. _phy.close(sock);
  1937. }
  1938. } break;
  1939. // Drop unknown protocols
  1940. default:
  1941. _phy.close(sock);
  1942. break;
  1943. }
  1944. return;
  1945. case TcpConnection::TCP_HTTP_INCOMING:
  1946. case TcpConnection::TCP_HTTP_OUTGOING:
  1947. http_parser_execute(&(tc->parser),&HTTP_PARSER_SETTINGS,(const char *)data,len);
  1948. if ((tc->parser.upgrade)||(tc->parser.http_errno != HPE_OK))
  1949. _phy.close(sock);
  1950. return;
  1951. case TcpConnection::TCP_TUNNEL_OUTGOING:
  1952. tc->readq.append((const char *)data,len);
  1953. while (tc->readq.length() >= 5) {
  1954. const char *data = tc->readq.data();
  1955. const unsigned long mlen = ( ((((unsigned long)data[3]) & 0xff) << 8) | (((unsigned long)data[4]) & 0xff) );
  1956. if (tc->readq.length() >= (mlen + 5)) {
  1957. InetAddress from;
  1958. unsigned long plen = mlen; // payload length, modified if there's an IP header
  1959. data += 5; // skip forward past pseudo-TLS junk and mlen
  1960. if (plen == 4) {
  1961. // Hello message, which isn't sent by proxy and would be ignored by client
  1962. } else if (plen) {
  1963. // Messages should contain IPv4 or IPv6 source IP address data
  1964. switch(data[0]) {
  1965. case 4: // IPv4
  1966. if (plen >= 7) {
  1967. from.set((const void *)(data + 1),4,((((unsigned int)data[5]) & 0xff) << 8) | (((unsigned int)data[6]) & 0xff));
  1968. data += 7; // type + 4 byte IP + 2 byte port
  1969. plen -= 7;
  1970. } else {
  1971. _phy.close(sock);
  1972. return;
  1973. }
  1974. break;
  1975. case 6: // IPv6
  1976. if (plen >= 19) {
  1977. from.set((const void *)(data + 1),16,((((unsigned int)data[17]) & 0xff) << 8) | (((unsigned int)data[18]) & 0xff));
  1978. data += 19; // type + 16 byte IP + 2 byte port
  1979. plen -= 19;
  1980. } else {
  1981. _phy.close(sock);
  1982. return;
  1983. }
  1984. break;
  1985. case 0: // none/omitted
  1986. ++data;
  1987. --plen;
  1988. break;
  1989. default: // invalid address type
  1990. _phy.close(sock);
  1991. return;
  1992. }
  1993. if (from) {
  1994. InetAddress fakeTcpLocalInterfaceAddress((uint32_t)0xffffffff,0xffff);
  1995. const ZT_ResultCode rc = _node->processWirePacket(
  1996. (void *)0,
  1997. OSUtils::now(),
  1998. -1,
  1999. reinterpret_cast<struct sockaddr_storage *>(&from),
  2000. data,
  2001. plen,
  2002. &_nextBackgroundTaskDeadline);
  2003. if (ZT_ResultCode_isFatal(rc)) {
  2004. char tmp[256];
  2005. OSUtils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc);
  2006. Mutex::Lock _l(_termReason_m);
  2007. _termReason = ONE_UNRECOVERABLE_ERROR;
  2008. _fatalErrorMessage = tmp;
  2009. this->terminate();
  2010. _phy.close(sock);
  2011. return;
  2012. }
  2013. }
  2014. }
  2015. if (tc->readq.length() > (mlen + 5))
  2016. tc->readq.erase(tc->readq.begin(),tc->readq.begin() + (mlen + 5));
  2017. else tc->readq.clear();
  2018. } else break;
  2019. }
  2020. return;
  2021. }
  2022. } catch ( ... ) {
  2023. _phy.close(sock);
  2024. }
  2025. }
  2026. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  2027. {
  2028. TcpConnection *tc = reinterpret_cast<TcpConnection *>(*uptr);
  2029. bool closeit = false;
  2030. {
  2031. Mutex::Lock _l(tc->writeq_m);
  2032. if (tc->writeq.length() > 0) {
  2033. long sent = (long)_phy.streamSend(sock,tc->writeq.data(),(unsigned long)tc->writeq.length(),true);
  2034. if (sent > 0) {
  2035. if ((unsigned long)sent >= (unsigned long)tc->writeq.length()) {
  2036. tc->writeq.clear();
  2037. _phy.setNotifyWritable(sock,false);
  2038. if (tc->type == TcpConnection::TCP_HTTP_INCOMING)
  2039. closeit = true; // HTTP keep alive not supported
  2040. } else {
  2041. tc->writeq.erase(tc->writeq.begin(),tc->writeq.begin() + sent);
  2042. }
  2043. }
  2044. } else {
  2045. _phy.setNotifyWritable(sock,false);
  2046. }
  2047. }
  2048. if (closeit)
  2049. _phy.close(sock);
  2050. }
  2051. inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
  2052. inline void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {}
  2053. inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
  2054. inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  2055. inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {}
  2056. inline int nodeVirtualNetworkConfigFunction(uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwc)
  2057. {
  2058. Mutex::Lock _l(_nets_m);
  2059. NetworkState &n = _nets[nwid];
  2060. switch(op) {
  2061. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
  2062. if (!n.tap) {
  2063. try {
  2064. char friendlyName[128];
  2065. OSUtils::ztsnprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid);
  2066. n.tap = EthernetTap::newInstance(
  2067. nullptr,
  2068. _homePath.c_str(),
  2069. MAC(nwc->mac),
  2070. nwc->mtu,
  2071. (unsigned int)ZT_IF_METRIC,
  2072. nwid,
  2073. friendlyName,
  2074. StapFrameHandler,
  2075. (void *)this);
  2076. *nuptr = (void *)&n;
  2077. char nlcpath[256];
  2078. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  2079. std::string nlcbuf;
  2080. if (OSUtils::readFile(nlcpath,nlcbuf)) {
  2081. Dictionary<4096> nc;
  2082. nc.load(nlcbuf.c_str());
  2083. Buffer<1024> allowManaged;
  2084. if (nc.get("allowManaged", allowManaged) && allowManaged.size() != 0) {
  2085. std::string addresses (allowManaged.begin(), allowManaged.size());
  2086. if (allowManaged.size() <= 5) { // untidy parsing for backward compatibility
  2087. if (allowManaged[0] == '1' || allowManaged[0] == 't' || allowManaged[0] == 'T') {
  2088. n.settings.allowManaged = true;
  2089. } else {
  2090. n.settings.allowManaged = false;
  2091. }
  2092. } else {
  2093. // this should be a list of IP addresses
  2094. n.settings.allowManaged = true;
  2095. size_t pos = 0;
  2096. while (true) {
  2097. size_t nextPos = addresses.find(',', pos);
  2098. std::string address = addresses.substr(pos, (nextPos == std::string::npos ? addresses.size() : nextPos) - pos);
  2099. n.settings.allowManagedWhitelist.push_back(InetAddress(address.c_str()));
  2100. if (nextPos == std::string::npos) break;
  2101. pos = nextPos + 1;
  2102. }
  2103. }
  2104. } else {
  2105. n.settings.allowManaged = true;
  2106. }
  2107. n.settings.allowGlobal = nc.getB("allowGlobal", false);
  2108. n.settings.allowDefault = nc.getB("allowDefault", false);
  2109. n.settings.allowDNS = nc.getB("allowDNS", false);
  2110. }
  2111. } catch (std::exception &exc) {
  2112. #ifdef __WINDOWS__
  2113. FILE *tapFailLog = fopen((_homePath + ZT_PATH_SEPARATOR_S"port_error_log.txt").c_str(),"a");
  2114. if (tapFailLog) {
  2115. fprintf(tapFailLog,"%.16llx: %s" ZT_EOL_S,(unsigned long long)nwid,exc.what());
  2116. fclose(tapFailLog);
  2117. }
  2118. #else
  2119. fprintf(stderr,"ERROR: unable to configure virtual network port: %s" ZT_EOL_S,exc.what());
  2120. #endif
  2121. _nets.erase(nwid);
  2122. return -999;
  2123. } catch ( ... ) {
  2124. return -999; // tap init failed
  2125. }
  2126. }
  2127. // After setting up tap, fall through to CONFIG_UPDATE since we also want to do this...
  2128. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
  2129. memcpy(&(n.config),nwc,sizeof(ZT_VirtualNetworkConfig));
  2130. if (n.tap) { // sanity check
  2131. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2132. // wait for up to 5 seconds for the WindowsEthernetTap to actually be initialized
  2133. //
  2134. // without WindowsEthernetTap::isInitialized() returning true, the won't actually
  2135. // be online yet and setting managed routes on it will fail.
  2136. const int MAX_SLEEP_COUNT = 500;
  2137. for (int i = 0; !((WindowsEthernetTap *)(n.tap.get()))->isInitialized() && i < MAX_SLEEP_COUNT; i++) {
  2138. Sleep(10);
  2139. }
  2140. #endif
  2141. syncManagedStuff(n,true,true,true);
  2142. n.tap->setMtu(nwc->mtu);
  2143. } else {
  2144. _nets.erase(nwid);
  2145. return -999; // tap init failed
  2146. }
  2147. break;
  2148. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
  2149. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
  2150. if (n.tap) { // sanity check
  2151. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2152. std::string winInstanceId(((WindowsEthernetTap *)(n.tap.get()))->instanceId());
  2153. #endif
  2154. *nuptr = (void *)0;
  2155. n.tap.reset();
  2156. _nets.erase(nwid);
  2157. #if defined(__WINDOWS__) && !defined(ZT_SDK)
  2158. if ((op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY)&&(winInstanceId.length() > 0))
  2159. WindowsEthernetTap::deletePersistentTapDevice(winInstanceId.c_str());
  2160. #endif
  2161. if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) {
  2162. char nlcpath[256];
  2163. OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid);
  2164. OSUtils::rm(nlcpath);
  2165. }
  2166. } else {
  2167. _nets.erase(nwid);
  2168. }
  2169. break;
  2170. }
  2171. return 0;
  2172. }
  2173. inline void nodeEventCallback(enum ZT_Event event,const void *metaData)
  2174. {
  2175. switch(event) {
  2176. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  2177. Mutex::Lock _l(_termReason_m);
  2178. _termReason = ONE_IDENTITY_COLLISION;
  2179. _fatalErrorMessage = "identity/address collision";
  2180. this->terminate();
  2181. } break;
  2182. case ZT_EVENT_TRACE: {
  2183. if (metaData) {
  2184. ::fprintf(stderr,"%s" ZT_EOL_S,(const char *)metaData);
  2185. ::fflush(stderr);
  2186. }
  2187. } break;
  2188. case ZT_EVENT_USER_MESSAGE: {
  2189. const ZT_UserMessage *um = reinterpret_cast<const ZT_UserMessage *>(metaData);
  2190. if ((um->typeId == ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE)&&(_updater)) {
  2191. _updater->handleSoftwareUpdateUserMessage(um->origin,um->data,um->length);
  2192. }
  2193. } break;
  2194. case ZT_EVENT_REMOTE_TRACE: {
  2195. const ZT_RemoteTrace *rt = reinterpret_cast<const ZT_RemoteTrace *>(metaData);
  2196. if ((rt)&&(rt->len > 0)&&(rt->len <= ZT_MAX_REMOTE_TRACE_SIZE)&&(rt->data))
  2197. _controller->handleRemoteTrace(*rt);
  2198. }
  2199. default:
  2200. break;
  2201. }
  2202. }
  2203. #if ZT_VAULT_SUPPORT
  2204. inline bool nodeVaultPutIdentity(enum ZT_StateObjectType type, const void *data, int len)
  2205. {
  2206. bool retval = false;
  2207. if (type != ZT_STATE_OBJECT_IDENTITY_PUBLIC && type != ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2208. return retval;
  2209. }
  2210. CURL *curl = curl_easy_init();
  2211. if (curl) {
  2212. char token[512] = { 0 };
  2213. snprintf(token, sizeof(token), "X-Vault-Token: %s", _vaultToken.c_str());
  2214. struct curl_slist *chunk = NULL;
  2215. chunk = curl_slist_append(chunk, token);
  2216. char content_type[512] = { 0 };
  2217. snprintf(content_type, sizeof(content_type), "Content-Type: application/json");
  2218. chunk = curl_slist_append(chunk, content_type);
  2219. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  2220. char url[2048] = { 0 };
  2221. snprintf(url, sizeof(url), "%s/v1/%s", _vaultURL.c_str(), _vaultPath.c_str());
  2222. curl_easy_setopt(curl, CURLOPT_URL, url);
  2223. json d = json::object();
  2224. if (type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2225. std::string key((const char*)data, len);
  2226. d["public"] = key;
  2227. }
  2228. else if (type == ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2229. std::string key((const char*)data, len);
  2230. d["secret"] = key;
  2231. }
  2232. if (!d.empty()) {
  2233. std::string post = d.dump();
  2234. if (!post.empty()) {
  2235. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
  2236. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post.length());
  2237. #ifndef NDEBUG
  2238. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  2239. #endif
  2240. CURLcode res = curl_easy_perform(curl);
  2241. if (res == CURLE_OK) {
  2242. long response_code = 0;
  2243. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  2244. if (response_code == 200 || response_code == 204) {
  2245. retval = true;
  2246. }
  2247. }
  2248. }
  2249. }
  2250. curl_easy_cleanup(curl);
  2251. curl = NULL;
  2252. curl_slist_free_all(chunk);
  2253. chunk = NULL;
  2254. }
  2255. return retval;
  2256. }
  2257. #endif
  2258. inline void nodeStatePutFunction(enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  2259. {
  2260. #if ZT_VAULT_SUPPORT
  2261. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC)) {
  2262. if (nodeVaultPutIdentity(type, data, len)) {
  2263. // value successfully written to Vault
  2264. return;
  2265. }
  2266. // else fallback to disk
  2267. }
  2268. #endif
  2269. char p[1024];
  2270. FILE *f;
  2271. bool secure = false;
  2272. char dirname[1024];
  2273. dirname[0] = 0;
  2274. switch(type) {
  2275. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  2276. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  2277. break;
  2278. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  2279. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  2280. secure = true;
  2281. break;
  2282. case ZT_STATE_OBJECT_PLANET:
  2283. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str());
  2284. break;
  2285. case ZT_STATE_OBJECT_MOON:
  2286. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "moons.d",_homePath.c_str());
  2287. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.moon",dirname,(unsigned long long)id[0]);
  2288. break;
  2289. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  2290. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "networks.d",_homePath.c_str());
  2291. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.conf",dirname,(unsigned long long)id[0]);
  2292. secure = true;
  2293. break;
  2294. case ZT_STATE_OBJECT_PEER:
  2295. OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "peers.d",_homePath.c_str());
  2296. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.10llx.peer",dirname,(unsigned long long)id[0]);
  2297. break;
  2298. default:
  2299. return;
  2300. }
  2301. if ((len >= 0)&&(data)) {
  2302. // Check to see if we've already written this first. This reduces
  2303. // redundant writes and I/O overhead on most platforms and has
  2304. // little effect on others.
  2305. f = fopen(p,"rb");
  2306. if (f) {
  2307. char *const buf = (char *)malloc(len*4);
  2308. if (buf) {
  2309. long l = (long)fread(buf,1,(size_t)(len*4),f);
  2310. fclose(f);
  2311. if ((l == (long)len)&&(memcmp(data,buf,l) == 0)) {
  2312. free(buf);
  2313. return;
  2314. }
  2315. free(buf);
  2316. }
  2317. }
  2318. f = fopen(p,"wb");
  2319. if ((!f)&&(dirname[0])) { // create subdirectory if it does not exist
  2320. OSUtils::mkdir(dirname);
  2321. f = fopen(p,"wb");
  2322. }
  2323. if (f) {
  2324. if (fwrite(data,len,1,f) != 1)
  2325. fprintf(stderr,"WARNING: unable to write to file: %s (I/O error)" ZT_EOL_S,p);
  2326. fclose(f);
  2327. if (secure)
  2328. OSUtils::lockDownFile(p,false);
  2329. } else {
  2330. fprintf(stderr,"WARNING: unable to write to file: %s (unable to open)" ZT_EOL_S,p);
  2331. }
  2332. } else {
  2333. OSUtils::rm(p);
  2334. }
  2335. }
  2336. #if ZT_VAULT_SUPPORT
  2337. inline int nodeVaultGetIdentity(enum ZT_StateObjectType type, void *data, unsigned int maxlen)
  2338. {
  2339. if (type != ZT_STATE_OBJECT_IDENTITY_SECRET && type != ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2340. return -1;
  2341. }
  2342. int ret = -1;
  2343. CURL *curl = curl_easy_init();
  2344. if (curl) {
  2345. char token[512] = { 0 };
  2346. snprintf(token, sizeof(token), "X-Vault-Token: %s", _vaultToken.c_str());
  2347. struct curl_slist *chunk = NULL;
  2348. chunk = curl_slist_append(chunk, token);
  2349. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  2350. char url[2048] = { 0 };
  2351. snprintf(url, sizeof(url), "%s/v1/%s", _vaultURL.c_str(), _vaultPath.c_str());
  2352. curl_easy_setopt(curl, CURLOPT_URL, url);
  2353. std::string response;
  2354. std::string res_headers;
  2355. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlResponseWrite);
  2356. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  2357. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &res_headers);
  2358. #ifndef NDEBUG
  2359. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  2360. #endif
  2361. CURLcode res = curl_easy_perform(curl);
  2362. if (res == CURLE_OK) {
  2363. long response_code = 0;
  2364. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  2365. if (response_code == 200) {
  2366. try {
  2367. json payload = json::parse(response);
  2368. if (!payload["data"].is_null()) {
  2369. json &d = payload["data"];
  2370. if (type == ZT_STATE_OBJECT_IDENTITY_SECRET) {
  2371. std::string secret = OSUtils::jsonString(d["secret"],"");
  2372. if (!secret.empty()) {
  2373. ret = (int)secret.length();
  2374. memcpy(data, secret.c_str(), ret);
  2375. }
  2376. }
  2377. else if (type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) {
  2378. std::string pub = OSUtils::jsonString(d["public"],"");
  2379. if (!pub.empty()) {
  2380. ret = (int)pub.length();
  2381. memcpy(data, pub.c_str(), ret);
  2382. }
  2383. }
  2384. }
  2385. }
  2386. catch (...) {
  2387. ret = -1;
  2388. }
  2389. }
  2390. }
  2391. curl_easy_cleanup(curl);
  2392. curl = NULL;
  2393. curl_slist_free_all(chunk);
  2394. chunk = NULL;
  2395. }
  2396. return ret;
  2397. }
  2398. #endif
  2399. inline int nodeStateGetFunction(enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  2400. {
  2401. #if ZT_VAULT_SUPPORT
  2402. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC) ) {
  2403. int retval = nodeVaultGetIdentity(type, data, maxlen);
  2404. if (retval >= 0)
  2405. return retval;
  2406. // else continue file based lookup
  2407. }
  2408. #endif
  2409. char p[4096];
  2410. switch(type) {
  2411. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  2412. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str());
  2413. break;
  2414. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  2415. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str());
  2416. break;
  2417. case ZT_STATE_OBJECT_PLANET:
  2418. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str());
  2419. break;
  2420. case ZT_STATE_OBJECT_MOON:
  2421. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "moons.d" ZT_PATH_SEPARATOR_S "%.16llx.moon",_homePath.c_str(),(unsigned long long)id[0]);
  2422. break;
  2423. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  2424. 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]);
  2425. break;
  2426. case ZT_STATE_OBJECT_PEER:
  2427. 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]);
  2428. break;
  2429. default:
  2430. return -1;
  2431. }
  2432. FILE *f = fopen(p,"rb");
  2433. if (f) {
  2434. int n = (int)fread(data,1,maxlen,f);
  2435. fclose(f);
  2436. #if ZT_VAULT_SUPPORT
  2437. if (_vaultEnabled && (type == ZT_STATE_OBJECT_IDENTITY_SECRET || type == ZT_STATE_OBJECT_IDENTITY_PUBLIC)) {
  2438. // If we've gotten here while Vault is enabled, Vault does not know the key and it's been
  2439. // read from disk instead.
  2440. //
  2441. // We should put the value in Vault and remove the local file.
  2442. if (nodeVaultPutIdentity(type, data, n)) {
  2443. unlink(p);
  2444. }
  2445. }
  2446. #endif
  2447. if (n >= 0)
  2448. return n;
  2449. }
  2450. return -1;
  2451. }
  2452. inline int nodeWirePacketSendFunction(const int64_t localSocket,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
  2453. {
  2454. #ifdef ZT_TCP_FALLBACK_RELAY
  2455. if(_allowTcpFallbackRelay) {
  2456. if (addr->ss_family == AF_INET) {
  2457. // TCP fallback tunnel support, currently IPv4 only
  2458. if ((len >= 16)&&(reinterpret_cast<const InetAddress *>(addr)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) {
  2459. // Engage TCP tunnel fallback if we haven't received anything valid from a global
  2460. // IP address in ZT_TCP_FALLBACK_AFTER milliseconds. If we do start getting
  2461. // valid direct traffic we'll stop using it and close the socket after a while.
  2462. const int64_t now = OSUtils::now();
  2463. if (((now - _lastDirectReceiveFromGlobal) > ZT_TCP_FALLBACK_AFTER)&&((now - _lastRestart) > ZT_TCP_FALLBACK_AFTER)) {
  2464. if (_tcpFallbackTunnel) {
  2465. bool flushNow = false;
  2466. {
  2467. Mutex::Lock _l(_tcpFallbackTunnel->writeq_m);
  2468. if (_tcpFallbackTunnel->writeq.size() < (1024 * 64)) {
  2469. if (_tcpFallbackTunnel->writeq.length() == 0) {
  2470. _phy.setNotifyWritable(_tcpFallbackTunnel->sock,true);
  2471. flushNow = true;
  2472. }
  2473. const unsigned long mlen = len + 7;
  2474. _tcpFallbackTunnel->writeq.push_back((char)0x17);
  2475. _tcpFallbackTunnel->writeq.push_back((char)0x03);
  2476. _tcpFallbackTunnel->writeq.push_back((char)0x03); // fake TLS 1.2 header
  2477. _tcpFallbackTunnel->writeq.push_back((char)((mlen >> 8) & 0xff));
  2478. _tcpFallbackTunnel->writeq.push_back((char)(mlen & 0xff));
  2479. _tcpFallbackTunnel->writeq.push_back((char)4); // IPv4
  2480. _tcpFallbackTunnel->writeq.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_addr.s_addr))),4);
  2481. _tcpFallbackTunnel->writeq.append(reinterpret_cast<const char *>(reinterpret_cast<const void *>(&(reinterpret_cast<const struct sockaddr_in *>(addr)->sin_port))),2);
  2482. _tcpFallbackTunnel->writeq.append((const char *)data,len);
  2483. }
  2484. }
  2485. if (flushNow) {
  2486. void *tmpptr = (void *)_tcpFallbackTunnel;
  2487. phyOnTcpWritable(_tcpFallbackTunnel->sock,&tmpptr);
  2488. }
  2489. } else if (((now - _lastSendToGlobalV4) < ZT_TCP_FALLBACK_AFTER)&&((now - _lastSendToGlobalV4) > (ZT_PING_CHECK_INVERVAL / 2))) {
  2490. const InetAddress addr(ZT_TCP_FALLBACK_RELAY);
  2491. TcpConnection *tc = new TcpConnection();
  2492. {
  2493. Mutex::Lock _l(_tcpConnections_m);
  2494. _tcpConnections.push_back(tc);
  2495. }
  2496. tc->type = TcpConnection::TCP_TUNNEL_OUTGOING;
  2497. tc->remoteAddr = addr;
  2498. tc->lastReceive = OSUtils::now();
  2499. tc->parent = this;
  2500. tc->sock = (PhySocket *)0; // set in connect handler
  2501. tc->messageSize = 0;
  2502. bool connected = false;
  2503. _phy.tcpConnect(reinterpret_cast<const struct sockaddr *>(&addr),connected,(void *)tc,true);
  2504. }
  2505. }
  2506. _lastSendToGlobalV4 = now;
  2507. }
  2508. }
  2509. }
  2510. #endif // ZT_TCP_FALLBACK_RELAY
  2511. // Even when relaying we still send via UDP. This way if UDP starts
  2512. // working we can instantly "fail forward" to it and stop using TCP
  2513. // proxy fallback, which is slow.
  2514. if ((localSocket != -1)&&(localSocket != 0)&&(_binder.isUdpSocketValid((PhySocket *)((uintptr_t)localSocket)))) {
  2515. if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),ttl);
  2516. const bool r = _phy.udpSend((PhySocket *)((uintptr_t)localSocket),(const struct sockaddr *)addr,data,len);
  2517. if ((ttl)&&(addr->ss_family == AF_INET)) _phy.setIp4UdpTtl((PhySocket *)((uintptr_t)localSocket),255);
  2518. return ((r) ? 0 : -1);
  2519. } else {
  2520. return ((_binder.udpSendAll(_phy,addr,data,len,ttl)) ? 0 : -1);
  2521. }
  2522. }
  2523. 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)
  2524. {
  2525. NetworkState *n = reinterpret_cast<NetworkState *>(*nuptr);
  2526. if ((!n)||(!n->tap))
  2527. return;
  2528. n->tap->put(MAC(sourceMac),MAC(destMac),etherType,data,len);
  2529. }
  2530. inline int nodePathCheckFunction(uint64_t ztaddr,const int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  2531. {
  2532. // Make sure we're not trying to do ZeroTier-over-ZeroTier
  2533. {
  2534. Mutex::Lock _l(_nets_m);
  2535. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  2536. if (n->second.tap) {
  2537. std::vector<InetAddress> ips(n->second.tap->ips());
  2538. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  2539. if (i->containsAddress(*(reinterpret_cast<const InetAddress *>(remoteAddr)))) {
  2540. return 0;
  2541. }
  2542. }
  2543. }
  2544. }
  2545. }
  2546. /* Note: I do not think we need to scan for overlap with managed routes
  2547. * because of the "route forking" and interface binding that we do. This
  2548. * ensures (we hope) that ZeroTier traffic will still take the physical
  2549. * path even if its managed routes override this for other traffic. Will
  2550. * revisit if we see recursion problems. */
  2551. // Check blacklists
  2552. const Hashtable< uint64_t,std::vector<InetAddress> > *blh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  2553. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  2554. if (remoteAddr->ss_family == AF_INET) {
  2555. blh = &_v4Blacklists;
  2556. gbl = &_globalV4Blacklist;
  2557. } else if (remoteAddr->ss_family == AF_INET6) {
  2558. blh = &_v6Blacklists;
  2559. gbl = &_globalV6Blacklist;
  2560. }
  2561. if (blh) {
  2562. Mutex::Lock _l(_localConfig_m);
  2563. const std::vector<InetAddress> *l = blh->get(ztaddr);
  2564. if (l) {
  2565. for(std::vector<InetAddress>::const_iterator a(l->begin());a!=l->end();++a) {
  2566. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  2567. return 0;
  2568. }
  2569. }
  2570. }
  2571. if (gbl) {
  2572. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  2573. if (a->containsAddress(*reinterpret_cast<const InetAddress *>(remoteAddr)))
  2574. return 0;
  2575. }
  2576. }
  2577. return 1;
  2578. }
  2579. inline int nodePathLookupFunction(uint64_t ztaddr,int family,struct sockaddr_storage *result)
  2580. {
  2581. const Hashtable< uint64_t,std::vector<InetAddress> > *lh = (const Hashtable< uint64_t,std::vector<InetAddress> > *)0;
  2582. if (family < 0)
  2583. lh = (_node->prng() & 1) ? &_v4Hints : &_v6Hints;
  2584. else if (family == AF_INET)
  2585. lh = &_v4Hints;
  2586. else if (family == AF_INET6)
  2587. lh = &_v6Hints;
  2588. else return 0;
  2589. const std::vector<InetAddress> *l = lh->get(ztaddr);
  2590. if ((l)&&(l->size() > 0)) {
  2591. memcpy(result,&((*l)[(unsigned long)_node->prng() % l->size()]),sizeof(struct sockaddr_storage));
  2592. return 1;
  2593. } else return 0;
  2594. }
  2595. inline void tapFrameHandler(uint64_t nwid,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  2596. {
  2597. _node->processVirtualNetworkFrame((void *)0,OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&_nextBackgroundTaskDeadline);
  2598. }
  2599. inline void onHttpRequestToServer(TcpConnection *tc)
  2600. {
  2601. char tmpn[4096];
  2602. std::string data;
  2603. std::string contentType("text/plain"); // default if not changed in handleRequest()
  2604. unsigned int scode = 404;
  2605. // Note that we check allowed IP ranges when HTTP connections are first detected in
  2606. // phyOnTcpData(). If we made it here the source IP is okay.
  2607. try {
  2608. scode = handleControlPlaneHttpRequest(tc->remoteAddr,tc->parser.method,tc->url,tc->headers,tc->readq,data,contentType);
  2609. } catch (std::exception &exc) {
  2610. fprintf(stderr,"WARNING: unexpected exception processing control HTTP request: %s" ZT_EOL_S,exc.what());
  2611. scode = 500;
  2612. } catch ( ... ) {
  2613. fprintf(stderr,"WARNING: unexpected exception processing control HTTP request: unknown exception" ZT_EOL_S);
  2614. scode = 500;
  2615. }
  2616. const char *scodestr;
  2617. switch(scode) {
  2618. case 200: scodestr = "OK"; break;
  2619. case 400: scodestr = "Bad Request"; break;
  2620. case 401: scodestr = "Unauthorized"; break;
  2621. case 403: scodestr = "Forbidden"; break;
  2622. case 404: scodestr = "Not Found"; break;
  2623. case 500: scodestr = "Internal Server Error"; break;
  2624. case 501: scodestr = "Not Implemented"; break;
  2625. case 503: scodestr = "Service Unavailable"; break;
  2626. default: scodestr = "Error"; break;
  2627. }
  2628. 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",
  2629. scode,
  2630. scodestr,
  2631. contentType.c_str(),
  2632. (unsigned long)data.length());
  2633. {
  2634. Mutex::Lock _l(tc->writeq_m);
  2635. tc->writeq = tmpn;
  2636. if (tc->parser.method != HTTP_HEAD)
  2637. tc->writeq.append(data);
  2638. }
  2639. _phy.setNotifyWritable(tc->sock,true);
  2640. }
  2641. inline void onHttpResponseFromClient(TcpConnection *tc)
  2642. {
  2643. _phy.close(tc->sock);
  2644. }
  2645. bool shouldBindInterface(const char *ifname,const InetAddress &ifaddr)
  2646. {
  2647. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  2648. if ((ifname[0] == 'l')&&(ifname[1] == 'o')) return false; // loopback
  2649. if ((ifname[0] == 'z')&&(ifname[1] == 't')) return false; // sanity check: zt#
  2650. if ((ifname[0] == 't')&&(ifname[1] == 'u')&&(ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  2651. if ((ifname[0] == 't')&&(ifname[1] == 'a')&&(ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  2652. #endif
  2653. #ifdef __APPLE__
  2654. if ((ifname[0] == 'f')&&(ifname[1] == 'e')&&(ifname[2] == 't')&&(ifname[3] == 'h')) return false; // ... as is feth#
  2655. if ((ifname[0] == 'l')&&(ifname[1] == 'o')) return false; // loopback
  2656. if ((ifname[0] == 'z')&&(ifname[1] == 't')) return false; // sanity check: zt#
  2657. if ((ifname[0] == 't')&&(ifname[1] == 'u')&&(ifname[2] == 'n')) return false; // tun# is probably an OpenVPN tunnel or similar
  2658. if ((ifname[0] == 't')&&(ifname[1] == 'a')&&(ifname[2] == 'p')) return false; // tap# is probably an OpenVPN tunnel or similar
  2659. if ((ifname[0] == 'u')&&(ifname[1] == 't')&&(ifname[2] == 'u')&&(ifname[3] == 'n')) return false; // ... as is utun#
  2660. #endif
  2661. {
  2662. Mutex::Lock _l(_localConfig_m);
  2663. for(std::vector<std::string>::const_iterator p(_interfacePrefixBlacklist.begin());p!=_interfacePrefixBlacklist.end();++p) {
  2664. if (!strncmp(p->c_str(),ifname,p->length()))
  2665. return false;
  2666. }
  2667. return _node->bondController()->allowedToBind(std::string(ifname));
  2668. }
  2669. {
  2670. // Check global blacklists
  2671. const std::vector<InetAddress> *gbl = (const std::vector<InetAddress> *)0;
  2672. if (ifaddr.ss_family == AF_INET) {
  2673. gbl = &_globalV4Blacklist;
  2674. } else if (ifaddr.ss_family == AF_INET6) {
  2675. gbl = &_globalV6Blacklist;
  2676. }
  2677. if (gbl) {
  2678. Mutex::Lock _l(_localConfig_m);
  2679. for(std::vector<InetAddress>::const_iterator a(gbl->begin());a!=gbl->end();++a) {
  2680. if (a->containsAddress(ifaddr))
  2681. return false;
  2682. }
  2683. }
  2684. }
  2685. {
  2686. Mutex::Lock _l(_nets_m);
  2687. for(std::map<uint64_t,NetworkState>::const_iterator n(_nets.begin());n!=_nets.end();++n) {
  2688. if (n->second.tap) {
  2689. std::vector<InetAddress> ips(n->second.tap->ips());
  2690. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  2691. if (i->ipsEqual(ifaddr))
  2692. return false;
  2693. }
  2694. }
  2695. }
  2696. }
  2697. return true;
  2698. }
  2699. bool _trialBind(unsigned int port)
  2700. {
  2701. struct sockaddr_in in4;
  2702. struct sockaddr_in6 in6;
  2703. PhySocket *tb;
  2704. memset(&in4,0,sizeof(in4));
  2705. in4.sin_family = AF_INET;
  2706. in4.sin_port = Utils::hton((uint16_t)port);
  2707. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0,0);
  2708. if (tb) {
  2709. _phy.close(tb,false);
  2710. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in4),(void *)0);
  2711. if (tb) {
  2712. _phy.close(tb,false);
  2713. return true;
  2714. }
  2715. }
  2716. memset(&in6,0,sizeof(in6));
  2717. in6.sin6_family = AF_INET6;
  2718. in6.sin6_port = Utils::hton((uint16_t)port);
  2719. tb = _phy.udpBind(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0,0);
  2720. if (tb) {
  2721. _phy.close(tb,false);
  2722. tb = _phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&in6),(void *)0);
  2723. if (tb) {
  2724. _phy.close(tb,false);
  2725. return true;
  2726. }
  2727. }
  2728. return false;
  2729. }
  2730. };
  2731. static int SnodeVirtualNetworkConfigFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t nwid,void **nuptr,enum ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nwconf)
  2732. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkConfigFunction(nwid,nuptr,op,nwconf); }
  2733. static void SnodeEventCallback(ZT_Node *node,void *uptr,void *tptr,enum ZT_Event event,const void *metaData)
  2734. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeEventCallback(event,metaData); }
  2735. static void SnodeStatePutFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],const void *data,int len)
  2736. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeStatePutFunction(type,id,data,len); }
  2737. static int SnodeStateGetFunction(ZT_Node *node,void *uptr,void *tptr,enum ZT_StateObjectType type,const uint64_t id[2],void *data,unsigned int maxlen)
  2738. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeStateGetFunction(type,id,data,maxlen); }
  2739. 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)
  2740. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodeWirePacketSendFunction(localSocket,addr,data,len,ttl); }
  2741. 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)
  2742. { reinterpret_cast<OneServiceImpl *>(uptr)->nodeVirtualNetworkFrameFunction(nwid,nuptr,sourceMac,destMac,etherType,vlanId,data,len); }
  2743. static int SnodePathCheckFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int64_t localSocket,const struct sockaddr_storage *remoteAddr)
  2744. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathCheckFunction(ztaddr,localSocket,remoteAddr); }
  2745. static int SnodePathLookupFunction(ZT_Node *node,void *uptr,void *tptr,uint64_t ztaddr,int family,struct sockaddr_storage *result)
  2746. { return reinterpret_cast<OneServiceImpl *>(uptr)->nodePathLookupFunction(ztaddr,family,result); }
  2747. 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)
  2748. { reinterpret_cast<OneServiceImpl *>(uptr)->tapFrameHandler(nwid,from,to,etherType,vlanId,data,len); }
  2749. static int ShttpOnMessageBegin(http_parser *parser)
  2750. {
  2751. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2752. tc->currentHeaderField = "";
  2753. tc->currentHeaderValue = "";
  2754. tc->messageSize = 0;
  2755. tc->url.clear();
  2756. tc->status.clear();
  2757. tc->headers.clear();
  2758. tc->readq.clear();
  2759. return 0;
  2760. }
  2761. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  2762. {
  2763. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2764. tc->messageSize += (unsigned long)length;
  2765. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2766. return -1;
  2767. tc->url.append(ptr,length);
  2768. return 0;
  2769. }
  2770. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  2771. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  2772. #else
  2773. static int ShttpOnStatus(http_parser *parser)
  2774. #endif
  2775. { return 0; }
  2776. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  2777. {
  2778. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2779. tc->messageSize += (unsigned long)length;
  2780. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2781. return -1;
  2782. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length())) {
  2783. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  2784. tc->currentHeaderField = "";
  2785. tc->currentHeaderValue = "";
  2786. }
  2787. for(size_t i=0;i<length;++i)
  2788. tc->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  2789. return 0;
  2790. }
  2791. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length)
  2792. {
  2793. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2794. tc->messageSize += (unsigned long)length;
  2795. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2796. return -1;
  2797. tc->currentHeaderValue.append(ptr,length);
  2798. return 0;
  2799. }
  2800. static int ShttpOnHeadersComplete(http_parser *parser)
  2801. {
  2802. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2803. if ((tc->currentHeaderField.length())&&(tc->currentHeaderValue.length()))
  2804. tc->headers[tc->currentHeaderField] = tc->currentHeaderValue;
  2805. return 0;
  2806. }
  2807. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  2808. {
  2809. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2810. tc->messageSize += (unsigned long)length;
  2811. if (tc->messageSize > ZT_MAX_HTTP_MESSAGE_SIZE)
  2812. return -1;
  2813. tc->readq.append(ptr,length);
  2814. return 0;
  2815. }
  2816. static int ShttpOnMessageComplete(http_parser *parser)
  2817. {
  2818. TcpConnection *tc = reinterpret_cast<TcpConnection *>(parser->data);
  2819. if (tc->type == TcpConnection::TCP_HTTP_INCOMING) {
  2820. tc->parent->onHttpRequestToServer(tc);
  2821. } else {
  2822. tc->parent->onHttpResponseFromClient(tc);
  2823. }
  2824. return 0;
  2825. }
  2826. } // anonymous namespace
  2827. std::string OneService::platformDefaultHomePath()
  2828. {
  2829. return OSUtils::platformDefaultHomePath();
  2830. }
  2831. OneService *OneService::newInstance(const char *hp,unsigned int port) { return new OneServiceImpl(hp,port); }
  2832. OneService::~OneService() {}
  2833. } // namespace ZeroTier