summaryrefslogtreecommitdiff
path: root/core/legacynet/dnsresolv.c
blob: 2b014cb449f4f6471551d70d62647e0717d115b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <stdio.h>
#include <string.h>
#include <core.h>
#include "pxe.h"

/* DNS CLASS values we care about */
#define CLASS_IN	1

/* DNS TYPE values we care about */
#define TYPE_A		1
#define TYPE_CNAME	5

/*
 * The DNS header structure
 */
struct dnshdr {
    uint16_t id;
    uint16_t flags;
    /* number of entries in the question section */
    uint16_t qdcount;
    /* number of resource records in the answer section */
    uint16_t ancount;
    /* number of name server resource records in the authority records section*/
    uint16_t nscount;
    /* number of resource records in the additional records section */
    uint16_t arcount;
} __attribute__ ((packed));

/*
 * The DNS query structure
 */
struct dnsquery {
    uint16_t qtype;
    uint16_t qclass;
} __attribute__ ((packed));

/*
 * The DNS Resource recodes structure
 */
struct dnsrr {
    uint16_t type;
    uint16_t class;
    uint32_t ttl;
    uint16_t rdlength;   /* The lenght of this rr data */
    char     rdata[];
} __attribute__ ((packed));


#define DNS_PORT	htons(53)               /* Default DNS port */
#define DNS_MAX_SERVERS 4		/* Max no of DNS servers */

uint32_t dns_server[DNS_MAX_SERVERS] = {0, };


/*
 * Turn a string in _src_ into a DNS "label set" in _dst_; returns the
 * number of dots encountered. On return, *dst is updated.
 */
int dns_mangle(char **dst, const char *p)
{
    char *q = *dst;
    char *count_ptr;
    char c;
    int dots = 0;

    count_ptr = q;
    *q++ = 0;

    while (1) {
        c = *p++;
        if (c == 0 || c == ':' || c == '/')
            break;
        if (c == '.') {
            dots++;
            count_ptr = q;
            *q++ = 0;
            continue;
        }

        *count_ptr += 1;
        *q++ = c;
    }

    if (*count_ptr)
        *q++ = 0;

    /* update the strings */
    *dst = q;
    return dots;
}


/*
 * Compare two sets of DNS labels, in _s1_ and _s2_; the one in _s2_
 * is allowed pointers relative to a packet in buf.
 *
 */
static bool dns_compare(const void *s1, const void *s2, const void *buf)
{
    const uint8_t *q = s1;
    const uint8_t *p = s2;
    unsigned int c0, c1;

    while (1) {
	c0 = p[0];
        if (c0 >= 0xc0) {
	    /* Follow pointer */
	    c1 = p[1];
	    p = (const uint8_t *)buf + ((c0 - 0xc0) << 8) + c1;
	} else if (c0) {
	    c0++;		/* Include the length byte */
	    if (memcmp(q, p, c0))
		return false;
	    q += c0;
	    p += c0;
	} else {
	    return *q == 0;
	}
    }
}

/*
 * Copy a DNS label into a buffer, considering the possibility that we might
 * have to follow pointers relative to "buf".
 * Returns a pointer to the first free byte *after* the terminal null.
 */
static void *dns_copylabel(void *dst, const void *src, const void *buf)
{
    uint8_t *q = dst;
    const uint8_t *p = src;
    unsigned int c0, c1;

    while (1) {
	c0 = p[0];
        if (c0 >= 0xc0) {
	    /* Follow pointer */
	    c1 = p[1];
	    p = (const uint8_t *)buf + ((c0 - 0xc0) << 8) + c1;
	} else if (c0) {
	    c0++;		/* Include the length byte */
	    memcpy(q, p, c0);
	    p += c0;
	    q += c0;
	} else {
	    *q++ = 0;
	    return q;
	}
    }
}

/*
 * Skip past a DNS label set in DS:SI
 */
static char *dns_skiplabel(char *label)
{
    uint8_t c;

    while (1) {
        c = *label++;
        if (c >= 0xc0)
            return ++label; /* pointer is two bytes */
        if (c == 0)
            return label;
        label += c;
    }
}

extern const uint8_t TimeoutTable[];
extern uint16_t get_port(void);
extern void free_port(uint16_t port);

/*
 * parse the ip_str and return the ip address with *res.
 * return true if the whole string was consumed and the result
 * was valid.
 *
 */
static bool parse_dotquad(const char *ip_str, uint32_t *res)
{
    const char *p = ip_str;
    uint8_t part = 0;
    uint32_t ip = 0;
    int i;

    for (i = 0; i < 4; i++) {
        while (is_digit(*p)) {
            part = part * 10 + *p - '0';
            p++;
        }
        if (i != 3 && *p != '.')
            return false;

        ip = (ip << 8) | part;
        part = 0;
        p++;
    }
    p--;

    *res = htonl(ip);
    return *p == '\0';
}

/*
 * Actual resolver function
 * Points to a null-terminated or :-terminated string in _name_
 * and returns the ip addr in _ip_ if it exists and can be found.
 * If _ip_ = 0 on exit, the lookup failed. _name_ will be updated
 *
 * XXX: probably need some caching here.
 */
