blob: b9dce6b1e32b3330c643ad71460ecf86b71ea00a [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
22#include <common/time.h>
23#include <common/ticks.h>
24
25#include <types/global.h>
26#include <types/dns.h>
27#include <types/proto_udp.h>
28
29#include <proto/checks.h>
30#include <proto/dns.h>
31#include <proto/fd.h>
32#include <proto/log.h>
33#include <proto/server.h>
34#include <proto/task.h>
35#include <proto/proto_udp.h>
36
37struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
38struct dns_resolution *resolution = NULL;
39
40static int64_t dns_query_id_seed; /* random seed */
41
42/* proto_udp callback functions for a DNS resolution */
43struct dgram_data_cb resolve_dgram_cb = {
44 .recv = dns_resolve_recv,
45 .send = dns_resolve_send,
46};
47
48#if DEBUG
49/*
50 * go through the resolutions associated to a resolvers section and print the ID and hostname in
51 * domain name format
52 * should be used for debug purpose only
53 */
54void dns_print_current_resolutions(struct dns_resolvers *resolvers)
55{
56 list_for_each_entry(resolution, &resolvers->curr_resolution, list) {
57 printf(" resolution %d for %s\n", resolution->query_id, resolution->hostname_dn);
58 }
59}
60#endif
61
62/*
63 * check if there is more than 1 resolution in the resolver's resolution list
64 * return value:
65 * 0: empty list
66 * 1: exactly one entry in the list
67 * 2: more than one entry in the list
68 */
69int dns_check_resolution_queue(struct dns_resolvers *resolvers)
70{
71
72 if (LIST_ISEMPTY(&resolvers->curr_resolution))
73 return 0;
74
75 if ((resolvers->curr_resolution.n) && (resolvers->curr_resolution.n == resolvers->curr_resolution.p))
76 return 1;
77
78 if (! ((resolvers->curr_resolution.n == resolvers->curr_resolution.p)
79 && (&resolvers->curr_resolution != resolvers->curr_resolution.n)))
80 return 2;
81
82 return 0;
83}
84
85/*
86 * reset all parameters of a DNS resolution to 0 (or equivalent)
87 * and clean it up from all associated lists (resolution->qid and resolution->list)
88 */
89void dns_reset_resolution(struct dns_resolution *resolution)
90{
91 /* update resolution status */
92 resolution->step = RSLV_STEP_NONE;
93
94 resolution->try = 0;
95 resolution->try_cname = 0;
96 resolution->last_resolution = now_ms;
97 resolution->nb_responses = 0;
98
99 /* clean up query id */
100 eb32_delete(&resolution->qid);
101 resolution->query_id = 0;
102 resolution->qid.key = 0;
103
104 /* default values */
Thierry Fournierada34842016-02-17 21:25:09 +0100105 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000106 resolution->query_type = DNS_RTYPE_A;
107 } else {
108 resolution->query_type = DNS_RTYPE_AAAA;
109 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200110
111 /* the second resolution in the queue becomes the first one */
112 LIST_DEL(&resolution->list);
113}
114
115/*
116 * function called when a network IO is generated on a name server socket for an incoming packet
117 * It performs the following actions:
118 * - check if the packet requires processing (not outdated resolution)
119 * - ensure the DNS packet received is valid and call requester's callback
120 * - call requester's error callback if invalid response
121 */
122void dns_resolve_recv(struct dgram_conn *dgram)
123{
124 struct dns_nameserver *nameserver;
125 struct dns_resolvers *resolvers;
126 struct dns_resolution *resolution;
127 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
128 unsigned char *bufend;
129 int fd, buflen, ret;
130 unsigned short query_id;
131 struct eb32_node *eb;
132
133 fd = dgram->t.sock.fd;
134
135 /* check if ready for reading */
136 if (!fd_recv_ready(fd))
137 return;
138
139 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200140 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200141 return;
142
143 resolvers = nameserver->resolvers;
144
145 /* process all pending input messages */
146 while (1) {
147 /* read message received */
148 memset(buf, '\0', DNS_MAX_UDP_MESSAGE + 1);
149 if ((buflen = recv(fd, (char*)buf , DNS_MAX_UDP_MESSAGE, 0)) < 0) {
150 /* FIXME : for now we consider EAGAIN only */
151 fd_cant_recv(fd);
152 break;
153 }
154
155 /* message too big */
156 if (buflen > DNS_MAX_UDP_MESSAGE) {
157 nameserver->counters.too_big += 1;
158 continue;
159 }
160
161 /* initializing variables */
162 bufend = buf + buflen; /* pointer to mark the end of the buffer */
163
164 /* read the query id from the packet (16 bits) */
165 if (buf + 2 > bufend) {
166 nameserver->counters.invalid += 1;
167 continue;
168 }
169 query_id = dns_response_get_query_id(buf);
170
171 /* search the query_id in the pending resolution tree */
Baptiste Assmann01daef32015-09-02 22:05:24 +0200172 eb = eb32_lookup(&resolvers->query_ids, query_id);
173 if (eb == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200174 /* unknown query id means an outdated response and can be safely ignored */
175 nameserver->counters.outdated += 1;
176 continue;
177 }
178
179 /* known query id means a resolution in prgress */
180 resolution = eb32_entry(eb, struct dns_resolution, qid);
181
182 if (!resolution) {
183 nameserver->counters.outdated += 1;
184 continue;
185 }
186
187 /* number of responses received */
188 resolution->nb_responses += 1;
189
190 ret = dns_validate_dns_response(buf, bufend, resolution->hostname_dn, resolution->hostname_dn_len);
191
192 /* treat only errors */
193 switch (ret) {
194 case DNS_RESP_INVALID:
195 case DNS_RESP_WRONG_NAME:
196 nameserver->counters.invalid += 1;
197 resolution->requester_error_cb(resolution, DNS_RESP_INVALID);
198 continue;
199
200 case DNS_RESP_ERROR:
201 nameserver->counters.other += 1;
202 resolution->requester_error_cb(resolution, DNS_RESP_ERROR);
203 continue;
204
205 case DNS_RESP_ANCOUNT_ZERO:
206 nameserver->counters.any_err += 1;
207 resolution->requester_error_cb(resolution, DNS_RESP_ANCOUNT_ZERO);
208 continue;
209
210 case DNS_RESP_NX_DOMAIN:
211 nameserver->counters.nx += 1;
212 resolution->requester_error_cb(resolution, DNS_RESP_NX_DOMAIN);
213 continue;
214
215 case DNS_RESP_REFUSED:
216 nameserver->counters.refused += 1;
217 resolution->requester_error_cb(resolution, DNS_RESP_REFUSED);
218 continue;
219
220 case DNS_RESP_CNAME_ERROR:
221 nameserver->counters.cname_error += 1;
222 resolution->requester_error_cb(resolution, DNS_RESP_CNAME_ERROR);
223 continue;
224
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200225 case DNS_RESP_TRUNCATED:
226 nameserver->counters.truncated += 1;
227 resolution->requester_error_cb(resolution, DNS_RESP_TRUNCATED);
228 continue;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200229
230 case DNS_RESP_NO_EXPECTED_RECORD:
231 nameserver->counters.other += 1;
232 resolution->requester_error_cb(resolution, DNS_RESP_NO_EXPECTED_RECORD);
233 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200234 }
235
Baptiste Assmann37bb3722015-08-07 10:18:32 +0200236 nameserver->counters.valid += 1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200237 resolution->requester_cb(resolution, nameserver, buf, buflen);
238 }
239}
240
241/*
242 * function called when a resolvers network socket is ready to send data
243 * It performs the following actions:
244 */
245void dns_resolve_send(struct dgram_conn *dgram)
246{
247 int fd;
248 struct dns_nameserver *nameserver;
249 struct dns_resolvers *resolvers;
250 struct dns_resolution *resolution;
251
252 fd = dgram->t.sock.fd;
253
254 /* check if ready for sending */
255 if (!fd_send_ready(fd))
256 return;
257
258 /* we don't want/need to be waked up any more for sending */
259 fd_stop_send(fd);
260
261 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200262 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200263 return;
264
265 resolvers = nameserver->resolvers;
266 resolution = LIST_NEXT(&resolvers->curr_resolution, struct dns_resolution *, list);
267
268 dns_send_query(resolution);
269 dns_update_resolvers_timeout(resolvers);
270}
271
272/*
273 * forge and send a DNS query to resolvers associated to a resolution
274 * It performs the following actions:
275 * returns:
276 * 0 in case of error or safe ignorance
277 * 1 if no error
278 */
279int dns_send_query(struct dns_resolution *resolution)
280{
281 struct dns_resolvers *resolvers;
282 struct dns_nameserver *nameserver;
Erwan Velu5457eb42015-10-15 15:07:26 +0200283 int ret, bufsize, fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200284
285 resolvers = resolution->resolvers;
286
Baptiste Assmann325137d2015-04-13 23:40:55 +0200287 bufsize = dns_build_query(resolution->query_id, resolution->query_type, resolution->hostname_dn,
288 resolution->hostname_dn_len, trash.str, trash.size);
289
290 if (bufsize == -1)
291 return 0;
292
293 list_for_each_entry(nameserver, &resolvers->nameserver_list, list) {
294 fd = nameserver->dgram->t.sock.fd;
295 errno = 0;
296
297 ret = send(fd, trash.str, bufsize, 0);
298
299 if (ret > 0)
300 nameserver->counters.sent += 1;
301
302 if (ret == 0 || errno == EAGAIN) {
303 /* nothing written, let's update the poller that we wanted to send
304 * but we were not able to */
305 fd_want_send(fd);
306 fd_cant_send(fd);
307 }
308 }
309
310 /* update resolution */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200311 resolution->nb_responses = 0;
312 resolution->last_sent_packet = now_ms;
313
314 return 1;
315}
316
317/*
318 * update a resolvers' task timeout for next wake up
319 */
320void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
321{
322 struct dns_resolution *resolution;
323
324 if (LIST_ISEMPTY(&resolvers->curr_resolution)) {
325 /* no more resolution pending, so no wakeup anymore */
326 resolvers->t->expire = TICK_ETERNITY;
327 }
328 else {
329 resolution = LIST_NEXT(&resolvers->curr_resolution, struct dns_resolution *, list);
330 resolvers->t->expire = tick_add(resolution->last_sent_packet, resolvers->timeout.retry);
331 }
332}
333
334/*
335 * Function to validate that the buffer DNS response provided in <resp> and
336 * finishing before <bufend> is valid from a DNS protocol point of view.
337 * The caller can also ask the function to check if the response contains data
338 * for a domain name <dn_name> whose length is <dn_name_len> returns one of the
339 * DNS_RESP_* code.
340 */
341int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, char *dn_name, int dn_name_len)
342{
343 unsigned char *reader, *cname, *ptr;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200344 int i, len, flags, type, ancount, cnamelen, expected_record;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200345
346 reader = resp;
347 cname = NULL;
348 cnamelen = 0;
349 len = 0;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200350 expected_record = 0; /* flag to report if at least one expected record type is found in the response.
351 * For now, only records containing an IP address (A and AAAA) are
352 * considered as expected.
353 * Later, this function may be updated to let the caller decide what type
354 * of record is expected to consider the response as valid. (SRV or TXT types)
355 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200356
357 /* move forward 2 bytes for the query id */
358 reader += 2;
359 if (reader >= bufend)
360 return DNS_RESP_INVALID;
361
362 /*
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200363 * flags are stored over 2 bytes
364 * First byte contains:
365 * - response flag (1 bit)
366 * - opcode (4 bits)
367 * - authoritative (1 bit)
368 * - truncated (1 bit)
369 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200370 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200371 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200372 return DNS_RESP_INVALID;
373
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200374 flags = reader[0] * 256 + reader[1];
375
376 if (flags & DNS_FLAG_TRUNCATED)
377 return DNS_RESP_TRUNCATED;
378
379 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
380 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200381 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200382 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200383 return DNS_RESP_REFUSED;
384
385 return DNS_RESP_ERROR;
386 }
387
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200388 /* move forward 2 bytes for flags */
389 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200390 if (reader >= bufend)
391 return DNS_RESP_INVALID;
392
393 /* move forward 2 bytes for question count */
394 reader += 2;
395 if (reader >= bufend)
396 return DNS_RESP_INVALID;
397
398 /* analyzing answer count */
399 if (reader + 2 > bufend)
400 return DNS_RESP_INVALID;
401 ancount = reader[0] * 256 + reader[1];
402
403 if (ancount == 0)
404 return DNS_RESP_ANCOUNT_ZERO;
405
406 /* move forward 2 bytes for answer count */
407 reader += 2;
408 if (reader >= bufend)
409 return DNS_RESP_INVALID;
410
411 /* move forward 4 bytes authority and additional count */
412 reader += 4;
413 if (reader >= bufend)
414 return DNS_RESP_INVALID;
415
416 /* check if the name can stand in response */
417 if (dn_name && ((reader + dn_name_len + 1) > bufend))
418 return DNS_RESP_INVALID;
419
420 /* check hostname */
421 if (dn_name && (memcmp(reader, dn_name, dn_name_len) != 0))
422 return DNS_RESP_WRONG_NAME;
423
424 /* move forward hostname len bytes + 1 for NULL byte */
425 if (dn_name) {
426 reader = reader + dn_name_len + 1;
427 }
428 else {
429 ptr = reader;
430 while (*ptr) {
431 ptr++;
432 if (ptr >= bufend)
433 return DNS_RESP_INVALID;
434 }
435 reader = ptr + 1;
436 }
437
438 /* move forward 4 bytes for question type and question class */
439 reader += 4;
440 if (reader >= bufend)
441 return DNS_RESP_INVALID;
442
443 /* now parsing response records */
444 for (i = 1; i <= ancount; i++) {
445 if (reader >= bufend)
446 return DNS_RESP_INVALID;
447
448 /*
449 * name can be a pointer, so move forward reader cursor accordingly
450 * if 1st byte is '11XXXXXX', it means name is a pointer
451 * and 2nd byte gives the offset from resp where the hostname can
452 * be found
453 */
454 if ((*reader & 0xc0) == 0xc0) {
455 /*
456 * pointer, hostname can be found at resp + *(reader + 1)
457 */
458 if (reader + 1 > bufend)
459 return DNS_RESP_INVALID;
460
461 ptr = resp + *(reader + 1);
462
463 /* check if the pointer points inside the buffer */
464 if (ptr >= bufend)
465 return DNS_RESP_INVALID;
466 }
467 else {
468 /*
469 * name is a string which starts at first byte
470 * checking against last cname when recursing through the response
471 */
472 /* look for the end of the string and ensure it's in the buffer */
473 ptr = reader;
474 len = 0;
475 while (*ptr) {
476 ++len;
477 ++ptr;
478 if (ptr >= bufend)
479 return DNS_RESP_INVALID;
480 }
481
482 /* if cname is set, it means a CNAME recursion is in progress */
483 ptr = reader;
484 }
485
486 /* ptr now points to the name */
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200487 if ((*reader & 0xc0) != 0xc0) {
488 /* if cname is set, it means a CNAME recursion is in progress */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200489 if (cname) {
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200490 /* check if the name can stand in response */
491 if ((reader + cnamelen) > bufend)
492 return DNS_RESP_INVALID;
493 /* compare cname and current name */
494 if (memcmp(ptr, cname, cnamelen) != 0)
495 return DNS_RESP_CNAME_ERROR;
496
Baptiste Assmann325137d2015-04-13 23:40:55 +0200497 cname = reader;
498 cnamelen = dns_str_to_dn_label_len((const char *)cname);
499
500 /* move forward cnamelen bytes + NULL byte */
501 reader += (cnamelen + 1);
502 }
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200503 /* compare server hostname to current name */
504 else if (dn_name) {
505 /* check if the name can stand in response */
506 if ((reader + dn_name_len) > bufend)
507 return DNS_RESP_INVALID;
508 if (memcmp(ptr, dn_name, dn_name_len) != 0)
509 return DNS_RESP_WRONG_NAME;
Baptiste Assmann5d681ba2015-10-15 15:23:28 +0200510
511 reader += (dn_name_len + 1);
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200512 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200513 else {
514 reader += (len + 1);
515 }
516 }
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200517 else {
518 /* shortname in progress */
519 /* move forward 2 bytes for information pointer and address pointer */
520 reader += 2;
521 }
522
Baptiste Assmann325137d2015-04-13 23:40:55 +0200523 if (reader >= bufend)
524 return DNS_RESP_INVALID;
525
526 /*
527 * we know the record is either for our server hostname
528 * or a valid CNAME in a crecursion
529 */
530
531 /* now reading record type (A, AAAA, CNAME, etc...) */
532 if (reader + 2 > bufend)
533 return DNS_RESP_INVALID;
534 type = reader[0] * 256 + reader[1];
535
536 /* move forward 2 bytes for type (2) */
537 reader += 2;
538
539 /* move forward 6 bytes for class (2) and ttl (4) */
540 reader += 6;
541 if (reader >= bufend)
542 return DNS_RESP_INVALID;
543
544 /* now reading data len */
545 if (reader + 2 > bufend)
546 return DNS_RESP_INVALID;
547 len = reader[0] * 256 + reader[1];
548
549 /* move forward 2 bytes for data len */
550 reader += 2;
551
552 /* analyzing record content */
553 switch (type) {
554 case DNS_RTYPE_A:
555 /* ipv4 is stored on 4 bytes */
556 if (len != 4)
557 return DNS_RESP_INVALID;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200558 expected_record = 1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200559 break;
560
561 case DNS_RTYPE_CNAME:
562 cname = reader;
563 cnamelen = len;
564 break;
565
566 case DNS_RTYPE_AAAA:
567 /* ipv6 is stored on 16 bytes */
568 if (len != 16)
569 return DNS_RESP_INVALID;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200570 expected_record = 1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200571 break;
572 } /* switch (record type) */
573
574 /* move forward len for analyzing next record in the response */
575 reader += len;
576 } /* for i 0 to ancount */
577
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200578 if (expected_record == 0)
579 return DNS_RESP_NO_EXPECTED_RECORD;
580
Baptiste Assmann325137d2015-04-13 23:40:55 +0200581 return DNS_RESP_VALID;
582}
583
584/*
585 * search dn_name resolution in resp.
586 * If existing IP not found, return the first IP matching family_priority,
587 * otherwise, first ip found
588 * The following tasks are the responsibility of the caller:
589 * - resp contains an error free DNS response
590 * - the response matches the dn_name
591 * For both cases above, dns_validate_dns_response is required
592 * returns one of the DNS_UPD_* code
593 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100594#define DNS_MAX_IP_REC 20
Baptiste Assmann325137d2015-04-13 23:40:55 +0200595int dns_get_ip_from_response(unsigned char *resp, unsigned char *resp_end,
Thierry Fournierada34842016-02-17 21:25:09 +0100596 struct dns_resolution *resol, void *currentip,
597 short currentip_sin_family,
598 void **newip, short *newip_sin_family)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200599{
Thierry Fournierada34842016-02-17 21:25:09 +0100600 int family_priority;
601 char *dn_name;
602 int dn_name_len;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200603 int i, ancount, cnamelen, type, data_len, currentip_found;
604 unsigned char *reader, *cname, *ptr, *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100605 struct {
606 unsigned char *ip;
607 unsigned char type;
608 } rec[DNS_MAX_IP_REC];
609 int currentip_sel;
610 int j;
611 int rec_nb = 0;
612 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200613
Thierry Fournierada34842016-02-17 21:25:09 +0100614 family_priority = resol->opts->family_prio;
615 dn_name = resol->hostname_dn;
616 dn_name_len = resol->hostname_dn_len;
617
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618 cname = *newip = newip4 = newip6 = NULL;
619 cnamelen = currentip_found = 0;
620 *newip_sin_family = AF_UNSPEC;
Vincent Bernat9b7125c2016-04-08 22:17:45 +0200621 ancount = *(resp + 7); /* Assume no more than 256 answers */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200622
623 /* bypass DNS response header */
624 reader = resp + sizeof(struct dns_header);
625
626 /* bypass DNS query section */
627 /* move forward hostname len bytes + 1 for NULL byte */
628 reader = reader + dn_name_len + 1;
629
630 /* move forward 4 bytes for question type and question class */
631 reader += 4;
632
633 /* now parsing response records */
634 for (i = 1; i <= ancount; i++) {
635 /*
636 * name can be a pointer, so move forward reader cursor accordingly
637 * if 1st byte is '11XXXXXX', it means name is a pointer
638 * and 2nd byte gives the offset from buf where the hostname can
639 * be found
640 */
641 if ((*reader & 0xc0) == 0xc0)
642 ptr = resp + *(reader + 1);
643 else
644 ptr = reader;
645
Baptiste Assmanne4c4b7d2015-10-28 02:10:02 +0100646 if (cname) {
647 if (memcmp(ptr, cname, cnamelen)) {
648 return DNS_UPD_NAME_ERROR;
649 }
650 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200651 else if (memcmp(ptr, dn_name, dn_name_len))
652 return DNS_UPD_NAME_ERROR;
653
654 if ((*reader & 0xc0) == 0xc0) {
655 /* move forward 2 bytes for information pointer and address pointer */
656 reader += 2;
657 }
658 else {
659 if (cname) {
660 cname = reader;
661 cnamelen = dns_str_to_dn_label_len((char *)cname);
662
663 /* move forward cnamelen bytes + NULL byte */
664 reader += (cnamelen + 1);
665 }
666 else {
667 /* move forward dn_name_len bytes + NULL byte */
668 reader += (dn_name_len + 1);
669 }
670 }
671
672 /*
673 * we know the record is either for our server hostname
674 * or a valid CNAME in a crecursion
675 */
676
677 /* now reading record type (A, AAAA, CNAME, etc...) */
678 type = reader[0] * 256 + reader[1];
679
680 /* move forward 2 bytes for type (2) */
681 reader += 2;
682
683 /* move forward 6 bytes for class (2) and ttl (4) */
684 reader += 6;
685
686 /* now reading data len */
687 data_len = reader[0] * 256 + reader[1];
688
689 /* move forward 2 bytes for data len */
690 reader += 2;
691
692 /* analyzing record content */
693 switch (type) {
694 case DNS_RTYPE_A:
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100695 /* Store IPv4, only if some room is avalaible. */
696 if (rec_nb < DNS_MAX_IP_REC) {
697 rec[rec_nb].ip = reader;
698 rec[rec_nb].type = AF_INET;
699 rec_nb++;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200700 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200701 /* move forward data_len for analyzing next record in the response */
702 reader += data_len;
703 break;
704
705 case DNS_RTYPE_CNAME:
706 cname = reader;
707 cnamelen = data_len;
708
709 reader += data_len;
710 break;
711
712 case DNS_RTYPE_AAAA:
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100713 /* Store IPv6, only if some room is avalaible. */
714 if (rec_nb < DNS_MAX_IP_REC) {
715 rec[rec_nb].ip = reader;
716 rec[rec_nb].type = AF_INET6;
717 rec_nb++;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200718 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200719 /* move forward data_len for analyzing next record in the response */
720 reader += data_len;
721 break;
722
723 default:
724 /* not supported record type */
725 /* move forward data_len for analyzing next record in the response */
726 reader += data_len;
727 } /* switch (record type) */
Baptiste Assmannbcbd4912016-04-18 19:42:57 +0200728 } /* list for each record entries */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200729
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100730 /* Select an IP regarding configuration preference.
731 * Top priority is the prefered network ip version,
732 * second priority is the prefered network.
733 * the last priority is the currently used IP,
734 *
735 * For these three priorities, a score is calculated. The
736 * weight are:
737 * 4 - prefered netwok ip version.
738 * 2 - prefered network.
739 * 1 - current ip.
740 * The result with the biggest score is returned.
741 */
742 max_score = -1;
743 for (i = 0; i < rec_nb; i++) {
744
745 score = 0;
746
747 /* Check for prefered ip protocol. */
748 if (rec[i].type == family_priority)
749 score += 4;
750
751 /* Check for prefered network. */
752 for (j = 0; j < resol->opts->pref_net_nb; j++) {
753
754 /* Compare only the same adresses class. */
755 if (resol->opts->pref_net[j].family != rec[i].type)
756 continue;
757
758 if ((rec[i].type == AF_INET &&
Willy Tarreaueec1d382016-07-13 11:59:39 +0200759 in_net_ipv4(rec[i].ip,
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100760 &resol->opts->pref_net[j].mask.in4,
761 &resol->opts->pref_net[j].addr.in4)) ||
762 (rec[i].type == AF_INET6 &&
Willy Tarreaueec1d382016-07-13 11:59:39 +0200763 in_net_ipv6(rec[i].ip,
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100764 &resol->opts->pref_net[j].mask.in6,
765 &resol->opts->pref_net[j].addr.in6))) {
766 score += 2;
767 break;
768 }
769 }
770
771 /* Check for current ip matching. */
772 if (rec[i].type == currentip_sin_family &&
773 ((currentip_sin_family == AF_INET &&
Willy Tarreaueec1d382016-07-13 11:59:39 +0200774 memcmp(rec[i].ip, currentip, 4) == 0) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100775 (currentip_sin_family == AF_INET6 &&
776 memcmp(rec[i].ip, currentip, 16) == 0))) {
777 score += 1;
778 currentip_sel = 1;
779 } else
780 currentip_sel = 0;
781
782 /* Keep the address if the score is better than the previous
783 * score. The maximum score is 7, if this value is reached,
784 * we break the parsing. Implicitly, this score is reached
785 * the ip selected is the current ip.
786 */
787 if (score > max_score) {
788 if (rec[i].type == AF_INET)
789 newip4 = rec[i].ip;
790 else
791 newip6 = rec[i].ip;
792 currentip_found = currentip_sel;
793 if (score == 7)
794 return DNS_UPD_NO;
795 max_score = score;
796 }
797 }
798
Baptiste Assmann325137d2015-04-13 23:40:55 +0200799 /* only CNAMEs in the response, no IP found */
800 if (cname && !newip4 && !newip6) {
801 return DNS_UPD_CNAME;
802 }
803
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200804 /* no IP found in the response */
805 if (!newip4 && !newip6) {
806 return DNS_UPD_NO_IP_FOUND;
807 }
808
Baptiste Assmann325137d2015-04-13 23:40:55 +0200809 /* case when the caller looks first for an IPv4 address */
810 if (family_priority == AF_INET) {
811 if (newip4) {
812 *newip = newip4;
813 *newip_sin_family = AF_INET;
814 if (currentip_found == 1)
815 return DNS_UPD_NO;
816 return DNS_UPD_SRVIP_NOT_FOUND;
817 }
818 else if (newip6) {
819 *newip = newip6;
820 *newip_sin_family = AF_INET6;
821 if (currentip_found == 1)
822 return DNS_UPD_NO;
823 return DNS_UPD_SRVIP_NOT_FOUND;
824 }
825 }
826 /* case when the caller looks first for an IPv6 address */
827 else if (family_priority == AF_INET6) {
828 if (newip6) {
829 *newip = newip6;
830 *newip_sin_family = AF_INET6;
831 if (currentip_found == 1)
832 return DNS_UPD_NO;
833 return DNS_UPD_SRVIP_NOT_FOUND;
834 }
835 else if (newip4) {
836 *newip = newip4;
837 *newip_sin_family = AF_INET;
838 if (currentip_found == 1)
839 return DNS_UPD_NO;
840 return DNS_UPD_SRVIP_NOT_FOUND;
841 }
842 }
843 /* case when the caller have no preference (we prefer IPv6) */
844 else if (family_priority == AF_UNSPEC) {
845 if (newip6) {
846 *newip = newip6;
847 *newip_sin_family = AF_INET6;
848 if (currentip_found == 1)
849 return DNS_UPD_NO;
850 return DNS_UPD_SRVIP_NOT_FOUND;
851 }
852 else if (newip4) {
853 *newip = newip4;
854 *newip_sin_family = AF_INET;
855 if (currentip_found == 1)
856 return DNS_UPD_NO;
857 return DNS_UPD_SRVIP_NOT_FOUND;
858 }
859 }
860
861 /* no reason why we should change the server's IP address */
862 return DNS_UPD_NO;
863}
864
865/*
866 * returns the query id contained in a DNS response
867 */
Thiago Farinab1af23e2016-01-20 23:46:34 +0100868unsigned short dns_response_get_query_id(unsigned char *resp)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200869{
870 /* read the query id from the response */
871 return resp[0] * 256 + resp[1];
872}
873
874/*
875 * used during haproxy's init phase
876 * parses resolvers sections and initializes:
877 * - task (time events) for each resolvers section
878 * - the datagram layer (network IO events) for each nameserver
879 * returns:
880 * 0 in case of error
881 * 1 when no error
882 */
883int dns_init_resolvers(void)
884{
885 struct dns_resolvers *curr_resolvers;
886 struct dns_nameserver *curnameserver;
887 struct dgram_conn *dgram;
888 struct task *t;
889 int fd;
890
891 /* give a first random value to our dns query_id seed */
892 dns_query_id_seed = random();
893
894 /* run through the resolvers section list */
895 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
896 /* create the task associated to the resolvers section */
897 if ((t = task_new()) == NULL) {
898 Alert("Starting [%s] resolvers: out of memory.\n", curr_resolvers->id);
899 return 0;
900 }
901
902 /* update task's parameters */
903 t->process = dns_process_resolve;
904 t->context = curr_resolvers;
905 t->expire = TICK_ETERNITY;
906
907 curr_resolvers->t = t;
908
909 list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200910 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200911 Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id,
912 curnameserver->id);
913 return 0;
914 }
915 /* update datagram's parameters */
916 dgram->owner = (void *)curnameserver;
917 dgram->data = &resolve_dgram_cb;
918
919 /* create network UDP socket for this nameserver */
920 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
921 Alert("Starting [%s/%s] nameserver: can't create socket.\n", curr_resolvers->id,
922 curnameserver->id);
923 free(dgram);
924 dgram = NULL;
925 return 0;
926 }
927
928 /* "connect" the UDP socket to the name server IP */
Baptiste Assmann8c62c472015-09-21 20:55:08 +0200929 if (connect(fd, (struct sockaddr*)&curnameserver->addr, get_addr_len(&curnameserver->addr)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200930 Alert("Starting [%s/%s] nameserver: can't connect socket.\n", curr_resolvers->id,
931 curnameserver->id);
932 close(fd);
933 free(dgram);
934 dgram = NULL;
935 return 0;
936 }
937
938 /* make the socket non blocking */
939 fcntl(fd, F_SETFL, O_NONBLOCK);
940
941 /* add the fd in the fd list and update its parameters */
942 fd_insert(fd);
943 fdtab[fd].owner = dgram;
944 fdtab[fd].iocb = dgram_fd_handler;
945 fd_want_recv(fd);
946 dgram->t.sock.fd = fd;
947
948 /* update nameserver's datagram property */
949 curnameserver->dgram = dgram;
950
951 continue;
952 }
953
954 /* task can be queued */
955 task_queue(t);
956 }
957
958 return 1;
959}
960
961/*
962 * Forge a DNS query. It needs the following information from the caller:
963 * - <query_id>: the DNS query id corresponding to this query
964 * - <query_type>: DNS_RTYPE_* request DNS record type (A, AAAA, ANY, etc...)
965 * - <hostname_dn>: hostname in domain name format
966 * - <hostname_dn_len>: length of <hostname_dn>
967 * To store the query, the caller must pass a buffer <buf> and its size <bufsize>
968 *
969 * the DNS query is stored in <buf>
970 * returns:
971 * -1 if <buf> is too short
972 */
973int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
974{
975 struct dns_header *dns;
Vincent Bernat9b7125c2016-04-08 22:17:45 +0200976 struct dns_question qinfo;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200977 char *ptr, *bufend;
978
979 memset(buf, '\0', bufsize);
980 ptr = buf;
981 bufend = buf + bufsize;
982
983 /* check if there is enough room for DNS headers */
984 if (ptr + sizeof(struct dns_header) >= bufend)
985 return -1;
986
987 /* set dns query headers */
988 dns = (struct dns_header *)ptr;
989 dns->id = (unsigned short) htons(query_id);
Nenad Merdanovic8ab79422016-07-13 14:03:43 +0200990 dns->flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200991 dns->qdcount = htons(1); /* 1 question */
992 dns->ancount = 0;
993 dns->nscount = 0;
994 dns->arcount = 0;
995
996 /* move forward ptr */
997 ptr += sizeof(struct dns_header);
998
999 /* check if there is enough room for query hostname */
1000 if ((ptr + hostname_dn_len) >= bufend)
1001 return -1;
1002
1003 /* set up query hostname */
1004 memcpy(ptr, hostname_dn, hostname_dn_len);
1005 ptr[hostname_dn_len + 1] = '\0';
1006
1007 /* move forward ptr */
1008 ptr += (hostname_dn_len + 1);
1009
1010 /* check if there is enough room for query hostname*/
1011 if (ptr + sizeof(struct dns_question) >= bufend)
1012 return -1;
1013
1014 /* set up query info (type and class) */
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001015 qinfo.qtype = htons(query_type);
1016 qinfo.qclass = htons(DNS_RCLASS_IN);
1017 memcpy(ptr, &qinfo, sizeof(qinfo));
Baptiste Assmann325137d2015-04-13 23:40:55 +02001018
1019 ptr += sizeof(struct dns_question);
1020
1021 return ptr - buf;
1022}
1023
1024/*
1025 * turn a string into domain name label:
1026 * www.haproxy.org into 3www7haproxy3org
1027 * if dn memory is pre-allocated, you must provide its size in dn_len
1028 * if dn memory isn't allocated, dn_len must be set to 0.
1029 * In the second case, memory will be allocated.
1030 * in case of error, -1 is returned, otherwise, number of bytes copied in dn
1031 */
Willy Tarreau2100b492015-07-22 16:42:43 +02001032char *dns_str_to_dn_label(const char *string, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001033{
1034 char *c, *d;
1035 int i, offset;
1036
1037 /* offset between string size and theorical dn size */
1038 offset = 1;
1039
1040 /*
1041 * first, get the size of the string turned into its domain name version
1042 * This function also validates the string respect the RFC
1043 */
1044 if ((i = dns_str_to_dn_label_len(string)) == -1)
1045 return NULL;
1046
1047 /* yes, so let's check there is enough memory */
1048 if (dn_len < i + offset)
1049 return NULL;
1050
Willy Tarreaud69d6f32015-07-22 16:45:36 +02001051 i = strlen(string);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001052 memcpy(dn + offset, string, i);
1053 dn[i + offset] = '\0';
1054 /* avoid a '\0' at the beginning of dn string which may prevent the for loop
1055 * below from working.
1056 * Actually, this is the reason of the offset. */
1057 dn[0] = '0';
1058
1059 for (c = dn; *c ; ++c) {
1060 /* c points to the first '0' char or a dot, which we don't want to read */
1061 d = c + offset;
1062 i = 0;
1063 while (*d != '.' && *d) {
1064 i++;
1065 d++;
1066 }
1067 *c = i;
1068
1069 c = d - 1; /* because of c++ of the for loop */
1070 }
1071
1072 return dn;
1073}
1074
1075/*
1076 * compute and return the length of <string> it it were translated into domain name
1077 * label:
1078 * www.haproxy.org into 3www7haproxy3org would return 16
1079 * NOTE: add +1 for '\0' when allocating memory ;)
1080 */
1081int dns_str_to_dn_label_len(const char *string)
1082{
1083 return strlen(string) + 1;
1084}
1085
1086/*
1087 * validates host name:
1088 * - total size
1089 * - each label size individually
1090 * returns:
1091 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1092 * 1 when no error. <err> is left unaffected.
1093 */
1094int dns_hostname_validation(const char *string, char **err)
1095{
1096 const char *c, *d;
1097 int i;
1098
1099 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1100 if (err)
1101 *err = DNS_TOO_LONG_FQDN;
1102 return 0;
1103 }
1104
1105 c = string;
1106 while (*c) {
1107 d = c;
1108
1109 i = 0;
1110 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1111 i++;
1112 if (!((*d == '-') || (*d == '_') ||
1113 ((*d >= 'a') && (*d <= 'z')) ||
1114 ((*d >= 'A') && (*d <= 'Z')) ||
1115 ((*d >= '0') && (*d <= '9')))) {
1116 if (err)
1117 *err = DNS_INVALID_CHARACTER;
1118 return 0;
1119 }
1120 d++;
1121 }
1122
1123 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1124 if (err)
1125 *err = DNS_LABEL_TOO_LONG;
1126 return 0;
1127 }
1128
1129 if (*d == '\0')
1130 goto out;
1131
1132 c = ++d;
1133 }
1134 out:
1135 return 1;
1136}
1137
1138/*
1139 * 2 bytes random generator to generate DNS query ID
1140 */
1141uint16_t dns_rnd16(void)
1142{
1143 dns_query_id_seed ^= dns_query_id_seed << 13;
1144 dns_query_id_seed ^= dns_query_id_seed >> 7;
1145 dns_query_id_seed ^= dns_query_id_seed << 17;
1146 return dns_query_id_seed;
1147}
1148
1149
1150/*
1151 * function called when a timeout occurs during name resolution process
1152 * if max number of tries is reached, then stop, otherwise, retry.
1153 */
1154struct task *dns_process_resolve(struct task *t)
1155{
1156 struct dns_resolvers *resolvers = t->context;
1157 struct dns_resolution *resolution, *res_back;
Baptiste Assmann060e5732016-01-06 02:01:59 +01001158 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001159
1160 /* timeout occurs inevitably for the first element of the FIFO queue */
1161 if (LIST_ISEMPTY(&resolvers->curr_resolution)) {
1162 /* no first entry, so wake up was useless */
1163 t->expire = TICK_ETERNITY;
1164 return t;
1165 }
1166
1167 /* look for the first resolution which is not expired */
1168 list_for_each_entry_safe(resolution, res_back, &resolvers->curr_resolution, list) {
1169 /* when we find the first resolution in the future, then we can stop here */
1170 if (tick_is_le(now_ms, resolution->last_sent_packet))
1171 goto out;
1172
1173 /*
1174 * if current resolution has been tried too many times and finishes in timeout
1175 * we update its status and remove it from the list
1176 */
Baptiste Assmannf778bb42015-09-09 00:54:38 +02001177 if (resolution->try <= 0) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001178 /* clean up resolution information and remove from the list */
1179 dns_reset_resolution(resolution);
1180
1181 /* notify the result to the requester */
1182 resolution->requester_error_cb(resolution, DNS_RESP_TIMEOUT);
Baptiste Assmann382824c2016-01-06 01:53:46 +01001183 goto out;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001184 }
1185
Baptiste Assmannf778bb42015-09-09 00:54:38 +02001186 resolution->try -= 1;
1187
Baptiste Assmann6f79aca2016-04-05 21:19:51 +02001188 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
1189 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann060e5732016-01-06 02:01:59 +01001190
1191 /* let's change the query type if needed */
1192 if (res_preferred_afinet6) {
1193 /* fallback from AAAA to A */
1194 resolution->query_type = DNS_RTYPE_A;
1195 }
1196 else if (res_preferred_afinet) {
1197 /* fallback from A to AAAA */
1198 resolution->query_type = DNS_RTYPE_AAAA;
1199 }
1200
Baptiste Assmann382824c2016-01-06 01:53:46 +01001201 /* resend the DNS query */
1202 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001203
Baptiste Assmann382824c2016-01-06 01:53:46 +01001204 /* check if we have more than one resolution in the list */
1205 if (dns_check_resolution_queue(resolvers) > 1) {
1206 /* move the rsolution to the end of the list */
1207 LIST_DEL(&resolution->list);
1208 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001209 }
1210 }
1211
1212 out:
1213 dns_update_resolvers_timeout(resolvers);
1214 return t;
1215}