2
0

Network.cpp 61 KB

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