/* NAT-детектор владельца endpoint. Один раунд на рабочий сокет: * S->B observation, B->C ingress(S), S->C/C-alt observations, S->B repeat. * Команды к C проходят через B: контроль не открывает фильтр S->C. */ #include "nat_detection.h" #include #include #include "utun_instance.h" #include "config_parser.h" #include "etcp_api.h" #include "etcp.h" #include "etcp_connections.h" #include "route_ping.h" #include "topo_node.h" #include "topo_group.h" #include "etcp_router.h" #include "route_crypto.h" #include "stcp_client.h" #include "stun_nat.h" #include "../lib/debug_config.h" #include "../lib/mem.h" #include "../lib/u_async.h" #define NAT_WIRE_VERSION 3 #define NAT_MAX_BINDINGS 128 #define NAT_MAX_PROBES 64 #define NAT_MAX_CONTACTS 256 #define NAT_TICK_TB 1000 #define NAT_ROUND_TB 300000 #define NAT_REFRESH_TB 18000000 #define NAT_RETRY_TB 300000 #define NAT_PROBE_COUNT 3 #define NAT_QUERY_TAG_SIZE (ROUTE_OBSERVE_TAG_SIZE + 9) enum nat_command { NAT_RUN = 1, NAT_RESULT }; enum nat_phase { NAT_IDLE, NAT_OBSERVE_B, NAT_PREPARE_C, NAT_PRE, NAT_OBSERVE_C, NAT_PREPARE_ALT, NAT_OBSERVE_ALT, NAT_POST, NAT_REPEAT_B, NAT_TCP }; enum nat_step { NAT_STEP_PREPARE = 1, NAT_STEP_PRE, NAT_STEP_ALT, NAT_STEP_POST, NAT_STEP_TCP }; /* Только байты: формат независим от alignment/endian платформы. */ struct nat_wire { uint8_t cmd, command, version, owner_socket, helper_socket, step, reason, sent, received, errors; uint8_t owner[8], generation[8], round[8], token[8], observer[8], helper[8], timeout_ms[2]; uint8_t target[ROUTE_ADDR_WIRE_SIZE], primary[ROUTE_ADDR_WIRE_SIZE], secondary[ROUTE_ADDR_WIRE_SIZE]; uint8_t observer_address[ROUTE_ADDR_WIRE_SIZE]; }; struct nat_pair { uint64_t group, observer, helper; }; struct nat_socket { struct nat_socket *next; struct NAT_DETECTION *detector; struct ETCP_SOCKET *socket, *helper_primary, *auxiliary; struct sockaddr_storage local, observer_address, helper_address, alternate_address; struct sockaddr_storage baseline, helper_seen, alternate_seen; struct sockaddr_storage measured_address; struct address_profile measured_profile; uint64_t select_due, available_fingerprint, reservation_fingerprint, cold_until; uint8_t fallback_allowed; uint8_t observer_key[32], helper_key[32]; uint64_t group_id, round, observer, helper, token, deadline, due, completed_tb, preferred_observer; struct nat_pair cursor, fallback; uint8_t contacts[NAT_MAX_CONTACTS][17]; unsigned contact_count, phase, reason, retries; uint8_t history_loaded, history_complete, history_incomplete, filter_clean, pre_received, post_received, pre_healthy; uint8_t helper_socket, active_ping, forced, cycling, using_fallback, prefer_fresh; uint32_t rounds, helper_reselections; }; struct nat_binding { struct nat_binding *next; uint64_t group_id, owner, generation, round, expires, helper, token; uint8_t owner_socket, owner_key[32]; struct sockaddr_storage target; }; struct nat_probe { struct nat_probe *next; struct NAT_DETECTION *detector; struct nat_wire message; uint64_t group_id, reply, source_generation, io_generation, due, expires; uint8_t owner_key[32], observer_key[32]; uint8_t source_socket, io_socket, active, cancelled, alternate_fallback; struct stcp_client *tcp; }; struct NAT_DETECTION { struct UTUN_INSTANCE *instance; struct nat_socket *sockets; struct nat_binding *bindings; struct nat_probe *probes; void *timer; void *topology_wake; uint64_t publish_after; uint8_t allow_local, stopping, history_table_ready, automatic, publish_pending, reservation_dirty; }; static void owner_ping_cb(int, uint16_t, void *, uint64_t, const uint8_t *, size_t); static void helper_ping_cb(int, uint16_t, void *, uint64_t, const uint8_t *, size_t); static void detector_tick(void *); static int owner_observe(struct nat_socket *); static int owner_run(struct nat_socket *, uint8_t); static void owner_finish(struct nat_socket *, uint8_t); static void helper_remove(struct nat_probe *); static int helper_send(struct nat_probe *); static void owner_start(struct nat_socket *); static int contacted(struct nat_socket *, const struct sockaddr_storage *); static uint64_t read64(const uint8_t *data) { uint64_t value = 0; for (unsigned i = 0; i < 8; i++) value = (value << 8) | data[i]; return value; } static void write64(uint8_t *data, uint64_t value) { for (unsigned i = 0; i < 8; i++) data[i] = (uint8_t)(value >> (56 - i * 8)); } static int random_id(uint64_t *value) { do { if (random_bytes((uint8_t *)value, sizeof(*value))) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT round/token RNG failed"); return -1; } } while (!*value); return 0; } const char *nat_check_reason_name(uint8_t reason) { static const char *const names[] = {"none", "no_observer", "no_helper", "control_lost", "send_error", "timeout", "address_changed", "contact_history", "proxy", "no_listener", "cancelled", "invalid_observation", "dependent_mapping"}; return reason < sizeof(names) / sizeof(names[0]) ? names[reason] : "invalid_reason"; } static struct ETCP_SOCKET *find_socket(struct NAT_DETECTION *detector, uint8_t id, uint64_t generation) { for (struct ETCP_SOCKET *socket = detector->instance->etcp_sockets; socket; socket = socket->next) if (socket->sock_id == id && (!generation || socket->generation == generation)) return socket; return NULL; } static int public_address(struct NAT_DETECTION *detector, const struct sockaddr_storage *address) { if (address->ss_family == AF_INET) { uint32_t ip = ((const struct sockaddr_in *)address)->sin_addr.s_addr; return ip && (ip_is_public(ip) || detector->allow_local); } if (address->ss_family == AF_INET6) { const uint8_t *ip = ((const struct sockaddr_in6 *)address)->sin6_addr.s6_addr; return (ip[0] & 0xe0) == 0x20 || (detector->allow_local && !memcmp(ip, &in6addr_loopback, 16)); } return 0; } /* Подписанная идентичность участника выбранной группы; сохранённый bootstrap не разрешает управление. */ static const struct TOPO_NODE *group_node(struct NAT_DETECTION *detector, uint64_t group_id, uint64_t node_id) { struct TOPO_GROUP *group = topo_groups_find(detector->instance->topo_groups, group_id); if (!group || group->stopping || (node_id != detector->instance->node_id && !topo_node_find_by_id(group, node_id))) return NULL; const struct TOPO_NODE *node = topo_node_registry_find(detector->instance->topo_groups, node_id); return node && node->timestamp ? node : NULL; } /* Управление допускает транзит и TCP, но следующий хоп должен завершить согласование этой группы. */ static int control_route(struct NAT_DETECTION *detector, uint64_t group_id, uint64_t node_id) { if (!group_node(detector, group_id, node_id)) return 0; struct ETCP_ROUTER_PATH path = etcp_router_get_path(detector->instance, group_id, node_id, ETCP_RT_ID_NAT_DETECTION); struct TOPO_GROUP *group = topo_groups_find(detector->instance->topo_groups, group_id); return path.type != CONN_TYPE_NONE && topo_group_peer_ready(group, path.next_hop_node_id); } /* Выбирает сокет по NODEINFO. Помощнику разрешён приватный endpoint: его mapping измеряется перед ingress. */ static int node_udp_endpoint(struct NAT_DETECTION *detector, const struct TOPO_NODE *node, int family, int allow_private, struct sockaddr_storage *address, uint8_t *socket_id) { int best = 4; for (const struct TOPO_ADDR4 *a = family == AF_INET ? node->v4_addrs : NULL; a; a = a->next) { if (!(a->protocol & TOPO_PROTO_UDP) || !a->port) continue; const struct TOPO_SOCKMETA4 *meta = node->v4_sock_meta; while (meta && meta->id != a->socket_id) meta = meta->next; if (meta && (meta->config_type == CFG_SERVER_TYPE_PRIVATE || meta->config_type == CFG_SERVER_TYPE_LOCAL)) continue; struct sockaddr_storage candidate = {0}; struct sockaddr_in *v4 = (struct sockaddr_in *)&candidate; v4->sin_family = AF_INET; memcpy(&v4->sin_addr, a->addr, 4); v4->sin_port = htons(a->port); int rank = public_address(detector, &candidate) ? 0 : 2; if ((!allow_private && rank) || rank > best || (rank == best && a->socket_id >= *socket_id)) continue; best = rank; *address = candidate; *socket_id = a->socket_id; } for (const struct TOPO_ADDR6 *a = family == AF_INET6 ? node->v6_addrs : NULL; a; a = a->next) { if (!(a->protocol & TOPO_PROTO_UDP) || !a->port) continue; const struct TOPO_SOCKMETA6 *meta = node->v6_sock_meta; while (meta && meta->id != a->socket_id) meta = meta->next; if (meta && (meta->config_type == CFG_SERVER_TYPE_PRIVATE || meta->config_type == CFG_SERVER_TYPE_LOCAL)) continue; struct sockaddr_storage candidate = {0}; struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)&candidate; v6->sin6_family = AF_INET6; memcpy(&v6->sin6_addr, a->addr, 16); v6->sin6_port = htons(a->port); int rank = public_address(detector, &candidate) ? 0 : 2; if ((!allow_private && rank) || rank > best || (rank == best && a->socket_id >= *socket_id)) continue; best = rank; *address = candidate; *socket_id = a->socket_id; } /* Наблюдаемый адрес уже работающего UDP помогает bootstrap за NAT; прямой линк не является условием отбора. */ struct ETCP_CONN *connection = instance_find_conn(detector->instance, node->node_id); for (struct ETCP_LINK *link = connection ? connection->links : NULL; link; link = link->next) { if (link->is_tcp || !link->initialized || !link->link_status || link->remote_only_local || link->remote_addr.ss_family != family || !public_address(detector, &link->remote_addr)) continue; if (best == 0) break; *address = link->remote_addr; *socket_id = link->remote_socket_id; best = 1; break; } return best < 4; } static int pair_after(struct nat_pair a, struct nat_pair b) { if (a.group != b.group) return a.group > b.group; if (a.observer != b.observer) return a.observer > b.observer; return a.helper > b.helper; } /* Обходит пары всех групп без ограничения числа узлов и без хранения указателей на топологию. */ static int select_pair(struct nat_socket *state) { struct NAT_DETECTION *detector = state->detector; struct nat_pair best = {0}; struct sockaddr_storage observer_address = {0}, helper_address = {0}; uint8_t observer_key[32] = {0}, helper_key[32] = {0}, helper_socket = 0; unsigned observers = 0, groups = 0; for (struct ll_entry *e = detector->instance->topo_groups->group_list->head; e; e = e->next) { struct TOPO_GROUP *group = (struct TOPO_GROUP *)e; if (group->stopping || !group->local_node) continue; groups++; for (struct ll_entry *b = group->nodes->head; b; b = b->next) { uint64_t observer = ((struct TOPO_GROUP_NODE *)b)->node_id; if (observer == detector->instance->node_id || (state->preferred_observer && observer != state->preferred_observer) || !control_route(detector, group->group_id, observer)) continue; const struct TOPO_NODE *bn = group_node(detector, group->group_id, observer); struct sockaddr_storage ba = {0}; uint8_t bs = 0; if (!state->socket->is_tcp && !node_udp_endpoint(detector, bn, state->local.ss_family, 0, &ba, &bs)) continue; observers++; for (struct ll_entry *c = state->socket->is_tcp ? b : group->nodes->head; c; c = state->socket->is_tcp ? NULL : c->next) { uint64_t helper = ((struct TOPO_GROUP_NODE *)c)->node_id; if (helper == detector->instance->node_id || (!state->socket->is_tcp && helper == observer) || !control_route(detector, group->group_id, helper)) continue; const struct TOPO_NODE *cn = group_node(detector, group->group_id, helper); struct sockaddr_storage ca = {0}; uint8_t cs = 0; if (!state->socket->is_tcp && !node_udp_endpoint(detector, cn, state->local.ss_family, 1, &ca, &cs)) continue; if (!state->socket->is_tcp && state->prefer_fresh && !state->using_fallback && contacted(state, &ca)) continue; struct nat_pair candidate = {group->group_id, observer, helper}; if (state->using_fallback) { if (memcmp(&candidate, &state->fallback, sizeof(candidate))) continue; } else if (!pair_after(candidate, state->cursor)) continue; if (best.group && !pair_after(best, candidate)) continue; best = candidate; observer_address = ba; helper_address = ca; helper_socket = cs; memcpy(observer_key, bn->public_key, 32); memcpy(helper_key, cn->public_key, 32); } } } if (!best.group) { if (state->prefer_fresh && !state->using_fallback) { state->prefer_fresh = 0; state->cursor = (struct nat_pair){0}; return select_pair(state); } if (!state->using_fallback && state->fallback.group) { state->using_fallback = 1; return select_pair(state); } state->reason = observers ? NAT_REASON_NO_HELPER : NAT_REASON_NO_OBSERVER; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT candidate cycle exhausted socket=%s groups=%u observers=%u reason=%s", state->socket->name, groups, observers, nat_check_reason_name(state->reason)); return -1; } if (!state->using_fallback) state->cursor = best; state->group_id = best.group; state->observer = best.observer; state->helper = best.helper; state->observer_address = observer_address; state->helper_address = helper_address; state->helper_socket = helper_socket; memcpy(state->observer_key, observer_key, 32); memcpy(state->helper_key, helper_key, 32); DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT candidates socket=%s group=%016llx observer=%016llx endpoint=%s helper=%016llx candidate=%s fallback=%u", state->socket->name, (unsigned long long)best.group, (unsigned long long)best.observer, sockaddr_storage_to_str(&observer_address).str, (unsigned long long)best.helper, sockaddr_storage_to_str(&helper_address).str, state->using_fallback); return 0; } static void publish(struct NAT_DETECTION *detector) { if (detector->stopping || !detector->instance->topo_groups) return; detector->publish_pending = topo_node_update_my_addresses(detector->instance) < 0; if (detector->publish_pending) detector->publish_after = get_time_tb() + 10000; if (detector->publish_pending) DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT profile publication failed; retry scheduled"); } static struct nat_socket *socket_state(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, int create) { if (!detector || !socket || socket->probe_only || detector->stopping) return NULL; for (struct nat_socket *state = detector->sockets; state; state = state->next) if (state->socket == socket) return state; if (!create) return NULL; struct nat_socket *state = u_calloc(1, sizeof(*state)); if (!state) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT endpoint state allocation failed socket=%s", socket->name); return NULL; } state->detector = detector; state->socket = socket; state->local = socket->local_addr; if (socket->is_tcp) state->local = socket->interface_addr; state->due = get_time_tb() + 2500; state->cold_until = state->due + NAT_ROUND_TB; state->next = detector->sockets; detector->sockets = state; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT endpoint created socket=%s id=%u generation=%016llx protocol=%s local=%s", socket->name, socket->sock_id, (unsigned long long)socket->generation, socket->is_tcp ? "TCP" : "UDP", sockaddr_storage_to_str(&state->local).str); return state; } /* История сохраняется один раз на новый IP. Перезапуск на том же IP:port её не очищает. */ static int history_table(struct NAT_DETECTION *detector) { if (detector->history_table_ready) return 0; sqlite3 *db = detector->instance->topo_sqlite_db; if (!db) return -1; if (sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS nat_contacts(local BLOB NOT NULL,remote BLOB NOT NULL," "PRIMARY KEY(local,remote)) WITHOUT ROWID", NULL, NULL, NULL) != SQLITE_OK) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT contact history initialization failed: %s", sqlite3_errmsg(db)); return -1; } detector->history_table_ready = 1; return 0; } static int history_insert(struct nat_socket *state, const uint8_t remote[17]) { uint8_t local[ROUTE_ADDR_WIRE_SIZE]; if (route_ping_addr_encode(local, &state->local) || history_table(state->detector)) return -1; sqlite3_stmt *statement = NULL; sqlite3 *db = state->detector->instance->topo_sqlite_db; if (sqlite3_prepare_v2(db, "INSERT OR IGNORE INTO nat_contacts(local,remote) VALUES(?,?)", -1, &statement, NULL) != SQLITE_OK) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT contact write failed: %s", sqlite3_errmsg(db)); return -1; } sqlite3_bind_blob(statement, 1, local, sizeof(local), SQLITE_TRANSIENT); sqlite3_bind_blob(statement, 2, remote, 17, SQLITE_TRANSIENT); int result = sqlite3_step(statement); sqlite3_finalize(statement); if (result != SQLITE_DONE) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT contact insert failed: %s", sqlite3_errmsg(db)); return -1; } return 0; } static void history_mark_incomplete(struct nat_socket *state) { static const uint8_t marker[17]; state->history_complete = 0; state->history_incomplete = 1; /* family=0 — постоянный маркер неполной истории, не адрес помощника. */ if (history_insert(state, marker)) DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT incomplete history marker persistence failed"); } static void history_load(struct nat_socket *state) { if (state->history_loaded || state->socket->is_tcp) return; struct NAT_DETECTION *detector = state->detector; if (history_table(detector)) { state->history_complete = 0; return; } uint8_t local[ROUTE_ADDR_WIRE_SIZE]; if (route_ping_addr_encode(local, &state->local)) return; sqlite3_stmt *statement = NULL; sqlite3 *db = detector->instance->topo_sqlite_db; if (sqlite3_prepare_v2(db, "SELECT remote FROM nat_contacts WHERE local=?", -1, &statement, NULL) != SQLITE_OK) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT history read failed: %s", sqlite3_errmsg(db)); return; } sqlite3_bind_blob(statement, 1, local, sizeof(local), SQLITE_TRANSIENT); state->history_complete = !state->history_incomplete; int result; while ((result = sqlite3_step(statement)) == SQLITE_ROW) { const uint8_t *remote = sqlite3_column_blob(statement, 0); if (!remote || sqlite3_column_bytes(statement, 0) != 17 || (remote[0] != 4 && remote[0] != 6)) { state->history_incomplete = 1; state->history_complete = 0; continue; } unsigned index; for (index = 0; index < state->contact_count; index++) if (!memcmp(state->contacts[index], remote, 17)) break; if (index < state->contact_count) continue; if (state->contact_count == NAT_MAX_CONTACTS) { state->history_incomplete = 1; state->history_complete = 0; continue; } memcpy(state->contacts[state->contact_count++], remote, 17); } if (result != SQLITE_DONE) { state->history_complete = 0; DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT history query failed: %s", sqlite3_errmsg(db)); } sqlite3_finalize(statement); state->history_loaded = 1; /* Сохраняем также отправки, которые случились до открытия общей SQLite. */ for (unsigned i = 0; i < state->contact_count; i++) if (history_insert(state, state->contacts[i])) { history_mark_incomplete(state); break; } if (state->history_incomplete) history_mark_incomplete(state); DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT history loaded socket=%s contacts=%u complete=%u", state->socket->name, state->contact_count, state->history_complete); } static int contacted(struct nat_socket *state, const struct sockaddr_storage *address) { uint8_t remote[ROUTE_ADDR_WIRE_SIZE]; if (!state->history_complete || route_ping_addr_encode(remote, address)) return 1; for (unsigned i = 0; i < state->contact_count; i++) if (!memcmp(state->contacts[i], remote, 17)) return 1; return 0; } void nat_detection_note_send(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, const struct sockaddr_storage *destination) { if (!detector || !socket || socket->is_tcp || socket->probe_only || socket->socks_udp || detector->stopping) return; struct nat_socket *state = socket_state(detector, socket, 1); if (!state) return; history_load(state); if (state->phase == NAT_PRE && !state->pre_received && route_ping_addr_equal(destination, &state->helper_address, 0)) { state->filter_clean = 0; DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT ingress contaminated by outgoing UDP socket=%s round=%016llx destination=%s", socket->name, (unsigned long long)state->round, sockaddr_storage_to_str(destination).str); } uint8_t local[ROUTE_ADDR_WIRE_SIZE], remote[ROUTE_ADDR_WIRE_SIZE]; if (route_ping_addr_encode(local, &state->local) || route_ping_addr_encode(remote, destination)) return; for (unsigned i = 0; i < state->contact_count; i++) if (!memcmp(state->contacts[i], remote, 17)) return; if (state->contact_count == NAT_MAX_CONTACTS) { if (state->history_complete) DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT contact history full socket=%s", socket->name); if (!state->history_incomplete) history_mark_incomplete(state); return; } memcpy(state->contacts[state->contact_count++], remote, 17); if (!state->history_loaded || !detector->instance->topo_sqlite_db) { state->history_complete = 0; return; } if (history_insert(state, remote)) history_mark_incomplete(state); } static int send_control(struct NAT_DETECTION *detector, uint64_t group_id, uint64_t destination, struct nat_wire *message) { if (!control_route(detector, group_id, destination)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT control route unavailable group=%016llx peer=%016llx", (unsigned long long)group_id, (unsigned long long)destination); return -1; } struct ll_entry *entry = queue_entry_new(0); if (!entry) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT control queue allocation failed"); return -1; } entry->dgram = u_malloc(1 + sizeof(*message)); if (!entry->dgram) { queue_entry_free(entry); DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT control allocation failed"); return -1; } message->cmd = ETCP_RT_ID_NAT_DETECTION; message->version = NAT_WIRE_VERSION; entry->dgram[0] = ETCP_RT_ID_NAT_DETECTION; memcpy(entry->dgram + 1, message, sizeof(*message)); entry->len = 1 + sizeof(*message); if (etcp_route_send(detector->instance, group_id, destination, entry, 0, ROUTE_CRYPTO_SIGN | ROUTE_CRYPTO_ENCRYPT)) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT control send failed group=%016llx peer=%016llx command=%u", (unsigned long long)group_id, (unsigned long long)destination, message->command); return -1; } DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT control TX group=%016llx command=%u step=%u peer=%016llx owner=%016llx round=%016llx helper=%016llx", (unsigned long long)group_id, message->command, message->step, (unsigned long long)destination, (unsigned long long)read64(message->owner), (unsigned long long)read64(message->round), (unsigned long long)read64(message->helper)); return 0; } static int make_message(struct nat_socket *state, struct nat_wire *message, uint8_t command) { memset(message, 0, sizeof(*message)); message->command = command; message->owner_socket = state->socket->sock_id; message->helper_socket = state->helper_socket; write64(message->owner, state->detector->instance->node_id); write64(message->generation, state->socket->generation); write64(message->round, state->round); write64(message->helper, state->helper); write64(message->observer, state->observer); write64(message->token, state->token); if (route_ping_addr_encode(message->target, state->socket->is_tcp ? &state->local : &state->baseline)) return -1; if (state->helper_address.ss_family && route_ping_addr_encode(message->primary, &state->helper_address)) return -1; if (state->observer_address.ss_family && route_ping_addr_encode(message->observer_address, &state->observer_address)) return -1; int timeout = 1000; struct ETCP_CONN *observer = topo_group_find_conn_for_node( topo_groups_find(state->detector->instance->topo_groups, state->group_id), state->observer); for (struct ETCP_LINK *link = observer ? observer->links : NULL; link; link = link->next) if (link->rtt_last / 10 * 4 > timeout) timeout = link->rtt_last / 10 * 4; if (timeout > 3000) timeout = 3000; message->timeout_ms[0] = timeout >> 8; message->timeout_ms[1] = timeout; return 0; } static struct nat_binding *binding_find(struct NAT_DETECTION *detector, uint64_t group_id, uint64_t owner, uint64_t generation, uint64_t round) { for (struct nat_binding *binding = detector->bindings; binding; binding = binding->next) if (binding->group_id == group_id && binding->owner == owner && binding->generation == generation && binding->round == round && binding->expires > get_time_tb()) return binding; return NULL; } void nat_detection_record_observation(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, const uint8_t peer_key[32], const struct sockaddr_storage *source, const uint8_t *tag, size_t length) { if (!detector || detector->stopping) return; if (!socket || socket->socks_udp || length != ROUTE_OBSERVE_TAG_SIZE || !tag || memcmp(tag, "NAT3UDP!", 8)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT observation query has invalid tag length=%zu", length); return; } uint64_t owner = sc_derive_node_id_from_pubkey(peer_key), generation = read64(tag + 9), round = read64(tag + 17); uint64_t group_id = read64(tag + 25); const struct TOPO_NODE *node = group_node(detector, group_id, owner); if (!generation || !round || !node || memcmp(node->public_key, peer_key, 32)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT observation has invalid group/identity group=%016llx owner=%016llx", (unsigned long long)group_id, (unsigned long long)owner); return; } struct nat_binding *binding = binding_find(detector, group_id, owner, generation, round); if (!binding) { unsigned count = 0; for (struct nat_binding *item = detector->bindings; item; item = item->next) count++; if (count >= NAT_MAX_BINDINGS) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT observation cache full"); return; } binding = u_calloc(1, sizeof(*binding)); if (!binding) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT observation cache allocation failed"); return; } binding->next = detector->bindings; detector->bindings = binding; } binding->group_id = group_id; binding->owner = owner; binding->generation = generation; binding->round = round; binding->owner_socket = tag[8]; memcpy(binding->owner_key, peer_key, 32); binding->target = *source; binding->expires = get_time_tb() + NAT_ROUND_TB; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT observation group=%016llx owner=%016llx socket=%u generation=%016llx round=%016llx observed=%s via=%s", (unsigned long long)group_id, (unsigned long long)owner, tag[8], (unsigned long long)generation, (unsigned long long)round, sockaddr_storage_to_str(source).str, socket->name); } static void clear_profile(struct nat_socket *state, uint8_t reason) { int changed = state->socket->address_profile.mapping || state->socket->address_profile.inbound || state->socket->nat_addr.ss_family; memset(&state->socket->address_profile, 0, sizeof(state->socket->address_profile)); memset(&state->socket->nat_addr, 0, sizeof(state->socket->nat_addr)); state->completed_tb = 0; state->cold_until = get_time_tb() + NAT_ROUND_TB + 2500; memset(&state->measured_profile, 0, sizeof(state->measured_profile)); memset(&state->measured_address, 0, sizeof(state->measured_address)); if (changed) { DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT profile invalidated socket=%s generation=%016llx reason=%s", state->socket->name, (unsigned long long)state->socket->generation, nat_check_reason_name(reason)); publish(state->detector); } } /* Единственный выбор публикуемого профиля. STUN переносит только тип IP/netif, * никогда не mapped endpoint тестового порта. Успешный ETCP действует до явного сброса. */ static void select_profile(struct nat_socket *state) { const struct address_profile *current = &state->socket->address_profile; struct address_profile selected = *current; struct sockaddr_storage selected_address = {0}; if (state->measured_profile.source == ADDRESS_SOURCE_ETCP) { selected = state->measured_profile; if (selected.mapping == ADDRESS_MAP_EIM) selected_address = state->measured_address; } else if (state->fallback_allowed && !state->socket->is_tcp) { struct stun_nat_result result; if (!stun_nat_get_socket_result(state->detector->instance, state->socket, &result) && result.generation && (result.status == STUN_NAT_COMPLETE || result.status == STUN_NAT_PARTIAL)) { struct address_profile fallback = {0}; if (result.mapping == STUN_MAP_EIM) fallback.mapping = result.translated == 0 ? ADDRESS_MAP_UNCHANGED : ADDRESS_MAP_EIM; else if (result.mapping == STUN_MAP_DEPENDENT || result.mapping == STUN_MAP_ADM || result.mapping == STUN_MAP_APDM) fallback.mapping = ADDRESS_MAP_DEPENDENT; if (result.filtering == STUN_FILTER_EIF) fallback.inbound = ADDRESS_IN_OPEN; else if (result.filtering == STUN_FILTER_ADF || result.filtering == STUN_FILTER_APDF) fallback.inbound = ADDRESS_IN_CONTACT; if (fallback.mapping || fallback.inbound) { fallback.source = ADDRESS_SOURCE_STUN; if (current->source == ADDRESS_SOURCE_STUN) { if (!fallback.mapping) fallback.mapping = current->mapping; if (fallback.mapping == current->mapping && !fallback.inbound) fallback.inbound = current->inbound; } selected = fallback; } } } int address_changed = selected_address.ss_family != state->socket->nat_addr.ss_family || (selected_address.ss_family && !route_ping_addr_equal(&selected_address, &state->socket->nat_addr, 1)); if (selected.mapping == current->mapping && selected.inbound == current->inbound && selected.source == current->source && !address_changed) return; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT selected socket=%s generation=%016llx source=%s previous=%s mapping=%s inbound=%s" " address_changed=%d reason=%s", state->socket->name, (unsigned long long)state->socket->generation, address_profile_source_name(selected.source), address_profile_source_name(current->source), address_mapping_name(selected.mapping), address_inbound_name(selected.inbound), address_changed, nat_check_reason_name(state->reason)); state->socket->address_profile = selected; state->socket->nat_addr = selected_address; publish(state->detector); } static void owner_finish(struct nat_socket *state, uint8_t reason) { int next_pair = reason && state->phase && state->cycling && !state->using_fallback && reason != NAT_REASON_CANCELLED && reason != NAT_REASON_ADDRESS_CHANGED; etcp_cancel_pings(state->detector->instance, owner_ping_cb, state); state->active_ping = 0; state->phase = NAT_IDLE; state->reason = reason; if (reason == NAT_REASON_NO_OBSERVER || reason == NAT_REASON_NO_HELPER) state->fallback_allowed = 1; if (next_pair) { state->forced = 1; state->due = get_time_tb() + 1000; state->helper_reselections++; } else { state->cycling = 0; state->using_fallback = 0; state->preferred_observer = 0; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0}; state->due = get_time_tb() + (reason == NAT_REASON_ADDRESS_CHANGED ? 2500 : state->socket->address_profile.source || !reason ? NAT_REFRESH_TB : NAT_RETRY_TB); } if (reason) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT round incomplete socket=%s group=%016llx generation=%016llx round=%016llx reason=%s next_pair=%d", state->socket->name, (unsigned long long)state->group_id, (unsigned long long)state->socket->generation, (unsigned long long)state->round, nat_check_reason_name(reason), next_pair); } select_profile(state); } static void owner_apply(struct nat_socket *state, uint8_t mapping, uint8_t inbound) { struct address_profile profile = {.mapping = mapping, .inbound = inbound, .source = ADDRESS_SOURCE_ETCP}; uint8_t partial_reason = inbound != ADDRESS_IN_UNKNOWN ? NAT_REASON_NONE : !state->filter_clean ? NAT_REASON_CONTACT_HISTORY : mapping == ADDRESS_MAP_DEPENDENT ? NAT_REASON_DEPENDENT_MAPPING : NAT_REASON_TIMEOUT; if (inbound == ADDRESS_IN_UNKNOWN && state->measured_profile.source == ADDRESS_SOURCE_ETCP && mapping == state->measured_profile.mapping && route_ping_addr_equal(&state->measured_address, &state->baseline, 1)) { profile.inbound = state->measured_profile.inbound; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT incomplete ingress preserves previous result socket=%s round=%016llx inbound=%s reason=%s", state->socket->name, (unsigned long long)state->round, address_inbound_name(profile.inbound), nat_check_reason_name(partial_reason)); } state->measured_profile = profile; state->measured_address = state->baseline; state->fallback_allowed = 0; state->completed_tb = get_time_tb(); DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT profile socket=%s id=%u generation=%016llx round=%016llx mapping=%s inbound=%s effective_inbound=%s" " local=%s external=%s observer=%016llx helper=%016llx clean=%u partial_reason=%s", state->socket->name, state->socket->sock_id, (unsigned long long)state->socket->generation, (unsigned long long)state->round, address_mapping_name(mapping), address_inbound_name(inbound), address_inbound_name(profile.inbound), sockaddr_storage_to_str(&state->local).str, sockaddr_storage_to_str(&state->baseline).str, (unsigned long long)state->observer, (unsigned long long)state->helper, state->filter_clean, nat_check_reason_name(partial_reason)); owner_finish(state, NAT_REASON_NONE); state->reason = partial_reason; } static int owner_run(struct nat_socket *state, uint8_t step) { if (random_id(&state->token)) return -1; struct nat_wire message; if (make_message(state, &message, NAT_RUN)) return -1; message.step = step; state->phase = step == NAT_STEP_TCP ? NAT_TCP : step == NAT_STEP_PREPARE ? NAT_PREPARE_C : step == NAT_STEP_PRE ? NAT_PRE : step == NAT_STEP_ALT ? NAT_PREPARE_ALT : NAT_POST; return send_control(state->detector, state->group_id, state->observer, &message); } static int owner_observe(struct nat_socket *state) { const struct sockaddr_storage *target = &state->observer_address; const uint8_t *key = state->observer_key; if (state->phase == NAT_OBSERVE_C || state->phase == NAT_OBSERVE_ALT) { target = state->phase == NAT_OBSERVE_C ? &state->helper_address : &state->alternate_address; key = state->helper_key; } int timeout = 1000; struct ETCP_CONN *connection = topo_group_find_conn_for_node( topo_groups_find(state->detector->instance->topo_groups, state->group_id), state->observer); for (struct ETCP_LINK *link = connection ? connection->links : NULL; link; link = link->next) if (link->rtt_last / 10 * 4 > timeout) timeout = link->rtt_last / 10 * 4; if (timeout > 3000) timeout = 3000; if (route_ping_observe(state->socket, key, target, state->group_id, state->round, timeout, owner_ping_cb, state)) return -1; state->active_ping = 1; return 0; } static void owner_ping_cb(int success, uint16_t rtt, void *arg, uint64_t nonce, const uint8_t *data, size_t length) { (void)rtt; (void)nonce; struct nat_socket *state = arg; state->active_ping = 0; if (state->phase == NAT_IDLE) return; struct sockaddr_storage observed; if (!success || route_ping_observation(data, length, state->socket, state->group_id, state->round, &observed)) { if (++state->retries < NAT_PROBE_COUNT) { state->due = get_time_tb() + 1000; return; } owner_finish(state, success ? NAT_REASON_INVALID_OBSERVATION : NAT_REASON_TIMEOUT); return; } state->retries = 0; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT own observation socket=%s phase=%u round=%016llx observed=%s", state->socket->name, state->phase, (unsigned long long)state->round, sockaddr_storage_to_str(&observed).str); if (state->phase == NAT_OBSERVE_B) { state->baseline = observed; if ((state->measured_profile.mapping == ADDRESS_MAP_UNCHANGED && !route_ping_addr_equal(&state->local, &observed, 1)) || (state->socket->nat_addr.ss_family && !route_ping_addr_equal(&state->socket->nat_addr, &observed, 1))) clear_profile(state, NAT_REASON_ADDRESS_CHANGED); if (owner_run(state, NAT_STEP_PREPARE)) owner_finish(state, NAT_REASON_CONTROL_LOST); return; } if (state->phase == NAT_OBSERVE_C) { state->helper_seen = observed; if (route_ping_addr_equal(&state->baseline, &observed, 1)) { if (owner_run(state, NAT_STEP_ALT)) owner_finish(state, NAT_REASON_CONTROL_LOST); return; } state->phase = NAT_REPEAT_B; } else if (state->phase == NAT_OBSERVE_ALT) { state->alternate_seen = observed; if (state->filter_clean && state->pre_healthy && !state->pre_received && route_ping_addr_equal(&state->baseline, &state->helper_seen, 1) && route_ping_addr_equal(&state->baseline, &state->alternate_seen, 1)) { if (owner_run(state, NAT_STEP_POST)) owner_finish(state, NAT_REASON_CONTROL_LOST); return; } state->phase = NAT_REPEAT_B; } else if (state->phase == NAT_REPEAT_B) { if (!route_ping_addr_equal(&observed, &state->baseline, 1)) { clear_profile(state, NAT_REASON_ADDRESS_CHANGED); owner_finish(state, NAT_REASON_ADDRESS_CHANGED); return; } int same = route_ping_addr_equal(&state->baseline, &state->helper_seen, 1) && route_ping_addr_equal(&state->baseline, &state->alternate_seen, 1); uint8_t mapping = same ? (route_ping_addr_equal(&state->local, &state->baseline, 1) ? ADDRESS_MAP_UNCHANGED : ADDRESS_MAP_EIM) : ADDRESS_MAP_DEPENDENT; uint8_t inbound = state->filter_clean && state->pre_received ? ADDRESS_IN_OPEN : state->filter_clean && state->pre_healthy && state->post_received && same ? ADDRESS_IN_CONTACT : ADDRESS_IN_UNKNOWN; owner_apply(state, mapping, inbound); return; } else { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT observation in unexpected phase=%u", state->phase); owner_finish(state, NAT_REASON_INVALID_OBSERVATION); return; } if (owner_observe(state)) owner_finish(state, NAT_REASON_SEND_ERROR); } void nat_detection_receive_ingress(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, const uint8_t peer_key[32], const struct sockaddr_storage *source, const uint8_t *tag, size_t length) { struct nat_socket *state = socket_state(detector, socket, 0); if (!state || !tag || length != NAT_QUERY_TAG_SIZE) { DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT ingress ignored without pending endpoint/valid tag length=%zu", length); return; } uint8_t expected[ROUTE_OBSERVE_TAG_SIZE]; route_ping_make_tag(expected, socket, state->group_id, state->round); uint8_t step = state->phase == NAT_PRE ? NAT_STEP_PRE : state->phase == NAT_POST ? NAT_STEP_POST : 0; if (!step || memcmp(tag, expected, sizeof(expected)) || tag[ROUTE_OBSERVE_TAG_SIZE] != step || read64(tag + ROUTE_OBSERVE_TAG_SIZE + 1) != state->token || sc_derive_node_id_from_pubkey(peer_key) != state->helper || memcmp(peer_key, state->helper_key, 32) || source->ss_family != state->local.ss_family || !public_address(detector, source) || ((step == NAT_STEP_POST || state->pre_received) && !route_ping_addr_equal(source, &state->helper_address, 1)) || route_ping_addr_equal(source, &state->observer_address, 0)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT ingress correlation/source mismatch socket=%s phase=%u round=%016llx source=%s", socket->name, state->phase, (unsigned long long)state->round, sockaddr_storage_to_str(source).str); return; } if (step == NAT_STEP_PRE) { if (!state->pre_received) { state->filter_clean = !contacted(state, source); state->helper_address = *source; } state->pre_received = 1; } else state->post_received = 1; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT direct ingress socket=%s round=%016llx step=%u source=%s clean=%u", socket->name, (unsigned long long)state->round, step, sockaddr_storage_to_str(source).str, state->filter_clean); } /* Дополнительный порт наблюдателя не публикуется и не участвует в NCD. */ static struct ETCP_SOCKET *helper_socket(struct NAT_DETECTION *detector, struct ETCP_SOCKET *source, int alternate) { struct nat_socket *state = socket_state(detector, source, 1); if (!state) return NULL; struct ETCP_SOCKET **slot = alternate ? &state->auxiliary : &state->helper_primary; if (*slot) return *slot; struct CFG_SERVER server; memset(&server, 0, sizeof(server)); server.ip = source->local_addr; server.netif_index = source->netif_index; server.type = CFG_SERVER_TYPE_PRIVATE; server.mtu = source->mtu; server.fib = -1; for (const struct CFG_SERVER *cfg = detector->instance->config->servers; cfg; cfg = cfg->next) if (!strcmp(cfg->name, source->name)) { server.so_mark = cfg->so_mark; server.fib = cfg->fib; break; } snprintf(server.name, sizeof(server.name), "nat-helper-%u-%u", source->sock_id, alternate); if (server.ip.ss_family == AF_INET) ((struct sockaddr_in *)&server.ip)->sin_port = 0; else ((struct sockaddr_in6 *)&server.ip)->sin6_port = 0; *slot = etcp_socket_add(detector->instance, &server); if (!*slot) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT helper socket failed source=%s alternate=%d", source->name, alternate); return NULL; } (*slot)->probe_only = 1; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT isolated helper socket source=%s alternate=%d local=%s helper=%s", source->name, alternate, sockaddr_storage_to_str(&source->local_addr).str, sockaddr_storage_to_str(&(*slot)->local_addr).str); return *slot; } static void helper_remove(struct nat_probe *probe) { struct NAT_DETECTION *detector = probe->detector; struct nat_probe **cursor = &detector->probes; while (*cursor && *cursor != probe) cursor = &(*cursor)->next; if (*cursor) *cursor = probe->next; probe->cancelled = 1; etcp_cancel_pings(detector->instance, helper_ping_cb, probe); if (probe->tcp) { struct stcp_client *tcp = probe->tcp; probe->tcp = NULL; stcp_client_destroy(tcp); } u_free(probe); } static void helper_finish(struct nat_probe *probe, uint8_t reason) { probe->message.command = NAT_RESULT; probe->message.reason = reason; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT helper done owner=%016llx round=%016llx step=%u sent=%u received=%u errors=%u reason=%s", (unsigned long long)read64(probe->message.owner), (unsigned long long)read64(probe->message.round), probe->message.step, probe->message.sent, probe->message.received, probe->message.errors, nat_check_reason_name(reason)); if (send_control(probe->detector, probe->group_id, probe->reply, &probe->message)) DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT helper result delivery failed"); helper_remove(probe); } /* Входящая проба несёт идентификаторы owner; OBSERVE с помощника — идентификаторы его собственного socket. */ static void ingress_tag(const struct nat_wire *message, uint64_t group_id, uint8_t tag[NAT_QUERY_TAG_SIZE]) { memcpy(tag, "NAT3UDP!", 8); tag[8] = message->owner_socket; memcpy(tag + 9, message->generation, 8); memcpy(tag + 17, message->round, 8); write64(tag + 25, group_id); tag[ROUTE_OBSERVE_TAG_SIZE] = message->step; memcpy(tag + ROUTE_OBSERVE_TAG_SIZE + 1, message->token, 8); } static void helper_ping_cb(int success, uint16_t rtt, void *arg, uint64_t nonce, const uint8_t *data, size_t length) { (void)rtt; (void)nonce; struct nat_probe *probe = arg; if (probe->cancelled) return; probe->active = 0; probe->tcp = NULL; uint8_t step = probe->message.step; if (step == NAT_STEP_PREPARE || step == NAT_STEP_ALT) { struct ETCP_SOCKET *socket = find_socket(probe->detector, probe->io_socket, probe->io_generation); struct sockaddr_storage mapped; if (success && socket && !route_ping_observation(data, length, socket, probe->group_id, read64(probe->message.round), &mapped)) { uint8_t *endpoint = step == NAT_STEP_PREPARE ? probe->message.primary : probe->message.secondary; if (route_ping_addr_encode(endpoint, &mapped)) { helper_finish(probe, NAT_REASON_INVALID_OBSERVATION); return; } probe->message.received++; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT helper endpoint measured group=%016llx owner=%016llx step=%u socket=%s mapped=%s fallback=%u", (unsigned long long)probe->group_id, (unsigned long long)read64(probe->message.owner), step, socket->name, sockaddr_storage_to_str(&mapped).str, probe->alternate_fallback); helper_finish(probe, NAT_REASON_NONE); return; } if (success) { helper_finish(probe, NAT_REASON_INVALID_OBSERVATION); return; } if (probe->message.sent >= NAT_PROBE_COUNT) { if (step == NAT_STEP_ALT && !probe->alternate_fallback) { probe->alternate_fallback = 1; probe->message.sent = 0; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT alternate endpoint blocked by owner; observe through broker group=%016llx owner=%016llx", (unsigned long long)probe->group_id, (unsigned long long)read64(probe->message.owner)); } else { helper_finish(probe, NAT_REASON_TIMEOUT); return; } } } else if (step == NAT_STEP_TCP) { if (success) probe->message.received++; helper_finish(probe, success ? NAT_REASON_NONE : NAT_REASON_TIMEOUT); return; } else { if (success) { uint8_t expected[NAT_QUERY_TAG_SIZE]; ingress_tag(&probe->message, probe->group_id, expected); struct sockaddr_storage mapped; if (length != ROUTE_ADDR_WIRE_SIZE + sizeof(expected) || memcmp(data + ROUTE_ADDR_WIRE_SIZE, expected, sizeof(expected)) || route_ping_addr_decode(data, &mapped) || route_ping_addr_encode(probe->message.primary, &mapped)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT ingress PONG has invalid endpoint/tag group=%016llx length=%zu", (unsigned long long)probe->group_id, length); helper_finish(probe, NAT_REASON_INVALID_OBSERVATION); return; } probe->message.received++; } if (probe->message.sent >= NAT_PROBE_COUNT) { helper_finish(probe, NAT_REASON_NONE); return; } } probe->due = get_time_tb() + 1000; } static int helper_send(struct nat_probe *probe) { struct NAT_DETECTION *detector = probe->detector; struct sockaddr_storage target; if (route_ping_addr_decode(probe->message.target, &target)) return -1; int timeout = ((unsigned)probe->message.timeout_ms[0] << 8) | probe->message.timeout_ms[1]; if (timeout < 500 || timeout > 3000) return -1; int result; if (probe->message.step == NAT_STEP_TCP) { struct reality_client_config reality, *camouflage = NULL; const struct TOPO_NODE *owner = topo_node_registry_find(detector->instance->topo_groups, read64(probe->message.owner)); const struct TOPO_REALITY_SOCK *parameters = topo_node_find_reality_sock(owner, probe->message.owner_socket); if (parameters) { topo_node_reality_config(&reality, parameters); camouflage = &reality; } result = etcp_send_tcp_ping(detector->instance, probe->owner_key, &target, timeout, helper_ping_cb, probe, &probe->tcp, camouflage); } else if (probe->message.step == NAT_STEP_PREPARE || probe->message.step == NAT_STEP_ALT) { struct ETCP_SOCKET *source = find_socket(detector, probe->source_socket, probe->source_generation); if (!source) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT helper source generation disappeared"); return -1; } struct ETCP_SOCKET *socket = helper_socket(detector, source, probe->message.step == NAT_STEP_ALT); if (!socket) return -1; const uint8_t *peer_key = probe->owner_key; if (probe->message.step == NAT_STEP_PREPARE || probe->alternate_fallback) { if (route_ping_addr_decode(probe->message.observer_address, &target)) return -1; peer_key = probe->observer_key; } probe->io_socket = socket->sock_id; probe->io_generation = socket->generation; result = route_ping_observe(socket, peer_key, &target, probe->group_id, read64(probe->message.round), timeout, helper_ping_cb, probe); } else { struct ETCP_SOCKET *source = find_socket(detector, probe->source_socket, probe->source_generation); if (!source || source->is_tcp || source->socks_udp) return -1; struct ETCP_SOCKET *socket = helper_socket(detector, source, 0); if (!socket) return -1; probe->io_socket = socket->sock_id; probe->io_generation = socket->generation; uint8_t tag[NAT_QUERY_TAG_SIZE]; ingress_tag(&probe->message, probe->group_id, tag); result = etcp_send_ping_to_socket(detector->instance, socket, probe->owner_key, &target, timeout, helper_ping_cb, probe, tag, sizeof(tag), ETCP_PING_FLAG_ADDRESS_CHECK); } if (result) { probe->message.errors++; return -1; } probe->message.sent++; probe->active = 1; return 0; } static int tcp_target_advertised(struct NAT_DETECTION *detector, uint64_t group_id, const struct nat_wire *message, const struct sockaddr_storage *target) { const struct TOPO_NODE *owner = group_node(detector, group_id, read64(message->owner)); if (!owner) return 0; uint8_t address[ROUTE_ADDR_WIRE_SIZE]; if (route_ping_addr_encode(address, target)) return 0; if (target->ss_family == AF_INET) { for (const struct TOPO_ADDR4 *item = owner->v4_addrs; item; item = item->next) if (item->socket_id == message->owner_socket && (item->protocol & TOPO_PROTO_TCP) && !memcmp(item->addr, address + 1, 4) && item->port == (((unsigned)address[17] << 8) | address[18])) return 1; } else { for (const struct TOPO_ADDR6 *item = owner->v6_addrs; item; item = item->next) if (item->socket_id == message->owner_socket && (item->protocol & TOPO_PROTO_TCP) && !memcmp(item->addr, address + 1, 16) && item->port == (((unsigned)address[17] << 8) | address[18])) return 1; } return 0; } /* Broker проверяет реальное наблюдение owner, затем делегирует выбранному владельцем помощнику через ту же группу. */ static void handle_run(struct NAT_DETECTION *detector, uint64_t group_id, uint64_t sender, struct nat_wire *message) { uint64_t owner = read64(message->owner), observer = read64(message->observer), helper = read64(message->helper); const struct TOPO_NODE *owner_node = group_node(detector, group_id, owner); const struct TOPO_NODE *observer_node = group_node(detector, group_id, observer); struct sockaddr_storage target; uint8_t reason = NAT_REASON_INVALID_OBSERVATION; if (!owner_node || !observer_node || owner == observer || owner == helper || route_ping_addr_decode(message->target, &target) || !public_address(detector, &target)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT run has invalid participants/target group=%016llx owner=%016llx observer=%016llx helper=%016llx", (unsigned long long)group_id, (unsigned long long)owner, (unsigned long long)observer, (unsigned long long)helper); goto reject; } if (sender == owner && observer == detector->instance->node_id) { if (message->step != NAT_STEP_TCP) { struct nat_binding *binding = binding_find(detector, group_id, owner, read64(message->generation), read64(message->round)); if (!binding || binding->owner_socket != message->owner_socket || !route_ping_addr_equal(&binding->target, &target, 1) || memcmp(binding->owner_key, owner_node->public_key, 32) || helper == observer) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT delegation rejected: stale own observation group=%016llx owner=%016llx", (unsigned long long)group_id, (unsigned long long)owner); goto reject; } binding->helper = helper; binding->token = read64(message->token); binding->expires = get_time_tb() + NAT_ROUND_TB; } if (helper != detector->instance->node_id) { if (!control_route(detector, group_id, helper) || send_control(detector, group_id, helper, message)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT delegation route failed group=%016llx helper=%016llx", (unsigned long long)group_id, (unsigned long long)helper); reason = NAT_REASON_CONTROL_LOST; goto reject; } return; } } else if (sender != observer || helper != detector->instance->node_id) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT run role mismatch group=%016llx sender=%016llx observer=%016llx helper=%016llx", (unsigned long long)group_id, (unsigned long long)sender, (unsigned long long)observer, (unsigned long long)helper); goto reject; } if (message->step == NAT_STEP_TCP && (helper != observer || !tcp_target_advertised(detector, group_id, message, &target))) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT TCP target is not the signed owner listener group=%016llx", (unsigned long long)group_id); goto reject; } unsigned count = 0; for (struct nat_probe *probe = detector->probes; probe; probe = probe->next) { count++; if (probe->group_id == group_id && read64(probe->message.owner) == owner && read64(probe->message.generation) == read64(message->generation) && read64(probe->message.round) == read64(message->round) && read64(probe->message.token) == read64(message->token)) { DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT duplicate helper run ignored"); return; } } if (count >= NAT_MAX_PROBES) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT helper probe limit reached"); reason = NAT_REASON_SEND_ERROR; goto reject; } struct ETCP_SOCKET *source = NULL; if (message->step != NAT_STEP_TCP) { struct sockaddr_storage observer_address; source = find_socket(detector, message->helper_socket, 0); if (!source || source->is_tcp || source->probe_only || source->socks_udp || source->type == CFG_SERVER_TYPE_PRIVATE || source->type == CFG_SERVER_TYPE_LOCAL || source->local_addr.ss_family != target.ss_family || route_ping_addr_decode(message->observer_address, &observer_address) || observer_address.ss_family != target.ss_family || !public_address(detector, &observer_address)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT helper source is not a native socket group=%016llx socket=%u", (unsigned long long)group_id, message->helper_socket); reason = source && source->socks_udp ? NAT_REASON_PROXY : NAT_REASON_NO_HELPER; goto reject; } } struct nat_probe *probe = u_calloc(1, sizeof(*probe)); if (!probe) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT helper probe allocation failed"); reason = NAT_REASON_SEND_ERROR; goto reject; } probe->detector = detector; probe->message = *message; probe->group_id = group_id; probe->reply = sender; memcpy(probe->owner_key, owner_node->public_key, 32); memcpy(probe->observer_key, observer_node->public_key, 32); if (source) { probe->source_socket = source->sock_id; probe->source_generation = source->generation; } probe->expires = get_time_tb() + NAT_ROUND_TB; probe->next = detector->probes; detector->probes = probe; if (helper_send(probe)) helper_finish(probe, NAT_REASON_SEND_ERROR); return; reject: message->command = NAT_RESULT; message->reason = reason; if (send_control(detector, group_id, sender, message)) DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT rejection delivery failed"); } static void handle_result(struct nat_socket *state, const struct nat_wire *message) { uint8_t step = state->phase == NAT_PRE ? NAT_STEP_PRE : state->phase == NAT_POST ? NAT_STEP_POST : state->phase == NAT_TCP ? NAT_STEP_TCP : state->phase == NAT_PREPARE_C ? NAT_STEP_PREPARE : state->phase == NAT_PREPARE_ALT ? NAT_STEP_ALT : 0; if (!step || message->step != step || read64(message->token) != state->token || read64(message->helper) != state->helper) { DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT stale helper result ignored socket=%s phase=%u", state->socket->name, state->phase); return; } if (message->reason || message->errors || !message->sent || message->received > message->sent) { owner_finish(state, message->reason ? message->reason : NAT_REASON_SEND_ERROR); return; } if (step == NAT_STEP_TCP) { if (message->received) owner_apply(state, ADDRESS_MAP_UNCHANGED, ADDRESS_IN_OPEN); else owner_finish(state, NAT_REASON_TIMEOUT); return; } if (step == NAT_STEP_PREPARE) { struct sockaddr_storage primary; if (route_ping_addr_decode(message->primary, &primary) || primary.ss_family != state->local.ss_family || !public_address(state->detector, &primary)) { owner_finish(state, NAT_REASON_INVALID_OBSERVATION); return; } int same_ip = route_ping_addr_equal(&state->observer_address, &primary, 0), old_contact = contacted(state, &primary); if (same_ip || (old_contact && !state->using_fallback)) { if (!same_ip && !state->fallback.group) state->fallback = (struct nat_pair){state->group_id, state->observer, state->helper}; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT try another pair socket=%s group=%016llx helper=%016llx endpoint=%s reason=%s", state->socket->name, (unsigned long long)state->group_id, (unsigned long long)state->helper, sockaddr_storage_to_str(&primary).str, same_ip ? "same_external_ip" : "previous_udp_contact"); owner_finish(state, same_ip ? NAT_REASON_NO_HELPER : NAT_REASON_CONTACT_HISTORY); return; } state->helper_address = primary; state->filter_clean = !old_contact; if (owner_run(state, NAT_STEP_PRE)) owner_finish(state, NAT_REASON_CONTROL_LOST); return; } if (step == NAT_STEP_ALT) { if (route_ping_addr_decode(message->secondary, &state->alternate_address) || !route_ping_addr_equal(&state->alternate_address, &state->helper_address, 0) || route_ping_addr_equal(&state->alternate_address, &state->helper_address, 1)) { owner_finish(state, NAT_REASON_INVALID_OBSERVATION); return; } state->phase = NAT_OBSERVE_ALT; } else if (step == NAT_STEP_PRE) { if (!state->pre_received && route_ping_addr_decode(message->primary, &state->helper_address)) { owner_finish(state, NAT_REASON_INVALID_OBSERVATION); return; } state->pre_healthy = message->sent == NAT_PROBE_COUNT; state->phase = NAT_OBSERVE_C; } else state->phase = NAT_REPEAT_B; state->retries = 0; if (owner_observe(state)) owner_finish(state, NAT_REASON_SEND_ERROR); } /* Потеря router-сессии отменяет только роли этого узла в данной группе, не общий транспорт. */ static void cancel_route(struct NAT_DETECTION *detector, uint64_t group_id, uint64_t peer) { for (struct nat_socket *state = detector->sockets; state; state = state->next) if (state->phase && state->group_id == group_id && (state->observer == peer || state->helper == peer)) owner_finish(state, NAT_REASON_CONTROL_LOST); struct nat_probe *probe = detector->probes; while (probe) { struct nat_probe *next = probe->next; if (probe->group_id == group_id && (probe->reply == peer || read64(probe->message.owner) == peer)) helper_remove(probe); probe = next; } for (struct nat_binding *binding = detector->bindings; binding; binding = binding->next) if (binding->group_id == group_id && (binding->owner == peer || binding->helper == peer)) binding->expires = 0; } /* SOURCE и group берём из проверенного router-заголовка, а не из ETCP next hop. */ static void receive_control(struct ETCP_CONN *from, struct ll_entry *entry) { struct NAT_DETECTION *detector = from && from->instance ? from->instance->nat_det : NULL; if (!detector || !entry || !entry->dgram || entry->len < ROUTER_SVC_HDR_SIZE) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT routed control invalid size=%zu", entry ? entry->len : 0); if (entry) { queue_dgram_free(entry); queue_entry_free(entry); } return; } uint64_t group_id, sender; memcpy(&group_id, entry->dgram + ROUTER_SVC_GROUP_OFF, 8); memcpy(&sender, entry->dgram + ROUTER_SVC_SRC_OFF, 8); if (entry->len == ROUTER_SVC_HDR_SIZE) { DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT routed control closed group=%016llx peer=%016llx", (unsigned long long)group_id, (unsigned long long)sender); queue_dgram_free(entry); queue_entry_free(entry); cancel_route(detector, group_id, sender); return; } if (entry->len != ROUTER_SVC_HDR_SIZE + sizeof(struct nat_wire) || entry->dgram[ROUTER_SVC_FLAGS_OFF] != (ROUTER_FLAG_SIGNED | ROUTER_FLAG_ENCRYPTED) || !group_node(detector, group_id, sender)) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT routed control rejected group=%016llx sender=%016llx size=%zu flags=%u", (unsigned long long)group_id, (unsigned long long)sender, entry->len, entry->dgram[ROUTER_SVC_FLAGS_OFF]); queue_dgram_free(entry); queue_entry_free(entry); return; } struct nat_wire message; memcpy(&message, entry->dgram + ROUTER_SVC_PAYLOAD_OFF, sizeof(message)); queue_dgram_free(entry); queue_entry_free(entry); if (message.cmd != ETCP_RT_ID_NAT_DETECTION || message.version != NAT_WIRE_VERSION || !read64(message.owner) || !read64(message.observer) || !read64(message.helper) || !read64(message.generation) || !read64(message.round) || !read64(message.token) || message.command < NAT_RUN || message.command > NAT_RESULT || message.reason > NAT_REASON_DEPENDENT_MAPPING || message.step < NAT_STEP_PREPARE || message.step > NAT_STEP_TCP) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT control malformed command=%u version=%u step=%u", message.command, message.version, message.step); return; } DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT control RX group=%016llx command=%u step=%u sender=%016llx via=%016llx owner=%016llx round=%016llx", (unsigned long long)group_id, message.command, message.step, (unsigned long long)sender, (unsigned long long)from->peer_node_id, (unsigned long long)read64(message.owner), (unsigned long long)read64(message.round)); if (message.command == NAT_RUN) { handle_run(detector, group_id, sender, &message); return; } uint64_t owner = read64(message.owner); if (owner != detector->instance->node_id) { struct nat_binding *binding = binding_find(detector, group_id, owner, read64(message.generation), read64(message.round)); if (!binding || binding->helper != sender || read64(message.helper) != sender || binding->token != read64(message.token) || read64(message.observer) != detector->instance->node_id) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT result has no matching delegation group=%016llx owner=%016llx sender=%016llx", (unsigned long long)group_id, (unsigned long long)owner, (unsigned long long)sender); return; } if (send_control(detector, group_id, owner, &message)) DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT result forwarding failed"); return; } struct ETCP_SOCKET *socket = find_socket(detector, message.owner_socket, read64(message.generation)); struct nat_socket *state = socket_state(detector, socket, 0); if (!state || state->group_id != group_id || state->round != read64(message.round) || state->observer != sender || read64(message.observer) != sender || state->phase == NAT_IDLE) { DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT stale/foreign owner response ignored group=%016llx", (unsigned long long)group_id); return; } handle_result(state, &message); } static void owner_start(struct nat_socket *state) { struct NAT_DETECTION *detector = state->detector; if (state->socket->socks_udp) { state->reason = NAT_REASON_PROXY; state->due = UINT64_MAX; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT native endpoint not measured through SOCKS5 socket=%s", state->socket->name); return; } if (state->socket->type == CFG_SERVER_TYPE_LOCAL || state->socket->type == CFG_SERVER_TYPE_PRIVATE) { state->due = UINT64_MAX; return; } if (state->socket->is_tcp && !public_address(detector, &state->local)) { state->reason = NAT_REASON_NO_LISTENER; state->due = UINT64_MAX; DEBUG_INFO(DEBUG_CATEGORY_NAT, "TCP inbound profile remains UNKNOWN: no public listener candidate socket=%s local=%s", state->socket->name, sockaddr_storage_to_str(&state->local).str); return; } if (!state->cycling) { state->cycling = 1; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0}; state->using_fallback = 0; state->prefer_fresh = 1; } history_load(state); if (random_id(&state->round)) { owner_finish(state, NAT_REASON_SEND_ERROR); return; } state->rounds++; if (select_pair(state)) { owner_finish(state, state->reason); return; } memset(&state->helper_seen, 0, sizeof(state->helper_seen)); memset(&state->alternate_seen, 0, sizeof(state->alternate_seen)); state->retries = 0; state->pre_received = 0; state->post_received = 0; state->filter_clean = 0; state->pre_healthy = 0; state->deadline = get_time_tb() + NAT_ROUND_TB; state->reason = NAT_REASON_NONE; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT round start socket=%s group=%016llx id=%u generation=%016llx round=%016llx observer=%016llx helper=%016llx protocol=%s", state->socket->name, (unsigned long long)state->group_id, state->socket->sock_id, (unsigned long long)state->socket->generation, (unsigned long long)state->round, (unsigned long long)state->observer, (unsigned long long)state->helper, state->socket->is_tcp ? "TCP" : "UDP"); if (state->socket->is_tcp) { state->baseline = state->local; if (owner_run(state, NAT_STEP_TCP)) owner_finish(state, NAT_REASON_CONTROL_LOST); } else { state->phase = NAT_OBSERVE_B; if (owner_observe(state)) owner_finish(state, NAT_REASON_SEND_ERROR); } } static void detector_tick(void *arg) { struct NAT_DETECTION *detector = arg; detector->timer = NULL; if (detector->publish_pending && get_time_tb() >= detector->publish_after) publish(detector); uint64_t now = get_time_tb(); for (struct ETCP_SOCKET *socket = detector->instance->etcp_sockets; socket; socket = socket->next) if (!socket->probe_only) socket_state(detector, socket, 1); for (struct nat_socket *state = detector->sockets; state; state = state->next) { if (now >= state->select_due) { select_profile(state); state->select_due = now + 10000; } if (state->phase && now >= state->deadline) { owner_finish(state, NAT_REASON_TIMEOUT); continue; } if (!state->phase && now >= state->due && (detector->automatic || state->forced)) { state->forced = 0; owner_start(state); } else if ((state->phase == NAT_OBSERVE_B || state->phase == NAT_OBSERVE_C || state->phase == NAT_OBSERVE_ALT || state->phase == NAT_REPEAT_B) && !state->active_ping && now >= state->due) { if (owner_observe(state)) owner_finish(state, NAT_REASON_SEND_ERROR); } } struct nat_probe *probe = detector->probes; while (probe) { struct nat_probe *next = probe->next; if (now >= probe->expires) helper_finish(probe, NAT_REASON_TIMEOUT); else if (!probe->active && now >= probe->due && helper_send(probe)) helper_finish(probe, NAT_REASON_SEND_ERROR); probe = next; } struct nat_binding **cursor = &detector->bindings; while (*cursor) { struct nat_binding *binding = *cursor; if (now < binding->expires) { cursor = &binding->next; continue; } *cursor = binding->next; u_free(binding); } detector->timer = uasync_set_timeout(detector->instance->ua, NAT_TICK_TB, detector, detector_tick, "nat_detection"); if (!detector->timer) DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT detector timer scheduling failed"); } /* Отпечаток доступных endpoint не содержит timestamp/имя/профиль: обычный повтор NODEINFO не запускает пробы. */ static uint64_t candidate_fingerprint(struct nat_socket *state, int ready) { struct NAT_DETECTION *detector = state->detector; uint64_t total = 0; for (struct ll_entry *e = detector->instance->topo_groups->group_list->head; e; e = e->next) { struct TOPO_GROUP *group = (struct TOPO_GROUP *)e; if (group->stopping || !group->local_node) continue; uint64_t group_total = 0; unsigned candidates = 0, observers = 0; for (struct ll_entry *n = group->nodes->head; n; n = n->next) { uint64_t id = ((struct TOPO_GROUP_NODE *)n)->node_id; if (id == detector->instance->node_id || (ready ? !control_route(detector, group->group_id, id) : !topo_node_best_path((struct TOPO_GROUP_NODE *)n))) continue; const struct TOPO_NODE *node = group_node(detector, group->group_id, id); if (!node) continue; struct sockaddr_storage endpoint = {0}; uint8_t socket_id = 0; uint8_t bytes[16 + ROUTE_ADDR_WIRE_SIZE + 1] = {0}; write64(bytes, group->group_id); write64(bytes + 8, id); if (!state->socket->is_tcp) { if (!node_udp_endpoint(detector, node, state->local.ss_family, 1, &endpoint, &socket_id)) continue; if (route_ping_addr_encode(bytes + 16, &endpoint)) continue; bytes[16 + ROUTE_ADDR_WIRE_SIZE] = socket_id; struct sockaddr_storage observer_address = {0}; uint8_t observer_socket = 0; observers += node_udp_endpoint(detector, node, state->local.ss_family, 0, &observer_address, &observer_socket); } candidates++; uint64_t hash = UINT64_C(14695981039346656037); for (unsigned i = 0; i < sizeof(bytes); i++) { hash ^= bytes[i]; hash *= UINT64_C(1099511628211); } group_total += hash; } if (ready || (candidates >= 2 && observers)) total += group_total; } return total; } /* Изменения нескольких групп объединяются; известный полный ETCP-профиль не перепроверяется из-за нового соседа. */ static void topology_wake(void *arg) { struct NAT_DETECTION *detector = arg; detector->topology_wake = NULL; for (struct nat_socket *state = detector->sockets; state; state = state->next) { uint64_t fingerprint = candidate_fingerprint(state, 1); int changed = fingerprint != state->available_fingerprint; state->available_fingerprint = fingerprint; if (state->phase) { if (!control_route(detector, state->group_id, state->observer) || !control_route(detector, state->group_id, state->helper)) owner_finish(state, NAT_REASON_CONTROL_LOST); continue; } if (!changed || !fingerprint || !detector->automatic || state->cycling) continue; if (state->measured_profile.source == ADDRESS_SOURCE_ETCP && state->measured_profile.mapping && state->measured_profile.inbound) continue; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0}; state->using_fallback = 0; state->due = get_time_tb() + 2500; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT available candidates changed socket=%s; early measurement scheduled", state->socket->name); } } void nat_detection_topology_changed(struct NAT_DETECTION *detector) { if (!detector || detector->stopping) return; detector->reservation_dirty = 1; if (detector->topology_wake) return; detector->topology_wake = uasync_call_soon(detector->instance->ua, detector, topology_wake); if (!detector->topology_wake) DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT topology wake scheduling failed"); } void nat_detection_cancel_group(struct NAT_DETECTION *detector, uint64_t group_id) { if (!detector || detector->stopping) return; for (struct nat_socket *state = detector->sockets; state; state = state->next) { if (state->fallback.group == group_id) state->fallback = (struct nat_pair){0}; if (state->group_id != group_id || (!state->phase && !state->cycling)) continue; owner_finish(state, NAT_REASON_CANCELLED); state->forced = 0; state->group_id = 0; state->due = get_time_tb() + 2500; } struct nat_probe *probe = detector->probes; while (probe) { struct nat_probe *next = probe->next; if (probe->group_id == group_id) helper_remove(probe); probe = next; } struct nat_binding **cursor = &detector->bindings; while (*cursor) { struct nat_binding *binding = *cursor; if (binding->group_id != group_id) { cursor = &binding->next; continue; } *cursor = binding->next; u_free(binding); } DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT group tasks cancelled group=%016llx", (unsigned long long)group_id); nat_detection_topology_changed(detector); } void nat_detection_cancel_for_conn(struct NAT_DETECTION *detector, struct ETCP_CONN *connection) { if (!detector || !connection || detector->stopping) return; nat_detection_topology_changed(detector); } static void connection_status(struct ETCP_CONN *connection, int status, void *arg) { struct NAT_DETECTION *detector = arg; if (status == ETCP_CONN_STATUS_DOWN || status == ETCP_CONN_STATUS_DELETE || status == ETCP_CONN_STATUS_REINIT) nat_detection_cancel_for_conn(detector, connection); else if (status == ETCP_CONN_STATUS_UP) nat_detection_topology_changed(detector); } void nat_detection_link_ready(struct NAT_DETECTION *detector, struct ETCP_LINK *link) { if (!detector || !link || !link->conn || link->conn->probe_only || detector->stopping) return; struct nat_socket *state = socket_state(detector, link->conn, 1); if (state && !state->phase && !state->completed_tb) state->due = get_time_tb() + 2500; } void nat_detection_observed_by_peer(struct NAT_DETECTION *detector, struct ETCP_LINK *link, const struct sockaddr_storage *observed) { if (!detector || !link || !link->conn || link->is_tcp || link->conn->socks_udp || !observed->ss_family) return; struct nat_socket *state = socket_state(detector, link->conn, 1); if (!state) return; if ((state->measured_profile.mapping == ADDRESS_MAP_UNCHANGED && !route_ping_addr_equal(&state->local, observed, 1)) || (link->conn->nat_addr.ss_family && !route_ping_addr_equal(&link->conn->nat_addr, observed, 1))) { if (state->phase) owner_finish(state, NAT_REASON_ADDRESS_CHANGED); clear_profile(state, NAT_REASON_ADDRESS_CHANGED); state->due = get_time_tb() + 2500; } } void nat_detection_socket_removed(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket) { if (!detector || !socket || detector->stopping) return; if (!socket->probe_only) { detector->publish_pending = 1; detector->publish_after = get_time_tb(); } struct nat_probe *probe = detector->probes; while (probe) { struct nat_probe *next = probe->next; if ((probe->source_socket == socket->sock_id && probe->source_generation == socket->generation) || (probe->io_socket == socket->sock_id && probe->io_generation == socket->generation)) helper_remove(probe); probe = next; } struct nat_socket **cursor = &detector->sockets; while (*cursor && (*cursor)->socket != socket) cursor = &(*cursor)->next; if (!*cursor) return; struct nat_socket *state = *cursor; *cursor = state->next; etcp_cancel_pings(detector->instance, owner_ping_cb, state); struct ETCP_SOCKET *primary = state->helper_primary, *auxiliary = state->auxiliary; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT endpoint removed socket=%s generation=%016llx", socket->name, (unsigned long long)socket->generation); u_free(state); if (primary) etcp_socket_remove(primary); if (auxiliary) etcp_socket_remove(auxiliary); } static void socket_changed(struct ETCP_SOCKET *socket, int event, void *arg) { struct NAT_DETECTION *detector = arg; struct nat_socket *state = socket_state(detector, socket, 0); if (!state || !(event & ETCP_SOCKET_EVENT_ADDR_CHANGED)) return; clear_profile(state, NAT_REASON_ADDRESS_CHANGED); nat_detection_socket_removed(detector, socket); if (random_id(&socket->generation)) return; socket_state(detector, socket, 1); } struct NAT_DETECTION *nat_detection_create(struct UTUN_INSTANCE *instance) { if (!instance || !instance->topo_groups || !instance->router_conns || instance->router_bindings.callbacks[ETCP_RT_ID_NAT_DETECTION]) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT detector invalid instance/binding collision"); return NULL; } struct NAT_DETECTION *detector = u_calloc(1, sizeof(*detector)); if (!detector) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT detector allocation failed"); return NULL; } detector->instance = instance; detector->automatic = 1; if (etcp_router_bind(instance, ETCP_RT_ID_NAT_DETECTION, receive_control)) { u_free(detector); return NULL; } etcp_add_conn_status_cbk(instance, connection_status, detector); etcp_add_socket_cbk(instance, socket_changed, detector, ETCP_SOCKET_EVENT_ADDR_CHANGED); detector->timer = uasync_set_timeout(instance->ua, NAT_TICK_TB, detector, detector_tick, "nat_detection"); if (!detector->timer) { etcp_remove_conn_status_cbk(instance, connection_status, detector); etcp_remove_socket_cbk(instance, socket_changed, detector); etcp_router_unbind(instance, ETCP_RT_ID_NAT_DETECTION); u_free(detector); DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT detector initial timer failed"); return NULL; } DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT endpoint detector started: UDP mapping/inbound measured separately, TCP listener probes"); return detector; } void nat_detection_destroy(struct NAT_DETECTION *detector) { if (!detector) return; detector->stopping = 1; if (detector->timer) uasync_cancel_timeout(detector->instance->ua, detector->timer); if (detector->topology_wake) uasync_call_soon_cancel(detector->instance->ua, detector->topology_wake); etcp_remove_conn_status_cbk(detector->instance, connection_status, detector); etcp_remove_socket_cbk(detector->instance, socket_changed, detector); etcp_router_unbind(detector->instance, ETCP_RT_ID_NAT_DETECTION); while (detector->probes) helper_remove(detector->probes); while (detector->sockets) { struct nat_socket *state = detector->sockets; detector->sockets = state->next; etcp_cancel_pings(detector->instance, owner_ping_cb, state); struct ETCP_SOCKET *primary = state->helper_primary, *auxiliary = state->auxiliary; u_free(state); if (primary) etcp_socket_remove(primary); if (auxiliary) etcp_socket_remove(auxiliary); } while (detector->bindings) { struct nat_binding *binding = detector->bindings; detector->bindings = binding->next; u_free(binding); } DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT detector stopped; all endpoint rounds and helper probes cancelled"); u_free(detector); } void nat_detection_request_check_all(struct NAT_DETECTION *detector) { if (!detector || detector->stopping) return; for (struct ETCP_SOCKET *socket = detector->instance->etcp_sockets; socket; socket = socket->next) { struct nat_socket *state = socket_state(detector, socket, 1); if (!state) continue; if (state->phase) owner_finish(state, NAT_REASON_CANCELLED); state->cycling = 0; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0}; state->using_fallback = 0; state->due = get_time_tb(); state->forced = 1; state->preferred_observer = 0; } DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT endpoint recheck requested"); } int nat_detection_request_check_socket(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, uint64_t observer) { struct nat_socket *state = socket_state(detector, socket, 1); if (!state || !observer) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT endpoint recheck has invalid endpoint/observer"); return -1; } if (state->phase) owner_finish(state, NAT_REASON_CANCELLED); state->cycling = 0; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0}; state->using_fallback = 0; state->preferred_observer = observer; state->due = get_time_tb(); state->forced = 1; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT endpoint recheck socket=%s observer=%016llx", socket->name, (unsigned long long)observer); return 0; } void nat_detection_set_allow_local(struct NAT_DETECTION *detector, int allow) { if (detector) detector->allow_local = !!allow; } void nat_detection_set_automatic(struct NAT_DETECTION *detector, int automatic) { if (!detector) return; detector->automatic = !!automatic; DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT automatic endpoint measurements %s", automatic ? "enabled" : "disabled"); } int nat_detection_get_status(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, struct nat_check_status *status) { struct nat_socket *state = socket_state(detector, socket, 0); if (!state || !status) return -1; *status = (struct nat_check_status){.generation = socket->generation, .round = state->round, .group_id = state->group_id, .observer = state->observer, .helper = state->helper, .rounds = state->rounds, .helper_reselections = state->helper_reselections, .phase = state->phase, .reason = state->reason, .history_complete = state->history_complete, .measured_profile = state->measured_profile, .effective_profile = socket->address_profile, .fallback_allowed = state->fallback_allowed}; return 0; }