blob: 53b65ab8d912794301c5769d7de96e9637a7b321 [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 */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000105 if (resolution->resolver_family_priority == AF_INET) {
106 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 */
140 if ((nameserver = (struct dns_nameserver *)dgram->owner) == NULL)
141 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 */
262 if ((nameserver = (struct dns_nameserver *)dgram->owner) == NULL)
263 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;
283 int ret, send_error, bufsize, fd;
284
285 resolvers = resolution->resolvers;
286
287 ret = send_error = 0;
288 bufsize = dns_build_query(resolution->query_id, resolution->query_type, resolution->hostname_dn,
289 resolution->hostname_dn_len, trash.str, trash.size);
290
291 if (bufsize == -1)
292 return 0;
293
294 list_for_each_entry(nameserver, &resolvers->nameserver_list, list) {
295 fd = nameserver->dgram->t.sock.fd;
296 errno = 0;
297
298 ret = send(fd, trash.str, bufsize, 0);
299
300 if (ret > 0)
301 nameserver->counters.sent += 1;
302
303 if (ret == 0 || errno == EAGAIN) {
304 /* nothing written, let's update the poller that we wanted to send
305 * but we were not able to */
306 fd_want_send(fd);
307 fd_cant_send(fd);
308 }
309 }
310
311 /* update resolution */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200312 resolution->nb_responses = 0;
313 resolution->last_sent_packet = now_ms;
314
315 return 1;
316}
317
318/*
319 * update a resolvers' task timeout for next wake up
320 */
321void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
322{
323 struct dns_resolution *resolution;
324
325 if (LIST_ISEMPTY(&resolvers->curr_resolution)) {
326 /* no more resolution pending, so no wakeup anymore */
327 resolvers->t->expire = TICK_ETERNITY;
328 }
329 else {
330 resolution = LIST_NEXT(&resolvers->curr_resolution, struct dns_resolution *, list);
331 resolvers->t->expire = tick_add(resolution->last_sent_packet, resolvers->timeout.retry);
332 }
333}
334
335/*
336 * Function to validate that the buffer DNS response provided in <resp> and
337 * finishing before <bufend> is valid from a DNS protocol point of view.
338 * The caller can also ask the function to check if the response contains data
339 * for a domain name <dn_name> whose length is <dn_name_len> returns one of the
340 * DNS_RESP_* code.
341 */
342int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, char *dn_name, int dn_name_len)
343{
344 unsigned char *reader, *cname, *ptr;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200345 int i, len, flags, type, ancount, cnamelen, expected_record;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200346
347 reader = resp;
348 cname = NULL;
349 cnamelen = 0;
350 len = 0;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200351 expected_record = 0; /* flag to report if at least one expected record type is found in the response.
352 * For now, only records containing an IP address (A and AAAA) are
353 * considered as expected.
354 * Later, this function may be updated to let the caller decide what type
355 * of record is expected to consider the response as valid. (SRV or TXT types)
356 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200357
358 /* move forward 2 bytes for the query id */
359 reader += 2;
360 if (reader >= bufend)
361 return DNS_RESP_INVALID;
362
363 /*
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200364 * flags are stored over 2 bytes
365 * First byte contains:
366 * - response flag (1 bit)
367 * - opcode (4 bits)
368 * - authoritative (1 bit)
369 * - truncated (1 bit)
370 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200371 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200372 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200373 return DNS_RESP_INVALID;
374
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200375 flags = reader[0] * 256 + reader[1];
376
377 if (flags & DNS_FLAG_TRUNCATED)
378 return DNS_RESP_TRUNCATED;
379
380 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
381 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200382 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200383 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200384 return DNS_RESP_REFUSED;
385
386 return DNS_RESP_ERROR;
387 }
388
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200389 /* move forward 2 bytes for flags */
390 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200391 if (reader >= bufend)
392 return DNS_RESP_INVALID;
393
394 /* move forward 2 bytes for question count */
395 reader += 2;
396 if (reader >= bufend)
397 return DNS_RESP_INVALID;
398
399 /* analyzing answer count */
400 if (reader + 2 > bufend)
401 return DNS_RESP_INVALID;
402 ancount = reader[0] * 256 + reader[1];
403
404 if (ancount == 0)
405 return DNS_RESP_ANCOUNT_ZERO;
406
407 /* move forward 2 bytes for answer count */
408 reader += 2;
409 if (reader >= bufend)
410 return DNS_RESP_INVALID;
411
412 /* move forward 4 bytes authority and additional count */
413 reader += 4;
414 if (reader >= bufend)
415 return DNS_RESP_INVALID;
416
417 /* check if the name can stand in response */
418 if (dn_name && ((reader + dn_name_len + 1) > bufend))
419 return DNS_RESP_INVALID;
420
421 /* check hostname */
422 if (dn_name && (memcmp(reader, dn_name, dn_name_len) != 0))
423 return DNS_RESP_WRONG_NAME;
424
425 /* move forward hostname len bytes + 1 for NULL byte */
426 if (dn_name) {
427 reader = reader + dn_name_len + 1;
428 }
429 else {
430 ptr = reader;
431 while (*ptr) {
432 ptr++;
433 if (ptr >= bufend)
434 return DNS_RESP_INVALID;
435 }
436 reader = ptr + 1;
437 }
438
439 /* move forward 4 bytes for question type and question class */
440 reader += 4;
441 if (reader >= bufend)
442 return DNS_RESP_INVALID;
443
444 /* now parsing response records */
445 for (i = 1; i <= ancount; i++) {
446 if (reader >= bufend)
447 return DNS_RESP_INVALID;
448
449 /*
450 * name can be a pointer, so move forward reader cursor accordingly
451 * if 1st byte is '11XXXXXX', it means name is a pointer
452 * and 2nd byte gives the offset from resp where the hostname can
453 * be found
454 */
455 if ((*reader & 0xc0) == 0xc0) {
456 /*
457 * pointer, hostname can be found at resp + *(reader + 1)
458 */
459 if (reader + 1 > bufend)
460 return DNS_RESP_INVALID;
461
462 ptr = resp + *(reader + 1);
463
464 /* check if the pointer points inside the buffer */
465 if (ptr >= bufend)
466 return DNS_RESP_INVALID;
467 }
468 else {
469 /*
470 * name is a string which starts at first byte
471 * checking against last cname when recursing through the response
472 */
473 /* look for the end of the string and ensure it's in the buffer */
474 ptr = reader;
475 len = 0;
476 while (*ptr) {
477 ++len;
478 ++ptr;
479 if (ptr >= bufend)
480 return DNS_RESP_INVALID;
481 }
482
483 /* if cname is set, it means a CNAME recursion is in progress */
484 ptr = reader;
485 }
486
487 /* ptr now points to the name */
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200488 if ((*reader & 0xc0) != 0xc0) {
489 /* if cname is set, it means a CNAME recursion is in progress */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200490 if (cname) {
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200491 /* check if the name can stand in response */
492 if ((reader + cnamelen) > bufend)
493 return DNS_RESP_INVALID;
494 /* compare cname and current name */
495 if (memcmp(ptr, cname, cnamelen) != 0)
496 return DNS_RESP_CNAME_ERROR;
497
Baptiste Assmann325137d2015-04-13 23:40:55 +0200498 cname = reader;
499 cnamelen = dns_str_to_dn_label_len((const char *)cname);
500
501 /* move forward cnamelen bytes + NULL byte */
502 reader += (cnamelen + 1);
503 }
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200504 /* compare server hostname to current name */
505 else if (dn_name) {
506 /* check if the name can stand in response */
507 if ((reader + dn_name_len) > bufend)
508 return DNS_RESP_INVALID;
509 if (memcmp(ptr, dn_name, dn_name_len) != 0)
510 return DNS_RESP_WRONG_NAME;
Baptiste Assmann5d681ba2015-10-15 15:23:28 +0200511
512 reader += (dn_name_len + 1);
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200513 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200514 else {
515 reader += (len + 1);
516 }
517 }
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200518 else {
519 /* shortname in progress */
520 /* move forward 2 bytes for information pointer and address pointer */
521 reader += 2;
522 }
523
Baptiste Assmann325137d2015-04-13 23:40:55 +0200524 if (reader >= bufend)
525 return DNS_RESP_INVALID;
526
527 /*
528 * we know the record is either for our server hostname
529 * or a valid CNAME in a crecursion
530 */
531
532 /* now reading record type (A, AAAA, CNAME, etc...) */
533 if (reader + 2 > bufend)
534 return DNS_RESP_INVALID;
535 type = reader[0] * 256 + reader[1];
536
537 /* move forward 2 bytes for type (2) */
538 reader += 2;
539
540 /* move forward 6 bytes for class (2) and ttl (4) */
541 reader += 6;
542 if (reader >= bufend)
543 return DNS_RESP_INVALID;
544
545 /* now reading data len */
546 if (reader + 2 > bufend)
547 return DNS_RESP_INVALID;
548 len = reader[0] * 256 + reader[1];
549
550 /* move forward 2 bytes for data len */
551 reader += 2;
552
553 /* analyzing record content */
554 switch (type) {
555 case DNS_RTYPE_A:
556 /* ipv4 is stored on 4 bytes */
557 if (len != 4)
558 return DNS_RESP_INVALID;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200559 expected_record = 1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200560 break;
561
562 case DNS_RTYPE_CNAME:
563 cname = reader;
564 cnamelen = len;
565 break;
566
567 case DNS_RTYPE_AAAA:
568 /* ipv6 is stored on 16 bytes */
569 if (len != 16)
570 return DNS_RESP_INVALID;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200571 expected_record = 1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200572 break;
573 } /* switch (record type) */
574
575 /* move forward len for analyzing next record in the response */
576 reader += len;
577 } /* for i 0 to ancount */
578
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200579 if (expected_record == 0)
580 return DNS_RESP_NO_EXPECTED_RECORD;
581
Baptiste Assmann325137d2015-04-13 23:40:55 +0200582 return DNS_RESP_VALID;
583}
584
585/*
586 * search dn_name resolution in resp.
587 * If existing IP not found, return the first IP matching family_priority,
588 * otherwise, first ip found
589 * The following tasks are the responsibility of the caller:
590 * - resp contains an error free DNS response
591 * - the response matches the dn_name
592 * For both cases above, dns_validate_dns_response is required
593 * returns one of the DNS_UPD_* code
594 */
595int dns_get_ip_from_response(unsigned char *resp, unsigned char *resp_end,
596 char *dn_name, int dn_name_len, void *currentip, short currentip_sin_family,
597 int family_priority, void **newip, short *newip_sin_family)
598{
599 int i, ancount, cnamelen, type, data_len, currentip_found;
600 unsigned char *reader, *cname, *ptr, *newip4, *newip6;
601
602 cname = *newip = newip4 = newip6 = NULL;
603 cnamelen = currentip_found = 0;
604 *newip_sin_family = AF_UNSPEC;
605 ancount = (((struct dns_header *)resp)->ancount);
606 ancount = *(resp + 7);
607
608 /* bypass DNS response header */
609 reader = resp + sizeof(struct dns_header);
610
611 /* bypass DNS query section */
612 /* move forward hostname len bytes + 1 for NULL byte */
613 reader = reader + dn_name_len + 1;
614
615 /* move forward 4 bytes for question type and question class */
616 reader += 4;
617
618 /* now parsing response records */
619 for (i = 1; i <= ancount; i++) {
620 /*
621 * name can be a pointer, so move forward reader cursor accordingly
622 * if 1st byte is '11XXXXXX', it means name is a pointer
623 * and 2nd byte gives the offset from buf where the hostname can
624 * be found
625 */
626 if ((*reader & 0xc0) == 0xc0)
627 ptr = resp + *(reader + 1);
628 else
629 ptr = reader;
630
631 if (cname && memcmp(ptr, cname, cnamelen))
632 return DNS_UPD_NAME_ERROR;
633 else if (memcmp(ptr, dn_name, dn_name_len))
634 return DNS_UPD_NAME_ERROR;
635
636 if ((*reader & 0xc0) == 0xc0) {
637 /* move forward 2 bytes for information pointer and address pointer */
638 reader += 2;
639 }
640 else {
641 if (cname) {
642 cname = reader;
643 cnamelen = dns_str_to_dn_label_len((char *)cname);
644
645 /* move forward cnamelen bytes + NULL byte */
646 reader += (cnamelen + 1);
647 }
648 else {
649 /* move forward dn_name_len bytes + NULL byte */
650 reader += (dn_name_len + 1);
651 }
652 }
653
654 /*
655 * we know the record is either for our server hostname
656 * or a valid CNAME in a crecursion
657 */
658
659 /* now reading record type (A, AAAA, CNAME, etc...) */
660 type = reader[0] * 256 + reader[1];
661
662 /* move forward 2 bytes for type (2) */
663 reader += 2;
664
665 /* move forward 6 bytes for class (2) and ttl (4) */
666 reader += 6;
667
668 /* now reading data len */
669 data_len = reader[0] * 256 + reader[1];
670
671 /* move forward 2 bytes for data len */
672 reader += 2;
673
674 /* analyzing record content */
675 switch (type) {
676 case DNS_RTYPE_A:
677 /* check if current reccord's IP is the same as server one's */
678 if ((currentip_sin_family == AF_INET)
679 && (*(uint32_t *)reader == *(uint32_t *)currentip)) {
680 currentip_found = 1;
681 newip4 = reader;
682 /* we can stop now if server's family preference is IPv4
683 * and its current IP is found in the response list */
684 if (family_priority == AF_INET)
685 return DNS_UPD_NO; /* DNS_UPD matrix #1 */
686 }
687 else if (!newip4) {
688 newip4 = reader;
689 }
690
691 /* move forward data_len for analyzing next record in the response */
692 reader += data_len;
693 break;
694
695 case DNS_RTYPE_CNAME:
696 cname = reader;
697 cnamelen = data_len;
698
699 reader += data_len;
700 break;
701
702 case DNS_RTYPE_AAAA:
703 /* check if current record's IP is the same as server's one */
704 if ((currentip_sin_family == AF_INET6) && (memcmp(reader, currentip, 16) == 0)) {
705 currentip_found = 1;
706 newip6 = reader;
707 /* we can stop now if server's preference is IPv6 or is not
708 * set (which implies we prioritize IPv6 over IPv4 */
709 if (family_priority == AF_INET6)
710 return DNS_UPD_NO;
711 }
712 else if (!newip6) {
713 newip6 = reader;
714 }
715
716 /* move forward data_len for analyzing next record in the response */
717 reader += data_len;
718 break;
719
720 default:
721 /* not supported record type */
722 /* move forward data_len for analyzing next record in the response */
723 reader += data_len;
724 } /* switch (record type) */
725 } /* for i 0 to ancount */
726
727 /* only CNAMEs in the response, no IP found */
728 if (cname && !newip4 && !newip6) {
729 return DNS_UPD_CNAME;
730 }
731
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200732 /* no IP found in the response */
733 if (!newip4 && !newip6) {
734 return DNS_UPD_NO_IP_FOUND;
735 }
736
Baptiste Assmann325137d2015-04-13 23:40:55 +0200737 /* case when the caller looks first for an IPv4 address */
738 if (family_priority == AF_INET) {
739 if (newip4) {
740 *newip = newip4;
741 *newip_sin_family = AF_INET;
742 if (currentip_found == 1)
743 return DNS_UPD_NO;
744 return DNS_UPD_SRVIP_NOT_FOUND;
745 }
746 else if (newip6) {
747 *newip = newip6;
748 *newip_sin_family = AF_INET6;
749 if (currentip_found == 1)
750 return DNS_UPD_NO;
751 return DNS_UPD_SRVIP_NOT_FOUND;
752 }
753 }
754 /* case when the caller looks first for an IPv6 address */
755 else if (family_priority == AF_INET6) {
756 if (newip6) {
757 *newip = newip6;
758 *newip_sin_family = AF_INET6;
759 if (currentip_found == 1)
760 return DNS_UPD_NO;
761 return DNS_UPD_SRVIP_NOT_FOUND;
762 }
763 else if (newip4) {
764 *newip = newip4;
765 *newip_sin_family = AF_INET;
766 if (currentip_found == 1)
767 return DNS_UPD_NO;
768 return DNS_UPD_SRVIP_NOT_FOUND;
769 }
770 }
771 /* case when the caller have no preference (we prefer IPv6) */
772 else if (family_priority == AF_UNSPEC) {
773 if (newip6) {
774 *newip = newip6;
775 *newip_sin_family = AF_INET6;
776 if (currentip_found == 1)
777 return DNS_UPD_NO;
778 return DNS_UPD_SRVIP_NOT_FOUND;
779 }
780 else if (newip4) {
781 *newip = newip4;
782 *newip_sin_family = AF_INET;
783 if (currentip_found == 1)
784 return DNS_UPD_NO;
785 return DNS_UPD_SRVIP_NOT_FOUND;
786 }
787 }
788
789 /* no reason why we should change the server's IP address */
790 return DNS_UPD_NO;
791}
792
793/*
794 * returns the query id contained in a DNS response
795 */
796int dns_response_get_query_id(unsigned char *resp)
797{
798 /* read the query id from the response */
799 return resp[0] * 256 + resp[1];
800}
801
802/*
803 * used during haproxy's init phase
804 * parses resolvers sections and initializes:
805 * - task (time events) for each resolvers section
806 * - the datagram layer (network IO events) for each nameserver
807 * returns:
808 * 0 in case of error
809 * 1 when no error
810 */
811int dns_init_resolvers(void)
812{
813 struct dns_resolvers *curr_resolvers;
814 struct dns_nameserver *curnameserver;
815 struct dgram_conn *dgram;
816 struct task *t;
817 int fd;
818
819 /* give a first random value to our dns query_id seed */
820 dns_query_id_seed = random();
821
822 /* run through the resolvers section list */
823 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
824 /* create the task associated to the resolvers section */
825 if ((t = task_new()) == NULL) {
826 Alert("Starting [%s] resolvers: out of memory.\n", curr_resolvers->id);
827 return 0;
828 }
829
830 /* update task's parameters */
831 t->process = dns_process_resolve;
832 t->context = curr_resolvers;
833 t->expire = TICK_ETERNITY;
834
835 curr_resolvers->t = t;
836
837 list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) {
838 if ((dgram = calloc(1, sizeof(struct dgram_conn))) == NULL) {
839 Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id,
840 curnameserver->id);
841 return 0;
842 }
843 /* update datagram's parameters */
844 dgram->owner = (void *)curnameserver;
845 dgram->data = &resolve_dgram_cb;
846
847 /* create network UDP socket for this nameserver */
848 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
849 Alert("Starting [%s/%s] nameserver: can't create socket.\n", curr_resolvers->id,
850 curnameserver->id);
851 free(dgram);
852 dgram = NULL;
853 return 0;
854 }
855
856 /* "connect" the UDP socket to the name server IP */
Baptiste Assmann8c62c472015-09-21 20:55:08 +0200857 if (connect(fd, (struct sockaddr*)&curnameserver->addr, get_addr_len(&curnameserver->addr)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200858 Alert("Starting [%s/%s] nameserver: can't connect socket.\n", curr_resolvers->id,
859 curnameserver->id);
860 close(fd);
861 free(dgram);
862 dgram = NULL;
863 return 0;
864 }
865
866 /* make the socket non blocking */
867 fcntl(fd, F_SETFL, O_NONBLOCK);
868
869 /* add the fd in the fd list and update its parameters */
870 fd_insert(fd);
871 fdtab[fd].owner = dgram;
872 fdtab[fd].iocb = dgram_fd_handler;
873 fd_want_recv(fd);
874 dgram->t.sock.fd = fd;
875
876 /* update nameserver's datagram property */
877 curnameserver->dgram = dgram;
878
879 continue;
880 }
881
882 /* task can be queued */
883 task_queue(t);
884 }
885
886 return 1;
887}
888
889/*
890 * Forge a DNS query. It needs the following information from the caller:
891 * - <query_id>: the DNS query id corresponding to this query
892 * - <query_type>: DNS_RTYPE_* request DNS record type (A, AAAA, ANY, etc...)
893 * - <hostname_dn>: hostname in domain name format
894 * - <hostname_dn_len>: length of <hostname_dn>
895 * To store the query, the caller must pass a buffer <buf> and its size <bufsize>
896 *
897 * the DNS query is stored in <buf>
898 * returns:
899 * -1 if <buf> is too short
900 */
901int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
902{
903 struct dns_header *dns;
904 struct dns_question *qinfo;
905 char *ptr, *bufend;
906
907 memset(buf, '\0', bufsize);
908 ptr = buf;
909 bufend = buf + bufsize;
910
911 /* check if there is enough room for DNS headers */
912 if (ptr + sizeof(struct dns_header) >= bufend)
913 return -1;
914
915 /* set dns query headers */
916 dns = (struct dns_header *)ptr;
917 dns->id = (unsigned short) htons(query_id);
918 dns->qr = 0; /* query */
919 dns->opcode = 0;
920 dns->aa = 0;
921 dns->tc = 0;
922 dns->rd = 1; /* recursion desired */
923 dns->ra = 0;
924 dns->z = 0;
925 dns->rcode = 0;
926 dns->qdcount = htons(1); /* 1 question */
927 dns->ancount = 0;
928 dns->nscount = 0;
929 dns->arcount = 0;
930
931 /* move forward ptr */
932 ptr += sizeof(struct dns_header);
933
934 /* check if there is enough room for query hostname */
935 if ((ptr + hostname_dn_len) >= bufend)
936 return -1;
937
938 /* set up query hostname */
939 memcpy(ptr, hostname_dn, hostname_dn_len);
940 ptr[hostname_dn_len + 1] = '\0';
941
942 /* move forward ptr */
943 ptr += (hostname_dn_len + 1);
944
945 /* check if there is enough room for query hostname*/
946 if (ptr + sizeof(struct dns_question) >= bufend)
947 return -1;
948
949 /* set up query info (type and class) */
950 qinfo = (struct dns_question *)ptr;
951 qinfo->qtype = htons(query_type);
952 qinfo->qclass = htons(DNS_RCLASS_IN);
953
954 ptr += sizeof(struct dns_question);
955
956 return ptr - buf;
957}
958
959/*
960 * turn a string into domain name label:
961 * www.haproxy.org into 3www7haproxy3org
962 * if dn memory is pre-allocated, you must provide its size in dn_len
963 * if dn memory isn't allocated, dn_len must be set to 0.
964 * In the second case, memory will be allocated.
965 * in case of error, -1 is returned, otherwise, number of bytes copied in dn
966 */
Willy Tarreau2100b492015-07-22 16:42:43 +0200967char *dns_str_to_dn_label(const char *string, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968{
969 char *c, *d;
970 int i, offset;
971
972 /* offset between string size and theorical dn size */
973 offset = 1;
974
975 /*
976 * first, get the size of the string turned into its domain name version
977 * This function also validates the string respect the RFC
978 */
979 if ((i = dns_str_to_dn_label_len(string)) == -1)
980 return NULL;
981
982 /* yes, so let's check there is enough memory */
983 if (dn_len < i + offset)
984 return NULL;
985
Willy Tarreaud69d6f32015-07-22 16:45:36 +0200986 i = strlen(string);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200987 memcpy(dn + offset, string, i);
988 dn[i + offset] = '\0';
989 /* avoid a '\0' at the beginning of dn string which may prevent the for loop
990 * below from working.
991 * Actually, this is the reason of the offset. */
992 dn[0] = '0';
993
994 for (c = dn; *c ; ++c) {
995 /* c points to the first '0' char or a dot, which we don't want to read */
996 d = c + offset;
997 i = 0;
998 while (*d != '.' && *d) {
999 i++;
1000 d++;
1001 }
1002 *c = i;
1003
1004 c = d - 1; /* because of c++ of the for loop */
1005 }
1006
1007 return dn;
1008}
1009
1010/*
1011 * compute and return the length of <string> it it were translated into domain name
1012 * label:
1013 * www.haproxy.org into 3www7haproxy3org would return 16
1014 * NOTE: add +1 for '\0' when allocating memory ;)
1015 */
1016int dns_str_to_dn_label_len(const char *string)
1017{
1018 return strlen(string) + 1;
1019}
1020
1021/*
1022 * validates host name:
1023 * - total size
1024 * - each label size individually
1025 * returns:
1026 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1027 * 1 when no error. <err> is left unaffected.
1028 */
1029int dns_hostname_validation(const char *string, char **err)
1030{
1031 const char *c, *d;
1032 int i;
1033
1034 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1035 if (err)
1036 *err = DNS_TOO_LONG_FQDN;
1037 return 0;
1038 }
1039
1040 c = string;
1041 while (*c) {
1042 d = c;
1043
1044 i = 0;
1045 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1046 i++;
1047 if (!((*d == '-') || (*d == '_') ||
1048 ((*d >= 'a') && (*d <= 'z')) ||
1049 ((*d >= 'A') && (*d <= 'Z')) ||
1050 ((*d >= '0') && (*d <= '9')))) {
1051 if (err)
1052 *err = DNS_INVALID_CHARACTER;
1053 return 0;
1054 }
1055 d++;
1056 }
1057
1058 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1059 if (err)
1060 *err = DNS_LABEL_TOO_LONG;
1061 return 0;
1062 }
1063
1064 if (*d == '\0')
1065 goto out;
1066
1067 c = ++d;
1068 }
1069 out:
1070 return 1;
1071}
1072
1073/*
1074 * 2 bytes random generator to generate DNS query ID
1075 */
1076uint16_t dns_rnd16(void)
1077{
1078 dns_query_id_seed ^= dns_query_id_seed << 13;
1079 dns_query_id_seed ^= dns_query_id_seed >> 7;
1080 dns_query_id_seed ^= dns_query_id_seed << 17;
1081 return dns_query_id_seed;
1082}
1083
1084
1085/*
1086 * function called when a timeout occurs during name resolution process
1087 * if max number of tries is reached, then stop, otherwise, retry.
1088 */
1089struct task *dns_process_resolve(struct task *t)
1090{
1091 struct dns_resolvers *resolvers = t->context;
1092 struct dns_resolution *resolution, *res_back;
1093
1094 /* timeout occurs inevitably for the first element of the FIFO queue */
1095 if (LIST_ISEMPTY(&resolvers->curr_resolution)) {
1096 /* no first entry, so wake up was useless */
1097 t->expire = TICK_ETERNITY;
1098 return t;
1099 }
1100
1101 /* look for the first resolution which is not expired */
1102 list_for_each_entry_safe(resolution, res_back, &resolvers->curr_resolution, list) {
1103 /* when we find the first resolution in the future, then we can stop here */
1104 if (tick_is_le(now_ms, resolution->last_sent_packet))
1105 goto out;
1106
1107 /*
1108 * if current resolution has been tried too many times and finishes in timeout
1109 * we update its status and remove it from the list
1110 */
Baptiste Assmannf778bb42015-09-09 00:54:38 +02001111 if (resolution->try <= 0) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 /* clean up resolution information and remove from the list */
1113 dns_reset_resolution(resolution);
1114
1115 /* notify the result to the requester */
1116 resolution->requester_error_cb(resolution, DNS_RESP_TIMEOUT);
1117 }
1118
Baptiste Assmannf778bb42015-09-09 00:54:38 +02001119 resolution->try -= 1;
1120
Baptiste Assmann325137d2015-04-13 23:40:55 +02001121 /* check current resolution status */
1122 if (resolution->step == RSLV_STEP_RUNNING) {
1123 /* resend the DNS query */
1124 dns_send_query(resolution);
1125
1126 /* check if we have more than one resolution in the list */
1127 if (dns_check_resolution_queue(resolvers) > 1) {
1128 /* move the rsolution to the end of the list */
1129 LIST_DEL(&resolution->list);
1130 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
1131 }
1132 }
1133 }
1134
1135 out:
1136 dns_update_resolvers_timeout(resolvers);
1137 return t;
1138}