blob: 2dfbdd58782dc430e930879fef4cfc92dd5fc739 [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 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020051static struct pool_head *dns_answer_item_pool = NULL;
52static struct pool_head *dns_resolution_pool = NULL;
53static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020054
Christopher Faulet67957bd2017-09-27 11:00:59 +020055/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
56 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020057 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020058struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059{
Christopher Faulet67957bd2017-09-27 11:00:59 +020060 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020061
Christopher Faulet67957bd2017-09-27 11:00:59 +020062 list_for_each_entry(res, &dns_resolvers, list) {
63 if (!strcmp(res->id, id))
64 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020066 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020067}
68
Christopher Faulet67957bd2017-09-27 11:00:59 +020069/* Returns a pointer on the SRV request matching the name <name> for the proxy
70 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020072struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073{
Christopher Faulet67957bd2017-09-27 11:00:59 +020074 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020075
Christopher Faulet67957bd2017-09-27 11:00:59 +020076 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
77 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
78 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020079 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020080 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020081}
82
Christopher Faulet67957bd2017-09-27 11:00:59 +020083/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
84 * NULL if an error occurred. */
85struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020086{
Christopher Faulet67957bd2017-09-27 11:00:59 +020087 struct proxy *px = srv->proxy;
88 struct dns_srvrq *srvrq = NULL;
89 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020090
Christopher Faulet67957bd2017-09-27 11:00:59 +020091 fqdn_len = strlen(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020092 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area,
93 trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +020094 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010095 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
96 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020098 }
99
Christopher Faulet67957bd2017-09-27 11:00:59 +0200100 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100101 ha_alert("config : %s '%s', server '%s': out of memory\n",
102 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200103 goto err;
104 }
105 srvrq->obj_type = OBJ_TYPE_SRVRQ;
106 srvrq->proxy = px;
107 srvrq->name = strdup(fqdn);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200108 srvrq->hostname_dn = strdup(trash.area);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200109 srvrq->hostname_dn_len = hostname_dn_len;
110 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100111 ha_alert("config : %s '%s', server '%s': out of memory\n",
112 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200113 goto err;
114 }
115 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
116 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200117
Christopher Faulet67957bd2017-09-27 11:00:59 +0200118 err:
119 if (srvrq) {
120 free(srvrq->name);
121 free(srvrq->hostname_dn);
122 free(srvrq);
123 }
124 return NULL;
125}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200126
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200127
Christopher Faulet67957bd2017-09-27 11:00:59 +0200128/* 2 bytes random generator to generate DNS query ID */
129static inline uint16_t dns_rnd16(void)
130{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200131 if (!dns_query_id_seed)
132 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200133 dns_query_id_seed ^= dns_query_id_seed << 13;
134 dns_query_id_seed ^= dns_query_id_seed >> 7;
135 dns_query_id_seed ^= dns_query_id_seed << 17;
136 return dns_query_id_seed;
137}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200138
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200139
Christopher Faulet67957bd2017-09-27 11:00:59 +0200140static inline int dns_resolution_timeout(struct dns_resolution *res)
141{
142 switch (res->status) {
143 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
144 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200145 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200146}
147
Christopher Faulet67957bd2017-09-27 11:00:59 +0200148/* Updates a resolvers' task timeout for next wake up and queue it */
149static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200150{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151 struct dns_resolution *res;
152 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200153
Christopher Faulet67957bd2017-09-27 11:00:59 +0200154 next = tick_add(now_ms, resolvers->timeout.resolve);
155 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
156 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
157 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
158 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200159
Christopher Faulet67957bd2017-09-27 11:00:59 +0200160 list_for_each_entry(res, &resolvers->resolutions.wait, list)
161 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200162
Christopher Faulet67957bd2017-09-27 11:00:59 +0200163 resolvers->t->expire = next;
164 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200165}
166
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
168 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200169 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200170static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172 struct dgram_conn *dgram = ns->dgram;
173 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200174
Christopher Faulet67957bd2017-09-27 11:00:59 +0200175 /* Already connected */
176 if (dgram->t.sock.fd != -1)
177 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200178
Christopher Faulet67957bd2017-09-27 11:00:59 +0200179 /* Create an UDP socket and connect it on the nameserver's IP/Port */
180 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
181 send_log(NULL, LOG_WARNING,
182 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
183 ns->resolvers->id, ns->id);
184 return -1;
185 }
186 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
187 send_log(NULL, LOG_WARNING,
188 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
189 ns->resolvers->id, ns->id);
190 close(fd);
191 return -1;
192 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200193
Christopher Faulet67957bd2017-09-27 11:00:59 +0200194 /* Make the socket non blocking */
195 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200196
Christopher Faulet67957bd2017-09-27 11:00:59 +0200197 /* Add the fd in the fd list and update its parameters */
198 dgram->t.sock.fd = fd;
Willy Tarreaua9786b62018-01-25 07:22:13 +0100199 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200200 fd_want_recv(fd);
201 return 0;
202}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200203
Christopher Faulet67957bd2017-09-27 11:00:59 +0200204/* Forges a DNS query. It needs the following information from the caller:
205 * - <query_id> : the DNS query id corresponding to this query
206 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
207 * - <hostname_dn> : hostname in domain name format
208 * - <hostname_dn_len> : length of <hostname_dn>
209 *
210 * To store the query, the caller must pass a buffer <buf> and its size
211 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
212 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200213 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200214static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
215 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200216{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200217 struct dns_header dns_hdr;
218 struct dns_question qinfo;
219 struct dns_additional_record edns;
220 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200221
Christopher Faulet67957bd2017-09-27 11:00:59 +0200222 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
223 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200224
Christopher Faulet67957bd2017-09-27 11:00:59 +0200225 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200226
Christopher Faulet67957bd2017-09-27 11:00:59 +0200227 /* Set dns query headers */
228 dns_hdr.id = (unsigned short) htons(query_id);
229 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
230 dns_hdr.qdcount = htons(1); /* 1 question */
231 dns_hdr.ancount = 0;
232 dns_hdr.nscount = 0;
233 dns_hdr.arcount = htons(1);
234 memcpy(p, &dns_hdr, sizeof(dns_hdr));
235 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200236
Christopher Faulet67957bd2017-09-27 11:00:59 +0200237 /* Set up query hostname */
238 memcpy(p, hostname_dn, hostname_dn_len);
239 p += hostname_dn_len;
240 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200241
Christopher Faulet67957bd2017-09-27 11:00:59 +0200242 /* Set up query info (type and class) */
243 qinfo.qtype = htons(query_type);
244 qinfo.qclass = htons(DNS_RCLASS_IN);
245 memcpy(p, &qinfo, sizeof(qinfo));
246 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200247
Christopher Faulet67957bd2017-09-27 11:00:59 +0200248 /* Set the DNS extension */
249 edns.name = 0;
250 edns.type = htons(DNS_RTYPE_OPT);
251 edns.udp_payload_size = htons(accepted_payload_size);
252 edns.extension = 0;
253 edns.data_length = 0;
254 memcpy(p, &edns, sizeof(edns));
255 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200256
Christopher Faulet67957bd2017-09-27 11:00:59 +0200257 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200258}
259
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
261 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200262 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200263static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200264{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265 struct dns_resolvers *resolvers = resolution->resolvers;
266 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200267
Christopher Faulet67957bd2017-09-27 11:00:59 +0200268 list_for_each_entry(ns, &resolvers->nameservers, list) {
269 int fd = ns->dgram->t.sock.fd;
270 if (fd == -1) {
271 if (dns_connect_namesaver(ns) == -1)
272 continue;
273 fd = ns->dgram->t.sock.fd;
274 resolvers->nb_nameservers++;
275 }
276 fd_want_send(fd);
277 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200278
Christopher Faulet67957bd2017-09-27 11:00:59 +0200279 /* Update resolution */
280 resolution->nb_queries = 0;
281 resolution->nb_responses = 0;
282 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200283
Christopher Faulet67957bd2017-09-27 11:00:59 +0200284 /* Push the resolution at the end of the active list */
285 LIST_DEL(&resolution->list);
286 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
287 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200288}
289
Christopher Faulet67957bd2017-09-27 11:00:59 +0200290/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
291 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200292 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200293static int
294dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200295{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200296 struct dns_resolvers *resolvers = resolution->resolvers;
297 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200298
Christopher Faulet67957bd2017-09-27 11:00:59 +0200299 /* Avoid sending requests for resolutions that don't yet have an
300 * hostname, ie resolutions linked to servers that do not yet have an
301 * fqdn */
302 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200303 return 0;
304
Christopher Faulet67957bd2017-09-27 11:00:59 +0200305 /* Check if a resolution has already been started for this server return
306 * directly to avoid resolution pill up. */
307 if (resolution->step != RSLV_STEP_NONE)
308 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200309
Christopher Faulet67957bd2017-09-27 11:00:59 +0200310 /* Generates a new query id. We try at most 100 times to find a free
311 * query id */
312 for (i = 0; i < 100; ++i) {
313 query_id = dns_rnd16();
314 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200315 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200316 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200317 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200318 if (query_id == -1) {
319 send_log(NULL, LOG_NOTICE,
320 "could not generate a query id for %s, in resolvers %s.\n",
321 resolution->hostname_dn, resolvers->id);
322 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200323 }
324
Christopher Faulet67957bd2017-09-27 11:00:59 +0200325 /* Update resolution parameters */
326 resolution->query_id = query_id;
327 resolution->qid.key = query_id;
328 resolution->step = RSLV_STEP_RUNNING;
329 resolution->query_type = resolution->prefered_query_type;
330 resolution->try = resolvers->resolve_retries;
331 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200332
Christopher Faulet67957bd2017-09-27 11:00:59 +0200333 /* Send the DNS query */
334 resolution->try -= 1;
335 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200336 return 1;
337}
338
Christopher Faulet67957bd2017-09-27 11:00:59 +0200339/* Performs a name resolution for the requester <req> */
340void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200341{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200342 struct dns_resolvers *resolvers;
343 struct dns_resolution *res;
344 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200345
Christopher Faulet67957bd2017-09-27 11:00:59 +0200346 if (!req || !req->resolution)
347 return;
348 res = req->resolution;
349 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200350
Christopher Faulet67957bd2017-09-27 11:00:59 +0200351 /* The resolution must not be triggered yet. Use the cached response, if
352 * valid */
353 exp = tick_add(res->last_resolution, resolvers->hold.valid);
Olivier Houchardf3d9e602018-05-22 18:40:07 +0200354 if (resolvers->t && (res->status != RSLV_STATUS_VALID ||
355 !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms)))
356 task_wakeup(resolvers->t, TASK_WOKEN_OTHER);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200357}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200358
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200359
Christopher Faulet67957bd2017-09-27 11:00:59 +0200360/* Resets some resolution parameters to initial values and also delete the query
361 * ID from the resolver's tree.
362 */
363static void dns_reset_resolution(struct dns_resolution *resolution)
364{
365 /* update resolution status */
366 resolution->step = RSLV_STEP_NONE;
367 resolution->try = 0;
368 resolution->last_resolution = now_ms;
369 resolution->nb_queries = 0;
370 resolution->nb_responses = 0;
371 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200372
Christopher Faulet67957bd2017-09-27 11:00:59 +0200373 /* clean up query id */
374 eb32_delete(&resolution->qid);
375 resolution->query_id = 0;
376 resolution->qid.key = 0;
377}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200378
Christopher Faulet67957bd2017-09-27 11:00:59 +0200379/* Returns the query id contained in a DNS response */
380static inline unsigned short dns_response_get_query_id(unsigned char *resp)
381{
382 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200383}
384
Christopher Faulet67957bd2017-09-27 11:00:59 +0200385
386/* Analyses, re-builds and copies the name <name> from the DNS response packet
387 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
388 * for compressed data. The result is copied into <dest>, ensuring we don't
389 * overflow using <dest_len> Returns the number of bytes the caller can move
390 * forward. If 0 it means an error occurred while parsing the name. <offset> is
391 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200392 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200393int dns_read_name(unsigned char *buffer, unsigned char *bufend,
394 unsigned char *name, char *destination, int dest_len,
395 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200396{
397 int nb_bytes = 0, n = 0;
398 int label_len;
399 unsigned char *reader = name;
400 char *dest = destination;
401
402 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200403 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200404 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200405 /* Must point BEFORE current position */
406 if ((buffer + reader[1]) > reader)
407 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200408
Christopher Faulet67957bd2017-09-27 11:00:59 +0200409 n = dns_read_name(buffer, bufend, buffer + reader[1],
410 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200411 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200412 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200413
Christopher Faulet67957bd2017-09-27 11:00:59 +0200414 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200415 nb_bytes += n;
416 goto out;
417 }
418
419 label_len = *reader;
420 if (label_len == 0)
421 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200422
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200423 /* Check if:
424 * - we won't read outside the buffer
425 * - there is enough place in the destination
426 */
427 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200428 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200429
430 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200431 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200432
433 memcpy(dest, reader, label_len);
434
Christopher Faulet67957bd2017-09-27 11:00:59 +0200435 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200436 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200437 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200438 }
439
Christopher Faulet67957bd2017-09-27 11:00:59 +0200440 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200441 /* offset computation:
442 * parse from <name> until finding either NULL or a pointer "c0xx"
443 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200444 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200445 *offset = 0;
446 while (reader < bufend) {
447 if ((reader[0] & 0xc0) == 0xc0) {
448 *offset += 2;
449 break;
450 }
451 else if (*reader == 0) {
452 *offset += 1;
453 break;
454 }
455 *offset += 1;
456 ++reader;
457 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200458 return nb_bytes;
459
Christopher Faulet67957bd2017-09-27 11:00:59 +0200460 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200461 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200462}
463
Christopher Faulet67957bd2017-09-27 11:00:59 +0200464/* Checks for any obsolete record, also identify any SRV request, and try to
465 * find a corresponding server.
466*/
467static void dns_check_dns_response(struct dns_resolution *res)
468{
469 struct dns_resolvers *resolvers = res->resolvers;
470 struct dns_requester *req, *reqback;
471 struct dns_answer_item *item, *itemback;
472 struct server *srv;
473 struct dns_srvrq *srvrq;
474
475 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
476
477 /* Remove obsolete items */
478 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
479 if (item->type != DNS_RTYPE_SRV)
480 goto rm_obselete_item;
481
482 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
483 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
484 continue;
485
486 /* Remove any associated server */
487 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100488 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200489 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
490 item->data_len == srv->hostname_dn_len &&
491 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
492 snr_update_srv_status(srv, 1);
493 free(srv->hostname);
494 free(srv->hostname_dn);
495 srv->hostname = NULL;
496 srv->hostname_dn = NULL;
497 srv->hostname_dn_len = 0;
498 dns_unlink_resolution(srv->dns_requester);
499 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100500 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200501 }
502 }
503
504 rm_obselete_item:
505 LIST_DEL(&item->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100506 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200507 continue;
508 }
509
510 if (item->type != DNS_RTYPE_SRV)
511 continue;
512
513 /* Now process SRV records */
514 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
515 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
516 continue;
517
518 /* Check if a server already uses that hostname */
519 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100520 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200521 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
522 item->data_len == srv->hostname_dn_len &&
523 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100524 int ha_weight;
525
526 /* Make sure weight is at least 1, so
527 * that the server will be used.
528 */
529 ha_weight = item->weight / 256 + 1;
530 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200531 char weight[9];
532
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100533 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200534 server_parse_weight_change_request(srv, weight);
535 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100536 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200537 break;
538 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100539 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200540 }
541 if (srv)
542 continue;
543
544 /* If not, try to find a server with undefined hostname */
545 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100546 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200547 if (srv->srvrq == srvrq && !srv->hostname_dn)
548 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100549 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200550 }
551 /* And update this server, if found */
552 if (srv) {
553 const char *msg = NULL;
554 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100555 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200556 char hostname[DNS_MAX_NAME_SIZE];
557
558 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100559 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200561 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100562 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100563 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200564 if (msg)
565 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
566
567 srv->svc_port = item->port;
568 srv->flags &= ~SRV_F_MAPPORTS;
569 if ((srv->check.state & CHK_ST_CONFIGURED) &&
570 !(srv->flags & SRV_F_CHECKPORT))
571 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100572
573 /* Make sure weight is at least 1, so
574 * that the server will be used.
575 */
576 ha_weight = item->weight / 256 + 1;
577
578 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200579 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100580 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200581 }
582 }
583 }
584}
585
586/* Validates that the buffer DNS response provided in <resp> and finishing
587 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200588 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200589 * The result is stored in <resolution>' response, buf_response,
590 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200591 *
592 * This function returns one of the DNS_RESP_* code to indicate the type of
593 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200594 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200595static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
596 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200597{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200598 unsigned char *reader;
599 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200600 int len, flags, offset;
601 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200602 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200603 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200604 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200605 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200606 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200607
Christopher Faulet67957bd2017-09-27 11:00:59 +0200608 reader = resp;
609 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200610 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200611 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200612
Christopher Faulet67957bd2017-09-27 11:00:59 +0200613 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200614 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200615
616 /* query id */
617 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200619 dns_p->header.id = reader[0] * 256 + reader[1];
620 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200621
Christopher Faulet67957bd2017-09-27 11:00:59 +0200622 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200623 * First byte contains:
624 * - response flag (1 bit)
625 * - opcode (4 bits)
626 * - authoritative (1 bit)
627 * - truncated (1 bit)
628 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200629 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200630 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200631 return DNS_RESP_INVALID;
632
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200633 flags = reader[0] * 256 + reader[1];
634
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200635 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
636 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200637 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200638 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200639 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200640 return DNS_RESP_ERROR;
641 }
642
Christopher Faulet67957bd2017-09-27 11:00:59 +0200643 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200644 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200645
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200646 /* 2 bytes for question count */
647 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200648 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200649 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200650 /* (for now) we send one query only, so we expect only one in the
651 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200652 if (dns_p->header.qdcount != 1)
653 return DNS_RESP_QUERY_COUNT_ERROR;
654 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200655 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200656 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200657
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200658 /* 2 bytes for answer count */
659 if (reader + 2 >= bufend)
660 return DNS_RESP_INVALID;
661 dns_p->header.ancount = reader[0] * 256 + reader[1];
662 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200663 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200664 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200665 if (dns_p->header.ancount > max_answer_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 authority count */
670 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200671 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200672 dns_p->header.nscount = reader[0] * 256 + reader[1];
673 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200674
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200675 /* 2 bytes additional count */
676 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200677 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200678 dns_p->header.arcount = reader[0] * 256 + reader[1];
679 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200680
Christopher Faulet67957bd2017-09-27 11:00:59 +0200681 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200682 LIST_INIT(&dns_p->query_list);
683 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 +0200684 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200685 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200686 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200687 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
688 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200689 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200690 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200691
Christopher Faulet67957bd2017-09-27 11:00:59 +0200692 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200693 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200694 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200695 offset = 0;
696 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200697
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200698 if (len == 0)
699 return DNS_RESP_INVALID;
700
701 reader += offset;
702 previous_dname = dns_query->name;
703
704 /* move forward 2 bytes for question type */
705 if (reader + 2 >= bufend)
706 return DNS_RESP_INVALID;
707 dns_query->type = reader[0] * 256 + reader[1];
708 reader += 2;
709
710 /* move forward 2 bytes for question class */
711 if (reader + 2 >= bufend)
712 return DNS_RESP_INVALID;
713 dns_query->class = reader[0] * 256 + reader[1];
714 reader += 2;
715 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200716
Baptiste Assmann251abb92017-08-11 09:58:27 +0200717 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200718 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200719 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
720 return DNS_RESP_TRUNCATED;
721
Baptiste Assmann325137d2015-04-13 23:40:55 +0200722 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200723 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200724 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200725 if (reader >= bufend)
726 return DNS_RESP_INVALID;
727
Willy Tarreaubafbe012017-11-24 17:34:44 +0100728 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200729 if (dns_answer_record == NULL)
730 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200731
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200732 offset = 0;
733 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200734
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200735 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100736 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200737 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200738 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200739
Christopher Faulet67957bd2017-09-27 11:00:59 +0200740 /* Check if the current record dname is valid. previous_dname
741 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200742 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100743 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200744 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200745 /* First record, means a mismatch issue between
746 * queried dname and dname found in the first
747 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200748 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200749 }
750 else {
751 /* If not the first record, this means we have a
752 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200754 }
755
Baptiste Assmann325137d2015-04-13 23:40:55 +0200756 }
757
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200758 memcpy(dns_answer_record->name, tmpname, len);
759 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200760
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200761 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200762 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100763 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200764 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200765 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200766
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200767 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200768 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100769 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200770 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200771 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200772 dns_answer_record->type = reader[0] * 256 + reader[1];
773 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200774
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200775 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200776 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100777 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200778 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200779 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200780 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200781 reader += 2;
782
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200783 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200784 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100785 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200786 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200787 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200788 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
789 + reader[2] * 256 + reader[3];
790 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200791
Christopher Faulet67957bd2017-09-27 11:00:59 +0200792 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200793 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100794 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200795 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200796 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200797 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200798
Christopher Faulet67957bd2017-09-27 11:00:59 +0200799 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200800 reader += 2;
801
Christopher Faulet67957bd2017-09-27 11:00:59 +0200802 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200803 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200804 case DNS_RTYPE_A:
805 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200806 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100807 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200808 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200809 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200810 dns_answer_record->address.sa_family = AF_INET;
811 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
812 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200813 break;
814
815 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200816 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200817 * no IP could be found and last record was a CNAME. Could be triggered
818 * by a wrong query type
819 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200820 * + 1 because dns_answer_record_id starts at 0
821 * while number of answers is an integer and
822 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200823 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200824 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100825 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200826 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200827 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200828
829 offset = 0;
830 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200831 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100832 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200833 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200834 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200835
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200836 memcpy(dns_answer_record->target, tmpname, len);
837 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200838 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200839 break;
840
Olivier Houchard8da5f982017-08-04 18:35:36 +0200841
842 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200843 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200844 * - 2 bytes for the priority
845 * - 2 bytes for the weight
846 * - 2 bytes for the port
847 * - the target hostname
848 */
849 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100850 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200851 return DNS_RESP_INVALID;
852 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200853 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200854 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200855 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200856 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200857 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200858 reader += sizeof(uint16_t);
859 offset = 0;
860 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
861 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100862 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200863 return DNS_RESP_INVALID;
864 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200865 dns_answer_record->data_len = len;
866 memcpy(dns_answer_record->target, tmpname, len);
867 dns_answer_record->target[len] = 0;
868 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200869
Baptiste Assmann325137d2015-04-13 23:40:55 +0200870 case DNS_RTYPE_AAAA:
871 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200872 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100873 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200874 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200875 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200876 dns_answer_record->address.sa_family = AF_INET6;
877 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
878 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200879 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200880
Baptiste Assmann325137d2015-04-13 23:40:55 +0200881 } /* switch (record type) */
882
Christopher Faulet67957bd2017-09-27 11:00:59 +0200883 /* Increment the counter for number of records saved into our
884 * local response */
885 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200886
Christopher Faulet67957bd2017-09-27 11:00:59 +0200887 /* Move forward dns_answer_record->data_len for analyzing next
888 * record in the response */
889 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
890 ? offset
891 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200892
893 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200894 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200895 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
896 if (tmp_record->type != dns_answer_record->type)
897 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200898
899 switch(tmp_record->type) {
900 case DNS_RTYPE_A:
901 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
902 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
903 sizeof(in_addr_t)))
904 found = 1;
905 break;
906
907 case DNS_RTYPE_AAAA:
908 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
909 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
910 sizeof(struct in6_addr)))
911 found = 1;
912 break;
913
Olivier Houchard8da5f982017-08-04 18:35:36 +0200914 case DNS_RTYPE_SRV:
915 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200916 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200917 dns_answer_record->port == tmp_record->port) {
918 tmp_record->weight = dns_answer_record->weight;
919 found = 1;
920 }
921 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200922
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200923 default:
924 break;
925 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200926
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200927 if (found == 1)
928 break;
929 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200930
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200931 if (found == 1) {
932 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100933 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200934 }
935 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200936 dns_answer_record->last_seen = now.tv_sec;
937 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
938 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200939 } /* for i 0 to ancount */
940
Christopher Faulet67957bd2017-09-27 11:00:59 +0200941 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200942 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200943 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200944 return DNS_RESP_VALID;
945}
946
Christopher Faulet67957bd2017-09-27 11:00:59 +0200947/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200948 * If existing IP not found, return the first IP matching family_priority,
949 * otherwise, first ip found
950 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200951 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200952 * For both cases above, dns_validate_dns_response is required
953 * returns one of the DNS_UPD_* code
954 */
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200955int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200956 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100957 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200958 void **newip, short *newip_sin_family,
959 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200960{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200961 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100962 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200963 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200964 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100965 int currentip_sel;
966 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100967 int score, max_score;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200968 int allowed_duplicated_ip;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200969
Christopher Faulet67957bd2017-09-27 11:00:59 +0200970 family_priority = dns_opts->family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200971 allowed_duplicated_ip = dns_opts->accept_duplicate_ip;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200972 *newip = newip4 = newip6 = NULL;
973 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200974 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200975 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200976
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100977 /* Select an IP regarding configuration preference.
Joseph Herlant42cf6392018-11-15 10:33:28 -0800978 * Top priority is the preferred network ip version,
979 * second priority is the preferred network.
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100980 * the last priority is the currently used IP,
981 *
982 * For these three priorities, a score is calculated. The
983 * weight are:
Joseph Herlant42cf6392018-11-15 10:33:28 -0800984 * 8 - preferred ip version.
985 * 4 - preferred network.
Baptistefc725902016-12-26 23:21:08 +0100986 * 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 +0100987 * 1 - current ip.
988 * The result with the biggest score is returned.
989 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200990
991 list_for_each_entry(record, &dns_p->answer_list, list) {
992 void *ip;
993 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100994
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200995 if (record->type == DNS_RTYPE_A) {
996 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
997 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200998 }
999 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001000 ip_type = AF_INET6;
1001 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001002 }
1003 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001004 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001005 score = 0;
1006
Joseph Herlant42cf6392018-11-15 10:33:28 -08001007 /* Check for preferred ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001008 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001009 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001010
Joseph Herlant42cf6392018-11-15 10:33:28 -08001011 /* Check for preferred network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001012 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001013
1014 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001015 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001016 continue;
1017
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001018 if ((ip_type == AF_INET &&
1019 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001020 &dns_opts->pref_net[j].mask.in4,
1021 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001022 (ip_type == AF_INET6 &&
1023 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001024 &dns_opts->pref_net[j].mask.in6,
1025 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001026 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001027 break;
1028 }
1029 }
1030
Christopher Faulet67957bd2017-09-27 11:00:59 +02001031 /* Check if the IP found in the record is already affected to a
Baptiste Assmann84221b42018-06-22 13:03:50 +02001032 * member of a group. If not, the score should be incremented
Christopher Faulet67957bd2017-09-27 11:00:59 +02001033 * by 2. */
Baptiste Assmann84221b42018-06-22 13:03:50 +02001034 if (owner && snr_check_ip_callback(owner, ip, &ip_type)) {
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001035 if (!allowed_duplicated_ip) {
1036 continue;
1037 }
Baptiste Assmann84221b42018-06-22 13:03:50 +02001038 } else {
1039 score += 2;
1040 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001041
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001042 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001043 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001044 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001045 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001046 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001047 !memcmp(ip, currentip, 16)))) {
1048 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001049 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001050 }
1051 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001052 currentip_sel = 0;
1053
1054 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001055 * score. The maximum score is 15, if this value is reached, we
1056 * break the parsing. Implicitly, this score is reached the ip
1057 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001058 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001059 if (ip_type == AF_INET)
1060 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001061 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001062 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001063 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001064 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001065 return DNS_UPD_NO;
1066 max_score = score;
1067 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001068 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001069
Christopher Faulet67957bd2017-09-27 11:00:59 +02001070 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001071 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001072 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001073
Christopher Faulet67957bd2017-09-27 11:00:59 +02001074 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001075 if (family_priority == AF_INET) {
1076 if (newip4) {
1077 *newip = newip4;
1078 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001079 }
1080 else if (newip6) {
1081 *newip = newip6;
1082 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001083 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001084 if (!currentip_found)
1085 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001086 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001087 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001088 else if (family_priority == AF_INET6) {
1089 if (newip6) {
1090 *newip = newip6;
1091 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001092 }
1093 else if (newip4) {
1094 *newip = newip4;
1095 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001096 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001097 if (!currentip_found)
1098 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001099 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001101 else if (family_priority == AF_UNSPEC) {
1102 if (newip6) {
1103 *newip = newip6;
1104 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001105 }
1106 else if (newip4) {
1107 *newip = newip4;
1108 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001110 if (!currentip_found)
1111 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 }
1113
Christopher Faulet67957bd2017-09-27 11:00:59 +02001114 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001115 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001116
Christopher Faulet67957bd2017-09-27 11:00:59 +02001117 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001118 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001119 /* Move the first record to the end of the list, for internal
1120 * round robin */
1121 LIST_DEL(&record->list);
1122 LIST_ADDQ(&dns_p->answer_list, &record->list);
1123 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001124 }
1125 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001126}
1127
Christopher Faulet67957bd2017-09-27 11:00:59 +02001128/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001129 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001130 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1131 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001132 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001133 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1134 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001136int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001138 char *ptr;
1139 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001140
Christopher Faulet67957bd2017-09-27 11:00:59 +02001141 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001142 return -1;
1143
Christopher Faulet67957bd2017-09-27 11:00:59 +02001144 ptr = str;
1145 for (i = 0; i < dn_len-1; ++i) {
1146 sz = dn[i];
1147 if (i)
1148 *ptr++ = '.';
1149 memcpy(ptr, dn+i+1, sz);
1150 ptr += sz;
1151 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001152 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001153 *ptr++ = '\0';
1154 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001155}
1156
Christopher Faulet67957bd2017-09-27 11:00:59 +02001157/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1158 *
1159 * <str> must be a null-terminated string. <str_len> must include the
1160 * terminating null byte. <dn> buffer must be allocated and its size must be
1161 * passed in <dn_len>.
1162 *
1163 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1164 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001165 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001166int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001167{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001168 int i, offset;
1169
Christopher Faulet67957bd2017-09-27 11:00:59 +02001170 if (dn_len < str_len + 1)
1171 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001172
Christopher Faulet67957bd2017-09-27 11:00:59 +02001173 /* First byte of dn will be used to store the length of the first
1174 * label */
1175 offset = 0;
1176 for (i = 0; i < str_len; ++i) {
1177 if (str[i] == '.') {
1178 /* 2 or more consecutive dots is invalid */
1179 if (i == offset)
1180 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001181
Christopher Faulet67957bd2017-09-27 11:00:59 +02001182 dn[offset] = (i - offset);
1183 offset = i+1;
1184 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001185 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001186 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001187 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001188 dn[offset] = (i - offset - 1);
1189 dn[i] = '\0';
1190 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001191}
1192
Christopher Faulet67957bd2017-09-27 11:00:59 +02001193/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001194 * - total size
1195 * - each label size individually
1196 * returns:
1197 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1198 * 1 when no error. <err> is left unaffected.
1199 */
1200int dns_hostname_validation(const char *string, char **err)
1201{
1202 const char *c, *d;
1203 int i;
1204
1205 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1206 if (err)
1207 *err = DNS_TOO_LONG_FQDN;
1208 return 0;
1209 }
1210
1211 c = string;
1212 while (*c) {
1213 d = c;
1214
1215 i = 0;
1216 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1217 i++;
1218 if (!((*d == '-') || (*d == '_') ||
1219 ((*d >= 'a') && (*d <= 'z')) ||
1220 ((*d >= 'A') && (*d <= 'Z')) ||
1221 ((*d >= '0') && (*d <= '9')))) {
1222 if (err)
1223 *err = DNS_INVALID_CHARACTER;
1224 return 0;
1225 }
1226 d++;
1227 }
1228
1229 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1230 if (err)
1231 *err = DNS_LABEL_TOO_LONG;
1232 return 0;
1233 }
1234
1235 if (*d == '\0')
1236 goto out;
1237
1238 c = ++d;
1239 }
1240 out:
1241 return 1;
1242}
1243
Christopher Faulet67957bd2017-09-27 11:00:59 +02001244/* Picks up an available resolution from the different resolution list
1245 * associated to a resolvers section, in this order:
1246 * 1. check in resolutions.curr for the same hostname and query_type
1247 * 2. check in resolutions.wait for the same hostname and query_type
1248 * 3. Get a new resolution from resolution pool
1249 *
1250 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001251 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001252static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1253 char **hostname_dn, int hostname_dn_len,
1254 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001255{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001256 struct dns_resolution *res;
1257
1258 if (!*hostname_dn)
1259 goto from_pool;
1260
1261 /* Search for same hostname and query type in resolutions.curr */
1262 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1263 if (!res->hostname_dn)
1264 continue;
1265 if ((query_type == res->prefered_query_type) &&
1266 hostname_dn_len == res->hostname_dn_len &&
1267 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1268 return res;
1269 }
1270
1271 /* Search for same hostname and query type in resolutions.wait */
1272 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1273 if (!res->hostname_dn)
1274 continue;
1275 if ((query_type == res->prefered_query_type) &&
1276 hostname_dn_len == res->hostname_dn_len &&
1277 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1278 return res;
1279 }
1280
1281 from_pool:
1282 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001283 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001284 if (res) {
1285 memset(res, 0, sizeof(*res));
1286 res->resolvers = resolvers;
1287 res->uuid = resolution_uuid;
1288 res->status = RSLV_STATUS_NONE;
1289 res->step = RSLV_STEP_NONE;
1290 res->last_valid = now_ms;
1291
1292 LIST_INIT(&res->requesters);
1293 LIST_INIT(&res->response.answer_list);
1294
1295 res->prefered_query_type = query_type;
1296 res->query_type = query_type;
1297 res->hostname_dn = *hostname_dn;
1298 res->hostname_dn_len = hostname_dn_len;
1299
1300 ++resolution_uuid;
1301
1302 /* Move the resolution to the resolvers wait queue */
1303 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1304 }
1305 return res;
1306}
1307
1308/* Releases a resolution from its requester(s) and move it back to the pool */
1309static void dns_free_resolution(struct dns_resolution *resolution)
1310{
1311 struct dns_requester *req, *reqback;
1312
1313 /* clean up configuration */
1314 dns_reset_resolution(resolution);
1315 resolution->hostname_dn = NULL;
1316 resolution->hostname_dn_len = 0;
1317
1318 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1319 LIST_DEL(&req->list);
1320 req->resolution = NULL;
1321 }
1322
1323 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001324 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001325}
1326
1327/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1328 * on success, -1 otherwise.
1329 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001330int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001331{
1332 struct dns_resolution *res = NULL;
1333 struct dns_requester *req;
1334 struct dns_resolvers *resolvers;
1335 struct server *srv = NULL;
1336 struct dns_srvrq *srvrq = NULL;
1337 char **hostname_dn;
1338 int hostname_dn_len, query_type;
1339
1340 switch (requester_type) {
1341 case OBJ_TYPE_SERVER:
1342 srv = (struct server *)requester;
1343 hostname_dn = &srv->hostname_dn;
1344 hostname_dn_len = srv->hostname_dn_len;
1345 resolvers = srv->resolvers;
1346 query_type = ((srv->dns_opts.family_prio == AF_INET)
1347 ? DNS_RTYPE_A
1348 : DNS_RTYPE_AAAA);
1349 break;
1350
1351 case OBJ_TYPE_SRVRQ:
1352 srvrq = (struct dns_srvrq *)requester;
1353 hostname_dn = &srvrq->hostname_dn;
1354 hostname_dn_len = srvrq->hostname_dn_len;
1355 resolvers = srvrq->resolvers;
1356 query_type = DNS_RTYPE_SRV;
1357 break;
1358
1359 default:
1360 goto err;
1361 }
1362
1363 /* Get a resolution from the resolvers' wait queue or pool */
1364 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1365 goto err;
1366
1367 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001368 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001369 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001370 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001371 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001372 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001373 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001374 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001375 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001376 req->owner = &srv->obj_type;
1377 srv->dns_requester = req;
1378 }
1379 else
1380 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001381 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001382 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001383 }
1384 else if (srvrq) {
1385 if (srvrq->dns_requester == NULL) {
1386 if ((req = calloc(1, sizeof(*req))) == NULL)
1387 goto err;
1388 req->owner = &srvrq->obj_type;
1389 srvrq->dns_requester = req;
1390 }
1391 else
1392 req = srvrq->dns_requester;
1393 }
1394 else
1395 goto err;
1396
1397 req->resolution = res;
1398 req->requester_cb = snr_resolution_cb;
1399 req->requester_error_cb = snr_resolution_error_cb;
1400
1401 LIST_ADDQ(&res->requesters, &req->list);
1402 return 0;
1403
1404 err:
1405 if (res && LIST_ISEMPTY(&res->requesters))
1406 dns_free_resolution(res);
1407 return -1;
1408}
1409
1410/* Removes a requester from a DNS resoltion. It takes takes care of all the
1411 * consequences. It also cleans up some parameters from the requester.
1412 */
1413void dns_unlink_resolution(struct dns_requester *requester)
1414{
1415 struct dns_resolution *res;
1416 struct dns_requester *req;
1417
1418 /* Nothing to do */
1419 if (!requester || !requester->resolution)
1420 return;
1421 res = requester->resolution;
1422
1423 /* Clean up the requester */
1424 LIST_DEL(&requester->list);
1425 requester->resolution = NULL;
1426
1427 /* We need to find another requester linked on this resolution */
1428 if (!LIST_ISEMPTY(&res->requesters))
1429 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1430 else {
1431 dns_free_resolution(res);
1432 return;
1433 }
1434
1435 /* Move hostname_dn related pointers to the next requester */
1436 switch (obj_type(req->owner)) {
1437 case OBJ_TYPE_SERVER:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001438 res->hostname_dn = __objt_server(req->owner)->hostname_dn;
1439 res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001440 break;
1441 case OBJ_TYPE_SRVRQ:
Willy Tarreau433c16f2018-09-20 11:15:27 +02001442 res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn;
1443 res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001444 break;
1445 default:
1446 res->hostname_dn = NULL;
1447 res->hostname_dn_len = 0;
1448 break;
1449 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001450}
1451
Christopher Faulet67957bd2017-09-27 11:00:59 +02001452/* Called when a network IO is generated on a name server socket for an incoming
1453 * packet. It performs the following actions:
1454 * - check if the packet requires processing (not outdated resolution)
1455 * - ensure the DNS packet received is valid and call requester's callback
1456 * - call requester's error callback if invalid response
1457 * - check the dn_name in the packet against the one sent
1458 */
1459static void dns_resolve_recv(struct dgram_conn *dgram)
1460{
1461 struct dns_nameserver *ns, *tmpns;
1462 struct dns_resolvers *resolvers;
1463 struct dns_resolution *res;
1464 struct dns_query_item *query;
1465 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1466 unsigned char *bufend;
1467 int fd, buflen, dns_resp;
1468 int max_answer_records;
1469 unsigned short query_id;
1470 struct eb32_node *eb;
1471 struct dns_requester *req;
1472
1473 fd = dgram->t.sock.fd;
1474
1475 /* check if ready for reading */
1476 if (!fd_recv_ready(fd))
1477 return;
1478
1479 /* no need to go further if we can't retrieve the nameserver */
1480 if ((ns = dgram->owner) == NULL)
1481 return;
1482
1483 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001484 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001485
1486 /* process all pending input messages */
1487 while (1) {
1488 /* read message received */
1489 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1490 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1491 /* FIXME : for now we consider EAGAIN only */
1492 fd_cant_recv(fd);
1493 break;
1494 }
1495
1496 /* message too big */
1497 if (buflen > resolvers->accepted_payload_size) {
1498 ns->counters.too_big++;
1499 continue;
1500 }
1501
1502 /* initializing variables */
1503 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1504
1505 /* read the query id from the packet (16 bits) */
1506 if (buf + 2 > bufend) {
1507 ns->counters.invalid++;
1508 continue;
1509 }
1510 query_id = dns_response_get_query_id(buf);
1511
1512 /* search the query_id in the pending resolution tree */
1513 eb = eb32_lookup(&resolvers->query_ids, query_id);
1514 if (eb == NULL) {
1515 /* unknown query id means an outdated response and can be safely ignored */
1516 ns->counters.outdated++;
1517 continue;
1518 }
1519
1520 /* known query id means a resolution in prgress */
1521 res = eb32_entry(eb, struct dns_resolution, qid);
1522 if (!res) {
1523 ns->counters.outdated++;
1524 continue;
1525 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001526
Christopher Faulet67957bd2017-09-27 11:00:59 +02001527 /* number of responses received */
1528 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001529
Christopher Faulet67957bd2017-09-27 11:00:59 +02001530 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1531 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001532
Christopher Faulet67957bd2017-09-27 11:00:59 +02001533 switch (dns_resp) {
1534 case DNS_RESP_VALID:
1535 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001536
Christopher Faulet67957bd2017-09-27 11:00:59 +02001537 case DNS_RESP_INVALID:
1538 case DNS_RESP_QUERY_COUNT_ERROR:
1539 case DNS_RESP_WRONG_NAME:
1540 res->status = RSLV_STATUS_INVALID;
1541 ns->counters.invalid++;
1542 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001543
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 case DNS_RESP_NX_DOMAIN:
1545 res->status = RSLV_STATUS_NX;
1546 ns->counters.nx++;
1547 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001548
Christopher Faulet67957bd2017-09-27 11:00:59 +02001549 case DNS_RESP_REFUSED:
1550 res->status = RSLV_STATUS_REFUSED;
1551 ns->counters.refused++;
1552 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001553
Christopher Faulet67957bd2017-09-27 11:00:59 +02001554 case DNS_RESP_ANCOUNT_ZERO:
1555 res->status = RSLV_STATUS_OTHER;
1556 ns->counters.any_err++;
1557 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001558
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559 case DNS_RESP_CNAME_ERROR:
1560 res->status = RSLV_STATUS_OTHER;
1561 ns->counters.cname_error++;
1562 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001563
Christopher Faulet67957bd2017-09-27 11:00:59 +02001564 case DNS_RESP_TRUNCATED:
1565 res->status = RSLV_STATUS_OTHER;
1566 ns->counters.truncated++;
1567 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001568
Christopher Faulet67957bd2017-09-27 11:00:59 +02001569 case DNS_RESP_NO_EXPECTED_RECORD:
1570 case DNS_RESP_ERROR:
1571 case DNS_RESP_INTERNAL:
1572 res->status = RSLV_STATUS_OTHER;
1573 ns->counters.other++;
1574 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001575 }
1576
Christopher Faulet67957bd2017-09-27 11:00:59 +02001577 /* Wait all nameservers response to handle errors */
1578 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1579 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001580
Christopher Faulet67957bd2017-09-27 11:00:59 +02001581 /* Process error codes */
1582 if (dns_resp != DNS_RESP_VALID) {
1583 if (res->prefered_query_type != res->query_type) {
1584 /* The fallback on the query type was already performed,
1585 * so check the try counter. If it falls to 0, we can
1586 * report an error. Else, wait the next attempt. */
1587 if (!res->try)
1588 goto report_res_error;
1589 }
1590 else {
1591 /* Fallback from A to AAAA or the opposite and re-send
1592 * the resolution immediately. try counter is not
1593 * decremented. */
1594 if (res->prefered_query_type == DNS_RTYPE_A) {
1595 res->query_type = DNS_RTYPE_AAAA;
1596 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001597 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001598 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1599 res->query_type = DNS_RTYPE_A;
1600 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001601 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001602 }
1603 continue;
1604 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001605
Christopher Faulet67957bd2017-09-27 11:00:59 +02001606 /* Now let's check the query's dname corresponds to the one we
1607 * sent. We can check only the first query of the list. We send
1608 * one query at a time so we get one query in the response */
1609 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1610 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1611 dns_resp = DNS_RESP_WRONG_NAME;
1612 ns->counters.other++;
1613 goto report_res_error;
1614 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001615
Christopher Faulet67957bd2017-09-27 11:00:59 +02001616 /* So the resolution succeeded */
1617 res->status = RSLV_STATUS_VALID;
1618 res->last_valid = now_ms;
1619 ns->counters.valid++;
1620 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001621
Christopher Faulet67957bd2017-09-27 11:00:59 +02001622 report_res_error:
1623 list_for_each_entry(req, &res->requesters, list)
1624 req->requester_error_cb(req, dns_resp);
1625 dns_reset_resolution(res);
1626 LIST_DEL(&res->list);
1627 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1628 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001629
Christopher Faulet67957bd2017-09-27 11:00:59 +02001630 report_res_success:
1631 /* Only the 1rst requester s managed by the server, others are
1632 * from the cache */
1633 tmpns = ns;
1634 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001635 struct server *s = objt_server(req->owner);
1636
1637 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001638 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001639 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001640 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001641 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001642 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001643 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001644
Christopher Faulet67957bd2017-09-27 11:00:59 +02001645 dns_reset_resolution(res);
1646 LIST_DEL(&res->list);
1647 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1648 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001649 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001650 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001651 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001652}
William Lallemand69e96442016-11-19 00:58:54 +01001653
Christopher Faulet67957bd2017-09-27 11:00:59 +02001654/* Called when a resolvers network socket is ready to send data */
1655static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001656{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001657 struct dns_resolvers *resolvers;
1658 struct dns_nameserver *ns;
1659 struct dns_resolution *res;
1660 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001661
Christopher Faulet67957bd2017-09-27 11:00:59 +02001662 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001663
Christopher Faulet67957bd2017-09-27 11:00:59 +02001664 /* check if ready for sending */
1665 if (!fd_send_ready(fd))
1666 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001667
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 /* we don't want/need to be waked up any more for sending */
1669 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001670
Christopher Faulet67957bd2017-09-27 11:00:59 +02001671 /* no need to go further if we can't retrieve the nameserver */
1672 if ((ns = dgram->owner) == NULL)
1673 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001674
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001676 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001677
Christopher Faulet67957bd2017-09-27 11:00:59 +02001678 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001679 int ret, len;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001680
Christopher Faulet67957bd2017-09-27 11:00:59 +02001681 if (res->nb_queries == resolvers->nb_nameservers)
1682 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001683
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001684 len = dns_build_query(res->query_id, res->query_type,
1685 resolvers->accepted_payload_size,
1686 res->hostname_dn, res->hostname_dn_len,
1687 trash.area, trash.size);
1688 if (len == -1)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001689 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001690
Willy Tarreauf6ee9dc2018-08-22 04:52:02 +02001691 ret = send(fd, trash.area, len, 0);
1692 if (ret != len)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001693 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001694
Christopher Faulet67957bd2017-09-27 11:00:59 +02001695 ns->counters.sent++;
1696 res->nb_queries++;
1697 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001698
Christopher Faulet67957bd2017-09-27 11:00:59 +02001699 snd_error:
1700 ns->counters.snd_error++;
1701 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001702 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001703 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001704}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001705
Christopher Faulet67957bd2017-09-27 11:00:59 +02001706/* Processes DNS resolution. First, it checks the active list to detect expired
1707 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1708 * checks the wait list to trigger new resolutions.
1709 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001710static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001711{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001712 struct dns_resolvers *resolvers = context;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001713 struct dns_resolution *res, *resback;
1714 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001715
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001716 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001717
Christopher Faulet67957bd2017-09-27 11:00:59 +02001718 /* Handle all expired resolutions from the active list */
1719 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1720 /* When we find the first resolution in the future, then we can
1721 * stop here */
1722 exp = tick_add(res->last_query, resolvers->timeout.retry);
1723 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001724 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001725
Christopher Faulet67957bd2017-09-27 11:00:59 +02001726 /* If current resolution has been tried too many times and
1727 * finishes in timeout we update its status and remove it from
1728 * the list */
1729 if (!res->try) {
1730 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001731
Christopher Faulet67957bd2017-09-27 11:00:59 +02001732 /* Notify the result to the requesters */
1733 if (!res->nb_responses)
1734 res->status = RSLV_STATUS_TIMEOUT;
1735 list_for_each_entry(req, &res->requesters, list)
1736 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001737
Christopher Faulet67957bd2017-09-27 11:00:59 +02001738 /* Clean up resolution info and remove it from the
1739 * current list */
1740 dns_reset_resolution(res);
1741 LIST_DEL(&res->list);
1742 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001743 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001744 else {
1745 /* Otherwise resend the DNS query and requeue the resolution */
1746 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1747 /* No response received (a real timeout) or fallback already done */
1748 res->query_type = res->prefered_query_type;
1749 res->try--;
1750 }
1751 else {
1752 /* Fallback from A to AAAA or the opposite and re-send
1753 * the resolution immediately. try counter is not
1754 * decremented. */
1755 if (res->prefered_query_type == DNS_RTYPE_A)
1756 res->query_type = DNS_RTYPE_AAAA;
1757 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1758 res->query_type = DNS_RTYPE_A;
1759 else
1760 res->try--;
1761 }
1762 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001763 }
1764 }
William Lallemand69e96442016-11-19 00:58:54 +01001765
Christopher Faulet67957bd2017-09-27 11:00:59 +02001766 /* Handle all resolutions in the wait list */
1767 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1768 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1769 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1770 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001771
Christopher Faulet67957bd2017-09-27 11:00:59 +02001772 if (dns_run_resolution(res) != 1) {
1773 res->last_resolution = now_ms;
1774 LIST_DEL(&res->list);
1775 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001776 }
1777 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001778
Christopher Faulet67957bd2017-09-27 11:00:59 +02001779 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001780 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781 return t;
1782}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001783
Christopher Faulet67957bd2017-09-27 11:00:59 +02001784/* proto_udp callback functions for a DNS resolution */
1785struct dgram_data_cb resolve_dgram_cb = {
1786 .recv = dns_resolve_recv,
1787 .send = dns_resolve_send,
1788};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001789
Christopher Faulet67957bd2017-09-27 11:00:59 +02001790/* Release memory allocated by DNS */
1791static void dns_deinit(void)
1792{
1793 struct dns_resolvers *resolvers, *resolversback;
1794 struct dns_nameserver *ns, *nsback;
1795 struct dns_resolution *res, *resback;
1796 struct dns_requester *req, *reqback;
1797 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001798
Christopher Faulet67957bd2017-09-27 11:00:59 +02001799 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1800 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1801 free(ns->id);
1802 free((char *)ns->conf.file);
1803 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1804 fd_delete(ns->dgram->t.sock.fd);
1805 free(ns->dgram);
1806 LIST_DEL(&ns->list);
1807 free(ns);
1808 }
1809
1810 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1811 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1812 LIST_DEL(&req->list);
1813 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001814 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001815 dns_free_resolution(res);
1816 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001817
Christopher Faulet67957bd2017-09-27 11:00:59 +02001818 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1819 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1820 LIST_DEL(&req->list);
1821 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001822 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001823 dns_free_resolution(res);
1824 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001825
Christopher Faulet67957bd2017-09-27 11:00:59 +02001826 free(resolvers->id);
1827 free((char *)resolvers->conf.file);
1828 task_delete(resolvers->t);
1829 task_free(resolvers->t);
1830 LIST_DEL(&resolvers->list);
1831 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001832 }
1833
Christopher Faulet67957bd2017-09-27 11:00:59 +02001834 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1835 free(srvrq->name);
1836 free(srvrq->hostname_dn);
1837 LIST_DEL(&srvrq->list);
1838 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001839 }
1840
Willy Tarreaubafbe012017-11-24 17:34:44 +01001841 pool_destroy(dns_answer_item_pool);
1842 pool_destroy(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001843}
1844
Christopher Faulet67957bd2017-09-27 11:00:59 +02001845/* Finalizes the DNS configuration by allocating required resources and checking
1846 * live parameters.
1847 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001848 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001849static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001850{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001851 struct dns_resolvers *resolvers;
1852 struct proxy *px;
1853 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001854
Christopher Faulet67957bd2017-09-27 11:00:59 +02001855 /* allocate pool of resolution per resolvers */
1856 list_for_each_entry(resolvers, &dns_resolvers, list) {
1857 struct dns_nameserver *ns;
1858 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001859
Christopher Faulet67957bd2017-09-27 11:00:59 +02001860 /* Check if we can create the socket with nameservers info */
1861 list_for_each_entry(ns, &resolvers->nameservers, list) {
1862 struct dgram_conn *dgram = NULL;
1863 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001864
Christopher Faulet67957bd2017-09-27 11:00:59 +02001865 /* Check nameserver info */
1866 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001867 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1868 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001869 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001870 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001871 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001872 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001873 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1874 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001875 close(fd);
1876 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001877 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001878 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001879 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001880
Christopher Faulet67957bd2017-09-27 11:00:59 +02001881 /* Create dgram structure that will hold the UPD socket
1882 * and attach it on the current nameserver */
1883 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001884 ha_alert("config: resolvers '%s' : out of memory.\n",
1885 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001886 err_code |= (ERR_ALERT|ERR_ABORT);
1887 goto err;
1888 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001889
Christopher Faulet67957bd2017-09-27 11:00:59 +02001890 /* Leave dgram partially initialized, no FD attached for
1891 * now. */
1892 dgram->owner = ns;
1893 dgram->data = &resolve_dgram_cb;
1894 dgram->t.sock.fd = -1;
1895 ns->dgram = dgram;
1896 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001897
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001899 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001900 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001901 err_code |= (ERR_ALERT|ERR_ABORT);
1902 goto err;
1903 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001904
Christopher Faulet67957bd2017-09-27 11:00:59 +02001905 /* Update task's parameters */
1906 t->process = dns_process_resolvers;
1907 t->context = resolvers;
1908 resolvers->t = t;
1909 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001910 }
1911
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001912 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001913 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001914
Christopher Faulet67957bd2017-09-27 11:00:59 +02001915 for (srv = px->srv; srv; srv = srv->next) {
1916 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001917
Christopher Faulet67957bd2017-09-27 11:00:59 +02001918 if (!srv->resolvers_id)
1919 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001920
Christopher Faulet67957bd2017-09-27 11:00:59 +02001921 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001922 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1923 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001924 err_code |= (ERR_ALERT|ERR_ABORT);
1925 continue;
1926 }
1927 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001928
Christopher Faulet67957bd2017-09-27 11:00:59 +02001929 if (srv->srvrq && !srv->srvrq->resolvers) {
1930 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001931 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001932 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1933 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934 err_code |= (ERR_ALERT|ERR_ABORT);
1935 continue;
1936 }
1937 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001938 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001939 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1940 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001941 err_code |= (ERR_ALERT|ERR_ABORT);
1942 continue;
1943 }
1944 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001945 }
1946
Christopher Faulet67957bd2017-09-27 11:00:59 +02001947 if (err_code & (ERR_ALERT|ERR_ABORT))
1948 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001949
Christopher Faulet67957bd2017-09-27 11:00:59 +02001950 return err_code;
1951 err:
1952 dns_deinit();
1953 return err_code;
1954
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001955}
1956
Christopher Faulet67957bd2017-09-27 11:00:59 +02001957/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001958static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001959{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001960 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001961
Christopher Faulet67957bd2017-09-27 11:00:59 +02001962 if (*args[2]) {
1963 list_for_each_entry(presolvers, &dns_resolvers, list) {
1964 if (strcmp(presolvers->id, args[2]) == 0) {
1965 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001966 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001967 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001968 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001969 if (appctx->ctx.cli.p0 == NULL) {
1970 appctx->ctx.cli.severity = LOG_ERR;
1971 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1972 appctx->st0 = CLI_ST_PRINT;
1973 return 1;
1974 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001975 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001976 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001977}
1978
Christopher Faulet67957bd2017-09-27 11:00:59 +02001979/* Dumps counters from all resolvers section and associated name servers. It
1980 * returns 0 if the output buffer is full and it needs to be called again,
1981 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001982 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001983 */
1984static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1985{
1986 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001987 struct dns_resolvers *resolvers;
1988 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001989
1990 chunk_reset(&trash);
1991
1992 switch (appctx->st2) {
1993 case STAT_ST_INIT:
1994 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1995 /* fall through */
1996
1997 case STAT_ST_LIST:
1998 if (LIST_ISEMPTY(&dns_resolvers)) {
1999 chunk_appendf(&trash, "No resolvers found\n");
2000 }
2001 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002 list_for_each_entry(resolvers, &dns_resolvers, list) {
2003 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002004 continue;
2005
Christopher Faulet67957bd2017-09-27 11:00:59 +02002006 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2007 list_for_each_entry(ns, &resolvers->nameservers, list) {
2008 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2009 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2010 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2011 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2012 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2013 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2014 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2015 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2016 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2017 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2018 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2019 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2020 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2021 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2022 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2023 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002024 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002025 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002026 }
2027 }
2028
2029 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002030 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002031 /* let's try again later from this session. We add ourselves into
2032 * this session's users so that it can remove us upon termination.
2033 */
Willy Tarreaudb398432018-11-15 11:08:52 +01002034 si_rx_room_blk(si);
William Lallemand69e96442016-11-19 00:58:54 +01002035 return 0;
2036 }
William Lallemand69e96442016-11-19 00:58:54 +01002037 /* fall through */
2038
2039 default:
2040 appctx->st2 = STAT_ST_FIN;
2041 return 1;
2042 }
2043}
2044
2045/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002046static struct cli_kw_list cli_kws = {{ }, {
2047 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2048 " associated name servers",
2049 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2050 {{},}
2051 }
2052};
William Lallemand69e96442016-11-19 00:58:54 +01002053
Willy Tarreau0108d902018-11-25 19:14:37 +01002054INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand69e96442016-11-19 00:58:54 +01002055
2056__attribute__((constructor))
2057static void __dns_init(void)
2058{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002059 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2060 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2061
Baptiste Assmann044fd5b2018-08-10 10:56:38 +02002062 cfg_register_postparser("dns runtime resolver", dns_finalize_config);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002063 hap_register_post_deinit(dns_deinit);
William Lallemand69e96442016-11-19 00:58:54 +01002064}