blob: 953f9414c41d83a2a17a0f8ead592658d45df64d [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
Baptiste Assmann044fd5b2018-08-10 10:56:38 +020022#include <common/cfgparse.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020023#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020025#include <common/time.h>
26#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020027#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020028
Baptiste Assmann333939c2019-01-21 08:34:50 +010029#include <types/action.h>
William Lallemand69e96442016-11-19 00:58:54 +010030#include <types/applet.h>
31#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032#include <types/global.h>
33#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010034#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035
Baptiste Assmann333939c2019-01-21 08:34:50 +010036#include <proto/action.h>
William Lallemand69e96442016-11-19 00:58:54 +010037#include <proto/channel.h>
38#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020039#include <proto/checks.h>
40#include <proto/dns.h>
41#include <proto/fd.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020042#include <proto/http_ana.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010043#include <proto/http_rules.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020044#include <proto/log.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010045#include <proto/sample.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020046#include <proto/server.h>
47#include <proto/task.h>
48#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020049#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010050#include <proto/stream_interface.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010051#include <proto/tcp_rules.h>
52#include <proto/vars.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020053
Christopher Faulet67957bd2017-09-27 11:00:59 +020054struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
55struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020056
Tim Duesterhusfcac33d2020-01-18 02:04:12 +010057static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010058
59DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
60DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010061DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010062
Christopher Faulet67957bd2017-09-27 11:00:59 +020063static unsigned int resolution_uuid = 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +010064unsigned int dns_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065
Christopher Faulet67957bd2017-09-27 11:00:59 +020066/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
67 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020068 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020069struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020070{
Christopher Faulet67957bd2017-09-27 11:00:59 +020071 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020072
Christopher Faulet67957bd2017-09-27 11:00:59 +020073 list_for_each_entry(res, &dns_resolvers, list) {
74 if (!strcmp(res->id, id))
75 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020076 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020077 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020078}
79
Christopher Faulet67957bd2017-09-27 11:00:59 +020080/* Returns a pointer on the SRV request matching the name <name> for the proxy
81 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020082 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020083struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020084{
Christopher Faulet67957bd2017-09-27 11:00:59 +020085 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020086
Christopher Faulet67957bd2017-09-27 11:00:59 +020087 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
88 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
89 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020090 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020091 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020092}
93
Christopher Faulet67957bd2017-09-27 11:00:59 +020094/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
95 * NULL if an error occurred. */
96struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020097{
Christopher Faulet67957bd2017-09-27 11:00:59 +020098 struct proxy *px = srv->proxy;
99 struct dns_srvrq *srvrq = NULL;
100 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200101
Christopher Faulet67957bd2017-09-27 11:00:59 +0200102 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200103 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
104 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200105 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100106 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
107 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200108 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200109 }
110
Christopher Faulet67957bd2017-09-27 11:00:59 +0200111 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100112 ha_alert("config : %s '%s', server '%s': out of memory\n",
113 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200114 goto err;
115 }
116 srvrq->obj_type = OBJ_TYPE_SRVRQ;
117 srvrq->proxy = px;
118 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200119 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200120 srvrq->hostname_dn_len = hostname_dn_len;
121 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100122 ha_alert("config : %s '%s', server '%s': out of memory\n",
123 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200124 goto err;
125 }
126 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
127 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200128
Christopher Faulet67957bd2017-09-27 11:00:59 +0200129 err:
130 if (srvrq) {
131 free(srvrq->name);
132 free(srvrq->hostname_dn);
133 free(srvrq);
134 }
135 return NULL;
136}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200137
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200138
Christopher Faulet67957bd2017-09-27 11:00:59 +0200139/* 2 bytes random generator to generate DNS query ID */
140static inline uint16_t dns_rnd16(void)
141{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200142 if (!dns_query_id_seed)
143 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200144 dns_query_id_seed ^= dns_query_id_seed << 13;
145 dns_query_id_seed ^= dns_query_id_seed >> 7;
146 dns_query_id_seed ^= dns_query_id_seed << 17;
147 return dns_query_id_seed;
148}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200149
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151static inline int dns_resolution_timeout(struct dns_resolution *res)
152{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100153 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200154}
155
Christopher Faulet67957bd2017-09-27 11:00:59 +0200156/* Updates a resolvers' task timeout for next wake up and queue it */
157static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200158{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200159 struct dns_resolution *res;
160 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200161
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162 next = tick_add(now_ms, resolvers->timeout.resolve);
163 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
164 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
165 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
166 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200167
Christopher Faulet67957bd2017-09-27 11:00:59 +0200168 list_for_each_entry(res, &resolvers->resolutions.wait, list)
169 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200170
Christopher Faulet67957bd2017-09-27 11:00:59 +0200171 resolvers->t->expire = next;
172 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173}
174
Christopher Faulet67957bd2017-09-27 11:00:59 +0200175/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
176 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200177 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200178static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200179{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200180 struct dgram_conn *dgram = ns->dgram;
181 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200182
Christopher Faulet67957bd2017-09-27 11:00:59 +0200183 /* Already connected */
184 if (dgram->t.sock.fd != -1)
185 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200186
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187 /* Create an UDP socket and connect it on the nameserver's IP/Port */
188 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
189 send_log(NULL, LOG_WARNING,
190 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
191 ns->resolvers->id, ns->id);
192 return -1;
193 }
194 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
195 send_log(NULL, LOG_WARNING,
196 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
197 ns->resolvers->id, ns->id);
198 close(fd);
199 return -1;
200 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200201
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202 /* Make the socket non blocking */
203 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200204
Christopher Faulet67957bd2017-09-27 11:00:59 +0200205 /* Add the fd in the fd list and update its parameters */
206 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100207 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200208 fd_want_recv(fd);
209 return 0;
210}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200211
Christopher Faulet67957bd2017-09-27 11:00:59 +0200212/* Forges a DNS query. It needs the following information from the caller:
213 * - <query_id> : the DNS query id corresponding to this query
214 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
215 * - <hostname_dn> : hostname in domain name format
216 * - <hostname_dn_len> : length of <hostname_dn>
217 *
218 * To store the query, the caller must pass a buffer <buf> and its size
219 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
220 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200221 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200222static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
223 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200224{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200225 struct dns_header dns_hdr;
226 struct dns_question qinfo;
227 struct dns_additional_record edns;
228 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200229
Christopher Faulet67957bd2017-09-27 11:00:59 +0200230 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
231 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200232
Christopher Faulet67957bd2017-09-27 11:00:59 +0200233 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200234
Christopher Faulet67957bd2017-09-27 11:00:59 +0200235 /* Set dns query headers */
236 dns_hdr.id = (unsigned short) htons(query_id);
237 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
238 dns_hdr.qdcount = htons(1); /* 1 question */
239 dns_hdr.ancount = 0;
240 dns_hdr.nscount = 0;
241 dns_hdr.arcount = htons(1);
242 memcpy(p, &dns_hdr, sizeof(dns_hdr));
243 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200244
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 /* Set up query hostname */
246 memcpy(p, hostname_dn, hostname_dn_len);
247 p += hostname_dn_len;
248 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200249
Christopher Faulet67957bd2017-09-27 11:00:59 +0200250 /* Set up query info (type and class) */
251 qinfo.qtype = htons(query_type);
252 qinfo.qclass = htons(DNS_RCLASS_IN);
253 memcpy(p, &qinfo, sizeof(qinfo));
254 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200255
Christopher Faulet67957bd2017-09-27 11:00:59 +0200256 /* Set the DNS extension */
257 edns.name = 0;
258 edns.type = htons(DNS_RTYPE_OPT);
259 edns.udp_payload_size = htons(accepted_payload_size);
260 edns.extension = 0;
261 edns.data_length = 0;
262 memcpy(p, &edns, sizeof(edns));
263 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200264
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266}
267
Christopher Faulet67957bd2017-09-27 11:00:59 +0200268/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
269 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200270 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200271static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200272{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200273 struct dns_resolvers *resolvers = resolution->resolvers;
274 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100275 int len;
276
277 /* Update resolution */
278 resolution->nb_queries = 0;
279 resolution->nb_responses = 0;
280 resolution->last_query = now_ms;
281
282 len = dns_build_query(resolution->query_id, resolution->query_type,
283 resolvers->accepted_payload_size,
284 resolution->hostname_dn, resolution->hostname_dn_len,
285 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200286
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287 list_for_each_entry(ns, &resolvers->nameservers, list) {
288 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100289 int ret;
290
Christopher Faulet67957bd2017-09-27 11:00:59 +0200291 if (fd == -1) {
292 if (dns_connect_namesaver(ns) == -1)
293 continue;
294 fd = ns->dgram->t.sock.fd;
295 resolvers->nb_nameservers++;
296 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200297
Willy Tarreau0eae6322019-12-20 11:18:54 +0100298 if (len < 0)
299 goto snd_error;
300
301 ret = send(fd, trash.area, len, 0);
302 if (ret == len) {
303 ns->counters.sent++;
304 resolution->nb_queries++;
305 continue;
306 }
307
308 if (ret == -1 && errno == EAGAIN) {
309 /* retry once the socket is ready */
310 fd_cant_send(fd);
311 continue;
312 }
313
314 snd_error:
315 ns->counters.snd_error++;
316 resolution->nb_queries++;
317 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200318
Christopher Faulet67957bd2017-09-27 11:00:59 +0200319 /* Push the resolution at the end of the active list */
320 LIST_DEL(&resolution->list);
321 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
322 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200323}
324
Christopher Faulet67957bd2017-09-27 11:00:59 +0200325/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
326 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200327 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200328static int
329dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200330{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200331 struct dns_resolvers *resolvers = resolution->resolvers;
332 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333
Christopher Faulet67957bd2017-09-27 11:00:59 +0200334 /* Avoid sending requests for resolutions that don't yet have an
335 * hostname, ie resolutions linked to servers that do not yet have an
336 * fqdn */
337 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200338 return 0;
339
Christopher Faulet67957bd2017-09-27 11:00:59 +0200340 /* Check if a resolution has already been started for this server return
341 * directly to avoid resolution pill up. */
342 if (resolution->step != RSLV_STEP_NONE)
343 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200344
Christopher Faulet67957bd2017-09-27 11:00:59 +0200345 /* Generates a new query id. We try at most 100 times to find a free
346 * query id */
347 for (i = 0; i < 100; ++i) {
348 query_id = dns_rnd16();
349 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200350 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200351 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200352 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200353 if (query_id == -1) {
354 send_log(NULL, LOG_NOTICE,
355 "could not generate a query id for %s, in resolvers %s.\n",
356 resolution->hostname_dn, resolvers->id);
357 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200358 }
359
Christopher Faulet67957bd2017-09-27 11:00:59 +0200360 /* Update resolution parameters */
361 resolution->query_id = query_id;
362 resolution->qid.key = query_id;
363 resolution->step = RSLV_STEP_RUNNING;
364 resolution->query_type = resolution->prefered_query_type;
365 resolution->try = resolvers->resolve_retries;
366 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200367
Christopher Faulet67957bd2017-09-27 11:00:59 +0200368 /* Send the DNS query */
369 resolution->try -= 1;
370 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200371 return 1;
372}
373
Christopher Faulet67957bd2017-09-27 11:00:59 +0200374/* Performs a name resolution for the requester <req> */
375void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200376{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200377 struct dns_resolvers *resolvers;
378 struct dns_resolution *res;
379 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200380
Christopher Faulet67957bd2017-09-27 11:00:59 +0200381 if (!req || !req->resolution)
382 return;
383 res = req->resolution;
384 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200385
Christopher Faulet67957bd2017-09-27 11:00:59 +0200386 /* The resolution must not be triggered yet. Use the cached response, if
387 * valid */
388 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200389 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
390 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
391 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200392}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200393
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200394
Christopher Faulet67957bd2017-09-27 11:00:59 +0200395/* Resets some resolution parameters to initial values and also delete the query
396 * ID from the resolver's tree.
397 */
398static void dns_reset_resolution(struct dns_resolution *resolution)
399{
400 /* update resolution status */
401 resolution->step = RSLV_STEP_NONE;
402 resolution->try = 0;
403 resolution->last_resolution = now_ms;
404 resolution->nb_queries = 0;
405 resolution->nb_responses = 0;
406 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200407
Christopher Faulet67957bd2017-09-27 11:00:59 +0200408 /* clean up query id */
409 eb32_delete(&resolution->qid);
410 resolution->query_id = 0;
411 resolution->qid.key = 0;
412}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200413
Christopher Faulet67957bd2017-09-27 11:00:59 +0200414/* Returns the query id contained in a DNS response */
415static inline unsigned short dns_response_get_query_id(unsigned char *resp)
416{
417 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200418}
419
Christopher Faulet67957bd2017-09-27 11:00:59 +0200420
421/* Analyses, re-builds and copies the name <name> from the DNS response packet
422 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
423 * for compressed data. The result is copied into <dest>, ensuring we don't
424 * overflow using <dest_len> Returns the number of bytes the caller can move
425 * forward. If 0 it means an error occurred while parsing the name. <offset> is
426 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200427 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200428int dns_read_name(unsigned char *buffer, unsigned char *bufend,
429 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100430 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200431{
432 int nb_bytes = 0, n = 0;
433 int label_len;
434 unsigned char *reader = name;
435 char *dest = destination;
436
437 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100438 if (reader >= bufend)
439 goto err;
440
Christopher Faulet67957bd2017-09-27 11:00:59 +0200441 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200442 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100443 if (reader + 1 >= bufend)
444 goto err;
445
Christopher Faulet67957bd2017-09-27 11:00:59 +0200446 /* Must point BEFORE current position */
447 if ((buffer + reader[1]) > reader)
448 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200449
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100450 if (depth++ > 100)
451 goto err;
452
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530453 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100454 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200455 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200456 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200457
Christopher Faulet67957bd2017-09-27 11:00:59 +0200458 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200459 nb_bytes += n;
460 goto out;
461 }
462
463 label_len = *reader;
464 if (label_len == 0)
465 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200466
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200467 /* Check if:
468 * - we won't read outside the buffer
469 * - there is enough place in the destination
470 */
471 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200472 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200473
474 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200475 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200476
477 memcpy(dest, reader, label_len);
478
Christopher Faulet67957bd2017-09-27 11:00:59 +0200479 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200480 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200481 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200482 }
483
Christopher Faulet67957bd2017-09-27 11:00:59 +0200484 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200485 /* offset computation:
486 * parse from <name> until finding either NULL or a pointer "c0xx"
487 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200488 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200489 *offset = 0;
490 while (reader < bufend) {
491 if ((reader[0] & 0xc0) == 0xc0) {
492 *offset += 2;
493 break;
494 }
495 else if (*reader == 0) {
496 *offset += 1;
497 break;
498 }
499 *offset += 1;
500 ++reader;
501 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200502 return nb_bytes;
503
Christopher Faulet67957bd2017-09-27 11:00:59 +0200504 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200505 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200506}
507
Christopher Faulet67957bd2017-09-27 11:00:59 +0200508/* Checks for any obsolete record, also identify any SRV request, and try to
509 * find a corresponding server.
510*/
511static void dns_check_dns_response(struct dns_resolution *res)
512{
513 struct dns_resolvers *resolvers = res->resolvers;
514 struct dns_requester *req, *reqback;
515 struct dns_answer_item *item, *itemback;
516 struct server *srv;
517 struct dns_srvrq *srvrq;
518
Baptiste Assmann13a92322019-06-07 09:40:55 +0200519 /* clean up obsolete Additional records */
520 list_for_each_entry_safe(item, itemback, &res->response.ar_list, list) {
521 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
522 LIST_DEL(&item->list);
523 pool_free(dns_answer_item_pool, item);
524 }
525 }
526
Christopher Faulet67957bd2017-09-27 11:00:59 +0200527 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
528
529 /* Remove obsolete items */
530 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
531 if (item->type != DNS_RTYPE_SRV)
532 goto rm_obselete_item;
533
534 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
535 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
536 continue;
537
538 /* Remove any associated server */
539 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100540 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200541 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
542 item->data_len == srv->hostname_dn_len &&
543 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
544 snr_update_srv_status(srv, 1);
545 free(srv->hostname);
546 free(srv->hostname_dn);
547 srv->hostname = NULL;
548 srv->hostname_dn = NULL;
549 srv->hostname_dn_len = 0;
550 dns_unlink_resolution(srv->dns_requester);
551 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100552 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200553 }
554 }
555
556 rm_obselete_item:
557 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100558 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200559 continue;
560 }
561
562 if (item->type != DNS_RTYPE_SRV)
563 continue;
564
565 /* Now process SRV records */
566 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
567 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
568 continue;
569
570 /* Check if a server already uses that hostname */
571 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100572 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200573 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
574 item->data_len == srv->hostname_dn_len &&
Daniel Corbettf8716912019-11-17 09:48:56 -0500575 !memcmp(srv->hostname_dn, item->target, item->data_len) &&
576 !srv->dns_opts.ignore_weight) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100577 int ha_weight;
578
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200579 /* DNS weight range if from 0 to 65535
580 * HAProxy weight is from 0 to 256
581 * The rule below ensures that weight 0 is well respected
582 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100583 */
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200584 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100585 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200586 char weight[9];
587
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100588 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200589 server_parse_weight_change_request(srv, weight);
590 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100591 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200592 break;
593 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100594 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200595 }
596 if (srv)
597 continue;
598
599 /* If not, try to find a server with undefined hostname */
600 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100601 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200602 if (srv->srvrq == srvrq && !srv->hostname_dn)
603 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100604 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200605 }
606 /* And update this server, if found */
607 if (srv) {
608 const char *msg = NULL;
609 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100610 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200611 char hostname[DNS_MAX_NAME_SIZE];
612
613 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100614 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100615 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200616 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100617 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200618
619 /* Check if an Additional Record is associated to this SRV record.
620 * Perform some sanity checks too to ensure the record can be used.
621 * If all fine, we simply pick up the IP address found and associate
622 * it to the server.
623 */
624 if ((item->ar_item != NULL) &&
625 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
626 {
627
628 switch (item->ar_item->type) {
629 case DNS_RTYPE_A:
630 update_server_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional recrd");
631 break;
632 case DNS_RTYPE_AAAA:
633 update_server_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional recrd");
634 break;
635 }
636
637 srv->flags |= SRV_F_NO_RESOLUTION;
638 }
639
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100640 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200641 if (msg)
642 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
643
644 srv->svc_port = item->port;
645 srv->flags &= ~SRV_F_MAPPORTS;
646 if ((srv->check.state & CHK_ST_CONFIGURED) &&
647 !(srv->flags & SRV_F_CHECKPORT))
648 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100649
Daniel Corbettf8716912019-11-17 09:48:56 -0500650 if (!srv->dns_opts.ignore_weight) {
651 /* DNS weight range if from 0 to 65535
652 * HAProxy weight is from 0 to 256
653 * The rule below ensures that weight 0 is well respected
654 * while allowing a "mapping" from DNS weight into HAProxy's one.
655 */
656 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100657
Daniel Corbettf8716912019-11-17 09:48:56 -0500658 snprintf(weight, sizeof(weight), "%d", ha_weight);
659 server_parse_weight_change_request(srv, weight);
660 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100661 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200662 }
663 }
664 }
665}
666
667/* Validates that the buffer DNS response provided in <resp> and finishing
668 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200670 * The result is stored in <resolution>' response, buf_response,
671 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200672 *
673 * This function returns one of the DNS_RESP_* code to indicate the type of
674 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200675 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200676static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
677 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200678{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 unsigned char *reader;
680 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200681 int len, flags, offset;
682 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200683 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200685 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200686 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200687 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200688
Christopher Faulet67957bd2017-09-27 11:00:59 +0200689 reader = resp;
690 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200691 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200692 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200693
Christopher Faulet67957bd2017-09-27 11:00:59 +0200694 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200695 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696
697 /* query id */
698 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200699 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200700 dns_p->header.id = reader[0] * 256 + reader[1];
701 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200702
Christopher Faulet67957bd2017-09-27 11:00:59 +0200703 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200704 * First byte contains:
705 * - response flag (1 bit)
706 * - opcode (4 bits)
707 * - authoritative (1 bit)
708 * - truncated (1 bit)
709 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200710 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200711 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200712 return DNS_RESP_INVALID;
713
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200714 flags = reader[0] * 256 + reader[1];
715
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200716 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
717 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200718 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200719 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200720 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200721 return DNS_RESP_ERROR;
722 }
723
Christopher Faulet67957bd2017-09-27 11:00:59 +0200724 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200725 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200726
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200727 /* 2 bytes for question count */
728 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200729 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200730 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200731 /* (for now) we send one query only, so we expect only one in the
732 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200733 if (dns_p->header.qdcount != 1)
734 return DNS_RESP_QUERY_COUNT_ERROR;
735 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200736 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200737 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200738
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200739 /* 2 bytes for answer count */
740 if (reader + 2 >= bufend)
741 return DNS_RESP_INVALID;
742 dns_p->header.ancount = reader[0] * 256 + reader[1];
743 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200744 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200745 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200746 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200747 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200748 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200749
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200750 /* 2 bytes authority count */
751 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 dns_p->header.nscount = reader[0] * 256 + reader[1];
754 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200755
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200756 /* 2 bytes additional count */
757 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200758 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200759 dns_p->header.arcount = reader[0] * 256 + reader[1];
760 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200761
Christopher Faulet67957bd2017-09-27 11:00:59 +0200762 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200763 LIST_INIT(&dns_p->query_list);
764 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 +0200765 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200767 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200768 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
769 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200770 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200771 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200772
Christopher Faulet67957bd2017-09-27 11:00:59 +0200773 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200774 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200775 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200776 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100777 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200778
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 if (len == 0)
780 return DNS_RESP_INVALID;
781
782 reader += offset;
783 previous_dname = dns_query->name;
784
785 /* move forward 2 bytes for question type */
786 if (reader + 2 >= bufend)
787 return DNS_RESP_INVALID;
788 dns_query->type = reader[0] * 256 + reader[1];
789 reader += 2;
790
791 /* move forward 2 bytes for question class */
792 if (reader + 2 >= bufend)
793 return DNS_RESP_INVALID;
794 dns_query->class = reader[0] * 256 + reader[1];
795 reader += 2;
796 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200797
Baptiste Assmann251abb92017-08-11 09:58:27 +0200798 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200799 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200800 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
801 return DNS_RESP_TRUNCATED;
802
Baptiste Assmann325137d2015-04-13 23:40:55 +0200803 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200804 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200805 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200806 if (reader >= bufend)
807 return DNS_RESP_INVALID;
808
Willy Tarreaubafbe012017-11-24 17:34:44 +0100809 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200810 if (dns_answer_record == NULL)
811 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200812
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200813 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100814 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200815
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200816 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100817 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200818 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200819 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200820
Christopher Faulet67957bd2017-09-27 11:00:59 +0200821 /* Check if the current record dname is valid. previous_dname
822 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200823 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100824 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200825 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200826 /* First record, means a mismatch issue between
827 * queried dname and dname found in the first
828 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200829 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200830 }
831 else {
832 /* If not the first record, this means we have a
833 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200834 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200835 }
836
Baptiste Assmann325137d2015-04-13 23:40:55 +0200837 }
838
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200839 memcpy(dns_answer_record->name, tmpname, len);
840 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200841
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200842 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200843 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100844 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200845 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200846 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200847
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200848 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200849 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100850 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200851 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200852 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200853 dns_answer_record->type = reader[0] * 256 + reader[1];
854 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200855
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200856 /* 2 bytes for class (2) */
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 Assmannc1ce5f32016-05-14 11:26:22 +0200859 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200860 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200861 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200862 reader += 2;
863
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200864 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200865 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100866 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200867 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200868 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200869 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
870 + reader[2] * 256 + reader[3];
871 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200872
Christopher Faulet67957bd2017-09-27 11:00:59 +0200873 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200874 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100875 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200876 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200877 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200878 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200879
Christopher Faulet67957bd2017-09-27 11:00:59 +0200880 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200881 reader += 2;
882
Jérôme Magnin8d4e7dc2018-12-20 16:47:31 +0100883 if (reader + dns_answer_record->data_len > bufend) {
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100884 pool_free(dns_answer_item_pool, dns_answer_record);
885 return DNS_RESP_INVALID;
886 }
887
Christopher Faulet67957bd2017-09-27 11:00:59 +0200888 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200889 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200890 case DNS_RTYPE_A:
891 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200892 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100893 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200894 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200895 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200896 dns_answer_record->address.sa_family = AF_INET;
897 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
898 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200899 break;
900
901 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200902 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200903 * no IP could be found and last record was a CNAME. Could be triggered
904 * by a wrong query type
905 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200906 * + 1 because dns_answer_record_id starts at 0
907 * while number of answers is an integer and
908 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200909 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200910 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100911 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200912 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200913 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200914
915 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100916 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200917 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100918 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200919 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200920 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200921
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200922 memcpy(dns_answer_record->target, tmpname, len);
923 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200924 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200925 break;
926
Olivier Houchard8da5f982017-08-04 18:35:36 +0200927
928 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200929 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200930 * - 2 bytes for the priority
931 * - 2 bytes for the weight
932 * - 2 bytes for the port
933 * - the target hostname
934 */
935 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100936 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200937 return DNS_RESP_INVALID;
938 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200939 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200940 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200941 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200942 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200943 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200944 reader += sizeof(uint16_t);
945 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100946 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200947 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100948 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200949 return DNS_RESP_INVALID;
950 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200951 dns_answer_record->data_len = len;
952 memcpy(dns_answer_record->target, tmpname, len);
953 dns_answer_record->target[len] = 0;
954 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200955
Baptiste Assmann325137d2015-04-13 23:40:55 +0200956 case DNS_RTYPE_AAAA:
957 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200958 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100959 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200960 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200961 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200962 dns_answer_record->address.sa_family = AF_INET6;
963 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
964 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200965 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200966
Baptiste Assmann325137d2015-04-13 23:40:55 +0200967 } /* switch (record type) */
968
Christopher Faulet67957bd2017-09-27 11:00:59 +0200969 /* Increment the counter for number of records saved into our
970 * local response */
971 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200972
Christopher Faulet67957bd2017-09-27 11:00:59 +0200973 /* Move forward dns_answer_record->data_len for analyzing next
974 * record in the response */
975 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
976 ? offset
977 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200978
979 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200980 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200981 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
982 if (tmp_record->type != dns_answer_record->type)
983 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200984
985 switch(tmp_record->type) {
986 case DNS_RTYPE_A:
987 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
988 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
989 sizeof(in_addr_t)))
990 found = 1;
991 break;
992
993 case DNS_RTYPE_AAAA:
994 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
995 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
996 sizeof(struct in6_addr)))
997 found = 1;
998 break;
999
Olivier Houchard8da5f982017-08-04 18:35:36 +02001000 case DNS_RTYPE_SRV:
1001 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001002 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001003 dns_answer_record->port == tmp_record->port) {
1004 tmp_record->weight = dns_answer_record->weight;
1005 found = 1;
1006 }
1007 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001008
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001009 default:
1010 break;
1011 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001012
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001013 if (found == 1)
1014 break;
1015 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001016
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001017 if (found == 1) {
1018 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001019 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001020 }
1021 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001022 dns_answer_record->last_seen = now.tv_sec;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001023 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001024 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1025 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001026 } /* for i 0 to ancount */
1027
Christopher Faulet67957bd2017-09-27 11:00:59 +02001028 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001029 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001030
Baptiste Assmann37950c82020-02-19 01:08:51 +01001031 /* now parsing additional records for SRV queries only */
1032 if (dns_query->type != DNS_RTYPE_SRV)
1033 goto skip_parsing_additional_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001034 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001035 for (i = 0; i < dns_p->header.arcount; i++) {
1036 if (reader >= bufend)
1037 return DNS_RESP_INVALID;
1038
1039 dns_answer_record = pool_alloc(dns_answer_item_pool);
1040 if (dns_answer_record == NULL)
1041 return (DNS_RESP_INVALID);
1042
1043 offset = 0;
1044 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1045
1046 if (len == 0) {
1047 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann37950c82020-02-19 01:08:51 +01001048 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001049 }
1050
1051 memcpy(dns_answer_record->name, tmpname, len);
1052 dns_answer_record->name[len] = 0;
1053
1054 reader += offset;
1055 if (reader >= bufend) {
1056 pool_free(dns_answer_item_pool, dns_answer_record);
1057 return DNS_RESP_INVALID;
1058 }
1059
1060 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1061 if (reader + 2 > bufend) {
1062 pool_free(dns_answer_item_pool, dns_answer_record);
1063 return DNS_RESP_INVALID;
1064 }
1065 dns_answer_record->type = reader[0] * 256 + reader[1];
1066 reader += 2;
1067
1068 /* 2 bytes for class (2) */
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->class = reader[0] * 256 + reader[1];
1074 reader += 2;
1075
1076 /* 4 bytes for ttl (4) */
1077 if (reader + 4 > bufend) {
1078 pool_free(dns_answer_item_pool, dns_answer_record);
1079 return DNS_RESP_INVALID;
1080 }
1081 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1082 + reader[2] * 256 + reader[3];
1083 reader += 4;
1084
1085 /* Now reading data len */
1086 if (reader + 2 > bufend) {
1087 pool_free(dns_answer_item_pool, dns_answer_record);
1088 return DNS_RESP_INVALID;
1089 }
1090 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1091
1092 /* Move forward 2 bytes for data len */
1093 reader += 2;
1094
1095 if (reader + dns_answer_record->data_len > bufend) {
1096 pool_free(dns_answer_item_pool, dns_answer_record);
1097 return DNS_RESP_INVALID;
1098 }
1099
1100 /* Analyzing record content */
1101 switch (dns_answer_record->type) {
1102 case DNS_RTYPE_A:
1103 /* ipv4 is stored on 4 bytes */
1104 if (dns_answer_record->data_len != 4) {
1105 pool_free(dns_answer_item_pool, dns_answer_record);
1106 return DNS_RESP_INVALID;
1107 }
1108 dns_answer_record->address.sa_family = AF_INET;
1109 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1110 reader, dns_answer_record->data_len);
1111 break;
1112
1113 case DNS_RTYPE_AAAA:
1114 /* ipv6 is stored on 16 bytes */
1115 if (dns_answer_record->data_len != 16) {
1116 pool_free(dns_answer_item_pool, dns_answer_record);
1117 return DNS_RESP_INVALID;
1118 }
1119 dns_answer_record->address.sa_family = AF_INET6;
1120 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1121 reader, dns_answer_record->data_len);
1122 break;
1123
1124 default:
1125 pool_free(dns_answer_item_pool, dns_answer_record);
1126 continue;
1127
1128 } /* switch (record type) */
1129
1130 /* Increment the counter for number of records saved into our
1131 * local response */
1132 nb_saved_records++;
1133
1134 /* Move forward dns_answer_record->data_len for analyzing next
1135 * record in the response */
1136 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1137 ? offset
1138 : dns_answer_record->data_len);
1139
1140 /* Lookup to see if we already had this entry */
1141 found = 0;
1142 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1143 if (tmp_record->type != dns_answer_record->type)
1144 continue;
1145
1146 switch(tmp_record->type) {
1147 case DNS_RTYPE_A:
1148 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1149 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1150 sizeof(in_addr_t)))
1151 found = 1;
1152 break;
1153
1154 case DNS_RTYPE_AAAA:
1155 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1156 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1157 sizeof(struct in6_addr)))
1158 found = 1;
1159 break;
1160
1161 default:
1162 break;
1163 }
1164
1165 if (found == 1)
1166 break;
1167 }
1168
1169 if (found == 1) {
1170 tmp_record->last_seen = now.tv_sec;
1171 pool_free(dns_answer_item_pool, dns_answer_record);
1172 }
1173 else {
1174 dns_answer_record->last_seen = now.tv_sec;
1175 dns_answer_record->ar_item = NULL;
1176
1177 // looking for the SRV record in the response list linked to this additional record
1178 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1179 if ( !(
1180 (tmp_record->type == DNS_RTYPE_SRV) &&
1181 (tmp_record->ar_item == NULL) &&
1182 (memcmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len) == 0)
1183 )
1184 )
1185 continue;
1186 tmp_record->ar_item = dns_answer_record;
1187 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001188
1189 LIST_ADDQ(&dns_p->ar_list, &dns_answer_record->list);
1190 }
1191 } /* for i 0 to arcount */
1192
Baptiste Assmann37950c82020-02-19 01:08:51 +01001193 skip_parsing_additional_records:
1194
Baptiste Assmann13a92322019-06-07 09:40:55 +02001195 /* Save the number of records we really own */
1196 dns_p->header.arcount = nb_saved_records;
1197
Christopher Faulet67957bd2017-09-27 11:00:59 +02001198 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001199 return DNS_RESP_VALID;
1200}
1201
Christopher Faulet67957bd2017-09-27 11:00:59 +02001202/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001203 * If existing IP not found, return the first IP matching family_priority,
1204 * otherwise, first ip found
1205 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001206 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001207 * For both cases above, dns_validate_dns_response is required
1208 * returns one of the DNS_UPD_* code
1209 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001210int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001211 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001212 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001213 void **newip, short *newip_sin_family,
1214 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001215{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001216 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001217 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001218 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001219 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001220 int currentip_sel;
1221 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001222 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001223 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001224
Christopher Faulet67957bd2017-09-27 11:00:59 +02001225 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001226 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001227 *newip = newip4 = newip6 = NULL;
1228 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001229 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001230 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001231
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001232 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001233 * Top priority is the preferred network ip version,
1234 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001235 * the last priority is the currently used IP,
1236 *
1237 * For these three priorities, a score is calculated. The
1238 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001239 * 8 - preferred ip version.
1240 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001241 * 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 +01001242 * 1 - current ip.
1243 * The result with the biggest score is returned.
1244 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001245
1246 list_for_each_entry(record, &dns_p->answer_list, list) {
1247 void *ip;
1248 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001249
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001250 if (record->type == DNS_RTYPE_A) {
1251 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1252 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001253 }
1254 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001255 ip_type = AF_INET6;
1256 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001257 }
1258 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001259 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001260 score = 0;
1261
Joseph Herlant42cf6392018-11-15 10:33:28 -08001262 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001263 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001264 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001265
Joseph Herlant42cf6392018-11-15 10:33:28 -08001266 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001267 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001268
1269 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001270 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001271 continue;
1272
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001273 if ((ip_type == AF_INET &&
1274 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001275 &dns_opts->pref_net[j].mask.in4,
1276 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001277 (ip_type == AF_INET6 &&
1278 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001279 &dns_opts->pref_net[j].mask.in6,
1280 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001281 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001282 break;
1283 }
1284 }
1285
Christopher Faulet67957bd2017-09-27 11:00:59 +02001286 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001287 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001288 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001289 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001290 if (!allowed_duplicated_ip) {
1291 continue;
1292 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001293 } else {
1294 score += 2;
1295 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001296
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001297 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001298 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001299 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001300 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001301 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001302 !memcmp(ip, currentip, 16)))) {
1303 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001304 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001305 }
1306 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001307 currentip_sel = 0;
1308
1309 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001310 * score. The maximum score is 15, if this value is reached, we
1311 * break the parsing. Implicitly, this score is reached the ip
1312 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001313 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001314 if (ip_type == AF_INET)
1315 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001316 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001317 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001318 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001319 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001320 return DNS_UPD_NO;
1321 max_score = score;
1322 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001323 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001324
Christopher Faulet67957bd2017-09-27 11:00:59 +02001325 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001326 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001327 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001328
Christopher Faulet67957bd2017-09-27 11:00:59 +02001329 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001330 if (family_priority == AF_INET) {
1331 if (newip4) {
1332 *newip = newip4;
1333 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001334 }
1335 else if (newip6) {
1336 *newip = newip6;
1337 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001338 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001339 if (!currentip_found)
1340 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001341 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001342 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001343 else if (family_priority == AF_INET6) {
1344 if (newip6) {
1345 *newip = newip6;
1346 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001347 }
1348 else if (newip4) {
1349 *newip = newip4;
1350 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001351 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001352 if (!currentip_found)
1353 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001354 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001355 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001356 else if (family_priority == AF_UNSPEC) {
1357 if (newip6) {
1358 *newip = newip6;
1359 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001360 }
1361 else if (newip4) {
1362 *newip = newip4;
1363 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001364 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001365 if (!currentip_found)
1366 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001367 }
1368
Christopher Faulet67957bd2017-09-27 11:00:59 +02001369 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001370 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001371
Christopher Faulet67957bd2017-09-27 11:00:59 +02001372 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001373 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001374 /* Move the first record to the end of the list, for internal
1375 * round robin */
1376 LIST_DEL(&record->list);
1377 LIST_ADDQ(&dns_p->answer_list, &record->list);
1378 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001379 }
1380 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001381}
1382
Christopher Faulet67957bd2017-09-27 11:00:59 +02001383/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001384 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1386 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001387 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001388 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1389 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001390 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001391int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001392{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393 char *ptr;
1394 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001395
Christopher Faulet67957bd2017-09-27 11:00:59 +02001396 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001397 return -1;
1398
Christopher Faulet67957bd2017-09-27 11:00:59 +02001399 ptr = str;
1400 for (i = 0; i < dn_len-1; ++i) {
1401 sz = dn[i];
1402 if (i)
1403 *ptr++ = '.';
1404 memcpy(ptr, dn+i+1, sz);
1405 ptr += sz;
1406 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001407 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001408 *ptr++ = '\0';
1409 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001410}
1411
Christopher Faulet67957bd2017-09-27 11:00:59 +02001412/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1413 *
1414 * <str> must be a null-terminated string. <str_len> must include the
1415 * terminating null byte. <dn> buffer must be allocated and its size must be
1416 * passed in <dn_len>.
1417 *
1418 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1419 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001420 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001421int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001422{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001423 int i, offset;
1424
Christopher Faulet67957bd2017-09-27 11:00:59 +02001425 if (dn_len < str_len + 1)
1426 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001427
Christopher Faulet67957bd2017-09-27 11:00:59 +02001428 /* First byte of dn will be used to store the length of the first
1429 * label */
1430 offset = 0;
1431 for (i = 0; i < str_len; ++i) {
1432 if (str[i] == '.') {
1433 /* 2 or more consecutive dots is invalid */
1434 if (i == offset)
1435 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001436
Lukas Tribus81725b82020-02-27 15:47:24 +01001437 /* ignore trailing dot */
1438 if (i + 2 == str_len) {
1439 i++;
1440 break;
1441 }
1442
Christopher Faulet67957bd2017-09-27 11:00:59 +02001443 dn[offset] = (i - offset);
1444 offset = i+1;
1445 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001446 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001447 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001448 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001449 dn[offset] = (i - offset - 1);
1450 dn[i] = '\0';
1451 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001452}
1453
Christopher Faulet67957bd2017-09-27 11:00:59 +02001454/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001455 * - total size
1456 * - each label size individually
1457 * returns:
1458 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1459 * 1 when no error. <err> is left unaffected.
1460 */
1461int dns_hostname_validation(const char *string, char **err)
1462{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001463 int i;
1464
1465 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1466 if (err)
1467 *err = DNS_TOO_LONG_FQDN;
1468 return 0;
1469 }
1470
William Dauchyaecd5dc2020-01-26 19:52:34 +01001471 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001472 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001473 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1474 if (!(*string == '-' || *string == '_' ||
1475 (*string >= 'a' && *string <= 'z') ||
1476 (*string >= 'A' && *string <= 'Z') ||
1477 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001478 if (err)
1479 *err = DNS_INVALID_CHARACTER;
1480 return 0;
1481 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001482 i++;
1483 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001484 }
1485
William Dauchyaecd5dc2020-01-26 19:52:34 +01001486 if (!(*string))
1487 break;
1488
1489 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001490 if (err)
1491 *err = DNS_LABEL_TOO_LONG;
1492 return 0;
1493 }
1494
William Dauchyaecd5dc2020-01-26 19:52:34 +01001495 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001496 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001497 return 1;
1498}
1499
Christopher Faulet67957bd2017-09-27 11:00:59 +02001500/* Picks up an available resolution from the different resolution list
1501 * associated to a resolvers section, in this order:
1502 * 1. check in resolutions.curr for the same hostname and query_type
1503 * 2. check in resolutions.wait for the same hostname and query_type
1504 * 3. Get a new resolution from resolution pool
1505 *
1506 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001507 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001508static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1509 char **hostname_dn, int hostname_dn_len,
1510 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001511{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001512 struct dns_resolution *res;
1513
1514 if (!*hostname_dn)
1515 goto from_pool;
1516
1517 /* Search for same hostname and query type in resolutions.curr */
1518 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1519 if (!res->hostname_dn)
1520 continue;
1521 if ((query_type == res->prefered_query_type) &&
1522 hostname_dn_len == res->hostname_dn_len &&
1523 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1524 return res;
1525 }
1526
1527 /* Search for same hostname and query type in resolutions.wait */
1528 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1529 if (!res->hostname_dn)
1530 continue;
1531 if ((query_type == res->prefered_query_type) &&
1532 hostname_dn_len == res->hostname_dn_len &&
1533 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1534 return res;
1535 }
1536
1537 from_pool:
1538 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001539 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001540 if (res) {
1541 memset(res, 0, sizeof(*res));
1542 res->resolvers = resolvers;
1543 res->uuid = resolution_uuid;
1544 res->status = RSLV_STATUS_NONE;
1545 res->step = RSLV_STEP_NONE;
1546 res->last_valid = now_ms;
1547
1548 LIST_INIT(&res->requesters);
1549 LIST_INIT(&res->response.answer_list);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001550 LIST_INIT(&res->response.ar_list);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001551
1552 res->prefered_query_type = query_type;
1553 res->query_type = query_type;
1554 res->hostname_dn = *hostname_dn;
1555 res->hostname_dn_len = hostname_dn_len;
1556
1557 ++resolution_uuid;
1558
1559 /* Move the resolution to the resolvers wait queue */
1560 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1561 }
1562 return res;
1563}
1564
1565/* Releases a resolution from its requester(s) and move it back to the pool */
1566static void dns_free_resolution(struct dns_resolution *resolution)
1567{
1568 struct dns_requester *req, *reqback;
1569
1570 /* clean up configuration */
1571 dns_reset_resolution(resolution);
1572 resolution->hostname_dn = NULL;
1573 resolution->hostname_dn_len = 0;
1574
1575 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1576 LIST_DEL(&req->list);
1577 req->resolution = NULL;
1578 }
1579
1580 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001581 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001582}
1583
1584/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1585 * on success, -1 otherwise.
1586 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001587int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001588{
1589 struct dns_resolution *res = NULL;
1590 struct dns_requester *req;
1591 struct dns_resolvers *resolvers;
1592 struct server *srv = NULL;
1593 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001594 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 char **hostname_dn;
1596 int hostname_dn_len, query_type;
1597
1598 switch (requester_type) {
1599 case OBJ_TYPE_SERVER:
1600 srv = (struct server *)requester;
1601 hostname_dn = &srv->hostname_dn;
1602 hostname_dn_len = srv->hostname_dn_len;
1603 resolvers = srv->resolvers;
1604 query_type = ((srv->dns_opts.family_prio == AF_INET)
1605 ? DNS_RTYPE_A
1606 : DNS_RTYPE_AAAA);
1607 break;
1608
1609 case OBJ_TYPE_SRVRQ:
1610 srvrq = (struct dns_srvrq *)requester;
1611 hostname_dn = &srvrq->hostname_dn;
1612 hostname_dn_len = srvrq->hostname_dn_len;
1613 resolvers = srvrq->resolvers;
1614 query_type = DNS_RTYPE_SRV;
1615 break;
1616
Baptiste Assmann333939c2019-01-21 08:34:50 +01001617 case OBJ_TYPE_STREAM:
1618 stream = (struct stream *)requester;
1619 hostname_dn = &stream->dns_ctx.hostname_dn;
1620 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1621 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001622 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001623 ? DNS_RTYPE_A
1624 : DNS_RTYPE_AAAA);
1625 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001626 default:
1627 goto err;
1628 }
1629
1630 /* Get a resolution from the resolvers' wait queue or pool */
1631 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1632 goto err;
1633
1634 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001635 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001636 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001637 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001638 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001639 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001640 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001641 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001642 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001643 req->owner = &srv->obj_type;
1644 srv->dns_requester = req;
1645 }
1646 else
1647 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001648 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001649 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001650
1651 req->requester_cb = snr_resolution_cb;
1652 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001653 }
1654 else if (srvrq) {
1655 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001656 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001657 goto err;
1658 req->owner = &srvrq->obj_type;
1659 srvrq->dns_requester = req;
1660 }
1661 else
1662 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001663
1664 req->requester_cb = snr_resolution_cb;
1665 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001666 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001667 else if (stream) {
1668 if (stream->dns_ctx.dns_requester == NULL) {
1669 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1670 goto err;
1671 req->owner = &stream->obj_type;
1672 stream->dns_ctx.dns_requester = req;
1673 }
1674 else
1675 req = stream->dns_ctx.dns_requester;
1676
1677 req->requester_cb = act_resolution_cb;
1678 req->requester_error_cb = act_resolution_error_cb;
1679 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001680 else
1681 goto err;
1682
1683 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684
1685 LIST_ADDQ(&res->requesters, &req->list);
1686 return 0;
1687
1688 err:
1689 if (res && LIST_ISEMPTY(&res->requesters))
1690 dns_free_resolution(res);
1691 return -1;
1692}
1693
1694/* Removes a requester from a DNS resoltion. It takes takes care of all the
1695 * consequences. It also cleans up some parameters from the requester.
1696 */
1697void dns_unlink_resolution(struct dns_requester *requester)
1698{
1699 struct dns_resolution *res;
1700 struct dns_requester *req;
1701
1702 /* Nothing to do */
1703 if (!requester || !requester->resolution)
1704 return;
1705 res = requester->resolution;
1706
1707 /* Clean up the requester */
1708 LIST_DEL(&requester->list);
1709 requester->resolution = NULL;
1710
1711 /* We need to find another requester linked on this resolution */
1712 if (!LIST_ISEMPTY(&res->requesters))
1713 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1714 else {
1715 dns_free_resolution(res);
1716 return;
1717 }
1718
1719 /* Move hostname_dn related pointers to the next requester */
1720 switch (obj_type(req->owner)) {
1721 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001722 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1723 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001724 break;
1725 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001726 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1727 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001729 case OBJ_TYPE_STREAM:
1730 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1731 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1732 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001733 default:
1734 res->hostname_dn = NULL;
1735 res->hostname_dn_len = 0;
1736 break;
1737 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001738}
1739
Christopher Faulet67957bd2017-09-27 11:00:59 +02001740/* Called when a network IO is generated on a name server socket for an incoming
1741 * packet. It performs the following actions:
1742 * - check if the packet requires processing (not outdated resolution)
1743 * - ensure the DNS packet received is valid and call requester's callback
1744 * - call requester's error callback if invalid response
1745 * - check the dn_name in the packet against the one sent
1746 */
1747static void dns_resolve_recv(struct dgram_conn *dgram)
1748{
1749 struct dns_nameserver *ns, *tmpns;
1750 struct dns_resolvers *resolvers;
1751 struct dns_resolution *res;
1752 struct dns_query_item *query;
1753 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1754 unsigned char *bufend;
1755 int fd, buflen, dns_resp;
1756 int max_answer_records;
1757 unsigned short query_id;
1758 struct eb32_node *eb;
1759 struct dns_requester *req;
1760
1761 fd = dgram->t.sock.fd;
1762
1763 /* check if ready for reading */
1764 if (!fd_recv_ready(fd))
1765 return;
1766
1767 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001768 if ((ns = dgram->owner) == NULL) {
1769 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1770 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001771 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001772 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001773
1774 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001775 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001776
1777 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001778 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001779 /* read message received */
1780 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1781 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001782 /* FIXME : for now we consider EAGAIN only, but at
1783 * least we purge sticky errors that would cause us to
1784 * be called in loops.
1785 */
1786 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001787 fd_cant_recv(fd);
1788 break;
1789 }
1790
1791 /* message too big */
1792 if (buflen > resolvers->accepted_payload_size) {
1793 ns->counters.too_big++;
1794 continue;
1795 }
1796
1797 /* initializing variables */
1798 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1799
1800 /* read the query id from the packet (16 bits) */
1801 if (buf + 2 > bufend) {
1802 ns->counters.invalid++;
1803 continue;
1804 }
1805 query_id = dns_response_get_query_id(buf);
1806
1807 /* search the query_id in the pending resolution tree */
1808 eb = eb32_lookup(&resolvers->query_ids, query_id);
1809 if (eb == NULL) {
1810 /* unknown query id means an outdated response and can be safely ignored */
1811 ns->counters.outdated++;
1812 continue;
1813 }
1814
William Dauchybe8a3872019-11-27 23:32:41 +01001815 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001816 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001817 /* number of responses received */
1818 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001819
Christopher Faulet67957bd2017-09-27 11:00:59 +02001820 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1821 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001822
Christopher Faulet67957bd2017-09-27 11:00:59 +02001823 switch (dns_resp) {
1824 case DNS_RESP_VALID:
1825 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001826
Christopher Faulet67957bd2017-09-27 11:00:59 +02001827 case DNS_RESP_INVALID:
1828 case DNS_RESP_QUERY_COUNT_ERROR:
1829 case DNS_RESP_WRONG_NAME:
1830 res->status = RSLV_STATUS_INVALID;
1831 ns->counters.invalid++;
1832 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001833
Christopher Faulet67957bd2017-09-27 11:00:59 +02001834 case DNS_RESP_NX_DOMAIN:
1835 res->status = RSLV_STATUS_NX;
1836 ns->counters.nx++;
1837 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001838
Christopher Faulet67957bd2017-09-27 11:00:59 +02001839 case DNS_RESP_REFUSED:
1840 res->status = RSLV_STATUS_REFUSED;
1841 ns->counters.refused++;
1842 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001843
Christopher Faulet67957bd2017-09-27 11:00:59 +02001844 case DNS_RESP_ANCOUNT_ZERO:
1845 res->status = RSLV_STATUS_OTHER;
1846 ns->counters.any_err++;
1847 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001848
Christopher Faulet67957bd2017-09-27 11:00:59 +02001849 case DNS_RESP_CNAME_ERROR:
1850 res->status = RSLV_STATUS_OTHER;
1851 ns->counters.cname_error++;
1852 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001853
Christopher Faulet67957bd2017-09-27 11:00:59 +02001854 case DNS_RESP_TRUNCATED:
1855 res->status = RSLV_STATUS_OTHER;
1856 ns->counters.truncated++;
1857 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001858
Christopher Faulet67957bd2017-09-27 11:00:59 +02001859 case DNS_RESP_NO_EXPECTED_RECORD:
1860 case DNS_RESP_ERROR:
1861 case DNS_RESP_INTERNAL:
1862 res->status = RSLV_STATUS_OTHER;
1863 ns->counters.other++;
1864 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001865 }
1866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 /* Wait all nameservers response to handle errors */
1868 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1869 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001870
Christopher Faulet67957bd2017-09-27 11:00:59 +02001871 /* Process error codes */
1872 if (dns_resp != DNS_RESP_VALID) {
1873 if (res->prefered_query_type != res->query_type) {
1874 /* The fallback on the query type was already performed,
1875 * so check the try counter. If it falls to 0, we can
1876 * report an error. Else, wait the next attempt. */
1877 if (!res->try)
1878 goto report_res_error;
1879 }
1880 else {
1881 /* Fallback from A to AAAA or the opposite and re-send
1882 * the resolution immediately. try counter is not
1883 * decremented. */
1884 if (res->prefered_query_type == DNS_RTYPE_A) {
1885 res->query_type = DNS_RTYPE_AAAA;
1886 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001887 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1889 res->query_type = DNS_RTYPE_A;
1890 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001891 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001892 }
1893 continue;
1894 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001895
Christopher Faulet67957bd2017-09-27 11:00:59 +02001896 /* Now let's check the query's dname corresponds to the one we
1897 * sent. We can check only the first query of the list. We send
1898 * one query at a time so we get one query in the response */
1899 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1900 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1901 dns_resp = DNS_RESP_WRONG_NAME;
1902 ns->counters.other++;
1903 goto report_res_error;
1904 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001905
Christopher Faulet67957bd2017-09-27 11:00:59 +02001906 /* So the resolution succeeded */
1907 res->status = RSLV_STATUS_VALID;
1908 res->last_valid = now_ms;
1909 ns->counters.valid++;
1910 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001911
Christopher Faulet67957bd2017-09-27 11:00:59 +02001912 report_res_error:
1913 list_for_each_entry(req, &res->requesters, list)
1914 req->requester_error_cb(req, dns_resp);
1915 dns_reset_resolution(res);
1916 LIST_DEL(&res->list);
1917 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1918 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001919
Christopher Faulet67957bd2017-09-27 11:00:59 +02001920 report_res_success:
1921 /* Only the 1rst requester s managed by the server, others are
1922 * from the cache */
1923 tmpns = ns;
1924 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001925 struct server *s = objt_server(req->owner);
1926
1927 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001928 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001929 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001930 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001931 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001933 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001934
Christopher Faulet67957bd2017-09-27 11:00:59 +02001935 dns_reset_resolution(res);
1936 LIST_DEL(&res->list);
1937 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1938 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001939 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001940 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001941 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001942}
William Lallemand69e96442016-11-19 00:58:54 +01001943
Christopher Faulet67957bd2017-09-27 11:00:59 +02001944/* Called when a resolvers network socket is ready to send data */
1945static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001946{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001947 struct dns_resolvers *resolvers;
1948 struct dns_nameserver *ns;
1949 struct dns_resolution *res;
1950 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001951
Christopher Faulet67957bd2017-09-27 11:00:59 +02001952 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001953
Christopher Faulet67957bd2017-09-27 11:00:59 +02001954 /* check if ready for sending */
1955 if (!fd_send_ready(fd))
1956 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001957
Christopher Faulet67957bd2017-09-27 11:00:59 +02001958 /* we don't want/need to be waked up any more for sending */
1959 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001960
Christopher Faulet67957bd2017-09-27 11:00:59 +02001961 /* no need to go further if we can't retrieve the nameserver */
1962 if ((ns = dgram->owner) == NULL)
1963 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001964
Christopher Faulet67957bd2017-09-27 11:00:59 +02001965 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001966 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001967
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001969 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001970
Christopher Faulet67957bd2017-09-27 11:00:59 +02001971 if (res->nb_queries == resolvers->nb_nameservers)
1972 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001973
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001974 len = dns_build_query(res->query_id, res->query_type,
1975 resolvers->accepted_payload_size,
1976 res->hostname_dn, res->hostname_dn_len,
1977 trash.area, trash.size);
1978 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001979 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001980
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001981 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01001982 if (ret != len) {
1983 if (ret == -1 && errno == EAGAIN) {
1984 /* retry once the socket is ready */
1985 fd_cant_send(fd);
1986 continue;
1987 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001988 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01001989 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001990
Christopher Faulet67957bd2017-09-27 11:00:59 +02001991 ns->counters.sent++;
1992 res->nb_queries++;
1993 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001994
Christopher Faulet67957bd2017-09-27 11:00:59 +02001995 snd_error:
1996 ns->counters.snd_error++;
1997 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001998 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001999 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002000}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002001
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002/* Processes DNS resolution. First, it checks the active list to detect expired
2003 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2004 * checks the wait list to trigger new resolutions.
2005 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002006static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002007{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002008 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002009 struct dns_resolution *res, *resback;
2010 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002011
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002012 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002013
Christopher Faulet67957bd2017-09-27 11:00:59 +02002014 /* Handle all expired resolutions from the active list */
2015 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2016 /* When we find the first resolution in the future, then we can
2017 * stop here */
2018 exp = tick_add(res->last_query, resolvers->timeout.retry);
2019 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002020 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002021
Christopher Faulet67957bd2017-09-27 11:00:59 +02002022 /* If current resolution has been tried too many times and
2023 * finishes in timeout we update its status and remove it from
2024 * the list */
2025 if (!res->try) {
2026 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002027
Christopher Faulet67957bd2017-09-27 11:00:59 +02002028 /* Notify the result to the requesters */
2029 if (!res->nb_responses)
2030 res->status = RSLV_STATUS_TIMEOUT;
2031 list_for_each_entry(req, &res->requesters, list)
2032 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002033
Christopher Faulet67957bd2017-09-27 11:00:59 +02002034 /* Clean up resolution info and remove it from the
2035 * current list */
2036 dns_reset_resolution(res);
2037 LIST_DEL(&res->list);
2038 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002039 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002040 else {
2041 /* Otherwise resend the DNS query and requeue the resolution */
2042 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2043 /* No response received (a real timeout) or fallback already done */
2044 res->query_type = res->prefered_query_type;
2045 res->try--;
2046 }
2047 else {
2048 /* Fallback from A to AAAA or the opposite and re-send
2049 * the resolution immediately. try counter is not
2050 * decremented. */
2051 if (res->prefered_query_type == DNS_RTYPE_A)
2052 res->query_type = DNS_RTYPE_AAAA;
2053 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2054 res->query_type = DNS_RTYPE_A;
2055 else
2056 res->try--;
2057 }
2058 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002059 }
2060 }
William Lallemand69e96442016-11-19 00:58:54 +01002061
Christopher Faulet67957bd2017-09-27 11:00:59 +02002062 /* Handle all resolutions in the wait list */
2063 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2064 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2065 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2066 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002067
Christopher Faulet67957bd2017-09-27 11:00:59 +02002068 if (dns_run_resolution(res) != 1) {
2069 res->last_resolution = now_ms;
2070 LIST_DEL(&res->list);
2071 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002072 }
2073 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002074
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002076 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002077 return t;
2078}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002079
Christopher Faulet67957bd2017-09-27 11:00:59 +02002080/* proto_udp callback functions for a DNS resolution */
2081struct dgram_data_cb resolve_dgram_cb = {
2082 .recv = dns_resolve_recv,
2083 .send = dns_resolve_send,
2084};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002085
Christopher Faulet67957bd2017-09-27 11:00:59 +02002086/* Release memory allocated by DNS */
2087static void dns_deinit(void)
2088{
2089 struct dns_resolvers *resolvers, *resolversback;
2090 struct dns_nameserver *ns, *nsback;
2091 struct dns_resolution *res, *resback;
2092 struct dns_requester *req, *reqback;
2093 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002094
Christopher Faulet67957bd2017-09-27 11:00:59 +02002095 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2096 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2097 free(ns->id);
2098 free((char *)ns->conf.file);
2099 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2100 fd_delete(ns->dgram->t.sock.fd);
2101 free(ns->dgram);
2102 LIST_DEL(&ns->list);
2103 free(ns);
2104 }
2105
2106 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2107 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2108 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002109 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002110 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002111 dns_free_resolution(res);
2112 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002113
Christopher Faulet67957bd2017-09-27 11:00:59 +02002114 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, 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 free(resolvers->id);
2123 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002124 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002125 LIST_DEL(&resolvers->list);
2126 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002127 }
2128
Christopher Faulet67957bd2017-09-27 11:00:59 +02002129 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2130 free(srvrq->name);
2131 free(srvrq->hostname_dn);
2132 LIST_DEL(&srvrq->list);
2133 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002134 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002135}
2136
Christopher Faulet67957bd2017-09-27 11:00:59 +02002137/* Finalizes the DNS configuration by allocating required resources and checking
2138 * live parameters.
2139 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002140 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002141static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002142{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002143 struct dns_resolvers *resolvers;
2144 struct proxy *px;
2145 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002146
Christopher Faulet67957bd2017-09-27 11:00:59 +02002147 /* allocate pool of resolution per resolvers */
2148 list_for_each_entry(resolvers, &dns_resolvers, list) {
2149 struct dns_nameserver *ns;
2150 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002151
Christopher Faulet67957bd2017-09-27 11:00:59 +02002152 /* Check if we can create the socket with nameservers info */
2153 list_for_each_entry(ns, &resolvers->nameservers, list) {
2154 struct dgram_conn *dgram = NULL;
2155 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002156
Christopher Faulet67957bd2017-09-27 11:00:59 +02002157 /* Check nameserver info */
2158 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002159 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2160 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002161 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002162 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002163 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002164 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002165 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2166 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002167 close(fd);
2168 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002169 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002170 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002171 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002172
Christopher Faulet67957bd2017-09-27 11:00:59 +02002173 /* Create dgram structure that will hold the UPD socket
2174 * and attach it on the current nameserver */
2175 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002176 ha_alert("config: resolvers '%s' : out of memory.\n",
2177 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002178 err_code |= (ERR_ALERT|ERR_ABORT);
2179 goto err;
2180 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002181
Christopher Faulet67957bd2017-09-27 11:00:59 +02002182 /* Leave dgram partially initialized, no FD attached for
2183 * now. */
2184 dgram->owner = ns;
2185 dgram->data = &resolve_dgram_cb;
2186 dgram->t.sock.fd = -1;
2187 ns->dgram = dgram;
2188 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002189
Christopher Faulet67957bd2017-09-27 11:00:59 +02002190 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002191 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002192 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002193 err_code |= (ERR_ALERT|ERR_ABORT);
2194 goto err;
2195 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002196
Christopher Faulet67957bd2017-09-27 11:00:59 +02002197 /* Update task's parameters */
2198 t->process = dns_process_resolvers;
2199 t->context = resolvers;
2200 resolvers->t = t;
2201 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002202 }
2203
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002204 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002205 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002206
Christopher Faulet67957bd2017-09-27 11:00:59 +02002207 for (srv = px->srv; srv; srv = srv->next) {
2208 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002209
Christopher Faulet67957bd2017-09-27 11:00:59 +02002210 if (!srv->resolvers_id)
2211 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002212
Christopher Faulet67957bd2017-09-27 11:00:59 +02002213 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002214 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2215 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002216 err_code |= (ERR_ALERT|ERR_ABORT);
2217 continue;
2218 }
2219 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002220
Christopher Faulet67957bd2017-09-27 11:00:59 +02002221 if (srv->srvrq && !srv->srvrq->resolvers) {
2222 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002223 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002224 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2225 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002226 err_code |= (ERR_ALERT|ERR_ABORT);
2227 continue;
2228 }
2229 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002230 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002231 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2232 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002233 err_code |= (ERR_ALERT|ERR_ABORT);
2234 continue;
2235 }
2236 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002237 }
2238
Christopher Faulet67957bd2017-09-27 11:00:59 +02002239 if (err_code & (ERR_ALERT|ERR_ABORT))
2240 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002241
Christopher Faulet67957bd2017-09-27 11:00:59 +02002242 return err_code;
2243 err:
2244 dns_deinit();
2245 return err_code;
2246
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002247}
2248
Christopher Faulet67957bd2017-09-27 11:00:59 +02002249/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002250static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002251{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002252 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002253
Christopher Faulet67957bd2017-09-27 11:00:59 +02002254 if (*args[2]) {
2255 list_for_each_entry(presolvers, &dns_resolvers, list) {
2256 if (strcmp(presolvers->id, args[2]) == 0) {
2257 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002258 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002259 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002260 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002261 if (appctx->ctx.cli.p0 == NULL)
2262 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002263 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002264 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002265}
2266
Christopher Faulet67957bd2017-09-27 11:00:59 +02002267/* Dumps counters from all resolvers section and associated name servers. It
2268 * returns 0 if the output buffer is full and it needs to be called again,
2269 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002270 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002271 */
2272static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2273{
2274 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002275 struct dns_resolvers *resolvers;
2276 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002277
2278 chunk_reset(&trash);
2279
2280 switch (appctx->st2) {
2281 case STAT_ST_INIT:
2282 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2283 /* fall through */
2284
2285 case STAT_ST_LIST:
2286 if (LIST_ISEMPTY(&dns_resolvers)) {
2287 chunk_appendf(&trash, "No resolvers found\n");
2288 }
2289 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002290 list_for_each_entry(resolvers, &dns_resolvers, list) {
2291 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002292 continue;
2293
Christopher Faulet67957bd2017-09-27 11:00:59 +02002294 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2295 list_for_each_entry(ns, &resolvers->nameservers, list) {
2296 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2297 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2298 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2299 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2300 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2301 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2302 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2303 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2304 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2305 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2306 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2307 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2308 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2309 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2310 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2311 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002312 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002313 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002314 }
2315 }
2316
2317 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002318 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002319 /* let's try again later from this session. We add ourselves into
2320 * this session's users so that it can remove us upon termination.
2321 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002322 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002323 return 0;
2324 }
William Lallemand69e96442016-11-19 00:58:54 +01002325 /* fall through */
2326
2327 default:
2328 appctx->st2 = STAT_ST_FIN;
2329 return 1;
2330 }
2331}
2332
2333/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002334static struct cli_kw_list cli_kws = {{ }, {
2335 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2336 " associated name servers",
2337 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2338 {{},}
2339 }
2340};
William Lallemand69e96442016-11-19 00:58:54 +01002341
Willy Tarreau0108d902018-11-25 19:14:37 +01002342INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002343
Baptiste Assmann333939c2019-01-21 08:34:50 +01002344/*
2345 * Prepare <rule> for hostname resolution.
2346 * Returns -1 in case of any allocation failure, 0 if not.
2347 * On error, a global failure counter is also incremented.
2348 */
2349static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2350{
2351 char *hostname_dn;
2352 int hostname_len, hostname_dn_len;
2353 struct buffer *tmp = get_trash_chunk();
2354
2355 if (!hostname)
2356 return 0;
2357
2358 hostname_len = strlen(hostname);
2359 hostname_dn = tmp->area;
2360 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2361 hostname_dn, tmp->size);
2362 if (hostname_dn_len == -1)
2363 goto err;
2364
2365
2366 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2367 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2368 if (!stream->dns_ctx.hostname_dn)
2369 goto err;
2370
2371 return 0;
2372
2373 err:
2374 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2375 dns_failed_resolutions += 1;
2376 return -1;
2377}
2378
2379
2380/*
2381 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2382 */
2383enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2384 struct session *sess, struct stream *s, int flags)
2385{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002386 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002387 struct sample *smp;
2388 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002389 struct dns_requester *req;
2390 struct dns_resolvers *resolvers;
2391 struct dns_resolution *res;
2392 int exp;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002393
2394 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002395 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002396 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2397 resolution = s->dns_ctx.dns_requester->resolution;
Baptiste Assmann4c52e4b2019-10-01 15:32:40 +02002398 if (resolution->step == RSLV_STEP_RUNNING) {
2399 return ACT_RET_YIELD;
2400 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01002401 if (resolution->step == RSLV_STEP_NONE) {
2402 /* We update the variable only if we have a valid response. */
2403 if (resolution->status == RSLV_STATUS_VALID) {
2404 struct sample smp;
2405 short ip_sin_family = 0;
2406 void *ip = NULL;
2407
Christopher Fauleta4168432020-01-24 18:08:42 +01002408 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002409 0, &ip, &ip_sin_family, NULL);
2410
2411 switch (ip_sin_family) {
2412 case AF_INET:
2413 smp.data.type = SMP_T_IPV4;
2414 memcpy(&smp.data.u.ipv4, ip, 4);
2415 break;
2416 case AF_INET6:
2417 smp.data.type = SMP_T_IPV6;
2418 memcpy(&smp.data.u.ipv6, ip, 16);
2419 break;
2420 default:
2421 ip = NULL;
2422 }
2423
2424 if (ip) {
2425 smp.px = px;
2426 smp.sess = sess;
2427 smp.strm = s;
2428
2429 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2430 }
2431 }
2432 }
2433
2434 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
2435 s->dns_ctx.hostname_dn_len = 0;
2436 dns_unlink_resolution(s->dns_ctx.dns_requester);
2437
2438 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2439 s->dns_ctx.dns_requester = NULL;
2440
2441 return ACT_RET_CONT;
2442 }
2443
2444 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002445 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2446 if (smp == NULL)
2447 return ACT_RET_CONT;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002448
Willy Tarreau45726fd2019-07-17 10:38:45 +02002449 fqdn = smp->data.u.str.area;
2450 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet13403762019-12-13 09:01:57 +01002451 return ACT_RET_CONT; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002452
Willy Tarreau45726fd2019-07-17 10:38:45 +02002453 s->dns_ctx.parent = rule;
2454 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002455
2456 /* Check if there is a fresh enough response in the cache of our associated resolution */
2457 req = s->dns_ctx.dns_requester;
2458 if (!req || !req->resolution) {
2459 dns_trigger_resolution(s->dns_ctx.dns_requester);
2460 return ACT_RET_YIELD;
2461 }
2462 res = req->resolution;
2463 resolvers = res->resolvers;
2464
2465 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2466 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2467 && !tick_is_expired(exp, now_ms)) {
2468 goto use_cache;
2469 }
2470
Willy Tarreau45726fd2019-07-17 10:38:45 +02002471 dns_trigger_resolution(s->dns_ctx.dns_requester);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002472 return ACT_RET_YIELD;
2473}
2474
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002475static void release_dns_action(struct act_rule *rule)
2476{
2477 release_sample_expr(rule->arg.dns.expr);
2478 free(rule->arg.dns.varname);
2479 free(rule->arg.dns.resolvers_id);
2480 free(rule->arg.dns.dns_opts);
2481}
2482
Baptiste Assmann333939c2019-01-21 08:34:50 +01002483
2484/* parse "do-resolve" action
2485 * This action takes the following arguments:
2486 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2487 *
2488 * - <varName> is the variable name where the result of the DNS resolution will be stored
2489 * (mandatory)
2490 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2491 * (mandatory)
2492 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2493 * (optional), defaults to ipv6
2494 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2495 */
2496enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2497{
2498 int cur_arg;
2499 struct sample_expr *expr;
2500 unsigned int where;
2501 const char *beg, *end;
2502
2503 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2504 cur_arg = *orig_arg - 1;
2505
2506 /* locate varName, which is mandatory */
2507 beg = strchr(args[cur_arg], '(');
2508 if (beg == NULL)
2509 goto do_resolve_parse_error;
2510 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2511 end = strchr(beg, ',');
2512 if (end == NULL)
2513 goto do_resolve_parse_error;
2514 rule->arg.dns.varname = my_strndup(beg, end - beg);
2515 if (rule->arg.dns.varname == NULL)
2516 goto do_resolve_parse_error;
2517
2518
2519 /* locate resolversSectionName, which is mandatory.
2520 * Since next parameters are optional, the delimiter may be comma ','
2521 * or closing parenthesis ')'
2522 */
2523 beg = end + 1;
2524 end = strchr(beg, ',');
2525 if (end == NULL)
2526 end = strchr(beg, ')');
2527 if (end == NULL)
2528 goto do_resolve_parse_error;
2529 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2530 if (rule->arg.dns.resolvers_id == NULL)
2531 goto do_resolve_parse_error;
2532
2533
Christopher Fauleta4168432020-01-24 18:08:42 +01002534 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2535 if (rule->arg.dns.dns_opts == NULL)
2536 goto do_resolve_parse_error;
2537
Baptiste Assmann333939c2019-01-21 08:34:50 +01002538 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002539 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002540
2541 /* optional arguments accepted for now:
2542 * ipv4 or ipv6
2543 */
2544 while (*end != ')') {
2545 beg = end + 1;
2546 end = strchr(beg, ',');
2547 if (end == NULL)
2548 end = strchr(beg, ')');
2549 if (end == NULL)
2550 goto do_resolve_parse_error;
2551
2552 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002553 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002554 }
2555 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002556 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002557 }
2558 else {
2559 goto do_resolve_parse_error;
2560 }
2561 }
2562
2563 cur_arg = cur_arg + 1;
2564
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002565 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 +01002566 if (!expr)
2567 goto do_resolve_parse_error;
2568
2569
2570 where = 0;
2571 if (px->cap & PR_CAP_FE)
2572 where |= SMP_VAL_FE_HRQ_HDR;
2573 if (px->cap & PR_CAP_BE)
2574 where |= SMP_VAL_BE_HRQ_HDR;
2575
2576 if (!(expr->fetch->val & where)) {
2577 memprintf(err,
2578 "fetch method '%s' extracts information from '%s', none of which is available here",
2579 args[cur_arg-1], sample_src_names(expr->fetch->use));
2580 free(expr);
2581 return ACT_RET_PRS_ERR;
2582 }
2583 rule->arg.dns.expr = expr;
2584 rule->action = ACT_CUSTOM;
2585 rule->action_ptr = dns_action_do_resolve;
2586 *orig_arg = cur_arg;
2587
2588 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002589 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002590
2591 return ACT_RET_PRS_OK;
2592
2593 do_resolve_parse_error:
2594 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2595 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2596 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2597 args[cur_arg]);
2598 return ACT_RET_PRS_ERR;
2599}
2600
2601static struct action_kw_list http_req_kws = { { }, {
2602 { "do-resolve", dns_parse_do_resolve, 1 },
2603 { /* END */ }
2604}};
2605
2606INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2607
2608static struct action_kw_list tcp_req_cont_actions = {ILH, {
2609 { "do-resolve", dns_parse_do_resolve, 1 },
2610 { /* END */ }
2611}};
2612
2613INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2614
2615/* Check an "http-request do-resolve" action.
2616 *
2617 * The function returns 1 in success case, otherwise, it returns 0 and err is
2618 * filled.
2619 */
2620int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2621{
2622 struct dns_resolvers *resolvers = NULL;
2623
2624 if (rule->arg.dns.resolvers_id == NULL) {
2625 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2626 return 0;
2627 }
2628
2629 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2630 if (resolvers == NULL) {
2631 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2632 return 0;
2633 }
2634 rule->arg.dns.resolvers = resolvers;
2635
2636 return 1;
2637}
2638
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002639REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002640REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);