dns.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "netdev.h"
  13. #include "net.h"
  14. #include "dns.h"
  15. #define DNS_PORT 53
  16. #define DNS_MAX_ANSWERS 4
  17. #define DNS_RETRY_MS 1000
  18. #define DNS_RETRIES 3
  19. /* Query buffer: 12-byte header + a 255-byte name + qtype/qclass. */
  20. static unsigned char query_buf[12 + 260 + 4];
  21. static void put_dec(unsigned int v)
  22. {
  23. char b[12];
  24. int i = 0;
  25. if (v == 0) {
  26. io_putchar('0');
  27. return;
  28. }
  29. while (v) {
  30. b[i++] = (char)('0' + (v % 10));
  31. v /= 10;
  32. }
  33. while (i)
  34. io_putchar(b[--i]);
  35. }
  36. static void put_ip(const unsigned char *ip)
  37. {
  38. unsigned int i;
  39. for (i = 0; i < 4; i++) {
  40. if (i)
  41. io_putchar('.');
  42. put_dec(ip[i]);
  43. }
  44. }
  45. static int ip_eq(const unsigned char *a, const unsigned char *b)
  46. {
  47. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  48. }
  49. /* Encode a dotted name as DNS labels (no trailing dot needed); 0 on error. */
  50. static int dns_encode_name(const char *name, unsigned char *out,
  51. unsigned int max)
  52. {
  53. const char *p = name;
  54. unsigned int o = 0;
  55. if (!*name)
  56. return -1;
  57. for (;;) {
  58. const char *q = p;
  59. unsigned int n = 0;
  60. while (*q && *q != '.') {
  61. q++;
  62. n++;
  63. }
  64. if (n == 0 || n > 63)
  65. return -1;
  66. /* length byte + label + a possible terminator must fit */
  67. if (o + 1 + n + 1 > max || o + 1 + n > 254)
  68. return -1;
  69. out[o++] = (unsigned char)n;
  70. while (p < q)
  71. out[o++] = (unsigned char)*p++;
  72. if (!*p)
  73. break;
  74. p++; /* skip the '.' */
  75. if (!*p) /* trailing dot */
  76. break;
  77. }
  78. out[o++] = 0;
  79. return (int)o;
  80. }
  81. /* Skip one (possibly compressed) name; 0, or -1 when malformed. */
  82. static int dns_skip_name(const unsigned char *p, unsigned int plen,
  83. unsigned int *pos)
  84. {
  85. while (*pos < plen) {
  86. unsigned char b = p[*pos];
  87. if (b == 0) {
  88. (*pos)++;
  89. return 0;
  90. }
  91. if ((b & 0xC0) == 0xC0) {
  92. if (*pos + 2 > plen)
  93. return -1;
  94. *pos += 2; /* pointer: nothing else to skip */
  95. return 0;
  96. }
  97. if (b & 0xC0)
  98. return -1;
  99. if (*pos + 1 + b > plen)
  100. return -1;
  101. *pos += 1 + b;
  102. }
  103. return -1;
  104. }
  105. /* Check one frame as a reply to our query. Returns:
  106. * 1 = at least one A record (ip and extras filled),
  107. * 0 = not our reply (keep waiting),
  108. * -1 = our reply but a DNS-level failure (already reported). */
  109. static int dns_parse(const unsigned char *frame, unsigned int len,
  110. unsigned short id, const unsigned char *server,
  111. unsigned char ip[4], unsigned char extra[DNS_MAX_ANSWERS][4],
  112. unsigned int *count)
  113. {
  114. unsigned char s_ip[4], d_ip[4];
  115. const unsigned char *p;
  116. unsigned int sport, dport, plen, pos, qd, an, i;
  117. if (!net_udp_parse(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen))
  118. return 0;
  119. if (sport != DNS_PORT || !ip_eq(s_ip, server))
  120. return 0;
  121. if (plen < 12)
  122. return 0;
  123. if ((((unsigned int)p[0] << 8) | p[1]) != id)
  124. return 0;
  125. if (!(p[2] & 0x80)) /* QR: must be a reply */
  126. return 0;
  127. if (p[2] & 0x02) { /* TC: truncated */
  128. io_putstr("dns: reply truncated (TC)\n");
  129. return -1;
  130. }
  131. switch (p[3] & 0x0F) {
  132. case 0:
  133. break;
  134. case 3:
  135. io_putstr("dns: no such name (NXDOMAIN)\n");
  136. return -1;
  137. case 2:
  138. io_putstr("dns: server failure (SERVFAIL)\n");
  139. return -1;
  140. default:
  141. io_putstr("dns: rcode ");
  142. put_dec(p[3] & 0x0F);
  143. io_putchar('\n');
  144. return -1;
  145. }
  146. qd = ((unsigned int)p[4] << 8) | p[5];
  147. an = ((unsigned int)p[6] << 8) | p[7];
  148. pos = 12;
  149. for (i = 0; i < qd; i++) {
  150. if (dns_skip_name(p, plen, &pos) != 0 || pos + 4 > plen)
  151. return 0;
  152. pos += 4; /* qtype + qclass */
  153. }
  154. *count = 0;
  155. for (i = 0; i < an; i++) {
  156. unsigned int type, class_, rdlen;
  157. if (dns_skip_name(p, plen, &pos) != 0 || pos + 10 > plen)
  158. return 0;
  159. type = ((unsigned int)p[pos] << 8) | p[pos + 1];
  160. class_ = ((unsigned int)p[pos + 2] << 8) | p[pos + 3];
  161. rdlen = ((unsigned int)p[pos + 8] << 8) | p[pos + 9];
  162. pos += 10;
  163. if (pos + rdlen > plen)
  164. return 0;
  165. if (type == 1 && class_ == 1 && rdlen == 4) {
  166. unsigned int k;
  167. if (*count == 0) {
  168. for (k = 0; k < 4; k++)
  169. ip[k] = p[pos + k];
  170. } else if (*count < DNS_MAX_ANSWERS) {
  171. for (k = 0; k < 4; k++)
  172. extra[*count - 1][k] = p[pos + k];
  173. }
  174. (*count)++;
  175. }
  176. pos += rdlen;
  177. }
  178. return *count > 0 ? 1 : 0;
  179. }
  180. int dns_resolve(const char *name, unsigned char ip[4])
  181. {
  182. unsigned char server[4], hop[4], server_mac[6];
  183. unsigned char extra[DNS_MAX_ANSWERS][4];
  184. unsigned char *rx_buf = netdev_rx_scratch();
  185. const struct netdev *nd = netdev_active();
  186. unsigned short id;
  187. unsigned int sport, qlen, tries, count, i;
  188. int r;
  189. if (!nd) {
  190. io_putstr("dns: no NIC found\n");
  191. return -1;
  192. }
  193. net_get_dns(server);
  194. if (!(server[0] || server[1] || server[2] || server[3])) {
  195. /* Home routers usually proxy DNS for their clients. */
  196. net_get_gateway(server);
  197. if (!(server[0] || server[1] || server[2] || server[3])) {
  198. io_putstr("dns: no resolver (run netdns <a.b.c.d>)\n");
  199. return -1;
  200. }
  201. }
  202. for (i = 0; i < 12; i++)
  203. query_buf[i] = 0;
  204. id = (unsigned short)(get_ticks_ms() ^
  205. ((unsigned int)(unsigned char)name[0] << 8) ^
  206. 0x4E42);
  207. query_buf[0] = (unsigned char)(id >> 8);
  208. query_buf[1] = (unsigned char)id;
  209. query_buf[2] = 0x01; /* RD=1: recursion desired */
  210. query_buf[5] = 1; /* QDCOUNT=1 */
  211. qlen = 12;
  212. {
  213. int n = dns_encode_name(name, query_buf + 12,
  214. sizeof(query_buf) - 12 - 4);
  215. if (n <= 0) {
  216. io_putstr("dns: bad name\n");
  217. return -1;
  218. }
  219. qlen += (unsigned int)n;
  220. }
  221. query_buf[qlen++] = 0; /* QTYPE A */
  222. query_buf[qlen++] = 1;
  223. query_buf[qlen++] = 0; /* QCLASS IN */
  224. query_buf[qlen++] = 1;
  225. sport = 45000 + (id & 0x0FFF);
  226. net_next_hop(server, hop);
  227. if (net_arp_resolve(hop, server_mac) != 0) {
  228. io_putstr("dns: no ARP reply from ");
  229. put_ip(hop);
  230. io_putchar('\n');
  231. return -1;
  232. }
  233. for (tries = 0; tries < DNS_RETRIES; tries++) {
  234. unsigned int deadline = get_ticks_ms() + DNS_RETRY_MS;
  235. if (net_udp_send(server_mac, server, sport, DNS_PORT,
  236. query_buf, qlen) != 0) {
  237. io_putstr("dns: send failed\n");
  238. return -1;
  239. }
  240. while ((int)(get_ticks_ms() - deadline) < 0) {
  241. int len = nd->rx(rx_buf, NETDEV_FRAME_MAX);
  242. if (len < 0)
  243. continue;
  244. r = dns_parse(rx_buf, (unsigned int)len, id, server,
  245. ip, extra, &count);
  246. if (r < 0)
  247. return -1;
  248. if (r == 0)
  249. continue;
  250. io_putstr("dns: ");
  251. io_putstr(name);
  252. io_putstr(" -> ");
  253. put_ip(ip);
  254. for (i = 1; i < count && i < DNS_MAX_ANSWERS; i++) {
  255. io_putstr(", ");
  256. put_ip(extra[i - 1]);
  257. }
  258. io_putchar('\n');
  259. return 0;
  260. }
  261. }
  262. io_putstr("dns: no reply from ");
  263. put_ip(server);
  264. io_putchar('\n');
  265. return -1;
  266. }
  267. /* ---- commands ---- */
  268. void program_netdns_main(const char *arg)
  269. {
  270. unsigned char dns[4], gw[4];
  271. if (arg[0] == '\0') {
  272. net_get_dns(dns);
  273. if (dns[0] || dns[1] || dns[2] || dns[3]) {
  274. io_putstr("dns=");
  275. put_ip(dns);
  276. io_putchar('\n');
  277. return;
  278. }
  279. net_get_gateway(gw);
  280. io_putstr("dns: unset");
  281. if (gw[0] || gw[1] || gw[2] || gw[3]) {
  282. io_putstr(" (fallback: gateway ");
  283. put_ip(gw);
  284. io_putchar(')');
  285. }
  286. io_putchar('\n');
  287. return;
  288. }
  289. if (net_parse_ip(arg, dns) != 0) {
  290. io_putstr("netdns: usage netdns [a.b.c.d]\n");
  291. return;
  292. }
  293. net_set_dns(dns);
  294. io_putstr("dns set to ");
  295. put_ip(dns);
  296. io_putchar('\n');
  297. }
  298. void program_lookup_main(const char *arg)
  299. {
  300. unsigned char ip[4];
  301. if (arg[0] == '\0') {
  302. io_putstr("lookup: usage lookup <name>\n");
  303. return;
  304. }
  305. netdev_acquire();
  306. dns_resolve(arg, ip);
  307. netdev_release();
  308. }