Robin Getz | 1a32bf4 | 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 | 049a95a | 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 | 1a32bf4 | 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 | |
| 25 | #include <common.h> |
| 26 | #include <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 27 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 28 | #include <log.h> |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 29 | #include <net.h> |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 31 | |
| 32 | #include "dns.h" |
| 33 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 34 | char *net_dns_resolve; /* The host to resolve */ |
| 35 | char *net_dns_env_var; /* The envvar to store the answer in */ |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 36 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 37 | static int dns_our_port; |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 38 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 39 | static void dns_send(void) |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 40 | { |
| 41 | struct header *header; |
| 42 | int n, name_len; |
| 43 | uchar *p, *pkt; |
| 44 | const char *s; |
| 45 | const char *name; |
| 46 | enum dns_query_type qtype = DNS_A_RECORD; |
| 47 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 48 | name = net_dns_resolve; |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 49 | pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE); |
| 50 | p = pkt; |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 51 | |
| 52 | /* Prepare DNS packet header */ |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 53 | header = (struct header *)pkt; |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 54 | header->tid = 1; |
| 55 | header->flags = htons(0x100); /* standard query */ |
| 56 | header->nqueries = htons(1); /* Just one query */ |
| 57 | header->nanswers = 0; |
| 58 | header->nauth = 0; |
| 59 | header->nother = 0; |
| 60 | |
| 61 | /* Encode DNS name */ |
| 62 | name_len = strlen(name); |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 63 | p = (uchar *)&header->data; /* For encoding host name into packet */ |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 64 | |
| 65 | do { |
| 66 | s = strchr(name, '.'); |
| 67 | if (!s) |
| 68 | s = name + name_len; |
| 69 | |
| 70 | n = s - name; /* Chunk length */ |
| 71 | *p++ = n; /* Copy length */ |
| 72 | memcpy(p, name, n); /* Copy chunk */ |
| 73 | p += n; |
| 74 | |
| 75 | if (*s == '.') |
| 76 | n++; |
| 77 | |
| 78 | name += n; |
| 79 | name_len -= n; |
| 80 | } while (*s != '\0'); |
| 81 | |
| 82 | *p++ = 0; /* Mark end of host name */ |
| 83 | *p++ = 0; /* Some servers require double null */ |
| 84 | *p++ = (unsigned char) qtype; /* Query Type */ |
| 85 | |
| 86 | *p++ = 0; |
| 87 | *p++ = 1; /* Class: inet, 0x0001 */ |
| 88 | |
| 89 | n = p - pkt; /* Total packet length */ |
| 90 | debug("Packet size %d\n", n); |
| 91 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 92 | dns_our_port = random_port(); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 93 | |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 94 | net_send_udp_packet(net_server_ethaddr, net_dns_server, |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 95 | DNS_SERVICE_PORT, dns_our_port, n); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 96 | debug("DNS packet sent\n"); |
| 97 | } |
| 98 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 99 | static void dns_timeout_handler(void) |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 100 | { |
| 101 | puts("Timeout\n"); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 102 | net_set_state(NETLOOP_FAIL); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 105 | static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 106 | unsigned src, unsigned len) |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 107 | { |
| 108 | struct header *header; |
| 109 | const unsigned char *p, *e, *s; |
| 110 | u16 type, i; |
| 111 | int found, stop, dlen; |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 112 | char ip_str[22]; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 113 | struct in_addr ip_addr; |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 114 | |
| 115 | |
| 116 | debug("%s\n", __func__); |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 117 | if (dest != dns_our_port) |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 118 | return; |
| 119 | |
| 120 | for (i = 0; i < len; i += 4) |
| 121 | debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 122 | pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 123 | |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 124 | /* We sent one query. We want to have a single answer: */ |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 125 | header = (struct header *)pkt; |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 126 | if (ntohs(header->nqueries) != 1) |
| 127 | return; |
| 128 | |
| 129 | /* Received 0 answers */ |
| 130 | if (header->nanswers == 0) { |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 131 | puts("DNS: host not found\n"); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 132 | net_set_state(NETLOOP_SUCCESS); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
| 136 | /* Skip host name */ |
| 137 | s = &header->data[0]; |
| 138 | e = pkt + len; |
| 139 | for (p = s; p < e && *p != '\0'; p++) |
| 140 | continue; |
| 141 | |
| 142 | /* We sent query class 1, query type 1 */ |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 143 | if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { |
| 144 | puts("DNS: response was not an A record\n"); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 145 | net_set_state(NETLOOP_SUCCESS); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
| 149 | /* Go to the first answer section */ |
| 150 | p += 5; |
| 151 | |
| 152 | /* Loop through the answers, we want A type answer */ |
| 153 | for (found = stop = 0; !stop && &p[12] < e; ) { |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 154 | /* Skip possible name in CNAME answer */ |
| 155 | if (*p != 0xc0) { |
| 156 | while (*p && &p[12] < e) |
| 157 | p++; |
| 158 | p--; |
| 159 | } |
| 160 | debug("Name (Offset in header): %d\n", p[1]); |
| 161 | |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 162 | type = get_unaligned_be16(p+2); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 163 | debug("type = %d\n", type); |
| 164 | if (type == DNS_CNAME_RECORD) { |
| 165 | /* CNAME answer. shift to the next section */ |
| 166 | debug("Found canonical name\n"); |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 167 | dlen = get_unaligned_be16(p+10); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 168 | debug("dlen = %d\n", dlen); |
| 169 | p += 12 + dlen; |
| 170 | } else if (type == DNS_A_RECORD) { |
| 171 | debug("Found A-record\n"); |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 172 | found = 1; |
| 173 | stop = 1; |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 174 | } else { |
| 175 | debug("Unknown type\n"); |
| 176 | stop = 1; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (found && &p[12] < e) { |
Bernhard Kaindl | 6dc809f | 2011-10-15 23:59:22 +0000 | [diff] [blame] | 181 | dlen = get_unaligned_be16(p+10); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 182 | p += 12; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 183 | memcpy(&ip_addr, p, 4); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 184 | |
| 185 | if (p + dlen <= e) { |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 186 | ip_to_string(ip_addr, ip_str); |
| 187 | printf("%s\n", ip_str); |
| 188 | if (net_dns_env_var) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 189 | env_set(net_dns_env_var, ip_str); |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 190 | } else { |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 191 | puts("server responded with invalid IP number\n"); |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 192 | } |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 195 | net_set_state(NETLOOP_SUCCESS); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 198 | void dns_start(void) |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 199 | { |
| 200 | debug("%s\n", __func__); |
| 201 | |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 202 | net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler); |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 203 | net_set_udp_handler(dns_handler); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 204 | |
Gerhard Sittig | f395e75 | 2014-09-12 08:48:15 +0200 | [diff] [blame] | 205 | /* Clear a previous MAC address, the server IP might have changed. */ |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 206 | memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr)); |
Gerhard Sittig | f395e75 | 2014-09-12 08:48:15 +0200 | [diff] [blame] | 207 | |
Joe Hershberger | 786eac5 | 2015-04-08 01:41:15 -0500 | [diff] [blame] | 208 | dns_send(); |
Robin Getz | 1a32bf4 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 209 | } |