blob: 8eddde9478dfd12d20ae60a44abbbb90e824a42c [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Willy Tarreau122eba92020-06-04 10:15:32 +020022#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Baptiste Assmann044fd5b2018-08-10 10:56:38 +020024#include <common/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020025#include <haproxy/check.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020026#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020028#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/errors.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020033#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020034#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020035#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020037#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020038#include <haproxy/tcp_rules.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020039#include <haproxy/time.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020040#include <haproxy/ticks.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020041#include <haproxy/net_helper.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020042#include <haproxy/vars.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020043
Willy Tarreauf268ee82020-06-04 17:05:57 +020044#include <haproxy/global.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020045
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020046#include <haproxy/fd.h>
Willy Tarreau832ce652020-06-04 08:36:05 +020047#include <haproxy/proto_udp.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020048
Christopher Faulet67957bd2017-09-27 11:00:59 +020049struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
50struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020051
Tim Duesterhusfcac33d2020-01-18 02:04:12 +010052static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010053
54DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
55DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010056DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010057
Christopher Faulet67957bd2017-09-27 11:00:59 +020058static unsigned int resolution_uuid = 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +010059unsigned int dns_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020060
Christopher Faulet67957bd2017-09-27 11:00:59 +020061/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
62 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020063 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020064struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065{
Christopher Faulet67957bd2017-09-27 11:00:59 +020066 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020067
Christopher Faulet67957bd2017-09-27 11:00:59 +020068 list_for_each_entry(res, &dns_resolvers, list) {
69 if (!strcmp(res->id, id))
70 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020072 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073}
74
Olivier Houchardb17b8842020-04-01 18:30:27 +020075/* Compare hostnames in a case-insensitive way .
76 * Returns 0 if they are the same, non-zero otherwise
77 */
78static __inline int dns_hostname_cmp(const char *name1, const char *name2, int len)
79{
80 int i;
81
82 for (i = 0; i < len; i++)
83 if (tolower(name1[i]) != tolower(name2[i]))
84 return -1;
85 return 0;
86}
87
Christopher Faulet67957bd2017-09-27 11:00:59 +020088/* Returns a pointer on the SRV request matching the name <name> for the proxy
89 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020090 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020091struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020092{
Christopher Faulet67957bd2017-09-27 11:00:59 +020093 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020094
Christopher Faulet67957bd2017-09-27 11:00:59 +020095 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
96 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
97 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020098 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020099 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200100}
101
Christopher Faulet67957bd2017-09-27 11:00:59 +0200102/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
103 * NULL if an error occurred. */
104struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200105{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200106 struct proxy *px = srv->proxy;
107 struct dns_srvrq *srvrq = NULL;
108 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200109
Christopher Faulet67957bd2017-09-27 11:00:59 +0200110 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200111 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
112 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200113 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100114 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
115 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200116 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200117 }
118
Christopher Faulet67957bd2017-09-27 11:00:59 +0200119 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100120 ha_alert("config : %s '%s', server '%s': out of memory\n",
121 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200122 goto err;
123 }
124 srvrq->obj_type = OBJ_TYPE_SRVRQ;
125 srvrq->proxy = px;
126 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200127 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200128 srvrq->hostname_dn_len = hostname_dn_len;
129 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100130 ha_alert("config : %s '%s', server '%s': out of memory\n",
131 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200132 goto err;
133 }
134 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
135 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136
Christopher Faulet67957bd2017-09-27 11:00:59 +0200137 err:
138 if (srvrq) {
139 free(srvrq->name);
140 free(srvrq->hostname_dn);
141 free(srvrq);
142 }
143 return NULL;
144}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200145
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200146
Christopher Faulet67957bd2017-09-27 11:00:59 +0200147/* 2 bytes random generator to generate DNS query ID */
148static inline uint16_t dns_rnd16(void)
149{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200150 if (!dns_query_id_seed)
151 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200152 dns_query_id_seed ^= dns_query_id_seed << 13;
153 dns_query_id_seed ^= dns_query_id_seed >> 7;
154 dns_query_id_seed ^= dns_query_id_seed << 17;
155 return dns_query_id_seed;
156}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200157
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200158
Christopher Faulet67957bd2017-09-27 11:00:59 +0200159static inline int dns_resolution_timeout(struct dns_resolution *res)
160{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100161 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200162}
163
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164/* Updates a resolvers' task timeout for next wake up and queue it */
165static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200166{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167 struct dns_resolution *res;
168 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200169
Christopher Faulet67957bd2017-09-27 11:00:59 +0200170 next = tick_add(now_ms, resolvers->timeout.resolve);
171 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
172 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
173 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
174 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200175
Christopher Faulet67957bd2017-09-27 11:00:59 +0200176 list_for_each_entry(res, &resolvers->resolutions.wait, list)
177 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200178
Christopher Faulet67957bd2017-09-27 11:00:59 +0200179 resolvers->t->expire = next;
180 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200181}
182
Christopher Faulet67957bd2017-09-27 11:00:59 +0200183/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
184 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200185 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200186static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200187{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200188 struct dgram_conn *dgram = ns->dgram;
189 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200190
Christopher Faulet67957bd2017-09-27 11:00:59 +0200191 /* Already connected */
192 if (dgram->t.sock.fd != -1)
193 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200194
Christopher Faulet67957bd2017-09-27 11:00:59 +0200195 /* Create an UDP socket and connect it on the nameserver's IP/Port */
196 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
197 send_log(NULL, LOG_WARNING,
198 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
199 ns->resolvers->id, ns->id);
200 return -1;
201 }
202 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
203 send_log(NULL, LOG_WARNING,
204 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
205 ns->resolvers->id, ns->id);
206 close(fd);
207 return -1;
208 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200209
Christopher Faulet67957bd2017-09-27 11:00:59 +0200210 /* Make the socket non blocking */
211 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200212
Christopher Faulet67957bd2017-09-27 11:00:59 +0200213 /* Add the fd in the fd list and update its parameters */
214 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100215 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200216 fd_want_recv(fd);
217 return 0;
218}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200219
Christopher Faulet67957bd2017-09-27 11:00:59 +0200220/* Forges a DNS query. It needs the following information from the caller:
221 * - <query_id> : the DNS query id corresponding to this query
222 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
223 * - <hostname_dn> : hostname in domain name format
224 * - <hostname_dn_len> : length of <hostname_dn>
225 *
226 * To store the query, the caller must pass a buffer <buf> and its size
227 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
228 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200229 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200230static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
231 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200232{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200233 struct dns_header dns_hdr;
234 struct dns_question qinfo;
235 struct dns_additional_record edns;
236 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200237
Christopher Faulet67957bd2017-09-27 11:00:59 +0200238 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
239 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200240
Christopher Faulet67957bd2017-09-27 11:00:59 +0200241 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200242
Christopher Faulet67957bd2017-09-27 11:00:59 +0200243 /* Set dns query headers */
244 dns_hdr.id = (unsigned short) htons(query_id);
245 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
246 dns_hdr.qdcount = htons(1); /* 1 question */
247 dns_hdr.ancount = 0;
248 dns_hdr.nscount = 0;
249 dns_hdr.arcount = htons(1);
250 memcpy(p, &dns_hdr, sizeof(dns_hdr));
251 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200252
Christopher Faulet67957bd2017-09-27 11:00:59 +0200253 /* Set up query hostname */
254 memcpy(p, hostname_dn, hostname_dn_len);
255 p += hostname_dn_len;
256 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200257
Christopher Faulet67957bd2017-09-27 11:00:59 +0200258 /* Set up query info (type and class) */
259 qinfo.qtype = htons(query_type);
260 qinfo.qclass = htons(DNS_RCLASS_IN);
261 memcpy(p, &qinfo, sizeof(qinfo));
262 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200263
Christopher Faulet67957bd2017-09-27 11:00:59 +0200264 /* Set the DNS extension */
265 edns.name = 0;
266 edns.type = htons(DNS_RTYPE_OPT);
267 edns.udp_payload_size = htons(accepted_payload_size);
268 edns.extension = 0;
269 edns.data_length = 0;
270 memcpy(p, &edns, sizeof(edns));
271 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200272
Christopher Faulet67957bd2017-09-27 11:00:59 +0200273 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200274}
275
Christopher Faulet67957bd2017-09-27 11:00:59 +0200276/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
277 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200278 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200279static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281 struct dns_resolvers *resolvers = resolution->resolvers;
282 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100283 int len;
284
285 /* Update resolution */
286 resolution->nb_queries = 0;
287 resolution->nb_responses = 0;
288 resolution->last_query = now_ms;
289
290 len = dns_build_query(resolution->query_id, resolution->query_type,
291 resolvers->accepted_payload_size,
292 resolution->hostname_dn, resolution->hostname_dn_len,
293 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200294
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295 list_for_each_entry(ns, &resolvers->nameservers, list) {
296 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100297 int ret;
298
Christopher Faulet67957bd2017-09-27 11:00:59 +0200299 if (fd == -1) {
300 if (dns_connect_namesaver(ns) == -1)
301 continue;
302 fd = ns->dgram->t.sock.fd;
303 resolvers->nb_nameservers++;
304 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200305
Willy Tarreau0eae6322019-12-20 11:18:54 +0100306 if (len < 0)
307 goto snd_error;
308
309 ret = send(fd, trash.area, len, 0);
310 if (ret == len) {
311 ns->counters.sent++;
312 resolution->nb_queries++;
313 continue;
314 }
315
316 if (ret == -1 && errno == EAGAIN) {
317 /* retry once the socket is ready */
318 fd_cant_send(fd);
319 continue;
320 }
321
322 snd_error:
323 ns->counters.snd_error++;
324 resolution->nb_queries++;
325 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200326
Christopher Faulet67957bd2017-09-27 11:00:59 +0200327 /* Push the resolution at the end of the active list */
328 LIST_DEL(&resolution->list);
329 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
330 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200331}
332
Christopher Faulet67957bd2017-09-27 11:00:59 +0200333/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
334 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200335 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200336static int
337dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200339 struct dns_resolvers *resolvers = resolution->resolvers;
340 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200341
Christopher Faulet67957bd2017-09-27 11:00:59 +0200342 /* Avoid sending requests for resolutions that don't yet have an
343 * hostname, ie resolutions linked to servers that do not yet have an
344 * fqdn */
345 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200346 return 0;
347
Christopher Faulet67957bd2017-09-27 11:00:59 +0200348 /* Check if a resolution has already been started for this server return
349 * directly to avoid resolution pill up. */
350 if (resolution->step != RSLV_STEP_NONE)
351 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200352
Christopher Faulet67957bd2017-09-27 11:00:59 +0200353 /* Generates a new query id. We try at most 100 times to find a free
354 * query id */
355 for (i = 0; i < 100; ++i) {
356 query_id = dns_rnd16();
357 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200358 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200360 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200361 if (query_id == -1) {
362 send_log(NULL, LOG_NOTICE,
363 "could not generate a query id for %s, in resolvers %s.\n",
364 resolution->hostname_dn, resolvers->id);
365 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200366 }
367
Christopher Faulet67957bd2017-09-27 11:00:59 +0200368 /* Update resolution parameters */
369 resolution->query_id = query_id;
370 resolution->qid.key = query_id;
371 resolution->step = RSLV_STEP_RUNNING;
372 resolution->query_type = resolution->prefered_query_type;
373 resolution->try = resolvers->resolve_retries;
374 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200375
Christopher Faulet67957bd2017-09-27 11:00:59 +0200376 /* Send the DNS query */
377 resolution->try -= 1;
378 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200379 return 1;
380}
381
Christopher Faulet67957bd2017-09-27 11:00:59 +0200382/* Performs a name resolution for the requester <req> */
383void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200384{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200385 struct dns_resolvers *resolvers;
386 struct dns_resolution *res;
387 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200388
Christopher Faulet67957bd2017-09-27 11:00:59 +0200389 if (!req || !req->resolution)
390 return;
391 res = req->resolution;
392 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200393
Christopher Faulet67957bd2017-09-27 11:00:59 +0200394 /* The resolution must not be triggered yet. Use the cached response, if
395 * valid */
396 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200397 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
398 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
399 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200400}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200401
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200402
Christopher Faulet67957bd2017-09-27 11:00:59 +0200403/* Resets some resolution parameters to initial values and also delete the query
404 * ID from the resolver's tree.
405 */
406static void dns_reset_resolution(struct dns_resolution *resolution)
407{
408 /* update resolution status */
409 resolution->step = RSLV_STEP_NONE;
410 resolution->try = 0;
411 resolution->last_resolution = now_ms;
412 resolution->nb_queries = 0;
413 resolution->nb_responses = 0;
414 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200415
Christopher Faulet67957bd2017-09-27 11:00:59 +0200416 /* clean up query id */
417 eb32_delete(&resolution->qid);
418 resolution->query_id = 0;
419 resolution->qid.key = 0;
420}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200421
Christopher Faulet67957bd2017-09-27 11:00:59 +0200422/* Returns the query id contained in a DNS response */
423static inline unsigned short dns_response_get_query_id(unsigned char *resp)
424{
425 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200426}
427
Christopher Faulet67957bd2017-09-27 11:00:59 +0200428
429/* Analyses, re-builds and copies the name <name> from the DNS response packet
430 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
431 * for compressed data. The result is copied into <dest>, ensuring we don't
432 * overflow using <dest_len> Returns the number of bytes the caller can move
433 * forward. If 0 it means an error occurred while parsing the name. <offset> is
434 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200435 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200436int dns_read_name(unsigned char *buffer, unsigned char *bufend,
437 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100438 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200439{
440 int nb_bytes = 0, n = 0;
441 int label_len;
442 unsigned char *reader = name;
443 char *dest = destination;
444
445 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100446 if (reader >= bufend)
447 goto err;
448
Christopher Faulet67957bd2017-09-27 11:00:59 +0200449 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200450 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100451 if (reader + 1 >= bufend)
452 goto err;
453
Christopher Faulet67957bd2017-09-27 11:00:59 +0200454 /* Must point BEFORE current position */
455 if ((buffer + reader[1]) > reader)
456 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200457
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100458 if (depth++ > 100)
459 goto err;
460
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530461 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100462 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200463 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200464 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200465
Christopher Faulet67957bd2017-09-27 11:00:59 +0200466 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200467 nb_bytes += n;
468 goto out;
469 }
470
471 label_len = *reader;
472 if (label_len == 0)
473 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200474
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200475 /* Check if:
476 * - we won't read outside the buffer
477 * - there is enough place in the destination
478 */
479 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200480 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200481
482 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200483 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200484
485 memcpy(dest, reader, label_len);
486
Christopher Faulet67957bd2017-09-27 11:00:59 +0200487 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200488 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200489 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200490 }
491
Christopher Faulet67957bd2017-09-27 11:00:59 +0200492 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200493 /* offset computation:
494 * parse from <name> until finding either NULL or a pointer "c0xx"
495 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200496 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200497 *offset = 0;
498 while (reader < bufend) {
499 if ((reader[0] & 0xc0) == 0xc0) {
500 *offset += 2;
501 break;
502 }
503 else if (*reader == 0) {
504 *offset += 1;
505 break;
506 }
507 *offset += 1;
508 ++reader;
509 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200510 return nb_bytes;
511
Christopher Faulet67957bd2017-09-27 11:00:59 +0200512 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200513 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200514}
515
Christopher Faulet67957bd2017-09-27 11:00:59 +0200516/* Checks for any obsolete record, also identify any SRV request, and try to
517 * find a corresponding server.
518*/
519static void dns_check_dns_response(struct dns_resolution *res)
520{
521 struct dns_resolvers *resolvers = res->resolvers;
522 struct dns_requester *req, *reqback;
523 struct dns_answer_item *item, *itemback;
524 struct server *srv;
525 struct dns_srvrq *srvrq;
526
Baptiste Assmann13a92322019-06-07 09:40:55 +0200527 /* clean up obsolete Additional records */
528 list_for_each_entry_safe(item, itemback, &res->response.ar_list, list) {
529 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
530 LIST_DEL(&item->list);
531 pool_free(dns_answer_item_pool, item);
532 }
533 }
534
Christopher Faulet67957bd2017-09-27 11:00:59 +0200535 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
536
537 /* Remove obsolete items */
538 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
539 if (item->type != DNS_RTYPE_SRV)
540 goto rm_obselete_item;
541
542 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
543 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
544 continue;
545
546 /* Remove any associated server */
547 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100548 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200549 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
550 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200551 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200552 snr_update_srv_status(srv, 1);
553 free(srv->hostname);
554 free(srv->hostname_dn);
555 srv->hostname = NULL;
556 srv->hostname_dn = NULL;
557 srv->hostname_dn_len = 0;
558 dns_unlink_resolution(srv->dns_requester);
559 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200561 }
562 }
563
564 rm_obselete_item:
565 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100566 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200567 continue;
568 }
569
570 if (item->type != DNS_RTYPE_SRV)
571 continue;
572
573 /* Now process SRV records */
574 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
575 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
576 continue;
577
578 /* Check if a server already uses that hostname */
579 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100580 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200581 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
582 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200583 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len) &&
Daniel Corbettf8716912019-11-17 09:48:56 -0500584 !srv->dns_opts.ignore_weight) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100585 int ha_weight;
586
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200587 /* DNS weight range if from 0 to 65535
588 * HAProxy weight is from 0 to 256
589 * The rule below ensures that weight 0 is well respected
590 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100591 */
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200592 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100593 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200594 char weight[9];
595
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100596 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200597 server_parse_weight_change_request(srv, weight);
598 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100599 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200600 break;
601 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100602 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200603 }
604 if (srv)
605 continue;
606
607 /* If not, try to find a server with undefined hostname */
608 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100609 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200610 if (srv->srvrq == srvrq && !srv->hostname_dn)
611 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100612 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200613 }
614 /* And update this server, if found */
615 if (srv) {
616 const char *msg = NULL;
617 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100618 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200619 char hostname[DNS_MAX_NAME_SIZE];
620
621 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100622 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100623 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200624 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100625 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200626
627 /* Check if an Additional Record is associated to this SRV record.
628 * Perform some sanity checks too to ensure the record can be used.
629 * If all fine, we simply pick up the IP address found and associate
630 * it to the server.
631 */
632 if ((item->ar_item != NULL) &&
633 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
634 {
635
636 switch (item->ar_item->type) {
637 case DNS_RTYPE_A:
638 update_server_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional recrd");
639 break;
640 case DNS_RTYPE_AAAA:
641 update_server_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional recrd");
642 break;
643 }
644
645 srv->flags |= SRV_F_NO_RESOLUTION;
646 }
647
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100648 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200649 if (msg)
650 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
651
652 srv->svc_port = item->port;
653 srv->flags &= ~SRV_F_MAPPORTS;
654 if ((srv->check.state & CHK_ST_CONFIGURED) &&
655 !(srv->flags & SRV_F_CHECKPORT))
656 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100657
Daniel Corbettf8716912019-11-17 09:48:56 -0500658 if (!srv->dns_opts.ignore_weight) {
659 /* DNS weight range if from 0 to 65535
660 * HAProxy weight is from 0 to 256
661 * The rule below ensures that weight 0 is well respected
662 * while allowing a "mapping" from DNS weight into HAProxy's one.
663 */
664 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100665
Daniel Corbettf8716912019-11-17 09:48:56 -0500666 snprintf(weight, sizeof(weight), "%d", ha_weight);
667 server_parse_weight_change_request(srv, weight);
668 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100669 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200670 }
671 }
672 }
673}
674
675/* Validates that the buffer DNS response provided in <resp> and finishing
676 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200677 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200678 * The result is stored in <resolution>' response, buf_response,
679 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200680 *
681 * This function returns one of the DNS_RESP_* code to indicate the type of
682 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200683 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200684static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
685 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200686{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200687 unsigned char *reader;
688 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200689 int len, flags, offset;
690 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200691 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200692 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200693 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200694 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200695 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200696
Christopher Faulet67957bd2017-09-27 11:00:59 +0200697 reader = resp;
698 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200699 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200700 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200701
Christopher Faulet67957bd2017-09-27 11:00:59 +0200702 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200703 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200704
705 /* query id */
706 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200707 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200708 dns_p->header.id = reader[0] * 256 + reader[1];
709 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200710
Christopher Faulet67957bd2017-09-27 11:00:59 +0200711 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200712 * First byte contains:
713 * - response flag (1 bit)
714 * - opcode (4 bits)
715 * - authoritative (1 bit)
716 * - truncated (1 bit)
717 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200718 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200719 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200720 return DNS_RESP_INVALID;
721
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200722 flags = reader[0] * 256 + reader[1];
723
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200724 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
725 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200726 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200727 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200728 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200729 return DNS_RESP_ERROR;
730 }
731
Christopher Faulet67957bd2017-09-27 11:00:59 +0200732 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200733 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200734
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200735 /* 2 bytes for question count */
736 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200737 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200738 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200739 /* (for now) we send one query only, so we expect only one in the
740 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200741 if (dns_p->header.qdcount != 1)
742 return DNS_RESP_QUERY_COUNT_ERROR;
743 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200744 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200745 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200746
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200747 /* 2 bytes for answer count */
748 if (reader + 2 >= bufend)
749 return DNS_RESP_INVALID;
750 dns_p->header.ancount = reader[0] * 256 + reader[1];
751 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200753 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200754 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200755 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200756 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200757
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 /* 2 bytes authority count */
759 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200760 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200761 dns_p->header.nscount = reader[0] * 256 + reader[1];
762 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200763
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 /* 2 bytes additional count */
765 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200766 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200767 dns_p->header.arcount = reader[0] * 256 + reader[1];
768 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200769
Christopher Faulet67957bd2017-09-27 11:00:59 +0200770 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200771 LIST_INIT(&dns_p->query_list);
772 for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200773 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200774 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200775 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200776 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
777 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200778 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200780
Christopher Faulet67957bd2017-09-27 11:00:59 +0200781 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200782 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200783 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200784 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100785 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200786
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200787 if (len == 0)
788 return DNS_RESP_INVALID;
789
790 reader += offset;
791 previous_dname = dns_query->name;
792
793 /* move forward 2 bytes for question type */
794 if (reader + 2 >= bufend)
795 return DNS_RESP_INVALID;
796 dns_query->type = reader[0] * 256 + reader[1];
797 reader += 2;
798
799 /* move forward 2 bytes for question class */
800 if (reader + 2 >= bufend)
801 return DNS_RESP_INVALID;
802 dns_query->class = reader[0] * 256 + reader[1];
803 reader += 2;
804 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200805
Baptiste Assmann251abb92017-08-11 09:58:27 +0200806 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200807 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200808 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
809 return DNS_RESP_TRUNCATED;
810
Baptiste Assmann325137d2015-04-13 23:40:55 +0200811 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200812 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200813 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200814 if (reader >= bufend)
815 return DNS_RESP_INVALID;
816
Willy Tarreaubafbe012017-11-24 17:34:44 +0100817 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200818 if (dns_answer_record == NULL)
819 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200820
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200821 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100822 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200823
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200824 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100825 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200826 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200827 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200828
Christopher Faulet67957bd2017-09-27 11:00:59 +0200829 /* Check if the current record dname is valid. previous_dname
830 * points either to queried dname or last CNAME target */
Olivier Houchardb17b8842020-04-01 18:30:27 +0200831 if (dns_query->type != DNS_RTYPE_SRV && dns_hostname_cmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100832 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200833 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200834 /* First record, means a mismatch issue between
835 * queried dname and dname found in the first
836 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200837 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200838 }
839 else {
840 /* If not the first record, this means we have a
841 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200842 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200843 }
844
Baptiste Assmann325137d2015-04-13 23:40:55 +0200845 }
846
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200847 memcpy(dns_answer_record->name, tmpname, len);
848 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200849
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200850 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200851 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100852 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200853 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200854 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200855
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200856 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200857 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100858 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200859 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200860 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200861 dns_answer_record->type = reader[0] * 256 + reader[1];
862 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200863
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200864 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200865 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100866 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200867 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200868 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200869 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200870 reader += 2;
871
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200872 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200873 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100874 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200875 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200876 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200877 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
878 + reader[2] * 256 + reader[3];
879 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200880
Christopher Faulet67957bd2017-09-27 11:00:59 +0200881 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200882 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100883 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200884 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200885 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200886 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200887
Christopher Faulet67957bd2017-09-27 11:00:59 +0200888 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200889 reader += 2;
890
Jérôme Magnin8d4e7dc2018-12-20 16:47:31 +0100891 if (reader + dns_answer_record->data_len > bufend) {
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100892 pool_free(dns_answer_item_pool, dns_answer_record);
893 return DNS_RESP_INVALID;
894 }
895
Christopher Faulet67957bd2017-09-27 11:00:59 +0200896 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200897 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200898 case DNS_RTYPE_A:
899 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200900 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100901 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200902 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200903 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200904 dns_answer_record->address.sa_family = AF_INET;
905 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
906 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200907 break;
908
909 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200910 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200911 * no IP could be found and last record was a CNAME. Could be triggered
912 * by a wrong query type
913 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200914 * + 1 because dns_answer_record_id starts at 0
915 * while number of answers is an integer and
916 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200917 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200918 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100919 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200920 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200921 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200922
923 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100924 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200925 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100926 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200927 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200928 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200929
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200930 memcpy(dns_answer_record->target, tmpname, len);
931 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200932 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200933 break;
934
Olivier Houchard8da5f982017-08-04 18:35:36 +0200935
936 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200937 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200938 * - 2 bytes for the priority
939 * - 2 bytes for the weight
940 * - 2 bytes for the port
941 * - the target hostname
942 */
943 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100944 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200945 return DNS_RESP_INVALID;
946 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200947 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200948 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200949 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200950 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200951 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200952 reader += sizeof(uint16_t);
953 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100954 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200955 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100956 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200957 return DNS_RESP_INVALID;
958 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200959 dns_answer_record->data_len = len;
960 memcpy(dns_answer_record->target, tmpname, len);
961 dns_answer_record->target[len] = 0;
962 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200963
Baptiste Assmann325137d2015-04-13 23:40:55 +0200964 case DNS_RTYPE_AAAA:
965 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200966 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100967 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200969 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200970 dns_answer_record->address.sa_family = AF_INET6;
971 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
972 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200973 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200974
Baptiste Assmann325137d2015-04-13 23:40:55 +0200975 } /* switch (record type) */
976
Christopher Faulet67957bd2017-09-27 11:00:59 +0200977 /* Increment the counter for number of records saved into our
978 * local response */
979 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200980
Christopher Faulet67957bd2017-09-27 11:00:59 +0200981 /* Move forward dns_answer_record->data_len for analyzing next
982 * record in the response */
983 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
984 ? offset
985 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200986
987 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200988 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200989 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
990 if (tmp_record->type != dns_answer_record->type)
991 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200992
993 switch(tmp_record->type) {
994 case DNS_RTYPE_A:
995 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
996 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
997 sizeof(in_addr_t)))
998 found = 1;
999 break;
1000
1001 case DNS_RTYPE_AAAA:
1002 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1003 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1004 sizeof(struct in6_addr)))
1005 found = 1;
1006 break;
1007
Olivier Houchard8da5f982017-08-04 18:35:36 +02001008 case DNS_RTYPE_SRV:
1009 if (dns_answer_record->data_len == tmp_record->data_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001010 !dns_hostname_cmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001011 dns_answer_record->port == tmp_record->port) {
1012 tmp_record->weight = dns_answer_record->weight;
1013 found = 1;
1014 }
1015 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001016
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001017 default:
1018 break;
1019 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001020
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001021 if (found == 1)
1022 break;
1023 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001024
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001025 if (found == 1) {
1026 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001027 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001028 }
1029 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001030 dns_answer_record->last_seen = now.tv_sec;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001031 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001032 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1033 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001034 } /* for i 0 to ancount */
1035
Christopher Faulet67957bd2017-09-27 11:00:59 +02001036 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001037 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001038
Baptiste Assmann37950c82020-02-19 01:08:51 +01001039 /* now parsing additional records for SRV queries only */
1040 if (dns_query->type != DNS_RTYPE_SRV)
1041 goto skip_parsing_additional_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001042 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001043 for (i = 0; i < dns_p->header.arcount; i++) {
1044 if (reader >= bufend)
1045 return DNS_RESP_INVALID;
1046
1047 dns_answer_record = pool_alloc(dns_answer_item_pool);
1048 if (dns_answer_record == NULL)
1049 return (DNS_RESP_INVALID);
1050
1051 offset = 0;
1052 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1053
1054 if (len == 0) {
1055 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann37950c82020-02-19 01:08:51 +01001056 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001057 }
1058
1059 memcpy(dns_answer_record->name, tmpname, len);
1060 dns_answer_record->name[len] = 0;
1061
1062 reader += offset;
1063 if (reader >= bufend) {
1064 pool_free(dns_answer_item_pool, dns_answer_record);
1065 return DNS_RESP_INVALID;
1066 }
1067
1068 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1069 if (reader + 2 > bufend) {
1070 pool_free(dns_answer_item_pool, dns_answer_record);
1071 return DNS_RESP_INVALID;
1072 }
1073 dns_answer_record->type = reader[0] * 256 + reader[1];
1074 reader += 2;
1075
1076 /* 2 bytes for class (2) */
1077 if (reader + 2 > bufend) {
1078 pool_free(dns_answer_item_pool, dns_answer_record);
1079 return DNS_RESP_INVALID;
1080 }
1081 dns_answer_record->class = reader[0] * 256 + reader[1];
1082 reader += 2;
1083
1084 /* 4 bytes for ttl (4) */
1085 if (reader + 4 > bufend) {
1086 pool_free(dns_answer_item_pool, dns_answer_record);
1087 return DNS_RESP_INVALID;
1088 }
1089 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1090 + reader[2] * 256 + reader[3];
1091 reader += 4;
1092
1093 /* Now reading data len */
1094 if (reader + 2 > bufend) {
1095 pool_free(dns_answer_item_pool, dns_answer_record);
1096 return DNS_RESP_INVALID;
1097 }
1098 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1099
1100 /* Move forward 2 bytes for data len */
1101 reader += 2;
1102
1103 if (reader + dns_answer_record->data_len > bufend) {
1104 pool_free(dns_answer_item_pool, dns_answer_record);
1105 return DNS_RESP_INVALID;
1106 }
1107
1108 /* Analyzing record content */
1109 switch (dns_answer_record->type) {
1110 case DNS_RTYPE_A:
1111 /* ipv4 is stored on 4 bytes */
1112 if (dns_answer_record->data_len != 4) {
1113 pool_free(dns_answer_item_pool, dns_answer_record);
1114 return DNS_RESP_INVALID;
1115 }
1116 dns_answer_record->address.sa_family = AF_INET;
1117 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1118 reader, dns_answer_record->data_len);
1119 break;
1120
1121 case DNS_RTYPE_AAAA:
1122 /* ipv6 is stored on 16 bytes */
1123 if (dns_answer_record->data_len != 16) {
1124 pool_free(dns_answer_item_pool, dns_answer_record);
1125 return DNS_RESP_INVALID;
1126 }
1127 dns_answer_record->address.sa_family = AF_INET6;
1128 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1129 reader, dns_answer_record->data_len);
1130 break;
1131
1132 default:
1133 pool_free(dns_answer_item_pool, dns_answer_record);
1134 continue;
1135
1136 } /* switch (record type) */
1137
1138 /* Increment the counter for number of records saved into our
1139 * local response */
1140 nb_saved_records++;
1141
1142 /* Move forward dns_answer_record->data_len for analyzing next
1143 * record in the response */
1144 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1145 ? offset
1146 : dns_answer_record->data_len);
1147
1148 /* Lookup to see if we already had this entry */
1149 found = 0;
1150 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1151 if (tmp_record->type != dns_answer_record->type)
1152 continue;
1153
1154 switch(tmp_record->type) {
1155 case DNS_RTYPE_A:
1156 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1157 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1158 sizeof(in_addr_t)))
1159 found = 1;
1160 break;
1161
1162 case DNS_RTYPE_AAAA:
1163 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1164 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1165 sizeof(struct in6_addr)))
1166 found = 1;
1167 break;
1168
1169 default:
1170 break;
1171 }
1172
1173 if (found == 1)
1174 break;
1175 }
1176
1177 if (found == 1) {
1178 tmp_record->last_seen = now.tv_sec;
1179 pool_free(dns_answer_item_pool, dns_answer_record);
1180 }
1181 else {
1182 dns_answer_record->last_seen = now.tv_sec;
1183 dns_answer_record->ar_item = NULL;
1184
1185 // looking for the SRV record in the response list linked to this additional record
1186 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1187 if ( !(
1188 (tmp_record->type == DNS_RTYPE_SRV) &&
1189 (tmp_record->ar_item == NULL) &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001190 (dns_hostname_cmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len) == 0)
Baptiste Assmann13a92322019-06-07 09:40:55 +02001191 )
1192 )
1193 continue;
1194 tmp_record->ar_item = dns_answer_record;
1195 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001196
1197 LIST_ADDQ(&dns_p->ar_list, &dns_answer_record->list);
1198 }
1199 } /* for i 0 to arcount */
1200
Baptiste Assmann37950c82020-02-19 01:08:51 +01001201 skip_parsing_additional_records:
1202
Baptiste Assmann13a92322019-06-07 09:40:55 +02001203 /* Save the number of records we really own */
1204 dns_p->header.arcount = nb_saved_records;
1205
Christopher Faulet67957bd2017-09-27 11:00:59 +02001206 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001207 return DNS_RESP_VALID;
1208}
1209
Christopher Faulet67957bd2017-09-27 11:00:59 +02001210/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001211 * If existing IP not found, return the first IP matching family_priority,
1212 * otherwise, first ip found
1213 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001214 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001215 * For both cases above, dns_validate_dns_response is required
1216 * returns one of the DNS_UPD_* code
1217 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001218int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001219 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001220 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001221 void **newip, short *newip_sin_family,
1222 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001223{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001224 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001225 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001226 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001227 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001228 int currentip_sel;
1229 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001230 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001231 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001232
Christopher Faulet67957bd2017-09-27 11:00:59 +02001233 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001234 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001235 *newip = newip4 = newip6 = NULL;
1236 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001237 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001238 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001239
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001240 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001241 * Top priority is the preferred network ip version,
1242 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001243 * the last priority is the currently used IP,
1244 *
1245 * For these three priorities, a score is calculated. The
1246 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001247 * 8 - preferred ip version.
1248 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001249 * 2 - if the ip in the record is not affected to any other server in the same backend (duplication)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001250 * 1 - current ip.
1251 * The result with the biggest score is returned.
1252 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001253
1254 list_for_each_entry(record, &dns_p->answer_list, list) {
1255 void *ip;
1256 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001257
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001258 if (record->type == DNS_RTYPE_A) {
1259 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1260 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001261 }
1262 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001263 ip_type = AF_INET6;
1264 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001265 }
1266 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001267 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001268 score = 0;
1269
Joseph Herlant42cf6392018-11-15 10:33:28 -08001270 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001271 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001272 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001273
Joseph Herlant42cf6392018-11-15 10:33:28 -08001274 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001275 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001276
1277 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001278 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001279 continue;
1280
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001281 if ((ip_type == AF_INET &&
1282 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001283 &dns_opts->pref_net[j].mask.in4,
1284 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001285 (ip_type == AF_INET6 &&
1286 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001287 &dns_opts->pref_net[j].mask.in6,
1288 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001289 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001290 break;
1291 }
1292 }
1293
Christopher Faulet67957bd2017-09-27 11:00:59 +02001294 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001295 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001296 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001297 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001298 if (!allowed_duplicated_ip) {
1299 continue;
1300 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001301 } else {
1302 score += 2;
1303 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001304
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001305 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001306 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001307 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001308 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001309 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001310 !memcmp(ip, currentip, 16)))) {
1311 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001312 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001313 }
1314 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001315 currentip_sel = 0;
1316
1317 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001318 * score. The maximum score is 15, if this value is reached, we
1319 * break the parsing. Implicitly, this score is reached the ip
1320 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001321 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001322 if (ip_type == AF_INET)
1323 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001324 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001325 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001326 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001327 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001328 return DNS_UPD_NO;
1329 max_score = score;
1330 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001331 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001332
Christopher Faulet67957bd2017-09-27 11:00:59 +02001333 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001334 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001335 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001336
Christopher Faulet67957bd2017-09-27 11:00:59 +02001337 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001338 if (family_priority == AF_INET) {
1339 if (newip4) {
1340 *newip = newip4;
1341 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001342 }
1343 else if (newip6) {
1344 *newip = newip6;
1345 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001346 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001347 if (!currentip_found)
1348 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001349 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001350 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001351 else if (family_priority == AF_INET6) {
1352 if (newip6) {
1353 *newip = newip6;
1354 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001355 }
1356 else if (newip4) {
1357 *newip = newip4;
1358 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001359 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001360 if (!currentip_found)
1361 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001362 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001363 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001364 else if (family_priority == AF_UNSPEC) {
1365 if (newip6) {
1366 *newip = newip6;
1367 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001368 }
1369 else if (newip4) {
1370 *newip = newip4;
1371 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001372 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001373 if (!currentip_found)
1374 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001375 }
1376
Christopher Faulet67957bd2017-09-27 11:00:59 +02001377 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001378 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001379
Christopher Faulet67957bd2017-09-27 11:00:59 +02001380 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001381 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001382 /* Move the first record to the end of the list, for internal
1383 * round robin */
1384 LIST_DEL(&record->list);
1385 LIST_ADDQ(&dns_p->answer_list, &record->list);
1386 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001387 }
1388 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001389}
1390
Christopher Faulet67957bd2017-09-27 11:00:59 +02001391/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001392 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1394 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001395 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001396 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1397 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001398 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001399int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001400{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001401 char *ptr;
1402 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001403
Christopher Faulet67957bd2017-09-27 11:00:59 +02001404 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001405 return -1;
1406
Christopher Faulet67957bd2017-09-27 11:00:59 +02001407 ptr = str;
1408 for (i = 0; i < dn_len-1; ++i) {
1409 sz = dn[i];
1410 if (i)
1411 *ptr++ = '.';
1412 memcpy(ptr, dn+i+1, sz);
1413 ptr += sz;
1414 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001415 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001416 *ptr++ = '\0';
1417 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001418}
1419
Christopher Faulet67957bd2017-09-27 11:00:59 +02001420/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1421 *
1422 * <str> must be a null-terminated string. <str_len> must include the
1423 * terminating null byte. <dn> buffer must be allocated and its size must be
1424 * passed in <dn_len>.
1425 *
1426 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1427 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001428 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001429int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001430{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001431 int i, offset;
1432
Christopher Faulet67957bd2017-09-27 11:00:59 +02001433 if (dn_len < str_len + 1)
1434 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001435
Christopher Faulet67957bd2017-09-27 11:00:59 +02001436 /* First byte of dn will be used to store the length of the first
1437 * label */
1438 offset = 0;
1439 for (i = 0; i < str_len; ++i) {
1440 if (str[i] == '.') {
1441 /* 2 or more consecutive dots is invalid */
1442 if (i == offset)
1443 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001444
Lukas Tribus81725b82020-02-27 15:47:24 +01001445 /* ignore trailing dot */
1446 if (i + 2 == str_len) {
1447 i++;
1448 break;
1449 }
1450
Christopher Faulet67957bd2017-09-27 11:00:59 +02001451 dn[offset] = (i - offset);
1452 offset = i+1;
1453 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001454 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001455 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001456 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001457 dn[offset] = (i - offset - 1);
1458 dn[i] = '\0';
1459 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001460}
1461
Christopher Faulet67957bd2017-09-27 11:00:59 +02001462/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001463 * - total size
1464 * - each label size individually
1465 * returns:
1466 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1467 * 1 when no error. <err> is left unaffected.
1468 */
1469int dns_hostname_validation(const char *string, char **err)
1470{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001471 int i;
1472
1473 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1474 if (err)
1475 *err = DNS_TOO_LONG_FQDN;
1476 return 0;
1477 }
1478
William Dauchyaecd5dc2020-01-26 19:52:34 +01001479 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001480 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001481 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1482 if (!(*string == '-' || *string == '_' ||
1483 (*string >= 'a' && *string <= 'z') ||
1484 (*string >= 'A' && *string <= 'Z') ||
1485 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001486 if (err)
1487 *err = DNS_INVALID_CHARACTER;
1488 return 0;
1489 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001490 i++;
1491 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001492 }
1493
William Dauchyaecd5dc2020-01-26 19:52:34 +01001494 if (!(*string))
1495 break;
1496
1497 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001498 if (err)
1499 *err = DNS_LABEL_TOO_LONG;
1500 return 0;
1501 }
1502
William Dauchyaecd5dc2020-01-26 19:52:34 +01001503 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001504 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001505 return 1;
1506}
1507
Christopher Faulet67957bd2017-09-27 11:00:59 +02001508/* Picks up an available resolution from the different resolution list
1509 * associated to a resolvers section, in this order:
1510 * 1. check in resolutions.curr for the same hostname and query_type
1511 * 2. check in resolutions.wait for the same hostname and query_type
1512 * 3. Get a new resolution from resolution pool
1513 *
1514 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001515 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001516static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1517 char **hostname_dn, int hostname_dn_len,
1518 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001519{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520 struct dns_resolution *res;
1521
1522 if (!*hostname_dn)
1523 goto from_pool;
1524
1525 /* Search for same hostname and query type in resolutions.curr */
1526 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1527 if (!res->hostname_dn)
1528 continue;
1529 if ((query_type == res->prefered_query_type) &&
1530 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001531 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001532 return res;
1533 }
1534
1535 /* Search for same hostname and query type in resolutions.wait */
1536 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1537 if (!res->hostname_dn)
1538 continue;
1539 if ((query_type == res->prefered_query_type) &&
1540 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001541 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001542 return res;
1543 }
1544
1545 from_pool:
1546 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001547 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001548 if (res) {
1549 memset(res, 0, sizeof(*res));
1550 res->resolvers = resolvers;
1551 res->uuid = resolution_uuid;
1552 res->status = RSLV_STATUS_NONE;
1553 res->step = RSLV_STEP_NONE;
1554 res->last_valid = now_ms;
1555
1556 LIST_INIT(&res->requesters);
1557 LIST_INIT(&res->response.answer_list);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001558 LIST_INIT(&res->response.ar_list);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559
1560 res->prefered_query_type = query_type;
1561 res->query_type = query_type;
1562 res->hostname_dn = *hostname_dn;
1563 res->hostname_dn_len = hostname_dn_len;
1564
1565 ++resolution_uuid;
1566
1567 /* Move the resolution to the resolvers wait queue */
1568 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1569 }
1570 return res;
1571}
1572
1573/* Releases a resolution from its requester(s) and move it back to the pool */
1574static void dns_free_resolution(struct dns_resolution *resolution)
1575{
1576 struct dns_requester *req, *reqback;
1577
1578 /* clean up configuration */
1579 dns_reset_resolution(resolution);
1580 resolution->hostname_dn = NULL;
1581 resolution->hostname_dn_len = 0;
1582
1583 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1584 LIST_DEL(&req->list);
1585 req->resolution = NULL;
1586 }
1587
1588 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001589 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001590}
1591
1592/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1593 * on success, -1 otherwise.
1594 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001595int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001596{
1597 struct dns_resolution *res = NULL;
1598 struct dns_requester *req;
1599 struct dns_resolvers *resolvers;
1600 struct server *srv = NULL;
1601 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001602 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001603 char **hostname_dn;
1604 int hostname_dn_len, query_type;
1605
1606 switch (requester_type) {
1607 case OBJ_TYPE_SERVER:
1608 srv = (struct server *)requester;
1609 hostname_dn = &srv->hostname_dn;
1610 hostname_dn_len = srv->hostname_dn_len;
1611 resolvers = srv->resolvers;
1612 query_type = ((srv->dns_opts.family_prio == AF_INET)
1613 ? DNS_RTYPE_A
1614 : DNS_RTYPE_AAAA);
1615 break;
1616
1617 case OBJ_TYPE_SRVRQ:
1618 srvrq = (struct dns_srvrq *)requester;
1619 hostname_dn = &srvrq->hostname_dn;
1620 hostname_dn_len = srvrq->hostname_dn_len;
1621 resolvers = srvrq->resolvers;
1622 query_type = DNS_RTYPE_SRV;
1623 break;
1624
Baptiste Assmann333939c2019-01-21 08:34:50 +01001625 case OBJ_TYPE_STREAM:
1626 stream = (struct stream *)requester;
1627 hostname_dn = &stream->dns_ctx.hostname_dn;
1628 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1629 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001630 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001631 ? DNS_RTYPE_A
1632 : DNS_RTYPE_AAAA);
1633 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001634 default:
1635 goto err;
1636 }
1637
1638 /* Get a resolution from the resolvers' wait queue or pool */
1639 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1640 goto err;
1641
1642 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001643 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001644 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001645 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001646 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001647 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001648 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001649 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001650 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001651 req->owner = &srv->obj_type;
1652 srv->dns_requester = req;
1653 }
1654 else
1655 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001656 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001657 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001658
1659 req->requester_cb = snr_resolution_cb;
1660 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001661 }
1662 else if (srvrq) {
1663 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001664 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001665 goto err;
1666 req->owner = &srvrq->obj_type;
1667 srvrq->dns_requester = req;
1668 }
1669 else
1670 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001671
1672 req->requester_cb = snr_resolution_cb;
1673 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001674 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001675 else if (stream) {
1676 if (stream->dns_ctx.dns_requester == NULL) {
1677 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1678 goto err;
1679 req->owner = &stream->obj_type;
1680 stream->dns_ctx.dns_requester = req;
1681 }
1682 else
1683 req = stream->dns_ctx.dns_requester;
1684
1685 req->requester_cb = act_resolution_cb;
1686 req->requester_error_cb = act_resolution_error_cb;
1687 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001688 else
1689 goto err;
1690
1691 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692
1693 LIST_ADDQ(&res->requesters, &req->list);
1694 return 0;
1695
1696 err:
1697 if (res && LIST_ISEMPTY(&res->requesters))
1698 dns_free_resolution(res);
1699 return -1;
1700}
1701
1702/* Removes a requester from a DNS resoltion. It takes takes care of all the
1703 * consequences. It also cleans up some parameters from the requester.
1704 */
1705void dns_unlink_resolution(struct dns_requester *requester)
1706{
1707 struct dns_resolution *res;
1708 struct dns_requester *req;
1709
1710 /* Nothing to do */
1711 if (!requester || !requester->resolution)
1712 return;
1713 res = requester->resolution;
1714
1715 /* Clean up the requester */
1716 LIST_DEL(&requester->list);
1717 requester->resolution = NULL;
1718
1719 /* We need to find another requester linked on this resolution */
1720 if (!LIST_ISEMPTY(&res->requesters))
1721 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1722 else {
1723 dns_free_resolution(res);
1724 return;
1725 }
1726
1727 /* Move hostname_dn related pointers to the next requester */
1728 switch (obj_type(req->owner)) {
1729 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001730 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1731 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001732 break;
1733 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001734 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1735 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001736 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001737 case OBJ_TYPE_STREAM:
1738 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1739 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1740 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001741 default:
1742 res->hostname_dn = NULL;
1743 res->hostname_dn_len = 0;
1744 break;
1745 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001746}
1747
Christopher Faulet67957bd2017-09-27 11:00:59 +02001748/* Called when a network IO is generated on a name server socket for an incoming
1749 * packet. It performs the following actions:
1750 * - check if the packet requires processing (not outdated resolution)
1751 * - ensure the DNS packet received is valid and call requester's callback
1752 * - call requester's error callback if invalid response
1753 * - check the dn_name in the packet against the one sent
1754 */
1755static void dns_resolve_recv(struct dgram_conn *dgram)
1756{
1757 struct dns_nameserver *ns, *tmpns;
1758 struct dns_resolvers *resolvers;
1759 struct dns_resolution *res;
1760 struct dns_query_item *query;
1761 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1762 unsigned char *bufend;
1763 int fd, buflen, dns_resp;
1764 int max_answer_records;
1765 unsigned short query_id;
1766 struct eb32_node *eb;
1767 struct dns_requester *req;
1768
1769 fd = dgram->t.sock.fd;
1770
1771 /* check if ready for reading */
1772 if (!fd_recv_ready(fd))
1773 return;
1774
1775 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001776 if ((ns = dgram->owner) == NULL) {
1777 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1778 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001779 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001780 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781
1782 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001783 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001784
1785 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001786 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001787 /* read message received */
1788 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1789 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001790 /* FIXME : for now we consider EAGAIN only, but at
1791 * least we purge sticky errors that would cause us to
1792 * be called in loops.
1793 */
1794 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001795 fd_cant_recv(fd);
1796 break;
1797 }
1798
1799 /* message too big */
1800 if (buflen > resolvers->accepted_payload_size) {
1801 ns->counters.too_big++;
1802 continue;
1803 }
1804
1805 /* initializing variables */
1806 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1807
1808 /* read the query id from the packet (16 bits) */
1809 if (buf + 2 > bufend) {
1810 ns->counters.invalid++;
1811 continue;
1812 }
1813 query_id = dns_response_get_query_id(buf);
1814
1815 /* search the query_id in the pending resolution tree */
1816 eb = eb32_lookup(&resolvers->query_ids, query_id);
1817 if (eb == NULL) {
1818 /* unknown query id means an outdated response and can be safely ignored */
1819 ns->counters.outdated++;
1820 continue;
1821 }
1822
William Dauchybe8a3872019-11-27 23:32:41 +01001823 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001824 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001825 /* number of responses received */
1826 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001827
Christopher Faulet67957bd2017-09-27 11:00:59 +02001828 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1829 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001830
Christopher Faulet67957bd2017-09-27 11:00:59 +02001831 switch (dns_resp) {
1832 case DNS_RESP_VALID:
1833 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001834
Christopher Faulet67957bd2017-09-27 11:00:59 +02001835 case DNS_RESP_INVALID:
1836 case DNS_RESP_QUERY_COUNT_ERROR:
1837 case DNS_RESP_WRONG_NAME:
1838 res->status = RSLV_STATUS_INVALID;
1839 ns->counters.invalid++;
1840 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001841
Christopher Faulet67957bd2017-09-27 11:00:59 +02001842 case DNS_RESP_NX_DOMAIN:
1843 res->status = RSLV_STATUS_NX;
1844 ns->counters.nx++;
1845 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001846
Christopher Faulet67957bd2017-09-27 11:00:59 +02001847 case DNS_RESP_REFUSED:
1848 res->status = RSLV_STATUS_REFUSED;
1849 ns->counters.refused++;
1850 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001851
Christopher Faulet67957bd2017-09-27 11:00:59 +02001852 case DNS_RESP_ANCOUNT_ZERO:
1853 res->status = RSLV_STATUS_OTHER;
1854 ns->counters.any_err++;
1855 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001856
Christopher Faulet67957bd2017-09-27 11:00:59 +02001857 case DNS_RESP_CNAME_ERROR:
1858 res->status = RSLV_STATUS_OTHER;
1859 ns->counters.cname_error++;
1860 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001861
Christopher Faulet67957bd2017-09-27 11:00:59 +02001862 case DNS_RESP_TRUNCATED:
1863 res->status = RSLV_STATUS_OTHER;
1864 ns->counters.truncated++;
1865 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 case DNS_RESP_NO_EXPECTED_RECORD:
1868 case DNS_RESP_ERROR:
1869 case DNS_RESP_INTERNAL:
1870 res->status = RSLV_STATUS_OTHER;
1871 ns->counters.other++;
1872 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001873 }
1874
Christopher Faulet67957bd2017-09-27 11:00:59 +02001875 /* Wait all nameservers response to handle errors */
1876 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1877 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001878
Christopher Faulet67957bd2017-09-27 11:00:59 +02001879 /* Process error codes */
1880 if (dns_resp != DNS_RESP_VALID) {
1881 if (res->prefered_query_type != res->query_type) {
1882 /* The fallback on the query type was already performed,
1883 * so check the try counter. If it falls to 0, we can
1884 * report an error. Else, wait the next attempt. */
1885 if (!res->try)
1886 goto report_res_error;
1887 }
1888 else {
1889 /* Fallback from A to AAAA or the opposite and re-send
1890 * the resolution immediately. try counter is not
1891 * decremented. */
1892 if (res->prefered_query_type == DNS_RTYPE_A) {
1893 res->query_type = DNS_RTYPE_AAAA;
1894 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001895 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001896 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1897 res->query_type = DNS_RTYPE_A;
1898 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001899 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001900 }
1901 continue;
1902 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001903
Christopher Faulet67957bd2017-09-27 11:00:59 +02001904 /* Now let's check the query's dname corresponds to the one we
1905 * sent. We can check only the first query of the list. We send
1906 * one query at a time so we get one query in the response */
1907 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
Olivier Houchardb17b8842020-04-01 18:30:27 +02001908 if (query && dns_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001909 dns_resp = DNS_RESP_WRONG_NAME;
1910 ns->counters.other++;
1911 goto report_res_error;
1912 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001913
Christopher Faulet67957bd2017-09-27 11:00:59 +02001914 /* So the resolution succeeded */
1915 res->status = RSLV_STATUS_VALID;
1916 res->last_valid = now_ms;
1917 ns->counters.valid++;
1918 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001919
Christopher Faulet67957bd2017-09-27 11:00:59 +02001920 report_res_error:
1921 list_for_each_entry(req, &res->requesters, list)
1922 req->requester_error_cb(req, dns_resp);
1923 dns_reset_resolution(res);
1924 LIST_DEL(&res->list);
1925 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1926 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001927
Christopher Faulet67957bd2017-09-27 11:00:59 +02001928 report_res_success:
1929 /* Only the 1rst requester s managed by the server, others are
1930 * from the cache */
1931 tmpns = ns;
1932 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001933 struct server *s = objt_server(req->owner);
1934
1935 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001936 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001937 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001938 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001939 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001941 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001942
Christopher Faulet67957bd2017-09-27 11:00:59 +02001943 dns_reset_resolution(res);
1944 LIST_DEL(&res->list);
1945 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1946 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001947 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001948 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001949 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001950}
William Lallemand69e96442016-11-19 00:58:54 +01001951
Christopher Faulet67957bd2017-09-27 11:00:59 +02001952/* Called when a resolvers network socket is ready to send data */
1953static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001954{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001955 struct dns_resolvers *resolvers;
1956 struct dns_nameserver *ns;
1957 struct dns_resolution *res;
1958 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001959
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001961
Christopher Faulet67957bd2017-09-27 11:00:59 +02001962 /* check if ready for sending */
1963 if (!fd_send_ready(fd))
1964 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001965
Christopher Faulet67957bd2017-09-27 11:00:59 +02001966 /* we don't want/need to be waked up any more for sending */
1967 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001968
Christopher Faulet67957bd2017-09-27 11:00:59 +02001969 /* no need to go further if we can't retrieve the nameserver */
1970 if ((ns = dgram->owner) == NULL)
1971 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001972
Christopher Faulet67957bd2017-09-27 11:00:59 +02001973 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001974 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001975
Christopher Faulet67957bd2017-09-27 11:00:59 +02001976 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001977 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001978
Christopher Faulet67957bd2017-09-27 11:00:59 +02001979 if (res->nb_queries == resolvers->nb_nameservers)
1980 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001981
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001982 len = dns_build_query(res->query_id, res->query_type,
1983 resolvers->accepted_payload_size,
1984 res->hostname_dn, res->hostname_dn_len,
1985 trash.area, trash.size);
1986 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001987 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001988
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001989 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01001990 if (ret != len) {
1991 if (ret == -1 && errno == EAGAIN) {
1992 /* retry once the socket is ready */
1993 fd_cant_send(fd);
1994 continue;
1995 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01001997 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001998
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 ns->counters.sent++;
2000 res->nb_queries++;
2001 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002002
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 snd_error:
2004 ns->counters.snd_error++;
2005 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002006 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002007 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002008}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002009
Christopher Faulet67957bd2017-09-27 11:00:59 +02002010/* Processes DNS resolution. First, it checks the active list to detect expired
2011 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2012 * checks the wait list to trigger new resolutions.
2013 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002014static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002015{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002016 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002017 struct dns_resolution *res, *resback;
2018 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002019
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002020 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002021
Christopher Faulet67957bd2017-09-27 11:00:59 +02002022 /* Handle all expired resolutions from the active list */
2023 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2024 /* When we find the first resolution in the future, then we can
2025 * stop here */
2026 exp = tick_add(res->last_query, resolvers->timeout.retry);
2027 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002028 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002029
Christopher Faulet67957bd2017-09-27 11:00:59 +02002030 /* If current resolution has been tried too many times and
2031 * finishes in timeout we update its status and remove it from
2032 * the list */
2033 if (!res->try) {
2034 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002035
Christopher Faulet67957bd2017-09-27 11:00:59 +02002036 /* Notify the result to the requesters */
2037 if (!res->nb_responses)
2038 res->status = RSLV_STATUS_TIMEOUT;
2039 list_for_each_entry(req, &res->requesters, list)
2040 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002041
Christopher Faulet67957bd2017-09-27 11:00:59 +02002042 /* Clean up resolution info and remove it from the
2043 * current list */
2044 dns_reset_resolution(res);
2045 LIST_DEL(&res->list);
2046 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002047 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048 else {
2049 /* Otherwise resend the DNS query and requeue the resolution */
2050 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2051 /* No response received (a real timeout) or fallback already done */
2052 res->query_type = res->prefered_query_type;
2053 res->try--;
2054 }
2055 else {
2056 /* Fallback from A to AAAA or the opposite and re-send
2057 * the resolution immediately. try counter is not
2058 * decremented. */
2059 if (res->prefered_query_type == DNS_RTYPE_A)
2060 res->query_type = DNS_RTYPE_AAAA;
2061 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2062 res->query_type = DNS_RTYPE_A;
2063 else
2064 res->try--;
2065 }
2066 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002067 }
2068 }
William Lallemand69e96442016-11-19 00:58:54 +01002069
Christopher Faulet67957bd2017-09-27 11:00:59 +02002070 /* Handle all resolutions in the wait list */
2071 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2072 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2073 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2074 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002075
Christopher Faulet67957bd2017-09-27 11:00:59 +02002076 if (dns_run_resolution(res) != 1) {
2077 res->last_resolution = now_ms;
2078 LIST_DEL(&res->list);
2079 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002080 }
2081 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002082
Christopher Faulet67957bd2017-09-27 11:00:59 +02002083 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002084 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002085 return t;
2086}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002087
Christopher Faulet67957bd2017-09-27 11:00:59 +02002088/* proto_udp callback functions for a DNS resolution */
2089struct dgram_data_cb resolve_dgram_cb = {
2090 .recv = dns_resolve_recv,
2091 .send = dns_resolve_send,
2092};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002093
Christopher Faulet67957bd2017-09-27 11:00:59 +02002094/* Release memory allocated by DNS */
2095static void dns_deinit(void)
2096{
2097 struct dns_resolvers *resolvers, *resolversback;
2098 struct dns_nameserver *ns, *nsback;
2099 struct dns_resolution *res, *resback;
2100 struct dns_requester *req, *reqback;
2101 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002102
Christopher Faulet67957bd2017-09-27 11:00:59 +02002103 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2104 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2105 free(ns->id);
2106 free((char *)ns->conf.file);
2107 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2108 fd_delete(ns->dgram->t.sock.fd);
2109 free(ns->dgram);
2110 LIST_DEL(&ns->list);
2111 free(ns);
2112 }
2113
2114 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2115 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2116 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002117 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002118 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002119 dns_free_resolution(res);
2120 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002121
Christopher Faulet67957bd2017-09-27 11:00:59 +02002122 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2123 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2124 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002125 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002126 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002127 dns_free_resolution(res);
2128 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002129
Christopher Faulet67957bd2017-09-27 11:00:59 +02002130 free(resolvers->id);
2131 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002132 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002133 LIST_DEL(&resolvers->list);
2134 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002135 }
2136
Christopher Faulet67957bd2017-09-27 11:00:59 +02002137 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2138 free(srvrq->name);
2139 free(srvrq->hostname_dn);
2140 LIST_DEL(&srvrq->list);
2141 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002142 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002143}
2144
Christopher Faulet67957bd2017-09-27 11:00:59 +02002145/* Finalizes the DNS configuration by allocating required resources and checking
2146 * live parameters.
2147 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002148 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002149static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002150{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002151 struct dns_resolvers *resolvers;
2152 struct proxy *px;
2153 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002154
Christopher Faulet67957bd2017-09-27 11:00:59 +02002155 /* allocate pool of resolution per resolvers */
2156 list_for_each_entry(resolvers, &dns_resolvers, list) {
2157 struct dns_nameserver *ns;
2158 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002159
Christopher Faulet67957bd2017-09-27 11:00:59 +02002160 /* Check if we can create the socket with nameservers info */
2161 list_for_each_entry(ns, &resolvers->nameservers, list) {
2162 struct dgram_conn *dgram = NULL;
2163 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002164
Christopher Faulet67957bd2017-09-27 11:00:59 +02002165 /* Check nameserver info */
2166 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002167 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2168 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002169 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002170 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002171 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002172 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002173 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2174 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002175 close(fd);
2176 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002177 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002178 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002179 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002180
Christopher Faulet67957bd2017-09-27 11:00:59 +02002181 /* Create dgram structure that will hold the UPD socket
2182 * and attach it on the current nameserver */
2183 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002184 ha_alert("config: resolvers '%s' : out of memory.\n",
2185 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002186 err_code |= (ERR_ALERT|ERR_ABORT);
2187 goto err;
2188 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002189
Christopher Faulet67957bd2017-09-27 11:00:59 +02002190 /* Leave dgram partially initialized, no FD attached for
2191 * now. */
2192 dgram->owner = ns;
2193 dgram->data = &resolve_dgram_cb;
2194 dgram->t.sock.fd = -1;
2195 ns->dgram = dgram;
2196 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002197
Christopher Faulet67957bd2017-09-27 11:00:59 +02002198 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002199 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002200 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002201 err_code |= (ERR_ALERT|ERR_ABORT);
2202 goto err;
2203 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002204
Christopher Faulet67957bd2017-09-27 11:00:59 +02002205 /* Update task's parameters */
2206 t->process = dns_process_resolvers;
2207 t->context = resolvers;
2208 resolvers->t = t;
2209 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002210 }
2211
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002212 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002213 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002214
Christopher Faulet67957bd2017-09-27 11:00:59 +02002215 for (srv = px->srv; srv; srv = srv->next) {
2216 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002217
Christopher Faulet67957bd2017-09-27 11:00:59 +02002218 if (!srv->resolvers_id)
2219 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002220
Christopher Faulet67957bd2017-09-27 11:00:59 +02002221 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002222 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2223 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002224 err_code |= (ERR_ALERT|ERR_ABORT);
2225 continue;
2226 }
2227 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002228
Christopher Faulet67957bd2017-09-27 11:00:59 +02002229 if (srv->srvrq && !srv->srvrq->resolvers) {
2230 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002231 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002232 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2233 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002234 err_code |= (ERR_ALERT|ERR_ABORT);
2235 continue;
2236 }
2237 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002238 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002239 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2240 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002241 err_code |= (ERR_ALERT|ERR_ABORT);
2242 continue;
2243 }
2244 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002245 }
2246
Christopher Faulet67957bd2017-09-27 11:00:59 +02002247 if (err_code & (ERR_ALERT|ERR_ABORT))
2248 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002249
Christopher Faulet67957bd2017-09-27 11:00:59 +02002250 return err_code;
2251 err:
2252 dns_deinit();
2253 return err_code;
2254
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002255}
2256
Christopher Faulet67957bd2017-09-27 11:00:59 +02002257/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002258static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002259{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002260 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002261
Christopher Faulet67957bd2017-09-27 11:00:59 +02002262 if (*args[2]) {
2263 list_for_each_entry(presolvers, &dns_resolvers, list) {
2264 if (strcmp(presolvers->id, args[2]) == 0) {
2265 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002266 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002267 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002268 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002269 if (appctx->ctx.cli.p0 == NULL)
2270 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002271 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002272 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002273}
2274
Christopher Faulet67957bd2017-09-27 11:00:59 +02002275/* Dumps counters from all resolvers section and associated name servers. It
2276 * returns 0 if the output buffer is full and it needs to be called again,
2277 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002278 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002279 */
2280static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2281{
2282 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002283 struct dns_resolvers *resolvers;
2284 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002285
2286 chunk_reset(&trash);
2287
2288 switch (appctx->st2) {
2289 case STAT_ST_INIT:
2290 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2291 /* fall through */
2292
2293 case STAT_ST_LIST:
2294 if (LIST_ISEMPTY(&dns_resolvers)) {
2295 chunk_appendf(&trash, "No resolvers found\n");
2296 }
2297 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002298 list_for_each_entry(resolvers, &dns_resolvers, list) {
2299 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002300 continue;
2301
Christopher Faulet67957bd2017-09-27 11:00:59 +02002302 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2303 list_for_each_entry(ns, &resolvers->nameservers, list) {
2304 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2305 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2306 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2307 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2308 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2309 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2310 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2311 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2312 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2313 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2314 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2315 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2316 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2317 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2318 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2319 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002320 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002321 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002322 }
2323 }
2324
2325 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002326 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002327 /* let's try again later from this session. We add ourselves into
2328 * this session's users so that it can remove us upon termination.
2329 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002330 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002331 return 0;
2332 }
William Lallemand69e96442016-11-19 00:58:54 +01002333 /* fall through */
2334
2335 default:
2336 appctx->st2 = STAT_ST_FIN;
2337 return 1;
2338 }
2339}
2340
2341/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002342static struct cli_kw_list cli_kws = {{ }, {
2343 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2344 " associated name servers",
2345 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2346 {{},}
2347 }
2348};
William Lallemand69e96442016-11-19 00:58:54 +01002349
Willy Tarreau0108d902018-11-25 19:14:37 +01002350INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002351
Baptiste Assmann333939c2019-01-21 08:34:50 +01002352/*
2353 * Prepare <rule> for hostname resolution.
2354 * Returns -1 in case of any allocation failure, 0 if not.
2355 * On error, a global failure counter is also incremented.
2356 */
2357static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2358{
2359 char *hostname_dn;
2360 int hostname_len, hostname_dn_len;
2361 struct buffer *tmp = get_trash_chunk();
2362
2363 if (!hostname)
2364 return 0;
2365
2366 hostname_len = strlen(hostname);
2367 hostname_dn = tmp->area;
2368 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2369 hostname_dn, tmp->size);
2370 if (hostname_dn_len == -1)
2371 goto err;
2372
2373
2374 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2375 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2376 if (!stream->dns_ctx.hostname_dn)
2377 goto err;
2378
2379 return 0;
2380
2381 err:
2382 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2383 dns_failed_resolutions += 1;
2384 return -1;
2385}
2386
2387
2388/*
2389 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2390 */
2391enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2392 struct session *sess, struct stream *s, int flags)
2393{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002394 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002395 struct sample *smp;
2396 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002397 struct dns_requester *req;
2398 struct dns_resolvers *resolvers;
2399 struct dns_resolution *res;
2400 int exp;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002401
2402 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002403 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002404 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2405 resolution = s->dns_ctx.dns_requester->resolution;
Baptiste Assmann4c52e4b2019-10-01 15:32:40 +02002406 if (resolution->step == RSLV_STEP_RUNNING) {
2407 return ACT_RET_YIELD;
2408 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01002409 if (resolution->step == RSLV_STEP_NONE) {
2410 /* We update the variable only if we have a valid response. */
2411 if (resolution->status == RSLV_STATUS_VALID) {
2412 struct sample smp;
2413 short ip_sin_family = 0;
2414 void *ip = NULL;
2415
Christopher Fauleta4168432020-01-24 18:08:42 +01002416 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002417 0, &ip, &ip_sin_family, NULL);
2418
2419 switch (ip_sin_family) {
2420 case AF_INET:
2421 smp.data.type = SMP_T_IPV4;
2422 memcpy(&smp.data.u.ipv4, ip, 4);
2423 break;
2424 case AF_INET6:
2425 smp.data.type = SMP_T_IPV6;
2426 memcpy(&smp.data.u.ipv6, ip, 16);
2427 break;
2428 default:
2429 ip = NULL;
2430 }
2431
2432 if (ip) {
2433 smp.px = px;
2434 smp.sess = sess;
2435 smp.strm = s;
2436
2437 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2438 }
2439 }
2440 }
2441
2442 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
2443 s->dns_ctx.hostname_dn_len = 0;
2444 dns_unlink_resolution(s->dns_ctx.dns_requester);
2445
2446 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2447 s->dns_ctx.dns_requester = NULL;
2448
2449 return ACT_RET_CONT;
2450 }
2451
2452 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002453 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2454 if (smp == NULL)
2455 return ACT_RET_CONT;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002456
Willy Tarreau45726fd2019-07-17 10:38:45 +02002457 fqdn = smp->data.u.str.area;
2458 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet13403762019-12-13 09:01:57 +01002459 return ACT_RET_CONT; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002460
Willy Tarreau45726fd2019-07-17 10:38:45 +02002461 s->dns_ctx.parent = rule;
2462 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002463
2464 /* Check if there is a fresh enough response in the cache of our associated resolution */
2465 req = s->dns_ctx.dns_requester;
2466 if (!req || !req->resolution) {
2467 dns_trigger_resolution(s->dns_ctx.dns_requester);
2468 return ACT_RET_YIELD;
2469 }
2470 res = req->resolution;
2471 resolvers = res->resolvers;
2472
2473 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2474 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2475 && !tick_is_expired(exp, now_ms)) {
2476 goto use_cache;
2477 }
2478
Willy Tarreau45726fd2019-07-17 10:38:45 +02002479 dns_trigger_resolution(s->dns_ctx.dns_requester);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002480 return ACT_RET_YIELD;
2481}
2482
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002483static void release_dns_action(struct act_rule *rule)
2484{
2485 release_sample_expr(rule->arg.dns.expr);
2486 free(rule->arg.dns.varname);
2487 free(rule->arg.dns.resolvers_id);
2488 free(rule->arg.dns.dns_opts);
2489}
2490
Baptiste Assmann333939c2019-01-21 08:34:50 +01002491
2492/* parse "do-resolve" action
2493 * This action takes the following arguments:
2494 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2495 *
2496 * - <varName> is the variable name where the result of the DNS resolution will be stored
2497 * (mandatory)
2498 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2499 * (mandatory)
2500 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2501 * (optional), defaults to ipv6
2502 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2503 */
2504enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2505{
2506 int cur_arg;
2507 struct sample_expr *expr;
2508 unsigned int where;
2509 const char *beg, *end;
2510
2511 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2512 cur_arg = *orig_arg - 1;
2513
2514 /* locate varName, which is mandatory */
2515 beg = strchr(args[cur_arg], '(');
2516 if (beg == NULL)
2517 goto do_resolve_parse_error;
2518 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2519 end = strchr(beg, ',');
2520 if (end == NULL)
2521 goto do_resolve_parse_error;
2522 rule->arg.dns.varname = my_strndup(beg, end - beg);
2523 if (rule->arg.dns.varname == NULL)
2524 goto do_resolve_parse_error;
2525
2526
2527 /* locate resolversSectionName, which is mandatory.
2528 * Since next parameters are optional, the delimiter may be comma ','
2529 * or closing parenthesis ')'
2530 */
2531 beg = end + 1;
2532 end = strchr(beg, ',');
2533 if (end == NULL)
2534 end = strchr(beg, ')');
2535 if (end == NULL)
2536 goto do_resolve_parse_error;
2537 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2538 if (rule->arg.dns.resolvers_id == NULL)
2539 goto do_resolve_parse_error;
2540
2541
Christopher Fauleta4168432020-01-24 18:08:42 +01002542 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2543 if (rule->arg.dns.dns_opts == NULL)
2544 goto do_resolve_parse_error;
2545
Baptiste Assmann333939c2019-01-21 08:34:50 +01002546 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002547 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002548
2549 /* optional arguments accepted for now:
2550 * ipv4 or ipv6
2551 */
2552 while (*end != ')') {
2553 beg = end + 1;
2554 end = strchr(beg, ',');
2555 if (end == NULL)
2556 end = strchr(beg, ')');
2557 if (end == NULL)
2558 goto do_resolve_parse_error;
2559
2560 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002561 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002562 }
2563 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002564 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002565 }
2566 else {
2567 goto do_resolve_parse_error;
2568 }
2569 }
2570
2571 cur_arg = cur_arg + 1;
2572
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002573 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002574 if (!expr)
2575 goto do_resolve_parse_error;
2576
2577
2578 where = 0;
2579 if (px->cap & PR_CAP_FE)
2580 where |= SMP_VAL_FE_HRQ_HDR;
2581 if (px->cap & PR_CAP_BE)
2582 where |= SMP_VAL_BE_HRQ_HDR;
2583
2584 if (!(expr->fetch->val & where)) {
2585 memprintf(err,
2586 "fetch method '%s' extracts information from '%s', none of which is available here",
2587 args[cur_arg-1], sample_src_names(expr->fetch->use));
2588 free(expr);
2589 return ACT_RET_PRS_ERR;
2590 }
2591 rule->arg.dns.expr = expr;
2592 rule->action = ACT_CUSTOM;
2593 rule->action_ptr = dns_action_do_resolve;
2594 *orig_arg = cur_arg;
2595
2596 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002597 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002598
2599 return ACT_RET_PRS_OK;
2600
2601 do_resolve_parse_error:
2602 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2603 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2604 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2605 args[cur_arg]);
2606 return ACT_RET_PRS_ERR;
2607}
2608
2609static struct action_kw_list http_req_kws = { { }, {
2610 { "do-resolve", dns_parse_do_resolve, 1 },
2611 { /* END */ }
2612}};
2613
2614INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2615
2616static struct action_kw_list tcp_req_cont_actions = {ILH, {
2617 { "do-resolve", dns_parse_do_resolve, 1 },
2618 { /* END */ }
2619}};
2620
2621INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2622
2623/* Check an "http-request do-resolve" action.
2624 *
2625 * The function returns 1 in success case, otherwise, it returns 0 and err is
2626 * filled.
2627 */
2628int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2629{
2630 struct dns_resolvers *resolvers = NULL;
2631
2632 if (rule->arg.dns.resolvers_id == NULL) {
2633 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2634 return 0;
2635 }
2636
2637 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2638 if (resolvers == NULL) {
2639 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2640 return 0;
2641 }
2642 rule->arg.dns.resolvers = resolvers;
2643
2644 return 1;
2645}
2646
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002647REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002648REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);