dns.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * DNS support driver
  3. *
  4. * Copyright (c) 2008 Pieter Voorthuijsen <pieter.voorthuijsen@prodrive.nl>
  5. * Copyright (c) 2009 Robin Getz <rgetz@blackfin.uclinux.org>
  6. *
  7. * This is a simple DNS implementation for U-Boot. It will use the first IP
  8. * in the DNS response as net_server_ip. This can then be used for any other
  9. * network related activities.
  10. *
  11. * The packet handling is partly based on TADNS, original copyrights
  12. * follow below.
  13. *
  14. */
  15. /*
  16. * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
  17. *
  18. * "THE BEER-WARE LICENSE" (Revision 42):
  19. * Sergey Lyubka wrote this file. As long as you retain this notice you
  20. * can do whatever you want with this stuff. If we meet some day, and you think
  21. * this stuff is worth it, you can buy me a beer in return.
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <env.h>
  26. #include <log.h>
  27. #include <net.h>
  28. #include <asm/unaligned.h>
  29. #include "dns.h"
  30. char *net_dns_resolve; /* The host to resolve */
  31. char *net_dns_env_var; /* The envvar to store the answer in */
  32. static int dns_our_port;
  33. /*
  34. * make port a little random (1024-17407)
  35. * This keeps the math somewhat trivial to compute, and seems to work with
  36. * all supported protocols/clients/servers
  37. */
  38. static unsigned int random_port(void)
  39. {
  40. return 1024 + (get_timer(0) % 0x4000);
  41. }
  42. static void dns_send(void)
  43. {
  44. struct header *header;
  45. int n, name_len;
  46. uchar *p, *pkt;
  47. const char *s;
  48. const char *name;
  49. enum dns_query_type qtype = DNS_A_RECORD;
  50. name = net_dns_resolve;
  51. pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
  52. p = pkt;
  53. /* Prepare DNS packet header */
  54. header = (struct header *)pkt;
  55. header->tid = 1;
  56. header->flags = htons(0x100); /* standard query */
  57. header->nqueries = htons(1); /* Just one query */
  58. header->nanswers = 0;
  59. header->nauth = 0;
  60. header->nother = 0;
  61. /* Encode DNS name */
  62. name_len = strlen(name);
  63. p = (uchar *)&header->data; /* For encoding host name into packet */
  64. do {
  65. s = strchr(name, '.');
  66. if (!s)
  67. s = name + name_len;
  68. n = s - name; /* Chunk length */
  69. *p++ = n; /* Copy length */
  70. memcpy(p, name, n); /* Copy chunk */
  71. p += n;
  72. if (*s == '.')
  73. n++;
  74. name += n;
  75. name_len -= n;
  76. } while (*s != '\0');
  77. *p++ = 0; /* Mark end of host name */
  78. *p++ = 0; /* Some servers require double null */
  79. *p++ = (unsigned char) qtype; /* Query Type */
  80. *p++ = 0;
  81. *p++ = 1; /* Class: inet, 0x0001 */
  82. n = p - pkt; /* Total packet length */
  83. debug("Packet size %d\n", n);
  84. dns_our_port = random_port();
  85. net_send_udp_packet(net_server_ethaddr, net_dns_server,
  86. DNS_SERVICE_PORT, dns_our_port, n);
  87. debug("DNS packet sent\n");
  88. }
  89. static void dns_timeout_handler(void)
  90. {
  91. puts("Timeout\n");
  92. net_set_state(NETLOOP_FAIL);
  93. }
  94. static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  95. unsigned src, unsigned len)
  96. {
  97. struct header *header;
  98. const unsigned char *p, *e, *s;
  99. u16 type, i;
  100. int found, stop, dlen;
  101. char ip_str[22];
  102. struct in_addr ip_addr;
  103. debug("%s\n", __func__);
  104. if (dest != dns_our_port)
  105. return;
  106. for (i = 0; i < len; i += 4)
  107. debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
  108. pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
  109. /* We sent one query. We want to have a single answer: */
  110. header = (struct header *)pkt;
  111. if (ntohs(header->nqueries) != 1)
  112. return;
  113. /* Received 0 answers */
  114. if (header->nanswers == 0) {
  115. puts("DNS: host not found\n");
  116. net_set_state(NETLOOP_SUCCESS);
  117. return;
  118. }
  119. /* Skip host name */
  120. s = &header->data[0];
  121. e = pkt + len;
  122. for (p = s; p < e && *p != '\0'; p++)
  123. continue;
  124. /* We sent query class 1, query type 1 */
  125. if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
  126. puts("DNS: response was not an A record\n");
  127. net_set_state(NETLOOP_SUCCESS);
  128. return;
  129. }
  130. /* Go to the first answer section */
  131. p += 5;
  132. /* Loop through the answers, we want A type answer */
  133. for (found = stop = 0; !stop && &p[12] < e; ) {
  134. /* Skip possible name in CNAME answer */
  135. if (*p != 0xc0) {
  136. while (*p && &p[12] < e)
  137. p++;
  138. p--;
  139. }
  140. debug("Name (Offset in header): %d\n", p[1]);
  141. type = get_unaligned_be16(p+2);
  142. debug("type = %d\n", type);
  143. if (type == DNS_CNAME_RECORD) {
  144. /* CNAME answer. shift to the next section */
  145. debug("Found canonical name\n");
  146. dlen = get_unaligned_be16(p+10);
  147. debug("dlen = %d\n", dlen);
  148. p += 12 + dlen;
  149. } else if (type == DNS_A_RECORD) {
  150. debug("Found A-record\n");
  151. found = 1;
  152. stop = 1;
  153. } else {
  154. debug("Unknown type\n");
  155. stop = 1;
  156. }
  157. }
  158. if (found && &p[12] < e) {
  159. dlen = get_unaligned_be16(p+10);
  160. p += 12;
  161. memcpy(&ip_addr, p, 4);
  162. if (p + dlen <= e) {
  163. ip_to_string(ip_addr, ip_str);
  164. printf("%s\n", ip_str);
  165. if (net_dns_env_var)
  166. env_set(net_dns_env_var, ip_str);
  167. } else {
  168. puts("server responded with invalid IP number\n");
  169. }
  170. }
  171. net_set_state(NETLOOP_SUCCESS);
  172. }
  173. void dns_start(void)
  174. {
  175. debug("%s\n", __func__);
  176. net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler);
  177. net_set_udp_handler(dns_handler);
  178. /* Clear a previous MAC address, the server IP might have changed. */
  179. memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
  180. dns_send();
  181. }