blob: 2a53c038c377d394371f7f7b535c2a0b236232c4 [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,
397 int *offset)
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) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200405 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200406 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200407 /* Must point BEFORE current position */
408 if ((buffer + reader[1]) > reader)
409 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200410
Christopher Faulet67957bd2017-09-27 11:00:59 +0200411 n = dns_read_name(buffer, bufend, buffer + reader[1],
412 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200413 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200414 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200415
Christopher Faulet67957bd2017-09-27 11:00:59 +0200416 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200417 nb_bytes += n;
418 goto out;
419 }
420
421 label_len = *reader;
422 if (label_len == 0)
423 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200424
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200425 /* Check if:
426 * - we won't read outside the buffer
427 * - there is enough place in the destination
428 */
429 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200430 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200431
432 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200433 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200434
435 memcpy(dest, reader, label_len);
436
Christopher Faulet67957bd2017-09-27 11:00:59 +0200437 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200438 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200440 }
441
Christopher Faulet67957bd2017-09-27 11:00:59 +0200442 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200443 /* offset computation:
444 * parse from <name> until finding either NULL or a pointer "c0xx"
445 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200446 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200447 *offset = 0;
448 while (reader < bufend) {
449 if ((reader[0] & 0xc0) == 0xc0) {
450 *offset += 2;
451 break;
452 }
453 else if (*reader == 0) {
454 *offset += 1;
455 break;
456 }
457 *offset += 1;
458 ++reader;
459 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200460 return nb_bytes;
461
Christopher Faulet67957bd2017-09-27 11:00:59 +0200462 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200463 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200464}
465
Christopher Faulet67957bd2017-09-27 11:00:59 +0200466/* Checks for any obsolete record, also identify any SRV request, and try to
467 * find a corresponding server.
468*/
469static void dns_check_dns_response(struct dns_resolution *res)
470{
471 struct dns_resolvers *resolvers = res->resolvers;
472 struct dns_requester *req, *reqback;
473 struct dns_answer_item *item, *itemback;
474 struct server *srv;
475 struct dns_srvrq *srvrq;
476
477 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
478
479 /* Remove obsolete items */
480 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
481 if (item->type != DNS_RTYPE_SRV)
482 goto rm_obselete_item;
483
484 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
485 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
486 continue;
487
488 /* Remove any associated server */
489 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100490 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200491 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
492 item->data_len == srv->hostname_dn_len &&
493 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
494 snr_update_srv_status(srv, 1);
495 free(srv->hostname);
496 free(srv->hostname_dn);
497 srv->hostname = NULL;
498 srv->hostname_dn = NULL;
499 srv->hostname_dn_len = 0;
500 dns_unlink_resolution(srv->dns_requester);
501 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100502 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200503 }
504 }
505
506 rm_obselete_item:
507 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100508 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200509 continue;
510 }
511
512 if (item->type != DNS_RTYPE_SRV)
513 continue;
514
515 /* Now process SRV records */
516 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
517 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
518 continue;
519
520 /* Check if a server already uses that hostname */
521 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100522 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200523 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
524 item->data_len == srv->hostname_dn_len &&
525 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100526 int ha_weight;
527
528 /* Make sure weight is at least 1, so
529 * that the server will be used.
530 */
531 ha_weight = item->weight / 256 + 1;
532 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200533 char weight[9];
534
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100535 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200536 server_parse_weight_change_request(srv, weight);
537 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100538 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200539 break;
540 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100541 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200542 }
543 if (srv)
544 continue;
545
546 /* If not, try to find a server with undefined hostname */
547 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100548 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200549 if (srv->srvrq == srvrq && !srv->hostname_dn)
550 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100551 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200552 }
553 /* And update this server, if found */
554 if (srv) {
555 const char *msg = NULL;
556 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100557 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200558 char hostname[DNS_MAX_NAME_SIZE];
559
560 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100561 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100562 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200563 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100564 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100565 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200566 if (msg)
567 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
568
569 srv->svc_port = item->port;
570 srv->flags &= ~SRV_F_MAPPORTS;
571 if ((srv->check.state & CHK_ST_CONFIGURED) &&
572 !(srv->flags & SRV_F_CHECKPORT))
573 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100574
575 /* Make sure weight is at least 1, so
576 * that the server will be used.
577 */
578 ha_weight = item->weight / 256 + 1;
579
580 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200581 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100582 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200583 }
584 }
585 }
586}
587
588/* Validates that the buffer DNS response provided in <resp> and finishing
589 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200590 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200591 * The result is stored in <resolution>' response, buf_response,
592 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200593 *
594 * This function returns one of the DNS_RESP_* code to indicate the type of
595 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200596 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200597static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
598 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200599{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200600 unsigned char *reader;
601 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200602 int len, flags, offset;
603 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200604 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200605 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200606 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200607 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200608 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200609
Christopher Faulet67957bd2017-09-27 11:00:59 +0200610 reader = resp;
611 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200612 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200613 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200614
Christopher Faulet67957bd2017-09-27 11:00:59 +0200615 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200616 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200617
618 /* query id */
619 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200620 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200621 dns_p->header.id = reader[0] * 256 + reader[1];
622 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200623
Christopher Faulet67957bd2017-09-27 11:00:59 +0200624 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200625 * First byte contains:
626 * - response flag (1 bit)
627 * - opcode (4 bits)
628 * - authoritative (1 bit)
629 * - truncated (1 bit)
630 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200631 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200632 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200633 return DNS_RESP_INVALID;
634
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200635 flags = reader[0] * 256 + reader[1];
636
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200637 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
638 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200639 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200640 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200641 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200642 return DNS_RESP_ERROR;
643 }
644
Christopher Faulet67957bd2017-09-27 11:00:59 +0200645 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200646 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200647
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200648 /* 2 bytes for question count */
649 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200650 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200651 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200652 /* (for now) we send one query only, so we expect only one in the
653 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200654 if (dns_p->header.qdcount != 1)
655 return DNS_RESP_QUERY_COUNT_ERROR;
656 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200657 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200658 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200659
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200660 /* 2 bytes for answer count */
661 if (reader + 2 >= bufend)
662 return DNS_RESP_INVALID;
663 dns_p->header.ancount = reader[0] * 256 + reader[1];
664 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200665 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200666 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200667 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200668 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200670
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200671 /* 2 bytes authority count */
672 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200673 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200674 dns_p->header.nscount = reader[0] * 256 + reader[1];
675 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200676
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200677 /* 2 bytes additional count */
678 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200679 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200680 dns_p->header.arcount = reader[0] * 256 + reader[1];
681 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200682
Christopher Faulet67957bd2017-09-27 11:00:59 +0200683 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 LIST_INIT(&dns_p->query_list);
685 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 +0200686 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200687 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200688 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200689 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
690 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200691 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200692 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200693
Christopher Faulet67957bd2017-09-27 11:00:59 +0200694 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200695 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200696 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200697 offset = 0;
698 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200699
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200700 if (len == 0)
701 return DNS_RESP_INVALID;
702
703 reader += offset;
704 previous_dname = dns_query->name;
705
706 /* move forward 2 bytes for question type */
707 if (reader + 2 >= bufend)
708 return DNS_RESP_INVALID;
709 dns_query->type = reader[0] * 256 + reader[1];
710 reader += 2;
711
712 /* move forward 2 bytes for question class */
713 if (reader + 2 >= bufend)
714 return DNS_RESP_INVALID;
715 dns_query->class = reader[0] * 256 + reader[1];
716 reader += 2;
717 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200718
Baptiste Assmann251abb92017-08-11 09:58:27 +0200719 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200720 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200721 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
722 return DNS_RESP_TRUNCATED;
723
Baptiste Assmann325137d2015-04-13 23:40:55 +0200724 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200725 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200726 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200727 if (reader >= bufend)
728 return DNS_RESP_INVALID;
729
Willy Tarreaubafbe012017-11-24 17:34:44 +0100730 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200731 if (dns_answer_record == NULL)
732 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200733
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200734 offset = 0;
735 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200736
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200737 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100738 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200739 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200740 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200741
Christopher Faulet67957bd2017-09-27 11:00:59 +0200742 /* Check if the current record dname is valid. previous_dname
743 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200744 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100745 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200746 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200747 /* First record, means a mismatch issue between
748 * queried dname and dname found in the first
749 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200750 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200751 }
752 else {
753 /* If not the first record, this means we have a
754 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200755 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200756 }
757
Baptiste Assmann325137d2015-04-13 23:40:55 +0200758 }
759
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200760 memcpy(dns_answer_record->name, tmpname, len);
761 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200762
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200763 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200764 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100765 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200766 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200767 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200768
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200769 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200770 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100771 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200772 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200773 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200774 dns_answer_record->type = reader[0] * 256 + reader[1];
775 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200776
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200777 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200778 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100779 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200780 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200781 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200782 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200783 reader += 2;
784
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200785 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200786 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100787 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200788 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200789 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200790 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
791 + reader[2] * 256 + reader[3];
792 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200793
Christopher Faulet67957bd2017-09-27 11:00:59 +0200794 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200795 if (reader + 2 > 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->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200800
Christopher Faulet67957bd2017-09-27 11:00:59 +0200801 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200802 reader += 2;
803
Christopher Faulet67957bd2017-09-27 11:00:59 +0200804 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200805 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200806 case DNS_RTYPE_A:
807 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200808 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100809 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200810 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200811 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200812 dns_answer_record->address.sa_family = AF_INET;
813 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
814 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200815 break;
816
817 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200818 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200819 * no IP could be found and last record was a CNAME. Could be triggered
820 * by a wrong query type
821 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200822 * + 1 because dns_answer_record_id starts at 0
823 * while number of answers is an integer and
824 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200825 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200826 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100827 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200828 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200829 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200830
831 offset = 0;
832 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200833 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100834 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200835 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200836 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200837
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200838 memcpy(dns_answer_record->target, tmpname, len);
839 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200840 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200841 break;
842
Olivier Houchard8da5f982017-08-04 18:35:36 +0200843
844 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200845 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200846 * - 2 bytes for the priority
847 * - 2 bytes for the weight
848 * - 2 bytes for the port
849 * - the target hostname
850 */
851 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100852 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200853 return DNS_RESP_INVALID;
854 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200855 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200856 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200857 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200858 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200859 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200860 reader += sizeof(uint16_t);
861 offset = 0;
862 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
863 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100864 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200865 return DNS_RESP_INVALID;
866 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200867 dns_answer_record->data_len = len;
868 memcpy(dns_answer_record->target, tmpname, len);
869 dns_answer_record->target[len] = 0;
870 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200871
Baptiste Assmann325137d2015-04-13 23:40:55 +0200872 case DNS_RTYPE_AAAA:
873 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200874 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100875 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200876 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200877 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200878 dns_answer_record->address.sa_family = AF_INET6;
879 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
880 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200881 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200882
Baptiste Assmann325137d2015-04-13 23:40:55 +0200883 } /* switch (record type) */
884
Christopher Faulet67957bd2017-09-27 11:00:59 +0200885 /* Increment the counter for number of records saved into our
886 * local response */
887 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200888
Christopher Faulet67957bd2017-09-27 11:00:59 +0200889 /* Move forward dns_answer_record->data_len for analyzing next
890 * record in the response */
891 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
892 ? offset
893 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200894
895 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200896 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200897 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
898 if (tmp_record->type != dns_answer_record->type)
899 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200900
901 switch(tmp_record->type) {
902 case DNS_RTYPE_A:
903 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
904 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
905 sizeof(in_addr_t)))
906 found = 1;
907 break;
908
909 case DNS_RTYPE_AAAA:
910 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
911 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
912 sizeof(struct in6_addr)))
913 found = 1;
914 break;
915
Olivier Houchard8da5f982017-08-04 18:35:36 +0200916 case DNS_RTYPE_SRV:
917 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200918 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200919 dns_answer_record->port == tmp_record->port) {
920 tmp_record->weight = dns_answer_record->weight;
921 found = 1;
922 }
923 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200924
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200925 default:
926 break;
927 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200928
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200929 if (found == 1)
930 break;
931 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200932
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200933 if (found == 1) {
934 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100935 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200936 }
937 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200938 dns_answer_record->last_seen = now.tv_sec;
939 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
940 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200941 } /* for i 0 to ancount */
942
Christopher Faulet67957bd2017-09-27 11:00:59 +0200943 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200944 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200945 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200946 return DNS_RESP_VALID;
947}
948
Christopher Faulet67957bd2017-09-27 11:00:59 +0200949/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200950 * If existing IP not found, return the first IP matching family_priority,
951 * otherwise, first ip found
952 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200953 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200954 * For both cases above, dns_validate_dns_response is required
955 * returns one of the DNS_UPD_* code
956 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200957int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200958 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100959 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200960 void **newip, short *newip_sin_family,
961 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200962{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200963 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100964 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200965 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200966 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100967 int currentip_sel;
968 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100969 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200970 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200971
Christopher Faulet67957bd2017-09-27 11:00:59 +0200972 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200973 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200974 *newip = newip4 = newip6 = NULL;
975 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200976 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200977 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200978
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100979 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -0800980 * Top priority is the preferred network ip version,
981 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100982 * the last priority is the currently used IP,
983 *
984 * For these three priorities, a score is calculated. The
985 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -0800986 * 8 - preferred ip version.
987 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +0100988 * 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 +0100989 * 1 - current ip.
990 * The result with the biggest score is returned.
991 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200992
993 list_for_each_entry(record, &dns_p->answer_list, list) {
994 void *ip;
995 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100996
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200997 if (record->type == DNS_RTYPE_A) {
998 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
999 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001000 }
1001 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001002 ip_type = AF_INET6;
1003 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001004 }
1005 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001006 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001007 score = 0;
1008
Joseph Herlant42cf6392018-11-15 10:33:28 -08001009 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001010 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001011 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001012
Joseph Herlant42cf6392018-11-15 10:33:28 -08001013 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001014 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001015
1016 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001017 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001018 continue;
1019
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001020 if ((ip_type == AF_INET &&
1021 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001022 &dns_opts->pref_net[j].mask.in4,
1023 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001024 (ip_type == AF_INET6 &&
1025 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001026 &dns_opts->pref_net[j].mask.in6,
1027 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001028 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001029 break;
1030 }
1031 }
1032
Christopher Faulet67957bd2017-09-27 11:00:59 +02001033 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001034 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001035 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001036 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001037 if (!allowed_duplicated_ip) {
1038 continue;
1039 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001040 } else {
1041 score += 2;
1042 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001043
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001044 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001045 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001046 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001047 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001048 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001049 !memcmp(ip, currentip, 16)))) {
1050 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001051 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001052 }
1053 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001054 currentip_sel = 0;
1055
1056 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001057 * score. The maximum score is 15, if this value is reached, we
1058 * break the parsing. Implicitly, this score is reached the ip
1059 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001060 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001061 if (ip_type == AF_INET)
1062 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001063 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001064 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001065 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001066 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001067 return DNS_UPD_NO;
1068 max_score = score;
1069 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001070 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001071
Christopher Faulet67957bd2017-09-27 11:00:59 +02001072 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001073 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001074 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001075
Christopher Faulet67957bd2017-09-27 11:00:59 +02001076 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001077 if (family_priority == AF_INET) {
1078 if (newip4) {
1079 *newip = newip4;
1080 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 }
1082 else if (newip6) {
1083 *newip = newip6;
1084 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001085 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001086 if (!currentip_found)
1087 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001088 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001089 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001090 else if (family_priority == AF_INET6) {
1091 if (newip6) {
1092 *newip = newip6;
1093 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001094 }
1095 else if (newip4) {
1096 *newip = newip4;
1097 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001098 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001099 if (!currentip_found)
1100 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001101 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001102 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001103 else if (family_priority == AF_UNSPEC) {
1104 if (newip6) {
1105 *newip = newip6;
1106 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107 }
1108 else if (newip4) {
1109 *newip = newip4;
1110 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001111 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001112 if (!currentip_found)
1113 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001114 }
1115
Christopher Faulet67957bd2017-09-27 11:00:59 +02001116 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001117 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001118
Christopher Faulet67957bd2017-09-27 11:00:59 +02001119 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001120 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001121 /* Move the first record to the end of the list, for internal
1122 * round robin */
1123 LIST_DEL(&record->list);
1124 LIST_ADDQ(&dns_p->answer_list, &record->list);
1125 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001126 }
1127 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001128}
1129
Christopher Faulet67957bd2017-09-27 11:00:59 +02001130/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001131 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001132 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1133 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001134 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001135 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1136 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001138int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001139{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001140 char *ptr;
1141 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001142
Christopher Faulet67957bd2017-09-27 11:00:59 +02001143 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001144 return -1;
1145
Christopher Faulet67957bd2017-09-27 11:00:59 +02001146 ptr = str;
1147 for (i = 0; i < dn_len-1; ++i) {
1148 sz = dn[i];
1149 if (i)
1150 *ptr++ = '.';
1151 memcpy(ptr, dn+i+1, sz);
1152 ptr += sz;
1153 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001154 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001155 *ptr++ = '\0';
1156 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001157}
1158
Christopher Faulet67957bd2017-09-27 11:00:59 +02001159/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1160 *
1161 * <str> must be a null-terminated string. <str_len> must include the
1162 * terminating null byte. <dn> buffer must be allocated and its size must be
1163 * passed in <dn_len>.
1164 *
1165 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1166 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001167 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001168int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001169{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001170 int i, offset;
1171
Christopher Faulet67957bd2017-09-27 11:00:59 +02001172 if (dn_len < str_len + 1)
1173 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001174
Christopher Faulet67957bd2017-09-27 11:00:59 +02001175 /* First byte of dn will be used to store the length of the first
1176 * label */
1177 offset = 0;
1178 for (i = 0; i < str_len; ++i) {
1179 if (str[i] == '.') {
1180 /* 2 or more consecutive dots is invalid */
1181 if (i == offset)
1182 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001183
Christopher Faulet67957bd2017-09-27 11:00:59 +02001184 dn[offset] = (i - offset);
1185 offset = i+1;
1186 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001187 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001188 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001189 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001190 dn[offset] = (i - offset - 1);
1191 dn[i] = '\0';
1192 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001193}
1194
Christopher Faulet67957bd2017-09-27 11:00:59 +02001195/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001196 * - total size
1197 * - each label size individually
1198 * returns:
1199 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1200 * 1 when no error. <err> is left unaffected.
1201 */
1202int dns_hostname_validation(const char *string, char **err)
1203{
1204 const char *c, *d;
1205 int i;
1206
1207 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1208 if (err)
1209 *err = DNS_TOO_LONG_FQDN;
1210 return 0;
1211 }
1212
1213 c = string;
1214 while (*c) {
1215 d = c;
1216
1217 i = 0;
1218 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1219 i++;
1220 if (!((*d == '-') || (*d == '_') ||
1221 ((*d >= 'a') && (*d <= 'z')) ||
1222 ((*d >= 'A') && (*d <= 'Z')) ||
1223 ((*d >= '0') && (*d <= '9')))) {
1224 if (err)
1225 *err = DNS_INVALID_CHARACTER;
1226 return 0;
1227 }
1228 d++;
1229 }
1230
1231 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1232 if (err)
1233 *err = DNS_LABEL_TOO_LONG;
1234 return 0;
1235 }
1236
1237 if (*d == '\0')
1238 goto out;
1239
1240 c = ++d;
1241 }
1242 out:
1243 return 1;
1244}
1245
Christopher Faulet67957bd2017-09-27 11:00:59 +02001246/* Picks up an available resolution from the different resolution list
1247 * associated to a resolvers section, in this order:
1248 * 1. check in resolutions.curr for the same hostname and query_type
1249 * 2. check in resolutions.wait for the same hostname and query_type
1250 * 3. Get a new resolution from resolution pool
1251 *
1252 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001253 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001254static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1255 char **hostname_dn, int hostname_dn_len,
1256 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001257{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001258 struct dns_resolution *res;
1259
1260 if (!*hostname_dn)
1261 goto from_pool;
1262
1263 /* Search for same hostname and query type in resolutions.curr */
1264 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1265 if (!res->hostname_dn)
1266 continue;
1267 if ((query_type == res->prefered_query_type) &&
1268 hostname_dn_len == res->hostname_dn_len &&
1269 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1270 return res;
1271 }
1272
1273 /* Search for same hostname and query type in resolutions.wait */
1274 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1275 if (!res->hostname_dn)
1276 continue;
1277 if ((query_type == res->prefered_query_type) &&
1278 hostname_dn_len == res->hostname_dn_len &&
1279 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1280 return res;
1281 }
1282
1283 from_pool:
1284 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001285 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001286 if (res) {
1287 memset(res, 0, sizeof(*res));
1288 res->resolvers = resolvers;
1289 res->uuid = resolution_uuid;
1290 res->status = RSLV_STATUS_NONE;
1291 res->step = RSLV_STEP_NONE;
1292 res->last_valid = now_ms;
1293
1294 LIST_INIT(&res->requesters);
1295 LIST_INIT(&res->response.answer_list);
1296
1297 res->prefered_query_type = query_type;
1298 res->query_type = query_type;
1299 res->hostname_dn = *hostname_dn;
1300 res->hostname_dn_len = hostname_dn_len;
1301
1302 ++resolution_uuid;
1303
1304 /* Move the resolution to the resolvers wait queue */
1305 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1306 }
1307 return res;
1308}
1309
1310/* Releases a resolution from its requester(s) and move it back to the pool */
1311static void dns_free_resolution(struct dns_resolution *resolution)
1312{
1313 struct dns_requester *req, *reqback;
1314
1315 /* clean up configuration */
1316 dns_reset_resolution(resolution);
1317 resolution->hostname_dn = NULL;
1318 resolution->hostname_dn_len = 0;
1319
1320 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1321 LIST_DEL(&req->list);
1322 req->resolution = NULL;
1323 }
1324
1325 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001326 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001327}
1328
1329/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1330 * on success, -1 otherwise.
1331 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001332int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001333{
1334 struct dns_resolution *res = NULL;
1335 struct dns_requester *req;
1336 struct dns_resolvers *resolvers;
1337 struct server *srv = NULL;
1338 struct dns_srvrq *srvrq = NULL;
1339 char **hostname_dn;
1340 int hostname_dn_len, query_type;
1341
1342 switch (requester_type) {
1343 case OBJ_TYPE_SERVER:
1344 srv = (struct server *)requester;
1345 hostname_dn = &srv->hostname_dn;
1346 hostname_dn_len = srv->hostname_dn_len;
1347 resolvers = srv->resolvers;
1348 query_type = ((srv->dns_opts.family_prio == AF_INET)
1349 ? DNS_RTYPE_A
1350 : DNS_RTYPE_AAAA);
1351 break;
1352
1353 case OBJ_TYPE_SRVRQ:
1354 srvrq = (struct dns_srvrq *)requester;
1355 hostname_dn = &srvrq->hostname_dn;
1356 hostname_dn_len = srvrq->hostname_dn_len;
1357 resolvers = srvrq->resolvers;
1358 query_type = DNS_RTYPE_SRV;
1359 break;
1360
1361 default:
1362 goto err;
1363 }
1364
1365 /* Get a resolution from the resolvers' wait queue or pool */
1366 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1367 goto err;
1368
1369 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001370 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001371 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001372 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001373 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001374 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001375 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001376 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001377 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001378 req->owner = &srv->obj_type;
1379 srv->dns_requester = req;
1380 }
1381 else
1382 req = srv->dns_requester;
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 }
1386 else if (srvrq) {
1387 if (srvrq->dns_requester == NULL) {
1388 if ((req = calloc(1, sizeof(*req))) == NULL)
1389 goto err;
1390 req->owner = &srvrq->obj_type;
1391 srvrq->dns_requester = req;
1392 }
1393 else
1394 req = srvrq->dns_requester;
1395 }
1396 else
1397 goto err;
1398
1399 req->resolution = res;
1400 req->requester_cb = snr_resolution_cb;
1401 req->requester_error_cb = snr_resolution_error_cb;
1402
1403 LIST_ADDQ(&res->requesters, &req->list);
1404 return 0;
1405
1406 err:
1407 if (res && LIST_ISEMPTY(&res->requesters))
1408 dns_free_resolution(res);
1409 return -1;
1410}
1411
1412/* Removes a requester from a DNS resoltion. It takes takes care of all the
1413 * consequences. It also cleans up some parameters from the requester.
1414 */
1415void dns_unlink_resolution(struct dns_requester *requester)
1416{
1417 struct dns_resolution *res;
1418 struct dns_requester *req;
1419
1420 /* Nothing to do */
1421 if (!requester || !requester->resolution)
1422 return;
1423 res = requester->resolution;
1424
1425 /* Clean up the requester */
1426 LIST_DEL(&requester->list);
1427 requester->resolution = NULL;
1428
1429 /* We need to find another requester linked on this resolution */
1430 if (!LIST_ISEMPTY(&res->requesters))
1431 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1432 else {
1433 dns_free_resolution(res);
1434 return;
1435 }
1436
1437 /* Move hostname_dn related pointers to the next requester */
1438 switch (obj_type(req->owner)) {
1439 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001440 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1441 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001442 break;
1443 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001444 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1445 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001446 break;
1447 default:
1448 res->hostname_dn = NULL;
1449 res->hostname_dn_len = 0;
1450 break;
1451 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001452}
1453
Christopher Faulet67957bd2017-09-27 11:00:59 +02001454/* Called when a network IO is generated on a name server socket for an incoming
1455 * packet. It performs the following actions:
1456 * - check if the packet requires processing (not outdated resolution)
1457 * - ensure the DNS packet received is valid and call requester's callback
1458 * - call requester's error callback if invalid response
1459 * - check the dn_name in the packet against the one sent
1460 */
1461static void dns_resolve_recv(struct dgram_conn *dgram)
1462{
1463 struct dns_nameserver *ns, *tmpns;
1464 struct dns_resolvers *resolvers;
1465 struct dns_resolution *res;
1466 struct dns_query_item *query;
1467 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1468 unsigned char *bufend;
1469 int fd, buflen, dns_resp;
1470 int max_answer_records;
1471 unsigned short query_id;
1472 struct eb32_node *eb;
1473 struct dns_requester *req;
1474
1475 fd = dgram->t.sock.fd;
1476
1477 /* check if ready for reading */
1478 if (!fd_recv_ready(fd))
1479 return;
1480
1481 /* no need to go further if we can't retrieve the nameserver */
1482 if ((ns = dgram->owner) == NULL)
1483 return;
1484
1485 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001486 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001487
1488 /* process all pending input messages */
1489 while (1) {
1490 /* read message received */
1491 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1492 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1493 /* FIXME : for now we consider EAGAIN only */
1494 fd_cant_recv(fd);
1495 break;
1496 }
1497
1498 /* message too big */
1499 if (buflen > resolvers->accepted_payload_size) {
1500 ns->counters.too_big++;
1501 continue;
1502 }
1503
1504 /* initializing variables */
1505 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1506
1507 /* read the query id from the packet (16 bits) */
1508 if (buf + 2 > bufend) {
1509 ns->counters.invalid++;
1510 continue;
1511 }
1512 query_id = dns_response_get_query_id(buf);
1513
1514 /* search the query_id in the pending resolution tree */
1515 eb = eb32_lookup(&resolvers->query_ids, query_id);
1516 if (eb == NULL) {
1517 /* unknown query id means an outdated response and can be safely ignored */
1518 ns->counters.outdated++;
1519 continue;
1520 }
1521
1522 /* known query id means a resolution in prgress */
1523 res = eb32_entry(eb, struct dns_resolution, qid);
1524 if (!res) {
1525 ns->counters.outdated++;
1526 continue;
1527 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001528
Christopher Faulet67957bd2017-09-27 11:00:59 +02001529 /* number of responses received */
1530 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001531
Christopher Faulet67957bd2017-09-27 11:00:59 +02001532 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1533 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001534
Christopher Faulet67957bd2017-09-27 11:00:59 +02001535 switch (dns_resp) {
1536 case DNS_RESP_VALID:
1537 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001538
Christopher Faulet67957bd2017-09-27 11:00:59 +02001539 case DNS_RESP_INVALID:
1540 case DNS_RESP_QUERY_COUNT_ERROR:
1541 case DNS_RESP_WRONG_NAME:
1542 res->status = RSLV_STATUS_INVALID;
1543 ns->counters.invalid++;
1544 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001545
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 case DNS_RESP_NX_DOMAIN:
1547 res->status = RSLV_STATUS_NX;
1548 ns->counters.nx++;
1549 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001550
Christopher Faulet67957bd2017-09-27 11:00:59 +02001551 case DNS_RESP_REFUSED:
1552 res->status = RSLV_STATUS_REFUSED;
1553 ns->counters.refused++;
1554 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001555
Christopher Faulet67957bd2017-09-27 11:00:59 +02001556 case DNS_RESP_ANCOUNT_ZERO:
1557 res->status = RSLV_STATUS_OTHER;
1558 ns->counters.any_err++;
1559 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001560
Christopher Faulet67957bd2017-09-27 11:00:59 +02001561 case DNS_RESP_CNAME_ERROR:
1562 res->status = RSLV_STATUS_OTHER;
1563 ns->counters.cname_error++;
1564 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001565
Christopher Faulet67957bd2017-09-27 11:00:59 +02001566 case DNS_RESP_TRUNCATED:
1567 res->status = RSLV_STATUS_OTHER;
1568 ns->counters.truncated++;
1569 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001570
Christopher Faulet67957bd2017-09-27 11:00:59 +02001571 case DNS_RESP_NO_EXPECTED_RECORD:
1572 case DNS_RESP_ERROR:
1573 case DNS_RESP_INTERNAL:
1574 res->status = RSLV_STATUS_OTHER;
1575 ns->counters.other++;
1576 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001577 }
1578
Christopher Faulet67957bd2017-09-27 11:00:59 +02001579 /* Wait all nameservers response to handle errors */
1580 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1581 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001582
Christopher Faulet67957bd2017-09-27 11:00:59 +02001583 /* Process error codes */
1584 if (dns_resp != DNS_RESP_VALID) {
1585 if (res->prefered_query_type != res->query_type) {
1586 /* The fallback on the query type was already performed,
1587 * so check the try counter. If it falls to 0, we can
1588 * report an error. Else, wait the next attempt. */
1589 if (!res->try)
1590 goto report_res_error;
1591 }
1592 else {
1593 /* Fallback from A to AAAA or the opposite and re-send
1594 * the resolution immediately. try counter is not
1595 * decremented. */
1596 if (res->prefered_query_type == DNS_RTYPE_A) {
1597 res->query_type = DNS_RTYPE_AAAA;
1598 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001599 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001600 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1601 res->query_type = DNS_RTYPE_A;
1602 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001603 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604 }
1605 continue;
1606 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001607
Christopher Faulet67957bd2017-09-27 11:00:59 +02001608 /* Now let's check the query's dname corresponds to the one we
1609 * sent. We can check only the first query of the list. We send
1610 * one query at a time so we get one query in the response */
1611 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1612 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1613 dns_resp = DNS_RESP_WRONG_NAME;
1614 ns->counters.other++;
1615 goto report_res_error;
1616 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001617
Christopher Faulet67957bd2017-09-27 11:00:59 +02001618 /* So the resolution succeeded */
1619 res->status = RSLV_STATUS_VALID;
1620 res->last_valid = now_ms;
1621 ns->counters.valid++;
1622 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001623
Christopher Faulet67957bd2017-09-27 11:00:59 +02001624 report_res_error:
1625 list_for_each_entry(req, &res->requesters, list)
1626 req->requester_error_cb(req, dns_resp);
1627 dns_reset_resolution(res);
1628 LIST_DEL(&res->list);
1629 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1630 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001631
Christopher Faulet67957bd2017-09-27 11:00:59 +02001632 report_res_success:
1633 /* Only the 1rst requester s managed by the server, others are
1634 * from the cache */
1635 tmpns = ns;
1636 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001637 struct server *s = objt_server(req->owner);
1638
1639 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001640 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001641 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001642 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001643 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001644 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001645 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001646
Christopher Faulet67957bd2017-09-27 11:00:59 +02001647 dns_reset_resolution(res);
1648 LIST_DEL(&res->list);
1649 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1650 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001651 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001652 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001653 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001654}
William Lallemand69e96442016-11-19 00:58:54 +01001655
Christopher Faulet67957bd2017-09-27 11:00:59 +02001656/* Called when a resolvers network socket is ready to send data */
1657static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001658{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001659 struct dns_resolvers *resolvers;
1660 struct dns_nameserver *ns;
1661 struct dns_resolution *res;
1662 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001663
Christopher Faulet67957bd2017-09-27 11:00:59 +02001664 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001665
Christopher Faulet67957bd2017-09-27 11:00:59 +02001666 /* check if ready for sending */
1667 if (!fd_send_ready(fd))
1668 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001669
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670 /* we don't want/need to be waked up any more for sending */
1671 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001672
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 /* no need to go further if we can't retrieve the nameserver */
1674 if ((ns = dgram->owner) == NULL)
1675 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001676
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001678 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001679
Christopher Faulet67957bd2017-09-27 11:00:59 +02001680 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001681 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001682
Christopher Faulet67957bd2017-09-27 11:00:59 +02001683 if (res->nb_queries == resolvers->nb_nameservers)
1684 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001685
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001686 len = dns_build_query(res->query_id, res->query_type,
1687 resolvers->accepted_payload_size,
1688 res->hostname_dn, res->hostname_dn_len,
1689 trash.area, trash.size);
1690 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001692
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001693 ret = send(fd, trash.area, len, 0);
1694 if (ret != len)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001695 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001696
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697 ns->counters.sent++;
1698 res->nb_queries++;
1699 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001700
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701 snd_error:
1702 ns->counters.snd_error++;
1703 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001704 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001705 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001706}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001707
Christopher Faulet67957bd2017-09-27 11:00:59 +02001708/* Processes DNS resolution. First, it checks the active list to detect expired
1709 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1710 * checks the wait list to trigger new resolutions.
1711 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001712static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001713{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001714 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001715 struct dns_resolution *res, *resback;
1716 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001717
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001718 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001719
Christopher Faulet67957bd2017-09-27 11:00:59 +02001720 /* Handle all expired resolutions from the active list */
1721 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1722 /* When we find the first resolution in the future, then we can
1723 * stop here */
1724 exp = tick_add(res->last_query, resolvers->timeout.retry);
1725 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001726 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001727
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728 /* If current resolution has been tried too many times and
1729 * finishes in timeout we update its status and remove it from
1730 * the list */
1731 if (!res->try) {
1732 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001733
Christopher Faulet67957bd2017-09-27 11:00:59 +02001734 /* Notify the result to the requesters */
1735 if (!res->nb_responses)
1736 res->status = RSLV_STATUS_TIMEOUT;
1737 list_for_each_entry(req, &res->requesters, list)
1738 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001739
Christopher Faulet67957bd2017-09-27 11:00:59 +02001740 /* Clean up resolution info and remove it from the
1741 * current list */
1742 dns_reset_resolution(res);
1743 LIST_DEL(&res->list);
1744 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001745 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001746 else {
1747 /* Otherwise resend the DNS query and requeue the resolution */
1748 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1749 /* No response received (a real timeout) or fallback already done */
1750 res->query_type = res->prefered_query_type;
1751 res->try--;
1752 }
1753 else {
1754 /* Fallback from A to AAAA or the opposite and re-send
1755 * the resolution immediately. try counter is not
1756 * decremented. */
1757 if (res->prefered_query_type == DNS_RTYPE_A)
1758 res->query_type = DNS_RTYPE_AAAA;
1759 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1760 res->query_type = DNS_RTYPE_A;
1761 else
1762 res->try--;
1763 }
1764 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001765 }
1766 }
William Lallemand69e96442016-11-19 00:58:54 +01001767
Christopher Faulet67957bd2017-09-27 11:00:59 +02001768 /* Handle all resolutions in the wait list */
1769 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1770 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1771 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1772 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001773
Christopher Faulet67957bd2017-09-27 11:00:59 +02001774 if (dns_run_resolution(res) != 1) {
1775 res->last_resolution = now_ms;
1776 LIST_DEL(&res->list);
1777 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001778 }
1779 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001780
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001782 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001783 return t;
1784}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001785
Christopher Faulet67957bd2017-09-27 11:00:59 +02001786/* proto_udp callback functions for a DNS resolution */
1787struct dgram_data_cb resolve_dgram_cb = {
1788 .recv = dns_resolve_recv,
1789 .send = dns_resolve_send,
1790};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001791
Christopher Faulet67957bd2017-09-27 11:00:59 +02001792/* Release memory allocated by DNS */
1793static void dns_deinit(void)
1794{
1795 struct dns_resolvers *resolvers, *resolversback;
1796 struct dns_nameserver *ns, *nsback;
1797 struct dns_resolution *res, *resback;
1798 struct dns_requester *req, *reqback;
1799 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001800
Christopher Faulet67957bd2017-09-27 11:00:59 +02001801 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1802 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1803 free(ns->id);
1804 free((char *)ns->conf.file);
1805 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1806 fd_delete(ns->dgram->t.sock.fd);
1807 free(ns->dgram);
1808 LIST_DEL(&ns->list);
1809 free(ns);
1810 }
1811
1812 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1813 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1814 LIST_DEL(&req->list);
1815 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001816 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001817 dns_free_resolution(res);
1818 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001819
Christopher Faulet67957bd2017-09-27 11:00:59 +02001820 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1821 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1822 LIST_DEL(&req->list);
1823 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001824 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001825 dns_free_resolution(res);
1826 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001827
Christopher Faulet67957bd2017-09-27 11:00:59 +02001828 free(resolvers->id);
1829 free((char *)resolvers->conf.file);
1830 task_delete(resolvers->t);
1831 task_free(resolvers->t);
1832 LIST_DEL(&resolvers->list);
1833 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001834 }
1835
Christopher Faulet67957bd2017-09-27 11:00:59 +02001836 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1837 free(srvrq->name);
1838 free(srvrq->hostname_dn);
1839 LIST_DEL(&srvrq->list);
1840 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001841 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001842}
1843
Christopher Faulet67957bd2017-09-27 11:00:59 +02001844/* Finalizes the DNS configuration by allocating required resources and checking
1845 * live parameters.
1846 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001847 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001848static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001849{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001850 struct dns_resolvers *resolvers;
1851 struct proxy *px;
1852 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001853
Christopher Faulet67957bd2017-09-27 11:00:59 +02001854 /* allocate pool of resolution per resolvers */
1855 list_for_each_entry(resolvers, &dns_resolvers, list) {
1856 struct dns_nameserver *ns;
1857 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001858
Christopher Faulet67957bd2017-09-27 11:00:59 +02001859 /* Check if we can create the socket with nameservers info */
1860 list_for_each_entry(ns, &resolvers->nameservers, list) {
1861 struct dgram_conn *dgram = NULL;
1862 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001863
Christopher Faulet67957bd2017-09-27 11:00:59 +02001864 /* Check nameserver info */
1865 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001866 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1867 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001868 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001869 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001870 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001871 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001872 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1873 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001874 close(fd);
1875 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001876 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001877 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001878 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001879
Christopher Faulet67957bd2017-09-27 11:00:59 +02001880 /* Create dgram structure that will hold the UPD socket
1881 * and attach it on the current nameserver */
1882 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001883 ha_alert("config: resolvers '%s' : out of memory.\n",
1884 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001885 err_code |= (ERR_ALERT|ERR_ABORT);
1886 goto err;
1887 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001888
Christopher Faulet67957bd2017-09-27 11:00:59 +02001889 /* Leave dgram partially initialized, no FD attached for
1890 * now. */
1891 dgram->owner = ns;
1892 dgram->data = &resolve_dgram_cb;
1893 dgram->t.sock.fd = -1;
1894 ns->dgram = dgram;
1895 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001896
Christopher Faulet67957bd2017-09-27 11:00:59 +02001897 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001898 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001899 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001900 err_code |= (ERR_ALERT|ERR_ABORT);
1901 goto err;
1902 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001903
Christopher Faulet67957bd2017-09-27 11:00:59 +02001904 /* Update task's parameters */
1905 t->process = dns_process_resolvers;
1906 t->context = resolvers;
1907 resolvers->t = t;
1908 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001909 }
1910
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001911 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001912 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001913
Christopher Faulet67957bd2017-09-27 11:00:59 +02001914 for (srv = px->srv; srv; srv = srv->next) {
1915 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001916
Christopher Faulet67957bd2017-09-27 11:00:59 +02001917 if (!srv->resolvers_id)
1918 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001919
Christopher Faulet67957bd2017-09-27 11:00:59 +02001920 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001921 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1922 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001923 err_code |= (ERR_ALERT|ERR_ABORT);
1924 continue;
1925 }
1926 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001927
Christopher Faulet67957bd2017-09-27 11:00:59 +02001928 if (srv->srvrq && !srv->srvrq->resolvers) {
1929 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001930 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001931 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1932 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001933 err_code |= (ERR_ALERT|ERR_ABORT);
1934 continue;
1935 }
1936 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001937 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001938 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1939 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940 err_code |= (ERR_ALERT|ERR_ABORT);
1941 continue;
1942 }
1943 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001944 }
1945
Christopher Faulet67957bd2017-09-27 11:00:59 +02001946 if (err_code & (ERR_ALERT|ERR_ABORT))
1947 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001948
Christopher Faulet67957bd2017-09-27 11:00:59 +02001949 return err_code;
1950 err:
1951 dns_deinit();
1952 return err_code;
1953
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001954}
1955
Christopher Faulet67957bd2017-09-27 11:00:59 +02001956/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001957static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001958{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001959 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001960
Christopher Faulet67957bd2017-09-27 11:00:59 +02001961 if (*args[2]) {
1962 list_for_each_entry(presolvers, &dns_resolvers, list) {
1963 if (strcmp(presolvers->id, args[2]) == 0) {
1964 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001965 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001966 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001967 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 if (appctx->ctx.cli.p0 == NULL) {
1969 appctx->ctx.cli.severity = LOG_ERR;
1970 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1971 appctx->st0 = CLI_ST_PRINT;
1972 return 1;
1973 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001974 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001976}
1977
Christopher Faulet67957bd2017-09-27 11:00:59 +02001978/* Dumps counters from all resolvers section and associated name servers. It
1979 * returns 0 if the output buffer is full and it needs to be called again,
1980 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001981 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001982 */
1983static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1984{
1985 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001986 struct dns_resolvers *resolvers;
1987 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001988
1989 chunk_reset(&trash);
1990
1991 switch (appctx->st2) {
1992 case STAT_ST_INIT:
1993 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1994 /* fall through */
1995
1996 case STAT_ST_LIST:
1997 if (LIST_ISEMPTY(&dns_resolvers)) {
1998 chunk_appendf(&trash, "No resolvers found\n");
1999 }
2000 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002001 list_for_each_entry(resolvers, &dns_resolvers, list) {
2002 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002003 continue;
2004
Christopher Faulet67957bd2017-09-27 11:00:59 +02002005 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2006 list_for_each_entry(ns, &resolvers->nameservers, list) {
2007 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2008 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2009 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2010 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2011 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2012 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2013 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2014 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2015 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2016 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2017 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2018 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2019 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2020 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2021 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2022 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002023 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002024 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002025 }
2026 }
2027
2028 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002029 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002030 /* let's try again later from this session. We add ourselves into
2031 * this session's users so that it can remove us upon termination.
2032 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002033 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002034 return 0;
2035 }
William Lallemand69e96442016-11-19 00:58:54 +01002036 /* fall through */
2037
2038 default:
2039 appctx->st2 = STAT_ST_FIN;
2040 return 1;
2041 }
2042}
2043
2044/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002045static struct cli_kw_list cli_kws = {{ }, {
2046 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2047 " associated name servers",
2048 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2049 {{},}
2050 }
2051};
William Lallemand69e96442016-11-19 00:58:54 +01002052
Willy Tarreau0108d902018-11-25 19:14:37 +01002053INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002054
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002055REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002056REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);