Network.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. /*
  2. * Copyright (c)2019 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: 2026-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 "Network.hpp"
  14. #include "../include/ZeroTierDebug.h"
  15. #include "../version.h"
  16. #include "Address.hpp"
  17. #include "Buffer.hpp"
  18. #include "Constants.hpp"
  19. #include "ECC.hpp"
  20. #include "InetAddress.hpp"
  21. #include "MAC.hpp"
  22. #include "Metrics.hpp"
  23. #include "NetworkController.hpp"
  24. #include "Node.hpp"
  25. #include "Packet.hpp"
  26. #include "Peer.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "Switch.hpp"
  29. #include "Trace.hpp"
  30. #include <math.h>
  31. #include <set>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. namespace ZeroTier {
  36. namespace {
  37. // Returns true if packet appears valid; pos and proto will be set
  38. static inline bool _ipv6GetPayload(const uint8_t* frameData, unsigned int frameLen, unsigned int& pos, unsigned int& proto)
  39. {
  40. if (frameLen < 40) {
  41. return false;
  42. }
  43. pos = 40;
  44. proto = frameData[6];
  45. while (pos <= frameLen) {
  46. switch (proto) {
  47. case 0: // hop-by-hop options
  48. case 43: // routing
  49. case 60: // destination options
  50. case 135: // mobility options
  51. if ((pos + 8) > frameLen) {
  52. return false; // invalid!
  53. }
  54. proto = frameData[pos];
  55. pos += ((unsigned int)frameData[pos + 1] * 8) + 8;
  56. break;
  57. // case 44: // fragment -- we currently can't parse these and they are deprecated in IPv6 anyway
  58. // case 50:
  59. // case 51: // IPSec ESP and AH -- we have to stop here since this is encrypted stuff
  60. default:
  61. return true;
  62. }
  63. }
  64. return false; // overflow == invalid
  65. }
  66. enum _doZtFilterResult { DOZTFILTER_NO_MATCH, DOZTFILTER_DROP, DOZTFILTER_REDIRECT, DOZTFILTER_ACCEPT, DOZTFILTER_SUPER_ACCEPT };
  67. static _doZtFilterResult _doZtFilter(
  68. const RuntimeEnvironment* RR,
  69. Trace::RuleResultLog& rrl,
  70. const NetworkConfig& nconf,
  71. const Membership* membership, // can be NULL
  72. const bool inbound,
  73. const Address& ztSource,
  74. Address& ztDest, // MUTABLE -- is changed on REDIRECT actions
  75. const MAC& macSource,
  76. const MAC& macDest,
  77. const uint8_t* const frameData,
  78. const unsigned int frameLen,
  79. const unsigned int etherType,
  80. const unsigned int vlanId,
  81. const ZT_VirtualNetworkRule* rules, // cannot be NULL
  82. const unsigned int ruleCount,
  83. Address& cc, // MUTABLE -- set to TEE destination if TEE action is taken or left alone otherwise
  84. unsigned int& ccLength, // MUTABLE -- set to length of packet payload to TEE
  85. bool& ccWatch, // MUTABLE -- set to true for WATCH target as opposed to normal TEE
  86. uint8_t& qosBucket) // MUTABLE -- set to the value of the argument provided to PRIORITY
  87. {
  88. // Set to true if we are a TEE/REDIRECT/WATCH target
  89. bool superAccept = false;
  90. // The default match state for each set of entries starts as 'true' since an
  91. // ACTION with no MATCH entries preceding it is always taken.
  92. uint8_t thisSetMatches = 1;
  93. uint8_t skipDrop = 0;
  94. rrl.clear();
  95. // uncomment for easier debugging fprintf
  96. // if (!ztDest) { return DOZTFILTER_ACCEPT; }
  97. #ifdef ZT_TRACE
  98. // char buf[40], buf2[40];
  99. // fprintf(stderr, "\nsrc %s dest %s inbound: %d ethertype %u", ztSource.toString(buf), ztDest.toString(buf2), inbound, etherType);
  100. #endif
  101. for (unsigned int rn = 0; rn < ruleCount; ++rn) {
  102. const ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[rn].t & 0x3f);
  103. #ifdef ZT_TRACE
  104. // fprintf(stderr, "\n%02u %02d", rn, rt);
  105. #endif
  106. // First check if this is an ACTION
  107. if ((unsigned int)rt <= (unsigned int)ZT_NETWORK_RULE_ACTION__MAX_ID) {
  108. if (thisSetMatches) {
  109. switch (rt) {
  110. case ZT_NETWORK_RULE_ACTION_PRIORITY:
  111. qosBucket = (rules[rn].v.qosBucket <= 8) ? rules[rn].v.qosBucket : 4; // 4 = default bucket (no priority)
  112. return DOZTFILTER_ACCEPT;
  113. case ZT_NETWORK_RULE_ACTION_DROP: {
  114. if (! ! skipDrop) {
  115. #ifdef ZT_TRACE
  116. // fprintf(stderr, "\tskip Drop");
  117. #endif
  118. skipDrop = 0;
  119. continue;
  120. }
  121. #ifdef ZT_TRACE
  122. // fprintf(stderr, "\tDrop\n");
  123. #endif
  124. return DOZTFILTER_DROP;
  125. }
  126. case ZT_NETWORK_RULE_ACTION_ACCEPT: {
  127. #ifdef ZT_TRACE
  128. // fprintf(stderr, "\tAccept\n");
  129. #endif
  130. return (superAccept ? DOZTFILTER_SUPER_ACCEPT : DOZTFILTER_ACCEPT); // match, accept packet
  131. }
  132. // These are initially handled together since preliminary logic is common
  133. case ZT_NETWORK_RULE_ACTION_TEE:
  134. case ZT_NETWORK_RULE_ACTION_WATCH:
  135. case ZT_NETWORK_RULE_ACTION_REDIRECT: {
  136. const Address fwdAddr(rules[rn].v.fwd.address);
  137. if (fwdAddr == ztSource) {
  138. // Skip as no-op since source is target
  139. }
  140. else if (fwdAddr == RR->identity.address()) {
  141. if (inbound) {
  142. return DOZTFILTER_SUPER_ACCEPT;
  143. }
  144. else {
  145. }
  146. }
  147. else if (fwdAddr == ztDest) {
  148. }
  149. else {
  150. if (rt == ZT_NETWORK_RULE_ACTION_REDIRECT) {
  151. ztDest = fwdAddr;
  152. return DOZTFILTER_REDIRECT;
  153. }
  154. else {
  155. cc = fwdAddr;
  156. ccLength = (rules[rn].v.fwd.length != 0) ? ((frameLen < (unsigned int)rules[rn].v.fwd.length) ? frameLen : (unsigned int)rules[rn].v.fwd.length) : frameLen;
  157. ccWatch = (rt == ZT_NETWORK_RULE_ACTION_WATCH);
  158. }
  159. }
  160. }
  161. continue;
  162. case ZT_NETWORK_RULE_ACTION_BREAK:
  163. return DOZTFILTER_NO_MATCH;
  164. // Unrecognized ACTIONs are ignored as no-ops
  165. default:
  166. continue;
  167. }
  168. }
  169. else {
  170. // If this is an incoming packet and we are a TEE or REDIRECT target, we should
  171. // super-accept if we accept at all. This will cause us to accept redirected or
  172. // tee'd packets in spite of MAC and ZT addressing checks.
  173. if (inbound) {
  174. switch (rt) {
  175. case ZT_NETWORK_RULE_ACTION_TEE:
  176. case ZT_NETWORK_RULE_ACTION_WATCH:
  177. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  178. if (RR->identity.address() == rules[rn].v.fwd.address) {
  179. superAccept = true;
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. thisSetMatches = 1; // reset to default true for next batch of entries
  187. continue;
  188. }
  189. }
  190. // Circuit breaker: no need to evaluate an AND if the set's match state
  191. // is currently false since anything AND false is false.
  192. if ((! thisSetMatches) && (! (rules[rn].t & 0x40))) {
  193. rrl.logSkipped(rn, thisSetMatches);
  194. continue;
  195. }
  196. // If this was not an ACTION evaluate next MATCH and update thisSetMatches with (AND [result])
  197. uint8_t thisRuleMatches = 0;
  198. uint64_t ownershipVerificationMask = 1; // this magic value means it hasn't been computed yet -- this is done lazily the first time it's needed
  199. uint8_t hardYes = (rules[rn].t >> 7) ^ 1; // XOR with the NOT bit of the rule
  200. uint8_t hardNo = (rules[rn].t >> 7) ^ 0;
  201. switch (rt) {
  202. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  203. thisRuleMatches = (uint8_t)(rules[rn].v.zt == ztSource.toInt());
  204. break;
  205. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  206. thisRuleMatches = (uint8_t)(rules[rn].v.zt == ztDest.toInt());
  207. break;
  208. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  209. thisRuleMatches = (uint8_t)(rules[rn].v.vlanId == (uint16_t)vlanId);
  210. break;
  211. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  212. // NOT SUPPORTED YET
  213. thisRuleMatches = (uint8_t)(rules[rn].v.vlanPcp == 0);
  214. break;
  215. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  216. // NOT SUPPORTED YET
  217. thisRuleMatches = (uint8_t)(rules[rn].v.vlanDei == 0);
  218. break;
  219. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  220. thisRuleMatches = (uint8_t)(MAC(rules[rn].v.mac, 6) == macSource);
  221. break;
  222. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  223. thisRuleMatches = (uint8_t)(MAC(rules[rn].v.mac, 6) == macDest);
  224. break;
  225. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  226. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  227. thisRuleMatches = (uint8_t)(InetAddress((const void*)&(rules[rn].v.ipv4.ip), 4, rules[rn].v.ipv4.mask).containsAddress(InetAddress((const void*)(frameData + 12), 4, 0)));
  228. }
  229. else {
  230. thisRuleMatches = hardNo;
  231. }
  232. break;
  233. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  234. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  235. thisRuleMatches = (uint8_t)(InetAddress((const void*)&(rules[rn].v.ipv4.ip), 4, rules[rn].v.ipv4.mask).containsAddress(InetAddress((const void*)(frameData + 16), 4, 0)));
  236. }
  237. else {
  238. thisRuleMatches = hardNo;
  239. }
  240. break;
  241. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  242. if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  243. thisRuleMatches = (uint8_t)(InetAddress((const void*)rules[rn].v.ipv6.ip, 16, rules[rn].v.ipv6.mask).containsAddress(InetAddress((const void*)(frameData + 8), 16, 0)));
  244. }
  245. else {
  246. thisRuleMatches = hardNo;
  247. }
  248. break;
  249. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  250. if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  251. thisRuleMatches = (uint8_t)(InetAddress((const void*)rules[rn].v.ipv6.ip, 16, rules[rn].v.ipv6.mask).containsAddress(InetAddress((const void*)(frameData + 24), 16, 0)));
  252. }
  253. else {
  254. thisRuleMatches = hardNo;
  255. }
  256. break;
  257. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  258. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  259. const uint8_t tosMasked = frameData[1] & rules[rn].v.ipTos.mask;
  260. thisRuleMatches = (uint8_t)((tosMasked >= rules[rn].v.ipTos.value[0]) && (tosMasked <= rules[rn].v.ipTos.value[1]));
  261. }
  262. else if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  263. const uint8_t tosMasked = (((frameData[0] << 4) & 0xf0) | ((frameData[1] >> 4) & 0x0f)) & rules[rn].v.ipTos.mask;
  264. thisRuleMatches = (uint8_t)((tosMasked >= rules[rn].v.ipTos.value[0]) && (tosMasked <= rules[rn].v.ipTos.value[1]));
  265. }
  266. else {
  267. thisRuleMatches = hardNo;
  268. }
  269. break;
  270. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  271. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  272. thisRuleMatches = (uint8_t)(rules[rn].v.ipProtocol == frameData[9]);
  273. }
  274. else if (etherType == ZT_ETHERTYPE_IPV6) {
  275. unsigned int pos = 0, proto = 0;
  276. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  277. thisRuleMatches = (uint8_t)(rules[rn].v.ipProtocol == (uint8_t)proto);
  278. }
  279. else {
  280. thisRuleMatches = hardNo;
  281. }
  282. }
  283. else {
  284. thisRuleMatches = hardNo;
  285. }
  286. break;
  287. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  288. thisRuleMatches = (uint8_t)(rules[rn].v.etherType == (uint16_t)etherType);
  289. break;
  290. case ZT_NETWORK_RULE_MATCH_ICMP:
  291. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  292. if (frameData[9] == 0x01) { // IP protocol == ICMP
  293. const unsigned int ihl = (frameData[0] & 0xf) * 4;
  294. if (frameLen >= (ihl + 2)) {
  295. if (rules[rn].v.icmp.type == frameData[ihl]) {
  296. if ((rules[rn].v.icmp.flags & 0x01) != 0) {
  297. thisRuleMatches = (uint8_t)(frameData[ihl + 1] == rules[rn].v.icmp.code);
  298. }
  299. else {
  300. thisRuleMatches = hardYes;
  301. }
  302. }
  303. else {
  304. thisRuleMatches = hardNo;
  305. }
  306. }
  307. else {
  308. thisRuleMatches = hardNo;
  309. }
  310. }
  311. else {
  312. thisRuleMatches = hardNo;
  313. }
  314. }
  315. else if (etherType == ZT_ETHERTYPE_IPV6) {
  316. unsigned int pos = 0, proto = 0;
  317. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  318. if ((proto == 0x3a) && (frameLen >= (pos + 2))) {
  319. if (rules[rn].v.icmp.type == frameData[pos]) {
  320. if ((rules[rn].v.icmp.flags & 0x01) != 0) {
  321. thisRuleMatches = (uint8_t)(frameData[pos + 1] == rules[rn].v.icmp.code);
  322. }
  323. else {
  324. thisRuleMatches = hardYes;
  325. }
  326. }
  327. else {
  328. thisRuleMatches = hardNo;
  329. }
  330. }
  331. else {
  332. thisRuleMatches = hardNo;
  333. }
  334. }
  335. else {
  336. thisRuleMatches = hardNo;
  337. }
  338. }
  339. else {
  340. thisRuleMatches = hardNo;
  341. }
  342. break;
  343. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  344. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  345. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  346. const unsigned int headerLen = 4 * (frameData[0] & 0xf);
  347. int p = -1;
  348. switch (frameData[9]) { // IP protocol number
  349. // All these start with 16-bit source and destination port in that order
  350. case 0x06: // TCP
  351. case 0x11: // UDP
  352. case 0x84: // SCTP
  353. case 0x88: // UDPLite
  354. if (frameLen > (headerLen + 4)) {
  355. unsigned int pos = headerLen + ((rt == ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE) ? 2 : 0);
  356. p = (int)frameData[pos++] << 8;
  357. p |= (int)frameData[pos];
  358. }
  359. break;
  360. }
  361. thisRuleMatches = (p >= 0) ? (uint8_t)((p >= (int)rules[rn].v.port[0]) && (p <= (int)rules[rn].v.port[1])) : (uint8_t)0;
  362. }
  363. else if (etherType == ZT_ETHERTYPE_IPV6) {
  364. unsigned int pos = 0, proto = 0;
  365. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  366. int p = -1;
  367. switch (proto) { // IP protocol number
  368. // All these start with 16-bit source and destination port in that order
  369. case 0x06: // TCP
  370. case 0x11: // UDP
  371. case 0x84: // SCTP
  372. case 0x88: // UDPLite
  373. if (frameLen > (pos + 4)) {
  374. if (rt == ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE) {
  375. pos += 2;
  376. }
  377. p = (int)frameData[pos++] << 8;
  378. p |= (int)frameData[pos];
  379. }
  380. break;
  381. }
  382. thisRuleMatches = (p > 0) ? (uint8_t)((p >= (int)rules[rn].v.port[0]) && (p <= (int)rules[rn].v.port[1])) : (uint8_t)0;
  383. }
  384. else {
  385. thisRuleMatches = hardNo;
  386. }
  387. }
  388. else {
  389. thisRuleMatches = hardNo;
  390. }
  391. break;
  392. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: {
  393. uint64_t cf = (inbound) ? ZT_RULE_PACKET_CHARACTERISTICS_INBOUND : 0ULL;
  394. if (macDest.isMulticast()) {
  395. cf |= ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST;
  396. }
  397. if (macDest.isBroadcast()) {
  398. cf |= ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST;
  399. }
  400. if (ownershipVerificationMask == 1) {
  401. ownershipVerificationMask = 0;
  402. InetAddress src;
  403. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20)) {
  404. src.set((const void*)(frameData + 12), 4, 0);
  405. }
  406. else if ((etherType == ZT_ETHERTYPE_IPV6) && (frameLen >= 40)) {
  407. // IPv6 NDP requires special handling, since the src and dest IPs in the packet are empty or link-local.
  408. if ((frameLen >= (40 + 8 + 16)) && (frameData[6] == 0x3a) && ((frameData[40] == 0x87) || (frameData[40] == 0x88))) {
  409. if (frameData[40] == 0x87) {
  410. // Neighbor solicitations contain no reliable source address, so we implement a small
  411. // hack by considering them authenticated. Otherwise you would pretty much have to do
  412. // this manually in the rule set for IPv6 to work at all.
  413. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED;
  414. }
  415. else {
  416. // Neighbor advertisements on the other hand can absolutely be authenticated.
  417. src.set((const void*)(frameData + 40 + 8), 16, 0);
  418. }
  419. }
  420. else {
  421. // Other IPv6 packets can be handled normally
  422. src.set((const void*)(frameData + 8), 16, 0);
  423. }
  424. }
  425. else if ((etherType == ZT_ETHERTYPE_ARP) && (frameLen >= 28)) {
  426. src.set((const void*)(frameData + 14), 4, 0);
  427. }
  428. if (inbound) {
  429. if (membership) {
  430. if ((src) && (membership->hasCertificateOfOwnershipFor<InetAddress>(nconf, src))) {
  431. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED;
  432. }
  433. if (membership->hasCertificateOfOwnershipFor<MAC>(nconf, macSource)) {
  434. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED;
  435. }
  436. }
  437. }
  438. else {
  439. for (unsigned int i = 0; i < nconf.certificateOfOwnershipCount; ++i) {
  440. if ((src) && (nconf.certificatesOfOwnership[i].owns(src))) {
  441. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED;
  442. }
  443. if (nconf.certificatesOfOwnership[i].owns(macSource)) {
  444. ownershipVerificationMask |= ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED;
  445. }
  446. }
  447. }
  448. }
  449. cf |= ownershipVerificationMask;
  450. if ((etherType == ZT_ETHERTYPE_IPV4) && (frameLen >= 20) && (frameData[9] == 0x06)) {
  451. const unsigned int headerLen = 4 * (frameData[0] & 0xf);
  452. cf |= (uint64_t)frameData[headerLen + 13];
  453. cf |= (((uint64_t)(frameData[headerLen + 12] & 0x0f)) << 8);
  454. }
  455. else if (etherType == ZT_ETHERTYPE_IPV6) {
  456. unsigned int pos = 0, proto = 0;
  457. if (_ipv6GetPayload(frameData, frameLen, pos, proto)) {
  458. if ((proto == 0x06) && (frameLen > (pos + 14))) {
  459. cf |= (uint64_t)frameData[pos + 13];
  460. cf |= (((uint64_t)(frameData[pos + 12] & 0x0f)) << 8);
  461. }
  462. }
  463. }
  464. thisRuleMatches = (uint8_t)((cf & rules[rn].v.characteristics) != 0);
  465. } break;
  466. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  467. thisRuleMatches = (uint8_t)((frameLen >= (unsigned int)rules[rn].v.frameSize[0]) && (frameLen <= (unsigned int)rules[rn].v.frameSize[1]));
  468. break;
  469. case ZT_NETWORK_RULE_MATCH_RANDOM:
  470. thisRuleMatches = (uint8_t)((uint32_t)(RR->node->prng() & 0xffffffffULL) <= rules[rn].v.randomProbability);
  471. break;
  472. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  473. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  474. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  475. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  476. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL: {
  477. const Tag* const localTag = std::lower_bound(&(nconf.tags[0]), &(nconf.tags[nconf.tagCount]), rules[rn].v.tag.id, Tag::IdComparePredicate());
  478. if ((localTag != &(nconf.tags[nconf.tagCount])) && (localTag->id() == rules[rn].v.tag.id)) {
  479. const Tag* const remoteTag = ((membership) ? membership->getTag(nconf, rules[rn].v.tag.id) : (const Tag*)0);
  480. #ifdef ZT_TRACE
  481. /*fprintf(stderr, "\tlocal tag [%u: %u] remote tag [%u: %u] match [%u]",
  482. !!localTag ? localTag->id() : 0,
  483. !!localTag ? localTag->value() : 0,
  484. !!remoteTag ? remoteTag->id() : 0,
  485. !!remoteTag ? remoteTag->value() : 0,
  486. thisRuleMatches);*/
  487. #endif
  488. if (remoteTag) {
  489. const uint32_t ltv = localTag->value();
  490. const uint32_t rtv = remoteTag->value();
  491. if (rt == ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE) {
  492. const uint32_t diff = (ltv > rtv) ? (ltv - rtv) : (rtv - ltv);
  493. thisRuleMatches = (uint8_t)(diff <= rules[rn].v.tag.value);
  494. }
  495. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND) {
  496. thisRuleMatches = (uint8_t)((ltv & rtv) == rules[rn].v.tag.value);
  497. }
  498. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR) {
  499. thisRuleMatches = (uint8_t)((ltv | rtv) == rules[rn].v.tag.value);
  500. }
  501. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR) {
  502. thisRuleMatches = (uint8_t)((ltv ^ rtv) == rules[rn].v.tag.value);
  503. }
  504. else if (rt == ZT_NETWORK_RULE_MATCH_TAGS_EQUAL) {
  505. thisRuleMatches = (uint8_t)((ltv == rules[rn].v.tag.value) && (rtv == rules[rn].v.tag.value));
  506. }
  507. else { // sanity check, can't really happen
  508. thisRuleMatches = hardNo;
  509. }
  510. }
  511. else {
  512. if ((inbound) && (! superAccept)) {
  513. thisRuleMatches = hardNo;
  514. #ifdef ZT_TRACE
  515. // fprintf(stderr, "\tinbound ");
  516. #endif
  517. }
  518. else {
  519. // Outbound side is not strict since if we have to match both tags and
  520. // we are sending a first packet to a recipient, we probably do not know
  521. // about their tags yet. They will filter on inbound and we will filter
  522. // once we get their tag. If we are a tee/redirect target we are also
  523. // not strict since we likely do not have these tags.
  524. skipDrop = 1;
  525. thisRuleMatches = hardYes;
  526. #ifdef ZT_TRACE
  527. // fprintf(stderr, "\toutbound ");
  528. #endif
  529. }
  530. }
  531. }
  532. else {
  533. thisRuleMatches = hardNo;
  534. }
  535. } break;
  536. case ZT_NETWORK_RULE_MATCH_TAG_SENDER:
  537. case ZT_NETWORK_RULE_MATCH_TAG_RECEIVER: {
  538. const Tag* const localTag = std::lower_bound(&(nconf.tags[0]), &(nconf.tags[nconf.tagCount]), rules[rn].v.tag.id, Tag::IdComparePredicate());
  539. #ifdef ZT_TRACE
  540. /*const Tag *const remoteTag = ((membership) ? membership->getTag(nconf,rules[rn].v.tag.id) : (const Tag *)0);
  541. fprintf(stderr, "\tlocal tag [%u: %u] remote tag [%u: %u] match [%u]",
  542. !!localTag ? localTag->id() : 0,
  543. !!localTag ? localTag->value() : 0,
  544. !!remoteTag ? remoteTag->id() : 0,
  545. !!remoteTag ? remoteTag->value() : 0,
  546. thisRuleMatches);*/
  547. #endif
  548. if (superAccept) {
  549. skipDrop = 1;
  550. thisRuleMatches = hardYes;
  551. }
  552. else if (((rt == ZT_NETWORK_RULE_MATCH_TAG_SENDER) && (inbound)) || ((rt == ZT_NETWORK_RULE_MATCH_TAG_RECEIVER) && (! inbound))) {
  553. const Tag* const remoteTag = ((membership) ? membership->getTag(nconf, rules[rn].v.tag.id) : (const Tag*)0);
  554. if (remoteTag) {
  555. thisRuleMatches = (uint8_t)(remoteTag->value() == rules[rn].v.tag.value);
  556. }
  557. else {
  558. if (rt == ZT_NETWORK_RULE_MATCH_TAG_RECEIVER) {
  559. // If we are checking the receiver and this is an outbound packet, we
  560. // can't be strict since we may not yet know the receiver's tag.
  561. skipDrop = 1;
  562. thisRuleMatches = hardYes;
  563. }
  564. else {
  565. thisRuleMatches = hardNo;
  566. }
  567. }
  568. }
  569. else { // sender and outbound or receiver and inbound
  570. if ((localTag != &(nconf.tags[nconf.tagCount])) && (localTag->id() == rules[rn].v.tag.id)) {
  571. thisRuleMatches = (uint8_t)(localTag->value() == rules[rn].v.tag.value);
  572. }
  573. else {
  574. thisRuleMatches = hardNo;
  575. }
  576. }
  577. } break;
  578. case ZT_NETWORK_RULE_MATCH_INTEGER_RANGE: {
  579. uint64_t integer = 0;
  580. const unsigned int bits = (rules[rn].v.intRange.format & 63) + 1;
  581. const unsigned int bytes = ((bits + 8 - 1) / 8); // integer ceiling of division by 8
  582. if ((rules[rn].v.intRange.format & 0x80) == 0) {
  583. // Big-endian
  584. unsigned int idx = rules[rn].v.intRange.idx + (8 - bytes);
  585. const unsigned int eof = idx + bytes;
  586. if (eof <= frameLen) {
  587. while (idx < eof) {
  588. integer <<= 8;
  589. integer |= frameData[idx++];
  590. }
  591. }
  592. integer &= 0xffffffffffffffffULL >> (64 - bits);
  593. }
  594. else {
  595. // Little-endian
  596. unsigned int idx = rules[rn].v.intRange.idx;
  597. const unsigned int eof = idx + bytes;
  598. if (eof <= frameLen) {
  599. while (idx < eof) {
  600. integer >>= 8;
  601. integer |= ((uint64_t)frameData[idx++]) << 56;
  602. }
  603. }
  604. integer >>= (64 - bits);
  605. }
  606. thisRuleMatches = (uint8_t)((integer >= rules[rn].v.intRange.start) && (integer <= (rules[rn].v.intRange.start + (uint64_t)rules[rn].v.intRange.end)));
  607. } break;
  608. // The result of an unsupported MATCH is configurable at the network
  609. // level via a flag.
  610. default:
  611. thisRuleMatches = (uint8_t)((nconf.flags & ZT_NETWORKCONFIG_FLAG_RULES_RESULT_OF_UNSUPPORTED_MATCH) != 0);
  612. break;
  613. }
  614. rrl.log(rn, thisRuleMatches, thisSetMatches);
  615. if ((rules[rn].t & 0x40)) {
  616. thisSetMatches |= (thisRuleMatches ^ ((rules[rn].t >> 7) & 1));
  617. }
  618. else {
  619. thisSetMatches &= (thisRuleMatches ^ ((rules[rn].t >> 7) & 1));
  620. }
  621. }
  622. return DOZTFILTER_NO_MATCH;
  623. }
  624. } // anonymous namespace
  625. const ZeroTier::MulticastGroup Network::BROADCAST(ZeroTier::MAC(0xffffffffffffULL), 0);
  626. Network::Network(const RuntimeEnvironment* renv, void* tPtr, uint64_t nwid, void* uptr, const NetworkConfig* nconf)
  627. : RR(renv)
  628. , _uPtr(uptr)
  629. , _id(nwid)
  630. , _nwidStr(OSUtils::networkIDStr(nwid))
  631. , _lastAnnouncedMulticastGroupsUpstream(0)
  632. , _mac(renv->identity.address(), nwid)
  633. , _portInitialized(false)
  634. , _lastConfigUpdate(0)
  635. , _destroyed(false)
  636. , _netconfFailure(NETCONF_FAILURE_NONE)
  637. , _portError(0)
  638. , _num_multicast_groups { Metrics::network_num_multicast_groups.Add({ { "network_id", _nwidStr } }) }
  639. , _incoming_packets_accepted { Metrics::network_packets.Add({ { "direction", "rx" }, { "network_id", _nwidStr }, { "accepted", "yes" } }) }
  640. , _incoming_packets_dropped { Metrics::network_packets.Add({ { "direction", "rx" }, { "network_id", _nwidStr }, { "accepted", "no" } }) }
  641. , _outgoing_packets_accepted { Metrics::network_packets.Add({ { "direction", "tx" }, { "network_id", _nwidStr }, { "accepted", "yes" } }) }
  642. , _outgoing_packets_dropped { Metrics::network_packets.Add({ { "direction", "tx" }, { "network_id", _nwidStr }, { "accepted", "no" } }) }
  643. {
  644. for (int i = 0; i < ZT_NETWORK_MAX_INCOMING_UPDATES; ++i) {
  645. _incomingConfigChunks[i].ts = 0;
  646. }
  647. if (nconf) {
  648. this->setConfiguration(tPtr, *nconf, false);
  649. _lastConfigUpdate = 0; // still want to re-request since it's likely outdated
  650. }
  651. else {
  652. uint64_t tmp[2];
  653. tmp[0] = nwid;
  654. tmp[1] = 0;
  655. bool got = false;
  656. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>* dict = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  657. try {
  658. int n = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_NETWORK_CONFIG, tmp, dict->unsafeData(), ZT_NETWORKCONFIG_DICT_CAPACITY - 1);
  659. if (n > 1) {
  660. NetworkConfig* nconf = new NetworkConfig();
  661. try {
  662. if (nconf->fromDictionary(*dict)) {
  663. this->setConfiguration(tPtr, *nconf, false);
  664. _lastConfigUpdate = 0; // still want to re-request an update since it's likely outdated
  665. got = true;
  666. }
  667. }
  668. catch (...) {
  669. }
  670. delete nconf;
  671. }
  672. }
  673. catch (...) {
  674. }
  675. delete dict;
  676. if (! got) {
  677. RR->node->stateObjectPut(tPtr, ZT_STATE_OBJECT_NETWORK_CONFIG, tmp, "\n", 1);
  678. }
  679. }
  680. if (! _portInitialized) {
  681. ZT_VirtualNetworkConfig ctmp;
  682. memset(&ctmp, 0, sizeof(ZT_VirtualNetworkConfig));
  683. _externalConfig(&ctmp);
  684. _portError = RR->node->configureVirtualNetworkPort(tPtr, _id, &_uPtr, ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP, &ctmp);
  685. _portInitialized = true;
  686. }
  687. Metrics::network_num_joined++;
  688. }
  689. Network::~Network()
  690. {
  691. ZT_VirtualNetworkConfig ctmp;
  692. _externalConfig(&ctmp);
  693. Metrics::network_num_joined--;
  694. if (_destroyed) {
  695. // This is done in Node::leave() so we can pass tPtr properly
  696. // RR->node->configureVirtualNetworkPort((void *)0,_id,&_uPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  697. }
  698. else {
  699. RR->node->configureVirtualNetworkPort((void*)0, _id, &_uPtr, ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN, &ctmp);
  700. }
  701. }
  702. bool Network::filterOutgoingPacket(
  703. void* tPtr,
  704. const bool noTee,
  705. const Address& ztSource,
  706. const Address& ztDest,
  707. const MAC& macSource,
  708. const MAC& macDest,
  709. const uint8_t* frameData,
  710. const unsigned int frameLen,
  711. const unsigned int etherType,
  712. const unsigned int vlanId,
  713. uint8_t& qosBucket)
  714. {
  715. Address ztFinalDest(ztDest);
  716. int localCapabilityIndex = -1;
  717. int accept = 0;
  718. Trace::RuleResultLog rrl, crrl;
  719. Address cc;
  720. unsigned int ccLength = 0;
  721. bool ccWatch = false;
  722. Mutex::Lock _l(_lock);
  723. Membership* const membership = (ztDest) ? _memberships.get(ztDest) : (Membership*)0;
  724. switch (_doZtFilter(RR, rrl, _config, membership, false, ztSource, ztFinalDest, macSource, macDest, frameData, frameLen, etherType, vlanId, _config.rules, _config.ruleCount, cc, ccLength, ccWatch, qosBucket)) {
  725. case DOZTFILTER_NO_MATCH: {
  726. for (unsigned int c = 0; c < _config.capabilityCount; ++c) {
  727. ztFinalDest = ztDest; // sanity check, shouldn't be possible if there was no match
  728. Address cc2;
  729. unsigned int ccLength2 = 0;
  730. bool ccWatch2 = false;
  731. switch (_doZtFilter(
  732. RR,
  733. crrl,
  734. _config,
  735. membership,
  736. false,
  737. ztSource,
  738. ztFinalDest,
  739. macSource,
  740. macDest,
  741. frameData,
  742. frameLen,
  743. etherType,
  744. vlanId,
  745. _config.capabilities[c].rules(),
  746. _config.capabilities[c].ruleCount(),
  747. cc2,
  748. ccLength2,
  749. ccWatch2,
  750. qosBucket)) {
  751. case DOZTFILTER_NO_MATCH:
  752. case DOZTFILTER_DROP: // explicit DROP in a capability just terminates its evaluation and is an anti-pattern
  753. break;
  754. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztFinalDest will have been changed in _doZtFilter()
  755. case DOZTFILTER_ACCEPT:
  756. case DOZTFILTER_SUPER_ACCEPT: // no difference in behavior on outbound side in capabilities
  757. localCapabilityIndex = (int)c;
  758. accept = 1;
  759. if ((! noTee) && (cc2)) {
  760. Packet outp(cc2, RR->identity.address(), Packet::VERB_EXT_FRAME);
  761. outp.append(_id);
  762. outp.append((uint8_t)(ccWatch2 ? 0x16 : 0x02));
  763. macDest.appendTo(outp);
  764. macSource.appendTo(outp);
  765. outp.append((uint16_t)etherType);
  766. outp.append(frameData, ccLength2);
  767. outp.compress();
  768. RR->sw->send(tPtr, outp, true);
  769. }
  770. break;
  771. }
  772. if (accept) {
  773. break;
  774. }
  775. }
  776. } break;
  777. case DOZTFILTER_DROP:
  778. if (_config.remoteTraceTarget) {
  779. RR->t->networkFilter(tPtr, *this, rrl, (Trace::RuleResultLog*)0, (Capability*)0, ztSource, ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, noTee, false, 0);
  780. }
  781. return false;
  782. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztFinalDest will have been changed in _doZtFilter()
  783. case DOZTFILTER_ACCEPT:
  784. accept = 1;
  785. break;
  786. case DOZTFILTER_SUPER_ACCEPT:
  787. accept = 2;
  788. break;
  789. }
  790. if (accept) {
  791. _outgoing_packets_accepted++;
  792. if ((! noTee) && (cc)) {
  793. Packet outp(cc, RR->identity.address(), Packet::VERB_EXT_FRAME);
  794. outp.append(_id);
  795. outp.append((uint8_t)(ccWatch ? 0x16 : 0x02));
  796. macDest.appendTo(outp);
  797. macSource.appendTo(outp);
  798. outp.append((uint16_t)etherType);
  799. outp.append(frameData, ccLength);
  800. outp.compress();
  801. RR->sw->send(tPtr, outp, true);
  802. }
  803. if ((ztDest != ztFinalDest) && (ztFinalDest)) {
  804. Packet outp(ztFinalDest, RR->identity.address(), Packet::VERB_EXT_FRAME);
  805. outp.append(_id);
  806. outp.append((uint8_t)0x04);
  807. macDest.appendTo(outp);
  808. macSource.appendTo(outp);
  809. outp.append((uint16_t)etherType);
  810. outp.append(frameData, frameLen);
  811. outp.compress();
  812. RR->sw->send(tPtr, outp, true);
  813. if (_config.remoteTraceTarget) {
  814. RR->t->networkFilter(
  815. tPtr,
  816. *this,
  817. rrl,
  818. (localCapabilityIndex >= 0) ? &crrl : (Trace::RuleResultLog*)0,
  819. (localCapabilityIndex >= 0) ? &(_config.capabilities[localCapabilityIndex]) : (Capability*)0,
  820. ztSource,
  821. ztDest,
  822. macSource,
  823. macDest,
  824. frameData,
  825. frameLen,
  826. etherType,
  827. vlanId,
  828. noTee,
  829. false,
  830. 0);
  831. }
  832. return false; // DROP locally, since we redirected
  833. }
  834. else {
  835. if (_config.remoteTraceTarget) {
  836. RR->t->networkFilter(
  837. tPtr,
  838. *this,
  839. rrl,
  840. (localCapabilityIndex >= 0) ? &crrl : (Trace::RuleResultLog*)0,
  841. (localCapabilityIndex >= 0) ? &(_config.capabilities[localCapabilityIndex]) : (Capability*)0,
  842. ztSource,
  843. ztDest,
  844. macSource,
  845. macDest,
  846. frameData,
  847. frameLen,
  848. etherType,
  849. vlanId,
  850. noTee,
  851. false,
  852. 1);
  853. }
  854. return true;
  855. }
  856. }
  857. else {
  858. _outgoing_packets_dropped++;
  859. if (_config.remoteTraceTarget) {
  860. RR->t->networkFilter(
  861. tPtr,
  862. *this,
  863. rrl,
  864. (localCapabilityIndex >= 0) ? &crrl : (Trace::RuleResultLog*)0,
  865. (localCapabilityIndex >= 0) ? &(_config.capabilities[localCapabilityIndex]) : (Capability*)0,
  866. ztSource,
  867. ztDest,
  868. macSource,
  869. macDest,
  870. frameData,
  871. frameLen,
  872. etherType,
  873. vlanId,
  874. noTee,
  875. false,
  876. 0);
  877. }
  878. return false;
  879. }
  880. }
  881. int Network::filterIncomingPacket(
  882. void* tPtr,
  883. const SharedPtr<Peer>& sourcePeer,
  884. const Address& ztDest,
  885. const MAC& macSource,
  886. const MAC& macDest,
  887. const uint8_t* frameData,
  888. const unsigned int frameLen,
  889. const unsigned int etherType,
  890. const unsigned int vlanId)
  891. {
  892. Address ztFinalDest(ztDest);
  893. Trace::RuleResultLog rrl, crrl;
  894. int accept = 0;
  895. Address cc;
  896. unsigned int ccLength = 0;
  897. bool ccWatch = false;
  898. const Capability* c = (Capability*)0;
  899. uint8_t qosBucket = 255; // For incoming packets this is a dummy value
  900. Mutex::Lock _l(_lock);
  901. Membership& membership = _membership(sourcePeer->address());
  902. switch (_doZtFilter(RR, rrl, _config, &membership, true, sourcePeer->address(), ztFinalDest, macSource, macDest, frameData, frameLen, etherType, vlanId, _config.rules, _config.ruleCount, cc, ccLength, ccWatch, qosBucket)) {
  903. case DOZTFILTER_NO_MATCH: {
  904. Membership::CapabilityIterator mci(membership, _config);
  905. while ((c = mci.next())) {
  906. ztFinalDest = ztDest; // sanity check, should be unmodified if there was no match
  907. Address cc2;
  908. unsigned int ccLength2 = 0;
  909. bool ccWatch2 = false;
  910. switch (_doZtFilter(RR, crrl, _config, &membership, true, sourcePeer->address(), ztFinalDest, macSource, macDest, frameData, frameLen, etherType, vlanId, c->rules(), c->ruleCount(), cc2, ccLength2, ccWatch2, qosBucket)) {
  911. case DOZTFILTER_NO_MATCH:
  912. case DOZTFILTER_DROP: // explicit DROP in a capability just terminates its evaluation and is an anti-pattern
  913. break;
  914. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztDest will have been changed in _doZtFilter()
  915. case DOZTFILTER_ACCEPT:
  916. accept = 1; // ACCEPT
  917. break;
  918. case DOZTFILTER_SUPER_ACCEPT:
  919. accept = 2; // super-ACCEPT
  920. break;
  921. }
  922. if (accept) {
  923. if (cc2) {
  924. Packet outp(cc2, RR->identity.address(), Packet::VERB_EXT_FRAME);
  925. outp.append(_id);
  926. outp.append((uint8_t)(ccWatch2 ? 0x1c : 0x08));
  927. macDest.appendTo(outp);
  928. macSource.appendTo(outp);
  929. outp.append((uint16_t)etherType);
  930. outp.append(frameData, ccLength2);
  931. outp.compress();
  932. RR->sw->send(tPtr, outp, true);
  933. }
  934. break;
  935. }
  936. }
  937. } break;
  938. case DOZTFILTER_DROP:
  939. if (_config.remoteTraceTarget) {
  940. RR->t->networkFilter(tPtr, *this, rrl, (Trace::RuleResultLog*)0, (Capability*)0, sourcePeer->address(), ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, false, true, 0);
  941. }
  942. return 0; // DROP
  943. case DOZTFILTER_REDIRECT: // interpreted as ACCEPT but ztFinalDest will have been changed in _doZtFilter()
  944. case DOZTFILTER_ACCEPT:
  945. accept = 1; // ACCEPT
  946. break;
  947. case DOZTFILTER_SUPER_ACCEPT:
  948. accept = 2; // super-ACCEPT
  949. break;
  950. }
  951. if (accept) {
  952. _incoming_packets_accepted++;
  953. if (cc) {
  954. Packet outp(cc, RR->identity.address(), Packet::VERB_EXT_FRAME);
  955. outp.append(_id);
  956. outp.append((uint8_t)(ccWatch ? 0x1c : 0x08));
  957. macDest.appendTo(outp);
  958. macSource.appendTo(outp);
  959. outp.append((uint16_t)etherType);
  960. outp.append(frameData, ccLength);
  961. outp.compress();
  962. RR->sw->send(tPtr, outp, true);
  963. }
  964. if ((ztDest != ztFinalDest) && (ztFinalDest)) {
  965. Packet outp(ztFinalDest, RR->identity.address(), Packet::VERB_EXT_FRAME);
  966. outp.append(_id);
  967. outp.append((uint8_t)0x0a);
  968. macDest.appendTo(outp);
  969. macSource.appendTo(outp);
  970. outp.append((uint16_t)etherType);
  971. outp.append(frameData, frameLen);
  972. outp.compress();
  973. RR->sw->send(tPtr, outp, true);
  974. if (_config.remoteTraceTarget) {
  975. RR->t->networkFilter(tPtr, *this, rrl, (c) ? &crrl : (Trace::RuleResultLog*)0, c, sourcePeer->address(), ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, false, true, 0);
  976. }
  977. return 0; // DROP locally, since we redirected
  978. }
  979. }
  980. else {
  981. _incoming_packets_dropped++;
  982. }
  983. if (_config.remoteTraceTarget) {
  984. RR->t->networkFilter(tPtr, *this, rrl, (c) ? &crrl : (Trace::RuleResultLog*)0, c, sourcePeer->address(), ztDest, macSource, macDest, frameData, frameLen, etherType, vlanId, false, true, accept);
  985. }
  986. return accept;
  987. }
  988. bool Network::subscribedToMulticastGroup(const MulticastGroup& mg, bool includeBridgedGroups) const
  989. {
  990. Mutex::Lock _l(_lock);
  991. if (std::binary_search(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg)) {
  992. return true;
  993. }
  994. else if (includeBridgedGroups) {
  995. return _multicastGroupsBehindMe.contains(mg);
  996. }
  997. return false;
  998. }
  999. void Network::multicastSubscribe(void* tPtr, const MulticastGroup& mg)
  1000. {
  1001. Mutex::Lock _l(_lock);
  1002. if (! std::binary_search(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg)) {
  1003. _myMulticastGroups.insert(std::upper_bound(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg), mg);
  1004. _sendUpdatesToMembers(tPtr, &mg);
  1005. _num_multicast_groups++;
  1006. }
  1007. }
  1008. void Network::multicastUnsubscribe(const MulticastGroup& mg)
  1009. {
  1010. Mutex::Lock _l(_lock);
  1011. std::vector<MulticastGroup>::iterator i(std::lower_bound(_myMulticastGroups.begin(), _myMulticastGroups.end(), mg));
  1012. if ((i != _myMulticastGroups.end()) && (*i == mg)) {
  1013. _myMulticastGroups.erase(i);
  1014. _num_multicast_groups--;
  1015. }
  1016. }
  1017. uint64_t Network::handleConfigChunk(void* tPtr, const uint64_t packetId, const Address& source, const Buffer<ZT_PROTO_MAX_PACKET_LENGTH>& chunk, unsigned int ptr)
  1018. {
  1019. if (_destroyed) {
  1020. return 0;
  1021. }
  1022. const unsigned int start = ptr;
  1023. ptr += 8; // skip network ID, which is already obviously known
  1024. const unsigned int chunkLen = chunk.at<uint16_t>(ptr);
  1025. ptr += 2;
  1026. const void* chunkData = chunk.field(ptr, chunkLen);
  1027. ptr += chunkLen;
  1028. NetworkConfig* nc = (NetworkConfig*)0;
  1029. uint64_t configUpdateId;
  1030. {
  1031. Mutex::Lock _l(_lock);
  1032. _IncomingConfigChunk* c = (_IncomingConfigChunk*)0;
  1033. uint64_t chunkId = 0;
  1034. unsigned long totalLength, chunkIndex;
  1035. if (ptr < chunk.size()) {
  1036. const bool fastPropagate = ((chunk[ptr++] & 0x01) != 0);
  1037. configUpdateId = chunk.at<uint64_t>(ptr);
  1038. ptr += 8;
  1039. totalLength = chunk.at<uint32_t>(ptr);
  1040. ptr += 4;
  1041. chunkIndex = chunk.at<uint32_t>(ptr);
  1042. ptr += 4;
  1043. if (((chunkIndex + chunkLen) > totalLength) || (totalLength >= ZT_NETWORKCONFIG_DICT_CAPACITY)) { // >= since we need room for a null at the end
  1044. return 0;
  1045. }
  1046. if ((chunk[ptr] != 1) || (chunk.at<uint16_t>(ptr + 1) != ZT_ECC_SIGNATURE_LEN)) {
  1047. return 0;
  1048. }
  1049. const uint8_t* sig = reinterpret_cast<const uint8_t*>(chunk.field(ptr + 3, ZT_ECC_SIGNATURE_LEN));
  1050. // We can use the signature, which is unique per chunk, to get a per-chunk ID for local deduplication use
  1051. for (unsigned int i = 0; i < 16; ++i) {
  1052. reinterpret_cast<uint8_t*>(&chunkId)[i & 7] ^= sig[i];
  1053. }
  1054. // Find existing or new slot for this update and check if this is a duplicate chunk
  1055. for (int i = 0; i < ZT_NETWORK_MAX_INCOMING_UPDATES; ++i) {
  1056. if (_incomingConfigChunks[i].updateId == configUpdateId) {
  1057. c = &(_incomingConfigChunks[i]);
  1058. for (unsigned long j = 0; j < c->haveChunks; ++j) {
  1059. if (c->haveChunkIds[j] == chunkId) {
  1060. return 0;
  1061. }
  1062. }
  1063. break;
  1064. }
  1065. else if ((! c) || (_incomingConfigChunks[i].ts < c->ts)) {
  1066. c = &(_incomingConfigChunks[i]);
  1067. }
  1068. }
  1069. // If it's not a duplicate, check chunk signature
  1070. const Identity controllerId(RR->topology->getIdentity(tPtr, controller()));
  1071. if (! controllerId) { // we should always have the controller identity by now, otherwise how would we have queried it the first time?
  1072. return 0;
  1073. }
  1074. if (! controllerId.verify(chunk.field(start, ptr - start), ptr - start, sig, ZT_ECC_SIGNATURE_LEN)) {
  1075. return 0;
  1076. }
  1077. // New properly verified chunks can be flooded "virally" through the network
  1078. if (fastPropagate) {
  1079. Address* a = (Address*)0;
  1080. Membership* m = (Membership*)0;
  1081. Hashtable<Address, Membership>::Iterator i(_memberships);
  1082. while (i.next(a, m)) {
  1083. if ((*a != source) && (*a != controller())) {
  1084. Packet outp(*a, RR->identity.address(), Packet::VERB_NETWORK_CONFIG);
  1085. outp.append(reinterpret_cast<const uint8_t*>(chunk.data()) + start, chunk.size() - start);
  1086. RR->sw->send(tPtr, outp, true);
  1087. }
  1088. }
  1089. }
  1090. }
  1091. else if ((source == controller()) || (! source)) { // since old chunks aren't signed, only accept from controller itself (or via cluster backplane)
  1092. // Legacy support for OK(NETWORK_CONFIG_REQUEST) from older controllers
  1093. chunkId = packetId;
  1094. configUpdateId = chunkId;
  1095. totalLength = chunkLen;
  1096. chunkIndex = 0;
  1097. if (totalLength >= ZT_NETWORKCONFIG_DICT_CAPACITY) {
  1098. return 0;
  1099. }
  1100. for (int i = 0; i < ZT_NETWORK_MAX_INCOMING_UPDATES; ++i) {
  1101. if ((! c) || (_incomingConfigChunks[i].ts < c->ts)) {
  1102. c = &(_incomingConfigChunks[i]);
  1103. }
  1104. }
  1105. }
  1106. else {
  1107. // Single-chunk unsigned legacy configs are only allowed from the controller itself
  1108. return 0;
  1109. }
  1110. ++c->ts; // newer is higher, that's all we need
  1111. if (c->updateId != configUpdateId) {
  1112. c->updateId = configUpdateId;
  1113. c->haveChunks = 0;
  1114. c->haveBytes = 0;
  1115. }
  1116. if (c->haveChunks >= ZT_NETWORK_MAX_UPDATE_CHUNKS) {
  1117. return false;
  1118. }
  1119. c->haveChunkIds[c->haveChunks++] = chunkId;
  1120. memcpy(c->data.unsafeData() + chunkIndex, chunkData, chunkLen);
  1121. c->haveBytes += chunkLen;
  1122. if (c->haveBytes == totalLength) {
  1123. c->data.unsafeData()[c->haveBytes] = (char)0; // ensure null terminated
  1124. nc = new NetworkConfig();
  1125. try {
  1126. if (! nc->fromDictionary(c->data)) {
  1127. delete nc;
  1128. nc = (NetworkConfig*)0;
  1129. }
  1130. }
  1131. catch (...) {
  1132. delete nc;
  1133. nc = (NetworkConfig*)0;
  1134. }
  1135. }
  1136. }
  1137. if (nc) {
  1138. this->setConfiguration(tPtr, *nc, true);
  1139. delete nc;
  1140. return configUpdateId;
  1141. }
  1142. else {
  1143. return 0;
  1144. }
  1145. return 0;
  1146. }
  1147. int Network::setConfiguration(void* tPtr, const NetworkConfig& nconf, bool saveToDisk)
  1148. {
  1149. if (_destroyed) {
  1150. return 0;
  1151. }
  1152. // _lock is NOT locked when this is called
  1153. try {
  1154. if ((nconf.issuedTo != RR->identity.address()) || (nconf.networkId != _id)) {
  1155. return 0; // invalid config that is not for us or not for this network
  1156. }
  1157. if (_config == nconf) {
  1158. return 1; // OK config, but duplicate of what we already have
  1159. }
  1160. ZT_VirtualNetworkConfig ctmp;
  1161. bool oldPortInitialized;
  1162. { // do things that require lock here, but unlock before calling callbacks
  1163. Mutex::Lock _l(_lock);
  1164. _config = nconf;
  1165. _lastConfigUpdate = RR->node->now();
  1166. _netconfFailure = NETCONF_FAILURE_NONE;
  1167. oldPortInitialized = _portInitialized;
  1168. _portInitialized = true;
  1169. _externalConfig(&ctmp);
  1170. }
  1171. _portError = RR->node->configureVirtualNetworkPort(tPtr, _id, &_uPtr, (oldPortInitialized) ? ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP, &ctmp);
  1172. _authenticationURL = nconf.authenticationURL;
  1173. if (saveToDisk) {
  1174. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>* const d = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  1175. try {
  1176. if (nconf.toDictionary(*d, false)) {
  1177. uint64_t tmp[2];
  1178. tmp[0] = _id;
  1179. tmp[1] = 0;
  1180. RR->node->stateObjectPut(tPtr, ZT_STATE_OBJECT_NETWORK_CONFIG, tmp, d->data(), d->sizeBytes());
  1181. }
  1182. }
  1183. catch (...) {
  1184. }
  1185. delete d;
  1186. }
  1187. return 2; // OK and configuration has changed
  1188. }
  1189. catch (...) {
  1190. } // ignore invalid configs
  1191. return 0;
  1192. }
  1193. void Network::requestConfiguration(void* tPtr)
  1194. {
  1195. if (_destroyed) {
  1196. return;
  1197. }
  1198. if ((_id >> 56) == 0xff) {
  1199. if ((_id & 0xffffff) == 0) {
  1200. const uint16_t startPortRange = (uint16_t)((_id >> 40) & 0xffff);
  1201. const uint16_t endPortRange = (uint16_t)((_id >> 24) & 0xffff);
  1202. if (endPortRange >= startPortRange) {
  1203. NetworkConfig* const nconf = new NetworkConfig();
  1204. nconf->networkId = _id;
  1205. nconf->timestamp = RR->node->now();
  1206. nconf->credentialTimeMaxDelta = ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA;
  1207. nconf->revision = 1;
  1208. nconf->issuedTo = RR->identity.address();
  1209. nconf->flags = ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION;
  1210. nconf->mtu = ZT_DEFAULT_MTU;
  1211. nconf->multicastLimit = 0;
  1212. nconf->staticIpCount = 1;
  1213. nconf->ruleCount = 14;
  1214. nconf->staticIps[0] = InetAddress::makeIpv66plane(_id, RR->identity.address().toInt());
  1215. // Drop everything but IPv6
  1216. nconf->rules[0].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE | 0x80; // NOT
  1217. nconf->rules[0].v.etherType = 0x86dd; // IPv6
  1218. nconf->rules[1].t = (uint8_t)ZT_NETWORK_RULE_ACTION_DROP;
  1219. // Allow ICMPv6
  1220. nconf->rules[2].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_PROTOCOL;
  1221. nconf->rules[2].v.ipProtocol = 0x3a; // ICMPv6
  1222. nconf->rules[3].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1223. // Allow destination ports within range
  1224. nconf->rules[4].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_PROTOCOL;
  1225. nconf->rules[4].v.ipProtocol = 0x11; // UDP
  1226. nconf->rules[5].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_PROTOCOL | 0x40; // OR
  1227. nconf->rules[5].v.ipProtocol = 0x06; // TCP
  1228. nconf->rules[6].t = (uint8_t)ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE;
  1229. nconf->rules[6].v.port[0] = startPortRange;
  1230. nconf->rules[6].v.port[1] = endPortRange;
  1231. nconf->rules[7].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1232. // Allow non-SYN TCP packets to permit non-connection-initiating traffic
  1233. nconf->rules[8].t = (uint8_t)ZT_NETWORK_RULE_MATCH_CHARACTERISTICS | 0x80; // NOT
  1234. nconf->rules[8].v.characteristics = ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN;
  1235. nconf->rules[9].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1236. // Also allow SYN+ACK which are replies to SYN
  1237. nconf->rules[10].t = (uint8_t)ZT_NETWORK_RULE_MATCH_CHARACTERISTICS;
  1238. nconf->rules[10].v.characteristics = ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN;
  1239. nconf->rules[11].t = (uint8_t)ZT_NETWORK_RULE_MATCH_CHARACTERISTICS;
  1240. nconf->rules[11].v.characteristics = ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK;
  1241. nconf->rules[12].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1242. nconf->rules[13].t = (uint8_t)ZT_NETWORK_RULE_ACTION_DROP;
  1243. nconf->type = ZT_NETWORK_TYPE_PUBLIC;
  1244. nconf->name[0] = 'a';
  1245. nconf->name[1] = 'd';
  1246. nconf->name[2] = 'h';
  1247. nconf->name[3] = 'o';
  1248. nconf->name[4] = 'c';
  1249. nconf->name[5] = '-';
  1250. Utils::hex((uint16_t)startPortRange, nconf->name + 6);
  1251. nconf->name[10] = '-';
  1252. Utils::hex((uint16_t)endPortRange, nconf->name + 11);
  1253. nconf->name[15] = (char)0;
  1254. this->setConfiguration(tPtr, *nconf, false);
  1255. delete nconf;
  1256. }
  1257. else {
  1258. this->setNotFound(tPtr);
  1259. }
  1260. }
  1261. else if ((_id & 0xff) == 0x01) {
  1262. // ffAAaaaaaaaaaa01 -- where AA is the IPv4 /8 to use and aaaaaaaaaa is the anchor node for multicast gather and replication
  1263. const uint64_t myAddress = RR->identity.address().toInt();
  1264. const uint64_t networkHub = (_id >> 8) & 0xffffffffffULL;
  1265. uint8_t ipv4[4];
  1266. ipv4[0] = (uint8_t)((_id >> 48) & 0xff);
  1267. ipv4[1] = (uint8_t)((myAddress >> 16) & 0xff);
  1268. ipv4[2] = (uint8_t)((myAddress >> 8) & 0xff);
  1269. ipv4[3] = (uint8_t)(myAddress & 0xff);
  1270. char v4ascii[24];
  1271. Utils::decimal(ipv4[0], v4ascii);
  1272. NetworkConfig* const nconf = new NetworkConfig();
  1273. nconf->networkId = _id;
  1274. nconf->timestamp = RR->node->now();
  1275. nconf->credentialTimeMaxDelta = ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA;
  1276. nconf->revision = 1;
  1277. nconf->issuedTo = RR->identity.address();
  1278. nconf->flags = ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION;
  1279. nconf->mtu = ZT_DEFAULT_MTU;
  1280. nconf->multicastLimit = 1024;
  1281. nconf->specialistCount = (networkHub == 0) ? 0 : 1;
  1282. nconf->staticIpCount = 2;
  1283. nconf->ruleCount = 1;
  1284. if (networkHub != 0) {
  1285. nconf->specialists[0] = networkHub;
  1286. }
  1287. nconf->staticIps[0] = InetAddress::makeIpv66plane(_id, myAddress);
  1288. nconf->staticIps[1].set(ipv4, 4, 8);
  1289. nconf->rules[0].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  1290. nconf->type = ZT_NETWORK_TYPE_PUBLIC;
  1291. nconf->name[0] = 'a';
  1292. nconf->name[1] = 'd';
  1293. nconf->name[2] = 'h';
  1294. nconf->name[3] = 'o';
  1295. nconf->name[4] = 'c';
  1296. nconf->name[5] = '-';
  1297. unsigned long nn = 6;
  1298. while ((nconf->name[nn] = v4ascii[nn - 6])) {
  1299. ++nn;
  1300. }
  1301. nconf->name[nn++] = '.';
  1302. nconf->name[nn++] = '0';
  1303. nconf->name[nn++] = '.';
  1304. nconf->name[nn++] = '0';
  1305. nconf->name[nn++] = '.';
  1306. nconf->name[nn++] = '0';
  1307. nconf->name[nn++] = (char)0;
  1308. this->setConfiguration(tPtr, *nconf, false);
  1309. delete nconf;
  1310. }
  1311. return;
  1312. }
  1313. const Address ctrl(controller());
  1314. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> rmd;
  1315. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION, (uint64_t)ZT_NETWORKCONFIG_VERSION);
  1316. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_VENDOR, (uint64_t)ZT_VENDOR_ZEROTIER);
  1317. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION, (uint64_t)ZT_PROTO_VERSION);
  1318. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION, (uint64_t)ZEROTIER_ONE_VERSION_MAJOR);
  1319. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION, (uint64_t)ZEROTIER_ONE_VERSION_MINOR);
  1320. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION, (uint64_t)ZEROTIER_ONE_VERSION_REVISION);
  1321. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_RULES, (uint64_t)ZT_MAX_NETWORK_RULES);
  1322. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_CAPABILITIES, (uint64_t)ZT_MAX_NETWORK_CAPABILITIES);
  1323. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_CAPABILITY_RULES, (uint64_t)ZT_MAX_CAPABILITY_RULES);
  1324. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_TAGS, (uint64_t)ZT_MAX_NETWORK_TAGS);
  1325. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_FLAGS, (uint64_t)0);
  1326. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_RULES_ENGINE_REV, (uint64_t)ZT_RULES_ENGINE_REVISION);
  1327. rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_OS_ARCH, ZT_TARGET_NAME);
  1328. RR->t->networkConfigRequestSent(tPtr, *this, ctrl);
  1329. if (ctrl == RR->identity.address()) {
  1330. if (RR->localNetworkController) {
  1331. RR->localNetworkController->request(_id, InetAddress(), 0xffffffffffffffffULL, RR->identity, rmd);
  1332. }
  1333. else {
  1334. this->setNotFound(tPtr);
  1335. }
  1336. return;
  1337. }
  1338. Packet outp(ctrl, RR->identity.address(), Packet::VERB_NETWORK_CONFIG_REQUEST);
  1339. outp.append((uint64_t)_id);
  1340. const unsigned int rmdSize = rmd.sizeBytes();
  1341. outp.append((uint16_t)rmdSize);
  1342. outp.append((const void*)rmd.data(), rmdSize);
  1343. if (_config) {
  1344. outp.append((uint64_t)_config.revision);
  1345. outp.append((uint64_t)_config.timestamp);
  1346. }
  1347. else {
  1348. outp.append((unsigned char)0, 16);
  1349. }
  1350. outp.compress();
  1351. RR->node->expectReplyTo(outp.packetId());
  1352. RR->sw->send(tPtr, outp, true);
  1353. }
  1354. bool Network::gate(void* tPtr, const SharedPtr<Peer>& peer)
  1355. {
  1356. const int64_t now = RR->node->now();
  1357. // int64_t comTimestamp = 0;
  1358. // int64_t comRevocationThreshold = 0;
  1359. Mutex::Lock _l(_lock);
  1360. try {
  1361. if (_config) {
  1362. Membership* m = _memberships.get(peer->address());
  1363. // if (m) {
  1364. // comTimestamp = m->comTimestamp();
  1365. // comRevocationThreshold = m->comRevocationThreshold();
  1366. // }
  1367. if ((_config.isPublic()) || ((m) && (m->isAllowedOnNetwork(_config, peer->identity())))) {
  1368. if (! m) {
  1369. m = &(_membership(peer->address()));
  1370. }
  1371. if (m->multicastLikeGate(now)) {
  1372. _announceMulticastGroupsTo(tPtr, peer->address(), _allMulticastGroups());
  1373. }
  1374. return true;
  1375. }
  1376. }
  1377. }
  1378. catch (...) {
  1379. }
  1380. // printf("%.16llx %.10llx not allowed, COM ts %lld revocation %lld\n", _id, peer->address().toInt(), comTimestamp, comRevocationThreshold); fflush(stdout);
  1381. return false;
  1382. }
  1383. bool Network::recentlyAssociatedWith(const Address& addr)
  1384. {
  1385. Mutex::Lock _l(_lock);
  1386. const Membership* m = _memberships.get(addr);
  1387. return ((m) && (m->recentlyAssociated(RR->node->now())));
  1388. }
  1389. void Network::clean()
  1390. {
  1391. const int64_t now = RR->node->now();
  1392. Mutex::Lock _l(_lock);
  1393. if (_destroyed) {
  1394. return;
  1395. }
  1396. {
  1397. Hashtable<MulticastGroup, uint64_t>::Iterator i(_multicastGroupsBehindMe);
  1398. MulticastGroup* mg = (MulticastGroup*)0;
  1399. uint64_t* ts = (uint64_t*)0;
  1400. while (i.next(mg, ts)) {
  1401. if ((now - *ts) > (ZT_MULTICAST_LIKE_EXPIRE * 2)) {
  1402. _multicastGroupsBehindMe.erase(*mg);
  1403. }
  1404. }
  1405. }
  1406. {
  1407. Address* a = (Address*)0;
  1408. Membership* m = (Membership*)0;
  1409. Hashtable<Address, Membership>::Iterator i(_memberships);
  1410. while (i.next(a, m)) {
  1411. if (! RR->topology->getPeerNoCache(*a)) {
  1412. _memberships.erase(*a);
  1413. }
  1414. else {
  1415. m->clean(now, _config);
  1416. }
  1417. }
  1418. }
  1419. }
  1420. void Network::learnBridgeRoute(const MAC& mac, const Address& addr)
  1421. {
  1422. Mutex::Lock _l(_lock);
  1423. _remoteBridgeRoutes[mac] = addr;
  1424. // Anti-DOS circuit breaker to prevent nodes from spamming us with absurd numbers of bridge routes
  1425. while (_remoteBridgeRoutes.size() > ZT_MAX_BRIDGE_ROUTES) {
  1426. Hashtable<Address, unsigned long> counts;
  1427. Address maxAddr;
  1428. unsigned long maxCount = 0;
  1429. MAC* m = (MAC*)0;
  1430. Address* a = (Address*)0;
  1431. // Find the address responsible for the most entries
  1432. {
  1433. Hashtable<MAC, Address>::Iterator i(_remoteBridgeRoutes);
  1434. while (i.next(m, a)) {
  1435. const unsigned long c = ++counts[*a];
  1436. if (c > maxCount) {
  1437. maxCount = c;
  1438. maxAddr = *a;
  1439. }
  1440. }
  1441. }
  1442. // Kill this address from our table, since it's most likely spamming us
  1443. {
  1444. Hashtable<MAC, Address>::Iterator i(_remoteBridgeRoutes);
  1445. while (i.next(m, a)) {
  1446. if (*a == maxAddr) {
  1447. _remoteBridgeRoutes.erase(*m);
  1448. }
  1449. }
  1450. }
  1451. }
  1452. }
  1453. void Network::learnBridgedMulticastGroup(void* tPtr, const MulticastGroup& mg, int64_t now)
  1454. {
  1455. Mutex::Lock _l(_lock);
  1456. const unsigned long tmp = (unsigned long)_multicastGroupsBehindMe.size();
  1457. _multicastGroupsBehindMe.set(mg, now);
  1458. if (tmp != _multicastGroupsBehindMe.size()) {
  1459. _sendUpdatesToMembers(tPtr, &mg);
  1460. }
  1461. }
  1462. Membership::AddCredentialResult Network::addCredential(void* tPtr, const CertificateOfMembership& com)
  1463. {
  1464. if (com.networkId() != _id) {
  1465. return Membership::ADD_REJECTED;
  1466. }
  1467. Mutex::Lock _l(_lock);
  1468. return _membership(com.issuedTo()).addCredential(RR, tPtr, _config, com);
  1469. }
  1470. Membership::AddCredentialResult Network::addCredential(void* tPtr, const Address& sentFrom, const Revocation& rev)
  1471. {
  1472. if (rev.networkId() != _id) {
  1473. return Membership::ADD_REJECTED;
  1474. }
  1475. Mutex::Lock _l(_lock);
  1476. Membership& m = _membership(rev.target());
  1477. const Membership::AddCredentialResult result = m.addCredential(RR, tPtr, _config, rev);
  1478. if ((result == Membership::ADD_ACCEPTED_NEW) && (rev.fastPropagate())) {
  1479. Address* a = (Address*)0;
  1480. Membership* m = (Membership*)0;
  1481. Hashtable<Address, Membership>::Iterator i(_memberships);
  1482. while (i.next(a, m)) {
  1483. if ((*a != sentFrom) && (*a != rev.signer())) {
  1484. Packet outp(*a, RR->identity.address(), Packet::VERB_NETWORK_CREDENTIALS);
  1485. outp.append((uint8_t)0x00); // no COM
  1486. outp.append((uint16_t)0); // no capabilities
  1487. outp.append((uint16_t)0); // no tags
  1488. outp.append((uint16_t)1); // one revocation!
  1489. rev.serialize(outp);
  1490. outp.append((uint16_t)0); // no certificates of ownership
  1491. RR->sw->send(tPtr, outp, true);
  1492. }
  1493. }
  1494. }
  1495. return result;
  1496. }
  1497. void Network::destroy()
  1498. {
  1499. Mutex::Lock _l(_lock);
  1500. _destroyed = true;
  1501. }
  1502. ZT_VirtualNetworkStatus Network::_status() const
  1503. {
  1504. // assumes _lock is locked
  1505. if (_portError) {
  1506. return ZT_NETWORK_STATUS_PORT_ERROR;
  1507. }
  1508. switch (_netconfFailure) {
  1509. case NETCONF_FAILURE_ACCESS_DENIED:
  1510. return ZT_NETWORK_STATUS_ACCESS_DENIED;
  1511. case NETCONF_FAILURE_NOT_FOUND:
  1512. return ZT_NETWORK_STATUS_NOT_FOUND;
  1513. case NETCONF_FAILURE_NONE:
  1514. return ((_config) ? ZT_NETWORK_STATUS_OK : ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION);
  1515. case NETCONF_FAILURE_AUTHENTICATION_REQUIRED:
  1516. return ZT_NETWORK_STATUS_AUTHENTICATION_REQUIRED;
  1517. default:
  1518. return ZT_NETWORK_STATUS_PORT_ERROR;
  1519. }
  1520. }
  1521. void Network::_externalConfig(ZT_VirtualNetworkConfig* ec) const
  1522. {
  1523. // assumes _lock is locked
  1524. ec->nwid = _id;
  1525. ec->mac = _mac.toInt();
  1526. if (_config) {
  1527. Utils::scopy(ec->name, sizeof(ec->name), _config.name);
  1528. }
  1529. else {
  1530. ec->name[0] = (char)0;
  1531. }
  1532. ec->status = _status();
  1533. ec->type = (_config) ? (_config.isPrivate() ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC) : ZT_NETWORK_TYPE_PRIVATE;
  1534. ec->mtu = (_config) ? _config.mtu : ZT_DEFAULT_MTU;
  1535. ec->dhcp = 0;
  1536. std::vector<Address> ab(_config.activeBridges());
  1537. ec->bridge = (std::find(ab.begin(), ab.end(), RR->identity.address()) != ab.end()) ? 1 : 0;
  1538. ec->broadcastEnabled = (_config) ? (_config.enableBroadcast() ? 1 : 0) : 0;
  1539. ec->portError = _portError;
  1540. ec->netconfRevision = (_config) ? (unsigned long)_config.revision : 0;
  1541. ec->assignedAddressCount = 0;
  1542. for (unsigned int i = 0; i < ZT_MAX_ZT_ASSIGNED_ADDRESSES; ++i) {
  1543. if (i < _config.staticIpCount) {
  1544. memcpy(&(ec->assignedAddresses[i]), &(_config.staticIps[i]), sizeof(struct sockaddr_storage));
  1545. ++ec->assignedAddressCount;
  1546. }
  1547. else {
  1548. memset(&(ec->assignedAddresses[i]), 0, sizeof(struct sockaddr_storage));
  1549. }
  1550. }
  1551. ec->routeCount = 0;
  1552. for (unsigned int i = 0; i < ZT_MAX_NETWORK_ROUTES; ++i) {
  1553. if (i < _config.routeCount) {
  1554. memcpy(&(ec->routes[i]), &(_config.routes[i]), sizeof(ZT_VirtualNetworkRoute));
  1555. ++ec->routeCount;
  1556. }
  1557. else {
  1558. memset(&(ec->routes[i]), 0, sizeof(ZT_VirtualNetworkRoute));
  1559. }
  1560. }
  1561. ec->multicastSubscriptionCount = (unsigned int)_myMulticastGroups.size();
  1562. for (unsigned long i = 0; i < (unsigned long)_myMulticastGroups.size(); ++i) {
  1563. ec->multicastSubscriptions[i].mac = _myMulticastGroups[i].mac().toInt();
  1564. ec->multicastSubscriptions[i].adi = _myMulticastGroups[i].adi();
  1565. }
  1566. memcpy(&ec->dns, &_config.dns, sizeof(ZT_VirtualNetworkDNS));
  1567. Utils::scopy(ec->authenticationURL, sizeof(ec->authenticationURL), _authenticationURL.c_str());
  1568. ec->ssoVersion = _config.ssoVersion;
  1569. ec->authenticationExpiryTime = _config.authenticationExpiryTime;
  1570. ec->ssoEnabled = _config.ssoEnabled;
  1571. Utils::scopy(ec->centralAuthURL, sizeof(ec->centralAuthURL), _config.centralAuthURL);
  1572. Utils::scopy(ec->issuerURL, sizeof(ec->issuerURL), _config.issuerURL);
  1573. Utils::scopy(ec->ssoNonce, sizeof(ec->ssoNonce), _config.ssoNonce);
  1574. Utils::scopy(ec->ssoState, sizeof(ec->ssoState), _config.ssoState);
  1575. Utils::scopy(ec->ssoClientID, sizeof(ec->ssoClientID), _config.ssoClientID);
  1576. Utils::scopy(ec->ssoProvider, sizeof(ec->ssoProvider), _config.ssoProvider);
  1577. }
  1578. void Network::_sendUpdatesToMembers(void* tPtr, const MulticastGroup* const newMulticastGroup)
  1579. {
  1580. // Assumes _lock is locked
  1581. const int64_t now = RR->node->now();
  1582. std::vector<MulticastGroup> groups;
  1583. if (newMulticastGroup) {
  1584. groups.push_back(*newMulticastGroup);
  1585. }
  1586. else {
  1587. groups = _allMulticastGroups();
  1588. }
  1589. std::vector<Address> alwaysAnnounceTo;
  1590. if ((newMulticastGroup) || ((now - _lastAnnouncedMulticastGroupsUpstream) >= ZT_MULTICAST_ANNOUNCE_PERIOD)) {
  1591. if (! newMulticastGroup) {
  1592. _lastAnnouncedMulticastGroupsUpstream = now;
  1593. }
  1594. alwaysAnnounceTo = _config.alwaysContactAddresses();
  1595. if (std::find(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end(), controller()) == alwaysAnnounceTo.end()) {
  1596. alwaysAnnounceTo.push_back(controller());
  1597. }
  1598. const std::vector<Address> upstreams(RR->topology->upstreamAddresses());
  1599. for (std::vector<Address>::const_iterator a(upstreams.begin()); a != upstreams.end(); ++a) {
  1600. if (std::find(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end(), *a) == alwaysAnnounceTo.end()) {
  1601. alwaysAnnounceTo.push_back(*a);
  1602. }
  1603. }
  1604. std::sort(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end());
  1605. for (std::vector<Address>::const_iterator a(alwaysAnnounceTo.begin()); a != alwaysAnnounceTo.end(); ++a) {
  1606. /*
  1607. // push COM to non-members so they can do multicast request auth
  1608. if ( (_config.com) && (!_memberships.contains(*a)) && (*a != RR->identity.address()) ) {
  1609. Packet outp(*a,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  1610. _config.com.serialize(outp);
  1611. outp.append((uint8_t)0x00);
  1612. outp.append((uint16_t)0); // no capabilities
  1613. outp.append((uint16_t)0); // no tags
  1614. outp.append((uint16_t)0); // no revocations
  1615. outp.append((uint16_t)0); // no certificates of ownership
  1616. RR->sw->send(tPtr,outp,true);
  1617. }
  1618. */
  1619. _announceMulticastGroupsTo(tPtr, *a, groups);
  1620. }
  1621. }
  1622. {
  1623. Address* a = (Address*)0;
  1624. Membership* m = (Membership*)0;
  1625. Hashtable<Address, Membership>::Iterator i(_memberships);
  1626. while (i.next(a, m)) {
  1627. const Identity remoteIdentity(RR->topology->getIdentity(tPtr, *a));
  1628. if (remoteIdentity) {
  1629. if ((m->multicastLikeGate(now) || (newMulticastGroup)) && (m->isAllowedOnNetwork(_config, remoteIdentity)) && (! std::binary_search(alwaysAnnounceTo.begin(), alwaysAnnounceTo.end(), *a))) {
  1630. _announceMulticastGroupsTo(tPtr, *a, groups);
  1631. }
  1632. }
  1633. }
  1634. }
  1635. }
  1636. void Network::_announceMulticastGroupsTo(void* tPtr, const Address& peer, const std::vector<MulticastGroup>& allMulticastGroups)
  1637. {
  1638. // Assumes _lock is locked
  1639. Packet* const outp = new Packet(peer, RR->identity.address(), Packet::VERB_MULTICAST_LIKE);
  1640. for (std::vector<MulticastGroup>::const_iterator mg(allMulticastGroups.begin()); mg != allMulticastGroups.end(); ++mg) {
  1641. if ((outp->size() + 24) >= ZT_PROTO_MAX_PACKET_LENGTH) {
  1642. outp->compress();
  1643. RR->sw->send(tPtr, *outp, true);
  1644. outp->reset(peer, RR->identity.address(), Packet::VERB_MULTICAST_LIKE);
  1645. }
  1646. // network ID, MAC, ADI
  1647. outp->append((uint64_t)_id);
  1648. mg->mac().appendTo(*outp);
  1649. outp->append((uint32_t)mg->adi());
  1650. }
  1651. if (outp->size() > ZT_PROTO_MIN_PACKET_LENGTH) {
  1652. outp->compress();
  1653. RR->sw->send(tPtr, *outp, true);
  1654. }
  1655. delete outp;
  1656. }
  1657. std::vector<MulticastGroup> Network::_allMulticastGroups() const
  1658. {
  1659. // Assumes _lock is locked
  1660. std::vector<MulticastGroup> mgs;
  1661. mgs.reserve(_myMulticastGroups.size() + _multicastGroupsBehindMe.size() + 1);
  1662. mgs.insert(mgs.end(), _myMulticastGroups.begin(), _myMulticastGroups.end());
  1663. _multicastGroupsBehindMe.appendKeys(mgs);
  1664. if ((_config) && (_config.enableBroadcast())) {
  1665. mgs.push_back(Network::BROADCAST);
  1666. }
  1667. std::sort(mgs.begin(), mgs.end());
  1668. mgs.erase(std::unique(mgs.begin(), mgs.end()), mgs.end());
  1669. return mgs;
  1670. }
  1671. Membership& Network::_membership(const Address& a)
  1672. {
  1673. // assumes _lock is locked
  1674. return _memberships[a];
  1675. }
  1676. void Network::setAuthenticationRequired(void* tPtr, const char* issuerURL, const char* centralEndpoint, const char* clientID, const char* ssoProvider, const char* nonce, const char* state)
  1677. {
  1678. Mutex::Lock _l(_lock);
  1679. _netconfFailure = NETCONF_FAILURE_AUTHENTICATION_REQUIRED;
  1680. _config.ssoEnabled = true;
  1681. _config.ssoVersion = 1;
  1682. Utils::scopy(_config.issuerURL, sizeof(_config.issuerURL), issuerURL);
  1683. Utils::scopy(_config.centralAuthURL, sizeof(_config.centralAuthURL), centralEndpoint);
  1684. Utils::scopy(_config.ssoClientID, sizeof(_config.ssoClientID), clientID);
  1685. Utils::scopy(_config.ssoNonce, sizeof(_config.ssoNonce), nonce);
  1686. Utils::scopy(_config.ssoState, sizeof(_config.ssoState), state);
  1687. Utils::scopy(_config.ssoProvider, sizeof(_config.ssoProvider), ssoProvider);
  1688. _sendUpdateEvent(tPtr);
  1689. }
  1690. void Network::_sendUpdateEvent(void* tPtr)
  1691. {
  1692. ZT_VirtualNetworkConfig ctmp;
  1693. _externalConfig(&ctmp);
  1694. RR->node->configureVirtualNetworkPort(tPtr, _id, &_uPtr, (_portInitialized) ? ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP, &ctmp);
  1695. }
  1696. } // namespace ZeroTier