1999-07-24 01:17:44 +00:00
|
|
|
/*
|
2016-06-27 14:56:38 +10:00
|
|
|
* Copyright (C) 1999-2016 Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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/.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
#include <isc/aes.h>
|
2001-08-08 22:54:55 +00:00
|
|
|
#include <isc/formatcheck.h>
|
2015-07-06 09:44:24 +10:00
|
|
|
#include <isc/hmacsha.h>
|
2000-08-25 01:08:07 +00:00
|
|
|
#include <isc/mutex.h>
|
2001-05-25 07:39:48 +00:00
|
|
|
#include <isc/once.h>
|
2005-06-04 05:32:50 +00:00
|
|
|
#include <isc/platform.h>
|
2001-01-22 18:58:36 +00:00
|
|
|
#include <isc/print.h>
|
2011-10-10 22:57:14 +00:00
|
|
|
#include <isc/queue.h>
|
2014-02-19 12:53:42 +11:00
|
|
|
#include <isc/random.h>
|
2015-08-17 18:26:44 -07:00
|
|
|
#include <isc/safe.h>
|
2014-02-19 12:53:42 +11:00
|
|
|
#include <isc/serial.h>
|
2009-01-27 22:30:00 +00:00
|
|
|
#include <isc/stats.h>
|
2001-05-25 07:39:48 +00:00
|
|
|
#include <isc/stdio.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2001-01-22 18:58:36 +00:00
|
|
|
#include <isc/task.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <isc/timer.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2016-05-05 11:46:11 +02:00
|
|
|
#include <dns/adb.h>
|
2014-09-03 23:28:14 -07:00
|
|
|
#include <dns/badcache.h>
|
2000-10-11 17:44:18 +00:00
|
|
|
#include <dns/db.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <dns/dispatch.h>
|
2015-10-02 12:32:42 -07:00
|
|
|
#include <dns/dnstap.h>
|
2016-05-05 11:46:11 +02:00
|
|
|
#include <dns/cache.h>
|
2014-09-10 15:31:40 +10:00
|
|
|
#include <dns/edns.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
#include <dns/events.h>
|
|
|
|
#include <dns/message.h>
|
2006-01-05 00:01:46 +00:00
|
|
|
#include <dns/peer.h>
|
2002-03-05 00:36:44 +00:00
|
|
|
#include <dns/rcode.h>
|
1999-09-02 01:52:31 +00:00
|
|
|
#include <dns/rdata.h>
|
2000-11-15 19:15:25 +00:00
|
|
|
#include <dns/rdataclass.h>
|
1999-09-02 01:52:31 +00:00
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
2006-01-05 00:01:46 +00:00
|
|
|
#include <dns/resolver.h>
|
2008-04-03 05:55:52 +00:00
|
|
|
#include <dns/stats.h>
|
2000-09-12 07:48:28 +00:00
|
|
|
#include <dns/tsig.h>
|
1999-08-05 22:14:43 +00:00
|
|
|
#include <dns/view.h>
|
2000-01-27 01:00:16 +00:00
|
|
|
#include <dns/zone.h>
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2016-05-05 11:46:11 +02:00
|
|
|
#include <named/fuzz.h>
|
2000-02-10 22:16:56 +00:00
|
|
|
#include <named/interfacemgr.h>
|
1999-10-22 19:35:19 +00:00
|
|
|
#include <named/log.h>
|
2000-02-10 22:16:56 +00:00
|
|
|
#include <named/notify.h>
|
2008-04-03 02:01:08 +00:00
|
|
|
#include <named/os.h>
|
1999-12-16 23:11:07 +00:00
|
|
|
#include <named/server.h>
|
1999-08-20 06:03:02 +00:00
|
|
|
#include <named/update.h>
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
*** Client
|
|
|
|
***/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file
|
|
|
|
* Client Routines
|
|
|
|
*
|
2000-02-10 22:16:56 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
#define NS_CLIENT_TRACE
|
|
|
|
#ifdef NS_CLIENT_TRACE
|
2000-04-04 18:28:51 +00:00
|
|
|
#define CTRACE(m) ns_client_log(client, \
|
1999-10-22 19:35:19 +00:00
|
|
|
NS_LOGCATEGORY_CLIENT, \
|
|
|
|
NS_LOGMODULE_CLIENT, \
|
1999-11-23 20:55:02 +00:00
|
|
|
ISC_LOG_DEBUG(3), \
|
2000-04-04 18:28:51 +00:00
|
|
|
"%s", (m))
|
1999-10-22 19:35:19 +00:00
|
|
|
#define MTRACE(m) isc_log_write(ns_g_lctx, \
|
|
|
|
NS_LOGCATEGORY_GENERAL, \
|
|
|
|
NS_LOGMODULE_CLIENT, \
|
1999-11-23 20:55:02 +00:00
|
|
|
ISC_LOG_DEBUG(3), \
|
2000-04-04 18:28:51 +00:00
|
|
|
"clientmgr @%p: %s", manager, (m))
|
2000-03-29 18:57:36 +00:00
|
|
|
#else
|
|
|
|
#define CTRACE(m) ((void)(m))
|
|
|
|
#define MTRACE(m) ((void)(m))
|
1999-07-24 01:17:44 +00:00
|
|
|
#endif
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
|
|
|
|
|
2000-06-22 00:05:11 +00:00
|
|
|
#define TCP_BUFFER_SIZE (65535 + 2)
|
2000-11-16 00:18:13 +00:00
|
|
|
#define SEND_BUFFER_SIZE 4096
|
|
|
|
#define RECV_BUFFER_SIZE 4096
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2005-10-16 23:21:25 +00:00
|
|
|
#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
|
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
#define COOKIE_SIZE 24U /* 8 + 4 + 4 + 8 */
|
2014-08-28 22:05:57 -07:00
|
|
|
#define ECS_SIZE 20U /* 2 + 1 + 1 + [0..16] */
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2016-02-27 11:23:50 +11:00
|
|
|
#define WANTNSID(x) (((x)->attributes & NS_CLIENTATTR_WANTNSID) != 0)
|
|
|
|
#define WANTEXPIRE(x) (((x)->attributes & NS_CLIENTATTR_WANTEXPIRE) != 0)
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*% nameserver client manager structure */
|
1999-07-24 01:17:44 +00:00
|
|
|
struct ns_clientmgr {
|
|
|
|
/* Unlocked. */
|
|
|
|
unsigned int magic;
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
/* The queue object has its own locks */
|
|
|
|
client_queue_t inactive; /*%< To be recycled */
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_mem_t * mctx;
|
|
|
|
isc_taskmgr_t * taskmgr;
|
|
|
|
isc_timermgr_t * timermgr;
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
/* Lock covers manager state. */
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_mutex_t lock;
|
|
|
|
isc_boolean_t exiting;
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
/* Lock covers the clients list */
|
|
|
|
isc_mutex_t listlock;
|
|
|
|
client_list_t clients; /*%< All active clients */
|
|
|
|
|
|
|
|
/* Lock covers the recursing list */
|
|
|
|
isc_mutex_t reclock;
|
|
|
|
client_list_t recursing; /*%< Recursing clients */
|
|
|
|
|
2005-10-16 23:21:25 +00:00
|
|
|
#if NMCTXS > 0
|
|
|
|
/*%< mctx pool for clients. */
|
|
|
|
unsigned int nextmctx;
|
|
|
|
isc_mem_t * mctxpool[NMCTXS];
|
|
|
|
#endif
|
1999-07-24 01:17:44 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define MANAGER_MAGIC ISC_MAGIC('N', 'S', 'C', 'm')
|
|
|
|
#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, MANAGER_MAGIC)
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2008-01-18 23:46:58 +00:00
|
|
|
/*!
|
2000-02-10 22:16:56 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_FREED 0
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%<
|
2000-02-10 22:16:56 +00:00
|
|
|
* The client object no longer exists.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_INACTIVE 1
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%<
|
2000-08-01 01:33:37 +00:00
|
|
|
* The client object exists and has a task and timer.
|
2000-02-10 22:16:56 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_READY 2
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%<
|
2000-02-10 22:16:56 +00:00
|
|
|
* 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.
|
1999-07-24 01:17:44 +00:00
|
|
|
*
|
2000-02-10 22:16:56 +00:00
|
|
|
* If it is a TCP client object, it has a TCP listener socket
|
2001-01-27 02:08:07 +00:00
|
|
|
* and an outstanding TCP listen request.
|
1999-07-24 01:17:44 +00:00
|
|
|
*
|
2001-01-27 02:08:07 +00:00
|
|
|
* If it is a UDP client object, it has a UDP listener socket
|
|
|
|
* and an outstanding UDP receive request.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_READING 3
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%<
|
2000-02-10 22:16:56 +00:00
|
|
|
* The client object is a TCP client object that has received
|
2000-08-01 01:33:37 +00:00
|
|
|
* a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
|
2000-02-10 22:16:56 +00:00
|
|
|
* outstanding TCP read request. This state is not used for
|
|
|
|
* UDP client objects.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NS_CLIENTSTATE_WORKING 4
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%<
|
2000-02-10 22:16:56 +00:00
|
|
|
* The client object has received a request and is working
|
2001-10-12 23:54:03 +00:00
|
|
|
* on it. It has a view, and it may have any of a non-reset OPT,
|
2001-01-27 02:08:07 +00:00
|
|
|
* recursion quota, and an outstanding write request.
|
2000-08-01 01:33:37 +00:00
|
|
|
*/
|
2000-02-10 22:16:56 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
#define NS_CLIENTSTATE_RECURSING 5
|
|
|
|
/*%<
|
|
|
|
* The client object is recursing. It will be on the 'recursing'
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
#define NS_CLIENTSTATE_MAX 9
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%<
|
2000-02-10 22:16:56 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-01-04 05:06:10 +00:00
|
|
|
/*
|
|
|
|
* Enable ns_client_dropport() by default.
|
|
|
|
*/
|
|
|
|
#ifndef NS_CLIENT_DROPPORT
|
|
|
|
#define NS_CLIENT_DROPPORT 1
|
|
|
|
#endif
|
|
|
|
|
2005-08-15 01:21:07 +00:00
|
|
|
unsigned int ns_client_requests;
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
static void client_read(ns_client_t *client);
|
|
|
|
static void client_accept(ns_client_t *client);
|
2001-01-27 02:08:07 +00:00
|
|
|
static void client_udprecv(ns_client_t *client);
|
2000-02-10 22:16:56 +00:00
|
|
|
static void clientmgr_destroy(ns_clientmgr_t *manager);
|
|
|
|
static isc_boolean_t exit_check(ns_client_t *client);
|
|
|
|
static void ns_client_endrequest(ns_client_t *client);
|
2000-07-26 17:39:12 +00:00
|
|
|
static void client_start(isc_task_t *task, isc_event_t *event);
|
|
|
|
static void client_request(isc_task_t *task, isc_event_t *event);
|
2001-05-25 07:39:48 +00:00
|
|
|
static void ns_client_dumpmessage(ns_client_t *client, const char *reason);
|
2011-10-04 16:04:22 +00:00
|
|
|
static isc_result_t get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
|
|
|
|
dns_dispatch_t *disp, isc_boolean_t tcp);
|
2015-01-20 16:10:30 -08:00
|
|
|
static isc_result_t get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp,
|
2015-02-27 10:55:55 +11:00
|
|
|
isc_socket_t *sock);
|
2014-02-06 19:37:26 -08:00
|
|
|
static inline isc_boolean_t
|
2014-08-28 22:05:57 -07:00
|
|
|
allowed(isc_netaddr_t *addr, dns_name_t *signer, isc_netaddr_t *ecs_addr,
|
|
|
|
isc_uint8_t ecs_addrlen, isc_uint8_t *ecs_scope, dns_acl_t *acl);
|
2015-07-06 09:44:24 +10:00
|
|
|
static void compute_cookie(ns_client_t *client, isc_uint32_t when,
|
|
|
|
isc_uint32_t nonce, isc_buffer_t *buf);
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2001-10-24 03:10:18 +00:00
|
|
|
void
|
2005-07-27 02:29:01 +00:00
|
|
|
ns_client_recursing(ns_client_t *client) {
|
2001-10-24 03:10:18 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
2011-10-10 22:57:14 +00:00
|
|
|
REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
|
2001-10-24 03:10:18 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
LOCK(&client->manager->reclock);
|
2012-03-15 11:33:43 +11:00
|
|
|
client->newstate = client->state = NS_CLIENTSTATE_RECURSING;
|
2011-11-09 22:05:09 +00:00
|
|
|
ISC_LIST_APPEND(client->manager->recursing, client, rlink);
|
2011-10-10 22:57:14 +00:00
|
|
|
UNLOCK(&client->manager->reclock);
|
2001-10-24 03:10:18 +00:00
|
|
|
}
|
|
|
|
|
2005-07-27 02:29:01 +00:00
|
|
|
void
|
|
|
|
ns_client_killoldestquery(ns_client_t *client) {
|
|
|
|
ns_client_t *oldest;
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
LOCK(&client->manager->reclock);
|
2005-07-27 02:29:01 +00:00
|
|
|
oldest = ISC_LIST_HEAD(client->manager->recursing);
|
|
|
|
if (oldest != NULL) {
|
2011-10-10 22:57:14 +00:00
|
|
|
ISC_LIST_UNLINK(client->manager->recursing, oldest, rlink);
|
|
|
|
UNLOCK(&client->manager->reclock);
|
2005-07-27 02:29:01 +00:00
|
|
|
ns_query_cancel(oldest);
|
2011-10-10 22:57:14 +00:00
|
|
|
} else
|
|
|
|
UNLOCK(&client->manager->reclock);
|
2005-07-27 02:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-01-29 19:49:52 +00:00
|
|
|
void
|
|
|
|
ns_client_settimeout(ns_client_t *client, unsigned int seconds) {
|
2000-02-14 23:56:47 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_interval_t interval;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
isc_interval_set(&interval, seconds, 0);
|
|
|
|
result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
|
|
|
|
&interval, ISC_FALSE);
|
2001-01-29 19:49:52 +00:00
|
|
|
client->timerset = ISC_TRUE;
|
2000-02-14 23:56:47 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2000-02-14 23:56:47 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
|
2000-07-13 00:21:27 +00:00
|
|
|
"setting timeout: %s",
|
2000-02-14 23:56:47 +00:00
|
|
|
isc_result_totext(result));
|
|
|
|
/* Continue anyway. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-02-10 22:16:56 +00:00
|
|
|
* 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
|
|
|
|
exit_check(ns_client_t *client) {
|
2011-10-10 22:57:14 +00:00
|
|
|
isc_boolean_t destroy_manager = ISC_FALSE;
|
|
|
|
ns_clientmgr_t *manager = NULL;
|
2001-11-14 22:00:22 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
2011-10-10 22:57:14 +00:00
|
|
|
manager = client->manager;
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
if (client->state <= client->newstate)
|
|
|
|
return (ISC_FALSE); /* Business as usual. */
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
INSIST(client->newstate < NS_CLIENTSTATE_RECURSING);
|
2000-02-14 23:56:47 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2000-02-11 20:56:19 +00:00
|
|
|
* - The client does not detach from the view until references is zero
|
|
|
|
* - references does not go to zero until the resolver has shut down
|
2000-02-10 22:16:56 +00:00
|
|
|
*
|
2004-07-23 02:57:27 +00:00
|
|
|
* Keep the view attached until any outstanding updates complete.
|
2000-02-10 22:16:56 +00:00
|
|
|
*/
|
2008-01-18 23:46:58 +00:00
|
|
|
if (client->nupdates == 0 &&
|
2004-07-23 02:57:27 +00:00
|
|
|
client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
|
2000-08-01 01:33:37 +00:00
|
|
|
dns_view_detach(&client->view);
|
2000-02-10 22:16:56 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
if (client->state == NS_CLIENTSTATE_WORKING ||
|
|
|
|
client->state == NS_CLIENTSTATE_RECURSING)
|
|
|
|
{
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->newstate <= NS_CLIENTSTATE_READING);
|
2004-07-23 02:57:27 +00:00
|
|
|
/*
|
|
|
|
* Let the update processing complete.
|
|
|
|
*/
|
|
|
|
if (client->nupdates > 0)
|
|
|
|
return (ISC_TRUE);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* We are trying to abort request processing.
|
|
|
|
*/
|
|
|
|
if (client->nsends > 0) {
|
2015-02-27 10:55:55 +11:00
|
|
|
isc_socket_t *sock;
|
2000-02-10 22:16:56 +00:00
|
|
|
if (TCP_CLIENT(client))
|
2015-02-27 10:55:55 +11:00
|
|
|
sock = client->tcpsocket;
|
2000-02-10 22:16:56 +00:00
|
|
|
else
|
2015-02-27 10:55:55 +11:00
|
|
|
sock = client->udpsocket;
|
|
|
|
isc_socket_cancel(sock, client->task,
|
2000-02-10 22:16:56 +00:00
|
|
|
ISC_SOCKCANCEL_SEND);
|
|
|
|
}
|
2000-02-14 23:56:47 +00:00
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
if (! (client->nsends == 0 && client->nrecvs == 0 &&
|
|
|
|
client->references == 0))
|
|
|
|
{
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* Still waiting for I/O cancel completion.
|
|
|
|
* or lingering references.
|
|
|
|
*/
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* I/O cancel is complete. Burn down all state
|
2006-06-04 23:59:33 +00:00
|
|
|
* related to the current request. Ensure that
|
2011-10-10 22:57:14 +00:00
|
|
|
* the client is no longer on the recursing list.
|
2011-11-07 23:03:09 +00:00
|
|
|
*
|
|
|
|
* 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()
|
2000-02-10 22:16:56 +00:00
|
|
|
*/
|
2011-10-10 22:57:14 +00:00
|
|
|
if (client->state == NS_CLIENTSTATE_RECURSING) {
|
|
|
|
LOCK(&manager->reclock);
|
2011-11-07 23:03:09 +00:00
|
|
|
if (ISC_LINK_LINKED(client, rlink))
|
|
|
|
ISC_LIST_UNLINK(manager->recursing,
|
|
|
|
client, rlink);
|
2011-10-10 22:57:14 +00:00
|
|
|
UNLOCK(&manager->reclock);
|
2006-06-04 23:59:33 +00:00
|
|
|
}
|
2000-02-10 22:16:56 +00:00
|
|
|
ns_client_endrequest(client);
|
|
|
|
|
|
|
|
client->state = NS_CLIENTSTATE_READING;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (NS_CLIENTSTATE_READING == client->newstate) {
|
2015-01-20 16:10:30 -08:00
|
|
|
if (!client->pipelined) {
|
|
|
|
client_read(client);
|
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
|
|
|
return (ISC_TRUE); /* We're done. */
|
|
|
|
} else if (client->mortal) {
|
|
|
|
client->newstate = NS_CLIENTSTATE_INACTIVE;
|
2015-08-14 08:34:54 -07:00
|
|
|
} else
|
|
|
|
return (ISC_FALSE);
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_READING) {
|
|
|
|
/*
|
|
|
|
* We are trying to abort the current TCP connection,
|
|
|
|
* if any.
|
|
|
|
*/
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->newstate <= NS_CLIENTSTATE_READY);
|
|
|
|
if (client->nreads > 0)
|
|
|
|
dns_tcpmsg_cancelread(&client->tcpmsg);
|
2016-06-02 10:18:17 +10:00
|
|
|
if (client->nreads != 0) {
|
2000-02-10 22:16:56 +00:00
|
|
|
/* Still waiting for read cancel completion. */
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->tcpmsg_valid) {
|
|
|
|
dns_tcpmsg_invalidate(&client->tcpmsg);
|
|
|
|
client->tcpmsg_valid = ISC_FALSE;
|
|
|
|
}
|
2000-04-20 17:07:46 +00:00
|
|
|
if (client->tcpsocket != NULL) {
|
|
|
|
CTRACE("closetcp");
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_socket_detach(&client->tcpsocket);
|
2000-04-20 17:07:46 +00:00
|
|
|
}
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
if (client->tcpquota != NULL)
|
|
|
|
isc_quota_detach(&client->tcpquota);
|
2000-02-14 23:56:47 +00:00
|
|
|
|
2001-01-29 19:49:52 +00:00
|
|
|
if (client->timerset) {
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)isc_timer_reset(client->timer,
|
|
|
|
isc_timertype_inactive,
|
|
|
|
NULL, NULL, ISC_TRUE);
|
2001-01-29 19:49:52 +00:00
|
|
|
client->timerset = ISC_FALSE;
|
|
|
|
}
|
2000-03-06 17:35:13 +00:00
|
|
|
|
2015-01-20 16:10:30 -08:00
|
|
|
client->pipelined = ISC_FALSE;
|
|
|
|
|
2000-04-04 18:28:51 +00:00
|
|
|
client->peeraddr_valid = ISC_FALSE;
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = NS_CLIENTSTATE_READY;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-03-06 17:35:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2011-10-10 22:57:14 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2000-03-06 17:35:13 +00:00
|
|
|
*/
|
2011-10-10 22:57:14 +00:00
|
|
|
if (client->mortal && TCP_CLIENT(client) && !ns_g_clienttest) {
|
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
if (client->interface->ntcpcurrent <
|
|
|
|
client->interface->ntcptarget)
|
|
|
|
client->mortal = ISC_FALSE;
|
|
|
|
UNLOCK(&client->interface->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need the client; send it to the inactive
|
|
|
|
* queue for recycling.
|
|
|
|
*/
|
|
|
|
if (client->mortal) {
|
|
|
|
if (client->newstate > NS_CLIENTSTATE_INACTIVE)
|
|
|
|
client->newstate = NS_CLIENTSTATE_INACTIVE;
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (NS_CLIENTSTATE_READY == client->newstate) {
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
client_accept(client);
|
2001-01-27 02:08:07 +00:00
|
|
|
} else
|
|
|
|
client_udprecv(client);
|
2000-02-10 22:16:56 +00:00
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_READY) {
|
2000-08-01 01:33:37 +00:00
|
|
|
INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* We are trying to enter the inactive state.
|
|
|
|
*/
|
|
|
|
if (client->naccepts > 0)
|
|
|
|
isc_socket_cancel(client->tcplistener, client->task,
|
|
|
|
ISC_SOCKCANCEL_ACCEPT);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
/* Still waiting for accept cancel completion. */
|
|
|
|
if (! (client->naccepts == 0))
|
2000-02-10 22:16:56 +00:00
|
|
|
return (ISC_TRUE);
|
2001-01-27 02:08:07 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
/* Accept cancel is complete. */
|
2001-01-27 02:08:07 +00:00
|
|
|
if (client->nrecvs > 0)
|
|
|
|
isc_socket_cancel(client->udpsocket, client->task,
|
|
|
|
ISC_SOCKCANCEL_RECV);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
/* Still waiting for recv cancel completion. */
|
|
|
|
if (! (client->nrecvs == 0))
|
2001-01-27 02:08:07 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
/* Still waiting for control event to be delivered */
|
|
|
|
if (client->nctls > 0)
|
2001-11-14 22:00:22 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
|
|
|
|
/* Deactivate the client. */
|
|
|
|
if (client->interface)
|
|
|
|
ns_interface_detach(&client->interface);
|
|
|
|
|
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
INSIST(client->recursionquota == NULL);
|
|
|
|
if (client->tcplistener != NULL)
|
|
|
|
isc_socket_detach(&client->tcplistener);
|
|
|
|
|
|
|
|
if (client->udpsocket != NULL)
|
|
|
|
isc_socket_detach(&client->udpsocket);
|
|
|
|
|
|
|
|
if (client->dispatch != NULL)
|
|
|
|
dns_dispatch_detach(&client->dispatch);
|
|
|
|
|
|
|
|
client->attributes = 0;
|
|
|
|
client->mortal = ISC_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-11-09 22:05:09 +00:00
|
|
|
client->state = NS_CLIENTSTATE_INACTIVE;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2001-11-14 22:00:22 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (client->state == client->newstate) {
|
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
2012-08-22 19:19:30 +10:00
|
|
|
if (!ns_g_clienttest && manager != NULL &&
|
|
|
|
!manager->exiting)
|
|
|
|
ISC_QUEUE_PUSH(manager->inactive, client,
|
|
|
|
ilink);
|
2008-01-02 05:03:07 +00:00
|
|
|
if (client->needshutdown)
|
|
|
|
isc_task_shutdown(client->task);
|
2011-10-10 22:57:14 +00:00
|
|
|
return (ISC_TRUE);
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->state == NS_CLIENTSTATE_INACTIVE) {
|
|
|
|
INSIST(client->newstate == NS_CLIENTSTATE_FREED);
|
|
|
|
/*
|
|
|
|
* We are trying to free the client.
|
2001-11-14 22:00:22 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2000-02-10 22:16:56 +00:00
|
|
|
*/
|
2001-11-14 22:00:22 +00:00
|
|
|
REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
|
|
|
|
|
|
|
|
INSIST(client->recursionquota == NULL);
|
2012-08-22 19:19:30 +10:00
|
|
|
INSIST(!ISC_QLINK_LINKED(client, ilink));
|
2001-11-14 22:00:22 +00:00
|
|
|
|
2015-02-03 11:42:58 +05:30
|
|
|
if (manager != NULL) {
|
|
|
|
LOCK(&manager->listlock);
|
|
|
|
ISC_LIST_UNLINK(manager->clients, client, link);
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
if (manager->exiting &&
|
|
|
|
ISC_LIST_EMPTY(manager->clients))
|
|
|
|
destroy_manager = ISC_TRUE;
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
UNLOCK(&manager->listlock);
|
|
|
|
}
|
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
ns_query_free(client);
|
|
|
|
isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
|
|
|
|
isc_event_free((isc_event_t **)&client->sendevent);
|
|
|
|
isc_event_free((isc_event_t **)&client->recvevent);
|
|
|
|
isc_timer_detach(&client->timer);
|
2012-09-29 13:07:09 +10:00
|
|
|
if (client->delaytimer != NULL)
|
|
|
|
isc_timer_detach(&client->delaytimer);
|
2001-11-14 22:00:22 +00:00
|
|
|
|
|
|
|
if (client->tcpbuf != NULL)
|
2011-10-10 22:57:14 +00:00
|
|
|
isc_mem_put(client->mctx, client->tcpbuf,
|
|
|
|
TCP_BUFFER_SIZE);
|
2001-11-14 22:00:22 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
INSIST(dns_rdataset_isassociated(client->opt));
|
|
|
|
dns_rdataset_disassociate(client->opt);
|
2011-10-10 22:57:14 +00:00
|
|
|
dns_message_puttemprdataset(client->message,
|
|
|
|
&client->opt);
|
2001-11-14 22:00:22 +00:00
|
|
|
}
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
dns_message_destroy(&client->message);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
/*
|
|
|
|
* Detaching the task must be done after unlinking from
|
|
|
|
* the manager's lists because the manager accesses
|
|
|
|
* client->task.
|
|
|
|
*/
|
|
|
|
if (client->task != NULL)
|
|
|
|
isc_task_detach(&client->task);
|
|
|
|
|
|
|
|
CTRACE("free");
|
|
|
|
client->magic = 0;
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2008-01-02 05:03:07 +00:00
|
|
|
/*
|
|
|
|
* Check that there are no other external references to
|
|
|
|
* the memory context.
|
|
|
|
*/
|
|
|
|
if (ns_g_clienttest && isc_mem_references(client->mctx) != 1) {
|
|
|
|
isc_mem_stats(client->mctx, stderr);
|
|
|
|
INSIST(0);
|
|
|
|
}
|
2015-02-03 11:42:58 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the fetchlock mutex that was created in
|
|
|
|
* ns_query_init().
|
|
|
|
*/
|
|
|
|
DESTROYLOCK(&client->query.fetchlock);
|
|
|
|
|
2005-06-04 05:32:50 +00:00
|
|
|
isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
if (destroy_manager && manager != NULL)
|
|
|
|
clientmgr_destroy(manager);
|
2001-11-14 22:00:22 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-07-26 17:39:12 +00:00
|
|
|
* The client's task has received the client's control event
|
|
|
|
* as part of the startup process.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
client_start(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client = (ns_client_t *) event->ev_arg;
|
|
|
|
|
|
|
|
INSIST(task == client->task);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
2001-11-14 22:00:22 +00:00
|
|
|
INSIST(client->nctls == 1);
|
|
|
|
client->nctls--;
|
|
|
|
|
|
|
|
if (exit_check(client))
|
|
|
|
return;
|
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
2015-01-20 16:10:30 -08:00
|
|
|
if (client->pipelined) {
|
|
|
|
client_read(client);
|
|
|
|
} else {
|
|
|
|
client_accept(client);
|
|
|
|
}
|
2000-07-26 17:39:12 +00:00
|
|
|
} else {
|
2001-01-27 02:08:07 +00:00
|
|
|
client_udprecv(client);
|
2000-07-26 17:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-12-22 16:59:05 +00:00
|
|
|
* The client's task has received a shutdown event.
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
|
|
|
client_shutdown(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(event != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
|
|
|
|
client = event->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
2000-05-02 01:16:21 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("shutdown");
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
if (client->shutdown != NULL) {
|
|
|
|
(client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
|
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:19:30 +10:00
|
|
|
if (ISC_QLINK_LINKED(client, ilink))
|
|
|
|
ISC_QUEUE_UNLINK(client->manager->inactive, client, ilink);
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
client->newstate = NS_CLIENTSTATE_FREED;
|
2008-01-02 05:03:07 +00:00
|
|
|
client->needshutdown = ISC_FALSE;
|
2000-04-17 19:22:44 +00:00
|
|
|
(void)exit_check(client);
|
2000-02-10 22:16:56 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
static void
|
|
|
|
ns_client_endrequest(ns_client_t *client) {
|
2000-01-28 23:35:53 +00:00
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
INSIST(client->nreads == 0);
|
|
|
|
INSIST(client->nsends == 0);
|
2001-01-27 02:08:07 +00:00
|
|
|
INSIST(client->nrecvs == 0);
|
2004-07-23 02:57:27 +00:00
|
|
|
INSIST(client->nupdates == 0);
|
2011-10-10 22:57:14 +00:00
|
|
|
INSIST(client->state == NS_CLIENTSTATE_WORKING ||
|
|
|
|
client->state == NS_CLIENTSTATE_RECURSING);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-05-02 01:16:21 +00:00
|
|
|
CTRACE("endrequest");
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
if (client->next != NULL) {
|
2000-02-10 22:16:56 +00:00
|
|
|
(client->next)(client);
|
1999-08-03 01:21:00 +00:00
|
|
|
client->next = NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-05 11:46:11 +02:00
|
|
|
if (client->view != NULL) {
|
|
|
|
#ifdef ENABLE_AFL
|
|
|
|
if (ns_g_fuzz_type == ns_fuzz_resolver) {
|
|
|
|
dns_cache_clean(client->view->cache, INT_MAX);
|
|
|
|
dns_adb_flush(client->view->adb);
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-05 22:14:43 +00:00
|
|
|
dns_view_detach(&client->view);
|
2016-05-05 11:46:11 +02:00
|
|
|
}
|
1999-11-24 21:05:45 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
INSIST(dns_rdataset_isassociated(client->opt));
|
|
|
|
dns_rdataset_disassociate(client->opt);
|
|
|
|
dns_message_puttemprdataset(client->message, &client->opt);
|
|
|
|
}
|
2000-01-07 19:20:25 +00:00
|
|
|
|
2011-05-05 23:44:52 +00:00
|
|
|
client->signer = NULL;
|
1999-10-07 19:43:18 +00:00
|
|
|
client->udpsize = 512;
|
2000-11-13 21:34:03 +00:00
|
|
|
client->extflags = 0;
|
2005-06-07 00:16:01 +00:00
|
|
|
client->ednsversion = -1;
|
1999-07-24 01:17:44 +00:00
|
|
|
dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2012-05-14 10:06:05 -07:00
|
|
|
if (client->recursionquota != NULL) {
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_quota_detach(&client->recursionquota);
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_stats_decrement(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_recursclients);
|
|
|
|
}
|
2000-07-17 23:19:14 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Clear all client attributes that are specific to
|
2000-07-17 23:19:14 +00:00
|
|
|
* the request; that's all except the TCP flag.
|
|
|
|
*/
|
|
|
|
client->attributes &= NS_CLIENTATTR_TCP;
|
2016-05-05 11:46:11 +02:00
|
|
|
#ifdef ENABLE_AFL
|
|
|
|
if (ns_g_fuzz_type == ns_fuzz_client ||
|
|
|
|
ns_g_fuzz_type == ns_fuzz_tcpclient ||
|
|
|
|
ns_g_fuzz_type == ns_fuzz_resolver) {
|
|
|
|
named_fuzz_notify();
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_AFL */
|
|
|
|
|
2000-03-06 17:35:13 +00:00
|
|
|
}
|
2000-01-15 00:36:26 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
void
|
|
|
|
ns_client_next(ns_client_t *client, isc_result_t result) {
|
|
|
|
int newstate;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
2003-07-04 04:38:54 +00:00
|
|
|
REQUIRE(client->state == NS_CLIENTSTATE_WORKING ||
|
2011-10-10 22:57:14 +00:00
|
|
|
client->state == NS_CLIENTSTATE_RECURSING ||
|
2003-07-04 04:38:54 +00:00
|
|
|
client->state == NS_CLIENTSTATE_READING);
|
2000-05-02 01:16:21 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
CTRACE("next");
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-22 16:03:31 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
2000-02-10 22:16:56 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request failed: %s", isc_result_totext(result));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
|
|
|
|
newstate = NS_CLIENTSTATE_READING;
|
|
|
|
else
|
|
|
|
newstate = NS_CLIENTSTATE_READY;
|
|
|
|
|
|
|
|
if (client->newstate > newstate)
|
|
|
|
client->newstate = newstate;
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)exit_check(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
|
|
|
client_senddone(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
2000-01-28 23:35:53 +00:00
|
|
|
isc_socketevent_t *sevent = (isc_socketevent_t *) event;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(sevent != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
|
|
|
|
client = sevent->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
2001-02-12 21:45:37 +00:00
|
|
|
REQUIRE(sevent == client->sendevent);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("senddone");
|
|
|
|
|
2000-05-22 16:03:31 +00:00
|
|
|
if (sevent->result != ISC_R_SUCCESS)
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
|
|
|
|
"error sending response: %s",
|
|
|
|
isc_result_totext(sevent->result));
|
|
|
|
|
1999-07-24 03:02:57 +00:00
|
|
|
INSIST(client->nsends > 0);
|
1999-07-24 01:17:44 +00:00
|
|
|
client->nsends--;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-06-22 00:05:11 +00:00
|
|
|
if (client->tcpbuf != NULL) {
|
2000-06-22 02:21:06 +00:00
|
|
|
INSIST(TCP_CLIENT(client));
|
2000-06-22 00:05:11 +00:00
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
client->tcpbuf = NULL;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2000-01-28 23:35:53 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2001-03-06 04:18:42 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
static isc_result_t
|
|
|
|
client_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
|
|
|
|
isc_buffer_t *tcpbuffer, isc_uint32_t length,
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned char *sendbuf, unsigned char **datap)
|
2000-09-13 03:15:01 +00:00
|
|
|
{
|
2000-09-13 01:30:34 +00:00
|
|
|
unsigned char *data;
|
2000-09-13 03:15:01 +00:00
|
|
|
isc_uint32_t bufsize;
|
|
|
|
isc_result_t result;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
INSIST(datap != NULL);
|
2001-03-06 04:18:42 +00:00
|
|
|
INSIST((tcpbuffer == NULL && length != 0) ||
|
|
|
|
(tcpbuffer != NULL && length == 0));
|
2000-09-13 01:30:34 +00:00
|
|
|
|
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
INSIST(client->tcpbuf == NULL);
|
2001-03-06 04:18:42 +00:00
|
|
|
if (length + 2 > TCP_BUFFER_SIZE) {
|
2000-09-13 01:30:34 +00:00
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
|
|
|
|
if (client->tcpbuf == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
data = client->tcpbuf;
|
2000-09-13 03:15:01 +00:00
|
|
|
if (tcpbuffer != NULL) {
|
|
|
|
isc_buffer_init(tcpbuffer, data, TCP_BUFFER_SIZE);
|
|
|
|
isc_buffer_init(buffer, data + 2, TCP_BUFFER_SIZE - 2);
|
|
|
|
} else {
|
|
|
|
isc_buffer_init(buffer, data, TCP_BUFFER_SIZE);
|
2001-03-12 22:27:15 +00:00
|
|
|
INSIST(length <= 0xffff);
|
|
|
|
isc_buffer_putuint16(buffer, (isc_uint16_t)length);
|
2000-09-13 03:15:01 +00:00
|
|
|
}
|
2000-09-13 01:30:34 +00:00
|
|
|
} else {
|
2001-03-06 01:24:40 +00:00
|
|
|
data = sendbuf;
|
2015-07-06 09:44:24 +10:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_HAVECOOKIE) == 0) {
|
2014-02-19 12:53:42 +11:00
|
|
|
if (client->view != NULL)
|
2015-07-06 09:44:24 +10:00
|
|
|
bufsize = client->view->nocookieudp;
|
2014-02-19 12:53:42 +11:00
|
|
|
else
|
|
|
|
bufsize = 512;
|
|
|
|
} else
|
|
|
|
bufsize = client->udpsize;
|
|
|
|
if (bufsize > client->udpsize)
|
|
|
|
bufsize = client->udpsize;
|
|
|
|
if (bufsize > SEND_BUFFER_SIZE)
|
|
|
|
bufsize = SEND_BUFFER_SIZE;
|
2001-03-06 04:18:42 +00:00
|
|
|
if (length > bufsize) {
|
2000-09-13 01:30:34 +00:00
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
goto done;
|
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
isc_buffer_init(buffer, data, bufsize);
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
*datap = data;
|
|
|
|
result = ISC_R_SUCCESS;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
done:
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
client_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
|
|
|
|
struct in6_pktinfo *pktinfo;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_region_t r;
|
|
|
|
isc_sockaddr_t *address;
|
2015-02-27 10:55:55 +11:00
|
|
|
isc_socket_t *sock;
|
2000-11-03 02:45:55 +00:00
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
int match;
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned int sockflags = ISC_SOCKFLAG_IMMEDIATE;
|
2013-03-22 12:27:54 -07:00
|
|
|
isc_dscp_t dispdscp = -1;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
|
|
|
if (TCP_CLIENT(client)) {
|
2015-02-27 10:55:55 +11:00
|
|
|
sock = client->tcpsocket;
|
2000-09-13 01:30:34 +00:00
|
|
|
address = NULL;
|
|
|
|
} else {
|
2015-02-27 10:55:55 +11:00
|
|
|
sock = client->udpsocket;
|
2001-01-27 02:08:07 +00:00
|
|
|
address = &client->peeraddr;
|
2000-11-03 02:45:55 +00:00
|
|
|
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
if (ns_g_server->blackholeacl != NULL &&
|
|
|
|
dns_acl_match(&netaddr, NULL,
|
2007-03-29 06:36:31 +00:00
|
|
|
ns_g_server->blackholeacl,
|
2001-03-26 21:33:07 +00:00
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL) == ISC_R_SUCCESS &&
|
2000-11-03 02:45:55 +00:00
|
|
|
match > 0)
|
|
|
|
return (DNS_R_BLACKHOLED);
|
2001-03-06 01:24:40 +00:00
|
|
|
sockflags |= ISC_SOCKFLAG_NORETRY;
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
|
2001-10-09 02:39:03 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0 &&
|
2001-10-13 00:44:24 +00:00
|
|
|
(client->attributes & NS_CLIENTATTR_MULTICAST) == 0)
|
2000-09-13 01:30:34 +00:00
|
|
|
pktinfo = &client->pktinfo;
|
|
|
|
else
|
|
|
|
pktinfo = NULL;
|
2000-09-13 03:15:01 +00:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
if (client->dispatch != NULL) {
|
|
|
|
dispdscp = dns_dispatch_getdscp(client->dispatch);
|
|
|
|
if (dispdscp != -1)
|
|
|
|
client->dscp = dispdscp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->dscp == -1) {
|
|
|
|
client->sendevent->attributes &= ~ISC_SOCKEVENTATTR_DSCP;
|
|
|
|
client->sendevent->dscp = 0;
|
|
|
|
} else {
|
|
|
|
client->sendevent->attributes |= ISC_SOCKEVENTATTR_DSCP;
|
|
|
|
client->sendevent->dscp = client->dscp;
|
|
|
|
}
|
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
isc_buffer_usedregion(buffer, &r);
|
|
|
|
|
|
|
|
CTRACE("sendto");
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2015-02-27 10:55:55 +11:00
|
|
|
result = isc_socket_sendto2(sock, &r, client->task,
|
2001-02-12 21:45:37 +00:00
|
|
|
address, pktinfo,
|
2001-03-06 01:24:40 +00:00
|
|
|
client->sendevent, sockflags);
|
2001-02-12 21:45:37 +00:00
|
|
|
if (result == ISC_R_SUCCESS || result == ISC_R_INPROGRESS) {
|
2000-09-13 01:30:34 +00:00
|
|
|
client->nsends++;
|
2001-02-12 21:45:37 +00:00
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
client_senddone(client->task,
|
|
|
|
(isc_event_t *)client->sendevent);
|
2001-02-14 01:46:59 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
2000-09-13 03:15:01 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_sendraw(ns_client_t *client, dns_message_t *message) {
|
|
|
|
isc_result_t result;
|
|
|
|
unsigned char *data;
|
|
|
|
isc_buffer_t buffer;
|
|
|
|
isc_region_t r;
|
|
|
|
isc_region_t *mr;
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned char sendbuf[SEND_BUFFER_SIZE];
|
2000-09-13 03:15:01 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("sendraw");
|
|
|
|
|
|
|
|
mr = dns_message_getrawmessage(message);
|
|
|
|
if (mr == NULL) {
|
|
|
|
result = ISC_R_UNEXPECTEDEND;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2001-03-06 01:24:40 +00:00
|
|
|
result = client_allocsendbuf(client, &buffer, NULL, mr->length,
|
|
|
|
sendbuf, &data);
|
2000-09-13 03:15:01 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy message to buffer and fixup id.
|
|
|
|
*/
|
|
|
|
isc_buffer_availableregion(&buffer, &r);
|
|
|
|
result = isc_buffer_copyregion(&buffer, mr);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
r.base[0] = (client->message->id >> 8) & 0xff;
|
|
|
|
r.base[1] = client->message->id & 0xff;
|
|
|
|
|
|
|
|
result = client_sendpkg(client, &buffer);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
return;
|
2000-09-13 01:30:34 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (client->tcpbuf != NULL) {
|
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
client->tcpbuf = NULL;
|
|
|
|
}
|
|
|
|
ns_client_next(client, result);
|
|
|
|
}
|
|
|
|
|
2012-09-29 13:07:09 +10:00
|
|
|
static void
|
|
|
|
client_send(ns_client_t *client) {
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t result;
|
|
|
|
unsigned char *data;
|
|
|
|
isc_buffer_t buffer;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_t tcpbuffer;
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_region_t r;
|
2001-03-05 21:15:47 +00:00
|
|
|
dns_compress_t cctx;
|
|
|
|
isc_boolean_t cleanup_cctx = ISC_FALSE;
|
2001-03-06 01:24:40 +00:00
|
|
|
unsigned char sendbuf[SEND_BUFFER_SIZE];
|
2009-10-26 23:14:54 +00:00
|
|
|
unsigned int render_opts;
|
2002-04-26 00:40:37 +00:00
|
|
|
unsigned int preferred_glue;
|
2008-04-03 05:55:52 +00:00
|
|
|
isc_boolean_t opt_included = ISC_FALSE;
|
2015-07-06 22:20:03 -07:00
|
|
|
size_t respsize;
|
2015-10-02 12:32:42 -07:00
|
|
|
#ifdef HAVE_DNSTAP
|
|
|
|
unsigned char zone[DNS_NAME_MAXWIRE];
|
|
|
|
dns_dtmsgtype_t dtmsgtype;
|
|
|
|
isc_region_t zr;
|
|
|
|
#endif /* HAVE_DNSTAP */
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("send");
|
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_RA) != 0)
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_RA;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2002-01-23 08:46:40 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_WANTDNSSEC) != 0)
|
2009-10-26 23:14:54 +00:00
|
|
|
render_opts = 0;
|
2002-01-23 08:46:40 +00:00
|
|
|
else
|
2009-10-26 23:14:54 +00:00
|
|
|
render_opts = DNS_MESSAGERENDER_OMITDNSSEC;
|
2012-01-31 06:58:39 +00:00
|
|
|
|
|
|
|
preferred_glue = 0;
|
|
|
|
if (client->view != NULL) {
|
|
|
|
if (client->view->preferred_glue == dns_rdatatype_a)
|
|
|
|
preferred_glue = DNS_MESSAGERENDER_PREFER_A;
|
|
|
|
else if (client->view->preferred_glue == dns_rdatatype_aaaa)
|
|
|
|
preferred_glue = DNS_MESSAGERENDER_PREFER_AAAA;
|
|
|
|
}
|
2015-09-11 13:27:58 +10:00
|
|
|
if (preferred_glue == 0) {
|
|
|
|
if (isc_sockaddr_pf(&client->peeraddr) == AF_INET)
|
|
|
|
preferred_glue = DNS_MESSAGERENDER_PREFER_A;
|
|
|
|
else
|
|
|
|
preferred_glue = DNS_MESSAGERENDER_PREFER_AAAA;
|
|
|
|
}
|
2012-01-31 06:58:39 +00:00
|
|
|
|
2012-05-14 11:50:00 -07:00
|
|
|
#ifdef ALLOW_FILTER_AAAA
|
2009-10-26 23:14:54 +00:00
|
|
|
/*
|
|
|
|
* filter-aaaa-on-v4 yes or break-dnssec option to suppress
|
2012-05-14 11:50:00 -07:00
|
|
|
* AAAA records.
|
|
|
|
*
|
2009-10-26 23:14:54 +00:00
|
|
|
* 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.
|
2012-01-31 06:58:39 +00:00
|
|
|
*
|
|
|
|
* Override preferred glue if necessary.
|
2009-10-26 23:14:54 +00:00
|
|
|
*/
|
2012-01-31 06:58:39 +00:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_FILTER_AAAA) != 0) {
|
2009-10-26 23:14:54 +00:00
|
|
|
render_opts |= DNS_MESSAGERENDER_FILTER_AAAA;
|
2012-01-31 06:58:39 +00:00
|
|
|
if (preferred_glue == DNS_MESSAGERENDER_PREFER_AAAA)
|
2002-04-29 23:43:46 +00:00
|
|
|
preferred_glue = DNS_MESSAGERENDER_PREFER_A;
|
|
|
|
}
|
2012-01-31 06:58:39 +00:00
|
|
|
#endif
|
2002-04-26 00:40:37 +00:00
|
|
|
|
2014-02-20 14:56:20 +11:00
|
|
|
/*
|
|
|
|
* Create an OPT for our reply.
|
|
|
|
*/
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_WANTOPT) != 0) {
|
2014-02-20 17:51:31 +11:00
|
|
|
result = ns_client_addopt(client, client->message,
|
|
|
|
&client->opt);
|
2014-02-20 14:56:20 +11:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
/*
|
2000-10-13 23:38:45 +00:00
|
|
|
* XXXRTH The following doesn't deal with TCP buffer resizing.
|
1999-07-28 02:20:36 +00:00
|
|
|
*/
|
2001-03-06 01:24:40 +00:00
|
|
|
result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0,
|
|
|
|
sendbuf, &data);
|
2000-09-13 03:15:01 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-08-05 01:51:32 +00:00
|
|
|
|
2001-03-05 21:15:47 +00:00
|
|
|
result = dns_compress_init(&cctx, -1, client->mctx);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
2014-02-06 19:37:26 -08:00
|
|
|
if (client->peeraddr_valid && client->view != NULL) {
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
dns_name_t *name = NULL;
|
|
|
|
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
if (client->message->tsigkey != NULL)
|
|
|
|
name = &client->message->tsigkey->name;
|
2015-11-05 12:19:04 +01:00
|
|
|
|
2014-02-06 19:37:26 -08:00
|
|
|
if (client->view->nocasecompress == NULL ||
|
2014-08-28 22:05:57 -07:00
|
|
|
!allowed(&netaddr, name, NULL, 0, NULL,
|
|
|
|
client->view->nocasecompress))
|
2014-02-06 19:37:26 -08:00
|
|
|
{
|
|
|
|
dns_compress_setsensitive(&cctx, ISC_TRUE);
|
|
|
|
}
|
2015-11-05 12:19:04 +01:00
|
|
|
|
|
|
|
if (client->view->msgcompression == ISC_FALSE) {
|
|
|
|
dns_compress_disable(&cctx);
|
|
|
|
}
|
2014-02-06 19:37:26 -08:00
|
|
|
}
|
2001-03-05 21:15:47 +00:00
|
|
|
cleanup_cctx = ISC_TRUE;
|
|
|
|
|
|
|
|
result = dns_message_renderbegin(client->message, &cctx, &buffer);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
2008-04-03 02:01:08 +00:00
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
if (client->opt != NULL) {
|
|
|
|
result = dns_message_setopt(client->message, client->opt);
|
2008-04-03 05:55:52 +00:00
|
|
|
opt_included = ISC_TRUE;
|
1999-11-24 21:05:45 +00:00
|
|
|
client->opt = NULL;
|
2001-02-14 02:51:12 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_rendersection(client->message,
|
1999-12-22 03:22:59 +00:00
|
|
|
DNS_SECTION_QUESTION, 0);
|
2001-01-08 20:36:14 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
2013-02-25 10:49:30 -08:00
|
|
|
/*
|
|
|
|
* Stop after the question if TC was set for rate limiting.
|
|
|
|
*/
|
|
|
|
if ((client->message->flags & DNS_MESSAGEFLAG_TC) != 0)
|
|
|
|
goto renderend;
|
1999-07-28 02:20:36 +00:00
|
|
|
result = dns_message_rendersection(client->message,
|
2001-06-05 09:02:16 +00:00
|
|
|
DNS_SECTION_ANSWER,
|
2002-01-23 08:46:40 +00:00
|
|
|
DNS_MESSAGERENDER_PARTIAL |
|
2009-10-26 23:14:54 +00:00
|
|
|
render_opts);
|
1999-09-01 18:25:05 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
result = dns_message_rendersection(client->message,
|
2001-06-05 09:02:16 +00:00
|
|
|
DNS_SECTION_AUTHORITY,
|
2002-01-23 08:46:40 +00:00
|
|
|
DNS_MESSAGERENDER_PARTIAL |
|
2009-10-26 23:14:54 +00:00
|
|
|
render_opts);
|
1999-09-01 18:25:05 +00:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
client->message->flags |= DNS_MESSAGEFLAG_TC;
|
|
|
|
goto renderend;
|
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
result = dns_message_rendersection(client->message,
|
2002-01-23 08:46:40 +00:00
|
|
|
DNS_SECTION_ADDITIONAL,
|
2009-10-26 23:14:54 +00:00
|
|
|
preferred_glue | render_opts);
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
|
|
|
|
goto done;
|
1999-09-01 18:25:05 +00:00
|
|
|
renderend:
|
1999-07-24 01:17:44 +00:00
|
|
|
result = dns_message_renderend(client->message);
|
2000-05-26 22:43:36 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto done;
|
1999-08-05 01:51:32 +00:00
|
|
|
|
2015-10-02 12:32:42 -07:00
|
|
|
#ifdef HAVE_DNSTAP
|
|
|
|
memset(&zr, 0, sizeof(zr));
|
|
|
|
if (((client->message->flags & DNS_MESSAGEFLAG_AA) != 0) &&
|
|
|
|
(client->query.authzone != NULL))
|
|
|
|
{
|
|
|
|
isc_buffer_t b;
|
|
|
|
dns_name_t *zo =
|
|
|
|
dns_zone_getorigin(client->query.authzone);
|
|
|
|
|
|
|
|
isc_buffer_init(&b, zone, sizeof(zone));
|
|
|
|
dns_compress_setmethods(&cctx, DNS_COMPRESS_NONE);
|
|
|
|
result = dns_name_towire(zo, &cctx, &b);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
isc_buffer_usedregion(&b, &zr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((client->message->flags & DNS_MESSAGEFLAG_RD) != 0)
|
|
|
|
dtmsgtype = DNS_DTTYPE_CR;
|
|
|
|
else
|
|
|
|
dtmsgtype = DNS_DTTYPE_AR;
|
|
|
|
#endif /* HAVE_DNSTAP */
|
|
|
|
|
2001-03-05 21:15:47 +00:00
|
|
|
if (cleanup_cctx) {
|
|
|
|
dns_compress_invalidate(&cctx);
|
|
|
|
cleanup_cctx = ISC_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(&buffer, &r);
|
2000-02-10 22:16:56 +00:00
|
|
|
isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_add(&tcpbuffer, r.length);
|
2015-07-06 22:20:03 -07:00
|
|
|
|
2015-10-02 12:32:42 -07:00
|
|
|
#ifdef HAVE_DNSTAP
|
|
|
|
if (client->view != NULL) {
|
|
|
|
dns_dt_send(client->view, dtmsgtype,
|
|
|
|
&client->peeraddr, ISC_TRUE, &zr,
|
|
|
|
&client->requesttime, NULL, &buffer);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_DNSTAP */
|
|
|
|
|
2016-07-13 16:56:11 +10:00
|
|
|
/* don't count the 2-octet length header */
|
|
|
|
respsize = isc_buffer_usedlength(&tcpbuffer) - 2;
|
|
|
|
result = client_sendpkg(client, &tcpbuffer);
|
2016-06-22 23:45:21 +00:00
|
|
|
|
2016-06-23 08:44:54 +10:00
|
|
|
switch (isc_sockaddr_pf(&client->peeraddr)) {
|
|
|
|
case AF_INET:
|
|
|
|
isc_stats_increment(ns_g_server->tcpoutstats4,
|
|
|
|
ISC_MIN((int)respsize / 16, 256));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
isc_stats_increment(ns_g_server->tcpoutstats6,
|
|
|
|
ISC_MIN((int)respsize / 16, 256));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-06 22:20:03 -07:00
|
|
|
} else {
|
|
|
|
respsize = isc_buffer_usedlength(&buffer);
|
2000-09-13 03:15:01 +00:00
|
|
|
result = client_sendpkg(client, &buffer);
|
2008-04-03 05:55:52 +00:00
|
|
|
|
2015-10-02 12:32:42 -07:00
|
|
|
#ifdef HAVE_DNSTAP
|
|
|
|
if (client->view != NULL) {
|
|
|
|
dns_dt_send(client->view, dtmsgtype,
|
|
|
|
&client->peeraddr, ISC_FALSE, &zr,
|
|
|
|
&client->requesttime, NULL, &buffer);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_DNSTAP */
|
|
|
|
|
2016-06-23 08:44:54 +10:00
|
|
|
switch (isc_sockaddr_pf(&client->peeraddr)) {
|
|
|
|
case AF_INET:
|
|
|
|
isc_stats_increment(ns_g_server->udpoutstats4,
|
|
|
|
ISC_MIN((int)respsize / 16, 256));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
isc_stats_increment(ns_g_server->udpoutstats6,
|
|
|
|
ISC_MIN((int)respsize / 16, 256));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-06 22:20:03 -07:00
|
|
|
}
|
|
|
|
|
2008-04-03 05:55:52 +00:00
|
|
|
/* update statistics (XXXJT: is it okay to access message->xxxkey?) */
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats, dns_nsstatscounter_response);
|
2015-07-06 22:20:03 -07:00
|
|
|
|
2016-06-23 08:44:54 +10:00
|
|
|
dns_rcodestats_increment(ns_g_server->rcodestats,
|
|
|
|
client->message->rcode);
|
2008-04-03 05:55:52 +00:00
|
|
|
if (opt_included) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_edns0out);
|
2008-04-03 05:55:52 +00:00
|
|
|
}
|
|
|
|
if (client->message->tsigkey != NULL) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_tsigout);
|
2008-04-03 05:55:52 +00:00
|
|
|
}
|
|
|
|
if (client->message->sig0key != NULL) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_sig0out);
|
2008-04-03 05:55:52 +00:00
|
|
|
}
|
|
|
|
if ((client->message->flags & DNS_MESSAGEFLAG_TC) != 0)
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_truncatedresp);
|
2008-04-03 05:55:52 +00:00
|
|
|
|
2000-09-13 03:15:01 +00:00
|
|
|
if (result == ISC_R_SUCCESS)
|
2000-01-28 23:35:53 +00:00
|
|
|
return;
|
2000-06-22 00:05:11 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
done:
|
2000-06-22 00:05:11 +00:00
|
|
|
if (client->tcpbuf != NULL) {
|
|
|
|
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
|
|
|
|
client->tcpbuf = NULL;
|
|
|
|
}
|
2001-03-05 21:15:47 +00:00
|
|
|
|
|
|
|
if (cleanup_cctx)
|
|
|
|
dns_compress_invalidate(&cctx);
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_client_next(client, result);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2012-09-29 13:07:09 +10:00
|
|
|
/*
|
|
|
|
* Completes the sending of a delayed client response.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
client_delay(isc_task_t *task, isc_event_t *event) {
|
2012-09-29 23:46:01 +00:00
|
|
|
ns_client_t *client;
|
2012-09-29 13:07:09 +10:00
|
|
|
|
2012-09-29 23:46:01 +00:00
|
|
|
REQUIRE(event != NULL);
|
|
|
|
REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE ||
|
|
|
|
event->ev_type == ISC_TIMEREVENT_IDLE);
|
|
|
|
client = event->ev_arg;
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
REQUIRE(client->delaytimer != NULL);
|
2012-09-29 13:07:09 +10:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
|
|
|
CTRACE("client_delay");
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
isc_timer_detach(&client->delaytimer);
|
|
|
|
|
|
|
|
client_send(client);
|
|
|
|
ns_client_detach(&client);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_send(ns_client_t *client) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delay the response by ns_g_delay ms.
|
|
|
|
*/
|
|
|
|
if (ns_g_delay != 0) {
|
|
|
|
ns_client_t *dummy = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_interval_t interval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace ourselves if we have not already been replaced.
|
|
|
|
*/
|
|
|
|
if (!client->mortal) {
|
|
|
|
result = ns_client_replace(client);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto nodelay;
|
|
|
|
}
|
2012-09-29 23:46:01 +00:00
|
|
|
|
2012-09-29 13:07:09 +10:00
|
|
|
ns_client_attach(client, &dummy);
|
|
|
|
if (ns_g_delay >= 1000)
|
|
|
|
isc_interval_set(&interval, ns_g_delay / 1000,
|
|
|
|
(ns_g_delay % 1000) * 1000000);
|
|
|
|
else
|
|
|
|
isc_interval_set(&interval, 0, ns_g_delay * 1000000);
|
|
|
|
result = isc_timer_create(client->manager->timermgr,
|
|
|
|
isc_timertype_once, NULL, &interval,
|
|
|
|
client->task, client_delay,
|
|
|
|
client, &client->delaytimer);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ns_client_detach(&dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
nodelay:
|
|
|
|
client_send(client);
|
|
|
|
}
|
|
|
|
|
2006-01-04 05:06:10 +00:00
|
|
|
#if NS_CLIENT_DROPPORT
|
|
|
|
#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
|
|
|
|
ns_client_dropport(in_port_t port) {
|
|
|
|
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
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
void
|
|
|
|
ns_client_error(ns_client_t *client, isc_result_t result) {
|
|
|
|
dns_rcode_t rcode;
|
1999-08-03 01:21:00 +00:00
|
|
|
dns_message_t *message;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
|
|
|
|
CTRACE("error");
|
|
|
|
|
1999-08-03 01:21:00 +00:00
|
|
|
message = client->message;
|
1999-07-24 01:17:44 +00:00
|
|
|
rcode = dns_result_torcode(result);
|
|
|
|
|
2006-01-04 05:06:10 +00:00
|
|
|
#if NS_CLIENT_DROPPORT
|
|
|
|
/*
|
|
|
|
* Don't send FORMERR to ports on the drop port list.
|
|
|
|
*/
|
|
|
|
if (rcode == dns_rcode_formerr &&
|
|
|
|
ns_client_dropport(isc_sockaddr_getport(&client->peeraddr)) !=
|
|
|
|
DROPPORT_NO) {
|
|
|
|
char buf[64];
|
|
|
|
isc_buffer_t b;
|
|
|
|
|
|
|
|
isc_buffer_init(&b, buf, sizeof(buf) - 1);
|
|
|
|
if (dns_rcode_totext(rcode, &b) != ISC_R_SUCCESS)
|
|
|
|
isc_buffer_putstr(&b, "UNKNOWN RCODE");
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"dropped error (%.*s) response: suspicious port",
|
|
|
|
(int)isc_buffer_usedlength(&b), buf);
|
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-25 10:49:30 -08:00
|
|
|
/*
|
|
|
|
* Try to rate limit error responses.
|
|
|
|
*/
|
|
|
|
if (client->view != NULL && client->view->rrl != NULL) {
|
|
|
|
isc_boolean_t wouldlog;
|
|
|
|
char log_buf[DNS_RRL_LOG_BUF_LEN];
|
|
|
|
dns_rrl_result_t rrl_result;
|
2015-02-25 16:44:43 -08:00
|
|
|
int loglevel;
|
2013-02-25 10:49:30 -08:00
|
|
|
|
|
|
|
INSIST(rcode != dns_rcode_noerror &&
|
|
|
|
rcode != dns_rcode_nxdomain);
|
2015-02-25 16:44:43 -08:00
|
|
|
if (ns_g_server->log_queries)
|
|
|
|
loglevel = DNS_RRL_LOG_DROP;
|
|
|
|
else
|
|
|
|
loglevel = ISC_LOG_DEBUG(1);
|
|
|
|
wouldlog = isc_log_wouldlog(ns_g_lctx, loglevel);
|
2013-02-25 10:49:30 -08:00
|
|
|
rrl_result = dns_rrl(client->view, &client->peeraddr,
|
|
|
|
TCP_CLIENT(client),
|
|
|
|
dns_rdataclass_in, dns_rdatatype_none,
|
|
|
|
NULL, result, client->now,
|
|
|
|
wouldlog, log_buf, sizeof(log_buf));
|
|
|
|
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) {
|
2013-05-21 12:20:54 -07:00
|
|
|
ns_client_log(client,
|
2015-09-29 15:02:49 +10:00
|
|
|
NS_LOGCATEGORY_QUERY_ERRORS,
|
2013-02-25 10:49:30 -08:00
|
|
|
NS_LOGMODULE_CLIENT,
|
2015-02-25 16:44:43 -08:00
|
|
|
loglevel,
|
2013-02-25 10:49:30 -08:00
|
|
|
"%s", log_buf);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Some error responses cannot be 'slipped',
|
2013-04-25 14:40:32 -07:00
|
|
|
* so don't try to slip any error responses.
|
2013-02-25 10:49:30 -08:00
|
|
|
*/
|
|
|
|
if (!client->view->rrl->log_only) {
|
2013-04-25 14:40:32 -07:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_ratedropped);
|
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_dropped);
|
2013-02-25 10:49:30 -08:00
|
|
|
ns_client_next(client, DNS_R_DROP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
/*
|
2000-06-22 23:04:27 +00:00
|
|
|
* Message may be an in-progress reply that we had trouble
|
1999-07-28 02:20:36 +00:00
|
|
|
* with, in which case QR will be set. We need to clear QR before
|
|
|
|
* calling dns_message_reply() to avoid triggering an assertion.
|
|
|
|
*/
|
1999-08-03 01:21:00 +00:00
|
|
|
message->flags &= ~DNS_MESSAGEFLAG_QR;
|
1999-10-07 19:43:18 +00:00
|
|
|
/*
|
|
|
|
* AA and AD shouldn't be set.
|
|
|
|
*/
|
|
|
|
message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
|
1999-08-03 01:21:00 +00:00
|
|
|
result = dns_message_reply(message, ISC_TRUE);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1999-08-03 01:21:00 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
result = dns_message_reply(message, ISC_FALSE);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_next(client, result);
|
|
|
|
return;
|
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-08-03 01:21:00 +00:00
|
|
|
message->rcode = rcode;
|
2001-03-19 20:52:21 +00:00
|
|
|
|
|
|
|
if (rcode == dns_rcode_formerr) {
|
2014-09-03 23:28:14 -07:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-03-19 20:52:21 +00:00
|
|
|
if (isc_sockaddr_equal(&client->peeraddr,
|
|
|
|
&client->formerrcache.addr) &&
|
|
|
|
message->id == client->formerrcache.id &&
|
2015-10-02 12:32:42 -07:00
|
|
|
(isc_time_seconds(&client->requesttime) -
|
|
|
|
client->formerrcache.time) < 2)
|
|
|
|
{
|
2001-03-19 20:52:21 +00:00
|
|
|
/* Drop packet. */
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
|
|
|
"possible error packet loop, "
|
|
|
|
"FORMERR dropped");
|
|
|
|
ns_client_next(client, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client->formerrcache.addr = client->peeraddr;
|
2015-10-02 12:32:42 -07:00
|
|
|
client->formerrcache.time =
|
|
|
|
isc_time_seconds(&client->requesttime);
|
2001-03-19 20:52:21 +00:00
|
|
|
client->formerrcache.id = message->id;
|
2014-10-01 07:24:16 +10:00
|
|
|
} else if (rcode == dns_rcode_servfail && client->query.qname != NULL &&
|
2014-09-03 23:28:14 -07:00
|
|
|
client->view != NULL && client->view->fail_ttl != 0 &&
|
|
|
|
((client->attributes & NS_CLIENTATTR_NOSETFC) == 0))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* SERVFAIL caching: store qname/qtype of failed queries
|
|
|
|
*/
|
|
|
|
isc_time_t expire;
|
|
|
|
isc_interval_t i;
|
|
|
|
isc_uint32_t flags = 0;
|
|
|
|
|
|
|
|
if ((message->flags & DNS_MESSAGEFLAG_CD) != 0)
|
|
|
|
flags = NS_FAILCACHE_CD;
|
|
|
|
|
|
|
|
isc_interval_set(&i, client->view->fail_ttl, 0);
|
|
|
|
result = isc_time_nowplusinterval(&expire, &i);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
dns_badcache_add(client->view->failcache,
|
|
|
|
client->query.qname,
|
|
|
|
client->query.qtype,
|
|
|
|
ISC_TRUE, flags, &expire);
|
2001-03-19 20:52:21 +00:00
|
|
|
}
|
1999-07-28 02:20:36 +00:00
|
|
|
ns_client_send(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 17:51:31 +11:00
|
|
|
isc_result_t
|
|
|
|
ns_client_addopt(ns_client_t *client, dns_message_t *message,
|
|
|
|
dns_rdataset_t **opt)
|
|
|
|
{
|
2014-08-28 22:05:57 -07:00
|
|
|
unsigned char ecs[ECS_SIZE];
|
2013-08-15 12:01:12 +10:00
|
|
|
char nsid[BUFSIZ], *nsidp;
|
2015-07-06 09:44:24 +10:00
|
|
|
unsigned char cookie[COOKIE_SIZE];
|
1999-09-02 01:52:31 +00:00
|
|
|
isc_result_t result;
|
2003-02-26 02:04:00 +00:00
|
|
|
dns_view_t *view;
|
|
|
|
dns_resolver_t *resolver;
|
|
|
|
isc_uint16_t udpsize;
|
2014-02-20 14:00:54 +11:00
|
|
|
dns_ednsopt_t ednsopts[DNS_EDNSOPTIONS];
|
2013-08-15 12:01:12 +10:00
|
|
|
int count = 0;
|
|
|
|
unsigned int flags;
|
2014-02-20 14:56:20 +11:00
|
|
|
unsigned char expire[4];
|
1999-09-02 01:52:31 +00:00
|
|
|
|
2014-02-20 17:51:31 +11:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(opt != NULL && *opt == NULL);
|
|
|
|
REQUIRE(message != NULL);
|
1999-09-02 01:52:31 +00:00
|
|
|
|
2003-02-26 02:04:00 +00:00
|
|
|
view = client->view;
|
|
|
|
resolver = (view != NULL) ? view->resolver : NULL;
|
|
|
|
if (resolver != NULL)
|
|
|
|
udpsize = dns_resolver_getudpsize(resolver);
|
|
|
|
else
|
|
|
|
udpsize = ns_g_udpsize;
|
1999-09-02 01:52:31 +00:00
|
|
|
|
2013-08-15 12:01:12 +10:00
|
|
|
flags = client->extflags & DNS_MESSAGEEXTFLAG_REPLYPRESERVE;
|
1999-09-02 01:52:31 +00:00
|
|
|
|
2008-04-03 02:01:08 +00:00
|
|
|
/* Set EDNS options if applicable */
|
2016-02-27 11:23:50 +11:00
|
|
|
if (WANTNSID(client) &&
|
2008-04-03 02:01:08 +00:00
|
|
|
(ns_g_server->server_id != NULL ||
|
|
|
|
ns_g_server->server_usehostname)) {
|
|
|
|
if (ns_g_server->server_usehostname) {
|
|
|
|
result = ns_os_gethostname(nsid, sizeof(nsid));
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto no_nsid;
|
|
|
|
}
|
2008-11-16 20:57:55 +00:00
|
|
|
nsidp = nsid;
|
|
|
|
} else
|
|
|
|
nsidp = ns_g_server->server_id;
|
2008-04-03 02:01:08 +00:00
|
|
|
|
2014-02-20 14:00:54 +11:00
|
|
|
INSIST(count < DNS_EDNSOPTIONS);
|
2013-08-15 12:01:12 +10:00
|
|
|
ednsopts[count].code = DNS_OPT_NSID;
|
2015-04-17 02:57:02 +02:00
|
|
|
ednsopts[count].length = (isc_uint16_t)strlen(nsidp);
|
2013-08-15 12:01:12 +10:00
|
|
|
ednsopts[count].value = (unsigned char *)nsidp;
|
|
|
|
count++;
|
2008-04-03 02:01:08 +00:00
|
|
|
}
|
2013-08-15 12:01:12 +10:00
|
|
|
no_nsid:
|
2015-07-06 09:44:24 +10:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_WANTCOOKIE) != 0) {
|
2014-02-19 12:53:42 +11:00
|
|
|
isc_buffer_t buf;
|
|
|
|
isc_stdtime_t now;
|
|
|
|
isc_uint32_t nonce;
|
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
isc_buffer_init(&buf, cookie, sizeof(cookie));
|
2014-02-19 12:53:42 +11:00
|
|
|
isc_stdtime_get(&now);
|
|
|
|
isc_random_get(&nonce);
|
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
compute_cookie(client, now, nonce, &buf);
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2014-02-20 14:00:54 +11:00
|
|
|
INSIST(count < DNS_EDNSOPTIONS);
|
2015-07-06 09:44:24 +10:00
|
|
|
ednsopts[count].code = DNS_OPT_COOKIE;
|
|
|
|
ednsopts[count].length = COOKIE_SIZE;
|
|
|
|
ednsopts[count].value = cookie;
|
2014-02-19 12:53:42 +11:00
|
|
|
count++;
|
|
|
|
}
|
2014-02-20 14:56:20 +11:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_HAVEEXPIRE) != 0) {
|
|
|
|
isc_buffer_t buf;
|
|
|
|
|
|
|
|
INSIST(count < DNS_EDNSOPTIONS);
|
|
|
|
|
|
|
|
isc_buffer_init(&buf, expire, sizeof(expire));
|
|
|
|
isc_buffer_putuint32(&buf, client->expire);
|
|
|
|
ednsopts[count].code = DNS_OPT_EXPIRE;
|
|
|
|
ednsopts[count].length = 4;
|
|
|
|
ednsopts[count].value = expire;
|
|
|
|
count++;
|
|
|
|
}
|
2014-08-28 22:05:57 -07:00
|
|
|
if (((client->attributes & NS_CLIENTATTR_HAVEECS) != 0) &&
|
|
|
|
(client->ecs_addr.family == AF_INET ||
|
2016-03-23 15:00:30 -07:00
|
|
|
client->ecs_addr.family == AF_INET6 ||
|
|
|
|
client->ecs_addr.family == AF_UNSPEC))
|
2014-08-28 22:05:57 -07:00
|
|
|
{
|
|
|
|
int i, addrbytes = (client->ecs_addrlen + 7) / 8;
|
|
|
|
isc_uint8_t *paddr;
|
|
|
|
isc_buffer_t buf;
|
|
|
|
|
|
|
|
/* Add client subnet option. */
|
|
|
|
isc_buffer_init(&buf, ecs, sizeof(ecs));
|
2016-03-23 15:00:30 -07:00
|
|
|
if (client->ecs_addr.family == AF_UNSPEC ||
|
|
|
|
client->ecs_addrlen == 0)
|
|
|
|
isc_buffer_putuint16(&buf, 0);
|
|
|
|
else if (client->ecs_addr.family == AF_INET)
|
2014-08-28 22:05:57 -07:00
|
|
|
isc_buffer_putuint16(&buf, 1);
|
|
|
|
else
|
|
|
|
isc_buffer_putuint16(&buf, 2);
|
|
|
|
isc_buffer_putuint8(&buf, client->ecs_addrlen);
|
|
|
|
isc_buffer_putuint8(&buf, client->ecs_scope);
|
|
|
|
|
|
|
|
paddr = (isc_uint8_t *) &client->ecs_addr.type;
|
|
|
|
for (i = 0; i < addrbytes; i++) {
|
|
|
|
unsigned char uc;
|
|
|
|
uc = paddr[i];
|
|
|
|
if (i == addrbytes - 1 &&
|
|
|
|
((client->ecs_addrlen % 8) != 0))
|
2016-03-23 15:00:30 -07:00
|
|
|
uc &= (0xffU << (8 -
|
|
|
|
(client->ecs_addrlen % 8)));
|
2014-08-28 22:05:57 -07:00
|
|
|
isc_buffer_putuint8(&buf, uc);
|
|
|
|
}
|
|
|
|
|
|
|
|
ednsopts[count].code = DNS_OPT_CLIENT_SUBNET;
|
|
|
|
ednsopts[count].length = addrbytes + 4;
|
|
|
|
ednsopts[count].value = ecs;
|
|
|
|
count++;
|
|
|
|
}
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2014-02-20 17:51:31 +11:00
|
|
|
result = dns_message_buildopt(message, opt, 0, udpsize, flags,
|
|
|
|
ednsopts, count);
|
2013-08-15 12:01:12 +10:00
|
|
|
return (result);
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
2001-07-26 20:42:46 +00:00
|
|
|
static inline isc_boolean_t
|
2014-08-28 22:05:57 -07:00
|
|
|
allowed(isc_netaddr_t *addr, dns_name_t *signer,
|
|
|
|
isc_netaddr_t *ecs_addr, isc_uint8_t ecs_addrlen,
|
|
|
|
isc_uint8_t *ecs_scope, dns_acl_t *acl)
|
|
|
|
{
|
2001-07-26 20:42:46 +00:00
|
|
|
int match;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
if (acl == NULL)
|
|
|
|
return (ISC_TRUE);
|
2014-08-28 22:05:57 -07:00
|
|
|
result = dns_acl_match2(addr, signer, ecs_addr, ecs_addrlen, ecs_scope,
|
|
|
|
acl, &ns_g_server->aclenv, &match, NULL);
|
2001-07-26 20:42:46 +00:00
|
|
|
if (result == ISC_R_SUCCESS && match > 0)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
2000-10-11 17:44:18 +00:00
|
|
|
|
2005-02-10 05:53:43 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2009-01-17 11:04:25 +00:00
|
|
|
* are updated when the appropriate task has exclusivity.
|
2005-02-10 05:53:43 +00:00
|
|
|
*/
|
|
|
|
isc_boolean_t
|
|
|
|
ns_client_isself(dns_view_t *myview, dns_tsigkey_t *mykey,
|
|
|
|
isc_sockaddr_t *srcaddr, isc_sockaddr_t *dstaddr,
|
|
|
|
dns_rdataclass_t rdclass, void *arg)
|
|
|
|
{
|
|
|
|
dns_view_t *view;
|
2007-03-06 01:14:27 +00:00
|
|
|
dns_tsigkey_t *key = NULL;
|
2006-12-04 01:54:53 +00:00
|
|
|
dns_name_t *tsig = NULL;
|
2005-02-10 05:53:43 +00:00
|
|
|
isc_netaddr_t netsrc;
|
|
|
|
isc_netaddr_t netdst;
|
|
|
|
|
|
|
|
UNUSED(arg);
|
|
|
|
|
2011-07-28 04:27:27 +00:00
|
|
|
/*
|
|
|
|
* ns_g_server->interfacemgr is task exclusive locked.
|
|
|
|
*/
|
|
|
|
if (ns_g_server->interfacemgr == NULL)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
|
2005-02-10 05:53:43 +00:00
|
|
|
if (!ns_interfacemgr_listeningon(ns_g_server->interfacemgr, dstaddr))
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
|
|
|
isc_netaddr_fromsockaddr(&netsrc, srcaddr);
|
|
|
|
isc_netaddr_fromsockaddr(&netdst, dstaddr);
|
|
|
|
|
|
|
|
for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
|
|
|
|
view != NULL;
|
|
|
|
view = ISC_LIST_NEXT(view, link)) {
|
|
|
|
|
|
|
|
if (view->matchrecursiveonly)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rdclass != view->rdclass)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mykey != NULL) {
|
|
|
|
isc_boolean_t match;
|
|
|
|
isc_result_t result;
|
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
result = dns_view_gettsig(view, &mykey->name, &key);
|
2005-02-10 05:53:43 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
continue;
|
|
|
|
match = dst_key_compare(mykey->key, key->key);
|
|
|
|
dns_tsigkey_detach(&key);
|
|
|
|
if (!match)
|
|
|
|
continue;
|
2006-12-04 01:54:53 +00:00
|
|
|
tsig = dns_tsigkey_identity(mykey);
|
2005-02-10 05:53:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 22:05:57 -07:00
|
|
|
if (allowed(&netsrc, tsig, NULL, 0, NULL,
|
|
|
|
view->matchclients) &&
|
|
|
|
allowed(&netdst, tsig, NULL, 0, NULL,
|
|
|
|
view->matchdestinations))
|
2005-02-10 05:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (ISC_TF(view == myview));
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:53:42 +11:00
|
|
|
static void
|
2015-07-06 09:44:24 +10:00
|
|
|
compute_cookie(ns_client_t *client, isc_uint32_t when, isc_uint32_t nonce,
|
|
|
|
isc_buffer_t *buf)
|
2014-02-19 12:53:42 +11:00
|
|
|
{
|
2015-07-06 09:44:24 +10:00
|
|
|
switch (ns_g_server->cookiealg) {
|
2015-07-08 11:54:25 +10:00
|
|
|
#if defined(HAVE_OPENSSL_AES) || defined(HAVE_OPENSSL_EVP_AES)
|
2015-07-06 09:44:24 +10:00
|
|
|
case ns_cookiealg_aes: {
|
|
|
|
unsigned char digest[ISC_AES_BLOCK_LENGTH];
|
|
|
|
unsigned char input[4 + 4 + 16];
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
unsigned char *cp;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
memset(input, 0, sizeof(input));
|
|
|
|
cp = isc_buffer_used(buf);
|
|
|
|
isc_buffer_putmem(buf, client->cookie, 8);
|
|
|
|
isc_buffer_putuint32(buf, nonce);
|
|
|
|
isc_buffer_putuint32(buf, when);
|
|
|
|
memmove(input, cp, 16);
|
2014-02-19 12:53:42 +11:00
|
|
|
isc_aes128_crypt(ns_g_server->secret, input, digest);
|
|
|
|
for (i = 0; i < 8; i++)
|
2015-07-06 09:44:24 +10:00
|
|
|
input[i] = digest[i] ^ digest[i + 8];
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
switch (netaddr.family) {
|
|
|
|
case AF_INET:
|
|
|
|
cp = (unsigned char *)&netaddr.type.in;
|
|
|
|
memmove(input + 8, cp, 4);
|
|
|
|
memset(input + 12, 0, 4);
|
|
|
|
isc_aes128_crypt(ns_g_server->secret, input, digest);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
cp = (unsigned char *)&netaddr.type.in6;
|
|
|
|
memmove(input + 8, cp, 16);
|
|
|
|
isc_aes128_crypt(ns_g_server->secret, input, digest);
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
input[i + 8] = digest[i] ^ digest[i + 8];
|
|
|
|
isc_aes128_crypt(ns_g_server->secret, input + 8,
|
|
|
|
digest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
digest[i] ^= digest[i + 8];
|
|
|
|
isc_buffer_putmem(buf, digest, 8);
|
2014-02-19 12:53:42 +11:00
|
|
|
break;
|
|
|
|
}
|
2015-07-08 11:54:25 +10:00
|
|
|
#endif
|
2015-07-06 09:44:24 +10:00
|
|
|
|
|
|
|
case ns_cookiealg_sha1: {
|
|
|
|
unsigned char digest[ISC_SHA1_DIGESTLENGTH];
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
unsigned char *cp;
|
|
|
|
isc_hmacsha1_t hmacsha1;
|
2015-11-16 17:47:10 +01:00
|
|
|
unsigned int length;
|
2015-07-06 09:44:24 +10:00
|
|
|
|
|
|
|
cp = isc_buffer_used(buf);
|
|
|
|
isc_buffer_putmem(buf, client->cookie, 8);
|
|
|
|
isc_buffer_putuint32(buf, nonce);
|
|
|
|
isc_buffer_putuint32(buf, when);
|
|
|
|
|
|
|
|
isc_hmacsha1_init(&hmacsha1,
|
|
|
|
ns_g_server->secret,
|
|
|
|
ISC_SHA1_DIGESTLENGTH);
|
|
|
|
isc_hmacsha1_update(&hmacsha1, cp, 16);
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
switch (netaddr.family) {
|
|
|
|
case AF_INET:
|
|
|
|
cp = (unsigned char *)&netaddr.type.in;
|
|
|
|
length = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
cp = (unsigned char *)&netaddr.type.in6;
|
|
|
|
length = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
}
|
|
|
|
isc_hmacsha1_update(&hmacsha1, cp, length);
|
|
|
|
isc_hmacsha1_update(&hmacsha1, client->cookie,
|
|
|
|
sizeof(client->cookie));
|
|
|
|
isc_hmacsha1_sign(&hmacsha1, digest, sizeof(digest));
|
|
|
|
isc_buffer_putmem(buf, digest, 8);
|
|
|
|
isc_hmacsha1_invalidate(&hmacsha1);
|
2014-02-19 12:53:42 +11:00
|
|
|
break;
|
|
|
|
}
|
2015-07-06 09:44:24 +10:00
|
|
|
|
|
|
|
case ns_cookiealg_sha256: {
|
|
|
|
unsigned char digest[ISC_SHA256_DIGESTLENGTH];
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
unsigned char *cp;
|
|
|
|
isc_hmacsha256_t hmacsha256;
|
2015-11-16 17:47:10 +01:00
|
|
|
unsigned int length;
|
2015-07-06 09:44:24 +10:00
|
|
|
|
|
|
|
cp = isc_buffer_used(buf);
|
|
|
|
isc_buffer_putmem(buf, client->cookie, 8);
|
|
|
|
isc_buffer_putuint32(buf, nonce);
|
|
|
|
isc_buffer_putuint32(buf, when);
|
|
|
|
|
|
|
|
isc_hmacsha256_init(&hmacsha256,
|
|
|
|
ns_g_server->secret,
|
|
|
|
ISC_SHA256_DIGESTLENGTH);
|
|
|
|
isc_hmacsha256_update(&hmacsha256, cp, 16);
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
switch (netaddr.family) {
|
|
|
|
case AF_INET:
|
|
|
|
cp = (unsigned char *)&netaddr.type.in;
|
|
|
|
length = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
cp = (unsigned char *)&netaddr.type.in6;
|
|
|
|
length = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
}
|
|
|
|
isc_hmacsha256_update(&hmacsha256, cp, length);
|
|
|
|
isc_hmacsha256_update(&hmacsha256, client->cookie,
|
|
|
|
sizeof(client->cookie));
|
|
|
|
isc_hmacsha256_sign(&hmacsha256, digest, sizeof(digest));
|
|
|
|
isc_buffer_putmem(buf, digest, 8);
|
|
|
|
isc_hmacsha256_invalidate(&hmacsha256);
|
2014-02-19 12:53:42 +11:00
|
|
|
break;
|
|
|
|
}
|
2015-07-06 09:44:24 +10:00
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
}
|
2014-02-19 12:53:42 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-07-06 09:44:24 +10:00
|
|
|
process_cookie(ns_client_t *client, isc_buffer_t *buf, size_t optlen) {
|
|
|
|
unsigned char dbuf[COOKIE_SIZE];
|
2014-02-19 12:53:42 +11:00
|
|
|
unsigned char *old;
|
|
|
|
isc_stdtime_t now;
|
|
|
|
isc_uint32_t when;
|
|
|
|
isc_uint32_t nonce;
|
|
|
|
isc_buffer_t db;
|
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
/*
|
|
|
|
* If we have already seen a cookie option skip this cookie option.
|
|
|
|
*/
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_WANTCOOKIE) != 0) {
|
|
|
|
isc_buffer_forward(buf, (unsigned int)optlen);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
client->attributes |= NS_CLIENTATTR_WANTCOOKIE;
|
|
|
|
|
|
|
|
isc_stats_increment(ns_g_server->nsstats, dns_nsstatscounter_cookiein);
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
if (optlen != COOKIE_SIZE) {
|
2014-02-19 12:53:42 +11:00
|
|
|
/*
|
|
|
|
* Not our token.
|
|
|
|
*/
|
2015-07-06 09:44:24 +10:00
|
|
|
INSIST(optlen >= 8U);
|
|
|
|
memmove(client->cookie, isc_buffer_current(buf), 8);
|
2014-02-19 23:17:52 +01:00
|
|
|
isc_buffer_forward(buf, (unsigned int)optlen);
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2014-03-16 11:14:26 +11:00
|
|
|
if (optlen == 8U)
|
2014-02-19 12:53:42 +11:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
2015-07-06 09:44:24 +10:00
|
|
|
dns_nsstatscounter_cookienew);
|
2014-02-19 12:53:42 +11:00
|
|
|
else
|
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
2015-07-06 09:44:24 +10:00
|
|
|
dns_nsstatscounter_cookiebadsize);
|
2014-02-19 12:53:42 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process all of the incoming buffer.
|
|
|
|
*/
|
|
|
|
old = isc_buffer_current(buf);
|
2014-02-24 09:29:49 +11:00
|
|
|
memmove(client->cookie, old, 8);
|
2014-02-19 12:53:42 +11:00
|
|
|
isc_buffer_forward(buf, 8);
|
|
|
|
nonce = isc_buffer_getuint32(buf);
|
|
|
|
when = isc_buffer_getuint32(buf);
|
|
|
|
isc_buffer_forward(buf, 8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow for a 5 minute clock skew between servers sharing a secret.
|
2015-07-06 09:44:24 +10:00
|
|
|
* Only accept COOKIE if we have talked to the client in the last hour.
|
2014-02-19 12:53:42 +11:00
|
|
|
*/
|
|
|
|
isc_stdtime_get(&now);
|
|
|
|
if (isc_serial_gt(when, (now + 300)) || /* In the future. */
|
|
|
|
isc_serial_lt(when, (now - 3600))) { /* In the past. */
|
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
2015-07-06 09:44:24 +10:00
|
|
|
dns_nsstatscounter_cookiebadtime);
|
2014-02-19 12:53:42 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_init(&db, dbuf, sizeof(dbuf));
|
2015-07-06 09:44:24 +10:00
|
|
|
compute_cookie(client, when, nonce, &db);
|
2014-02-19 12:53:42 +11:00
|
|
|
|
2015-08-17 18:26:44 -07:00
|
|
|
if (!isc_safe_memequal(old, dbuf, COOKIE_SIZE)) {
|
2014-02-19 12:53:42 +11:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
2015-07-06 09:44:24 +10:00
|
|
|
dns_nsstatscounter_cookienomatch);
|
2014-02-19 12:53:42 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-06 09:44:24 +10:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_cookiematch);
|
|
|
|
client->attributes |= NS_CLIENTATTR_HAVECOOKIE;
|
2014-02-19 12:53:42 +11:00
|
|
|
}
|
|
|
|
|
2014-08-28 22:05:57 -07:00
|
|
|
static isc_result_t
|
|
|
|
process_ecs(ns_client_t *client, isc_buffer_t *buf, size_t optlen) {
|
|
|
|
isc_uint16_t family;
|
|
|
|
isc_uint8_t addrlen, addrbytes, scope, *paddr;
|
|
|
|
isc_netaddr_t caddr;
|
|
|
|
|
2016-02-27 11:23:50 +11:00
|
|
|
/*
|
|
|
|
* If we have already seen a ECS option skip this ECS option.
|
|
|
|
*/
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_HAVEECS) != 0) {
|
2016-05-17 17:22:51 +10:00
|
|
|
isc_buffer_forward(buf, (unsigned int)optlen);
|
2016-02-27 11:23:50 +11:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2015-07-06 12:52:37 +10:00
|
|
|
/*
|
|
|
|
* 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?
|
|
|
|
*/
|
|
|
|
|
2014-09-16 23:46:37 +10:00
|
|
|
if (optlen < 4U) {
|
2014-08-28 22:05:57 -07:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
2015-07-06 12:52:37 +10:00
|
|
|
"EDNS client-subnet option too short");
|
2014-08-28 22:05:57 -07:00
|
|
|
return (DNS_R_FORMERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
family = isc_buffer_getuint16(buf);
|
|
|
|
addrlen = isc_buffer_getuint8(buf);
|
|
|
|
scope = isc_buffer_getuint8(buf);
|
|
|
|
optlen -= 4;
|
|
|
|
|
|
|
|
if (scope != 0U) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
2015-07-06 12:52:37 +10:00
|
|
|
"EDNS client-subnet option: invalid scope");
|
|
|
|
return (DNS_R_OPTERR);
|
2014-08-28 22:05:57 -07:00
|
|
|
}
|
|
|
|
|
2016-03-23 15:00:30 -07:00
|
|
|
if (addrlen == 0U && family != 0U) {
|
2016-03-23 08:54:46 -07:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
|
|
|
"EDNS client-subnet option: "
|
|
|
|
"source == 0 but family != 0");
|
|
|
|
return (DNS_R_OPTERR);
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:05:57 -07:00
|
|
|
memset(&caddr, 0, sizeof(caddr));
|
|
|
|
switch (family) {
|
2016-03-23 08:54:46 -07:00
|
|
|
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) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
|
|
|
"EDNS client-subnet option: invalid "
|
|
|
|
"address length (%u) for FAMILY=0",
|
|
|
|
addrlen);
|
|
|
|
return (DNS_R_OPTERR);
|
|
|
|
}
|
|
|
|
caddr.family = AF_UNSPEC;
|
|
|
|
break;
|
2014-08-28 22:05:57 -07:00
|
|
|
case 1:
|
2016-03-23 08:54:46 -07:00
|
|
|
if (addrlen > 32U) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
|
|
|
"EDNS client-subnet option: invalid "
|
|
|
|
"address length (%u) for IPv4",
|
|
|
|
addrlen);
|
|
|
|
return (DNS_R_OPTERR);
|
|
|
|
}
|
2014-08-28 22:05:57 -07:00
|
|
|
caddr.family = AF_INET;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (addrlen > 128U) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
2015-07-06 12:52:37 +10:00
|
|
|
"EDNS client-subnet option: invalid "
|
2016-03-23 08:54:46 -07:00
|
|
|
"address length (%u) for IPv6",
|
|
|
|
addrlen);
|
2015-07-06 12:52:37 +10:00
|
|
|
return (DNS_R_OPTERR);
|
2014-08-28 22:05:57 -07:00
|
|
|
}
|
|
|
|
caddr.family = AF_INET6;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
2015-07-06 12:52:37 +10:00
|
|
|
"EDNS client-subnet option: invalid family");
|
|
|
|
return (DNS_R_OPTERR);
|
2014-08-28 22:05:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
addrbytes = (addrlen + 7) / 8;
|
|
|
|
if (isc_buffer_remaininglength(buf) < addrbytes) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
2015-07-06 12:52:37 +10:00
|
|
|
"EDNS client-subnet option: address too short");
|
|
|
|
return (DNS_R_OPTERR);
|
2014-08-28 22:05:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
paddr = (isc_uint8_t *) &caddr.type;
|
2016-03-23 08:54:46 -07:00
|
|
|
if (addrbytes != 0U) {
|
|
|
|
memmove(paddr, isc_buffer_current(buf), addrbytes);
|
|
|
|
isc_buffer_forward(buf, addrbytes);
|
|
|
|
optlen -= addrbytes;
|
|
|
|
|
|
|
|
if ((addrlen % 8) != 0) {
|
2016-07-13 13:54:12 +05:30
|
|
|
isc_uint8_t bits = ~0U << (8 - (addrlen % 8));
|
2016-03-23 08:54:46 -07:00
|
|
|
bits &= paddr[addrbytes - 1];
|
|
|
|
if (bits != paddr[addrbytes - 1])
|
|
|
|
return (DNS_R_OPTERR);
|
|
|
|
}
|
2015-07-06 12:52:37 +10:00
|
|
|
}
|
|
|
|
|
2014-08-28 22:05:57 -07:00
|
|
|
memmove(&client->ecs_addr, &caddr, sizeof(caddr));
|
|
|
|
client->ecs_addrlen = addrlen;
|
|
|
|
client->ecs_scope = 0;
|
|
|
|
client->attributes |= NS_CLIENTATTR_HAVEECS;
|
|
|
|
|
2015-04-17 11:39:26 +02:00
|
|
|
isc_buffer_forward(buf, (unsigned int)optlen);
|
2014-08-28 22:05:57 -07:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2013-08-15 12:01:12 +10:00
|
|
|
static isc_result_t
|
|
|
|
process_opt(ns_client_t *client, dns_rdataset_t *opt) {
|
|
|
|
dns_rdata_t rdata;
|
|
|
|
isc_buffer_t optbuf;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_uint16_t optcode;
|
|
|
|
isc_uint16_t optlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the client's UDP buffer size.
|
|
|
|
*/
|
|
|
|
client->udpsize = opt->rdclass;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the requested UDP buffer size is less than 512,
|
|
|
|
* ignore it and use 512.
|
|
|
|
*/
|
|
|
|
if (client->udpsize < 512)
|
|
|
|
client->udpsize = 512;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the flags out of the OPT record.
|
|
|
|
*/
|
|
|
|
client->extflags = (isc_uint16_t)(opt->ttl & 0xFFFF);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do we understand this version of EDNS?
|
|
|
|
*
|
|
|
|
* XXXRTH need library support for this!
|
|
|
|
*/
|
|
|
|
client->ednsversion = (opt->ttl & 0x00FF0000) >> 16;
|
2014-09-10 15:31:40 +10:00
|
|
|
if (client->ednsversion > DNS_EDNS_VERSION) {
|
2013-08-15 12:01:12 +10:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_badednsver);
|
2014-02-20 17:51:31 +11:00
|
|
|
result = ns_client_addopt(client, client->message,
|
|
|
|
&client->opt);
|
2013-08-15 12:01:12 +10:00
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
result = DNS_R_BADVERS;
|
|
|
|
ns_client_error(client, result);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for NSID request */
|
|
|
|
result = dns_rdataset_first(opt);
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
dns_rdata_init(&rdata);
|
|
|
|
dns_rdataset_current(opt, &rdata);
|
|
|
|
isc_buffer_init(&optbuf, rdata.data, rdata.length);
|
|
|
|
isc_buffer_add(&optbuf, rdata.length);
|
|
|
|
while (isc_buffer_remaininglength(&optbuf) >= 4) {
|
|
|
|
optcode = isc_buffer_getuint16(&optbuf);
|
|
|
|
optlen = isc_buffer_getuint16(&optbuf);
|
|
|
|
switch (optcode) {
|
|
|
|
case DNS_OPT_NSID:
|
2016-02-27 11:23:50 +11:00
|
|
|
if (!WANTNSID(client))
|
|
|
|
isc_stats_increment(
|
|
|
|
ns_g_server->nsstats,
|
2014-02-24 09:24:25 +11:00
|
|
|
dns_nsstatscounter_nsidopt);
|
2013-08-15 12:01:12 +10:00
|
|
|
client->attributes |= NS_CLIENTATTR_WANTNSID;
|
|
|
|
isc_buffer_forward(&optbuf, optlen);
|
|
|
|
break;
|
2015-07-06 09:44:24 +10:00
|
|
|
case DNS_OPT_COOKIE:
|
|
|
|
process_cookie(client, &optbuf, optlen);
|
2014-02-19 12:53:42 +11:00
|
|
|
break;
|
2014-02-20 14:56:20 +11:00
|
|
|
case DNS_OPT_EXPIRE:
|
2016-02-27 11:23:50 +11:00
|
|
|
if (!WANTEXPIRE(client))
|
|
|
|
isc_stats_increment(
|
|
|
|
ns_g_server->nsstats,
|
2014-02-24 09:24:25 +11:00
|
|
|
dns_nsstatscounter_expireopt);
|
2014-02-20 14:56:20 +11:00
|
|
|
client->attributes |= NS_CLIENTATTR_WANTEXPIRE;
|
|
|
|
isc_buffer_forward(&optbuf, optlen);
|
|
|
|
break;
|
2014-08-28 22:05:57 -07:00
|
|
|
case DNS_OPT_CLIENT_SUBNET:
|
|
|
|
result = process_ecs(client, &optbuf, optlen);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_error(client, result);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_ecsopt);
|
|
|
|
break;
|
2013-08-15 12:01:12 +10:00
|
|
|
default:
|
2014-02-24 09:24:25 +11:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_otheropt);
|
2013-08-15 12:01:12 +10:00
|
|
|
isc_buffer_forward(&optbuf, optlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_stats_increment(ns_g_server->nsstats, dns_nsstatscounter_edns0in);
|
2014-02-20 14:56:20 +11:00
|
|
|
client->attributes |= NS_CLIENTATTR_WANTOPT;
|
2013-08-15 12:01:12 +10:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2000-01-06 01:09:27 +00:00
|
|
|
/*
|
2001-01-27 02:08:07 +00:00
|
|
|
* Handle an incoming request event from the socket (UDP case)
|
2000-01-06 01:09:27 +00:00
|
|
|
* or tcpmsg (TCP case).
|
|
|
|
*/
|
1999-07-24 01:17:44 +00:00
|
|
|
static void
|
1999-08-05 01:51:32 +00:00
|
|
|
client_request(isc_task_t *task, isc_event_t *event) {
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_client_t *client;
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_socketevent_t *sevent;
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t result;
|
2001-12-06 04:38:51 +00:00
|
|
|
isc_result_t sigresult = ISC_R_SUCCESS;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_buffer_t *buffer;
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_buffer_t tbuffer;
|
1999-08-05 22:14:43 +00:00
|
|
|
dns_view_t *view;
|
1999-09-02 01:52:31 +00:00
|
|
|
dns_rdataset_t *opt;
|
2008-04-03 05:55:52 +00:00
|
|
|
dns_name_t *signame;
|
2007-03-29 06:36:31 +00:00
|
|
|
isc_boolean_t ra; /* Recursion available. */
|
2001-04-16 21:18:10 +00:00
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
int match;
|
2001-05-09 23:13:03 +00:00
|
|
|
dns_messageid_t id;
|
|
|
|
unsigned int flags;
|
2001-10-09 02:05:55 +00:00
|
|
|
isc_boolean_t notimp;
|
2015-07-06 22:20:03 -07:00
|
|
|
size_t reqsize;
|
2015-10-02 12:32:42 -07:00
|
|
|
#ifdef HAVE_DNSTAP
|
|
|
|
dns_dtmsgtype_t dtmsgtype;
|
|
|
|
#endif
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
REQUIRE(event != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
client = event->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
|
2000-01-15 00:36:26 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-01-27 01:00:16 +00:00
|
|
|
|
2012-10-23 22:04:06 -07:00
|
|
|
INSIST(client->state == (TCP_CLIENT(client) ?
|
2011-10-10 22:57:14 +00:00
|
|
|
NS_CLIENTSTATE_READING :
|
2012-10-23 22:04:06 -07:00
|
|
|
NS_CLIENTSTATE_READY));
|
2000-02-10 22:16:56 +00:00
|
|
|
|
2005-08-15 01:21:07 +00:00
|
|
|
ns_client_requests++;
|
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
if (event->ev_type == ISC_SOCKEVENT_RECVDONE) {
|
2000-03-14 03:30:52 +00:00
|
|
|
INSIST(!TCP_CLIENT(client));
|
2001-01-27 02:08:07 +00:00
|
|
|
sevent = (isc_socketevent_t *)event;
|
2001-02-12 21:45:37 +00:00
|
|
|
REQUIRE(sevent == client->recvevent);
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_buffer_init(&tbuffer, sevent->region.base, sevent->n);
|
|
|
|
isc_buffer_add(&tbuffer, sevent->n);
|
|
|
|
buffer = &tbuffer;
|
|
|
|
result = sevent->result;
|
2001-05-14 21:12:32 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
client->peeraddr = sevent->address;
|
|
|
|
client->peeraddr_valid = ISC_TRUE;
|
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
if ((sevent->attributes & ISC_SOCKEVENTATTR_DSCP) != 0) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(90),
|
|
|
|
"received DSCP %d", sevent->dscp);
|
|
|
|
if (client->dscp == -1)
|
|
|
|
client->dscp = sevent->dscp;
|
|
|
|
}
|
2001-01-27 02:08:07 +00:00
|
|
|
if ((sevent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
|
2000-03-14 03:30:52 +00:00
|
|
|
client->attributes |= NS_CLIENTATTR_PKTINFO;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->pktinfo = sevent->pktinfo;
|
2000-03-14 03:30:52 +00:00
|
|
|
}
|
2001-01-27 02:08:07 +00:00
|
|
|
if ((sevent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
|
2000-06-15 18:26:28 +00:00
|
|
|
client->attributes |= NS_CLIENTATTR_MULTICAST;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->nrecvs--;
|
1999-08-05 01:51:32 +00:00
|
|
|
} else {
|
2000-03-14 03:30:52 +00:00
|
|
|
INSIST(TCP_CLIENT(client));
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
|
|
|
|
REQUIRE(event->ev_sender == &client->tcpmsg);
|
1999-08-05 01:51:32 +00:00
|
|
|
buffer = &client->tcpmsg.buffer;
|
|
|
|
result = client->tcpmsg.result;
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->nreads == 1);
|
2000-02-17 18:18:24 +00:00
|
|
|
/*
|
|
|
|
* client->peeraddr was set when the connection was accepted.
|
|
|
|
*/
|
1999-12-22 16:59:05 +00:00
|
|
|
client->nreads--;
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 22:20:03 -07:00
|
|
|
reqsize = isc_buffer_usedlength(buffer);
|
2016-01-20 17:30:27 -08:00
|
|
|
/* don't count the length header */
|
|
|
|
if (TCP_CLIENT(client))
|
|
|
|
reqsize -= 2;
|
2015-07-06 22:20:03 -07:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (exit_check(client))
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-06-22 01:14:38 +00:00
|
|
|
client->state = client->newstate = NS_CLIENTSTATE_WORKING;
|
2000-06-15 18:26:28 +00:00
|
|
|
|
2015-10-02 12:32:42 -07:00
|
|
|
isc_task_getcurrenttimex(task, &client->requesttime);
|
|
|
|
client->tnow = client->requesttime;
|
|
|
|
client->now = isc_time_seconds(&client->tnow);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2001-05-14 21:12:32 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
1999-08-05 01:51:32 +00:00
|
|
|
ns_client_next(client, result);
|
2001-05-14 21:12:32 +00:00
|
|
|
} else {
|
2001-05-14 21:33:45 +00:00
|
|
|
if (result != ISC_R_CANCELED)
|
|
|
|
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT,
|
|
|
|
ISC_LOG_ERROR,
|
|
|
|
"UDP client handler shutting "
|
|
|
|
"down due to fatal receive "
|
|
|
|
"error: %s",
|
|
|
|
isc_result_totext(result));
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_task_shutdown(client->task);
|
2001-05-14 21:12:32 +00:00
|
|
|
}
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2001-04-16 21:18:10 +00:00
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
|
2006-01-04 05:06:10 +00:00
|
|
|
#if NS_CLIENT_DROPPORT
|
|
|
|
if (ns_client_dropport(isc_sockaddr_getport(&client->peeraddr)) ==
|
|
|
|
DROPPORT_REQUEST) {
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"dropped request: suspicious port");
|
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-05-14 21:12:32 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"%s request",
|
|
|
|
TCP_CLIENT(client) ? "TCP" : "UDP");
|
|
|
|
|
2001-04-16 21:18:10 +00:00
|
|
|
/*
|
|
|
|
* Check the blackhole ACL for UDP only, since TCP is done in
|
|
|
|
* client_newconn.
|
|
|
|
*/
|
|
|
|
if (!TCP_CLIENT(client)) {
|
|
|
|
if (ns_g_server->blackholeacl != NULL &&
|
|
|
|
dns_acl_match(&netaddr, NULL, ns_g_server->blackholeacl,
|
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL) == ISC_R_SUCCESS &&
|
|
|
|
match > 0)
|
|
|
|
{
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
2001-04-16 22:10:44 +00:00
|
|
|
"blackholed UDP datagram");
|
2001-04-16 21:18:10 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-09 04:29:19 +00:00
|
|
|
/*
|
|
|
|
* Silently drop multicast requests for the present.
|
2009-05-07 09:41:23 +00:00
|
|
|
* XXXMPA revisit this as mDNS spec was published.
|
2001-10-09 04:29:19 +00:00
|
|
|
*/
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
|
|
|
|
"dropping multicast request");
|
|
|
|
ns_client_next(client, DNS_R_REFUSED);
|
2006-07-21 23:38:16 +00:00
|
|
|
goto cleanup;
|
2001-10-09 04:29:19 +00:00
|
|
|
}
|
|
|
|
|
2001-05-09 23:13:03 +00:00
|
|
|
result = dns_message_peekheader(buffer, &id, &flags);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2001-05-09 23:13:03 +00:00
|
|
|
/*
|
|
|
|
* There isn't enough header to determine whether
|
|
|
|
* this was a request or a response. Drop it.
|
|
|
|
*/
|
|
|
|
ns_client_next(client, result);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
1999-12-20 23:06:17 +00:00
|
|
|
|
|
|
|
/*
|
2001-05-09 23:13:03 +00:00
|
|
|
* 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.
|
1999-12-20 23:06:17 +00:00
|
|
|
*/
|
2001-05-09 23:13:03 +00:00
|
|
|
if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
|
2001-01-27 02:08:07 +00:00
|
|
|
if (TCP_CLIENT(client)) {
|
|
|
|
CTRACE("unexpected response");
|
|
|
|
ns_client_next(client, DNS_R_FORMERR);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2001-01-27 02:08:07 +00:00
|
|
|
} else {
|
2001-02-10 02:00:11 +00:00
|
|
|
dns_dispatch_importrecv(client->dispatch, event);
|
2001-01-27 02:08:07 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2001-01-27 02:08:07 +00:00
|
|
|
}
|
1999-12-20 23:06:17 +00:00
|
|
|
}
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2008-04-03 05:55:52 +00:00
|
|
|
/*
|
|
|
|
* Update some statistics counters. Don't count responses.
|
|
|
|
*/
|
|
|
|
if (isc_sockaddr_pf(&client->peeraddr) == PF_INET) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_requestv4);
|
2008-04-03 05:55:52 +00:00
|
|
|
} else {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_requestv6);
|
2008-04-03 05:55:52 +00:00
|
|
|
}
|
2015-07-06 22:20:03 -07:00
|
|
|
if (TCP_CLIENT(client)) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
2014-02-15 20:57:00 -08:00
|
|
|
dns_nsstatscounter_requesttcp);
|
2016-06-23 08:44:54 +10:00
|
|
|
switch (isc_sockaddr_pf(&client->peeraddr)) {
|
|
|
|
case AF_INET:
|
|
|
|
isc_stats_increment(ns_g_server->tcpinstats4,
|
|
|
|
ISC_MIN((int)reqsize / 16, 18));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
isc_stats_increment(ns_g_server->tcpinstats6,
|
|
|
|
ISC_MIN((int)reqsize / 16, 18));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-06 22:20:03 -07:00
|
|
|
} else {
|
2016-06-23 08:44:54 +10:00
|
|
|
switch (isc_sockaddr_pf(&client->peeraddr)) {
|
|
|
|
case AF_INET:
|
|
|
|
isc_stats_increment(ns_g_server->udpinstats4,
|
|
|
|
ISC_MIN((int)reqsize / 16, 18));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
isc_stats_increment(ns_g_server->udpinstats6,
|
|
|
|
ISC_MIN((int)reqsize / 16, 18));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-06 22:20:03 -07:00
|
|
|
}
|
2008-04-03 05:55:52 +00:00
|
|
|
|
2001-05-09 23:13:03 +00:00
|
|
|
/*
|
|
|
|
* It's a request. Parse it.
|
|
|
|
*/
|
|
|
|
result = dns_message_parse(client->message, buffer, 0);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* Parsing the request failed. Send a response
|
|
|
|
* (typically FORMERR or SERVFAIL).
|
|
|
|
*/
|
2015-07-06 09:44:24 +10:00
|
|
|
if (result == DNS_R_OPTERR)
|
|
|
|
(void)ns_client_addopt(client, client->message,
|
|
|
|
&client->opt);
|
2015-07-08 22:53:39 -07:00
|
|
|
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2016-01-04 13:05:23 -08:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2015-07-08 22:53:39 -07:00
|
|
|
"message parsing failed: %s",
|
|
|
|
isc_result_totext(result));
|
2001-05-09 23:13:03 +00:00
|
|
|
ns_client_error(client, result);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-01-20 16:10:30 -08:00
|
|
|
/*
|
|
|
|
* Pipeline TCP query processing.
|
|
|
|
*/
|
|
|
|
if (client->message->opcode != dns_opcode_query)
|
|
|
|
client->pipelined = ISC_FALSE;
|
|
|
|
if (TCP_CLIENT(client) && client->pipelined) {
|
|
|
|
result = isc_quota_reserve(&ns_g_server->tcpquota);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
result = ns_client_replace(client);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
|
|
|
|
"no more TCP clients(read): %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
client->pipelined = ISC_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-03 05:55:52 +00:00
|
|
|
dns_opcodestats_increment(ns_g_server->opcodestats,
|
|
|
|
client->message->opcode);
|
2001-10-09 02:05:55 +00:00
|
|
|
switch (client->message->opcode) {
|
|
|
|
case dns_opcode_query:
|
|
|
|
case dns_opcode_update:
|
|
|
|
case dns_opcode_notify:
|
|
|
|
notimp = ISC_FALSE;
|
|
|
|
break;
|
|
|
|
case dns_opcode_iquery:
|
|
|
|
default:
|
|
|
|
notimp = ISC_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-19 00:02:26 +00:00
|
|
|
client->message->rcode = dns_rcode_noerror;
|
|
|
|
|
2001-10-13 01:10:26 +00:00
|
|
|
/* RFC1123 section 6.1.3.2 */
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0)
|
|
|
|
client->message->flags &= ~DNS_MESSAGEFLAG_RD;
|
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
/*
|
|
|
|
* Deal with EDNS.
|
|
|
|
*/
|
2012-10-16 10:23:08 +11:00
|
|
|
if (ns_g_noedns)
|
|
|
|
opt = NULL;
|
|
|
|
else
|
|
|
|
opt = dns_message_getopt(client->message);
|
2014-08-28 22:05:57 -07:00
|
|
|
|
|
|
|
client->ecs_addrlen = 0;
|
|
|
|
client->ecs_scope = 0;
|
|
|
|
|
1999-09-02 01:52:31 +00:00
|
|
|
if (opt != NULL) {
|
2012-10-16 10:23:08 +11:00
|
|
|
/*
|
|
|
|
* Are we dropping all EDNS queries?
|
|
|
|
*/
|
|
|
|
if (ns_g_dropedns) {
|
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-08-15 12:01:12 +10:00
|
|
|
result = process_opt(client, opt);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
1999-09-02 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
2000-08-21 23:45:05 +00:00
|
|
|
if (client->message->rdclass == 0) {
|
2015-07-06 09:44:24 +10:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_WANTCOOKIE) != 0 ||
|
|
|
|
(client->message->opcode == dns_opcode_query &&
|
|
|
|
client->message->counts[DNS_SECTION_QUESTION] == 0U)) {
|
|
|
|
result = dns_message_reply(client->message, ISC_TRUE);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
ns_client_error(client, result);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-24 13:35:29 +10:00
|
|
|
if (notimp)
|
|
|
|
client->message->rcode = dns_rcode_notimp;
|
2015-07-06 09:44:24 +10:00
|
|
|
ns_client_send(client);
|
|
|
|
return;
|
|
|
|
}
|
2000-08-21 23:45:05 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2001-06-28 02:39:46 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2000-08-21 23:45:05 +00:00
|
|
|
"message class could not be determined");
|
2001-05-25 07:39:48 +00:00
|
|
|
ns_client_dumpmessage(client,
|
|
|
|
"message class could not be determined");
|
2001-10-09 02:05:55 +00:00
|
|
|
ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-08-21 23:45:05 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 20:01:57 +00:00
|
|
|
/*
|
2004-04-29 01:37:14 +00:00
|
|
|
* 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,
|
2008-01-18 23:46:58 +00:00
|
|
|
* etc), we regard this as an error for safety.
|
2001-11-16 20:01:57 +00:00
|
|
|
*/
|
2004-04-29 01:37:14 +00:00
|
|
|
if ((client->interface->flags & NS_INTERFACEFLAG_ANYADDR) == 0)
|
2009-03-03 01:36:17 +00:00
|
|
|
isc_netaddr_fromsockaddr(&client->destaddr,
|
|
|
|
&client->interface->addr);
|
2004-04-29 01:37:14 +00:00
|
|
|
else {
|
2009-03-03 01:36:17 +00:00
|
|
|
isc_sockaddr_t sockaddr;
|
2004-04-29 01:37:14 +00:00
|
|
|
result = ISC_R_FAILURE;
|
|
|
|
|
2009-03-03 01:36:17 +00:00
|
|
|
if (TCP_CLIENT(client))
|
2004-04-29 01:37:14 +00:00
|
|
|
result = isc_socket_getsockname(client->tcpsocket,
|
2009-03-03 01:36:17 +00:00
|
|
|
&sockaddr);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
isc_netaddr_fromsockaddr(&client->destaddr, &sockaddr);
|
2004-04-29 01:37:14 +00:00
|
|
|
if (result != ISC_R_SUCCESS &&
|
|
|
|
client->interface->addr.type.sa.sa_family == AF_INET6 &&
|
|
|
|
(client->attributes & NS_CLIENTATTR_PKTINFO) != 0) {
|
2002-10-24 03:52:35 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-03-03 01:36:17 +00:00
|
|
|
isc_netaddr_fromin6(&client->destaddr,
|
2002-10-24 03:52:35 +00:00
|
|
|
&client->pktinfo.ipi6_addr);
|
2009-03-03 01:36:17 +00:00
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&client->pktinfo.ipi6_addr))
|
|
|
|
isc_netaddr_setzone(&client->destaddr,
|
2009-03-03 23:48:02 +00:00
|
|
|
client->pktinfo.ipi6_ifindex);
|
2004-04-29 01:37:14 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"failed to get request's "
|
|
|
|
"destination: %s",
|
|
|
|
isc_result_totext(result));
|
2007-02-26 00:57:03 +00:00
|
|
|
ns_client_next(client, ISC_R_SUCCESS);
|
2004-04-29 01:37:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2001-11-16 20:01:57 +00:00
|
|
|
}
|
|
|
|
|
2000-01-21 20:24:49 +00:00
|
|
|
/*
|
2000-04-06 17:29:47 +00:00
|
|
|
* Find a view that matches the client's source address.
|
2000-01-21 20:24:49 +00:00
|
|
|
*/
|
|
|
|
for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
|
|
|
|
view != NULL;
|
|
|
|
view = ISC_LIST_NEXT(view, link)) {
|
2000-01-24 21:29:12 +00:00
|
|
|
if (client->message->rdclass == view->rdclass ||
|
|
|
|
client->message->rdclass == dns_rdataclass_any)
|
|
|
|
{
|
2001-12-06 04:38:51 +00:00
|
|
|
dns_name_t *tsig = NULL;
|
2014-08-28 22:05:57 -07:00
|
|
|
isc_netaddr_t *addr = NULL;
|
|
|
|
isc_uint8_t *scope = NULL;
|
2006-12-04 01:54:53 +00:00
|
|
|
|
2001-12-06 04:38:51 +00:00
|
|
|
sigresult = dns_message_rechecksig(client->message,
|
|
|
|
view);
|
2014-08-28 22:05:57 -07:00
|
|
|
if (sigresult == ISC_R_SUCCESS) {
|
|
|
|
dns_tsigkey_t *tsigkey;
|
|
|
|
|
|
|
|
tsigkey = client->message->tsigkey;
|
|
|
|
tsig = dns_tsigkey_identity(tsigkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((client->attributes & NS_CLIENTATTR_HAVEECS) != 0) {
|
|
|
|
addr = &client->ecs_addr;
|
|
|
|
scope = &client->ecs_scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allowed(&netaddr, tsig, addr, client->ecs_addrlen,
|
|
|
|
scope, view->matchclients) &&
|
|
|
|
allowed(&client->destaddr, tsig, NULL,
|
|
|
|
0, NULL, view->matchdestinations) &&
|
|
|
|
!(view->matchrecursiveonly &&
|
|
|
|
(client->message->flags & DNS_MESSAGEFLAG_RD) == 0))
|
2000-04-06 17:29:47 +00:00
|
|
|
{
|
|
|
|
dns_view_attach(view, &client->view);
|
|
|
|
break;
|
|
|
|
}
|
2000-01-21 20:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == NULL) {
|
2000-11-15 19:15:25 +00:00
|
|
|
char classname[DNS_RDATACLASS_FORMATSIZE];
|
|
|
|
|
2000-09-18 22:54:08 +00:00
|
|
|
/*
|
|
|
|
* Do a dummy TSIG verification attempt so that the
|
|
|
|
* response will have a TSIG if the query did, as
|
|
|
|
* required by RFC2845.
|
|
|
|
*/
|
2000-09-12 07:48:28 +00:00
|
|
|
isc_buffer_t b;
|
|
|
|
isc_region_t *r;
|
2001-12-06 04:38:51 +00:00
|
|
|
|
|
|
|
dns_message_resetsig(client->message);
|
|
|
|
|
2000-09-12 07:48:28 +00:00
|
|
|
r = dns_message_getrawmessage(client->message);
|
|
|
|
isc_buffer_init(&b, r->base, r->length);
|
|
|
|
isc_buffer_add(&b, r->length);
|
|
|
|
(void)dns_tsig_verify(&b, client->message, NULL, NULL);
|
|
|
|
|
2000-11-15 19:15:25 +00:00
|
|
|
dns_rdataclass_format(client->message->rdclass, classname,
|
|
|
|
sizeof(classname));
|
2000-04-06 17:29:47 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2001-06-28 02:39:46 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2000-11-15 19:15:25 +00:00
|
|
|
"no matching view in class '%s'", classname);
|
2001-05-25 07:39:48 +00:00
|
|
|
ns_client_dumpmessage(client, "no matching view in class");
|
2001-10-09 02:05:55 +00:00
|
|
|
ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-01-21 20:24:49 +00:00
|
|
|
}
|
|
|
|
|
2000-04-06 20:26:39 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
|
|
|
|
"using view '%s'", view->name);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-12-10 18:14:49 +00:00
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Check for a signature. We log bad signatures regardless of
|
1999-12-10 18:14:49 +00:00
|
|
|
* whether they ultimately cause the request to be rejected or
|
|
|
|
* not. We do not log the lack of a signature unless we are
|
|
|
|
* debugging.
|
|
|
|
*/
|
|
|
|
client->signer = NULL;
|
2000-01-21 20:24:49 +00:00
|
|
|
dns_name_init(&client->signername, NULL);
|
1999-12-10 18:14:49 +00:00
|
|
|
result = dns_message_signer(client->message, &client->signername);
|
2008-04-03 05:55:52 +00:00
|
|
|
if (result != ISC_R_NOTFOUND) {
|
|
|
|
signame = NULL;
|
|
|
|
if (dns_message_gettsig(client->message, &signame) != NULL) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_tsigin);
|
2008-04-03 05:55:52 +00:00
|
|
|
} else {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_sig0in);
|
2008-04-03 05:55:52 +00:00
|
|
|
}
|
2008-04-03 06:09:05 +00:00
|
|
|
|
2008-04-03 05:55:52 +00:00
|
|
|
}
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2011-01-10 05:32:04 +00:00
|
|
|
char namebuf[DNS_NAME_FORMATSIZE];
|
|
|
|
dns_name_format(&client->signername, namebuf, sizeof(namebuf));
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
1999-12-10 18:14:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
2011-01-10 05:32:04 +00:00
|
|
|
"request has valid signature: %s", namebuf);
|
1999-12-10 18:14:49 +00:00
|
|
|
client->signer = &client->signername;
|
2000-04-06 22:03:35 +00:00
|
|
|
} else if (result == ISC_R_NOTFOUND) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
1999-12-10 18:14:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request is not signed");
|
2000-01-21 20:24:49 +00:00
|
|
|
} else if (result == DNS_R_NOIDENTITY) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
2000-01-21 20:24:49 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"request is signed by a nonauthoritative key");
|
1999-12-10 18:14:49 +00:00
|
|
|
} else {
|
2002-03-05 00:36:44 +00:00
|
|
|
char tsigrcode[64];
|
|
|
|
isc_buffer_t b;
|
2007-08-22 00:42:42 +00:00
|
|
|
dns_rcode_t status;
|
|
|
|
isc_result_t tresult;
|
2002-03-05 00:36:44 +00:00
|
|
|
|
1999-12-10 18:14:49 +00:00
|
|
|
/* There is a signature, but it is bad. */
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(ns_g_server->nsstats,
|
|
|
|
dns_nsstatscounter_invalidsig);
|
2008-04-03 05:55:52 +00:00
|
|
|
signame = NULL;
|
|
|
|
if (dns_message_gettsig(client->message, &signame) != NULL) {
|
2004-02-09 23:25:22 +00:00
|
|
|
char namebuf[DNS_NAME_FORMATSIZE];
|
2006-12-04 01:54:53 +00:00
|
|
|
char cnamebuf[DNS_NAME_FORMATSIZE];
|
2008-04-03 05:55:52 +00:00
|
|
|
dns_name_format(signame, namebuf, sizeof(namebuf));
|
2007-08-22 00:42:42 +00:00
|
|
|
status = client->message->tsigstatus;
|
|
|
|
isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1);
|
|
|
|
tresult = dns_tsigrcode_totext(status, &b);
|
|
|
|
INSIST(tresult == ISC_R_SUCCESS);
|
|
|
|
tsigrcode[isc_buffer_usedlength(&b)] = '\0';
|
2006-12-04 01:54:53 +00:00
|
|
|
if (client->message->tsigkey->generated) {
|
|
|
|
dns_name_format(client->message->tsigkey->creator,
|
|
|
|
cnamebuf, sizeof(cnamebuf));
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT,
|
|
|
|
ISC_LOG_ERROR,
|
|
|
|
"request has invalid signature: "
|
|
|
|
"TSIG %s (%s): %s (%s)", namebuf,
|
|
|
|
cnamebuf,
|
|
|
|
isc_result_totext(result),
|
|
|
|
tsigrcode);
|
|
|
|
} else {
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT,
|
|
|
|
ISC_LOG_ERROR,
|
|
|
|
"request has invalid signature: "
|
|
|
|
"TSIG %s: %s (%s)", namebuf,
|
|
|
|
isc_result_totext(result),
|
|
|
|
tsigrcode);
|
|
|
|
}
|
2004-02-09 23:25:22 +00:00
|
|
|
} else {
|
2007-08-22 00:42:42 +00:00
|
|
|
status = client->message->sig0status;
|
|
|
|
isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1);
|
|
|
|
tresult = dns_tsigrcode_totext(status, &b);
|
|
|
|
INSIST(tresult == ISC_R_SUCCESS);
|
|
|
|
tsigrcode[isc_buffer_usedlength(&b)] = '\0';
|
2004-02-09 23:25:22 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
|
|
|
|
"request has invalid signature: %s (%s)",
|
|
|
|
isc_result_totext(result), tsigrcode);
|
|
|
|
}
|
2001-10-12 04:01:40 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-11-03 04:45:03 +00:00
|
|
|
if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
|
|
|
|
client->message->opcode == dns_opcode_update)) {
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_client_error(client, sigresult);
|
2001-02-14 03:54:53 +00:00
|
|
|
goto cleanup;
|
2000-09-13 01:30:34 +00:00
|
|
|
}
|
1999-12-10 18:14:49 +00:00
|
|
|
}
|
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2006-07-22 01:00:04 +00:00
|
|
|
* responses to ordinary queries. Note if you can't query the
|
|
|
|
* cache there is no point in setting RA.
|
1999-12-10 23:58:04 +00:00
|
|
|
*/
|
2000-04-06 20:26:39 +00:00
|
|
|
ra = ISC_FALSE;
|
|
|
|
if (client->view->resolver != NULL &&
|
|
|
|
client->view->recursion == ISC_TRUE &&
|
2007-03-29 06:36:31 +00:00
|
|
|
ns_client_checkaclsilent(client, NULL,
|
|
|
|
client->view->recursionacl,
|
2006-07-22 01:00:04 +00:00
|
|
|
ISC_TRUE) == ISC_R_SUCCESS &&
|
2007-03-29 06:36:31 +00:00
|
|
|
ns_client_checkaclsilent(client, NULL,
|
2010-09-24 05:09:03 +00:00
|
|
|
client->view->cacheacl,
|
2007-03-29 06:36:31 +00:00
|
|
|
ISC_TRUE) == ISC_R_SUCCESS &&
|
2009-03-03 01:36:17 +00:00
|
|
|
ns_client_checkaclsilent(client, &client->destaddr,
|
2007-03-29 06:36:31 +00:00
|
|
|
client->view->recursiononacl,
|
|
|
|
ISC_TRUE) == ISC_R_SUCCESS &&
|
2009-03-03 01:36:17 +00:00
|
|
|
ns_client_checkaclsilent(client, &client->destaddr,
|
2010-09-24 05:09:03 +00:00
|
|
|
client->view->cacheonacl,
|
2002-05-16 04:05:42 +00:00
|
|
|
ISC_TRUE) == ISC_R_SUCCESS)
|
1999-12-10 23:58:04 +00:00
|
|
|
ra = ISC_TRUE;
|
2000-04-06 20:26:39 +00:00
|
|
|
|
1999-12-10 23:58:04 +00:00
|
|
|
if (ra == ISC_TRUE)
|
|
|
|
client->attributes |= NS_CLIENTATTR_RA;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
2002-05-16 04:05:42 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT,
|
|
|
|
ISC_LOG_DEBUG(3), ra ? "recursion available" :
|
2007-03-29 06:36:31 +00:00
|
|
|
"recursion not available");
|
2002-05-16 04:05:42 +00:00
|
|
|
|
2006-01-05 00:01:46 +00:00
|
|
|
/*
|
|
|
|
* Adjust maximum UDP response size for this client.
|
|
|
|
*/
|
|
|
|
if (client->udpsize > 512) {
|
|
|
|
dns_peer_t *peer = NULL;
|
|
|
|
isc_uint16_t udpsize = view->maxudp;
|
|
|
|
(void) dns_peerlist_peerbyaddr(view->peers, &netaddr, &peer);
|
|
|
|
if (peer != NULL)
|
|
|
|
dns_peer_getmaxudp(peer, &udpsize);
|
|
|
|
if (client->udpsize > udpsize)
|
|
|
|
client->udpsize = udpsize;
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/*
|
|
|
|
* Dispatch the request.
|
|
|
|
*/
|
|
|
|
switch (client->message->opcode) {
|
|
|
|
case dns_opcode_query:
|
|
|
|
CTRACE("query");
|
2015-10-02 12:32:42 -07:00
|
|
|
#ifdef HAVE_DNSTAP
|
|
|
|
if ((client->message->flags & DNS_MESSAGEFLAG_RD) != 0)
|
|
|
|
dtmsgtype = DNS_DTTYPE_CQ;
|
|
|
|
else
|
|
|
|
dtmsgtype = DNS_DTTYPE_AQ;
|
|
|
|
|
|
|
|
dns_dt_send(view, dtmsgtype, &client->peeraddr,
|
|
|
|
TCP_CLIENT(client), NULL,
|
|
|
|
&client->requesttime, NULL, buffer);
|
|
|
|
#endif /* HAVE_DNSTAP */
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_query_start(client);
|
|
|
|
break;
|
1999-08-20 06:03:02 +00:00
|
|
|
case dns_opcode_update:
|
|
|
|
CTRACE("update");
|
2001-01-29 19:49:52 +00:00
|
|
|
ns_client_settimeout(client, 60);
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_update_start(client, sigresult);
|
1999-08-20 06:03:02 +00:00
|
|
|
break;
|
|
|
|
case dns_opcode_notify:
|
|
|
|
CTRACE("notify");
|
2001-01-29 19:49:52 +00:00
|
|
|
ns_client_settimeout(client, 60);
|
1999-12-16 01:23:17 +00:00
|
|
|
ns_notify_start(client);
|
1999-08-20 06:03:02 +00:00
|
|
|
break;
|
1999-07-24 01:17:44 +00:00
|
|
|
case dns_opcode_iquery:
|
|
|
|
CTRACE("iquery");
|
1999-11-30 22:35:43 +00:00
|
|
|
ns_client_error(client, DNS_R_NOTIMP);
|
2000-02-04 00:46:58 +00:00
|
|
|
break;
|
1999-07-24 01:17:44 +00:00
|
|
|
default:
|
|
|
|
CTRACE("unknown opcode");
|
|
|
|
ns_client_error(client, DNS_R_NOTIMP);
|
|
|
|
}
|
2000-01-27 01:00:16 +00:00
|
|
|
|
2001-02-14 03:54:53 +00:00
|
|
|
cleanup:
|
|
|
|
return;
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_timeout(isc_task_t *task, isc_event_t *event) {
|
|
|
|
ns_client_t *client;
|
|
|
|
|
|
|
|
REQUIRE(event != NULL);
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE ||
|
|
|
|
event->ev_type == ISC_TIMEREVENT_IDLE);
|
|
|
|
client = event->ev_arg;
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(task == client->task);
|
|
|
|
REQUIRE(client->timer != NULL);
|
|
|
|
|
2000-04-28 01:24:18 +00:00
|
|
|
UNUSED(task);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("timeout");
|
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
if (client->shutdown != NULL) {
|
|
|
|
(client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
|
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
if (client->newstate > NS_CLIENTSTATE_READY)
|
|
|
|
client->newstate = NS_CLIENTSTATE_READY;
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)exit_check(client);
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
2005-10-16 23:21:25 +00:00
|
|
|
static isc_result_t
|
|
|
|
get_clientmctx(ns_clientmgr_t *manager, isc_mem_t **mctxp) {
|
|
|
|
isc_mem_t *clientmctx;
|
|
|
|
isc_result_t result;
|
2011-10-25 16:21:21 +00:00
|
|
|
#if NMCTXS > 0
|
|
|
|
unsigned int nextmctx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MTRACE("clientmctx");
|
2005-10-16 23:21:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Caller must be holding the manager lock.
|
|
|
|
*/
|
2008-03-31 05:00:30 +00:00
|
|
|
if (ns_g_clienttest) {
|
|
|
|
result = isc_mem_create(0, 0, mctxp);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
isc_mem_setname(*mctxp, "client", NULL);
|
|
|
|
return (result);
|
|
|
|
}
|
2006-05-29 01:27:58 +00:00
|
|
|
#if NMCTXS > 0
|
2011-10-25 16:21:21 +00:00
|
|
|
nextmctx = manager->nextmctx++;
|
|
|
|
if (manager->nextmctx == NMCTXS)
|
|
|
|
manager->nextmctx = 0;
|
|
|
|
|
|
|
|
INSIST(nextmctx < NMCTXS);
|
|
|
|
|
|
|
|
clientmctx = manager->mctxpool[nextmctx];
|
2005-10-16 23:21:25 +00:00
|
|
|
if (clientmctx == NULL) {
|
|
|
|
result = isc_mem_create(0, 0, &clientmctx);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
2008-03-31 05:00:30 +00:00
|
|
|
isc_mem_setname(clientmctx, "client", NULL);
|
2005-10-16 23:21:25 +00:00
|
|
|
|
2011-10-25 16:21:21 +00:00
|
|
|
manager->mctxpool[nextmctx] = clientmctx;
|
2005-10-16 23:21:25 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
clientmctx = manager->mctx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
isc_mem_attach(clientmctx, mctxp);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
static isc_result_t
|
2005-03-15 00:46:29 +00:00
|
|
|
client_create(ns_clientmgr_t *manager, ns_client_t **clientp) {
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_client_t *client;
|
|
|
|
isc_result_t result;
|
2005-06-04 05:32:50 +00:00
|
|
|
isc_mem_t *mctx = NULL;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Caller must be holding the manager lock.
|
|
|
|
*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Note: creating a client does not add the client to the
|
1999-12-22 16:59:05 +00:00
|
|
|
* manager's client list or set the client's manager pointer.
|
|
|
|
* The caller is responsible for that.
|
1999-07-24 01:17:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(clientp != NULL && *clientp == NULL);
|
|
|
|
|
2005-10-16 23:21:25 +00:00
|
|
|
result = get_clientmctx(manager, &mctx);
|
2005-06-04 05:32:50 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
client = isc_mem_get(mctx, sizeof(*client));
|
|
|
|
if (client == NULL) {
|
|
|
|
isc_mem_detach(&mctx);
|
1999-07-24 01:17:44 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
2005-06-04 05:32:50 +00:00
|
|
|
}
|
|
|
|
client->mctx = mctx;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
client->task = NULL;
|
2000-04-12 01:41:21 +00:00
|
|
|
result = isc_task_create(manager->taskmgr, 0, &client->task);
|
1999-07-24 01:17:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_client;
|
2000-01-25 19:31:23 +00:00
|
|
|
isc_task_setname(client->task, "client", client);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
client->timer = NULL;
|
|
|
|
result = isc_timer_create(manager->timermgr, isc_timertype_inactive,
|
|
|
|
NULL, NULL, client->task, client_timeout,
|
|
|
|
client, &client->timer);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_task;
|
2001-01-29 19:49:52 +00:00
|
|
|
client->timerset = ISC_FALSE;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2012-09-29 13:07:09 +10:00
|
|
|
client->delaytimer = NULL;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
client->message = NULL;
|
2005-06-04 05:32:50 +00:00
|
|
|
result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE,
|
1999-07-24 01:17:44 +00:00
|
|
|
&client->message);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_timer;
|
|
|
|
|
|
|
|
/* XXXRTH Hardwired constants */
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
client->sendevent = isc_socket_socketevent(client->mctx, client,
|
|
|
|
ISC_SOCKEVENT_SENDDONE,
|
|
|
|
client_senddone, client);
|
2001-02-22 23:16:59 +00:00
|
|
|
if (client->sendevent == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2001-03-06 01:24:40 +00:00
|
|
|
goto cleanup_message;
|
2001-02-22 23:16:59 +00:00
|
|
|
}
|
2001-02-12 21:45:37 +00:00
|
|
|
|
2005-06-04 05:32:50 +00:00
|
|
|
client->recvbuf = isc_mem_get(client->mctx, RECV_BUFFER_SIZE);
|
2001-02-22 23:16:59 +00:00
|
|
|
if (client->recvbuf == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2001-02-12 21:45:37 +00:00
|
|
|
goto cleanup_sendevent;
|
2001-02-22 23:16:59 +00:00
|
|
|
}
|
2001-02-12 21:45:37 +00:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
client->recvevent = isc_socket_socketevent(client->mctx, client,
|
|
|
|
ISC_SOCKEVENT_RECVDONE,
|
|
|
|
client_request, client);
|
2001-02-22 23:16:59 +00:00
|
|
|
if (client->recvevent == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2001-02-12 21:45:37 +00:00
|
|
|
goto cleanup_recvbuf;
|
2001-02-22 23:16:59 +00:00
|
|
|
}
|
2001-01-27 02:08:07 +00:00
|
|
|
|
1999-07-28 02:20:36 +00:00
|
|
|
client->magic = NS_CLIENT_MAGIC;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->manager = NULL;
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = NS_CLIENTSTATE_INACTIVE;
|
|
|
|
client->newstate = NS_CLIENTSTATE_MAX;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->naccepts = 0;
|
|
|
|
client->nreads = 0;
|
|
|
|
client->nsends = 0;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->nrecvs = 0;
|
2004-07-23 02:57:27 +00:00
|
|
|
client->nupdates = 0;
|
2001-11-14 22:00:22 +00:00
|
|
|
client->nctls = 0;
|
2000-02-11 20:56:19 +00:00
|
|
|
client->references = 0;
|
1999-07-24 01:17:44 +00:00
|
|
|
client->attributes = 0;
|
1999-08-05 22:14:43 +00:00
|
|
|
client->view = NULL;
|
1999-07-24 01:17:44 +00:00
|
|
|
client->dispatch = NULL;
|
2001-01-27 02:08:07 +00:00
|
|
|
client->udpsocket = NULL;
|
1999-08-05 01:51:32 +00:00
|
|
|
client->tcplistener = NULL;
|
|
|
|
client->tcpsocket = NULL;
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_FALSE;
|
2000-06-22 00:05:11 +00:00
|
|
|
client->tcpbuf = NULL;
|
1999-09-02 01:52:31 +00:00
|
|
|
client->opt = NULL;
|
1999-10-07 19:43:18 +00:00
|
|
|
client->udpsize = 512;
|
2013-03-22 12:27:54 -07:00
|
|
|
client->dscp = -1;
|
2000-11-13 21:34:03 +00:00
|
|
|
client->extflags = 0;
|
2005-06-07 00:16:01 +00:00
|
|
|
client->ednsversion = -1;
|
1999-08-03 01:21:00 +00:00
|
|
|
client->next = NULL;
|
2000-01-12 18:01:12 +00:00
|
|
|
client->shutdown = NULL;
|
|
|
|
client->shutdown_arg = NULL;
|
2011-05-05 23:44:52 +00:00
|
|
|
client->signer = NULL;
|
1999-12-10 18:14:49 +00:00
|
|
|
dns_name_init(&client->signername, NULL);
|
2000-01-11 21:18:22 +00:00
|
|
|
client->mortal = ISC_FALSE;
|
2015-01-20 16:10:30 -08:00
|
|
|
client->pipelined = ISC_FALSE;
|
2000-01-15 00:36:26 +00:00
|
|
|
client->tcpquota = NULL;
|
|
|
|
client->recursionquota = NULL;
|
2000-01-11 21:18:22 +00:00
|
|
|
client->interface = NULL;
|
2000-04-04 18:28:51 +00:00
|
|
|
client->peeraddr_valid = ISC_FALSE;
|
2014-08-28 22:05:57 -07:00
|
|
|
client->ecs_addrlen = 0;
|
|
|
|
client->ecs_scope = 0;
|
2012-05-14 11:50:00 -07:00
|
|
|
#ifdef ALLOW_FILTER_AAAA
|
|
|
|
client->filter_aaaa = dns_aaaa_ok;
|
2012-01-31 06:58:39 +00:00
|
|
|
#endif
|
2012-08-22 19:19:30 +10:00
|
|
|
client->needshutdown = ns_g_clienttest;
|
|
|
|
|
2000-07-26 17:39:12 +00:00
|
|
|
ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
|
2000-07-26 23:45:55 +00:00
|
|
|
NS_EVENT_CLIENTCONTROL, client_start, client, client,
|
2000-07-26 17:39:12 +00:00
|
|
|
NULL, NULL);
|
2001-03-19 20:52:21 +00:00
|
|
|
/*
|
|
|
|
* Initialize FORMERR cache to sentinel value that will not match
|
|
|
|
* any actual FORMERR response.
|
|
|
|
*/
|
|
|
|
isc_sockaddr_any(&client->formerrcache.addr);
|
|
|
|
client->formerrcache.time = 0;
|
|
|
|
client->formerrcache.id = 0;
|
1999-07-24 01:17:44 +00:00
|
|
|
ISC_LINK_INIT(client, link);
|
2011-10-10 22:57:14 +00:00
|
|
|
ISC_LINK_INIT(client, rlink);
|
|
|
|
ISC_QLINK_INIT(client, ilink);
|
1999-07-28 02:20:36 +00:00
|
|
|
|
1999-07-29 00:55:35 +00:00
|
|
|
/*
|
|
|
|
* 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)).
|
|
|
|
*/
|
|
|
|
result = ns_query_init(client);
|
1999-07-28 02:20:36 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2001-02-12 21:45:37 +00:00
|
|
|
goto cleanup_recvevent;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2001-10-30 00:48:25 +00:00
|
|
|
result = isc_task_onshutdown(client->task, client_shutdown, client);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_query;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
CTRACE("create");
|
|
|
|
|
|
|
|
*clientp = client;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2001-10-30 00:48:25 +00:00
|
|
|
cleanup_query:
|
|
|
|
ns_query_free(client);
|
|
|
|
|
2001-02-12 21:45:37 +00:00
|
|
|
cleanup_recvevent:
|
|
|
|
isc_event_free((isc_event_t **)&client->recvevent);
|
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
cleanup_recvbuf:
|
2005-06-04 05:32:50 +00:00
|
|
|
isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
|
2001-01-27 02:08:07 +00:00
|
|
|
|
2001-02-12 21:45:37 +00:00
|
|
|
cleanup_sendevent:
|
|
|
|
isc_event_free((isc_event_t **)&client->sendevent);
|
|
|
|
|
1999-07-29 00:55:35 +00:00
|
|
|
client->magic = 0;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
cleanup_message:
|
|
|
|
dns_message_destroy(&client->message);
|
|
|
|
|
|
|
|
cleanup_timer:
|
|
|
|
isc_timer_detach(&client->timer);
|
|
|
|
|
|
|
|
cleanup_task:
|
|
|
|
isc_task_detach(&client->task);
|
|
|
|
|
|
|
|
cleanup_client:
|
2005-06-04 05:32:50 +00:00
|
|
|
isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
static void
|
|
|
|
client_read(ns_client_t *client) {
|
|
|
|
isc_result_t result;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
CTRACE("read");
|
|
|
|
|
|
|
|
result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
|
|
|
|
client_request, client);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
2000-02-10 22:16:56 +00:00
|
|
|
goto fail;
|
|
|
|
|
2000-02-14 23:56:47 +00:00
|
|
|
/*
|
|
|
|
* Set a timeout to limit the amount of time we will wait
|
|
|
|
* for a request on this TCP connection.
|
|
|
|
*/
|
2001-01-29 19:49:52 +00:00
|
|
|
ns_client_settimeout(client, 30);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
client->state = client->newstate = NS_CLIENTSTATE_READING;
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->nreads == 0);
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
1999-12-22 16:59:05 +00:00
|
|
|
client->nreads++;
|
2000-02-10 22:16:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
fail:
|
2000-08-01 01:33:37 +00:00
|
|
|
ns_client_next(client, result);
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_newconn(isc_task_t *task, isc_event_t *event) {
|
2000-04-17 19:22:44 +00:00
|
|
|
ns_client_t *client = event->ev_arg;
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_t result;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
|
1999-08-05 01:51:32 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(client->task == task);
|
|
|
|
|
2000-04-28 01:24:18 +00:00
|
|
|
UNUSED(task);
|
|
|
|
|
2000-02-10 22:16:56 +00:00
|
|
|
INSIST(client->state == NS_CLIENTSTATE_READY);
|
2000-07-26 17:39:12 +00:00
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->naccepts == 1);
|
|
|
|
client->naccepts--;
|
2000-01-11 21:18:22 +00:00
|
|
|
|
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
INSIST(client->interface->ntcpcurrent > 0);
|
|
|
|
client->interface->ntcpcurrent--;
|
|
|
|
UNLOCK(&client->interface->lock);
|
|
|
|
|
2000-02-10 00:28:36 +00:00
|
|
|
/*
|
|
|
|
* We must take ownership of the new socket before the exit
|
2000-02-17 18:18:24 +00:00
|
|
|
* check to make sure it gets destroyed if we decide to exit.
|
2000-02-10 00:28:36 +00:00
|
|
|
*/
|
2000-02-17 18:18:24 +00:00
|
|
|
if (nevent->result == ISC_R_SUCCESS) {
|
|
|
|
client->tcpsocket = nevent->newsocket;
|
2007-02-13 02:49:08 +00:00
|
|
|
isc_socket_setname(client->tcpsocket, "client-tcp", NULL);
|
2000-02-17 18:18:24 +00:00
|
|
|
client->state = NS_CLIENTSTATE_READING;
|
2000-11-27 23:54:12 +00:00
|
|
|
INSIST(client->recursionquota == NULL);
|
2000-02-17 18:18:24 +00:00
|
|
|
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)isc_socket_getpeername(client->tcpsocket,
|
|
|
|
&client->peeraddr);
|
2000-04-04 18:28:51 +00:00
|
|
|
client->peeraddr_valid = ISC_TRUE;
|
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"new TCP connection");
|
2000-02-17 18:18:24 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* XXXRTH What should we do? We're trying to accept but
|
2007-03-29 06:36:31 +00:00
|
|
|
* it didn't work. If we just give up, then TCP
|
2000-02-17 18:18:24 +00:00
|
|
|
* service may eventually stop.
|
|
|
|
*
|
|
|
|
* For now, we just go idle.
|
|
|
|
*
|
|
|
|
* Going idle is probably the right thing if the
|
|
|
|
* I/O was canceled.
|
|
|
|
*/
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2000-02-17 18:18:24 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
2000-04-04 18:28:51 +00:00
|
|
|
"accept failed: %s",
|
2000-02-17 18:18:24 +00:00
|
|
|
isc_result_totext(nevent->result));
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-17 18:18:24 +00:00
|
|
|
if (exit_check(client))
|
|
|
|
goto freeevent;
|
|
|
|
|
|
|
|
if (nevent->result == ISC_R_SUCCESS) {
|
2000-11-03 02:45:55 +00:00
|
|
|
int match;
|
|
|
|
isc_netaddr_t netaddr;
|
|
|
|
|
2001-04-16 21:18:10 +00:00
|
|
|
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
|
|
|
|
|
|
|
|
if (ns_g_server->blackholeacl != NULL &&
|
|
|
|
dns_acl_match(&netaddr, NULL,
|
2007-03-29 06:36:31 +00:00
|
|
|
ns_g_server->blackholeacl,
|
2001-04-16 21:18:10 +00:00
|
|
|
&ns_g_server->aclenv,
|
|
|
|
&match, NULL) == ISC_R_SUCCESS &&
|
|
|
|
match > 0)
|
|
|
|
{
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"blackholed connection attempt");
|
2002-04-03 05:30:03 +00:00
|
|
|
client->newstate = NS_CLIENTSTATE_READY;
|
|
|
|
(void)exit_check(client);
|
2001-04-16 21:18:10 +00:00
|
|
|
goto freeevent;
|
|
|
|
}
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->tcpmsg_valid == ISC_FALSE);
|
1999-08-05 01:51:32 +00:00
|
|
|
dns_tcpmsg_init(client->mctx, client->tcpsocket,
|
|
|
|
&client->tcpmsg);
|
1999-12-22 16:59:05 +00:00
|
|
|
client->tcpmsg_valid = ISC_TRUE;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Let a new client take our place immediately, before
|
|
|
|
* we wait for a request packet. If we don't,
|
2000-11-03 02:45:55 +00:00
|
|
|
* telnetting to port 53 (once per CPU) will
|
2009-01-17 11:04:25 +00:00
|
|
|
* deny service to legitimate TCP clients.
|
2000-01-07 19:20:25 +00:00
|
|
|
*/
|
2015-01-20 16:10:30 -08:00
|
|
|
client->pipelined = ISC_FALSE;
|
2000-01-15 00:36:26 +00:00
|
|
|
result = isc_quota_attach(&ns_g_server->tcpquota,
|
|
|
|
&client->tcpquota);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
result = ns_client_replace(client);
|
2000-01-11 21:18:22 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
2000-01-11 21:18:22 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
|
2015-01-20 16:10:30 -08:00
|
|
|
"no more TCP clients(accept): %s",
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_totext(result));
|
2015-01-20 16:10:30 -08:00
|
|
|
} else if (ns_g_server->keepresporder == NULL ||
|
|
|
|
!allowed(&netaddr, NULL, NULL, 0, NULL,
|
|
|
|
ns_g_server->keepresporder)) {
|
|
|
|
client->pipelined = ISC_TRUE;
|
2000-01-11 21:18:22 +00:00
|
|
|
}
|
2000-11-03 02:45:55 +00:00
|
|
|
|
1999-08-05 01:51:32 +00:00
|
|
|
client_read(client);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-17 18:18:24 +00:00
|
|
|
freeevent:
|
1999-08-05 01:51:32 +00:00
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_accept(ns_client_t *client) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
CTRACE("accept");
|
|
|
|
|
|
|
|
result = isc_socket_accept(client->tcplistener, client->task,
|
|
|
|
client_newconn, client);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_socket_accept() failed: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
/*
|
|
|
|
* XXXRTH What should we do? We're trying to accept but
|
2007-03-29 06:36:31 +00:00
|
|
|
* it didn't work. If we just give up, then TCP
|
1999-08-05 01:51:32 +00:00
|
|
|
* service may eventually stop.
|
|
|
|
*
|
|
|
|
* For now, we just go idle.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
1999-12-22 16:59:05 +00:00
|
|
|
INSIST(client->naccepts == 0);
|
|
|
|
client->naccepts++;
|
2000-01-11 21:18:22 +00:00
|
|
|
LOCK(&client->interface->lock);
|
|
|
|
client->interface->ntcpcurrent++;
|
|
|
|
UNLOCK(&client->interface->lock);
|
1999-12-22 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
2001-01-27 02:08:07 +00:00
|
|
|
static void
|
|
|
|
client_udprecv(ns_client_t *client) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_region_t r;
|
|
|
|
|
|
|
|
CTRACE("udprecv");
|
|
|
|
|
|
|
|
r.base = client->recvbuf;
|
|
|
|
r.length = RECV_BUFFER_SIZE;
|
2001-02-12 21:45:37 +00:00
|
|
|
result = isc_socket_recv2(client->udpsocket, &r, 1,
|
|
|
|
client->task, client->recvevent, 0);
|
2001-01-27 02:08:07 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2004-09-26 22:34:32 +00:00
|
|
|
"isc_socket_recv2() failed: %s",
|
2001-01-27 02:08:07 +00:00
|
|
|
isc_result_totext(result));
|
|
|
|
/*
|
2001-02-23 22:38:28 +00:00
|
|
|
* This cannot happen in the current implementation, since
|
2001-11-14 02:03:43 +00:00
|
|
|
* isc_socket_recv2() cannot fail if flags == 0.
|
2001-01-27 02:08:07 +00:00
|
|
|
*
|
2001-02-23 22:38:28 +00:00
|
|
|
* If this does fail, we just go idle.
|
2001-01-27 02:08:07 +00:00
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
INSIST(client->nrecvs == 0);
|
|
|
|
client->nrecvs++;
|
|
|
|
}
|
|
|
|
|
1999-12-22 16:59:05 +00:00
|
|
|
void
|
2000-02-11 20:56:19 +00:00
|
|
|
ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
|
|
|
|
REQUIRE(NS_CLIENT_VALID(source));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-11 20:56:19 +00:00
|
|
|
source->references++;
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_client_log(source, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"ns_client_attach: ref = %d", source->references);
|
2000-02-11 20:56:19 +00:00
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_detach(ns_client_t **clientp) {
|
|
|
|
ns_client_t *client = *clientp;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-11 20:56:19 +00:00
|
|
|
client->references--;
|
|
|
|
INSIST(client->references >= 0);
|
|
|
|
*clientp = NULL;
|
2000-09-13 01:30:34 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
|
|
|
|
"ns_client_detach: ref = %d", client->references);
|
2001-11-14 19:11:06 +00:00
|
|
|
(void)exit_check(client);
|
1999-12-22 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
2000-01-06 01:09:27 +00:00
|
|
|
ns_client_shuttingdown(ns_client_t *client) {
|
2000-05-14 04:02:10 +00:00
|
|
|
return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
|
2000-01-06 01:09:27 +00:00
|
|
|
}
|
|
|
|
|
2000-01-11 21:18:22 +00:00
|
|
|
isc_result_t
|
2000-01-15 00:36:26 +00:00
|
|
|
ns_client_replace(ns_client_t *client) {
|
2000-01-07 19:20:25 +00:00
|
|
|
isc_result_t result;
|
2015-01-20 16:10:30 -08:00
|
|
|
isc_boolean_t tcp;
|
2000-05-02 01:16:21 +00:00
|
|
|
|
2000-01-07 19:20:25 +00:00
|
|
|
CTRACE("replace");
|
|
|
|
|
2012-10-06 14:20:45 +10:00
|
|
|
REQUIRE(client != NULL);
|
|
|
|
REQUIRE(client->manager != NULL);
|
|
|
|
|
2015-01-20 16:10:30 -08:00
|
|
|
tcp = TCP_CLIENT(client);
|
|
|
|
if (tcp && client->pipelined) {
|
|
|
|
result = get_worker(client->manager, client->interface,
|
|
|
|
client->tcpsocket);
|
|
|
|
} else {
|
|
|
|
result = get_client(client->manager, client->interface,
|
|
|
|
client->dispatch, tcp);
|
|
|
|
}
|
2000-01-07 19:20:25 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* The responsibility for listening for new requests is hereby
|
|
|
|
* transferred to the new client. Therefore, the old client
|
2000-01-17 23:48:15 +00:00
|
|
|
* should refrain from listening for any more requests.
|
2000-01-07 19:20:25 +00:00
|
|
|
*/
|
2000-01-11 21:18:22 +00:00
|
|
|
client->mortal = ISC_TRUE;
|
2000-01-07 19:20:25 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
/***
|
|
|
|
*** Client Manager
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
clientmgr_destroy(ns_clientmgr_t *manager) {
|
2005-10-16 23:21:25 +00:00
|
|
|
#if NMCTXS > 0
|
|
|
|
int i;
|
|
|
|
#endif
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
REQUIRE(ISC_LIST_EMPTY(manager->clients));
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
MTRACE("clientmgr_destroy");
|
|
|
|
|
2005-10-16 23:21:25 +00:00
|
|
|
#if NMCTXS > 0
|
|
|
|
for (i = 0; i < NMCTXS; i++) {
|
|
|
|
if (manager->mctxpool[i] != NULL)
|
|
|
|
isc_mem_detach(&manager->mctxpool[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
ISC_QUEUE_DESTROY(manager->inactive);
|
2000-09-26 22:09:20 +00:00
|
|
|
DESTROYLOCK(&manager->lock);
|
2011-10-10 22:57:14 +00:00
|
|
|
DESTROYLOCK(&manager->listlock);
|
|
|
|
DESTROYLOCK(&manager->reclock);
|
1999-07-24 01:17:44 +00:00
|
|
|
manager->magic = 0;
|
2001-09-19 23:08:24 +00:00
|
|
|
isc_mem_put(manager->mctx, manager, sizeof(*manager));
|
1999-07-24 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
|
|
|
|
isc_timermgr_t *timermgr, ns_clientmgr_t **managerp)
|
|
|
|
{
|
|
|
|
ns_clientmgr_t *manager;
|
|
|
|
isc_result_t result;
|
2005-10-16 23:21:25 +00:00
|
|
|
#if NMCTXS > 0
|
|
|
|
int i;
|
|
|
|
#endif
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2001-09-19 23:08:24 +00:00
|
|
|
manager = isc_mem_get(mctx, sizeof(*manager));
|
1999-07-24 01:17:44 +00:00
|
|
|
if (manager == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
result = isc_mutex_init(&manager->lock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_manager;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
result = isc_mutex_init(&manager->listlock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_lock;
|
|
|
|
|
|
|
|
result = isc_mutex_init(&manager->reclock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_listlock;
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
manager->mctx = mctx;
|
|
|
|
manager->taskmgr = taskmgr;
|
|
|
|
manager->timermgr = timermgr;
|
|
|
|
manager->exiting = ISC_FALSE;
|
2011-10-10 22:57:14 +00:00
|
|
|
ISC_LIST_INIT(manager->clients);
|
2001-10-24 03:10:18 +00:00
|
|
|
ISC_LIST_INIT(manager->recursing);
|
2011-10-10 22:57:14 +00:00
|
|
|
ISC_QUEUE_INIT(manager->inactive, ilink);
|
2005-10-16 23:21:25 +00:00
|
|
|
#if NMCTXS > 0
|
|
|
|
manager->nextmctx = 0;
|
|
|
|
for (i = 0; i < NMCTXS; i++)
|
|
|
|
manager->mctxpool[i] = NULL; /* will be created on-demand */
|
|
|
|
#endif
|
1999-07-24 01:17:44 +00:00
|
|
|
manager->magic = MANAGER_MAGIC;
|
|
|
|
|
|
|
|
MTRACE("create");
|
|
|
|
|
|
|
|
*managerp = manager;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
cleanup_listlock:
|
2012-11-30 18:50:38 +11:00
|
|
|
(void) isc_mutex_destroy(&manager->listlock);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
cleanup_lock:
|
2012-11-30 18:50:38 +11:00
|
|
|
(void) isc_mutex_destroy(&manager->lock);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
cleanup_manager:
|
2001-09-19 23:08:24 +00:00
|
|
|
isc_mem_put(manager->mctx, manager, sizeof(*manager));
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_clientmgr_destroy(ns_clientmgr_t **managerp) {
|
2012-08-22 19:19:30 +10:00
|
|
|
isc_result_t result;
|
1999-07-24 01:17:44 +00:00
|
|
|
ns_clientmgr_t *manager;
|
|
|
|
ns_client_t *client;
|
2012-08-22 19:19:30 +10:00
|
|
|
isc_boolean_t need_destroy = ISC_FALSE, unlock = ISC_FALSE;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
REQUIRE(managerp != NULL);
|
|
|
|
manager = *managerp;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
MTRACE("destroy");
|
|
|
|
|
2012-08-22 19:19:30 +10:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
result = isc_task_beginexclusive(ns_g_server->task);
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
unlock = ISC_TRUE;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
manager->exiting = ISC_TRUE;
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
for (client = ISC_LIST_HEAD(manager->clients);
|
2000-01-28 23:35:53 +00:00
|
|
|
client != NULL;
|
|
|
|
client = ISC_LIST_NEXT(client, link))
|
|
|
|
isc_task_shutdown(client->task);
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
if (ISC_LIST_EMPTY(manager->clients))
|
1999-07-24 01:17:44 +00:00
|
|
|
need_destroy = ISC_TRUE;
|
|
|
|
|
2012-08-22 19:19:30 +10:00
|
|
|
if (unlock)
|
|
|
|
isc_task_endexclusive(ns_g_server->task);
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
if (need_destroy)
|
|
|
|
clientmgr_destroy(manager);
|
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-04 16:04:22 +00:00
|
|
|
static isc_result_t
|
|
|
|
get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
|
|
|
|
dns_dispatch_t *disp, isc_boolean_t tcp)
|
|
|
|
{
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
isc_event_t *ev;
|
|
|
|
ns_client_t *client;
|
|
|
|
MTRACE("get client");
|
|
|
|
|
2012-10-23 22:04:06 -07:00
|
|
|
REQUIRE(manager != NULL);
|
|
|
|
|
|
|
|
if (manager->exiting)
|
2012-08-22 19:19:30 +10:00
|
|
|
return (ISC_R_SHUTTINGDOWN);
|
|
|
|
|
2011-10-04 16:04:22 +00:00
|
|
|
/*
|
|
|
|
* Allocate a client. First try to get a recycled one;
|
|
|
|
* if that fails, make a new one.
|
|
|
|
*/
|
|
|
|
client = NULL;
|
|
|
|
if (!ns_g_clienttest)
|
2011-10-10 22:57:14 +00:00
|
|
|
ISC_QUEUE_POP(manager->inactive, ilink, client);
|
|
|
|
|
|
|
|
if (client != NULL)
|
2011-10-04 16:04:22 +00:00
|
|
|
MTRACE("recycle");
|
2011-10-10 22:57:14 +00:00
|
|
|
else {
|
2011-10-04 16:04:22 +00:00
|
|
|
MTRACE("create new");
|
2011-10-10 22:57:14 +00:00
|
|
|
|
2011-10-25 16:21:21 +00:00
|
|
|
LOCK(&manager->lock);
|
2011-10-04 16:04:22 +00:00
|
|
|
result = client_create(manager, &client);
|
2011-10-25 16:21:21 +00:00
|
|
|
UNLOCK(&manager->lock);
|
2011-10-04 16:04:22 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
LOCK(&manager->listlock);
|
|
|
|
ISC_LIST_APPEND(manager->clients, client, link);
|
|
|
|
UNLOCK(&manager->listlock);
|
2011-10-04 16:04:22 +00:00
|
|
|
}
|
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
client->manager = manager;
|
2011-10-04 16:04:22 +00:00
|
|
|
ns_interface_attach(ifp, &client->interface);
|
|
|
|
client->state = NS_CLIENTSTATE_READY;
|
|
|
|
INSIST(client->recursionquota == NULL);
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
client->dscp = ifp->dscp;
|
|
|
|
|
2011-10-04 16:04:22 +00:00
|
|
|
if (tcp) {
|
|
|
|
client->attributes |= NS_CLIENTATTR_TCP;
|
|
|
|
isc_socket_attach(ifp->tcpsocket,
|
|
|
|
&client->tcplistener);
|
|
|
|
} else {
|
|
|
|
isc_socket_t *sock;
|
|
|
|
|
|
|
|
dns_dispatch_attach(disp, &client->dispatch);
|
|
|
|
sock = dns_dispatch_getsocket(client->dispatch);
|
|
|
|
isc_socket_attach(sock, &client->udpsocket);
|
|
|
|
}
|
|
|
|
|
|
|
|
INSIST(client->nctls == 0);
|
|
|
|
client->nctls++;
|
|
|
|
ev = &client->ctlevent;
|
|
|
|
isc_task_send(client->task, &ev);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2015-01-20 16:10:30 -08:00
|
|
|
static isc_result_t
|
2015-02-27 10:55:55 +11:00
|
|
|
get_worker(ns_clientmgr_t *manager, ns_interface_t *ifp, isc_socket_t *sock)
|
2015-01-20 16:10:30 -08:00
|
|
|
{
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
isc_event_t *ev;
|
|
|
|
ns_client_t *client;
|
|
|
|
MTRACE("get worker");
|
|
|
|
|
|
|
|
REQUIRE(manager != NULL);
|
|
|
|
|
|
|
|
if (manager->exiting)
|
|
|
|
return (ISC_R_SHUTTINGDOWN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a client. First try to get a recycled one;
|
|
|
|
* if that fails, make a new one.
|
|
|
|
*/
|
|
|
|
client = NULL;
|
|
|
|
if (!ns_g_clienttest)
|
|
|
|
ISC_QUEUE_POP(manager->inactive, ilink, client);
|
|
|
|
|
|
|
|
if (client != NULL)
|
|
|
|
MTRACE("recycle");
|
|
|
|
else {
|
|
|
|
MTRACE("create new");
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
result = client_create(manager, &client);
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
LOCK(&manager->listlock);
|
|
|
|
ISC_LIST_APPEND(manager->clients, client, link);
|
|
|
|
UNLOCK(&manager->listlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
client->manager = manager;
|
|
|
|
ns_interface_attach(ifp, &client->interface);
|
|
|
|
client->newstate = client->state = NS_CLIENTSTATE_WORKING;
|
|
|
|
INSIST(client->recursionquota == NULL);
|
|
|
|
client->tcpquota = &ns_g_server->tcpquota;
|
|
|
|
|
|
|
|
client->dscp = ifp->dscp;
|
|
|
|
|
|
|
|
client->attributes |= NS_CLIENTATTR_TCP;
|
|
|
|
client->pipelined = ISC_TRUE;
|
|
|
|
|
|
|
|
isc_socket_attach(ifp->tcpsocket, &client->tcplistener);
|
2015-02-27 10:55:55 +11:00
|
|
|
isc_socket_attach(sock, &client->tcpsocket);
|
2015-01-20 16:10:30 -08:00
|
|
|
isc_socket_setname(client->tcpsocket, "worker-tcp", NULL);
|
|
|
|
(void)isc_socket_getpeername(client->tcpsocket, &client->peeraddr);
|
|
|
|
client->peeraddr_valid = ISC_TRUE;
|
|
|
|
|
|
|
|
INSIST(client->tcpmsg_valid == ISC_FALSE);
|
|
|
|
dns_tcpmsg_init(client->mctx, client->tcpsocket, &client->tcpmsg);
|
|
|
|
client->tcpmsg_valid = ISC_TRUE;
|
|
|
|
|
|
|
|
INSIST(client->nctls == 0);
|
|
|
|
client->nctls++;
|
|
|
|
ev = &client->ctlevent;
|
|
|
|
isc_task_send(client->task, &ev);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-07-24 01:17:44 +00:00
|
|
|
isc_result_t
|
2000-01-17 23:48:15 +00:00
|
|
|
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
|
|
|
|
ns_interface_t *ifp, isc_boolean_t tcp)
|
1999-07-24 01:17:44 +00:00
|
|
|
{
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2011-07-28 04:04:37 +00:00
|
|
|
unsigned int disp;
|
1999-07-24 01:17:44 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(n > 0);
|
|
|
|
|
2000-01-17 23:48:15 +00:00
|
|
|
MTRACE("createclients");
|
1999-07-24 01:17:44 +00:00
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
for (disp = 0; disp < n; disp++) {
|
2011-10-04 16:04:22 +00:00
|
|
|
result = get_client(manager, ifp, ifp->udpdispatch[disp], tcp);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
break;
|
1999-08-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-11-30 02:49:38 +00:00
|
|
|
isc_sockaddr_t *
|
|
|
|
ns_client_getsockaddr(ns_client_t *client) {
|
2000-02-17 18:18:24 +00:00
|
|
|
return (&client->peeraddr);
|
1999-11-30 02:49:38 +00:00
|
|
|
}
|
2000-02-22 21:24:24 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2009-03-03 01:36:17 +00:00
|
|
|
ns_client_checkaclsilent(ns_client_t *client, isc_netaddr_t *netaddr,
|
2007-03-29 06:36:31 +00:00
|
|
|
dns_acl_t *acl, isc_boolean_t default_allow)
|
2000-02-22 21:24:24 +00:00
|
|
|
{
|
|
|
|
isc_result_t result;
|
2009-03-03 01:36:17 +00:00
|
|
|
isc_netaddr_t tmpnetaddr;
|
2014-08-28 22:05:57 -07:00
|
|
|
isc_netaddr_t *ecs_addr = NULL;
|
|
|
|
isc_uint8_t ecs_addrlen = 0;
|
2000-02-22 21:24:24 +00:00
|
|
|
int match;
|
|
|
|
|
|
|
|
if (acl == NULL) {
|
|
|
|
if (default_allow)
|
|
|
|
goto allow;
|
|
|
|
else
|
|
|
|
goto deny;
|
|
|
|
}
|
|
|
|
|
2009-03-03 01:36:17 +00:00
|
|
|
if (netaddr == NULL) {
|
|
|
|
isc_netaddr_fromsockaddr(&tmpnetaddr, &client->peeraddr);
|
|
|
|
netaddr = &tmpnetaddr;
|
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2014-08-28 22:05:57 -07:00
|
|
|
if ((client->attributes & NS_CLIENTATTR_HAVEECS) != 0) {
|
|
|
|
ecs_addr = &client->ecs_addr;
|
|
|
|
ecs_addrlen = client->ecs_addrlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = dns_acl_match2(netaddr, client->signer,
|
|
|
|
ecs_addr, ecs_addrlen, NULL, acl,
|
|
|
|
&ns_g_server->aclenv, &match, NULL);
|
2007-03-29 06:36:31 +00:00
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2000-02-22 21:24:24 +00:00
|
|
|
goto deny; /* Internal error, already logged. */
|
2014-08-28 22:05:57 -07:00
|
|
|
|
2000-02-22 21:24:24 +00:00
|
|
|
if (match > 0)
|
|
|
|
goto allow;
|
|
|
|
goto deny; /* Negative match or no match. */
|
|
|
|
|
|
|
|
allow:
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2000-02-22 21:24:24 +00:00
|
|
|
|
|
|
|
deny:
|
|
|
|
return (DNS_R_REFUSED);
|
|
|
|
}
|
|
|
|
|
2001-06-15 23:28:29 +00:00
|
|
|
isc_result_t
|
2007-03-29 06:36:31 +00:00
|
|
|
ns_client_checkacl(ns_client_t *client, isc_sockaddr_t *sockaddr,
|
2001-06-15 23:28:29 +00:00
|
|
|
const char *opname, dns_acl_t *acl,
|
|
|
|
isc_boolean_t default_allow, int log_level)
|
|
|
|
{
|
2009-03-03 01:36:17 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_netaddr_t netaddr;
|
2009-03-03 23:48:02 +00:00
|
|
|
|
2009-03-03 01:36:17 +00:00
|
|
|
if (sockaddr != NULL)
|
|
|
|
isc_netaddr_fromsockaddr(&netaddr, sockaddr);
|
|
|
|
|
|
|
|
result = ns_client_checkaclsilent(client, sockaddr ? &netaddr : NULL,
|
|
|
|
acl, default_allow);
|
2001-06-15 23:28:29 +00:00
|
|
|
|
2008-01-18 23:46:58 +00:00
|
|
|
if (result == ISC_R_SUCCESS)
|
2001-06-15 23:28:29 +00:00
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
|
|
|
|
"%s approved", opname);
|
|
|
|
else
|
|
|
|
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
|
|
|
|
NS_LOGMODULE_CLIENT,
|
|
|
|
log_level, "%s denied", opname);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2001-05-25 07:39:48 +00:00
|
|
|
static void
|
|
|
|
ns_client_name(ns_client_t *client, char *peerbuf, size_t len) {
|
|
|
|
if (client->peeraddr_valid)
|
2013-12-04 12:47:23 +11:00
|
|
|
isc_sockaddr_format(&client->peeraddr, peerbuf,
|
|
|
|
(unsigned int)len);
|
2001-05-25 07:39:48 +00:00
|
|
|
else
|
|
|
|
snprintf(peerbuf, len, "@%p", client);
|
|
|
|
}
|
|
|
|
|
2001-12-10 23:09:24 +00:00
|
|
|
void
|
2000-04-04 18:28:51 +00:00
|
|
|
ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
|
2007-03-29 06:36:31 +00:00
|
|
|
isc_logmodule_t *module, int level, const char *fmt, va_list ap)
|
2000-04-04 18:28:51 +00:00
|
|
|
{
|
2013-02-20 13:54:52 -08:00
|
|
|
char msgbuf[4096];
|
2000-12-19 19:21:39 +00:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
2011-11-03 21:14:22 +00:00
|
|
|
char signerbuf[DNS_NAME_FORMATSIZE], qnamebuf[DNS_NAME_FORMATSIZE];
|
2011-05-05 20:04:24 +00:00
|
|
|
const char *viewname = "";
|
2011-11-03 21:14:22 +00:00
|
|
|
const char *sep1 = "", *sep2 = "", *sep3 = "", *sep4 = "";
|
|
|
|
const char *signer = "", *qname = "";
|
|
|
|
dns_name_t *q = NULL;
|
2000-04-04 18:28:51 +00:00
|
|
|
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
2011-05-05 20:04:24 +00:00
|
|
|
|
2001-09-19 23:08:24 +00:00
|
|
|
ns_client_name(client, peerbuf, sizeof(peerbuf));
|
2011-05-05 20:04:24 +00:00
|
|
|
|
|
|
|
if (client->signer != NULL) {
|
|
|
|
dns_name_format(client->signer, signerbuf, sizeof(signerbuf));
|
|
|
|
sep1 = "/key ";
|
|
|
|
signer = signerbuf;
|
|
|
|
}
|
|
|
|
|
2011-11-03 21:14:22 +00:00
|
|
|
q = client->query.origqname != NULL
|
|
|
|
? client->query.origqname : client->query.qname;
|
|
|
|
if (q != NULL) {
|
|
|
|
dns_name_format(q, qnamebuf, sizeof(qnamebuf));
|
|
|
|
sep2 = " (";
|
|
|
|
sep3 = ")";
|
|
|
|
qname = qnamebuf;
|
|
|
|
}
|
|
|
|
|
2002-05-24 06:22:30 +00:00
|
|
|
if (client->view != NULL && strcmp(client->view->name, "_bind") != 0 &&
|
|
|
|
strcmp(client->view->name, "_default") != 0) {
|
2011-11-03 21:14:22 +00:00
|
|
|
sep4 = ": view ";
|
2011-05-05 20:04:24 +00:00
|
|
|
viewname = client->view->name;
|
2002-05-24 06:22:30 +00:00
|
|
|
}
|
2000-04-04 18:28:51 +00:00
|
|
|
|
2001-05-25 07:39:48 +00:00
|
|
|
isc_log_write(ns_g_lctx, category, module, level,
|
2011-11-03 21:14:22 +00:00
|
|
|
"client %s%s%s%s%s%s%s%s: %s",
|
|
|
|
peerbuf, sep1, signer, sep2, qname, sep3,
|
|
|
|
sep4, viewname, msgbuf);
|
2000-04-04 18:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_log(ns_client_t *client, isc_logcategory_t *category,
|
|
|
|
isc_logmodule_t *module, int level, const char *fmt, ...)
|
|
|
|
{
|
2000-05-02 01:16:21 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2000-08-08 23:56:10 +00:00
|
|
|
if (! isc_log_wouldlog(ns_g_lctx, level))
|
2000-07-13 00:21:27 +00:00
|
|
|
return;
|
|
|
|
|
2000-04-04 18:28:51 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ns_client_logv(client, category, module, level, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2001-03-11 06:19:39 +00:00
|
|
|
void
|
2003-01-21 06:11:46 +00:00
|
|
|
ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdatatype_t type,
|
2008-01-18 23:46:58 +00:00
|
|
|
dns_rdataclass_t rdclass, char *buf, size_t len)
|
2001-03-11 06:19:39 +00:00
|
|
|
{
|
2007-03-29 06:36:31 +00:00
|
|
|
char namebuf[DNS_NAME_FORMATSIZE];
|
|
|
|
char typebuf[DNS_RDATATYPE_FORMATSIZE];
|
|
|
|
char classbuf[DNS_RDATACLASS_FORMATSIZE];
|
|
|
|
|
|
|
|
dns_name_format(name, namebuf, sizeof(namebuf));
|
|
|
|
dns_rdatatype_format(type, typebuf, sizeof(typebuf));
|
|
|
|
dns_rdataclass_format(rdclass, classbuf, sizeof(classbuf));
|
|
|
|
(void)snprintf(buf, len, "%s '%s/%s/%s'", msg, namebuf, typebuf,
|
2003-01-21 06:11:46 +00:00
|
|
|
classbuf);
|
2001-03-11 06:19:39 +00:00
|
|
|
}
|
2001-05-25 07:39:48 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ns_client_dumpmessage(ns_client_t *client, const char *reason) {
|
|
|
|
isc_buffer_t buffer;
|
|
|
|
char *buf = NULL;
|
|
|
|
int len = 1024;
|
|
|
|
isc_result_t result;
|
|
|
|
|
2014-05-27 12:16:04 +10:00
|
|
|
if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(1)))
|
|
|
|
return;
|
|
|
|
|
2001-05-28 05:17:05 +00:00
|
|
|
/*
|
2001-06-15 22:35:42 +00:00
|
|
|
* Note that these are multiline debug messages. We want a newline
|
2001-05-28 05:17:05 +00:00
|
|
|
* to appear in the log after each message.
|
|
|
|
*/
|
2001-05-25 07:39:48 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
buf = isc_mem_get(client->mctx, len);
|
|
|
|
if (buf == NULL)
|
|
|
|
break;
|
|
|
|
isc_buffer_init(&buffer, buf, len);
|
|
|
|
result = dns_message_totext(client->message,
|
|
|
|
&dns_master_style_debug,
|
|
|
|
0, &buffer);
|
|
|
|
if (result == ISC_R_NOSPACE) {
|
|
|
|
isc_mem_put(client->mctx, buf, len);
|
|
|
|
len += 1024;
|
|
|
|
} else if (result == ISC_R_SUCCESS)
|
2007-03-29 06:36:31 +00:00
|
|
|
ns_client_log(client, NS_LOGCATEGORY_UNMATCHED,
|
2001-06-28 02:39:46 +00:00
|
|
|
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
|
2001-05-28 05:17:05 +00:00
|
|
|
"%s\n%.*s", reason,
|
|
|
|
(int)isc_buffer_usedlength(&buffer),
|
|
|
|
buf);
|
2001-05-25 07:39:48 +00:00
|
|
|
} while (result == ISC_R_NOSPACE);
|
|
|
|
|
|
|
|
if (buf != NULL)
|
|
|
|
isc_mem_put(client->mctx, buf, len);
|
|
|
|
}
|
2002-09-10 04:45:54 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager) {
|
|
|
|
ns_client_t *client;
|
|
|
|
char namebuf[DNS_NAME_FORMATSIZE];
|
2010-09-13 03:37:43 +00:00
|
|
|
char original[DNS_NAME_FORMATSIZE];
|
2002-09-10 04:45:54 +00:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
2010-09-13 03:37:43 +00:00
|
|
|
char typebuf[DNS_RDATATYPE_FORMATSIZE];
|
|
|
|
char classbuf[DNS_RDATACLASS_FORMATSIZE];
|
2002-09-10 04:45:54 +00:00
|
|
|
const char *name;
|
|
|
|
const char *sep;
|
2010-09-13 03:37:43 +00:00
|
|
|
const char *origfor;
|
|
|
|
dns_rdataset_t *rdataset;
|
2002-09-10 04:45:54 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
2007-03-29 06:36:31 +00:00
|
|
|
|
2011-10-10 22:57:14 +00:00
|
|
|
LOCK(&manager->reclock);
|
2002-09-10 04:45:54 +00:00
|
|
|
client = ISC_LIST_HEAD(manager->recursing);
|
|
|
|
while (client != NULL) {
|
2011-11-09 22:05:09 +00:00
|
|
|
INSIST(client->state == NS_CLIENTSTATE_RECURSING);
|
|
|
|
|
2002-09-10 04:45:54 +00:00
|
|
|
ns_client_name(client, peerbuf, sizeof(peerbuf));
|
|
|
|
if (client->view != NULL &&
|
|
|
|
strcmp(client->view->name, "_bind") != 0 &&
|
|
|
|
strcmp(client->view->name, "_default") != 0) {
|
|
|
|
name = client->view->name;
|
|
|
|
sep = ": view ";
|
|
|
|
} else {
|
|
|
|
name = "";
|
|
|
|
sep = "";
|
|
|
|
}
|
2011-10-10 22:57:14 +00:00
|
|
|
|
|
|
|
LOCK(&client->query.fetchlock);
|
2011-11-09 22:05:09 +00:00
|
|
|
INSIST(client->query.qname != NULL);
|
2002-09-10 04:45:54 +00:00
|
|
|
dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
|
2010-09-13 03:37:43 +00:00
|
|
|
if (client->query.qname != client->query.origqname &&
|
|
|
|
client->query.origqname != NULL) {
|
|
|
|
origfor = " for ";
|
|
|
|
dns_name_format(client->query.origqname, original,
|
|
|
|
sizeof(original));
|
|
|
|
} else {
|
|
|
|
origfor = "";
|
|
|
|
original[0] = '\0';
|
|
|
|
}
|
|
|
|
rdataset = ISC_LIST_HEAD(client->query.qname->list);
|
|
|
|
if (rdataset == NULL && client->query.origqname != NULL)
|
|
|
|
rdataset = ISC_LIST_HEAD(client->query.origqname->list);
|
|
|
|
if (rdataset != NULL) {
|
|
|
|
dns_rdatatype_format(rdataset->type, typebuf,
|
|
|
|
sizeof(typebuf));
|
|
|
|
dns_rdataclass_format(rdataset->rdclass, classbuf,
|
|
|
|
sizeof(classbuf));
|
|
|
|
} else {
|
|
|
|
strcpy(typebuf, "-");
|
|
|
|
strcpy(classbuf, "-");
|
|
|
|
}
|
2011-10-10 22:57:14 +00:00
|
|
|
UNLOCK(&client->query.fetchlock);
|
2010-09-13 03:37:43 +00:00
|
|
|
fprintf(f, "; client %s%s%s: id %u '%s/%s/%s'%s%s "
|
|
|
|
"requesttime %d\n", peerbuf, sep, name,
|
|
|
|
client->message->id, namebuf, typebuf, classbuf,
|
2015-10-02 12:32:42 -07:00
|
|
|
origfor, original,
|
|
|
|
isc_time_seconds(&client->requesttime));
|
2011-11-09 22:05:09 +00:00
|
|
|
client = ISC_LIST_NEXT(client, rlink);
|
2002-09-10 04:45:54 +00:00
|
|
|
}
|
2011-10-10 22:57:14 +00:00
|
|
|
UNLOCK(&manager->reclock);
|
2002-09-10 04:45:54 +00:00
|
|
|
}
|
2006-06-04 23:59:33 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ns_client_qnamereplace(ns_client_t *client, dns_name_t *name) {
|
2011-10-10 22:57:14 +00:00
|
|
|
LOCK(&client->query.fetchlock);
|
2006-06-04 23:59:33 +00:00
|
|
|
if (client->query.restarts > 0) {
|
|
|
|
/*
|
|
|
|
* client->query.qname was dynamically allocated.
|
|
|
|
*/
|
|
|
|
dns_message_puttempname(client->message,
|
|
|
|
&client->query.qname);
|
|
|
|
}
|
|
|
|
client->query.qname = name;
|
2015-04-23 16:57:15 +10:00
|
|
|
client->query.attributes &= ~NS_QUERYATTR_REDIRECT;
|
2011-10-10 22:57:14 +00:00
|
|
|
UNLOCK(&client->query.fetchlock);
|
2006-06-04 23:59:33 +00:00
|
|
|
}
|
2011-10-11 00:09:03 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_client_sourceip(dns_clientinfo_t *ci, isc_sockaddr_t **addrp) {
|
|
|
|
ns_client_t *client = (ns_client_t *) ci->data;
|
2011-10-11 23:46:45 +00:00
|
|
|
|
2011-10-11 00:09:03 +00:00
|
|
|
REQUIRE(NS_CLIENT_VALID(client));
|
|
|
|
REQUIRE(addrp != NULL);
|
|
|
|
|
|
|
|
*addrp = &client->peeraddr;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|