2 * Copyright (C) 2004-2013 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
22 #include <isc/formatcheck.h>
23 #include <isc/mutex.h>
25 #include <isc/platform.h>
26 #include <isc/print.h>
27 #include <isc/queue.h>
28 #include <isc/stats.h>
29 #include <isc/stdio.h>
30 #include <isc/string.h>
32 #include <isc/timer.h>
36 #include <dns/dispatch.h>
37 #include <dns/events.h>
38 #include <dns/message.h>
40 #include <dns/rcode.h>
41 #include <dns/rdata.h>
42 #include <dns/rdataclass.h>
43 #include <dns/rdatalist.h>
44 #include <dns/rdataset.h>
45 #include <dns/resolver.h>
46 #include <dns/stats.h>
51 #include <named/interfacemgr.h>
52 #include <named/log.h>
53 #include <named/notify.h>
55 #include <named/server.h>
56 #include <named/update.h>
67 * All client state changes, other than that from idle to listening, occur
68 * as a result of events. This guarantees serialization and avoids the
71 * If a routine is ever created that allows someone other than the client's
72 * task to change the client, then the client will have to be locked.
75 #define NS_CLIENT_TRACE
76 #ifdef NS_CLIENT_TRACE
77 #define CTRACE(m) ns_client_log(client, \
78 NS_LOGCATEGORY_CLIENT, \
79 NS_LOGMODULE_CLIENT, \
82 #define MTRACE(m) isc_log_write(ns_g_lctx, \
83 NS_LOGCATEGORY_GENERAL, \
84 NS_LOGMODULE_CLIENT, \
86 "clientmgr @%p: %s", manager, (m))
88 #define CTRACE(m) ((void)(m))
89 #define MTRACE(m) ((void)(m))
92 #define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
94 #define TCP_BUFFER_SIZE (65535 + 2)
95 #define SEND_BUFFER_SIZE 4096
96 #define RECV_BUFFER_SIZE 4096
98 #ifdef ISC_PLATFORM_USETHREADS
101 * Number of 'mctx pools' for clients. (Should this be configurable?)
102 * When enabling threads, we use a pool of memory contexts shared by
103 * client objects, since concurrent access to a shared context would cause
104 * heavy contentions. The above constant is expected to be enough for
105 * completely avoiding contentions among threads for an authoritative-only
111 * If named with built without thread, simply share manager's context. Using
112 * a separate context in this case would simply waste memory.
116 /*% nameserver client manager structure */
117 struct ns_clientmgr {
121 /* The queue object has its own locks */
122 client_queue_t inactive; /*%< To be recycled */
125 isc_taskmgr_t * taskmgr;
126 isc_timermgr_t * timermgr;
128 /* Lock covers manager state. */
130 isc_boolean_t exiting;
132 /* Lock covers the clients list */
133 isc_mutex_t listlock;
134 client_list_t clients; /*%< All active clients */
136 /* Lock covers the recursing list */
138 client_list_t recursing; /*%< Recursing clients */
141 /*%< mctx pool for clients. */
142 unsigned int nextmctx;
143 isc_mem_t * mctxpool[NMCTXS];
147 #define MANAGER_MAGIC ISC_MAGIC('N', 'S', 'C', 'm')
148 #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, MANAGER_MAGIC)
151 * Client object states. Ordering is significant: higher-numbered
152 * states are generally "more active", meaning that the client can
153 * have more dynamically allocated data, outstanding events, etc.
154 * In the list below, any such properties listed for state N
155 * also apply to any state > N.
157 * To force the client into a less active state, set client->newstate
158 * to that state and call exit_check(). This will cause any
159 * activities defined for higher-numbered states to be aborted.
162 #define NS_CLIENTSTATE_FREED 0
164 * The client object no longer exists.
167 #define NS_CLIENTSTATE_INACTIVE 1
169 * The client object exists and has a task and timer.
170 * Its "query" struct and sendbuf are initialized.
171 * It is on the client manager's list of inactive clients.
172 * It has a message and OPT, both in the reset state.
175 #define NS_CLIENTSTATE_READY 2
177 * The client object is either a TCP or a UDP one, and
178 * it is associated with a network interface. It is on the
179 * client manager's list of active clients.
181 * If it is a TCP client object, it has a TCP listener socket
182 * and an outstanding TCP listen request.
184 * If it is a UDP client object, it has a UDP listener socket
185 * and an outstanding UDP receive request.
188 #define NS_CLIENTSTATE_READING 3
190 * The client object is a TCP client object that has received
191 * a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
192 * outstanding TCP read request. This state is not used for
193 * UDP client objects.
196 #define NS_CLIENTSTATE_WORKING 4
198 * The client object has received a request and is working
199 * on it. It has a view, and it may have any of a non-reset OPT,
200 * recursion quota, and an outstanding write request.
203 #define NS_CLIENTSTATE_RECURSING 5
205 * The client object is recursing. It will be on the 'recursing'
209 #define NS_CLIENTSTATE_MAX 9
211 * Sentinel value used to indicate "no state". When client->newstate
212 * has this value, we are not attempting to exit the current state.
213 * Must be greater than any valid state.
217 * Enable ns_client_dropport() by default.
219 #ifndef NS_CLIENT_DROPPORT
220 #define NS_CLIENT_DROPPORT 1
223 unsigned int ns_client_requests;
225 static void client_read(ns_client_t *client);
226 static void client_accept(ns_client_t *client);
227 static void client_udprecv(ns_client_t *client);
228 static void clientmgr_destroy(ns_clientmgr_t *manager);
229 static isc_boolean_t exit_check(ns_client_t *client);
230 static void ns_client_endrequest(ns_client_t *client);
231 static void client_start(isc_task_t *task, isc_event_t *event);
232 static void client_request(isc_task_t *task, isc_event_t *event);
233 static void ns_client_dumpmessage(ns_client_t *client, const char *reason);
234 static isc_result_t get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
235 dns_dispatch_t *disp, isc_boolean_t tcp);
238 ns_client_recursing(ns_client_t *client) {
239 REQUIRE(NS_CLIENT_VALID(client));
240 REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
242 LOCK(&client->manager->reclock);
243 client->newstate = client->state = NS_CLIENTSTATE_RECURSING;
244 ISC_LIST_APPEND(client->manager->recursing, client, rlink);
245 UNLOCK(&client->manager->reclock);
249 ns_client_killoldestquery(ns_client_t *client) {
251 REQUIRE(NS_CLIENT_VALID(client));
253 LOCK(&client->manager->reclock);
254 oldest = ISC_LIST_HEAD(client->manager->recursing);
255 if (oldest != NULL) {
256 ISC_LIST_UNLINK(client->manager->recursing, oldest, rlink);
257 UNLOCK(&client->manager->reclock);
258 ns_query_cancel(oldest);
260 UNLOCK(&client->manager->reclock);
264 ns_client_settimeout(ns_client_t *client, unsigned int seconds) {
266 isc_interval_t interval;
268 isc_interval_set(&interval, seconds, 0);
269 result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
270 &interval, ISC_FALSE);
271 client->timerset = ISC_TRUE;
272 if (result != ISC_R_SUCCESS) {
273 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
274 NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
275 "setting timeout: %s",
276 isc_result_totext(result));
277 /* Continue anyway. */
282 * Check for a deactivation or shutdown request and take appropriate
283 * action. Returns ISC_TRUE if either is in progress; in this case
284 * the caller must no longer use the client object as it may have been
288 exit_check(ns_client_t *client) {
289 isc_boolean_t destroy_manager = ISC_FALSE;
290 ns_clientmgr_t *manager = NULL;
292 REQUIRE(NS_CLIENT_VALID(client));
293 manager = client->manager;
295 if (client->state <= client->newstate)
296 return (ISC_FALSE); /* Business as usual. */
298 INSIST(client->newstate < NS_CLIENTSTATE_RECURSING);
301 * We need to detach from the view early when shutting down
302 * the server to break the following vicious circle:
304 * - The resolver will not shut down until the view refcount is zero
305 * - The view refcount does not go to zero until all clients detach
306 * - The client does not detach from the view until references is zero
307 * - references does not go to zero until the resolver has shut down
309 * Keep the view attached until any outstanding updates complete.
311 if (client->nupdates == 0 &&
312 client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
313 dns_view_detach(&client->view);
315 if (client->state == NS_CLIENTSTATE_WORKING ||
316 client->state == NS_CLIENTSTATE_RECURSING)
318 INSIST(client->newstate <= NS_CLIENTSTATE_READING);
320 * Let the update processing complete.
322 if (client->nupdates > 0)
326 * We are trying to abort request processing.
328 if (client->nsends > 0) {
329 isc_socket_t *socket;
330 if (TCP_CLIENT(client))
331 socket = client->tcpsocket;
333 socket = client->udpsocket;
334 isc_socket_cancel(socket, client->task,
335 ISC_SOCKCANCEL_SEND);
338 if (! (client->nsends == 0 && client->nrecvs == 0 &&
339 client->references == 0))
342 * Still waiting for I/O cancel completion.
343 * or lingering references.
349 * I/O cancel is complete. Burn down all state
350 * related to the current request. Ensure that
351 * the client is no longer on the recursing list.
353 * We need to check whether the client is still linked,
354 * because it may already have been removed from the
355 * recursing list by ns_client_killoldestquery()
357 if (client->state == NS_CLIENTSTATE_RECURSING) {
358 LOCK(&manager->reclock);
359 if (ISC_LINK_LINKED(client, rlink))
360 ISC_LIST_UNLINK(manager->recursing,
362 UNLOCK(&manager->reclock);
364 ns_client_endrequest(client);
366 client->state = NS_CLIENTSTATE_READING;
367 INSIST(client->recursionquota == NULL);
369 if (NS_CLIENTSTATE_READING == client->newstate) {
371 client->newstate = NS_CLIENTSTATE_MAX;
372 return (ISC_TRUE); /* We're done. */
376 if (client->state == NS_CLIENTSTATE_READING) {
378 * We are trying to abort the current TCP connection,
381 INSIST(client->recursionquota == NULL);
382 INSIST(client->newstate <= NS_CLIENTSTATE_READY);
383 if (client->nreads > 0)
384 dns_tcpmsg_cancelread(&client->tcpmsg);
385 if (! client->nreads == 0) {
386 /* Still waiting for read cancel completion. */
390 if (client->tcpmsg_valid) {
391 dns_tcpmsg_invalidate(&client->tcpmsg);
392 client->tcpmsg_valid = ISC_FALSE;
394 if (client->tcpsocket != NULL) {
396 isc_socket_detach(&client->tcpsocket);
399 if (client->tcpquota != NULL)
400 isc_quota_detach(&client->tcpquota);
402 if (client->timerset) {
403 (void)isc_timer_reset(client->timer,
404 isc_timertype_inactive,
405 NULL, NULL, ISC_TRUE);
406 client->timerset = ISC_FALSE;
409 client->peeraddr_valid = ISC_FALSE;
411 client->state = NS_CLIENTSTATE_READY;
412 INSIST(client->recursionquota == NULL);
415 * Now the client is ready to accept a new TCP connection
416 * or UDP request, but we may have enough clients doing
417 * that already. Check whether this client needs to remain
418 * active and force it to go inactive if not.
420 * UDP clients go inactive at this point, but TCP clients
421 * may remain active if we have fewer active TCP client
422 * objects than desired due to an earlier quota exhaustion.
424 if (client->mortal && TCP_CLIENT(client) && !ns_g_clienttest) {
425 LOCK(&client->interface->lock);
426 if (client->interface->ntcpcurrent <
427 client->interface->ntcptarget)
428 client->mortal = ISC_FALSE;
429 UNLOCK(&client->interface->lock);
433 * We don't need the client; send it to the inactive
434 * queue for recycling.
436 if (client->mortal) {
437 if (client->newstate > NS_CLIENTSTATE_INACTIVE)
438 client->newstate = NS_CLIENTSTATE_INACTIVE;
441 if (NS_CLIENTSTATE_READY == client->newstate) {
442 if (TCP_CLIENT(client)) {
443 client_accept(client);
445 client_udprecv(client);
446 client->newstate = NS_CLIENTSTATE_MAX;
451 if (client->state == NS_CLIENTSTATE_READY) {
452 INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
455 * We are trying to enter the inactive state.
457 if (client->naccepts > 0)
458 isc_socket_cancel(client->tcplistener, client->task,
459 ISC_SOCKCANCEL_ACCEPT);
461 /* Still waiting for accept cancel completion. */
462 if (! (client->naccepts == 0))
465 /* Accept cancel is complete. */
466 if (client->nrecvs > 0)
467 isc_socket_cancel(client->udpsocket, client->task,
468 ISC_SOCKCANCEL_RECV);
470 /* Still waiting for recv cancel completion. */
471 if (! (client->nrecvs == 0))
474 /* Still waiting for control event to be delivered */
475 if (client->nctls > 0)
478 /* Deactivate the client. */
479 if (client->interface)
480 ns_interface_detach(&client->interface);
482 INSIST(client->naccepts == 0);
483 INSIST(client->recursionquota == NULL);
484 if (client->tcplistener != NULL)
485 isc_socket_detach(&client->tcplistener);
487 if (client->udpsocket != NULL)
488 isc_socket_detach(&client->udpsocket);
490 if (client->dispatch != NULL)
491 dns_dispatch_detach(&client->dispatch);
493 client->attributes = 0;
494 client->mortal = ISC_FALSE;
497 * Put the client on the inactive list. If we are aiming for
498 * the "freed" state, it will be removed from the inactive
499 * list shortly, and we need to keep the manager locked until
500 * that has been done, lest the manager decide to reactivate
501 * the dying client inbetween.
503 client->state = NS_CLIENTSTATE_INACTIVE;
504 INSIST(client->recursionquota == NULL);
506 if (client->state == client->newstate) {
507 client->newstate = NS_CLIENTSTATE_MAX;
508 if (!ns_g_clienttest && manager != NULL &&
510 ISC_QUEUE_PUSH(manager->inactive, client,
512 if (client->needshutdown)
513 isc_task_shutdown(client->task);
518 if (client->state == NS_CLIENTSTATE_INACTIVE) {
519 INSIST(client->newstate == NS_CLIENTSTATE_FREED);
521 * We are trying to free the client.
523 * When "shuttingdown" is true, either the task has received
524 * its shutdown event or no shutdown event has ever been
525 * set up. Thus, we have no outstanding shutdown
526 * event at this point.
528 REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
530 INSIST(client->recursionquota == NULL);
531 INSIST(!ISC_QLINK_LINKED(client, ilink));
533 ns_query_free(client);
534 isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
535 isc_event_free((isc_event_t **)&client->sendevent);
536 isc_event_free((isc_event_t **)&client->recvevent);
537 isc_timer_detach(&client->timer);
539 if (client->tcpbuf != NULL)
540 isc_mem_put(client->mctx, client->tcpbuf,
542 if (client->opt != NULL) {
543 INSIST(dns_rdataset_isassociated(client->opt));
544 dns_rdataset_disassociate(client->opt);
545 dns_message_puttemprdataset(client->message,
549 dns_message_destroy(&client->message);
550 if (manager != NULL) {
551 LOCK(&manager->listlock);
552 ISC_LIST_UNLINK(manager->clients, client, link);
553 LOCK(&manager->lock);
554 if (manager->exiting &&
555 ISC_LIST_EMPTY(manager->clients))
556 destroy_manager = ISC_TRUE;
557 UNLOCK(&manager->lock);
558 UNLOCK(&manager->listlock);
562 * Detaching the task must be done after unlinking from
563 * the manager's lists because the manager accesses
566 if (client->task != NULL)
567 isc_task_detach(&client->task);
573 * Check that there are no other external references to
574 * the memory context.
576 if (ns_g_clienttest && isc_mem_references(client->mctx) != 1) {
577 isc_mem_stats(client->mctx, stderr);
580 isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
583 if (destroy_manager && manager != NULL)
584 clientmgr_destroy(manager);
590 * The client's task has received the client's control event
591 * as part of the startup process.
594 client_start(isc_task_t *task, isc_event_t *event) {
595 ns_client_t *client = (ns_client_t *) event->ev_arg;
597 INSIST(task == client->task);
601 INSIST(client->nctls == 1);
604 if (exit_check(client))
607 if (TCP_CLIENT(client)) {
608 client_accept(client);
610 client_udprecv(client);
616 * The client's task has received a shutdown event.
619 client_shutdown(isc_task_t *task, isc_event_t *event) {
622 REQUIRE(event != NULL);
623 REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
624 client = event->ev_arg;
625 REQUIRE(NS_CLIENT_VALID(client));
626 REQUIRE(task == client->task);
632 isc_event_free(&event);
634 if (client->shutdown != NULL) {
635 (client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
636 client->shutdown = NULL;
637 client->shutdown_arg = NULL;
640 if (ISC_QLINK_LINKED(client, ilink))
641 ISC_QUEUE_UNLINK(client->manager->inactive, client, ilink);
643 client->newstate = NS_CLIENTSTATE_FREED;
644 client->needshutdown = ISC_FALSE;
645 (void)exit_check(client);
649 ns_client_endrequest(ns_client_t *client) {
650 INSIST(client->naccepts == 0);
651 INSIST(client->nreads == 0);
652 INSIST(client->nsends == 0);
653 INSIST(client->nrecvs == 0);
654 INSIST(client->nupdates == 0);
655 INSIST(client->state == NS_CLIENTSTATE_WORKING ||
656 client->state == NS_CLIENTSTATE_RECURSING);
658 CTRACE("endrequest");
660 if (client->next != NULL) {
661 (client->next)(client);
665 if (client->view != NULL)
666 dns_view_detach(&client->view);
667 if (client->opt != NULL) {
668 INSIST(dns_rdataset_isassociated(client->opt));
669 dns_rdataset_disassociate(client->opt);
670 dns_message_puttemprdataset(client->message, &client->opt);
673 client->signer = NULL;
674 client->udpsize = 512;
675 client->extflags = 0;
676 client->ednsversion = -1;
677 dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
679 if (client->recursionquota != NULL)
680 isc_quota_detach(&client->recursionquota);
683 * Clear all client attributes that are specific to
684 * the request; that's all except the TCP flag.
686 client->attributes &= NS_CLIENTATTR_TCP;
690 ns_client_next(ns_client_t *client, isc_result_t result) {
693 REQUIRE(NS_CLIENT_VALID(client));
694 REQUIRE(client->state == NS_CLIENTSTATE_WORKING ||
695 client->state == NS_CLIENTSTATE_RECURSING ||
696 client->state == NS_CLIENTSTATE_READING);
700 if (result != ISC_R_SUCCESS)
701 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
702 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
703 "request failed: %s", isc_result_totext(result));
706 * An error processing a TCP request may have left
707 * the connection out of sync. To be safe, we always
708 * sever the connection when result != ISC_R_SUCCESS.
710 if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
711 newstate = NS_CLIENTSTATE_READING;
713 newstate = NS_CLIENTSTATE_READY;
715 if (client->newstate > newstate)
716 client->newstate = newstate;
717 (void)exit_check(client);
722 client_senddone(isc_task_t *task, isc_event_t *event) {
724 isc_socketevent_t *sevent = (isc_socketevent_t *) event;
726 REQUIRE(sevent != NULL);
727 REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
728 client = sevent->ev_arg;
729 REQUIRE(NS_CLIENT_VALID(client));
730 REQUIRE(task == client->task);
731 REQUIRE(sevent == client->sendevent);
737 if (sevent->result != ISC_R_SUCCESS)
738 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
739 NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
740 "error sending response: %s",
741 isc_result_totext(sevent->result));
743 INSIST(client->nsends > 0);
746 if (client->tcpbuf != NULL) {
747 INSIST(TCP_CLIENT(client));
748 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
749 client->tcpbuf = NULL;
752 ns_client_next(client, ISC_R_SUCCESS);
756 * We only want to fail with ISC_R_NOSPACE when called from
757 * ns_client_sendraw() and not when called from ns_client_send(),
758 * tcpbuffer is NULL when called from ns_client_sendraw() and
759 * length != 0. tcpbuffer != NULL when called from ns_client_send()
764 client_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
765 isc_buffer_t *tcpbuffer, isc_uint32_t length,
766 unsigned char *sendbuf, unsigned char **datap)
769 isc_uint32_t bufsize;
772 INSIST(datap != NULL);
773 INSIST((tcpbuffer == NULL && length != 0) ||
774 (tcpbuffer != NULL && length == 0));
776 if (TCP_CLIENT(client)) {
777 INSIST(client->tcpbuf == NULL);
778 if (length + 2 > TCP_BUFFER_SIZE) {
779 result = ISC_R_NOSPACE;
782 client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
783 if (client->tcpbuf == NULL) {
784 result = ISC_R_NOMEMORY;
787 data = client->tcpbuf;
788 if (tcpbuffer != NULL) {
789 isc_buffer_init(tcpbuffer, data, TCP_BUFFER_SIZE);
790 isc_buffer_init(buffer, data + 2, TCP_BUFFER_SIZE - 2);
792 isc_buffer_init(buffer, data, TCP_BUFFER_SIZE);
793 INSIST(length <= 0xffff);
794 isc_buffer_putuint16(buffer, (isc_uint16_t)length);
798 if (client->udpsize < SEND_BUFFER_SIZE)
799 bufsize = client->udpsize;
801 bufsize = SEND_BUFFER_SIZE;
802 if (length > bufsize) {
803 result = ISC_R_NOSPACE;
806 isc_buffer_init(buffer, data, bufsize);
809 result = ISC_R_SUCCESS;
816 client_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
817 struct in6_pktinfo *pktinfo;
820 isc_sockaddr_t *address;
821 isc_socket_t *socket;
822 isc_netaddr_t netaddr;
824 unsigned int sockflags = ISC_SOCKFLAG_IMMEDIATE;
826 if (TCP_CLIENT(client)) {
827 socket = client->tcpsocket;
830 socket = client->udpsocket;
831 address = &client->peeraddr;
833 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
834 if (ns_g_server->blackholeacl != NULL &&
835 dns_acl_match(&netaddr, NULL,
836 ns_g_server->blackholeacl,
837 &ns_g_server->aclenv,
838 &match, NULL) == ISC_R_SUCCESS &&
840 return (DNS_R_BLACKHOLED);
841 sockflags |= ISC_SOCKFLAG_NORETRY;
844 if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0 &&
845 (client->attributes & NS_CLIENTATTR_MULTICAST) == 0)
846 pktinfo = &client->pktinfo;
850 isc_buffer_usedregion(buffer, &r);
854 result = isc_socket_sendto2(socket, &r, client->task,
856 client->sendevent, sockflags);
857 if (result == ISC_R_SUCCESS || result == ISC_R_INPROGRESS) {
859 if (result == ISC_R_SUCCESS)
860 client_senddone(client->task,
861 (isc_event_t *)client->sendevent);
862 result = ISC_R_SUCCESS;
868 ns_client_sendraw(ns_client_t *client, dns_message_t *message) {
874 unsigned char sendbuf[SEND_BUFFER_SIZE];
876 REQUIRE(NS_CLIENT_VALID(client));
880 mr = dns_message_getrawmessage(message);
882 result = ISC_R_UNEXPECTEDEND;
886 result = client_allocsendbuf(client, &buffer, NULL, mr->length,
888 if (result != ISC_R_SUCCESS)
892 * Copy message to buffer and fixup id.
894 isc_buffer_availableregion(&buffer, &r);
895 result = isc_buffer_copyregion(&buffer, mr);
896 if (result != ISC_R_SUCCESS)
898 r.base[0] = (client->message->id >> 8) & 0xff;
899 r.base[1] = client->message->id & 0xff;
901 result = client_sendpkg(client, &buffer);
902 if (result == ISC_R_SUCCESS)
906 if (client->tcpbuf != NULL) {
907 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
908 client->tcpbuf = NULL;
910 ns_client_next(client, result);
914 ns_client_send(ns_client_t *client) {
918 isc_buffer_t tcpbuffer;
921 isc_boolean_t cleanup_cctx = ISC_FALSE;
922 unsigned char sendbuf[SEND_BUFFER_SIZE];
923 unsigned int render_opts;
924 unsigned int preferred_glue;
925 isc_boolean_t opt_included = ISC_FALSE;
927 REQUIRE(NS_CLIENT_VALID(client));
931 if ((client->attributes & NS_CLIENTATTR_RA) != 0)
932 client->message->flags |= DNS_MESSAGEFLAG_RA;
934 if ((client->attributes & NS_CLIENTATTR_WANTDNSSEC) != 0)
937 render_opts = DNS_MESSAGERENDER_OMITDNSSEC;
940 if (client->view != NULL) {
941 if (client->view->preferred_glue == dns_rdatatype_a)
942 preferred_glue = DNS_MESSAGERENDER_PREFER_A;
943 else if (client->view->preferred_glue == dns_rdatatype_aaaa)
944 preferred_glue = DNS_MESSAGERENDER_PREFER_AAAA;
947 #ifdef ALLOW_FILTER_AAAA_ON_V4
949 * filter-aaaa-on-v4 yes or break-dnssec option to suppress
951 * We already know that request came via IPv4,
952 * that we have both AAAA and A records,
953 * and that we either have no signatures that the client wants
954 * or we are supposed to break DNSSEC.
956 * Override preferred glue if necessary.
958 if ((client->attributes & NS_CLIENTATTR_FILTER_AAAA) != 0) {
959 render_opts |= DNS_MESSAGERENDER_FILTER_AAAA;
960 if (preferred_glue == DNS_MESSAGERENDER_PREFER_AAAA)
961 preferred_glue = DNS_MESSAGERENDER_PREFER_A;
966 * XXXRTH The following doesn't deal with TCP buffer resizing.
968 result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0,
970 if (result != ISC_R_SUCCESS)
973 result = dns_compress_init(&cctx, -1, client->mctx);
974 if (result != ISC_R_SUCCESS)
976 dns_compress_setsensitive(&cctx, ISC_TRUE);
977 cleanup_cctx = ISC_TRUE;
979 result = dns_message_renderbegin(client->message, &cctx, &buffer);
980 if (result != ISC_R_SUCCESS)
983 if (client->opt != NULL) {
984 result = dns_message_setopt(client->message, client->opt);
985 opt_included = ISC_TRUE;
987 if (result != ISC_R_SUCCESS)
990 result = dns_message_rendersection(client->message,
991 DNS_SECTION_QUESTION, 0);
992 if (result == ISC_R_NOSPACE) {
993 client->message->flags |= DNS_MESSAGEFLAG_TC;
996 if (result != ISC_R_SUCCESS)
1000 * Stop after the question if TC was set for rate limiting.
1002 if ((client->message->flags & DNS_MESSAGEFLAG_TC) != 0)
1004 #endif /* USE_RRL */
1005 result = dns_message_rendersection(client->message,
1007 DNS_MESSAGERENDER_PARTIAL |
1009 if (result == ISC_R_NOSPACE) {
1010 client->message->flags |= DNS_MESSAGEFLAG_TC;
1013 if (result != ISC_R_SUCCESS)
1015 result = dns_message_rendersection(client->message,
1016 DNS_SECTION_AUTHORITY,
1017 DNS_MESSAGERENDER_PARTIAL |
1019 if (result == ISC_R_NOSPACE) {
1020 client->message->flags |= DNS_MESSAGEFLAG_TC;
1023 if (result != ISC_R_SUCCESS)
1025 result = dns_message_rendersection(client->message,
1026 DNS_SECTION_ADDITIONAL,
1027 preferred_glue | render_opts);
1028 if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
1031 result = dns_message_renderend(client->message);
1033 if (result != ISC_R_SUCCESS)
1037 dns_compress_invalidate(&cctx);
1038 cleanup_cctx = ISC_FALSE;
1041 if (TCP_CLIENT(client)) {
1042 isc_buffer_usedregion(&buffer, &r);
1043 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
1044 isc_buffer_add(&tcpbuffer, r.length);
1045 result = client_sendpkg(client, &tcpbuffer);
1047 result = client_sendpkg(client, &buffer);
1049 /* update statistics (XXXJT: is it okay to access message->xxxkey?) */
1050 isc_stats_increment(ns_g_server->nsstats, dns_nsstatscounter_response);
1052 isc_stats_increment(ns_g_server->nsstats,
1053 dns_nsstatscounter_edns0out);
1055 if (client->message->tsigkey != NULL) {
1056 isc_stats_increment(ns_g_server->nsstats,
1057 dns_nsstatscounter_tsigout);
1059 if (client->message->sig0key != NULL) {
1060 isc_stats_increment(ns_g_server->nsstats,
1061 dns_nsstatscounter_sig0out);
1063 if ((client->message->flags & DNS_MESSAGEFLAG_TC) != 0)
1064 isc_stats_increment(ns_g_server->nsstats,
1065 dns_nsstatscounter_truncatedresp);
1067 if (result == ISC_R_SUCCESS)
1071 if (client->tcpbuf != NULL) {
1072 isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
1073 client->tcpbuf = NULL;
1077 dns_compress_invalidate(&cctx);
1079 ns_client_next(client, result);
1082 #if NS_CLIENT_DROPPORT
1083 #define DROPPORT_NO 0
1084 #define DROPPORT_REQUEST 1
1085 #define DROPPORT_RESPONSE 2
1087 * ns_client_dropport determines if certain requests / responses
1088 * should be dropped based on the port number.
1091 * \li 0: Don't drop.
1092 * \li 1: Drop request.
1093 * \li 2: Drop (error) response.
1096 ns_client_dropport(in_port_t port) {
1099 case 13: /* daytime */
1100 case 19: /* chargen */
1102 return (DROPPORT_REQUEST);
1103 case 464: /* kpasswd */
1104 return (DROPPORT_RESPONSE);
1106 return (DROPPORT_NO);
1111 ns_client_error(ns_client_t *client, isc_result_t result) {
1113 dns_message_t *message;
1115 REQUIRE(NS_CLIENT_VALID(client));
1119 message = client->message;
1120 rcode = dns_result_torcode(result);
1122 #if NS_CLIENT_DROPPORT
1124 * Don't send FORMERR to ports on the drop port list.
1126 if (rcode == dns_rcode_formerr &&
1127 ns_client_dropport(isc_sockaddr_getport(&client->peeraddr)) !=
1132 isc_buffer_init(&b, buf, sizeof(buf) - 1);
1133 if (dns_rcode_totext(rcode, &b) != ISC_R_SUCCESS)
1134 isc_buffer_putstr(&b, "UNKNOWN RCODE");
1135 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1136 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1137 "dropped error (%.*s) response: suspicious port",
1138 (int)isc_buffer_usedlength(&b), buf);
1139 ns_client_next(client, ISC_R_SUCCESS);
1146 * Try to rate limit error responses.
1148 if (client->view != NULL && client->view->rrl != NULL) {
1149 isc_boolean_t wouldlog;
1150 char log_buf[DNS_RRL_LOG_BUF_LEN];
1151 dns_rrl_result_t rrl_result;
1153 INSIST(rcode != dns_rcode_noerror &&
1154 rcode != dns_rcode_nxdomain);
1155 wouldlog = isc_log_wouldlog(ns_g_lctx, DNS_RRL_LOG_DROP);
1156 rrl_result = dns_rrl(client->view, &client->peeraddr,
1158 dns_rdataclass_in, dns_rdatatype_none,
1159 NULL, result, client->now,
1160 wouldlog, log_buf, sizeof(log_buf));
1161 if (rrl_result != DNS_RRL_RESULT_OK) {
1163 * Log dropped errors in the query category
1164 * so that they are not lost in silence.
1165 * Starts of rate-limited bursts are logged in
1166 * NS_LOGCATEGORY_RRL.
1169 ns_client_log(client,
1170 NS_LOGCATEGORY_QUERY_EERRORS,
1171 NS_LOGMODULE_CLIENT,
1176 * Some error responses cannot be 'slipped',
1177 * so don't try to slip any error responses.
1179 if (!client->view->rrl->log_only) {
1180 isc_stats_increment(ns_g_server->nsstats,
1181 dns_nsstatscounter_ratedropped);
1182 isc_stats_increment(ns_g_server->nsstats,
1183 dns_nsstatscounter_dropped);
1184 ns_client_next(client, DNS_R_DROP);
1189 #endif /* USE_RRL */
1192 * Message may be an in-progress reply that we had trouble
1193 * with, in which case QR will be set. We need to clear QR before
1194 * calling dns_message_reply() to avoid triggering an assertion.
1196 message->flags &= ~DNS_MESSAGEFLAG_QR;
1198 * AA and AD shouldn't be set.
1200 message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
1201 result = dns_message_reply(message, ISC_TRUE);
1202 if (result != ISC_R_SUCCESS) {
1204 * It could be that we've got a query with a good header,
1205 * but a bad question section, so we try again with
1206 * want_question_section set to ISC_FALSE.
1208 result = dns_message_reply(message, ISC_FALSE);
1209 if (result != ISC_R_SUCCESS) {
1210 ns_client_next(client, result);
1214 message->rcode = rcode;
1217 * FORMERR loop avoidance: If we sent a FORMERR message
1218 * with the same ID to the same client less than two
1219 * seconds ago, assume that we are in an infinite error
1220 * packet dialog with a server for some protocol whose
1221 * error responses look enough like DNS queries to
1222 * elicit a FORMERR response. Drop a packet to break
1225 if (rcode == dns_rcode_formerr) {
1226 if (isc_sockaddr_equal(&client->peeraddr,
1227 &client->formerrcache.addr) &&
1228 message->id == client->formerrcache.id &&
1229 client->requesttime - client->formerrcache.time < 2) {
1231 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1232 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
1233 "possible error packet loop, "
1235 ns_client_next(client, result);
1238 client->formerrcache.addr = client->peeraddr;
1239 client->formerrcache.time = client->requesttime;
1240 client->formerrcache.id = message->id;
1242 ns_client_send(client);
1245 static inline isc_result_t
1246 client_addopt(ns_client_t *client) {
1247 char nsid[BUFSIZ], *nsidp;
1248 isc_result_t result;
1250 dns_resolver_t *resolver;
1251 isc_uint16_t udpsize;
1252 dns_ednsopt_t ednsopts[2];
1256 REQUIRE(client->opt == NULL); /* XXXRTH free old. */
1258 view = client->view;
1259 resolver = (view != NULL) ? view->resolver : NULL;
1260 if (resolver != NULL)
1261 udpsize = dns_resolver_getudpsize(resolver);
1263 udpsize = ns_g_udpsize;
1265 flags = client->extflags & DNS_MESSAGEEXTFLAG_REPLYPRESERVE;
1267 /* Set EDNS options if applicable */
1268 if ((client->attributes & NS_CLIENTATTR_WANTNSID) != 0 &&
1269 (ns_g_server->server_id != NULL ||
1270 ns_g_server->server_usehostname)) {
1271 if (ns_g_server->server_usehostname) {
1272 isc_result_t result;
1273 result = ns_os_gethostname(nsid, sizeof(nsid));
1274 if (result != ISC_R_SUCCESS) {
1279 nsidp = ns_g_server->server_id;
1281 ednsopts[count].code = DNS_OPT_NSID;
1282 ednsopts[count].length = strlen(nsidp);
1283 ednsopts[count].value = (unsigned char *)nsidp;
1287 result = dns_message_buildopt(client->message, &client->opt, 0,
1288 udpsize, flags, ednsopts, count);
1292 static inline isc_boolean_t
1293 allowed(isc_netaddr_t *addr, dns_name_t *signer, dns_acl_t *acl) {
1295 isc_result_t result;
1299 result = dns_acl_match(addr, signer, acl, &ns_g_server->aclenv,
1301 if (result == ISC_R_SUCCESS && match > 0)
1307 * Callback to see if a non-recursive query coming from 'srcaddr' to
1308 * 'destaddr', with optional key 'mykey' for class 'rdclass' would be
1309 * delivered to 'myview'.
1311 * We run this unlocked as both the view list and the interface list
1312 * are updated when the appropriate task has exclusivity.
1315 ns_client_isself(dns_view_t *myview, dns_tsigkey_t *mykey,
1316 isc_sockaddr_t *srcaddr, isc_sockaddr_t *dstaddr,
1317 dns_rdataclass_t rdclass, void *arg)
1320 dns_tsigkey_t *key = NULL;
1321 dns_name_t *tsig = NULL;
1322 isc_netaddr_t netsrc;
1323 isc_netaddr_t netdst;
1328 * ns_g_server->interfacemgr is task exclusive locked.
1330 if (ns_g_server->interfacemgr == NULL)
1333 if (!ns_interfacemgr_listeningon(ns_g_server->interfacemgr, dstaddr))
1336 isc_netaddr_fromsockaddr(&netsrc, srcaddr);
1337 isc_netaddr_fromsockaddr(&netdst, dstaddr);
1339 for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
1341 view = ISC_LIST_NEXT(view, link)) {
1343 if (view->matchrecursiveonly)
1346 if (rdclass != view->rdclass)
1349 if (mykey != NULL) {
1350 isc_boolean_t match;
1351 isc_result_t result;
1353 result = dns_view_gettsig(view, &mykey->name, &key);
1354 if (result != ISC_R_SUCCESS)
1356 match = dst_key_compare(mykey->key, key->key);
1357 dns_tsigkey_detach(&key);
1360 tsig = dns_tsigkey_identity(mykey);
1363 if (allowed(&netsrc, tsig, view->matchclients) &&
1364 allowed(&netdst, tsig, view->matchdestinations))
1367 return (ISC_TF(view == myview));
1371 process_opt(ns_client_t *client, dns_rdataset_t *opt) {
1373 isc_buffer_t optbuf;
1374 isc_result_t result;
1375 isc_uint16_t optcode;
1376 isc_uint16_t optlen;
1379 * Set the client's UDP buffer size.
1381 client->udpsize = opt->rdclass;
1384 * If the requested UDP buffer size is less than 512,
1385 * ignore it and use 512.
1387 if (client->udpsize < 512)
1388 client->udpsize = 512;
1391 * Get the flags out of the OPT record.
1393 client->extflags = (isc_uint16_t)(opt->ttl & 0xFFFF);
1396 * Do we understand this version of EDNS?
1398 * XXXRTH need library support for this!
1400 client->ednsversion = (opt->ttl & 0x00FF0000) >> 16;
1401 if (client->ednsversion > 0) {
1402 isc_stats_increment(ns_g_server->nsstats,
1403 dns_nsstatscounter_badednsver);
1404 result = client_addopt(client);
1405 if (result == ISC_R_SUCCESS)
1406 result = DNS_R_BADVERS;
1407 ns_client_error(client, result);
1411 /* Check for NSID request */
1412 result = dns_rdataset_first(opt);
1413 if (result == ISC_R_SUCCESS) {
1414 dns_rdata_init(&rdata);
1415 dns_rdataset_current(opt, &rdata);
1416 isc_buffer_init(&optbuf, rdata.data, rdata.length);
1417 isc_buffer_add(&optbuf, rdata.length);
1418 while (isc_buffer_remaininglength(&optbuf) >= 4) {
1419 optcode = isc_buffer_getuint16(&optbuf);
1420 optlen = isc_buffer_getuint16(&optbuf);
1423 client->attributes |= NS_CLIENTATTR_WANTNSID;
1424 isc_buffer_forward(&optbuf, optlen);
1427 isc_buffer_forward(&optbuf, optlen);
1433 isc_stats_increment(ns_g_server->nsstats, dns_nsstatscounter_edns0in);
1436 * Create an OPT for our reply.
1438 result = client_addopt(client);
1439 if (result != ISC_R_SUCCESS) {
1440 ns_client_error(client, result);
1448 * Handle an incoming request event from the socket (UDP case)
1449 * or tcpmsg (TCP case).
1452 client_request(isc_task_t *task, isc_event_t *event) {
1453 ns_client_t *client;
1454 isc_socketevent_t *sevent;
1455 isc_result_t result;
1456 isc_result_t sigresult = ISC_R_SUCCESS;
1457 isc_buffer_t *buffer;
1458 isc_buffer_t tbuffer;
1460 dns_rdataset_t *opt;
1461 dns_name_t *signame;
1462 isc_boolean_t ra; /* Recursion available. */
1463 isc_netaddr_t netaddr;
1467 isc_boolean_t notimp;
1469 REQUIRE(event != NULL);
1470 client = event->ev_arg;
1471 REQUIRE(NS_CLIENT_VALID(client));
1472 REQUIRE(task == client->task);
1474 INSIST(client->recursionquota == NULL);
1476 INSIST(client->state == (TCP_CLIENT(client) ?
1477 NS_CLIENTSTATE_READING :
1478 NS_CLIENTSTATE_READY));
1480 ns_client_requests++;
1482 if (event->ev_type == ISC_SOCKEVENT_RECVDONE) {
1483 INSIST(!TCP_CLIENT(client));
1484 sevent = (isc_socketevent_t *)event;
1485 REQUIRE(sevent == client->recvevent);
1486 isc_buffer_init(&tbuffer, sevent->region.base, sevent->n);
1487 isc_buffer_add(&tbuffer, sevent->n);
1489 result = sevent->result;
1490 if (result == ISC_R_SUCCESS) {
1491 client->peeraddr = sevent->address;
1492 client->peeraddr_valid = ISC_TRUE;
1494 if ((sevent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
1495 client->attributes |= NS_CLIENTATTR_PKTINFO;
1496 client->pktinfo = sevent->pktinfo;
1498 if ((sevent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
1499 client->attributes |= NS_CLIENTATTR_MULTICAST;
1502 INSIST(TCP_CLIENT(client));
1503 REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
1504 REQUIRE(event->ev_sender == &client->tcpmsg);
1505 buffer = &client->tcpmsg.buffer;
1506 result = client->tcpmsg.result;
1507 INSIST(client->nreads == 1);
1509 * client->peeraddr was set when the connection was accepted.
1514 if (exit_check(client))
1516 client->state = client->newstate = NS_CLIENTSTATE_WORKING;
1518 isc_task_getcurrenttime(task, &client->requesttime);
1519 client->now = client->requesttime;
1521 if (result != ISC_R_SUCCESS) {
1522 if (TCP_CLIENT(client)) {
1523 ns_client_next(client, result);
1525 if (result != ISC_R_CANCELED)
1526 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
1527 NS_LOGMODULE_CLIENT,
1529 "UDP client handler shutting "
1530 "down due to fatal receive "
1532 isc_result_totext(result));
1533 isc_task_shutdown(client->task);
1538 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
1540 #if NS_CLIENT_DROPPORT
1541 if (ns_client_dropport(isc_sockaddr_getport(&client->peeraddr)) ==
1543 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1544 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1545 "dropped request: suspicious port");
1546 ns_client_next(client, ISC_R_SUCCESS);
1551 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1552 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1554 TCP_CLIENT(client) ? "TCP" : "UDP");
1557 * Check the blackhole ACL for UDP only, since TCP is done in
1560 if (!TCP_CLIENT(client)) {
1562 if (ns_g_server->blackholeacl != NULL &&
1563 dns_acl_match(&netaddr, NULL, ns_g_server->blackholeacl,
1564 &ns_g_server->aclenv,
1565 &match, NULL) == ISC_R_SUCCESS &&
1568 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1569 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1570 "blackholed UDP datagram");
1571 ns_client_next(client, ISC_R_SUCCESS);
1577 * Silently drop multicast requests for the present.
1578 * XXXMPA revisit this as mDNS spec was published.
1580 if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) {
1581 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1582 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
1583 "dropping multicast request");
1584 ns_client_next(client, DNS_R_REFUSED);
1588 result = dns_message_peekheader(buffer, &id, &flags);
1589 if (result != ISC_R_SUCCESS) {
1591 * There isn't enough header to determine whether
1592 * this was a request or a response. Drop it.
1594 ns_client_next(client, result);
1599 * The client object handles requests, not responses.
1600 * If this is a UDP response, forward it to the dispatcher.
1601 * If it's a TCP response, discard it here.
1603 if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
1604 if (TCP_CLIENT(client)) {
1605 CTRACE("unexpected response");
1606 ns_client_next(client, DNS_R_FORMERR);
1609 dns_dispatch_importrecv(client->dispatch, event);
1610 ns_client_next(client, ISC_R_SUCCESS);
1616 * Update some statistics counters. Don't count responses.
1618 if (isc_sockaddr_pf(&client->peeraddr) == PF_INET) {
1619 isc_stats_increment(ns_g_server->nsstats,
1620 dns_nsstatscounter_requestv4);
1622 isc_stats_increment(ns_g_server->nsstats,
1623 dns_nsstatscounter_requestv6);
1625 if (TCP_CLIENT(client))
1626 isc_stats_increment(ns_g_server->nsstats,
1627 dns_nsstatscounter_tcp);
1630 * It's a request. Parse it.
1632 result = dns_message_parse(client->message, buffer, 0);
1633 if (result != ISC_R_SUCCESS) {
1635 * Parsing the request failed. Send a response
1636 * (typically FORMERR or SERVFAIL).
1638 ns_client_error(client, result);
1642 dns_opcodestats_increment(ns_g_server->opcodestats,
1643 client->message->opcode);
1644 switch (client->message->opcode) {
1645 case dns_opcode_query:
1646 case dns_opcode_update:
1647 case dns_opcode_notify:
1650 case dns_opcode_iquery:
1656 client->message->rcode = dns_rcode_noerror;
1658 /* RFC1123 section 6.1.3.2 */
1659 if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0)
1660 client->message->flags &= ~DNS_MESSAGEFLAG_RD;
1665 opt = dns_message_getopt(client->message);
1667 result = process_opt(client, opt);
1668 if (result != ISC_R_SUCCESS)
1672 if (client->message->rdclass == 0) {
1673 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1674 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
1675 "message class could not be determined");
1676 ns_client_dumpmessage(client,
1677 "message class could not be determined");
1678 ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR);
1683 * Determine the destination address. If the receiving interface is
1684 * bound to a specific address, we simply use it regardless of the
1685 * address family. All IPv4 queries should fall into this case.
1686 * Otherwise, if this is a TCP query, get the address from the
1687 * receiving socket (this needs a system call and can be heavy).
1688 * For IPv6 UDP queries, we get this from the pktinfo structure (if
1690 * If all the attempts fail (this can happen due to memory shortage,
1691 * etc), we regard this as an error for safety.
1693 if ((client->interface->flags & NS_INTERFACEFLAG_ANYADDR) == 0)
1694 isc_netaddr_fromsockaddr(&client->destaddr,
1695 &client->interface->addr);
1697 isc_sockaddr_t sockaddr;
1698 result = ISC_R_FAILURE;
1700 if (TCP_CLIENT(client))
1701 result = isc_socket_getsockname(client->tcpsocket,
1703 if (result == ISC_R_SUCCESS)
1704 isc_netaddr_fromsockaddr(&client->destaddr, &sockaddr);
1705 if (result != ISC_R_SUCCESS &&
1706 client->interface->addr.type.sa.sa_family == AF_INET6 &&
1707 (client->attributes & NS_CLIENTATTR_PKTINFO) != 0) {
1709 * XXXJT technically, we should convert the receiving
1710 * interface ID to a proper scope zone ID. However,
1711 * due to the fact there is no standard API for this,
1712 * we only handle link-local addresses and use the
1713 * interface index as link ID. Despite the assumption,
1714 * it should cover most typical cases.
1716 isc_netaddr_fromin6(&client->destaddr,
1717 &client->pktinfo.ipi6_addr);
1718 if (IN6_IS_ADDR_LINKLOCAL(&client->pktinfo.ipi6_addr))
1719 isc_netaddr_setzone(&client->destaddr,
1720 client->pktinfo.ipi6_ifindex);
1721 result = ISC_R_SUCCESS;
1723 if (result != ISC_R_SUCCESS) {
1724 UNEXPECTED_ERROR(__FILE__, __LINE__,
1725 "failed to get request's "
1727 isc_result_totext(result));
1728 ns_client_next(client, ISC_R_SUCCESS);
1734 * Find a view that matches the client's source address.
1736 for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
1738 view = ISC_LIST_NEXT(view, link)) {
1739 if (client->message->rdclass == view->rdclass ||
1740 client->message->rdclass == dns_rdataclass_any)
1742 dns_name_t *tsig = NULL;
1744 sigresult = dns_message_rechecksig(client->message,
1746 if (sigresult == ISC_R_SUCCESS)
1747 tsig = dns_tsigkey_identity(client->message->tsigkey);
1749 if (allowed(&netaddr, tsig, view->matchclients) &&
1750 allowed(&client->destaddr, tsig,
1751 view->matchdestinations) &&
1752 !((client->message->flags & DNS_MESSAGEFLAG_RD)
1753 == 0 && view->matchrecursiveonly))
1755 dns_view_attach(view, &client->view);
1762 char classname[DNS_RDATACLASS_FORMATSIZE];
1765 * Do a dummy TSIG verification attempt so that the
1766 * response will have a TSIG if the query did, as
1767 * required by RFC2845.
1772 dns_message_resetsig(client->message);
1774 r = dns_message_getrawmessage(client->message);
1775 isc_buffer_init(&b, r->base, r->length);
1776 isc_buffer_add(&b, r->length);
1777 (void)dns_tsig_verify(&b, client->message, NULL, NULL);
1779 dns_rdataclass_format(client->message->rdclass, classname,
1781 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1782 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
1783 "no matching view in class '%s'", classname);
1784 ns_client_dumpmessage(client, "no matching view in class");
1785 ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED);
1789 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1790 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
1791 "using view '%s'", view->name);
1794 * Check for a signature. We log bad signatures regardless of
1795 * whether they ultimately cause the request to be rejected or
1796 * not. We do not log the lack of a signature unless we are
1799 client->signer = NULL;
1800 dns_name_init(&client->signername, NULL);
1801 result = dns_message_signer(client->message, &client->signername);
1802 if (result != ISC_R_NOTFOUND) {
1804 if (dns_message_gettsig(client->message, &signame) != NULL) {
1805 isc_stats_increment(ns_g_server->nsstats,
1806 dns_nsstatscounter_tsigin);
1808 isc_stats_increment(ns_g_server->nsstats,
1809 dns_nsstatscounter_sig0in);
1813 if (result == ISC_R_SUCCESS) {
1814 char namebuf[DNS_NAME_FORMATSIZE];
1815 dns_name_format(&client->signername, namebuf, sizeof(namebuf));
1816 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1817 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1818 "request has valid signature: %s", namebuf);
1819 client->signer = &client->signername;
1820 } else if (result == ISC_R_NOTFOUND) {
1821 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1822 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1823 "request is not signed");
1824 } else if (result == DNS_R_NOIDENTITY) {
1825 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1826 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1827 "request is signed by a nonauthoritative key");
1832 isc_result_t tresult;
1834 /* There is a signature, but it is bad. */
1835 isc_stats_increment(ns_g_server->nsstats,
1836 dns_nsstatscounter_invalidsig);
1838 if (dns_message_gettsig(client->message, &signame) != NULL) {
1839 char namebuf[DNS_NAME_FORMATSIZE];
1840 char cnamebuf[DNS_NAME_FORMATSIZE];
1841 dns_name_format(signame, namebuf, sizeof(namebuf));
1842 status = client->message->tsigstatus;
1843 isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1);
1844 tresult = dns_tsigrcode_totext(status, &b);
1845 INSIST(tresult == ISC_R_SUCCESS);
1846 tsigrcode[isc_buffer_usedlength(&b)] = '\0';
1847 if (client->message->tsigkey->generated) {
1848 dns_name_format(client->message->tsigkey->creator,
1849 cnamebuf, sizeof(cnamebuf));
1850 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1851 NS_LOGMODULE_CLIENT,
1853 "request has invalid signature: "
1854 "TSIG %s (%s): %s (%s)", namebuf,
1856 isc_result_totext(result),
1859 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1860 NS_LOGMODULE_CLIENT,
1862 "request has invalid signature: "
1863 "TSIG %s: %s (%s)", namebuf,
1864 isc_result_totext(result),
1868 status = client->message->sig0status;
1869 isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1);
1870 tresult = dns_tsigrcode_totext(status, &b);
1871 INSIST(tresult == ISC_R_SUCCESS);
1872 tsigrcode[isc_buffer_usedlength(&b)] = '\0';
1873 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1874 NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
1875 "request has invalid signature: %s (%s)",
1876 isc_result_totext(result), tsigrcode);
1879 * Accept update messages signed by unknown keys so that
1880 * update forwarding works transparently through slaves
1881 * that don't have all the same keys as the master.
1883 if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
1884 client->message->opcode == dns_opcode_update)) {
1885 ns_client_error(client, sigresult);
1891 * Decide whether recursive service is available to this client.
1892 * We do this here rather than in the query code so that we can
1893 * set the RA bit correctly on all kinds of responses, not just
1894 * responses to ordinary queries. Note if you can't query the
1895 * cache there is no point in setting RA.
1898 if (client->view->resolver != NULL &&
1899 client->view->recursion == ISC_TRUE &&
1900 ns_client_checkaclsilent(client, NULL,
1901 client->view->recursionacl,
1902 ISC_TRUE) == ISC_R_SUCCESS &&
1903 ns_client_checkaclsilent(client, NULL,
1904 client->view->cacheacl,
1905 ISC_TRUE) == ISC_R_SUCCESS &&
1906 ns_client_checkaclsilent(client, &client->destaddr,
1907 client->view->recursiononacl,
1908 ISC_TRUE) == ISC_R_SUCCESS &&
1909 ns_client_checkaclsilent(client, &client->destaddr,
1910 client->view->cacheonacl,
1911 ISC_TRUE) == ISC_R_SUCCESS)
1915 client->attributes |= NS_CLIENTATTR_RA;
1917 ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT,
1918 ISC_LOG_DEBUG(3), ra ? "recursion available" :
1919 "recursion not available");
1922 * Adjust maximum UDP response size for this client.
1924 if (client->udpsize > 512) {
1925 dns_peer_t *peer = NULL;
1926 isc_uint16_t udpsize = view->maxudp;
1927 (void) dns_peerlist_peerbyaddr(view->peers, &netaddr, &peer);
1929 dns_peer_getmaxudp(peer, &udpsize);
1930 if (client->udpsize > udpsize)
1931 client->udpsize = udpsize;
1935 * Dispatch the request.
1937 switch (client->message->opcode) {
1938 case dns_opcode_query:
1940 ns_query_start(client);
1942 case dns_opcode_update:
1944 ns_client_settimeout(client, 60);
1945 ns_update_start(client, sigresult);
1947 case dns_opcode_notify:
1949 ns_client_settimeout(client, 60);
1950 ns_notify_start(client);
1952 case dns_opcode_iquery:
1954 ns_client_error(client, DNS_R_NOTIMP);
1957 CTRACE("unknown opcode");
1958 ns_client_error(client, DNS_R_NOTIMP);
1966 client_timeout(isc_task_t *task, isc_event_t *event) {
1967 ns_client_t *client;
1969 REQUIRE(event != NULL);
1970 REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE ||
1971 event->ev_type == ISC_TIMEREVENT_IDLE);
1972 client = event->ev_arg;
1973 REQUIRE(NS_CLIENT_VALID(client));
1974 REQUIRE(task == client->task);
1975 REQUIRE(client->timer != NULL);
1981 isc_event_free(&event);
1983 if (client->shutdown != NULL) {
1984 (client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
1985 client->shutdown = NULL;
1986 client->shutdown_arg = NULL;
1989 if (client->newstate > NS_CLIENTSTATE_READY)
1990 client->newstate = NS_CLIENTSTATE_READY;
1991 (void)exit_check(client);
1995 get_clientmctx(ns_clientmgr_t *manager, isc_mem_t **mctxp) {
1996 isc_mem_t *clientmctx;
1997 isc_result_t result;
1999 unsigned int nextmctx;
2002 MTRACE("clientmctx");
2005 * Caller must be holding the manager lock.
2007 if (ns_g_clienttest) {
2008 result = isc_mem_create(0, 0, mctxp);
2009 if (result == ISC_R_SUCCESS)
2010 isc_mem_setname(*mctxp, "client", NULL);
2014 nextmctx = manager->nextmctx++;
2015 if (manager->nextmctx == NMCTXS)
2016 manager->nextmctx = 0;
2018 INSIST(nextmctx < NMCTXS);
2020 clientmctx = manager->mctxpool[nextmctx];
2021 if (clientmctx == NULL) {
2022 result = isc_mem_create(0, 0, &clientmctx);
2023 if (result != ISC_R_SUCCESS)
2025 isc_mem_setname(clientmctx, "client", NULL);
2027 manager->mctxpool[nextmctx] = clientmctx;
2030 clientmctx = manager->mctx;
2033 isc_mem_attach(clientmctx, mctxp);
2035 return (ISC_R_SUCCESS);
2039 client_create(ns_clientmgr_t *manager, ns_client_t **clientp) {
2040 ns_client_t *client;
2041 isc_result_t result;
2042 isc_mem_t *mctx = NULL;
2045 * Caller must be holding the manager lock.
2047 * Note: creating a client does not add the client to the
2048 * manager's client list or set the client's manager pointer.
2049 * The caller is responsible for that.
2052 REQUIRE(clientp != NULL && *clientp == NULL);
2054 result = get_clientmctx(manager, &mctx);
2055 if (result != ISC_R_SUCCESS)
2058 client = isc_mem_get(mctx, sizeof(*client));
2059 if (client == NULL) {
2060 isc_mem_detach(&mctx);
2061 return (ISC_R_NOMEMORY);
2063 client->mctx = mctx;
2065 client->task = NULL;
2066 result = isc_task_create(manager->taskmgr, 0, &client->task);
2067 if (result != ISC_R_SUCCESS)
2068 goto cleanup_client;
2069 isc_task_setname(client->task, "client", client);
2071 client->timer = NULL;
2072 result = isc_timer_create(manager->timermgr, isc_timertype_inactive,
2073 NULL, NULL, client->task, client_timeout,
2074 client, &client->timer);
2075 if (result != ISC_R_SUCCESS)
2077 client->timerset = ISC_FALSE;
2079 client->message = NULL;
2080 result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE,
2082 if (result != ISC_R_SUCCESS)
2085 /* XXXRTH Hardwired constants */
2087 client->sendevent = (isc_socketevent_t *)
2088 isc_event_allocate(client->mctx, client,
2089 ISC_SOCKEVENT_SENDDONE,
2090 client_senddone, client,
2091 sizeof(isc_socketevent_t));
2092 if (client->sendevent == NULL) {
2093 result = ISC_R_NOMEMORY;
2094 goto cleanup_message;
2097 client->recvbuf = isc_mem_get(client->mctx, RECV_BUFFER_SIZE);
2098 if (client->recvbuf == NULL) {
2099 result = ISC_R_NOMEMORY;
2100 goto cleanup_sendevent;
2103 client->recvevent = (isc_socketevent_t *)
2104 isc_event_allocate(client->mctx, client,
2105 ISC_SOCKEVENT_RECVDONE,
2106 client_request, client,
2107 sizeof(isc_socketevent_t));
2108 if (client->recvevent == NULL) {
2109 result = ISC_R_NOMEMORY;
2110 goto cleanup_recvbuf;
2113 client->magic = NS_CLIENT_MAGIC;
2114 client->manager = NULL;
2115 client->state = NS_CLIENTSTATE_INACTIVE;
2116 client->newstate = NS_CLIENTSTATE_MAX;
2117 client->naccepts = 0;
2121 client->nupdates = 0;
2123 client->references = 0;
2124 client->attributes = 0;
2125 client->view = NULL;
2126 client->dispatch = NULL;
2127 client->udpsocket = NULL;
2128 client->tcplistener = NULL;
2129 client->tcpsocket = NULL;
2130 client->tcpmsg_valid = ISC_FALSE;
2131 client->tcpbuf = NULL;
2133 client->udpsize = 512;
2134 client->extflags = 0;
2135 client->ednsversion = -1;
2136 client->next = NULL;
2137 client->shutdown = NULL;
2138 client->shutdown_arg = NULL;
2139 client->signer = NULL;
2140 dns_name_init(&client->signername, NULL);
2141 client->mortal = ISC_FALSE;
2142 client->tcpquota = NULL;
2143 client->recursionquota = NULL;
2144 client->interface = NULL;
2145 client->peeraddr_valid = ISC_FALSE;
2146 #ifdef ALLOW_FILTER_AAAA_ON_V4
2147 client->filter_aaaa = dns_v4_aaaa_ok;
2149 client->needshutdown = ns_g_clienttest;
2151 ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
2152 NS_EVENT_CLIENTCONTROL, client_start, client, client,
2155 * Initialize FORMERR cache to sentinel value that will not match
2156 * any actual FORMERR response.
2158 isc_sockaddr_any(&client->formerrcache.addr);
2159 client->formerrcache.time = 0;
2160 client->formerrcache.id = 0;
2161 ISC_LINK_INIT(client, link);
2162 ISC_LINK_INIT(client, rlink);
2163 ISC_QLINK_INIT(client, ilink);
2166 * We call the init routines for the various kinds of client here,
2167 * after we have created an otherwise valid client, because some
2168 * of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
2170 result = ns_query_init(client);
2171 if (result != ISC_R_SUCCESS)
2172 goto cleanup_recvevent;
2174 result = isc_task_onshutdown(client->task, client_shutdown, client);
2175 if (result != ISC_R_SUCCESS)
2182 return (ISC_R_SUCCESS);
2185 ns_query_free(client);
2188 isc_event_free((isc_event_t **)&client->recvevent);
2191 isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
2194 isc_event_free((isc_event_t **)&client->sendevent);
2199 dns_message_destroy(&client->message);
2202 isc_timer_detach(&client->timer);
2205 isc_task_detach(&client->task);
2208 isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
2214 client_read(ns_client_t *client) {
2215 isc_result_t result;
2219 result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
2220 client_request, client);
2221 if (result != ISC_R_SUCCESS)
2225 * Set a timeout to limit the amount of time we will wait
2226 * for a request on this TCP connection.
2228 ns_client_settimeout(client, 30);
2230 client->state = client->newstate = NS_CLIENTSTATE_READING;
2231 INSIST(client->nreads == 0);
2232 INSIST(client->recursionquota == NULL);
2237 ns_client_next(client, result);
2241 client_newconn(isc_task_t *task, isc_event_t *event) {
2242 ns_client_t *client = event->ev_arg;
2243 isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
2244 isc_result_t result;
2246 REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
2247 REQUIRE(NS_CLIENT_VALID(client));
2248 REQUIRE(client->task == task);
2252 INSIST(client->state == NS_CLIENTSTATE_READY);
2254 INSIST(client->naccepts == 1);
2257 LOCK(&client->interface->lock);
2258 INSIST(client->interface->ntcpcurrent > 0);
2259 client->interface->ntcpcurrent--;
2260 UNLOCK(&client->interface->lock);
2263 * We must take ownership of the new socket before the exit
2264 * check to make sure it gets destroyed if we decide to exit.
2266 if (nevent->result == ISC_R_SUCCESS) {
2267 client->tcpsocket = nevent->newsocket;
2268 isc_socket_setname(client->tcpsocket, "client-tcp", NULL);
2269 client->state = NS_CLIENTSTATE_READING;
2270 INSIST(client->recursionquota == NULL);
2272 (void)isc_socket_getpeername(client->tcpsocket,
2274 client->peeraddr_valid = ISC_TRUE;
2275 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2276 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
2277 "new TCP connection");
2280 * XXXRTH What should we do? We're trying to accept but
2281 * it didn't work. If we just give up, then TCP
2282 * service may eventually stop.
2284 * For now, we just go idle.
2286 * Going idle is probably the right thing if the
2289 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2290 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
2291 "accept failed: %s",
2292 isc_result_totext(nevent->result));
2295 if (exit_check(client))
2298 if (nevent->result == ISC_R_SUCCESS) {
2300 isc_netaddr_t netaddr;
2302 isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
2304 if (ns_g_server->blackholeacl != NULL &&
2305 dns_acl_match(&netaddr, NULL,
2306 ns_g_server->blackholeacl,
2307 &ns_g_server->aclenv,
2308 &match, NULL) == ISC_R_SUCCESS &&
2311 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2312 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
2313 "blackholed connection attempt");
2314 client->newstate = NS_CLIENTSTATE_READY;
2315 (void)exit_check(client);
2319 INSIST(client->tcpmsg_valid == ISC_FALSE);
2320 dns_tcpmsg_init(client->mctx, client->tcpsocket,
2322 client->tcpmsg_valid = ISC_TRUE;
2325 * Let a new client take our place immediately, before
2326 * we wait for a request packet. If we don't,
2327 * telnetting to port 53 (once per CPU) will
2328 * deny service to legitimate TCP clients.
2330 result = isc_quota_attach(&ns_g_server->tcpquota,
2332 if (result == ISC_R_SUCCESS)
2333 result = ns_client_replace(client);
2334 if (result != ISC_R_SUCCESS) {
2335 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2336 NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
2337 "no more TCP clients: %s",
2338 isc_result_totext(result));
2341 client_read(client);
2345 isc_event_free(&event);
2349 client_accept(ns_client_t *client) {
2350 isc_result_t result;
2354 result = isc_socket_accept(client->tcplistener, client->task,
2355 client_newconn, client);
2356 if (result != ISC_R_SUCCESS) {
2357 UNEXPECTED_ERROR(__FILE__, __LINE__,
2358 "isc_socket_accept() failed: %s",
2359 isc_result_totext(result));
2361 * XXXRTH What should we do? We're trying to accept but
2362 * it didn't work. If we just give up, then TCP
2363 * service may eventually stop.
2365 * For now, we just go idle.
2369 INSIST(client->naccepts == 0);
2371 LOCK(&client->interface->lock);
2372 client->interface->ntcpcurrent++;
2373 UNLOCK(&client->interface->lock);
2377 client_udprecv(ns_client_t *client) {
2378 isc_result_t result;
2383 r.base = client->recvbuf;
2384 r.length = RECV_BUFFER_SIZE;
2385 result = isc_socket_recv2(client->udpsocket, &r, 1,
2386 client->task, client->recvevent, 0);
2387 if (result != ISC_R_SUCCESS) {
2388 UNEXPECTED_ERROR(__FILE__, __LINE__,
2389 "isc_socket_recv2() failed: %s",
2390 isc_result_totext(result));
2392 * This cannot happen in the current implementation, since
2393 * isc_socket_recv2() cannot fail if flags == 0.
2395 * If this does fail, we just go idle.
2399 INSIST(client->nrecvs == 0);
2404 ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
2405 REQUIRE(NS_CLIENT_VALID(source));
2406 REQUIRE(targetp != NULL && *targetp == NULL);
2408 source->references++;
2409 ns_client_log(source, NS_LOGCATEGORY_CLIENT,
2410 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
2411 "ns_client_attach: ref = %d", source->references);
2416 ns_client_detach(ns_client_t **clientp) {
2417 ns_client_t *client = *clientp;
2419 client->references--;
2420 INSIST(client->references >= 0);
2422 ns_client_log(client, NS_LOGCATEGORY_CLIENT,
2423 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
2424 "ns_client_detach: ref = %d", client->references);
2425 (void)exit_check(client);
2429 ns_client_shuttingdown(ns_client_t *client) {
2430 return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
2434 ns_client_replace(ns_client_t *client) {
2435 isc_result_t result;
2439 REQUIRE(client != NULL);
2440 REQUIRE(client->manager != NULL);
2442 result = get_client(client->manager, client->interface,
2443 client->dispatch, TCP_CLIENT(client));
2444 if (result != ISC_R_SUCCESS)
2448 * The responsibility for listening for new requests is hereby
2449 * transferred to the new client. Therefore, the old client
2450 * should refrain from listening for any more requests.
2452 client->mortal = ISC_TRUE;
2454 return (ISC_R_SUCCESS);
2462 clientmgr_destroy(ns_clientmgr_t *manager) {
2467 REQUIRE(ISC_LIST_EMPTY(manager->clients));
2469 MTRACE("clientmgr_destroy");
2472 for (i = 0; i < NMCTXS; i++) {
2473 if (manager->mctxpool[i] != NULL)
2474 isc_mem_detach(&manager->mctxpool[i]);
2478 ISC_QUEUE_DESTROY(manager->inactive);
2479 DESTROYLOCK(&manager->lock);
2480 DESTROYLOCK(&manager->listlock);
2481 DESTROYLOCK(&manager->reclock);
2483 isc_mem_put(manager->mctx, manager, sizeof(*manager));
2487 ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
2488 isc_timermgr_t *timermgr, ns_clientmgr_t **managerp)
2490 ns_clientmgr_t *manager;
2491 isc_result_t result;
2496 manager = isc_mem_get(mctx, sizeof(*manager));
2497 if (manager == NULL)
2498 return (ISC_R_NOMEMORY);
2500 result = isc_mutex_init(&manager->lock);
2501 if (result != ISC_R_SUCCESS)
2502 goto cleanup_manager;
2504 result = isc_mutex_init(&manager->listlock);
2505 if (result != ISC_R_SUCCESS)
2508 result = isc_mutex_init(&manager->reclock);
2509 if (result != ISC_R_SUCCESS)
2510 goto cleanup_listlock;
2512 manager->mctx = mctx;
2513 manager->taskmgr = taskmgr;
2514 manager->timermgr = timermgr;
2515 manager->exiting = ISC_FALSE;
2516 ISC_LIST_INIT(manager->clients);
2517 ISC_LIST_INIT(manager->recursing);
2518 ISC_QUEUE_INIT(manager->inactive, ilink);
2520 manager->nextmctx = 0;
2521 for (i = 0; i < NMCTXS; i++)
2522 manager->mctxpool[i] = NULL; /* will be created on-demand */
2524 manager->magic = MANAGER_MAGIC;
2528 *managerp = manager;
2530 return (ISC_R_SUCCESS);
2533 (void) isc_mutex_destroy(&manager->listlock);
2536 (void) isc_mutex_destroy(&manager->lock);
2539 isc_mem_put(manager->mctx, manager, sizeof(*manager));
2545 ns_clientmgr_destroy(ns_clientmgr_t **managerp) {
2546 isc_result_t result;
2547 ns_clientmgr_t *manager;
2548 ns_client_t *client;
2549 isc_boolean_t need_destroy = ISC_FALSE, unlock = ISC_FALSE;
2551 REQUIRE(managerp != NULL);
2552 manager = *managerp;
2553 REQUIRE(VALID_MANAGER(manager));
2558 * Check for success because we may already be task-exclusive
2559 * at this point. Only if we succeed at obtaining an exclusive
2560 * lock now will we need to relinquish it later.
2562 result = isc_task_beginexclusive(ns_g_server->task);
2563 if (result == ISC_R_SUCCESS)
2566 manager->exiting = ISC_TRUE;
2568 for (client = ISC_LIST_HEAD(manager->clients);
2570 client = ISC_LIST_NEXT(client, link))
2571 isc_task_shutdown(client->task);
2573 if (ISC_LIST_EMPTY(manager->clients))
2574 need_destroy = ISC_TRUE;
2577 isc_task_endexclusive(ns_g_server->task);
2580 clientmgr_destroy(manager);
2586 get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
2587 dns_dispatch_t *disp, isc_boolean_t tcp)
2589 isc_result_t result = ISC_R_SUCCESS;
2591 ns_client_t *client;
2592 MTRACE("get client");
2594 REQUIRE(manager != NULL);
2596 if (manager->exiting)
2597 return (ISC_R_SHUTTINGDOWN);
2600 * Allocate a client. First try to get a recycled one;
2601 * if that fails, make a new one.
2604 if (!ns_g_clienttest)
2605 ISC_QUEUE_POP(manager->inactive, ilink, client);
2610 MTRACE("create new");
2612 LOCK(&manager->lock);
2613 result = client_create(manager, &client);
2614 UNLOCK(&manager->lock);
2615 if (result != ISC_R_SUCCESS)
2618 LOCK(&manager->listlock);
2619 ISC_LIST_APPEND(manager->clients, client, link);
2620 UNLOCK(&manager->listlock);
2623 client->manager = manager;
2624 ns_interface_attach(ifp, &client->interface);
2625 client->state = NS_CLIENTSTATE_READY;
2626 INSIST(client->recursionquota == NULL);
2629 client->attributes |= NS_CLIENTATTR_TCP;
2630 isc_socket_attach(ifp->tcpsocket,
2631 &client->tcplistener);
2635 dns_dispatch_attach(disp, &client->dispatch);
2636 sock = dns_dispatch_getsocket(client->dispatch);
2637 isc_socket_attach(sock, &client->udpsocket);
2640 INSIST(client->nctls == 0);
2642 ev = &client->ctlevent;
2643 isc_task_send(client->task, &ev);
2645 return (ISC_R_SUCCESS);
2649 ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
2650 ns_interface_t *ifp, isc_boolean_t tcp)
2652 isc_result_t result = ISC_R_SUCCESS;
2655 REQUIRE(VALID_MANAGER(manager));
2658 MTRACE("createclients");
2660 for (disp = 0; disp < n; disp++) {
2661 result = get_client(manager, ifp, ifp->udpdispatch[disp], tcp);
2662 if (result != ISC_R_SUCCESS)
2670 ns_client_getsockaddr(ns_client_t *client) {
2671 return (&client->peeraddr);
2675 ns_client_checkaclsilent(ns_client_t *client, isc_netaddr_t *netaddr,
2676 dns_acl_t *acl, isc_boolean_t default_allow)
2678 isc_result_t result;
2679 isc_netaddr_t tmpnetaddr;
2689 if (netaddr == NULL) {
2690 isc_netaddr_fromsockaddr(&tmpnetaddr, &client->peeraddr);
2691 netaddr = &tmpnetaddr;
2694 result = dns_acl_match(netaddr, client->signer, acl,
2695 &ns_g_server->aclenv, &match, NULL);
2697 if (result != ISC_R_SUCCESS)
2698 goto deny; /* Internal error, already logged. */
2701 goto deny; /* Negative match or no match. */
2704 return (ISC_R_SUCCESS);
2707 return (DNS_R_REFUSED);
2711 ns_client_checkacl(ns_client_t *client, isc_sockaddr_t *sockaddr,
2712 const char *opname, dns_acl_t *acl,
2713 isc_boolean_t default_allow, int log_level)
2715 isc_result_t result;
2716 isc_netaddr_t netaddr;
2718 if (sockaddr != NULL)
2719 isc_netaddr_fromsockaddr(&netaddr, sockaddr);
2721 result = ns_client_checkaclsilent(client, sockaddr ? &netaddr : NULL,
2722 acl, default_allow);
2724 if (result == ISC_R_SUCCESS)
2725 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2726 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
2727 "%s approved", opname);
2729 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2730 NS_LOGMODULE_CLIENT,
2731 log_level, "%s denied", opname);
2736 ns_client_name(ns_client_t *client, char *peerbuf, size_t len) {
2737 if (client->peeraddr_valid)
2738 isc_sockaddr_format(&client->peeraddr, peerbuf,
2741 snprintf(peerbuf, len, "@%p", client);
2745 ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
2746 isc_logmodule_t *module, int level, const char *fmt, va_list ap)
2749 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
2750 char signerbuf[DNS_NAME_FORMATSIZE], qnamebuf[DNS_NAME_FORMATSIZE];
2751 const char *viewname = "";
2752 const char *sep1 = "", *sep2 = "", *sep3 = "", *sep4 = "";
2753 const char *signer = "", *qname = "";
2754 dns_name_t *q = NULL;
2756 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
2758 ns_client_name(client, peerbuf, sizeof(peerbuf));
2760 if (client->signer != NULL) {
2761 dns_name_format(client->signer, signerbuf, sizeof(signerbuf));
2766 q = client->query.origqname != NULL
2767 ? client->query.origqname : client->query.qname;
2769 dns_name_format(q, qnamebuf, sizeof(qnamebuf));
2775 if (client->view != NULL && strcmp(client->view->name, "_bind") != 0 &&
2776 strcmp(client->view->name, "_default") != 0) {
2778 viewname = client->view->name;
2781 isc_log_write(ns_g_lctx, category, module, level,
2782 "client %s%s%s%s%s%s%s%s: %s",
2783 peerbuf, sep1, signer, sep2, qname, sep3,
2784 sep4, viewname, msgbuf);
2788 ns_client_log(ns_client_t *client, isc_logcategory_t *category,
2789 isc_logmodule_t *module, int level, const char *fmt, ...)
2793 if (! isc_log_wouldlog(ns_g_lctx, level))
2797 ns_client_logv(client, category, module, level, fmt, ap);
2802 ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdatatype_t type,
2803 dns_rdataclass_t rdclass, char *buf, size_t len)
2805 char namebuf[DNS_NAME_FORMATSIZE];
2806 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2807 char classbuf[DNS_RDATACLASS_FORMATSIZE];
2809 dns_name_format(name, namebuf, sizeof(namebuf));
2810 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
2811 dns_rdataclass_format(rdclass, classbuf, sizeof(classbuf));
2812 (void)snprintf(buf, len, "%s '%s/%s/%s'", msg, namebuf, typebuf,
2817 ns_client_dumpmessage(ns_client_t *client, const char *reason) {
2818 isc_buffer_t buffer;
2821 isc_result_t result;
2824 * Note that these are multiline debug messages. We want a newline
2825 * to appear in the log after each message.
2829 buf = isc_mem_get(client->mctx, len);
2832 isc_buffer_init(&buffer, buf, len);
2833 result = dns_message_totext(client->message,
2834 &dns_master_style_debug,
2836 if (result == ISC_R_NOSPACE) {
2837 isc_mem_put(client->mctx, buf, len);
2839 } else if (result == ISC_R_SUCCESS)
2840 ns_client_log(client, NS_LOGCATEGORY_UNMATCHED,
2841 NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
2843 (int)isc_buffer_usedlength(&buffer),
2845 } while (result == ISC_R_NOSPACE);
2848 isc_mem_put(client->mctx, buf, len);
2852 ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager) {
2853 ns_client_t *client;
2854 char namebuf[DNS_NAME_FORMATSIZE];
2855 char original[DNS_NAME_FORMATSIZE];
2856 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
2857 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2858 char classbuf[DNS_RDATACLASS_FORMATSIZE];
2861 const char *origfor;
2862 dns_rdataset_t *rdataset;
2864 REQUIRE(VALID_MANAGER(manager));
2866 LOCK(&manager->reclock);
2867 client = ISC_LIST_HEAD(manager->recursing);
2868 while (client != NULL) {
2869 INSIST(client->state == NS_CLIENTSTATE_RECURSING);
2871 ns_client_name(client, peerbuf, sizeof(peerbuf));
2872 if (client->view != NULL &&
2873 strcmp(client->view->name, "_bind") != 0 &&
2874 strcmp(client->view->name, "_default") != 0) {
2875 name = client->view->name;
2882 LOCK(&client->query.fetchlock);
2883 INSIST(client->query.qname != NULL);
2884 dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
2885 if (client->query.qname != client->query.origqname &&
2886 client->query.origqname != NULL) {
2888 dns_name_format(client->query.origqname, original,
2894 rdataset = ISC_LIST_HEAD(client->query.qname->list);
2895 if (rdataset == NULL && client->query.origqname != NULL)
2896 rdataset = ISC_LIST_HEAD(client->query.origqname->list);
2897 if (rdataset != NULL) {
2898 dns_rdatatype_format(rdataset->type, typebuf,
2900 dns_rdataclass_format(rdataset->rdclass, classbuf,
2903 strcpy(typebuf, "-");
2904 strcpy(classbuf, "-");
2906 UNLOCK(&client->query.fetchlock);
2907 fprintf(f, "; client %s%s%s: id %u '%s/%s/%s'%s%s "
2908 "requesttime %d\n", peerbuf, sep, name,
2909 client->message->id, namebuf, typebuf, classbuf,
2910 origfor, original, client->requesttime);
2911 client = ISC_LIST_NEXT(client, rlink);
2913 UNLOCK(&manager->reclock);
2917 ns_client_qnamereplace(ns_client_t *client, dns_name_t *name) {
2918 LOCK(&client->query.fetchlock);
2919 if (client->query.restarts > 0) {
2921 * client->query.qname was dynamically allocated.
2923 dns_message_puttempname(client->message,
2924 &client->query.qname);
2926 client->query.qname = name;
2927 UNLOCK(&client->query.fetchlock);
2931 ns_client_sourceip(dns_clientinfo_t *ci, isc_sockaddr_t **addrp) {
2932 ns_client_t *client = (ns_client_t *) ci->data;
2934 REQUIRE(NS_CLIENT_VALID(client));
2935 REQUIRE(addrp != NULL);
2937 *addrp = &client->peeraddr;
2938 return (ISC_R_SUCCESS);