Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 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 |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 8 | * in the DNS response as net_server_ip. This can then be used for any other |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 9 | * 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 Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 25 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 26 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 27 | #include <log.h> |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 28 | #include <net.h> |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 29 | #include <asm/unaligned.h> |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 30 | |
| 31 | #include "dns.h" |
| 32 | |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 33 | char *net_dns_resolve; /* The host to resolve */ |
| 34 | char *net_dns_env_var; /* The envvar to store the answer in */ |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 35 | |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 36 | static int dns_our_port; |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 37 | |
Baruch Siach | 235361a | 2020-05-20 13:31:41 +0300 | [diff] [blame] | 38 | /* |
| 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 | */ |
| 43 | static unsigned int random_port(void) |
| 44 | { |
| 45 | return 1024 + (get_timer(0) % 0x4000); |
| 46 | } |
| 47 | |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 48 | static void dns_send(void) |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 49 | { |
| 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 Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 57 | name = net_dns_resolve; |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 58 | pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE); |
| 59 | p = pkt; |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 60 | |
| 61 | /* Prepare DNS packet header */ |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 62 | header = (struct header *)pkt; |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 63 | 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 Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 72 | p = (uchar *)&header->data; /* For encoding host name into packet */ |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 73 | |
| 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 Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 101 | dns_our_port = random_port(); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 102 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 103 | net_send_udp_packet(net_server_ethaddr, net_dns_server, |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 104 | DNS_SERVICE_PORT, dns_our_port, n); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 105 | debug("DNS packet sent\n"); |
| 106 | } |
| 107 | |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 108 | static void dns_timeout_handler(void) |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 109 | { |
| 110 | puts("Timeout\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 111 | net_set_state(NETLOOP_FAIL); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 114 | static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 115 | unsigned src, unsigned len) |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 116 | { |
| 117 | struct header *header; |
| 118 | const unsigned char *p, *e, *s; |
| 119 | u16 type, i; |
| 120 | int found, stop, dlen; |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 121 | char ip_str[22]; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 122 | struct in_addr ip_addr; |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 123 | |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 124 | debug("%s\n", __func__); |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 125 | if (dest != dns_our_port) |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 126 | return; |
| 127 | |
| 128 | for (i = 0; i < len; i += 4) |
| 129 | debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 130 | pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 131 | |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 132 | /* We sent one query. We want to have a single answer: */ |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 133 | header = (struct header *)pkt; |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 134 | if (ntohs(header->nqueries) != 1) |
| 135 | return; |
| 136 | |
| 137 | /* Received 0 answers */ |
| 138 | if (header->nanswers == 0) { |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 139 | puts("DNS: host not found\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 140 | net_set_state(NETLOOP_SUCCESS); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | /* Skip host name */ |
| 145 | s = &header->data[0]; |
| 146 | e = pkt + len; |
| 147 | for (p = s; p < e && *p != '\0'; p++) |
| 148 | continue; |
| 149 | |
| 150 | /* We sent query class 1, query type 1 */ |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 151 | if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { |
| 152 | puts("DNS: response was not an A record\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 153 | net_set_state(NETLOOP_SUCCESS); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | /* Go to the first answer section */ |
| 158 | p += 5; |
| 159 | |
| 160 | /* Loop through the answers, we want A type answer */ |
| 161 | for (found = stop = 0; !stop && &p[12] < e; ) { |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 162 | /* Skip possible name in CNAME answer */ |
| 163 | if (*p != 0xc0) { |
| 164 | while (*p && &p[12] < e) |
| 165 | p++; |
| 166 | p--; |
| 167 | } |
| 168 | debug("Name (Offset in header): %d\n", p[1]); |
| 169 | |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 170 | type = get_unaligned_be16(p+2); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 171 | debug("type = %d\n", type); |
| 172 | if (type == DNS_CNAME_RECORD) { |
| 173 | /* CNAME answer. shift to the next section */ |
| 174 | debug("Found canonical name\n"); |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 175 | dlen = get_unaligned_be16(p+10); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 176 | debug("dlen = %d\n", dlen); |
| 177 | p += 12 + dlen; |
| 178 | } else if (type == DNS_A_RECORD) { |
| 179 | debug("Found A-record\n"); |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 180 | found = 1; |
| 181 | stop = 1; |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 182 | } else { |
| 183 | debug("Unknown type\n"); |
| 184 | stop = 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (found && &p[12] < e) { |
Bernhard Kaindl | a77e293 | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 189 | dlen = get_unaligned_be16(p+10); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 190 | p += 12; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 191 | memcpy(&ip_addr, p, 4); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 192 | |
| 193 | if (p + dlen <= e) { |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 194 | ip_to_string(ip_addr, ip_str); |
| 195 | printf("%s\n", ip_str); |
| 196 | if (net_dns_env_var) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 197 | env_set(net_dns_env_var, ip_str); |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 198 | } else { |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 199 | puts("server responded with invalid IP number\n"); |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 200 | } |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 203 | net_set_state(NETLOOP_SUCCESS); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 206 | void dns_start(void) |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 207 | { |
| 208 | debug("%s\n", __func__); |
| 209 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 210 | net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 211 | net_set_udp_handler(dns_handler); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 212 | |
Gerhard Sittig | 57b8013 | 2014-09-12 08:48:15 +0200 | [diff] [blame] | 213 | /* Clear a previous MAC address, the server IP might have changed. */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 214 | memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr)); |
Gerhard Sittig | 57b8013 | 2014-09-12 08:48:15 +0200 | [diff] [blame] | 215 | |
Joe Hershberger | f725e34 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 216 | dns_send(); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 217 | } |