]>
Commit | Line | Data |
---|---|---|
1a32bf41 RG |
1 | /* |
2 | * DNS support driver | |
3 | * | |
4 | * Copyright (c) 2008 Pieter Voorthuijsen <[email protected]> | |
5 | * Copyright (c) 2009 Robin Getz <[email protected]> | |
6 | * | |
7 | * This is a simple DNS implementation for U-Boot. It will use the first IP | |
049a95a7 | 8 | * in the DNS response as net_server_ip. This can then be used for any other |
1a32bf41 RG |
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 <[email protected]> | |
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 | ||
1a32bf41 | 25 | #include <command.h> |
9fb625ce | 26 | #include <env.h> |
f7ae49fc | 27 | #include <log.h> |
1a32bf41 | 28 | #include <net.h> |
6dc809f4 | 29 | #include <asm/unaligned.h> |
1a32bf41 RG |
30 | |
31 | #include "dns.h" | |
32 | ||
786eac5f JH |
33 | char *net_dns_resolve; /* The host to resolve */ |
34 | char *net_dns_env_var; /* The envvar to store the answer in */ | |
1a32bf41 | 35 | |
786eac5f | 36 | static int dns_our_port; |
1a32bf41 | 37 | |
f1d925d9 BS |
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 | ||
786eac5f | 48 | static void dns_send(void) |
1a32bf41 RG |
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 | ||
786eac5f | 57 | name = net_dns_resolve; |
1203fcce JH |
58 | pkt = (uchar *)(net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE); |
59 | p = pkt; | |
1a32bf41 RG |
60 | |
61 | /* Prepare DNS packet header */ | |
786eac5f | 62 | header = (struct header *)pkt; |
1a32bf41 RG |
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); | |
786eac5f | 72 | p = (uchar *)&header->data; /* For encoding host name into packet */ |
1a32bf41 RG |
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 | ||
786eac5f | 101 | dns_our_port = random_port(); |
1a32bf41 | 102 | |
1203fcce | 103 | net_send_udp_packet(net_server_ethaddr, net_dns_server, |
786eac5f | 104 | DNS_SERVICE_PORT, dns_our_port, n); |
1a32bf41 RG |
105 | debug("DNS packet sent\n"); |
106 | } | |
107 | ||
786eac5f | 108 | static void dns_timeout_handler(void) |
1a32bf41 RG |
109 | { |
110 | puts("Timeout\n"); | |
22f6e99d | 111 | net_set_state(NETLOOP_FAIL); |
1a32bf41 RG |
112 | } |
113 | ||
049a95a7 JH |
114 | static void dns_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
115 | unsigned src, unsigned len) | |
1a32bf41 RG |
116 | { |
117 | struct header *header; | |
118 | const unsigned char *p, *e, *s; | |
119 | u16 type, i; | |
120 | int found, stop, dlen; | |
786eac5f | 121 | char ip_str[22]; |
049a95a7 | 122 | struct in_addr ip_addr; |
1a32bf41 | 123 | |
1a32bf41 | 124 | debug("%s\n", __func__); |
786eac5f | 125 | if (dest != dns_our_port) |
1a32bf41 RG |
126 | return; |
127 | ||
128 | for (i = 0; i < len; i += 4) | |
129 | debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", | |
786eac5f | 130 | pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]); |
1a32bf41 | 131 | |
6dc809f4 | 132 | /* We sent one query. We want to have a single answer: */ |
786eac5f | 133 | header = (struct header *)pkt; |
1a32bf41 RG |
134 | if (ntohs(header->nqueries) != 1) |
135 | return; | |
136 | ||
137 | /* Received 0 answers */ | |
138 | if (header->nanswers == 0) { | |
6dc809f4 | 139 | puts("DNS: host not found\n"); |
22f6e99d | 140 | net_set_state(NETLOOP_SUCCESS); |
1a32bf41 RG |
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 */ | |
6dc809f4 BK |
151 | if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) { |
152 | puts("DNS: response was not an A record\n"); | |
22f6e99d | 153 | net_set_state(NETLOOP_SUCCESS); |
1a32bf41 RG |
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; ) { | |
1a32bf41 RG |
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 | ||
6dc809f4 | 170 | type = get_unaligned_be16(p+2); |
1a32bf41 RG |
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"); | |
6dc809f4 | 175 | dlen = get_unaligned_be16(p+10); |
1a32bf41 RG |
176 | debug("dlen = %d\n", dlen); |
177 | p += 12 + dlen; | |
178 | } else if (type == DNS_A_RECORD) { | |
179 | debug("Found A-record\n"); | |
786eac5f JH |
180 | found = 1; |
181 | stop = 1; | |
1a32bf41 RG |
182 | } else { |
183 | debug("Unknown type\n"); | |
184 | stop = 1; | |
185 | } | |
186 | } | |
187 | ||
188 | if (found && &p[12] < e) { | |
6dc809f4 | 189 | dlen = get_unaligned_be16(p+10); |
1a32bf41 | 190 | p += 12; |
049a95a7 | 191 | memcpy(&ip_addr, p, 4); |
1a32bf41 RG |
192 | |
193 | if (p + dlen <= e) { | |
786eac5f JH |
194 | ip_to_string(ip_addr, ip_str); |
195 | printf("%s\n", ip_str); | |
196 | if (net_dns_env_var) | |
382bee57 | 197 | env_set(net_dns_env_var, ip_str); |
786eac5f | 198 | } else { |
1a32bf41 | 199 | puts("server responded with invalid IP number\n"); |
786eac5f | 200 | } |
1a32bf41 RG |
201 | } |
202 | ||
22f6e99d | 203 | net_set_state(NETLOOP_SUCCESS); |
1a32bf41 RG |
204 | } |
205 | ||
786eac5f | 206 | void dns_start(void) |
1a32bf41 RG |
207 | { |
208 | debug("%s\n", __func__); | |
209 | ||
bc0571fc | 210 | net_set_timeout_handler(DNS_TIMEOUT, dns_timeout_handler); |
049a95a7 | 211 | net_set_udp_handler(dns_handler); |
1a32bf41 | 212 | |
f395e75e | 213 | /* Clear a previous MAC address, the server IP might have changed. */ |
0adb5b76 | 214 | memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr)); |
f395e75e | 215 | |
786eac5f | 216 | dns_send(); |
1a32bf41 | 217 | } |