From b270dd4a2df384b29c2890289d82c114fcf67148 Mon Sep 17 00:00:00 2001 From: Marcel Verdult Date: Wed, 18 Nov 2026 12:10:32 +0100 Subject: [PATCH] Drop echoes of own datagrams to a peer that shares our hash Peer datagrams (TXT_MSG, REQ, RESPONSE, PATH) carry a 1-byte dest_hash and src_hash. If a contact's public key starts with the same byte as ours, a repeater echo of our own datagram to that contact matches self_id as destination, searchPeersByHash(src_hash) finds the contact, and the shared secret is symmetric, so the MAC checks out. onPeerDataRecv() then reports our own message as received from the contact and sends an ACK and a path return. The only guard today is the seen table, which is a ring of MAX_PACKET_HASHES entries shared with all received traffic. On a busy mesh the entry for our packet can be evicted before a late echo arrives. Keep a small separate ring of hashes of sent datagrams whose dest_hash equals our own hash. Normal traffic does not write to it, so these entries are not evicted by it. An incoming datagram with dest_hash == src_hash == our hash whose hash is in that ring is handled like a seen packet: not processed and not forwarded. Genuine datagrams from that contact differ in content and hash and are delivered as before. No wire format change. --- src/Mesh.cpp | 32 ++++++++++++++++++++++++++++++++ src/Mesh.h | 14 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 9dc42f0b04b..a58d52c6b85 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -30,6 +30,27 @@ uint32_t Mesh::getCADFailRetryDelay() const { return _rng->nextInt(1, 4)*120; } +static bool isPeerDatagramType(uint8_t type) { + return type == PAYLOAD_TYPE_PATH || type == PAYLOAD_TYPE_REQ || type == PAYLOAD_TYPE_RESPONSE || type == PAYLOAD_TYPE_TXT_MSG; +} + +void Mesh::rememberSelfAddressed(const Packet* packet) { + if (!isPeerDatagramType(packet->getPayloadType()) || packet->payload_len < 2) return; + if (!self_id.isHashMatch(&packet->payload[0])) return; // dest_hash differs from ours, seen table is enough + + packet->calculatePacketHash(_self_addressed_sent[_self_addressed_next]); + _self_addressed_next = (_self_addressed_next + 1) % MAX_SELF_ADDRESSED_SENT; +} + +bool Mesh::isOwnSelfAddressedEcho(const Packet* packet) { + uint8_t hash[MAX_HASH_SIZE]; + packet->calculatePacketHash(hash); + for (int i = 0; i < MAX_SELF_ADDRESSED_SENT; i++) { + if (memcmp(hash, _self_addressed_sent[i], MAX_HASH_SIZE) == 0) return true; + } + return false; +} + int Mesh::searchPeersByHash(const uint8_t* hash) { return 0; // not found } @@ -140,6 +161,12 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): incomplete data packet", getLogDateTime()); } else if (!_tables->wasSeen(pkt)) { _tables->markSeen(pkt); + if (dest_hash == src_hash && self_id.isHashMatch(&dest_hash) && isOwnSelfAddressedEcho(pkt)) { + // our own datagram to a peer that shares our hash, echoed back after its seen-table entry was + // evicted. It would decrypt as sent by that peer, so handle it like a seen packet: drop it. + MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): dropping echo of own datagram, hash=%02X", getLogDateTime(), (uint32_t)dest_hash); + break; + } // NOTE: this is a 'first packet wins' impl. When receiving from multiple paths, the first to arrive wins. // For flood mode, the path may not be the 'best' in terms of hops. // FUTURE: could send back multiple paths, using createPathReturn(), and let sender choose which to use(?) @@ -649,6 +676,7 @@ void Mesh::sendFlood(Packet* packet, uint32_t delay_millis, uint8_t path_hash_si packet->setPathHashSizeAndCount(path_hash_size, 0); _tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us + rememberSelfAddressed(packet); uint8_t pri; if (packet->getPayloadType() == PAYLOAD_TYPE_PATH) { @@ -678,6 +706,7 @@ void Mesh::sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_m packet->setPathHashSizeAndCount(path_hash_size, 0); _tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us + rememberSelfAddressed(packet); uint8_t pri; if (packet->getPayloadType() == PAYLOAD_TYPE_PATH) { @@ -711,6 +740,7 @@ void Mesh::sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uin } } _tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us + rememberSelfAddressed(packet); sendPacket(packet, pri, delay_millis); } @@ -721,6 +751,7 @@ void Mesh::sendZeroHop(Packet* packet, uint32_t delay_millis) { packet->path_len = 0; // path_len of zero means Zero Hop _tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us + rememberSelfAddressed(packet); sendPacket(packet, 0, delay_millis); } @@ -734,6 +765,7 @@ void Mesh::sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay packet->path_len = 0; // path_len of zero means Zero Hop _tables->markSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us + rememberSelfAddressed(packet); sendPacket(packet, 0, delay_millis); } diff --git a/src/Mesh.h b/src/Mesh.h index 22bb5c6d592..d8f56ac7f9e 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -24,11 +24,23 @@ class MeshTables { * \brief The next layer in the basic Dispatcher task, Mesh recognises the particular Payload TYPES, * and provides virtual methods for sub-classes on handling incoming, and also preparing outbound Packets. */ +#ifndef MAX_SELF_ADDRESSED_SENT + #define MAX_SELF_ADDRESSED_SENT 8 +#endif + class Mesh : public Dispatcher { RTCClock* _rtc; RNG* _rng; MeshTables* _tables; + // Hashes of recently sent datagrams whose dest_hash equals our own hash (a peer shares our first + // pub_key byte). The shared secret is symmetric, so an echo of such a packet decrypts as if that peer + // sent it. Kept apart from _tables, so normal traffic cannot evict these entries. + uint8_t _self_addressed_sent[MAX_SELF_ADDRESSED_SENT][MAX_HASH_SIZE]; + uint8_t _self_addressed_next; + + void rememberSelfAddressed(const Packet* packet); + bool isOwnSelfAddressedEcho(const Packet* packet); void removeSelfFromPath(Packet* packet); void routeDirectRecvAcks(Packet* packet, uint32_t delay_millis); //void routeRecvAcks(Packet* packet, uint32_t delay_millis); @@ -169,6 +181,8 @@ class Mesh : public Dispatcher { Mesh(Radio& radio, MillisecondClock& ms, RNG& rng, RTCClock& rtc, PacketManager& mgr, MeshTables& tables) : Dispatcher(radio, ms, mgr), _rng(&rng), _rtc(&rtc), _tables(&tables) { + memset(_self_addressed_sent, 0, sizeof(_self_addressed_sent)); + _self_addressed_next = 0; } MeshTables* getTables() const { return _tables; }