blob: 20f4cd31d2588af17d68b791d57be59cdff3590f [file] [log] [blame]
Emeric Brunc9437992021-02-12 19:42:55 +01001/*
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
Emeric Brun0c4a8a32021-06-11 10:48:45 +020022#include <import/ebistree.h>
23
Emeric Brunc9437992021-02-12 19:42:55 +010024#include <haproxy/action.h>
25#include <haproxy/api.h>
26#include <haproxy/cfgparse.h>
27#include <haproxy/channel.h>
28#include <haproxy/check.h>
29#include <haproxy/cli.h>
30#include <haproxy/dns.h>
31#include <haproxy/errors.h>
32#include <haproxy/fd.h>
Emeric Brunc9437992021-02-12 19:42:55 +010033#include <haproxy/http_rules.h>
34#include <haproxy/log.h>
35#include <haproxy/net_helper.h>
36#include <haproxy/protocol.h>
37#include <haproxy/proxy.h>
38#include <haproxy/resolvers.h>
39#include <haproxy/ring.h>
40#include <haproxy/sample.h>
41#include <haproxy/server.h>
42#include <haproxy/stats.h>
43#include <haproxy/stream_interface.h>
44#include <haproxy/task.h>
45#include <haproxy/tcp_rules.h>
46#include <haproxy/ticks.h>
47#include <haproxy/time.h>
Willy Tarreauca14dd52021-05-08 12:59:47 +020048#include <haproxy/tools.h>
Emeric Brunc9437992021-02-12 19:42:55 +010049#include <haproxy/vars.h>
50
51
52struct list sec_resolvers = LIST_HEAD_INIT(sec_resolvers);
53struct list resolv_srvrq_list = LIST_HEAD_INIT(resolv_srvrq_list);
54
55static THREAD_LOCAL uint64_t resolv_query_id_seed = 0; /* random seed */
56struct resolvers *curr_resolvers = NULL;
57
58DECLARE_STATIC_POOL(resolv_answer_item_pool, "resolv_answer_item", sizeof(struct resolv_answer_item));
59DECLARE_STATIC_POOL(resolv_resolution_pool, "resolv_resolution", sizeof(struct resolv_resolution));
60DECLARE_POOL(resolv_requester_pool, "resolv_requester", sizeof(struct resolv_requester));
61
62static unsigned int resolution_uuid = 1;
63unsigned int resolv_failed_resolutions = 0;
64
65enum {
66 DNS_STAT_ID,
67 DNS_STAT_PID,
68 DNS_STAT_SENT,
69 DNS_STAT_SND_ERROR,
70 DNS_STAT_VALID,
71 DNS_STAT_UPDATE,
72 DNS_STAT_CNAME,
73 DNS_STAT_CNAME_ERROR,
74 DNS_STAT_ANY_ERR,
75 DNS_STAT_NX,
76 DNS_STAT_TIMEOUT,
77 DNS_STAT_REFUSED,
78 DNS_STAT_OTHER,
79 DNS_STAT_INVALID,
80 DNS_STAT_TOO_BIG,
81 DNS_STAT_TRUNCATED,
82 DNS_STAT_OUTDATED,
83 DNS_STAT_END,
84};
85
86static struct name_desc dns_stats[] = {
87 [DNS_STAT_ID] = { .name = "id", .desc = "ID" },
88 [DNS_STAT_PID] = { .name = "pid", .desc = "Parent ID" },
89 [DNS_STAT_SENT] = { .name = "sent", .desc = "Sent" },
90 [DNS_STAT_SND_ERROR] = { .name = "send_error", .desc = "Send error" },
91 [DNS_STAT_VALID] = { .name = "valid", .desc = "Valid" },
92 [DNS_STAT_UPDATE] = { .name = "update", .desc = "Update" },
93 [DNS_STAT_CNAME] = { .name = "cname", .desc = "CNAME" },
94 [DNS_STAT_CNAME_ERROR] = { .name = "cname_error", .desc = "CNAME error" },
95 [DNS_STAT_ANY_ERR] = { .name = "any_err", .desc = "Any errors" },
96 [DNS_STAT_NX] = { .name = "nx", .desc = "NX" },
97 [DNS_STAT_TIMEOUT] = { .name = "timeout", .desc = "Timeout" },
98 [DNS_STAT_REFUSED] = { .name = "refused", .desc = "Refused" },
99 [DNS_STAT_OTHER] = { .name = "other", .desc = "Other" },
100 [DNS_STAT_INVALID] = { .name = "invalid", .desc = "Invalid" },
101 [DNS_STAT_TOO_BIG] = { .name = "too_big", .desc = "Too big" },
102 [DNS_STAT_TRUNCATED] = { .name = "truncated", .desc = "Truncated" },
103 [DNS_STAT_OUTDATED] = { .name = "outdated", .desc = "Outdated" },
104};
105
106static struct dns_counters dns_counters;
107
108static void dns_fill_stats(void *d, struct field *stats)
109{
110 struct dns_counters *counters = d;
111 stats[DNS_STAT_ID] = mkf_str(FO_CONFIG, counters->id);
112 stats[DNS_STAT_PID] = mkf_str(FO_CONFIG, counters->pid);
113 stats[DNS_STAT_SENT] = mkf_u64(FN_GAUGE, counters->sent);
114 stats[DNS_STAT_SND_ERROR] = mkf_u64(FN_GAUGE, counters->snd_error);
115 stats[DNS_STAT_VALID] = mkf_u64(FN_GAUGE, counters->valid);
116 stats[DNS_STAT_UPDATE] = mkf_u64(FN_GAUGE, counters->update);
117 stats[DNS_STAT_CNAME] = mkf_u64(FN_GAUGE, counters->cname);
118 stats[DNS_STAT_CNAME_ERROR] = mkf_u64(FN_GAUGE, counters->cname_error);
119 stats[DNS_STAT_ANY_ERR] = mkf_u64(FN_GAUGE, counters->any_err);
120 stats[DNS_STAT_NX] = mkf_u64(FN_GAUGE, counters->nx);
121 stats[DNS_STAT_TIMEOUT] = mkf_u64(FN_GAUGE, counters->timeout);
122 stats[DNS_STAT_REFUSED] = mkf_u64(FN_GAUGE, counters->refused);
123 stats[DNS_STAT_OTHER] = mkf_u64(FN_GAUGE, counters->other);
124 stats[DNS_STAT_INVALID] = mkf_u64(FN_GAUGE, counters->invalid);
125 stats[DNS_STAT_TOO_BIG] = mkf_u64(FN_GAUGE, counters->too_big);
126 stats[DNS_STAT_TRUNCATED] = mkf_u64(FN_GAUGE, counters->truncated);
127 stats[DNS_STAT_OUTDATED] = mkf_u64(FN_GAUGE, counters->outdated);
128}
129
130static struct stats_module dns_stats_module = {
131 .name = "dns",
132 .domain_flags = STATS_DOMAIN_DNS << STATS_DOMAIN,
133 .fill_stats = dns_fill_stats,
134 .stats = dns_stats,
135 .stats_count = DNS_STAT_END,
136 .counters = &dns_counters,
137 .counters_size = sizeof(dns_counters),
138 .clearable = 0,
139};
140
141INITCALL1(STG_REGISTER, stats_register_module, &dns_stats_module);
142
143/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
144 * no match is found.
145 */
146struct resolvers *find_resolvers_by_id(const char *id)
147{
148 struct resolvers *res;
149
150 list_for_each_entry(res, &sec_resolvers, list) {
151 if (strcmp(res->id, id) == 0)
152 return res;
153 }
154 return NULL;
155}
156
157/* Compare hostnames in a case-insensitive way .
158 * Returns 0 if they are the same, non-zero otherwise
159 */
160static __inline int resolv_hostname_cmp(const char *name1, const char *name2, int len)
161{
162 int i;
163
164 for (i = 0; i < len; i++)
165 if (tolower((unsigned char)name1[i]) != tolower((unsigned char)name2[i]))
166 return -1;
167 return 0;
168}
169
170/* Returns a pointer on the SRV request matching the name <name> for the proxy
171 * <px>. NULL is returned if no match is found.
172 */
173struct resolv_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
174{
175 struct resolv_srvrq *srvrq;
176
177 list_for_each_entry(srvrq, &resolv_srvrq_list, list) {
178 if (srvrq->proxy == px && strcmp(srvrq->name, name) == 0)
179 return srvrq;
180 }
181 return NULL;
182}
183
184/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
185 * NULL if an error occurred. */
186struct resolv_srvrq *new_resolv_srvrq(struct server *srv, char *fqdn)
187{
188 struct proxy *px = srv->proxy;
189 struct resolv_srvrq *srvrq = NULL;
190 int fqdn_len, hostname_dn_len;
191
192 fqdn_len = strlen(fqdn);
193 hostname_dn_len = resolv_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
194 trash.size);
195 if (hostname_dn_len == -1) {
196 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
197 proxy_type_str(px), px->id, srv->id, fqdn);
198 goto err;
199 }
200
201 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
202 ha_alert("config : %s '%s', server '%s': out of memory\n",
203 proxy_type_str(px), px->id, srv->id);
204 goto err;
205 }
206 srvrq->obj_type = OBJ_TYPE_SRVRQ;
207 srvrq->proxy = px;
208 srvrq->name = strdup(fqdn);
209 srvrq->hostname_dn = strdup(trash.area);
210 srvrq->hostname_dn_len = hostname_dn_len;
211 if (!srvrq->name || !srvrq->hostname_dn) {
212 ha_alert("config : %s '%s', server '%s': out of memory\n",
213 proxy_type_str(px), px->id, srv->id);
214 goto err;
215 }
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200216 LIST_INIT(&srvrq->attached_servers);
217 srvrq->named_servers = EB_ROOT;
Willy Tarreau2b718102021-04-21 07:32:39 +0200218 LIST_APPEND(&resolv_srvrq_list, &srvrq->list);
Emeric Brunc9437992021-02-12 19:42:55 +0100219 return srvrq;
220
221 err:
222 if (srvrq) {
223 free(srvrq->name);
224 free(srvrq->hostname_dn);
225 free(srvrq);
226 }
227 return NULL;
228}
229
230
Baptiste Assmann6a8d11d2021-03-10 12:22:18 +0100231/* finds and return the SRV answer item associated to a requester (whose type is 'server').
232 *
233 * returns NULL in case of error or not found.
234 */
235struct resolv_answer_item *find_srvrq_answer_record(const struct resolv_requester *requester)
236{
237 struct resolv_resolution *res;
238 struct resolv_answer_item *item;
239 struct server *srv;
240
241 if (!requester)
242 return NULL;
243
244 if ((srv = objt_server(requester->owner)) == NULL)
245 return NULL;
246 /* check if the server is managed by a SRV record */
247 if (srv->srvrq == NULL)
248 return NULL;
249
250 res = srv->srvrq->requester->resolution;
251 /* search an ANSWER record whose target points to the server's hostname and whose port is
252 * the same as server's svc_port */
253 list_for_each_entry(item, &res->response.answer_list, list) {
254 if (resolv_hostname_cmp(srv->hostname_dn, item->target, srv->hostname_dn_len) == 0 &&
255 (srv->svc_port == item->port))
256 return item;
257 }
258
259 return NULL;
260}
261
Emeric Brunc9437992021-02-12 19:42:55 +0100262/* 2 bytes random generator to generate DNS query ID */
263static inline uint16_t resolv_rnd16(void)
264{
265 if (!resolv_query_id_seed)
266 resolv_query_id_seed = now_ms;
267 resolv_query_id_seed ^= resolv_query_id_seed << 13;
268 resolv_query_id_seed ^= resolv_query_id_seed >> 7;
269 resolv_query_id_seed ^= resolv_query_id_seed << 17;
270 return resolv_query_id_seed;
271}
272
273
274static inline int resolv_resolution_timeout(struct resolv_resolution *res)
275{
276 return res->resolvers->timeout.resolve;
277}
278
279/* Updates a resolvers' task timeout for next wake up and queue it */
280static void resolv_update_resolvers_timeout(struct resolvers *resolvers)
281{
282 struct resolv_resolution *res;
283 int next;
284
285 next = tick_add(now_ms, resolvers->timeout.resolve);
286 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
287 res = LIST_NEXT(&resolvers->resolutions.curr, struct resolv_resolution *, list);
288 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
289 }
290
291 list_for_each_entry(res, &resolvers->resolutions.wait, list)
292 next = MIN(next, tick_add(res->last_resolution, resolv_resolution_timeout(res)));
293
294 resolvers->t->expire = next;
295 task_queue(resolvers->t);
296}
297
298/* Forges a DNS query. It needs the following information from the caller:
299 * - <query_id> : the DNS query id corresponding to this query
300 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
301 * - <hostname_dn> : hostname in domain name format
302 * - <hostname_dn_len> : length of <hostname_dn>
303 *
304 * To store the query, the caller must pass a buffer <buf> and its size
305 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
306 * too short.
307 */
308static int resolv_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
309 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
310{
311 struct dns_header dns_hdr;
312 struct dns_question qinfo;
313 struct dns_additional_record edns;
314 char *p = buf;
315
316 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
317 return -1;
318
319 memset(buf, 0, bufsize);
320
321 /* Set dns query headers */
322 dns_hdr.id = (unsigned short) htons(query_id);
323 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
324 dns_hdr.qdcount = htons(1); /* 1 question */
325 dns_hdr.ancount = 0;
326 dns_hdr.nscount = 0;
327 dns_hdr.arcount = htons(1);
328 memcpy(p, &dns_hdr, sizeof(dns_hdr));
329 p += sizeof(dns_hdr);
330
331 /* Set up query hostname */
332 memcpy(p, hostname_dn, hostname_dn_len);
333 p += hostname_dn_len;
334 *p++ = 0;
335
336 /* Set up query info (type and class) */
337 qinfo.qtype = htons(query_type);
338 qinfo.qclass = htons(DNS_RCLASS_IN);
339 memcpy(p, &qinfo, sizeof(qinfo));
340 p += sizeof(qinfo);
341
342 /* Set the DNS extension */
343 edns.name = 0;
344 edns.type = htons(DNS_RTYPE_OPT);
345 edns.udp_payload_size = htons(accepted_payload_size);
346 edns.extension = 0;
347 edns.data_length = 0;
348 memcpy(p, &edns, sizeof(edns));
349 p += sizeof(edns);
350
351 return (p - buf);
352}
353
354/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
355 * success, -1 otherwise.
356 */
357static int resolv_send_query(struct resolv_resolution *resolution)
358{
359 struct resolvers *resolvers = resolution->resolvers;
360 struct dns_nameserver *ns;
361 int len;
362
363 /* Update resolution */
364 resolution->nb_queries = 0;
365 resolution->nb_responses = 0;
366 resolution->last_query = now_ms;
367
368 len = resolv_build_query(resolution->query_id, resolution->query_type,
369 resolvers->accepted_payload_size,
370 resolution->hostname_dn, resolution->hostname_dn_len,
371 trash.area, trash.size);
372
373 list_for_each_entry(ns, &resolvers->nameservers, list) {
374 if (len < 0) {
375 ns->counters->snd_error++;
376 continue;
377 }
378
379 if (dns_send_nameserver(ns, trash.area, len) < 0)
380 ns->counters->snd_error++;
381 else
382 resolution->nb_queries++;
383 }
384
385 /* Push the resolution at the end of the active list */
Willy Tarreau2b718102021-04-21 07:32:39 +0200386 LIST_DELETE(&resolution->list);
387 LIST_APPEND(&resolvers->resolutions.curr, &resolution->list);
Emeric Brunc9437992021-02-12 19:42:55 +0100388 return 0;
389}
390
391/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
392 * skipped and -1 if an error occurred.
393 */
394static int
395resolv_run_resolution(struct resolv_resolution *resolution)
396{
397 struct resolvers *resolvers = resolution->resolvers;
398 int query_id, i;
399
400 /* Avoid sending requests for resolutions that don't yet have an
401 * hostname, ie resolutions linked to servers that do not yet have an
402 * fqdn */
403 if (!resolution->hostname_dn)
404 return 0;
405
406 /* Check if a resolution has already been started for this server return
407 * directly to avoid resolution pill up. */
408 if (resolution->step != RSLV_STEP_NONE)
409 return 0;
410
411 /* Generates a new query id. We try at most 100 times to find a free
412 * query id */
413 for (i = 0; i < 100; ++i) {
414 query_id = resolv_rnd16();
415 if (!eb32_lookup(&resolvers->query_ids, query_id))
416 break;
417 query_id = -1;
418 }
419 if (query_id == -1) {
420 send_log(NULL, LOG_NOTICE,
421 "could not generate a query id for %s, in resolvers %s.\n",
422 resolution->hostname_dn, resolvers->id);
423 return -1;
424 }
425
426 /* Update resolution parameters */
427 resolution->query_id = query_id;
428 resolution->qid.key = query_id;
429 resolution->step = RSLV_STEP_RUNNING;
430 resolution->query_type = resolution->prefered_query_type;
431 resolution->try = resolvers->resolve_retries;
432 eb32_insert(&resolvers->query_ids, &resolution->qid);
433
434 /* Send the DNS query */
435 resolution->try -= 1;
436 resolv_send_query(resolution);
437 return 1;
438}
439
440/* Performs a name resolution for the requester <req> */
441void resolv_trigger_resolution(struct resolv_requester *req)
442{
443 struct resolvers *resolvers;
444 struct resolv_resolution *res;
445 int exp;
446
447 if (!req || !req->resolution)
448 return;
449 res = req->resolution;
450 resolvers = res->resolvers;
451
452 /* The resolution must not be triggered yet. Use the cached response, if
453 * valid */
454 exp = tick_add(res->last_resolution, resolvers->hold.valid);
455 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
456 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
457 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
458}
459
460
461/* Resets some resolution parameters to initial values and also delete the query
462 * ID from the resolver's tree.
463 */
464static void resolv_reset_resolution(struct resolv_resolution *resolution)
465{
466 /* update resolution status */
467 resolution->step = RSLV_STEP_NONE;
468 resolution->try = 0;
469 resolution->last_resolution = now_ms;
470 resolution->nb_queries = 0;
471 resolution->nb_responses = 0;
472 resolution->query_type = resolution->prefered_query_type;
473
474 /* clean up query id */
475 eb32_delete(&resolution->qid);
476 resolution->query_id = 0;
477 resolution->qid.key = 0;
478}
479
480/* Returns the query id contained in a DNS response */
481static inline unsigned short resolv_response_get_query_id(unsigned char *resp)
482{
483 return resp[0] * 256 + resp[1];
484}
485
486
487/* Analyses, re-builds and copies the name <name> from the DNS response packet
488 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
489 * for compressed data. The result is copied into <dest>, ensuring we don't
490 * overflow using <dest_len> Returns the number of bytes the caller can move
491 * forward. If 0 it means an error occurred while parsing the name. <offset> is
492 * the number of bytes the caller could move forward.
493 */
494int resolv_read_name(unsigned char *buffer, unsigned char *bufend,
495 unsigned char *name, char *destination, int dest_len,
496 int *offset, unsigned int depth)
497{
498 int nb_bytes = 0, n = 0;
499 int label_len;
500 unsigned char *reader = name;
501 char *dest = destination;
502
503 while (1) {
504 if (reader >= bufend)
505 goto err;
506
507 /* Name compression is in use */
508 if ((*reader & 0xc0) == 0xc0) {
509 if (reader + 1 >= bufend)
510 goto err;
511
512 /* Must point BEFORE current position */
513 if ((buffer + reader[1]) > reader)
514 goto err;
515
516 if (depth++ > 100)
517 goto err;
518
519 n = resolv_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
520 dest, dest_len - nb_bytes, offset, depth);
521 if (n == 0)
522 goto err;
523
524 dest += n;
525 nb_bytes += n;
526 goto out;
527 }
528
529 label_len = *reader;
530 if (label_len == 0)
531 goto out;
532
533 /* Check if:
534 * - we won't read outside the buffer
535 * - there is enough place in the destination
536 */
537 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
538 goto err;
539
540 /* +1 to take label len + label string */
541 label_len++;
542
543 memcpy(dest, reader, label_len);
544
545 dest += label_len;
546 nb_bytes += label_len;
547 reader += label_len;
548 }
549
550 out:
551 /* offset computation:
552 * parse from <name> until finding either NULL or a pointer "c0xx"
553 */
554 reader = name;
555 *offset = 0;
556 while (reader < bufend) {
557 if ((reader[0] & 0xc0) == 0xc0) {
558 *offset += 2;
559 break;
560 }
561 else if (*reader == 0) {
562 *offset += 1;
563 break;
564 }
565 *offset += 1;
566 ++reader;
567 }
568 return nb_bytes;
569
570 err:
571 return 0;
572}
573
Christopher Faulet5f8ccff2021-06-15 16:08:48 +0200574/* Cleanup fqdn/port and address of a server attached to a SRV resolution. This
575 * happens when an SRV item is purged or when the server status is considered as
576 * obsolete.
577 *
578 * Must be called with the DNS lock held.
579 */
580static void resolv_srvrq_cleanup_srv(struct server *srv)
581{
582 resolv_unlink_resolution(srv->resolv_requester, 0);
583 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
584 srvrq_update_srv_status(srv, 1);
585 ha_free(&srv->hostname);
586 ha_free(&srv->hostname_dn);
587 srv->hostname_dn_len = 0;
588 memset(&srv->addr, 0, sizeof(srv->addr));
589 srv->svc_port = 0;
590 srv->flags |= SRV_F_NO_RESOLUTION;
591 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
592 LIST_DELETE(&srv->srv_rec_item);
593 LIST_APPEND(&srv->srvrq->attached_servers, &srv->srv_rec_item);
594}
595
Emeric Brunc9437992021-02-12 19:42:55 +0100596/* Checks for any obsolete record, also identify any SRV request, and try to
597 * find a corresponding server.
598*/
599static void resolv_check_response(struct resolv_resolution *res)
600{
601 struct resolvers *resolvers = res->resolvers;
Christopher Fauletdb31b442021-03-11 18:19:41 +0100602 struct resolv_requester *req;
Emeric Brunc9437992021-02-12 19:42:55 +0100603 struct resolv_answer_item *item, *itemback;
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200604 struct server *srv, *srvback;
Emeric Brunc9437992021-02-12 19:42:55 +0100605 struct resolv_srvrq *srvrq;
606
607 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
608 struct resolv_answer_item *ar_item = item->ar_item;
609
610 /* clean up obsolete Additional record */
Christopher Faulet55c1c402021-03-11 09:36:05 +0100611 if (ar_item && tick_is_lt(tick_add(ar_item->last_seen, resolvers->hold.obsolete), now_ms)) {
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100612 /* Cleaning up the AR item will trigger an extra DNS resolution, except if the SRV
613 * item is also obsolete.
614 */
Emeric Brunc9437992021-02-12 19:42:55 +0100615 pool_free(resolv_answer_item_pool, ar_item);
616 item->ar_item = NULL;
617 }
618
619 /* Remove obsolete items */
Christopher Faulet55c1c402021-03-11 09:36:05 +0100620 if (tick_is_lt(tick_add(item->last_seen, resolvers->hold.obsolete), now_ms)) {
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200621 if (item->type == DNS_RTYPE_A || item->type == DNS_RTYPE_AAAA) {
Emeric Brunc9437992021-02-12 19:42:55 +0100622 /* Remove any associated server */
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200623 list_for_each_entry_safe(srv, srvback, &item->attached_servers, ip_rec_item) {
624 LIST_DEL_INIT(&srv->ip_rec_item);
625 }
626 }
627 else if (item->type == DNS_RTYPE_SRV) {
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200628 /* Remove any associated server */
Christopher Faulet5f8ccff2021-06-15 16:08:48 +0200629 list_for_each_entry_safe(srv, srvback, &item->attached_servers, srv_rec_item)
630 resolv_srvrq_cleanup_srv(srv);
Emeric Brunc9437992021-02-12 19:42:55 +0100631 }
632
Willy Tarreau2b718102021-04-21 07:32:39 +0200633 LIST_DELETE(&item->list);
Emeric Brunc9437992021-02-12 19:42:55 +0100634 if (item->ar_item) {
635 pool_free(resolv_answer_item_pool, item->ar_item);
636 item->ar_item = NULL;
637 }
638 pool_free(resolv_answer_item_pool, item);
639 continue;
640 }
641
642 if (item->type != DNS_RTYPE_SRV)
643 continue;
644
645 /* Now process SRV records */
Christopher Fauletdb31b442021-03-11 18:19:41 +0100646 list_for_each_entry(req, &res->requesters, list) {
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200647 struct ebpt_node *node;
648 char target[DNS_MAX_NAME_SIZE+1];
649
650 int i;
Emeric Brunc9437992021-02-12 19:42:55 +0100651 if ((srvrq = objt_resolv_srvrq(req->owner)) == NULL)
652 continue;
653
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200654 /* Check if a server already uses that record */
655 srv = NULL;
656 list_for_each_entry(srv, &item->attached_servers, srv_rec_item) {
657 if (srv->srvrq == srvrq) {
658 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
659 goto srv_found;
Emeric Brunc9437992021-02-12 19:42:55 +0100660 }
Emeric Brunc9437992021-02-12 19:42:55 +0100661 }
662
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200663
664 /* If not empty we try to match a server
665 * in server state file tree with the same hostname
666 */
667 if (!eb_is_empty(&srvrq->named_servers)) {
668 srv = NULL;
669
670 /* convert the key to lookup in lower case */
671 for (i = 0 ; item->target[i] ; i++)
672 target[i] = tolower(item->target[i]);
673
674 node = ebis_lookup(&srvrq->named_servers, target);
675 if (node) {
676 srv = ebpt_entry(node, struct server, host_dn);
Emeric Brunc9437992021-02-12 19:42:55 +0100677 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200678
679 /* an entry was found with the same hostname
680 * let check this node if the port matches
681 * and try next node if the hostname
682 * is still the same
683 */
684 while (1) {
685 if (srv->svc_port == item->port) {
686 /* server found, we remove it from tree */
687 ebpt_delete(node);
688 free(srv->host_dn.key);
689 goto srv_found;
690 }
691
692 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
693
694 node = ebpt_next(node);
695 if (!node)
696 break;
697
698 srv = ebpt_entry(node, struct server, host_dn);
699 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
700
701 if ((item->data_len != srv->hostname_dn_len)
702 || resolv_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
703 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
704 break;
705 }
706 }
Emeric Brunc9437992021-02-12 19:42:55 +0100707 }
708 }
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200709
710 /* Pick the first server listed in srvrq (those ones don't
711 * have hostname and are free to use)
712 */
713 srv = NULL;
714 list_for_each_entry(srv, &srvrq->attached_servers, srv_rec_item) {
715 LIST_DEL_INIT(&srv->srv_rec_item);
716 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
717 goto srv_found;
718 }
719 srv = NULL;
Emeric Brunc9437992021-02-12 19:42:55 +0100720
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200721srv_found:
Emeric Brunc9437992021-02-12 19:42:55 +0100722 /* And update this server, if found (srv is locked here) */
723 if (srv) {
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100724 /* re-enable DNS resolution for this server by default */
725 srv->flags &= ~SRV_F_NO_RESOLUTION;
726
Emeric Brunc9437992021-02-12 19:42:55 +0100727 /* Check if an Additional Record is associated to this SRV record.
728 * Perform some sanity checks too to ensure the record can be used.
729 * If all fine, we simply pick up the IP address found and associate
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100730 * it to the server. And DNS resolution is disabled for this server.
Emeric Brunc9437992021-02-12 19:42:55 +0100731 */
732 if ((item->ar_item != NULL) &&
733 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100734 {
Emeric Brunc9437992021-02-12 19:42:55 +0100735
736 switch (item->ar_item->type) {
737 case DNS_RTYPE_A:
Christopher Faulet69beaa92021-02-16 12:07:47 +0100738 srv_update_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional record");
Emeric Brunc9437992021-02-12 19:42:55 +0100739 break;
740 case DNS_RTYPE_AAAA:
Christopher Faulet69beaa92021-02-16 12:07:47 +0100741 srv_update_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional record");
Emeric Brunc9437992021-02-12 19:42:55 +0100742 break;
743 }
744
745 srv->flags |= SRV_F_NO_RESOLUTION;
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100746
747 /* Unlink A/AAAA resolution for this server if there is an AR item.
748 * It is usless to perform an extra resolution
749 */
Christopher Faulet0efc0992021-03-11 18:09:53 +0100750 resolv_unlink_resolution(srv->resolv_requester, 0);
Emeric Brunc9437992021-02-12 19:42:55 +0100751 }
752
753 if (!srv->hostname_dn) {
754 const char *msg = NULL;
755 char hostname[DNS_MAX_NAME_SIZE];
756
757 if (resolv_dn_label_to_str(item->target, item->data_len+1,
758 hostname, DNS_MAX_NAME_SIZE) == -1) {
759 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
760 continue;
761 }
Christopher Faulet69beaa92021-02-16 12:07:47 +0100762 msg = srv_update_fqdn(srv, hostname, "SRV record", 1);
Emeric Brunc9437992021-02-12 19:42:55 +0100763 if (msg)
764 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
765 }
766
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200767 if (!LIST_INLIST(&srv->srv_rec_item))
768 LIST_APPEND(&item->attached_servers, &srv->srv_rec_item);
769
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100770 if (!(srv->flags & SRV_F_NO_RESOLUTION)) {
771 /* If there is no AR item responsible of the FQDN resolution,
772 * trigger a dedicated DNS resolution
773 */
774 if (!srv->resolv_requester || !srv->resolv_requester->resolution)
775 resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1);
776 }
777
Christopher Fauletab177ac2021-03-10 15:34:52 +0100778 /* Update the server status */
Christopher Faulet6b117ae2021-03-11 18:06:23 +0100779 srvrq_update_srv_status(srv, (srv->addr.ss_family != AF_INET && srv->addr.ss_family != AF_INET6));
Emeric Brunc9437992021-02-12 19:42:55 +0100780
781 srv->svc_port = item->port;
782 srv->flags &= ~SRV_F_MAPPORTS;
783
784 if (!srv->resolv_opts.ignore_weight) {
785 char weight[9];
786 int ha_weight;
787
788 /* DNS weight range if from 0 to 65535
789 * HAProxy weight is from 0 to 256
790 * The rule below ensures that weight 0 is well respected
791 * while allowing a "mapping" from DNS weight into HAProxy's one.
792 */
793 ha_weight = (item->weight + 255) / 256;
794
795 snprintf(weight, sizeof(weight), "%d", ha_weight);
796 server_parse_weight_change_request(srv, weight);
797 }
798 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
799 }
800 }
801 }
802}
803
804/* Validates that the buffer DNS response provided in <resp> and finishing
805 * before <bufend> is valid from a DNS protocol point of view.
806 *
807 * The result is stored in <resolution>' response, buf_response,
808 * response_query_records and response_answer_records members.
809 *
810 * This function returns one of the RSLV_RESP_* code to indicate the type of
811 * error found.
812 */
813static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufend,
814 struct resolv_resolution *resolution, int max_answer_records)
815{
816 unsigned char *reader;
817 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
818 int len, flags, offset;
819 int query_record_id;
820 int nb_saved_records;
821 struct resolv_query_item *query;
822 struct resolv_answer_item *answer_record, *tmp_record;
823 struct resolv_response *r_res;
824 int i, found = 0;
825 int cause = RSLV_RESP_ERROR;
826
827 reader = resp;
828 len = 0;
829 previous_dname = NULL;
830 query = NULL;
831 answer_record = NULL;
832
833 /* Initialization of response buffer and structure */
834 r_res = &resolution->response;
835
836 /* query id */
837 if (reader + 2 >= bufend)
838 goto invalid_resp;
839
840 r_res->header.id = reader[0] * 256 + reader[1];
841 reader += 2;
842
843 /* Flags and rcode are stored over 2 bytes
844 * First byte contains:
845 * - response flag (1 bit)
846 * - opcode (4 bits)
847 * - authoritative (1 bit)
848 * - truncated (1 bit)
849 * - recursion desired (1 bit)
850 */
851 if (reader + 2 >= bufend)
852 goto invalid_resp;
853
854 flags = reader[0] * 256 + reader[1];
855
856 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
857 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
858 cause = RSLV_RESP_NX_DOMAIN;
859 goto return_error;
860 }
861 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) {
862 cause = RSLV_RESP_REFUSED;
863 goto return_error;
864 }
865 else {
866 cause = RSLV_RESP_ERROR;
867 goto return_error;
868 }
869 }
870
871 /* Move forward 2 bytes for flags */
872 reader += 2;
873
874 /* 2 bytes for question count */
875 if (reader + 2 >= bufend)
876 goto invalid_resp;
877 r_res->header.qdcount = reader[0] * 256 + reader[1];
878 /* (for now) we send one query only, so we expect only one in the
879 * response too */
880 if (r_res->header.qdcount != 1) {
881 cause = RSLV_RESP_QUERY_COUNT_ERROR;
882 goto return_error;
883 }
884
885 if (r_res->header.qdcount > DNS_MAX_QUERY_RECORDS)
886 goto invalid_resp;
887 reader += 2;
888
889 /* 2 bytes for answer count */
890 if (reader + 2 >= bufend)
891 goto invalid_resp;
892 r_res->header.ancount = reader[0] * 256 + reader[1];
893 if (r_res->header.ancount == 0) {
894 cause = RSLV_RESP_ANCOUNT_ZERO;
895 goto return_error;
896 }
897
898 /* Check if too many records are announced */
899 if (r_res->header.ancount > max_answer_records)
900 goto invalid_resp;
901 reader += 2;
902
903 /* 2 bytes authority count */
904 if (reader + 2 >= bufend)
905 goto invalid_resp;
906 r_res->header.nscount = reader[0] * 256 + reader[1];
907 reader += 2;
908
909 /* 2 bytes additional count */
910 if (reader + 2 >= bufend)
911 goto invalid_resp;
912 r_res->header.arcount = reader[0] * 256 + reader[1];
913 reader += 2;
914
915 /* Parsing dns queries */
916 LIST_INIT(&r_res->query_list);
917 for (query_record_id = 0; query_record_id < r_res->header.qdcount; query_record_id++) {
918 /* Use next pre-allocated resolv_query_item after ensuring there is
919 * still one available.
920 * It's then added to our packet query list. */
921 if (query_record_id > DNS_MAX_QUERY_RECORDS)
922 goto invalid_resp;
923 query = &resolution->response_query_records[query_record_id];
Willy Tarreau2b718102021-04-21 07:32:39 +0200924 LIST_APPEND(&r_res->query_list, &query->list);
Emeric Brunc9437992021-02-12 19:42:55 +0100925
926 /* Name is a NULL terminated string in our case, since we have
927 * one query per response and the first one can't be compressed
928 * (using the 0x0c format) */
929 offset = 0;
930 len = resolv_read_name(resp, bufend, reader, query->name, DNS_MAX_NAME_SIZE, &offset, 0);
931
932 if (len == 0)
933 goto invalid_resp;
934
935 reader += offset;
936 previous_dname = query->name;
937
938 /* move forward 2 bytes for question type */
939 if (reader + 2 >= bufend)
940 goto invalid_resp;
941 query->type = reader[0] * 256 + reader[1];
942 reader += 2;
943
944 /* move forward 2 bytes for question class */
945 if (reader + 2 >= bufend)
946 goto invalid_resp;
947 query->class = reader[0] * 256 + reader[1];
948 reader += 2;
949 }
950
951 /* TRUNCATED flag must be checked after we could read the query type
952 * because a TRUNCATED SRV query type response can still be exploited */
953 if (query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) {
954 cause = RSLV_RESP_TRUNCATED;
955 goto return_error;
956 }
957
958 /* now parsing response records */
959 nb_saved_records = 0;
960 for (i = 0; i < r_res->header.ancount; i++) {
961 if (reader >= bufend)
962 goto invalid_resp;
963
964 answer_record = pool_alloc(resolv_answer_item_pool);
965 if (answer_record == NULL)
966 goto invalid_resp;
967
968 /* initialization */
969 answer_record->ar_item = NULL;
Christopher Faulet55c1c402021-03-11 09:36:05 +0100970 answer_record->last_seen = TICK_ETERNITY;
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200971 LIST_INIT(&answer_record->attached_servers);
Emeric Brunc9437992021-02-12 19:42:55 +0100972
973 offset = 0;
974 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
975
976 if (len == 0)
977 goto invalid_resp;
978
979 /* Check if the current record dname is valid. previous_dname
980 * points either to queried dname or last CNAME target */
981 if (query->type != DNS_RTYPE_SRV && resolv_hostname_cmp(previous_dname, tmpname, len) != 0) {
982 if (i == 0) {
983 /* First record, means a mismatch issue between
984 * queried dname and dname found in the first
985 * record */
986 goto invalid_resp;
987 }
988 else {
989 /* If not the first record, this means we have a
990 * CNAME resolution error.
991 */
992 cause = RSLV_RESP_CNAME_ERROR;
993 goto return_error;
994 }
995
996 }
997
998 memcpy(answer_record->name, tmpname, len);
999 answer_record->name[len] = 0;
1000
1001 reader += offset;
1002 if (reader >= bufend)
1003 goto invalid_resp;
1004
1005 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1006 if (reader + 2 > bufend)
1007 goto invalid_resp;
1008
1009 answer_record->type = reader[0] * 256 + reader[1];
1010 reader += 2;
1011
1012 /* 2 bytes for class (2) */
1013 if (reader + 2 > bufend)
1014 goto invalid_resp;
1015
1016 answer_record->class = reader[0] * 256 + reader[1];
1017 reader += 2;
1018
1019 /* 4 bytes for ttl (4) */
1020 if (reader + 4 > bufend)
1021 goto invalid_resp;
1022
1023 answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1024 + reader[2] * 256 + reader[3];
1025 reader += 4;
1026
1027 /* Now reading data len */
1028 if (reader + 2 > bufend)
1029 goto invalid_resp;
1030
1031 answer_record->data_len = reader[0] * 256 + reader[1];
1032
1033 /* Move forward 2 bytes for data len */
1034 reader += 2;
1035
1036 if (reader + answer_record->data_len > bufend)
1037 goto invalid_resp;
1038
1039 /* Analyzing record content */
1040 switch (answer_record->type) {
1041 case DNS_RTYPE_A:
1042 /* ipv4 is stored on 4 bytes */
1043 if (answer_record->data_len != 4)
1044 goto invalid_resp;
1045
1046 answer_record->address.sa_family = AF_INET;
1047 memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr),
1048 reader, answer_record->data_len);
1049 break;
1050
1051 case DNS_RTYPE_CNAME:
1052 /* Check if this is the last record and update the caller about the status:
1053 * no IP could be found and last record was a CNAME. Could be triggered
1054 * by a wrong query type
1055 *
1056 * + 1 because answer_record_id starts at 0
1057 * while number of answers is an integer and
1058 * starts at 1.
1059 */
1060 if (i + 1 == r_res->header.ancount) {
1061 cause = RSLV_RESP_CNAME_ERROR;
1062 goto return_error;
1063 }
1064
1065 offset = 0;
1066 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1067 if (len == 0)
1068 goto invalid_resp;
1069
1070 memcpy(answer_record->target, tmpname, len);
1071 answer_record->target[len] = 0;
1072 previous_dname = answer_record->target;
1073 break;
1074
1075
1076 case DNS_RTYPE_SRV:
1077 /* Answer must contain :
1078 * - 2 bytes for the priority
1079 * - 2 bytes for the weight
1080 * - 2 bytes for the port
1081 * - the target hostname
1082 */
1083 if (answer_record->data_len <= 6)
1084 goto invalid_resp;
1085
1086 answer_record->priority = read_n16(reader);
1087 reader += sizeof(uint16_t);
1088 answer_record->weight = read_n16(reader);
1089 reader += sizeof(uint16_t);
1090 answer_record->port = read_n16(reader);
1091 reader += sizeof(uint16_t);
1092 offset = 0;
1093 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1094 if (len == 0)
1095 goto invalid_resp;
1096
1097 answer_record->data_len = len;
1098 memcpy(answer_record->target, tmpname, len);
1099 answer_record->target[len] = 0;
1100 if (answer_record->ar_item != NULL) {
1101 pool_free(resolv_answer_item_pool, answer_record->ar_item);
1102 answer_record->ar_item = NULL;
1103 }
1104 break;
1105
1106 case DNS_RTYPE_AAAA:
1107 /* ipv6 is stored on 16 bytes */
1108 if (answer_record->data_len != 16)
1109 goto invalid_resp;
1110
1111 answer_record->address.sa_family = AF_INET6;
1112 memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr),
1113 reader, answer_record->data_len);
1114 break;
1115
1116 } /* switch (record type) */
1117
1118 /* Increment the counter for number of records saved into our
1119 * local response */
1120 nb_saved_records++;
1121
1122 /* Move forward answer_record->data_len for analyzing next
1123 * record in the response */
1124 reader += ((answer_record->type == DNS_RTYPE_SRV)
1125 ? offset
1126 : answer_record->data_len);
1127
1128 /* Lookup to see if we already had this entry */
1129 found = 0;
1130 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
1131 if (tmp_record->type != answer_record->type)
1132 continue;
1133
1134 switch(tmp_record->type) {
1135 case DNS_RTYPE_A:
1136 if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr,
1137 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1138 sizeof(in_addr_t)))
1139 found = 1;
1140 break;
1141
1142 case DNS_RTYPE_AAAA:
1143 if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr,
1144 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1145 sizeof(struct in6_addr)))
1146 found = 1;
1147 break;
1148
1149 case DNS_RTYPE_SRV:
1150 if (answer_record->data_len == tmp_record->data_len &&
1151 !resolv_hostname_cmp(answer_record->target, tmp_record->target, answer_record->data_len) &&
1152 answer_record->port == tmp_record->port) {
1153 tmp_record->weight = answer_record->weight;
1154 found = 1;
1155 }
1156 break;
1157
1158 default:
1159 break;
1160 }
1161
1162 if (found == 1)
1163 break;
1164 }
1165
1166 if (found == 1) {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001167 tmp_record->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001168 pool_free(resolv_answer_item_pool, answer_record);
1169 answer_record = NULL;
1170 }
1171 else {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001172 answer_record->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001173 answer_record->ar_item = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +02001174 LIST_APPEND(&r_res->answer_list, &answer_record->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001175 answer_record = NULL;
1176 }
1177 } /* for i 0 to ancount */
1178
1179 /* Save the number of records we really own */
1180 r_res->header.ancount = nb_saved_records;
1181
1182 /* now parsing additional records for SRV queries only */
1183 if (query->type != DNS_RTYPE_SRV)
1184 goto skip_parsing_additional_records;
1185
1186 /* if we find Authority records, just skip them */
1187 for (i = 0; i < r_res->header.nscount; i++) {
1188 offset = 0;
1189 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE,
1190 &offset, 0);
1191 if (len == 0)
1192 continue;
1193
1194 if (reader + offset + 10 >= bufend)
1195 goto invalid_resp;
1196
1197 reader += offset;
1198 /* skip 2 bytes for class */
1199 reader += 2;
1200 /* skip 2 bytes for type */
1201 reader += 2;
1202 /* skip 4 bytes for ttl */
1203 reader += 4;
1204 /* read data len */
1205 len = reader[0] * 256 + reader[1];
1206 reader += 2;
1207
1208 if (reader + len >= bufend)
1209 goto invalid_resp;
1210
1211 reader += len;
1212 }
1213
1214 nb_saved_records = 0;
1215 for (i = 0; i < r_res->header.arcount; i++) {
1216 if (reader >= bufend)
1217 goto invalid_resp;
1218
1219 answer_record = pool_alloc(resolv_answer_item_pool);
1220 if (answer_record == NULL)
1221 goto invalid_resp;
Christopher Faulet55c1c402021-03-11 09:36:05 +01001222 answer_record->last_seen = TICK_ETERNITY;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001223 LIST_INIT(&answer_record->attached_servers);
Emeric Brunc9437992021-02-12 19:42:55 +01001224
1225 offset = 0;
1226 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1227
1228 if (len == 0) {
1229 pool_free(resolv_answer_item_pool, answer_record);
1230 answer_record = NULL;
1231 continue;
1232 }
1233
1234 memcpy(answer_record->name, tmpname, len);
1235 answer_record->name[len] = 0;
1236
1237 reader += offset;
1238 if (reader >= bufend)
1239 goto invalid_resp;
1240
1241 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1242 if (reader + 2 > bufend)
1243 goto invalid_resp;
1244
1245 answer_record->type = reader[0] * 256 + reader[1];
1246 reader += 2;
1247
1248 /* 2 bytes for class (2) */
1249 if (reader + 2 > bufend)
1250 goto invalid_resp;
1251
1252 answer_record->class = reader[0] * 256 + reader[1];
1253 reader += 2;
1254
1255 /* 4 bytes for ttl (4) */
1256 if (reader + 4 > bufend)
1257 goto invalid_resp;
1258
1259 answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1260 + reader[2] * 256 + reader[3];
1261 reader += 4;
1262
1263 /* Now reading data len */
1264 if (reader + 2 > bufend)
1265 goto invalid_resp;
1266
1267 answer_record->data_len = reader[0] * 256 + reader[1];
1268
1269 /* Move forward 2 bytes for data len */
1270 reader += 2;
1271
1272 if (reader + answer_record->data_len > bufend)
1273 goto invalid_resp;
1274
1275 /* Analyzing record content */
1276 switch (answer_record->type) {
1277 case DNS_RTYPE_A:
1278 /* ipv4 is stored on 4 bytes */
1279 if (answer_record->data_len != 4)
1280 goto invalid_resp;
1281
1282 answer_record->address.sa_family = AF_INET;
1283 memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr),
1284 reader, answer_record->data_len);
1285 break;
1286
1287 case DNS_RTYPE_AAAA:
1288 /* ipv6 is stored on 16 bytes */
1289 if (answer_record->data_len != 16)
1290 goto invalid_resp;
1291
1292 answer_record->address.sa_family = AF_INET6;
1293 memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr),
1294 reader, answer_record->data_len);
1295 break;
1296
1297 default:
1298 pool_free(resolv_answer_item_pool, answer_record);
1299 answer_record = NULL;
1300 continue;
1301
1302 } /* switch (record type) */
1303
1304 /* Increment the counter for number of records saved into our
1305 * local response */
1306 nb_saved_records++;
1307
1308 /* Move forward answer_record->data_len for analyzing next
1309 * record in the response */
Christopher Faulet77f86062021-03-10 15:19:57 +01001310 reader += answer_record->data_len;
Emeric Brunc9437992021-02-12 19:42:55 +01001311
1312 /* Lookup to see if we already had this entry */
1313 found = 0;
1314 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
Christopher Faulet77f86062021-03-10 15:19:57 +01001315 struct resolv_answer_item *ar_item;
1316
1317 if (tmp_record->type != DNS_RTYPE_SRV || !tmp_record->ar_item)
1318 continue;
1319
1320 ar_item = tmp_record->ar_item;
Christopher Faulete8674c72021-03-12 16:42:45 +01001321 if (ar_item->type != answer_record->type || ar_item->last_seen == now_ms ||
Christopher Faulet77f86062021-03-10 15:19:57 +01001322 len != tmp_record->data_len ||
1323 resolv_hostname_cmp(answer_record->name, tmp_record->target, tmp_record->data_len))
Emeric Brunc9437992021-02-12 19:42:55 +01001324 continue;
1325
Christopher Faulet77f86062021-03-10 15:19:57 +01001326 switch(ar_item->type) {
Emeric Brunc9437992021-02-12 19:42:55 +01001327 case DNS_RTYPE_A:
1328 if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr,
Christopher Faulet77f86062021-03-10 15:19:57 +01001329 &((struct sockaddr_in *)&ar_item->address)->sin_addr,
Emeric Brunc9437992021-02-12 19:42:55 +01001330 sizeof(in_addr_t)))
1331 found = 1;
1332 break;
1333
1334 case DNS_RTYPE_AAAA:
1335 if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr,
Christopher Faulet77f86062021-03-10 15:19:57 +01001336 &((struct sockaddr_in6 *)&ar_item->address)->sin6_addr,
Emeric Brunc9437992021-02-12 19:42:55 +01001337 sizeof(struct in6_addr)))
1338 found = 1;
1339 break;
1340
1341 default:
1342 break;
1343 }
1344
1345 if (found == 1)
1346 break;
1347 }
1348
1349 if (found == 1) {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001350 tmp_record->ar_item->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001351 pool_free(resolv_answer_item_pool, answer_record);
1352 answer_record = NULL;
1353 }
1354 else {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001355 answer_record->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001356 answer_record->ar_item = NULL;
1357
1358 // looking for the SRV record in the response list linked to this additional record
1359 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
1360 if (tmp_record->type == DNS_RTYPE_SRV &&
1361 tmp_record->ar_item == NULL &&
1362 !resolv_hostname_cmp(tmp_record->target, answer_record->name, tmp_record->data_len)) {
1363 /* Always use the received additional record to refresh info */
1364 if (tmp_record->ar_item)
1365 pool_free(resolv_answer_item_pool, tmp_record->ar_item);
1366 tmp_record->ar_item = answer_record;
Christopher Faulet9c246a42021-02-23 11:59:19 +01001367 answer_record = NULL;
Emeric Brunc9437992021-02-12 19:42:55 +01001368 break;
1369 }
1370 }
Christopher Faulet9c246a42021-02-23 11:59:19 +01001371 if (answer_record) {
Emeric Brunc9437992021-02-12 19:42:55 +01001372 pool_free(resolv_answer_item_pool, answer_record);
Christopher Faulet9c246a42021-02-23 11:59:19 +01001373 answer_record = NULL;
1374 }
Emeric Brunc9437992021-02-12 19:42:55 +01001375 }
1376 } /* for i 0 to arcount */
1377
1378 skip_parsing_additional_records:
1379
1380 /* Save the number of records we really own */
1381 r_res->header.arcount = nb_saved_records;
Emeric Brunc9437992021-02-12 19:42:55 +01001382 resolv_check_response(resolution);
1383 return RSLV_RESP_VALID;
1384
1385 invalid_resp:
1386 cause = RSLV_RESP_INVALID;
1387
1388 return_error:
1389 pool_free(resolv_answer_item_pool, answer_record);
1390 return cause;
1391}
1392
1393/* Searches dn_name resolution in resp.
1394 * If existing IP not found, return the first IP matching family_priority,
1395 * otherwise, first ip found
1396 * The following tasks are the responsibility of the caller:
1397 * - <r_res> contains an error free DNS response
1398 * For both cases above, resolv_validate_dns_response is required
1399 * returns one of the RSLV_UPD_* code
1400 */
1401int resolv_get_ip_from_response(struct resolv_response *r_res,
1402 struct resolv_options *resolv_opts, void *currentip,
1403 short currentip_sin_family,
1404 void **newip, short *newip_sin_family,
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001405 struct server *owner)
Emeric Brunc9437992021-02-12 19:42:55 +01001406{
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001407 struct resolv_answer_item *record, *found_record = NULL;
Emeric Brunc9437992021-02-12 19:42:55 +01001408 int family_priority;
1409 int currentip_found;
1410 unsigned char *newip4, *newip6;
1411 int currentip_sel;
1412 int j;
1413 int score, max_score;
1414 int allowed_duplicated_ip;
1415
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001416 /* srv is linked to an alive ip record */
1417 if (owner && LIST_INLIST(&owner->ip_rec_item))
1418 return RSLV_UPD_NO;
1419
Emeric Brunc9437992021-02-12 19:42:55 +01001420 family_priority = resolv_opts->family_prio;
1421 allowed_duplicated_ip = resolv_opts->accept_duplicate_ip;
1422 *newip = newip4 = newip6 = NULL;
1423 currentip_found = 0;
1424 *newip_sin_family = AF_UNSPEC;
1425 max_score = -1;
1426
1427 /* Select an IP regarding configuration preference.
1428 * Top priority is the preferred network ip version,
1429 * second priority is the preferred network.
1430 * the last priority is the currently used IP,
1431 *
1432 * For these three priorities, a score is calculated. The
1433 * weight are:
1434 * 8 - preferred ip version.
1435 * 4 - preferred network.
1436 * 2 - if the ip in the record is not affected to any other server in the same backend (duplication)
1437 * 1 - current ip.
1438 * The result with the biggest score is returned.
1439 */
1440
1441 list_for_each_entry(record, &r_res->answer_list, list) {
1442 void *ip;
1443 unsigned char ip_type;
1444
1445 if (record->type == DNS_RTYPE_A) {
1446 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1447 ip_type = AF_INET;
1448 }
1449 else if (record->type == DNS_RTYPE_AAAA) {
1450 ip_type = AF_INET6;
1451 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
1452 }
1453 else
1454 continue;
1455 score = 0;
1456
1457 /* Check for preferred ip protocol. */
1458 if (ip_type == family_priority)
1459 score += 8;
1460
1461 /* Check for preferred network. */
1462 for (j = 0; j < resolv_opts->pref_net_nb; j++) {
1463
1464 /* Compare only the same addresses class. */
1465 if (resolv_opts->pref_net[j].family != ip_type)
1466 continue;
1467
1468 if ((ip_type == AF_INET &&
1469 in_net_ipv4(ip,
1470 &resolv_opts->pref_net[j].mask.in4,
1471 &resolv_opts->pref_net[j].addr.in4)) ||
1472 (ip_type == AF_INET6 &&
1473 in_net_ipv6(ip,
1474 &resolv_opts->pref_net[j].mask.in6,
1475 &resolv_opts->pref_net[j].addr.in6))) {
1476 score += 4;
1477 break;
1478 }
1479 }
1480
1481 /* Check if the IP found in the record is already affected to a
1482 * member of a group. If not, the score should be incremented
1483 * by 2. */
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001484 if (owner) {
1485 struct server *srv;
1486 int already_used = 0;
1487
1488 list_for_each_entry(srv, &record->attached_servers, ip_rec_item) {
1489 if (srv == owner)
1490 continue;
1491 if (srv->proxy == owner->proxy) {
1492 already_used = 1;
1493 break;
1494 }
1495 }
1496 if (already_used) {
1497 if (!allowed_duplicated_ip) {
1498 continue;
1499 }
1500 }
1501 else {
1502 score += 2;
Emeric Brunc9437992021-02-12 19:42:55 +01001503 }
1504 } else {
1505 score += 2;
1506 }
1507
1508 /* Check for current ip matching. */
1509 if (ip_type == currentip_sin_family &&
1510 ((currentip_sin_family == AF_INET &&
1511 !memcmp(ip, currentip, 4)) ||
1512 (currentip_sin_family == AF_INET6 &&
1513 !memcmp(ip, currentip, 16)))) {
1514 score++;
1515 currentip_sel = 1;
1516 }
1517 else
1518 currentip_sel = 0;
1519
1520 /* Keep the address if the score is better than the previous
1521 * score. The maximum score is 15, if this value is reached, we
1522 * break the parsing. Implicitly, this score is reached the ip
1523 * selected is the current ip. */
1524 if (score > max_score) {
1525 if (ip_type == AF_INET)
1526 newip4 = ip;
1527 else
1528 newip6 = ip;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001529 found_record = record;
Emeric Brunc9437992021-02-12 19:42:55 +01001530 currentip_found = currentip_sel;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001531 if (score == 15) {
1532 /* this was not registered on the current record but it matches
1533 * let's fix it (it may comes from state file */
1534 if (owner)
1535 LIST_APPEND(&found_record->attached_servers, &owner->ip_rec_item);
Emeric Brunc9437992021-02-12 19:42:55 +01001536 return RSLV_UPD_NO;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001537 }
Emeric Brunc9437992021-02-12 19:42:55 +01001538 max_score = score;
1539 }
1540 } /* list for each record entries */
1541
1542 /* No IP found in the response */
1543 if (!newip4 && !newip6)
1544 return RSLV_UPD_NO_IP_FOUND;
1545
1546 /* Case when the caller looks first for an IPv4 address */
1547 if (family_priority == AF_INET) {
1548 if (newip4) {
1549 *newip = newip4;
1550 *newip_sin_family = AF_INET;
1551 }
1552 else if (newip6) {
1553 *newip = newip6;
1554 *newip_sin_family = AF_INET6;
1555 }
1556 if (!currentip_found)
1557 goto not_found;
1558 }
1559 /* Case when the caller looks first for an IPv6 address */
1560 else if (family_priority == AF_INET6) {
1561 if (newip6) {
1562 *newip = newip6;
1563 *newip_sin_family = AF_INET6;
1564 }
1565 else if (newip4) {
1566 *newip = newip4;
1567 *newip_sin_family = AF_INET;
1568 }
1569 if (!currentip_found)
1570 goto not_found;
1571 }
1572 /* Case when the caller have no preference (we prefer IPv6) */
1573 else if (family_priority == AF_UNSPEC) {
1574 if (newip6) {
1575 *newip = newip6;
1576 *newip_sin_family = AF_INET6;
1577 }
1578 else if (newip4) {
1579 *newip = newip4;
1580 *newip_sin_family = AF_INET;
1581 }
1582 if (!currentip_found)
1583 goto not_found;
1584 }
1585
1586 /* No reason why we should change the server's IP address */
1587 return RSLV_UPD_NO;
1588
1589 not_found:
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001590
1591 /* the ip of this record was chosen for the server */
1592 if (owner && found_record) {
1593 LIST_APPEND(&found_record->attached_servers, &owner->ip_rec_item);
1594 }
1595
Emeric Brunc9437992021-02-12 19:42:55 +01001596 list_for_each_entry(record, &r_res->answer_list, list) {
1597 /* Move the first record to the end of the list, for internal
1598 * round robin */
Willy Tarreau2b718102021-04-21 07:32:39 +02001599 LIST_DELETE(&record->list);
1600 LIST_APPEND(&r_res->answer_list, &record->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001601 break;
1602 }
1603 return RSLV_UPD_SRVIP_NOT_FOUND;
1604}
1605
1606/* Turns a domain name label into a string.
1607 *
1608 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1609 * null byte. <str> must be allocated and its size must be passed in <str_len>.
1610 *
1611 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1612 * <str> (including the terminating null byte).
1613 */
1614int resolv_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
1615{
1616 char *ptr;
1617 int i, sz;
1618
1619 if (str_len < dn_len - 1)
1620 return -1;
1621
1622 ptr = str;
1623 for (i = 0; i < dn_len-1; ++i) {
1624 sz = dn[i];
1625 if (i)
1626 *ptr++ = '.';
1627 memcpy(ptr, dn+i+1, sz);
1628 ptr += sz;
1629 i += sz;
1630 }
1631 *ptr++ = '\0';
1632 return (ptr - str);
1633}
1634
1635/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1636 *
1637 * <str> must be a null-terminated string. <str_len> must include the
1638 * terminating null byte. <dn> buffer must be allocated and its size must be
1639 * passed in <dn_len>.
1640 *
1641 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1642 * <dn> (excluding the terminating null byte).
1643 */
1644int resolv_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
1645{
1646 int i, offset;
1647
1648 if (dn_len < str_len + 1)
1649 return -1;
1650
1651 /* First byte of dn will be used to store the length of the first
1652 * label */
1653 offset = 0;
1654 for (i = 0; i < str_len; ++i) {
1655 if (str[i] == '.') {
1656 /* 2 or more consecutive dots is invalid */
1657 if (i == offset)
1658 return -1;
1659
1660 /* ignore trailing dot */
1661 if (i + 2 == str_len) {
1662 i++;
1663 break;
1664 }
1665
1666 dn[offset] = (i - offset);
1667 offset = i+1;
1668 continue;
1669 }
1670 dn[i+1] = str[i];
1671 }
1672 dn[offset] = (i - offset - 1);
1673 dn[i] = '\0';
1674 return i;
1675}
1676
1677/* Validates host name:
1678 * - total size
1679 * - each label size individually
1680 * returns:
1681 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1682 * 1 when no error. <err> is left unaffected.
1683 */
1684int resolv_hostname_validation(const char *string, char **err)
1685{
1686 int i;
1687
1688 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1689 if (err)
1690 *err = DNS_TOO_LONG_FQDN;
1691 return 0;
1692 }
1693
1694 while (*string) {
1695 i = 0;
1696 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1697 if (!(*string == '-' || *string == '_' ||
1698 (*string >= 'a' && *string <= 'z') ||
1699 (*string >= 'A' && *string <= 'Z') ||
1700 (*string >= '0' && *string <= '9'))) {
1701 if (err)
1702 *err = DNS_INVALID_CHARACTER;
1703 return 0;
1704 }
1705 i++;
1706 string++;
1707 }
1708
1709 if (!(*string))
1710 break;
1711
1712 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
1713 if (err)
1714 *err = DNS_LABEL_TOO_LONG;
1715 return 0;
1716 }
1717
1718 string++;
1719 }
1720 return 1;
1721}
1722
1723/* Picks up an available resolution from the different resolution list
1724 * associated to a resolvers section, in this order:
1725 * 1. check in resolutions.curr for the same hostname and query_type
1726 * 2. check in resolutions.wait for the same hostname and query_type
1727 * 3. Get a new resolution from resolution pool
1728 *
1729 * Returns an available resolution, NULL if none found.
1730 */
1731static struct resolv_resolution *resolv_pick_resolution(struct resolvers *resolvers,
1732 char **hostname_dn, int hostname_dn_len,
1733 int query_type)
1734{
1735 struct resolv_resolution *res;
1736
1737 if (!*hostname_dn)
1738 goto from_pool;
1739
1740 /* Search for same hostname and query type in resolutions.curr */
1741 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1742 if (!res->hostname_dn)
1743 continue;
1744 if ((query_type == res->prefered_query_type) &&
1745 hostname_dn_len == res->hostname_dn_len &&
1746 !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1747 return res;
1748 }
1749
1750 /* Search for same hostname and query type in resolutions.wait */
1751 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1752 if (!res->hostname_dn)
1753 continue;
1754 if ((query_type == res->prefered_query_type) &&
1755 hostname_dn_len == res->hostname_dn_len &&
1756 !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1757 return res;
1758 }
1759
1760 from_pool:
1761 /* No resolution could be found, so let's allocate a new one */
Willy Tarreau70490eb2021-03-22 21:08:50 +01001762 res = pool_zalloc(resolv_resolution_pool);
Emeric Brunc9437992021-02-12 19:42:55 +01001763 if (res) {
Emeric Brunc9437992021-02-12 19:42:55 +01001764 res->resolvers = resolvers;
1765 res->uuid = resolution_uuid;
1766 res->status = RSLV_STATUS_NONE;
1767 res->step = RSLV_STEP_NONE;
1768 res->last_valid = now_ms;
1769
1770 LIST_INIT(&res->requesters);
1771 LIST_INIT(&res->response.answer_list);
1772
1773 res->prefered_query_type = query_type;
1774 res->query_type = query_type;
1775 res->hostname_dn = *hostname_dn;
1776 res->hostname_dn_len = hostname_dn_len;
1777
1778 ++resolution_uuid;
1779
1780 /* Move the resolution to the resolvers wait queue */
Willy Tarreau2b718102021-04-21 07:32:39 +02001781 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001782 }
1783 return res;
1784}
1785
Christopher Faulet1dec5c72021-03-10 14:40:39 +01001786void resolv_purge_resolution_answer_records(struct resolv_resolution *resolution)
1787{
1788 struct resolv_answer_item *item, *itemback;
1789
1790 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001791 LIST_DELETE(&item->list);
Christopher Faulet1dec5c72021-03-10 14:40:39 +01001792 pool_free(resolv_answer_item_pool, item->ar_item);
1793 pool_free(resolv_answer_item_pool, item);
1794 }
1795}
1796
Emeric Brunc9437992021-02-12 19:42:55 +01001797/* Releases a resolution from its requester(s) and move it back to the pool */
1798static void resolv_free_resolution(struct resolv_resolution *resolution)
1799{
1800 struct resolv_requester *req, *reqback;
Emeric Brunc9437992021-02-12 19:42:55 +01001801
1802 /* clean up configuration */
1803 resolv_reset_resolution(resolution);
1804 resolution->hostname_dn = NULL;
1805 resolution->hostname_dn_len = 0;
1806
1807 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001808 LIST_DELETE(&req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001809 req->resolution = NULL;
1810 }
Christopher Faulet1dec5c72021-03-10 14:40:39 +01001811 resolv_purge_resolution_answer_records(resolution);
Willy Tarreau2b718102021-04-21 07:32:39 +02001812 LIST_DELETE(&resolution->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001813 pool_free(resolv_resolution_pool, resolution);
1814}
1815
1816/* Links a requester (a server or a resolv_srvrq) with a resolution. It returns 0
1817 * on success, -1 otherwise.
1818 */
1819int resolv_link_resolution(void *requester, int requester_type, int requester_locked)
1820{
1821 struct resolv_resolution *res = NULL;
1822 struct resolv_requester *req;
1823 struct resolvers *resolvers;
1824 struct server *srv = NULL;
1825 struct resolv_srvrq *srvrq = NULL;
1826 struct stream *stream = NULL;
1827 char **hostname_dn;
1828 int hostname_dn_len, query_type;
1829
1830 switch (requester_type) {
1831 case OBJ_TYPE_SERVER:
1832 srv = (struct server *)requester;
1833 hostname_dn = &srv->hostname_dn;
1834 hostname_dn_len = srv->hostname_dn_len;
1835 resolvers = srv->resolvers;
1836 query_type = ((srv->resolv_opts.family_prio == AF_INET)
1837 ? DNS_RTYPE_A
1838 : DNS_RTYPE_AAAA);
1839 break;
1840
1841 case OBJ_TYPE_SRVRQ:
1842 srvrq = (struct resolv_srvrq *)requester;
1843 hostname_dn = &srvrq->hostname_dn;
1844 hostname_dn_len = srvrq->hostname_dn_len;
1845 resolvers = srvrq->resolvers;
1846 query_type = DNS_RTYPE_SRV;
1847 break;
1848
1849 case OBJ_TYPE_STREAM:
1850 stream = (struct stream *)requester;
1851 hostname_dn = &stream->resolv_ctx.hostname_dn;
1852 hostname_dn_len = stream->resolv_ctx.hostname_dn_len;
1853 resolvers = stream->resolv_ctx.parent->arg.resolv.resolvers;
1854 query_type = ((stream->resolv_ctx.parent->arg.resolv.opts->family_prio == AF_INET)
1855 ? DNS_RTYPE_A
1856 : DNS_RTYPE_AAAA);
1857 break;
1858 default:
1859 goto err;
1860 }
1861
1862 /* Get a resolution from the resolvers' wait queue or pool */
1863 if ((res = resolv_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1864 goto err;
1865
1866 if (srv) {
1867 if (!requester_locked)
1868 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1869 if (srv->resolv_requester == NULL) {
1870 if ((req = pool_alloc(resolv_requester_pool)) == NULL) {
1871 if (!requester_locked)
1872 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1873 goto err;
1874 }
1875 req->owner = &srv->obj_type;
1876 srv->resolv_requester = req;
1877 }
1878 else
1879 req = srv->resolv_requester;
1880 if (!requester_locked)
1881 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1882
1883 req->requester_cb = snr_resolution_cb;
1884 req->requester_error_cb = snr_resolution_error_cb;
1885 }
1886 else if (srvrq) {
1887 if (srvrq->requester == NULL) {
1888 if ((req = pool_alloc(resolv_requester_pool)) == NULL)
1889 goto err;
1890 req->owner = &srvrq->obj_type;
1891 srvrq->requester = req;
1892 }
1893 else
1894 req = srvrq->requester;
1895
1896 req->requester_cb = snr_resolution_cb;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01001897 req->requester_error_cb = srvrq_resolution_error_cb;
Emeric Brunc9437992021-02-12 19:42:55 +01001898 }
1899 else if (stream) {
1900 if (stream->resolv_ctx.requester == NULL) {
1901 if ((req = pool_alloc(resolv_requester_pool)) == NULL)
1902 goto err;
1903 req->owner = &stream->obj_type;
1904 stream->resolv_ctx.requester = req;
1905 }
1906 else
1907 req = stream->resolv_ctx.requester;
1908
1909 req->requester_cb = act_resolution_cb;
1910 req->requester_error_cb = act_resolution_error_cb;
1911 }
1912 else
1913 goto err;
1914
1915 req->resolution = res;
1916
Willy Tarreau2b718102021-04-21 07:32:39 +02001917 LIST_APPEND(&res->requesters, &req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001918 return 0;
1919
1920 err:
1921 if (res && LIST_ISEMPTY(&res->requesters))
1922 resolv_free_resolution(res);
1923 return -1;
1924}
1925
Emeric Brun0c4a8a32021-06-11 10:48:45 +02001926/* This function removes all server/srvrq references on answer items
1927 * if <safe> is set to 1, in case of srvrq, sub server resquesters unlink
1928 * is called using safe == 1 to make it usable into callbacks
1929 */
1930void resolv_detach_from_resolution_answer_items(struct resolv_resolution *res, struct resolv_requester *req, int safe)
1931{
1932 struct resolv_answer_item *item, *itemback;
1933 struct server *srv, *srvback;
1934 struct resolv_srvrq *srvrq;
1935
1936 if ((srv = objt_server(req->owner)) != NULL) {
1937 LIST_DEL_INIT(&srv->ip_rec_item);
1938 }
1939 else if ((srvrq = objt_resolv_srvrq(req->owner)) != NULL) {
1940 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
1941 if (item->type == DNS_RTYPE_SRV) {
1942 list_for_each_entry_safe(srv, srvback, &item->attached_servers, srv_rec_item) {
Christopher Faulet5f8ccff2021-06-15 16:08:48 +02001943 if (srv->srvrq == srvrq)
1944 resolv_srvrq_cleanup_srv(srv);
Emeric Brun0c4a8a32021-06-11 10:48:45 +02001945 }
1946 }
1947 }
1948 }
1949}
1950
Emeric Brunc9437992021-02-12 19:42:55 +01001951/* Removes a requester from a DNS resolution. It takes takes care of all the
1952 * consequences. It also cleans up some parameters from the requester.
Christopher Faulet0efc0992021-03-11 18:09:53 +01001953 * if <safe> is set to 1, the corresponding resolution is not released.
Emeric Brunc9437992021-02-12 19:42:55 +01001954 */
Christopher Faulet0efc0992021-03-11 18:09:53 +01001955void resolv_unlink_resolution(struct resolv_requester *requester, int safe)
Emeric Brunc9437992021-02-12 19:42:55 +01001956{
1957 struct resolv_resolution *res;
1958 struct resolv_requester *req;
1959
1960 /* Nothing to do */
1961 if (!requester || !requester->resolution)
1962 return;
1963 res = requester->resolution;
1964
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001965 /* remove ref from the resolution answer item list to the requester */
Emeric Brun0c4a8a32021-06-11 10:48:45 +02001966 resolv_detach_from_resolution_answer_items(res, requester, safe);
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001967
Emeric Brunc9437992021-02-12 19:42:55 +01001968 /* Clean up the requester */
Willy Tarreau2b718102021-04-21 07:32:39 +02001969 LIST_DELETE(&requester->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001970 requester->resolution = NULL;
1971
1972 /* We need to find another requester linked on this resolution */
1973 if (!LIST_ISEMPTY(&res->requesters))
1974 req = LIST_NEXT(&res->requesters, struct resolv_requester *, list);
1975 else {
Christopher Faulet0efc0992021-03-11 18:09:53 +01001976 if (safe) {
1977 /* Don't release it yet. */
1978 resolv_reset_resolution(res);
1979 res->hostname_dn = NULL;
1980 res->hostname_dn_len = 0;
1981 resolv_purge_resolution_answer_records(res);
1982 return;
1983 }
1984
Emeric Brunc9437992021-02-12 19:42:55 +01001985 resolv_free_resolution(res);
1986 return;
1987 }
1988
1989 /* Move hostname_dn related pointers to the next requester */
1990 switch (obj_type(req->owner)) {
1991 case OBJ_TYPE_SERVER:
1992 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1993 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
1994 break;
1995 case OBJ_TYPE_SRVRQ:
1996 res->hostname_dn = __objt_resolv_srvrq(req->owner)->hostname_dn;
1997 res->hostname_dn_len = __objt_resolv_srvrq(req->owner)->hostname_dn_len;
1998 break;
1999 case OBJ_TYPE_STREAM:
2000 res->hostname_dn = __objt_stream(req->owner)->resolv_ctx.hostname_dn;
2001 res->hostname_dn_len = __objt_stream(req->owner)->resolv_ctx.hostname_dn_len;
2002 break;
2003 default:
2004 res->hostname_dn = NULL;
2005 res->hostname_dn_len = 0;
2006 break;
2007 }
2008}
2009
2010/* Called when a network IO is generated on a name server socket for an incoming
2011 * packet. It performs the following actions:
2012 * - check if the packet requires processing (not outdated resolution)
2013 * - ensure the DNS packet received is valid and call requester's callback
2014 * - call requester's error callback if invalid response
2015 * - check the dn_name in the packet against the one sent
2016 */
2017static int resolv_process_responses(struct dns_nameserver *ns)
2018{
2019 struct dns_counters *tmpcounters;
2020 struct resolvers *resolvers;
2021 struct resolv_resolution *res;
2022 struct resolv_query_item *query;
2023 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
2024 unsigned char *bufend;
2025 int buflen, dns_resp;
2026 int max_answer_records;
2027 unsigned short query_id;
2028 struct eb32_node *eb;
2029 struct resolv_requester *req;
Emeric Brun43839d02021-06-10 15:25:25 +02002030 int keep_answer_items;
Emeric Brunc9437992021-02-12 19:42:55 +01002031
2032 resolvers = ns->parent;
2033 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2034
2035 /* process all pending input messages */
2036 while (1) {
2037 /* read message received */
2038 memset(buf, '\0', resolvers->accepted_payload_size + 1);
2039 if ((buflen = dns_recv_nameserver(ns, (void *)buf, sizeof(buf))) <= 0) {
2040 break;
2041 }
2042
2043 /* message too big */
2044 if (buflen > resolvers->accepted_payload_size) {
2045 ns->counters->too_big++;
2046 continue;
2047 }
2048
2049 /* initializing variables */
2050 bufend = buf + buflen; /* pointer to mark the end of the buffer */
2051
2052 /* read the query id from the packet (16 bits) */
2053 if (buf + 2 > bufend) {
2054 ns->counters->invalid++;
2055 continue;
2056 }
2057 query_id = resolv_response_get_query_id(buf);
2058
2059 /* search the query_id in the pending resolution tree */
2060 eb = eb32_lookup(&resolvers->query_ids, query_id);
2061 if (eb == NULL) {
2062 /* unknown query id means an outdated response and can be safely ignored */
2063 ns->counters->outdated++;
2064 continue;
2065 }
2066
2067 /* known query id means a resolution in progress */
2068 res = eb32_entry(eb, struct resolv_resolution, qid);
2069 /* number of responses received */
2070 res->nb_responses++;
2071
2072 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
2073 dns_resp = resolv_validate_dns_response(buf, bufend, res, max_answer_records);
2074
2075 switch (dns_resp) {
2076 case RSLV_RESP_VALID:
2077 break;
2078
2079 case RSLV_RESP_INVALID:
2080 case RSLV_RESP_QUERY_COUNT_ERROR:
2081 case RSLV_RESP_WRONG_NAME:
2082 res->status = RSLV_STATUS_INVALID;
2083 ns->counters->invalid++;
2084 break;
2085
2086 case RSLV_RESP_NX_DOMAIN:
2087 res->status = RSLV_STATUS_NX;
2088 ns->counters->nx++;
2089 break;
2090
2091 case RSLV_RESP_REFUSED:
2092 res->status = RSLV_STATUS_REFUSED;
2093 ns->counters->refused++;
2094 break;
2095
2096 case RSLV_RESP_ANCOUNT_ZERO:
2097 res->status = RSLV_STATUS_OTHER;
2098 ns->counters->any_err++;
2099 break;
2100
2101 case RSLV_RESP_CNAME_ERROR:
2102 res->status = RSLV_STATUS_OTHER;
2103 ns->counters->cname_error++;
2104 break;
2105
2106 case RSLV_RESP_TRUNCATED:
2107 res->status = RSLV_STATUS_OTHER;
2108 ns->counters->truncated++;
2109 break;
2110
2111 case RSLV_RESP_NO_EXPECTED_RECORD:
2112 case RSLV_RESP_ERROR:
2113 case RSLV_RESP_INTERNAL:
2114 res->status = RSLV_STATUS_OTHER;
2115 ns->counters->other++;
2116 break;
2117 }
2118
2119 /* Wait all nameservers response to handle errors */
2120 if (dns_resp != RSLV_RESP_VALID && res->nb_responses < res->nb_queries)
2121 continue;
2122
2123 /* Process error codes */
2124 if (dns_resp != RSLV_RESP_VALID) {
2125 if (res->prefered_query_type != res->query_type) {
2126 /* The fallback on the query type was already performed,
2127 * so check the try counter. If it falls to 0, we can
2128 * report an error. Else, wait the next attempt. */
2129 if (!res->try)
2130 goto report_res_error;
2131 }
2132 else {
2133 /* Fallback from A to AAAA or the opposite and re-send
2134 * the resolution immediately. try counter is not
2135 * decremented. */
2136 if (res->prefered_query_type == DNS_RTYPE_A) {
2137 res->query_type = DNS_RTYPE_AAAA;
2138 resolv_send_query(res);
2139 }
2140 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
2141 res->query_type = DNS_RTYPE_A;
2142 resolv_send_query(res);
2143 }
2144 }
2145 continue;
2146 }
2147
2148 /* Now let's check the query's dname corresponds to the one we
2149 * sent. We can check only the first query of the list. We send
2150 * one query at a time so we get one query in the response */
2151 query = LIST_NEXT(&res->response.query_list, struct resolv_query_item *, list);
2152 if (query && resolv_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
2153 dns_resp = RSLV_RESP_WRONG_NAME;
2154 ns->counters->other++;
2155 goto report_res_error;
2156 }
2157
2158 /* So the resolution succeeded */
2159 res->status = RSLV_STATUS_VALID;
2160 res->last_valid = now_ms;
2161 ns->counters->valid++;
2162 goto report_res_success;
2163
2164 report_res_error:
Emeric Brun43839d02021-06-10 15:25:25 +02002165 keep_answer_items = 0;
Emeric Brunc9437992021-02-12 19:42:55 +01002166 list_for_each_entry(req, &res->requesters, list)
Emeric Brun43839d02021-06-10 15:25:25 +02002167 keep_answer_items |= req->requester_error_cb(req, dns_resp);
2168 if (!keep_answer_items)
2169 resolv_purge_resolution_answer_records(res);
Emeric Brunc9437992021-02-12 19:42:55 +01002170 resolv_reset_resolution(res);
Willy Tarreau2b718102021-04-21 07:32:39 +02002171 LIST_DELETE(&res->list);
2172 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002173 continue;
2174
2175 report_res_success:
2176 /* Only the 1rst requester s managed by the server, others are
2177 * from the cache */
2178 tmpcounters = ns->counters;
2179 list_for_each_entry(req, &res->requesters, list) {
2180 struct server *s = objt_server(req->owner);
2181
2182 if (s)
2183 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
2184 req->requester_cb(req, tmpcounters);
2185 if (s)
2186 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2187 tmpcounters = NULL;
2188 }
2189
2190 resolv_reset_resolution(res);
Willy Tarreau2b718102021-04-21 07:32:39 +02002191 LIST_DELETE(&res->list);
2192 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002193 continue;
2194 }
2195 resolv_update_resolvers_timeout(resolvers);
2196 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2197
2198 return buflen;
2199}
2200
2201/* Processes DNS resolution. First, it checks the active list to detect expired
2202 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2203 * checks the wait list to trigger new resolutions.
2204 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002205static struct task *process_resolvers(struct task *t, void *context, unsigned int state)
Emeric Brunc9437992021-02-12 19:42:55 +01002206{
2207 struct resolvers *resolvers = context;
2208 struct resolv_resolution *res, *resback;
2209 int exp;
2210
2211 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2212
2213 /* Handle all expired resolutions from the active list */
2214 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01002215 if (LIST_ISEMPTY(&res->requesters)) {
2216 resolv_free_resolution(res);
2217 continue;
2218 }
2219
Emeric Brunc9437992021-02-12 19:42:55 +01002220 /* When we find the first resolution in the future, then we can
2221 * stop here */
2222 exp = tick_add(res->last_query, resolvers->timeout.retry);
2223 if (!tick_is_expired(exp, now_ms))
2224 break;
2225
2226 /* If current resolution has been tried too many times and
2227 * finishes in timeout we update its status and remove it from
2228 * the list */
2229 if (!res->try) {
2230 struct resolv_requester *req;
Emeric Brun43839d02021-06-10 15:25:25 +02002231 int keep_answer_items = 0;
Emeric Brunc9437992021-02-12 19:42:55 +01002232
2233 /* Notify the result to the requesters */
2234 if (!res->nb_responses)
2235 res->status = RSLV_STATUS_TIMEOUT;
2236 list_for_each_entry(req, &res->requesters, list)
Emeric Brun43839d02021-06-10 15:25:25 +02002237 keep_answer_items |= req->requester_error_cb(req, res->status);
2238 if (!keep_answer_items)
2239 resolv_purge_resolution_answer_records(res);
Emeric Brunc9437992021-02-12 19:42:55 +01002240
2241 /* Clean up resolution info and remove it from the
2242 * current list */
2243 resolv_reset_resolution(res);
Willy Tarreau2b718102021-04-21 07:32:39 +02002244 LIST_DELETE(&res->list);
2245 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002246 }
2247 else {
2248 /* Otherwise resend the DNS query and requeue the resolution */
2249 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2250 /* No response received (a real timeout) or fallback already done */
2251 res->query_type = res->prefered_query_type;
2252 res->try--;
2253 }
2254 else {
2255 /* Fallback from A to AAAA or the opposite and re-send
2256 * the resolution immediately. try counter is not
2257 * decremented. */
2258 if (res->prefered_query_type == DNS_RTYPE_A)
2259 res->query_type = DNS_RTYPE_AAAA;
2260 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2261 res->query_type = DNS_RTYPE_A;
2262 else
2263 res->try--;
2264 }
2265 resolv_send_query(res);
2266 }
2267 }
2268
2269 /* Handle all resolutions in the wait list */
2270 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01002271 if (LIST_ISEMPTY(&res->requesters)) {
2272 resolv_free_resolution(res);
2273 continue;
2274 }
2275
Emeric Brunc9437992021-02-12 19:42:55 +01002276 exp = tick_add(res->last_resolution, resolv_resolution_timeout(res));
2277 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2278 continue;
2279
2280 if (resolv_run_resolution(res) != 1) {
2281 res->last_resolution = now_ms;
Willy Tarreau2b718102021-04-21 07:32:39 +02002282 LIST_DELETE(&res->list);
2283 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002284 }
2285 }
2286
2287 resolv_update_resolvers_timeout(resolvers);
2288 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2289 return t;
2290}
2291
2292/* Release memory allocated by DNS */
2293static void resolvers_deinit(void)
2294{
2295 struct resolvers *resolvers, *resolversback;
2296 struct dns_nameserver *ns, *nsback;
2297 struct resolv_resolution *res, *resback;
2298 struct resolv_requester *req, *reqback;
2299 struct resolv_srvrq *srvrq, *srvrqback;
2300
2301 list_for_each_entry_safe(resolvers, resolversback, &sec_resolvers, list) {
2302 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2303 free(ns->id);
2304 free((char *)ns->conf.file);
2305 if (ns->dgram) {
2306 if (ns->dgram->conn.t.sock.fd != -1) {
2307 fd_delete(ns->dgram->conn.t.sock.fd);
2308 close(ns->dgram->conn.t.sock.fd);
2309 }
2310 if (ns->dgram->ring_req)
2311 ring_free(ns->dgram->ring_req);
2312 free(ns->dgram);
2313 }
Emeric Brun56fc5d92021-02-12 20:05:45 +01002314 if (ns->stream) {
2315 if (ns->stream->ring_req)
2316 ring_free(ns->stream->ring_req);
2317 if (ns->stream->task_req)
2318 task_destroy(ns->stream->task_req);
2319 if (ns->stream->task_rsp)
2320 task_destroy(ns->stream->task_rsp);
2321 free(ns->stream);
2322 }
Willy Tarreau2b718102021-04-21 07:32:39 +02002323 LIST_DELETE(&ns->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002324 EXTRA_COUNTERS_FREE(ns->extra_counters);
2325 free(ns);
2326 }
2327
2328 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2329 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002330 LIST_DELETE(&req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002331 pool_free(resolv_requester_pool, req);
2332 }
2333 resolv_free_resolution(res);
2334 }
2335
2336 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2337 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002338 LIST_DELETE(&req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002339 pool_free(resolv_requester_pool, req);
2340 }
2341 resolv_free_resolution(res);
2342 }
2343
2344 free(resolvers->id);
2345 free((char *)resolvers->conf.file);
2346 task_destroy(resolvers->t);
Willy Tarreau2b718102021-04-21 07:32:39 +02002347 LIST_DELETE(&resolvers->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002348 free(resolvers);
2349 }
2350
2351 list_for_each_entry_safe(srvrq, srvrqback, &resolv_srvrq_list, list) {
2352 free(srvrq->name);
2353 free(srvrq->hostname_dn);
Willy Tarreau2b718102021-04-21 07:32:39 +02002354 LIST_DELETE(&srvrq->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002355 free(srvrq);
2356 }
2357}
2358
2359/* Finalizes the DNS configuration by allocating required resources and checking
2360 * live parameters.
2361 * Returns 0 on success, ERR_* flags otherwise.
2362 */
2363static int resolvers_finalize_config(void)
2364{
2365 struct resolvers *resolvers;
2366 struct proxy *px;
2367 int err_code = 0;
2368
2369 /* allocate pool of resolution per resolvers */
2370 list_for_each_entry(resolvers, &sec_resolvers, list) {
2371 struct dns_nameserver *ns;
2372 struct task *t;
2373
2374 /* Check if we can create the socket with nameservers info */
2375 list_for_each_entry(ns, &resolvers->nameservers, list) {
2376 int fd;
2377
2378 if (ns->dgram) {
2379 /* Check nameserver info */
2380 if ((fd = socket(ns->dgram->conn.addr.to.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
2381 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2382 resolvers->id, ns->id);
2383 err_code |= (ERR_ALERT|ERR_ABORT);
2384 continue;
2385 }
2386 if (connect(fd, (struct sockaddr*)&ns->dgram->conn.addr.to, get_addr_len(&ns->dgram->conn.addr.to)) == -1) {
2387 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2388 resolvers->id, ns->id);
2389 close(fd);
2390 err_code |= (ERR_ALERT|ERR_ABORT);
2391 continue;
2392 }
2393 close(fd);
2394 }
2395 }
2396
2397 /* Create the task associated to the resolvers section */
2398 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
2399 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
2400 err_code |= (ERR_ALERT|ERR_ABORT);
2401 goto err;
2402 }
2403
2404 /* Update task's parameters */
2405 t->process = process_resolvers;
2406 t->context = resolvers;
2407 resolvers->t = t;
2408 task_wakeup(t, TASK_WOKEN_INIT);
2409 }
2410
2411 for (px = proxies_list; px; px = px->next) {
2412 struct server *srv;
2413
2414 for (srv = px->srv; srv; srv = srv->next) {
2415 struct resolvers *resolvers;
2416
2417 if (!srv->resolvers_id)
2418 continue;
2419
2420 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
2421 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2422 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
2423 err_code |= (ERR_ALERT|ERR_ABORT);
2424 continue;
2425 }
2426 srv->resolvers = resolvers;
2427
2428 if (srv->srvrq && !srv->srvrq->resolvers) {
2429 srv->srvrq->resolvers = srv->resolvers;
2430 if (resolv_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
2431 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2432 proxy_type_str(px), px->id, srv->id);
2433 err_code |= (ERR_ALERT|ERR_ABORT);
2434 continue;
2435 }
2436 }
Christopher Fauletd83a6df2021-03-12 10:23:05 +01002437 if (!srv->srvrq && resolv_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Emeric Brunc9437992021-02-12 19:42:55 +01002438 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2439 proxy_type_str(px), px->id, srv->id);
2440 err_code |= (ERR_ALERT|ERR_ABORT);
2441 continue;
2442 }
2443 }
2444 }
2445
2446 if (err_code & (ERR_ALERT|ERR_ABORT))
2447 goto err;
2448
2449 return err_code;
2450 err:
2451 resolvers_deinit();
2452 return err_code;
2453
2454}
2455
2456static int stats_dump_resolv_to_buffer(struct stream_interface *si,
2457 struct dns_nameserver *ns,
2458 struct field *stats, size_t stats_count,
2459 struct list *stat_modules)
2460{
2461 struct appctx *appctx = __objt_appctx(si->end);
2462 struct channel *rep = si_ic(si);
2463 struct stats_module *mod;
2464 size_t idx = 0;
2465
2466 memset(stats, 0, sizeof(struct field) * stats_count);
2467
2468 list_for_each_entry(mod, stat_modules, list) {
2469 struct counters_node *counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2470
2471 mod->fill_stats(counters, stats + idx);
2472 idx += mod->stats_count;
2473 }
2474
2475 if (!stats_dump_one_line(stats, idx, appctx))
2476 return 0;
2477
2478 if (!stats_putchk(rep, NULL, &trash))
2479 goto full;
2480
2481 return 1;
2482
2483 full:
2484 si_rx_room_rdy(si);
2485 return 0;
2486}
2487
2488/* Uses <appctx.ctx.stats.obj1> as a pointer to the current resolver and <obj2>
2489 * as a pointer to the current nameserver.
2490 */
2491int stats_dump_resolvers(struct stream_interface *si,
2492 struct field *stats, size_t stats_count,
2493 struct list *stat_modules)
2494{
2495 struct appctx *appctx = __objt_appctx(si->end);
2496 struct channel *rep = si_ic(si);
2497 struct resolvers *resolver = appctx->ctx.stats.obj1;
2498 struct dns_nameserver *ns = appctx->ctx.stats.obj2;
2499
2500 if (!resolver)
2501 resolver = LIST_NEXT(&sec_resolvers, struct resolvers *, list);
2502
2503 /* dump resolvers */
2504 list_for_each_entry_from(resolver, &sec_resolvers, list) {
2505 appctx->ctx.stats.obj1 = resolver;
2506
2507 ns = appctx->ctx.stats.obj2 ?
2508 appctx->ctx.stats.obj2 :
2509 LIST_NEXT(&resolver->nameservers, struct dns_nameserver *, list);
2510
2511 list_for_each_entry_from(ns, &resolver->nameservers, list) {
2512 appctx->ctx.stats.obj2 = ns;
2513
2514 if (buffer_almost_full(&rep->buf))
2515 goto full;
2516
2517 if (!stats_dump_resolv_to_buffer(si, ns,
2518 stats, stats_count,
2519 stat_modules)) {
2520 return 0;
2521 }
2522 }
2523
2524 appctx->ctx.stats.obj2 = NULL;
2525 }
2526
2527 return 1;
2528
2529 full:
2530 si_rx_room_blk(si);
2531 return 0;
2532}
2533
2534void resolv_stats_clear_counters(int clrall, struct list *stat_modules)
2535{
2536 struct resolvers *resolvers;
2537 struct dns_nameserver *ns;
2538 struct stats_module *mod;
2539 void *counters;
2540
2541 list_for_each_entry(mod, stat_modules, list) {
2542 if (!mod->clearable && !clrall)
2543 continue;
2544
2545 list_for_each_entry(resolvers, &sec_resolvers, list) {
2546 list_for_each_entry(ns, &resolvers->nameservers, list) {
2547 counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2548 memcpy(counters, mod->counters, mod->counters_size);
2549 }
2550 }
2551 }
2552
2553}
2554
2555int resolv_allocate_counters(struct list *stat_modules)
2556{
2557 struct stats_module *mod;
2558 struct resolvers *resolvers;
2559 struct dns_nameserver *ns;
2560
2561 list_for_each_entry(resolvers, &sec_resolvers, list) {
2562 list_for_each_entry(ns, &resolvers->nameservers, list) {
2563 EXTRA_COUNTERS_REGISTER(&ns->extra_counters, COUNTERS_DNS,
2564 alloc_failed);
2565
2566 list_for_each_entry(mod, stat_modules, list) {
2567 EXTRA_COUNTERS_ADD(mod,
2568 ns->extra_counters,
2569 mod->counters,
2570 mod->counters_size);
2571 }
2572
2573 EXTRA_COUNTERS_ALLOC(ns->extra_counters, alloc_failed);
2574
2575 list_for_each_entry(mod, stat_modules, list) {
2576 memcpy(ns->extra_counters->data + mod->counters_off[ns->extra_counters->type],
2577 mod->counters, mod->counters_size);
2578
2579 /* Store the ns counters pointer */
2580 if (strcmp(mod->name, "dns") == 0) {
2581 ns->counters = (struct dns_counters *)ns->extra_counters->data + mod->counters_off[COUNTERS_DNS];
2582 ns->counters->id = ns->id;
2583 ns->counters->pid = resolvers->id;
2584 }
2585 }
2586 }
2587 }
2588
2589 return 1;
2590
2591alloc_failed:
2592 return 0;
2593}
2594
2595/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
2596static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
2597{
2598 struct resolvers *presolvers;
2599
2600 if (*args[2]) {
2601 list_for_each_entry(presolvers, &sec_resolvers, list) {
2602 if (strcmp(presolvers->id, args[2]) == 0) {
2603 appctx->ctx.cli.p0 = presolvers;
2604 break;
2605 }
2606 }
2607 if (appctx->ctx.cli.p0 == NULL)
2608 return cli_err(appctx, "Can't find that resolvers section\n");
2609 }
2610 return 0;
2611}
2612
2613/* Dumps counters from all resolvers section and associated name servers. It
2614 * returns 0 if the output buffer is full and it needs to be called again,
2615 * otherwise non-zero. It may limit itself to the resolver pointed to by
2616 * <cli.p0> if it's not null.
2617 */
2618static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2619{
2620 struct stream_interface *si = appctx->owner;
2621 struct resolvers *resolvers;
2622 struct dns_nameserver *ns;
2623
2624 chunk_reset(&trash);
2625
2626 switch (appctx->st2) {
2627 case STAT_ST_INIT:
2628 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2629 /* fall through */
2630
2631 case STAT_ST_LIST:
2632 if (LIST_ISEMPTY(&sec_resolvers)) {
2633 chunk_appendf(&trash, "No resolvers found\n");
2634 }
2635 else {
2636 list_for_each_entry(resolvers, &sec_resolvers, list) {
2637 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
2638 continue;
2639
2640 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2641 list_for_each_entry(ns, &resolvers->nameservers, list) {
2642 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2643 chunk_appendf(&trash, " sent: %lld\n", ns->counters->sent);
2644 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters->snd_error);
2645 chunk_appendf(&trash, " valid: %lld\n", ns->counters->valid);
2646 chunk_appendf(&trash, " update: %lld\n", ns->counters->update);
2647 chunk_appendf(&trash, " cname: %lld\n", ns->counters->cname);
2648 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters->cname_error);
2649 chunk_appendf(&trash, " any_err: %lld\n", ns->counters->any_err);
2650 chunk_appendf(&trash, " nx: %lld\n", ns->counters->nx);
2651 chunk_appendf(&trash, " timeout: %lld\n", ns->counters->timeout);
2652 chunk_appendf(&trash, " refused: %lld\n", ns->counters->refused);
2653 chunk_appendf(&trash, " other: %lld\n", ns->counters->other);
2654 chunk_appendf(&trash, " invalid: %lld\n", ns->counters->invalid);
2655 chunk_appendf(&trash, " too_big: %lld\n", ns->counters->too_big);
2656 chunk_appendf(&trash, " truncated: %lld\n", ns->counters->truncated);
2657 chunk_appendf(&trash, " outdated: %lld\n", ns->counters->outdated);
2658 }
2659 chunk_appendf(&trash, "\n");
2660 }
2661 }
2662
2663 /* display response */
2664 if (ci_putchk(si_ic(si), &trash) == -1) {
2665 /* let's try again later from this session. We add ourselves into
2666 * this session's users so that it can remove us upon termination.
2667 */
2668 si_rx_room_blk(si);
2669 return 0;
2670 }
2671 /* fall through */
2672
2673 default:
2674 appctx->st2 = STAT_ST_FIN;
2675 return 1;
2676 }
2677}
2678
2679/* register cli keywords */
2680static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02002681 { { "show", "resolvers", NULL }, "show resolvers [id] : dumps counters from all resolvers section and associated name servers",
Emeric Brunc9437992021-02-12 19:42:55 +01002682 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2683 {{},}
2684 }
2685};
2686
2687INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
2688
2689/*
2690 * Prepare <rule> for hostname resolution.
2691 * Returns -1 in case of any allocation failure, 0 if not.
2692 * On error, a global failure counter is also incremented.
2693 */
2694static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2695{
2696 char *hostname_dn;
2697 int hostname_len, hostname_dn_len;
2698 struct buffer *tmp = get_trash_chunk();
2699
2700 if (!hostname)
2701 return 0;
2702
2703 hostname_len = strlen(hostname);
2704 hostname_dn = tmp->area;
2705 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
2706 hostname_dn, tmp->size);
2707 if (hostname_dn_len == -1)
2708 goto err;
2709
2710
2711 stream->resolv_ctx.hostname_dn = strdup(hostname_dn);
2712 stream->resolv_ctx.hostname_dn_len = hostname_dn_len;
2713 if (!stream->resolv_ctx.hostname_dn)
2714 goto err;
2715
2716 return 0;
2717
2718 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002719 ha_free(&stream->resolv_ctx.hostname_dn);
Emeric Brunc9437992021-02-12 19:42:55 +01002720 resolv_failed_resolutions += 1;
2721 return -1;
2722}
2723
2724
2725/*
2726 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2727 */
2728enum act_return resolv_action_do_resolve(struct act_rule *rule, struct proxy *px,
2729 struct session *sess, struct stream *s, int flags)
2730{
2731 struct resolv_resolution *resolution;
2732 struct sample *smp;
2733 char *fqdn;
2734 struct resolv_requester *req;
2735 struct resolvers *resolvers;
2736 struct resolv_resolution *res;
2737 int exp, locked = 0;
2738 enum act_return ret = ACT_RET_CONT;
2739
2740 resolvers = rule->arg.resolv.resolvers;
2741
2742 /* we have a response to our DNS resolution */
2743 use_cache:
2744 if (s->resolv_ctx.requester && s->resolv_ctx.requester->resolution != NULL) {
2745 resolution = s->resolv_ctx.requester->resolution;
2746 if (!locked) {
2747 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2748 locked = 1;
2749 }
2750
2751 if (resolution->step == RSLV_STEP_RUNNING)
2752 goto yield;
2753 if (resolution->step == RSLV_STEP_NONE) {
2754 /* We update the variable only if we have a valid response. */
2755 if (resolution->status == RSLV_STATUS_VALID) {
2756 struct sample smp;
2757 short ip_sin_family = 0;
2758 void *ip = NULL;
2759
2760 resolv_get_ip_from_response(&resolution->response, rule->arg.resolv.opts, NULL,
2761 0, &ip, &ip_sin_family, NULL);
2762
2763 switch (ip_sin_family) {
2764 case AF_INET:
2765 smp.data.type = SMP_T_IPV4;
2766 memcpy(&smp.data.u.ipv4, ip, 4);
2767 break;
2768 case AF_INET6:
2769 smp.data.type = SMP_T_IPV6;
2770 memcpy(&smp.data.u.ipv6, ip, 16);
2771 break;
2772 default:
2773 ip = NULL;
2774 }
2775
2776 if (ip) {
2777 smp.px = px;
2778 smp.sess = sess;
2779 smp.strm = s;
2780
2781 vars_set_by_name(rule->arg.resolv.varname, strlen(rule->arg.resolv.varname), &smp);
2782 }
2783 }
2784 }
2785
2786 goto release_requester;
2787 }
2788
2789 /* need to configure and start a new DNS resolution */
2790 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.resolv.expr, SMP_T_STR);
2791 if (smp == NULL)
2792 goto end;
2793
2794 fqdn = smp->data.u.str.area;
2795 if (action_prepare_for_resolution(s, fqdn) == -1)
2796 goto end; /* on error, ignore the action */
2797
2798 s->resolv_ctx.parent = rule;
2799
2800 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2801 locked = 1;
2802
2803 resolv_link_resolution(s, OBJ_TYPE_STREAM, 0);
2804
2805 /* Check if there is a fresh enough response in the cache of our associated resolution */
2806 req = s->resolv_ctx.requester;
2807 if (!req || !req->resolution)
2808 goto release_requester; /* on error, ignore the action */
2809 res = req->resolution;
2810
2811 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2812 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2813 && !tick_is_expired(exp, now_ms)) {
2814 goto use_cache;
2815 }
2816
2817 resolv_trigger_resolution(s->resolv_ctx.requester);
2818
2819 yield:
2820 if (flags & ACT_OPT_FINAL)
2821 goto release_requester;
2822 ret = ACT_RET_YIELD;
2823
2824 end:
2825 if (locked)
2826 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2827 return ret;
2828
2829 release_requester:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002830 ha_free(&s->resolv_ctx.hostname_dn);
Emeric Brunc9437992021-02-12 19:42:55 +01002831 s->resolv_ctx.hostname_dn_len = 0;
2832 if (s->resolv_ctx.requester) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01002833 resolv_unlink_resolution(s->resolv_ctx.requester, 0);
Emeric Brunc9437992021-02-12 19:42:55 +01002834 pool_free(resolv_requester_pool, s->resolv_ctx.requester);
2835 s->resolv_ctx.requester = NULL;
2836 }
2837 goto end;
2838}
2839
2840static void release_resolv_action(struct act_rule *rule)
2841{
2842 release_sample_expr(rule->arg.resolv.expr);
2843 free(rule->arg.resolv.varname);
2844 free(rule->arg.resolv.resolvers_id);
2845 free(rule->arg.resolv.opts);
2846}
2847
2848
2849/* parse "do-resolve" action
2850 * This action takes the following arguments:
2851 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2852 *
2853 * - <varName> is the variable name where the result of the DNS resolution will be stored
2854 * (mandatory)
2855 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2856 * (mandatory)
2857 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2858 * (optional), defaults to ipv6
2859 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2860 */
2861enum act_parse_ret resolv_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2862{
2863 int cur_arg;
2864 struct sample_expr *expr;
2865 unsigned int where;
2866 const char *beg, *end;
2867
2868 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2869 cur_arg = *orig_arg - 1;
2870
2871 /* locate varName, which is mandatory */
2872 beg = strchr(args[cur_arg], '(');
2873 if (beg == NULL)
2874 goto do_resolve_parse_error;
2875 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2876 end = strchr(beg, ',');
2877 if (end == NULL)
2878 goto do_resolve_parse_error;
2879 rule->arg.resolv.varname = my_strndup(beg, end - beg);
2880 if (rule->arg.resolv.varname == NULL)
2881 goto do_resolve_parse_error;
2882
2883
2884 /* locate resolversSectionName, which is mandatory.
2885 * Since next parameters are optional, the delimiter may be comma ','
2886 * or closing parenthesis ')'
2887 */
2888 beg = end + 1;
2889 end = strchr(beg, ',');
2890 if (end == NULL)
2891 end = strchr(beg, ')');
2892 if (end == NULL)
2893 goto do_resolve_parse_error;
2894 rule->arg.resolv.resolvers_id = my_strndup(beg, end - beg);
2895 if (rule->arg.resolv.resolvers_id == NULL)
2896 goto do_resolve_parse_error;
2897
2898
2899 rule->arg.resolv.opts = calloc(1, sizeof(*rule->arg.resolv.opts));
2900 if (rule->arg.resolv.opts == NULL)
2901 goto do_resolve_parse_error;
2902
2903 /* Default priority is ipv6 */
2904 rule->arg.resolv.opts->family_prio = AF_INET6;
2905
2906 /* optional arguments accepted for now:
2907 * ipv4 or ipv6
2908 */
2909 while (*end != ')') {
2910 beg = end + 1;
2911 end = strchr(beg, ',');
2912 if (end == NULL)
2913 end = strchr(beg, ')');
2914 if (end == NULL)
2915 goto do_resolve_parse_error;
2916
2917 if (strncmp(beg, "ipv4", end - beg) == 0) {
2918 rule->arg.resolv.opts->family_prio = AF_INET;
2919 }
2920 else if (strncmp(beg, "ipv6", end - beg) == 0) {
2921 rule->arg.resolv.opts->family_prio = AF_INET6;
2922 }
2923 else {
2924 goto do_resolve_parse_error;
2925 }
2926 }
2927
2928 cur_arg = cur_arg + 1;
2929
2930 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
2931 if (!expr)
2932 goto do_resolve_parse_error;
2933
2934
2935 where = 0;
2936 if (px->cap & PR_CAP_FE)
2937 where |= SMP_VAL_FE_HRQ_HDR;
2938 if (px->cap & PR_CAP_BE)
2939 where |= SMP_VAL_BE_HRQ_HDR;
2940
2941 if (!(expr->fetch->val & where)) {
2942 memprintf(err,
2943 "fetch method '%s' extracts information from '%s', none of which is available here",
2944 args[cur_arg-1], sample_src_names(expr->fetch->use));
2945 free(expr);
2946 return ACT_RET_PRS_ERR;
2947 }
2948 rule->arg.resolv.expr = expr;
2949 rule->action = ACT_CUSTOM;
2950 rule->action_ptr = resolv_action_do_resolve;
2951 *orig_arg = cur_arg;
2952
2953 rule->check_ptr = check_action_do_resolve;
2954 rule->release_ptr = release_resolv_action;
2955
2956 return ACT_RET_PRS_OK;
2957
2958 do_resolve_parse_error:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002959 ha_free(&rule->arg.resolv.varname);
2960 ha_free(&rule->arg.resolv.resolvers_id);
Emeric Brunc9437992021-02-12 19:42:55 +01002961 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2962 args[cur_arg]);
2963 return ACT_RET_PRS_ERR;
2964}
2965
2966static struct action_kw_list http_req_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002967 { "do-resolve", resolv_parse_do_resolve, KWF_MATCH_PREFIX },
Emeric Brunc9437992021-02-12 19:42:55 +01002968 { /* END */ }
2969}};
2970
2971INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2972
2973static struct action_kw_list tcp_req_cont_actions = {ILH, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002974 { "do-resolve", resolv_parse_do_resolve, KWF_MATCH_PREFIX },
Emeric Brunc9437992021-02-12 19:42:55 +01002975 { /* END */ }
2976}};
2977
2978INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2979
2980/* Check an "http-request do-resolve" action.
2981 *
2982 * The function returns 1 in success case, otherwise, it returns 0 and err is
2983 * filled.
2984 */
2985int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2986{
2987 struct resolvers *resolvers = NULL;
2988
2989 if (rule->arg.resolv.resolvers_id == NULL) {
2990 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2991 return 0;
2992 }
2993
2994 resolvers = find_resolvers_by_id(rule->arg.resolv.resolvers_id);
2995 if (resolvers == NULL) {
2996 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.resolv.resolvers_id);
2997 return 0;
2998 }
2999 rule->arg.resolv.resolvers = resolvers;
3000
3001 return 1;
3002}
3003
3004void resolvers_setup_proxy(struct proxy *px)
3005{
3006 px->last_change = now.tv_sec;
3007 px->cap = PR_CAP_FE | PR_CAP_BE;
3008 px->maxconn = 0;
3009 px->conn_retries = 1;
3010 px->timeout.server = TICK_ETERNITY;
3011 px->timeout.client = TICK_ETERNITY;
3012 px->timeout.connect = TICK_ETERNITY;
3013 px->accept = NULL;
3014 px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON;
3015 px->bind_proc = 0; /* will be filled by users */
3016}
3017
3018/*
3019 * Parse a <resolvers> section.
3020 * Returns the error code, 0 if OK, or any combination of :
3021 * - ERR_ABORT: must abort ASAP
3022 * - ERR_FATAL: we can continue parsing but not start the service
3023 * - ERR_WARN: a warning has been emitted
3024 * - ERR_ALERT: an alert has been emitted
3025 * Only the two first ones can stop processing, the two others are just
3026 * indicators.
3027 */
3028int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
3029{
3030 const char *err;
3031 int err_code = 0;
3032 char *errmsg = NULL;
3033 struct proxy *p;
3034
3035 if (strcmp(args[0], "resolvers") == 0) { /* new resolvers section */
3036 if (!*args[1]) {
3037 ha_alert("parsing [%s:%d] : missing name for resolvers section.\n", file, linenum);
3038 err_code |= ERR_ALERT | ERR_ABORT;
3039 goto out;
3040 }
3041
3042 err = invalid_char(args[1]);
3043 if (err) {
3044 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3045 file, linenum, *err, args[0], args[1]);
3046 err_code |= ERR_ALERT | ERR_ABORT;
3047 goto out;
3048 }
3049
3050 list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
3051 /* Error if two resolvers owns the same name */
3052 if (strcmp(curr_resolvers->id, args[1]) == 0) {
3053 ha_alert("Parsing [%s:%d]: resolvers '%s' has same name as another resolvers (declared at %s:%d).\n",
3054 file, linenum, args[1], curr_resolvers->conf.file, curr_resolvers->conf.line);
3055 err_code |= ERR_ALERT | ERR_ABORT;
3056 }
3057 }
3058
3059 if ((curr_resolvers = calloc(1, sizeof(*curr_resolvers))) == NULL) {
3060 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3061 err_code |= ERR_ALERT | ERR_ABORT;
3062 goto out;
3063 }
3064
3065 /* allocate new proxy to tcp servers */
3066 p = calloc(1, sizeof *p);
3067 if (!p) {
3068 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3069 err_code |= ERR_ALERT | ERR_FATAL;
3070 goto out;
3071 }
3072
3073 init_new_proxy(p);
3074 resolvers_setup_proxy(p);
3075 p->parent = curr_resolvers;
3076 p->id = strdup(args[1]);
3077 p->conf.args.file = p->conf.file = strdup(file);
3078 p->conf.args.line = p->conf.line = linenum;
3079 curr_resolvers->px = p;
3080
3081 /* default values */
Willy Tarreau2b718102021-04-21 07:32:39 +02003082 LIST_APPEND(&sec_resolvers, &curr_resolvers->list);
Emeric Brunc9437992021-02-12 19:42:55 +01003083 curr_resolvers->conf.file = strdup(file);
3084 curr_resolvers->conf.line = linenum;
3085 curr_resolvers->id = strdup(args[1]);
3086 curr_resolvers->query_ids = EB_ROOT;
3087 /* default maximum response size */
3088 curr_resolvers->accepted_payload_size = 512;
3089 /* default hold period for nx, other, refuse and timeout is 30s */
3090 curr_resolvers->hold.nx = 30000;
3091 curr_resolvers->hold.other = 30000;
3092 curr_resolvers->hold.refused = 30000;
3093 curr_resolvers->hold.timeout = 30000;
3094 curr_resolvers->hold.obsolete = 0;
3095 /* default hold period for valid is 10s */
3096 curr_resolvers->hold.valid = 10000;
3097 curr_resolvers->timeout.resolve = 1000;
3098 curr_resolvers->timeout.retry = 1000;
3099 curr_resolvers->resolve_retries = 3;
3100 LIST_INIT(&curr_resolvers->nameservers);
3101 LIST_INIT(&curr_resolvers->resolutions.curr);
3102 LIST_INIT(&curr_resolvers->resolutions.wait);
3103 HA_SPIN_INIT(&curr_resolvers->lock);
3104 }
3105 else if (strcmp(args[0], "nameserver") == 0) { /* nameserver definition */
3106 struct dns_nameserver *newnameserver = NULL;
3107 struct sockaddr_storage *sk;
3108 int port1, port2;
Emeric Brunc8f3e452021-04-07 16:04:54 +02003109 struct protocol *proto;
Emeric Brunc9437992021-02-12 19:42:55 +01003110
3111 if (!*args[2]) {
3112 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
3113 file, linenum, args[0]);
3114 err_code |= ERR_ALERT | ERR_FATAL;
3115 goto out;
3116 }
3117
3118 err = invalid_char(args[1]);
3119 if (err) {
3120 ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
3121 file, linenum, *err, args[1]);
3122 err_code |= ERR_ALERT | ERR_FATAL;
3123 goto out;
3124 }
3125
3126 list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) {
3127 /* Error if two resolvers owns the same name */
3128 if (strcmp(newnameserver->id, args[1]) == 0) {
3129 ha_alert("Parsing [%s:%d]: nameserver '%s' has same name as another nameserver (declared at %s:%d).\n",
3130 file, linenum, args[1], newnameserver->conf.file, newnameserver->conf.line);
3131 err_code |= ERR_ALERT | ERR_FATAL;
3132 }
3133 }
3134
Emeric Brunc8f3e452021-04-07 16:04:54 +02003135 sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto,
3136 &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
Emeric Brunc9437992021-02-12 19:42:55 +01003137 if (!sk) {
3138 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3139 err_code |= ERR_ALERT | ERR_FATAL;
3140 goto out;
3141 }
3142
3143 if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) {
3144 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3145 err_code |= ERR_ALERT | ERR_ABORT;
3146 goto out;
3147 }
3148
Emeric Brunc8f3e452021-04-07 16:04:54 +02003149 if (proto && proto->ctrl_type == SOCK_STREAM) {
3150 err_code |= parse_server(file, linenum, args, curr_resolvers->px, NULL,
3151 SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
3152 if (err_code & (ERR_FATAL|ERR_ABORT)) {
3153 err_code |= ERR_ABORT;
3154 goto out;
3155 }
3156
3157 if (dns_stream_init(newnameserver, curr_resolvers->px->srv) < 0) {
3158 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3159 err_code |= ERR_ALERT|ERR_ABORT;
3160 goto out;
3161 }
3162 }
3163 else if (dns_dgram_init(newnameserver, sk) < 0) {
Emeric Brunc9437992021-02-12 19:42:55 +01003164 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3165 err_code |= ERR_ALERT | ERR_ABORT;
3166 goto out;
3167 }
3168
3169 if ((newnameserver->conf.file = strdup(file)) == NULL) {
3170 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3171 err_code |= ERR_ALERT | ERR_ABORT;
3172 goto out;
3173 }
3174
3175 if ((newnameserver->id = strdup(args[1])) == NULL) {
3176 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3177 err_code |= ERR_ALERT | ERR_ABORT;
3178 goto out;
3179 }
3180
3181 newnameserver->parent = curr_resolvers;
3182 newnameserver->process_responses = resolv_process_responses;
3183 newnameserver->conf.line = linenum;
3184 /* the nameservers are linked backward first */
Willy Tarreau2b718102021-04-21 07:32:39 +02003185 LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list);
Emeric Brunc9437992021-02-12 19:42:55 +01003186 }
3187 else if (strcmp(args[0], "parse-resolv-conf") == 0) {
3188 struct dns_nameserver *newnameserver = NULL;
3189 const char *whitespace = "\r\n\t ";
3190 char *resolv_line = NULL;
3191 int resolv_linenum = 0;
3192 FILE *f = NULL;
3193 char *address = NULL;
3194 struct sockaddr_storage *sk = NULL;
3195 struct protocol *proto;
3196 int duplicate_name = 0;
3197
3198 if ((resolv_line = malloc(sizeof(*resolv_line) * LINESIZE)) == NULL) {
3199 ha_alert("parsing [%s:%d] : out of memory.\n",
3200 file, linenum);
3201 err_code |= ERR_ALERT | ERR_FATAL;
3202 goto resolv_out;
3203 }
3204
3205 if ((f = fopen("/etc/resolv.conf", "r")) == NULL) {
3206 ha_alert("parsing [%s:%d] : failed to open /etc/resolv.conf.\n",
3207 file, linenum);
3208 err_code |= ERR_ALERT | ERR_FATAL;
3209 goto resolv_out;
3210 }
3211
3212 sk = calloc(1, sizeof(*sk));
3213 if (sk == NULL) {
3214 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n",
3215 resolv_linenum);
3216 err_code |= ERR_ALERT | ERR_FATAL;
3217 goto resolv_out;
3218 }
3219
3220 while (fgets(resolv_line, LINESIZE, f) != NULL) {
3221 resolv_linenum++;
3222 if (strncmp(resolv_line, "nameserver", 10) != 0)
3223 continue;
3224
3225 address = strtok(resolv_line + 10, whitespace);
3226 if (address == resolv_line + 10)
3227 continue;
3228
3229 if (address == NULL) {
3230 ha_warning("parsing [/etc/resolv.conf:%d] : nameserver line is missing address.\n",
3231 resolv_linenum);
3232 err_code |= ERR_WARN;
3233 continue;
3234 }
3235
3236 duplicate_name = 0;
3237 list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) {
3238 if (strcmp(newnameserver->id, address) == 0) {
3239 ha_warning("Parsing [/etc/resolv.conf:%d] : generated name for /etc/resolv.conf nameserver '%s' conflicts with another nameserver (declared at %s:%d), it appears to be a duplicate and will be excluded.\n",
3240 resolv_linenum, address, newnameserver->conf.file, newnameserver->conf.line);
3241 err_code |= ERR_WARN;
3242 duplicate_name = 1;
3243 }
3244 }
3245
3246 if (duplicate_name)
3247 continue;
3248
3249 memset(sk, 0, sizeof(*sk));
3250 if (!str2ip2(address, sk, 1)) {
3251 ha_warning("parsing [/etc/resolv.conf:%d] : address '%s' could not be recognized, nameserver will be excluded.\n",
3252 resolv_linenum, address);
3253 err_code |= ERR_WARN;
3254 continue;
3255 }
3256
3257 set_host_port(sk, 53);
3258
3259 proto = protocol_by_family(sk->ss_family);
3260 if (!proto || !proto->connect) {
3261 ha_warning("parsing [/etc/resolv.conf:%d] : '%s' : connect() not supported for this address family.\n",
3262 resolv_linenum, address);
3263 err_code |= ERR_WARN;
3264 continue;
3265 }
3266
3267 if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) {
3268 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3269 err_code |= ERR_ALERT | ERR_FATAL;
3270 goto resolv_out;
3271 }
3272
3273 if (dns_dgram_init(newnameserver, sk) < 0) {
3274 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3275 err_code |= ERR_ALERT | ERR_FATAL;
3276 free(newnameserver);
3277 goto resolv_out;
3278 }
3279
3280 newnameserver->conf.file = strdup("/etc/resolv.conf");
3281 if (newnameserver->conf.file == NULL) {
3282 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3283 err_code |= ERR_ALERT | ERR_FATAL;
3284 free(newnameserver);
3285 goto resolv_out;
3286 }
3287
3288 newnameserver->id = strdup(address);
3289 if (newnameserver->id == NULL) {
3290 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3291 err_code |= ERR_ALERT | ERR_FATAL;
3292 free((char *)newnameserver->conf.file);
3293 free(newnameserver);
3294 goto resolv_out;
3295 }
3296
3297 newnameserver->parent = curr_resolvers;
3298 newnameserver->process_responses = resolv_process_responses;
3299 newnameserver->conf.line = resolv_linenum;
Willy Tarreau2b718102021-04-21 07:32:39 +02003300 LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list);
Emeric Brunc9437992021-02-12 19:42:55 +01003301 }
3302
3303resolv_out:
3304 free(sk);
3305 free(resolv_line);
3306 if (f != NULL)
3307 fclose(f);
3308 }
3309 else if (strcmp(args[0], "hold") == 0) { /* hold periods */
3310 const char *res;
3311 unsigned int time;
3312
3313 if (!*args[2]) {
3314 ha_alert("parsing [%s:%d] : '%s' expects an <event> and a <time> as arguments.\n",
3315 file, linenum, args[0]);
3316 ha_alert("<event> can be either 'valid', 'nx', 'refused', 'timeout', or 'other'\n");
3317 err_code |= ERR_ALERT | ERR_FATAL;
3318 goto out;
3319 }
3320 res = parse_time_err(args[2], &time, TIME_UNIT_MS);
3321 if (res == PARSE_TIME_OVER) {
3322 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
3323 file, linenum, args[1], args[0]);
3324 err_code |= ERR_ALERT | ERR_FATAL;
3325 goto out;
3326 }
3327 else if (res == PARSE_TIME_UNDER) {
3328 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
3329 file, linenum, args[1], args[0]);
3330 err_code |= ERR_ALERT | ERR_FATAL;
3331 goto out;
3332 }
3333 else if (res) {
3334 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s>.\n",
3335 file, linenum, *res, args[0]);
3336 err_code |= ERR_ALERT | ERR_FATAL;
3337 goto out;
3338 }
3339 if (strcmp(args[1], "nx") == 0)
3340 curr_resolvers->hold.nx = time;
3341 else if (strcmp(args[1], "other") == 0)
3342 curr_resolvers->hold.other = time;
3343 else if (strcmp(args[1], "refused") == 0)
3344 curr_resolvers->hold.refused = time;
3345 else if (strcmp(args[1], "timeout") == 0)
3346 curr_resolvers->hold.timeout = time;
3347 else if (strcmp(args[1], "valid") == 0)
3348 curr_resolvers->hold.valid = time;
3349 else if (strcmp(args[1], "obsolete") == 0)
3350 curr_resolvers->hold.obsolete = time;
3351 else {
3352 ha_alert("parsing [%s:%d] : '%s' unknown <event>: '%s', expects either 'nx', 'timeout', 'valid', 'obsolete' or 'other'.\n",
3353 file, linenum, args[0], args[1]);
3354 err_code |= ERR_ALERT | ERR_FATAL;
3355 goto out;
3356 }
3357
3358 }
3359 else if (strcmp(args[0], "accepted_payload_size") == 0) {
3360 int i = 0;
3361
3362 if (!*args[1]) {
3363 ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n",
3364 file, linenum, args[0]);
3365 err_code |= ERR_ALERT | ERR_FATAL;
3366 goto out;
3367 }
3368
3369 i = atoi(args[1]);
3370 if (i < DNS_HEADER_SIZE || i > DNS_MAX_UDP_MESSAGE) {
3371 ha_alert("parsing [%s:%d] : '%s' must be between %d and %d inclusive (was %s).\n",
3372 file, linenum, args[0], DNS_HEADER_SIZE, DNS_MAX_UDP_MESSAGE, args[1]);
3373 err_code |= ERR_ALERT | ERR_FATAL;
3374 goto out;
3375 }
3376
3377 curr_resolvers->accepted_payload_size = i;
3378 }
3379 else if (strcmp(args[0], "resolution_pool_size") == 0) {
3380 ha_alert("parsing [%s:%d] : '%s' directive is not supported anymore (it never appeared in a stable release).\n",
3381 file, linenum, args[0]);
3382 err_code |= ERR_ALERT | ERR_FATAL;
3383 goto out;
3384 }
3385 else if (strcmp(args[0], "resolve_retries") == 0) {
3386 if (!*args[1]) {
3387 ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n",
3388 file, linenum, args[0]);
3389 err_code |= ERR_ALERT | ERR_FATAL;
3390 goto out;
3391 }
3392 curr_resolvers->resolve_retries = atoi(args[1]);
3393 }
3394 else if (strcmp(args[0], "timeout") == 0) {
3395 if (!*args[1]) {
3396 ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments.\n",
3397 file, linenum, args[0]);
3398 err_code |= ERR_ALERT | ERR_FATAL;
3399 goto out;
3400 }
3401 else if (strcmp(args[1], "retry") == 0 ||
3402 strcmp(args[1], "resolve") == 0) {
3403 const char *res;
3404 unsigned int tout;
3405
3406 if (!*args[2]) {
3407 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
3408 file, linenum, args[0], args[1]);
3409 err_code |= ERR_ALERT | ERR_FATAL;
3410 goto out;
3411 }
3412 res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
3413 if (res == PARSE_TIME_OVER) {
3414 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3415 file, linenum, args[2], args[0], args[1]);
3416 err_code |= ERR_ALERT | ERR_FATAL;
3417 goto out;
3418 }
3419 else if (res == PARSE_TIME_UNDER) {
3420 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3421 file, linenum, args[2], args[0], args[1]);
3422 err_code |= ERR_ALERT | ERR_FATAL;
3423 goto out;
3424 }
3425 else if (res) {
3426 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
3427 file, linenum, *res, args[0], args[1]);
3428 err_code |= ERR_ALERT | ERR_FATAL;
3429 goto out;
3430 }
3431 if (args[1][2] == 't')
3432 curr_resolvers->timeout.retry = tout;
3433 else
3434 curr_resolvers->timeout.resolve = tout;
3435 }
3436 else {
3437 ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments got '%s'.\n",
3438 file, linenum, args[0], args[1]);
3439 err_code |= ERR_ALERT | ERR_FATAL;
3440 goto out;
3441 }
3442 }
3443 else if (*args[0] != 0) {
3444 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
3445 err_code |= ERR_ALERT | ERR_FATAL;
3446 goto out;
3447 }
3448
3449 out:
3450 free(errmsg);
3451 return err_code;
3452}
Emeric Brun56fc5d92021-02-12 20:05:45 +01003453int cfg_post_parse_resolvers()
3454{
3455 int err_code = 0;
3456 struct server *srv;
3457
3458 if (curr_resolvers) {
3459
3460 /* prepare forward server descriptors */
3461 if (curr_resolvers->px) {
3462 srv = curr_resolvers->px->srv;
3463 while (srv) {
Emeric Brun56fc5d92021-02-12 20:05:45 +01003464 /* init ssl if needed */
3465 if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
3466 if (xprt_get(XPRT_SSL)->prepare_srv(srv)) {
3467 ha_alert("unable to prepare SSL for server '%s' in resolvers section '%s'.\n", srv->id, curr_resolvers->id);
3468 err_code |= ERR_ALERT | ERR_FATAL;
3469 break;
3470 }
3471 }
Emeric Brun56fc5d92021-02-12 20:05:45 +01003472 srv = srv->next;
3473 }
3474 }
3475 }
3476 curr_resolvers = NULL;
3477 return err_code;
3478}
Emeric Brunc9437992021-02-12 19:42:55 +01003479
Emeric Brun56fc5d92021-02-12 20:05:45 +01003480REGISTER_CONFIG_SECTION("resolvers", cfg_parse_resolvers, cfg_post_parse_resolvers);
Emeric Brunc9437992021-02-12 19:42:55 +01003481REGISTER_POST_DEINIT(resolvers_deinit);
3482REGISTER_CONFIG_POSTPARSER("dns runtime resolver", resolvers_finalize_config);