blob: 00f7b10ca5f6d26f7c4d6420d57b85f7f96003bd [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
22#include <common/time.h>
23#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020024#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020025
Baptiste Assmannfa4a6632017-05-04 09:05:00 +020026#include <import/lru.h>
27#include <import/xxhash.h>
28
William Lallemand69e96442016-11-19 00:58:54 +010029#include <types/applet.h>
30#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020031#include <types/global.h>
32#include <types/dns.h>
33#include <types/proto_udp.h>
William Lallemand69e96442016-11-19 00:58:54 +010034#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035
William Lallemand69e96442016-11-19 00:58:54 +010036#include <proto/channel.h>
37#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020038#include <proto/checks.h>
39#include <proto/dns.h>
40#include <proto/fd.h>
41#include <proto/log.h>
42#include <proto/server.h>
43#include <proto/task.h>
44#include <proto/proto_udp.h>
William Lallemand69e96442016-11-19 00:58:54 +010045#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020046
47struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
48struct dns_resolution *resolution = NULL;
49
50static int64_t dns_query_id_seed; /* random seed */
51
Baptiste Assmannfa4a6632017-05-04 09:05:00 +020052static struct lru64_head *dns_lru_tree;
53static int dns_cache_size = 1024; /* arbitrary DNS cache size */
54
Olivier Houcharda8c6db82017-07-06 18:46:47 +020055static struct pool_head *dns_answer_item_pool;
56
Baptiste Assmann325137d2015-04-13 23:40:55 +020057/* proto_udp callback functions for a DNS resolution */
58struct dgram_data_cb resolve_dgram_cb = {
59 .recv = dns_resolve_recv,
60 .send = dns_resolve_send,
61};
62
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063/* local function prototypes */
64static int dns_run_resolution(struct dns_requester *requester);
65
Baptiste Assmann325137d2015-04-13 23:40:55 +020066#if DEBUG
67/*
68 * go through the resolutions associated to a resolvers section and print the ID and hostname in
69 * domain name format
70 * should be used for debug purpose only
71 */
72void dns_print_current_resolutions(struct dns_resolvers *resolvers)
73{
Baptiste Assmann201c07f2017-05-22 15:17:15 +020074 list_for_each_entry(resolution, &resolvers->resolution.curr, list) {
Baptiste Assmann325137d2015-04-13 23:40:55 +020075 printf(" resolution %d for %s\n", resolution->query_id, resolution->hostname_dn);
76 }
77}
78#endif
79
Baptiste Assmann201c07f2017-05-22 15:17:15 +020080void dump_dns_config()
81{
82 struct dns_resolvers *curr_resolvers = NULL;
83 struct dns_nameserver *curr_nameserver = NULL;
84 struct dns_resolution *curr_resolution = NULL;
85 struct dns_requester *curr_requester = NULL;
86
87 printf("===============\n");
88 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
89 printf("Resolvers: %s\n", curr_resolvers->id);
90
91 printf(" nameservers:\n");
92 list_for_each_entry(curr_nameserver, &curr_resolvers->nameserver_list, list) {
93 printf(" %s\n", curr_nameserver->id);
94 }
95
96/*
97 printf(" resolution.pool list:\n");
98 list_for_each_entry(curr_resolution, &curr_resolvers->resolution.pool, list) {
99 printf(" %p\n", curr_resolution);
100 }
101*/
102
103 printf(" resolution.wait list:\n");
104 list_for_each_entry(curr_resolution, &curr_resolvers->resolution.wait, list) {
105 printf(" %p %s\n", curr_resolution, curr_resolution->hostname_dn);
106 printf(" requester.wait list:\n");
107 list_for_each_entry(curr_requester, &curr_resolution->requester.wait, list) {
108 printf(" %p %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
109 }
110 printf(" requester.curr list:\n");
111 list_for_each_entry(curr_requester, &curr_resolution->requester.curr, list) {
112 printf(" %p %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
113 }
114 }
115 printf(" resolution.curr list:\n");
116 list_for_each_entry(curr_resolution, &curr_resolvers->resolution.curr, list) {
117 printf(" %p %s\n", curr_resolution, curr_resolution->hostname_dn);
118 printf(" requester.wait list:\n");
119 list_for_each_entry(curr_requester, &curr_resolution->requester.wait, list) {
120 printf(" %p %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
121 }
122 printf(" requester.curr list:\n");
123 list_for_each_entry(curr_requester, &curr_resolution->requester.curr, list) {
124 printf(" %p %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
125 }
126 }
127 }
128
129 printf("===============\n");
130}
131
132/*
133 * Initiates a new name resolution:
134 * - generates a query id
135 * - configure the resolution structure
136 * - startup the resolvers task if required
137 *
138 * returns:
139 * - 0 if everything started properly
140 * - -1 in case of error or if resolution already running
141 */
142int dns_trigger_resolution(struct dns_resolution *resolution)
143{
144 struct dns_requester *requester = NULL, *tmprequester;
145 struct dns_resolvers *resolvers = NULL;
146 int inter;
147
148 /* process the element of the wait queue */
149 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.wait, list) {
150 inter = 0;
151
152 switch (obj_type(requester->requester)) {
153 case OBJ_TYPE_SERVER:
154 inter = objt_server(requester->requester)->check.inter;
155 resolvers = objt_server(requester->requester)->resolvers;
156 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200157 case OBJ_TYPE_SRVRQ:
158 inter = objt_dns_srvrq(requester->requester)->inter;
159 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
160 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200161 case OBJ_TYPE_NONE:
162 default:
163 return -1;
164 }
165
166 /* if data is fresh enough, let's use it */
167 if (!tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms)) {
168 /* we only use cache if the response there is valid.
169 * If not valid, we run the resolution and move the requester to
170 * the run queue. */
171 if (resolution->status != RSLV_STATUS_VALID) {
172 LIST_DEL(&requester->list);
173 LIST_ADDQ(&resolution->requester.curr, &requester->list);
174 dns_run_resolution(requester);
175 continue;
176 }
177
178 requester->requester_cb(requester, NULL);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200179 resolvers = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200180 }
181 else {
182 LIST_DEL(&requester->list);
183 LIST_ADDQ(&resolution->requester.curr, &requester->list);
184 dns_run_resolution(requester);
185 }
186 }
187
188 if (resolvers)
189 dns_update_resolvers_timeout(resolvers);
190
191 return 0;
192}
193
Baptiste Assmann325137d2015-04-13 23:40:55 +0200194/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200195 * Prepare and send a DNS resolution.
196 *
197 * Return code:
198 * - 0 if no error occured
199 * - -1 in case of error
200 */
201static int
202dns_run_resolution(struct dns_requester *requester)
203{
204 struct dns_resolution *resolution;
205 struct dns_resolvers *resolvers;
206 int query_id, query_type, i;
207 struct proxy *proxy;
208
209 resolution = NULL;
210 resolvers = NULL;
211 proxy = NULL;
212 query_type = -1;
213 switch (obj_type(requester->requester)) {
214 case OBJ_TYPE_SERVER:
215 resolution = objt_server(requester->requester)->resolution;
216 resolvers = objt_server(requester->requester)->resolvers;
217 proxy = objt_server(requester->requester)->proxy;
218 query_type = requester->prefered_query_type;
219 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200220 case OBJ_TYPE_SRVRQ:
221 resolution = objt_dns_srvrq(requester->requester)->resolution;
222 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
223 proxy = objt_dns_srvrq(requester->requester)->proxy;
224 query_type = DNS_RTYPE_SRV;
225 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200226 case OBJ_TYPE_NONE:
227 default:
228 return -1;
229 }
230
231 /*
Olivier Houchard8da5f982017-08-04 18:35:36 +0200232 * Avoid sending requests for resolutions that don't yet have
233 * an hostname, ie resolutions linked to servers that do not yet
234 * have an fqdn
235 */
236 if (!resolution->hostname_dn)
237 return 0;
238
239 /*
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200240 * check if a resolution has already been started for this server
241 * return directly to avoid resolution pill up.
242 */
243 if (resolution->step != RSLV_STEP_NONE)
244 return 0;
245
246 /* generates a query id */
247 i = 0;
248 do {
249 query_id = dns_rnd16();
250 /* we do try only 100 times to find a free query id */
251 if (i++ > 100) {
252 chunk_printf(&trash, "could not generate a query id for %s, in resolvers %s",
253 resolution->hostname_dn, resolvers->id);
254
255 if (proxy)
256 send_log(proxy, LOG_NOTICE, "%s.\n", trash.str);
257 return -1;
258 }
259 } while (eb32_lookup(&resolvers->query_ids, query_id));
260
261 /* move the resolution into the run queue */
262 LIST_DEL(&resolution->list);
263 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
264
265 /* now update resolution parameters */
266 resolution->query_id = query_id;
267 resolution->qid.key = query_id;
268 resolution->step = RSLV_STEP_RUNNING;
269 resolution->query_type = query_type;
270 resolution->try = resolvers->resolve_retries;
271 resolution->try_cname = 0;
272 resolution->nb_responses = 0;
273 eb32_insert(&resolvers->query_ids, &resolution->qid);
274
275 dns_send_query(resolution);
276 resolution->try -= 1;
277
278 /* update wakeup date if this resolution is the only one in the FIFO list */
279 if (dns_check_resolution_queue(resolvers) == 1) {
280 /* update task timeout */
281 dns_update_resolvers_timeout(resolvers);
282 task_queue(resolvers->t);
283 }
284
285 return 0;
286}
287
288/*
Baptiste Assmann325137d2015-04-13 23:40:55 +0200289 * check if there is more than 1 resolution in the resolver's resolution list
290 * return value:
291 * 0: empty list
292 * 1: exactly one entry in the list
293 * 2: more than one entry in the list
294 */
295int dns_check_resolution_queue(struct dns_resolvers *resolvers)
296{
297
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200298 if (LIST_ISEMPTY(&resolvers->resolution.curr))
Baptiste Assmann325137d2015-04-13 23:40:55 +0200299 return 0;
300
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200301 if ((resolvers->resolution.curr.n) && (resolvers->resolution.curr.n == resolvers->resolution.curr.p))
Baptiste Assmann325137d2015-04-13 23:40:55 +0200302 return 1;
303
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200304 if (! ((resolvers->resolution.curr.n == resolvers->resolution.curr.p)
305 && (&resolvers->resolution.curr != resolvers->resolution.curr.n)))
Baptiste Assmann325137d2015-04-13 23:40:55 +0200306 return 2;
307
308 return 0;
309}
310
311/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200312 * reset some resolution parameters to initial values and also delete the
313 * query ID from the resolver's tree.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200314 */
315void dns_reset_resolution(struct dns_resolution *resolution)
316{
317 /* update resolution status */
318 resolution->step = RSLV_STEP_NONE;
319
320 resolution->try = 0;
321 resolution->try_cname = 0;
322 resolution->last_resolution = now_ms;
323 resolution->nb_responses = 0;
324
325 /* clean up query id */
326 eb32_delete(&resolution->qid);
327 resolution->query_id = 0;
328 resolution->qid.key = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200329}
330
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200331static inline void free_dns_answer_item(struct dns_answer_item *item)
332{
333 pool_free2(dns_answer_item_pool, item);
334}
335
336
Baptiste Assmann325137d2015-04-13 23:40:55 +0200337/*
338 * function called when a network IO is generated on a name server socket for an incoming packet
339 * It performs the following actions:
340 * - check if the packet requires processing (not outdated resolution)
341 * - ensure the DNS packet received is valid and call requester's callback
342 * - call requester's error callback if invalid response
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200343 * - check the dn_name in the packet against the one sent
Baptiste Assmann325137d2015-04-13 23:40:55 +0200344 */
345void dns_resolve_recv(struct dgram_conn *dgram)
346{
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200347 struct dns_nameserver *nameserver, *tmpnameserver;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200348 struct dns_resolvers *resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200349 struct dns_resolution *resolution = NULL;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200350 struct dns_query_item *query;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200351 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
352 unsigned char *bufend;
William Lallemandcc9b94a2017-06-08 19:30:39 +0200353 int fd, buflen, dns_resp, need_resend = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200354 unsigned short query_id;
355 struct eb32_node *eb;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200356 struct lru64 *lru = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200357 struct dns_requester *requester = NULL, *tmprequester = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200358 struct dns_answer_item *item1, *item2 = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200359
360 fd = dgram->t.sock.fd;
361
362 /* check if ready for reading */
363 if (!fd_recv_ready(fd))
364 return;
365
366 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200367 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200368 return;
369
370 resolvers = nameserver->resolvers;
371
372 /* process all pending input messages */
373 while (1) {
Olivier Houchard8da5f982017-08-04 18:35:36 +0200374 int removed_reso = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200375 /* read message received */
376 memset(buf, '\0', DNS_MAX_UDP_MESSAGE + 1);
377 if ((buflen = recv(fd, (char*)buf , DNS_MAX_UDP_MESSAGE, 0)) < 0) {
378 /* FIXME : for now we consider EAGAIN only */
379 fd_cant_recv(fd);
380 break;
381 }
382
383 /* message too big */
384 if (buflen > DNS_MAX_UDP_MESSAGE) {
385 nameserver->counters.too_big += 1;
386 continue;
387 }
388
389 /* initializing variables */
390 bufend = buf + buflen; /* pointer to mark the end of the buffer */
391
392 /* read the query id from the packet (16 bits) */
393 if (buf + 2 > bufend) {
394 nameserver->counters.invalid += 1;
395 continue;
396 }
397 query_id = dns_response_get_query_id(buf);
398
399 /* search the query_id in the pending resolution tree */
Baptiste Assmann01daef32015-09-02 22:05:24 +0200400 eb = eb32_lookup(&resolvers->query_ids, query_id);
401 if (eb == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200402 /* unknown query id means an outdated response and can be safely ignored */
403 nameserver->counters.outdated += 1;
404 continue;
405 }
406
407 /* known query id means a resolution in prgress */
408 resolution = eb32_entry(eb, struct dns_resolution, qid);
409
410 if (!resolution) {
411 nameserver->counters.outdated += 1;
412 continue;
413 }
414
415 /* number of responses received */
416 resolution->nb_responses += 1;
417
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200418 dns_resp = dns_validate_dns_response(buf, bufend, resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200419
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200420 switch (dns_resp) {
421 case DNS_RESP_VALID:
422 need_resend = 0;
423 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200424
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200425 case DNS_RESP_INVALID:
426 case DNS_RESP_QUERY_COUNT_ERROR:
427 case DNS_RESP_WRONG_NAME:
428 if (resolution->status != RSLV_STATUS_INVALID) {
429 resolution->status = RSLV_STATUS_INVALID;
430 resolution->last_status_change = now_ms;
431 }
432 nameserver->counters.invalid += 1;
433 need_resend = 0;
434 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200435
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200436 case DNS_RESP_ANCOUNT_ZERO:
437 if (resolution->status != RSLV_STATUS_OTHER) {
438 resolution->status = RSLV_STATUS_OTHER;
439 resolution->last_status_change = now_ms;
440 }
441 nameserver->counters.any_err += 1;
442 need_resend = 1;
443 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200444
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200445 case DNS_RESP_NX_DOMAIN:
446 if (resolution->status != RSLV_STATUS_NX) {
447 resolution->status = RSLV_STATUS_NX;
448 resolution->last_status_change = now_ms;
449 }
450 nameserver->counters.nx += 1;
451 need_resend = 0;
452 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200453
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200454 case DNS_RESP_REFUSED:
455 if (resolution->status != RSLV_STATUS_REFUSED) {
456 resolution->status = RSLV_STATUS_REFUSED;
457 resolution->last_status_change = now_ms;
458 }
459 nameserver->counters.refused += 1;
460 need_resend = 0;
461 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200462
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200463 case DNS_RESP_CNAME_ERROR:
464 if (resolution->status != RSLV_STATUS_OTHER) {
465 resolution->status = RSLV_STATUS_OTHER;
466 resolution->last_status_change = now_ms;
467 }
468 nameserver->counters.cname_error += 1;
469 need_resend = 1;
470 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200471
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200472 case DNS_RESP_TRUNCATED:
473 if (resolution->status != RSLV_STATUS_OTHER) {
474 resolution->status = RSLV_STATUS_OTHER;
475 resolution->last_status_change = now_ms;
476 }
477 nameserver->counters.truncated += 1;
478 need_resend = 1;
479 break;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200480
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200481 case DNS_RESP_NO_EXPECTED_RECORD:
482 if (resolution->status != RSLV_STATUS_OTHER) {
483 resolution->status = RSLV_STATUS_OTHER;
484 resolution->last_status_change = now_ms;
485 }
486 nameserver->counters.other += 1;
487 need_resend = 1;
488 break;
489
490 case DNS_RESP_ERROR:
491 case DNS_RESP_INTERNAL:
492 if (resolution->status != RSLV_STATUS_OTHER) {
493 resolution->status = RSLV_STATUS_OTHER;
494 resolution->last_status_change = now_ms;
495 }
496 nameserver->counters.other += 1;
497 need_resend = 1;
498 break;
499 }
500
Olivier Houchard8da5f982017-08-04 18:35:36 +0200501 /* Check for any obsolete record, also identify any SRV request, and try to find a corresponding server */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200502 list_for_each_entry_safe(item1, item2, &resolution->response.answer_list,
503 list) {
504 if (item1->last_seen + nameserver->resolvers->hold.obsolete / 1000 < now.tv_sec) {
505 LIST_DEL(&item1->list);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200506 if (item1->type == DNS_RTYPE_SRV && !LIST_ISEMPTY(&resolution->requester.curr)) {
507 struct dns_srvrq *srvrq;
508
509 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
510
511 srvrq = objt_dns_srvrq(requester->requester);
512 /* We're removing an obsolete entry, remove any associated server */
513 if (srvrq) {
514 struct server *srv;
515
516 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
517 if (srv->srvrq == srvrq &&
518 item1->data_len ==
519 srv->hostname_dn_len &&
520 !memcmp(srv->hostname_dn, item1->target, item1->data_len) &&
521 srv->svc_port == item1->port) {
522 snr_update_srv_status(srv, 1);
523 free(srv->hostname);
524 srv->hostname = NULL;
525 srv->hostname_dn_len = 0;
526 free(srv->hostname_dn);
527 srv->hostname_dn = NULL;
528 dns_resolution_free(srv->resolvers, srv->resolution);
529 srv->resolution = dns_resolution_list_get(srv->resolvers, NULL, srv->dns_requester->prefered_query_type);
530 if (resolution == srv->resolution)
531 removed_reso = 1;
532 }
533 }
534 }
535 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200536 free_dns_answer_item(item1);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200537 continue;
538 }
539 if (item1->type == DNS_RTYPE_SRV) {
540 struct server *srv;
541 struct dns_srvrq *srvrq;
542
543 if (LIST_ISEMPTY(&resolution->requester.curr))
544 continue;
545
546 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
547 srvrq = objt_dns_srvrq(requester->requester);
548 if (!srvrq)
549 continue;
550 /* Check if a server already uses that hostname */
551 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
552 if (srv->srvrq == srvrq &&
553 item1->data_len == srv->hostname_dn_len &&
554 !memcmp(srv->hostname_dn, item1->target, item1->data_len) &&
555 srv->svc_port == item1->port) {
556 if (srv->uweight != item1->weight) {
557 char weight[9];
558
559 snprintf(weight, sizeof(weight),
560 "%d", item1->weight);
561 server_parse_weight_change_request(srv, weight);
562
563 }
564
565 break;
566 }
567 }
568 /* If not, try to find a server that is down */
569 if (!srv) {
570 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
571
572 if (srv->srvrq == srvrq &&
573 !srv->hostname_dn)
574 break;
575 }
576 if (srv) {
577 char weight[9];
578
579 char hostname[DNS_MAX_NAME_SIZE];
580
581 if (item1->data_len > DNS_MAX_NAME_SIZE)
582 continue;
583 dns_dn_label_to_str(item1->target, hostname, item1->data_len);
584 update_server_fqdn(srv, hostname, "SRV record");
585 srv->svc_port = item1->port;
586 srv->flags &= ~SRV_F_MAPPORTS;
587 if ((srv->check.state & CHK_ST_CONFIGURED) && !(srv->flags & SRV_F_CHECKPORT))
588 srv->check.port = item1->port;
589 snprintf(weight, sizeof(weight),
590 "%d", item1->weight);
591 server_parse_weight_change_request(srv, weight);
592 }
593
594 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200595 }
596 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200597 if (removed_reso)
598 goto next_packet;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200599
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200600 /* some error codes trigger a re-send of the query, but switching the
601 * query type.
602 * This is the case for the following error codes:
603 * DNS_RESP_ANCOUNT_ZERO
604 * DNS_RESP_TRUNCATED
605 * DNS_RESP_ERROR
606 * DNS_RESP_INTERNAL
607 * DNS_RESP_NO_EXPECTED_RECORD
608 * DNS_RESP_CNAME_ERROR
609 */
610 if (need_resend) {
611 int family_prio;
612 int res_preferred_afinet, res_preferred_afinet6;
613
614 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
615 switch (obj_type(requester->requester)) {
616 case OBJ_TYPE_SERVER:
617 family_prio = objt_server(requester->requester)->dns_opts.family_prio;
618 break;
619 case OBJ_TYPE_NONE:
620 default:
621 family_prio = AF_INET6;
622 }
623 res_preferred_afinet = family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
624 res_preferred_afinet6 = family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
625 if ((res_preferred_afinet || res_preferred_afinet6)
626 || (resolution->try > 0)) {
627 /* let's change the query type */
628 if (res_preferred_afinet6) {
629 /* fallback from AAAA to A */
630 resolution->query_type = DNS_RTYPE_A;
631 }
632 else if (res_preferred_afinet) {
633 /* fallback from A to AAAA */
634 resolution->query_type = DNS_RTYPE_AAAA;
635 }
636 else {
637 resolution->try -= 1;
638 if (family_prio == AF_INET) {
639 resolution->query_type = DNS_RTYPE_A;
640 } else {
641 resolution->query_type = DNS_RTYPE_AAAA;
642 }
643 }
644
645 dns_send_query(resolution);
646 /*
647 * move the resolution to the last element of the FIFO queue
648 * and update timeout wakeup based on the new first entry
649 */
650 if (dns_check_resolution_queue(resolvers) > 1) {
651 /* second resolution becomes first one */
652 LIST_DEL(&resolution->list);
653 /* ex first resolution goes to the end of the queue */
654 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
655 }
656
657 dns_update_resolvers_timeout(resolvers);
658 goto next_packet;
659 }
660
661 /* if we're there, this means that we already ran out of chances to re-send
662 * the query */
663 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
664 requester->requester_error_cb(requester, dns_resp);
665 }
666 goto next_packet;
667 }
668
669 /* now processing those error codes only:
670 * DNS_RESP_NX_DOMAIN
671 * DNS_RESP_REFUSED
672 */
673 if (dns_resp != DNS_RESP_VALID) {
674 /* now parse list of requesters currently waiting for this resolution */
675 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
676 requester->requester_error_cb(requester, dns_resp);
677
678 /* we can move the requester the wait queue */
679 LIST_DEL(&requester->list);
680 LIST_ADDQ(&resolution->requester.wait, &requester->list);
681 }
682 goto next_packet;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200683 }
684
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200685 /* Now let's check the query's dname corresponds to the one we sent.
686 * We can check only the first query of the list. We send one query at a time
687 * so we get one query in the response */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200688 query = LIST_NEXT(&resolution->response.query_list, struct dns_query_item *, list);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200689 if (!resolution->hostname_dn)
690 abort();
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200691 if (query && memcmp(query->name, resolution->hostname_dn, resolution->hostname_dn_len) != 0) {
692 nameserver->counters.other += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200693 /* now parse list of requesters currently waiting for this resolution */
694 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
695 requester->requester_error_cb(requester, DNS_RESP_WRONG_NAME);
696 /* we can move the requester the wait queue */
697 LIST_DEL(&requester->list);
698 LIST_ADDQ(&resolution->requester.wait, &requester->list);
699 }
700 goto next_packet;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200701 }
702
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200703 /* no errors, we can save the response in the cache */
704 if (dns_lru_tree) {
705 unsigned long long seed = 1;
706 struct chunk *buf = get_trash_chunk();
707 struct chunk *tmp = NULL;
708
709 chunk_reset(buf);
710 tmp = dns_cache_key(resolution->query_type, resolution->hostname_dn,
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200711 resolution->hostname_dn_len, buf);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200712 if (!tmp) {
713 nameserver->counters.other += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200714 /* now parse list of requesters currently waiting for this resolution */
715 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
716 requester->requester_error_cb(requester, DNS_RESP_ERROR);
717 /* we can move the requester the wait queue */
718 LIST_DEL(&requester->list);
719 LIST_ADDQ(&resolution->requester.wait, &requester->list);
720 }
721 goto next_packet;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200722 }
723
724 lru = lru64_get(XXH64(buf->str, buf->len, seed),
725 dns_lru_tree, nameserver->resolvers, 1);
726
727 lru64_commit(lru, resolution, nameserver->resolvers, 1, NULL);
728 }
729
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200730 if (resolution->status != RSLV_STATUS_VALID) {
731 resolution->status = RSLV_STATUS_VALID;
732 resolution->last_status_change = now_ms;
733 }
734
Baptiste Assmann37bb3722015-08-07 10:18:32 +0200735 nameserver->counters.valid += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200736 /* now parse list of requesters currently waiting for this resolution */
737 tmpnameserver = nameserver;
738 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
739 requester->requester_cb(requester, tmpnameserver);
740 /* we can move the requester the wait queue */
741 LIST_DEL(&requester->list);
742 LIST_ADDQ(&resolution->requester.wait, &requester->list);
743 /* first response is managed by the server, others are from the cache */
744 tmpnameserver = NULL;
745 }
746
747 next_packet:
748 /* resolution may be NULL when we receive an ICMP unreachable packet */
749 if (resolution && LIST_ISEMPTY(&resolution->requester.curr)) {
750 /* move the resolution into the wait queue */
751 LIST_DEL(&resolution->list);
752 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
753 /* update last resolution date and time */
754 resolution->last_resolution = now_ms;
755 /* reset current status flag */
756 resolution->step = RSLV_STEP_NONE;
757 /* reset values */
758 dns_reset_resolution(resolution);
759 }
760
761 } // end of while "packets" loop
762
763 dns_update_resolvers_timeout(nameserver->resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200764}
765
766/*
767 * function called when a resolvers network socket is ready to send data
768 * It performs the following actions:
769 */
770void dns_resolve_send(struct dgram_conn *dgram)
771{
772 int fd;
773 struct dns_nameserver *nameserver;
774 struct dns_resolvers *resolvers;
775 struct dns_resolution *resolution;
776
777 fd = dgram->t.sock.fd;
778
779 /* check if ready for sending */
780 if (!fd_send_ready(fd))
781 return;
782
783 /* we don't want/need to be waked up any more for sending */
784 fd_stop_send(fd);
785
786 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200787 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200788 return;
789
790 resolvers = nameserver->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200791 resolution = LIST_NEXT(&resolvers->resolution.curr, struct dns_resolution *, list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200792
793 dns_send_query(resolution);
794 dns_update_resolvers_timeout(resolvers);
795}
796
797/*
798 * forge and send a DNS query to resolvers associated to a resolution
799 * It performs the following actions:
800 * returns:
801 * 0 in case of error or safe ignorance
802 * 1 if no error
803 */
804int dns_send_query(struct dns_resolution *resolution)
805{
Baptiste Assmann42746372017-05-03 12:12:02 +0200806 struct dns_resolvers *resolvers = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200807 struct dns_nameserver *nameserver;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200808 struct dns_requester *requester = NULL;
Erwan Velu5457eb42015-10-15 15:07:26 +0200809 int ret, bufsize, fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200810
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200811 /* nothing to do */
812 if (LIST_ISEMPTY(&resolution->requester.curr))
813 return 0;
814
815 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
816
817 switch (obj_type(requester->requester)) {
818 case OBJ_TYPE_SERVER:
819 resolvers = objt_server(requester->requester)->resolvers;
820 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200821 case OBJ_TYPE_SRVRQ:
822 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
823 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200824 case OBJ_TYPE_NONE:
825 default:
826 return 0;
827 }
Baptiste Assmann42746372017-05-03 12:12:02 +0200828
829 if (!resolvers)
830 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200831
Baptiste Assmann325137d2015-04-13 23:40:55 +0200832 bufsize = dns_build_query(resolution->query_id, resolution->query_type, resolution->hostname_dn,
833 resolution->hostname_dn_len, trash.str, trash.size);
834
835 if (bufsize == -1)
836 return 0;
837
838 list_for_each_entry(nameserver, &resolvers->nameserver_list, list) {
839 fd = nameserver->dgram->t.sock.fd;
840 errno = 0;
841
842 ret = send(fd, trash.str, bufsize, 0);
843
844 if (ret > 0)
845 nameserver->counters.sent += 1;
846
847 if (ret == 0 || errno == EAGAIN) {
848 /* nothing written, let's update the poller that we wanted to send
849 * but we were not able to */
850 fd_want_send(fd);
851 fd_cant_send(fd);
852 }
853 }
854
855 /* update resolution */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200856 resolution->nb_responses = 0;
857 resolution->last_sent_packet = now_ms;
858
859 return 1;
860}
861
862/*
863 * update a resolvers' task timeout for next wake up
864 */
865void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
866{
867 struct dns_resolution *resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200868 struct dns_requester *requester;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200869
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200870 if ((LIST_ISEMPTY(&resolvers->resolution.curr)) && (LIST_ISEMPTY(&resolvers->resolution.wait))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200871 resolvers->t->expire = TICK_ETERNITY;
872 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200873 else if (!LIST_ISEMPTY(&resolvers->resolution.curr)) {
874 resolution = LIST_NEXT(&resolvers->resolution.curr, struct dns_resolution *, list);
875 if (!resolvers->t->expire || tick_is_le(resolvers->t->expire, tick_add(resolution->last_sent_packet, resolvers->timeout.retry))) {
876 resolvers->t->expire = tick_add(resolution->last_sent_packet, resolvers->timeout.retry);
877 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200878 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200879 else if (!LIST_ISEMPTY(&resolvers->resolution.wait)) {
880 int valid_period, inter, need_wakeup;
881 struct dns_resolution *res_back;
882 need_wakeup = 0;
883 list_for_each_entry_safe(resolution, res_back, &resolvers->resolution.wait, list) {
884 valid_period = 0;
885 inter = 0;
886
887 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
888
889 switch (obj_type(requester->requester)) {
890 case OBJ_TYPE_SERVER:
891 valid_period = objt_server(requester->requester)->check.inter;
892 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200893 case OBJ_TYPE_SRVRQ:
894 valid_period = objt_dns_srvrq(requester->requester)->inter;
895 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200896 case OBJ_TYPE_NONE:
897 default:
898 continue;
899 }
900
901 if (resolvers->hold.valid < valid_period)
902 inter = resolvers->hold.valid;
903 else
904 inter = valid_period;
905
906 if (tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms)) {
907 switch (obj_type(requester->requester)) {
908 case OBJ_TYPE_SERVER:
909 dns_trigger_resolution(objt_server(requester->requester)->resolution);
910 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200911 case OBJ_TYPE_SRVRQ:
912 dns_trigger_resolution(objt_dns_srvrq(requester->requester)->resolution);
913 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200914 case OBJ_TYPE_NONE:
915 default:
916 ;;
917 }
918 }
919 else {
920 need_wakeup = 1;
921 }
922 }
923 /* in such case, we wake up in 1s */
924 if (need_wakeup) {
925 int r = 1000;
926
927 resolution = LIST_NEXT(&resolvers->resolution.wait, struct dns_resolution *, list);
928 if (tick_is_le(resolvers->t->expire, tick_add(now_ms, r)))
929 resolvers->t->expire = tick_add(now_ms, r);
930 resolvers->t->expire = tick_add(now_ms, 1000);
931 }
932 }
933
934 task_queue(resolvers->t);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200935}
936
937/*
938 * Analyse, re-build and copy the name <name> from the DNS response packet <buffer>.
939 * <name> must point to the 'data_len' information or pointer 'c0' for compressed data.
940 * The result is copied into <dest>, ensuring we don't overflow using <dest_len>
941 * Returns the number of bytes the caller can move forward. If 0 it means an error occured
942 * while parsing the name.
943 * <offset> is the number of bytes the caller could move forward.
944 */
945int dns_read_name(unsigned char *buffer, unsigned char *bufend, unsigned char *name, char *destination, int dest_len, int *offset)
946{
947 int nb_bytes = 0, n = 0;
948 int label_len;
949 unsigned char *reader = name;
950 char *dest = destination;
951
952 while (1) {
953 /* name compression is in use */
954 if ((*reader & 0xc0) == 0xc0) {
955 /* a pointer must point BEFORE current position */
956 if ((buffer + reader[1]) > reader) {
957 goto out_error;
958 }
959
960 n = dns_read_name(buffer, bufend, buffer + reader[1], dest, dest_len - nb_bytes, offset);
961 if (n == 0)
962 goto out_error;
963
964 dest += n;
965 nb_bytes += n;
966 goto out;
967 }
968
969 label_len = *reader;
970 if (label_len == 0)
971 goto out;
972 /* Check if:
973 * - we won't read outside the buffer
974 * - there is enough place in the destination
975 */
976 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
977 goto out_error;
978
979 /* +1 to take label len + label string */
980 label_len += 1;
981
982 memcpy(dest, reader, label_len);
983
984 dest += label_len;
985 nb_bytes += label_len;
986 reader += label_len;
987 }
988
989 out:
990 /* offset computation:
991 * parse from <name> until finding either NULL or a pointer "c0xx"
992 */
993 reader = name;
994 *offset = 0;
995 while (reader < bufend) {
996 if ((reader[0] & 0xc0) == 0xc0) {
997 *offset += 2;
998 break;
999 }
1000 else if (*reader == 0) {
1001 *offset += 1;
1002 break;
1003 }
1004 *offset += 1;
1005 ++reader;
1006 }
1007
1008 return nb_bytes;
1009
1010 out_error:
1011 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001012}
1013
1014/*
1015 * Function to validate that the buffer DNS response provided in <resp> and
1016 * finishing before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001017 *
Baptiste Assmann729c9012017-05-22 15:13:10 +02001018 * The result is stored in <resolution>' response, buf_response, response_query_records
1019 * and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001020 *
1021 * This function returns one of the DNS_RESP_* code to indicate the type of
1022 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001023 */
Baptiste Assmann729c9012017-05-22 15:13:10 +02001024int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001025{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001026 unsigned char *reader;
1027 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001028 int len, flags, offset;
1029 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +02001030 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001031 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001032 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +02001033 struct dns_response_packet *dns_p;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001034 int found = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001035 int i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001036
1037 reader = resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001038 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001039 previous_dname = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001040
Baptiste Assmann729c9012017-05-22 15:13:10 +02001041 /* initialization of response buffer and structure */
1042 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001043
1044 /* query id */
1045 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001046 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001047 dns_p->header.id = reader[0] * 256 + reader[1];
1048 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001049
1050 /*
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001051 * flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001052 * First byte contains:
1053 * - response flag (1 bit)
1054 * - opcode (4 bits)
1055 * - authoritative (1 bit)
1056 * - truncated (1 bit)
1057 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001058 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001059 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001060 return DNS_RESP_INVALID;
1061
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001062 flags = reader[0] * 256 + reader[1];
1063
1064 if (flags & DNS_FLAG_TRUNCATED)
1065 return DNS_RESP_TRUNCATED;
1066
1067 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
1068 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001069 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001070 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001071 return DNS_RESP_REFUSED;
1072
1073 return DNS_RESP_ERROR;
1074 }
1075
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001076 /* move forward 2 bytes for flags */
1077 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001078
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001079 /* 2 bytes for question count */
1080 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001082 dns_p->header.qdcount = reader[0] * 256 + reader[1];
1083 /* (for now) we send one query only, so we expect only one in the response too */
1084 if (dns_p->header.qdcount != 1)
1085 return DNS_RESP_QUERY_COUNT_ERROR;
1086 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001087 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001088 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001089
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001090 /* 2 bytes for answer count */
1091 if (reader + 2 >= bufend)
1092 return DNS_RESP_INVALID;
1093 dns_p->header.ancount = reader[0] * 256 + reader[1];
1094 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001095 return DNS_RESP_ANCOUNT_ZERO;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001096 /* check if too many records are announced */
1097 if (dns_p->header.ancount > DNS_MAX_ANSWER_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001098 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001099 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001100
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001101 /* 2 bytes authority count */
1102 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001103 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001104 dns_p->header.nscount = reader[0] * 256 + reader[1];
1105 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001106
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001107 /* 2 bytes additional count */
1108 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001110 dns_p->header.arcount = reader[0] * 256 + reader[1];
1111 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001113 /* parsing dns queries */
1114 LIST_INIT(&dns_p->query_list);
1115 for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) {
1116 /* use next pre-allocated dns_query_item after ensuring there is
1117 * still one available.
1118 * It's then added to our packet query list.
1119 */
1120 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
1121 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +02001122 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001123 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001124
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001125 /* name is a NULL terminated string in our case, since we have
1126 * one query per response and the first one can't be compressed
1127 * (using the 0x0c format)
1128 */
1129 offset = 0;
1130 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001131
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001132 if (len == 0)
1133 return DNS_RESP_INVALID;
1134
1135 reader += offset;
1136 previous_dname = dns_query->name;
1137
1138 /* move forward 2 bytes for question type */
1139 if (reader + 2 >= bufend)
1140 return DNS_RESP_INVALID;
1141 dns_query->type = reader[0] * 256 + reader[1];
1142 reader += 2;
1143
1144 /* move forward 2 bytes for question class */
1145 if (reader + 2 >= bufend)
1146 return DNS_RESP_INVALID;
1147 dns_query->class = reader[0] * 256 + reader[1];
1148 reader += 2;
1149 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001150
1151 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001152 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001153 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001154 if (reader >= bufend)
1155 return DNS_RESP_INVALID;
1156
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001157 dns_answer_record = pool_alloc2(dns_answer_item_pool);
1158 if (dns_answer_record == NULL)
1159 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001160
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001161 offset = 0;
1162 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001163
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001164 if (len == 0) {
1165 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001166 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001167 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001168
1169 /* check if the current record dname is valid.
1170 * previous_dname points either to queried dname or last CNAME target
1171 */
1172 if (memcmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001173 free_dns_answer_item(dns_answer_record);
1174 if (i == 0) {
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001175 /* first record, means a mismatch issue between queried dname
1176 * and dname found in the first record */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001177 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001178 } else {
1179 /* if not the first record, this means we have a CNAME resolution
1180 * error */
1181 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001182 }
1183
Baptiste Assmann325137d2015-04-13 23:40:55 +02001184 }
1185
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001186 memcpy(dns_answer_record->name, tmpname, len);
1187 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +02001188
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001189 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001190 if (reader >= bufend) {
1191 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001192 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001193 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001194
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001195 if (reader >= bufend) {
1196 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001197 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001198 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001199
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001200 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001201 if (reader + 2 > bufend) {
1202 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001203 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001204 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001205 dns_answer_record->type = reader[0] * 256 + reader[1];
1206 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001207
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001208 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001209 if (reader + 2 > bufend) {
1210 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001211 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001212 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001213 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001214 reader += 2;
1215
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001216 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001217 if (reader + 4 > bufend) {
1218 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001219 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001220 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001221 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1222 + reader[2] * 256 + reader[3];
1223 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001224
1225 /* now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001226 if (reader + 2 > bufend) {
1227 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001228 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001229 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001230 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001231
1232 /* move forward 2 bytes for data len */
1233 reader += 2;
1234
1235 /* analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001236 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001237 case DNS_RTYPE_A:
1238 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001239 if (dns_answer_record->data_len != 4) {
1240 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001241 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001242 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001243 dns_answer_record->address.sa_family = AF_INET;
1244 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1245 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001246 break;
1247
1248 case DNS_RTYPE_CNAME:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001249 /* check if this is the last record and update the caller about the status:
1250 * no IP could be found and last record was a CNAME. Could be triggered
1251 * by a wrong query type
1252 *
1253 * + 1 because dns_answer_record_id starts at 0 while number of answers
1254 * is an integer and starts at 1.
1255 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001256 if (i + 1 == dns_p->header.ancount) {
1257 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001258 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001259 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001260
1261 offset = 0;
1262 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
1263
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001264 if (len == 0) {
1265 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001266 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001267 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001268
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001269 memcpy(dns_answer_record->target, tmpname, len);
1270 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001271
1272 previous_dname = dns_answer_record->target;
1273
Baptiste Assmann325137d2015-04-13 23:40:55 +02001274 break;
1275
Olivier Houchard8da5f982017-08-04 18:35:36 +02001276
1277 case DNS_RTYPE_SRV:
1278 /*
1279 * Answer must contain :
1280 * - 2 bytes for the priority
1281 * - 2 bytes for the weight
1282 * - 2 bytes for the port
1283 * - the target hostname
1284 */
1285 if (dns_answer_record->data_len <= 6) {
1286 free_dns_answer_item(dns_answer_record);
1287 return DNS_RESP_INVALID;
1288 }
1289 dns_answer_record->priority = readn16(reader);
1290 reader += sizeof(uint16_t);
1291 dns_answer_record->weight = readn16(reader);
1292 reader += sizeof(uint16_t);
1293 dns_answer_record->port = readn16(reader);
1294 reader += sizeof(uint16_t);
1295 offset = 0;
1296 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
1297 if (len == 0) {
1298 free_dns_answer_item(dns_answer_record);
1299 return DNS_RESP_INVALID;
1300 }
1301 reader++;
1302 dns_answer_record->data_len = len;
1303 memcpy(dns_answer_record->target, tmpname, len);
1304 dns_answer_record->target[len] = 0;
1305 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001306 case DNS_RTYPE_AAAA:
1307 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001308 if (dns_answer_record->data_len != 16) {
1309 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001310 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001311 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001312 dns_answer_record->address.sa_family = AF_INET6;
1313 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1314 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001315 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001316
Baptiste Assmann325137d2015-04-13 23:40:55 +02001317 } /* switch (record type) */
1318
Baptiste Assmann69fce672017-05-04 08:37:45 +02001319 /* increment the counter for number of records saved into our local response */
1320 nb_saved_records += 1;
1321
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001322 /* move forward dns_answer_record->data_len for analyzing next record in the response */
1323 reader += dns_answer_record->data_len;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001324
1325 /* Lookup to see if we already had this entry */
1326
Olivier Houchard8da5f982017-08-04 18:35:36 +02001327 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001328 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1329 if (tmp_record->type != dns_answer_record->type)
1330 continue;
1331 switch (tmp_record->type) {
1332 case DNS_RTYPE_A:
1333 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1334 &((struct sockaddr_in *)&tmp_record->address)->sin_addr, sizeof(in_addr_t)))
1335 found = 1;
1336 break;
1337 case DNS_RTYPE_AAAA:
1338 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1339 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr, sizeof(struct in6_addr)))
1340 found = 1;
1341 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001342 case DNS_RTYPE_SRV:
1343 if (dns_answer_record->data_len == tmp_record->data_len &&
1344 !memcmp(dns_answer_record->target,
1345 tmp_record->target, dns_answer_record->data_len) &&
1346 dns_answer_record->port == tmp_record->port) {
1347 tmp_record->weight = dns_answer_record->weight;
1348 found = 1;
1349 }
1350 break;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001351 default:
1352 break;
1353 }
1354 if (found == 1)
1355 break;
1356 }
1357 if (found == 1) {
1358 tmp_record->last_seen = now.tv_sec;
1359 free_dns_answer_item(dns_answer_record);
1360 } else {
1361 dns_answer_record->last_seen = now.tv_sec;
1362 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1363 }
1364
Baptiste Assmann325137d2015-04-13 23:40:55 +02001365 } /* for i 0 to ancount */
1366
Baptiste Assmann96972bc2015-09-09 00:46:58 +02001367
Baptiste Assmann69fce672017-05-04 08:37:45 +02001368 /* save the number of records we really own */
1369 dns_p->header.ancount = nb_saved_records;
1370
Baptiste Assmann325137d2015-04-13 23:40:55 +02001371 return DNS_RESP_VALID;
1372}
1373
1374/*
1375 * search dn_name resolution in resp.
1376 * If existing IP not found, return the first IP matching family_priority,
1377 * otherwise, first ip found
1378 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001379 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001380 * For both cases above, dns_validate_dns_response is required
1381 * returns one of the DNS_UPD_* code
1382 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001383#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001384int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001385 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001386 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001387 void **newip, short *newip_sin_family,
1388 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001389{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001390 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001391 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001392 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001393 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001394 int currentip_sel;
1395 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001396 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001397
Baptiste Assmann42746372017-05-03 12:12:02 +02001398 family_priority = dns_opts->family_prio;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001399 *newip = newip4 = newip6 = NULL;
1400 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001401 *newip_sin_family = AF_UNSPEC;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001402 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001403
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001404 /* Select an IP regarding configuration preference.
1405 * Top priority is the prefered network ip version,
1406 * second priority is the prefered network.
1407 * the last priority is the currently used IP,
1408 *
1409 * For these three priorities, a score is calculated. The
1410 * weight are:
Baptistefc725902016-12-26 23:21:08 +01001411 * 8 - prefered netwok ip version.
1412 * 4 - prefered network.
1413 * 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 +01001414 * 1 - current ip.
1415 * The result with the biggest score is returned.
1416 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001417
1418 list_for_each_entry(record, &dns_p->answer_list, list) {
1419 void *ip;
1420 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001421
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001422 if (record->type == DNS_RTYPE_A) {
1423 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1424 ip_type = AF_INET;
1425 } else if (record->type == DNS_RTYPE_AAAA) {
1426 ip_type = AF_INET6;
1427 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
1428 } else
1429 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001430 score = 0;
1431
1432 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001433 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001434 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001435
1436 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001437 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001438
1439 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001440 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001441 continue;
1442
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001443 if ((ip_type == AF_INET &&
1444 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001445 &dns_opts->pref_net[j].mask.in4,
1446 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001447 (ip_type == AF_INET6 &&
1448 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001449 &dns_opts->pref_net[j].mask.in6,
1450 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001451 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001452 break;
1453 }
1454 }
1455
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001456 /* Check if the IP found in the record is already affected to a member of a group.
1457 * If yes, the score should be incremented by 2.
1458 */
1459 if (owner) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001460 if (snr_check_ip_callback(owner, ip, &ip_type))
1461 {
1462 continue;
1463 }
Baptistefc725902016-12-26 23:21:08 +01001464 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001465 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001466 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001467 ((currentip_sin_family == AF_INET &&
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001468 memcmp(ip, currentip, 4) == 0) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001469 (currentip_sin_family == AF_INET6 &&
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001470 memcmp(ip, currentip, 16) == 0))) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001471 score += 1;
1472 currentip_sel = 1;
1473 } else
1474 currentip_sel = 0;
1475
Baptistefc725902016-12-26 23:21:08 +01001476
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001477 /* Keep the address if the score is better than the previous
Baptistefc725902016-12-26 23:21:08 +01001478 * score. The maximum score is 15, if this value is reached,
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001479 * we break the parsing. Implicitly, this score is reached
1480 * the ip selected is the current ip.
1481 */
1482 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001483 if (ip_type == AF_INET)
1484 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001485 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001486 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001487 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001488 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001489 return DNS_UPD_NO;
1490 max_score = score;
1491 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001492
1493
1494 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001495
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001496 /* no IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001497 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001498 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001499
Baptiste Assmann325137d2015-04-13 23:40:55 +02001500 /* case when the caller looks first for an IPv4 address */
1501 if (family_priority == AF_INET) {
1502 if (newip4) {
1503 *newip = newip4;
1504 *newip_sin_family = AF_INET;
1505 if (currentip_found == 1)
1506 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001507 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001508 }
1509 else if (newip6) {
1510 *newip = newip6;
1511 *newip_sin_family = AF_INET6;
1512 if (currentip_found == 1)
1513 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001514 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001515 }
1516 }
1517 /* case when the caller looks first for an IPv6 address */
1518 else if (family_priority == AF_INET6) {
1519 if (newip6) {
1520 *newip = newip6;
1521 *newip_sin_family = AF_INET6;
1522 if (currentip_found == 1)
1523 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001524 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001525 }
1526 else if (newip4) {
1527 *newip = newip4;
1528 *newip_sin_family = AF_INET;
1529 if (currentip_found == 1)
1530 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001531 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001532 }
1533 }
1534 /* case when the caller have no preference (we prefer IPv6) */
1535 else if (family_priority == AF_UNSPEC) {
1536 if (newip6) {
1537 *newip = newip6;
1538 *newip_sin_family = AF_INET6;
1539 if (currentip_found == 1)
1540 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001541 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001542 }
1543 else if (newip4) {
1544 *newip = newip4;
1545 *newip_sin_family = AF_INET;
1546 if (currentip_found == 1)
1547 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001548 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001549 }
1550 }
1551
1552 /* no reason why we should change the server's IP address */
1553 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001554
1555 return_DNS_UPD_SRVIP_NOT_FOUND:
1556 list_for_each_entry(record, &dns_p->answer_list, list) {
1557 /* move the first record to the end of the list, for internal round robin */
1558 if (record) {
1559 LIST_DEL(&record->list);
1560 LIST_ADDQ(&dns_p->answer_list, &record->list);
1561 break;
1562 }
1563 }
1564 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001565}
1566
1567/*
1568 * returns the query id contained in a DNS response
1569 */
Thiago Farinab1af23e2016-01-20 23:46:34 +01001570unsigned short dns_response_get_query_id(unsigned char *resp)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001571{
1572 /* read the query id from the response */
1573 return resp[0] * 256 + resp[1];
1574}
1575
1576/*
1577 * used during haproxy's init phase
1578 * parses resolvers sections and initializes:
1579 * - task (time events) for each resolvers section
1580 * - the datagram layer (network IO events) for each nameserver
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001581 * It takes one argument:
1582 * - close_first takes 2 values: 0 or 1. If 1, the connection is closed first.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001583 * returns:
1584 * 0 in case of error
1585 * 1 when no error
1586 */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001587int dns_init_resolvers(int close_socket)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001588{
1589 struct dns_resolvers *curr_resolvers;
1590 struct dns_nameserver *curnameserver;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001591 struct dns_resolution *resolution, *res_back;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001592 struct dgram_conn *dgram;
1593 struct task *t;
1594 int fd;
1595
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001596 /* initialize our DNS resolution cache */
1597 dns_lru_tree = lru64_new(dns_cache_size);
1598
Baptiste Assmann325137d2015-04-13 23:40:55 +02001599 /* give a first random value to our dns query_id seed */
1600 dns_query_id_seed = random();
1601
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001602 /* Initialize the answer items pool */
1603 dns_answer_item_pool = create_pool("dns_answer_item",
1604 sizeof(struct dns_answer_item), MEM_F_SHARED);
1605 if (dns_answer_item_pool == NULL) {
1606 Alert("Failed to create the dns answer items pool");
1607 return 0;
1608 }
1609
Baptiste Assmann325137d2015-04-13 23:40:55 +02001610 /* run through the resolvers section list */
1611 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
1612 /* create the task associated to the resolvers section */
1613 if ((t = task_new()) == NULL) {
1614 Alert("Starting [%s] resolvers: out of memory.\n", curr_resolvers->id);
1615 return 0;
1616 }
1617
1618 /* update task's parameters */
1619 t->process = dns_process_resolve;
1620 t->context = curr_resolvers;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001621
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001622 /* no need to keep the new task if one is already affected to our resolvers
1623 * section */
1624 if (!curr_resolvers->t)
1625 curr_resolvers->t = t;
1626 else
1627 task_free(t);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001628
1629 list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001630 dgram = NULL;
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001631
1632 if (close_socket == 1) {
1633 if (curnameserver->dgram) {
Frédéric Lécaille64920532017-05-12 09:57:15 +02001634 fd_delete(curnameserver->dgram->t.sock.fd);
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001635 memset(curnameserver->dgram, '\0', sizeof(*dgram));
1636 dgram = curnameserver->dgram;
1637 }
1638 }
1639
1640 /* allocate memory only if it has not already been allocated
1641 * by a previous call to this function */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001642
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001643 if (!dgram && (dgram = calloc(1, sizeof(*dgram))) == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001644 Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id,
1645 curnameserver->id);
1646 return 0;
1647 }
1648 /* update datagram's parameters */
1649 dgram->owner = (void *)curnameserver;
1650 dgram->data = &resolve_dgram_cb;
1651
1652 /* create network UDP socket for this nameserver */
Frédéric Lécaille5e5bc9f2017-04-11 08:46:37 +02001653 if ((fd = socket(curnameserver->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001654 Alert("Starting [%s/%s] nameserver: can't create socket.\n", curr_resolvers->id,
1655 curnameserver->id);
1656 free(dgram);
1657 dgram = NULL;
1658 return 0;
1659 }
1660
1661 /* "connect" the UDP socket to the name server IP */
Baptiste Assmann8c62c472015-09-21 20:55:08 +02001662 if (connect(fd, (struct sockaddr*)&curnameserver->addr, get_addr_len(&curnameserver->addr)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001663 Alert("Starting [%s/%s] nameserver: can't connect socket.\n", curr_resolvers->id,
1664 curnameserver->id);
1665 close(fd);
1666 free(dgram);
1667 dgram = NULL;
1668 return 0;
1669 }
1670
1671 /* make the socket non blocking */
1672 fcntl(fd, F_SETFL, O_NONBLOCK);
1673
1674 /* add the fd in the fd list and update its parameters */
1675 fd_insert(fd);
1676 fdtab[fd].owner = dgram;
1677 fdtab[fd].iocb = dgram_fd_handler;
1678 fd_want_recv(fd);
1679 dgram->t.sock.fd = fd;
1680
1681 /* update nameserver's datagram property */
1682 curnameserver->dgram = dgram;
1683
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001684 continue;
1685 }
1686
1687 if (close_socket == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001688 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001689
1690 /* now, we can trigger DNS resolution */
1691 list_for_each_entry_safe(resolution, res_back, &curr_resolvers->resolution.wait, list) {
1692 /* if there is no requester in the wait queue, no need to trigger the resolution */
1693 if (LIST_ISEMPTY(&resolution->requester.wait))
1694 continue;
1695
1696 dns_trigger_resolution(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001697 }
1698
1699 /* task can be queued */
1700 task_queue(t);
1701 }
1702
1703 return 1;
1704}
1705
1706/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001707 * Allocate a pool of resolution to a resolvers section.
1708 * Each resolution is associated with a UUID.
1709 *
1710 * Return code:
1711 * - 0 if everything went smoothly
1712 * - -1 if an error occured
1713 */
1714int dns_alloc_resolution_pool(struct dns_resolvers *resolvers)
1715{
1716 int i;
1717 struct dns_resolution *resolution;
1718
1719 /* return if a pool has already been set for this resolvers */
1720 if (!LIST_ISEMPTY(&resolvers->resolution.pool)) {
1721 return 0;
1722 }
1723
1724 for (i = 0; i < resolvers->resolution_pool_size; i++) {
1725 resolution = dns_alloc_resolution();
1726 if (!resolution) {
1727 Alert("Starting [%s] resolvers: can't allocate memory for DNS resolution pool.\n", resolvers->id);
1728 return -1;
1729 }
1730 resolution->uuid = i;
1731 LIST_ADDQ(&resolvers->resolution.pool, &resolution->list);
1732 }
1733
1734 return 0;
1735}
1736
1737/*
Baptiste Assmann325137d2015-04-13 23:40:55 +02001738 * Forge a DNS query. It needs the following information from the caller:
1739 * - <query_id>: the DNS query id corresponding to this query
1740 * - <query_type>: DNS_RTYPE_* request DNS record type (A, AAAA, ANY, etc...)
1741 * - <hostname_dn>: hostname in domain name format
1742 * - <hostname_dn_len>: length of <hostname_dn>
1743 * To store the query, the caller must pass a buffer <buf> and its size <bufsize>
1744 *
1745 * the DNS query is stored in <buf>
1746 * returns:
1747 * -1 if <buf> is too short
1748 */
1749int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
1750{
1751 struct dns_header *dns;
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001752 struct dns_question qinfo;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001753 char *ptr, *bufend;
1754
1755 memset(buf, '\0', bufsize);
1756 ptr = buf;
1757 bufend = buf + bufsize;
1758
1759 /* check if there is enough room for DNS headers */
1760 if (ptr + sizeof(struct dns_header) >= bufend)
1761 return -1;
1762
1763 /* set dns query headers */
1764 dns = (struct dns_header *)ptr;
1765 dns->id = (unsigned short) htons(query_id);
Nenad Merdanovic8ab79422016-07-13 14:03:43 +02001766 dns->flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001767 dns->qdcount = htons(1); /* 1 question */
1768 dns->ancount = 0;
1769 dns->nscount = 0;
1770 dns->arcount = 0;
1771
1772 /* move forward ptr */
1773 ptr += sizeof(struct dns_header);
1774
1775 /* check if there is enough room for query hostname */
1776 if ((ptr + hostname_dn_len) >= bufend)
1777 return -1;
1778
1779 /* set up query hostname */
1780 memcpy(ptr, hostname_dn, hostname_dn_len);
1781 ptr[hostname_dn_len + 1] = '\0';
1782
1783 /* move forward ptr */
1784 ptr += (hostname_dn_len + 1);
1785
1786 /* check if there is enough room for query hostname*/
1787 if (ptr + sizeof(struct dns_question) >= bufend)
1788 return -1;
1789
1790 /* set up query info (type and class) */
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001791 qinfo.qtype = htons(query_type);
1792 qinfo.qclass = htons(DNS_RCLASS_IN);
1793 memcpy(ptr, &qinfo, sizeof(qinfo));
Baptiste Assmann325137d2015-04-13 23:40:55 +02001794
1795 ptr += sizeof(struct dns_question);
1796
1797 return ptr - buf;
1798}
1799
Olivier Houchard8da5f982017-08-04 18:35:36 +02001800/* Turn a domain name label into a string */
1801void dns_dn_label_to_str(char *dn, char *str, int dn_len)
1802{
1803 int remain_size = 0;
1804 int i;
1805
1806 for (i = 0; i < dn_len; i++) {
1807 if (remain_size == 0) {
1808 remain_size = dn[i];
1809 if (i != 0) {
1810 str[i - 1] = '.';
1811
1812 }
1813 } else {
1814 str[i - 1] = dn[i];
1815 remain_size--;
1816 }
1817 }
1818 str[dn_len - 1] = 0;
1819
1820}
1821
Baptiste Assmann325137d2015-04-13 23:40:55 +02001822/*
1823 * turn a string into domain name label:
1824 * www.haproxy.org into 3www7haproxy3org
1825 * if dn memory is pre-allocated, you must provide its size in dn_len
1826 * if dn memory isn't allocated, dn_len must be set to 0.
1827 * In the second case, memory will be allocated.
1828 * in case of error, -1 is returned, otherwise, number of bytes copied in dn
1829 */
Willy Tarreau2100b492015-07-22 16:42:43 +02001830char *dns_str_to_dn_label(const char *string, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001831{
1832 char *c, *d;
1833 int i, offset;
1834
1835 /* offset between string size and theorical dn size */
1836 offset = 1;
1837
1838 /*
1839 * first, get the size of the string turned into its domain name version
1840 * This function also validates the string respect the RFC
1841 */
1842 if ((i = dns_str_to_dn_label_len(string)) == -1)
1843 return NULL;
1844
1845 /* yes, so let's check there is enough memory */
1846 if (dn_len < i + offset)
1847 return NULL;
1848
Willy Tarreaud69d6f32015-07-22 16:45:36 +02001849 i = strlen(string);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001850 memcpy(dn + offset, string, i);
1851 dn[i + offset] = '\0';
1852 /* avoid a '\0' at the beginning of dn string which may prevent the for loop
1853 * below from working.
1854 * Actually, this is the reason of the offset. */
1855 dn[0] = '0';
1856
1857 for (c = dn; *c ; ++c) {
1858 /* c points to the first '0' char or a dot, which we don't want to read */
1859 d = c + offset;
1860 i = 0;
1861 while (*d != '.' && *d) {
1862 i++;
1863 d++;
1864 }
1865 *c = i;
1866
1867 c = d - 1; /* because of c++ of the for loop */
1868 }
1869
1870 return dn;
1871}
1872
1873/*
1874 * compute and return the length of <string> it it were translated into domain name
1875 * label:
1876 * www.haproxy.org into 3www7haproxy3org would return 16
1877 * NOTE: add +1 for '\0' when allocating memory ;)
1878 */
1879int dns_str_to_dn_label_len(const char *string)
1880{
1881 return strlen(string) + 1;
1882}
1883
1884/*
1885 * validates host name:
1886 * - total size
1887 * - each label size individually
1888 * returns:
1889 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1890 * 1 when no error. <err> is left unaffected.
1891 */
1892int dns_hostname_validation(const char *string, char **err)
1893{
1894 const char *c, *d;
1895 int i;
1896
1897 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1898 if (err)
1899 *err = DNS_TOO_LONG_FQDN;
1900 return 0;
1901 }
1902
1903 c = string;
1904 while (*c) {
1905 d = c;
1906
1907 i = 0;
1908 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1909 i++;
1910 if (!((*d == '-') || (*d == '_') ||
1911 ((*d >= 'a') && (*d <= 'z')) ||
1912 ((*d >= 'A') && (*d <= 'Z')) ||
1913 ((*d >= '0') && (*d <= '9')))) {
1914 if (err)
1915 *err = DNS_INVALID_CHARACTER;
1916 return 0;
1917 }
1918 d++;
1919 }
1920
1921 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1922 if (err)
1923 *err = DNS_LABEL_TOO_LONG;
1924 return 0;
1925 }
1926
1927 if (*d == '\0')
1928 goto out;
1929
1930 c = ++d;
1931 }
1932 out:
1933 return 1;
1934}
1935
1936/*
1937 * 2 bytes random generator to generate DNS query ID
1938 */
1939uint16_t dns_rnd16(void)
1940{
1941 dns_query_id_seed ^= dns_query_id_seed << 13;
1942 dns_query_id_seed ^= dns_query_id_seed >> 7;
1943 dns_query_id_seed ^= dns_query_id_seed << 17;
1944 return dns_query_id_seed;
1945}
1946
1947
1948/*
1949 * function called when a timeout occurs during name resolution process
1950 * if max number of tries is reached, then stop, otherwise, retry.
1951 */
1952struct task *dns_process_resolve(struct task *t)
1953{
1954 struct dns_resolvers *resolvers = t->context;
1955 struct dns_resolution *resolution, *res_back;
Baptiste Assmann060e5732016-01-06 02:01:59 +01001956 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmann42746372017-05-03 12:12:02 +02001957 struct dns_options *dns_opts = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001958
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001959 /* if both there is no resolution in the run queue, we can re-schedule a wake up */
1960 if (LIST_ISEMPTY(&resolvers->resolution.curr)) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001961 /* no first entry, so wake up was useless */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001962 dns_update_resolvers_timeout(resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001963 return t;
1964 }
1965
1966 /* look for the first resolution which is not expired */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001967 list_for_each_entry_safe(resolution, res_back, &resolvers->resolution.curr, list) {
1968 struct dns_requester *requester = NULL;
1969
Baptiste Assmann325137d2015-04-13 23:40:55 +02001970 /* when we find the first resolution in the future, then we can stop here */
1971 if (tick_is_le(now_ms, resolution->last_sent_packet))
1972 goto out;
1973
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001974 if (LIST_ISEMPTY(&resolution->requester.curr))
1975 goto out;
1976
Baptiste Assmann325137d2015-04-13 23:40:55 +02001977 /*
1978 * if current resolution has been tried too many times and finishes in timeout
1979 * we update its status and remove it from the list
1980 */
Baptiste Assmannf778bb42015-09-09 00:54:38 +02001981 if (resolution->try <= 0) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001982 struct dns_requester *tmprequester;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001983 /* clean up resolution information and remove from the list */
1984 dns_reset_resolution(resolution);
1985
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001986 LIST_DEL(&resolution->list);
1987 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
1988
1989 if (resolution->status != RSLV_STATUS_TIMEOUT) {
1990 resolution->status = RSLV_STATUS_TIMEOUT;
1991 resolution->last_status_change = now_ms;
1992 }
1993
1994 /* notify the result to the requesters */
1995 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
1996 requester->requester_error_cb(requester, DNS_RESP_TIMEOUT);
1997 LIST_DEL(&requester->list);
1998 LIST_ADDQ(&resolution->requester.wait, &requester->list);
1999 }
Baptiste Assmann382824c2016-01-06 01:53:46 +01002000 goto out;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002001 }
2002
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002003 resolution->try -= 1;
2004
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002005 /* running queue is empty, nothing to do but wait */
2006 if (LIST_ISEMPTY(&resolution->requester.curr))
2007 goto out;
2008
2009 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
2010
2011 switch (obj_type(requester->requester)) {
2012 case OBJ_TYPE_SERVER:
2013 dns_opts = &(objt_server(requester->requester)->dns_opts);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002014 res_preferred_afinet = dns_opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
2015 res_preferred_afinet6 = dns_opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
2016
2017 /* let's change the query type if needed */
2018 if (res_preferred_afinet6) {
2019 /* fallback from AAAA to A */
2020 resolution->query_type = DNS_RTYPE_A;
2021 }
2022 else if (res_preferred_afinet) {
2023 /* fallback from A to AAAA */
2024 resolution->query_type = DNS_RTYPE_AAAA;
2025 }
2026
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002027 break;
2028
Olivier Houchard8da5f982017-08-04 18:35:36 +02002029 case OBJ_TYPE_SRVRQ:
2030 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002031 case OBJ_TYPE_NONE:
2032 default:
2033 /* clean up resolution information and remove from the list */
2034 dns_reset_resolution(resolution);
2035
2036 LIST_DEL(&resolution->list);
2037 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
2038
2039 /* notify the result to the requester */
2040 requester->requester_error_cb(requester, DNS_RESP_INTERNAL);
2041 goto out;
2042 }
Baptiste Assmann42746372017-05-03 12:12:02 +02002043
Baptiste Assmann382824c2016-01-06 01:53:46 +01002044 /* resend the DNS query */
2045 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002046
Baptiste Assmann382824c2016-01-06 01:53:46 +01002047 /* check if we have more than one resolution in the list */
2048 if (dns_check_resolution_queue(resolvers) > 1) {
2049 /* move the rsolution to the end of the list */
2050 LIST_DEL(&resolution->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002051 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002052 }
2053 }
2054
2055 out:
2056 dns_update_resolvers_timeout(resolvers);
2057 return t;
2058}
William Lallemand69e96442016-11-19 00:58:54 +01002059
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002060/*
2061 * build a dns cache key composed as follow:
2062 * <query type>#<hostname in domain name format>
2063 * and store it into <str>.
2064 * It's up to the caller to allocate <buf> and to reset it.
2065 * The function returns NULL in case of error (IE <buf> too small) or a pointer
2066 * to buf if successful
2067 */
2068struct chunk *
2069dns_cache_key(int query_type, char *hostname_dn, int hostname_dn_len, struct chunk *buf)
2070{
2071 int len, size;
2072 char *str;
2073
2074 str = buf->str;
2075 len = buf->len;
2076 size = buf->size;
2077
2078 switch (query_type) {
2079 case DNS_RTYPE_A:
2080 if (len + 1 > size)
2081 return NULL;
2082 memcpy(&str[len], "A", 1);
2083 len += 1;
2084 break;
2085 case DNS_RTYPE_AAAA:
2086 if (len + 4 > size)
2087 return NULL;
2088 memcpy(&str[len], "AAAA", 4);
2089 len += 4;
2090 break;
2091 default:
2092 return NULL;
2093 }
2094
2095 if (len + 1 > size)
2096 return NULL;
2097 memcpy(&str[len], "#", 1);
2098 len += 1;
2099
2100 if (len + hostname_dn_len + 1 > size) // +1 for trailing zero
2101 return NULL;
2102 memcpy(&str[len], hostname_dn, hostname_dn_len);
2103 len += hostname_dn_len;
2104 str[len] = '\0';
2105
2106 return buf;
2107}
2108
2109/*
2110 * returns a pointer to a cache entry which may still be considered as up to date
2111 * by the caller.
2112 * returns NULL if no entry can be found or if the data found is outdated.
2113 */
2114struct lru64 *
2115dns_cache_lookup(int query_type, char *hostname_dn, int hostname_dn_len, int valid_period, void *cache_domain) {
2116 struct lru64 *elem = NULL;
2117 struct dns_resolution *resolution = NULL;
2118 struct dns_resolvers *resolvers = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002119 struct dns_requester *requester = NULL;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002120 int inter = 0;
2121 struct chunk *buf = get_trash_chunk();
2122 struct chunk *tmp = NULL;
2123
2124 if (!dns_lru_tree)
2125 return NULL;
2126
2127 chunk_reset(buf);
2128 tmp = dns_cache_key(query_type, hostname_dn, hostname_dn_len, buf);
2129 if (tmp == NULL)
2130 return NULL;
2131
2132 elem = lru64_lookup(XXH64(buf->str, buf->len, 1), dns_lru_tree, cache_domain, 1);
2133
2134 if (!elem || !elem->data)
2135 return NULL;
2136
2137 resolution = elem->data;
2138
2139 /* since we can change the fqdn of a server at run time, it may happen that
2140 * we got an innacurate elem.
2141 * This is because resolution->hostname_dn points to (owner)->hostname_dn (which
2142 * may be changed at run time)
2143 */
2144 if ((hostname_dn_len == resolution->hostname_dn_len) &&
2145 (memcmp(hostname_dn, resolution->hostname_dn, hostname_dn_len) != 0)) {
2146 return NULL;
2147 }
2148
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002149 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2150
2151 switch (obj_type(requester->requester)) {
2152 case OBJ_TYPE_SERVER:
2153 resolvers = objt_server(requester->requester)->resolvers;
2154 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002155 case OBJ_TYPE_SRVRQ:
2156 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
2157 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002158 case OBJ_TYPE_NONE:
2159 default:
2160 return NULL;
2161 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002162
2163 if (!resolvers)
2164 return NULL;
2165
2166 if (resolvers->hold.valid < valid_period)
2167 inter = resolvers->hold.valid;
2168 else
2169 inter = valid_period;
2170
2171 if (!tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms))
2172 return elem;
2173
2174 return NULL;
2175}
2176
Willy Tarreau777b5602016-12-16 18:06:26 +01002177/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
William Lallemand69e96442016-11-19 00:58:54 +01002178static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
2179{
2180 struct dns_resolvers *presolvers;
2181
2182 if (*args[3]) {
William Lallemand69e96442016-11-19 00:58:54 +01002183 list_for_each_entry(presolvers, &dns_resolvers, list) {
2184 if (strcmp(presolvers->id, args[3]) == 0) {
Willy Tarreau777b5602016-12-16 18:06:26 +01002185 appctx->ctx.cli.p0 = presolvers;
William Lallemand69e96442016-11-19 00:58:54 +01002186 break;
2187 }
2188 }
Willy Tarreau777b5602016-12-16 18:06:26 +01002189 if (appctx->ctx.cli.p0 == NULL) {
William Lallemand69e96442016-11-19 00:58:54 +01002190 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002191 appctx->st0 = CLI_ST_PRINT;
William Lallemand69e96442016-11-19 00:58:54 +01002192 return 1;
2193 }
2194 }
Willy Tarreau3067bfa2016-12-05 14:50:15 +01002195 return 0;
William Lallemand69e96442016-11-19 00:58:54 +01002196}
2197
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002198/*
2199 * if <resolution> is provided, then the function skips the memory allocation part.
2200 * It does the linking only.
2201 *
2202 * if <resolution> is NULL, the function links a dns resolution to a requester:
2203 * - it allocates memory for the struct requester used to link
2204 * the resolution to the requester
2205 * - it configures the resolution if this is the first requester to be linked to it
2206 * - it updates the requester with a pointer to the resolution
2207 *
2208 * Return code:
2209 * - 0 if everything happened smoothly
2210 * - -1 if an error occured. Of course, no resolution is linked to the requester
2211 */
2212int dns_link_resolution(void *requester, int requester_type, struct dns_resolution *resolution)
2213{
2214 struct dns_resolution *tmpresolution = NULL;
2215 struct dns_requester *tmprequester = NULL;
2216 struct dns_resolvers *resolvers = NULL;
2217 char *hostname_dn = NULL;
2218 int new_resolution;
2219
Olivier Houchard8da5f982017-08-04 18:35:36 +02002220
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002221 if (!resolution) {
2222 tmprequester = calloc(1, sizeof(*tmprequester));
2223 if (!tmprequester)
2224 return -1;
2225
2226 switch (requester_type) {
2227 case OBJ_TYPE_SERVER:
2228 tmprequester->requester = &((struct server *)requester)->obj_type;
2229 hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2230 resolvers = objt_server(tmprequester->requester)->resolvers;
2231 switch (objt_server(tmprequester->requester)->dns_opts.family_prio) {
2232 case AF_INET:
2233 tmprequester->prefered_query_type = DNS_RTYPE_A;
2234 break;
2235 default:
2236 tmprequester->prefered_query_type = DNS_RTYPE_AAAA;
2237 }
2238
2239 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002240 case OBJ_TYPE_SRVRQ:
2241 tmprequester->requester = &((struct dns_srvrq *)requester)->obj_type;
2242 hostname_dn = objt_dns_srvrq(requester)->hostname_dn;
2243 resolvers = objt_dns_srvrq(requester)->resolvers;
2244 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002245 case OBJ_TYPE_NONE:
2246 default:
2247 free(tmprequester);
2248 return -1;
2249 }
2250
2251 /* get a resolution from the resolvers' wait queue or pool */
2252 tmpresolution = dns_resolution_list_get(resolvers, hostname_dn, tmprequester->prefered_query_type);
2253 if (!tmpresolution) {
2254 free(tmprequester);
2255 return -1;
2256 }
2257 }
2258 else {
2259 tmpresolution = resolution;
2260
2261 switch (requester_type) {
2262 case OBJ_TYPE_SERVER:
2263 tmprequester = ((struct server *)requester)->dns_requester;
2264 resolvers = ((struct server *)requester)->resolvers;
2265 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002266 case OBJ_TYPE_SRVRQ:
2267 tmprequester = objt_dns_srvrq(requester)->dns_requester;
2268 resolvers = objt_dns_srvrq(requester)->resolvers;
2269 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002270 case OBJ_TYPE_NONE:
2271 default:
2272 return -1;
2273 }
2274 }
2275
2276 /* flag this resolution as NEW if applicable (not already linked to any requester).
2277 * this is required to decide which parameters we have to update on the resolution.
2278 * If new, it means we pulled up the resolution from the resolvers' pool.
2279 */
2280 if (LIST_ISEMPTY(&tmpresolution->requester.wait)) {
2281 new_resolution = 1;
2282 }
2283 else
2284 new_resolution = 0;
2285
2286 /* those parameters are related to the requester type */
2287 switch (obj_type(tmprequester->requester)) {
2288 case OBJ_TYPE_SERVER:
2289 /* some parameters should be set only if the resolution is brand new */
2290 if (new_resolution) {
2291 tmpresolution->query_type = tmprequester->prefered_query_type;
2292 tmpresolution->hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2293 tmpresolution->hostname_dn_len = objt_server(tmprequester->requester)->hostname_dn_len;
2294 }
2295
2296 /* update requester as well, only if we just allocated it */
2297 objt_server(tmprequester->requester)->resolution = tmpresolution;
2298 if (!resolution) {
2299 tmprequester->requester_cb = snr_resolution_cb;
2300 tmprequester->requester_error_cb = snr_resolution_error_cb;
2301 objt_server(tmprequester->requester)->dns_requester = tmprequester;
2302 }
2303 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002304 case OBJ_TYPE_SRVRQ:
2305 /* some parameters should be set only if the resolution is brand new */
2306 if (new_resolution) {
2307 tmpresolution->query_type = DNS_RTYPE_SRV;
2308 tmpresolution->hostname_dn = objt_dns_srvrq(tmprequester->requester)->hostname_dn;
2309 tmpresolution->hostname_dn_len = objt_dns_srvrq(tmprequester->requester)->hostname_dn_len;
2310 }
2311
2312 /* update requester as well, only if we just allocated it */
2313 objt_dns_srvrq(tmprequester->requester)->resolution = tmpresolution;
2314 if (!resolution) {
2315 tmprequester->requester_cb = snr_resolution_cb;
2316 tmprequester->requester_error_cb = snr_resolution_error_cb;
2317 objt_dns_srvrq(tmprequester->requester)->dns_requester = tmprequester;
2318 }
2319 break;
2320
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002321 case OBJ_TYPE_NONE:
2322 default:
2323 free(tmprequester);
2324 return -1;
2325 }
2326
2327 /* update some parameters only if this is a brand new resolution */
2328 if (new_resolution) {
2329 /* move the resolution to the requesters' wait queue */
2330 LIST_DEL(&tmpresolution->list);
2331 LIST_ADDQ(&resolvers->resolution.wait, &tmpresolution->list);
2332
2333 tmpresolution->status = RSLV_STATUS_NONE;
2334 tmpresolution->step = RSLV_STEP_NONE;
2335 tmpresolution->revision = 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002336 LIST_INIT(&tmpresolution->response.answer_list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002337 }
2338
2339 /* add the requester to the resolution's wait queue */
2340 if (resolution)
2341 LIST_DEL(&tmprequester->list);
2342 LIST_ADDQ(&tmpresolution->requester.wait, &tmprequester->list);
2343
2344 return 0;
2345}
2346
2347/*
2348 * pick up an available resolution from the different resolution list associated to a resolvers section,
2349 * in this order:
2350 * 1. check in resolution.curr for the same hostname and query_type
2351 * 2. check in resolution.wait for the same hostname and query_type
2352 * 3. take an available resolution from resolution.pool
2353 *
2354 * return an available resolution, NULL if none found.
2355 */
2356struct dns_resolution *dns_resolution_list_get(struct dns_resolvers *resolvers, char *hostname_dn, int query_type)
2357{
2358 struct dns_resolution *resolution, *tmpresolution;
2359 struct dns_requester *requester;
2360
Olivier Houchard8da5f982017-08-04 18:35:36 +02002361 if (hostname_dn) {
2362 /* search for same hostname and query type in resolution.curr */
2363 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.curr, list) {
2364 requester = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002365
Olivier Houchard8da5f982017-08-04 18:35:36 +02002366 if (!LIST_ISEMPTY(&resolution->requester.wait))
2367 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2368 else if (!LIST_ISEMPTY(&resolution->requester.curr))
2369 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002370
Olivier Houchard8da5f982017-08-04 18:35:36 +02002371 if (!requester)
2372 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002373
Olivier Houchard8da5f982017-08-04 18:35:36 +02002374 if ((query_type == requester->prefered_query_type) &&
2375 (resolution->hostname_dn &&
2376 strcmp(hostname_dn, resolution->hostname_dn) == 0)) {
2377 return resolution;
2378 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002379 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002380
Olivier Houchard8da5f982017-08-04 18:35:36 +02002381 /* search for same hostname and query type in resolution.wait */
2382 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.wait, list) {
2383 requester = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002384
Olivier Houchard8da5f982017-08-04 18:35:36 +02002385 if (!LIST_ISEMPTY(&resolution->requester.wait))
2386 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2387 else if (!LIST_ISEMPTY(&resolution->requester.curr))
2388 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002389
Olivier Houchard8da5f982017-08-04 18:35:36 +02002390 if (!requester)
2391 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002392
Olivier Houchard8da5f982017-08-04 18:35:36 +02002393 if ((query_type == requester->prefered_query_type) &&
2394 (resolution->hostname_dn &&
2395 strcmp(hostname_dn, resolution->hostname_dn) == 0)) {
2396 return resolution;
2397 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002398 }
2399 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002400 /* take the first one (hopefully) from the pool */
2401 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.pool, list) {
2402 if (LIST_ISEMPTY(&resolution->requester.wait)) {
2403 return resolution;
2404 }
2405 }
2406
2407 return NULL;
2408}
2409
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002410/* This function allocates memory for a DNS resolution structure.
2411 * It's up to the caller to set the parameters
2412 * Returns a pointer to the structure resolution or NULL if memory could
2413 * not be allocated.
2414 */
2415struct dns_resolution *dns_alloc_resolution(void)
2416{
2417 struct dns_resolution *resolution = NULL;
Baptiste Assmann729c9012017-05-22 15:13:10 +02002418 char *buffer = NULL;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002419
2420 resolution = calloc(1, sizeof(*resolution));
Baptiste Assmann729c9012017-05-22 15:13:10 +02002421 buffer = calloc(1, global.tune.bufsize);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002422
Baptiste Assmann729c9012017-05-22 15:13:10 +02002423 if (!resolution || !buffer) {
2424 free(buffer);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002425 free(resolution);
2426 return NULL;
2427 }
2428
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002429 LIST_INIT(&resolution->requester.wait);
2430 LIST_INIT(&resolution->requester.curr);
Baptiste Assmann729c9012017-05-22 15:13:10 +02002431
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002432 return resolution;
2433}
2434
2435/* This function free the memory allocated to a DNS resolution */
2436void dns_free_resolution(struct dns_resolution *resolution)
2437{
2438 free(resolution);
2439
2440 return;
2441}
2442
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002443/* this function free a resolution from its requester(s) and move it back to the pool */
2444void dns_resolution_free(struct dns_resolvers *resolvers, struct dns_resolution *resolution)
2445{
2446 struct dns_requester *requester, *tmprequester;
2447
2448 /* clean up configuration */
2449 dns_reset_resolution(resolution);
2450 resolution->hostname_dn = NULL;
2451 resolution->hostname_dn_len = 0;
2452
2453 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.wait, list) {
2454 LIST_DEL(&requester->list);
2455 }
2456 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
2457 LIST_DEL(&requester->list);
2458 }
2459
2460 LIST_DEL(&resolution->list);
2461 LIST_ADDQ(&resolvers->resolution.pool, &resolution->list);
2462
2463 return;
2464}
2465
2466/*
2467 * this function remove a requester from a resolution
2468 * and takes care of all the consequences.
2469 * It also cleans up some parameters from the requester
2470 */
2471void dns_rm_requester_from_resolution(struct dns_requester *requester, struct dns_resolution *resolution)
2472{
2473 char *hostname_dn;
2474 struct dns_requester *tmprequester;
2475
2476 /* resolution is still used by other requesters, we need to move
2477 * some pointers to an other requester if needed
2478 */
2479 switch (obj_type(requester->requester)) {
2480 case OBJ_TYPE_SERVER:
2481 hostname_dn = objt_server(requester->requester)->hostname_dn;
2482 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002483 case OBJ_TYPE_SRVRQ:
2484 hostname_dn = objt_dns_srvrq(requester->requester)->hostname_dn;
2485 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002486 case OBJ_TYPE_NONE:
2487 default:
2488 hostname_dn = NULL;
2489 break;
2490 }
2491
2492 if (resolution->hostname_dn != hostname_dn)
2493 return;
2494
2495 /* First, we need to find this other requester */
2496 tmprequester = NULL;
2497 list_for_each_entry(tmprequester, &resolution->requester.wait, list) {
2498 if (tmprequester != requester)
2499 break;
2500 }
2501 if (!tmprequester) {
2502 /* if we can't find it in wait queue, let's get one in run queue */
2503 list_for_each_entry(tmprequester, &resolution->requester.curr, list) {
2504 if (tmprequester != requester)
2505 break;
2506 }
2507 }
2508
2509 /* move hostname_dn related pointers to the next requester */
2510 switch (obj_type(tmprequester->requester)) {
2511 case OBJ_TYPE_SERVER:
2512 resolution->hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2513 resolution->hostname_dn_len = objt_server(tmprequester->requester)->hostname_dn_len;
2514 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002515 case OBJ_TYPE_SRVRQ:
2516 resolution->hostname_dn = objt_dns_srvrq(tmprequester->requester)->hostname_dn;
2517 resolution->hostname_dn_len = objt_dns_srvrq(tmprequester->requester)->hostname_dn_len;
2518 break;
2519
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002520 case OBJ_TYPE_NONE:
2521 default:
2522 ;;
2523 }
2524
2525
2526 /* clean up the requester */
2527 LIST_DEL(&requester->list);
2528 switch (obj_type(requester->requester)) {
2529 case OBJ_TYPE_SERVER:
2530 objt_server(requester->requester)->resolution = NULL;
2531 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002532 case OBJ_TYPE_SRVRQ:
2533 objt_dns_srvrq(requester->requester)->resolution = NULL;
2534 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002535 case OBJ_TYPE_NONE:
2536 default:
2537 ;;
2538 }
2539}
2540
Willy Tarreau777b5602016-12-16 18:06:26 +01002541/* This function dumps counters from all resolvers section and associated name
2542 * servers. It returns 0 if the output buffer is full and it needs to be called
2543 * again, otherwise non-zero. It may limit itself to the resolver pointed to by
2544 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002545 */
2546static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2547{
2548 struct stream_interface *si = appctx->owner;
2549 struct dns_resolvers *presolvers;
2550 struct dns_nameserver *pnameserver;
2551
2552 chunk_reset(&trash);
2553
2554 switch (appctx->st2) {
2555 case STAT_ST_INIT:
2556 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2557 /* fall through */
2558
2559 case STAT_ST_LIST:
2560 if (LIST_ISEMPTY(&dns_resolvers)) {
2561 chunk_appendf(&trash, "No resolvers found\n");
2562 }
2563 else {
2564 list_for_each_entry(presolvers, &dns_resolvers, list) {
Willy Tarreau777b5602016-12-16 18:06:26 +01002565 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != presolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002566 continue;
2567
2568 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
2569 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
2570 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
2571 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
2572 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
2573 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
2574 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
2575 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
2576 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
2577 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
2578 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
2579 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
2580 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
2581 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
2582 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
2583 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
2584 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
2585 }
2586 }
2587 }
2588
2589 /* display response */
2590 if (bi_putchk(si_ic(si), &trash) == -1) {
2591 /* let's try again later from this session. We add ourselves into
2592 * this session's users so that it can remove us upon termination.
2593 */
2594 si->flags |= SI_FL_WAIT_ROOM;
2595 return 0;
2596 }
2597
2598 appctx->st2 = STAT_ST_FIN;
2599 /* fall through */
2600
2601 default:
2602 appctx->st2 = STAT_ST_FIN;
2603 return 1;
2604 }
2605}
2606
2607/* register cli keywords */
2608static struct cli_kw_list cli_kws = {{ },{
2609 { { "show", "stat", "resolvers", NULL }, "show stat resolvers [id]: dumps counters from all resolvers section and\n"
2610 " associated name servers",
2611 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2612 {{},}
2613}};
2614
2615
2616__attribute__((constructor))
2617static void __dns_init(void)
2618{
2619 cli_register_kw(&cli_kws);
2620}
2621