xref: /freebsd/contrib/unbound/services/outside_network.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1 /*
2  * services/outside_network.c - implement sending of queries and wait answer.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file has functions to send queries to authoritative servers and
40  * wait for the pending answer events.
41  */
42 #include "config.h"
43 #include <ctype.h>
44 #ifdef HAVE_SYS_TYPES_H
45 #  include <sys/types.h>
46 #endif
47 #include <sys/time.h>
48 #include "services/outside_network.h"
49 #include "services/listen_dnsport.h"
50 #include "services/cache/infra.h"
51 #include "util/data/msgparse.h"
52 #include "util/data/msgreply.h"
53 #include "util/data/msgencode.h"
54 #include "util/data/dname.h"
55 #include "util/netevent.h"
56 #include "util/log.h"
57 #include "util/net_help.h"
58 #include "util/random.h"
59 #include "util/fptr_wlist.h"
60 #include "sldns/sbuffer.h"
61 #include "dnstap/dnstap.h"
62 #ifdef HAVE_OPENSSL_SSL_H
63 #include <openssl/ssl.h>
64 #endif
65 
66 #ifdef HAVE_NETDB_H
67 #include <netdb.h>
68 #endif
69 #include <fcntl.h>
70 
71 /** number of times to retry making a random ID that is unique. */
72 #define MAX_ID_RETRY 1000
73 /** number of times to retry finding interface, port that can be opened. */
74 #define MAX_PORT_RETRY 10000
75 /** number of retries on outgoing UDP queries */
76 #define OUTBOUND_UDP_RETRY 1
77 
78 /** initiate TCP transaction for serviced query */
79 static void serviced_tcp_initiate(struct serviced_query* sq, sldns_buffer* buff);
80 /** with a fd available, randomize and send UDP */
81 static int randomize_and_send_udp(struct pending* pend, sldns_buffer* packet,
82 	int timeout);
83 
84 /** remove waiting tcp from the outnet waiting list */
85 static void waiting_list_remove(struct outside_network* outnet,
86 	struct waiting_tcp* w);
87 
88 int
89 pending_cmp(const void* key1, const void* key2)
90 {
91 	struct pending *p1 = (struct pending*)key1;
92 	struct pending *p2 = (struct pending*)key2;
93 	if(p1->id < p2->id)
94 		return -1;
95 	if(p1->id > p2->id)
96 		return 1;
97 	log_assert(p1->id == p2->id);
98 	return sockaddr_cmp(&p1->addr, p1->addrlen, &p2->addr, p2->addrlen);
99 }
100 
101 int
102 serviced_cmp(const void* key1, const void* key2)
103 {
104 	struct serviced_query* q1 = (struct serviced_query*)key1;
105 	struct serviced_query* q2 = (struct serviced_query*)key2;
106 	int r;
107 	if(q1->qbuflen < q2->qbuflen)
108 		return -1;
109 	if(q1->qbuflen > q2->qbuflen)
110 		return 1;
111 	log_assert(q1->qbuflen == q2->qbuflen);
112 	log_assert(q1->qbuflen >= 15 /* 10 header, root, type, class */);
113 	/* alternate casing of qname is still the same query */
114 	if((r = memcmp(q1->qbuf, q2->qbuf, 10)) != 0)
115 		return r;
116 	if((r = memcmp(q1->qbuf+q1->qbuflen-4, q2->qbuf+q2->qbuflen-4, 4)) != 0)
117 		return r;
118 	if(q1->dnssec != q2->dnssec) {
119 		if(q1->dnssec < q2->dnssec)
120 			return -1;
121 		return 1;
122 	}
123 	if((r = query_dname_compare(q1->qbuf+10, q2->qbuf+10)) != 0)
124 		return r;
125 	if((r = edns_opt_list_compare(q1->opt_list, q2->opt_list)) != 0)
126 		return r;
127 	return sockaddr_cmp(&q1->addr, q1->addrlen, &q2->addr, q2->addrlen);
128 }
129 
130 /** delete waiting_tcp entry. Does not unlink from waiting list.
131  * @param w: to delete.
132  */
133 static void
134 waiting_tcp_delete(struct waiting_tcp* w)
135 {
136 	if(!w) return;
137 	if(w->timer)
138 		comm_timer_delete(w->timer);
139 	free(w);
140 }
141 
142 /**
143  * Pick random outgoing-interface of that family, and bind it.
144  * port set to 0 so OS picks a port number for us.
145  * if it is the ANY address, do not bind.
146  * @param w: tcp structure with destination address.
147  * @param s: socket fd.
148  * @return false on error, socket closed.
149  */
150 static int
151 pick_outgoing_tcp(struct waiting_tcp* w, int s)
152 {
153 	struct port_if* pi = NULL;
154 	int num;
155 #ifdef INET6
156 	if(addr_is_ip6(&w->addr, w->addrlen))
157 		num = w->outnet->num_ip6;
158 	else
159 #endif
160 		num = w->outnet->num_ip4;
161 	if(num == 0) {
162 		log_err("no TCP outgoing interfaces of family");
163 		log_addr(VERB_OPS, "for addr", &w->addr, w->addrlen);
164 #ifndef USE_WINSOCK
165 		close(s);
166 #else
167 		closesocket(s);
168 #endif
169 		return 0;
170 	}
171 #ifdef INET6
172 	if(addr_is_ip6(&w->addr, w->addrlen))
173 		pi = &w->outnet->ip6_ifs[ub_random_max(w->outnet->rnd, num)];
174 	else
175 #endif
176 		pi = &w->outnet->ip4_ifs[ub_random_max(w->outnet->rnd, num)];
177 	log_assert(pi);
178 	if(addr_is_any(&pi->addr, pi->addrlen)) {
179 		/* binding to the ANY interface is for listening sockets */
180 		return 1;
181 	}
182 	/* set port to 0 */
183 	if(addr_is_ip6(&pi->addr, pi->addrlen))
184 		((struct sockaddr_in6*)&pi->addr)->sin6_port = 0;
185 	else	((struct sockaddr_in*)&pi->addr)->sin_port = 0;
186 	if(bind(s, (struct sockaddr*)&pi->addr, pi->addrlen) != 0) {
187 #ifndef USE_WINSOCK
188 		log_err("outgoing tcp: bind: %s", strerror(errno));
189 		close(s);
190 #else
191 		log_err("outgoing tcp: bind: %s",
192 			wsa_strerror(WSAGetLastError()));
193 		closesocket(s);
194 #endif
195 		return 0;
196 	}
197 	log_addr(VERB_ALGO, "tcp bound to src", &pi->addr, pi->addrlen);
198 	return 1;
199 }
200 
201 /** use next free buffer to service a tcp query */
202 static int
203 outnet_tcp_take_into_use(struct waiting_tcp* w, uint8_t* pkt, size_t pkt_len)
204 {
205 	struct pending_tcp* pend = w->outnet->tcp_free;
206 	int s;
207 	log_assert(pend);
208 	log_assert(pkt);
209 	log_assert(w->addrlen > 0);
210 	/* open socket */
211 #ifdef INET6
212 	if(addr_is_ip6(&w->addr, w->addrlen))
213 		s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
214 	else
215 #endif
216 		s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
217 	if(s == -1) {
218 #ifndef USE_WINSOCK
219 		log_err_addr("outgoing tcp: socket", strerror(errno),
220 			&w->addr, w->addrlen);
221 #else
222 		log_err_addr("outgoing tcp: socket",
223 			wsa_strerror(WSAGetLastError()), &w->addr, w->addrlen);
224 #endif
225 		return 0;
226 	}
227 
228 	if (w->outnet->tcp_mss > 0) {
229 #if defined(IPPROTO_TCP) && defined(TCP_MAXSEG)
230 		if(setsockopt(s, IPPROTO_TCP, TCP_MAXSEG,
231 			(void*)&w->outnet->tcp_mss,
232 			(socklen_t)sizeof(w->outnet->tcp_mss)) < 0) {
233 			verbose(VERB_ALGO, "outgoing tcp:"
234 				" setsockopt(.. SO_REUSEADDR ..) failed");
235 		}
236 #else
237 		verbose(VERB_ALGO, "outgoing tcp:"
238 			" setsockopt(TCP_MAXSEG) unsupported");
239 #endif /* defined(IPPROTO_TCP) && defined(TCP_MAXSEG) */
240 	}
241 
242 	if(!pick_outgoing_tcp(w, s))
243 		return 0;
244 
245 	fd_set_nonblock(s);
246 	if(connect(s, (struct sockaddr*)&w->addr, w->addrlen) == -1) {
247 #ifndef USE_WINSOCK
248 #ifdef EINPROGRESS
249 		if(errno != EINPROGRESS) {
250 #else
251 		if(1) {
252 #endif
253 			if(tcp_connect_errno_needs_log(
254 				(struct sockaddr*)&w->addr, w->addrlen))
255 				log_err_addr("outgoing tcp: connect",
256 					strerror(errno), &w->addr, w->addrlen);
257 			close(s);
258 #else /* USE_WINSOCK */
259 		if(WSAGetLastError() != WSAEINPROGRESS &&
260 			WSAGetLastError() != WSAEWOULDBLOCK) {
261 			closesocket(s);
262 #endif
263 			return 0;
264 		}
265 	}
266 	if(w->outnet->sslctx && w->ssl_upstream) {
267 		pend->c->ssl = outgoing_ssl_fd(w->outnet->sslctx, s);
268 		if(!pend->c->ssl) {
269 			pend->c->fd = s;
270 			comm_point_close(pend->c);
271 			return 0;
272 		}
273 #ifdef USE_WINSOCK
274 		comm_point_tcp_win_bio_cb(pend->c, pend->c->ssl);
275 #endif
276 		pend->c->ssl_shake_state = comm_ssl_shake_write;
277 	}
278 	w->pkt = NULL;
279 	w->next_waiting = (void*)pend;
280 	pend->id = LDNS_ID_WIRE(pkt);
281 	w->outnet->num_tcp_outgoing++;
282 	w->outnet->tcp_free = pend->next_free;
283 	pend->next_free = NULL;
284 	pend->query = w;
285 	pend->c->repinfo.addrlen = w->addrlen;
286 	memcpy(&pend->c->repinfo.addr, &w->addr, w->addrlen);
287 	sldns_buffer_clear(pend->c->buffer);
288 	sldns_buffer_write(pend->c->buffer, pkt, pkt_len);
289 	sldns_buffer_flip(pend->c->buffer);
290 	pend->c->tcp_is_reading = 0;
291 	pend->c->tcp_byte_count = 0;
292 	comm_point_start_listening(pend->c, s, -1);
293 	return 1;
294 }
295 
296 /** see if buffers can be used to service TCP queries */
297 static void
298 use_free_buffer(struct outside_network* outnet)
299 {
300 	struct waiting_tcp* w;
301 	while(outnet->tcp_free && outnet->tcp_wait_first
302 		&& !outnet->want_to_quit) {
303 		w = outnet->tcp_wait_first;
304 		outnet->tcp_wait_first = w->next_waiting;
305 		if(outnet->tcp_wait_last == w)
306 			outnet->tcp_wait_last = NULL;
307 		if(!outnet_tcp_take_into_use(w, w->pkt, w->pkt_len)) {
308 			comm_point_callback_t* cb = w->cb;
309 			void* cb_arg = w->cb_arg;
310 			waiting_tcp_delete(w);
311 			fptr_ok(fptr_whitelist_pending_tcp(cb));
312 			(void)(*cb)(NULL, cb_arg, NETEVENT_CLOSED, NULL);
313 		}
314 	}
315 }
316 
317 /** decomission a tcp buffer, closes commpoint and frees waiting_tcp entry */
318 static void
319 decomission_pending_tcp(struct outside_network* outnet,
320 	struct pending_tcp* pend)
321 {
322 	if(pend->c->ssl) {
323 #ifdef HAVE_SSL
324 		SSL_shutdown(pend->c->ssl);
325 		SSL_free(pend->c->ssl);
326 		pend->c->ssl = NULL;
327 #endif
328 	}
329 	comm_point_close(pend->c);
330 	pend->next_free = outnet->tcp_free;
331 	outnet->tcp_free = pend;
332 	waiting_tcp_delete(pend->query);
333 	pend->query = NULL;
334 	use_free_buffer(outnet);
335 }
336 
337 int
338 outnet_tcp_cb(struct comm_point* c, void* arg, int error,
339 	struct comm_reply *reply_info)
340 {
341 	struct pending_tcp* pend = (struct pending_tcp*)arg;
342 	struct outside_network* outnet = pend->query->outnet;
343 	verbose(VERB_ALGO, "outnettcp cb");
344 	if(error != NETEVENT_NOERROR) {
345 		verbose(VERB_QUERY, "outnettcp got tcp error %d", error);
346 		/* pass error below and exit */
347 	} else {
348 		/* check ID */
349 		if(sldns_buffer_limit(c->buffer) < sizeof(uint16_t) ||
350 			LDNS_ID_WIRE(sldns_buffer_begin(c->buffer))!=pend->id) {
351 			log_addr(VERB_QUERY,
352 				"outnettcp: bad ID in reply, from:",
353 				&pend->query->addr, pend->query->addrlen);
354 			error = NETEVENT_CLOSED;
355 		}
356 	}
357 	fptr_ok(fptr_whitelist_pending_tcp(pend->query->cb));
358 	(void)(*pend->query->cb)(c, pend->query->cb_arg, error, reply_info);
359 	decomission_pending_tcp(outnet, pend);
360 	return 0;
361 }
362 
363 /** lower use count on pc, see if it can be closed */
364 static void
365 portcomm_loweruse(struct outside_network* outnet, struct port_comm* pc)
366 {
367 	struct port_if* pif;
368 	pc->num_outstanding--;
369 	if(pc->num_outstanding > 0) {
370 		return;
371 	}
372 	/* close it and replace in unused list */
373 	verbose(VERB_ALGO, "close of port %d", pc->number);
374 	comm_point_close(pc->cp);
375 	pif = pc->pif;
376 	log_assert(pif->inuse > 0);
377 	pif->avail_ports[pif->avail_total - pif->inuse] = pc->number;
378 	pif->inuse--;
379 	pif->out[pc->index] = pif->out[pif->inuse];
380 	pif->out[pc->index]->index = pc->index;
381 	pc->next = outnet->unused_fds;
382 	outnet->unused_fds = pc;
383 }
384 
385 /** try to send waiting UDP queries */
386 static void
387 outnet_send_wait_udp(struct outside_network* outnet)
388 {
389 	struct pending* pend;
390 	/* process waiting queries */
391 	while(outnet->udp_wait_first && outnet->unused_fds
392 		&& !outnet->want_to_quit) {
393 		pend = outnet->udp_wait_first;
394 		outnet->udp_wait_first = pend->next_waiting;
395 		if(!pend->next_waiting) outnet->udp_wait_last = NULL;
396 		sldns_buffer_clear(outnet->udp_buff);
397 		sldns_buffer_write(outnet->udp_buff, pend->pkt, pend->pkt_len);
398 		sldns_buffer_flip(outnet->udp_buff);
399 		free(pend->pkt); /* freeing now makes get_mem correct */
400 		pend->pkt = NULL;
401 		pend->pkt_len = 0;
402 		if(!randomize_and_send_udp(pend, outnet->udp_buff,
403 			pend->timeout)) {
404 			/* callback error on pending */
405 			if(pend->cb) {
406 				fptr_ok(fptr_whitelist_pending_udp(pend->cb));
407 				(void)(*pend->cb)(outnet->unused_fds->cp, pend->cb_arg,
408 					NETEVENT_CLOSED, NULL);
409 			}
410 			pending_delete(outnet, pend);
411 		}
412 	}
413 }
414 
415 int
416 outnet_udp_cb(struct comm_point* c, void* arg, int error,
417 	struct comm_reply *reply_info)
418 {
419 	struct outside_network* outnet = (struct outside_network*)arg;
420 	struct pending key;
421 	struct pending* p;
422 	verbose(VERB_ALGO, "answer cb");
423 
424 	if(error != NETEVENT_NOERROR) {
425 		verbose(VERB_QUERY, "outnetudp got udp error %d", error);
426 		return 0;
427 	}
428 	if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
429 		verbose(VERB_QUERY, "outnetudp udp too short");
430 		return 0;
431 	}
432 	log_assert(reply_info);
433 
434 	/* setup lookup key */
435 	key.id = (unsigned)LDNS_ID_WIRE(sldns_buffer_begin(c->buffer));
436 	memcpy(&key.addr, &reply_info->addr, reply_info->addrlen);
437 	key.addrlen = reply_info->addrlen;
438 	verbose(VERB_ALGO, "Incoming reply id = %4.4x", key.id);
439 	log_addr(VERB_ALGO, "Incoming reply addr =",
440 		&reply_info->addr, reply_info->addrlen);
441 
442 	/* find it, see if this thing is a valid query response */
443 	verbose(VERB_ALGO, "lookup size is %d entries", (int)outnet->pending->count);
444 	p = (struct pending*)rbtree_search(outnet->pending, &key);
445 	if(!p) {
446 		verbose(VERB_QUERY, "received unwanted or unsolicited udp reply dropped.");
447 		log_buf(VERB_ALGO, "dropped message", c->buffer);
448 		outnet->unwanted_replies++;
449 		if(outnet->unwanted_threshold && ++outnet->unwanted_total
450 			>= outnet->unwanted_threshold) {
451 			log_warn("unwanted reply total reached threshold (%u)"
452 				" you may be under attack."
453 				" defensive action: clearing the cache",
454 				(unsigned)outnet->unwanted_threshold);
455 			fptr_ok(fptr_whitelist_alloc_cleanup(
456 				outnet->unwanted_action));
457 			(*outnet->unwanted_action)(outnet->unwanted_param);
458 			outnet->unwanted_total = 0;
459 		}
460 		return 0;
461 	}
462 
463 	verbose(VERB_ALGO, "received udp reply.");
464 	log_buf(VERB_ALGO, "udp message", c->buffer);
465 	if(p->pc->cp != c) {
466 		verbose(VERB_QUERY, "received reply id,addr on wrong port. "
467 			"dropped.");
468 		outnet->unwanted_replies++;
469 		if(outnet->unwanted_threshold && ++outnet->unwanted_total
470 			>= outnet->unwanted_threshold) {
471 			log_warn("unwanted reply total reached threshold (%u)"
472 				" you may be under attack."
473 				" defensive action: clearing the cache",
474 				(unsigned)outnet->unwanted_threshold);
475 			fptr_ok(fptr_whitelist_alloc_cleanup(
476 				outnet->unwanted_action));
477 			(*outnet->unwanted_action)(outnet->unwanted_param);
478 			outnet->unwanted_total = 0;
479 		}
480 		return 0;
481 	}
482 	comm_timer_disable(p->timer);
483 	verbose(VERB_ALGO, "outnet handle udp reply");
484 	/* delete from tree first in case callback creates a retry */
485 	(void)rbtree_delete(outnet->pending, p->node.key);
486 	if(p->cb) {
487 		fptr_ok(fptr_whitelist_pending_udp(p->cb));
488 		(void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_NOERROR, reply_info);
489 	}
490 	portcomm_loweruse(outnet, p->pc);
491 	pending_delete(NULL, p);
492 	outnet_send_wait_udp(outnet);
493 	return 0;
494 }
495 
496 /** calculate number of ip4 and ip6 interfaces*/
497 static void
498 calc_num46(char** ifs, int num_ifs, int do_ip4, int do_ip6,
499 	int* num_ip4, int* num_ip6)
500 {
501 	int i;
502 	*num_ip4 = 0;
503 	*num_ip6 = 0;
504 	if(num_ifs <= 0) {
505 		if(do_ip4)
506 			*num_ip4 = 1;
507 		if(do_ip6)
508 			*num_ip6 = 1;
509 		return;
510 	}
511 	for(i=0; i<num_ifs; i++)
512 	{
513 		if(str_is_ip6(ifs[i])) {
514 			if(do_ip6)
515 				(*num_ip6)++;
516 		} else {
517 			if(do_ip4)
518 				(*num_ip4)++;
519 		}
520 	}
521 
522 }
523 
524 void
525 pending_udp_timer_delay_cb(void* arg)
526 {
527 	struct pending* p = (struct pending*)arg;
528 	struct outside_network* outnet = p->outnet;
529 	verbose(VERB_ALGO, "timeout udp with delay");
530 	portcomm_loweruse(outnet, p->pc);
531 	pending_delete(outnet, p);
532 	outnet_send_wait_udp(outnet);
533 }
534 
535 void
536 pending_udp_timer_cb(void *arg)
537 {
538 	struct pending* p = (struct pending*)arg;
539 	struct outside_network* outnet = p->outnet;
540 	/* it timed out */
541 	verbose(VERB_ALGO, "timeout udp");
542 	if(p->cb) {
543 		fptr_ok(fptr_whitelist_pending_udp(p->cb));
544 		(void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_TIMEOUT, NULL);
545 	}
546 	/* if delayclose, keep port open for a longer time.
547 	 * But if the udpwaitlist exists, then we are struggling to
548 	 * keep up with demand for sockets, so do not wait, but service
549 	 * the customer (customer service more important than portICMPs) */
550 	if(outnet->delayclose && !outnet->udp_wait_first) {
551 		p->cb = NULL;
552 		p->timer->callback = &pending_udp_timer_delay_cb;
553 		comm_timer_set(p->timer, &outnet->delay_tv);
554 		return;
555 	}
556 	portcomm_loweruse(outnet, p->pc);
557 	pending_delete(outnet, p);
558 	outnet_send_wait_udp(outnet);
559 }
560 
561 /** create pending_tcp buffers */
562 static int
563 create_pending_tcp(struct outside_network* outnet, size_t bufsize)
564 {
565 	size_t i;
566 	if(outnet->num_tcp == 0)
567 		return 1; /* no tcp needed, nothing to do */
568 	if(!(outnet->tcp_conns = (struct pending_tcp **)calloc(
569 			outnet->num_tcp, sizeof(struct pending_tcp*))))
570 		return 0;
571 	for(i=0; i<outnet->num_tcp; i++) {
572 		if(!(outnet->tcp_conns[i] = (struct pending_tcp*)calloc(1,
573 			sizeof(struct pending_tcp))))
574 			return 0;
575 		outnet->tcp_conns[i]->next_free = outnet->tcp_free;
576 		outnet->tcp_free = outnet->tcp_conns[i];
577 		outnet->tcp_conns[i]->c = comm_point_create_tcp_out(
578 			outnet->base, bufsize, outnet_tcp_cb,
579 			outnet->tcp_conns[i]);
580 		if(!outnet->tcp_conns[i]->c)
581 			return 0;
582 	}
583 	return 1;
584 }
585 
586 /** setup an outgoing interface, ready address */
587 static int setup_if(struct port_if* pif, const char* addrstr,
588 	int* avail, int numavail, size_t numfd)
589 {
590 	pif->avail_total = numavail;
591 	pif->avail_ports = (int*)memdup(avail, (size_t)numavail*sizeof(int));
592 	if(!pif->avail_ports)
593 		return 0;
594 	if(!ipstrtoaddr(addrstr, UNBOUND_DNS_PORT, &pif->addr, &pif->addrlen))
595 		return 0;
596 	pif->maxout = (int)numfd;
597 	pif->inuse = 0;
598 	pif->out = (struct port_comm**)calloc(numfd,
599 		sizeof(struct port_comm*));
600 	if(!pif->out)
601 		return 0;
602 	return 1;
603 }
604 
605 struct outside_network*
606 outside_network_create(struct comm_base *base, size_t bufsize,
607 	size_t num_ports, char** ifs, int num_ifs, int do_ip4,
608 	int do_ip6, size_t num_tcp, struct infra_cache* infra,
609 	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
610 	int numavailports, size_t unwanted_threshold, int tcp_mss,
611 	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
612 	void* sslctx, int delayclose, struct dt_env* dtenv)
613 {
614 	struct outside_network* outnet = (struct outside_network*)
615 		calloc(1, sizeof(struct outside_network));
616 	size_t k;
617 	if(!outnet) {
618 		log_err("malloc failed");
619 		return NULL;
620 	}
621 	comm_base_timept(base, &outnet->now_secs, &outnet->now_tv);
622 	outnet->base = base;
623 	outnet->num_tcp = num_tcp;
624 	outnet->num_tcp_outgoing = 0;
625 	outnet->infra = infra;
626 	outnet->rnd = rnd;
627 	outnet->sslctx = sslctx;
628 #ifdef USE_DNSTAP
629 	outnet->dtenv = dtenv;
630 #else
631 	(void)dtenv;
632 #endif
633 	outnet->svcd_overhead = 0;
634 	outnet->want_to_quit = 0;
635 	outnet->unwanted_threshold = unwanted_threshold;
636 	outnet->unwanted_action = unwanted_action;
637 	outnet->unwanted_param = unwanted_param;
638 	outnet->use_caps_for_id = use_caps_for_id;
639 	outnet->do_udp = do_udp;
640 	outnet->tcp_mss = tcp_mss;
641 #ifndef S_SPLINT_S
642 	if(delayclose) {
643 		outnet->delayclose = 1;
644 		outnet->delay_tv.tv_sec = delayclose/1000;
645 		outnet->delay_tv.tv_usec = (delayclose%1000)*1000;
646 	}
647 #endif
648 	if(numavailports == 0) {
649 		log_err("no outgoing ports available");
650 		outside_network_delete(outnet);
651 		return NULL;
652 	}
653 #ifndef INET6
654 	do_ip6 = 0;
655 #endif
656 	calc_num46(ifs, num_ifs, do_ip4, do_ip6,
657 		&outnet->num_ip4, &outnet->num_ip6);
658 	if(outnet->num_ip4 != 0) {
659 		if(!(outnet->ip4_ifs = (struct port_if*)calloc(
660 			(size_t)outnet->num_ip4, sizeof(struct port_if)))) {
661 			log_err("malloc failed");
662 			outside_network_delete(outnet);
663 			return NULL;
664 		}
665 	}
666 	if(outnet->num_ip6 != 0) {
667 		if(!(outnet->ip6_ifs = (struct port_if*)calloc(
668 			(size_t)outnet->num_ip6, sizeof(struct port_if)))) {
669 			log_err("malloc failed");
670 			outside_network_delete(outnet);
671 			return NULL;
672 		}
673 	}
674 	if(	!(outnet->udp_buff = sldns_buffer_new(bufsize)) ||
675 		!(outnet->pending = rbtree_create(pending_cmp)) ||
676 		!(outnet->serviced = rbtree_create(serviced_cmp)) ||
677 		!create_pending_tcp(outnet, bufsize)) {
678 		log_err("malloc failed");
679 		outside_network_delete(outnet);
680 		return NULL;
681 	}
682 
683 	/* allocate commpoints */
684 	for(k=0; k<num_ports; k++) {
685 		struct port_comm* pc;
686 		pc = (struct port_comm*)calloc(1, sizeof(*pc));
687 		if(!pc) {
688 			log_err("malloc failed");
689 			outside_network_delete(outnet);
690 			return NULL;
691 		}
692 		pc->cp = comm_point_create_udp(outnet->base, -1,
693 			outnet->udp_buff, outnet_udp_cb, outnet);
694 		if(!pc->cp) {
695 			log_err("malloc failed");
696 			free(pc);
697 			outside_network_delete(outnet);
698 			return NULL;
699 		}
700 		pc->next = outnet->unused_fds;
701 		outnet->unused_fds = pc;
702 	}
703 
704 	/* allocate interfaces */
705 	if(num_ifs == 0) {
706 		if(do_ip4 && !setup_if(&outnet->ip4_ifs[0], "0.0.0.0",
707 			availports, numavailports, num_ports)) {
708 			log_err("malloc failed");
709 			outside_network_delete(outnet);
710 			return NULL;
711 		}
712 		if(do_ip6 && !setup_if(&outnet->ip6_ifs[0], "::",
713 			availports, numavailports, num_ports)) {
714 			log_err("malloc failed");
715 			outside_network_delete(outnet);
716 			return NULL;
717 		}
718 	} else {
719 		size_t done_4 = 0, done_6 = 0;
720 		int i;
721 		for(i=0; i<num_ifs; i++) {
722 			if(str_is_ip6(ifs[i]) && do_ip6) {
723 				if(!setup_if(&outnet->ip6_ifs[done_6], ifs[i],
724 					availports, numavailports, num_ports)){
725 					log_err("malloc failed");
726 					outside_network_delete(outnet);
727 					return NULL;
728 				}
729 				done_6++;
730 			}
731 			if(!str_is_ip6(ifs[i]) && do_ip4) {
732 				if(!setup_if(&outnet->ip4_ifs[done_4], ifs[i],
733 					availports, numavailports, num_ports)){
734 					log_err("malloc failed");
735 					outside_network_delete(outnet);
736 					return NULL;
737 				}
738 				done_4++;
739 			}
740 		}
741 	}
742 	return outnet;
743 }
744 
745 /** helper pending delete */
746 static void
747 pending_node_del(rbnode_t* node, void* arg)
748 {
749 	struct pending* pend = (struct pending*)node;
750 	struct outside_network* outnet = (struct outside_network*)arg;
751 	pending_delete(outnet, pend);
752 }
753 
754 /** helper serviced delete */
755 static void
756 serviced_node_del(rbnode_t* node, void* ATTR_UNUSED(arg))
757 {
758 	struct serviced_query* sq = (struct serviced_query*)node;
759 	struct service_callback* p = sq->cblist, *np;
760 	free(sq->qbuf);
761 	free(sq->zone);
762 	edns_opt_list_free(sq->opt_list);
763 	while(p) {
764 		np = p->next;
765 		free(p);
766 		p = np;
767 	}
768 	free(sq);
769 }
770 
771 void
772 outside_network_quit_prepare(struct outside_network* outnet)
773 {
774 	if(!outnet)
775 		return;
776 	/* prevent queued items from being sent */
777 	outnet->want_to_quit = 1;
778 }
779 
780 void
781 outside_network_delete(struct outside_network* outnet)
782 {
783 	if(!outnet)
784 		return;
785 	outnet->want_to_quit = 1;
786 	/* check every element, since we can be called on malloc error */
787 	if(outnet->pending) {
788 		/* free pending elements, but do no unlink from tree. */
789 		traverse_postorder(outnet->pending, pending_node_del, NULL);
790 		free(outnet->pending);
791 	}
792 	if(outnet->serviced) {
793 		traverse_postorder(outnet->serviced, serviced_node_del, NULL);
794 		free(outnet->serviced);
795 	}
796 	if(outnet->udp_buff)
797 		sldns_buffer_free(outnet->udp_buff);
798 	if(outnet->unused_fds) {
799 		struct port_comm* p = outnet->unused_fds, *np;
800 		while(p) {
801 			np = p->next;
802 			comm_point_delete(p->cp);
803 			free(p);
804 			p = np;
805 		}
806 		outnet->unused_fds = NULL;
807 	}
808 	if(outnet->ip4_ifs) {
809 		int i, k;
810 		for(i=0; i<outnet->num_ip4; i++) {
811 			for(k=0; k<outnet->ip4_ifs[i].inuse; k++) {
812 				struct port_comm* pc = outnet->ip4_ifs[i].
813 					out[k];
814 				comm_point_delete(pc->cp);
815 				free(pc);
816 			}
817 			free(outnet->ip4_ifs[i].avail_ports);
818 			free(outnet->ip4_ifs[i].out);
819 		}
820 		free(outnet->ip4_ifs);
821 	}
822 	if(outnet->ip6_ifs) {
823 		int i, k;
824 		for(i=0; i<outnet->num_ip6; i++) {
825 			for(k=0; k<outnet->ip6_ifs[i].inuse; k++) {
826 				struct port_comm* pc = outnet->ip6_ifs[i].
827 					out[k];
828 				comm_point_delete(pc->cp);
829 				free(pc);
830 			}
831 			free(outnet->ip6_ifs[i].avail_ports);
832 			free(outnet->ip6_ifs[i].out);
833 		}
834 		free(outnet->ip6_ifs);
835 	}
836 	if(outnet->tcp_conns) {
837 		size_t i;
838 		for(i=0; i<outnet->num_tcp; i++)
839 			if(outnet->tcp_conns[i]) {
840 				comm_point_delete(outnet->tcp_conns[i]->c);
841 				waiting_tcp_delete(outnet->tcp_conns[i]->query);
842 				free(outnet->tcp_conns[i]);
843 			}
844 		free(outnet->tcp_conns);
845 	}
846 	if(outnet->tcp_wait_first) {
847 		struct waiting_tcp* p = outnet->tcp_wait_first, *np;
848 		while(p) {
849 			np = p->next_waiting;
850 			waiting_tcp_delete(p);
851 			p = np;
852 		}
853 	}
854 	if(outnet->udp_wait_first) {
855 		struct pending* p = outnet->udp_wait_first, *np;
856 		while(p) {
857 			np = p->next_waiting;
858 			pending_delete(NULL, p);
859 			p = np;
860 		}
861 	}
862 	free(outnet);
863 }
864 
865 void
866 pending_delete(struct outside_network* outnet, struct pending* p)
867 {
868 	if(!p)
869 		return;
870 	if(outnet && outnet->udp_wait_first &&
871 		(p->next_waiting || p == outnet->udp_wait_last) ) {
872 		/* delete from waiting list, if it is in the waiting list */
873 		struct pending* prev = NULL, *x = outnet->udp_wait_first;
874 		while(x && x != p) {
875 			prev = x;
876 			x = x->next_waiting;
877 		}
878 		if(x) {
879 			log_assert(x == p);
880 			if(prev)
881 				prev->next_waiting = p->next_waiting;
882 			else	outnet->udp_wait_first = p->next_waiting;
883 			if(outnet->udp_wait_last == p)
884 				outnet->udp_wait_last = prev;
885 		}
886 	}
887 	if(outnet) {
888 		(void)rbtree_delete(outnet->pending, p->node.key);
889 	}
890 	if(p->timer)
891 		comm_timer_delete(p->timer);
892 	free(p->pkt);
893 	free(p);
894 }
895 
896 /**
897  * Try to open a UDP socket for outgoing communication.
898  * Sets sockets options as needed.
899  * @param addr: socket address.
900  * @param addrlen: length of address.
901  * @param port: port override for addr.
902  * @param inuse: if -1 is returned, this bool means the port was in use.
903  * @return fd or -1
904  */
905 static int
906 udp_sockport(struct sockaddr_storage* addr, socklen_t addrlen, int port,
907 	int* inuse)
908 {
909 	int fd, noproto;
910 	if(addr_is_ip6(addr, addrlen)) {
911 		struct sockaddr_in6* sa = (struct sockaddr_in6*)addr;
912 		sa->sin6_port = (in_port_t)htons((uint16_t)port);
913 		fd = create_udp_sock(AF_INET6, SOCK_DGRAM,
914 			(struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
915 			0, 0, 0, NULL, 0, 0);
916 	} else {
917 		struct sockaddr_in* sa = (struct sockaddr_in*)addr;
918 		sa->sin_port = (in_port_t)htons((uint16_t)port);
919 		fd = create_udp_sock(AF_INET, SOCK_DGRAM,
920 			(struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
921 			0, 0, 0, NULL, 0, 0);
922 	}
923 	return fd;
924 }
925 
926 /** Select random ID */
927 static int
928 select_id(struct outside_network* outnet, struct pending* pend,
929 	sldns_buffer* packet)
930 {
931 	int id_tries = 0;
932 	pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
933 	LDNS_ID_SET(sldns_buffer_begin(packet), pend->id);
934 
935 	/* insert in tree */
936 	pend->node.key = pend;
937 	while(!rbtree_insert(outnet->pending, &pend->node)) {
938 		/* change ID to avoid collision */
939 		pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
940 		LDNS_ID_SET(sldns_buffer_begin(packet), pend->id);
941 		id_tries++;
942 		if(id_tries == MAX_ID_RETRY) {
943 			pend->id=99999; /* non existant ID */
944 			log_err("failed to generate unique ID, drop msg");
945 			return 0;
946 		}
947 	}
948 	verbose(VERB_ALGO, "inserted new pending reply id=%4.4x", pend->id);
949 	return 1;
950 }
951 
952 /** Select random interface and port */
953 static int
954 select_ifport(struct outside_network* outnet, struct pending* pend,
955 	int num_if, struct port_if* ifs)
956 {
957 	int my_if, my_port, fd, portno, inuse, tries=0;
958 	struct port_if* pif;
959 	/* randomly select interface and port */
960 	if(num_if == 0) {
961 		verbose(VERB_QUERY, "Need to send query but have no "
962 			"outgoing interfaces of that family");
963 		return 0;
964 	}
965 	log_assert(outnet->unused_fds);
966 	tries = 0;
967 	while(1) {
968 		my_if = ub_random_max(outnet->rnd, num_if);
969 		pif = &ifs[my_if];
970 		my_port = ub_random_max(outnet->rnd, pif->avail_total);
971 		if(my_port < pif->inuse) {
972 			/* port already open */
973 			pend->pc = pif->out[my_port];
974 			verbose(VERB_ALGO, "using UDP if=%d port=%d",
975 				my_if, pend->pc->number);
976 			break;
977 		}
978 		/* try to open new port, if fails, loop to try again */
979 		log_assert(pif->inuse < pif->maxout);
980 		portno = pif->avail_ports[my_port - pif->inuse];
981 		fd = udp_sockport(&pif->addr, pif->addrlen, portno, &inuse);
982 		if(fd == -1 && !inuse) {
983 			/* nonrecoverable error making socket */
984 			return 0;
985 		}
986 		if(fd != -1) {
987 			verbose(VERB_ALGO, "opened UDP if=%d port=%d",
988 				my_if, portno);
989 			/* grab fd */
990 			pend->pc = outnet->unused_fds;
991 			outnet->unused_fds = pend->pc->next;
992 
993 			/* setup portcomm */
994 			pend->pc->next = NULL;
995 			pend->pc->number = portno;
996 			pend->pc->pif = pif;
997 			pend->pc->index = pif->inuse;
998 			pend->pc->num_outstanding = 0;
999 			comm_point_start_listening(pend->pc->cp, fd, -1);
1000 
1001 			/* grab port in interface */
1002 			pif->out[pif->inuse] = pend->pc;
1003 			pif->avail_ports[my_port - pif->inuse] =
1004 				pif->avail_ports[pif->avail_total-pif->inuse-1];
1005 			pif->inuse++;
1006 			break;
1007 		}
1008 		/* failed, already in use */
1009 		verbose(VERB_QUERY, "port %d in use, trying another", portno);
1010 		tries++;
1011 		if(tries == MAX_PORT_RETRY) {
1012 			log_err("failed to find an open port, drop msg");
1013 			return 0;
1014 		}
1015 	}
1016 	log_assert(pend->pc);
1017 	pend->pc->num_outstanding++;
1018 
1019 	return 1;
1020 }
1021 
1022 static int
1023 randomize_and_send_udp(struct pending* pend, sldns_buffer* packet, int timeout)
1024 {
1025 	struct timeval tv;
1026 	struct outside_network* outnet = pend->sq->outnet;
1027 
1028 	/* select id */
1029 	if(!select_id(outnet, pend, packet)) {
1030 		return 0;
1031 	}
1032 
1033 	/* select src_if, port */
1034 	if(addr_is_ip6(&pend->addr, pend->addrlen)) {
1035 		if(!select_ifport(outnet, pend,
1036 			outnet->num_ip6, outnet->ip6_ifs))
1037 			return 0;
1038 	} else {
1039 		if(!select_ifport(outnet, pend,
1040 			outnet->num_ip4, outnet->ip4_ifs))
1041 			return 0;
1042 	}
1043 	log_assert(pend->pc && pend->pc->cp);
1044 
1045 	/* send it over the commlink */
1046 	if(!comm_point_send_udp_msg(pend->pc->cp, packet,
1047 		(struct sockaddr*)&pend->addr, pend->addrlen)) {
1048 		portcomm_loweruse(outnet, pend->pc);
1049 		return 0;
1050 	}
1051 
1052 	/* system calls to set timeout after sending UDP to make roundtrip
1053 	   smaller. */
1054 #ifndef S_SPLINT_S
1055 	tv.tv_sec = timeout/1000;
1056 	tv.tv_usec = (timeout%1000)*1000;
1057 #endif
1058 	comm_timer_set(pend->timer, &tv);
1059 
1060 #ifdef USE_DNSTAP
1061 	if(outnet->dtenv &&
1062 	   (outnet->dtenv->log_resolver_query_messages ||
1063 	    outnet->dtenv->log_forwarder_query_messages))
1064 		dt_msg_send_outside_query(outnet->dtenv, &pend->addr, comm_udp,
1065 		pend->sq->zone, pend->sq->zonelen, packet);
1066 #endif
1067 	return 1;
1068 }
1069 
1070 struct pending*
1071 pending_udp_query(struct serviced_query* sq, struct sldns_buffer* packet,
1072 	int timeout, comm_point_callback_t* cb, void* cb_arg)
1073 {
1074 	struct pending* pend = (struct pending*)calloc(1, sizeof(*pend));
1075 	if(!pend) return NULL;
1076 	pend->outnet = sq->outnet;
1077 	pend->sq = sq;
1078 	pend->addrlen = sq->addrlen;
1079 	memmove(&pend->addr, &sq->addr, sq->addrlen);
1080 	pend->cb = cb;
1081 	pend->cb_arg = cb_arg;
1082 	pend->node.key = pend;
1083 	pend->timer = comm_timer_create(sq->outnet->base, pending_udp_timer_cb,
1084 		pend);
1085 	if(!pend->timer) {
1086 		free(pend);
1087 		return NULL;
1088 	}
1089 
1090 	if(sq->outnet->unused_fds == NULL) {
1091 		/* no unused fd, cannot create a new port (randomly) */
1092 		verbose(VERB_ALGO, "no fds available, udp query waiting");
1093 		pend->timeout = timeout;
1094 		pend->pkt_len = sldns_buffer_limit(packet);
1095 		pend->pkt = (uint8_t*)memdup(sldns_buffer_begin(packet),
1096 			pend->pkt_len);
1097 		if(!pend->pkt) {
1098 			comm_timer_delete(pend->timer);
1099 			free(pend);
1100 			return NULL;
1101 		}
1102 		/* put at end of waiting list */
1103 		if(sq->outnet->udp_wait_last)
1104 			sq->outnet->udp_wait_last->next_waiting = pend;
1105 		else
1106 			sq->outnet->udp_wait_first = pend;
1107 		sq->outnet->udp_wait_last = pend;
1108 		return pend;
1109 	}
1110 	if(!randomize_and_send_udp(pend, packet, timeout)) {
1111 		pending_delete(sq->outnet, pend);
1112 		return NULL;
1113 	}
1114 	return pend;
1115 }
1116 
1117 void
1118 outnet_tcptimer(void* arg)
1119 {
1120 	struct waiting_tcp* w = (struct waiting_tcp*)arg;
1121 	struct outside_network* outnet = w->outnet;
1122 	comm_point_callback_t* cb;
1123 	void* cb_arg;
1124 	if(w->pkt) {
1125 		/* it is on the waiting list */
1126 		waiting_list_remove(outnet, w);
1127 	} else {
1128 		/* it was in use */
1129 		struct pending_tcp* pend=(struct pending_tcp*)w->next_waiting;
1130 		comm_point_close(pend->c);
1131 		pend->query = NULL;
1132 		pend->next_free = outnet->tcp_free;
1133 		outnet->tcp_free = pend;
1134 	}
1135 	cb = w->cb;
1136 	cb_arg = w->cb_arg;
1137 	waiting_tcp_delete(w);
1138 	fptr_ok(fptr_whitelist_pending_tcp(cb));
1139 	(void)(*cb)(NULL, cb_arg, NETEVENT_TIMEOUT, NULL);
1140 	use_free_buffer(outnet);
1141 }
1142 
1143 struct waiting_tcp*
1144 pending_tcp_query(struct serviced_query* sq, sldns_buffer* packet,
1145 	int timeout, comm_point_callback_t* callback, void* callback_arg)
1146 {
1147 	struct pending_tcp* pend = sq->outnet->tcp_free;
1148 	struct waiting_tcp* w;
1149 	struct timeval tv;
1150 	uint16_t id;
1151 	/* if no buffer is free allocate space to store query */
1152 	w = (struct waiting_tcp*)malloc(sizeof(struct waiting_tcp)
1153 		+ (pend?0:sldns_buffer_limit(packet)));
1154 	if(!w) {
1155 		return NULL;
1156 	}
1157 	if(!(w->timer = comm_timer_create(sq->outnet->base, outnet_tcptimer, w))) {
1158 		free(w);
1159 		return NULL;
1160 	}
1161 	w->pkt = NULL;
1162 	w->pkt_len = 0;
1163 	id = ((unsigned)ub_random(sq->outnet->rnd)>>8) & 0xffff;
1164 	LDNS_ID_SET(sldns_buffer_begin(packet), id);
1165 	memcpy(&w->addr, &sq->addr, sq->addrlen);
1166 	w->addrlen = sq->addrlen;
1167 	w->outnet = sq->outnet;
1168 	w->cb = callback;
1169 	w->cb_arg = callback_arg;
1170 	w->ssl_upstream = sq->ssl_upstream;
1171 #ifndef S_SPLINT_S
1172 	tv.tv_sec = timeout;
1173 	tv.tv_usec = 0;
1174 #endif
1175 	comm_timer_set(w->timer, &tv);
1176 	if(pend) {
1177 		/* we have a buffer available right now */
1178 		if(!outnet_tcp_take_into_use(w, sldns_buffer_begin(packet),
1179 			sldns_buffer_limit(packet))) {
1180 			waiting_tcp_delete(w);
1181 			return NULL;
1182 		}
1183 #ifdef USE_DNSTAP
1184 		if(sq->outnet->dtenv &&
1185 		   (sq->outnet->dtenv->log_resolver_query_messages ||
1186 		    sq->outnet->dtenv->log_forwarder_query_messages))
1187 		dt_msg_send_outside_query(sq->outnet->dtenv, &sq->addr,
1188 		comm_tcp, sq->zone, sq->zonelen, packet);
1189 #endif
1190 	} else {
1191 		/* queue up */
1192 		w->pkt = (uint8_t*)w + sizeof(struct waiting_tcp);
1193 		w->pkt_len = sldns_buffer_limit(packet);
1194 		memmove(w->pkt, sldns_buffer_begin(packet), w->pkt_len);
1195 		w->next_waiting = NULL;
1196 		if(sq->outnet->tcp_wait_last)
1197 			sq->outnet->tcp_wait_last->next_waiting = w;
1198 		else	sq->outnet->tcp_wait_first = w;
1199 		sq->outnet->tcp_wait_last = w;
1200 	}
1201 	return w;
1202 }
1203 
1204 /** create query for serviced queries */
1205 static void
1206 serviced_gen_query(sldns_buffer* buff, uint8_t* qname, size_t qnamelen,
1207 	uint16_t qtype, uint16_t qclass, uint16_t flags)
1208 {
1209 	sldns_buffer_clear(buff);
1210 	/* skip id */
1211 	sldns_buffer_write_u16(buff, flags);
1212 	sldns_buffer_write_u16(buff, 1); /* qdcount */
1213 	sldns_buffer_write_u16(buff, 0); /* ancount */
1214 	sldns_buffer_write_u16(buff, 0); /* nscount */
1215 	sldns_buffer_write_u16(buff, 0); /* arcount */
1216 	sldns_buffer_write(buff, qname, qnamelen);
1217 	sldns_buffer_write_u16(buff, qtype);
1218 	sldns_buffer_write_u16(buff, qclass);
1219 	sldns_buffer_flip(buff);
1220 }
1221 
1222 /** lookup serviced query in serviced query rbtree */
1223 static struct serviced_query*
1224 lookup_serviced(struct outside_network* outnet, sldns_buffer* buff, int dnssec,
1225 	struct sockaddr_storage* addr, socklen_t addrlen,
1226 	struct edns_option* opt_list)
1227 {
1228 	struct serviced_query key;
1229 	key.node.key = &key;
1230 	key.qbuf = sldns_buffer_begin(buff);
1231 	key.qbuflen = sldns_buffer_limit(buff);
1232 	key.dnssec = dnssec;
1233 	memcpy(&key.addr, addr, addrlen);
1234 	key.addrlen = addrlen;
1235 	key.outnet = outnet;
1236 	key.opt_list = opt_list;
1237 	return (struct serviced_query*)rbtree_search(outnet->serviced, &key);
1238 }
1239 
1240 /** Create new serviced entry */
1241 static struct serviced_query*
1242 serviced_create(struct outside_network* outnet, sldns_buffer* buff, int dnssec,
1243 	int want_dnssec, int nocaps, int tcp_upstream, int ssl_upstream,
1244 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
1245 	size_t zonelen, int qtype, struct edns_option* opt_list)
1246 {
1247 	struct serviced_query* sq = (struct serviced_query*)malloc(sizeof(*sq));
1248 #ifdef UNBOUND_DEBUG
1249 	rbnode_t* ins;
1250 #endif
1251 	if(!sq)
1252 		return NULL;
1253 	sq->node.key = sq;
1254 	sq->qbuf = memdup(sldns_buffer_begin(buff), sldns_buffer_limit(buff));
1255 	if(!sq->qbuf) {
1256 		free(sq);
1257 		return NULL;
1258 	}
1259 	sq->qbuflen = sldns_buffer_limit(buff);
1260 	sq->zone = memdup(zone, zonelen);
1261 	if(!sq->zone) {
1262 		free(sq->qbuf);
1263 		free(sq);
1264 		return NULL;
1265 	}
1266 	sq->zonelen = zonelen;
1267 	sq->qtype = qtype;
1268 	sq->dnssec = dnssec;
1269 	sq->want_dnssec = want_dnssec;
1270 	sq->nocaps = nocaps;
1271 	sq->tcp_upstream = tcp_upstream;
1272 	sq->ssl_upstream = ssl_upstream;
1273 	memcpy(&sq->addr, addr, addrlen);
1274 	sq->addrlen = addrlen;
1275 	sq->opt_list = NULL;
1276 	if(opt_list) {
1277 		sq->opt_list = edns_opt_copy_alloc(opt_list);
1278 		if(!sq->opt_list) {
1279 			free(sq->zone);
1280 			free(sq->qbuf);
1281 			free(sq);
1282 			return NULL;
1283 		}
1284 	}
1285 	sq->outnet = outnet;
1286 	sq->cblist = NULL;
1287 	sq->pending = NULL;
1288 	sq->status = serviced_initial;
1289 	sq->retry = 0;
1290 	sq->to_be_deleted = 0;
1291 #ifdef UNBOUND_DEBUG
1292 	ins =
1293 #else
1294 	(void)
1295 #endif
1296 	rbtree_insert(outnet->serviced, &sq->node);
1297 	log_assert(ins != NULL); /* must not be already present */
1298 	return sq;
1299 }
1300 
1301 /** remove waiting tcp from the outnet waiting list */
1302 static void
1303 waiting_list_remove(struct outside_network* outnet, struct waiting_tcp* w)
1304 {
1305 	struct waiting_tcp* p = outnet->tcp_wait_first, *prev = NULL;
1306 	while(p) {
1307 		if(p == w) {
1308 			/* remove w */
1309 			if(prev)
1310 				prev->next_waiting = w->next_waiting;
1311 			else	outnet->tcp_wait_first = w->next_waiting;
1312 			if(outnet->tcp_wait_last == w)
1313 				outnet->tcp_wait_last = prev;
1314 			return;
1315 		}
1316 		prev = p;
1317 		p = p->next_waiting;
1318 	}
1319 }
1320 
1321 /** cleanup serviced query entry */
1322 static void
1323 serviced_delete(struct serviced_query* sq)
1324 {
1325 	if(sq->pending) {
1326 		/* clear up the pending query */
1327 		if(sq->status == serviced_query_UDP_EDNS ||
1328 			sq->status == serviced_query_UDP ||
1329 			sq->status == serviced_query_PROBE_EDNS ||
1330 			sq->status == serviced_query_UDP_EDNS_FRAG ||
1331 			sq->status == serviced_query_UDP_EDNS_fallback) {
1332 			struct pending* p = (struct pending*)sq->pending;
1333 			if(p->pc)
1334 				portcomm_loweruse(sq->outnet, p->pc);
1335 			pending_delete(sq->outnet, p);
1336 			/* this call can cause reentrant calls back into the
1337 			 * mesh */
1338 			outnet_send_wait_udp(sq->outnet);
1339 		} else {
1340 			struct waiting_tcp* p = (struct waiting_tcp*)
1341 				sq->pending;
1342 			if(p->pkt == NULL) {
1343 				decomission_pending_tcp(sq->outnet,
1344 					(struct pending_tcp*)p->next_waiting);
1345 			} else {
1346 				waiting_list_remove(sq->outnet, p);
1347 				waiting_tcp_delete(p);
1348 			}
1349 		}
1350 	}
1351 	/* does not delete from tree, caller has to do that */
1352 	serviced_node_del(&sq->node, NULL);
1353 }
1354 
1355 /** perturb a dname capitalization randomly */
1356 static void
1357 serviced_perturb_qname(struct ub_randstate* rnd, uint8_t* qbuf, size_t len)
1358 {
1359 	uint8_t lablen;
1360 	uint8_t* d = qbuf + 10;
1361 	long int random = 0;
1362 	int bits = 0;
1363 	log_assert(len >= 10 + 5 /* offset qname, root, qtype, qclass */);
1364 	lablen = *d++;
1365 	while(lablen) {
1366 		while(lablen--) {
1367 			/* only perturb A-Z, a-z */
1368 			if(isalpha((unsigned char)*d)) {
1369 				/* get a random bit */
1370 				if(bits == 0) {
1371 					random = ub_random(rnd);
1372 					bits = 30;
1373 				}
1374 				if(random & 0x1) {
1375 					*d = (uint8_t)toupper((unsigned char)*d);
1376 				} else {
1377 					*d = (uint8_t)tolower((unsigned char)*d);
1378 				}
1379 				random >>= 1;
1380 				bits--;
1381 			}
1382 			d++;
1383 		}
1384 		lablen = *d++;
1385 	}
1386 	if(verbosity >= VERB_ALGO) {
1387 		char buf[LDNS_MAX_DOMAINLEN+1];
1388 		dname_str(qbuf+10, buf);
1389 		verbose(VERB_ALGO, "qname perturbed to %s", buf);
1390 	}
1391 }
1392 
1393 /** put serviced query into a buffer */
1394 static void
1395 serviced_encode(struct serviced_query* sq, sldns_buffer* buff, int with_edns)
1396 {
1397 	/* if we are using 0x20 bits for ID randomness, perturb them */
1398 	if(sq->outnet->use_caps_for_id && !sq->nocaps) {
1399 		serviced_perturb_qname(sq->outnet->rnd, sq->qbuf, sq->qbuflen);
1400 	}
1401 	/* generate query */
1402 	sldns_buffer_clear(buff);
1403 	sldns_buffer_write_u16(buff, 0); /* id placeholder */
1404 	sldns_buffer_write(buff, sq->qbuf, sq->qbuflen);
1405 	sldns_buffer_flip(buff);
1406 	if(with_edns) {
1407 		/* add edns section */
1408 		struct edns_data edns;
1409 		edns.edns_present = 1;
1410 		edns.ext_rcode = 0;
1411 		edns.edns_version = EDNS_ADVERTISED_VERSION;
1412 		edns.opt_list = sq->opt_list;
1413 		if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1414 			if(addr_is_ip6(&sq->addr, sq->addrlen)) {
1415 				if(EDNS_FRAG_SIZE_IP6 < EDNS_ADVERTISED_SIZE)
1416 					edns.udp_size = EDNS_FRAG_SIZE_IP6;
1417 				else	edns.udp_size = EDNS_ADVERTISED_SIZE;
1418 			} else {
1419 				if(EDNS_FRAG_SIZE_IP4 < EDNS_ADVERTISED_SIZE)
1420 					edns.udp_size = EDNS_FRAG_SIZE_IP4;
1421 				else	edns.udp_size = EDNS_ADVERTISED_SIZE;
1422 			}
1423 		} else {
1424 			edns.udp_size = EDNS_ADVERTISED_SIZE;
1425 		}
1426 		edns.bits = 0;
1427 		if(sq->dnssec & EDNS_DO)
1428 			edns.bits = EDNS_DO;
1429 		if(sq->dnssec & BIT_CD)
1430 			LDNS_CD_SET(sldns_buffer_begin(buff));
1431 		attach_edns_record(buff, &edns);
1432 	}
1433 }
1434 
1435 /**
1436  * Perform serviced query UDP sending operation.
1437  * Sends UDP with EDNS, unless infra host marked non EDNS.
1438  * @param sq: query to send.
1439  * @param buff: buffer scratch space.
1440  * @return 0 on error.
1441  */
1442 static int
1443 serviced_udp_send(struct serviced_query* sq, sldns_buffer* buff)
1444 {
1445 	int rtt, vs;
1446 	uint8_t edns_lame_known;
1447 	time_t now = *sq->outnet->now_secs;
1448 
1449 	if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1450 		sq->zonelen, now, &vs, &edns_lame_known, &rtt))
1451 		return 0;
1452 	sq->last_rtt = rtt;
1453 	verbose(VERB_ALGO, "EDNS lookup known=%d vs=%d", edns_lame_known, vs);
1454 	if(sq->status == serviced_initial) {
1455 		if(edns_lame_known == 0 && rtt > 5000 && rtt < 10001) {
1456 			/* perform EDNS lame probe - check if server is
1457 			 * EDNS lame (EDNS queries to it are dropped) */
1458 			verbose(VERB_ALGO, "serviced query: send probe to see "
1459 				" if use of EDNS causes timeouts");
1460 			/* even 700 msec may be too small */
1461 			rtt = 1000;
1462 			sq->status = serviced_query_PROBE_EDNS;
1463 		} else if(vs != -1) {
1464 			sq->status = serviced_query_UDP_EDNS;
1465 		} else {
1466 			sq->status = serviced_query_UDP;
1467 		}
1468 	}
1469 	serviced_encode(sq, buff, (sq->status == serviced_query_UDP_EDNS) ||
1470 		(sq->status == serviced_query_UDP_EDNS_FRAG));
1471 	sq->last_sent_time = *sq->outnet->now_tv;
1472 	sq->edns_lame_known = (int)edns_lame_known;
1473 	verbose(VERB_ALGO, "serviced query UDP timeout=%d msec", rtt);
1474 	sq->pending = pending_udp_query(sq, buff, rtt,
1475 		serviced_udp_callback, sq);
1476 	if(!sq->pending)
1477 		return 0;
1478 	return 1;
1479 }
1480 
1481 /** check that perturbed qname is identical */
1482 static int
1483 serviced_check_qname(sldns_buffer* pkt, uint8_t* qbuf, size_t qbuflen)
1484 {
1485 	uint8_t* d1 = sldns_buffer_at(pkt, 12);
1486 	uint8_t* d2 = qbuf+10;
1487 	uint8_t len1, len2;
1488 	int count = 0;
1489 	log_assert(qbuflen >= 15 /* 10 header, root, type, class */);
1490 	len1 = *d1++;
1491 	len2 = *d2++;
1492 	if(sldns_buffer_limit(pkt) < 12+1+4) /* packet too small for qname */
1493 		return 0;
1494 	while(len1 != 0 || len2 != 0) {
1495 		if(LABEL_IS_PTR(len1)) {
1496 			d1 = sldns_buffer_at(pkt, PTR_OFFSET(len1, *d1));
1497 			if(d1 >= sldns_buffer_at(pkt, sldns_buffer_limit(pkt)))
1498 				return 0;
1499 			len1 = *d1++;
1500 			if(count++ > MAX_COMPRESS_PTRS)
1501 				return 0;
1502 			continue;
1503 		}
1504 		if(d2 > qbuf+qbuflen)
1505 			return 0;
1506 		if(len1 != len2)
1507 			return 0;
1508 		if(len1 > LDNS_MAX_LABELLEN)
1509 			return 0;
1510 		log_assert(len1 <= LDNS_MAX_LABELLEN);
1511 		log_assert(len2 <= LDNS_MAX_LABELLEN);
1512 		log_assert(len1 == len2 && len1 != 0);
1513 		/* compare the labels - bitwise identical */
1514 		if(memcmp(d1, d2, len1) != 0)
1515 			return 0;
1516 		d1 += len1;
1517 		d2 += len2;
1518 		len1 = *d1++;
1519 		len2 = *d2++;
1520 	}
1521 	return 1;
1522 }
1523 
1524 /** call the callbacks for a serviced query */
1525 static void
1526 serviced_callbacks(struct serviced_query* sq, int error, struct comm_point* c,
1527 	struct comm_reply* rep)
1528 {
1529 	struct service_callback* p;
1530 	int dobackup = (sq->cblist && sq->cblist->next); /* >1 cb*/
1531 	uint8_t *backup_p = NULL;
1532 	size_t backlen = 0;
1533 #ifdef UNBOUND_DEBUG
1534 	rbnode_t* rem =
1535 #else
1536 	(void)
1537 #endif
1538 	/* remove from tree, and schedule for deletion, so that callbacks
1539 	 * can safely deregister themselves and even create new serviced
1540 	 * queries that are identical to this one. */
1541 	rbtree_delete(sq->outnet->serviced, sq);
1542 	log_assert(rem); /* should have been present */
1543 	sq->to_be_deleted = 1;
1544 	verbose(VERB_ALGO, "svcd callbacks start");
1545 	if(sq->outnet->use_caps_for_id && error == NETEVENT_NOERROR && c &&
1546 		!sq->nocaps && sq->qtype != LDNS_RR_TYPE_PTR) {
1547 		/* for type PTR do not check perturbed name in answer,
1548 		 * compatibility with cisco dns guard boxes that mess up
1549 		 * reverse queries 0x20 contents */
1550 		/* noerror and nxdomain must have a qname in reply */
1551 		if(sldns_buffer_read_u16_at(c->buffer, 4) == 0 &&
1552 			(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1553 				== LDNS_RCODE_NOERROR ||
1554 			 LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1555 				== LDNS_RCODE_NXDOMAIN)) {
1556 			verbose(VERB_DETAIL, "no qname in reply to check 0x20ID");
1557 			log_addr(VERB_DETAIL, "from server",
1558 				&sq->addr, sq->addrlen);
1559 			log_buf(VERB_DETAIL, "for packet", c->buffer);
1560 			error = NETEVENT_CLOSED;
1561 			c = NULL;
1562 		} else if(sldns_buffer_read_u16_at(c->buffer, 4) > 0 &&
1563 			!serviced_check_qname(c->buffer, sq->qbuf,
1564 			sq->qbuflen)) {
1565 			verbose(VERB_DETAIL, "wrong 0x20-ID in reply qname");
1566 			log_addr(VERB_DETAIL, "from server",
1567 				&sq->addr, sq->addrlen);
1568 			log_buf(VERB_DETAIL, "for packet", c->buffer);
1569 			error = NETEVENT_CAPSFAIL;
1570 			/* and cleanup too */
1571 			pkt_dname_tolower(c->buffer,
1572 				sldns_buffer_at(c->buffer, 12));
1573 		} else {
1574 			verbose(VERB_ALGO, "good 0x20-ID in reply qname");
1575 			/* cleanup caps, prettier cache contents. */
1576 			pkt_dname_tolower(c->buffer,
1577 				sldns_buffer_at(c->buffer, 12));
1578 		}
1579 	}
1580 	if(dobackup && c) {
1581 		/* make a backup of the query, since the querystate processing
1582 		 * may send outgoing queries that overwrite the buffer.
1583 		 * use secondary buffer to store the query.
1584 		 * This is a data copy, but faster than packet to server */
1585 		backlen = sldns_buffer_limit(c->buffer);
1586 		backup_p = memdup(sldns_buffer_begin(c->buffer), backlen);
1587 		if(!backup_p) {
1588 			log_err("malloc failure in serviced query callbacks");
1589 			error = NETEVENT_CLOSED;
1590 			c = NULL;
1591 		}
1592 		sq->outnet->svcd_overhead = backlen;
1593 	}
1594 	/* test the actual sq->cblist, because the next elem could be deleted*/
1595 	while((p=sq->cblist) != NULL) {
1596 		sq->cblist = p->next; /* remove this element */
1597 		if(dobackup && c) {
1598 			sldns_buffer_clear(c->buffer);
1599 			sldns_buffer_write(c->buffer, backup_p, backlen);
1600 			sldns_buffer_flip(c->buffer);
1601 		}
1602 		fptr_ok(fptr_whitelist_serviced_query(p->cb));
1603 		(void)(*p->cb)(c, p->cb_arg, error, rep);
1604 		free(p);
1605 	}
1606 	if(backup_p) {
1607 		free(backup_p);
1608 		sq->outnet->svcd_overhead = 0;
1609 	}
1610 	verbose(VERB_ALGO, "svcd callbacks end");
1611 	log_assert(sq->cblist == NULL);
1612 	serviced_delete(sq);
1613 }
1614 
1615 int
1616 serviced_tcp_callback(struct comm_point* c, void* arg, int error,
1617         struct comm_reply* rep)
1618 {
1619 	struct serviced_query* sq = (struct serviced_query*)arg;
1620 	struct comm_reply r2;
1621 	sq->pending = NULL; /* removed after this callback */
1622 	if(error != NETEVENT_NOERROR)
1623 		log_addr(VERB_QUERY, "tcp error for address",
1624 			&sq->addr, sq->addrlen);
1625 	if(error==NETEVENT_NOERROR)
1626 		infra_update_tcp_works(sq->outnet->infra, &sq->addr,
1627 			sq->addrlen, sq->zone, sq->zonelen);
1628 #ifdef USE_DNSTAP
1629 	if(error==NETEVENT_NOERROR && sq->outnet->dtenv &&
1630 	   (sq->outnet->dtenv->log_resolver_response_messages ||
1631 	    sq->outnet->dtenv->log_forwarder_response_messages))
1632 		dt_msg_send_outside_response(sq->outnet->dtenv, &sq->addr,
1633 		c->type, sq->zone, sq->zonelen, sq->qbuf, sq->qbuflen,
1634 		&sq->last_sent_time, sq->outnet->now_tv, c->buffer);
1635 #endif
1636 	if(error==NETEVENT_NOERROR && sq->status == serviced_query_TCP_EDNS &&
1637 		(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1638 		LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(sldns_buffer_begin(
1639 		c->buffer)) == LDNS_RCODE_NOTIMPL) ) {
1640 		/* attempt to fallback to nonEDNS */
1641 		sq->status = serviced_query_TCP_EDNS_fallback;
1642 		serviced_tcp_initiate(sq, c->buffer);
1643 		return 0;
1644 	} else if(error==NETEVENT_NOERROR &&
1645 		sq->status == serviced_query_TCP_EDNS_fallback &&
1646 			(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1647 			LDNS_RCODE_NOERROR || LDNS_RCODE_WIRE(
1648 			sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NXDOMAIN
1649 			|| LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1650 			== LDNS_RCODE_YXDOMAIN)) {
1651 		/* the fallback produced a result that looks promising, note
1652 		 * that this server should be approached without EDNS */
1653 		/* only store noEDNS in cache if domain is noDNSSEC */
1654 		if(!sq->want_dnssec)
1655 		  if(!infra_edns_update(sq->outnet->infra, &sq->addr,
1656 			sq->addrlen, sq->zone, sq->zonelen, -1,
1657 			*sq->outnet->now_secs))
1658 			log_err("Out of memory caching no edns for host");
1659 		sq->status = serviced_query_TCP;
1660 	}
1661 	if(sq->tcp_upstream || sq->ssl_upstream) {
1662 	    struct timeval now = *sq->outnet->now_tv;
1663 	    if(now.tv_sec > sq->last_sent_time.tv_sec ||
1664 		(now.tv_sec == sq->last_sent_time.tv_sec &&
1665 		now.tv_usec > sq->last_sent_time.tv_usec)) {
1666 		/* convert from microseconds to milliseconds */
1667 		int roundtime = ((int)(now.tv_sec - sq->last_sent_time.tv_sec))*1000
1668 		  + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1669 		verbose(VERB_ALGO, "measured TCP-time at %d msec", roundtime);
1670 		log_assert(roundtime >= 0);
1671 		/* only store if less then AUTH_TIMEOUT seconds, it could be
1672 		 * huge due to system-hibernated and we woke up */
1673 		if(roundtime < TCP_AUTH_QUERY_TIMEOUT*1000) {
1674 		    if(!infra_rtt_update(sq->outnet->infra, &sq->addr,
1675 			sq->addrlen, sq->zone, sq->zonelen, sq->qtype,
1676 			roundtime, sq->last_rtt, (time_t)now.tv_sec))
1677 			log_err("out of memory noting rtt.");
1678 		}
1679 	    }
1680 	}
1681 	/* insert address into reply info */
1682 	if(!rep) {
1683 		/* create one if there isn't (on errors) */
1684 		rep = &r2;
1685 		r2.c = c;
1686 	}
1687 	memcpy(&rep->addr, &sq->addr, sq->addrlen);
1688 	rep->addrlen = sq->addrlen;
1689 	serviced_callbacks(sq, error, c, rep);
1690 	return 0;
1691 }
1692 
1693 static void
1694 serviced_tcp_initiate(struct serviced_query* sq, sldns_buffer* buff)
1695 {
1696 	verbose(VERB_ALGO, "initiate TCP query %s",
1697 		sq->status==serviced_query_TCP_EDNS?"EDNS":"");
1698 	serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1699 	sq->last_sent_time = *sq->outnet->now_tv;
1700 	sq->pending = pending_tcp_query(sq, buff, TCP_AUTH_QUERY_TIMEOUT,
1701 		serviced_tcp_callback, sq);
1702 	if(!sq->pending) {
1703 		/* delete from tree so that a retry by above layer does not
1704 		 * clash with this entry */
1705 		log_err("serviced_tcp_initiate: failed to send tcp query");
1706 		serviced_callbacks(sq, NETEVENT_CLOSED, NULL, NULL);
1707 	}
1708 }
1709 
1710 /** Send serviced query over TCP return false on initial failure */
1711 static int
1712 serviced_tcp_send(struct serviced_query* sq, sldns_buffer* buff)
1713 {
1714 	int vs, rtt;
1715 	uint8_t edns_lame_known;
1716 	if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1717 		sq->zonelen, *sq->outnet->now_secs, &vs, &edns_lame_known,
1718 		&rtt))
1719 		return 0;
1720 	if(vs != -1)
1721 		sq->status = serviced_query_TCP_EDNS;
1722 	else 	sq->status = serviced_query_TCP;
1723 	serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1724 	sq->last_sent_time = *sq->outnet->now_tv;
1725 	sq->pending = pending_tcp_query(sq, buff, TCP_AUTH_QUERY_TIMEOUT,
1726 		serviced_tcp_callback, sq);
1727 	return sq->pending != NULL;
1728 }
1729 
1730 /* see if packet is edns malformed; got zeroes at start.
1731  * This is from servers that return malformed packets to EDNS0 queries,
1732  * but they return good packets for nonEDNS0 queries.
1733  * We try to detect their output; without resorting to a full parse or
1734  * check for too many bytes after the end of the packet. */
1735 static int
1736 packet_edns_malformed(struct sldns_buffer* buf, int qtype)
1737 {
1738 	size_t len;
1739 	if(sldns_buffer_limit(buf) < LDNS_HEADER_SIZE)
1740 		return 1; /* malformed */
1741 	/* they have NOERROR rcode, 1 answer. */
1742 	if(LDNS_RCODE_WIRE(sldns_buffer_begin(buf)) != LDNS_RCODE_NOERROR)
1743 		return 0;
1744 	/* one query (to skip) and answer records */
1745 	if(LDNS_QDCOUNT(sldns_buffer_begin(buf)) != 1 ||
1746 		LDNS_ANCOUNT(sldns_buffer_begin(buf)) == 0)
1747 		return 0;
1748 	/* skip qname */
1749 	len = dname_valid(sldns_buffer_at(buf, LDNS_HEADER_SIZE),
1750 		sldns_buffer_limit(buf)-LDNS_HEADER_SIZE);
1751 	if(len == 0)
1752 		return 0;
1753 	if(len == 1 && qtype == 0)
1754 		return 0; /* we asked for '.' and type 0 */
1755 	/* and then 4 bytes (type and class of query) */
1756 	if(sldns_buffer_limit(buf) < LDNS_HEADER_SIZE + len + 4 + 3)
1757 		return 0;
1758 
1759 	/* and start with 11 zeroes as the answer RR */
1760 	/* so check the qtype of the answer record, qname=0, type=0 */
1761 	if(sldns_buffer_at(buf, LDNS_HEADER_SIZE+len+4)[0] == 0 &&
1762 	   sldns_buffer_at(buf, LDNS_HEADER_SIZE+len+4)[1] == 0 &&
1763 	   sldns_buffer_at(buf, LDNS_HEADER_SIZE+len+4)[2] == 0)
1764 		return 1;
1765 	return 0;
1766 }
1767 
1768 int
1769 serviced_udp_callback(struct comm_point* c, void* arg, int error,
1770         struct comm_reply* rep)
1771 {
1772 	struct serviced_query* sq = (struct serviced_query*)arg;
1773 	struct outside_network* outnet = sq->outnet;
1774 	struct timeval now = *sq->outnet->now_tv;
1775 	int fallback_tcp = 0;
1776 
1777 	sq->pending = NULL; /* removed after callback */
1778 	if(error == NETEVENT_TIMEOUT) {
1779 		int rto = 0;
1780 		if(sq->status == serviced_query_PROBE_EDNS) {
1781 			/* non-EDNS probe failed; we do not know its status,
1782 			 * keep trying with EDNS, timeout may not be caused
1783 			 * by EDNS. */
1784 			sq->status = serviced_query_UDP_EDNS;
1785 		}
1786 		if(sq->status == serviced_query_UDP_EDNS && sq->last_rtt < 5000) {
1787 			/* fallback to 1480/1280 */
1788 			sq->status = serviced_query_UDP_EDNS_FRAG;
1789 			log_name_addr(VERB_ALGO, "try edns1xx0", sq->qbuf+10,
1790 				&sq->addr, sq->addrlen);
1791 			if(!serviced_udp_send(sq, c->buffer)) {
1792 				serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1793 			}
1794 			return 0;
1795 		}
1796 		if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1797 			/* fragmentation size did not fix it */
1798 			sq->status = serviced_query_UDP_EDNS;
1799 		}
1800 		sq->retry++;
1801 		if(!(rto=infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1802 			sq->zone, sq->zonelen, sq->qtype, -1, sq->last_rtt,
1803 			(time_t)now.tv_sec)))
1804 			log_err("out of memory in UDP exponential backoff");
1805 		if(sq->retry < OUTBOUND_UDP_RETRY) {
1806 			log_name_addr(VERB_ALGO, "retry query", sq->qbuf+10,
1807 				&sq->addr, sq->addrlen);
1808 			if(!serviced_udp_send(sq, c->buffer)) {
1809 				serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1810 			}
1811 			return 0;
1812 		}
1813 		if(rto >= RTT_MAX_TIMEOUT) {
1814 			fallback_tcp = 1;
1815 			/* UDP does not work, fallback to TCP below */
1816 		} else {
1817 			serviced_callbacks(sq, NETEVENT_TIMEOUT, c, rep);
1818 			return 0;
1819 		}
1820 	} else if(error != NETEVENT_NOERROR) {
1821 		/* udp returns error (due to no ID or interface available) */
1822 		serviced_callbacks(sq, error, c, rep);
1823 		return 0;
1824 	}
1825 #ifdef USE_DNSTAP
1826 	if(outnet->dtenv &&
1827 	   (outnet->dtenv->log_resolver_response_messages ||
1828 	    outnet->dtenv->log_forwarder_response_messages))
1829 		dt_msg_send_outside_response(outnet->dtenv, &sq->addr, c->type,
1830 		sq->zone, sq->zonelen, sq->qbuf, sq->qbuflen,
1831 		&sq->last_sent_time, sq->outnet->now_tv, c->buffer);
1832 #endif
1833 	if(!fallback_tcp) {
1834 	    if( (sq->status == serviced_query_UDP_EDNS
1835 	        ||sq->status == serviced_query_UDP_EDNS_FRAG)
1836 		&& (LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1837 			== LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(
1838 			sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOTIMPL
1839 		    || packet_edns_malformed(c->buffer, sq->qtype)
1840 			)) {
1841 		/* try to get an answer by falling back without EDNS */
1842 		verbose(VERB_ALGO, "serviced query: attempt without EDNS");
1843 		sq->status = serviced_query_UDP_EDNS_fallback;
1844 		sq->retry = 0;
1845 		if(!serviced_udp_send(sq, c->buffer)) {
1846 			serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1847 		}
1848 		return 0;
1849 	    } else if(sq->status == serviced_query_PROBE_EDNS) {
1850 		/* probe without EDNS succeeds, so we conclude that this
1851 		 * host likely has EDNS packets dropped */
1852 		log_addr(VERB_DETAIL, "timeouts, concluded that connection to "
1853 			"host drops EDNS packets", &sq->addr, sq->addrlen);
1854 		/* only store noEDNS in cache if domain is noDNSSEC */
1855 		if(!sq->want_dnssec)
1856 		  if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1857 			sq->zone, sq->zonelen, -1, (time_t)now.tv_sec)) {
1858 			log_err("Out of memory caching no edns for host");
1859 		  }
1860 		sq->status = serviced_query_UDP;
1861 	    } else if(sq->status == serviced_query_UDP_EDNS &&
1862 		!sq->edns_lame_known) {
1863 		/* now we know that edns queries received answers store that */
1864 		log_addr(VERB_ALGO, "serviced query: EDNS works for",
1865 			&sq->addr, sq->addrlen);
1866 		if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1867 			sq->zone, sq->zonelen, 0, (time_t)now.tv_sec)) {
1868 			log_err("Out of memory caching edns works");
1869 		}
1870 		sq->edns_lame_known = 1;
1871 	    } else if(sq->status == serviced_query_UDP_EDNS_fallback &&
1872 		!sq->edns_lame_known && (LDNS_RCODE_WIRE(
1873 		sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOERROR ||
1874 		LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1875 		LDNS_RCODE_NXDOMAIN || LDNS_RCODE_WIRE(sldns_buffer_begin(
1876 		c->buffer)) == LDNS_RCODE_YXDOMAIN)) {
1877 		/* the fallback produced a result that looks promising, note
1878 		 * that this server should be approached without EDNS */
1879 		/* only store noEDNS in cache if domain is noDNSSEC */
1880 		if(!sq->want_dnssec) {
1881 		  log_addr(VERB_ALGO, "serviced query: EDNS fails for",
1882 			&sq->addr, sq->addrlen);
1883 		  if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1884 			sq->zone, sq->zonelen, -1, (time_t)now.tv_sec)) {
1885 			log_err("Out of memory caching no edns for host");
1886 		  }
1887 		} else {
1888 		  log_addr(VERB_ALGO, "serviced query: EDNS fails, but "
1889 		  	"not stored because need DNSSEC for", &sq->addr,
1890 			sq->addrlen);
1891 		}
1892 		sq->status = serviced_query_UDP;
1893 	    }
1894 	    if(now.tv_sec > sq->last_sent_time.tv_sec ||
1895 		(now.tv_sec == sq->last_sent_time.tv_sec &&
1896 		now.tv_usec > sq->last_sent_time.tv_usec)) {
1897 		/* convert from microseconds to milliseconds */
1898 		int roundtime = ((int)(now.tv_sec - sq->last_sent_time.tv_sec))*1000
1899 		  + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1900 		verbose(VERB_ALGO, "measured roundtrip at %d msec", roundtime);
1901 		log_assert(roundtime >= 0);
1902 		/* in case the system hibernated, do not enter a huge value,
1903 		 * above this value gives trouble with server selection */
1904 		if(roundtime < 60000) {
1905 		    if(!infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1906 			sq->zone, sq->zonelen, sq->qtype, roundtime,
1907 			sq->last_rtt, (time_t)now.tv_sec))
1908 			log_err("out of memory noting rtt.");
1909 		}
1910 	    }
1911 	} /* end of if_!fallback_tcp */
1912 	/* perform TC flag check and TCP fallback after updating our
1913 	 * cache entries for EDNS status and RTT times */
1914 	if(LDNS_TC_WIRE(sldns_buffer_begin(c->buffer)) || fallback_tcp) {
1915 		/* fallback to TCP */
1916 		/* this discards partial UDP contents */
1917 		if(sq->status == serviced_query_UDP_EDNS ||
1918 			sq->status == serviced_query_UDP_EDNS_FRAG ||
1919 			sq->status == serviced_query_UDP_EDNS_fallback)
1920 			/* if we have unfinished EDNS_fallback, start again */
1921 			sq->status = serviced_query_TCP_EDNS;
1922 		else	sq->status = serviced_query_TCP;
1923 		serviced_tcp_initiate(sq, c->buffer);
1924 		return 0;
1925 	}
1926 	/* yay! an answer */
1927 	serviced_callbacks(sq, error, c, rep);
1928 	return 0;
1929 }
1930 
1931 struct serviced_query*
1932 outnet_serviced_query(struct outside_network* outnet,
1933 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
1934 	uint16_t flags, int dnssec, int want_dnssec, int nocaps,
1935 	int tcp_upstream, int ssl_upstream, struct edns_option* opt_list,
1936 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
1937 	size_t zonelen, comm_point_callback_t* callback, void* callback_arg,
1938 	sldns_buffer* buff)
1939 {
1940 	struct serviced_query* sq;
1941 	struct service_callback* cb;
1942 	serviced_gen_query(buff, qname, qnamelen, qtype, qclass, flags);
1943 	sq = lookup_serviced(outnet, buff, dnssec, addr, addrlen, opt_list);
1944 	/* duplicate entries are included in the callback list, because
1945 	 * there is a counterpart registration by our caller that needs to
1946 	 * be doubly-removed (with callbacks perhaps). */
1947 	if(!(cb = (struct service_callback*)malloc(sizeof(*cb))))
1948 		return NULL;
1949 	if(!sq) {
1950 		/* make new serviced query entry */
1951 		sq = serviced_create(outnet, buff, dnssec, want_dnssec, nocaps,
1952 			tcp_upstream, ssl_upstream, addr, addrlen, zone,
1953 			zonelen, (int)qtype, opt_list);
1954 		if(!sq) {
1955 			free(cb);
1956 			return NULL;
1957 		}
1958 		/* perform first network action */
1959 		if(outnet->do_udp && !(tcp_upstream || ssl_upstream)) {
1960 			if(!serviced_udp_send(sq, buff)) {
1961 				(void)rbtree_delete(outnet->serviced, sq);
1962 				free(sq->qbuf);
1963 				free(sq->zone);
1964 				free(sq);
1965 				free(cb);
1966 				return NULL;
1967 			}
1968 		} else {
1969 			if(!serviced_tcp_send(sq, buff)) {
1970 				(void)rbtree_delete(outnet->serviced, sq);
1971 				free(sq->qbuf);
1972 				free(sq->zone);
1973 				free(sq);
1974 				free(cb);
1975 				return NULL;
1976 			}
1977 		}
1978 	}
1979 	/* add callback to list of callbacks */
1980 	cb->cb = callback;
1981 	cb->cb_arg = callback_arg;
1982 	cb->next = sq->cblist;
1983 	sq->cblist = cb;
1984 	return sq;
1985 }
1986 
1987 /** remove callback from list */
1988 static void
1989 callback_list_remove(struct serviced_query* sq, void* cb_arg)
1990 {
1991 	struct service_callback** pp = &sq->cblist;
1992 	while(*pp) {
1993 		if((*pp)->cb_arg == cb_arg) {
1994 			struct service_callback* del = *pp;
1995 			*pp = del->next;
1996 			free(del);
1997 			return;
1998 		}
1999 		pp = &(*pp)->next;
2000 	}
2001 }
2002 
2003 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
2004 {
2005 	if(!sq)
2006 		return;
2007 	callback_list_remove(sq, cb_arg);
2008 	/* if callbacks() routine scheduled deletion, let it do that */
2009 	if(!sq->cblist && !sq->to_be_deleted) {
2010 		(void)rbtree_delete(sq->outnet->serviced, sq);
2011 		serviced_delete(sq);
2012 	}
2013 }
2014 
2015 /** get memory used by waiting tcp entry (in use or not) */
2016 static size_t
2017 waiting_tcp_get_mem(struct waiting_tcp* w)
2018 {
2019 	size_t s;
2020 	if(!w) return 0;
2021 	s = sizeof(*w) + w->pkt_len;
2022 	if(w->timer)
2023 		s += comm_timer_get_mem(w->timer);
2024 	return s;
2025 }
2026 
2027 /** get memory used by port if */
2028 static size_t
2029 if_get_mem(struct port_if* pif)
2030 {
2031 	size_t s;
2032 	int i;
2033 	s = sizeof(*pif) + sizeof(int)*pif->avail_total +
2034 		sizeof(struct port_comm*)*pif->maxout;
2035 	for(i=0; i<pif->inuse; i++)
2036 		s += sizeof(*pif->out[i]) +
2037 			comm_point_get_mem(pif->out[i]->cp);
2038 	return s;
2039 }
2040 
2041 /** get memory used by waiting udp */
2042 static size_t
2043 waiting_udp_get_mem(struct pending* w)
2044 {
2045 	size_t s;
2046 	s = sizeof(*w) + comm_timer_get_mem(w->timer) + w->pkt_len;
2047 	return s;
2048 }
2049 
2050 size_t outnet_get_mem(struct outside_network* outnet)
2051 {
2052 	size_t i;
2053 	int k;
2054 	struct waiting_tcp* w;
2055 	struct pending* u;
2056 	struct serviced_query* sq;
2057 	struct service_callback* sb;
2058 	struct port_comm* pc;
2059 	size_t s = sizeof(*outnet) + sizeof(*outnet->base) +
2060 		sizeof(*outnet->udp_buff) +
2061 		sldns_buffer_capacity(outnet->udp_buff);
2062 	/* second buffer is not ours */
2063 	for(pc = outnet->unused_fds; pc; pc = pc->next) {
2064 		s += sizeof(*pc) + comm_point_get_mem(pc->cp);
2065 	}
2066 	for(k=0; k<outnet->num_ip4; k++)
2067 		s += if_get_mem(&outnet->ip4_ifs[k]);
2068 	for(k=0; k<outnet->num_ip6; k++)
2069 		s += if_get_mem(&outnet->ip6_ifs[k]);
2070 	for(u=outnet->udp_wait_first; u; u=u->next_waiting)
2071 		s += waiting_udp_get_mem(u);
2072 
2073 	s += sizeof(struct pending_tcp*)*outnet->num_tcp;
2074 	for(i=0; i<outnet->num_tcp; i++) {
2075 		s += sizeof(struct pending_tcp);
2076 		s += comm_point_get_mem(outnet->tcp_conns[i]->c);
2077 		if(outnet->tcp_conns[i]->query)
2078 			s += waiting_tcp_get_mem(outnet->tcp_conns[i]->query);
2079 	}
2080 	for(w=outnet->tcp_wait_first; w; w = w->next_waiting)
2081 		s += waiting_tcp_get_mem(w);
2082 	s += sizeof(*outnet->pending);
2083 	s += (sizeof(struct pending) + comm_timer_get_mem(NULL)) *
2084 		outnet->pending->count;
2085 	s += sizeof(*outnet->serviced);
2086 	s += outnet->svcd_overhead;
2087 	RBTREE_FOR(sq, struct serviced_query*, outnet->serviced) {
2088 		s += sizeof(*sq) + sq->qbuflen;
2089 		for(sb = sq->cblist; sb; sb = sb->next)
2090 			s += sizeof(*sb);
2091 	}
2092 	return s;
2093 }
2094 
2095 size_t
2096 serviced_get_mem(struct serviced_query* sq)
2097 {
2098 	struct service_callback* sb;
2099 	size_t s;
2100 	s = sizeof(*sq) + sq->qbuflen;
2101 	for(sb = sq->cblist; sb; sb = sb->next)
2102 		s += sizeof(*sb);
2103 	if(sq->status == serviced_query_UDP_EDNS ||
2104 		sq->status == serviced_query_UDP ||
2105 		sq->status == serviced_query_PROBE_EDNS ||
2106 		sq->status == serviced_query_UDP_EDNS_FRAG ||
2107 		sq->status == serviced_query_UDP_EDNS_fallback) {
2108 		s += sizeof(struct pending);
2109 		s += comm_timer_get_mem(NULL);
2110 	} else {
2111 		/* does not have size of the pkt pointer */
2112 		/* always has a timer except on malloc failures */
2113 
2114 		/* these sizes are part of the main outside network mem */
2115 		/*
2116 		s += sizeof(struct waiting_tcp);
2117 		s += comm_timer_get_mem(NULL);
2118 		*/
2119 	}
2120 	return s;
2121 }
2122 
2123