From 6938bfcb6a3ac9e558430605426a89ee6ae56016 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 30 Mar 2026 19:00:08 +0300 Subject: [PATCH] Routing: add local routes to table via hop_count==0 detection --- src/route_lib.c | 3 +-- src/routing.c | 15 ++++----------- src/utun_instance.c | 15 ++++----------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/route_lib.c b/src/route_lib.c index 8e4ccf9..ac1ed5f 100644 --- a/src/route_lib.c +++ b/src/route_lib.c @@ -277,8 +277,7 @@ void route_table_print(const struct ROUTE_TABLE *table) { const struct ROUTE_ENTRY *entry = &table->entries[i]; DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " %zu: %s/%d", i + 1, ip_to_string(entry->network).a, entry->prefix_length); - - if (entry->v_node_info) { + if (entry->v_node_info && entry->v_node_info->node.hop_count != 0) { DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " v_node_info=%p", (void*)entry->v_node_info); } else { DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " LOCAL"); diff --git a/src/routing.c b/src/routing.c index e7fba94..009f558 100644 --- a/src/routing.c +++ b/src/routing.c @@ -140,16 +140,12 @@ static void route_pkt(struct UTUN_INSTANCE* instance, struct ll_entry* entry, ui return; } - // Determine destination node ID and connection - uint64_t dst_node_id = instance->node_id; // Default to local node + uint64_t dst_node_id = instance->node_id; struct ETCP_CONN* conn = NULL; - - if (route->v_node_info == NULL) { - // Local route - send to TUN + struct NODEINFO_Q* nq = route->v_node_info; + if (!nq || nq->node.hop_count == 0) { DEBUG_TRACE(DEBUG_CATEGORY_ROUTING, "Local route to %s", ip_to_str(&addr, AF_INET).str); } else { - // Learned route - use first available path from NODEINFO_Q - struct NODEINFO_Q* nq = route->v_node_info; if (nq->paths && nq->paths->head) { struct ll_entry* path_entry = nq->paths->head; conn = (struct ETCP_CONN*)path_entry->data; @@ -163,7 +159,6 @@ static void route_pkt(struct UTUN_INSTANCE* instance, struct ll_entry* entry, ui return; } dst_node_id = conn->peer_node_id; - if (!conn->normalizer) { DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "route_pkt: connection for %s has no normalizer", ip_to_str(&addr, AF_INET).str); instance->dropped_packets++; @@ -179,8 +174,7 @@ static void route_pkt(struct UTUN_INSTANCE* instance, struct ll_entry* entry, ui } else DEBUG_INFO(DEBUG_CATEGORY_TRAFFIC, "NODE %016llx -> NODE %016llx", (unsigned long long)src_node_id, (unsigned long long)dst_node_id); - if (route->v_node_info == NULL) { - // Local route - send to TUN (entry has [cmd=0][IP data], TUN skips cmd byte) + if (!nq || nq->node.hop_count == 0) { int put_err = queue_data_put(instance->tun->input_queue, entry); if (put_err != 0) { DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "route_pkt: failed to put to TUN: dst=%s err=%d", @@ -190,7 +184,6 @@ static void route_pkt(struct UTUN_INSTANCE* instance, struct ll_entry* entry, ui queue_dgram_free(entry); return; } - // Entry sent to TUN, don't free here instance->routed_packets++; DEBUG_TRACE(DEBUG_CATEGORY_ROUTING, "route_pkt: sent %zu bytes to TUN", ip_len); return; diff --git a/src/utun_instance.c b/src/utun_instance.c index 1313b72..75d4525 100644 --- a/src/utun_instance.c +++ b/src/utun_instance.c @@ -74,10 +74,6 @@ static int instance_init_common(struct UTUN_INSTANCE* instance, struct UASYNC* u return -1; } DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Routing module created"); - - if(instance->bgp&&instance->bgp->local_node){if(route_insert(instance->rt,instance->bgp->local_node)){DEBUG_INFO(DEBUG_CATEGORY_ROUTING,"Added local routes from local_node");}else{DEBUG_WARN(DEBUG_CATEGORY_ROUTING,"Failed to add local routes");}} - - // Initialize TUN device if enabled if (g_tun_init_enabled) { instance->tun = tun_init(ua, config); if (!instance->tun) { @@ -85,8 +81,6 @@ static int instance_init_common(struct UTUN_INSTANCE* instance, struct UASYNC* u return -1; } DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN interface initialized: %s", instance->tun->ifname); - - // Add system routes for route_subnets if (config->route_subnets) { int added = tun_route_add_all(instance->tun->ifindex, instance->tun->ifname, config->route_subnets); DEBUG_INFO(DEBUG_CATEGORY_TUN, "Added %d system routes for TUN interface", added); @@ -96,20 +90,19 @@ static int instance_init_common(struct UTUN_INSTANCE* instance, struct UASYNC* u DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN initialization disabled - skipping TUN device setup"); instance->tun = NULL; } - - // Initialize sockets first (needed for BGP nodeinfo) if (init_sockets(instance) < 0) { DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to initialize sockets"); return -1; } - - // Initialize BGP module for route exchange instance->bgp = route_bgp_init(instance); if (!instance->bgp) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Failed to initialize BGP module"); - // Non-fatal: BGP is optional for basic operation } else { DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP module initialized"); + if (instance->rt && instance->bgp->local_node) { + route_bgp_update_my_nodeinfo(instance,instance->bgp); + if (route_insert(instance->rt,instance->bgp->local_node)) DEBUG_INFO(DEBUG_CATEGORY_ROUTING,"Added local routes"); else DEBUG_WARN(DEBUG_CATEGORY_ROUTING,"Failed to add local routes"); + } } // Initialize firewall