blob: fead2613ad7bc4258bc8925e2c3aa9a46f462a67 [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
William Lallemand69e96442016-11-19 00:58:54 +010029#include <types/applet.h>
30#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020031#include <types/global.h>
32#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010033#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020034
William Lallemand69e96442016-11-19 00:58:54 +010035#include <proto/channel.h>
36#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020037#include <proto/checks.h>
38#include <proto/dns.h>
39#include <proto/fd.h>
40#include <proto/log.h>
41#include <proto/server.h>
42#include <proto/task.h>
43#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020044#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010045#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020046
Christopher Faulet67957bd2017-09-27 11:00:59 +020047struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
48struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020049
Christopher Fauletb2812a62017-10-04 16:17:58 +020050static THREAD_LOCAL int64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010051
52DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
53DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
54
Christopher Faulet67957bd2017-09-27 11:00:59 +020055static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020056
Christopher Faulet67957bd2017-09-27 11:00:59 +020057/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
58 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020059 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020060struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020061{
Christopher Faulet67957bd2017-09-27 11:00:59 +020062 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063
Christopher Faulet67957bd2017-09-27 11:00:59 +020064 list_for_each_entry(res, &dns_resolvers, list) {
65 if (!strcmp(res->id, id))
66 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020067 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020068 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069}
70
Christopher Faulet67957bd2017-09-27 11:00:59 +020071/* Returns a pointer on the SRV request matching the name <name> for the proxy
72 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020074struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020075{
Christopher Faulet67957bd2017-09-27 11:00:59 +020076 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077
Christopher Faulet67957bd2017-09-27 11:00:59 +020078 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
79 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
80 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020081 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020082 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020083}
84
Christopher Faulet67957bd2017-09-27 11:00:59 +020085/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
86 * NULL if an error occurred. */
87struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020088{
Christopher Faulet67957bd2017-09-27 11:00:59 +020089 struct proxy *px = srv->proxy;
90 struct dns_srvrq *srvrq = NULL;
91 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020092
Christopher Faulet67957bd2017-09-27 11:00:59 +020093 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020094 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
95 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +020096 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010097 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
98 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +020099 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200100 }
101
Christopher Faulet67957bd2017-09-27 11:00:59 +0200102 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100103 ha_alert("config : %s '%s', server '%s': out of memory\n",
104 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200105 goto err;
106 }
107 srvrq->obj_type = OBJ_TYPE_SRVRQ;
108 srvrq->proxy = px;
109 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200110 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200111 srvrq->hostname_dn_len = hostname_dn_len;
112 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100113 ha_alert("config : %s '%s', server '%s': out of memory\n",
114 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 goto err;
116 }
117 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
118 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200119
Christopher Faulet67957bd2017-09-27 11:00:59 +0200120 err:
121 if (srvrq) {
122 free(srvrq->name);
123 free(srvrq->hostname_dn);
124 free(srvrq);
125 }
126 return NULL;
127}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200128
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200129
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130/* 2 bytes random generator to generate DNS query ID */
131static inline uint16_t dns_rnd16(void)
132{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200133 if (!dns_query_id_seed)
134 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200135 dns_query_id_seed ^= dns_query_id_seed << 13;
136 dns_query_id_seed ^= dns_query_id_seed >> 7;
137 dns_query_id_seed ^= dns_query_id_seed << 17;
138 return dns_query_id_seed;
139}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200140
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200141
Christopher Faulet67957bd2017-09-27 11:00:59 +0200142static inline int dns_resolution_timeout(struct dns_resolution *res)
143{
144 switch (res->status) {
145 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
146 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200147 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200148}
149
Christopher Faulet67957bd2017-09-27 11:00:59 +0200150/* Updates a resolvers' task timeout for next wake up and queue it */
151static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200152{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200153 struct dns_resolution *res;
154 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200155
Christopher Faulet67957bd2017-09-27 11:00:59 +0200156 next = tick_add(now_ms, resolvers->timeout.resolve);
157 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
158 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
159 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
160 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200161
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162 list_for_each_entry(res, &resolvers->resolutions.wait, list)
163 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200164
Christopher Faulet67957bd2017-09-27 11:00:59 +0200165 resolvers->t->expire = next;
166 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200167}
168
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
170 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200174 struct dgram_conn *dgram = ns->dgram;
175 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200176
Christopher Faulet67957bd2017-09-27 11:00:59 +0200177 /* Already connected */
178 if (dgram->t.sock.fd != -1)
179 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200180
Christopher Faulet67957bd2017-09-27 11:00:59 +0200181 /* Create an UDP socket and connect it on the nameserver's IP/Port */
182 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
183 send_log(NULL, LOG_WARNING,
184 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
185 ns->resolvers->id, ns->id);
186 return -1;
187 }
188 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
189 send_log(NULL, LOG_WARNING,
190 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
191 ns->resolvers->id, ns->id);
192 close(fd);
193 return -1;
194 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200195
Christopher Faulet67957bd2017-09-27 11:00:59 +0200196 /* Make the socket non blocking */
197 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200198
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 /* Add the fd in the fd list and update its parameters */
200 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100201 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202 fd_want_recv(fd);
203 return 0;
204}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200205
Christopher Faulet67957bd2017-09-27 11:00:59 +0200206/* Forges a DNS query. It needs the following information from the caller:
207 * - <query_id> : the DNS query id corresponding to this query
208 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
209 * - <hostname_dn> : hostname in domain name format
210 * - <hostname_dn_len> : length of <hostname_dn>
211 *
212 * To store the query, the caller must pass a buffer <buf> and its size
213 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
214 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200215 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200216static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
217 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200218{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200219 struct dns_header dns_hdr;
220 struct dns_question qinfo;
221 struct dns_additional_record edns;
222 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
225 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200226
Christopher Faulet67957bd2017-09-27 11:00:59 +0200227 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200228
Christopher Faulet67957bd2017-09-27 11:00:59 +0200229 /* Set dns query headers */
230 dns_hdr.id = (unsigned short) htons(query_id);
231 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
232 dns_hdr.qdcount = htons(1); /* 1 question */
233 dns_hdr.ancount = 0;
234 dns_hdr.nscount = 0;
235 dns_hdr.arcount = htons(1);
236 memcpy(p, &dns_hdr, sizeof(dns_hdr));
237 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200238
Christopher Faulet67957bd2017-09-27 11:00:59 +0200239 /* Set up query hostname */
240 memcpy(p, hostname_dn, hostname_dn_len);
241 p += hostname_dn_len;
242 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200243
Christopher Faulet67957bd2017-09-27 11:00:59 +0200244 /* Set up query info (type and class) */
245 qinfo.qtype = htons(query_type);
246 qinfo.qclass = htons(DNS_RCLASS_IN);
247 memcpy(p, &qinfo, sizeof(qinfo));
248 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200249
Christopher Faulet67957bd2017-09-27 11:00:59 +0200250 /* Set the DNS extension */
251 edns.name = 0;
252 edns.type = htons(DNS_RTYPE_OPT);
253 edns.udp_payload_size = htons(accepted_payload_size);
254 edns.extension = 0;
255 edns.data_length = 0;
256 memcpy(p, &edns, sizeof(edns));
257 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200258
Christopher Faulet67957bd2017-09-27 11:00:59 +0200259 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200260}
261
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
263 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200264 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200267 struct dns_resolvers *resolvers = resolution->resolvers;
268 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200269
Christopher Faulet67957bd2017-09-27 11:00:59 +0200270 list_for_each_entry(ns, &resolvers->nameservers, list) {
271 int fd = ns->dgram->t.sock.fd;
272 if (fd == -1) {
273 if (dns_connect_namesaver(ns) == -1)
274 continue;
275 fd = ns->dgram->t.sock.fd;
276 resolvers->nb_nameservers++;
277 }
278 fd_want_send(fd);
279 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281 /* Update resolution */
282 resolution->nb_queries = 0;
283 resolution->nb_responses = 0;
284 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200285
Christopher Faulet67957bd2017-09-27 11:00:59 +0200286 /* Push the resolution at the end of the active list */
287 LIST_DEL(&resolution->list);
288 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
289 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200290}
291
Christopher Faulet67957bd2017-09-27 11:00:59 +0200292/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
293 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200294 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295static int
296dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200297{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298 struct dns_resolvers *resolvers = resolution->resolvers;
299 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200300
Christopher Faulet67957bd2017-09-27 11:00:59 +0200301 /* Avoid sending requests for resolutions that don't yet have an
302 * hostname, ie resolutions linked to servers that do not yet have an
303 * fqdn */
304 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200305 return 0;
306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307 /* Check if a resolution has already been started for this server return
308 * directly to avoid resolution pill up. */
309 if (resolution->step != RSLV_STEP_NONE)
310 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200311
Christopher Faulet67957bd2017-09-27 11:00:59 +0200312 /* Generates a new query id. We try at most 100 times to find a free
313 * query id */
314 for (i = 0; i < 100; ++i) {
315 query_id = dns_rnd16();
316 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200317 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200318 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200319 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200320 if (query_id == -1) {
321 send_log(NULL, LOG_NOTICE,
322 "could not generate a query id for %s, in resolvers %s.\n",
323 resolution->hostname_dn, resolvers->id);
324 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200325 }
326
Christopher Faulet67957bd2017-09-27 11:00:59 +0200327 /* Update resolution parameters */
328 resolution->query_id = query_id;
329 resolution->qid.key = query_id;
330 resolution->step = RSLV_STEP_RUNNING;
331 resolution->query_type = resolution->prefered_query_type;
332 resolution->try = resolvers->resolve_retries;
333 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200334
Christopher Faulet67957bd2017-09-27 11:00:59 +0200335 /* Send the DNS query */
336 resolution->try -= 1;
337 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338 return 1;
339}
340
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341/* Performs a name resolution for the requester <req> */
342void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200343{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200344 struct dns_resolvers *resolvers;
345 struct dns_resolution *res;
346 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200347
Christopher Faulet67957bd2017-09-27 11:00:59 +0200348 if (!req || !req->resolution)
349 return;
350 res = req->resolution;
351 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200352
Christopher Faulet67957bd2017-09-27 11:00:59 +0200353 /* The resolution must not be triggered yet. Use the cached response, if
354 * valid */
355 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200356 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
357 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
358 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200360
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200361
Christopher Faulet67957bd2017-09-27 11:00:59 +0200362/* Resets some resolution parameters to initial values and also delete the query
363 * ID from the resolver's tree.
364 */
365static void dns_reset_resolution(struct dns_resolution *resolution)
366{
367 /* update resolution status */
368 resolution->step = RSLV_STEP_NONE;
369 resolution->try = 0;
370 resolution->last_resolution = now_ms;
371 resolution->nb_queries = 0;
372 resolution->nb_responses = 0;
373 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200374
Christopher Faulet67957bd2017-09-27 11:00:59 +0200375 /* clean up query id */
376 eb32_delete(&resolution->qid);
377 resolution->query_id = 0;
378 resolution->qid.key = 0;
379}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200380
Christopher Faulet67957bd2017-09-27 11:00:59 +0200381/* Returns the query id contained in a DNS response */
382static inline unsigned short dns_response_get_query_id(unsigned char *resp)
383{
384 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200385}
386
Christopher Faulet67957bd2017-09-27 11:00:59 +0200387
388/* Analyses, re-builds and copies the name <name> from the DNS response packet
389 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
390 * for compressed data. The result is copied into <dest>, ensuring we don't
391 * overflow using <dest_len> Returns the number of bytes the caller can move
392 * forward. If 0 it means an error occurred while parsing the name. <offset> is
393 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200394 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200395int dns_read_name(unsigned char *buffer, unsigned char *bufend,
396 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100397 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200398{
399 int nb_bytes = 0, n = 0;
400 int label_len;
401 unsigned char *reader = name;
402 char *dest = destination;
403
404 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100405 if (reader >= bufend)
406 goto err;
407
Christopher Faulet67957bd2017-09-27 11:00:59 +0200408 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200409 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100410 if (reader + 1 >= bufend)
411 goto err;
412
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 /* Must point BEFORE current position */
414 if ((buffer + reader[1]) > reader)
415 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200416
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100417 if (depth++ > 100)
418 goto err;
419
Christopher Faulet67957bd2017-09-27 11:00:59 +0200420 n = dns_read_name(buffer, bufend, buffer + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100421 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200422 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200423 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200424
Christopher Faulet67957bd2017-09-27 11:00:59 +0200425 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200426 nb_bytes += n;
427 goto out;
428 }
429
430 label_len = *reader;
431 if (label_len == 0)
432 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200433
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200434 /* Check if:
435 * - we won't read outside the buffer
436 * - there is enough place in the destination
437 */
438 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200440
441 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200442 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200443
444 memcpy(dest, reader, label_len);
445
Christopher Faulet67957bd2017-09-27 11:00:59 +0200446 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200447 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200448 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200449 }
450
Christopher Faulet67957bd2017-09-27 11:00:59 +0200451 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200452 /* offset computation:
453 * parse from <name> until finding either NULL or a pointer "c0xx"
454 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200455 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200456 *offset = 0;
457 while (reader < bufend) {
458 if ((reader[0] & 0xc0) == 0xc0) {
459 *offset += 2;
460 break;
461 }
462 else if (*reader == 0) {
463 *offset += 1;
464 break;
465 }
466 *offset += 1;
467 ++reader;
468 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200469 return nb_bytes;
470
Christopher Faulet67957bd2017-09-27 11:00:59 +0200471 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200472 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200473}
474
Christopher Faulet67957bd2017-09-27 11:00:59 +0200475/* Checks for any obsolete record, also identify any SRV request, and try to
476 * find a corresponding server.
477*/
478static void dns_check_dns_response(struct dns_resolution *res)
479{
480 struct dns_resolvers *resolvers = res->resolvers;
481 struct dns_requester *req, *reqback;
482 struct dns_answer_item *item, *itemback;
483 struct server *srv;
484 struct dns_srvrq *srvrq;
485
486 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
487
488 /* Remove obsolete items */
489 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
490 if (item->type != DNS_RTYPE_SRV)
491 goto rm_obselete_item;
492
493 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
494 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
495 continue;
496
497 /* Remove any associated server */
498 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100499 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200500 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
501 item->data_len == srv->hostname_dn_len &&
502 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
503 snr_update_srv_status(srv, 1);
504 free(srv->hostname);
505 free(srv->hostname_dn);
506 srv->hostname = NULL;
507 srv->hostname_dn = NULL;
508 srv->hostname_dn_len = 0;
509 dns_unlink_resolution(srv->dns_requester);
510 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100511 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200512 }
513 }
514
515 rm_obselete_item:
516 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100517 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200518 continue;
519 }
520
521 if (item->type != DNS_RTYPE_SRV)
522 continue;
523
524 /* Now process SRV records */
525 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
526 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
527 continue;
528
529 /* Check if a server already uses that hostname */
530 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100531 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200532 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
533 item->data_len == srv->hostname_dn_len &&
534 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100535 int ha_weight;
536
537 /* Make sure weight is at least 1, so
538 * that the server will be used.
539 */
540 ha_weight = item->weight / 256 + 1;
541 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200542 char weight[9];
543
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100544 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200545 server_parse_weight_change_request(srv, weight);
546 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200548 break;
549 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100550 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551 }
552 if (srv)
553 continue;
554
555 /* If not, try to find a server with undefined hostname */
556 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100557 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200558 if (srv->srvrq == srvrq && !srv->hostname_dn)
559 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200561 }
562 /* And update this server, if found */
563 if (srv) {
564 const char *msg = NULL;
565 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100566 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200567 char hostname[DNS_MAX_NAME_SIZE];
568
569 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100570 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100571 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200572 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100573 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100574 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200575 if (msg)
576 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
577
578 srv->svc_port = item->port;
579 srv->flags &= ~SRV_F_MAPPORTS;
580 if ((srv->check.state & CHK_ST_CONFIGURED) &&
581 !(srv->flags & SRV_F_CHECKPORT))
582 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100583
584 /* Make sure weight is at least 1, so
585 * that the server will be used.
586 */
587 ha_weight = item->weight / 256 + 1;
588
589 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200590 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100591 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200592 }
593 }
594 }
595}
596
597/* Validates that the buffer DNS response provided in <resp> and finishing
598 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200599 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200600 * The result is stored in <resolution>' response, buf_response,
601 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200602 *
603 * This function returns one of the DNS_RESP_* code to indicate the type of
604 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200605 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200606static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
607 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200608{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200609 unsigned char *reader;
610 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200611 int len, flags, offset;
612 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200613 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200614 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200615 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200616 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200617 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618
Christopher Faulet67957bd2017-09-27 11:00:59 +0200619 reader = resp;
620 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200621 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200622 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200623
Christopher Faulet67957bd2017-09-27 11:00:59 +0200624 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200625 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200626
627 /* query id */
628 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200629 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200630 dns_p->header.id = reader[0] * 256 + reader[1];
631 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200632
Christopher Faulet67957bd2017-09-27 11:00:59 +0200633 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200634 * First byte contains:
635 * - response flag (1 bit)
636 * - opcode (4 bits)
637 * - authoritative (1 bit)
638 * - truncated (1 bit)
639 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200640 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200641 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200642 return DNS_RESP_INVALID;
643
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200644 flags = reader[0] * 256 + reader[1];
645
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200646 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
647 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200648 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200649 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200650 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200651 return DNS_RESP_ERROR;
652 }
653
Christopher Faulet67957bd2017-09-27 11:00:59 +0200654 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200655 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200656
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200657 /* 2 bytes for question count */
658 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200659 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200660 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200661 /* (for now) we send one query only, so we expect only one in the
662 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200663 if (dns_p->header.qdcount != 1)
664 return DNS_RESP_QUERY_COUNT_ERROR;
665 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200666 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200667 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200668
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 /* 2 bytes for answer count */
670 if (reader + 2 >= bufend)
671 return DNS_RESP_INVALID;
672 dns_p->header.ancount = reader[0] * 256 + reader[1];
673 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200674 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200675 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200676 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200677 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200678 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200679
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200680 /* 2 bytes authority count */
681 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200682 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200683 dns_p->header.nscount = reader[0] * 256 + reader[1];
684 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200685
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200686 /* 2 bytes additional count */
687 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200688 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200689 dns_p->header.arcount = reader[0] * 256 + reader[1];
690 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200691
Christopher Faulet67957bd2017-09-27 11:00:59 +0200692 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200693 LIST_INIT(&dns_p->query_list);
694 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 +0200695 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200697 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200698 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
699 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200700 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200701 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200702
Christopher Faulet67957bd2017-09-27 11:00:59 +0200703 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200704 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200705 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200706 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100707 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200708
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200709 if (len == 0)
710 return DNS_RESP_INVALID;
711
712 reader += offset;
713 previous_dname = dns_query->name;
714
715 /* move forward 2 bytes for question type */
716 if (reader + 2 >= bufend)
717 return DNS_RESP_INVALID;
718 dns_query->type = reader[0] * 256 + reader[1];
719 reader += 2;
720
721 /* move forward 2 bytes for question class */
722 if (reader + 2 >= bufend)
723 return DNS_RESP_INVALID;
724 dns_query->class = reader[0] * 256 + reader[1];
725 reader += 2;
726 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200727
Baptiste Assmann251abb92017-08-11 09:58:27 +0200728 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200729 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200730 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
731 return DNS_RESP_TRUNCATED;
732
Baptiste Assmann325137d2015-04-13 23:40:55 +0200733 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200734 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200735 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200736 if (reader >= bufend)
737 return DNS_RESP_INVALID;
738
Willy Tarreaubafbe012017-11-24 17:34:44 +0100739 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200740 if (dns_answer_record == NULL)
741 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200742
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200743 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100744 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200745
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200746 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100747 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200748 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200749 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200750
Christopher Faulet67957bd2017-09-27 11:00:59 +0200751 /* Check if the current record dname is valid. previous_dname
752 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200753 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100754 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200755 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200756 /* First record, means a mismatch issue between
757 * queried dname and dname found in the first
758 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200759 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200760 }
761 else {
762 /* If not the first record, this means we have a
763 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200765 }
766
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767 }
768
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200769 memcpy(dns_answer_record->name, tmpname, len);
770 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200771
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200772 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200773 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100774 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200775 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200776 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200777
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200778 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200779 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100780 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200781 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200782 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200783 dns_answer_record->type = reader[0] * 256 + reader[1];
784 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200785
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200786 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200787 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100788 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200789 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200790 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200791 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200792 reader += 2;
793
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200794 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200795 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100796 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200797 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200798 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200799 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
800 + reader[2] * 256 + reader[3];
801 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200802
Christopher Faulet67957bd2017-09-27 11:00:59 +0200803 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200804 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100805 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200806 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200807 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200808 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200809
Christopher Faulet67957bd2017-09-27 11:00:59 +0200810 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200811 reader += 2;
812
Christopher Faulet67957bd2017-09-27 11:00:59 +0200813 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200814 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200815 case DNS_RTYPE_A:
816 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200817 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100818 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200819 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200820 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200821 dns_answer_record->address.sa_family = AF_INET;
822 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
823 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200824 break;
825
826 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200827 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200828 * no IP could be found and last record was a CNAME. Could be triggered
829 * by a wrong query type
830 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200831 * + 1 because dns_answer_record_id starts at 0
832 * while number of answers is an integer and
833 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200834 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200835 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100836 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200837 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200838 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200839
840 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100841 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200842 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100843 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200844 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200845 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200846
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200847 memcpy(dns_answer_record->target, tmpname, len);
848 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200849 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200850 break;
851
Olivier Houchard8da5f982017-08-04 18:35:36 +0200852
853 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200854 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200855 * - 2 bytes for the priority
856 * - 2 bytes for the weight
857 * - 2 bytes for the port
858 * - the target hostname
859 */
860 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100861 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200862 return DNS_RESP_INVALID;
863 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200864 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200865 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200866 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200867 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200868 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200869 reader += sizeof(uint16_t);
870 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100871 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200872 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100873 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200874 return DNS_RESP_INVALID;
875 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200876 dns_answer_record->data_len = len;
877 memcpy(dns_answer_record->target, tmpname, len);
878 dns_answer_record->target[len] = 0;
879 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200880
Baptiste Assmann325137d2015-04-13 23:40:55 +0200881 case DNS_RTYPE_AAAA:
882 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200883 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100884 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200885 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200886 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200887 dns_answer_record->address.sa_family = AF_INET6;
888 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
889 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200890 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200891
Baptiste Assmann325137d2015-04-13 23:40:55 +0200892 } /* switch (record type) */
893
Christopher Faulet67957bd2017-09-27 11:00:59 +0200894 /* Increment the counter for number of records saved into our
895 * local response */
896 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200897
Christopher Faulet67957bd2017-09-27 11:00:59 +0200898 /* Move forward dns_answer_record->data_len for analyzing next
899 * record in the response */
900 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
901 ? offset
902 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200903
904 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200905 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200906 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
907 if (tmp_record->type != dns_answer_record->type)
908 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200909
910 switch(tmp_record->type) {
911 case DNS_RTYPE_A:
912 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
913 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
914 sizeof(in_addr_t)))
915 found = 1;
916 break;
917
918 case DNS_RTYPE_AAAA:
919 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
920 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
921 sizeof(struct in6_addr)))
922 found = 1;
923 break;
924
Olivier Houchard8da5f982017-08-04 18:35:36 +0200925 case DNS_RTYPE_SRV:
926 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200927 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200928 dns_answer_record->port == tmp_record->port) {
929 tmp_record->weight = dns_answer_record->weight;
930 found = 1;
931 }
932 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200933
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200934 default:
935 break;
936 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200937
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200938 if (found == 1)
939 break;
940 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200941
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200942 if (found == 1) {
943 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100944 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200945 }
946 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200947 dns_answer_record->last_seen = now.tv_sec;
948 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
949 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200950 } /* for i 0 to ancount */
951
Christopher Faulet67957bd2017-09-27 11:00:59 +0200952 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200953 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200954 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200955 return DNS_RESP_VALID;
956}
957
Christopher Faulet67957bd2017-09-27 11:00:59 +0200958/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200959 * If existing IP not found, return the first IP matching family_priority,
960 * otherwise, first ip found
961 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200962 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200963 * For both cases above, dns_validate_dns_response is required
964 * returns one of the DNS_UPD_* code
965 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200966int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200967 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100968 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200969 void **newip, short *newip_sin_family,
970 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200971{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200972 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100973 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200974 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200975 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100976 int currentip_sel;
977 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100978 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200979 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200980
Christopher Faulet67957bd2017-09-27 11:00:59 +0200981 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200982 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200983 *newip = newip4 = newip6 = NULL;
984 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200985 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200986 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200987
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100988 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -0800989 * Top priority is the preferred network ip version,
990 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100991 * the last priority is the currently used IP,
992 *
993 * For these three priorities, a score is calculated. The
994 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -0800995 * 8 - preferred ip version.
996 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +0100997 * 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 +0100998 * 1 - current ip.
999 * The result with the biggest score is returned.
1000 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001001
1002 list_for_each_entry(record, &dns_p->answer_list, list) {
1003 void *ip;
1004 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001005
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001006 if (record->type == DNS_RTYPE_A) {
1007 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1008 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001009 }
1010 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001011 ip_type = AF_INET6;
1012 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001013 }
1014 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001015 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001016 score = 0;
1017
Joseph Herlant42cf6392018-11-15 10:33:28 -08001018 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001019 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001020 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001021
Joseph Herlant42cf6392018-11-15 10:33:28 -08001022 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001023 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001024
1025 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001026 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001027 continue;
1028
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001029 if ((ip_type == AF_INET &&
1030 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001031 &dns_opts->pref_net[j].mask.in4,
1032 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001033 (ip_type == AF_INET6 &&
1034 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001035 &dns_opts->pref_net[j].mask.in6,
1036 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001037 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001038 break;
1039 }
1040 }
1041
Christopher Faulet67957bd2017-09-27 11:00:59 +02001042 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001043 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001044 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001045 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001046 if (!allowed_duplicated_ip) {
1047 continue;
1048 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001049 } else {
1050 score += 2;
1051 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001052
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001053 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001054 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001055 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001056 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001057 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001058 !memcmp(ip, currentip, 16)))) {
1059 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001060 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001061 }
1062 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001063 currentip_sel = 0;
1064
1065 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001066 * score. The maximum score is 15, if this value is reached, we
1067 * break the parsing. Implicitly, this score is reached the ip
1068 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001069 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001070 if (ip_type == AF_INET)
1071 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001072 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001073 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001074 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001075 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001076 return DNS_UPD_NO;
1077 max_score = score;
1078 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001079 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001080
Christopher Faulet67957bd2017-09-27 11:00:59 +02001081 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001082 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001083 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001084
Christopher Faulet67957bd2017-09-27 11:00:59 +02001085 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001086 if (family_priority == AF_INET) {
1087 if (newip4) {
1088 *newip = newip4;
1089 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001090 }
1091 else if (newip6) {
1092 *newip = newip6;
1093 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001094 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001095 if (!currentip_found)
1096 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001097 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001098 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001099 else if (family_priority == AF_INET6) {
1100 if (newip6) {
1101 *newip = newip6;
1102 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001103 }
1104 else if (newip4) {
1105 *newip = newip4;
1106 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001108 if (!currentip_found)
1109 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001110 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001111 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 else if (family_priority == AF_UNSPEC) {
1113 if (newip6) {
1114 *newip = newip6;
1115 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001116 }
1117 else if (newip4) {
1118 *newip = newip4;
1119 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001120 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001121 if (!currentip_found)
1122 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001123 }
1124
Christopher Faulet67957bd2017-09-27 11:00:59 +02001125 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001126 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001127
Christopher Faulet67957bd2017-09-27 11:00:59 +02001128 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001129 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001130 /* Move the first record to the end of the list, for internal
1131 * round robin */
1132 LIST_DEL(&record->list);
1133 LIST_ADDQ(&dns_p->answer_list, &record->list);
1134 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001135 }
1136 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137}
1138
Christopher Faulet67957bd2017-09-27 11:00:59 +02001139/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001140 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001141 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1142 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001143 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001144 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1145 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001146 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001147int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001148{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001149 char *ptr;
1150 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001151
Christopher Faulet67957bd2017-09-27 11:00:59 +02001152 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001153 return -1;
1154
Christopher Faulet67957bd2017-09-27 11:00:59 +02001155 ptr = str;
1156 for (i = 0; i < dn_len-1; ++i) {
1157 sz = dn[i];
1158 if (i)
1159 *ptr++ = '.';
1160 memcpy(ptr, dn+i+1, sz);
1161 ptr += sz;
1162 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001163 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001164 *ptr++ = '\0';
1165 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001166}
1167
Christopher Faulet67957bd2017-09-27 11:00:59 +02001168/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1169 *
1170 * <str> must be a null-terminated string. <str_len> must include the
1171 * terminating null byte. <dn> buffer must be allocated and its size must be
1172 * passed in <dn_len>.
1173 *
1174 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1175 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001176 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001177int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001178{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001179 int i, offset;
1180
Christopher Faulet67957bd2017-09-27 11:00:59 +02001181 if (dn_len < str_len + 1)
1182 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001183
Christopher Faulet67957bd2017-09-27 11:00:59 +02001184 /* First byte of dn will be used to store the length of the first
1185 * label */
1186 offset = 0;
1187 for (i = 0; i < str_len; ++i) {
1188 if (str[i] == '.') {
1189 /* 2 or more consecutive dots is invalid */
1190 if (i == offset)
1191 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001192
Christopher Faulet67957bd2017-09-27 11:00:59 +02001193 dn[offset] = (i - offset);
1194 offset = i+1;
1195 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001196 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001197 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001198 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001199 dn[offset] = (i - offset - 1);
1200 dn[i] = '\0';
1201 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001202}
1203
Christopher Faulet67957bd2017-09-27 11:00:59 +02001204/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001205 * - total size
1206 * - each label size individually
1207 * returns:
1208 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1209 * 1 when no error. <err> is left unaffected.
1210 */
1211int dns_hostname_validation(const char *string, char **err)
1212{
1213 const char *c, *d;
1214 int i;
1215
1216 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1217 if (err)
1218 *err = DNS_TOO_LONG_FQDN;
1219 return 0;
1220 }
1221
1222 c = string;
1223 while (*c) {
1224 d = c;
1225
1226 i = 0;
1227 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1228 i++;
1229 if (!((*d == '-') || (*d == '_') ||
1230 ((*d >= 'a') && (*d <= 'z')) ||
1231 ((*d >= 'A') && (*d <= 'Z')) ||
1232 ((*d >= '0') && (*d <= '9')))) {
1233 if (err)
1234 *err = DNS_INVALID_CHARACTER;
1235 return 0;
1236 }
1237 d++;
1238 }
1239
1240 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1241 if (err)
1242 *err = DNS_LABEL_TOO_LONG;
1243 return 0;
1244 }
1245
1246 if (*d == '\0')
1247 goto out;
1248
1249 c = ++d;
1250 }
1251 out:
1252 return 1;
1253}
1254
Christopher Faulet67957bd2017-09-27 11:00:59 +02001255/* Picks up an available resolution from the different resolution list
1256 * associated to a resolvers section, in this order:
1257 * 1. check in resolutions.curr for the same hostname and query_type
1258 * 2. check in resolutions.wait for the same hostname and query_type
1259 * 3. Get a new resolution from resolution pool
1260 *
1261 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001262 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001263static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1264 char **hostname_dn, int hostname_dn_len,
1265 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001266{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001267 struct dns_resolution *res;
1268
1269 if (!*hostname_dn)
1270 goto from_pool;
1271
1272 /* Search for same hostname and query type in resolutions.curr */
1273 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1274 if (!res->hostname_dn)
1275 continue;
1276 if ((query_type == res->prefered_query_type) &&
1277 hostname_dn_len == res->hostname_dn_len &&
1278 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1279 return res;
1280 }
1281
1282 /* Search for same hostname and query type in resolutions.wait */
1283 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1284 if (!res->hostname_dn)
1285 continue;
1286 if ((query_type == res->prefered_query_type) &&
1287 hostname_dn_len == res->hostname_dn_len &&
1288 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1289 return res;
1290 }
1291
1292 from_pool:
1293 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001294 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001295 if (res) {
1296 memset(res, 0, sizeof(*res));
1297 res->resolvers = resolvers;
1298 res->uuid = resolution_uuid;
1299 res->status = RSLV_STATUS_NONE;
1300 res->step = RSLV_STEP_NONE;
1301 res->last_valid = now_ms;
1302
1303 LIST_INIT(&res->requesters);
1304 LIST_INIT(&res->response.answer_list);
1305
1306 res->prefered_query_type = query_type;
1307 res->query_type = query_type;
1308 res->hostname_dn = *hostname_dn;
1309 res->hostname_dn_len = hostname_dn_len;
1310
1311 ++resolution_uuid;
1312
1313 /* Move the resolution to the resolvers wait queue */
1314 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1315 }
1316 return res;
1317}
1318
1319/* Releases a resolution from its requester(s) and move it back to the pool */
1320static void dns_free_resolution(struct dns_resolution *resolution)
1321{
1322 struct dns_requester *req, *reqback;
1323
1324 /* clean up configuration */
1325 dns_reset_resolution(resolution);
1326 resolution->hostname_dn = NULL;
1327 resolution->hostname_dn_len = 0;
1328
1329 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1330 LIST_DEL(&req->list);
1331 req->resolution = NULL;
1332 }
1333
1334 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001335 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001336}
1337
1338/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1339 * on success, -1 otherwise.
1340 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001341int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001342{
1343 struct dns_resolution *res = NULL;
1344 struct dns_requester *req;
1345 struct dns_resolvers *resolvers;
1346 struct server *srv = NULL;
1347 struct dns_srvrq *srvrq = NULL;
1348 char **hostname_dn;
1349 int hostname_dn_len, query_type;
1350
1351 switch (requester_type) {
1352 case OBJ_TYPE_SERVER:
1353 srv = (struct server *)requester;
1354 hostname_dn = &srv->hostname_dn;
1355 hostname_dn_len = srv->hostname_dn_len;
1356 resolvers = srv->resolvers;
1357 query_type = ((srv->dns_opts.family_prio == AF_INET)
1358 ? DNS_RTYPE_A
1359 : DNS_RTYPE_AAAA);
1360 break;
1361
1362 case OBJ_TYPE_SRVRQ:
1363 srvrq = (struct dns_srvrq *)requester;
1364 hostname_dn = &srvrq->hostname_dn;
1365 hostname_dn_len = srvrq->hostname_dn_len;
1366 resolvers = srvrq->resolvers;
1367 query_type = DNS_RTYPE_SRV;
1368 break;
1369
1370 default:
1371 goto err;
1372 }
1373
1374 /* Get a resolution from the resolvers' wait queue or pool */
1375 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1376 goto err;
1377
1378 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001379 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001380 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001381 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001382 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001383 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001384 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001386 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001387 req->owner = &srv->obj_type;
1388 srv->dns_requester = req;
1389 }
1390 else
1391 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001392 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001393 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001394 }
1395 else if (srvrq) {
1396 if (srvrq->dns_requester == NULL) {
1397 if ((req = calloc(1, sizeof(*req))) == NULL)
1398 goto err;
1399 req->owner = &srvrq->obj_type;
1400 srvrq->dns_requester = req;
1401 }
1402 else
1403 req = srvrq->dns_requester;
1404 }
1405 else
1406 goto err;
1407
1408 req->resolution = res;
1409 req->requester_cb = snr_resolution_cb;
1410 req->requester_error_cb = snr_resolution_error_cb;
1411
1412 LIST_ADDQ(&res->requesters, &req->list);
1413 return 0;
1414
1415 err:
1416 if (res && LIST_ISEMPTY(&res->requesters))
1417 dns_free_resolution(res);
1418 return -1;
1419}
1420
1421/* Removes a requester from a DNS resoltion. It takes takes care of all the
1422 * consequences. It also cleans up some parameters from the requester.
1423 */
1424void dns_unlink_resolution(struct dns_requester *requester)
1425{
1426 struct dns_resolution *res;
1427 struct dns_requester *req;
1428
1429 /* Nothing to do */
1430 if (!requester || !requester->resolution)
1431 return;
1432 res = requester->resolution;
1433
1434 /* Clean up the requester */
1435 LIST_DEL(&requester->list);
1436 requester->resolution = NULL;
1437
1438 /* We need to find another requester linked on this resolution */
1439 if (!LIST_ISEMPTY(&res->requesters))
1440 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1441 else {
1442 dns_free_resolution(res);
1443 return;
1444 }
1445
1446 /* Move hostname_dn related pointers to the next requester */
1447 switch (obj_type(req->owner)) {
1448 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001449 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1450 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001451 break;
1452 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001453 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1454 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001455 break;
1456 default:
1457 res->hostname_dn = NULL;
1458 res->hostname_dn_len = 0;
1459 break;
1460 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001461}
1462
Christopher Faulet67957bd2017-09-27 11:00:59 +02001463/* Called when a network IO is generated on a name server socket for an incoming
1464 * packet. It performs the following actions:
1465 * - check if the packet requires processing (not outdated resolution)
1466 * - ensure the DNS packet received is valid and call requester's callback
1467 * - call requester's error callback if invalid response
1468 * - check the dn_name in the packet against the one sent
1469 */
1470static void dns_resolve_recv(struct dgram_conn *dgram)
1471{
1472 struct dns_nameserver *ns, *tmpns;
1473 struct dns_resolvers *resolvers;
1474 struct dns_resolution *res;
1475 struct dns_query_item *query;
1476 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1477 unsigned char *bufend;
1478 int fd, buflen, dns_resp;
1479 int max_answer_records;
1480 unsigned short query_id;
1481 struct eb32_node *eb;
1482 struct dns_requester *req;
1483
1484 fd = dgram->t.sock.fd;
1485
1486 /* check if ready for reading */
1487 if (!fd_recv_ready(fd))
1488 return;
1489
1490 /* no need to go further if we can't retrieve the nameserver */
1491 if ((ns = dgram->owner) == NULL)
1492 return;
1493
1494 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001495 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001496
1497 /* process all pending input messages */
1498 while (1) {
1499 /* read message received */
1500 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1501 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1502 /* FIXME : for now we consider EAGAIN only */
1503 fd_cant_recv(fd);
1504 break;
1505 }
1506
1507 /* message too big */
1508 if (buflen > resolvers->accepted_payload_size) {
1509 ns->counters.too_big++;
1510 continue;
1511 }
1512
1513 /* initializing variables */
1514 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1515
1516 /* read the query id from the packet (16 bits) */
1517 if (buf + 2 > bufend) {
1518 ns->counters.invalid++;
1519 continue;
1520 }
1521 query_id = dns_response_get_query_id(buf);
1522
1523 /* search the query_id in the pending resolution tree */
1524 eb = eb32_lookup(&resolvers->query_ids, query_id);
1525 if (eb == NULL) {
1526 /* unknown query id means an outdated response and can be safely ignored */
1527 ns->counters.outdated++;
1528 continue;
1529 }
1530
1531 /* known query id means a resolution in prgress */
1532 res = eb32_entry(eb, struct dns_resolution, qid);
1533 if (!res) {
1534 ns->counters.outdated++;
1535 continue;
1536 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001537
Christopher Faulet67957bd2017-09-27 11:00:59 +02001538 /* number of responses received */
1539 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001540
Christopher Faulet67957bd2017-09-27 11:00:59 +02001541 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1542 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001543
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 switch (dns_resp) {
1545 case DNS_RESP_VALID:
1546 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001547
Christopher Faulet67957bd2017-09-27 11:00:59 +02001548 case DNS_RESP_INVALID:
1549 case DNS_RESP_QUERY_COUNT_ERROR:
1550 case DNS_RESP_WRONG_NAME:
1551 res->status = RSLV_STATUS_INVALID;
1552 ns->counters.invalid++;
1553 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001554
Christopher Faulet67957bd2017-09-27 11:00:59 +02001555 case DNS_RESP_NX_DOMAIN:
1556 res->status = RSLV_STATUS_NX;
1557 ns->counters.nx++;
1558 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001559
Christopher Faulet67957bd2017-09-27 11:00:59 +02001560 case DNS_RESP_REFUSED:
1561 res->status = RSLV_STATUS_REFUSED;
1562 ns->counters.refused++;
1563 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001564
Christopher Faulet67957bd2017-09-27 11:00:59 +02001565 case DNS_RESP_ANCOUNT_ZERO:
1566 res->status = RSLV_STATUS_OTHER;
1567 ns->counters.any_err++;
1568 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001569
Christopher Faulet67957bd2017-09-27 11:00:59 +02001570 case DNS_RESP_CNAME_ERROR:
1571 res->status = RSLV_STATUS_OTHER;
1572 ns->counters.cname_error++;
1573 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001574
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575 case DNS_RESP_TRUNCATED:
1576 res->status = RSLV_STATUS_OTHER;
1577 ns->counters.truncated++;
1578 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001579
Christopher Faulet67957bd2017-09-27 11:00:59 +02001580 case DNS_RESP_NO_EXPECTED_RECORD:
1581 case DNS_RESP_ERROR:
1582 case DNS_RESP_INTERNAL:
1583 res->status = RSLV_STATUS_OTHER;
1584 ns->counters.other++;
1585 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001586 }
1587
Christopher Faulet67957bd2017-09-27 11:00:59 +02001588 /* Wait all nameservers response to handle errors */
1589 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1590 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001591
Christopher Faulet67957bd2017-09-27 11:00:59 +02001592 /* Process error codes */
1593 if (dns_resp != DNS_RESP_VALID) {
1594 if (res->prefered_query_type != res->query_type) {
1595 /* The fallback on the query type was already performed,
1596 * so check the try counter. If it falls to 0, we can
1597 * report an error. Else, wait the next attempt. */
1598 if (!res->try)
1599 goto report_res_error;
1600 }
1601 else {
1602 /* Fallback from A to AAAA or the opposite and re-send
1603 * the resolution immediately. try counter is not
1604 * decremented. */
1605 if (res->prefered_query_type == DNS_RTYPE_A) {
1606 res->query_type = DNS_RTYPE_AAAA;
1607 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001608 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001609 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1610 res->query_type = DNS_RTYPE_A;
1611 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001612 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001613 }
1614 continue;
1615 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001616
Christopher Faulet67957bd2017-09-27 11:00:59 +02001617 /* Now let's check the query's dname corresponds to the one we
1618 * sent. We can check only the first query of the list. We send
1619 * one query at a time so we get one query in the response */
1620 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1621 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1622 dns_resp = DNS_RESP_WRONG_NAME;
1623 ns->counters.other++;
1624 goto report_res_error;
1625 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001626
Christopher Faulet67957bd2017-09-27 11:00:59 +02001627 /* So the resolution succeeded */
1628 res->status = RSLV_STATUS_VALID;
1629 res->last_valid = now_ms;
1630 ns->counters.valid++;
1631 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001632
Christopher Faulet67957bd2017-09-27 11:00:59 +02001633 report_res_error:
1634 list_for_each_entry(req, &res->requesters, list)
1635 req->requester_error_cb(req, dns_resp);
1636 dns_reset_resolution(res);
1637 LIST_DEL(&res->list);
1638 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1639 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001640
Christopher Faulet67957bd2017-09-27 11:00:59 +02001641 report_res_success:
1642 /* Only the 1rst requester s managed by the server, others are
1643 * from the cache */
1644 tmpns = ns;
1645 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001646 struct server *s = objt_server(req->owner);
1647
1648 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001649 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001650 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001651 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001652 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001653 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001654 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001655
Christopher Faulet67957bd2017-09-27 11:00:59 +02001656 dns_reset_resolution(res);
1657 LIST_DEL(&res->list);
1658 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1659 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001660 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001661 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001662 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001663}
William Lallemand69e96442016-11-19 00:58:54 +01001664
Christopher Faulet67957bd2017-09-27 11:00:59 +02001665/* Called when a resolvers network socket is ready to send data */
1666static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001667{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 struct dns_resolvers *resolvers;
1669 struct dns_nameserver *ns;
1670 struct dns_resolution *res;
1671 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001672
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001674
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675 /* check if ready for sending */
1676 if (!fd_send_ready(fd))
1677 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001678
Christopher Faulet67957bd2017-09-27 11:00:59 +02001679 /* we don't want/need to be waked up any more for sending */
1680 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001681
Christopher Faulet67957bd2017-09-27 11:00:59 +02001682 /* no need to go further if we can't retrieve the nameserver */
1683 if ((ns = dgram->owner) == NULL)
1684 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001685
Christopher Faulet67957bd2017-09-27 11:00:59 +02001686 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001687 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001688
Christopher Faulet67957bd2017-09-27 11:00:59 +02001689 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001690 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001691
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 if (res->nb_queries == resolvers->nb_nameservers)
1693 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001694
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001695 len = dns_build_query(res->query_id, res->query_type,
1696 resolvers->accepted_payload_size,
1697 res->hostname_dn, res->hostname_dn_len,
1698 trash.area, trash.size);
1699 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001700 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001701
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001702 ret = send(fd, trash.area, len, 0);
1703 if (ret != len)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001704 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001705
Christopher Faulet67957bd2017-09-27 11:00:59 +02001706 ns->counters.sent++;
1707 res->nb_queries++;
1708 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001709
Christopher Faulet67957bd2017-09-27 11:00:59 +02001710 snd_error:
1711 ns->counters.snd_error++;
1712 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001713 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001714 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001715}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001716
Christopher Faulet67957bd2017-09-27 11:00:59 +02001717/* Processes DNS resolution. First, it checks the active list to detect expired
1718 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1719 * checks the wait list to trigger new resolutions.
1720 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001721static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001722{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001723 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001724 struct dns_resolution *res, *resback;
1725 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001726
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001727 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001728
Christopher Faulet67957bd2017-09-27 11:00:59 +02001729 /* Handle all expired resolutions from the active list */
1730 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1731 /* When we find the first resolution in the future, then we can
1732 * stop here */
1733 exp = tick_add(res->last_query, resolvers->timeout.retry);
1734 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001735 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001736
Christopher Faulet67957bd2017-09-27 11:00:59 +02001737 /* If current resolution has been tried too many times and
1738 * finishes in timeout we update its status and remove it from
1739 * the list */
1740 if (!res->try) {
1741 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001742
Christopher Faulet67957bd2017-09-27 11:00:59 +02001743 /* Notify the result to the requesters */
1744 if (!res->nb_responses)
1745 res->status = RSLV_STATUS_TIMEOUT;
1746 list_for_each_entry(req, &res->requesters, list)
1747 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001748
Christopher Faulet67957bd2017-09-27 11:00:59 +02001749 /* Clean up resolution info and remove it from the
1750 * current list */
1751 dns_reset_resolution(res);
1752 LIST_DEL(&res->list);
1753 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001754 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001755 else {
1756 /* Otherwise resend the DNS query and requeue the resolution */
1757 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1758 /* No response received (a real timeout) or fallback already done */
1759 res->query_type = res->prefered_query_type;
1760 res->try--;
1761 }
1762 else {
1763 /* Fallback from A to AAAA or the opposite and re-send
1764 * the resolution immediately. try counter is not
1765 * decremented. */
1766 if (res->prefered_query_type == DNS_RTYPE_A)
1767 res->query_type = DNS_RTYPE_AAAA;
1768 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1769 res->query_type = DNS_RTYPE_A;
1770 else
1771 res->try--;
1772 }
1773 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001774 }
1775 }
William Lallemand69e96442016-11-19 00:58:54 +01001776
Christopher Faulet67957bd2017-09-27 11:00:59 +02001777 /* Handle all resolutions in the wait list */
1778 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1779 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1780 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1781 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001782
Christopher Faulet67957bd2017-09-27 11:00:59 +02001783 if (dns_run_resolution(res) != 1) {
1784 res->last_resolution = now_ms;
1785 LIST_DEL(&res->list);
1786 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001787 }
1788 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001789
Christopher Faulet67957bd2017-09-27 11:00:59 +02001790 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001791 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001792 return t;
1793}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001794
Christopher Faulet67957bd2017-09-27 11:00:59 +02001795/* proto_udp callback functions for a DNS resolution */
1796struct dgram_data_cb resolve_dgram_cb = {
1797 .recv = dns_resolve_recv,
1798 .send = dns_resolve_send,
1799};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001800
Christopher Faulet67957bd2017-09-27 11:00:59 +02001801/* Release memory allocated by DNS */
1802static void dns_deinit(void)
1803{
1804 struct dns_resolvers *resolvers, *resolversback;
1805 struct dns_nameserver *ns, *nsback;
1806 struct dns_resolution *res, *resback;
1807 struct dns_requester *req, *reqback;
1808 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001809
Christopher Faulet67957bd2017-09-27 11:00:59 +02001810 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1811 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1812 free(ns->id);
1813 free((char *)ns->conf.file);
1814 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1815 fd_delete(ns->dgram->t.sock.fd);
1816 free(ns->dgram);
1817 LIST_DEL(&ns->list);
1818 free(ns);
1819 }
1820
1821 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1822 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1823 LIST_DEL(&req->list);
1824 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001825 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001826 dns_free_resolution(res);
1827 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001828
Christopher Faulet67957bd2017-09-27 11:00:59 +02001829 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1830 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1831 LIST_DEL(&req->list);
1832 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001833 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001834 dns_free_resolution(res);
1835 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001836
Christopher Faulet67957bd2017-09-27 11:00:59 +02001837 free(resolvers->id);
1838 free((char *)resolvers->conf.file);
1839 task_delete(resolvers->t);
1840 task_free(resolvers->t);
1841 LIST_DEL(&resolvers->list);
1842 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001843 }
1844
Christopher Faulet67957bd2017-09-27 11:00:59 +02001845 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1846 free(srvrq->name);
1847 free(srvrq->hostname_dn);
1848 LIST_DEL(&srvrq->list);
1849 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001850 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001851}
1852
Christopher Faulet67957bd2017-09-27 11:00:59 +02001853/* Finalizes the DNS configuration by allocating required resources and checking
1854 * live parameters.
1855 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001856 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001857static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001858{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001859 struct dns_resolvers *resolvers;
1860 struct proxy *px;
1861 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001862
Christopher Faulet67957bd2017-09-27 11:00:59 +02001863 /* allocate pool of resolution per resolvers */
1864 list_for_each_entry(resolvers, &dns_resolvers, list) {
1865 struct dns_nameserver *ns;
1866 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001867
Christopher Faulet67957bd2017-09-27 11:00:59 +02001868 /* Check if we can create the socket with nameservers info */
1869 list_for_each_entry(ns, &resolvers->nameservers, list) {
1870 struct dgram_conn *dgram = NULL;
1871 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001872
Christopher Faulet67957bd2017-09-27 11:00:59 +02001873 /* Check nameserver info */
1874 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001875 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1876 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001877 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001878 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001879 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001880 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001881 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1882 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001883 close(fd);
1884 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001885 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001886 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001887 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001888
Christopher Faulet67957bd2017-09-27 11:00:59 +02001889 /* Create dgram structure that will hold the UPD socket
1890 * and attach it on the current nameserver */
1891 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001892 ha_alert("config: resolvers '%s' : out of memory.\n",
1893 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001894 err_code |= (ERR_ALERT|ERR_ABORT);
1895 goto err;
1896 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001897
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 /* Leave dgram partially initialized, no FD attached for
1899 * now. */
1900 dgram->owner = ns;
1901 dgram->data = &resolve_dgram_cb;
1902 dgram->t.sock.fd = -1;
1903 ns->dgram = dgram;
1904 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001905
Christopher Faulet67957bd2017-09-27 11:00:59 +02001906 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001907 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001908 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001909 err_code |= (ERR_ALERT|ERR_ABORT);
1910 goto err;
1911 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001912
Christopher Faulet67957bd2017-09-27 11:00:59 +02001913 /* Update task's parameters */
1914 t->process = dns_process_resolvers;
1915 t->context = resolvers;
1916 resolvers->t = t;
1917 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001918 }
1919
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001920 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001921 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001922
Christopher Faulet67957bd2017-09-27 11:00:59 +02001923 for (srv = px->srv; srv; srv = srv->next) {
1924 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001925
Christopher Faulet67957bd2017-09-27 11:00:59 +02001926 if (!srv->resolvers_id)
1927 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001928
Christopher Faulet67957bd2017-09-27 11:00:59 +02001929 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001930 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1931 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 err_code |= (ERR_ALERT|ERR_ABORT);
1933 continue;
1934 }
1935 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001936
Christopher Faulet67957bd2017-09-27 11:00:59 +02001937 if (srv->srvrq && !srv->srvrq->resolvers) {
1938 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001939 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001940 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1941 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 err_code |= (ERR_ALERT|ERR_ABORT);
1943 continue;
1944 }
1945 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001946 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001947 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1948 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001949 err_code |= (ERR_ALERT|ERR_ABORT);
1950 continue;
1951 }
1952 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001953 }
1954
Christopher Faulet67957bd2017-09-27 11:00:59 +02001955 if (err_code & (ERR_ALERT|ERR_ABORT))
1956 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001957
Christopher Faulet67957bd2017-09-27 11:00:59 +02001958 return err_code;
1959 err:
1960 dns_deinit();
1961 return err_code;
1962
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001963}
1964
Christopher Faulet67957bd2017-09-27 11:00:59 +02001965/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001966static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001967{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001969
Christopher Faulet67957bd2017-09-27 11:00:59 +02001970 if (*args[2]) {
1971 list_for_each_entry(presolvers, &dns_resolvers, list) {
1972 if (strcmp(presolvers->id, args[2]) == 0) {
1973 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001974 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001976 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 if (appctx->ctx.cli.p0 == NULL) {
1978 appctx->ctx.cli.severity = LOG_ERR;
1979 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1980 appctx->st0 = CLI_ST_PRINT;
1981 return 1;
1982 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001983 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001984 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001985}
1986
Christopher Faulet67957bd2017-09-27 11:00:59 +02001987/* Dumps counters from all resolvers section and associated name servers. It
1988 * returns 0 if the output buffer is full and it needs to be called again,
1989 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001990 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001991 */
1992static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1993{
1994 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001995 struct dns_resolvers *resolvers;
1996 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001997
1998 chunk_reset(&trash);
1999
2000 switch (appctx->st2) {
2001 case STAT_ST_INIT:
2002 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2003 /* fall through */
2004
2005 case STAT_ST_LIST:
2006 if (LIST_ISEMPTY(&dns_resolvers)) {
2007 chunk_appendf(&trash, "No resolvers found\n");
2008 }
2009 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002010 list_for_each_entry(resolvers, &dns_resolvers, list) {
2011 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002012 continue;
2013
Christopher Faulet67957bd2017-09-27 11:00:59 +02002014 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2015 list_for_each_entry(ns, &resolvers->nameservers, list) {
2016 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2017 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2018 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2019 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2020 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2021 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2022 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2023 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2024 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2025 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2026 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2027 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2028 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2029 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2030 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2031 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002032 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002033 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002034 }
2035 }
2036
2037 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002038 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002039 /* let's try again later from this session. We add ourselves into
2040 * this session's users so that it can remove us upon termination.
2041 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002042 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002043 return 0;
2044 }
William Lallemand69e96442016-11-19 00:58:54 +01002045 /* fall through */
2046
2047 default:
2048 appctx->st2 = STAT_ST_FIN;
2049 return 1;
2050 }
2051}
2052
2053/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002054static struct cli_kw_list cli_kws = {{ }, {
2055 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2056 " associated name servers",
2057 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2058 {{},}
2059 }
2060};
William Lallemand69e96442016-11-19 00:58:54 +01002061
Willy Tarreau0108d902018-11-25 19:14:37 +01002062INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002063
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002064REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002065REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);