blob: c38152d7f6c3096c9b5ca2744db568b4ba0a320a [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>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020025#include <haproxy/channel.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau7c18b542020-06-11 09:23:02 +020028#include <haproxy/dgram.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020029#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
32#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020033#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020034#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020036#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020037#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020038#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020039#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020041#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020042#include <haproxy/tcp_rules.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020043#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020044#include <haproxy/time.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020045#include <haproxy/vars.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020046
Baptiste Assmann325137d2015-04-13 23:40:55 +020047
Christopher Faulet67957bd2017-09-27 11:00:59 +020048struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
49struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020050
Tim Duesterhusfcac33d2020-01-18 02:04:12 +010051static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010052
53DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
54DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010055DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010056
Christopher Faulet67957bd2017-09-27 11:00:59 +020057static unsigned int resolution_uuid = 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +010058unsigned int dns_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059
Christopher Faulet67957bd2017-09-27 11:00:59 +020060/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
61 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020062 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020063struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020064{
Christopher Faulet67957bd2017-09-27 11:00:59 +020065 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020066
Christopher Faulet67957bd2017-09-27 11:00:59 +020067 list_for_each_entry(res, &dns_resolvers, list) {
68 if (!strcmp(res->id, id))
69 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020070 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020071 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020072}
73
Olivier Houchardb17b8842020-04-01 18:30:27 +020074/* Compare hostnames in a case-insensitive way .
75 * Returns 0 if they are the same, non-zero otherwise
76 */
77static __inline int dns_hostname_cmp(const char *name1, const char *name2, int len)
78{
79 int i;
80
81 for (i = 0; i < len; i++)
Willy Tarreauf278eec2020-07-05 21:46:32 +020082 if (tolower((unsigned char)name1[i]) != tolower((unsigned char)name2[i]))
Olivier Houchardb17b8842020-04-01 18:30:27 +020083 return -1;
84 return 0;
85}
86
Christopher Faulet67957bd2017-09-27 11:00:59 +020087/* Returns a pointer on the SRV request matching the name <name> for the proxy
88 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020089 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020090struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020091{
Christopher Faulet67957bd2017-09-27 11:00:59 +020092 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020093
Christopher Faulet67957bd2017-09-27 11:00:59 +020094 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
95 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
96 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020097 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020098 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020099}
100
Christopher Faulet67957bd2017-09-27 11:00:59 +0200101/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
102 * NULL if an error occurred. */
103struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200104{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200105 struct proxy *px = srv->proxy;
106 struct dns_srvrq *srvrq = NULL;
107 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200108
Christopher Faulet67957bd2017-09-27 11:00:59 +0200109 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200110 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
111 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200112 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100113 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
114 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200116 }
117
Christopher Faulet67957bd2017-09-27 11:00:59 +0200118 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100119 ha_alert("config : %s '%s', server '%s': out of memory\n",
120 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200121 goto err;
122 }
123 srvrq->obj_type = OBJ_TYPE_SRVRQ;
124 srvrq->proxy = px;
125 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200126 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200127 srvrq->hostname_dn_len = hostname_dn_len;
128 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100129 ha_alert("config : %s '%s', server '%s': out of memory\n",
130 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200131 goto err;
132 }
133 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
134 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200135
Christopher Faulet67957bd2017-09-27 11:00:59 +0200136 err:
137 if (srvrq) {
138 free(srvrq->name);
139 free(srvrq->hostname_dn);
140 free(srvrq);
141 }
142 return NULL;
143}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200144
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200145
Christopher Faulet67957bd2017-09-27 11:00:59 +0200146/* 2 bytes random generator to generate DNS query ID */
147static inline uint16_t dns_rnd16(void)
148{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200149 if (!dns_query_id_seed)
150 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151 dns_query_id_seed ^= dns_query_id_seed << 13;
152 dns_query_id_seed ^= dns_query_id_seed >> 7;
153 dns_query_id_seed ^= dns_query_id_seed << 17;
154 return dns_query_id_seed;
155}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200156
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200157
Christopher Faulet67957bd2017-09-27 11:00:59 +0200158static inline int dns_resolution_timeout(struct dns_resolution *res)
159{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100160 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200161}
162
Christopher Faulet67957bd2017-09-27 11:00:59 +0200163/* Updates a resolvers' task timeout for next wake up and queue it */
164static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200165{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200166 struct dns_resolution *res;
167 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 next = tick_add(now_ms, resolvers->timeout.resolve);
170 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
171 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
172 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
173 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200174
Christopher Faulet67957bd2017-09-27 11:00:59 +0200175 list_for_each_entry(res, &resolvers->resolutions.wait, list)
176 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200177
Christopher Faulet67957bd2017-09-27 11:00:59 +0200178 resolvers->t->expire = next;
179 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200180}
181
Christopher Faulet67957bd2017-09-27 11:00:59 +0200182/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
183 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200184 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200185static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200186{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187 struct dgram_conn *dgram = ns->dgram;
188 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200189
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190 /* Already connected */
191 if (dgram->t.sock.fd != -1)
192 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200193
Christopher Faulet67957bd2017-09-27 11:00:59 +0200194 /* Create an UDP socket and connect it on the nameserver's IP/Port */
195 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
196 send_log(NULL, LOG_WARNING,
197 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
198 ns->resolvers->id, ns->id);
199 return -1;
200 }
201 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
202 send_log(NULL, LOG_WARNING,
203 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
204 ns->resolvers->id, ns->id);
205 close(fd);
206 return -1;
207 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200208
Christopher Faulet67957bd2017-09-27 11:00:59 +0200209 /* Make the socket non blocking */
210 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200211
Christopher Faulet67957bd2017-09-27 11:00:59 +0200212 /* Add the fd in the fd list and update its parameters */
213 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100214 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200215 fd_want_recv(fd);
216 return 0;
217}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200218
Christopher Faulet67957bd2017-09-27 11:00:59 +0200219/* Forges a DNS query. It needs the following information from the caller:
220 * - <query_id> : the DNS query id corresponding to this query
221 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
222 * - <hostname_dn> : hostname in domain name format
223 * - <hostname_dn_len> : length of <hostname_dn>
224 *
225 * To store the query, the caller must pass a buffer <buf> and its size
226 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
227 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200228 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200229static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
230 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200231{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200232 struct dns_header dns_hdr;
233 struct dns_question qinfo;
234 struct dns_additional_record edns;
235 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200236
Christopher Faulet67957bd2017-09-27 11:00:59 +0200237 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
238 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200239
Christopher Faulet67957bd2017-09-27 11:00:59 +0200240 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200241
Christopher Faulet67957bd2017-09-27 11:00:59 +0200242 /* Set dns query headers */
243 dns_hdr.id = (unsigned short) htons(query_id);
244 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
245 dns_hdr.qdcount = htons(1); /* 1 question */
246 dns_hdr.ancount = 0;
247 dns_hdr.nscount = 0;
248 dns_hdr.arcount = htons(1);
249 memcpy(p, &dns_hdr, sizeof(dns_hdr));
250 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200251
Christopher Faulet67957bd2017-09-27 11:00:59 +0200252 /* Set up query hostname */
253 memcpy(p, hostname_dn, hostname_dn_len);
254 p += hostname_dn_len;
255 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200256
Christopher Faulet67957bd2017-09-27 11:00:59 +0200257 /* Set up query info (type and class) */
258 qinfo.qtype = htons(query_type);
259 qinfo.qclass = htons(DNS_RCLASS_IN);
260 memcpy(p, &qinfo, sizeof(qinfo));
261 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200262
Christopher Faulet67957bd2017-09-27 11:00:59 +0200263 /* Set the DNS extension */
264 edns.name = 0;
265 edns.type = htons(DNS_RTYPE_OPT);
266 edns.udp_payload_size = htons(accepted_payload_size);
267 edns.extension = 0;
268 edns.data_length = 0;
269 memcpy(p, &edns, sizeof(edns));
270 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200271
Christopher Faulet67957bd2017-09-27 11:00:59 +0200272 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200273}
274
Christopher Faulet67957bd2017-09-27 11:00:59 +0200275/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
276 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200277 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200278static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200279{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200280 struct dns_resolvers *resolvers = resolution->resolvers;
281 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100282 int len;
283
284 /* Update resolution */
285 resolution->nb_queries = 0;
286 resolution->nb_responses = 0;
287 resolution->last_query = now_ms;
288
289 len = dns_build_query(resolution->query_id, resolution->query_type,
290 resolvers->accepted_payload_size,
291 resolution->hostname_dn, resolution->hostname_dn_len,
292 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200293
Christopher Faulet67957bd2017-09-27 11:00:59 +0200294 list_for_each_entry(ns, &resolvers->nameservers, list) {
295 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100296 int ret;
297
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298 if (fd == -1) {
299 if (dns_connect_namesaver(ns) == -1)
300 continue;
301 fd = ns->dgram->t.sock.fd;
302 resolvers->nb_nameservers++;
303 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200304
Willy Tarreau0eae6322019-12-20 11:18:54 +0100305 if (len < 0)
306 goto snd_error;
307
308 ret = send(fd, trash.area, len, 0);
309 if (ret == len) {
310 ns->counters.sent++;
311 resolution->nb_queries++;
312 continue;
313 }
314
315 if (ret == -1 && errno == EAGAIN) {
316 /* retry once the socket is ready */
317 fd_cant_send(fd);
318 continue;
319 }
320
321 snd_error:
322 ns->counters.snd_error++;
323 resolution->nb_queries++;
324 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200325
Christopher Faulet67957bd2017-09-27 11:00:59 +0200326 /* Push the resolution at the end of the active list */
327 LIST_DEL(&resolution->list);
328 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
329 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200330}
331
Christopher Faulet67957bd2017-09-27 11:00:59 +0200332/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
333 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200334 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200335static int
336dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200337{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200338 struct dns_resolvers *resolvers = resolution->resolvers;
339 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200340
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 /* Avoid sending requests for resolutions that don't yet have an
342 * hostname, ie resolutions linked to servers that do not yet have an
343 * fqdn */
344 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200345 return 0;
346
Christopher Faulet67957bd2017-09-27 11:00:59 +0200347 /* Check if a resolution has already been started for this server return
348 * directly to avoid resolution pill up. */
349 if (resolution->step != RSLV_STEP_NONE)
350 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200351
Christopher Faulet67957bd2017-09-27 11:00:59 +0200352 /* Generates a new query id. We try at most 100 times to find a free
353 * query id */
354 for (i = 0; i < 100; ++i) {
355 query_id = dns_rnd16();
356 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200357 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200358 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200359 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200360 if (query_id == -1) {
361 send_log(NULL, LOG_NOTICE,
362 "could not generate a query id for %s, in resolvers %s.\n",
363 resolution->hostname_dn, resolvers->id);
364 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200365 }
366
Christopher Faulet67957bd2017-09-27 11:00:59 +0200367 /* Update resolution parameters */
368 resolution->query_id = query_id;
369 resolution->qid.key = query_id;
370 resolution->step = RSLV_STEP_RUNNING;
371 resolution->query_type = resolution->prefered_query_type;
372 resolution->try = resolvers->resolve_retries;
373 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200374
Christopher Faulet67957bd2017-09-27 11:00:59 +0200375 /* Send the DNS query */
376 resolution->try -= 1;
377 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200378 return 1;
379}
380
Christopher Faulet67957bd2017-09-27 11:00:59 +0200381/* Performs a name resolution for the requester <req> */
382void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200383{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200384 struct dns_resolvers *resolvers;
385 struct dns_resolution *res;
386 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200387
Christopher Faulet67957bd2017-09-27 11:00:59 +0200388 if (!req || !req->resolution)
389 return;
390 res = req->resolution;
391 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200392
Christopher Faulet67957bd2017-09-27 11:00:59 +0200393 /* The resolution must not be triggered yet. Use the cached response, if
394 * valid */
395 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200396 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
397 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
398 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200399}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200400
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200401
Christopher Faulet67957bd2017-09-27 11:00:59 +0200402/* Resets some resolution parameters to initial values and also delete the query
403 * ID from the resolver's tree.
404 */
405static void dns_reset_resolution(struct dns_resolution *resolution)
406{
407 /* update resolution status */
408 resolution->step = RSLV_STEP_NONE;
409 resolution->try = 0;
410 resolution->last_resolution = now_ms;
411 resolution->nb_queries = 0;
412 resolution->nb_responses = 0;
413 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200414
Christopher Faulet67957bd2017-09-27 11:00:59 +0200415 /* clean up query id */
416 eb32_delete(&resolution->qid);
417 resolution->query_id = 0;
418 resolution->qid.key = 0;
419}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200420
Christopher Faulet67957bd2017-09-27 11:00:59 +0200421/* Returns the query id contained in a DNS response */
422static inline unsigned short dns_response_get_query_id(unsigned char *resp)
423{
424 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200425}
426
Christopher Faulet67957bd2017-09-27 11:00:59 +0200427
428/* Analyses, re-builds and copies the name <name> from the DNS response packet
429 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
430 * for compressed data. The result is copied into <dest>, ensuring we don't
431 * overflow using <dest_len> Returns the number of bytes the caller can move
432 * forward. If 0 it means an error occurred while parsing the name. <offset> is
433 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200434 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200435int dns_read_name(unsigned char *buffer, unsigned char *bufend,
436 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100437 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200438{
439 int nb_bytes = 0, n = 0;
440 int label_len;
441 unsigned char *reader = name;
442 char *dest = destination;
443
444 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100445 if (reader >= bufend)
446 goto err;
447
Christopher Faulet67957bd2017-09-27 11:00:59 +0200448 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200449 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100450 if (reader + 1 >= bufend)
451 goto err;
452
Christopher Faulet67957bd2017-09-27 11:00:59 +0200453 /* Must point BEFORE current position */
454 if ((buffer + reader[1]) > reader)
455 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200456
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100457 if (depth++ > 100)
458 goto err;
459
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530460 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100461 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200462 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200463 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200464
Christopher Faulet67957bd2017-09-27 11:00:59 +0200465 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200466 nb_bytes += n;
467 goto out;
468 }
469
470 label_len = *reader;
471 if (label_len == 0)
472 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200473
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200474 /* Check if:
475 * - we won't read outside the buffer
476 * - there is enough place in the destination
477 */
478 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200479 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200480
481 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200482 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200483
484 memcpy(dest, reader, label_len);
485
Christopher Faulet67957bd2017-09-27 11:00:59 +0200486 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200487 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200488 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200489 }
490
Christopher Faulet67957bd2017-09-27 11:00:59 +0200491 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200492 /* offset computation:
493 * parse from <name> until finding either NULL or a pointer "c0xx"
494 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200495 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200496 *offset = 0;
497 while (reader < bufend) {
498 if ((reader[0] & 0xc0) == 0xc0) {
499 *offset += 2;
500 break;
501 }
502 else if (*reader == 0) {
503 *offset += 1;
504 break;
505 }
506 *offset += 1;
507 ++reader;
508 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200509 return nb_bytes;
510
Christopher Faulet67957bd2017-09-27 11:00:59 +0200511 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200512 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200513}
514
Christopher Faulet67957bd2017-09-27 11:00:59 +0200515/* Checks for any obsolete record, also identify any SRV request, and try to
516 * find a corresponding server.
517*/
518static void dns_check_dns_response(struct dns_resolution *res)
519{
520 struct dns_resolvers *resolvers = res->resolvers;
521 struct dns_requester *req, *reqback;
522 struct dns_answer_item *item, *itemback;
523 struct server *srv;
524 struct dns_srvrq *srvrq;
525
526 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +0200527 struct dns_answer_item *ar_item = item->ar_item;
528
529 /* clean up obsolete Additional record */
530 if (ar_item && (ar_item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
531 pool_free(dns_answer_item_pool, ar_item);
532 item->ar_item = NULL;
533 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200534
535 /* Remove obsolete items */
536 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
537 if (item->type != DNS_RTYPE_SRV)
538 goto rm_obselete_item;
539
540 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
541 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
542 continue;
543
544 /* Remove any associated server */
545 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100546 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200547 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
548 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200549 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200550 snr_update_srv_status(srv, 1);
551 free(srv->hostname);
552 free(srv->hostname_dn);
553 srv->hostname = NULL;
554 srv->hostname_dn = NULL;
555 srv->hostname_dn_len = 0;
556 dns_unlink_resolution(srv->dns_requester);
557 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100558 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200559 }
560 }
561
562 rm_obselete_item:
563 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +0200564 if (item->ar_item) {
565 pool_free(dns_answer_item_pool, item->ar_item);
566 item->ar_item = NULL;
567 }
Willy Tarreaubafbe012017-11-24 17:34:44 +0100568 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200569 continue;
570 }
571
572 if (item->type != DNS_RTYPE_SRV)
573 continue;
574
575 /* Now process SRV records */
576 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
577 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
578 continue;
579
580 /* Check if a server already uses that hostname */
581 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100582 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200583 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
584 item->data_len == srv->hostname_dn_len &&
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200585 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200586 break;
587 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100588 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200589 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200590
591 /* If not, try to find a server with undefined hostname */
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200592 if (!srv) {
593 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
594 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
595 if (srv->srvrq == srvrq && !srv->hostname_dn)
596 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100597 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100598 }
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200599 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200600
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200601 /* And update this server, if found (srv is locked here) */
602 if (srv) {
Baptiste Assmann13a92322019-06-07 09:40:55 +0200603 /* Check if an Additional Record is associated to this SRV record.
604 * Perform some sanity checks too to ensure the record can be used.
605 * If all fine, we simply pick up the IP address found and associate
606 * it to the server.
607 */
608 if ((item->ar_item != NULL) &&
609 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
610 {
611
612 switch (item->ar_item->type) {
613 case DNS_RTYPE_A:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200614 update_server_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional record");
Baptiste Assmann13a92322019-06-07 09:40:55 +0200615 break;
616 case DNS_RTYPE_AAAA:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200617 update_server_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional record");
Baptiste Assmann13a92322019-06-07 09:40:55 +0200618 break;
619 }
620
621 srv->flags |= SRV_F_NO_RESOLUTION;
622 }
623
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200624 if (!srv->hostname_dn) {
625 const char *msg = NULL;
626 char hostname[DNS_MAX_NAME_SIZE];
627
628 if (dns_dn_label_to_str(item->target, item->data_len+1,
629 hostname, DNS_MAX_NAME_SIZE) == -1) {
630 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
631 continue;
632 }
633 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
634 if (msg)
635 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
636 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200637
Baptiste Assmann87138c32020-08-04 10:57:21 +0200638 /* now we have an IP address associated to this server, we can update its status */
639 snr_update_srv_status(srv, 0);
640
Christopher Faulet67957bd2017-09-27 11:00:59 +0200641 srv->svc_port = item->port;
642 srv->flags &= ~SRV_F_MAPPORTS;
643 if ((srv->check.state & CHK_ST_CONFIGURED) &&
644 !(srv->flags & SRV_F_CHECKPORT))
645 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100646
Daniel Corbettf8716912019-11-17 09:48:56 -0500647 if (!srv->dns_opts.ignore_weight) {
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200648 char weight[9];
649 int ha_weight;
650
Daniel Corbettf8716912019-11-17 09:48:56 -0500651 /* DNS weight range if from 0 to 65535
652 * HAProxy weight is from 0 to 256
653 * The rule below ensures that weight 0 is well respected
654 * while allowing a "mapping" from DNS weight into HAProxy's one.
655 */
656 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100657
Daniel Corbettf8716912019-11-17 09:48:56 -0500658 snprintf(weight, sizeof(weight), "%d", ha_weight);
659 server_parse_weight_change_request(srv, weight);
660 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100661 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200662 }
663 }
664 }
665}
666
667/* Validates that the buffer DNS response provided in <resp> and finishing
668 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200670 * The result is stored in <resolution>' response, buf_response,
671 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200672 *
673 * This function returns one of the DNS_RESP_* code to indicate the type of
674 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200675 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200676static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
677 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200678{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 unsigned char *reader;
680 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200681 int len, flags, offset;
682 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200683 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200685 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200686 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200687 int i, found = 0;
Willy Tarreau963f7012020-07-22 17:00:45 +0200688 int cause = DNS_RESP_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200689
Christopher Faulet67957bd2017-09-27 11:00:59 +0200690 reader = resp;
691 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200692 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200693 dns_query = NULL;
Willy Tarreau963f7012020-07-22 17:00:45 +0200694 dns_answer_record = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200695
Christopher Faulet67957bd2017-09-27 11:00:59 +0200696 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200697 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200698
699 /* query id */
700 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200701 goto invalid_resp;
702
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200703 dns_p->header.id = reader[0] * 256 + reader[1];
704 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200705
Christopher Faulet67957bd2017-09-27 11:00:59 +0200706 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200707 * First byte contains:
708 * - response flag (1 bit)
709 * - opcode (4 bits)
710 * - authoritative (1 bit)
711 * - truncated (1 bit)
712 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200713 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200714 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200715 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200716
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200717 flags = reader[0] * 256 + reader[1];
718
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200719 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
Willy Tarreau963f7012020-07-22 17:00:45 +0200720 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
721 cause = DNS_RESP_NX_DOMAIN;
722 goto return_error;
723 }
724 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) {
725 cause = DNS_RESP_REFUSED;
726 goto return_error;
727 }
728 else {
729 cause = DNS_RESP_ERROR;
730 goto return_error;
731 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200732 }
733
Christopher Faulet67957bd2017-09-27 11:00:59 +0200734 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200735 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200736
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200737 /* 2 bytes for question count */
738 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200739 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200740 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200741 /* (for now) we send one query only, so we expect only one in the
742 * response too */
Willy Tarreau963f7012020-07-22 17:00:45 +0200743 if (dns_p->header.qdcount != 1) {
744 cause = DNS_RESP_QUERY_COUNT_ERROR;
745 goto return_error;
746 }
747
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200748 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200749 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200750 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200751
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200752 /* 2 bytes for answer count */
753 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200754 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200755 dns_p->header.ancount = reader[0] * 256 + reader[1];
Willy Tarreau963f7012020-07-22 17:00:45 +0200756 if (dns_p->header.ancount == 0) {
757 cause = DNS_RESP_ANCOUNT_ZERO;
758 goto return_error;
759 }
760
Christopher Faulet67957bd2017-09-27 11:00:59 +0200761 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200762 if (dns_p->header.ancount > max_answer_records)
Willy Tarreau963f7012020-07-22 17:00:45 +0200763 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200765
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 /* 2 bytes authority count */
767 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200768 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200769 dns_p->header.nscount = reader[0] * 256 + reader[1];
770 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200771
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200772 /* 2 bytes additional count */
773 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200774 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200775 dns_p->header.arcount = reader[0] * 256 + reader[1];
776 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200777
Christopher Faulet67957bd2017-09-27 11:00:59 +0200778 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 LIST_INIT(&dns_p->query_list);
780 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 +0200781 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200782 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200783 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200784 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200785 goto invalid_resp;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200786 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200787 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200788
Christopher Faulet67957bd2017-09-27 11:00:59 +0200789 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200790 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200791 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200792 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100793 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200794
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200795 if (len == 0)
Willy Tarreau963f7012020-07-22 17:00:45 +0200796 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200797
798 reader += offset;
799 previous_dname = dns_query->name;
800
801 /* move forward 2 bytes for question type */
802 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200803 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804 dns_query->type = reader[0] * 256 + reader[1];
805 reader += 2;
806
807 /* move forward 2 bytes for question class */
808 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200809 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200810 dns_query->class = reader[0] * 256 + reader[1];
811 reader += 2;
812 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200813
Baptiste Assmann251abb92017-08-11 09:58:27 +0200814 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200815 * because a TRUNCATED SRV query type response can still be exploited */
Willy Tarreau963f7012020-07-22 17:00:45 +0200816 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) {
817 cause = DNS_RESP_TRUNCATED;
818 goto return_error;
819 }
Baptiste Assmann251abb92017-08-11 09:58:27 +0200820
Baptiste Assmann325137d2015-04-13 23:40:55 +0200821 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200822 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200823 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200824 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200825 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200826
Willy Tarreaubafbe012017-11-24 17:34:44 +0100827 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200828 if (dns_answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +0200829 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200830
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200831 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100832 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200833
Willy Tarreau963f7012020-07-22 17:00:45 +0200834 if (len == 0)
835 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200836
Christopher Faulet67957bd2017-09-27 11:00:59 +0200837 /* Check if the current record dname is valid. previous_dname
838 * points either to queried dname or last CNAME target */
Olivier Houchardb17b8842020-04-01 18:30:27 +0200839 if (dns_query->type != DNS_RTYPE_SRV && dns_hostname_cmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200840 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200841 /* First record, means a mismatch issue between
842 * queried dname and dname found in the first
843 * record */
Willy Tarreau963f7012020-07-22 17:00:45 +0200844 goto invalid_resp;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200845 }
846 else {
847 /* If not the first record, this means we have a
Willy Tarreau963f7012020-07-22 17:00:45 +0200848 * CNAME resolution error.
849 */
850 cause = DNS_RESP_CNAME_ERROR;
851 goto return_error;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200852 }
853
Baptiste Assmann325137d2015-04-13 23:40:55 +0200854 }
855
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200856 memcpy(dns_answer_record->name, tmpname, len);
857 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200858
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200859 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +0200860 if (reader >= bufend)
861 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200862
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200863 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200864 if (reader + 2 > bufend)
865 goto invalid_resp;
866
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200867 dns_answer_record->type = reader[0] * 256 + reader[1];
868 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200869
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200870 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200871 if (reader + 2 > bufend)
872 goto invalid_resp;
873
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200874 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200875 reader += 2;
876
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200877 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200878 if (reader + 4 > bufend)
879 goto invalid_resp;
880
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 */
Willy Tarreau963f7012020-07-22 17:00:45 +0200886 if (reader + 2 > bufend)
887 goto invalid_resp;
888
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200889 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200890
Christopher Faulet67957bd2017-09-27 11:00:59 +0200891 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200892 reader += 2;
893
Willy Tarreau963f7012020-07-22 17:00:45 +0200894 if (reader + dns_answer_record->data_len > bufend)
895 goto invalid_resp;
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100896
Christopher Faulet67957bd2017-09-27 11:00:59 +0200897 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200898 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200899 case DNS_RTYPE_A:
900 /* ipv4 is stored on 4 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +0200901 if (dns_answer_record->data_len != 4)
902 goto invalid_resp;
903
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200904 dns_answer_record->address.sa_family = AF_INET;
905 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
906 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200907 break;
908
909 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200910 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200911 * no IP could be found and last record was a CNAME. Could be triggered
912 * by a wrong query type
913 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200914 * + 1 because dns_answer_record_id starts at 0
915 * while number of answers is an integer and
916 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200917 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200918 if (i + 1 == dns_p->header.ancount) {
Willy Tarreau963f7012020-07-22 17:00:45 +0200919 cause = DNS_RESP_CNAME_ERROR;
920 goto return_error;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200921 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200922
923 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100924 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +0200925 if (len == 0)
926 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200927
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200928 memcpy(dns_answer_record->target, tmpname, len);
929 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200930 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200931 break;
932
Olivier Houchard8da5f982017-08-04 18:35:36 +0200933
934 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200935 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200936 * - 2 bytes for the priority
937 * - 2 bytes for the weight
938 * - 2 bytes for the port
939 * - the target hostname
940 */
Willy Tarreau963f7012020-07-22 17:00:45 +0200941 if (dns_answer_record->data_len <= 6)
942 goto invalid_resp;
943
Willy Tarreaud5370e12017-09-19 14:59:52 +0200944 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200945 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200946 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200947 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200948 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200949 reader += sizeof(uint16_t);
950 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100951 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +0200952 if (len == 0)
953 goto invalid_resp;
954
Olivier Houchard8da5f982017-08-04 18:35:36 +0200955 dns_answer_record->data_len = len;
956 memcpy(dns_answer_record->target, tmpname, len);
957 dns_answer_record->target[len] = 0;
958 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200959
Baptiste Assmann325137d2015-04-13 23:40:55 +0200960 case DNS_RTYPE_AAAA:
961 /* ipv6 is stored on 16 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +0200962 if (dns_answer_record->data_len != 16)
963 goto invalid_resp;
964
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200965 dns_answer_record->address.sa_family = AF_INET6;
966 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
967 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200969
Baptiste Assmann325137d2015-04-13 23:40:55 +0200970 } /* switch (record type) */
971
Christopher Faulet67957bd2017-09-27 11:00:59 +0200972 /* Increment the counter for number of records saved into our
973 * local response */
974 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200975
Christopher Faulet67957bd2017-09-27 11:00:59 +0200976 /* Move forward dns_answer_record->data_len for analyzing next
977 * record in the response */
978 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
979 ? offset
980 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200981
982 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200983 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200984 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
985 if (tmp_record->type != dns_answer_record->type)
986 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200987
988 switch(tmp_record->type) {
989 case DNS_RTYPE_A:
990 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
991 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
992 sizeof(in_addr_t)))
993 found = 1;
994 break;
995
996 case DNS_RTYPE_AAAA:
997 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
998 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
999 sizeof(struct in6_addr)))
1000 found = 1;
1001 break;
1002
Olivier Houchard8da5f982017-08-04 18:35:36 +02001003 case DNS_RTYPE_SRV:
1004 if (dns_answer_record->data_len == tmp_record->data_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001005 !dns_hostname_cmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001006 dns_answer_record->port == tmp_record->port) {
1007 tmp_record->weight = dns_answer_record->weight;
1008 found = 1;
1009 }
1010 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001011
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001012 default:
1013 break;
1014 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001015
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001016 if (found == 1)
1017 break;
1018 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001019
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001020 if (found == 1) {
1021 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001022 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001023 dns_answer_record = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001024 }
1025 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001026 dns_answer_record->last_seen = now.tv_sec;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001027 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001028 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
Willy Tarreau963f7012020-07-22 17:00:45 +02001029 dns_answer_record = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001030 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001031 } /* for i 0 to ancount */
1032
Christopher Faulet67957bd2017-09-27 11:00:59 +02001033 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001034 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001035
Baptiste Assmann37950c82020-02-19 01:08:51 +01001036 /* now parsing additional records for SRV queries only */
1037 if (dns_query->type != DNS_RTYPE_SRV)
1038 goto skip_parsing_additional_records;
Willy Tarreau963f7012020-07-22 17:00:45 +02001039
Jerome Magnin4002f8d2020-07-26 12:13:12 +02001040 /* if we find Authority records, just skip them */
1041 for (i = 0; i < dns_p->header.nscount; i++) {
1042 offset = 0;
1043 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE,
1044 &offset, 0);
1045 if (len == 0)
1046 continue;
1047
1048 if (reader + offset + 10 >= bufend)
1049 goto invalid_resp;
1050
1051 reader += offset;
1052 /* skip 2 bytes for class */
1053 reader += 2;
1054 /* skip 2 bytes for type */
1055 reader += 2;
1056 /* skip 4 bytes for ttl */
1057 reader += 4;
1058 /* read data len */
1059 len = reader[0] * 256 + reader[1];
1060 reader += 2;
1061
1062 if (reader + len >= bufend)
1063 goto invalid_resp;
1064
1065 reader += len;
1066 }
1067
Baptiste Assmann13a92322019-06-07 09:40:55 +02001068 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001069 for (i = 0; i < dns_p->header.arcount; i++) {
1070 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +02001071 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001072
1073 dns_answer_record = pool_alloc(dns_answer_item_pool);
1074 if (dns_answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +02001075 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001076
1077 offset = 0;
1078 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1079
1080 if (len == 0) {
1081 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001082 dns_answer_record = NULL;
Baptiste Assmann37950c82020-02-19 01:08:51 +01001083 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001084 }
1085
1086 memcpy(dns_answer_record->name, tmpname, len);
1087 dns_answer_record->name[len] = 0;
1088
1089 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +02001090 if (reader >= bufend)
1091 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001092
1093 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001094 if (reader + 2 > bufend)
1095 goto invalid_resp;
1096
Baptiste Assmann13a92322019-06-07 09:40:55 +02001097 dns_answer_record->type = reader[0] * 256 + reader[1];
1098 reader += 2;
1099
1100 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001101 if (reader + 2 > bufend)
1102 goto invalid_resp;
1103
Baptiste Assmann13a92322019-06-07 09:40:55 +02001104 dns_answer_record->class = reader[0] * 256 + reader[1];
1105 reader += 2;
1106
1107 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001108 if (reader + 4 > bufend)
1109 goto invalid_resp;
1110
Baptiste Assmann13a92322019-06-07 09:40:55 +02001111 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1112 + reader[2] * 256 + reader[3];
1113 reader += 4;
1114
1115 /* Now reading data len */
Willy Tarreau963f7012020-07-22 17:00:45 +02001116 if (reader + 2 > bufend)
1117 goto invalid_resp;
1118
Baptiste Assmann13a92322019-06-07 09:40:55 +02001119 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1120
1121 /* Move forward 2 bytes for data len */
1122 reader += 2;
1123
Willy Tarreau963f7012020-07-22 17:00:45 +02001124 if (reader + dns_answer_record->data_len > bufend)
1125 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001126
1127 /* Analyzing record content */
1128 switch (dns_answer_record->type) {
1129 case DNS_RTYPE_A:
1130 /* ipv4 is stored on 4 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001131 if (dns_answer_record->data_len != 4)
1132 goto invalid_resp;
1133
Baptiste Assmann13a92322019-06-07 09:40:55 +02001134 dns_answer_record->address.sa_family = AF_INET;
1135 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1136 reader, dns_answer_record->data_len);
1137 break;
1138
1139 case DNS_RTYPE_AAAA:
1140 /* ipv6 is stored on 16 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001141 if (dns_answer_record->data_len != 16)
1142 goto invalid_resp;
1143
Baptiste Assmann13a92322019-06-07 09:40:55 +02001144 dns_answer_record->address.sa_family = AF_INET6;
1145 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1146 reader, dns_answer_record->data_len);
1147 break;
1148
1149 default:
1150 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001151 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001152 continue;
1153
1154 } /* switch (record type) */
1155
1156 /* Increment the counter for number of records saved into our
1157 * local response */
1158 nb_saved_records++;
1159
1160 /* Move forward dns_answer_record->data_len for analyzing next
1161 * record in the response */
1162 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1163 ? offset
1164 : dns_answer_record->data_len);
1165
1166 /* Lookup to see if we already had this entry */
1167 found = 0;
1168 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1169 if (tmp_record->type != dns_answer_record->type)
1170 continue;
1171
1172 switch(tmp_record->type) {
1173 case DNS_RTYPE_A:
1174 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1175 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1176 sizeof(in_addr_t)))
1177 found = 1;
1178 break;
1179
1180 case DNS_RTYPE_AAAA:
1181 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1182 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1183 sizeof(struct in6_addr)))
1184 found = 1;
1185 break;
1186
1187 default:
1188 break;
1189 }
1190
1191 if (found == 1)
1192 break;
1193 }
1194
1195 if (found == 1) {
1196 tmp_record->last_seen = now.tv_sec;
1197 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001198 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001199 }
1200 else {
1201 dns_answer_record->last_seen = now.tv_sec;
1202 dns_answer_record->ar_item = NULL;
1203
1204 // looking for the SRV record in the response list linked to this additional record
1205 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001206 if (tmp_record->type == DNS_RTYPE_SRV &&
1207 !dns_hostname_cmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len)) {
1208 /* Always use the received additional record to refresh info */
1209 if (tmp_record->ar_item)
1210 pool_free(dns_answer_item_pool, tmp_record->ar_item);
1211 tmp_record->ar_item = dns_answer_record;
1212 break;
1213 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001214 }
Christopher Faulet5a891752020-09-08 10:06:01 +02001215 if (tmp_record->ar_item != dns_answer_record)
1216 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001217 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001218 }
1219 } /* for i 0 to arcount */
1220
Baptiste Assmann37950c82020-02-19 01:08:51 +01001221 skip_parsing_additional_records:
1222
Baptiste Assmann13a92322019-06-07 09:40:55 +02001223 /* Save the number of records we really own */
1224 dns_p->header.arcount = nb_saved_records;
1225
Christopher Faulet67957bd2017-09-27 11:00:59 +02001226 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001227 return DNS_RESP_VALID;
Willy Tarreau963f7012020-07-22 17:00:45 +02001228
1229 invalid_resp:
1230 cause = DNS_RESP_INVALID;
1231
1232 return_error:
1233 pool_free(dns_answer_item_pool, dns_answer_record);
1234 return cause;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001235}
1236
Christopher Faulet67957bd2017-09-27 11:00:59 +02001237/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001238 * If existing IP not found, return the first IP matching family_priority,
1239 * otherwise, first ip found
1240 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001241 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001242 * For both cases above, dns_validate_dns_response is required
1243 * returns one of the DNS_UPD_* code
1244 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001245int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001246 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001247 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001248 void **newip, short *newip_sin_family,
1249 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001250{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001251 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001252 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001253 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001254 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001255 int currentip_sel;
1256 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001257 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001258 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001259
Christopher Faulet67957bd2017-09-27 11:00:59 +02001260 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001261 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001262 *newip = newip4 = newip6 = NULL;
1263 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001264 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001265 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001266
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001267 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001268 * Top priority is the preferred network ip version,
1269 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001270 * the last priority is the currently used IP,
1271 *
1272 * For these three priorities, a score is calculated. The
1273 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001274 * 8 - preferred ip version.
1275 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001276 * 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 +01001277 * 1 - current ip.
1278 * The result with the biggest score is returned.
1279 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001280
1281 list_for_each_entry(record, &dns_p->answer_list, list) {
1282 void *ip;
1283 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001284
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001285 if (record->type == DNS_RTYPE_A) {
1286 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1287 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001288 }
1289 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001290 ip_type = AF_INET6;
1291 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001292 }
1293 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001294 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001295 score = 0;
1296
Joseph Herlant42cf6392018-11-15 10:33:28 -08001297 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001298 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001299 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001300
Joseph Herlant42cf6392018-11-15 10:33:28 -08001301 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001302 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001303
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001304 /* Compare only the same addresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001305 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001306 continue;
1307
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001308 if ((ip_type == AF_INET &&
1309 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001310 &dns_opts->pref_net[j].mask.in4,
1311 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001312 (ip_type == AF_INET6 &&
1313 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001314 &dns_opts->pref_net[j].mask.in6,
1315 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001316 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001317 break;
1318 }
1319 }
1320
Christopher Faulet67957bd2017-09-27 11:00:59 +02001321 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001322 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001323 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001324 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001325 if (!allowed_duplicated_ip) {
1326 continue;
1327 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001328 } else {
1329 score += 2;
1330 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001331
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001332 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001333 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001334 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001335 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001336 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001337 !memcmp(ip, currentip, 16)))) {
1338 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001339 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001340 }
1341 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001342 currentip_sel = 0;
1343
1344 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001345 * score. The maximum score is 15, if this value is reached, we
1346 * break the parsing. Implicitly, this score is reached the ip
1347 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001348 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001349 if (ip_type == AF_INET)
1350 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001351 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001352 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001353 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001354 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001355 return DNS_UPD_NO;
1356 max_score = score;
1357 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001358 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001359
Christopher Faulet67957bd2017-09-27 11:00:59 +02001360 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001361 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001362 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001363
Christopher Faulet67957bd2017-09-27 11:00:59 +02001364 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001365 if (family_priority == AF_INET) {
1366 if (newip4) {
1367 *newip = newip4;
1368 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001369 }
1370 else if (newip6) {
1371 *newip = newip6;
1372 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001373 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001374 if (!currentip_found)
1375 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001376 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001377 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001378 else if (family_priority == AF_INET6) {
1379 if (newip6) {
1380 *newip = newip6;
1381 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001382 }
1383 else if (newip4) {
1384 *newip = newip4;
1385 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001386 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001387 if (!currentip_found)
1388 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001389 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001390 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001391 else if (family_priority == AF_UNSPEC) {
1392 if (newip6) {
1393 *newip = newip6;
1394 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001395 }
1396 else if (newip4) {
1397 *newip = newip4;
1398 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001399 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001400 if (!currentip_found)
1401 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001402 }
1403
Christopher Faulet67957bd2017-09-27 11:00:59 +02001404 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001405 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001406
Christopher Faulet67957bd2017-09-27 11:00:59 +02001407 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001408 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001409 /* Move the first record to the end of the list, for internal
1410 * round robin */
1411 LIST_DEL(&record->list);
1412 LIST_ADDQ(&dns_p->answer_list, &record->list);
1413 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001414 }
1415 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001416}
1417
Christopher Faulet67957bd2017-09-27 11:00:59 +02001418/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001419 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001420 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1421 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001422 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001423 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1424 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001425 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001426int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001427{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001428 char *ptr;
1429 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001430
Christopher Faulet67957bd2017-09-27 11:00:59 +02001431 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001432 return -1;
1433
Christopher Faulet67957bd2017-09-27 11:00:59 +02001434 ptr = str;
1435 for (i = 0; i < dn_len-1; ++i) {
1436 sz = dn[i];
1437 if (i)
1438 *ptr++ = '.';
1439 memcpy(ptr, dn+i+1, sz);
1440 ptr += sz;
1441 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001442 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001443 *ptr++ = '\0';
1444 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001445}
1446
Christopher Faulet67957bd2017-09-27 11:00:59 +02001447/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1448 *
1449 * <str> must be a null-terminated string. <str_len> must include the
1450 * terminating null byte. <dn> buffer must be allocated and its size must be
1451 * passed in <dn_len>.
1452 *
1453 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1454 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001455 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001456int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001457{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001458 int i, offset;
1459
Christopher Faulet67957bd2017-09-27 11:00:59 +02001460 if (dn_len < str_len + 1)
1461 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001462
Christopher Faulet67957bd2017-09-27 11:00:59 +02001463 /* First byte of dn will be used to store the length of the first
1464 * label */
1465 offset = 0;
1466 for (i = 0; i < str_len; ++i) {
1467 if (str[i] == '.') {
1468 /* 2 or more consecutive dots is invalid */
1469 if (i == offset)
1470 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001471
Lukas Tribus81725b82020-02-27 15:47:24 +01001472 /* ignore trailing dot */
1473 if (i + 2 == str_len) {
1474 i++;
1475 break;
1476 }
1477
Christopher Faulet67957bd2017-09-27 11:00:59 +02001478 dn[offset] = (i - offset);
1479 offset = i+1;
1480 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001481 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001482 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001483 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001484 dn[offset] = (i - offset - 1);
1485 dn[i] = '\0';
1486 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001487}
1488
Christopher Faulet67957bd2017-09-27 11:00:59 +02001489/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001490 * - total size
1491 * - each label size individually
1492 * returns:
1493 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1494 * 1 when no error. <err> is left unaffected.
1495 */
1496int dns_hostname_validation(const char *string, char **err)
1497{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001498 int i;
1499
1500 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1501 if (err)
1502 *err = DNS_TOO_LONG_FQDN;
1503 return 0;
1504 }
1505
William Dauchyaecd5dc2020-01-26 19:52:34 +01001506 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001507 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001508 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1509 if (!(*string == '-' || *string == '_' ||
1510 (*string >= 'a' && *string <= 'z') ||
1511 (*string >= 'A' && *string <= 'Z') ||
1512 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001513 if (err)
1514 *err = DNS_INVALID_CHARACTER;
1515 return 0;
1516 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001517 i++;
1518 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001519 }
1520
William Dauchyaecd5dc2020-01-26 19:52:34 +01001521 if (!(*string))
1522 break;
1523
1524 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001525 if (err)
1526 *err = DNS_LABEL_TOO_LONG;
1527 return 0;
1528 }
1529
William Dauchyaecd5dc2020-01-26 19:52:34 +01001530 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001531 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001532 return 1;
1533}
1534
Christopher Faulet67957bd2017-09-27 11:00:59 +02001535/* Picks up an available resolution from the different resolution list
1536 * associated to a resolvers section, in this order:
1537 * 1. check in resolutions.curr for the same hostname and query_type
1538 * 2. check in resolutions.wait for the same hostname and query_type
1539 * 3. Get a new resolution from resolution pool
1540 *
1541 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001542 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001543static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1544 char **hostname_dn, int hostname_dn_len,
1545 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001546{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001547 struct dns_resolution *res;
1548
1549 if (!*hostname_dn)
1550 goto from_pool;
1551
1552 /* Search for same hostname and query type in resolutions.curr */
1553 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1554 if (!res->hostname_dn)
1555 continue;
1556 if ((query_type == res->prefered_query_type) &&
1557 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001558 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559 return res;
1560 }
1561
1562 /* Search for same hostname and query type in resolutions.wait */
1563 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1564 if (!res->hostname_dn)
1565 continue;
1566 if ((query_type == res->prefered_query_type) &&
1567 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001568 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001569 return res;
1570 }
1571
1572 from_pool:
1573 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001574 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575 if (res) {
1576 memset(res, 0, sizeof(*res));
1577 res->resolvers = resolvers;
1578 res->uuid = resolution_uuid;
1579 res->status = RSLV_STATUS_NONE;
1580 res->step = RSLV_STEP_NONE;
1581 res->last_valid = now_ms;
1582
1583 LIST_INIT(&res->requesters);
1584 LIST_INIT(&res->response.answer_list);
1585
1586 res->prefered_query_type = query_type;
1587 res->query_type = query_type;
1588 res->hostname_dn = *hostname_dn;
1589 res->hostname_dn_len = hostname_dn_len;
1590
1591 ++resolution_uuid;
1592
1593 /* Move the resolution to the resolvers wait queue */
1594 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1595 }
1596 return res;
1597}
1598
1599/* Releases a resolution from its requester(s) and move it back to the pool */
1600static void dns_free_resolution(struct dns_resolution *resolution)
1601{
1602 struct dns_requester *req, *reqback;
Christopher Faulet010ab352020-07-22 15:55:49 +02001603 struct dns_answer_item *item, *itemback;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604
1605 /* clean up configuration */
1606 dns_reset_resolution(resolution);
1607 resolution->hostname_dn = NULL;
1608 resolution->hostname_dn_len = 0;
1609
1610 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1611 LIST_DEL(&req->list);
1612 req->resolution = NULL;
1613 }
1614
Christopher Faulet010ab352020-07-22 15:55:49 +02001615 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
1616 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +02001617 if (item->ar_item) {
1618 pool_free(dns_answer_item_pool, item->ar_item);
1619 item->ar_item = NULL;
1620 }
Christopher Faulet010ab352020-07-22 15:55:49 +02001621 pool_free(dns_answer_item_pool, item);
1622 }
1623
Christopher Faulet67957bd2017-09-27 11:00:59 +02001624 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001625 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001626}
1627
1628/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1629 * on success, -1 otherwise.
1630 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001631int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001632{
1633 struct dns_resolution *res = NULL;
1634 struct dns_requester *req;
1635 struct dns_resolvers *resolvers;
1636 struct server *srv = NULL;
1637 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001638 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001639 char **hostname_dn;
1640 int hostname_dn_len, query_type;
1641
1642 switch (requester_type) {
1643 case OBJ_TYPE_SERVER:
1644 srv = (struct server *)requester;
1645 hostname_dn = &srv->hostname_dn;
1646 hostname_dn_len = srv->hostname_dn_len;
1647 resolvers = srv->resolvers;
1648 query_type = ((srv->dns_opts.family_prio == AF_INET)
1649 ? DNS_RTYPE_A
1650 : DNS_RTYPE_AAAA);
1651 break;
1652
1653 case OBJ_TYPE_SRVRQ:
1654 srvrq = (struct dns_srvrq *)requester;
1655 hostname_dn = &srvrq->hostname_dn;
1656 hostname_dn_len = srvrq->hostname_dn_len;
1657 resolvers = srvrq->resolvers;
1658 query_type = DNS_RTYPE_SRV;
1659 break;
1660
Baptiste Assmann333939c2019-01-21 08:34:50 +01001661 case OBJ_TYPE_STREAM:
1662 stream = (struct stream *)requester;
1663 hostname_dn = &stream->dns_ctx.hostname_dn;
1664 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1665 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001666 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001667 ? DNS_RTYPE_A
1668 : DNS_RTYPE_AAAA);
1669 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670 default:
1671 goto err;
1672 }
1673
1674 /* Get a resolution from the resolvers' wait queue or pool */
1675 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1676 goto err;
1677
1678 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001679 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001680 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001681 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001682 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001683 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001684 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001685 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001686 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687 req->owner = &srv->obj_type;
1688 srv->dns_requester = req;
1689 }
1690 else
1691 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001692 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001693 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001694
1695 req->requester_cb = snr_resolution_cb;
1696 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697 }
1698 else if (srvrq) {
1699 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001700 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701 goto err;
1702 req->owner = &srvrq->obj_type;
1703 srvrq->dns_requester = req;
1704 }
1705 else
1706 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001707
1708 req->requester_cb = snr_resolution_cb;
1709 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001710 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001711 else if (stream) {
1712 if (stream->dns_ctx.dns_requester == NULL) {
1713 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1714 goto err;
1715 req->owner = &stream->obj_type;
1716 stream->dns_ctx.dns_requester = req;
1717 }
1718 else
1719 req = stream->dns_ctx.dns_requester;
1720
1721 req->requester_cb = act_resolution_cb;
1722 req->requester_error_cb = act_resolution_error_cb;
1723 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001724 else
1725 goto err;
1726
1727 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728
1729 LIST_ADDQ(&res->requesters, &req->list);
1730 return 0;
1731
1732 err:
1733 if (res && LIST_ISEMPTY(&res->requesters))
1734 dns_free_resolution(res);
1735 return -1;
1736}
1737
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001738/* Removes a requester from a DNS resolution. It takes takes care of all the
Christopher Faulet67957bd2017-09-27 11:00:59 +02001739 * consequences. It also cleans up some parameters from the requester.
1740 */
1741void dns_unlink_resolution(struct dns_requester *requester)
1742{
1743 struct dns_resolution *res;
1744 struct dns_requester *req;
1745
1746 /* Nothing to do */
1747 if (!requester || !requester->resolution)
1748 return;
1749 res = requester->resolution;
1750
1751 /* Clean up the requester */
1752 LIST_DEL(&requester->list);
1753 requester->resolution = NULL;
1754
1755 /* We need to find another requester linked on this resolution */
1756 if (!LIST_ISEMPTY(&res->requesters))
1757 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1758 else {
1759 dns_free_resolution(res);
1760 return;
1761 }
1762
1763 /* Move hostname_dn related pointers to the next requester */
1764 switch (obj_type(req->owner)) {
1765 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001766 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1767 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001768 break;
1769 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001770 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1771 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001772 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001773 case OBJ_TYPE_STREAM:
1774 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1775 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1776 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001777 default:
1778 res->hostname_dn = NULL;
1779 res->hostname_dn_len = 0;
1780 break;
1781 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001782}
1783
Christopher Faulet67957bd2017-09-27 11:00:59 +02001784/* Called when a network IO is generated on a name server socket for an incoming
1785 * packet. It performs the following actions:
1786 * - check if the packet requires processing (not outdated resolution)
1787 * - ensure the DNS packet received is valid and call requester's callback
1788 * - call requester's error callback if invalid response
1789 * - check the dn_name in the packet against the one sent
1790 */
1791static void dns_resolve_recv(struct dgram_conn *dgram)
1792{
1793 struct dns_nameserver *ns, *tmpns;
1794 struct dns_resolvers *resolvers;
1795 struct dns_resolution *res;
1796 struct dns_query_item *query;
1797 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1798 unsigned char *bufend;
1799 int fd, buflen, dns_resp;
1800 int max_answer_records;
1801 unsigned short query_id;
1802 struct eb32_node *eb;
1803 struct dns_requester *req;
1804
1805 fd = dgram->t.sock.fd;
1806
1807 /* check if ready for reading */
1808 if (!fd_recv_ready(fd))
1809 return;
1810
1811 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001812 if ((ns = dgram->owner) == NULL) {
1813 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1814 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001815 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001816 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001817
1818 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001819 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001820
1821 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001822 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001823 /* read message received */
1824 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1825 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001826 /* FIXME : for now we consider EAGAIN only, but at
1827 * least we purge sticky errors that would cause us to
1828 * be called in loops.
1829 */
1830 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001831 fd_cant_recv(fd);
1832 break;
1833 }
1834
1835 /* message too big */
1836 if (buflen > resolvers->accepted_payload_size) {
1837 ns->counters.too_big++;
1838 continue;
1839 }
1840
1841 /* initializing variables */
1842 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1843
1844 /* read the query id from the packet (16 bits) */
1845 if (buf + 2 > bufend) {
1846 ns->counters.invalid++;
1847 continue;
1848 }
1849 query_id = dns_response_get_query_id(buf);
1850
1851 /* search the query_id in the pending resolution tree */
1852 eb = eb32_lookup(&resolvers->query_ids, query_id);
1853 if (eb == NULL) {
1854 /* unknown query id means an outdated response and can be safely ignored */
1855 ns->counters.outdated++;
1856 continue;
1857 }
1858
William Dauchybe8a3872019-11-27 23:32:41 +01001859 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001860 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001861 /* number of responses received */
1862 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001863
Christopher Faulet67957bd2017-09-27 11:00:59 +02001864 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1865 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 switch (dns_resp) {
1868 case DNS_RESP_VALID:
1869 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001870
Christopher Faulet67957bd2017-09-27 11:00:59 +02001871 case DNS_RESP_INVALID:
1872 case DNS_RESP_QUERY_COUNT_ERROR:
1873 case DNS_RESP_WRONG_NAME:
1874 res->status = RSLV_STATUS_INVALID;
1875 ns->counters.invalid++;
1876 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001877
Christopher Faulet67957bd2017-09-27 11:00:59 +02001878 case DNS_RESP_NX_DOMAIN:
1879 res->status = RSLV_STATUS_NX;
1880 ns->counters.nx++;
1881 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001882
Christopher Faulet67957bd2017-09-27 11:00:59 +02001883 case DNS_RESP_REFUSED:
1884 res->status = RSLV_STATUS_REFUSED;
1885 ns->counters.refused++;
1886 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001887
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888 case DNS_RESP_ANCOUNT_ZERO:
1889 res->status = RSLV_STATUS_OTHER;
1890 ns->counters.any_err++;
1891 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001892
Christopher Faulet67957bd2017-09-27 11:00:59 +02001893 case DNS_RESP_CNAME_ERROR:
1894 res->status = RSLV_STATUS_OTHER;
1895 ns->counters.cname_error++;
1896 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001897
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 case DNS_RESP_TRUNCATED:
1899 res->status = RSLV_STATUS_OTHER;
1900 ns->counters.truncated++;
1901 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001902
Christopher Faulet67957bd2017-09-27 11:00:59 +02001903 case DNS_RESP_NO_EXPECTED_RECORD:
1904 case DNS_RESP_ERROR:
1905 case DNS_RESP_INTERNAL:
1906 res->status = RSLV_STATUS_OTHER;
1907 ns->counters.other++;
1908 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001909 }
1910
Christopher Faulet67957bd2017-09-27 11:00:59 +02001911 /* Wait all nameservers response to handle errors */
1912 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1913 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001914
Christopher Faulet67957bd2017-09-27 11:00:59 +02001915 /* Process error codes */
1916 if (dns_resp != DNS_RESP_VALID) {
1917 if (res->prefered_query_type != res->query_type) {
1918 /* The fallback on the query type was already performed,
1919 * so check the try counter. If it falls to 0, we can
1920 * report an error. Else, wait the next attempt. */
1921 if (!res->try)
1922 goto report_res_error;
1923 }
1924 else {
1925 /* Fallback from A to AAAA or the opposite and re-send
1926 * the resolution immediately. try counter is not
1927 * decremented. */
1928 if (res->prefered_query_type == DNS_RTYPE_A) {
1929 res->query_type = DNS_RTYPE_AAAA;
1930 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001931 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1933 res->query_type = DNS_RTYPE_A;
1934 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001935 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001936 }
1937 continue;
1938 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001939
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940 /* Now let's check the query's dname corresponds to the one we
1941 * sent. We can check only the first query of the list. We send
1942 * one query at a time so we get one query in the response */
1943 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
Olivier Houchardb17b8842020-04-01 18:30:27 +02001944 if (query && dns_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001945 dns_resp = DNS_RESP_WRONG_NAME;
1946 ns->counters.other++;
1947 goto report_res_error;
1948 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001949
Christopher Faulet67957bd2017-09-27 11:00:59 +02001950 /* So the resolution succeeded */
1951 res->status = RSLV_STATUS_VALID;
1952 res->last_valid = now_ms;
1953 ns->counters.valid++;
1954 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001955
Christopher Faulet67957bd2017-09-27 11:00:59 +02001956 report_res_error:
1957 list_for_each_entry(req, &res->requesters, list)
1958 req->requester_error_cb(req, dns_resp);
1959 dns_reset_resolution(res);
1960 LIST_DEL(&res->list);
1961 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1962 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001963
Christopher Faulet67957bd2017-09-27 11:00:59 +02001964 report_res_success:
1965 /* Only the 1rst requester s managed by the server, others are
1966 * from the cache */
1967 tmpns = ns;
1968 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001969 struct server *s = objt_server(req->owner);
1970
1971 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001972 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001973 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001974 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001975 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001976 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001977 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001978
Christopher Faulet67957bd2017-09-27 11:00:59 +02001979 dns_reset_resolution(res);
1980 LIST_DEL(&res->list);
1981 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1982 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001983 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001984 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001985 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001986}
William Lallemand69e96442016-11-19 00:58:54 +01001987
Christopher Faulet67957bd2017-09-27 11:00:59 +02001988/* Called when a resolvers network socket is ready to send data */
1989static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001990{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001991 struct dns_resolvers *resolvers;
1992 struct dns_nameserver *ns;
1993 struct dns_resolution *res;
1994 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001995
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001997
Christopher Faulet67957bd2017-09-27 11:00:59 +02001998 /* check if ready for sending */
1999 if (!fd_send_ready(fd))
2000 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002001
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002 /* we don't want/need to be waked up any more for sending */
2003 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002004
Christopher Faulet67957bd2017-09-27 11:00:59 +02002005 /* no need to go further if we can't retrieve the nameserver */
2006 if ((ns = dgram->owner) == NULL)
2007 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002008
Christopher Faulet67957bd2017-09-27 11:00:59 +02002009 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002010 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002011
Christopher Faulet67957bd2017-09-27 11:00:59 +02002012 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002013 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002014
Christopher Faulet67957bd2017-09-27 11:00:59 +02002015 if (res->nb_queries == resolvers->nb_nameservers)
2016 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002017
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002018 len = dns_build_query(res->query_id, res->query_type,
2019 resolvers->accepted_payload_size,
2020 res->hostname_dn, res->hostname_dn_len,
2021 trash.area, trash.size);
2022 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002023 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002024
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002025 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01002026 if (ret != len) {
2027 if (ret == -1 && errno == EAGAIN) {
2028 /* retry once the socket is ready */
2029 fd_cant_send(fd);
2030 continue;
2031 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002032 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01002033 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002034
Christopher Faulet67957bd2017-09-27 11:00:59 +02002035 ns->counters.sent++;
2036 res->nb_queries++;
2037 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002038
Christopher Faulet67957bd2017-09-27 11:00:59 +02002039 snd_error:
2040 ns->counters.snd_error++;
2041 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002042 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002043 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002044}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002045
Christopher Faulet67957bd2017-09-27 11:00:59 +02002046/* Processes DNS resolution. First, it checks the active list to detect expired
2047 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2048 * checks the wait list to trigger new resolutions.
2049 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002050static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002051{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002052 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002053 struct dns_resolution *res, *resback;
2054 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002055
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002056 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002057
Christopher Faulet67957bd2017-09-27 11:00:59 +02002058 /* Handle all expired resolutions from the active list */
2059 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2060 /* When we find the first resolution in the future, then we can
2061 * stop here */
2062 exp = tick_add(res->last_query, resolvers->timeout.retry);
2063 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002064 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002065
Christopher Faulet67957bd2017-09-27 11:00:59 +02002066 /* If current resolution has been tried too many times and
2067 * finishes in timeout we update its status and remove it from
2068 * the list */
2069 if (!res->try) {
2070 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002071
Christopher Faulet67957bd2017-09-27 11:00:59 +02002072 /* Notify the result to the requesters */
2073 if (!res->nb_responses)
2074 res->status = RSLV_STATUS_TIMEOUT;
2075 list_for_each_entry(req, &res->requesters, list)
2076 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002077
Christopher Faulet67957bd2017-09-27 11:00:59 +02002078 /* Clean up resolution info and remove it from the
2079 * current list */
2080 dns_reset_resolution(res);
2081 LIST_DEL(&res->list);
2082 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002083 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002084 else {
2085 /* Otherwise resend the DNS query and requeue the resolution */
2086 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2087 /* No response received (a real timeout) or fallback already done */
2088 res->query_type = res->prefered_query_type;
2089 res->try--;
2090 }
2091 else {
2092 /* Fallback from A to AAAA or the opposite and re-send
2093 * the resolution immediately. try counter is not
2094 * decremented. */
2095 if (res->prefered_query_type == DNS_RTYPE_A)
2096 res->query_type = DNS_RTYPE_AAAA;
2097 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2098 res->query_type = DNS_RTYPE_A;
2099 else
2100 res->try--;
2101 }
2102 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002103 }
2104 }
William Lallemand69e96442016-11-19 00:58:54 +01002105
Christopher Faulet67957bd2017-09-27 11:00:59 +02002106 /* Handle all resolutions in the wait list */
2107 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2108 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2109 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2110 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002111
Christopher Faulet67957bd2017-09-27 11:00:59 +02002112 if (dns_run_resolution(res) != 1) {
2113 res->last_resolution = now_ms;
2114 LIST_DEL(&res->list);
2115 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002116 }
2117 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002118
Christopher Faulet67957bd2017-09-27 11:00:59 +02002119 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002120 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002121 return t;
2122}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002123
Christopher Faulet67957bd2017-09-27 11:00:59 +02002124/* proto_udp callback functions for a DNS resolution */
2125struct dgram_data_cb resolve_dgram_cb = {
2126 .recv = dns_resolve_recv,
2127 .send = dns_resolve_send,
2128};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002129
Christopher Faulet67957bd2017-09-27 11:00:59 +02002130/* Release memory allocated by DNS */
2131static void dns_deinit(void)
2132{
2133 struct dns_resolvers *resolvers, *resolversback;
2134 struct dns_nameserver *ns, *nsback;
2135 struct dns_resolution *res, *resback;
2136 struct dns_requester *req, *reqback;
2137 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002138
Christopher Faulet67957bd2017-09-27 11:00:59 +02002139 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2140 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2141 free(ns->id);
2142 free((char *)ns->conf.file);
2143 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2144 fd_delete(ns->dgram->t.sock.fd);
2145 free(ns->dgram);
2146 LIST_DEL(&ns->list);
2147 free(ns);
2148 }
2149
2150 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2151 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2152 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002153 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002154 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002155 dns_free_resolution(res);
2156 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002157
Christopher Faulet67957bd2017-09-27 11:00:59 +02002158 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2159 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2160 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002161 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002162 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002163 dns_free_resolution(res);
2164 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002165
Christopher Faulet67957bd2017-09-27 11:00:59 +02002166 free(resolvers->id);
2167 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002168 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002169 LIST_DEL(&resolvers->list);
2170 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002171 }
2172
Christopher Faulet67957bd2017-09-27 11:00:59 +02002173 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2174 free(srvrq->name);
2175 free(srvrq->hostname_dn);
2176 LIST_DEL(&srvrq->list);
2177 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002178 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002179}
2180
Christopher Faulet67957bd2017-09-27 11:00:59 +02002181/* Finalizes the DNS configuration by allocating required resources and checking
2182 * live parameters.
2183 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002184 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002185static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002186{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002187 struct dns_resolvers *resolvers;
2188 struct proxy *px;
2189 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002190
Christopher Faulet67957bd2017-09-27 11:00:59 +02002191 /* allocate pool of resolution per resolvers */
2192 list_for_each_entry(resolvers, &dns_resolvers, list) {
2193 struct dns_nameserver *ns;
2194 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002195
Christopher Faulet67957bd2017-09-27 11:00:59 +02002196 /* Check if we can create the socket with nameservers info */
2197 list_for_each_entry(ns, &resolvers->nameservers, list) {
2198 struct dgram_conn *dgram = NULL;
2199 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002200
Christopher Faulet67957bd2017-09-27 11:00:59 +02002201 /* Check nameserver info */
2202 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002203 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2204 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002205 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002206 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002207 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002208 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002209 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2210 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002211 close(fd);
2212 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002213 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002214 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002215 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002216
Christopher Faulet67957bd2017-09-27 11:00:59 +02002217 /* Create dgram structure that will hold the UPD socket
2218 * and attach it on the current nameserver */
2219 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002220 ha_alert("config: resolvers '%s' : out of memory.\n",
2221 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002222 err_code |= (ERR_ALERT|ERR_ABORT);
2223 goto err;
2224 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002225
Christopher Faulet67957bd2017-09-27 11:00:59 +02002226 /* Leave dgram partially initialized, no FD attached for
2227 * now. */
2228 dgram->owner = ns;
2229 dgram->data = &resolve_dgram_cb;
2230 dgram->t.sock.fd = -1;
2231 ns->dgram = dgram;
2232 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002233
Christopher Faulet67957bd2017-09-27 11:00:59 +02002234 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002235 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002236 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002237 err_code |= (ERR_ALERT|ERR_ABORT);
2238 goto err;
2239 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002240
Christopher Faulet67957bd2017-09-27 11:00:59 +02002241 /* Update task's parameters */
2242 t->process = dns_process_resolvers;
2243 t->context = resolvers;
2244 resolvers->t = t;
2245 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002246 }
2247
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002248 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002249 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002250
Christopher Faulet67957bd2017-09-27 11:00:59 +02002251 for (srv = px->srv; srv; srv = srv->next) {
2252 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002253
Christopher Faulet67957bd2017-09-27 11:00:59 +02002254 if (!srv->resolvers_id)
2255 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002256
Christopher Faulet67957bd2017-09-27 11:00:59 +02002257 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002258 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2259 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002260 err_code |= (ERR_ALERT|ERR_ABORT);
2261 continue;
2262 }
2263 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002264
Christopher Faulet67957bd2017-09-27 11:00:59 +02002265 if (srv->srvrq && !srv->srvrq->resolvers) {
2266 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002267 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002268 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2269 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002270 err_code |= (ERR_ALERT|ERR_ABORT);
2271 continue;
2272 }
2273 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002274 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2276 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002277 err_code |= (ERR_ALERT|ERR_ABORT);
2278 continue;
2279 }
2280 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002281 }
2282
Christopher Faulet67957bd2017-09-27 11:00:59 +02002283 if (err_code & (ERR_ALERT|ERR_ABORT))
2284 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002285
Christopher Faulet67957bd2017-09-27 11:00:59 +02002286 return err_code;
2287 err:
2288 dns_deinit();
2289 return err_code;
2290
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002291}
2292
Christopher Faulet67957bd2017-09-27 11:00:59 +02002293/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002294static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002295{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002296 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002297
Christopher Faulet67957bd2017-09-27 11:00:59 +02002298 if (*args[2]) {
2299 list_for_each_entry(presolvers, &dns_resolvers, list) {
2300 if (strcmp(presolvers->id, args[2]) == 0) {
2301 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002302 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002303 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002304 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002305 if (appctx->ctx.cli.p0 == NULL)
2306 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002307 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002308 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002309}
2310
Christopher Faulet67957bd2017-09-27 11:00:59 +02002311/* Dumps counters from all resolvers section and associated name servers. It
2312 * returns 0 if the output buffer is full and it needs to be called again,
2313 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002314 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002315 */
2316static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2317{
2318 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002319 struct dns_resolvers *resolvers;
2320 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002321
2322 chunk_reset(&trash);
2323
2324 switch (appctx->st2) {
2325 case STAT_ST_INIT:
2326 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2327 /* fall through */
2328
2329 case STAT_ST_LIST:
2330 if (LIST_ISEMPTY(&dns_resolvers)) {
2331 chunk_appendf(&trash, "No resolvers found\n");
2332 }
2333 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002334 list_for_each_entry(resolvers, &dns_resolvers, list) {
2335 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002336 continue;
2337
Christopher Faulet67957bd2017-09-27 11:00:59 +02002338 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2339 list_for_each_entry(ns, &resolvers->nameservers, list) {
2340 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2341 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2342 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2343 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2344 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2345 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2346 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2347 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2348 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2349 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2350 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2351 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2352 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2353 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2354 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2355 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002356 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002357 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002358 }
2359 }
2360
2361 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002362 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002363 /* let's try again later from this session. We add ourselves into
2364 * this session's users so that it can remove us upon termination.
2365 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002366 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002367 return 0;
2368 }
William Lallemand69e96442016-11-19 00:58:54 +01002369 /* fall through */
2370
2371 default:
2372 appctx->st2 = STAT_ST_FIN;
2373 return 1;
2374 }
2375}
2376
2377/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002378static struct cli_kw_list cli_kws = {{ }, {
2379 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2380 " associated name servers",
2381 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2382 {{},}
2383 }
2384};
William Lallemand69e96442016-11-19 00:58:54 +01002385
Willy Tarreau0108d902018-11-25 19:14:37 +01002386INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002387
Baptiste Assmann333939c2019-01-21 08:34:50 +01002388/*
2389 * Prepare <rule> for hostname resolution.
2390 * Returns -1 in case of any allocation failure, 0 if not.
2391 * On error, a global failure counter is also incremented.
2392 */
2393static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2394{
2395 char *hostname_dn;
2396 int hostname_len, hostname_dn_len;
2397 struct buffer *tmp = get_trash_chunk();
2398
2399 if (!hostname)
2400 return 0;
2401
2402 hostname_len = strlen(hostname);
2403 hostname_dn = tmp->area;
2404 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2405 hostname_dn, tmp->size);
2406 if (hostname_dn_len == -1)
2407 goto err;
2408
2409
2410 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2411 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2412 if (!stream->dns_ctx.hostname_dn)
2413 goto err;
2414
2415 return 0;
2416
2417 err:
2418 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2419 dns_failed_resolutions += 1;
2420 return -1;
2421}
2422
2423
2424/*
2425 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2426 */
2427enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2428 struct session *sess, struct stream *s, int flags)
2429{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002430 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002431 struct sample *smp;
2432 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002433 struct dns_requester *req;
2434 struct dns_resolvers *resolvers;
2435 struct dns_resolution *res;
Christopher Faulet5098a082020-07-22 11:46:32 +02002436 int exp, locked = 0;
2437 enum act_return ret = ACT_RET_CONT;
2438
2439 resolvers = rule->arg.dns.resolvers;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002440
2441 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002442 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002443 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2444 resolution = s->dns_ctx.dns_requester->resolution;
Christopher Faulet5098a082020-07-22 11:46:32 +02002445 if (!locked) {
2446 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2447 locked = 1;
2448 }
2449
Christopher Faulet385101e2020-07-28 10:21:54 +02002450 if (resolution->step == RSLV_STEP_RUNNING)
2451 goto yield;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002452 if (resolution->step == RSLV_STEP_NONE) {
2453 /* We update the variable only if we have a valid response. */
2454 if (resolution->status == RSLV_STATUS_VALID) {
2455 struct sample smp;
2456 short ip_sin_family = 0;
2457 void *ip = NULL;
2458
Christopher Fauleta4168432020-01-24 18:08:42 +01002459 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002460 0, &ip, &ip_sin_family, NULL);
2461
2462 switch (ip_sin_family) {
2463 case AF_INET:
2464 smp.data.type = SMP_T_IPV4;
2465 memcpy(&smp.data.u.ipv4, ip, 4);
2466 break;
2467 case AF_INET6:
2468 smp.data.type = SMP_T_IPV6;
2469 memcpy(&smp.data.u.ipv6, ip, 16);
2470 break;
2471 default:
2472 ip = NULL;
2473 }
2474
2475 if (ip) {
2476 smp.px = px;
2477 smp.sess = sess;
2478 smp.strm = s;
2479
2480 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2481 }
2482 }
2483 }
2484
Christopher Faulet385101e2020-07-28 10:21:54 +02002485 goto release_requester;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002486 }
2487
2488 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002489 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2490 if (smp == NULL)
Christopher Faulet5098a082020-07-22 11:46:32 +02002491 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002492
Willy Tarreau45726fd2019-07-17 10:38:45 +02002493 fqdn = smp->data.u.str.area;
2494 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet5098a082020-07-22 11:46:32 +02002495 goto end; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002496
Willy Tarreau45726fd2019-07-17 10:38:45 +02002497 s->dns_ctx.parent = rule;
Christopher Faulet5098a082020-07-22 11:46:32 +02002498
2499 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2500 locked = 1;
2501
Willy Tarreau45726fd2019-07-17 10:38:45 +02002502 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002503
2504 /* Check if there is a fresh enough response in the cache of our associated resolution */
2505 req = s->dns_ctx.dns_requester;
Christopher Faulet385101e2020-07-28 10:21:54 +02002506 if (!req || !req->resolution)
2507 goto release_requester; /* on error, ignore the action */
Christopher Faulet5098a082020-07-22 11:46:32 +02002508 res = req->resolution;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002509
2510 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2511 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
Christopher Faulet385101e2020-07-28 10:21:54 +02002512 && !tick_is_expired(exp, now_ms)) {
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002513 goto use_cache;
2514 }
2515
Willy Tarreau45726fd2019-07-17 10:38:45 +02002516 dns_trigger_resolution(s->dns_ctx.dns_requester);
Christopher Faulet385101e2020-07-28 10:21:54 +02002517
2518 yield:
2519 if (flags & ACT_OPT_FINAL)
2520 goto release_requester;
Christopher Faulet5098a082020-07-22 11:46:32 +02002521 ret = ACT_RET_YIELD;
2522
2523 end:
2524 if (locked)
2525 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2526 return ret;
Christopher Faulet385101e2020-07-28 10:21:54 +02002527
2528 release_requester:
2529 free(s->dns_ctx.hostname_dn);
2530 s->dns_ctx.hostname_dn = NULL;
2531 s->dns_ctx.hostname_dn_len = 0;
2532 if (s->dns_ctx.dns_requester) {
2533 dns_unlink_resolution(s->dns_ctx.dns_requester);
2534 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2535 s->dns_ctx.dns_requester = NULL;
2536 }
2537 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002538}
2539
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002540static void release_dns_action(struct act_rule *rule)
2541{
2542 release_sample_expr(rule->arg.dns.expr);
2543 free(rule->arg.dns.varname);
2544 free(rule->arg.dns.resolvers_id);
2545 free(rule->arg.dns.dns_opts);
2546}
2547
Baptiste Assmann333939c2019-01-21 08:34:50 +01002548
2549/* parse "do-resolve" action
2550 * This action takes the following arguments:
2551 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2552 *
2553 * - <varName> is the variable name where the result of the DNS resolution will be stored
2554 * (mandatory)
2555 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2556 * (mandatory)
2557 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2558 * (optional), defaults to ipv6
2559 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2560 */
2561enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2562{
2563 int cur_arg;
2564 struct sample_expr *expr;
2565 unsigned int where;
2566 const char *beg, *end;
2567
2568 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2569 cur_arg = *orig_arg - 1;
2570
2571 /* locate varName, which is mandatory */
2572 beg = strchr(args[cur_arg], '(');
2573 if (beg == NULL)
2574 goto do_resolve_parse_error;
2575 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2576 end = strchr(beg, ',');
2577 if (end == NULL)
2578 goto do_resolve_parse_error;
2579 rule->arg.dns.varname = my_strndup(beg, end - beg);
2580 if (rule->arg.dns.varname == NULL)
2581 goto do_resolve_parse_error;
2582
2583
2584 /* locate resolversSectionName, which is mandatory.
2585 * Since next parameters are optional, the delimiter may be comma ','
2586 * or closing parenthesis ')'
2587 */
2588 beg = end + 1;
2589 end = strchr(beg, ',');
2590 if (end == NULL)
2591 end = strchr(beg, ')');
2592 if (end == NULL)
2593 goto do_resolve_parse_error;
2594 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2595 if (rule->arg.dns.resolvers_id == NULL)
2596 goto do_resolve_parse_error;
2597
2598
Christopher Fauleta4168432020-01-24 18:08:42 +01002599 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2600 if (rule->arg.dns.dns_opts == NULL)
2601 goto do_resolve_parse_error;
2602
Baptiste Assmann333939c2019-01-21 08:34:50 +01002603 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002604 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002605
2606 /* optional arguments accepted for now:
2607 * ipv4 or ipv6
2608 */
2609 while (*end != ')') {
2610 beg = end + 1;
2611 end = strchr(beg, ',');
2612 if (end == NULL)
2613 end = strchr(beg, ')');
2614 if (end == NULL)
2615 goto do_resolve_parse_error;
2616
2617 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002618 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002619 }
2620 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002621 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002622 }
2623 else {
2624 goto do_resolve_parse_error;
2625 }
2626 }
2627
2628 cur_arg = cur_arg + 1;
2629
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002630 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 +01002631 if (!expr)
2632 goto do_resolve_parse_error;
2633
2634
2635 where = 0;
2636 if (px->cap & PR_CAP_FE)
2637 where |= SMP_VAL_FE_HRQ_HDR;
2638 if (px->cap & PR_CAP_BE)
2639 where |= SMP_VAL_BE_HRQ_HDR;
2640
2641 if (!(expr->fetch->val & where)) {
2642 memprintf(err,
2643 "fetch method '%s' extracts information from '%s', none of which is available here",
2644 args[cur_arg-1], sample_src_names(expr->fetch->use));
2645 free(expr);
2646 return ACT_RET_PRS_ERR;
2647 }
2648 rule->arg.dns.expr = expr;
2649 rule->action = ACT_CUSTOM;
2650 rule->action_ptr = dns_action_do_resolve;
2651 *orig_arg = cur_arg;
2652
2653 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002654 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002655
2656 return ACT_RET_PRS_OK;
2657
2658 do_resolve_parse_error:
2659 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2660 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2661 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2662 args[cur_arg]);
2663 return ACT_RET_PRS_ERR;
2664}
2665
2666static struct action_kw_list http_req_kws = { { }, {
2667 { "do-resolve", dns_parse_do_resolve, 1 },
2668 { /* END */ }
2669}};
2670
2671INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2672
2673static struct action_kw_list tcp_req_cont_actions = {ILH, {
2674 { "do-resolve", dns_parse_do_resolve, 1 },
2675 { /* END */ }
2676}};
2677
2678INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2679
2680/* Check an "http-request do-resolve" action.
2681 *
2682 * The function returns 1 in success case, otherwise, it returns 0 and err is
2683 * filled.
2684 */
2685int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2686{
2687 struct dns_resolvers *resolvers = NULL;
2688
2689 if (rule->arg.dns.resolvers_id == NULL) {
2690 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2691 return 0;
2692 }
2693
2694 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2695 if (resolvers == NULL) {
2696 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2697 return 0;
2698 }
2699 rule->arg.dns.resolvers = resolvers;
2700
2701 return 1;
2702}
2703
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002704REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002705REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);