__export uint32_t pxe_dns(const char *name)
{
    static char __lowmem DNSSendBuf[PKTBUF_SIZE];
    static char __lowmem DNSRecvBuf[PKTBUF_SIZE];
    char *p;
    int err;
    int dots;
    int same;
    int rd_len;
    int ques, reps;    /* number of questions and replies */
    uint8_t timeout;
    const uint8_t *timeout_ptr = TimeoutTable;
    uint32_t oldtime;
    uint32_t srv;
    uint32_t *srv_ptr;
    struct dnshdr *hd1 = (struct dnshdr *)DNSSendBuf;
    struct dnshdr *hd2 = (struct dnshdr *)DNSRecvBuf;
    struct dnsquery *query;
    struct dnsrr *rr;
    static __lowmem struct s_PXENV_UDP_WRITE udp_write;
    static __lowmem struct s_PXENV_UDP_READ  udp_read;
    uint16_t local_port;
    uint32_t result = 0;

    /*
     * Return failure on an empty input... this can happen during
     * some types of URL parsing, and this is the easiest place to
     * check for it.
     */
    if (!name || !*name)
	return 0;

    /* If it is a valid dot quad, just return that value */
    if (parse_dotquad(name, &result))
	return result;

    /* Make sure we have at least one valid DNS server */
    if (!dns_server[0])
	return 0;

    /* Get a local port number */
    local_port = get_port();

    /* First, fill the DNS header struct */
    hd1->id++;                      /* New query ID */
    hd1->flags   = htons(0x0100);   /* Recursion requested */
    hd1->qdcount = htons(1);        /* One question */
    hd1->ancount = 0;               /* No answers */
    hd1->nscount = 0;               /* No NS */
    hd1->arcount = 0;               /* No AR */

    p = DNSSendBuf + sizeof(struct dnshdr);
    dots = dns_mangle(&p, name);   /* store the CNAME */

    if (!dots) {
        p--; /* Remove final null */
        /* Uncompressed DNS label set so it ends in null */
        p = stpcpy(p, LocalDomain);
    }

    /* Fill the DNS query packet */
    query = (struct dnsquery *)p;
    query->qtype  = htons(TYPE_A);
    query->qclass = htons(CLASS_IN);
    p += sizeof(struct dnsquery);

    /* Now send it to name server */
    timeout_ptr = TimeoutTable;
    timeout = *timeout_ptr++;
    srv_ptr = dns_server;
    while (timeout) {
	srv = *srv_ptr++;
	if (!srv) {
	    srv_ptr = dns_server;
	    srv = *srv_ptr++;
	}

        udp_write.status      = 0;
        udp_write.ip          = srv;
        udp_write.gw          = gateway(srv);
        udp_write.src_port    = local_port;
        udp_write.dst_port    = DNS_PORT;
        udp_write.buffer_size = p - DNSSendBuf;
        udp_write.buffer      = FAR_PTR(DNSSendBuf);
        err = pxe_call(PXENV_UDP_WRITE, &udp_write);
        if (err || udp_write.status)
            continue;

        oldtime = jiffies();
	do {
	    if (jiffies() - oldtime >= timeout)
		goto again;

            udp_read.status      = 0;
            udp_read.src_ip      = srv;
            udp_read.dest_ip     = IPInfo.myip;
            udp_read.s_port      = DNS_PORT;
            udp_read.d_port      = local_port;
            udp_read.buffer_size = PKTBUF_SIZE;
            udp_read.buffer      = FAR_PTR(DNSRecvBuf);
            err = pxe_call(PXENV_UDP_READ, &udp_read);
	} while (err || udp_read.status || hd2->id != hd1->id);

        if ((hd2->flags ^ 0x80) & htons(0xf80f))
            goto badness;

        ques = htons(hd2->qdcount);   /* Questions */
        reps = htons(hd2->ancount);   /* Replies   */
        p = DNSRecvBuf + sizeof(struct dnshdr);
        while (ques--) {
            p = dns_skiplabel(p); /* Skip name */
            p += 4;               /* Skip question trailer */
        }

        /* Parse the replies */
        while (reps--) {
            same = dns_compare(DNSSendBuf + sizeof(struct dnshdr),
			       p, DNSRecvBuf);
            p = dns_skiplabel(p);
            rr = (struct dnsrr *)p;
            rd_len = ntohs(rr->rdlength);
            if (same && ntohs(rr->class) == CLASS_IN) {
		switch (ntohs(rr->type)) {
		case TYPE_A:
		    if (rd_len == 4) {
			result = *(uint32_t *)rr->rdata;
			goto done;
		    }
		    break;
		case TYPE_CNAME:
		    dns_copylabel(DNSSendBuf + sizeof(struct dnshdr),
				  rr->rdata, DNSRecvBuf);
		    /*
		     * We should probably rescan the packet from the top
		     * here, and technically we might have to send a whole
		     * new request here...
		     */
		    break;
		default:
		    break;
		}
	    }

            /* not the one we want, try next */
            p += sizeof(struct dnsrr) + rd_len;
        }

    badness:
        /*
         *
         ; We got back no data from this server.
         ; Unfortunately, for a recursive, non-authoritative
         ; query there is no such thing as an NXDOMAIN reply,
         ; which technically means we can't draw any
         ; conclusions.  However, in practice that means the
         ; domain doesn't exist.  If this turns out to be a
         ; problem, we may want to add code to go through all
         ; the servers before giving up.

         ; If the DNS server wasn't capable of recursion, and
         ; isn't capable of giving us an authoritative reply
         ; (i.e. neither AA or RA set), then at least try a
         ; different setver...
        */
        if (hd2->flags == htons(0x480))
            continue;

        break; /* failed */

    again:
	continue;
    }

done:
    free_port(local_port);	/* Return port number to the free pool */

    return result;
}