OneService.cpp 112 KB

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