client.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 1999-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#include <isc/formatcheck.h>
#include <isc/platform.h>
#include <dns/badcache.h>
#include <dns/dispatch.h>
#include <dns/rdataclass.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/resolver.h>
#include <named/interfacemgr.h>
/***
*** Client
***/
/*! \file
* Client Routines
*
* Important note!
*
* All client state changes, other than that from idle to listening, occur
* as a result of events. This guarantees serialization and avoids the
* need for locking.
*
* If a routine is ever created that allows someone other than the client's
* task to change the client, then the client will have to be locked.
*/
#define NS_CLIENT_TRACE
#ifdef NS_CLIENT_TRACE
ISC_LOG_DEBUG(3), \
"%s", (m))
ISC_LOG_DEBUG(3), \
"clientmgr @%p: %s", manager, (m))
#else
#define CTRACE(m) ((void)(m))
#define MTRACE(m) ((void)(m))
#endif
#define SEND_BUFFER_SIZE 4096
#define RECV_BUFFER_SIZE 4096
#ifdef ISC_PLATFORM_USETHREADS
#define NMCTXS 100
/*%<
* Number of 'mctx pools' for clients. (Should this be configurable?)
* When enabling threads, we use a pool of memory contexts shared by
* client objects, since concurrent access to a shared context would cause
* heavy contentions. The above constant is expected to be enough for
* completely avoiding contentions among threads for an authoritative-only
* server.
*/
#else
#define NMCTXS 0
/*%<
* If named with built without thread, simply share manager's context. Using
* a separate context in this case would simply waste memory.
*/
#endif
/*% nameserver client manager structure */
struct ns_clientmgr {
/* Unlocked. */
unsigned int magic;
/* The queue object has its own locks */
/* Lock covers manager state. */
/* Lock covers the clients list */
/* Lock covers the recursing list */
#if NMCTXS > 0
/*%< mctx pool for clients. */
unsigned int nextmctx;
#endif
};
/*!
* Client object states. Ordering is significant: higher-numbered
* states are generally "more active", meaning that the client can
* have more dynamically allocated data, outstanding events, etc.
* In the list below, any such properties listed for state N
* also apply to any state > N.
*
* To force the client into a less active state, set client->newstate
* to that state and call exit_check(). This will cause any
* activities defined for higher-numbered states to be aborted.
*/
#define NS_CLIENTSTATE_FREED 0
/*%<
* The client object no longer exists.
*/
#define NS_CLIENTSTATE_INACTIVE 1
/*%<
* The client object exists and has a task and timer.
* Its "query" struct and sendbuf are initialized.
* It is on the client manager's list of inactive clients.
* It has a message and OPT, both in the reset state.
*/
#define NS_CLIENTSTATE_READY 2
/*%<
* The client object is either a TCP or a UDP one, and
* it is associated with a network interface. It is on the
* client manager's list of active clients.
*
* If it is a TCP client object, it has a TCP listener socket
* and an outstanding TCP listen request.
*
* If it is a UDP client object, it has a UDP listener socket
* and an outstanding UDP receive request.
*/
#define NS_CLIENTSTATE_READING 3
/*%<
* The client object is a TCP client object that has received
* a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
* outstanding TCP read request. This state is not used for
* UDP client objects.
*/
#define NS_CLIENTSTATE_WORKING 4
/*%<
* The client object has received a request and is working
* on it. It has a view, and it may have any of a non-reset OPT,
* recursion quota, and an outstanding write request.
*/
#define NS_CLIENTSTATE_RECURSING 5
/*%<
* The client object is recursing. It will be on the 'recursing'
* list.
*/
#define NS_CLIENTSTATE_MAX 9
/*%<
* Sentinel value used to indicate "no state". When client->newstate
* has this value, we are not attempting to exit the current state.
* Must be greater than any valid state.
*/
/*
* Enable ns_client_dropport() by default.
*/
#ifndef NS_CLIENT_DROPPORT
#define NS_CLIENT_DROPPORT 1
#endif
unsigned int ns_client_requests;
isc_socket_t *sock);
static inline isc_boolean_t
void
}
void
} else
}
void
if (result != ISC_R_SUCCESS) {
"setting timeout: %s",
/* Continue anyway. */
}
}
/*%
* Check for a deactivation or shutdown request and take appropriate
* action. Returns ISC_TRUE if either is in progress; in this case
* the caller must no longer use the client object as it may have been
* freed.
*/
static isc_boolean_t
return (ISC_FALSE); /* Business as usual. */
/*
* We need to detach from the view early when shutting down
* the server to break the following vicious circle:
*
* - The resolver will not shut down until the view refcount is zero
* - The view refcount does not go to zero until all clients detach
* - The client does not detach from the view until references is zero
* - references does not go to zero until the resolver has shut down
*
* Keep the view attached until any outstanding updates complete.
*/
{
/*
* Let the update processing complete.
*/
return (ISC_TRUE);
/*
* We are trying to abort request processing.
*/
if (TCP_CLIENT(client))
else
}
client->references == 0))
{
/*
* Still waiting for I/O cancel completion.
* or lingering references.
*/
return (ISC_TRUE);
}
/*
* I/O cancel is complete. Burn down all state
* related to the current request. Ensure that
* the client is no longer on the recursing list.
*
* We need to check whether the client is still linked,
* because it may already have been removed from the
* recursing list by ns_client_killoldestquery()
*/
}
return (ISC_TRUE); /* We're done. */
} else
return (ISC_FALSE);
}
}
/*
* We are trying to abort the current TCP connection,
* if any.
*/
/* Still waiting for read cancel completion. */
return (ISC_TRUE);
}
if (client->tcpmsg_valid) {
}
CTRACE("closetcp");
}
}
/*
* Now the client is ready to accept a new TCP connection
* or UDP request, but we may have enough clients doing
* that already. Check whether this client needs to remain
* active and force it to go inactive if not.
*
* UDP clients go inactive at this point, but TCP clients
* may remain active if we have fewer active TCP client
* objects than desired due to an earlier quota exhaustion.
*/
}
/*
* We don't need the client; send it to the inactive
* queue for recycling.
*/
}
if (TCP_CLIENT(client)) {
} else
return (ISC_TRUE);
}
}
/*
* We are trying to enter the inactive state.
*/
/* Still waiting for accept cancel completion. */
return (ISC_TRUE);
/* Accept cancel is complete. */
/* Still waiting for recv cancel completion. */
return (ISC_TRUE);
/* Still waiting for control event to be delivered */
return (ISC_TRUE);
/* Deactivate the client. */
client->attributes = 0;
/*
* Put the client on the inactive list. If we are aiming for
* the "freed" state, it will be removed from the inactive
* list shortly, and we need to keep the manager locked until
* that has been done, lest the manager decide to reactivate
* the dying client inbetween.
*/
ilink);
if (client->needshutdown)
return (ISC_TRUE);
}
}
/*
* We are trying to free the client.
*
* When "shuttingdown" is true, either the task has received
* its shutdown event or no shutdown event has ever been
* set up. Thus, we have no outstanding shutdown
* event at this point.
*/
}
}
/*
* Detaching the task must be done after unlinking from
* the manager's lists because the manager accesses
* client->task.
*/
CTRACE("free");
/*
* Check that there are no other external references to
* the memory context.
*/
INSIST(0);
}
/*
* Destroy the fetchlock mutex that was created in
* ns_query_init().
*/
}
return (ISC_TRUE);
}
/*%
* The client's task has received the client's control event
* as part of the startup process.
*/
static void
if (exit_check(client))
return;
if (TCP_CLIENT(client)) {
} else {
}
} else {
}
}
/*%
* The client's task has received a shutdown event.
*/
static void
CTRACE("shutdown");
}
(void)exit_check(client);
}
static void
CTRACE("endrequest");
}
#ifdef ENABLE_AFL
if (ns_g_fuzz_type == ns_fuzz_resolver) {
}
#endif
}
}
}
/*
* Clear all client attributes that are specific to
* the request; that's all except the TCP flag.
*/
#ifdef ENABLE_AFL
if (ns_g_fuzz_type == ns_fuzz_client ||
}
#endif /* ENABLE_AFL */
}
void
int newstate;
CTRACE("next");
if (result != ISC_R_SUCCESS)
/*
* An error processing a TCP request may have left
* the connection out of sync. To be safe, we always
* sever the connection when result != ISC_R_SUCCESS.
*/
else
(void)exit_check(client);
}
static void
CTRACE("senddone");
"error sending response: %s",
}
}
/*%
* We only want to fail with ISC_R_NOSPACE when called from
* ns_client_sendraw() and not when called from ns_client_send(),
* tcpbuffer is NULL when called from ns_client_sendraw() and
* length != 0. tcpbuffer != NULL when called from ns_client_send()
* and length == 0.
*/
static isc_result_t
{
unsigned char *data;
if (TCP_CLIENT(client)) {
goto done;
}
goto done;
}
} else {
}
} else {
else
bufsize = 512;
} else
if (bufsize > SEND_BUFFER_SIZE)
goto done;
}
}
done:
return (result);
}
static isc_result_t
struct in6_pktinfo *pktinfo;
isc_region_t r;
int match;
unsigned int sockflags = ISC_SOCKFLAG_IMMEDIATE;
if (TCP_CLIENT(client)) {
} else {
match > 0)
return (DNS_R_BLACKHOLED);
}
else
if (dispdscp != -1)
}
} else {
}
isc_buffer_usedregion(buffer, &r);
CTRACE("sendto");
if (result == ISC_R_SUCCESS)
}
return (result);
}
void
unsigned char *data;
isc_region_t r;
unsigned char sendbuf[SEND_BUFFER_SIZE];
CTRACE("sendraw");
goto done;
}
if (result != ISC_R_SUCCESS)
goto done;
/*
* Copy message to buffer and fixup id.
*/
isc_buffer_availableregion(&buffer, &r);
if (result != ISC_R_SUCCESS)
goto done;
if (result == ISC_R_SUCCESS)
return;
done:
}
}
static void
unsigned char *data;
isc_region_t r;
unsigned char sendbuf[SEND_BUFFER_SIZE];
unsigned int render_opts;
unsigned int preferred_glue;
#ifdef HAVE_DNSTAP
unsigned char zone[DNS_NAME_MAXWIRE];
#endif /* HAVE_DNSTAP */
CTRACE("send");
render_opts = 0;
else
preferred_glue = 0;
}
if (preferred_glue == 0) {
else
}
#ifdef ALLOW_FILTER_AAAA
/*
* filter-aaaa-on-v4 yes or break-dnssec option to suppress
* AAAA records.
*
* We already know that request came via IPv4,
* that we have both AAAA and A records,
* and that we either have no signatures that the client wants
* or we are supposed to break DNSSEC.
*
* Override preferred glue if necessary.
*/
}
#endif
/*
* Create an OPT for our reply.
*/
if (result != ISC_R_SUCCESS)
goto done;
}
/*
* XXXRTH The following doesn't deal with TCP buffer resizing.
*/
if (result != ISC_R_SUCCESS)
goto done;
if (result != ISC_R_SUCCESS)
goto done;
{
}
}
}
if (result != ISC_R_SUCCESS)
goto done;
if (result != ISC_R_SUCCESS)
goto done;
}
DNS_SECTION_QUESTION, 0);
if (result == ISC_R_NOSPACE) {
goto renderend;
}
if (result != ISC_R_SUCCESS)
goto done;
/*
* Stop after the question if TC was set for rate limiting.
*/
goto renderend;
if (result == ISC_R_NOSPACE) {
goto renderend;
}
if (result != ISC_R_SUCCESS)
goto done;
if (result == ISC_R_NOSPACE) {
goto renderend;
}
if (result != ISC_R_SUCCESS)
goto done;
goto done;
if (result != ISC_R_SUCCESS)
goto done;
#ifdef HAVE_DNSTAP
{
isc_buffer_t b;
dns_name_t *zo =
if (result == ISC_R_SUCCESS)
isc_buffer_usedregion(&b, &zr);
}
else
#endif /* HAVE_DNSTAP */
if (cleanup_cctx) {
}
if (TCP_CLIENT(client)) {
isc_buffer_usedregion(&buffer, &r);
/* don't count the 2-octet length header */
#ifdef HAVE_DNSTAP
}
#endif /* HAVE_DNSTAP */
case AF_INET:
break;
case AF_INET6:
break;
default:
INSIST(0);
break;
}
} else {
#ifdef HAVE_DNSTAP
}
#endif /* HAVE_DNSTAP */
case AF_INET:
break;
case AF_INET6:
break;
default:
INSIST(0);
break;
}
}
/* update statistics (XXXJT: is it okay to access message->xxxkey?) */
if (opt_included) {
}
}
}
if (result == ISC_R_SUCCESS)
return;
done:
}
if (cleanup_cctx)
}
/*
* Completes the sending of a delayed client response.
*/
static void
CTRACE("client_delay");
}
void
/*
* Delay the response by ns_g_delay ms.
*/
if (ns_g_delay != 0) {
/*
* Replace ourselves if we have not already been replaced.
*/
if (result != ISC_R_SUCCESS)
goto nodelay;
}
if (ns_g_delay >= 1000)
else
if (result == ISC_R_SUCCESS)
return;
}
}
#define DROPPORT_NO 0
#define DROPPORT_REQUEST 1
#define DROPPORT_RESPONSE 2
/*%
* ns_client_dropport determines if certain requests / responses
* should be dropped based on the port number.
*
* Returns:
* \li 0: Don't drop.
* \li 1: Drop request.
* \li 2: Drop (error) response.
*/
static int
switch (port) {
case 7: /* echo */
case 13: /* daytime */
case 19: /* chargen */
case 37: /* time */
return (DROPPORT_REQUEST);
case 464: /* kpasswd */
return (DROPPORT_RESPONSE);
}
return (DROPPORT_NO);
}
#endif
void
CTRACE("error");
/*
* Don't send FORMERR to ports on the drop port list.
*/
if (rcode == dns_rcode_formerr &&
DROPPORT_NO) {
char buf[64];
isc_buffer_t b;
isc_buffer_putstr(&b, "UNKNOWN RCODE");
"dropped error (%.*s) response: suspicious port",
(int)isc_buffer_usedlength(&b), buf);
return;
}
#endif
/*
* Try to rate limit error responses.
*/
char log_buf[DNS_RRL_LOG_BUF_LEN];
int loglevel;
rcode != dns_rcode_nxdomain);
if (ns_g_server->log_queries)
else
if (rrl_result != DNS_RRL_RESULT_OK) {
/*
* Log dropped errors in the query category
* so that they are not lost in silence.
* Starts of rate-limited bursts are logged in
* NS_LOGCATEGORY_RRL.
*/
if (wouldlog) {
"%s", log_buf);
}
/*
* Some error responses cannot be 'slipped',
* so don't try to slip any error responses.
*/
return;
}
}
}
/*
* Message may be an in-progress reply that we had trouble
* with, in which case QR will be set. We need to clear QR before
* calling dns_message_reply() to avoid triggering an assertion.
*/
/*
* AA and AD shouldn't be set.
*/
if (result != ISC_R_SUCCESS) {
/*
* It could be that we've got a query with a good header,
* but a bad question section, so we try again with
* want_question_section set to ISC_FALSE.
*/
if (result != ISC_R_SUCCESS) {
return;
}
}
if (rcode == dns_rcode_formerr) {
/*
* FORMERR loop avoidance: If we sent a FORMERR message
* with the same ID to the same client less than two
* seconds ago, assume that we are in an infinite error
* packet dialog with a server for some protocol whose
* error responses look enough like DNS queries to
* elicit a FORMERR response. Drop a packet to break
* the loop.
*/
{
/* Drop packet. */
"possible error packet loop, "
"FORMERR dropped");
return;
}
{
/*
*/
isc_uint32_t flags = 0;
if (result == ISC_R_SUCCESS)
}
}
{
unsigned char cookie[COOKIE_SIZE];
int count = 0;
unsigned int flags;
unsigned char expire[4];
else
/* Set EDNS options if applicable */
if (ns_g_server->server_usehostname) {
if (result != ISC_R_SUCCESS) {
goto no_nsid;
}
} else
count++;
}
count++;
}
count++;
}
{
/* Add client subnet option. */
client->ecs_addrlen == 0)
isc_buffer_putuint16(&buf, 0);
else
for (i = 0; i < addrbytes; i++) {
unsigned char uc;
if (i == addrbytes - 1 &&
}
count++;
}
return (result);
}
static inline isc_boolean_t
{
int match;
return (ISC_TRUE);
return (ISC_TRUE);
return (ISC_FALSE);
}
/*
* Callback to see if a non-recursive query coming from 'srcaddr' to
* 'destaddr', with optional key 'mykey' for class 'rdclass' would be
* delivered to 'myview'.
*
* We run this unlocked as both the view list and the interface list
* are updated when the appropriate task has exclusivity.
*/
{
/*
* ns_g_server->interfacemgr is task exclusive locked.
*/
return (ISC_TRUE);
return (ISC_FALSE);
if (view->matchrecursiveonly)
continue;
continue;
if (result != ISC_R_SUCCESS)
continue;
if (!match)
continue;
}
view->matchclients) &&
break;
}
}
static void
{
switch (ns_g_server->cookiealg) {
#if defined(HAVE_OPENSSL_AES) || defined(HAVE_OPENSSL_EVP_AES)
case ns_cookiealg_aes: {
unsigned char digest[ISC_AES_BLOCK_LENGTH];
unsigned char *cp;
unsigned int i;
for (i = 0; i < 8; i++)
case AF_INET:
break;
case AF_INET6:
for (i = 0; i < 8; i++)
digest);
break;
}
for (i = 0; i < 8; i++)
break;
}
#endif
case ns_cookiealg_sha1: {
unsigned char digest[ISC_SHA1_DIGESTLENGTH];
unsigned char *cp;
unsigned int length;
case AF_INET:
length = 4;
break;
case AF_INET6:
length = 4;
break;
default:
INSIST(0);
}
break;
}
case ns_cookiealg_sha256: {
unsigned char digest[ISC_SHA256_DIGESTLENGTH];
unsigned char *cp;
unsigned int length;
case AF_INET:
length = 4;
break;
case AF_INET6:
length = 4;
break;
default:
INSIST(0);
}
break;
}
default:
INSIST(0);
}
}
static void
unsigned char dbuf[COOKIE_SIZE];
unsigned char *old;
/*
* If we have already seen a cookie option skip this cookie option.
*/
return;
}
if (optlen != COOKIE_SIZE) {
/*
* Not our token.
*/
if (optlen == 8U)
else
return;
}
/*
* Process all of the incoming buffer.
*/
/*
* Allow for a 5 minute clock skew between servers sharing a secret.
* Only accept COOKIE if we have talked to the client in the last hour.
*/
return;
}
return;
}
}
static isc_result_t
/*
* If we have already seen a ECS option skip this ECS option.
*/
return (ISC_R_SUCCESS);
}
/*
* XXXMUKS: Is there any need to repeat these checks here
* (except query's scope length) when they are done in the OPT
* RDATA fromwire code?
*/
if (optlen < 4U) {
"EDNS client-subnet option too short");
return (DNS_R_FORMERR);
}
optlen -= 4;
if (scope != 0U) {
"EDNS client-subnet option: invalid scope");
return (DNS_R_OPTERR);
}
"EDNS client-subnet option: "
"source == 0 but family != 0");
return (DNS_R_OPTERR);
}
switch (family) {
case 0:
/*
* XXXMUKS: In queries, if FAMILY is set to 0, SOURCE
* PREFIX-LENGTH must be 0 and ADDRESS should not be
* present as the address and prefix lengths don't make
* sense because the family is unknown.
*/
if (addrlen != 0U) {
"EDNS client-subnet option: invalid "
"address length (%u) for FAMILY=0",
addrlen);
return (DNS_R_OPTERR);
}
break;
case 1:
if (addrlen > 32U) {
"EDNS client-subnet option: invalid "
"address length (%u) for IPv4",
addrlen);
return (DNS_R_OPTERR);
}
break;
case 2:
if (addrlen > 128U) {
"EDNS client-subnet option: invalid "
"address length (%u) for IPv6",
addrlen);
return (DNS_R_OPTERR);
}
break;
default:
"EDNS client-subnet option: invalid family");
return (DNS_R_OPTERR);
}
"EDNS client-subnet option: address too short");
return (DNS_R_OPTERR);
}
if (addrbytes != 0U) {
if ((addrlen % 8) != 0) {
return (DNS_R_OPTERR);
}
}
return (ISC_R_SUCCESS);
}
static isc_result_t
/*
* Set the client's UDP buffer size.
*/
/*
* If the requested UDP buffer size is less than 512,
* ignore it and use 512.
*/
/*
* Get the flags out of the OPT record.
*/
/*
* Do we understand this version of EDNS?
*
* XXXRTH need library support for this!
*/
if (result == ISC_R_SUCCESS)
goto cleanup;
}
/* Check for NSID request */
if (result == ISC_R_SUCCESS) {
switch (optcode) {
case DNS_OPT_NSID:
break;
case DNS_OPT_COOKIE:
break;
case DNS_OPT_EXPIRE:
if (!WANTEXPIRE(client))
break;
case DNS_OPT_CLIENT_SUBNET:
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
break;
default:
break;
}
}
}
return (result);
}
/*
* Handle an incoming request event from the socket (UDP case)
* or tcpmsg (TCP case).
*/
static void
int match;
unsigned int flags;
#ifdef HAVE_DNSTAP
#endif
if (result == ISC_R_SUCCESS) {
}
}
}
} else {
/*
* client->peeraddr was set when the connection was accepted.
*/
}
/* don't count the length header */
if (TCP_CLIENT(client))
reqsize -= 2;
if (exit_check(client))
goto cleanup;
if (result != ISC_R_SUCCESS) {
if (TCP_CLIENT(client)) {
} else {
if (result != ISC_R_CANCELED)
"UDP client handler shutting "
"down due to fatal receive "
"error: %s",
}
goto cleanup;
}
"dropped request: suspicious port");
goto cleanup;
}
#endif
"%s request",
/*
* Check the blackhole ACL for UDP only, since TCP is done in
* client_newconn.
*/
if (!TCP_CLIENT(client)) {
match > 0)
{
"blackholed UDP datagram");
goto cleanup;
}
}
/*
* Silently drop multicast requests for the present.
* XXXMPA revisit this as mDNS spec was published.
*/
"dropping multicast request");
goto cleanup;
}
if (result != ISC_R_SUCCESS) {
/*
* There isn't enough header to determine whether
* this was a request or a response. Drop it.
*/
goto cleanup;
}
/*
* The client object handles requests, not responses.
* If this is a UDP response, forward it to the dispatcher.
* If it's a TCP response, discard it here.
*/
if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
if (TCP_CLIENT(client)) {
CTRACE("unexpected response");
goto cleanup;
} else {
goto cleanup;
}
}
/*
* Update some statistics counters. Don't count responses.
*/
} else {
}
if (TCP_CLIENT(client)) {
case AF_INET:
break;
case AF_INET6:
break;
default:
INSIST(0);
break;
}
} else {
case AF_INET:
break;
case AF_INET6:
break;
default:
INSIST(0);
break;
}
}
/*
* It's a request. Parse it.
*/
if (result != ISC_R_SUCCESS) {
/*
* Parsing the request failed. Send a response
* (typically FORMERR or SERVFAIL).
*/
if (result == DNS_R_OPTERR)
"message parsing failed: %s",
goto cleanup;
}
/*
* Pipeline TCP query processing.
*/
if (result == ISC_R_SUCCESS)
if (result != ISC_R_SUCCESS) {
"no more TCP clients(read): %s",
}
}
case dns_opcode_query:
case dns_opcode_update:
case dns_opcode_notify:
break;
case dns_opcode_iquery:
default:
break;
}
/* RFC1123 section 6.1.3.2 */
/*
* Deal with EDNS.
*/
if (ns_g_noedns)
else
client->ecs_addrlen = 0;
/*
* Are we dropping all EDNS queries?
*/
if (ns_g_dropedns) {
goto cleanup;
}
if (result != ISC_R_SUCCESS)
goto cleanup;
}
if (result != ISC_R_SUCCESS) {
return;
}
if (notimp)
return;
}
"message class could not be determined");
"message class could not be determined");
goto cleanup;
}
/*
* Determine the destination address. If the receiving interface is
* bound to a specific address, we simply use it regardless of the
* address family. All IPv4 queries should fall into this case.
* Otherwise, if this is a TCP query, get the address from the
* receiving socket (this needs a system call and can be heavy).
* For IPv6 UDP queries, we get this from the pktinfo structure (if
* supported).
* If all the attempts fail (this can happen due to memory shortage,
* etc), we regard this as an error for safety.
*/
else {
if (TCP_CLIENT(client))
&sockaddr);
if (result == ISC_R_SUCCESS)
if (result != ISC_R_SUCCESS &&
/*
* XXXJT technically, we should convert the receiving
* interface ID to a proper scope zone ID. However,
* due to the fact there is no standard API for this,
* we only handle link-local addresses and use the
* interface index as link ID. Despite the assumption,
* it should cover most typical cases.
*/
}
if (result != ISC_R_SUCCESS) {
"failed to get request's "
"destination: %s",
goto cleanup;
}
}
/*
* Find a view that matches the client's source address.
*/
{
view);
if (sigresult == ISC_R_SUCCESS) {
}
}
!(view->matchrecursiveonly &&
{
break;
}
}
}
/*
* Do a dummy TSIG verification attempt so that the
* response will have a TSIG if the query did, as
* required by RFC2845.
*/
isc_buffer_t b;
isc_region_t *r;
isc_buffer_add(&b, r->length);
sizeof(classname));
"no matching view in class '%s'", classname);
goto cleanup;
}
/*
* Check for a signature. We log bad signatures regardless of
* whether they ultimately cause the request to be rejected or
* not. We do not log the lack of a signature unless we are
* debugging.
*/
if (result != ISC_R_NOTFOUND) {
} else {
}
}
if (result == ISC_R_SUCCESS) {
char namebuf[DNS_NAME_FORMATSIZE];
"request has valid signature: %s", namebuf);
} else if (result == ISC_R_NOTFOUND) {
"request is not signed");
} else if (result == DNS_R_NOIDENTITY) {
"request is signed by a nonauthoritative key");
} else {
char tsigrcode[64];
isc_buffer_t b;
/* There is a signature, but it is bad. */
char namebuf[DNS_NAME_FORMATSIZE];
char cnamebuf[DNS_NAME_FORMATSIZE];
"request has invalid signature: "
"TSIG %s (%s): %s (%s)", namebuf,
} else {
"request has invalid signature: "
"TSIG %s: %s (%s)", namebuf,
}
} else {
"request has invalid signature: %s (%s)",
}
/*
* Accept update messages signed by unknown keys so that
* update forwarding works transparently through slaves
* that don't have all the same keys as the master.
*/
goto cleanup;
}
}
/*
* Decide whether recursive service is available to this client.
* We do this here rather than in the query code so that we can
* set the RA bit correctly on all kinds of responses, not just
* responses to ordinary queries. Note if you can't query the
* cache there is no point in setting RA.
*/
ISC_TRUE) == ISC_R_SUCCESS &&
ISC_TRUE) == ISC_R_SUCCESS &&
ISC_TRUE) == ISC_R_SUCCESS &&
ISC_TRUE) == ISC_R_SUCCESS)
"recursion not available");
/*
* Adjust maximum UDP response size for this client.
*/
}
/*
* Dispatch the request.
*/
case dns_opcode_query:
CTRACE("query");
#ifdef HAVE_DNSTAP
else
#endif /* HAVE_DNSTAP */
break;
case dns_opcode_update:
CTRACE("update");
break;
case dns_opcode_notify:
CTRACE("notify");
break;
case dns_opcode_iquery:
CTRACE("iquery");
break;
default:
CTRACE("unknown opcode");
}
return;
}
static void
CTRACE("timeout");
}
(void)exit_check(client);
}
static isc_result_t
#if NMCTXS > 0
unsigned int nextmctx;
#endif
MTRACE("clientmctx");
/*
* Caller must be holding the manager lock.
*/
if (ns_g_clienttest) {
if (result == ISC_R_SUCCESS)
return (result);
}
#if NMCTXS > 0
if (clientmctx == NULL) {
if (result != ISC_R_SUCCESS)
return (result);
}
#else
#endif
return (ISC_R_SUCCESS);
}
static isc_result_t
/*
* Caller must be holding the manager lock.
*
* Note: creating a client does not add the client to the
* manager's client list or set the client's manager pointer.
* The caller is responsible for that.
*/
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_NOMEMORY);
}
if (result != ISC_R_SUCCESS)
goto cleanup_client;
if (result != ISC_R_SUCCESS)
goto cleanup_task;
if (result != ISC_R_SUCCESS)
goto cleanup_timer;
/* XXXRTH Hardwired constants */
goto cleanup_message;
}
goto cleanup_sendevent;
}
goto cleanup_recvbuf;
}
client->references = 0;
client->attributes = 0;
client->ecs_addrlen = 0;
#ifdef ALLOW_FILTER_AAAA
#endif
/*
* Initialize FORMERR cache to sentinel value that will not match
* any actual FORMERR response.
*/
/*
* We call the init routines for the various kinds of client here,
* after we have created an otherwise valid client, because some
* of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
*/
if (result != ISC_R_SUCCESS)
goto cleanup_recvevent;
if (result != ISC_R_SUCCESS)
goto cleanup_query;
CTRACE("create");
return (ISC_R_SUCCESS);
return (result);
}
static void
CTRACE("read");
if (result != ISC_R_SUCCESS)
goto fail;
/*
* Set a timeout to limit the amount of time we will wait
* for a request on this TCP connection.
*/
return;
fail:
}
static void
/*
* We must take ownership of the new socket before the exit
* check to make sure it gets destroyed if we decide to exit.
*/
"new TCP connection");
} else {
/*
* XXXRTH What should we do? We're trying to accept but
* it didn't work. If we just give up, then TCP
* service may eventually stop.
*
* For now, we just go idle.
*
* Going idle is probably the right thing if the
* I/O was canceled.
*/
"accept failed: %s",
}
if (exit_check(client))
goto freeevent;
int match;
match > 0)
{
"blackholed connection attempt");
(void)exit_check(client);
goto freeevent;
}
/*
* Let a new client take our place immediately, before
* we wait for a request packet. If we don't,
* telnetting to port 53 (once per CPU) will
* deny service to legitimate TCP clients.
*/
if (result == ISC_R_SUCCESS)
if (result != ISC_R_SUCCESS) {
"no more TCP clients(accept): %s",
ns_g_server->keepresporder)) {
}
}
}
static void
CTRACE("accept");
if (result != ISC_R_SUCCESS) {
"isc_socket_accept() failed: %s",
/*
* XXXRTH What should we do? We're trying to accept but
* it didn't work. If we just give up, then TCP
* service may eventually stop.
*
* For now, we just go idle.
*/
return;
}
}
static void
isc_region_t r;
CTRACE("udprecv");
r.length = RECV_BUFFER_SIZE;
if (result != ISC_R_SUCCESS) {
"isc_socket_recv2() failed: %s",
/*
* This cannot happen in the current implementation, since
* isc_socket_recv2() cannot fail if flags == 0.
*
* If this does fail, we just go idle.
*/
return;
}
}
void
source->references++;
}
void
client->references--;
(void)exit_check(client);
}
}
CTRACE("replace");
} else {
}
if (result != ISC_R_SUCCESS)
return (result);
/*
* The responsibility for listening for new requests is hereby
* transferred to the new client. Therefore, the old client
* should refrain from listening for any more requests.
*/
return (ISC_R_SUCCESS);
}
/***
*** Client Manager
***/
static void
#if NMCTXS > 0
int i;
#endif
MTRACE("clientmgr_destroy");
#if NMCTXS > 0
for (i = 0; i < NMCTXS; i++) {
}
#endif
}
{
#if NMCTXS > 0
int i;
#endif
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS)
goto cleanup_manager;
if (result != ISC_R_SUCCESS)
goto cleanup_lock;
if (result != ISC_R_SUCCESS)
goto cleanup_listlock;
#if NMCTXS > 0
for (i = 0; i < NMCTXS; i++)
#endif
MTRACE("create");
return (ISC_R_SUCCESS);
return (result);
}
void
MTRACE("destroy");
/*
* Check for success because we may already be task-exclusive
* at this point. Only if we succeed at obtaining an exclusive
* lock now will we need to relinquish it later.
*/
if (result == ISC_R_SUCCESS)
if (unlock)
if (need_destroy)
}
static isc_result_t
{
MTRACE("get client");
return (ISC_R_SHUTTINGDOWN);
/*
* Allocate a client. First try to get a recycled one;
* if that fails, make a new one.
*/
if (!ns_g_clienttest)
MTRACE("recycle");
else {
MTRACE("create new");
if (result != ISC_R_SUCCESS)
return (result);
}
if (tcp) {
&client->tcplistener);
} else {
}
return (ISC_R_SUCCESS);
}
static isc_result_t
{
MTRACE("get worker");
return (ISC_R_SHUTTINGDOWN);
/*
* Allocate a client. First try to get a recycled one;
* if that fails, make a new one.
*/
if (!ns_g_clienttest)
MTRACE("recycle");
else {
MTRACE("create new");
if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
{
unsigned int disp;
REQUIRE(n > 0);
MTRACE("createclients");
if (result != ISC_R_SUCCESS)
break;
}
return (result);
}
}
{
isc_uint8_t ecs_addrlen = 0;
int match;
if (default_allow)
goto allow;
else
goto deny;
}
netaddr = &tmpnetaddr;
}
}
if (result != ISC_R_SUCCESS)
goto deny; /* Internal error, already logged. */
if (match > 0)
goto allow;
goto deny; /* Negative match or no match. */
return (ISC_R_SUCCESS);
deny:
return (DNS_R_REFUSED);
}
{
acl, default_allow);
if (result == ISC_R_SUCCESS)
"%s approved", opname);
else
return (result);
}
static void
if (client->peeraddr_valid)
(unsigned int)len);
else
}
void
{
char msgbuf[4096];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
const char *viewname = "";
dns_name_t *q = NULL;
sep1 = "/key ";
}
if (q != NULL) {
sep2 = " (";
sep3 = ")";
}
sep4 = ": view ";
}
"client %s%s%s%s%s%s%s%s: %s",
}
void
{
return;
}
void
{
char namebuf[DNS_NAME_FORMATSIZE];
char typebuf[DNS_RDATATYPE_FORMATSIZE];
classbuf);
}
static void
int len = 1024;
return;
/*
* Note that these are multiline debug messages. We want a newline
* to appear in the log after each message.
*/
do {
break;
0, &buffer);
if (result == ISC_R_NOSPACE) {
len += 1024;
} else if (result == ISC_R_SUCCESS)
"%s\n%.*s", reason,
(int)isc_buffer_usedlength(&buffer),
buf);
} while (result == ISC_R_NOSPACE);
}
void
char namebuf[DNS_NAME_FORMATSIZE];
char original[DNS_NAME_FORMATSIZE];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
char typebuf[DNS_RDATATYPE_FORMATSIZE];
const char *name;
const char *sep;
const char *origfor;
sep = ": view ";
} else {
name = "";
sep = "";
}
origfor = " for ";
sizeof(original));
} else {
origfor = "";
original[0] = '\0';
}
sizeof(typebuf));
sizeof(classbuf));
} else {
}
fprintf(f, "; client %s%s%s: id %u '%s/%s/%s'%s%s "
}
}
void
/*
* client->query.qname was dynamically allocated.
*/
}
}
return (ISC_R_SUCCESS);
}