blob: 52e02026ffcd3b998748a22a65f52e379e75d8a7 [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 Assmann9d8dbbc2017-08-18 23:35:08 +0200394 int max_answer_records = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200395 unsigned short query_id;
396 struct eb32_node *eb;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200397 struct lru64 *lru = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200398 struct dns_requester *requester = NULL, *tmprequester = NULL;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200399 struct dns_answer_item *item1, *item2 = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200400
401 fd = dgram->t.sock.fd;
402
403 /* check if ready for reading */
404 if (!fd_recv_ready(fd))
405 return;
406
407 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200408 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200409 return;
410
411 resolvers = nameserver->resolvers;
412
413 /* process all pending input messages */
414 while (1) {
Olivier Houchard8da5f982017-08-04 18:35:36 +0200415 int removed_reso = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200416 /* read message received */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200417 memset(buf, '\0', resolvers->accepted_payload_size + 1);
418 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200419 /* FIXME : for now we consider EAGAIN only */
420 fd_cant_recv(fd);
421 break;
422 }
423
424 /* message too big */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200425 if (buflen > resolvers->accepted_payload_size) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200426 nameserver->counters.too_big += 1;
427 continue;
428 }
429
430 /* initializing variables */
431 bufend = buf + buflen; /* pointer to mark the end of the buffer */
432
433 /* read the query id from the packet (16 bits) */
434 if (buf + 2 > bufend) {
435 nameserver->counters.invalid += 1;
436 continue;
437 }
438 query_id = dns_response_get_query_id(buf);
439
440 /* search the query_id in the pending resolution tree */
Baptiste Assmann01daef32015-09-02 22:05:24 +0200441 eb = eb32_lookup(&resolvers->query_ids, query_id);
442 if (eb == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200443 /* unknown query id means an outdated response and can be safely ignored */
444 nameserver->counters.outdated += 1;
445 continue;
446 }
447
448 /* known query id means a resolution in prgress */
449 resolution = eb32_entry(eb, struct dns_resolution, qid);
450
451 if (!resolution) {
452 nameserver->counters.outdated += 1;
453 continue;
454 }
455
456 /* number of responses received */
457 resolution->nb_responses += 1;
458
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200459
460 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
461 dns_resp = dns_validate_dns_response(buf, bufend, resolution, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200462
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200463 switch (dns_resp) {
464 case DNS_RESP_VALID:
465 need_resend = 0;
466 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200467
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200468 case DNS_RESP_INVALID:
469 case DNS_RESP_QUERY_COUNT_ERROR:
470 case DNS_RESP_WRONG_NAME:
471 if (resolution->status != RSLV_STATUS_INVALID) {
472 resolution->status = RSLV_STATUS_INVALID;
473 resolution->last_status_change = now_ms;
474 }
475 nameserver->counters.invalid += 1;
476 need_resend = 0;
477 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200478
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200479 case DNS_RESP_ANCOUNT_ZERO:
480 if (resolution->status != RSLV_STATUS_OTHER) {
481 resolution->status = RSLV_STATUS_OTHER;
482 resolution->last_status_change = now_ms;
483 }
484 nameserver->counters.any_err += 1;
485 need_resend = 1;
486 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200487
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200488 case DNS_RESP_NX_DOMAIN:
489 if (resolution->status != RSLV_STATUS_NX) {
490 resolution->status = RSLV_STATUS_NX;
491 resolution->last_status_change = now_ms;
492 }
493 nameserver->counters.nx += 1;
494 need_resend = 0;
495 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200496
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200497 case DNS_RESP_REFUSED:
498 if (resolution->status != RSLV_STATUS_REFUSED) {
499 resolution->status = RSLV_STATUS_REFUSED;
500 resolution->last_status_change = now_ms;
501 }
502 nameserver->counters.refused += 1;
503 need_resend = 0;
504 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200505
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200506 case DNS_RESP_CNAME_ERROR:
507 if (resolution->status != RSLV_STATUS_OTHER) {
508 resolution->status = RSLV_STATUS_OTHER;
509 resolution->last_status_change = now_ms;
510 }
511 nameserver->counters.cname_error += 1;
512 need_resend = 1;
513 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200514
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200515 case DNS_RESP_TRUNCATED:
516 if (resolution->status != RSLV_STATUS_OTHER) {
517 resolution->status = RSLV_STATUS_OTHER;
518 resolution->last_status_change = now_ms;
519 }
520 nameserver->counters.truncated += 1;
521 need_resend = 1;
522 break;
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200523
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200524 case DNS_RESP_NO_EXPECTED_RECORD:
525 if (resolution->status != RSLV_STATUS_OTHER) {
526 resolution->status = RSLV_STATUS_OTHER;
527 resolution->last_status_change = now_ms;
528 }
529 nameserver->counters.other += 1;
530 need_resend = 1;
531 break;
532
533 case DNS_RESP_ERROR:
534 case DNS_RESP_INTERNAL:
535 if (resolution->status != RSLV_STATUS_OTHER) {
536 resolution->status = RSLV_STATUS_OTHER;
537 resolution->last_status_change = now_ms;
538 }
539 nameserver->counters.other += 1;
540 need_resend = 1;
541 break;
542 }
543
Olivier Houchard8da5f982017-08-04 18:35:36 +0200544 /* Check for any obsolete record, also identify any SRV request, and try to find a corresponding server */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200545 list_for_each_entry_safe(item1, item2, &resolution->response.answer_list,
546 list) {
547 if (item1->last_seen + nameserver->resolvers->hold.obsolete / 1000 < now.tv_sec) {
548 LIST_DEL(&item1->list);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200549 if (item1->type == DNS_RTYPE_SRV && !LIST_ISEMPTY(&resolution->requester.curr)) {
550 struct dns_srvrq *srvrq;
551
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200552 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
553 srvrq = objt_dns_srvrq(requester->requester);
554 /* We're removing an obsolete entry, remove any associated server */
555 if (srvrq) {
556 struct server *srv;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200557
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200558 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
559 if (srv->srvrq == srvrq &&
560 item1->data_len ==
561 srv->hostname_dn_len &&
562 !memcmp(srv->hostname_dn, item1->target, item1->data_len) &&
563 srv->svc_port == item1->port) {
564 snr_update_srv_status(srv, 1);
565 free(srv->hostname);
566 srv->hostname = NULL;
567 srv->hostname_dn_len = 0;
568 free(srv->hostname_dn);
569 srv->hostname_dn = NULL;
Baptiste Assmann747359e2017-08-14 10:37:46 +0200570 srv_free_from_resolution(srv);
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200571 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200572 }
573 }
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200574 } /* end of list_for_each(requester) */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200575 }
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) {
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200580 struct server *srv = NULL;
581 struct dns_srvrq *srvrq = NULL;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200582
583 if (LIST_ISEMPTY(&resolution->requester.curr))
584 continue;
585
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200586 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, 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) {
Olivier Houchard8da5f982017-08-04 18:35:36 +0200617 char weight[9];
618
Baptiste Assmann7d2a1262017-08-14 16:38:29 +0200619 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;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200629 snprintf(weight, sizeof(weight),
630 "%d", item1->weight);
631 server_parse_weight_change_request(srv, weight);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200632 }
633
Olivier Houchard8da5f982017-08-04 18:35:36 +0200634 }
635 }
636 /* If not, try to find a server that is down */
637 if (!srv) {
638 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
639
640 if (srv->srvrq == srvrq &&
641 !srv->hostname_dn)
642 break;
643 }
644 if (srv) {
645 char weight[9];
646
647 char hostname[DNS_MAX_NAME_SIZE];
648
649 if (item1->data_len > DNS_MAX_NAME_SIZE)
650 continue;
651 dns_dn_label_to_str(item1->target, hostname, item1->data_len);
652 update_server_fqdn(srv, hostname, "SRV record");
653 srv->svc_port = item1->port;
654 srv->flags &= ~SRV_F_MAPPORTS;
655 if ((srv->check.state & CHK_ST_CONFIGURED) && !(srv->flags & SRV_F_CHECKPORT))
656 srv->check.port = item1->port;
657 snprintf(weight, sizeof(weight),
658 "%d", item1->weight);
659 server_parse_weight_change_request(srv, weight);
660 }
661
662 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200663 }
664 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200665 if (removed_reso)
666 goto next_packet;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200667
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200668 /* some error codes trigger a re-send of the query, but switching the
669 * query type.
670 * This is the case for the following error codes:
671 * DNS_RESP_ANCOUNT_ZERO
672 * DNS_RESP_TRUNCATED
673 * DNS_RESP_ERROR
674 * DNS_RESP_INTERNAL
675 * DNS_RESP_NO_EXPECTED_RECORD
676 * DNS_RESP_CNAME_ERROR
677 */
678 if (need_resend) {
679 int family_prio;
680 int res_preferred_afinet, res_preferred_afinet6;
681
682 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
683 switch (obj_type(requester->requester)) {
684 case OBJ_TYPE_SERVER:
685 family_prio = objt_server(requester->requester)->dns_opts.family_prio;
686 break;
687 case OBJ_TYPE_NONE:
688 default:
689 family_prio = AF_INET6;
690 }
691 res_preferred_afinet = family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
692 res_preferred_afinet6 = family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
693 if ((res_preferred_afinet || res_preferred_afinet6)
694 || (resolution->try > 0)) {
695 /* let's change the query type */
696 if (res_preferred_afinet6) {
697 /* fallback from AAAA to A */
698 resolution->query_type = DNS_RTYPE_A;
699 }
700 else if (res_preferred_afinet) {
701 /* fallback from A to AAAA */
702 resolution->query_type = DNS_RTYPE_AAAA;
703 }
704 else {
705 resolution->try -= 1;
706 if (family_prio == AF_INET) {
707 resolution->query_type = DNS_RTYPE_A;
708 } else {
709 resolution->query_type = DNS_RTYPE_AAAA;
710 }
711 }
712
713 dns_send_query(resolution);
714 /*
715 * move the resolution to the last element of the FIFO queue
716 * and update timeout wakeup based on the new first entry
717 */
718 if (dns_check_resolution_queue(resolvers) > 1) {
719 /* second resolution becomes first one */
720 LIST_DEL(&resolution->list);
721 /* ex first resolution goes to the end of the queue */
722 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
723 }
724
725 dns_update_resolvers_timeout(resolvers);
726 goto next_packet;
727 }
728
729 /* if we're there, this means that we already ran out of chances to re-send
730 * the query */
731 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
732 requester->requester_error_cb(requester, dns_resp);
733 }
734 goto next_packet;
735 }
736
737 /* now processing those error codes only:
738 * DNS_RESP_NX_DOMAIN
739 * DNS_RESP_REFUSED
740 */
741 if (dns_resp != DNS_RESP_VALID) {
742 /* now parse list of requesters currently waiting for this resolution */
743 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
744 requester->requester_error_cb(requester, dns_resp);
745
746 /* we can move the requester the wait queue */
747 LIST_DEL(&requester->list);
748 LIST_ADDQ(&resolution->requester.wait, &requester->list);
749 }
750 goto next_packet;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200751 }
752
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 /* Now let's check the query's dname corresponds to the one we sent.
754 * We can check only the first query of the list. We send one query at a time
755 * so we get one query in the response */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200756 query = LIST_NEXT(&resolution->response.query_list, struct dns_query_item *, list);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200757 if (!resolution->hostname_dn)
758 abort();
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200759 if (query && memcmp(query->name, resolution->hostname_dn, resolution->hostname_dn_len) != 0) {
760 nameserver->counters.other += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200761 /* now parse list of requesters currently waiting for this resolution */
762 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
763 requester->requester_error_cb(requester, DNS_RESP_WRONG_NAME);
764 /* we can move the requester the wait queue */
765 LIST_DEL(&requester->list);
766 LIST_ADDQ(&resolution->requester.wait, &requester->list);
767 }
768 goto next_packet;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200769 }
770
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200771 /* no errors, we can save the response in the cache */
772 if (dns_lru_tree) {
773 unsigned long long seed = 1;
774 struct chunk *buf = get_trash_chunk();
775 struct chunk *tmp = NULL;
776
777 chunk_reset(buf);
778 tmp = dns_cache_key(resolution->query_type, resolution->hostname_dn,
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200779 resolution->hostname_dn_len, buf);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200780 if (!tmp) {
781 nameserver->counters.other += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200782 /* now parse list of requesters currently waiting for this resolution */
783 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
784 requester->requester_error_cb(requester, DNS_RESP_ERROR);
785 /* we can move the requester the wait queue */
786 LIST_DEL(&requester->list);
787 LIST_ADDQ(&resolution->requester.wait, &requester->list);
788 }
789 goto next_packet;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200790 }
791
792 lru = lru64_get(XXH64(buf->str, buf->len, seed),
793 dns_lru_tree, nameserver->resolvers, 1);
794
795 lru64_commit(lru, resolution, nameserver->resolvers, 1, NULL);
796 }
797
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200798 if (resolution->status != RSLV_STATUS_VALID) {
799 resolution->status = RSLV_STATUS_VALID;
800 resolution->last_status_change = now_ms;
801 }
802
Baptiste Assmann37bb3722015-08-07 10:18:32 +0200803 nameserver->counters.valid += 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200804 /* now parse list of requesters currently waiting for this resolution */
805 tmpnameserver = nameserver;
806 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
807 requester->requester_cb(requester, tmpnameserver);
808 /* we can move the requester the wait queue */
809 LIST_DEL(&requester->list);
810 LIST_ADDQ(&resolution->requester.wait, &requester->list);
811 /* first response is managed by the server, others are from the cache */
812 tmpnameserver = NULL;
813 }
814
815 next_packet:
816 /* resolution may be NULL when we receive an ICMP unreachable packet */
817 if (resolution && LIST_ISEMPTY(&resolution->requester.curr)) {
818 /* move the resolution into the wait queue */
819 LIST_DEL(&resolution->list);
820 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
821 /* update last resolution date and time */
822 resolution->last_resolution = now_ms;
823 /* reset current status flag */
824 resolution->step = RSLV_STEP_NONE;
825 /* reset values */
826 dns_reset_resolution(resolution);
827 }
828
829 } // end of while "packets" loop
830
831 dns_update_resolvers_timeout(nameserver->resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200832}
833
834/*
835 * function called when a resolvers network socket is ready to send data
836 * It performs the following actions:
837 */
838void dns_resolve_send(struct dgram_conn *dgram)
839{
840 int fd;
841 struct dns_nameserver *nameserver;
842 struct dns_resolvers *resolvers;
843 struct dns_resolution *resolution;
844
845 fd = dgram->t.sock.fd;
846
847 /* check if ready for sending */
848 if (!fd_send_ready(fd))
849 return;
850
851 /* we don't want/need to be waked up any more for sending */
852 fd_stop_send(fd);
853
854 /* no need to go further if we can't retrieve the nameserver */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200855 if ((nameserver = dgram->owner) == NULL)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200856 return;
857
858 resolvers = nameserver->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200859 resolution = LIST_NEXT(&resolvers->resolution.curr, struct dns_resolution *, list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200860
861 dns_send_query(resolution);
862 dns_update_resolvers_timeout(resolvers);
863}
864
865/*
866 * forge and send a DNS query to resolvers associated to a resolution
867 * It performs the following actions:
868 * returns:
869 * 0 in case of error or safe ignorance
870 * 1 if no error
871 */
872int dns_send_query(struct dns_resolution *resolution)
873{
Baptiste Assmann42746372017-05-03 12:12:02 +0200874 struct dns_resolvers *resolvers = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200875 struct dns_nameserver *nameserver;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200876 struct dns_requester *requester = NULL;
Erwan Velu5457eb42015-10-15 15:07:26 +0200877 int ret, bufsize, fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200878
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200879 /* nothing to do */
880 if (LIST_ISEMPTY(&resolution->requester.curr))
881 return 0;
882
883 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
884
885 switch (obj_type(requester->requester)) {
886 case OBJ_TYPE_SERVER:
887 resolvers = objt_server(requester->requester)->resolvers;
888 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200889 case OBJ_TYPE_SRVRQ:
890 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
891 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200892 case OBJ_TYPE_NONE:
893 default:
894 return 0;
895 }
Baptiste Assmann42746372017-05-03 12:12:02 +0200896
897 if (!resolvers)
898 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200899
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200900 bufsize = dns_build_query(resolution->query_id, resolution->query_type, resolvers->accepted_payload_size,
901 resolution->hostname_dn, resolution->hostname_dn_len, trash.str, trash.size);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200902
903 if (bufsize == -1)
904 return 0;
905
906 list_for_each_entry(nameserver, &resolvers->nameserver_list, list) {
907 fd = nameserver->dgram->t.sock.fd;
908 errno = 0;
909
910 ret = send(fd, trash.str, bufsize, 0);
911
912 if (ret > 0)
913 nameserver->counters.sent += 1;
914
915 if (ret == 0 || errno == EAGAIN) {
916 /* nothing written, let's update the poller that we wanted to send
917 * but we were not able to */
918 fd_want_send(fd);
919 fd_cant_send(fd);
920 }
921 }
922
923 /* update resolution */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200924 resolution->nb_responses = 0;
925 resolution->last_sent_packet = now_ms;
926
927 return 1;
928}
929
930/*
931 * update a resolvers' task timeout for next wake up
932 */
933void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
934{
935 struct dns_resolution *resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200936 struct dns_requester *requester;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200937
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200938 if ((LIST_ISEMPTY(&resolvers->resolution.curr)) && (LIST_ISEMPTY(&resolvers->resolution.wait))) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200939 resolvers->t->expire = TICK_ETERNITY;
940 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200941 else if (!LIST_ISEMPTY(&resolvers->resolution.curr)) {
942 resolution = LIST_NEXT(&resolvers->resolution.curr, struct dns_resolution *, list);
943 if (!resolvers->t->expire || tick_is_le(resolvers->t->expire, tick_add(resolution->last_sent_packet, resolvers->timeout.retry))) {
944 resolvers->t->expire = tick_add(resolution->last_sent_packet, resolvers->timeout.retry);
945 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200946 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200947 else if (!LIST_ISEMPTY(&resolvers->resolution.wait)) {
948 int valid_period, inter, need_wakeup;
949 struct dns_resolution *res_back;
950 need_wakeup = 0;
951 list_for_each_entry_safe(resolution, res_back, &resolvers->resolution.wait, list) {
952 valid_period = 0;
953 inter = 0;
954
955 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
956
957 switch (obj_type(requester->requester)) {
958 case OBJ_TYPE_SERVER:
959 valid_period = objt_server(requester->requester)->check.inter;
960 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200961 case OBJ_TYPE_SRVRQ:
962 valid_period = objt_dns_srvrq(requester->requester)->inter;
963 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200964 case OBJ_TYPE_NONE:
965 default:
966 continue;
967 }
968
969 if (resolvers->hold.valid < valid_period)
970 inter = resolvers->hold.valid;
971 else
972 inter = valid_period;
973
974 if (tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms)) {
975 switch (obj_type(requester->requester)) {
976 case OBJ_TYPE_SERVER:
977 dns_trigger_resolution(objt_server(requester->requester)->resolution);
978 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200979 case OBJ_TYPE_SRVRQ:
980 dns_trigger_resolution(objt_dns_srvrq(requester->requester)->resolution);
981 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200982 case OBJ_TYPE_NONE:
983 default:
984 ;;
985 }
986 }
987 else {
988 need_wakeup = 1;
989 }
990 }
991 /* in such case, we wake up in 1s */
992 if (need_wakeup) {
993 int r = 1000;
994
995 resolution = LIST_NEXT(&resolvers->resolution.wait, struct dns_resolution *, list);
996 if (tick_is_le(resolvers->t->expire, tick_add(now_ms, r)))
997 resolvers->t->expire = tick_add(now_ms, r);
998 resolvers->t->expire = tick_add(now_ms, 1000);
999 }
1000 }
1001
1002 task_queue(resolvers->t);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001003}
1004
1005/*
1006 * Analyse, re-build and copy the name <name> from the DNS response packet <buffer>.
1007 * <name> must point to the 'data_len' information or pointer 'c0' for compressed data.
1008 * The result is copied into <dest>, ensuring we don't overflow using <dest_len>
1009 * Returns the number of bytes the caller can move forward. If 0 it means an error occured
1010 * while parsing the name.
1011 * <offset> is the number of bytes the caller could move forward.
1012 */
1013int dns_read_name(unsigned char *buffer, unsigned char *bufend, unsigned char *name, char *destination, int dest_len, int *offset)
1014{
1015 int nb_bytes = 0, n = 0;
1016 int label_len;
1017 unsigned char *reader = name;
1018 char *dest = destination;
1019
1020 while (1) {
1021 /* name compression is in use */
1022 if ((*reader & 0xc0) == 0xc0) {
1023 /* a pointer must point BEFORE current position */
1024 if ((buffer + reader[1]) > reader) {
1025 goto out_error;
1026 }
1027
1028 n = dns_read_name(buffer, bufend, buffer + reader[1], dest, dest_len - nb_bytes, offset);
1029 if (n == 0)
1030 goto out_error;
1031
1032 dest += n;
1033 nb_bytes += n;
1034 goto out;
1035 }
1036
1037 label_len = *reader;
1038 if (label_len == 0)
1039 goto out;
1040 /* Check if:
1041 * - we won't read outside the buffer
1042 * - there is enough place in the destination
1043 */
1044 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
1045 goto out_error;
1046
1047 /* +1 to take label len + label string */
1048 label_len += 1;
1049
1050 memcpy(dest, reader, label_len);
1051
1052 dest += label_len;
1053 nb_bytes += label_len;
1054 reader += label_len;
1055 }
1056
1057 out:
1058 /* offset computation:
1059 * parse from <name> until finding either NULL or a pointer "c0xx"
1060 */
1061 reader = name;
1062 *offset = 0;
1063 while (reader < bufend) {
1064 if ((reader[0] & 0xc0) == 0xc0) {
1065 *offset += 2;
1066 break;
1067 }
1068 else if (*reader == 0) {
1069 *offset += 1;
1070 break;
1071 }
1072 *offset += 1;
1073 ++reader;
1074 }
1075
1076 return nb_bytes;
1077
1078 out_error:
1079 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001080}
1081
1082/*
1083 * Function to validate that the buffer DNS response provided in <resp> and
1084 * finishing before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001085 *
Baptiste Assmann729c9012017-05-22 15:13:10 +02001086 * The result is stored in <resolution>' response, buf_response, response_query_records
1087 * and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001088 *
1089 * This function returns one of the DNS_RESP_* code to indicate the type of
1090 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001091 */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +02001092int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001093{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001094 unsigned char *reader;
1095 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001096 int len, flags, offset;
1097 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +02001098 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001099 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001100 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +02001101 struct dns_response_packet *dns_p;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001102 int found = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001103 int i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001104
1105 reader = resp;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001106 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001107 previous_dname = NULL;
Baptiste Assmann251abb92017-08-11 09:58:27 +02001108 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109
Baptiste Assmann729c9012017-05-22 15:13:10 +02001110 /* initialization of response buffer and structure */
1111 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001112
1113 /* query id */
1114 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001115 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001116 dns_p->header.id = reader[0] * 256 + reader[1];
1117 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001118
1119 /*
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001120 * flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001121 * First byte contains:
1122 * - response flag (1 bit)
1123 * - opcode (4 bits)
1124 * - authoritative (1 bit)
1125 * - truncated (1 bit)
1126 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001127 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001128 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001129 return DNS_RESP_INVALID;
1130
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001131 flags = reader[0] * 256 + reader[1];
1132
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001133 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
1134 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001136 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137 return DNS_RESP_REFUSED;
1138
1139 return DNS_RESP_ERROR;
1140 }
1141
Baptiste Assmann3440f0d2015-09-02 22:08:38 +02001142 /* move forward 2 bytes for flags */
1143 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001144
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001145 /* 2 bytes for question 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.qdcount = reader[0] * 256 + reader[1];
1149 /* (for now) we send one query only, so we expect only one in the response too */
1150 if (dns_p->header.qdcount != 1)
1151 return DNS_RESP_QUERY_COUNT_ERROR;
1152 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001153 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001154 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001155
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001156 /* 2 bytes for answer count */
1157 if (reader + 2 >= bufend)
1158 return DNS_RESP_INVALID;
1159 dns_p->header.ancount = reader[0] * 256 + reader[1];
1160 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001161 return DNS_RESP_ANCOUNT_ZERO;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001162 /* check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +02001163 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001164 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001165 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001166
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001167 /* 2 bytes authority count */
1168 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001169 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001170 dns_p->header.nscount = reader[0] * 256 + reader[1];
1171 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001172
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001173 /* 2 bytes additional count */
1174 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001175 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001176 dns_p->header.arcount = reader[0] * 256 + reader[1];
1177 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001178
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001179 /* parsing dns queries */
1180 LIST_INIT(&dns_p->query_list);
1181 for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) {
1182 /* use next pre-allocated dns_query_item after ensuring there is
1183 * still one available.
1184 * It's then added to our packet query list.
1185 */
1186 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
1187 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +02001188 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001189 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001190
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001191 /* name is a NULL terminated string in our case, since we have
1192 * one query per response and the first one can't be compressed
1193 * (using the 0x0c format)
1194 */
1195 offset = 0;
1196 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001197
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001198 if (len == 0)
1199 return DNS_RESP_INVALID;
1200
1201 reader += offset;
1202 previous_dname = dns_query->name;
1203
1204 /* move forward 2 bytes for question type */
1205 if (reader + 2 >= bufend)
1206 return DNS_RESP_INVALID;
1207 dns_query->type = reader[0] * 256 + reader[1];
1208 reader += 2;
1209
1210 /* move forward 2 bytes for question class */
1211 if (reader + 2 >= bufend)
1212 return DNS_RESP_INVALID;
1213 dns_query->class = reader[0] * 256 + reader[1];
1214 reader += 2;
1215 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001216
Baptiste Assmann251abb92017-08-11 09:58:27 +02001217 /* TRUNCATED flag must be checked after we could read the query type
1218 * because a TRUNCATED SRV query type response can still be exploited
1219 */
1220 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
1221 return DNS_RESP_TRUNCATED;
1222
Baptiste Assmann325137d2015-04-13 23:40:55 +02001223 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +02001224 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001225 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001226 if (reader >= bufend)
1227 return DNS_RESP_INVALID;
1228
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001229 dns_answer_record = pool_alloc2(dns_answer_item_pool);
1230 if (dns_answer_record == NULL)
1231 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001232
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001233 offset = 0;
1234 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001235
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001236 if (len == 0) {
1237 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001238 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001239 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001240
1241 /* check if the current record dname is valid.
1242 * previous_dname points either to queried dname or last CNAME target
1243 */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +02001244 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001245 free_dns_answer_item(dns_answer_record);
1246 if (i == 0) {
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001247 /* first record, means a mismatch issue between queried dname
1248 * and dname found in the first record */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001249 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001250 } else {
1251 /* if not the first record, this means we have a CNAME resolution
1252 * error */
1253 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001254 }
1255
Baptiste Assmann325137d2015-04-13 23:40:55 +02001256 }
1257
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001258 memcpy(dns_answer_record->name, tmpname, len);
1259 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +02001260
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001261 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001262 if (reader >= bufend) {
1263 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001264 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001265 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001266
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001267 if (reader >= bufend) {
1268 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001269 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001270 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001271
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001272 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001273 if (reader + 2 > bufend) {
1274 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001275 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001276 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001277 dns_answer_record->type = reader[0] * 256 + reader[1];
1278 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001279
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001280 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001281 if (reader + 2 > bufend) {
1282 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001283 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001284 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001285 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001286 reader += 2;
1287
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001288 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001289 if (reader + 4 > bufend) {
1290 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001291 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001292 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001293 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
1294 + reader[2] * 256 + reader[3];
1295 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001296
1297 /* now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001298 if (reader + 2 > bufend) {
1299 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001300 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001301 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001302 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001303
1304 /* move forward 2 bytes for data len */
1305 reader += 2;
1306
1307 /* analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001308 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001309 case DNS_RTYPE_A:
1310 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001311 if (dns_answer_record->data_len != 4) {
1312 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001313 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001314 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001315 dns_answer_record->address.sa_family = AF_INET;
1316 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
1317 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001318 break;
1319
1320 case DNS_RTYPE_CNAME:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001321 /* check if this is the last record and update the caller about the status:
1322 * no IP could be found and last record was a CNAME. Could be triggered
1323 * by a wrong query type
1324 *
1325 * + 1 because dns_answer_record_id starts at 0 while number of answers
1326 * is an integer and starts at 1.
1327 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001328 if (i + 1 == dns_p->header.ancount) {
1329 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001330 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001331 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001332
1333 offset = 0;
1334 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
1335
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001336 if (len == 0) {
1337 free_dns_answer_item(dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001338 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001339 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001340
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001341 memcpy(dns_answer_record->target, tmpname, len);
1342 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001343
1344 previous_dname = dns_answer_record->target;
1345
Baptiste Assmann325137d2015-04-13 23:40:55 +02001346 break;
1347
Olivier Houchard8da5f982017-08-04 18:35:36 +02001348
1349 case DNS_RTYPE_SRV:
1350 /*
1351 * Answer must contain :
1352 * - 2 bytes for the priority
1353 * - 2 bytes for the weight
1354 * - 2 bytes for the port
1355 * - the target hostname
1356 */
1357 if (dns_answer_record->data_len <= 6) {
1358 free_dns_answer_item(dns_answer_record);
1359 return DNS_RESP_INVALID;
1360 }
1361 dns_answer_record->priority = readn16(reader);
1362 reader += sizeof(uint16_t);
1363 dns_answer_record->weight = readn16(reader);
1364 reader += sizeof(uint16_t);
1365 dns_answer_record->port = readn16(reader);
1366 reader += sizeof(uint16_t);
1367 offset = 0;
1368 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
1369 if (len == 0) {
1370 free_dns_answer_item(dns_answer_record);
1371 return DNS_RESP_INVALID;
1372 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001373 dns_answer_record->data_len = len;
1374 memcpy(dns_answer_record->target, tmpname, len);
1375 dns_answer_record->target[len] = 0;
1376 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001377 case DNS_RTYPE_AAAA:
1378 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001379 if (dns_answer_record->data_len != 16) {
1380 free_dns_answer_item(dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001381 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001382 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001383 dns_answer_record->address.sa_family = AF_INET6;
1384 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
1385 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001386 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001387
Baptiste Assmann325137d2015-04-13 23:40:55 +02001388 } /* switch (record type) */
1389
Baptiste Assmann69fce672017-05-04 08:37:45 +02001390 /* increment the counter for number of records saved into our local response */
1391 nb_saved_records += 1;
1392
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001393 /* move forward dns_answer_record->data_len for analyzing next record in the response */
Baptiste Assmann63a28112017-08-11 10:37:20 +02001394 if (dns_answer_record->type == DNS_RTYPE_SRV)
1395 reader += offset;
1396 else
1397 reader += dns_answer_record->data_len;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001398
1399 /* Lookup to see if we already had this entry */
1400
Olivier Houchard8da5f982017-08-04 18:35:36 +02001401 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001402 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
1403 if (tmp_record->type != dns_answer_record->type)
1404 continue;
1405 switch (tmp_record->type) {
1406 case DNS_RTYPE_A:
1407 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
1408 &((struct sockaddr_in *)&tmp_record->address)->sin_addr, sizeof(in_addr_t)))
1409 found = 1;
1410 break;
1411 case DNS_RTYPE_AAAA:
1412 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
1413 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr, sizeof(struct in6_addr)))
1414 found = 1;
1415 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001416 case DNS_RTYPE_SRV:
1417 if (dns_answer_record->data_len == tmp_record->data_len &&
1418 !memcmp(dns_answer_record->target,
1419 tmp_record->target, dns_answer_record->data_len) &&
1420 dns_answer_record->port == tmp_record->port) {
1421 tmp_record->weight = dns_answer_record->weight;
1422 found = 1;
1423 }
1424 break;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001425 default:
1426 break;
1427 }
1428 if (found == 1)
1429 break;
1430 }
1431 if (found == 1) {
1432 tmp_record->last_seen = now.tv_sec;
1433 free_dns_answer_item(dns_answer_record);
1434 } else {
1435 dns_answer_record->last_seen = now.tv_sec;
1436 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
1437 }
1438
Baptiste Assmann325137d2015-04-13 23:40:55 +02001439 } /* for i 0 to ancount */
1440
Baptiste Assmann96972bc2015-09-09 00:46:58 +02001441
Baptiste Assmann69fce672017-05-04 08:37:45 +02001442 /* save the number of records we really own */
1443 dns_p->header.ancount = nb_saved_records;
1444
Baptiste Assmann325137d2015-04-13 23:40:55 +02001445 return DNS_RESP_VALID;
1446}
1447
1448/*
1449 * search dn_name resolution in resp.
1450 * If existing IP not found, return the first IP matching family_priority,
1451 * otherwise, first ip found
1452 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001453 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +02001454 * For both cases above, dns_validate_dns_response is required
1455 * returns one of the DNS_UPD_* code
1456 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001457#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001458int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +02001459 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +01001460 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001461 void **newip, short *newip_sin_family,
1462 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001463{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001464 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +01001465 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001466 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001467 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001468 int currentip_sel;
1469 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001470 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001471
Baptiste Assmann42746372017-05-03 12:12:02 +02001472 family_priority = dns_opts->family_prio;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +02001473 *newip = newip4 = newip6 = NULL;
1474 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001475 *newip_sin_family = AF_UNSPEC;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001476 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001477
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001478 /* Select an IP regarding configuration preference.
1479 * Top priority is the prefered network ip version,
1480 * second priority is the prefered network.
1481 * the last priority is the currently used IP,
1482 *
1483 * For these three priorities, a score is calculated. The
1484 * weight are:
Baptistefc725902016-12-26 23:21:08 +01001485 * 8 - prefered netwok ip version.
1486 * 4 - prefered network.
1487 * 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 +01001488 * 1 - current ip.
1489 * The result with the biggest score is returned.
1490 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001491
1492 list_for_each_entry(record, &dns_p->answer_list, list) {
1493 void *ip;
1494 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001495
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001496 if (record->type == DNS_RTYPE_A) {
1497 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
1498 ip_type = AF_INET;
1499 } else if (record->type == DNS_RTYPE_AAAA) {
1500 ip_type = AF_INET6;
1501 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
1502 } else
1503 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001504 score = 0;
1505
1506 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001507 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +01001508 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001509
1510 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +02001511 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001512
1513 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001514 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001515 continue;
1516
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001517 if ((ip_type == AF_INET &&
1518 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001519 &dns_opts->pref_net[j].mask.in4,
1520 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001521 (ip_type == AF_INET6 &&
1522 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001523 &dns_opts->pref_net[j].mask.in6,
1524 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001525 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001526 break;
1527 }
1528 }
1529
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02001530 /* Check if the IP found in the record is already affected to a member of a group.
1531 * If yes, the score should be incremented by 2.
1532 */
1533 if (owner) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001534 if (snr_check_ip_callback(owner, ip, &ip_type))
1535 {
1536 continue;
1537 }
Baptistefc725902016-12-26 23:21:08 +01001538 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001539 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001540 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001541 ((currentip_sin_family == AF_INET &&
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001542 memcmp(ip, currentip, 4) == 0) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001543 (currentip_sin_family == AF_INET6 &&
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001544 memcmp(ip, currentip, 16) == 0))) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001545 score += 1;
1546 currentip_sel = 1;
1547 } else
1548 currentip_sel = 0;
1549
Baptistefc725902016-12-26 23:21:08 +01001550
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001551 /* Keep the address if the score is better than the previous
Baptistefc725902016-12-26 23:21:08 +01001552 * score. The maximum score is 15, if this value is reached,
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001553 * we break the parsing. Implicitly, this score is reached
1554 * the ip selected is the current ip.
1555 */
1556 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001557 if (ip_type == AF_INET)
1558 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001559 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001560 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001561 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001562 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001563 return DNS_UPD_NO;
1564 max_score = score;
1565 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001566
1567
1568 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001569
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001570 /* no IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001571 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001572 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001573
Baptiste Assmann325137d2015-04-13 23:40:55 +02001574 /* case when the caller looks first for an IPv4 address */
1575 if (family_priority == AF_INET) {
1576 if (newip4) {
1577 *newip = newip4;
1578 *newip_sin_family = AF_INET;
1579 if (currentip_found == 1)
1580 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001581 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001582 }
1583 else if (newip6) {
1584 *newip = newip6;
1585 *newip_sin_family = AF_INET6;
1586 if (currentip_found == 1)
1587 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001588 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001589 }
1590 }
1591 /* case when the caller looks first for an IPv6 address */
1592 else if (family_priority == AF_INET6) {
1593 if (newip6) {
1594 *newip = newip6;
1595 *newip_sin_family = AF_INET6;
1596 if (currentip_found == 1)
1597 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001598 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001599 }
1600 else if (newip4) {
1601 *newip = newip4;
1602 *newip_sin_family = AF_INET;
1603 if (currentip_found == 1)
1604 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001605 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001606 }
1607 }
1608 /* case when the caller have no preference (we prefer IPv6) */
1609 else if (family_priority == AF_UNSPEC) {
1610 if (newip6) {
1611 *newip = newip6;
1612 *newip_sin_family = AF_INET6;
1613 if (currentip_found == 1)
1614 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001615 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001616 }
1617 else if (newip4) {
1618 *newip = newip4;
1619 *newip_sin_family = AF_INET;
1620 if (currentip_found == 1)
1621 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001622 goto return_DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001623 }
1624 }
1625
1626 /* no reason why we should change the server's IP address */
1627 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001628
1629 return_DNS_UPD_SRVIP_NOT_FOUND:
1630 list_for_each_entry(record, &dns_p->answer_list, list) {
1631 /* move the first record to the end of the list, for internal round robin */
1632 if (record) {
1633 LIST_DEL(&record->list);
1634 LIST_ADDQ(&dns_p->answer_list, &record->list);
1635 break;
1636 }
1637 }
1638 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001639}
1640
1641/*
1642 * returns the query id contained in a DNS response
1643 */
Thiago Farinab1af23e2016-01-20 23:46:34 +01001644unsigned short dns_response_get_query_id(unsigned char *resp)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001645{
1646 /* read the query id from the response */
1647 return resp[0] * 256 + resp[1];
1648}
1649
1650/*
1651 * used during haproxy's init phase
1652 * parses resolvers sections and initializes:
1653 * - task (time events) for each resolvers section
1654 * - the datagram layer (network IO events) for each nameserver
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001655 * It takes one argument:
1656 * - close_first takes 2 values: 0 or 1. If 1, the connection is closed first.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001657 * returns:
1658 * 0 in case of error
1659 * 1 when no error
1660 */
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001661int dns_init_resolvers(int close_socket)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001662{
1663 struct dns_resolvers *curr_resolvers;
1664 struct dns_nameserver *curnameserver;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001665 struct dns_resolution *resolution, *res_back;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001666 struct dgram_conn *dgram;
1667 struct task *t;
1668 int fd;
1669
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001670 /* initialize our DNS resolution cache */
1671 dns_lru_tree = lru64_new(dns_cache_size);
1672
Baptiste Assmann325137d2015-04-13 23:40:55 +02001673 /* give a first random value to our dns query_id seed */
1674 dns_query_id_seed = random();
1675
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001676 /* Initialize the answer items pool */
1677 dns_answer_item_pool = create_pool("dns_answer_item",
1678 sizeof(struct dns_answer_item), MEM_F_SHARED);
1679 if (dns_answer_item_pool == NULL) {
1680 Alert("Failed to create the dns answer items pool");
1681 return 0;
1682 }
1683
Baptiste Assmann325137d2015-04-13 23:40:55 +02001684 /* run through the resolvers section list */
1685 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
1686 /* create the task associated to the resolvers section */
1687 if ((t = task_new()) == NULL) {
1688 Alert("Starting [%s] resolvers: out of memory.\n", curr_resolvers->id);
1689 return 0;
1690 }
1691
1692 /* update task's parameters */
1693 t->process = dns_process_resolve;
1694 t->context = curr_resolvers;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001695
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001696 /* no need to keep the new task if one is already affected to our resolvers
1697 * section */
1698 if (!curr_resolvers->t)
1699 curr_resolvers->t = t;
1700 else
1701 task_free(t);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001702
1703 list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001704 dgram = NULL;
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001705
1706 if (close_socket == 1) {
1707 if (curnameserver->dgram) {
Frédéric Lécaille64920532017-05-12 09:57:15 +02001708 fd_delete(curnameserver->dgram->t.sock.fd);
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001709 memset(curnameserver->dgram, '\0', sizeof(*dgram));
1710 dgram = curnameserver->dgram;
1711 }
1712 }
1713
1714 /* allocate memory only if it has not already been allocated
1715 * by a previous call to this function */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001716
Baptiste Assmann5cd1b922017-02-02 22:44:15 +01001717 if (!dgram && (dgram = calloc(1, sizeof(*dgram))) == NULL) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001718 Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id,
1719 curnameserver->id);
1720 return 0;
1721 }
1722 /* update datagram's parameters */
1723 dgram->owner = (void *)curnameserver;
1724 dgram->data = &resolve_dgram_cb;
1725
1726 /* create network UDP socket for this nameserver */
Frédéric Lécaille5e5bc9f2017-04-11 08:46:37 +02001727 if ((fd = socket(curnameserver->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001728 Alert("Starting [%s/%s] nameserver: can't create socket.\n", curr_resolvers->id,
1729 curnameserver->id);
1730 free(dgram);
1731 dgram = NULL;
1732 return 0;
1733 }
1734
1735 /* "connect" the UDP socket to the name server IP */
Baptiste Assmann8c62c472015-09-21 20:55:08 +02001736 if (connect(fd, (struct sockaddr*)&curnameserver->addr, get_addr_len(&curnameserver->addr)) == -1) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02001737 Alert("Starting [%s/%s] nameserver: can't connect socket.\n", curr_resolvers->id,
1738 curnameserver->id);
1739 close(fd);
1740 free(dgram);
1741 dgram = NULL;
1742 return 0;
1743 }
1744
1745 /* make the socket non blocking */
1746 fcntl(fd, F_SETFL, O_NONBLOCK);
1747
1748 /* add the fd in the fd list and update its parameters */
1749 fd_insert(fd);
1750 fdtab[fd].owner = dgram;
1751 fdtab[fd].iocb = dgram_fd_handler;
1752 fd_want_recv(fd);
1753 dgram->t.sock.fd = fd;
1754
1755 /* update nameserver's datagram property */
1756 curnameserver->dgram = dgram;
1757
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001758 continue;
1759 }
1760
1761 if (close_socket == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001762 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001763
1764 /* now, we can trigger DNS resolution */
1765 list_for_each_entry_safe(resolution, res_back, &curr_resolvers->resolution.wait, list) {
1766 /* if there is no requester in the wait queue, no need to trigger the resolution */
1767 if (LIST_ISEMPTY(&resolution->requester.wait))
1768 continue;
1769
1770 dns_trigger_resolution(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001771 }
1772
1773 /* task can be queued */
1774 task_queue(t);
1775 }
1776
1777 return 1;
1778}
1779
1780/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001781 * Allocate a pool of resolution to a resolvers section.
1782 * Each resolution is associated with a UUID.
1783 *
1784 * Return code:
1785 * - 0 if everything went smoothly
1786 * - -1 if an error occured
1787 */
1788int dns_alloc_resolution_pool(struct dns_resolvers *resolvers)
1789{
1790 int i;
1791 struct dns_resolution *resolution;
1792
1793 /* return if a pool has already been set for this resolvers */
1794 if (!LIST_ISEMPTY(&resolvers->resolution.pool)) {
1795 return 0;
1796 }
1797
1798 for (i = 0; i < resolvers->resolution_pool_size; i++) {
1799 resolution = dns_alloc_resolution();
1800 if (!resolution) {
1801 Alert("Starting [%s] resolvers: can't allocate memory for DNS resolution pool.\n", resolvers->id);
1802 return -1;
1803 }
1804 resolution->uuid = i;
1805 LIST_ADDQ(&resolvers->resolution.pool, &resolution->list);
1806 }
1807
1808 return 0;
1809}
1810
1811/*
Baptiste Assmann325137d2015-04-13 23:40:55 +02001812 * Forge a DNS query. It needs the following information from the caller:
1813 * - <query_id>: the DNS query id corresponding to this query
1814 * - <query_type>: DNS_RTYPE_* request DNS record type (A, AAAA, ANY, etc...)
1815 * - <hostname_dn>: hostname in domain name format
1816 * - <hostname_dn_len>: length of <hostname_dn>
1817 * To store the query, the caller must pass a buffer <buf> and its size <bufsize>
1818 *
1819 * the DNS query is stored in <buf>
1820 * returns:
1821 * -1 if <buf> is too short
1822 */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001823int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001824{
1825 struct dns_header *dns;
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001826 struct dns_question qinfo;
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001827 struct dns_additional_record edns;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001828 char *ptr, *bufend;
1829
1830 memset(buf, '\0', bufsize);
1831 ptr = buf;
1832 bufend = buf + bufsize;
1833
1834 /* check if there is enough room for DNS headers */
1835 if (ptr + sizeof(struct dns_header) >= bufend)
1836 return -1;
1837
1838 /* set dns query headers */
1839 dns = (struct dns_header *)ptr;
1840 dns->id = (unsigned short) htons(query_id);
Nenad Merdanovic8ab79422016-07-13 14:03:43 +02001841 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 +02001842 dns->qdcount = htons(1); /* 1 question */
1843 dns->ancount = 0;
1844 dns->nscount = 0;
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001845 dns->arcount = htons(1);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001846
1847 /* move forward ptr */
1848 ptr += sizeof(struct dns_header);
1849
1850 /* check if there is enough room for query hostname */
1851 if ((ptr + hostname_dn_len) >= bufend)
1852 return -1;
1853
1854 /* set up query hostname */
1855 memcpy(ptr, hostname_dn, hostname_dn_len);
1856 ptr[hostname_dn_len + 1] = '\0';
1857
1858 /* move forward ptr */
1859 ptr += (hostname_dn_len + 1);
1860
1861 /* check if there is enough room for query hostname*/
1862 if (ptr + sizeof(struct dns_question) >= bufend)
1863 return -1;
1864
1865 /* set up query info (type and class) */
Vincent Bernat9b7125c2016-04-08 22:17:45 +02001866 qinfo.qtype = htons(query_type);
1867 qinfo.qclass = htons(DNS_RCLASS_IN);
1868 memcpy(ptr, &qinfo, sizeof(qinfo));
Baptiste Assmann325137d2015-04-13 23:40:55 +02001869
1870 ptr += sizeof(struct dns_question);
1871
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001872 /* check if there is enough room for additional records */
1873 if (ptr + sizeof(edns) >= bufend)
1874 return -1;
1875
1876 /* set the DNS extension */
1877 edns.name = 0;
1878 edns.type = htons(DNS_RTYPE_OPT);
1879 edns.udp_payload_size = htons(accepted_payload_size);
1880 edns.extension = 0;
1881 edns.data_length = 0;
1882 memcpy(ptr, &edns, sizeof(edns));
1883 ptr += sizeof(edns);
1884
Baptiste Assmann325137d2015-04-13 23:40:55 +02001885 return ptr - buf;
1886}
1887
Olivier Houchard8da5f982017-08-04 18:35:36 +02001888/* Turn a domain name label into a string */
1889void dns_dn_label_to_str(char *dn, char *str, int dn_len)
1890{
1891 int remain_size = 0;
1892 int i;
1893
1894 for (i = 0; i < dn_len; i++) {
1895 if (remain_size == 0) {
1896 remain_size = dn[i];
1897 if (i != 0) {
1898 str[i - 1] = '.';
1899
1900 }
1901 } else {
1902 str[i - 1] = dn[i];
1903 remain_size--;
1904 }
1905 }
1906 str[dn_len - 1] = 0;
1907
1908}
1909
Baptiste Assmann325137d2015-04-13 23:40:55 +02001910/*
1911 * turn a string into domain name label:
1912 * www.haproxy.org into 3www7haproxy3org
1913 * if dn memory is pre-allocated, you must provide its size in dn_len
1914 * if dn memory isn't allocated, dn_len must be set to 0.
1915 * In the second case, memory will be allocated.
1916 * in case of error, -1 is returned, otherwise, number of bytes copied in dn
1917 */
Willy Tarreau2100b492015-07-22 16:42:43 +02001918char *dns_str_to_dn_label(const char *string, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001919{
1920 char *c, *d;
1921 int i, offset;
1922
1923 /* offset between string size and theorical dn size */
1924 offset = 1;
1925
1926 /*
1927 * first, get the size of the string turned into its domain name version
1928 * This function also validates the string respect the RFC
1929 */
1930 if ((i = dns_str_to_dn_label_len(string)) == -1)
1931 return NULL;
1932
1933 /* yes, so let's check there is enough memory */
1934 if (dn_len < i + offset)
1935 return NULL;
1936
Willy Tarreaud69d6f32015-07-22 16:45:36 +02001937 i = strlen(string);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001938 memcpy(dn + offset, string, i);
1939 dn[i + offset] = '\0';
1940 /* avoid a '\0' at the beginning of dn string which may prevent the for loop
1941 * below from working.
1942 * Actually, this is the reason of the offset. */
1943 dn[0] = '0';
1944
1945 for (c = dn; *c ; ++c) {
1946 /* c points to the first '0' char or a dot, which we don't want to read */
1947 d = c + offset;
1948 i = 0;
1949 while (*d != '.' && *d) {
1950 i++;
1951 d++;
1952 }
1953 *c = i;
1954
1955 c = d - 1; /* because of c++ of the for loop */
1956 }
1957
1958 return dn;
1959}
1960
1961/*
1962 * compute and return the length of <string> it it were translated into domain name
1963 * label:
1964 * www.haproxy.org into 3www7haproxy3org would return 16
1965 * NOTE: add +1 for '\0' when allocating memory ;)
1966 */
1967int dns_str_to_dn_label_len(const char *string)
1968{
1969 return strlen(string) + 1;
1970}
1971
1972/*
1973 * validates host name:
1974 * - total size
1975 * - each label size individually
1976 * returns:
1977 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1978 * 1 when no error. <err> is left unaffected.
1979 */
1980int dns_hostname_validation(const char *string, char **err)
1981{
1982 const char *c, *d;
1983 int i;
1984
1985 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1986 if (err)
1987 *err = DNS_TOO_LONG_FQDN;
1988 return 0;
1989 }
1990
1991 c = string;
1992 while (*c) {
1993 d = c;
1994
1995 i = 0;
1996 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1997 i++;
1998 if (!((*d == '-') || (*d == '_') ||
1999 ((*d >= 'a') && (*d <= 'z')) ||
2000 ((*d >= 'A') && (*d <= 'Z')) ||
2001 ((*d >= '0') && (*d <= '9')))) {
2002 if (err)
2003 *err = DNS_INVALID_CHARACTER;
2004 return 0;
2005 }
2006 d++;
2007 }
2008
2009 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
2010 if (err)
2011 *err = DNS_LABEL_TOO_LONG;
2012 return 0;
2013 }
2014
2015 if (*d == '\0')
2016 goto out;
2017
2018 c = ++d;
2019 }
2020 out:
2021 return 1;
2022}
2023
2024/*
2025 * 2 bytes random generator to generate DNS query ID
2026 */
2027uint16_t dns_rnd16(void)
2028{
2029 dns_query_id_seed ^= dns_query_id_seed << 13;
2030 dns_query_id_seed ^= dns_query_id_seed >> 7;
2031 dns_query_id_seed ^= dns_query_id_seed << 17;
2032 return dns_query_id_seed;
2033}
2034
2035
2036/*
2037 * function called when a timeout occurs during name resolution process
2038 * if max number of tries is reached, then stop, otherwise, retry.
2039 */
2040struct task *dns_process_resolve(struct task *t)
2041{
2042 struct dns_resolvers *resolvers = t->context;
2043 struct dns_resolution *resolution, *res_back;
Baptiste Assmann060e5732016-01-06 02:01:59 +01002044 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmann42746372017-05-03 12:12:02 +02002045 struct dns_options *dns_opts = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002046
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002047 /* if both there is no resolution in the run queue, we can re-schedule a wake up */
2048 if (LIST_ISEMPTY(&resolvers->resolution.curr)) {
Baptiste Assmann325137d2015-04-13 23:40:55 +02002049 /* no first entry, so wake up was useless */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002050 dns_update_resolvers_timeout(resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002051 return t;
2052 }
2053
2054 /* look for the first resolution which is not expired */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002055 list_for_each_entry_safe(resolution, res_back, &resolvers->resolution.curr, list) {
2056 struct dns_requester *requester = NULL;
2057
Baptiste Assmann325137d2015-04-13 23:40:55 +02002058 /* when we find the first resolution in the future, then we can stop here */
2059 if (tick_is_le(now_ms, resolution->last_sent_packet))
2060 goto out;
2061
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002062 if (LIST_ISEMPTY(&resolution->requester.curr))
2063 goto out;
2064
Baptiste Assmann325137d2015-04-13 23:40:55 +02002065 /*
2066 * if current resolution has been tried too many times and finishes in timeout
2067 * we update its status and remove it from the list
2068 */
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002069 if (resolution->try <= 0) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002070 struct dns_requester *tmprequester;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002071 /* clean up resolution information and remove from the list */
2072 dns_reset_resolution(resolution);
2073
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002074 LIST_DEL(&resolution->list);
2075 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
2076
2077 if (resolution->status != RSLV_STATUS_TIMEOUT) {
2078 resolution->status = RSLV_STATUS_TIMEOUT;
2079 resolution->last_status_change = now_ms;
2080 }
2081
2082 /* notify the result to the requesters */
2083 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
2084 requester->requester_error_cb(requester, DNS_RESP_TIMEOUT);
2085 LIST_DEL(&requester->list);
2086 LIST_ADDQ(&resolution->requester.wait, &requester->list);
2087 }
Baptiste Assmanne70bc052017-08-21 16:51:09 +02002088
2089 /* this might be triggered by too big UDP packets dropped
2090 * somewhere on the network, so lowering the accepted_payload_size
2091 * announced */
2092 if (resolvers->accepted_payload_size > 1280)
2093 resolvers->accepted_payload_size = 1280;
Baptiste Assmann382824c2016-01-06 01:53:46 +01002094 goto out;
Baptiste Assmann325137d2015-04-13 23:40:55 +02002095 }
2096
Baptiste Assmannf778bb42015-09-09 00:54:38 +02002097 resolution->try -= 1;
2098
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002099 /* running queue is empty, nothing to do but wait */
2100 if (LIST_ISEMPTY(&resolution->requester.curr))
2101 goto out;
2102
2103 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
2104
2105 switch (obj_type(requester->requester)) {
2106 case OBJ_TYPE_SERVER:
2107 dns_opts = &(objt_server(requester->requester)->dns_opts);
Olivier Houchard8da5f982017-08-04 18:35:36 +02002108 res_preferred_afinet = dns_opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
2109 res_preferred_afinet6 = dns_opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
2110
2111 /* let's change the query type if needed */
2112 if (res_preferred_afinet6) {
2113 /* fallback from AAAA to A */
2114 resolution->query_type = DNS_RTYPE_A;
2115 }
2116 else if (res_preferred_afinet) {
2117 /* fallback from A to AAAA */
2118 resolution->query_type = DNS_RTYPE_AAAA;
2119 }
2120
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002121 break;
2122
Olivier Houchard8da5f982017-08-04 18:35:36 +02002123 case OBJ_TYPE_SRVRQ:
2124 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002125 case OBJ_TYPE_NONE:
2126 default:
2127 /* clean up resolution information and remove from the list */
2128 dns_reset_resolution(resolution);
2129
2130 LIST_DEL(&resolution->list);
2131 LIST_ADDQ(&resolvers->resolution.wait, &resolution->list);
2132
2133 /* notify the result to the requester */
2134 requester->requester_error_cb(requester, DNS_RESP_INTERNAL);
2135 goto out;
2136 }
Baptiste Assmann42746372017-05-03 12:12:02 +02002137
Baptiste Assmann382824c2016-01-06 01:53:46 +01002138 /* resend the DNS query */
2139 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002140
Baptiste Assmann382824c2016-01-06 01:53:46 +01002141 /* check if we have more than one resolution in the list */
2142 if (dns_check_resolution_queue(resolvers) > 1) {
2143 /* move the rsolution to the end of the list */
2144 LIST_DEL(&resolution->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002145 LIST_ADDQ(&resolvers->resolution.curr, &resolution->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +02002146 }
2147 }
2148
2149 out:
2150 dns_update_resolvers_timeout(resolvers);
2151 return t;
2152}
William Lallemand69e96442016-11-19 00:58:54 +01002153
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002154/*
2155 * build a dns cache key composed as follow:
2156 * <query type>#<hostname in domain name format>
2157 * and store it into <str>.
2158 * It's up to the caller to allocate <buf> and to reset it.
2159 * The function returns NULL in case of error (IE <buf> too small) or a pointer
2160 * to buf if successful
2161 */
2162struct chunk *
2163dns_cache_key(int query_type, char *hostname_dn, int hostname_dn_len, struct chunk *buf)
2164{
2165 int len, size;
2166 char *str;
2167
2168 str = buf->str;
2169 len = buf->len;
2170 size = buf->size;
2171
2172 switch (query_type) {
2173 case DNS_RTYPE_A:
2174 if (len + 1 > size)
2175 return NULL;
2176 memcpy(&str[len], "A", 1);
2177 len += 1;
2178 break;
2179 case DNS_RTYPE_AAAA:
2180 if (len + 4 > size)
2181 return NULL;
2182 memcpy(&str[len], "AAAA", 4);
2183 len += 4;
2184 break;
2185 default:
2186 return NULL;
2187 }
2188
2189 if (len + 1 > size)
2190 return NULL;
2191 memcpy(&str[len], "#", 1);
2192 len += 1;
2193
2194 if (len + hostname_dn_len + 1 > size) // +1 for trailing zero
2195 return NULL;
2196 memcpy(&str[len], hostname_dn, hostname_dn_len);
2197 len += hostname_dn_len;
2198 str[len] = '\0';
2199
2200 return buf;
2201}
2202
2203/*
2204 * returns a pointer to a cache entry which may still be considered as up to date
2205 * by the caller.
2206 * returns NULL if no entry can be found or if the data found is outdated.
2207 */
2208struct lru64 *
2209dns_cache_lookup(int query_type, char *hostname_dn, int hostname_dn_len, int valid_period, void *cache_domain) {
2210 struct lru64 *elem = NULL;
2211 struct dns_resolution *resolution = NULL;
2212 struct dns_resolvers *resolvers = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002213 struct dns_requester *requester = NULL;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002214 int inter = 0;
2215 struct chunk *buf = get_trash_chunk();
2216 struct chunk *tmp = NULL;
2217
2218 if (!dns_lru_tree)
2219 return NULL;
2220
2221 chunk_reset(buf);
2222 tmp = dns_cache_key(query_type, hostname_dn, hostname_dn_len, buf);
2223 if (tmp == NULL)
2224 return NULL;
2225
2226 elem = lru64_lookup(XXH64(buf->str, buf->len, 1), dns_lru_tree, cache_domain, 1);
2227
2228 if (!elem || !elem->data)
2229 return NULL;
2230
2231 resolution = elem->data;
2232
2233 /* since we can change the fqdn of a server at run time, it may happen that
2234 * we got an innacurate elem.
2235 * This is because resolution->hostname_dn points to (owner)->hostname_dn (which
2236 * may be changed at run time)
2237 */
2238 if ((hostname_dn_len == resolution->hostname_dn_len) &&
2239 (memcmp(hostname_dn, resolution->hostname_dn, hostname_dn_len) != 0)) {
2240 return NULL;
2241 }
2242
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002243 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2244
2245 switch (obj_type(requester->requester)) {
2246 case OBJ_TYPE_SERVER:
2247 resolvers = objt_server(requester->requester)->resolvers;
2248 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002249 case OBJ_TYPE_SRVRQ:
2250 resolvers = objt_dns_srvrq(requester->requester)->resolvers;
2251 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002252 case OBJ_TYPE_NONE:
2253 default:
2254 return NULL;
2255 }
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02002256
2257 if (!resolvers)
2258 return NULL;
2259
2260 if (resolvers->hold.valid < valid_period)
2261 inter = resolvers->hold.valid;
2262 else
2263 inter = valid_period;
2264
2265 if (!tick_is_expired(tick_add(resolution->last_resolution, inter), now_ms))
2266 return elem;
2267
2268 return NULL;
2269}
2270
Willy Tarreau777b5602016-12-16 18:06:26 +01002271/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
William Lallemand69e96442016-11-19 00:58:54 +01002272static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
2273{
2274 struct dns_resolvers *presolvers;
2275
2276 if (*args[3]) {
William Lallemand69e96442016-11-19 00:58:54 +01002277 list_for_each_entry(presolvers, &dns_resolvers, list) {
2278 if (strcmp(presolvers->id, args[3]) == 0) {
Willy Tarreau777b5602016-12-16 18:06:26 +01002279 appctx->ctx.cli.p0 = presolvers;
William Lallemand69e96442016-11-19 00:58:54 +01002280 break;
2281 }
2282 }
Willy Tarreau777b5602016-12-16 18:06:26 +01002283 if (appctx->ctx.cli.p0 == NULL) {
William Lallemand69e96442016-11-19 00:58:54 +01002284 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002285 appctx->st0 = CLI_ST_PRINT;
William Lallemand69e96442016-11-19 00:58:54 +01002286 return 1;
2287 }
2288 }
Willy Tarreau3067bfa2016-12-05 14:50:15 +01002289 return 0;
William Lallemand69e96442016-11-19 00:58:54 +01002290}
2291
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002292/*
2293 * if <resolution> is provided, then the function skips the memory allocation part.
2294 * It does the linking only.
2295 *
2296 * if <resolution> is NULL, the function links a dns resolution to a requester:
2297 * - it allocates memory for the struct requester used to link
2298 * the resolution to the requester
2299 * - it configures the resolution if this is the first requester to be linked to it
2300 * - it updates the requester with a pointer to the resolution
2301 *
2302 * Return code:
2303 * - 0 if everything happened smoothly
2304 * - -1 if an error occured. Of course, no resolution is linked to the requester
2305 */
2306int dns_link_resolution(void *requester, int requester_type, struct dns_resolution *resolution)
2307{
2308 struct dns_resolution *tmpresolution = NULL;
2309 struct dns_requester *tmprequester = NULL;
2310 struct dns_resolvers *resolvers = NULL;
2311 char *hostname_dn = NULL;
2312 int new_resolution;
2313
Olivier Houchard8da5f982017-08-04 18:35:36 +02002314
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002315 if (!resolution) {
2316 tmprequester = calloc(1, sizeof(*tmprequester));
2317 if (!tmprequester)
2318 return -1;
2319
2320 switch (requester_type) {
2321 case OBJ_TYPE_SERVER:
2322 tmprequester->requester = &((struct server *)requester)->obj_type;
2323 hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2324 resolvers = objt_server(tmprequester->requester)->resolvers;
2325 switch (objt_server(tmprequester->requester)->dns_opts.family_prio) {
2326 case AF_INET:
2327 tmprequester->prefered_query_type = DNS_RTYPE_A;
2328 break;
2329 default:
2330 tmprequester->prefered_query_type = DNS_RTYPE_AAAA;
2331 }
2332
2333 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002334 case OBJ_TYPE_SRVRQ:
2335 tmprequester->requester = &((struct dns_srvrq *)requester)->obj_type;
2336 hostname_dn = objt_dns_srvrq(requester)->hostname_dn;
2337 resolvers = objt_dns_srvrq(requester)->resolvers;
2338 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002339 case OBJ_TYPE_NONE:
2340 default:
2341 free(tmprequester);
2342 return -1;
2343 }
2344
2345 /* get a resolution from the resolvers' wait queue or pool */
2346 tmpresolution = dns_resolution_list_get(resolvers, hostname_dn, tmprequester->prefered_query_type);
2347 if (!tmpresolution) {
2348 free(tmprequester);
2349 return -1;
2350 }
2351 }
2352 else {
2353 tmpresolution = resolution;
2354
2355 switch (requester_type) {
2356 case OBJ_TYPE_SERVER:
2357 tmprequester = ((struct server *)requester)->dns_requester;
2358 resolvers = ((struct server *)requester)->resolvers;
2359 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002360 case OBJ_TYPE_SRVRQ:
2361 tmprequester = objt_dns_srvrq(requester)->dns_requester;
2362 resolvers = objt_dns_srvrq(requester)->resolvers;
2363 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002364 case OBJ_TYPE_NONE:
2365 default:
2366 return -1;
2367 }
2368 }
2369
2370 /* flag this resolution as NEW if applicable (not already linked to any requester).
2371 * this is required to decide which parameters we have to update on the resolution.
2372 * If new, it means we pulled up the resolution from the resolvers' pool.
2373 */
2374 if (LIST_ISEMPTY(&tmpresolution->requester.wait)) {
2375 new_resolution = 1;
2376 }
2377 else
2378 new_resolution = 0;
2379
2380 /* those parameters are related to the requester type */
2381 switch (obj_type(tmprequester->requester)) {
2382 case OBJ_TYPE_SERVER:
2383 /* some parameters should be set only if the resolution is brand new */
2384 if (new_resolution) {
2385 tmpresolution->query_type = tmprequester->prefered_query_type;
2386 tmpresolution->hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2387 tmpresolution->hostname_dn_len = objt_server(tmprequester->requester)->hostname_dn_len;
2388 }
2389
2390 /* update requester as well, only if we just allocated it */
2391 objt_server(tmprequester->requester)->resolution = tmpresolution;
2392 if (!resolution) {
2393 tmprequester->requester_cb = snr_resolution_cb;
2394 tmprequester->requester_error_cb = snr_resolution_error_cb;
2395 objt_server(tmprequester->requester)->dns_requester = tmprequester;
2396 }
2397 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002398 case OBJ_TYPE_SRVRQ:
2399 /* some parameters should be set only if the resolution is brand new */
2400 if (new_resolution) {
2401 tmpresolution->query_type = DNS_RTYPE_SRV;
2402 tmpresolution->hostname_dn = objt_dns_srvrq(tmprequester->requester)->hostname_dn;
2403 tmpresolution->hostname_dn_len = objt_dns_srvrq(tmprequester->requester)->hostname_dn_len;
2404 }
2405
2406 /* update requester as well, only if we just allocated it */
2407 objt_dns_srvrq(tmprequester->requester)->resolution = tmpresolution;
2408 if (!resolution) {
2409 tmprequester->requester_cb = snr_resolution_cb;
2410 tmprequester->requester_error_cb = snr_resolution_error_cb;
2411 objt_dns_srvrq(tmprequester->requester)->dns_requester = tmprequester;
2412 }
2413 break;
2414
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002415 case OBJ_TYPE_NONE:
2416 default:
2417 free(tmprequester);
2418 return -1;
2419 }
2420
2421 /* update some parameters only if this is a brand new resolution */
2422 if (new_resolution) {
2423 /* move the resolution to the requesters' wait queue */
2424 LIST_DEL(&tmpresolution->list);
2425 LIST_ADDQ(&resolvers->resolution.wait, &tmpresolution->list);
2426
2427 tmpresolution->status = RSLV_STATUS_NONE;
2428 tmpresolution->step = RSLV_STEP_NONE;
2429 tmpresolution->revision = 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02002430 LIST_INIT(&tmpresolution->response.answer_list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002431 }
2432
2433 /* add the requester to the resolution's wait queue */
2434 if (resolution)
2435 LIST_DEL(&tmprequester->list);
2436 LIST_ADDQ(&tmpresolution->requester.wait, &tmprequester->list);
2437
2438 return 0;
2439}
2440
2441/*
2442 * pick up an available resolution from the different resolution list associated to a resolvers section,
2443 * in this order:
2444 * 1. check in resolution.curr for the same hostname and query_type
2445 * 2. check in resolution.wait for the same hostname and query_type
2446 * 3. take an available resolution from resolution.pool
2447 *
2448 * return an available resolution, NULL if none found.
2449 */
2450struct dns_resolution *dns_resolution_list_get(struct dns_resolvers *resolvers, char *hostname_dn, int query_type)
2451{
2452 struct dns_resolution *resolution, *tmpresolution;
2453 struct dns_requester *requester;
2454
Olivier Houchard8da5f982017-08-04 18:35:36 +02002455 if (hostname_dn) {
2456 /* search for same hostname and query type in resolution.curr */
2457 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.curr, list) {
2458 requester = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002459
Olivier Houchard8da5f982017-08-04 18:35:36 +02002460 if (!LIST_ISEMPTY(&resolution->requester.wait))
2461 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2462 else if (!LIST_ISEMPTY(&resolution->requester.curr))
2463 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002464
Olivier Houchard8da5f982017-08-04 18:35:36 +02002465 if (!requester)
2466 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002467
Olivier Houchard8da5f982017-08-04 18:35:36 +02002468 if ((query_type == requester->prefered_query_type) &&
2469 (resolution->hostname_dn &&
2470 strcmp(hostname_dn, resolution->hostname_dn) == 0)) {
2471 return resolution;
2472 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002473 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002474
Olivier Houchard8da5f982017-08-04 18:35:36 +02002475 /* search for same hostname and query type in resolution.wait */
2476 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.wait, list) {
2477 requester = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002478
Olivier Houchard8da5f982017-08-04 18:35:36 +02002479 if (!LIST_ISEMPTY(&resolution->requester.wait))
2480 requester = LIST_NEXT(&resolution->requester.wait, struct dns_requester *, list);
2481 else if (!LIST_ISEMPTY(&resolution->requester.curr))
2482 requester = LIST_NEXT(&resolution->requester.curr, struct dns_requester *, list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002483
Olivier Houchard8da5f982017-08-04 18:35:36 +02002484 if (!requester)
2485 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002486
Olivier Houchard8da5f982017-08-04 18:35:36 +02002487 if ((query_type == requester->prefered_query_type) &&
2488 (resolution->hostname_dn &&
2489 strcmp(hostname_dn, resolution->hostname_dn) == 0)) {
2490 return resolution;
2491 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002492 }
2493 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002494 /* take the first one (hopefully) from the pool */
2495 list_for_each_entry_safe(resolution, tmpresolution, &resolvers->resolution.pool, list) {
2496 if (LIST_ISEMPTY(&resolution->requester.wait)) {
2497 return resolution;
2498 }
2499 }
2500
2501 return NULL;
2502}
2503
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002504/* This function allocates memory for a DNS resolution structure.
2505 * It's up to the caller to set the parameters
2506 * Returns a pointer to the structure resolution or NULL if memory could
2507 * not be allocated.
2508 */
2509struct dns_resolution *dns_alloc_resolution(void)
2510{
2511 struct dns_resolution *resolution = NULL;
Baptiste Assmann729c9012017-05-22 15:13:10 +02002512 char *buffer = NULL;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002513
2514 resolution = calloc(1, sizeof(*resolution));
Baptiste Assmann729c9012017-05-22 15:13:10 +02002515 buffer = calloc(1, global.tune.bufsize);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002516
Baptiste Assmann729c9012017-05-22 15:13:10 +02002517 if (!resolution || !buffer) {
2518 free(buffer);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002519 free(resolution);
2520 return NULL;
2521 }
2522
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002523 LIST_INIT(&resolution->requester.wait);
2524 LIST_INIT(&resolution->requester.curr);
Baptiste Assmann729c9012017-05-22 15:13:10 +02002525
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002526 return resolution;
2527}
2528
2529/* This function free the memory allocated to a DNS resolution */
2530void dns_free_resolution(struct dns_resolution *resolution)
2531{
2532 free(resolution);
2533
2534 return;
2535}
2536
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002537/* this function free a resolution from its requester(s) and move it back to the pool */
2538void dns_resolution_free(struct dns_resolvers *resolvers, struct dns_resolution *resolution)
2539{
2540 struct dns_requester *requester, *tmprequester;
2541
2542 /* clean up configuration */
2543 dns_reset_resolution(resolution);
2544 resolution->hostname_dn = NULL;
2545 resolution->hostname_dn_len = 0;
2546
2547 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.wait, list) {
2548 LIST_DEL(&requester->list);
2549 }
2550 list_for_each_entry_safe(requester, tmprequester, &resolution->requester.curr, list) {
2551 LIST_DEL(&requester->list);
2552 }
2553
2554 LIST_DEL(&resolution->list);
2555 LIST_ADDQ(&resolvers->resolution.pool, &resolution->list);
2556
2557 return;
2558}
2559
2560/*
2561 * this function remove a requester from a resolution
2562 * and takes care of all the consequences.
2563 * It also cleans up some parameters from the requester
2564 */
2565void dns_rm_requester_from_resolution(struct dns_requester *requester, struct dns_resolution *resolution)
2566{
2567 char *hostname_dn;
2568 struct dns_requester *tmprequester;
2569
2570 /* resolution is still used by other requesters, we need to move
2571 * some pointers to an other requester if needed
2572 */
2573 switch (obj_type(requester->requester)) {
2574 case OBJ_TYPE_SERVER:
2575 hostname_dn = objt_server(requester->requester)->hostname_dn;
2576 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002577 case OBJ_TYPE_SRVRQ:
2578 hostname_dn = objt_dns_srvrq(requester->requester)->hostname_dn;
2579 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002580 case OBJ_TYPE_NONE:
2581 default:
2582 hostname_dn = NULL;
2583 break;
2584 }
2585
2586 if (resolution->hostname_dn != hostname_dn)
2587 return;
2588
2589 /* First, we need to find this other requester */
2590 tmprequester = NULL;
2591 list_for_each_entry(tmprequester, &resolution->requester.wait, list) {
2592 if (tmprequester != requester)
2593 break;
2594 }
2595 if (!tmprequester) {
2596 /* if we can't find it in wait queue, let's get one in run queue */
2597 list_for_each_entry(tmprequester, &resolution->requester.curr, list) {
2598 if (tmprequester != requester)
2599 break;
2600 }
2601 }
2602
2603 /* move hostname_dn related pointers to the next requester */
2604 switch (obj_type(tmprequester->requester)) {
2605 case OBJ_TYPE_SERVER:
2606 resolution->hostname_dn = objt_server(tmprequester->requester)->hostname_dn;
2607 resolution->hostname_dn_len = objt_server(tmprequester->requester)->hostname_dn_len;
2608 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002609 case OBJ_TYPE_SRVRQ:
2610 resolution->hostname_dn = objt_dns_srvrq(tmprequester->requester)->hostname_dn;
2611 resolution->hostname_dn_len = objt_dns_srvrq(tmprequester->requester)->hostname_dn_len;
2612 break;
2613
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002614 case OBJ_TYPE_NONE:
2615 default:
2616 ;;
2617 }
2618
2619
2620 /* clean up the requester */
2621 LIST_DEL(&requester->list);
2622 switch (obj_type(requester->requester)) {
2623 case OBJ_TYPE_SERVER:
2624 objt_server(requester->requester)->resolution = NULL;
2625 break;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002626 case OBJ_TYPE_SRVRQ:
2627 objt_dns_srvrq(requester->requester)->resolution = NULL;
2628 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002629 case OBJ_TYPE_NONE:
2630 default:
2631 ;;
2632 }
2633}
2634
Willy Tarreau777b5602016-12-16 18:06:26 +01002635/* This function dumps counters from all resolvers section and associated name
2636 * servers. It returns 0 if the output buffer is full and it needs to be called
2637 * again, otherwise non-zero. It may limit itself to the resolver pointed to by
2638 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01002639 */
2640static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
2641{
2642 struct stream_interface *si = appctx->owner;
2643 struct dns_resolvers *presolvers;
2644 struct dns_nameserver *pnameserver;
2645
2646 chunk_reset(&trash);
2647
2648 switch (appctx->st2) {
2649 case STAT_ST_INIT:
2650 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
2651 /* fall through */
2652
2653 case STAT_ST_LIST:
2654 if (LIST_ISEMPTY(&dns_resolvers)) {
2655 chunk_appendf(&trash, "No resolvers found\n");
2656 }
2657 else {
2658 list_for_each_entry(presolvers, &dns_resolvers, list) {
Willy Tarreau777b5602016-12-16 18:06:26 +01002659 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != presolvers)
William Lallemand69e96442016-11-19 00:58:54 +01002660 continue;
2661
2662 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
2663 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
2664 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
2665 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
2666 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
2667 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
2668 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
2669 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
2670 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
2671 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
2672 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
2673 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
2674 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
2675 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
2676 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
2677 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
2678 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
2679 }
2680 }
2681 }
2682
2683 /* display response */
2684 if (bi_putchk(si_ic(si), &trash) == -1) {
2685 /* let's try again later from this session. We add ourselves into
2686 * this session's users so that it can remove us upon termination.
2687 */
2688 si->flags |= SI_FL_WAIT_ROOM;
2689 return 0;
2690 }
2691
2692 appctx->st2 = STAT_ST_FIN;
2693 /* fall through */
2694
2695 default:
2696 appctx->st2 = STAT_ST_FIN;
2697 return 1;
2698 }
2699}
2700
2701/* register cli keywords */
2702static struct cli_kw_list cli_kws = {{ },{
2703 { { "show", "stat", "resolvers", NULL }, "show stat resolvers [id]: dumps counters from all resolvers section and\n"
2704 " associated name servers",
2705 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2706 {{},}
2707}};
2708
2709
2710__attribute__((constructor))
2711static void __dns_init(void)
2712{
2713 cli_register_kw(&cli_kws);
2714}
2715