blob: 9e128ca97a98c4b6e38c22dd190f86c10298877b [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
Emeric Brun750fe792020-12-23 16:51:12 +010048struct list sec_resolvers = LIST_HEAD_INIT(sec_resolvers);
Emeric Brun08622d32020-12-23 17:41:43 +010049struct list resolv_srvrq_list = LIST_HEAD_INIT(resolv_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020050
Emeric Brund30e9a12020-12-23 18:49:16 +010051static THREAD_LOCAL uint64_t resolv_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010052
Emeric Brun85914e92020-12-23 16:38:06 +010053DECLARE_STATIC_POOL(resolv_answer_item_pool, "resolv_answer_item", sizeof(struct resolv_answer_item));
Emeric Brun08622d32020-12-23 17:41:43 +010054DECLARE_STATIC_POOL(resolv_resolution_pool, "resolv_resolution", sizeof(struct resolv_resolution));
55DECLARE_POOL(resolv_requester_pool, "resolv_requester", sizeof(struct resolv_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010056
Christopher Faulet67957bd2017-09-27 11:00:59 +020057static unsigned int resolution_uuid = 1;
Emeric Brund30e9a12020-12-23 18:49:16 +010058unsigned int resolv_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +020060enum {
61 DNS_STAT_ID,
Emeric Brun50c870e2021-01-04 10:40:46 +010062 DNS_STAT_PID,
63 DNS_STAT_SENT,
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +020064 DNS_STAT_SND_ERROR,
65 DNS_STAT_VALID,
66 DNS_STAT_UPDATE,
67 DNS_STAT_CNAME,
68 DNS_STAT_CNAME_ERROR,
69 DNS_STAT_ANY_ERR,
70 DNS_STAT_NX,
71 DNS_STAT_TIMEOUT,
72 DNS_STAT_REFUSED,
73 DNS_STAT_OTHER,
74 DNS_STAT_INVALID,
75 DNS_STAT_TOO_BIG,
76 DNS_STAT_TRUNCATED,
77 DNS_STAT_OUTDATED,
78 DNS_STAT_END,
79};
80
81static struct name_desc dns_stats[] = {
82 [DNS_STAT_ID] = { .name = "id", .desc = "ID" },
Emeric Brun50c870e2021-01-04 10:40:46 +010083 [DNS_STAT_PID] = { .name = "pid", .desc = "Parent ID" },
84 [DNS_STAT_SENT] = { .name = "sent", .desc = "Sent" },
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +020085 [DNS_STAT_SND_ERROR] = { .name = "send_error", .desc = "Send error" },
86 [DNS_STAT_VALID] = { .name = "valid", .desc = "Valid" },
87 [DNS_STAT_UPDATE] = { .name = "update", .desc = "Update" },
88 [DNS_STAT_CNAME] = { .name = "cname", .desc = "CNAME" },
89 [DNS_STAT_CNAME_ERROR] = { .name = "cname_error", .desc = "CNAME error" },
90 [DNS_STAT_ANY_ERR] = { .name = "any_err", .desc = "Any errors" },
91 [DNS_STAT_NX] = { .name = "nx", .desc = "NX" },
92 [DNS_STAT_TIMEOUT] = { .name = "timeout", .desc = "Timeout" },
93 [DNS_STAT_REFUSED] = { .name = "refused", .desc = "Refused" },
94 [DNS_STAT_OTHER] = { .name = "other", .desc = "Other" },
95 [DNS_STAT_INVALID] = { .name = "invalid", .desc = "Invalid" },
96 [DNS_STAT_TOO_BIG] = { .name = "too_big", .desc = "Too big" },
97 [DNS_STAT_TRUNCATED] = { .name = "truncated", .desc = "Truncated" },
98 [DNS_STAT_OUTDATED] = { .name = "outdated", .desc = "Outdated" },
99};
100
101static struct dns_counters dns_counters;
102
103static void dns_fill_stats(void *d, struct field *stats)
104{
105 struct dns_counters *counters = d;
106 stats[DNS_STAT_ID] = mkf_str(FO_CONFIG, counters->id);
Emeric Brun50c870e2021-01-04 10:40:46 +0100107 stats[DNS_STAT_PID] = mkf_str(FO_CONFIG, counters->pid);
108 stats[DNS_STAT_SENT] = mkf_u64(FN_GAUGE, counters->sent);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200109 stats[DNS_STAT_SND_ERROR] = mkf_u64(FN_GAUGE, counters->snd_error);
110 stats[DNS_STAT_VALID] = mkf_u64(FN_GAUGE, counters->valid);
111 stats[DNS_STAT_UPDATE] = mkf_u64(FN_GAUGE, counters->update);
112 stats[DNS_STAT_CNAME] = mkf_u64(FN_GAUGE, counters->cname);
113 stats[DNS_STAT_CNAME_ERROR] = mkf_u64(FN_GAUGE, counters->cname_error);
114 stats[DNS_STAT_ANY_ERR] = mkf_u64(FN_GAUGE, counters->any_err);
115 stats[DNS_STAT_NX] = mkf_u64(FN_GAUGE, counters->nx);
116 stats[DNS_STAT_TIMEOUT] = mkf_u64(FN_GAUGE, counters->timeout);
117 stats[DNS_STAT_REFUSED] = mkf_u64(FN_GAUGE, counters->refused);
118 stats[DNS_STAT_OTHER] = mkf_u64(FN_GAUGE, counters->other);
119 stats[DNS_STAT_INVALID] = mkf_u64(FN_GAUGE, counters->invalid);
120 stats[DNS_STAT_TOO_BIG] = mkf_u64(FN_GAUGE, counters->too_big);
121 stats[DNS_STAT_TRUNCATED] = mkf_u64(FN_GAUGE, counters->truncated);
122 stats[DNS_STAT_OUTDATED] = mkf_u64(FN_GAUGE, counters->outdated);
123}
124
125static struct stats_module dns_stats_module = {
126 .name = "dns",
127 .domain_flags = STATS_DOMAIN_DNS << STATS_DOMAIN,
128 .fill_stats = dns_fill_stats,
129 .stats = dns_stats,
130 .stats_count = DNS_STAT_END,
131 .counters = &dns_counters,
132 .counters_size = sizeof(dns_counters),
133 .clearable = 0,
134};
135
136INITCALL1(STG_REGISTER, stats_register_module, &dns_stats_module);
137
Christopher Faulet67957bd2017-09-27 11:00:59 +0200138/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
139 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200140 */
Emeric Brun750fe792020-12-23 16:51:12 +0100141struct resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200142{
Emeric Brun750fe792020-12-23 16:51:12 +0100143 struct resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200144
Emeric Brun750fe792020-12-23 16:51:12 +0100145 list_for_each_entry(res, &sec_resolvers, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100146 if (strcmp(res->id, id) == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200147 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200148 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200149 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200150}
151
Olivier Houchardb17b8842020-04-01 18:30:27 +0200152/* Compare hostnames in a case-insensitive way .
153 * Returns 0 if they are the same, non-zero otherwise
154 */
Emeric Brund30e9a12020-12-23 18:49:16 +0100155static __inline int resolv_hostname_cmp(const char *name1, const char *name2, int len)
Olivier Houchardb17b8842020-04-01 18:30:27 +0200156{
157 int i;
158
159 for (i = 0; i < len; i++)
Willy Tarreauf278eec2020-07-05 21:46:32 +0200160 if (tolower((unsigned char)name1[i]) != tolower((unsigned char)name2[i]))
Olivier Houchardb17b8842020-04-01 18:30:27 +0200161 return -1;
162 return 0;
163}
164
Christopher Faulet67957bd2017-09-27 11:00:59 +0200165/* Returns a pointer on the SRV request matching the name <name> for the proxy
166 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200167 */
Emeric Brun08622d32020-12-23 17:41:43 +0100168struct resolv_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200169{
Emeric Brun08622d32020-12-23 17:41:43 +0100170 struct resolv_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200171
Emeric Brun08622d32020-12-23 17:41:43 +0100172 list_for_each_entry(srvrq, &resolv_srvrq_list, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100173 if (srvrq->proxy == px && strcmp(srvrq->name, name) == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200174 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200175 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200176 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200177}
178
Christopher Faulet67957bd2017-09-27 11:00:59 +0200179/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
180 * NULL if an error occurred. */
Emeric Brun08622d32020-12-23 17:41:43 +0100181struct resolv_srvrq *new_resolv_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200182{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200183 struct proxy *px = srv->proxy;
Emeric Brun08622d32020-12-23 17:41:43 +0100184 struct resolv_srvrq *srvrq = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200185 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200186
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187 fqdn_len = strlen(fqdn);
Emeric Brund30e9a12020-12-23 18:49:16 +0100188 hostname_dn_len = resolv_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200189 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100191 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
192 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200193 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200194 }
195
Christopher Faulet67957bd2017-09-27 11:00:59 +0200196 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100197 ha_alert("config : %s '%s', server '%s': out of memory\n",
198 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 goto err;
200 }
201 srvrq->obj_type = OBJ_TYPE_SRVRQ;
202 srvrq->proxy = px;
203 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200204 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200205 srvrq->hostname_dn_len = hostname_dn_len;
206 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100207 ha_alert("config : %s '%s', server '%s': out of memory\n",
208 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200209 goto err;
210 }
Emeric Brun08622d32020-12-23 17:41:43 +0100211 LIST_ADDQ(&resolv_srvrq_list, &srvrq->list);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200212 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200213
Christopher Faulet67957bd2017-09-27 11:00:59 +0200214 err:
215 if (srvrq) {
216 free(srvrq->name);
217 free(srvrq->hostname_dn);
218 free(srvrq);
219 }
220 return NULL;
221}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200222
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224/* 2 bytes random generator to generate DNS query ID */
Emeric Brund30e9a12020-12-23 18:49:16 +0100225static inline uint16_t resolv_rnd16(void)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200226{
Emeric Brund30e9a12020-12-23 18:49:16 +0100227 if (!resolv_query_id_seed)
228 resolv_query_id_seed = now_ms;
229 resolv_query_id_seed ^= resolv_query_id_seed << 13;
230 resolv_query_id_seed ^= resolv_query_id_seed >> 7;
231 resolv_query_id_seed ^= resolv_query_id_seed << 17;
232 return resolv_query_id_seed;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200233}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200234
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200235
Emeric Brund30e9a12020-12-23 18:49:16 +0100236static inline int resolv_resolution_timeout(struct resolv_resolution *res)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200237{
Baptiste Assmannf50e1ac2019-11-07 11:02:18 +0100238 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200239}
240
Christopher Faulet67957bd2017-09-27 11:00:59 +0200241/* Updates a resolvers' task timeout for next wake up and queue it */
Emeric Brund30e9a12020-12-23 18:49:16 +0100242static void resolv_update_resolvers_timeout(struct resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200243{
Emeric Brun08622d32020-12-23 17:41:43 +0100244 struct resolv_resolution *res;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200246
Christopher Faulet67957bd2017-09-27 11:00:59 +0200247 next = tick_add(now_ms, resolvers->timeout.resolve);
248 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
Emeric Brun08622d32020-12-23 17:41:43 +0100249 res = LIST_NEXT(&resolvers->resolutions.curr, struct resolv_resolution *, list);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200250 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
251 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200252
Christopher Faulet67957bd2017-09-27 11:00:59 +0200253 list_for_each_entry(res, &resolvers->resolutions.wait, list)
Emeric Brund30e9a12020-12-23 18:49:16 +0100254 next = MIN(next, tick_add(res->last_resolution, resolv_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200255
Christopher Faulet67957bd2017-09-27 11:00:59 +0200256 resolvers->t->expire = next;
257 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200258}
259
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
261 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200262 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200263static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200264{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265 struct dgram_conn *dgram = ns->dgram;
266 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200267
Christopher Faulet67957bd2017-09-27 11:00:59 +0200268 /* Already connected */
269 if (dgram->t.sock.fd != -1)
270 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200271
Christopher Faulet67957bd2017-09-27 11:00:59 +0200272 /* Create an UDP socket and connect it on the nameserver's IP/Port */
273 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
274 send_log(NULL, LOG_WARNING,
275 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
276 ns->resolvers->id, ns->id);
277 return -1;
278 }
279 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
280 send_log(NULL, LOG_WARNING,
281 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
282 ns->resolvers->id, ns->id);
283 close(fd);
284 return -1;
285 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200286
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287 /* Make the socket non blocking */
288 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200289
Christopher Faulet67957bd2017-09-27 11:00:59 +0200290 /* Add the fd in the fd list and update its parameters */
291 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100292 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200293 fd_want_recv(fd);
294 return 0;
295}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200296
Christopher Faulet67957bd2017-09-27 11:00:59 +0200297/* Forges a DNS query. It needs the following information from the caller:
298 * - <query_id> : the DNS query id corresponding to this query
299 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
300 * - <hostname_dn> : hostname in domain name format
301 * - <hostname_dn_len> : length of <hostname_dn>
302 *
303 * To store the query, the caller must pass a buffer <buf> and its size
304 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
305 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200306 */
Emeric Brund30e9a12020-12-23 18:49:16 +0100307static int resolv_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
308 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200309{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200310 struct dns_header dns_hdr;
311 struct dns_question qinfo;
312 struct dns_additional_record edns;
313 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200314
Christopher Faulet67957bd2017-09-27 11:00:59 +0200315 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
316 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200317
Christopher Faulet67957bd2017-09-27 11:00:59 +0200318 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200319
Christopher Faulet67957bd2017-09-27 11:00:59 +0200320 /* Set dns query headers */
321 dns_hdr.id = (unsigned short) htons(query_id);
322 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
323 dns_hdr.qdcount = htons(1); /* 1 question */
324 dns_hdr.ancount = 0;
325 dns_hdr.nscount = 0;
326 dns_hdr.arcount = htons(1);
327 memcpy(p, &dns_hdr, sizeof(dns_hdr));
328 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200329
Christopher Faulet67957bd2017-09-27 11:00:59 +0200330 /* Set up query hostname */
331 memcpy(p, hostname_dn, hostname_dn_len);
332 p += hostname_dn_len;
333 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200334
Christopher Faulet67957bd2017-09-27 11:00:59 +0200335 /* Set up query info (type and class) */
336 qinfo.qtype = htons(query_type);
337 qinfo.qclass = htons(DNS_RCLASS_IN);
338 memcpy(p, &qinfo, sizeof(qinfo));
339 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200340
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 /* Set the DNS extension */
342 edns.name = 0;
343 edns.type = htons(DNS_RTYPE_OPT);
344 edns.udp_payload_size = htons(accepted_payload_size);
345 edns.extension = 0;
346 edns.data_length = 0;
347 memcpy(p, &edns, sizeof(edns));
348 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200349
Christopher Faulet67957bd2017-09-27 11:00:59 +0200350 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200351}
352
Christopher Faulet67957bd2017-09-27 11:00:59 +0200353/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
354 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200355 */
Emeric Brund30e9a12020-12-23 18:49:16 +0100356static int resolv_send_query(struct resolv_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200357{
Emeric Brun750fe792020-12-23 16:51:12 +0100358 struct resolvers *resolvers = resolution->resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359 struct dns_nameserver *ns;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100360 int len;
361
362 /* Update resolution */
363 resolution->nb_queries = 0;
364 resolution->nb_responses = 0;
365 resolution->last_query = now_ms;
366
Emeric Brund30e9a12020-12-23 18:49:16 +0100367 len = resolv_build_query(resolution->query_id, resolution->query_type,
Willy Tarreau0eae6322019-12-20 11:18:54 +0100368 resolvers->accepted_payload_size,
369 resolution->hostname_dn, resolution->hostname_dn_len,
370 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200371
Christopher Faulet67957bd2017-09-27 11:00:59 +0200372 list_for_each_entry(ns, &resolvers->nameservers, list) {
373 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100374 int ret;
375
Christopher Faulet67957bd2017-09-27 11:00:59 +0200376 if (fd == -1) {
377 if (dns_connect_namesaver(ns) == -1)
378 continue;
379 fd = ns->dgram->t.sock.fd;
380 resolvers->nb_nameservers++;
381 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200382
Willy Tarreau0eae6322019-12-20 11:18:54 +0100383 if (len < 0)
384 goto snd_error;
385
386 ret = send(fd, trash.area, len, 0);
387 if (ret == len) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200388 ns->counters->sent++;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100389 resolution->nb_queries++;
390 continue;
391 }
392
393 if (ret == -1 && errno == EAGAIN) {
394 /* retry once the socket is ready */
395 fd_cant_send(fd);
396 continue;
397 }
398
399 snd_error:
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +0200400 ns->counters->snd_error++;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100401 resolution->nb_queries++;
402 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200403
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404 /* Push the resolution at the end of the active list */
405 LIST_DEL(&resolution->list);
406 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
407 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200408}
409
Christopher Faulet67957bd2017-09-27 11:00:59 +0200410/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
411 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200412 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413static int
Emeric Brund30e9a12020-12-23 18:49:16 +0100414resolv_run_resolution(struct resolv_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200415{
Emeric Brun750fe792020-12-23 16:51:12 +0100416 struct resolvers *resolvers = resolution->resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200417 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200418
Christopher Faulet67957bd2017-09-27 11:00:59 +0200419 /* Avoid sending requests for resolutions that don't yet have an
420 * hostname, ie resolutions linked to servers that do not yet have an
421 * fqdn */
422 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200423 return 0;
424
Christopher Faulet67957bd2017-09-27 11:00:59 +0200425 /* Check if a resolution has already been started for this server return
426 * directly to avoid resolution pill up. */
427 if (resolution->step != RSLV_STEP_NONE)
428 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200429
Christopher Faulet67957bd2017-09-27 11:00:59 +0200430 /* Generates a new query id. We try at most 100 times to find a free
431 * query id */
432 for (i = 0; i < 100; ++i) {
Emeric Brund30e9a12020-12-23 18:49:16 +0100433 query_id = resolv_rnd16();
Christopher Faulet67957bd2017-09-27 11:00:59 +0200434 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200435 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200436 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200437 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200438 if (query_id == -1) {
439 send_log(NULL, LOG_NOTICE,
440 "could not generate a query id for %s, in resolvers %s.\n",
441 resolution->hostname_dn, resolvers->id);
442 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200443 }
444
Christopher Faulet67957bd2017-09-27 11:00:59 +0200445 /* Update resolution parameters */
446 resolution->query_id = query_id;
447 resolution->qid.key = query_id;
448 resolution->step = RSLV_STEP_RUNNING;
449 resolution->query_type = resolution->prefered_query_type;
450 resolution->try = resolvers->resolve_retries;
451 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200452
Christopher Faulet67957bd2017-09-27 11:00:59 +0200453 /* Send the DNS query */
454 resolution->try -= 1;
Emeric Brund30e9a12020-12-23 18:49:16 +0100455 resolv_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200456 return 1;
457}
458
Christopher Faulet67957bd2017-09-27 11:00:59 +0200459/* Performs a name resolution for the requester <req> */
Emeric Brund30e9a12020-12-23 18:49:16 +0100460void resolv_trigger_resolution(struct resolv_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200461{
Emeric Brun750fe792020-12-23 16:51:12 +0100462 struct resolvers *resolvers;
Emeric Brun08622d32020-12-23 17:41:43 +0100463 struct resolv_resolution *res;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200464 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200465
Christopher Faulet67957bd2017-09-27 11:00:59 +0200466 if (!req || !req->resolution)
467 return;
468 res = req->resolution;
469 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200470
Christopher Faulet67957bd2017-09-27 11:00:59 +0200471 /* The resolution must not be triggered yet. Use the cached response, if
472 * valid */
473 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200474 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
475 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
476 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200477}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200478
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200479
Christopher Faulet67957bd2017-09-27 11:00:59 +0200480/* Resets some resolution parameters to initial values and also delete the query
481 * ID from the resolver's tree.
482 */
Emeric Brund30e9a12020-12-23 18:49:16 +0100483static void resolv_reset_resolution(struct resolv_resolution *resolution)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200484{
485 /* update resolution status */
486 resolution->step = RSLV_STEP_NONE;
487 resolution->try = 0;
488 resolution->last_resolution = now_ms;
489 resolution->nb_queries = 0;
490 resolution->nb_responses = 0;
491 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200492
Christopher Faulet67957bd2017-09-27 11:00:59 +0200493 /* clean up query id */
494 eb32_delete(&resolution->qid);
495 resolution->query_id = 0;
496 resolution->qid.key = 0;
497}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200498
Christopher Faulet67957bd2017-09-27 11:00:59 +0200499/* Returns the query id contained in a DNS response */
Emeric Brund30e9a12020-12-23 18:49:16 +0100500static inline unsigned short resolv_response_get_query_id(unsigned char *resp)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200501{
502 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200503}
504
Christopher Faulet67957bd2017-09-27 11:00:59 +0200505
506/* Analyses, re-builds and copies the name <name> from the DNS response packet
507 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
508 * for compressed data. The result is copied into <dest>, ensuring we don't
509 * overflow using <dest_len> Returns the number of bytes the caller can move
510 * forward. If 0 it means an error occurred while parsing the name. <offset> is
511 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200512 */
Emeric Brund30e9a12020-12-23 18:49:16 +0100513int resolv_read_name(unsigned char *buffer, unsigned char *bufend,
514 unsigned char *name, char *destination, int dest_len,
515 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200516{
517 int nb_bytes = 0, n = 0;
518 int label_len;
519 unsigned char *reader = name;
520 char *dest = destination;
521
522 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100523 if (reader >= bufend)
524 goto err;
525
Christopher Faulet67957bd2017-09-27 11:00:59 +0200526 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200527 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100528 if (reader + 1 >= bufend)
529 goto err;
530
Christopher Faulet67957bd2017-09-27 11:00:59 +0200531 /* Must point BEFORE current position */
532 if ((buffer + reader[1]) > reader)
533 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200534
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100535 if (depth++ > 100)
536 goto err;
537
Emeric Brund30e9a12020-12-23 18:49:16 +0100538 n = resolv_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
539 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200540 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200541 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200542
Christopher Faulet67957bd2017-09-27 11:00:59 +0200543 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200544 nb_bytes += n;
545 goto out;
546 }
547
548 label_len = *reader;
549 if (label_len == 0)
550 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200552 /* Check if:
553 * - we won't read outside the buffer
554 * - there is enough place in the destination
555 */
556 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200557 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200558
559 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200560 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200561
562 memcpy(dest, reader, label_len);
563
Christopher Faulet67957bd2017-09-27 11:00:59 +0200564 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200565 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200566 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200567 }
568
Christopher Faulet67957bd2017-09-27 11:00:59 +0200569 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200570 /* offset computation:
571 * parse from <name> until finding either NULL or a pointer "c0xx"
572 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200573 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200574 *offset = 0;
575 while (reader < bufend) {
576 if ((reader[0] & 0xc0) == 0xc0) {
577 *offset += 2;
578 break;
579 }
580 else if (*reader == 0) {
581 *offset += 1;
582 break;
583 }
584 *offset += 1;
585 ++reader;
586 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200587 return nb_bytes;
588
Christopher Faulet67957bd2017-09-27 11:00:59 +0200589 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200590 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200591}
592
Christopher Faulet67957bd2017-09-27 11:00:59 +0200593/* Checks for any obsolete record, also identify any SRV request, and try to
594 * find a corresponding server.
595*/
Emeric Brund30e9a12020-12-23 18:49:16 +0100596static void resolv_check_response(struct resolv_resolution *res)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200597{
Emeric Brun750fe792020-12-23 16:51:12 +0100598 struct resolvers *resolvers = res->resolvers;
Emeric Brun08622d32020-12-23 17:41:43 +0100599 struct resolv_requester *req, *reqback;
Emeric Brun85914e92020-12-23 16:38:06 +0100600 struct resolv_answer_item *item, *itemback;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200601 struct server *srv;
Emeric Brun08622d32020-12-23 17:41:43 +0100602 struct resolv_srvrq *srvrq;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200603
604 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
Emeric Brun85914e92020-12-23 16:38:06 +0100605 struct resolv_answer_item *ar_item = item->ar_item;
Christopher Faulet5a891752020-09-08 10:06:01 +0200606
607 /* clean up obsolete Additional record */
608 if (ar_item && (ar_item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
Emeric Brun85914e92020-12-23 16:38:06 +0100609 pool_free(resolv_answer_item_pool, ar_item);
Christopher Faulet5a891752020-09-08 10:06:01 +0200610 item->ar_item = NULL;
611 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200612
613 /* Remove obsolete items */
614 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
615 if (item->type != DNS_RTYPE_SRV)
616 goto rm_obselete_item;
617
618 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
Emeric Brun08622d32020-12-23 17:41:43 +0100619 if ((srvrq = objt_resolv_srvrq(req->owner)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200620 continue;
621
622 /* Remove any associated server */
623 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100624 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200625 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
626 item->data_len == srv->hostname_dn_len &&
Emeric Brund30e9a12020-12-23 18:49:16 +0100627 !resolv_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200628 snr_update_srv_status(srv, 1);
629 free(srv->hostname);
630 free(srv->hostname_dn);
631 srv->hostname = NULL;
632 srv->hostname_dn = NULL;
633 srv->hostname_dn_len = 0;
Emeric Brund30e9a12020-12-23 18:49:16 +0100634 resolv_unlink_resolution(srv->resolv_requester);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200635 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100636 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200637 }
638 }
639
640 rm_obselete_item:
641 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +0200642 if (item->ar_item) {
Emeric Brun85914e92020-12-23 16:38:06 +0100643 pool_free(resolv_answer_item_pool, item->ar_item);
Christopher Faulet5a891752020-09-08 10:06:01 +0200644 item->ar_item = NULL;
645 }
Emeric Brun85914e92020-12-23 16:38:06 +0100646 pool_free(resolv_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200647 continue;
648 }
649
650 if (item->type != DNS_RTYPE_SRV)
651 continue;
652
653 /* Now process SRV records */
654 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
Emeric Brun08622d32020-12-23 17:41:43 +0100655 if ((srvrq = objt_resolv_srvrq(req->owner)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200656 continue;
657
658 /* Check if a server already uses that hostname */
659 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100660 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200661 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
662 item->data_len == srv->hostname_dn_len &&
Emeric Brund30e9a12020-12-23 18:49:16 +0100663 !resolv_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200664 break;
665 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100666 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200667 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200668
669 /* If not, try to find a server with undefined hostname */
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200670 if (!srv) {
671 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
672 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
673 if (srv->srvrq == srvrq && !srv->hostname_dn)
674 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100675 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100676 }
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200677 }
Baptiste Assmann13a92322019-06-07 09:40:55 +0200678
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200679 /* And update this server, if found (srv is locked here) */
680 if (srv) {
Baptiste Assmann13a92322019-06-07 09:40:55 +0200681 /* Check if an Additional Record is associated to this SRV record.
682 * Perform some sanity checks too to ensure the record can be used.
683 * If all fine, we simply pick up the IP address found and associate
684 * it to the server.
685 */
686 if ((item->ar_item != NULL) &&
687 (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA))
688 {
689
690 switch (item->ar_item->type) {
691 case DNS_RTYPE_A:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200692 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 +0200693 break;
694 case DNS_RTYPE_AAAA:
Baptiste Assmanncde83032020-08-04 10:54:14 +0200695 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 +0200696 break;
697 }
698
699 srv->flags |= SRV_F_NO_RESOLUTION;
700 }
701
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200702 if (!srv->hostname_dn) {
703 const char *msg = NULL;
704 char hostname[DNS_MAX_NAME_SIZE];
705
Emeric Brund30e9a12020-12-23 18:49:16 +0100706 if (resolv_dn_label_to_str(item->target, item->data_len+1,
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200707 hostname, DNS_MAX_NAME_SIZE) == -1) {
708 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
709 continue;
710 }
711 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
712 if (msg)
713 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
714 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200715
Baptiste Assmann87138c32020-08-04 10:57:21 +0200716 /* now we have an IP address associated to this server, we can update its status */
717 snr_update_srv_status(srv, 0);
718
Christopher Faulet67957bd2017-09-27 11:00:59 +0200719 srv->svc_port = item->port;
720 srv->flags &= ~SRV_F_MAPPORTS;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100721
Emeric Brun21fbeed2020-12-23 18:01:04 +0100722 if (!srv->resolv_opts.ignore_weight) {
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200723 char weight[9];
724 int ha_weight;
725
Daniel Corbettf8716912019-11-17 09:48:56 -0500726 /* DNS weight range if from 0 to 65535
727 * HAProxy weight is from 0 to 256
728 * The rule below ensures that weight 0 is well respected
729 * while allowing a "mapping" from DNS weight into HAProxy's one.
730 */
731 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100732
Daniel Corbettf8716912019-11-17 09:48:56 -0500733 snprintf(weight, sizeof(weight), "%d", ha_weight);
734 server_parse_weight_change_request(srv, weight);
735 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100736 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200737 }
738 }
739 }
740}
741
742/* Validates that the buffer DNS response provided in <resp> and finishing
743 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200744 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200745 * The result is stored in <resolution>' response, buf_response,
746 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200747 *
Emeric Brun30c766e2020-12-23 18:12:31 +0100748 * This function returns one of the RSLV_RESP_* code to indicate the type of
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200749 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200750 */
Emeric Brund30e9a12020-12-23 18:49:16 +0100751static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufend,
752 struct resolv_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200753{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200754 unsigned char *reader;
755 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200756 int len, flags, offset;
Emeric Brun85914e92020-12-23 16:38:06 +0100757 int query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200758 int nb_saved_records;
Emeric Brun85914e92020-12-23 16:38:06 +0100759 struct resolv_query_item *query;
760 struct resolv_answer_item *answer_record, *tmp_record;
761 struct resolv_response *r_res;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200762 int i, found = 0;
Emeric Brun30c766e2020-12-23 18:12:31 +0100763 int cause = RSLV_RESP_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200764
Christopher Faulet67957bd2017-09-27 11:00:59 +0200765 reader = resp;
766 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200767 previous_dname = NULL;
Emeric Brun85914e92020-12-23 16:38:06 +0100768 query = NULL;
769 answer_record = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200770
Christopher Faulet67957bd2017-09-27 11:00:59 +0200771 /* Initialization of response buffer and structure */
Emeric Brun85914e92020-12-23 16:38:06 +0100772 r_res = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200773
774 /* query id */
775 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200776 goto invalid_resp;
777
Emeric Brun85914e92020-12-23 16:38:06 +0100778 r_res->header.id = reader[0] * 256 + reader[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200780
Christopher Faulet67957bd2017-09-27 11:00:59 +0200781 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200782 * First byte contains:
783 * - response flag (1 bit)
784 * - opcode (4 bits)
785 * - authoritative (1 bit)
786 * - truncated (1 bit)
787 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200788 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200789 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200790 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200791
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200792 flags = reader[0] * 256 + reader[1];
793
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200794 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
Willy Tarreau963f7012020-07-22 17:00:45 +0200795 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
Emeric Brun30c766e2020-12-23 18:12:31 +0100796 cause = RSLV_RESP_NX_DOMAIN;
Willy Tarreau963f7012020-07-22 17:00:45 +0200797 goto return_error;
798 }
799 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) {
Emeric Brun30c766e2020-12-23 18:12:31 +0100800 cause = RSLV_RESP_REFUSED;
Willy Tarreau963f7012020-07-22 17:00:45 +0200801 goto return_error;
802 }
803 else {
Emeric Brun30c766e2020-12-23 18:12:31 +0100804 cause = RSLV_RESP_ERROR;
Willy Tarreau963f7012020-07-22 17:00:45 +0200805 goto return_error;
806 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807 }
808
Christopher Faulet67957bd2017-09-27 11:00:59 +0200809 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200810 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200811
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200812 /* 2 bytes for question count */
813 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200814 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100815 r_res->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200816 /* (for now) we send one query only, so we expect only one in the
817 * response too */
Emeric Brun85914e92020-12-23 16:38:06 +0100818 if (r_res->header.qdcount != 1) {
Emeric Brun30c766e2020-12-23 18:12:31 +0100819 cause = RSLV_RESP_QUERY_COUNT_ERROR;
Willy Tarreau963f7012020-07-22 17:00:45 +0200820 goto return_error;
821 }
822
Emeric Brun85914e92020-12-23 16:38:06 +0100823 if (r_res->header.qdcount > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200824 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200825 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200826
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200827 /* 2 bytes for answer count */
828 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200829 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100830 r_res->header.ancount = reader[0] * 256 + reader[1];
831 if (r_res->header.ancount == 0) {
Emeric Brun30c766e2020-12-23 18:12:31 +0100832 cause = RSLV_RESP_ANCOUNT_ZERO;
Willy Tarreau963f7012020-07-22 17:00:45 +0200833 goto return_error;
834 }
835
Christopher Faulet67957bd2017-09-27 11:00:59 +0200836 /* Check if too many records are announced */
Emeric Brun85914e92020-12-23 16:38:06 +0100837 if (r_res->header.ancount > max_answer_records)
Willy Tarreau963f7012020-07-22 17:00:45 +0200838 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200839 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200840
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200841 /* 2 bytes authority count */
842 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200843 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100844 r_res->header.nscount = reader[0] * 256 + reader[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200845 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200846
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200847 /* 2 bytes additional count */
848 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200849 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100850 r_res->header.arcount = reader[0] * 256 + reader[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200851 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200852
Christopher Faulet67957bd2017-09-27 11:00:59 +0200853 /* Parsing dns queries */
Emeric Brun85914e92020-12-23 16:38:06 +0100854 LIST_INIT(&r_res->query_list);
855 for (query_record_id = 0; query_record_id < r_res->header.qdcount; query_record_id++) {
856 /* Use next pre-allocated resolv_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200857 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200858 * It's then added to our packet query list. */
Emeric Brun85914e92020-12-23 16:38:06 +0100859 if (query_record_id > DNS_MAX_QUERY_RECORDS)
Willy Tarreau963f7012020-07-22 17:00:45 +0200860 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100861 query = &resolution->response_query_records[query_record_id];
862 LIST_ADDQ(&r_res->query_list, &query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200863
Christopher Faulet67957bd2017-09-27 11:00:59 +0200864 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200865 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200866 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200867 offset = 0;
Emeric Brund30e9a12020-12-23 18:49:16 +0100868 len = resolv_read_name(resp, bufend, reader, query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200869
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200870 if (len == 0)
Willy Tarreau963f7012020-07-22 17:00:45 +0200871 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200872
873 reader += offset;
Emeric Brun85914e92020-12-23 16:38:06 +0100874 previous_dname = query->name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200875
876 /* move forward 2 bytes for question type */
877 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200878 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100879 query->type = reader[0] * 256 + reader[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200880 reader += 2;
881
882 /* move forward 2 bytes for question class */
883 if (reader + 2 >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200884 goto invalid_resp;
Emeric Brun85914e92020-12-23 16:38:06 +0100885 query->class = reader[0] * 256 + reader[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200886 reader += 2;
887 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200888
Baptiste Assmann251abb92017-08-11 09:58:27 +0200889 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200890 * because a TRUNCATED SRV query type response can still be exploited */
Emeric Brun85914e92020-12-23 16:38:06 +0100891 if (query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) {
Emeric Brun30c766e2020-12-23 18:12:31 +0100892 cause = RSLV_RESP_TRUNCATED;
Willy Tarreau963f7012020-07-22 17:00:45 +0200893 goto return_error;
894 }
Baptiste Assmann251abb92017-08-11 09:58:27 +0200895
Baptiste Assmann325137d2015-04-13 23:40:55 +0200896 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200897 nb_saved_records = 0;
Emeric Brun85914e92020-12-23 16:38:06 +0100898 for (i = 0; i < r_res->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200899 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200900 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200901
Emeric Brun85914e92020-12-23 16:38:06 +0100902 answer_record = pool_alloc(resolv_answer_item_pool);
903 if (answer_record == NULL)
Willy Tarreau963f7012020-07-22 17:00:45 +0200904 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200905
Baptiste Assmann65547422021-01-15 17:01:24 +0100906 /* initialization */
Emeric Brun85914e92020-12-23 16:38:06 +0100907 answer_record->ar_item = NULL;
Baptiste Assmann65547422021-01-15 17:01:24 +0100908
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200909 offset = 0;
Emeric Brund30e9a12020-12-23 18:49:16 +0100910 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200911
Willy Tarreau963f7012020-07-22 17:00:45 +0200912 if (len == 0)
913 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200914
Christopher Faulet67957bd2017-09-27 11:00:59 +0200915 /* Check if the current record dname is valid. previous_dname
916 * points either to queried dname or last CNAME target */
Emeric Brund30e9a12020-12-23 18:49:16 +0100917 if (query->type != DNS_RTYPE_SRV && resolv_hostname_cmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200918 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200919 /* First record, means a mismatch issue between
920 * queried dname and dname found in the first
921 * record */
Willy Tarreau963f7012020-07-22 17:00:45 +0200922 goto invalid_resp;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200923 }
924 else {
925 /* If not the first record, this means we have a
Willy Tarreau963f7012020-07-22 17:00:45 +0200926 * CNAME resolution error.
927 */
Emeric Brun30c766e2020-12-23 18:12:31 +0100928 cause = RSLV_RESP_CNAME_ERROR;
Willy Tarreau963f7012020-07-22 17:00:45 +0200929 goto return_error;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200930 }
931
Baptiste Assmann325137d2015-04-13 23:40:55 +0200932 }
933
Emeric Brun85914e92020-12-23 16:38:06 +0100934 memcpy(answer_record->name, tmpname, len);
935 answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200936
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200937 reader += offset;
Willy Tarreau963f7012020-07-22 17:00:45 +0200938 if (reader >= bufend)
939 goto invalid_resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200940
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200941 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200942 if (reader + 2 > bufend)
943 goto invalid_resp;
944
Emeric Brun85914e92020-12-23 16:38:06 +0100945 answer_record->type = reader[0] * 256 + reader[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200946 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200947
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200948 /* 2 bytes for class (2) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200949 if (reader + 2 > bufend)
950 goto invalid_resp;
951
Emeric Brun85914e92020-12-23 16:38:06 +0100952 answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200953 reader += 2;
954
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200955 /* 4 bytes for ttl (4) */
Willy Tarreau963f7012020-07-22 17:00:45 +0200956 if (reader + 4 > bufend)
957 goto invalid_resp;
958
Emeric Brun85914e92020-12-23 16:38:06 +0100959 answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
960 + reader[2] * 256 + reader[3];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200961 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200962
Christopher Faulet67957bd2017-09-27 11:00:59 +0200963 /* Now reading data len */
Willy Tarreau963f7012020-07-22 17:00:45 +0200964 if (reader + 2 > bufend)
965 goto invalid_resp;
966
Emeric Brun85914e92020-12-23 16:38:06 +0100967 answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968
Christopher Faulet67957bd2017-09-27 11:00:59 +0200969 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200970 reader += 2;
971
Emeric Brun85914e92020-12-23 16:38:06 +0100972 if (reader + answer_record->data_len > bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +0200973 goto invalid_resp;
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100974
Christopher Faulet67957bd2017-09-27 11:00:59 +0200975 /* Analyzing record content */
Emeric Brun85914e92020-12-23 16:38:06 +0100976 switch (answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200977 case DNS_RTYPE_A:
978 /* ipv4 is stored on 4 bytes */
Emeric Brun85914e92020-12-23 16:38:06 +0100979 if (answer_record->data_len != 4)
Willy Tarreau963f7012020-07-22 17:00:45 +0200980 goto invalid_resp;
981
Emeric Brun85914e92020-12-23 16:38:06 +0100982 answer_record->address.sa_family = AF_INET;
983 memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr),
984 reader, answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200985 break;
986
987 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200988 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200989 * no IP could be found and last record was a CNAME. Could be triggered
990 * by a wrong query type
991 *
Emeric Brun85914e92020-12-23 16:38:06 +0100992 * + 1 because answer_record_id starts at 0
Christopher Faulet67957bd2017-09-27 11:00:59 +0200993 * while number of answers is an integer and
994 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200995 */
Emeric Brun85914e92020-12-23 16:38:06 +0100996 if (i + 1 == r_res->header.ancount) {
Emeric Brun30c766e2020-12-23 18:12:31 +0100997 cause = RSLV_RESP_CNAME_ERROR;
Willy Tarreau963f7012020-07-22 17:00:45 +0200998 goto return_error;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200999 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001000
1001 offset = 0;
Emeric Brund30e9a12020-12-23 18:49:16 +01001002 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +02001003 if (len == 0)
1004 goto invalid_resp;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001005
Emeric Brun85914e92020-12-23 16:38:06 +01001006 memcpy(answer_record->target, tmpname, len);
1007 answer_record->target[len] = 0;
1008 previous_dname = answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001009 break;
1010
Olivier Houchard8da5f982017-08-04 18:35:36 +02001011
1012 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001013 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +02001014 * - 2 bytes for the priority
1015 * - 2 bytes for the weight
1016 * - 2 bytes for the port
1017 * - the target hostname
1018 */
Emeric Brun85914e92020-12-23 16:38:06 +01001019 if (answer_record->data_len <= 6)
Willy Tarreau963f7012020-07-22 17:00:45 +02001020 goto invalid_resp;
1021
Emeric Brun85914e92020-12-23 16:38:06 +01001022 answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001023 reader += sizeof(uint16_t);
Emeric Brun85914e92020-12-23 16:38:06 +01001024 answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001025 reader += sizeof(uint16_t);
Emeric Brun85914e92020-12-23 16:38:06 +01001026 answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001027 reader += sizeof(uint16_t);
1028 offset = 0;
Emeric Brund30e9a12020-12-23 18:49:16 +01001029 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Willy Tarreau963f7012020-07-22 17:00:45 +02001030 if (len == 0)
1031 goto invalid_resp;
1032
Emeric Brun85914e92020-12-23 16:38:06 +01001033 answer_record->data_len = len;
1034 memcpy(answer_record->target, tmpname, len);
1035 answer_record->target[len] = 0;
1036 if (answer_record->ar_item != NULL) {
1037 pool_free(resolv_answer_item_pool, answer_record->ar_item);
1038 answer_record->ar_item = NULL;
Baptiste Assmann65547422021-01-15 17:01:24 +01001039 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001040 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001041
Baptiste Assmann325137d2015-04-13 23:40:55 +02001042 case DNS_RTYPE_AAAA:
1043 /* ipv6 is stored on 16 bytes */
Emeric Brun85914e92020-12-23 16:38:06 +01001044 if (answer_record->data_len != 16)
Willy Tarreau963f7012020-07-22 17:00:45 +02001045 goto invalid_resp;
1046
Emeric Brun85914e92020-12-23 16:38:06 +01001047 answer_record->address.sa_family = AF_INET6;
1048 memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr),
1049 reader, answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001050 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001051
Baptiste Assmann325137d2015-04-13 23:40:55 +02001052 } /* switch (record type) */
1053
Christopher Faulet67957bd2017-09-27 11:00:59 +02001054 /* Increment the counter for number of records saved into our
1055 * local response */
1056 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +02001057
Emeric Brun85914e92020-12-23 16:38:06 +01001058 /* Move forward answer_record->data_len for analyzing next
Christopher Faulet67957bd2017-09-27 11:00:59 +02001059 * record in the response */
Emeric Brun85914e92020-12-23 16:38:06 +01001060 reader += ((answer_record->type == DNS_RTYPE_SRV)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001061 ? offset
Emeric Brun85914e92020-12-23 16:38:06 +01001062 : answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001063
1064 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001065 found = 0;
Emeric Brun85914e92020-12-23 16:38:06 +01001066 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
1067 if (tmp_record->type != answer_record->type)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001068 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001069
1070 switch(tmp_record->type) {
1071 case DNS_RTYPE_A:
Emeric Brun85914e92020-12-23 16:38:06 +01001072 if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr,
Christopher Faulet67957bd2017-09-27 11:00:59 +02001073 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1074 sizeof(in_addr_t)))
1075 found = 1;
1076 break;
1077
1078 case DNS_RTYPE_AAAA:
Emeric Brun85914e92020-12-23 16:38:06 +01001079 if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr,
Christopher Faulet67957bd2017-09-27 11:00:59 +02001080 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
1081 sizeof(struct in6_addr)))
1082 found = 1;
1083 break;
1084
Olivier Houchard8da5f982017-08-04 18:35:36 +02001085 case DNS_RTYPE_SRV:
Emeric Brun85914e92020-12-23 16:38:06 +01001086 if (answer_record->data_len == tmp_record->data_len &&
Emeric Brund30e9a12020-12-23 18:49:16 +01001087 !resolv_hostname_cmp(answer_record->target, tmp_record->target, answer_record->data_len) &&
Emeric Brun85914e92020-12-23 16:38:06 +01001088 answer_record->port == tmp_record->port) {
1089 tmp_record->weight = answer_record->weight;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001090 found = 1;
1091 }
1092 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001093
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001094 default:
1095 break;
1096 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001097
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001098 if (found == 1)
1099 break;
1100 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001101
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001102 if (found == 1) {
1103 tmp_record->last_seen = now.tv_sec;
Emeric Brun85914e92020-12-23 16:38:06 +01001104 pool_free(resolv_answer_item_pool, answer_record);
1105 answer_record = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001106 }
1107 else {
Emeric Brun85914e92020-12-23 16:38:06 +01001108 answer_record->last_seen = now.tv_sec;
1109 answer_record->ar_item = NULL;
1110 LIST_ADDQ(&r_res->answer_list, &answer_record->list);
1111 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 */
Emeric Brun85914e92020-12-23 16:38:06 +01001116 r_res->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 */
Emeric Brun85914e92020-12-23 16:38:06 +01001119 if (query->type != DNS_RTYPE_SRV)
Baptiste Assmann37950c82020-02-19 01:08:51 +01001120 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 */
Emeric Brun85914e92020-12-23 16:38:06 +01001123 for (i = 0; i < r_res->header.nscount; i++) {
Jerome Magnin4002f8d2020-07-26 12:13:12 +02001124 offset = 0;
Emeric Brund30e9a12020-12-23 18:49:16 +01001125 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE,
Jerome Magnin4002f8d2020-07-26 12:13:12 +02001126 &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;
Emeric Brun85914e92020-12-23 16:38:06 +01001151 for (i = 0; i < r_res->header.arcount; i++) {
Baptiste Assmann13a92322019-06-07 09:40:55 +02001152 if (reader >= bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +02001153 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001154
Emeric Brun85914e92020-12-23 16:38:06 +01001155 answer_record = pool_alloc(resolv_answer_item_pool);
1156 if (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;
Emeric Brund30e9a12020-12-23 18:49:16 +01001160 len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001161
1162 if (len == 0) {
Emeric Brun85914e92020-12-23 16:38:06 +01001163 pool_free(resolv_answer_item_pool, answer_record);
1164 answer_record = NULL;
Baptiste Assmann37950c82020-02-19 01:08:51 +01001165 continue;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001166 }
1167
Emeric Brun85914e92020-12-23 16:38:06 +01001168 memcpy(answer_record->name, tmpname, len);
1169 answer_record->name[len] = 0;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001170
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
Emeric Brun85914e92020-12-23 16:38:06 +01001179 answer_record->type = reader[0] * 256 + reader[1];
Baptiste Assmann13a92322019-06-07 09:40:55 +02001180 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
Emeric Brun85914e92020-12-23 16:38:06 +01001186 answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann13a92322019-06-07 09:40:55 +02001187 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
Emeric Brun85914e92020-12-23 16:38:06 +01001193 answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1194 + reader[2] * 256 + reader[3];
Baptiste Assmann13a92322019-06-07 09:40:55 +02001195 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
Emeric Brun85914e92020-12-23 16:38:06 +01001201 answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann13a92322019-06-07 09:40:55 +02001202
1203 /* Move forward 2 bytes for data len */
1204 reader += 2;
1205
Emeric Brun85914e92020-12-23 16:38:06 +01001206 if (reader + answer_record->data_len > bufend)
Willy Tarreau963f7012020-07-22 17:00:45 +02001207 goto invalid_resp;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001208
1209 /* Analyzing record content */
Emeric Brun85914e92020-12-23 16:38:06 +01001210 switch (answer_record->type) {
Baptiste Assmann13a92322019-06-07 09:40:55 +02001211 case DNS_RTYPE_A:
1212 /* ipv4 is stored on 4 bytes */
Emeric Brun85914e92020-12-23 16:38:06 +01001213 if (answer_record->data_len != 4)
Willy Tarreau963f7012020-07-22 17:00:45 +02001214 goto invalid_resp;
1215
Emeric Brun85914e92020-12-23 16:38:06 +01001216 answer_record->address.sa_family = AF_INET;
1217 memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr),
1218 reader, answer_record->data_len);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001219 break;
1220
1221 case DNS_RTYPE_AAAA:
1222 /* ipv6 is stored on 16 bytes */
Emeric Brun85914e92020-12-23 16:38:06 +01001223 if (answer_record->data_len != 16)
Willy Tarreau963f7012020-07-22 17:00:45 +02001224 goto invalid_resp;
1225
Emeric Brun85914e92020-12-23 16:38:06 +01001226 answer_record->address.sa_family = AF_INET6;
1227 memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr),
1228 reader, answer_record->data_len);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001229 break;
1230
1231 default:
Emeric Brun85914e92020-12-23 16:38:06 +01001232 pool_free(resolv_answer_item_pool, answer_record);
1233 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
Emeric Brun85914e92020-12-23 16:38:06 +01001242 /* Move forward answer_record->data_len for analyzing next
Baptiste Assmann13a92322019-06-07 09:40:55 +02001243 * record in the response */
Emeric Brun85914e92020-12-23 16:38:06 +01001244 reader += ((answer_record->type == DNS_RTYPE_SRV)
Baptiste Assmann13a92322019-06-07 09:40:55 +02001245 ? offset
Emeric Brun85914e92020-12-23 16:38:06 +01001246 : answer_record->data_len);
Baptiste Assmann13a92322019-06-07 09:40:55 +02001247
1248 /* Lookup to see if we already had this entry */
1249 found = 0;
Emeric Brun85914e92020-12-23 16:38:06 +01001250 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
1251 if (tmp_record->type != answer_record->type)
Baptiste Assmann13a92322019-06-07 09:40:55 +02001252 continue;
1253
1254 switch(tmp_record->type) {
1255 case DNS_RTYPE_A:
Emeric Brun85914e92020-12-23 16:38:06 +01001256 if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr,
Baptiste Assmann13a92322019-06-07 09:40:55 +02001257 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
1258 sizeof(in_addr_t)))
1259 found = 1;
1260 break;
1261
1262 case DNS_RTYPE_AAAA:
Emeric Brun85914e92020-12-23 16:38:06 +01001263 if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr,
Baptiste Assmann13a92322019-06-07 09:40:55 +02001264 &((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;
Emeric Brun85914e92020-12-23 16:38:06 +01001279 pool_free(resolv_answer_item_pool, answer_record);
1280 answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001281 }
1282 else {
Emeric Brun85914e92020-12-23 16:38:06 +01001283 answer_record->last_seen = now.tv_sec;
1284 answer_record->ar_item = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001285
1286 // looking for the SRV record in the response list linked to this additional record
Emeric Brun85914e92020-12-23 16:38:06 +01001287 list_for_each_entry(tmp_record, &r_res->answer_list, list) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001288 if (tmp_record->type == DNS_RTYPE_SRV &&
Baptiste Assmann65547422021-01-15 17:01:24 +01001289 tmp_record->ar_item == NULL &&
Emeric Brund30e9a12020-12-23 18:49:16 +01001290 !resolv_hostname_cmp(tmp_record->target, answer_record->name, tmp_record->data_len)) {
Christopher Faulet5a891752020-09-08 10:06:01 +02001291 /* Always use the received additional record to refresh info */
Emeric Brun85914e92020-12-23 16:38:06 +01001292 if (tmp_record->ar_item)
1293 pool_free(resolv_answer_item_pool, tmp_record->ar_item);
1294 tmp_record->ar_item = answer_record;
Christopher Faulet5a891752020-09-08 10:06:01 +02001295 break;
1296 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02001297 }
Emeric Brun85914e92020-12-23 16:38:06 +01001298 if (tmp_record->ar_item != answer_record)
1299 pool_free(resolv_answer_item_pool, answer_record);
1300 answer_record = NULL;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001301 }
1302 } /* for i 0 to arcount */
1303
Baptiste Assmann37950c82020-02-19 01:08:51 +01001304 skip_parsing_additional_records:
1305
Baptiste Assmann13a92322019-06-07 09:40:55 +02001306 /* Save the number of records we really own */
Emeric Brun85914e92020-12-23 16:38:06 +01001307 r_res->header.arcount = nb_saved_records;
Baptiste Assmann13a92322019-06-07 09:40:55 +02001308
Emeric Brund30e9a12020-12-23 18:49:16 +01001309 resolv_check_response(resolution);
Emeric Brun30c766e2020-12-23 18:12:31 +01001310 return RSLV_RESP_VALID;
Willy Tarreau963f7012020-07-22 17:00:45 +02001311
1312 invalid_resp:
Emeric Brun30c766e2020-12-23 18:12:31 +01001313 cause = RSLV_RESP_INVALID;
Willy Tarreau963f7012020-07-22 17:00:45 +02001314
1315 return_error:
Emeric Brun85914e92020-12-23 16:38:06 +01001316 pool_free(resolv_answer_item_pool, answer_record);
Willy Tarreau963f7012020-07-22 17:00:45 +02001317 return cause;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001318}
1319
Christopher Faulet67957bd2017-09-27 11:00:59 +02001320/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001321 * If existing IP not found, return the first IP matching family_priority,
1322 * otherwise, first ip found
1323 * The following tasks are the responsibility of the caller:
Emeric Brund30e9a12020-12-23 18:49:16 +01001324 * - <r_res> contains an error free DNS response
1325 * For both cases above, resolv_validate_dns_response is required
Emeric Brun456de772020-12-23 18:17:31 +01001326 * returns one of the RSLV_UPD_* code
Baptiste Assmann325137d2015-04-13 23:40:55 +02001327 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001328int resolv_get_ip_from_response(struct resolv_response *r_res,
Emeric Brun21fbeed2020-12-23 18:01:04 +01001329 struct resolv_options *resolv_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001330 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001331 void **newip, short *newip_sin_family,
1332 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001333{
Emeric Brun85914e92020-12-23 16:38:06 +01001334 struct resolv_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001335 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001336 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001337 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001338 int currentip_sel;
1339 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001340 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001341 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001342
Emeric Brun21fbeed2020-12-23 18:01:04 +01001343 family_priority = resolv_opts->family_prio;
1344 allowed_duplicated_ip = resolv_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001345 *newip = newip4 = newip6 = NULL;
1346 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001347 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001348 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001349
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001350 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001351 * Top priority is the preferred network ip version,
1352 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001353 * the last priority is the currently used IP,
1354 *
1355 * For these three priorities, a score is calculated. The
1356 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001357 * 8 - preferred ip version.
1358 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001359 * 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 +01001360 * 1 - current ip.
1361 * The result with the biggest score is returned.
1362 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001363
Emeric Brun85914e92020-12-23 16:38:06 +01001364 list_for_each_entry(record, &r_res->answer_list, list) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001365 void *ip;
1366 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001367
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001368 if (record->type == DNS_RTYPE_A) {
1369 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1370 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001371 }
1372 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001373 ip_type = AF_INET6;
1374 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001375 }
1376 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001377 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001378 score = 0;
1379
Joseph Herlant42cf6392018-11-15 10:33:28 -08001380 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001381 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001382 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001383
Joseph Herlant42cf6392018-11-15 10:33:28 -08001384 /* Check for preferred network. */
Emeric Brun21fbeed2020-12-23 18:01:04 +01001385 for (j = 0; j < resolv_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001386
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001387 /* Compare only the same addresses class. */
Emeric Brun21fbeed2020-12-23 18:01:04 +01001388 if (resolv_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001389 continue;
1390
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001391 if ((ip_type == AF_INET &&
1392 in_net_ipv4(ip,
Emeric Brun21fbeed2020-12-23 18:01:04 +01001393 &resolv_opts->pref_net[j].mask.in4,
1394 &resolv_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001395 (ip_type == AF_INET6 &&
1396 in_net_ipv6(ip,
Emeric Brun21fbeed2020-12-23 18:01:04 +01001397 &resolv_opts->pref_net[j].mask.in6,
1398 &resolv_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001399 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001400 break;
1401 }
1402 }
1403
Christopher Faulet67957bd2017-09-27 11:00:59 +02001404 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001405 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001406 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001407 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001408 if (!allowed_duplicated_ip) {
1409 continue;
1410 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001411 } else {
1412 score += 2;
1413 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001414
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001415 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001416 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001417 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001418 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001419 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001420 !memcmp(ip, currentip, 16)))) {
1421 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001422 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001423 }
1424 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001425 currentip_sel = 0;
1426
1427 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001428 * score. The maximum score is 15, if this value is reached, we
1429 * break the parsing. Implicitly, this score is reached the ip
1430 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001431 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001432 if (ip_type == AF_INET)
1433 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001434 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001435 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001436 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001437 if (score == 15)
Emeric Brun456de772020-12-23 18:17:31 +01001438 return RSLV_UPD_NO;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001439 max_score = score;
1440 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001441 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001442
Christopher Faulet67957bd2017-09-27 11:00:59 +02001443 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001444 if (!newip4 && !newip6)
Emeric Brun456de772020-12-23 18:17:31 +01001445 return RSLV_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001446
Christopher Faulet67957bd2017-09-27 11:00:59 +02001447 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001448 if (family_priority == AF_INET) {
1449 if (newip4) {
1450 *newip = newip4;
1451 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001452 }
1453 else if (newip6) {
1454 *newip = newip6;
1455 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001456 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001457 if (!currentip_found)
1458 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001459 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001460 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001461 else if (family_priority == AF_INET6) {
1462 if (newip6) {
1463 *newip = newip6;
1464 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001465 }
1466 else if (newip4) {
1467 *newip = newip4;
1468 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001469 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001470 if (!currentip_found)
1471 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001472 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001473 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001474 else if (family_priority == AF_UNSPEC) {
1475 if (newip6) {
1476 *newip = newip6;
1477 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001478 }
1479 else if (newip4) {
1480 *newip = newip4;
1481 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001482 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001483 if (!currentip_found)
1484 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001485 }
1486
Christopher Faulet67957bd2017-09-27 11:00:59 +02001487 /* No reason why we should change the server's IP address */
Emeric Brun456de772020-12-23 18:17:31 +01001488 return RSLV_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001489
Christopher Faulet67957bd2017-09-27 11:00:59 +02001490 not_found:
Emeric Brun85914e92020-12-23 16:38:06 +01001491 list_for_each_entry(record, &r_res->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001492 /* Move the first record to the end of the list, for internal
1493 * round robin */
1494 LIST_DEL(&record->list);
Emeric Brun85914e92020-12-23 16:38:06 +01001495 LIST_ADDQ(&r_res->answer_list, &record->list);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001496 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001497 }
Emeric Brun456de772020-12-23 18:17:31 +01001498 return RSLV_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001499}
1500
Christopher Faulet67957bd2017-09-27 11:00:59 +02001501/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001502 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001503 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1504 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001505 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001506 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1507 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001508 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001509int resolv_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001510{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001511 char *ptr;
1512 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001513
Christopher Faulet67957bd2017-09-27 11:00:59 +02001514 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001515 return -1;
1516
Christopher Faulet67957bd2017-09-27 11:00:59 +02001517 ptr = str;
1518 for (i = 0; i < dn_len-1; ++i) {
1519 sz = dn[i];
1520 if (i)
1521 *ptr++ = '.';
1522 memcpy(ptr, dn+i+1, sz);
1523 ptr += sz;
1524 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001525 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001526 *ptr++ = '\0';
1527 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001528}
1529
Christopher Faulet67957bd2017-09-27 11:00:59 +02001530/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1531 *
1532 * <str> must be a null-terminated string. <str_len> must include the
1533 * terminating null byte. <dn> buffer must be allocated and its size must be
1534 * passed in <dn_len>.
1535 *
1536 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1537 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001538 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001539int resolv_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001540{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001541 int i, offset;
1542
Christopher Faulet67957bd2017-09-27 11:00:59 +02001543 if (dn_len < str_len + 1)
1544 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001545
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 /* First byte of dn will be used to store the length of the first
1547 * label */
1548 offset = 0;
1549 for (i = 0; i < str_len; ++i) {
1550 if (str[i] == '.') {
1551 /* 2 or more consecutive dots is invalid */
1552 if (i == offset)
1553 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001554
Lukas Tribus81725b82020-02-27 15:47:24 +01001555 /* ignore trailing dot */
1556 if (i + 2 == str_len) {
1557 i++;
1558 break;
1559 }
1560
Christopher Faulet67957bd2017-09-27 11:00:59 +02001561 dn[offset] = (i - offset);
1562 offset = i+1;
1563 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001564 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001565 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001566 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001567 dn[offset] = (i - offset - 1);
1568 dn[i] = '\0';
1569 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001570}
1571
Christopher Faulet67957bd2017-09-27 11:00:59 +02001572/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001573 * - total size
1574 * - each label size individually
1575 * returns:
1576 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1577 * 1 when no error. <err> is left unaffected.
1578 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001579int resolv_hostname_validation(const char *string, char **err)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001580{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001581 int i;
1582
1583 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1584 if (err)
1585 *err = DNS_TOO_LONG_FQDN;
1586 return 0;
1587 }
1588
William Dauchyaecd5dc2020-01-26 19:52:34 +01001589 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001590 i = 0;
William Dauchyaecd5dc2020-01-26 19:52:34 +01001591 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1592 if (!(*string == '-' || *string == '_' ||
1593 (*string >= 'a' && *string <= 'z') ||
1594 (*string >= 'A' && *string <= 'Z') ||
1595 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001596 if (err)
1597 *err = DNS_INVALID_CHARACTER;
1598 return 0;
1599 }
William Dauchyaecd5dc2020-01-26 19:52:34 +01001600 i++;
1601 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001602 }
1603
William Dauchyaecd5dc2020-01-26 19:52:34 +01001604 if (!(*string))
1605 break;
1606
1607 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001608 if (err)
1609 *err = DNS_LABEL_TOO_LONG;
1610 return 0;
1611 }
1612
William Dauchyaecd5dc2020-01-26 19:52:34 +01001613 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001614 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001615 return 1;
1616}
1617
Christopher Faulet67957bd2017-09-27 11:00:59 +02001618/* Picks up an available resolution from the different resolution list
1619 * associated to a resolvers section, in this order:
1620 * 1. check in resolutions.curr for the same hostname and query_type
1621 * 2. check in resolutions.wait for the same hostname and query_type
1622 * 3. Get a new resolution from resolution pool
1623 *
1624 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001625 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001626static struct resolv_resolution *resolv_pick_resolution(struct resolvers *resolvers,
1627 char **hostname_dn, int hostname_dn_len,
1628 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001629{
Emeric Brun08622d32020-12-23 17:41:43 +01001630 struct resolv_resolution *res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001631
1632 if (!*hostname_dn)
1633 goto from_pool;
1634
1635 /* Search for same hostname and query type in resolutions.curr */
1636 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1637 if (!res->hostname_dn)
1638 continue;
1639 if ((query_type == res->prefered_query_type) &&
1640 hostname_dn_len == res->hostname_dn_len &&
Emeric Brund30e9a12020-12-23 18:49:16 +01001641 !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001642 return res;
1643 }
1644
1645 /* Search for same hostname and query type in resolutions.wait */
1646 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1647 if (!res->hostname_dn)
1648 continue;
1649 if ((query_type == res->prefered_query_type) &&
1650 hostname_dn_len == res->hostname_dn_len &&
Emeric Brund30e9a12020-12-23 18:49:16 +01001651 !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652 return res;
1653 }
1654
1655 from_pool:
1656 /* No resolution could be found, so let's allocate a new one */
Emeric Brun08622d32020-12-23 17:41:43 +01001657 res = pool_alloc(resolv_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658 if (res) {
1659 memset(res, 0, sizeof(*res));
1660 res->resolvers = resolvers;
1661 res->uuid = resolution_uuid;
1662 res->status = RSLV_STATUS_NONE;
1663 res->step = RSLV_STEP_NONE;
1664 res->last_valid = now_ms;
1665
1666 LIST_INIT(&res->requesters);
1667 LIST_INIT(&res->response.answer_list);
1668
1669 res->prefered_query_type = query_type;
1670 res->query_type = query_type;
1671 res->hostname_dn = *hostname_dn;
1672 res->hostname_dn_len = hostname_dn_len;
1673
1674 ++resolution_uuid;
1675
1676 /* Move the resolution to the resolvers wait queue */
1677 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1678 }
1679 return res;
1680}
1681
1682/* Releases a resolution from its requester(s) and move it back to the pool */
Emeric Brund30e9a12020-12-23 18:49:16 +01001683static void resolv_free_resolution(struct resolv_resolution *resolution)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684{
Emeric Brun08622d32020-12-23 17:41:43 +01001685 struct resolv_requester *req, *reqback;
Emeric Brun85914e92020-12-23 16:38:06 +01001686 struct resolv_answer_item *item, *itemback;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687
1688 /* clean up configuration */
Emeric Brund30e9a12020-12-23 18:49:16 +01001689 resolv_reset_resolution(resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001690 resolution->hostname_dn = NULL;
1691 resolution->hostname_dn_len = 0;
1692
1693 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1694 LIST_DEL(&req->list);
1695 req->resolution = NULL;
1696 }
1697
Christopher Faulet010ab352020-07-22 15:55:49 +02001698 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
1699 LIST_DEL(&item->list);
Christopher Faulet5a891752020-09-08 10:06:01 +02001700 if (item->ar_item) {
Emeric Brun85914e92020-12-23 16:38:06 +01001701 pool_free(resolv_answer_item_pool, item->ar_item);
Christopher Faulet5a891752020-09-08 10:06:01 +02001702 item->ar_item = NULL;
1703 }
Emeric Brun85914e92020-12-23 16:38:06 +01001704 pool_free(resolv_answer_item_pool, item);
Christopher Faulet010ab352020-07-22 15:55:49 +02001705 }
1706
Christopher Faulet67957bd2017-09-27 11:00:59 +02001707 LIST_DEL(&resolution->list);
Emeric Brun08622d32020-12-23 17:41:43 +01001708 pool_free(resolv_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001709}
1710
Emeric Brun08622d32020-12-23 17:41:43 +01001711/* Links a requester (a server or a resolv_srvrq) with a resolution. It returns 0
Christopher Faulet67957bd2017-09-27 11:00:59 +02001712 * on success, -1 otherwise.
1713 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001714int resolv_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001715{
Emeric Brun08622d32020-12-23 17:41:43 +01001716 struct resolv_resolution *res = NULL;
1717 struct resolv_requester *req;
Emeric Brun750fe792020-12-23 16:51:12 +01001718 struct resolvers *resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001719 struct server *srv = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01001720 struct resolv_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001721 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001722 char **hostname_dn;
1723 int hostname_dn_len, query_type;
1724
1725 switch (requester_type) {
1726 case OBJ_TYPE_SERVER:
1727 srv = (struct server *)requester;
1728 hostname_dn = &srv->hostname_dn;
1729 hostname_dn_len = srv->hostname_dn_len;
1730 resolvers = srv->resolvers;
Emeric Brun21fbeed2020-12-23 18:01:04 +01001731 query_type = ((srv->resolv_opts.family_prio == AF_INET)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001732 ? DNS_RTYPE_A
1733 : DNS_RTYPE_AAAA);
1734 break;
1735
1736 case OBJ_TYPE_SRVRQ:
Emeric Brun08622d32020-12-23 17:41:43 +01001737 srvrq = (struct resolv_srvrq *)requester;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001738 hostname_dn = &srvrq->hostname_dn;
1739 hostname_dn_len = srvrq->hostname_dn_len;
1740 resolvers = srvrq->resolvers;
1741 query_type = DNS_RTYPE_SRV;
1742 break;
1743
Baptiste Assmann333939c2019-01-21 08:34:50 +01001744 case OBJ_TYPE_STREAM:
1745 stream = (struct stream *)requester;
Emeric Brun08622d32020-12-23 17:41:43 +01001746 hostname_dn = &stream->resolv_ctx.hostname_dn;
1747 hostname_dn_len = stream->resolv_ctx.hostname_dn_len;
Emeric Brun21fbeed2020-12-23 18:01:04 +01001748 resolvers = stream->resolv_ctx.parent->arg.resolv.resolvers;
1749 query_type = ((stream->resolv_ctx.parent->arg.resolv.opts->family_prio == AF_INET)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001750 ? DNS_RTYPE_A
1751 : DNS_RTYPE_AAAA);
1752 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001753 default:
1754 goto err;
1755 }
1756
1757 /* Get a resolution from the resolvers' wait queue or pool */
Emeric Brund30e9a12020-12-23 18:49:16 +01001758 if ((res = resolv_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001759 goto err;
1760
1761 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001762 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001763 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun08622d32020-12-23 17:41:43 +01001764 if (srv->resolv_requester == NULL) {
1765 if ((req = pool_alloc(resolv_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001766 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001767 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001768 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001769 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001770 req->owner = &srv->obj_type;
Emeric Brun08622d32020-12-23 17:41:43 +01001771 srv->resolv_requester = req;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001772 }
1773 else
Emeric Brun08622d32020-12-23 17:41:43 +01001774 req = srv->resolv_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001775 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001776 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001777
1778 req->requester_cb = snr_resolution_cb;
1779 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001780 }
1781 else if (srvrq) {
Emeric Brun08622d32020-12-23 17:41:43 +01001782 if (srvrq->requester == NULL) {
1783 if ((req = pool_alloc(resolv_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001784 goto err;
1785 req->owner = &srvrq->obj_type;
Emeric Brun08622d32020-12-23 17:41:43 +01001786 srvrq->requester = req;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001787 }
1788 else
Emeric Brun08622d32020-12-23 17:41:43 +01001789 req = srvrq->requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001790
1791 req->requester_cb = snr_resolution_cb;
1792 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001793 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001794 else if (stream) {
Emeric Brun08622d32020-12-23 17:41:43 +01001795 if (stream->resolv_ctx.requester == NULL) {
1796 if ((req = pool_alloc(resolv_requester_pool)) == NULL)
Baptiste Assmann333939c2019-01-21 08:34:50 +01001797 goto err;
1798 req->owner = &stream->obj_type;
Emeric Brun08622d32020-12-23 17:41:43 +01001799 stream->resolv_ctx.requester = req;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001800 }
1801 else
Emeric Brun08622d32020-12-23 17:41:43 +01001802 req = stream->resolv_ctx.requester;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001803
1804 req->requester_cb = act_resolution_cb;
1805 req->requester_error_cb = act_resolution_error_cb;
1806 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001807 else
1808 goto err;
1809
1810 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001811
1812 LIST_ADDQ(&res->requesters, &req->list);
1813 return 0;
1814
1815 err:
1816 if (res && LIST_ISEMPTY(&res->requesters))
Emeric Brund30e9a12020-12-23 18:49:16 +01001817 resolv_free_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001818 return -1;
1819}
1820
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001821/* Removes a requester from a DNS resolution. It takes takes care of all the
Christopher Faulet67957bd2017-09-27 11:00:59 +02001822 * consequences. It also cleans up some parameters from the requester.
1823 */
Emeric Brund30e9a12020-12-23 18:49:16 +01001824void resolv_unlink_resolution(struct resolv_requester *requester)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001825{
Emeric Brun08622d32020-12-23 17:41:43 +01001826 struct resolv_resolution *res;
1827 struct resolv_requester *req;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001828
1829 /* Nothing to do */
1830 if (!requester || !requester->resolution)
1831 return;
1832 res = requester->resolution;
1833
1834 /* Clean up the requester */
1835 LIST_DEL(&requester->list);
1836 requester->resolution = NULL;
1837
1838 /* We need to find another requester linked on this resolution */
1839 if (!LIST_ISEMPTY(&res->requesters))
Emeric Brun08622d32020-12-23 17:41:43 +01001840 req = LIST_NEXT(&res->requesters, struct resolv_requester *, list);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001841 else {
Emeric Brund30e9a12020-12-23 18:49:16 +01001842 resolv_free_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001843 return;
1844 }
1845
1846 /* Move hostname_dn related pointers to the next requester */
1847 switch (obj_type(req->owner)) {
1848 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001849 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1850 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001851 break;
1852 case OBJ_TYPE_SRVRQ:
Emeric Brun08622d32020-12-23 17:41:43 +01001853 res->hostname_dn = __objt_resolv_srvrq(req->owner)->hostname_dn;
1854 res->hostname_dn_len = __objt_resolv_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001855 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001856 case OBJ_TYPE_STREAM:
Emeric Brun08622d32020-12-23 17:41:43 +01001857 res->hostname_dn = __objt_stream(req->owner)->resolv_ctx.hostname_dn;
1858 res->hostname_dn_len = __objt_stream(req->owner)->resolv_ctx.hostname_dn_len;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001859 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001860 default:
1861 res->hostname_dn = NULL;
1862 res->hostname_dn_len = 0;
1863 break;
1864 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001865}
1866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867/* Called when a network IO is generated on a name server socket for an incoming
1868 * packet. It performs the following actions:
1869 * - check if the packet requires processing (not outdated resolution)
1870 * - ensure the DNS packet received is valid and call requester's callback
1871 * - call requester's error callback if invalid response
1872 * - check the dn_name in the packet against the one sent
1873 */
1874static void dns_resolve_recv(struct dgram_conn *dgram)
1875{
Emeric Brun50c870e2021-01-04 10:40:46 +01001876 struct dns_nameserver *ns;
1877 struct dns_counters *tmpcounters;
Emeric Brun750fe792020-12-23 16:51:12 +01001878 struct resolvers *resolvers;
Emeric Brun08622d32020-12-23 17:41:43 +01001879 struct resolv_resolution *res;
Emeric Brun85914e92020-12-23 16:38:06 +01001880 struct resolv_query_item *query;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001881 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;
Emeric Brun08622d32020-12-23 17:41:43 +01001887 struct resolv_requester *req;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888
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 }
Emeric Brund30e9a12020-12-23 18:49:16 +01001933 query_id = resolv_response_get_query_id(buf);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934
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 */
Emeric Brun08622d32020-12-23 17:41:43 +01001944 res = eb32_entry(eb, struct resolv_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;
Emeric Brund30e9a12020-12-23 18:49:16 +01001949 dns_resp = resolv_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) {
Emeric Brun30c766e2020-12-23 18:12:31 +01001952 case RSLV_RESP_VALID:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001953 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001954
Emeric Brun30c766e2020-12-23 18:12:31 +01001955 case RSLV_RESP_INVALID:
1956 case RSLV_RESP_QUERY_COUNT_ERROR:
1957 case RSLV_RESP_WRONG_NAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001958 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
Emeric Brun30c766e2020-12-23 18:12:31 +01001962 case RSLV_RESP_NX_DOMAIN:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001963 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
Emeric Brun30c766e2020-12-23 18:12:31 +01001967 case RSLV_RESP_REFUSED:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 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
Emeric Brun30c766e2020-12-23 18:12:31 +01001972 case RSLV_RESP_ANCOUNT_ZERO:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001973 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
Emeric Brun30c766e2020-12-23 18:12:31 +01001977 case RSLV_RESP_CNAME_ERROR:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001978 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
Emeric Brun30c766e2020-12-23 18:12:31 +01001982 case RSLV_RESP_TRUNCATED:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001983 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
Emeric Brun30c766e2020-12-23 18:12:31 +01001987 case RSLV_RESP_NO_EXPECTED_RECORD:
1988 case RSLV_RESP_ERROR:
1989 case RSLV_RESP_INTERNAL:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001990 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 */
Emeric Brun30c766e2020-12-23 18:12:31 +01001996 if (dns_resp != RSLV_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001997 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001998
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 /* Process error codes */
Emeric Brun30c766e2020-12-23 18:12:31 +01002000 if (dns_resp != RSLV_RESP_VALID) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002001 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;
Emeric Brund30e9a12020-12-23 18:49:16 +01002014 resolv_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;
Emeric Brund30e9a12020-12-23 18:49:16 +01002018 resolv_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 */
Emeric Brun85914e92020-12-23 16:38:06 +01002027 query = LIST_NEXT(&res->response.query_list, struct resolv_query_item *, list);
Emeric Brund30e9a12020-12-23 18:49:16 +01002028 if (query && resolv_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
Emeric Brun30c766e2020-12-23 18:12:31 +01002029 dns_resp = RSLV_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);
Emeric Brund30e9a12020-12-23 18:49:16 +01002043 resolv_reset_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002044 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 */
Emeric Brun50c870e2021-01-04 10:40:46 +01002051 tmpcounters = ns->counters;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002052 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);
Emeric Brun50c870e2021-01-04 10:40:46 +01002057 req->requester_cb(req, tmpcounters);
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);
Emeric Brun50c870e2021-01-04 10:40:46 +01002060 tmpcounters = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002061 }
Baptiste Assmann42746372017-05-03 12:12:02 +02002062
Emeric Brund30e9a12020-12-23 18:49:16 +01002063 resolv_reset_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002064 LIST_DEL(&res->list);
2065 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
2066 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002067 }
Emeric Brund30e9a12020-12-23 18:49:16 +01002068 resolv_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{
Emeric Brun750fe792020-12-23 16:51:12 +01002075 struct resolvers *resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002076 struct dns_nameserver *ns;
Emeric Brun08622d32020-12-23 17:41:43 +01002077 struct resolv_resolution *res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002078 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
Emeric Brund30e9a12020-12-23 18:49:16 +01002102 len = resolv_build_query(res->query_id, res->query_type,
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02002103 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 */
Emeric Brund30e9a12020-12-23 18:49:16 +01002135static struct task *process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002136{
Emeric Brun750fe792020-12-23 16:51:12 +01002137 struct resolvers *resolvers = context;
Emeric Brun08622d32020-12-23 17:41:43 +01002138 struct resolv_resolution *res, *resback;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002139 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) {
Emeric Brun08622d32020-12-23 17:41:43 +01002155 struct resolv_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 */
Emeric Brund30e9a12020-12-23 18:49:16 +01002165 resolv_reset_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002166 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 }
Emeric Brund30e9a12020-12-23 18:49:16 +01002187 resolv_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) {
Emeric Brund30e9a12020-12-23 18:49:16 +01002193 exp = tick_add(res->last_resolution, resolv_resolution_timeout(res));
Christopher Faulet67957bd2017-09-27 11:00:59 +02002194 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
2195 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002196
Emeric Brund30e9a12020-12-23 18:49:16 +01002197 if (resolv_run_resolution(res) != 1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002198 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
Emeric Brund30e9a12020-12-23 18:49:16 +01002204 resolv_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 */
Emeric Brund30e9a12020-12-23 18:49:16 +01002216static void resolvers_deinit(void)
Christopher Faulet67957bd2017-09-27 11:00:59 +02002217{
Emeric Brun750fe792020-12-23 16:51:12 +01002218 struct resolvers *resolvers, *resolversback;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002219 struct dns_nameserver *ns, *nsback;
Emeric Brun08622d32020-12-23 17:41:43 +01002220 struct resolv_resolution *res, *resback;
2221 struct resolv_requester *req, *reqback;
2222 struct resolv_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002223
Emeric Brun750fe792020-12-23 16:51:12 +01002224 list_for_each_entry_safe(resolvers, resolversback, &sec_resolvers, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002225 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);
Emeric Brun08622d32020-12-23 17:41:43 +01002239 pool_free(resolv_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002240 }
Emeric Brund30e9a12020-12-23 18:49:16 +01002241 resolv_free_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002242 }
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);
Emeric Brun08622d32020-12-23 17:41:43 +01002247 pool_free(resolv_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002248 }
Emeric Brund30e9a12020-12-23 18:49:16 +01002249 resolv_free_resolution(res);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002250 }
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
Emeric Brun08622d32020-12-23 17:41:43 +01002259 list_for_each_entry_safe(srvrq, srvrqback, &resolv_srvrq_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002260 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 */
Emeric Brund30e9a12020-12-23 18:49:16 +01002271static int resolvers_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002272{
Emeric Brun750fe792020-12-23 16:51:12 +01002273 struct resolvers *resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002274 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 */
Emeric Brun750fe792020-12-23 16:51:12 +01002278 list_for_each_entry(resolvers, &sec_resolvers, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002279 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;
Emeric Brun50c870e2021-01-04 10:40:46 +01002323 ns->counters->pid = ns->resolvers->id;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002324 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002325 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002326
Christopher Faulet67957bd2017-09-27 11:00:59 +02002327 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02002328 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002329 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002330 err_code |= (ERR_ALERT|ERR_ABORT);
2331 goto err;
2332 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002333
Christopher Faulet67957bd2017-09-27 11:00:59 +02002334 /* Update task's parameters */
Emeric Brund30e9a12020-12-23 18:49:16 +01002335 t->process = process_resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002336 t->context = resolvers;
2337 resolvers->t = t;
2338 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002339 }
2340
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002341 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002342 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002343
Christopher Faulet67957bd2017-09-27 11:00:59 +02002344 for (srv = px->srv; srv; srv = srv->next) {
Emeric Brun750fe792020-12-23 16:51:12 +01002345 struct resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002346
Christopher Faulet67957bd2017-09-27 11:00:59 +02002347 if (!srv->resolvers_id)
2348 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002349
Christopher Faulet67957bd2017-09-27 11:00:59 +02002350 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002351 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2352 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002353 err_code |= (ERR_ALERT|ERR_ABORT);
2354 continue;
2355 }
2356 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002357
Christopher Faulet67957bd2017-09-27 11:00:59 +02002358 if (srv->srvrq && !srv->srvrq->resolvers) {
2359 srv->srvrq->resolvers = srv->resolvers;
Emeric Brund30e9a12020-12-23 18:49:16 +01002360 if (resolv_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002361 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2362 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002363 err_code |= (ERR_ALERT|ERR_ABORT);
2364 continue;
2365 }
2366 }
Emeric Brund30e9a12020-12-23 18:49:16 +01002367 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002368 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2369 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002370 err_code |= (ERR_ALERT|ERR_ABORT);
2371 continue;
2372 }
2373 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002374 }
2375
Christopher Faulet67957bd2017-09-27 11:00:59 +02002376 if (err_code & (ERR_ALERT|ERR_ABORT))
2377 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002378
Christopher Faulet67957bd2017-09-27 11:00:59 +02002379 return err_code;
2380 err:
Emeric Brund30e9a12020-12-23 18:49:16 +01002381 resolvers_deinit();
Christopher Faulet67957bd2017-09-27 11:00:59 +02002382 return err_code;
2383
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002384}
2385
2386static int stats_dump_dns_to_buffer(struct stream_interface *si,
2387 struct dns_nameserver *ns,
2388 struct field *stats, size_t stats_count,
2389 struct list *stat_modules)
2390{
2391 struct appctx *appctx = __objt_appctx(si->end);
2392 struct channel *rep = si_ic(si);
2393 struct stats_module *mod;
2394 size_t idx = 0;
2395
2396 memset(stats, 0, sizeof(struct field) * stats_count);
2397
2398 list_for_each_entry(mod, stat_modules, list) {
2399 struct counters_node *counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2400
2401 mod->fill_stats(counters, stats + idx);
2402 idx += mod->stats_count;
2403 }
2404
2405 if (!stats_dump_one_line(stats, idx, appctx))
2406 return 0;
2407
2408 if (!stats_putchk(rep, NULL, &trash))
2409 goto full;
2410
2411 return 1;
2412
2413 full:
2414 si_rx_room_rdy(si);
2415 return 0;
2416}
2417
2418/* Uses <appctx.ctx.stats.obj1> as a pointer to the current resolver and <obj2>
2419 * as a pointer to the current nameserver.
2420 */
2421int stats_dump_dns(struct stream_interface *si,
2422 struct field *stats, size_t stats_count,
2423 struct list *stat_modules)
2424{
2425 struct appctx *appctx = __objt_appctx(si->end);
2426 struct channel *rep = si_ic(si);
Emeric Brun750fe792020-12-23 16:51:12 +01002427 struct resolvers *resolver = appctx->ctx.stats.obj1;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002428 struct dns_nameserver *ns = appctx->ctx.stats.obj2;
2429
2430 if (!resolver)
Emeric Brun750fe792020-12-23 16:51:12 +01002431 resolver = LIST_NEXT(&sec_resolvers, struct resolvers *, list);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002432
2433 /* dump resolvers */
Emeric Brun750fe792020-12-23 16:51:12 +01002434 list_for_each_entry_from(resolver, &sec_resolvers, list) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002435 appctx->ctx.stats.obj1 = resolver;
2436
2437 ns = appctx->ctx.stats.obj2 ?
2438 appctx->ctx.stats.obj2 :
2439 LIST_NEXT(&resolver->nameservers, struct dns_nameserver *, list);
2440
2441 list_for_each_entry_from(ns, &resolver->nameservers, list) {
2442 appctx->ctx.stats.obj2 = ns;
2443
2444 if (buffer_almost_full(&rep->buf))
2445 goto full;
2446
2447 if (!stats_dump_dns_to_buffer(si, ns,
2448 stats, stats_count,
2449 stat_modules)) {
2450 return 0;
2451 }
2452 }
2453
2454 appctx->ctx.stats.obj2 = NULL;
2455 }
2456
2457 return 1;
2458
2459 full:
2460 si_rx_room_blk(si);
2461 return 0;
2462}
2463
2464void dns_stats_clear_counters(int clrall, struct list *stat_modules)
2465{
Emeric Brun750fe792020-12-23 16:51:12 +01002466 struct resolvers *resolvers;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002467 struct dns_nameserver *ns;
2468 struct stats_module *mod;
2469 void *counters;
2470
2471 list_for_each_entry(mod, stat_modules, list) {
2472 if (!mod->clearable && !clrall)
2473 continue;
2474
Emeric Brun750fe792020-12-23 16:51:12 +01002475 list_for_each_entry(resolvers, &sec_resolvers, list) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002476 list_for_each_entry(ns, &resolvers->nameservers, list) {
2477 counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod);
2478 memcpy(counters, mod->counters, mod->counters_size);
2479 }
2480 }
2481 }
2482
2483}
2484
2485int dns_allocate_counters(struct list *stat_modules)
2486{
2487 struct stats_module *mod;
Emeric Brun750fe792020-12-23 16:51:12 +01002488 struct resolvers *resolvers;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002489 struct dns_nameserver *ns;
2490
Emeric Brun750fe792020-12-23 16:51:12 +01002491 list_for_each_entry(resolvers, &sec_resolvers, list) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002492 list_for_each_entry(ns, &resolvers->nameservers, list) {
2493 EXTRA_COUNTERS_REGISTER(&ns->extra_counters, COUNTERS_DNS,
2494 alloc_failed);
2495
2496 list_for_each_entry(mod, stat_modules, list) {
2497 EXTRA_COUNTERS_ADD(mod,
2498 ns->extra_counters,
2499 mod->counters,
2500 mod->counters_size);
2501 }
2502
2503 EXTRA_COUNTERS_ALLOC(ns->extra_counters, alloc_failed);
2504
2505 list_for_each_entry(mod, stat_modules, list) {
2506 memcpy(ns->extra_counters->data + mod->counters_off[ns->extra_counters->type],
2507 mod->counters, mod->counters_size);
2508
2509 /* Store the ns counters pointer */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002510 if (strcmp(mod->name, "dns") == 0) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002511 ns->counters = (struct dns_counters *)ns->extra_counters->data + mod->counters_off[COUNTERS_DNS];
2512 ns->counters->id = ns->id;
Emeric Brun50c870e2021-01-04 10:40:46 +01002513 ns->counters->pid = ns->resolvers->id;
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002514 }
2515 }
2516 }
2517 }
2518
2519 return 1;
2520
2521alloc_failed:
2522 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002523}
2524
Christopher Faulet67957bd2017-09-27 11:00:59 +02002525/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002526static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002527{
Emeric Brun750fe792020-12-23 16:51:12 +01002528 struct resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002529
Christopher Faulet67957bd2017-09-27 11:00:59 +02002530 if (*args[2]) {
Emeric Brun750fe792020-12-23 16:51:12 +01002531 list_for_each_entry(presolvers, &sec_resolvers, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002532 if (strcmp(presolvers->id, args[2]) == 0) {
2533 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002534 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002535 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002536 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002537 if (appctx->ctx.cli.p0 == NULL)
2538 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002539 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002540 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002541}
2542
Christopher Faulet67957bd2017-09-27 11:00:59 +02002543/* Dumps counters from all resolvers section and associated name servers. It
2544 * returns 0 if the output buffer is full and it needs to be called again,
2545 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002546 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002547 */
2548static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2549{
2550 struct stream_interface *si = appctx->owner;
Emeric Brun750fe792020-12-23 16:51:12 +01002551 struct resolvers *resolvers;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002552 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002553
2554 chunk_reset(&trash);
2555
2556 switch (appctx->st2) {
2557 case STAT_ST_INIT:
2558 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2559 /* fall through */
2560
2561 case STAT_ST_LIST:
Emeric Brun750fe792020-12-23 16:51:12 +01002562 if (LIST_ISEMPTY(&sec_resolvers)) {
William Lallemand69e96442016-11-19 00:58:54 +01002563 chunk_appendf(&trash, "No resolvers found\n");
2564 }
2565 else {
Emeric Brun750fe792020-12-23 16:51:12 +01002566 list_for_each_entry(resolvers, &sec_resolvers, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002567 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002568 continue;
2569
Christopher Faulet67957bd2017-09-27 11:00:59 +02002570 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2571 list_for_each_entry(ns, &resolvers->nameservers, list) {
2572 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02002573 chunk_appendf(&trash, " sent: %lld\n", ns->counters->sent);
2574 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters->snd_error);
2575 chunk_appendf(&trash, " valid: %lld\n", ns->counters->valid);
2576 chunk_appendf(&trash, " update: %lld\n", ns->counters->update);
2577 chunk_appendf(&trash, " cname: %lld\n", ns->counters->cname);
2578 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters->cname_error);
2579 chunk_appendf(&trash, " any_err: %lld\n", ns->counters->any_err);
2580 chunk_appendf(&trash, " nx: %lld\n", ns->counters->nx);
2581 chunk_appendf(&trash, " timeout: %lld\n", ns->counters->timeout);
2582 chunk_appendf(&trash, " refused: %lld\n", ns->counters->refused);
2583 chunk_appendf(&trash, " other: %lld\n", ns->counters->other);
2584 chunk_appendf(&trash, " invalid: %lld\n", ns->counters->invalid);
2585 chunk_appendf(&trash, " too_big: %lld\n", ns->counters->too_big);
2586 chunk_appendf(&trash, " truncated: %lld\n", ns->counters->truncated);
2587 chunk_appendf(&trash, " outdated: %lld\n", ns->counters->outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002588 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002589 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002590 }
2591 }
2592
2593 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002594 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002595 /* let's try again later from this session. We add ourselves into
2596 * this session's users so that it can remove us upon termination.
2597 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002598 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002599 return 0;
2600 }
William Lallemand69e96442016-11-19 00:58:54 +01002601 /* fall through */
2602
2603 default:
2604 appctx->st2 = STAT_ST_FIN;
2605 return 1;
2606 }
2607}
2608
2609/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002610static struct cli_kw_list cli_kws = {{ }, {
2611 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2612 " associated name servers",
2613 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2614 {{},}
2615 }
2616};
William Lallemand69e96442016-11-19 00:58:54 +01002617
Willy Tarreau0108d902018-11-25 19:14:37 +01002618INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002619
Baptiste Assmann333939c2019-01-21 08:34:50 +01002620/*
2621 * Prepare <rule> for hostname resolution.
2622 * Returns -1 in case of any allocation failure, 0 if not.
2623 * On error, a global failure counter is also incremented.
2624 */
2625static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2626{
2627 char *hostname_dn;
2628 int hostname_len, hostname_dn_len;
2629 struct buffer *tmp = get_trash_chunk();
2630
2631 if (!hostname)
2632 return 0;
2633
2634 hostname_len = strlen(hostname);
2635 hostname_dn = tmp->area;
Emeric Brund30e9a12020-12-23 18:49:16 +01002636 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
2637 hostname_dn, tmp->size);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002638 if (hostname_dn_len == -1)
2639 goto err;
2640
2641
Emeric Brun08622d32020-12-23 17:41:43 +01002642 stream->resolv_ctx.hostname_dn = strdup(hostname_dn);
2643 stream->resolv_ctx.hostname_dn_len = hostname_dn_len;
2644 if (!stream->resolv_ctx.hostname_dn)
Baptiste Assmann333939c2019-01-21 08:34:50 +01002645 goto err;
2646
2647 return 0;
2648
2649 err:
Emeric Brun08622d32020-12-23 17:41:43 +01002650 free(stream->resolv_ctx.hostname_dn); stream->resolv_ctx.hostname_dn = NULL;
Emeric Brund30e9a12020-12-23 18:49:16 +01002651 resolv_failed_resolutions += 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002652 return -1;
2653}
2654
2655
2656/*
2657 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2658 */
Emeric Brund30e9a12020-12-23 18:49:16 +01002659enum act_return resolv_action_do_resolve(struct act_rule *rule, struct proxy *px,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002660 struct session *sess, struct stream *s, int flags)
2661{
Emeric Brun08622d32020-12-23 17:41:43 +01002662 struct resolv_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002663 struct sample *smp;
2664 char *fqdn;
Emeric Brun08622d32020-12-23 17:41:43 +01002665 struct resolv_requester *req;
Emeric Brun750fe792020-12-23 16:51:12 +01002666 struct resolvers *resolvers;
Emeric Brun08622d32020-12-23 17:41:43 +01002667 struct resolv_resolution *res;
Christopher Faulet5098a082020-07-22 11:46:32 +02002668 int exp, locked = 0;
2669 enum act_return ret = ACT_RET_CONT;
2670
Emeric Brun21fbeed2020-12-23 18:01:04 +01002671 resolvers = rule->arg.resolv.resolvers;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002672
2673 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002674 use_cache:
Emeric Brun08622d32020-12-23 17:41:43 +01002675 if (s->resolv_ctx.requester && s->resolv_ctx.requester->resolution != NULL) {
2676 resolution = s->resolv_ctx.requester->resolution;
Christopher Faulet5098a082020-07-22 11:46:32 +02002677 if (!locked) {
2678 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2679 locked = 1;
2680 }
2681
Christopher Faulet385101e2020-07-28 10:21:54 +02002682 if (resolution->step == RSLV_STEP_RUNNING)
2683 goto yield;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002684 if (resolution->step == RSLV_STEP_NONE) {
2685 /* We update the variable only if we have a valid response. */
2686 if (resolution->status == RSLV_STATUS_VALID) {
2687 struct sample smp;
2688 short ip_sin_family = 0;
2689 void *ip = NULL;
2690
Emeric Brund30e9a12020-12-23 18:49:16 +01002691 resolv_get_ip_from_response(&resolution->response, rule->arg.resolv.opts, NULL,
Baptiste Assmann333939c2019-01-21 08:34:50 +01002692 0, &ip, &ip_sin_family, NULL);
2693
2694 switch (ip_sin_family) {
2695 case AF_INET:
2696 smp.data.type = SMP_T_IPV4;
2697 memcpy(&smp.data.u.ipv4, ip, 4);
2698 break;
2699 case AF_INET6:
2700 smp.data.type = SMP_T_IPV6;
2701 memcpy(&smp.data.u.ipv6, ip, 16);
2702 break;
2703 default:
2704 ip = NULL;
2705 }
2706
2707 if (ip) {
2708 smp.px = px;
2709 smp.sess = sess;
2710 smp.strm = s;
2711
Emeric Brun21fbeed2020-12-23 18:01:04 +01002712 vars_set_by_name(rule->arg.resolv.varname, strlen(rule->arg.resolv.varname), &smp);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002713 }
2714 }
2715 }
2716
Christopher Faulet385101e2020-07-28 10:21:54 +02002717 goto release_requester;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002718 }
2719
2720 /* need to configure and start a new DNS resolution */
Emeric Brun21fbeed2020-12-23 18:01:04 +01002721 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.resolv.expr, SMP_T_STR);
Willy Tarreau45726fd2019-07-17 10:38:45 +02002722 if (smp == NULL)
Christopher Faulet5098a082020-07-22 11:46:32 +02002723 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002724
Willy Tarreau45726fd2019-07-17 10:38:45 +02002725 fqdn = smp->data.u.str.area;
2726 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Faulet5098a082020-07-22 11:46:32 +02002727 goto end; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002728
Emeric Brun08622d32020-12-23 17:41:43 +01002729 s->resolv_ctx.parent = rule;
Christopher Faulet5098a082020-07-22 11:46:32 +02002730
2731 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2732 locked = 1;
2733
Emeric Brund30e9a12020-12-23 18:49:16 +01002734 resolv_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002735
2736 /* Check if there is a fresh enough response in the cache of our associated resolution */
Emeric Brun08622d32020-12-23 17:41:43 +01002737 req = s->resolv_ctx.requester;
Christopher Faulet385101e2020-07-28 10:21:54 +02002738 if (!req || !req->resolution)
2739 goto release_requester; /* on error, ignore the action */
Christopher Faulet5098a082020-07-22 11:46:32 +02002740 res = req->resolution;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002741
2742 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2743 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
Christopher Faulet385101e2020-07-28 10:21:54 +02002744 && !tick_is_expired(exp, now_ms)) {
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002745 goto use_cache;
2746 }
2747
Emeric Brund30e9a12020-12-23 18:49:16 +01002748 resolv_trigger_resolution(s->resolv_ctx.requester);
Christopher Faulet385101e2020-07-28 10:21:54 +02002749
2750 yield:
2751 if (flags & ACT_OPT_FINAL)
2752 goto release_requester;
Christopher Faulet5098a082020-07-22 11:46:32 +02002753 ret = ACT_RET_YIELD;
2754
2755 end:
2756 if (locked)
2757 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2758 return ret;
Christopher Faulet385101e2020-07-28 10:21:54 +02002759
2760 release_requester:
Emeric Brun08622d32020-12-23 17:41:43 +01002761 free(s->resolv_ctx.hostname_dn);
2762 s->resolv_ctx.hostname_dn = NULL;
2763 s->resolv_ctx.hostname_dn_len = 0;
2764 if (s->resolv_ctx.requester) {
Emeric Brund30e9a12020-12-23 18:49:16 +01002765 resolv_unlink_resolution(s->resolv_ctx.requester);
Emeric Brun08622d32020-12-23 17:41:43 +01002766 pool_free(resolv_requester_pool, s->resolv_ctx.requester);
2767 s->resolv_ctx.requester = NULL;
Christopher Faulet385101e2020-07-28 10:21:54 +02002768 }
2769 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002770}
2771
Emeric Brund30e9a12020-12-23 18:49:16 +01002772static void release_resolv_action(struct act_rule *rule)
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002773{
Emeric Brun21fbeed2020-12-23 18:01:04 +01002774 release_sample_expr(rule->arg.resolv.expr);
2775 free(rule->arg.resolv.varname);
2776 free(rule->arg.resolv.resolvers_id);
2777 free(rule->arg.resolv.opts);
Christopher Faulet3b2bb632020-01-24 18:12:58 +01002778}
2779
Baptiste Assmann333939c2019-01-21 08:34:50 +01002780
2781/* parse "do-resolve" action
2782 * This action takes the following arguments:
2783 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2784 *
2785 * - <varName> is the variable name where the result of the DNS resolution will be stored
2786 * (mandatory)
2787 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2788 * (mandatory)
2789 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2790 * (optional), defaults to ipv6
2791 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2792 */
Emeric Brund30e9a12020-12-23 18:49:16 +01002793enum act_parse_ret resolv_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Baptiste Assmann333939c2019-01-21 08:34:50 +01002794{
2795 int cur_arg;
2796 struct sample_expr *expr;
2797 unsigned int where;
2798 const char *beg, *end;
2799
2800 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2801 cur_arg = *orig_arg - 1;
2802
2803 /* locate varName, which is mandatory */
2804 beg = strchr(args[cur_arg], '(');
2805 if (beg == NULL)
2806 goto do_resolve_parse_error;
2807 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2808 end = strchr(beg, ',');
2809 if (end == NULL)
2810 goto do_resolve_parse_error;
Emeric Brun21fbeed2020-12-23 18:01:04 +01002811 rule->arg.resolv.varname = my_strndup(beg, end - beg);
2812 if (rule->arg.resolv.varname == NULL)
Baptiste Assmann333939c2019-01-21 08:34:50 +01002813 goto do_resolve_parse_error;
2814
2815
2816 /* locate resolversSectionName, which is mandatory.
2817 * Since next parameters are optional, the delimiter may be comma ','
2818 * or closing parenthesis ')'
2819 */
2820 beg = end + 1;
2821 end = strchr(beg, ',');
2822 if (end == NULL)
2823 end = strchr(beg, ')');
2824 if (end == NULL)
2825 goto do_resolve_parse_error;
Emeric Brun21fbeed2020-12-23 18:01:04 +01002826 rule->arg.resolv.resolvers_id = my_strndup(beg, end - beg);
2827 if (rule->arg.resolv.resolvers_id == NULL)
Baptiste Assmann333939c2019-01-21 08:34:50 +01002828 goto do_resolve_parse_error;
2829
2830
Emeric Brun21fbeed2020-12-23 18:01:04 +01002831 rule->arg.resolv.opts = calloc(1, sizeof(*rule->arg.resolv.opts));
2832 if (rule->arg.resolv.opts == NULL)
Christopher Fauleta4168432020-01-24 18:08:42 +01002833 goto do_resolve_parse_error;
2834
Baptiste Assmann333939c2019-01-21 08:34:50 +01002835 /* Default priority is ipv6 */
Emeric Brun21fbeed2020-12-23 18:01:04 +01002836 rule->arg.resolv.opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002837
2838 /* optional arguments accepted for now:
2839 * ipv4 or ipv6
2840 */
2841 while (*end != ')') {
2842 beg = end + 1;
2843 end = strchr(beg, ',');
2844 if (end == NULL)
2845 end = strchr(beg, ')');
2846 if (end == NULL)
2847 goto do_resolve_parse_error;
2848
2849 if (strncmp(beg, "ipv4", end - beg) == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002850 rule->arg.resolv.opts->family_prio = AF_INET;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002851 }
2852 else if (strncmp(beg, "ipv6", end - beg) == 0) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002853 rule->arg.resolv.opts->family_prio = AF_INET6;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002854 }
2855 else {
2856 goto do_resolve_parse_error;
2857 }
2858 }
2859
2860 cur_arg = cur_arg + 1;
2861
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002862 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 +01002863 if (!expr)
2864 goto do_resolve_parse_error;
2865
2866
2867 where = 0;
2868 if (px->cap & PR_CAP_FE)
2869 where |= SMP_VAL_FE_HRQ_HDR;
2870 if (px->cap & PR_CAP_BE)
2871 where |= SMP_VAL_BE_HRQ_HDR;
2872
2873 if (!(expr->fetch->val & where)) {
2874 memprintf(err,
2875 "fetch method '%s' extracts information from '%s', none of which is available here",
2876 args[cur_arg-1], sample_src_names(expr->fetch->use));
2877 free(expr);
2878 return ACT_RET_PRS_ERR;
2879 }
Emeric Brun21fbeed2020-12-23 18:01:04 +01002880 rule->arg.resolv.expr = expr;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002881 rule->action = ACT_CUSTOM;
Emeric Brund30e9a12020-12-23 18:49:16 +01002882 rule->action_ptr = resolv_action_do_resolve;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002883 *orig_arg = cur_arg;
2884
2885 rule->check_ptr = check_action_do_resolve;
Emeric Brund30e9a12020-12-23 18:49:16 +01002886 rule->release_ptr = release_resolv_action;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002887
2888 return ACT_RET_PRS_OK;
2889
2890 do_resolve_parse_error:
Emeric Brun21fbeed2020-12-23 18:01:04 +01002891 free(rule->arg.resolv.varname); rule->arg.resolv.varname = NULL;
2892 free(rule->arg.resolv.resolvers_id); rule->arg.resolv.resolvers_id = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002893 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2894 args[cur_arg]);
2895 return ACT_RET_PRS_ERR;
2896}
2897
2898static struct action_kw_list http_req_kws = { { }, {
Emeric Brund30e9a12020-12-23 18:49:16 +01002899 { "do-resolve", resolv_parse_do_resolve, 1 },
Baptiste Assmann333939c2019-01-21 08:34:50 +01002900 { /* END */ }
2901}};
2902
2903INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2904
2905static struct action_kw_list tcp_req_cont_actions = {ILH, {
Emeric Brund30e9a12020-12-23 18:49:16 +01002906 { "do-resolve", resolv_parse_do_resolve, 1 },
Baptiste Assmann333939c2019-01-21 08:34:50 +01002907 { /* END */ }
2908}};
2909
2910INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2911
2912/* Check an "http-request do-resolve" action.
2913 *
2914 * The function returns 1 in success case, otherwise, it returns 0 and err is
2915 * filled.
2916 */
2917int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2918{
Emeric Brun750fe792020-12-23 16:51:12 +01002919 struct resolvers *resolvers = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002920
Emeric Brun21fbeed2020-12-23 18:01:04 +01002921 if (rule->arg.resolv.resolvers_id == NULL) {
Baptiste Assmann333939c2019-01-21 08:34:50 +01002922 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2923 return 0;
2924 }
2925
Emeric Brun21fbeed2020-12-23 18:01:04 +01002926 resolvers = find_resolvers_by_id(rule->arg.resolv.resolvers_id);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002927 if (resolvers == NULL) {
Emeric Brun21fbeed2020-12-23 18:01:04 +01002928 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.resolv.resolvers_id);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002929 return 0;
2930 }
Emeric Brun21fbeed2020-12-23 18:01:04 +01002931 rule->arg.resolv.resolvers = resolvers;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002932
2933 return 1;
2934}
2935
Emeric Brund30e9a12020-12-23 18:49:16 +01002936REGISTER_POST_DEINIT(resolvers_deinit);
2937REGISTER_CONFIG_POSTPARSER("dns runtime resolver", resolvers_finalize_config);