blob: 402f03f4be27efa8be8718da3dd2cfd588ddaa59 [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;
Christopher Fauleta62d9742021-06-15 16:14:37 +0200591
592 ebpt_delete(&srv->host_dn);
593 ha_free(&srv->host_dn.key);
594
Christopher Faulet5f8ccff2021-06-15 16:08:48 +0200595 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
596 LIST_DELETE(&srv->srv_rec_item);
597 LIST_APPEND(&srv->srvrq->attached_servers, &srv->srv_rec_item);
598}
599
Emeric Brunc9437992021-02-12 19:42:55 +0100600/* Checks for any obsolete record, also identify any SRV request, and try to
601 * find a corresponding server.
602*/
603static void resolv_check_response(struct resolv_resolution *res)
604{
605 struct resolvers *resolvers = res->resolvers;
Christopher Fauletdb31b442021-03-11 18:19:41 +0100606 struct resolv_requester *req;
Emeric Brunc9437992021-02-12 19:42:55 +0100607 struct resolv_answer_item *item, *itemback;
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200608 struct server *srv, *srvback;
Emeric Brunc9437992021-02-12 19:42:55 +0100609 struct resolv_srvrq *srvrq;
610
611 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
612 struct resolv_answer_item *ar_item = item->ar_item;
613
614 /* clean up obsolete Additional record */
Christopher Faulet55c1c402021-03-11 09:36:05 +0100615 if (ar_item && tick_is_lt(tick_add(ar_item->last_seen, resolvers->hold.obsolete), now_ms)) {
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100616 /* Cleaning up the AR item will trigger an extra DNS resolution, except if the SRV
617 * item is also obsolete.
618 */
Emeric Brunc9437992021-02-12 19:42:55 +0100619 pool_free(resolv_answer_item_pool, ar_item);
620 item->ar_item = NULL;
621 }
622
623 /* Remove obsolete items */
Christopher Faulet55c1c402021-03-11 09:36:05 +0100624 if (tick_is_lt(tick_add(item->last_seen, resolvers->hold.obsolete), now_ms)) {
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200625 if (item->type == DNS_RTYPE_A || item->type == DNS_RTYPE_AAAA) {
Emeric Brunc9437992021-02-12 19:42:55 +0100626 /* Remove any associated server */
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200627 list_for_each_entry_safe(srv, srvback, &item->attached_servers, ip_rec_item) {
628 LIST_DEL_INIT(&srv->ip_rec_item);
629 }
630 }
631 else if (item->type == DNS_RTYPE_SRV) {
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200632 /* Remove any associated server */
Christopher Faulet5f8ccff2021-06-15 16:08:48 +0200633 list_for_each_entry_safe(srv, srvback, &item->attached_servers, srv_rec_item)
634 resolv_srvrq_cleanup_srv(srv);
Emeric Brunc9437992021-02-12 19:42:55 +0100635 }
636
Willy Tarreau2b718102021-04-21 07:32:39 +0200637 LIST_DELETE(&item->list);
Emeric Brunc9437992021-02-12 19:42:55 +0100638 if (item->ar_item) {
639 pool_free(resolv_answer_item_pool, item->ar_item);
640 item->ar_item = NULL;
641 }
642 pool_free(resolv_answer_item_pool, item);
643 continue;
644 }
645
646 if (item->type != DNS_RTYPE_SRV)
647 continue;
648
649 /* Now process SRV records */
Christopher Fauletdb31b442021-03-11 18:19:41 +0100650 list_for_each_entry(req, &res->requesters, list) {
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200651 struct ebpt_node *node;
652 char target[DNS_MAX_NAME_SIZE+1];
653
654 int i;
Emeric Brunc9437992021-02-12 19:42:55 +0100655 if ((srvrq = objt_resolv_srvrq(req->owner)) == NULL)
656 continue;
657
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200658 /* Check if a server already uses that record */
659 srv = NULL;
660 list_for_each_entry(srv, &item->attached_servers, srv_rec_item) {
661 if (srv->srvrq == srvrq) {
662 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
663 goto srv_found;
Emeric Brunc9437992021-02-12 19:42:55 +0100664 }
Emeric Brunc9437992021-02-12 19:42:55 +0100665 }
666
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200667
668 /* If not empty we try to match a server
669 * in server state file tree with the same hostname
670 */
671 if (!eb_is_empty(&srvrq->named_servers)) {
672 srv = NULL;
673
674 /* convert the key to lookup in lower case */
675 for (i = 0 ; item->target[i] ; i++)
676 target[i] = tolower(item->target[i]);
677
678 node = ebis_lookup(&srvrq->named_servers, target);
679 if (node) {
680 srv = ebpt_entry(node, struct server, host_dn);
Emeric Brunc9437992021-02-12 19:42:55 +0100681 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200682
683 /* an entry was found with the same hostname
684 * let check this node if the port matches
685 * and try next node if the hostname
686 * is still the same
687 */
688 while (1) {
689 if (srv->svc_port == item->port) {
690 /* server found, we remove it from tree */
691 ebpt_delete(node);
Christopher Fauleta62d9742021-06-15 16:14:37 +0200692 ha_free(&srv->host_dn.key);
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200693 goto srv_found;
694 }
695
696 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
697
698 node = ebpt_next(node);
699 if (!node)
700 break;
701
702 srv = ebpt_entry(node, struct server, host_dn);
703 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
704
705 if ((item->data_len != srv->hostname_dn_len)
706 || resolv_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
707 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
708 break;
709 }
710 }
Emeric Brunc9437992021-02-12 19:42:55 +0100711 }
712 }
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200713
714 /* Pick the first server listed in srvrq (those ones don't
715 * have hostname and are free to use)
716 */
717 srv = NULL;
718 list_for_each_entry(srv, &srvrq->attached_servers, srv_rec_item) {
719 LIST_DEL_INIT(&srv->srv_rec_item);
720 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
721 goto srv_found;
722 }
723 srv = NULL;
Emeric Brunc9437992021-02-12 19:42:55 +0100724
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200725srv_found:
Emeric Brunc9437992021-02-12 19:42:55 +0100726 /* And update this server, if found (srv is locked here) */
727 if (srv) {
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100728 /* re-enable DNS resolution for this server by default */
729 srv->flags &= ~SRV_F_NO_RESOLUTION;
730
Emeric Brunc9437992021-02-12 19:42:55 +0100731 /* Check if an Additional Record is associated to this SRV record.
732 * Perform some sanity checks too to ensure the record can be used.
733 * If all fine, we simply pick up the IP address found and associate
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100734 * it to the server. And DNS resolution is disabled for this server.
Emeric Brunc9437992021-02-12 19:42:55 +0100735 */
736 if ((item->ar_item != NULL) &&
737 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100738 {
Emeric Brunc9437992021-02-12 19:42:55 +0100739
740 switch (item->ar_item->type) {
741 case DNS_RTYPE_A:
Christopher Faulet69beaa92021-02-16 12:07:47 +0100742 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 +0100743 break;
744 case DNS_RTYPE_AAAA:
Christopher Faulet69beaa92021-02-16 12:07:47 +0100745 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 +0100746 break;
747 }
748
749 srv->flags |= SRV_F_NO_RESOLUTION;
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100750
751 /* Unlink A/AAAA resolution for this server if there is an AR item.
752 * It is usless to perform an extra resolution
753 */
Christopher Faulet0efc0992021-03-11 18:09:53 +0100754 resolv_unlink_resolution(srv->resolv_requester, 0);
Emeric Brunc9437992021-02-12 19:42:55 +0100755 }
756
757 if (!srv->hostname_dn) {
758 const char *msg = NULL;
759 char hostname[DNS_MAX_NAME_SIZE];
760
761 if (resolv_dn_label_to_str(item->target, item->data_len+1,
762 hostname, DNS_MAX_NAME_SIZE) == -1) {
763 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
764 continue;
765 }
Christopher Faulet69beaa92021-02-16 12:07:47 +0100766 msg = srv_update_fqdn(srv, hostname, "SRV record", 1);
Emeric Brunc9437992021-02-12 19:42:55 +0100767 if (msg)
768 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
769 }
770
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200771 if (!LIST_INLIST(&srv->srv_rec_item))
772 LIST_APPEND(&item->attached_servers, &srv->srv_rec_item);
773
Christopher Faulet3e0600f2021-03-10 18:38:37 +0100774 if (!(srv->flags & SRV_F_NO_RESOLUTION)) {
775 /* If there is no AR item responsible of the FQDN resolution,
776 * trigger a dedicated DNS resolution
777 */
778 if (!srv->resolv_requester || !srv->resolv_requester->resolution)
779 resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1);
780 }
781
Christopher Fauletab177ac2021-03-10 15:34:52 +0100782 /* Update the server status */
Christopher Faulet6b117ae2021-03-11 18:06:23 +0100783 srvrq_update_srv_status(srv, (srv->addr.ss_family != AF_INET && srv->addr.ss_family != AF_INET6));
Emeric Brunc9437992021-02-12 19:42:55 +0100784
785 srv->svc_port = item->port;
786 srv->flags &= ~SRV_F_MAPPORTS;
787
788 if (!srv->resolv_opts.ignore_weight) {
789 char weight[9];
790 int ha_weight;
791
792 /* DNS weight range if from 0 to 65535
793 * HAProxy weight is from 0 to 256
794 * The rule below ensures that weight 0 is well respected
795 * while allowing a "mapping" from DNS weight into HAProxy's one.
796 */
797 ha_weight = (item->weight + 255) / 256;
798
799 snprintf(weight, sizeof(weight), "%d", ha_weight);
800 server_parse_weight_change_request(srv, weight);
801 }
802 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
803 }
804 }
805 }
806}
807
808/* Validates that the buffer DNS response provided in <resp> and finishing
809 * before <bufend> is valid from a DNS protocol point of view.
810 *
811 * The result is stored in <resolution>' response, buf_response,
812 * response_query_records and response_answer_records members.
813 *
814 * This function returns one of the RSLV_RESP_* code to indicate the type of
815 * error found.
816 */
817static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufend,
818 struct resolv_resolution *resolution, int max_answer_records)
819{
820 unsigned char *reader;
821 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
822 int len, flags, offset;
823 int query_record_id;
824 int nb_saved_records;
825 struct resolv_query_item *query;
826 struct resolv_answer_item *answer_record, *tmp_record;
827 struct resolv_response *r_res;
828 int i, found = 0;
829 int cause = RSLV_RESP_ERROR;
830
831 reader = resp;
832 len = 0;
833 previous_dname = NULL;
834 query = NULL;
835 answer_record = NULL;
836
837 /* Initialization of response buffer and structure */
838 r_res = &resolution->response;
839
840 /* query id */
841 if (reader + 2 >= bufend)
842 goto invalid_resp;
843
844 r_res->header.id = reader[0] * 256 + reader[1];
845 reader += 2;
846
847 /* Flags and rcode are stored over 2 bytes
848 * First byte contains:
849 * - response flag (1 bit)
850 * - opcode (4 bits)
851 * - authoritative (1 bit)
852 * - truncated (1 bit)
853 * - recursion desired (1 bit)
854 */
855 if (reader + 2 >= bufend)
856 goto invalid_resp;
857
858 flags = reader[0] * 256 + reader[1];
859
860 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
861 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
862 cause = RSLV_RESP_NX_DOMAIN;
863 goto return_error;
864 }
865 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) {
866 cause = RSLV_RESP_REFUSED;
867 goto return_error;
868 }
869 else {
870 cause = RSLV_RESP_ERROR;
871 goto return_error;
872 }
873 }
874
875 /* Move forward 2 bytes for flags */
876 reader += 2;
877
878 /* 2 bytes for question count */
879 if (reader + 2 >= bufend)
880 goto invalid_resp;
881 r_res->header.qdcount = reader[0] * 256 + reader[1];
882 /* (for now) we send one query only, so we expect only one in the
883 * response too */
884 if (r_res->header.qdcount != 1) {
885 cause = RSLV_RESP_QUERY_COUNT_ERROR;
886 goto return_error;
887 }
888
889 if (r_res->header.qdcount > DNS_MAX_QUERY_RECORDS)
890 goto invalid_resp;
891 reader += 2;
892
893 /* 2 bytes for answer count */
894 if (reader + 2 >= bufend)
895 goto invalid_resp;
896 r_res->header.ancount = reader[0] * 256 + reader[1];
897 if (r_res->header.ancount == 0) {
898 cause = RSLV_RESP_ANCOUNT_ZERO;
899 goto return_error;
900 }
901
902 /* Check if too many records are announced */
903 if (r_res->header.ancount > max_answer_records)
904 goto invalid_resp;
905 reader += 2;
906
907 /* 2 bytes authority count */
908 if (reader + 2 >= bufend)
909 goto invalid_resp;
910 r_res->header.nscount = reader[0] * 256 + reader[1];
911 reader += 2;
912
913 /* 2 bytes additional count */
914 if (reader + 2 >= bufend)
915 goto invalid_resp;
916 r_res->header.arcount = reader[0] * 256 + reader[1];
917 reader += 2;
918
919 /* Parsing dns queries */
920 LIST_INIT(&r_res->query_list);
921 for (query_record_id = 0; query_record_id < r_res->header.qdcount; query_record_id++) {
922 /* Use next pre-allocated resolv_query_item after ensuring there is
923 * still one available.
924 * It's then added to our packet query list. */
925 if (query_record_id > DNS_MAX_QUERY_RECORDS)
926 goto invalid_resp;
927 query = &resolution->response_query_records[query_record_id];
Willy Tarreau2b718102021-04-21 07:32:39 +0200928 LIST_APPEND(&r_res->query_list, &query->list);
Emeric Brunc9437992021-02-12 19:42:55 +0100929
930 /* Name is a NULL terminated string in our case, since we have
931 * one query per response and the first one can't be compressed
932 * (using the 0x0c format) */
933 offset = 0;
934 len = resolv_read_name(resp, bufend, reader, query->name, DNS_MAX_NAME_SIZE, &offset, 0);
935
936 if (len == 0)
937 goto invalid_resp;
938
939 reader += offset;
940 previous_dname = query->name;
941
942 /* move forward 2 bytes for question type */
943 if (reader + 2 >= bufend)
944 goto invalid_resp;
945 query->type = reader[0] * 256 + reader[1];
946 reader += 2;
947
948 /* move forward 2 bytes for question class */
949 if (reader + 2 >= bufend)
950 goto invalid_resp;
951 query->class = reader[0] * 256 + reader[1];
952 reader += 2;
953 }
954
955 /* TRUNCATED flag must be checked after we could read the query type
956 * because a TRUNCATED SRV query type response can still be exploited */
957 if (query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) {
958 cause = RSLV_RESP_TRUNCATED;
959 goto return_error;
960 }
961
962 /* now parsing response records */
963 nb_saved_records = 0;
964 for (i = 0; i < r_res->header.ancount; i++) {
965 if (reader >= bufend)
966 goto invalid_resp;
967
968 answer_record = pool_alloc(resolv_answer_item_pool);
969 if (answer_record == NULL)
970 goto invalid_resp;
971
972 /* initialization */
973 answer_record->ar_item = NULL;
Christopher Faulet55c1c402021-03-11 09:36:05 +0100974 answer_record->last_seen = TICK_ETERNITY;
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200975 LIST_INIT(&answer_record->attached_servers);
Emeric Brunc9437992021-02-12 19:42:55 +0100976
977 offset = 0;
978 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
979
980 if (len == 0)
981 goto invalid_resp;
982
983 /* Check if the current record dname is valid. previous_dname
984 * points either to queried dname or last CNAME target */
985 if (query->type != DNS_RTYPE_SRV && resolv_hostname_cmp(previous_dname, tmpname, len) != 0) {
986 if (i == 0) {
987 /* First record, means a mismatch issue between
988 * queried dname and dname found in the first
989 * record */
990 goto invalid_resp;
991 }
992 else {
993 /* If not the first record, this means we have a
994 * CNAME resolution error.
995 */
996 cause = RSLV_RESP_CNAME_ERROR;
997 goto return_error;
998 }
999
1000 }
1001
1002 memcpy(answer_record->name, tmpname, len);
1003 answer_record->name[len] = 0;
1004
1005 reader += offset;
1006 if (reader >= bufend)
1007 goto invalid_resp;
1008
1009 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1010 if (reader + 2 > bufend)
1011 goto invalid_resp;
1012
1013 answer_record->type = reader[0] * 256 + reader[1];
1014 reader += 2;
1015
1016 /* 2 bytes for class (2) */
1017 if (reader + 2 > bufend)
1018 goto invalid_resp;
1019
1020 answer_record->class = reader[0] * 256 + reader[1];
1021 reader += 2;
1022
1023 /* 4 bytes for ttl (4) */
1024 if (reader + 4 > bufend)
1025 goto invalid_resp;
1026
1027 answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1028 + reader[2] * 256 + reader[3];
1029 reader += 4;
1030
1031 /* Now reading data len */
1032 if (reader + 2 > bufend)
1033 goto invalid_resp;
1034
1035 answer_record->data_len = reader[0] * 256 + reader[1];
1036
1037 /* Move forward 2 bytes for data len */
1038 reader += 2;
1039
1040 if (reader + answer_record->data_len > bufend)
1041 goto invalid_resp;
1042
1043 /* Analyzing record content */
1044 switch (answer_record->type) {
1045 case DNS_RTYPE_A:
1046 /* ipv4 is stored on 4 bytes */
1047 if (answer_record->data_len != 4)
1048 goto invalid_resp;
1049
1050 answer_record->address.sa_family = AF_INET;
1051 memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr),
1052 reader, answer_record->data_len);
1053 break;
1054
1055 case DNS_RTYPE_CNAME:
1056 /* Check if this is the last record and update the caller about the status:
1057 * no IP could be found and last record was a CNAME. Could be triggered
1058 * by a wrong query type
1059 *
1060 * + 1 because answer_record_id starts at 0
1061 * while number of answers is an integer and
1062 * starts at 1.
1063 */
1064 if (i + 1 == r_res->header.ancount) {
1065 cause = RSLV_RESP_CNAME_ERROR;
1066 goto return_error;
1067 }
1068
1069 offset = 0;
1070 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1071 if (len == 0)
1072 goto invalid_resp;
1073
1074 memcpy(answer_record->target, tmpname, len);
1075 answer_record->target[len] = 0;
1076 previous_dname = answer_record->target;
1077 break;
1078
1079
1080 case DNS_RTYPE_SRV:
1081 /* Answer must contain :
1082 * - 2 bytes for the priority
1083 * - 2 bytes for the weight
1084 * - 2 bytes for the port
1085 * - the target hostname
1086 */
1087 if (answer_record->data_len <= 6)
1088 goto invalid_resp;
1089
1090 answer_record->priority = read_n16(reader);
1091 reader += sizeof(uint16_t);
1092 answer_record->weight = read_n16(reader);
1093 reader += sizeof(uint16_t);
1094 answer_record->port = read_n16(reader);
1095 reader += sizeof(uint16_t);
1096 offset = 0;
1097 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1098 if (len == 0)
1099 goto invalid_resp;
1100
1101 answer_record->data_len = len;
1102 memcpy(answer_record->target, tmpname, len);
1103 answer_record->target[len] = 0;
1104 if (answer_record->ar_item != NULL) {
1105 pool_free(resolv_answer_item_pool, answer_record->ar_item);
1106 answer_record->ar_item = NULL;
1107 }
1108 break;
1109
1110 case DNS_RTYPE_AAAA:
1111 /* ipv6 is stored on 16 bytes */
1112 if (answer_record->data_len != 16)
1113 goto invalid_resp;
1114
1115 answer_record->address.sa_family = AF_INET6;
1116 memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr),
1117 reader, answer_record->data_len);
1118 break;
1119
1120 } /* switch (record type) */
1121
1122 /* Increment the counter for number of records saved into our
1123 * local response */
1124 nb_saved_records++;
1125
1126 /* Move forward answer_record->data_len for analyzing next
1127 * record in the response */
1128 reader += ((answer_record->type == DNS_RTYPE_SRV)
1129 ? offset
1130 : answer_record->data_len);
1131
1132 /* Lookup to see if we already had this entry */
1133 found = 0;
1134 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
1135 if (tmp_record->type != answer_record->type)
1136 continue;
1137
1138 switch(tmp_record->type) {
1139 case DNS_RTYPE_A:
1140 if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr,
1141 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1142 sizeof(in_addr_t)))
1143 found = 1;
1144 break;
1145
1146 case DNS_RTYPE_AAAA:
1147 if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr,
1148 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1149 sizeof(struct in6_addr)))
1150 found = 1;
1151 break;
1152
1153 case DNS_RTYPE_SRV:
1154 if (answer_record->data_len == tmp_record->data_len &&
1155 !resolv_hostname_cmp(answer_record->target, tmp_record->target, answer_record->data_len) &&
1156 answer_record->port == tmp_record->port) {
1157 tmp_record->weight = answer_record->weight;
1158 found = 1;
1159 }
1160 break;
1161
1162 default:
1163 break;
1164 }
1165
1166 if (found == 1)
1167 break;
1168 }
1169
1170 if (found == 1) {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001171 tmp_record->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001172 pool_free(resolv_answer_item_pool, answer_record);
1173 answer_record = NULL;
1174 }
1175 else {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001176 answer_record->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001177 answer_record->ar_item = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +02001178 LIST_APPEND(&r_res->answer_list, &answer_record->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001179 answer_record = NULL;
1180 }
1181 } /* for i 0 to ancount */
1182
1183 /* Save the number of records we really own */
1184 r_res->header.ancount = nb_saved_records;
1185
1186 /* now parsing additional records for SRV queries only */
1187 if (query->type != DNS_RTYPE_SRV)
1188 goto skip_parsing_additional_records;
1189
1190 /* if we find Authority records, just skip them */
1191 for (i = 0; i < r_res->header.nscount; i++) {
1192 offset = 0;
1193 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE,
1194 &offset, 0);
1195 if (len == 0)
1196 continue;
1197
1198 if (reader + offset + 10 >= bufend)
1199 goto invalid_resp;
1200
1201 reader += offset;
1202 /* skip 2 bytes for class */
1203 reader += 2;
1204 /* skip 2 bytes for type */
1205 reader += 2;
1206 /* skip 4 bytes for ttl */
1207 reader += 4;
1208 /* read data len */
1209 len = reader[0] * 256 + reader[1];
1210 reader += 2;
1211
1212 if (reader + len >= bufend)
1213 goto invalid_resp;
1214
1215 reader += len;
1216 }
1217
1218 nb_saved_records = 0;
1219 for (i = 0; i < r_res->header.arcount; i++) {
1220 if (reader >= bufend)
1221 goto invalid_resp;
1222
1223 answer_record = pool_alloc(resolv_answer_item_pool);
1224 if (answer_record == NULL)
1225 goto invalid_resp;
Christopher Faulet55c1c402021-03-11 09:36:05 +01001226 answer_record->last_seen = TICK_ETERNITY;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001227 LIST_INIT(&answer_record->attached_servers);
Emeric Brunc9437992021-02-12 19:42:55 +01001228
1229 offset = 0;
1230 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1231
1232 if (len == 0) {
1233 pool_free(resolv_answer_item_pool, answer_record);
1234 answer_record = NULL;
1235 continue;
1236 }
1237
1238 memcpy(answer_record->name, tmpname, len);
1239 answer_record->name[len] = 0;
1240
1241 reader += offset;
1242 if (reader >= bufend)
1243 goto invalid_resp;
1244
1245 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1246 if (reader + 2 > bufend)
1247 goto invalid_resp;
1248
1249 answer_record->type = reader[0] * 256 + reader[1];
1250 reader += 2;
1251
1252 /* 2 bytes for class (2) */
1253 if (reader + 2 > bufend)
1254 goto invalid_resp;
1255
1256 answer_record->class = reader[0] * 256 + reader[1];
1257 reader += 2;
1258
1259 /* 4 bytes for ttl (4) */
1260 if (reader + 4 > bufend)
1261 goto invalid_resp;
1262
1263 answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1264 + reader[2] * 256 + reader[3];
1265 reader += 4;
1266
1267 /* Now reading data len */
1268 if (reader + 2 > bufend)
1269 goto invalid_resp;
1270
1271 answer_record->data_len = reader[0] * 256 + reader[1];
1272
1273 /* Move forward 2 bytes for data len */
1274 reader += 2;
1275
1276 if (reader + answer_record->data_len > bufend)
1277 goto invalid_resp;
1278
1279 /* Analyzing record content */
1280 switch (answer_record->type) {
1281 case DNS_RTYPE_A:
1282 /* ipv4 is stored on 4 bytes */
1283 if (answer_record->data_len != 4)
1284 goto invalid_resp;
1285
1286 answer_record->address.sa_family = AF_INET;
1287 memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr),
1288 reader, answer_record->data_len);
1289 break;
1290
1291 case DNS_RTYPE_AAAA:
1292 /* ipv6 is stored on 16 bytes */
1293 if (answer_record->data_len != 16)
1294 goto invalid_resp;
1295
1296 answer_record->address.sa_family = AF_INET6;
1297 memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr),
1298 reader, answer_record->data_len);
1299 break;
1300
1301 default:
1302 pool_free(resolv_answer_item_pool, answer_record);
1303 answer_record = NULL;
1304 continue;
1305
1306 } /* switch (record type) */
1307
1308 /* Increment the counter for number of records saved into our
1309 * local response */
1310 nb_saved_records++;
1311
1312 /* Move forward answer_record->data_len for analyzing next
1313 * record in the response */
Christopher Faulet77f86062021-03-10 15:19:57 +01001314 reader += answer_record->data_len;
Emeric Brunc9437992021-02-12 19:42:55 +01001315
1316 /* Lookup to see if we already had this entry */
1317 found = 0;
1318 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
Christopher Faulet77f86062021-03-10 15:19:57 +01001319 struct resolv_answer_item *ar_item;
1320
1321 if (tmp_record->type != DNS_RTYPE_SRV || !tmp_record->ar_item)
1322 continue;
1323
1324 ar_item = tmp_record->ar_item;
Christopher Faulete8674c72021-03-12 16:42:45 +01001325 if (ar_item->type != answer_record->type || ar_item->last_seen == now_ms ||
Christopher Faulet77f86062021-03-10 15:19:57 +01001326 len != tmp_record->data_len ||
1327 resolv_hostname_cmp(answer_record->name, tmp_record->target, tmp_record->data_len))
Emeric Brunc9437992021-02-12 19:42:55 +01001328 continue;
1329
Christopher Faulet77f86062021-03-10 15:19:57 +01001330 switch(ar_item->type) {
Emeric Brunc9437992021-02-12 19:42:55 +01001331 case DNS_RTYPE_A:
1332 if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr,
Christopher Faulet77f86062021-03-10 15:19:57 +01001333 &((struct sockaddr_in *)&ar_item->address)->sin_addr,
Emeric Brunc9437992021-02-12 19:42:55 +01001334 sizeof(in_addr_t)))
1335 found = 1;
1336 break;
1337
1338 case DNS_RTYPE_AAAA:
1339 if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr,
Christopher Faulet77f86062021-03-10 15:19:57 +01001340 &((struct sockaddr_in6 *)&ar_item->address)->sin6_addr,
Emeric Brunc9437992021-02-12 19:42:55 +01001341 sizeof(struct in6_addr)))
1342 found = 1;
1343 break;
1344
1345 default:
1346 break;
1347 }
1348
1349 if (found == 1)
1350 break;
1351 }
1352
1353 if (found == 1) {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001354 tmp_record->ar_item->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001355 pool_free(resolv_answer_item_pool, answer_record);
1356 answer_record = NULL;
1357 }
1358 else {
Christopher Faulet55c1c402021-03-11 09:36:05 +01001359 answer_record->last_seen = now_ms;
Emeric Brunc9437992021-02-12 19:42:55 +01001360 answer_record->ar_item = NULL;
1361
1362 // looking for the SRV record in the response list linked to this additional record
1363 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
1364 if (tmp_record->type == DNS_RTYPE_SRV &&
1365 tmp_record->ar_item == NULL &&
1366 !resolv_hostname_cmp(tmp_record->target, answer_record->name, tmp_record->data_len)) {
1367 /* Always use the received additional record to refresh info */
1368 if (tmp_record->ar_item)
1369 pool_free(resolv_answer_item_pool, tmp_record->ar_item);
1370 tmp_record->ar_item = answer_record;
Christopher Faulet9c246a42021-02-23 11:59:19 +01001371 answer_record = NULL;
Emeric Brunc9437992021-02-12 19:42:55 +01001372 break;
1373 }
1374 }
Christopher Faulet9c246a42021-02-23 11:59:19 +01001375 if (answer_record) {
Emeric Brunc9437992021-02-12 19:42:55 +01001376 pool_free(resolv_answer_item_pool, answer_record);
Christopher Faulet9c246a42021-02-23 11:59:19 +01001377 answer_record = NULL;
1378 }
Emeric Brunc9437992021-02-12 19:42:55 +01001379 }
1380 } /* for i 0 to arcount */
1381
1382 skip_parsing_additional_records:
1383
1384 /* Save the number of records we really own */
1385 r_res->header.arcount = nb_saved_records;
Emeric Brunc9437992021-02-12 19:42:55 +01001386 resolv_check_response(resolution);
1387 return RSLV_RESP_VALID;
1388
1389 invalid_resp:
1390 cause = RSLV_RESP_INVALID;
1391
1392 return_error:
1393 pool_free(resolv_answer_item_pool, answer_record);
1394 return cause;
1395}
1396
1397/* Searches dn_name resolution in resp.
1398 * If existing IP not found, return the first IP matching family_priority,
1399 * otherwise, first ip found
1400 * The following tasks are the responsibility of the caller:
1401 * - <r_res> contains an error free DNS response
1402 * For both cases above, resolv_validate_dns_response is required
1403 * returns one of the RSLV_UPD_* code
1404 */
1405int resolv_get_ip_from_response(struct resolv_response *r_res,
1406 struct resolv_options *resolv_opts, void *currentip,
1407 short currentip_sin_family,
1408 void **newip, short *newip_sin_family,
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001409 struct server *owner)
Emeric Brunc9437992021-02-12 19:42:55 +01001410{
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001411 struct resolv_answer_item *record, *found_record = NULL;
Emeric Brunc9437992021-02-12 19:42:55 +01001412 int family_priority;
1413 int currentip_found;
1414 unsigned char *newip4, *newip6;
1415 int currentip_sel;
1416 int j;
1417 int score, max_score;
1418 int allowed_duplicated_ip;
1419
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001420 /* srv is linked to an alive ip record */
1421 if (owner && LIST_INLIST(&owner->ip_rec_item))
1422 return RSLV_UPD_NO;
1423
Emeric Brunc9437992021-02-12 19:42:55 +01001424 family_priority = resolv_opts->family_prio;
1425 allowed_duplicated_ip = resolv_opts->accept_duplicate_ip;
1426 *newip = newip4 = newip6 = NULL;
1427 currentip_found = 0;
1428 *newip_sin_family = AF_UNSPEC;
1429 max_score = -1;
1430
1431 /* Select an IP regarding configuration preference.
1432 * Top priority is the preferred network ip version,
1433 * second priority is the preferred network.
1434 * the last priority is the currently used IP,
1435 *
1436 * For these three priorities, a score is calculated. The
1437 * weight are:
1438 * 8 - preferred ip version.
1439 * 4 - preferred network.
1440 * 2 - if the ip in the record is not affected to any other server in the same backend (duplication)
1441 * 1 - current ip.
1442 * The result with the biggest score is returned.
1443 */
1444
1445 list_for_each_entry(record, &r_res->answer_list, list) {
1446 void *ip;
1447 unsigned char ip_type;
1448
1449 if (record->type == DNS_RTYPE_A) {
1450 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1451 ip_type = AF_INET;
1452 }
1453 else if (record->type == DNS_RTYPE_AAAA) {
1454 ip_type = AF_INET6;
1455 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
1456 }
1457 else
1458 continue;
1459 score = 0;
1460
1461 /* Check for preferred ip protocol. */
1462 if (ip_type == family_priority)
1463 score += 8;
1464
1465 /* Check for preferred network. */
1466 for (j = 0; j < resolv_opts->pref_net_nb; j++) {
1467
1468 /* Compare only the same addresses class. */
1469 if (resolv_opts->pref_net[j].family != ip_type)
1470 continue;
1471
1472 if ((ip_type == AF_INET &&
1473 in_net_ipv4(ip,
1474 &resolv_opts->pref_net[j].mask.in4,
1475 &resolv_opts->pref_net[j].addr.in4)) ||
1476 (ip_type == AF_INET6 &&
1477 in_net_ipv6(ip,
1478 &resolv_opts->pref_net[j].mask.in6,
1479 &resolv_opts->pref_net[j].addr.in6))) {
1480 score += 4;
1481 break;
1482 }
1483 }
1484
1485 /* Check if the IP found in the record is already affected to a
1486 * member of a group. If not, the score should be incremented
1487 * by 2. */
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001488 if (owner) {
1489 struct server *srv;
1490 int already_used = 0;
1491
1492 list_for_each_entry(srv, &record->attached_servers, ip_rec_item) {
1493 if (srv == owner)
1494 continue;
1495 if (srv->proxy == owner->proxy) {
1496 already_used = 1;
1497 break;
1498 }
1499 }
1500 if (already_used) {
1501 if (!allowed_duplicated_ip) {
1502 continue;
1503 }
1504 }
1505 else {
1506 score += 2;
Emeric Brunc9437992021-02-12 19:42:55 +01001507 }
1508 } else {
1509 score += 2;
1510 }
1511
1512 /* Check for current ip matching. */
1513 if (ip_type == currentip_sin_family &&
1514 ((currentip_sin_family == AF_INET &&
1515 !memcmp(ip, currentip, 4)) ||
1516 (currentip_sin_family == AF_INET6 &&
1517 !memcmp(ip, currentip, 16)))) {
1518 score++;
1519 currentip_sel = 1;
1520 }
1521 else
1522 currentip_sel = 0;
1523
1524 /* Keep the address if the score is better than the previous
1525 * score. The maximum score is 15, if this value is reached, we
1526 * break the parsing. Implicitly, this score is reached the ip
1527 * selected is the current ip. */
1528 if (score > max_score) {
1529 if (ip_type == AF_INET)
1530 newip4 = ip;
1531 else
1532 newip6 = ip;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001533 found_record = record;
Emeric Brunc9437992021-02-12 19:42:55 +01001534 currentip_found = currentip_sel;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001535 if (score == 15) {
1536 /* this was not registered on the current record but it matches
1537 * let's fix it (it may comes from state file */
1538 if (owner)
1539 LIST_APPEND(&found_record->attached_servers, &owner->ip_rec_item);
Emeric Brunc9437992021-02-12 19:42:55 +01001540 return RSLV_UPD_NO;
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001541 }
Emeric Brunc9437992021-02-12 19:42:55 +01001542 max_score = score;
1543 }
1544 } /* list for each record entries */
1545
1546 /* No IP found in the response */
1547 if (!newip4 && !newip6)
1548 return RSLV_UPD_NO_IP_FOUND;
1549
1550 /* Case when the caller looks first for an IPv4 address */
1551 if (family_priority == AF_INET) {
1552 if (newip4) {
1553 *newip = newip4;
1554 *newip_sin_family = AF_INET;
1555 }
1556 else if (newip6) {
1557 *newip = newip6;
1558 *newip_sin_family = AF_INET6;
1559 }
1560 if (!currentip_found)
1561 goto not_found;
1562 }
1563 /* Case when the caller looks first for an IPv6 address */
1564 else if (family_priority == AF_INET6) {
1565 if (newip6) {
1566 *newip = newip6;
1567 *newip_sin_family = AF_INET6;
1568 }
1569 else if (newip4) {
1570 *newip = newip4;
1571 *newip_sin_family = AF_INET;
1572 }
1573 if (!currentip_found)
1574 goto not_found;
1575 }
1576 /* Case when the caller have no preference (we prefer IPv6) */
1577 else if (family_priority == AF_UNSPEC) {
1578 if (newip6) {
1579 *newip = newip6;
1580 *newip_sin_family = AF_INET6;
1581 }
1582 else if (newip4) {
1583 *newip = newip4;
1584 *newip_sin_family = AF_INET;
1585 }
1586 if (!currentip_found)
1587 goto not_found;
1588 }
1589
1590 /* No reason why we should change the server's IP address */
1591 return RSLV_UPD_NO;
1592
1593 not_found:
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001594
1595 /* the ip of this record was chosen for the server */
1596 if (owner && found_record) {
1597 LIST_APPEND(&found_record->attached_servers, &owner->ip_rec_item);
1598 }
1599
Emeric Brunc9437992021-02-12 19:42:55 +01001600 list_for_each_entry(record, &r_res->answer_list, list) {
1601 /* Move the first record to the end of the list, for internal
1602 * round robin */
Willy Tarreau2b718102021-04-21 07:32:39 +02001603 LIST_DELETE(&record->list);
1604 LIST_APPEND(&r_res->answer_list, &record->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001605 break;
1606 }
1607 return RSLV_UPD_SRVIP_NOT_FOUND;
1608}
1609
1610/* Turns a domain name label into a string.
1611 *
1612 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1613 * null byte. <str> must be allocated and its size must be passed in <str_len>.
1614 *
1615 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1616 * <str> (including the terminating null byte).
1617 */
1618int resolv_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
1619{
1620 char *ptr;
1621 int i, sz;
1622
1623 if (str_len < dn_len - 1)
1624 return -1;
1625
1626 ptr = str;
1627 for (i = 0; i < dn_len-1; ++i) {
1628 sz = dn[i];
1629 if (i)
1630 *ptr++ = '.';
1631 memcpy(ptr, dn+i+1, sz);
1632 ptr += sz;
1633 i += sz;
1634 }
1635 *ptr++ = '\0';
1636 return (ptr - str);
1637}
1638
1639/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1640 *
1641 * <str> must be a null-terminated string. <str_len> must include the
1642 * terminating null byte. <dn> buffer must be allocated and its size must be
1643 * passed in <dn_len>.
1644 *
1645 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1646 * <dn> (excluding the terminating null byte).
1647 */
1648int resolv_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
1649{
1650 int i, offset;
1651
1652 if (dn_len < str_len + 1)
1653 return -1;
1654
1655 /* First byte of dn will be used to store the length of the first
1656 * label */
1657 offset = 0;
1658 for (i = 0; i < str_len; ++i) {
1659 if (str[i] == '.') {
1660 /* 2 or more consecutive dots is invalid */
1661 if (i == offset)
1662 return -1;
1663
1664 /* ignore trailing dot */
1665 if (i + 2 == str_len) {
1666 i++;
1667 break;
1668 }
1669
1670 dn[offset] = (i - offset);
1671 offset = i+1;
1672 continue;
1673 }
1674 dn[i+1] = str[i];
1675 }
1676 dn[offset] = (i - offset - 1);
1677 dn[i] = '\0';
1678 return i;
1679}
1680
1681/* Validates host name:
1682 * - total size
1683 * - each label size individually
1684 * returns:
1685 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1686 * 1 when no error. <err> is left unaffected.
1687 */
1688int resolv_hostname_validation(const char *string, char **err)
1689{
1690 int i;
1691
1692 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1693 if (err)
1694 *err = DNS_TOO_LONG_FQDN;
1695 return 0;
1696 }
1697
1698 while (*string) {
1699 i = 0;
1700 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1701 if (!(*string == '-' || *string == '_' ||
1702 (*string >= 'a' && *string <= 'z') ||
1703 (*string >= 'A' && *string <= 'Z') ||
1704 (*string >= '0' && *string <= '9'))) {
1705 if (err)
1706 *err = DNS_INVALID_CHARACTER;
1707 return 0;
1708 }
1709 i++;
1710 string++;
1711 }
1712
1713 if (!(*string))
1714 break;
1715
1716 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
1717 if (err)
1718 *err = DNS_LABEL_TOO_LONG;
1719 return 0;
1720 }
1721
1722 string++;
1723 }
1724 return 1;
1725}
1726
1727/* Picks up an available resolution from the different resolution list
1728 * associated to a resolvers section, in this order:
1729 * 1. check in resolutions.curr for the same hostname and query_type
1730 * 2. check in resolutions.wait for the same hostname and query_type
1731 * 3. Get a new resolution from resolution pool
1732 *
1733 * Returns an available resolution, NULL if none found.
1734 */
1735static struct resolv_resolution *resolv_pick_resolution(struct resolvers *resolvers,
1736 char **hostname_dn, int hostname_dn_len,
1737 int query_type)
1738{
1739 struct resolv_resolution *res;
1740
1741 if (!*hostname_dn)
1742 goto from_pool;
1743
1744 /* Search for same hostname and query type in resolutions.curr */
1745 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1746 if (!res->hostname_dn)
1747 continue;
1748 if ((query_type == res->prefered_query_type) &&
1749 hostname_dn_len == res->hostname_dn_len &&
1750 !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1751 return res;
1752 }
1753
1754 /* Search for same hostname and query type in resolutions.wait */
1755 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1756 if (!res->hostname_dn)
1757 continue;
1758 if ((query_type == res->prefered_query_type) &&
1759 hostname_dn_len == res->hostname_dn_len &&
1760 !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1761 return res;
1762 }
1763
1764 from_pool:
1765 /* No resolution could be found, so let's allocate a new one */
Willy Tarreau70490eb2021-03-22 21:08:50 +01001766 res = pool_zalloc(resolv_resolution_pool);
Emeric Brunc9437992021-02-12 19:42:55 +01001767 if (res) {
Emeric Brunc9437992021-02-12 19:42:55 +01001768 res->resolvers = resolvers;
1769 res->uuid = resolution_uuid;
1770 res->status = RSLV_STATUS_NONE;
1771 res->step = RSLV_STEP_NONE;
1772 res->last_valid = now_ms;
1773
1774 LIST_INIT(&res->requesters);
1775 LIST_INIT(&res->response.answer_list);
1776
1777 res->prefered_query_type = query_type;
1778 res->query_type = query_type;
1779 res->hostname_dn = *hostname_dn;
1780 res->hostname_dn_len = hostname_dn_len;
1781
1782 ++resolution_uuid;
1783
1784 /* Move the resolution to the resolvers wait queue */
Willy Tarreau2b718102021-04-21 07:32:39 +02001785 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001786 }
1787 return res;
1788}
1789
Christopher Faulet1dec5c72021-03-10 14:40:39 +01001790void resolv_purge_resolution_answer_records(struct resolv_resolution *resolution)
1791{
1792 struct resolv_answer_item *item, *itemback;
1793
1794 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001795 LIST_DELETE(&item->list);
Christopher Faulet1dec5c72021-03-10 14:40:39 +01001796 pool_free(resolv_answer_item_pool, item->ar_item);
1797 pool_free(resolv_answer_item_pool, item);
1798 }
1799}
1800
Emeric Brunc9437992021-02-12 19:42:55 +01001801/* Releases a resolution from its requester(s) and move it back to the pool */
1802static void resolv_free_resolution(struct resolv_resolution *resolution)
1803{
1804 struct resolv_requester *req, *reqback;
Emeric Brunc9437992021-02-12 19:42:55 +01001805
1806 /* clean up configuration */
1807 resolv_reset_resolution(resolution);
1808 resolution->hostname_dn = NULL;
1809 resolution->hostname_dn_len = 0;
1810
1811 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001812 LIST_DELETE(&req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001813 req->resolution = NULL;
1814 }
Christopher Faulet1dec5c72021-03-10 14:40:39 +01001815 resolv_purge_resolution_answer_records(resolution);
Willy Tarreau2b718102021-04-21 07:32:39 +02001816 LIST_DELETE(&resolution->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001817 pool_free(resolv_resolution_pool, resolution);
1818}
1819
1820/* Links a requester (a server or a resolv_srvrq) with a resolution. It returns 0
1821 * on success, -1 otherwise.
1822 */
1823int resolv_link_resolution(void *requester, int requester_type, int requester_locked)
1824{
1825 struct resolv_resolution *res = NULL;
1826 struct resolv_requester *req;
1827 struct resolvers *resolvers;
1828 struct server *srv = NULL;
1829 struct resolv_srvrq *srvrq = NULL;
1830 struct stream *stream = NULL;
1831 char **hostname_dn;
1832 int hostname_dn_len, query_type;
1833
1834 switch (requester_type) {
1835 case OBJ_TYPE_SERVER:
1836 srv = (struct server *)requester;
1837 hostname_dn = &srv->hostname_dn;
1838 hostname_dn_len = srv->hostname_dn_len;
1839 resolvers = srv->resolvers;
1840 query_type = ((srv->resolv_opts.family_prio == AF_INET)
1841 ? DNS_RTYPE_A
1842 : DNS_RTYPE_AAAA);
1843 break;
1844
1845 case OBJ_TYPE_SRVRQ:
1846 srvrq = (struct resolv_srvrq *)requester;
1847 hostname_dn = &srvrq->hostname_dn;
1848 hostname_dn_len = srvrq->hostname_dn_len;
1849 resolvers = srvrq->resolvers;
1850 query_type = DNS_RTYPE_SRV;
1851 break;
1852
1853 case OBJ_TYPE_STREAM:
1854 stream = (struct stream *)requester;
1855 hostname_dn = &stream->resolv_ctx.hostname_dn;
1856 hostname_dn_len = stream->resolv_ctx.hostname_dn_len;
1857 resolvers = stream->resolv_ctx.parent->arg.resolv.resolvers;
1858 query_type = ((stream->resolv_ctx.parent->arg.resolv.opts->family_prio == AF_INET)
1859 ? DNS_RTYPE_A
1860 : DNS_RTYPE_AAAA);
1861 break;
1862 default:
1863 goto err;
1864 }
1865
1866 /* Get a resolution from the resolvers' wait queue or pool */
1867 if ((res = resolv_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1868 goto err;
1869
1870 if (srv) {
1871 if (!requester_locked)
1872 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
1873 if (srv->resolv_requester == NULL) {
1874 if ((req = pool_alloc(resolv_requester_pool)) == NULL) {
1875 if (!requester_locked)
1876 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1877 goto err;
1878 }
1879 req->owner = &srv->obj_type;
1880 srv->resolv_requester = req;
1881 }
1882 else
1883 req = srv->resolv_requester;
1884 if (!requester_locked)
1885 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1886
1887 req->requester_cb = snr_resolution_cb;
1888 req->requester_error_cb = snr_resolution_error_cb;
1889 }
1890 else if (srvrq) {
1891 if (srvrq->requester == NULL) {
1892 if ((req = pool_alloc(resolv_requester_pool)) == NULL)
1893 goto err;
1894 req->owner = &srvrq->obj_type;
1895 srvrq->requester = req;
1896 }
1897 else
1898 req = srvrq->requester;
1899
1900 req->requester_cb = snr_resolution_cb;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01001901 req->requester_error_cb = srvrq_resolution_error_cb;
Emeric Brunc9437992021-02-12 19:42:55 +01001902 }
1903 else if (stream) {
1904 if (stream->resolv_ctx.requester == NULL) {
1905 if ((req = pool_alloc(resolv_requester_pool)) == NULL)
1906 goto err;
1907 req->owner = &stream->obj_type;
1908 stream->resolv_ctx.requester = req;
1909 }
1910 else
1911 req = stream->resolv_ctx.requester;
1912
1913 req->requester_cb = act_resolution_cb;
1914 req->requester_error_cb = act_resolution_error_cb;
1915 }
1916 else
1917 goto err;
1918
1919 req->resolution = res;
1920
Willy Tarreau2b718102021-04-21 07:32:39 +02001921 LIST_APPEND(&res->requesters, &req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001922 return 0;
1923
1924 err:
1925 if (res && LIST_ISEMPTY(&res->requesters))
1926 resolv_free_resolution(res);
1927 return -1;
1928}
1929
Emeric Brun0c4a8a32021-06-11 10:48:45 +02001930/* This function removes all server/srvrq references on answer items
1931 * if <safe> is set to 1, in case of srvrq, sub server resquesters unlink
1932 * is called using safe == 1 to make it usable into callbacks
1933 */
1934void resolv_detach_from_resolution_answer_items(struct resolv_resolution *res, struct resolv_requester *req, int safe)
1935{
1936 struct resolv_answer_item *item, *itemback;
1937 struct server *srv, *srvback;
1938 struct resolv_srvrq *srvrq;
1939
1940 if ((srv = objt_server(req->owner)) != NULL) {
1941 LIST_DEL_INIT(&srv->ip_rec_item);
1942 }
1943 else if ((srvrq = objt_resolv_srvrq(req->owner)) != NULL) {
1944 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
1945 if (item->type == DNS_RTYPE_SRV) {
1946 list_for_each_entry_safe(srv, srvback, &item->attached_servers, srv_rec_item) {
Christopher Faulet5f8ccff2021-06-15 16:08:48 +02001947 if (srv->srvrq == srvrq)
1948 resolv_srvrq_cleanup_srv(srv);
Emeric Brun0c4a8a32021-06-11 10:48:45 +02001949 }
1950 }
1951 }
1952 }
1953}
1954
Emeric Brunc9437992021-02-12 19:42:55 +01001955/* Removes a requester from a DNS resolution. It takes takes care of all the
1956 * consequences. It also cleans up some parameters from the requester.
Christopher Faulet0efc0992021-03-11 18:09:53 +01001957 * if <safe> is set to 1, the corresponding resolution is not released.
Emeric Brunc9437992021-02-12 19:42:55 +01001958 */
Christopher Faulet0efc0992021-03-11 18:09:53 +01001959void resolv_unlink_resolution(struct resolv_requester *requester, int safe)
Emeric Brunc9437992021-02-12 19:42:55 +01001960{
1961 struct resolv_resolution *res;
1962 struct resolv_requester *req;
1963
1964 /* Nothing to do */
1965 if (!requester || !requester->resolution)
1966 return;
1967 res = requester->resolution;
1968
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001969 /* remove ref from the resolution answer item list to the requester */
Emeric Brun0c4a8a32021-06-11 10:48:45 +02001970 resolv_detach_from_resolution_answer_items(res, requester, safe);
Emeric Brunf9ca5d82021-06-11 10:08:05 +02001971
Emeric Brunc9437992021-02-12 19:42:55 +01001972 /* Clean up the requester */
Willy Tarreau2b718102021-04-21 07:32:39 +02001973 LIST_DELETE(&requester->list);
Emeric Brunc9437992021-02-12 19:42:55 +01001974 requester->resolution = NULL;
1975
1976 /* We need to find another requester linked on this resolution */
1977 if (!LIST_ISEMPTY(&res->requesters))
1978 req = LIST_NEXT(&res->requesters, struct resolv_requester *, list);
1979 else {
Christopher Faulet0efc0992021-03-11 18:09:53 +01001980 if (safe) {
1981 /* Don't release it yet. */
1982 resolv_reset_resolution(res);
1983 res->hostname_dn = NULL;
1984 res->hostname_dn_len = 0;
1985 resolv_purge_resolution_answer_records(res);
1986 return;
1987 }
1988
Emeric Brunc9437992021-02-12 19:42:55 +01001989 resolv_free_resolution(res);
1990 return;
1991 }
1992
1993 /* Move hostname_dn related pointers to the next requester */
1994 switch (obj_type(req->owner)) {
1995 case OBJ_TYPE_SERVER:
1996 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1997 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
1998 break;
1999 case OBJ_TYPE_SRVRQ:
2000 res->hostname_dn = __objt_resolv_srvrq(req->owner)->hostname_dn;
2001 res->hostname_dn_len = __objt_resolv_srvrq(req->owner)->hostname_dn_len;
2002 break;
2003 case OBJ_TYPE_STREAM:
2004 res->hostname_dn = __objt_stream(req->owner)->resolv_ctx.hostname_dn;
2005 res->hostname_dn_len = __objt_stream(req->owner)->resolv_ctx.hostname_dn_len;
2006 break;
2007 default:
2008 res->hostname_dn = NULL;
2009 res->hostname_dn_len = 0;
2010 break;
2011 }
2012}
2013
2014/* Called when a network IO is generated on a name server socket for an incoming
2015 * packet. It performs the following actions:
2016 * - check if the packet requires processing (not outdated resolution)
2017 * - ensure the DNS packet received is valid and call requester's callback
2018 * - call requester's error callback if invalid response
2019 * - check the dn_name in the packet against the one sent
2020 */
2021static int resolv_process_responses(struct dns_nameserver *ns)
2022{
2023 struct dns_counters *tmpcounters;
2024 struct resolvers *resolvers;
2025 struct resolv_resolution *res;
2026 struct resolv_query_item *query;
2027 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
2028 unsigned char *bufend;
2029 int buflen, dns_resp;
2030 int max_answer_records;
2031 unsigned short query_id;
2032 struct eb32_node *eb;
2033 struct resolv_requester *req;
Emeric Brun43839d02021-06-10 15:25:25 +02002034 int keep_answer_items;
Emeric Brunc9437992021-02-12 19:42:55 +01002035
2036 resolvers = ns->parent;
2037 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2038
2039 /* process all pending input messages */
2040 while (1) {
2041 /* read message received */
2042 memset(buf, '\0', resolvers->accepted_payload_size + 1);
2043 if ((buflen = dns_recv_nameserver(ns, (void *)buf, sizeof(buf))) <= 0) {
2044 break;
2045 }
2046
2047 /* message too big */
2048 if (buflen > resolvers->accepted_payload_size) {
2049 ns->counters->too_big++;
2050 continue;
2051 }
2052
2053 /* initializing variables */
2054 bufend = buf + buflen; /* pointer to mark the end of the buffer */
2055
2056 /* read the query id from the packet (16 bits) */
2057 if (buf + 2 > bufend) {
2058 ns->counters->invalid++;
2059 continue;
2060 }
2061 query_id = resolv_response_get_query_id(buf);
2062
2063 /* search the query_id in the pending resolution tree */
2064 eb = eb32_lookup(&resolvers->query_ids, query_id);
2065 if (eb == NULL) {
2066 /* unknown query id means an outdated response and can be safely ignored */
2067 ns->counters->outdated++;
2068 continue;
2069 }
2070
2071 /* known query id means a resolution in progress */
2072 res = eb32_entry(eb, struct resolv_resolution, qid);
2073 /* number of responses received */
2074 res->nb_responses++;
2075
2076 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
2077 dns_resp = resolv_validate_dns_response(buf, bufend, res, max_answer_records);
2078
2079 switch (dns_resp) {
2080 case RSLV_RESP_VALID:
2081 break;
2082
2083 case RSLV_RESP_INVALID:
2084 case RSLV_RESP_QUERY_COUNT_ERROR:
2085 case RSLV_RESP_WRONG_NAME:
2086 res->status = RSLV_STATUS_INVALID;
2087 ns->counters->invalid++;
2088 break;
2089
2090 case RSLV_RESP_NX_DOMAIN:
2091 res->status = RSLV_STATUS_NX;
2092 ns->counters->nx++;
2093 break;
2094
2095 case RSLV_RESP_REFUSED:
2096 res->status = RSLV_STATUS_REFUSED;
2097 ns->counters->refused++;
2098 break;
2099
2100 case RSLV_RESP_ANCOUNT_ZERO:
2101 res->status = RSLV_STATUS_OTHER;
2102 ns->counters->any_err++;
2103 break;
2104
2105 case RSLV_RESP_CNAME_ERROR:
2106 res->status = RSLV_STATUS_OTHER;
2107 ns->counters->cname_error++;
2108 break;
2109
2110 case RSLV_RESP_TRUNCATED:
2111 res->status = RSLV_STATUS_OTHER;
2112 ns->counters->truncated++;
2113 break;
2114
2115 case RSLV_RESP_NO_EXPECTED_RECORD:
2116 case RSLV_RESP_ERROR:
2117 case RSLV_RESP_INTERNAL:
2118 res->status = RSLV_STATUS_OTHER;
2119 ns->counters->other++;
2120 break;
2121 }
2122
2123 /* Wait all nameservers response to handle errors */
2124 if (dns_resp != RSLV_RESP_VALID && res->nb_responses < res->nb_queries)
2125 continue;
2126
2127 /* Process error codes */
2128 if (dns_resp != RSLV_RESP_VALID) {
2129 if (res->prefered_query_type != res->query_type) {
2130 /* The fallback on the query type was already performed,
2131 * so check the try counter. If it falls to 0, we can
2132 * report an error. Else, wait the next attempt. */
2133 if (!res->try)
2134 goto report_res_error;
2135 }
2136 else {
2137 /* Fallback from A to AAAA or the opposite and re-send
2138 * the resolution immediately. try counter is not
2139 * decremented. */
2140 if (res->prefered_query_type == DNS_RTYPE_A) {
2141 res->query_type = DNS_RTYPE_AAAA;
2142 resolv_send_query(res);
2143 }
2144 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
2145 res->query_type = DNS_RTYPE_A;
2146 resolv_send_query(res);
2147 }
2148 }
2149 continue;
2150 }
2151
2152 /* Now let's check the query's dname corresponds to the one we
2153 * sent. We can check only the first query of the list. We send
2154 * one query at a time so we get one query in the response */
2155 query = LIST_NEXT(&res->response.query_list, struct resolv_query_item *, list);
2156 if (query && resolv_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
2157 dns_resp = RSLV_RESP_WRONG_NAME;
2158 ns->counters->other++;
2159 goto report_res_error;
2160 }
2161
2162 /* So the resolution succeeded */
2163 res->status = RSLV_STATUS_VALID;
2164 res->last_valid = now_ms;
2165 ns->counters->valid++;
2166 goto report_res_success;
2167
2168 report_res_error:
Emeric Brun43839d02021-06-10 15:25:25 +02002169 keep_answer_items = 0;
Emeric Brunc9437992021-02-12 19:42:55 +01002170 list_for_each_entry(req, &res->requesters, list)
Emeric Brun43839d02021-06-10 15:25:25 +02002171 keep_answer_items |= req->requester_error_cb(req, dns_resp);
2172 if (!keep_answer_items)
2173 resolv_purge_resolution_answer_records(res);
Emeric Brunc9437992021-02-12 19:42:55 +01002174 resolv_reset_resolution(res);
Willy Tarreau2b718102021-04-21 07:32:39 +02002175 LIST_DELETE(&res->list);
2176 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002177 continue;
2178
2179 report_res_success:
2180 /* Only the 1rst requester s managed by the server, others are
2181 * from the cache */
2182 tmpcounters = ns->counters;
2183 list_for_each_entry(req, &res->requesters, list) {
2184 struct server *s = objt_server(req->owner);
2185
2186 if (s)
2187 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
2188 req->requester_cb(req, tmpcounters);
2189 if (s)
2190 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2191 tmpcounters = NULL;
2192 }
2193
2194 resolv_reset_resolution(res);
Willy Tarreau2b718102021-04-21 07:32:39 +02002195 LIST_DELETE(&res->list);
2196 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002197 continue;
2198 }
2199 resolv_update_resolvers_timeout(resolvers);
2200 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2201
2202 return buflen;
2203}
2204
2205/* Processes DNS resolution. First, it checks the active list to detect expired
2206 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2207 * checks the wait list to trigger new resolutions.
2208 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002209static struct task *process_resolvers(struct task *t, void *context, unsigned int state)
Emeric Brunc9437992021-02-12 19:42:55 +01002210{
2211 struct resolvers *resolvers = context;
2212 struct resolv_resolution *res, *resback;
2213 int exp;
2214
2215 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2216
2217 /* Handle all expired resolutions from the active list */
2218 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01002219 if (LIST_ISEMPTY(&res->requesters)) {
2220 resolv_free_resolution(res);
2221 continue;
2222 }
2223
Emeric Brunc9437992021-02-12 19:42:55 +01002224 /* When we find the first resolution in the future, then we can
2225 * stop here */
2226 exp = tick_add(res->last_query, resolvers->timeout.retry);
2227 if (!tick_is_expired(exp, now_ms))
2228 break;
2229
2230 /* If current resolution has been tried too many times and
2231 * finishes in timeout we update its status and remove it from
2232 * the list */
2233 if (!res->try) {
2234 struct resolv_requester *req;
Emeric Brun43839d02021-06-10 15:25:25 +02002235 int keep_answer_items = 0;
Emeric Brunc9437992021-02-12 19:42:55 +01002236
2237 /* Notify the result to the requesters */
2238 if (!res->nb_responses)
2239 res->status = RSLV_STATUS_TIMEOUT;
2240 list_for_each_entry(req, &res->requesters, list)
Emeric Brun43839d02021-06-10 15:25:25 +02002241 keep_answer_items |= req->requester_error_cb(req, res->status);
2242 if (!keep_answer_items)
2243 resolv_purge_resolution_answer_records(res);
Emeric Brunc9437992021-02-12 19:42:55 +01002244
2245 /* Clean up resolution info and remove it from the
2246 * current list */
2247 resolv_reset_resolution(res);
Willy Tarreau2b718102021-04-21 07:32:39 +02002248 LIST_DELETE(&res->list);
2249 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002250 }
2251 else {
2252 /* Otherwise resend the DNS query and requeue the resolution */
2253 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2254 /* No response received (a real timeout) or fallback already done */
2255 res->query_type = res->prefered_query_type;
2256 res->try--;
2257 }
2258 else {
2259 /* Fallback from A to AAAA or the opposite and re-send
2260 * the resolution immediately. try counter is not
2261 * decremented. */
2262 if (res->prefered_query_type == DNS_RTYPE_A)
2263 res->query_type = DNS_RTYPE_AAAA;
2264 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2265 res->query_type = DNS_RTYPE_A;
2266 else
2267 res->try--;
2268 }
2269 resolv_send_query(res);
2270 }
2271 }
2272
2273 /* Handle all resolutions in the wait list */
2274 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01002275 if (LIST_ISEMPTY(&res->requesters)) {
2276 resolv_free_resolution(res);
2277 continue;
2278 }
2279
Emeric Brunc9437992021-02-12 19:42:55 +01002280 exp = tick_add(res->last_resolution, resolv_resolution_timeout(res));
2281 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2282 continue;
2283
2284 if (resolv_run_resolution(res) != 1) {
2285 res->last_resolution = now_ms;
Willy Tarreau2b718102021-04-21 07:32:39 +02002286 LIST_DELETE(&res->list);
2287 LIST_APPEND(&resolvers->resolutions.wait, &res->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002288 }
2289 }
2290
2291 resolv_update_resolvers_timeout(resolvers);
2292 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2293 return t;
2294}
2295
2296/* Release memory allocated by DNS */
2297static void resolvers_deinit(void)
2298{
2299 struct resolvers *resolvers, *resolversback;
2300 struct dns_nameserver *ns, *nsback;
2301 struct resolv_resolution *res, *resback;
2302 struct resolv_requester *req, *reqback;
2303 struct resolv_srvrq *srvrq, *srvrqback;
2304
2305 list_for_each_entry_safe(resolvers, resolversback, &sec_resolvers, list) {
2306 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2307 free(ns->id);
2308 free((char *)ns->conf.file);
2309 if (ns->dgram) {
2310 if (ns->dgram->conn.t.sock.fd != -1) {
2311 fd_delete(ns->dgram->conn.t.sock.fd);
2312 close(ns->dgram->conn.t.sock.fd);
2313 }
2314 if (ns->dgram->ring_req)
2315 ring_free(ns->dgram->ring_req);
2316 free(ns->dgram);
2317 }
Emeric Brun56fc5d92021-02-12 20:05:45 +01002318 if (ns->stream) {
2319 if (ns->stream->ring_req)
2320 ring_free(ns->stream->ring_req);
2321 if (ns->stream->task_req)
2322 task_destroy(ns->stream->task_req);
2323 if (ns->stream->task_rsp)
2324 task_destroy(ns->stream->task_rsp);
2325 free(ns->stream);
2326 }
Willy Tarreau2b718102021-04-21 07:32:39 +02002327 LIST_DELETE(&ns->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002328 EXTRA_COUNTERS_FREE(ns->extra_counters);
2329 free(ns);
2330 }
2331
2332 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2333 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002334 LIST_DELETE(&req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002335 pool_free(resolv_requester_pool, req);
2336 }
2337 resolv_free_resolution(res);
2338 }
2339
2340 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2341 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002342 LIST_DELETE(&req->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002343 pool_free(resolv_requester_pool, req);
2344 }
2345 resolv_free_resolution(res);
2346 }
2347
2348 free(resolvers->id);
2349 free((char *)resolvers->conf.file);
2350 task_destroy(resolvers->t);
Willy Tarreau2b718102021-04-21 07:32:39 +02002351 LIST_DELETE(&resolvers->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002352 free(resolvers);
2353 }
2354
2355 list_for_each_entry_safe(srvrq, srvrqback, &resolv_srvrq_list, list) {
2356 free(srvrq->name);
2357 free(srvrq->hostname_dn);
Willy Tarreau2b718102021-04-21 07:32:39 +02002358 LIST_DELETE(&srvrq->list);
Emeric Brunc9437992021-02-12 19:42:55 +01002359 free(srvrq);
2360 }
2361}
2362
2363/* Finalizes the DNS configuration by allocating required resources and checking
2364 * live parameters.
2365 * Returns 0 on success, ERR_* flags otherwise.
2366 */
2367static int resolvers_finalize_config(void)
2368{
2369 struct resolvers *resolvers;
2370 struct proxy *px;
2371 int err_code = 0;
2372
2373 /* allocate pool of resolution per resolvers */
2374 list_for_each_entry(resolvers, &sec_resolvers, list) {
2375 struct dns_nameserver *ns;
2376 struct task *t;
2377
2378 /* Check if we can create the socket with nameservers info */
2379 list_for_each_entry(ns, &resolvers->nameservers, list) {
2380 int fd;
2381
2382 if (ns->dgram) {
2383 /* Check nameserver info */
2384 if ((fd = socket(ns->dgram->conn.addr.to.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
2385 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2386 resolvers->id, ns->id);
2387 err_code |= (ERR_ALERT|ERR_ABORT);
2388 continue;
2389 }
2390 if (connect(fd, (struct sockaddr*)&ns->dgram->conn.addr.to, get_addr_len(&ns->dgram->conn.addr.to)) == -1) {
2391 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2392 resolvers->id, ns->id);
2393 close(fd);
2394 err_code |= (ERR_ALERT|ERR_ABORT);
2395 continue;
2396 }
2397 close(fd);
2398 }
2399 }
2400
2401 /* Create the task associated to the resolvers section */
2402 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
2403 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
2404 err_code |= (ERR_ALERT|ERR_ABORT);
2405 goto err;
2406 }
2407
2408 /* Update task's parameters */
2409 t->process = process_resolvers;
2410 t->context = resolvers;
2411 resolvers->t = t;
2412 task_wakeup(t, TASK_WOKEN_INIT);
2413 }
2414
2415 for (px = proxies_list; px; px = px->next) {
2416 struct server *srv;
2417
2418 for (srv = px->srv; srv; srv = srv->next) {
2419 struct resolvers *resolvers;
2420
2421 if (!srv->resolvers_id)
2422 continue;
2423
2424 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
2425 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2426 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
2427 err_code |= (ERR_ALERT|ERR_ABORT);
2428 continue;
2429 }
2430 srv->resolvers = resolvers;
2431
2432 if (srv->srvrq && !srv->srvrq->resolvers) {
2433 srv->srvrq->resolvers = srv->resolvers;
2434 if (resolv_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
2435 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2436 proxy_type_str(px), px->id, srv->id);
2437 err_code |= (ERR_ALERT|ERR_ABORT);
2438 continue;
2439 }
2440 }
Christopher Fauletd83a6df2021-03-12 10:23:05 +01002441 if (!srv->srvrq && resolv_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Emeric Brunc9437992021-02-12 19:42:55 +01002442 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2443 proxy_type_str(px), px->id, srv->id);
2444 err_code |= (ERR_ALERT|ERR_ABORT);
2445 continue;
2446 }
2447 }
2448 }
2449
2450 if (err_code & (ERR_ALERT|ERR_ABORT))
2451 goto err;
2452
2453 return err_code;
2454 err:
2455 resolvers_deinit();
2456 return err_code;
2457
2458}
2459
2460static int stats_dump_resolv_to_buffer(struct stream_interface *si,
2461 struct dns_nameserver *ns,
2462 struct field *stats, size_t stats_count,
2463 struct list *stat_modules)
2464{
2465 struct appctx *appctx = __objt_appctx(si->end);
2466 struct channel *rep = si_ic(si);
2467 struct stats_module *mod;
2468 size_t idx = 0;
2469
2470 memset(stats, 0, sizeof(struct field) * stats_count);
2471
2472 list_for_each_entry(mod, stat_modules, list) {
2473 struct counters_node *counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2474
2475 mod->fill_stats(counters, stats + idx);
2476 idx += mod->stats_count;
2477 }
2478
2479 if (!stats_dump_one_line(stats, idx, appctx))
2480 return 0;
2481
2482 if (!stats_putchk(rep, NULL, &trash))
2483 goto full;
2484
2485 return 1;
2486
2487 full:
2488 si_rx_room_rdy(si);
2489 return 0;
2490}
2491
2492/* Uses <appctx.ctx.stats.obj1> as a pointer to the current resolver and <obj2>
2493 * as a pointer to the current nameserver.
2494 */
2495int stats_dump_resolvers(struct stream_interface *si,
2496 struct field *stats, size_t stats_count,
2497 struct list *stat_modules)
2498{
2499 struct appctx *appctx = __objt_appctx(si->end);
2500 struct channel *rep = si_ic(si);
2501 struct resolvers *resolver = appctx->ctx.stats.obj1;
2502 struct dns_nameserver *ns = appctx->ctx.stats.obj2;
2503
2504 if (!resolver)
2505 resolver = LIST_NEXT(&sec_resolvers, struct resolvers *, list);
2506
2507 /* dump resolvers */
2508 list_for_each_entry_from(resolver, &sec_resolvers, list) {
2509 appctx->ctx.stats.obj1 = resolver;
2510
2511 ns = appctx->ctx.stats.obj2 ?
2512 appctx->ctx.stats.obj2 :
2513 LIST_NEXT(&resolver->nameservers, struct dns_nameserver *, list);
2514
2515 list_for_each_entry_from(ns, &resolver->nameservers, list) {
2516 appctx->ctx.stats.obj2 = ns;
2517
2518 if (buffer_almost_full(&rep->buf))
2519 goto full;
2520
2521 if (!stats_dump_resolv_to_buffer(si, ns,
2522 stats, stats_count,
2523 stat_modules)) {
2524 return 0;
2525 }
2526 }
2527
2528 appctx->ctx.stats.obj2 = NULL;
2529 }
2530
2531 return 1;
2532
2533 full:
2534 si_rx_room_blk(si);
2535 return 0;
2536}
2537
2538void resolv_stats_clear_counters(int clrall, struct list *stat_modules)
2539{
2540 struct resolvers *resolvers;
2541 struct dns_nameserver *ns;
2542 struct stats_module *mod;
2543 void *counters;
2544
2545 list_for_each_entry(mod, stat_modules, list) {
2546 if (!mod->clearable && !clrall)
2547 continue;
2548
2549 list_for_each_entry(resolvers, &sec_resolvers, list) {
2550 list_for_each_entry(ns, &resolvers->nameservers, list) {
2551 counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2552 memcpy(counters, mod->counters, mod->counters_size);
2553 }
2554 }
2555 }
2556
2557}
2558
2559int resolv_allocate_counters(struct list *stat_modules)
2560{
2561 struct stats_module *mod;
2562 struct resolvers *resolvers;
2563 struct dns_nameserver *ns;
2564
2565 list_for_each_entry(resolvers, &sec_resolvers, list) {
2566 list_for_each_entry(ns, &resolvers->nameservers, list) {
2567 EXTRA_COUNTERS_REGISTER(&ns->extra_counters, COUNTERS_DNS,
2568 alloc_failed);
2569
2570 list_for_each_entry(mod, stat_modules, list) {
2571 EXTRA_COUNTERS_ADD(mod,
2572 ns->extra_counters,
2573 mod->counters,
2574 mod->counters_size);
2575 }
2576
2577 EXTRA_COUNTERS_ALLOC(ns->extra_counters, alloc_failed);
2578
2579 list_for_each_entry(mod, stat_modules, list) {
2580 memcpy(ns->extra_counters->data + mod->counters_off[ns->extra_counters->type],
2581 mod->counters, mod->counters_size);
2582
2583 /* Store the ns counters pointer */
2584 if (strcmp(mod->name, "dns") == 0) {
2585 ns->counters = (struct dns_counters *)ns->extra_counters->data + mod->counters_off[COUNTERS_DNS];
2586 ns->counters->id = ns->id;
2587 ns->counters->pid = resolvers->id;
2588 }
2589 }
2590 }
2591 }
2592
2593 return 1;
2594
2595alloc_failed:
2596 return 0;
2597}
2598
2599/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
2600static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
2601{
2602 struct resolvers *presolvers;
2603
2604 if (*args[2]) {
2605 list_for_each_entry(presolvers, &sec_resolvers, list) {
2606 if (strcmp(presolvers->id, args[2]) == 0) {
2607 appctx->ctx.cli.p0 = presolvers;
2608 break;
2609 }
2610 }
2611 if (appctx->ctx.cli.p0 == NULL)
2612 return cli_err(appctx, "Can't find that resolvers section\n");
2613 }
2614 return 0;
2615}
2616
2617/* Dumps counters from all resolvers section and associated name servers. It
2618 * returns 0 if the output buffer is full and it needs to be called again,
2619 * otherwise non-zero. It may limit itself to the resolver pointed to by
2620 * <cli.p0> if it's not null.
2621 */
2622static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2623{
2624 struct stream_interface *si = appctx->owner;
2625 struct resolvers *resolvers;
2626 struct dns_nameserver *ns;
2627
2628 chunk_reset(&trash);
2629
2630 switch (appctx->st2) {
2631 case STAT_ST_INIT:
2632 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2633 /* fall through */
2634
2635 case STAT_ST_LIST:
2636 if (LIST_ISEMPTY(&sec_resolvers)) {
2637 chunk_appendf(&trash, "No resolvers found\n");
2638 }
2639 else {
2640 list_for_each_entry(resolvers, &sec_resolvers, list) {
2641 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
2642 continue;
2643
2644 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2645 list_for_each_entry(ns, &resolvers->nameservers, list) {
2646 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2647 chunk_appendf(&trash, " sent: %lld\n", ns->counters->sent);
2648 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters->snd_error);
2649 chunk_appendf(&trash, " valid: %lld\n", ns->counters->valid);
2650 chunk_appendf(&trash, " update: %lld\n", ns->counters->update);
2651 chunk_appendf(&trash, " cname: %lld\n", ns->counters->cname);
2652 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters->cname_error);
2653 chunk_appendf(&trash, " any_err: %lld\n", ns->counters->any_err);
2654 chunk_appendf(&trash, " nx: %lld\n", ns->counters->nx);
2655 chunk_appendf(&trash, " timeout: %lld\n", ns->counters->timeout);
2656 chunk_appendf(&trash, " refused: %lld\n", ns->counters->refused);
2657 chunk_appendf(&trash, " other: %lld\n", ns->counters->other);
2658 chunk_appendf(&trash, " invalid: %lld\n", ns->counters->invalid);
2659 chunk_appendf(&trash, " too_big: %lld\n", ns->counters->too_big);
2660 chunk_appendf(&trash, " truncated: %lld\n", ns->counters->truncated);
2661 chunk_appendf(&trash, " outdated: %lld\n", ns->counters->outdated);
2662 }
2663 chunk_appendf(&trash, "\n");
2664 }
2665 }
2666
2667 /* display response */
2668 if (ci_putchk(si_ic(si), &trash) == -1) {
2669 /* let's try again later from this session. We add ourselves into
2670 * this session's users so that it can remove us upon termination.
2671 */
2672 si_rx_room_blk(si);
2673 return 0;
2674 }
2675 /* fall through */
2676
2677 default:
2678 appctx->st2 = STAT_ST_FIN;
2679 return 1;
2680 }
2681}
2682
2683/* register cli keywords */
2684static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02002685 { { "show", "resolvers", NULL }, "show resolvers [id] : dumps counters from all resolvers section and associated name servers",
Emeric Brunc9437992021-02-12 19:42:55 +01002686 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2687 {{},}
2688 }
2689};
2690
2691INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
2692
2693/*
2694 * Prepare <rule> for hostname resolution.
2695 * Returns -1 in case of any allocation failure, 0 if not.
2696 * On error, a global failure counter is also incremented.
2697 */
2698static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2699{
2700 char *hostname_dn;
2701 int hostname_len, hostname_dn_len;
2702 struct buffer *tmp = get_trash_chunk();
2703
2704 if (!hostname)
2705 return 0;
2706
2707 hostname_len = strlen(hostname);
2708 hostname_dn = tmp->area;
2709 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
2710 hostname_dn, tmp->size);
2711 if (hostname_dn_len == -1)
2712 goto err;
2713
2714
2715 stream->resolv_ctx.hostname_dn = strdup(hostname_dn);
2716 stream->resolv_ctx.hostname_dn_len = hostname_dn_len;
2717 if (!stream->resolv_ctx.hostname_dn)
2718 goto err;
2719
2720 return 0;
2721
2722 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002723 ha_free(&stream->resolv_ctx.hostname_dn);
Emeric Brunc9437992021-02-12 19:42:55 +01002724 resolv_failed_resolutions += 1;
2725 return -1;
2726}
2727
2728
2729/*
2730 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2731 */
2732enum act_return resolv_action_do_resolve(struct act_rule *rule, struct proxy *px,
2733 struct session *sess, struct stream *s, int flags)
2734{
2735 struct resolv_resolution *resolution;
2736 struct sample *smp;
2737 char *fqdn;
2738 struct resolv_requester *req;
2739 struct resolvers *resolvers;
2740 struct resolv_resolution *res;
2741 int exp, locked = 0;
2742 enum act_return ret = ACT_RET_CONT;
2743
2744 resolvers = rule->arg.resolv.resolvers;
2745
2746 /* we have a response to our DNS resolution */
2747 use_cache:
2748 if (s->resolv_ctx.requester && s->resolv_ctx.requester->resolution != NULL) {
2749 resolution = s->resolv_ctx.requester->resolution;
2750 if (!locked) {
2751 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2752 locked = 1;
2753 }
2754
2755 if (resolution->step == RSLV_STEP_RUNNING)
2756 goto yield;
2757 if (resolution->step == RSLV_STEP_NONE) {
2758 /* We update the variable only if we have a valid response. */
2759 if (resolution->status == RSLV_STATUS_VALID) {
2760 struct sample smp;
2761 short ip_sin_family = 0;
2762 void *ip = NULL;
2763
2764 resolv_get_ip_from_response(&resolution->response, rule->arg.resolv.opts, NULL,
2765 0, &ip, &ip_sin_family, NULL);
2766
2767 switch (ip_sin_family) {
2768 case AF_INET:
2769 smp.data.type = SMP_T_IPV4;
2770 memcpy(&smp.data.u.ipv4, ip, 4);
2771 break;
2772 case AF_INET6:
2773 smp.data.type = SMP_T_IPV6;
2774 memcpy(&smp.data.u.ipv6, ip, 16);
2775 break;
2776 default:
2777 ip = NULL;
2778 }
2779
2780 if (ip) {
2781 smp.px = px;
2782 smp.sess = sess;
2783 smp.strm = s;
2784
2785 vars_set_by_name(rule->arg.resolv.varname, strlen(rule->arg.resolv.varname), &smp);
2786 }
2787 }
2788 }
2789
2790 goto release_requester;
2791 }
2792
2793 /* need to configure and start a new DNS resolution */
2794 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.resolv.expr, SMP_T_STR);
2795 if (smp == NULL)
2796 goto end;
2797
2798 fqdn = smp->data.u.str.area;
2799 if (action_prepare_for_resolution(s, fqdn) == -1)
2800 goto end; /* on error, ignore the action */
2801
2802 s->resolv_ctx.parent = rule;
2803
2804 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2805 locked = 1;
2806
2807 resolv_link_resolution(s, OBJ_TYPE_STREAM, 0);
2808
2809 /* Check if there is a fresh enough response in the cache of our associated resolution */
2810 req = s->resolv_ctx.requester;
2811 if (!req || !req->resolution)
2812 goto release_requester; /* on error, ignore the action */
2813 res = req->resolution;
2814
2815 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2816 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2817 && !tick_is_expired(exp, now_ms)) {
2818 goto use_cache;
2819 }
2820
2821 resolv_trigger_resolution(s->resolv_ctx.requester);
2822
2823 yield:
2824 if (flags & ACT_OPT_FINAL)
2825 goto release_requester;
2826 ret = ACT_RET_YIELD;
2827
2828 end:
2829 if (locked)
2830 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2831 return ret;
2832
2833 release_requester:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002834 ha_free(&s->resolv_ctx.hostname_dn);
Emeric Brunc9437992021-02-12 19:42:55 +01002835 s->resolv_ctx.hostname_dn_len = 0;
2836 if (s->resolv_ctx.requester) {
Christopher Faulet0efc0992021-03-11 18:09:53 +01002837 resolv_unlink_resolution(s->resolv_ctx.requester, 0);
Emeric Brunc9437992021-02-12 19:42:55 +01002838 pool_free(resolv_requester_pool, s->resolv_ctx.requester);
2839 s->resolv_ctx.requester = NULL;
2840 }
2841 goto end;
2842}
2843
2844static void release_resolv_action(struct act_rule *rule)
2845{
2846 release_sample_expr(rule->arg.resolv.expr);
2847 free(rule->arg.resolv.varname);
2848 free(rule->arg.resolv.resolvers_id);
2849 free(rule->arg.resolv.opts);
2850}
2851
2852
2853/* parse "do-resolve" action
2854 * This action takes the following arguments:
2855 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2856 *
2857 * - <varName> is the variable name where the result of the DNS resolution will be stored
2858 * (mandatory)
2859 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2860 * (mandatory)
2861 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2862 * (optional), defaults to ipv6
2863 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2864 */
2865enum act_parse_ret resolv_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2866{
2867 int cur_arg;
2868 struct sample_expr *expr;
2869 unsigned int where;
2870 const char *beg, *end;
2871
2872 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2873 cur_arg = *orig_arg - 1;
2874
2875 /* locate varName, which is mandatory */
2876 beg = strchr(args[cur_arg], '(');
2877 if (beg == NULL)
2878 goto do_resolve_parse_error;
2879 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2880 end = strchr(beg, ',');
2881 if (end == NULL)
2882 goto do_resolve_parse_error;
2883 rule->arg.resolv.varname = my_strndup(beg, end - beg);
2884 if (rule->arg.resolv.varname == NULL)
2885 goto do_resolve_parse_error;
2886
2887
2888 /* locate resolversSectionName, which is mandatory.
2889 * Since next parameters are optional, the delimiter may be comma ','
2890 * or closing parenthesis ')'
2891 */
2892 beg = end + 1;
2893 end = strchr(beg, ',');
2894 if (end == NULL)
2895 end = strchr(beg, ')');
2896 if (end == NULL)
2897 goto do_resolve_parse_error;
2898 rule->arg.resolv.resolvers_id = my_strndup(beg, end - beg);
2899 if (rule->arg.resolv.resolvers_id == NULL)
2900 goto do_resolve_parse_error;
2901
2902
2903 rule->arg.resolv.opts = calloc(1, sizeof(*rule->arg.resolv.opts));
2904 if (rule->arg.resolv.opts == NULL)
2905 goto do_resolve_parse_error;
2906
2907 /* Default priority is ipv6 */
2908 rule->arg.resolv.opts->family_prio = AF_INET6;
2909
2910 /* optional arguments accepted for now:
2911 * ipv4 or ipv6
2912 */
2913 while (*end != ')') {
2914 beg = end + 1;
2915 end = strchr(beg, ',');
2916 if (end == NULL)
2917 end = strchr(beg, ')');
2918 if (end == NULL)
2919 goto do_resolve_parse_error;
2920
2921 if (strncmp(beg, "ipv4", end - beg) == 0) {
2922 rule->arg.resolv.opts->family_prio = AF_INET;
2923 }
2924 else if (strncmp(beg, "ipv6", end - beg) == 0) {
2925 rule->arg.resolv.opts->family_prio = AF_INET6;
2926 }
2927 else {
2928 goto do_resolve_parse_error;
2929 }
2930 }
2931
2932 cur_arg = cur_arg + 1;
2933
2934 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
2935 if (!expr)
2936 goto do_resolve_parse_error;
2937
2938
2939 where = 0;
2940 if (px->cap & PR_CAP_FE)
2941 where |= SMP_VAL_FE_HRQ_HDR;
2942 if (px->cap & PR_CAP_BE)
2943 where |= SMP_VAL_BE_HRQ_HDR;
2944
2945 if (!(expr->fetch->val & where)) {
2946 memprintf(err,
2947 "fetch method '%s' extracts information from '%s', none of which is available here",
2948 args[cur_arg-1], sample_src_names(expr->fetch->use));
2949 free(expr);
2950 return ACT_RET_PRS_ERR;
2951 }
2952 rule->arg.resolv.expr = expr;
2953 rule->action = ACT_CUSTOM;
2954 rule->action_ptr = resolv_action_do_resolve;
2955 *orig_arg = cur_arg;
2956
2957 rule->check_ptr = check_action_do_resolve;
2958 rule->release_ptr = release_resolv_action;
2959
2960 return ACT_RET_PRS_OK;
2961
2962 do_resolve_parse_error:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002963 ha_free(&rule->arg.resolv.varname);
2964 ha_free(&rule->arg.resolv.resolvers_id);
Emeric Brunc9437992021-02-12 19:42:55 +01002965 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2966 args[cur_arg]);
2967 return ACT_RET_PRS_ERR;
2968}
2969
2970static struct action_kw_list http_req_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002971 { "do-resolve", resolv_parse_do_resolve, KWF_MATCH_PREFIX },
Emeric Brunc9437992021-02-12 19:42:55 +01002972 { /* END */ }
2973}};
2974
2975INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2976
2977static struct action_kw_list tcp_req_cont_actions = {ILH, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002978 { "do-resolve", resolv_parse_do_resolve, KWF_MATCH_PREFIX },
Emeric Brunc9437992021-02-12 19:42:55 +01002979 { /* END */ }
2980}};
2981
2982INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2983
2984/* Check an "http-request do-resolve" action.
2985 *
2986 * The function returns 1 in success case, otherwise, it returns 0 and err is
2987 * filled.
2988 */
2989int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2990{
2991 struct resolvers *resolvers = NULL;
2992
2993 if (rule->arg.resolv.resolvers_id == NULL) {
2994 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2995 return 0;
2996 }
2997
2998 resolvers = find_resolvers_by_id(rule->arg.resolv.resolvers_id);
2999 if (resolvers == NULL) {
3000 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.resolv.resolvers_id);
3001 return 0;
3002 }
3003 rule->arg.resolv.resolvers = resolvers;
3004
3005 return 1;
3006}
3007
3008void resolvers_setup_proxy(struct proxy *px)
3009{
3010 px->last_change = now.tv_sec;
3011 px->cap = PR_CAP_FE | PR_CAP_BE;
3012 px->maxconn = 0;
3013 px->conn_retries = 1;
3014 px->timeout.server = TICK_ETERNITY;
3015 px->timeout.client = TICK_ETERNITY;
3016 px->timeout.connect = TICK_ETERNITY;
3017 px->accept = NULL;
3018 px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON;
3019 px->bind_proc = 0; /* will be filled by users */
3020}
3021
3022/*
3023 * Parse a <resolvers> section.
3024 * Returns the error code, 0 if OK, or any combination of :
3025 * - ERR_ABORT: must abort ASAP
3026 * - ERR_FATAL: we can continue parsing but not start the service
3027 * - ERR_WARN: a warning has been emitted
3028 * - ERR_ALERT: an alert has been emitted
3029 * Only the two first ones can stop processing, the two others are just
3030 * indicators.
3031 */
3032int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
3033{
3034 const char *err;
3035 int err_code = 0;
3036 char *errmsg = NULL;
3037 struct proxy *p;
3038
3039 if (strcmp(args[0], "resolvers") == 0) { /* new resolvers section */
3040 if (!*args[1]) {
3041 ha_alert("parsing [%s:%d] : missing name for resolvers section.\n", file, linenum);
3042 err_code |= ERR_ALERT | ERR_ABORT;
3043 goto out;
3044 }
3045
3046 err = invalid_char(args[1]);
3047 if (err) {
3048 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3049 file, linenum, *err, args[0], args[1]);
3050 err_code |= ERR_ALERT | ERR_ABORT;
3051 goto out;
3052 }
3053
3054 list_for_each_entry(curr_resolvers, &sec_resolvers, list) {
3055 /* Error if two resolvers owns the same name */
3056 if (strcmp(curr_resolvers->id, args[1]) == 0) {
3057 ha_alert("Parsing [%s:%d]: resolvers '%s' has same name as another resolvers (declared at %s:%d).\n",
3058 file, linenum, args[1], curr_resolvers->conf.file, curr_resolvers->conf.line);
3059 err_code |= ERR_ALERT | ERR_ABORT;
3060 }
3061 }
3062
3063 if ((curr_resolvers = calloc(1, sizeof(*curr_resolvers))) == NULL) {
3064 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3065 err_code |= ERR_ALERT | ERR_ABORT;
3066 goto out;
3067 }
3068
3069 /* allocate new proxy to tcp servers */
3070 p = calloc(1, sizeof *p);
3071 if (!p) {
3072 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3073 err_code |= ERR_ALERT | ERR_FATAL;
3074 goto out;
3075 }
3076
3077 init_new_proxy(p);
3078 resolvers_setup_proxy(p);
3079 p->parent = curr_resolvers;
3080 p->id = strdup(args[1]);
3081 p->conf.args.file = p->conf.file = strdup(file);
3082 p->conf.args.line = p->conf.line = linenum;
3083 curr_resolvers->px = p;
3084
3085 /* default values */
Willy Tarreau2b718102021-04-21 07:32:39 +02003086 LIST_APPEND(&sec_resolvers, &curr_resolvers->list);
Emeric Brunc9437992021-02-12 19:42:55 +01003087 curr_resolvers->conf.file = strdup(file);
3088 curr_resolvers->conf.line = linenum;
3089 curr_resolvers->id = strdup(args[1]);
3090 curr_resolvers->query_ids = EB_ROOT;
3091 /* default maximum response size */
3092 curr_resolvers->accepted_payload_size = 512;
3093 /* default hold period for nx, other, refuse and timeout is 30s */
3094 curr_resolvers->hold.nx = 30000;
3095 curr_resolvers->hold.other = 30000;
3096 curr_resolvers->hold.refused = 30000;
3097 curr_resolvers->hold.timeout = 30000;
3098 curr_resolvers->hold.obsolete = 0;
3099 /* default hold period for valid is 10s */
3100 curr_resolvers->hold.valid = 10000;
3101 curr_resolvers->timeout.resolve = 1000;
3102 curr_resolvers->timeout.retry = 1000;
3103 curr_resolvers->resolve_retries = 3;
3104 LIST_INIT(&curr_resolvers->nameservers);
3105 LIST_INIT(&curr_resolvers->resolutions.curr);
3106 LIST_INIT(&curr_resolvers->resolutions.wait);
3107 HA_SPIN_INIT(&curr_resolvers->lock);
3108 }
3109 else if (strcmp(args[0], "nameserver") == 0) { /* nameserver definition */
3110 struct dns_nameserver *newnameserver = NULL;
3111 struct sockaddr_storage *sk;
3112 int port1, port2;
Emeric Brunc8f3e452021-04-07 16:04:54 +02003113 struct protocol *proto;
Emeric Brunc9437992021-02-12 19:42:55 +01003114
3115 if (!*args[2]) {
3116 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
3117 file, linenum, args[0]);
3118 err_code |= ERR_ALERT | ERR_FATAL;
3119 goto out;
3120 }
3121
3122 err = invalid_char(args[1]);
3123 if (err) {
3124 ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
3125 file, linenum, *err, args[1]);
3126 err_code |= ERR_ALERT | ERR_FATAL;
3127 goto out;
3128 }
3129
3130 list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) {
3131 /* Error if two resolvers owns the same name */
3132 if (strcmp(newnameserver->id, args[1]) == 0) {
3133 ha_alert("Parsing [%s:%d]: nameserver '%s' has same name as another nameserver (declared at %s:%d).\n",
3134 file, linenum, args[1], newnameserver->conf.file, newnameserver->conf.line);
3135 err_code |= ERR_ALERT | ERR_FATAL;
3136 }
3137 }
3138
Emeric Brunc8f3e452021-04-07 16:04:54 +02003139 sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto,
3140 &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 +01003141 if (!sk) {
3142 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3143 err_code |= ERR_ALERT | ERR_FATAL;
3144 goto out;
3145 }
3146
3147 if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) {
3148 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3149 err_code |= ERR_ALERT | ERR_ABORT;
3150 goto out;
3151 }
3152
Emeric Brunc8f3e452021-04-07 16:04:54 +02003153 if (proto && proto->ctrl_type == SOCK_STREAM) {
3154 err_code |= parse_server(file, linenum, args, curr_resolvers->px, NULL,
3155 SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
3156 if (err_code & (ERR_FATAL|ERR_ABORT)) {
3157 err_code |= ERR_ABORT;
3158 goto out;
3159 }
3160
3161 if (dns_stream_init(newnameserver, curr_resolvers->px->srv) < 0) {
3162 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3163 err_code |= ERR_ALERT|ERR_ABORT;
3164 goto out;
3165 }
3166 }
3167 else if (dns_dgram_init(newnameserver, sk) < 0) {
Emeric Brunc9437992021-02-12 19:42:55 +01003168 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3169 err_code |= ERR_ALERT | ERR_ABORT;
3170 goto out;
3171 }
3172
3173 if ((newnameserver->conf.file = strdup(file)) == NULL) {
3174 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3175 err_code |= ERR_ALERT | ERR_ABORT;
3176 goto out;
3177 }
3178
3179 if ((newnameserver->id = strdup(args[1])) == NULL) {
3180 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3181 err_code |= ERR_ALERT | ERR_ABORT;
3182 goto out;
3183 }
3184
3185 newnameserver->parent = curr_resolvers;
3186 newnameserver->process_responses = resolv_process_responses;
3187 newnameserver->conf.line = linenum;
3188 /* the nameservers are linked backward first */
Willy Tarreau2b718102021-04-21 07:32:39 +02003189 LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list);
Emeric Brunc9437992021-02-12 19:42:55 +01003190 }
3191 else if (strcmp(args[0], "parse-resolv-conf") == 0) {
3192 struct dns_nameserver *newnameserver = NULL;
3193 const char *whitespace = "\r\n\t ";
3194 char *resolv_line = NULL;
3195 int resolv_linenum = 0;
3196 FILE *f = NULL;
3197 char *address = NULL;
3198 struct sockaddr_storage *sk = NULL;
3199 struct protocol *proto;
3200 int duplicate_name = 0;
3201
3202 if ((resolv_line = malloc(sizeof(*resolv_line) * LINESIZE)) == NULL) {
3203 ha_alert("parsing [%s:%d] : out of memory.\n",
3204 file, linenum);
3205 err_code |= ERR_ALERT | ERR_FATAL;
3206 goto resolv_out;
3207 }
3208
3209 if ((f = fopen("/etc/resolv.conf", "r")) == NULL) {
3210 ha_alert("parsing [%s:%d] : failed to open /etc/resolv.conf.\n",
3211 file, linenum);
3212 err_code |= ERR_ALERT | ERR_FATAL;
3213 goto resolv_out;
3214 }
3215
3216 sk = calloc(1, sizeof(*sk));
3217 if (sk == NULL) {
3218 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n",
3219 resolv_linenum);
3220 err_code |= ERR_ALERT | ERR_FATAL;
3221 goto resolv_out;
3222 }
3223
3224 while (fgets(resolv_line, LINESIZE, f) != NULL) {
3225 resolv_linenum++;
3226 if (strncmp(resolv_line, "nameserver", 10) != 0)
3227 continue;
3228
3229 address = strtok(resolv_line + 10, whitespace);
3230 if (address == resolv_line + 10)
3231 continue;
3232
3233 if (address == NULL) {
3234 ha_warning("parsing [/etc/resolv.conf:%d] : nameserver line is missing address.\n",
3235 resolv_linenum);
3236 err_code |= ERR_WARN;
3237 continue;
3238 }
3239
3240 duplicate_name = 0;
3241 list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) {
3242 if (strcmp(newnameserver->id, address) == 0) {
3243 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",
3244 resolv_linenum, address, newnameserver->conf.file, newnameserver->conf.line);
3245 err_code |= ERR_WARN;
3246 duplicate_name = 1;
3247 }
3248 }
3249
3250 if (duplicate_name)
3251 continue;
3252
3253 memset(sk, 0, sizeof(*sk));
3254 if (!str2ip2(address, sk, 1)) {
3255 ha_warning("parsing [/etc/resolv.conf:%d] : address '%s' could not be recognized, nameserver will be excluded.\n",
3256 resolv_linenum, address);
3257 err_code |= ERR_WARN;
3258 continue;
3259 }
3260
3261 set_host_port(sk, 53);
3262
3263 proto = protocol_by_family(sk->ss_family);
3264 if (!proto || !proto->connect) {
3265 ha_warning("parsing [/etc/resolv.conf:%d] : '%s' : connect() not supported for this address family.\n",
3266 resolv_linenum, address);
3267 err_code |= ERR_WARN;
3268 continue;
3269 }
3270
3271 if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) {
3272 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3273 err_code |= ERR_ALERT | ERR_FATAL;
3274 goto resolv_out;
3275 }
3276
3277 if (dns_dgram_init(newnameserver, sk) < 0) {
3278 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3279 err_code |= ERR_ALERT | ERR_FATAL;
3280 free(newnameserver);
3281 goto resolv_out;
3282 }
3283
3284 newnameserver->conf.file = strdup("/etc/resolv.conf");
3285 if (newnameserver->conf.file == NULL) {
3286 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3287 err_code |= ERR_ALERT | ERR_FATAL;
3288 free(newnameserver);
3289 goto resolv_out;
3290 }
3291
3292 newnameserver->id = strdup(address);
3293 if (newnameserver->id == NULL) {
3294 ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
3295 err_code |= ERR_ALERT | ERR_FATAL;
3296 free((char *)newnameserver->conf.file);
3297 free(newnameserver);
3298 goto resolv_out;
3299 }
3300
3301 newnameserver->parent = curr_resolvers;
3302 newnameserver->process_responses = resolv_process_responses;
3303 newnameserver->conf.line = resolv_linenum;
Willy Tarreau2b718102021-04-21 07:32:39 +02003304 LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list);
Emeric Brunc9437992021-02-12 19:42:55 +01003305 }
3306
3307resolv_out:
3308 free(sk);
3309 free(resolv_line);
3310 if (f != NULL)
3311 fclose(f);
3312 }
3313 else if (strcmp(args[0], "hold") == 0) { /* hold periods */
3314 const char *res;
3315 unsigned int time;
3316
3317 if (!*args[2]) {
3318 ha_alert("parsing [%s:%d] : '%s' expects an <event> and a <time> as arguments.\n",
3319 file, linenum, args[0]);
3320 ha_alert("<event> can be either 'valid', 'nx', 'refused', 'timeout', or 'other'\n");
3321 err_code |= ERR_ALERT | ERR_FATAL;
3322 goto out;
3323 }
3324 res = parse_time_err(args[2], &time, TIME_UNIT_MS);
3325 if (res == PARSE_TIME_OVER) {
3326 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
3327 file, linenum, args[1], args[0]);
3328 err_code |= ERR_ALERT | ERR_FATAL;
3329 goto out;
3330 }
3331 else if (res == PARSE_TIME_UNDER) {
3332 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
3333 file, linenum, args[1], args[0]);
3334 err_code |= ERR_ALERT | ERR_FATAL;
3335 goto out;
3336 }
3337 else if (res) {
3338 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s>.\n",
3339 file, linenum, *res, args[0]);
3340 err_code |= ERR_ALERT | ERR_FATAL;
3341 goto out;
3342 }
3343 if (strcmp(args[1], "nx") == 0)
3344 curr_resolvers->hold.nx = time;
3345 else if (strcmp(args[1], "other") == 0)
3346 curr_resolvers->hold.other = time;
3347 else if (strcmp(args[1], "refused") == 0)
3348 curr_resolvers->hold.refused = time;
3349 else if (strcmp(args[1], "timeout") == 0)
3350 curr_resolvers->hold.timeout = time;
3351 else if (strcmp(args[1], "valid") == 0)
3352 curr_resolvers->hold.valid = time;
3353 else if (strcmp(args[1], "obsolete") == 0)
3354 curr_resolvers->hold.obsolete = time;
3355 else {
3356 ha_alert("parsing [%s:%d] : '%s' unknown <event>: '%s', expects either 'nx', 'timeout', 'valid', 'obsolete' or 'other'.\n",
3357 file, linenum, args[0], args[1]);
3358 err_code |= ERR_ALERT | ERR_FATAL;
3359 goto out;
3360 }
3361
3362 }
3363 else if (strcmp(args[0], "accepted_payload_size") == 0) {
3364 int i = 0;
3365
3366 if (!*args[1]) {
3367 ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n",
3368 file, linenum, args[0]);
3369 err_code |= ERR_ALERT | ERR_FATAL;
3370 goto out;
3371 }
3372
3373 i = atoi(args[1]);
3374 if (i < DNS_HEADER_SIZE || i > DNS_MAX_UDP_MESSAGE) {
3375 ha_alert("parsing [%s:%d] : '%s' must be between %d and %d inclusive (was %s).\n",
3376 file, linenum, args[0], DNS_HEADER_SIZE, DNS_MAX_UDP_MESSAGE, args[1]);
3377 err_code |= ERR_ALERT | ERR_FATAL;
3378 goto out;
3379 }
3380
3381 curr_resolvers->accepted_payload_size = i;
3382 }
3383 else if (strcmp(args[0], "resolution_pool_size") == 0) {
3384 ha_alert("parsing [%s:%d] : '%s' directive is not supported anymore (it never appeared in a stable release).\n",
3385 file, linenum, args[0]);
3386 err_code |= ERR_ALERT | ERR_FATAL;
3387 goto out;
3388 }
3389 else if (strcmp(args[0], "resolve_retries") == 0) {
3390 if (!*args[1]) {
3391 ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n",
3392 file, linenum, args[0]);
3393 err_code |= ERR_ALERT | ERR_FATAL;
3394 goto out;
3395 }
3396 curr_resolvers->resolve_retries = atoi(args[1]);
3397 }
3398 else if (strcmp(args[0], "timeout") == 0) {
3399 if (!*args[1]) {
3400 ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments.\n",
3401 file, linenum, args[0]);
3402 err_code |= ERR_ALERT | ERR_FATAL;
3403 goto out;
3404 }
3405 else if (strcmp(args[1], "retry") == 0 ||
3406 strcmp(args[1], "resolve") == 0) {
3407 const char *res;
3408 unsigned int tout;
3409
3410 if (!*args[2]) {
3411 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
3412 file, linenum, args[0], args[1]);
3413 err_code |= ERR_ALERT | ERR_FATAL;
3414 goto out;
3415 }
3416 res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
3417 if (res == PARSE_TIME_OVER) {
3418 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3419 file, linenum, args[2], args[0], args[1]);
3420 err_code |= ERR_ALERT | ERR_FATAL;
3421 goto out;
3422 }
3423 else if (res == PARSE_TIME_UNDER) {
3424 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3425 file, linenum, args[2], args[0], args[1]);
3426 err_code |= ERR_ALERT | ERR_FATAL;
3427 goto out;
3428 }
3429 else if (res) {
3430 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
3431 file, linenum, *res, args[0], args[1]);
3432 err_code |= ERR_ALERT | ERR_FATAL;
3433 goto out;
3434 }
3435 if (args[1][2] == 't')
3436 curr_resolvers->timeout.retry = tout;
3437 else
3438 curr_resolvers->timeout.resolve = tout;
3439 }
3440 else {
3441 ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments got '%s'.\n",
3442 file, linenum, args[0], args[1]);
3443 err_code |= ERR_ALERT | ERR_FATAL;
3444 goto out;
3445 }
3446 }
3447 else if (*args[0] != 0) {
3448 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
3449 err_code |= ERR_ALERT | ERR_FATAL;
3450 goto out;
3451 }
3452
3453 out:
3454 free(errmsg);
3455 return err_code;
3456}
Emeric Brun56fc5d92021-02-12 20:05:45 +01003457int cfg_post_parse_resolvers()
3458{
3459 int err_code = 0;
3460 struct server *srv;
3461
3462 if (curr_resolvers) {
3463
3464 /* prepare forward server descriptors */
3465 if (curr_resolvers->px) {
3466 srv = curr_resolvers->px->srv;
3467 while (srv) {
Emeric Brun56fc5d92021-02-12 20:05:45 +01003468 /* init ssl if needed */
3469 if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
3470 if (xprt_get(XPRT_SSL)->prepare_srv(srv)) {
3471 ha_alert("unable to prepare SSL for server '%s' in resolvers section '%s'.\n", srv->id, curr_resolvers->id);
3472 err_code |= ERR_ALERT | ERR_FATAL;
3473 break;
3474 }
3475 }
Emeric Brun56fc5d92021-02-12 20:05:45 +01003476 srv = srv->next;
3477 }
3478 }
3479 }
3480 curr_resolvers = NULL;
3481 return err_code;
3482}
Emeric Brunc9437992021-02-12 19:42:55 +01003483
Emeric Brun56fc5d92021-02-12 20:05:45 +01003484REGISTER_CONFIG_SECTION("resolvers", cfg_parse_resolvers, cfg_post_parse_resolvers);
Emeric Brunc9437992021-02-12 19:42:55 +01003485REGISTER_POST_DEINIT(resolvers_deinit);
3486REGISTER_CONFIG_POSTPARSER("dns runtime resolver", resolvers_finalize_config);