blob: c1396f525985de4d0494dc1d28cb960d19118d84 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Baptiste Assmann044fd5b2018-08-10 10:56:38 +020022#include <common/cfgparse.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020023#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020025#include <common/time.h>
26#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020027#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020028
William Lallemand69e96442016-11-19 00:58:54 +010029#include <types/applet.h>
30#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020031#include <types/global.h>
32#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010033#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020034
William Lallemand69e96442016-11-19 00:58:54 +010035#include <proto/channel.h>
36#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020037#include <proto/checks.h>
38#include <proto/dns.h>
39#include <proto/fd.h>
40#include <proto/log.h>
41#include <proto/server.h>
42#include <proto/task.h>
43#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020044#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010045#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020046
Christopher Faulet67957bd2017-09-27 11:00:59 +020047struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
48struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020049
Christopher Fauletb2812a62017-10-04 16:17:58 +020050static THREAD_LOCAL int64_t dns_query_id_seed = 0; /* random seed */
Willy Tarreau8ceae722018-11-26 11:58:30 +010051
52DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item));
53DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution));
54
Christopher Faulet67957bd2017-09-27 11:00:59 +020055static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020056
Christopher Faulet67957bd2017-09-27 11:00:59 +020057/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
58 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020059 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020060struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020061{
Christopher Faulet67957bd2017-09-27 11:00:59 +020062 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063
Christopher Faulet67957bd2017-09-27 11:00:59 +020064 list_for_each_entry(res, &dns_resolvers, list) {
65 if (!strcmp(res->id, id))
66 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020067 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020068 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069}
70
Christopher Faulet67957bd2017-09-27 11:00:59 +020071/* Returns a pointer on the SRV request matching the name <name> for the proxy
72 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020074struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020075{
Christopher Faulet67957bd2017-09-27 11:00:59 +020076 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077
Christopher Faulet67957bd2017-09-27 11:00:59 +020078 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
79 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
80 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020081 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020082 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020083}
84
Christopher Faulet67957bd2017-09-27 11:00:59 +020085/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
86 * NULL if an error occurred. */
87struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020088{
Christopher Faulet67957bd2017-09-27 11:00:59 +020089 struct proxy *px = srv->proxy;
90 struct dns_srvrq *srvrq = NULL;
91 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020092
Christopher Faulet67957bd2017-09-27 11:00:59 +020093 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020094 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
95 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +020096 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010097 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
98 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +020099 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200100 }
101
Christopher Faulet67957bd2017-09-27 11:00:59 +0200102 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100103 ha_alert("config : %s '%s', server '%s': out of memory\n",
104 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200105 goto err;
106 }
107 srvrq->obj_type = OBJ_TYPE_SRVRQ;
108 srvrq->proxy = px;
109 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200110 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200111 srvrq->hostname_dn_len = hostname_dn_len;
112 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100113 ha_alert("config : %s '%s', server '%s': out of memory\n",
114 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 goto err;
116 }
117 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
118 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200119
Christopher Faulet67957bd2017-09-27 11:00:59 +0200120 err:
121 if (srvrq) {
122 free(srvrq->name);
123 free(srvrq->hostname_dn);
124 free(srvrq);
125 }
126 return NULL;
127}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200128
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200129
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130/* 2 bytes random generator to generate DNS query ID */
131static inline uint16_t dns_rnd16(void)
132{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200133 if (!dns_query_id_seed)
134 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200135 dns_query_id_seed ^= dns_query_id_seed << 13;
136 dns_query_id_seed ^= dns_query_id_seed >> 7;
137 dns_query_id_seed ^= dns_query_id_seed << 17;
138 return dns_query_id_seed;
139}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200140
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200141
Christopher Faulet67957bd2017-09-27 11:00:59 +0200142static inline int dns_resolution_timeout(struct dns_resolution *res)
143{
144 switch (res->status) {
145 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
146 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200147 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200148}
149
Christopher Faulet67957bd2017-09-27 11:00:59 +0200150/* Updates a resolvers' task timeout for next wake up and queue it */
151static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200152{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200153 struct dns_resolution *res;
154 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200155
Christopher Faulet67957bd2017-09-27 11:00:59 +0200156 next = tick_add(now_ms, resolvers->timeout.resolve);
157 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
158 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
159 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
160 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200161
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162 list_for_each_entry(res, &resolvers->resolutions.wait, list)
163 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200164
Christopher Faulet67957bd2017-09-27 11:00:59 +0200165 resolvers->t->expire = next;
166 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200167}
168
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
170 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200174 struct dgram_conn *dgram = ns->dgram;
175 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200176
Christopher Faulet67957bd2017-09-27 11:00:59 +0200177 /* Already connected */
178 if (dgram->t.sock.fd != -1)
179 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200180
Christopher Faulet67957bd2017-09-27 11:00:59 +0200181 /* Create an UDP socket and connect it on the nameserver's IP/Port */
182 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
183 send_log(NULL, LOG_WARNING,
184 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
185 ns->resolvers->id, ns->id);
186 return -1;
187 }
188 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
189 send_log(NULL, LOG_WARNING,
190 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
191 ns->resolvers->id, ns->id);
192 close(fd);
193 return -1;
194 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200195
Christopher Faulet67957bd2017-09-27 11:00:59 +0200196 /* Make the socket non blocking */
197 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200198
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 /* Add the fd in the fd list and update its parameters */
200 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100201 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202 fd_want_recv(fd);
203 return 0;
204}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200205
Christopher Faulet67957bd2017-09-27 11:00:59 +0200206/* Forges a DNS query. It needs the following information from the caller:
207 * - <query_id> : the DNS query id corresponding to this query
208 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
209 * - <hostname_dn> : hostname in domain name format
210 * - <hostname_dn_len> : length of <hostname_dn>
211 *
212 * To store the query, the caller must pass a buffer <buf> and its size
213 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
214 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200215 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200216static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
217 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200218{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200219 struct dns_header dns_hdr;
220 struct dns_question qinfo;
221 struct dns_additional_record edns;
222 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
225 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200226
Christopher Faulet67957bd2017-09-27 11:00:59 +0200227 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200228
Christopher Faulet67957bd2017-09-27 11:00:59 +0200229 /* Set dns query headers */
230 dns_hdr.id = (unsigned short) htons(query_id);
231 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
232 dns_hdr.qdcount = htons(1); /* 1 question */
233 dns_hdr.ancount = 0;
234 dns_hdr.nscount = 0;
235 dns_hdr.arcount = htons(1);
236 memcpy(p, &dns_hdr, sizeof(dns_hdr));
237 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200238
Christopher Faulet67957bd2017-09-27 11:00:59 +0200239 /* Set up query hostname */
240 memcpy(p, hostname_dn, hostname_dn_len);
241 p += hostname_dn_len;
242 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200243
Christopher Faulet67957bd2017-09-27 11:00:59 +0200244 /* Set up query info (type and class) */
245 qinfo.qtype = htons(query_type);
246 qinfo.qclass = htons(DNS_RCLASS_IN);
247 memcpy(p, &qinfo, sizeof(qinfo));
248 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200249
Christopher Faulet67957bd2017-09-27 11:00:59 +0200250 /* Set the DNS extension */
251 edns.name = 0;
252 edns.type = htons(DNS_RTYPE_OPT);
253 edns.udp_payload_size = htons(accepted_payload_size);
254 edns.extension = 0;
255 edns.data_length = 0;
256 memcpy(p, &edns, sizeof(edns));
257 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200258
Christopher Faulet67957bd2017-09-27 11:00:59 +0200259 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200260}
261
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
263 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200264 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200267 struct dns_resolvers *resolvers = resolution->resolvers;
268 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200269
Christopher Faulet67957bd2017-09-27 11:00:59 +0200270 list_for_each_entry(ns, &resolvers->nameservers, list) {
271 int fd = ns->dgram->t.sock.fd;
272 if (fd == -1) {
273 if (dns_connect_namesaver(ns) == -1)
274 continue;
275 fd = ns->dgram->t.sock.fd;
276 resolvers->nb_nameservers++;
277 }
278 fd_want_send(fd);
279 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281 /* Update resolution */
282 resolution->nb_queries = 0;
283 resolution->nb_responses = 0;
284 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200285
Christopher Faulet67957bd2017-09-27 11:00:59 +0200286 /* Push the resolution at the end of the active list */
287 LIST_DEL(&resolution->list);
288 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
289 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200290}
291
Christopher Faulet67957bd2017-09-27 11:00:59 +0200292/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
293 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200294 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295static int
296dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200297{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298 struct dns_resolvers *resolvers = resolution->resolvers;
299 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200300
Christopher Faulet67957bd2017-09-27 11:00:59 +0200301 /* Avoid sending requests for resolutions that don't yet have an
302 * hostname, ie resolutions linked to servers that do not yet have an
303 * fqdn */
304 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200305 return 0;
306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307 /* Check if a resolution has already been started for this server return
308 * directly to avoid resolution pill up. */
309 if (resolution->step != RSLV_STEP_NONE)
310 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200311
Christopher Faulet67957bd2017-09-27 11:00:59 +0200312 /* Generates a new query id. We try at most 100 times to find a free
313 * query id */
314 for (i = 0; i < 100; ++i) {
315 query_id = dns_rnd16();
316 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200317 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200318 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200319 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200320 if (query_id == -1) {
321 send_log(NULL, LOG_NOTICE,
322 "could not generate a query id for %s, in resolvers %s.\n",
323 resolution->hostname_dn, resolvers->id);
324 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200325 }
326
Christopher Faulet67957bd2017-09-27 11:00:59 +0200327 /* Update resolution parameters */
328 resolution->query_id = query_id;
329 resolution->qid.key = query_id;
330 resolution->step = RSLV_STEP_RUNNING;
331 resolution->query_type = resolution->prefered_query_type;
332 resolution->try = resolvers->resolve_retries;
333 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200334
Christopher Faulet67957bd2017-09-27 11:00:59 +0200335 /* Send the DNS query */
336 resolution->try -= 1;
337 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338 return 1;
339}
340
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341/* Performs a name resolution for the requester <req> */
342void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200343{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200344 struct dns_resolvers *resolvers;
345 struct dns_resolution *res;
346 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200347
Christopher Faulet67957bd2017-09-27 11:00:59 +0200348 if (!req || !req->resolution)
349 return;
350 res = req->resolution;
351 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200352
Christopher Faulet67957bd2017-09-27 11:00:59 +0200353 /* The resolution must not be triggered yet. Use the cached response, if
354 * valid */
355 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200356 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
357 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
358 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200360
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200361
Christopher Faulet67957bd2017-09-27 11:00:59 +0200362/* Resets some resolution parameters to initial values and also delete the query
363 * ID from the resolver's tree.
364 */
365static void dns_reset_resolution(struct dns_resolution *resolution)
366{
367 /* update resolution status */
368 resolution->step = RSLV_STEP_NONE;
369 resolution->try = 0;
370 resolution->last_resolution = now_ms;
371 resolution->nb_queries = 0;
372 resolution->nb_responses = 0;
373 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200374
Christopher Faulet67957bd2017-09-27 11:00:59 +0200375 /* clean up query id */
376 eb32_delete(&resolution->qid);
377 resolution->query_id = 0;
378 resolution->qid.key = 0;
379}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200380
Christopher Faulet67957bd2017-09-27 11:00:59 +0200381/* Returns the query id contained in a DNS response */
382static inline unsigned short dns_response_get_query_id(unsigned char *resp)
383{
384 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200385}
386
Christopher Faulet67957bd2017-09-27 11:00:59 +0200387
388/* Analyses, re-builds and copies the name <name> from the DNS response packet
389 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
390 * for compressed data. The result is copied into <dest>, ensuring we don't
391 * overflow using <dest_len> Returns the number of bytes the caller can move
392 * forward. If 0 it means an error occurred while parsing the name. <offset> is
393 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200394 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200395int dns_read_name(unsigned char *buffer, unsigned char *bufend,
396 unsigned char *name, char *destination, int dest_len,
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100397 int *offset, unsigned int depth)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200398{
399 int nb_bytes = 0, n = 0;
400 int label_len;
401 unsigned char *reader = name;
402 char *dest = destination;
403
404 while (1) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100405 if (reader >= bufend)
406 goto err;
407
Christopher Faulet67957bd2017-09-27 11:00:59 +0200408 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200409 if ((*reader & 0xc0) == 0xc0) {
Remi Gacogne2d19fbc2018-12-05 17:55:10 +0100410 if (reader + 1 >= bufend)
411 goto err;
412
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 /* Must point BEFORE current position */
414 if ((buffer + reader[1]) > reader)
415 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200416
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100417 if (depth++ > 100)
418 goto err;
419
Christopher Faulet67957bd2017-09-27 11:00:59 +0200420 n = dns_read_name(buffer, bufend, buffer + reader[1],
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100421 dest, dest_len - nb_bytes, offset, depth);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200422 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200423 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200424
Christopher Faulet67957bd2017-09-27 11:00:59 +0200425 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200426 nb_bytes += n;
427 goto out;
428 }
429
430 label_len = *reader;
431 if (label_len == 0)
432 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200433
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200434 /* Check if:
435 * - we won't read outside the buffer
436 * - there is enough place in the destination
437 */
438 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200440
441 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200442 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200443
444 memcpy(dest, reader, label_len);
445
Christopher Faulet67957bd2017-09-27 11:00:59 +0200446 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200447 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200448 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200449 }
450
Christopher Faulet67957bd2017-09-27 11:00:59 +0200451 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200452 /* offset computation:
453 * parse from <name> until finding either NULL or a pointer "c0xx"
454 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200455 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200456 *offset = 0;
457 while (reader < bufend) {
458 if ((reader[0] & 0xc0) == 0xc0) {
459 *offset += 2;
460 break;
461 }
462 else if (*reader == 0) {
463 *offset += 1;
464 break;
465 }
466 *offset += 1;
467 ++reader;
468 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200469 return nb_bytes;
470
Christopher Faulet67957bd2017-09-27 11:00:59 +0200471 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200472 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200473}
474
Christopher Faulet67957bd2017-09-27 11:00:59 +0200475/* Checks for any obsolete record, also identify any SRV request, and try to
476 * find a corresponding server.
477*/
478static void dns_check_dns_response(struct dns_resolution *res)
479{
480 struct dns_resolvers *resolvers = res->resolvers;
481 struct dns_requester *req, *reqback;
482 struct dns_answer_item *item, *itemback;
483 struct server *srv;
484 struct dns_srvrq *srvrq;
485
486 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
487
488 /* Remove obsolete items */
489 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
490 if (item->type != DNS_RTYPE_SRV)
491 goto rm_obselete_item;
492
493 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
494 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
495 continue;
496
497 /* Remove any associated server */
498 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100499 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200500 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
501 item->data_len == srv->hostname_dn_len &&
502 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
503 snr_update_srv_status(srv, 1);
504 free(srv->hostname);
505 free(srv->hostname_dn);
506 srv->hostname = NULL;
507 srv->hostname_dn = NULL;
508 srv->hostname_dn_len = 0;
509 dns_unlink_resolution(srv->dns_requester);
510 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100511 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200512 }
513 }
514
515 rm_obselete_item:
516 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100517 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200518 continue;
519 }
520
521 if (item->type != DNS_RTYPE_SRV)
522 continue;
523
524 /* Now process SRV records */
525 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
526 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
527 continue;
528
529 /* Check if a server already uses that hostname */
530 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100531 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200532 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
533 item->data_len == srv->hostname_dn_len &&
534 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100535 int ha_weight;
536
537 /* Make sure weight is at least 1, so
538 * that the server will be used.
539 */
540 ha_weight = item->weight / 256 + 1;
541 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200542 char weight[9];
543
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100544 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200545 server_parse_weight_change_request(srv, weight);
546 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200548 break;
549 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100550 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551 }
552 if (srv)
553 continue;
554
555 /* If not, try to find a server with undefined hostname */
556 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100557 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200558 if (srv->srvrq == srvrq && !srv->hostname_dn)
559 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200561 }
562 /* And update this server, if found */
563 if (srv) {
564 const char *msg = NULL;
565 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100566 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200567 char hostname[DNS_MAX_NAME_SIZE];
568
569 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100570 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100571 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200572 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100573 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100574 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200575 if (msg)
576 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
577
578 srv->svc_port = item->port;
579 srv->flags &= ~SRV_F_MAPPORTS;
580 if ((srv->check.state & CHK_ST_CONFIGURED) &&
581 !(srv->flags & SRV_F_CHECKPORT))
582 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100583
584 /* Make sure weight is at least 1, so
585 * that the server will be used.
586 */
587 ha_weight = item->weight / 256 + 1;
588
589 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200590 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100591 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200592 }
593 }
594 }
595}
596
597/* Validates that the buffer DNS response provided in <resp> and finishing
598 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200599 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200600 * The result is stored in <resolution>' response, buf_response,
601 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200602 *
603 * This function returns one of the DNS_RESP_* code to indicate the type of
604 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200605 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200606static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
607 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200608{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200609 unsigned char *reader;
610 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200611 int len, flags, offset;
612 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200613 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200614 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200615 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200616 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200617 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618
Christopher Faulet67957bd2017-09-27 11:00:59 +0200619 reader = resp;
620 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200621 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200622 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200623
Christopher Faulet67957bd2017-09-27 11:00:59 +0200624 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200625 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200626
627 /* query id */
628 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200629 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200630 dns_p->header.id = reader[0] * 256 + reader[1];
631 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200632
Christopher Faulet67957bd2017-09-27 11:00:59 +0200633 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200634 * First byte contains:
635 * - response flag (1 bit)
636 * - opcode (4 bits)
637 * - authoritative (1 bit)
638 * - truncated (1 bit)
639 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200640 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200641 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200642 return DNS_RESP_INVALID;
643
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200644 flags = reader[0] * 256 + reader[1];
645
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200646 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
647 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200648 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200649 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200650 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200651 return DNS_RESP_ERROR;
652 }
653
Christopher Faulet67957bd2017-09-27 11:00:59 +0200654 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200655 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200656
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200657 /* 2 bytes for question count */
658 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200659 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200660 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200661 /* (for now) we send one query only, so we expect only one in the
662 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200663 if (dns_p->header.qdcount != 1)
664 return DNS_RESP_QUERY_COUNT_ERROR;
665 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200666 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200667 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200668
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 /* 2 bytes for answer count */
670 if (reader + 2 >= bufend)
671 return DNS_RESP_INVALID;
672 dns_p->header.ancount = reader[0] * 256 + reader[1];
673 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200674 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200675 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200676 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200677 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200678 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200679
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200680 /* 2 bytes authority count */
681 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200682 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200683 dns_p->header.nscount = reader[0] * 256 + reader[1];
684 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200685
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200686 /* 2 bytes additional count */
687 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200688 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200689 dns_p->header.arcount = reader[0] * 256 + reader[1];
690 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200691
Christopher Faulet67957bd2017-09-27 11:00:59 +0200692 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200693 LIST_INIT(&dns_p->query_list);
694 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 +0200695 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200697 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200698 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
699 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200700 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200701 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200702
Christopher Faulet67957bd2017-09-27 11:00:59 +0200703 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200704 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200705 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200706 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100707 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200708
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200709 if (len == 0)
710 return DNS_RESP_INVALID;
711
712 reader += offset;
713 previous_dname = dns_query->name;
714
715 /* move forward 2 bytes for question type */
716 if (reader + 2 >= bufend)
717 return DNS_RESP_INVALID;
718 dns_query->type = reader[0] * 256 + reader[1];
719 reader += 2;
720
721 /* move forward 2 bytes for question class */
722 if (reader + 2 >= bufend)
723 return DNS_RESP_INVALID;
724 dns_query->class = reader[0] * 256 + reader[1];
725 reader += 2;
726 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200727
Baptiste Assmann251abb92017-08-11 09:58:27 +0200728 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200729 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200730 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
731 return DNS_RESP_TRUNCATED;
732
Baptiste Assmann325137d2015-04-13 23:40:55 +0200733 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200734 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200735 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200736 if (reader >= bufend)
737 return DNS_RESP_INVALID;
738
Willy Tarreaubafbe012017-11-24 17:34:44 +0100739 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200740 if (dns_answer_record == NULL)
741 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200742
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200743 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100744 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200745
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200746 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100747 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200748 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200749 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200750
Christopher Faulet67957bd2017-09-27 11:00:59 +0200751 /* Check if the current record dname is valid. previous_dname
752 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200753 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100754 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200755 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200756 /* First record, means a mismatch issue between
757 * queried dname and dname found in the first
758 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200759 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200760 }
761 else {
762 /* If not the first record, this means we have a
763 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200765 }
766
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767 }
768
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200769 memcpy(dns_answer_record->name, tmpname, len);
770 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200771
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200772 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200773 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100774 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200775 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200776 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200777
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200778 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200779 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100780 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200781 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200782 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200783 dns_answer_record->type = reader[0] * 256 + reader[1];
784 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200785
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200786 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200787 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100788 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200789 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200790 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200791 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200792 reader += 2;
793
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200794 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200795 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100796 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200797 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200798 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200799 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
800 + reader[2] * 256 + reader[3];
801 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200802
Christopher Faulet67957bd2017-09-27 11:00:59 +0200803 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200804 if (reader + 2 > bufend) {
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->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200809
Christopher Faulet67957bd2017-09-27 11:00:59 +0200810 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200811 reader += 2;
812
Remi Gacogneefbbdf72018-12-05 17:56:29 +0100813 if (reader + dns_answer_record->data_len >= bufend) {
814 pool_free(dns_answer_item_pool, dns_answer_record);
815 return DNS_RESP_INVALID;
816 }
817
Christopher Faulet67957bd2017-09-27 11:00:59 +0200818 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200819 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200820 case DNS_RTYPE_A:
821 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200822 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100823 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200824 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200825 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200826 dns_answer_record->address.sa_family = AF_INET;
827 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
828 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200829 break;
830
831 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200832 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200833 * no IP could be found and last record was a CNAME. Could be triggered
834 * by a wrong query type
835 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200836 * + 1 because dns_answer_record_id starts at 0
837 * while number of answers is an integer and
838 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200839 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200840 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100841 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200842 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200843 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200844
845 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100846 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200847 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100848 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200849 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200850 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200851
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200852 memcpy(dns_answer_record->target, tmpname, len);
853 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200854 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200855 break;
856
Olivier Houchard8da5f982017-08-04 18:35:36 +0200857
858 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200859 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200860 * - 2 bytes for the priority
861 * - 2 bytes for the weight
862 * - 2 bytes for the port
863 * - the target hostname
864 */
865 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100866 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200867 return DNS_RESP_INVALID;
868 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200869 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200870 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200871 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200872 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200873 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200874 reader += sizeof(uint16_t);
875 offset = 0;
Remi Gacogne58df5ae2018-12-05 17:52:54 +0100876 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200877 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100878 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200879 return DNS_RESP_INVALID;
880 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200881 dns_answer_record->data_len = len;
882 memcpy(dns_answer_record->target, tmpname, len);
883 dns_answer_record->target[len] = 0;
884 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200885
Baptiste Assmann325137d2015-04-13 23:40:55 +0200886 case DNS_RTYPE_AAAA:
887 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200888 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100889 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200890 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200891 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200892 dns_answer_record->address.sa_family = AF_INET6;
893 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
894 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200895 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200896
Baptiste Assmann325137d2015-04-13 23:40:55 +0200897 } /* switch (record type) */
898
Christopher Faulet67957bd2017-09-27 11:00:59 +0200899 /* Increment the counter for number of records saved into our
900 * local response */
901 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200902
Christopher Faulet67957bd2017-09-27 11:00:59 +0200903 /* Move forward dns_answer_record->data_len for analyzing next
904 * record in the response */
905 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
906 ? offset
907 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200908
909 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200910 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200911 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
912 if (tmp_record->type != dns_answer_record->type)
913 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200914
915 switch(tmp_record->type) {
916 case DNS_RTYPE_A:
917 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
918 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
919 sizeof(in_addr_t)))
920 found = 1;
921 break;
922
923 case DNS_RTYPE_AAAA:
924 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
925 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
926 sizeof(struct in6_addr)))
927 found = 1;
928 break;
929
Olivier Houchard8da5f982017-08-04 18:35:36 +0200930 case DNS_RTYPE_SRV:
931 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200932 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200933 dns_answer_record->port == tmp_record->port) {
934 tmp_record->weight = dns_answer_record->weight;
935 found = 1;
936 }
937 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200938
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200939 default:
940 break;
941 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200942
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200943 if (found == 1)
944 break;
945 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200946
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200947 if (found == 1) {
948 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100949 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200950 }
951 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200952 dns_answer_record->last_seen = now.tv_sec;
953 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
954 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200955 } /* for i 0 to ancount */
956
Christopher Faulet67957bd2017-09-27 11:00:59 +0200957 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200958 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200959 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200960 return DNS_RESP_VALID;
961}
962
Christopher Faulet67957bd2017-09-27 11:00:59 +0200963/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200964 * If existing IP not found, return the first IP matching family_priority,
965 * otherwise, first ip found
966 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200967 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200968 * For both cases above, dns_validate_dns_response is required
969 * returns one of the DNS_UPD_* code
970 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200971int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200972 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100973 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200974 void **newip, short *newip_sin_family,
975 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200976{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200977 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100978 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200979 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200980 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100981 int currentip_sel;
982 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100983 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200984 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200985
Christopher Faulet67957bd2017-09-27 11:00:59 +0200986 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200987 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200988 *newip = newip4 = newip6 = NULL;
989 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200990 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200991 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200992
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100993 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -0800994 * Top priority is the preferred network ip version,
995 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100996 * the last priority is the currently used IP,
997 *
998 * For these three priorities, a score is calculated. The
999 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -08001000 * 8 - preferred ip version.
1001 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +01001002 * 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 +01001003 * 1 - current ip.
1004 * The result with the biggest score is returned.
1005 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001006
1007 list_for_each_entry(record, &dns_p->answer_list, list) {
1008 void *ip;
1009 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001010
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001011 if (record->type == DNS_RTYPE_A) {
1012 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1013 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001014 }
1015 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001016 ip_type = AF_INET6;
1017 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001018 }
1019 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001020 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001021 score = 0;
1022
Joseph Herlant42cf6392018-11-15 10:33:28 -08001023 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001024 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001025 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001026
Joseph Herlant42cf6392018-11-15 10:33:28 -08001027 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001028 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001029
1030 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001031 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001032 continue;
1033
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001034 if ((ip_type == AF_INET &&
1035 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001036 &dns_opts->pref_net[j].mask.in4,
1037 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001038 (ip_type == AF_INET6 &&
1039 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001040 &dns_opts->pref_net[j].mask.in6,
1041 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001042 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001043 break;
1044 }
1045 }
1046
Christopher Faulet67957bd2017-09-27 11:00:59 +02001047 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001048 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001049 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001050 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001051 if (!allowed_duplicated_ip) {
1052 continue;
1053 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001054 } else {
1055 score += 2;
1056 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001057
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001058 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001059 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001060 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001061 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001062 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001063 !memcmp(ip, currentip, 16)))) {
1064 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001065 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001066 }
1067 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001068 currentip_sel = 0;
1069
1070 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001071 * score. The maximum score is 15, if this value is reached, we
1072 * break the parsing. Implicitly, this score is reached the ip
1073 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001074 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001075 if (ip_type == AF_INET)
1076 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001077 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001078 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001079 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001080 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001081 return DNS_UPD_NO;
1082 max_score = score;
1083 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001084 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001085
Christopher Faulet67957bd2017-09-27 11:00:59 +02001086 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001087 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001088 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001089
Christopher Faulet67957bd2017-09-27 11:00:59 +02001090 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001091 if (family_priority == AF_INET) {
1092 if (newip4) {
1093 *newip = newip4;
1094 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001095 }
1096 else if (newip6) {
1097 *newip = newip6;
1098 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001099 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100 if (!currentip_found)
1101 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001102 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001103 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001104 else if (family_priority == AF_INET6) {
1105 if (newip6) {
1106 *newip = newip6;
1107 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001108 }
1109 else if (newip4) {
1110 *newip = newip4;
1111 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001113 if (!currentip_found)
1114 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001115 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001116 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001117 else if (family_priority == AF_UNSPEC) {
1118 if (newip6) {
1119 *newip = newip6;
1120 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001121 }
1122 else if (newip4) {
1123 *newip = newip4;
1124 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001125 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001126 if (!currentip_found)
1127 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001128 }
1129
Christopher Faulet67957bd2017-09-27 11:00:59 +02001130 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001131 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001132
Christopher Faulet67957bd2017-09-27 11:00:59 +02001133 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001134 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001135 /* Move the first record to the end of the list, for internal
1136 * round robin */
1137 LIST_DEL(&record->list);
1138 LIST_ADDQ(&dns_p->answer_list, &record->list);
1139 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001140 }
1141 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001142}
1143
Christopher Faulet67957bd2017-09-27 11:00:59 +02001144/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001145 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001146 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1147 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001148 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001149 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1150 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001151 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001152int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001153{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001154 char *ptr;
1155 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001156
Christopher Faulet67957bd2017-09-27 11:00:59 +02001157 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001158 return -1;
1159
Christopher Faulet67957bd2017-09-27 11:00:59 +02001160 ptr = str;
1161 for (i = 0; i < dn_len-1; ++i) {
1162 sz = dn[i];
1163 if (i)
1164 *ptr++ = '.';
1165 memcpy(ptr, dn+i+1, sz);
1166 ptr += sz;
1167 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001168 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001169 *ptr++ = '\0';
1170 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001171}
1172
Christopher Faulet67957bd2017-09-27 11:00:59 +02001173/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1174 *
1175 * <str> must be a null-terminated string. <str_len> must include the
1176 * terminating null byte. <dn> buffer must be allocated and its size must be
1177 * passed in <dn_len>.
1178 *
1179 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1180 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001181 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001182int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001183{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001184 int i, offset;
1185
Christopher Faulet67957bd2017-09-27 11:00:59 +02001186 if (dn_len < str_len + 1)
1187 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001188
Christopher Faulet67957bd2017-09-27 11:00:59 +02001189 /* First byte of dn will be used to store the length of the first
1190 * label */
1191 offset = 0;
1192 for (i = 0; i < str_len; ++i) {
1193 if (str[i] == '.') {
1194 /* 2 or more consecutive dots is invalid */
1195 if (i == offset)
1196 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001197
Christopher Faulet67957bd2017-09-27 11:00:59 +02001198 dn[offset] = (i - offset);
1199 offset = i+1;
1200 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001201 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001202 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001203 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001204 dn[offset] = (i - offset - 1);
1205 dn[i] = '\0';
1206 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001207}
1208
Christopher Faulet67957bd2017-09-27 11:00:59 +02001209/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001210 * - total size
1211 * - each label size individually
1212 * returns:
1213 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1214 * 1 when no error. <err> is left unaffected.
1215 */
1216int dns_hostname_validation(const char *string, char **err)
1217{
1218 const char *c, *d;
1219 int i;
1220
1221 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1222 if (err)
1223 *err = DNS_TOO_LONG_FQDN;
1224 return 0;
1225 }
1226
1227 c = string;
1228 while (*c) {
1229 d = c;
1230
1231 i = 0;
1232 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1233 i++;
1234 if (!((*d == '-') || (*d == '_') ||
1235 ((*d >= 'a') && (*d <= 'z')) ||
1236 ((*d >= 'A') && (*d <= 'Z')) ||
1237 ((*d >= '0') && (*d <= '9')))) {
1238 if (err)
1239 *err = DNS_INVALID_CHARACTER;
1240 return 0;
1241 }
1242 d++;
1243 }
1244
1245 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1246 if (err)
1247 *err = DNS_LABEL_TOO_LONG;
1248 return 0;
1249 }
1250
1251 if (*d == '\0')
1252 goto out;
1253
1254 c = ++d;
1255 }
1256 out:
1257 return 1;
1258}
1259
Christopher Faulet67957bd2017-09-27 11:00:59 +02001260/* Picks up an available resolution from the different resolution list
1261 * associated to a resolvers section, in this order:
1262 * 1. check in resolutions.curr for the same hostname and query_type
1263 * 2. check in resolutions.wait for the same hostname and query_type
1264 * 3. Get a new resolution from resolution pool
1265 *
1266 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001267 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001268static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1269 char **hostname_dn, int hostname_dn_len,
1270 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001271{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001272 struct dns_resolution *res;
1273
1274 if (!*hostname_dn)
1275 goto from_pool;
1276
1277 /* Search for same hostname and query type in resolutions.curr */
1278 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1279 if (!res->hostname_dn)
1280 continue;
1281 if ((query_type == res->prefered_query_type) &&
1282 hostname_dn_len == res->hostname_dn_len &&
1283 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1284 return res;
1285 }
1286
1287 /* Search for same hostname and query type in resolutions.wait */
1288 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1289 if (!res->hostname_dn)
1290 continue;
1291 if ((query_type == res->prefered_query_type) &&
1292 hostname_dn_len == res->hostname_dn_len &&
1293 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1294 return res;
1295 }
1296
1297 from_pool:
1298 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001299 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001300 if (res) {
1301 memset(res, 0, sizeof(*res));
1302 res->resolvers = resolvers;
1303 res->uuid = resolution_uuid;
1304 res->status = RSLV_STATUS_NONE;
1305 res->step = RSLV_STEP_NONE;
1306 res->last_valid = now_ms;
1307
1308 LIST_INIT(&res->requesters);
1309 LIST_INIT(&res->response.answer_list);
1310
1311 res->prefered_query_type = query_type;
1312 res->query_type = query_type;
1313 res->hostname_dn = *hostname_dn;
1314 res->hostname_dn_len = hostname_dn_len;
1315
1316 ++resolution_uuid;
1317
1318 /* Move the resolution to the resolvers wait queue */
1319 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1320 }
1321 return res;
1322}
1323
1324/* Releases a resolution from its requester(s) and move it back to the pool */
1325static void dns_free_resolution(struct dns_resolution *resolution)
1326{
1327 struct dns_requester *req, *reqback;
1328
1329 /* clean up configuration */
1330 dns_reset_resolution(resolution);
1331 resolution->hostname_dn = NULL;
1332 resolution->hostname_dn_len = 0;
1333
1334 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1335 LIST_DEL(&req->list);
1336 req->resolution = NULL;
1337 }
1338
1339 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001340 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001341}
1342
1343/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1344 * on success, -1 otherwise.
1345 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001346int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001347{
1348 struct dns_resolution *res = NULL;
1349 struct dns_requester *req;
1350 struct dns_resolvers *resolvers;
1351 struct server *srv = NULL;
1352 struct dns_srvrq *srvrq = NULL;
1353 char **hostname_dn;
1354 int hostname_dn_len, query_type;
1355
1356 switch (requester_type) {
1357 case OBJ_TYPE_SERVER:
1358 srv = (struct server *)requester;
1359 hostname_dn = &srv->hostname_dn;
1360 hostname_dn_len = srv->hostname_dn_len;
1361 resolvers = srv->resolvers;
1362 query_type = ((srv->dns_opts.family_prio == AF_INET)
1363 ? DNS_RTYPE_A
1364 : DNS_RTYPE_AAAA);
1365 break;
1366
1367 case OBJ_TYPE_SRVRQ:
1368 srvrq = (struct dns_srvrq *)requester;
1369 hostname_dn = &srvrq->hostname_dn;
1370 hostname_dn_len = srvrq->hostname_dn_len;
1371 resolvers = srvrq->resolvers;
1372 query_type = DNS_RTYPE_SRV;
1373 break;
1374
1375 default:
1376 goto err;
1377 }
1378
1379 /* Get a resolution from the resolvers' wait queue or pool */
1380 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1381 goto err;
1382
1383 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001384 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001385 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001386 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001387 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001388 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001389 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001390 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001391 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001392 req->owner = &srv->obj_type;
1393 srv->dns_requester = req;
1394 }
1395 else
1396 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001397 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001398 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001399 }
1400 else if (srvrq) {
1401 if (srvrq->dns_requester == NULL) {
1402 if ((req = calloc(1, sizeof(*req))) == NULL)
1403 goto err;
1404 req->owner = &srvrq->obj_type;
1405 srvrq->dns_requester = req;
1406 }
1407 else
1408 req = srvrq->dns_requester;
1409 }
1410 else
1411 goto err;
1412
1413 req->resolution = res;
1414 req->requester_cb = snr_resolution_cb;
1415 req->requester_error_cb = snr_resolution_error_cb;
1416
1417 LIST_ADDQ(&res->requesters, &req->list);
1418 return 0;
1419
1420 err:
1421 if (res && LIST_ISEMPTY(&res->requesters))
1422 dns_free_resolution(res);
1423 return -1;
1424}
1425
1426/* Removes a requester from a DNS resoltion. It takes takes care of all the
1427 * consequences. It also cleans up some parameters from the requester.
1428 */
1429void dns_unlink_resolution(struct dns_requester *requester)
1430{
1431 struct dns_resolution *res;
1432 struct dns_requester *req;
1433
1434 /* Nothing to do */
1435 if (!requester || !requester->resolution)
1436 return;
1437 res = requester->resolution;
1438
1439 /* Clean up the requester */
1440 LIST_DEL(&requester->list);
1441 requester->resolution = NULL;
1442
1443 /* We need to find another requester linked on this resolution */
1444 if (!LIST_ISEMPTY(&res->requesters))
1445 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1446 else {
1447 dns_free_resolution(res);
1448 return;
1449 }
1450
1451 /* Move hostname_dn related pointers to the next requester */
1452 switch (obj_type(req->owner)) {
1453 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001454 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1455 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001456 break;
1457 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001458 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1459 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001460 break;
1461 default:
1462 res->hostname_dn = NULL;
1463 res->hostname_dn_len = 0;
1464 break;
1465 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001466}
1467
Christopher Faulet67957bd2017-09-27 11:00:59 +02001468/* Called when a network IO is generated on a name server socket for an incoming
1469 * packet. It performs the following actions:
1470 * - check if the packet requires processing (not outdated resolution)
1471 * - ensure the DNS packet received is valid and call requester's callback
1472 * - call requester's error callback if invalid response
1473 * - check the dn_name in the packet against the one sent
1474 */
1475static void dns_resolve_recv(struct dgram_conn *dgram)
1476{
1477 struct dns_nameserver *ns, *tmpns;
1478 struct dns_resolvers *resolvers;
1479 struct dns_resolution *res;
1480 struct dns_query_item *query;
1481 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1482 unsigned char *bufend;
1483 int fd, buflen, dns_resp;
1484 int max_answer_records;
1485 unsigned short query_id;
1486 struct eb32_node *eb;
1487 struct dns_requester *req;
1488
1489 fd = dgram->t.sock.fd;
1490
1491 /* check if ready for reading */
1492 if (!fd_recv_ready(fd))
1493 return;
1494
1495 /* no need to go further if we can't retrieve the nameserver */
1496 if ((ns = dgram->owner) == NULL)
1497 return;
1498
1499 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001500 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001501
1502 /* process all pending input messages */
1503 while (1) {
1504 /* read message received */
1505 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1506 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1507 /* FIXME : for now we consider EAGAIN only */
1508 fd_cant_recv(fd);
1509 break;
1510 }
1511
1512 /* message too big */
1513 if (buflen > resolvers->accepted_payload_size) {
1514 ns->counters.too_big++;
1515 continue;
1516 }
1517
1518 /* initializing variables */
1519 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1520
1521 /* read the query id from the packet (16 bits) */
1522 if (buf + 2 > bufend) {
1523 ns->counters.invalid++;
1524 continue;
1525 }
1526 query_id = dns_response_get_query_id(buf);
1527
1528 /* search the query_id in the pending resolution tree */
1529 eb = eb32_lookup(&resolvers->query_ids, query_id);
1530 if (eb == NULL) {
1531 /* unknown query id means an outdated response and can be safely ignored */
1532 ns->counters.outdated++;
1533 continue;
1534 }
1535
1536 /* known query id means a resolution in prgress */
1537 res = eb32_entry(eb, struct dns_resolution, qid);
1538 if (!res) {
1539 ns->counters.outdated++;
1540 continue;
1541 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001542
Christopher Faulet67957bd2017-09-27 11:00:59 +02001543 /* number of responses received */
1544 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001545
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1547 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001548
Christopher Faulet67957bd2017-09-27 11:00:59 +02001549 switch (dns_resp) {
1550 case DNS_RESP_VALID:
1551 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001552
Christopher Faulet67957bd2017-09-27 11:00:59 +02001553 case DNS_RESP_INVALID:
1554 case DNS_RESP_QUERY_COUNT_ERROR:
1555 case DNS_RESP_WRONG_NAME:
1556 res->status = RSLV_STATUS_INVALID;
1557 ns->counters.invalid++;
1558 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001559
Christopher Faulet67957bd2017-09-27 11:00:59 +02001560 case DNS_RESP_NX_DOMAIN:
1561 res->status = RSLV_STATUS_NX;
1562 ns->counters.nx++;
1563 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001564
Christopher Faulet67957bd2017-09-27 11:00:59 +02001565 case DNS_RESP_REFUSED:
1566 res->status = RSLV_STATUS_REFUSED;
1567 ns->counters.refused++;
1568 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001569
Christopher Faulet67957bd2017-09-27 11:00:59 +02001570 case DNS_RESP_ANCOUNT_ZERO:
1571 res->status = RSLV_STATUS_OTHER;
1572 ns->counters.any_err++;
1573 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001574
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575 case DNS_RESP_CNAME_ERROR:
1576 res->status = RSLV_STATUS_OTHER;
1577 ns->counters.cname_error++;
1578 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001579
Christopher Faulet67957bd2017-09-27 11:00:59 +02001580 case DNS_RESP_TRUNCATED:
1581 res->status = RSLV_STATUS_OTHER;
1582 ns->counters.truncated++;
1583 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001584
Christopher Faulet67957bd2017-09-27 11:00:59 +02001585 case DNS_RESP_NO_EXPECTED_RECORD:
1586 case DNS_RESP_ERROR:
1587 case DNS_RESP_INTERNAL:
1588 res->status = RSLV_STATUS_OTHER;
1589 ns->counters.other++;
1590 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001591 }
1592
Christopher Faulet67957bd2017-09-27 11:00:59 +02001593 /* Wait all nameservers response to handle errors */
1594 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1595 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001596
Christopher Faulet67957bd2017-09-27 11:00:59 +02001597 /* Process error codes */
1598 if (dns_resp != DNS_RESP_VALID) {
1599 if (res->prefered_query_type != res->query_type) {
1600 /* The fallback on the query type was already performed,
1601 * so check the try counter. If it falls to 0, we can
1602 * report an error. Else, wait the next attempt. */
1603 if (!res->try)
1604 goto report_res_error;
1605 }
1606 else {
1607 /* Fallback from A to AAAA or the opposite and re-send
1608 * the resolution immediately. try counter is not
1609 * decremented. */
1610 if (res->prefered_query_type == DNS_RTYPE_A) {
1611 res->query_type = DNS_RTYPE_AAAA;
1612 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001613 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001614 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1615 res->query_type = DNS_RTYPE_A;
1616 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001617 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001618 }
1619 continue;
1620 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001621
Christopher Faulet67957bd2017-09-27 11:00:59 +02001622 /* Now let's check the query's dname corresponds to the one we
1623 * sent. We can check only the first query of the list. We send
1624 * one query at a time so we get one query in the response */
1625 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1626 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1627 dns_resp = DNS_RESP_WRONG_NAME;
1628 ns->counters.other++;
1629 goto report_res_error;
1630 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001631
Christopher Faulet67957bd2017-09-27 11:00:59 +02001632 /* So the resolution succeeded */
1633 res->status = RSLV_STATUS_VALID;
1634 res->last_valid = now_ms;
1635 ns->counters.valid++;
1636 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001637
Christopher Faulet67957bd2017-09-27 11:00:59 +02001638 report_res_error:
1639 list_for_each_entry(req, &res->requesters, list)
1640 req->requester_error_cb(req, dns_resp);
1641 dns_reset_resolution(res);
1642 LIST_DEL(&res->list);
1643 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1644 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001645
Christopher Faulet67957bd2017-09-27 11:00:59 +02001646 report_res_success:
1647 /* Only the 1rst requester s managed by the server, others are
1648 * from the cache */
1649 tmpns = ns;
1650 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001651 struct server *s = objt_server(req->owner);
1652
1653 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001654 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001655 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001656 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001657 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001659 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001660
Christopher Faulet67957bd2017-09-27 11:00:59 +02001661 dns_reset_resolution(res);
1662 LIST_DEL(&res->list);
1663 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1664 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001665 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001666 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001667 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001668}
William Lallemand69e96442016-11-19 00:58:54 +01001669
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670/* Called when a resolvers network socket is ready to send data */
1671static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001672{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 struct dns_resolvers *resolvers;
1674 struct dns_nameserver *ns;
1675 struct dns_resolution *res;
1676 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001677
Christopher Faulet67957bd2017-09-27 11:00:59 +02001678 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001679
Christopher Faulet67957bd2017-09-27 11:00:59 +02001680 /* check if ready for sending */
1681 if (!fd_send_ready(fd))
1682 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001683
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684 /* we don't want/need to be waked up any more for sending */
1685 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001686
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687 /* no need to go further if we can't retrieve the nameserver */
1688 if ((ns = dgram->owner) == NULL)
1689 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001690
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001692 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001693
Christopher Faulet67957bd2017-09-27 11:00:59 +02001694 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001695 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001696
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697 if (res->nb_queries == resolvers->nb_nameservers)
1698 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001699
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001700 len = dns_build_query(res->query_id, res->query_type,
1701 resolvers->accepted_payload_size,
1702 res->hostname_dn, res->hostname_dn_len,
1703 trash.area, trash.size);
1704 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001705 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001706
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001707 ret = send(fd, trash.area, len, 0);
1708 if (ret != len)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001709 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001710
Christopher Faulet67957bd2017-09-27 11:00:59 +02001711 ns->counters.sent++;
1712 res->nb_queries++;
1713 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001714
Christopher Faulet67957bd2017-09-27 11:00:59 +02001715 snd_error:
1716 ns->counters.snd_error++;
1717 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001718 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001719 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001720}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001721
Christopher Faulet67957bd2017-09-27 11:00:59 +02001722/* Processes DNS resolution. First, it checks the active list to detect expired
1723 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1724 * checks the wait list to trigger new resolutions.
1725 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001726static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001727{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001728 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001729 struct dns_resolution *res, *resback;
1730 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001731
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001732 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001733
Christopher Faulet67957bd2017-09-27 11:00:59 +02001734 /* Handle all expired resolutions from the active list */
1735 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1736 /* When we find the first resolution in the future, then we can
1737 * stop here */
1738 exp = tick_add(res->last_query, resolvers->timeout.retry);
1739 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001740 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001741
Christopher Faulet67957bd2017-09-27 11:00:59 +02001742 /* If current resolution has been tried too many times and
1743 * finishes in timeout we update its status and remove it from
1744 * the list */
1745 if (!res->try) {
1746 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001747
Christopher Faulet67957bd2017-09-27 11:00:59 +02001748 /* Notify the result to the requesters */
1749 if (!res->nb_responses)
1750 res->status = RSLV_STATUS_TIMEOUT;
1751 list_for_each_entry(req, &res->requesters, list)
1752 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001753
Christopher Faulet67957bd2017-09-27 11:00:59 +02001754 /* Clean up resolution info and remove it from the
1755 * current list */
1756 dns_reset_resolution(res);
1757 LIST_DEL(&res->list);
1758 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001759 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001760 else {
1761 /* Otherwise resend the DNS query and requeue the resolution */
1762 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1763 /* No response received (a real timeout) or fallback already done */
1764 res->query_type = res->prefered_query_type;
1765 res->try--;
1766 }
1767 else {
1768 /* Fallback from A to AAAA or the opposite and re-send
1769 * the resolution immediately. try counter is not
1770 * decremented. */
1771 if (res->prefered_query_type == DNS_RTYPE_A)
1772 res->query_type = DNS_RTYPE_AAAA;
1773 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1774 res->query_type = DNS_RTYPE_A;
1775 else
1776 res->try--;
1777 }
1778 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001779 }
1780 }
William Lallemand69e96442016-11-19 00:58:54 +01001781
Christopher Faulet67957bd2017-09-27 11:00:59 +02001782 /* Handle all resolutions in the wait list */
1783 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1784 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1785 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1786 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001787
Christopher Faulet67957bd2017-09-27 11:00:59 +02001788 if (dns_run_resolution(res) != 1) {
1789 res->last_resolution = now_ms;
1790 LIST_DEL(&res->list);
1791 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001792 }
1793 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001794
Christopher Faulet67957bd2017-09-27 11:00:59 +02001795 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001796 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001797 return t;
1798}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001799
Christopher Faulet67957bd2017-09-27 11:00:59 +02001800/* proto_udp callback functions for a DNS resolution */
1801struct dgram_data_cb resolve_dgram_cb = {
1802 .recv = dns_resolve_recv,
1803 .send = dns_resolve_send,
1804};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001805
Christopher Faulet67957bd2017-09-27 11:00:59 +02001806/* Release memory allocated by DNS */
1807static void dns_deinit(void)
1808{
1809 struct dns_resolvers *resolvers, *resolversback;
1810 struct dns_nameserver *ns, *nsback;
1811 struct dns_resolution *res, *resback;
1812 struct dns_requester *req, *reqback;
1813 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001814
Christopher Faulet67957bd2017-09-27 11:00:59 +02001815 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1816 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1817 free(ns->id);
1818 free((char *)ns->conf.file);
1819 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1820 fd_delete(ns->dgram->t.sock.fd);
1821 free(ns->dgram);
1822 LIST_DEL(&ns->list);
1823 free(ns);
1824 }
1825
1826 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1827 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1828 LIST_DEL(&req->list);
1829 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001830 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001831 dns_free_resolution(res);
1832 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001833
Christopher Faulet67957bd2017-09-27 11:00:59 +02001834 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1835 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1836 LIST_DEL(&req->list);
1837 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001838 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001839 dns_free_resolution(res);
1840 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001841
Christopher Faulet67957bd2017-09-27 11:00:59 +02001842 free(resolvers->id);
1843 free((char *)resolvers->conf.file);
1844 task_delete(resolvers->t);
1845 task_free(resolvers->t);
1846 LIST_DEL(&resolvers->list);
1847 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001848 }
1849
Christopher Faulet67957bd2017-09-27 11:00:59 +02001850 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1851 free(srvrq->name);
1852 free(srvrq->hostname_dn);
1853 LIST_DEL(&srvrq->list);
1854 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001855 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001856}
1857
Christopher Faulet67957bd2017-09-27 11:00:59 +02001858/* Finalizes the DNS configuration by allocating required resources and checking
1859 * live parameters.
1860 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001861 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001862static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001863{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001864 struct dns_resolvers *resolvers;
1865 struct proxy *px;
1866 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001867
Christopher Faulet67957bd2017-09-27 11:00:59 +02001868 /* allocate pool of resolution per resolvers */
1869 list_for_each_entry(resolvers, &dns_resolvers, list) {
1870 struct dns_nameserver *ns;
1871 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001872
Christopher Faulet67957bd2017-09-27 11:00:59 +02001873 /* Check if we can create the socket with nameservers info */
1874 list_for_each_entry(ns, &resolvers->nameservers, list) {
1875 struct dgram_conn *dgram = NULL;
1876 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001877
Christopher Faulet67957bd2017-09-27 11:00:59 +02001878 /* Check nameserver info */
1879 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001880 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1881 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001882 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001883 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001884 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001885 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001886 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1887 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001888 close(fd);
1889 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001890 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001891 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001892 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001893
Christopher Faulet67957bd2017-09-27 11:00:59 +02001894 /* Create dgram structure that will hold the UPD socket
1895 * and attach it on the current nameserver */
1896 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001897 ha_alert("config: resolvers '%s' : out of memory.\n",
1898 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001899 err_code |= (ERR_ALERT|ERR_ABORT);
1900 goto err;
1901 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001902
Christopher Faulet67957bd2017-09-27 11:00:59 +02001903 /* Leave dgram partially initialized, no FD attached for
1904 * now. */
1905 dgram->owner = ns;
1906 dgram->data = &resolve_dgram_cb;
1907 dgram->t.sock.fd = -1;
1908 ns->dgram = dgram;
1909 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001910
Christopher Faulet67957bd2017-09-27 11:00:59 +02001911 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001912 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001913 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001914 err_code |= (ERR_ALERT|ERR_ABORT);
1915 goto err;
1916 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001917
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 /* Update task's parameters */
1919 t->process = dns_process_resolvers;
1920 t->context = resolvers;
1921 resolvers->t = t;
1922 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001923 }
1924
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001925 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001926 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001927
Christopher Faulet67957bd2017-09-27 11:00:59 +02001928 for (srv = px->srv; srv; srv = srv->next) {
1929 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001930
Christopher Faulet67957bd2017-09-27 11:00:59 +02001931 if (!srv->resolvers_id)
1932 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001933
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001935 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1936 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001937 err_code |= (ERR_ALERT|ERR_ABORT);
1938 continue;
1939 }
1940 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001941
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 if (srv->srvrq && !srv->srvrq->resolvers) {
1943 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001944 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001945 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1946 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001947 err_code |= (ERR_ALERT|ERR_ABORT);
1948 continue;
1949 }
1950 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001951 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001952 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1953 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001954 err_code |= (ERR_ALERT|ERR_ABORT);
1955 continue;
1956 }
1957 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001958 }
1959
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 if (err_code & (ERR_ALERT|ERR_ABORT))
1961 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001962
Christopher Faulet67957bd2017-09-27 11:00:59 +02001963 return err_code;
1964 err:
1965 dns_deinit();
1966 return err_code;
1967
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001968}
1969
Christopher Faulet67957bd2017-09-27 11:00:59 +02001970/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001971static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001972{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001973 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001974
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 if (*args[2]) {
1976 list_for_each_entry(presolvers, &dns_resolvers, list) {
1977 if (strcmp(presolvers->id, args[2]) == 0) {
1978 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001979 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001981 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001982 if (appctx->ctx.cli.p0 == NULL) {
1983 appctx->ctx.cli.severity = LOG_ERR;
1984 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1985 appctx->st0 = CLI_ST_PRINT;
1986 return 1;
1987 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001988 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001989 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001990}
1991
Christopher Faulet67957bd2017-09-27 11:00:59 +02001992/* Dumps counters from all resolvers section and associated name servers. It
1993 * returns 0 if the output buffer is full and it needs to be called again,
1994 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001995 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001996 */
1997static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1998{
1999 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002000 struct dns_resolvers *resolvers;
2001 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01002002
2003 chunk_reset(&trash);
2004
2005 switch (appctx->st2) {
2006 case STAT_ST_INIT:
2007 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2008 /* fall through */
2009
2010 case STAT_ST_LIST:
2011 if (LIST_ISEMPTY(&dns_resolvers)) {
2012 chunk_appendf(&trash, "No resolvers found\n");
2013 }
2014 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002015 list_for_each_entry(resolvers, &dns_resolvers, list) {
2016 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002017 continue;
2018
Christopher Faulet67957bd2017-09-27 11:00:59 +02002019 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2020 list_for_each_entry(ns, &resolvers->nameservers, list) {
2021 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2022 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2023 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2024 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2025 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2026 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2027 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2028 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2029 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2030 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2031 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2032 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2033 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2034 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2035 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2036 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002037 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002038 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002039 }
2040 }
2041
2042 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002043 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002044 /* let's try again later from this session. We add ourselves into
2045 * this session's users so that it can remove us upon termination.
2046 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002047 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002048 return 0;
2049 }
William Lallemand69e96442016-11-19 00:58:54 +01002050 /* fall through */
2051
2052 default:
2053 appctx->st2 = STAT_ST_FIN;
2054 return 1;
2055 }
2056}
2057
2058/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002059static struct cli_kw_list cli_kws = {{ }, {
2060 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2061 " associated name servers",
2062 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2063 {{},}
2064 }
2065};
William Lallemand69e96442016-11-19 00:58:54 +01002066
Willy Tarreau0108d902018-11-25 19:14:37 +01002067INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002068
Willy Tarreau172f5ce2018-11-26 11:21:50 +01002069REGISTER_POST_DEINIT(dns_deinit);
Willy Tarreaue6552512018-11-26 11:33:13 +01002070REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config);