blob: a957710edb2f92e31069844a15338cc8834e8402 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Christopher Faulet67957bd2017-09-27 11:00:59 +020022#include <common/errors.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020023#include <common/time.h>
24#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020025#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020026
William Lallemand69e96442016-11-19 00:58:54 +010027#include <types/applet.h>
28#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020029#include <types/global.h>
30#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010031#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032
William Lallemand69e96442016-11-19 00:58:54 +010033#include <proto/channel.h>
34#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035#include <proto/checks.h>
36#include <proto/dns.h>
37#include <proto/fd.h>
38#include <proto/log.h>
39#include <proto/server.h>
40#include <proto/task.h>
41#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020042#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010043#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020044
Christopher Faulet67957bd2017-09-27 11:00:59 +020045struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
46struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020047
Christopher Fauletb2812a62017-10-04 16:17:58 +020048static THREAD_LOCAL int64_t dns_query_id_seed = 0; /* random seed */
Christopher Faulet67957bd2017-09-27 11:00:59 +020049static struct pool_head *dns_answer_item_pool = NULL;
50static struct pool_head *dns_resolution_pool = NULL;
51static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020052
Christopher Faulet67957bd2017-09-27 11:00:59 +020053/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
54 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020055 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020056struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020057{
Christopher Faulet67957bd2017-09-27 11:00:59 +020058 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059
Christopher Faulet67957bd2017-09-27 11:00:59 +020060 list_for_each_entry(res, &dns_resolvers, list) {
61 if (!strcmp(res->id, id))
62 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020064 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065}
66
Christopher Faulet67957bd2017-09-27 11:00:59 +020067/* Returns a pointer on the SRV request matching the name <name> for the proxy
68 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020070struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071{
Christopher Faulet67957bd2017-09-27 11:00:59 +020072 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073
Christopher Faulet67957bd2017-09-27 11:00:59 +020074 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
75 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
76 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020078 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020079}
80
Christopher Faulet67957bd2017-09-27 11:00:59 +020081/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
82 * NULL if an error occurred. */
83struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020084{
Christopher Faulet67957bd2017-09-27 11:00:59 +020085 struct proxy *px = srv->proxy;
86 struct dns_srvrq *srvrq = NULL;
87 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020088
Christopher Faulet67957bd2017-09-27 11:00:59 +020089 fqdn_len = strlen(fqdn);
90 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.str, trash.size);
91 if (hostname_dn_len == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010092 ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
93 proxy_type_str(px), px->id, srv->id, fqdn);
Christopher Faulet67957bd2017-09-27 11:00:59 +020094 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020095 }
96
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010098 ha_alert("config : %s '%s', server '%s': out of memory\n",
99 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200100 goto err;
101 }
102 srvrq->obj_type = OBJ_TYPE_SRVRQ;
103 srvrq->proxy = px;
104 srvrq->name = strdup(fqdn);
105 srvrq->hostname_dn = strdup(trash.str);
106 srvrq->hostname_dn_len = hostname_dn_len;
107 if (!srvrq->name || !srvrq->hostname_dn) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100108 ha_alert("config : %s '%s', server '%s': out of memory\n",
109 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200110 goto err;
111 }
112 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
113 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200114
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 err:
116 if (srvrq) {
117 free(srvrq->name);
118 free(srvrq->hostname_dn);
119 free(srvrq);
120 }
121 return NULL;
122}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200123
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200124
Christopher Faulet67957bd2017-09-27 11:00:59 +0200125/* 2 bytes random generator to generate DNS query ID */
126static inline uint16_t dns_rnd16(void)
127{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200128 if (!dns_query_id_seed)
129 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130 dns_query_id_seed ^= dns_query_id_seed << 13;
131 dns_query_id_seed ^= dns_query_id_seed >> 7;
132 dns_query_id_seed ^= dns_query_id_seed << 17;
133 return dns_query_id_seed;
134}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200135
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136
Christopher Faulet67957bd2017-09-27 11:00:59 +0200137static inline int dns_resolution_timeout(struct dns_resolution *res)
138{
139 switch (res->status) {
140 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
141 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200142 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200143}
144
Christopher Faulet67957bd2017-09-27 11:00:59 +0200145/* Updates a resolvers' task timeout for next wake up and queue it */
146static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200147{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200148 struct dns_resolution *res;
149 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151 next = tick_add(now_ms, resolvers->timeout.resolve);
152 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
153 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
154 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
155 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200156
Christopher Faulet67957bd2017-09-27 11:00:59 +0200157 list_for_each_entry(res, &resolvers->resolutions.wait, list)
158 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200159
Christopher Faulet67957bd2017-09-27 11:00:59 +0200160 resolvers->t->expire = next;
161 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200162}
163
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
165 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200166 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 struct dgram_conn *dgram = ns->dgram;
170 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172 /* Already connected */
173 if (dgram->t.sock.fd != -1)
174 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200175
Christopher Faulet67957bd2017-09-27 11:00:59 +0200176 /* Create an UDP socket and connect it on the nameserver's IP/Port */
177 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
178 send_log(NULL, LOG_WARNING,
179 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
180 ns->resolvers->id, ns->id);
181 return -1;
182 }
183 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
184 send_log(NULL, LOG_WARNING,
185 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
186 ns->resolvers->id, ns->id);
187 close(fd);
188 return -1;
189 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200190
Christopher Faulet67957bd2017-09-27 11:00:59 +0200191 /* Make the socket non blocking */
192 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200193
Christopher Faulet67957bd2017-09-27 11:00:59 +0200194 /* Add the fd in the fd list and update its parameters */
195 dgram->t.sock.fd = fd;
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) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100489 HA_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 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100501 HA_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);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100507 pool_free(dns_answer_item_pool, item);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200508 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) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100521 HA_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)) {
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100525 int ha_weight;
526
527 /* Make sure weight is at least 1, so
528 * that the server will be used.
529 */
530 ha_weight = item->weight / 256 + 1;
531 if (srv->uweight != ha_weight) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200532 char weight[9];
533
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100534 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200535 server_parse_weight_change_request(srv, weight);
536 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100537 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200538 break;
539 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100540 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200541 }
542 if (srv)
543 continue;
544
545 /* If not, try to find a server with undefined hostname */
546 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200548 if (srv->srvrq == srvrq && !srv->hostname_dn)
549 break;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100550 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200551 }
552 /* And update this server, if found */
553 if (srv) {
554 const char *msg = NULL;
555 char weight[9];
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100556 int ha_weight;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200557 char hostname[DNS_MAX_NAME_SIZE];
558
559 if (dns_dn_label_to_str(item->target, item->data_len+1,
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100560 hostname, DNS_MAX_NAME_SIZE) == -1) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100561 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200562 continue;
Olivier Houchard55dcdf42017-11-06 15:15:04 +0100563 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100564 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200565 if (msg)
566 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
567
568 srv->svc_port = item->port;
569 srv->flags &= ~SRV_F_MAPPORTS;
570 if ((srv->check.state & CHK_ST_CONFIGURED) &&
571 !(srv->flags & SRV_F_CHECKPORT))
572 srv->check.port = item->port;
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100573
574 /* Make sure weight is at least 1, so
575 * that the server will be used.
576 */
577 ha_weight = item->weight / 256 + 1;
578
579 snprintf(weight, sizeof(weight), "%d", ha_weight);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200580 server_parse_weight_change_request(srv, weight);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100581 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200582 }
583 }
584 }
585}
586
587/* Validates that the buffer DNS response provided in <resp> and finishing
588 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200589 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200590 * The result is stored in <resolution>' response, buf_response,
591 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200592 *
593 * This function returns one of the DNS_RESP_* code to indicate the type of
594 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200595 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200596static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
597 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200598{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200599 unsigned char *reader;
600 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200601 int len, flags, offset;
602 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200603 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200604 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200605 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200606 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200607 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200608
Christopher Faulet67957bd2017-09-27 11:00:59 +0200609 reader = resp;
610 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200611 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200612 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200613
Christopher Faulet67957bd2017-09-27 11:00:59 +0200614 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200615 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200616
617 /* query id */
618 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200619 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200620 dns_p->header.id = reader[0] * 256 + reader[1];
621 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200622
Christopher Faulet67957bd2017-09-27 11:00:59 +0200623 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200624 * First byte contains:
625 * - response flag (1 bit)
626 * - opcode (4 bits)
627 * - authoritative (1 bit)
628 * - truncated (1 bit)
629 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200630 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200631 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200632 return DNS_RESP_INVALID;
633
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200634 flags = reader[0] * 256 + reader[1];
635
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200636 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
637 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200638 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200639 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200640 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200641 return DNS_RESP_ERROR;
642 }
643
Christopher Faulet67957bd2017-09-27 11:00:59 +0200644 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200645 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200646
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200647 /* 2 bytes for question count */
648 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200649 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200650 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200651 /* (for now) we send one query only, so we expect only one in the
652 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200653 if (dns_p->header.qdcount != 1)
654 return DNS_RESP_QUERY_COUNT_ERROR;
655 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200656 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200657 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200658
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200659 /* 2 bytes for answer count */
660 if (reader + 2 >= bufend)
661 return DNS_RESP_INVALID;
662 dns_p->header.ancount = reader[0] * 256 + reader[1];
663 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200664 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200665 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200666 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200667 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200668 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200669
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200670 /* 2 bytes authority count */
671 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200672 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200673 dns_p->header.nscount = reader[0] * 256 + reader[1];
674 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200675
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200676 /* 2 bytes additional count */
677 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200678 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200679 dns_p->header.arcount = reader[0] * 256 + reader[1];
680 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200681
Christopher Faulet67957bd2017-09-27 11:00:59 +0200682 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200683 LIST_INIT(&dns_p->query_list);
684 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 +0200685 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200686 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200687 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200688 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
689 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200690 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200691 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200692
Christopher Faulet67957bd2017-09-27 11:00:59 +0200693 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200694 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200695 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200696 offset = 0;
697 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200698
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200699 if (len == 0)
700 return DNS_RESP_INVALID;
701
702 reader += offset;
703 previous_dname = dns_query->name;
704
705 /* move forward 2 bytes for question type */
706 if (reader + 2 >= bufend)
707 return DNS_RESP_INVALID;
708 dns_query->type = reader[0] * 256 + reader[1];
709 reader += 2;
710
711 /* move forward 2 bytes for question class */
712 if (reader + 2 >= bufend)
713 return DNS_RESP_INVALID;
714 dns_query->class = reader[0] * 256 + reader[1];
715 reader += 2;
716 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200717
Baptiste Assmann251abb92017-08-11 09:58:27 +0200718 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200719 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200720 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
721 return DNS_RESP_TRUNCATED;
722
Baptiste Assmann325137d2015-04-13 23:40:55 +0200723 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200724 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200725 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200726 if (reader >= bufend)
727 return DNS_RESP_INVALID;
728
Willy Tarreaubafbe012017-11-24 17:34:44 +0100729 dns_answer_record = pool_alloc(dns_answer_item_pool);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200730 if (dns_answer_record == NULL)
731 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200732
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200733 offset = 0;
734 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200735
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200736 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100737 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200738 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200739 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200740
Christopher Faulet67957bd2017-09-27 11:00:59 +0200741 /* Check if the current record dname is valid. previous_dname
742 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200743 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100744 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200745 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200746 /* First record, means a mismatch issue between
747 * queried dname and dname found in the first
748 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200749 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200750 }
751 else {
752 /* If not the first record, this means we have a
753 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200754 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200755 }
756
Baptiste Assmann325137d2015-04-13 23:40:55 +0200757 }
758
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200759 memcpy(dns_answer_record->name, tmpname, len);
760 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200761
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200762 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200763 if (reader >= bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100764 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200765 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200766 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200768 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200769 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100770 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200771 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200772 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200773 dns_answer_record->type = reader[0] * 256 + reader[1];
774 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200775
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200776 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200777 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100778 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200780 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200781 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200782 reader += 2;
783
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200784 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200785 if (reader + 4 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100786 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200787 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200788 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200789 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
790 + reader[2] * 256 + reader[3];
791 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200792
Christopher Faulet67957bd2017-09-27 11:00:59 +0200793 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200794 if (reader + 2 > bufend) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100795 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200796 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200797 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200798 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200799
Christopher Faulet67957bd2017-09-27 11:00:59 +0200800 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200801 reader += 2;
802
Christopher Faulet67957bd2017-09-27 11:00:59 +0200803 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200805 case DNS_RTYPE_A:
806 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200807 if (dns_answer_record->data_len != 4) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100808 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200809 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200810 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200811 dns_answer_record->address.sa_family = AF_INET;
812 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
813 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200814 break;
815
816 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200817 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200818 * no IP could be found and last record was a CNAME. Could be triggered
819 * by a wrong query type
820 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200821 * + 1 because dns_answer_record_id starts at 0
822 * while number of answers is an integer and
823 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200824 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200825 if (i + 1 == dns_p->header.ancount) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100826 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200827 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200828 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200829
830 offset = 0;
831 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200832 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100833 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200834 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200835 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200836
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200837 memcpy(dns_answer_record->target, tmpname, len);
838 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200839 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200840 break;
841
Olivier Houchard8da5f982017-08-04 18:35:36 +0200842
843 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200844 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200845 * - 2 bytes for the priority
846 * - 2 bytes for the weight
847 * - 2 bytes for the port
848 * - the target hostname
849 */
850 if (dns_answer_record->data_len <= 6) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100851 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200852 return DNS_RESP_INVALID;
853 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200854 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200855 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200856 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200857 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200858 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200859 reader += sizeof(uint16_t);
860 offset = 0;
861 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
862 if (len == 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100863 pool_free(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200864 return DNS_RESP_INVALID;
865 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200866 dns_answer_record->data_len = len;
867 memcpy(dns_answer_record->target, tmpname, len);
868 dns_answer_record->target[len] = 0;
869 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200870
Baptiste Assmann325137d2015-04-13 23:40:55 +0200871 case DNS_RTYPE_AAAA:
872 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200873 if (dns_answer_record->data_len != 16) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100874 pool_free(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200875 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200876 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200877 dns_answer_record->address.sa_family = AF_INET6;
878 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
879 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200880 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200881
Baptiste Assmann325137d2015-04-13 23:40:55 +0200882 } /* switch (record type) */
883
Christopher Faulet67957bd2017-09-27 11:00:59 +0200884 /* Increment the counter for number of records saved into our
885 * local response */
886 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200887
Christopher Faulet67957bd2017-09-27 11:00:59 +0200888 /* Move forward dns_answer_record->data_len for analyzing next
889 * record in the response */
890 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
891 ? offset
892 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200893
894 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200895 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200896 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
897 if (tmp_record->type != dns_answer_record->type)
898 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200899
900 switch(tmp_record->type) {
901 case DNS_RTYPE_A:
902 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
903 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
904 sizeof(in_addr_t)))
905 found = 1;
906 break;
907
908 case DNS_RTYPE_AAAA:
909 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
910 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
911 sizeof(struct in6_addr)))
912 found = 1;
913 break;
914
Olivier Houchard8da5f982017-08-04 18:35:36 +0200915 case DNS_RTYPE_SRV:
916 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200917 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200918 dns_answer_record->port == tmp_record->port) {
919 tmp_record->weight = dns_answer_record->weight;
920 found = 1;
921 }
922 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200923
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200924 default:
925 break;
926 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200927
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200928 if (found == 1)
929 break;
930 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200931
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200932 if (found == 1) {
933 tmp_record->last_seen = now.tv_sec;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100934 pool_free(dns_answer_item_pool, dns_answer_record);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200935 }
936 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200937 dns_answer_record->last_seen = now.tv_sec;
938 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
939 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200940 } /* for i 0 to ancount */
941
Christopher Faulet67957bd2017-09-27 11:00:59 +0200942 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200943 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200944 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200945 return DNS_RESP_VALID;
946}
947
Christopher Faulet67957bd2017-09-27 11:00:59 +0200948/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200949 * If existing IP not found, return the first IP matching family_priority,
950 * otherwise, first ip found
951 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200952 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200953 * For both cases above, dns_validate_dns_response is required
954 * returns one of the DNS_UPD_* code
955 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100956#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200957int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200958 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100959 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200960 void **newip, short *newip_sin_family,
961 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200962{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200963 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100964 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200965 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200966 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100967 int currentip_sel;
968 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100969 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200970
Christopher Faulet67957bd2017-09-27 11:00:59 +0200971 family_priority = dns_opts->family_prio;
972 *newip = newip4 = newip6 = NULL;
973 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200974 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200975 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200976
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100977 /* Select an IP regarding configuration preference.
978 * Top priority is the prefered network ip version,
979 * second priority is the prefered network.
980 * the last priority is the currently used IP,
981 *
982 * For these three priorities, a score is calculated. The
983 * weight are:
Baptistefc725902016-12-26 23:21:08 +0100984 * 8 - prefered netwok ip version.
985 * 4 - prefered network.
986 * 2 - if the ip in the record is not affected to any other server in the same backend (duplication)
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100987 * 1 - current ip.
988 * The result with the biggest score is returned.
989 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200990
991 list_for_each_entry(record, &dns_p->answer_list, list) {
992 void *ip;
993 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100994
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200995 if (record->type == DNS_RTYPE_A) {
996 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
997 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200998 }
999 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001000 ip_type = AF_INET6;
1001 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001002 }
1003 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001004 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001005 score = 0;
1006
1007 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001008 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001009 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001010
1011 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001012 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001013
1014 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001015 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001016 continue;
1017
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001018 if ((ip_type == AF_INET &&
1019 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001020 &dns_opts->pref_net[j].mask.in4,
1021 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001022 (ip_type == AF_INET6 &&
1023 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001024 &dns_opts->pref_net[j].mask.in6,
1025 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001026 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001027 break;
1028 }
1029 }
1030
Christopher Faulet67957bd2017-09-27 11:00:59 +02001031 /* Check if the IP found in the record is already affected to a
1032 * member of a group. If yes, the score should be incremented
1033 * by 2. */
1034 if (owner && snr_check_ip_callback(owner, ip, &ip_type))
1035 continue;
1036
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001037 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001038 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001039 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001040 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001041 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001042 !memcmp(ip, currentip, 16)))) {
1043 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001044 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001045 }
1046 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001047 currentip_sel = 0;
1048
1049 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001050 * score. The maximum score is 15, if this value is reached, we
1051 * break the parsing. Implicitly, this score is reached the ip
1052 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001053 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001054 if (ip_type == AF_INET)
1055 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001056 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001057 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001058 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001059 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001060 return DNS_UPD_NO;
1061 max_score = score;
1062 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001063 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001064
Christopher Faulet67957bd2017-09-27 11:00:59 +02001065 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001066 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001067 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001068
Christopher Faulet67957bd2017-09-27 11:00:59 +02001069 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001070 if (family_priority == AF_INET) {
1071 if (newip4) {
1072 *newip = newip4;
1073 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001074 }
1075 else if (newip6) {
1076 *newip = newip6;
1077 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001078 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001079 if (!currentip_found)
1080 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001082 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001083 else if (family_priority == AF_INET6) {
1084 if (newip6) {
1085 *newip = newip6;
1086 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001087 }
1088 else if (newip4) {
1089 *newip = newip4;
1090 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001091 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001092 if (!currentip_found)
1093 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001094 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001095 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001096 else if (family_priority == AF_UNSPEC) {
1097 if (newip6) {
1098 *newip = newip6;
1099 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001100 }
1101 else if (newip4) {
1102 *newip = newip4;
1103 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001104 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001105 if (!currentip_found)
1106 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107 }
1108
Christopher Faulet67957bd2017-09-27 11:00:59 +02001109 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001110 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001111
Christopher Faulet67957bd2017-09-27 11:00:59 +02001112 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001113 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001114 /* Move the first record to the end of the list, for internal
1115 * round robin */
1116 LIST_DEL(&record->list);
1117 LIST_ADDQ(&dns_p->answer_list, &record->list);
1118 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001119 }
1120 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001121}
1122
Christopher Faulet67957bd2017-09-27 11:00:59 +02001123/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001124 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001125 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1126 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001127 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001128 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1129 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001130 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001131int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001132{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001133 char *ptr;
1134 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135
Christopher Faulet67957bd2017-09-27 11:00:59 +02001136 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001137 return -1;
1138
Christopher Faulet67957bd2017-09-27 11:00:59 +02001139 ptr = str;
1140 for (i = 0; i < dn_len-1; ++i) {
1141 sz = dn[i];
1142 if (i)
1143 *ptr++ = '.';
1144 memcpy(ptr, dn+i+1, sz);
1145 ptr += sz;
1146 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001147 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001148 *ptr++ = '\0';
1149 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001150}
1151
Christopher Faulet67957bd2017-09-27 11:00:59 +02001152/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1153 *
1154 * <str> must be a null-terminated string. <str_len> must include the
1155 * terminating null byte. <dn> buffer must be allocated and its size must be
1156 * passed in <dn_len>.
1157 *
1158 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1159 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001160 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001161int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001162{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001163 int i, offset;
1164
Christopher Faulet67957bd2017-09-27 11:00:59 +02001165 if (dn_len < str_len + 1)
1166 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001167
Christopher Faulet67957bd2017-09-27 11:00:59 +02001168 /* First byte of dn will be used to store the length of the first
1169 * label */
1170 offset = 0;
1171 for (i = 0; i < str_len; ++i) {
1172 if (str[i] == '.') {
1173 /* 2 or more consecutive dots is invalid */
1174 if (i == offset)
1175 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001176
Christopher Faulet67957bd2017-09-27 11:00:59 +02001177 dn[offset] = (i - offset);
1178 offset = i+1;
1179 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001180 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001181 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001182 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001183 dn[offset] = (i - offset - 1);
1184 dn[i] = '\0';
1185 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001186}
1187
Christopher Faulet67957bd2017-09-27 11:00:59 +02001188/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001189 * - total size
1190 * - each label size individually
1191 * returns:
1192 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1193 * 1 when no error. <err> is left unaffected.
1194 */
1195int dns_hostname_validation(const char *string, char **err)
1196{
1197 const char *c, *d;
1198 int i;
1199
1200 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1201 if (err)
1202 *err = DNS_TOO_LONG_FQDN;
1203 return 0;
1204 }
1205
1206 c = string;
1207 while (*c) {
1208 d = c;
1209
1210 i = 0;
1211 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1212 i++;
1213 if (!((*d == '-') || (*d == '_') ||
1214 ((*d >= 'a') && (*d <= 'z')) ||
1215 ((*d >= 'A') && (*d <= 'Z')) ||
1216 ((*d >= '0') && (*d <= '9')))) {
1217 if (err)
1218 *err = DNS_INVALID_CHARACTER;
1219 return 0;
1220 }
1221 d++;
1222 }
1223
1224 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1225 if (err)
1226 *err = DNS_LABEL_TOO_LONG;
1227 return 0;
1228 }
1229
1230 if (*d == '\0')
1231 goto out;
1232
1233 c = ++d;
1234 }
1235 out:
1236 return 1;
1237}
1238
Christopher Faulet67957bd2017-09-27 11:00:59 +02001239/* Picks up an available resolution from the different resolution list
1240 * associated to a resolvers section, in this order:
1241 * 1. check in resolutions.curr for the same hostname and query_type
1242 * 2. check in resolutions.wait for the same hostname and query_type
1243 * 3. Get a new resolution from resolution pool
1244 *
1245 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001246 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001247static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1248 char **hostname_dn, int hostname_dn_len,
1249 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001250{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001251 struct dns_resolution *res;
1252
1253 if (!*hostname_dn)
1254 goto from_pool;
1255
1256 /* Search for same hostname and query type in resolutions.curr */
1257 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1258 if (!res->hostname_dn)
1259 continue;
1260 if ((query_type == res->prefered_query_type) &&
1261 hostname_dn_len == res->hostname_dn_len &&
1262 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1263 return res;
1264 }
1265
1266 /* Search for same hostname and query type in resolutions.wait */
1267 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1268 if (!res->hostname_dn)
1269 continue;
1270 if ((query_type == res->prefered_query_type) &&
1271 hostname_dn_len == res->hostname_dn_len &&
1272 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1273 return res;
1274 }
1275
1276 from_pool:
1277 /* No resolution could be found, so let's allocate a new one */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001278 res = pool_alloc(dns_resolution_pool);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001279 if (res) {
1280 memset(res, 0, sizeof(*res));
1281 res->resolvers = resolvers;
1282 res->uuid = resolution_uuid;
1283 res->status = RSLV_STATUS_NONE;
1284 res->step = RSLV_STEP_NONE;
1285 res->last_valid = now_ms;
1286
1287 LIST_INIT(&res->requesters);
1288 LIST_INIT(&res->response.answer_list);
1289
1290 res->prefered_query_type = query_type;
1291 res->query_type = query_type;
1292 res->hostname_dn = *hostname_dn;
1293 res->hostname_dn_len = hostname_dn_len;
1294
1295 ++resolution_uuid;
1296
1297 /* Move the resolution to the resolvers wait queue */
1298 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1299 }
1300 return res;
1301}
1302
1303/* Releases a resolution from its requester(s) and move it back to the pool */
1304static void dns_free_resolution(struct dns_resolution *resolution)
1305{
1306 struct dns_requester *req, *reqback;
1307
1308 /* clean up configuration */
1309 dns_reset_resolution(resolution);
1310 resolution->hostname_dn = NULL;
1311 resolution->hostname_dn_len = 0;
1312
1313 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1314 LIST_DEL(&req->list);
1315 req->resolution = NULL;
1316 }
1317
1318 LIST_DEL(&resolution->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001319 pool_free(dns_resolution_pool, resolution);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001320}
1321
1322/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1323 * on success, -1 otherwise.
1324 */
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001325int dns_link_resolution(void *requester, int requester_type, int requester_locked)
Christopher Faulet67957bd2017-09-27 11:00:59 +02001326{
1327 struct dns_resolution *res = NULL;
1328 struct dns_requester *req;
1329 struct dns_resolvers *resolvers;
1330 struct server *srv = NULL;
1331 struct dns_srvrq *srvrq = NULL;
1332 char **hostname_dn;
1333 int hostname_dn_len, query_type;
1334
1335 switch (requester_type) {
1336 case OBJ_TYPE_SERVER:
1337 srv = (struct server *)requester;
1338 hostname_dn = &srv->hostname_dn;
1339 hostname_dn_len = srv->hostname_dn_len;
1340 resolvers = srv->resolvers;
1341 query_type = ((srv->dns_opts.family_prio == AF_INET)
1342 ? DNS_RTYPE_A
1343 : DNS_RTYPE_AAAA);
1344 break;
1345
1346 case OBJ_TYPE_SRVRQ:
1347 srvrq = (struct dns_srvrq *)requester;
1348 hostname_dn = &srvrq->hostname_dn;
1349 hostname_dn_len = srvrq->hostname_dn_len;
1350 resolvers = srvrq->resolvers;
1351 query_type = DNS_RTYPE_SRV;
1352 break;
1353
1354 default:
1355 goto err;
1356 }
1357
1358 /* Get a resolution from the resolvers' wait queue or pool */
1359 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1360 goto err;
1361
1362 if (srv) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001363 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001364 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001365 if (srv->dns_requester == NULL) {
Willy Tarreau5ec84572017-11-05 10:35:57 +01001366 if ((req = calloc(1, sizeof(*req))) == NULL) {
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001367 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001368 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001369 goto err;
Willy Tarreau5ec84572017-11-05 10:35:57 +01001370 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001371 req->owner = &srv->obj_type;
1372 srv->dns_requester = req;
1373 }
1374 else
1375 req = srv->dns_requester;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001376 if (!requester_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001377 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001378 }
1379 else if (srvrq) {
1380 if (srvrq->dns_requester == NULL) {
1381 if ((req = calloc(1, sizeof(*req))) == NULL)
1382 goto err;
1383 req->owner = &srvrq->obj_type;
1384 srvrq->dns_requester = req;
1385 }
1386 else
1387 req = srvrq->dns_requester;
1388 }
1389 else
1390 goto err;
1391
1392 req->resolution = res;
1393 req->requester_cb = snr_resolution_cb;
1394 req->requester_error_cb = snr_resolution_error_cb;
1395
1396 LIST_ADDQ(&res->requesters, &req->list);
1397 return 0;
1398
1399 err:
1400 if (res && LIST_ISEMPTY(&res->requesters))
1401 dns_free_resolution(res);
1402 return -1;
1403}
1404
1405/* Removes a requester from a DNS resoltion. It takes takes care of all the
1406 * consequences. It also cleans up some parameters from the requester.
1407 */
1408void dns_unlink_resolution(struct dns_requester *requester)
1409{
1410 struct dns_resolution *res;
1411 struct dns_requester *req;
1412
1413 /* Nothing to do */
1414 if (!requester || !requester->resolution)
1415 return;
1416 res = requester->resolution;
1417
1418 /* Clean up the requester */
1419 LIST_DEL(&requester->list);
1420 requester->resolution = NULL;
1421
1422 /* We need to find another requester linked on this resolution */
1423 if (!LIST_ISEMPTY(&res->requesters))
1424 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1425 else {
1426 dns_free_resolution(res);
1427 return;
1428 }
1429
1430 /* Move hostname_dn related pointers to the next requester */
1431 switch (obj_type(req->owner)) {
1432 case OBJ_TYPE_SERVER:
1433 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1434 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1435 break;
1436 case OBJ_TYPE_SRVRQ:
1437 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1438 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1439 break;
1440 default:
1441 res->hostname_dn = NULL;
1442 res->hostname_dn_len = 0;
1443 break;
1444 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001445}
1446
Christopher Faulet67957bd2017-09-27 11:00:59 +02001447/* Called when a network IO is generated on a name server socket for an incoming
1448 * packet. It performs the following actions:
1449 * - check if the packet requires processing (not outdated resolution)
1450 * - ensure the DNS packet received is valid and call requester's callback
1451 * - call requester's error callback if invalid response
1452 * - check the dn_name in the packet against the one sent
1453 */
1454static void dns_resolve_recv(struct dgram_conn *dgram)
1455{
1456 struct dns_nameserver *ns, *tmpns;
1457 struct dns_resolvers *resolvers;
1458 struct dns_resolution *res;
1459 struct dns_query_item *query;
1460 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1461 unsigned char *bufend;
1462 int fd, buflen, dns_resp;
1463 int max_answer_records;
1464 unsigned short query_id;
1465 struct eb32_node *eb;
1466 struct dns_requester *req;
1467
1468 fd = dgram->t.sock.fd;
1469
1470 /* check if ready for reading */
1471 if (!fd_recv_ready(fd))
1472 return;
1473
1474 /* no need to go further if we can't retrieve the nameserver */
1475 if ((ns = dgram->owner) == NULL)
1476 return;
1477
1478 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001479 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001480
1481 /* process all pending input messages */
1482 while (1) {
1483 /* read message received */
1484 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1485 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1486 /* FIXME : for now we consider EAGAIN only */
1487 fd_cant_recv(fd);
1488 break;
1489 }
1490
1491 /* message too big */
1492 if (buflen > resolvers->accepted_payload_size) {
1493 ns->counters.too_big++;
1494 continue;
1495 }
1496
1497 /* initializing variables */
1498 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1499
1500 /* read the query id from the packet (16 bits) */
1501 if (buf + 2 > bufend) {
1502 ns->counters.invalid++;
1503 continue;
1504 }
1505 query_id = dns_response_get_query_id(buf);
1506
1507 /* search the query_id in the pending resolution tree */
1508 eb = eb32_lookup(&resolvers->query_ids, query_id);
1509 if (eb == NULL) {
1510 /* unknown query id means an outdated response and can be safely ignored */
1511 ns->counters.outdated++;
1512 continue;
1513 }
1514
1515 /* known query id means a resolution in prgress */
1516 res = eb32_entry(eb, struct dns_resolution, qid);
1517 if (!res) {
1518 ns->counters.outdated++;
1519 continue;
1520 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001521
Christopher Faulet67957bd2017-09-27 11:00:59 +02001522 /* number of responses received */
1523 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001524
Christopher Faulet67957bd2017-09-27 11:00:59 +02001525 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1526 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001527
Christopher Faulet67957bd2017-09-27 11:00:59 +02001528 switch (dns_resp) {
1529 case DNS_RESP_VALID:
1530 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001531
Christopher Faulet67957bd2017-09-27 11:00:59 +02001532 case DNS_RESP_INVALID:
1533 case DNS_RESP_QUERY_COUNT_ERROR:
1534 case DNS_RESP_WRONG_NAME:
1535 res->status = RSLV_STATUS_INVALID;
1536 ns->counters.invalid++;
1537 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001538
Christopher Faulet67957bd2017-09-27 11:00:59 +02001539 case DNS_RESP_NX_DOMAIN:
1540 res->status = RSLV_STATUS_NX;
1541 ns->counters.nx++;
1542 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001543
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 case DNS_RESP_REFUSED:
1545 res->status = RSLV_STATUS_REFUSED;
1546 ns->counters.refused++;
1547 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001548
Christopher Faulet67957bd2017-09-27 11:00:59 +02001549 case DNS_RESP_ANCOUNT_ZERO:
1550 res->status = RSLV_STATUS_OTHER;
1551 ns->counters.any_err++;
1552 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001553
Christopher Faulet67957bd2017-09-27 11:00:59 +02001554 case DNS_RESP_CNAME_ERROR:
1555 res->status = RSLV_STATUS_OTHER;
1556 ns->counters.cname_error++;
1557 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001558
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559 case DNS_RESP_TRUNCATED:
1560 res->status = RSLV_STATUS_OTHER;
1561 ns->counters.truncated++;
1562 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001563
Christopher Faulet67957bd2017-09-27 11:00:59 +02001564 case DNS_RESP_NO_EXPECTED_RECORD:
1565 case DNS_RESP_ERROR:
1566 case DNS_RESP_INTERNAL:
1567 res->status = RSLV_STATUS_OTHER;
1568 ns->counters.other++;
1569 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001570 }
1571
Christopher Faulet67957bd2017-09-27 11:00:59 +02001572 /* Wait all nameservers response to handle errors */
1573 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1574 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001575
Christopher Faulet67957bd2017-09-27 11:00:59 +02001576 /* Process error codes */
1577 if (dns_resp != DNS_RESP_VALID) {
1578 if (res->prefered_query_type != res->query_type) {
1579 /* The fallback on the query type was already performed,
1580 * so check the try counter. If it falls to 0, we can
1581 * report an error. Else, wait the next attempt. */
1582 if (!res->try)
1583 goto report_res_error;
1584 }
1585 else {
1586 /* Fallback from A to AAAA or the opposite and re-send
1587 * the resolution immediately. try counter is not
1588 * decremented. */
1589 if (res->prefered_query_type == DNS_RTYPE_A) {
1590 res->query_type = DNS_RTYPE_AAAA;
1591 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001592 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001593 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1594 res->query_type = DNS_RTYPE_A;
1595 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001596 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001597 }
1598 continue;
1599 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001600
Christopher Faulet67957bd2017-09-27 11:00:59 +02001601 /* Now let's check the query's dname corresponds to the one we
1602 * sent. We can check only the first query of the list. We send
1603 * one query at a time so we get one query in the response */
1604 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1605 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1606 dns_resp = DNS_RESP_WRONG_NAME;
1607 ns->counters.other++;
1608 goto report_res_error;
1609 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001610
Christopher Faulet67957bd2017-09-27 11:00:59 +02001611 /* So the resolution succeeded */
1612 res->status = RSLV_STATUS_VALID;
1613 res->last_valid = now_ms;
1614 ns->counters.valid++;
1615 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001616
Christopher Faulet67957bd2017-09-27 11:00:59 +02001617 report_res_error:
1618 list_for_each_entry(req, &res->requesters, list)
1619 req->requester_error_cb(req, dns_resp);
1620 dns_reset_resolution(res);
1621 LIST_DEL(&res->list);
1622 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1623 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001624
Christopher Faulet67957bd2017-09-27 11:00:59 +02001625 report_res_success:
1626 /* Only the 1rst requester s managed by the server, others are
1627 * from the cache */
1628 tmpns = ns;
1629 list_for_each_entry(req, &res->requesters, list) {
Olivier Houchard28381072017-11-06 17:30:28 +01001630 struct server *s = objt_server(req->owner);
1631
1632 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001633 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001634 req->requester_cb(req, tmpns);
Olivier Houchard28381072017-11-06 17:30:28 +01001635 if (s)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001636 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001637 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001638 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001639
Christopher Faulet67957bd2017-09-27 11:00:59 +02001640 dns_reset_resolution(res);
1641 LIST_DEL(&res->list);
1642 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1643 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001644 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001645 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001646 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001647}
William Lallemand69e96442016-11-19 00:58:54 +01001648
Christopher Faulet67957bd2017-09-27 11:00:59 +02001649/* Called when a resolvers network socket is ready to send data */
1650static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001651{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652 struct dns_resolvers *resolvers;
1653 struct dns_nameserver *ns;
1654 struct dns_resolution *res;
1655 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001656
Christopher Faulet67957bd2017-09-27 11:00:59 +02001657 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001658
Christopher Faulet67957bd2017-09-27 11:00:59 +02001659 /* check if ready for sending */
1660 if (!fd_send_ready(fd))
1661 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001662
Christopher Faulet67957bd2017-09-27 11:00:59 +02001663 /* we don't want/need to be waked up any more for sending */
1664 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001665
Christopher Faulet67957bd2017-09-27 11:00:59 +02001666 /* no need to go further if we can't retrieve the nameserver */
1667 if ((ns = dgram->owner) == NULL)
1668 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001669
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670 resolvers = ns->resolvers;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001671 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001672
Christopher Faulet67957bd2017-09-27 11:00:59 +02001673 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1674 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001675
Christopher Faulet67957bd2017-09-27 11:00:59 +02001676 if (res->nb_queries == resolvers->nb_nameservers)
1677 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001678
Christopher Faulet67957bd2017-09-27 11:00:59 +02001679 trash.len = dns_build_query(res->query_id, res->query_type,
1680 resolvers->accepted_payload_size,
1681 res->hostname_dn, res->hostname_dn_len,
1682 trash.str, trash.size);
1683 if (trash.len == -1)
1684 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001685
Christopher Faulet67957bd2017-09-27 11:00:59 +02001686 ret = send(fd, trash.str, trash.len, 0);
1687 if (ret != trash.len)
1688 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001689
Christopher Faulet67957bd2017-09-27 11:00:59 +02001690 ns->counters.sent++;
1691 res->nb_queries++;
1692 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001693
Christopher Faulet67957bd2017-09-27 11:00:59 +02001694 snd_error:
1695 ns->counters.snd_error++;
1696 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001697 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001698 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001699}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001700
Christopher Faulet67957bd2017-09-27 11:00:59 +02001701/* Processes DNS resolution. First, it checks the active list to detect expired
1702 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1703 * checks the wait list to trigger new resolutions.
1704 */
1705static struct task *dns_process_resolvers(struct task *t)
1706{
1707 struct dns_resolvers *resolvers = t->context;
1708 struct dns_resolution *res, *resback;
1709 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001710
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001711 HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001712
Christopher Faulet67957bd2017-09-27 11:00:59 +02001713 /* Handle all expired resolutions from the active list */
1714 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1715 /* When we find the first resolution in the future, then we can
1716 * stop here */
1717 exp = tick_add(res->last_query, resolvers->timeout.retry);
1718 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001719 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001720
Christopher Faulet67957bd2017-09-27 11:00:59 +02001721 /* If current resolution has been tried too many times and
1722 * finishes in timeout we update its status and remove it from
1723 * the list */
1724 if (!res->try) {
1725 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001726
Christopher Faulet67957bd2017-09-27 11:00:59 +02001727 /* Notify the result to the requesters */
1728 if (!res->nb_responses)
1729 res->status = RSLV_STATUS_TIMEOUT;
1730 list_for_each_entry(req, &res->requesters, list)
1731 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001732
Christopher Faulet67957bd2017-09-27 11:00:59 +02001733 /* Clean up resolution info and remove it from the
1734 * current list */
1735 dns_reset_resolution(res);
1736 LIST_DEL(&res->list);
1737 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001738
Christopher Faulet67957bd2017-09-27 11:00:59 +02001739 /* This might be triggered by too big UDP packets
1740 * dropped somewhere on the network, so lowering the
1741 * accepted_payload_size announced */
1742 if (resolvers->accepted_payload_size > 1280)
1743 resolvers->accepted_payload_size = 1280;
William Lallemand69e96442016-11-19 00:58:54 +01001744 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001745 else {
1746 /* Otherwise resend the DNS query and requeue the resolution */
1747 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1748 /* No response received (a real timeout) or fallback already done */
1749 res->query_type = res->prefered_query_type;
1750 res->try--;
1751 }
1752 else {
1753 /* Fallback from A to AAAA or the opposite and re-send
1754 * the resolution immediately. try counter is not
1755 * decremented. */
1756 if (res->prefered_query_type == DNS_RTYPE_A)
1757 res->query_type = DNS_RTYPE_AAAA;
1758 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1759 res->query_type = DNS_RTYPE_A;
1760 else
1761 res->try--;
1762 }
1763 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001764 }
1765 }
William Lallemand69e96442016-11-19 00:58:54 +01001766
Christopher Faulet67957bd2017-09-27 11:00:59 +02001767 /* Handle all resolutions in the wait list */
1768 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1769 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1770 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1771 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001772
Christopher Faulet67957bd2017-09-27 11:00:59 +02001773 if (dns_run_resolution(res) != 1) {
1774 res->last_resolution = now_ms;
1775 LIST_DEL(&res->list);
1776 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001777 }
1778 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001779
Christopher Faulet67957bd2017-09-27 11:00:59 +02001780 dns_update_resolvers_timeout(resolvers);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001781 HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001782 return t;
1783}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001784
Christopher Faulet67957bd2017-09-27 11:00:59 +02001785/* proto_udp callback functions for a DNS resolution */
1786struct dgram_data_cb resolve_dgram_cb = {
1787 .recv = dns_resolve_recv,
1788 .send = dns_resolve_send,
1789};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001790
Christopher Faulet67957bd2017-09-27 11:00:59 +02001791/* Release memory allocated by DNS */
1792static void dns_deinit(void)
1793{
1794 struct dns_resolvers *resolvers, *resolversback;
1795 struct dns_nameserver *ns, *nsback;
1796 struct dns_resolution *res, *resback;
1797 struct dns_requester *req, *reqback;
1798 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001799
Christopher Faulet67957bd2017-09-27 11:00:59 +02001800 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1801 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1802 free(ns->id);
1803 free((char *)ns->conf.file);
1804 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1805 fd_delete(ns->dgram->t.sock.fd);
1806 free(ns->dgram);
1807 LIST_DEL(&ns->list);
1808 free(ns);
1809 }
1810
1811 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1812 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1813 LIST_DEL(&req->list);
1814 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001815 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001816 dns_free_resolution(res);
1817 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001818
Christopher Faulet67957bd2017-09-27 11:00:59 +02001819 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1820 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1821 LIST_DEL(&req->list);
1822 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001823 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001824 dns_free_resolution(res);
1825 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001826
Christopher Faulet67957bd2017-09-27 11:00:59 +02001827 free(resolvers->id);
1828 free((char *)resolvers->conf.file);
1829 task_delete(resolvers->t);
1830 task_free(resolvers->t);
1831 LIST_DEL(&resolvers->list);
1832 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001833 }
1834
Christopher Faulet67957bd2017-09-27 11:00:59 +02001835 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1836 free(srvrq->name);
1837 free(srvrq->hostname_dn);
1838 LIST_DEL(&srvrq->list);
1839 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001840 }
1841
Willy Tarreaubafbe012017-11-24 17:34:44 +01001842 pool_destroy(dns_answer_item_pool);
1843 pool_destroy(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001844}
1845
Christopher Faulet67957bd2017-09-27 11:00:59 +02001846/* Finalizes the DNS configuration by allocating required resources and checking
1847 * live parameters.
1848 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001849 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001850static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001851{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001852 struct dns_resolvers *resolvers;
1853 struct proxy *px;
1854 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001855
Christopher Faulet67957bd2017-09-27 11:00:59 +02001856 /* allocate pool of resolution per resolvers */
1857 list_for_each_entry(resolvers, &dns_resolvers, list) {
1858 struct dns_nameserver *ns;
1859 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001860
Christopher Faulet67957bd2017-09-27 11:00:59 +02001861 /* Check if we can create the socket with nameservers info */
1862 list_for_each_entry(ns, &resolvers->nameservers, list) {
1863 struct dgram_conn *dgram = NULL;
1864 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001865
Christopher Faulet67957bd2017-09-27 11:00:59 +02001866 /* Check nameserver info */
1867 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001868 ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1869 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001870 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001871 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001872 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001873 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001874 ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1875 resolvers->id, ns->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001876 close(fd);
1877 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001878 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001879 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001880 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001881
Christopher Faulet67957bd2017-09-27 11:00:59 +02001882 /* Create dgram structure that will hold the UPD socket
1883 * and attach it on the current nameserver */
1884 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001885 ha_alert("config: resolvers '%s' : out of memory.\n",
1886 resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001887 err_code |= (ERR_ALERT|ERR_ABORT);
1888 goto err;
1889 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001890
Christopher Faulet67957bd2017-09-27 11:00:59 +02001891 /* Leave dgram partially initialized, no FD attached for
1892 * now. */
1893 dgram->owner = ns;
1894 dgram->data = &resolve_dgram_cb;
1895 dgram->t.sock.fd = -1;
1896 ns->dgram = dgram;
1897 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001898
Christopher Faulet67957bd2017-09-27 11:00:59 +02001899 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001900 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001901 ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001902 err_code |= (ERR_ALERT|ERR_ABORT);
1903 goto err;
1904 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001905
Christopher Faulet67957bd2017-09-27 11:00:59 +02001906 /* Update task's parameters */
1907 t->process = dns_process_resolvers;
1908 t->context = resolvers;
1909 resolvers->t = t;
1910 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001911 }
1912
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001913 for (px = proxies_list; px; px = px->next) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001914 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001915
Christopher Faulet67957bd2017-09-27 11:00:59 +02001916 for (srv = px->srv; srv; srv = srv->next) {
1917 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001918
Christopher Faulet67957bd2017-09-27 11:00:59 +02001919 if (!srv->resolvers_id)
1920 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001921
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001923 ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1924 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001925 err_code |= (ERR_ALERT|ERR_ABORT);
1926 continue;
1927 }
1928 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001929
Christopher Faulet67957bd2017-09-27 11:00:59 +02001930 if (srv->srvrq && !srv->srvrq->resolvers) {
1931 srv->srvrq->resolvers = srv->resolvers;
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001932 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001933 ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1934 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001935 err_code |= (ERR_ALERT|ERR_ABORT);
1936 continue;
1937 }
1938 }
Olivier Houchard55dcdf42017-11-06 15:15:04 +01001939 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001940 ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1941 proxy_type_str(px), px->id, srv->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 err_code |= (ERR_ALERT|ERR_ABORT);
1943 continue;
1944 }
1945 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001946 }
1947
Christopher Faulet67957bd2017-09-27 11:00:59 +02001948 if (err_code & (ERR_ALERT|ERR_ABORT))
1949 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001950
Christopher Faulet67957bd2017-09-27 11:00:59 +02001951 return err_code;
1952 err:
1953 dns_deinit();
1954 return err_code;
1955
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001956}
1957
Christopher Faulet67957bd2017-09-27 11:00:59 +02001958/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
1959static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001960{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001961 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001962
Christopher Faulet67957bd2017-09-27 11:00:59 +02001963 if (*args[2]) {
1964 list_for_each_entry(presolvers, &dns_resolvers, list) {
1965 if (strcmp(presolvers->id, args[2]) == 0) {
1966 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001967 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001968 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001969 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001970 if (appctx->ctx.cli.p0 == NULL) {
1971 appctx->ctx.cli.severity = LOG_ERR;
1972 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1973 appctx->st0 = CLI_ST_PRINT;
1974 return 1;
1975 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001976 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001978}
1979
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980/* Dumps counters from all resolvers section and associated name servers. It
1981 * returns 0 if the output buffer is full and it needs to be called again,
1982 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001983 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001984 */
1985static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1986{
1987 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001988 struct dns_resolvers *resolvers;
1989 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001990
1991 chunk_reset(&trash);
1992
1993 switch (appctx->st2) {
1994 case STAT_ST_INIT:
1995 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1996 /* fall through */
1997
1998 case STAT_ST_LIST:
1999 if (LIST_ISEMPTY(&dns_resolvers)) {
2000 chunk_appendf(&trash, "No resolvers found\n");
2001 }
2002 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 list_for_each_entry(resolvers, &dns_resolvers, list) {
2004 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002005 continue;
2006
Christopher Faulet67957bd2017-09-27 11:00:59 +02002007 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
2008 list_for_each_entry(ns, &resolvers->nameservers, list) {
2009 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
2010 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
2011 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
2012 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
2013 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
2014 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
2015 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
2016 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
2017 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
2018 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
2019 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
2020 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
2021 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
2022 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
2023 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
2024 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01002025 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02002026 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01002027 }
2028 }
2029
2030 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002031 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01002032 /* let's try again later from this session. We add ourselves into
2033 * this session's users so that it can remove us upon termination.
2034 */
2035 si->flags |= SI_FL_WAIT_ROOM;
2036 return 0;
2037 }
2038
2039 appctx->st2 = STAT_ST_FIN;
2040 /* fall through */
2041
2042 default:
2043 appctx->st2 = STAT_ST_FIN;
2044 return 1;
2045 }
2046}
2047
2048/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002049static struct cli_kw_list cli_kws = {{ }, {
2050 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2051 " associated name servers",
2052 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2053 {{},}
2054 }
2055};
William Lallemand69e96442016-11-19 00:58:54 +01002056
2057
2058__attribute__((constructor))
2059static void __dns_init(void)
2060{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002061 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2062 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2063
Christopher Faulet67957bd2017-09-27 11:00:59 +02002064 hap_register_post_check(dns_finalize_config);
2065 hap_register_post_deinit(dns_deinit);
2066
William Lallemand69e96442016-11-19 00:58:54 +01002067 cli_register_kw(&cli_kws);
2068}