Commit Graph
1387 Commits
Author SHA1 Message Date
Evgeny d0d4199273 etcp: unified send hook via etcp_udp_send, remove loss_rate, add dummynet_filter inline mode, rewrite BGP test
- Add etcp_udp_send_fn_t hook typedef, send_hook/send_hook_ctx fields to ETCP_LINK
- Implement etcp_udp_send() wrapper — all UDP sends go through one point
- Remove loss_rate from ETCP_SOCKET, CFG_SERVER, config parser, send path
- Add dummynet_filter: inline send-hook with loss/delay, no proxy socket needed
- Rewrite test_bgp_route_exchange: 3-node chain A(2 links)↔B↔C, 7 phases
  with dummynet_filter-based link failure/recovery simulation
- Clean up tests: remove skeleton test_routing_mesh, 6 debug/stale test files
- Refactor test_etcp_simple_traffic and test_pkt_normalizer_etcp (remove debug spam)
- Fix test_etcp_two_instances: remove private field access, fix false PASS on timeout
2026-04-27 12:43:52 +03:00
Evgeny e7c32d9ac1 1 2026-04-26 19:58:37 +03:00
Evgeny 7d03749522 1 2026-04-26 19:53:05 +03:00
Evgeny 61fee27478 congestion: burst-based BW measurement + adaptive inflight (slow start/cong avoidance/loss response)
New features:
- get_time_us() in lib/u_async.c/h for µs-precision timing
- Burst measurement (ETCP_SECTION_MEAS_TS 0x07, MEAS_RESP 0x08, FILLER 0x09)
  - 12 MTU packets sent at line rate, receiver measures inter-packet gaps
  - Burst triggered when link saturated + min 500ms interval
  - Result: measured BW -> bandwidth for shaper, burst_target_bdp for window cap
- 3-phase adaptive inflight in link_stats_timer_cb:
  - Slow start: windows doubles each RTT, exits on loss/RTT spike/threshold
  - Congestion avoidance: additive increase, proportional RTT shrink, multiplicative loss shrink
  - Burst target cap: inflight_lim <= burst_target_bdp * 1.1
- Dynamic optimal_inflight = sum(link->inflight_lim_bytes), recalculated on change
- Adaptive win_timebase = rtt/2 (was hardcoded 10ms)

Fixes:
- dummynet.c: shaper formula fixed (80000 -> 80)
- dummynet.c: queue_data_put call fixed (3 args -> 2)
- etcp.c: SIZE ERROR format string (missing log_name argument)
- etcp_request_pkt: FILLER leaves room for PAYLOAD overhead

Tests:
- test_etcp_congestion.c: single-threaded, 5-phase BW/delay/loss test
  - sender + receiver + dummynet in one uasync
  - CSV metrics output every 100ms
