blob: 4ef27494534365092a0c695a49b413b22c4d7915 [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>
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +020039#include <haproxy/stats.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
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +020060enum {
61 DNS_STAT_ID,
62 DNS_STAT_SND_ERROR,
63 DNS_STAT_VALID,
64 DNS_STAT_UPDATE,
65 DNS_STAT_CNAME,
66 DNS_STAT_CNAME_ERROR,
67 DNS_STAT_ANY_ERR,
68 DNS_STAT_NX,
69 DNS_STAT_TIMEOUT,
70 DNS_STAT_REFUSED,
71 DNS_STAT_OTHER,
72 DNS_STAT_INVALID,
73 DNS_STAT_TOO_BIG,
74 DNS_STAT_TRUNCATED,
75 DNS_STAT_OUTDATED,
76 DNS_STAT_END,
77};
78
79static struct name_desc dns_stats[] = {
80 [DNS_STAT_ID] = { .name = "id", .desc = "ID" },
81 [DNS_STAT_SND_ERROR] = { .name = "send_error", .desc = "Send error" },
82 [DNS_STAT_VALID] = { .name = "valid", .desc = "Valid" },
83 [DNS_STAT_UPDATE] = { .name = "update", .desc = "Update" },
84 [DNS_STAT_CNAME] = { .name = "cname", .desc = "CNAME" },
85 [DNS_STAT_CNAME_ERROR] = { .name = "cname_error", .desc = "CNAME error" },
86 [DNS_STAT_ANY_ERR] = { .name = "any_err", .desc = "Any errors" },
87 [DNS_STAT_NX] = { .name = "nx", .desc = "NX" },
88 [DNS_STAT_TIMEOUT] = { .name = "timeout", .desc = "Timeout" },
89 [DNS_STAT_REFUSED] = { .name = "refused", .desc = "Refused" },
90 [DNS_STAT_OTHER] = { .name = "other", .desc = "Other" },
91 [DNS_STAT_INVALID] = { .name = "invalid", .desc = "Invalid" },
92 [DNS_STAT_TOO_BIG] = { .name = "too_big", .desc = "Too big" },
93 [DNS_STAT_TRUNCATED] = { .name = "truncated", .desc = "Truncated" },
94 [DNS_STAT_OUTDATED] = { .name = "outdated", .desc = "Outdated" },
95};
96
97static struct dns_counters dns_counters;
98
99static void dns_fill_stats(void *d, struct field *stats)
100{
101 struct dns_counters *counters = d;
102 stats[DNS_STAT_ID] = mkf_str(FO_CONFIG, counters->id);
103 stats[DNS_STAT_SND_ERROR] = mkf_u64(FN_GAUGE, counters->snd_error);
104 stats[DNS_STAT_VALID] = mkf_u64(FN_GAUGE, counters->valid);
105 stats[DNS_STAT_UPDATE] = mkf_u64(FN_GAUGE, counters->update);
106 stats[DNS_STAT_CNAME] = mkf_u64(FN_GAUGE, counters->cname);
107 stats[DNS_STAT_CNAME_ERROR] = mkf_u64(FN_GAUGE, counters->cname_error);
108 stats[DNS_STAT_ANY_ERR] = mkf_u64(FN_GAUGE, counters->any_err);
109 stats[DNS_STAT_NX] = mkf_u64(FN_GAUGE, counters->nx);
110 stats[DNS_STAT_TIMEOUT] = mkf_u64(FN_GAUGE, counters->timeout);
111 stats[DNS_STAT_REFUSED] = mkf_u64(FN_GAUGE, counters->refused);
112 stats[DNS_STAT_OTHER] = mkf_u64(FN_GAUGE, counters->other);
113 stats[DNS_STAT_INVALID] = mkf_u64(FN_GAUGE, counters->invalid);
114 stats[DNS_STAT_TOO_BIG] = mkf_u64(FN_GAUGE, counters->too_big);
115 stats[DNS_STAT_TRUNCATED] = mkf_u64(FN_GAUGE, counters->truncated);
116 stats[DNS_STAT_OUTDATED] = mkf_u64(FN_GAUGE, counters->outdated);
117}
118
119static struct stats_module dns_stats_module = {
120 .name = "dns",
121 .domain_flags = STATS_DOMAIN_DNS << STATS_DOMAIN,
122 .fill_stats = dns_fill_stats,
123 .stats = dns_stats,
124 .stats_count = DNS_STAT_END,
125 .counters = &dns_counters,
126 .counters_size = sizeof(dns_counters),
127 .clearable = 0,
128};
129
130INITCALL1(STG_REGISTER, stats_register_module, &dns_stats_module);
131
Christopher Faulet67957bd2017-09-27 11:00:59 +0200132/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
133 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200134 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200135struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200137 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200138
Christopher Faulet67957bd2017-09-27 11:00:59 +0200139 list_for_each_entry(res, &dns_resolvers, list) {
140 if (!strcmp(res->id, id))
141 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200142 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200143 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200144}
145
Olivier Houchardb17b8842020-04-01 18:30:27 +0200146/* Compare hostnames in a case-insensitive way .
147 * Returns 0 if they are the same, non-zero otherwise
148 */
149static __inline int dns_hostname_cmp(const char *name1, const char *name2, int len)
150{
151 int i;
152
153 for (i = 0; i < len; i++)
Willy Tarreauf278eec2020-07-05 21:46:32 +0200154 if (tolower((unsigned char)name1[i]) != tolower((unsigned char)name2[i]))
Olivier Houchardb17b8842020-04-01 18:30:27 +0200155 return -1;
156 return 0;
157}
158
Christopher Faulet67957bd2017-09-27 11:00:59 +0200159/* Returns a pointer on the SRV request matching the name <name> for the proxy
160 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200161 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200163{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200165
Christopher Faulet67957bd2017-09-27 11:00:59 +0200166 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
167 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
168 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200169 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200170 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200171}
172
Christopher Faulet67957bd2017-09-27 11:00:59 +0200173/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
174 * NULL if an error occurred. */
175struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200176{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200177 struct proxy *px = srv->proxy;
178 struct dns_srvrq *srvrq = NULL;
179 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200180
Christopher Faulet67957bd2017-09-27 11:00:59 +0200181 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200182 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
183 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200184 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100185 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
186 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200188 }
189
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100191 ha_alert("config : %s '%s', server '%s': out of memory\n",
192 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200193 goto err;
194 }
195 srvrq->obj_type = OBJ_TYPE_SRVRQ;
196 srvrq->proxy = px;
197 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200198 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 srvrq->hostname_dn_len = hostname_dn_len;
200 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100201 ha_alert("config : %s '%s', server '%s': out of memory\n",
202 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200203 goto err;
204 }
205 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
206 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200207
Christopher Faulet67957bd2017-09-27 11:00:59 +0200208 err:
209 if (srvrq) {
210 free(srvrq->name);
211 free(srvrq->hostname_dn);
212 free(srvrq);
213 }
214 return NULL;
215}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200216
Baptiste Assmann99348c32021-03-10 12:22:18 +0100217/* finds and return the SRV answer item associated to a requester (whose type is 'server').
218 *
219 * returns NULL in case of error or not found.
220 */
221struct dns_answer_item *find_srvrq_answer_record(const struct dns_requester *requester)
222{
223 struct dns_resolution *res;
224 struct dns_answer_item *item;
225 struct server *srv;
226
227 if (!requester)
228 return NULL;
229
230 if ((srv = objt_server(requester->owner)) == NULL)
231 return NULL;
232 /* check if the server is managed by a SRV record */
233 if (srv->srvrq == NULL)
234 return NULL;
235
236 res = srv->srvrq->dns_requester->resolution;
237 /* search an ANSWER record whose target points to the server's hostname and whose port is
238 * the same as server's svc_port */
239 list_for_each_entry(item, &res->response.answer_list, list) {
240 if (dns_hostname_cmp(srv->hostname_dn, item->target, srv->hostname_dn_len) == 0 &&
241 (srv->svc_port == item->port))
242 return item;
243 }
244
245 return NULL;
246}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200247
Christopher Faulet67957bd2017-09-27 11:00:59 +0200248/* 2 bytes random generator to generate DNS query ID */
249static inline uint16_t dns_rnd16(void)
250{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200251 if (!dns_query_id_seed)
252 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200253 dns_query_id_seed ^= dns_query_id_seed << 13;
254 dns_query_id_seed ^= dns_query_id_seed >> 7;
255 dns_query_id_seed ^= dns_query_id_seed << 17;
256 return dns_query_id_seed;
257}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200258
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200259
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260static inline int dns_resolution_timeout(struct dns_resolution *res)
261{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100262 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200263}
264
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265/* Updates a resolvers' task timeout for next wake up and queue it */
266static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200267{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200268 struct dns_resolution *res;
269 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200270
Christopher Faulet67957bd2017-09-27 11:00:59 +0200271 next = tick_add(now_ms, resolvers->timeout.resolve);
272 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
273 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
274 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
275 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200276
Christopher Faulet67957bd2017-09-27 11:00:59 +0200277 list_for_each_entry(res, &resolvers->resolutions.wait, list)
278 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200279
Christopher Faulet67957bd2017-09-27 11:00:59 +0200280 resolvers->t->expire = next;
281 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200282}
283
Christopher Faulet67957bd2017-09-27 11:00:59 +0200284/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
285 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200286 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200288{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200289 struct dgram_conn *dgram = ns->dgram;
290 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200291
Christopher Faulet67957bd2017-09-27 11:00:59 +0200292 /* Already connected */
293 if (dgram->t.sock.fd != -1)
294 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200295
Christopher Faulet67957bd2017-09-27 11:00:59 +0200296 /* Create an UDP socket and connect it on the nameserver's IP/Port */
297 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
298 send_log(NULL, LOG_WARNING,
299 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
300 ns->resolvers->id, ns->id);
301 return -1;
302 }
303 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
304 send_log(NULL, LOG_WARNING,
305 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
306 ns->resolvers->id, ns->id);
307 close(fd);
308 return -1;
309 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200310
Christopher Faulet67957bd2017-09-27 11:00:59 +0200311 /* Make the socket non blocking */
312 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200313
Christopher Faulet67957bd2017-09-27 11:00:59 +0200314 /* Add the fd in the fd list and update its parameters */
315 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100316 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200317 fd_want_recv(fd);
318 return 0;
319}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200320
Christopher Faulet67957bd2017-09-27 11:00:59 +0200321/* Forges a DNS query. It needs the following information from the caller:
322 * - <query_id> : the DNS query id corresponding to this query
323 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
324 * - <hostname_dn> : hostname in domain name format
325 * - <hostname_dn_len> : length of <hostname_dn>
326 *
327 * To store the query, the caller must pass a buffer <buf> and its size
328 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
329 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200330 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200331static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
332 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200334 struct dns_header dns_hdr;
335 struct dns_question qinfo;
336 struct dns_additional_record edns;
337 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200338
Christopher Faulet67957bd2017-09-27 11:00:59 +0200339 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
340 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200341
Christopher Faulet67957bd2017-09-27 11:00:59 +0200342 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200343
Christopher Faulet67957bd2017-09-27 11:00:59 +0200344 /* Set dns query headers */
345 dns_hdr.id = (unsigned short) htons(query_id);
346 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
347 dns_hdr.qdcount = htons(1); /* 1 question */
348 dns_hdr.ancount = 0;
349 dns_hdr.nscount = 0;
350 dns_hdr.arcount = htons(1);
351 memcpy(p, &dns_hdr, sizeof(dns_hdr));
352 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200353
Christopher Faulet67957bd2017-09-27 11:00:59 +0200354 /* Set up query hostname */
355 memcpy(p, hostname_dn, hostname_dn_len);
356 p += hostname_dn_len;
357 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200358
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359 /* Set up query info (type and class) */
360 qinfo.qtype = htons(query_type);
361 qinfo.qclass = htons(DNS_RCLASS_IN);
362 memcpy(p, &qinfo, sizeof(qinfo));
363 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200364
Christopher Faulet67957bd2017-09-27 11:00:59 +0200365 /* Set the DNS extension */
366 edns.name = 0;
367 edns.type = htons(DNS_RTYPE_OPT);
368 edns.udp_payload_size = htons(accepted_payload_size);
369 edns.extension = 0;
370 edns.data_length = 0;
371 memcpy(p, &edns, sizeof(edns));
372 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200373
Christopher Faulet67957bd2017-09-27 11:00:59 +0200374 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200375}
376
Christopher Faulet67957bd2017-09-27 11:00:59 +0200377/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
378 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200379 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200380static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200381{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200382 struct dns_resolvers *resolvers = resolution->resolvers;
383 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100384 int len;
385
386 /* Update resolution */
387 resolution->nb_queries = 0;
388 resolution->nb_responses = 0;
389 resolution->last_query = now_ms;
390
391 len = dns_build_query(resolution->query_id, resolution->query_type,
392 resolvers->accepted_payload_size,
393 resolution->hostname_dn, resolution->hostname_dn_len,
394 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200395
Christopher Faulet67957bd2017-09-27 11:00:59 +0200396 list_for_each_entry(ns, &resolvers->nameservers, list) {
397 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100398 int ret;
399
Christopher Faulet67957bd2017-09-27 11:00:59 +0200400 if (fd == -1) {
401 if (dns_connect_namesaver(ns) == -1)
402 continue;
403 fd = ns->dgram->t.sock.fd;
404 resolvers->nb_nameservers++;
405 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200406
Willy Tarreau0eae6322019-12-20 11:18:54 +0100407 if (len < 0)
408 goto snd_error;
409
410 ret = send(fd, trash.area, len, 0);
411 if (ret == len) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200412 ns->counters->sent++;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100413 resolution->nb_queries++;
414 continue;
415 }
Emeric Brun751872e2021-05-04 18:16:53 +0200416 else if (ret == -1) {
417 if (errno == EAGAIN) {
418 /* retry once the socket is ready */
419 fd_cant_send(fd);
420 continue;
421 }
Willy Tarreau0eae6322019-12-20 11:18:54 +0100422
Emeric Brun751872e2021-05-04 18:16:53 +0200423 /* purge the fd and set sock.fd to -1
424 * to create a new one during next
425 * send_query attempt to the same
426 * nameserver
427 */
428 fd_delete(fd);
429 ns->dgram->t.sock.fd = -1;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100430 }
431
432 snd_error:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200433 ns->counters->snd_error++;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100434 resolution->nb_queries++;
435 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200436
Christopher Faulet67957bd2017-09-27 11:00:59 +0200437 /* Push the resolution at the end of the active list */
438 LIST_DEL(&resolution->list);
439 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
440 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200441}
442
Christopher Faulet67957bd2017-09-27 11:00:59 +0200443/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
444 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200445 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200446static int
447dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200448{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200449 struct dns_resolvers *resolvers = resolution->resolvers;
450 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200451
Christopher Faulet67957bd2017-09-27 11:00:59 +0200452 /* Avoid sending requests for resolutions that don't yet have an
453 * hostname, ie resolutions linked to servers that do not yet have an
454 * fqdn */
455 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200456 return 0;
457
Christopher Faulet67957bd2017-09-27 11:00:59 +0200458 /* Check if a resolution has already been started for this server return
459 * directly to avoid resolution pill up. */
460 if (resolution->step != RSLV_STEP_NONE)
461 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200462
Christopher Faulet67957bd2017-09-27 11:00:59 +0200463 /* Generates a new query id. We try at most 100 times to find a free
464 * query id */
465 for (i = 0; i < 100; ++i) {
466 query_id = dns_rnd16();
467 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200468 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200469 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200470 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200471 if (query_id == -1) {
472 send_log(NULL, LOG_NOTICE,
473 "could not generate a query id for %s, in resolvers %s.\n",
474 resolution->hostname_dn, resolvers->id);
475 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200476 }
477
Christopher Faulet67957bd2017-09-27 11:00:59 +0200478 /* Update resolution parameters */
479 resolution->query_id = query_id;
480 resolution->qid.key = query_id;
481 resolution->step = RSLV_STEP_RUNNING;
482 resolution->query_type = resolution->prefered_query_type;
483 resolution->try = resolvers->resolve_retries;
484 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200485
Christopher Faulet67957bd2017-09-27 11:00:59 +0200486 /* Send the DNS query */
487 resolution->try -= 1;
488 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200489 return 1;
490}
491
Christopher Faulet67957bd2017-09-27 11:00:59 +0200492/* Performs a name resolution for the requester <req> */
493void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200494{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200495 struct dns_resolvers *resolvers;
496 struct dns_resolution *res;
497 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200498
Christopher Faulet67957bd2017-09-27 11:00:59 +0200499 if (!req || !req->resolution)
500 return;
501 res = req->resolution;
502 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200503
Christopher Faulet67957bd2017-09-27 11:00:59 +0200504 /* The resolution must not be triggered yet. Use the cached response, if
505 * valid */
506 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200507 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
508 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
509 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200510}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200511
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200512
Christopher Faulet67957bd2017-09-27 11:00:59 +0200513/* Resets some resolution parameters to initial values and also delete the query
514 * ID from the resolver's tree.
515 */
516static void dns_reset_resolution(struct dns_resolution *resolution)
517{
518 /* update resolution status */
519 resolution->step = RSLV_STEP_NONE;
520 resolution->try = 0;
521 resolution->last_resolution = now_ms;
522 resolution->nb_queries = 0;
523 resolution->nb_responses = 0;
524 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200525
Christopher Faulet67957bd2017-09-27 11:00:59 +0200526 /* clean up query id */
527 eb32_delete(&resolution->qid);
528 resolution->query_id = 0;
529 resolution->qid.key = 0;
530}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200531
Christopher Faulet67957bd2017-09-27 11:00:59 +0200532/* Returns the query id contained in a DNS response */
533static inline unsigned short dns_response_get_query_id(unsigned char *resp)
534{
535 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200536}
537
Christopher Faulet67957bd2017-09-27 11:00:59 +0200538
539/* Analyses, re-builds and copies the name <name> from the DNS response packet
540 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
541 * for compressed data. The result is copied into <dest>, ensuring we don't
542 * overflow using <dest_len> Returns the number of bytes the caller can move
543 * forward. If 0 it means an error occurred while parsing the name. <offset> is
544 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200545 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200546int dns_read_name(unsigned char *buffer, unsigned char *bufend,
547 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100548 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200549{
550 int nb_bytes = 0, n = 0;
551 int label_len;
552 unsigned char *reader = name;
553 char *dest = destination;
554
555 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100556 if (reader >= bufend)
557 goto err;
558
Christopher Faulet67957bd2017-09-27 11:00:59 +0200559 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200560 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100561 if (reader + 1 >= bufend)
562 goto err;
563
Christopher Faulet67957bd2017-09-27 11:00:59 +0200564 /* Must point BEFORE current position */
565 if ((buffer + reader[1]) > reader)
566 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200567
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100568 if (depth++ > 100)
569 goto err;
570
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530571 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100572 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200573 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200574 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200575
Christopher Faulet67957bd2017-09-27 11:00:59 +0200576 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200577 nb_bytes += n;
578 goto out;
579 }
580
581 label_len = *reader;
582 if (label_len == 0)
583 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200584
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200585 /* Check if:
586 * - we won't read outside the buffer
587 * - there is enough place in the destination
588 */
589 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200590 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200591
592 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200593 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200594
595 memcpy(dest, reader, label_len);
596
Christopher Faulet67957bd2017-09-27 11:00:59 +0200597 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200598 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200599 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200600 }
601
Christopher Faulet67957bd2017-09-27 11:00:59 +0200602 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200603 /* offset computation:
604 * parse from <name> until finding either NULL or a pointer "c0xx"
605 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200606 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200607 *offset = 0;
608 while (reader < bufend) {
609 if ((reader[0] & 0xc0) == 0xc0) {
610 *offset += 2;
611 break;
612 }
613 else if (*reader == 0) {
614 *offset += 1;
615 break;
616 }
617 *offset += 1;
618 ++reader;
619 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200620 return nb_bytes;
621
Christopher Faulet67957bd2017-09-27 11:00:59 +0200622 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200623 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200624}
625
Christopher Faulet67957bd2017-09-27 11:00:59 +0200626/* Checks for any obsolete record, also identify any SRV request, and try to
627 * find a corresponding server.
628*/
629static void dns_check_dns_response(struct dns_resolution *res)
630{
631 struct dns_resolvers *resolvers = res->resolvers;
632 struct dns_requester *req, *reqback;
633 struct dns_answer_item *item, *itemback;
634 struct server *srv;
635 struct dns_srvrq *srvrq;
636
637 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +0200638 struct dns_answer_item *ar_item = item->ar_item;
639
640 /* clean up obsolete Additional record */
Christopher Faulet8b6d5b02021-03-11 09:36:05 +0100641 if (ar_item && tick_is_lt(tick_add(ar_item->last_seen, resolvers->hold.obsolete), now_ms)) {
Christopher Faulet02cf7b72021-03-10 18:38:37 +0100642 /* Cleaning up the AR item will trigger an extra DNS resolution, except if the SRV
643 * item is also obsolete.
644 */
Christopher Faulet5a891752020-09-08 10:06:01 +0200645 pool_free(dns_answer_item_pool, ar_item);
646 item->ar_item = NULL;
647 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200648
649 /* Remove obsolete items */
Christopher Faulet8b6d5b02021-03-11 09:36:05 +0100650 if (tick_is_lt(tick_add(item->last_seen, resolvers->hold.obsolete), now_ms)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200651 if (item->type != DNS_RTYPE_SRV)
652 goto rm_obselete_item;
653
654 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
655 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
656 continue;
657
658 /* Remove any associated server */
659 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100660 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200661 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
662 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200663 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet119ec522021-03-11 18:09:53 +0100664 dns_unlink_resolution(srv->dns_requester, 0);
Christopher Faulet07cb0e12021-03-11 18:06:23 +0100665 srvrq_update_srv_status(srv, 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200666 free(srv->hostname);
667 free(srv->hostname_dn);
668 srv->hostname = NULL;
669 srv->hostname_dn = NULL;
670 srv->hostname_dn_len = 0;
Christopher Faulet8427aa62021-02-23 12:24:09 +0100671 memset(&srv->addr, 0, sizeof(srv->addr));
672 srv->svc_port = 0;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200673 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100674 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200675 }
676 }
677
678 rm_obselete_item:
679 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +0200680 if (item->ar_item) {
681 pool_free(dns_answer_item_pool, item->ar_item);
682 item->ar_item = NULL;
683 }
Willy Tarreaubafbe012017-11-24 17:34:44 +0100684 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200685 continue;
686 }
687
688 if (item->type != DNS_RTYPE_SRV)
689 continue;
690
691 /* Now process SRV records */
692 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
693 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
694 continue;
695
696 /* Check if a server already uses that hostname */
697 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100698 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200699 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
700 item->data_len == srv->hostname_dn_len &&
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200701 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200702 break;
703 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100704 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200705 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200706
707 /* If not, try to find a server with undefined hostname */
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200708 if (!srv) {
709 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
710 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
711 if (srv->srvrq == srvrq && !srv->hostname_dn)
712 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100713 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100714 }
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200715 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200716
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200717 /* And update this server, if found (srv is locked here) */
718 if (srv) {
Christopher Faulet02cf7b72021-03-10 18:38:37 +0100719 /* re-enable DNS resolution for this server by default */
720 srv->flags &= ~SRV_F_NO_RESOLUTION;
721
Baptiste Assmann13a92322019-06-07 09:40:55 +0200722 /* Check if an Additional Record is associated to this SRV record.
723 * Perform some sanity checks too to ensure the record can be used.
724 * If all fine, we simply pick up the IP address found and associate
Christopher Faulet02cf7b72021-03-10 18:38:37 +0100725 * it to the server. And DNS resolution is disabled for this server.
Baptiste Assmann13a92322019-06-07 09:40:55 +0200726 */
727 if ((item->ar_item != NULL) &&
728 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
Christopher Faulet02cf7b72021-03-10 18:38:37 +0100729 {
Baptiste Assmann13a92322019-06-07 09:40:55 +0200730
731 switch (item->ar_item->type) {
732 case DNS_RTYPE_A:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200733 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 +0200734 break;
735 case DNS_RTYPE_AAAA:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200736 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 +0200737 break;
738 }
739
740 srv->flags |= SRV_F_NO_RESOLUTION;
Christopher Faulet02cf7b72021-03-10 18:38:37 +0100741
742 /* Unlink A/AAAA resolution for this server if there is an AR item.
743 * It is usless to perform an extra resolution
744 */
Christopher Faulet119ec522021-03-11 18:09:53 +0100745 dns_unlink_resolution(srv->dns_requester, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +0200746 }
747
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200748 if (!srv->hostname_dn) {
749 const char *msg = NULL;
750 char hostname[DNS_MAX_NAME_SIZE];
751
752 if (dns_dn_label_to_str(item->target, item->data_len+1,
753 hostname, DNS_MAX_NAME_SIZE) == -1) {
754 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
755 continue;
756 }
757 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
758 if (msg)
759 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
760 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200761
Christopher Faulet02cf7b72021-03-10 18:38:37 +0100762 if (!(srv->flags & SRV_F_NO_RESOLUTION)) {
763 /* If there is no AR item responsible of the FQDN resolution,
764 * trigger a dedicated DNS resolution
765 */
766 if (!srv->dns_requester || !srv->dns_requester->resolution)
767 dns_link_resolution(srv, OBJ_TYPE_SERVER, 1);
768 }
769
Christopher Faulet1ec90772021-03-10 15:34:52 +0100770 /* Update the server status */
Christopher Faulet07cb0e12021-03-11 18:06:23 +0100771 srvrq_update_srv_status(srv, (srv->addr.ss_family != AF_INET && srv->addr.ss_family != AF_INET6));
Baptiste Assmann87138c32020-08-04 10:57:21 +0200772
Christopher Faulet67957bd2017-09-27 11:00:59 +0200773 srv->svc_port = item->port;
774 srv->flags &= ~SRV_F_MAPPORTS;
775 if ((srv->check.state & CHK_ST_CONFIGURED) &&
776 !(srv->flags & SRV_F_CHECKPORT))
777 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100778
Daniel Corbettf8716912019-11-17 09:48:56 -0500779 if (!srv->dns_opts.ignore_weight) {
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200780 char weight[9];
781 int ha_weight;
782
Daniel Corbettf8716912019-11-17 09:48:56 -0500783 /* DNS weight range if from 0 to 65535
784 * HAProxy weight is from 0 to 256
785 * The rule below ensures that weight 0 is well respected
786 * while allowing a "mapping" from DNS weight into HAProxy's one.
787 */
788 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100789
Daniel Corbettf8716912019-11-17 09:48:56 -0500790 snprintf(weight, sizeof(weight), "%d", ha_weight);
791 server_parse_weight_change_request(srv, weight);
792 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100793 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200794 }
795 }
796 }
797}
798
799/* Validates that the buffer DNS response provided in <resp> and finishing
800 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200801 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200802 * The result is stored in <resolution>' response, buf_response,
803 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804 *
805 * This function returns one of the DNS_RESP_* code to indicate the type of
806 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200808static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
809 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200810{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200811 unsigned char *reader;
812 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200813 int len, flags, offset;
814 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200815 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200816 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200817 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200818 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200819 int i, found = 0;
Willy Tarreau963f7012020-07-22 17:00:45 +0200820 int cause = DNS_RESP_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200821
Christopher Faulet67957bd2017-09-27 11:00:59 +0200822 reader = resp;
823 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200824 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200825 dns_query = NULL;
Willy Tarreau963f7012020-07-22 17:00:45 +0200826 dns_answer_record = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200827
Christopher Faulet67957bd2017-09-27 11:00:59 +0200828 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200829 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200830
831 /* query id */
832 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200833 goto invalid_resp;
834
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200835 dns_p->header.id = reader[0] * 256 + reader[1];
836 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200837
Christopher Faulet67957bd2017-09-27 11:00:59 +0200838 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200839 * First byte contains:
840 * - response flag (1 bit)
841 * - opcode (4 bits)
842 * - authoritative (1 bit)
843 * - truncated (1 bit)
844 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200845 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200846 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200847 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200848
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200849 flags = reader[0] * 256 + reader[1];
850
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200851 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
Willy Tarreau963f7012020-07-22 17:00:45 +0200852 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
853 cause = DNS_RESP_NX_DOMAIN;
854 goto return_error;
855 }
856 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) {
857 cause = DNS_RESP_REFUSED;
858 goto return_error;
859 }
860 else {
861 cause = DNS_RESP_ERROR;
862 goto return_error;
863 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200864 }
865
Christopher Faulet67957bd2017-09-27 11:00:59 +0200866 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200867 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200868
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200869 /* 2 bytes for question count */
870 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200871 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200872 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200873 /* (for now) we send one query only, so we expect only one in the
874 * response too */
Willy Tarreau963f7012020-07-22 17:00:45 +0200875 if (dns_p->header.qdcount != 1) {
876 cause = DNS_RESP_QUERY_COUNT_ERROR;
877 goto return_error;
878 }
879
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200880 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200881 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200882 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200883
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200884 /* 2 bytes for answer count */
885 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200886 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200887 dns_p->header.ancount = reader[0] * 256 + reader[1];
Willy Tarreau963f7012020-07-22 17:00:45 +0200888 if (dns_p->header.ancount == 0) {
889 cause = DNS_RESP_ANCOUNT_ZERO;
890 goto return_error;
891 }
892
Christopher Faulet67957bd2017-09-27 11:00:59 +0200893 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200894 if (dns_p->header.ancount > max_answer_records)
Willy Tarreau963f7012020-07-22 17:00:45 +0200895 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200896 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200897
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200898 /* 2 bytes authority count */
899 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200900 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200901 dns_p->header.nscount = reader[0] * 256 + reader[1];
902 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200903
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200904 /* 2 bytes additional count */
905 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200906 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200907 dns_p->header.arcount = reader[0] * 256 + reader[1];
908 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200909
Christopher Faulet67957bd2017-09-27 11:00:59 +0200910 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200911 LIST_INIT(&dns_p->query_list);
912 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 +0200913 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200914 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200915 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200916 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200917 goto invalid_resp;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200918 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200919 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200920
Christopher Faulet67957bd2017-09-27 11:00:59 +0200921 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200922 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200923 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200924 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100925 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200926
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200927 if (len == 0)
Willy Tarreau963f7012020-07-22 17:00:45 +0200928 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200929
930 reader += offset;
931 previous_dname = dns_query->name;
932
933 /* move forward 2 bytes for question type */
934 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200935 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200936 dns_query->type = reader[0] * 256 + reader[1];
937 reader += 2;
938
939 /* move forward 2 bytes for question class */
940 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200941 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200942 dns_query->class = reader[0] * 256 + reader[1];
943 reader += 2;
944 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200945
Baptiste Assmann251abb92017-08-11 09:58:27 +0200946 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200947 * because a TRUNCATED SRV query type response can still be exploited */
Willy Tarreau963f7012020-07-22 17:00:45 +0200948 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) {
949 cause = DNS_RESP_TRUNCATED;
950 goto return_error;
951 }
Baptiste Assmann251abb92017-08-11 09:58:27 +0200952
Baptiste Assmann325137d2015-04-13 23:40:55 +0200953 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200954 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200955 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200956 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200957 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200958
Willy Tarreaubafbe012017-11-24 17:34:44 +0100959 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200960 if (dns_answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +0200961 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200962
Baptiste Assmann155bc682021-01-15 17:01:24 +0100963 /* initialization */
964 dns_answer_record->ar_item = NULL;
Christopher Faulet8b6d5b02021-03-11 09:36:05 +0100965 dns_answer_record->last_seen = TICK_ETERNITY;
Baptiste Assmann155bc682021-01-15 17:01:24 +0100966
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200967 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100968 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200969
Willy Tarreau963f7012020-07-22 17:00:45 +0200970 if (len == 0)
971 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200972
Christopher Faulet67957bd2017-09-27 11:00:59 +0200973 /* Check if the current record dname is valid. previous_dname
974 * points either to queried dname or last CNAME target */
Olivier Houchardb17b8842020-04-01 18:30:27 +0200975 if (dns_query->type != DNS_RTYPE_SRV && dns_hostname_cmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200976 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200977 /* First record, means a mismatch issue between
978 * queried dname and dname found in the first
979 * record */
Willy Tarreau963f7012020-07-22 17:00:45 +0200980 goto invalid_resp;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200981 }
982 else {
983 /* If not the first record, this means we have a
Willy Tarreau963f7012020-07-22 17:00:45 +0200984 * CNAME resolution error.
985 */
986 cause = DNS_RESP_CNAME_ERROR;
987 goto return_error;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200988 }
989
Baptiste Assmann325137d2015-04-13 23:40:55 +0200990 }
991
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200992 memcpy(dns_answer_record->name, tmpname, len);
993 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200994
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200995 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +0200996 if (reader >= bufend)
997 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200998
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200999 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001000 if (reader + 2 > bufend)
1001 goto invalid_resp;
1002
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001003 dns_answer_record->type = reader[0] * 256 + reader[1];
1004 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001005
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001006 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001007 if (reader + 2 > bufend)
1008 goto invalid_resp;
1009
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001010 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001011 reader += 2;
1012
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001013 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001014 if (reader + 4 > bufend)
1015 goto invalid_resp;
1016
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001017 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1018 + reader[2] * 256 + reader[3];
1019 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001020
Christopher Faulet67957bd2017-09-27 11:00:59 +02001021 /* Now reading data len */
Willy Tarreau963f7012020-07-22 17:00:45 +02001022 if (reader + 2 > bufend)
1023 goto invalid_resp;
1024
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001025 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001026
Christopher Faulet67957bd2017-09-27 11:00:59 +02001027 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001028 reader += 2;
1029
Willy Tarreau963f7012020-07-22 17:00:45 +02001030 if (reader + dns_answer_record->data_len > bufend)
1031 goto invalid_resp;
Remi Gacogneefbbdf72018-12-05 17:56:29 +01001032
Christopher Faulet67957bd2017-09-27 11:00:59 +02001033 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001034 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001035 case DNS_RTYPE_A:
1036 /* ipv4 is stored on 4 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001037 if (dns_answer_record->data_len != 4)
1038 goto invalid_resp;
1039
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001040 dns_answer_record->address.sa_family = AF_INET;
1041 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1042 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001043 break;
1044
1045 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001046 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001047 * no IP could be found and last record was a CNAME. Could be triggered
1048 * by a wrong query type
1049 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001050 * + 1 because dns_answer_record_id starts at 0
1051 * while number of answers is an integer and
1052 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001053 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001054 if (i + 1 == dns_p->header.ancount) {
Willy Tarreau963f7012020-07-22 17:00:45 +02001055 cause = DNS_RESP_CNAME_ERROR;
1056 goto return_error;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001057 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001058
1059 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +01001060 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +02001061 if (len == 0)
1062 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001063
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001064 memcpy(dns_answer_record->target, tmpname, len);
1065 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001066 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001067 break;
1068
Olivier Houchard8da5f982017-08-04 18:35:36 +02001069
1070 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001071 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +02001072 * - 2 bytes for the priority
1073 * - 2 bytes for the weight
1074 * - 2 bytes for the port
1075 * - the target hostname
1076 */
Willy Tarreau963f7012020-07-22 17:00:45 +02001077 if (dns_answer_record->data_len <= 6)
1078 goto invalid_resp;
1079
Willy Tarreaud5370e12017-09-19 14:59:52 +02001080 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001081 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +02001082 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001083 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +02001084 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001085 reader += sizeof(uint16_t);
1086 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +01001087 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +02001088 if (len == 0)
1089 goto invalid_resp;
1090
Olivier Houchard8da5f982017-08-04 18:35:36 +02001091 dns_answer_record->data_len = len;
1092 memcpy(dns_answer_record->target, tmpname, len);
1093 dns_answer_record->target[len] = 0;
Baptiste Assmann155bc682021-01-15 17:01:24 +01001094 if (dns_answer_record->ar_item != NULL) {
1095 pool_free(dns_answer_item_pool, dns_answer_record->ar_item);
1096 dns_answer_record->ar_item = NULL;
1097 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001098 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001099
Baptiste Assmann325137d2015-04-13 23:40:55 +02001100 case DNS_RTYPE_AAAA:
1101 /* ipv6 is stored on 16 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001102 if (dns_answer_record->data_len != 16)
1103 goto invalid_resp;
1104
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001105 dns_answer_record->address.sa_family = AF_INET6;
1106 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1107 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001108 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001109
Baptiste Assmann325137d2015-04-13 23:40:55 +02001110 } /* switch (record type) */
1111
Christopher Faulet67957bd2017-09-27 11:00:59 +02001112 /* Increment the counter for number of records saved into our
1113 * local response */
1114 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +02001115
Christopher Faulet67957bd2017-09-27 11:00:59 +02001116 /* Move forward dns_answer_record->data_len for analyzing next
1117 * record in the response */
1118 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1119 ? offset
1120 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001121
1122 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001123 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001124 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1125 if (tmp_record->type != dns_answer_record->type)
1126 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001127
1128 switch(tmp_record->type) {
1129 case DNS_RTYPE_A:
1130 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1131 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1132 sizeof(in_addr_t)))
1133 found = 1;
1134 break;
1135
1136 case DNS_RTYPE_AAAA:
1137 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1138 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1139 sizeof(struct in6_addr)))
1140 found = 1;
1141 break;
1142
Olivier Houchard8da5f982017-08-04 18:35:36 +02001143 case DNS_RTYPE_SRV:
1144 if (dns_answer_record->data_len == tmp_record->data_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001145 !dns_hostname_cmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001146 dns_answer_record->port == tmp_record->port) {
1147 tmp_record->weight = dns_answer_record->weight;
1148 found = 1;
1149 }
1150 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001151
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001152 default:
1153 break;
1154 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001155
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001156 if (found == 1)
1157 break;
1158 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001159
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001160 if (found == 1) {
Christopher Faulet8b6d5b02021-03-11 09:36:05 +01001161 tmp_record->last_seen = now_ms;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001162 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001163 dns_answer_record = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001164 }
1165 else {
Christopher Faulet8b6d5b02021-03-11 09:36:05 +01001166 dns_answer_record->last_seen = now_ms;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001167 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001168 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
Willy Tarreau963f7012020-07-22 17:00:45 +02001169 dns_answer_record = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001170 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001171 } /* for i 0 to ancount */
1172
Christopher Faulet67957bd2017-09-27 11:00:59 +02001173 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001174 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001175
Baptiste Assmann37950c82020-02-19 01:08:51 +01001176 /* now parsing additional records for SRV queries only */
1177 if (dns_query->type != DNS_RTYPE_SRV)
1178 goto skip_parsing_additional_records;
Willy Tarreau963f7012020-07-22 17:00:45 +02001179
Jerome Magnin4002f8d2020-07-26 12:13:12 +02001180 /* if we find Authority records, just skip them */
1181 for (i = 0; i < dns_p->header.nscount; i++) {
1182 offset = 0;
1183 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE,
1184 &offset, 0);
1185 if (len == 0)
1186 continue;
1187
1188 if (reader + offset + 10 >= bufend)
1189 goto invalid_resp;
1190
1191 reader += offset;
1192 /* skip 2 bytes for class */
1193 reader += 2;
1194 /* skip 2 bytes for type */
1195 reader += 2;
1196 /* skip 4 bytes for ttl */
1197 reader += 4;
1198 /* read data len */
1199 len = reader[0] * 256 + reader[1];
1200 reader += 2;
1201
1202 if (reader + len >= bufend)
1203 goto invalid_resp;
1204
1205 reader += len;
1206 }
1207
Baptiste Assmann13a92322019-06-07 09:40:55 +02001208 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001209 for (i = 0; i < dns_p->header.arcount; i++) {
1210 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +02001211 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001212
1213 dns_answer_record = pool_alloc(dns_answer_item_pool);
1214 if (dns_answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +02001215 goto invalid_resp;
Christopher Faulet8b6d5b02021-03-11 09:36:05 +01001216 dns_answer_record->last_seen = TICK_ETERNITY;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001217
1218 offset = 0;
1219 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1220
1221 if (len == 0) {
1222 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001223 dns_answer_record = NULL;
Baptiste Assmann37950c82020-02-19 01:08:51 +01001224 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001225 }
1226
1227 memcpy(dns_answer_record->name, tmpname, len);
1228 dns_answer_record->name[len] = 0;
1229
1230 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +02001231 if (reader >= bufend)
1232 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001233
1234 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001235 if (reader + 2 > bufend)
1236 goto invalid_resp;
1237
Baptiste Assmann13a92322019-06-07 09:40:55 +02001238 dns_answer_record->type = reader[0] * 256 + reader[1];
1239 reader += 2;
1240
1241 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001242 if (reader + 2 > bufend)
1243 goto invalid_resp;
1244
Baptiste Assmann13a92322019-06-07 09:40:55 +02001245 dns_answer_record->class = reader[0] * 256 + reader[1];
1246 reader += 2;
1247
1248 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001249 if (reader + 4 > bufend)
1250 goto invalid_resp;
1251
Baptiste Assmann13a92322019-06-07 09:40:55 +02001252 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1253 + reader[2] * 256 + reader[3];
1254 reader += 4;
1255
1256 /* Now reading data len */
Willy Tarreau963f7012020-07-22 17:00:45 +02001257 if (reader + 2 > bufend)
1258 goto invalid_resp;
1259
Baptiste Assmann13a92322019-06-07 09:40:55 +02001260 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1261
1262 /* Move forward 2 bytes for data len */
1263 reader += 2;
1264
Willy Tarreau963f7012020-07-22 17:00:45 +02001265 if (reader + dns_answer_record->data_len > bufend)
1266 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001267
1268 /* Analyzing record content */
1269 switch (dns_answer_record->type) {
1270 case DNS_RTYPE_A:
1271 /* ipv4 is stored on 4 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001272 if (dns_answer_record->data_len != 4)
1273 goto invalid_resp;
1274
Baptiste Assmann13a92322019-06-07 09:40:55 +02001275 dns_answer_record->address.sa_family = AF_INET;
1276 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1277 reader, dns_answer_record->data_len);
1278 break;
1279
1280 case DNS_RTYPE_AAAA:
1281 /* ipv6 is stored on 16 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001282 if (dns_answer_record->data_len != 16)
1283 goto invalid_resp;
1284
Baptiste Assmann13a92322019-06-07 09:40:55 +02001285 dns_answer_record->address.sa_family = AF_INET6;
1286 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1287 reader, dns_answer_record->data_len);
1288 break;
1289
1290 default:
1291 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001292 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001293 continue;
1294
1295 } /* switch (record type) */
1296
1297 /* Increment the counter for number of records saved into our
1298 * local response */
1299 nb_saved_records++;
1300
1301 /* Move forward dns_answer_record->data_len for analyzing next
1302 * record in the response */
Christopher Fauletb8e92812021-03-10 15:19:57 +01001303 reader += dns_answer_record->data_len;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001304
1305 /* Lookup to see if we already had this entry */
1306 found = 0;
1307 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
Christopher Fauletb8e92812021-03-10 15:19:57 +01001308 struct dns_answer_item *ar_item;
1309
1310 if (tmp_record->type != DNS_RTYPE_SRV || !tmp_record->ar_item)
1311 continue;
1312
1313 ar_item = tmp_record->ar_item;
Christopher Faulet27c8d7a2021-03-12 16:42:45 +01001314 if (ar_item->type != dns_answer_record->type || ar_item->last_seen == now_ms ||
Christopher Fauletb8e92812021-03-10 15:19:57 +01001315 len != tmp_record->data_len ||
1316 dns_hostname_cmp(dns_answer_record->name, tmp_record->target, tmp_record->data_len))
Baptiste Assmann13a92322019-06-07 09:40:55 +02001317 continue;
1318
Christopher Fauletb8e92812021-03-10 15:19:57 +01001319 switch(ar_item->type) {
Baptiste Assmann13a92322019-06-07 09:40:55 +02001320 case DNS_RTYPE_A:
1321 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
Christopher Fauletb8e92812021-03-10 15:19:57 +01001322 &((struct sockaddr_in *)&ar_item->address)->sin_addr,
Baptiste Assmann13a92322019-06-07 09:40:55 +02001323 sizeof(in_addr_t)))
1324 found = 1;
1325 break;
1326
1327 case DNS_RTYPE_AAAA:
1328 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
Christopher Fauletb8e92812021-03-10 15:19:57 +01001329 &((struct sockaddr_in6 *)&ar_item->address)->sin6_addr,
Baptiste Assmann13a92322019-06-07 09:40:55 +02001330 sizeof(struct in6_addr)))
1331 found = 1;
1332 break;
1333
1334 default:
1335 break;
1336 }
1337
1338 if (found == 1)
1339 break;
1340 }
1341
1342 if (found == 1) {
Christopher Faulet8b6d5b02021-03-11 09:36:05 +01001343 tmp_record->ar_item->last_seen = now_ms;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001344 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001345 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001346 }
1347 else {
Christopher Faulet8b6d5b02021-03-11 09:36:05 +01001348 dns_answer_record->last_seen = now_ms;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001349 dns_answer_record->ar_item = NULL;
1350
1351 // looking for the SRV record in the response list linked to this additional record
1352 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001353 if (tmp_record->type == DNS_RTYPE_SRV &&
Baptiste Assmann155bc682021-01-15 17:01:24 +01001354 tmp_record->ar_item == NULL &&
Christopher Faulet5a891752020-09-08 10:06:01 +02001355 !dns_hostname_cmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len)) {
1356 /* Always use the received additional record to refresh info */
Christopher Faulet5a891752020-09-08 10:06:01 +02001357 tmp_record->ar_item = dns_answer_record;
Christopher Fauletd67f8bb2021-02-23 11:59:19 +01001358 dns_answer_record = NULL;
Christopher Faulet5a891752020-09-08 10:06:01 +02001359 break;
1360 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001361 }
Christopher Fauletd67f8bb2021-02-23 11:59:19 +01001362 if (dns_answer_record) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001363 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Fauletd67f8bb2021-02-23 11:59:19 +01001364 dns_answer_record = NULL;
1365 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001366 }
1367 } /* for i 0 to arcount */
1368
Baptiste Assmann37950c82020-02-19 01:08:51 +01001369 skip_parsing_additional_records:
1370
Baptiste Assmann13a92322019-06-07 09:40:55 +02001371 /* Save the number of records we really own */
1372 dns_p->header.arcount = nb_saved_records;
1373
Christopher Faulet67957bd2017-09-27 11:00:59 +02001374 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001375 return DNS_RESP_VALID;
Willy Tarreau963f7012020-07-22 17:00:45 +02001376
1377 invalid_resp:
1378 cause = DNS_RESP_INVALID;
1379
1380 return_error:
1381 pool_free(dns_answer_item_pool, dns_answer_record);
1382 return cause;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001383}
1384
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001386 * If existing IP not found, return the first IP matching family_priority,
1387 * otherwise, first ip found
1388 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001389 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001390 * For both cases above, dns_validate_dns_response is required
1391 * returns one of the DNS_UPD_* code
1392 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001393int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001394 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001395 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001396 void **newip, short *newip_sin_family,
1397 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001398{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001399 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001400 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001401 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001402 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001403 int currentip_sel;
1404 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001405 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001406 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001407
Christopher Faulet67957bd2017-09-27 11:00:59 +02001408 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001409 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001410 *newip = newip4 = newip6 = NULL;
1411 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001412 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001413 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001414
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001415 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001416 * Top priority is the preferred network ip version,
1417 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001418 * the last priority is the currently used IP,
1419 *
1420 * For these three priorities, a score is calculated. The
1421 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001422 * 8 - preferred ip version.
1423 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001424 * 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 +01001425 * 1 - current ip.
1426 * The result with the biggest score is returned.
1427 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001428
1429 list_for_each_entry(record, &dns_p->answer_list, list) {
1430 void *ip;
1431 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001432
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001433 if (record->type == DNS_RTYPE_A) {
1434 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1435 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001436 }
1437 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001438 ip_type = AF_INET6;
1439 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001440 }
1441 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001442 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001443 score = 0;
1444
Joseph Herlant42cf6392018-11-15 10:33:28 -08001445 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001446 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001447 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001448
Joseph Herlant42cf6392018-11-15 10:33:28 -08001449 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001450 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001451
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001452 /* Compare only the same addresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001453 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001454 continue;
1455
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001456 if ((ip_type == AF_INET &&
1457 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001458 &dns_opts->pref_net[j].mask.in4,
1459 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001460 (ip_type == AF_INET6 &&
1461 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001462 &dns_opts->pref_net[j].mask.in6,
1463 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001464 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001465 break;
1466 }
1467 }
1468
Christopher Faulet67957bd2017-09-27 11:00:59 +02001469 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001470 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001471 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001472 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001473 if (!allowed_duplicated_ip) {
1474 continue;
1475 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001476 } else {
1477 score += 2;
1478 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001479
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001480 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001481 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001482 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001483 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001484 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001485 !memcmp(ip, currentip, 16)))) {
1486 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001487 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001488 }
1489 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001490 currentip_sel = 0;
1491
1492 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001493 * score. The maximum score is 15, if this value is reached, we
1494 * break the parsing. Implicitly, this score is reached the ip
1495 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001496 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001497 if (ip_type == AF_INET)
1498 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001499 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001500 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001501 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001502 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001503 return DNS_UPD_NO;
1504 max_score = score;
1505 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001506 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001507
Christopher Faulet67957bd2017-09-27 11:00:59 +02001508 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001509 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001510 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001511
Christopher Faulet67957bd2017-09-27 11:00:59 +02001512 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001513 if (family_priority == AF_INET) {
1514 if (newip4) {
1515 *newip = newip4;
1516 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001517 }
1518 else if (newip6) {
1519 *newip = newip6;
1520 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001521 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001522 if (!currentip_found)
1523 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001524 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001525 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001526 else if (family_priority == AF_INET6) {
1527 if (newip6) {
1528 *newip = newip6;
1529 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001530 }
1531 else if (newip4) {
1532 *newip = newip4;
1533 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001534 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001535 if (!currentip_found)
1536 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001537 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001538 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001539 else if (family_priority == AF_UNSPEC) {
1540 if (newip6) {
1541 *newip = newip6;
1542 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001543 }
1544 else if (newip4) {
1545 *newip = newip4;
1546 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001547 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001548 if (!currentip_found)
1549 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001550 }
1551
Christopher Faulet67957bd2017-09-27 11:00:59 +02001552 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001553 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001554
Christopher Faulet67957bd2017-09-27 11:00:59 +02001555 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001556 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001557 /* Move the first record to the end of the list, for internal
1558 * round robin */
1559 LIST_DEL(&record->list);
1560 LIST_ADDQ(&dns_p->answer_list, &record->list);
1561 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001562 }
1563 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001564}
1565
Christopher Faulet67957bd2017-09-27 11:00:59 +02001566/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001567 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001568 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1569 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001570 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001571 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1572 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001573 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001574int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001575{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001576 char *ptr;
1577 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001578
Christopher Faulet67957bd2017-09-27 11:00:59 +02001579 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001580 return -1;
1581
Christopher Faulet67957bd2017-09-27 11:00:59 +02001582 ptr = str;
1583 for (i = 0; i < dn_len-1; ++i) {
1584 sz = dn[i];
1585 if (i)
1586 *ptr++ = '.';
1587 memcpy(ptr, dn+i+1, sz);
1588 ptr += sz;
1589 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001590 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001591 *ptr++ = '\0';
1592 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001593}
1594
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1596 *
1597 * <str> must be a null-terminated string. <str_len> must include the
1598 * terminating null byte. <dn> buffer must be allocated and its size must be
1599 * passed in <dn_len>.
1600 *
1601 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1602 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001603 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001605{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001606 int i, offset;
1607
Christopher Faulet67957bd2017-09-27 11:00:59 +02001608 if (dn_len < str_len + 1)
1609 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001610
Christopher Faulet67957bd2017-09-27 11:00:59 +02001611 /* First byte of dn will be used to store the length of the first
1612 * label */
1613 offset = 0;
1614 for (i = 0; i < str_len; ++i) {
1615 if (str[i] == '.') {
1616 /* 2 or more consecutive dots is invalid */
1617 if (i == offset)
1618 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001619
Lukas Tribus81725b82020-02-27 15:47:24 +01001620 /* ignore trailing dot */
1621 if (i + 2 == str_len) {
1622 i++;
1623 break;
1624 }
1625
Christopher Faulet67957bd2017-09-27 11:00:59 +02001626 dn[offset] = (i - offset);
1627 offset = i+1;
1628 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001629 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001630 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001631 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001632 dn[offset] = (i - offset - 1);
1633 dn[i] = '\0';
1634 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001635}
1636
Christopher Faulet67957bd2017-09-27 11:00:59 +02001637/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001638 * - total size
1639 * - each label size individually
1640 * returns:
1641 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1642 * 1 when no error. <err> is left unaffected.
1643 */
1644int dns_hostname_validation(const char *string, char **err)
1645{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001646 int i;
1647
1648 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1649 if (err)
1650 *err = DNS_TOO_LONG_FQDN;
1651 return 0;
1652 }
1653
William Dauchyaecd5dc2020-01-26 19:52:34 +01001654 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001655 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001656 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1657 if (!(*string == '-' || *string == '_' ||
1658 (*string >= 'a' && *string <= 'z') ||
1659 (*string >= 'A' && *string <= 'Z') ||
1660 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001661 if (err)
1662 *err = DNS_INVALID_CHARACTER;
1663 return 0;
1664 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001665 i++;
1666 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001667 }
1668
William Dauchyaecd5dc2020-01-26 19:52:34 +01001669 if (!(*string))
1670 break;
1671
1672 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001673 if (err)
1674 *err = DNS_LABEL_TOO_LONG;
1675 return 0;
1676 }
1677
William Dauchyaecd5dc2020-01-26 19:52:34 +01001678 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001679 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001680 return 1;
1681}
1682
Christopher Faulet67957bd2017-09-27 11:00:59 +02001683/* Picks up an available resolution from the different resolution list
1684 * associated to a resolvers section, in this order:
1685 * 1. check in resolutions.curr for the same hostname and query_type
1686 * 2. check in resolutions.wait for the same hostname and query_type
1687 * 3. Get a new resolution from resolution pool
1688 *
1689 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001690 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1692 char **hostname_dn, int hostname_dn_len,
1693 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001694{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001695 struct dns_resolution *res;
1696
1697 if (!*hostname_dn)
1698 goto from_pool;
1699
1700 /* Search for same hostname and query type in resolutions.curr */
1701 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1702 if (!res->hostname_dn)
1703 continue;
1704 if ((query_type == res->prefered_query_type) &&
1705 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001706 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001707 return res;
1708 }
1709
1710 /* Search for same hostname and query type in resolutions.wait */
1711 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1712 if (!res->hostname_dn)
1713 continue;
1714 if ((query_type == res->prefered_query_type) &&
1715 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001716 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001717 return res;
1718 }
1719
1720 from_pool:
1721 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001722 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001723 if (res) {
1724 memset(res, 0, sizeof(*res));
1725 res->resolvers = resolvers;
1726 res->uuid = resolution_uuid;
1727 res->status = RSLV_STATUS_NONE;
1728 res->step = RSLV_STEP_NONE;
1729 res->last_valid = now_ms;
1730
1731 LIST_INIT(&res->requesters);
1732 LIST_INIT(&res->response.answer_list);
1733
1734 res->prefered_query_type = query_type;
1735 res->query_type = query_type;
1736 res->hostname_dn = *hostname_dn;
1737 res->hostname_dn_len = hostname_dn_len;
1738
1739 ++resolution_uuid;
1740
1741 /* Move the resolution to the resolvers wait queue */
1742 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1743 }
1744 return res;
1745}
1746
Christopher Faulet656b4272021-03-10 14:40:39 +01001747void dns_purge_resolution_answer_records(struct dns_resolution *resolution)
1748{
1749 struct dns_answer_item *item, *itemback;
1750
1751 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
1752 LIST_DEL(&item->list);
1753 pool_free(dns_answer_item_pool, item->ar_item);
1754 pool_free(dns_answer_item_pool, item);
1755 }
1756}
1757
Christopher Faulet67957bd2017-09-27 11:00:59 +02001758/* Releases a resolution from its requester(s) and move it back to the pool */
1759static void dns_free_resolution(struct dns_resolution *resolution)
1760{
1761 struct dns_requester *req, *reqback;
1762
1763 /* clean up configuration */
1764 dns_reset_resolution(resolution);
1765 resolution->hostname_dn = NULL;
1766 resolution->hostname_dn_len = 0;
1767
1768 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1769 LIST_DEL(&req->list);
1770 req->resolution = NULL;
1771 }
Christopher Faulet656b4272021-03-10 14:40:39 +01001772 dns_purge_resolution_answer_records(resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001773 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001774 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001775}
1776
1777/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1778 * on success, -1 otherwise.
1779 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001780int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781{
1782 struct dns_resolution *res = NULL;
1783 struct dns_requester *req;
1784 struct dns_resolvers *resolvers;
1785 struct server *srv = NULL;
1786 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001787 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001788 char **hostname_dn;
1789 int hostname_dn_len, query_type;
1790
1791 switch (requester_type) {
1792 case OBJ_TYPE_SERVER:
1793 srv = (struct server *)requester;
1794 hostname_dn = &srv->hostname_dn;
1795 hostname_dn_len = srv->hostname_dn_len;
1796 resolvers = srv->resolvers;
1797 query_type = ((srv->dns_opts.family_prio == AF_INET)
1798 ? DNS_RTYPE_A
1799 : DNS_RTYPE_AAAA);
1800 break;
1801
1802 case OBJ_TYPE_SRVRQ:
1803 srvrq = (struct dns_srvrq *)requester;
1804 hostname_dn = &srvrq->hostname_dn;
1805 hostname_dn_len = srvrq->hostname_dn_len;
1806 resolvers = srvrq->resolvers;
1807 query_type = DNS_RTYPE_SRV;
1808 break;
1809
Baptiste Assmann333939c2019-01-21 08:34:50 +01001810 case OBJ_TYPE_STREAM:
1811 stream = (struct stream *)requester;
1812 hostname_dn = &stream->dns_ctx.hostname_dn;
1813 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1814 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001815 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001816 ? DNS_RTYPE_A
1817 : DNS_RTYPE_AAAA);
1818 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001819 default:
1820 goto err;
1821 }
1822
1823 /* Get a resolution from the resolvers' wait queue or pool */
1824 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1825 goto err;
1826
1827 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001828 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001829 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001830 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001831 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001832 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001833 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001834 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001835 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001836 req->owner = &srv->obj_type;
1837 srv->dns_requester = req;
1838 }
1839 else
1840 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001841 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001842 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001843
1844 req->requester_cb = snr_resolution_cb;
1845 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001846 }
1847 else if (srvrq) {
1848 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001849 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001850 goto err;
1851 req->owner = &srvrq->obj_type;
1852 srvrq->dns_requester = req;
1853 }
1854 else
1855 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001856
1857 req->requester_cb = snr_resolution_cb;
Baptiste Assmann826060e2020-11-19 22:38:33 +01001858 req->requester_error_cb = srvrq_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001859 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001860 else if (stream) {
1861 if (stream->dns_ctx.dns_requester == NULL) {
1862 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1863 goto err;
1864 req->owner = &stream->obj_type;
1865 stream->dns_ctx.dns_requester = req;
1866 }
1867 else
1868 req = stream->dns_ctx.dns_requester;
1869
1870 req->requester_cb = act_resolution_cb;
1871 req->requester_error_cb = act_resolution_error_cb;
1872 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001873 else
1874 goto err;
1875
1876 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001877
1878 LIST_ADDQ(&res->requesters, &req->list);
1879 return 0;
1880
1881 err:
1882 if (res && LIST_ISEMPTY(&res->requesters))
1883 dns_free_resolution(res);
1884 return -1;
1885}
1886
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001887/* Removes a requester from a DNS resolution. It takes takes care of all the
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888 * consequences. It also cleans up some parameters from the requester.
Christopher Faulet119ec522021-03-11 18:09:53 +01001889 * if <safe> is set to 1, the corresponding resolution is not released.
Christopher Faulet67957bd2017-09-27 11:00:59 +02001890 */
Christopher Faulet119ec522021-03-11 18:09:53 +01001891void dns_unlink_resolution(struct dns_requester *requester, int safe)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001892{
1893 struct dns_resolution *res;
1894 struct dns_requester *req;
1895
1896 /* Nothing to do */
1897 if (!requester || !requester->resolution)
1898 return;
1899 res = requester->resolution;
1900
1901 /* Clean up the requester */
1902 LIST_DEL(&requester->list);
1903 requester->resolution = NULL;
1904
1905 /* We need to find another requester linked on this resolution */
1906 if (!LIST_ISEMPTY(&res->requesters))
1907 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1908 else {
Christopher Faulet119ec522021-03-11 18:09:53 +01001909 if (safe) {
1910 /* Don't release it yet. */
1911 dns_reset_resolution(res);
1912 res->hostname_dn = NULL;
1913 res->hostname_dn_len = 0;
1914 dns_purge_resolution_answer_records(res);
1915 return;
1916 }
1917
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 dns_free_resolution(res);
1919 return;
1920 }
1921
1922 /* Move hostname_dn related pointers to the next requester */
1923 switch (obj_type(req->owner)) {
1924 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001925 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1926 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001927 break;
1928 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001929 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1930 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001931 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001932 case OBJ_TYPE_STREAM:
1933 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1934 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1935 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001936 default:
1937 res->hostname_dn = NULL;
1938 res->hostname_dn_len = 0;
1939 break;
1940 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001941}
1942
Christopher Faulet67957bd2017-09-27 11:00:59 +02001943/* Called when a network IO is generated on a name server socket for an incoming
1944 * packet. It performs the following actions:
1945 * - check if the packet requires processing (not outdated resolution)
1946 * - ensure the DNS packet received is valid and call requester's callback
1947 * - call requester's error callback if invalid response
1948 * - check the dn_name in the packet against the one sent
1949 */
1950static void dns_resolve_recv(struct dgram_conn *dgram)
1951{
1952 struct dns_nameserver *ns, *tmpns;
1953 struct dns_resolvers *resolvers;
1954 struct dns_resolution *res;
1955 struct dns_query_item *query;
1956 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1957 unsigned char *bufend;
1958 int fd, buflen, dns_resp;
1959 int max_answer_records;
1960 unsigned short query_id;
1961 struct eb32_node *eb;
1962 struct dns_requester *req;
Emeric Bruna8b60f22021-06-10 15:25:25 +02001963 int keep_answer_items;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001964
1965 fd = dgram->t.sock.fd;
1966
1967 /* check if ready for reading */
1968 if (!fd_recv_ready(fd))
1969 return;
1970
1971 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001972 if ((ns = dgram->owner) == NULL) {
1973 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1974 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001976 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977
1978 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001979 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980
1981 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001982 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001983 /* read message received */
1984 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1985 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001986 /* FIXME : for now we consider EAGAIN only, but at
1987 * least we purge sticky errors that would cause us to
1988 * be called in loops.
1989 */
1990 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001991 fd_cant_recv(fd);
1992 break;
1993 }
1994
1995 /* message too big */
1996 if (buflen > resolvers->accepted_payload_size) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001997 ns->counters->too_big++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001998 continue;
1999 }
2000
2001 /* initializing variables */
2002 bufend = buf + buflen; /* pointer to mark the end of the buffer */
2003
2004 /* read the query id from the packet (16 bits) */
2005 if (buf + 2 > bufend) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002006 ns->counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002007 continue;
2008 }
2009 query_id = dns_response_get_query_id(buf);
2010
2011 /* search the query_id in the pending resolution tree */
2012 eb = eb32_lookup(&resolvers->query_ids, query_id);
2013 if (eb == NULL) {
2014 /* unknown query id means an outdated response and can be safely ignored */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002015 ns->counters->outdated++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002016 continue;
2017 }
2018
William Dauchybe8a3872019-11-27 23:32:41 +01002019 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002020 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002021 /* number of responses received */
2022 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002023
Christopher Faulet67957bd2017-09-27 11:00:59 +02002024 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
2025 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002026
Christopher Faulet67957bd2017-09-27 11:00:59 +02002027 switch (dns_resp) {
2028 case DNS_RESP_VALID:
2029 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002030
Christopher Faulet67957bd2017-09-27 11:00:59 +02002031 case DNS_RESP_INVALID:
2032 case DNS_RESP_QUERY_COUNT_ERROR:
2033 case DNS_RESP_WRONG_NAME:
2034 res->status = RSLV_STATUS_INVALID;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002035 ns->counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002036 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002037
Christopher Faulet67957bd2017-09-27 11:00:59 +02002038 case DNS_RESP_NX_DOMAIN:
2039 res->status = RSLV_STATUS_NX;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002040 ns->counters->nx++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002041 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002042
Christopher Faulet67957bd2017-09-27 11:00:59 +02002043 case DNS_RESP_REFUSED:
2044 res->status = RSLV_STATUS_REFUSED;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002045 ns->counters->refused++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002046 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002047
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048 case DNS_RESP_ANCOUNT_ZERO:
2049 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002050 ns->counters->any_err++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002051 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002052
Christopher Faulet67957bd2017-09-27 11:00:59 +02002053 case DNS_RESP_CNAME_ERROR:
2054 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002055 ns->counters->cname_error++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002056 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002057
Christopher Faulet67957bd2017-09-27 11:00:59 +02002058 case DNS_RESP_TRUNCATED:
2059 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002060 ns->counters->truncated++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002061 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02002062
Christopher Faulet67957bd2017-09-27 11:00:59 +02002063 case DNS_RESP_NO_EXPECTED_RECORD:
2064 case DNS_RESP_ERROR:
2065 case DNS_RESP_INTERNAL:
2066 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002067 ns->counters->other++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002068 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002069 }
2070
Christopher Faulet67957bd2017-09-27 11:00:59 +02002071 /* Wait all nameservers response to handle errors */
2072 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
2073 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002074
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 /* Process error codes */
2076 if (dns_resp != DNS_RESP_VALID) {
2077 if (res->prefered_query_type != res->query_type) {
2078 /* The fallback on the query type was already performed,
2079 * so check the try counter. If it falls to 0, we can
2080 * report an error. Else, wait the next attempt. */
2081 if (!res->try)
2082 goto report_res_error;
2083 }
2084 else {
2085 /* Fallback from A to AAAA or the opposite and re-send
2086 * the resolution immediately. try counter is not
2087 * decremented. */
2088 if (res->prefered_query_type == DNS_RTYPE_A) {
2089 res->query_type = DNS_RTYPE_AAAA;
2090 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002091 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002092 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
2093 res->query_type = DNS_RTYPE_A;
2094 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002095 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002096 }
2097 continue;
2098 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002099
Christopher Faulet67957bd2017-09-27 11:00:59 +02002100 /* Now let's check the query's dname corresponds to the one we
2101 * sent. We can check only the first query of the list. We send
2102 * one query at a time so we get one query in the response */
2103 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
Olivier Houchardb17b8842020-04-01 18:30:27 +02002104 if (query && dns_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002105 dns_resp = DNS_RESP_WRONG_NAME;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002106 ns->counters->other++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002107 goto report_res_error;
2108 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002109
Christopher Faulet67957bd2017-09-27 11:00:59 +02002110 /* So the resolution succeeded */
2111 res->status = RSLV_STATUS_VALID;
2112 res->last_valid = now_ms;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002113 ns->counters->valid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002114 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002115
Christopher Faulet67957bd2017-09-27 11:00:59 +02002116 report_res_error:
Emeric Bruna8b60f22021-06-10 15:25:25 +02002117 keep_answer_items = 0;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002118 list_for_each_entry(req, &res->requesters, list)
Emeric Bruna8b60f22021-06-10 15:25:25 +02002119 keep_answer_items |= req->requester_error_cb(req, dns_resp);
2120 if (!keep_answer_items)
2121 dns_purge_resolution_answer_records(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002122 dns_reset_resolution(res);
2123 LIST_DEL(&res->list);
2124 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
2125 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002126
Christopher Faulet67957bd2017-09-27 11:00:59 +02002127 report_res_success:
2128 /* Only the 1rst requester s managed by the server, others are
2129 * from the cache */
2130 tmpns = ns;
2131 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01002132 struct server *s = objt_server(req->owner);
2133
2134 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002135 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002136 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01002137 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002138 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002139 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002140 }
Baptiste Assmann42746372017-05-03 12:12:02 +02002141
Christopher Faulet67957bd2017-09-27 11:00:59 +02002142 dns_reset_resolution(res);
2143 LIST_DEL(&res->list);
2144 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
2145 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002146 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02002147 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002149}
William Lallemand69e96442016-11-19 00:58:54 +01002150
Christopher Faulet67957bd2017-09-27 11:00:59 +02002151/* Called when a resolvers network socket is ready to send data */
2152static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002153{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002154 struct dns_resolvers *resolvers;
2155 struct dns_nameserver *ns;
2156 struct dns_resolution *res;
2157 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002158
Christopher Faulet67957bd2017-09-27 11:00:59 +02002159 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002160
Christopher Faulet67957bd2017-09-27 11:00:59 +02002161 /* check if ready for sending */
2162 if (!fd_send_ready(fd))
2163 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002164
Christopher Faulet67957bd2017-09-27 11:00:59 +02002165 /* we don't want/need to be waked up any more for sending */
2166 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002167
Christopher Faulet67957bd2017-09-27 11:00:59 +02002168 /* no need to go further if we can't retrieve the nameserver */
2169 if ((ns = dgram->owner) == NULL)
2170 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002171
Christopher Faulet67957bd2017-09-27 11:00:59 +02002172 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002173 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002174
Christopher Faulet67957bd2017-09-27 11:00:59 +02002175 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002176 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002177
Christopher Faulet67957bd2017-09-27 11:00:59 +02002178 if (res->nb_queries == resolvers->nb_nameservers)
2179 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002180
Emeric Brun079c6092021-06-17 18:23:04 +02002181 /* if fd was detected broken by previous send */
2182 if (fd == -1)
2183 goto snd_error;
2184
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002185 len = dns_build_query(res->query_id, res->query_type,
2186 resolvers->accepted_payload_size,
2187 res->hostname_dn, res->hostname_dn_len,
2188 trash.area, trash.size);
2189 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002190 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002191
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002192 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01002193 if (ret != len) {
Emeric Brun751872e2021-05-04 18:16:53 +02002194 if (ret == -1) {
2195 if (errno == EAGAIN) {
2196 /* retry once the socket is ready */
2197 fd_cant_send(fd);
2198 continue;
2199 }
2200
2201 /* purge the fd and set sock.fd to -1
2202 * to create a new one during next
2203 * send_query attempt to the same
2204 * nameserver
2205 */
2206 fd_delete(fd);
Emeric Brun079c6092021-06-17 18:23:04 +02002207 fd = dgram->t.sock.fd = -1;
Willy Tarreau0eae6322019-12-20 11:18:54 +01002208 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002209 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01002210 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002211
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002212 ns->counters->sent++;
2213
Christopher Faulet67957bd2017-09-27 11:00:59 +02002214 res->nb_queries++;
2215 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002216
Christopher Faulet67957bd2017-09-27 11:00:59 +02002217 snd_error:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002218 ns->counters->snd_error++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002219 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002220 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002221 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002222}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002223
Christopher Faulet67957bd2017-09-27 11:00:59 +02002224/* Processes DNS resolution. First, it checks the active list to detect expired
2225 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2226 * checks the wait list to trigger new resolutions.
2227 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002228static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002229{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002230 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002231 struct dns_resolution *res, *resback;
2232 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002233
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002234 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002235
Christopher Faulet67957bd2017-09-27 11:00:59 +02002236 /* Handle all expired resolutions from the active list */
2237 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
Christopher Faulet119ec522021-03-11 18:09:53 +01002238 if (LIST_ISEMPTY(&res->requesters)) {
2239 dns_free_resolution(res);
2240 continue;
2241 }
2242
Christopher Faulet67957bd2017-09-27 11:00:59 +02002243 /* When we find the first resolution in the future, then we can
2244 * stop here */
2245 exp = tick_add(res->last_query, resolvers->timeout.retry);
2246 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002247 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002248
Christopher Faulet67957bd2017-09-27 11:00:59 +02002249 /* If current resolution has been tried too many times and
2250 * finishes in timeout we update its status and remove it from
2251 * the list */
2252 if (!res->try) {
2253 struct dns_requester *req;
Emeric Bruna8b60f22021-06-10 15:25:25 +02002254 int keep_answer_items = 0;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002255
Christopher Faulet67957bd2017-09-27 11:00:59 +02002256 /* Notify the result to the requesters */
2257 if (!res->nb_responses)
2258 res->status = RSLV_STATUS_TIMEOUT;
2259 list_for_each_entry(req, &res->requesters, list)
Emeric Bruna8b60f22021-06-10 15:25:25 +02002260 keep_answer_items |= req->requester_error_cb(req, res->status);
2261 if (!keep_answer_items)
2262 dns_purge_resolution_answer_records(res);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002263
Christopher Faulet67957bd2017-09-27 11:00:59 +02002264 /* Clean up resolution info and remove it from the
2265 * current list */
2266 dns_reset_resolution(res);
2267 LIST_DEL(&res->list);
2268 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002269 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002270 else {
2271 /* Otherwise resend the DNS query and requeue the resolution */
2272 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2273 /* No response received (a real timeout) or fallback already done */
2274 res->query_type = res->prefered_query_type;
2275 res->try--;
2276 }
2277 else {
2278 /* Fallback from A to AAAA or the opposite and re-send
2279 * the resolution immediately. try counter is not
2280 * decremented. */
2281 if (res->prefered_query_type == DNS_RTYPE_A)
2282 res->query_type = DNS_RTYPE_AAAA;
2283 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2284 res->query_type = DNS_RTYPE_A;
2285 else
2286 res->try--;
2287 }
2288 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002289 }
2290 }
William Lallemand69e96442016-11-19 00:58:54 +01002291
Christopher Faulet67957bd2017-09-27 11:00:59 +02002292 /* Handle all resolutions in the wait list */
2293 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
Christopher Faulet119ec522021-03-11 18:09:53 +01002294 if (LIST_ISEMPTY(&res->requesters)) {
2295 dns_free_resolution(res);
2296 continue;
2297 }
2298
Christopher Faulet67957bd2017-09-27 11:00:59 +02002299 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2300 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2301 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002302
Christopher Faulet67957bd2017-09-27 11:00:59 +02002303 if (dns_run_resolution(res) != 1) {
2304 res->last_resolution = now_ms;
2305 LIST_DEL(&res->list);
2306 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002307 }
2308 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002309
Christopher Faulet67957bd2017-09-27 11:00:59 +02002310 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002311 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002312 return t;
2313}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002314
Christopher Faulet67957bd2017-09-27 11:00:59 +02002315/* proto_udp callback functions for a DNS resolution */
2316struct dgram_data_cb resolve_dgram_cb = {
2317 .recv = dns_resolve_recv,
2318 .send = dns_resolve_send,
2319};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002320
Christopher Faulet67957bd2017-09-27 11:00:59 +02002321/* Release memory allocated by DNS */
2322static void dns_deinit(void)
2323{
2324 struct dns_resolvers *resolvers, *resolversback;
2325 struct dns_nameserver *ns, *nsback;
2326 struct dns_resolution *res, *resback;
2327 struct dns_requester *req, *reqback;
2328 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002329
Christopher Faulet67957bd2017-09-27 11:00:59 +02002330 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2331 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2332 free(ns->id);
2333 free((char *)ns->conf.file);
2334 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2335 fd_delete(ns->dgram->t.sock.fd);
2336 free(ns->dgram);
2337 LIST_DEL(&ns->list);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002338 EXTRA_COUNTERS_FREE(ns->extra_counters);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002339 free(ns);
2340 }
2341
2342 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2343 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2344 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002345 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002346 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002347 dns_free_resolution(res);
2348 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002349
Christopher Faulet67957bd2017-09-27 11:00:59 +02002350 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2351 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2352 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002353 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002354 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002355 dns_free_resolution(res);
2356 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002357
Christopher Faulet67957bd2017-09-27 11:00:59 +02002358 free(resolvers->id);
2359 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002360 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002361 LIST_DEL(&resolvers->list);
2362 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002363 }
2364
Christopher Faulet67957bd2017-09-27 11:00:59 +02002365 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2366 free(srvrq->name);
2367 free(srvrq->hostname_dn);
2368 LIST_DEL(&srvrq->list);
2369 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002370 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002371}
2372
Christopher Faulet67957bd2017-09-27 11:00:59 +02002373/* Finalizes the DNS configuration by allocating required resources and checking
2374 * live parameters.
2375 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002376 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002377static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002378{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002379 struct dns_resolvers *resolvers;
2380 struct proxy *px;
2381 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002382
Christopher Faulet67957bd2017-09-27 11:00:59 +02002383 /* allocate pool of resolution per resolvers */
2384 list_for_each_entry(resolvers, &dns_resolvers, list) {
2385 struct dns_nameserver *ns;
2386 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002387
Christopher Faulet67957bd2017-09-27 11:00:59 +02002388 /* Check if we can create the socket with nameservers info */
2389 list_for_each_entry(ns, &resolvers->nameservers, list) {
2390 struct dgram_conn *dgram = NULL;
2391 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002392
Christopher Faulet67957bd2017-09-27 11:00:59 +02002393 /* Check nameserver info */
2394 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002395 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2396 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002397 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002398 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002399 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002400 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002401 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2402 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002403 close(fd);
2404 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002405 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002406 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002407 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002408
Christopher Faulet67957bd2017-09-27 11:00:59 +02002409 /* Create dgram structure that will hold the UPD socket
2410 * and attach it on the current nameserver */
2411 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002412 ha_alert("config: resolvers '%s' : out of memory.\n",
2413 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002414 err_code |= (ERR_ALERT|ERR_ABORT);
2415 goto err;
2416 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002417
Christopher Faulet67957bd2017-09-27 11:00:59 +02002418 /* Leave dgram partially initialized, no FD attached for
2419 * now. */
2420 dgram->owner = ns;
2421 dgram->data = &resolve_dgram_cb;
2422 dgram->t.sock.fd = -1;
2423 ns->dgram = dgram;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002424
2425 /* Store the ns counters pointer */
2426 if (ns->extra_counters) {
2427 ns->counters = EXTRA_COUNTERS_GET(ns->extra_counters, &dns_stats_module);
2428 ns->counters->id = ns->id;
2429 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002430 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002431
Christopher Faulet67957bd2017-09-27 11:00:59 +02002432 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002433 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002434 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002435 err_code |= (ERR_ALERT|ERR_ABORT);
2436 goto err;
2437 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002438
Christopher Faulet67957bd2017-09-27 11:00:59 +02002439 /* Update task's parameters */
2440 t->process = dns_process_resolvers;
2441 t->context = resolvers;
2442 resolvers->t = t;
2443 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002444 }
2445
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002446 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002447 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002448
Christopher Faulet67957bd2017-09-27 11:00:59 +02002449 for (srv = px->srv; srv; srv = srv->next) {
2450 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002451
Christopher Faulet67957bd2017-09-27 11:00:59 +02002452 if (!srv->resolvers_id)
2453 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002454
Christopher Faulet67957bd2017-09-27 11:00:59 +02002455 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002456 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2457 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002458 err_code |= (ERR_ALERT|ERR_ABORT);
2459 continue;
2460 }
2461 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002462
Christopher Faulet67957bd2017-09-27 11:00:59 +02002463 if (srv->srvrq && !srv->srvrq->resolvers) {
2464 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002465 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002466 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2467 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002468 err_code |= (ERR_ALERT|ERR_ABORT);
2469 continue;
2470 }
2471 }
Christopher Faulet55810262021-03-12 10:23:05 +01002472 if (!srv->srvrq && dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002473 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2474 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002475 err_code |= (ERR_ALERT|ERR_ABORT);
2476 continue;
2477 }
2478 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002479 }
2480
Christopher Faulet67957bd2017-09-27 11:00:59 +02002481 if (err_code & (ERR_ALERT|ERR_ABORT))
2482 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002483
Christopher Faulet67957bd2017-09-27 11:00:59 +02002484 return err_code;
2485 err:
2486 dns_deinit();
2487 return err_code;
2488
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002489}
2490
2491static int stats_dump_dns_to_buffer(struct stream_interface *si,
2492 struct dns_nameserver *ns,
2493 struct field *stats, size_t stats_count,
2494 struct list *stat_modules)
2495{
2496 struct appctx *appctx = __objt_appctx(si->end);
2497 struct channel *rep = si_ic(si);
2498 struct stats_module *mod;
2499 size_t idx = 0;
2500
2501 memset(stats, 0, sizeof(struct field) * stats_count);
2502
2503 list_for_each_entry(mod, stat_modules, list) {
2504 struct counters_node *counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2505
2506 mod->fill_stats(counters, stats + idx);
2507 idx += mod->stats_count;
2508 }
2509
2510 if (!stats_dump_one_line(stats, idx, appctx))
2511 return 0;
2512
2513 if (!stats_putchk(rep, NULL, &trash))
2514 goto full;
2515
2516 return 1;
2517
2518 full:
2519 si_rx_room_rdy(si);
2520 return 0;
2521}
2522
2523/* Uses <appctx.ctx.stats.obj1> as a pointer to the current resolver and <obj2>
2524 * as a pointer to the current nameserver.
2525 */
2526int stats_dump_dns(struct stream_interface *si,
2527 struct field *stats, size_t stats_count,
2528 struct list *stat_modules)
2529{
2530 struct appctx *appctx = __objt_appctx(si->end);
2531 struct channel *rep = si_ic(si);
2532 struct dns_resolvers *resolver = appctx->ctx.stats.obj1;
2533 struct dns_nameserver *ns = appctx->ctx.stats.obj2;
2534
2535 if (!resolver)
2536 resolver = LIST_NEXT(&dns_resolvers, struct dns_resolvers *, list);
2537
2538 /* dump resolvers */
2539 list_for_each_entry_from(resolver, &dns_resolvers, list) {
2540 appctx->ctx.stats.obj1 = resolver;
2541
2542 ns = appctx->ctx.stats.obj2 ?
2543 appctx->ctx.stats.obj2 :
2544 LIST_NEXT(&resolver->nameservers, struct dns_nameserver *, list);
2545
2546 list_for_each_entry_from(ns, &resolver->nameservers, list) {
2547 appctx->ctx.stats.obj2 = ns;
2548
2549 if (buffer_almost_full(&rep->buf))
2550 goto full;
2551
2552 if (!stats_dump_dns_to_buffer(si, ns,
2553 stats, stats_count,
2554 stat_modules)) {
2555 return 0;
2556 }
2557 }
2558
2559 appctx->ctx.stats.obj2 = NULL;
2560 }
2561
2562 return 1;
2563
2564 full:
2565 si_rx_room_blk(si);
2566 return 0;
2567}
2568
2569void dns_stats_clear_counters(int clrall, struct list *stat_modules)
2570{
2571 struct dns_resolvers *resolvers;
2572 struct dns_nameserver *ns;
2573 struct stats_module *mod;
2574 void *counters;
2575
2576 list_for_each_entry(mod, stat_modules, list) {
2577 if (!mod->clearable && !clrall)
2578 continue;
2579
2580 list_for_each_entry(resolvers, &dns_resolvers, list) {
2581 list_for_each_entry(ns, &resolvers->nameservers, list) {
2582 counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2583 memcpy(counters, mod->counters, mod->counters_size);
2584 }
2585 }
2586 }
2587
2588}
2589
2590int dns_allocate_counters(struct list *stat_modules)
2591{
2592 struct stats_module *mod;
2593 struct dns_resolvers *resolvers;
2594 struct dns_nameserver *ns;
2595
2596 list_for_each_entry(resolvers, &dns_resolvers, list) {
2597 list_for_each_entry(ns, &resolvers->nameservers, list) {
2598 EXTRA_COUNTERS_REGISTER(&ns->extra_counters, COUNTERS_DNS,
2599 alloc_failed);
2600
2601 list_for_each_entry(mod, stat_modules, list) {
2602 EXTRA_COUNTERS_ADD(mod,
2603 ns->extra_counters,
2604 mod->counters,
2605 mod->counters_size);
2606 }
2607
2608 EXTRA_COUNTERS_ALLOC(ns->extra_counters, alloc_failed);
2609
2610 list_for_each_entry(mod, stat_modules, list) {
2611 memcpy(ns->extra_counters->data + mod->counters_off[ns->extra_counters->type],
2612 mod->counters, mod->counters_size);
2613
2614 /* Store the ns counters pointer */
2615 if (!strcmp(mod->name, "dns")) {
2616 ns->counters = (struct dns_counters *)ns->extra_counters->data + mod->counters_off[COUNTERS_DNS];
2617 ns->counters->id = ns->id;
2618 }
2619 }
2620 }
2621 }
2622
2623 return 1;
2624
2625alloc_failed:
2626 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002627}
2628
Christopher Faulet67957bd2017-09-27 11:00:59 +02002629/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002630static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002631{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002632 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002633
Christopher Faulet67957bd2017-09-27 11:00:59 +02002634 if (*args[2]) {
2635 list_for_each_entry(presolvers, &dns_resolvers, list) {
2636 if (strcmp(presolvers->id, args[2]) == 0) {
2637 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002638 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002639 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002640 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002641 if (appctx->ctx.cli.p0 == NULL)
2642 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002643 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002644 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002645}
2646
Christopher Faulet67957bd2017-09-27 11:00:59 +02002647/* Dumps counters from all resolvers section and associated name servers. It
2648 * returns 0 if the output buffer is full and it needs to be called again,
2649 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002650 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002651 */
2652static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2653{
2654 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002655 struct dns_resolvers *resolvers;
2656 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002657
2658 chunk_reset(&trash);
2659
2660 switch (appctx->st2) {
2661 case STAT_ST_INIT:
2662 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2663 /* fall through */
2664
2665 case STAT_ST_LIST:
2666 if (LIST_ISEMPTY(&dns_resolvers)) {
2667 chunk_appendf(&trash, "No resolvers found\n");
2668 }
2669 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002670 list_for_each_entry(resolvers, &dns_resolvers, list) {
2671 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002672 continue;
2673
Christopher Faulet67957bd2017-09-27 11:00:59 +02002674 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2675 list_for_each_entry(ns, &resolvers->nameservers, list) {
2676 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002677 chunk_appendf(&trash, " sent: %lld\n", ns->counters->sent);
2678 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters->snd_error);
2679 chunk_appendf(&trash, " valid: %lld\n", ns->counters->valid);
2680 chunk_appendf(&trash, " update: %lld\n", ns->counters->update);
2681 chunk_appendf(&trash, " cname: %lld\n", ns->counters->cname);
2682 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters->cname_error);
2683 chunk_appendf(&trash, " any_err: %lld\n", ns->counters->any_err);
2684 chunk_appendf(&trash, " nx: %lld\n", ns->counters->nx);
2685 chunk_appendf(&trash, " timeout: %lld\n", ns->counters->timeout);
2686 chunk_appendf(&trash, " refused: %lld\n", ns->counters->refused);
2687 chunk_appendf(&trash, " other: %lld\n", ns->counters->other);
2688 chunk_appendf(&trash, " invalid: %lld\n", ns->counters->invalid);
2689 chunk_appendf(&trash, " too_big: %lld\n", ns->counters->too_big);
2690 chunk_appendf(&trash, " truncated: %lld\n", ns->counters->truncated);
2691 chunk_appendf(&trash, " outdated: %lld\n", ns->counters->outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002692 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002693 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002694 }
2695 }
2696
2697 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002698 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002699 /* let's try again later from this session. We add ourselves into
2700 * this session's users so that it can remove us upon termination.
2701 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002702 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002703 return 0;
2704 }
William Lallemand69e96442016-11-19 00:58:54 +01002705 /* fall through */
2706
2707 default:
2708 appctx->st2 = STAT_ST_FIN;
2709 return 1;
2710 }
2711}
2712
2713/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002714static struct cli_kw_list cli_kws = {{ }, {
2715 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2716 " associated name servers",
2717 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2718 {{},}
2719 }
2720};
William Lallemand69e96442016-11-19 00:58:54 +01002721
Willy Tarreau0108d902018-11-25 19:14:37 +01002722INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002723
Baptiste Assmann333939c2019-01-21 08:34:50 +01002724/*
2725 * Prepare <rule> for hostname resolution.
2726 * Returns -1 in case of any allocation failure, 0 if not.
2727 * On error, a global failure counter is also incremented.
2728 */
2729static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2730{
2731 char *hostname_dn;
2732 int hostname_len, hostname_dn_len;
2733 struct buffer *tmp = get_trash_chunk();
2734
2735 if (!hostname)
2736 return 0;
2737
2738 hostname_len = strlen(hostname);
2739 hostname_dn = tmp->area;
2740 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2741 hostname_dn, tmp->size);
2742 if (hostname_dn_len == -1)
2743 goto err;
2744
2745
2746 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2747 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2748 if (!stream->dns_ctx.hostname_dn)
2749 goto err;
2750
2751 return 0;
2752
2753 err:
2754 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2755 dns_failed_resolutions += 1;
2756 return -1;
2757}
2758
2759
2760/*
2761 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2762 */
2763enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2764 struct session *sess, struct stream *s, int flags)
2765{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002766 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002767 struct sample *smp;
2768 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002769 struct dns_requester *req;
2770 struct dns_resolvers *resolvers;
2771 struct dns_resolution *res;
Christopher Faulet5098a082020-07-22 11:46:32 +02002772 int exp, locked = 0;
2773 enum act_return ret = ACT_RET_CONT;
2774
2775 resolvers = rule->arg.dns.resolvers;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002776
2777 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002778 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002779 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2780 resolution = s->dns_ctx.dns_requester->resolution;
Christopher Faulet5098a082020-07-22 11:46:32 +02002781 if (!locked) {
2782 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2783 locked = 1;
2784 }
2785
Christopher Faulet385101e2020-07-28 10:21:54 +02002786 if (resolution->step == RSLV_STEP_RUNNING)
2787 goto yield;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002788 if (resolution->step == RSLV_STEP_NONE) {
2789 /* We update the variable only if we have a valid response. */
2790 if (resolution->status == RSLV_STATUS_VALID) {
2791 struct sample smp;
2792 short ip_sin_family = 0;
2793 void *ip = NULL;
2794
Christopher Fauleta4168432020-01-24 18:08:42 +01002795 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002796 0, &ip, &ip_sin_family, NULL);
2797
2798 switch (ip_sin_family) {
2799 case AF_INET:
2800 smp.data.type = SMP_T_IPV4;
2801 memcpy(&smp.data.u.ipv4, ip, 4);
2802 break;
2803 case AF_INET6:
2804 smp.data.type = SMP_T_IPV6;
2805 memcpy(&smp.data.u.ipv6, ip, 16);
2806 break;
2807 default:
2808 ip = NULL;
2809 }
2810
2811 if (ip) {
2812 smp.px = px;
2813 smp.sess = sess;
2814 smp.strm = s;
2815
2816 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2817 }
2818 }
2819 }
2820
Christopher Faulet385101e2020-07-28 10:21:54 +02002821 goto release_requester;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002822 }
2823
2824 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002825 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2826 if (smp == NULL)
Christopher Faulet5098a082020-07-22 11:46:32 +02002827 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002828
Willy Tarreau45726fd2019-07-17 10:38:45 +02002829 fqdn = smp->data.u.str.area;
2830 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet5098a082020-07-22 11:46:32 +02002831 goto end; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002832
Willy Tarreau45726fd2019-07-17 10:38:45 +02002833 s->dns_ctx.parent = rule;
Christopher Faulet5098a082020-07-22 11:46:32 +02002834
2835 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2836 locked = 1;
2837
Willy Tarreau45726fd2019-07-17 10:38:45 +02002838 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002839
2840 /* Check if there is a fresh enough response in the cache of our associated resolution */
2841 req = s->dns_ctx.dns_requester;
Christopher Faulet385101e2020-07-28 10:21:54 +02002842 if (!req || !req->resolution)
2843 goto release_requester; /* on error, ignore the action */
Christopher Faulet5098a082020-07-22 11:46:32 +02002844 res = req->resolution;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002845
2846 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2847 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
Christopher Faulet385101e2020-07-28 10:21:54 +02002848 && !tick_is_expired(exp, now_ms)) {
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002849 goto use_cache;
2850 }
2851
Willy Tarreau45726fd2019-07-17 10:38:45 +02002852 dns_trigger_resolution(s->dns_ctx.dns_requester);
Christopher Faulet385101e2020-07-28 10:21:54 +02002853
2854 yield:
2855 if (flags & ACT_OPT_FINAL)
2856 goto release_requester;
Christopher Faulet5098a082020-07-22 11:46:32 +02002857 ret = ACT_RET_YIELD;
2858
2859 end:
2860 if (locked)
2861 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2862 return ret;
Christopher Faulet385101e2020-07-28 10:21:54 +02002863
2864 release_requester:
2865 free(s->dns_ctx.hostname_dn);
2866 s->dns_ctx.hostname_dn = NULL;
2867 s->dns_ctx.hostname_dn_len = 0;
2868 if (s->dns_ctx.dns_requester) {
Christopher Faulet119ec522021-03-11 18:09:53 +01002869 dns_unlink_resolution(s->dns_ctx.dns_requester, 0);
Christopher Faulet385101e2020-07-28 10:21:54 +02002870 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2871 s->dns_ctx.dns_requester = NULL;
2872 }
2873 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002874}
2875
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002876static void release_dns_action(struct act_rule *rule)
2877{
2878 release_sample_expr(rule->arg.dns.expr);
2879 free(rule->arg.dns.varname);
2880 free(rule->arg.dns.resolvers_id);
2881 free(rule->arg.dns.dns_opts);
2882}
2883
Baptiste Assmann333939c2019-01-21 08:34:50 +01002884
2885/* parse "do-resolve" action
2886 * This action takes the following arguments:
2887 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2888 *
2889 * - <varName> is the variable name where the result of the DNS resolution will be stored
2890 * (mandatory)
2891 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2892 * (mandatory)
2893 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2894 * (optional), defaults to ipv6
2895 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2896 */
2897enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2898{
2899 int cur_arg;
2900 struct sample_expr *expr;
2901 unsigned int where;
2902 const char *beg, *end;
2903
2904 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2905 cur_arg = *orig_arg - 1;
2906
2907 /* locate varName, which is mandatory */
2908 beg = strchr(args[cur_arg], '(');
2909 if (beg == NULL)
2910 goto do_resolve_parse_error;
2911 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2912 end = strchr(beg, ',');
2913 if (end == NULL)
2914 goto do_resolve_parse_error;
2915 rule->arg.dns.varname = my_strndup(beg, end - beg);
2916 if (rule->arg.dns.varname == NULL)
2917 goto do_resolve_parse_error;
2918
2919
2920 /* locate resolversSectionName, which is mandatory.
2921 * Since next parameters are optional, the delimiter may be comma ','
2922 * or closing parenthesis ')'
2923 */
2924 beg = end + 1;
2925 end = strchr(beg, ',');
2926 if (end == NULL)
2927 end = strchr(beg, ')');
2928 if (end == NULL)
2929 goto do_resolve_parse_error;
2930 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2931 if (rule->arg.dns.resolvers_id == NULL)
2932 goto do_resolve_parse_error;
2933
2934
Christopher Fauleta4168432020-01-24 18:08:42 +01002935 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2936 if (rule->arg.dns.dns_opts == NULL)
2937 goto do_resolve_parse_error;
2938
Baptiste Assmann333939c2019-01-21 08:34:50 +01002939 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002940 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002941
2942 /* optional arguments accepted for now:
2943 * ipv4 or ipv6
2944 */
2945 while (*end != ')') {
2946 beg = end + 1;
2947 end = strchr(beg, ',');
2948 if (end == NULL)
2949 end = strchr(beg, ')');
2950 if (end == NULL)
2951 goto do_resolve_parse_error;
2952
2953 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002954 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002955 }
2956 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002957 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002958 }
2959 else {
2960 goto do_resolve_parse_error;
2961 }
2962 }
2963
2964 cur_arg = cur_arg + 1;
2965
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002966 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 +01002967 if (!expr)
2968 goto do_resolve_parse_error;
2969
2970
2971 where = 0;
2972 if (px->cap & PR_CAP_FE)
2973 where |= SMP_VAL_FE_HRQ_HDR;
2974 if (px->cap & PR_CAP_BE)
2975 where |= SMP_VAL_BE_HRQ_HDR;
2976
2977 if (!(expr->fetch->val & where)) {
2978 memprintf(err,
2979 "fetch method '%s' extracts information from '%s', none of which is available here",
2980 args[cur_arg-1], sample_src_names(expr->fetch->use));
2981 free(expr);
2982 return ACT_RET_PRS_ERR;
2983 }
2984 rule->arg.dns.expr = expr;
2985 rule->action = ACT_CUSTOM;
2986 rule->action_ptr = dns_action_do_resolve;
2987 *orig_arg = cur_arg;
2988
2989 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002990 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002991
2992 return ACT_RET_PRS_OK;
2993
2994 do_resolve_parse_error:
2995 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2996 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2997 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2998 args[cur_arg]);
2999 return ACT_RET_PRS_ERR;
3000}
3001
3002static struct action_kw_list http_req_kws = { { }, {
3003 { "do-resolve", dns_parse_do_resolve, 1 },
3004 { /* END */ }
3005}};
3006
3007INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
3008
3009static struct action_kw_list tcp_req_cont_actions = {ILH, {
3010 { "do-resolve", dns_parse_do_resolve, 1 },
3011 { /* END */ }
3012}};
3013
3014INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
3015
3016/* Check an "http-request do-resolve" action.
3017 *
3018 * The function returns 1 in success case, otherwise, it returns 0 and err is
3019 * filled.
3020 */
3021int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
3022{
3023 struct dns_resolvers *resolvers = NULL;
3024
3025 if (rule->arg.dns.resolvers_id == NULL) {
3026 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
3027 return 0;
3028 }
3029
3030 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
3031 if (resolvers == NULL) {
3032 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
3033 return 0;
3034 }
3035 rule->arg.dns.resolvers = resolvers;
3036
3037 return 1;
3038}
3039
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003040REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01003041REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);