blob: 64bb0d15f1e3988ab0717ab52c6e33e3e6e0d14b [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>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020042#include <proto/http_ana.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010043#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 Duesterhusfcac33d2020-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 Assmannf50e1ac2019-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;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100275 int len;
276
277 /* Update resolution */
278 resolution->nb_queries = 0;
279 resolution->nb_responses = 0;
280 resolution->last_query = now_ms;
281
282 len = dns_build_query(resolution->query_id, resolution->query_type,
283 resolvers->accepted_payload_size,
284 resolution->hostname_dn, resolution->hostname_dn_len,
285 trash.area, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200286
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287 list_for_each_entry(ns, &resolvers->nameservers, list) {
288 int fd = ns->dgram->t.sock.fd;
Willy Tarreau0eae6322019-12-20 11:18:54 +0100289 int ret;
290
Christopher Faulet67957bd2017-09-27 11:00:59 +0200291 if (fd == -1) {
292 if (dns_connect_namesaver(ns) == -1)
293 continue;
294 fd = ns->dgram->t.sock.fd;
295 resolvers->nb_nameservers++;
296 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200297
Willy Tarreau0eae6322019-12-20 11:18:54 +0100298 if (len < 0)
299 goto snd_error;
300
301 ret = send(fd, trash.area, len, 0);
302 if (ret == len) {
303 ns->counters.sent++;
304 resolution->nb_queries++;
305 continue;
306 }
307
308 if (ret == -1 && errno == EAGAIN) {
309 /* retry once the socket is ready */
310 fd_cant_send(fd);
311 continue;
312 }
313
314 snd_error:
315 ns->counters.snd_error++;
316 resolution->nb_queries++;
317 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200318
Christopher Faulet67957bd2017-09-27 11:00:59 +0200319 /* Push the resolution at the end of the active list */
320 LIST_DEL(&resolution->list);
321 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
322 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200323}
324
Christopher Faulet67957bd2017-09-27 11:00:59 +0200325/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
326 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200327 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200328static int
329dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200330{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200331 struct dns_resolvers *resolvers = resolution->resolvers;
332 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333
Christopher Faulet67957bd2017-09-27 11:00:59 +0200334 /* Avoid sending requests for resolutions that don't yet have an
335 * hostname, ie resolutions linked to servers that do not yet have an
336 * fqdn */
337 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200338 return 0;
339
Christopher Faulet67957bd2017-09-27 11:00:59 +0200340 /* Check if a resolution has already been started for this server return
341 * directly to avoid resolution pill up. */
342 if (resolution->step != RSLV_STEP_NONE)
343 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200344
Christopher Faulet67957bd2017-09-27 11:00:59 +0200345 /* Generates a new query id. We try at most 100 times to find a free
346 * query id */
347 for (i = 0; i < 100; ++i) {
348 query_id = dns_rnd16();
349 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200350 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200351 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200352 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200353 if (query_id == -1) {
354 send_log(NULL, LOG_NOTICE,
355 "could not generate a query id for %s, in resolvers %s.\n",
356 resolution->hostname_dn, resolvers->id);
357 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200358 }
359
Christopher Faulet67957bd2017-09-27 11:00:59 +0200360 /* Update resolution parameters */
361 resolution->query_id = query_id;
362 resolution->qid.key = query_id;
363 resolution->step = RSLV_STEP_RUNNING;
364 resolution->query_type = resolution->prefered_query_type;
365 resolution->try = resolvers->resolve_retries;
366 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200367
Christopher Faulet67957bd2017-09-27 11:00:59 +0200368 /* Send the DNS query */
369 resolution->try -= 1;
370 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200371 return 1;
372}
373
Christopher Faulet67957bd2017-09-27 11:00:59 +0200374/* Performs a name resolution for the requester <req> */
375void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200376{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200377 struct dns_resolvers *resolvers;
378 struct dns_resolution *res;
379 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200380
Christopher Faulet67957bd2017-09-27 11:00:59 +0200381 if (!req || !req->resolution)
382 return;
383 res = req->resolution;
384 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200385
Christopher Faulet67957bd2017-09-27 11:00:59 +0200386 /* The resolution must not be triggered yet. Use the cached response, if
387 * valid */
388 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200389 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
390 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
391 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200392}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200393
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200394
Christopher Faulet67957bd2017-09-27 11:00:59 +0200395/* Resets some resolution parameters to initial values and also delete the query
396 * ID from the resolver's tree.
397 */
398static void dns_reset_resolution(struct dns_resolution *resolution)
399{
400 /* update resolution status */
401 resolution->step = RSLV_STEP_NONE;
402 resolution->try = 0;
403 resolution->last_resolution = now_ms;
404 resolution->nb_queries = 0;
405 resolution->nb_responses = 0;
406 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200407
Christopher Faulet67957bd2017-09-27 11:00:59 +0200408 /* clean up query id */
409 eb32_delete(&resolution->qid);
410 resolution->query_id = 0;
411 resolution->qid.key = 0;
412}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200413
Christopher Faulet67957bd2017-09-27 11:00:59 +0200414/* Returns the query id contained in a DNS response */
415static inline unsigned short dns_response_get_query_id(unsigned char *resp)
416{
417 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200418}
419
Christopher Faulet67957bd2017-09-27 11:00:59 +0200420
421/* Analyses, re-builds and copies the name <name> from the DNS response packet
422 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
423 * for compressed data. The result is copied into <dest>, ensuring we don't
424 * overflow using <dest_len> Returns the number of bytes the caller can move
425 * forward. If 0 it means an error occurred while parsing the name. <offset> is
426 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200427 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200428int dns_read_name(unsigned char *buffer, unsigned char *bufend,
429 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100430 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200431{
432 int nb_bytes = 0, n = 0;
433 int label_len;
434 unsigned char *reader = name;
435 char *dest = destination;
436
437 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100438 if (reader >= bufend)
439 goto err;
440
Christopher Faulet67957bd2017-09-27 11:00:59 +0200441 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200442 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100443 if (reader + 1 >= bufend)
444 goto err;
445
Christopher Faulet67957bd2017-09-27 11:00:59 +0200446 /* Must point BEFORE current position */
447 if ((buffer + reader[1]) > reader)
448 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200449
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100450 if (depth++ > 100)
451 goto err;
452
Nikhil Agrawal2fa66c32018-12-20 10:50:59 +0530453 n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100454 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200455 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200456 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200457
Christopher Faulet67957bd2017-09-27 11:00:59 +0200458 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200459 nb_bytes += n;
460 goto out;
461 }
462
463 label_len = *reader;
464 if (label_len == 0)
465 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200466
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200467 /* Check if:
468 * - we won't read outside the buffer
469 * - there is enough place in the destination
470 */
471 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200472 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200473
474 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200475 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200476
477 memcpy(dest, reader, label_len);
478
Christopher Faulet67957bd2017-09-27 11:00:59 +0200479 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200480 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200481 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200482 }
483
Christopher Faulet67957bd2017-09-27 11:00:59 +0200484 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200485 /* offset computation:
486 * parse from <name> until finding either NULL or a pointer "c0xx"
487 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200488 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200489 *offset = 0;
490 while (reader < bufend) {
491 if ((reader[0] & 0xc0) == 0xc0) {
492 *offset += 2;
493 break;
494 }
495 else if (*reader == 0) {
496 *offset += 1;
497 break;
498 }
499 *offset += 1;
500 ++reader;
501 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200502 return nb_bytes;
503
Christopher Faulet67957bd2017-09-27 11:00:59 +0200504 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200505 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200506}
507
Christopher Faulet67957bd2017-09-27 11:00:59 +0200508/* Checks for any obsolete record, also identify any SRV request, and try to
509 * find a corresponding server.
510*/
511static void dns_check_dns_response(struct dns_resolution *res)
512{
513 struct dns_resolvers *resolvers = res->resolvers;
514 struct dns_requester *req, *reqback;
515 struct dns_answer_item *item, *itemback;
516 struct server *srv;
517 struct dns_srvrq *srvrq;
518
519 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
520
521 /* Remove obsolete items */
522 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
523 if (item->type != DNS_RTYPE_SRV)
524 goto rm_obselete_item;
525
526 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
527 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
528 continue;
529
530 /* Remove any associated server */
531 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100532 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200533 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
534 item->data_len == srv->hostname_dn_len &&
535 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
536 snr_update_srv_status(srv, 1);
537 free(srv->hostname);
538 free(srv->hostname_dn);
539 srv->hostname = NULL;
540 srv->hostname_dn = NULL;
541 srv->hostname_dn_len = 0;
542 dns_unlink_resolution(srv->dns_requester);
543 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100544 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200545 }
546 }
547
548 rm_obselete_item:
549 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100550 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551 continue;
552 }
553
554 if (item->type != DNS_RTYPE_SRV)
555 continue;
556
557 /* Now process SRV records */
558 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
559 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
560 continue;
561
562 /* Check if a server already uses that hostname */
563 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100564 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200565 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
566 item->data_len == srv->hostname_dn_len &&
Daniel Corbettf8716912019-11-17 09:48:56 -0500567 !memcmp(srv->hostname_dn, item->target, item->data_len) &&
568 !srv->dns_opts.ignore_weight) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100569 int ha_weight;
570
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200571 /* DNS weight range if from 0 to 65535
572 * HAProxy weight is from 0 to 256
573 * The rule below ensures that weight 0 is well respected
574 * while allowing a "mapping" from DNS weight into HAProxy's one.
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100575 */
Baptiste Assmann25e6fc22019-10-21 15:13:48 +0200576 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100577 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200578 char weight[9];
579
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100580 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200581 server_parse_weight_change_request(srv, weight);
582 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100583 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200584 break;
585 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100586 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200587 }
588 if (srv)
589 continue;
590
591 /* If not, try to find a server with undefined hostname */
592 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100593 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200594 if (srv->srvrq == srvrq && !srv->hostname_dn)
595 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100596 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200597 }
598 /* And update this server, if found */
599 if (srv) {
600 const char *msg = NULL;
601 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100602 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200603 char hostname[DNS_MAX_NAME_SIZE];
604
605 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100606 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100607 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200608 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100609 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100610 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200611 if (msg)
612 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
613
614 srv->svc_port = item->port;
615 srv->flags &= ~SRV_F_MAPPORTS;
616 if ((srv->check.state & CHK_ST_CONFIGURED) &&
617 !(srv->flags & SRV_F_CHECKPORT))
618 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100619
Daniel Corbettf8716912019-11-17 09:48:56 -0500620 if (!srv->dns_opts.ignore_weight) {
621 /* DNS weight range if from 0 to 65535
622 * HAProxy weight is from 0 to 256
623 * The rule below ensures that weight 0 is well respected
624 * while allowing a "mapping" from DNS weight into HAProxy's one.
625 */
626 ha_weight = (item->weight + 255) / 256;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100627
Daniel Corbettf8716912019-11-17 09:48:56 -0500628 snprintf(weight, sizeof(weight), "%d", ha_weight);
629 server_parse_weight_change_request(srv, weight);
630 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200632 }
633 }
634 }
635}
636
637/* Validates that the buffer DNS response provided in <resp> and finishing
638 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200639 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200640 * The result is stored in <resolution>' response, buf_response,
641 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200642 *
643 * This function returns one of the DNS_RESP_* code to indicate the type of
644 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200645 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200646static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
647 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200648{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200649 unsigned char *reader;
650 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200651 int len, flags, offset;
652 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200653 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200654 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200655 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200656 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200657 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200658
Christopher Faulet67957bd2017-09-27 11:00:59 +0200659 reader = resp;
660 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200661 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200662 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200663
Christopher Faulet67957bd2017-09-27 11:00:59 +0200664 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200665 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200666
667 /* query id */
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.id = reader[0] * 256 + reader[1];
671 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200672
Christopher Faulet67957bd2017-09-27 11:00:59 +0200673 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200674 * First byte contains:
675 * - response flag (1 bit)
676 * - opcode (4 bits)
677 * - authoritative (1 bit)
678 * - truncated (1 bit)
679 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200680 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200681 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200682 return DNS_RESP_INVALID;
683
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200684 flags = reader[0] * 256 + reader[1];
685
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200686 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
687 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200688 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200689 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200690 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200691 return DNS_RESP_ERROR;
692 }
693
Christopher Faulet67957bd2017-09-27 11:00:59 +0200694 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200695 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200696
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200697 /* 2 bytes for question count */
698 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200699 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200700 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200701 /* (for now) we send one query only, so we expect only one in the
702 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200703 if (dns_p->header.qdcount != 1)
704 return DNS_RESP_QUERY_COUNT_ERROR;
705 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200706 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200707 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200708
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200709 /* 2 bytes for answer count */
710 if (reader + 2 >= bufend)
711 return DNS_RESP_INVALID;
712 dns_p->header.ancount = reader[0] * 256 + reader[1];
713 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200714 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200715 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200716 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200717 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200718 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200719
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200720 /* 2 bytes authority count */
721 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200722 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200723 dns_p->header.nscount = reader[0] * 256 + reader[1];
724 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200725
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200726 /* 2 bytes additional count */
727 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200728 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200729 dns_p->header.arcount = reader[0] * 256 + reader[1];
730 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200731
Christopher Faulet67957bd2017-09-27 11:00:59 +0200732 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200733 LIST_INIT(&dns_p->query_list);
734 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 +0200735 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200736 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200737 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200738 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
739 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200740 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200741 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200742
Christopher Faulet67957bd2017-09-27 11:00:59 +0200743 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200744 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200745 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200746 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100747 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200748
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200749 if (len == 0)
750 return DNS_RESP_INVALID;
751
752 reader += offset;
753 previous_dname = dns_query->name;
754
755 /* move forward 2 bytes for question type */
756 if (reader + 2 >= bufend)
757 return DNS_RESP_INVALID;
758 dns_query->type = reader[0] * 256 + reader[1];
759 reader += 2;
760
761 /* move forward 2 bytes for question class */
762 if (reader + 2 >= bufend)
763 return DNS_RESP_INVALID;
764 dns_query->class = reader[0] * 256 + reader[1];
765 reader += 2;
766 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767
Baptiste Assmann251abb92017-08-11 09:58:27 +0200768 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200769 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200770 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
771 return DNS_RESP_TRUNCATED;
772
Baptiste Assmann325137d2015-04-13 23:40:55 +0200773 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200774 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200775 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200776 if (reader >= bufend)
777 return DNS_RESP_INVALID;
778
Willy Tarreaubafbe012017-11-24 17:34:44 +0100779 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200780 if (dns_answer_record == NULL)
781 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200782
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200783 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100784 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200785
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200786 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100787 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200788 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200789 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200790
Christopher Faulet67957bd2017-09-27 11:00:59 +0200791 /* Check if the current record dname is valid. previous_dname
792 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200793 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100794 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200795 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200796 /* First record, means a mismatch issue between
797 * queried dname and dname found in the first
798 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200799 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200800 }
801 else {
802 /* If not the first record, this means we have a
803 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200805 }
806
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807 }
808
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200809 memcpy(dns_answer_record->name, tmpname, len);
810 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200811
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200812 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200813 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100814 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200815 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200816 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200817
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200818 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200819 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100820 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200821 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200822 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200823 dns_answer_record->type = reader[0] * 256 + reader[1];
824 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200825
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200826 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200827 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100828 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200829 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200830 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200831 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200832 reader += 2;
833
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200834 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200835 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100836 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200837 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200838 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200839 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
840 + reader[2] * 256 + reader[3];
841 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200842
Christopher Faulet67957bd2017-09-27 11:00:59 +0200843 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200844 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100845 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200846 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200847 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200848 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200849
Christopher Faulet67957bd2017-09-27 11:00:59 +0200850 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200851 reader += 2;
852
Jérôme Magnin8d4e7dc2018-12-20 16:47:31 +0100853 if (reader + dns_answer_record->data_len > bufend) {
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100854 pool_free(dns_answer_item_pool, dns_answer_record);
855 return DNS_RESP_INVALID;
856 }
857
Christopher Faulet67957bd2017-09-27 11:00:59 +0200858 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200859 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200860 case DNS_RTYPE_A:
861 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200862 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100863 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200864 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200865 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200866 dns_answer_record->address.sa_family = AF_INET;
867 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
868 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200869 break;
870
871 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200872 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200873 * no IP could be found and last record was a CNAME. Could be triggered
874 * by a wrong query type
875 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200876 * + 1 because dns_answer_record_id starts at 0
877 * while number of answers is an integer and
878 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200879 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200880 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100881 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200882 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200883 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200884
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 Houcharda8c6db82017-07-06 18:46:47 +0200887 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100888 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200889 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200890 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200891
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200892 memcpy(dns_answer_record->target, tmpname, len);
893 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200894 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200895 break;
896
Olivier Houchard8da5f982017-08-04 18:35:36 +0200897
898 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200899 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200900 * - 2 bytes for the priority
901 * - 2 bytes for the weight
902 * - 2 bytes for the port
903 * - the target hostname
904 */
905 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100906 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200907 return DNS_RESP_INVALID;
908 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200909 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200910 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200911 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200912 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200913 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200914 reader += sizeof(uint16_t);
915 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100916 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200917 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100918 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200919 return DNS_RESP_INVALID;
920 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200921 dns_answer_record->data_len = len;
922 memcpy(dns_answer_record->target, tmpname, len);
923 dns_answer_record->target[len] = 0;
924 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200925
Baptiste Assmann325137d2015-04-13 23:40:55 +0200926 case DNS_RTYPE_AAAA:
927 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200928 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100929 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200930 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200931 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200932 dns_answer_record->address.sa_family = AF_INET6;
933 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
934 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200935 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200936
Baptiste Assmann325137d2015-04-13 23:40:55 +0200937 } /* switch (record type) */
938
Christopher Faulet67957bd2017-09-27 11:00:59 +0200939 /* Increment the counter for number of records saved into our
940 * local response */
941 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200942
Christopher Faulet67957bd2017-09-27 11:00:59 +0200943 /* Move forward dns_answer_record->data_len for analyzing next
944 * record in the response */
945 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
946 ? offset
947 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200948
949 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200950 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200951 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
952 if (tmp_record->type != dns_answer_record->type)
953 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200954
955 switch(tmp_record->type) {
956 case DNS_RTYPE_A:
957 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
958 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
959 sizeof(in_addr_t)))
960 found = 1;
961 break;
962
963 case DNS_RTYPE_AAAA:
964 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
965 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
966 sizeof(struct in6_addr)))
967 found = 1;
968 break;
969
Olivier Houchard8da5f982017-08-04 18:35:36 +0200970 case DNS_RTYPE_SRV:
971 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200972 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200973 dns_answer_record->port == tmp_record->port) {
974 tmp_record->weight = dns_answer_record->weight;
975 found = 1;
976 }
977 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200978
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200979 default:
980 break;
981 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200982
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200983 if (found == 1)
984 break;
985 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200986
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200987 if (found == 1) {
988 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100989 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200990 }
991 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200992 dns_answer_record->last_seen = now.tv_sec;
993 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
994 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200995 } /* for i 0 to ancount */
996
Christopher Faulet67957bd2017-09-27 11:00:59 +0200997 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200998 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200999 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001000 return DNS_RESP_VALID;
1001}
1002
Christopher Faulet67957bd2017-09-27 11:00:59 +02001003/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001004 * If existing IP not found, return the first IP matching family_priority,
1005 * otherwise, first ip found
1006 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001007 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001008 * For both cases above, dns_validate_dns_response is required
1009 * returns one of the DNS_UPD_* code
1010 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001011int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001012 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001013 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001014 void **newip, short *newip_sin_family,
1015 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001016{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001017 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001018 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001019 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001020 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001021 int currentip_sel;
1022 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001023 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001024 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001025
Christopher Faulet67957bd2017-09-27 11:00:59 +02001026 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001027 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001028 *newip = newip4 = newip6 = NULL;
1029 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001030 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001031 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001032
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001033 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -08001034 * Top priority is the preferred network ip version,
1035 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001036 * the last priority is the currently used IP,
1037 *
1038 * For these three priorities, a score is calculated. The
1039 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001040 * 8 - preferred ip version.
1041 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001042 * 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 +01001043 * 1 - current ip.
1044 * The result with the biggest score is returned.
1045 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001046
1047 list_for_each_entry(record, &dns_p->answer_list, list) {
1048 void *ip;
1049 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001050
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001051 if (record->type == DNS_RTYPE_A) {
1052 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1053 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001054 }
1055 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001056 ip_type = AF_INET6;
1057 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001058 }
1059 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001060 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001061 score = 0;
1062
Joseph Herlant42cf6392018-11-15 10:33:28 -08001063 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001064 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001065 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001066
Joseph Herlant42cf6392018-11-15 10:33:28 -08001067 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001068 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001069
1070 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001071 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001072 continue;
1073
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001074 if ((ip_type == AF_INET &&
1075 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001076 &dns_opts->pref_net[j].mask.in4,
1077 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001078 (ip_type == AF_INET6 &&
1079 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001080 &dns_opts->pref_net[j].mask.in6,
1081 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001082 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001083 break;
1084 }
1085 }
1086
Christopher Faulet67957bd2017-09-27 11:00:59 +02001087 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001088 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001089 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001090 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001091 if (!allowed_duplicated_ip) {
1092 continue;
1093 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001094 } else {
1095 score += 2;
1096 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001097
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001098 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001099 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001100 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001101 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001102 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001103 !memcmp(ip, currentip, 16)))) {
1104 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001105 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001106 }
1107 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001108 currentip_sel = 0;
1109
1110 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001111 * score. The maximum score is 15, if this value is reached, we
1112 * break the parsing. Implicitly, this score is reached the ip
1113 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001114 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001115 if (ip_type == AF_INET)
1116 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001117 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001118 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001119 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001120 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001121 return DNS_UPD_NO;
1122 max_score = score;
1123 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001124 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001125
Christopher Faulet67957bd2017-09-27 11:00:59 +02001126 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001127 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001128 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001129
Christopher Faulet67957bd2017-09-27 11:00:59 +02001130 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001131 if (family_priority == AF_INET) {
1132 if (newip4) {
1133 *newip = newip4;
1134 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135 }
1136 else if (newip6) {
1137 *newip = newip6;
1138 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001139 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001140 if (!currentip_found)
1141 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001142 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001143 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001144 else if (family_priority == AF_INET6) {
1145 if (newip6) {
1146 *newip = newip6;
1147 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001148 }
1149 else if (newip4) {
1150 *newip = newip4;
1151 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001152 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001153 if (!currentip_found)
1154 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001155 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001156 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001157 else if (family_priority == AF_UNSPEC) {
1158 if (newip6) {
1159 *newip = newip6;
1160 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001161 }
1162 else if (newip4) {
1163 *newip = newip4;
1164 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001165 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001166 if (!currentip_found)
1167 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001168 }
1169
Christopher Faulet67957bd2017-09-27 11:00:59 +02001170 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001171 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001172
Christopher Faulet67957bd2017-09-27 11:00:59 +02001173 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001174 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001175 /* Move the first record to the end of the list, for internal
1176 * round robin */
1177 LIST_DEL(&record->list);
1178 LIST_ADDQ(&dns_p->answer_list, &record->list);
1179 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001180 }
1181 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001182}
1183
Christopher Faulet67957bd2017-09-27 11:00:59 +02001184/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001185 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001186 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1187 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001188 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001189 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1190 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001191 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001192int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001193{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001194 char *ptr;
1195 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001196
Christopher Faulet67957bd2017-09-27 11:00:59 +02001197 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001198 return -1;
1199
Christopher Faulet67957bd2017-09-27 11:00:59 +02001200 ptr = str;
1201 for (i = 0; i < dn_len-1; ++i) {
1202 sz = dn[i];
1203 if (i)
1204 *ptr++ = '.';
1205 memcpy(ptr, dn+i+1, sz);
1206 ptr += sz;
1207 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001208 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001209 *ptr++ = '\0';
1210 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001211}
1212
Christopher Faulet67957bd2017-09-27 11:00:59 +02001213/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1214 *
1215 * <str> must be a null-terminated string. <str_len> must include the
1216 * terminating null byte. <dn> buffer must be allocated and its size must be
1217 * passed in <dn_len>.
1218 *
1219 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1220 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001221 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001222int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001223{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001224 int i, offset;
1225
Christopher Faulet67957bd2017-09-27 11:00:59 +02001226 if (dn_len < str_len + 1)
1227 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001228
Christopher Faulet67957bd2017-09-27 11:00:59 +02001229 /* First byte of dn will be used to store the length of the first
1230 * label */
1231 offset = 0;
1232 for (i = 0; i < str_len; ++i) {
1233 if (str[i] == '.') {
1234 /* 2 or more consecutive dots is invalid */
1235 if (i == offset)
1236 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001237
Christopher Faulet67957bd2017-09-27 11:00:59 +02001238 dn[offset] = (i - offset);
1239 offset = i+1;
1240 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001241 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001242 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001243 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001244 dn[offset] = (i - offset - 1);
1245 dn[i] = '\0';
1246 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001247}
1248
Christopher Faulet67957bd2017-09-27 11:00:59 +02001249/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001250 * - total size
1251 * - each label size individually
1252 * returns:
1253 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1254 * 1 when no error. <err> is left unaffected.
1255 */
1256int dns_hostname_validation(const char *string, char **err)
1257{
1258 const char *c, *d;
1259 int i;
1260
1261 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1262 if (err)
1263 *err = DNS_TOO_LONG_FQDN;
1264 return 0;
1265 }
1266
1267 c = string;
1268 while (*c) {
1269 d = c;
1270
1271 i = 0;
1272 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1273 i++;
1274 if (!((*d == '-') || (*d == '_') ||
1275 ((*d >= 'a') && (*d <= 'z')) ||
1276 ((*d >= 'A') && (*d <= 'Z')) ||
1277 ((*d >= '0') && (*d <= '9')))) {
1278 if (err)
1279 *err = DNS_INVALID_CHARACTER;
1280 return 0;
1281 }
1282 d++;
1283 }
1284
1285 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1286 if (err)
1287 *err = DNS_LABEL_TOO_LONG;
1288 return 0;
1289 }
1290
1291 if (*d == '\0')
1292 goto out;
1293
1294 c = ++d;
1295 }
1296 out:
1297 return 1;
1298}
1299
Christopher Faulet67957bd2017-09-27 11:00:59 +02001300/* Picks up an available resolution from the different resolution list
1301 * associated to a resolvers section, in this order:
1302 * 1. check in resolutions.curr for the same hostname and query_type
1303 * 2. check in resolutions.wait for the same hostname and query_type
1304 * 3. Get a new resolution from resolution pool
1305 *
1306 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001307 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001308static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1309 char **hostname_dn, int hostname_dn_len,
1310 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001311{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001312 struct dns_resolution *res;
1313
1314 if (!*hostname_dn)
1315 goto from_pool;
1316
1317 /* Search for same hostname and query type in resolutions.curr */
1318 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1319 if (!res->hostname_dn)
1320 continue;
1321 if ((query_type == res->prefered_query_type) &&
1322 hostname_dn_len == res->hostname_dn_len &&
1323 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1324 return res;
1325 }
1326
1327 /* Search for same hostname and query type in resolutions.wait */
1328 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1329 if (!res->hostname_dn)
1330 continue;
1331 if ((query_type == res->prefered_query_type) &&
1332 hostname_dn_len == res->hostname_dn_len &&
1333 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1334 return res;
1335 }
1336
1337 from_pool:
1338 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001339 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001340 if (res) {
1341 memset(res, 0, sizeof(*res));
1342 res->resolvers = resolvers;
1343 res->uuid = resolution_uuid;
1344 res->status = RSLV_STATUS_NONE;
1345 res->step = RSLV_STEP_NONE;
1346 res->last_valid = now_ms;
1347
1348 LIST_INIT(&res->requesters);
1349 LIST_INIT(&res->response.answer_list);
1350
1351 res->prefered_query_type = query_type;
1352 res->query_type = query_type;
1353 res->hostname_dn = *hostname_dn;
1354 res->hostname_dn_len = hostname_dn_len;
1355
1356 ++resolution_uuid;
1357
1358 /* Move the resolution to the resolvers wait queue */
1359 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1360 }
1361 return res;
1362}
1363
1364/* Releases a resolution from its requester(s) and move it back to the pool */
1365static void dns_free_resolution(struct dns_resolution *resolution)
1366{
1367 struct dns_requester *req, *reqback;
1368
1369 /* clean up configuration */
1370 dns_reset_resolution(resolution);
1371 resolution->hostname_dn = NULL;
1372 resolution->hostname_dn_len = 0;
1373
1374 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1375 LIST_DEL(&req->list);
1376 req->resolution = NULL;
1377 }
1378
1379 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001380 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001381}
1382
1383/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1384 * on success, -1 otherwise.
1385 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001386int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001387{
1388 struct dns_resolution *res = NULL;
1389 struct dns_requester *req;
1390 struct dns_resolvers *resolvers;
1391 struct server *srv = NULL;
1392 struct dns_srvrq *srvrq = NULL;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001393 struct stream *stream = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001394 char **hostname_dn;
1395 int hostname_dn_len, query_type;
1396
1397 switch (requester_type) {
1398 case OBJ_TYPE_SERVER:
1399 srv = (struct server *)requester;
1400 hostname_dn = &srv->hostname_dn;
1401 hostname_dn_len = srv->hostname_dn_len;
1402 resolvers = srv->resolvers;
1403 query_type = ((srv->dns_opts.family_prio == AF_INET)
1404 ? DNS_RTYPE_A
1405 : DNS_RTYPE_AAAA);
1406 break;
1407
1408 case OBJ_TYPE_SRVRQ:
1409 srvrq = (struct dns_srvrq *)requester;
1410 hostname_dn = &srvrq->hostname_dn;
1411 hostname_dn_len = srvrq->hostname_dn_len;
1412 resolvers = srvrq->resolvers;
1413 query_type = DNS_RTYPE_SRV;
1414 break;
1415
Baptiste Assmann333939c2019-01-21 08:34:50 +01001416 case OBJ_TYPE_STREAM:
1417 stream = (struct stream *)requester;
1418 hostname_dn = &stream->dns_ctx.hostname_dn;
1419 hostname_dn_len = stream->dns_ctx.hostname_dn_len;
1420 resolvers = stream->dns_ctx.parent->arg.dns.resolvers;
1421 query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts.family_prio == AF_INET)
1422 ? DNS_RTYPE_A
1423 : DNS_RTYPE_AAAA);
1424 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001425 default:
1426 goto err;
1427 }
1428
1429 /* Get a resolution from the resolvers' wait queue or pool */
1430 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1431 goto err;
1432
1433 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001434 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001435 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001436 if (srv->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001437 if ((req = pool_alloc(dns_requester_pool)) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001438 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001439 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001440 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001441 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001442 req->owner = &srv->obj_type;
1443 srv->dns_requester = req;
1444 }
1445 else
1446 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001447 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001448 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001449
1450 req->requester_cb = snr_resolution_cb;
1451 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001452 }
1453 else if (srvrq) {
1454 if (srvrq->dns_requester == NULL) {
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001455 if ((req = pool_alloc(dns_requester_pool)) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001456 goto err;
1457 req->owner = &srvrq->obj_type;
1458 srvrq->dns_requester = req;
1459 }
1460 else
1461 req = srvrq->dns_requester;
Baptiste Assmanndb4c8522018-01-30 08:08:04 +01001462
1463 req->requester_cb = snr_resolution_cb;
1464 req->requester_error_cb = snr_resolution_error_cb;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001465 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01001466 else if (stream) {
1467 if (stream->dns_ctx.dns_requester == NULL) {
1468 if ((req = pool_alloc(dns_requester_pool)) == NULL)
1469 goto err;
1470 req->owner = &stream->obj_type;
1471 stream->dns_ctx.dns_requester = req;
1472 }
1473 else
1474 req = stream->dns_ctx.dns_requester;
1475
1476 req->requester_cb = act_resolution_cb;
1477 req->requester_error_cb = act_resolution_error_cb;
1478 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001479 else
1480 goto err;
1481
1482 req->resolution = res;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001483
1484 LIST_ADDQ(&res->requesters, &req->list);
1485 return 0;
1486
1487 err:
1488 if (res && LIST_ISEMPTY(&res->requesters))
1489 dns_free_resolution(res);
1490 return -1;
1491}
1492
1493/* Removes a requester from a DNS resoltion. It takes takes care of all the
1494 * consequences. It also cleans up some parameters from the requester.
1495 */
1496void dns_unlink_resolution(struct dns_requester *requester)
1497{
1498 struct dns_resolution *res;
1499 struct dns_requester *req;
1500
1501 /* Nothing to do */
1502 if (!requester || !requester->resolution)
1503 return;
1504 res = requester->resolution;
1505
1506 /* Clean up the requester */
1507 LIST_DEL(&requester->list);
1508 requester->resolution = NULL;
1509
1510 /* We need to find another requester linked on this resolution */
1511 if (!LIST_ISEMPTY(&res->requesters))
1512 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1513 else {
1514 dns_free_resolution(res);
1515 return;
1516 }
1517
1518 /* Move hostname_dn related pointers to the next requester */
1519 switch (obj_type(req->owner)) {
1520 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001521 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1522 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001523 break;
1524 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001525 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1526 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001527 break;
Baptiste Assmann333939c2019-01-21 08:34:50 +01001528 case OBJ_TYPE_STREAM:
1529 res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn;
1530 res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len;
1531 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001532 default:
1533 res->hostname_dn = NULL;
1534 res->hostname_dn_len = 0;
1535 break;
1536 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001537}
1538
Christopher Faulet67957bd2017-09-27 11:00:59 +02001539/* Called when a network IO is generated on a name server socket for an incoming
1540 * packet. It performs the following actions:
1541 * - check if the packet requires processing (not outdated resolution)
1542 * - ensure the DNS packet received is valid and call requester's callback
1543 * - call requester's error callback if invalid response
1544 * - check the dn_name in the packet against the one sent
1545 */
1546static void dns_resolve_recv(struct dgram_conn *dgram)
1547{
1548 struct dns_nameserver *ns, *tmpns;
1549 struct dns_resolvers *resolvers;
1550 struct dns_resolution *res;
1551 struct dns_query_item *query;
1552 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1553 unsigned char *bufend;
1554 int fd, buflen, dns_resp;
1555 int max_answer_records;
1556 unsigned short query_id;
1557 struct eb32_node *eb;
1558 struct dns_requester *req;
1559
1560 fd = dgram->t.sock.fd;
1561
1562 /* check if ready for reading */
1563 if (!fd_recv_ready(fd))
1564 return;
1565
1566 /* no need to go further if we can't retrieve the nameserver */
Willy Tarreau1c759952019-12-10 18:38:09 +01001567 if ((ns = dgram->owner) == NULL) {
1568 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
1569 fd_stop_recv(fd);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001570 return;
Willy Tarreau1c759952019-12-10 18:38:09 +01001571 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001572
1573 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001574 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575
1576 /* process all pending input messages */
Willy Tarreau1c759952019-12-10 18:38:09 +01001577 while (fd_recv_ready(fd)) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001578 /* read message received */
1579 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1580 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Willy Tarreau1c759952019-12-10 18:38:09 +01001581 /* FIXME : for now we consider EAGAIN only, but at
1582 * least we purge sticky errors that would cause us to
1583 * be called in loops.
1584 */
1585 _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR));
Christopher Faulet67957bd2017-09-27 11:00:59 +02001586 fd_cant_recv(fd);
1587 break;
1588 }
1589
1590 /* message too big */
1591 if (buflen > resolvers->accepted_payload_size) {
1592 ns->counters.too_big++;
1593 continue;
1594 }
1595
1596 /* initializing variables */
1597 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1598
1599 /* read the query id from the packet (16 bits) */
1600 if (buf + 2 > bufend) {
1601 ns->counters.invalid++;
1602 continue;
1603 }
1604 query_id = dns_response_get_query_id(buf);
1605
1606 /* search the query_id in the pending resolution tree */
1607 eb = eb32_lookup(&resolvers->query_ids, query_id);
1608 if (eb == NULL) {
1609 /* unknown query id means an outdated response and can be safely ignored */
1610 ns->counters.outdated++;
1611 continue;
1612 }
1613
William Dauchybe8a3872019-11-27 23:32:41 +01001614 /* known query id means a resolution in progress */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001615 res = eb32_entry(eb, struct dns_resolution, qid);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001616 /* number of responses received */
1617 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001618
Christopher Faulet67957bd2017-09-27 11:00:59 +02001619 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1620 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001621
Christopher Faulet67957bd2017-09-27 11:00:59 +02001622 switch (dns_resp) {
1623 case DNS_RESP_VALID:
1624 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001625
Christopher Faulet67957bd2017-09-27 11:00:59 +02001626 case DNS_RESP_INVALID:
1627 case DNS_RESP_QUERY_COUNT_ERROR:
1628 case DNS_RESP_WRONG_NAME:
1629 res->status = RSLV_STATUS_INVALID;
1630 ns->counters.invalid++;
1631 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001632
Christopher Faulet67957bd2017-09-27 11:00:59 +02001633 case DNS_RESP_NX_DOMAIN:
1634 res->status = RSLV_STATUS_NX;
1635 ns->counters.nx++;
1636 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001637
Christopher Faulet67957bd2017-09-27 11:00:59 +02001638 case DNS_RESP_REFUSED:
1639 res->status = RSLV_STATUS_REFUSED;
1640 ns->counters.refused++;
1641 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001642
Christopher Faulet67957bd2017-09-27 11:00:59 +02001643 case DNS_RESP_ANCOUNT_ZERO:
1644 res->status = RSLV_STATUS_OTHER;
1645 ns->counters.any_err++;
1646 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001647
Christopher Faulet67957bd2017-09-27 11:00:59 +02001648 case DNS_RESP_CNAME_ERROR:
1649 res->status = RSLV_STATUS_OTHER;
1650 ns->counters.cname_error++;
1651 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001652
Christopher Faulet67957bd2017-09-27 11:00:59 +02001653 case DNS_RESP_TRUNCATED:
1654 res->status = RSLV_STATUS_OTHER;
1655 ns->counters.truncated++;
1656 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001657
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658 case DNS_RESP_NO_EXPECTED_RECORD:
1659 case DNS_RESP_ERROR:
1660 case DNS_RESP_INTERNAL:
1661 res->status = RSLV_STATUS_OTHER;
1662 ns->counters.other++;
1663 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001664 }
1665
Christopher Faulet67957bd2017-09-27 11:00:59 +02001666 /* Wait all nameservers response to handle errors */
1667 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1668 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001669
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670 /* Process error codes */
1671 if (dns_resp != DNS_RESP_VALID) {
1672 if (res->prefered_query_type != res->query_type) {
1673 /* The fallback on the query type was already performed,
1674 * so check the try counter. If it falls to 0, we can
1675 * report an error. Else, wait the next attempt. */
1676 if (!res->try)
1677 goto report_res_error;
1678 }
1679 else {
1680 /* Fallback from A to AAAA or the opposite and re-send
1681 * the resolution immediately. try counter is not
1682 * decremented. */
1683 if (res->prefered_query_type == DNS_RTYPE_A) {
1684 res->query_type = DNS_RTYPE_AAAA;
1685 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001686 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1688 res->query_type = DNS_RTYPE_A;
1689 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001690 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691 }
1692 continue;
1693 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001694
Christopher Faulet67957bd2017-09-27 11:00:59 +02001695 /* Now let's check the query's dname corresponds to the one we
1696 * sent. We can check only the first query of the list. We send
1697 * one query at a time so we get one query in the response */
1698 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1699 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1700 dns_resp = DNS_RESP_WRONG_NAME;
1701 ns->counters.other++;
1702 goto report_res_error;
1703 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001704
Christopher Faulet67957bd2017-09-27 11:00:59 +02001705 /* So the resolution succeeded */
1706 res->status = RSLV_STATUS_VALID;
1707 res->last_valid = now_ms;
1708 ns->counters.valid++;
1709 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001710
Christopher Faulet67957bd2017-09-27 11:00:59 +02001711 report_res_error:
1712 list_for_each_entry(req, &res->requesters, list)
1713 req->requester_error_cb(req, dns_resp);
1714 dns_reset_resolution(res);
1715 LIST_DEL(&res->list);
1716 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1717 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001718
Christopher Faulet67957bd2017-09-27 11:00:59 +02001719 report_res_success:
1720 /* Only the 1rst requester s managed by the server, others are
1721 * from the cache */
1722 tmpns = ns;
1723 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001724 struct server *s = objt_server(req->owner);
1725
1726 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001727 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001729 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001730 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001731 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001732 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001733
Christopher Faulet67957bd2017-09-27 11:00:59 +02001734 dns_reset_resolution(res);
1735 LIST_DEL(&res->list);
1736 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1737 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001738 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001739 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001740 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001741}
William Lallemand69e96442016-11-19 00:58:54 +01001742
Christopher Faulet67957bd2017-09-27 11:00:59 +02001743/* Called when a resolvers network socket is ready to send data */
1744static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001745{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001746 struct dns_resolvers *resolvers;
1747 struct dns_nameserver *ns;
1748 struct dns_resolution *res;
1749 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001750
Christopher Faulet67957bd2017-09-27 11:00:59 +02001751 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001752
Christopher Faulet67957bd2017-09-27 11:00:59 +02001753 /* check if ready for sending */
1754 if (!fd_send_ready(fd))
1755 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001756
Christopher Faulet67957bd2017-09-27 11:00:59 +02001757 /* we don't want/need to be waked up any more for sending */
1758 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001759
Christopher Faulet67957bd2017-09-27 11:00:59 +02001760 /* no need to go further if we can't retrieve the nameserver */
1761 if ((ns = dgram->owner) == NULL)
1762 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001763
Christopher Faulet67957bd2017-09-27 11:00:59 +02001764 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001765 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001766
Christopher Faulet67957bd2017-09-27 11:00:59 +02001767 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001768 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001769
Christopher Faulet67957bd2017-09-27 11:00:59 +02001770 if (res->nb_queries == resolvers->nb_nameservers)
1771 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001772
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001773 len = dns_build_query(res->query_id, res->query_type,
1774 resolvers->accepted_payload_size,
1775 res->hostname_dn, res->hostname_dn_len,
1776 trash.area, trash.size);
1777 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001778 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001779
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001780 ret = send(fd, trash.area, len, 0);
Willy Tarreau0eae6322019-12-20 11:18:54 +01001781 if (ret != len) {
1782 if (ret == -1 && errno == EAGAIN) {
1783 /* retry once the socket is ready */
1784 fd_cant_send(fd);
1785 continue;
1786 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001787 goto snd_error;
Willy Tarreau0eae6322019-12-20 11:18:54 +01001788 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001789
Christopher Faulet67957bd2017-09-27 11:00:59 +02001790 ns->counters.sent++;
1791 res->nb_queries++;
1792 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001793
Christopher Faulet67957bd2017-09-27 11:00:59 +02001794 snd_error:
1795 ns->counters.snd_error++;
1796 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001797 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001798 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001799}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001800
Christopher Faulet67957bd2017-09-27 11:00:59 +02001801/* Processes DNS resolution. First, it checks the active list to detect expired
1802 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1803 * checks the wait list to trigger new resolutions.
1804 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001805static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001806{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001807 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001808 struct dns_resolution *res, *resback;
1809 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001810
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001811 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001812
Christopher Faulet67957bd2017-09-27 11:00:59 +02001813 /* Handle all expired resolutions from the active list */
1814 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1815 /* When we find the first resolution in the future, then we can
1816 * stop here */
1817 exp = tick_add(res->last_query, resolvers->timeout.retry);
1818 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001819 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001820
Christopher Faulet67957bd2017-09-27 11:00:59 +02001821 /* If current resolution has been tried too many times and
1822 * finishes in timeout we update its status and remove it from
1823 * the list */
1824 if (!res->try) {
1825 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001826
Christopher Faulet67957bd2017-09-27 11:00:59 +02001827 /* Notify the result to the requesters */
1828 if (!res->nb_responses)
1829 res->status = RSLV_STATUS_TIMEOUT;
1830 list_for_each_entry(req, &res->requesters, list)
1831 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001832
Christopher Faulet67957bd2017-09-27 11:00:59 +02001833 /* Clean up resolution info and remove it from the
1834 * current list */
1835 dns_reset_resolution(res);
1836 LIST_DEL(&res->list);
1837 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001838 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001839 else {
1840 /* Otherwise resend the DNS query and requeue the resolution */
1841 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1842 /* No response received (a real timeout) or fallback already done */
1843 res->query_type = res->prefered_query_type;
1844 res->try--;
1845 }
1846 else {
1847 /* Fallback from A to AAAA or the opposite and re-send
1848 * the resolution immediately. try counter is not
1849 * decremented. */
1850 if (res->prefered_query_type == DNS_RTYPE_A)
1851 res->query_type = DNS_RTYPE_AAAA;
1852 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1853 res->query_type = DNS_RTYPE_A;
1854 else
1855 res->try--;
1856 }
1857 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001858 }
1859 }
William Lallemand69e96442016-11-19 00:58:54 +01001860
Christopher Faulet67957bd2017-09-27 11:00:59 +02001861 /* Handle all resolutions in the wait list */
1862 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1863 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1864 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1865 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 if (dns_run_resolution(res) != 1) {
1868 res->last_resolution = now_ms;
1869 LIST_DEL(&res->list);
1870 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001871 }
1872 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001873
Christopher Faulet67957bd2017-09-27 11:00:59 +02001874 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001875 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001876 return t;
1877}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001878
Christopher Faulet67957bd2017-09-27 11:00:59 +02001879/* proto_udp callback functions for a DNS resolution */
1880struct dgram_data_cb resolve_dgram_cb = {
1881 .recv = dns_resolve_recv,
1882 .send = dns_resolve_send,
1883};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001884
Christopher Faulet67957bd2017-09-27 11:00:59 +02001885/* Release memory allocated by DNS */
1886static void dns_deinit(void)
1887{
1888 struct dns_resolvers *resolvers, *resolversback;
1889 struct dns_nameserver *ns, *nsback;
1890 struct dns_resolution *res, *resback;
1891 struct dns_requester *req, *reqback;
1892 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001893
Christopher Faulet67957bd2017-09-27 11:00:59 +02001894 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1895 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1896 free(ns->id);
1897 free((char *)ns->conf.file);
1898 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1899 fd_delete(ns->dgram->t.sock.fd);
1900 free(ns->dgram);
1901 LIST_DEL(&ns->list);
1902 free(ns);
1903 }
1904
1905 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1906 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1907 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001908 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001909 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001910 dns_free_resolution(res);
1911 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001912
Christopher Faulet67957bd2017-09-27 11:00:59 +02001913 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1914 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1915 LIST_DEL(&req->list);
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +01001916 pool_free(dns_requester_pool, req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001917 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 dns_free_resolution(res);
1919 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001920
Christopher Faulet67957bd2017-09-27 11:00:59 +02001921 free(resolvers->id);
1922 free((char *)resolvers->conf.file);
Olivier Houchard3f795f72019-04-17 22:51:06 +02001923 task_destroy(resolvers->t);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001924 LIST_DEL(&resolvers->list);
1925 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001926 }
1927
Christopher Faulet67957bd2017-09-27 11:00:59 +02001928 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1929 free(srvrq->name);
1930 free(srvrq->hostname_dn);
1931 LIST_DEL(&srvrq->list);
1932 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001933 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001934}
1935
Christopher Faulet67957bd2017-09-27 11:00:59 +02001936/* Finalizes the DNS configuration by allocating required resources and checking
1937 * live parameters.
1938 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001939 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001941{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 struct dns_resolvers *resolvers;
1943 struct proxy *px;
1944 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001945
Christopher Faulet67957bd2017-09-27 11:00:59 +02001946 /* allocate pool of resolution per resolvers */
1947 list_for_each_entry(resolvers, &dns_resolvers, list) {
1948 struct dns_nameserver *ns;
1949 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001950
Christopher Faulet67957bd2017-09-27 11:00:59 +02001951 /* Check if we can create the socket with nameservers info */
1952 list_for_each_entry(ns, &resolvers->nameservers, list) {
1953 struct dgram_conn *dgram = NULL;
1954 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001955
Christopher Faulet67957bd2017-09-27 11:00:59 +02001956 /* Check nameserver info */
1957 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001958 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1959 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001961 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001962 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001963 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001964 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1965 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001966 close(fd);
1967 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001968 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001969 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001970 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001971
Christopher Faulet67957bd2017-09-27 11:00:59 +02001972 /* Create dgram structure that will hold the UPD socket
1973 * and attach it on the current nameserver */
1974 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001975 ha_alert("config: resolvers '%s' : out of memory.\n",
1976 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 err_code |= (ERR_ALERT|ERR_ABORT);
1978 goto err;
1979 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001980
Christopher Faulet67957bd2017-09-27 11:00:59 +02001981 /* Leave dgram partially initialized, no FD attached for
1982 * now. */
1983 dgram->owner = ns;
1984 dgram->data = &resolve_dgram_cb;
1985 dgram->t.sock.fd = -1;
1986 ns->dgram = dgram;
1987 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001988
Christopher Faulet67957bd2017-09-27 11:00:59 +02001989 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001990 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001991 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001992 err_code |= (ERR_ALERT|ERR_ABORT);
1993 goto err;
1994 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001995
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 /* Update task's parameters */
1997 t->process = dns_process_resolvers;
1998 t->context = resolvers;
1999 resolvers->t = t;
2000 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002001 }
2002
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002003 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002004 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002005
Christopher Faulet67957bd2017-09-27 11:00:59 +02002006 for (srv = px->srv; srv; srv = srv->next) {
2007 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002008
Christopher Faulet67957bd2017-09-27 11:00:59 +02002009 if (!srv->resolvers_id)
2010 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002011
Christopher Faulet67957bd2017-09-27 11:00:59 +02002012 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002013 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
2014 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002015 err_code |= (ERR_ALERT|ERR_ABORT);
2016 continue;
2017 }
2018 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002019
Christopher Faulet67957bd2017-09-27 11:00:59 +02002020 if (srv->srvrq && !srv->srvrq->resolvers) {
2021 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002022 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002023 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
2024 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002025 err_code |= (ERR_ALERT|ERR_ABORT);
2026 continue;
2027 }
2028 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01002029 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002030 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
2031 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002032 err_code |= (ERR_ALERT|ERR_ABORT);
2033 continue;
2034 }
2035 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002036 }
2037
Christopher Faulet67957bd2017-09-27 11:00:59 +02002038 if (err_code & (ERR_ALERT|ERR_ABORT))
2039 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002040
Christopher Faulet67957bd2017-09-27 11:00:59 +02002041 return err_code;
2042 err:
2043 dns_deinit();
2044 return err_code;
2045
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002046}
2047
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002049static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002050{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002051 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002052
Christopher Faulet67957bd2017-09-27 11:00:59 +02002053 if (*args[2]) {
2054 list_for_each_entry(presolvers, &dns_resolvers, list) {
2055 if (strcmp(presolvers->id, args[2]) == 0) {
2056 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002057 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002058 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002059 }
Willy Tarreau9d008692019-08-09 11:21:01 +02002060 if (appctx->ctx.cli.p0 == NULL)
2061 return cli_err(appctx, "Can't find that resolvers section\n");
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002062 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002063 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002064}
2065
Christopher Faulet67957bd2017-09-27 11:00:59 +02002066/* Dumps counters from all resolvers section and associated name servers. It
2067 * returns 0 if the output buffer is full and it needs to be called again,
2068 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01002069 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002070 */
2071static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2072{
2073 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002074 struct dns_resolvers *resolvers;
2075 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002076
2077 chunk_reset(&trash);
2078
2079 switch (appctx->st2) {
2080 case STAT_ST_INIT:
2081 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2082 /* fall through */
2083
2084 case STAT_ST_LIST:
2085 if (LIST_ISEMPTY(&dns_resolvers)) {
2086 chunk_appendf(&trash, "No resolvers found\n");
2087 }
2088 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002089 list_for_each_entry(resolvers, &dns_resolvers, list) {
2090 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002091 continue;
2092
Christopher Faulet67957bd2017-09-27 11:00:59 +02002093 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2094 list_for_each_entry(ns, &resolvers->nameservers, list) {
2095 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2096 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2097 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2098 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2099 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2100 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2101 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2102 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2103 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2104 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2105 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2106 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2107 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2108 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2109 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2110 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002111 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002112 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002113 }
2114 }
2115
2116 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002117 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002118 /* let's try again later from this session. We add ourselves into
2119 * this session's users so that it can remove us upon termination.
2120 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002121 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002122 return 0;
2123 }
William Lallemand69e96442016-11-19 00:58:54 +01002124 /* fall through */
2125
2126 default:
2127 appctx->st2 = STAT_ST_FIN;
2128 return 1;
2129 }
2130}
2131
2132/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002133static struct cli_kw_list cli_kws = {{ }, {
2134 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2135 " associated name servers",
2136 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2137 {{},}
2138 }
2139};
William Lallemand69e96442016-11-19 00:58:54 +01002140
Willy Tarreau0108d902018-11-25 19:14:37 +01002141INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002142
Baptiste Assmann333939c2019-01-21 08:34:50 +01002143/*
2144 * Prepare <rule> for hostname resolution.
2145 * Returns -1 in case of any allocation failure, 0 if not.
2146 * On error, a global failure counter is also incremented.
2147 */
2148static int action_prepare_for_resolution(struct stream *stream, const char *hostname)
2149{
2150 char *hostname_dn;
2151 int hostname_len, hostname_dn_len;
2152 struct buffer *tmp = get_trash_chunk();
2153
2154 if (!hostname)
2155 return 0;
2156
2157 hostname_len = strlen(hostname);
2158 hostname_dn = tmp->area;
2159 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
2160 hostname_dn, tmp->size);
2161 if (hostname_dn_len == -1)
2162 goto err;
2163
2164
2165 stream->dns_ctx.hostname_dn = strdup(hostname_dn);
2166 stream->dns_ctx.hostname_dn_len = hostname_dn_len;
2167 if (!stream->dns_ctx.hostname_dn)
2168 goto err;
2169
2170 return 0;
2171
2172 err:
2173 free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL;
2174 dns_failed_resolutions += 1;
2175 return -1;
2176}
2177
2178
2179/*
2180 * Execute the "do-resolution" action. May be called from {tcp,http}request.
2181 */
2182enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
2183 struct session *sess, struct stream *s, int flags)
2184{
Baptiste Assmann333939c2019-01-21 08:34:50 +01002185 struct dns_resolution *resolution;
Willy Tarreau45726fd2019-07-17 10:38:45 +02002186 struct sample *smp;
2187 char *fqdn;
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002188 struct dns_requester *req;
2189 struct dns_resolvers *resolvers;
2190 struct dns_resolution *res;
2191 int exp;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002192
2193 /* we have a response to our DNS resolution */
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002194 use_cache:
Baptiste Assmann333939c2019-01-21 08:34:50 +01002195 if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
2196 resolution = s->dns_ctx.dns_requester->resolution;
Baptiste Assmann4c52e4b2019-10-01 15:32:40 +02002197 if (resolution->step == RSLV_STEP_RUNNING) {
2198 return ACT_RET_YIELD;
2199 }
Baptiste Assmann333939c2019-01-21 08:34:50 +01002200 if (resolution->step == RSLV_STEP_NONE) {
2201 /* We update the variable only if we have a valid response. */
2202 if (resolution->status == RSLV_STATUS_VALID) {
2203 struct sample smp;
2204 short ip_sin_family = 0;
2205 void *ip = NULL;
2206
2207 dns_get_ip_from_response(&resolution->response, &rule->arg.dns.dns_opts, NULL,
2208 0, &ip, &ip_sin_family, NULL);
2209
2210 switch (ip_sin_family) {
2211 case AF_INET:
2212 smp.data.type = SMP_T_IPV4;
2213 memcpy(&smp.data.u.ipv4, ip, 4);
2214 break;
2215 case AF_INET6:
2216 smp.data.type = SMP_T_IPV6;
2217 memcpy(&smp.data.u.ipv6, ip, 16);
2218 break;
2219 default:
2220 ip = NULL;
2221 }
2222
2223 if (ip) {
2224 smp.px = px;
2225 smp.sess = sess;
2226 smp.strm = s;
2227
2228 vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp);
2229 }
2230 }
2231 }
2232
2233 free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL;
2234 s->dns_ctx.hostname_dn_len = 0;
2235 dns_unlink_resolution(s->dns_ctx.dns_requester);
2236
2237 pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
2238 s->dns_ctx.dns_requester = NULL;
2239
2240 return ACT_RET_CONT;
2241 }
2242
2243 /* need to configure and start a new DNS resolution */
Willy Tarreau45726fd2019-07-17 10:38:45 +02002244 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR);
2245 if (smp == NULL)
2246 return ACT_RET_CONT;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002247
Willy Tarreau45726fd2019-07-17 10:38:45 +02002248 fqdn = smp->data.u.str.area;
2249 if (action_prepare_for_resolution(s, fqdn) == -1)
2250 return ACT_RET_ERR;
Baptiste Assmann333939c2019-01-21 08:34:50 +01002251
Willy Tarreau45726fd2019-07-17 10:38:45 +02002252 s->dns_ctx.parent = rule;
2253 dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
Baptiste Assmann7264dfe2019-10-30 16:06:53 +01002254
2255 /* Check if there is a fresh enough response in the cache of our associated resolution */
2256 req = s->dns_ctx.dns_requester;
2257 if (!req || !req->resolution) {
2258 dns_trigger_resolution(s->dns_ctx.dns_requester);
2259 return ACT_RET_YIELD;
2260 }
2261 res = req->resolution;
2262 resolvers = res->resolvers;
2263
2264 exp = tick_add(res->last_resolution, resolvers->hold.valid);
2265 if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
2266 && !tick_is_expired(exp, now_ms)) {
2267 goto use_cache;
2268 }
2269
Willy Tarreau45726fd2019-07-17 10:38:45 +02002270 dns_trigger_resolution(s->dns_ctx.dns_requester);
Baptiste Assmann333939c2019-01-21 08:34:50 +01002271 return ACT_RET_YIELD;
2272}
2273
2274
2275/* parse "do-resolve" action
2276 * This action takes the following arguments:
2277 * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr>
2278 *
2279 * - <varName> is the variable name where the result of the DNS resolution will be stored
2280 * (mandatory)
2281 * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution
2282 * (mandatory)
2283 * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first
2284 * (optional), defaults to ipv6
2285 * - <expr> is an HAProxy expression used to fetch the name to be resolved
2286 */
2287enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
2288{
2289 int cur_arg;
2290 struct sample_expr *expr;
2291 unsigned int where;
2292 const char *beg, *end;
2293
2294 /* orig_arg points to the first argument, but we need to analyse the command itself first */
2295 cur_arg = *orig_arg - 1;
2296
2297 /* locate varName, which is mandatory */
2298 beg = strchr(args[cur_arg], '(');
2299 if (beg == NULL)
2300 goto do_resolve_parse_error;
2301 beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */
2302 end = strchr(beg, ',');
2303 if (end == NULL)
2304 goto do_resolve_parse_error;
2305 rule->arg.dns.varname = my_strndup(beg, end - beg);
2306 if (rule->arg.dns.varname == NULL)
2307 goto do_resolve_parse_error;
2308
2309
2310 /* locate resolversSectionName, which is mandatory.
2311 * Since next parameters are optional, the delimiter may be comma ','
2312 * or closing parenthesis ')'
2313 */
2314 beg = end + 1;
2315 end = strchr(beg, ',');
2316 if (end == NULL)
2317 end = strchr(beg, ')');
2318 if (end == NULL)
2319 goto do_resolve_parse_error;
2320 rule->arg.dns.resolvers_id = my_strndup(beg, end - beg);
2321 if (rule->arg.dns.resolvers_id == NULL)
2322 goto do_resolve_parse_error;
2323
2324
2325 /* Default priority is ipv6 */
2326 rule->arg.dns.dns_opts.family_prio = AF_INET6;
2327
2328 /* optional arguments accepted for now:
2329 * ipv4 or ipv6
2330 */
2331 while (*end != ')') {
2332 beg = end + 1;
2333 end = strchr(beg, ',');
2334 if (end == NULL)
2335 end = strchr(beg, ')');
2336 if (end == NULL)
2337 goto do_resolve_parse_error;
2338
2339 if (strncmp(beg, "ipv4", end - beg) == 0) {
2340 rule->arg.dns.dns_opts.family_prio = AF_INET;
2341 }
2342 else if (strncmp(beg, "ipv6", end - beg) == 0) {
2343 rule->arg.dns.dns_opts.family_prio = AF_INET6;
2344 }
2345 else {
2346 goto do_resolve_parse_error;
2347 }
2348 }
2349
2350 cur_arg = cur_arg + 1;
2351
2352 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
2353 if (!expr)
2354 goto do_resolve_parse_error;
2355
2356
2357 where = 0;
2358 if (px->cap & PR_CAP_FE)
2359 where |= SMP_VAL_FE_HRQ_HDR;
2360 if (px->cap & PR_CAP_BE)
2361 where |= SMP_VAL_BE_HRQ_HDR;
2362
2363 if (!(expr->fetch->val & where)) {
2364 memprintf(err,
2365 "fetch method '%s' extracts information from '%s', none of which is available here",
2366 args[cur_arg-1], sample_src_names(expr->fetch->use));
2367 free(expr);
2368 return ACT_RET_PRS_ERR;
2369 }
2370 rule->arg.dns.expr = expr;
2371 rule->action = ACT_CUSTOM;
2372 rule->action_ptr = dns_action_do_resolve;
2373 *orig_arg = cur_arg;
2374
2375 rule->check_ptr = check_action_do_resolve;
2376
2377 return ACT_RET_PRS_OK;
2378
2379 do_resolve_parse_error:
2380 free(rule->arg.dns.varname); rule->arg.dns.varname = NULL;
2381 free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL;
2382 memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'",
2383 args[cur_arg]);
2384 return ACT_RET_PRS_ERR;
2385}
2386
2387static struct action_kw_list http_req_kws = { { }, {
2388 { "do-resolve", dns_parse_do_resolve, 1 },
2389 { /* END */ }
2390}};
2391
2392INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
2393
2394static struct action_kw_list tcp_req_cont_actions = {ILH, {
2395 { "do-resolve", dns_parse_do_resolve, 1 },
2396 { /* END */ }
2397}};
2398
2399INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
2400
2401/* Check an "http-request do-resolve" action.
2402 *
2403 * The function returns 1 in success case, otherwise, it returns 0 and err is
2404 * filled.
2405 */
2406int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
2407{
2408 struct dns_resolvers *resolvers = NULL;
2409
2410 if (rule->arg.dns.resolvers_id == NULL) {
2411 memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers");
2412 return 0;
2413 }
2414
2415 resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id);
2416 if (resolvers == NULL) {
2417 memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id);
2418 return 0;
2419 }
2420 rule->arg.dns.resolvers = resolvers;
2421
2422 return 1;
2423}
2424
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002425REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002426REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);