blob: 18e64d9344fd5aaa67f1f3614be115b6c35cbebe [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
Baptiste Assmann044fd5b2018-08-10 10:56:38 +020022#include <common/cfgparse.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020023#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020025#include <common/time.h>
26#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020027#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020028
Baptiste Assmann333939c2019-01-21 08:34:50 +010029#include <types/action.h>
William Lallemand69e96442016-11-19 00:58:54 +010030#include <types/applet.h>
31#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032#include <types/global.h>
33#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010034#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035
Baptiste Assmann333939c2019-01-21 08:34:50 +010036#include <proto/action.h>
William Lallemand69e96442016-11-19 00:58:54 +010037#include <proto/channel.h>
38#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020039#include <proto/checks.h>
40#include <proto/dns.h>
41#include <proto/fd.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010042#include <proto/proto_http.h>
43#include <proto/http_rules.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020044#include <proto/log.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010045#include <proto/sample.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020046#include <proto/server.h>
47#include <proto/task.h>
48#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020049#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010050#include <proto/stream_interface.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010051#include <proto/tcp_rules.h>
52#include <proto/vars.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020053
Christopher Faulet67957bd2017-09-27 11:00:59 +020054struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
55struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020056
Tim Duesterhus7e91ef92020-01-18 02:04:12 +010057static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010058
59DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
60DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010061DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester));
Willy Tarreau8ceae722018-11-26 11:58:30 +010062
Christopher Faulet67957bd2017-09-27 11:00:59 +020063static unsigned int resolution_uuid = 1;
Baptiste Assmann333939c2019-01-21 08:34:50 +010064unsigned int dns_failed_resolutions = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065
Christopher Faulet67957bd2017-09-27 11:00:59 +020066/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
67 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020068 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020069struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020070{
Christopher Faulet67957bd2017-09-27 11:00:59 +020071 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020072
Christopher Faulet67957bd2017-09-27 11:00:59 +020073 list_for_each_entry(res, &dns_resolvers, list) {
74 if (!strcmp(res->id, id))
75 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020076 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020077 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020078}
79
Christopher Faulet67957bd2017-09-27 11:00:59 +020080/* Returns a pointer on the SRV request matching the name <name> for the proxy
81 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020082 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020083struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020084{
Christopher Faulet67957bd2017-09-27 11:00:59 +020085 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020086
Christopher Faulet67957bd2017-09-27 11:00:59 +020087 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
88 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
89 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020090 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020091 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020092}
93
Christopher Faulet67957bd2017-09-27 11:00:59 +020094/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
95 * NULL if an error occurred. */
96struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020097{
Christopher Faulet67957bd2017-09-27 11:00:59 +020098 struct proxy *px = srv->proxy;
99 struct dns_srvrq *srvrq = NULL;
100 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200101
Christopher Faulet67957bd2017-09-27 11:00:59 +0200102 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200103 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
104 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200105 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100106 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
107 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200108 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200109 }
110
Christopher Faulet67957bd2017-09-27 11:00:59 +0200111 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100112 ha_alert("config : %s '%s', server '%s': out of memory\n",
113 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200114 goto err;
115 }
116 srvrq->obj_type = OBJ_TYPE_SRVRQ;
117 srvrq->proxy = px;
118 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200119 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200120 srvrq->hostname_dn_len = hostname_dn_len;
121 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100122 ha_alert("config : %s '%s', server '%s': out of memory\n",
123 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200124 goto err;
125 }
126 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
127 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200128
Christopher Faulet67957bd2017-09-27 11:00:59 +0200129 err:
130 if (srvrq) {
131 free(srvrq->name);
132 free(srvrq->hostname_dn);
133 free(srvrq);
134 }
135 return NULL;
136}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200137
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200138
Christopher Faulet67957bd2017-09-27 11:00:59 +0200139/* 2 bytes random generator to generate DNS query ID */
140static inline uint16_t dns_rnd16(void)
141{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200142 if (!dns_query_id_seed)
143 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200144 dns_query_id_seed ^= dns_query_id_seed << 13;
145 dns_query_id_seed ^= dns_query_id_seed >> 7;
146 dns_query_id_seed ^= dns_query_id_seed << 17;
147 return dns_query_id_seed;
148}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200149
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151static inline int dns_resolution_timeout(struct dns_resolution *res)
152{
Baptiste Assmann3697f032019-11-07 11:02:18 +0100153 return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200154}
155
Christopher Faulet67957bd2017-09-27 11:00:59 +0200156/* Updates a resolvers' task timeout for next wake up and queue it */
157static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200158{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200159 struct dns_resolution *res;
160 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200161
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162 next = tick_add(now_ms, resolvers->timeout.resolve);
163 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
164 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
165 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
166 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200167
Christopher Faulet67957bd2017-09-27 11:00:59 +0200168 list_for_each_entry(res, &resolvers->resolutions.wait, list)
169 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200170
Christopher Faulet67957bd2017-09-27 11:00:59 +0200171 resolvers->t->expire = next;
172 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173}
174
Christopher Faulet67957bd2017-09-27 11:00:59 +0200175/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
176 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200177 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200178static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200179{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200180 struct dgram_conn *dgram = ns->dgram;
181 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200182
Christopher Faulet67957bd2017-09-27 11:00:59 +0200183 /* Already connected */
184 if (dgram->t.sock.fd != -1)
185 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200186
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187 /* Create an UDP socket and connect it on the nameserver's IP/Port */
188 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
189 send_log(NULL, LOG_WARNING,
190 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
191 ns->resolvers->id, ns->id);
192 return -1;
193 }
194 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
195 send_log(NULL, LOG_WARNING,
196 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
197 ns->resolvers->id, ns->id);
198 close(fd);
199 return -1;
200 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200201
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202 /* Make the socket non blocking */
203 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200204
Christopher Faulet67957bd2017-09-27 11:00:59 +0200205 /* Add the fd in the fd list and update its parameters */
206 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100207 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200208 fd_want_recv(fd);
209 return 0;
210}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200211
Christopher Faulet67957bd2017-09-27 11:00:59 +0200212/* Forges a DNS query. It needs the following information from the caller:
213 * - <query_id> : the DNS query id corresponding to this query
214 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
215 * - <hostname_dn> : hostname in domain name format
216 * - <hostname_dn_len> : length of <hostname_dn>
217 *
218 * To store the query, the caller must pass a buffer <buf> and its size
219 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
220 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200221 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200222static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
223 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200224{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200225 struct dns_header dns_hdr;
226 struct dns_question qinfo;
227 struct dns_additional_record edns;
228 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200229
Christopher Faulet67957bd2017-09-27 11:00:59 +0200230 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
231 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200232
Christopher Faulet67957bd2017-09-27 11:00:59 +0200233 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200234
Christopher Faulet67957bd2017-09-27 11:00:59 +0200235 /* Set dns query headers */
236 dns_hdr.id = (unsigned short) htons(query_id);
237 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
238 dns_hdr.qdcount = htons(1); /* 1 question */
239 dns_hdr.ancount = 0;
240 dns_hdr.nscount = 0;
241 dns_hdr.arcount = htons(1);
242 memcpy(p, &dns_hdr, sizeof(dns_hdr));
243 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200244
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 /* Set up query hostname */
246 memcpy(p, hostname_dn, hostname_dn_len);
247 p += hostname_dn_len;
248 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200249
Christopher Faulet67957bd2017-09-27 11:00:59 +0200250 /* Set up query info (type and class) */
251 qinfo.qtype = htons(query_type);
252 qinfo.qclass = htons(DNS_RCLASS_IN);
253 memcpy(p, &qinfo, sizeof(qinfo));
254 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200255
Christopher Faulet67957bd2017-09-27 11:00:59 +0200256 /* Set the DNS extension */
257 edns.name = 0;
258 edns.type = htons(DNS_RTYPE_OPT);
259 edns.udp_payload_size = htons(accepted_payload_size);
260 edns.extension = 0;
261 edns.data_length = 0;
262 memcpy(p, &edns, sizeof(edns));
263 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200264
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266}
267
Christopher Faulet67957bd2017-09-27 11:00:59 +0200268/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
269 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200270 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200271static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200272{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200273 struct dns_resolvers *resolvers = resolution->resolvers;
274 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200275
Christopher Faulet67957bd2017-09-27 11:00:59 +0200276 list_for_each_entry(ns, &resolvers->nameservers, list) {
277 int fd = ns->dgram->t.sock.fd;
278 if (fd == -1) {
279 if (dns_connect_namesaver(ns) == -1)
280 continue;
281 fd = ns->dgram->t.sock.fd;
282 resolvers->nb_nameservers++;
283 }
284 fd_want_send(fd);
285 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200286
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287 /* Update resolution */
288 resolution->nb_queries = 0;
289 resolution->nb_responses = 0;
290 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200291
Christopher Faulet67957bd2017-09-27 11:00:59 +0200292 /* Push the resolution at the end of the active list */
293 LIST_DEL(&resolution->list);
294 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
295 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200296}
297
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
299 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200300 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200301static int
302dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200303{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200304 struct dns_resolvers *resolvers = resolution->resolvers;
305 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307 /* Avoid sending requests for resolutions that don't yet have an
308 * hostname, ie resolutions linked to servers that do not yet have an
309 * fqdn */
310 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200311 return 0;
312
Christopher Faulet67957bd2017-09-27 11:00:59 +0200313 /* Check if a resolution has already been started for this server return
314 * directly to avoid resolution pill up. */
315 if (resolution->step != RSLV_STEP_NONE)
316 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200317
Christopher Faulet67957bd2017-09-27 11:00:59 +0200318 /* Generates a new query id. We try at most 100 times to find a free
319 * query id */
320 for (i = 0; i < 100; ++i) {
321 query_id = dns_rnd16();
322 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200323 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200324 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200325 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200326 if (query_id == -1) {
327 send_log(NULL, LOG_NOTICE,
328 "could not generate a query id for %s, in resolvers %s.\n",
329 resolution->hostname_dn, resolvers->id);
330 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200331 }
332
Christopher Faulet67957bd2017-09-27 11:00:59 +0200333 /* Update resolution parameters */
334 resolution->query_id = query_id;
335 resolution->qid.key = query_id;
336 resolution->step = RSLV_STEP_RUNNING;
337 resolution->query_type = resolution->prefered_query_type;
338 resolution->try = resolvers->resolve_retries;
339 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200340
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 /* Send the DNS query */
342 resolution->try -= 1;
343 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200344 return 1;
345}
346
Christopher Faulet67957bd2017-09-27 11:00:59 +0200347/* Performs a name resolution for the requester <req> */
348void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200349{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200350 struct dns_resolvers *resolvers;
351 struct dns_resolution *res;
352 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200353
Christopher Faulet67957bd2017-09-27 11:00:59 +0200354 if (!req || !req->resolution)
355 return;
356 res = req->resolution;
357 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200358
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359 /* The resolution must not be triggered yet. Use the cached response, if
360 * valid */
361 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200362 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
363 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
364 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200365}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200366
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200367
Christopher Faulet67957bd2017-09-27 11:00:59 +0200368/* Resets some resolution parameters to initial values and also delete the query
369 * ID from the resolver's tree.
370 */
371static void dns_reset_resolution(struct dns_resolution *resolution)
372{
373 /* update resolution status */
374 resolution->step = RSLV_STEP_NONE;
375 resolution->try = 0;
376 resolution->last_resolution = now_ms;
377 resolution->nb_queries = 0;
378 resolution->nb_responses = 0;
379 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200380
Christopher Faulet67957bd2017-09-27 11:00:59 +0200381 /* clean up query id */
382 eb32_delete(&resolution->qid);
383 resolution->query_id = 0;
384 resolution->qid.key = 0;
385}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200386
Christopher Faulet67957bd2017-09-27 11:00:59 +0200387/* Returns the query id contained in a DNS response */
388static inline unsigned short dns_response_get_query_id(unsigned char *resp)
389{
390 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200391}
392
Christopher Faulet67957bd2017-09-27 11:00:59 +0200393
394/* Analyses, re-builds and copies the name <name> from the DNS response packet
395 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
396 * for compressed data. The result is copied into <dest>, ensuring we don't
397 * overflow using <dest_len> Returns the number of bytes the caller can move
398 * forward. If 0 it means an error occurred while parsing the name. <offset> is
399 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200400 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200401int dns_read_name(unsigned char *buffer, unsigned char *bufend,
402 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100403 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200404{
405 int nb_bytes = 0, n = 0;
406 int label_len;
407 unsigned char *reader = name;
408 char *dest = destination;
409
410 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100411 if (reader >= bufend)
412 goto err;
413
Christopher Faulet67957bd2017-09-27 11:00:59 +0200414 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200415 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100416 if (reader + 1 >= bufend)
417 goto err;
418
Christopher Faulet67957bd2017-09-27 11:00:59 +0200419 /* Must point BEFORE current position */
420 if ((buffer + reader[1]) > reader)
421 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200422
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100423 if (depth++ > 100)
424 goto err;
425
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530426 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100427 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200428 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200429 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200430
Christopher Faulet67957bd2017-09-27 11:00:59 +0200431 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200432 nb_bytes += n;
433 goto out;
434 }
435
436 label_len = *reader;
437 if (label_len == 0)
438 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200440 /* Check if:
441 * - we won't read outside the buffer
442 * - there is enough place in the destination
443 */
444 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200445 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200446
447 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200448 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200449
450 memcpy(dest, reader, label_len);
451
Christopher Faulet67957bd2017-09-27 11:00:59 +0200452 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200453 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200454 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200455 }
456
Christopher Faulet67957bd2017-09-27 11:00:59 +0200457 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200458 /* offset computation:
459 * parse from <name> until finding either NULL or a pointer "c0xx"
460 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200461 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200462 *offset = 0;
463 while (reader < bufend) {
464 if ((reader[0] & 0xc0) == 0xc0) {
465 *offset += 2;
466 break;
467 }
468 else if (*reader == 0) {
469 *offset += 1;
470 break;
471 }
472 *offset += 1;
473 ++reader;
474 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200475 return nb_bytes;
476
Christopher Faulet67957bd2017-09-27 11:00:59 +0200477 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200478 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200479}
480
Christopher Faulet67957bd2017-09-27 11:00:59 +0200481/* Checks for any obsolete record, also identify any SRV request, and try to
482 * find a corresponding server.
483*/
484static void dns_check_dns_response(struct dns_resolution *res)
485{
486 struct dns_resolvers *resolvers = res->resolvers;
487 struct dns_requester *req, *reqback;
488 struct dns_answer_item *item, *itemback;
489 struct server *srv;
490 struct dns_srvrq *srvrq;
491
492 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
493
494 /* Remove obsolete items */
495 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
496 if (item->type != DNS_RTYPE_SRV)
497 goto rm_obselete_item;
498
499 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
500 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
501 continue;
502
503 /* Remove any associated server */
504 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100505 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200506 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
507 item->data_len == srv->hostname_dn_len &&
508 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
509 snr_update_srv_status(srv, 1);
510 free(srv->hostname);
511 free(srv->hostname_dn);
512 srv->hostname = NULL;
513 srv->hostname_dn = NULL;
514 srv->hostname_dn_len = 0;
515 dns_unlink_resolution(srv->dns_requester);
516 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100517 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200518 }
519 }
520
521 rm_obselete_item:
522 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100523 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200524 continue;
525 }
526
527 if (item->type != DNS_RTYPE_SRV)
528 continue;
529
530 /* Now process SRV records */
531 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
532 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
533 continue;
534
535 /* Check if a server already uses that hostname */
536 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100537 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200538 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
539 item->data_len == srv->hostname_dn_len &&
540 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100541 int ha_weight;
542
Baptiste Assmannba0244b2019-10-21 15:13:48 +0200543 /* DNS weight range if from 0 to 65535
544 * HAProxy weight is from 0 to 256
545 * The rule below ensures that weight 0 is well respected
546 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100547 */
Baptiste Assmannba0244b2019-10-21 15:13:48 +0200548 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100549 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200550 char weight[9];
551
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100552 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200553 server_parse_weight_change_request(srv, weight);
554 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100555 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200556 break;
557 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100558 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200559 }
560 if (srv)
561 continue;
562
563 /* If not, try to find a server with undefined hostname */
564 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100565 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200566 if (srv->srvrq == srvrq && !srv->hostname_dn)
567 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100568 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200569 }
570 /* And update this server, if found */
571 if (srv) {
572 const char *msg = NULL;
573 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100574 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200575 char hostname[DNS_MAX_NAME_SIZE];
576
577 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100578 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100579 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200580 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100581 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100582 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200583 if (msg)
584 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
585
586 srv->svc_port = item->port;
587 srv->flags &= ~SRV_F_MAPPORTS;
588 if ((srv->check.state & CHK_ST_CONFIGURED) &&
589 !(srv->flags & SRV_F_CHECKPORT))
590 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100591
Baptiste Assmannba0244b2019-10-21 15:13:48 +0200592 /* DNS weight range if from 0 to 65535
593 * HAProxy weight is from 0 to 256
594 * The rule below ensures that weight 0 is well respected
595 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100596 */
Baptiste Assmannba0244b2019-10-21 15:13:48 +0200597 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100598
599 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200600 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100601 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200602 }
603 }
604 }
605}
606
607/* Validates that the buffer DNS response provided in <resp> and finishing
608 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200609 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200610 * The result is stored in <resolution>' response, buf_response,
611 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200612 *
613 * This function returns one of the DNS_RESP_* code to indicate the type of
614 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200615 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200616static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
617 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200619 unsigned char *reader;
620 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200621 int len, flags, offset;
622 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200623 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200624 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200625 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200626 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200627 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200628
Christopher Faulet67957bd2017-09-27 11:00:59 +0200629 reader = resp;
630 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200631 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200632 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200633
Christopher Faulet67957bd2017-09-27 11:00:59 +0200634 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200635 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200636
637 /* query id */
638 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200639 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200640 dns_p->header.id = reader[0] * 256 + reader[1];
641 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200642
Christopher Faulet67957bd2017-09-27 11:00:59 +0200643 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200644 * First byte contains:
645 * - response flag (1 bit)
646 * - opcode (4 bits)
647 * - authoritative (1 bit)
648 * - truncated (1 bit)
649 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200650 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200651 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200652 return DNS_RESP_INVALID;
653
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200654 flags = reader[0] * 256 + reader[1];
655
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200656 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
657 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200658 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200659 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200660 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200661 return DNS_RESP_ERROR;
662 }
663
Christopher Faulet67957bd2017-09-27 11:00:59 +0200664 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200665 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200666
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200667 /* 2 bytes for question count */
668 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200669 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200670 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200671 /* (for now) we send one query only, so we expect only one in the
672 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200673 if (dns_p->header.qdcount != 1)
674 return DNS_RESP_QUERY_COUNT_ERROR;
675 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200676 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200677 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200678
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 /* 2 bytes for answer count */
680 if (reader + 2 >= bufend)
681 return DNS_RESP_INVALID;
682 dns_p->header.ancount = reader[0] * 256 + reader[1];
683 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200684 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200685 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200686 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200687 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200688 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200689
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200690 /* 2 bytes authority count */
691 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200692 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200693 dns_p->header.nscount = reader[0] * 256 + reader[1];
694 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200695
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696 /* 2 bytes additional count */
697 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200698 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200699 dns_p->header.arcount = reader[0] * 256 + reader[1];
700 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200701
Christopher Faulet67957bd2017-09-27 11:00:59 +0200702 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200703 LIST_INIT(&dns_p->query_list);
704 for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200705 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200706 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200707 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200708 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
709 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200710 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200711 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200712
Christopher Faulet67957bd2017-09-27 11:00:59 +0200713 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200714 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200715 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200716 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100717 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200718
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200719 if (len == 0)
720 return DNS_RESP_INVALID;
721
722 reader += offset;
723 previous_dname = dns_query->name;
724
725 /* move forward 2 bytes for question type */
726 if (reader + 2 >= bufend)
727 return DNS_RESP_INVALID;
728 dns_query->type = reader[0] * 256 + reader[1];
729 reader += 2;
730
731 /* move forward 2 bytes for question class */
732 if (reader + 2 >= bufend)
733 return DNS_RESP_INVALID;
734 dns_query->class = reader[0] * 256 + reader[1];
735 reader += 2;
736 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200737
Baptiste Assmann251abb92017-08-11 09:58:27 +0200738 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200739 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200740 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
741 return DNS_RESP_TRUNCATED;
742
Baptiste Assmann325137d2015-04-13 23:40:55 +0200743 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200744 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200745 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200746 if (reader >= bufend)
747 return DNS_RESP_INVALID;
748
Willy Tarreaubafbe012017-11-24 17:34:44 +0100749 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200750 if (dns_answer_record == NULL)
751 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100754 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200755
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200756 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100757 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200759 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200760
Christopher Faulet67957bd2017-09-27 11:00:59 +0200761 /* Check if the current record dname is valid. previous_dname
762 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200763 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100764 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200765 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200766 /* First record, means a mismatch issue between
767 * queried dname and dname found in the first
768 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200769 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200770 }
771 else {
772 /* If not the first record, this means we have a
773 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200774 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200775 }
776
Baptiste Assmann325137d2015-04-13 23:40:55 +0200777 }
778
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200779 memcpy(dns_answer_record->name, tmpname, len);
780 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200781
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200782 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200783 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100784 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200785 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200786 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200787
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200788 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200789 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100790 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200791 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200792 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200793 dns_answer_record->type = reader[0] * 256 + reader[1];
794 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200795
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200796 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200797 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100798 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200799 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200800 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200801 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200802 reader += 2;
803
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200805 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100806 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200808 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200809 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
810 + reader[2] * 256 + reader[3];
811 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200812
Christopher Faulet67957bd2017-09-27 11:00:59 +0200813 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200814 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100815 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200816 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200817 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200818 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200819
Christopher Faulet67957bd2017-09-27 11:00:59 +0200820 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200821 reader += 2;
822
Jérôme Magnin8d4e7dc2018-12-20 16:47:31 +0100823 if (reader + dns_answer_record->data_len > bufend) {
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100824 pool_free(dns_answer_item_pool, dns_answer_record);
825 return DNS_RESP_INVALID;
826 }
827
Christopher Faulet67957bd2017-09-27 11:00:59 +0200828 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200829 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200830 case DNS_RTYPE_A:
831 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200832 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100833 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200834 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200835 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200836 dns_answer_record->address.sa_family = AF_INET;
837 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
838 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200839 break;
840
841 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200842 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200843 * no IP could be found and last record was a CNAME. Could be triggered
844 * by a wrong query type
845 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200846 * + 1 because dns_answer_record_id starts at 0
847 * while number of answers is an integer and
848 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200849 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200850 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100851 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200852 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200853 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200854
855 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100856 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200857 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100858 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200859 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200860 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200861
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200862 memcpy(dns_answer_record->target, tmpname, len);
863 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200864 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200865 break;
866
Olivier Houchard8da5f982017-08-04 18:35:36 +0200867
868 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200869 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200870 * - 2 bytes for the priority
871 * - 2 bytes for the weight
872 * - 2 bytes for the port
873 * - the target hostname
874 */
875 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100876 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200877 return DNS_RESP_INVALID;
878 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200879 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200880 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200881 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200882 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200883 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200884 reader += sizeof(uint16_t);
885 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100886 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200887 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100888 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200889 return DNS_RESP_INVALID;
890 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200891 dns_answer_record->data_len = len;
892 memcpy(dns_answer_record->target, tmpname, len);
893 dns_answer_record->target[len] = 0;
894 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200895
Baptiste Assmann325137d2015-04-13 23:40:55 +0200896 case DNS_RTYPE_AAAA:
897 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200898 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100899 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200900 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200901 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200902 dns_answer_record->address.sa_family = AF_INET6;
903 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
904 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200905 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200906
Baptiste Assmann325137d2015-04-13 23:40:55 +0200907 } /* switch (record type) */
908
Christopher Faulet67957bd2017-09-27 11:00:59 +0200909 /* Increment the counter for number of records saved into our
910 * local response */
911 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200912
Christopher Faulet67957bd2017-09-27 11:00:59 +0200913 /* Move forward dns_answer_record->data_len for analyzing next
914 * record in the response */
915 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
916 ? offset
917 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200918
919 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200920 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200921 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
922 if (tmp_record->type != dns_answer_record->type)
923 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200924
925 switch(tmp_record->type) {
926 case DNS_RTYPE_A:
927 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
928 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
929 sizeof(in_addr_t)))
930 found = 1;
931 break;
932
933 case DNS_RTYPE_AAAA:
934 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
935 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
936 sizeof(struct in6_addr)))
937 found = 1;
938 break;
939
Olivier Houchard8da5f982017-08-04 18:35:36 +0200940 case DNS_RTYPE_SRV:
941 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200942 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200943 dns_answer_record->port == tmp_record->port) {
944 tmp_record->weight = dns_answer_record->weight;
945 found = 1;
946 }
947 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200948
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200949 default:
950 break;
951 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200952
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200953 if (found == 1)
954 break;
955 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200956
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200957 if (found == 1) {
958 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100959 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200960 }
961 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200962 dns_answer_record->last_seen = now.tv_sec;
963 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
964 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200965 } /* for i 0 to ancount */
966
Christopher Faulet67957bd2017-09-27 11:00:59 +0200967 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200968 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200969 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200970 return DNS_RESP_VALID;
971}
972
Christopher Faulet67957bd2017-09-27 11:00:59 +0200973/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200974 * If existing IP not found, return the first IP matching family_priority,
975 * otherwise, first ip found
976 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200977 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200978 * For both cases above, dns_validate_dns_response is required
979 * returns one of the DNS_UPD_* code
980 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200981int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200982 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100983 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200984 void **newip, short *newip_sin_family,
985 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200986{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200987 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100988 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200989 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200990 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100991 int currentip_sel;
992 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100993 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200994 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200995
Christopher Faulet67957bd2017-09-27 11:00:59 +0200996 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200997 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200998 *newip = newip4 = newip6 = NULL;
999 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001000 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001001 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001002
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001003 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001004 * Top priority is the preferred network ip version,
1005 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001006 * the last priority is the currently used IP,
1007 *
1008 * For these three priorities, a score is calculated. The
1009 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001010 * 8 - preferred ip version.
1011 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001012 * 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 +01001013 * 1 - current ip.
1014 * The result with the biggest score is returned.
1015 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001016
1017 list_for_each_entry(record, &dns_p->answer_list, list) {
1018 void *ip;
1019 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001020
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001021 if (record->type == DNS_RTYPE_A) {
1022 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1023 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001024 }
1025 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001026 ip_type = AF_INET6;
1027 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001028 }
1029 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001030 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001031 score = 0;
1032
Joseph Herlant42cf6392018-11-15 10:33:28 -08001033 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001034 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001035 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001036
Joseph Herlant42cf6392018-11-15 10:33:28 -08001037 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001038 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001039
1040 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001041 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001042 continue;
1043
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001044 if ((ip_type == AF_INET &&
1045 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001046 &dns_opts->pref_net[j].mask.in4,
1047 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001048 (ip_type == AF_INET6 &&
1049 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001050 &dns_opts->pref_net[j].mask.in6,
1051 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001052 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001053 break;
1054 }
1055 }
1056
Christopher Faulet67957bd2017-09-27 11:00:59 +02001057 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001058 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001059 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001060 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001061 if (!allowed_duplicated_ip) {
1062 continue;
1063 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001064 } else {
1065 score += 2;
1066 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001067
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001068 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001069 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001070 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001071 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001072 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001073 !memcmp(ip, currentip, 16)))) {
1074 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001075 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001076 }
1077 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001078 currentip_sel = 0;
1079
1080 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001081 * score. The maximum score is 15, if this value is reached, we
1082 * break the parsing. Implicitly, this score is reached the ip
1083 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001084 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001085 if (ip_type == AF_INET)
1086 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001087 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001088 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001089 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001090 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001091 return DNS_UPD_NO;
1092 max_score = score;
1093 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001094 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001095
Christopher Faulet67957bd2017-09-27 11:00:59 +02001096 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001097 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001098 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001099
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001101 if (family_priority == AF_INET) {
1102 if (newip4) {
1103 *newip = newip4;
1104 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001105 }
1106 else if (newip6) {
1107 *newip = newip6;
1108 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001110 if (!currentip_found)
1111 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001113 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001114 else if (family_priority == AF_INET6) {
1115 if (newip6) {
1116 *newip = newip6;
1117 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001118 }
1119 else if (newip4) {
1120 *newip = newip4;
1121 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001122 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001123 if (!currentip_found)
1124 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001125 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001126 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001127 else if (family_priority == AF_UNSPEC) {
1128 if (newip6) {
1129 *newip = newip6;
1130 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001131 }
1132 else if (newip4) {
1133 *newip = newip4;
1134 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001136 if (!currentip_found)
1137 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001138 }
1139
Christopher Faulet67957bd2017-09-27 11:00:59 +02001140 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001141 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001142
Christopher Faulet67957bd2017-09-27 11:00:59 +02001143 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001144 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001145 /* Move the first record to the end of the list, for internal
1146 * round robin */
1147 LIST_DEL(&record->list);
1148 LIST_ADDQ(&dns_p->answer_list, &record->list);
1149 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001150 }
1151 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001152}
1153
Christopher Faulet67957bd2017-09-27 11:00:59 +02001154/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001155 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001156 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1157 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001158 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001159 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1160 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001161 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001162int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001163{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001164 char *ptr;
1165 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001166
Christopher Faulet67957bd2017-09-27 11:00:59 +02001167 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001168 return -1;
1169
Christopher Faulet67957bd2017-09-27 11:00:59 +02001170 ptr = str;
1171 for (i = 0; i < dn_len-1; ++i) {
1172 sz = dn[i];
1173 if (i)
1174 *ptr++ = '.';
1175 memcpy(ptr, dn+i+1, sz);
1176 ptr += sz;
1177 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001178 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001179 *ptr++ = '\0';
1180 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001181}
1182
Christopher Faulet67957bd2017-09-27 11:00:59 +02001183/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1184 *
1185 * <str> must be a null-terminated string. <str_len> must include the
1186 * terminating null byte. <dn> buffer must be allocated and its size must be
1187 * passed in <dn_len>.
1188 *
1189 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1190 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001191 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001192int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001193{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001194 int i, offset;
1195
Christopher Faulet67957bd2017-09-27 11:00:59 +02001196 if (dn_len < str_len + 1)
1197 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001198
Christopher Faulet67957bd2017-09-27 11:00:59 +02001199 /* First byte of dn will be used to store the length of the first
1200 * label */
1201 offset = 0;
1202 for (i = 0; i < str_len; ++i) {
1203 if (str[i] == '.') {
1204 /* 2 or more consecutive dots is invalid */
1205 if (i == offset)
1206 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001207
Lukas Tribus76d4ff02020-02-27 15:47:24 +01001208 /* ignore trailing dot */
1209 if (i + 2 == str_len) {
1210 i++;
1211 break;
1212 }
1213
Christopher Faulet67957bd2017-09-27 11:00:59 +02001214 dn[offset] = (i - offset);
1215 offset = i+1;
1216 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001217 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001218 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001219 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001220 dn[offset] = (i - offset - 1);
1221 dn[i] = '\0';
1222 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001223}
1224
Christopher Faulet67957bd2017-09-27 11:00:59 +02001225/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001226 * - total size
1227 * - each label size individually
1228 * returns:
1229 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1230 * 1 when no error. <err> is left unaffected.
1231 */
1232int dns_hostname_validation(const char *string, char **err)
1233{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001234 int i;
1235
1236 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1237 if (err)
1238 *err = DNS_TOO_LONG_FQDN;
1239 return 0;
1240 }
1241
William Dauchy5d2ca062020-01-26 19:52:34 +01001242 while (*string) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001243 i = 0;
William Dauchy5d2ca062020-01-26 19:52:34 +01001244 while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) {
1245 if (!(*string == '-' || *string == '_' ||
1246 (*string >= 'a' && *string <= 'z') ||
1247 (*string >= 'A' && *string <= 'Z') ||
1248 (*string >= '0' && *string <= '9'))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001249 if (err)
1250 *err = DNS_INVALID_CHARACTER;
1251 return 0;
1252 }
William Dauchy5d2ca062020-01-26 19:52:34 +01001253 i++;
1254 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001255 }
1256
William Dauchy5d2ca062020-01-26 19:52:34 +01001257 if (!(*string))
1258 break;
1259
1260 if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001261 if (err)
1262 *err = DNS_LABEL_TOO_LONG;
1263 return 0;
1264 }
1265
William Dauchy5d2ca062020-01-26 19:52:34 +01001266 string++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001267 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001268 return 1;
1269}
1270
Christopher Faulet67957bd2017-09-27 11:00:59 +02001271/* Picks up an available resolution from the different resolution list
1272 * associated to a resolvers section, in this order:
1273 * 1. check in resolutions.curr for the same hostname and query_type
1274 * 2. check in resolutions.wait for the same hostname and query_type
1275 * 3. Get a new resolution from resolution pool
1276 *
1277 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001278 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001279static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1280 char **hostname_dn, int hostname_dn_len,
1281 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001282{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001283 struct dns_resolution *res;
1284
1285 if (!*hostname_dn)
1286 goto from_pool;
1287
1288 /* Search for same hostname and query type in resolutions.curr */
1289 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1290 if (!res->hostname_dn)
1291 continue;
1292 if ((query_type == res->prefered_query_type) &&
1293 hostname_dn_len == res->hostname_dn_len &&
1294 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1295 return res;
1296 }
1297
1298 /* Search for same hostname and query type in resolutions.wait */
1299 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1300 if (!res->hostname_dn)
1301 continue;
1302 if ((query_type == res->prefered_query_type) &&
1303 hostname_dn_len == res->hostname_dn_len &&
1304 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1305 return res;
1306 }
1307
1308 from_pool:
1309 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001310 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001311 if (res) {
1312 memset(res, 0, sizeof(*res));
1313 res->resolvers = resolvers;
1314 res->uuid = resolution_uuid;
1315 res->status = RSLV_STATUS_NONE;
1316 res->step = RSLV_STEP_NONE;
1317 res->last_valid = now_ms;
1318
1319 LIST_INIT(&res->requesters);
1320 LIST_INIT(&res->response.answer_list);
1321
1322 res->prefered_query_type = query_type;
1323 res->query_type = query_type;
1324 res->hostname_dn = *hostname_dn;
1325 res->hostname_dn_len = hostname_dn_len;
1326
1327 ++resolution_uuid;
1328
1329 /* Move the resolution to the resolvers wait queue */
1330 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1331 }
1332 return res;
1333}
1334
1335/* Releases a resolution from its requester(s) and move it back to the pool */
1336static void dns_free_resolution(struct dns_resolution *resolution)
1337{
1338 struct dns_requester *req, *reqback;
Christopher Faulet39eb7662020-07-22 15:55:49 +02001339 struct dns_answer_item *item, *itemback;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001340
1341 /* clean up configuration */
1342 dns_reset_resolution(resolution);
1343 resolution->hostname_dn = NULL;
1344 resolution->hostname_dn_len = 0;
1345
1346 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1347 LIST_DEL(&req->list);
1348 req->resolution = NULL;
1349 }
1350
Christopher Faulet39eb7662020-07-22 15:55:49 +02001351 list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) {
1352 LIST_DEL(&item->list);
1353 pool_free(dns_answer_item_pool, item);
1354 }
1355
Christopher Faulet67957bd2017-09-27 11:00:59 +02001356 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001357 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001358}
1359
1360/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1361 * on success, -1 otherwise.
1362 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001363int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001364{
1365 struct dns_resolution *res = NULL;
1366 struct dns_requester *req;
1367 struct dns_resolvers *resolvers;
1368 struct server *srv = NULL;
1369 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001370 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001371 char **hostname_dn;
1372 int hostname_dn_len, query_type;
1373
1374 switch (requester_type) {
1375 case OBJ_TYPE_SERVER:
1376 srv = (struct server *)requester;
1377 hostname_dn = &srv->hostname_dn;
1378 hostname_dn_len = srv->hostname_dn_len;
1379 resolvers = srv->resolvers;
1380 query_type = ((srv->dns_opts.family_prio == AF_INET)
1381 ? DNS_RTYPE_A
1382 : DNS_RTYPE_AAAA);
1383 break;
1384
1385 case OBJ_TYPE_SRVRQ:
1386 srvrq = (struct dns_srvrq *)requester;
1387 hostname_dn = &srvrq->hostname_dn;
1388 hostname_dn_len = srvrq->hostname_dn_len;
1389 resolvers = srvrq->resolvers;
1390 query_type = DNS_RTYPE_SRV;
1391 break;
1392
Baptiste Assmann333939c2019-01-21 08:34:50 +01001393 case OBJ_TYPE_STREAM:
1394 stream = (struct stream *)requester;
1395 hostname_dn = &stream->dns_ctx.hostname_dn;
1396 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1397 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
1398 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts.family_prio == AF_INET)
1399 ? DNS_RTYPE_A
1400 : DNS_RTYPE_AAAA);
1401 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001402 default:
1403 goto err;
1404 }
1405
1406 /* Get a resolution from the resolvers' wait queue or pool */
1407 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1408 goto err;
1409
1410 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001411 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001412 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001413 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001414 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001415 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001416 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001417 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001418 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001419 req->owner = &srv->obj_type;
1420 srv->dns_requester = req;
1421 }
1422 else
1423 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001424 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001425 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001426
1427 req->requester_cb = snr_resolution_cb;
1428 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001429 }
1430 else if (srvrq) {
1431 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001432 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001433 goto err;
1434 req->owner = &srvrq->obj_type;
1435 srvrq->dns_requester = req;
1436 }
1437 else
1438 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001439
1440 req->requester_cb = snr_resolution_cb;
1441 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001442 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001443 else if (stream) {
1444 if (stream->dns_ctx.dns_requester == NULL) {
1445 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1446 goto err;
1447 req->owner = &stream->obj_type;
1448 stream->dns_ctx.dns_requester = req;
1449 }
1450 else
1451 req = stream->dns_ctx.dns_requester;
1452
1453 req->requester_cb = act_resolution_cb;
1454 req->requester_error_cb = act_resolution_error_cb;
1455 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001456 else
1457 goto err;
1458
1459 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001460
1461 LIST_ADDQ(&res->requesters, &req->list);
1462 return 0;
1463
1464 err:
1465 if (res && LIST_ISEMPTY(&res->requesters))
1466 dns_free_resolution(res);
1467 return -1;
1468}
1469
1470/* Removes a requester from a DNS resoltion. It takes takes care of all the
1471 * consequences. It also cleans up some parameters from the requester.
1472 */
1473void dns_unlink_resolution(struct dns_requester *requester)
1474{
1475 struct dns_resolution *res;
1476 struct dns_requester *req;
1477
1478 /* Nothing to do */
1479 if (!requester || !requester->resolution)
1480 return;
1481 res = requester->resolution;
1482
1483 /* Clean up the requester */
1484 LIST_DEL(&requester->list);
1485 requester->resolution = NULL;
1486
1487 /* We need to find another requester linked on this resolution */
1488 if (!LIST_ISEMPTY(&res->requesters))
1489 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1490 else {
1491 dns_free_resolution(res);
1492 return;
1493 }
1494
1495 /* Move hostname_dn related pointers to the next requester */
1496 switch (obj_type(req->owner)) {
1497 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001498 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1499 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001500 break;
1501 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001502 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1503 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001504 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001505 case OBJ_TYPE_STREAM:
1506 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1507 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1508 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001509 default:
1510 res->hostname_dn = NULL;
1511 res->hostname_dn_len = 0;
1512 break;
1513 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001514}
1515
Christopher Faulet67957bd2017-09-27 11:00:59 +02001516/* Called when a network IO is generated on a name server socket for an incoming
1517 * packet. It performs the following actions:
1518 * - check if the packet requires processing (not outdated resolution)
1519 * - ensure the DNS packet received is valid and call requester's callback
1520 * - call requester's error callback if invalid response
1521 * - check the dn_name in the packet against the one sent
1522 */
1523static void dns_resolve_recv(struct dgram_conn *dgram)
1524{
1525 struct dns_nameserver *ns, *tmpns;
1526 struct dns_resolvers *resolvers;
1527 struct dns_resolution *res;
1528 struct dns_query_item *query;
1529 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1530 unsigned char *bufend;
1531 int fd, buflen, dns_resp;
1532 int max_answer_records;
1533 unsigned short query_id;
1534 struct eb32_node *eb;
1535 struct dns_requester *req;
1536
1537 fd = dgram->t.sock.fd;
1538
1539 /* check if ready for reading */
1540 if (!fd_recv_ready(fd))
1541 return;
1542
1543 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1899dbf2019-12-10 18:38:09 +01001544 if ((ns = dgram->owner) == NULL) {
1545 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1546 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001547 return;
Willy Tarreau1899dbf2019-12-10 18:38:09 +01001548 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001549
1550 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001551 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001552
1553 /* process all pending input messages */
Willy Tarreau1899dbf2019-12-10 18:38:09 +01001554 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001555 /* read message received */
1556 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1557 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1899dbf2019-12-10 18:38:09 +01001558 /* FIXME : for now we consider EAGAIN only, but at
1559 * least we purge sticky errors that would cause us to
1560 * be called in loops.
1561 */
1562 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001563 fd_cant_recv(fd);
1564 break;
1565 }
1566
1567 /* message too big */
1568 if (buflen > resolvers->accepted_payload_size) {
1569 ns->counters.too_big++;
1570 continue;
1571 }
1572
1573 /* initializing variables */
1574 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1575
1576 /* read the query id from the packet (16 bits) */
1577 if (buf + 2 > bufend) {
1578 ns->counters.invalid++;
1579 continue;
1580 }
1581 query_id = dns_response_get_query_id(buf);
1582
1583 /* search the query_id in the pending resolution tree */
1584 eb = eb32_lookup(&resolvers->query_ids, query_id);
1585 if (eb == NULL) {
1586 /* unknown query id means an outdated response and can be safely ignored */
1587 ns->counters.outdated++;
1588 continue;
1589 }
1590
1591 /* known query id means a resolution in prgress */
1592 res = eb32_entry(eb, struct dns_resolution, qid);
1593 if (!res) {
1594 ns->counters.outdated++;
1595 continue;
1596 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001597
Christopher Faulet67957bd2017-09-27 11:00:59 +02001598 /* number of responses received */
1599 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001600
Christopher Faulet67957bd2017-09-27 11:00:59 +02001601 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1602 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001603
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604 switch (dns_resp) {
1605 case DNS_RESP_VALID:
1606 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001607
Christopher Faulet67957bd2017-09-27 11:00:59 +02001608 case DNS_RESP_INVALID:
1609 case DNS_RESP_QUERY_COUNT_ERROR:
1610 case DNS_RESP_WRONG_NAME:
1611 res->status = RSLV_STATUS_INVALID;
1612 ns->counters.invalid++;
1613 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001614
Christopher Faulet67957bd2017-09-27 11:00:59 +02001615 case DNS_RESP_NX_DOMAIN:
1616 res->status = RSLV_STATUS_NX;
1617 ns->counters.nx++;
1618 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001619
Christopher Faulet67957bd2017-09-27 11:00:59 +02001620 case DNS_RESP_REFUSED:
1621 res->status = RSLV_STATUS_REFUSED;
1622 ns->counters.refused++;
1623 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001624
Christopher Faulet67957bd2017-09-27 11:00:59 +02001625 case DNS_RESP_ANCOUNT_ZERO:
1626 res->status = RSLV_STATUS_OTHER;
1627 ns->counters.any_err++;
1628 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001629
Christopher Faulet67957bd2017-09-27 11:00:59 +02001630 case DNS_RESP_CNAME_ERROR:
1631 res->status = RSLV_STATUS_OTHER;
1632 ns->counters.cname_error++;
1633 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001634
Christopher Faulet67957bd2017-09-27 11:00:59 +02001635 case DNS_RESP_TRUNCATED:
1636 res->status = RSLV_STATUS_OTHER;
1637 ns->counters.truncated++;
1638 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001639
Christopher Faulet67957bd2017-09-27 11:00:59 +02001640 case DNS_RESP_NO_EXPECTED_RECORD:
1641 case DNS_RESP_ERROR:
1642 case DNS_RESP_INTERNAL:
1643 res->status = RSLV_STATUS_OTHER;
1644 ns->counters.other++;
1645 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001646 }
1647
Christopher Faulet67957bd2017-09-27 11:00:59 +02001648 /* Wait all nameservers response to handle errors */
1649 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1650 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001651
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652 /* Process error codes */
1653 if (dns_resp != DNS_RESP_VALID) {
1654 if (res->prefered_query_type != res->query_type) {
1655 /* The fallback on the query type was already performed,
1656 * so check the try counter. If it falls to 0, we can
1657 * report an error. Else, wait the next attempt. */
1658 if (!res->try)
1659 goto report_res_error;
1660 }
1661 else {
1662 /* Fallback from A to AAAA or the opposite and re-send
1663 * the resolution immediately. try counter is not
1664 * decremented. */
1665 if (res->prefered_query_type == DNS_RTYPE_A) {
1666 res->query_type = DNS_RTYPE_AAAA;
1667 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001668 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001669 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1670 res->query_type = DNS_RTYPE_A;
1671 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001672 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 }
1674 continue;
1675 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001676
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677 /* Now let's check the query's dname corresponds to the one we
1678 * sent. We can check only the first query of the list. We send
1679 * one query at a time so we get one query in the response */
1680 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1681 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1682 dns_resp = DNS_RESP_WRONG_NAME;
1683 ns->counters.other++;
1684 goto report_res_error;
1685 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001686
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687 /* So the resolution succeeded */
1688 res->status = RSLV_STATUS_VALID;
1689 res->last_valid = now_ms;
1690 ns->counters.valid++;
1691 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001692
Christopher Faulet67957bd2017-09-27 11:00:59 +02001693 report_res_error:
1694 list_for_each_entry(req, &res->requesters, list)
1695 req->requester_error_cb(req, dns_resp);
1696 dns_reset_resolution(res);
1697 LIST_DEL(&res->list);
1698 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1699 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001700
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701 report_res_success:
1702 /* Only the 1rst requester s managed by the server, others are
1703 * from the cache */
1704 tmpns = ns;
1705 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001706 struct server *s = objt_server(req->owner);
1707
1708 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001709 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001710 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001711 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001712 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001713 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001714 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001715
Christopher Faulet67957bd2017-09-27 11:00:59 +02001716 dns_reset_resolution(res);
1717 LIST_DEL(&res->list);
1718 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1719 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001720 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001721 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001722 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001723}
William Lallemand69e96442016-11-19 00:58:54 +01001724
Christopher Faulet67957bd2017-09-27 11:00:59 +02001725/* Called when a resolvers network socket is ready to send data */
1726static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001727{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728 struct dns_resolvers *resolvers;
1729 struct dns_nameserver *ns;
1730 struct dns_resolution *res;
1731 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001732
Christopher Faulet67957bd2017-09-27 11:00:59 +02001733 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001734
Christopher Faulet67957bd2017-09-27 11:00:59 +02001735 /* check if ready for sending */
1736 if (!fd_send_ready(fd))
1737 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001738
Christopher Faulet67957bd2017-09-27 11:00:59 +02001739 /* we don't want/need to be waked up any more for sending */
1740 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001741
Christopher Faulet67957bd2017-09-27 11:00:59 +02001742 /* no need to go further if we can't retrieve the nameserver */
1743 if ((ns = dgram->owner) == NULL)
1744 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001745
Christopher Faulet67957bd2017-09-27 11:00:59 +02001746 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001747 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001748
Christopher Faulet67957bd2017-09-27 11:00:59 +02001749 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001750 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001751
Christopher Faulet67957bd2017-09-27 11:00:59 +02001752 if (res->nb_queries == resolvers->nb_nameservers)
1753 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001754
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001755 len = dns_build_query(res->query_id, res->query_type,
1756 resolvers->accepted_payload_size,
1757 res->hostname_dn, res->hostname_dn_len,
1758 trash.area, trash.size);
1759 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001760 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001761
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001762 ret = send(fd, trash.area, len, 0);
1763 if (ret != len)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001764 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001765
Christopher Faulet67957bd2017-09-27 11:00:59 +02001766 ns->counters.sent++;
1767 res->nb_queries++;
1768 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001769
Christopher Faulet67957bd2017-09-27 11:00:59 +02001770 snd_error:
1771 ns->counters.snd_error++;
1772 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001773 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001774 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001775}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001776
Christopher Faulet67957bd2017-09-27 11:00:59 +02001777/* Processes DNS resolution. First, it checks the active list to detect expired
1778 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1779 * checks the wait list to trigger new resolutions.
1780 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001781static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001782{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001783 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001784 struct dns_resolution *res, *resback;
1785 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001786
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001787 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001788
Christopher Faulet67957bd2017-09-27 11:00:59 +02001789 /* Handle all expired resolutions from the active list */
1790 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1791 /* When we find the first resolution in the future, then we can
1792 * stop here */
1793 exp = tick_add(res->last_query, resolvers->timeout.retry);
1794 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001795 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001796
Christopher Faulet67957bd2017-09-27 11:00:59 +02001797 /* If current resolution has been tried too many times and
1798 * finishes in timeout we update its status and remove it from
1799 * the list */
1800 if (!res->try) {
1801 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001802
Christopher Faulet67957bd2017-09-27 11:00:59 +02001803 /* Notify the result to the requesters */
1804 if (!res->nb_responses)
1805 res->status = RSLV_STATUS_TIMEOUT;
1806 list_for_each_entry(req, &res->requesters, list)
1807 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001808
Christopher Faulet67957bd2017-09-27 11:00:59 +02001809 /* Clean up resolution info and remove it from the
1810 * current list */
1811 dns_reset_resolution(res);
1812 LIST_DEL(&res->list);
1813 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001814 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001815 else {
1816 /* Otherwise resend the DNS query and requeue the resolution */
1817 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1818 /* No response received (a real timeout) or fallback already done */
1819 res->query_type = res->prefered_query_type;
1820 res->try--;
1821 }
1822 else {
1823 /* Fallback from A to AAAA or the opposite and re-send
1824 * the resolution immediately. try counter is not
1825 * decremented. */
1826 if (res->prefered_query_type == DNS_RTYPE_A)
1827 res->query_type = DNS_RTYPE_AAAA;
1828 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1829 res->query_type = DNS_RTYPE_A;
1830 else
1831 res->try--;
1832 }
1833 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001834 }
1835 }
William Lallemand69e96442016-11-19 00:58:54 +01001836
Christopher Faulet67957bd2017-09-27 11:00:59 +02001837 /* Handle all resolutions in the wait list */
1838 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1839 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1840 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1841 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001842
Christopher Faulet67957bd2017-09-27 11:00:59 +02001843 if (dns_run_resolution(res) != 1) {
1844 res->last_resolution = now_ms;
1845 LIST_DEL(&res->list);
1846 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001847 }
1848 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001849
Christopher Faulet67957bd2017-09-27 11:00:59 +02001850 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001851 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001852 return t;
1853}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001854
Christopher Faulet67957bd2017-09-27 11:00:59 +02001855/* proto_udp callback functions for a DNS resolution */
1856struct dgram_data_cb resolve_dgram_cb = {
1857 .recv = dns_resolve_recv,
1858 .send = dns_resolve_send,
1859};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001860
Christopher Faulet67957bd2017-09-27 11:00:59 +02001861/* Release memory allocated by DNS */
1862static void dns_deinit(void)
1863{
1864 struct dns_resolvers *resolvers, *resolversback;
1865 struct dns_nameserver *ns, *nsback;
1866 struct dns_resolution *res, *resback;
1867 struct dns_requester *req, *reqback;
1868 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001869
Christopher Faulet67957bd2017-09-27 11:00:59 +02001870 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1871 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1872 free(ns->id);
1873 free((char *)ns->conf.file);
1874 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1875 fd_delete(ns->dgram->t.sock.fd);
1876 free(ns->dgram);
1877 LIST_DEL(&ns->list);
1878 free(ns);
1879 }
1880
1881 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1882 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1883 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001884 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001885 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001886 dns_free_resolution(res);
1887 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001888
Christopher Faulet67957bd2017-09-27 11:00:59 +02001889 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1890 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1891 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001892 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001893 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001894 dns_free_resolution(res);
1895 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001896
Christopher Faulet67957bd2017-09-27 11:00:59 +02001897 free(resolvers->id);
1898 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02001899 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001900 LIST_DEL(&resolvers->list);
1901 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001902 }
1903
Christopher Faulet67957bd2017-09-27 11:00:59 +02001904 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1905 free(srvrq->name);
1906 free(srvrq->hostname_dn);
1907 LIST_DEL(&srvrq->list);
1908 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001909 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001910}
1911
Christopher Faulet67957bd2017-09-27 11:00:59 +02001912/* Finalizes the DNS configuration by allocating required resources and checking
1913 * live parameters.
1914 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001915 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001916static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001917{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 struct dns_resolvers *resolvers;
1919 struct proxy *px;
1920 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001921
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 /* allocate pool of resolution per resolvers */
1923 list_for_each_entry(resolvers, &dns_resolvers, list) {
1924 struct dns_nameserver *ns;
1925 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001926
Christopher Faulet67957bd2017-09-27 11:00:59 +02001927 /* Check if we can create the socket with nameservers info */
1928 list_for_each_entry(ns, &resolvers->nameservers, list) {
1929 struct dgram_conn *dgram = NULL;
1930 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001931
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 /* Check nameserver info */
1933 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001934 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1935 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001936 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001937 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001938 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001939 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001940 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1941 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 close(fd);
1943 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001944 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001945 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001946 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001947
Christopher Faulet67957bd2017-09-27 11:00:59 +02001948 /* Create dgram structure that will hold the UPD socket
1949 * and attach it on the current nameserver */
1950 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001951 ha_alert("config: resolvers '%s' : out of memory.\n",
1952 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001953 err_code |= (ERR_ALERT|ERR_ABORT);
1954 goto err;
1955 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001956
Christopher Faulet67957bd2017-09-27 11:00:59 +02001957 /* Leave dgram partially initialized, no FD attached for
1958 * now. */
1959 dgram->owner = ns;
1960 dgram->data = &resolve_dgram_cb;
1961 dgram->t.sock.fd = -1;
1962 ns->dgram = dgram;
1963 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001964
Christopher Faulet67957bd2017-09-27 11:00:59 +02001965 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001966 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001967 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 err_code |= (ERR_ALERT|ERR_ABORT);
1969 goto err;
1970 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001971
Christopher Faulet67957bd2017-09-27 11:00:59 +02001972 /* Update task's parameters */
1973 t->process = dns_process_resolvers;
1974 t->context = resolvers;
1975 resolvers->t = t;
1976 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001977 }
1978
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001979 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001981
Christopher Faulet67957bd2017-09-27 11:00:59 +02001982 for (srv = px->srv; srv; srv = srv->next) {
1983 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001984
Christopher Faulet67957bd2017-09-27 11:00:59 +02001985 if (!srv->resolvers_id)
1986 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001987
Christopher Faulet67957bd2017-09-27 11:00:59 +02001988 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001989 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1990 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001991 err_code |= (ERR_ALERT|ERR_ABORT);
1992 continue;
1993 }
1994 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001995
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 if (srv->srvrq && !srv->srvrq->resolvers) {
1997 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001998 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001999 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2000 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002001 err_code |= (ERR_ALERT|ERR_ABORT);
2002 continue;
2003 }
2004 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002005 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002006 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2007 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002008 err_code |= (ERR_ALERT|ERR_ABORT);
2009 continue;
2010 }
2011 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002012 }
2013
Christopher Faulet67957bd2017-09-27 11:00:59 +02002014 if (err_code & (ERR_ALERT|ERR_ABORT))
2015 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002016
Christopher Faulet67957bd2017-09-27 11:00:59 +02002017 return err_code;
2018 err:
2019 dns_deinit();
2020 return err_code;
2021
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002022}
2023
Christopher Faulet67957bd2017-09-27 11:00:59 +02002024/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002025static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002026{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002027 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002028
Christopher Faulet67957bd2017-09-27 11:00:59 +02002029 if (*args[2]) {
2030 list_for_each_entry(presolvers, &dns_resolvers, list) {
2031 if (strcmp(presolvers->id, args[2]) == 0) {
2032 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002033 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002034 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002035 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002036 if (appctx->ctx.cli.p0 == NULL) {
2037 appctx->ctx.cli.severity = LOG_ERR;
2038 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
2039 appctx->st0 = CLI_ST_PRINT;
2040 return 1;
2041 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002042 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002043 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002044}
2045
Christopher Faulet67957bd2017-09-27 11:00:59 +02002046/* Dumps counters from all resolvers section and associated name servers. It
2047 * returns 0 if the output buffer is full and it needs to be called again,
2048 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002049 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002050 */
2051static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2052{
2053 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002054 struct dns_resolvers *resolvers;
2055 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002056
2057 chunk_reset(&trash);
2058
2059 switch (appctx->st2) {
2060 case STAT_ST_INIT:
2061 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2062 /* fall through */
2063
2064 case STAT_ST_LIST:
2065 if (LIST_ISEMPTY(&dns_resolvers)) {
2066 chunk_appendf(&trash, "No resolvers found\n");
2067 }
2068 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002069 list_for_each_entry(resolvers, &dns_resolvers, list) {
2070 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002071 continue;
2072
Christopher Faulet67957bd2017-09-27 11:00:59 +02002073 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2074 list_for_each_entry(ns, &resolvers->nameservers, list) {
2075 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2076 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2077 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2078 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2079 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2080 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2081 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2082 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2083 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2084 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2085 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2086 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2087 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2088 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2089 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2090 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002091 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002092 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002093 }
2094 }
2095
2096 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002097 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002098 /* let's try again later from this session. We add ourselves into
2099 * this session's users so that it can remove us upon termination.
2100 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002101 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002102 return 0;
2103 }
William Lallemand69e96442016-11-19 00:58:54 +01002104 /* fall through */
2105
2106 default:
2107 appctx->st2 = STAT_ST_FIN;
2108 return 1;
2109 }
2110}
2111
2112/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002113static struct cli_kw_list cli_kws = {{ }, {
2114 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2115 " associated name servers",
2116 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2117 {{},}
2118 }
2119};
William Lallemand69e96442016-11-19 00:58:54 +01002120
Willy Tarreau0108d902018-11-25 19:14:37 +01002121INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002122
Baptiste Assmann333939c2019-01-21 08:34:50 +01002123/*
2124 * Prepare <rule> for hostname resolution.
2125 * Returns -1 in case of any allocation failure, 0 if not.
2126 * On error, a global failure counter is also incremented.
2127 */
2128static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2129{
2130 char *hostname_dn;
2131 int hostname_len, hostname_dn_len;
2132 struct buffer *tmp = get_trash_chunk();
2133
2134 if (!hostname)
2135 return 0;
2136
2137 hostname_len = strlen(hostname);
2138 hostname_dn = tmp->area;
2139 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2140 hostname_dn, tmp->size);
2141 if (hostname_dn_len == -1)
2142 goto err;
2143
2144
2145 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2146 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2147 if (!stream->dns_ctx.hostname_dn)
2148 goto err;
2149
2150 return 0;
2151
2152 err:
2153 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2154 dns_failed_resolutions += 1;
2155 return -1;
2156}
2157
2158
2159/*
2160 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2161 */
2162enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2163 struct session *sess, struct stream *s, int flags)
2164{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002165 struct dns_resolution *resolution;
Willy Tarreau85835ee2019-07-17 10:38:45 +02002166 struct sample *smp;
2167 char *fqdn;
Baptiste Assmann44af0d02019-10-30 16:06:53 +01002168 struct dns_requester *req;
2169 struct dns_resolvers *resolvers;
2170 struct dns_resolution *res;
Christopher Fauletef131ae2020-07-22 11:46:32 +02002171 int exp, locked = 0;
2172 enum act_return ret = ACT_RET_CONT;
2173
2174 resolvers = rule->arg.dns.resolvers;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002175
2176 /* we have a response to our DNS resolution */
Baptiste Assmann44af0d02019-10-30 16:06:53 +01002177 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002178 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2179 resolution = s->dns_ctx.dns_requester->resolution;
Christopher Fauletef131ae2020-07-22 11:46:32 +02002180 if (!locked) {
2181 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2182 locked = 1;
2183 }
2184
Baptiste Assmann82c6aa52019-10-01 15:32:40 +02002185 if (resolution->step == RSLV_STEP_RUNNING) {
Christopher Fauletef131ae2020-07-22 11:46:32 +02002186 ret = ACT_RET_YIELD;
2187 goto end;
Baptiste Assmann82c6aa52019-10-01 15:32:40 +02002188 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01002189 if (resolution->step == RSLV_STEP_NONE) {
2190 /* We update the variable only if we have a valid response. */
2191 if (resolution->status == RSLV_STATUS_VALID) {
2192 struct sample smp;
2193 short ip_sin_family = 0;
2194 void *ip = NULL;
2195
2196 dns_get_ip_from_response(&resolution->response, &rule->arg.dns.dns_opts, NULL,
2197 0, &ip, &ip_sin_family, NULL);
2198
2199 switch (ip_sin_family) {
2200 case AF_INET:
2201 smp.data.type = SMP_T_IPV4;
2202 memcpy(&smp.data.u.ipv4, ip, 4);
2203 break;
2204 case AF_INET6:
2205 smp.data.type = SMP_T_IPV6;
2206 memcpy(&smp.data.u.ipv6, ip, 16);
2207 break;
2208 default:
2209 ip = NULL;
2210 }
2211
2212 if (ip) {
2213 smp.px = px;
2214 smp.sess = sess;
2215 smp.strm = s;
2216
2217 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2218 }
2219 }
2220 }
2221
2222 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
2223 s->dns_ctx.hostname_dn_len = 0;
2224 dns_unlink_resolution(s->dns_ctx.dns_requester);
2225
2226 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2227 s->dns_ctx.dns_requester = NULL;
2228
Christopher Fauletef131ae2020-07-22 11:46:32 +02002229 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002230 }
2231
2232 /* need to configure and start a new DNS resolution */
Willy Tarreau85835ee2019-07-17 10:38:45 +02002233 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2234 if (smp == NULL)
Christopher Fauletef131ae2020-07-22 11:46:32 +02002235 goto end;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002236
Willy Tarreau85835ee2019-07-17 10:38:45 +02002237 fqdn = smp->data.u.str.area;
2238 if (action_prepare_for_resolution(s, fqdn) == -1)
Christopher Fauletef131ae2020-07-22 11:46:32 +02002239 goto end; /* on error, ignore the action */
Baptiste Assmann333939c2019-01-21 08:34:50 +01002240
Willy Tarreau85835ee2019-07-17 10:38:45 +02002241 s->dns_ctx.parent = rule;
Christopher Fauletef131ae2020-07-22 11:46:32 +02002242
2243 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
2244 locked = 1;
2245
Willy Tarreau85835ee2019-07-17 10:38:45 +02002246 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann44af0d02019-10-30 16:06:53 +01002247
2248 /* Check if there is a fresh enough response in the cache of our associated resolution */
2249 req = s->dns_ctx.dns_requester;
2250 if (!req || !req->resolution) {
2251 dns_trigger_resolution(s->dns_ctx.dns_requester);
Christopher Fauletef131ae2020-07-22 11:46:32 +02002252 ret = ACT_RET_YIELD;
2253 goto end;
Baptiste Assmann44af0d02019-10-30 16:06:53 +01002254 }
Christopher Fauletef131ae2020-07-22 11:46:32 +02002255 res = req->resolution;
Baptiste Assmann44af0d02019-10-30 16:06:53 +01002256
2257 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2258 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2259 && !tick_is_expired(exp, now_ms)) {
2260 goto use_cache;
2261 }
2262
Willy Tarreau85835ee2019-07-17 10:38:45 +02002263 dns_trigger_resolution(s->dns_ctx.dns_requester);
Christopher Fauletef131ae2020-07-22 11:46:32 +02002264 ret = ACT_RET_YIELD;
2265
2266 end:
2267 if (locked)
2268 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
2269 return ret;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002270}
2271
2272
2273/* parse "do-resolve" action
2274 * This action takes the following arguments:
2275 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2276 *
2277 * - <varName> is the variable name where the result of the DNS resolution will be stored
2278 * (mandatory)
2279 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2280 * (mandatory)
2281 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2282 * (optional), defaults to ipv6
2283 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2284 */
2285enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2286{
2287 int cur_arg;
2288 struct sample_expr *expr;
2289 unsigned int where;
2290 const char *beg, *end;
2291
2292 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2293 cur_arg = *orig_arg - 1;
2294
2295 /* locate varName, which is mandatory */
2296 beg = strchr(args[cur_arg], '(');
2297 if (beg == NULL)
2298 goto do_resolve_parse_error;
2299 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2300 end = strchr(beg, ',');
2301 if (end == NULL)
2302 goto do_resolve_parse_error;
2303 rule->arg.dns.varname = my_strndup(beg, end - beg);
2304 if (rule->arg.dns.varname == NULL)
2305 goto do_resolve_parse_error;
2306
2307
2308 /* locate resolversSectionName, which is mandatory.
2309 * Since next parameters are optional, the delimiter may be comma ','
2310 * or closing parenthesis ')'
2311 */
2312 beg = end + 1;
2313 end = strchr(beg, ',');
2314 if (end == NULL)
2315 end = strchr(beg, ')');
2316 if (end == NULL)
2317 goto do_resolve_parse_error;
2318 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2319 if (rule->arg.dns.resolvers_id == NULL)
2320 goto do_resolve_parse_error;
2321
2322
2323 /* Default priority is ipv6 */
2324 rule->arg.dns.dns_opts.family_prio = AF_INET6;
2325
2326 /* optional arguments accepted for now:
2327 * ipv4 or ipv6
2328 */
2329 while (*end != ')') {
2330 beg = end + 1;
2331 end = strchr(beg, ',');
2332 if (end == NULL)
2333 end = strchr(beg, ')');
2334 if (end == NULL)
2335 goto do_resolve_parse_error;
2336
2337 if (strncmp(beg, "ipv4", end - beg) == 0) {
2338 rule->arg.dns.dns_opts.family_prio = AF_INET;
2339 }
2340 else if (strncmp(beg, "ipv6", end - beg) == 0) {
2341 rule->arg.dns.dns_opts.family_prio = AF_INET6;
2342 }
2343 else {
2344 goto do_resolve_parse_error;
2345 }
2346 }
2347
2348 cur_arg = cur_arg + 1;
2349
2350 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
2351 if (!expr)
2352 goto do_resolve_parse_error;
2353
2354
2355 where = 0;
2356 if (px->cap & PR_CAP_FE)
2357 where |= SMP_VAL_FE_HRQ_HDR;
2358 if (px->cap & PR_CAP_BE)
2359 where |= SMP_VAL_BE_HRQ_HDR;
2360
2361 if (!(expr->fetch->val & where)) {
2362 memprintf(err,
2363 "fetch method '%s' extracts information from '%s', none of which is available here",
2364 args[cur_arg-1], sample_src_names(expr->fetch->use));
2365 free(expr);
2366 return ACT_RET_PRS_ERR;
2367 }
2368 rule->arg.dns.expr = expr;
2369 rule->action = ACT_CUSTOM;
2370 rule->action_ptr = dns_action_do_resolve;
2371 *orig_arg = cur_arg;
2372
2373 rule->check_ptr = check_action_do_resolve;
2374
2375 return ACT_RET_PRS_OK;
2376
2377 do_resolve_parse_error:
2378 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2379 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2380 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2381 args[cur_arg]);
2382 return ACT_RET_PRS_ERR;
2383}
2384
2385static struct action_kw_list http_req_kws = { { }, {
2386 { "do-resolve", dns_parse_do_resolve, 1 },
2387 { /* END */ }
2388}};
2389
2390INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2391
2392static struct action_kw_list tcp_req_cont_actions = {ILH, {
2393 { "do-resolve", dns_parse_do_resolve, 1 },
2394 { /* END */ }
2395}};
2396
2397INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2398
2399/* Check an "http-request do-resolve" action.
2400 *
2401 * The function returns 1 in success case, otherwise, it returns 0 and err is
2402 * filled.
2403 */
2404int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2405{
2406 struct dns_resolvers *resolvers = NULL;
2407
2408 if (rule->arg.dns.resolvers_id == NULL) {
2409 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2410 return 0;
2411 }
2412
2413 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2414 if (resolvers == NULL) {
2415 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2416 return 0;
2417 }
2418 rule->arg.dns.resolvers = resolvers;
2419
2420 return 1;
2421}
2422
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002423REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002424REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);