1
0

InterfaceController.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/AddressCalc.h"
  16. #include "crypto/Ca.h"
  17. #include "interface/Iface.h"
  18. #include "net/InterfaceController.h"
  19. #include "memory/Allocator.h"
  20. #include "net/SwitchPinger.h"
  21. #include "wire/PFChan.h"
  22. #include "net/EventEmitter.h"
  23. #include "util/Base32.h"
  24. #include "util/Bits.h"
  25. #include "util/events/Time.h"
  26. #include "util/events/Timeout.h"
  27. #include "util/Identity.h"
  28. #include "util/version/Version.h"
  29. #include "util/AddrTools.h"
  30. #include "util/Defined.h"
  31. #include "util/Checksum.h"
  32. #include "util/Hex.h"
  33. #include "util/Kbps.h"
  34. #include "wire/Error.h"
  35. #include "wire/Message.h"
  36. #include "wire/Headers.h"
  37. #include "wire/Metric.h"
  38. #include "wire/CryptoHeader.h"
  39. /** After this number of milliseconds, a node will be regarded as unresponsive. */
  40. #define UNRESPONSIVE_AFTER_MILLISECONDS (20*1024)
  41. /**
  42. * After this number of milliseconds without a valid incoming message,
  43. * a peer is "lazy" and should be pinged.
  44. */
  45. #define PING_AFTER_MILLISECONDS (3*1024)
  46. /** How often to ping "lazy" peers, "unresponsive" peers are only pinged 20% of the time. */
  47. #define PING_INTERVAL_MILLISECONDS 1024
  48. /** The number of milliseconds to wait for a ping response. */
  49. #define TIMEOUT_MILLISECONDS (2*1024)
  50. /**
  51. * The number of seconds to wait before an unresponsive peer
  52. * making an incoming connection is forgotten.
  53. */
  54. #define FORGET_AFTER_MILLISECONDS (256*1024)
  55. /** Wait 32 seconds between sending beacon messages. */
  56. #define BEACON_INTERVAL 32768
  57. /** Every 3 seconds inform the pathfinder of the current link states. */
  58. #define LINKSTATE_UPDATE_INTERVAL 3000
  59. // Recommended by boringtun documentation
  60. #define HANDSHAKE_CYCLE_INTERVAL 100
  61. // ---------------- Map ----------------
  62. #define Map_NAME EndpointsBySockaddr
  63. #define Map_ENABLE_HANDLES
  64. #define Map_KEY_TYPE struct Sockaddr*
  65. #define Map_VALUE_TYPE struct Peer*
  66. #define Map_USE_HASH
  67. #define Map_USE_COMPARATOR
  68. #include "util/Map.h"
  69. static inline uint32_t Map_EndpointsBySockaddr_hash(struct Sockaddr** key)
  70. {
  71. return Sockaddr_hash(*key);
  72. }
  73. static inline int Map_EndpointsBySockaddr_compare(struct Sockaddr** keyA, struct Sockaddr** keyB)
  74. {
  75. return Sockaddr_compare(*keyA, *keyB);
  76. }
  77. // ---------------- EndMap ----------------
  78. #define ArrayList_TYPE struct InterfaceController_Iface_pvt
  79. #define ArrayList_NAME OfIfaces
  80. #include "util/ArrayList.h"
  81. struct InterfaceController_pvt;
  82. struct InterfaceController_Iface_pvt
  83. {
  84. struct InterfaceController_Iface pub;
  85. struct Map_EndpointsBySockaddr peerMap;
  86. /** The number of the next peer to try pinging, this iterates through the list of peers. */
  87. uint32_t lastPeerPinged;
  88. struct InterfaceController_pvt* ic;
  89. struct Allocator* alloc;
  90. Identity
  91. };
  92. struct Peer
  93. {
  94. /** The interface which is registered with the switch. */
  95. struct Iface switchIf;
  96. struct Iface plaintext;
  97. struct Iface ciphertext;
  98. struct Allocator* alloc;
  99. Ca_Session_t* caSession;
  100. struct Kbps sendBw;
  101. struct Kbps recvBw;
  102. /** The interface which this peer belongs to. */
  103. struct InterfaceController_Iface_pvt* ici;
  104. /** The address within the interface of this peer. */
  105. struct Sockaddr* lladdr;
  106. struct Address addr;
  107. /** Milliseconds since the epoch when the last *valid* message was received. */
  108. uint64_t timeOfLastMessage;
  109. /** Time when the last switch ping response was received from this node. */
  110. uint64_t timeOfLastPing;
  111. /** A counter to allow for 3/4 of all pings to be skipped when a node is definitely down. */
  112. uint32_t pingCount;
  113. /** The handle which can be used to look up this endpoint in the endpoint set. */
  114. uint32_t handle;
  115. /** True if we should forget about the peer if they do not respond. */
  116. bool isIncomingConnection;
  117. /**
  118. * If InterfaceController_PeerState_UNAUTHENTICATED, no permanent state will be kept.
  119. * During transition from HANDSHAKE to ESTABLISHED, a check is done for a registeration of a
  120. * node which is already registered in a different switch slot, if there is one and the
  121. * handshake completes, it will be moved.
  122. */
  123. enum InterfaceController_PeerState state;
  124. /**
  125. * The number of lost packets last time we checked.
  126. * _lastDrops and _lastPackets are the direct readings off of the ReplayProtector
  127. * so they will be reset to zero when the session resets. lastDrops and lastPackets
  128. * are monotonic and so probably what you want.
  129. */
  130. uint64_t _lastDrops;
  131. uint64_t _lastPackets;
  132. uint64_t lastDrops;
  133. uint64_t lastPackets;
  134. // traffic counters
  135. uint64_t bytesOut;
  136. uint64_t bytesIn;
  137. Identity
  138. };
  139. struct InterfaceController_pvt
  140. {
  141. /** Public functions and fields for this ifcontroller. */
  142. struct InterfaceController pub;
  143. struct Allocator* const alloc;
  144. Ca_t* const ca;
  145. /** Switch for adding nodes when they are discovered. */
  146. struct SwitchCore* const switchCore;
  147. struct Random* const rand;
  148. struct Log* const logger;
  149. struct EventBase* const eventBase;
  150. /** For communicating with the Pathfinder. */
  151. struct Iface eventEmitterIf;
  152. /** After this number of milliseconds, a neoghbor will be regarded as unresponsive. */
  153. uint32_t unresponsiveAfterMilliseconds;
  154. /** The number of milliseconds to wait before pinging. */
  155. uint32_t pingAfterMilliseconds;
  156. /** The number of milliseconds to let a ping go before timing it out. */
  157. uint32_t timeoutMilliseconds;
  158. /** After this number of milliseconds, an incoming connection is forgotten entirely. */
  159. uint32_t forgetAfterMilliseconds;
  160. /** How often to send beacon messages (milliseconds). */
  161. uint32_t beaconInterval;
  162. // Whether or not to create sessions using the noise protocol
  163. const bool enableNoise;
  164. /** The timeout event to use for pinging potentially unresponsive neighbors. */
  165. struct Timeout* const pingInterval;
  166. /** The timeout event for updating the link state to the pathfinders. */
  167. struct Timeout* const linkStateInterval;
  168. // Timeout for noise re-handshakes
  169. struct Timeout* const noiseHandshakeInterval;
  170. /** For pinging lazy/unresponsive nodes. */
  171. struct SwitchPinger* const switchPinger;
  172. struct ArrayList_OfIfaces* icis;
  173. /** Temporary allocator for allocating timeouts for sending beacon messages. */
  174. struct Allocator* beaconTimeoutAlloc;
  175. /** A password which is generated per-startup and sent out in beacon messages. */
  176. uint8_t beaconPassword[Headers_Beacon_PASSWORD_LEN];
  177. struct Headers_Beacon beacon;
  178. uint8_t ourPubKey[32];
  179. Identity
  180. };
  181. static bool knownIncompatibleVersion(uint32_t version)
  182. {
  183. if (!version) {
  184. return false;
  185. } else if (Defined(SUBNODE) && version < 21) {
  186. // Subnode doesn't talk to peers with less than v21
  187. return true;
  188. }
  189. return !Version_isCompatible(version, Version_CURRENT_PROTOCOL);
  190. }
  191. static void sendPeer(uint32_t pathfinderId,
  192. enum PFChan_Core ev,
  193. struct Peer* peer,
  194. uint16_t latency)
  195. {
  196. if (!peer->addr.protocolVersion || knownIncompatibleVersion(peer->addr.protocolVersion)) {
  197. // Don't know the protocol version, never add them
  198. return;
  199. }
  200. struct InterfaceController_pvt* ic = Identity_check(peer->ici->ic);
  201. struct Allocator* alloc = Allocator_child(ic->alloc);
  202. struct Message* msg = Message_new(PFChan_Node_SIZE, 512, alloc);
  203. struct PFChan_Node* node = (struct PFChan_Node*) msg->msgbytes;
  204. Bits_memcpy(node->ip6, peer->addr.ip6.bytes, 16);
  205. Bits_memcpy(node->publicKey, peer->addr.key, 32);
  206. node->path_be = Endian_hostToBigEndian64(peer->addr.path);
  207. node->version_be = Endian_hostToBigEndian32(peer->addr.protocolVersion);
  208. if (ev != PFChan_Core_PEER_GONE) {
  209. Assert_true(peer->addr.protocolVersion);
  210. node->metric_be =
  211. Endian_hostToBigEndian32(Metric_IC_PEER | (latency & Metric_IC_PEER_MASK));
  212. } else {
  213. node->metric_be = Endian_hostToBigEndian32(Metric_DEAD_LINK);
  214. }
  215. Er_assert(Message_epush32be(msg, pathfinderId));
  216. Er_assert(Message_epush32be(msg, ev));
  217. Iface_send(&ic->eventEmitterIf, msg);
  218. Allocator_free(alloc);
  219. }
  220. static void onPingResponse(struct SwitchPinger_Response* resp, void* onResponseContext)
  221. {
  222. if (SwitchPinger_Result_OK != resp->res) {
  223. return;
  224. }
  225. struct Peer* ep = Identity_check((struct Peer*) onResponseContext);
  226. struct InterfaceController_pvt* ic = Identity_check(ep->ici->ic);
  227. ep->addr.protocolVersion = resp->version;
  228. if (Defined(Log_DEBUG)) {
  229. String* addr = Address_toString(&ep->addr, resp->ping->pingAlloc);
  230. if (knownIncompatibleVersion(resp->version)) {
  231. Log_debug(ic->logger, "got switch pong from node [%s] with incompatible version",
  232. addr->bytes);
  233. } else if (ep->addr.path != resp->label) {
  234. uint8_t sl[20];
  235. AddrTools_printPath(sl, resp->label);
  236. Log_debug(ic->logger, "got switch pong from node [%s] mismatch label [%s]",
  237. addr->bytes, sl);
  238. } else {
  239. Log_debug(ic->logger, "got switch pong from node [%s]", addr->bytes);
  240. }
  241. }
  242. if (knownIncompatibleVersion(resp->version) || ep->addr.path != resp->label) {
  243. ep->state = InterfaceController_PeerState_INCOMPATIBLE;
  244. return;
  245. }
  246. if (ep->state == InterfaceController_PeerState_ESTABLISHED) {
  247. sendPeer(0xffffffff, PFChan_Core_PEER, ep, resp->milliseconds);
  248. }
  249. ep->timeOfLastPing = Time_currentTimeMilliseconds();
  250. if (Defined(Log_DEBUG)) {
  251. String* addr = Address_toString(&ep->addr, resp->ping->pingAlloc);
  252. Log_debug(ic->logger, "Received [%s] from lazy endpoint [%s], state: [%s]",
  253. SwitchPinger_resultString(resp->res)->bytes, addr->bytes,
  254. InterfaceController_stateString(ep->state));
  255. }
  256. }
  257. /*
  258. * Send a ping packet to one of the endpoints.
  259. */
  260. static void sendPing(struct Peer* ep)
  261. {
  262. struct InterfaceController_pvt* ic = Identity_check(ep->ici->ic);
  263. ep->pingCount++;
  264. struct SwitchPinger_Ping* ping =
  265. SwitchPinger_newPing(ep->addr.path,
  266. String_CONST(""),
  267. ic->timeoutMilliseconds,
  268. onPingResponse,
  269. ep->alloc,
  270. ic->switchPinger);
  271. if (!ping) {
  272. struct Allocator* alloc = Allocator_child(ep->alloc);
  273. Log_debug(ic->logger, "Sending switch ping to [%s] failed, out of ping slots",
  274. Address_toString(&ep->addr, alloc)->bytes);
  275. Allocator_free(alloc);
  276. } else {
  277. Log_debug(ic->logger, "Sending switch ping to [%s]",
  278. Address_toString(&ep->addr, ping->pingAlloc)->bytes);
  279. }
  280. if (ping) {
  281. ping->onResponseContext = ep;
  282. }
  283. }
  284. static void linkState(void* vic)
  285. {
  286. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) vic);
  287. uint32_t msgLen = 64;
  288. for (int i = 0; i < ic->icis->length; i++) {
  289. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  290. msgLen += PFChan_LinkState_Entry_SIZE * ici->peerMap.count;
  291. }
  292. struct Allocator* alloc = Allocator_child(ic->alloc);
  293. struct Message* msg = Message_new(0, msgLen, alloc);
  294. for (int i = 0; i < ic->icis->length; i++) {
  295. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  296. for (uint32_t i = 0; i < ici->peerMap.count; i++) {
  297. struct Peer* ep = ici->peerMap.values[i];
  298. RTypes_CryptoStats_t stats;
  299. Ca_stats(ep->caSession, &stats);
  300. uint64_t newDrops = 0;
  301. // Prevents invalid number when the session resets
  302. if (stats.lost_packets > ep->_lastDrops) {
  303. newDrops = stats.lost_packets - ep->_lastDrops;
  304. }
  305. ep->_lastDrops = stats.lost_packets;
  306. ep->lastDrops += newDrops;
  307. uint64_t newPackets = 0;
  308. if (stats.received_packets > ep->_lastPackets) {
  309. newPackets = stats.received_packets - ep->_lastPackets;
  310. }
  311. ep->_lastPackets = stats.received_packets;
  312. ep->lastPackets += newPackets;
  313. struct PFChan_LinkState_Entry e = {
  314. .peerLabel = ep->addr.path,
  315. .sumOfPackets = ep->lastPackets,
  316. .sumOfDrops = ep->lastDrops,
  317. .sumOfKb = (ep->bytesIn >> 10),
  318. };
  319. Er_assert(Message_epush(msg, &e, PFChan_LinkState_Entry_SIZE));
  320. }
  321. }
  322. if (Message_getLength(msg)) {
  323. Er_assert(Message_epush32be(msg, 0xffffffff));
  324. Er_assert(Message_epush32be(msg, PFChan_Core_LINK_STATE));
  325. Iface_send(&ic->eventEmitterIf, msg);
  326. }
  327. Allocator_free(alloc);
  328. }
  329. static void iciPing(struct InterfaceController_Iface_pvt* ici, struct InterfaceController_pvt* ic)
  330. {
  331. if (!ici->peerMap.count) { return; }
  332. uint64_t now = Time_currentTimeMilliseconds();
  333. // scan for endpoints have not sent anything recently.
  334. uint32_t startAt = ici->lastPeerPinged = (ici->lastPeerPinged + 1) % ici->peerMap.count;
  335. for (uint32_t i = startAt, count = 0; count < ici->peerMap.count;) {
  336. i = (i + 1) % ici->peerMap.count;
  337. count++;
  338. struct Peer* ep = ici->peerMap.values[i];
  339. if (knownIncompatibleVersion(ep->addr.protocolVersion)) {
  340. // This is a version mismatch, we have nothing to do with this node
  341. // but we keep the session in INCOMPATIBLE state to keep track of the
  342. // fact that we don't want to talk to it.
  343. ep->state = InterfaceController_PeerState_INCOMPATIBLE;
  344. continue;
  345. }
  346. uint8_t ipIfDebug[40];
  347. if (Defined(Log_DEBUG)) {
  348. Address_printIp(ipIfDebug, &ep->addr);
  349. }
  350. if (ep->addr.protocolVersion && now < ep->timeOfLastMessage + ic->pingAfterMilliseconds) {
  351. // It's sending traffic so leave it alone.
  352. // wait just a minute here !
  353. // There is a risk that the NodeStore somehow forgets about our peers while the peers
  354. // are still happily sending traffic. To break this bad cycle lets just send a PEER
  355. // message once per second for whichever peer is the first that we address.
  356. if (count == 1 && ep->state == InterfaceController_PeerState_ESTABLISHED) {
  357. // noisy
  358. //Log_debug(ic->logger, "Notifying about peer number [%d/%d] [%s]",
  359. // i, ici->peerMap.count, ipIfDebug);
  360. sendPeer(0xffffffff, PFChan_Core_PEER, ep, 0xffff);
  361. }
  362. continue;
  363. }
  364. if (now < ep->timeOfLastPing + ic->pingAfterMilliseconds) {
  365. // Possibly an out-of-date node which is mangling packets, don't ping too often
  366. // because it causes the RumorMill to be filled with this node over and over.
  367. continue;
  368. }
  369. if (ep->isIncomingConnection && now > ep->timeOfLastMessage + ic->forgetAfterMilliseconds) {
  370. Log_debug(ic->logger, "Unresponsive peer [%s] has not responded in [%u] "
  371. "seconds, dropping connection",
  372. ipIfDebug, ic->forgetAfterMilliseconds / 1024);
  373. sendPeer(0xffffffff, PFChan_Core_PEER_GONE, ep, 0xffff);
  374. Allocator_free(ep->alloc);
  375. continue;
  376. }
  377. bool unresponsive = (now > ep->timeOfLastMessage + ic->unresponsiveAfterMilliseconds);
  378. if (unresponsive) {
  379. // our link to the peer is broken...
  380. // Lets skip 87% of pings when they're really down.
  381. if (ep->pingCount % 8) {
  382. ep->pingCount++;
  383. continue;
  384. }
  385. sendPeer(0xffffffff, PFChan_Core_PEER_GONE, ep, 0xffff);
  386. ep->state = InterfaceController_PeerState_UNRESPONSIVE;
  387. }
  388. Log_debug(ic->logger,
  389. "Pinging %s peer [%s] lag [%u]",
  390. (unresponsive ? "unresponsive" : "lazy"),
  391. ipIfDebug,
  392. (uint32_t)((now - ep->timeOfLastMessage) / 1024));
  393. sendPing(ep);
  394. // we only ping one node
  395. return;
  396. }
  397. }
  398. /**
  399. * Check the table for nodes which might need to be pinged, ping a node if necessary.
  400. * If a node has not responded in unresponsiveAfterMilliseconds then mark them as unresponsive
  401. * and if the connection is incoming and the node has not responded in forgetAfterMilliseconds
  402. * then drop them entirely.
  403. * This is called every PING_INTERVAL_MILLISECONDS
  404. */
  405. static void pingCycle(void* vic)
  406. {
  407. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) vic);
  408. for (int i = 0; i < ic->icis->length; i++) {
  409. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  410. iciPing(ici, ic);
  411. }
  412. }
  413. static Iface_DEFUN afterEncrypt(struct Message* msg, struct Iface* ciphertext)
  414. {
  415. struct Peer* ep = Identity_containerOf(ciphertext, struct Peer, ciphertext);
  416. // push the lladdr...
  417. Er_assert(Message_epush(msg, ep->lladdr, ep->lladdr->addrLen));
  418. // very noisy
  419. if (Defined(Log_DEBUG) && false) {
  420. char* printedAddr =
  421. Hex_print(&ep->lladdr[1], ep->lladdr->addrLen - Sockaddr_OVERHEAD, Message_getAlloc(msg));
  422. Log_debug(ep->ici->ic->logger, "Outgoing message to [%s]", printedAddr);
  423. }
  424. return Iface_send(&ep->ici->pub.addrIf, msg);
  425. }
  426. // This is directly called from SwitchCore, message is not encrypted.
  427. static Iface_DEFUN sendFromSwitch(struct Message* msg, struct Iface* switchIf)
  428. {
  429. struct Peer* ep = Identity_check((struct Peer*) switchIf);
  430. // Once we know it to be an incompetible version, we quarentine it
  431. if (knownIncompatibleVersion(ep->addr.protocolVersion)) {
  432. if (Defined(Log_DEBUG)) {
  433. Log_debug(ep->ici->ic->logger, "[%s] DROP msg to node with incompat version [%d] ",
  434. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes, ep->addr.protocolVersion);
  435. }
  436. ep->state = InterfaceController_PeerState_INCOMPATIBLE;
  437. return Error(msg, "UNHANDLED");
  438. }
  439. ep->bytesOut += Message_getLength(msg);
  440. Kbps_accumulate(&ep->sendBw, Time_currentTimeMilliseconds(), Message_getLength(msg));
  441. return Iface_next(&ep->plaintext, msg); // --> afterEncrypt
  442. }
  443. static int closeInterface(struct Allocator_OnFreeJob* job)
  444. {
  445. struct Peer* toClose = Identity_check((struct Peer*) job->userData);
  446. int index = Map_EndpointsBySockaddr_indexForHandle(toClose->handle, &toClose->ici->peerMap);
  447. if (index < 0 || toClose->ici->peerMap.values[index] != toClose) {
  448. // Happens if the ep was created as a result of handleUnexpectedIncoming
  449. return 0;
  450. }
  451. if (!Allocator_isFreeing(toClose->ici->ic->alloc)) {
  452. sendPeer(0xffffffff, PFChan_Core_PEER_GONE, toClose, 0xffff);
  453. }
  454. Log_debug(toClose->ici->ic->logger,
  455. "Closing interface [%d] with handle [%u]", index, toClose->handle);
  456. Map_EndpointsBySockaddr_remove(index, &toClose->ici->peerMap);
  457. return 0;
  458. }
  459. static Iface_DEFUN afterDecrypt(struct Message* msg, struct Iface* plaintext);
  460. static struct Peer* mkEp(
  461. const struct Sockaddr* lladdr,
  462. struct InterfaceController_Iface_pvt* ici,
  463. uint8_t publicKey[32],
  464. bool authNeeded,
  465. const char* name,
  466. bool useNoise
  467. ) {
  468. struct Allocator* epAlloc = Allocator_child(ici->alloc);
  469. struct Peer* ep = Allocator_calloc(epAlloc, sizeof(struct Peer), 1);
  470. Identity_set(ep);
  471. ep->ici = ici;
  472. ep->lladdr = Sockaddr_clone(lladdr, epAlloc);
  473. ep->alloc = epAlloc;
  474. ep->state = InterfaceController_PeerState_UNAUTHENTICATED;
  475. ep->isIncomingConnection = false;
  476. ep->switchIf.send = sendFromSwitch;
  477. ep->ciphertext.send = afterEncrypt;
  478. ep->plaintext.send = afterDecrypt;
  479. useNoise = useNoise && ici->ic->enableNoise;
  480. ep->caSession = Ca_newSession(ici->ic->ca, epAlloc, publicKey, authNeeded, name, useNoise);
  481. Iface_plumb(ep->caSession->ciphertext, &ep->ciphertext);
  482. Iface_plumb(ep->caSession->plaintext, &ep->plaintext);
  483. Bits_memcpy(ep->addr.key, publicKey, 32);
  484. Address_getPrefix(&ep->addr);
  485. Allocator_onFree(epAlloc, closeInterface, ep);
  486. return ep;
  487. }
  488. static struct Peer* epFromSess(
  489. const struct Sockaddr* lladdr,
  490. struct InterfaceController_Iface_pvt* ici,
  491. Ca_Session_t* sess,
  492. Allocator_t* alloc
  493. ) {
  494. struct Peer* ep = Allocator_calloc(alloc, sizeof(struct Peer), 1);
  495. Identity_set(ep);
  496. ep->ici = ici;
  497. ep->lladdr = Sockaddr_clone(lladdr, alloc);
  498. ep->alloc = alloc;
  499. ep->state = InterfaceController_PeerState_UNAUTHENTICATED;
  500. ep->isIncomingConnection = true;
  501. ep->switchIf.send = sendFromSwitch;
  502. ep->ciphertext.send = afterEncrypt;
  503. ep->plaintext.send = afterDecrypt;
  504. ep->caSession = sess;
  505. Iface_plumb(ep->caSession->ciphertext, &ep->ciphertext);
  506. Iface_plumb(ep->caSession->plaintext, &ep->plaintext);
  507. Ca_getHerPubKey(sess, ep->addr.key);
  508. ep->addr.protocolVersion = Rffi_CryptoAuth2_cjdnsVer(sess);
  509. Address_getPrefix(&ep->addr);
  510. Allocator_onFree(alloc, closeInterface, ep);
  511. return ep;
  512. }
  513. /**
  514. * Expects [ struct LLAddress ][ beacon ]
  515. */
  516. static Iface_DEFUN handleBeacon(
  517. struct Message* msg,
  518. struct InterfaceController_Iface_pvt* ici,
  519. struct Sockaddr* lladdr)
  520. {
  521. struct InterfaceController_pvt* ic = ici->ic;
  522. if (!ici->pub.beaconState) {
  523. // accepting beacons disabled.
  524. Log_debug(ic->logger, "[%s] Dropping beacon because beaconing is disabled",
  525. ici->pub.name->bytes);
  526. return NULL;
  527. }
  528. if (Message_getLength(msg) < Headers_Beacon_SIZE) {
  529. Log_debug(ic->logger, "[%s] Dropping runt beacon", ici->pub.name->bytes);
  530. return Error(msg, "RUNT");
  531. }
  532. // clear the bcast flag
  533. lladdr->flags = 0;
  534. struct Headers_Beacon beacon;
  535. Er_assert(Message_epop(msg, &beacon, Headers_Beacon_SIZE));
  536. if (Defined(Log_DEBUG)) {
  537. char* content = Hex_print(&beacon, Headers_Beacon_SIZE, Message_getAlloc(msg));
  538. Log_debug(ici->ic->logger, "RECV BEACON CONTENT[%s]", content);
  539. }
  540. struct Address addr;
  541. Bits_memset(&addr, 0, sizeof(struct Address));
  542. Bits_memcpy(addr.key, beacon.publicKey, 32);
  543. addr.protocolVersion = Endian_bigEndianToHost32(beacon.version_be);
  544. Address_getPrefix(&addr);
  545. String* printedAddr = NULL;
  546. if (Defined(Log_DEBUG)) {
  547. printedAddr = Address_toString(&addr, Message_getAlloc(msg));
  548. }
  549. if (!AddressCalc_validAddress(addr.ip6.bytes)) {
  550. Log_debug(ic->logger, "handleBeacon invalid key [%s]", printedAddr->bytes);
  551. return Error(msg, "INVALID");
  552. } else if (!Bits_memcmp(ic->ourPubKey, addr.key, 32)) {
  553. // receive beacon from self, drop silent
  554. return NULL;
  555. }
  556. if (knownIncompatibleVersion(addr.protocolVersion)) {
  557. if (Defined(Log_DEBUG)) {
  558. Log_debug(ic->logger, "[%s] DROP beacon from [%s] which was version [%d] "
  559. "our version is [%d] making them incompatable", ici->pub.name->bytes,
  560. printedAddr->bytes, addr.protocolVersion, Version_CURRENT_PROTOCOL);
  561. }
  562. return Error(msg, "UNHANDLED");
  563. }
  564. String* beaconPass = String_newBinary(beacon.password, Headers_Beacon_PASSWORD_LEN, Message_getAlloc(msg));
  565. int epIndex = Map_EndpointsBySockaddr_indexForKey(&lladdr, &ici->peerMap);
  566. if (epIndex > -1) {
  567. // The password might have changed!
  568. struct Peer* ep = ici->peerMap.values[epIndex];
  569. Ca_setAuth(beaconPass, String_CONST("Local Peers"), ep->caSession);
  570. return NULL;
  571. }
  572. bool useNoise = addr.protocolVersion >= 22;
  573. struct Peer* ep = mkEp(lladdr, ici, beacon.publicKey, false, "beacon_peer", useNoise);
  574. int setIndex = Map_EndpointsBySockaddr_put(&ep->lladdr, &ep, &ici->peerMap);
  575. ep->handle = ici->peerMap.handles[setIndex];
  576. // We make the connection ourselves but we still consider
  577. // it "incoming" because we replied to a beacon
  578. ep->isIncomingConnection = true;
  579. ep->addr.protocolVersion = addr.protocolVersion;
  580. Ca_setAuth(beaconPass, String_CONST("Local Peers"), ep->caSession);
  581. if (SwitchCore_addInterface(ic->switchCore, &ep->switchIf, ep->alloc, &ep->addr.path)) {
  582. Log_debug(ic->logger, "handleBeacon() SwitchCore out of space");
  583. Allocator_free(ep->alloc);
  584. return Error(msg, "UNHANDLED");
  585. }
  586. // We want the node to immedietly be pinged but we don't want it to appear unresponsive because
  587. // the pinger will only ping every (PING_INTERVAL * 8) so we set timeOfLastMessage to
  588. // (now - pingAfterMilliseconds - 1) so it will be considered a "lazy node".
  589. ep->timeOfLastMessage =
  590. Time_currentTimeMilliseconds() - ic->pingAfterMilliseconds - 1;
  591. Log_info(ic->logger, "Added peer [%s] from beacon",
  592. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes);
  593. // Ping them immediately, this prevents beacon tests from taking 1 second each
  594. sendPing(ep);
  595. return NULL;
  596. }
  597. static Iface_DEFUN handleIncomingFromWire(struct Message* msg, struct Iface* addrIf)
  598. {
  599. struct InterfaceController_Iface_pvt* ici =
  600. Identity_containerOf(addrIf, struct InterfaceController_Iface_pvt, pub.addrIf);
  601. struct Sockaddr_storage lladdrStore;
  602. struct Sockaddr* lladdr = (struct Sockaddr*) &lladdrStore;
  603. {
  604. struct Sockaddr* lladdr0 = (struct Sockaddr*) msg->msgbytes;
  605. if (Message_getLength(msg) < Sockaddr_OVERHEAD || Message_getLength(msg) < lladdr0->addrLen) {
  606. Log_debug(ici->ic->logger, "DROP runt");
  607. return Error(msg, "RUNT");
  608. }
  609. Er_assert(Message_epop(msg, lladdr, lladdr0->addrLen));
  610. }
  611. Assert_true(!((uintptr_t)msg->msgbytes % 4) && "alignment fault");
  612. Assert_true(!((uintptr_t)lladdr->addrLen % 4) && "alignment fault");
  613. char* printedAddr = "<unknown>";
  614. if (Defined(Log_DEBUG)) {
  615. printedAddr = Hex_print(&lladdr[1], lladdr->addrLen - Sockaddr_OVERHEAD, Message_getAlloc(msg));
  616. }
  617. // noisy
  618. if (Defined(Log_DEBUG) && false) {
  619. Log_debug(ici->ic->logger, "Incoming message from [%s]", printedAddr);
  620. }
  621. if (lladdr->flags & Sockaddr_flags_BCAST) {
  622. return handleBeacon(msg, ici, lladdr);
  623. }
  624. if (Message_getLength(msg) < 4) {
  625. return Error(msg, "RUNT");
  626. }
  627. int epIndex = Map_EndpointsBySockaddr_indexForKey(&lladdr, &ici->peerMap);
  628. if (epIndex == -1) {
  629. // noise control message
  630. Er_assert(Message_epush(msg, NULL, 16));
  631. Sockaddr_asIp6(msg->msgbytes, lladdr);
  632. RTypes_CryptoAuth2_TryHandshake_Ret_t ret = { .code = 0 };
  633. Rffi_CryptoAuth2_tryHandshake(ici->ic->ca, msg, ici->alloc, true, &ret);
  634. if (ret.sess) {
  635. // We have a new session, setup the endpoint
  636. struct Peer* ep = epFromSess(lladdr, ici, ret.sess, ret.alloc);
  637. if (SwitchCore_addInterface(ici->ic->switchCore, &ep->switchIf, ep->alloc, &ep->addr.path)) {
  638. Log_debug(ici->ic->logger, "handleUnexpectedIncoming() SwitchCore out of space");
  639. Allocator_free(ep->alloc);
  640. return Error(msg, "UNHANDLED");
  641. }
  642. Assert_true(Map_EndpointsBySockaddr_indexForKey(&ep->lladdr, &ici->peerMap) == -1);
  643. int index = Map_EndpointsBySockaddr_put(&ep->lladdr, &ep, &ici->peerMap);
  644. Assert_true(index >= 0);
  645. ep->handle = ici->peerMap.handles[index];
  646. // We want the node to immedietly be pinged but we don't want it to appear unresponsive because
  647. // the pinger will only ping every (PING_INTERVAL * 8) so we set timeOfLastMessage to
  648. // (now - pingAfterMilliseconds - 1) so it will be considered a "lazy node".
  649. ep->timeOfLastMessage =
  650. Time_currentTimeMilliseconds() - ici->ic->pingAfterMilliseconds - 1;
  651. Log_info(ici->ic->logger, "Added peer [%s] from incoming message",
  652. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes);
  653. if (ep->addr.protocolVersion) {
  654. // This will only work if the other end sent us their version (WG mode)
  655. sendPeer(0xffffffff, PFChan_Core_PEER, ep, 0xffff);
  656. } else {
  657. // We don't know their version, ping them to find out
  658. sendPing(ep);
  659. }
  660. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_RecvPlaintext) {
  661. // receive the packet
  662. return afterDecrypt(msg, &ep->plaintext);
  663. }
  664. }
  665. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_ReplyToPeer) {
  666. // Send back a reply to the node who sent us this packet
  667. Er_assert(Message_epush(msg, lladdr, lladdr->addrLen));
  668. return Iface_next(&ici->pub.addrIf, msg);
  669. }
  670. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_Error) {
  671. Log_debug(ici->ic->logger, "Error on unexpected packet from [%s]: [%d]",
  672. printedAddr, ret.err);
  673. return Error(msg, "DECRYPT");
  674. }
  675. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_Done) {
  676. // Nothing to do
  677. return NULL;
  678. }
  679. Assert_failure("Rffi_CryptoAuth2_tryHandshake() replied [%d]", ret.code);
  680. }
  681. struct Peer* ep = Identity_check((struct Peer*) ici->peerMap.values[epIndex]);
  682. // Once we know it to be an incompetible version, we quarentine it
  683. if (knownIncompatibleVersion(ep->addr.protocolVersion)) {
  684. if (Defined(Log_DEBUG)) {
  685. Log_debug(ici->ic->logger, "[%s] DROP msg from node with incompat version [%d] ",
  686. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes, ep->addr.protocolVersion);
  687. }
  688. ep->state = InterfaceController_PeerState_INCOMPATIBLE;
  689. return NULL;
  690. }
  691. Ca_resetIfTimeout(ep->caSession);
  692. Er_assert(Message_epush(msg, NULL, 16));
  693. Sockaddr_asIp6(msg->msgbytes, lladdr);
  694. return Iface_next(&ep->ciphertext, msg); // -> afterDecrypt
  695. }
  696. // Expects result of CryptoAuth decrypt
  697. static Iface_DEFUN afterDecrypt(struct Message* msg, struct Iface* plaintext)
  698. {
  699. struct Peer* ep = Identity_containerOf(plaintext, struct Peer, plaintext);
  700. struct InterfaceController_Iface_pvt* ici = Identity_check(ep->ici);
  701. struct InterfaceController_pvt* ic = Identity_check(ici->ic);
  702. enum Ca_DecryptErr err = Er_assert(Message_epop32h(msg));
  703. if (err) {
  704. return Error(msg, "AUTHENTICATION");
  705. }
  706. Kbps_accumulate(&ep->recvBw, Time_currentTimeMilliseconds(), Message_getLength(msg));
  707. ep->bytesIn += Message_getLength(msg);
  708. int caState = Ca_getState(ep->caSession);
  709. if (caState != Ca_State_ESTABLISHED) {
  710. // prevent some kinds of nasty things which could be done with packet replay.
  711. // This is checking the message switch header and will drop it unless the label
  712. // directs it to *this* router.
  713. if (Message_getLength(msg) < 8 || msg->msgbytes[7] != 1) {
  714. Log_info(ic->logger, "DROP message because CA is not established.");
  715. return Error(msg, "UNHANDLED");
  716. } else {
  717. // When a "server" gets a new connection from a "client" the router doesn't
  718. // know about that client so if the client sends a packet to the server, the
  719. // server will be unable to handle it until the client has sent inter-router
  720. // communication to the server. Here we will ping the client so when the
  721. // server gets the ping response, it will insert the client into its table
  722. // and know its version.
  723. // prevent DoS by limiting the number of times this can be called per second
  724. // limit it to 7, this will affect innocent packets but it doesn't matter much
  725. // since this is mostly just an optimization and for keeping the tests happy.
  726. if ((ep->pingCount + 1) % 7) {
  727. sendPing(ep);
  728. }
  729. }
  730. } else {
  731. if (ep->state != caState) {
  732. sendPeer(0xffffffff, PFChan_Core_PEER, ep, 0xffff);
  733. }
  734. ep->timeOfLastMessage = Time_currentTimeMilliseconds();
  735. }
  736. ep->state = caState;
  737. Identity_check(ep);
  738. return Iface_next(&ep->switchIf, msg);
  739. }
  740. int InterfaceController_ifaceCount(struct InterfaceController* ifc)
  741. {
  742. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  743. return ic->icis->length;
  744. }
  745. struct InterfaceController_Iface* InterfaceController_getIface(struct InterfaceController* ifc,
  746. int ifNum)
  747. {
  748. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  749. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, ifNum);
  750. return (ici) ? &ici->pub : NULL;
  751. }
  752. struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceController* ifc,
  753. String* name,
  754. struct Allocator* alloc)
  755. {
  756. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  757. struct InterfaceController_Iface_pvt* ici =
  758. Allocator_calloc(alloc, sizeof(struct InterfaceController_Iface_pvt), 1);
  759. ici->pub.name = String_clone(name, alloc);
  760. ici->peerMap.allocator = alloc;
  761. ici->ic = ic;
  762. ici->alloc = alloc;
  763. ici->pub.addrIf.send = handleIncomingFromWire;
  764. ici->pub.ifNum = ArrayList_OfIfaces_add(ic->icis, ici);
  765. Identity_set(ici);
  766. return &ici->pub;
  767. }
  768. static void sendBeacon(struct InterfaceController_Iface_pvt* ici, struct Allocator* tempAlloc)
  769. {
  770. if (ici->pub.beaconState < InterfaceController_beaconState_newState_SEND) {
  771. Log_debug(ici->ic->logger, "sendBeacon(%s) -> beaconing disabled", ici->pub.name->bytes);
  772. return;
  773. }
  774. Log_debug(ici->ic->logger, "sendBeacon(%s)", ici->pub.name->bytes);
  775. struct Message* msg = Message_new(0, 128, tempAlloc);
  776. Er_assert(Message_epush(msg, &ici->ic->beacon, Headers_Beacon_SIZE));
  777. if (Defined(Log_DEBUG)) {
  778. char* content = Hex_print(msg->msgbytes, Message_getLength(msg), tempAlloc);
  779. Log_debug(ici->ic->logger, "SEND BEACON CONTENT[%s]", content);
  780. }
  781. struct Sockaddr sa = {
  782. .addrLen = Sockaddr_OVERHEAD,
  783. .flags = Sockaddr_flags_BCAST
  784. };
  785. Er_assert(Message_epush(msg, &sa, Sockaddr_OVERHEAD));
  786. Iface_send(&ici->pub.addrIf, msg);
  787. }
  788. static void handshakeCycle(void* vInterfaceController)
  789. {
  790. struct InterfaceController_pvt* ic =
  791. Identity_check((struct InterfaceController_pvt*) vInterfaceController);
  792. struct Allocator* alloc = Allocator_child(ic->alloc);
  793. for (int i = 0; i < ic->icis->length; i++) {
  794. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  795. for (uint32_t j = 0; j < ici->peerMap.count; j++) {
  796. struct Peer* ep = Identity_check((struct Peer*) ici->peerMap.values[j]);
  797. Message_t* msg = Rffi_CryptoAuth2_noiseTick(ep->caSession, alloc);
  798. if (msg != NULL) {
  799. Er_assert(Message_epush(msg, ep->lladdr, ep->lladdr->addrLen));
  800. Iface_send(&ep->ici->pub.addrIf, msg);
  801. }
  802. }
  803. }
  804. Allocator_free(alloc);
  805. }
  806. static void beaconInterval(void* vInterfaceController)
  807. {
  808. struct InterfaceController_pvt* ic =
  809. Identity_check((struct InterfaceController_pvt*) vInterfaceController);
  810. struct Allocator* alloc = Allocator_child(ic->alloc);
  811. for (int i = 0; i < ic->icis->length; i++) {
  812. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  813. sendBeacon(ici, alloc);
  814. }
  815. Allocator_free(alloc);
  816. if (ic->beaconTimeoutAlloc) {
  817. Allocator_free(ic->beaconTimeoutAlloc);
  818. }
  819. ic->beaconTimeoutAlloc = Allocator_child(ic->alloc);
  820. Timeout_setTimeout(
  821. beaconInterval, ic, ic->beaconInterval, ic->eventBase, ic->beaconTimeoutAlloc);
  822. }
  823. int InterfaceController_beaconState(struct InterfaceController* ifc,
  824. int interfaceNumber,
  825. int newState)
  826. {
  827. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  828. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, interfaceNumber);
  829. if (!ici) {
  830. return InterfaceController_beaconState_NO_SUCH_IFACE;
  831. }
  832. char* val = NULL;
  833. switch (newState) {
  834. default: return InterfaceController_beaconState_INVALID_STATE;
  835. case InterfaceController_beaconState_newState_OFF: val = "OFF"; break;
  836. case InterfaceController_beaconState_newState_ACCEPT: val = "ACCEPT"; break;
  837. case InterfaceController_beaconState_newState_SEND: val = "SEND"; break;
  838. }
  839. Log_debug(ic->logger, "InterfaceController_beaconState(%s, %s)", ici->pub.name->bytes, val);
  840. ici->pub.beaconState = newState;
  841. if (newState == InterfaceController_beaconState_newState_SEND) {
  842. // Send out a beacon right away so we don't have to wait.
  843. struct Allocator* alloc = Allocator_child(ici->alloc);
  844. sendBeacon(ici, alloc);
  845. Allocator_free(alloc);
  846. }
  847. return 0;
  848. }
  849. int InterfaceController_bootstrapPeer(struct InterfaceController* ifc,
  850. int interfaceNumber,
  851. uint8_t* herPublicKey,
  852. const struct Sockaddr* lladdrParm,
  853. String* password,
  854. String* login,
  855. String* user,
  856. int version)
  857. {
  858. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  859. Assert_true(herPublicKey);
  860. Assert_true(password);
  861. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, interfaceNumber);
  862. if (!ici) {
  863. return InterfaceController_bootstrapPeer_BAD_IFNUM;
  864. }
  865. Log_debug(ic->logger, "bootstrapPeer total [%u]", ici->peerMap.count);
  866. uint8_t ip6[16];
  867. AddressCalc_addressForPublicKey(ip6, herPublicKey);
  868. if (!AddressCalc_validAddress(ip6) || !Bits_memcmp(ic->ourPubKey, herPublicKey, 32)) {
  869. return InterfaceController_bootstrapPeer_BAD_KEY;
  870. }
  871. // We often don't know this, but in that case we will fallback to the old peering
  872. bool useNoise = version >= 22;
  873. struct Peer* ep = mkEp(lladdrParm, ici, herPublicKey, false, user ? user->bytes : NULL, useNoise);
  874. ep->addr.protocolVersion = version;
  875. int index = Map_EndpointsBySockaddr_put(&ep->lladdr, &ep, &ici->peerMap);
  876. Assert_true(index >= 0);
  877. ep->handle = ici->peerMap.handles[index];
  878. Ca_setAuth(password, login, ep->caSession);
  879. if (SwitchCore_addInterface(ic->switchCore, &ep->switchIf, ep->alloc, &ep->addr.path)) {
  880. Log_debug(ic->logger, "bootstrapPeer() SwitchCore out of space");
  881. Allocator_free(ep->alloc);
  882. return InterfaceController_bootstrapPeer_OUT_OF_SPACE;
  883. }
  884. // We want the node to immedietly be pinged but we don't want it to appear unresponsive because
  885. // the pinger will only ping every (PING_INTERVAL * 8) so we set timeOfLastMessage to
  886. // (now - pingAfterMilliseconds - 1) so it will be considered a "lazy node".
  887. ep->timeOfLastMessage =
  888. Time_currentTimeMilliseconds() - ic->pingAfterMilliseconds - 1;
  889. if (Defined(Log_INFO)) {
  890. struct Allocator* tempAlloc = Allocator_child(ep->alloc);
  891. String* addrStr = Address_toString(&ep->addr, tempAlloc);
  892. Log_info(ic->logger, "Adding peer [%s] from bootstrapPeer()", addrStr->bytes);
  893. Allocator_free(tempAlloc);
  894. }
  895. // We can't just add the node directly to the routing table because we do not know
  896. // the version. We'll send it a switch ping and when it responds, we will know it's
  897. // key (if we don't already) and version number.
  898. sendPing(ep);
  899. return 0;
  900. }
  901. int InterfaceController_getPeerStats(struct InterfaceController* ifController,
  902. struct Allocator* alloc,
  903. struct InterfaceController_PeerStats** statsOut)
  904. {
  905. struct InterfaceController_pvt* ic =
  906. Identity_check((struct InterfaceController_pvt*) ifController);
  907. int count = 0;
  908. for (int i = 0; i < ic->icis->length; i++) {
  909. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  910. count += ici->peerMap.count;
  911. }
  912. struct InterfaceController_PeerStats* stats =
  913. Allocator_calloc(alloc, sizeof(struct InterfaceController_PeerStats), count);
  914. uint32_t now = Time_currentTimeMilliseconds();
  915. int xcount = 0;
  916. for (int j = 0; j < ic->icis->length; j++) {
  917. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  918. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  919. struct Peer* peer = Identity_check((struct Peer*) ici->peerMap.values[i]);
  920. struct InterfaceController_PeerStats* s = &stats[xcount];
  921. xcount++;
  922. s->ifNum = ici->pub.ifNum;
  923. s->lladdr = Sockaddr_clone(peer->lladdr, alloc);
  924. Bits_memcpy(&s->addr, &peer->addr, sizeof(struct Address));
  925. s->bytesOut = peer->bytesOut;
  926. s->bytesIn = peer->bytesIn;
  927. s->timeOfLastMessage = peer->timeOfLastMessage;
  928. s->state = peer->state;
  929. s->isIncomingConnection = peer->isIncomingConnection;
  930. s->user = Ca_getName(peer->caSession, alloc);
  931. RTypes_CryptoStats_t stats;
  932. Ca_stats(peer->caSession, &stats);
  933. s->duplicates = stats.duplicate_packets;
  934. s->receivedOutOfRange = stats.received_unexpected;
  935. s->noiseProto = stats.noise_proto;
  936. s->recvKbps = Kbps_accumulate(&peer->recvBw, now, Kbps_accumulate_NO_PACKET);
  937. s->sendKbps = Kbps_accumulate(&peer->sendBw, now, Kbps_accumulate_NO_PACKET);
  938. s->receivedPackets = peer->lastPackets;
  939. s->lostPackets = peer->lastDrops;
  940. }
  941. }
  942. Assert_true(xcount == count);
  943. *statsOut = stats;
  944. return count;
  945. }
  946. void InterfaceController_resetPeering(struct InterfaceController* ifController,
  947. uint8_t herPublicKey[32])
  948. {
  949. struct InterfaceController_pvt* ic =
  950. Identity_check((struct InterfaceController_pvt*) ifController);
  951. for (int j = 0; j < ic->icis->length; j++) {
  952. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  953. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  954. struct Peer* peer = ici->peerMap.values[i];
  955. if (!herPublicKey || !Bits_memcmp(herPublicKey, peer->addr.key, 32)) {
  956. Ca_reset(peer->caSession);
  957. }
  958. }
  959. }
  960. }
  961. int InterfaceController_disconnectPeer(struct InterfaceController* ifController,
  962. uint8_t herPublicKey[32])
  963. {
  964. struct InterfaceController_pvt* ic =
  965. Identity_check((struct InterfaceController_pvt*) ifController);
  966. int count = 0;
  967. for (int j = 0; j < ic->icis->length; j++) {
  968. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  969. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  970. struct Peer* peer = ici->peerMap.values[i];
  971. if (!Bits_memcmp(herPublicKey, peer->addr.key, 32)) {
  972. Allocator_free(peer->alloc);
  973. count++;
  974. }
  975. }
  976. }
  977. return count;
  978. }
  979. static Iface_DEFUN incomingFromEventEmitterIf(struct Message* msg, struct Iface* eventEmitterIf)
  980. {
  981. struct InterfaceController_pvt* ic =
  982. Identity_containerOf(eventEmitterIf, struct InterfaceController_pvt, eventEmitterIf);
  983. uint32_t peers = Er_assert(Message_epop32be(msg));
  984. Assert_true(peers == PFChan_Pathfinder_PEERS);
  985. uint32_t pathfinderId = Er_assert(Message_epop32be(msg));
  986. Assert_true(!Message_getLength(msg));
  987. for (int j = 0; j < ic->icis->length; j++) {
  988. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  989. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  990. struct Peer* peer = Identity_check((struct Peer*) ici->peerMap.values[i]);
  991. if (peer->state != InterfaceController_PeerState_ESTABLISHED) { continue; }
  992. sendPeer(pathfinderId, PFChan_Core_PEER, peer, 0xffff);
  993. }
  994. }
  995. return NULL;
  996. }
  997. struct InterfaceController* InterfaceController_new(Ca_t* ca,
  998. struct SwitchCore* switchCore,
  999. struct Log* logger,
  1000. struct EventBase* eventBase,
  1001. struct SwitchPinger* switchPinger,
  1002. struct Random* rand,
  1003. struct Allocator* allocator,
  1004. struct EventEmitter* ee,
  1005. bool enableNoise)
  1006. {
  1007. struct Allocator* alloc = Allocator_child(allocator);
  1008. struct InterfaceController_pvt* out =
  1009. Allocator_calloc(alloc, sizeof(struct InterfaceController_pvt), 1);
  1010. Bits_memcpy(out, (&(struct InterfaceController_pvt) {
  1011. .alloc = alloc,
  1012. .ca = ca,
  1013. .rand = rand,
  1014. .switchCore = switchCore,
  1015. .logger = logger,
  1016. .eventBase = eventBase,
  1017. .switchPinger = switchPinger,
  1018. .unresponsiveAfterMilliseconds = UNRESPONSIVE_AFTER_MILLISECONDS,
  1019. .pingAfterMilliseconds = PING_AFTER_MILLISECONDS,
  1020. .timeoutMilliseconds = TIMEOUT_MILLISECONDS,
  1021. .forgetAfterMilliseconds = FORGET_AFTER_MILLISECONDS,
  1022. .beaconInterval = BEACON_INTERVAL,
  1023. .enableNoise = enableNoise,
  1024. .linkStateInterval = Timeout_setInterval(
  1025. linkState,
  1026. out,
  1027. LINKSTATE_UPDATE_INTERVAL,
  1028. eventBase,
  1029. alloc),
  1030. .pingInterval = (switchPinger)
  1031. ? Timeout_setInterval(pingCycle,
  1032. out,
  1033. PING_INTERVAL_MILLISECONDS,
  1034. eventBase,
  1035. alloc)
  1036. : NULL,
  1037. .noiseHandshakeInterval = Timeout_setInterval(
  1038. handshakeCycle,
  1039. out,
  1040. HANDSHAKE_CYCLE_INTERVAL,
  1041. eventBase,
  1042. alloc)
  1043. }), sizeof(struct InterfaceController_pvt));
  1044. Identity_set(out);
  1045. out->icis = ArrayList_OfIfaces_new(alloc);
  1046. out->eventEmitterIf.send = incomingFromEventEmitterIf;
  1047. EventEmitter_regCore(ee, &out->eventEmitterIf, PFChan_Pathfinder_PEERS);
  1048. // Add the beaconing password.
  1049. Random_base32(rand, out->beacon.password, Headers_Beacon_PASSWORD_LEN);
  1050. String strPass = { .bytes=(char*)out->beacon.password, .len=Headers_Beacon_PASSWORD_LEN };
  1051. int ret = Ca_addUser(&strPass, String_CONST("Local Peers"), ca);
  1052. if (ret) {
  1053. Log_warn(logger, "Ca_addUser() returned [%d]", ret);
  1054. }
  1055. Ca_getPubKey(ca, out->ourPubKey);
  1056. Bits_memcpy(out->beacon.publicKey, out->ourPubKey, 32);
  1057. if (enableNoise) {
  1058. out->beacon.version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  1059. } else {
  1060. // this is mostly here for testing, we have to lie about our protocol version
  1061. out->beacon.version_be = Endian_hostToBigEndian32(21);
  1062. }
  1063. Timeout_setTimeout(beaconInterval, out, BEACON_INTERVAL, eventBase, alloc);
  1064. return &out->pub;
  1065. }