InterfaceController.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 = {0};
  541. Bits_memcpy(addr.key, beacon.publicKey, 32);
  542. addr.protocolVersion = Endian_bigEndianToHost32(beacon.version_be);
  543. Address_getPrefix(&addr);
  544. String* printedAddr = NULL;
  545. if (Defined(Log_DEBUG)) {
  546. printedAddr = Address_toString(&addr, Message_getAlloc(msg));
  547. }
  548. if (!AddressCalc_validAddress(addr.ip6.bytes)) {
  549. Log_debug(ic->logger, "handleBeacon invalid key [%s]", printedAddr->bytes);
  550. return Error(msg, "INVALID");
  551. } else if (!Bits_memcmp(ic->ourPubKey, addr.key, 32)) {
  552. // receive beacon from self, drop silent
  553. return NULL;
  554. }
  555. if (knownIncompatibleVersion(addr.protocolVersion)) {
  556. if (Defined(Log_DEBUG)) {
  557. Log_debug(ic->logger, "[%s] DROP beacon from [%s] which was version [%d] "
  558. "our version is [%d] making them incompatable", ici->pub.name->bytes,
  559. printedAddr->bytes, addr.protocolVersion, Version_CURRENT_PROTOCOL);
  560. }
  561. return Error(msg, "UNHANDLED");
  562. }
  563. String* beaconPass = String_newBinary(beacon.password, Headers_Beacon_PASSWORD_LEN, Message_getAlloc(msg));
  564. int epIndex = Map_EndpointsBySockaddr_indexForKey(&lladdr, &ici->peerMap);
  565. if (epIndex > -1) {
  566. // The password might have changed!
  567. struct Peer* ep = ici->peerMap.values[epIndex];
  568. Ca_setAuth(beaconPass, String_CONST("Local Peers"), ep->caSession);
  569. return NULL;
  570. }
  571. bool useNoise = addr.protocolVersion >= 22;
  572. struct Peer* ep = mkEp(lladdr, ici, beacon.publicKey, false, "beacon_peer", useNoise);
  573. int setIndex = Map_EndpointsBySockaddr_put(&ep->lladdr, &ep, &ici->peerMap);
  574. ep->handle = ici->peerMap.handles[setIndex];
  575. // We make the connection ourselves but we still consider
  576. // it "incoming" because we replied to a beacon
  577. ep->isIncomingConnection = true;
  578. ep->addr.protocolVersion = addr.protocolVersion;
  579. Ca_setAuth(beaconPass, String_CONST("Local Peers"), ep->caSession);
  580. if (SwitchCore_addInterface(ic->switchCore, &ep->switchIf, ep->alloc, &ep->addr.path)) {
  581. Log_debug(ic->logger, "handleBeacon() SwitchCore out of space");
  582. Allocator_free(ep->alloc);
  583. return Error(msg, "UNHANDLED");
  584. }
  585. // We want the node to immedietly be pinged but we don't want it to appear unresponsive because
  586. // the pinger will only ping every (PING_INTERVAL * 8) so we set timeOfLastMessage to
  587. // (now - pingAfterMilliseconds - 1) so it will be considered a "lazy node".
  588. ep->timeOfLastMessage =
  589. Time_currentTimeMilliseconds() - ic->pingAfterMilliseconds - 1;
  590. Log_info(ic->logger, "Added peer [%s] from beacon",
  591. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes);
  592. // Ping them immediately, this prevents beacon tests from taking 1 second each
  593. sendPing(ep);
  594. return NULL;
  595. }
  596. static Iface_DEFUN handleIncomingFromWire(struct Message* msg, struct Iface* addrIf)
  597. {
  598. struct InterfaceController_Iface_pvt* ici =
  599. Identity_containerOf(addrIf, struct InterfaceController_Iface_pvt, pub.addrIf);
  600. struct Sockaddr_storage lladdrStore;
  601. struct Sockaddr* lladdr = (struct Sockaddr*) &lladdrStore;
  602. {
  603. struct Sockaddr* lladdr0 = (struct Sockaddr*) msg->msgbytes;
  604. if (Message_getLength(msg) < Sockaddr_OVERHEAD || Message_getLength(msg) < lladdr0->addrLen) {
  605. Log_debug(ici->ic->logger, "DROP runt");
  606. return Error(msg, "RUNT");
  607. }
  608. Er_assert(Message_epop(msg, lladdr, lladdr0->addrLen));
  609. }
  610. Assert_true(!((uintptr_t)msg->msgbytes % 4) && "alignment fault");
  611. Assert_true(!((uintptr_t)lladdr->addrLen % 4) && "alignment fault");
  612. char* printedAddr = "<unknown>";
  613. if (Defined(Log_DEBUG)) {
  614. printedAddr = Hex_print(&lladdr[1], lladdr->addrLen - Sockaddr_OVERHEAD, Message_getAlloc(msg));
  615. }
  616. // noisy
  617. if (Defined(Log_DEBUG) && false) {
  618. Log_debug(ici->ic->logger, "Incoming message from [%s]", printedAddr);
  619. }
  620. if (lladdr->flags & Sockaddr_flags_BCAST) {
  621. return handleBeacon(msg, ici, lladdr);
  622. }
  623. if (Message_getLength(msg) < 4) {
  624. return Error(msg, "RUNT");
  625. }
  626. int epIndex = Map_EndpointsBySockaddr_indexForKey(&lladdr, &ici->peerMap);
  627. if (epIndex == -1) {
  628. // noise control message
  629. Er_assert(Message_epush(msg, NULL, 16));
  630. Sockaddr_asIp6(msg->msgbytes, lladdr);
  631. RTypes_CryptoAuth2_TryHandshake_Ret_t ret = { .code = 0 };
  632. Rffi_CryptoAuth2_tryHandshake(ici->ic->ca, msg, ici->alloc, true, &ret);
  633. if (ret.sess) {
  634. // We have a new session, setup the endpoint
  635. struct Peer* ep = epFromSess(lladdr, ici, ret.sess, ret.alloc);
  636. if (SwitchCore_addInterface(ici->ic->switchCore, &ep->switchIf, ep->alloc, &ep->addr.path)) {
  637. Log_debug(ici->ic->logger, "handleUnexpectedIncoming() SwitchCore out of space");
  638. Allocator_free(ep->alloc);
  639. return Error(msg, "UNHANDLED");
  640. }
  641. Assert_true(Map_EndpointsBySockaddr_indexForKey(&ep->lladdr, &ici->peerMap) == -1);
  642. int index = Map_EndpointsBySockaddr_put(&ep->lladdr, &ep, &ici->peerMap);
  643. Assert_true(index >= 0);
  644. ep->handle = ici->peerMap.handles[index];
  645. // We want the node to immedietly be pinged but we don't want it to appear unresponsive because
  646. // the pinger will only ping every (PING_INTERVAL * 8) so we set timeOfLastMessage to
  647. // (now - pingAfterMilliseconds - 1) so it will be considered a "lazy node".
  648. ep->timeOfLastMessage =
  649. Time_currentTimeMilliseconds() - ici->ic->pingAfterMilliseconds - 1;
  650. Log_info(ici->ic->logger, "Added peer [%s] from incoming message",
  651. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes);
  652. if (ep->addr.protocolVersion) {
  653. // This will only work if the other end sent us their version (WG mode)
  654. sendPeer(0xffffffff, PFChan_Core_PEER, ep, 0xffff);
  655. } else {
  656. // We don't know their version, ping them to find out
  657. sendPing(ep);
  658. }
  659. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_RecvPlaintext) {
  660. // receive the packet
  661. return afterDecrypt(msg, &ep->plaintext);
  662. }
  663. }
  664. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_ReplyToPeer) {
  665. // Send back a reply to the node who sent us this packet
  666. Er_assert(Message_epush(msg, lladdr, lladdr->addrLen));
  667. return Iface_next(&ici->pub.addrIf, msg);
  668. }
  669. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_Error) {
  670. Log_debug(ici->ic->logger, "Error on unexpected packet from [%s]: [%d]",
  671. printedAddr, ret.err);
  672. return Error(msg, "DECRYPT");
  673. }
  674. if (ret.code == RTypes_CryptoAuth2_TryHandshake_Code_t_Done) {
  675. // Nothing to do
  676. return NULL;
  677. }
  678. Assert_failure("Rffi_CryptoAuth2_tryHandshake() replied [%d]", ret.code);
  679. }
  680. struct Peer* ep = Identity_check((struct Peer*) ici->peerMap.values[epIndex]);
  681. // Once we know it to be an incompetible version, we quarentine it
  682. if (knownIncompatibleVersion(ep->addr.protocolVersion)) {
  683. if (Defined(Log_DEBUG)) {
  684. Log_debug(ici->ic->logger, "[%s] DROP msg from node with incompat version [%d] ",
  685. Address_toString(&ep->addr, Message_getAlloc(msg))->bytes, ep->addr.protocolVersion);
  686. }
  687. ep->state = InterfaceController_PeerState_INCOMPATIBLE;
  688. return NULL;
  689. }
  690. Ca_resetIfTimeout(ep->caSession);
  691. Er_assert(Message_epush(msg, NULL, 16));
  692. Sockaddr_asIp6(msg->msgbytes, lladdr);
  693. return Iface_next(&ep->ciphertext, msg); // -> afterDecrypt
  694. }
  695. // Expects result of CryptoAuth decrypt
  696. static Iface_DEFUN afterDecrypt(struct Message* msg, struct Iface* plaintext)
  697. {
  698. struct Peer* ep = Identity_containerOf(plaintext, struct Peer, plaintext);
  699. struct InterfaceController_Iface_pvt* ici = Identity_check(ep->ici);
  700. struct InterfaceController_pvt* ic = Identity_check(ici->ic);
  701. enum Ca_DecryptErr err = Er_assert(Message_epop32h(msg));
  702. if (err) {
  703. return Error(msg, "AUTHENTICATION");
  704. }
  705. Kbps_accumulate(&ep->recvBw, Time_currentTimeMilliseconds(), Message_getLength(msg));
  706. ep->bytesIn += Message_getLength(msg);
  707. int caState = Ca_getState(ep->caSession);
  708. if (caState != Ca_State_ESTABLISHED) {
  709. // prevent some kinds of nasty things which could be done with packet replay.
  710. // This is checking the message switch header and will drop it unless the label
  711. // directs it to *this* router.
  712. if (Message_getLength(msg) < 8 || msg->msgbytes[7] != 1) {
  713. Log_info(ic->logger, "DROP message because CA is not established.");
  714. return Error(msg, "UNHANDLED");
  715. } else {
  716. // When a "server" gets a new connection from a "client" the router doesn't
  717. // know about that client so if the client sends a packet to the server, the
  718. // server will be unable to handle it until the client has sent inter-router
  719. // communication to the server. Here we will ping the client so when the
  720. // server gets the ping response, it will insert the client into its table
  721. // and know its version.
  722. // prevent DoS by limiting the number of times this can be called per second
  723. // limit it to 7, this will affect innocent packets but it doesn't matter much
  724. // since this is mostly just an optimization and for keeping the tests happy.
  725. if ((ep->pingCount + 1) % 7) {
  726. sendPing(ep);
  727. }
  728. }
  729. } else {
  730. if (ep->state != caState) {
  731. sendPeer(0xffffffff, PFChan_Core_PEER, ep, 0xffff);
  732. }
  733. ep->timeOfLastMessage = Time_currentTimeMilliseconds();
  734. }
  735. ep->state = caState;
  736. Identity_check(ep);
  737. return Iface_next(&ep->switchIf, msg);
  738. }
  739. int InterfaceController_ifaceCount(struct InterfaceController* ifc)
  740. {
  741. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  742. return ic->icis->length;
  743. }
  744. struct InterfaceController_Iface* InterfaceController_getIface(struct InterfaceController* ifc,
  745. int ifNum)
  746. {
  747. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  748. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, ifNum);
  749. return (ici) ? &ici->pub : NULL;
  750. }
  751. struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceController* ifc,
  752. String* name,
  753. struct Allocator* alloc)
  754. {
  755. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  756. struct InterfaceController_Iface_pvt* ici =
  757. Allocator_calloc(alloc, sizeof(struct InterfaceController_Iface_pvt), 1);
  758. ici->pub.name = String_clone(name, alloc);
  759. ici->peerMap.allocator = alloc;
  760. ici->ic = ic;
  761. ici->alloc = alloc;
  762. ici->pub.addrIf.send = handleIncomingFromWire;
  763. ici->pub.ifNum = ArrayList_OfIfaces_add(ic->icis, ici);
  764. Identity_set(ici);
  765. return &ici->pub;
  766. }
  767. static void sendBeacon(struct InterfaceController_Iface_pvt* ici, struct Allocator* tempAlloc)
  768. {
  769. if (ici->pub.beaconState < InterfaceController_beaconState_newState_SEND) {
  770. Log_debug(ici->ic->logger, "sendBeacon(%s) -> beaconing disabled", ici->pub.name->bytes);
  771. return;
  772. }
  773. Log_debug(ici->ic->logger, "sendBeacon(%s)", ici->pub.name->bytes);
  774. struct Message* msg = Message_new(0, 128, tempAlloc);
  775. Er_assert(Message_epush(msg, &ici->ic->beacon, Headers_Beacon_SIZE));
  776. if (Defined(Log_DEBUG)) {
  777. char* content = Hex_print(msg->msgbytes, Message_getLength(msg), tempAlloc);
  778. Log_debug(ici->ic->logger, "SEND BEACON CONTENT[%s]", content);
  779. }
  780. struct Sockaddr sa = {
  781. .addrLen = Sockaddr_OVERHEAD,
  782. .flags = Sockaddr_flags_BCAST
  783. };
  784. Er_assert(Message_epush(msg, &sa, Sockaddr_OVERHEAD));
  785. Iface_send(&ici->pub.addrIf, msg);
  786. }
  787. static void handshakeCycle(void* vInterfaceController)
  788. {
  789. struct InterfaceController_pvt* ic =
  790. Identity_check((struct InterfaceController_pvt*) vInterfaceController);
  791. struct Allocator* alloc = Allocator_child(ic->alloc);
  792. for (int i = 0; i < ic->icis->length; i++) {
  793. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  794. for (uint32_t j = 0; j < ici->peerMap.count; j++) {
  795. struct Peer* ep = Identity_check((struct Peer*) ici->peerMap.values[j]);
  796. Message_t* msg = Rffi_CryptoAuth2_noiseTick(ep->caSession, alloc);
  797. if (msg != NULL) {
  798. Er_assert(Message_epush(msg, ep->lladdr, ep->lladdr->addrLen));
  799. Iface_send(&ep->ici->pub.addrIf, msg);
  800. }
  801. }
  802. }
  803. Allocator_free(alloc);
  804. }
  805. static void beaconInterval(void* vInterfaceController)
  806. {
  807. struct InterfaceController_pvt* ic =
  808. Identity_check((struct InterfaceController_pvt*) vInterfaceController);
  809. struct Allocator* alloc = Allocator_child(ic->alloc);
  810. for (int i = 0; i < ic->icis->length; i++) {
  811. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  812. sendBeacon(ici, alloc);
  813. }
  814. Allocator_free(alloc);
  815. if (ic->beaconTimeoutAlloc) {
  816. Allocator_free(ic->beaconTimeoutAlloc);
  817. }
  818. ic->beaconTimeoutAlloc = Allocator_child(ic->alloc);
  819. Timeout_setTimeout(
  820. beaconInterval, ic, ic->beaconInterval, ic->eventBase, ic->beaconTimeoutAlloc);
  821. }
  822. int InterfaceController_beaconState(struct InterfaceController* ifc,
  823. int interfaceNumber,
  824. int newState)
  825. {
  826. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  827. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, interfaceNumber);
  828. if (!ici) {
  829. return InterfaceController_beaconState_NO_SUCH_IFACE;
  830. }
  831. char* val = NULL;
  832. switch (newState) {
  833. default: return InterfaceController_beaconState_INVALID_STATE;
  834. case InterfaceController_beaconState_newState_OFF: val = "OFF"; break;
  835. case InterfaceController_beaconState_newState_ACCEPT: val = "ACCEPT"; break;
  836. case InterfaceController_beaconState_newState_SEND: val = "SEND"; break;
  837. }
  838. Log_debug(ic->logger, "InterfaceController_beaconState(%s, %s)", ici->pub.name->bytes, val);
  839. ici->pub.beaconState = newState;
  840. if (newState == InterfaceController_beaconState_newState_SEND) {
  841. // Send out a beacon right away so we don't have to wait.
  842. struct Allocator* alloc = Allocator_child(ici->alloc);
  843. sendBeacon(ici, alloc);
  844. Allocator_free(alloc);
  845. }
  846. return 0;
  847. }
  848. int InterfaceController_bootstrapPeer(struct InterfaceController* ifc,
  849. int interfaceNumber,
  850. uint8_t* herPublicKey,
  851. const struct Sockaddr* lladdrParm,
  852. String* password,
  853. String* login,
  854. String* user,
  855. int version)
  856. {
  857. struct InterfaceController_pvt* ic = Identity_check((struct InterfaceController_pvt*) ifc);
  858. Assert_true(herPublicKey);
  859. Assert_true(password);
  860. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, interfaceNumber);
  861. if (!ici) {
  862. return InterfaceController_bootstrapPeer_BAD_IFNUM;
  863. }
  864. Log_debug(ic->logger, "bootstrapPeer total [%u]", ici->peerMap.count);
  865. uint8_t ip6[16];
  866. AddressCalc_addressForPublicKey(ip6, herPublicKey);
  867. if (!AddressCalc_validAddress(ip6) || !Bits_memcmp(ic->ourPubKey, herPublicKey, 32)) {
  868. return InterfaceController_bootstrapPeer_BAD_KEY;
  869. }
  870. // We often don't know this, but in that case we will fallback to the old peering
  871. bool useNoise = version >= 22;
  872. struct Peer* ep = mkEp(lladdrParm, ici, herPublicKey, false, user ? user->bytes : NULL, useNoise);
  873. ep->addr.protocolVersion = version;
  874. int index = Map_EndpointsBySockaddr_put(&ep->lladdr, &ep, &ici->peerMap);
  875. Assert_true(index >= 0);
  876. ep->handle = ici->peerMap.handles[index];
  877. Ca_setAuth(password, login, ep->caSession);
  878. if (SwitchCore_addInterface(ic->switchCore, &ep->switchIf, ep->alloc, &ep->addr.path)) {
  879. Log_debug(ic->logger, "bootstrapPeer() SwitchCore out of space");
  880. Allocator_free(ep->alloc);
  881. return InterfaceController_bootstrapPeer_OUT_OF_SPACE;
  882. }
  883. // We want the node to immedietly be pinged but we don't want it to appear unresponsive because
  884. // the pinger will only ping every (PING_INTERVAL * 8) so we set timeOfLastMessage to
  885. // (now - pingAfterMilliseconds - 1) so it will be considered a "lazy node".
  886. ep->timeOfLastMessage =
  887. Time_currentTimeMilliseconds() - ic->pingAfterMilliseconds - 1;
  888. if (Defined(Log_INFO)) {
  889. struct Allocator* tempAlloc = Allocator_child(ep->alloc);
  890. String* addrStr = Address_toString(&ep->addr, tempAlloc);
  891. Log_info(ic->logger, "Adding peer [%s] from bootstrapPeer()", addrStr->bytes);
  892. Allocator_free(tempAlloc);
  893. }
  894. // We can't just add the node directly to the routing table because we do not know
  895. // the version. We'll send it a switch ping and when it responds, we will know it's
  896. // key (if we don't already) and version number.
  897. sendPing(ep);
  898. return 0;
  899. }
  900. int InterfaceController_getPeerStats(struct InterfaceController* ifController,
  901. struct Allocator* alloc,
  902. struct InterfaceController_PeerStats** statsOut)
  903. {
  904. struct InterfaceController_pvt* ic =
  905. Identity_check((struct InterfaceController_pvt*) ifController);
  906. int count = 0;
  907. for (int i = 0; i < ic->icis->length; i++) {
  908. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, i);
  909. count += ici->peerMap.count;
  910. }
  911. struct InterfaceController_PeerStats* stats =
  912. Allocator_calloc(alloc, sizeof(struct InterfaceController_PeerStats), count);
  913. uint32_t now = Time_currentTimeMilliseconds();
  914. int xcount = 0;
  915. for (int j = 0; j < ic->icis->length; j++) {
  916. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  917. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  918. struct Peer* peer = Identity_check((struct Peer*) ici->peerMap.values[i]);
  919. struct InterfaceController_PeerStats* s = &stats[xcount];
  920. xcount++;
  921. s->ifNum = ici->pub.ifNum;
  922. s->lladdr = Sockaddr_clone(peer->lladdr, alloc);
  923. Bits_memcpy(&s->addr, &peer->addr, sizeof(struct Address));
  924. s->bytesOut = peer->bytesOut;
  925. s->bytesIn = peer->bytesIn;
  926. s->timeOfLastMessage = peer->timeOfLastMessage;
  927. s->state = peer->state;
  928. s->isIncomingConnection = peer->isIncomingConnection;
  929. s->user = Ca_getName(peer->caSession, alloc);
  930. RTypes_CryptoStats_t stats;
  931. Ca_stats(peer->caSession, &stats);
  932. s->duplicates = stats.duplicate_packets;
  933. s->receivedOutOfRange = stats.received_unexpected;
  934. s->noiseProto = stats.noise_proto;
  935. s->recvKbps = Kbps_accumulate(&peer->recvBw, now, Kbps_accumulate_NO_PACKET);
  936. s->sendKbps = Kbps_accumulate(&peer->sendBw, now, Kbps_accumulate_NO_PACKET);
  937. s->receivedPackets = peer->lastPackets;
  938. s->lostPackets = peer->lastDrops;
  939. }
  940. }
  941. Assert_true(xcount == count);
  942. *statsOut = stats;
  943. return count;
  944. }
  945. void InterfaceController_resetPeering(struct InterfaceController* ifController,
  946. uint8_t herPublicKey[32])
  947. {
  948. struct InterfaceController_pvt* ic =
  949. Identity_check((struct InterfaceController_pvt*) ifController);
  950. for (int j = 0; j < ic->icis->length; j++) {
  951. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  952. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  953. struct Peer* peer = ici->peerMap.values[i];
  954. if (!herPublicKey || !Bits_memcmp(herPublicKey, peer->addr.key, 32)) {
  955. Ca_reset(peer->caSession);
  956. }
  957. }
  958. }
  959. }
  960. int InterfaceController_disconnectPeer(struct InterfaceController* ifController,
  961. uint8_t herPublicKey[32])
  962. {
  963. struct InterfaceController_pvt* ic =
  964. Identity_check((struct InterfaceController_pvt*) ifController);
  965. int count = 0;
  966. for (int j = 0; j < ic->icis->length; j++) {
  967. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  968. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  969. struct Peer* peer = ici->peerMap.values[i];
  970. if (!Bits_memcmp(herPublicKey, peer->addr.key, 32)) {
  971. Allocator_free(peer->alloc);
  972. count++;
  973. }
  974. }
  975. }
  976. return count;
  977. }
  978. static Iface_DEFUN incomingFromEventEmitterIf(struct Message* msg, struct Iface* eventEmitterIf)
  979. {
  980. struct InterfaceController_pvt* ic =
  981. Identity_containerOf(eventEmitterIf, struct InterfaceController_pvt, eventEmitterIf);
  982. uint32_t peers = Er_assert(Message_epop32be(msg));
  983. Assert_true(peers == PFChan_Pathfinder_PEERS);
  984. uint32_t pathfinderId = Er_assert(Message_epop32be(msg));
  985. Assert_true(!Message_getLength(msg));
  986. for (int j = 0; j < ic->icis->length; j++) {
  987. struct InterfaceController_Iface_pvt* ici = ArrayList_OfIfaces_get(ic->icis, j);
  988. for (int i = 0; i < (int)ici->peerMap.count; i++) {
  989. struct Peer* peer = Identity_check((struct Peer*) ici->peerMap.values[i]);
  990. if (peer->state != InterfaceController_PeerState_ESTABLISHED) { continue; }
  991. sendPeer(pathfinderId, PFChan_Core_PEER, peer, 0xffff);
  992. }
  993. }
  994. return NULL;
  995. }
  996. struct InterfaceController* InterfaceController_new(Ca_t* ca,
  997. struct SwitchCore* switchCore,
  998. struct Log* logger,
  999. struct EventBase* eventBase,
  1000. struct SwitchPinger* switchPinger,
  1001. struct Random* rand,
  1002. struct Allocator* allocator,
  1003. struct EventEmitter* ee,
  1004. bool enableNoise)
  1005. {
  1006. struct Allocator* alloc = Allocator_child(allocator);
  1007. struct InterfaceController_pvt* out =
  1008. Allocator_calloc(alloc, sizeof(struct InterfaceController_pvt), 1);
  1009. Bits_memcpy(out, (&(struct InterfaceController_pvt) {
  1010. .alloc = alloc,
  1011. .ca = ca,
  1012. .rand = rand,
  1013. .switchCore = switchCore,
  1014. .logger = logger,
  1015. .eventBase = eventBase,
  1016. .switchPinger = switchPinger,
  1017. .unresponsiveAfterMilliseconds = UNRESPONSIVE_AFTER_MILLISECONDS,
  1018. .pingAfterMilliseconds = PING_AFTER_MILLISECONDS,
  1019. .timeoutMilliseconds = TIMEOUT_MILLISECONDS,
  1020. .forgetAfterMilliseconds = FORGET_AFTER_MILLISECONDS,
  1021. .beaconInterval = BEACON_INTERVAL,
  1022. .enableNoise = enableNoise,
  1023. .linkStateInterval = Timeout_setInterval(
  1024. linkState,
  1025. out,
  1026. LINKSTATE_UPDATE_INTERVAL,
  1027. eventBase,
  1028. alloc),
  1029. .pingInterval = (switchPinger)
  1030. ? Timeout_setInterval(pingCycle,
  1031. out,
  1032. PING_INTERVAL_MILLISECONDS,
  1033. eventBase,
  1034. alloc)
  1035. : NULL,
  1036. .noiseHandshakeInterval = Timeout_setInterval(
  1037. handshakeCycle,
  1038. out,
  1039. HANDSHAKE_CYCLE_INTERVAL,
  1040. eventBase,
  1041. alloc)
  1042. }), sizeof(struct InterfaceController_pvt));
  1043. Identity_set(out);
  1044. out->icis = ArrayList_OfIfaces_new(alloc);
  1045. out->eventEmitterIf.send = incomingFromEventEmitterIf;
  1046. EventEmitter_regCore(ee, &out->eventEmitterIf, PFChan_Pathfinder_PEERS);
  1047. // Add the beaconing password.
  1048. Random_base32(rand, out->beacon.password, Headers_Beacon_PASSWORD_LEN);
  1049. String strPass = { .bytes=(char*)out->beacon.password, .len=Headers_Beacon_PASSWORD_LEN };
  1050. int ret = Ca_addUser(&strPass, String_CONST("Local Peers"), ca);
  1051. if (ret) {
  1052. Log_warn(logger, "Ca_addUser() returned [%d]", ret);
  1053. }
  1054. Ca_getPubKey(ca, out->ourPubKey);
  1055. Bits_memcpy(out->beacon.publicKey, out->ourPubKey, 32);
  1056. if (enableNoise) {
  1057. out->beacon.version_be = Endian_hostToBigEndian32(Version_CURRENT_PROTOCOL);
  1058. } else {
  1059. // this is mostly here for testing, we have to lie about our protocol version
  1060. out->beacon.version_be = Endian_hostToBigEndian32(21);
  1061. }
  1062. Timeout_setTimeout(beaconInterval, out, BEACON_INTERVAL, eventBase, alloc);
  1063. return &out->pub;
  1064. }