blob: 036d4af3dc34c7fe0fb13ccf86369cb992a9cfe2 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Willy Tarreau122eba92020-06-04 10:15:32 +020022#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Baptiste Assmann044fd5b2018-08-10 10:56:38 +020024#include <common/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020025#include <haproxy/check.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020026#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020027#include <haproxy/errors.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020028#include <haproxy/http_rules.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020029#include <haproxy/sample.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020030#include <haproxy/stats-t.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020031#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020032#include <haproxy/tcp_rules.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020033#include <haproxy/time.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020034#include <haproxy/ticks.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020035#include <haproxy/net_helper.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020036#include <haproxy/vars.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020037
William Lallemand69e96442016-11-19 00:58:54 +010038#include <types/cli.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020039#include <haproxy/global.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020040
William Lallemand69e96442016-11-19 00:58:54 +010041#include <proto/channel.h>
42#include <proto/cli.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020043#include <haproxy/fd.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020044#include <proto/http_ana.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020045#include <proto/log.h>
46#include <proto/server.h>
Willy Tarreau832ce652020-06-04 08:36:05 +020047#include <haproxy/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020048#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010049#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020050
Christopher Faulet67957bd2017-09-27 11:00:59 +020051struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
52struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020053
Tim Duesterhusfcac33d2020-01-18 02:04:12 +010054static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010055
56DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
57DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010058DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010059
Christopher Faulet67957bd2017-09-27 11:00:59 +020060static unsigned int resolution_uuid = 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +010061unsigned int dns_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020062
Christopher Faulet67957bd2017-09-27 11:00:59 +020063/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
64 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020065 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020066struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020067{
Christopher Faulet67957bd2017-09-27 11:00:59 +020068 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069
Christopher Faulet67957bd2017-09-27 11:00:59 +020070 list_for_each_entry(res, &dns_resolvers, list) {
71 if (!strcmp(res->id, id))
72 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020074 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020075}
76
Olivier Houchardb17b8842020-04-01 18:30:27 +020077/* Compare hostnames in a case-insensitive way .
78 * Returns 0 if they are the same, non-zero otherwise
79 */
80static __inline int dns_hostname_cmp(const char *name1, const char *name2, int len)
81{
82 int i;
83
84 for (i = 0; i < len; i++)
85 if (tolower(name1[i]) != tolower(name2[i]))
86 return -1;
87 return 0;
88}
89
Christopher Faulet67957bd2017-09-27 11:00:59 +020090/* Returns a pointer on the SRV request matching the name <name> for the proxy
91 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020092 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020093struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020094{
Christopher Faulet67957bd2017-09-27 11:00:59 +020095 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020096
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
98 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
99 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200100 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200101 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200102}
103
Christopher Faulet67957bd2017-09-27 11:00:59 +0200104/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
105 * NULL if an error occurred. */
106struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200107{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200108 struct proxy *px = srv->proxy;
109 struct dns_srvrq *srvrq = NULL;
110 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200111
Christopher Faulet67957bd2017-09-27 11:00:59 +0200112 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200113 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
114 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100116 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
117 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200118 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200119 }
120
Christopher Faulet67957bd2017-09-27 11:00:59 +0200121 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
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 srvrq->obj_type = OBJ_TYPE_SRVRQ;
127 srvrq->proxy = px;
128 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200129 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130 srvrq->hostname_dn_len = hostname_dn_len;
131 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100132 ha_alert("config : %s '%s', server '%s': out of memory\n",
133 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200134 goto err;
135 }
136 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
137 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200138
Christopher Faulet67957bd2017-09-27 11:00:59 +0200139 err:
140 if (srvrq) {
141 free(srvrq->name);
142 free(srvrq->hostname_dn);
143 free(srvrq);
144 }
145 return NULL;
146}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200147
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200148
Christopher Faulet67957bd2017-09-27 11:00:59 +0200149/* 2 bytes random generator to generate DNS query ID */
150static inline uint16_t dns_rnd16(void)
151{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200152 if (!dns_query_id_seed)
153 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200154 dns_query_id_seed ^= dns_query_id_seed << 13;
155 dns_query_id_seed ^= dns_query_id_seed >> 7;
156 dns_query_id_seed ^= dns_query_id_seed << 17;
157 return dns_query_id_seed;
158}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200159
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200160
Christopher Faulet67957bd2017-09-27 11:00:59 +0200161static inline int dns_resolution_timeout(struct dns_resolution *res)
162{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100163 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200164}
165
Christopher Faulet67957bd2017-09-27 11:00:59 +0200166/* Updates a resolvers' task timeout for next wake up and queue it */
167static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 struct dns_resolution *res;
170 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172 next = tick_add(now_ms, resolvers->timeout.resolve);
173 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
174 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
175 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
176 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200177
Christopher Faulet67957bd2017-09-27 11:00:59 +0200178 list_for_each_entry(res, &resolvers->resolutions.wait, list)
179 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200180
Christopher Faulet67957bd2017-09-27 11:00:59 +0200181 resolvers->t->expire = next;
182 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200183}
184
Christopher Faulet67957bd2017-09-27 11:00:59 +0200185/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
186 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200187 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200188static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200189{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190 struct dgram_conn *dgram = ns->dgram;
191 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200192
Christopher Faulet67957bd2017-09-27 11:00:59 +0200193 /* Already connected */
194 if (dgram->t.sock.fd != -1)
195 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200196
Christopher Faulet67957bd2017-09-27 11:00:59 +0200197 /* Create an UDP socket and connect it on the nameserver's IP/Port */
198 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
199 send_log(NULL, LOG_WARNING,
200 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
201 ns->resolvers->id, ns->id);
202 return -1;
203 }
204 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
205 send_log(NULL, LOG_WARNING,
206 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
207 ns->resolvers->id, ns->id);
208 close(fd);
209 return -1;
210 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200211
Christopher Faulet67957bd2017-09-27 11:00:59 +0200212 /* Make the socket non blocking */
213 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200214
Christopher Faulet67957bd2017-09-27 11:00:59 +0200215 /* Add the fd in the fd list and update its parameters */
216 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100217 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200218 fd_want_recv(fd);
219 return 0;
220}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200221
Christopher Faulet67957bd2017-09-27 11:00:59 +0200222/* Forges a DNS query. It needs the following information from the caller:
223 * - <query_id> : the DNS query id corresponding to this query
224 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
225 * - <hostname_dn> : hostname in domain name format
226 * - <hostname_dn_len> : length of <hostname_dn>
227 *
228 * To store the query, the caller must pass a buffer <buf> and its size
229 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
230 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200231 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200232static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
233 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200234{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200235 struct dns_header dns_hdr;
236 struct dns_question qinfo;
237 struct dns_additional_record edns;
238 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200239
Christopher Faulet67957bd2017-09-27 11:00:59 +0200240 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
241 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200242
Christopher Faulet67957bd2017-09-27 11:00:59 +0200243 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200244
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 /* Set dns query headers */
246 dns_hdr.id = (unsigned short) htons(query_id);
247 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
248 dns_hdr.qdcount = htons(1); /* 1 question */
249 dns_hdr.ancount = 0;
250 dns_hdr.nscount = 0;
251 dns_hdr.arcount = htons(1);
252 memcpy(p, &dns_hdr, sizeof(dns_hdr));
253 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200254
Christopher Faulet67957bd2017-09-27 11:00:59 +0200255 /* Set up query hostname */
256 memcpy(p, hostname_dn, hostname_dn_len);
257 p += hostname_dn_len;
258 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200259
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260 /* Set up query info (type and class) */
261 qinfo.qtype = htons(query_type);
262 qinfo.qclass = htons(DNS_RCLASS_IN);
263 memcpy(p, &qinfo, sizeof(qinfo));
264 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200265
Christopher Faulet67957bd2017-09-27 11:00:59 +0200266 /* Set the DNS extension */
267 edns.name = 0;
268 edns.type = htons(DNS_RTYPE_OPT);
269 edns.udp_payload_size = htons(accepted_payload_size);
270 edns.extension = 0;
271 edns.data_length = 0;
272 memcpy(p, &edns, sizeof(edns));
273 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200274
Christopher Faulet67957bd2017-09-27 11:00:59 +0200275 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200276}
277
Christopher Faulet67957bd2017-09-27 11:00:59 +0200278/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
279 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200282{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200283 struct dns_resolvers *resolvers = resolution->resolvers;
284 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100285 int len;
286
287 /* Update resolution */
288 resolution->nb_queries = 0;
289 resolution->nb_responses = 0;
290 resolution->last_query = now_ms;
291
292 len = dns_build_query(resolution->query_id, resolution->query_type,
293 resolvers->accepted_payload_size,
294 resolution->hostname_dn, resolution->hostname_dn_len,
295 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200296
Christopher Faulet67957bd2017-09-27 11:00:59 +0200297 list_for_each_entry(ns, &resolvers->nameservers, list) {
298 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100299 int ret;
300
Christopher Faulet67957bd2017-09-27 11:00:59 +0200301 if (fd == -1) {
302 if (dns_connect_namesaver(ns) == -1)
303 continue;
304 fd = ns->dgram->t.sock.fd;
305 resolvers->nb_nameservers++;
306 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200307
Willy Tarreau0eae6322019-12-20 11:18:54 +0100308 if (len < 0)
309 goto snd_error;
310
311 ret = send(fd, trash.area, len, 0);
312 if (ret == len) {
313 ns->counters.sent++;
314 resolution->nb_queries++;
315 continue;
316 }
317
318 if (ret == -1 && errno == EAGAIN) {
319 /* retry once the socket is ready */
320 fd_cant_send(fd);
321 continue;
322 }
323
324 snd_error:
325 ns->counters.snd_error++;
326 resolution->nb_queries++;
327 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200328
Christopher Faulet67957bd2017-09-27 11:00:59 +0200329 /* Push the resolution at the end of the active list */
330 LIST_DEL(&resolution->list);
331 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
332 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333}
334
Christopher Faulet67957bd2017-09-27 11:00:59 +0200335/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
336 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200337 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200338static int
339dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200340{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 struct dns_resolvers *resolvers = resolution->resolvers;
342 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200343
Christopher Faulet67957bd2017-09-27 11:00:59 +0200344 /* Avoid sending requests for resolutions that don't yet have an
345 * hostname, ie resolutions linked to servers that do not yet have an
346 * fqdn */
347 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200348 return 0;
349
Christopher Faulet67957bd2017-09-27 11:00:59 +0200350 /* Check if a resolution has already been started for this server return
351 * directly to avoid resolution pill up. */
352 if (resolution->step != RSLV_STEP_NONE)
353 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200354
Christopher Faulet67957bd2017-09-27 11:00:59 +0200355 /* Generates a new query id. We try at most 100 times to find a free
356 * query id */
357 for (i = 0; i < 100; ++i) {
358 query_id = dns_rnd16();
359 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200360 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200361 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200362 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200363 if (query_id == -1) {
364 send_log(NULL, LOG_NOTICE,
365 "could not generate a query id for %s, in resolvers %s.\n",
366 resolution->hostname_dn, resolvers->id);
367 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200368 }
369
Christopher Faulet67957bd2017-09-27 11:00:59 +0200370 /* Update resolution parameters */
371 resolution->query_id = query_id;
372 resolution->qid.key = query_id;
373 resolution->step = RSLV_STEP_RUNNING;
374 resolution->query_type = resolution->prefered_query_type;
375 resolution->try = resolvers->resolve_retries;
376 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200377
Christopher Faulet67957bd2017-09-27 11:00:59 +0200378 /* Send the DNS query */
379 resolution->try -= 1;
380 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200381 return 1;
382}
383
Christopher Faulet67957bd2017-09-27 11:00:59 +0200384/* Performs a name resolution for the requester <req> */
385void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200386{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200387 struct dns_resolvers *resolvers;
388 struct dns_resolution *res;
389 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200390
Christopher Faulet67957bd2017-09-27 11:00:59 +0200391 if (!req || !req->resolution)
392 return;
393 res = req->resolution;
394 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200395
Christopher Faulet67957bd2017-09-27 11:00:59 +0200396 /* The resolution must not be triggered yet. Use the cached response, if
397 * valid */
398 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200399 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
400 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
401 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200402}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200403
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200404
Christopher Faulet67957bd2017-09-27 11:00:59 +0200405/* Resets some resolution parameters to initial values and also delete the query
406 * ID from the resolver's tree.
407 */
408static void dns_reset_resolution(struct dns_resolution *resolution)
409{
410 /* update resolution status */
411 resolution->step = RSLV_STEP_NONE;
412 resolution->try = 0;
413 resolution->last_resolution = now_ms;
414 resolution->nb_queries = 0;
415 resolution->nb_responses = 0;
416 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200417
Christopher Faulet67957bd2017-09-27 11:00:59 +0200418 /* clean up query id */
419 eb32_delete(&resolution->qid);
420 resolution->query_id = 0;
421 resolution->qid.key = 0;
422}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200423
Christopher Faulet67957bd2017-09-27 11:00:59 +0200424/* Returns the query id contained in a DNS response */
425static inline unsigned short dns_response_get_query_id(unsigned char *resp)
426{
427 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200428}
429
Christopher Faulet67957bd2017-09-27 11:00:59 +0200430
431/* Analyses, re-builds and copies the name <name> from the DNS response packet
432 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
433 * for compressed data. The result is copied into <dest>, ensuring we don't
434 * overflow using <dest_len> Returns the number of bytes the caller can move
435 * forward. If 0 it means an error occurred while parsing the name. <offset> is
436 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200437 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200438int dns_read_name(unsigned char *buffer, unsigned char *bufend,
439 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100440 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200441{
442 int nb_bytes = 0, n = 0;
443 int label_len;
444 unsigned char *reader = name;
445 char *dest = destination;
446
447 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100448 if (reader >= bufend)
449 goto err;
450
Christopher Faulet67957bd2017-09-27 11:00:59 +0200451 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200452 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100453 if (reader + 1 >= bufend)
454 goto err;
455
Christopher Faulet67957bd2017-09-27 11:00:59 +0200456 /* Must point BEFORE current position */
457 if ((buffer + reader[1]) > reader)
458 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200459
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100460 if (depth++ > 100)
461 goto err;
462
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530463 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100464 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200465 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200466 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200467
Christopher Faulet67957bd2017-09-27 11:00:59 +0200468 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200469 nb_bytes += n;
470 goto out;
471 }
472
473 label_len = *reader;
474 if (label_len == 0)
475 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200476
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200477 /* Check if:
478 * - we won't read outside the buffer
479 * - there is enough place in the destination
480 */
481 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200482 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200483
484 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200485 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200486
487 memcpy(dest, reader, label_len);
488
Christopher Faulet67957bd2017-09-27 11:00:59 +0200489 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200490 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200491 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200492 }
493
Christopher Faulet67957bd2017-09-27 11:00:59 +0200494 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200495 /* offset computation:
496 * parse from <name> until finding either NULL or a pointer "c0xx"
497 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200498 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200499 *offset = 0;
500 while (reader < bufend) {
501 if ((reader[0] & 0xc0) == 0xc0) {
502 *offset += 2;
503 break;
504 }
505 else if (*reader == 0) {
506 *offset += 1;
507 break;
508 }
509 *offset += 1;
510 ++reader;
511 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200512 return nb_bytes;
513
Christopher Faulet67957bd2017-09-27 11:00:59 +0200514 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200515 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200516}
517
Christopher Faulet67957bd2017-09-27 11:00:59 +0200518/* Checks for any obsolete record, also identify any SRV request, and try to
519 * find a corresponding server.
520*/
521static void dns_check_dns_response(struct dns_resolution *res)
522{
523 struct dns_resolvers *resolvers = res->resolvers;
524 struct dns_requester *req, *reqback;
525 struct dns_answer_item *item, *itemback;
526 struct server *srv;
527 struct dns_srvrq *srvrq;
528
Baptiste Assmann13a92322019-06-07 09:40:55 +0200529 /* clean up obsolete Additional records */
530 list_for_each_entry_safe(item, itemback, &res->response.ar_list, list) {
531 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
532 LIST_DEL(&item->list);
533 pool_free(dns_answer_item_pool, item);
534 }
535 }
536
Christopher Faulet67957bd2017-09-27 11:00:59 +0200537 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
538
539 /* Remove obsolete items */
540 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
541 if (item->type != DNS_RTYPE_SRV)
542 goto rm_obselete_item;
543
544 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
545 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
546 continue;
547
548 /* Remove any associated server */
549 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100550 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
552 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200553 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200554 snr_update_srv_status(srv, 1);
555 free(srv->hostname);
556 free(srv->hostname_dn);
557 srv->hostname = NULL;
558 srv->hostname_dn = NULL;
559 srv->hostname_dn_len = 0;
560 dns_unlink_resolution(srv->dns_requester);
561 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100562 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200563 }
564 }
565
566 rm_obselete_item:
567 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100568 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200569 continue;
570 }
571
572 if (item->type != DNS_RTYPE_SRV)
573 continue;
574
575 /* Now process SRV records */
576 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
577 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
578 continue;
579
580 /* Check if a server already uses that hostname */
581 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100582 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200583 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
584 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200585 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len) &&
Daniel Corbettf8716912019-11-17 09:48:56 -0500586 !srv->dns_opts.ignore_weight) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100587 int ha_weight;
588
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200589 /* DNS weight range if from 0 to 65535
590 * HAProxy weight is from 0 to 256
591 * The rule below ensures that weight 0 is well respected
592 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100593 */
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200594 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100595 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200596 char weight[9];
597
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100598 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200599 server_parse_weight_change_request(srv, weight);
600 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100601 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200602 break;
603 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100604 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200605 }
606 if (srv)
607 continue;
608
609 /* If not, try to find a server with undefined hostname */
610 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100611 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200612 if (srv->srvrq == srvrq && !srv->hostname_dn)
613 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100614 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200615 }
616 /* And update this server, if found */
617 if (srv) {
618 const char *msg = NULL;
619 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100620 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200621 char hostname[DNS_MAX_NAME_SIZE];
622
623 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100624 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100625 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200626 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100627 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200628
629 /* Check if an Additional Record is associated to this SRV record.
630 * Perform some sanity checks too to ensure the record can be used.
631 * If all fine, we simply pick up the IP address found and associate
632 * it to the server.
633 */
634 if ((item->ar_item != NULL) &&
635 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
636 {
637
638 switch (item->ar_item->type) {
639 case DNS_RTYPE_A:
640 update_server_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional recrd");
641 break;
642 case DNS_RTYPE_AAAA:
643 update_server_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional recrd");
644 break;
645 }
646
647 srv->flags |= SRV_F_NO_RESOLUTION;
648 }
649
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100650 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200651 if (msg)
652 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
653
654 srv->svc_port = item->port;
655 srv->flags &= ~SRV_F_MAPPORTS;
656 if ((srv->check.state & CHK_ST_CONFIGURED) &&
657 !(srv->flags & SRV_F_CHECKPORT))
658 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100659
Daniel Corbettf8716912019-11-17 09:48:56 -0500660 if (!srv->dns_opts.ignore_weight) {
661 /* DNS weight range if from 0 to 65535
662 * HAProxy weight is from 0 to 256
663 * The rule below ensures that weight 0 is well respected
664 * while allowing a "mapping" from DNS weight into HAProxy's one.
665 */
666 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100667
Daniel Corbettf8716912019-11-17 09:48:56 -0500668 snprintf(weight, sizeof(weight), "%d", ha_weight);
669 server_parse_weight_change_request(srv, weight);
670 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100671 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200672 }
673 }
674 }
675}
676
677/* Validates that the buffer DNS response provided in <resp> and finishing
678 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200680 * The result is stored in <resolution>' response, buf_response,
681 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200682 *
683 * This function returns one of the DNS_RESP_* code to indicate the type of
684 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200685 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200686static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
687 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200688{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200689 unsigned char *reader;
690 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200691 int len, flags, offset;
692 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200693 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200694 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200695 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200696 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200697 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200698
Christopher Faulet67957bd2017-09-27 11:00:59 +0200699 reader = resp;
700 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200701 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200702 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200703
Christopher Faulet67957bd2017-09-27 11:00:59 +0200704 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200705 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200706
707 /* query id */
708 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200709 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200710 dns_p->header.id = reader[0] * 256 + reader[1];
711 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200712
Christopher Faulet67957bd2017-09-27 11:00:59 +0200713 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200714 * First byte contains:
715 * - response flag (1 bit)
716 * - opcode (4 bits)
717 * - authoritative (1 bit)
718 * - truncated (1 bit)
719 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200720 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200721 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200722 return DNS_RESP_INVALID;
723
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200724 flags = reader[0] * 256 + reader[1];
725
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200726 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
727 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200728 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200729 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200730 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200731 return DNS_RESP_ERROR;
732 }
733
Christopher Faulet67957bd2017-09-27 11:00:59 +0200734 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200735 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200736
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200737 /* 2 bytes for question count */
738 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200739 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200740 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200741 /* (for now) we send one query only, so we expect only one in the
742 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200743 if (dns_p->header.qdcount != 1)
744 return DNS_RESP_QUERY_COUNT_ERROR;
745 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200746 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200747 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200748
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200749 /* 2 bytes for answer count */
750 if (reader + 2 >= bufend)
751 return DNS_RESP_INVALID;
752 dns_p->header.ancount = reader[0] * 256 + reader[1];
753 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200754 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200755 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200756 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200757 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200759
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200760 /* 2 bytes authority count */
761 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200762 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200763 dns_p->header.nscount = reader[0] * 256 + reader[1];
764 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200765
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 /* 2 bytes additional count */
767 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200768 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200769 dns_p->header.arcount = reader[0] * 256 + reader[1];
770 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200771
Christopher Faulet67957bd2017-09-27 11:00:59 +0200772 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200773 LIST_INIT(&dns_p->query_list);
774 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 +0200775 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200776 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200777 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200778 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
779 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200780 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200781 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200782
Christopher Faulet67957bd2017-09-27 11:00:59 +0200783 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200784 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200785 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200786 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100787 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200788
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200789 if (len == 0)
790 return DNS_RESP_INVALID;
791
792 reader += offset;
793 previous_dname = dns_query->name;
794
795 /* move forward 2 bytes for question type */
796 if (reader + 2 >= bufend)
797 return DNS_RESP_INVALID;
798 dns_query->type = reader[0] * 256 + reader[1];
799 reader += 2;
800
801 /* move forward 2 bytes for question class */
802 if (reader + 2 >= bufend)
803 return DNS_RESP_INVALID;
804 dns_query->class = reader[0] * 256 + reader[1];
805 reader += 2;
806 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807
Baptiste Assmann251abb92017-08-11 09:58:27 +0200808 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200809 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200810 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
811 return DNS_RESP_TRUNCATED;
812
Baptiste Assmann325137d2015-04-13 23:40:55 +0200813 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200814 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200815 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200816 if (reader >= bufend)
817 return DNS_RESP_INVALID;
818
Willy Tarreaubafbe012017-11-24 17:34:44 +0100819 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200820 if (dns_answer_record == NULL)
821 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200822
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200823 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100824 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200825
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200826 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100827 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200828 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200829 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200830
Christopher Faulet67957bd2017-09-27 11:00:59 +0200831 /* Check if the current record dname is valid. previous_dname
832 * points either to queried dname or last CNAME target */
Olivier Houchardb17b8842020-04-01 18:30:27 +0200833 if (dns_query->type != DNS_RTYPE_SRV && dns_hostname_cmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100834 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200835 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200836 /* First record, means a mismatch issue between
837 * queried dname and dname found in the first
838 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200839 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200840 }
841 else {
842 /* If not the first record, this means we have a
843 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200844 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200845 }
846
Baptiste Assmann325137d2015-04-13 23:40:55 +0200847 }
848
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200849 memcpy(dns_answer_record->name, tmpname, len);
850 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200851
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200852 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200853 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100854 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200855 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200856 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200857
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200858 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200859 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100860 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200861 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200862 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200863 dns_answer_record->type = reader[0] * 256 + reader[1];
864 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200865
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200866 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200867 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100868 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200869 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200870 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200871 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200872 reader += 2;
873
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200874 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200875 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100876 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200877 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200878 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200879 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
880 + reader[2] * 256 + reader[3];
881 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200882
Christopher Faulet67957bd2017-09-27 11:00:59 +0200883 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200884 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100885 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200886 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200887 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200888 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200889
Christopher Faulet67957bd2017-09-27 11:00:59 +0200890 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200891 reader += 2;
892
Jérôme Magnin8d4e7dc2018-12-20 16:47:31 +0100893 if (reader + dns_answer_record->data_len > bufend) {
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100894 pool_free(dns_answer_item_pool, dns_answer_record);
895 return DNS_RESP_INVALID;
896 }
897
Christopher Faulet67957bd2017-09-27 11:00:59 +0200898 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200899 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200900 case DNS_RTYPE_A:
901 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200902 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100903 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200904 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200905 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200906 dns_answer_record->address.sa_family = AF_INET;
907 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
908 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200909 break;
910
911 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200912 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200913 * no IP could be found and last record was a CNAME. Could be triggered
914 * by a wrong query type
915 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200916 * + 1 because dns_answer_record_id starts at 0
917 * while number of answers is an integer and
918 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200919 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200920 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100921 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200922 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200923 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200924
925 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100926 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200927 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100928 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200929 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200930 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200931
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200932 memcpy(dns_answer_record->target, tmpname, len);
933 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200934 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200935 break;
936
Olivier Houchard8da5f982017-08-04 18:35:36 +0200937
938 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200939 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200940 * - 2 bytes for the priority
941 * - 2 bytes for the weight
942 * - 2 bytes for the port
943 * - the target hostname
944 */
945 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100946 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200947 return DNS_RESP_INVALID;
948 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200949 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200950 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200951 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200952 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200953 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200954 reader += sizeof(uint16_t);
955 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100956 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200957 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100958 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200959 return DNS_RESP_INVALID;
960 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200961 dns_answer_record->data_len = len;
962 memcpy(dns_answer_record->target, tmpname, len);
963 dns_answer_record->target[len] = 0;
964 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200965
Baptiste Assmann325137d2015-04-13 23:40:55 +0200966 case DNS_RTYPE_AAAA:
967 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200968 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100969 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200970 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200971 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200972 dns_answer_record->address.sa_family = AF_INET6;
973 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
974 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200975 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200976
Baptiste Assmann325137d2015-04-13 23:40:55 +0200977 } /* switch (record type) */
978
Christopher Faulet67957bd2017-09-27 11:00:59 +0200979 /* Increment the counter for number of records saved into our
980 * local response */
981 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200982
Christopher Faulet67957bd2017-09-27 11:00:59 +0200983 /* Move forward dns_answer_record->data_len for analyzing next
984 * record in the response */
985 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
986 ? offset
987 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200988
989 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200990 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200991 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
992 if (tmp_record->type != dns_answer_record->type)
993 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200994
995 switch(tmp_record->type) {
996 case DNS_RTYPE_A:
997 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
998 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
999 sizeof(in_addr_t)))
1000 found = 1;
1001 break;
1002
1003 case DNS_RTYPE_AAAA:
1004 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1005 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1006 sizeof(struct in6_addr)))
1007 found = 1;
1008 break;
1009
Olivier Houchard8da5f982017-08-04 18:35:36 +02001010 case DNS_RTYPE_SRV:
1011 if (dns_answer_record->data_len == tmp_record->data_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001012 !dns_hostname_cmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001013 dns_answer_record->port == tmp_record->port) {
1014 tmp_record->weight = dns_answer_record->weight;
1015 found = 1;
1016 }
1017 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001018
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001019 default:
1020 break;
1021 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001022
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001023 if (found == 1)
1024 break;
1025 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001026
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001027 if (found == 1) {
1028 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001029 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001030 }
1031 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001032 dns_answer_record->last_seen = now.tv_sec;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001033 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001034 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1035 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001036 } /* for i 0 to ancount */
1037
Christopher Faulet67957bd2017-09-27 11:00:59 +02001038 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001039 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001040
Baptiste Assmann37950c82020-02-19 01:08:51 +01001041 /* now parsing additional records for SRV queries only */
1042 if (dns_query->type != DNS_RTYPE_SRV)
1043 goto skip_parsing_additional_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001044 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001045 for (i = 0; i < dns_p->header.arcount; i++) {
1046 if (reader >= bufend)
1047 return DNS_RESP_INVALID;
1048
1049 dns_answer_record = pool_alloc(dns_answer_item_pool);
1050 if (dns_answer_record == NULL)
1051 return (DNS_RESP_INVALID);
1052
1053 offset = 0;
1054 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1055
1056 if (len == 0) {
1057 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann37950c82020-02-19 01:08:51 +01001058 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001059 }
1060
1061 memcpy(dns_answer_record->name, tmpname, len);
1062 dns_answer_record->name[len] = 0;
1063
1064 reader += offset;
1065 if (reader >= bufend) {
1066 pool_free(dns_answer_item_pool, dns_answer_record);
1067 return DNS_RESP_INVALID;
1068 }
1069
1070 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1071 if (reader + 2 > bufend) {
1072 pool_free(dns_answer_item_pool, dns_answer_record);
1073 return DNS_RESP_INVALID;
1074 }
1075 dns_answer_record->type = reader[0] * 256 + reader[1];
1076 reader += 2;
1077
1078 /* 2 bytes for class (2) */
1079 if (reader + 2 > bufend) {
1080 pool_free(dns_answer_item_pool, dns_answer_record);
1081 return DNS_RESP_INVALID;
1082 }
1083 dns_answer_record->class = reader[0] * 256 + reader[1];
1084 reader += 2;
1085
1086 /* 4 bytes for ttl (4) */
1087 if (reader + 4 > bufend) {
1088 pool_free(dns_answer_item_pool, dns_answer_record);
1089 return DNS_RESP_INVALID;
1090 }
1091 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1092 + reader[2] * 256 + reader[3];
1093 reader += 4;
1094
1095 /* Now reading data len */
1096 if (reader + 2 > bufend) {
1097 pool_free(dns_answer_item_pool, dns_answer_record);
1098 return DNS_RESP_INVALID;
1099 }
1100 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1101
1102 /* Move forward 2 bytes for data len */
1103 reader += 2;
1104
1105 if (reader + dns_answer_record->data_len > bufend) {
1106 pool_free(dns_answer_item_pool, dns_answer_record);
1107 return DNS_RESP_INVALID;
1108 }
1109
1110 /* Analyzing record content */
1111 switch (dns_answer_record->type) {
1112 case DNS_RTYPE_A:
1113 /* ipv4 is stored on 4 bytes */
1114 if (dns_answer_record->data_len != 4) {
1115 pool_free(dns_answer_item_pool, dns_answer_record);
1116 return DNS_RESP_INVALID;
1117 }
1118 dns_answer_record->address.sa_family = AF_INET;
1119 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1120 reader, dns_answer_record->data_len);
1121 break;
1122
1123 case DNS_RTYPE_AAAA:
1124 /* ipv6 is stored on 16 bytes */
1125 if (dns_answer_record->data_len != 16) {
1126 pool_free(dns_answer_item_pool, dns_answer_record);
1127 return DNS_RESP_INVALID;
1128 }
1129 dns_answer_record->address.sa_family = AF_INET6;
1130 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1131 reader, dns_answer_record->data_len);
1132 break;
1133
1134 default:
1135 pool_free(dns_answer_item_pool, dns_answer_record);
1136 continue;
1137
1138 } /* switch (record type) */
1139
1140 /* Increment the counter for number of records saved into our
1141 * local response */
1142 nb_saved_records++;
1143
1144 /* Move forward dns_answer_record->data_len for analyzing next
1145 * record in the response */
1146 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1147 ? offset
1148 : dns_answer_record->data_len);
1149
1150 /* Lookup to see if we already had this entry */
1151 found = 0;
1152 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1153 if (tmp_record->type != dns_answer_record->type)
1154 continue;
1155
1156 switch(tmp_record->type) {
1157 case DNS_RTYPE_A:
1158 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1159 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1160 sizeof(in_addr_t)))
1161 found = 1;
1162 break;
1163
1164 case DNS_RTYPE_AAAA:
1165 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1166 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1167 sizeof(struct in6_addr)))
1168 found = 1;
1169 break;
1170
1171 default:
1172 break;
1173 }
1174
1175 if (found == 1)
1176 break;
1177 }
1178
1179 if (found == 1) {
1180 tmp_record->last_seen = now.tv_sec;
1181 pool_free(dns_answer_item_pool, dns_answer_record);
1182 }
1183 else {
1184 dns_answer_record->last_seen = now.tv_sec;
1185 dns_answer_record->ar_item = NULL;
1186
1187 // looking for the SRV record in the response list linked to this additional record
1188 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1189 if ( !(
1190 (tmp_record->type == DNS_RTYPE_SRV) &&
1191 (tmp_record->ar_item == NULL) &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001192 (dns_hostname_cmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len) == 0)
Baptiste Assmann13a92322019-06-07 09:40:55 +02001193 )
1194 )
1195 continue;
1196 tmp_record->ar_item = dns_answer_record;
1197 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001198
1199 LIST_ADDQ(&dns_p->ar_list, &dns_answer_record->list);
1200 }
1201 } /* for i 0 to arcount */
1202
Baptiste Assmann37950c82020-02-19 01:08:51 +01001203 skip_parsing_additional_records:
1204
Baptiste Assmann13a92322019-06-07 09:40:55 +02001205 /* Save the number of records we really own */
1206 dns_p->header.arcount = nb_saved_records;
1207
Christopher Faulet67957bd2017-09-27 11:00:59 +02001208 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001209 return DNS_RESP_VALID;
1210}
1211
Christopher Faulet67957bd2017-09-27 11:00:59 +02001212/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001213 * If existing IP not found, return the first IP matching family_priority,
1214 * otherwise, first ip found
1215 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001216 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001217 * For both cases above, dns_validate_dns_response is required
1218 * returns one of the DNS_UPD_* code
1219 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001220int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001221 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001222 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001223 void **newip, short *newip_sin_family,
1224 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001225{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001226 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001227 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001228 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001229 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001230 int currentip_sel;
1231 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001232 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001233 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001234
Christopher Faulet67957bd2017-09-27 11:00:59 +02001235 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001236 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001237 *newip = newip4 = newip6 = NULL;
1238 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001239 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001240 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001241
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001242 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001243 * Top priority is the preferred network ip version,
1244 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001245 * the last priority is the currently used IP,
1246 *
1247 * For these three priorities, a score is calculated. The
1248 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001249 * 8 - preferred ip version.
1250 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001251 * 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 +01001252 * 1 - current ip.
1253 * The result with the biggest score is returned.
1254 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001255
1256 list_for_each_entry(record, &dns_p->answer_list, list) {
1257 void *ip;
1258 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001259
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001260 if (record->type == DNS_RTYPE_A) {
1261 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1262 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001263 }
1264 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001265 ip_type = AF_INET6;
1266 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001267 }
1268 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001269 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001270 score = 0;
1271
Joseph Herlant42cf6392018-11-15 10:33:28 -08001272 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001273 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001274 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001275
Joseph Herlant42cf6392018-11-15 10:33:28 -08001276 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001277 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001278
1279 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001280 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001281 continue;
1282
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001283 if ((ip_type == AF_INET &&
1284 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001285 &dns_opts->pref_net[j].mask.in4,
1286 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001287 (ip_type == AF_INET6 &&
1288 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001289 &dns_opts->pref_net[j].mask.in6,
1290 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001291 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001292 break;
1293 }
1294 }
1295
Christopher Faulet67957bd2017-09-27 11:00:59 +02001296 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001297 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001298 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001299 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001300 if (!allowed_duplicated_ip) {
1301 continue;
1302 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001303 } else {
1304 score += 2;
1305 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001306
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001307 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001308 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001309 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001310 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001311 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001312 !memcmp(ip, currentip, 16)))) {
1313 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001314 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001315 }
1316 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001317 currentip_sel = 0;
1318
1319 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001320 * score. The maximum score is 15, if this value is reached, we
1321 * break the parsing. Implicitly, this score is reached the ip
1322 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001323 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001324 if (ip_type == AF_INET)
1325 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001326 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001327 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001328 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001329 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001330 return DNS_UPD_NO;
1331 max_score = score;
1332 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001333 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001334
Christopher Faulet67957bd2017-09-27 11:00:59 +02001335 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001336 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001337 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001338
Christopher Faulet67957bd2017-09-27 11:00:59 +02001339 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001340 if (family_priority == AF_INET) {
1341 if (newip4) {
1342 *newip = newip4;
1343 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001344 }
1345 else if (newip6) {
1346 *newip = newip6;
1347 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001348 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001349 if (!currentip_found)
1350 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001351 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001352 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001353 else if (family_priority == AF_INET6) {
1354 if (newip6) {
1355 *newip = newip6;
1356 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001357 }
1358 else if (newip4) {
1359 *newip = newip4;
1360 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001361 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001362 if (!currentip_found)
1363 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001364 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001365 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001366 else if (family_priority == AF_UNSPEC) {
1367 if (newip6) {
1368 *newip = newip6;
1369 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001370 }
1371 else if (newip4) {
1372 *newip = newip4;
1373 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001374 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001375 if (!currentip_found)
1376 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001377 }
1378
Christopher Faulet67957bd2017-09-27 11:00:59 +02001379 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001380 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001381
Christopher Faulet67957bd2017-09-27 11:00:59 +02001382 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001383 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001384 /* Move the first record to the end of the list, for internal
1385 * round robin */
1386 LIST_DEL(&record->list);
1387 LIST_ADDQ(&dns_p->answer_list, &record->list);
1388 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001389 }
1390 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001391}
1392
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001394 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001395 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1396 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001397 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001398 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1399 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001400 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001401int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001402{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001403 char *ptr;
1404 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001405
Christopher Faulet67957bd2017-09-27 11:00:59 +02001406 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001407 return -1;
1408
Christopher Faulet67957bd2017-09-27 11:00:59 +02001409 ptr = str;
1410 for (i = 0; i < dn_len-1; ++i) {
1411 sz = dn[i];
1412 if (i)
1413 *ptr++ = '.';
1414 memcpy(ptr, dn+i+1, sz);
1415 ptr += sz;
1416 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001417 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001418 *ptr++ = '\0';
1419 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001420}
1421
Christopher Faulet67957bd2017-09-27 11:00:59 +02001422/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1423 *
1424 * <str> must be a null-terminated string. <str_len> must include the
1425 * terminating null byte. <dn> buffer must be allocated and its size must be
1426 * passed in <dn_len>.
1427 *
1428 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1429 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001430 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001431int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001432{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001433 int i, offset;
1434
Christopher Faulet67957bd2017-09-27 11:00:59 +02001435 if (dn_len < str_len + 1)
1436 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001437
Christopher Faulet67957bd2017-09-27 11:00:59 +02001438 /* First byte of dn will be used to store the length of the first
1439 * label */
1440 offset = 0;
1441 for (i = 0; i < str_len; ++i) {
1442 if (str[i] == '.') {
1443 /* 2 or more consecutive dots is invalid */
1444 if (i == offset)
1445 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001446
Lukas Tribus81725b82020-02-27 15:47:24 +01001447 /* ignore trailing dot */
1448 if (i + 2 == str_len) {
1449 i++;
1450 break;
1451 }
1452
Christopher Faulet67957bd2017-09-27 11:00:59 +02001453 dn[offset] = (i - offset);
1454 offset = i+1;
1455 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001456 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001457 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001458 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001459 dn[offset] = (i - offset - 1);
1460 dn[i] = '\0';
1461 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001462}
1463
Christopher Faulet67957bd2017-09-27 11:00:59 +02001464/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001465 * - total size
1466 * - each label size individually
1467 * returns:
1468 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1469 * 1 when no error. <err> is left unaffected.
1470 */
1471int dns_hostname_validation(const char *string, char **err)
1472{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001473 int i;
1474
1475 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1476 if (err)
1477 *err = DNS_TOO_LONG_FQDN;
1478 return 0;
1479 }
1480
William Dauchyaecd5dc2020-01-26 19:52:34 +01001481 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001482 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001483 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1484 if (!(*string == '-' || *string == '_' ||
1485 (*string >= 'a' && *string <= 'z') ||
1486 (*string >= 'A' && *string <= 'Z') ||
1487 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001488 if (err)
1489 *err = DNS_INVALID_CHARACTER;
1490 return 0;
1491 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001492 i++;
1493 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001494 }
1495
William Dauchyaecd5dc2020-01-26 19:52:34 +01001496 if (!(*string))
1497 break;
1498
1499 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001500 if (err)
1501 *err = DNS_LABEL_TOO_LONG;
1502 return 0;
1503 }
1504
William Dauchyaecd5dc2020-01-26 19:52:34 +01001505 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001506 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001507 return 1;
1508}
1509
Christopher Faulet67957bd2017-09-27 11:00:59 +02001510/* Picks up an available resolution from the different resolution list
1511 * associated to a resolvers section, in this order:
1512 * 1. check in resolutions.curr for the same hostname and query_type
1513 * 2. check in resolutions.wait for the same hostname and query_type
1514 * 3. Get a new resolution from resolution pool
1515 *
1516 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001517 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001518static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1519 char **hostname_dn, int hostname_dn_len,
1520 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001521{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001522 struct dns_resolution *res;
1523
1524 if (!*hostname_dn)
1525 goto from_pool;
1526
1527 /* Search for same hostname and query type in resolutions.curr */
1528 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1529 if (!res->hostname_dn)
1530 continue;
1531 if ((query_type == res->prefered_query_type) &&
1532 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001533 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001534 return res;
1535 }
1536
1537 /* Search for same hostname and query type in resolutions.wait */
1538 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1539 if (!res->hostname_dn)
1540 continue;
1541 if ((query_type == res->prefered_query_type) &&
1542 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001543 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 return res;
1545 }
1546
1547 from_pool:
1548 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001549 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001550 if (res) {
1551 memset(res, 0, sizeof(*res));
1552 res->resolvers = resolvers;
1553 res->uuid = resolution_uuid;
1554 res->status = RSLV_STATUS_NONE;
1555 res->step = RSLV_STEP_NONE;
1556 res->last_valid = now_ms;
1557
1558 LIST_INIT(&res->requesters);
1559 LIST_INIT(&res->response.answer_list);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001560 LIST_INIT(&res->response.ar_list);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001561
1562 res->prefered_query_type = query_type;
1563 res->query_type = query_type;
1564 res->hostname_dn = *hostname_dn;
1565 res->hostname_dn_len = hostname_dn_len;
1566
1567 ++resolution_uuid;
1568
1569 /* Move the resolution to the resolvers wait queue */
1570 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1571 }
1572 return res;
1573}
1574
1575/* Releases a resolution from its requester(s) and move it back to the pool */
1576static void dns_free_resolution(struct dns_resolution *resolution)
1577{
1578 struct dns_requester *req, *reqback;
1579
1580 /* clean up configuration */
1581 dns_reset_resolution(resolution);
1582 resolution->hostname_dn = NULL;
1583 resolution->hostname_dn_len = 0;
1584
1585 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1586 LIST_DEL(&req->list);
1587 req->resolution = NULL;
1588 }
1589
1590 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001591 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001592}
1593
1594/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1595 * on success, -1 otherwise.
1596 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001597int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001598{
1599 struct dns_resolution *res = NULL;
1600 struct dns_requester *req;
1601 struct dns_resolvers *resolvers;
1602 struct server *srv = NULL;
1603 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001604 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001605 char **hostname_dn;
1606 int hostname_dn_len, query_type;
1607
1608 switch (requester_type) {
1609 case OBJ_TYPE_SERVER:
1610 srv = (struct server *)requester;
1611 hostname_dn = &srv->hostname_dn;
1612 hostname_dn_len = srv->hostname_dn_len;
1613 resolvers = srv->resolvers;
1614 query_type = ((srv->dns_opts.family_prio == AF_INET)
1615 ? DNS_RTYPE_A
1616 : DNS_RTYPE_AAAA);
1617 break;
1618
1619 case OBJ_TYPE_SRVRQ:
1620 srvrq = (struct dns_srvrq *)requester;
1621 hostname_dn = &srvrq->hostname_dn;
1622 hostname_dn_len = srvrq->hostname_dn_len;
1623 resolvers = srvrq->resolvers;
1624 query_type = DNS_RTYPE_SRV;
1625 break;
1626
Baptiste Assmann333939c2019-01-21 08:34:50 +01001627 case OBJ_TYPE_STREAM:
1628 stream = (struct stream *)requester;
1629 hostname_dn = &stream->dns_ctx.hostname_dn;
1630 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1631 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001632 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001633 ? DNS_RTYPE_A
1634 : DNS_RTYPE_AAAA);
1635 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001636 default:
1637 goto err;
1638 }
1639
1640 /* Get a resolution from the resolvers' wait queue or pool */
1641 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1642 goto err;
1643
1644 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001645 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001646 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001647 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001648 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001649 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001650 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001651 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001652 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001653 req->owner = &srv->obj_type;
1654 srv->dns_requester = req;
1655 }
1656 else
1657 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001658 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001659 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001660
1661 req->requester_cb = snr_resolution_cb;
1662 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001663 }
1664 else if (srvrq) {
1665 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001666 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001667 goto err;
1668 req->owner = &srvrq->obj_type;
1669 srvrq->dns_requester = req;
1670 }
1671 else
1672 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001673
1674 req->requester_cb = snr_resolution_cb;
1675 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001676 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001677 else if (stream) {
1678 if (stream->dns_ctx.dns_requester == NULL) {
1679 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1680 goto err;
1681 req->owner = &stream->obj_type;
1682 stream->dns_ctx.dns_requester = req;
1683 }
1684 else
1685 req = stream->dns_ctx.dns_requester;
1686
1687 req->requester_cb = act_resolution_cb;
1688 req->requester_error_cb = act_resolution_error_cb;
1689 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001690 else
1691 goto err;
1692
1693 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001694
1695 LIST_ADDQ(&res->requesters, &req->list);
1696 return 0;
1697
1698 err:
1699 if (res && LIST_ISEMPTY(&res->requesters))
1700 dns_free_resolution(res);
1701 return -1;
1702}
1703
1704/* Removes a requester from a DNS resoltion. It takes takes care of all the
1705 * consequences. It also cleans up some parameters from the requester.
1706 */
1707void dns_unlink_resolution(struct dns_requester *requester)
1708{
1709 struct dns_resolution *res;
1710 struct dns_requester *req;
1711
1712 /* Nothing to do */
1713 if (!requester || !requester->resolution)
1714 return;
1715 res = requester->resolution;
1716
1717 /* Clean up the requester */
1718 LIST_DEL(&requester->list);
1719 requester->resolution = NULL;
1720
1721 /* We need to find another requester linked on this resolution */
1722 if (!LIST_ISEMPTY(&res->requesters))
1723 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1724 else {
1725 dns_free_resolution(res);
1726 return;
1727 }
1728
1729 /* Move hostname_dn related pointers to the next requester */
1730 switch (obj_type(req->owner)) {
1731 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001732 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1733 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001734 break;
1735 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001736 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1737 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001738 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001739 case OBJ_TYPE_STREAM:
1740 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1741 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1742 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001743 default:
1744 res->hostname_dn = NULL;
1745 res->hostname_dn_len = 0;
1746 break;
1747 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001748}
1749
Christopher Faulet67957bd2017-09-27 11:00:59 +02001750/* Called when a network IO is generated on a name server socket for an incoming
1751 * packet. It performs the following actions:
1752 * - check if the packet requires processing (not outdated resolution)
1753 * - ensure the DNS packet received is valid and call requester's callback
1754 * - call requester's error callback if invalid response
1755 * - check the dn_name in the packet against the one sent
1756 */
1757static void dns_resolve_recv(struct dgram_conn *dgram)
1758{
1759 struct dns_nameserver *ns, *tmpns;
1760 struct dns_resolvers *resolvers;
1761 struct dns_resolution *res;
1762 struct dns_query_item *query;
1763 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1764 unsigned char *bufend;
1765 int fd, buflen, dns_resp;
1766 int max_answer_records;
1767 unsigned short query_id;
1768 struct eb32_node *eb;
1769 struct dns_requester *req;
1770
1771 fd = dgram->t.sock.fd;
1772
1773 /* check if ready for reading */
1774 if (!fd_recv_ready(fd))
1775 return;
1776
1777 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001778 if ((ns = dgram->owner) == NULL) {
1779 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1780 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001782 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001783
1784 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001785 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001786
1787 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001788 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001789 /* read message received */
1790 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1791 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001792 /* FIXME : for now we consider EAGAIN only, but at
1793 * least we purge sticky errors that would cause us to
1794 * be called in loops.
1795 */
1796 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001797 fd_cant_recv(fd);
1798 break;
1799 }
1800
1801 /* message too big */
1802 if (buflen > resolvers->accepted_payload_size) {
1803 ns->counters.too_big++;
1804 continue;
1805 }
1806
1807 /* initializing variables */
1808 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1809
1810 /* read the query id from the packet (16 bits) */
1811 if (buf + 2 > bufend) {
1812 ns->counters.invalid++;
1813 continue;
1814 }
1815 query_id = dns_response_get_query_id(buf);
1816
1817 /* search the query_id in the pending resolution tree */
1818 eb = eb32_lookup(&resolvers->query_ids, query_id);
1819 if (eb == NULL) {
1820 /* unknown query id means an outdated response and can be safely ignored */
1821 ns->counters.outdated++;
1822 continue;
1823 }
1824
William Dauchybe8a3872019-11-27 23:32:41 +01001825 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001826 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001827 /* number of responses received */
1828 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001829
Christopher Faulet67957bd2017-09-27 11:00:59 +02001830 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1831 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001832
Christopher Faulet67957bd2017-09-27 11:00:59 +02001833 switch (dns_resp) {
1834 case DNS_RESP_VALID:
1835 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001836
Christopher Faulet67957bd2017-09-27 11:00:59 +02001837 case DNS_RESP_INVALID:
1838 case DNS_RESP_QUERY_COUNT_ERROR:
1839 case DNS_RESP_WRONG_NAME:
1840 res->status = RSLV_STATUS_INVALID;
1841 ns->counters.invalid++;
1842 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001843
Christopher Faulet67957bd2017-09-27 11:00:59 +02001844 case DNS_RESP_NX_DOMAIN:
1845 res->status = RSLV_STATUS_NX;
1846 ns->counters.nx++;
1847 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001848
Christopher Faulet67957bd2017-09-27 11:00:59 +02001849 case DNS_RESP_REFUSED:
1850 res->status = RSLV_STATUS_REFUSED;
1851 ns->counters.refused++;
1852 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001853
Christopher Faulet67957bd2017-09-27 11:00:59 +02001854 case DNS_RESP_ANCOUNT_ZERO:
1855 res->status = RSLV_STATUS_OTHER;
1856 ns->counters.any_err++;
1857 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001858
Christopher Faulet67957bd2017-09-27 11:00:59 +02001859 case DNS_RESP_CNAME_ERROR:
1860 res->status = RSLV_STATUS_OTHER;
1861 ns->counters.cname_error++;
1862 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001863
Christopher Faulet67957bd2017-09-27 11:00:59 +02001864 case DNS_RESP_TRUNCATED:
1865 res->status = RSLV_STATUS_OTHER;
1866 ns->counters.truncated++;
1867 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001868
Christopher Faulet67957bd2017-09-27 11:00:59 +02001869 case DNS_RESP_NO_EXPECTED_RECORD:
1870 case DNS_RESP_ERROR:
1871 case DNS_RESP_INTERNAL:
1872 res->status = RSLV_STATUS_OTHER;
1873 ns->counters.other++;
1874 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001875 }
1876
Christopher Faulet67957bd2017-09-27 11:00:59 +02001877 /* Wait all nameservers response to handle errors */
1878 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1879 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001880
Christopher Faulet67957bd2017-09-27 11:00:59 +02001881 /* Process error codes */
1882 if (dns_resp != DNS_RESP_VALID) {
1883 if (res->prefered_query_type != res->query_type) {
1884 /* The fallback on the query type was already performed,
1885 * so check the try counter. If it falls to 0, we can
1886 * report an error. Else, wait the next attempt. */
1887 if (!res->try)
1888 goto report_res_error;
1889 }
1890 else {
1891 /* Fallback from A to AAAA or the opposite and re-send
1892 * the resolution immediately. try counter is not
1893 * decremented. */
1894 if (res->prefered_query_type == DNS_RTYPE_A) {
1895 res->query_type = DNS_RTYPE_AAAA;
1896 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001897 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1899 res->query_type = DNS_RTYPE_A;
1900 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001901 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001902 }
1903 continue;
1904 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001905
Christopher Faulet67957bd2017-09-27 11:00:59 +02001906 /* Now let's check the query's dname corresponds to the one we
1907 * sent. We can check only the first query of the list. We send
1908 * one query at a time so we get one query in the response */
1909 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
Olivier Houchardb17b8842020-04-01 18:30:27 +02001910 if (query && dns_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001911 dns_resp = DNS_RESP_WRONG_NAME;
1912 ns->counters.other++;
1913 goto report_res_error;
1914 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001915
Christopher Faulet67957bd2017-09-27 11:00:59 +02001916 /* So the resolution succeeded */
1917 res->status = RSLV_STATUS_VALID;
1918 res->last_valid = now_ms;
1919 ns->counters.valid++;
1920 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001921
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 report_res_error:
1923 list_for_each_entry(req, &res->requesters, list)
1924 req->requester_error_cb(req, dns_resp);
1925 dns_reset_resolution(res);
1926 LIST_DEL(&res->list);
1927 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1928 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001929
Christopher Faulet67957bd2017-09-27 11:00:59 +02001930 report_res_success:
1931 /* Only the 1rst requester s managed by the server, others are
1932 * from the cache */
1933 tmpns = ns;
1934 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001935 struct server *s = objt_server(req->owner);
1936
1937 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001938 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001939 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001940 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001941 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001943 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001944
Christopher Faulet67957bd2017-09-27 11:00:59 +02001945 dns_reset_resolution(res);
1946 LIST_DEL(&res->list);
1947 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1948 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001949 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001950 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001951 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001952}
William Lallemand69e96442016-11-19 00:58:54 +01001953
Christopher Faulet67957bd2017-09-27 11:00:59 +02001954/* Called when a resolvers network socket is ready to send data */
1955static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001956{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001957 struct dns_resolvers *resolvers;
1958 struct dns_nameserver *ns;
1959 struct dns_resolution *res;
1960 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001961
Christopher Faulet67957bd2017-09-27 11:00:59 +02001962 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001963
Christopher Faulet67957bd2017-09-27 11:00:59 +02001964 /* check if ready for sending */
1965 if (!fd_send_ready(fd))
1966 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001967
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 /* we don't want/need to be waked up any more for sending */
1969 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001970
Christopher Faulet67957bd2017-09-27 11:00:59 +02001971 /* no need to go further if we can't retrieve the nameserver */
1972 if ((ns = dgram->owner) == NULL)
1973 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001974
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001976 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001977
Christopher Faulet67957bd2017-09-27 11:00:59 +02001978 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001979 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001980
Christopher Faulet67957bd2017-09-27 11:00:59 +02001981 if (res->nb_queries == resolvers->nb_nameservers)
1982 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001983
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001984 len = dns_build_query(res->query_id, res->query_type,
1985 resolvers->accepted_payload_size,
1986 res->hostname_dn, res->hostname_dn_len,
1987 trash.area, trash.size);
1988 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001989 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001990
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001991 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01001992 if (ret != len) {
1993 if (ret == -1 && errno == EAGAIN) {
1994 /* retry once the socket is ready */
1995 fd_cant_send(fd);
1996 continue;
1997 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001998 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01001999 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002000
Christopher Faulet67957bd2017-09-27 11:00:59 +02002001 ns->counters.sent++;
2002 res->nb_queries++;
2003 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002004
Christopher Faulet67957bd2017-09-27 11:00:59 +02002005 snd_error:
2006 ns->counters.snd_error++;
2007 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002008 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002009 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002010}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002011
Christopher Faulet67957bd2017-09-27 11:00:59 +02002012/* Processes DNS resolution. First, it checks the active list to detect expired
2013 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2014 * checks the wait list to trigger new resolutions.
2015 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002016static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002017{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002018 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002019 struct dns_resolution *res, *resback;
2020 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002021
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002022 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002023
Christopher Faulet67957bd2017-09-27 11:00:59 +02002024 /* Handle all expired resolutions from the active list */
2025 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2026 /* When we find the first resolution in the future, then we can
2027 * stop here */
2028 exp = tick_add(res->last_query, resolvers->timeout.retry);
2029 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002030 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002031
Christopher Faulet67957bd2017-09-27 11:00:59 +02002032 /* If current resolution has been tried too many times and
2033 * finishes in timeout we update its status and remove it from
2034 * the list */
2035 if (!res->try) {
2036 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002037
Christopher Faulet67957bd2017-09-27 11:00:59 +02002038 /* Notify the result to the requesters */
2039 if (!res->nb_responses)
2040 res->status = RSLV_STATUS_TIMEOUT;
2041 list_for_each_entry(req, &res->requesters, list)
2042 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002043
Christopher Faulet67957bd2017-09-27 11:00:59 +02002044 /* Clean up resolution info and remove it from the
2045 * current list */
2046 dns_reset_resolution(res);
2047 LIST_DEL(&res->list);
2048 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002049 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002050 else {
2051 /* Otherwise resend the DNS query and requeue the resolution */
2052 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2053 /* No response received (a real timeout) or fallback already done */
2054 res->query_type = res->prefered_query_type;
2055 res->try--;
2056 }
2057 else {
2058 /* Fallback from A to AAAA or the opposite and re-send
2059 * the resolution immediately. try counter is not
2060 * decremented. */
2061 if (res->prefered_query_type == DNS_RTYPE_A)
2062 res->query_type = DNS_RTYPE_AAAA;
2063 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2064 res->query_type = DNS_RTYPE_A;
2065 else
2066 res->try--;
2067 }
2068 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002069 }
2070 }
William Lallemand69e96442016-11-19 00:58:54 +01002071
Christopher Faulet67957bd2017-09-27 11:00:59 +02002072 /* Handle all resolutions in the wait list */
2073 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2074 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2075 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2076 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002077
Christopher Faulet67957bd2017-09-27 11:00:59 +02002078 if (dns_run_resolution(res) != 1) {
2079 res->last_resolution = now_ms;
2080 LIST_DEL(&res->list);
2081 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002082 }
2083 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002084
Christopher Faulet67957bd2017-09-27 11:00:59 +02002085 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002086 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002087 return t;
2088}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002089
Christopher Faulet67957bd2017-09-27 11:00:59 +02002090/* proto_udp callback functions for a DNS resolution */
2091struct dgram_data_cb resolve_dgram_cb = {
2092 .recv = dns_resolve_recv,
2093 .send = dns_resolve_send,
2094};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002095
Christopher Faulet67957bd2017-09-27 11:00:59 +02002096/* Release memory allocated by DNS */
2097static void dns_deinit(void)
2098{
2099 struct dns_resolvers *resolvers, *resolversback;
2100 struct dns_nameserver *ns, *nsback;
2101 struct dns_resolution *res, *resback;
2102 struct dns_requester *req, *reqback;
2103 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002104
Christopher Faulet67957bd2017-09-27 11:00:59 +02002105 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2106 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2107 free(ns->id);
2108 free((char *)ns->conf.file);
2109 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2110 fd_delete(ns->dgram->t.sock.fd);
2111 free(ns->dgram);
2112 LIST_DEL(&ns->list);
2113 free(ns);
2114 }
2115
2116 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2117 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2118 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002119 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002120 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002121 dns_free_resolution(res);
2122 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002123
Christopher Faulet67957bd2017-09-27 11:00:59 +02002124 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2125 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2126 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002127 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002128 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002129 dns_free_resolution(res);
2130 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002131
Christopher Faulet67957bd2017-09-27 11:00:59 +02002132 free(resolvers->id);
2133 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002134 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002135 LIST_DEL(&resolvers->list);
2136 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002137 }
2138
Christopher Faulet67957bd2017-09-27 11:00:59 +02002139 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2140 free(srvrq->name);
2141 free(srvrq->hostname_dn);
2142 LIST_DEL(&srvrq->list);
2143 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002144 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002145}
2146
Christopher Faulet67957bd2017-09-27 11:00:59 +02002147/* Finalizes the DNS configuration by allocating required resources and checking
2148 * live parameters.
2149 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002150 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002151static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002152{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002153 struct dns_resolvers *resolvers;
2154 struct proxy *px;
2155 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002156
Christopher Faulet67957bd2017-09-27 11:00:59 +02002157 /* allocate pool of resolution per resolvers */
2158 list_for_each_entry(resolvers, &dns_resolvers, list) {
2159 struct dns_nameserver *ns;
2160 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002161
Christopher Faulet67957bd2017-09-27 11:00:59 +02002162 /* Check if we can create the socket with nameservers info */
2163 list_for_each_entry(ns, &resolvers->nameservers, list) {
2164 struct dgram_conn *dgram = NULL;
2165 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002166
Christopher Faulet67957bd2017-09-27 11:00:59 +02002167 /* Check nameserver info */
2168 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002169 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2170 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002171 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002172 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002173 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002174 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002175 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2176 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002177 close(fd);
2178 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002179 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002180 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002181 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002182
Christopher Faulet67957bd2017-09-27 11:00:59 +02002183 /* Create dgram structure that will hold the UPD socket
2184 * and attach it on the current nameserver */
2185 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002186 ha_alert("config: resolvers '%s' : out of memory.\n",
2187 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002188 err_code |= (ERR_ALERT|ERR_ABORT);
2189 goto err;
2190 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002191
Christopher Faulet67957bd2017-09-27 11:00:59 +02002192 /* Leave dgram partially initialized, no FD attached for
2193 * now. */
2194 dgram->owner = ns;
2195 dgram->data = &resolve_dgram_cb;
2196 dgram->t.sock.fd = -1;
2197 ns->dgram = dgram;
2198 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002199
Christopher Faulet67957bd2017-09-27 11:00:59 +02002200 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002201 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002202 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002203 err_code |= (ERR_ALERT|ERR_ABORT);
2204 goto err;
2205 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002206
Christopher Faulet67957bd2017-09-27 11:00:59 +02002207 /* Update task's parameters */
2208 t->process = dns_process_resolvers;
2209 t->context = resolvers;
2210 resolvers->t = t;
2211 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002212 }
2213
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002214 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002215 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002216
Christopher Faulet67957bd2017-09-27 11:00:59 +02002217 for (srv = px->srv; srv; srv = srv->next) {
2218 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002219
Christopher Faulet67957bd2017-09-27 11:00:59 +02002220 if (!srv->resolvers_id)
2221 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002222
Christopher Faulet67957bd2017-09-27 11:00:59 +02002223 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002224 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2225 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002226 err_code |= (ERR_ALERT|ERR_ABORT);
2227 continue;
2228 }
2229 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002230
Christopher Faulet67957bd2017-09-27 11:00:59 +02002231 if (srv->srvrq && !srv->srvrq->resolvers) {
2232 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002233 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002234 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2235 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002236 err_code |= (ERR_ALERT|ERR_ABORT);
2237 continue;
2238 }
2239 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002240 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002241 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2242 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002243 err_code |= (ERR_ALERT|ERR_ABORT);
2244 continue;
2245 }
2246 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002247 }
2248
Christopher Faulet67957bd2017-09-27 11:00:59 +02002249 if (err_code & (ERR_ALERT|ERR_ABORT))
2250 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002251
Christopher Faulet67957bd2017-09-27 11:00:59 +02002252 return err_code;
2253 err:
2254 dns_deinit();
2255 return err_code;
2256
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002257}
2258
Christopher Faulet67957bd2017-09-27 11:00:59 +02002259/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002260static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002261{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002262 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002263
Christopher Faulet67957bd2017-09-27 11:00:59 +02002264 if (*args[2]) {
2265 list_for_each_entry(presolvers, &dns_resolvers, list) {
2266 if (strcmp(presolvers->id, args[2]) == 0) {
2267 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002268 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002269 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002270 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002271 if (appctx->ctx.cli.p0 == NULL)
2272 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002273 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002274 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002275}
2276
Christopher Faulet67957bd2017-09-27 11:00:59 +02002277/* Dumps counters from all resolvers section and associated name servers. It
2278 * returns 0 if the output buffer is full and it needs to be called again,
2279 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002280 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002281 */
2282static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2283{
2284 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002285 struct dns_resolvers *resolvers;
2286 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002287
2288 chunk_reset(&trash);
2289
2290 switch (appctx->st2) {
2291 case STAT_ST_INIT:
2292 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2293 /* fall through */
2294
2295 case STAT_ST_LIST:
2296 if (LIST_ISEMPTY(&dns_resolvers)) {
2297 chunk_appendf(&trash, "No resolvers found\n");
2298 }
2299 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002300 list_for_each_entry(resolvers, &dns_resolvers, list) {
2301 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002302 continue;
2303
Christopher Faulet67957bd2017-09-27 11:00:59 +02002304 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2305 list_for_each_entry(ns, &resolvers->nameservers, list) {
2306 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2307 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2308 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2309 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2310 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2311 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2312 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2313 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2314 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2315 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2316 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2317 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2318 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2319 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2320 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2321 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002322 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002323 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002324 }
2325 }
2326
2327 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002328 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002329 /* let's try again later from this session. We add ourselves into
2330 * this session's users so that it can remove us upon termination.
2331 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002332 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002333 return 0;
2334 }
William Lallemand69e96442016-11-19 00:58:54 +01002335 /* fall through */
2336
2337 default:
2338 appctx->st2 = STAT_ST_FIN;
2339 return 1;
2340 }
2341}
2342
2343/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002344static struct cli_kw_list cli_kws = {{ }, {
2345 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2346 " associated name servers",
2347 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2348 {{},}
2349 }
2350};
William Lallemand69e96442016-11-19 00:58:54 +01002351
Willy Tarreau0108d902018-11-25 19:14:37 +01002352INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002353
Baptiste Assmann333939c2019-01-21 08:34:50 +01002354/*
2355 * Prepare <rule> for hostname resolution.
2356 * Returns -1 in case of any allocation failure, 0 if not.
2357 * On error, a global failure counter is also incremented.
2358 */
2359static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2360{
2361 char *hostname_dn;
2362 int hostname_len, hostname_dn_len;
2363 struct buffer *tmp = get_trash_chunk();
2364
2365 if (!hostname)
2366 return 0;
2367
2368 hostname_len = strlen(hostname);
2369 hostname_dn = tmp->area;
2370 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2371 hostname_dn, tmp->size);
2372 if (hostname_dn_len == -1)
2373 goto err;
2374
2375
2376 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2377 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2378 if (!stream->dns_ctx.hostname_dn)
2379 goto err;
2380
2381 return 0;
2382
2383 err:
2384 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2385 dns_failed_resolutions += 1;
2386 return -1;
2387}
2388
2389
2390/*
2391 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2392 */
2393enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2394 struct session *sess, struct stream *s, int flags)
2395{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002396 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002397 struct sample *smp;
2398 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002399 struct dns_requester *req;
2400 struct dns_resolvers *resolvers;
2401 struct dns_resolution *res;
2402 int exp;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002403
2404 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002405 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002406 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2407 resolution = s->dns_ctx.dns_requester->resolution;
Baptiste Assmann4c52e4b2019-10-01 15:32:40 +02002408 if (resolution->step == RSLV_STEP_RUNNING) {
2409 return ACT_RET_YIELD;
2410 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01002411 if (resolution->step == RSLV_STEP_NONE) {
2412 /* We update the variable only if we have a valid response. */
2413 if (resolution->status == RSLV_STATUS_VALID) {
2414 struct sample smp;
2415 short ip_sin_family = 0;
2416 void *ip = NULL;
2417
Christopher Fauleta4168432020-01-24 18:08:42 +01002418 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002419 0, &ip, &ip_sin_family, NULL);
2420
2421 switch (ip_sin_family) {
2422 case AF_INET:
2423 smp.data.type = SMP_T_IPV4;
2424 memcpy(&smp.data.u.ipv4, ip, 4);
2425 break;
2426 case AF_INET6:
2427 smp.data.type = SMP_T_IPV6;
2428 memcpy(&smp.data.u.ipv6, ip, 16);
2429 break;
2430 default:
2431 ip = NULL;
2432 }
2433
2434 if (ip) {
2435 smp.px = px;
2436 smp.sess = sess;
2437 smp.strm = s;
2438
2439 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2440 }
2441 }
2442 }
2443
2444 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
2445 s->dns_ctx.hostname_dn_len = 0;
2446 dns_unlink_resolution(s->dns_ctx.dns_requester);
2447
2448 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2449 s->dns_ctx.dns_requester = NULL;
2450
2451 return ACT_RET_CONT;
2452 }
2453
2454 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002455 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2456 if (smp == NULL)
2457 return ACT_RET_CONT;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002458
Willy Tarreau45726fd2019-07-17 10:38:45 +02002459 fqdn = smp->data.u.str.area;
2460 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet13403762019-12-13 09:01:57 +01002461 return ACT_RET_CONT; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002462
Willy Tarreau45726fd2019-07-17 10:38:45 +02002463 s->dns_ctx.parent = rule;
2464 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002465
2466 /* Check if there is a fresh enough response in the cache of our associated resolution */
2467 req = s->dns_ctx.dns_requester;
2468 if (!req || !req->resolution) {
2469 dns_trigger_resolution(s->dns_ctx.dns_requester);
2470 return ACT_RET_YIELD;
2471 }
2472 res = req->resolution;
2473 resolvers = res->resolvers;
2474
2475 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2476 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2477 && !tick_is_expired(exp, now_ms)) {
2478 goto use_cache;
2479 }
2480
Willy Tarreau45726fd2019-07-17 10:38:45 +02002481 dns_trigger_resolution(s->dns_ctx.dns_requester);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002482 return ACT_RET_YIELD;
2483}
2484
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002485static void release_dns_action(struct act_rule *rule)
2486{
2487 release_sample_expr(rule->arg.dns.expr);
2488 free(rule->arg.dns.varname);
2489 free(rule->arg.dns.resolvers_id);
2490 free(rule->arg.dns.dns_opts);
2491}
2492
Baptiste Assmann333939c2019-01-21 08:34:50 +01002493
2494/* parse "do-resolve" action
2495 * This action takes the following arguments:
2496 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2497 *
2498 * - <varName> is the variable name where the result of the DNS resolution will be stored
2499 * (mandatory)
2500 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2501 * (mandatory)
2502 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2503 * (optional), defaults to ipv6
2504 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2505 */
2506enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2507{
2508 int cur_arg;
2509 struct sample_expr *expr;
2510 unsigned int where;
2511 const char *beg, *end;
2512
2513 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2514 cur_arg = *orig_arg - 1;
2515
2516 /* locate varName, which is mandatory */
2517 beg = strchr(args[cur_arg], '(');
2518 if (beg == NULL)
2519 goto do_resolve_parse_error;
2520 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2521 end = strchr(beg, ',');
2522 if (end == NULL)
2523 goto do_resolve_parse_error;
2524 rule->arg.dns.varname = my_strndup(beg, end - beg);
2525 if (rule->arg.dns.varname == NULL)
2526 goto do_resolve_parse_error;
2527
2528
2529 /* locate resolversSectionName, which is mandatory.
2530 * Since next parameters are optional, the delimiter may be comma ','
2531 * or closing parenthesis ')'
2532 */
2533 beg = end + 1;
2534 end = strchr(beg, ',');
2535 if (end == NULL)
2536 end = strchr(beg, ')');
2537 if (end == NULL)
2538 goto do_resolve_parse_error;
2539 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2540 if (rule->arg.dns.resolvers_id == NULL)
2541 goto do_resolve_parse_error;
2542
2543
Christopher Fauleta4168432020-01-24 18:08:42 +01002544 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2545 if (rule->arg.dns.dns_opts == NULL)
2546 goto do_resolve_parse_error;
2547
Baptiste Assmann333939c2019-01-21 08:34:50 +01002548 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002549 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002550
2551 /* optional arguments accepted for now:
2552 * ipv4 or ipv6
2553 */
2554 while (*end != ')') {
2555 beg = end + 1;
2556 end = strchr(beg, ',');
2557 if (end == NULL)
2558 end = strchr(beg, ')');
2559 if (end == NULL)
2560 goto do_resolve_parse_error;
2561
2562 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002563 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002564 }
2565 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002566 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002567 }
2568 else {
2569 goto do_resolve_parse_error;
2570 }
2571 }
2572
2573 cur_arg = cur_arg + 1;
2574
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002575 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 +01002576 if (!expr)
2577 goto do_resolve_parse_error;
2578
2579
2580 where = 0;
2581 if (px->cap & PR_CAP_FE)
2582 where |= SMP_VAL_FE_HRQ_HDR;
2583 if (px->cap & PR_CAP_BE)
2584 where |= SMP_VAL_BE_HRQ_HDR;
2585
2586 if (!(expr->fetch->val & where)) {
2587 memprintf(err,
2588 "fetch method '%s' extracts information from '%s', none of which is available here",
2589 args[cur_arg-1], sample_src_names(expr->fetch->use));
2590 free(expr);
2591 return ACT_RET_PRS_ERR;
2592 }
2593 rule->arg.dns.expr = expr;
2594 rule->action = ACT_CUSTOM;
2595 rule->action_ptr = dns_action_do_resolve;
2596 *orig_arg = cur_arg;
2597
2598 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002599 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002600
2601 return ACT_RET_PRS_OK;
2602
2603 do_resolve_parse_error:
2604 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2605 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2606 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2607 args[cur_arg]);
2608 return ACT_RET_PRS_ERR;
2609}
2610
2611static struct action_kw_list http_req_kws = { { }, {
2612 { "do-resolve", dns_parse_do_resolve, 1 },
2613 { /* END */ }
2614}};
2615
2616INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2617
2618static struct action_kw_list tcp_req_cont_actions = {ILH, {
2619 { "do-resolve", dns_parse_do_resolve, 1 },
2620 { /* END */ }
2621}};
2622
2623INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2624
2625/* Check an "http-request do-resolve" action.
2626 *
2627 * The function returns 1 in success case, otherwise, it returns 0 and err is
2628 * filled.
2629 */
2630int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2631{
2632 struct dns_resolvers *resolvers = NULL;
2633
2634 if (rule->arg.dns.resolvers_id == NULL) {
2635 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2636 return 0;
2637 }
2638
2639 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2640 if (resolvers == NULL) {
2641 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2642 return 0;
2643 }
2644 rule->arg.dns.resolvers = resolvers;
2645
2646 return 1;
2647}
2648
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002649REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002650REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);