blob: c2f0ab98c8d040945551218ec4658412d8ec04c9 [file] [log] [blame]
Robin Getz82f0d232009-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 Hershberger5874dec2015-04-08 01:41:01 -05008 * in the DNS response as net_server_ip. This can then be used for any other
Robin Getz82f0d232009-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
Robin Getz82f0d232009-07-20 14:53:54 -040025#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060026#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060027#include <log.h>
Robin Getz82f0d232009-07-20 14:53:54 -040028#include <net.h>
Bernhard Kaindla77e2932011-10-15 23:59:22 +000029#include <asm/unaligned.h>
Robin Getz82f0d232009-07-20 14:53:54 -040030
31#include "dns.h"
32
Joe Hershbergerf725e342015-04-08 01:41:15 -050033char *net_dns_resolve; /* The host to resolve */
34char *net_dns_env_var; /* The envvar to store the answer in */
Robin Getz82f0d232009-07-20 14:53:54 -040035
Joe Hershbergerf725e342015-04-08 01:41:15 -050036static int dns_our_port;
Robin Getz82f0d232009-07-20 14:53:54 -040037
Baruch Siach235361a2020-05-20 13:31:41 +030038/*
39 * make port a little random (1024-17407)
40 * This keeps the math somewhat trivial to compute, and seems to work with
41 * all supported protocols/clients/servers
42 */
43static unsigned int random_port(void)
44{
45 return 1024 + (get_timer(0) % 0x4000);
46}
47
Joe Hershbergerf725e342015-04-08 01:41:15 -050048static void dns_send(void)
Robin Getz82f0d232009-07-20 14:53:54 -040049{
50 struct header *header;
51 int n, name_len;
52 uchar *p, *pkt;
53 const char *s;
54 const char *name;
55 enum dns_query_type qtype = DNS_A_RECORD;
56
Joe Hershbergerf725e342015-04-08 01:41:15 -050057 name = net_dns_resolve;
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050058 pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE);
59 p = pkt;
Robin Getz82f0d232009-07-20 14:53:54 -040060
61 /* Prepare DNS packet header */
Joe Hershbergerf725e342015-04-08 01:41:15 -050062 header = (struct header *)pkt;
Robin Getz82f0d232009-07-20 14:53:54 -040063 header->tid = 1;
64 header->flags = htons(0x100); /* standard query */
65 header->nqueries = htons(1); /* Just one query */
66 header->nanswers = 0;
67 header->nauth = 0;
68 header->nother = 0;
69
70 /* Encode DNS name */
71 name_len = strlen(name);
Joe Hershbergerf725e342015-04-08 01:41:15 -050072 p = (uchar *)&header->data; /* For encoding host name into packet */
Robin Getz82f0d232009-07-20 14:53:54 -040073
74 do {
75 s = strchr(name, '.');
76 if (!s)
77 s = name + name_len;
78
79 n = s - name; /* Chunk length */
80 *p++ = n; /* Copy length */
81 memcpy(p, name, n); /* Copy chunk */
82 p += n;
83
84 if (*s == '.')
85 n++;
86
87 name += n;
88 name_len -= n;
89 } while (*s != '\0');
90
91 *p++ = 0; /* Mark end of host name */
92 *p++ = 0; /* Some servers require double null */
93 *p++ = (unsigned char) qtype; /* Query Type */
94
95 *p++ = 0;
96 *p++ = 1; /* Class: inet, 0x0001 */
97
98 n = p - pkt; /* Total packet length */
99 debug("Packet size %d\n", n);
100
Joe Hershbergerf725e342015-04-08 01:41:15 -0500101 dns_our_port = random_port();
Robin Getz82f0d232009-07-20 14:53:54 -0400102
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500103 net_send_udp_packet(net_server_ethaddr, net_dns_server,
Joe Hershbergerf725e342015-04-08 01:41:15 -0500104 DNS_SERVICE_PORT, dns_our_port, n);
Robin Getz82f0d232009-07-20 14:53:54 -0400105 debug("DNS packet sent\n");
106}
107
Joe Hershbergerf725e342015-04-08 01:41:15 -0500108static void dns_timeout_handler(void)
Robin Getz82f0d232009-07-20 14:53:54 -0400109{
110 puts("Timeout\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000111 net_set_state(NETLOOP_FAIL);
Robin Getz82f0d232009-07-20 14:53:54 -0400112}
113
Joe Hershberger5874dec2015-04-08 01:41:01 -0500114static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip,
115 unsigned src, unsigned len)
Robin Getz82f0d232009-07-20 14:53:54 -0400116{
117 struct header *header;
118 const unsigned char *p, *e, *s;
119 u16 type, i;
120 int found, stop, dlen;
Joe Hershbergerf725e342015-04-08 01:41:15 -0500121 char ip_str[22];
Joe Hershberger5874dec2015-04-08 01:41:01 -0500122 struct in_addr ip_addr;
Robin Getz82f0d232009-07-20 14:53:54 -0400123
124
125 debug("%s\n", __func__);
Joe Hershbergerf725e342015-04-08 01:41:15 -0500126 if (dest != dns_our_port)
Robin Getz82f0d232009-07-20 14:53:54 -0400127 return;
128
129 for (i = 0; i < len; i += 4)
130 debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
Joe Hershbergerf725e342015-04-08 01:41:15 -0500131 pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
Robin Getz82f0d232009-07-20 14:53:54 -0400132
Bernhard Kaindla77e2932011-10-15 23:59:22 +0000133 /* We sent one query. We want to have a single answer: */
Joe Hershbergerf725e342015-04-08 01:41:15 -0500134 header = (struct header *)pkt;
Robin Getz82f0d232009-07-20 14:53:54 -0400135 if (ntohs(header->nqueries) != 1)
136 return;
137
138 /* Received 0 answers */
139 if (header->nanswers == 0) {
Bernhard Kaindla77e2932011-10-15 23:59:22 +0000140 puts("DNS: host not found\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000141 net_set_state(NETLOOP_SUCCESS);
Robin Getz82f0d232009-07-20 14:53:54 -0400142 return;
143 }
144
145 /* Skip host name */
146 s = &header->data[0];
147 e = pkt + len;
148 for (p = s; p < e && *p != '\0'; p++)
149 continue;
150
151 /* We sent query class 1, query type 1 */
Bernhard Kaindla77e2932011-10-15 23:59:22 +0000152 if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
153 puts("DNS: response was not an A record\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000154 net_set_state(NETLOOP_SUCCESS);
Robin Getz82f0d232009-07-20 14:53:54 -0400155 return;
156 }
157
158 /* Go to the first answer section */
159 p += 5;
160
161 /* Loop through the answers, we want A type answer */
162 for (found = stop = 0; !stop && &p[12] < e; ) {
Robin Getz82f0d232009-07-20 14:53:54 -0400163 /* Skip possible name in CNAME answer */
164 if (*p != 0xc0) {
165 while (*p && &p[12] < e)
166 p++;
167 p--;
168 }
169 debug("Name (Offset in header): %d\n", p[1]);
170
Bernhard Kaindla77e2932011-10-15 23:59:22 +0000171 type = get_unaligned_be16(p+2);
Robin Getz82f0d232009-07-20 14:53:54 -0400172 debug("type = %d\n", type);
173 if (type == DNS_CNAME_RECORD) {
174 /* CNAME answer. shift to the next section */
175 debug("Found canonical name\n");
Bernhard Kaindla77e2932011-10-15 23:59:22 +0000176 dlen = get_unaligned_be16(p+10);
Robin Getz82f0d232009-07-20 14:53:54 -0400177 debug("dlen = %d\n", dlen);
178 p += 12 + dlen;
179 } else if (type == DNS_A_RECORD) {
180 debug("Found A-record\n");
Joe Hershbergerf725e342015-04-08 01:41:15 -0500181 found = 1;
182 stop = 1;
Robin Getz82f0d232009-07-20 14:53:54 -0400183 } else {
184 debug("Unknown type\n");
185 stop = 1;
186 }
187 }
188
189 if (found && &p[12] < e) {
Bernhard Kaindla77e2932011-10-15 23:59:22 +0000190 dlen = get_unaligned_be16(p+10);
Robin Getz82f0d232009-07-20 14:53:54 -0400191 p += 12;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500192 memcpy(&ip_addr, p, 4);
Robin Getz82f0d232009-07-20 14:53:54 -0400193
194 if (p + dlen <= e) {
Joe Hershbergerf725e342015-04-08 01:41:15 -0500195 ip_to_string(ip_addr, ip_str);
196 printf("%s\n", ip_str);
197 if (net_dns_env_var)
Simon Glass6a38e412017-08-03 12:22:09 -0600198 env_set(net_dns_env_var, ip_str);
Joe Hershbergerf725e342015-04-08 01:41:15 -0500199 } else {
Robin Getz82f0d232009-07-20 14:53:54 -0400200 puts("server responded with invalid IP number\n");
Joe Hershbergerf725e342015-04-08 01:41:15 -0500201 }
Robin Getz82f0d232009-07-20 14:53:54 -0400202 }
203
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000204 net_set_state(NETLOOP_SUCCESS);
Robin Getz82f0d232009-07-20 14:53:54 -0400205}
206
Joe Hershbergerf725e342015-04-08 01:41:15 -0500207void dns_start(void)
Robin Getz82f0d232009-07-20 14:53:54 -0400208{
209 debug("%s\n", __func__);
210
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500211 net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500212 net_set_udp_handler(dns_handler);
Robin Getz82f0d232009-07-20 14:53:54 -0400213
Gerhard Sittig57b80132014-09-12 08:48:15 +0200214 /* Clear a previous MAC address, the server IP might have changed. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500215 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
Gerhard Sittig57b80132014-09-12 08:48:15 +0200216
Joe Hershbergerf725e342015-04-08 01:41:15 -0500217 dns_send();
Robin Getz82f0d232009-07-20 14:53:54 -0400218}