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