|
| 1 | +/** |
| 2 | + * @copyright Utilitech AS 2023-2026 |
| 3 | + * License: Fair Source |
| 4 | + * |
| 5 | + * @brief Keeps the IPv4 DNS servers from being evicted by IPv6 ones |
| 6 | + * |
| 7 | + * @details lwIP keeps a single global DNS server table, and both nd6 RDNSS and |
| 8 | + * stateless DHCPv6 start writing IPv6 servers at index 0, evicting the IPv4 |
| 9 | + * servers handed out by DHCP. RDNSS is re-applied on every router |
| 10 | + * advertisement, and those are processed even when IPv6 is disabled in our |
| 11 | + * configuration, since lwIP is always joined to the link-local all-nodes group. |
| 12 | + * |
| 13 | + * We therefore reserve the first slots of the table for IPv4 and the last one |
| 14 | + * for IPv6, which lets both stacks resolve names on a dual stack network. |
| 15 | + * |
| 16 | + * The table is touched from the lwIP thread, the network event handler and the |
| 17 | + * main loop. All the state here is word sized and every disagreement is fixed |
| 18 | + * by the next reconcile, so no locking is needed. Taking one on the lwIP thread |
| 19 | + * would not be safe anyway. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "DnsGuard.h" |
| 23 | + |
| 24 | +#if defined(ESP32) |
| 25 | + |
| 26 | +#include <lwip/dns.h> |
| 27 | + |
| 28 | +#if DNS_MAX_SERVERS < 3 |
| 29 | +#error "DnsGuard needs room for both an IPv4 and an IPv6 DNS server" |
| 30 | +#endif |
| 31 | + |
| 32 | +// Slots reserved for IPv4, the remaining one is where IPv6 is parked |
| 33 | +#define DNS_V4_SLOTS (DNS_MAX_SERVERS - 1) |
| 34 | + |
| 35 | +extern "C" void __real_dns_setserver(u8_t numdns, const ip_addr_t *dnsserver); |
| 36 | + |
| 37 | +static ip4_addr_t knownDns[DNS_V4_SLOTS]; |
| 38 | +static bool ipv6Allowed = false; |
| 39 | +static uint32_t repairs = 0; |
| 40 | + |
| 41 | +static bool isUsableV4(const ip_addr_t* server) { |
| 42 | + return server != NULL && IP_IS_V4(server) && !ip_addr_isany(server); |
| 43 | +} |
| 44 | + |
| 45 | +static bool hasV4Server() { |
| 46 | + for(uint8_t i = 0; i < DNS_MAX_SERVERS; i++) { |
| 47 | + if(isUsableV4(dns_getserver(i))) return true; |
| 48 | + } |
| 49 | + return false; |
| 50 | +} |
| 51 | + |
| 52 | +#if defined(AMS_WRAP_DNS_SETSERVER) |
| 53 | +/** |
| 54 | + * Intercepts every write to the DNS table. Runs on the lwIP thread, so it must |
| 55 | + * not block or log. |
| 56 | + */ |
| 57 | +extern "C" void __wrap_dns_setserver(u8_t numdns, const ip_addr_t *dnsserver) { |
| 58 | + // Only displace an IPv6 server when there is an IPv4 one worth protecting |
| 59 | + if(dnsserver != NULL && IP_IS_V6(dnsserver) && hasV4Server()) { |
| 60 | + if(ipv6Allowed) { |
| 61 | + __real_dns_setserver(DNS_MAX_SERVERS - 1, dnsserver); |
| 62 | + } |
| 63 | + return; |
| 64 | + } |
| 65 | + __real_dns_setserver(numdns, dnsserver); |
| 66 | +} |
| 67 | +#endif |
| 68 | + |
| 69 | +/** |
| 70 | + * Remember the IPv4 servers, but only while the IPv4 slots are untainted, so a |
| 71 | + * table that has already been overwritten can never become the source of truth. |
| 72 | + * The IPv6 slot is deliberately not read here. |
| 73 | + */ |
| 74 | +static void remember() { |
| 75 | + for(uint8_t i = 0; i < DNS_V4_SLOTS; i++) { |
| 76 | + if(IP_IS_V6(dns_getserver(i))) return; |
| 77 | + } |
| 78 | + |
| 79 | + ip4_addr_t found[DNS_V4_SLOTS]; |
| 80 | + uint8_t count = 0; |
| 81 | + for(uint8_t i = 0; i < DNS_V4_SLOTS; i++) { |
| 82 | + const ip_addr_t* server = dns_getserver(i); |
| 83 | + if(isUsableV4(server)) { |
| 84 | + found[count++] = *ip_2_ip4(server); |
| 85 | + } |
| 86 | + } |
| 87 | + if(count == 0) return; |
| 88 | + |
| 89 | + for(uint8_t i = 0; i < DNS_V4_SLOTS; i++) { |
| 90 | + if(i < count) { |
| 91 | + knownDns[i] = found[i]; |
| 92 | + } else { |
| 93 | + ip4_addr_set_zero(&knownDns[i]); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void dnsGuardSetIpv6Allowed(bool allowed) { |
| 99 | + ipv6Allowed = allowed; |
| 100 | +} |
| 101 | + |
| 102 | +void dnsGuardEnforce() { |
| 103 | + remember(); |
| 104 | + |
| 105 | + ip_addr_t desired[DNS_MAX_SERVERS]; |
| 106 | + uint8_t count = 0; |
| 107 | + for(uint8_t i = 0; i < DNS_V4_SLOTS; i++) { |
| 108 | + if(!ip4_addr_isany_val(knownDns[i])) { |
| 109 | + ip_addr_copy_from_ip4(desired[count], knownDns[i]); |
| 110 | + count++; |
| 111 | + } |
| 112 | + } |
| 113 | + if(count == 0) return; // No IPv4 DNS known, leave an IPv6 only or closed network alone |
| 114 | + |
| 115 | + if(ipv6Allowed) { |
| 116 | + for(uint8_t i = 0; i < DNS_MAX_SERVERS; i++) { |
| 117 | + const ip_addr_t* server = dns_getserver(i); |
| 118 | + if(server != NULL && IP_IS_V6(server) && !ip_addr_isany(server)) { |
| 119 | + ip_addr_copy(desired[count], *server); |
| 120 | + count++; |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + while(count < DNS_MAX_SERVERS) { |
| 126 | + ip_addr_set_zero(&desired[count]); |
| 127 | + count++; |
| 128 | + } |
| 129 | + |
| 130 | + bool repaired = false; |
| 131 | + for(uint8_t i = 0; i < DNS_MAX_SERVERS; i++) { |
| 132 | + if(!ip_addr_cmp(dns_getserver(i), &desired[i])) { |
| 133 | + dns_setserver(i, &desired[i]); |
| 134 | + repaired = true; |
| 135 | + } |
| 136 | + } |
| 137 | + if(repaired) repairs++; |
| 138 | +} |
| 139 | + |
| 140 | +uint32_t dnsGuardRepairs() { |
| 141 | + return repairs; |
| 142 | +} |
| 143 | + |
| 144 | +#endif |
0 commit comments