blob: 6b87460908452e7958f4283b5bee443107066491 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Christopher Faulet67957bd2017-09-27 11:00:59 +020022#include <common/errors.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020023#include <common/time.h>
24#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020025#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020026
William Lallemand69e96442016-11-19 00:58:54 +010027#include <types/applet.h>
28#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020029#include <types/global.h>
30#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010031#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032
William Lallemand69e96442016-11-19 00:58:54 +010033#include <proto/channel.h>
34#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035#include <proto/checks.h>
36#include <proto/dns.h>
37#include <proto/fd.h>
38#include <proto/log.h>
39#include <proto/server.h>
40#include <proto/task.h>
41#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020042#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010043#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020044
Christopher Faulet67957bd2017-09-27 11:00:59 +020045struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
46struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020047
Christopher Fauletb2812a62017-10-04 16:17:58 +020048static THREAD_LOCAL int64_t dns_query_id_seed = 0; /* random seed */
Christopher Faulet67957bd2017-09-27 11:00:59 +020049static struct pool_head *dns_answer_item_pool = NULL;
50static struct pool_head *dns_resolution_pool = NULL;
51static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020052
Christopher Faulet67957bd2017-09-27 11:00:59 +020053/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
54 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020055 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020056struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020057{
Christopher Faulet67957bd2017-09-27 11:00:59 +020058 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059
Christopher Faulet67957bd2017-09-27 11:00:59 +020060 list_for_each_entry(res, &dns_resolvers, list) {
61 if (!strcmp(res->id, id))
62 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020064 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065}
66
Christopher Faulet67957bd2017-09-27 11:00:59 +020067/* Returns a pointer on the SRV request matching the name <name> for the proxy
68 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020070struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071{
Christopher Faulet67957bd2017-09-27 11:00:59 +020072 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073
Christopher Faulet67957bd2017-09-27 11:00:59 +020074 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
75 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
76 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020078 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020079}
80
Christopher Faulet67957bd2017-09-27 11:00:59 +020081/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
82 * NULL if an error occurred. */
83struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020084{
Christopher Faulet67957bd2017-09-27 11:00:59 +020085 struct proxy *px = srv->proxy;
86 struct dns_srvrq *srvrq = NULL;
87 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020088
Christopher Faulet67957bd2017-09-27 11:00:59 +020089 fqdn_len = strlen(fqdn);
90 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.str, trash.size);
91 if (hostname_dn_len == -1) {
92 Alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
93 proxy_type_str(px), px->id, srv->id, fqdn);
94 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020095 }
96
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
98 Alert("config : %s '%s', server '%s': out of memory\n",
99 proxy_type_str(px), px->id, srv->id);
100 goto err;
101 }
102 srvrq->obj_type = OBJ_TYPE_SRVRQ;
103 srvrq->proxy = px;
104 srvrq->name = strdup(fqdn);
105 srvrq->hostname_dn = strdup(trash.str);
106 srvrq->hostname_dn_len = hostname_dn_len;
107 if (!srvrq->name || !srvrq->hostname_dn) {
108 Alert("config : %s '%s', server '%s': out of memory\n",
109 proxy_type_str(px), px->id, srv->id);
110 goto err;
111 }
112 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
113 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200114
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 err:
116 if (srvrq) {
117 free(srvrq->name);
118 free(srvrq->hostname_dn);
119 free(srvrq);
120 }
121 return NULL;
122}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200123
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200124
Christopher Faulet67957bd2017-09-27 11:00:59 +0200125/* 2 bytes random generator to generate DNS query ID */
126static inline uint16_t dns_rnd16(void)
127{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200128 if (!dns_query_id_seed)
129 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130 dns_query_id_seed ^= dns_query_id_seed << 13;
131 dns_query_id_seed ^= dns_query_id_seed >> 7;
132 dns_query_id_seed ^= dns_query_id_seed << 17;
133 return dns_query_id_seed;
134}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200135
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136
Christopher Faulet67957bd2017-09-27 11:00:59 +0200137static inline int dns_resolution_timeout(struct dns_resolution *res)
138{
139 switch (res->status) {
140 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
141 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200142 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200143}
144
Christopher Faulet67957bd2017-09-27 11:00:59 +0200145/* Updates a resolvers' task timeout for next wake up and queue it */
146static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200147{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200148 struct dns_resolution *res;
149 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151 next = tick_add(now_ms, resolvers->timeout.resolve);
152 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
153 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
154 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
155 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200156
Christopher Faulet67957bd2017-09-27 11:00:59 +0200157 list_for_each_entry(res, &resolvers->resolutions.wait, list)
158 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200159
Christopher Faulet67957bd2017-09-27 11:00:59 +0200160 resolvers->t->expire = next;
161 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200162}
163
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
165 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200166 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 struct dgram_conn *dgram = ns->dgram;
170 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172 /* Already connected */
173 if (dgram->t.sock.fd != -1)
174 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200175
Christopher Faulet67957bd2017-09-27 11:00:59 +0200176 /* Create an UDP socket and connect it on the nameserver's IP/Port */
177 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
178 send_log(NULL, LOG_WARNING,
179 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
180 ns->resolvers->id, ns->id);
181 return -1;
182 }
183 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
184 send_log(NULL, LOG_WARNING,
185 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
186 ns->resolvers->id, ns->id);
187 close(fd);
188 return -1;
189 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200190
Christopher Faulet67957bd2017-09-27 11:00:59 +0200191 /* Make the socket non blocking */
192 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200193
Christopher Faulet67957bd2017-09-27 11:00:59 +0200194 /* Add the fd in the fd list and update its parameters */
195 dgram->t.sock.fd = fd;
196 fdtab[fd].owner = dgram;
197 fdtab[fd].iocb = dgram_fd_handler;
Christopher Faulet36716a72017-05-30 11:07:16 +0200198 fd_insert(fd, (unsigned long)-1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 fd_want_recv(fd);
200 return 0;
201}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200202
Christopher Faulet67957bd2017-09-27 11:00:59 +0200203/* Forges a DNS query. It needs the following information from the caller:
204 * - <query_id> : the DNS query id corresponding to this query
205 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
206 * - <hostname_dn> : hostname in domain name format
207 * - <hostname_dn_len> : length of <hostname_dn>
208 *
209 * To store the query, the caller must pass a buffer <buf> and its size
210 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
211 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200212 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200213static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
214 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200215{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200216 struct dns_header dns_hdr;
217 struct dns_question qinfo;
218 struct dns_additional_record edns;
219 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200220
Christopher Faulet67957bd2017-09-27 11:00:59 +0200221 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
222 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200225
Christopher Faulet67957bd2017-09-27 11:00:59 +0200226 /* Set dns query headers */
227 dns_hdr.id = (unsigned short) htons(query_id);
228 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
229 dns_hdr.qdcount = htons(1); /* 1 question */
230 dns_hdr.ancount = 0;
231 dns_hdr.nscount = 0;
232 dns_hdr.arcount = htons(1);
233 memcpy(p, &dns_hdr, sizeof(dns_hdr));
234 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200235
Christopher Faulet67957bd2017-09-27 11:00:59 +0200236 /* Set up query hostname */
237 memcpy(p, hostname_dn, hostname_dn_len);
238 p += hostname_dn_len;
239 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200240
Christopher Faulet67957bd2017-09-27 11:00:59 +0200241 /* Set up query info (type and class) */
242 qinfo.qtype = htons(query_type);
243 qinfo.qclass = htons(DNS_RCLASS_IN);
244 memcpy(p, &qinfo, sizeof(qinfo));
245 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200246
Christopher Faulet67957bd2017-09-27 11:00:59 +0200247 /* Set the DNS extension */
248 edns.name = 0;
249 edns.type = htons(DNS_RTYPE_OPT);
250 edns.udp_payload_size = htons(accepted_payload_size);
251 edns.extension = 0;
252 edns.data_length = 0;
253 memcpy(p, &edns, sizeof(edns));
254 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200255
Christopher Faulet67957bd2017-09-27 11:00:59 +0200256 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200257}
258
Christopher Faulet67957bd2017-09-27 11:00:59 +0200259/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
260 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200261 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200263{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200264 struct dns_resolvers *resolvers = resolution->resolvers;
265 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266
Christopher Faulet67957bd2017-09-27 11:00:59 +0200267 list_for_each_entry(ns, &resolvers->nameservers, list) {
268 int fd = ns->dgram->t.sock.fd;
269 if (fd == -1) {
270 if (dns_connect_namesaver(ns) == -1)
271 continue;
272 fd = ns->dgram->t.sock.fd;
273 resolvers->nb_nameservers++;
274 }
275 fd_want_send(fd);
276 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200277
Christopher Faulet67957bd2017-09-27 11:00:59 +0200278 /* Update resolution */
279 resolution->nb_queries = 0;
280 resolution->nb_responses = 0;
281 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200282
Christopher Faulet67957bd2017-09-27 11:00:59 +0200283 /* Push the resolution at the end of the active list */
284 LIST_DEL(&resolution->list);
285 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
286 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200287}
288
Christopher Faulet67957bd2017-09-27 11:00:59 +0200289/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
290 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200291 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200292static int
293dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200294{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295 struct dns_resolvers *resolvers = resolution->resolvers;
296 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200297
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298 /* Avoid sending requests for resolutions that don't yet have an
299 * hostname, ie resolutions linked to servers that do not yet have an
300 * fqdn */
301 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200302 return 0;
303
Christopher Faulet67957bd2017-09-27 11:00:59 +0200304 /* Check if a resolution has already been started for this server return
305 * directly to avoid resolution pill up. */
306 if (resolution->step != RSLV_STEP_NONE)
307 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200308
Christopher Faulet67957bd2017-09-27 11:00:59 +0200309 /* Generates a new query id. We try at most 100 times to find a free
310 * query id */
311 for (i = 0; i < 100; ++i) {
312 query_id = dns_rnd16();
313 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200314 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200315 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200316 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200317 if (query_id == -1) {
318 send_log(NULL, LOG_NOTICE,
319 "could not generate a query id for %s, in resolvers %s.\n",
320 resolution->hostname_dn, resolvers->id);
321 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200322 }
323
Christopher Faulet67957bd2017-09-27 11:00:59 +0200324 /* Update resolution parameters */
325 resolution->query_id = query_id;
326 resolution->qid.key = query_id;
327 resolution->step = RSLV_STEP_RUNNING;
328 resolution->query_type = resolution->prefered_query_type;
329 resolution->try = resolvers->resolve_retries;
330 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200331
Christopher Faulet67957bd2017-09-27 11:00:59 +0200332 /* Send the DNS query */
333 resolution->try -= 1;
334 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200335 return 1;
336}
337
Christopher Faulet67957bd2017-09-27 11:00:59 +0200338/* Performs a name resolution for the requester <req> */
339void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200340{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 struct dns_resolvers *resolvers;
342 struct dns_resolution *res;
343 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200344
Christopher Faulet67957bd2017-09-27 11:00:59 +0200345 if (!req || !req->resolution)
346 return;
347 res = req->resolution;
348 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200349
Christopher Faulet67957bd2017-09-27 11:00:59 +0200350 /* The resolution must not be triggered yet. Use the cached response, if
351 * valid */
352 exp = tick_add(res->last_resolution, resolvers->hold.valid);
353 if (res->status == RSLV_STATUS_VALID &&
354 tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
355 req->requester_cb(req, NULL);
356 else
357 dns_run_resolution(res);
358}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200359
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200360
Christopher Faulet67957bd2017-09-27 11:00:59 +0200361/* Resets some resolution parameters to initial values and also delete the query
362 * ID from the resolver's tree.
363 */
364static void dns_reset_resolution(struct dns_resolution *resolution)
365{
366 /* update resolution status */
367 resolution->step = RSLV_STEP_NONE;
368 resolution->try = 0;
369 resolution->last_resolution = now_ms;
370 resolution->nb_queries = 0;
371 resolution->nb_responses = 0;
372 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200373
Christopher Faulet67957bd2017-09-27 11:00:59 +0200374 /* clean up query id */
375 eb32_delete(&resolution->qid);
376 resolution->query_id = 0;
377 resolution->qid.key = 0;
378}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200379
Christopher Faulet67957bd2017-09-27 11:00:59 +0200380/* Returns the query id contained in a DNS response */
381static inline unsigned short dns_response_get_query_id(unsigned char *resp)
382{
383 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200384}
385
Christopher Faulet67957bd2017-09-27 11:00:59 +0200386
387/* Analyses, re-builds and copies the name <name> from the DNS response packet
388 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
389 * for compressed data. The result is copied into <dest>, ensuring we don't
390 * overflow using <dest_len> Returns the number of bytes the caller can move
391 * forward. If 0 it means an error occurred while parsing the name. <offset> is
392 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200393 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200394int dns_read_name(unsigned char *buffer, unsigned char *bufend,
395 unsigned char *name, char *destination, int dest_len,
396 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200397{
398 int nb_bytes = 0, n = 0;
399 int label_len;
400 unsigned char *reader = name;
401 char *dest = destination;
402
403 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200405 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200406 /* Must point BEFORE current position */
407 if ((buffer + reader[1]) > reader)
408 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200409
Christopher Faulet67957bd2017-09-27 11:00:59 +0200410 n = dns_read_name(buffer, bufend, buffer + reader[1],
411 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200412 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200414
Christopher Faulet67957bd2017-09-27 11:00:59 +0200415 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200416 nb_bytes += n;
417 goto out;
418 }
419
420 label_len = *reader;
421 if (label_len == 0)
422 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200423
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200424 /* Check if:
425 * - we won't read outside the buffer
426 * - there is enough place in the destination
427 */
428 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200429 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200430
431 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200432 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200433
434 memcpy(dest, reader, label_len);
435
Christopher Faulet67957bd2017-09-27 11:00:59 +0200436 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200437 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200438 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200439 }
440
Christopher Faulet67957bd2017-09-27 11:00:59 +0200441 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200442 /* offset computation:
443 * parse from <name> until finding either NULL or a pointer "c0xx"
444 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200445 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200446 *offset = 0;
447 while (reader < bufend) {
448 if ((reader[0] & 0xc0) == 0xc0) {
449 *offset += 2;
450 break;
451 }
452 else if (*reader == 0) {
453 *offset += 1;
454 break;
455 }
456 *offset += 1;
457 ++reader;
458 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200459 return nb_bytes;
460
Christopher Faulet67957bd2017-09-27 11:00:59 +0200461 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200462 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200463}
464
Christopher Faulet67957bd2017-09-27 11:00:59 +0200465/* Checks for any obsolete record, also identify any SRV request, and try to
466 * find a corresponding server.
467*/
468static void dns_check_dns_response(struct dns_resolution *res)
469{
470 struct dns_resolvers *resolvers = res->resolvers;
471 struct dns_requester *req, *reqback;
472 struct dns_answer_item *item, *itemback;
473 struct server *srv;
474 struct dns_srvrq *srvrq;
475
476 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
477
478 /* Remove obsolete items */
479 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
480 if (item->type != DNS_RTYPE_SRV)
481 goto rm_obselete_item;
482
483 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
484 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
485 continue;
486
487 /* Remove any associated server */
488 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +0100489 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200490 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
491 item->data_len == srv->hostname_dn_len &&
492 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
493 snr_update_srv_status(srv, 1);
494 free(srv->hostname);
495 free(srv->hostname_dn);
496 srv->hostname = NULL;
497 srv->hostname_dn = NULL;
498 srv->hostname_dn_len = 0;
499 dns_unlink_resolution(srv->dns_requester);
500 }
Emeric Brune9fd6b52017-11-02 17:20:39 +0100501 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200502 }
503 }
504
505 rm_obselete_item:
506 LIST_DEL(&item->list);
507 pool_free2(dns_answer_item_pool, item);
508 continue;
509 }
510
511 if (item->type != DNS_RTYPE_SRV)
512 continue;
513
514 /* Now process SRV records */
515 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
516 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
517 continue;
518
519 /* Check if a server already uses that hostname */
520 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +0100521 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200522 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
523 item->data_len == srv->hostname_dn_len &&
524 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
525 if (srv->uweight != item->weight) {
526 char weight[9];
527
528 snprintf(weight, sizeof(weight), "%d", item->weight);
529 server_parse_weight_change_request(srv, weight);
530 }
Emeric Brune9fd6b52017-11-02 17:20:39 +0100531 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200532 break;
533 }
Emeric Brune9fd6b52017-11-02 17:20:39 +0100534 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200535 }
536 if (srv)
537 continue;
538
539 /* If not, try to find a server with undefined hostname */
540 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +0100541 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200542 if (srv->srvrq == srvrq && !srv->hostname_dn)
543 break;
Emeric Brune9fd6b52017-11-02 17:20:39 +0100544 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200545 }
546 /* And update this server, if found */
547 if (srv) {
548 const char *msg = NULL;
549 char weight[9];
550 char hostname[DNS_MAX_NAME_SIZE];
551
552 if (dns_dn_label_to_str(item->target, item->data_len+1,
553 hostname, DNS_MAX_NAME_SIZE) == -1)
554 continue;
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100555 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200556 if (msg)
557 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
558
559 srv->svc_port = item->port;
560 srv->flags &= ~SRV_F_MAPPORTS;
561 if ((srv->check.state & CHK_ST_CONFIGURED) &&
562 !(srv->flags & SRV_F_CHECKPORT))
563 srv->check.port = item->port;
564 snprintf(weight, sizeof(weight), "%d", item->weight);
565 server_parse_weight_change_request(srv, weight);
Emeric Brune9fd6b52017-11-02 17:20:39 +0100566 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200567 }
568 }
569 }
570}
571
572/* Validates that the buffer DNS response provided in <resp> and finishing
573 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200574 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200575 * The result is stored in <resolution>' response, buf_response,
576 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200577 *
578 * This function returns one of the DNS_RESP_* code to indicate the type of
579 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200580 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200581static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
582 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200583{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200584 unsigned char *reader;
585 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200586 int len, flags, offset;
587 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200588 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200589 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200590 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200591 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200592 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200593
Christopher Faulet67957bd2017-09-27 11:00:59 +0200594 reader = resp;
595 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200596 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200597 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200598
Christopher Faulet67957bd2017-09-27 11:00:59 +0200599 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200600 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200601
602 /* query id */
603 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200604 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200605 dns_p->header.id = reader[0] * 256 + reader[1];
606 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200607
Christopher Faulet67957bd2017-09-27 11:00:59 +0200608 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200609 * First byte contains:
610 * - response flag (1 bit)
611 * - opcode (4 bits)
612 * - authoritative (1 bit)
613 * - truncated (1 bit)
614 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200615 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200616 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200617 return DNS_RESP_INVALID;
618
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200619 flags = reader[0] * 256 + reader[1];
620
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200621 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
622 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200623 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200624 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200625 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200626 return DNS_RESP_ERROR;
627 }
628
Christopher Faulet67957bd2017-09-27 11:00:59 +0200629 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200630 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200631
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200632 /* 2 bytes for question count */
633 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200634 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200635 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200636 /* (for now) we send one query only, so we expect only one in the
637 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200638 if (dns_p->header.qdcount != 1)
639 return DNS_RESP_QUERY_COUNT_ERROR;
640 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200641 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200642 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200643
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200644 /* 2 bytes for answer count */
645 if (reader + 2 >= bufend)
646 return DNS_RESP_INVALID;
647 dns_p->header.ancount = reader[0] * 256 + reader[1];
648 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200649 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200650 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200651 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200652 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200653 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200654
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200655 /* 2 bytes authority count */
656 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200657 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200658 dns_p->header.nscount = reader[0] * 256 + reader[1];
659 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200660
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200661 /* 2 bytes additional count */
662 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200663 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200664 dns_p->header.arcount = reader[0] * 256 + reader[1];
665 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200666
Christopher Faulet67957bd2017-09-27 11:00:59 +0200667 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200668 LIST_INIT(&dns_p->query_list);
669 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 +0200670 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200671 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200672 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200673 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
674 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200675 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200676 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200677
Christopher Faulet67957bd2017-09-27 11:00:59 +0200678 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200680 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200681 offset = 0;
682 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200683
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200684 if (len == 0)
685 return DNS_RESP_INVALID;
686
687 reader += offset;
688 previous_dname = dns_query->name;
689
690 /* move forward 2 bytes for question type */
691 if (reader + 2 >= bufend)
692 return DNS_RESP_INVALID;
693 dns_query->type = reader[0] * 256 + reader[1];
694 reader += 2;
695
696 /* move forward 2 bytes for question class */
697 if (reader + 2 >= bufend)
698 return DNS_RESP_INVALID;
699 dns_query->class = reader[0] * 256 + reader[1];
700 reader += 2;
701 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200702
Baptiste Assmann251abb92017-08-11 09:58:27 +0200703 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200704 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200705 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
706 return DNS_RESP_TRUNCATED;
707
Baptiste Assmann325137d2015-04-13 23:40:55 +0200708 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200709 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200710 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200711 if (reader >= bufend)
712 return DNS_RESP_INVALID;
713
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200714 dns_answer_record = pool_alloc2(dns_answer_item_pool);
715 if (dns_answer_record == NULL)
716 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200717
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200718 offset = 0;
719 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200720
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200721 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200722 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200723 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200724 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200725
Christopher Faulet67957bd2017-09-27 11:00:59 +0200726 /* Check if the current record dname is valid. previous_dname
727 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200728 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200729 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200730 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200731 /* First record, means a mismatch issue between
732 * queried dname and dname found in the first
733 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200734 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200735 }
736 else {
737 /* If not the first record, this means we have a
738 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200739 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200740 }
741
Baptiste Assmann325137d2015-04-13 23:40:55 +0200742 }
743
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200744 memcpy(dns_answer_record->name, tmpname, len);
745 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200746
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200747 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200748 if (reader >= bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200749 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200750 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200751 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200754 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200755 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200756 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200757 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 dns_answer_record->type = reader[0] * 256 + reader[1];
759 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200760
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200761 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200762 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200763 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200765 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767 reader += 2;
768
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200769 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200770 if (reader + 4 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200771 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200772 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200773 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200774 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
775 + reader[2] * 256 + reader[3];
776 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200777
Christopher Faulet67957bd2017-09-27 11:00:59 +0200778 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200779 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200780 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200781 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200782 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200783 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200784
Christopher Faulet67957bd2017-09-27 11:00:59 +0200785 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200786 reader += 2;
787
Christopher Faulet67957bd2017-09-27 11:00:59 +0200788 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200789 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200790 case DNS_RTYPE_A:
791 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200792 if (dns_answer_record->data_len != 4) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200793 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200794 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200795 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200796 dns_answer_record->address.sa_family = AF_INET;
797 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
798 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200799 break;
800
801 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200802 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200803 * no IP could be found and last record was a CNAME. Could be triggered
804 * by a wrong query type
805 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200806 * + 1 because dns_answer_record_id starts at 0
807 * while number of answers is an integer and
808 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200809 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200810 if (i + 1 == dns_p->header.ancount) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200811 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200812 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200813 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200814
815 offset = 0;
816 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200817 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200818 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200819 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200820 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200821
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200822 memcpy(dns_answer_record->target, tmpname, len);
823 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200824 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200825 break;
826
Olivier Houchard8da5f982017-08-04 18:35:36 +0200827
828 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200829 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200830 * - 2 bytes for the priority
831 * - 2 bytes for the weight
832 * - 2 bytes for the port
833 * - the target hostname
834 */
835 if (dns_answer_record->data_len <= 6) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200836 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200837 return DNS_RESP_INVALID;
838 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200839 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200840 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200841 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200842 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200843 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200844 reader += sizeof(uint16_t);
845 offset = 0;
846 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
847 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200848 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200849 return DNS_RESP_INVALID;
850 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200851 dns_answer_record->data_len = len;
852 memcpy(dns_answer_record->target, tmpname, len);
853 dns_answer_record->target[len] = 0;
854 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200855
Baptiste Assmann325137d2015-04-13 23:40:55 +0200856 case DNS_RTYPE_AAAA:
857 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200858 if (dns_answer_record->data_len != 16) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200859 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200860 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200861 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200862 dns_answer_record->address.sa_family = AF_INET6;
863 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
864 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200865 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200866
Baptiste Assmann325137d2015-04-13 23:40:55 +0200867 } /* switch (record type) */
868
Christopher Faulet67957bd2017-09-27 11:00:59 +0200869 /* Increment the counter for number of records saved into our
870 * local response */
871 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200872
Christopher Faulet67957bd2017-09-27 11:00:59 +0200873 /* Move forward dns_answer_record->data_len for analyzing next
874 * record in the response */
875 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
876 ? offset
877 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200878
879 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200880 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200881 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
882 if (tmp_record->type != dns_answer_record->type)
883 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200884
885 switch(tmp_record->type) {
886 case DNS_RTYPE_A:
887 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
888 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
889 sizeof(in_addr_t)))
890 found = 1;
891 break;
892
893 case DNS_RTYPE_AAAA:
894 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
895 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
896 sizeof(struct in6_addr)))
897 found = 1;
898 break;
899
Olivier Houchard8da5f982017-08-04 18:35:36 +0200900 case DNS_RTYPE_SRV:
901 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200902 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200903 dns_answer_record->port == tmp_record->port) {
904 tmp_record->weight = dns_answer_record->weight;
905 found = 1;
906 }
907 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200908
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200909 default:
910 break;
911 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200912
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200913 if (found == 1)
914 break;
915 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200916
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200917 if (found == 1) {
918 tmp_record->last_seen = now.tv_sec;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200919 pool_free2(dns_answer_item_pool, dns_answer_record);
920 }
921 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200922 dns_answer_record->last_seen = now.tv_sec;
923 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
924 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200925 } /* for i 0 to ancount */
926
Christopher Faulet67957bd2017-09-27 11:00:59 +0200927 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200928 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200929 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200930 return DNS_RESP_VALID;
931}
932
Christopher Faulet67957bd2017-09-27 11:00:59 +0200933/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200934 * If existing IP not found, return the first IP matching family_priority,
935 * otherwise, first ip found
936 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200937 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200938 * For both cases above, dns_validate_dns_response is required
939 * returns one of the DNS_UPD_* code
940 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100941#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200942int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200943 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100944 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200945 void **newip, short *newip_sin_family,
946 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200947{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200948 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100949 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200950 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200951 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100952 int currentip_sel;
953 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100954 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200955
Christopher Faulet67957bd2017-09-27 11:00:59 +0200956 family_priority = dns_opts->family_prio;
957 *newip = newip4 = newip6 = NULL;
958 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200959 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200960 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200961
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100962 /* Select an IP regarding configuration preference.
963 * Top priority is the prefered network ip version,
964 * second priority is the prefered network.
965 * the last priority is the currently used IP,
966 *
967 * For these three priorities, a score is calculated. The
968 * weight are:
Baptistefc725902016-12-26 23:21:08 +0100969 * 8 - prefered netwok ip version.
970 * 4 - prefered network.
971 * 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 +0100972 * 1 - current ip.
973 * The result with the biggest score is returned.
974 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200975
976 list_for_each_entry(record, &dns_p->answer_list, list) {
977 void *ip;
978 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100979
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200980 if (record->type == DNS_RTYPE_A) {
981 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
982 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200983 }
984 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200985 ip_type = AF_INET6;
986 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200987 }
988 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200989 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100990 score = 0;
991
992 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200993 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +0100994 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100995
996 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +0200997 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100998
999 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001000 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001001 continue;
1002
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001003 if ((ip_type == AF_INET &&
1004 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001005 &dns_opts->pref_net[j].mask.in4,
1006 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001007 (ip_type == AF_INET6 &&
1008 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001009 &dns_opts->pref_net[j].mask.in6,
1010 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001011 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001012 break;
1013 }
1014 }
1015
Christopher Faulet67957bd2017-09-27 11:00:59 +02001016 /* Check if the IP found in the record is already affected to a
1017 * member of a group. If yes, the score should be incremented
1018 * by 2. */
1019 if (owner && snr_check_ip_callback(owner, ip, &ip_type))
1020 continue;
1021
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001022 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001023 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001024 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001025 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001026 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001027 !memcmp(ip, currentip, 16)))) {
1028 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001029 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001030 }
1031 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001032 currentip_sel = 0;
1033
1034 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001035 * score. The maximum score is 15, if this value is reached, we
1036 * break the parsing. Implicitly, this score is reached the ip
1037 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001038 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001039 if (ip_type == AF_INET)
1040 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001041 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001042 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001043 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001044 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001045 return DNS_UPD_NO;
1046 max_score = score;
1047 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001048 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001049
Christopher Faulet67957bd2017-09-27 11:00:59 +02001050 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001051 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001052 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001053
Christopher Faulet67957bd2017-09-27 11:00:59 +02001054 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001055 if (family_priority == AF_INET) {
1056 if (newip4) {
1057 *newip = newip4;
1058 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001059 }
1060 else if (newip6) {
1061 *newip = newip6;
1062 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001063 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001064 if (!currentip_found)
1065 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001066 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001067 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001068 else if (family_priority == AF_INET6) {
1069 if (newip6) {
1070 *newip = newip6;
1071 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001072 }
1073 else if (newip4) {
1074 *newip = newip4;
1075 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001076 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001077 if (!currentip_found)
1078 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001079 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001080 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 else if (family_priority == AF_UNSPEC) {
1082 if (newip6) {
1083 *newip = newip6;
1084 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001085 }
1086 else if (newip4) {
1087 *newip = newip4;
1088 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001089 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001090 if (!currentip_found)
1091 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001092 }
1093
Christopher Faulet67957bd2017-09-27 11:00:59 +02001094 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001095 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001096
Christopher Faulet67957bd2017-09-27 11:00:59 +02001097 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001098 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001099 /* Move the first record to the end of the list, for internal
1100 * round robin */
1101 LIST_DEL(&record->list);
1102 LIST_ADDQ(&dns_p->answer_list, &record->list);
1103 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001104 }
1105 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001106}
1107
Christopher Faulet67957bd2017-09-27 11:00:59 +02001108/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001109 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001110 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1111 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001113 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1114 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001115 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001116int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001117{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001118 char *ptr;
1119 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001120
Christopher Faulet67957bd2017-09-27 11:00:59 +02001121 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001122 return -1;
1123
Christopher Faulet67957bd2017-09-27 11:00:59 +02001124 ptr = str;
1125 for (i = 0; i < dn_len-1; ++i) {
1126 sz = dn[i];
1127 if (i)
1128 *ptr++ = '.';
1129 memcpy(ptr, dn+i+1, sz);
1130 ptr += sz;
1131 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001132 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001133 *ptr++ = '\0';
1134 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001135}
1136
Christopher Faulet67957bd2017-09-27 11:00:59 +02001137/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1138 *
1139 * <str> must be a null-terminated string. <str_len> must include the
1140 * terminating null byte. <dn> buffer must be allocated and its size must be
1141 * passed in <dn_len>.
1142 *
1143 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1144 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001145 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001146int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001147{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001148 int i, offset;
1149
Christopher Faulet67957bd2017-09-27 11:00:59 +02001150 if (dn_len < str_len + 1)
1151 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001152
Christopher Faulet67957bd2017-09-27 11:00:59 +02001153 /* First byte of dn will be used to store the length of the first
1154 * label */
1155 offset = 0;
1156 for (i = 0; i < str_len; ++i) {
1157 if (str[i] == '.') {
1158 /* 2 or more consecutive dots is invalid */
1159 if (i == offset)
1160 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001161
Christopher Faulet67957bd2017-09-27 11:00:59 +02001162 dn[offset] = (i - offset);
1163 offset = i+1;
1164 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001165 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001166 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001167 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001168 dn[offset] = (i - offset - 1);
1169 dn[i] = '\0';
1170 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001171}
1172
Christopher Faulet67957bd2017-09-27 11:00:59 +02001173/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001174 * - total size
1175 * - each label size individually
1176 * returns:
1177 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1178 * 1 when no error. <err> is left unaffected.
1179 */
1180int dns_hostname_validation(const char *string, char **err)
1181{
1182 const char *c, *d;
1183 int i;
1184
1185 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1186 if (err)
1187 *err = DNS_TOO_LONG_FQDN;
1188 return 0;
1189 }
1190
1191 c = string;
1192 while (*c) {
1193 d = c;
1194
1195 i = 0;
1196 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1197 i++;
1198 if (!((*d == '-') || (*d == '_') ||
1199 ((*d >= 'a') && (*d <= 'z')) ||
1200 ((*d >= 'A') && (*d <= 'Z')) ||
1201 ((*d >= '0') && (*d <= '9')))) {
1202 if (err)
1203 *err = DNS_INVALID_CHARACTER;
1204 return 0;
1205 }
1206 d++;
1207 }
1208
1209 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1210 if (err)
1211 *err = DNS_LABEL_TOO_LONG;
1212 return 0;
1213 }
1214
1215 if (*d == '\0')
1216 goto out;
1217
1218 c = ++d;
1219 }
1220 out:
1221 return 1;
1222}
1223
Christopher Faulet67957bd2017-09-27 11:00:59 +02001224/* Picks up an available resolution from the different resolution list
1225 * associated to a resolvers section, in this order:
1226 * 1. check in resolutions.curr for the same hostname and query_type
1227 * 2. check in resolutions.wait for the same hostname and query_type
1228 * 3. Get a new resolution from resolution pool
1229 *
1230 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001231 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001232static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1233 char **hostname_dn, int hostname_dn_len,
1234 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001235{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001236 struct dns_resolution *res;
1237
1238 if (!*hostname_dn)
1239 goto from_pool;
1240
1241 /* Search for same hostname and query type in resolutions.curr */
1242 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1243 if (!res->hostname_dn)
1244 continue;
1245 if ((query_type == res->prefered_query_type) &&
1246 hostname_dn_len == res->hostname_dn_len &&
1247 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1248 return res;
1249 }
1250
1251 /* Search for same hostname and query type in resolutions.wait */
1252 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1253 if (!res->hostname_dn)
1254 continue;
1255 if ((query_type == res->prefered_query_type) &&
1256 hostname_dn_len == res->hostname_dn_len &&
1257 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1258 return res;
1259 }
1260
1261 from_pool:
1262 /* No resolution could be found, so let's allocate a new one */
1263 res = pool_alloc2(dns_resolution_pool);
1264 if (res) {
1265 memset(res, 0, sizeof(*res));
1266 res->resolvers = resolvers;
1267 res->uuid = resolution_uuid;
1268 res->status = RSLV_STATUS_NONE;
1269 res->step = RSLV_STEP_NONE;
1270 res->last_valid = now_ms;
1271
1272 LIST_INIT(&res->requesters);
1273 LIST_INIT(&res->response.answer_list);
1274
1275 res->prefered_query_type = query_type;
1276 res->query_type = query_type;
1277 res->hostname_dn = *hostname_dn;
1278 res->hostname_dn_len = hostname_dn_len;
1279
1280 ++resolution_uuid;
1281
1282 /* Move the resolution to the resolvers wait queue */
1283 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1284 }
1285 return res;
1286}
1287
1288/* Releases a resolution from its requester(s) and move it back to the pool */
1289static void dns_free_resolution(struct dns_resolution *resolution)
1290{
1291 struct dns_requester *req, *reqback;
1292
1293 /* clean up configuration */
1294 dns_reset_resolution(resolution);
1295 resolution->hostname_dn = NULL;
1296 resolution->hostname_dn_len = 0;
1297
1298 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1299 LIST_DEL(&req->list);
1300 req->resolution = NULL;
1301 }
1302
1303 LIST_DEL(&resolution->list);
1304 pool_free2(dns_resolution_pool, resolution);
1305}
1306
1307/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1308 * on success, -1 otherwise.
1309 */
1310int dns_link_resolution(void *requester, int requester_type)
1311{
1312 struct dns_resolution *res = NULL;
1313 struct dns_requester *req;
1314 struct dns_resolvers *resolvers;
1315 struct server *srv = NULL;
1316 struct dns_srvrq *srvrq = NULL;
1317 char **hostname_dn;
1318 int hostname_dn_len, query_type;
1319
1320 switch (requester_type) {
1321 case OBJ_TYPE_SERVER:
1322 srv = (struct server *)requester;
1323 hostname_dn = &srv->hostname_dn;
1324 hostname_dn_len = srv->hostname_dn_len;
1325 resolvers = srv->resolvers;
1326 query_type = ((srv->dns_opts.family_prio == AF_INET)
1327 ? DNS_RTYPE_A
1328 : DNS_RTYPE_AAAA);
1329 break;
1330
1331 case OBJ_TYPE_SRVRQ:
1332 srvrq = (struct dns_srvrq *)requester;
1333 hostname_dn = &srvrq->hostname_dn;
1334 hostname_dn_len = srvrq->hostname_dn_len;
1335 resolvers = srvrq->resolvers;
1336 query_type = DNS_RTYPE_SRV;
1337 break;
1338
1339 default:
1340 goto err;
1341 }
1342
1343 /* Get a resolution from the resolvers' wait queue or pool */
1344 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1345 goto err;
1346
1347 if (srv) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01001348 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001349 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001350 if ((req = calloc(1, sizeof(*req))) == NULL) {
1351 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001352 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001353 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001354 req->owner = &srv->obj_type;
1355 srv->dns_requester = req;
1356 }
1357 else
1358 req = srv->dns_requester;
Emeric Brune9fd6b52017-11-02 17:20:39 +01001359 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001360 }
1361 else if (srvrq) {
1362 if (srvrq->dns_requester == NULL) {
1363 if ((req = calloc(1, sizeof(*req))) == NULL)
1364 goto err;
1365 req->owner = &srvrq->obj_type;
1366 srvrq->dns_requester = req;
1367 }
1368 else
1369 req = srvrq->dns_requester;
1370 }
1371 else
1372 goto err;
1373
1374 req->resolution = res;
1375 req->requester_cb = snr_resolution_cb;
1376 req->requester_error_cb = snr_resolution_error_cb;
1377
1378 LIST_ADDQ(&res->requesters, &req->list);
1379 return 0;
1380
1381 err:
1382 if (res && LIST_ISEMPTY(&res->requesters))
1383 dns_free_resolution(res);
1384 return -1;
1385}
1386
1387/* Removes a requester from a DNS resoltion. It takes takes care of all the
1388 * consequences. It also cleans up some parameters from the requester.
1389 */
1390void dns_unlink_resolution(struct dns_requester *requester)
1391{
1392 struct dns_resolution *res;
1393 struct dns_requester *req;
1394
1395 /* Nothing to do */
1396 if (!requester || !requester->resolution)
1397 return;
1398 res = requester->resolution;
1399
1400 /* Clean up the requester */
1401 LIST_DEL(&requester->list);
1402 requester->resolution = NULL;
1403
1404 /* We need to find another requester linked on this resolution */
1405 if (!LIST_ISEMPTY(&res->requesters))
1406 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1407 else {
1408 dns_free_resolution(res);
1409 return;
1410 }
1411
1412 /* Move hostname_dn related pointers to the next requester */
1413 switch (obj_type(req->owner)) {
1414 case OBJ_TYPE_SERVER:
1415 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1416 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1417 break;
1418 case OBJ_TYPE_SRVRQ:
1419 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1420 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1421 break;
1422 default:
1423 res->hostname_dn = NULL;
1424 res->hostname_dn_len = 0;
1425 break;
1426 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001427}
1428
Christopher Faulet67957bd2017-09-27 11:00:59 +02001429/* Called when a network IO is generated on a name server socket for an incoming
1430 * packet. It performs the following actions:
1431 * - check if the packet requires processing (not outdated resolution)
1432 * - ensure the DNS packet received is valid and call requester's callback
1433 * - call requester's error callback if invalid response
1434 * - check the dn_name in the packet against the one sent
1435 */
1436static void dns_resolve_recv(struct dgram_conn *dgram)
1437{
1438 struct dns_nameserver *ns, *tmpns;
1439 struct dns_resolvers *resolvers;
1440 struct dns_resolution *res;
1441 struct dns_query_item *query;
1442 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1443 unsigned char *bufend;
1444 int fd, buflen, dns_resp;
1445 int max_answer_records;
1446 unsigned short query_id;
1447 struct eb32_node *eb;
1448 struct dns_requester *req;
1449
1450 fd = dgram->t.sock.fd;
1451
1452 /* check if ready for reading */
1453 if (!fd_recv_ready(fd))
1454 return;
1455
1456 /* no need to go further if we can't retrieve the nameserver */
1457 if ((ns = dgram->owner) == NULL)
1458 return;
1459
1460 resolvers = ns->resolvers;
Christopher Fauletb2812a62017-10-04 16:17:58 +02001461 SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001462
1463 /* process all pending input messages */
1464 while (1) {
1465 /* read message received */
1466 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1467 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1468 /* FIXME : for now we consider EAGAIN only */
1469 fd_cant_recv(fd);
1470 break;
1471 }
1472
1473 /* message too big */
1474 if (buflen > resolvers->accepted_payload_size) {
1475 ns->counters.too_big++;
1476 continue;
1477 }
1478
1479 /* initializing variables */
1480 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1481
1482 /* read the query id from the packet (16 bits) */
1483 if (buf + 2 > bufend) {
1484 ns->counters.invalid++;
1485 continue;
1486 }
1487 query_id = dns_response_get_query_id(buf);
1488
1489 /* search the query_id in the pending resolution tree */
1490 eb = eb32_lookup(&resolvers->query_ids, query_id);
1491 if (eb == NULL) {
1492 /* unknown query id means an outdated response and can be safely ignored */
1493 ns->counters.outdated++;
1494 continue;
1495 }
1496
1497 /* known query id means a resolution in prgress */
1498 res = eb32_entry(eb, struct dns_resolution, qid);
1499 if (!res) {
1500 ns->counters.outdated++;
1501 continue;
1502 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001503
Christopher Faulet67957bd2017-09-27 11:00:59 +02001504 /* number of responses received */
1505 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001506
Christopher Faulet67957bd2017-09-27 11:00:59 +02001507 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1508 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001509
Christopher Faulet67957bd2017-09-27 11:00:59 +02001510 switch (dns_resp) {
1511 case DNS_RESP_VALID:
1512 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001513
Christopher Faulet67957bd2017-09-27 11:00:59 +02001514 case DNS_RESP_INVALID:
1515 case DNS_RESP_QUERY_COUNT_ERROR:
1516 case DNS_RESP_WRONG_NAME:
1517 res->status = RSLV_STATUS_INVALID;
1518 ns->counters.invalid++;
1519 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001520
Christopher Faulet67957bd2017-09-27 11:00:59 +02001521 case DNS_RESP_NX_DOMAIN:
1522 res->status = RSLV_STATUS_NX;
1523 ns->counters.nx++;
1524 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001525
Christopher Faulet67957bd2017-09-27 11:00:59 +02001526 case DNS_RESP_REFUSED:
1527 res->status = RSLV_STATUS_REFUSED;
1528 ns->counters.refused++;
1529 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001530
Christopher Faulet67957bd2017-09-27 11:00:59 +02001531 case DNS_RESP_ANCOUNT_ZERO:
1532 res->status = RSLV_STATUS_OTHER;
1533 ns->counters.any_err++;
1534 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001535
Christopher Faulet67957bd2017-09-27 11:00:59 +02001536 case DNS_RESP_CNAME_ERROR:
1537 res->status = RSLV_STATUS_OTHER;
1538 ns->counters.cname_error++;
1539 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001540
Christopher Faulet67957bd2017-09-27 11:00:59 +02001541 case DNS_RESP_TRUNCATED:
1542 res->status = RSLV_STATUS_OTHER;
1543 ns->counters.truncated++;
1544 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001545
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 case DNS_RESP_NO_EXPECTED_RECORD:
1547 case DNS_RESP_ERROR:
1548 case DNS_RESP_INTERNAL:
1549 res->status = RSLV_STATUS_OTHER;
1550 ns->counters.other++;
1551 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001552 }
1553
Christopher Faulet67957bd2017-09-27 11:00:59 +02001554 /* Wait all nameservers response to handle errors */
1555 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1556 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001557
Christopher Faulet67957bd2017-09-27 11:00:59 +02001558 /* Process error codes */
1559 if (dns_resp != DNS_RESP_VALID) {
1560 if (res->prefered_query_type != res->query_type) {
1561 /* The fallback on the query type was already performed,
1562 * so check the try counter. If it falls to 0, we can
1563 * report an error. Else, wait the next attempt. */
1564 if (!res->try)
1565 goto report_res_error;
1566 }
1567 else {
1568 /* Fallback from A to AAAA or the opposite and re-send
1569 * the resolution immediately. try counter is not
1570 * decremented. */
1571 if (res->prefered_query_type == DNS_RTYPE_A) {
1572 res->query_type = DNS_RTYPE_AAAA;
1573 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001574 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001575 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1576 res->query_type = DNS_RTYPE_A;
1577 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001578 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001579 }
1580 continue;
1581 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001582
Christopher Faulet67957bd2017-09-27 11:00:59 +02001583 /* Now let's check the query's dname corresponds to the one we
1584 * sent. We can check only the first query of the list. We send
1585 * one query at a time so we get one query in the response */
1586 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1587 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1588 dns_resp = DNS_RESP_WRONG_NAME;
1589 ns->counters.other++;
1590 goto report_res_error;
1591 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001592
Christopher Faulet67957bd2017-09-27 11:00:59 +02001593 /* So the resolution succeeded */
1594 res->status = RSLV_STATUS_VALID;
1595 res->last_valid = now_ms;
1596 ns->counters.valid++;
1597 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001598
Christopher Faulet67957bd2017-09-27 11:00:59 +02001599 report_res_error:
1600 list_for_each_entry(req, &res->requesters, list)
1601 req->requester_error_cb(req, dns_resp);
1602 dns_reset_resolution(res);
1603 LIST_DEL(&res->list);
1604 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1605 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001606
Christopher Faulet67957bd2017-09-27 11:00:59 +02001607 report_res_success:
1608 /* Only the 1rst requester s managed by the server, others are
1609 * from the cache */
1610 tmpns = ns;
1611 list_for_each_entry(req, &res->requesters, list) {
1612 req->requester_cb(req, tmpns);
1613 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001614 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001615
Christopher Faulet67957bd2017-09-27 11:00:59 +02001616 dns_reset_resolution(res);
1617 LIST_DEL(&res->list);
1618 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1619 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001620 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001621 dns_update_resolvers_timeout(resolvers);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001622 SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001623}
William Lallemand69e96442016-11-19 00:58:54 +01001624
Christopher Faulet67957bd2017-09-27 11:00:59 +02001625/* Called when a resolvers network socket is ready to send data */
1626static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001627{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001628 struct dns_resolvers *resolvers;
1629 struct dns_nameserver *ns;
1630 struct dns_resolution *res;
1631 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001632
Christopher Faulet67957bd2017-09-27 11:00:59 +02001633 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001634
Christopher Faulet67957bd2017-09-27 11:00:59 +02001635 /* check if ready for sending */
1636 if (!fd_send_ready(fd))
1637 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001638
Christopher Faulet67957bd2017-09-27 11:00:59 +02001639 /* we don't want/need to be waked up any more for sending */
1640 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001641
Christopher Faulet67957bd2017-09-27 11:00:59 +02001642 /* no need to go further if we can't retrieve the nameserver */
1643 if ((ns = dgram->owner) == NULL)
1644 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001645
Christopher Faulet67957bd2017-09-27 11:00:59 +02001646 resolvers = ns->resolvers;
Christopher Fauletb2812a62017-10-04 16:17:58 +02001647 SPIN_LOCK(DNS_LOCK, &resolvers->lock);
1648
Christopher Faulet67957bd2017-09-27 11:00:59 +02001649 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1650 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001651
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652 if (res->nb_queries == resolvers->nb_nameservers)
1653 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001654
Christopher Faulet67957bd2017-09-27 11:00:59 +02001655 trash.len = dns_build_query(res->query_id, res->query_type,
1656 resolvers->accepted_payload_size,
1657 res->hostname_dn, res->hostname_dn_len,
1658 trash.str, trash.size);
1659 if (trash.len == -1)
1660 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001661
Christopher Faulet67957bd2017-09-27 11:00:59 +02001662 ret = send(fd, trash.str, trash.len, 0);
1663 if (ret != trash.len)
1664 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001665
Christopher Faulet67957bd2017-09-27 11:00:59 +02001666 ns->counters.sent++;
1667 res->nb_queries++;
1668 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001669
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670 snd_error:
1671 ns->counters.snd_error++;
1672 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001673 }
Christopher Fauletb2812a62017-10-04 16:17:58 +02001674 SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001676
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677/* Processes DNS resolution. First, it checks the active list to detect expired
1678 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1679 * checks the wait list to trigger new resolutions.
1680 */
1681static struct task *dns_process_resolvers(struct task *t)
1682{
1683 struct dns_resolvers *resolvers = t->context;
1684 struct dns_resolution *res, *resback;
1685 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001686
Christopher Fauletb2812a62017-10-04 16:17:58 +02001687 SPIN_LOCK(DNS_LOCK, &resolvers->lock);
1688
Christopher Faulet67957bd2017-09-27 11:00:59 +02001689 /* Handle all expired resolutions from the active list */
1690 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1691 /* When we find the first resolution in the future, then we can
1692 * stop here */
1693 exp = tick_add(res->last_query, resolvers->timeout.retry);
1694 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001695 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001696
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697 /* If current resolution has been tried too many times and
1698 * finishes in timeout we update its status and remove it from
1699 * the list */
1700 if (!res->try) {
1701 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001702
Christopher Faulet67957bd2017-09-27 11:00:59 +02001703 /* Notify the result to the requesters */
1704 if (!res->nb_responses)
1705 res->status = RSLV_STATUS_TIMEOUT;
1706 list_for_each_entry(req, &res->requesters, list)
1707 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001708
Christopher Faulet67957bd2017-09-27 11:00:59 +02001709 /* Clean up resolution info and remove it from the
1710 * current list */
1711 dns_reset_resolution(res);
1712 LIST_DEL(&res->list);
1713 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001714
Christopher Faulet67957bd2017-09-27 11:00:59 +02001715 /* This might be triggered by too big UDP packets
1716 * dropped somewhere on the network, so lowering the
1717 * accepted_payload_size announced */
1718 if (resolvers->accepted_payload_size > 1280)
1719 resolvers->accepted_payload_size = 1280;
William Lallemand69e96442016-11-19 00:58:54 +01001720 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001721 else {
1722 /* Otherwise resend the DNS query and requeue the resolution */
1723 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1724 /* No response received (a real timeout) or fallback already done */
1725 res->query_type = res->prefered_query_type;
1726 res->try--;
1727 }
1728 else {
1729 /* Fallback from A to AAAA or the opposite and re-send
1730 * the resolution immediately. try counter is not
1731 * decremented. */
1732 if (res->prefered_query_type == DNS_RTYPE_A)
1733 res->query_type = DNS_RTYPE_AAAA;
1734 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1735 res->query_type = DNS_RTYPE_A;
1736 else
1737 res->try--;
1738 }
1739 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001740 }
1741 }
William Lallemand69e96442016-11-19 00:58:54 +01001742
Christopher Faulet67957bd2017-09-27 11:00:59 +02001743 /* Handle all resolutions in the wait list */
1744 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1745 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1746 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1747 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001748
Christopher Faulet67957bd2017-09-27 11:00:59 +02001749 if (dns_run_resolution(res) != 1) {
1750 res->last_resolution = now_ms;
1751 LIST_DEL(&res->list);
1752 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001753 }
1754 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001755
Christopher Faulet67957bd2017-09-27 11:00:59 +02001756 dns_update_resolvers_timeout(resolvers);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001757 SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001758 return t;
1759}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001760
Christopher Faulet67957bd2017-09-27 11:00:59 +02001761/* proto_udp callback functions for a DNS resolution */
1762struct dgram_data_cb resolve_dgram_cb = {
1763 .recv = dns_resolve_recv,
1764 .send = dns_resolve_send,
1765};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001766
Christopher Faulet67957bd2017-09-27 11:00:59 +02001767/* Release memory allocated by DNS */
1768static void dns_deinit(void)
1769{
1770 struct dns_resolvers *resolvers, *resolversback;
1771 struct dns_nameserver *ns, *nsback;
1772 struct dns_resolution *res, *resback;
1773 struct dns_requester *req, *reqback;
1774 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001775
Christopher Faulet67957bd2017-09-27 11:00:59 +02001776 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1777 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1778 free(ns->id);
1779 free((char *)ns->conf.file);
1780 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1781 fd_delete(ns->dgram->t.sock.fd);
1782 free(ns->dgram);
1783 LIST_DEL(&ns->list);
1784 free(ns);
1785 }
1786
1787 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1788 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1789 LIST_DEL(&req->list);
1790 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001791 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001792 dns_free_resolution(res);
1793 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001794
Christopher Faulet67957bd2017-09-27 11:00:59 +02001795 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1796 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1797 LIST_DEL(&req->list);
1798 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001799 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001800 dns_free_resolution(res);
1801 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001802
Christopher Faulet67957bd2017-09-27 11:00:59 +02001803 free(resolvers->id);
1804 free((char *)resolvers->conf.file);
1805 task_delete(resolvers->t);
1806 task_free(resolvers->t);
1807 LIST_DEL(&resolvers->list);
1808 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001809 }
1810
Christopher Faulet67957bd2017-09-27 11:00:59 +02001811 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1812 free(srvrq->name);
1813 free(srvrq->hostname_dn);
1814 LIST_DEL(&srvrq->list);
1815 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001816 }
1817
Christopher Faulet67957bd2017-09-27 11:00:59 +02001818 pool_destroy2(dns_answer_item_pool);
1819 pool_destroy2(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001820}
1821
Christopher Faulet67957bd2017-09-27 11:00:59 +02001822/* Finalizes the DNS configuration by allocating required resources and checking
1823 * live parameters.
1824 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001825 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001826static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001827{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001828 struct dns_resolvers *resolvers;
1829 struct proxy *px;
1830 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001831
Christopher Faulet67957bd2017-09-27 11:00:59 +02001832 /* allocate pool of resolution per resolvers */
1833 list_for_each_entry(resolvers, &dns_resolvers, list) {
1834 struct dns_nameserver *ns;
1835 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001836
Christopher Faulet67957bd2017-09-27 11:00:59 +02001837 /* Check if we can create the socket with nameservers info */
1838 list_for_each_entry(ns, &resolvers->nameservers, list) {
1839 struct dgram_conn *dgram = NULL;
1840 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001841
Christopher Faulet67957bd2017-09-27 11:00:59 +02001842 /* Check nameserver info */
1843 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
1844 Alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1845 resolvers->id, ns->id);
1846 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001847 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001848 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001849 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
1850 Alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1851 resolvers->id, ns->id);
1852 close(fd);
1853 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001854 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001855 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001856 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001857
Christopher Faulet67957bd2017-09-27 11:00:59 +02001858 /* Create dgram structure that will hold the UPD socket
1859 * and attach it on the current nameserver */
1860 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
1861 Alert("config: resolvers '%s' : out of memory.\n",
1862 resolvers->id);
1863 err_code |= (ERR_ALERT|ERR_ABORT);
1864 goto err;
1865 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 /* Leave dgram partially initialized, no FD attached for
1868 * now. */
1869 dgram->owner = ns;
1870 dgram->data = &resolve_dgram_cb;
1871 dgram->t.sock.fd = -1;
1872 ns->dgram = dgram;
1873 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001874
Christopher Faulet67957bd2017-09-27 11:00:59 +02001875 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001876 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001877 Alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
1878 err_code |= (ERR_ALERT|ERR_ABORT);
1879 goto err;
1880 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001881
Christopher Faulet67957bd2017-09-27 11:00:59 +02001882 /* Update task's parameters */
1883 t->process = dns_process_resolvers;
1884 t->context = resolvers;
1885 resolvers->t = t;
1886 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001887 }
1888
Christopher Faulet67957bd2017-09-27 11:00:59 +02001889 for (px = proxy; px; px = px->next) {
1890 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001891
Christopher Faulet67957bd2017-09-27 11:00:59 +02001892 for (srv = px->srv; srv; srv = srv->next) {
1893 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001894
Christopher Faulet67957bd2017-09-27 11:00:59 +02001895 if (!srv->resolvers_id)
1896 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001897
Christopher Faulet67957bd2017-09-27 11:00:59 +02001898 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
1899 Alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1900 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
1901 err_code |= (ERR_ALERT|ERR_ABORT);
1902 continue;
1903 }
1904 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001905
Christopher Faulet67957bd2017-09-27 11:00:59 +02001906 if (srv->srvrq && !srv->srvrq->resolvers) {
1907 srv->srvrq->resolvers = srv->resolvers;
1908 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ) == -1) {
1909 Alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1910 proxy_type_str(px), px->id, srv->id);
1911 err_code |= (ERR_ALERT|ERR_ABORT);
1912 continue;
1913 }
1914 }
1915 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1) {
1916 Alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1917 proxy_type_str(px), px->id, srv->id);
1918 err_code |= (ERR_ALERT|ERR_ABORT);
1919 continue;
1920 }
1921 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001922 }
1923
Christopher Faulet67957bd2017-09-27 11:00:59 +02001924 if (err_code & (ERR_ALERT|ERR_ABORT))
1925 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001926
Christopher Faulet67957bd2017-09-27 11:00:59 +02001927 return err_code;
1928 err:
1929 dns_deinit();
1930 return err_code;
1931
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001932}
1933
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
1935static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001936{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001937 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001938
Christopher Faulet67957bd2017-09-27 11:00:59 +02001939 if (*args[2]) {
1940 list_for_each_entry(presolvers, &dns_resolvers, list) {
1941 if (strcmp(presolvers->id, args[2]) == 0) {
1942 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001943 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001944 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001945 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001946 if (appctx->ctx.cli.p0 == NULL) {
1947 appctx->ctx.cli.severity = LOG_ERR;
1948 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1949 appctx->st0 = CLI_ST_PRINT;
1950 return 1;
1951 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001952 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001953 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001954}
1955
Christopher Faulet67957bd2017-09-27 11:00:59 +02001956/* Dumps counters from all resolvers section and associated name servers. It
1957 * returns 0 if the output buffer is full and it needs to be called again,
1958 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001959 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001960 */
1961static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1962{
1963 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001964 struct dns_resolvers *resolvers;
1965 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001966
1967 chunk_reset(&trash);
1968
1969 switch (appctx->st2) {
1970 case STAT_ST_INIT:
1971 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1972 /* fall through */
1973
1974 case STAT_ST_LIST:
1975 if (LIST_ISEMPTY(&dns_resolvers)) {
1976 chunk_appendf(&trash, "No resolvers found\n");
1977 }
1978 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001979 list_for_each_entry(resolvers, &dns_resolvers, list) {
1980 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01001981 continue;
1982
Christopher Faulet67957bd2017-09-27 11:00:59 +02001983 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
1984 list_for_each_entry(ns, &resolvers->nameservers, list) {
1985 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
1986 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
1987 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
1988 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
1989 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
1990 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
1991 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
1992 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
1993 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
1994 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
1995 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
1996 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
1997 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
1998 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
1999 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2000 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002001 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002003 }
2004 }
2005
2006 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002007 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002008 /* let's try again later from this session. We add ourselves into
2009 * this session's users so that it can remove us upon termination.
2010 */
2011 si->flags |= SI_FL_WAIT_ROOM;
2012 return 0;
2013 }
2014
2015 appctx->st2 = STAT_ST_FIN;
2016 /* fall through */
2017
2018 default:
2019 appctx->st2 = STAT_ST_FIN;
2020 return 1;
2021 }
2022}
2023
2024/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002025static struct cli_kw_list cli_kws = {{ }, {
2026 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2027 " associated name servers",
2028 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2029 {{},}
2030 }
2031};
William Lallemand69e96442016-11-19 00:58:54 +01002032
2033
2034__attribute__((constructor))
2035static void __dns_init(void)
2036{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002037 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2038 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2039
Christopher Faulet67957bd2017-09-27 11:00:59 +02002040 hap_register_post_check(dns_finalize_config);
2041 hap_register_post_deinit(dns_deinit);
2042
William Lallemand69e96442016-11-19 00:58:54 +01002043 cli_register_kw(&cli_kws);
2044}