- All 29 existing tests pass
2026-04-26 00:17:32 +03:00
Evgeny edfe665837 log: move individual ping logs from INFO/TRACE to DEBUG level 2026-04-25 21:59:34 +03:00
Evgeny 6c057ca89a fix BGP: delay up_cbk until connection initialized, fix send_table_request leak 2026-04-25 21:46:35 +03:00
Evgeny a356eedc0d cleanup BGP/connection logging
- Consolidate on_up/on_down to single INFO line with peer_id
- Remove DEBUG_CATEGORY_DEBUG duplications from route_bgp.c
- NODEINFO: inline lightweight dump (no malloc), verbose log -> DEBUG
- NAT_INFO: remove hex dump duplications, single clean INFO line
- Send NODEINFO per-recipient: INFO->DEBUG_DEBUG
- Per-ping callbacks: INFO->DEBUG_TRACE
- Per-packet traffic direction: INFO->DEBUG_TRACE
- route_insert/delete: use node_id instead of pointer
- Add cancel_all log, add_to_senders conn name
- send_full_table: ERROR->WARN for consistency checks
- is_local_subnet: DEBUG->TRACE
- Fix etcp_send_ping BGP category
2026-04-25 20:06:48 +03:00
Evgeny 227a1aafad cleanup debug logging 2026-04-25 19:43:51 +03:00
Evgeny 048d1c18b1 fix: restore lost if(changed) block in update_my_nodeinfo + add debug warnings 2026-04-25 19:41:17 +03:00
Evgeny 9c509d4c86 add connectivity probing + NAT type rename OPEN->EIM, RESTRICTED->STRICT
- Rename NAT types: OPEN->EIM, RESTRICTED->STRICT across all code
- Add flat addr list {type,ip,port,socket_id} to NODEINFO (NODEINFO_IPV4_ADDR)
- Add NODEINFO_IPV4_SOCKET_META for per-socket metadata
- Add NODE_CONNECTIVITY with per-type probe status and min_rtt
- New module: route_connectivity.c/h (probing engine with socket fallback)
- STRICT verified addresses excluded from NODEINFO broadcast
- Trigger probing on new/updated BGP node, cancel on remove/withdraw
- Update NAT type names in etcpmon GUI
2026-04-25 18:41:45 +03:00
Evgeny 39544e6b2b Fix: Add bounds/mtu checks, underflow protection and null deref fix for ETCP robustness 2026-04-25 12:38:05 +03:00
Evgeny 595d2b177b Refactor: Replace custom tc_hton*/tc_ntoh* with standard htobe16/be16toh/htobe32/be32toh, move htobe16/be16toh to platform_compat.h 2026-04-25 01:16:09 +03:00
Evgeny 8cb05da11c Fix: Add forward declaration of struct UTUN_INSTANCE in route_node.h 2026-04-25 00:54:20 +03:00
Evgeny 907ef244c6 NAT: Full Cone (EIM) NAT with ETCP_ID_NAT protocol, per-node routing, and comprehensive test suite
- New ETCP protocol ETCP_ID_NAT (0x02) for NAT traffic between nodes
- eim_nat.c/h: Pure NAT engine (table, port allocation, checksum updates, egress/ingress)
- nat_transport.c/h: Transport layer (NAT TUN, ETCP binding, client/provider roles)
- route_bgp_find_conn_for_node(): shared utility for optimal connection lookup
- tun_init_nat(): dedicated TUN creation for NAT
- Config: [nat] section with tun_ip, port_start/end, nat_via, forward rules
- Fix: csum_update_ip/transport use htonl() halves (correct on LE systems)
- Fix: IP_FRAG_MF_MASK changed to 0x2000 (after ntohs, MF is bit 13)
- Tests: 26 engine unit + 4 transport integration + 7 stress (29 total)
2026-04-25 00:43:25 +03:00
Evgeny b7da11c2bf Crypto: Refactored ETCP header size calculations with offsetof-based defines, increased CCM tag to 16 bytes 2026-04-24 16:03:07 +03:00
Evgeny a66edb8f5f Crypto: Added SHA256-based KDF for session key derivation from ECDH shared secret 2026-04-24 15:43:58 +03:00
Evgeny 11d511797a Fix compiler warnings: remove incorrect address-of operator and add getpid for Windows
- etcp_connections.c:1142: remove & before addr in sockaddr_storage_to_str call
- test_etcp_simple_traffic.c: add #include <process.h> and #define getpid _getpid for Windows
2026-04-24 14:48:29 +03:00
Evgeny e0ce9fefc3 Fix allowed_keys check to only apply to ETCP_INIT packets, not PING/PONG
The allowed_keys authorization was incorrectly checking all INIT-decrypted
packets including PING/PONG. Moved the check after packet type validation
so it only applies to ETCP_INIT_REQUEST/NOINIT (incoming connection requests).

