blob: 5b1fe5b0103747a0fe91c5c17bc69a8f2801dfb9 [file] [log] [blame]
Robin Getz1a32bf42009-07-20 14:53:54 -04001/*
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
Joe Hershberger049a95a2015-04-08 01:41:01 -05008 * in the DNS response as net_server_ip. This can then be used for any other
Robin Getz1a32bf42009-07-20 14:53:54 -04009 * network related activities.
10 *
11 * The packet handling is partly based on TADNS, original copyrights
12 * follow below.
13 *
14 */
15
16/*
17 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
18 *
19 * "THE BEER-WARE LICENSE" (Revision 42):
20 * Sergey Lyubka wrote this file. As long as you retain this notice you
21 * can do whatever you want with this stuff. If we meet some day, and you think
22 * this stuff is worth it, you can buy me a beer in return.
23 */
24
25#include <common.h>
26#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060027#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060028#include <log.h>
Robin Getz1a32bf42009-07-20 14:53:54 -040029#include <net.h>
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +000030#include <asm/unaligned.h>
Robin Getz1a32bf42009-07-20 14:53:54 -040031
32#include "dns.h"
33
Joe Hershberger786eac52015-04-08 01:41:15 -050034char *net_dns_resolve; /* The host to resolve */
35char *net_dns_env_var; /* The envvar to store the answer in */
Robin Getz1a32bf42009-07-20 14:53:54 -040036
Joe Hershberger786eac52015-04-08 01:41:15 -050037static int dns_our_port;
Robin Getz1a32bf42009-07-20 14:53:54 -040038
Baruch Siachf1d925d2020-05-20 13:31:41 +030039/*
40 * make port a little random (1024-17407)
41 * This keeps the math somewhat trivial to compute, and seems to work with
42 * all supported protocols/clients/servers
43 */
44static unsigned int random_port(void)
45{
46 return 1024 + (get_timer(0) % 0x4000);
47}
48
Joe Hershberger786eac52015-04-08 01:41:15 -050049static void dns_send(void)
Robin Getz1a32bf42009-07-20 14:53:54 -040050{
51 struct header *header;
52 int n, name_len;
53 uchar *p, *pkt;
54 const char *s;
55 const char *name;
56 enum dns_query_type qtype = DNS_A_RECORD;
57
Joe Hershberger786eac52015-04-08 01:41:15 -050058 name = net_dns_resolve;
Joe Hershberger1203fcc2015-04-08 01:41:05 -050059 pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
60 p = pkt;
Robin Getz1a32bf42009-07-20 14:53:54 -040061
62 /* Prepare DNS packet header */
Joe Hershberger786eac52015-04-08 01:41:15 -050063 header = (struct header *)pkt;
Robin Getz1a32bf42009-07-20 14:53:54 -040064 header->tid = 1;
65 header->flags = htons(0x100); /* standard query */
66 header->nqueries = htons(1); /* Just one query */
67 header->nanswers = 0;
68 header->nauth = 0;
69 header->nother = 0;
70
71 /* Encode DNS name */
72 name_len = strlen(name);
Joe Hershberger786eac52015-04-08 01:41:15 -050073 p = (uchar *)&header->data; /* For encoding host name into packet */
Robin Getz1a32bf42009-07-20 14:53:54 -040074
75 do {
76 s = strchr(name, '.');
77 if (!s)
78 s = name + name_len;
79
80 n = s - name; /* Chunk length */
81 *p++ = n; /* Copy length */
82 memcpy(p, name, n); /* Copy chunk */
83 p += n;
84
85 if (*s == '.')
86 n++;
87
88 name += n;
89 name_len -= n;
90 } while (*s != '\0');
91
92 *p++ = 0; /* Mark end of host name */
93 *p++ = 0; /* Some servers require double null */
94 *p++ = (unsigned char) qtype; /* Query Type */
95
96 *p++ = 0;
97 *p++ = 1; /* Class: inet, 0x0001 */
98
99 n = p - pkt; /* Total packet length */
100 debug("Packet size %d\n", n);
101
Joe Hershberger786eac52015-04-08 01:41:15 -0500102 dns_our_port = random_port();
Robin Getz1a32bf42009-07-20 14:53:54 -0400103
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500104 net_send_udp_packet(net_server_ethaddr, net_dns_server,
Joe Hershberger786eac52015-04-08 01:41:15 -0500105 DNS_SERVICE_PORT, dns_our_port, n);
Robin Getz1a32bf42009-07-20 14:53:54 -0400106 debug("DNS packet sent\n");
107}
108
Joe Hershberger786eac52015-04-08 01:41:15 -0500109static void dns_timeout_handler(void)
Robin Getz1a32bf42009-07-20 14:53:54 -0400110{
111 puts("Timeout\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000112 net_set_state(NETLOOP_FAIL);
Robin Getz1a32bf42009-07-20 14:53:54 -0400113}
114
Joe Hershberger049a95a2015-04-08 01:41:01 -0500115static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
116 unsigned src, unsigned len)
Robin Getz1a32bf42009-07-20 14:53:54 -0400117{
118 struct header *header;
119 const unsigned char *p, *e, *s;
120 u16 type, i;
121 int found, stop, dlen;
Joe Hershberger786eac52015-04-08 01:41:15 -0500122 char ip_str[22];
Joe Hershberger049a95a2015-04-08 01:41:01 -0500123 struct in_addr ip_addr;
Robin Getz1a32bf42009-07-20 14:53:54 -0400124
125
126 debug("%s\n", __func__);
Joe Hershberger786eac52015-04-08 01:41:15 -0500127 if (dest != dns_our_port)
Robin Getz1a32bf42009-07-20 14:53:54 -0400128 return;
129
130 for (i = 0; i < len; i += 4)
131 debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
Joe Hershberger786eac52015-04-08 01:41:15 -0500132 pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
Robin Getz1a32bf42009-07-20 14:53:54 -0400133
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +0000134 /* We sent one query. We want to have a single answer: */
Joe Hershberger786eac52015-04-08 01:41:15 -0500135 header = (struct header *)pkt;
Robin Getz1a32bf42009-07-20 14:53:54 -0400136 if (ntohs(header->nqueries) != 1)
137 return;
138
139 /* Received 0 answers */
140 if (header->nanswers == 0) {
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +0000141 puts("DNS: host not found\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000142 net_set_state(NETLOOP_SUCCESS);
Robin Getz1a32bf42009-07-20 14:53:54 -0400143 return;
144 }
145
146 /* Skip host name */
147 s = &header->data[0];
148 e = pkt + len;
149 for (p = s; p < e && *p != '\0'; p++)
150 continue;
151
152 /* We sent query class 1, query type 1 */
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +0000153 if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
154 puts("DNS: response was not an A record\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +0000155 net_set_state(NETLOOP_SUCCESS);
Robin Getz1a32bf42009-07-20 14:53:54 -0400156 return;
157 }
158
159 /* Go to the first answer section */
160 p += 5;
161
162 /* Loop through the answers, we want A type answer */
163 for (found = stop = 0; !stop && &p[12] < e; ) {
Robin Getz1a32bf42009-07-20 14:53:54 -0400164 /* Skip possible name in CNAME answer */
165 if (*p != 0xc0) {
166 while (*p && &p[12] < e)
167 p++;
168 p--;
169 }
170 debug("Name (Offset in header): %d\n", p[1]);
171
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +0000172 type = get_unaligned_be16(p+2);
Robin Getz1a32bf42009-07-20 14:53:54 -0400173 debug("type = %d\n", type);
174 if (type == DNS_CNAME_RECORD) {
175 /* CNAME answer. shift to the next section */
176 debug("Found canonical name\n");
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +0000177 dlen = get_unaligned_be16(p+10);
Robin Getz1a32bf42009-07-20 14:53:54 -0400178 debug("dlen = %d\n", dlen);
179 p += 12 + dlen;
180 } else if (type == DNS_A_RECORD) {
181 debug("Found A-record\n");
Joe Hershberger786eac52015-04-08 01:41:15 -0500182 found = 1;
183 stop = 1;
Robin Getz1a32bf42009-07-20 14:53:54 -0400184 } else {
185 debug("Unknown type\n");
186 stop = 1;
187 }
188 }
189
190 if (found && &p[12] < e) {
Bernhard Kaindl6dc809f2011-10-15 23:59:22 +0000191 dlen = get_unaligned_be16(p+10);
Robin Getz1a32bf42009-07-20 14:53:54 -0400192 p += 12;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500193 memcpy(&ip_addr, p, 4);
Robin Getz1a32bf42009-07-20 14:53:54 -0400194
195 if (p + dlen <= e) {
Joe Hershberger786eac52015-04-08 01:41:15 -0500196 ip_to_string(ip_addr, ip_str);
197 printf("%s\n", ip_str);
198 if (net_dns_env_var)
Simon Glass382bee52017-08-03 12:22:09 -0600199 env_set(net_dns_env_var, ip_str);
Joe Hershberger786eac52015-04-08 01:41:15 -0500200 } else {
Robin Getz1a32bf42009-07-20 14:53:54 -0400201 puts("server responded with invalid IP number\n");
Joe Hershberger786eac52015-04-08 01:41:15 -0500202 }
Robin Getz1a32bf42009-07-20 14:53:54 -0400203 }
204
Joe Hershberger22f6e992012-05-23 07:59:14 +0000205 net_set_state(NETLOOP_SUCCESS);
Robin Getz1a32bf42009-07-20 14:53:54 -0400206}
207
Joe Hershberger786eac52015-04-08 01:41:15 -0500208void dns_start(void)
Robin Getz1a32bf42009-07-20 14:53:54 -0400209{
210 debug("%s\n", __func__);
211
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500212 net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500213 net_set_udp_handler(dns_handler);
Robin Getz1a32bf42009-07-20 14:53:54 -0400214
Gerhard Sittigf395e752014-09-12 08:48:15 +0200215 /* Clear a previous MAC address, the server IP might have changed. */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500216 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
Gerhard Sittigf395e752014-09-12 08:48:15 +0200217
Joe Hershberger786eac52015-04-08 01:41:15 -0500218 dns_send();
Robin Getz1a32bf42009-07-20 14:53:54 -0400219}