blob: 385ecbb6dd06fea45dfec7bfc920a040caad2c26 [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
Christopher Faulet67957bd2017-09-27 11:00:59 +020022#include <common/errors.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020023#include <common/time.h>
24#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020025#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020026
William Lallemand69e96442016-11-19 00:58:54 +010027#include <types/applet.h>
28#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020029#include <types/global.h>
30#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010031#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032
William Lallemand69e96442016-11-19 00:58:54 +010033#include <proto/channel.h>
34#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035#include <proto/checks.h>
36#include <proto/dns.h>
37#include <proto/fd.h>
38#include <proto/log.h>
39#include <proto/server.h>
40#include <proto/task.h>
41#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020042#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010043#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020044
Christopher Faulet67957bd2017-09-27 11:00:59 +020045struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
46struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020047
Christopher Fauletb2812a62017-10-04 16:17:58 +020048static THREAD_LOCAL int64_t dns_query_id_seed = 0; /* random seed */
Christopher Faulet67957bd2017-09-27 11:00:59 +020049static struct pool_head *dns_answer_item_pool = NULL;
50static struct pool_head *dns_resolution_pool = NULL;
51static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020052
Christopher Faulet67957bd2017-09-27 11:00:59 +020053/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
54 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020055 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020056struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020057{
Christopher Faulet67957bd2017-09-27 11:00:59 +020058 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059
Christopher Faulet67957bd2017-09-27 11:00:59 +020060 list_for_each_entry(res, &dns_resolvers, list) {
61 if (!strcmp(res->id, id))
62 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020064 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065}
66
Christopher Faulet67957bd2017-09-27 11:00:59 +020067/* Returns a pointer on the SRV request matching the name <name> for the proxy
68 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020070struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071{
Christopher Faulet67957bd2017-09-27 11:00:59 +020072 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073
Christopher Faulet67957bd2017-09-27 11:00:59 +020074 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
75 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
76 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020078 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020079}
80
Christopher Faulet67957bd2017-09-27 11:00:59 +020081/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
82 * NULL if an error occurred. */
83struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020084{
Christopher Faulet67957bd2017-09-27 11:00:59 +020085 struct proxy *px = srv->proxy;
86 struct dns_srvrq *srvrq = NULL;
87 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020088
Christopher Faulet67957bd2017-09-27 11:00:59 +020089 fqdn_len = strlen(fqdn);
90 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.str, trash.size);
91 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010092 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
93 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +020094 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020095 }
96
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010098 ha_alert("config : %s '%s', server '%s': out of memory\n",
99 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200100 goto err;
101 }
102 srvrq->obj_type = OBJ_TYPE_SRVRQ;
103 srvrq->proxy = px;
104 srvrq->name = strdup(fqdn);
105 srvrq->hostname_dn = strdup(trash.str);
106 srvrq->hostname_dn_len = hostname_dn_len;
107 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100108 ha_alert("config : %s '%s', server '%s': out of memory\n",
109 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200110 goto err;
111 }
112 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
113 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200114
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 err:
116 if (srvrq) {
117 free(srvrq->name);
118 free(srvrq->hostname_dn);
119 free(srvrq);
120 }
121 return NULL;
122}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200123
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200124
Christopher Faulet67957bd2017-09-27 11:00:59 +0200125/* 2 bytes random generator to generate DNS query ID */
126static inline uint16_t dns_rnd16(void)
127{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200128 if (!dns_query_id_seed)
129 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130 dns_query_id_seed ^= dns_query_id_seed << 13;
131 dns_query_id_seed ^= dns_query_id_seed >> 7;
132 dns_query_id_seed ^= dns_query_id_seed << 17;
133 return dns_query_id_seed;
134}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200135
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136
Christopher Faulet67957bd2017-09-27 11:00:59 +0200137static inline int dns_resolution_timeout(struct dns_resolution *res)
138{
139 switch (res->status) {
140 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
141 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200142 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200143}
144
Christopher Faulet67957bd2017-09-27 11:00:59 +0200145/* Updates a resolvers' task timeout for next wake up and queue it */
146static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200147{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200148 struct dns_resolution *res;
149 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151 next = tick_add(now_ms, resolvers->timeout.resolve);
152 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
153 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
154 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
155 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200156
Christopher Faulet67957bd2017-09-27 11:00:59 +0200157 list_for_each_entry(res, &resolvers->resolutions.wait, list)
158 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200159
Christopher Faulet67957bd2017-09-27 11:00:59 +0200160 resolvers->t->expire = next;
161 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200162}
163
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
165 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200166 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 struct dgram_conn *dgram = ns->dgram;
170 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172 /* Already connected */
173 if (dgram->t.sock.fd != -1)
174 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200175
Christopher Faulet67957bd2017-09-27 11:00:59 +0200176 /* Create an UDP socket and connect it on the nameserver's IP/Port */
177 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
178 send_log(NULL, LOG_WARNING,
179 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
180 ns->resolvers->id, ns->id);
181 return -1;
182 }
183 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
184 send_log(NULL, LOG_WARNING,
185 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
186 ns->resolvers->id, ns->id);
187 close(fd);
188 return -1;
189 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200190
Christopher Faulet67957bd2017-09-27 11:00:59 +0200191 /* Make the socket non blocking */
192 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200193
Christopher Faulet67957bd2017-09-27 11:00:59 +0200194 /* Add the fd in the fd list and update its parameters */
195 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100196 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200197 fd_want_recv(fd);
198 return 0;
199}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200200
Christopher Faulet67957bd2017-09-27 11:00:59 +0200201/* Forges a DNS query. It needs the following information from the caller:
202 * - <query_id> : the DNS query id corresponding to this query
203 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
204 * - <hostname_dn> : hostname in domain name format
205 * - <hostname_dn_len> : length of <hostname_dn>
206 *
207 * To store the query, the caller must pass a buffer <buf> and its size
208 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
209 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200210 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200211static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
212 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200213{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200214 struct dns_header dns_hdr;
215 struct dns_question qinfo;
216 struct dns_additional_record edns;
217 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200218
Christopher Faulet67957bd2017-09-27 11:00:59 +0200219 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
220 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200221
Christopher Faulet67957bd2017-09-27 11:00:59 +0200222 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224 /* Set dns query headers */
225 dns_hdr.id = (unsigned short) htons(query_id);
226 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
227 dns_hdr.qdcount = htons(1); /* 1 question */
228 dns_hdr.ancount = 0;
229 dns_hdr.nscount = 0;
230 dns_hdr.arcount = htons(1);
231 memcpy(p, &dns_hdr, sizeof(dns_hdr));
232 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200233
Christopher Faulet67957bd2017-09-27 11:00:59 +0200234 /* Set up query hostname */
235 memcpy(p, hostname_dn, hostname_dn_len);
236 p += hostname_dn_len;
237 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200238
Christopher Faulet67957bd2017-09-27 11:00:59 +0200239 /* Set up query info (type and class) */
240 qinfo.qtype = htons(query_type);
241 qinfo.qclass = htons(DNS_RCLASS_IN);
242 memcpy(p, &qinfo, sizeof(qinfo));
243 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200244
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 /* Set the DNS extension */
246 edns.name = 0;
247 edns.type = htons(DNS_RTYPE_OPT);
248 edns.udp_payload_size = htons(accepted_payload_size);
249 edns.extension = 0;
250 edns.data_length = 0;
251 memcpy(p, &edns, sizeof(edns));
252 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200253
Christopher Faulet67957bd2017-09-27 11:00:59 +0200254 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200255}
256
Christopher Faulet67957bd2017-09-27 11:00:59 +0200257/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
258 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200259 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200261{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262 struct dns_resolvers *resolvers = resolution->resolvers;
263 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200264
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265 list_for_each_entry(ns, &resolvers->nameservers, list) {
266 int fd = ns->dgram->t.sock.fd;
267 if (fd == -1) {
268 if (dns_connect_namesaver(ns) == -1)
269 continue;
270 fd = ns->dgram->t.sock.fd;
271 resolvers->nb_nameservers++;
272 }
273 fd_want_send(fd);
274 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200275
Christopher Faulet67957bd2017-09-27 11:00:59 +0200276 /* Update resolution */
277 resolution->nb_queries = 0;
278 resolution->nb_responses = 0;
279 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281 /* Push the resolution at the end of the active list */
282 LIST_DEL(&resolution->list);
283 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
284 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200285}
286
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
288 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200289 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200290static int
291dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200292{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200293 struct dns_resolvers *resolvers = resolution->resolvers;
294 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200295
Christopher Faulet67957bd2017-09-27 11:00:59 +0200296 /* Avoid sending requests for resolutions that don't yet have an
297 * hostname, ie resolutions linked to servers that do not yet have an
298 * fqdn */
299 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200300 return 0;
301
Christopher Faulet67957bd2017-09-27 11:00:59 +0200302 /* Check if a resolution has already been started for this server return
303 * directly to avoid resolution pill up. */
304 if (resolution->step != RSLV_STEP_NONE)
305 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307 /* Generates a new query id. We try at most 100 times to find a free
308 * query id */
309 for (i = 0; i < 100; ++i) {
310 query_id = dns_rnd16();
311 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200312 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200313 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200314 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200315 if (query_id == -1) {
316 send_log(NULL, LOG_NOTICE,
317 "could not generate a query id for %s, in resolvers %s.\n",
318 resolution->hostname_dn, resolvers->id);
319 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200320 }
321
Christopher Faulet67957bd2017-09-27 11:00:59 +0200322 /* Update resolution parameters */
323 resolution->query_id = query_id;
324 resolution->qid.key = query_id;
325 resolution->step = RSLV_STEP_RUNNING;
326 resolution->query_type = resolution->prefered_query_type;
327 resolution->try = resolvers->resolve_retries;
328 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200329
Christopher Faulet67957bd2017-09-27 11:00:59 +0200330 /* Send the DNS query */
331 resolution->try -= 1;
332 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333 return 1;
334}
335
Christopher Faulet67957bd2017-09-27 11:00:59 +0200336/* Performs a name resolution for the requester <req> */
337void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200339 struct dns_resolvers *resolvers;
340 struct dns_resolution *res;
341 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200342
Christopher Faulet67957bd2017-09-27 11:00:59 +0200343 if (!req || !req->resolution)
344 return;
345 res = req->resolution;
346 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200347
Christopher Faulet67957bd2017-09-27 11:00:59 +0200348 /* The resolution must not be triggered yet. Use the cached response, if
349 * valid */
350 exp = tick_add(res->last_resolution, resolvers->hold.valid);
351 if (res->status == RSLV_STATUS_VALID &&
352 tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
353 req->requester_cb(req, NULL);
354 else
355 dns_run_resolution(res);
356}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200357
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200358
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359/* Resets some resolution parameters to initial values and also delete the query
360 * ID from the resolver's tree.
361 */
362static void dns_reset_resolution(struct dns_resolution *resolution)
363{
364 /* update resolution status */
365 resolution->step = RSLV_STEP_NONE;
366 resolution->try = 0;
367 resolution->last_resolution = now_ms;
368 resolution->nb_queries = 0;
369 resolution->nb_responses = 0;
370 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200371
Christopher Faulet67957bd2017-09-27 11:00:59 +0200372 /* clean up query id */
373 eb32_delete(&resolution->qid);
374 resolution->query_id = 0;
375 resolution->qid.key = 0;
376}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200377
Christopher Faulet67957bd2017-09-27 11:00:59 +0200378/* Returns the query id contained in a DNS response */
379static inline unsigned short dns_response_get_query_id(unsigned char *resp)
380{
381 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200382}
383
Christopher Faulet67957bd2017-09-27 11:00:59 +0200384
385/* Analyses, re-builds and copies the name <name> from the DNS response packet
386 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
387 * for compressed data. The result is copied into <dest>, ensuring we don't
388 * overflow using <dest_len> Returns the number of bytes the caller can move
389 * forward. If 0 it means an error occurred while parsing the name. <offset> is
390 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200391 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200392int dns_read_name(unsigned char *buffer, unsigned char *bufend,
393 unsigned char *name, char *destination, int dest_len,
394 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200395{
396 int nb_bytes = 0, n = 0;
397 int label_len;
398 unsigned char *reader = name;
399 char *dest = destination;
400
401 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200402 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200403 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404 /* Must point BEFORE current position */
405 if ((buffer + reader[1]) > reader)
406 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200407
Christopher Faulet67957bd2017-09-27 11:00:59 +0200408 n = dns_read_name(buffer, bufend, buffer + reader[1],
409 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200410 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200411 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200412
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200414 nb_bytes += n;
415 goto out;
416 }
417
418 label_len = *reader;
419 if (label_len == 0)
420 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200421
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200422 /* Check if:
423 * - we won't read outside the buffer
424 * - there is enough place in the destination
425 */
426 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200427 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200428
429 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200430 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200431
432 memcpy(dest, reader, label_len);
433
Christopher Faulet67957bd2017-09-27 11:00:59 +0200434 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200435 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200436 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200437 }
438
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200440 /* offset computation:
441 * parse from <name> until finding either NULL or a pointer "c0xx"
442 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200443 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200444 *offset = 0;
445 while (reader < bufend) {
446 if ((reader[0] & 0xc0) == 0xc0) {
447 *offset += 2;
448 break;
449 }
450 else if (*reader == 0) {
451 *offset += 1;
452 break;
453 }
454 *offset += 1;
455 ++reader;
456 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200457 return nb_bytes;
458
Christopher Faulet67957bd2017-09-27 11:00:59 +0200459 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200460 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200461}
462
Christopher Faulet67957bd2017-09-27 11:00:59 +0200463/* Checks for any obsolete record, also identify any SRV request, and try to
464 * find a corresponding server.
465*/
466static void dns_check_dns_response(struct dns_resolution *res)
467{
468 struct dns_resolvers *resolvers = res->resolvers;
469 struct dns_requester *req, *reqback;
470 struct dns_answer_item *item, *itemback;
471 struct server *srv;
472 struct dns_srvrq *srvrq;
473
474 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
475
476 /* Remove obsolete items */
477 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
478 if (item->type != DNS_RTYPE_SRV)
479 goto rm_obselete_item;
480
481 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
482 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
483 continue;
484
485 /* Remove any associated server */
486 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100487 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200488 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
489 item->data_len == srv->hostname_dn_len &&
490 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
491 snr_update_srv_status(srv, 1);
492 free(srv->hostname);
493 free(srv->hostname_dn);
494 srv->hostname = NULL;
495 srv->hostname_dn = NULL;
496 srv->hostname_dn_len = 0;
497 dns_unlink_resolution(srv->dns_requester);
498 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100499 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200500 }
501 }
502
503 rm_obselete_item:
504 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100505 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200506 continue;
507 }
508
509 if (item->type != DNS_RTYPE_SRV)
510 continue;
511
512 /* Now process SRV records */
513 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
514 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
515 continue;
516
517 /* Check if a server already uses that hostname */
518 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100519 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200520 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
521 item->data_len == srv->hostname_dn_len &&
522 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100523 int ha_weight;
524
525 /* Make sure weight is at least 1, so
526 * that the server will be used.
527 */
528 ha_weight = item->weight / 256 + 1;
529 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200530 char weight[9];
531
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100532 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200533 server_parse_weight_change_request(srv, weight);
534 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100535 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200536 break;
537 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100538 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200539 }
540 if (srv)
541 continue;
542
543 /* If not, try to find a server with undefined hostname */
544 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100545 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200546 if (srv->srvrq == srvrq && !srv->hostname_dn)
547 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100548 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200549 }
550 /* And update this server, if found */
551 if (srv) {
552 const char *msg = NULL;
553 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100554 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200555 char hostname[DNS_MAX_NAME_SIZE];
556
557 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100558 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100559 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200560 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100561 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100562 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200563 if (msg)
564 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
565
566 srv->svc_port = item->port;
567 srv->flags &= ~SRV_F_MAPPORTS;
568 if ((srv->check.state & CHK_ST_CONFIGURED) &&
569 !(srv->flags & SRV_F_CHECKPORT))
570 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100571
572 /* Make sure weight is at least 1, so
573 * that the server will be used.
574 */
575 ha_weight = item->weight / 256 + 1;
576
577 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200578 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100579 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200580 }
581 }
582 }
583}
584
585/* Validates that the buffer DNS response provided in <resp> and finishing
586 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200587 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200588 * The result is stored in <resolution>' response, buf_response,
589 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200590 *
591 * This function returns one of the DNS_RESP_* code to indicate the type of
592 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200593 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200594static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
595 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200596{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200597 unsigned char *reader;
598 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200599 int len, flags, offset;
600 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200601 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200602 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200603 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200604 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200605 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200606
Christopher Faulet67957bd2017-09-27 11:00:59 +0200607 reader = resp;
608 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200609 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200610 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200611
Christopher Faulet67957bd2017-09-27 11:00:59 +0200612 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200613 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200614
615 /* query id */
616 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200617 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200618 dns_p->header.id = reader[0] * 256 + reader[1];
619 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200620
Christopher Faulet67957bd2017-09-27 11:00:59 +0200621 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200622 * First byte contains:
623 * - response flag (1 bit)
624 * - opcode (4 bits)
625 * - authoritative (1 bit)
626 * - truncated (1 bit)
627 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200628 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200629 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200630 return DNS_RESP_INVALID;
631
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200632 flags = reader[0] * 256 + reader[1];
633
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200634 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
635 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200636 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200637 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200638 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200639 return DNS_RESP_ERROR;
640 }
641
Christopher Faulet67957bd2017-09-27 11:00:59 +0200642 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200643 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200644
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200645 /* 2 bytes for question count */
646 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200647 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200648 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200649 /* (for now) we send one query only, so we expect only one in the
650 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200651 if (dns_p->header.qdcount != 1)
652 return DNS_RESP_QUERY_COUNT_ERROR;
653 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200654 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200655 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200656
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200657 /* 2 bytes for answer count */
658 if (reader + 2 >= bufend)
659 return DNS_RESP_INVALID;
660 dns_p->header.ancount = reader[0] * 256 + reader[1];
661 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200662 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200663 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200664 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200665 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200666 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200667
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200668 /* 2 bytes authority count */
669 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200670 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200671 dns_p->header.nscount = reader[0] * 256 + reader[1];
672 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200673
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200674 /* 2 bytes additional count */
675 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200676 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200677 dns_p->header.arcount = reader[0] * 256 + reader[1];
678 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200679
Christopher Faulet67957bd2017-09-27 11:00:59 +0200680 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200681 LIST_INIT(&dns_p->query_list);
682 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 +0200683 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200685 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200686 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
687 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200688 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200689 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200690
Christopher Faulet67957bd2017-09-27 11:00:59 +0200691 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200692 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200693 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200694 offset = 0;
695 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200696
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200697 if (len == 0)
698 return DNS_RESP_INVALID;
699
700 reader += offset;
701 previous_dname = dns_query->name;
702
703 /* move forward 2 bytes for question type */
704 if (reader + 2 >= bufend)
705 return DNS_RESP_INVALID;
706 dns_query->type = reader[0] * 256 + reader[1];
707 reader += 2;
708
709 /* move forward 2 bytes for question class */
710 if (reader + 2 >= bufend)
711 return DNS_RESP_INVALID;
712 dns_query->class = reader[0] * 256 + reader[1];
713 reader += 2;
714 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200715
Baptiste Assmann251abb92017-08-11 09:58:27 +0200716 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200717 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200718 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
719 return DNS_RESP_TRUNCATED;
720
Baptiste Assmann325137d2015-04-13 23:40:55 +0200721 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200722 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200723 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200724 if (reader >= bufend)
725 return DNS_RESP_INVALID;
726
Willy Tarreaubafbe012017-11-24 17:34:44 +0100727 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200728 if (dns_answer_record == NULL)
729 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200730
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200731 offset = 0;
732 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200733
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200734 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100735 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200736 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200737 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200738
Christopher Faulet67957bd2017-09-27 11:00:59 +0200739 /* Check if the current record dname is valid. previous_dname
740 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200741 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100742 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200743 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200744 /* First record, means a mismatch issue between
745 * queried dname and dname found in the first
746 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200747 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200748 }
749 else {
750 /* If not the first record, this means we have a
751 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200752 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200753 }
754
Baptiste Assmann325137d2015-04-13 23:40:55 +0200755 }
756
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200757 memcpy(dns_answer_record->name, tmpname, len);
758 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200759
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200760 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200761 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100762 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200763 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200764 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200765
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200767 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100768 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200769 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200770 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200771 dns_answer_record->type = reader[0] * 256 + reader[1];
772 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200773
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200774 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200775 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100776 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200777 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200778 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200780 reader += 2;
781
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200782 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200783 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100784 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200785 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200786 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200787 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
788 + reader[2] * 256 + reader[3];
789 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200790
Christopher Faulet67957bd2017-09-27 11:00:59 +0200791 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200792 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100793 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200794 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200795 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200796 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200797
Christopher Faulet67957bd2017-09-27 11:00:59 +0200798 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200799 reader += 2;
800
Christopher Faulet67957bd2017-09-27 11:00:59 +0200801 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200802 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200803 case DNS_RTYPE_A:
804 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200805 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100806 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200808 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200809 dns_answer_record->address.sa_family = AF_INET;
810 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
811 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200812 break;
813
814 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200815 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200816 * no IP could be found and last record was a CNAME. Could be triggered
817 * by a wrong query type
818 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200819 * + 1 because dns_answer_record_id starts at 0
820 * while number of answers is an integer and
821 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200822 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200823 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100824 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200825 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200826 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200827
828 offset = 0;
829 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200830 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100831 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200832 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200833 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200834
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200835 memcpy(dns_answer_record->target, tmpname, len);
836 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200837 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200838 break;
839
Olivier Houchard8da5f982017-08-04 18:35:36 +0200840
841 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200842 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200843 * - 2 bytes for the priority
844 * - 2 bytes for the weight
845 * - 2 bytes for the port
846 * - the target hostname
847 */
848 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100849 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200850 return DNS_RESP_INVALID;
851 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200852 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200853 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200854 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200855 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200856 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200857 reader += sizeof(uint16_t);
858 offset = 0;
859 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
860 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100861 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200862 return DNS_RESP_INVALID;
863 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200864 dns_answer_record->data_len = len;
865 memcpy(dns_answer_record->target, tmpname, len);
866 dns_answer_record->target[len] = 0;
867 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200868
Baptiste Assmann325137d2015-04-13 23:40:55 +0200869 case DNS_RTYPE_AAAA:
870 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200871 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100872 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200873 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200874 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200875 dns_answer_record->address.sa_family = AF_INET6;
876 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
877 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200878 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200879
Baptiste Assmann325137d2015-04-13 23:40:55 +0200880 } /* switch (record type) */
881
Christopher Faulet67957bd2017-09-27 11:00:59 +0200882 /* Increment the counter for number of records saved into our
883 * local response */
884 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200885
Christopher Faulet67957bd2017-09-27 11:00:59 +0200886 /* Move forward dns_answer_record->data_len for analyzing next
887 * record in the response */
888 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
889 ? offset
890 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200891
892 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200893 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200894 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
895 if (tmp_record->type != dns_answer_record->type)
896 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200897
898 switch(tmp_record->type) {
899 case DNS_RTYPE_A:
900 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
901 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
902 sizeof(in_addr_t)))
903 found = 1;
904 break;
905
906 case DNS_RTYPE_AAAA:
907 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
908 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
909 sizeof(struct in6_addr)))
910 found = 1;
911 break;
912
Olivier Houchard8da5f982017-08-04 18:35:36 +0200913 case DNS_RTYPE_SRV:
914 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200915 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200916 dns_answer_record->port == tmp_record->port) {
917 tmp_record->weight = dns_answer_record->weight;
918 found = 1;
919 }
920 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200921
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200922 default:
923 break;
924 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200925
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200926 if (found == 1)
927 break;
928 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200929
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200930 if (found == 1) {
931 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100932 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200933 }
934 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200935 dns_answer_record->last_seen = now.tv_sec;
936 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
937 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200938 } /* for i 0 to ancount */
939
Christopher Faulet67957bd2017-09-27 11:00:59 +0200940 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200941 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200942 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200943 return DNS_RESP_VALID;
944}
945
Christopher Faulet67957bd2017-09-27 11:00:59 +0200946/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200947 * If existing IP not found, return the first IP matching family_priority,
948 * otherwise, first ip found
949 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200950 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200951 * For both cases above, dns_validate_dns_response is required
952 * returns one of the DNS_UPD_* code
953 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100954#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200955int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200956 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100957 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200958 void **newip, short *newip_sin_family,
959 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200960{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200961 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100962 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200963 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200964 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100965 int currentip_sel;
966 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100967 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968
Christopher Faulet67957bd2017-09-27 11:00:59 +0200969 family_priority = dns_opts->family_prio;
970 *newip = newip4 = newip6 = NULL;
971 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200972 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200973 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200974
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100975 /* Select an IP regarding configuration preference.
976 * Top priority is the prefered network ip version,
977 * second priority is the prefered network.
978 * the last priority is the currently used IP,
979 *
980 * For these three priorities, a score is calculated. The
981 * weight are:
Baptistefc725902016-12-26 23:21:08 +0100982 * 8 - prefered netwok ip version.
983 * 4 - prefered network.
984 * 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 +0100985 * 1 - current ip.
986 * The result with the biggest score is returned.
987 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200988
989 list_for_each_entry(record, &dns_p->answer_list, list) {
990 void *ip;
991 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100992
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200993 if (record->type == DNS_RTYPE_A) {
994 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
995 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200996 }
997 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200998 ip_type = AF_INET6;
999 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001000 }
1001 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001002 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001003 score = 0;
1004
1005 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001006 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001007 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001008
1009 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001010 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001011
1012 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001013 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001014 continue;
1015
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001016 if ((ip_type == AF_INET &&
1017 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001018 &dns_opts->pref_net[j].mask.in4,
1019 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001020 (ip_type == AF_INET6 &&
1021 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001022 &dns_opts->pref_net[j].mask.in6,
1023 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001024 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001025 break;
1026 }
1027 }
1028
Christopher Faulet67957bd2017-09-27 11:00:59 +02001029 /* Check if the IP found in the record is already affected to a
1030 * member of a group. If yes, the score should be incremented
1031 * by 2. */
1032 if (owner && snr_check_ip_callback(owner, ip, &ip_type))
1033 continue;
1034
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001035 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001036 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001037 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001038 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001039 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001040 !memcmp(ip, currentip, 16)))) {
1041 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001042 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001043 }
1044 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001045 currentip_sel = 0;
1046
1047 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001048 * score. The maximum score is 15, if this value is reached, we
1049 * break the parsing. Implicitly, this score is reached the ip
1050 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001051 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001052 if (ip_type == AF_INET)
1053 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001054 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001055 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001056 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001057 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001058 return DNS_UPD_NO;
1059 max_score = score;
1060 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001061 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001062
Christopher Faulet67957bd2017-09-27 11:00:59 +02001063 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001064 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001065 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001066
Christopher Faulet67957bd2017-09-27 11:00:59 +02001067 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001068 if (family_priority == AF_INET) {
1069 if (newip4) {
1070 *newip = newip4;
1071 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001072 }
1073 else if (newip6) {
1074 *newip = newip6;
1075 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001076 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001077 if (!currentip_found)
1078 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001079 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001080 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 else if (family_priority == AF_INET6) {
1082 if (newip6) {
1083 *newip = newip6;
1084 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001085 }
1086 else if (newip4) {
1087 *newip = newip4;
1088 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001089 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001090 if (!currentip_found)
1091 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001092 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001093 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001094 else if (family_priority == AF_UNSPEC) {
1095 if (newip6) {
1096 *newip = newip6;
1097 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001098 }
1099 else if (newip4) {
1100 *newip = newip4;
1101 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001102 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001103 if (!currentip_found)
1104 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001105 }
1106
Christopher Faulet67957bd2017-09-27 11:00:59 +02001107 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001108 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001109
Christopher Faulet67957bd2017-09-27 11:00:59 +02001110 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001111 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001112 /* Move the first record to the end of the list, for internal
1113 * round robin */
1114 LIST_DEL(&record->list);
1115 LIST_ADDQ(&dns_p->answer_list, &record->list);
1116 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001117 }
1118 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001119}
1120
Christopher Faulet67957bd2017-09-27 11:00:59 +02001121/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001122 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001123 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1124 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001125 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001126 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1127 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001128 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001129int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001130{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001131 char *ptr;
1132 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001133
Christopher Faulet67957bd2017-09-27 11:00:59 +02001134 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001135 return -1;
1136
Christopher Faulet67957bd2017-09-27 11:00:59 +02001137 ptr = str;
1138 for (i = 0; i < dn_len-1; ++i) {
1139 sz = dn[i];
1140 if (i)
1141 *ptr++ = '.';
1142 memcpy(ptr, dn+i+1, sz);
1143 ptr += sz;
1144 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001145 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001146 *ptr++ = '\0';
1147 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001148}
1149
Christopher Faulet67957bd2017-09-27 11:00:59 +02001150/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1151 *
1152 * <str> must be a null-terminated string. <str_len> must include the
1153 * terminating null byte. <dn> buffer must be allocated and its size must be
1154 * passed in <dn_len>.
1155 *
1156 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1157 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001158 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001159int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001160{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001161 int i, offset;
1162
Christopher Faulet67957bd2017-09-27 11:00:59 +02001163 if (dn_len < str_len + 1)
1164 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001165
Christopher Faulet67957bd2017-09-27 11:00:59 +02001166 /* First byte of dn will be used to store the length of the first
1167 * label */
1168 offset = 0;
1169 for (i = 0; i < str_len; ++i) {
1170 if (str[i] == '.') {
1171 /* 2 or more consecutive dots is invalid */
1172 if (i == offset)
1173 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001174
Christopher Faulet67957bd2017-09-27 11:00:59 +02001175 dn[offset] = (i - offset);
1176 offset = i+1;
1177 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001178 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001179 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001180 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001181 dn[offset] = (i - offset - 1);
1182 dn[i] = '\0';
1183 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001184}
1185
Christopher Faulet67957bd2017-09-27 11:00:59 +02001186/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001187 * - total size
1188 * - each label size individually
1189 * returns:
1190 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1191 * 1 when no error. <err> is left unaffected.
1192 */
1193int dns_hostname_validation(const char *string, char **err)
1194{
1195 const char *c, *d;
1196 int i;
1197
1198 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1199 if (err)
1200 *err = DNS_TOO_LONG_FQDN;
1201 return 0;
1202 }
1203
1204 c = string;
1205 while (*c) {
1206 d = c;
1207
1208 i = 0;
1209 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1210 i++;
1211 if (!((*d == '-') || (*d == '_') ||
1212 ((*d >= 'a') && (*d <= 'z')) ||
1213 ((*d >= 'A') && (*d <= 'Z')) ||
1214 ((*d >= '0') && (*d <= '9')))) {
1215 if (err)
1216 *err = DNS_INVALID_CHARACTER;
1217 return 0;
1218 }
1219 d++;
1220 }
1221
1222 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1223 if (err)
1224 *err = DNS_LABEL_TOO_LONG;
1225 return 0;
1226 }
1227
1228 if (*d == '\0')
1229 goto out;
1230
1231 c = ++d;
1232 }
1233 out:
1234 return 1;
1235}
1236
Christopher Faulet67957bd2017-09-27 11:00:59 +02001237/* Picks up an available resolution from the different resolution list
1238 * associated to a resolvers section, in this order:
1239 * 1. check in resolutions.curr for the same hostname and query_type
1240 * 2. check in resolutions.wait for the same hostname and query_type
1241 * 3. Get a new resolution from resolution pool
1242 *
1243 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001244 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001245static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1246 char **hostname_dn, int hostname_dn_len,
1247 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001248{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001249 struct dns_resolution *res;
1250
1251 if (!*hostname_dn)
1252 goto from_pool;
1253
1254 /* Search for same hostname and query type in resolutions.curr */
1255 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1256 if (!res->hostname_dn)
1257 continue;
1258 if ((query_type == res->prefered_query_type) &&
1259 hostname_dn_len == res->hostname_dn_len &&
1260 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1261 return res;
1262 }
1263
1264 /* Search for same hostname and query type in resolutions.wait */
1265 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1266 if (!res->hostname_dn)
1267 continue;
1268 if ((query_type == res->prefered_query_type) &&
1269 hostname_dn_len == res->hostname_dn_len &&
1270 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1271 return res;
1272 }
1273
1274 from_pool:
1275 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001276 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001277 if (res) {
1278 memset(res, 0, sizeof(*res));
1279 res->resolvers = resolvers;
1280 res->uuid = resolution_uuid;
1281 res->status = RSLV_STATUS_NONE;
1282 res->step = RSLV_STEP_NONE;
1283 res->last_valid = now_ms;
1284
1285 LIST_INIT(&res->requesters);
1286 LIST_INIT(&res->response.answer_list);
1287
1288 res->prefered_query_type = query_type;
1289 res->query_type = query_type;
1290 res->hostname_dn = *hostname_dn;
1291 res->hostname_dn_len = hostname_dn_len;
1292
1293 ++resolution_uuid;
1294
1295 /* Move the resolution to the resolvers wait queue */
1296 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1297 }
1298 return res;
1299}
1300
1301/* Releases a resolution from its requester(s) and move it back to the pool */
1302static void dns_free_resolution(struct dns_resolution *resolution)
1303{
1304 struct dns_requester *req, *reqback;
1305
1306 /* clean up configuration */
1307 dns_reset_resolution(resolution);
1308 resolution->hostname_dn = NULL;
1309 resolution->hostname_dn_len = 0;
1310
1311 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1312 LIST_DEL(&req->list);
1313 req->resolution = NULL;
1314 }
1315
1316 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001317 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001318}
1319
1320/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1321 * on success, -1 otherwise.
1322 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001323int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001324{
1325 struct dns_resolution *res = NULL;
1326 struct dns_requester *req;
1327 struct dns_resolvers *resolvers;
1328 struct server *srv = NULL;
1329 struct dns_srvrq *srvrq = NULL;
1330 char **hostname_dn;
1331 int hostname_dn_len, query_type;
1332
1333 switch (requester_type) {
1334 case OBJ_TYPE_SERVER:
1335 srv = (struct server *)requester;
1336 hostname_dn = &srv->hostname_dn;
1337 hostname_dn_len = srv->hostname_dn_len;
1338 resolvers = srv->resolvers;
1339 query_type = ((srv->dns_opts.family_prio == AF_INET)
1340 ? DNS_RTYPE_A
1341 : DNS_RTYPE_AAAA);
1342 break;
1343
1344 case OBJ_TYPE_SRVRQ:
1345 srvrq = (struct dns_srvrq *)requester;
1346 hostname_dn = &srvrq->hostname_dn;
1347 hostname_dn_len = srvrq->hostname_dn_len;
1348 resolvers = srvrq->resolvers;
1349 query_type = DNS_RTYPE_SRV;
1350 break;
1351
1352 default:
1353 goto err;
1354 }
1355
1356 /* Get a resolution from the resolvers' wait queue or pool */
1357 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1358 goto err;
1359
1360 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001361 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001362 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001363 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001364 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001365 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001366 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001367 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001368 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001369 req->owner = &srv->obj_type;
1370 srv->dns_requester = req;
1371 }
1372 else
1373 req = srv->dns_requester;
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 }
1377 else if (srvrq) {
1378 if (srvrq->dns_requester == NULL) {
1379 if ((req = calloc(1, sizeof(*req))) == NULL)
1380 goto err;
1381 req->owner = &srvrq->obj_type;
1382 srvrq->dns_requester = req;
1383 }
1384 else
1385 req = srvrq->dns_requester;
1386 }
1387 else
1388 goto err;
1389
1390 req->resolution = res;
1391 req->requester_cb = snr_resolution_cb;
1392 req->requester_error_cb = snr_resolution_error_cb;
1393
1394 LIST_ADDQ(&res->requesters, &req->list);
1395 return 0;
1396
1397 err:
1398 if (res && LIST_ISEMPTY(&res->requesters))
1399 dns_free_resolution(res);
1400 return -1;
1401}
1402
1403/* Removes a requester from a DNS resoltion. It takes takes care of all the
1404 * consequences. It also cleans up some parameters from the requester.
1405 */
1406void dns_unlink_resolution(struct dns_requester *requester)
1407{
1408 struct dns_resolution *res;
1409 struct dns_requester *req;
1410
1411 /* Nothing to do */
1412 if (!requester || !requester->resolution)
1413 return;
1414 res = requester->resolution;
1415
1416 /* Clean up the requester */
1417 LIST_DEL(&requester->list);
1418 requester->resolution = NULL;
1419
1420 /* We need to find another requester linked on this resolution */
1421 if (!LIST_ISEMPTY(&res->requesters))
1422 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1423 else {
1424 dns_free_resolution(res);
1425 return;
1426 }
1427
1428 /* Move hostname_dn related pointers to the next requester */
1429 switch (obj_type(req->owner)) {
1430 case OBJ_TYPE_SERVER:
1431 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1432 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1433 break;
1434 case OBJ_TYPE_SRVRQ:
1435 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1436 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1437 break;
1438 default:
1439 res->hostname_dn = NULL;
1440 res->hostname_dn_len = 0;
1441 break;
1442 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001443}
1444
Christopher Faulet67957bd2017-09-27 11:00:59 +02001445/* Called when a network IO is generated on a name server socket for an incoming
1446 * packet. It performs the following actions:
1447 * - check if the packet requires processing (not outdated resolution)
1448 * - ensure the DNS packet received is valid and call requester's callback
1449 * - call requester's error callback if invalid response
1450 * - check the dn_name in the packet against the one sent
1451 */
1452static void dns_resolve_recv(struct dgram_conn *dgram)
1453{
1454 struct dns_nameserver *ns, *tmpns;
1455 struct dns_resolvers *resolvers;
1456 struct dns_resolution *res;
1457 struct dns_query_item *query;
1458 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1459 unsigned char *bufend;
1460 int fd, buflen, dns_resp;
1461 int max_answer_records;
1462 unsigned short query_id;
1463 struct eb32_node *eb;
1464 struct dns_requester *req;
1465
1466 fd = dgram->t.sock.fd;
1467
1468 /* check if ready for reading */
1469 if (!fd_recv_ready(fd))
1470 return;
1471
1472 /* no need to go further if we can't retrieve the nameserver */
1473 if ((ns = dgram->owner) == NULL)
1474 return;
1475
1476 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001477 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001478
1479 /* process all pending input messages */
1480 while (1) {
1481 /* read message received */
1482 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1483 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1484 /* FIXME : for now we consider EAGAIN only */
1485 fd_cant_recv(fd);
1486 break;
1487 }
1488
1489 /* message too big */
1490 if (buflen > resolvers->accepted_payload_size) {
1491 ns->counters.too_big++;
1492 continue;
1493 }
1494
1495 /* initializing variables */
1496 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1497
1498 /* read the query id from the packet (16 bits) */
1499 if (buf + 2 > bufend) {
1500 ns->counters.invalid++;
1501 continue;
1502 }
1503 query_id = dns_response_get_query_id(buf);
1504
1505 /* search the query_id in the pending resolution tree */
1506 eb = eb32_lookup(&resolvers->query_ids, query_id);
1507 if (eb == NULL) {
1508 /* unknown query id means an outdated response and can be safely ignored */
1509 ns->counters.outdated++;
1510 continue;
1511 }
1512
1513 /* known query id means a resolution in prgress */
1514 res = eb32_entry(eb, struct dns_resolution, qid);
1515 if (!res) {
1516 ns->counters.outdated++;
1517 continue;
1518 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001519
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520 /* number of responses received */
1521 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001522
Christopher Faulet67957bd2017-09-27 11:00:59 +02001523 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1524 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001525
Christopher Faulet67957bd2017-09-27 11:00:59 +02001526 switch (dns_resp) {
1527 case DNS_RESP_VALID:
1528 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001529
Christopher Faulet67957bd2017-09-27 11:00:59 +02001530 case DNS_RESP_INVALID:
1531 case DNS_RESP_QUERY_COUNT_ERROR:
1532 case DNS_RESP_WRONG_NAME:
1533 res->status = RSLV_STATUS_INVALID;
1534 ns->counters.invalid++;
1535 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001536
Christopher Faulet67957bd2017-09-27 11:00:59 +02001537 case DNS_RESP_NX_DOMAIN:
1538 res->status = RSLV_STATUS_NX;
1539 ns->counters.nx++;
1540 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001541
Christopher Faulet67957bd2017-09-27 11:00:59 +02001542 case DNS_RESP_REFUSED:
1543 res->status = RSLV_STATUS_REFUSED;
1544 ns->counters.refused++;
1545 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001546
Christopher Faulet67957bd2017-09-27 11:00:59 +02001547 case DNS_RESP_ANCOUNT_ZERO:
1548 res->status = RSLV_STATUS_OTHER;
1549 ns->counters.any_err++;
1550 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001551
Christopher Faulet67957bd2017-09-27 11:00:59 +02001552 case DNS_RESP_CNAME_ERROR:
1553 res->status = RSLV_STATUS_OTHER;
1554 ns->counters.cname_error++;
1555 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001556
Christopher Faulet67957bd2017-09-27 11:00:59 +02001557 case DNS_RESP_TRUNCATED:
1558 res->status = RSLV_STATUS_OTHER;
1559 ns->counters.truncated++;
1560 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001561
Christopher Faulet67957bd2017-09-27 11:00:59 +02001562 case DNS_RESP_NO_EXPECTED_RECORD:
1563 case DNS_RESP_ERROR:
1564 case DNS_RESP_INTERNAL:
1565 res->status = RSLV_STATUS_OTHER;
1566 ns->counters.other++;
1567 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001568 }
1569
Christopher Faulet67957bd2017-09-27 11:00:59 +02001570 /* Wait all nameservers response to handle errors */
1571 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1572 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001573
Christopher Faulet67957bd2017-09-27 11:00:59 +02001574 /* Process error codes */
1575 if (dns_resp != DNS_RESP_VALID) {
1576 if (res->prefered_query_type != res->query_type) {
1577 /* The fallback on the query type was already performed,
1578 * so check the try counter. If it falls to 0, we can
1579 * report an error. Else, wait the next attempt. */
1580 if (!res->try)
1581 goto report_res_error;
1582 }
1583 else {
1584 /* Fallback from A to AAAA or the opposite and re-send
1585 * the resolution immediately. try counter is not
1586 * decremented. */
1587 if (res->prefered_query_type == DNS_RTYPE_A) {
1588 res->query_type = DNS_RTYPE_AAAA;
1589 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001590 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001591 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1592 res->query_type = DNS_RTYPE_A;
1593 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001594 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 }
1596 continue;
1597 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001598
Christopher Faulet67957bd2017-09-27 11:00:59 +02001599 /* Now let's check the query's dname corresponds to the one we
1600 * sent. We can check only the first query of the list. We send
1601 * one query at a time so we get one query in the response */
1602 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1603 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1604 dns_resp = DNS_RESP_WRONG_NAME;
1605 ns->counters.other++;
1606 goto report_res_error;
1607 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001608
Christopher Faulet67957bd2017-09-27 11:00:59 +02001609 /* So the resolution succeeded */
1610 res->status = RSLV_STATUS_VALID;
1611 res->last_valid = now_ms;
1612 ns->counters.valid++;
1613 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001614
Christopher Faulet67957bd2017-09-27 11:00:59 +02001615 report_res_error:
1616 list_for_each_entry(req, &res->requesters, list)
1617 req->requester_error_cb(req, dns_resp);
1618 dns_reset_resolution(res);
1619 LIST_DEL(&res->list);
1620 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1621 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001622
Christopher Faulet67957bd2017-09-27 11:00:59 +02001623 report_res_success:
1624 /* Only the 1rst requester s managed by the server, others are
1625 * from the cache */
1626 tmpns = ns;
1627 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001628 struct server *s = objt_server(req->owner);
1629
1630 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001631 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001632 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001633 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001634 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001635 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001636 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001637
Christopher Faulet67957bd2017-09-27 11:00:59 +02001638 dns_reset_resolution(res);
1639 LIST_DEL(&res->list);
1640 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1641 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001642 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001643 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001644 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001645}
William Lallemand69e96442016-11-19 00:58:54 +01001646
Christopher Faulet67957bd2017-09-27 11:00:59 +02001647/* Called when a resolvers network socket is ready to send data */
1648static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001649{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001650 struct dns_resolvers *resolvers;
1651 struct dns_nameserver *ns;
1652 struct dns_resolution *res;
1653 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001654
Christopher Faulet67957bd2017-09-27 11:00:59 +02001655 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001656
Christopher Faulet67957bd2017-09-27 11:00:59 +02001657 /* check if ready for sending */
1658 if (!fd_send_ready(fd))
1659 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001660
Christopher Faulet67957bd2017-09-27 11:00:59 +02001661 /* we don't want/need to be waked up any more for sending */
1662 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001663
Christopher Faulet67957bd2017-09-27 11:00:59 +02001664 /* no need to go further if we can't retrieve the nameserver */
1665 if ((ns = dgram->owner) == NULL)
1666 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001667
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001669 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001670
Christopher Faulet67957bd2017-09-27 11:00:59 +02001671 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1672 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001673
Christopher Faulet67957bd2017-09-27 11:00:59 +02001674 if (res->nb_queries == resolvers->nb_nameservers)
1675 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001676
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677 trash.len = dns_build_query(res->query_id, res->query_type,
1678 resolvers->accepted_payload_size,
1679 res->hostname_dn, res->hostname_dn_len,
1680 trash.str, trash.size);
1681 if (trash.len == -1)
1682 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001683
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684 ret = send(fd, trash.str, trash.len, 0);
1685 if (ret != trash.len)
1686 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001687
Christopher Faulet67957bd2017-09-27 11:00:59 +02001688 ns->counters.sent++;
1689 res->nb_queries++;
1690 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001691
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 snd_error:
1693 ns->counters.snd_error++;
1694 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001695 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001696 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001698
Christopher Faulet67957bd2017-09-27 11:00:59 +02001699/* Processes DNS resolution. First, it checks the active list to detect expired
1700 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1701 * checks the wait list to trigger new resolutions.
1702 */
1703static struct task *dns_process_resolvers(struct task *t)
1704{
1705 struct dns_resolvers *resolvers = t->context;
1706 struct dns_resolution *res, *resback;
1707 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001708
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001709 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001710
Christopher Faulet67957bd2017-09-27 11:00:59 +02001711 /* Handle all expired resolutions from the active list */
1712 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1713 /* When we find the first resolution in the future, then we can
1714 * stop here */
1715 exp = tick_add(res->last_query, resolvers->timeout.retry);
1716 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001717 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001718
Christopher Faulet67957bd2017-09-27 11:00:59 +02001719 /* If current resolution has been tried too many times and
1720 * finishes in timeout we update its status and remove it from
1721 * the list */
1722 if (!res->try) {
1723 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001724
Christopher Faulet67957bd2017-09-27 11:00:59 +02001725 /* Notify the result to the requesters */
1726 if (!res->nb_responses)
1727 res->status = RSLV_STATUS_TIMEOUT;
1728 list_for_each_entry(req, &res->requesters, list)
1729 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001730
Christopher Faulet67957bd2017-09-27 11:00:59 +02001731 /* Clean up resolution info and remove it from the
1732 * current list */
1733 dns_reset_resolution(res);
1734 LIST_DEL(&res->list);
1735 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001736 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001737 else {
1738 /* Otherwise resend the DNS query and requeue the resolution */
1739 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1740 /* No response received (a real timeout) or fallback already done */
1741 res->query_type = res->prefered_query_type;
1742 res->try--;
1743 }
1744 else {
1745 /* Fallback from A to AAAA or the opposite and re-send
1746 * the resolution immediately. try counter is not
1747 * decremented. */
1748 if (res->prefered_query_type == DNS_RTYPE_A)
1749 res->query_type = DNS_RTYPE_AAAA;
1750 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1751 res->query_type = DNS_RTYPE_A;
1752 else
1753 res->try--;
1754 }
1755 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001756 }
1757 }
William Lallemand69e96442016-11-19 00:58:54 +01001758
Christopher Faulet67957bd2017-09-27 11:00:59 +02001759 /* Handle all resolutions in the wait list */
1760 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1761 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1762 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1763 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001764
Christopher Faulet67957bd2017-09-27 11:00:59 +02001765 if (dns_run_resolution(res) != 1) {
1766 res->last_resolution = now_ms;
1767 LIST_DEL(&res->list);
1768 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001769 }
1770 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001771
Christopher Faulet67957bd2017-09-27 11:00:59 +02001772 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001773 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001774 return t;
1775}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001776
Christopher Faulet67957bd2017-09-27 11:00:59 +02001777/* proto_udp callback functions for a DNS resolution */
1778struct dgram_data_cb resolve_dgram_cb = {
1779 .recv = dns_resolve_recv,
1780 .send = dns_resolve_send,
1781};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001782
Christopher Faulet67957bd2017-09-27 11:00:59 +02001783/* Release memory allocated by DNS */
1784static void dns_deinit(void)
1785{
1786 struct dns_resolvers *resolvers, *resolversback;
1787 struct dns_nameserver *ns, *nsback;
1788 struct dns_resolution *res, *resback;
1789 struct dns_requester *req, *reqback;
1790 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001791
Christopher Faulet67957bd2017-09-27 11:00:59 +02001792 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1793 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1794 free(ns->id);
1795 free((char *)ns->conf.file);
1796 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1797 fd_delete(ns->dgram->t.sock.fd);
1798 free(ns->dgram);
1799 LIST_DEL(&ns->list);
1800 free(ns);
1801 }
1802
1803 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1804 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1805 LIST_DEL(&req->list);
1806 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001807 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001808 dns_free_resolution(res);
1809 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001810
Christopher Faulet67957bd2017-09-27 11:00:59 +02001811 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1812 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1813 LIST_DEL(&req->list);
1814 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001815 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001816 dns_free_resolution(res);
1817 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001818
Christopher Faulet67957bd2017-09-27 11:00:59 +02001819 free(resolvers->id);
1820 free((char *)resolvers->conf.file);
1821 task_delete(resolvers->t);
1822 task_free(resolvers->t);
1823 LIST_DEL(&resolvers->list);
1824 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001825 }
1826
Christopher Faulet67957bd2017-09-27 11:00:59 +02001827 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1828 free(srvrq->name);
1829 free(srvrq->hostname_dn);
1830 LIST_DEL(&srvrq->list);
1831 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001832 }
1833
Willy Tarreaubafbe012017-11-24 17:34:44 +01001834 pool_destroy(dns_answer_item_pool);
1835 pool_destroy(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001836}
1837
Christopher Faulet67957bd2017-09-27 11:00:59 +02001838/* Finalizes the DNS configuration by allocating required resources and checking
1839 * live parameters.
1840 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001841 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001842static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001843{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001844 struct dns_resolvers *resolvers;
1845 struct proxy *px;
1846 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001847
Christopher Faulet67957bd2017-09-27 11:00:59 +02001848 /* allocate pool of resolution per resolvers */
1849 list_for_each_entry(resolvers, &dns_resolvers, list) {
1850 struct dns_nameserver *ns;
1851 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001852
Christopher Faulet67957bd2017-09-27 11:00:59 +02001853 /* Check if we can create the socket with nameservers info */
1854 list_for_each_entry(ns, &resolvers->nameservers, list) {
1855 struct dgram_conn *dgram = NULL;
1856 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001857
Christopher Faulet67957bd2017-09-27 11:00:59 +02001858 /* Check nameserver info */
1859 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001860 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1861 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001862 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001863 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001864 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001865 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001866 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1867 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001868 close(fd);
1869 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001870 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001871 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001872 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001873
Christopher Faulet67957bd2017-09-27 11:00:59 +02001874 /* Create dgram structure that will hold the UPD socket
1875 * and attach it on the current nameserver */
1876 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001877 ha_alert("config: resolvers '%s' : out of memory.\n",
1878 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001879 err_code |= (ERR_ALERT|ERR_ABORT);
1880 goto err;
1881 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001882
Christopher Faulet67957bd2017-09-27 11:00:59 +02001883 /* Leave dgram partially initialized, no FD attached for
1884 * now. */
1885 dgram->owner = ns;
1886 dgram->data = &resolve_dgram_cb;
1887 dgram->t.sock.fd = -1;
1888 ns->dgram = dgram;
1889 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001890
Christopher Faulet67957bd2017-09-27 11:00:59 +02001891 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001892 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001893 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001894 err_code |= (ERR_ALERT|ERR_ABORT);
1895 goto err;
1896 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001897
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 /* Update task's parameters */
1899 t->process = dns_process_resolvers;
1900 t->context = resolvers;
1901 resolvers->t = t;
1902 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001903 }
1904
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001905 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001906 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001907
Christopher Faulet67957bd2017-09-27 11:00:59 +02001908 for (srv = px->srv; srv; srv = srv->next) {
1909 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001910
Christopher Faulet67957bd2017-09-27 11:00:59 +02001911 if (!srv->resolvers_id)
1912 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001913
Christopher Faulet67957bd2017-09-27 11:00:59 +02001914 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001915 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1916 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001917 err_code |= (ERR_ALERT|ERR_ABORT);
1918 continue;
1919 }
1920 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001921
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 if (srv->srvrq && !srv->srvrq->resolvers) {
1923 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001924 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001925 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1926 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001927 err_code |= (ERR_ALERT|ERR_ABORT);
1928 continue;
1929 }
1930 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001931 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001932 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1933 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934 err_code |= (ERR_ALERT|ERR_ABORT);
1935 continue;
1936 }
1937 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001938 }
1939
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940 if (err_code & (ERR_ALERT|ERR_ABORT))
1941 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001942
Christopher Faulet67957bd2017-09-27 11:00:59 +02001943 return err_code;
1944 err:
1945 dns_deinit();
1946 return err_code;
1947
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001948}
1949
Christopher Faulet67957bd2017-09-27 11:00:59 +02001950/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001951static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001952{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001953 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001954
Christopher Faulet67957bd2017-09-27 11:00:59 +02001955 if (*args[2]) {
1956 list_for_each_entry(presolvers, &dns_resolvers, list) {
1957 if (strcmp(presolvers->id, args[2]) == 0) {
1958 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001959 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001961 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001962 if (appctx->ctx.cli.p0 == NULL) {
1963 appctx->ctx.cli.severity = LOG_ERR;
1964 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1965 appctx->st0 = CLI_ST_PRINT;
1966 return 1;
1967 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001968 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001969 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001970}
1971
Christopher Faulet67957bd2017-09-27 11:00:59 +02001972/* Dumps counters from all resolvers section and associated name servers. It
1973 * returns 0 if the output buffer is full and it needs to be called again,
1974 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001975 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001976 */
1977static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1978{
1979 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980 struct dns_resolvers *resolvers;
1981 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001982
1983 chunk_reset(&trash);
1984
1985 switch (appctx->st2) {
1986 case STAT_ST_INIT:
1987 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1988 /* fall through */
1989
1990 case STAT_ST_LIST:
1991 if (LIST_ISEMPTY(&dns_resolvers)) {
1992 chunk_appendf(&trash, "No resolvers found\n");
1993 }
1994 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001995 list_for_each_entry(resolvers, &dns_resolvers, list) {
1996 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01001997 continue;
1998
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2000 list_for_each_entry(ns, &resolvers->nameservers, list) {
2001 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2002 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2003 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2004 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2005 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2006 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2007 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2008 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2009 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2010 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2011 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2012 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2013 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2014 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2015 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2016 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002017 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002018 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002019 }
2020 }
2021
2022 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002023 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002024 /* let's try again later from this session. We add ourselves into
2025 * this session's users so that it can remove us upon termination.
2026 */
2027 si->flags |= SI_FL_WAIT_ROOM;
2028 return 0;
2029 }
William Lallemand69e96442016-11-19 00:58:54 +01002030 /* fall through */
2031
2032 default:
2033 appctx->st2 = STAT_ST_FIN;
2034 return 1;
2035 }
2036}
2037
2038/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002039static struct cli_kw_list cli_kws = {{ }, {
2040 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2041 " associated name servers",
2042 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2043 {{},}
2044 }
2045};
William Lallemand69e96442016-11-19 00:58:54 +01002046
2047
2048__attribute__((constructor))
2049static void __dns_init(void)
2050{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002051 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2052 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2053
Christopher Faulet67957bd2017-09-27 11:00:59 +02002054 hap_register_post_check(dns_finalize_config);
2055 hap_register_post_deinit(dns_deinit);
2056
William Lallemand69e96442016-11-19 00:58:54 +01002057 cli_register_kw(&cli_kws);
2058}