blob: d251674c97856d96f0b87ed190607c4b2082fcf1 [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 Tarreau8d366972020-05-27 16:10:29 +020025#include <haproxy/errors.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020026#include <haproxy/time.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020027#include <haproxy/ticks.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020028#include <haproxy/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020029
William Lallemand69e96442016-11-19 00:58:54 +010030#include <types/applet.h>
31#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032#include <types/global.h>
33#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010034#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035
William Lallemand69e96442016-11-19 00:58:54 +010036#include <proto/channel.h>
37#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020038#include <proto/checks.h>
39#include <proto/dns.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020040#include <haproxy/fd.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020041#include <proto/http_ana.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010042#include <proto/http_rules.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020043#include <proto/log.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010044#include <proto/sample.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020045#include <proto/server.h>
46#include <proto/task.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 Assmann333939c2019-01-21 08:34:50 +010050#include <proto/tcp_rules.h>
51#include <proto/vars.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020052
Christopher Faulet67957bd2017-09-27 11:00:59 +020053struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
54struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020055
Tim Duesterhusfcac33d2020-01-18 02:04:12 +010056static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010057
58DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
59DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010060DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010061
Christopher Faulet67957bd2017-09-27 11:00:59 +020062static unsigned int resolution_uuid = 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +010063unsigned int dns_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020064
Christopher Faulet67957bd2017-09-27 11:00:59 +020065/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
66 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020067 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020068struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069{
Christopher Faulet67957bd2017-09-27 11:00:59 +020070 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071
Christopher Faulet67957bd2017-09-27 11:00:59 +020072 list_for_each_entry(res, &dns_resolvers, list) {
73 if (!strcmp(res->id, id))
74 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020075 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020076 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077}
78
Olivier Houchardb17b8842020-04-01 18:30:27 +020079/* Compare hostnames in a case-insensitive way .
80 * Returns 0 if they are the same, non-zero otherwise
81 */
82static __inline int dns_hostname_cmp(const char *name1, const char *name2, int len)
83{
84 int i;
85
86 for (i = 0; i < len; i++)
87 if (tolower(name1[i]) != tolower(name2[i]))
88 return -1;
89 return 0;
90}
91
Christopher Faulet67957bd2017-09-27 11:00:59 +020092/* Returns a pointer on the SRV request matching the name <name> for the proxy
93 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020094 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020095struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020096{
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020098
Christopher Faulet67957bd2017-09-27 11:00:59 +020099 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
100 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
101 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200102 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200103 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200104}
105
Christopher Faulet67957bd2017-09-27 11:00:59 +0200106/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
107 * NULL if an error occurred. */
108struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200109{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200110 struct proxy *px = srv->proxy;
111 struct dns_srvrq *srvrq = NULL;
112 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200113
Christopher Faulet67957bd2017-09-27 11:00:59 +0200114 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200115 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
116 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200117 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100118 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
119 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200120 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200121 }
122
Christopher Faulet67957bd2017-09-27 11:00:59 +0200123 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100124 ha_alert("config : %s '%s', server '%s': out of memory\n",
125 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200126 goto err;
127 }
128 srvrq->obj_type = OBJ_TYPE_SRVRQ;
129 srvrq->proxy = px;
130 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200131 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200132 srvrq->hostname_dn_len = hostname_dn_len;
133 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100134 ha_alert("config : %s '%s', server '%s': out of memory\n",
135 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200136 goto err;
137 }
138 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
139 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200140
Christopher Faulet67957bd2017-09-27 11:00:59 +0200141 err:
142 if (srvrq) {
143 free(srvrq->name);
144 free(srvrq->hostname_dn);
145 free(srvrq);
146 }
147 return NULL;
148}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200149
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151/* 2 bytes random generator to generate DNS query ID */
152static inline uint16_t dns_rnd16(void)
153{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200154 if (!dns_query_id_seed)
155 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200156 dns_query_id_seed ^= dns_query_id_seed << 13;
157 dns_query_id_seed ^= dns_query_id_seed >> 7;
158 dns_query_id_seed ^= dns_query_id_seed << 17;
159 return dns_query_id_seed;
160}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200161
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200162
Christopher Faulet67957bd2017-09-27 11:00:59 +0200163static inline int dns_resolution_timeout(struct dns_resolution *res)
164{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100165 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200166}
167
Christopher Faulet67957bd2017-09-27 11:00:59 +0200168/* Updates a resolvers' task timeout for next wake up and queue it */
169static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200170{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200171 struct dns_resolution *res;
172 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173
Christopher Faulet67957bd2017-09-27 11:00:59 +0200174 next = tick_add(now_ms, resolvers->timeout.resolve);
175 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
176 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
177 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
178 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200179
Christopher Faulet67957bd2017-09-27 11:00:59 +0200180 list_for_each_entry(res, &resolvers->resolutions.wait, list)
181 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200182
Christopher Faulet67957bd2017-09-27 11:00:59 +0200183 resolvers->t->expire = next;
184 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200185}
186
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
188 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200189 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200191{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200192 struct dgram_conn *dgram = ns->dgram;
193 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200194
Christopher Faulet67957bd2017-09-27 11:00:59 +0200195 /* Already connected */
196 if (dgram->t.sock.fd != -1)
197 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200198
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 /* Create an UDP socket and connect it on the nameserver's IP/Port */
200 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
201 send_log(NULL, LOG_WARNING,
202 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
203 ns->resolvers->id, ns->id);
204 return -1;
205 }
206 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
207 send_log(NULL, LOG_WARNING,
208 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
209 ns->resolvers->id, ns->id);
210 close(fd);
211 return -1;
212 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200213
Christopher Faulet67957bd2017-09-27 11:00:59 +0200214 /* Make the socket non blocking */
215 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200216
Christopher Faulet67957bd2017-09-27 11:00:59 +0200217 /* Add the fd in the fd list and update its parameters */
218 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100219 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200220 fd_want_recv(fd);
221 return 0;
222}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224/* Forges a DNS query. It needs the following information from the caller:
225 * - <query_id> : the DNS query id corresponding to this query
226 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
227 * - <hostname_dn> : hostname in domain name format
228 * - <hostname_dn_len> : length of <hostname_dn>
229 *
230 * To store the query, the caller must pass a buffer <buf> and its size
231 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
232 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200233 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200234static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
235 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200236{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200237 struct dns_header dns_hdr;
238 struct dns_question qinfo;
239 struct dns_additional_record edns;
240 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200241
Christopher Faulet67957bd2017-09-27 11:00:59 +0200242 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
243 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200244
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200246
Christopher Faulet67957bd2017-09-27 11:00:59 +0200247 /* Set dns query headers */
248 dns_hdr.id = (unsigned short) htons(query_id);
249 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
250 dns_hdr.qdcount = htons(1); /* 1 question */
251 dns_hdr.ancount = 0;
252 dns_hdr.nscount = 0;
253 dns_hdr.arcount = htons(1);
254 memcpy(p, &dns_hdr, sizeof(dns_hdr));
255 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200256
Christopher Faulet67957bd2017-09-27 11:00:59 +0200257 /* Set up query hostname */
258 memcpy(p, hostname_dn, hostname_dn_len);
259 p += hostname_dn_len;
260 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200261
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262 /* Set up query info (type and class) */
263 qinfo.qtype = htons(query_type);
264 qinfo.qclass = htons(DNS_RCLASS_IN);
265 memcpy(p, &qinfo, sizeof(qinfo));
266 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200267
Christopher Faulet67957bd2017-09-27 11:00:59 +0200268 /* Set the DNS extension */
269 edns.name = 0;
270 edns.type = htons(DNS_RTYPE_OPT);
271 edns.udp_payload_size = htons(accepted_payload_size);
272 edns.extension = 0;
273 edns.data_length = 0;
274 memcpy(p, &edns, sizeof(edns));
275 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200276
Christopher Faulet67957bd2017-09-27 11:00:59 +0200277 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200278}
279
Christopher Faulet67957bd2017-09-27 11:00:59 +0200280/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
281 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200282 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200283static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200284{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200285 struct dns_resolvers *resolvers = resolution->resolvers;
286 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100287 int len;
288
289 /* Update resolution */
290 resolution->nb_queries = 0;
291 resolution->nb_responses = 0;
292 resolution->last_query = now_ms;
293
294 len = dns_build_query(resolution->query_id, resolution->query_type,
295 resolvers->accepted_payload_size,
296 resolution->hostname_dn, resolution->hostname_dn_len,
297 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200298
Christopher Faulet67957bd2017-09-27 11:00:59 +0200299 list_for_each_entry(ns, &resolvers->nameservers, list) {
300 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100301 int ret;
302
Christopher Faulet67957bd2017-09-27 11:00:59 +0200303 if (fd == -1) {
304 if (dns_connect_namesaver(ns) == -1)
305 continue;
306 fd = ns->dgram->t.sock.fd;
307 resolvers->nb_nameservers++;
308 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200309
Willy Tarreau0eae6322019-12-20 11:18:54 +0100310 if (len < 0)
311 goto snd_error;
312
313 ret = send(fd, trash.area, len, 0);
314 if (ret == len) {
315 ns->counters.sent++;
316 resolution->nb_queries++;
317 continue;
318 }
319
320 if (ret == -1 && errno == EAGAIN) {
321 /* retry once the socket is ready */
322 fd_cant_send(fd);
323 continue;
324 }
325
326 snd_error:
327 ns->counters.snd_error++;
328 resolution->nb_queries++;
329 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200330
Christopher Faulet67957bd2017-09-27 11:00:59 +0200331 /* Push the resolution at the end of the active list */
332 LIST_DEL(&resolution->list);
333 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
334 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200335}
336
Christopher Faulet67957bd2017-09-27 11:00:59 +0200337/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
338 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200339 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200340static int
341dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200342{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200343 struct dns_resolvers *resolvers = resolution->resolvers;
344 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200345
Christopher Faulet67957bd2017-09-27 11:00:59 +0200346 /* Avoid sending requests for resolutions that don't yet have an
347 * hostname, ie resolutions linked to servers that do not yet have an
348 * fqdn */
349 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200350 return 0;
351
Christopher Faulet67957bd2017-09-27 11:00:59 +0200352 /* Check if a resolution has already been started for this server return
353 * directly to avoid resolution pill up. */
354 if (resolution->step != RSLV_STEP_NONE)
355 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200356
Christopher Faulet67957bd2017-09-27 11:00:59 +0200357 /* Generates a new query id. We try at most 100 times to find a free
358 * query id */
359 for (i = 0; i < 100; ++i) {
360 query_id = dns_rnd16();
361 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200362 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200363 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200364 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200365 if (query_id == -1) {
366 send_log(NULL, LOG_NOTICE,
367 "could not generate a query id for %s, in resolvers %s.\n",
368 resolution->hostname_dn, resolvers->id);
369 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200370 }
371
Christopher Faulet67957bd2017-09-27 11:00:59 +0200372 /* Update resolution parameters */
373 resolution->query_id = query_id;
374 resolution->qid.key = query_id;
375 resolution->step = RSLV_STEP_RUNNING;
376 resolution->query_type = resolution->prefered_query_type;
377 resolution->try = resolvers->resolve_retries;
378 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200379
Christopher Faulet67957bd2017-09-27 11:00:59 +0200380 /* Send the DNS query */
381 resolution->try -= 1;
382 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200383 return 1;
384}
385
Christopher Faulet67957bd2017-09-27 11:00:59 +0200386/* Performs a name resolution for the requester <req> */
387void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200388{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200389 struct dns_resolvers *resolvers;
390 struct dns_resolution *res;
391 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200392
Christopher Faulet67957bd2017-09-27 11:00:59 +0200393 if (!req || !req->resolution)
394 return;
395 res = req->resolution;
396 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200397
Christopher Faulet67957bd2017-09-27 11:00:59 +0200398 /* The resolution must not be triggered yet. Use the cached response, if
399 * valid */
400 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200401 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
402 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
403 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200405
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200406
Christopher Faulet67957bd2017-09-27 11:00:59 +0200407/* Resets some resolution parameters to initial values and also delete the query
408 * ID from the resolver's tree.
409 */
410static void dns_reset_resolution(struct dns_resolution *resolution)
411{
412 /* update resolution status */
413 resolution->step = RSLV_STEP_NONE;
414 resolution->try = 0;
415 resolution->last_resolution = now_ms;
416 resolution->nb_queries = 0;
417 resolution->nb_responses = 0;
418 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200419
Christopher Faulet67957bd2017-09-27 11:00:59 +0200420 /* clean up query id */
421 eb32_delete(&resolution->qid);
422 resolution->query_id = 0;
423 resolution->qid.key = 0;
424}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200425
Christopher Faulet67957bd2017-09-27 11:00:59 +0200426/* Returns the query id contained in a DNS response */
427static inline unsigned short dns_response_get_query_id(unsigned char *resp)
428{
429 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200430}
431
Christopher Faulet67957bd2017-09-27 11:00:59 +0200432
433/* Analyses, re-builds and copies the name <name> from the DNS response packet
434 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
435 * for compressed data. The result is copied into <dest>, ensuring we don't
436 * overflow using <dest_len> Returns the number of bytes the caller can move
437 * forward. If 0 it means an error occurred while parsing the name. <offset> is
438 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200439 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200440int dns_read_name(unsigned char *buffer, unsigned char *bufend,
441 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100442 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200443{
444 int nb_bytes = 0, n = 0;
445 int label_len;
446 unsigned char *reader = name;
447 char *dest = destination;
448
449 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100450 if (reader >= bufend)
451 goto err;
452
Christopher Faulet67957bd2017-09-27 11:00:59 +0200453 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200454 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100455 if (reader + 1 >= bufend)
456 goto err;
457
Christopher Faulet67957bd2017-09-27 11:00:59 +0200458 /* Must point BEFORE current position */
459 if ((buffer + reader[1]) > reader)
460 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200461
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100462 if (depth++ > 100)
463 goto err;
464
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530465 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100466 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200467 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200468 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200469
Christopher Faulet67957bd2017-09-27 11:00:59 +0200470 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200471 nb_bytes += n;
472 goto out;
473 }
474
475 label_len = *reader;
476 if (label_len == 0)
477 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200478
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200479 /* Check if:
480 * - we won't read outside the buffer
481 * - there is enough place in the destination
482 */
483 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200484 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200485
486 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200487 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200488
489 memcpy(dest, reader, label_len);
490
Christopher Faulet67957bd2017-09-27 11:00:59 +0200491 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200492 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200493 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200494 }
495
Christopher Faulet67957bd2017-09-27 11:00:59 +0200496 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200497 /* offset computation:
498 * parse from <name> until finding either NULL or a pointer "c0xx"
499 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200500 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200501 *offset = 0;
502 while (reader < bufend) {
503 if ((reader[0] & 0xc0) == 0xc0) {
504 *offset += 2;
505 break;
506 }
507 else if (*reader == 0) {
508 *offset += 1;
509 break;
510 }
511 *offset += 1;
512 ++reader;
513 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200514 return nb_bytes;
515
Christopher Faulet67957bd2017-09-27 11:00:59 +0200516 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200517 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200518}
519
Christopher Faulet67957bd2017-09-27 11:00:59 +0200520/* Checks for any obsolete record, also identify any SRV request, and try to
521 * find a corresponding server.
522*/
523static void dns_check_dns_response(struct dns_resolution *res)
524{
525 struct dns_resolvers *resolvers = res->resolvers;
526 struct dns_requester *req, *reqback;
527 struct dns_answer_item *item, *itemback;
528 struct server *srv;
529 struct dns_srvrq *srvrq;
530
Baptiste Assmann13a92322019-06-07 09:40:55 +0200531 /* clean up obsolete Additional records */
532 list_for_each_entry_safe(item, itemback, &res->response.ar_list, list) {
533 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
534 LIST_DEL(&item->list);
535 pool_free(dns_answer_item_pool, item);
536 }
537 }
538
Christopher Faulet67957bd2017-09-27 11:00:59 +0200539 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
540
541 /* Remove obsolete items */
542 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
543 if (item->type != DNS_RTYPE_SRV)
544 goto rm_obselete_item;
545
546 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
547 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
548 continue;
549
550 /* Remove any associated server */
551 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100552 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200553 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
554 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200555 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200556 snr_update_srv_status(srv, 1);
557 free(srv->hostname);
558 free(srv->hostname_dn);
559 srv->hostname = NULL;
560 srv->hostname_dn = NULL;
561 srv->hostname_dn_len = 0;
562 dns_unlink_resolution(srv->dns_requester);
563 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100564 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200565 }
566 }
567
568 rm_obselete_item:
569 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100570 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200571 continue;
572 }
573
574 if (item->type != DNS_RTYPE_SRV)
575 continue;
576
577 /* Now process SRV records */
578 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
579 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
580 continue;
581
582 /* Check if a server already uses that hostname */
583 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100584 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200585 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
586 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200587 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len) &&
Daniel Corbettf8716912019-11-17 09:48:56 -0500588 !srv->dns_opts.ignore_weight) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100589 int ha_weight;
590
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200591 /* DNS weight range if from 0 to 65535
592 * HAProxy weight is from 0 to 256
593 * The rule below ensures that weight 0 is well respected
594 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100595 */
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200596 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100597 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200598 char weight[9];
599
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100600 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200601 server_parse_weight_change_request(srv, weight);
602 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100603 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200604 break;
605 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100606 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200607 }
608 if (srv)
609 continue;
610
611 /* If not, try to find a server with undefined hostname */
612 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100613 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200614 if (srv->srvrq == srvrq && !srv->hostname_dn)
615 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100616 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200617 }
618 /* And update this server, if found */
619 if (srv) {
620 const char *msg = NULL;
621 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100622 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200623 char hostname[DNS_MAX_NAME_SIZE];
624
625 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100626 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100627 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200628 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100629 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200630
631 /* Check if an Additional Record is associated to this SRV record.
632 * Perform some sanity checks too to ensure the record can be used.
633 * If all fine, we simply pick up the IP address found and associate
634 * it to the server.
635 */
636 if ((item->ar_item != NULL) &&
637 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
638 {
639
640 switch (item->ar_item->type) {
641 case DNS_RTYPE_A:
642 update_server_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional recrd");
643 break;
644 case DNS_RTYPE_AAAA:
645 update_server_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional recrd");
646 break;
647 }
648
649 srv->flags |= SRV_F_NO_RESOLUTION;
650 }
651
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100652 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200653 if (msg)
654 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
655
656 srv->svc_port = item->port;
657 srv->flags &= ~SRV_F_MAPPORTS;
658 if ((srv->check.state & CHK_ST_CONFIGURED) &&
659 !(srv->flags & SRV_F_CHECKPORT))
660 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100661
Daniel Corbettf8716912019-11-17 09:48:56 -0500662 if (!srv->dns_opts.ignore_weight) {
663 /* DNS weight range if from 0 to 65535
664 * HAProxy weight is from 0 to 256
665 * The rule below ensures that weight 0 is well respected
666 * while allowing a "mapping" from DNS weight into HAProxy's one.
667 */
668 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100669
Daniel Corbettf8716912019-11-17 09:48:56 -0500670 snprintf(weight, sizeof(weight), "%d", ha_weight);
671 server_parse_weight_change_request(srv, weight);
672 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100673 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200674 }
675 }
676 }
677}
678
679/* Validates that the buffer DNS response provided in <resp> and finishing
680 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200681 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200682 * The result is stored in <resolution>' response, buf_response,
683 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 *
685 * This function returns one of the DNS_RESP_* code to indicate the type of
686 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200687 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200688static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
689 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200690{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200691 unsigned char *reader;
692 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200693 int len, flags, offset;
694 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200695 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200697 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200698 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200699 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200700
Christopher Faulet67957bd2017-09-27 11:00:59 +0200701 reader = resp;
702 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200703 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200704 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200705
Christopher Faulet67957bd2017-09-27 11:00:59 +0200706 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200707 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200708
709 /* query id */
710 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200711 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200712 dns_p->header.id = reader[0] * 256 + reader[1];
713 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200714
Christopher Faulet67957bd2017-09-27 11:00:59 +0200715 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200716 * First byte contains:
717 * - response flag (1 bit)
718 * - opcode (4 bits)
719 * - authoritative (1 bit)
720 * - truncated (1 bit)
721 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200722 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200723 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200724 return DNS_RESP_INVALID;
725
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200726 flags = reader[0] * 256 + reader[1];
727
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200728 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
729 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200730 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200731 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200732 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200733 return DNS_RESP_ERROR;
734 }
735
Christopher Faulet67957bd2017-09-27 11:00:59 +0200736 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200737 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200738
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200739 /* 2 bytes for question count */
740 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200741 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200742 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200743 /* (for now) we send one query only, so we expect only one in the
744 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200745 if (dns_p->header.qdcount != 1)
746 return DNS_RESP_QUERY_COUNT_ERROR;
747 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200748 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200749 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200750
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200751 /* 2 bytes for answer count */
752 if (reader + 2 >= bufend)
753 return DNS_RESP_INVALID;
754 dns_p->header.ancount = reader[0] * 256 + reader[1];
755 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200756 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200757 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200758 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200759 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200760 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200761
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200762 /* 2 bytes authority count */
763 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200764 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200765 dns_p->header.nscount = reader[0] * 256 + reader[1];
766 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200768 /* 2 bytes additional count */
769 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200770 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200771 dns_p->header.arcount = reader[0] * 256 + reader[1];
772 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200773
Christopher Faulet67957bd2017-09-27 11:00:59 +0200774 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200775 LIST_INIT(&dns_p->query_list);
776 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 +0200777 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200778 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200779 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200780 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
781 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200782 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200783 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200784
Christopher Faulet67957bd2017-09-27 11:00:59 +0200785 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200786 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200787 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200788 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100789 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200790
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200791 if (len == 0)
792 return DNS_RESP_INVALID;
793
794 reader += offset;
795 previous_dname = dns_query->name;
796
797 /* move forward 2 bytes for question type */
798 if (reader + 2 >= bufend)
799 return DNS_RESP_INVALID;
800 dns_query->type = reader[0] * 256 + reader[1];
801 reader += 2;
802
803 /* move forward 2 bytes for question class */
804 if (reader + 2 >= bufend)
805 return DNS_RESP_INVALID;
806 dns_query->class = reader[0] * 256 + reader[1];
807 reader += 2;
808 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200809
Baptiste Assmann251abb92017-08-11 09:58:27 +0200810 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200811 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200812 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
813 return DNS_RESP_TRUNCATED;
814
Baptiste Assmann325137d2015-04-13 23:40:55 +0200815 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200816 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200817 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200818 if (reader >= bufend)
819 return DNS_RESP_INVALID;
820
Willy Tarreaubafbe012017-11-24 17:34:44 +0100821 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200822 if (dns_answer_record == NULL)
823 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200824
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200825 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100826 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200827
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200828 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100829 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200830 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200831 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200832
Christopher Faulet67957bd2017-09-27 11:00:59 +0200833 /* Check if the current record dname is valid. previous_dname
834 * points either to queried dname or last CNAME target */
Olivier Houchardb17b8842020-04-01 18:30:27 +0200835 if (dns_query->type != DNS_RTYPE_SRV && dns_hostname_cmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100836 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200837 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200838 /* First record, means a mismatch issue between
839 * queried dname and dname found in the first
840 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200841 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200842 }
843 else {
844 /* If not the first record, this means we have a
845 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200846 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200847 }
848
Baptiste Assmann325137d2015-04-13 23:40:55 +0200849 }
850
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200851 memcpy(dns_answer_record->name, tmpname, len);
852 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200853
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200854 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200855 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100856 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200857 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200858 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200859
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200860 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200861 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100862 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200863 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200864 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200865 dns_answer_record->type = reader[0] * 256 + reader[1];
866 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200867
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200868 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200869 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100870 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200871 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200872 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200873 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200874 reader += 2;
875
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200876 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200877 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100878 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200879 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200880 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200881 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
882 + reader[2] * 256 + reader[3];
883 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200884
Christopher Faulet67957bd2017-09-27 11:00:59 +0200885 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200886 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100887 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200888 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200889 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200890 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200891
Christopher Faulet67957bd2017-09-27 11:00:59 +0200892 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200893 reader += 2;
894
Jérôme Magnin8d4e7dc2018-12-20 16:47:31 +0100895 if (reader + dns_answer_record->data_len > bufend) {
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100896 pool_free(dns_answer_item_pool, dns_answer_record);
897 return DNS_RESP_INVALID;
898 }
899
Christopher Faulet67957bd2017-09-27 11:00:59 +0200900 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200901 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200902 case DNS_RTYPE_A:
903 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200904 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100905 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200906 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200907 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200908 dns_answer_record->address.sa_family = AF_INET;
909 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
910 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200911 break;
912
913 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200914 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200915 * no IP could be found and last record was a CNAME. Could be triggered
916 * by a wrong query type
917 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200918 * + 1 because dns_answer_record_id starts at 0
919 * while number of answers is an integer and
920 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200921 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200922 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100923 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200924 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200925 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200926
927 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100928 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200929 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100930 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200931 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200932 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200933
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200934 memcpy(dns_answer_record->target, tmpname, len);
935 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200936 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200937 break;
938
Olivier Houchard8da5f982017-08-04 18:35:36 +0200939
940 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200941 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200942 * - 2 bytes for the priority
943 * - 2 bytes for the weight
944 * - 2 bytes for the port
945 * - the target hostname
946 */
947 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100948 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200949 return DNS_RESP_INVALID;
950 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200951 dns_answer_record->priority = 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->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200954 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200955 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200956 reader += sizeof(uint16_t);
957 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100958 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200959 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100960 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200961 return DNS_RESP_INVALID;
962 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200963 dns_answer_record->data_len = len;
964 memcpy(dns_answer_record->target, tmpname, len);
965 dns_answer_record->target[len] = 0;
966 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200967
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968 case DNS_RTYPE_AAAA:
969 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200970 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100971 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200972 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200973 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200974 dns_answer_record->address.sa_family = AF_INET6;
975 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
976 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200977 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200978
Baptiste Assmann325137d2015-04-13 23:40:55 +0200979 } /* switch (record type) */
980
Christopher Faulet67957bd2017-09-27 11:00:59 +0200981 /* Increment the counter for number of records saved into our
982 * local response */
983 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200984
Christopher Faulet67957bd2017-09-27 11:00:59 +0200985 /* Move forward dns_answer_record->data_len for analyzing next
986 * record in the response */
987 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
988 ? offset
989 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200990
991 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200992 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200993 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
994 if (tmp_record->type != dns_answer_record->type)
995 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200996
997 switch(tmp_record->type) {
998 case DNS_RTYPE_A:
999 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1000 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1001 sizeof(in_addr_t)))
1002 found = 1;
1003 break;
1004
1005 case DNS_RTYPE_AAAA:
1006 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1007 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1008 sizeof(struct in6_addr)))
1009 found = 1;
1010 break;
1011
Olivier Houchard8da5f982017-08-04 18:35:36 +02001012 case DNS_RTYPE_SRV:
1013 if (dns_answer_record->data_len == tmp_record->data_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001014 !dns_hostname_cmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001015 dns_answer_record->port == tmp_record->port) {
1016 tmp_record->weight = dns_answer_record->weight;
1017 found = 1;
1018 }
1019 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001020
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001021 default:
1022 break;
1023 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001024
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001025 if (found == 1)
1026 break;
1027 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001028
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001029 if (found == 1) {
1030 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001031 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001032 }
1033 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001034 dns_answer_record->last_seen = now.tv_sec;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001035 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001036 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1037 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001038 } /* for i 0 to ancount */
1039
Christopher Faulet67957bd2017-09-27 11:00:59 +02001040 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001041 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001042
Baptiste Assmann37950c82020-02-19 01:08:51 +01001043 /* now parsing additional records for SRV queries only */
1044 if (dns_query->type != DNS_RTYPE_SRV)
1045 goto skip_parsing_additional_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001046 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001047 for (i = 0; i < dns_p->header.arcount; i++) {
1048 if (reader >= bufend)
1049 return DNS_RESP_INVALID;
1050
1051 dns_answer_record = pool_alloc(dns_answer_item_pool);
1052 if (dns_answer_record == NULL)
1053 return (DNS_RESP_INVALID);
1054
1055 offset = 0;
1056 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1057
1058 if (len == 0) {
1059 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann37950c82020-02-19 01:08:51 +01001060 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001061 }
1062
1063 memcpy(dns_answer_record->name, tmpname, len);
1064 dns_answer_record->name[len] = 0;
1065
1066 reader += offset;
1067 if (reader >= bufend) {
1068 pool_free(dns_answer_item_pool, dns_answer_record);
1069 return DNS_RESP_INVALID;
1070 }
1071
1072 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
1073 if (reader + 2 > bufend) {
1074 pool_free(dns_answer_item_pool, dns_answer_record);
1075 return DNS_RESP_INVALID;
1076 }
1077 dns_answer_record->type = reader[0] * 256 + reader[1];
1078 reader += 2;
1079
1080 /* 2 bytes for class (2) */
1081 if (reader + 2 > bufend) {
1082 pool_free(dns_answer_item_pool, dns_answer_record);
1083 return DNS_RESP_INVALID;
1084 }
1085 dns_answer_record->class = reader[0] * 256 + reader[1];
1086 reader += 2;
1087
1088 /* 4 bytes for ttl (4) */
1089 if (reader + 4 > bufend) {
1090 pool_free(dns_answer_item_pool, dns_answer_record);
1091 return DNS_RESP_INVALID;
1092 }
1093 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1094 + reader[2] * 256 + reader[3];
1095 reader += 4;
1096
1097 /* Now reading data len */
1098 if (reader + 2 > bufend) {
1099 pool_free(dns_answer_item_pool, dns_answer_record);
1100 return DNS_RESP_INVALID;
1101 }
1102 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1103
1104 /* Move forward 2 bytes for data len */
1105 reader += 2;
1106
1107 if (reader + dns_answer_record->data_len > bufend) {
1108 pool_free(dns_answer_item_pool, dns_answer_record);
1109 return DNS_RESP_INVALID;
1110 }
1111
1112 /* Analyzing record content */
1113 switch (dns_answer_record->type) {
1114 case DNS_RTYPE_A:
1115 /* ipv4 is stored on 4 bytes */
1116 if (dns_answer_record->data_len != 4) {
1117 pool_free(dns_answer_item_pool, dns_answer_record);
1118 return DNS_RESP_INVALID;
1119 }
1120 dns_answer_record->address.sa_family = AF_INET;
1121 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1122 reader, dns_answer_record->data_len);
1123 break;
1124
1125 case DNS_RTYPE_AAAA:
1126 /* ipv6 is stored on 16 bytes */
1127 if (dns_answer_record->data_len != 16) {
1128 pool_free(dns_answer_item_pool, dns_answer_record);
1129 return DNS_RESP_INVALID;
1130 }
1131 dns_answer_record->address.sa_family = AF_INET6;
1132 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1133 reader, dns_answer_record->data_len);
1134 break;
1135
1136 default:
1137 pool_free(dns_answer_item_pool, dns_answer_record);
1138 continue;
1139
1140 } /* switch (record type) */
1141
1142 /* Increment the counter for number of records saved into our
1143 * local response */
1144 nb_saved_records++;
1145
1146 /* Move forward dns_answer_record->data_len for analyzing next
1147 * record in the response */
1148 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1149 ? offset
1150 : dns_answer_record->data_len);
1151
1152 /* Lookup to see if we already had this entry */
1153 found = 0;
1154 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1155 if (tmp_record->type != dns_answer_record->type)
1156 continue;
1157
1158 switch(tmp_record->type) {
1159 case DNS_RTYPE_A:
1160 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1161 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1162 sizeof(in_addr_t)))
1163 found = 1;
1164 break;
1165
1166 case DNS_RTYPE_AAAA:
1167 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1168 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1169 sizeof(struct in6_addr)))
1170 found = 1;
1171 break;
1172
1173 default:
1174 break;
1175 }
1176
1177 if (found == 1)
1178 break;
1179 }
1180
1181 if (found == 1) {
1182 tmp_record->last_seen = now.tv_sec;
1183 pool_free(dns_answer_item_pool, dns_answer_record);
1184 }
1185 else {
1186 dns_answer_record->last_seen = now.tv_sec;
1187 dns_answer_record->ar_item = NULL;
1188
1189 // looking for the SRV record in the response list linked to this additional record
1190 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1191 if ( !(
1192 (tmp_record->type == DNS_RTYPE_SRV) &&
1193 (tmp_record->ar_item == NULL) &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001194 (dns_hostname_cmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len) == 0)
Baptiste Assmann13a92322019-06-07 09:40:55 +02001195 )
1196 )
1197 continue;
1198 tmp_record->ar_item = dns_answer_record;
1199 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001200
1201 LIST_ADDQ(&dns_p->ar_list, &dns_answer_record->list);
1202 }
1203 } /* for i 0 to arcount */
1204
Baptiste Assmann37950c82020-02-19 01:08:51 +01001205 skip_parsing_additional_records:
1206
Baptiste Assmann13a92322019-06-07 09:40:55 +02001207 /* Save the number of records we really own */
1208 dns_p->header.arcount = nb_saved_records;
1209
Christopher Faulet67957bd2017-09-27 11:00:59 +02001210 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001211 return DNS_RESP_VALID;
1212}
1213
Christopher Faulet67957bd2017-09-27 11:00:59 +02001214/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001215 * If existing IP not found, return the first IP matching family_priority,
1216 * otherwise, first ip found
1217 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001218 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001219 * For both cases above, dns_validate_dns_response is required
1220 * returns one of the DNS_UPD_* code
1221 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001222int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001223 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001224 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001225 void **newip, short *newip_sin_family,
1226 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001227{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001228 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001229 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001230 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001231 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001232 int currentip_sel;
1233 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001234 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001235 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001236
Christopher Faulet67957bd2017-09-27 11:00:59 +02001237 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001238 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001239 *newip = newip4 = newip6 = NULL;
1240 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001241 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001242 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001243
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001244 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001245 * Top priority is the preferred network ip version,
1246 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001247 * the last priority is the currently used IP,
1248 *
1249 * For these three priorities, a score is calculated. The
1250 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001251 * 8 - preferred ip version.
1252 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001253 * 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 +01001254 * 1 - current ip.
1255 * The result with the biggest score is returned.
1256 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001257
1258 list_for_each_entry(record, &dns_p->answer_list, list) {
1259 void *ip;
1260 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001261
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001262 if (record->type == DNS_RTYPE_A) {
1263 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1264 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001265 }
1266 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001267 ip_type = AF_INET6;
1268 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001269 }
1270 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001271 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001272 score = 0;
1273
Joseph Herlant42cf6392018-11-15 10:33:28 -08001274 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001275 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001276 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001277
Joseph Herlant42cf6392018-11-15 10:33:28 -08001278 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001279 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001280
1281 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001282 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001283 continue;
1284
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001285 if ((ip_type == AF_INET &&
1286 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001287 &dns_opts->pref_net[j].mask.in4,
1288 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001289 (ip_type == AF_INET6 &&
1290 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001291 &dns_opts->pref_net[j].mask.in6,
1292 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001293 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001294 break;
1295 }
1296 }
1297
Christopher Faulet67957bd2017-09-27 11:00:59 +02001298 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001299 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001300 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001301 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001302 if (!allowed_duplicated_ip) {
1303 continue;
1304 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001305 } else {
1306 score += 2;
1307 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001308
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001309 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001310 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001311 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001312 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001313 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001314 !memcmp(ip, currentip, 16)))) {
1315 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001316 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001317 }
1318 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001319 currentip_sel = 0;
1320
1321 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001322 * score. The maximum score is 15, if this value is reached, we
1323 * break the parsing. Implicitly, this score is reached the ip
1324 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001325 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001326 if (ip_type == AF_INET)
1327 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001328 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001329 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001330 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001331 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001332 return DNS_UPD_NO;
1333 max_score = score;
1334 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001335 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001336
Christopher Faulet67957bd2017-09-27 11:00:59 +02001337 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001338 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001339 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001340
Christopher Faulet67957bd2017-09-27 11:00:59 +02001341 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001342 if (family_priority == AF_INET) {
1343 if (newip4) {
1344 *newip = newip4;
1345 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001346 }
1347 else if (newip6) {
1348 *newip = newip6;
1349 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001350 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001351 if (!currentip_found)
1352 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001353 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001354 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001355 else if (family_priority == AF_INET6) {
1356 if (newip6) {
1357 *newip = newip6;
1358 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001359 }
1360 else if (newip4) {
1361 *newip = newip4;
1362 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001363 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001364 if (!currentip_found)
1365 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001366 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001367 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001368 else if (family_priority == AF_UNSPEC) {
1369 if (newip6) {
1370 *newip = newip6;
1371 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001372 }
1373 else if (newip4) {
1374 *newip = newip4;
1375 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001376 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001377 if (!currentip_found)
1378 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001379 }
1380
Christopher Faulet67957bd2017-09-27 11:00:59 +02001381 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001382 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001383
Christopher Faulet67957bd2017-09-27 11:00:59 +02001384 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001385 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001386 /* Move the first record to the end of the list, for internal
1387 * round robin */
1388 LIST_DEL(&record->list);
1389 LIST_ADDQ(&dns_p->answer_list, &record->list);
1390 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001391 }
1392 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001393}
1394
Christopher Faulet67957bd2017-09-27 11:00:59 +02001395/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001396 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001397 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1398 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001399 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001400 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1401 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001402 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001403int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001404{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001405 char *ptr;
1406 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001407
Christopher Faulet67957bd2017-09-27 11:00:59 +02001408 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001409 return -1;
1410
Christopher Faulet67957bd2017-09-27 11:00:59 +02001411 ptr = str;
1412 for (i = 0; i < dn_len-1; ++i) {
1413 sz = dn[i];
1414 if (i)
1415 *ptr++ = '.';
1416 memcpy(ptr, dn+i+1, sz);
1417 ptr += sz;
1418 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001419 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001420 *ptr++ = '\0';
1421 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001422}
1423
Christopher Faulet67957bd2017-09-27 11:00:59 +02001424/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1425 *
1426 * <str> must be a null-terminated string. <str_len> must include the
1427 * terminating null byte. <dn> buffer must be allocated and its size must be
1428 * passed in <dn_len>.
1429 *
1430 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1431 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001432 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001433int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001434{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001435 int i, offset;
1436
Christopher Faulet67957bd2017-09-27 11:00:59 +02001437 if (dn_len < str_len + 1)
1438 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001439
Christopher Faulet67957bd2017-09-27 11:00:59 +02001440 /* First byte of dn will be used to store the length of the first
1441 * label */
1442 offset = 0;
1443 for (i = 0; i < str_len; ++i) {
1444 if (str[i] == '.') {
1445 /* 2 or more consecutive dots is invalid */
1446 if (i == offset)
1447 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001448
Lukas Tribus81725b82020-02-27 15:47:24 +01001449 /* ignore trailing dot */
1450 if (i + 2 == str_len) {
1451 i++;
1452 break;
1453 }
1454
Christopher Faulet67957bd2017-09-27 11:00:59 +02001455 dn[offset] = (i - offset);
1456 offset = i+1;
1457 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001458 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001459 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001460 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001461 dn[offset] = (i - offset - 1);
1462 dn[i] = '\0';
1463 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001464}
1465
Christopher Faulet67957bd2017-09-27 11:00:59 +02001466/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001467 * - total size
1468 * - each label size individually
1469 * returns:
1470 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1471 * 1 when no error. <err> is left unaffected.
1472 */
1473int dns_hostname_validation(const char *string, char **err)
1474{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001475 int i;
1476
1477 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1478 if (err)
1479 *err = DNS_TOO_LONG_FQDN;
1480 return 0;
1481 }
1482
William Dauchyaecd5dc2020-01-26 19:52:34 +01001483 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001484 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001485 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1486 if (!(*string == '-' || *string == '_' ||
1487 (*string >= 'a' && *string <= 'z') ||
1488 (*string >= 'A' && *string <= 'Z') ||
1489 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001490 if (err)
1491 *err = DNS_INVALID_CHARACTER;
1492 return 0;
1493 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001494 i++;
1495 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001496 }
1497
William Dauchyaecd5dc2020-01-26 19:52:34 +01001498 if (!(*string))
1499 break;
1500
1501 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001502 if (err)
1503 *err = DNS_LABEL_TOO_LONG;
1504 return 0;
1505 }
1506
William Dauchyaecd5dc2020-01-26 19:52:34 +01001507 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001508 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001509 return 1;
1510}
1511
Christopher Faulet67957bd2017-09-27 11:00:59 +02001512/* Picks up an available resolution from the different resolution list
1513 * associated to a resolvers section, in this order:
1514 * 1. check in resolutions.curr for the same hostname and query_type
1515 * 2. check in resolutions.wait for the same hostname and query_type
1516 * 3. Get a new resolution from resolution pool
1517 *
1518 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001519 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1521 char **hostname_dn, int hostname_dn_len,
1522 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001523{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001524 struct dns_resolution *res;
1525
1526 if (!*hostname_dn)
1527 goto from_pool;
1528
1529 /* Search for same hostname and query type in resolutions.curr */
1530 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1531 if (!res->hostname_dn)
1532 continue;
1533 if ((query_type == res->prefered_query_type) &&
1534 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001535 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001536 return res;
1537 }
1538
1539 /* Search for same hostname and query type in resolutions.wait */
1540 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1541 if (!res->hostname_dn)
1542 continue;
1543 if ((query_type == res->prefered_query_type) &&
1544 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001545 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 return res;
1547 }
1548
1549 from_pool:
1550 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001551 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001552 if (res) {
1553 memset(res, 0, sizeof(*res));
1554 res->resolvers = resolvers;
1555 res->uuid = resolution_uuid;
1556 res->status = RSLV_STATUS_NONE;
1557 res->step = RSLV_STEP_NONE;
1558 res->last_valid = now_ms;
1559
1560 LIST_INIT(&res->requesters);
1561 LIST_INIT(&res->response.answer_list);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001562 LIST_INIT(&res->response.ar_list);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001563
1564 res->prefered_query_type = query_type;
1565 res->query_type = query_type;
1566 res->hostname_dn = *hostname_dn;
1567 res->hostname_dn_len = hostname_dn_len;
1568
1569 ++resolution_uuid;
1570
1571 /* Move the resolution to the resolvers wait queue */
1572 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1573 }
1574 return res;
1575}
1576
1577/* Releases a resolution from its requester(s) and move it back to the pool */
1578static void dns_free_resolution(struct dns_resolution *resolution)
1579{
1580 struct dns_requester *req, *reqback;
1581
1582 /* clean up configuration */
1583 dns_reset_resolution(resolution);
1584 resolution->hostname_dn = NULL;
1585 resolution->hostname_dn_len = 0;
1586
1587 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1588 LIST_DEL(&req->list);
1589 req->resolution = NULL;
1590 }
1591
1592 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001593 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001594}
1595
1596/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1597 * on success, -1 otherwise.
1598 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001599int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001600{
1601 struct dns_resolution *res = NULL;
1602 struct dns_requester *req;
1603 struct dns_resolvers *resolvers;
1604 struct server *srv = NULL;
1605 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001606 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001607 char **hostname_dn;
1608 int hostname_dn_len, query_type;
1609
1610 switch (requester_type) {
1611 case OBJ_TYPE_SERVER:
1612 srv = (struct server *)requester;
1613 hostname_dn = &srv->hostname_dn;
1614 hostname_dn_len = srv->hostname_dn_len;
1615 resolvers = srv->resolvers;
1616 query_type = ((srv->dns_opts.family_prio == AF_INET)
1617 ? DNS_RTYPE_A
1618 : DNS_RTYPE_AAAA);
1619 break;
1620
1621 case OBJ_TYPE_SRVRQ:
1622 srvrq = (struct dns_srvrq *)requester;
1623 hostname_dn = &srvrq->hostname_dn;
1624 hostname_dn_len = srvrq->hostname_dn_len;
1625 resolvers = srvrq->resolvers;
1626 query_type = DNS_RTYPE_SRV;
1627 break;
1628
Baptiste Assmann333939c2019-01-21 08:34:50 +01001629 case OBJ_TYPE_STREAM:
1630 stream = (struct stream *)requester;
1631 hostname_dn = &stream->dns_ctx.hostname_dn;
1632 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1633 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001634 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001635 ? DNS_RTYPE_A
1636 : DNS_RTYPE_AAAA);
1637 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001638 default:
1639 goto err;
1640 }
1641
1642 /* Get a resolution from the resolvers' wait queue or pool */
1643 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1644 goto err;
1645
1646 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001647 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001648 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001649 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001650 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001651 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001652 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001653 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001654 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001655 req->owner = &srv->obj_type;
1656 srv->dns_requester = req;
1657 }
1658 else
1659 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001660 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001661 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001662
1663 req->requester_cb = snr_resolution_cb;
1664 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001665 }
1666 else if (srvrq) {
1667 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001668 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001669 goto err;
1670 req->owner = &srvrq->obj_type;
1671 srvrq->dns_requester = req;
1672 }
1673 else
1674 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001675
1676 req->requester_cb = snr_resolution_cb;
1677 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001678 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001679 else if (stream) {
1680 if (stream->dns_ctx.dns_requester == NULL) {
1681 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1682 goto err;
1683 req->owner = &stream->obj_type;
1684 stream->dns_ctx.dns_requester = req;
1685 }
1686 else
1687 req = stream->dns_ctx.dns_requester;
1688
1689 req->requester_cb = act_resolution_cb;
1690 req->requester_error_cb = act_resolution_error_cb;
1691 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 else
1693 goto err;
1694
1695 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001696
1697 LIST_ADDQ(&res->requesters, &req->list);
1698 return 0;
1699
1700 err:
1701 if (res && LIST_ISEMPTY(&res->requesters))
1702 dns_free_resolution(res);
1703 return -1;
1704}
1705
1706/* Removes a requester from a DNS resoltion. It takes takes care of all the
1707 * consequences. It also cleans up some parameters from the requester.
1708 */
1709void dns_unlink_resolution(struct dns_requester *requester)
1710{
1711 struct dns_resolution *res;
1712 struct dns_requester *req;
1713
1714 /* Nothing to do */
1715 if (!requester || !requester->resolution)
1716 return;
1717 res = requester->resolution;
1718
1719 /* Clean up the requester */
1720 LIST_DEL(&requester->list);
1721 requester->resolution = NULL;
1722
1723 /* We need to find another requester linked on this resolution */
1724 if (!LIST_ISEMPTY(&res->requesters))
1725 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1726 else {
1727 dns_free_resolution(res);
1728 return;
1729 }
1730
1731 /* Move hostname_dn related pointers to the next requester */
1732 switch (obj_type(req->owner)) {
1733 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001734 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1735 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001736 break;
1737 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001738 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1739 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001740 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001741 case OBJ_TYPE_STREAM:
1742 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1743 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1744 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001745 default:
1746 res->hostname_dn = NULL;
1747 res->hostname_dn_len = 0;
1748 break;
1749 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001750}
1751
Christopher Faulet67957bd2017-09-27 11:00:59 +02001752/* Called when a network IO is generated on a name server socket for an incoming
1753 * packet. It performs the following actions:
1754 * - check if the packet requires processing (not outdated resolution)
1755 * - ensure the DNS packet received is valid and call requester's callback
1756 * - call requester's error callback if invalid response
1757 * - check the dn_name in the packet against the one sent
1758 */
1759static void dns_resolve_recv(struct dgram_conn *dgram)
1760{
1761 struct dns_nameserver *ns, *tmpns;
1762 struct dns_resolvers *resolvers;
1763 struct dns_resolution *res;
1764 struct dns_query_item *query;
1765 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1766 unsigned char *bufend;
1767 int fd, buflen, dns_resp;
1768 int max_answer_records;
1769 unsigned short query_id;
1770 struct eb32_node *eb;
1771 struct dns_requester *req;
1772
1773 fd = dgram->t.sock.fd;
1774
1775 /* check if ready for reading */
1776 if (!fd_recv_ready(fd))
1777 return;
1778
1779 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001780 if ((ns = dgram->owner) == NULL) {
1781 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1782 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001783 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001784 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001785
1786 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001787 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001788
1789 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001790 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001791 /* read message received */
1792 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1793 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001794 /* FIXME : for now we consider EAGAIN only, but at
1795 * least we purge sticky errors that would cause us to
1796 * be called in loops.
1797 */
1798 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001799 fd_cant_recv(fd);
1800 break;
1801 }
1802
1803 /* message too big */
1804 if (buflen > resolvers->accepted_payload_size) {
1805 ns->counters.too_big++;
1806 continue;
1807 }
1808
1809 /* initializing variables */
1810 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1811
1812 /* read the query id from the packet (16 bits) */
1813 if (buf + 2 > bufend) {
1814 ns->counters.invalid++;
1815 continue;
1816 }
1817 query_id = dns_response_get_query_id(buf);
1818
1819 /* search the query_id in the pending resolution tree */
1820 eb = eb32_lookup(&resolvers->query_ids, query_id);
1821 if (eb == NULL) {
1822 /* unknown query id means an outdated response and can be safely ignored */
1823 ns->counters.outdated++;
1824 continue;
1825 }
1826
William Dauchybe8a3872019-11-27 23:32:41 +01001827 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001828 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001829 /* number of responses received */
1830 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001831
Christopher Faulet67957bd2017-09-27 11:00:59 +02001832 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1833 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001834
Christopher Faulet67957bd2017-09-27 11:00:59 +02001835 switch (dns_resp) {
1836 case DNS_RESP_VALID:
1837 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001838
Christopher Faulet67957bd2017-09-27 11:00:59 +02001839 case DNS_RESP_INVALID:
1840 case DNS_RESP_QUERY_COUNT_ERROR:
1841 case DNS_RESP_WRONG_NAME:
1842 res->status = RSLV_STATUS_INVALID;
1843 ns->counters.invalid++;
1844 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001845
Christopher Faulet67957bd2017-09-27 11:00:59 +02001846 case DNS_RESP_NX_DOMAIN:
1847 res->status = RSLV_STATUS_NX;
1848 ns->counters.nx++;
1849 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001850
Christopher Faulet67957bd2017-09-27 11:00:59 +02001851 case DNS_RESP_REFUSED:
1852 res->status = RSLV_STATUS_REFUSED;
1853 ns->counters.refused++;
1854 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001855
Christopher Faulet67957bd2017-09-27 11:00:59 +02001856 case DNS_RESP_ANCOUNT_ZERO:
1857 res->status = RSLV_STATUS_OTHER;
1858 ns->counters.any_err++;
1859 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001860
Christopher Faulet67957bd2017-09-27 11:00:59 +02001861 case DNS_RESP_CNAME_ERROR:
1862 res->status = RSLV_STATUS_OTHER;
1863 ns->counters.cname_error++;
1864 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001865
Christopher Faulet67957bd2017-09-27 11:00:59 +02001866 case DNS_RESP_TRUNCATED:
1867 res->status = RSLV_STATUS_OTHER;
1868 ns->counters.truncated++;
1869 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001870
Christopher Faulet67957bd2017-09-27 11:00:59 +02001871 case DNS_RESP_NO_EXPECTED_RECORD:
1872 case DNS_RESP_ERROR:
1873 case DNS_RESP_INTERNAL:
1874 res->status = RSLV_STATUS_OTHER;
1875 ns->counters.other++;
1876 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001877 }
1878
Christopher Faulet67957bd2017-09-27 11:00:59 +02001879 /* Wait all nameservers response to handle errors */
1880 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1881 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001882
Christopher Faulet67957bd2017-09-27 11:00:59 +02001883 /* Process error codes */
1884 if (dns_resp != DNS_RESP_VALID) {
1885 if (res->prefered_query_type != res->query_type) {
1886 /* The fallback on the query type was already performed,
1887 * so check the try counter. If it falls to 0, we can
1888 * report an error. Else, wait the next attempt. */
1889 if (!res->try)
1890 goto report_res_error;
1891 }
1892 else {
1893 /* Fallback from A to AAAA or the opposite and re-send
1894 * the resolution immediately. try counter is not
1895 * decremented. */
1896 if (res->prefered_query_type == DNS_RTYPE_A) {
1897 res->query_type = DNS_RTYPE_AAAA;
1898 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001899 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001900 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1901 res->query_type = DNS_RTYPE_A;
1902 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001903 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001904 }
1905 continue;
1906 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001907
Christopher Faulet67957bd2017-09-27 11:00:59 +02001908 /* Now let's check the query's dname corresponds to the one we
1909 * sent. We can check only the first query of the list. We send
1910 * one query at a time so we get one query in the response */
1911 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
Olivier Houchardb17b8842020-04-01 18:30:27 +02001912 if (query && dns_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001913 dns_resp = DNS_RESP_WRONG_NAME;
1914 ns->counters.other++;
1915 goto report_res_error;
1916 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001917
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 /* So the resolution succeeded */
1919 res->status = RSLV_STATUS_VALID;
1920 res->last_valid = now_ms;
1921 ns->counters.valid++;
1922 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001923
Christopher Faulet67957bd2017-09-27 11:00:59 +02001924 report_res_error:
1925 list_for_each_entry(req, &res->requesters, list)
1926 req->requester_error_cb(req, dns_resp);
1927 dns_reset_resolution(res);
1928 LIST_DEL(&res->list);
1929 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1930 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001931
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 report_res_success:
1933 /* Only the 1rst requester s managed by the server, others are
1934 * from the cache */
1935 tmpns = ns;
1936 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001937 struct server *s = objt_server(req->owner);
1938
1939 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001940 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001941 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001942 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001943 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001944 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001945 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001946
Christopher Faulet67957bd2017-09-27 11:00:59 +02001947 dns_reset_resolution(res);
1948 LIST_DEL(&res->list);
1949 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1950 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001951 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001952 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001953 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001954}
William Lallemand69e96442016-11-19 00:58:54 +01001955
Christopher Faulet67957bd2017-09-27 11:00:59 +02001956/* Called when a resolvers network socket is ready to send data */
1957static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001958{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001959 struct dns_resolvers *resolvers;
1960 struct dns_nameserver *ns;
1961 struct dns_resolution *res;
1962 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001963
Christopher Faulet67957bd2017-09-27 11:00:59 +02001964 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001965
Christopher Faulet67957bd2017-09-27 11:00:59 +02001966 /* check if ready for sending */
1967 if (!fd_send_ready(fd))
1968 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001969
Christopher Faulet67957bd2017-09-27 11:00:59 +02001970 /* we don't want/need to be waked up any more for sending */
1971 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001972
Christopher Faulet67957bd2017-09-27 11:00:59 +02001973 /* no need to go further if we can't retrieve the nameserver */
1974 if ((ns = dgram->owner) == NULL)
1975 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001976
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001978 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001979
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001981 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001982
Christopher Faulet67957bd2017-09-27 11:00:59 +02001983 if (res->nb_queries == resolvers->nb_nameservers)
1984 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001985
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001986 len = dns_build_query(res->query_id, res->query_type,
1987 resolvers->accepted_payload_size,
1988 res->hostname_dn, res->hostname_dn_len,
1989 trash.area, trash.size);
1990 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001991 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001992
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001993 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01001994 if (ret != len) {
1995 if (ret == -1 && errno == EAGAIN) {
1996 /* retry once the socket is ready */
1997 fd_cant_send(fd);
1998 continue;
1999 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002000 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01002001 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002002
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 ns->counters.sent++;
2004 res->nb_queries++;
2005 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002006
Christopher Faulet67957bd2017-09-27 11:00:59 +02002007 snd_error:
2008 ns->counters.snd_error++;
2009 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002010 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002011 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002012}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002013
Christopher Faulet67957bd2017-09-27 11:00:59 +02002014/* Processes DNS resolution. First, it checks the active list to detect expired
2015 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2016 * checks the wait list to trigger new resolutions.
2017 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002018static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002019{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002020 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002021 struct dns_resolution *res, *resback;
2022 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002023
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002024 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002025
Christopher Faulet67957bd2017-09-27 11:00:59 +02002026 /* Handle all expired resolutions from the active list */
2027 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2028 /* When we find the first resolution in the future, then we can
2029 * stop here */
2030 exp = tick_add(res->last_query, resolvers->timeout.retry);
2031 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002032 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002033
Christopher Faulet67957bd2017-09-27 11:00:59 +02002034 /* If current resolution has been tried too many times and
2035 * finishes in timeout we update its status and remove it from
2036 * the list */
2037 if (!res->try) {
2038 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002039
Christopher Faulet67957bd2017-09-27 11:00:59 +02002040 /* Notify the result to the requesters */
2041 if (!res->nb_responses)
2042 res->status = RSLV_STATUS_TIMEOUT;
2043 list_for_each_entry(req, &res->requesters, list)
2044 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002045
Christopher Faulet67957bd2017-09-27 11:00:59 +02002046 /* Clean up resolution info and remove it from the
2047 * current list */
2048 dns_reset_resolution(res);
2049 LIST_DEL(&res->list);
2050 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002051 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002052 else {
2053 /* Otherwise resend the DNS query and requeue the resolution */
2054 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2055 /* No response received (a real timeout) or fallback already done */
2056 res->query_type = res->prefered_query_type;
2057 res->try--;
2058 }
2059 else {
2060 /* Fallback from A to AAAA or the opposite and re-send
2061 * the resolution immediately. try counter is not
2062 * decremented. */
2063 if (res->prefered_query_type == DNS_RTYPE_A)
2064 res->query_type = DNS_RTYPE_AAAA;
2065 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2066 res->query_type = DNS_RTYPE_A;
2067 else
2068 res->try--;
2069 }
2070 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002071 }
2072 }
William Lallemand69e96442016-11-19 00:58:54 +01002073
Christopher Faulet67957bd2017-09-27 11:00:59 +02002074 /* Handle all resolutions in the wait list */
2075 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2076 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2077 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2078 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002079
Christopher Faulet67957bd2017-09-27 11:00:59 +02002080 if (dns_run_resolution(res) != 1) {
2081 res->last_resolution = now_ms;
2082 LIST_DEL(&res->list);
2083 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002084 }
2085 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002086
Christopher Faulet67957bd2017-09-27 11:00:59 +02002087 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002088 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002089 return t;
2090}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002091
Christopher Faulet67957bd2017-09-27 11:00:59 +02002092/* proto_udp callback functions for a DNS resolution */
2093struct dgram_data_cb resolve_dgram_cb = {
2094 .recv = dns_resolve_recv,
2095 .send = dns_resolve_send,
2096};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002097
Christopher Faulet67957bd2017-09-27 11:00:59 +02002098/* Release memory allocated by DNS */
2099static void dns_deinit(void)
2100{
2101 struct dns_resolvers *resolvers, *resolversback;
2102 struct dns_nameserver *ns, *nsback;
2103 struct dns_resolution *res, *resback;
2104 struct dns_requester *req, *reqback;
2105 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002106
Christopher Faulet67957bd2017-09-27 11:00:59 +02002107 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2108 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2109 free(ns->id);
2110 free((char *)ns->conf.file);
2111 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2112 fd_delete(ns->dgram->t.sock.fd);
2113 free(ns->dgram);
2114 LIST_DEL(&ns->list);
2115 free(ns);
2116 }
2117
2118 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2119 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2120 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002121 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002122 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002123 dns_free_resolution(res);
2124 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002125
Christopher Faulet67957bd2017-09-27 11:00:59 +02002126 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2127 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2128 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002129 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002130 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002131 dns_free_resolution(res);
2132 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002133
Christopher Faulet67957bd2017-09-27 11:00:59 +02002134 free(resolvers->id);
2135 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002136 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002137 LIST_DEL(&resolvers->list);
2138 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002139 }
2140
Christopher Faulet67957bd2017-09-27 11:00:59 +02002141 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2142 free(srvrq->name);
2143 free(srvrq->hostname_dn);
2144 LIST_DEL(&srvrq->list);
2145 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002146 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002147}
2148
Christopher Faulet67957bd2017-09-27 11:00:59 +02002149/* Finalizes the DNS configuration by allocating required resources and checking
2150 * live parameters.
2151 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002152 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002153static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002154{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002155 struct dns_resolvers *resolvers;
2156 struct proxy *px;
2157 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002158
Christopher Faulet67957bd2017-09-27 11:00:59 +02002159 /* allocate pool of resolution per resolvers */
2160 list_for_each_entry(resolvers, &dns_resolvers, list) {
2161 struct dns_nameserver *ns;
2162 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002163
Christopher Faulet67957bd2017-09-27 11:00:59 +02002164 /* Check if we can create the socket with nameservers info */
2165 list_for_each_entry(ns, &resolvers->nameservers, list) {
2166 struct dgram_conn *dgram = NULL;
2167 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002168
Christopher Faulet67957bd2017-09-27 11:00:59 +02002169 /* Check nameserver info */
2170 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002171 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2172 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002173 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002174 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002175 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002176 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002177 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2178 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002179 close(fd);
2180 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002181 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002182 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002183 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002184
Christopher Faulet67957bd2017-09-27 11:00:59 +02002185 /* Create dgram structure that will hold the UPD socket
2186 * and attach it on the current nameserver */
2187 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002188 ha_alert("config: resolvers '%s' : out of memory.\n",
2189 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002190 err_code |= (ERR_ALERT|ERR_ABORT);
2191 goto err;
2192 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002193
Christopher Faulet67957bd2017-09-27 11:00:59 +02002194 /* Leave dgram partially initialized, no FD attached for
2195 * now. */
2196 dgram->owner = ns;
2197 dgram->data = &resolve_dgram_cb;
2198 dgram->t.sock.fd = -1;
2199 ns->dgram = dgram;
2200 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002201
Christopher Faulet67957bd2017-09-27 11:00:59 +02002202 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002203 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002204 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002205 err_code |= (ERR_ALERT|ERR_ABORT);
2206 goto err;
2207 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002208
Christopher Faulet67957bd2017-09-27 11:00:59 +02002209 /* Update task's parameters */
2210 t->process = dns_process_resolvers;
2211 t->context = resolvers;
2212 resolvers->t = t;
2213 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002214 }
2215
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002216 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002217 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002218
Christopher Faulet67957bd2017-09-27 11:00:59 +02002219 for (srv = px->srv; srv; srv = srv->next) {
2220 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002221
Christopher Faulet67957bd2017-09-27 11:00:59 +02002222 if (!srv->resolvers_id)
2223 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002224
Christopher Faulet67957bd2017-09-27 11:00:59 +02002225 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002226 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2227 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002228 err_code |= (ERR_ALERT|ERR_ABORT);
2229 continue;
2230 }
2231 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002232
Christopher Faulet67957bd2017-09-27 11:00:59 +02002233 if (srv->srvrq && !srv->srvrq->resolvers) {
2234 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002235 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002236 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2237 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002238 err_code |= (ERR_ALERT|ERR_ABORT);
2239 continue;
2240 }
2241 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002242 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002243 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2244 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002245 err_code |= (ERR_ALERT|ERR_ABORT);
2246 continue;
2247 }
2248 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002249 }
2250
Christopher Faulet67957bd2017-09-27 11:00:59 +02002251 if (err_code & (ERR_ALERT|ERR_ABORT))
2252 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002253
Christopher Faulet67957bd2017-09-27 11:00:59 +02002254 return err_code;
2255 err:
2256 dns_deinit();
2257 return err_code;
2258
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002259}
2260
Christopher Faulet67957bd2017-09-27 11:00:59 +02002261/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002262static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002263{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002264 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002265
Christopher Faulet67957bd2017-09-27 11:00:59 +02002266 if (*args[2]) {
2267 list_for_each_entry(presolvers, &dns_resolvers, list) {
2268 if (strcmp(presolvers->id, args[2]) == 0) {
2269 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002270 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002271 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002272 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002273 if (appctx->ctx.cli.p0 == NULL)
2274 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002275 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002276 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002277}
2278
Christopher Faulet67957bd2017-09-27 11:00:59 +02002279/* Dumps counters from all resolvers section and associated name servers. It
2280 * returns 0 if the output buffer is full and it needs to be called again,
2281 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002282 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002283 */
2284static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2285{
2286 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002287 struct dns_resolvers *resolvers;
2288 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002289
2290 chunk_reset(&trash);
2291
2292 switch (appctx->st2) {
2293 case STAT_ST_INIT:
2294 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2295 /* fall through */
2296
2297 case STAT_ST_LIST:
2298 if (LIST_ISEMPTY(&dns_resolvers)) {
2299 chunk_appendf(&trash, "No resolvers found\n");
2300 }
2301 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002302 list_for_each_entry(resolvers, &dns_resolvers, list) {
2303 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002304 continue;
2305
Christopher Faulet67957bd2017-09-27 11:00:59 +02002306 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2307 list_for_each_entry(ns, &resolvers->nameservers, list) {
2308 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2309 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2310 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2311 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2312 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2313 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2314 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2315 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2316 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2317 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2318 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2319 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2320 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2321 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2322 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2323 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002324 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002325 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002326 }
2327 }
2328
2329 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002330 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002331 /* let's try again later from this session. We add ourselves into
2332 * this session's users so that it can remove us upon termination.
2333 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002334 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002335 return 0;
2336 }
William Lallemand69e96442016-11-19 00:58:54 +01002337 /* fall through */
2338
2339 default:
2340 appctx->st2 = STAT_ST_FIN;
2341 return 1;
2342 }
2343}
2344
2345/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002346static struct cli_kw_list cli_kws = {{ }, {
2347 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2348 " associated name servers",
2349 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2350 {{},}
2351 }
2352};
William Lallemand69e96442016-11-19 00:58:54 +01002353
Willy Tarreau0108d902018-11-25 19:14:37 +01002354INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002355
Baptiste Assmann333939c2019-01-21 08:34:50 +01002356/*
2357 * Prepare <rule> for hostname resolution.
2358 * Returns -1 in case of any allocation failure, 0 if not.
2359 * On error, a global failure counter is also incremented.
2360 */
2361static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2362{
2363 char *hostname_dn;
2364 int hostname_len, hostname_dn_len;
2365 struct buffer *tmp = get_trash_chunk();
2366
2367 if (!hostname)
2368 return 0;
2369
2370 hostname_len = strlen(hostname);
2371 hostname_dn = tmp->area;
2372 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2373 hostname_dn, tmp->size);
2374 if (hostname_dn_len == -1)
2375 goto err;
2376
2377
2378 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2379 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2380 if (!stream->dns_ctx.hostname_dn)
2381 goto err;
2382
2383 return 0;
2384
2385 err:
2386 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2387 dns_failed_resolutions += 1;
2388 return -1;
2389}
2390
2391
2392/*
2393 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2394 */
2395enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2396 struct session *sess, struct stream *s, int flags)
2397{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002398 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002399 struct sample *smp;
2400 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002401 struct dns_requester *req;
2402 struct dns_resolvers *resolvers;
2403 struct dns_resolution *res;
2404 int exp;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002405
2406 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002407 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002408 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2409 resolution = s->dns_ctx.dns_requester->resolution;
Baptiste Assmann4c52e4b2019-10-01 15:32:40 +02002410 if (resolution->step == RSLV_STEP_RUNNING) {
2411 return ACT_RET_YIELD;
2412 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01002413 if (resolution->step == RSLV_STEP_NONE) {
2414 /* We update the variable only if we have a valid response. */
2415 if (resolution->status == RSLV_STATUS_VALID) {
2416 struct sample smp;
2417 short ip_sin_family = 0;
2418 void *ip = NULL;
2419
Christopher Fauleta4168432020-01-24 18:08:42 +01002420 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002421 0, &ip, &ip_sin_family, NULL);
2422
2423 switch (ip_sin_family) {
2424 case AF_INET:
2425 smp.data.type = SMP_T_IPV4;
2426 memcpy(&smp.data.u.ipv4, ip, 4);
2427 break;
2428 case AF_INET6:
2429 smp.data.type = SMP_T_IPV6;
2430 memcpy(&smp.data.u.ipv6, ip, 16);
2431 break;
2432 default:
2433 ip = NULL;
2434 }
2435
2436 if (ip) {
2437 smp.px = px;
2438 smp.sess = sess;
2439 smp.strm = s;
2440
2441 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2442 }
2443 }
2444 }
2445
2446 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
2447 s->dns_ctx.hostname_dn_len = 0;
2448 dns_unlink_resolution(s->dns_ctx.dns_requester);
2449
2450 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2451 s->dns_ctx.dns_requester = NULL;
2452
2453 return ACT_RET_CONT;
2454 }
2455
2456 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002457 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2458 if (smp == NULL)
2459 return ACT_RET_CONT;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002460
Willy Tarreau45726fd2019-07-17 10:38:45 +02002461 fqdn = smp->data.u.str.area;
2462 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet13403762019-12-13 09:01:57 +01002463 return ACT_RET_CONT; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002464
Willy Tarreau45726fd2019-07-17 10:38:45 +02002465 s->dns_ctx.parent = rule;
2466 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002467
2468 /* Check if there is a fresh enough response in the cache of our associated resolution */
2469 req = s->dns_ctx.dns_requester;
2470 if (!req || !req->resolution) {
2471 dns_trigger_resolution(s->dns_ctx.dns_requester);
2472 return ACT_RET_YIELD;
2473 }
2474 res = req->resolution;
2475 resolvers = res->resolvers;
2476
2477 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2478 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2479 && !tick_is_expired(exp, now_ms)) {
2480 goto use_cache;
2481 }
2482
Willy Tarreau45726fd2019-07-17 10:38:45 +02002483 dns_trigger_resolution(s->dns_ctx.dns_requester);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002484 return ACT_RET_YIELD;
2485}
2486
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002487static void release_dns_action(struct act_rule *rule)
2488{
2489 release_sample_expr(rule->arg.dns.expr);
2490 free(rule->arg.dns.varname);
2491 free(rule->arg.dns.resolvers_id);
2492 free(rule->arg.dns.dns_opts);
2493}
2494
Baptiste Assmann333939c2019-01-21 08:34:50 +01002495
2496/* parse "do-resolve" action
2497 * This action takes the following arguments:
2498 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2499 *
2500 * - <varName> is the variable name where the result of the DNS resolution will be stored
2501 * (mandatory)
2502 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2503 * (mandatory)
2504 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2505 * (optional), defaults to ipv6
2506 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2507 */
2508enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2509{
2510 int cur_arg;
2511 struct sample_expr *expr;
2512 unsigned int where;
2513 const char *beg, *end;
2514
2515 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2516 cur_arg = *orig_arg - 1;
2517
2518 /* locate varName, which is mandatory */
2519 beg = strchr(args[cur_arg], '(');
2520 if (beg == NULL)
2521 goto do_resolve_parse_error;
2522 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2523 end = strchr(beg, ',');
2524 if (end == NULL)
2525 goto do_resolve_parse_error;
2526 rule->arg.dns.varname = my_strndup(beg, end - beg);
2527 if (rule->arg.dns.varname == NULL)
2528 goto do_resolve_parse_error;
2529
2530
2531 /* locate resolversSectionName, which is mandatory.
2532 * Since next parameters are optional, the delimiter may be comma ','
2533 * or closing parenthesis ')'
2534 */
2535 beg = end + 1;
2536 end = strchr(beg, ',');
2537 if (end == NULL)
2538 end = strchr(beg, ')');
2539 if (end == NULL)
2540 goto do_resolve_parse_error;
2541 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2542 if (rule->arg.dns.resolvers_id == NULL)
2543 goto do_resolve_parse_error;
2544
2545
Christopher Fauleta4168432020-01-24 18:08:42 +01002546 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2547 if (rule->arg.dns.dns_opts == NULL)
2548 goto do_resolve_parse_error;
2549
Baptiste Assmann333939c2019-01-21 08:34:50 +01002550 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002551 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002552
2553 /* optional arguments accepted for now:
2554 * ipv4 or ipv6
2555 */
2556 while (*end != ')') {
2557 beg = end + 1;
2558 end = strchr(beg, ',');
2559 if (end == NULL)
2560 end = strchr(beg, ')');
2561 if (end == NULL)
2562 goto do_resolve_parse_error;
2563
2564 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002565 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002566 }
2567 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002568 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002569 }
2570 else {
2571 goto do_resolve_parse_error;
2572 }
2573 }
2574
2575 cur_arg = cur_arg + 1;
2576
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002577 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 +01002578 if (!expr)
2579 goto do_resolve_parse_error;
2580
2581
2582 where = 0;
2583 if (px->cap & PR_CAP_FE)
2584 where |= SMP_VAL_FE_HRQ_HDR;
2585 if (px->cap & PR_CAP_BE)
2586 where |= SMP_VAL_BE_HRQ_HDR;
2587
2588 if (!(expr->fetch->val & where)) {
2589 memprintf(err,
2590 "fetch method '%s' extracts information from '%s', none of which is available here",
2591 args[cur_arg-1], sample_src_names(expr->fetch->use));
2592 free(expr);
2593 return ACT_RET_PRS_ERR;
2594 }
2595 rule->arg.dns.expr = expr;
2596 rule->action = ACT_CUSTOM;
2597 rule->action_ptr = dns_action_do_resolve;
2598 *orig_arg = cur_arg;
2599
2600 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002601 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002602
2603 return ACT_RET_PRS_OK;
2604
2605 do_resolve_parse_error:
2606 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2607 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2608 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2609 args[cur_arg]);
2610 return ACT_RET_PRS_ERR;
2611}
2612
2613static struct action_kw_list http_req_kws = { { }, {
2614 { "do-resolve", dns_parse_do_resolve, 1 },
2615 { /* END */ }
2616}};
2617
2618INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2619
2620static struct action_kw_list tcp_req_cont_actions = {ILH, {
2621 { "do-resolve", dns_parse_do_resolve, 1 },
2622 { /* END */ }
2623}};
2624
2625INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2626
2627/* Check an "http-request do-resolve" action.
2628 *
2629 * The function returns 1 in success case, otherwise, it returns 0 and err is
2630 * filled.
2631 */
2632int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2633{
2634 struct dns_resolvers *resolvers = NULL;
2635
2636 if (rule->arg.dns.resolvers_id == NULL) {
2637 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2638 return 0;
2639 }
2640
2641 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2642 if (resolvers == NULL) {
2643 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2644 return 0;
2645 }
2646 rule->arg.dns.resolvers = resolvers;
2647
2648 return 1;
2649}
2650
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002651REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002652REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);