2 * services/outside_network.c - implement sending of queries and wait answer.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file has functions to send queries to authoritative servers and
40 * wait for the pending answer events.
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
48 #include <ldns/wire2host.h>
49 #include "services/outside_network.h"
50 #include "services/listen_dnsport.h"
51 #include "services/cache/infra.h"
52 #include "util/data/msgparse.h"
53 #include "util/data/msgreply.h"
54 #include "util/data/msgencode.h"
55 #include "util/data/dname.h"
56 #include "util/netevent.h"
58 #include "util/net_help.h"
59 #include "util/random.h"
60 #include "util/fptr_wlist.h"
61 #ifdef HAVE_OPENSSL_SSL_H
62 #include <openssl/ssl.h>
70 /** number of times to retry making a random ID that is unique. */
71 #define MAX_ID_RETRY 1000
72 /** number of times to retry finding interface, port that can be opened. */
73 #define MAX_PORT_RETRY 10000
74 /** number of retries on outgoing UDP queries */
75 #define OUTBOUND_UDP_RETRY 1
77 /** initiate TCP transaction for serviced query */
78 static void serviced_tcp_initiate(struct outside_network* outnet,
79 struct serviced_query* sq, ldns_buffer* buff);
80 /** with a fd available, randomize and send UDP */
81 static int randomize_and_send_udp(struct outside_network* outnet,
82 struct pending* pend, ldns_buffer* packet, int timeout);
85 pending_cmp(const void* key1, const void* key2)
87 struct pending *p1 = (struct pending*)key1;
88 struct pending *p2 = (struct pending*)key2;
93 log_assert(p1->id == p2->id);
94 return sockaddr_cmp(&p1->addr, p1->addrlen, &p2->addr, p2->addrlen);
98 serviced_cmp(const void* key1, const void* key2)
100 struct serviced_query* q1 = (struct serviced_query*)key1;
101 struct serviced_query* q2 = (struct serviced_query*)key2;
103 if(q1->qbuflen < q2->qbuflen)
105 if(q1->qbuflen > q2->qbuflen)
107 log_assert(q1->qbuflen == q2->qbuflen);
108 log_assert(q1->qbuflen >= 15 /* 10 header, root, type, class */);
109 /* alternate casing of qname is still the same query */
110 if((r = memcmp(q1->qbuf, q2->qbuf, 10)) != 0)
112 if((r = memcmp(q1->qbuf+q1->qbuflen-4, q2->qbuf+q2->qbuflen-4, 4)) != 0)
114 if(q1->dnssec != q2->dnssec) {
115 if(q1->dnssec < q2->dnssec)
119 if((r = query_dname_compare(q1->qbuf+10, q2->qbuf+10)) != 0)
121 return sockaddr_cmp(&q1->addr, q1->addrlen, &q2->addr, q2->addrlen);
124 /** delete waiting_tcp entry. Does not unlink from waiting list.
125 * @param w: to delete.
128 waiting_tcp_delete(struct waiting_tcp* w)
132 comm_timer_delete(w->timer);
137 * Pick random outgoing-interface of that family, and bind it.
138 * port set to 0 so OS picks a port number for us.
139 * if it is the ANY address, do not bind.
140 * @param w: tcp structure with destination address.
141 * @param s: socket fd.
142 * @return false on error, socket closed.
145 pick_outgoing_tcp(struct waiting_tcp* w, int s)
147 struct port_if* pi = NULL;
150 if(addr_is_ip6(&w->addr, w->addrlen))
151 num = w->outnet->num_ip6;
154 num = w->outnet->num_ip4;
156 log_err("no TCP outgoing interfaces of family");
157 log_addr(VERB_OPS, "for addr", &w->addr, w->addrlen);
166 if(addr_is_ip6(&w->addr, w->addrlen))
167 pi = &w->outnet->ip6_ifs[ub_random_max(w->outnet->rnd, num)];
170 pi = &w->outnet->ip4_ifs[ub_random_max(w->outnet->rnd, num)];
172 if(addr_is_any(&pi->addr, pi->addrlen)) {
173 /* binding to the ANY interface is for listening sockets */
177 if(addr_is_ip6(&pi->addr, pi->addrlen))
178 ((struct sockaddr_in6*)&pi->addr)->sin6_port = 0;
179 else ((struct sockaddr_in*)&pi->addr)->sin_port = 0;
180 if(bind(s, (struct sockaddr*)&pi->addr, pi->addrlen) != 0) {
182 log_err("outgoing tcp: bind: %s", strerror(errno));
185 log_err("outgoing tcp: bind: %s",
186 wsa_strerror(WSAGetLastError()));
191 log_addr(VERB_ALGO, "tcp bound to src", &pi->addr, pi->addrlen);
195 /** use next free buffer to service a tcp query */
197 outnet_tcp_take_into_use(struct waiting_tcp* w, uint8_t* pkt, size_t pkt_len)
199 struct pending_tcp* pend = w->outnet->tcp_free;
203 log_assert(w->addrlen > 0);
206 if(addr_is_ip6(&w->addr, w->addrlen))
207 s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
210 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
213 log_err("outgoing tcp: socket: %s", strerror(errno));
215 log_err("outgoing tcp: socket: %s",
216 wsa_strerror(WSAGetLastError()));
218 log_addr(0, "failed address", &w->addr, w->addrlen);
221 if(!pick_outgoing_tcp(w, s))
225 if(connect(s, (struct sockaddr*)&w->addr, w->addrlen) == -1) {
228 if(errno != EINPROGRESS) {
232 if(tcp_connect_errno_needs_log(
233 (struct sockaddr*)&w->addr, w->addrlen))
234 log_err("outgoing tcp: connect: %s",
237 #else /* USE_WINSOCK */
238 if(WSAGetLastError() != WSAEINPROGRESS &&
239 WSAGetLastError() != WSAEWOULDBLOCK) {
242 log_addr(0, "failed address", &w->addr, w->addrlen);
246 if(w->outnet->sslctx && w->ssl_upstream) {
247 pend->c->ssl = outgoing_ssl_fd(w->outnet->sslctx, s);
250 comm_point_close(pend->c);
254 comm_point_tcp_win_bio_cb(pend->c, pend->c->ssl);
256 pend->c->ssl_shake_state = comm_ssl_shake_write;
259 w->next_waiting = (void*)pend;
260 pend->id = LDNS_ID_WIRE(pkt);
261 w->outnet->tcp_free = pend->next_free;
262 pend->next_free = NULL;
264 pend->c->repinfo.addrlen = w->addrlen;
265 memcpy(&pend->c->repinfo.addr, &w->addr, w->addrlen);
266 ldns_buffer_clear(pend->c->buffer);
267 ldns_buffer_write(pend->c->buffer, pkt, pkt_len);
268 ldns_buffer_flip(pend->c->buffer);
269 pend->c->tcp_is_reading = 0;
270 pend->c->tcp_byte_count = 0;
271 comm_point_start_listening(pend->c, s, -1);
275 /** see if buffers can be used to service TCP queries */
277 use_free_buffer(struct outside_network* outnet)
279 struct waiting_tcp* w;
280 while(outnet->tcp_free && outnet->tcp_wait_first
281 && !outnet->want_to_quit) {
282 w = outnet->tcp_wait_first;
283 outnet->tcp_wait_first = w->next_waiting;
284 if(outnet->tcp_wait_last == w)
285 outnet->tcp_wait_last = NULL;
286 if(!outnet_tcp_take_into_use(w, w->pkt, w->pkt_len)) {
287 comm_point_callback_t* cb = w->cb;
288 void* cb_arg = w->cb_arg;
289 waiting_tcp_delete(w);
290 fptr_ok(fptr_whitelist_pending_tcp(cb));
291 (void)(*cb)(NULL, cb_arg, NETEVENT_CLOSED, NULL);
296 /** decomission a tcp buffer, closes commpoint and frees waiting_tcp entry */
298 decomission_pending_tcp(struct outside_network* outnet,
299 struct pending_tcp* pend)
303 SSL_shutdown(pend->c->ssl);
304 SSL_free(pend->c->ssl);
308 comm_point_close(pend->c);
309 pend->next_free = outnet->tcp_free;
310 outnet->tcp_free = pend;
311 waiting_tcp_delete(pend->query);
313 use_free_buffer(outnet);
317 outnet_tcp_cb(struct comm_point* c, void* arg, int error,
318 struct comm_reply *reply_info)
320 struct pending_tcp* pend = (struct pending_tcp*)arg;
321 struct outside_network* outnet = pend->query->outnet;
322 verbose(VERB_ALGO, "outnettcp cb");
323 if(error != NETEVENT_NOERROR) {
324 verbose(VERB_QUERY, "outnettcp got tcp error %d", error);
325 /* pass error below and exit */
328 if(ldns_buffer_limit(c->buffer) < sizeof(uint16_t) ||
329 LDNS_ID_WIRE(ldns_buffer_begin(c->buffer))!=pend->id) {
331 "outnettcp: bad ID in reply, from:",
332 &pend->query->addr, pend->query->addrlen);
333 error = NETEVENT_CLOSED;
336 fptr_ok(fptr_whitelist_pending_tcp(pend->query->cb));
337 (void)(*pend->query->cb)(c, pend->query->cb_arg, error, reply_info);
338 decomission_pending_tcp(outnet, pend);
342 /** lower use count on pc, see if it can be closed */
344 portcomm_loweruse(struct outside_network* outnet, struct port_comm* pc)
347 pc->num_outstanding--;
348 if(pc->num_outstanding > 0) {
351 /* close it and replace in unused list */
352 verbose(VERB_ALGO, "close of port %d", pc->number);
353 comm_point_close(pc->cp);
355 log_assert(pif->inuse > 0);
356 pif->avail_ports[pif->avail_total - pif->inuse] = pc->number;
358 pif->out[pc->index] = pif->out[pif->inuse];
359 pif->out[pc->index]->index = pc->index;
360 pc->next = outnet->unused_fds;
361 outnet->unused_fds = pc;
364 /** try to send waiting UDP queries */
366 outnet_send_wait_udp(struct outside_network* outnet)
368 struct pending* pend;
369 /* process waiting queries */
370 while(outnet->udp_wait_first && outnet->unused_fds
371 && !outnet->want_to_quit) {
372 pend = outnet->udp_wait_first;
373 outnet->udp_wait_first = pend->next_waiting;
374 if(!pend->next_waiting) outnet->udp_wait_last = NULL;
375 ldns_buffer_clear(outnet->udp_buff);
376 ldns_buffer_write(outnet->udp_buff, pend->pkt, pend->pkt_len);
377 ldns_buffer_flip(outnet->udp_buff);
378 free(pend->pkt); /* freeing now makes get_mem correct */
381 if(!randomize_and_send_udp(outnet, pend, outnet->udp_buff,
383 /* callback error on pending */
384 fptr_ok(fptr_whitelist_pending_udp(pend->cb));
385 (void)(*pend->cb)(outnet->unused_fds->cp, pend->cb_arg,
386 NETEVENT_CLOSED, NULL);
387 pending_delete(outnet, pend);
393 outnet_udp_cb(struct comm_point* c, void* arg, int error,
394 struct comm_reply *reply_info)
396 struct outside_network* outnet = (struct outside_network*)arg;
399 verbose(VERB_ALGO, "answer cb");
401 if(error != NETEVENT_NOERROR) {
402 verbose(VERB_QUERY, "outnetudp got udp error %d", error);
405 if(ldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
406 verbose(VERB_QUERY, "outnetudp udp too short");
409 log_assert(reply_info);
411 /* setup lookup key */
412 key.id = (unsigned)LDNS_ID_WIRE(ldns_buffer_begin(c->buffer));
413 memcpy(&key.addr, &reply_info->addr, reply_info->addrlen);
414 key.addrlen = reply_info->addrlen;
415 verbose(VERB_ALGO, "Incoming reply id = %4.4x", key.id);
416 log_addr(VERB_ALGO, "Incoming reply addr =",
417 &reply_info->addr, reply_info->addrlen);
419 /* find it, see if this thing is a valid query response */
420 verbose(VERB_ALGO, "lookup size is %d entries", (int)outnet->pending->count);
421 p = (struct pending*)rbtree_search(outnet->pending, &key);
423 verbose(VERB_QUERY, "received unwanted or unsolicited udp reply dropped.");
424 log_buf(VERB_ALGO, "dropped message", c->buffer);
425 outnet->unwanted_replies++;
426 if(outnet->unwanted_threshold && ++outnet->unwanted_total
427 >= outnet->unwanted_threshold) {
428 log_warn("unwanted reply total reached threshold (%u)"
429 " you may be under attack."
430 " defensive action: clearing the cache",
431 (unsigned)outnet->unwanted_threshold);
432 fptr_ok(fptr_whitelist_alloc_cleanup(
433 outnet->unwanted_action));
434 (*outnet->unwanted_action)(outnet->unwanted_param);
435 outnet->unwanted_total = 0;
440 verbose(VERB_ALGO, "received udp reply.");
441 log_buf(VERB_ALGO, "udp message", c->buffer);
443 verbose(VERB_QUERY, "received reply id,addr on wrong port. "
445 outnet->unwanted_replies++;
446 if(outnet->unwanted_threshold && ++outnet->unwanted_total
447 >= outnet->unwanted_threshold) {
448 log_warn("unwanted reply total reached threshold (%u)"
449 " you may be under attack."
450 " defensive action: clearing the cache",
451 (unsigned)outnet->unwanted_threshold);
452 fptr_ok(fptr_whitelist_alloc_cleanup(
453 outnet->unwanted_action));
454 (*outnet->unwanted_action)(outnet->unwanted_param);
455 outnet->unwanted_total = 0;
459 comm_timer_disable(p->timer);
460 verbose(VERB_ALGO, "outnet handle udp reply");
461 /* delete from tree first in case callback creates a retry */
462 (void)rbtree_delete(outnet->pending, p->node.key);
463 fptr_ok(fptr_whitelist_pending_udp(p->cb));
464 (void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_NOERROR, reply_info);
465 portcomm_loweruse(outnet, p->pc);
466 pending_delete(NULL, p);
467 outnet_send_wait_udp(outnet);
471 /** calculate number of ip4 and ip6 interfaces*/
473 calc_num46(char** ifs, int num_ifs, int do_ip4, int do_ip6,
474 int* num_ip4, int* num_ip6)
486 for(i=0; i<num_ifs; i++)
488 if(str_is_ip6(ifs[i])) {
500 pending_udp_timer_cb(void *arg)
502 struct pending* p = (struct pending*)arg;
503 struct outside_network* outnet = p->outnet;
505 verbose(VERB_ALGO, "timeout udp");
506 fptr_ok(fptr_whitelist_pending_udp(p->cb));
507 (void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_TIMEOUT, NULL);
508 portcomm_loweruse(outnet, p->pc);
509 pending_delete(outnet, p);
510 outnet_send_wait_udp(outnet);
513 /** create pending_tcp buffers */
515 create_pending_tcp(struct outside_network* outnet, size_t bufsize)
518 if(outnet->num_tcp == 0)
519 return 1; /* no tcp needed, nothing to do */
520 if(!(outnet->tcp_conns = (struct pending_tcp **)calloc(
521 outnet->num_tcp, sizeof(struct pending_tcp*))))
523 for(i=0; i<outnet->num_tcp; i++) {
524 if(!(outnet->tcp_conns[i] = (struct pending_tcp*)calloc(1,
525 sizeof(struct pending_tcp))))
527 outnet->tcp_conns[i]->next_free = outnet->tcp_free;
528 outnet->tcp_free = outnet->tcp_conns[i];
529 outnet->tcp_conns[i]->c = comm_point_create_tcp_out(
530 outnet->base, bufsize, outnet_tcp_cb,
531 outnet->tcp_conns[i]);
532 if(!outnet->tcp_conns[i]->c)
538 /** setup an outgoing interface, ready address */
539 static int setup_if(struct port_if* pif, const char* addrstr,
540 int* avail, int numavail, size_t numfd)
542 pif->avail_total = numavail;
543 pif->avail_ports = (int*)memdup(avail, (size_t)numavail*sizeof(int));
544 if(!pif->avail_ports)
546 if(!ipstrtoaddr(addrstr, UNBOUND_DNS_PORT, &pif->addr, &pif->addrlen))
548 pif->maxout = (int)numfd;
550 pif->out = (struct port_comm**)calloc(numfd,
551 sizeof(struct port_comm*));
557 struct outside_network*
558 outside_network_create(struct comm_base *base, size_t bufsize,
559 size_t num_ports, char** ifs, int num_ifs, int do_ip4,
560 int do_ip6, size_t num_tcp, struct infra_cache* infra,
561 struct ub_randstate* rnd, int use_caps_for_id, int* availports,
562 int numavailports, size_t unwanted_threshold,
563 void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
566 struct outside_network* outnet = (struct outside_network*)
567 calloc(1, sizeof(struct outside_network));
570 log_err("malloc failed");
573 comm_base_timept(base, &outnet->now_secs, &outnet->now_tv);
575 outnet->num_tcp = num_tcp;
576 outnet->infra = infra;
578 outnet->sslctx = sslctx;
579 outnet->svcd_overhead = 0;
580 outnet->want_to_quit = 0;
581 outnet->unwanted_threshold = unwanted_threshold;
582 outnet->unwanted_action = unwanted_action;
583 outnet->unwanted_param = unwanted_param;
584 outnet->use_caps_for_id = use_caps_for_id;
585 outnet->do_udp = do_udp;
586 if(numavailports == 0) {
587 log_err("no outgoing ports available");
588 outside_network_delete(outnet);
594 calc_num46(ifs, num_ifs, do_ip4, do_ip6,
595 &outnet->num_ip4, &outnet->num_ip6);
596 if(outnet->num_ip4 != 0) {
597 if(!(outnet->ip4_ifs = (struct port_if*)calloc(
598 (size_t)outnet->num_ip4, sizeof(struct port_if)))) {
599 log_err("malloc failed");
600 outside_network_delete(outnet);
604 if(outnet->num_ip6 != 0) {
605 if(!(outnet->ip6_ifs = (struct port_if*)calloc(
606 (size_t)outnet->num_ip6, sizeof(struct port_if)))) {
607 log_err("malloc failed");
608 outside_network_delete(outnet);
612 if( !(outnet->udp_buff = ldns_buffer_new(bufsize)) ||
613 !(outnet->pending = rbtree_create(pending_cmp)) ||
614 !(outnet->serviced = rbtree_create(serviced_cmp)) ||
615 !create_pending_tcp(outnet, bufsize)) {
616 log_err("malloc failed");
617 outside_network_delete(outnet);
621 /* allocate commpoints */
622 for(k=0; k<num_ports; k++) {
623 struct port_comm* pc;
624 pc = (struct port_comm*)calloc(1, sizeof(*pc));
626 log_err("malloc failed");
627 outside_network_delete(outnet);
630 pc->cp = comm_point_create_udp(outnet->base, -1,
631 outnet->udp_buff, outnet_udp_cb, outnet);
633 log_err("malloc failed");
635 outside_network_delete(outnet);
638 pc->next = outnet->unused_fds;
639 outnet->unused_fds = pc;
642 /* allocate interfaces */
644 if(do_ip4 && !setup_if(&outnet->ip4_ifs[0], "0.0.0.0",
645 availports, numavailports, num_ports)) {
646 log_err("malloc failed");
647 outside_network_delete(outnet);
650 if(do_ip6 && !setup_if(&outnet->ip6_ifs[0], "::",
651 availports, numavailports, num_ports)) {
652 log_err("malloc failed");
653 outside_network_delete(outnet);
657 size_t done_4 = 0, done_6 = 0;
659 for(i=0; i<num_ifs; i++) {
660 if(str_is_ip6(ifs[i]) && do_ip6) {
661 if(!setup_if(&outnet->ip6_ifs[done_6], ifs[i],
662 availports, numavailports, num_ports)){
663 log_err("malloc failed");
664 outside_network_delete(outnet);
669 if(!str_is_ip6(ifs[i]) && do_ip4) {
670 if(!setup_if(&outnet->ip4_ifs[done_4], ifs[i],
671 availports, numavailports, num_ports)){
672 log_err("malloc failed");
673 outside_network_delete(outnet);
683 /** helper pending delete */
685 pending_node_del(rbnode_t* node, void* arg)
687 struct pending* pend = (struct pending*)node;
688 struct outside_network* outnet = (struct outside_network*)arg;
689 pending_delete(outnet, pend);
692 /** helper serviced delete */
694 serviced_node_del(rbnode_t* node, void* ATTR_UNUSED(arg))
696 struct serviced_query* sq = (struct serviced_query*)node;
697 struct service_callback* p = sq->cblist, *np;
709 outside_network_quit_prepare(struct outside_network* outnet)
713 /* prevent queued items from being sent */
714 outnet->want_to_quit = 1;
718 outside_network_delete(struct outside_network* outnet)
722 outnet->want_to_quit = 1;
723 /* check every element, since we can be called on malloc error */
724 if(outnet->pending) {
725 /* free pending elements, but do no unlink from tree. */
726 traverse_postorder(outnet->pending, pending_node_del, NULL);
727 free(outnet->pending);
729 if(outnet->serviced) {
730 traverse_postorder(outnet->serviced, serviced_node_del, NULL);
731 free(outnet->serviced);
734 ldns_buffer_free(outnet->udp_buff);
735 if(outnet->unused_fds) {
736 struct port_comm* p = outnet->unused_fds, *np;
739 comm_point_delete(p->cp);
743 outnet->unused_fds = NULL;
745 if(outnet->ip4_ifs) {
747 for(i=0; i<outnet->num_ip4; i++) {
748 for(k=0; k<outnet->ip4_ifs[i].inuse; k++) {
749 struct port_comm* pc = outnet->ip4_ifs[i].
751 comm_point_delete(pc->cp);
754 free(outnet->ip4_ifs[i].avail_ports);
755 free(outnet->ip4_ifs[i].out);
757 free(outnet->ip4_ifs);
759 if(outnet->ip6_ifs) {
761 for(i=0; i<outnet->num_ip6; i++) {
762 for(k=0; k<outnet->ip6_ifs[i].inuse; k++) {
763 struct port_comm* pc = outnet->ip6_ifs[i].
765 comm_point_delete(pc->cp);
768 free(outnet->ip6_ifs[i].avail_ports);
769 free(outnet->ip6_ifs[i].out);
771 free(outnet->ip6_ifs);
773 if(outnet->tcp_conns) {
775 for(i=0; i<outnet->num_tcp; i++)
776 if(outnet->tcp_conns[i]) {
777 comm_point_delete(outnet->tcp_conns[i]->c);
778 waiting_tcp_delete(outnet->tcp_conns[i]->query);
779 free(outnet->tcp_conns[i]);
781 free(outnet->tcp_conns);
783 if(outnet->tcp_wait_first) {
784 struct waiting_tcp* p = outnet->tcp_wait_first, *np;
786 np = p->next_waiting;
787 waiting_tcp_delete(p);
791 if(outnet->udp_wait_first) {
792 struct pending* p = outnet->udp_wait_first, *np;
794 np = p->next_waiting;
795 pending_delete(NULL, p);
803 pending_delete(struct outside_network* outnet, struct pending* p)
807 if(outnet && outnet->udp_wait_first &&
808 (p->next_waiting || p == outnet->udp_wait_last) ) {
809 /* delete from waiting list, if it is in the waiting list */
810 struct pending* prev = NULL, *x = outnet->udp_wait_first;
818 prev->next_waiting = p->next_waiting;
819 else outnet->udp_wait_first = p->next_waiting;
820 if(outnet->udp_wait_last == p)
821 outnet->udp_wait_last = prev;
825 (void)rbtree_delete(outnet->pending, p->node.key);
828 comm_timer_delete(p->timer);
834 * Try to open a UDP socket for outgoing communication.
835 * Sets sockets options as needed.
836 * @param addr: socket address.
837 * @param addrlen: length of address.
838 * @param port: port override for addr.
839 * @param inuse: if -1 is returned, this bool means the port was in use.
843 udp_sockport(struct sockaddr_storage* addr, socklen_t addrlen, int port,
847 if(addr_is_ip6(addr, addrlen)) {
848 struct sockaddr_in6* sa = (struct sockaddr_in6*)addr;
849 sa->sin6_port = (in_port_t)htons((uint16_t)port);
850 fd = create_udp_sock(AF_INET6, SOCK_DGRAM,
851 (struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
854 struct sockaddr_in* sa = (struct sockaddr_in*)addr;
855 sa->sin_port = (in_port_t)htons((uint16_t)port);
856 fd = create_udp_sock(AF_INET, SOCK_DGRAM,
857 (struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
863 /** Select random ID */
865 select_id(struct outside_network* outnet, struct pending* pend,
869 pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
870 LDNS_ID_SET(ldns_buffer_begin(packet), pend->id);
873 pend->node.key = pend;
874 while(!rbtree_insert(outnet->pending, &pend->node)) {
875 /* change ID to avoid collision */
876 pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
877 LDNS_ID_SET(ldns_buffer_begin(packet), pend->id);
879 if(id_tries == MAX_ID_RETRY) {
880 pend->id=99999; /* non existant ID */
881 log_err("failed to generate unique ID, drop msg");
885 verbose(VERB_ALGO, "inserted new pending reply id=%4.4x", pend->id);
889 /** Select random interface and port */
891 select_ifport(struct outside_network* outnet, struct pending* pend,
892 int num_if, struct port_if* ifs)
894 int my_if, my_port, fd, portno, inuse, tries=0;
896 /* randomly select interface and port */
898 verbose(VERB_QUERY, "Need to send query but have no "
899 "outgoing interfaces of that family");
902 log_assert(outnet->unused_fds);
905 my_if = ub_random_max(outnet->rnd, num_if);
907 my_port = ub_random_max(outnet->rnd, pif->avail_total);
908 if(my_port < pif->inuse) {
909 /* port already open */
910 pend->pc = pif->out[my_port];
911 verbose(VERB_ALGO, "using UDP if=%d port=%d",
912 my_if, pend->pc->number);
915 /* try to open new port, if fails, loop to try again */
916 log_assert(pif->inuse < pif->maxout);
917 portno = pif->avail_ports[my_port - pif->inuse];
918 fd = udp_sockport(&pif->addr, pif->addrlen, portno, &inuse);
919 if(fd == -1 && !inuse) {
920 /* nonrecoverable error making socket */
924 verbose(VERB_ALGO, "opened UDP if=%d port=%d",
927 pend->pc = outnet->unused_fds;
928 outnet->unused_fds = pend->pc->next;
931 pend->pc->next = NULL;
932 pend->pc->number = portno;
934 pend->pc->index = pif->inuse;
935 pend->pc->num_outstanding = 0;
936 comm_point_start_listening(pend->pc->cp, fd, -1);
938 /* grab port in interface */
939 pif->out[pif->inuse] = pend->pc;
940 pif->avail_ports[my_port - pif->inuse] =
941 pif->avail_ports[pif->avail_total-pif->inuse-1];
945 /* failed, already in use */
946 verbose(VERB_QUERY, "port %d in use, trying another", portno);
948 if(tries == MAX_PORT_RETRY) {
949 log_err("failed to find an open port, drop msg");
953 log_assert(pend->pc);
954 pend->pc->num_outstanding++;
960 randomize_and_send_udp(struct outside_network* outnet, struct pending* pend,
961 ldns_buffer* packet, int timeout)
966 if(!select_id(outnet, pend, packet)) {
970 /* select src_if, port */
971 if(addr_is_ip6(&pend->addr, pend->addrlen)) {
972 if(!select_ifport(outnet, pend,
973 outnet->num_ip6, outnet->ip6_ifs))
976 if(!select_ifport(outnet, pend,
977 outnet->num_ip4, outnet->ip4_ifs))
980 log_assert(pend->pc && pend->pc->cp);
982 /* send it over the commlink */
983 if(!comm_point_send_udp_msg(pend->pc->cp, packet,
984 (struct sockaddr*)&pend->addr, pend->addrlen)) {
985 portcomm_loweruse(outnet, pend->pc);
989 /* system calls to set timeout after sending UDP to make roundtrip
992 tv.tv_sec = timeout/1000;
993 tv.tv_usec = (timeout%1000)*1000;
995 comm_timer_set(pend->timer, &tv);
1000 pending_udp_query(struct outside_network* outnet, ldns_buffer* packet,
1001 struct sockaddr_storage* addr, socklen_t addrlen, int timeout,
1002 comm_point_callback_t* cb, void* cb_arg)
1004 struct pending* pend = (struct pending*)calloc(1, sizeof(*pend));
1005 if(!pend) return NULL;
1006 pend->outnet = outnet;
1007 pend->addrlen = addrlen;
1008 memmove(&pend->addr, addr, addrlen);
1010 pend->cb_arg = cb_arg;
1011 pend->node.key = pend;
1012 pend->timer = comm_timer_create(outnet->base, pending_udp_timer_cb,
1019 if(outnet->unused_fds == NULL) {
1020 /* no unused fd, cannot create a new port (randomly) */
1021 verbose(VERB_ALGO, "no fds available, udp query waiting");
1022 pend->timeout = timeout;
1023 pend->pkt_len = ldns_buffer_limit(packet);
1024 pend->pkt = (uint8_t*)memdup(ldns_buffer_begin(packet),
1027 comm_timer_delete(pend->timer);
1031 /* put at end of waiting list */
1032 if(outnet->udp_wait_last)
1033 outnet->udp_wait_last->next_waiting = pend;
1035 outnet->udp_wait_first = pend;
1036 outnet->udp_wait_last = pend;
1039 if(!randomize_and_send_udp(outnet, pend, packet, timeout)) {
1040 pending_delete(outnet, pend);
1047 outnet_tcptimer(void* arg)
1049 struct waiting_tcp* w = (struct waiting_tcp*)arg;
1050 struct outside_network* outnet = w->outnet;
1051 comm_point_callback_t* cb;
1054 /* it is on the waiting list */
1055 struct waiting_tcp* p=outnet->tcp_wait_first, *prev=NULL;
1058 if(prev) prev->next_waiting = w->next_waiting;
1059 else outnet->tcp_wait_first=w->next_waiting;
1060 outnet->tcp_wait_last = prev;
1068 struct pending_tcp* pend=(struct pending_tcp*)w->next_waiting;
1069 comm_point_close(pend->c);
1071 pend->next_free = outnet->tcp_free;
1072 outnet->tcp_free = pend;
1076 waiting_tcp_delete(w);
1077 fptr_ok(fptr_whitelist_pending_tcp(cb));
1078 (void)(*cb)(NULL, cb_arg, NETEVENT_TIMEOUT, NULL);
1079 use_free_buffer(outnet);
1083 pending_tcp_query(struct outside_network* outnet, ldns_buffer* packet,
1084 struct sockaddr_storage* addr, socklen_t addrlen, int timeout,
1085 comm_point_callback_t* callback, void* callback_arg, int ssl_upstream)
1087 struct pending_tcp* pend = outnet->tcp_free;
1088 struct waiting_tcp* w;
1091 /* if no buffer is free allocate space to store query */
1092 w = (struct waiting_tcp*)malloc(sizeof(struct waiting_tcp)
1093 + (pend?0:ldns_buffer_limit(packet)));
1097 if(!(w->timer = comm_timer_create(outnet->base, outnet_tcptimer, w))) {
1103 id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
1104 LDNS_ID_SET(ldns_buffer_begin(packet), id);
1105 memcpy(&w->addr, addr, addrlen);
1106 w->addrlen = addrlen;
1109 w->cb_arg = callback_arg;
1110 w->ssl_upstream = ssl_upstream;
1112 tv.tv_sec = timeout;
1115 comm_timer_set(w->timer, &tv);
1117 /* we have a buffer available right now */
1118 if(!outnet_tcp_take_into_use(w, ldns_buffer_begin(packet),
1119 ldns_buffer_limit(packet))) {
1120 waiting_tcp_delete(w);
1125 w->pkt = (uint8_t*)w + sizeof(struct waiting_tcp);
1126 w->pkt_len = ldns_buffer_limit(packet);
1127 memmove(w->pkt, ldns_buffer_begin(packet), w->pkt_len);
1128 w->next_waiting = NULL;
1129 if(outnet->tcp_wait_last)
1130 outnet->tcp_wait_last->next_waiting = w;
1131 else outnet->tcp_wait_first = w;
1132 outnet->tcp_wait_last = w;
1137 /** create query for serviced queries */
1139 serviced_gen_query(ldns_buffer* buff, uint8_t* qname, size_t qnamelen,
1140 uint16_t qtype, uint16_t qclass, uint16_t flags)
1142 ldns_buffer_clear(buff);
1144 ldns_buffer_write_u16(buff, flags);
1145 ldns_buffer_write_u16(buff, 1); /* qdcount */
1146 ldns_buffer_write_u16(buff, 0); /* ancount */
1147 ldns_buffer_write_u16(buff, 0); /* nscount */
1148 ldns_buffer_write_u16(buff, 0); /* arcount */
1149 ldns_buffer_write(buff, qname, qnamelen);
1150 ldns_buffer_write_u16(buff, qtype);
1151 ldns_buffer_write_u16(buff, qclass);
1152 ldns_buffer_flip(buff);
1155 /** lookup serviced query in serviced query rbtree */
1156 static struct serviced_query*
1157 lookup_serviced(struct outside_network* outnet, ldns_buffer* buff, int dnssec,
1158 struct sockaddr_storage* addr, socklen_t addrlen)
1160 struct serviced_query key;
1161 key.node.key = &key;
1162 key.qbuf = ldns_buffer_begin(buff);
1163 key.qbuflen = ldns_buffer_limit(buff);
1164 key.dnssec = dnssec;
1165 memcpy(&key.addr, addr, addrlen);
1166 key.addrlen = addrlen;
1167 key.outnet = outnet;
1168 return (struct serviced_query*)rbtree_search(outnet->serviced, &key);
1171 /** Create new serviced entry */
1172 static struct serviced_query*
1173 serviced_create(struct outside_network* outnet, ldns_buffer* buff, int dnssec,
1174 int want_dnssec, int tcp_upstream, int ssl_upstream,
1175 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
1176 size_t zonelen, int qtype)
1178 struct serviced_query* sq = (struct serviced_query*)malloc(sizeof(*sq));
1179 #ifdef UNBOUND_DEBUG
1185 sq->qbuf = memdup(ldns_buffer_begin(buff), ldns_buffer_limit(buff));
1190 sq->qbuflen = ldns_buffer_limit(buff);
1191 sq->zone = memdup(zone, zonelen);
1197 sq->zonelen = zonelen;
1199 sq->dnssec = dnssec;
1200 sq->want_dnssec = want_dnssec;
1201 sq->tcp_upstream = tcp_upstream;
1202 sq->ssl_upstream = ssl_upstream;
1203 memcpy(&sq->addr, addr, addrlen);
1204 sq->addrlen = addrlen;
1205 sq->outnet = outnet;
1208 sq->status = serviced_initial;
1210 sq->to_be_deleted = 0;
1211 #ifdef UNBOUND_DEBUG
1214 rbtree_insert(outnet->serviced, &sq->node);
1215 log_assert(ins != NULL); /* must not be already present */
1219 /** remove waiting tcp from the outnet waiting list */
1221 waiting_list_remove(struct outside_network* outnet, struct waiting_tcp* w)
1223 struct waiting_tcp* p = outnet->tcp_wait_first, *prev = NULL;
1228 prev->next_waiting = w->next_waiting;
1229 else outnet->tcp_wait_first = w->next_waiting;
1230 if(outnet->tcp_wait_last == w)
1231 outnet->tcp_wait_last = prev;
1235 p = p->next_waiting;
1239 /** cleanup serviced query entry */
1241 serviced_delete(struct serviced_query* sq)
1244 /* clear up the pending query */
1245 if(sq->status == serviced_query_UDP_EDNS ||
1246 sq->status == serviced_query_UDP ||
1247 sq->status == serviced_query_PROBE_EDNS ||
1248 sq->status == serviced_query_UDP_EDNS_FRAG ||
1249 sq->status == serviced_query_UDP_EDNS_fallback) {
1250 struct pending* p = (struct pending*)sq->pending;
1252 portcomm_loweruse(sq->outnet, p->pc);
1253 pending_delete(sq->outnet, p);
1254 /* this call can cause reentrant calls back into the
1256 outnet_send_wait_udp(sq->outnet);
1258 struct waiting_tcp* p = (struct waiting_tcp*)
1260 if(p->pkt == NULL) {
1261 decomission_pending_tcp(sq->outnet,
1262 (struct pending_tcp*)p->next_waiting);
1264 waiting_list_remove(sq->outnet, p);
1265 waiting_tcp_delete(p);
1269 /* does not delete from tree, caller has to do that */
1270 serviced_node_del(&sq->node, NULL);
1273 /** perturb a dname capitalization randomly */
1275 serviced_perturb_qname(struct ub_randstate* rnd, uint8_t* qbuf, size_t len)
1278 uint8_t* d = qbuf + 10;
1279 long int random = 0;
1281 log_assert(len >= 10 + 5 /* offset qname, root, qtype, qclass */);
1285 /* only perturb A-Z, a-z */
1286 if(isalpha((int)*d)) {
1287 /* get a random bit */
1289 random = ub_random(rnd);
1293 *d = (uint8_t)toupper((int)*d);
1295 *d = (uint8_t)tolower((int)*d);
1304 if(verbosity >= VERB_ALGO) {
1305 char buf[LDNS_MAX_DOMAINLEN+1];
1306 dname_str(qbuf+10, buf);
1307 verbose(VERB_ALGO, "qname perturbed to %s", buf);
1311 /** put serviced query into a buffer */
1313 serviced_encode(struct serviced_query* sq, ldns_buffer* buff, int with_edns)
1315 /* if we are using 0x20 bits for ID randomness, perturb them */
1316 if(sq->outnet->use_caps_for_id) {
1317 serviced_perturb_qname(sq->outnet->rnd, sq->qbuf, sq->qbuflen);
1319 /* generate query */
1320 ldns_buffer_clear(buff);
1321 ldns_buffer_write_u16(buff, 0); /* id placeholder */
1322 ldns_buffer_write(buff, sq->qbuf, sq->qbuflen);
1323 ldns_buffer_flip(buff);
1325 /* add edns section */
1326 struct edns_data edns;
1327 edns.edns_present = 1;
1329 edns.edns_version = EDNS_ADVERTISED_VERSION;
1330 if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1331 if(addr_is_ip6(&sq->addr, sq->addrlen)) {
1332 if(EDNS_FRAG_SIZE_IP6 < EDNS_ADVERTISED_SIZE)
1333 edns.udp_size = EDNS_FRAG_SIZE_IP6;
1334 else edns.udp_size = EDNS_ADVERTISED_SIZE;
1336 if(EDNS_FRAG_SIZE_IP4 < EDNS_ADVERTISED_SIZE)
1337 edns.udp_size = EDNS_FRAG_SIZE_IP4;
1338 else edns.udp_size = EDNS_ADVERTISED_SIZE;
1341 edns.udp_size = EDNS_ADVERTISED_SIZE;
1344 if(sq->dnssec & EDNS_DO)
1345 edns.bits = EDNS_DO;
1346 if(sq->dnssec & BIT_CD)
1347 LDNS_CD_SET(ldns_buffer_begin(buff));
1348 attach_edns_record(buff, &edns);
1353 * Perform serviced query UDP sending operation.
1354 * Sends UDP with EDNS, unless infra host marked non EDNS.
1355 * @param sq: query to send.
1356 * @param buff: buffer scratch space.
1357 * @return 0 on error.
1360 serviced_udp_send(struct serviced_query* sq, ldns_buffer* buff)
1363 uint8_t edns_lame_known;
1364 uint32_t now = *sq->outnet->now_secs;
1366 if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1367 sq->zonelen, now, &vs, &edns_lame_known, &rtt))
1370 verbose(VERB_ALGO, "EDNS lookup known=%d vs=%d", edns_lame_known, vs);
1371 if(sq->status == serviced_initial) {
1372 if(edns_lame_known == 0 && rtt > 5000 && rtt < 10001) {
1373 /* perform EDNS lame probe - check if server is
1374 * EDNS lame (EDNS queries to it are dropped) */
1375 verbose(VERB_ALGO, "serviced query: send probe to see "
1376 " if use of EDNS causes timeouts");
1377 /* even 700 msec may be too small */
1379 sq->status = serviced_query_PROBE_EDNS;
1380 } else if(vs != -1) {
1381 sq->status = serviced_query_UDP_EDNS;
1383 sq->status = serviced_query_UDP;
1386 serviced_encode(sq, buff, (sq->status == serviced_query_UDP_EDNS) ||
1387 (sq->status == serviced_query_UDP_EDNS_FRAG));
1388 sq->last_sent_time = *sq->outnet->now_tv;
1389 sq->edns_lame_known = (int)edns_lame_known;
1390 verbose(VERB_ALGO, "serviced query UDP timeout=%d msec", rtt);
1391 sq->pending = pending_udp_query(sq->outnet, buff, &sq->addr,
1392 sq->addrlen, rtt, serviced_udp_callback, sq);
1398 /** check that perturbed qname is identical */
1400 serviced_check_qname(ldns_buffer* pkt, uint8_t* qbuf, size_t qbuflen)
1402 uint8_t* d1 = ldns_buffer_at(pkt, 12);
1403 uint8_t* d2 = qbuf+10;
1406 log_assert(qbuflen >= 15 /* 10 header, root, type, class */);
1409 if(ldns_buffer_limit(pkt) < 12+1+4) /* packet too small for qname */
1411 while(len1 != 0 || len2 != 0) {
1412 if(LABEL_IS_PTR(len1)) {
1413 d1 = ldns_buffer_at(pkt, PTR_OFFSET(len1, *d1));
1414 if(d1 >= ldns_buffer_at(pkt, ldns_buffer_limit(pkt)))
1417 if(count++ > MAX_COMPRESS_PTRS)
1421 if(d2 > qbuf+qbuflen)
1425 if(len1 > LDNS_MAX_LABELLEN)
1427 log_assert(len1 <= LDNS_MAX_LABELLEN);
1428 log_assert(len2 <= LDNS_MAX_LABELLEN);
1429 log_assert(len1 == len2 && len1 != 0);
1430 /* compare the labels - bitwise identical */
1431 if(memcmp(d1, d2, len1) != 0)
1441 /** call the callbacks for a serviced query */
1443 serviced_callbacks(struct serviced_query* sq, int error, struct comm_point* c,
1444 struct comm_reply* rep)
1446 struct service_callback* p;
1447 int dobackup = (sq->cblist && sq->cblist->next); /* >1 cb*/
1448 uint8_t *backup_p = NULL;
1450 #ifdef UNBOUND_DEBUG
1453 /* remove from tree, and schedule for deletion, so that callbacks
1454 * can safely deregister themselves and even create new serviced
1455 * queries that are identical to this one. */
1456 rbtree_delete(sq->outnet->serviced, sq);
1457 log_assert(rem); /* should have been present */
1458 sq->to_be_deleted = 1;
1459 verbose(VERB_ALGO, "svcd callbacks start");
1460 if(sq->outnet->use_caps_for_id && error == NETEVENT_NOERROR && c) {
1461 /* noerror and nxdomain must have a qname in reply */
1462 if(ldns_buffer_read_u16_at(c->buffer, 4) == 0 &&
1463 (LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer))
1464 == LDNS_RCODE_NOERROR ||
1465 LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer))
1466 == LDNS_RCODE_NXDOMAIN)) {
1467 verbose(VERB_DETAIL, "no qname in reply to check 0x20ID");
1468 log_addr(VERB_DETAIL, "from server",
1469 &sq->addr, sq->addrlen);
1470 log_buf(VERB_DETAIL, "for packet", c->buffer);
1471 error = NETEVENT_CLOSED;
1473 } else if(ldns_buffer_read_u16_at(c->buffer, 4) > 0 &&
1474 !serviced_check_qname(c->buffer, sq->qbuf,
1476 verbose(VERB_DETAIL, "wrong 0x20-ID in reply qname");
1477 log_addr(VERB_DETAIL, "from server",
1478 &sq->addr, sq->addrlen);
1479 log_buf(VERB_DETAIL, "for packet", c->buffer);
1480 error = NETEVENT_CAPSFAIL;
1481 /* and cleanup too */
1482 pkt_dname_tolower(c->buffer,
1483 ldns_buffer_at(c->buffer, 12));
1485 verbose(VERB_ALGO, "good 0x20-ID in reply qname");
1486 /* cleanup caps, prettier cache contents. */
1487 pkt_dname_tolower(c->buffer,
1488 ldns_buffer_at(c->buffer, 12));
1492 /* make a backup of the query, since the querystate processing
1493 * may send outgoing queries that overwrite the buffer.
1494 * use secondary buffer to store the query.
1495 * This is a data copy, but faster than packet to server */
1496 backlen = ldns_buffer_limit(c->buffer);
1497 backup_p = memdup(ldns_buffer_begin(c->buffer), backlen);
1499 log_err("malloc failure in serviced query callbacks");
1500 error = NETEVENT_CLOSED;
1503 sq->outnet->svcd_overhead = backlen;
1505 /* test the actual sq->cblist, because the next elem could be deleted*/
1506 while((p=sq->cblist) != NULL) {
1507 sq->cblist = p->next; /* remove this element */
1509 ldns_buffer_clear(c->buffer);
1510 ldns_buffer_write(c->buffer, backup_p, backlen);
1511 ldns_buffer_flip(c->buffer);
1513 fptr_ok(fptr_whitelist_serviced_query(p->cb));
1514 (void)(*p->cb)(c, p->cb_arg, error, rep);
1519 sq->outnet->svcd_overhead = 0;
1521 verbose(VERB_ALGO, "svcd callbacks end");
1522 log_assert(sq->cblist == NULL);
1523 serviced_delete(sq);
1527 serviced_tcp_callback(struct comm_point* c, void* arg, int error,
1528 struct comm_reply* rep)
1530 struct serviced_query* sq = (struct serviced_query*)arg;
1531 struct comm_reply r2;
1532 sq->pending = NULL; /* removed after this callback */
1533 if(error != NETEVENT_NOERROR)
1534 log_addr(VERB_QUERY, "tcp error for address",
1535 &sq->addr, sq->addrlen);
1536 if(error==NETEVENT_NOERROR)
1537 infra_update_tcp_works(sq->outnet->infra, &sq->addr,
1538 sq->addrlen, sq->zone, sq->zonelen);
1539 if(error==NETEVENT_NOERROR && sq->status == serviced_query_TCP_EDNS &&
1540 (LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer)) ==
1541 LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(ldns_buffer_begin(
1542 c->buffer)) == LDNS_RCODE_NOTIMPL) ) {
1543 /* attempt to fallback to nonEDNS */
1544 sq->status = serviced_query_TCP_EDNS_fallback;
1545 serviced_tcp_initiate(sq->outnet, sq, c->buffer);
1547 } else if(error==NETEVENT_NOERROR &&
1548 sq->status == serviced_query_TCP_EDNS_fallback &&
1549 (LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer)) ==
1550 LDNS_RCODE_NOERROR || LDNS_RCODE_WIRE(
1551 ldns_buffer_begin(c->buffer)) == LDNS_RCODE_NXDOMAIN
1552 || LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer))
1553 == LDNS_RCODE_YXDOMAIN)) {
1554 /* the fallback produced a result that looks promising, note
1555 * that this server should be approached without EDNS */
1556 /* only store noEDNS in cache if domain is noDNSSEC */
1557 if(!sq->want_dnssec)
1558 if(!infra_edns_update(sq->outnet->infra, &sq->addr,
1559 sq->addrlen, sq->zone, sq->zonelen, -1,
1560 *sq->outnet->now_secs))
1561 log_err("Out of memory caching no edns for host");
1562 sq->status = serviced_query_TCP;
1564 if(sq->tcp_upstream || sq->ssl_upstream) {
1565 struct timeval now = *sq->outnet->now_tv;
1566 if(now.tv_sec > sq->last_sent_time.tv_sec ||
1567 (now.tv_sec == sq->last_sent_time.tv_sec &&
1568 now.tv_usec > sq->last_sent_time.tv_usec)) {
1569 /* convert from microseconds to milliseconds */
1570 int roundtime = ((int)now.tv_sec - (int)sq->last_sent_time.tv_sec)*1000
1571 + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1572 verbose(VERB_ALGO, "measured TCP-time at %d msec", roundtime);
1573 log_assert(roundtime >= 0);
1574 /* only store if less then AUTH_TIMEOUT seconds, it could be
1575 * huge due to system-hibernated and we woke up */
1576 if(roundtime < TCP_AUTH_QUERY_TIMEOUT*1000) {
1577 if(!infra_rtt_update(sq->outnet->infra, &sq->addr,
1578 sq->addrlen, sq->zone, sq->zonelen, sq->qtype,
1579 roundtime, sq->last_rtt, (uint32_t)now.tv_sec))
1580 log_err("out of memory noting rtt.");
1584 /* insert address into reply info */
1586 /* create one if there isn't (on errors) */
1590 memcpy(&rep->addr, &sq->addr, sq->addrlen);
1591 rep->addrlen = sq->addrlen;
1592 serviced_callbacks(sq, error, c, rep);
1597 serviced_tcp_initiate(struct outside_network* outnet,
1598 struct serviced_query* sq, ldns_buffer* buff)
1600 verbose(VERB_ALGO, "initiate TCP query %s",
1601 sq->status==serviced_query_TCP_EDNS?"EDNS":"");
1602 serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1603 sq->last_sent_time = *sq->outnet->now_tv;
1604 sq->pending = pending_tcp_query(outnet, buff, &sq->addr,
1605 sq->addrlen, TCP_AUTH_QUERY_TIMEOUT, serviced_tcp_callback,
1606 sq, sq->ssl_upstream);
1608 /* delete from tree so that a retry by above layer does not
1609 * clash with this entry */
1610 log_err("serviced_tcp_initiate: failed to send tcp query");
1611 serviced_callbacks(sq, NETEVENT_CLOSED, NULL, NULL);
1615 /** Send serviced query over TCP return false on initial failure */
1617 serviced_tcp_send(struct serviced_query* sq, ldns_buffer* buff)
1620 uint8_t edns_lame_known;
1621 if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1622 sq->zonelen, *sq->outnet->now_secs, &vs, &edns_lame_known,
1626 sq->status = serviced_query_TCP_EDNS;
1627 else sq->status = serviced_query_TCP;
1628 serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1629 sq->last_sent_time = *sq->outnet->now_tv;
1630 sq->pending = pending_tcp_query(sq->outnet, buff, &sq->addr,
1631 sq->addrlen, TCP_AUTH_QUERY_TIMEOUT, serviced_tcp_callback,
1632 sq, sq->ssl_upstream);
1633 return sq->pending != NULL;
1637 serviced_udp_callback(struct comm_point* c, void* arg, int error,
1638 struct comm_reply* rep)
1640 struct serviced_query* sq = (struct serviced_query*)arg;
1641 struct outside_network* outnet = sq->outnet;
1642 struct timeval now = *sq->outnet->now_tv;
1643 int fallback_tcp = 0;
1645 sq->pending = NULL; /* removed after callback */
1646 if(error == NETEVENT_TIMEOUT) {
1648 if(sq->status == serviced_query_PROBE_EDNS) {
1649 /* non-EDNS probe failed; we do not know its status,
1650 * keep trying with EDNS, timeout may not be caused
1652 sq->status = serviced_query_UDP_EDNS;
1654 if(sq->status == serviced_query_UDP_EDNS && sq->last_rtt < 5000) {
1655 /* fallback to 1480/1280 */
1656 sq->status = serviced_query_UDP_EDNS_FRAG;
1657 log_name_addr(VERB_ALGO, "try edns1xx0", sq->qbuf+10,
1658 &sq->addr, sq->addrlen);
1659 if(!serviced_udp_send(sq, c->buffer)) {
1660 serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1664 if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1665 /* fragmentation size did not fix it */
1666 sq->status = serviced_query_UDP_EDNS;
1669 if(!(rto=infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1670 sq->zone, sq->zonelen, sq->qtype, -1, sq->last_rtt,
1671 (uint32_t)now.tv_sec)))
1672 log_err("out of memory in UDP exponential backoff");
1673 if(sq->retry < OUTBOUND_UDP_RETRY) {
1674 log_name_addr(VERB_ALGO, "retry query", sq->qbuf+10,
1675 &sq->addr, sq->addrlen);
1676 if(!serviced_udp_send(sq, c->buffer)) {
1677 serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1681 if(rto >= RTT_MAX_TIMEOUT) {
1683 /* UDP does not work, fallback to TCP below */
1685 serviced_callbacks(sq, NETEVENT_TIMEOUT, c, rep);
1688 } else if(error != NETEVENT_NOERROR) {
1689 /* udp returns error (due to no ID or interface available) */
1690 serviced_callbacks(sq, error, c, rep);
1694 if( (sq->status == serviced_query_UDP_EDNS
1695 ||sq->status == serviced_query_UDP_EDNS_FRAG)
1696 && (LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer))
1697 == LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(
1698 ldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOTIMPL)) {
1699 /* try to get an answer by falling back without EDNS */
1700 verbose(VERB_ALGO, "serviced query: attempt without EDNS");
1701 sq->status = serviced_query_UDP_EDNS_fallback;
1703 if(!serviced_udp_send(sq, c->buffer)) {
1704 serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1707 } else if(sq->status == serviced_query_PROBE_EDNS) {
1708 /* probe without EDNS succeeds, so we conclude that this
1709 * host likely has EDNS packets dropped */
1710 log_addr(VERB_DETAIL, "timeouts, concluded that connection to "
1711 "host drops EDNS packets", &sq->addr, sq->addrlen);
1712 /* only store noEDNS in cache if domain is noDNSSEC */
1713 if(!sq->want_dnssec)
1714 if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1715 sq->zone, sq->zonelen, -1, (uint32_t)now.tv_sec)) {
1716 log_err("Out of memory caching no edns for host");
1718 sq->status = serviced_query_UDP;
1719 } else if(sq->status == serviced_query_UDP_EDNS &&
1720 !sq->edns_lame_known) {
1721 /* now we know that edns queries received answers store that */
1722 log_addr(VERB_ALGO, "serviced query: EDNS works for",
1723 &sq->addr, sq->addrlen);
1724 if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1725 sq->zone, sq->zonelen, 0, (uint32_t)now.tv_sec)) {
1726 log_err("Out of memory caching edns works");
1728 sq->edns_lame_known = 1;
1729 } else if(sq->status == serviced_query_UDP_EDNS_fallback &&
1730 !sq->edns_lame_known && (LDNS_RCODE_WIRE(
1731 ldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOERROR ||
1732 LDNS_RCODE_WIRE(ldns_buffer_begin(c->buffer)) ==
1733 LDNS_RCODE_NXDOMAIN || LDNS_RCODE_WIRE(ldns_buffer_begin(
1734 c->buffer)) == LDNS_RCODE_YXDOMAIN)) {
1735 /* the fallback produced a result that looks promising, note
1736 * that this server should be approached without EDNS */
1737 /* only store noEDNS in cache if domain is noDNSSEC */
1738 if(!sq->want_dnssec) {
1739 log_addr(VERB_ALGO, "serviced query: EDNS fails for",
1740 &sq->addr, sq->addrlen);
1741 if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1742 sq->zone, sq->zonelen, -1, (uint32_t)now.tv_sec)) {
1743 log_err("Out of memory caching no edns for host");
1746 log_addr(VERB_ALGO, "serviced query: EDNS fails, but "
1747 "not stored because need DNSSEC for", &sq->addr,
1750 sq->status = serviced_query_UDP;
1752 if(now.tv_sec > sq->last_sent_time.tv_sec ||
1753 (now.tv_sec == sq->last_sent_time.tv_sec &&
1754 now.tv_usec > sq->last_sent_time.tv_usec)) {
1755 /* convert from microseconds to milliseconds */
1756 int roundtime = ((int)now.tv_sec - (int)sq->last_sent_time.tv_sec)*1000
1757 + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1758 verbose(VERB_ALGO, "measured roundtrip at %d msec", roundtime);
1759 log_assert(roundtime >= 0);
1760 /* in case the system hibernated, do not enter a huge value,
1761 * above this value gives trouble with server selection */
1762 if(roundtime < 60000) {
1763 if(!infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1764 sq->zone, sq->zonelen, sq->qtype, roundtime,
1765 sq->last_rtt, (uint32_t)now.tv_sec))
1766 log_err("out of memory noting rtt.");
1769 } /* end of if_!fallback_tcp */
1770 /* perform TC flag check and TCP fallback after updating our
1771 * cache entries for EDNS status and RTT times */
1772 if(LDNS_TC_WIRE(ldns_buffer_begin(c->buffer)) || fallback_tcp) {
1773 /* fallback to TCP */
1774 /* this discards partial UDP contents */
1775 if(sq->status == serviced_query_UDP_EDNS ||
1776 sq->status == serviced_query_UDP_EDNS_FRAG ||
1777 sq->status == serviced_query_UDP_EDNS_fallback)
1778 /* if we have unfinished EDNS_fallback, start again */
1779 sq->status = serviced_query_TCP_EDNS;
1780 else sq->status = serviced_query_TCP;
1781 serviced_tcp_initiate(outnet, sq, c->buffer);
1784 /* yay! an answer */
1785 serviced_callbacks(sq, error, c, rep);
1789 struct serviced_query*
1790 outnet_serviced_query(struct outside_network* outnet,
1791 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
1792 uint16_t flags, int dnssec, int want_dnssec, int tcp_upstream,
1793 int ssl_upstream, struct sockaddr_storage* addr, socklen_t addrlen,
1794 uint8_t* zone, size_t zonelen, comm_point_callback_t* callback,
1795 void* callback_arg, ldns_buffer* buff)
1797 struct serviced_query* sq;
1798 struct service_callback* cb;
1799 serviced_gen_query(buff, qname, qnamelen, qtype, qclass, flags);
1800 sq = lookup_serviced(outnet, buff, dnssec, addr, addrlen);
1801 /* duplicate entries are included in the callback list, because
1802 * there is a counterpart registration by our caller that needs to
1803 * be doubly-removed (with callbacks perhaps). */
1804 if(!(cb = (struct service_callback*)malloc(sizeof(*cb))))
1807 /* make new serviced query entry */
1808 sq = serviced_create(outnet, buff, dnssec, want_dnssec,
1809 tcp_upstream, ssl_upstream, addr, addrlen, zone,
1810 zonelen, (int)qtype);
1815 /* perform first network action */
1816 if(outnet->do_udp && !(tcp_upstream || ssl_upstream)) {
1817 if(!serviced_udp_send(sq, buff)) {
1818 (void)rbtree_delete(outnet->serviced, sq);
1826 if(!serviced_tcp_send(sq, buff)) {
1827 (void)rbtree_delete(outnet->serviced, sq);
1836 /* add callback to list of callbacks */
1838 cb->cb_arg = callback_arg;
1839 cb->next = sq->cblist;
1844 /** remove callback from list */
1846 callback_list_remove(struct serviced_query* sq, void* cb_arg)
1848 struct service_callback** pp = &sq->cblist;
1850 if((*pp)->cb_arg == cb_arg) {
1851 struct service_callback* del = *pp;
1860 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
1864 callback_list_remove(sq, cb_arg);
1865 /* if callbacks() routine scheduled deletion, let it do that */
1866 if(!sq->cblist && !sq->to_be_deleted) {
1867 #ifdef UNBOUND_DEBUG
1870 rbtree_delete(sq->outnet->serviced, sq);
1871 log_assert(rem); /* should be present */
1872 serviced_delete(sq);
1876 /** get memory used by waiting tcp entry (in use or not) */
1878 waiting_tcp_get_mem(struct waiting_tcp* w)
1882 s = sizeof(*w) + w->pkt_len;
1884 s += comm_timer_get_mem(w->timer);
1888 /** get memory used by port if */
1890 if_get_mem(struct port_if* pif)
1894 s = sizeof(*pif) + sizeof(int)*pif->avail_total +
1895 sizeof(struct port_comm*)*pif->maxout;
1896 for(i=0; i<pif->inuse; i++)
1897 s += sizeof(*pif->out[i]) +
1898 comm_point_get_mem(pif->out[i]->cp);
1902 /** get memory used by waiting udp */
1904 waiting_udp_get_mem(struct pending* w)
1907 s = sizeof(*w) + comm_timer_get_mem(w->timer) + w->pkt_len;
1911 size_t outnet_get_mem(struct outside_network* outnet)
1915 struct waiting_tcp* w;
1917 struct serviced_query* sq;
1918 struct service_callback* sb;
1919 struct port_comm* pc;
1920 size_t s = sizeof(*outnet) + sizeof(*outnet->base) +
1921 sizeof(*outnet->udp_buff) +
1922 ldns_buffer_capacity(outnet->udp_buff);
1923 /* second buffer is not ours */
1924 for(pc = outnet->unused_fds; pc; pc = pc->next) {
1925 s += sizeof(*pc) + comm_point_get_mem(pc->cp);
1927 for(k=0; k<outnet->num_ip4; k++)
1928 s += if_get_mem(&outnet->ip4_ifs[k]);
1929 for(k=0; k<outnet->num_ip6; k++)
1930 s += if_get_mem(&outnet->ip6_ifs[k]);
1931 for(u=outnet->udp_wait_first; u; u=u->next_waiting)
1932 s += waiting_udp_get_mem(u);
1934 s += sizeof(struct pending_tcp*)*outnet->num_tcp;
1935 for(i=0; i<outnet->num_tcp; i++) {
1936 s += sizeof(struct pending_tcp);
1937 s += comm_point_get_mem(outnet->tcp_conns[i]->c);
1938 if(outnet->tcp_conns[i]->query)
1939 s += waiting_tcp_get_mem(outnet->tcp_conns[i]->query);
1941 for(w=outnet->tcp_wait_first; w; w = w->next_waiting)
1942 s += waiting_tcp_get_mem(w);
1943 s += sizeof(*outnet->pending);
1944 s += (sizeof(struct pending) + comm_timer_get_mem(NULL)) *
1945 outnet->pending->count;
1946 s += sizeof(*outnet->serviced);
1947 s += outnet->svcd_overhead;
1948 RBTREE_FOR(sq, struct serviced_query*, outnet->serviced) {
1949 s += sizeof(*sq) + sq->qbuflen;
1950 for(sb = sq->cblist; sb; sb = sb->next)
1957 serviced_get_mem(struct serviced_query* sq)
1959 struct service_callback* sb;
1961 s = sizeof(*sq) + sq->qbuflen;
1962 for(sb = sq->cblist; sb; sb = sb->next)
1964 if(sq->status == serviced_query_UDP_EDNS ||
1965 sq->status == serviced_query_UDP ||
1966 sq->status == serviced_query_PROBE_EDNS ||
1967 sq->status == serviced_query_UDP_EDNS_FRAG ||
1968 sq->status == serviced_query_UDP_EDNS_fallback) {
1969 s += sizeof(struct pending);
1970 s += comm_timer_get_mem(NULL);
1972 /* does not have size of the pkt pointer */
1973 /* always has a timer except on malloc failures */
1975 /* these sizes are part of the main outside network mem */
1977 s += sizeof(struct waiting_tcp);
1978 s += comm_timer_get_mem(NULL);