diff options
| author | Your Name <you@example.com> | 2026-05-23 04:50:26 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-23 04:50:26 +0530 |
| commit | 9330b01c28aebc865a3c0a51df8730196cb4152e (patch) | |
| tree | 60c3ae35b2550c737c228e81fd342c8d07af11af /main/firewall.c | |
| parent | 851801f50f1282ab1db5f8a241dbd245f59e447e (diff) | |
Phase 6a-6d: Consolidate and clean up
6a: Delete dead standalone tollgate_core/ (12 files, never compiled)
6b: Rewrite dns_server.c as thin shim to component's tollgate_core_dns
- Fix component DNS to bind to AP IP instead of INADDR_ANY
6c: Rewrite stratum_proxy.c as thin shim with struct cast to component types
6d: Move sandbox logic into component's tollgate_core_firewall
- Add tollgate_core_fw_set_sandbox_ports/set_sandbox_mint_access
- Add is_sandbox_allowed() to component's ip4_canforward_filter
- Clean main/firewall.c to delegate filter to component
- Remove redundant DNS auth double-calls from main firewall
Add ROADMAP.md with full extraction plan and checklists
All 21 unit tests pass. ESP-IDF build passes.
Diffstat (limited to 'main/firewall.c')
| -rw-r--r-- | main/firewall.c | 72 |
1 files changed, 4 insertions, 68 deletions
diff --git a/main/firewall.c b/main/firewall.c index 077d16c..5ffdee0 100644 --- a/main/firewall.c +++ b/main/firewall.c | |||
| @@ -1,36 +1,28 @@ | |||
| 1 | #include "firewall.h" | 1 | #include "firewall.h" |
| 2 | #include "dns_server.h" | ||
| 3 | #include "tollgate_core.h" | 2 | #include "tollgate_core.h" |
| 4 | #include "tollgate_core_firewall.h" | 3 | #include "tollgate_core_firewall.h" |
| 5 | #include "esp_log.h" | 4 | #include "esp_log.h" |
| 6 | #include "lwip/netif.h" | ||
| 7 | #include "lwip/lwip_napt.h" | ||
| 8 | #include "lwip/prot/ip4.h" | 5 | #include "lwip/prot/ip4.h" |
| 9 | #include "lwip/prot/tcp.h" | ||
| 10 | #include "lwip/prot/ip.h" | ||
| 11 | #include <string.h> | 6 | #include <string.h> |
| 12 | 7 | ||
| 13 | static const char *TAG = "firewall"; | 8 | static const char *TAG = "firewall"; |
| 14 | static esp_ip4_addr_t s_ap_ip; | 9 | static esp_ip4_addr_t s_ap_ip; |
| 15 | static uint16_t s_mining_port = 3333; | ||
| 16 | static bool s_sandbox_mint_access = false; | ||
| 17 | 10 | ||
| 18 | esp_err_t firewall_init(esp_ip4_addr_t ap_ip) | 11 | esp_err_t firewall_init(esp_ip4_addr_t ap_ip) |
| 19 | { | 12 | { |
| 20 | s_ap_ip = ap_ip; | 13 | s_ap_ip = ap_ip; |
| 21 | ip_napt_enable(s_ap_ip.addr, 1); | 14 | ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR, IP2STR(&s_ap_ip)); |
| 22 | ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR " (NAT always on, per-client filter)", IP2STR(&s_ap_ip)); | ||
| 23 | return ESP_OK; | 15 | return ESP_OK; |
| 24 | } | 16 | } |
| 25 | 17 | ||
| 26 | void firewall_set_mining_port(uint16_t port) | 18 | void firewall_set_mining_port(uint16_t port) |
| 27 | { | 19 | { |
| 28 | s_mining_port = port; | 20 | tollgate_core_fw_set_sandbox_ports(port); |
| 29 | } | 21 | } |
| 30 | 22 | ||
| 31 | void firewall_set_sandbox_mint_access(bool enabled) | 23 | void firewall_set_sandbox_mint_access(bool enabled) |
| 32 | { | 24 | { |
| 33 | s_sandbox_mint_access = enabled; | 25 | tollgate_core_fw_set_sandbox_mint_access(enabled); |
| 34 | } | 26 | } |
| 35 | 27 | ||
| 36 | esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size) | 28 | esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size) |
| @@ -38,80 +30,24 @@ esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_ | |||
| 38 | return tollgate_core_fw_get_mac_for_ip(client_ip, mac_out, mac_out_size); | 30 | return tollgate_core_fw_get_mac_for_ip(client_ip, mac_out, mac_out_size); |
| 39 | } | 31 | } |
| 40 | 32 | ||
| 41 | static bool is_sandbox_allowed(struct pbuf *p) | ||
| 42 | { | ||
| 43 | if (p->len < IP_HLEN) return false; | ||
| 44 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; | ||
| 45 | uint32_t dest_ip_h = lwip_ntohl(iphdr->dest.addr); | ||
| 46 | uint32_t ap_ip_h = lwip_ntohl(s_ap_ip.addr); | ||
| 47 | |||
| 48 | if (dest_ip_h == ap_ip_h) { | ||
| 49 | if (iphdr->_proto == IP_PROTO_TCP) { | ||
| 50 | uint16_t dst_port = 0; | ||
| 51 | if (p->len >= IP_HLEN + TCP_HLEN) { | ||
| 52 | struct tcp_hdr *tcphdr = (struct tcp_hdr *)((uint8_t *)p->payload + IP_HLEN); | ||
| 53 | dst_port = lwip_ntohs(tcphdr->dest); | ||
| 54 | } | ||
| 55 | if (dst_port == 80 || dst_port == 2121 || dst_port == s_mining_port) { | ||
| 56 | return true; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | if (iphdr->_proto == IP_PROTO_UDP) { | ||
| 60 | return true; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | if (s_sandbox_mint_access && iphdr->_proto == IP_PROTO_TCP) { | ||
| 65 | return true; | ||
| 66 | } | ||
| 67 | |||
| 68 | return false; | ||
| 69 | } | ||
| 70 | |||
| 71 | int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder) | 33 | int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder) |
| 72 | { | 34 | { |
| 73 | (void)dest_addr_hostorder; | 35 | return tollgate_core_ip4_canforward_filter(p, dest_addr_hostorder); |
| 74 | if (p->len < IP_HLEN) return -1; | ||
| 75 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; | ||
| 76 | uint32_t src_ip_h = lwip_ntohl(iphdr->src.addr); | ||
| 77 | uint32_t ap_subnet = lwip_ntohl(s_ap_ip.addr) & 0xFFFFFF00; | ||
| 78 | if ((src_ip_h & 0xFFFFFF00) != ap_subnet) { | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | if (firewall_is_client_allowed(iphdr->src.addr)) { | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | if (is_sandbox_allowed(p)) { | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | return 0; | ||
| 88 | } | 36 | } |
| 89 | 37 | ||
| 90 | void firewall_grant_access(uint32_t client_ip) | 38 | void firewall_grant_access(uint32_t client_ip) |
| 91 | { | 39 | { |
| 92 | tollgate_core_fw_grant(client_ip); | 40 | tollgate_core_fw_grant(client_ip); |
| 93 | dns_server_set_client_authenticated(client_ip, true); | ||
| 94 | |||
| 95 | char mac[18] = {0}; | ||
| 96 | tollgate_core_fw_get_mac_for_ip(client_ip, mac, sizeof(mac)); | ||
| 97 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 98 | ESP_LOGI(TAG, "Access granted to " IPSTR " mac=%s", IP2STR(&ip_addr), | ||
| 99 | mac[0] ? mac : "unknown"); | ||
| 100 | } | 41 | } |
| 101 | 42 | ||
| 102 | void firewall_revoke_access(uint32_t client_ip) | 43 | void firewall_revoke_access(uint32_t client_ip) |
| 103 | { | 44 | { |
| 104 | tollgate_core_fw_revoke(client_ip); | 45 | tollgate_core_fw_revoke(client_ip); |
| 105 | dns_server_set_client_authenticated(client_ip, false); | ||
| 106 | |||
| 107 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 108 | ESP_LOGI(TAG, "Access revoked for " IPSTR, IP2STR(&ip_addr)); | ||
| 109 | } | 46 | } |
| 110 | 47 | ||
| 111 | void firewall_revoke_all(void) | 48 | void firewall_revoke_all(void) |
| 112 | { | 49 | { |
| 113 | tollgate_core_fw_revoke_all(); | 50 | tollgate_core_fw_revoke_all(); |
| 114 | ESP_LOGI(TAG, "All client access revoked"); | ||
| 115 | } | 51 | } |
| 116 | 52 | ||
| 117 | bool firewall_is_client_allowed(uint32_t client_ip) | 53 | bool firewall_is_client_allowed(uint32_t client_ip) |