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