/src/bind9/lib/dns/dispatch.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | /*! \file */ |
15 | | |
16 | | /* |
17 | | * FIXME: Might need dns_dispatch_shuttingdown() |
18 | | */ |
19 | | |
20 | | #include <inttypes.h> |
21 | | #include <stdbool.h> |
22 | | #include <stdlib.h> |
23 | | #include <sys/types.h> |
24 | | #include <unistd.h> |
25 | | |
26 | | #include <isc/async.h> |
27 | | #include <isc/hash.h> |
28 | | #include <isc/hashmap.h> |
29 | | #include <isc/log.h> |
30 | | #include <isc/loop.h> |
31 | | #include <isc/mem.h> |
32 | | #include <isc/mutex.h> |
33 | | #include <isc/net.h> |
34 | | #include <isc/netmgr.h> |
35 | | #include <isc/portset.h> |
36 | | #include <isc/random.h> |
37 | | #include <isc/stats.h> |
38 | | #include <isc/string.h> |
39 | | #include <isc/tid.h> |
40 | | #include <isc/time.h> |
41 | | #include <isc/tls.h> |
42 | | #include <isc/urcu.h> |
43 | | #include <isc/util.h> |
44 | | |
45 | | #include <dns/acl.h> |
46 | | #include <dns/dispatch.h> |
47 | | #include <dns/message.h> |
48 | | #include <dns/stats.h> |
49 | | #include <dns/transport.h> |
50 | | #include <dns/types.h> |
51 | | |
52 | | typedef ISC_LIST(dns_dispentry_t) dns_displist_t; |
53 | | |
54 | | struct dns_dispatchmgr { |
55 | | /* Unlocked. */ |
56 | | unsigned int magic; |
57 | | isc_refcount_t references; |
58 | | isc_mem_t *mctx; |
59 | | dns_acl_t *blackhole; |
60 | | isc_stats_t *stats; |
61 | | |
62 | | uint32_t nloops; |
63 | | |
64 | | struct cds_lfht **tcps; |
65 | | |
66 | | struct cds_lfht *qids; |
67 | | |
68 | | in_port_t *v4ports; /*%< available ports for IPv4 */ |
69 | | unsigned int nv4ports; /*%< # of available ports for IPv4 */ |
70 | | in_port_t *v6ports; /*%< available ports for IPv4 */ |
71 | | unsigned int nv6ports; /*%< # of available ports for IPv4 */ |
72 | | }; |
73 | | |
74 | | typedef enum { |
75 | | DNS_DISPATCHSTATE_NONE = 0UL, |
76 | | DNS_DISPATCHSTATE_CONNECTING, |
77 | | DNS_DISPATCHSTATE_CONNECTED, |
78 | | DNS_DISPATCHSTATE_CANCELED, |
79 | | } dns_dispatchstate_t; |
80 | | |
81 | | struct dns_dispentry { |
82 | | unsigned int magic; |
83 | | isc_refcount_t references; |
84 | | isc_mem_t *mctx; |
85 | | dns_dispatch_t *disp; |
86 | | isc_loop_t *loop; |
87 | | isc_nmhandle_t *handle; /*%< netmgr handle for UDP connection */ |
88 | | dns_dispatchstate_t state; |
89 | | dns_transport_t *transport; |
90 | | isc_tlsctx_cache_t *tlsctx_cache; |
91 | | unsigned int retries; |
92 | | unsigned int connect_timeout; |
93 | | unsigned int timeout; |
94 | | isc_time_t start; |
95 | | isc_sockaddr_t local; |
96 | | isc_sockaddr_t peer; |
97 | | in_port_t port; |
98 | | dns_messageid_t id; |
99 | | dispatch_cb_t connected; |
100 | | dispatch_cb_t sent; |
101 | | dispatch_cb_t response; |
102 | | void *arg; |
103 | | bool reading; |
104 | | isc_result_t result; |
105 | | ISC_LINK(dns_dispentry_t) alink; |
106 | | ISC_LINK(dns_dispentry_t) plink; |
107 | | ISC_LINK(dns_dispentry_t) rlink; |
108 | | |
109 | | struct cds_lfht_node ht_node; |
110 | | struct rcu_head rcu_head; |
111 | | }; |
112 | | |
113 | | struct dns_dispatch { |
114 | | /* Unlocked. */ |
115 | | unsigned int magic; /*%< magic */ |
116 | | isc_tid_t tid; |
117 | | isc_socktype_t socktype; |
118 | | isc_refcount_t references; |
119 | | isc_mem_t *mctx; |
120 | | dns_dispatchmgr_t *mgr; /*%< dispatch manager */ |
121 | | isc_nmhandle_t *handle; /*%< netmgr handle for TCP connection */ |
122 | | isc_sockaddr_t local; /*%< local address */ |
123 | | isc_sockaddr_t peer; /*%< peer address (TCP) */ |
124 | | dns_transport_t *transport; /*%< TCP transport parameters */ |
125 | | |
126 | | dns_dispatchopt_t options; |
127 | | dns_dispatchstate_t state; |
128 | | |
129 | | bool reading; |
130 | | |
131 | | dns_displist_t pending; |
132 | | dns_displist_t active; |
133 | | |
134 | | uint_fast32_t requests; /*%< how many requests we have */ |
135 | | |
136 | | unsigned int timedout; |
137 | | |
138 | | struct cds_lfht_node ht_node; |
139 | | struct rcu_head rcu_head; |
140 | | }; |
141 | | |
142 | 0 | #define RESPONSE_MAGIC ISC_MAGIC('D', 'r', 's', 'p') |
143 | | #define VALID_RESPONSE(e) ISC_MAGIC_VALID((e), RESPONSE_MAGIC) |
144 | | |
145 | 0 | #define DISPATCH_MAGIC ISC_MAGIC('D', 'i', 's', 'p') |
146 | | #define VALID_DISPATCH(e) ISC_MAGIC_VALID((e), DISPATCH_MAGIC) |
147 | | |
148 | 0 | #define DNS_DISPATCHMGR_MAGIC ISC_MAGIC('D', 'M', 'g', 'r') |
149 | | #define VALID_DISPATCHMGR(e) ISC_MAGIC_VALID((e), DNS_DISPATCHMGR_MAGIC) |
150 | | |
151 | | #if DNS_DISPATCH_TRACE |
152 | | #define dns_dispentry_ref(ptr) \ |
153 | | dns_dispentry__ref(ptr, __func__, __FILE__, __LINE__) |
154 | | #define dns_dispentry_unref(ptr) \ |
155 | | dns_dispentry__unref(ptr, __func__, __FILE__, __LINE__) |
156 | | #define dns_dispentry_attach(ptr, ptrp) \ |
157 | | dns_dispentry__attach(ptr, ptrp, __func__, __FILE__, __LINE__) |
158 | | #define dns_dispentry_detach(ptrp) \ |
159 | | dns_dispentry__detach(ptrp, __func__, __FILE__, __LINE__) |
160 | | ISC_REFCOUNT_TRACE_DECL(dns_dispentry); |
161 | | #else |
162 | | ISC_REFCOUNT_DECL(dns_dispentry); |
163 | | #endif |
164 | | |
165 | | /* |
166 | | * The number of attempts to find unique <addr, port, query_id> combination |
167 | | */ |
168 | 0 | #define QID_MAX_TRIES 64 |
169 | | |
170 | | /* |
171 | | * Initial and minimum QID table sizes. |
172 | | */ |
173 | 0 | #define QIDS_INIT_SIZE (1 << 4) /* Must be power of 2 */ |
174 | 0 | #define QIDS_MIN_SIZE (1 << 4) /* Must be power of 2 */ |
175 | | |
176 | | /* |
177 | | * Statics. |
178 | | */ |
179 | | static void |
180 | | dispatchmgr_destroy(dns_dispatchmgr_t *mgr); |
181 | | |
182 | | static void |
183 | | udp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region, |
184 | | void *arg); |
185 | | static void |
186 | | tcp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region, |
187 | | void *arg); |
188 | | static void |
189 | | dispentry_cancel(dns_dispentry_t *resp, isc_result_t result); |
190 | | static isc_result_t |
191 | | dispatch_createudp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr, |
192 | | isc_tid_t tid, dns_dispatch_t **dispp); |
193 | | static void |
194 | | udp_startrecv(isc_nmhandle_t *handle, dns_dispentry_t *resp); |
195 | | static void |
196 | | udp_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp); |
197 | | static void |
198 | | tcp_startrecv(dns_dispatch_t *disp, dns_dispentry_t *resp); |
199 | | static void |
200 | | tcp_dispatch_getnext(dns_dispatch_t *disp, dns_dispentry_t *resp, |
201 | | int64_t timeout); |
202 | | static void |
203 | | udp_dispatch_getnext(dns_dispentry_t *resp, int64_t timeout); |
204 | | |
205 | | static const char * |
206 | 0 | socktype2str(dns_dispentry_t *resp) { |
207 | 0 | dns_transport_type_t transport_type = DNS_TRANSPORT_UDP; |
208 | 0 | dns_dispatch_t *disp = resp->disp; |
209 | |
|
210 | 0 | if (disp->socktype == isc_socktype_tcp) { |
211 | 0 | if (resp->transport != NULL) { |
212 | 0 | transport_type = |
213 | 0 | dns_transport_get_type(resp->transport); |
214 | 0 | } else { |
215 | 0 | transport_type = DNS_TRANSPORT_TCP; |
216 | 0 | } |
217 | 0 | } |
218 | |
|
219 | 0 | switch (transport_type) { |
220 | 0 | case DNS_TRANSPORT_UDP: |
221 | 0 | return "UDP"; |
222 | 0 | case DNS_TRANSPORT_TCP: |
223 | 0 | return "TCP"; |
224 | 0 | case DNS_TRANSPORT_TLS: |
225 | 0 | return "TLS"; |
226 | 0 | case DNS_TRANSPORT_HTTP: |
227 | 0 | return "HTTP"; |
228 | 0 | default: |
229 | 0 | return "<unexpected>"; |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | | static const char * |
234 | 0 | state2str(dns_dispatchstate_t state) { |
235 | 0 | switch (state) { |
236 | 0 | case DNS_DISPATCHSTATE_NONE: |
237 | 0 | return "none"; |
238 | 0 | case DNS_DISPATCHSTATE_CONNECTING: |
239 | 0 | return "connecting"; |
240 | 0 | case DNS_DISPATCHSTATE_CONNECTED: |
241 | 0 | return "connected"; |
242 | 0 | case DNS_DISPATCHSTATE_CANCELED: |
243 | 0 | return "canceled"; |
244 | 0 | default: |
245 | 0 | return "<unexpected>"; |
246 | 0 | } |
247 | 0 | } |
248 | | |
249 | | static void |
250 | | mgr_log(dns_dispatchmgr_t *mgr, int level, const char *fmt, ...) |
251 | | ISC_FORMAT_PRINTF(3, 4); |
252 | | |
253 | | static void |
254 | 0 | mgr_log(dns_dispatchmgr_t *mgr, int level, const char *fmt, ...) { |
255 | 0 | char msgbuf[2048]; |
256 | 0 | va_list ap; |
257 | |
|
258 | 0 | if (!isc_log_wouldlog(level)) { |
259 | 0 | return; |
260 | 0 | } |
261 | | |
262 | 0 | va_start(ap, fmt); |
263 | 0 | vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap); |
264 | 0 | va_end(ap); |
265 | |
|
266 | 0 | isc_log_write(DNS_LOGCATEGORY_DISPATCH, DNS_LOGMODULE_DISPATCH, level, |
267 | 0 | "dispatchmgr %p: %s", mgr, msgbuf); |
268 | 0 | } |
269 | | |
270 | | static void |
271 | 0 | inc_stats(dns_dispatchmgr_t *mgr, isc_statscounter_t counter) { |
272 | 0 | if (mgr->stats != NULL) { |
273 | 0 | isc_stats_increment(mgr->stats, counter); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | static void |
278 | 0 | dec_stats(dns_dispatchmgr_t *mgr, isc_statscounter_t counter) { |
279 | 0 | if (mgr->stats != NULL) { |
280 | 0 | isc_stats_decrement(mgr->stats, counter); |
281 | 0 | } |
282 | 0 | } |
283 | | |
284 | | static void |
285 | | dispatch_log(dns_dispatch_t *disp, int level, const char *fmt, ...) |
286 | | ISC_FORMAT_PRINTF(3, 4); |
287 | | |
288 | | static void |
289 | 0 | dispatch_log(dns_dispatch_t *disp, int level, const char *fmt, ...) { |
290 | 0 | char msgbuf[2048]; |
291 | 0 | va_list ap; |
292 | 0 | int r; |
293 | |
|
294 | 0 | if (!isc_log_wouldlog(level)) { |
295 | 0 | return; |
296 | 0 | } |
297 | | |
298 | 0 | va_start(ap, fmt); |
299 | 0 | r = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap); |
300 | 0 | if (r < 0) { |
301 | 0 | msgbuf[0] = '\0'; |
302 | 0 | } else if ((unsigned int)r >= sizeof(msgbuf)) { |
303 | | /* Truncated */ |
304 | 0 | msgbuf[sizeof(msgbuf) - 1] = '\0'; |
305 | 0 | } |
306 | 0 | va_end(ap); |
307 | |
|
308 | 0 | isc_log_write(DNS_LOGCATEGORY_DISPATCH, DNS_LOGMODULE_DISPATCH, level, |
309 | 0 | "dispatch %p: %s", disp, msgbuf); |
310 | 0 | } |
311 | | |
312 | | static void |
313 | | dispentry_log(dns_dispentry_t *resp, int level, const char *fmt, ...) |
314 | | ISC_FORMAT_PRINTF(3, 4); |
315 | | |
316 | | static void |
317 | 0 | dispentry_log(dns_dispentry_t *resp, int level, const char *fmt, ...) { |
318 | 0 | char msgbuf[2048]; |
319 | 0 | va_list ap; |
320 | 0 | int r; |
321 | |
|
322 | 0 | if (!isc_log_wouldlog(level)) { |
323 | 0 | return; |
324 | 0 | } |
325 | | |
326 | 0 | va_start(ap, fmt); |
327 | 0 | r = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap); |
328 | 0 | if (r < 0) { |
329 | 0 | msgbuf[0] = '\0'; |
330 | 0 | } else if ((unsigned int)r >= sizeof(msgbuf)) { |
331 | | /* Truncated */ |
332 | 0 | msgbuf[sizeof(msgbuf) - 1] = '\0'; |
333 | 0 | } |
334 | 0 | va_end(ap); |
335 | |
|
336 | 0 | dispatch_log(resp->disp, level, "%s response %p: %s", |
337 | 0 | socktype2str(resp), resp, msgbuf); |
338 | 0 | } |
339 | | |
340 | | /*% |
341 | | * Choose a random port number for a dispatch entry. |
342 | | */ |
343 | | static isc_result_t |
344 | | setup_socket(dns_dispatch_t *disp, dns_dispentry_t *resp, |
345 | 0 | const isc_sockaddr_t *dest, in_port_t *portp) { |
346 | 0 | dns_dispatchmgr_t *mgr = disp->mgr; |
347 | 0 | unsigned int nports; |
348 | 0 | in_port_t *ports = NULL; |
349 | 0 | in_port_t port = *portp; |
350 | |
|
351 | 0 | if (resp->retries++ > 5) { |
352 | 0 | return ISC_R_FAILURE; |
353 | 0 | } |
354 | | |
355 | 0 | if (isc_sockaddr_pf(&disp->local) == AF_INET) { |
356 | 0 | nports = mgr->nv4ports; |
357 | 0 | ports = mgr->v4ports; |
358 | 0 | } else { |
359 | 0 | nports = mgr->nv6ports; |
360 | 0 | ports = mgr->v6ports; |
361 | 0 | } |
362 | 0 | if (nports == 0) { |
363 | 0 | return ISC_R_ADDRNOTAVAIL; |
364 | 0 | } |
365 | | |
366 | 0 | resp->local = disp->local; |
367 | 0 | resp->peer = *dest; |
368 | |
|
369 | 0 | if (port == 0) { |
370 | 0 | port = ports[isc_random_uniform(nports)]; |
371 | 0 | isc_sockaddr_setport(&resp->local, port); |
372 | 0 | *portp = port; |
373 | 0 | } |
374 | 0 | resp->port = port; |
375 | |
|
376 | 0 | return ISC_R_SUCCESS; |
377 | 0 | } |
378 | | |
379 | | static uint32_t |
380 | 0 | qid_hash(const dns_dispentry_t *dispentry) { |
381 | 0 | isc_hash32_t hash; |
382 | |
|
383 | 0 | isc_hash32_init(&hash); |
384 | |
|
385 | 0 | isc_sockaddr_hash_ex(&hash, &dispentry->peer, true); |
386 | 0 | isc_hash32_hash(&hash, &dispentry->id, sizeof(dispentry->id), true); |
387 | 0 | isc_hash32_hash(&hash, &dispentry->port, sizeof(dispentry->port), true); |
388 | |
|
389 | 0 | return isc_hash32_finalize(&hash); |
390 | 0 | } |
391 | | |
392 | | static int |
393 | 0 | qid_match(struct cds_lfht_node *node, const void *key0) { |
394 | 0 | const dns_dispentry_t *dispentry = |
395 | 0 | caa_container_of(node, dns_dispentry_t, ht_node); |
396 | 0 | const dns_dispentry_t *key = key0; |
397 | |
|
398 | 0 | return dispentry->id == key->id && dispentry->port == key->port && |
399 | 0 | isc_sockaddr_equal(&dispentry->peer, &key->peer); |
400 | 0 | } |
401 | | |
402 | | static void |
403 | 0 | dispentry_destroy_rcu(struct rcu_head *rcu_head) { |
404 | 0 | dns_dispentry_t *resp = caa_container_of(rcu_head, dns_dispentry_t, |
405 | 0 | rcu_head); |
406 | 0 | isc_mem_putanddetach(&resp->mctx, resp, sizeof(*resp)); |
407 | 0 | } |
408 | | |
409 | | static void |
410 | 0 | dispentry_destroy(dns_dispentry_t *resp) { |
411 | 0 | dns_dispatch_t *disp = resp->disp; |
412 | | |
413 | | /* |
414 | | * We need to call this from here in case there's an external event that |
415 | | * shuts down our dispatch (like ISC_R_SHUTTINGDOWN). |
416 | | */ |
417 | 0 | dispentry_cancel(resp, ISC_R_CANCELED); |
418 | |
|
419 | 0 | INSIST(disp->requests > 0); |
420 | 0 | disp->requests--; |
421 | |
|
422 | 0 | resp->magic = 0; |
423 | |
|
424 | 0 | INSIST(!ISC_LINK_LINKED(resp, plink)); |
425 | 0 | INSIST(!ISC_LINK_LINKED(resp, alink)); |
426 | 0 | INSIST(!ISC_LINK_LINKED(resp, rlink)); |
427 | |
|
428 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "destroying"); |
429 | |
|
430 | 0 | if (resp->handle != NULL) { |
431 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
432 | 0 | "detaching handle %p from %p", resp->handle, |
433 | 0 | &resp->handle); |
434 | 0 | isc_nmhandle_detach(&resp->handle); |
435 | 0 | } |
436 | |
|
437 | 0 | if (resp->tlsctx_cache != NULL) { |
438 | 0 | isc_tlsctx_cache_detach(&resp->tlsctx_cache); |
439 | 0 | } |
440 | |
|
441 | 0 | if (resp->transport != NULL) { |
442 | 0 | dns_transport_detach(&resp->transport); |
443 | 0 | } |
444 | |
|
445 | 0 | dns_dispatch_detach(&disp); /* DISPATCH001 */ |
446 | |
|
447 | 0 | call_rcu(&resp->rcu_head, dispentry_destroy_rcu); |
448 | 0 | } |
449 | | |
450 | | #if DNS_DISPATCH_TRACE |
451 | | ISC_REFCOUNT_TRACE_IMPL(dns_dispentry, dispentry_destroy); |
452 | | #else |
453 | | ISC_REFCOUNT_IMPL(dns_dispentry, dispentry_destroy); |
454 | | #endif |
455 | | |
456 | | /* |
457 | | * How long in milliseconds has it been since this dispentry |
458 | | * started reading? |
459 | | */ |
460 | | static unsigned int |
461 | 0 | dispentry_runtime(dns_dispentry_t *resp, const isc_time_t *now) { |
462 | 0 | if (isc_time_isepoch(&resp->start)) { |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | 0 | return isc_time_microdiff(now, &resp->start) / 1000; |
467 | 0 | } |
468 | | |
469 | | /* |
470 | | * General flow: |
471 | | * |
472 | | * If I/O result == CANCELED or error, free the buffer. |
473 | | * |
474 | | * If query, free the buffer, restart. |
475 | | * |
476 | | * If response: |
477 | | * Allocate event, fill in details. |
478 | | * If cannot allocate, free buffer, restart. |
479 | | * find target. If not found, free buffer, restart. |
480 | | * if event queue is not empty, queue. else, send. |
481 | | * restart. |
482 | | */ |
483 | | static void |
484 | | udp_recv(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region, |
485 | 0 | void *arg) { |
486 | 0 | dns_dispentry_t *resp = (dns_dispentry_t *)arg; |
487 | 0 | dns_dispatch_t *disp = NULL; |
488 | 0 | dns_messageid_t id; |
489 | 0 | isc_result_t dres; |
490 | 0 | isc_buffer_t source; |
491 | 0 | unsigned int flags; |
492 | 0 | isc_sockaddr_t peer; |
493 | 0 | isc_netaddr_t netaddr; |
494 | 0 | int match; |
495 | 0 | int64_t timeout = 0; |
496 | 0 | bool respond = true; |
497 | 0 | isc_time_t now; |
498 | |
|
499 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
500 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
501 | |
|
502 | 0 | disp = resp->disp; |
503 | |
|
504 | 0 | REQUIRE(disp->tid == isc_tid()); |
505 | 0 | INSIST(resp->reading); |
506 | 0 | resp->reading = false; |
507 | |
|
508 | 0 | if (resp->state == DNS_DISPATCHSTATE_CANCELED) { |
509 | | /* |
510 | | * Nobody is interested in the callback if the response |
511 | | * has been canceled already. Detach from the response |
512 | | * and the handle. |
513 | | */ |
514 | 0 | respond = false; |
515 | 0 | eresult = ISC_R_CANCELED; |
516 | 0 | } |
517 | |
|
518 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
519 | 0 | "read callback:%s, requests %" PRIuFAST32, |
520 | 0 | isc_result_totext(eresult), disp->requests); |
521 | |
|
522 | 0 | if (eresult != ISC_R_SUCCESS) { |
523 | | /* |
524 | | * This is most likely a network error on a connected |
525 | | * socket, a timeout, or the query has been canceled. |
526 | | * It makes no sense to check the address or parse the |
527 | | * packet, but we can return the error to the caller. |
528 | | */ |
529 | 0 | goto done; |
530 | 0 | } |
531 | | |
532 | 0 | peer = isc_nmhandle_peeraddr(handle); |
533 | 0 | isc_netaddr_fromsockaddr(&netaddr, &peer); |
534 | | |
535 | | /* |
536 | | * If this is from a blackholed address, drop it. |
537 | | */ |
538 | 0 | if (disp->mgr->blackhole != NULL && |
539 | 0 | dns_acl_match(&netaddr, NULL, disp->mgr->blackhole, NULL, &match, |
540 | 0 | NULL) == ISC_R_SUCCESS && |
541 | 0 | match > 0) |
542 | 0 | { |
543 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(10))) { |
544 | 0 | char netaddrstr[ISC_NETADDR_FORMATSIZE]; |
545 | 0 | isc_netaddr_format(&netaddr, netaddrstr, |
546 | 0 | sizeof(netaddrstr)); |
547 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(10), |
548 | 0 | "blackholed packet from %s", netaddrstr); |
549 | 0 | } |
550 | 0 | goto next; |
551 | 0 | } |
552 | | |
553 | | /* |
554 | | * Peek into the buffer to see what we can see. |
555 | | */ |
556 | 0 | id = resp->id; |
557 | 0 | isc_buffer_init(&source, region->base, region->length); |
558 | 0 | isc_buffer_add(&source, region->length); |
559 | 0 | dres = dns_message_peekheader(&source, &id, &flags); |
560 | 0 | if (dres != ISC_R_SUCCESS) { |
561 | 0 | char netaddrstr[ISC_NETADDR_FORMATSIZE]; |
562 | 0 | isc_netaddr_format(&netaddr, netaddrstr, sizeof(netaddrstr)); |
563 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(10), |
564 | 0 | "got garbage packet from %s", netaddrstr); |
565 | 0 | goto next; |
566 | 0 | } |
567 | | |
568 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(92), |
569 | 0 | "got valid DNS message header, /QR %c, id %u", |
570 | 0 | ((flags & DNS_MESSAGEFLAG_QR) != 0) ? '1' : '0', id); |
571 | | |
572 | | /* |
573 | | * Look at the message flags. If it's a query, ignore it. |
574 | | */ |
575 | 0 | if ((flags & DNS_MESSAGEFLAG_QR) == 0) { |
576 | 0 | goto next; |
577 | 0 | } |
578 | | |
579 | | /* |
580 | | * The QID and the address must match the expected ones. |
581 | | */ |
582 | 0 | if (resp->id != id || !isc_sockaddr_equal(&peer, &resp->peer)) { |
583 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
584 | 0 | "response doesn't match"); |
585 | 0 | inc_stats(disp->mgr, dns_resstatscounter_mismatch); |
586 | 0 | goto next; |
587 | 0 | } |
588 | | |
589 | | /* |
590 | | * We have the right resp, so call the caller back. |
591 | | */ |
592 | 0 | goto done; |
593 | | |
594 | 0 | next: |
595 | | /* |
596 | | * This is the wrong response. Check whether there is still enough |
597 | | * time to wait for the correct one to arrive before the timeout fires. |
598 | | */ |
599 | 0 | now = isc_loop_now(resp->loop); |
600 | 0 | if (resp->timeout > 0) { |
601 | 0 | timeout = resp->timeout - dispentry_runtime(resp, &now); |
602 | 0 | if (timeout <= 0) { |
603 | | /* |
604 | | * The time window for receiving the correct response is |
605 | | * already closed, libuv has just not processed the |
606 | | * socket timer yet. Invoke the read callback, |
607 | | * indicating a timeout. |
608 | | */ |
609 | 0 | eresult = ISC_R_TIMEDOUT; |
610 | 0 | goto done; |
611 | 0 | } |
612 | 0 | } |
613 | | |
614 | | /* |
615 | | * Do not invoke the read callback just yet and instead wait for the |
616 | | * proper response to arrive until the original timeout fires. |
617 | | */ |
618 | 0 | respond = false; |
619 | 0 | udp_dispatch_getnext(resp, timeout); |
620 | |
|
621 | 0 | done: |
622 | 0 | if (respond) { |
623 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
624 | 0 | "UDP read callback on %p: %s", handle, |
625 | 0 | isc_result_totext(eresult)); |
626 | 0 | resp->response(eresult, region, resp->arg); |
627 | 0 | } |
628 | |
|
629 | 0 | dns_dispentry_detach(&resp); /* DISPENTRY003 */ |
630 | 0 | } |
631 | | |
632 | | static isc_result_t |
633 | 0 | tcp_recv_oldest(dns_dispatch_t *disp, dns_dispentry_t **respp) { |
634 | 0 | dns_dispentry_t *resp = NULL; |
635 | 0 | resp = ISC_LIST_HEAD(disp->active); |
636 | 0 | if (resp != NULL) { |
637 | 0 | disp->timedout++; |
638 | |
|
639 | 0 | *respp = resp; |
640 | 0 | return ISC_R_TIMEDOUT; |
641 | 0 | } |
642 | | |
643 | 0 | return ISC_R_NOTFOUND; |
644 | 0 | } |
645 | | |
646 | | /* |
647 | | * NOTE: Must be RCU read locked! |
648 | | */ |
649 | | static isc_result_t |
650 | | tcp_recv_success(dns_dispatch_t *disp, isc_region_t *region, |
651 | 0 | isc_sockaddr_t *peer, dns_dispentry_t **respp) { |
652 | 0 | isc_buffer_t source; |
653 | 0 | dns_messageid_t id; |
654 | 0 | unsigned int flags; |
655 | 0 | isc_result_t result = ISC_R_SUCCESS; |
656 | |
|
657 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
658 | 0 | "TCP read success, length == %d, addr = %p", |
659 | 0 | region->length, region->base); |
660 | | |
661 | | /* |
662 | | * Peek into the buffer to see what we can see. |
663 | | */ |
664 | 0 | isc_buffer_init(&source, region->base, region->length); |
665 | 0 | isc_buffer_add(&source, region->length); |
666 | 0 | result = dns_message_peekheader(&source, &id, &flags); |
667 | 0 | if (result != ISC_R_SUCCESS) { |
668 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(10), "got garbage packet"); |
669 | 0 | return ISC_R_UNEXPECTED; |
670 | 0 | } |
671 | | |
672 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(92), |
673 | 0 | "got valid DNS message header, /QR %c, id %u", |
674 | 0 | ((flags & DNS_MESSAGEFLAG_QR) != 0) ? '1' : '0', id); |
675 | | |
676 | | /* |
677 | | * Look at the message flags. If it's a query, ignore it and keep |
678 | | * reading. |
679 | | */ |
680 | 0 | if ((flags & DNS_MESSAGEFLAG_QR) == 0) { |
681 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(10), |
682 | 0 | "got DNS query instead of answer"); |
683 | 0 | return ISC_R_UNEXPECTED; |
684 | 0 | } |
685 | | |
686 | | /* |
687 | | * We have a valid response; find the associated dispentry object |
688 | | * and call the caller back. |
689 | | */ |
690 | 0 | dns_dispentry_t key = { |
691 | 0 | .id = id, |
692 | 0 | .peer = *peer, |
693 | 0 | .port = isc_sockaddr_getport(&disp->local), |
694 | 0 | }; |
695 | 0 | struct cds_lfht_iter iter; |
696 | 0 | cds_lfht_lookup(disp->mgr->qids, qid_hash(&key), qid_match, &key, |
697 | 0 | &iter); |
698 | |
|
699 | 0 | dns_dispentry_t *resp = cds_lfht_entry(cds_lfht_iter_get_node(&iter), |
700 | 0 | dns_dispentry_t, ht_node); |
701 | | |
702 | | /* Skip responses that are not ours */ |
703 | 0 | if (resp != NULL && resp->disp == disp) { |
704 | 0 | if (!resp->reading) { |
705 | | /* |
706 | | * We already got a message for this QID and weren't |
707 | | * expecting any more. |
708 | | */ |
709 | 0 | result = ISC_R_UNEXPECTED; |
710 | 0 | } else { |
711 | 0 | *respp = resp; |
712 | 0 | } |
713 | 0 | } else { |
714 | 0 | result = ISC_R_NOTFOUND; |
715 | 0 | } |
716 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
717 | 0 | "search for response in hashtable: %s", |
718 | 0 | isc_result_totext(result)); |
719 | |
|
720 | 0 | return result; |
721 | 0 | } |
722 | | |
723 | | static void |
724 | | tcp_recv_add(dns_displist_t *resps, dns_dispentry_t *resp, |
725 | 0 | isc_result_t result) { |
726 | 0 | dns_dispentry_ref(resp); /* DISPENTRY009 */ |
727 | 0 | ISC_LIST_UNLINK(resp->disp->active, resp, alink); |
728 | 0 | ISC_LIST_APPEND(*resps, resp, rlink); |
729 | 0 | INSIST(resp->reading); |
730 | 0 | resp->reading = false; |
731 | 0 | resp->result = result; |
732 | 0 | } |
733 | | |
734 | | static void |
735 | | tcp_recv_shutdown(dns_dispatch_t *disp, dns_displist_t *resps, |
736 | 0 | isc_result_t result) { |
737 | | /* |
738 | | * If there are any active responses, shut them all down. |
739 | | */ |
740 | 0 | ISC_LIST_FOREACH(disp->active, resp, alink) { |
741 | 0 | tcp_recv_add(resps, resp, result); |
742 | 0 | } |
743 | 0 | disp->state = DNS_DISPATCHSTATE_CANCELED; |
744 | 0 | } |
745 | | |
746 | | static void |
747 | 0 | tcp_recv_processall(dns_displist_t *resps, isc_region_t *region) { |
748 | 0 | ISC_LIST_FOREACH(*resps, resp, rlink) { |
749 | 0 | ISC_LIST_UNLINK(*resps, resp, rlink); |
750 | |
|
751 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "read callback: %s", |
752 | 0 | isc_result_totext(resp->result)); |
753 | 0 | resp->response(resp->result, region, resp->arg); |
754 | 0 | dns_dispentry_detach(&resp); /* DISPENTRY009 */ |
755 | 0 | } |
756 | 0 | } |
757 | | |
758 | | /* |
759 | | * General flow: |
760 | | * |
761 | | * If I/O result == CANCELED, EOF, or error, notify everyone as the |
762 | | * various queues drain. |
763 | | * |
764 | | * If response: |
765 | | * Allocate event, fill in details. |
766 | | * If cannot allocate, restart. |
767 | | * find target. If not found, restart. |
768 | | * if event queue is not empty, queue. else, send. |
769 | | * restart. |
770 | | */ |
771 | | static void |
772 | | tcp_recv(isc_nmhandle_t *handle, isc_result_t result, isc_region_t *region, |
773 | 0 | void *arg) { |
774 | 0 | dns_dispatch_t *disp = (dns_dispatch_t *)arg; |
775 | 0 | dns_dispentry_t *resp = NULL; |
776 | 0 | char buf[ISC_SOCKADDR_FORMATSIZE]; |
777 | 0 | isc_sockaddr_t peer; |
778 | 0 | dns_displist_t resps = ISC_LIST_INITIALIZER; |
779 | 0 | isc_time_t now; |
780 | 0 | int timeout = 0; |
781 | |
|
782 | 0 | REQUIRE(VALID_DISPATCH(disp)); |
783 | |
|
784 | 0 | REQUIRE(disp->tid == isc_tid()); |
785 | 0 | INSIST(disp->reading); |
786 | 0 | disp->reading = false; |
787 | |
|
788 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
789 | 0 | "TCP read:%s:requests %" PRIuFAST32, |
790 | 0 | isc_result_totext(result), disp->requests); |
791 | |
|
792 | 0 | peer = isc_nmhandle_peeraddr(handle); |
793 | |
|
794 | 0 | rcu_read_lock(); |
795 | | /* |
796 | | * Phase 1: Process timeout and success. |
797 | | */ |
798 | 0 | switch (result) { |
799 | 0 | case ISC_R_TIMEDOUT: |
800 | | /* |
801 | | * Time out the oldest response in the active queue. |
802 | | */ |
803 | 0 | result = tcp_recv_oldest(disp, &resp); |
804 | 0 | break; |
805 | 0 | case ISC_R_SUCCESS: |
806 | | /* We got an answer */ |
807 | 0 | result = tcp_recv_success(disp, region, &peer, &resp); |
808 | 0 | break; |
809 | | |
810 | 0 | default: |
811 | 0 | break; |
812 | 0 | } |
813 | | |
814 | 0 | if (resp != NULL) { |
815 | 0 | tcp_recv_add(&resps, resp, result); |
816 | 0 | } |
817 | | |
818 | | /* |
819 | | * Phase 2: Look if we timed out before. |
820 | | */ |
821 | |
|
822 | 0 | if (result == ISC_R_NOTFOUND) { |
823 | 0 | if (disp->timedout > 0) { |
824 | | /* There was active query that timed-out before */ |
825 | 0 | disp->timedout--; |
826 | 0 | } else { |
827 | 0 | result = ISC_R_UNEXPECTED; |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | | /* |
832 | | * Phase 3: Trigger timeouts. It's possible that the responses would |
833 | | * have been timed out out already, but non-matching TCP reads have |
834 | | * prevented this. |
835 | | */ |
836 | 0 | resp = ISC_LIST_HEAD(disp->active); |
837 | 0 | if (resp != NULL) { |
838 | 0 | now = isc_loop_now(resp->loop); |
839 | 0 | } |
840 | 0 | while (resp != NULL) { |
841 | 0 | dns_dispentry_t *next = ISC_LIST_NEXT(resp, alink); |
842 | |
|
843 | 0 | if (resp->timeout > 0) { |
844 | 0 | timeout = resp->timeout - dispentry_runtime(resp, &now); |
845 | 0 | if (timeout <= 0) { |
846 | 0 | tcp_recv_add(&resps, resp, ISC_R_TIMEDOUT); |
847 | 0 | } |
848 | 0 | } |
849 | |
|
850 | 0 | resp = next; |
851 | 0 | } |
852 | | |
853 | | /* |
854 | | * Phase 4: log if we errored out. |
855 | | */ |
856 | 0 | switch (result) { |
857 | 0 | case ISC_R_SUCCESS: |
858 | 0 | case ISC_R_TIMEDOUT: |
859 | 0 | case ISC_R_NOTFOUND: |
860 | 0 | break; |
861 | | |
862 | 0 | case ISC_R_SHUTTINGDOWN: |
863 | 0 | case ISC_R_CANCELED: |
864 | 0 | case ISC_R_EOF: |
865 | 0 | case ISC_R_CONNECTIONRESET: |
866 | 0 | isc_sockaddr_format(&peer, buf, sizeof(buf)); |
867 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
868 | 0 | "shutting down TCP: %s: %s", buf, |
869 | 0 | isc_result_totext(result)); |
870 | 0 | tcp_recv_shutdown(disp, &resps, result); |
871 | 0 | break; |
872 | 0 | default: |
873 | 0 | isc_sockaddr_format(&peer, buf, sizeof(buf)); |
874 | 0 | dispatch_log(disp, ISC_LOG_ERROR, |
875 | 0 | "shutting down due to TCP " |
876 | 0 | "receive error: %s: %s", |
877 | 0 | buf, isc_result_totext(result)); |
878 | 0 | tcp_recv_shutdown(disp, &resps, result); |
879 | 0 | break; |
880 | 0 | } |
881 | | |
882 | | /* |
883 | | * Phase 5: Resume reading if there are still active responses |
884 | | */ |
885 | 0 | resp = ISC_LIST_HEAD(disp->active); |
886 | 0 | if (resp != NULL) { |
887 | 0 | if (resp->timeout > 0) { |
888 | 0 | timeout = resp->timeout - dispentry_runtime(resp, &now); |
889 | 0 | INSIST(timeout > 0); |
890 | 0 | } |
891 | 0 | tcp_startrecv(disp, resp); |
892 | 0 | if (timeout > 0) { |
893 | 0 | isc_nmhandle_settimeout(handle, timeout); |
894 | 0 | } |
895 | 0 | } |
896 | |
|
897 | 0 | rcu_read_unlock(); |
898 | | |
899 | | /* |
900 | | * Phase 6: Process all scheduled callbacks. |
901 | | */ |
902 | 0 | tcp_recv_processall(&resps, region); |
903 | |
|
904 | 0 | dns_dispatch_detach(&disp); /* DISPATCH002 */ |
905 | 0 | } |
906 | | |
907 | | /*% |
908 | | * Create a temporary port list to set the initial default set of dispatch |
909 | | * ephemeral ports. This is almost meaningless as the application will |
910 | | * normally set the ports explicitly, but is provided to fill some minor corner |
911 | | * cases. |
912 | | */ |
913 | | static void |
914 | 0 | create_default_portset(isc_mem_t *mctx, int family, isc_portset_t **portsetp) { |
915 | 0 | in_port_t low, high; |
916 | |
|
917 | 0 | isc_net_getudpportrange(family, &low, &high); |
918 | |
|
919 | 0 | isc_portset_create(mctx, portsetp); |
920 | 0 | isc_portset_addrange(*portsetp, low, high); |
921 | 0 | } |
922 | | |
923 | | static isc_result_t |
924 | | setavailports(dns_dispatchmgr_t *mgr, isc_portset_t *v4portset, |
925 | 0 | isc_portset_t *v6portset) { |
926 | 0 | in_port_t *v4ports, *v6ports, p = 0; |
927 | 0 | unsigned int nv4ports, nv6ports, i4 = 0, i6 = 0; |
928 | |
|
929 | 0 | nv4ports = isc_portset_nports(v4portset); |
930 | 0 | nv6ports = isc_portset_nports(v6portset); |
931 | |
|
932 | 0 | v4ports = NULL; |
933 | 0 | if (nv4ports != 0) { |
934 | 0 | v4ports = isc_mem_cget(mgr->mctx, nv4ports, sizeof(in_port_t)); |
935 | 0 | } |
936 | 0 | v6ports = NULL; |
937 | 0 | if (nv6ports != 0) { |
938 | 0 | v6ports = isc_mem_cget(mgr->mctx, nv6ports, sizeof(in_port_t)); |
939 | 0 | } |
940 | |
|
941 | 0 | do { |
942 | 0 | if (isc_portset_isset(v4portset, p)) { |
943 | 0 | INSIST(i4 < nv4ports); |
944 | 0 | v4ports[i4++] = p; |
945 | 0 | } |
946 | 0 | if (isc_portset_isset(v6portset, p)) { |
947 | 0 | INSIST(i6 < nv6ports); |
948 | 0 | v6ports[i6++] = p; |
949 | 0 | } |
950 | 0 | } while (p++ < 65535); |
951 | 0 | INSIST(i4 == nv4ports && i6 == nv6ports); |
952 | |
|
953 | 0 | if (mgr->v4ports != NULL) { |
954 | 0 | isc_mem_cput(mgr->mctx, mgr->v4ports, mgr->nv4ports, |
955 | 0 | sizeof(in_port_t)); |
956 | 0 | } |
957 | 0 | mgr->v4ports = v4ports; |
958 | 0 | mgr->nv4ports = nv4ports; |
959 | |
|
960 | 0 | if (mgr->v6ports != NULL) { |
961 | 0 | isc_mem_cput(mgr->mctx, mgr->v6ports, mgr->nv6ports, |
962 | 0 | sizeof(in_port_t)); |
963 | 0 | } |
964 | 0 | mgr->v6ports = v6ports; |
965 | 0 | mgr->nv6ports = nv6ports; |
966 | |
|
967 | 0 | return ISC_R_SUCCESS; |
968 | 0 | } |
969 | | |
970 | | /* |
971 | | * Publics. |
972 | | */ |
973 | | |
974 | | isc_result_t |
975 | 0 | dns_dispatchmgr_create(isc_mem_t *mctx, dns_dispatchmgr_t **mgrp) { |
976 | 0 | dns_dispatchmgr_t *mgr = NULL; |
977 | 0 | isc_portset_t *v4portset = NULL; |
978 | 0 | isc_portset_t *v6portset = NULL; |
979 | |
|
980 | 0 | REQUIRE(mctx != NULL); |
981 | 0 | REQUIRE(mgrp != NULL && *mgrp == NULL); |
982 | |
|
983 | 0 | mgr = isc_mem_get(mctx, sizeof(dns_dispatchmgr_t)); |
984 | 0 | *mgr = (dns_dispatchmgr_t){ |
985 | 0 | .magic = 0, |
986 | 0 | .nloops = isc_loopmgr_nloops(), |
987 | 0 | }; |
988 | |
|
989 | | #if DNS_DISPATCH_TRACE |
990 | | fprintf(stderr, "dns_dispatchmgr__init:%s:%s:%d:%p->references = 1\n", |
991 | | __func__, __FILE__, __LINE__, mgr); |
992 | | #endif |
993 | 0 | isc_refcount_init(&mgr->references, 1); |
994 | |
|
995 | 0 | isc_mem_attach(mctx, &mgr->mctx); |
996 | |
|
997 | 0 | mgr->tcps = isc_mem_cget(mgr->mctx, mgr->nloops, sizeof(mgr->tcps[0])); |
998 | 0 | for (size_t i = 0; i < mgr->nloops; i++) { |
999 | 0 | mgr->tcps[i] = cds_lfht_new( |
1000 | 0 | 2, 2, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
1001 | 0 | NULL); |
1002 | 0 | } |
1003 | |
|
1004 | 0 | create_default_portset(mgr->mctx, AF_INET, &v4portset); |
1005 | 0 | create_default_portset(mgr->mctx, AF_INET6, &v6portset); |
1006 | |
|
1007 | 0 | setavailports(mgr, v4portset, v6portset); |
1008 | |
|
1009 | 0 | isc_portset_destroy(mgr->mctx, &v4portset); |
1010 | 0 | isc_portset_destroy(mgr->mctx, &v6portset); |
1011 | |
|
1012 | 0 | mgr->qids = cds_lfht_new(QIDS_INIT_SIZE, QIDS_MIN_SIZE, 0, |
1013 | 0 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
1014 | 0 | NULL); |
1015 | |
|
1016 | 0 | mgr->magic = DNS_DISPATCHMGR_MAGIC; |
1017 | |
|
1018 | 0 | *mgrp = mgr; |
1019 | 0 | return ISC_R_SUCCESS; |
1020 | 0 | } |
1021 | | |
1022 | | #if DNS_DISPATCH_TRACE |
1023 | | ISC_REFCOUNT_TRACE_IMPL(dns_dispatchmgr, dispatchmgr_destroy); |
1024 | | #else |
1025 | | ISC_REFCOUNT_IMPL(dns_dispatchmgr, dispatchmgr_destroy); |
1026 | | #endif |
1027 | | |
1028 | | void |
1029 | 0 | dns_dispatchmgr_setblackhole(dns_dispatchmgr_t *mgr, dns_acl_t *blackhole) { |
1030 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1031 | 0 | if (mgr->blackhole != NULL) { |
1032 | 0 | dns_acl_detach(&mgr->blackhole); |
1033 | 0 | } |
1034 | 0 | dns_acl_attach(blackhole, &mgr->blackhole); |
1035 | 0 | } |
1036 | | |
1037 | | dns_acl_t * |
1038 | 0 | dns_dispatchmgr_getblackhole(dns_dispatchmgr_t *mgr) { |
1039 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1040 | 0 | return mgr->blackhole; |
1041 | 0 | } |
1042 | | |
1043 | | isc_result_t |
1044 | | dns_dispatchmgr_setavailports(dns_dispatchmgr_t *mgr, isc_portset_t *v4portset, |
1045 | 0 | isc_portset_t *v6portset) { |
1046 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1047 | 0 | return setavailports(mgr, v4portset, v6portset); |
1048 | 0 | } |
1049 | | |
1050 | | static void |
1051 | 0 | dispatchmgr_destroy(dns_dispatchmgr_t *mgr) { |
1052 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1053 | |
|
1054 | 0 | isc_refcount_destroy(&mgr->references); |
1055 | |
|
1056 | 0 | mgr->magic = 0; |
1057 | |
|
1058 | 0 | RUNTIME_CHECK(!cds_lfht_destroy(mgr->qids, NULL)); |
1059 | |
|
1060 | 0 | for (size_t i = 0; i < mgr->nloops; i++) { |
1061 | 0 | RUNTIME_CHECK(!cds_lfht_destroy(mgr->tcps[i], NULL)); |
1062 | 0 | } |
1063 | 0 | isc_mem_cput(mgr->mctx, mgr->tcps, mgr->nloops, sizeof(mgr->tcps[0])); |
1064 | |
|
1065 | 0 | if (mgr->blackhole != NULL) { |
1066 | 0 | dns_acl_detach(&mgr->blackhole); |
1067 | 0 | } |
1068 | |
|
1069 | 0 | if (mgr->stats != NULL) { |
1070 | 0 | isc_stats_detach(&mgr->stats); |
1071 | 0 | } |
1072 | |
|
1073 | 0 | if (mgr->v4ports != NULL) { |
1074 | 0 | isc_mem_cput(mgr->mctx, mgr->v4ports, mgr->nv4ports, |
1075 | 0 | sizeof(in_port_t)); |
1076 | 0 | } |
1077 | 0 | if (mgr->v6ports != NULL) { |
1078 | 0 | isc_mem_cput(mgr->mctx, mgr->v6ports, mgr->nv6ports, |
1079 | 0 | sizeof(in_port_t)); |
1080 | 0 | } |
1081 | |
|
1082 | 0 | isc_mem_putanddetach(&mgr->mctx, mgr, sizeof(dns_dispatchmgr_t)); |
1083 | 0 | } |
1084 | | |
1085 | | void |
1086 | 0 | dns_dispatchmgr_setstats(dns_dispatchmgr_t *mgr, isc_stats_t *stats) { |
1087 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1088 | 0 | REQUIRE(mgr->stats == NULL); |
1089 | |
|
1090 | 0 | isc_stats_attach(stats, &mgr->stats); |
1091 | 0 | } |
1092 | | |
1093 | | /* |
1094 | | * Allocate and set important limits. |
1095 | | */ |
1096 | | static void |
1097 | | dispatch_allocate(dns_dispatchmgr_t *mgr, isc_socktype_t type, isc_tid_t tid, |
1098 | 0 | dns_dispatch_t **dispp) { |
1099 | 0 | dns_dispatch_t *disp = NULL; |
1100 | |
|
1101 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1102 | 0 | REQUIRE(dispp != NULL && *dispp == NULL); |
1103 | | |
1104 | | /* |
1105 | | * Set up the dispatcher, mostly. Don't bother setting some of |
1106 | | * the options that are controlled by tcp vs. udp, etc. |
1107 | | */ |
1108 | |
|
1109 | 0 | disp = isc_mem_get(mgr->mctx, sizeof(*disp)); |
1110 | 0 | *disp = (dns_dispatch_t){ |
1111 | 0 | .socktype = type, |
1112 | 0 | .active = ISC_LIST_INITIALIZER, |
1113 | 0 | .pending = ISC_LIST_INITIALIZER, |
1114 | 0 | .tid = tid, |
1115 | 0 | .magic = DISPATCH_MAGIC, |
1116 | 0 | }; |
1117 | |
|
1118 | 0 | isc_mem_attach(mgr->mctx, &disp->mctx); |
1119 | |
|
1120 | 0 | dns_dispatchmgr_attach(mgr, &disp->mgr); |
1121 | | #if DNS_DISPATCH_TRACE |
1122 | | fprintf(stderr, "dns_dispatch__init:%s:%s:%d:%p->references = 1\n", |
1123 | | __func__, __FILE__, __LINE__, disp); |
1124 | | #endif |
1125 | 0 | isc_refcount_init(&disp->references, 1); /* DISPATCH000 */ |
1126 | |
|
1127 | 0 | *dispp = disp; |
1128 | 0 | } |
1129 | | |
1130 | | struct dispatch_key { |
1131 | | const isc_sockaddr_t *local; |
1132 | | const isc_sockaddr_t *peer; |
1133 | | const dns_transport_t *transport; |
1134 | | }; |
1135 | | |
1136 | | static uint32_t |
1137 | 0 | dispatch_hash(struct dispatch_key *key) { |
1138 | 0 | uint32_t hashval = isc_sockaddr_hash(key->peer, false); |
1139 | 0 | if (key->local) { |
1140 | 0 | hashval ^= isc_sockaddr_hash(key->local, true); |
1141 | 0 | } |
1142 | |
|
1143 | 0 | return hashval; |
1144 | 0 | } |
1145 | | |
1146 | | static int |
1147 | 0 | dispatch_match(struct cds_lfht_node *node, const void *key0) { |
1148 | 0 | dns_dispatch_t *disp = caa_container_of(node, dns_dispatch_t, ht_node); |
1149 | 0 | const struct dispatch_key *key = key0; |
1150 | |
|
1151 | 0 | return disp->transport == key->transport && |
1152 | 0 | isc_sockaddr_equal(&disp->peer, key->peer) && |
1153 | 0 | (key->local == NULL || |
1154 | 0 | isc_sockaddr_equal(&disp->local, key->local)); |
1155 | 0 | } |
1156 | | |
1157 | | isc_result_t |
1158 | | dns_dispatch_createtcp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr, |
1159 | | const isc_sockaddr_t *destaddr, |
1160 | | dns_transport_t *transport, dns_dispatchopt_t options, |
1161 | 0 | dns_dispatch_t **dispp) { |
1162 | 0 | dns_dispatch_t *disp = NULL; |
1163 | 0 | isc_tid_t tid = isc_tid(); |
1164 | |
|
1165 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1166 | 0 | REQUIRE(destaddr != NULL); |
1167 | |
|
1168 | 0 | dispatch_allocate(mgr, isc_socktype_tcp, tid, &disp); |
1169 | |
|
1170 | 0 | disp->options = options; |
1171 | 0 | disp->peer = *destaddr; |
1172 | 0 | if (transport != NULL) { |
1173 | 0 | dns_transport_attach(transport, &disp->transport); |
1174 | 0 | } |
1175 | |
|
1176 | 0 | if (localaddr != NULL) { |
1177 | 0 | disp->local = *localaddr; |
1178 | 0 | } else { |
1179 | 0 | int pf; |
1180 | 0 | pf = isc_sockaddr_pf(destaddr); |
1181 | 0 | isc_sockaddr_anyofpf(&disp->local, pf); |
1182 | 0 | isc_sockaddr_setport(&disp->local, 0); |
1183 | 0 | } |
1184 | | |
1185 | | /* |
1186 | | * Append it to the dispatcher list. |
1187 | | */ |
1188 | 0 | struct dispatch_key key = { |
1189 | 0 | .local = &disp->local, |
1190 | 0 | .peer = &disp->peer, |
1191 | 0 | .transport = transport, |
1192 | 0 | }; |
1193 | |
|
1194 | 0 | if ((disp->options & DNS_DISPATCHOPT_UNSHARED) == 0) { |
1195 | 0 | rcu_read_lock(); |
1196 | 0 | cds_lfht_add(mgr->tcps[tid], dispatch_hash(&key), |
1197 | 0 | &disp->ht_node); |
1198 | 0 | rcu_read_unlock(); |
1199 | 0 | } |
1200 | |
|
1201 | 0 | if (isc_log_wouldlog(90)) { |
1202 | 0 | char addrbuf[ISC_SOCKADDR_FORMATSIZE]; |
1203 | |
|
1204 | 0 | isc_sockaddr_format(&disp->local, addrbuf, |
1205 | 0 | ISC_SOCKADDR_FORMATSIZE); |
1206 | |
|
1207 | 0 | mgr_log(mgr, ISC_LOG_DEBUG(90), |
1208 | 0 | "dns_dispatch_createtcp: created TCP dispatch %p for " |
1209 | 0 | "%s", |
1210 | 0 | disp, addrbuf); |
1211 | 0 | } |
1212 | 0 | *dispp = disp; |
1213 | |
|
1214 | 0 | return ISC_R_SUCCESS; |
1215 | 0 | } |
1216 | | |
1217 | | isc_result_t |
1218 | | dns_dispatch_gettcp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *destaddr, |
1219 | | const isc_sockaddr_t *localaddr, dns_transport_t *transport, |
1220 | 0 | dns_dispatch_t **dispp) { |
1221 | 0 | dns_dispatch_t *disp_connected = NULL; |
1222 | 0 | dns_dispatch_t *disp_fallback = NULL; |
1223 | 0 | isc_result_t result = ISC_R_NOTFOUND; |
1224 | 0 | isc_tid_t tid = isc_tid(); |
1225 | |
|
1226 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1227 | 0 | REQUIRE(destaddr != NULL); |
1228 | 0 | REQUIRE(dispp != NULL && *dispp == NULL); |
1229 | |
|
1230 | 0 | struct dispatch_key key = { |
1231 | 0 | .local = localaddr, |
1232 | 0 | .peer = destaddr, |
1233 | 0 | .transport = transport, |
1234 | 0 | }; |
1235 | |
|
1236 | 0 | rcu_read_lock(); |
1237 | 0 | struct cds_lfht_iter iter; |
1238 | 0 | dns_dispatch_t *disp = NULL; |
1239 | 0 | cds_lfht_for_each_entry_duplicate(mgr->tcps[tid], dispatch_hash(&key), |
1240 | 0 | dispatch_match, &key, &iter, disp, |
1241 | 0 | ht_node) { |
1242 | 0 | INSIST(disp->tid == isc_tid()); |
1243 | 0 | INSIST(disp->socktype == isc_socktype_tcp); |
1244 | |
|
1245 | 0 | switch (disp->state) { |
1246 | 0 | case DNS_DISPATCHSTATE_NONE: |
1247 | | /* A dispatch in indeterminate state, skip it */ |
1248 | 0 | break; |
1249 | 0 | case DNS_DISPATCHSTATE_CONNECTED: |
1250 | 0 | if (ISC_LIST_EMPTY(disp->active)) { |
1251 | | /* Ignore dispatch with no responses */ |
1252 | 0 | break; |
1253 | 0 | } |
1254 | | /* We found a connected dispatch */ |
1255 | 0 | dns_dispatch_attach(disp, &disp_connected); |
1256 | 0 | break; |
1257 | 0 | case DNS_DISPATCHSTATE_CONNECTING: |
1258 | 0 | if (ISC_LIST_EMPTY(disp->pending)) { |
1259 | | /* Ignore dispatch with no responses */ |
1260 | 0 | break; |
1261 | 0 | } |
1262 | | /* We found "a" dispatch, store it for later */ |
1263 | 0 | if (disp_fallback == NULL) { |
1264 | 0 | dns_dispatch_attach(disp, &disp_fallback); |
1265 | 0 | } |
1266 | 0 | break; |
1267 | 0 | case DNS_DISPATCHSTATE_CANCELED: |
1268 | | /* A canceled dispatch, skip it. */ |
1269 | 0 | break; |
1270 | 0 | default: |
1271 | 0 | UNREACHABLE(); |
1272 | 0 | } |
1273 | | |
1274 | 0 | if (disp_connected != NULL) { |
1275 | 0 | break; |
1276 | 0 | } |
1277 | 0 | } |
1278 | 0 | rcu_read_unlock(); |
1279 | |
|
1280 | 0 | if (disp_connected != NULL) { |
1281 | | /* We found connected dispatch */ |
1282 | 0 | INSIST(disp_connected->handle != NULL); |
1283 | |
|
1284 | 0 | *dispp = disp_connected; |
1285 | 0 | disp_connected = NULL; |
1286 | |
|
1287 | 0 | result = ISC_R_SUCCESS; |
1288 | |
|
1289 | 0 | if (disp_fallback != NULL) { |
1290 | 0 | dns_dispatch_detach(&disp_fallback); |
1291 | 0 | } |
1292 | 0 | } else if (disp_fallback != NULL) { |
1293 | 0 | *dispp = disp_fallback; |
1294 | |
|
1295 | 0 | result = ISC_R_SUCCESS; |
1296 | 0 | } |
1297 | |
|
1298 | 0 | return result; |
1299 | 0 | } |
1300 | | |
1301 | | isc_result_t |
1302 | | dns_dispatch_createudp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr, |
1303 | 0 | dns_dispatch_t **dispp) { |
1304 | 0 | isc_result_t result; |
1305 | 0 | dns_dispatch_t *disp = NULL; |
1306 | |
|
1307 | 0 | REQUIRE(VALID_DISPATCHMGR(mgr)); |
1308 | 0 | REQUIRE(localaddr != NULL); |
1309 | 0 | REQUIRE(dispp != NULL && *dispp == NULL); |
1310 | |
|
1311 | 0 | result = dispatch_createudp(mgr, localaddr, isc_tid(), &disp); |
1312 | 0 | if (result == ISC_R_SUCCESS) { |
1313 | 0 | *dispp = disp; |
1314 | 0 | } |
1315 | |
|
1316 | 0 | return result; |
1317 | 0 | } |
1318 | | |
1319 | | static isc_result_t |
1320 | | dispatch_createudp(dns_dispatchmgr_t *mgr, const isc_sockaddr_t *localaddr, |
1321 | 0 | isc_tid_t tid, dns_dispatch_t **dispp) { |
1322 | 0 | isc_result_t result = ISC_R_SUCCESS; |
1323 | 0 | dns_dispatch_t *disp = NULL; |
1324 | 0 | isc_sockaddr_t sa_any; |
1325 | | |
1326 | | /* |
1327 | | * Check whether this address/port is available locally. |
1328 | | */ |
1329 | 0 | isc_sockaddr_anyofpf(&sa_any, isc_sockaddr_pf(localaddr)); |
1330 | 0 | if (!isc_sockaddr_eqaddr(&sa_any, localaddr)) { |
1331 | 0 | result = isc_nm_checkaddr(localaddr, isc_socktype_udp); |
1332 | 0 | if (result != ISC_R_SUCCESS) { |
1333 | 0 | return result; |
1334 | 0 | } |
1335 | 0 | } |
1336 | | |
1337 | 0 | dispatch_allocate(mgr, isc_socktype_udp, tid, &disp); |
1338 | |
|
1339 | 0 | if (isc_log_wouldlog(90)) { |
1340 | 0 | char addrbuf[ISC_SOCKADDR_FORMATSIZE]; |
1341 | |
|
1342 | 0 | isc_sockaddr_format(localaddr, addrbuf, |
1343 | 0 | ISC_SOCKADDR_FORMATSIZE); |
1344 | 0 | mgr_log(mgr, ISC_LOG_DEBUG(90), |
1345 | 0 | "dispatch_createudp: created UDP dispatch %p for %s", |
1346 | 0 | disp, addrbuf); |
1347 | 0 | } |
1348 | |
|
1349 | 0 | disp->local = *localaddr; |
1350 | | |
1351 | | /* |
1352 | | * Don't append it to the dispatcher list, we don't care about UDP, only |
1353 | | * TCP should be searched |
1354 | | * |
1355 | | * ISC_LIST_APPEND(mgr->list, disp, link); |
1356 | | */ |
1357 | |
|
1358 | 0 | *dispp = disp; |
1359 | |
|
1360 | 0 | return result; |
1361 | 0 | } |
1362 | | |
1363 | | static void |
1364 | 0 | dispatch_destroy_rcu(struct rcu_head *rcu_head) { |
1365 | 0 | dns_dispatch_t *disp = caa_container_of(rcu_head, dns_dispatch_t, |
1366 | 0 | rcu_head); |
1367 | |
|
1368 | 0 | isc_mem_putanddetach(&disp->mctx, disp, sizeof(*disp)); |
1369 | 0 | } |
1370 | | |
1371 | | static void |
1372 | 0 | dispatch_destroy(dns_dispatch_t *disp) { |
1373 | 0 | dns_dispatchmgr_t *mgr = disp->mgr; |
1374 | 0 | isc_tid_t tid = isc_tid(); |
1375 | |
|
1376 | 0 | disp->magic = 0; |
1377 | |
|
1378 | 0 | if (disp->socktype == isc_socktype_tcp && |
1379 | 0 | (disp->options & DNS_DISPATCHOPT_UNSHARED) == 0) |
1380 | 0 | { |
1381 | 0 | (void)cds_lfht_del(mgr->tcps[tid], &disp->ht_node); |
1382 | 0 | } |
1383 | |
|
1384 | 0 | INSIST(disp->requests == 0); |
1385 | 0 | INSIST(ISC_LIST_EMPTY(disp->pending)); |
1386 | 0 | INSIST(ISC_LIST_EMPTY(disp->active)); |
1387 | |
|
1388 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), "destroying dispatch %p", disp); |
1389 | |
|
1390 | 0 | if (disp->handle) { |
1391 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
1392 | 0 | "detaching TCP handle %p from %p", disp->handle, |
1393 | 0 | &disp->handle); |
1394 | 0 | isc_nmhandle_detach(&disp->handle); |
1395 | 0 | } |
1396 | 0 | if (disp->transport != NULL) { |
1397 | 0 | dns_transport_detach(&disp->transport); |
1398 | 0 | } |
1399 | 0 | dns_dispatchmgr_detach(&disp->mgr); |
1400 | |
|
1401 | 0 | call_rcu(&disp->rcu_head, dispatch_destroy_rcu); |
1402 | 0 | } |
1403 | | |
1404 | | #if DNS_DISPATCH_TRACE |
1405 | | ISC_REFCOUNT_TRACE_IMPL(dns_dispatch, dispatch_destroy); |
1406 | | #else |
1407 | | ISC_REFCOUNT_IMPL(dns_dispatch, dispatch_destroy); |
1408 | | #endif |
1409 | | |
1410 | | isc_result_t |
1411 | | dns_dispatch_add(dns_dispatch_t *disp, isc_loop_t *loop, |
1412 | | dns_dispatchopt_t options, unsigned int connect_timeout, |
1413 | | unsigned int timeout, const isc_sockaddr_t *dest, |
1414 | | dns_transport_t *transport, isc_tlsctx_cache_t *tlsctx_cache, |
1415 | | dispatch_cb_t connected, dispatch_cb_t sent, |
1416 | | dispatch_cb_t response, void *arg, dns_messageid_t *idp, |
1417 | 0 | dns_dispentry_t **respp) { |
1418 | 0 | REQUIRE(VALID_DISPATCH(disp)); |
1419 | 0 | REQUIRE(dest != NULL); |
1420 | 0 | REQUIRE(respp != NULL && *respp == NULL); |
1421 | 0 | REQUIRE(idp != NULL); |
1422 | 0 | REQUIRE(disp->socktype == isc_socktype_tcp || |
1423 | 0 | disp->socktype == isc_socktype_udp); |
1424 | 0 | REQUIRE(connected != NULL); |
1425 | 0 | REQUIRE(response != NULL); |
1426 | 0 | REQUIRE(sent != NULL); |
1427 | 0 | REQUIRE(loop != NULL); |
1428 | 0 | REQUIRE(disp->tid == isc_tid()); |
1429 | 0 | REQUIRE(disp->transport == transport); |
1430 | |
|
1431 | 0 | if (disp->state == DNS_DISPATCHSTATE_CANCELED) { |
1432 | 0 | return ISC_R_CANCELED; |
1433 | 0 | } |
1434 | | |
1435 | 0 | in_port_t localport = isc_sockaddr_getport(&disp->local); |
1436 | 0 | dns_dispentry_t *resp = isc_mem_get(disp->mctx, sizeof(*resp)); |
1437 | 0 | *resp = (dns_dispentry_t){ |
1438 | 0 | .connect_timeout = connect_timeout, |
1439 | 0 | .timeout = timeout, |
1440 | 0 | .port = localport, |
1441 | 0 | .peer = *dest, |
1442 | 0 | .loop = loop, |
1443 | 0 | .connected = connected, |
1444 | 0 | .sent = sent, |
1445 | 0 | .response = response, |
1446 | 0 | .arg = arg, |
1447 | 0 | .alink = ISC_LINK_INITIALIZER, |
1448 | 0 | .plink = ISC_LINK_INITIALIZER, |
1449 | 0 | .rlink = ISC_LINK_INITIALIZER, |
1450 | 0 | .magic = RESPONSE_MAGIC, |
1451 | 0 | }; |
1452 | |
|
1453 | | #if DNS_DISPATCH_TRACE |
1454 | | fprintf(stderr, "dns_dispentry__init:%s:%s:%d:%p->references = 1\n", |
1455 | | __func__, __FILE__, __LINE__, resp); |
1456 | | #endif |
1457 | 0 | isc_refcount_init(&resp->references, 1); /* DISPENTRY000 */ |
1458 | |
|
1459 | 0 | if (disp->socktype == isc_socktype_udp) { |
1460 | 0 | isc_result_t result = setup_socket(disp, resp, dest, |
1461 | 0 | &localport); |
1462 | 0 | if (result != ISC_R_SUCCESS) { |
1463 | 0 | isc_mem_put(disp->mctx, resp, sizeof(*resp)); |
1464 | 0 | inc_stats(disp->mgr, dns_resstatscounter_dispsockfail); |
1465 | 0 | return result; |
1466 | 0 | } |
1467 | 0 | } |
1468 | | |
1469 | 0 | isc_result_t result = ISC_R_NOMORE; |
1470 | 0 | size_t i = 0; |
1471 | 0 | rcu_read_lock(); |
1472 | 0 | do { |
1473 | | /* |
1474 | | * Try somewhat hard to find a unique ID. Start with |
1475 | | * a random number unless DNS_DISPATCHOPT_FIXEDID is set, |
1476 | | * in which case we start with the ID passed in via *idp. |
1477 | | */ |
1478 | 0 | resp->id = ((options & DNS_DISPATCHOPT_FIXEDID) != 0) |
1479 | 0 | ? *idp |
1480 | 0 | : (dns_messageid_t)isc_random16(); |
1481 | |
|
1482 | 0 | struct cds_lfht_node *node = |
1483 | 0 | cds_lfht_add_unique(disp->mgr->qids, qid_hash(resp), |
1484 | 0 | qid_match, resp, &resp->ht_node); |
1485 | |
|
1486 | 0 | if (node != &resp->ht_node) { |
1487 | 0 | if ((options & DNS_DISPATCHOPT_FIXEDID) != 0) { |
1488 | | /* |
1489 | | * When using fixed ID, we either must |
1490 | | * use it or fail |
1491 | | */ |
1492 | 0 | goto fail; |
1493 | 0 | } |
1494 | 0 | } else { |
1495 | 0 | result = ISC_R_SUCCESS; |
1496 | 0 | break; |
1497 | 0 | } |
1498 | 0 | } while (i++ < QID_MAX_TRIES); |
1499 | 0 | fail: |
1500 | 0 | if (result != ISC_R_SUCCESS) { |
1501 | 0 | isc_mem_put(disp->mctx, resp, sizeof(*resp)); |
1502 | 0 | rcu_read_unlock(); |
1503 | 0 | return result; |
1504 | 0 | } |
1505 | | |
1506 | 0 | isc_mem_attach(disp->mctx, &resp->mctx); |
1507 | |
|
1508 | 0 | if (transport != NULL) { |
1509 | 0 | dns_transport_attach(transport, &resp->transport); |
1510 | 0 | } |
1511 | |
|
1512 | 0 | if (tlsctx_cache != NULL) { |
1513 | 0 | isc_tlsctx_cache_attach(tlsctx_cache, &resp->tlsctx_cache); |
1514 | 0 | } |
1515 | |
|
1516 | 0 | dns_dispatch_attach(disp, &resp->disp); /* DISPATCH001 */ |
1517 | |
|
1518 | 0 | disp->requests++; |
1519 | |
|
1520 | 0 | inc_stats(disp->mgr, (disp->socktype == isc_socktype_udp) |
1521 | 0 | ? dns_resstatscounter_disprequdp |
1522 | 0 | : dns_resstatscounter_dispreqtcp); |
1523 | |
|
1524 | 0 | rcu_read_unlock(); |
1525 | |
|
1526 | 0 | *idp = resp->id; |
1527 | 0 | *respp = resp; |
1528 | |
|
1529 | 0 | return ISC_R_SUCCESS; |
1530 | 0 | } |
1531 | | |
1532 | | isc_result_t |
1533 | 0 | dns_dispatch_getnext(dns_dispentry_t *resp) { |
1534 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
1535 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
1536 | |
|
1537 | 0 | dns_dispatch_t *disp = resp->disp; |
1538 | 0 | isc_result_t result = ISC_R_SUCCESS; |
1539 | 0 | int64_t timeout = 0; |
1540 | |
|
1541 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "getnext for QID %d", resp->id); |
1542 | |
|
1543 | 0 | if (resp->timeout > 0) { |
1544 | 0 | isc_time_t now = isc_loop_now(resp->loop); |
1545 | 0 | timeout = resp->timeout - dispentry_runtime(resp, &now); |
1546 | 0 | if (timeout <= 0) { |
1547 | 0 | return ISC_R_TIMEDOUT; |
1548 | 0 | } |
1549 | 0 | } |
1550 | | |
1551 | 0 | INSIST(disp->tid == isc_tid()); |
1552 | 0 | switch (disp->socktype) { |
1553 | 0 | case isc_socktype_udp: |
1554 | 0 | udp_dispatch_getnext(resp, timeout); |
1555 | 0 | break; |
1556 | 0 | case isc_socktype_tcp: |
1557 | 0 | tcp_dispatch_getnext(disp, resp, timeout); |
1558 | 0 | break; |
1559 | 0 | default: |
1560 | 0 | UNREACHABLE(); |
1561 | 0 | } |
1562 | | |
1563 | 0 | return result; |
1564 | 0 | } |
1565 | | |
1566 | | /* |
1567 | | * NOTE: Must be RCU read locked! |
1568 | | */ |
1569 | | static void |
1570 | 0 | udp_dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) { |
1571 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
1572 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
1573 | 0 | REQUIRE(VALID_DISPATCHMGR(resp->disp->mgr)); |
1574 | |
|
1575 | 0 | dns_dispatch_t *disp = resp->disp; |
1576 | 0 | bool respond = false; |
1577 | |
|
1578 | 0 | REQUIRE(disp->tid == isc_tid()); |
1579 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
1580 | 0 | "canceling response: %s, %s/%s (%s/%s), " |
1581 | 0 | "requests %" PRIuFAST32, |
1582 | 0 | isc_result_totext(result), state2str(resp->state), |
1583 | 0 | resp->reading ? "reading" : "not reading", |
1584 | 0 | state2str(disp->state), |
1585 | 0 | disp->reading ? "reading" : "not reading", |
1586 | 0 | disp->requests); |
1587 | |
|
1588 | 0 | if (ISC_LINK_LINKED(resp, alink)) { |
1589 | 0 | ISC_LIST_UNLINK(disp->active, resp, alink); |
1590 | 0 | } |
1591 | |
|
1592 | 0 | switch (resp->state) { |
1593 | 0 | case DNS_DISPATCHSTATE_NONE: |
1594 | 0 | break; |
1595 | | |
1596 | 0 | case DNS_DISPATCHSTATE_CONNECTING: |
1597 | 0 | break; |
1598 | | |
1599 | 0 | case DNS_DISPATCHSTATE_CONNECTED: |
1600 | 0 | if (resp->reading) { |
1601 | 0 | respond = true; |
1602 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
1603 | 0 | "canceling read on %p", resp->handle); |
1604 | 0 | isc_nm_cancelread(resp->handle); |
1605 | 0 | } |
1606 | 0 | break; |
1607 | | |
1608 | 0 | case DNS_DISPATCHSTATE_CANCELED: |
1609 | 0 | goto unlock; |
1610 | | |
1611 | 0 | default: |
1612 | 0 | UNREACHABLE(); |
1613 | 0 | } |
1614 | | |
1615 | 0 | dec_stats(disp->mgr, dns_resstatscounter_disprequdp); |
1616 | |
|
1617 | 0 | (void)cds_lfht_del(disp->mgr->qids, &resp->ht_node); |
1618 | |
|
1619 | 0 | resp->state = DNS_DISPATCHSTATE_CANCELED; |
1620 | |
|
1621 | 0 | unlock: |
1622 | 0 | if (respond) { |
1623 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "read callback: %s", |
1624 | 0 | isc_result_totext(result)); |
1625 | 0 | resp->response(result, NULL, resp->arg); |
1626 | 0 | } |
1627 | 0 | } |
1628 | | |
1629 | | /* |
1630 | | * NOTE: Must be RCU read locked! |
1631 | | */ |
1632 | | static void |
1633 | 0 | tcp_dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) { |
1634 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
1635 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
1636 | 0 | REQUIRE(VALID_DISPATCHMGR(resp->disp->mgr)); |
1637 | |
|
1638 | 0 | dns_dispatch_t *disp = resp->disp; |
1639 | 0 | dns_displist_t resps = ISC_LIST_INITIALIZER; |
1640 | |
|
1641 | 0 | REQUIRE(disp->tid == isc_tid()); |
1642 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
1643 | 0 | "canceling response: %s, %s/%s (%s/%s), " |
1644 | 0 | "requests %" PRIuFAST32, |
1645 | 0 | isc_result_totext(result), state2str(resp->state), |
1646 | 0 | resp->reading ? "reading" : "not reading", |
1647 | 0 | state2str(disp->state), |
1648 | 0 | disp->reading ? "reading" : "not reading", |
1649 | 0 | disp->requests); |
1650 | |
|
1651 | 0 | switch (resp->state) { |
1652 | 0 | case DNS_DISPATCHSTATE_NONE: |
1653 | 0 | break; |
1654 | | |
1655 | 0 | case DNS_DISPATCHSTATE_CONNECTING: |
1656 | 0 | break; |
1657 | | |
1658 | 0 | case DNS_DISPATCHSTATE_CONNECTED: |
1659 | 0 | if (resp->reading) { |
1660 | 0 | tcp_recv_add(&resps, resp, ISC_R_CANCELED); |
1661 | 0 | } |
1662 | |
|
1663 | 0 | INSIST(!ISC_LINK_LINKED(resp, alink)); |
1664 | |
|
1665 | 0 | if (ISC_LIST_EMPTY(disp->active)) { |
1666 | 0 | INSIST(disp->handle != NULL); |
1667 | |
|
1668 | | #if DISPATCH_TCP_KEEPALIVE |
1669 | | /* |
1670 | | * This is an experimental code that keeps the TCP |
1671 | | * connection open for 1 second before it is finally |
1672 | | * closed. By keeping the TCP connection open, it can |
1673 | | * be reused by dns_request that uses |
1674 | | * dns_dispatch_gettcp() to join existing TCP |
1675 | | * connections. |
1676 | | * |
1677 | | * It is disabled for now, because it changes the |
1678 | | * behaviour, but I am keeping the code here for future |
1679 | | * reference when we improve the dns_dispatch to reuse |
1680 | | * the TCP connections also in the resolver. |
1681 | | * |
1682 | | * The TCP connection reuse should be seamless and not |
1683 | | * require any extra handling on the client side though. |
1684 | | */ |
1685 | | isc_nmhandle_cleartimeout(disp->handle); |
1686 | | isc_nmhandle_settimeout(disp->handle, 1000); |
1687 | | |
1688 | | if (!disp->reading) { |
1689 | | dispentry_log(resp, ISC_LOG_DEBUG(90), |
1690 | | "final 1 second timeout on %p", |
1691 | | disp->handle); |
1692 | | tcp_startrecv(disp, NULL); |
1693 | | } |
1694 | | #else |
1695 | 0 | if (disp->reading) { |
1696 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
1697 | 0 | "canceling read on %p", |
1698 | 0 | disp->handle); |
1699 | 0 | isc_nm_cancelread(disp->handle); |
1700 | 0 | } |
1701 | 0 | #endif |
1702 | 0 | } |
1703 | 0 | break; |
1704 | | |
1705 | 0 | case DNS_DISPATCHSTATE_CANCELED: |
1706 | 0 | goto unlock; |
1707 | | |
1708 | 0 | default: |
1709 | 0 | UNREACHABLE(); |
1710 | 0 | } |
1711 | | |
1712 | 0 | dec_stats(disp->mgr, dns_resstatscounter_dispreqtcp); |
1713 | |
|
1714 | 0 | (void)cds_lfht_del(disp->mgr->qids, &resp->ht_node); |
1715 | |
|
1716 | 0 | resp->state = DNS_DISPATCHSTATE_CANCELED; |
1717 | |
|
1718 | 0 | unlock: |
1719 | | |
1720 | | /* |
1721 | | * NOTE: Calling the response callback directly from here should be done |
1722 | | * asynchronously, as the dns_dispatch_done() is usually called directly |
1723 | | * from the response callback, so there's a slight chance that the call |
1724 | | * stack will get higher here, but it's mitigated by the ".reading" |
1725 | | * flag, so we don't ever go into a loop. |
1726 | | */ |
1727 | |
|
1728 | 0 | tcp_recv_processall(&resps, NULL); |
1729 | 0 | } |
1730 | | |
1731 | | static void |
1732 | 0 | dispentry_cancel(dns_dispentry_t *resp, isc_result_t result) { |
1733 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
1734 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
1735 | |
|
1736 | 0 | dns_dispatch_t *disp = resp->disp; |
1737 | |
|
1738 | 0 | rcu_read_lock(); |
1739 | 0 | switch (disp->socktype) { |
1740 | 0 | case isc_socktype_udp: |
1741 | 0 | udp_dispentry_cancel(resp, result); |
1742 | 0 | break; |
1743 | 0 | case isc_socktype_tcp: |
1744 | 0 | tcp_dispentry_cancel(resp, result); |
1745 | 0 | break; |
1746 | 0 | default: |
1747 | 0 | UNREACHABLE(); |
1748 | 0 | } |
1749 | 0 | rcu_read_unlock(); |
1750 | 0 | } |
1751 | | |
1752 | | void |
1753 | 0 | dns_dispatch_done(dns_dispentry_t **respp) { |
1754 | 0 | REQUIRE(VALID_RESPONSE(*respp)); |
1755 | |
|
1756 | 0 | dns_dispentry_t *resp = *respp; |
1757 | 0 | *respp = NULL; |
1758 | |
|
1759 | 0 | dispentry_cancel(resp, ISC_R_CANCELED); |
1760 | 0 | dns_dispentry_detach(&resp); /* DISPENTRY000 */ |
1761 | 0 | } |
1762 | | |
1763 | | static void |
1764 | 0 | udp_startrecv(isc_nmhandle_t *handle, dns_dispentry_t *resp) { |
1765 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
1766 | |
|
1767 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "attaching handle %p to %p", |
1768 | 0 | handle, &resp->handle); |
1769 | 0 | isc_nmhandle_attach(handle, &resp->handle); |
1770 | 0 | dns_dispentry_ref(resp); /* DISPENTRY003 */ |
1771 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "reading"); |
1772 | 0 | isc_nm_read(resp->handle, udp_recv, resp); |
1773 | 0 | resp->reading = true; |
1774 | 0 | } |
1775 | | |
1776 | | static void |
1777 | 0 | tcp_startrecv(dns_dispatch_t *disp, dns_dispentry_t *resp) { |
1778 | 0 | REQUIRE(VALID_DISPATCH(disp)); |
1779 | 0 | REQUIRE(disp->socktype == isc_socktype_tcp); |
1780 | |
|
1781 | 0 | dns_dispatch_ref(disp); /* DISPATCH002 */ |
1782 | 0 | if (resp != NULL) { |
1783 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "reading from %p", |
1784 | 0 | disp->handle); |
1785 | 0 | INSIST(!isc_time_isepoch(&resp->start)); |
1786 | 0 | } else { |
1787 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
1788 | 0 | "TCP reading without response from %p", |
1789 | 0 | disp->handle); |
1790 | 0 | } |
1791 | 0 | isc_nm_read(disp->handle, tcp_recv, disp); |
1792 | 0 | disp->reading = true; |
1793 | 0 | } |
1794 | | |
1795 | | static void |
1796 | 0 | resp_connected(void *arg) { |
1797 | 0 | dns_dispentry_t *resp = arg; |
1798 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "connect callback: %s", |
1799 | 0 | isc_result_totext(resp->result)); |
1800 | |
|
1801 | 0 | resp->connected(resp->result, NULL, resp->arg); |
1802 | 0 | dns_dispentry_detach(&resp); /* DISPENTRY005 */ |
1803 | 0 | } |
1804 | | |
1805 | | static void |
1806 | 0 | tcp_connected(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) { |
1807 | 0 | dns_dispatch_t *disp = (dns_dispatch_t *)arg; |
1808 | 0 | dns_displist_t resps = ISC_LIST_INITIALIZER; |
1809 | |
|
1810 | 0 | if (isc_log_wouldlog(90)) { |
1811 | 0 | char localbuf[ISC_SOCKADDR_FORMATSIZE]; |
1812 | 0 | char peerbuf[ISC_SOCKADDR_FORMATSIZE]; |
1813 | 0 | if (handle != NULL) { |
1814 | 0 | isc_sockaddr_t local = isc_nmhandle_localaddr(handle); |
1815 | 0 | isc_sockaddr_t peer = isc_nmhandle_peeraddr(handle); |
1816 | |
|
1817 | 0 | isc_sockaddr_format(&local, localbuf, |
1818 | 0 | ISC_SOCKADDR_FORMATSIZE); |
1819 | 0 | isc_sockaddr_format(&peer, peerbuf, |
1820 | 0 | ISC_SOCKADDR_FORMATSIZE); |
1821 | 0 | } else { |
1822 | 0 | isc_sockaddr_format(&disp->local, localbuf, |
1823 | 0 | ISC_SOCKADDR_FORMATSIZE); |
1824 | 0 | isc_sockaddr_format(&disp->peer, peerbuf, |
1825 | 0 | ISC_SOCKADDR_FORMATSIZE); |
1826 | 0 | } |
1827 | |
|
1828 | 0 | dispatch_log(disp, ISC_LOG_DEBUG(90), |
1829 | 0 | "connected from %s to %s: %s", localbuf, peerbuf, |
1830 | 0 | isc_result_totext(eresult)); |
1831 | 0 | } |
1832 | |
|
1833 | 0 | REQUIRE(disp->tid == isc_tid()); |
1834 | 0 | INSIST(disp->state == DNS_DISPATCHSTATE_CONNECTING); |
1835 | | |
1836 | | /* |
1837 | | * If there are pending responses, call the connect |
1838 | | * callbacks for all of them. |
1839 | | */ |
1840 | 0 | ISC_LIST_FOREACH(disp->pending, resp, plink) { |
1841 | 0 | ISC_LIST_UNLINK(disp->pending, resp, plink); |
1842 | 0 | ISC_LIST_APPEND(resps, resp, rlink); |
1843 | 0 | resp->result = eresult; |
1844 | |
|
1845 | 0 | if (resp->state == DNS_DISPATCHSTATE_CANCELED) { |
1846 | 0 | resp->result = ISC_R_CANCELED; |
1847 | 0 | } else if (eresult == ISC_R_SUCCESS) { |
1848 | 0 | resp->state = DNS_DISPATCHSTATE_CONNECTED; |
1849 | 0 | ISC_LIST_APPEND(disp->active, resp, alink); |
1850 | 0 | resp->reading = true; |
1851 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "start reading"); |
1852 | 0 | } else { |
1853 | 0 | resp->state = DNS_DISPATCHSTATE_NONE; |
1854 | 0 | } |
1855 | 0 | } |
1856 | | |
1857 | | /* Take the oldest active response. */ |
1858 | 0 | dns_dispentry_t *oldest = ISC_LIST_HEAD(disp->active); |
1859 | 0 | if (oldest == NULL) { |
1860 | | /* All responses have been canceled */ |
1861 | 0 | disp->state = DNS_DISPATCHSTATE_CANCELED; |
1862 | 0 | } else if (eresult == ISC_R_SUCCESS) { |
1863 | 0 | disp->state = DNS_DISPATCHSTATE_CONNECTED; |
1864 | 0 | isc_nmhandle_attach(handle, &disp->handle); |
1865 | 0 | isc_nmhandle_cleartimeout(disp->handle); |
1866 | 0 | if (oldest->timeout != 0) { |
1867 | 0 | isc_nmhandle_settimeout(disp->handle, oldest->timeout); |
1868 | 0 | } |
1869 | 0 | tcp_startrecv(disp, oldest); |
1870 | 0 | } else { |
1871 | 0 | disp->state = DNS_DISPATCHSTATE_NONE; |
1872 | 0 | } |
1873 | |
|
1874 | 0 | ISC_LIST_FOREACH(resps, resp, rlink) { |
1875 | 0 | ISC_LIST_UNLINK(resps, resp, rlink); |
1876 | |
|
1877 | 0 | resp_connected(resp); |
1878 | 0 | } |
1879 | |
|
1880 | 0 | dns_dispatch_detach(&disp); /* DISPATCH003 */ |
1881 | 0 | } |
1882 | | |
1883 | | static void |
1884 | 0 | udp_connected(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) { |
1885 | 0 | dns_dispentry_t *resp = (dns_dispentry_t *)arg; |
1886 | 0 | dns_dispatch_t *disp = resp->disp; |
1887 | |
|
1888 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "connected: %s", |
1889 | 0 | isc_result_totext(eresult)); |
1890 | |
|
1891 | 0 | REQUIRE(disp->tid == isc_tid()); |
1892 | 0 | switch (resp->state) { |
1893 | 0 | case DNS_DISPATCHSTATE_CANCELED: |
1894 | 0 | eresult = ISC_R_CANCELED; |
1895 | 0 | ISC_LIST_UNLINK(disp->pending, resp, plink); |
1896 | 0 | goto unlock; |
1897 | 0 | case DNS_DISPATCHSTATE_CONNECTING: |
1898 | 0 | ISC_LIST_UNLINK(disp->pending, resp, plink); |
1899 | 0 | break; |
1900 | 0 | default: |
1901 | 0 | UNREACHABLE(); |
1902 | 0 | } |
1903 | | |
1904 | 0 | switch (eresult) { |
1905 | 0 | case ISC_R_CANCELED: |
1906 | 0 | break; |
1907 | 0 | case ISC_R_SUCCESS: |
1908 | 0 | resp->state = DNS_DISPATCHSTATE_CONNECTED; |
1909 | 0 | udp_startrecv(handle, resp); |
1910 | 0 | break; |
1911 | 0 | case ISC_R_NOPERM: |
1912 | 0 | case ISC_R_ADDRINUSE: { |
1913 | 0 | in_port_t localport = isc_sockaddr_getport(&disp->local); |
1914 | 0 | isc_result_t result; |
1915 | | |
1916 | | /* probably a port collision; try a different one */ |
1917 | 0 | result = setup_socket(disp, resp, &resp->peer, &localport); |
1918 | 0 | if (result == ISC_R_SUCCESS) { |
1919 | 0 | udp_dispatch_connect(disp, resp); |
1920 | 0 | goto detach; |
1921 | 0 | } |
1922 | 0 | resp->state = DNS_DISPATCHSTATE_NONE; |
1923 | 0 | break; |
1924 | 0 | } |
1925 | 0 | default: |
1926 | 0 | resp->state = DNS_DISPATCHSTATE_NONE; |
1927 | 0 | break; |
1928 | 0 | } |
1929 | 0 | unlock: |
1930 | |
|
1931 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "connect callback: %s", |
1932 | 0 | isc_result_totext(eresult)); |
1933 | 0 | resp->connected(eresult, NULL, resp->arg); |
1934 | |
|
1935 | 0 | detach: |
1936 | 0 | dns_dispentry_detach(&resp); /* DISPENTRY004 */ |
1937 | 0 | } |
1938 | | |
1939 | | static void |
1940 | 0 | udp_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp) { |
1941 | 0 | REQUIRE(disp->tid == isc_tid()); |
1942 | 0 | resp->state = DNS_DISPATCHSTATE_CONNECTING; |
1943 | 0 | resp->start = isc_loop_now(resp->loop); |
1944 | 0 | dns_dispentry_ref(resp); /* DISPENTRY004 */ |
1945 | 0 | ISC_LIST_APPEND(disp->pending, resp, plink); |
1946 | |
|
1947 | 0 | isc_nm_udpconnect(&resp->local, &resp->peer, udp_connected, resp, |
1948 | 0 | resp->timeout); |
1949 | 0 | } |
1950 | | |
1951 | | static inline const char * |
1952 | 0 | get_tls_sni_hostname(dns_dispentry_t *resp) { |
1953 | 0 | char *hostname = NULL; |
1954 | |
|
1955 | 0 | if (resp->transport != NULL) { |
1956 | 0 | hostname = dns_transport_get_remote_hostname(resp->transport); |
1957 | 0 | } |
1958 | |
|
1959 | 0 | if (hostname == NULL) { |
1960 | 0 | return NULL; |
1961 | 0 | } |
1962 | | |
1963 | 0 | if (isc_tls_valid_sni_hostname(hostname)) { |
1964 | 0 | return hostname; |
1965 | 0 | } |
1966 | | |
1967 | 0 | return NULL; |
1968 | 0 | } |
1969 | | |
1970 | | static isc_result_t |
1971 | 0 | tcp_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp) { |
1972 | 0 | dns_transport_type_t transport_type = DNS_TRANSPORT_TCP; |
1973 | 0 | isc_tlsctx_t *tlsctx = NULL; |
1974 | 0 | isc_tlsctx_client_session_cache_t *sess_cache = NULL; |
1975 | |
|
1976 | 0 | if (resp->transport != NULL) { |
1977 | 0 | transport_type = dns_transport_get_type(resp->transport); |
1978 | 0 | } |
1979 | |
|
1980 | 0 | if (transport_type == DNS_TRANSPORT_TLS) { |
1981 | 0 | isc_result_t result; |
1982 | |
|
1983 | 0 | result = dns_transport_get_tlsctx( |
1984 | 0 | resp->transport, &resp->peer, resp->tlsctx_cache, |
1985 | 0 | resp->mctx, &tlsctx, &sess_cache); |
1986 | |
|
1987 | 0 | if (result != ISC_R_SUCCESS) { |
1988 | 0 | return result; |
1989 | 0 | } |
1990 | 0 | INSIST(tlsctx != NULL); |
1991 | 0 | } |
1992 | | |
1993 | | /* Check whether the dispatch is already connecting or connected. */ |
1994 | 0 | REQUIRE(disp->tid == isc_tid()); |
1995 | 0 | switch (disp->state) { |
1996 | 0 | case DNS_DISPATCHSTATE_NONE: |
1997 | | /* First connection, continue with connecting */ |
1998 | 0 | disp->state = DNS_DISPATCHSTATE_CONNECTING; |
1999 | 0 | resp->state = DNS_DISPATCHSTATE_CONNECTING; |
2000 | 0 | resp->start = isc_loop_now(resp->loop); |
2001 | 0 | dns_dispentry_ref(resp); /* DISPENTRY005 */ |
2002 | 0 | ISC_LIST_APPEND(disp->pending, resp, plink); |
2003 | |
|
2004 | 0 | char localbuf[ISC_SOCKADDR_FORMATSIZE]; |
2005 | 0 | char peerbuf[ISC_SOCKADDR_FORMATSIZE]; |
2006 | |
|
2007 | 0 | isc_sockaddr_format(&disp->local, localbuf, |
2008 | 0 | ISC_SOCKADDR_FORMATSIZE); |
2009 | 0 | isc_sockaddr_format(&disp->peer, peerbuf, |
2010 | 0 | ISC_SOCKADDR_FORMATSIZE); |
2011 | |
|
2012 | 0 | dns_dispatch_ref(disp); /* DISPATCH003 */ |
2013 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
2014 | 0 | "connecting from %s to %s, timeout %u", localbuf, |
2015 | 0 | peerbuf, resp->connect_timeout); |
2016 | |
|
2017 | 0 | const char *hostname = get_tls_sni_hostname(resp); |
2018 | |
|
2019 | 0 | isc_nm_streamdnsconnect(&disp->local, &disp->peer, |
2020 | 0 | tcp_connected, disp, |
2021 | 0 | resp->connect_timeout, tlsctx, hostname, |
2022 | 0 | sess_cache, ISC_NM_PROXY_NONE, NULL); |
2023 | 0 | break; |
2024 | | |
2025 | 0 | case DNS_DISPATCHSTATE_CONNECTING: |
2026 | | /* Connection pending; add resp to the list */ |
2027 | 0 | resp->state = DNS_DISPATCHSTATE_CONNECTING; |
2028 | 0 | resp->start = isc_loop_now(resp->loop); |
2029 | 0 | dns_dispentry_ref(resp); /* DISPENTRY005 */ |
2030 | 0 | ISC_LIST_APPEND(disp->pending, resp, plink); |
2031 | 0 | break; |
2032 | | |
2033 | 0 | case DNS_DISPATCHSTATE_CONNECTED: |
2034 | 0 | resp->state = DNS_DISPATCHSTATE_CONNECTED; |
2035 | 0 | resp->start = isc_loop_now(resp->loop); |
2036 | | |
2037 | | /* Add the resp to the reading list */ |
2038 | 0 | ISC_LIST_APPEND(disp->active, resp, alink); |
2039 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), |
2040 | 0 | "already connected; attaching"); |
2041 | 0 | resp->reading = true; |
2042 | |
|
2043 | 0 | if (!disp->reading) { |
2044 | | /* Restart the reading */ |
2045 | 0 | isc_nmhandle_cleartimeout(disp->handle); |
2046 | 0 | if (resp->timeout != 0) { |
2047 | 0 | isc_nmhandle_settimeout(disp->handle, |
2048 | 0 | resp->timeout); |
2049 | 0 | } |
2050 | 0 | tcp_startrecv(disp, resp); |
2051 | 0 | } |
2052 | | |
2053 | | /* Already connected; call the connected cb asynchronously */ |
2054 | 0 | dns_dispentry_ref(resp); /* DISPENTRY005 */ |
2055 | 0 | isc_async_run(resp->loop, resp_connected, resp); |
2056 | 0 | break; |
2057 | | |
2058 | 0 | default: |
2059 | 0 | UNREACHABLE(); |
2060 | 0 | } |
2061 | | |
2062 | 0 | return ISC_R_SUCCESS; |
2063 | 0 | } |
2064 | | |
2065 | | isc_result_t |
2066 | 0 | dns_dispatch_connect(dns_dispentry_t *resp) { |
2067 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
2068 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
2069 | |
|
2070 | 0 | dns_dispatch_t *disp = resp->disp; |
2071 | |
|
2072 | 0 | switch (disp->socktype) { |
2073 | 0 | case isc_socktype_tcp: |
2074 | 0 | return tcp_dispatch_connect(disp, resp); |
2075 | | |
2076 | 0 | case isc_socktype_udp: |
2077 | 0 | udp_dispatch_connect(disp, resp); |
2078 | 0 | return ISC_R_SUCCESS; |
2079 | | |
2080 | 0 | default: |
2081 | 0 | UNREACHABLE(); |
2082 | 0 | } |
2083 | 0 | } |
2084 | | |
2085 | | static void |
2086 | 0 | send_done(isc_nmhandle_t *handle, isc_result_t result, void *cbarg) { |
2087 | 0 | dns_dispentry_t *resp = (dns_dispentry_t *)cbarg; |
2088 | |
|
2089 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
2090 | |
|
2091 | 0 | dns_dispatch_t *disp = resp->disp; |
2092 | |
|
2093 | 0 | REQUIRE(VALID_DISPATCH(disp)); |
2094 | |
|
2095 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "sent: %s", |
2096 | 0 | isc_result_totext(result)); |
2097 | |
|
2098 | 0 | resp->sent(result, NULL, resp->arg); |
2099 | |
|
2100 | 0 | if (result != ISC_R_SUCCESS) { |
2101 | 0 | dispentry_cancel(resp, result); |
2102 | 0 | } |
2103 | |
|
2104 | 0 | dns_dispentry_detach(&resp); /* DISPENTRY007 */ |
2105 | 0 | isc_nmhandle_detach(&handle); |
2106 | 0 | } |
2107 | | |
2108 | | static void |
2109 | | tcp_dispatch_getnext(dns_dispatch_t *disp, dns_dispentry_t *resp, |
2110 | 0 | int64_t timeout) { |
2111 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "continue reading"); |
2112 | |
|
2113 | 0 | if (!resp->reading) { |
2114 | 0 | ISC_LIST_APPEND(disp->active, resp, alink); |
2115 | 0 | resp->reading = true; |
2116 | 0 | } |
2117 | |
|
2118 | 0 | if (disp->reading) { |
2119 | 0 | return; |
2120 | 0 | } |
2121 | | |
2122 | 0 | if (timeout != 0) { |
2123 | 0 | INSIST(timeout > 0 && timeout <= UINT32_MAX); |
2124 | 0 | isc_nmhandle_settimeout(disp->handle, timeout); |
2125 | 0 | } |
2126 | |
|
2127 | 0 | dns_dispatch_ref(disp); /* DISPATCH002 */ |
2128 | 0 | isc_nm_read(disp->handle, tcp_recv, disp); |
2129 | 0 | disp->reading = true; |
2130 | 0 | } |
2131 | | |
2132 | | static void |
2133 | 0 | udp_dispatch_getnext(dns_dispentry_t *resp, int64_t timeout) { |
2134 | 0 | if (resp->reading) { |
2135 | 0 | return; |
2136 | 0 | } |
2137 | | |
2138 | 0 | if (timeout != 0) { |
2139 | 0 | INSIST(timeout > 0 && timeout <= UINT32_MAX); |
2140 | 0 | isc_nmhandle_settimeout(resp->handle, timeout); |
2141 | 0 | } |
2142 | |
|
2143 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "continue reading"); |
2144 | |
|
2145 | 0 | dns_dispentry_ref(resp); /* DISPENTRY003 */ |
2146 | 0 | isc_nm_read(resp->handle, udp_recv, resp); |
2147 | 0 | resp->reading = true; |
2148 | 0 | } |
2149 | | |
2150 | | void |
2151 | 0 | dns_dispatch_resume(dns_dispentry_t *resp, unsigned int timeout) { |
2152 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
2153 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
2154 | |
|
2155 | 0 | dns_dispatch_t *disp = resp->disp; |
2156 | |
|
2157 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "resume"); |
2158 | |
|
2159 | 0 | REQUIRE(disp->tid == isc_tid()); |
2160 | 0 | switch (disp->socktype) { |
2161 | 0 | case isc_socktype_udp: { |
2162 | 0 | udp_dispatch_getnext(resp, timeout); |
2163 | 0 | break; |
2164 | 0 | } |
2165 | 0 | case isc_socktype_tcp: |
2166 | 0 | INSIST(disp->timedout > 0); |
2167 | 0 | disp->timedout--; |
2168 | 0 | tcp_dispatch_getnext(disp, resp, timeout); |
2169 | 0 | break; |
2170 | 0 | default: |
2171 | 0 | UNREACHABLE(); |
2172 | 0 | } |
2173 | 0 | } |
2174 | | |
2175 | | void |
2176 | 0 | dns_dispatch_send(dns_dispentry_t *resp, isc_region_t *r) { |
2177 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
2178 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
2179 | |
|
2180 | 0 | dns_dispatch_t *disp = resp->disp; |
2181 | 0 | isc_nmhandle_t *sendhandle = NULL; |
2182 | |
|
2183 | 0 | dispentry_log(resp, ISC_LOG_DEBUG(90), "sending"); |
2184 | 0 | switch (disp->socktype) { |
2185 | 0 | case isc_socktype_udp: |
2186 | 0 | isc_nmhandle_attach(resp->handle, &sendhandle); |
2187 | 0 | break; |
2188 | 0 | case isc_socktype_tcp: |
2189 | 0 | isc_nmhandle_attach(disp->handle, &sendhandle); |
2190 | 0 | break; |
2191 | 0 | default: |
2192 | 0 | UNREACHABLE(); |
2193 | 0 | } |
2194 | 0 | dns_dispentry_ref(resp); /* DISPENTRY007 */ |
2195 | 0 | isc_nm_send(sendhandle, r, send_done, resp); |
2196 | 0 | } |
2197 | | |
2198 | | isc_result_t |
2199 | 0 | dns_dispatch_getlocaladdress(dns_dispatch_t *disp, isc_sockaddr_t *addrp) { |
2200 | 0 | REQUIRE(VALID_DISPATCH(disp)); |
2201 | 0 | REQUIRE(addrp != NULL); |
2202 | |
|
2203 | 0 | if (disp->socktype == isc_socktype_udp) { |
2204 | 0 | *addrp = disp->local; |
2205 | 0 | return ISC_R_SUCCESS; |
2206 | 0 | } |
2207 | 0 | return ISC_R_NOTIMPLEMENTED; |
2208 | 0 | } |
2209 | | |
2210 | | isc_result_t |
2211 | 0 | dns_dispentry_getlocaladdress(dns_dispentry_t *resp, isc_sockaddr_t *addrp) { |
2212 | 0 | REQUIRE(VALID_RESPONSE(resp)); |
2213 | 0 | REQUIRE(VALID_DISPATCH(resp->disp)); |
2214 | 0 | REQUIRE(addrp != NULL); |
2215 | |
|
2216 | 0 | dns_dispatch_t *disp = resp->disp; |
2217 | |
|
2218 | 0 | switch (disp->socktype) { |
2219 | 0 | case isc_socktype_tcp: |
2220 | 0 | *addrp = isc_nmhandle_localaddr(disp->handle); |
2221 | 0 | return ISC_R_SUCCESS; |
2222 | 0 | case isc_socktype_udp: |
2223 | 0 | *addrp = isc_nmhandle_localaddr(resp->handle); |
2224 | 0 | return ISC_R_SUCCESS; |
2225 | 0 | default: |
2226 | 0 | UNREACHABLE(); |
2227 | 0 | } |
2228 | 0 | } |
2229 | | |
2230 | | dns_dispatch_t * |
2231 | 0 | dns_dispatchset_get(dns_dispatchset_t *dset) { |
2232 | 0 | isc_tid_t tid = isc_tid(); |
2233 | | |
2234 | | /* check that dispatch set is configured */ |
2235 | 0 | if (dset == NULL || dset->ndisp == 0) { |
2236 | 0 | return NULL; |
2237 | 0 | } |
2238 | | |
2239 | 0 | INSIST((uint32_t)tid < dset->ndisp); |
2240 | |
|
2241 | 0 | return dset->dispatches[tid]; |
2242 | 0 | } |
2243 | | |
2244 | | isc_result_t |
2245 | | dns_dispatchset_create(isc_mem_t *mctx, dns_dispatch_t *source, |
2246 | 0 | dns_dispatchset_t **dsetp, uint32_t ndisp) { |
2247 | 0 | isc_result_t result; |
2248 | 0 | dns_dispatchset_t *dset = NULL; |
2249 | 0 | dns_dispatchmgr_t *mgr = NULL; |
2250 | 0 | size_t i; |
2251 | |
|
2252 | 0 | REQUIRE(VALID_DISPATCH(source)); |
2253 | 0 | REQUIRE(source->socktype == isc_socktype_udp); |
2254 | 0 | REQUIRE(dsetp != NULL && *dsetp == NULL); |
2255 | |
|
2256 | 0 | mgr = source->mgr; |
2257 | |
|
2258 | 0 | dset = isc_mem_get(mctx, sizeof(dns_dispatchset_t)); |
2259 | 0 | *dset = (dns_dispatchset_t){ .ndisp = ndisp }; |
2260 | |
|
2261 | 0 | isc_mem_attach(mctx, &dset->mctx); |
2262 | |
|
2263 | 0 | dset->dispatches = isc_mem_cget(dset->mctx, ndisp, |
2264 | 0 | sizeof(dns_dispatch_t *)); |
2265 | |
|
2266 | 0 | dset->dispatches[0] = NULL; |
2267 | 0 | dns_dispatch_attach(source, &dset->dispatches[0]); /* DISPATCH004 */ |
2268 | |
|
2269 | 0 | for (i = 1; i < dset->ndisp; i++) { |
2270 | 0 | result = dispatch_createudp(mgr, &source->local, i, |
2271 | 0 | &dset->dispatches[i]); |
2272 | 0 | if (result != ISC_R_SUCCESS) { |
2273 | 0 | goto fail; |
2274 | 0 | } |
2275 | 0 | } |
2276 | | |
2277 | 0 | *dsetp = dset; |
2278 | |
|
2279 | 0 | return ISC_R_SUCCESS; |
2280 | | |
2281 | 0 | fail: |
2282 | 0 | for (size_t j = 0; j < i; j++) { |
2283 | 0 | dns_dispatch_detach(&(dset->dispatches[j])); /* DISPATCH004 */ |
2284 | 0 | } |
2285 | 0 | isc_mem_cput(dset->mctx, dset->dispatches, ndisp, |
2286 | 0 | sizeof(dns_dispatch_t *)); |
2287 | |
|
2288 | 0 | isc_mem_putanddetach(&dset->mctx, dset, sizeof(dns_dispatchset_t)); |
2289 | 0 | return result; |
2290 | 0 | } |
2291 | | |
2292 | | void |
2293 | 0 | dns_dispatchset_destroy(dns_dispatchset_t **dsetp) { |
2294 | 0 | REQUIRE(dsetp != NULL && *dsetp != NULL); |
2295 | |
|
2296 | 0 | dns_dispatchset_t *dset = *dsetp; |
2297 | 0 | *dsetp = NULL; |
2298 | |
|
2299 | 0 | for (size_t i = 0; i < dset->ndisp; i++) { |
2300 | 0 | dns_dispatch_detach(&(dset->dispatches[i])); /* DISPATCH004 */ |
2301 | 0 | } |
2302 | 0 | isc_mem_cput(dset->mctx, dset->dispatches, dset->ndisp, |
2303 | 0 | sizeof(dns_dispatch_t *)); |
2304 | 0 | isc_mem_putanddetach(&dset->mctx, dset, sizeof(dns_dispatchset_t)); |
2305 | 0 | } |
2306 | | |
2307 | | isc_result_t |
2308 | 0 | dns_dispatch_checkperm(dns_dispatch_t *disp) { |
2309 | 0 | REQUIRE(VALID_DISPATCH(disp)); |
2310 | |
|
2311 | 0 | if (disp->handle == NULL || disp->socktype == isc_socktype_udp) { |
2312 | 0 | return ISC_R_NOPERM; |
2313 | 0 | } |
2314 | | |
2315 | 0 | return isc_nm_xfr_checkperm(disp->handle); |
2316 | 0 | } |