dns.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Minimal DNS resolver: an A-record query over UDP (RFC 1035).
  2. *
  3. * One question, up to four answers, three sends one second apart. Name
  4. * compression is handled when skipping over the answer section; the reply's
  5. * checksum is not validated, matching the rest of the stack (net_udp_send()
  6. * sends with the UDP checksum disabled too).
  7. *
  8. * Polled like every other net command: the caller owns the NIC
  9. * (netdev_acquire) while dns_resolve() runs. */
  10. #include "io.h"
  11. #include "timer.h"
  12. #include "netcore.h"
  13. #include "netdev.h"
  14. #include "net.h"
  15. #include "process.h"
  16. #include "dns.h"
  17. #define DNS_PORT 53
  18. #define DNS_MAX_ANSWERS 4
  19. #define DNS_RETRY_MS 1000
  20. #define DNS_RETRIES 3
  21. #define DNS_WAIT_MS 5000
  22. /* Query buffer: 12-byte header + a 255-byte name + qtype/qclass. */
  23. static unsigned char query_buf[12 + 260 + 4];
  24. /* One query may be pending at a time. Stack-mode resolvers hand received
  25. * frames to dns_input() through net_pump(); a raw owner keeps its direct
  26. * polling path because it consumes every frame itself. */
  27. static int dns_busy;
  28. static unsigned int dns_owner;
  29. static unsigned short dns_pending_id;
  30. static unsigned char dns_pending_server[4];
  31. static unsigned char dns_pending_ip[4];
  32. static unsigned char dns_pending_extra[DNS_MAX_ANSWERS][4];
  33. static unsigned int dns_pending_count;
  34. static int dns_pending_result; /* 0 none, 1 answer, -1 failure */
  35. static void put_dec(unsigned int v)
  36. {
  37. char b[12];
  38. int i = 0;
  39. if (v == 0) {
  40. io_putchar('0');
  41. return;
  42. }
  43. while (v) {
  44. b[i++] = (char)('0' + (v % 10));
  45. v /= 10;
  46. }
  47. while (i)
  48. io_putchar(b[--i]);
  49. }
  50. static void put_ip(const unsigned char *ip)
  51. {
  52. unsigned int i;
  53. for (i = 0; i < 4; i++) {
  54. if (i)
  55. io_putchar('.');
  56. put_dec(ip[i]);
  57. }
  58. }
  59. static int ip_eq(const unsigned char *a, const unsigned char *b)
  60. {
  61. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  62. }
  63. /* Encode a dotted name as DNS labels (no trailing dot needed); 0 on error. */
  64. static int dns_encode_name(const char *name, unsigned char *out,
  65. unsigned int max)
  66. {
  67. const char *p = name;
  68. unsigned int o = 0;
  69. if (!*name)
  70. return -1;
  71. for (;;) {
  72. const char *q = p;
  73. unsigned int n = 0;
  74. while (*q && *q != '.') {
  75. q++;
  76. n++;
  77. }
  78. if (n == 0 || n > 63)
  79. return -1;
  80. /* length byte + label + a possible terminator must fit */
  81. if (o + 1 + n + 1 > max || o + 1 + n > 254)
  82. return -1;
  83. out[o++] = (unsigned char)n;
  84. while (p < q)
  85. out[o++] = (unsigned char)*p++;
  86. if (!*p)
  87. break;
  88. p++; /* skip the '.' */
  89. if (!*p) /* trailing dot */
  90. break;
  91. }
  92. out[o++] = 0;
  93. return (int)o;
  94. }
  95. /* Skip one (possibly compressed) name; 0, or -1 when malformed. */
  96. static int dns_skip_name(const unsigned char *p, unsigned int plen,
  97. unsigned int *pos)
  98. {
  99. while (*pos < plen) {
  100. unsigned char b = p[*pos];
  101. if (b == 0) {
  102. (*pos)++;
  103. return 0;
  104. }
  105. if ((b & 0xC0) == 0xC0) {
  106. if (*pos + 2 > plen)
  107. return -1;
  108. *pos += 2; /* pointer: nothing else to skip */
  109. return 0;
  110. }
  111. if (b & 0xC0)
  112. return -1;
  113. if (*pos + 1 + b > plen)
  114. return -1;
  115. *pos += 1 + b;
  116. }
  117. return -1;
  118. }
  119. /* Check one frame as a reply to our query. Returns:
  120. * 1 = at least one A record (ip and extras filled),
  121. * 0 = not our reply (keep waiting),
  122. * -1 = our reply but a DNS-level failure (already reported). */
  123. static int dns_parse(const unsigned char *frame, unsigned int len,
  124. unsigned short id, const unsigned char *server,
  125. unsigned char ip[4], unsigned char extra[DNS_MAX_ANSWERS][4],
  126. unsigned int *count)
  127. {
  128. unsigned char s_ip[4], d_ip[4];
  129. const unsigned char *p;
  130. unsigned int sport, dport, plen, pos, qd, an, i;
  131. if (!net_udp_parse(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen))
  132. return 0;
  133. if (sport != DNS_PORT || !ip_eq(s_ip, server))
  134. return 0;
  135. if (plen < 12)
  136. return 0;
  137. if ((((unsigned int)p[0] << 8) | p[1]) != id)
  138. return 0;
  139. if (!(p[2] & 0x80)) /* QR: must be a reply */
  140. return 0;
  141. if (p[2] & 0x02) { /* TC: truncated */
  142. io_putstr("dns: reply truncated (TC)\n");
  143. return -1;
  144. }
  145. switch (p[3] & 0x0F) {
  146. case 0:
  147. break;
  148. case 3:
  149. io_putstr("dns: no such name (NXDOMAIN)\n");
  150. return -1;
  151. case 2:
  152. io_putstr("dns: server failure (SERVFAIL)\n");
  153. return -1;
  154. default:
  155. io_putstr("dns: rcode ");
  156. put_dec(p[3] & 0x0F);
  157. io_putchar('\n');
  158. return -1;
  159. }
  160. qd = ((unsigned int)p[4] << 8) | p[5];
  161. an = ((unsigned int)p[6] << 8) | p[7];
  162. pos = 12;
  163. for (i = 0; i < qd; i++) {
  164. if (dns_skip_name(p, plen, &pos) != 0 || pos + 4 > plen)
  165. return 0;
  166. pos += 4; /* qtype + qclass */
  167. }
  168. *count = 0;
  169. for (i = 0; i < an; i++) {
  170. unsigned int type, class_, rdlen;
  171. if (dns_skip_name(p, plen, &pos) != 0 || pos + 10 > plen)
  172. return 0;
  173. type = ((unsigned int)p[pos] << 8) | p[pos + 1];
  174. class_ = ((unsigned int)p[pos + 2] << 8) | p[pos + 3];
  175. rdlen = ((unsigned int)p[pos + 8] << 8) | p[pos + 9];
  176. pos += 10;
  177. if (pos + rdlen > plen)
  178. return 0;
  179. if (type == 1 && class_ == 1 && rdlen == 4) {
  180. unsigned int k;
  181. if (*count == 0) {
  182. for (k = 0; k < 4; k++)
  183. ip[k] = p[pos + k];
  184. } else if (*count < DNS_MAX_ANSWERS) {
  185. for (k = 0; k < 4; k++)
  186. extra[*count - 1][k] = p[pos + k];
  187. }
  188. (*count)++;
  189. }
  190. pos += rdlen;
  191. }
  192. return *count > 0 ? 1 : 0;
  193. }
  194. /* Feed one frame to a pending stack-mode query. Returns 1 when it was a
  195. * reply to the pending query (consumed), 0 otherwise. */
  196. int dns_input(const unsigned char *frame, unsigned int len)
  197. {
  198. int r;
  199. if (!dns_busy || dns_pending_result)
  200. return 0;
  201. r = dns_parse(frame, len, dns_pending_id, dns_pending_server,
  202. dns_pending_ip, dns_pending_extra,
  203. &dns_pending_count);
  204. if (r == 0)
  205. return 0;
  206. dns_pending_result = r > 0 ? 1 : -1;
  207. return 1;
  208. }
  209. void dns_reap(unsigned int pid)
  210. {
  211. if (dns_busy && dns_owner == pid) {
  212. dns_busy = 0;
  213. dns_owner = 0;
  214. dns_pending_result = 0;
  215. }
  216. }
  217. static void dns_print_answer(const char *name, const unsigned char *ip,
  218. const unsigned char extra[DNS_MAX_ANSWERS][4],
  219. unsigned int count)
  220. {
  221. unsigned int i;
  222. io_putstr("dns: ");
  223. io_putstr(name);
  224. io_putstr(" -> ");
  225. put_ip(ip);
  226. for (i = 1; i < count && i < DNS_MAX_ANSWERS; i++) {
  227. io_putstr(", ");
  228. put_ip(extra[i - 1]);
  229. }
  230. io_putchar('\n');
  231. }
  232. int dns_resolve(const char *name, unsigned char ip[4])
  233. {
  234. unsigned char server[4], hop[4], server_mac[6];
  235. unsigned char extra[DNS_MAX_ANSWERS][4];
  236. const struct netdev *nd = netdev_active();
  237. unsigned short id;
  238. unsigned int sport, qlen, tries, count = 0, i;
  239. unsigned int pid = current_proc() ? current_proc()->pid : 0;
  240. unsigned int wait_start;
  241. int raw, r = -1;
  242. if (!nd) {
  243. io_putstr("dns: no NIC found\n");
  244. return -1;
  245. }
  246. raw = pid && netdev_owner() == pid;
  247. if (!raw)
  248. net_stack_enter();
  249. /* One query in flight. A second stack-mode resolver waits (and keeps
  250. * pumping TCP frames) until the first one finishes. */
  251. wait_start = get_ticks_ms();
  252. while (dns_busy && dns_owner != pid) {
  253. if (raw ||
  254. (int)(get_ticks_ms() - (wait_start + DNS_WAIT_MS)) >= 0) {
  255. if (!raw)
  256. net_stack_leave();
  257. return -1;
  258. }
  259. net_pump(8);
  260. process_sleep_ms(1);
  261. }
  262. dns_busy = 1;
  263. dns_owner = pid;
  264. dns_pending_result = 0;
  265. dns_pending_count = 0;
  266. net_get_dns(server);
  267. if (!(server[0] || server[1] || server[2] || server[3])) {
  268. /* Home routers usually proxy DNS for their clients. */
  269. net_get_gateway(server);
  270. if (!(server[0] || server[1] || server[2] || server[3])) {
  271. io_putstr("dns: no resolver (run netdns <a.b.c.d>)\n");
  272. goto out;
  273. }
  274. }
  275. for (i = 0; i < 12; i++)
  276. query_buf[i] = 0;
  277. id = (unsigned short)(get_ticks_ms() ^
  278. ((unsigned int)(unsigned char)name[0] << 8) ^
  279. 0x4E42);
  280. query_buf[0] = (unsigned char)(id >> 8);
  281. query_buf[1] = (unsigned char)id;
  282. query_buf[2] = 0x01; /* RD=1: recursion desired */
  283. query_buf[5] = 1; /* QDCOUNT=1 */
  284. qlen = 12;
  285. {
  286. int n = dns_encode_name(name, query_buf + 12,
  287. sizeof(query_buf) - 12 - 4);
  288. if (n <= 0) {
  289. io_putstr("dns: bad name\n");
  290. goto out;
  291. }
  292. qlen += (unsigned int)n;
  293. }
  294. query_buf[qlen++] = 0; /* QTYPE A */
  295. query_buf[qlen++] = 1;
  296. query_buf[qlen++] = 0; /* QCLASS IN */
  297. query_buf[qlen++] = 1;
  298. sport = 45000 + (id & 0x0FFF);
  299. for (i = 0; i < 4; i++)
  300. dns_pending_server[i] = server[i];
  301. dns_pending_id = id;
  302. net_next_hop(server, hop);
  303. if (net_arp_resolve(hop, server_mac) != 0) {
  304. io_putstr("dns: no ARP reply from ");
  305. put_ip(hop);
  306. io_putchar('\n');
  307. goto out;
  308. }
  309. for (tries = 0; tries < DNS_RETRIES; tries++) {
  310. unsigned int deadline = get_ticks_ms() + DNS_RETRY_MS;
  311. unsigned int flags;
  312. int tx;
  313. flags = net_irq_enter();
  314. tx = net_udp_send(server_mac, server, sport, DNS_PORT,
  315. query_buf, qlen);
  316. net_irq_exit(flags);
  317. if (tx != 0) {
  318. io_putstr("dns: send failed\n");
  319. goto out;
  320. }
  321. while ((int)(get_ticks_ms() - deadline) < 0) {
  322. if (raw) {
  323. int len = nd->rx(netdev_rx_scratch(),
  324. NETDEV_FRAME_MAX);
  325. if (len < 0)
  326. continue;
  327. r = dns_parse(netdev_rx_scratch(),
  328. (unsigned int)len, id, server,
  329. ip, extra, &count);
  330. if (r < 0)
  331. goto out;
  332. if (r == 0)
  333. continue;
  334. dns_print_answer(name, ip, extra, count);
  335. r = 0;
  336. goto out;
  337. }
  338. net_pump(8);
  339. if (dns_pending_result > 0) {
  340. for (i = 0; i < 4; i++)
  341. ip[i] = dns_pending_ip[i];
  342. for (i = 0; i < DNS_MAX_ANSWERS; i++) {
  343. unsigned int j;
  344. for (j = 0; j < 4; j++)
  345. extra[i][j] =
  346. dns_pending_extra[i][j];
  347. }
  348. count = dns_pending_count;
  349. dns_print_answer(name, ip, extra, count);
  350. r = 0;
  351. goto out;
  352. }
  353. if (dns_pending_result < 0)
  354. goto out;
  355. process_sleep_ms(1);
  356. }
  357. }
  358. io_putstr("dns: no reply from ");
  359. put_ip(server);
  360. io_putchar('\n');
  361. out:
  362. dns_busy = 0;
  363. dns_owner = 0;
  364. dns_pending_result = 0;
  365. if (!raw)
  366. net_stack_leave();
  367. return r;
  368. }