dns.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <net.h>
  26. #include <asm/unaligned.h>
  27. #include "dns.h"
  28. char *NetDNSResolve; /* The host to resolve */
  29. char *NetDNSenvvar; /* The envvar to store the answer in */
  30. static int DnsOurPort;
  31. static void
  32. DnsSend(void)
  33. {
  34. struct header *header;
  35. int n, name_len;
  36. uchar *p, *pkt;
  37. const char *s;
  38. const char *name;
  39. enum dns_query_type qtype = DNS_A_RECORD;
  40. name = NetDNSResolve;
  41. pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
  42. p = pkt;
  43. /* Prepare DNS packet header */
  44. header = (struct header *) pkt;
  45. header->tid = 1;
  46. header->flags = htons(0x100); /* standard query */
  47. header->nqueries = htons(1); /* Just one query */
  48. header->nanswers = 0;
  49. header->nauth = 0;
  50. header->nother = 0;
  51. /* Encode DNS name */
  52. name_len = strlen(name);
  53. p = (uchar *) &header->data; /* For encoding host name into packet */
  54. do {
  55. s = strchr(name, '.');
  56. if (!s)
  57. s = name + name_len;
  58. n = s - name; /* Chunk length */
  59. *p++ = n; /* Copy length */
  60. memcpy(p, name, n); /* Copy chunk */
  61. p += n;
  62. if (*s == '.')
  63. n++;
  64. name += n;
  65. name_len -= n;
  66. } while (*s != '\0');
  67. *p++ = 0; /* Mark end of host name */
  68. *p++ = 0; /* Some servers require double null */
  69. *p++ = (unsigned char) qtype; /* Query Type */
  70. *p++ = 0;
  71. *p++ = 1; /* Class: inet, 0x0001 */
  72. n = p - pkt; /* Total packet length */
  73. debug("Packet size %d\n", n);
  74. DnsOurPort = random_port();
  75. net_send_udp_packet(net_server_ethaddr, net_dns_server,
  76. DNS_SERVICE_PORT, DnsOurPort, n);
  77. debug("DNS packet sent\n");
  78. }
  79. static void
  80. DnsTimeout(void)
  81. {
  82. puts("Timeout\n");
  83. net_set_state(NETLOOP_FAIL);
  84. }
  85. static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  86. unsigned src, unsigned len)
  87. {
  88. struct header *header;
  89. const unsigned char *p, *e, *s;
  90. u16 type, i;
  91. int found, stop, dlen;
  92. char IPStr[22];
  93. struct in_addr ip_addr;
  94. debug("%s\n", __func__);
  95. if (dest != DnsOurPort)
  96. return;
  97. for (i = 0; i < len; i += 4)
  98. debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
  99. pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
  100. /* We sent one query. We want to have a single answer: */
  101. header = (struct header *) pkt;
  102. if (ntohs(header->nqueries) != 1)
  103. return;
  104. /* Received 0 answers */
  105. if (header->nanswers == 0) {
  106. puts("DNS: host not found\n");
  107. net_set_state(NETLOOP_SUCCESS);
  108. return;
  109. }
  110. /* Skip host name */
  111. s = &header->data[0];
  112. e = pkt + len;
  113. for (p = s; p < e && *p != '\0'; p++)
  114. continue;
  115. /* We sent query class 1, query type 1 */
  116. if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
  117. puts("DNS: response was not an A record\n");
  118. net_set_state(NETLOOP_SUCCESS);
  119. return;
  120. }
  121. /* Go to the first answer section */
  122. p += 5;
  123. /* Loop through the answers, we want A type answer */
  124. for (found = stop = 0; !stop && &p[12] < e; ) {
  125. /* Skip possible name in CNAME answer */
  126. if (*p != 0xc0) {
  127. while (*p && &p[12] < e)
  128. p++;
  129. p--;
  130. }
  131. debug("Name (Offset in header): %d\n", p[1]);
  132. type = get_unaligned_be16(p+2);
  133. debug("type = %d\n", type);
  134. if (type == DNS_CNAME_RECORD) {
  135. /* CNAME answer. shift to the next section */
  136. debug("Found canonical name\n");
  137. dlen = get_unaligned_be16(p+10);
  138. debug("dlen = %d\n", dlen);
  139. p += 12 + dlen;
  140. } else if (type == DNS_A_RECORD) {
  141. debug("Found A-record\n");
  142. found = stop = 1;
  143. } else {
  144. debug("Unknown type\n");
  145. stop = 1;
  146. }
  147. }
  148. if (found && &p[12] < e) {
  149. dlen = get_unaligned_be16(p+10);
  150. p += 12;
  151. memcpy(&ip_addr, p, 4);
  152. if (p + dlen <= e) {
  153. ip_to_string(ip_addr, IPStr);
  154. printf("%s\n", IPStr);
  155. if (NetDNSenvvar)
  156. setenv(NetDNSenvvar, IPStr);
  157. } else
  158. puts("server responded with invalid IP number\n");
  159. }
  160. net_set_state(NETLOOP_SUCCESS);
  161. }
  162. void
  163. DnsStart(void)
  164. {
  165. debug("%s\n", __func__);
  166. NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
  167. net_set_udp_handler(dns_handler);
  168. /* Clear a previous MAC address, the server IP might have changed. */
  169. memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
  170. DnsSend();
  171. }