/* Minimal DNS resolver: an A-record query over UDP (RFC 1035). * * One question, up to four answers, three sends one second apart. Name * compression is handled when skipping over the answer section; the reply's * checksum is not validated, matching the rest of the stack (net_udp_send() * sends with the UDP checksum disabled too). * * Polled like every other net command: the caller owns the NIC * (netdev_acquire) while dns_resolve() runs. */ #include "io.h" #include "timer.h" #include "netcore.h" #include "netdev.h" #include "net.h" #include "process.h" #include "dns.h" #define DNS_PORT 53 #define DNS_MAX_ANSWERS 4 #define DNS_RETRY_MS 1000 #define DNS_RETRIES 3 #define DNS_WAIT_MS 15000 #define DNS_CACHE_MAX 8 #define DNS_CACHE_TTL_MS 300000 #define DNS_CACHE_NAME 96 /* Query buffer: 12-byte header + a 255-byte name + qtype/qclass. */ static unsigned char query_buf[12 + 260 + 4]; /* One query may be pending at a time. Stack-mode resolvers hand received * frames to dns_input() through net_pump(); a raw owner keeps its direct * polling path because it consumes every frame itself. */ static int dns_busy; static unsigned int dns_owner; static unsigned short dns_pending_id; static unsigned char dns_pending_server[4]; static unsigned char dns_pending_ip[4]; static unsigned char dns_pending_extra[DNS_MAX_ANSWERS][4]; static unsigned int dns_pending_count; static int dns_pending_result; /* 0 none, 1 answer, -1 failure */ /* Small A-record cache so parallel browser workers that ask for the same * host do not queue a separate query each. */ struct dns_cache_entry { char name[DNS_CACHE_NAME]; unsigned char ip[4]; unsigned int age_ms; int used; }; static struct dns_cache_entry dns_cache[DNS_CACHE_MAX]; static void put_dec(unsigned int v) { char b[12]; int i = 0; if (v == 0) { io_putchar('0'); return; } while (v) { b[i++] = (char)('0' + (v % 10)); v /= 10; } while (i) io_putchar(b[--i]); } static void put_ip(const unsigned char *ip) { unsigned int i; for (i = 0; i < 4; i++) { if (i) io_putchar('.'); put_dec(ip[i]); } } static int ip_eq(const unsigned char *a, const unsigned char *b) { return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]; } /* Encode a dotted name as DNS labels (no trailing dot needed); 0 on error. */ static int dns_encode_name(const char *name, unsigned char *out, unsigned int max) { const char *p = name; unsigned int o = 0; if (!*name) return -1; for (;;) { const char *q = p; unsigned int n = 0; while (*q && *q != '.') { q++; n++; } if (n == 0 || n > 63) return -1; /* length byte + label + a possible terminator must fit */ if (o + 1 + n + 1 > max || o + 1 + n > 254) return -1; out[o++] = (unsigned char)n; while (p < q) out[o++] = (unsigned char)*p++; if (!*p) break; p++; /* skip the '.' */ if (!*p) /* trailing dot */ break; } out[o++] = 0; return (int)o; } /* Skip one (possibly compressed) name; 0, or -1 when malformed. */ static int dns_skip_name(const unsigned char *p, unsigned int plen, unsigned int *pos) { while (*pos < plen) { unsigned char b = p[*pos]; if (b == 0) { (*pos)++; return 0; } if ((b & 0xC0) == 0xC0) { if (*pos + 2 > plen) return -1; *pos += 2; /* pointer: nothing else to skip */ return 0; } if (b & 0xC0) return -1; if (*pos + 1 + b > plen) return -1; *pos += 1 + b; } return -1; } /* Check one frame as a reply to our query. Returns: * 1 = at least one A record (ip and extras filled), * 0 = not our reply (keep waiting), * -1 = our reply but a DNS-level failure (already reported). */ static int dns_parse(const unsigned char *frame, unsigned int len, unsigned short id, const unsigned char *server, unsigned char ip[4], unsigned char extra[DNS_MAX_ANSWERS][4], unsigned int *count) { unsigned char s_ip[4], d_ip[4]; const unsigned char *p; unsigned int sport, dport, plen, pos, qd, an, i; if (!net_udp_parse(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen)) return 0; if (sport != DNS_PORT || !ip_eq(s_ip, server)) return 0; if (plen < 12) return 0; if ((((unsigned int)p[0] << 8) | p[1]) != id) return 0; if (!(p[2] & 0x80)) /* QR: must be a reply */ return 0; if (p[2] & 0x02) { /* TC: truncated */ io_putstr("dns: reply truncated (TC)\n"); return -1; } switch (p[3] & 0x0F) { case 0: break; case 3: io_putstr("dns: no such name (NXDOMAIN)\n"); return -1; case 2: io_putstr("dns: server failure (SERVFAIL)\n"); return -1; default: io_putstr("dns: rcode "); put_dec(p[3] & 0x0F); io_putchar('\n'); return -1; } qd = ((unsigned int)p[4] << 8) | p[5]; an = ((unsigned int)p[6] << 8) | p[7]; pos = 12; for (i = 0; i < qd; i++) { if (dns_skip_name(p, plen, &pos) != 0 || pos + 4 > plen) return 0; pos += 4; /* qtype + qclass */ } *count = 0; for (i = 0; i < an; i++) { unsigned int type, class_, rdlen; if (dns_skip_name(p, plen, &pos) != 0 || pos + 10 > plen) return 0; type = ((unsigned int)p[pos] << 8) | p[pos + 1]; class_ = ((unsigned int)p[pos + 2] << 8) | p[pos + 3]; rdlen = ((unsigned int)p[pos + 8] << 8) | p[pos + 9]; pos += 10; if (pos + rdlen > plen) return 0; if (type == 1 && class_ == 1 && rdlen == 4) { unsigned int k; if (*count == 0) { for (k = 0; k < 4; k++) ip[k] = p[pos + k]; } else if (*count < DNS_MAX_ANSWERS) { for (k = 0; k < 4; k++) extra[*count - 1][k] = p[pos + k]; } (*count)++; } pos += rdlen; } return *count > 0 ? 1 : 0; } /* Feed one frame to a pending stack-mode query. Returns 1 when it was a * reply to the pending query (consumed), 0 otherwise. */ int dns_input(const unsigned char *frame, unsigned int len) { int r; if (!dns_busy || dns_pending_result) return 0; r = dns_parse(frame, len, dns_pending_id, dns_pending_server, dns_pending_ip, dns_pending_extra, &dns_pending_count); if (r == 0) return 0; dns_pending_result = r > 0 ? 1 : -1; return 1; } void dns_reap(unsigned int pid) { if (dns_busy && dns_owner == pid) { dns_busy = 0; dns_owner = 0; dns_pending_result = 0; } } static void dns_print_answer(const char *name, const unsigned char *ip, const unsigned char extra[DNS_MAX_ANSWERS][4], unsigned int count) { unsigned int i; io_putstr("dns: "); io_putstr(name); io_putstr(" -> "); put_ip(ip); for (i = 1; i < count && i < DNS_MAX_ANSWERS; i++) { io_putstr(", "); put_ip(extra[i - 1]); } io_putchar('\n'); } static void dns_print_cached(const char *name, const unsigned char *ip) { io_putstr("dns: "); io_putstr(name); io_putstr(" -> "); put_ip(ip); io_putchar('\n'); } static int dns_name_eq(const char *a, const char *b) { while (*a && *b && *a == *b) a++, b++; return *a == *b; } static int dns_cache_lookup(const char *name, unsigned char ip[4]) { unsigned int i, now = get_ticks_ms(); for (i = 0; i < DNS_CACHE_MAX; i++) { if (!dns_cache[i].used || !dns_name_eq(dns_cache[i].name, name)) continue; if (now - dns_cache[i].age_ms > DNS_CACHE_TTL_MS) { dns_cache[i].used = 0; return 0; } ip[0] = dns_cache[i].ip[0]; ip[1] = dns_cache[i].ip[1]; ip[2] = dns_cache[i].ip[2]; ip[3] = dns_cache[i].ip[3]; return 1; } return 0; } static void dns_cache_put(const char *name, const unsigned char ip[4]) { unsigned int i, n = 0, slot = 0; while (name[n] && n + 1 < DNS_CACHE_NAME) n++; if (name[n]) /* too long: do not truncate */ return; for (i = 0; i < DNS_CACHE_MAX; i++) { if (dns_cache[i].used && dns_name_eq(dns_cache[i].name, name)) { slot = i; goto store; } } for (i = 0; i < DNS_CACHE_MAX; i++) { if (!dns_cache[i].used) { slot = i; goto store; } if (dns_cache[i].age_ms < dns_cache[slot].age_ms) slot = i; } store: for (i = 0; i < n; i++) dns_cache[slot].name[i] = name[i]; dns_cache[slot].name[n] = '\0'; dns_cache[slot].ip[0] = ip[0]; dns_cache[slot].ip[1] = ip[1]; dns_cache[slot].ip[2] = ip[2]; dns_cache[slot].ip[3] = ip[3]; dns_cache[slot].age_ms = get_ticks_ms(); dns_cache[slot].used = 1; } int dns_resolve(const char *name, unsigned char ip[4]) { unsigned char server[4], hop[4], server_mac[6]; unsigned char extra[DNS_MAX_ANSWERS][4]; const struct netdev *nd = netdev_active(); unsigned short id; unsigned int sport, qlen, tries, count = 0, i; unsigned int pid = current_proc() ? current_proc()->pid : 0; unsigned int wait_start; int raw, r = -1; if (!nd) { io_putstr("dns: no NIC found\n"); return -1; } if (dns_cache_lookup(name, ip)) { dns_print_cached(name, ip); return 0; } raw = pid && netdev_owner() == pid; if (!raw) net_stack_enter(); /* One query in flight. A second stack-mode resolver waits (and keeps * pumping TCP frames) until the first one finishes. */ wait_start = get_ticks_ms(); while (dns_busy && dns_owner != pid) { if (raw || (int)(get_ticks_ms() - (wait_start + DNS_WAIT_MS)) >= 0) { if (!raw) net_stack_leave(); return -1; } net_pump_burst(64); process_sleep_ms(1); } dns_busy = 1; dns_owner = pid; dns_pending_result = 0; dns_pending_count = 0; /* The waiter we just queued behind may have filled the cache. */ if (dns_cache_lookup(name, ip)) { dns_print_cached(name, ip); goto out; } net_get_dns(server); if (!(server[0] || server[1] || server[2] || server[3])) { /* Home routers usually proxy DNS for their clients. */ net_get_gateway(server); if (!(server[0] || server[1] || server[2] || server[3])) { io_putstr("dns: no resolver (run netdns )\n"); goto out; } } for (i = 0; i < 12; i++) query_buf[i] = 0; id = (unsigned short)(get_ticks_ms() ^ ((unsigned int)(unsigned char)name[0] << 8) ^ 0x4E42); query_buf[0] = (unsigned char)(id >> 8); query_buf[1] = (unsigned char)id; query_buf[2] = 0x01; /* RD=1: recursion desired */ query_buf[5] = 1; /* QDCOUNT=1 */ qlen = 12; { int n = dns_encode_name(name, query_buf + 12, sizeof(query_buf) - 12 - 4); if (n <= 0) { io_putstr("dns: bad name\n"); goto out; } qlen += (unsigned int)n; } query_buf[qlen++] = 0; /* QTYPE A */ query_buf[qlen++] = 1; query_buf[qlen++] = 0; /* QCLASS IN */ query_buf[qlen++] = 1; sport = 45000 + (id & 0x0FFF); for (i = 0; i < 4; i++) dns_pending_server[i] = server[i]; dns_pending_id = id; net_next_hop(server, hop); if (net_arp_resolve(hop, server_mac) != 0) { io_putstr("dns: no ARP reply from "); put_ip(hop); io_putchar('\n'); goto out; } for (tries = 0; tries < DNS_RETRIES; tries++) { unsigned int deadline = get_ticks_ms() + DNS_RETRY_MS; unsigned int flags; int tx; flags = net_irq_enter(); tx = net_udp_send(server_mac, server, sport, DNS_PORT, query_buf, qlen); net_irq_exit(flags); if (tx != 0) { io_putstr("dns: send failed\n"); goto out; } while ((int)(get_ticks_ms() - deadline) < 0) { if (raw) { int len = netdev_rx_frame(nd, netdev_rx_scratch(), NETDEV_FRAME_MAX); if (len < 0) continue; r = dns_parse(netdev_rx_scratch(), (unsigned int)len, id, server, ip, extra, &count); if (r < 0) goto out; if (r == 0) continue; dns_cache_put(name, ip); dns_print_answer(name, ip, extra, count); r = 0; goto out; } net_pump_burst(64); if (dns_pending_result > 0) { for (i = 0; i < 4; i++) ip[i] = dns_pending_ip[i]; for (i = 0; i < DNS_MAX_ANSWERS; i++) { unsigned int j; for (j = 0; j < 4; j++) extra[i][j] = dns_pending_extra[i][j]; } count = dns_pending_count; dns_cache_put(name, ip); dns_print_answer(name, ip, extra, count); r = 0; goto out; } if (dns_pending_result < 0) goto out; process_sleep_ms(1); } } io_putstr("dns: no reply from "); put_ip(server); io_putchar('\n'); out: dns_busy = 0; dns_owner = 0; dns_pending_result = 0; if (!raw) net_stack_leave(); return r; }