Fixes test_etcp_ping and test_nat_detection failures.
2026-04-24 14:37:08 +03:00
Evgeny 6aedf5fe91 Replace buggy get_default_route_ip() with get_outgoing_local_ip() for reliable local IP detection
- Add lib/getmyip.c/h with get_outgoing_local_ip() using UDP connect+getsockname
- Update etcp_socket_add() to use new function for 0.0.0.0 and :: bindings
- Support both IPv4 (8.8.8.8) and IPv6 (2001:4860:4860::8888) auto-detection
- Remove broken get_default_route_ip() from platform_compat (it returned gateway IP, not local IP)
- Keep get_interface_ip_by_index() for explicit interface bindings
- Update Makefile.am to include new getmyip module
2026-04-24 14:07:41 +03:00
Evgeny 724a9ff5c7 Add getmyip module for reliable local IP detection
- New lib/getmyip.c/h with get_outgoing_ip(), get_default_local_ip(), get_local_ip_by_index()
- Uses UDP connect + getsockname instead of /proc/net/route parsing
- Supports both IPv4 and IPv6
- Updated etcp_socket_add() to use new functions for interface_addr detection
2026-04-24 13:58:24 +03:00
Evgeny 697aee0351 Socket info: show config and netif IP in log, monitor shows interface IP 2026-04-24 01:23:52 +03:00
jeka 30413ebce0 1 2026-04-24 00:43:59 +03:00
Evgeny e39979bfac Config: Added [allowed_keys] section for client public key authorization on server
- Added CFG_ALLOWED_KEY struct with binary key storage (64 bytes) for fast memcmp comparison
- Implemented parsing of [allowed_keys] section with allow_all flag and per-key entries
- Added key validation in etcp_connections.c server handler (errorcode=8 for rejected keys)
- Default behavior: deny all if [allowed_keys] section is missing or allow_all=0 with no keys
- Added proper memory cleanup and debug logging for allowed_keys configuration
- Updated all test configs to include [allowed_keys] allow_all=1 for backward compatibility
2026-04-24 00:16:30 +03:00
Evgeny 3625a4e5d5 Restart NAT check when client address changes in INIT_REQUEST 2026-04-23 23:56:49 +03:00
Evgeny a3063ea47a NODEINFO: Add nat_addr/nat_port fields, use interface_addr for addr/port 2026-04-23 23:21:12 +03:00
Evgeny f02603f8ac Add socket type (public/nat/private) to handshake and skip private nodes in NAT detection 2026-04-23 22:44:43 +03:00
Evgeny 0c748501c8 GUI: Add NAT address to socket display in etcpmon 2026-04-23 22:02:42 +03:00
Evgeny 72c6b6f197 Reset NAT check status on etcp_conn_reinit when session_id changes
When peer's random session_id changes, etcp_conn_reinit() is called.
Previously NAT check status was not reset, so route_bgp_new_conn()
skipped NAT re-detection because nat_check_status was still
NAT_CHECK_IN_PROGRESS from the previous session.

Now etcp_conn_reinit():
- Cancels pending NAT-ping requests via route_ping_cancel_for_conn()
- Resets nat_type and nat_check_status to UNKNOWN/NONE for all links

This ensures NAT type is re-detected for all links after reinit.
2026-04-23 20:00:09 +03:00
Evgeny 4b9bffffbd Debug: Add diagnostic logging for PONG handling to trace ping response issues 2026-04-23 13:18:59 +03:00
Evgeny bcb1c37c87 Fix two more UAF in route_bgp: move DEBUG_INFO before etcp_send
- route_bgp_send_nat_info: moved ip_to_str(pkt->nat_ip) log before etcp_send
- route_bgp_send_nat_check_req: moved ip_to_str(pkt->interface_ip) log before etcp_send

Both were reading from pkt memory that could be freed by etcp_send callback.
2026-04-23 02:09:09 +03:00
Evgeny 3959ed8afe Fix use-after-free: read request_id before etcp_send frees req_pkt
req_pkt memory could be freed by etcp_send callback before pending->request_id
was assigned. Store request_id in local variable and use it for pending.
2026-04-23 01:59:41 +03:00
Evgeny 5b6455ccff Add debug logging for request_id tracking and pending list dump 2026-04-23 01:48:16 +03:00
Evgeny ae1c59f3a9 Fix unaligned uint64_t access in packed struct causing garbage request_id
- Changed request_id from uint64_t to uint32_t in BGP_PING_REQUEST/RESPONSE
- Added uint16_t reserved field after subcmd for proper alignment
- Updated next_ping_req_id from uint64_t to uint32_t in ROUTE_BGP
- Fixed all log format strings from %016llx to %08x
- All 26 tests passing
2026-04-23 01:38:17 +03:00
Evgeny 4760baa973 1 2026-04-23 01:15:15 +03:00
Evgeny 33fd5bc123 ETCP: add session_id to prevent false reinit on new link addition
- Added 32-bit session_id to ETCP_CONN struct (random per client)
- Session_id transmitted in INIT_REQUEST/RESPONSE after node_id
- Server: skip reinit if session_id matches existing connection
- Server: send INIT_RESPONSE (0x03) only when reinit actually done
- Server: send INIT_RESPONSE_NOINIT (0x05) for same session
- Client: ignore INIT_RESPONSE with wrong session_id
- Fixed tx_state initialization in etcp_conn_ready for noinit case
- All 26 tests passing
2026-04-22 23:53:25 +03:00
Evgeny 04f02ffead NAT: полное решение для отмены pending при падении conn
1. Добавлен via_conn в struct route_ping_pending - отслеживает через
   какое соединение (third_conn) отправлен запрос на ping
2. route_ping_cancel_for_conn теперь отменяет pending при падении ЛЮБОГО
   из двух conn: владельца линка (link->etcp) ИЛИ third_conn (via_conn)
