blob: b87e1019f9f4de27ceb3441228f25cb3cd76ae8b [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) {
Baptiste Assmanna644aa82017-08-12 11:16:55 +0200108 switch (obj_type(curr_requester->requester)) {
109 case OBJ_TYPE_SERVER:
110 printf(" %p SRV %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
111 break;
112 case OBJ_TYPE_SRVRQ:
113 printf(" %p SRVRQ %s %d\n", curr_requester, objt_dns_srvrq(curr_requester->requester)->name, curr_requester->prefered_query_type);
114 break;
115 case OBJ_TYPE_NONE:
116 default:
117 ;;
118 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200119 }
120 printf(" requester.curr list:\n");
121 list_for_each_entry(curr_requester, &curr_resolution->requester.curr, list) {
Baptiste Assmanna644aa82017-08-12 11:16:55 +0200122 switch (obj_type(curr_requester->requester)) {
123 case OBJ_TYPE_SERVER:
124 printf(" %p SRV %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
125 break;
126 case OBJ_TYPE_SRVRQ:
127 printf(" %p SRVRQ %s %d\n", curr_requester, objt_dns_srvrq(curr_requester->requester)->name, curr_requester->prefered_query_type);
128 break;
129 case OBJ_TYPE_NONE:
130 default:
131 ;;
132 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200133 }
134 }
135 printf(" resolution.curr list:\n");
136 list_for_each_entry(curr_resolution, &curr_resolvers->resolution.curr, list) {
137 printf(" %p %s\n", curr_resolution, curr_resolution->hostname_dn);
138 printf(" requester.wait list:\n");
139 list_for_each_entry(curr_requester, &curr_resolution->requester.wait, list) {
Baptiste Assmanna644aa82017-08-12 11:16:55 +0200140 switch (obj_type(curr_requester->requester)) {
141 case OBJ_TYPE_SERVER:
142 printf(" %p SRV %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
143 break;
144 case OBJ_TYPE_SRVRQ:
145 printf(" %p SRVRQ %s %d\n", curr_requester, objt_dns_srvrq(curr_requester->requester)->name, curr_requester->prefered_query_type);
146 break;
147 case OBJ_TYPE_NONE:
148 default:
149 ;;
150 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200151 }
152 printf(" requester.curr list:\n");
153 list_for_each_entry(curr_requester, &curr_resolution->requester.curr, list) {
Baptiste Assmanna644aa82017-08-12 11:16:55 +0200154 switch (obj_type(curr_requester->requester)) {
155 case OBJ_TYPE_SERVER:
156 printf(" %p SRV %s %d\n", curr_requester, objt_server(curr_requester->requester)->id, curr_requester->prefered_query_type);
157 break;
158 case OBJ_TYPE_SRVRQ:
159 printf(" %p SRVRQ %s %d\n", curr_requester, objt_dns_srvrq(curr_requester->requester)->name, curr_requester->prefered_query_type);
160 break;
161 case OBJ_TYPE_NONE:
162 default:
163 ;;
164 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200165 }
166 }
167 }
168
169 printf("===============\n");
170}
171
172/*
173 * Initiates a new name resolution:
174 * - generates a query id
175 * - configure the resolution structure
176 * - startup the resolvers task if required
177 *
178 * returns:
179 * - 0 if everything started properly
180 * - -1 in case of error or if resolution already running
181 */
182int dns_trigger_resolution(struct dns_resolution *resolution)
183{
184 struct dns_requester *requester = NULL, *tmprequester;
185 struct dns_resolvers *resolvers = NULL;
186 int inter;
187
188 /* process the element of the wait queue */
189 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.wait, list) {
190 inter = 0;
191
192 switch (obj_type(requester->requester)) {
193 case OBJ_TYPE_SERVER:
194 inter = objt_server(requester->requester)->check.inter;
195 resolvers = objt_server(requester->requester)->resolvers;
196 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200197 case OBJ_TYPE_SRVRQ:
198 inter = objt_dns_srvrq(requester->requester)->inter;
199 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
200 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200201 case OBJ_TYPE_NONE:
202 default:
203 return -1;
204 }
205
206 /* if data is fresh enough, let's use it */
207 if (!tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms)) {
208 /* we only use cache if the response there is valid.
209 * If not valid, we run the resolution and move the requester to
210 * the run queue. */
211 if (resolution->status != RSLV_STATUS_VALID) {
212 LIST_DEL(&requester->list);
213 LIST_ADDQ(&resolution->requester.curr, &requester->list);
214 dns_run_resolution(requester);
215 continue;
216 }
217
218 requester->requester_cb(requester, NULL);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200219 resolvers = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200220 }
221 else {
222 LIST_DEL(&requester->list);
223 LIST_ADDQ(&resolution->requester.curr, &requester->list);
224 dns_run_resolution(requester);
225 }
226 }
227
228 if (resolvers)
229 dns_update_resolvers_timeout(resolvers);
230
231 return 0;
232}
233
Baptiste Assmann325137d2015-04-13 23:40:55 +0200234/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200235 * Prepare and send a DNS resolution.
236 *
237 * Return code:
238 * - 0 if no error occured
239 * - -1 in case of error
240 */
241static int
242dns_run_resolution(struct dns_requester *requester)
243{
244 struct dns_resolution *resolution;
245 struct dns_resolvers *resolvers;
246 int query_id, query_type, i;
247 struct proxy *proxy;
248
249 resolution = NULL;
250 resolvers = NULL;
251 proxy = NULL;
252 query_type = -1;
253 switch (obj_type(requester->requester)) {
254 case OBJ_TYPE_SERVER:
255 resolution = objt_server(requester->requester)->resolution;
256 resolvers = objt_server(requester->requester)->resolvers;
257 proxy = objt_server(requester->requester)->proxy;
258 query_type = requester->prefered_query_type;
259 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200260 case OBJ_TYPE_SRVRQ:
261 resolution = objt_dns_srvrq(requester->requester)->resolution;
262 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
263 proxy = objt_dns_srvrq(requester->requester)->proxy;
264 query_type = DNS_RTYPE_SRV;
265 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200266 case OBJ_TYPE_NONE:
267 default:
268 return -1;
269 }
270
271 /*
Olivier Houchard8da5f982017-08-04 18:35:36 +0200272 * Avoid sending requests for resolutions that don't yet have
273 * an hostname, ie resolutions linked to servers that do not yet
274 * have an fqdn
275 */
276 if (!resolution->hostname_dn)
277 return 0;
278
279 /*
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200280 * check if a resolution has already been started for this server
281 * return directly to avoid resolution pill up.
282 */
283 if (resolution->step != RSLV_STEP_NONE)
284 return 0;
285
286 /* generates a query id */
287 i = 0;
288 do {
289 query_id = dns_rnd16();
290 /* we do try only 100 times to find a free query id */
291 if (i++ > 100) {
292 chunk_printf(&trash, "could not generate a query id for %s, in resolvers %s",
293 resolution->hostname_dn, resolvers->id);
294
295 if (proxy)
296 send_log(proxy, LOG_NOTICE, "%s.\n", trash.str);
297 return -1;
298 }
299 } while (eb32_lookup(&resolvers->query_ids, query_id));
300
301 /* move the resolution into the run queue */
302 LIST_DEL(&resolution->list);
303 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
304
305 /* now update resolution parameters */
306 resolution->query_id = query_id;
307 resolution->qid.key = query_id;
308 resolution->step = RSLV_STEP_RUNNING;
309 resolution->query_type = query_type;
310 resolution->try = resolvers->resolve_retries;
311 resolution->try_cname = 0;
312 resolution->nb_responses = 0;
313 eb32_insert(&resolvers->query_ids, &resolution->qid);
314
315 dns_send_query(resolution);
316 resolution->try -= 1;
317
318 /* update wakeup date if this resolution is the only one in the FIFO list */
319 if (dns_check_resolution_queue(resolvers) == 1) {
320 /* update task timeout */
321 dns_update_resolvers_timeout(resolvers);
322 task_queue(resolvers->t);
323 }
324
325 return 0;
326}
327
328/*
Baptiste Assmann325137d2015-04-13 23:40:55 +0200329 * check if there is more than 1 resolution in the resolver's resolution list
330 * return value:
331 * 0: empty list
332 * 1: exactly one entry in the list
333 * 2: more than one entry in the list
334 */
335int dns_check_resolution_queue(struct dns_resolvers *resolvers)
336{
337
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200338 if (LIST_ISEMPTY(&resolvers->resolution.curr))
Baptiste Assmann325137d2015-04-13 23:40:55 +0200339 return 0;
340
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200341 if ((resolvers->resolution.curr.n) && (resolvers->resolution.curr.n == resolvers->resolution.curr.p))
Baptiste Assmann325137d2015-04-13 23:40:55 +0200342 return 1;
343
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200344 if (! ((resolvers->resolution.curr.n == resolvers->resolution.curr.p)
345 && (&resolvers->resolution.curr != resolvers->resolution.curr.n)))
Baptiste Assmann325137d2015-04-13 23:40:55 +0200346 return 2;
347
348 return 0;
349}
350
351/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200352 * reset some resolution parameters to initial values and also delete the
353 * query ID from the resolver's tree.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200354 */
355void dns_reset_resolution(struct dns_resolution *resolution)
356{
357 /* update resolution status */
358 resolution->step = RSLV_STEP_NONE;
359
360 resolution->try = 0;
361 resolution->try_cname = 0;
362 resolution->last_resolution = now_ms;
363 resolution->nb_responses = 0;
364
365 /* clean up query id */
366 eb32_delete(&resolution->qid);
367 resolution->query_id = 0;
368 resolution->qid.key = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200369}
370
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200371static inline void free_dns_answer_item(struct dns_answer_item *item)
372{
373 pool_free2(dns_answer_item_pool, item);
374}
375
376
Baptiste Assmann325137d2015-04-13 23:40:55 +0200377/*
378 * function called when a network IO is generated on a name server socket for an incoming packet
379 * It performs the following actions:
380 * - check if the packet requires processing (not outdated resolution)
381 * - ensure the DNS packet received is valid and call requester's callback
382 * - call requester's error callback if invalid response
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200383 * - check the dn_name in the packet against the one sent
Baptiste Assmann325137d2015-04-13 23:40:55 +0200384 */
385void dns_resolve_recv(struct dgram_conn *dgram)
386{
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200387 struct dns_nameserver *nameserver, *tmpnameserver;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200388 struct dns_resolvers *resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200389 struct dns_resolution *resolution = NULL;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200390 struct dns_query_item *query;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200391 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
392 unsigned char *bufend;
William Lallemandcc9b94a2017-06-08 19:30:39 +0200393 int fd, buflen, dns_resp, need_resend = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200394 unsigned short query_id;
395 struct eb32_node *eb;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200396 struct lru64 *lru = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200397 struct dns_requester *requester = NULL, *tmprequester = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200398 struct dns_answer_item *item1, *item2 = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200399
400 fd = dgram->t.sock.fd;
401
402 /* check if ready for reading */
403 if (!fd_recv_ready(fd))
404 return;
405
406 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200407 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200408 return;
409
410 resolvers = nameserver->resolvers;
411
412 /* process all pending input messages */
413 while (1) {
Olivier Houchard8da5f982017-08-04 18:35:36 +0200414 int removed_reso = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200415 /* read message received */
416 memset(buf, '\0', DNS_MAX_UDP_MESSAGE + 1);
417 if ((buflen = recv(fd, (char*)buf , DNS_MAX_UDP_MESSAGE, 0)) < 0) {
418 /* FIXME : for now we consider EAGAIN only */
419 fd_cant_recv(fd);
420 break;
421 }
422
423 /* message too big */
424 if (buflen > DNS_MAX_UDP_MESSAGE) {
425 nameserver->counters.too_big += 1;
426 continue;
427 }
428
429 /* initializing variables */
430 bufend = buf + buflen; /* pointer to mark the end of the buffer */
431
432 /* read the query id from the packet (16 bits) */
433 if (buf + 2 > bufend) {
434 nameserver->counters.invalid += 1;
435 continue;
436 }
437 query_id = dns_response_get_query_id(buf);
438
439 /* search the query_id in the pending resolution tree */
Baptiste Assmann01daef32015-09-02 22:05:24 +0200440 eb = eb32_lookup(&resolvers->query_ids, query_id);
441 if (eb == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200442 /* unknown query id means an outdated response and can be safely ignored */
443 nameserver->counters.outdated += 1;
444 continue;
445 }
446
447 /* known query id means a resolution in prgress */
448 resolution = eb32_entry(eb, struct dns_resolution, qid);
449
450 if (!resolution) {
451 nameserver->counters.outdated += 1;
452 continue;
453 }
454
455 /* number of responses received */
456 resolution->nb_responses += 1;
457
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200458 dns_resp = dns_validate_dns_response(buf, bufend, resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200459
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200460 switch (dns_resp) {
461 case DNS_RESP_VALID:
462 need_resend = 0;
463 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200464
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200465 case DNS_RESP_INVALID:
466 case DNS_RESP_QUERY_COUNT_ERROR:
467 case DNS_RESP_WRONG_NAME:
468 if (resolution->status != RSLV_STATUS_INVALID) {
469 resolution->status = RSLV_STATUS_INVALID;
470 resolution->last_status_change = now_ms;
471 }
472 nameserver->counters.invalid += 1;
473 need_resend = 0;
474 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200475
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200476 case DNS_RESP_ANCOUNT_ZERO:
477 if (resolution->status != RSLV_STATUS_OTHER) {
478 resolution->status = RSLV_STATUS_OTHER;
479 resolution->last_status_change = now_ms;
480 }
481 nameserver->counters.any_err += 1;
482 need_resend = 1;
483 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200484
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200485 case DNS_RESP_NX_DOMAIN:
486 if (resolution->status != RSLV_STATUS_NX) {
487 resolution->status = RSLV_STATUS_NX;
488 resolution->last_status_change = now_ms;
489 }
490 nameserver->counters.nx += 1;
491 need_resend = 0;
492 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200493
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200494 case DNS_RESP_REFUSED:
495 if (resolution->status != RSLV_STATUS_REFUSED) {
496 resolution->status = RSLV_STATUS_REFUSED;
497 resolution->last_status_change = now_ms;
498 }
499 nameserver->counters.refused += 1;
500 need_resend = 0;
501 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200502
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200503 case DNS_RESP_CNAME_ERROR:
504 if (resolution->status != RSLV_STATUS_OTHER) {
505 resolution->status = RSLV_STATUS_OTHER;
506 resolution->last_status_change = now_ms;
507 }
508 nameserver->counters.cname_error += 1;
509 need_resend = 1;
510 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200511
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200512 case DNS_RESP_TRUNCATED:
513 if (resolution->status != RSLV_STATUS_OTHER) {
514 resolution->status = RSLV_STATUS_OTHER;
515 resolution->last_status_change = now_ms;
516 }
517 nameserver->counters.truncated += 1;
518 need_resend = 1;
519 break;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200520
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200521 case DNS_RESP_NO_EXPECTED_RECORD:
522 if (resolution->status != RSLV_STATUS_OTHER) {
523 resolution->status = RSLV_STATUS_OTHER;
524 resolution->last_status_change = now_ms;
525 }
526 nameserver->counters.other += 1;
527 need_resend = 1;
528 break;
529
530 case DNS_RESP_ERROR:
531 case DNS_RESP_INTERNAL:
532 if (resolution->status != RSLV_STATUS_OTHER) {
533 resolution->status = RSLV_STATUS_OTHER;
534 resolution->last_status_change = now_ms;
535 }
536 nameserver->counters.other += 1;
537 need_resend = 1;
538 break;
539 }
540
Olivier Houchard8da5f982017-08-04 18:35:36 +0200541 /* Check for any obsolete record, also identify any SRV request, and try to find a corresponding server */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200542 list_for_each_entry_safe(item1, item2, &resolution->response.answer_list,
543 list) {
544 if (item1->last_seen + nameserver->resolvers->hold.obsolete / 1000 < now.tv_sec) {
545 LIST_DEL(&item1->list);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200546 if (item1->type == DNS_RTYPE_SRV && !LIST_ISEMPTY(&resolution->requester.curr)) {
547 struct dns_srvrq *srvrq;
548
549 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
550
551 srvrq = objt_dns_srvrq(requester->requester);
552 /* We're removing an obsolete entry, remove any associated server */
553 if (srvrq) {
554 struct server *srv;
555
556 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
557 if (srv->srvrq == srvrq &&
558 item1->data_len ==
559 srv->hostname_dn_len &&
560 !memcmp(srv->hostname_dn, item1->target, item1->data_len) &&
561 srv->svc_port == item1->port) {
562 snr_update_srv_status(srv, 1);
563 free(srv->hostname);
564 srv->hostname = NULL;
565 srv->hostname_dn_len = 0;
566 free(srv->hostname_dn);
567 srv->hostname_dn = NULL;
568 dns_resolution_free(srv->resolvers, srv->resolution);
569 srv->resolution = dns_resolution_list_get(srv->resolvers, NULL, srv->dns_requester->prefered_query_type);
570 if (resolution == srv->resolution)
571 removed_reso = 1;
572 }
573 }
574 }
575 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200576 free_dns_answer_item(item1);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200577 continue;
578 }
579 if (item1->type == DNS_RTYPE_SRV) {
580 struct server *srv;
581 struct dns_srvrq *srvrq;
582
583 if (LIST_ISEMPTY(&resolution->requester.curr))
584 continue;
585
586 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
587 srvrq = objt_dns_srvrq(requester->requester);
588 if (!srvrq)
589 continue;
590 /* Check if a server already uses that hostname */
591 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
592 if (srv->srvrq == srvrq &&
593 item1->data_len == srv->hostname_dn_len &&
594 !memcmp(srv->hostname_dn, item1->target, item1->data_len) &&
595 srv->svc_port == item1->port) {
596 if (srv->uweight != item1->weight) {
597 char weight[9];
598
599 snprintf(weight, sizeof(weight),
600 "%d", item1->weight);
601 server_parse_weight_change_request(srv, weight);
602
603 }
604
605 break;
606 }
607 }
608 /* If not, try to find a server that is down */
609 if (!srv) {
610 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
611
612 if (srv->srvrq == srvrq &&
613 !srv->hostname_dn)
614 break;
615 }
616 if (srv) {
617 char weight[9];
618
619 char hostname[DNS_MAX_NAME_SIZE];
620
621 if (item1->data_len > DNS_MAX_NAME_SIZE)
622 continue;
623 dns_dn_label_to_str(item1->target, hostname, item1->data_len);
624 update_server_fqdn(srv, hostname, "SRV record");
625 srv->svc_port = item1->port;
626 srv->flags &= ~SRV_F_MAPPORTS;
627 if ((srv->check.state & CHK_ST_CONFIGURED) && !(srv->flags & SRV_F_CHECKPORT))
628 srv->check.port = item1->port;
629 snprintf(weight, sizeof(weight),
630 "%d", item1->weight);
631 server_parse_weight_change_request(srv, weight);
632 }
633
634 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200635 }
636 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200637 if (removed_reso)
638 goto next_packet;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200639
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200640 /* some error codes trigger a re-send of the query, but switching the
641 * query type.
642 * This is the case for the following error codes:
643 * DNS_RESP_ANCOUNT_ZERO
644 * DNS_RESP_TRUNCATED
645 * DNS_RESP_ERROR
646 * DNS_RESP_INTERNAL
647 * DNS_RESP_NO_EXPECTED_RECORD
648 * DNS_RESP_CNAME_ERROR
649 */
650 if (need_resend) {
651 int family_prio;
652 int res_preferred_afinet, res_preferred_afinet6;
653
654 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
655 switch (obj_type(requester->requester)) {
656 case OBJ_TYPE_SERVER:
657 family_prio = objt_server(requester->requester)->dns_opts.family_prio;
658 break;
659 case OBJ_TYPE_NONE:
660 default:
661 family_prio = AF_INET6;
662 }
663 res_preferred_afinet = family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
664 res_preferred_afinet6 = family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
665 if ((res_preferred_afinet || res_preferred_afinet6)
666 || (resolution->try > 0)) {
667 /* let's change the query type */
668 if (res_preferred_afinet6) {
669 /* fallback from AAAA to A */
670 resolution->query_type = DNS_RTYPE_A;
671 }
672 else if (res_preferred_afinet) {
673 /* fallback from A to AAAA */
674 resolution->query_type = DNS_RTYPE_AAAA;
675 }
676 else {
677 resolution->try -= 1;
678 if (family_prio == AF_INET) {
679 resolution->query_type = DNS_RTYPE_A;
680 } else {
681 resolution->query_type = DNS_RTYPE_AAAA;
682 }
683 }
684
685 dns_send_query(resolution);
686 /*
687 * move the resolution to the last element of the FIFO queue
688 * and update timeout wakeup based on the new first entry
689 */
690 if (dns_check_resolution_queue(resolvers) > 1) {
691 /* second resolution becomes first one */
692 LIST_DEL(&resolution->list);
693 /* ex first resolution goes to the end of the queue */
694 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
695 }
696
697 dns_update_resolvers_timeout(resolvers);
698 goto next_packet;
699 }
700
701 /* if we're there, this means that we already ran out of chances to re-send
702 * the query */
703 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
704 requester->requester_error_cb(requester, dns_resp);
705 }
706 goto next_packet;
707 }
708
709 /* now processing those error codes only:
710 * DNS_RESP_NX_DOMAIN
711 * DNS_RESP_REFUSED
712 */
713 if (dns_resp != DNS_RESP_VALID) {
714 /* 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);
717
718 /* we can move the requester the wait queue */
719 LIST_DEL(&requester->list);
720 LIST_ADDQ(&resolution->requester.wait, &requester->list);
721 }
722 goto next_packet;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200723 }
724
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200725 /* Now let's check the query's dname corresponds to the one we sent.
726 * We can check only the first query of the list. We send one query at a time
727 * so we get one query in the response */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200728 query = LIST_NEXT(&resolution->response.query_list, struct dns_query_item *, list);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200729 if (!resolution->hostname_dn)
730 abort();
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200731 if (query && memcmp(query->name, resolution->hostname_dn, resolution->hostname_dn_len) != 0) {
732 nameserver->counters.other += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200733 /* now parse list of requesters currently waiting for this resolution */
734 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
735 requester->requester_error_cb(requester, DNS_RESP_WRONG_NAME);
736 /* we can move the requester the wait queue */
737 LIST_DEL(&requester->list);
738 LIST_ADDQ(&resolution->requester.wait, &requester->list);
739 }
740 goto next_packet;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200741 }
742
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200743 /* no errors, we can save the response in the cache */
744 if (dns_lru_tree) {
745 unsigned long long seed = 1;
746 struct chunk *buf = get_trash_chunk();
747 struct chunk *tmp = NULL;
748
749 chunk_reset(buf);
750 tmp = dns_cache_key(resolution->query_type, resolution->hostname_dn,
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200751 resolution->hostname_dn_len, buf);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200752 if (!tmp) {
753 nameserver->counters.other += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200754 /* now parse list of requesters currently waiting for this resolution */
755 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
756 requester->requester_error_cb(requester, DNS_RESP_ERROR);
757 /* we can move the requester the wait queue */
758 LIST_DEL(&requester->list);
759 LIST_ADDQ(&resolution->requester.wait, &requester->list);
760 }
761 goto next_packet;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200762 }
763
764 lru = lru64_get(XXH64(buf->str, buf->len, seed),
765 dns_lru_tree, nameserver->resolvers, 1);
766
767 lru64_commit(lru, resolution, nameserver->resolvers, 1, NULL);
768 }
769
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200770 if (resolution->status != RSLV_STATUS_VALID) {
771 resolution->status = RSLV_STATUS_VALID;
772 resolution->last_status_change = now_ms;
773 }
774
Baptiste Assmann37bb3722015-08-07 10:18:32 +0200775 nameserver->counters.valid += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200776 /* now parse list of requesters currently waiting for this resolution */
777 tmpnameserver = nameserver;
778 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
779 requester->requester_cb(requester, tmpnameserver);
780 /* we can move the requester the wait queue */
781 LIST_DEL(&requester->list);
782 LIST_ADDQ(&resolution->requester.wait, &requester->list);
783 /* first response is managed by the server, others are from the cache */
784 tmpnameserver = NULL;
785 }
786
787 next_packet:
788 /* resolution may be NULL when we receive an ICMP unreachable packet */
789 if (resolution && LIST_ISEMPTY(&resolution->requester.curr)) {
790 /* move the resolution into the wait queue */
791 LIST_DEL(&resolution->list);
792 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
793 /* update last resolution date and time */
794 resolution->last_resolution = now_ms;
795 /* reset current status flag */
796 resolution->step = RSLV_STEP_NONE;
797 /* reset values */
798 dns_reset_resolution(resolution);
799 }
800
801 } // end of while "packets" loop
802
803 dns_update_resolvers_timeout(nameserver->resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200804}
805
806/*
807 * function called when a resolvers network socket is ready to send data
808 * It performs the following actions:
809 */
810void dns_resolve_send(struct dgram_conn *dgram)
811{
812 int fd;
813 struct dns_nameserver *nameserver;
814 struct dns_resolvers *resolvers;
815 struct dns_resolution *resolution;
816
817 fd = dgram->t.sock.fd;
818
819 /* check if ready for sending */
820 if (!fd_send_ready(fd))
821 return;
822
823 /* we don't want/need to be waked up any more for sending */
824 fd_stop_send(fd);
825
826 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200827 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200828 return;
829
830 resolvers = nameserver->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200831 resolution = LIST_NEXT(&resolvers->resolution.curr, struct dns_resolution *, list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200832
833 dns_send_query(resolution);
834 dns_update_resolvers_timeout(resolvers);
835}
836
837/*
838 * forge and send a DNS query to resolvers associated to a resolution
839 * It performs the following actions:
840 * returns:
841 * 0 in case of error or safe ignorance
842 * 1 if no error
843 */
844int dns_send_query(struct dns_resolution *resolution)
845{
Baptiste Assmann42746372017-05-03 12:12:02 +0200846 struct dns_resolvers *resolvers = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200847 struct dns_nameserver *nameserver;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200848 struct dns_requester *requester = NULL;
Erwan Velu5457eb42015-10-15 15:07:26 +0200849 int ret, bufsize, fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200850
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200851 /* nothing to do */
852 if (LIST_ISEMPTY(&resolution->requester.curr))
853 return 0;
854
855 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
856
857 switch (obj_type(requester->requester)) {
858 case OBJ_TYPE_SERVER:
859 resolvers = objt_server(requester->requester)->resolvers;
860 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200861 case OBJ_TYPE_SRVRQ:
862 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
863 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200864 case OBJ_TYPE_NONE:
865 default:
866 return 0;
867 }
Baptiste Assmann42746372017-05-03 12:12:02 +0200868
869 if (!resolvers)
870 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200871
Baptiste Assmann325137d2015-04-13 23:40:55 +0200872 bufsize = dns_build_query(resolution->query_id, resolution->query_type, resolution->hostname_dn,
873 resolution->hostname_dn_len, trash.str, trash.size);
874
875 if (bufsize == -1)
876 return 0;
877
878 list_for_each_entry(nameserver, &resolvers->nameserver_list, list) {
879 fd = nameserver->dgram->t.sock.fd;
880 errno = 0;
881
882 ret = send(fd, trash.str, bufsize, 0);
883
884 if (ret > 0)
885 nameserver->counters.sent += 1;
886
887 if (ret == 0 || errno == EAGAIN) {
888 /* nothing written, let's update the poller that we wanted to send
889 * but we were not able to */
890 fd_want_send(fd);
891 fd_cant_send(fd);
892 }
893 }
894
895 /* update resolution */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200896 resolution->nb_responses = 0;
897 resolution->last_sent_packet = now_ms;
898
899 return 1;
900}
901
902/*
903 * update a resolvers' task timeout for next wake up
904 */
905void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
906{
907 struct dns_resolution *resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200908 struct dns_requester *requester;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200909
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200910 if ((LIST_ISEMPTY(&resolvers->resolution.curr)) && (LIST_ISEMPTY(&resolvers->resolution.wait))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200911 resolvers->t->expire = TICK_ETERNITY;
912 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200913 else if (!LIST_ISEMPTY(&resolvers->resolution.curr)) {
914 resolution = LIST_NEXT(&resolvers->resolution.curr, struct dns_resolution *, list);
915 if (!resolvers->t->expire || tick_is_le(resolvers->t->expire, tick_add(resolution->last_sent_packet, resolvers->timeout.retry))) {
916 resolvers->t->expire = tick_add(resolution->last_sent_packet, resolvers->timeout.retry);
917 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200918 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200919 else if (!LIST_ISEMPTY(&resolvers->resolution.wait)) {
920 int valid_period, inter, need_wakeup;
921 struct dns_resolution *res_back;
922 need_wakeup = 0;
923 list_for_each_entry_safe(resolution, res_back, &resolvers->resolution.wait, list) {
924 valid_period = 0;
925 inter = 0;
926
927 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
928
929 switch (obj_type(requester->requester)) {
930 case OBJ_TYPE_SERVER:
931 valid_period = objt_server(requester->requester)->check.inter;
932 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200933 case OBJ_TYPE_SRVRQ:
934 valid_period = objt_dns_srvrq(requester->requester)->inter;
935 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200936 case OBJ_TYPE_NONE:
937 default:
938 continue;
939 }
940
941 if (resolvers->hold.valid < valid_period)
942 inter = resolvers->hold.valid;
943 else
944 inter = valid_period;
945
946 if (tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms)) {
947 switch (obj_type(requester->requester)) {
948 case OBJ_TYPE_SERVER:
949 dns_trigger_resolution(objt_server(requester->requester)->resolution);
950 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200951 case OBJ_TYPE_SRVRQ:
952 dns_trigger_resolution(objt_dns_srvrq(requester->requester)->resolution);
953 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200954 case OBJ_TYPE_NONE:
955 default:
956 ;;
957 }
958 }
959 else {
960 need_wakeup = 1;
961 }
962 }
963 /* in such case, we wake up in 1s */
964 if (need_wakeup) {
965 int r = 1000;
966
967 resolution = LIST_NEXT(&resolvers->resolution.wait, struct dns_resolution *, list);
968 if (tick_is_le(resolvers->t->expire, tick_add(now_ms, r)))
969 resolvers->t->expire = tick_add(now_ms, r);
970 resolvers->t->expire = tick_add(now_ms, 1000);
971 }
972 }
973
974 task_queue(resolvers->t);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200975}
976
977/*
978 * Analyse, re-build and copy the name <name> from the DNS response packet <buffer>.
979 * <name> must point to the 'data_len' information or pointer 'c0' for compressed data.
980 * The result is copied into <dest>, ensuring we don't overflow using <dest_len>
981 * Returns the number of bytes the caller can move forward. If 0 it means an error occured
982 * while parsing the name.
983 * <offset> is the number of bytes the caller could move forward.
984 */
985int dns_read_name(unsigned char *buffer, unsigned char *bufend, unsigned char *name, char *destination, int dest_len, int *offset)
986{
987 int nb_bytes = 0, n = 0;
988 int label_len;
989 unsigned char *reader = name;
990 char *dest = destination;
991
992 while (1) {
993 /* name compression is in use */
994 if ((*reader & 0xc0) == 0xc0) {
995 /* a pointer must point BEFORE current position */
996 if ((buffer + reader[1]) > reader) {
997 goto out_error;
998 }
999
1000 n = dns_read_name(buffer, bufend, buffer + reader[1], dest, dest_len - nb_bytes, offset);
1001 if (n == 0)
1002 goto out_error;
1003
1004 dest += n;
1005 nb_bytes += n;
1006 goto out;
1007 }
1008
1009 label_len = *reader;
1010 if (label_len == 0)
1011 goto out;
1012 /* Check if:
1013 * - we won't read outside the buffer
1014 * - there is enough place in the destination
1015 */
1016 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
1017 goto out_error;
1018
1019 /* +1 to take label len + label string */
1020 label_len += 1;
1021
1022 memcpy(dest, reader, label_len);
1023
1024 dest += label_len;
1025 nb_bytes += label_len;
1026 reader += label_len;
1027 }
1028
1029 out:
1030 /* offset computation:
1031 * parse from <name> until finding either NULL or a pointer "c0xx"
1032 */
1033 reader = name;
1034 *offset = 0;
1035 while (reader < bufend) {
1036 if ((reader[0] & 0xc0) == 0xc0) {
1037 *offset += 2;
1038 break;
1039 }
1040 else if (*reader == 0) {
1041 *offset += 1;
1042 break;
1043 }
1044 *offset += 1;
1045 ++reader;
1046 }
1047
1048 return nb_bytes;
1049
1050 out_error:
1051 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001052}
1053
1054/*
1055 * Function to validate that the buffer DNS response provided in <resp> and
1056 * finishing before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001057 *
Baptiste Assmann729c9012017-05-22 15:13:10 +02001058 * The result is stored in <resolution>' response, buf_response, response_query_records
1059 * and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001060 *
1061 * This function returns one of the DNS_RESP_* code to indicate the type of
1062 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001063 */
Baptiste Assmann729c9012017-05-22 15:13:10 +02001064int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001065{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001066 unsigned char *reader;
1067 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001068 int len, flags, offset;
1069 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +02001070 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001071 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001072 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +02001073 struct dns_response_packet *dns_p;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001074 int found = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001075 int i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001076
1077 reader = resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001078 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001079 previous_dname = NULL;
Baptiste Assmann251abb92017-08-11 09:58:27 +02001080 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081
Baptiste Assmann729c9012017-05-22 15:13:10 +02001082 /* initialization of response buffer and structure */
1083 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001084
1085 /* query id */
1086 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001087 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001088 dns_p->header.id = reader[0] * 256 + reader[1];
1089 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001090
1091 /*
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001092 * flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001093 * First byte contains:
1094 * - response flag (1 bit)
1095 * - opcode (4 bits)
1096 * - authoritative (1 bit)
1097 * - truncated (1 bit)
1098 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001099 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001100 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001101 return DNS_RESP_INVALID;
1102
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001103 flags = reader[0] * 256 + reader[1];
1104
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001105 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
1106 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001108 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109 return DNS_RESP_REFUSED;
1110
1111 return DNS_RESP_ERROR;
1112 }
1113
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001114 /* move forward 2 bytes for flags */
1115 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001116
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001117 /* 2 bytes for question count */
1118 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001119 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001120 dns_p->header.qdcount = reader[0] * 256 + reader[1];
1121 /* (for now) we send one query only, so we expect only one in the response too */
1122 if (dns_p->header.qdcount != 1)
1123 return DNS_RESP_QUERY_COUNT_ERROR;
1124 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001125 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001126 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001127
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001128 /* 2 bytes for answer count */
1129 if (reader + 2 >= bufend)
1130 return DNS_RESP_INVALID;
1131 dns_p->header.ancount = reader[0] * 256 + reader[1];
1132 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001133 return DNS_RESP_ANCOUNT_ZERO;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001134 /* check if too many records are announced */
1135 if (dns_p->header.ancount > DNS_MAX_ANSWER_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001136 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001137 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001138
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001139 /* 2 bytes authority count */
1140 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001141 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001142 dns_p->header.nscount = reader[0] * 256 + reader[1];
1143 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001144
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001145 /* 2 bytes additional count */
1146 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001147 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001148 dns_p->header.arcount = reader[0] * 256 + reader[1];
1149 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001150
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001151 /* parsing dns queries */
1152 LIST_INIT(&dns_p->query_list);
1153 for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) {
1154 /* use next pre-allocated dns_query_item after ensuring there is
1155 * still one available.
1156 * It's then added to our packet query list.
1157 */
1158 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
1159 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +02001160 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001161 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001162
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001163 /* name is a NULL terminated string in our case, since we have
1164 * one query per response and the first one can't be compressed
1165 * (using the 0x0c format)
1166 */
1167 offset = 0;
1168 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001169
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001170 if (len == 0)
1171 return DNS_RESP_INVALID;
1172
1173 reader += offset;
1174 previous_dname = dns_query->name;
1175
1176 /* move forward 2 bytes for question type */
1177 if (reader + 2 >= bufend)
1178 return DNS_RESP_INVALID;
1179 dns_query->type = reader[0] * 256 + reader[1];
1180 reader += 2;
1181
1182 /* move forward 2 bytes for question class */
1183 if (reader + 2 >= bufend)
1184 return DNS_RESP_INVALID;
1185 dns_query->class = reader[0] * 256 + reader[1];
1186 reader += 2;
1187 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001188
Baptiste Assmann251abb92017-08-11 09:58:27 +02001189 /* TRUNCATED flag must be checked after we could read the query type
1190 * because a TRUNCATED SRV query type response can still be exploited
1191 */
1192 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
1193 return DNS_RESP_TRUNCATED;
1194
Baptiste Assmann325137d2015-04-13 23:40:55 +02001195 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001196 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001197 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001198 if (reader >= bufend)
1199 return DNS_RESP_INVALID;
1200
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001201 dns_answer_record = pool_alloc2(dns_answer_item_pool);
1202 if (dns_answer_record == NULL)
1203 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001204
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001205 offset = 0;
1206 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001207
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001208 if (len == 0) {
1209 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001210 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001211 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001212
1213 /* check if the current record dname is valid.
1214 * previous_dname points either to queried dname or last CNAME target
1215 */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +02001216 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001217 free_dns_answer_item(dns_answer_record);
1218 if (i == 0) {
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001219 /* first record, means a mismatch issue between queried dname
1220 * and dname found in the first record */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001221 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001222 } else {
1223 /* if not the first record, this means we have a CNAME resolution
1224 * error */
1225 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001226 }
1227
Baptiste Assmann325137d2015-04-13 23:40:55 +02001228 }
1229
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001230 memcpy(dns_answer_record->name, tmpname, len);
1231 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +02001232
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001233 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001234 if (reader >= bufend) {
1235 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001236 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001237 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001238
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001239 if (reader >= bufend) {
1240 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001241 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001242 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001243
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001244 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001245 if (reader + 2 > bufend) {
1246 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001247 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001248 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001249 dns_answer_record->type = reader[0] * 256 + reader[1];
1250 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001251
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001252 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001253 if (reader + 2 > bufend) {
1254 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001255 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001256 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001257 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001258 reader += 2;
1259
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001260 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001261 if (reader + 4 > bufend) {
1262 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001263 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001264 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001265 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1266 + reader[2] * 256 + reader[3];
1267 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001268
1269 /* now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001270 if (reader + 2 > bufend) {
1271 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001272 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001273 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001274 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001275
1276 /* move forward 2 bytes for data len */
1277 reader += 2;
1278
1279 /* analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001280 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001281 case DNS_RTYPE_A:
1282 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001283 if (dns_answer_record->data_len != 4) {
1284 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001285 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001286 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001287 dns_answer_record->address.sa_family = AF_INET;
1288 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1289 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001290 break;
1291
1292 case DNS_RTYPE_CNAME:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001293 /* check if this is the last record and update the caller about the status:
1294 * no IP could be found and last record was a CNAME. Could be triggered
1295 * by a wrong query type
1296 *
1297 * + 1 because dns_answer_record_id starts at 0 while number of answers
1298 * is an integer and starts at 1.
1299 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001300 if (i + 1 == dns_p->header.ancount) {
1301 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001302 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001303 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001304
1305 offset = 0;
1306 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
1307
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001308 if (len == 0) {
1309 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001310 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001311 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001312
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001313 memcpy(dns_answer_record->target, tmpname, len);
1314 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001315
1316 previous_dname = dns_answer_record->target;
1317
Baptiste Assmann325137d2015-04-13 23:40:55 +02001318 break;
1319
Olivier Houchard8da5f982017-08-04 18:35:36 +02001320
1321 case DNS_RTYPE_SRV:
1322 /*
1323 * Answer must contain :
1324 * - 2 bytes for the priority
1325 * - 2 bytes for the weight
1326 * - 2 bytes for the port
1327 * - the target hostname
1328 */
1329 if (dns_answer_record->data_len <= 6) {
1330 free_dns_answer_item(dns_answer_record);
1331 return DNS_RESP_INVALID;
1332 }
1333 dns_answer_record->priority = readn16(reader);
1334 reader += sizeof(uint16_t);
1335 dns_answer_record->weight = readn16(reader);
1336 reader += sizeof(uint16_t);
1337 dns_answer_record->port = readn16(reader);
1338 reader += sizeof(uint16_t);
1339 offset = 0;
1340 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
1341 if (len == 0) {
1342 free_dns_answer_item(dns_answer_record);
1343 return DNS_RESP_INVALID;
1344 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001345 dns_answer_record->data_len = len;
1346 memcpy(dns_answer_record->target, tmpname, len);
1347 dns_answer_record->target[len] = 0;
1348 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001349 case DNS_RTYPE_AAAA:
1350 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001351 if (dns_answer_record->data_len != 16) {
1352 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001353 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001354 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001355 dns_answer_record->address.sa_family = AF_INET6;
1356 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1357 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001358 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001359
Baptiste Assmann325137d2015-04-13 23:40:55 +02001360 } /* switch (record type) */
1361
Baptiste Assmann69fce672017-05-04 08:37:45 +02001362 /* increment the counter for number of records saved into our local response */
1363 nb_saved_records += 1;
1364
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001365 /* move forward dns_answer_record->data_len for analyzing next record in the response */
Baptiste Assmann63a28112017-08-11 10:37:20 +02001366 if (dns_answer_record->type == DNS_RTYPE_SRV)
1367 reader += offset;
1368 else
1369 reader += dns_answer_record->data_len;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001370
1371 /* Lookup to see if we already had this entry */
1372
Olivier Houchard8da5f982017-08-04 18:35:36 +02001373 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001374 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1375 if (tmp_record->type != dns_answer_record->type)
1376 continue;
1377 switch (tmp_record->type) {
1378 case DNS_RTYPE_A:
1379 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1380 &((struct sockaddr_in *)&tmp_record->address)->sin_addr, sizeof(in_addr_t)))
1381 found = 1;
1382 break;
1383 case DNS_RTYPE_AAAA:
1384 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1385 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr, sizeof(struct in6_addr)))
1386 found = 1;
1387 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001388 case DNS_RTYPE_SRV:
1389 if (dns_answer_record->data_len == tmp_record->data_len &&
1390 !memcmp(dns_answer_record->target,
1391 tmp_record->target, dns_answer_record->data_len) &&
1392 dns_answer_record->port == tmp_record->port) {
1393 tmp_record->weight = dns_answer_record->weight;
1394 found = 1;
1395 }
1396 break;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001397 default:
1398 break;
1399 }
1400 if (found == 1)
1401 break;
1402 }
1403 if (found == 1) {
1404 tmp_record->last_seen = now.tv_sec;
1405 free_dns_answer_item(dns_answer_record);
1406 } else {
1407 dns_answer_record->last_seen = now.tv_sec;
1408 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1409 }
1410
Baptiste Assmann325137d2015-04-13 23:40:55 +02001411 } /* for i 0 to ancount */
1412
Baptiste Assmann96972bc2015-09-09 00:46:58 +02001413
Baptiste Assmann69fce672017-05-04 08:37:45 +02001414 /* save the number of records we really own */
1415 dns_p->header.ancount = nb_saved_records;
1416
Baptiste Assmann325137d2015-04-13 23:40:55 +02001417 return DNS_RESP_VALID;
1418}
1419
1420/*
1421 * search dn_name resolution in resp.
1422 * If existing IP not found, return the first IP matching family_priority,
1423 * otherwise, first ip found
1424 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001425 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001426 * For both cases above, dns_validate_dns_response is required
1427 * returns one of the DNS_UPD_* code
1428 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001429#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001430int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001431 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001432 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001433 void **newip, short *newip_sin_family,
1434 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001435{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001436 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001437 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001438 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001439 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001440 int currentip_sel;
1441 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001442 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001443
Baptiste Assmann42746372017-05-03 12:12:02 +02001444 family_priority = dns_opts->family_prio;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001445 *newip = newip4 = newip6 = NULL;
1446 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001447 *newip_sin_family = AF_UNSPEC;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001448 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001449
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001450 /* Select an IP regarding configuration preference.
1451 * Top priority is the prefered network ip version,
1452 * second priority is the prefered network.
1453 * the last priority is the currently used IP,
1454 *
1455 * For these three priorities, a score is calculated. The
1456 * weight are:
Baptistefc725902016-12-26 23:21:08 +01001457 * 8 - prefered netwok ip version.
1458 * 4 - prefered network.
1459 * 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 +01001460 * 1 - current ip.
1461 * The result with the biggest score is returned.
1462 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001463
1464 list_for_each_entry(record, &dns_p->answer_list, list) {
1465 void *ip;
1466 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001467
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001468 if (record->type == DNS_RTYPE_A) {
1469 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1470 ip_type = AF_INET;
1471 } else if (record->type == DNS_RTYPE_AAAA) {
1472 ip_type = AF_INET6;
1473 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
1474 } else
1475 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001476 score = 0;
1477
1478 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001479 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001480 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001481
1482 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001483 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001484
1485 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001486 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001487 continue;
1488
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001489 if ((ip_type == AF_INET &&
1490 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001491 &dns_opts->pref_net[j].mask.in4,
1492 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001493 (ip_type == AF_INET6 &&
1494 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001495 &dns_opts->pref_net[j].mask.in6,
1496 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001497 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001498 break;
1499 }
1500 }
1501
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001502 /* Check if the IP found in the record is already affected to a member of a group.
1503 * If yes, the score should be incremented by 2.
1504 */
1505 if (owner) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001506 if (snr_check_ip_callback(owner, ip, &ip_type))
1507 {
1508 continue;
1509 }
Baptistefc725902016-12-26 23:21:08 +01001510 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001511 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001512 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001513 ((currentip_sin_family == AF_INET &&
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001514 memcmp(ip, currentip, 4) == 0) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001515 (currentip_sin_family == AF_INET6 &&
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001516 memcmp(ip, currentip, 16) == 0))) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001517 score += 1;
1518 currentip_sel = 1;
1519 } else
1520 currentip_sel = 0;
1521
Baptistefc725902016-12-26 23:21:08 +01001522
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001523 /* Keep the address if the score is better than the previous
Baptistefc725902016-12-26 23:21:08 +01001524 * score. The maximum score is 15, if this value is reached,
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001525 * we break the parsing. Implicitly, this score is reached
1526 * the ip selected is the current ip.
1527 */
1528 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001529 if (ip_type == AF_INET)
1530 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001531 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001532 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001533 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001534 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001535 return DNS_UPD_NO;
1536 max_score = score;
1537 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001538
1539
1540 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001541
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001542 /* no IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001543 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001544 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001545
Baptiste Assmann325137d2015-04-13 23:40:55 +02001546 /* case when the caller looks first for an IPv4 address */
1547 if (family_priority == AF_INET) {
1548 if (newip4) {
1549 *newip = newip4;
1550 *newip_sin_family = AF_INET;
1551 if (currentip_found == 1)
1552 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001553 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001554 }
1555 else if (newip6) {
1556 *newip = newip6;
1557 *newip_sin_family = AF_INET6;
1558 if (currentip_found == 1)
1559 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001560 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001561 }
1562 }
1563 /* case when the caller looks first for an IPv6 address */
1564 else if (family_priority == AF_INET6) {
1565 if (newip6) {
1566 *newip = newip6;
1567 *newip_sin_family = AF_INET6;
1568 if (currentip_found == 1)
1569 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001570 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001571 }
1572 else if (newip4) {
1573 *newip = newip4;
1574 *newip_sin_family = AF_INET;
1575 if (currentip_found == 1)
1576 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001577 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001578 }
1579 }
1580 /* case when the caller have no preference (we prefer IPv6) */
1581 else if (family_priority == AF_UNSPEC) {
1582 if (newip6) {
1583 *newip = newip6;
1584 *newip_sin_family = AF_INET6;
1585 if (currentip_found == 1)
1586 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001587 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001588 }
1589 else if (newip4) {
1590 *newip = newip4;
1591 *newip_sin_family = AF_INET;
1592 if (currentip_found == 1)
1593 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001594 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001595 }
1596 }
1597
1598 /* no reason why we should change the server's IP address */
1599 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001600
1601 return_DNS_UPD_SRVIP_NOT_FOUND:
1602 list_for_each_entry(record, &dns_p->answer_list, list) {
1603 /* move the first record to the end of the list, for internal round robin */
1604 if (record) {
1605 LIST_DEL(&record->list);
1606 LIST_ADDQ(&dns_p->answer_list, &record->list);
1607 break;
1608 }
1609 }
1610 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001611}
1612
1613/*
1614 * returns the query id contained in a DNS response
1615 */
Thiago Farinab1af23e2016-01-20 23:46:34 +01001616unsigned short dns_response_get_query_id(unsigned char *resp)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001617{
1618 /* read the query id from the response */
1619 return resp[0] * 256 + resp[1];
1620}
1621
1622/*
1623 * used during haproxy's init phase
1624 * parses resolvers sections and initializes:
1625 * - task (time events) for each resolvers section
1626 * - the datagram layer (network IO events) for each nameserver
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001627 * It takes one argument:
1628 * - close_first takes 2 values: 0 or 1. If 1, the connection is closed first.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001629 * returns:
1630 * 0 in case of error
1631 * 1 when no error
1632 */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001633int dns_init_resolvers(int close_socket)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001634{
1635 struct dns_resolvers *curr_resolvers;
1636 struct dns_nameserver *curnameserver;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001637 struct dns_resolution *resolution, *res_back;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001638 struct dgram_conn *dgram;
1639 struct task *t;
1640 int fd;
1641
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001642 /* initialize our DNS resolution cache */
1643 dns_lru_tree = lru64_new(dns_cache_size);
1644
Baptiste Assmann325137d2015-04-13 23:40:55 +02001645 /* give a first random value to our dns query_id seed */
1646 dns_query_id_seed = random();
1647
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001648 /* Initialize the answer items pool */
1649 dns_answer_item_pool = create_pool("dns_answer_item",
1650 sizeof(struct dns_answer_item), MEM_F_SHARED);
1651 if (dns_answer_item_pool == NULL) {
1652 Alert("Failed to create the dns answer items pool");
1653 return 0;
1654 }
1655
Baptiste Assmann325137d2015-04-13 23:40:55 +02001656 /* run through the resolvers section list */
1657 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
1658 /* create the task associated to the resolvers section */
1659 if ((t = task_new()) == NULL) {
1660 Alert("Starting [%s] resolvers: out of memory.\n", curr_resolvers->id);
1661 return 0;
1662 }
1663
1664 /* update task's parameters */
1665 t->process = dns_process_resolve;
1666 t->context = curr_resolvers;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001667
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001668 /* no need to keep the new task if one is already affected to our resolvers
1669 * section */
1670 if (!curr_resolvers->t)
1671 curr_resolvers->t = t;
1672 else
1673 task_free(t);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001674
1675 list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001676 dgram = NULL;
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001677
1678 if (close_socket == 1) {
1679 if (curnameserver->dgram) {
Frédéric Lécaille64920532017-05-12 09:57:15 +02001680 fd_delete(curnameserver->dgram->t.sock.fd);
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001681 memset(curnameserver->dgram, '\0', sizeof(*dgram));
1682 dgram = curnameserver->dgram;
1683 }
1684 }
1685
1686 /* allocate memory only if it has not already been allocated
1687 * by a previous call to this function */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001688
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001689 if (!dgram && (dgram = calloc(1, sizeof(*dgram))) == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001690 Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id,
1691 curnameserver->id);
1692 return 0;
1693 }
1694 /* update datagram's parameters */
1695 dgram->owner = (void *)curnameserver;
1696 dgram->data = &resolve_dgram_cb;
1697
1698 /* create network UDP socket for this nameserver */
Frédéric Lécaille5e5bc9f2017-04-11 08:46:37 +02001699 if ((fd = socket(curnameserver->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001700 Alert("Starting [%s/%s] nameserver: can't create socket.\n", curr_resolvers->id,
1701 curnameserver->id);
1702 free(dgram);
1703 dgram = NULL;
1704 return 0;
1705 }
1706
1707 /* "connect" the UDP socket to the name server IP */
Baptiste Assmann8c62c472015-09-21 20:55:08 +02001708 if (connect(fd, (struct sockaddr*)&curnameserver->addr, get_addr_len(&curnameserver->addr)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001709 Alert("Starting [%s/%s] nameserver: can't connect socket.\n", curr_resolvers->id,
1710 curnameserver->id);
1711 close(fd);
1712 free(dgram);
1713 dgram = NULL;
1714 return 0;
1715 }
1716
1717 /* make the socket non blocking */
1718 fcntl(fd, F_SETFL, O_NONBLOCK);
1719
1720 /* add the fd in the fd list and update its parameters */
1721 fd_insert(fd);
1722 fdtab[fd].owner = dgram;
1723 fdtab[fd].iocb = dgram_fd_handler;
1724 fd_want_recv(fd);
1725 dgram->t.sock.fd = fd;
1726
1727 /* update nameserver's datagram property */
1728 curnameserver->dgram = dgram;
1729
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001730 continue;
1731 }
1732
1733 if (close_socket == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001734 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001735
1736 /* now, we can trigger DNS resolution */
1737 list_for_each_entry_safe(resolution, res_back, &curr_resolvers->resolution.wait, list) {
1738 /* if there is no requester in the wait queue, no need to trigger the resolution */
1739 if (LIST_ISEMPTY(&resolution->requester.wait))
1740 continue;
1741
1742 dns_trigger_resolution(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001743 }
1744
1745 /* task can be queued */
1746 task_queue(t);
1747 }
1748
1749 return 1;
1750}
1751
1752/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001753 * Allocate a pool of resolution to a resolvers section.
1754 * Each resolution is associated with a UUID.
1755 *
1756 * Return code:
1757 * - 0 if everything went smoothly
1758 * - -1 if an error occured
1759 */
1760int dns_alloc_resolution_pool(struct dns_resolvers *resolvers)
1761{
1762 int i;
1763 struct dns_resolution *resolution;
1764
1765 /* return if a pool has already been set for this resolvers */
1766 if (!LIST_ISEMPTY(&resolvers->resolution.pool)) {
1767 return 0;
1768 }
1769
1770 for (i = 0; i < resolvers->resolution_pool_size; i++) {
1771 resolution = dns_alloc_resolution();
1772 if (!resolution) {
1773 Alert("Starting [%s] resolvers: can't allocate memory for DNS resolution pool.\n", resolvers->id);
1774 return -1;
1775 }
1776 resolution->uuid = i;
1777 LIST_ADDQ(&resolvers->resolution.pool, &resolution->list);
1778 }
1779
1780 return 0;
1781}
1782
1783/*
Baptiste Assmann325137d2015-04-13 23:40:55 +02001784 * Forge a DNS query. It needs the following information from the caller:
1785 * - <query_id>: the DNS query id corresponding to this query
1786 * - <query_type>: DNS_RTYPE_* request DNS record type (A, AAAA, ANY, etc...)
1787 * - <hostname_dn>: hostname in domain name format
1788 * - <hostname_dn_len>: length of <hostname_dn>
1789 * To store the query, the caller must pass a buffer <buf> and its size <bufsize>
1790 *
1791 * the DNS query is stored in <buf>
1792 * returns:
1793 * -1 if <buf> is too short
1794 */
1795int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
1796{
1797 struct dns_header *dns;
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001798 struct dns_question qinfo;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001799 char *ptr, *bufend;
1800
1801 memset(buf, '\0', bufsize);
1802 ptr = buf;
1803 bufend = buf + bufsize;
1804
1805 /* check if there is enough room for DNS headers */
1806 if (ptr + sizeof(struct dns_header) >= bufend)
1807 return -1;
1808
1809 /* set dns query headers */
1810 dns = (struct dns_header *)ptr;
1811 dns->id = (unsigned short) htons(query_id);
Nenad Merdanovic8ab79422016-07-13 14:03:43 +02001812 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 +02001813 dns->qdcount = htons(1); /* 1 question */
1814 dns->ancount = 0;
1815 dns->nscount = 0;
1816 dns->arcount = 0;
1817
1818 /* move forward ptr */
1819 ptr += sizeof(struct dns_header);
1820
1821 /* check if there is enough room for query hostname */
1822 if ((ptr + hostname_dn_len) >= bufend)
1823 return -1;
1824
1825 /* set up query hostname */
1826 memcpy(ptr, hostname_dn, hostname_dn_len);
1827 ptr[hostname_dn_len + 1] = '\0';
1828
1829 /* move forward ptr */
1830 ptr += (hostname_dn_len + 1);
1831
1832 /* check if there is enough room for query hostname*/
1833 if (ptr + sizeof(struct dns_question) >= bufend)
1834 return -1;
1835
1836 /* set up query info (type and class) */
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001837 qinfo.qtype = htons(query_type);
1838 qinfo.qclass = htons(DNS_RCLASS_IN);
1839 memcpy(ptr, &qinfo, sizeof(qinfo));
Baptiste Assmann325137d2015-04-13 23:40:55 +02001840
1841 ptr += sizeof(struct dns_question);
1842
1843 return ptr - buf;
1844}
1845
Olivier Houchard8da5f982017-08-04 18:35:36 +02001846/* Turn a domain name label into a string */
1847void dns_dn_label_to_str(char *dn, char *str, int dn_len)
1848{
1849 int remain_size = 0;
1850 int i;
1851
1852 for (i = 0; i < dn_len; i++) {
1853 if (remain_size == 0) {
1854 remain_size = dn[i];
1855 if (i != 0) {
1856 str[i - 1] = '.';
1857
1858 }
1859 } else {
1860 str[i - 1] = dn[i];
1861 remain_size--;
1862 }
1863 }
1864 str[dn_len - 1] = 0;
1865
1866}
1867
Baptiste Assmann325137d2015-04-13 23:40:55 +02001868/*
1869 * turn a string into domain name label:
1870 * www.haproxy.org into 3www7haproxy3org
1871 * if dn memory is pre-allocated, you must provide its size in dn_len
1872 * if dn memory isn't allocated, dn_len must be set to 0.
1873 * In the second case, memory will be allocated.
1874 * in case of error, -1 is returned, otherwise, number of bytes copied in dn
1875 */
Willy Tarreau2100b492015-07-22 16:42:43 +02001876char *dns_str_to_dn_label(const char *string, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001877{
1878 char *c, *d;
1879 int i, offset;
1880
1881 /* offset between string size and theorical dn size */
1882 offset = 1;
1883
1884 /*
1885 * first, get the size of the string turned into its domain name version
1886 * This function also validates the string respect the RFC
1887 */
1888 if ((i = dns_str_to_dn_label_len(string)) == -1)
1889 return NULL;
1890
1891 /* yes, so let's check there is enough memory */
1892 if (dn_len < i + offset)
1893 return NULL;
1894
Willy Tarreaud69d6f32015-07-22 16:45:36 +02001895 i = strlen(string);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001896 memcpy(dn + offset, string, i);
1897 dn[i + offset] = '\0';
1898 /* avoid a '\0' at the beginning of dn string which may prevent the for loop
1899 * below from working.
1900 * Actually, this is the reason of the offset. */
1901 dn[0] = '0';
1902
1903 for (c = dn; *c ; ++c) {
1904 /* c points to the first '0' char or a dot, which we don't want to read */
1905 d = c + offset;
1906 i = 0;
1907 while (*d != '.' && *d) {
1908 i++;
1909 d++;
1910 }
1911 *c = i;
1912
1913 c = d - 1; /* because of c++ of the for loop */
1914 }
1915
1916 return dn;
1917}
1918
1919/*
1920 * compute and return the length of <string> it it were translated into domain name
1921 * label:
1922 * www.haproxy.org into 3www7haproxy3org would return 16
1923 * NOTE: add +1 for '\0' when allocating memory ;)
1924 */
1925int dns_str_to_dn_label_len(const char *string)
1926{
1927 return strlen(string) + 1;
1928}
1929
1930/*
1931 * validates host name:
1932 * - total size
1933 * - each label size individually
1934 * returns:
1935 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1936 * 1 when no error. <err> is left unaffected.
1937 */
1938int dns_hostname_validation(const char *string, char **err)
1939{
1940 const char *c, *d;
1941 int i;
1942
1943 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1944 if (err)
1945 *err = DNS_TOO_LONG_FQDN;
1946 return 0;
1947 }
1948
1949 c = string;
1950 while (*c) {
1951 d = c;
1952
1953 i = 0;
1954 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1955 i++;
1956 if (!((*d == '-') || (*d == '_') ||
1957 ((*d >= 'a') && (*d <= 'z')) ||
1958 ((*d >= 'A') && (*d <= 'Z')) ||
1959 ((*d >= '0') && (*d <= '9')))) {
1960 if (err)
1961 *err = DNS_INVALID_CHARACTER;
1962 return 0;
1963 }
1964 d++;
1965 }
1966
1967 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1968 if (err)
1969 *err = DNS_LABEL_TOO_LONG;
1970 return 0;
1971 }
1972
1973 if (*d == '\0')
1974 goto out;
1975
1976 c = ++d;
1977 }
1978 out:
1979 return 1;
1980}
1981
1982/*
1983 * 2 bytes random generator to generate DNS query ID
1984 */
1985uint16_t dns_rnd16(void)
1986{
1987 dns_query_id_seed ^= dns_query_id_seed << 13;
1988 dns_query_id_seed ^= dns_query_id_seed >> 7;
1989 dns_query_id_seed ^= dns_query_id_seed << 17;
1990 return dns_query_id_seed;
1991}
1992
1993
1994/*
1995 * function called when a timeout occurs during name resolution process
1996 * if max number of tries is reached, then stop, otherwise, retry.
1997 */
1998struct task *dns_process_resolve(struct task *t)
1999{
2000 struct dns_resolvers *resolvers = t->context;
2001 struct dns_resolution *resolution, *res_back;
Baptiste Assmann060e5732016-01-06 02:01:59 +01002002 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmann42746372017-05-03 12:12:02 +02002003 struct dns_options *dns_opts = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002004
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002005 /* if both there is no resolution in the run queue, we can re-schedule a wake up */
2006 if (LIST_ISEMPTY(&resolvers->resolution.curr)) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02002007 /* no first entry, so wake up was useless */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002008 dns_update_resolvers_timeout(resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002009 return t;
2010 }
2011
2012 /* look for the first resolution which is not expired */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002013 list_for_each_entry_safe(resolution, res_back, &resolvers->resolution.curr, list) {
2014 struct dns_requester *requester = NULL;
2015
Baptiste Assmann325137d2015-04-13 23:40:55 +02002016 /* when we find the first resolution in the future, then we can stop here */
2017 if (tick_is_le(now_ms, resolution->last_sent_packet))
2018 goto out;
2019
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002020 if (LIST_ISEMPTY(&resolution->requester.curr))
2021 goto out;
2022
Baptiste Assmann325137d2015-04-13 23:40:55 +02002023 /*
2024 * if current resolution has been tried too many times and finishes in timeout
2025 * we update its status and remove it from the list
2026 */
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002027 if (resolution->try <= 0) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002028 struct dns_requester *tmprequester;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002029 /* clean up resolution information and remove from the list */
2030 dns_reset_resolution(resolution);
2031
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002032 LIST_DEL(&resolution->list);
2033 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
2034
2035 if (resolution->status != RSLV_STATUS_TIMEOUT) {
2036 resolution->status = RSLV_STATUS_TIMEOUT;
2037 resolution->last_status_change = now_ms;
2038 }
2039
2040 /* notify the result to the requesters */
2041 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
2042 requester->requester_error_cb(requester, DNS_RESP_TIMEOUT);
2043 LIST_DEL(&requester->list);
2044 LIST_ADDQ(&resolution->requester.wait, &requester->list);
2045 }
Baptiste Assmann382824c2016-01-06 01:53:46 +01002046 goto out;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002047 }
2048
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002049 resolution->try -= 1;
2050
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002051 /* running queue is empty, nothing to do but wait */
2052 if (LIST_ISEMPTY(&resolution->requester.curr))
2053 goto out;
2054
2055 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
2056
2057 switch (obj_type(requester->requester)) {
2058 case OBJ_TYPE_SERVER:
2059 dns_opts = &(objt_server(requester->requester)->dns_opts);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002060 res_preferred_afinet = dns_opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
2061 res_preferred_afinet6 = dns_opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
2062
2063 /* let's change the query type if needed */
2064 if (res_preferred_afinet6) {
2065 /* fallback from AAAA to A */
2066 resolution->query_type = DNS_RTYPE_A;
2067 }
2068 else if (res_preferred_afinet) {
2069 /* fallback from A to AAAA */
2070 resolution->query_type = DNS_RTYPE_AAAA;
2071 }
2072
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002073 break;
2074
Olivier Houchard8da5f982017-08-04 18:35:36 +02002075 case OBJ_TYPE_SRVRQ:
2076 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002077 case OBJ_TYPE_NONE:
2078 default:
2079 /* clean up resolution information and remove from the list */
2080 dns_reset_resolution(resolution);
2081
2082 LIST_DEL(&resolution->list);
2083 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
2084
2085 /* notify the result to the requester */
2086 requester->requester_error_cb(requester, DNS_RESP_INTERNAL);
2087 goto out;
2088 }
Baptiste Assmann42746372017-05-03 12:12:02 +02002089
Baptiste Assmann382824c2016-01-06 01:53:46 +01002090 /* resend the DNS query */
2091 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002092
Baptiste Assmann382824c2016-01-06 01:53:46 +01002093 /* check if we have more than one resolution in the list */
2094 if (dns_check_resolution_queue(resolvers) > 1) {
2095 /* move the rsolution to the end of the list */
2096 LIST_DEL(&resolution->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002097 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002098 }
2099 }
2100
2101 out:
2102 dns_update_resolvers_timeout(resolvers);
2103 return t;
2104}
William Lallemand69e96442016-11-19 00:58:54 +01002105
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002106/*
2107 * build a dns cache key composed as follow:
2108 * <query type>#<hostname in domain name format>
2109 * and store it into <str>.
2110 * It's up to the caller to allocate <buf> and to reset it.
2111 * The function returns NULL in case of error (IE <buf> too small) or a pointer
2112 * to buf if successful
2113 */
2114struct chunk *
2115dns_cache_key(int query_type, char *hostname_dn, int hostname_dn_len, struct chunk *buf)
2116{
2117 int len, size;
2118 char *str;
2119
2120 str = buf->str;
2121 len = buf->len;
2122 size = buf->size;
2123
2124 switch (query_type) {
2125 case DNS_RTYPE_A:
2126 if (len + 1 > size)
2127 return NULL;
2128 memcpy(&str[len], "A", 1);
2129 len += 1;
2130 break;
2131 case DNS_RTYPE_AAAA:
2132 if (len + 4 > size)
2133 return NULL;
2134 memcpy(&str[len], "AAAA", 4);
2135 len += 4;
2136 break;
2137 default:
2138 return NULL;
2139 }
2140
2141 if (len + 1 > size)
2142 return NULL;
2143 memcpy(&str[len], "#", 1);
2144 len += 1;
2145
2146 if (len + hostname_dn_len + 1 > size) // +1 for trailing zero
2147 return NULL;
2148 memcpy(&str[len], hostname_dn, hostname_dn_len);
2149 len += hostname_dn_len;
2150 str[len] = '\0';
2151
2152 return buf;
2153}
2154
2155/*
2156 * returns a pointer to a cache entry which may still be considered as up to date
2157 * by the caller.
2158 * returns NULL if no entry can be found or if the data found is outdated.
2159 */
2160struct lru64 *
2161dns_cache_lookup(int query_type, char *hostname_dn, int hostname_dn_len, int valid_period, void *cache_domain) {
2162 struct lru64 *elem = NULL;
2163 struct dns_resolution *resolution = NULL;
2164 struct dns_resolvers *resolvers = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002165 struct dns_requester *requester = NULL;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002166 int inter = 0;
2167 struct chunk *buf = get_trash_chunk();
2168 struct chunk *tmp = NULL;
2169
2170 if (!dns_lru_tree)
2171 return NULL;
2172
2173 chunk_reset(buf);
2174 tmp = dns_cache_key(query_type, hostname_dn, hostname_dn_len, buf);
2175 if (tmp == NULL)
2176 return NULL;
2177
2178 elem = lru64_lookup(XXH64(buf->str, buf->len, 1), dns_lru_tree, cache_domain, 1);
2179
2180 if (!elem || !elem->data)
2181 return NULL;
2182
2183 resolution = elem->data;
2184
2185 /* since we can change the fqdn of a server at run time, it may happen that
2186 * we got an innacurate elem.
2187 * This is because resolution->hostname_dn points to (owner)->hostname_dn (which
2188 * may be changed at run time)
2189 */
2190 if ((hostname_dn_len == resolution->hostname_dn_len) &&
2191 (memcmp(hostname_dn, resolution->hostname_dn, hostname_dn_len) != 0)) {
2192 return NULL;
2193 }
2194
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002195 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2196
2197 switch (obj_type(requester->requester)) {
2198 case OBJ_TYPE_SERVER:
2199 resolvers = objt_server(requester->requester)->resolvers;
2200 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002201 case OBJ_TYPE_SRVRQ:
2202 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
2203 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002204 case OBJ_TYPE_NONE:
2205 default:
2206 return NULL;
2207 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002208
2209 if (!resolvers)
2210 return NULL;
2211
2212 if (resolvers->hold.valid < valid_period)
2213 inter = resolvers->hold.valid;
2214 else
2215 inter = valid_period;
2216
2217 if (!tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms))
2218 return elem;
2219
2220 return NULL;
2221}
2222
Willy Tarreau777b5602016-12-16 18:06:26 +01002223/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
William Lallemand69e96442016-11-19 00:58:54 +01002224static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
2225{
2226 struct dns_resolvers *presolvers;
2227
2228 if (*args[3]) {
William Lallemand69e96442016-11-19 00:58:54 +01002229 list_for_each_entry(presolvers, &dns_resolvers, list) {
2230 if (strcmp(presolvers->id, args[3]) == 0) {
Willy Tarreau777b5602016-12-16 18:06:26 +01002231 appctx->ctx.cli.p0 = presolvers;
William Lallemand69e96442016-11-19 00:58:54 +01002232 break;
2233 }
2234 }
Willy Tarreau777b5602016-12-16 18:06:26 +01002235 if (appctx->ctx.cli.p0 == NULL) {
William Lallemand69e96442016-11-19 00:58:54 +01002236 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002237 appctx->st0 = CLI_ST_PRINT;
William Lallemand69e96442016-11-19 00:58:54 +01002238 return 1;
2239 }
2240 }
Willy Tarreau3067bfa2016-12-05 14:50:15 +01002241 return 0;
William Lallemand69e96442016-11-19 00:58:54 +01002242}
2243
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002244/*
2245 * if <resolution> is provided, then the function skips the memory allocation part.
2246 * It does the linking only.
2247 *
2248 * if <resolution> is NULL, the function links a dns resolution to a requester:
2249 * - it allocates memory for the struct requester used to link
2250 * the resolution to the requester
2251 * - it configures the resolution if this is the first requester to be linked to it
2252 * - it updates the requester with a pointer to the resolution
2253 *
2254 * Return code:
2255 * - 0 if everything happened smoothly
2256 * - -1 if an error occured. Of course, no resolution is linked to the requester
2257 */
2258int dns_link_resolution(void *requester, int requester_type, struct dns_resolution *resolution)
2259{
2260 struct dns_resolution *tmpresolution = NULL;
2261 struct dns_requester *tmprequester = NULL;
2262 struct dns_resolvers *resolvers = NULL;
2263 char *hostname_dn = NULL;
2264 int new_resolution;
2265
Olivier Houchard8da5f982017-08-04 18:35:36 +02002266
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002267 if (!resolution) {
2268 tmprequester = calloc(1, sizeof(*tmprequester));
2269 if (!tmprequester)
2270 return -1;
2271
2272 switch (requester_type) {
2273 case OBJ_TYPE_SERVER:
2274 tmprequester->requester = &((struct server *)requester)->obj_type;
2275 hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2276 resolvers = objt_server(tmprequester->requester)->resolvers;
2277 switch (objt_server(tmprequester->requester)->dns_opts.family_prio) {
2278 case AF_INET:
2279 tmprequester->prefered_query_type = DNS_RTYPE_A;
2280 break;
2281 default:
2282 tmprequester->prefered_query_type = DNS_RTYPE_AAAA;
2283 }
2284
2285 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002286 case OBJ_TYPE_SRVRQ:
2287 tmprequester->requester = &((struct dns_srvrq *)requester)->obj_type;
2288 hostname_dn = objt_dns_srvrq(requester)->hostname_dn;
2289 resolvers = objt_dns_srvrq(requester)->resolvers;
2290 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002291 case OBJ_TYPE_NONE:
2292 default:
2293 free(tmprequester);
2294 return -1;
2295 }
2296
2297 /* get a resolution from the resolvers' wait queue or pool */
2298 tmpresolution = dns_resolution_list_get(resolvers, hostname_dn, tmprequester->prefered_query_type);
2299 if (!tmpresolution) {
2300 free(tmprequester);
2301 return -1;
2302 }
2303 }
2304 else {
2305 tmpresolution = resolution;
2306
2307 switch (requester_type) {
2308 case OBJ_TYPE_SERVER:
2309 tmprequester = ((struct server *)requester)->dns_requester;
2310 resolvers = ((struct server *)requester)->resolvers;
2311 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002312 case OBJ_TYPE_SRVRQ:
2313 tmprequester = objt_dns_srvrq(requester)->dns_requester;
2314 resolvers = objt_dns_srvrq(requester)->resolvers;
2315 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002316 case OBJ_TYPE_NONE:
2317 default:
2318 return -1;
2319 }
2320 }
2321
2322 /* flag this resolution as NEW if applicable (not already linked to any requester).
2323 * this is required to decide which parameters we have to update on the resolution.
2324 * If new, it means we pulled up the resolution from the resolvers' pool.
2325 */
2326 if (LIST_ISEMPTY(&tmpresolution->requester.wait)) {
2327 new_resolution = 1;
2328 }
2329 else
2330 new_resolution = 0;
2331
2332 /* those parameters are related to the requester type */
2333 switch (obj_type(tmprequester->requester)) {
2334 case OBJ_TYPE_SERVER:
2335 /* some parameters should be set only if the resolution is brand new */
2336 if (new_resolution) {
2337 tmpresolution->query_type = tmprequester->prefered_query_type;
2338 tmpresolution->hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2339 tmpresolution->hostname_dn_len = objt_server(tmprequester->requester)->hostname_dn_len;
2340 }
2341
2342 /* update requester as well, only if we just allocated it */
2343 objt_server(tmprequester->requester)->resolution = tmpresolution;
2344 if (!resolution) {
2345 tmprequester->requester_cb = snr_resolution_cb;
2346 tmprequester->requester_error_cb = snr_resolution_error_cb;
2347 objt_server(tmprequester->requester)->dns_requester = tmprequester;
2348 }
2349 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002350 case OBJ_TYPE_SRVRQ:
2351 /* some parameters should be set only if the resolution is brand new */
2352 if (new_resolution) {
2353 tmpresolution->query_type = DNS_RTYPE_SRV;
2354 tmpresolution->hostname_dn = objt_dns_srvrq(tmprequester->requester)->hostname_dn;
2355 tmpresolution->hostname_dn_len = objt_dns_srvrq(tmprequester->requester)->hostname_dn_len;
2356 }
2357
2358 /* update requester as well, only if we just allocated it */
2359 objt_dns_srvrq(tmprequester->requester)->resolution = tmpresolution;
2360 if (!resolution) {
2361 tmprequester->requester_cb = snr_resolution_cb;
2362 tmprequester->requester_error_cb = snr_resolution_error_cb;
2363 objt_dns_srvrq(tmprequester->requester)->dns_requester = tmprequester;
2364 }
2365 break;
2366
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002367 case OBJ_TYPE_NONE:
2368 default:
2369 free(tmprequester);
2370 return -1;
2371 }
2372
2373 /* update some parameters only if this is a brand new resolution */
2374 if (new_resolution) {
2375 /* move the resolution to the requesters' wait queue */
2376 LIST_DEL(&tmpresolution->list);
2377 LIST_ADDQ(&resolvers->resolution.wait, &tmpresolution->list);
2378
2379 tmpresolution->status = RSLV_STATUS_NONE;
2380 tmpresolution->step = RSLV_STEP_NONE;
2381 tmpresolution->revision = 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002382 LIST_INIT(&tmpresolution->response.answer_list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002383 }
2384
2385 /* add the requester to the resolution's wait queue */
2386 if (resolution)
2387 LIST_DEL(&tmprequester->list);
2388 LIST_ADDQ(&tmpresolution->requester.wait, &tmprequester->list);
2389
2390 return 0;
2391}
2392
2393/*
2394 * pick up an available resolution from the different resolution list associated to a resolvers section,
2395 * in this order:
2396 * 1. check in resolution.curr for the same hostname and query_type
2397 * 2. check in resolution.wait for the same hostname and query_type
2398 * 3. take an available resolution from resolution.pool
2399 *
2400 * return an available resolution, NULL if none found.
2401 */
2402struct dns_resolution *dns_resolution_list_get(struct dns_resolvers *resolvers, char *hostname_dn, int query_type)
2403{
2404 struct dns_resolution *resolution, *tmpresolution;
2405 struct dns_requester *requester;
2406
Olivier Houchard8da5f982017-08-04 18:35:36 +02002407 if (hostname_dn) {
2408 /* search for same hostname and query type in resolution.curr */
2409 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.curr, list) {
2410 requester = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002411
Olivier Houchard8da5f982017-08-04 18:35:36 +02002412 if (!LIST_ISEMPTY(&resolution->requester.wait))
2413 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2414 else if (!LIST_ISEMPTY(&resolution->requester.curr))
2415 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002416
Olivier Houchard8da5f982017-08-04 18:35:36 +02002417 if (!requester)
2418 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002419
Olivier Houchard8da5f982017-08-04 18:35:36 +02002420 if ((query_type == requester->prefered_query_type) &&
2421 (resolution->hostname_dn &&
2422 strcmp(hostname_dn, resolution->hostname_dn) == 0)) {
2423 return resolution;
2424 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002425 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002426
Olivier Houchard8da5f982017-08-04 18:35:36 +02002427 /* search for same hostname and query type in resolution.wait */
2428 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.wait, list) {
2429 requester = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002430
Olivier Houchard8da5f982017-08-04 18:35:36 +02002431 if (!LIST_ISEMPTY(&resolution->requester.wait))
2432 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2433 else if (!LIST_ISEMPTY(&resolution->requester.curr))
2434 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002435
Olivier Houchard8da5f982017-08-04 18:35:36 +02002436 if (!requester)
2437 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002438
Olivier Houchard8da5f982017-08-04 18:35:36 +02002439 if ((query_type == requester->prefered_query_type) &&
2440 (resolution->hostname_dn &&
2441 strcmp(hostname_dn, resolution->hostname_dn) == 0)) {
2442 return resolution;
2443 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002444 }
2445 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002446 /* take the first one (hopefully) from the pool */
2447 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.pool, list) {
2448 if (LIST_ISEMPTY(&resolution->requester.wait)) {
2449 return resolution;
2450 }
2451 }
2452
2453 return NULL;
2454}
2455
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002456/* This function allocates memory for a DNS resolution structure.
2457 * It's up to the caller to set the parameters
2458 * Returns a pointer to the structure resolution or NULL if memory could
2459 * not be allocated.
2460 */
2461struct dns_resolution *dns_alloc_resolution(void)
2462{
2463 struct dns_resolution *resolution = NULL;
Baptiste Assmann729c9012017-05-22 15:13:10 +02002464 char *buffer = NULL;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002465
2466 resolution = calloc(1, sizeof(*resolution));
Baptiste Assmann729c9012017-05-22 15:13:10 +02002467 buffer = calloc(1, global.tune.bufsize);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002468
Baptiste Assmann729c9012017-05-22 15:13:10 +02002469 if (!resolution || !buffer) {
2470 free(buffer);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002471 free(resolution);
2472 return NULL;
2473 }
2474
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002475 LIST_INIT(&resolution->requester.wait);
2476 LIST_INIT(&resolution->requester.curr);
Baptiste Assmann729c9012017-05-22 15:13:10 +02002477
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002478 return resolution;
2479}
2480
2481/* This function free the memory allocated to a DNS resolution */
2482void dns_free_resolution(struct dns_resolution *resolution)
2483{
2484 free(resolution);
2485
2486 return;
2487}
2488
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002489/* this function free a resolution from its requester(s) and move it back to the pool */
2490void dns_resolution_free(struct dns_resolvers *resolvers, struct dns_resolution *resolution)
2491{
2492 struct dns_requester *requester, *tmprequester;
2493
2494 /* clean up configuration */
2495 dns_reset_resolution(resolution);
2496 resolution->hostname_dn = NULL;
2497 resolution->hostname_dn_len = 0;
2498
2499 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.wait, list) {
2500 LIST_DEL(&requester->list);
2501 }
2502 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
2503 LIST_DEL(&requester->list);
2504 }
2505
2506 LIST_DEL(&resolution->list);
2507 LIST_ADDQ(&resolvers->resolution.pool, &resolution->list);
2508
2509 return;
2510}
2511
2512/*
2513 * this function remove a requester from a resolution
2514 * and takes care of all the consequences.
2515 * It also cleans up some parameters from the requester
2516 */
2517void dns_rm_requester_from_resolution(struct dns_requester *requester, struct dns_resolution *resolution)
2518{
2519 char *hostname_dn;
2520 struct dns_requester *tmprequester;
2521
2522 /* resolution is still used by other requesters, we need to move
2523 * some pointers to an other requester if needed
2524 */
2525 switch (obj_type(requester->requester)) {
2526 case OBJ_TYPE_SERVER:
2527 hostname_dn = objt_server(requester->requester)->hostname_dn;
2528 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002529 case OBJ_TYPE_SRVRQ:
2530 hostname_dn = objt_dns_srvrq(requester->requester)->hostname_dn;
2531 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002532 case OBJ_TYPE_NONE:
2533 default:
2534 hostname_dn = NULL;
2535 break;
2536 }
2537
2538 if (resolution->hostname_dn != hostname_dn)
2539 return;
2540
2541 /* First, we need to find this other requester */
2542 tmprequester = NULL;
2543 list_for_each_entry(tmprequester, &resolution->requester.wait, list) {
2544 if (tmprequester != requester)
2545 break;
2546 }
2547 if (!tmprequester) {
2548 /* if we can't find it in wait queue, let's get one in run queue */
2549 list_for_each_entry(tmprequester, &resolution->requester.curr, list) {
2550 if (tmprequester != requester)
2551 break;
2552 }
2553 }
2554
2555 /* move hostname_dn related pointers to the next requester */
2556 switch (obj_type(tmprequester->requester)) {
2557 case OBJ_TYPE_SERVER:
2558 resolution->hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2559 resolution->hostname_dn_len = objt_server(tmprequester->requester)->hostname_dn_len;
2560 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002561 case OBJ_TYPE_SRVRQ:
2562 resolution->hostname_dn = objt_dns_srvrq(tmprequester->requester)->hostname_dn;
2563 resolution->hostname_dn_len = objt_dns_srvrq(tmprequester->requester)->hostname_dn_len;
2564 break;
2565
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002566 case OBJ_TYPE_NONE:
2567 default:
2568 ;;
2569 }
2570
2571
2572 /* clean up the requester */
2573 LIST_DEL(&requester->list);
2574 switch (obj_type(requester->requester)) {
2575 case OBJ_TYPE_SERVER:
2576 objt_server(requester->requester)->resolution = NULL;
2577 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002578 case OBJ_TYPE_SRVRQ:
2579 objt_dns_srvrq(requester->requester)->resolution = NULL;
2580 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002581 case OBJ_TYPE_NONE:
2582 default:
2583 ;;
2584 }
2585}
2586
Willy Tarreau777b5602016-12-16 18:06:26 +01002587/* This function dumps counters from all resolvers section and associated name
2588 * servers. It returns 0 if the output buffer is full and it needs to be called
2589 * again, otherwise non-zero. It may limit itself to the resolver pointed to by
2590 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002591 */
2592static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2593{
2594 struct stream_interface *si = appctx->owner;
2595 struct dns_resolvers *presolvers;
2596 struct dns_nameserver *pnameserver;
2597
2598 chunk_reset(&trash);
2599
2600 switch (appctx->st2) {
2601 case STAT_ST_INIT:
2602 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2603 /* fall through */
2604
2605 case STAT_ST_LIST:
2606 if (LIST_ISEMPTY(&dns_resolvers)) {
2607 chunk_appendf(&trash, "No resolvers found\n");
2608 }
2609 else {
2610 list_for_each_entry(presolvers, &dns_resolvers, list) {
Willy Tarreau777b5602016-12-16 18:06:26 +01002611 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != presolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002612 continue;
2613
2614 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
2615 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
2616 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
2617 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
2618 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
2619 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
2620 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
2621 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
2622 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
2623 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
2624 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
2625 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
2626 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
2627 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
2628 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
2629 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
2630 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
2631 }
2632 }
2633 }
2634
2635 /* display response */
2636 if (bi_putchk(si_ic(si), &trash) == -1) {
2637 /* let's try again later from this session. We add ourselves into
2638 * this session's users so that it can remove us upon termination.
2639 */
2640 si->flags |= SI_FL_WAIT_ROOM;
2641 return 0;
2642 }
2643
2644 appctx->st2 = STAT_ST_FIN;
2645 /* fall through */
2646
2647 default:
2648 appctx->st2 = STAT_ST_FIN;
2649 return 1;
2650 }
2651}
2652
2653/* register cli keywords */
2654static struct cli_kw_list cli_kws = {{ },{
2655 { { "show", "stat", "resolvers", NULL }, "show stat resolvers [id]: dumps counters from all resolvers section and\n"
2656 " associated name servers",
2657 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2658 {{},}
2659}};
2660
2661
2662__attribute__((constructor))
2663static void __dns_init(void)
2664{
2665 cli_register_kw(&cli_kws);
2666}
2667