blob: 985ed0f3379bae28c5e6ffa7a835de042caac665 [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);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020090 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
91 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +020092 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010093 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
94 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +020095 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020096 }
97
Christopher Faulet67957bd2017-09-27 11:00:59 +020098 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010099 ha_alert("config : %s '%s', server '%s': out of memory\n",
100 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200101 goto err;
102 }
103 srvrq->obj_type = OBJ_TYPE_SRVRQ;
104 srvrq->proxy = px;
105 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200106 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200107 srvrq->hostname_dn_len = hostname_dn_len;
108 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100109 ha_alert("config : %s '%s', server '%s': out of memory\n",
110 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200111 goto err;
112 }
113 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
114 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200115
Christopher Faulet67957bd2017-09-27 11:00:59 +0200116 err:
117 if (srvrq) {
118 free(srvrq->name);
119 free(srvrq->hostname_dn);
120 free(srvrq);
121 }
122 return NULL;
123}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200124
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200125
Christopher Faulet67957bd2017-09-27 11:00:59 +0200126/* 2 bytes random generator to generate DNS query ID */
127static inline uint16_t dns_rnd16(void)
128{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200129 if (!dns_query_id_seed)
130 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200131 dns_query_id_seed ^= dns_query_id_seed << 13;
132 dns_query_id_seed ^= dns_query_id_seed >> 7;
133 dns_query_id_seed ^= dns_query_id_seed << 17;
134 return dns_query_id_seed;
135}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200137
Christopher Faulet67957bd2017-09-27 11:00:59 +0200138static inline int dns_resolution_timeout(struct dns_resolution *res)
139{
140 switch (res->status) {
141 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
142 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200143 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200144}
145
Christopher Faulet67957bd2017-09-27 11:00:59 +0200146/* Updates a resolvers' task timeout for next wake up and queue it */
147static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200148{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200149 struct dns_resolution *res;
150 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200151
Christopher Faulet67957bd2017-09-27 11:00:59 +0200152 next = tick_add(now_ms, resolvers->timeout.resolve);
153 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
154 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
155 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
156 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200157
Christopher Faulet67957bd2017-09-27 11:00:59 +0200158 list_for_each_entry(res, &resolvers->resolutions.wait, list)
159 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200160
Christopher Faulet67957bd2017-09-27 11:00:59 +0200161 resolvers->t->expire = next;
162 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200163}
164
Christopher Faulet67957bd2017-09-27 11:00:59 +0200165/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
166 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200167 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200168static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200169{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200170 struct dgram_conn *dgram = ns->dgram;
171 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200172
Christopher Faulet67957bd2017-09-27 11:00:59 +0200173 /* Already connected */
174 if (dgram->t.sock.fd != -1)
175 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200176
Christopher Faulet67957bd2017-09-27 11:00:59 +0200177 /* Create an UDP socket and connect it on the nameserver's IP/Port */
178 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
179 send_log(NULL, LOG_WARNING,
180 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
181 ns->resolvers->id, ns->id);
182 return -1;
183 }
184 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
185 send_log(NULL, LOG_WARNING,
186 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
187 ns->resolvers->id, ns->id);
188 close(fd);
189 return -1;
190 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200191
Christopher Faulet67957bd2017-09-27 11:00:59 +0200192 /* Make the socket non blocking */
193 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200194
Christopher Faulet67957bd2017-09-27 11:00:59 +0200195 /* Add the fd in the fd list and update its parameters */
196 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100197 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200198 fd_want_recv(fd);
199 return 0;
200}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200201
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202/* Forges a DNS query. It needs the following information from the caller:
203 * - <query_id> : the DNS query id corresponding to this query
204 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
205 * - <hostname_dn> : hostname in domain name format
206 * - <hostname_dn_len> : length of <hostname_dn>
207 *
208 * To store the query, the caller must pass a buffer <buf> and its size
209 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
210 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200211 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200212static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
213 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200214{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200215 struct dns_header dns_hdr;
216 struct dns_question qinfo;
217 struct dns_additional_record edns;
218 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200219
Christopher Faulet67957bd2017-09-27 11:00:59 +0200220 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
221 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200222
Christopher Faulet67957bd2017-09-27 11:00:59 +0200223 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200224
Christopher Faulet67957bd2017-09-27 11:00:59 +0200225 /* Set dns query headers */
226 dns_hdr.id = (unsigned short) htons(query_id);
227 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
228 dns_hdr.qdcount = htons(1); /* 1 question */
229 dns_hdr.ancount = 0;
230 dns_hdr.nscount = 0;
231 dns_hdr.arcount = htons(1);
232 memcpy(p, &dns_hdr, sizeof(dns_hdr));
233 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200234
Christopher Faulet67957bd2017-09-27 11:00:59 +0200235 /* Set up query hostname */
236 memcpy(p, hostname_dn, hostname_dn_len);
237 p += hostname_dn_len;
238 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200239
Christopher Faulet67957bd2017-09-27 11:00:59 +0200240 /* Set up query info (type and class) */
241 qinfo.qtype = htons(query_type);
242 qinfo.qclass = htons(DNS_RCLASS_IN);
243 memcpy(p, &qinfo, sizeof(qinfo));
244 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200245
Christopher Faulet67957bd2017-09-27 11:00:59 +0200246 /* Set the DNS extension */
247 edns.name = 0;
248 edns.type = htons(DNS_RTYPE_OPT);
249 edns.udp_payload_size = htons(accepted_payload_size);
250 edns.extension = 0;
251 edns.data_length = 0;
252 memcpy(p, &edns, sizeof(edns));
253 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200254
Christopher Faulet67957bd2017-09-27 11:00:59 +0200255 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200256}
257
Christopher Faulet67957bd2017-09-27 11:00:59 +0200258/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
259 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200260 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200261static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200262{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200263 struct dns_resolvers *resolvers = resolution->resolvers;
264 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200265
Christopher Faulet67957bd2017-09-27 11:00:59 +0200266 list_for_each_entry(ns, &resolvers->nameservers, list) {
267 int fd = ns->dgram->t.sock.fd;
268 if (fd == -1) {
269 if (dns_connect_namesaver(ns) == -1)
270 continue;
271 fd = ns->dgram->t.sock.fd;
272 resolvers->nb_nameservers++;
273 }
274 fd_want_send(fd);
275 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200276
Christopher Faulet67957bd2017-09-27 11:00:59 +0200277 /* Update resolution */
278 resolution->nb_queries = 0;
279 resolution->nb_responses = 0;
280 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200281
Christopher Faulet67957bd2017-09-27 11:00:59 +0200282 /* Push the resolution at the end of the active list */
283 LIST_DEL(&resolution->list);
284 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
285 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200286}
287
Christopher Faulet67957bd2017-09-27 11:00:59 +0200288/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
289 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200290 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200291static int
292dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200293{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200294 struct dns_resolvers *resolvers = resolution->resolvers;
295 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200296
Christopher Faulet67957bd2017-09-27 11:00:59 +0200297 /* Avoid sending requests for resolutions that don't yet have an
298 * hostname, ie resolutions linked to servers that do not yet have an
299 * fqdn */
300 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200301 return 0;
302
Christopher Faulet67957bd2017-09-27 11:00:59 +0200303 /* Check if a resolution has already been started for this server return
304 * directly to avoid resolution pill up. */
305 if (resolution->step != RSLV_STEP_NONE)
306 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200307
Christopher Faulet67957bd2017-09-27 11:00:59 +0200308 /* Generates a new query id. We try at most 100 times to find a free
309 * query id */
310 for (i = 0; i < 100; ++i) {
311 query_id = dns_rnd16();
312 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200313 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200314 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200315 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200316 if (query_id == -1) {
317 send_log(NULL, LOG_NOTICE,
318 "could not generate a query id for %s, in resolvers %s.\n",
319 resolution->hostname_dn, resolvers->id);
320 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200321 }
322
Christopher Faulet67957bd2017-09-27 11:00:59 +0200323 /* Update resolution parameters */
324 resolution->query_id = query_id;
325 resolution->qid.key = query_id;
326 resolution->step = RSLV_STEP_RUNNING;
327 resolution->query_type = resolution->prefered_query_type;
328 resolution->try = resolvers->resolve_retries;
329 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200330
Christopher Faulet67957bd2017-09-27 11:00:59 +0200331 /* Send the DNS query */
332 resolution->try -= 1;
333 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200334 return 1;
335}
336
Christopher Faulet67957bd2017-09-27 11:00:59 +0200337/* Performs a name resolution for the requester <req> */
338void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200339{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200340 struct dns_resolvers *resolvers;
341 struct dns_resolution *res;
342 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200343
Christopher Faulet67957bd2017-09-27 11:00:59 +0200344 if (!req || !req->resolution)
345 return;
346 res = req->resolution;
347 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200348
Christopher Faulet67957bd2017-09-27 11:00:59 +0200349 /* The resolution must not be triggered yet. Use the cached response, if
350 * valid */
351 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200352 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
353 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
354 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200355}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200356
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200357
Christopher Faulet67957bd2017-09-27 11:00:59 +0200358/* Resets some resolution parameters to initial values and also delete the query
359 * ID from the resolver's tree.
360 */
361static void dns_reset_resolution(struct dns_resolution *resolution)
362{
363 /* update resolution status */
364 resolution->step = RSLV_STEP_NONE;
365 resolution->try = 0;
366 resolution->last_resolution = now_ms;
367 resolution->nb_queries = 0;
368 resolution->nb_responses = 0;
369 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200370
Christopher Faulet67957bd2017-09-27 11:00:59 +0200371 /* clean up query id */
372 eb32_delete(&resolution->qid);
373 resolution->query_id = 0;
374 resolution->qid.key = 0;
375}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200376
Christopher Faulet67957bd2017-09-27 11:00:59 +0200377/* Returns the query id contained in a DNS response */
378static inline unsigned short dns_response_get_query_id(unsigned char *resp)
379{
380 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200381}
382
Christopher Faulet67957bd2017-09-27 11:00:59 +0200383
384/* Analyses, re-builds and copies the name <name> from the DNS response packet
385 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
386 * for compressed data. The result is copied into <dest>, ensuring we don't
387 * overflow using <dest_len> Returns the number of bytes the caller can move
388 * forward. If 0 it means an error occurred while parsing the name. <offset> is
389 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200390 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200391int dns_read_name(unsigned char *buffer, unsigned char *bufend,
392 unsigned char *name, char *destination, int dest_len,
393 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200394{
395 int nb_bytes = 0, n = 0;
396 int label_len;
397 unsigned char *reader = name;
398 char *dest = destination;
399
400 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200401 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200402 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200403 /* Must point BEFORE current position */
404 if ((buffer + reader[1]) > reader)
405 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200406
Christopher Faulet67957bd2017-09-27 11:00:59 +0200407 n = dns_read_name(buffer, bufend, buffer + reader[1],
408 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200409 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200410 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200411
Christopher Faulet67957bd2017-09-27 11:00:59 +0200412 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200413 nb_bytes += n;
414 goto out;
415 }
416
417 label_len = *reader;
418 if (label_len == 0)
419 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200420
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200421 /* Check if:
422 * - we won't read outside the buffer
423 * - there is enough place in the destination
424 */
425 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200426 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200427
428 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200429 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200430
431 memcpy(dest, reader, label_len);
432
Christopher Faulet67957bd2017-09-27 11:00:59 +0200433 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200434 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200435 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200436 }
437
Christopher Faulet67957bd2017-09-27 11:00:59 +0200438 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200439 /* offset computation:
440 * parse from <name> until finding either NULL or a pointer "c0xx"
441 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200442 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200443 *offset = 0;
444 while (reader < bufend) {
445 if ((reader[0] & 0xc0) == 0xc0) {
446 *offset += 2;
447 break;
448 }
449 else if (*reader == 0) {
450 *offset += 1;
451 break;
452 }
453 *offset += 1;
454 ++reader;
455 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200456 return nb_bytes;
457
Christopher Faulet67957bd2017-09-27 11:00:59 +0200458 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200459 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200460}
461
Christopher Faulet67957bd2017-09-27 11:00:59 +0200462/* Checks for any obsolete record, also identify any SRV request, and try to
463 * find a corresponding server.
464*/
465static void dns_check_dns_response(struct dns_resolution *res)
466{
467 struct dns_resolvers *resolvers = res->resolvers;
468 struct dns_requester *req, *reqback;
469 struct dns_answer_item *item, *itemback;
470 struct server *srv;
471 struct dns_srvrq *srvrq;
472
473 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
474
475 /* Remove obsolete items */
476 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
477 if (item->type != DNS_RTYPE_SRV)
478 goto rm_obselete_item;
479
480 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
481 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
482 continue;
483
484 /* Remove any associated server */
485 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100486 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200487 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
488 item->data_len == srv->hostname_dn_len &&
489 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
490 snr_update_srv_status(srv, 1);
491 free(srv->hostname);
492 free(srv->hostname_dn);
493 srv->hostname = NULL;
494 srv->hostname_dn = NULL;
495 srv->hostname_dn_len = 0;
496 dns_unlink_resolution(srv->dns_requester);
497 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100498 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200499 }
500 }
501
502 rm_obselete_item:
503 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100504 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200505 continue;
506 }
507
508 if (item->type != DNS_RTYPE_SRV)
509 continue;
510
511 /* Now process SRV records */
512 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
513 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
514 continue;
515
516 /* Check if a server already uses that hostname */
517 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100518 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200519 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
520 item->data_len == srv->hostname_dn_len &&
521 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100522 int ha_weight;
523
524 /* Make sure weight is at least 1, so
525 * that the server will be used.
526 */
527 ha_weight = item->weight / 256 + 1;
528 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200529 char weight[9];
530
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100531 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200532 server_parse_weight_change_request(srv, weight);
533 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100534 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200535 break;
536 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100537 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200538 }
539 if (srv)
540 continue;
541
542 /* If not, try to find a server with undefined hostname */
543 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100544 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200545 if (srv->srvrq == srvrq && !srv->hostname_dn)
546 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200548 }
549 /* And update this server, if found */
550 if (srv) {
551 const char *msg = NULL;
552 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100553 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200554 char hostname[DNS_MAX_NAME_SIZE];
555
556 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100557 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100558 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200559 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100560 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100561 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200562 if (msg)
563 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
564
565 srv->svc_port = item->port;
566 srv->flags &= ~SRV_F_MAPPORTS;
567 if ((srv->check.state & CHK_ST_CONFIGURED) &&
568 !(srv->flags & SRV_F_CHECKPORT))
569 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100570
571 /* Make sure weight is at least 1, so
572 * that the server will be used.
573 */
574 ha_weight = item->weight / 256 + 1;
575
576 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200577 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100578 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200579 }
580 }
581 }
582}
583
584/* Validates that the buffer DNS response provided in <resp> and finishing
585 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200586 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200587 * The result is stored in <resolution>' response, buf_response,
588 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200589 *
590 * This function returns one of the DNS_RESP_* code to indicate the type of
591 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200592 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200593static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
594 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200595{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200596 unsigned char *reader;
597 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200598 int len, flags, offset;
599 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200600 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200601 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200602 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200603 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200604 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200605
Christopher Faulet67957bd2017-09-27 11:00:59 +0200606 reader = resp;
607 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200608 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200609 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200610
Christopher Faulet67957bd2017-09-27 11:00:59 +0200611 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200612 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200613
614 /* query id */
615 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200616 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200617 dns_p->header.id = reader[0] * 256 + reader[1];
618 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200619
Christopher Faulet67957bd2017-09-27 11:00:59 +0200620 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200621 * First byte contains:
622 * - response flag (1 bit)
623 * - opcode (4 bits)
624 * - authoritative (1 bit)
625 * - truncated (1 bit)
626 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200627 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200628 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200629 return DNS_RESP_INVALID;
630
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200631 flags = reader[0] * 256 + reader[1];
632
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200633 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
634 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200635 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200636 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200637 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200638 return DNS_RESP_ERROR;
639 }
640
Christopher Faulet67957bd2017-09-27 11:00:59 +0200641 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200642 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200643
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200644 /* 2 bytes for question count */
645 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200646 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200647 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200648 /* (for now) we send one query only, so we expect only one in the
649 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200650 if (dns_p->header.qdcount != 1)
651 return DNS_RESP_QUERY_COUNT_ERROR;
652 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200653 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200654 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200655
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200656 /* 2 bytes for answer count */
657 if (reader + 2 >= bufend)
658 return DNS_RESP_INVALID;
659 dns_p->header.ancount = reader[0] * 256 + reader[1];
660 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200661 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200662 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200663 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200664 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200665 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200666
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200667 /* 2 bytes authority count */
668 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200669 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200670 dns_p->header.nscount = reader[0] * 256 + reader[1];
671 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200672
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200673 /* 2 bytes additional count */
674 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200675 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200676 dns_p->header.arcount = reader[0] * 256 + reader[1];
677 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200678
Christopher Faulet67957bd2017-09-27 11:00:59 +0200679 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200680 LIST_INIT(&dns_p->query_list);
681 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 +0200682 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200683 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200684 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200685 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
686 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200687 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200688 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200689
Christopher Faulet67957bd2017-09-27 11:00:59 +0200690 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200691 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200692 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200693 offset = 0;
694 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200695
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696 if (len == 0)
697 return DNS_RESP_INVALID;
698
699 reader += offset;
700 previous_dname = dns_query->name;
701
702 /* move forward 2 bytes for question type */
703 if (reader + 2 >= bufend)
704 return DNS_RESP_INVALID;
705 dns_query->type = reader[0] * 256 + reader[1];
706 reader += 2;
707
708 /* move forward 2 bytes for question class */
709 if (reader + 2 >= bufend)
710 return DNS_RESP_INVALID;
711 dns_query->class = reader[0] * 256 + reader[1];
712 reader += 2;
713 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200714
Baptiste Assmann251abb92017-08-11 09:58:27 +0200715 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200716 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200717 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
718 return DNS_RESP_TRUNCATED;
719
Baptiste Assmann325137d2015-04-13 23:40:55 +0200720 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200721 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200722 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200723 if (reader >= bufend)
724 return DNS_RESP_INVALID;
725
Willy Tarreaubafbe012017-11-24 17:34:44 +0100726 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200727 if (dns_answer_record == NULL)
728 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200729
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200730 offset = 0;
731 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200732
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200733 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100734 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200735 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200736 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200737
Christopher Faulet67957bd2017-09-27 11:00:59 +0200738 /* Check if the current record dname is valid. previous_dname
739 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200740 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100741 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200742 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200743 /* First record, means a mismatch issue between
744 * queried dname and dname found in the first
745 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200746 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200747 }
748 else {
749 /* If not the first record, this means we have a
750 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200751 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752 }
753
Baptiste Assmann325137d2015-04-13 23:40:55 +0200754 }
755
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200756 memcpy(dns_answer_record->name, tmpname, len);
757 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200758
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200759 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200760 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100761 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200762 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200763 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200764
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200765 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200766 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100767 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200768 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200769 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200770 dns_answer_record->type = reader[0] * 256 + reader[1];
771 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200772
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200773 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200774 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100775 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200776 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200777 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200778 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200779 reader += 2;
780
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200781 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200782 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100783 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200784 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200785 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200786 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
787 + reader[2] * 256 + reader[3];
788 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200789
Christopher Faulet67957bd2017-09-27 11:00:59 +0200790 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200791 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100792 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200793 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200794 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200795 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200796
Christopher Faulet67957bd2017-09-27 11:00:59 +0200797 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200798 reader += 2;
799
Christopher Faulet67957bd2017-09-27 11:00:59 +0200800 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200801 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200802 case DNS_RTYPE_A:
803 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200804 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100805 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200806 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200807 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200808 dns_answer_record->address.sa_family = AF_INET;
809 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
810 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200811 break;
812
813 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200814 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200815 * no IP could be found and last record was a CNAME. Could be triggered
816 * by a wrong query type
817 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200818 * + 1 because dns_answer_record_id starts at 0
819 * while number of answers is an integer and
820 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200821 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200822 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100823 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200824 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200825 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200826
827 offset = 0;
828 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200829 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100830 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200831 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200832 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200833
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200834 memcpy(dns_answer_record->target, tmpname, len);
835 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200836 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200837 break;
838
Olivier Houchard8da5f982017-08-04 18:35:36 +0200839
840 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200841 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200842 * - 2 bytes for the priority
843 * - 2 bytes for the weight
844 * - 2 bytes for the port
845 * - the target hostname
846 */
847 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100848 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200849 return DNS_RESP_INVALID;
850 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200851 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200852 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200853 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200854 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200855 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200856 reader += sizeof(uint16_t);
857 offset = 0;
858 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
859 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100860 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200861 return DNS_RESP_INVALID;
862 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200863 dns_answer_record->data_len = len;
864 memcpy(dns_answer_record->target, tmpname, len);
865 dns_answer_record->target[len] = 0;
866 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200867
Baptiste Assmann325137d2015-04-13 23:40:55 +0200868 case DNS_RTYPE_AAAA:
869 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200870 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100871 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200872 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200873 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200874 dns_answer_record->address.sa_family = AF_INET6;
875 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
876 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200877 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200878
Baptiste Assmann325137d2015-04-13 23:40:55 +0200879 } /* switch (record type) */
880
Christopher Faulet67957bd2017-09-27 11:00:59 +0200881 /* Increment the counter for number of records saved into our
882 * local response */
883 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200884
Christopher Faulet67957bd2017-09-27 11:00:59 +0200885 /* Move forward dns_answer_record->data_len for analyzing next
886 * record in the response */
887 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
888 ? offset
889 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200890
891 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200892 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200893 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
894 if (tmp_record->type != dns_answer_record->type)
895 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200896
897 switch(tmp_record->type) {
898 case DNS_RTYPE_A:
899 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
900 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
901 sizeof(in_addr_t)))
902 found = 1;
903 break;
904
905 case DNS_RTYPE_AAAA:
906 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
907 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
908 sizeof(struct in6_addr)))
909 found = 1;
910 break;
911
Olivier Houchard8da5f982017-08-04 18:35:36 +0200912 case DNS_RTYPE_SRV:
913 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200914 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200915 dns_answer_record->port == tmp_record->port) {
916 tmp_record->weight = dns_answer_record->weight;
917 found = 1;
918 }
919 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200920
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200921 default:
922 break;
923 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200924
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200925 if (found == 1)
926 break;
927 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200928
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200929 if (found == 1) {
930 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100931 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200932 }
933 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200934 dns_answer_record->last_seen = now.tv_sec;
935 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
936 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200937 } /* for i 0 to ancount */
938
Christopher Faulet67957bd2017-09-27 11:00:59 +0200939 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200940 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200941 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200942 return DNS_RESP_VALID;
943}
944
Christopher Faulet67957bd2017-09-27 11:00:59 +0200945/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200946 * If existing IP not found, return the first IP matching family_priority,
947 * otherwise, first ip found
948 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200949 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200950 * For both cases above, dns_validate_dns_response is required
951 * returns one of the DNS_UPD_* code
952 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200953int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200954 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100955 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200956 void **newip, short *newip_sin_family,
957 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200958{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200959 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100960 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200961 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200962 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100963 int currentip_sel;
964 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100965 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200966 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200967
Christopher Faulet67957bd2017-09-27 11:00:59 +0200968 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200969 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200970 *newip = newip4 = newip6 = NULL;
971 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200972 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200973 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200974
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100975 /* Select an IP regarding configuration preference.
976 * Top priority is the prefered network ip version,
977 * second priority is the prefered network.
978 * the last priority is the currently used IP,
979 *
980 * For these three priorities, a score is calculated. The
981 * weight are:
Baptiste Assmann741e00a2018-06-22 12:51:51 +0200982 * 8 - prefered ip version.
Baptistefc725902016-12-26 23:21:08 +0100983 * 4 - prefered network.
984 * 2 - if the ip in the record is not affected to any other server in the same backend (duplication)
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100985 * 1 - current ip.
986 * The result with the biggest score is returned.
987 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200988
989 list_for_each_entry(record, &dns_p->answer_list, list) {
990 void *ip;
991 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100992
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200993 if (record->type == DNS_RTYPE_A) {
994 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
995 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200996 }
997 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200998 ip_type = AF_INET6;
999 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001000 }
1001 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001002 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001003 score = 0;
1004
1005 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001006 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001007 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001008
1009 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001010 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001011
1012 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001013 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001014 continue;
1015
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001016 if ((ip_type == AF_INET &&
1017 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001018 &dns_opts->pref_net[j].mask.in4,
1019 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001020 (ip_type == AF_INET6 &&
1021 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001022 &dns_opts->pref_net[j].mask.in6,
1023 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001024 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001025 break;
1026 }
1027 }
1028
Christopher Faulet67957bd2017-09-27 11:00:59 +02001029 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001030 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001031 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001032 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001033 if (!allowed_duplicated_ip) {
1034 continue;
1035 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001036 } else {
1037 score += 2;
1038 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001039
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001040 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001041 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001042 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001043 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001044 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001045 !memcmp(ip, currentip, 16)))) {
1046 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001047 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001048 }
1049 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001050 currentip_sel = 0;
1051
1052 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001053 * score. The maximum score is 15, if this value is reached, we
1054 * break the parsing. Implicitly, this score is reached the ip
1055 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001056 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001057 if (ip_type == AF_INET)
1058 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001059 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001060 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001061 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001062 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001063 return DNS_UPD_NO;
1064 max_score = score;
1065 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001066 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001067
Christopher Faulet67957bd2017-09-27 11:00:59 +02001068 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001069 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001070 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001071
Christopher Faulet67957bd2017-09-27 11:00:59 +02001072 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001073 if (family_priority == AF_INET) {
1074 if (newip4) {
1075 *newip = newip4;
1076 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001077 }
1078 else if (newip6) {
1079 *newip = newip6;
1080 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001082 if (!currentip_found)
1083 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001084 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001085 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001086 else if (family_priority == AF_INET6) {
1087 if (newip6) {
1088 *newip = newip6;
1089 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001090 }
1091 else if (newip4) {
1092 *newip = newip4;
1093 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001094 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001095 if (!currentip_found)
1096 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001097 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001098 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001099 else if (family_priority == AF_UNSPEC) {
1100 if (newip6) {
1101 *newip = newip6;
1102 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001103 }
1104 else if (newip4) {
1105 *newip = newip4;
1106 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001108 if (!currentip_found)
1109 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001110 }
1111
Christopher Faulet67957bd2017-09-27 11:00:59 +02001112 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001113 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001114
Christopher Faulet67957bd2017-09-27 11:00:59 +02001115 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001116 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001117 /* Move the first record to the end of the list, for internal
1118 * round robin */
1119 LIST_DEL(&record->list);
1120 LIST_ADDQ(&dns_p->answer_list, &record->list);
1121 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001122 }
1123 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001124}
1125
Christopher Faulet67957bd2017-09-27 11:00:59 +02001126/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001127 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001128 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1129 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001130 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001131 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1132 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001133 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001134int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001136 char *ptr;
1137 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001138
Christopher Faulet67957bd2017-09-27 11:00:59 +02001139 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001140 return -1;
1141
Christopher Faulet67957bd2017-09-27 11:00:59 +02001142 ptr = str;
1143 for (i = 0; i < dn_len-1; ++i) {
1144 sz = dn[i];
1145 if (i)
1146 *ptr++ = '.';
1147 memcpy(ptr, dn+i+1, sz);
1148 ptr += sz;
1149 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001150 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001151 *ptr++ = '\0';
1152 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001153}
1154
Christopher Faulet67957bd2017-09-27 11:00:59 +02001155/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1156 *
1157 * <str> must be a null-terminated string. <str_len> must include the
1158 * terminating null byte. <dn> buffer must be allocated and its size must be
1159 * passed in <dn_len>.
1160 *
1161 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1162 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001163 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001164int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001165{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001166 int i, offset;
1167
Christopher Faulet67957bd2017-09-27 11:00:59 +02001168 if (dn_len < str_len + 1)
1169 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001170
Christopher Faulet67957bd2017-09-27 11:00:59 +02001171 /* First byte of dn will be used to store the length of the first
1172 * label */
1173 offset = 0;
1174 for (i = 0; i < str_len; ++i) {
1175 if (str[i] == '.') {
1176 /* 2 or more consecutive dots is invalid */
1177 if (i == offset)
1178 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001179
Christopher Faulet67957bd2017-09-27 11:00:59 +02001180 dn[offset] = (i - offset);
1181 offset = i+1;
1182 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001183 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001184 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001185 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001186 dn[offset] = (i - offset - 1);
1187 dn[i] = '\0';
1188 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001189}
1190
Christopher Faulet67957bd2017-09-27 11:00:59 +02001191/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001192 * - total size
1193 * - each label size individually
1194 * returns:
1195 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1196 * 1 when no error. <err> is left unaffected.
1197 */
1198int dns_hostname_validation(const char *string, char **err)
1199{
1200 const char *c, *d;
1201 int i;
1202
1203 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1204 if (err)
1205 *err = DNS_TOO_LONG_FQDN;
1206 return 0;
1207 }
1208
1209 c = string;
1210 while (*c) {
1211 d = c;
1212
1213 i = 0;
1214 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1215 i++;
1216 if (!((*d == '-') || (*d == '_') ||
1217 ((*d >= 'a') && (*d <= 'z')) ||
1218 ((*d >= 'A') && (*d <= 'Z')) ||
1219 ((*d >= '0') && (*d <= '9')))) {
1220 if (err)
1221 *err = DNS_INVALID_CHARACTER;
1222 return 0;
1223 }
1224 d++;
1225 }
1226
1227 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1228 if (err)
1229 *err = DNS_LABEL_TOO_LONG;
1230 return 0;
1231 }
1232
1233 if (*d == '\0')
1234 goto out;
1235
1236 c = ++d;
1237 }
1238 out:
1239 return 1;
1240}
1241
Christopher Faulet67957bd2017-09-27 11:00:59 +02001242/* Picks up an available resolution from the different resolution list
1243 * associated to a resolvers section, in this order:
1244 * 1. check in resolutions.curr for the same hostname and query_type
1245 * 2. check in resolutions.wait for the same hostname and query_type
1246 * 3. Get a new resolution from resolution pool
1247 *
1248 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001249 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001250static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1251 char **hostname_dn, int hostname_dn_len,
1252 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001253{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001254 struct dns_resolution *res;
1255
1256 if (!*hostname_dn)
1257 goto from_pool;
1258
1259 /* Search for same hostname and query type in resolutions.curr */
1260 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1261 if (!res->hostname_dn)
1262 continue;
1263 if ((query_type == res->prefered_query_type) &&
1264 hostname_dn_len == res->hostname_dn_len &&
1265 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1266 return res;
1267 }
1268
1269 /* Search for same hostname and query type in resolutions.wait */
1270 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1271 if (!res->hostname_dn)
1272 continue;
1273 if ((query_type == res->prefered_query_type) &&
1274 hostname_dn_len == res->hostname_dn_len &&
1275 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1276 return res;
1277 }
1278
1279 from_pool:
1280 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001281 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001282 if (res) {
1283 memset(res, 0, sizeof(*res));
1284 res->resolvers = resolvers;
1285 res->uuid = resolution_uuid;
1286 res->status = RSLV_STATUS_NONE;
1287 res->step = RSLV_STEP_NONE;
1288 res->last_valid = now_ms;
1289
1290 LIST_INIT(&res->requesters);
1291 LIST_INIT(&res->response.answer_list);
1292
1293 res->prefered_query_type = query_type;
1294 res->query_type = query_type;
1295 res->hostname_dn = *hostname_dn;
1296 res->hostname_dn_len = hostname_dn_len;
1297
1298 ++resolution_uuid;
1299
1300 /* Move the resolution to the resolvers wait queue */
1301 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1302 }
1303 return res;
1304}
1305
1306/* Releases a resolution from its requester(s) and move it back to the pool */
1307static void dns_free_resolution(struct dns_resolution *resolution)
1308{
1309 struct dns_requester *req, *reqback;
1310
1311 /* clean up configuration */
1312 dns_reset_resolution(resolution);
1313 resolution->hostname_dn = NULL;
1314 resolution->hostname_dn_len = 0;
1315
1316 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1317 LIST_DEL(&req->list);
1318 req->resolution = NULL;
1319 }
1320
1321 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001322 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001323}
1324
1325/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1326 * on success, -1 otherwise.
1327 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001328int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001329{
1330 struct dns_resolution *res = NULL;
1331 struct dns_requester *req;
1332 struct dns_resolvers *resolvers;
1333 struct server *srv = NULL;
1334 struct dns_srvrq *srvrq = NULL;
1335 char **hostname_dn;
1336 int hostname_dn_len, query_type;
1337
1338 switch (requester_type) {
1339 case OBJ_TYPE_SERVER:
1340 srv = (struct server *)requester;
1341 hostname_dn = &srv->hostname_dn;
1342 hostname_dn_len = srv->hostname_dn_len;
1343 resolvers = srv->resolvers;
1344 query_type = ((srv->dns_opts.family_prio == AF_INET)
1345 ? DNS_RTYPE_A
1346 : DNS_RTYPE_AAAA);
1347 break;
1348
1349 case OBJ_TYPE_SRVRQ:
1350 srvrq = (struct dns_srvrq *)requester;
1351 hostname_dn = &srvrq->hostname_dn;
1352 hostname_dn_len = srvrq->hostname_dn_len;
1353 resolvers = srvrq->resolvers;
1354 query_type = DNS_RTYPE_SRV;
1355 break;
1356
1357 default:
1358 goto err;
1359 }
1360
1361 /* Get a resolution from the resolvers' wait queue or pool */
1362 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1363 goto err;
1364
1365 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001366 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001367 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001368 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001369 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001370 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001371 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001372 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001373 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001374 req->owner = &srv->obj_type;
1375 srv->dns_requester = req;
1376 }
1377 else
1378 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001379 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001380 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001381 }
1382 else if (srvrq) {
1383 if (srvrq->dns_requester == NULL) {
1384 if ((req = calloc(1, sizeof(*req))) == NULL)
1385 goto err;
1386 req->owner = &srvrq->obj_type;
1387 srvrq->dns_requester = req;
1388 }
1389 else
1390 req = srvrq->dns_requester;
1391 }
1392 else
1393 goto err;
1394
1395 req->resolution = res;
1396 req->requester_cb = snr_resolution_cb;
1397 req->requester_error_cb = snr_resolution_error_cb;
1398
1399 LIST_ADDQ(&res->requesters, &req->list);
1400 return 0;
1401
1402 err:
1403 if (res && LIST_ISEMPTY(&res->requesters))
1404 dns_free_resolution(res);
1405 return -1;
1406}
1407
1408/* Removes a requester from a DNS resoltion. It takes takes care of all the
1409 * consequences. It also cleans up some parameters from the requester.
1410 */
1411void dns_unlink_resolution(struct dns_requester *requester)
1412{
1413 struct dns_resolution *res;
1414 struct dns_requester *req;
1415
1416 /* Nothing to do */
1417 if (!requester || !requester->resolution)
1418 return;
1419 res = requester->resolution;
1420
1421 /* Clean up the requester */
1422 LIST_DEL(&requester->list);
1423 requester->resolution = NULL;
1424
1425 /* We need to find another requester linked on this resolution */
1426 if (!LIST_ISEMPTY(&res->requesters))
1427 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1428 else {
1429 dns_free_resolution(res);
1430 return;
1431 }
1432
1433 /* Move hostname_dn related pointers to the next requester */
1434 switch (obj_type(req->owner)) {
1435 case OBJ_TYPE_SERVER:
1436 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1437 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1438 break;
1439 case OBJ_TYPE_SRVRQ:
1440 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1441 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1442 break;
1443 default:
1444 res->hostname_dn = NULL;
1445 res->hostname_dn_len = 0;
1446 break;
1447 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001448}
1449
Christopher Faulet67957bd2017-09-27 11:00:59 +02001450/* Called when a network IO is generated on a name server socket for an incoming
1451 * packet. It performs the following actions:
1452 * - check if the packet requires processing (not outdated resolution)
1453 * - ensure the DNS packet received is valid and call requester's callback
1454 * - call requester's error callback if invalid response
1455 * - check the dn_name in the packet against the one sent
1456 */
1457static void dns_resolve_recv(struct dgram_conn *dgram)
1458{
1459 struct dns_nameserver *ns, *tmpns;
1460 struct dns_resolvers *resolvers;
1461 struct dns_resolution *res;
1462 struct dns_query_item *query;
1463 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1464 unsigned char *bufend;
1465 int fd, buflen, dns_resp;
1466 int max_answer_records;
1467 unsigned short query_id;
1468 struct eb32_node *eb;
1469 struct dns_requester *req;
1470
1471 fd = dgram->t.sock.fd;
1472
1473 /* check if ready for reading */
1474 if (!fd_recv_ready(fd))
1475 return;
1476
1477 /* no need to go further if we can't retrieve the nameserver */
1478 if ((ns = dgram->owner) == NULL)
1479 return;
1480
1481 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001482 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001483
1484 /* process all pending input messages */
1485 while (1) {
1486 /* read message received */
1487 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1488 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1489 /* FIXME : for now we consider EAGAIN only */
1490 fd_cant_recv(fd);
1491 break;
1492 }
1493
1494 /* message too big */
1495 if (buflen > resolvers->accepted_payload_size) {
1496 ns->counters.too_big++;
1497 continue;
1498 }
1499
1500 /* initializing variables */
1501 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1502
1503 /* read the query id from the packet (16 bits) */
1504 if (buf + 2 > bufend) {
1505 ns->counters.invalid++;
1506 continue;
1507 }
1508 query_id = dns_response_get_query_id(buf);
1509
1510 /* search the query_id in the pending resolution tree */
1511 eb = eb32_lookup(&resolvers->query_ids, query_id);
1512 if (eb == NULL) {
1513 /* unknown query id means an outdated response and can be safely ignored */
1514 ns->counters.outdated++;
1515 continue;
1516 }
1517
1518 /* known query id means a resolution in prgress */
1519 res = eb32_entry(eb, struct dns_resolution, qid);
1520 if (!res) {
1521 ns->counters.outdated++;
1522 continue;
1523 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001524
Christopher Faulet67957bd2017-09-27 11:00:59 +02001525 /* number of responses received */
1526 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001527
Christopher Faulet67957bd2017-09-27 11:00:59 +02001528 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1529 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001530
Christopher Faulet67957bd2017-09-27 11:00:59 +02001531 switch (dns_resp) {
1532 case DNS_RESP_VALID:
1533 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001534
Christopher Faulet67957bd2017-09-27 11:00:59 +02001535 case DNS_RESP_INVALID:
1536 case DNS_RESP_QUERY_COUNT_ERROR:
1537 case DNS_RESP_WRONG_NAME:
1538 res->status = RSLV_STATUS_INVALID;
1539 ns->counters.invalid++;
1540 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001541
Christopher Faulet67957bd2017-09-27 11:00:59 +02001542 case DNS_RESP_NX_DOMAIN:
1543 res->status = RSLV_STATUS_NX;
1544 ns->counters.nx++;
1545 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001546
Christopher Faulet67957bd2017-09-27 11:00:59 +02001547 case DNS_RESP_REFUSED:
1548 res->status = RSLV_STATUS_REFUSED;
1549 ns->counters.refused++;
1550 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001551
Christopher Faulet67957bd2017-09-27 11:00:59 +02001552 case DNS_RESP_ANCOUNT_ZERO:
1553 res->status = RSLV_STATUS_OTHER;
1554 ns->counters.any_err++;
1555 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001556
Christopher Faulet67957bd2017-09-27 11:00:59 +02001557 case DNS_RESP_CNAME_ERROR:
1558 res->status = RSLV_STATUS_OTHER;
1559 ns->counters.cname_error++;
1560 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001561
Christopher Faulet67957bd2017-09-27 11:00:59 +02001562 case DNS_RESP_TRUNCATED:
1563 res->status = RSLV_STATUS_OTHER;
1564 ns->counters.truncated++;
1565 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001566
Christopher Faulet67957bd2017-09-27 11:00:59 +02001567 case DNS_RESP_NO_EXPECTED_RECORD:
1568 case DNS_RESP_ERROR:
1569 case DNS_RESP_INTERNAL:
1570 res->status = RSLV_STATUS_OTHER;
1571 ns->counters.other++;
1572 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001573 }
1574
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575 /* Wait all nameservers response to handle errors */
1576 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1577 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001578
Christopher Faulet67957bd2017-09-27 11:00:59 +02001579 /* Process error codes */
1580 if (dns_resp != DNS_RESP_VALID) {
1581 if (res->prefered_query_type != res->query_type) {
1582 /* The fallback on the query type was already performed,
1583 * so check the try counter. If it falls to 0, we can
1584 * report an error. Else, wait the next attempt. */
1585 if (!res->try)
1586 goto report_res_error;
1587 }
1588 else {
1589 /* Fallback from A to AAAA or the opposite and re-send
1590 * the resolution immediately. try counter is not
1591 * decremented. */
1592 if (res->prefered_query_type == DNS_RTYPE_A) {
1593 res->query_type = DNS_RTYPE_AAAA;
1594 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001595 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001596 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1597 res->query_type = DNS_RTYPE_A;
1598 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001599 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001600 }
1601 continue;
1602 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001603
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604 /* Now let's check the query's dname corresponds to the one we
1605 * sent. We can check only the first query of the list. We send
1606 * one query at a time so we get one query in the response */
1607 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1608 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1609 dns_resp = DNS_RESP_WRONG_NAME;
1610 ns->counters.other++;
1611 goto report_res_error;
1612 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001613
Christopher Faulet67957bd2017-09-27 11:00:59 +02001614 /* So the resolution succeeded */
1615 res->status = RSLV_STATUS_VALID;
1616 res->last_valid = now_ms;
1617 ns->counters.valid++;
1618 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001619
Christopher Faulet67957bd2017-09-27 11:00:59 +02001620 report_res_error:
1621 list_for_each_entry(req, &res->requesters, list)
1622 req->requester_error_cb(req, dns_resp);
1623 dns_reset_resolution(res);
1624 LIST_DEL(&res->list);
1625 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1626 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001627
Christopher Faulet67957bd2017-09-27 11:00:59 +02001628 report_res_success:
1629 /* Only the 1rst requester s managed by the server, others are
1630 * from the cache */
1631 tmpns = ns;
1632 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001633 struct server *s = objt_server(req->owner);
1634
1635 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001636 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001637 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001638 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001639 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001640 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001641 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001642
Christopher Faulet67957bd2017-09-27 11:00:59 +02001643 dns_reset_resolution(res);
1644 LIST_DEL(&res->list);
1645 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1646 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001647 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001648 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001649 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001650}
William Lallemand69e96442016-11-19 00:58:54 +01001651
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652/* Called when a resolvers network socket is ready to send data */
1653static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001654{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001655 struct dns_resolvers *resolvers;
1656 struct dns_nameserver *ns;
1657 struct dns_resolution *res;
1658 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001659
Christopher Faulet67957bd2017-09-27 11:00:59 +02001660 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001661
Christopher Faulet67957bd2017-09-27 11:00:59 +02001662 /* check if ready for sending */
1663 if (!fd_send_ready(fd))
1664 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001665
Christopher Faulet67957bd2017-09-27 11:00:59 +02001666 /* we don't want/need to be waked up any more for sending */
1667 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001668
Christopher Faulet67957bd2017-09-27 11:00:59 +02001669 /* no need to go further if we can't retrieve the nameserver */
1670 if ((ns = dgram->owner) == NULL)
1671 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001672
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001674 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001675
Christopher Faulet67957bd2017-09-27 11:00:59 +02001676 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1677 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001678
Christopher Faulet67957bd2017-09-27 11:00:59 +02001679 if (res->nb_queries == resolvers->nb_nameservers)
1680 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001681
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001682 trash.data = dns_build_query(res->query_id, res->query_type,
Christopher Faulet67957bd2017-09-27 11:00:59 +02001683 resolvers->accepted_payload_size,
1684 res->hostname_dn, res->hostname_dn_len,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001685 trash.area, trash.size);
1686 if (trash.data == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001688
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001689 ret = send(fd, trash.area, trash.data, 0);
1690 if (ret != trash.data)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001692
Christopher Faulet67957bd2017-09-27 11:00:59 +02001693 ns->counters.sent++;
1694 res->nb_queries++;
1695 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001696
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697 snd_error:
1698 ns->counters.snd_error++;
1699 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001700 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001701 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001702}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001703
Christopher Faulet67957bd2017-09-27 11:00:59 +02001704/* Processes DNS resolution. First, it checks the active list to detect expired
1705 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1706 * checks the wait list to trigger new resolutions.
1707 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001708static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001709{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001710 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001711 struct dns_resolution *res, *resback;
1712 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001713
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001714 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001715
Christopher Faulet67957bd2017-09-27 11:00:59 +02001716 /* Handle all expired resolutions from the active list */
1717 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1718 /* When we find the first resolution in the future, then we can
1719 * stop here */
1720 exp = tick_add(res->last_query, resolvers->timeout.retry);
1721 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001722 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001723
Christopher Faulet67957bd2017-09-27 11:00:59 +02001724 /* If current resolution has been tried too many times and
1725 * finishes in timeout we update its status and remove it from
1726 * the list */
1727 if (!res->try) {
1728 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001729
Christopher Faulet67957bd2017-09-27 11:00:59 +02001730 /* Notify the result to the requesters */
1731 if (!res->nb_responses)
1732 res->status = RSLV_STATUS_TIMEOUT;
1733 list_for_each_entry(req, &res->requesters, list)
1734 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001735
Christopher Faulet67957bd2017-09-27 11:00:59 +02001736 /* Clean up resolution info and remove it from the
1737 * current list */
1738 dns_reset_resolution(res);
1739 LIST_DEL(&res->list);
1740 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001741 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001742 else {
1743 /* Otherwise resend the DNS query and requeue the resolution */
1744 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1745 /* No response received (a real timeout) or fallback already done */
1746 res->query_type = res->prefered_query_type;
1747 res->try--;
1748 }
1749 else {
1750 /* Fallback from A to AAAA or the opposite and re-send
1751 * the resolution immediately. try counter is not
1752 * decremented. */
1753 if (res->prefered_query_type == DNS_RTYPE_A)
1754 res->query_type = DNS_RTYPE_AAAA;
1755 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1756 res->query_type = DNS_RTYPE_A;
1757 else
1758 res->try--;
1759 }
1760 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001761 }
1762 }
William Lallemand69e96442016-11-19 00:58:54 +01001763
Christopher Faulet67957bd2017-09-27 11:00:59 +02001764 /* Handle all resolutions in the wait list */
1765 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1766 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1767 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1768 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001769
Christopher Faulet67957bd2017-09-27 11:00:59 +02001770 if (dns_run_resolution(res) != 1) {
1771 res->last_resolution = now_ms;
1772 LIST_DEL(&res->list);
1773 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001774 }
1775 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001776
Christopher Faulet67957bd2017-09-27 11:00:59 +02001777 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001778 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001779 return t;
1780}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001781
Christopher Faulet67957bd2017-09-27 11:00:59 +02001782/* proto_udp callback functions for a DNS resolution */
1783struct dgram_data_cb resolve_dgram_cb = {
1784 .recv = dns_resolve_recv,
1785 .send = dns_resolve_send,
1786};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001787
Christopher Faulet67957bd2017-09-27 11:00:59 +02001788/* Release memory allocated by DNS */
1789static void dns_deinit(void)
1790{
1791 struct dns_resolvers *resolvers, *resolversback;
1792 struct dns_nameserver *ns, *nsback;
1793 struct dns_resolution *res, *resback;
1794 struct dns_requester *req, *reqback;
1795 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001796
Christopher Faulet67957bd2017-09-27 11:00:59 +02001797 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1798 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1799 free(ns->id);
1800 free((char *)ns->conf.file);
1801 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1802 fd_delete(ns->dgram->t.sock.fd);
1803 free(ns->dgram);
1804 LIST_DEL(&ns->list);
1805 free(ns);
1806 }
1807
1808 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1809 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1810 LIST_DEL(&req->list);
1811 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001812 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001813 dns_free_resolution(res);
1814 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001815
Christopher Faulet67957bd2017-09-27 11:00:59 +02001816 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1817 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1818 LIST_DEL(&req->list);
1819 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001820 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001821 dns_free_resolution(res);
1822 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001823
Christopher Faulet67957bd2017-09-27 11:00:59 +02001824 free(resolvers->id);
1825 free((char *)resolvers->conf.file);
1826 task_delete(resolvers->t);
1827 task_free(resolvers->t);
1828 LIST_DEL(&resolvers->list);
1829 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001830 }
1831
Christopher Faulet67957bd2017-09-27 11:00:59 +02001832 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1833 free(srvrq->name);
1834 free(srvrq->hostname_dn);
1835 LIST_DEL(&srvrq->list);
1836 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001837 }
1838
Willy Tarreaubafbe012017-11-24 17:34:44 +01001839 pool_destroy(dns_answer_item_pool);
1840 pool_destroy(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001841}
1842
Christopher Faulet67957bd2017-09-27 11:00:59 +02001843/* Finalizes the DNS configuration by allocating required resources and checking
1844 * live parameters.
1845 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001846 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001847static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001848{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001849 struct dns_resolvers *resolvers;
1850 struct proxy *px;
1851 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001852
Christopher Faulet67957bd2017-09-27 11:00:59 +02001853 /* allocate pool of resolution per resolvers */
1854 list_for_each_entry(resolvers, &dns_resolvers, list) {
1855 struct dns_nameserver *ns;
1856 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001857
Christopher Faulet67957bd2017-09-27 11:00:59 +02001858 /* Check if we can create the socket with nameservers info */
1859 list_for_each_entry(ns, &resolvers->nameservers, list) {
1860 struct dgram_conn *dgram = NULL;
1861 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001862
Christopher Faulet67957bd2017-09-27 11:00:59 +02001863 /* Check nameserver info */
1864 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001865 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1866 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001868 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001869 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001870 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001871 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1872 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001873 close(fd);
1874 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001875 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001876 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001877 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001878
Christopher Faulet67957bd2017-09-27 11:00:59 +02001879 /* Create dgram structure that will hold the UPD socket
1880 * and attach it on the current nameserver */
1881 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001882 ha_alert("config: resolvers '%s' : out of memory.\n",
1883 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001884 err_code |= (ERR_ALERT|ERR_ABORT);
1885 goto err;
1886 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001887
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888 /* Leave dgram partially initialized, no FD attached for
1889 * now. */
1890 dgram->owner = ns;
1891 dgram->data = &resolve_dgram_cb;
1892 dgram->t.sock.fd = -1;
1893 ns->dgram = dgram;
1894 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001895
Christopher Faulet67957bd2017-09-27 11:00:59 +02001896 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001897 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001898 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001899 err_code |= (ERR_ALERT|ERR_ABORT);
1900 goto err;
1901 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001902
Christopher Faulet67957bd2017-09-27 11:00:59 +02001903 /* Update task's parameters */
1904 t->process = dns_process_resolvers;
1905 t->context = resolvers;
1906 resolvers->t = t;
1907 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001908 }
1909
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001910 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001911 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001912
Christopher Faulet67957bd2017-09-27 11:00:59 +02001913 for (srv = px->srv; srv; srv = srv->next) {
1914 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001915
Christopher Faulet67957bd2017-09-27 11:00:59 +02001916 if (!srv->resolvers_id)
1917 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001918
Christopher Faulet67957bd2017-09-27 11:00:59 +02001919 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001920 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1921 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 err_code |= (ERR_ALERT|ERR_ABORT);
1923 continue;
1924 }
1925 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001926
Christopher Faulet67957bd2017-09-27 11:00:59 +02001927 if (srv->srvrq && !srv->srvrq->resolvers) {
1928 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001929 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001930 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1931 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 err_code |= (ERR_ALERT|ERR_ABORT);
1933 continue;
1934 }
1935 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001936 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001937 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1938 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001939 err_code |= (ERR_ALERT|ERR_ABORT);
1940 continue;
1941 }
1942 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001943 }
1944
Christopher Faulet67957bd2017-09-27 11:00:59 +02001945 if (err_code & (ERR_ALERT|ERR_ABORT))
1946 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001947
Christopher Faulet67957bd2017-09-27 11:00:59 +02001948 return err_code;
1949 err:
1950 dns_deinit();
1951 return err_code;
1952
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001953}
1954
Christopher Faulet67957bd2017-09-27 11:00:59 +02001955/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001956static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001957{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001958 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001959
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 if (*args[2]) {
1961 list_for_each_entry(presolvers, &dns_resolvers, list) {
1962 if (strcmp(presolvers->id, args[2]) == 0) {
1963 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001964 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001965 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001966 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001967 if (appctx->ctx.cli.p0 == NULL) {
1968 appctx->ctx.cli.severity = LOG_ERR;
1969 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1970 appctx->st0 = CLI_ST_PRINT;
1971 return 1;
1972 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001973 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001974 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001975}
1976
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977/* Dumps counters from all resolvers section and associated name servers. It
1978 * returns 0 if the output buffer is full and it needs to be called again,
1979 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001980 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001981 */
1982static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1983{
1984 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001985 struct dns_resolvers *resolvers;
1986 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001987
1988 chunk_reset(&trash);
1989
1990 switch (appctx->st2) {
1991 case STAT_ST_INIT:
1992 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1993 /* fall through */
1994
1995 case STAT_ST_LIST:
1996 if (LIST_ISEMPTY(&dns_resolvers)) {
1997 chunk_appendf(&trash, "No resolvers found\n");
1998 }
1999 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002000 list_for_each_entry(resolvers, &dns_resolvers, list) {
2001 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002002 continue;
2003
Christopher Faulet67957bd2017-09-27 11:00:59 +02002004 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2005 list_for_each_entry(ns, &resolvers->nameservers, list) {
2006 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2007 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2008 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2009 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2010 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2011 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2012 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2013 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2014 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2015 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2016 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2017 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2018 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2019 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2020 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2021 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002022 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002023 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002024 }
2025 }
2026
2027 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002028 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002029 /* let's try again later from this session. We add ourselves into
2030 * this session's users so that it can remove us upon termination.
2031 */
2032 si->flags |= SI_FL_WAIT_ROOM;
2033 return 0;
2034 }
William Lallemand69e96442016-11-19 00:58:54 +01002035 /* fall through */
2036
2037 default:
2038 appctx->st2 = STAT_ST_FIN;
2039 return 1;
2040 }
2041}
2042
2043/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002044static struct cli_kw_list cli_kws = {{ }, {
2045 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2046 " associated name servers",
2047 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2048 {{},}
2049 }
2050};
William Lallemand69e96442016-11-19 00:58:54 +01002051
2052
2053__attribute__((constructor))
2054static void __dns_init(void)
2055{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002056 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2057 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2058
Christopher Faulet67957bd2017-09-27 11:00:59 +02002059 hap_register_post_check(dns_finalize_config);
2060 hap_register_post_deinit(dns_deinit);
2061
William Lallemand69e96442016-11-19 00:58:54 +01002062 cli_register_kw(&cli_kws);
2063}