3. nat_link_check_cb проверяет link->nat_check_status == IN_PROGRESS
   перед обработкой - защита от race condition когда conn упал раньше
   чем пришел ответ
4. Убраны избыточные DEBUG-логи, оставлены только информативные
2026-04-22 23:08:55 +03:00
Evgeny 07db51382e NAT: исправлена ошибка в route_ping_cancel_for_conn + добавлен дебаг
- Исправлена критическая ошибка: использовалось na->link->conn вместо
  na->link->etcp (struct ETCP_LINK.conn = ETCP_SOCKET*, а .etcp = ETCP_CONN*)
- Добавлен подробный дебаг в 4 места:
  * route_ping_send_req_addr - при создании pending
  * route_ping_cancel_for_conn - при поиске и отмене
  * route_bgp_remove_conn - состояние линков перед отменой
  * etcp_connection_close - очистка линков при закрытии
2026-04-22 22:58:36 +03:00
Evgeny ca42e2fa66 NAT: отмена pending NAT checks при удалении соединения
- route_bgp_remove_conn теперь отменяет pending NAT checks перед удалением
- Добавлена route_ping_cancel_for_conn: ищет pending запросы для conn,
  отменяет таймеры, сбрасывает arg->link = NULL, удаляет из списка
- struct nat_check_arg вынесена в route_bgp.h для видимости из route_ping.c
- После отмены pending, статусы линков с NAT_CHECK_IN_PROGRESS сбрасываются в NONE
2026-04-22 22:16:06 +03:00
Evgeny 7a4e7ca4ae Backup before NAT cancel fix 2026-04-22 22:07:33 +03:00
Evgeny 8e9db5211a 1 2026-04-22 21:16:46 +03:00
Evgeny 57dba26e7c GUI: поле действия и кнопка Send для отправки команд в utun
- Добавлен ETCPMON_CMD_ACTION в протокол (32 байта текст)
- В GUI добавлено поле ввода и кнопка Send правее блока Sockets
- Добавлен etcpmon_client_send_action для отправки команд
- Обработка action 'nat' в control_server: запускает NAT check для всех линков
- Сделана route_bgp_start_link_nat_check не-static
- Добавлена route_bgp_request_nat_check_all
- Исправлен баг route_ping.c (request_id -> nonce)
- Исправлена линковка tests/Makefile.am
2026-04-22 20:52:30 +03:00
Evgeny 5b0a743e02 1 2026-04-22 20:35:05 +03:00
Evgeny 0984ea749a Исправлен byte order для IP адресов в NAT detection flow
- etcp_connections.c: парсинг NAT IP через memcpy (сохраняет NBO)
- route_bgp.c: убраны ntohl() для IP в NAT detection и route_bgp_send_nat_info
- route_ping.c: убран htonl() для IP в ping request
- тесты: обновлены ожидания NBO для nat_ip
2026-04-22 18:23:32 +03:00
Evgeny b466d14fe4 Добавлено поле interface_addr в ETCP_SOCKET и interface_ip/port в BGP_NAT_CHECK_REQ
- platform_compat: get_interface_ip_by_index() для получения IP по ifindex
- ETCP_SOCKET: interface_addr заполняется из конфига/interface/default route
- BGP_NAT_CHECK_REQ: добавлены interface_ip/interface_port (network byte order)
- route_bgp_send_nat_check_req: отправляет interface_addr линка
- route_bgp_handle_nat_check_req: принимает interface данные в sockaddr-заглушку
2026-04-22 18:16:49 +03:00
Evgeny 385aa066b2 Add duplicate packet counter to ACK frames
- Add rx_dup_count (local) and tx_dup_count (remote via ACK) to ETCP_CONN
- Extend ACK section format: add 16-bit rx_dup_count after last_delivered_id
- Reconstruct 32-bit tx_dup_count from received 16-bit diff
- Update etcpmon protocol and control_server to expose counters
- Update packet dumper for new ACK format
2026-04-22 16:04:11 +03:00
Evgeny ceef0d701a Suppress route command output to console 2026-04-22 12:42:52 +03:00
Evgeny cbde38d52a Remove accidentally committed binaries and temp files 2026-04-22 11:45:54 +03:00
Evgeny 5d95f6db30 Merge branch 'master' of http://git.syqiq.tech/jeka/utun2 2026-04-22 11:40:24 +03:00
Evgeny c136db5666 Fix: round-robin link selection + byte order fixes + local_node update 2026-04-22 11:39:50 +03:00
Evgeny 77fd7c19de WIP: debug byte order diagnostics 2026-04-22 02:14:47 +03:00