blob: 77bf5c00410027da102009961f60d427f9665633 [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 Assmann8e2d9432018-06-22 15:04:43 +0200965 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200966
Christopher Faulet67957bd2017-09-27 11:00:59 +0200967 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200968 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200969 *newip = newip4 = newip6 = NULL;
970 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200971 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200972 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200973
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100974 /* Select an IP regarding configuration preference.
975 * Top priority is the prefered network ip version,
976 * second priority is the prefered network.
977 * the last priority is the currently used IP,
978 *
979 * For these three priorities, a score is calculated. The
980 * weight are:
Baptiste Assmann741e00a2018-06-22 12:51:51 +0200981 * 8 - prefered ip version.
Baptistefc725902016-12-26 23:21:08 +0100982 * 4 - prefered network.
983 * 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 +0100984 * 1 - current ip.
985 * The result with the biggest score is returned.
986 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200987
988 list_for_each_entry(record, &dns_p->answer_list, list) {
989 void *ip;
990 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100991
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200992 if (record->type == DNS_RTYPE_A) {
993 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
994 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200995 }
996 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200997 ip_type = AF_INET6;
998 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200999 }
1000 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001001 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001002 score = 0;
1003
1004 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001005 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001006 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001007
1008 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001009 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001010
1011 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001012 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001013 continue;
1014
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001015 if ((ip_type == AF_INET &&
1016 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001017 &dns_opts->pref_net[j].mask.in4,
1018 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001019 (ip_type == AF_INET6 &&
1020 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001021 &dns_opts->pref_net[j].mask.in6,
1022 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001023 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001024 break;
1025 }
1026 }
1027
Christopher Faulet67957bd2017-09-27 11:00:59 +02001028 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001029 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001030 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001031 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001032 if (!allowed_duplicated_ip) {
1033 continue;
1034 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001035 } else {
1036 score += 2;
1037 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001038
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001039 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001040 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001041 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001042 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001043 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001044 !memcmp(ip, currentip, 16)))) {
1045 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001046 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001047 }
1048 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001049 currentip_sel = 0;
1050
1051 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001052 * score. The maximum score is 15, if this value is reached, we
1053 * break the parsing. Implicitly, this score is reached the ip
1054 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001055 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001056 if (ip_type == AF_INET)
1057 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001058 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001059 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001060 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001061 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001062 return DNS_UPD_NO;
1063 max_score = score;
1064 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001065 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001066
Christopher Faulet67957bd2017-09-27 11:00:59 +02001067 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001068 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001069 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001070
Christopher Faulet67957bd2017-09-27 11:00:59 +02001071 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001072 if (family_priority == AF_INET) {
1073 if (newip4) {
1074 *newip = newip4;
1075 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001076 }
1077 else if (newip6) {
1078 *newip = newip6;
1079 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001080 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001081 if (!currentip_found)
1082 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001083 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001084 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001085 else if (family_priority == AF_INET6) {
1086 if (newip6) {
1087 *newip = newip6;
1088 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001089 }
1090 else if (newip4) {
1091 *newip = newip4;
1092 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001093 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001094 if (!currentip_found)
1095 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001096 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001097 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001098 else if (family_priority == AF_UNSPEC) {
1099 if (newip6) {
1100 *newip = newip6;
1101 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001102 }
1103 else if (newip4) {
1104 *newip = newip4;
1105 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001106 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001107 if (!currentip_found)
1108 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109 }
1110
Christopher Faulet67957bd2017-09-27 11:00:59 +02001111 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001113
Christopher Faulet67957bd2017-09-27 11:00:59 +02001114 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001115 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001116 /* Move the first record to the end of the list, for internal
1117 * round robin */
1118 LIST_DEL(&record->list);
1119 LIST_ADDQ(&dns_p->answer_list, &record->list);
1120 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001121 }
1122 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001123}
1124
Christopher Faulet67957bd2017-09-27 11:00:59 +02001125/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001126 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001127 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1128 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001129 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001130 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1131 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001132 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001133int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001134{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001135 char *ptr;
1136 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137
Christopher Faulet67957bd2017-09-27 11:00:59 +02001138 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001139 return -1;
1140
Christopher Faulet67957bd2017-09-27 11:00:59 +02001141 ptr = str;
1142 for (i = 0; i < dn_len-1; ++i) {
1143 sz = dn[i];
1144 if (i)
1145 *ptr++ = '.';
1146 memcpy(ptr, dn+i+1, sz);
1147 ptr += sz;
1148 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001149 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001150 *ptr++ = '\0';
1151 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001152}
1153
Christopher Faulet67957bd2017-09-27 11:00:59 +02001154/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1155 *
1156 * <str> must be a null-terminated string. <str_len> must include the
1157 * terminating null byte. <dn> buffer must be allocated and its size must be
1158 * passed in <dn_len>.
1159 *
1160 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1161 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001162 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001163int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001164{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001165 int i, offset;
1166
Christopher Faulet67957bd2017-09-27 11:00:59 +02001167 if (dn_len < str_len + 1)
1168 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001169
Christopher Faulet67957bd2017-09-27 11:00:59 +02001170 /* First byte of dn will be used to store the length of the first
1171 * label */
1172 offset = 0;
1173 for (i = 0; i < str_len; ++i) {
1174 if (str[i] == '.') {
1175 /* 2 or more consecutive dots is invalid */
1176 if (i == offset)
1177 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001178
Christopher Faulet67957bd2017-09-27 11:00:59 +02001179 dn[offset] = (i - offset);
1180 offset = i+1;
1181 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001182 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001183 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001184 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001185 dn[offset] = (i - offset - 1);
1186 dn[i] = '\0';
1187 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001188}
1189
Christopher Faulet67957bd2017-09-27 11:00:59 +02001190/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001191 * - total size
1192 * - each label size individually
1193 * returns:
1194 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1195 * 1 when no error. <err> is left unaffected.
1196 */
1197int dns_hostname_validation(const char *string, char **err)
1198{
1199 const char *c, *d;
1200 int i;
1201
1202 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1203 if (err)
1204 *err = DNS_TOO_LONG_FQDN;
1205 return 0;
1206 }
1207
1208 c = string;
1209 while (*c) {
1210 d = c;
1211
1212 i = 0;
1213 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1214 i++;
1215 if (!((*d == '-') || (*d == '_') ||
1216 ((*d >= 'a') && (*d <= 'z')) ||
1217 ((*d >= 'A') && (*d <= 'Z')) ||
1218 ((*d >= '0') && (*d <= '9')))) {
1219 if (err)
1220 *err = DNS_INVALID_CHARACTER;
1221 return 0;
1222 }
1223 d++;
1224 }
1225
1226 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1227 if (err)
1228 *err = DNS_LABEL_TOO_LONG;
1229 return 0;
1230 }
1231
1232 if (*d == '\0')
1233 goto out;
1234
1235 c = ++d;
1236 }
1237 out:
1238 return 1;
1239}
1240
Christopher Faulet67957bd2017-09-27 11:00:59 +02001241/* Picks up an available resolution from the different resolution list
1242 * associated to a resolvers section, in this order:
1243 * 1. check in resolutions.curr for the same hostname and query_type
1244 * 2. check in resolutions.wait for the same hostname and query_type
1245 * 3. Get a new resolution from resolution pool
1246 *
1247 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001248 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001249static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1250 char **hostname_dn, int hostname_dn_len,
1251 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001252{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001253 struct dns_resolution *res;
1254
1255 if (!*hostname_dn)
1256 goto from_pool;
1257
1258 /* Search for same hostname and query type in resolutions.curr */
1259 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1260 if (!res->hostname_dn)
1261 continue;
1262 if ((query_type == res->prefered_query_type) &&
1263 hostname_dn_len == res->hostname_dn_len &&
1264 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1265 return res;
1266 }
1267
1268 /* Search for same hostname and query type in resolutions.wait */
1269 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1270 if (!res->hostname_dn)
1271 continue;
1272 if ((query_type == res->prefered_query_type) &&
1273 hostname_dn_len == res->hostname_dn_len &&
1274 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1275 return res;
1276 }
1277
1278 from_pool:
1279 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001280 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001281 if (res) {
1282 memset(res, 0, sizeof(*res));
1283 res->resolvers = resolvers;
1284 res->uuid = resolution_uuid;
1285 res->status = RSLV_STATUS_NONE;
1286 res->step = RSLV_STEP_NONE;
1287 res->last_valid = now_ms;
1288
1289 LIST_INIT(&res->requesters);
1290 LIST_INIT(&res->response.answer_list);
1291
1292 res->prefered_query_type = query_type;
1293 res->query_type = query_type;
1294 res->hostname_dn = *hostname_dn;
1295 res->hostname_dn_len = hostname_dn_len;
1296
1297 ++resolution_uuid;
1298
1299 /* Move the resolution to the resolvers wait queue */
1300 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1301 }
1302 return res;
1303}
1304
1305/* Releases a resolution from its requester(s) and move it back to the pool */
1306static void dns_free_resolution(struct dns_resolution *resolution)
1307{
1308 struct dns_requester *req, *reqback;
1309
1310 /* clean up configuration */
1311 dns_reset_resolution(resolution);
1312 resolution->hostname_dn = NULL;
1313 resolution->hostname_dn_len = 0;
1314
1315 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1316 LIST_DEL(&req->list);
1317 req->resolution = NULL;
1318 }
1319
1320 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001321 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001322}
1323
1324/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1325 * on success, -1 otherwise.
1326 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001327int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001328{
1329 struct dns_resolution *res = NULL;
1330 struct dns_requester *req;
1331 struct dns_resolvers *resolvers;
1332 struct server *srv = NULL;
1333 struct dns_srvrq *srvrq = NULL;
1334 char **hostname_dn;
1335 int hostname_dn_len, query_type;
1336
1337 switch (requester_type) {
1338 case OBJ_TYPE_SERVER:
1339 srv = (struct server *)requester;
1340 hostname_dn = &srv->hostname_dn;
1341 hostname_dn_len = srv->hostname_dn_len;
1342 resolvers = srv->resolvers;
1343 query_type = ((srv->dns_opts.family_prio == AF_INET)
1344 ? DNS_RTYPE_A
1345 : DNS_RTYPE_AAAA);
1346 break;
1347
1348 case OBJ_TYPE_SRVRQ:
1349 srvrq = (struct dns_srvrq *)requester;
1350 hostname_dn = &srvrq->hostname_dn;
1351 hostname_dn_len = srvrq->hostname_dn_len;
1352 resolvers = srvrq->resolvers;
1353 query_type = DNS_RTYPE_SRV;
1354 break;
1355
1356 default:
1357 goto err;
1358 }
1359
1360 /* Get a resolution from the resolvers' wait queue or pool */
1361 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1362 goto err;
1363
1364 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001365 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001366 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001367 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001368 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001369 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001370 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001371 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001372 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001373 req->owner = &srv->obj_type;
1374 srv->dns_requester = req;
1375 }
1376 else
1377 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001378 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001379 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001380 }
1381 else if (srvrq) {
1382 if (srvrq->dns_requester == NULL) {
1383 if ((req = calloc(1, sizeof(*req))) == NULL)
1384 goto err;
1385 req->owner = &srvrq->obj_type;
1386 srvrq->dns_requester = req;
1387 }
1388 else
1389 req = srvrq->dns_requester;
1390 }
1391 else
1392 goto err;
1393
1394 req->resolution = res;
1395 req->requester_cb = snr_resolution_cb;
1396 req->requester_error_cb = snr_resolution_error_cb;
1397
1398 LIST_ADDQ(&res->requesters, &req->list);
1399 return 0;
1400
1401 err:
1402 if (res && LIST_ISEMPTY(&res->requesters))
1403 dns_free_resolution(res);
1404 return -1;
1405}
1406
1407/* Removes a requester from a DNS resoltion. It takes takes care of all the
1408 * consequences. It also cleans up some parameters from the requester.
1409 */
1410void dns_unlink_resolution(struct dns_requester *requester)
1411{
1412 struct dns_resolution *res;
1413 struct dns_requester *req;
1414
1415 /* Nothing to do */
1416 if (!requester || !requester->resolution)
1417 return;
1418 res = requester->resolution;
1419
1420 /* Clean up the requester */
1421 LIST_DEL(&requester->list);
1422 requester->resolution = NULL;
1423
1424 /* We need to find another requester linked on this resolution */
1425 if (!LIST_ISEMPTY(&res->requesters))
1426 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1427 else {
1428 dns_free_resolution(res);
1429 return;
1430 }
1431
1432 /* Move hostname_dn related pointers to the next requester */
1433 switch (obj_type(req->owner)) {
1434 case OBJ_TYPE_SERVER:
1435 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1436 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1437 break;
1438 case OBJ_TYPE_SRVRQ:
1439 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1440 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1441 break;
1442 default:
1443 res->hostname_dn = NULL;
1444 res->hostname_dn_len = 0;
1445 break;
1446 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001447}
1448
Christopher Faulet67957bd2017-09-27 11:00:59 +02001449/* Called when a network IO is generated on a name server socket for an incoming
1450 * packet. It performs the following actions:
1451 * - check if the packet requires processing (not outdated resolution)
1452 * - ensure the DNS packet received is valid and call requester's callback
1453 * - call requester's error callback if invalid response
1454 * - check the dn_name in the packet against the one sent
1455 */
1456static void dns_resolve_recv(struct dgram_conn *dgram)
1457{
1458 struct dns_nameserver *ns, *tmpns;
1459 struct dns_resolvers *resolvers;
1460 struct dns_resolution *res;
1461 struct dns_query_item *query;
1462 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1463 unsigned char *bufend;
1464 int fd, buflen, dns_resp;
1465 int max_answer_records;
1466 unsigned short query_id;
1467 struct eb32_node *eb;
1468 struct dns_requester *req;
1469
1470 fd = dgram->t.sock.fd;
1471
1472 /* check if ready for reading */
1473 if (!fd_recv_ready(fd))
1474 return;
1475
1476 /* no need to go further if we can't retrieve the nameserver */
1477 if ((ns = dgram->owner) == NULL)
1478 return;
1479
1480 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001481 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001482
1483 /* process all pending input messages */
1484 while (1) {
1485 /* read message received */
1486 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1487 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1488 /* FIXME : for now we consider EAGAIN only */
1489 fd_cant_recv(fd);
1490 break;
1491 }
1492
1493 /* message too big */
1494 if (buflen > resolvers->accepted_payload_size) {
1495 ns->counters.too_big++;
1496 continue;
1497 }
1498
1499 /* initializing variables */
1500 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1501
1502 /* read the query id from the packet (16 bits) */
1503 if (buf + 2 > bufend) {
1504 ns->counters.invalid++;
1505 continue;
1506 }
1507 query_id = dns_response_get_query_id(buf);
1508
1509 /* search the query_id in the pending resolution tree */
1510 eb = eb32_lookup(&resolvers->query_ids, query_id);
1511 if (eb == NULL) {
1512 /* unknown query id means an outdated response and can be safely ignored */
1513 ns->counters.outdated++;
1514 continue;
1515 }
1516
1517 /* known query id means a resolution in prgress */
1518 res = eb32_entry(eb, struct dns_resolution, qid);
1519 if (!res) {
1520 ns->counters.outdated++;
1521 continue;
1522 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001523
Christopher Faulet67957bd2017-09-27 11:00:59 +02001524 /* number of responses received */
1525 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001526
Christopher Faulet67957bd2017-09-27 11:00:59 +02001527 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1528 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001529
Christopher Faulet67957bd2017-09-27 11:00:59 +02001530 switch (dns_resp) {
1531 case DNS_RESP_VALID:
1532 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001533
Christopher Faulet67957bd2017-09-27 11:00:59 +02001534 case DNS_RESP_INVALID:
1535 case DNS_RESP_QUERY_COUNT_ERROR:
1536 case DNS_RESP_WRONG_NAME:
1537 res->status = RSLV_STATUS_INVALID;
1538 ns->counters.invalid++;
1539 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001540
Christopher Faulet67957bd2017-09-27 11:00:59 +02001541 case DNS_RESP_NX_DOMAIN:
1542 res->status = RSLV_STATUS_NX;
1543 ns->counters.nx++;
1544 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001545
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 case DNS_RESP_REFUSED:
1547 res->status = RSLV_STATUS_REFUSED;
1548 ns->counters.refused++;
1549 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001550
Christopher Faulet67957bd2017-09-27 11:00:59 +02001551 case DNS_RESP_ANCOUNT_ZERO:
1552 res->status = RSLV_STATUS_OTHER;
1553 ns->counters.any_err++;
1554 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001555
Christopher Faulet67957bd2017-09-27 11:00:59 +02001556 case DNS_RESP_CNAME_ERROR:
1557 res->status = RSLV_STATUS_OTHER;
1558 ns->counters.cname_error++;
1559 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001560
Christopher Faulet67957bd2017-09-27 11:00:59 +02001561 case DNS_RESP_TRUNCATED:
1562 res->status = RSLV_STATUS_OTHER;
1563 ns->counters.truncated++;
1564 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001565
Christopher Faulet67957bd2017-09-27 11:00:59 +02001566 case DNS_RESP_NO_EXPECTED_RECORD:
1567 case DNS_RESP_ERROR:
1568 case DNS_RESP_INTERNAL:
1569 res->status = RSLV_STATUS_OTHER;
1570 ns->counters.other++;
1571 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001572 }
1573
Christopher Faulet67957bd2017-09-27 11:00:59 +02001574 /* Wait all nameservers response to handle errors */
1575 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1576 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001577
Christopher Faulet67957bd2017-09-27 11:00:59 +02001578 /* Process error codes */
1579 if (dns_resp != DNS_RESP_VALID) {
1580 if (res->prefered_query_type != res->query_type) {
1581 /* The fallback on the query type was already performed,
1582 * so check the try counter. If it falls to 0, we can
1583 * report an error. Else, wait the next attempt. */
1584 if (!res->try)
1585 goto report_res_error;
1586 }
1587 else {
1588 /* Fallback from A to AAAA or the opposite and re-send
1589 * the resolution immediately. try counter is not
1590 * decremented. */
1591 if (res->prefered_query_type == DNS_RTYPE_A) {
1592 res->query_type = DNS_RTYPE_AAAA;
1593 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001594 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1596 res->query_type = DNS_RTYPE_A;
1597 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001598 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001599 }
1600 continue;
1601 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001602
Christopher Faulet67957bd2017-09-27 11:00:59 +02001603 /* Now let's check the query's dname corresponds to the one we
1604 * sent. We can check only the first query of the list. We send
1605 * one query at a time so we get one query in the response */
1606 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1607 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1608 dns_resp = DNS_RESP_WRONG_NAME;
1609 ns->counters.other++;
1610 goto report_res_error;
1611 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001612
Christopher Faulet67957bd2017-09-27 11:00:59 +02001613 /* So the resolution succeeded */
1614 res->status = RSLV_STATUS_VALID;
1615 res->last_valid = now_ms;
1616 ns->counters.valid++;
1617 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001618
Christopher Faulet67957bd2017-09-27 11:00:59 +02001619 report_res_error:
1620 list_for_each_entry(req, &res->requesters, list)
1621 req->requester_error_cb(req, dns_resp);
1622 dns_reset_resolution(res);
1623 LIST_DEL(&res->list);
1624 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1625 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001626
Christopher Faulet67957bd2017-09-27 11:00:59 +02001627 report_res_success:
1628 /* Only the 1rst requester s managed by the server, others are
1629 * from the cache */
1630 tmpns = ns;
1631 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001632 struct server *s = objt_server(req->owner);
1633
1634 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001635 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001636 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001637 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001638 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001639 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001640 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001641
Christopher Faulet67957bd2017-09-27 11:00:59 +02001642 dns_reset_resolution(res);
1643 LIST_DEL(&res->list);
1644 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1645 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001646 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001647 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001648 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001649}
William Lallemand69e96442016-11-19 00:58:54 +01001650
Christopher Faulet67957bd2017-09-27 11:00:59 +02001651/* Called when a resolvers network socket is ready to send data */
1652static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001653{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001654 struct dns_resolvers *resolvers;
1655 struct dns_nameserver *ns;
1656 struct dns_resolution *res;
1657 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001658
Christopher Faulet67957bd2017-09-27 11:00:59 +02001659 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001660
Christopher Faulet67957bd2017-09-27 11:00:59 +02001661 /* check if ready for sending */
1662 if (!fd_send_ready(fd))
1663 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001664
Christopher Faulet67957bd2017-09-27 11:00:59 +02001665 /* we don't want/need to be waked up any more for sending */
1666 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001667
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 /* no need to go further if we can't retrieve the nameserver */
1669 if ((ns = dgram->owner) == NULL)
1670 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001671
Christopher Faulet67957bd2017-09-27 11:00:59 +02001672 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001673 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001674
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1676 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001677
Christopher Faulet67957bd2017-09-27 11:00:59 +02001678 if (res->nb_queries == resolvers->nb_nameservers)
1679 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001680
Christopher Faulet67957bd2017-09-27 11:00:59 +02001681 trash.len = dns_build_query(res->query_id, res->query_type,
1682 resolvers->accepted_payload_size,
1683 res->hostname_dn, res->hostname_dn_len,
1684 trash.str, trash.size);
1685 if (trash.len == -1)
1686 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001687
Christopher Faulet67957bd2017-09-27 11:00:59 +02001688 ret = send(fd, trash.str, trash.len, 0);
1689 if (ret != trash.len)
1690 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001691
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 ns->counters.sent++;
1693 res->nb_queries++;
1694 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001695
Christopher Faulet67957bd2017-09-27 11:00:59 +02001696 snd_error:
1697 ns->counters.snd_error++;
1698 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001699 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001700 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001702
Christopher Faulet67957bd2017-09-27 11:00:59 +02001703/* Processes DNS resolution. First, it checks the active list to detect expired
1704 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1705 * checks the wait list to trigger new resolutions.
1706 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001707static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001708{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001709 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001710 struct dns_resolution *res, *resback;
1711 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001712
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001713 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001714
Christopher Faulet67957bd2017-09-27 11:00:59 +02001715 /* Handle all expired resolutions from the active list */
1716 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1717 /* When we find the first resolution in the future, then we can
1718 * stop here */
1719 exp = tick_add(res->last_query, resolvers->timeout.retry);
1720 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001721 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001722
Christopher Faulet67957bd2017-09-27 11:00:59 +02001723 /* If current resolution has been tried too many times and
1724 * finishes in timeout we update its status and remove it from
1725 * the list */
1726 if (!res->try) {
1727 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001728
Christopher Faulet67957bd2017-09-27 11:00:59 +02001729 /* Notify the result to the requesters */
1730 if (!res->nb_responses)
1731 res->status = RSLV_STATUS_TIMEOUT;
1732 list_for_each_entry(req, &res->requesters, list)
1733 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001734
Christopher Faulet67957bd2017-09-27 11:00:59 +02001735 /* Clean up resolution info and remove it from the
1736 * current list */
1737 dns_reset_resolution(res);
1738 LIST_DEL(&res->list);
1739 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001740 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001741 else {
1742 /* Otherwise resend the DNS query and requeue the resolution */
1743 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1744 /* No response received (a real timeout) or fallback already done */
1745 res->query_type = res->prefered_query_type;
1746 res->try--;
1747 }
1748 else {
1749 /* Fallback from A to AAAA or the opposite and re-send
1750 * the resolution immediately. try counter is not
1751 * decremented. */
1752 if (res->prefered_query_type == DNS_RTYPE_A)
1753 res->query_type = DNS_RTYPE_AAAA;
1754 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1755 res->query_type = DNS_RTYPE_A;
1756 else
1757 res->try--;
1758 }
1759 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001760 }
1761 }
William Lallemand69e96442016-11-19 00:58:54 +01001762
Christopher Faulet67957bd2017-09-27 11:00:59 +02001763 /* Handle all resolutions in the wait list */
1764 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1765 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1766 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1767 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001768
Christopher Faulet67957bd2017-09-27 11:00:59 +02001769 if (dns_run_resolution(res) != 1) {
1770 res->last_resolution = now_ms;
1771 LIST_DEL(&res->list);
1772 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001773 }
1774 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001775
Christopher Faulet67957bd2017-09-27 11:00:59 +02001776 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001777 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001778 return t;
1779}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001780
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781/* proto_udp callback functions for a DNS resolution */
1782struct dgram_data_cb resolve_dgram_cb = {
1783 .recv = dns_resolve_recv,
1784 .send = dns_resolve_send,
1785};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001786
Christopher Faulet67957bd2017-09-27 11:00:59 +02001787/* Release memory allocated by DNS */
1788static void dns_deinit(void)
1789{
1790 struct dns_resolvers *resolvers, *resolversback;
1791 struct dns_nameserver *ns, *nsback;
1792 struct dns_resolution *res, *resback;
1793 struct dns_requester *req, *reqback;
1794 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001795
Christopher Faulet67957bd2017-09-27 11:00:59 +02001796 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1797 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1798 free(ns->id);
1799 free((char *)ns->conf.file);
1800 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1801 fd_delete(ns->dgram->t.sock.fd);
1802 free(ns->dgram);
1803 LIST_DEL(&ns->list);
1804 free(ns);
1805 }
1806
1807 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1808 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1809 LIST_DEL(&req->list);
1810 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001811 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001812 dns_free_resolution(res);
1813 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001814
Christopher Faulet67957bd2017-09-27 11:00:59 +02001815 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1816 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1817 LIST_DEL(&req->list);
1818 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001819 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001820 dns_free_resolution(res);
1821 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001822
Christopher Faulet67957bd2017-09-27 11:00:59 +02001823 free(resolvers->id);
1824 free((char *)resolvers->conf.file);
1825 task_delete(resolvers->t);
1826 task_free(resolvers->t);
1827 LIST_DEL(&resolvers->list);
1828 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001829 }
1830
Christopher Faulet67957bd2017-09-27 11:00:59 +02001831 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1832 free(srvrq->name);
1833 free(srvrq->hostname_dn);
1834 LIST_DEL(&srvrq->list);
1835 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001836 }
1837
Willy Tarreaubafbe012017-11-24 17:34:44 +01001838 pool_destroy(dns_answer_item_pool);
1839 pool_destroy(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001840}
1841
Christopher Faulet67957bd2017-09-27 11:00:59 +02001842/* Finalizes the DNS configuration by allocating required resources and checking
1843 * live parameters.
1844 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001845 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001846static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001847{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001848 struct dns_resolvers *resolvers;
1849 struct proxy *px;
1850 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001851
Christopher Faulet67957bd2017-09-27 11:00:59 +02001852 /* allocate pool of resolution per resolvers */
1853 list_for_each_entry(resolvers, &dns_resolvers, list) {
1854 struct dns_nameserver *ns;
1855 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001856
Christopher Faulet67957bd2017-09-27 11:00:59 +02001857 /* Check if we can create the socket with nameservers info */
1858 list_for_each_entry(ns, &resolvers->nameservers, list) {
1859 struct dgram_conn *dgram = NULL;
1860 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001861
Christopher Faulet67957bd2017-09-27 11:00:59 +02001862 /* Check nameserver info */
1863 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001864 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1865 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001866 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 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001870 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1871 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001872 close(fd);
1873 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001874 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001875 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001876 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001877
Christopher Faulet67957bd2017-09-27 11:00:59 +02001878 /* Create dgram structure that will hold the UPD socket
1879 * and attach it on the current nameserver */
1880 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001881 ha_alert("config: resolvers '%s' : out of memory.\n",
1882 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001883 err_code |= (ERR_ALERT|ERR_ABORT);
1884 goto err;
1885 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001886
Christopher Faulet67957bd2017-09-27 11:00:59 +02001887 /* Leave dgram partially initialized, no FD attached for
1888 * now. */
1889 dgram->owner = ns;
1890 dgram->data = &resolve_dgram_cb;
1891 dgram->t.sock.fd = -1;
1892 ns->dgram = dgram;
1893 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001894
Christopher Faulet67957bd2017-09-27 11:00:59 +02001895 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001896 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001897 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 err_code |= (ERR_ALERT|ERR_ABORT);
1899 goto err;
1900 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001901
Christopher Faulet67957bd2017-09-27 11:00:59 +02001902 /* Update task's parameters */
1903 t->process = dns_process_resolvers;
1904 t->context = resolvers;
1905 resolvers->t = t;
1906 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001907 }
1908
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001909 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001910 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001911
Christopher Faulet67957bd2017-09-27 11:00:59 +02001912 for (srv = px->srv; srv; srv = srv->next) {
1913 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001914
Christopher Faulet67957bd2017-09-27 11:00:59 +02001915 if (!srv->resolvers_id)
1916 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001917
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001919 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1920 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001921 err_code |= (ERR_ALERT|ERR_ABORT);
1922 continue;
1923 }
1924 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001925
Christopher Faulet67957bd2017-09-27 11:00:59 +02001926 if (srv->srvrq && !srv->srvrq->resolvers) {
1927 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001928 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 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 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001935 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001936 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1937 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001938 err_code |= (ERR_ALERT|ERR_ABORT);
1939 continue;
1940 }
1941 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001942 }
1943
Christopher Faulet67957bd2017-09-27 11:00:59 +02001944 if (err_code & (ERR_ALERT|ERR_ABORT))
1945 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001946
Christopher Faulet67957bd2017-09-27 11:00:59 +02001947 return err_code;
1948 err:
1949 dns_deinit();
1950 return err_code;
1951
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001952}
1953
Christopher Faulet67957bd2017-09-27 11:00:59 +02001954/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001955static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001956{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001957 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001958
Christopher Faulet67957bd2017-09-27 11:00:59 +02001959 if (*args[2]) {
1960 list_for_each_entry(presolvers, &dns_resolvers, list) {
1961 if (strcmp(presolvers->id, args[2]) == 0) {
1962 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001963 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001964 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001965 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001966 if (appctx->ctx.cli.p0 == NULL) {
1967 appctx->ctx.cli.severity = LOG_ERR;
1968 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1969 appctx->st0 = CLI_ST_PRINT;
1970 return 1;
1971 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001972 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001973 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001974}
1975
Christopher Faulet67957bd2017-09-27 11:00:59 +02001976/* Dumps counters from all resolvers section and associated name servers. It
1977 * returns 0 if the output buffer is full and it needs to be called again,
1978 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001979 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001980 */
1981static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1982{
1983 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001984 struct dns_resolvers *resolvers;
1985 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001986
1987 chunk_reset(&trash);
1988
1989 switch (appctx->st2) {
1990 case STAT_ST_INIT:
1991 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1992 /* fall through */
1993
1994 case STAT_ST_LIST:
1995 if (LIST_ISEMPTY(&dns_resolvers)) {
1996 chunk_appendf(&trash, "No resolvers found\n");
1997 }
1998 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 list_for_each_entry(resolvers, &dns_resolvers, list) {
2000 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002001 continue;
2002
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2004 list_for_each_entry(ns, &resolvers->nameservers, list) {
2005 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2006 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2007 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2008 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2009 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2010 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2011 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2012 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2013 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2014 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2015 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2016 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2017 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2018 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2019 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2020 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002021 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002022 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002023 }
2024 }
2025
2026 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002027 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002028 /* let's try again later from this session. We add ourselves into
2029 * this session's users so that it can remove us upon termination.
2030 */
2031 si->flags |= SI_FL_WAIT_ROOM;
2032 return 0;
2033 }
William Lallemand69e96442016-11-19 00:58:54 +01002034 /* fall through */
2035
2036 default:
2037 appctx->st2 = STAT_ST_FIN;
2038 return 1;
2039 }
2040}
2041
2042/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002043static struct cli_kw_list cli_kws = {{ }, {
2044 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2045 " associated name servers",
2046 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2047 {{},}
2048 }
2049};
William Lallemand69e96442016-11-19 00:58:54 +01002050
2051
2052__attribute__((constructor))
2053static void __dns_init(void)
2054{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002055 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2056 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2057
Christopher Faulet67957bd2017-09-27 11:00:59 +02002058 hap_register_post_check(dns_finalize_config);
2059 hap_register_post_deinit(dns_deinit);
2060
William Lallemand69e96442016-11-19 00:58:54 +01002061 cli_register_kw(&cli_kws);
2062}