blob: fa1a246925af41f6df9e0f29e1e5b5a7c3276f4a [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 Assmann201c07f2017-05-22 15:17:15 +0200217
Christopher Faulet67957bd2017-09-27 11:00:59 +0200218/* 2 bytes random generator to generate DNS query ID */
219static inline uint16_t dns_rnd16(void)
220{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200221 if (!dns_query_id_seed)
222 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200223 dns_query_id_seed ^= dns_query_id_seed << 13;
224 dns_query_id_seed ^= dns_query_id_seed >> 7;
225 dns_query_id_seed ^= dns_query_id_seed << 17;
226 return dns_query_id_seed;
227}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200228
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200229
Christopher Faulet67957bd2017-09-27 11:00:59 +0200230static inline int dns_resolution_timeout(struct dns_resolution *res)
231{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100232 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200233}
234
Christopher Faulet67957bd2017-09-27 11:00:59 +0200235/* Updates a resolvers' task timeout for next wake up and queue it */
236static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200237{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200238 struct dns_resolution *res;
239 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200240
Christopher Faulet67957bd2017-09-27 11:00:59 +0200241 next = tick_add(now_ms, resolvers->timeout.resolve);
242 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
243 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
244 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
245 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200246
Christopher Faulet67957bd2017-09-27 11:00:59 +0200247 list_for_each_entry(res, &resolvers->resolutions.wait, list)
248 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200249
Christopher Faulet67957bd2017-09-27 11:00:59 +0200250 resolvers->t->expire = next;
251 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200252}
253
Christopher Faulet67957bd2017-09-27 11:00:59 +0200254/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
255 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200256 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200257static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200258{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200259 struct dgram_conn *dgram = ns->dgram;
260 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200261
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262 /* Already connected */
263 if (dgram->t.sock.fd != -1)
264 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200265
Christopher Faulet67957bd2017-09-27 11:00:59 +0200266 /* Create an UDP socket and connect it on the nameserver's IP/Port */
267 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
268 send_log(NULL, LOG_WARNING,
269 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
270 ns->resolvers->id, ns->id);
271 return -1;
272 }
273 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
274 send_log(NULL, LOG_WARNING,
275 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
276 ns->resolvers->id, ns->id);
277 close(fd);
278 return -1;
279 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281 /* Make the socket non blocking */
282 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200283
Christopher Faulet67957bd2017-09-27 11:00:59 +0200284 /* Add the fd in the fd list and update its parameters */
285 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100286 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287 fd_want_recv(fd);
288 return 0;
289}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200290
Christopher Faulet67957bd2017-09-27 11:00:59 +0200291/* Forges a DNS query. It needs the following information from the caller:
292 * - <query_id> : the DNS query id corresponding to this query
293 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
294 * - <hostname_dn> : hostname in domain name format
295 * - <hostname_dn_len> : length of <hostname_dn>
296 *
297 * To store the query, the caller must pass a buffer <buf> and its size
298 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
299 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200300 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200301static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
302 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200303{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200304 struct dns_header dns_hdr;
305 struct dns_question qinfo;
306 struct dns_additional_record edns;
307 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200308
Christopher Faulet67957bd2017-09-27 11:00:59 +0200309 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
310 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200311
Christopher Faulet67957bd2017-09-27 11:00:59 +0200312 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200313
Christopher Faulet67957bd2017-09-27 11:00:59 +0200314 /* Set dns query headers */
315 dns_hdr.id = (unsigned short) htons(query_id);
316 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
317 dns_hdr.qdcount = htons(1); /* 1 question */
318 dns_hdr.ancount = 0;
319 dns_hdr.nscount = 0;
320 dns_hdr.arcount = htons(1);
321 memcpy(p, &dns_hdr, sizeof(dns_hdr));
322 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200323
Christopher Faulet67957bd2017-09-27 11:00:59 +0200324 /* Set up query hostname */
325 memcpy(p, hostname_dn, hostname_dn_len);
326 p += hostname_dn_len;
327 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200328
Christopher Faulet67957bd2017-09-27 11:00:59 +0200329 /* Set up query info (type and class) */
330 qinfo.qtype = htons(query_type);
331 qinfo.qclass = htons(DNS_RCLASS_IN);
332 memcpy(p, &qinfo, sizeof(qinfo));
333 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200334
Christopher Faulet67957bd2017-09-27 11:00:59 +0200335 /* Set the DNS extension */
336 edns.name = 0;
337 edns.type = htons(DNS_RTYPE_OPT);
338 edns.udp_payload_size = htons(accepted_payload_size);
339 edns.extension = 0;
340 edns.data_length = 0;
341 memcpy(p, &edns, sizeof(edns));
342 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200343
Christopher Faulet67957bd2017-09-27 11:00:59 +0200344 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200345}
346
Christopher Faulet67957bd2017-09-27 11:00:59 +0200347/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
348 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200349 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200350static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200351{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200352 struct dns_resolvers *resolvers = resolution->resolvers;
353 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100354 int len;
355
356 /* Update resolution */
357 resolution->nb_queries = 0;
358 resolution->nb_responses = 0;
359 resolution->last_query = now_ms;
360
361 len = dns_build_query(resolution->query_id, resolution->query_type,
362 resolvers->accepted_payload_size,
363 resolution->hostname_dn, resolution->hostname_dn_len,
364 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200365
Christopher Faulet67957bd2017-09-27 11:00:59 +0200366 list_for_each_entry(ns, &resolvers->nameservers, list) {
367 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100368 int ret;
369
Christopher Faulet67957bd2017-09-27 11:00:59 +0200370 if (fd == -1) {
371 if (dns_connect_namesaver(ns) == -1)
372 continue;
373 fd = ns->dgram->t.sock.fd;
374 resolvers->nb_nameservers++;
375 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200376
Willy Tarreau0eae6322019-12-20 11:18:54 +0100377 if (len < 0)
378 goto snd_error;
379
380 ret = send(fd, trash.area, len, 0);
381 if (ret == len) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200382 ns->counters->sent++;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100383 resolution->nb_queries++;
384 continue;
385 }
386
387 if (ret == -1 && errno == EAGAIN) {
388 /* retry once the socket is ready */
389 fd_cant_send(fd);
390 continue;
391 }
392
393 snd_error:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200394 ns->counters->snd_error++;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100395 resolution->nb_queries++;
396 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200397
Christopher Faulet67957bd2017-09-27 11:00:59 +0200398 /* Push the resolution at the end of the active list */
399 LIST_DEL(&resolution->list);
400 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
401 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200402}
403
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
405 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200406 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200407static int
408dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200409{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200410 struct dns_resolvers *resolvers = resolution->resolvers;
411 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200412
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 /* Avoid sending requests for resolutions that don't yet have an
414 * hostname, ie resolutions linked to servers that do not yet have an
415 * fqdn */
416 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200417 return 0;
418
Christopher Faulet67957bd2017-09-27 11:00:59 +0200419 /* Check if a resolution has already been started for this server return
420 * directly to avoid resolution pill up. */
421 if (resolution->step != RSLV_STEP_NONE)
422 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200423
Christopher Faulet67957bd2017-09-27 11:00:59 +0200424 /* Generates a new query id. We try at most 100 times to find a free
425 * query id */
426 for (i = 0; i < 100; ++i) {
427 query_id = dns_rnd16();
428 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200429 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200430 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200431 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200432 if (query_id == -1) {
433 send_log(NULL, LOG_NOTICE,
434 "could not generate a query id for %s, in resolvers %s.\n",
435 resolution->hostname_dn, resolvers->id);
436 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200437 }
438
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439 /* Update resolution parameters */
440 resolution->query_id = query_id;
441 resolution->qid.key = query_id;
442 resolution->step = RSLV_STEP_RUNNING;
443 resolution->query_type = resolution->prefered_query_type;
444 resolution->try = resolvers->resolve_retries;
445 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200446
Christopher Faulet67957bd2017-09-27 11:00:59 +0200447 /* Send the DNS query */
448 resolution->try -= 1;
449 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200450 return 1;
451}
452
Christopher Faulet67957bd2017-09-27 11:00:59 +0200453/* Performs a name resolution for the requester <req> */
454void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200455{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200456 struct dns_resolvers *resolvers;
457 struct dns_resolution *res;
458 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200459
Christopher Faulet67957bd2017-09-27 11:00:59 +0200460 if (!req || !req->resolution)
461 return;
462 res = req->resolution;
463 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200464
Christopher Faulet67957bd2017-09-27 11:00:59 +0200465 /* The resolution must not be triggered yet. Use the cached response, if
466 * valid */
467 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200468 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
469 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
470 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200471}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200472
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200473
Christopher Faulet67957bd2017-09-27 11:00:59 +0200474/* Resets some resolution parameters to initial values and also delete the query
475 * ID from the resolver's tree.
476 */
477static void dns_reset_resolution(struct dns_resolution *resolution)
478{
479 /* update resolution status */
480 resolution->step = RSLV_STEP_NONE;
481 resolution->try = 0;
482 resolution->last_resolution = now_ms;
483 resolution->nb_queries = 0;
484 resolution->nb_responses = 0;
485 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200486
Christopher Faulet67957bd2017-09-27 11:00:59 +0200487 /* clean up query id */
488 eb32_delete(&resolution->qid);
489 resolution->query_id = 0;
490 resolution->qid.key = 0;
491}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200492
Christopher Faulet67957bd2017-09-27 11:00:59 +0200493/* Returns the query id contained in a DNS response */
494static inline unsigned short dns_response_get_query_id(unsigned char *resp)
495{
496 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200497}
498
Christopher Faulet67957bd2017-09-27 11:00:59 +0200499
500/* Analyses, re-builds and copies the name <name> from the DNS response packet
501 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
502 * for compressed data. The result is copied into <dest>, ensuring we don't
503 * overflow using <dest_len> Returns the number of bytes the caller can move
504 * forward. If 0 it means an error occurred while parsing the name. <offset> is
505 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200506 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200507int dns_read_name(unsigned char *buffer, unsigned char *bufend,
508 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100509 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200510{
511 int nb_bytes = 0, n = 0;
512 int label_len;
513 unsigned char *reader = name;
514 char *dest = destination;
515
516 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100517 if (reader >= bufend)
518 goto err;
519
Christopher Faulet67957bd2017-09-27 11:00:59 +0200520 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200521 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100522 if (reader + 1 >= bufend)
523 goto err;
524
Christopher Faulet67957bd2017-09-27 11:00:59 +0200525 /* Must point BEFORE current position */
526 if ((buffer + reader[1]) > reader)
527 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200528
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100529 if (depth++ > 100)
530 goto err;
531
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530532 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100533 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200534 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200535 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200536
Christopher Faulet67957bd2017-09-27 11:00:59 +0200537 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200538 nb_bytes += n;
539 goto out;
540 }
541
542 label_len = *reader;
543 if (label_len == 0)
544 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200545
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200546 /* Check if:
547 * - we won't read outside the buffer
548 * - there is enough place in the destination
549 */
550 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200552
553 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200554 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200555
556 memcpy(dest, reader, label_len);
557
Christopher Faulet67957bd2017-09-27 11:00:59 +0200558 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200559 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200560 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200561 }
562
Christopher Faulet67957bd2017-09-27 11:00:59 +0200563 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200564 /* offset computation:
565 * parse from <name> until finding either NULL or a pointer "c0xx"
566 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200567 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200568 *offset = 0;
569 while (reader < bufend) {
570 if ((reader[0] & 0xc0) == 0xc0) {
571 *offset += 2;
572 break;
573 }
574 else if (*reader == 0) {
575 *offset += 1;
576 break;
577 }
578 *offset += 1;
579 ++reader;
580 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200581 return nb_bytes;
582
Christopher Faulet67957bd2017-09-27 11:00:59 +0200583 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200584 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200585}
586
Christopher Faulet67957bd2017-09-27 11:00:59 +0200587/* Checks for any obsolete record, also identify any SRV request, and try to
588 * find a corresponding server.
589*/
590static void dns_check_dns_response(struct dns_resolution *res)
591{
592 struct dns_resolvers *resolvers = res->resolvers;
593 struct dns_requester *req, *reqback;
594 struct dns_answer_item *item, *itemback;
595 struct server *srv;
596 struct dns_srvrq *srvrq;
597
598 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +0200599 struct dns_answer_item *ar_item = item->ar_item;
600
601 /* clean up obsolete Additional record */
602 if (ar_item && (ar_item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
603 pool_free(dns_answer_item_pool, ar_item);
604 item->ar_item = NULL;
605 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200606
607 /* Remove obsolete items */
608 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
609 if (item->type != DNS_RTYPE_SRV)
610 goto rm_obselete_item;
611
612 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
613 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
614 continue;
615
616 /* Remove any associated server */
617 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100618 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200619 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
620 item->data_len == srv->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +0200621 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200622 snr_update_srv_status(srv, 1);
623 free(srv->hostname);
624 free(srv->hostname_dn);
625 srv->hostname = NULL;
626 srv->hostname_dn = NULL;
627 srv->hostname_dn_len = 0;
Christopher Faulet8427aa62021-02-23 12:24:09 +0100628 memset(&srv->addr, 0, sizeof(srv->addr));
629 srv->svc_port = 0;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200630 dns_unlink_resolution(srv->dns_requester);
631 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100632 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200633 }
634 }
635
636 rm_obselete_item:
637 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +0200638 if (item->ar_item) {
639 pool_free(dns_answer_item_pool, item->ar_item);
640 item->ar_item = NULL;
641 }
Willy Tarreaubafbe012017-11-24 17:34:44 +0100642 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200643 continue;
644 }
645
646 if (item->type != DNS_RTYPE_SRV)
647 continue;
648
649 /* Now process SRV records */
650 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
651 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
652 continue;
653
654 /* Check if a server already uses that hostname */
655 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100656 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200657 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
658 item->data_len == srv->hostname_dn_len &&
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200659 !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200660 break;
661 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100662 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200663 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200664
665 /* If not, try to find a server with undefined hostname */
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200666 if (!srv) {
667 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
668 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
669 if (srv->srvrq == srvrq && !srv->hostname_dn)
670 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100671 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100672 }
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200673 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200674
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200675 /* And update this server, if found (srv is locked here) */
676 if (srv) {
Baptiste Assmann13a92322019-06-07 09:40:55 +0200677 /* Check if an Additional Record is associated to this SRV record.
678 * Perform some sanity checks too to ensure the record can be used.
679 * If all fine, we simply pick up the IP address found and associate
680 * it to the server.
681 */
682 if ((item->ar_item != NULL) &&
683 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
684 {
685
686 switch (item->ar_item->type) {
687 case DNS_RTYPE_A:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200688 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 +0200689 break;
690 case DNS_RTYPE_AAAA:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200691 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 +0200692 break;
693 }
694
695 srv->flags |= SRV_F_NO_RESOLUTION;
696 }
697
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200698 if (!srv->hostname_dn) {
699 const char *msg = NULL;
700 char hostname[DNS_MAX_NAME_SIZE];
701
702 if (dns_dn_label_to_str(item->target, item->data_len+1,
703 hostname, DNS_MAX_NAME_SIZE) == -1) {
704 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
705 continue;
706 }
707 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
708 if (msg)
709 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
710 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200711
Baptiste Assmann87138c32020-08-04 10:57:21 +0200712 /* now we have an IP address associated to this server, we can update its status */
713 snr_update_srv_status(srv, 0);
714
Christopher Faulet67957bd2017-09-27 11:00:59 +0200715 srv->svc_port = item->port;
716 srv->flags &= ~SRV_F_MAPPORTS;
717 if ((srv->check.state & CHK_ST_CONFIGURED) &&
718 !(srv->flags & SRV_F_CHECKPORT))
719 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100720
Daniel Corbettf8716912019-11-17 09:48:56 -0500721 if (!srv->dns_opts.ignore_weight) {
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200722 char weight[9];
723 int ha_weight;
724
Daniel Corbettf8716912019-11-17 09:48:56 -0500725 /* DNS weight range if from 0 to 65535
726 * HAProxy weight is from 0 to 256
727 * The rule below ensures that weight 0 is well respected
728 * while allowing a "mapping" from DNS weight into HAProxy's one.
729 */
730 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100731
Daniel Corbettf8716912019-11-17 09:48:56 -0500732 snprintf(weight, sizeof(weight), "%d", ha_weight);
733 server_parse_weight_change_request(srv, weight);
734 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100735 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200736 }
737 }
738 }
739}
740
741/* Validates that the buffer DNS response provided in <resp> and finishing
742 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200743 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200744 * The result is stored in <resolution>' response, buf_response,
745 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200746 *
747 * This function returns one of the DNS_RESP_* code to indicate the type of
748 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200749 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200750static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
751 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 unsigned char *reader;
754 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200755 int len, flags, offset;
756 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200757 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200759 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200760 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200761 int i, found = 0;
Willy Tarreau963f7012020-07-22 17:00:45 +0200762 int cause = DNS_RESP_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200763
Christopher Faulet67957bd2017-09-27 11:00:59 +0200764 reader = resp;
765 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200767 dns_query = NULL;
Willy Tarreau963f7012020-07-22 17:00:45 +0200768 dns_answer_record = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200769
Christopher Faulet67957bd2017-09-27 11:00:59 +0200770 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200771 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200772
773 /* query id */
774 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200775 goto invalid_resp;
776
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200777 dns_p->header.id = reader[0] * 256 + reader[1];
778 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200779
Christopher Faulet67957bd2017-09-27 11:00:59 +0200780 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200781 * First byte contains:
782 * - response flag (1 bit)
783 * - opcode (4 bits)
784 * - authoritative (1 bit)
785 * - truncated (1 bit)
786 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200787 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200788 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200789 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200790
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200791 flags = reader[0] * 256 + reader[1];
792
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200793 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
Willy Tarreau963f7012020-07-22 17:00:45 +0200794 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
795 cause = DNS_RESP_NX_DOMAIN;
796 goto return_error;
797 }
798 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) {
799 cause = DNS_RESP_REFUSED;
800 goto return_error;
801 }
802 else {
803 cause = DNS_RESP_ERROR;
804 goto return_error;
805 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200806 }
807
Christopher Faulet67957bd2017-09-27 11:00:59 +0200808 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200809 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200810
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200811 /* 2 bytes for question count */
812 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200813 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200814 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200815 /* (for now) we send one query only, so we expect only one in the
816 * response too */
Willy Tarreau963f7012020-07-22 17:00:45 +0200817 if (dns_p->header.qdcount != 1) {
818 cause = DNS_RESP_QUERY_COUNT_ERROR;
819 goto return_error;
820 }
821
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200822 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200823 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200824 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200825
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200826 /* 2 bytes for answer count */
827 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200828 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200829 dns_p->header.ancount = reader[0] * 256 + reader[1];
Willy Tarreau963f7012020-07-22 17:00:45 +0200830 if (dns_p->header.ancount == 0) {
831 cause = DNS_RESP_ANCOUNT_ZERO;
832 goto return_error;
833 }
834
Christopher Faulet67957bd2017-09-27 11:00:59 +0200835 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200836 if (dns_p->header.ancount > max_answer_records)
Willy Tarreau963f7012020-07-22 17:00:45 +0200837 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200838 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200839
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200840 /* 2 bytes authority count */
841 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200842 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200843 dns_p->header.nscount = reader[0] * 256 + reader[1];
844 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200845
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200846 /* 2 bytes additional count */
847 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200848 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200849 dns_p->header.arcount = reader[0] * 256 + reader[1];
850 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200851
Christopher Faulet67957bd2017-09-27 11:00:59 +0200852 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200853 LIST_INIT(&dns_p->query_list);
854 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 +0200855 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200856 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200857 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200858 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200859 goto invalid_resp;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200860 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200861 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200862
Christopher Faulet67957bd2017-09-27 11:00:59 +0200863 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200864 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200865 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200866 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100867 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200868
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200869 if (len == 0)
Willy Tarreau963f7012020-07-22 17:00:45 +0200870 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200871
872 reader += offset;
873 previous_dname = dns_query->name;
874
875 /* move forward 2 bytes for question type */
876 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200877 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200878 dns_query->type = reader[0] * 256 + reader[1];
879 reader += 2;
880
881 /* move forward 2 bytes for question class */
882 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200883 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200884 dns_query->class = reader[0] * 256 + reader[1];
885 reader += 2;
886 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200887
Baptiste Assmann251abb92017-08-11 09:58:27 +0200888 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200889 * because a TRUNCATED SRV query type response can still be exploited */
Willy Tarreau963f7012020-07-22 17:00:45 +0200890 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) {
891 cause = DNS_RESP_TRUNCATED;
892 goto return_error;
893 }
Baptiste Assmann251abb92017-08-11 09:58:27 +0200894
Baptiste Assmann325137d2015-04-13 23:40:55 +0200895 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200896 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200897 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200898 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200899 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200900
Willy Tarreaubafbe012017-11-24 17:34:44 +0100901 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200902 if (dns_answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +0200903 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200904
Baptiste Assmann155bc682021-01-15 17:01:24 +0100905 /* initialization */
906 dns_answer_record->ar_item = NULL;
907
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200908 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100909 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200910
Willy Tarreau963f7012020-07-22 17:00:45 +0200911 if (len == 0)
912 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200913
Christopher Faulet67957bd2017-09-27 11:00:59 +0200914 /* Check if the current record dname is valid. previous_dname
915 * points either to queried dname or last CNAME target */
Olivier Houchardb17b8842020-04-01 18:30:27 +0200916 if (dns_query->type != DNS_RTYPE_SRV && dns_hostname_cmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200917 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200918 /* First record, means a mismatch issue between
919 * queried dname and dname found in the first
920 * record */
Willy Tarreau963f7012020-07-22 17:00:45 +0200921 goto invalid_resp;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200922 }
923 else {
924 /* If not the first record, this means we have a
Willy Tarreau963f7012020-07-22 17:00:45 +0200925 * CNAME resolution error.
926 */
927 cause = DNS_RESP_CNAME_ERROR;
928 goto return_error;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200929 }
930
Baptiste Assmann325137d2015-04-13 23:40:55 +0200931 }
932
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200933 memcpy(dns_answer_record->name, tmpname, len);
934 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200935
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200936 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +0200937 if (reader >= bufend)
938 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200939
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200940 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200941 if (reader + 2 > bufend)
942 goto invalid_resp;
943
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200944 dns_answer_record->type = reader[0] * 256 + reader[1];
945 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200946
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200947 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200948 if (reader + 2 > bufend)
949 goto invalid_resp;
950
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200951 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200952 reader += 2;
953
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200954 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200955 if (reader + 4 > bufend)
956 goto invalid_resp;
957
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200958 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
959 + reader[2] * 256 + reader[3];
960 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200961
Christopher Faulet67957bd2017-09-27 11:00:59 +0200962 /* Now reading data len */
Willy Tarreau963f7012020-07-22 17:00:45 +0200963 if (reader + 2 > bufend)
964 goto invalid_resp;
965
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200966 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200967
Christopher Faulet67957bd2017-09-27 11:00:59 +0200968 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200969 reader += 2;
970
Willy Tarreau963f7012020-07-22 17:00:45 +0200971 if (reader + dns_answer_record->data_len > bufend)
972 goto invalid_resp;
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100973
Christopher Faulet67957bd2017-09-27 11:00:59 +0200974 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200975 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200976 case DNS_RTYPE_A:
977 /* ipv4 is stored on 4 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +0200978 if (dns_answer_record->data_len != 4)
979 goto invalid_resp;
980
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200981 dns_answer_record->address.sa_family = AF_INET;
982 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
983 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200984 break;
985
986 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200987 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200988 * no IP could be found and last record was a CNAME. Could be triggered
989 * by a wrong query type
990 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200991 * + 1 because dns_answer_record_id starts at 0
992 * while number of answers is an integer and
993 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200994 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200995 if (i + 1 == dns_p->header.ancount) {
Willy Tarreau963f7012020-07-22 17:00:45 +0200996 cause = DNS_RESP_CNAME_ERROR;
997 goto return_error;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200998 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200999
1000 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +01001001 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +02001002 if (len == 0)
1003 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001004
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001005 memcpy(dns_answer_record->target, tmpname, len);
1006 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001007 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001008 break;
1009
Olivier Houchard8da5f982017-08-04 18:35:36 +02001010
1011 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001012 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +02001013 * - 2 bytes for the priority
1014 * - 2 bytes for the weight
1015 * - 2 bytes for the port
1016 * - the target hostname
1017 */
Willy Tarreau963f7012020-07-22 17:00:45 +02001018 if (dns_answer_record->data_len <= 6)
1019 goto invalid_resp;
1020
Willy Tarreaud5370e12017-09-19 14:59:52 +02001021 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001022 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +02001023 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001024 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +02001025 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001026 reader += sizeof(uint16_t);
1027 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +01001028 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +02001029 if (len == 0)
1030 goto invalid_resp;
1031
Olivier Houchard8da5f982017-08-04 18:35:36 +02001032 dns_answer_record->data_len = len;
1033 memcpy(dns_answer_record->target, tmpname, len);
1034 dns_answer_record->target[len] = 0;
Baptiste Assmann155bc682021-01-15 17:01:24 +01001035 if (dns_answer_record->ar_item != NULL) {
1036 pool_free(dns_answer_item_pool, dns_answer_record->ar_item);
1037 dns_answer_record->ar_item = NULL;
1038 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001039 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001040
Baptiste Assmann325137d2015-04-13 23:40:55 +02001041 case DNS_RTYPE_AAAA:
1042 /* ipv6 is stored on 16 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001043 if (dns_answer_record->data_len != 16)
1044 goto invalid_resp;
1045
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001046 dns_answer_record->address.sa_family = AF_INET6;
1047 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1048 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001049 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001050
Baptiste Assmann325137d2015-04-13 23:40:55 +02001051 } /* switch (record type) */
1052
Christopher Faulet67957bd2017-09-27 11:00:59 +02001053 /* Increment the counter for number of records saved into our
1054 * local response */
1055 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +02001056
Christopher Faulet67957bd2017-09-27 11:00:59 +02001057 /* Move forward dns_answer_record->data_len for analyzing next
1058 * record in the response */
1059 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1060 ? offset
1061 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001062
1063 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001064 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001065 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1066 if (tmp_record->type != dns_answer_record->type)
1067 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001068
1069 switch(tmp_record->type) {
1070 case DNS_RTYPE_A:
1071 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1072 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1073 sizeof(in_addr_t)))
1074 found = 1;
1075 break;
1076
1077 case DNS_RTYPE_AAAA:
1078 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1079 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1080 sizeof(struct in6_addr)))
1081 found = 1;
1082 break;
1083
Olivier Houchard8da5f982017-08-04 18:35:36 +02001084 case DNS_RTYPE_SRV:
1085 if (dns_answer_record->data_len == tmp_record->data_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001086 !dns_hostname_cmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +02001087 dns_answer_record->port == tmp_record->port) {
1088 tmp_record->weight = dns_answer_record->weight;
1089 found = 1;
1090 }
1091 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001092
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001093 default:
1094 break;
1095 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001096
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001097 if (found == 1)
1098 break;
1099 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001101 if (found == 1) {
Christopher Fauletc22e27c2021-02-23 12:22:29 +01001102 if (tmp_record->type != DNS_RTYPE_SRV || tmp_record->ar_item != NULL)
1103 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001104 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001105 dns_answer_record = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001106 }
1107 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001108 dns_answer_record->last_seen = now.tv_sec;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001109 dns_answer_record->ar_item = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001110 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
Willy Tarreau963f7012020-07-22 17:00:45 +02001111 dns_answer_record = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001112 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001113 } /* for i 0 to ancount */
1114
Christopher Faulet67957bd2017-09-27 11:00:59 +02001115 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001116 dns_p->header.ancount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001117
Baptiste Assmann37950c82020-02-19 01:08:51 +01001118 /* now parsing additional records for SRV queries only */
1119 if (dns_query->type != DNS_RTYPE_SRV)
1120 goto skip_parsing_additional_records;
Willy Tarreau963f7012020-07-22 17:00:45 +02001121
Jerome Magnin4002f8d2020-07-26 12:13:12 +02001122 /* if we find Authority records, just skip them */
1123 for (i = 0; i < dns_p->header.nscount; i++) {
1124 offset = 0;
1125 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE,
1126 &offset, 0);
1127 if (len == 0)
1128 continue;
1129
1130 if (reader + offset + 10 >= bufend)
1131 goto invalid_resp;
1132
1133 reader += offset;
1134 /* skip 2 bytes for class */
1135 reader += 2;
1136 /* skip 2 bytes for type */
1137 reader += 2;
1138 /* skip 4 bytes for ttl */
1139 reader += 4;
1140 /* read data len */
1141 len = reader[0] * 256 + reader[1];
1142 reader += 2;
1143
1144 if (reader + len >= bufend)
1145 goto invalid_resp;
1146
1147 reader += len;
1148 }
1149
Baptiste Assmann13a92322019-06-07 09:40:55 +02001150 nb_saved_records = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001151 for (i = 0; i < dns_p->header.arcount; i++) {
1152 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +02001153 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001154
1155 dns_answer_record = pool_alloc(dns_answer_item_pool);
1156 if (dns_answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +02001157 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001158
1159 offset = 0;
1160 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
1161
1162 if (len == 0) {
1163 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001164 dns_answer_record = NULL;
Baptiste Assmann37950c82020-02-19 01:08:51 +01001165 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001166 }
1167
1168 memcpy(dns_answer_record->name, tmpname, len);
1169 dns_answer_record->name[len] = 0;
1170
1171 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +02001172 if (reader >= bufend)
1173 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001174
1175 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001176 if (reader + 2 > bufend)
1177 goto invalid_resp;
1178
Baptiste Assmann13a92322019-06-07 09:40:55 +02001179 dns_answer_record->type = reader[0] * 256 + reader[1];
1180 reader += 2;
1181
1182 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001183 if (reader + 2 > bufend)
1184 goto invalid_resp;
1185
Baptiste Assmann13a92322019-06-07 09:40:55 +02001186 dns_answer_record->class = reader[0] * 256 + reader[1];
1187 reader += 2;
1188
1189 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +02001190 if (reader + 4 > bufend)
1191 goto invalid_resp;
1192
Baptiste Assmann13a92322019-06-07 09:40:55 +02001193 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1194 + reader[2] * 256 + reader[3];
1195 reader += 4;
1196
1197 /* Now reading data len */
Willy Tarreau963f7012020-07-22 17:00:45 +02001198 if (reader + 2 > bufend)
1199 goto invalid_resp;
1200
Baptiste Assmann13a92322019-06-07 09:40:55 +02001201 dns_answer_record->data_len = reader[0] * 256 + reader[1];
1202
1203 /* Move forward 2 bytes for data len */
1204 reader += 2;
1205
Willy Tarreau963f7012020-07-22 17:00:45 +02001206 if (reader + dns_answer_record->data_len > bufend)
1207 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001208
1209 /* Analyzing record content */
1210 switch (dns_answer_record->type) {
1211 case DNS_RTYPE_A:
1212 /* ipv4 is stored on 4 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001213 if (dns_answer_record->data_len != 4)
1214 goto invalid_resp;
1215
Baptiste Assmann13a92322019-06-07 09:40:55 +02001216 dns_answer_record->address.sa_family = AF_INET;
1217 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1218 reader, dns_answer_record->data_len);
1219 break;
1220
1221 case DNS_RTYPE_AAAA:
1222 /* ipv6 is stored on 16 bytes */
Willy Tarreau963f7012020-07-22 17:00:45 +02001223 if (dns_answer_record->data_len != 16)
1224 goto invalid_resp;
1225
Baptiste Assmann13a92322019-06-07 09:40:55 +02001226 dns_answer_record->address.sa_family = AF_INET6;
1227 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1228 reader, dns_answer_record->data_len);
1229 break;
1230
1231 default:
1232 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001233 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001234 continue;
1235
1236 } /* switch (record type) */
1237
1238 /* Increment the counter for number of records saved into our
1239 * local response */
1240 nb_saved_records++;
1241
1242 /* Move forward dns_answer_record->data_len for analyzing next
1243 * record in the response */
1244 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
1245 ? offset
1246 : dns_answer_record->data_len);
1247
1248 /* Lookup to see if we already had this entry */
1249 found = 0;
1250 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1251 if (tmp_record->type != dns_answer_record->type)
1252 continue;
1253
1254 switch(tmp_record->type) {
1255 case DNS_RTYPE_A:
1256 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1257 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1258 sizeof(in_addr_t)))
1259 found = 1;
1260 break;
1261
1262 case DNS_RTYPE_AAAA:
1263 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1264 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1265 sizeof(struct in6_addr)))
1266 found = 1;
1267 break;
1268
1269 default:
1270 break;
1271 }
1272
1273 if (found == 1)
1274 break;
1275 }
1276
1277 if (found == 1) {
1278 tmp_record->last_seen = now.tv_sec;
1279 pool_free(dns_answer_item_pool, dns_answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001280 dns_answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001281 }
1282 else {
1283 dns_answer_record->last_seen = now.tv_sec;
1284 dns_answer_record->ar_item = NULL;
1285
1286 // looking for the SRV record in the response list linked to this additional record
1287 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001288 if (tmp_record->type == DNS_RTYPE_SRV &&
Baptiste Assmann155bc682021-01-15 17:01:24 +01001289 tmp_record->ar_item == NULL &&
Christopher Faulet5a891752020-09-08 10:06:01 +02001290 !dns_hostname_cmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len)) {
1291 /* Always use the received additional record to refresh info */
Christopher Faulet5a891752020-09-08 10:06:01 +02001292 tmp_record->ar_item = dns_answer_record;
Christopher Fauletc22e27c2021-02-23 12:22:29 +01001293 tmp_record->last_seen = dns_answer_record->last_seen;
Christopher Fauletd67f8bb2021-02-23 11:59:19 +01001294 dns_answer_record = NULL;
Christopher Faulet5a891752020-09-08 10:06:01 +02001295 break;
1296 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001297 }
Christopher Fauletd67f8bb2021-02-23 11:59:19 +01001298 if (dns_answer_record) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001299 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Fauletd67f8bb2021-02-23 11:59:19 +01001300 dns_answer_record = NULL;
1301 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001302 }
1303 } /* for i 0 to arcount */
1304
Baptiste Assmann37950c82020-02-19 01:08:51 +01001305 skip_parsing_additional_records:
1306
Baptiste Assmann13a92322019-06-07 09:40:55 +02001307 /* Save the number of records we really own */
1308 dns_p->header.arcount = nb_saved_records;
1309
Christopher Faulet67957bd2017-09-27 11:00:59 +02001310 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001311 return DNS_RESP_VALID;
Willy Tarreau963f7012020-07-22 17:00:45 +02001312
1313 invalid_resp:
1314 cause = DNS_RESP_INVALID;
1315
1316 return_error:
1317 pool_free(dns_answer_item_pool, dns_answer_record);
1318 return cause;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001319}
1320
Christopher Faulet67957bd2017-09-27 11:00:59 +02001321/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001322 * If existing IP not found, return the first IP matching family_priority,
1323 * otherwise, first ip found
1324 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001325 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001326 * For both cases above, dns_validate_dns_response is required
1327 * returns one of the DNS_UPD_* code
1328 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001329int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001330 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001331 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001332 void **newip, short *newip_sin_family,
1333 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001334{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001335 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001336 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001337 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001338 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001339 int currentip_sel;
1340 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001341 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001342 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001343
Christopher Faulet67957bd2017-09-27 11:00:59 +02001344 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001345 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001346 *newip = newip4 = newip6 = NULL;
1347 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001348 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001349 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001350
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001351 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001352 * Top priority is the preferred network ip version,
1353 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001354 * the last priority is the currently used IP,
1355 *
1356 * For these three priorities, a score is calculated. The
1357 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001358 * 8 - preferred ip version.
1359 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001360 * 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 +01001361 * 1 - current ip.
1362 * The result with the biggest score is returned.
1363 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001364
1365 list_for_each_entry(record, &dns_p->answer_list, list) {
1366 void *ip;
1367 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001368
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001369 if (record->type == DNS_RTYPE_A) {
1370 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1371 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001372 }
1373 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001374 ip_type = AF_INET6;
1375 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001376 }
1377 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001378 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001379 score = 0;
1380
Joseph Herlant42cf6392018-11-15 10:33:28 -08001381 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001382 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001383 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001384
Joseph Herlant42cf6392018-11-15 10:33:28 -08001385 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001386 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001387
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001388 /* Compare only the same addresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001389 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001390 continue;
1391
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001392 if ((ip_type == AF_INET &&
1393 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001394 &dns_opts->pref_net[j].mask.in4,
1395 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001396 (ip_type == AF_INET6 &&
1397 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001398 &dns_opts->pref_net[j].mask.in6,
1399 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001400 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001401 break;
1402 }
1403 }
1404
Christopher Faulet67957bd2017-09-27 11:00:59 +02001405 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001406 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001407 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001408 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001409 if (!allowed_duplicated_ip) {
1410 continue;
1411 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001412 } else {
1413 score += 2;
1414 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001415
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001416 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001417 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001418 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001419 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001420 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001421 !memcmp(ip, currentip, 16)))) {
1422 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001423 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001424 }
1425 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001426 currentip_sel = 0;
1427
1428 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001429 * score. The maximum score is 15, if this value is reached, we
1430 * break the parsing. Implicitly, this score is reached the ip
1431 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001432 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001433 if (ip_type == AF_INET)
1434 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001435 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001436 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001437 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001438 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001439 return DNS_UPD_NO;
1440 max_score = score;
1441 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001442 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001443
Christopher Faulet67957bd2017-09-27 11:00:59 +02001444 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001445 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001446 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001447
Christopher Faulet67957bd2017-09-27 11:00:59 +02001448 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001449 if (family_priority == AF_INET) {
1450 if (newip4) {
1451 *newip = newip4;
1452 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001453 }
1454 else if (newip6) {
1455 *newip = newip6;
1456 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001457 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001458 if (!currentip_found)
1459 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001460 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001461 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001462 else if (family_priority == AF_INET6) {
1463 if (newip6) {
1464 *newip = newip6;
1465 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001466 }
1467 else if (newip4) {
1468 *newip = newip4;
1469 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001470 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001471 if (!currentip_found)
1472 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001473 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001474 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001475 else if (family_priority == AF_UNSPEC) {
1476 if (newip6) {
1477 *newip = newip6;
1478 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001479 }
1480 else if (newip4) {
1481 *newip = newip4;
1482 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001483 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001484 if (!currentip_found)
1485 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001486 }
1487
Christopher Faulet67957bd2017-09-27 11:00:59 +02001488 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001489 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001490
Christopher Faulet67957bd2017-09-27 11:00:59 +02001491 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001492 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001493 /* Move the first record to the end of the list, for internal
1494 * round robin */
1495 LIST_DEL(&record->list);
1496 LIST_ADDQ(&dns_p->answer_list, &record->list);
1497 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001498 }
1499 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001500}
1501
Christopher Faulet67957bd2017-09-27 11:00:59 +02001502/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001503 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001504 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1505 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001506 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001507 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1508 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001509 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001510int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001511{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001512 char *ptr;
1513 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001514
Christopher Faulet67957bd2017-09-27 11:00:59 +02001515 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001516 return -1;
1517
Christopher Faulet67957bd2017-09-27 11:00:59 +02001518 ptr = str;
1519 for (i = 0; i < dn_len-1; ++i) {
1520 sz = dn[i];
1521 if (i)
1522 *ptr++ = '.';
1523 memcpy(ptr, dn+i+1, sz);
1524 ptr += sz;
1525 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001526 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001527 *ptr++ = '\0';
1528 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001529}
1530
Christopher Faulet67957bd2017-09-27 11:00:59 +02001531/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1532 *
1533 * <str> must be a null-terminated string. <str_len> must include the
1534 * terminating null byte. <dn> buffer must be allocated and its size must be
1535 * passed in <dn_len>.
1536 *
1537 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1538 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001539 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001540int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001541{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001542 int i, offset;
1543
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 if (dn_len < str_len + 1)
1545 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001546
Christopher Faulet67957bd2017-09-27 11:00:59 +02001547 /* First byte of dn will be used to store the length of the first
1548 * label */
1549 offset = 0;
1550 for (i = 0; i < str_len; ++i) {
1551 if (str[i] == '.') {
1552 /* 2 or more consecutive dots is invalid */
1553 if (i == offset)
1554 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001555
Lukas Tribus81725b82020-02-27 15:47:24 +01001556 /* ignore trailing dot */
1557 if (i + 2 == str_len) {
1558 i++;
1559 break;
1560 }
1561
Christopher Faulet67957bd2017-09-27 11:00:59 +02001562 dn[offset] = (i - offset);
1563 offset = i+1;
1564 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001565 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001566 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001567 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001568 dn[offset] = (i - offset - 1);
1569 dn[i] = '\0';
1570 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001571}
1572
Christopher Faulet67957bd2017-09-27 11:00:59 +02001573/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001574 * - total size
1575 * - each label size individually
1576 * returns:
1577 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1578 * 1 when no error. <err> is left unaffected.
1579 */
1580int dns_hostname_validation(const char *string, char **err)
1581{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001582 int i;
1583
1584 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1585 if (err)
1586 *err = DNS_TOO_LONG_FQDN;
1587 return 0;
1588 }
1589
William Dauchyaecd5dc2020-01-26 19:52:34 +01001590 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001591 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001592 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1593 if (!(*string == '-' || *string == '_' ||
1594 (*string >= 'a' && *string <= 'z') ||
1595 (*string >= 'A' && *string <= 'Z') ||
1596 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001597 if (err)
1598 *err = DNS_INVALID_CHARACTER;
1599 return 0;
1600 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001601 i++;
1602 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001603 }
1604
William Dauchyaecd5dc2020-01-26 19:52:34 +01001605 if (!(*string))
1606 break;
1607
1608 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001609 if (err)
1610 *err = DNS_LABEL_TOO_LONG;
1611 return 0;
1612 }
1613
William Dauchyaecd5dc2020-01-26 19:52:34 +01001614 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001615 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001616 return 1;
1617}
1618
Christopher Faulet67957bd2017-09-27 11:00:59 +02001619/* Picks up an available resolution from the different resolution list
1620 * associated to a resolvers section, in this order:
1621 * 1. check in resolutions.curr for the same hostname and query_type
1622 * 2. check in resolutions.wait for the same hostname and query_type
1623 * 3. Get a new resolution from resolution pool
1624 *
1625 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001626 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001627static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1628 char **hostname_dn, int hostname_dn_len,
1629 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001630{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001631 struct dns_resolution *res;
1632
1633 if (!*hostname_dn)
1634 goto from_pool;
1635
1636 /* Search for same hostname and query type in resolutions.curr */
1637 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1638 if (!res->hostname_dn)
1639 continue;
1640 if ((query_type == res->prefered_query_type) &&
1641 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001642 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001643 return res;
1644 }
1645
1646 /* Search for same hostname and query type in resolutions.wait */
1647 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1648 if (!res->hostname_dn)
1649 continue;
1650 if ((query_type == res->prefered_query_type) &&
1651 hostname_dn_len == res->hostname_dn_len &&
Olivier Houchardb17b8842020-04-01 18:30:27 +02001652 !dns_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001653 return res;
1654 }
1655
1656 from_pool:
1657 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001658 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001659 if (res) {
1660 memset(res, 0, sizeof(*res));
1661 res->resolvers = resolvers;
1662 res->uuid = resolution_uuid;
1663 res->status = RSLV_STATUS_NONE;
1664 res->step = RSLV_STEP_NONE;
1665 res->last_valid = now_ms;
1666
1667 LIST_INIT(&res->requesters);
1668 LIST_INIT(&res->response.answer_list);
1669
1670 res->prefered_query_type = query_type;
1671 res->query_type = query_type;
1672 res->hostname_dn = *hostname_dn;
1673 res->hostname_dn_len = hostname_dn_len;
1674
1675 ++resolution_uuid;
1676
1677 /* Move the resolution to the resolvers wait queue */
1678 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1679 }
1680 return res;
1681}
1682
1683/* Releases a resolution from its requester(s) and move it back to the pool */
1684static void dns_free_resolution(struct dns_resolution *resolution)
1685{
1686 struct dns_requester *req, *reqback;
Christopher Faulet010ab352020-07-22 15:55:49 +02001687 struct dns_answer_item *item, *itemback;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001688
1689 /* clean up configuration */
1690 dns_reset_resolution(resolution);
1691 resolution->hostname_dn = NULL;
1692 resolution->hostname_dn_len = 0;
1693
1694 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1695 LIST_DEL(&req->list);
1696 req->resolution = NULL;
1697 }
1698
Christopher Faulet010ab352020-07-22 15:55:49 +02001699 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
1700 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +02001701 if (item->ar_item) {
1702 pool_free(dns_answer_item_pool, item->ar_item);
1703 item->ar_item = NULL;
1704 }
Christopher Faulet010ab352020-07-22 15:55:49 +02001705 pool_free(dns_answer_item_pool, item);
1706 }
1707
Christopher Faulet67957bd2017-09-27 11:00:59 +02001708 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001709 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001710}
1711
1712/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1713 * on success, -1 otherwise.
1714 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001715int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001716{
1717 struct dns_resolution *res = NULL;
1718 struct dns_requester *req;
1719 struct dns_resolvers *resolvers;
1720 struct server *srv = NULL;
1721 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001722 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001723 char **hostname_dn;
1724 int hostname_dn_len, query_type;
1725
1726 switch (requester_type) {
1727 case OBJ_TYPE_SERVER:
1728 srv = (struct server *)requester;
1729 hostname_dn = &srv->hostname_dn;
1730 hostname_dn_len = srv->hostname_dn_len;
1731 resolvers = srv->resolvers;
1732 query_type = ((srv->dns_opts.family_prio == AF_INET)
1733 ? DNS_RTYPE_A
1734 : DNS_RTYPE_AAAA);
1735 break;
1736
1737 case OBJ_TYPE_SRVRQ:
1738 srvrq = (struct dns_srvrq *)requester;
1739 hostname_dn = &srvrq->hostname_dn;
1740 hostname_dn_len = srvrq->hostname_dn_len;
1741 resolvers = srvrq->resolvers;
1742 query_type = DNS_RTYPE_SRV;
1743 break;
1744
Baptiste Assmann333939c2019-01-21 08:34:50 +01001745 case OBJ_TYPE_STREAM:
1746 stream = (struct stream *)requester;
1747 hostname_dn = &stream->dns_ctx.hostname_dn;
1748 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1749 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
Christopher Fauleta4168432020-01-24 18:08:42 +01001750 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001751 ? DNS_RTYPE_A
1752 : DNS_RTYPE_AAAA);
1753 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001754 default:
1755 goto err;
1756 }
1757
1758 /* Get a resolution from the resolvers' wait queue or pool */
1759 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1760 goto err;
1761
1762 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001763 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001764 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001765 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001766 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001767 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001768 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001769 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001770 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001771 req->owner = &srv->obj_type;
1772 srv->dns_requester = req;
1773 }
1774 else
1775 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001776 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001777 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001778
1779 req->requester_cb = snr_resolution_cb;
1780 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781 }
1782 else if (srvrq) {
1783 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001784 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001785 goto err;
1786 req->owner = &srvrq->obj_type;
1787 srvrq->dns_requester = req;
1788 }
1789 else
1790 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001791
1792 req->requester_cb = snr_resolution_cb;
Baptiste Assmann826060e2020-11-19 22:38:33 +01001793 req->requester_error_cb = srvrq_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001794 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001795 else if (stream) {
1796 if (stream->dns_ctx.dns_requester == NULL) {
1797 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1798 goto err;
1799 req->owner = &stream->obj_type;
1800 stream->dns_ctx.dns_requester = req;
1801 }
1802 else
1803 req = stream->dns_ctx.dns_requester;
1804
1805 req->requester_cb = act_resolution_cb;
1806 req->requester_error_cb = act_resolution_error_cb;
1807 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001808 else
1809 goto err;
1810
1811 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001812
1813 LIST_ADDQ(&res->requesters, &req->list);
1814 return 0;
1815
1816 err:
1817 if (res && LIST_ISEMPTY(&res->requesters))
1818 dns_free_resolution(res);
1819 return -1;
1820}
1821
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001822/* Removes a requester from a DNS resolution. It takes takes care of all the
Christopher Faulet67957bd2017-09-27 11:00:59 +02001823 * consequences. It also cleans up some parameters from the requester.
1824 */
1825void dns_unlink_resolution(struct dns_requester *requester)
1826{
1827 struct dns_resolution *res;
1828 struct dns_requester *req;
1829
1830 /* Nothing to do */
1831 if (!requester || !requester->resolution)
1832 return;
1833 res = requester->resolution;
1834
1835 /* Clean up the requester */
1836 LIST_DEL(&requester->list);
1837 requester->resolution = NULL;
1838
1839 /* We need to find another requester linked on this resolution */
1840 if (!LIST_ISEMPTY(&res->requesters))
1841 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1842 else {
1843 dns_free_resolution(res);
1844 return;
1845 }
1846
1847 /* Move hostname_dn related pointers to the next requester */
1848 switch (obj_type(req->owner)) {
1849 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001850 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1851 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001852 break;
1853 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001854 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1855 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001856 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001857 case OBJ_TYPE_STREAM:
1858 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1859 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1860 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001861 default:
1862 res->hostname_dn = NULL;
1863 res->hostname_dn_len = 0;
1864 break;
1865 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001866}
1867
Christopher Faulet67957bd2017-09-27 11:00:59 +02001868/* Called when a network IO is generated on a name server socket for an incoming
1869 * packet. It performs the following actions:
1870 * - check if the packet requires processing (not outdated resolution)
1871 * - ensure the DNS packet received is valid and call requester's callback
1872 * - call requester's error callback if invalid response
1873 * - check the dn_name in the packet against the one sent
1874 */
1875static void dns_resolve_recv(struct dgram_conn *dgram)
1876{
1877 struct dns_nameserver *ns, *tmpns;
1878 struct dns_resolvers *resolvers;
1879 struct dns_resolution *res;
1880 struct dns_query_item *query;
1881 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1882 unsigned char *bufend;
1883 int fd, buflen, dns_resp;
1884 int max_answer_records;
1885 unsigned short query_id;
1886 struct eb32_node *eb;
1887 struct dns_requester *req;
1888
1889 fd = dgram->t.sock.fd;
1890
1891 /* check if ready for reading */
1892 if (!fd_recv_ready(fd))
1893 return;
1894
1895 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001896 if ((ns = dgram->owner) == NULL) {
1897 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1898 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001899 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001900 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001901
1902 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001903 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001904
1905 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001906 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001907 /* read message received */
1908 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1909 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001910 /* FIXME : for now we consider EAGAIN only, but at
1911 * least we purge sticky errors that would cause us to
1912 * be called in loops.
1913 */
1914 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001915 fd_cant_recv(fd);
1916 break;
1917 }
1918
1919 /* message too big */
1920 if (buflen > resolvers->accepted_payload_size) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001921 ns->counters->too_big++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 continue;
1923 }
1924
1925 /* initializing variables */
1926 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1927
1928 /* read the query id from the packet (16 bits) */
1929 if (buf + 2 > bufend) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001930 ns->counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001931 continue;
1932 }
1933 query_id = dns_response_get_query_id(buf);
1934
1935 /* search the query_id in the pending resolution tree */
1936 eb = eb32_lookup(&resolvers->query_ids, query_id);
1937 if (eb == NULL) {
1938 /* unknown query id means an outdated response and can be safely ignored */
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001939 ns->counters->outdated++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940 continue;
1941 }
1942
William Dauchybe8a3872019-11-27 23:32:41 +01001943 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001944 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001945 /* number of responses received */
1946 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001947
Christopher Faulet67957bd2017-09-27 11:00:59 +02001948 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1949 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001950
Christopher Faulet67957bd2017-09-27 11:00:59 +02001951 switch (dns_resp) {
1952 case DNS_RESP_VALID:
1953 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001954
Christopher Faulet67957bd2017-09-27 11:00:59 +02001955 case DNS_RESP_INVALID:
1956 case DNS_RESP_QUERY_COUNT_ERROR:
1957 case DNS_RESP_WRONG_NAME:
1958 res->status = RSLV_STATUS_INVALID;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001959 ns->counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001961
Christopher Faulet67957bd2017-09-27 11:00:59 +02001962 case DNS_RESP_NX_DOMAIN:
1963 res->status = RSLV_STATUS_NX;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001964 ns->counters->nx++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001965 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001966
Christopher Faulet67957bd2017-09-27 11:00:59 +02001967 case DNS_RESP_REFUSED:
1968 res->status = RSLV_STATUS_REFUSED;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001969 ns->counters->refused++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001970 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001971
Christopher Faulet67957bd2017-09-27 11:00:59 +02001972 case DNS_RESP_ANCOUNT_ZERO:
1973 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001974 ns->counters->any_err++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001976
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 case DNS_RESP_CNAME_ERROR:
1978 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001979 ns->counters->cname_error++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001981
Christopher Faulet67957bd2017-09-27 11:00:59 +02001982 case DNS_RESP_TRUNCATED:
1983 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001984 ns->counters->truncated++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001985 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001986
Christopher Faulet67957bd2017-09-27 11:00:59 +02001987 case DNS_RESP_NO_EXPECTED_RECORD:
1988 case DNS_RESP_ERROR:
1989 case DNS_RESP_INTERNAL:
1990 res->status = RSLV_STATUS_OTHER;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02001991 ns->counters->other++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001992 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001993 }
1994
Christopher Faulet67957bd2017-09-27 11:00:59 +02001995 /* Wait all nameservers response to handle errors */
1996 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1997 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001998
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 /* Process error codes */
2000 if (dns_resp != DNS_RESP_VALID) {
2001 if (res->prefered_query_type != res->query_type) {
2002 /* The fallback on the query type was already performed,
2003 * so check the try counter. If it falls to 0, we can
2004 * report an error. Else, wait the next attempt. */
2005 if (!res->try)
2006 goto report_res_error;
2007 }
2008 else {
2009 /* Fallback from A to AAAA or the opposite and re-send
2010 * the resolution immediately. try counter is not
2011 * decremented. */
2012 if (res->prefered_query_type == DNS_RTYPE_A) {
2013 res->query_type = DNS_RTYPE_AAAA;
2014 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002015 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002016 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
2017 res->query_type = DNS_RTYPE_A;
2018 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002019 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002020 }
2021 continue;
2022 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002023
Christopher Faulet67957bd2017-09-27 11:00:59 +02002024 /* Now let's check the query's dname corresponds to the one we
2025 * sent. We can check only the first query of the list. We send
2026 * one query at a time so we get one query in the response */
2027 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
Olivier Houchardb17b8842020-04-01 18:30:27 +02002028 if (query && dns_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002029 dns_resp = DNS_RESP_WRONG_NAME;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002030 ns->counters->other++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002031 goto report_res_error;
2032 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002033
Christopher Faulet67957bd2017-09-27 11:00:59 +02002034 /* So the resolution succeeded */
2035 res->status = RSLV_STATUS_VALID;
2036 res->last_valid = now_ms;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002037 ns->counters->valid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002038 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002039
Christopher Faulet67957bd2017-09-27 11:00:59 +02002040 report_res_error:
2041 list_for_each_entry(req, &res->requesters, list)
2042 req->requester_error_cb(req, dns_resp);
2043 dns_reset_resolution(res);
2044 LIST_DEL(&res->list);
2045 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
2046 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002047
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048 report_res_success:
2049 /* Only the 1rst requester s managed by the server, others are
2050 * from the cache */
2051 tmpns = ns;
2052 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01002053 struct server *s = objt_server(req->owner);
2054
2055 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002056 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002057 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01002058 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002059 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002060 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002061 }
Baptiste Assmann42746372017-05-03 12:12:02 +02002062
Christopher Faulet67957bd2017-09-27 11:00:59 +02002063 dns_reset_resolution(res);
2064 LIST_DEL(&res->list);
2065 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
2066 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002067 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02002068 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002069 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002070}
William Lallemand69e96442016-11-19 00:58:54 +01002071
Christopher Faulet67957bd2017-09-27 11:00:59 +02002072/* Called when a resolvers network socket is ready to send data */
2073static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002074{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 struct dns_resolvers *resolvers;
2076 struct dns_nameserver *ns;
2077 struct dns_resolution *res;
2078 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002079
Christopher Faulet67957bd2017-09-27 11:00:59 +02002080 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002081
Christopher Faulet67957bd2017-09-27 11:00:59 +02002082 /* check if ready for sending */
2083 if (!fd_send_ready(fd))
2084 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002085
Christopher Faulet67957bd2017-09-27 11:00:59 +02002086 /* we don't want/need to be waked up any more for sending */
2087 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002088
Christopher Faulet67957bd2017-09-27 11:00:59 +02002089 /* no need to go further if we can't retrieve the nameserver */
2090 if ((ns = dgram->owner) == NULL)
2091 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002092
Christopher Faulet67957bd2017-09-27 11:00:59 +02002093 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002094 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002095
Christopher Faulet67957bd2017-09-27 11:00:59 +02002096 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002097 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002098
Christopher Faulet67957bd2017-09-27 11:00:59 +02002099 if (res->nb_queries == resolvers->nb_nameservers)
2100 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002101
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002102 len = dns_build_query(res->query_id, res->query_type,
2103 resolvers->accepted_payload_size,
2104 res->hostname_dn, res->hostname_dn_len,
2105 trash.area, trash.size);
2106 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002107 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002108
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002109 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01002110 if (ret != len) {
2111 if (ret == -1 && errno == EAGAIN) {
2112 /* retry once the socket is ready */
2113 fd_cant_send(fd);
2114 continue;
2115 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002116 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01002117 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002118
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002119 ns->counters->sent++;
2120
Christopher Faulet67957bd2017-09-27 11:00:59 +02002121 res->nb_queries++;
2122 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002123
Christopher Faulet67957bd2017-09-27 11:00:59 +02002124 snd_error:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002125 ns->counters->snd_error++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002126 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002127 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002128 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002129}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002130
Christopher Faulet67957bd2017-09-27 11:00:59 +02002131/* Processes DNS resolution. First, it checks the active list to detect expired
2132 * resolutions and retry them if possible. Else a timeout is reported. Then, it
2133 * checks the wait list to trigger new resolutions.
2134 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002135static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002136{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002137 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002138 struct dns_resolution *res, *resback;
2139 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002140
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002141 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02002142
Christopher Faulet67957bd2017-09-27 11:00:59 +02002143 /* Handle all expired resolutions from the active list */
2144 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2145 /* When we find the first resolution in the future, then we can
2146 * stop here */
2147 exp = tick_add(res->last_query, resolvers->timeout.retry);
2148 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002149 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002150
Christopher Faulet67957bd2017-09-27 11:00:59 +02002151 /* If current resolution has been tried too many times and
2152 * finishes in timeout we update its status and remove it from
2153 * the list */
2154 if (!res->try) {
2155 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002156
Christopher Faulet67957bd2017-09-27 11:00:59 +02002157 /* Notify the result to the requesters */
2158 if (!res->nb_responses)
2159 res->status = RSLV_STATUS_TIMEOUT;
2160 list_for_each_entry(req, &res->requesters, list)
2161 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002162
Christopher Faulet67957bd2017-09-27 11:00:59 +02002163 /* Clean up resolution info and remove it from the
2164 * current list */
2165 dns_reset_resolution(res);
2166 LIST_DEL(&res->list);
2167 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01002168 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002169 else {
2170 /* Otherwise resend the DNS query and requeue the resolution */
2171 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
2172 /* No response received (a real timeout) or fallback already done */
2173 res->query_type = res->prefered_query_type;
2174 res->try--;
2175 }
2176 else {
2177 /* Fallback from A to AAAA or the opposite and re-send
2178 * the resolution immediately. try counter is not
2179 * decremented. */
2180 if (res->prefered_query_type == DNS_RTYPE_A)
2181 res->query_type = DNS_RTYPE_AAAA;
2182 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
2183 res->query_type = DNS_RTYPE_A;
2184 else
2185 res->try--;
2186 }
2187 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01002188 }
2189 }
William Lallemand69e96442016-11-19 00:58:54 +01002190
Christopher Faulet67957bd2017-09-27 11:00:59 +02002191 /* Handle all resolutions in the wait list */
2192 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2193 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
2194 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2195 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002196
Christopher Faulet67957bd2017-09-27 11:00:59 +02002197 if (dns_run_resolution(res) != 1) {
2198 res->last_resolution = now_ms;
2199 LIST_DEL(&res->list);
2200 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002201 }
2202 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002203
Christopher Faulet67957bd2017-09-27 11:00:59 +02002204 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002205 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002206 return t;
2207}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002208
Christopher Faulet67957bd2017-09-27 11:00:59 +02002209/* proto_udp callback functions for a DNS resolution */
2210struct dgram_data_cb resolve_dgram_cb = {
2211 .recv = dns_resolve_recv,
2212 .send = dns_resolve_send,
2213};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002214
Christopher Faulet67957bd2017-09-27 11:00:59 +02002215/* Release memory allocated by DNS */
2216static void dns_deinit(void)
2217{
2218 struct dns_resolvers *resolvers, *resolversback;
2219 struct dns_nameserver *ns, *nsback;
2220 struct dns_resolution *res, *resback;
2221 struct dns_requester *req, *reqback;
2222 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002223
Christopher Faulet67957bd2017-09-27 11:00:59 +02002224 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
2225 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
2226 free(ns->id);
2227 free((char *)ns->conf.file);
2228 if (ns->dgram && ns->dgram->t.sock.fd != -1)
2229 fd_delete(ns->dgram->t.sock.fd);
2230 free(ns->dgram);
2231 LIST_DEL(&ns->list);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002232 EXTRA_COUNTERS_FREE(ns->extra_counters);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002233 free(ns);
2234 }
2235
2236 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
2237 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2238 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002239 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002240 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002241 dns_free_resolution(res);
2242 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002243
Christopher Faulet67957bd2017-09-27 11:00:59 +02002244 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
2245 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
2246 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01002247 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002248 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002249 dns_free_resolution(res);
2250 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02002251
Christopher Faulet67957bd2017-09-27 11:00:59 +02002252 free(resolvers->id);
2253 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002254 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002255 LIST_DEL(&resolvers->list);
2256 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002257 }
2258
Christopher Faulet67957bd2017-09-27 11:00:59 +02002259 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
2260 free(srvrq->name);
2261 free(srvrq->hostname_dn);
2262 LIST_DEL(&srvrq->list);
2263 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002264 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002265}
2266
Christopher Faulet67957bd2017-09-27 11:00:59 +02002267/* Finalizes the DNS configuration by allocating required resources and checking
2268 * live parameters.
2269 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002270 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002271static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002272{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002273 struct dns_resolvers *resolvers;
2274 struct proxy *px;
2275 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002276
Christopher Faulet67957bd2017-09-27 11:00:59 +02002277 /* allocate pool of resolution per resolvers */
2278 list_for_each_entry(resolvers, &dns_resolvers, list) {
2279 struct dns_nameserver *ns;
2280 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002281
Christopher Faulet67957bd2017-09-27 11:00:59 +02002282 /* Check if we can create the socket with nameservers info */
2283 list_for_each_entry(ns, &resolvers->nameservers, list) {
2284 struct dgram_conn *dgram = NULL;
2285 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002286
Christopher Faulet67957bd2017-09-27 11:00:59 +02002287 /* Check nameserver info */
2288 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002289 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
2290 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002291 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002292 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002293 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002294 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002295 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
2296 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002297 close(fd);
2298 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002299 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002300 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002301 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002302
Christopher Faulet67957bd2017-09-27 11:00:59 +02002303 /* Create dgram structure that will hold the UPD socket
2304 * and attach it on the current nameserver */
2305 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002306 ha_alert("config: resolvers '%s' : out of memory.\n",
2307 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002308 err_code |= (ERR_ALERT|ERR_ABORT);
2309 goto err;
2310 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002311
Christopher Faulet67957bd2017-09-27 11:00:59 +02002312 /* Leave dgram partially initialized, no FD attached for
2313 * now. */
2314 dgram->owner = ns;
2315 dgram->data = &resolve_dgram_cb;
2316 dgram->t.sock.fd = -1;
2317 ns->dgram = dgram;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002318
2319 /* Store the ns counters pointer */
2320 if (ns->extra_counters) {
2321 ns->counters = EXTRA_COUNTERS_GET(ns->extra_counters, &dns_stats_module);
2322 ns->counters->id = ns->id;
2323 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002324 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002325
Christopher Faulet67957bd2017-09-27 11:00:59 +02002326 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002327 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002328 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002329 err_code |= (ERR_ALERT|ERR_ABORT);
2330 goto err;
2331 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002332
Christopher Faulet67957bd2017-09-27 11:00:59 +02002333 /* Update task's parameters */
2334 t->process = dns_process_resolvers;
2335 t->context = resolvers;
2336 resolvers->t = t;
2337 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002338 }
2339
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002340 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002341 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002342
Christopher Faulet67957bd2017-09-27 11:00:59 +02002343 for (srv = px->srv; srv; srv = srv->next) {
2344 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002345
Christopher Faulet67957bd2017-09-27 11:00:59 +02002346 if (!srv->resolvers_id)
2347 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002348
Christopher Faulet67957bd2017-09-27 11:00:59 +02002349 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002350 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2351 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002352 err_code |= (ERR_ALERT|ERR_ABORT);
2353 continue;
2354 }
2355 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002356
Christopher Faulet67957bd2017-09-27 11:00:59 +02002357 if (srv->srvrq && !srv->srvrq->resolvers) {
2358 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002359 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002360 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2361 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002362 err_code |= (ERR_ALERT|ERR_ABORT);
2363 continue;
2364 }
2365 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002366 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002367 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2368 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002369 err_code |= (ERR_ALERT|ERR_ABORT);
2370 continue;
2371 }
2372 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002373 }
2374
Christopher Faulet67957bd2017-09-27 11:00:59 +02002375 if (err_code & (ERR_ALERT|ERR_ABORT))
2376 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002377
Christopher Faulet67957bd2017-09-27 11:00:59 +02002378 return err_code;
2379 err:
2380 dns_deinit();
2381 return err_code;
2382
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002383}
2384
2385static int stats_dump_dns_to_buffer(struct stream_interface *si,
2386 struct dns_nameserver *ns,
2387 struct field *stats, size_t stats_count,
2388 struct list *stat_modules)
2389{
2390 struct appctx *appctx = __objt_appctx(si->end);
2391 struct channel *rep = si_ic(si);
2392 struct stats_module *mod;
2393 size_t idx = 0;
2394
2395 memset(stats, 0, sizeof(struct field) * stats_count);
2396
2397 list_for_each_entry(mod, stat_modules, list) {
2398 struct counters_node *counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2399
2400 mod->fill_stats(counters, stats + idx);
2401 idx += mod->stats_count;
2402 }
2403
2404 if (!stats_dump_one_line(stats, idx, appctx))
2405 return 0;
2406
2407 if (!stats_putchk(rep, NULL, &trash))
2408 goto full;
2409
2410 return 1;
2411
2412 full:
2413 si_rx_room_rdy(si);
2414 return 0;
2415}
2416
2417/* Uses <appctx.ctx.stats.obj1> as a pointer to the current resolver and <obj2>
2418 * as a pointer to the current nameserver.
2419 */
2420int stats_dump_dns(struct stream_interface *si,
2421 struct field *stats, size_t stats_count,
2422 struct list *stat_modules)
2423{
2424 struct appctx *appctx = __objt_appctx(si->end);
2425 struct channel *rep = si_ic(si);
2426 struct dns_resolvers *resolver = appctx->ctx.stats.obj1;
2427 struct dns_nameserver *ns = appctx->ctx.stats.obj2;
2428
2429 if (!resolver)
2430 resolver = LIST_NEXT(&dns_resolvers, struct dns_resolvers *, list);
2431
2432 /* dump resolvers */
2433 list_for_each_entry_from(resolver, &dns_resolvers, list) {
2434 appctx->ctx.stats.obj1 = resolver;
2435
2436 ns = appctx->ctx.stats.obj2 ?
2437 appctx->ctx.stats.obj2 :
2438 LIST_NEXT(&resolver->nameservers, struct dns_nameserver *, list);
2439
2440 list_for_each_entry_from(ns, &resolver->nameservers, list) {
2441 appctx->ctx.stats.obj2 = ns;
2442
2443 if (buffer_almost_full(&rep->buf))
2444 goto full;
2445
2446 if (!stats_dump_dns_to_buffer(si, ns,
2447 stats, stats_count,
2448 stat_modules)) {
2449 return 0;
2450 }
2451 }
2452
2453 appctx->ctx.stats.obj2 = NULL;
2454 }
2455
2456 return 1;
2457
2458 full:
2459 si_rx_room_blk(si);
2460 return 0;
2461}
2462
2463void dns_stats_clear_counters(int clrall, struct list *stat_modules)
2464{
2465 struct dns_resolvers *resolvers;
2466 struct dns_nameserver *ns;
2467 struct stats_module *mod;
2468 void *counters;
2469
2470 list_for_each_entry(mod, stat_modules, list) {
2471 if (!mod->clearable && !clrall)
2472 continue;
2473
2474 list_for_each_entry(resolvers, &dns_resolvers, list) {
2475 list_for_each_entry(ns, &resolvers->nameservers, list) {
2476 counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2477 memcpy(counters, mod->counters, mod->counters_size);
2478 }
2479 }
2480 }
2481
2482}
2483
2484int dns_allocate_counters(struct list *stat_modules)
2485{
2486 struct stats_module *mod;
2487 struct dns_resolvers *resolvers;
2488 struct dns_nameserver *ns;
2489
2490 list_for_each_entry(resolvers, &dns_resolvers, list) {
2491 list_for_each_entry(ns, &resolvers->nameservers, list) {
2492 EXTRA_COUNTERS_REGISTER(&ns->extra_counters, COUNTERS_DNS,
2493 alloc_failed);
2494
2495 list_for_each_entry(mod, stat_modules, list) {
2496 EXTRA_COUNTERS_ADD(mod,
2497 ns->extra_counters,
2498 mod->counters,
2499 mod->counters_size);
2500 }
2501
2502 EXTRA_COUNTERS_ALLOC(ns->extra_counters, alloc_failed);
2503
2504 list_for_each_entry(mod, stat_modules, list) {
2505 memcpy(ns->extra_counters->data + mod->counters_off[ns->extra_counters->type],
2506 mod->counters, mod->counters_size);
2507
2508 /* Store the ns counters pointer */
2509 if (!strcmp(mod->name, "dns")) {
2510 ns->counters = (struct dns_counters *)ns->extra_counters->data + mod->counters_off[COUNTERS_DNS];
2511 ns->counters->id = ns->id;
2512 }
2513 }
2514 }
2515 }
2516
2517 return 1;
2518
2519alloc_failed:
2520 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002521}
2522
Christopher Faulet67957bd2017-09-27 11:00:59 +02002523/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002524static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002525{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002526 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002527
Christopher Faulet67957bd2017-09-27 11:00:59 +02002528 if (*args[2]) {
2529 list_for_each_entry(presolvers, &dns_resolvers, list) {
2530 if (strcmp(presolvers->id, args[2]) == 0) {
2531 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002532 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002533 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002534 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002535 if (appctx->ctx.cli.p0 == NULL)
2536 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002537 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002538 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002539}
2540
Christopher Faulet67957bd2017-09-27 11:00:59 +02002541/* Dumps counters from all resolvers section and associated name servers. It
2542 * returns 0 if the output buffer is full and it needs to be called again,
2543 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002544 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002545 */
2546static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2547{
2548 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002549 struct dns_resolvers *resolvers;
2550 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002551
2552 chunk_reset(&trash);
2553
2554 switch (appctx->st2) {
2555 case STAT_ST_INIT:
2556 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2557 /* fall through */
2558
2559 case STAT_ST_LIST:
2560 if (LIST_ISEMPTY(&dns_resolvers)) {
2561 chunk_appendf(&trash, "No resolvers found\n");
2562 }
2563 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002564 list_for_each_entry(resolvers, &dns_resolvers, list) {
2565 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002566 continue;
2567
Christopher Faulet67957bd2017-09-27 11:00:59 +02002568 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2569 list_for_each_entry(ns, &resolvers->nameservers, list) {
2570 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002571 chunk_appendf(&trash, " sent: %lld\n", ns->counters->sent);
2572 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters->snd_error);
2573 chunk_appendf(&trash, " valid: %lld\n", ns->counters->valid);
2574 chunk_appendf(&trash, " update: %lld\n", ns->counters->update);
2575 chunk_appendf(&trash, " cname: %lld\n", ns->counters->cname);
2576 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters->cname_error);
2577 chunk_appendf(&trash, " any_err: %lld\n", ns->counters->any_err);
2578 chunk_appendf(&trash, " nx: %lld\n", ns->counters->nx);
2579 chunk_appendf(&trash, " timeout: %lld\n", ns->counters->timeout);
2580 chunk_appendf(&trash, " refused: %lld\n", ns->counters->refused);
2581 chunk_appendf(&trash, " other: %lld\n", ns->counters->other);
2582 chunk_appendf(&trash, " invalid: %lld\n", ns->counters->invalid);
2583 chunk_appendf(&trash, " too_big: %lld\n", ns->counters->too_big);
2584 chunk_appendf(&trash, " truncated: %lld\n", ns->counters->truncated);
2585 chunk_appendf(&trash, " outdated: %lld\n", ns->counters->outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002586 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002587 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002588 }
2589 }
2590
2591 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002592 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002593 /* let's try again later from this session. We add ourselves into
2594 * this session's users so that it can remove us upon termination.
2595 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002596 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002597 return 0;
2598 }
William Lallemand69e96442016-11-19 00:58:54 +01002599 /* fall through */
2600
2601 default:
2602 appctx->st2 = STAT_ST_FIN;
2603 return 1;
2604 }
2605}
2606
2607/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002608static struct cli_kw_list cli_kws = {{ }, {
2609 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2610 " associated name servers",
2611 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2612 {{},}
2613 }
2614};
William Lallemand69e96442016-11-19 00:58:54 +01002615
Willy Tarreau0108d902018-11-25 19:14:37 +01002616INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002617
Baptiste Assmann333939c2019-01-21 08:34:50 +01002618/*
2619 * Prepare <rule> for hostname resolution.
2620 * Returns -1 in case of any allocation failure, 0 if not.
2621 * On error, a global failure counter is also incremented.
2622 */
2623static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2624{
2625 char *hostname_dn;
2626 int hostname_len, hostname_dn_len;
2627 struct buffer *tmp = get_trash_chunk();
2628
2629 if (!hostname)
2630 return 0;
2631
2632 hostname_len = strlen(hostname);
2633 hostname_dn = tmp->area;
2634 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2635 hostname_dn, tmp->size);
2636 if (hostname_dn_len == -1)
2637 goto err;
2638
2639
2640 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2641 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2642 if (!stream->dns_ctx.hostname_dn)
2643 goto err;
2644
2645 return 0;
2646
2647 err:
2648 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2649 dns_failed_resolutions += 1;
2650 return -1;
2651}
2652
2653
2654/*
2655 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2656 */
2657enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2658 struct session *sess, struct stream *s, int flags)
2659{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002660 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002661 struct sample *smp;
2662 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002663 struct dns_requester *req;
2664 struct dns_resolvers *resolvers;
2665 struct dns_resolution *res;
Christopher Faulet5098a082020-07-22 11:46:32 +02002666 int exp, locked = 0;
2667 enum act_return ret = ACT_RET_CONT;
2668
2669 resolvers = rule->arg.dns.resolvers;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002670
2671 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002672 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002673 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2674 resolution = s->dns_ctx.dns_requester->resolution;
Christopher Faulet5098a082020-07-22 11:46:32 +02002675 if (!locked) {
2676 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2677 locked = 1;
2678 }
2679
Christopher Faulet385101e2020-07-28 10:21:54 +02002680 if (resolution->step == RSLV_STEP_RUNNING)
2681 goto yield;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002682 if (resolution->step == RSLV_STEP_NONE) {
2683 /* We update the variable only if we have a valid response. */
2684 if (resolution->status == RSLV_STATUS_VALID) {
2685 struct sample smp;
2686 short ip_sin_family = 0;
2687 void *ip = NULL;
2688
Christopher Fauleta4168432020-01-24 18:08:42 +01002689 dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002690 0, &ip, &ip_sin_family, NULL);
2691
2692 switch (ip_sin_family) {
2693 case AF_INET:
2694 smp.data.type = SMP_T_IPV4;
2695 memcpy(&smp.data.u.ipv4, ip, 4);
2696 break;
2697 case AF_INET6:
2698 smp.data.type = SMP_T_IPV6;
2699 memcpy(&smp.data.u.ipv6, ip, 16);
2700 break;
2701 default:
2702 ip = NULL;
2703 }
2704
2705 if (ip) {
2706 smp.px = px;
2707 smp.sess = sess;
2708 smp.strm = s;
2709
2710 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2711 }
2712 }
2713 }
2714
Christopher Faulet385101e2020-07-28 10:21:54 +02002715 goto release_requester;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002716 }
2717
2718 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002719 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2720 if (smp == NULL)
Christopher Faulet5098a082020-07-22 11:46:32 +02002721 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002722
Willy Tarreau45726fd2019-07-17 10:38:45 +02002723 fqdn = smp->data.u.str.area;
2724 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet5098a082020-07-22 11:46:32 +02002725 goto end; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002726
Willy Tarreau45726fd2019-07-17 10:38:45 +02002727 s->dns_ctx.parent = rule;
Christopher Faulet5098a082020-07-22 11:46:32 +02002728
2729 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2730 locked = 1;
2731
Willy Tarreau45726fd2019-07-17 10:38:45 +02002732 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002733
2734 /* Check if there is a fresh enough response in the cache of our associated resolution */
2735 req = s->dns_ctx.dns_requester;
Christopher Faulet385101e2020-07-28 10:21:54 +02002736 if (!req || !req->resolution)
2737 goto release_requester; /* on error, ignore the action */
Christopher Faulet5098a082020-07-22 11:46:32 +02002738 res = req->resolution;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002739
2740 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2741 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
Christopher Faulet385101e2020-07-28 10:21:54 +02002742 && !tick_is_expired(exp, now_ms)) {
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002743 goto use_cache;
2744 }
2745
Willy Tarreau45726fd2019-07-17 10:38:45 +02002746 dns_trigger_resolution(s->dns_ctx.dns_requester);
Christopher Faulet385101e2020-07-28 10:21:54 +02002747
2748 yield:
2749 if (flags & ACT_OPT_FINAL)
2750 goto release_requester;
Christopher Faulet5098a082020-07-22 11:46:32 +02002751 ret = ACT_RET_YIELD;
2752
2753 end:
2754 if (locked)
2755 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2756 return ret;
Christopher Faulet385101e2020-07-28 10:21:54 +02002757
2758 release_requester:
2759 free(s->dns_ctx.hostname_dn);
2760 s->dns_ctx.hostname_dn = NULL;
2761 s->dns_ctx.hostname_dn_len = 0;
2762 if (s->dns_ctx.dns_requester) {
2763 dns_unlink_resolution(s->dns_ctx.dns_requester);
2764 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2765 s->dns_ctx.dns_requester = NULL;
2766 }
2767 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002768}
2769
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002770static void release_dns_action(struct act_rule *rule)
2771{
2772 release_sample_expr(rule->arg.dns.expr);
2773 free(rule->arg.dns.varname);
2774 free(rule->arg.dns.resolvers_id);
2775 free(rule->arg.dns.dns_opts);
2776}
2777
Baptiste Assmann333939c2019-01-21 08:34:50 +01002778
2779/* parse "do-resolve" action
2780 * This action takes the following arguments:
2781 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2782 *
2783 * - <varName> is the variable name where the result of the DNS resolution will be stored
2784 * (mandatory)
2785 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2786 * (mandatory)
2787 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2788 * (optional), defaults to ipv6
2789 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2790 */
2791enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2792{
2793 int cur_arg;
2794 struct sample_expr *expr;
2795 unsigned int where;
2796 const char *beg, *end;
2797
2798 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2799 cur_arg = *orig_arg - 1;
2800
2801 /* locate varName, which is mandatory */
2802 beg = strchr(args[cur_arg], '(');
2803 if (beg == NULL)
2804 goto do_resolve_parse_error;
2805 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2806 end = strchr(beg, ',');
2807 if (end == NULL)
2808 goto do_resolve_parse_error;
2809 rule->arg.dns.varname = my_strndup(beg, end - beg);
2810 if (rule->arg.dns.varname == NULL)
2811 goto do_resolve_parse_error;
2812
2813
2814 /* locate resolversSectionName, which is mandatory.
2815 * Since next parameters are optional, the delimiter may be comma ','
2816 * or closing parenthesis ')'
2817 */
2818 beg = end + 1;
2819 end = strchr(beg, ',');
2820 if (end == NULL)
2821 end = strchr(beg, ')');
2822 if (end == NULL)
2823 goto do_resolve_parse_error;
2824 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2825 if (rule->arg.dns.resolvers_id == NULL)
2826 goto do_resolve_parse_error;
2827
2828
Christopher Fauleta4168432020-01-24 18:08:42 +01002829 rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts));
2830 if (rule->arg.dns.dns_opts == NULL)
2831 goto do_resolve_parse_error;
2832
Baptiste Assmann333939c2019-01-21 08:34:50 +01002833 /* Default priority is ipv6 */
Christopher Fauleta4168432020-01-24 18:08:42 +01002834 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002835
2836 /* optional arguments accepted for now:
2837 * ipv4 or ipv6
2838 */
2839 while (*end != ')') {
2840 beg = end + 1;
2841 end = strchr(beg, ',');
2842 if (end == NULL)
2843 end = strchr(beg, ')');
2844 if (end == NULL)
2845 goto do_resolve_parse_error;
2846
2847 if (strncmp(beg, "ipv4", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002848 rule->arg.dns.dns_opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002849 }
2850 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Christopher Fauleta4168432020-01-24 18:08:42 +01002851 rule->arg.dns.dns_opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002852 }
2853 else {
2854 goto do_resolve_parse_error;
2855 }
2856 }
2857
2858 cur_arg = cur_arg + 1;
2859
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002860 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 +01002861 if (!expr)
2862 goto do_resolve_parse_error;
2863
2864
2865 where = 0;
2866 if (px->cap & PR_CAP_FE)
2867 where |= SMP_VAL_FE_HRQ_HDR;
2868 if (px->cap & PR_CAP_BE)
2869 where |= SMP_VAL_BE_HRQ_HDR;
2870
2871 if (!(expr->fetch->val & where)) {
2872 memprintf(err,
2873 "fetch method '%s' extracts information from '%s', none of which is available here",
2874 args[cur_arg-1], sample_src_names(expr->fetch->use));
2875 free(expr);
2876 return ACT_RET_PRS_ERR;
2877 }
2878 rule->arg.dns.expr = expr;
2879 rule->action = ACT_CUSTOM;
2880 rule->action_ptr = dns_action_do_resolve;
2881 *orig_arg = cur_arg;
2882
2883 rule->check_ptr = check_action_do_resolve;
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002884 rule->release_ptr = release_dns_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002885
2886 return ACT_RET_PRS_OK;
2887
2888 do_resolve_parse_error:
2889 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2890 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2891 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2892 args[cur_arg]);
2893 return ACT_RET_PRS_ERR;
2894}
2895
2896static struct action_kw_list http_req_kws = { { }, {
2897 { "do-resolve", dns_parse_do_resolve, 1 },
2898 { /* END */ }
2899}};
2900
2901INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2902
2903static struct action_kw_list tcp_req_cont_actions = {ILH, {
2904 { "do-resolve", dns_parse_do_resolve, 1 },
2905 { /* END */ }
2906}};
2907
2908INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2909
2910/* Check an "http-request do-resolve" action.
2911 *
2912 * The function returns 1 in success case, otherwise, it returns 0 and err is
2913 * filled.
2914 */
2915int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2916{
2917 struct dns_resolvers *resolvers = NULL;
2918
2919 if (rule->arg.dns.resolvers_id == NULL) {
2920 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2921 return 0;
2922 }
2923
2924 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2925 if (resolvers == NULL) {
2926 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2927 return 0;
2928 }
2929 rule->arg.dns.resolvers = resolvers;
2930
2931 return 1;
2932}
2933
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002934REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002935REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);