blob: c86e57d9c829f53e4fba8367a30425c08962c96e [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);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200351 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
352 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
353 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200354}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200355
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200356
Christopher Faulet67957bd2017-09-27 11:00:59 +0200357/* Resets some resolution parameters to initial values and also delete the query
358 * ID from the resolver's tree.
359 */
360static void dns_reset_resolution(struct dns_resolution *resolution)
361{
362 /* update resolution status */
363 resolution->step = RSLV_STEP_NONE;
364 resolution->try = 0;
365 resolution->last_resolution = now_ms;
366 resolution->nb_queries = 0;
367 resolution->nb_responses = 0;
368 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200369
Christopher Faulet67957bd2017-09-27 11:00:59 +0200370 /* clean up query id */
371 eb32_delete(&resolution->qid);
372 resolution->query_id = 0;
373 resolution->qid.key = 0;
374}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200375
Christopher Faulet67957bd2017-09-27 11:00:59 +0200376/* Returns the query id contained in a DNS response */
377static inline unsigned short dns_response_get_query_id(unsigned char *resp)
378{
379 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200380}
381
Christopher Faulet67957bd2017-09-27 11:00:59 +0200382
383/* Analyses, re-builds and copies the name <name> from the DNS response packet
384 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
385 * for compressed data. The result is copied into <dest>, ensuring we don't
386 * overflow using <dest_len> Returns the number of bytes the caller can move
387 * forward. If 0 it means an error occurred while parsing the name. <offset> is
388 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200389 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200390int dns_read_name(unsigned char *buffer, unsigned char *bufend,
391 unsigned char *name, char *destination, int dest_len,
392 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200393{
394 int nb_bytes = 0, n = 0;
395 int label_len;
396 unsigned char *reader = name;
397 char *dest = destination;
398
399 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200400 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200401 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200402 /* Must point BEFORE current position */
403 if ((buffer + reader[1]) > reader)
404 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200405
Christopher Faulet67957bd2017-09-27 11:00:59 +0200406 n = dns_read_name(buffer, bufend, buffer + reader[1],
407 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200408 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200409 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200410
Christopher Faulet67957bd2017-09-27 11:00:59 +0200411 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200412 nb_bytes += n;
413 goto out;
414 }
415
416 label_len = *reader;
417 if (label_len == 0)
418 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200419
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200420 /* Check if:
421 * - we won't read outside the buffer
422 * - there is enough place in the destination
423 */
424 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200425 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200426
427 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200428 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200429
430 memcpy(dest, reader, label_len);
431
Christopher Faulet67957bd2017-09-27 11:00:59 +0200432 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200433 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200434 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200435 }
436
Christopher Faulet67957bd2017-09-27 11:00:59 +0200437 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200438 /* offset computation:
439 * parse from <name> until finding either NULL or a pointer "c0xx"
440 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200441 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200442 *offset = 0;
443 while (reader < bufend) {
444 if ((reader[0] & 0xc0) == 0xc0) {
445 *offset += 2;
446 break;
447 }
448 else if (*reader == 0) {
449 *offset += 1;
450 break;
451 }
452 *offset += 1;
453 ++reader;
454 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200455 return nb_bytes;
456
Christopher Faulet67957bd2017-09-27 11:00:59 +0200457 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200458 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200459}
460
Christopher Faulet67957bd2017-09-27 11:00:59 +0200461/* Checks for any obsolete record, also identify any SRV request, and try to
462 * find a corresponding server.
463*/
464static void dns_check_dns_response(struct dns_resolution *res)
465{
466 struct dns_resolvers *resolvers = res->resolvers;
467 struct dns_requester *req, *reqback;
468 struct dns_answer_item *item, *itemback;
469 struct server *srv;
470 struct dns_srvrq *srvrq;
471
472 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
473
474 /* Remove obsolete items */
475 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
476 if (item->type != DNS_RTYPE_SRV)
477 goto rm_obselete_item;
478
479 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
480 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
481 continue;
482
483 /* Remove any associated server */
484 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100485 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200486 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
487 item->data_len == srv->hostname_dn_len &&
488 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
489 snr_update_srv_status(srv, 1);
490 free(srv->hostname);
491 free(srv->hostname_dn);
492 srv->hostname = NULL;
493 srv->hostname_dn = NULL;
494 srv->hostname_dn_len = 0;
495 dns_unlink_resolution(srv->dns_requester);
496 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100497 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200498 }
499 }
500
501 rm_obselete_item:
502 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100503 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200504 continue;
505 }
506
507 if (item->type != DNS_RTYPE_SRV)
508 continue;
509
510 /* Now process SRV records */
511 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
512 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
513 continue;
514
515 /* Check if a server already uses that hostname */
516 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100517 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200518 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
519 item->data_len == srv->hostname_dn_len &&
520 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100521 int ha_weight;
522
523 /* Make sure weight is at least 1, so
524 * that the server will be used.
525 */
526 ha_weight = item->weight / 256 + 1;
527 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200528 char weight[9];
529
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100530 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200531 server_parse_weight_change_request(srv, weight);
532 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100533 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200534 break;
535 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100536 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200537 }
538 if (srv)
539 continue;
540
541 /* If not, try to find a server with undefined hostname */
542 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100543 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200544 if (srv->srvrq == srvrq && !srv->hostname_dn)
545 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100546 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200547 }
548 /* And update this server, if found */
549 if (srv) {
550 const char *msg = NULL;
551 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100552 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200553 char hostname[DNS_MAX_NAME_SIZE];
554
555 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100556 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100557 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200558 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100559 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100560 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200561 if (msg)
562 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
563
564 srv->svc_port = item->port;
565 srv->flags &= ~SRV_F_MAPPORTS;
566 if ((srv->check.state & CHK_ST_CONFIGURED) &&
567 !(srv->flags & SRV_F_CHECKPORT))
568 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100569
570 /* Make sure weight is at least 1, so
571 * that the server will be used.
572 */
573 ha_weight = item->weight / 256 + 1;
574
575 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200576 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100577 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200578 }
579 }
580 }
581}
582
583/* Validates that the buffer DNS response provided in <resp> and finishing
584 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200585 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200586 * The result is stored in <resolution>' response, buf_response,
587 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200588 *
589 * This function returns one of the DNS_RESP_* code to indicate the type of
590 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200591 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200592static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
593 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200594{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200595 unsigned char *reader;
596 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200597 int len, flags, offset;
598 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200599 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200600 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200601 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200602 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200603 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200604
Christopher Faulet67957bd2017-09-27 11:00:59 +0200605 reader = resp;
606 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200607 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200608 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200609
Christopher Faulet67957bd2017-09-27 11:00:59 +0200610 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200611 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200612
613 /* query id */
614 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200615 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200616 dns_p->header.id = reader[0] * 256 + reader[1];
617 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618
Christopher Faulet67957bd2017-09-27 11:00:59 +0200619 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200620 * First byte contains:
621 * - response flag (1 bit)
622 * - opcode (4 bits)
623 * - authoritative (1 bit)
624 * - truncated (1 bit)
625 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200626 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200627 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200628 return DNS_RESP_INVALID;
629
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200630 flags = reader[0] * 256 + reader[1];
631
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200632 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
633 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200634 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200635 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200636 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200637 return DNS_RESP_ERROR;
638 }
639
Christopher Faulet67957bd2017-09-27 11:00:59 +0200640 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200641 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200642
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200643 /* 2 bytes for question count */
644 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200645 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200646 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200647 /* (for now) we send one query only, so we expect only one in the
648 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200649 if (dns_p->header.qdcount != 1)
650 return DNS_RESP_QUERY_COUNT_ERROR;
651 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200652 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200653 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200654
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200655 /* 2 bytes for answer count */
656 if (reader + 2 >= bufend)
657 return DNS_RESP_INVALID;
658 dns_p->header.ancount = reader[0] * 256 + reader[1];
659 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200660 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200661 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200662 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200663 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200664 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200665
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200666 /* 2 bytes authority count */
667 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200668 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 dns_p->header.nscount = reader[0] * 256 + reader[1];
670 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200671
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200672 /* 2 bytes additional count */
673 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200674 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200675 dns_p->header.arcount = reader[0] * 256 + reader[1];
676 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200677
Christopher Faulet67957bd2017-09-27 11:00:59 +0200678 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 LIST_INIT(&dns_p->query_list);
680 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 +0200681 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200682 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200683 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
685 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200686 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200687 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200688
Christopher Faulet67957bd2017-09-27 11:00:59 +0200689 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200690 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200691 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200692 offset = 0;
693 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200694
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200695 if (len == 0)
696 return DNS_RESP_INVALID;
697
698 reader += offset;
699 previous_dname = dns_query->name;
700
701 /* move forward 2 bytes for question type */
702 if (reader + 2 >= bufend)
703 return DNS_RESP_INVALID;
704 dns_query->type = reader[0] * 256 + reader[1];
705 reader += 2;
706
707 /* move forward 2 bytes for question class */
708 if (reader + 2 >= bufend)
709 return DNS_RESP_INVALID;
710 dns_query->class = reader[0] * 256 + reader[1];
711 reader += 2;
712 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200713
Baptiste Assmann251abb92017-08-11 09:58:27 +0200714 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200715 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200716 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
717 return DNS_RESP_TRUNCATED;
718
Baptiste Assmann325137d2015-04-13 23:40:55 +0200719 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200720 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200721 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200722 if (reader >= bufend)
723 return DNS_RESP_INVALID;
724
Willy Tarreaubafbe012017-11-24 17:34:44 +0100725 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200726 if (dns_answer_record == NULL)
727 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200728
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200729 offset = 0;
730 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200731
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200732 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100733 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200734 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200735 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200736
Christopher Faulet67957bd2017-09-27 11:00:59 +0200737 /* Check if the current record dname is valid. previous_dname
738 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200739 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100740 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200741 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200742 /* First record, means a mismatch issue between
743 * queried dname and dname found in the first
744 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200745 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200746 }
747 else {
748 /* If not the first record, this means we have a
749 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200750 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200751 }
752
Baptiste Assmann325137d2015-04-13 23:40:55 +0200753 }
754
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200755 memcpy(dns_answer_record->name, tmpname, len);
756 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200757
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200759 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100760 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200761 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200762 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200763
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200765 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100766 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200768 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200769 dns_answer_record->type = reader[0] * 256 + reader[1];
770 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200771
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200772 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200773 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100774 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200775 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200776 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200777 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200778 reader += 2;
779
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200780 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200781 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100782 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200783 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200784 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200785 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
786 + reader[2] * 256 + reader[3];
787 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200788
Christopher Faulet67957bd2017-09-27 11:00:59 +0200789 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200790 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100791 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200792 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200793 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200794 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200795
Christopher Faulet67957bd2017-09-27 11:00:59 +0200796 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200797 reader += 2;
798
Christopher Faulet67957bd2017-09-27 11:00:59 +0200799 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200800 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200801 case DNS_RTYPE_A:
802 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200803 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100804 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200805 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200806 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200807 dns_answer_record->address.sa_family = AF_INET;
808 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
809 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200810 break;
811
812 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200813 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200814 * no IP could be found and last record was a CNAME. Could be triggered
815 * by a wrong query type
816 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200817 * + 1 because dns_answer_record_id starts at 0
818 * while number of answers is an integer and
819 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200820 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200821 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100822 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200823 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200824 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200825
826 offset = 0;
827 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200828 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100829 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200830 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200831 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200832
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200833 memcpy(dns_answer_record->target, tmpname, len);
834 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200835 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200836 break;
837
Olivier Houchard8da5f982017-08-04 18:35:36 +0200838
839 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200840 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200841 * - 2 bytes for the priority
842 * - 2 bytes for the weight
843 * - 2 bytes for the port
844 * - the target hostname
845 */
846 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100847 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200848 return DNS_RESP_INVALID;
849 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200850 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200851 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200852 dns_answer_record->weight = 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->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200855 reader += sizeof(uint16_t);
856 offset = 0;
857 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
858 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100859 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200860 return DNS_RESP_INVALID;
861 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200862 dns_answer_record->data_len = len;
863 memcpy(dns_answer_record->target, tmpname, len);
864 dns_answer_record->target[len] = 0;
865 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200866
Baptiste Assmann325137d2015-04-13 23:40:55 +0200867 case DNS_RTYPE_AAAA:
868 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200869 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100870 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200871 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200872 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200873 dns_answer_record->address.sa_family = AF_INET6;
874 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
875 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200876 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200877
Baptiste Assmann325137d2015-04-13 23:40:55 +0200878 } /* switch (record type) */
879
Christopher Faulet67957bd2017-09-27 11:00:59 +0200880 /* Increment the counter for number of records saved into our
881 * local response */
882 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200883
Christopher Faulet67957bd2017-09-27 11:00:59 +0200884 /* Move forward dns_answer_record->data_len for analyzing next
885 * record in the response */
886 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
887 ? offset
888 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200889
890 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200891 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200892 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
893 if (tmp_record->type != dns_answer_record->type)
894 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200895
896 switch(tmp_record->type) {
897 case DNS_RTYPE_A:
898 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
899 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
900 sizeof(in_addr_t)))
901 found = 1;
902 break;
903
904 case DNS_RTYPE_AAAA:
905 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
906 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
907 sizeof(struct in6_addr)))
908 found = 1;
909 break;
910
Olivier Houchard8da5f982017-08-04 18:35:36 +0200911 case DNS_RTYPE_SRV:
912 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200913 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200914 dns_answer_record->port == tmp_record->port) {
915 tmp_record->weight = dns_answer_record->weight;
916 found = 1;
917 }
918 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200919
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200920 default:
921 break;
922 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200923
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200924 if (found == 1)
925 break;
926 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200927
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200928 if (found == 1) {
929 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100930 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200931 }
932 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200933 dns_answer_record->last_seen = now.tv_sec;
934 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
935 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200936 } /* for i 0 to ancount */
937
Christopher Faulet67957bd2017-09-27 11:00:59 +0200938 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200939 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200940 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200941 return DNS_RESP_VALID;
942}
943
Christopher Faulet67957bd2017-09-27 11:00:59 +0200944/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200945 * If existing IP not found, return the first IP matching family_priority,
946 * otherwise, first ip found
947 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200948 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200949 * For both cases above, dns_validate_dns_response is required
950 * returns one of the DNS_UPD_* code
951 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200952int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200953 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100954 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200955 void **newip, short *newip_sin_family,
956 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200957{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200958 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100959 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200960 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200961 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100962 int currentip_sel;
963 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100964 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200965
Christopher Faulet67957bd2017-09-27 11:00:59 +0200966 family_priority = dns_opts->family_prio;
967 *newip = newip4 = newip6 = NULL;
968 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200969 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200970 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200971
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100972 /* Select an IP regarding configuration preference.
973 * Top priority is the prefered network ip version,
974 * second priority is the prefered network.
975 * the last priority is the currently used IP,
976 *
977 * For these three priorities, a score is calculated. The
978 * weight are:
Baptiste Assmann741e00a2018-06-22 12:51:51 +0200979 * 8 - prefered ip version.
Baptistefc725902016-12-26 23:21:08 +0100980 * 4 - prefered network.
981 * 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 +0100982 * 1 - current ip.
983 * The result with the biggest score is returned.
984 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200985
986 list_for_each_entry(record, &dns_p->answer_list, list) {
987 void *ip;
988 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100989
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200990 if (record->type == DNS_RTYPE_A) {
991 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
992 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200993 }
994 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200995 ip_type = AF_INET6;
996 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200997 }
998 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200999 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001000 score = 0;
1001
1002 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001003 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001004 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001005
1006 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001007 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001008
1009 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001010 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001011 continue;
1012
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001013 if ((ip_type == AF_INET &&
1014 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001015 &dns_opts->pref_net[j].mask.in4,
1016 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001017 (ip_type == AF_INET6 &&
1018 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001019 &dns_opts->pref_net[j].mask.in6,
1020 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001021 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001022 break;
1023 }
1024 }
1025
Christopher Faulet67957bd2017-09-27 11:00:59 +02001026 /* Check if the IP found in the record is already affected to a
1027 * member of a group. If yes, the score should be incremented
1028 * by 2. */
1029 if (owner && snr_check_ip_callback(owner, ip, &ip_type))
1030 continue;
1031
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001032 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001033 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001034 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001035 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001036 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001037 !memcmp(ip, currentip, 16)))) {
1038 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001039 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001040 }
1041 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001042 currentip_sel = 0;
1043
1044 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001045 * score. The maximum score is 15, if this value is reached, we
1046 * break the parsing. Implicitly, this score is reached the ip
1047 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001048 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001049 if (ip_type == AF_INET)
1050 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001051 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001052 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001053 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001054 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001055 return DNS_UPD_NO;
1056 max_score = score;
1057 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001058 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001059
Christopher Faulet67957bd2017-09-27 11:00:59 +02001060 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001061 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001062 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001063
Christopher Faulet67957bd2017-09-27 11:00:59 +02001064 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001065 if (family_priority == AF_INET) {
1066 if (newip4) {
1067 *newip = newip4;
1068 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001069 }
1070 else if (newip6) {
1071 *newip = newip6;
1072 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001073 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001074 if (!currentip_found)
1075 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001076 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001077 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001078 else if (family_priority == AF_INET6) {
1079 if (newip6) {
1080 *newip = newip6;
1081 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001082 }
1083 else if (newip4) {
1084 *newip = newip4;
1085 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001086 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001087 if (!currentip_found)
1088 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001089 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001090 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001091 else if (family_priority == AF_UNSPEC) {
1092 if (newip6) {
1093 *newip = newip6;
1094 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001095 }
1096 else if (newip4) {
1097 *newip = newip4;
1098 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001099 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100 if (!currentip_found)
1101 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001102 }
1103
Christopher Faulet67957bd2017-09-27 11:00:59 +02001104 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001105 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001106
Christopher Faulet67957bd2017-09-27 11:00:59 +02001107 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001108 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001109 /* Move the first record to the end of the list, for internal
1110 * round robin */
1111 LIST_DEL(&record->list);
1112 LIST_ADDQ(&dns_p->answer_list, &record->list);
1113 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001114 }
1115 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001116}
1117
Christopher Faulet67957bd2017-09-27 11:00:59 +02001118/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001119 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001120 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1121 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001122 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001123 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1124 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001125 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001126int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001127{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001128 char *ptr;
1129 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001130
Christopher Faulet67957bd2017-09-27 11:00:59 +02001131 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001132 return -1;
1133
Christopher Faulet67957bd2017-09-27 11:00:59 +02001134 ptr = str;
1135 for (i = 0; i < dn_len-1; ++i) {
1136 sz = dn[i];
1137 if (i)
1138 *ptr++ = '.';
1139 memcpy(ptr, dn+i+1, sz);
1140 ptr += sz;
1141 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001142 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001143 *ptr++ = '\0';
1144 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001145}
1146
Christopher Faulet67957bd2017-09-27 11:00:59 +02001147/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1148 *
1149 * <str> must be a null-terminated string. <str_len> must include the
1150 * terminating null byte. <dn> buffer must be allocated and its size must be
1151 * passed in <dn_len>.
1152 *
1153 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1154 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001155 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001156int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001157{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001158 int i, offset;
1159
Christopher Faulet67957bd2017-09-27 11:00:59 +02001160 if (dn_len < str_len + 1)
1161 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001162
Christopher Faulet67957bd2017-09-27 11:00:59 +02001163 /* First byte of dn will be used to store the length of the first
1164 * label */
1165 offset = 0;
1166 for (i = 0; i < str_len; ++i) {
1167 if (str[i] == '.') {
1168 /* 2 or more consecutive dots is invalid */
1169 if (i == offset)
1170 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001171
Christopher Faulet67957bd2017-09-27 11:00:59 +02001172 dn[offset] = (i - offset);
1173 offset = i+1;
1174 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001175 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001176 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001177 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001178 dn[offset] = (i - offset - 1);
1179 dn[i] = '\0';
1180 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001181}
1182
Christopher Faulet67957bd2017-09-27 11:00:59 +02001183/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001184 * - total size
1185 * - each label size individually
1186 * returns:
1187 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1188 * 1 when no error. <err> is left unaffected.
1189 */
1190int dns_hostname_validation(const char *string, char **err)
1191{
1192 const char *c, *d;
1193 int i;
1194
1195 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1196 if (err)
1197 *err = DNS_TOO_LONG_FQDN;
1198 return 0;
1199 }
1200
1201 c = string;
1202 while (*c) {
1203 d = c;
1204
1205 i = 0;
1206 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1207 i++;
1208 if (!((*d == '-') || (*d == '_') ||
1209 ((*d >= 'a') && (*d <= 'z')) ||
1210 ((*d >= 'A') && (*d <= 'Z')) ||
1211 ((*d >= '0') && (*d <= '9')))) {
1212 if (err)
1213 *err = DNS_INVALID_CHARACTER;
1214 return 0;
1215 }
1216 d++;
1217 }
1218
1219 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1220 if (err)
1221 *err = DNS_LABEL_TOO_LONG;
1222 return 0;
1223 }
1224
1225 if (*d == '\0')
1226 goto out;
1227
1228 c = ++d;
1229 }
1230 out:
1231 return 1;
1232}
1233
Christopher Faulet67957bd2017-09-27 11:00:59 +02001234/* Picks up an available resolution from the different resolution list
1235 * associated to a resolvers section, in this order:
1236 * 1. check in resolutions.curr for the same hostname and query_type
1237 * 2. check in resolutions.wait for the same hostname and query_type
1238 * 3. Get a new resolution from resolution pool
1239 *
1240 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001241 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001242static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1243 char **hostname_dn, int hostname_dn_len,
1244 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001245{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001246 struct dns_resolution *res;
1247
1248 if (!*hostname_dn)
1249 goto from_pool;
1250
1251 /* Search for same hostname and query type in resolutions.curr */
1252 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1253 if (!res->hostname_dn)
1254 continue;
1255 if ((query_type == res->prefered_query_type) &&
1256 hostname_dn_len == res->hostname_dn_len &&
1257 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1258 return res;
1259 }
1260
1261 /* Search for same hostname and query type in resolutions.wait */
1262 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1263 if (!res->hostname_dn)
1264 continue;
1265 if ((query_type == res->prefered_query_type) &&
1266 hostname_dn_len == res->hostname_dn_len &&
1267 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1268 return res;
1269 }
1270
1271 from_pool:
1272 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001273 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001274 if (res) {
1275 memset(res, 0, sizeof(*res));
1276 res->resolvers = resolvers;
1277 res->uuid = resolution_uuid;
1278 res->status = RSLV_STATUS_NONE;
1279 res->step = RSLV_STEP_NONE;
1280 res->last_valid = now_ms;
1281
1282 LIST_INIT(&res->requesters);
1283 LIST_INIT(&res->response.answer_list);
1284
1285 res->prefered_query_type = query_type;
1286 res->query_type = query_type;
1287 res->hostname_dn = *hostname_dn;
1288 res->hostname_dn_len = hostname_dn_len;
1289
1290 ++resolution_uuid;
1291
1292 /* Move the resolution to the resolvers wait queue */
1293 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1294 }
1295 return res;
1296}
1297
1298/* Releases a resolution from its requester(s) and move it back to the pool */
1299static void dns_free_resolution(struct dns_resolution *resolution)
1300{
1301 struct dns_requester *req, *reqback;
1302
1303 /* clean up configuration */
1304 dns_reset_resolution(resolution);
1305 resolution->hostname_dn = NULL;
1306 resolution->hostname_dn_len = 0;
1307
1308 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1309 LIST_DEL(&req->list);
1310 req->resolution = NULL;
1311 }
1312
1313 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001314 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001315}
1316
1317/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1318 * on success, -1 otherwise.
1319 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001320int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001321{
1322 struct dns_resolution *res = NULL;
1323 struct dns_requester *req;
1324 struct dns_resolvers *resolvers;
1325 struct server *srv = NULL;
1326 struct dns_srvrq *srvrq = NULL;
1327 char **hostname_dn;
1328 int hostname_dn_len, query_type;
1329
1330 switch (requester_type) {
1331 case OBJ_TYPE_SERVER:
1332 srv = (struct server *)requester;
1333 hostname_dn = &srv->hostname_dn;
1334 hostname_dn_len = srv->hostname_dn_len;
1335 resolvers = srv->resolvers;
1336 query_type = ((srv->dns_opts.family_prio == AF_INET)
1337 ? DNS_RTYPE_A
1338 : DNS_RTYPE_AAAA);
1339 break;
1340
1341 case OBJ_TYPE_SRVRQ:
1342 srvrq = (struct dns_srvrq *)requester;
1343 hostname_dn = &srvrq->hostname_dn;
1344 hostname_dn_len = srvrq->hostname_dn_len;
1345 resolvers = srvrq->resolvers;
1346 query_type = DNS_RTYPE_SRV;
1347 break;
1348
1349 default:
1350 goto err;
1351 }
1352
1353 /* Get a resolution from the resolvers' wait queue or pool */
1354 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1355 goto err;
1356
1357 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001358 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001359 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001360 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001361 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001362 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001363 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001364 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001365 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001366 req->owner = &srv->obj_type;
1367 srv->dns_requester = req;
1368 }
1369 else
1370 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001371 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001372 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001373 }
1374 else if (srvrq) {
1375 if (srvrq->dns_requester == NULL) {
1376 if ((req = calloc(1, sizeof(*req))) == NULL)
1377 goto err;
1378 req->owner = &srvrq->obj_type;
1379 srvrq->dns_requester = req;
1380 }
1381 else
1382 req = srvrq->dns_requester;
1383 }
1384 else
1385 goto err;
1386
1387 req->resolution = res;
1388 req->requester_cb = snr_resolution_cb;
1389 req->requester_error_cb = snr_resolution_error_cb;
1390
1391 LIST_ADDQ(&res->requesters, &req->list);
1392 return 0;
1393
1394 err:
1395 if (res && LIST_ISEMPTY(&res->requesters))
1396 dns_free_resolution(res);
1397 return -1;
1398}
1399
1400/* Removes a requester from a DNS resoltion. It takes takes care of all the
1401 * consequences. It also cleans up some parameters from the requester.
1402 */
1403void dns_unlink_resolution(struct dns_requester *requester)
1404{
1405 struct dns_resolution *res;
1406 struct dns_requester *req;
1407
1408 /* Nothing to do */
1409 if (!requester || !requester->resolution)
1410 return;
1411 res = requester->resolution;
1412
1413 /* Clean up the requester */
1414 LIST_DEL(&requester->list);
1415 requester->resolution = NULL;
1416
1417 /* We need to find another requester linked on this resolution */
1418 if (!LIST_ISEMPTY(&res->requesters))
1419 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1420 else {
1421 dns_free_resolution(res);
1422 return;
1423 }
1424
1425 /* Move hostname_dn related pointers to the next requester */
1426 switch (obj_type(req->owner)) {
1427 case OBJ_TYPE_SERVER:
1428 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1429 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1430 break;
1431 case OBJ_TYPE_SRVRQ:
1432 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1433 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1434 break;
1435 default:
1436 res->hostname_dn = NULL;
1437 res->hostname_dn_len = 0;
1438 break;
1439 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001440}
1441
Christopher Faulet67957bd2017-09-27 11:00:59 +02001442/* Called when a network IO is generated on a name server socket for an incoming
1443 * packet. It performs the following actions:
1444 * - check if the packet requires processing (not outdated resolution)
1445 * - ensure the DNS packet received is valid and call requester's callback
1446 * - call requester's error callback if invalid response
1447 * - check the dn_name in the packet against the one sent
1448 */
1449static void dns_resolve_recv(struct dgram_conn *dgram)
1450{
1451 struct dns_nameserver *ns, *tmpns;
1452 struct dns_resolvers *resolvers;
1453 struct dns_resolution *res;
1454 struct dns_query_item *query;
1455 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1456 unsigned char *bufend;
1457 int fd, buflen, dns_resp;
1458 int max_answer_records;
1459 unsigned short query_id;
1460 struct eb32_node *eb;
1461 struct dns_requester *req;
1462
1463 fd = dgram->t.sock.fd;
1464
1465 /* check if ready for reading */
1466 if (!fd_recv_ready(fd))
1467 return;
1468
1469 /* no need to go further if we can't retrieve the nameserver */
1470 if ((ns = dgram->owner) == NULL)
1471 return;
1472
1473 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001474 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001475
1476 /* process all pending input messages */
1477 while (1) {
1478 /* read message received */
1479 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1480 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1481 /* FIXME : for now we consider EAGAIN only */
1482 fd_cant_recv(fd);
1483 break;
1484 }
1485
1486 /* message too big */
1487 if (buflen > resolvers->accepted_payload_size) {
1488 ns->counters.too_big++;
1489 continue;
1490 }
1491
1492 /* initializing variables */
1493 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1494
1495 /* read the query id from the packet (16 bits) */
1496 if (buf + 2 > bufend) {
1497 ns->counters.invalid++;
1498 continue;
1499 }
1500 query_id = dns_response_get_query_id(buf);
1501
1502 /* search the query_id in the pending resolution tree */
1503 eb = eb32_lookup(&resolvers->query_ids, query_id);
1504 if (eb == NULL) {
1505 /* unknown query id means an outdated response and can be safely ignored */
1506 ns->counters.outdated++;
1507 continue;
1508 }
1509
1510 /* known query id means a resolution in prgress */
1511 res = eb32_entry(eb, struct dns_resolution, qid);
1512 if (!res) {
1513 ns->counters.outdated++;
1514 continue;
1515 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001516
Christopher Faulet67957bd2017-09-27 11:00:59 +02001517 /* number of responses received */
1518 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001519
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1521 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001522
Christopher Faulet67957bd2017-09-27 11:00:59 +02001523 switch (dns_resp) {
1524 case DNS_RESP_VALID:
1525 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001526
Christopher Faulet67957bd2017-09-27 11:00:59 +02001527 case DNS_RESP_INVALID:
1528 case DNS_RESP_QUERY_COUNT_ERROR:
1529 case DNS_RESP_WRONG_NAME:
1530 res->status = RSLV_STATUS_INVALID;
1531 ns->counters.invalid++;
1532 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001533
Christopher Faulet67957bd2017-09-27 11:00:59 +02001534 case DNS_RESP_NX_DOMAIN:
1535 res->status = RSLV_STATUS_NX;
1536 ns->counters.nx++;
1537 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001538
Christopher Faulet67957bd2017-09-27 11:00:59 +02001539 case DNS_RESP_REFUSED:
1540 res->status = RSLV_STATUS_REFUSED;
1541 ns->counters.refused++;
1542 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001543
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 case DNS_RESP_ANCOUNT_ZERO:
1545 res->status = RSLV_STATUS_OTHER;
1546 ns->counters.any_err++;
1547 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001548
Christopher Faulet67957bd2017-09-27 11:00:59 +02001549 case DNS_RESP_CNAME_ERROR:
1550 res->status = RSLV_STATUS_OTHER;
1551 ns->counters.cname_error++;
1552 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001553
Christopher Faulet67957bd2017-09-27 11:00:59 +02001554 case DNS_RESP_TRUNCATED:
1555 res->status = RSLV_STATUS_OTHER;
1556 ns->counters.truncated++;
1557 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001558
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559 case DNS_RESP_NO_EXPECTED_RECORD:
1560 case DNS_RESP_ERROR:
1561 case DNS_RESP_INTERNAL:
1562 res->status = RSLV_STATUS_OTHER;
1563 ns->counters.other++;
1564 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001565 }
1566
Christopher Faulet67957bd2017-09-27 11:00:59 +02001567 /* Wait all nameservers response to handle errors */
1568 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1569 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001570
Christopher Faulet67957bd2017-09-27 11:00:59 +02001571 /* Process error codes */
1572 if (dns_resp != DNS_RESP_VALID) {
1573 if (res->prefered_query_type != res->query_type) {
1574 /* The fallback on the query type was already performed,
1575 * so check the try counter. If it falls to 0, we can
1576 * report an error. Else, wait the next attempt. */
1577 if (!res->try)
1578 goto report_res_error;
1579 }
1580 else {
1581 /* Fallback from A to AAAA or the opposite and re-send
1582 * the resolution immediately. try counter is not
1583 * decremented. */
1584 if (res->prefered_query_type == DNS_RTYPE_A) {
1585 res->query_type = DNS_RTYPE_AAAA;
1586 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001587 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001588 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1589 res->query_type = DNS_RTYPE_A;
1590 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001591 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001592 }
1593 continue;
1594 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001595
Christopher Faulet67957bd2017-09-27 11:00:59 +02001596 /* Now let's check the query's dname corresponds to the one we
1597 * sent. We can check only the first query of the list. We send
1598 * one query at a time so we get one query in the response */
1599 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1600 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1601 dns_resp = DNS_RESP_WRONG_NAME;
1602 ns->counters.other++;
1603 goto report_res_error;
1604 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001605
Christopher Faulet67957bd2017-09-27 11:00:59 +02001606 /* So the resolution succeeded */
1607 res->status = RSLV_STATUS_VALID;
1608 res->last_valid = now_ms;
1609 ns->counters.valid++;
1610 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001611
Christopher Faulet67957bd2017-09-27 11:00:59 +02001612 report_res_error:
1613 list_for_each_entry(req, &res->requesters, list)
1614 req->requester_error_cb(req, dns_resp);
1615 dns_reset_resolution(res);
1616 LIST_DEL(&res->list);
1617 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1618 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001619
Christopher Faulet67957bd2017-09-27 11:00:59 +02001620 report_res_success:
1621 /* Only the 1rst requester s managed by the server, others are
1622 * from the cache */
1623 tmpns = ns;
1624 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001625 struct server *s = objt_server(req->owner);
1626
1627 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001628 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001629 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001630 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001631 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001632 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001633 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001634
Christopher Faulet67957bd2017-09-27 11:00:59 +02001635 dns_reset_resolution(res);
1636 LIST_DEL(&res->list);
1637 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1638 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001639 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001640 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001641 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001642}
William Lallemand69e96442016-11-19 00:58:54 +01001643
Christopher Faulet67957bd2017-09-27 11:00:59 +02001644/* Called when a resolvers network socket is ready to send data */
1645static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001646{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001647 struct dns_resolvers *resolvers;
1648 struct dns_nameserver *ns;
1649 struct dns_resolution *res;
1650 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001651
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001653
Christopher Faulet67957bd2017-09-27 11:00:59 +02001654 /* check if ready for sending */
1655 if (!fd_send_ready(fd))
1656 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001657
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658 /* we don't want/need to be waked up any more for sending */
1659 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001660
Christopher Faulet67957bd2017-09-27 11:00:59 +02001661 /* no need to go further if we can't retrieve the nameserver */
1662 if ((ns = dgram->owner) == NULL)
1663 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001664
Christopher Faulet67957bd2017-09-27 11:00:59 +02001665 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001666 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001667
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1669 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001670
Christopher Faulet67957bd2017-09-27 11:00:59 +02001671 if (res->nb_queries == resolvers->nb_nameservers)
1672 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001673
Christopher Faulet67957bd2017-09-27 11:00:59 +02001674 trash.len = dns_build_query(res->query_id, res->query_type,
1675 resolvers->accepted_payload_size,
1676 res->hostname_dn, res->hostname_dn_len,
1677 trash.str, trash.size);
1678 if (trash.len == -1)
1679 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001680
Christopher Faulet67957bd2017-09-27 11:00:59 +02001681 ret = send(fd, trash.str, trash.len, 0);
1682 if (ret != trash.len)
1683 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001684
Christopher Faulet67957bd2017-09-27 11:00:59 +02001685 ns->counters.sent++;
1686 res->nb_queries++;
1687 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001688
Christopher Faulet67957bd2017-09-27 11:00:59 +02001689 snd_error:
1690 ns->counters.snd_error++;
1691 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001692 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001693 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001694}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001695
Christopher Faulet67957bd2017-09-27 11:00:59 +02001696/* Processes DNS resolution. First, it checks the active list to detect expired
1697 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1698 * checks the wait list to trigger new resolutions.
1699 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001700static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001702 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001703 struct dns_resolution *res, *resback;
1704 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001705
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001706 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001707
Christopher Faulet67957bd2017-09-27 11:00:59 +02001708 /* Handle all expired resolutions from the active list */
1709 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1710 /* When we find the first resolution in the future, then we can
1711 * stop here */
1712 exp = tick_add(res->last_query, resolvers->timeout.retry);
1713 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001714 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001715
Christopher Faulet67957bd2017-09-27 11:00:59 +02001716 /* If current resolution has been tried too many times and
1717 * finishes in timeout we update its status and remove it from
1718 * the list */
1719 if (!res->try) {
1720 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001721
Christopher Faulet67957bd2017-09-27 11:00:59 +02001722 /* Notify the result to the requesters */
1723 if (!res->nb_responses)
1724 res->status = RSLV_STATUS_TIMEOUT;
1725 list_for_each_entry(req, &res->requesters, list)
1726 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001727
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728 /* Clean up resolution info and remove it from the
1729 * current list */
1730 dns_reset_resolution(res);
1731 LIST_DEL(&res->list);
1732 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001733 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001734 else {
1735 /* Otherwise resend the DNS query and requeue the resolution */
1736 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1737 /* No response received (a real timeout) or fallback already done */
1738 res->query_type = res->prefered_query_type;
1739 res->try--;
1740 }
1741 else {
1742 /* Fallback from A to AAAA or the opposite and re-send
1743 * the resolution immediately. try counter is not
1744 * decremented. */
1745 if (res->prefered_query_type == DNS_RTYPE_A)
1746 res->query_type = DNS_RTYPE_AAAA;
1747 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1748 res->query_type = DNS_RTYPE_A;
1749 else
1750 res->try--;
1751 }
1752 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001753 }
1754 }
William Lallemand69e96442016-11-19 00:58:54 +01001755
Christopher Faulet67957bd2017-09-27 11:00:59 +02001756 /* Handle all resolutions in the wait list */
1757 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1758 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1759 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1760 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001761
Christopher Faulet67957bd2017-09-27 11:00:59 +02001762 if (dns_run_resolution(res) != 1) {
1763 res->last_resolution = now_ms;
1764 LIST_DEL(&res->list);
1765 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001766 }
1767 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001768
Christopher Faulet67957bd2017-09-27 11:00:59 +02001769 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001770 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001771 return t;
1772}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001773
Christopher Faulet67957bd2017-09-27 11:00:59 +02001774/* proto_udp callback functions for a DNS resolution */
1775struct dgram_data_cb resolve_dgram_cb = {
1776 .recv = dns_resolve_recv,
1777 .send = dns_resolve_send,
1778};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001779
Christopher Faulet67957bd2017-09-27 11:00:59 +02001780/* Release memory allocated by DNS */
1781static void dns_deinit(void)
1782{
1783 struct dns_resolvers *resolvers, *resolversback;
1784 struct dns_nameserver *ns, *nsback;
1785 struct dns_resolution *res, *resback;
1786 struct dns_requester *req, *reqback;
1787 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001788
Christopher Faulet67957bd2017-09-27 11:00:59 +02001789 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1790 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1791 free(ns->id);
1792 free((char *)ns->conf.file);
1793 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1794 fd_delete(ns->dgram->t.sock.fd);
1795 free(ns->dgram);
1796 LIST_DEL(&ns->list);
1797 free(ns);
1798 }
1799
1800 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1801 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1802 LIST_DEL(&req->list);
1803 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001804 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001805 dns_free_resolution(res);
1806 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001807
Christopher Faulet67957bd2017-09-27 11:00:59 +02001808 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1809 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1810 LIST_DEL(&req->list);
1811 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001812 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001813 dns_free_resolution(res);
1814 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001815
Christopher Faulet67957bd2017-09-27 11:00:59 +02001816 free(resolvers->id);
1817 free((char *)resolvers->conf.file);
1818 task_delete(resolvers->t);
1819 task_free(resolvers->t);
1820 LIST_DEL(&resolvers->list);
1821 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001822 }
1823
Christopher Faulet67957bd2017-09-27 11:00:59 +02001824 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1825 free(srvrq->name);
1826 free(srvrq->hostname_dn);
1827 LIST_DEL(&srvrq->list);
1828 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001829 }
1830
Willy Tarreaubafbe012017-11-24 17:34:44 +01001831 pool_destroy(dns_answer_item_pool);
1832 pool_destroy(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001833}
1834
Christopher Faulet67957bd2017-09-27 11:00:59 +02001835/* Finalizes the DNS configuration by allocating required resources and checking
1836 * live parameters.
1837 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001838 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001839static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001840{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001841 struct dns_resolvers *resolvers;
1842 struct proxy *px;
1843 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001844
Christopher Faulet67957bd2017-09-27 11:00:59 +02001845 /* allocate pool of resolution per resolvers */
1846 list_for_each_entry(resolvers, &dns_resolvers, list) {
1847 struct dns_nameserver *ns;
1848 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001849
Christopher Faulet67957bd2017-09-27 11:00:59 +02001850 /* Check if we can create the socket with nameservers info */
1851 list_for_each_entry(ns, &resolvers->nameservers, list) {
1852 struct dgram_conn *dgram = NULL;
1853 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001854
Christopher Faulet67957bd2017-09-27 11:00:59 +02001855 /* Check nameserver info */
1856 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001857 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1858 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001859 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001860 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001861 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001862 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001863 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1864 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001865 close(fd);
1866 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001867 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001868 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001869 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001870
Christopher Faulet67957bd2017-09-27 11:00:59 +02001871 /* Create dgram structure that will hold the UPD socket
1872 * and attach it on the current nameserver */
1873 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001874 ha_alert("config: resolvers '%s' : out of memory.\n",
1875 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001876 err_code |= (ERR_ALERT|ERR_ABORT);
1877 goto err;
1878 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001879
Christopher Faulet67957bd2017-09-27 11:00:59 +02001880 /* Leave dgram partially initialized, no FD attached for
1881 * now. */
1882 dgram->owner = ns;
1883 dgram->data = &resolve_dgram_cb;
1884 dgram->t.sock.fd = -1;
1885 ns->dgram = dgram;
1886 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001887
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001889 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001890 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001891 err_code |= (ERR_ALERT|ERR_ABORT);
1892 goto err;
1893 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001894
Christopher Faulet67957bd2017-09-27 11:00:59 +02001895 /* Update task's parameters */
1896 t->process = dns_process_resolvers;
1897 t->context = resolvers;
1898 resolvers->t = t;
1899 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001900 }
1901
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001902 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001903 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001904
Christopher Faulet67957bd2017-09-27 11:00:59 +02001905 for (srv = px->srv; srv; srv = srv->next) {
1906 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001907
Christopher Faulet67957bd2017-09-27 11:00:59 +02001908 if (!srv->resolvers_id)
1909 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001910
Christopher Faulet67957bd2017-09-27 11:00:59 +02001911 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001912 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1913 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001914 err_code |= (ERR_ALERT|ERR_ABORT);
1915 continue;
1916 }
1917 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001918
Christopher Faulet67957bd2017-09-27 11:00:59 +02001919 if (srv->srvrq && !srv->srvrq->resolvers) {
1920 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001921 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001922 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1923 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001924 err_code |= (ERR_ALERT|ERR_ABORT);
1925 continue;
1926 }
1927 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001928 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001929 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1930 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001931 err_code |= (ERR_ALERT|ERR_ABORT);
1932 continue;
1933 }
1934 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001935 }
1936
Christopher Faulet67957bd2017-09-27 11:00:59 +02001937 if (err_code & (ERR_ALERT|ERR_ABORT))
1938 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001939
Christopher Faulet67957bd2017-09-27 11:00:59 +02001940 return err_code;
1941 err:
1942 dns_deinit();
1943 return err_code;
1944
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001945}
1946
Christopher Faulet67957bd2017-09-27 11:00:59 +02001947/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001948static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001949{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001950 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001951
Christopher Faulet67957bd2017-09-27 11:00:59 +02001952 if (*args[2]) {
1953 list_for_each_entry(presolvers, &dns_resolvers, list) {
1954 if (strcmp(presolvers->id, args[2]) == 0) {
1955 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001956 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001957 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001958 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001959 if (appctx->ctx.cli.p0 == NULL) {
1960 appctx->ctx.cli.severity = LOG_ERR;
1961 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1962 appctx->st0 = CLI_ST_PRINT;
1963 return 1;
1964 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001965 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001966 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001967}
1968
Christopher Faulet67957bd2017-09-27 11:00:59 +02001969/* Dumps counters from all resolvers section and associated name servers. It
1970 * returns 0 if the output buffer is full and it needs to be called again,
1971 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001972 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001973 */
1974static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1975{
1976 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 struct dns_resolvers *resolvers;
1978 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001979
1980 chunk_reset(&trash);
1981
1982 switch (appctx->st2) {
1983 case STAT_ST_INIT:
1984 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1985 /* fall through */
1986
1987 case STAT_ST_LIST:
1988 if (LIST_ISEMPTY(&dns_resolvers)) {
1989 chunk_appendf(&trash, "No resolvers found\n");
1990 }
1991 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001992 list_for_each_entry(resolvers, &dns_resolvers, list) {
1993 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01001994 continue;
1995
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
1997 list_for_each_entry(ns, &resolvers->nameservers, list) {
1998 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
1999 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2000 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2001 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2002 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2003 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2004 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2005 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2006 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2007 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2008 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2009 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2010 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2011 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2012 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2013 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002014 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002015 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002016 }
2017 }
2018
2019 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002020 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002021 /* let's try again later from this session. We add ourselves into
2022 * this session's users so that it can remove us upon termination.
2023 */
2024 si->flags |= SI_FL_WAIT_ROOM;
2025 return 0;
2026 }
William Lallemand69e96442016-11-19 00:58:54 +01002027 /* fall through */
2028
2029 default:
2030 appctx->st2 = STAT_ST_FIN;
2031 return 1;
2032 }
2033}
2034
2035/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002036static struct cli_kw_list cli_kws = {{ }, {
2037 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2038 " associated name servers",
2039 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2040 {{},}
2041 }
2042};
William Lallemand69e96442016-11-19 00:58:54 +01002043
2044
2045__attribute__((constructor))
2046static void __dns_init(void)
2047{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2049 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2050
Christopher Faulet67957bd2017-09-27 11:00:59 +02002051 hap_register_post_check(dns_finalize_config);
2052 hap_register_post_deinit(dns_deinit);
2053
William Lallemand69e96442016-11-19 00:58:54 +01002054 cli_register_kw(&cli_kws);
2055}