client.c revision 83c0ef88159fcae73fdbf8fe04d220c1878cce3e
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2004-2011 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1999-2003 Internet Software Consortium.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer *
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Permission to use, copy, modify, and/or distribute this software for any
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington/* $Id: client.c,v 1.283 2011/11/07 23:03:09 each Exp $ */
47058d17266420179fa294de6b82d8fb5b918df4Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <config.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <isc/formatcheck.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <isc/mutex.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/once.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/platform.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/print.h>
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington#include <isc/queue.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/stats.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/stdio.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/string.h>
1893b56ef9f5f2bc2a0fbe80d3c6b69df1bdc7c2Michael Sawyer#include <isc/task.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <isc/timer.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <isc/util.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/db.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <dns/dispatch.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/events.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/message.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/peer.h>
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson#include <dns/rcode.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdata.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <dns/rdataclass.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdatalist.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dns/rdataset.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dns/resolver.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dns/stats.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dns/tsig.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dns/view.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dns/zone.h>
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <named/interfacemgr.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <named/log.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <named/notify.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <named/os.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <named/server.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <named/update.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/***
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer *** Client
1893b56ef9f5f2bc2a0fbe80d3c6b69df1bdc7c2Michael Sawyer ***/
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer/*! \file
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Client Routines
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington *
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington * Important note!
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington *
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * All client state changes, other than that from idle to listening, occur
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * as a result of events. This guarantees serialization and avoids the
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * need for locking.
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson *
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington * If a routine is ever created that allows someone other than the client's
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington * task to change the client, then the client will have to be locked.
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington */
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define NS_CLIENT_TRACE
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington#ifdef NS_CLIENT_TRACE
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson#define CTRACE(m) ns_client_log(client, \
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyer NS_LOGCATEGORY_CLIENT, \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer NS_LOGMODULE_CLIENT, \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LOG_DEBUG(3), \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "%s", (m))
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define MTRACE(m) isc_log_write(ns_g_lctx, \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer NS_LOGCATEGORY_GENERAL, \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer NS_LOGMODULE_CLIENT, \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LOG_DEBUG(3), \
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "clientmgr @%p: %s", manager, (m))
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#else
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define CTRACE(m) ((void)(m))
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define MTRACE(m) ((void)(m))
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#endif
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define TCP_CLIENT(c) (((c)->attributes & NS_CLIENTATTR_TCP) != 0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define TCP_BUFFER_SIZE (65535 + 2)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define SEND_BUFFER_SIZE 4096
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define RECV_BUFFER_SIZE 4096
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyer#ifdef ISC_PLATFORM_USETHREADS
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#define NMCTXS 100
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer/*%<
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Number of 'mctx pools' for clients. (Should this be configurable?)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * When enabling threads, we use a pool of memory contexts shared by
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * client objects, since concurrent access to a shared context would cause
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * heavy contentions. The above constant is expected to be enough for
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * completely avoiding contentions among threads for an authoritative-only
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * server.
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#else
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#define NMCTXS 0
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer/*%<
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * If named with built without thread, simply share manager's context. Using
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * a separate context in this case would simply waste memory.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#endif
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer/*% nameserver client manager structure */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstruct ns_clientmgr {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /* Unlocked. */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer unsigned int magic;
e32394a2ac3466a2235f79ee32c247a11be42a8dAndreas Gustafsson
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /* The queue object has its own locks */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client_queue_t inactive; /*%< To be recycled */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mem_t * mctx;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington isc_taskmgr_t * taskmgr;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington isc_timermgr_t * timermgr;
36c754f274dd439e5a7ebf2401427c1d51b21302Brian Wellington
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington /* Lock covers manager state. */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mutex_t lock;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_boolean_t exiting;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington /* Lock covers the clients list */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mutex_t listlock;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client_list_t clients; /*%< All active clients */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /* Lock covers the recursing list */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mutex_t reclock;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client_list_t recursing; /*%< Recursing clients */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#if NMCTXS > 0
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /*%< mctx pool for clients. */
066cb385cd8f801dd3757db2922c9b1f343e65edAndreas Gustafsson unsigned int nextmctx;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mem_t * mctxpool[NMCTXS];
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington#endif
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington};
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#define MANAGER_MAGIC ISC_MAGIC('N', 'S', 'C', 'm')
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, MANAGER_MAGIC)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington/*!
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * Client object states. Ordering is significant: higher-numbered
cc48bb397fa6ba889f25157840492e68114dec8fBrian Wellington * states are generally "more active", meaning that the client can
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * have more dynamically allocated data, outstanding events, etc.
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * In the list below, any such properties listed for state N
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * also apply to any state > N.
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington *
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * To force the client into a less active state, set client->newstate
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * to that state and call exit_check(). This will cause any
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * activities defined for higher-numbered states to be aborted.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cc48bb397fa6ba889f25157840492e68114dec8fBrian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_FREED 0
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * The client object no longer exists.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_INACTIVE 1
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * The client object exists and has a task and timer.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Its "query" struct and sendbuf are initialized.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * It is on the client manager's list of inactive clients.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * It has a message and OPT, both in the reset state.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_READY 2
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * The client object is either a TCP or a UDP one, and
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * it is associated with a network interface. It is on the
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * client manager's list of active clients.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington *
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * If it is a TCP client object, it has a TCP listener socket
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * and an outstanding TCP listen request.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington *
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * If it is a UDP client object, it has a UDP listener socket
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * and an outstanding UDP receive request.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_READING 3
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * The client object is a TCP client object that has received
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * a connection. It has a tcpsocket, tcpmsg, TCP quota, and an
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * outstanding TCP read request. This state is not used for
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * UDP client objects.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_WORKING 4
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * The client object has received a request and is working
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * on it. It has a view, and it may have any of a non-reset OPT,
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * recursion quota, and an outstanding write request.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_RECURSING 5
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * The client object is recursing. It will be on the 'recursing'
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * list.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENTSTATE_MAX 9
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*%<
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Sentinel value used to indicate "no state". When client->newstate
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * has this value, we are not attempting to exit the current state.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Must be greater than any valid state.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Enable ns_client_dropport() by default.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#ifndef NS_CLIENT_DROPPORT
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#define NS_CLIENT_DROPPORT 1
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington#endif
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtonunsigned int ns_client_requests;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void client_read(ns_client_t *client);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void client_accept(ns_client_t *client);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic void client_udprecv(ns_client_t *client);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void clientmgr_destroy(ns_clientmgr_t *manager);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic isc_boolean_t exit_check(ns_client_t *client);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void ns_client_endrequest(ns_client_t *client);
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafssonstatic void client_start(isc_task_t *task, isc_event_t *event);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void client_request(isc_task_t *task, isc_event_t *event);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafssonstatic void ns_client_dumpmessage(ns_client_t *client, const char *reason);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafssonstatic isc_result_t get_client(ns_clientmgr_t *manager, ns_interface_t *ifp,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dns_dispatch_t *disp, isc_boolean_t tcp);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyervoid
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerns_client_recursing(ns_client_t *client) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer REQUIRE(NS_CLIENT_VALID(client));
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer REQUIRE(client->state == NS_CLIENTSTATE_WORKING);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer LOCK(&client->manager->reclock);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_APPEND(client->manager->recursing, client, rlink);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer client->state = NS_CLIENTSTATE_RECURSING;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence UNLOCK(&client->manager->reclock);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyervoid
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerns_client_killoldestquery(ns_client_t *client) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ns_client_t *oldest;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(NS_CLIENT_VALID(client));
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson LOCK(&client->manager->reclock);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer oldest = ISC_LIST_HEAD(client->manager->recursing);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (oldest != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_LIST_UNLINK(client->manager->recursing, oldest, rlink);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson UNLOCK(&client->manager->reclock);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson ns_query_cancel(oldest);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington } else
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington UNLOCK(&client->manager->reclock);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafssonvoid
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafssonns_client_settimeout(ns_client_t *client, unsigned int seconds) {
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson isc_result_t result;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_interval_t interval;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_interval_set(&interval, seconds, 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson &interval, ISC_FALSE);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson client->timerset = ISC_TRUE;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (result != ISC_R_SUCCESS) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington ns_client_log(client, NS_LOGCATEGORY_CLIENT,
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence "setting timeout: %s",
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews isc_result_totext(result));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /* Continue anyway. */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*%
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Check for a deactivation or shutdown request and take appropriate
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * action. Returns ISC_TRUE if either is in progress; in this case
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * the caller must no longer use the client object as it may have been
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * freed.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic isc_boolean_t
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerexit_check(ns_client_t *client) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_boolean_t destroy_manager = ISC_FALSE;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer ns_clientmgr_t *manager = NULL;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer REQUIRE(NS_CLIENT_VALID(client));
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer manager = client->manager;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->state <= client->newstate)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (ISC_FALSE); /* Business as usual. */
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer INSIST(client->newstate < NS_CLIENTSTATE_RECURSING);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer /*
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer * We need to detach from the view early when shutting down
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * the server to break the following vicious circle:
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer *
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * - The resolver will not shut down until the view refcount is zero
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * - The view refcount does not go to zero until all clients detach
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * - The client does not detach from the view until references is zero
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * - references does not go to zero until the resolver has shut down
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer *
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * Keep the view attached until any outstanding updates complete.
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->nupdates == 0 &&
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dns_view_detach(&client->view);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson if (client->state == NS_CLIENTSTATE_WORKING ||
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client->state == NS_CLIENTSTATE_RECURSING)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer INSIST(client->newstate <= NS_CLIENTSTATE_READING);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer /*
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * Let the update processing complete.
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->nupdates > 0)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (ISC_TRUE);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer /*
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * We are trying to abort request processing.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->nsends > 0) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_socket_t *socket;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (TCP_CLIENT(client))
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer socket = client->tcpsocket;
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer else
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson socket = client->udpsocket;
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson isc_socket_cancel(socket, client->task,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson ISC_SOCKCANCEL_SEND);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer }
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson if (! (client->nsends == 0 && client->nrecvs == 0 &&
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson client->references == 0))
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer {
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson /*
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * Still waiting for I/O cancel completion.
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * or lingering references.
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (ISC_TRUE);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson /*
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * I/O cancel is complete. Burn down all state
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * related to the current request. Ensure that
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * the client is no longer on the recursing list.
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson *
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * We need to check whether the client is still linked,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * because it may already have been removed from the
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * recursing list by ns_client_killoldestquery()
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->state == NS_CLIENTSTATE_RECURSING) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington LOCK(&manager->reclock);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (ISC_LINK_LINKED(client, rlink))
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ISC_LIST_UNLINK(manager->recursing,
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews client, rlink);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer UNLOCK(&manager->reclock);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer ns_client_endrequest(client);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client->state = NS_CLIENTSTATE_READING;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer INSIST(client->recursionquota == NULL);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (NS_CLIENTSTATE_READING == client->newstate) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client_read(client);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client->newstate = NS_CLIENTSTATE_MAX;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (ISC_TRUE); /* We're done. */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (client->state == NS_CLIENTSTATE_READING) {
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson /*
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * We are trying to abort the current TCP connection,
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * if any.
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson */
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson INSIST(client->recursionquota == NULL);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson INSIST(client->newstate <= NS_CLIENTSTATE_READY);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (client->nreads > 0)
f9521c63ea42fee93f7ae67512e4330ab13081c8Brian Wellington dns_tcpmsg_cancelread(&client->tcpmsg);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (! client->nreads == 0) {
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson /* Still waiting for read cancel completion. */
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson return (ISC_TRUE);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson }
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (client->tcpmsg_valid) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dns_tcpmsg_invalidate(&client->tcpmsg);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson client->tcpmsg_valid = ISC_FALSE;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson if (client->tcpsocket != NULL) {
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson CTRACE("closetcp");
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson isc_socket_detach(&client->tcpsocket);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer }
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (client->tcpquota != NULL)
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer isc_quota_detach(&client->tcpquota);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson if (client->timerset) {
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson (void)isc_timer_reset(client->timer,
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson isc_timertype_inactive,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer NULL, NULL, ISC_TRUE);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client->timerset = ISC_FALSE;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client->peeraddr_valid = ISC_FALSE;
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client->state = NS_CLIENTSTATE_READY;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence INSIST(client->recursionquota == NULL);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson /*
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * Now the client is ready to accept a new TCP connection
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * or UDP request, but we may have enough clients doing
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * that already. Check whether this client needs to remain
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * active and force it to go inactive if not.
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson *
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * UDP clients go inactive at this point, but TCP clients
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * may remain active if we have fewer active TCP client
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * objects than desired due to an earlier quota exhaustion.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
7804502532d6e2eee80083431a22eb5d957f52a0Michael Sawyer if (client->mortal && TCP_CLIENT(client) && !ns_g_clienttest) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer LOCK(&client->interface->lock);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (client->interface->ntcpcurrent <
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer client->interface->ntcptarget)
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer client->mortal = ISC_FALSE;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer UNLOCK(&client->interface->lock);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer /*
7804502532d6e2eee80083431a22eb5d957f52a0Michael Sawyer * We don't need the client; send it to the inactive
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * queue for recycling.
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->mortal) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->newstate > NS_CLIENTSTATE_INACTIVE)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer client->newstate = NS_CLIENTSTATE_INACTIVE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (NS_CLIENTSTATE_READY == client->newstate) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (TCP_CLIENT(client)) {
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer client_accept(client);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer client_udprecv(client);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer client->newstate = NS_CLIENTSTATE_MAX;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (ISC_TRUE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington if (client->state == NS_CLIENTSTATE_READY) {
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
53e7d12ab53e74650c00ea4c0d6568bf675d8b17Michael Sawyer
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington /*
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington * We are trying to enter the inactive state.
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (client->naccepts > 0)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_socket_cancel(client->tcplistener, client->task,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_SOCKCANCEL_ACCEPT);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer /* Still waiting for accept cancel completion. */
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer if (! (client->naccepts == 0))
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson return (ISC_TRUE);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson /* Accept cancel is complete. */
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson if (client->nrecvs > 0)
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson isc_socket_cancel(client->udpsocket, client->task,
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson ISC_SOCKCANCEL_RECV);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson /* Still waiting for recv cancel completion. */
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson if (! (client->nrecvs == 0))
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson return (ISC_TRUE);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson /* Still waiting for control event to be delivered */
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson if (client->nctls > 0)
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson return (ISC_TRUE);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson /* Deactivate the client. */
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson if (client->interface)
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson ns_interface_detach(&client->interface);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson INSIST(client->naccepts == 0);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer INSIST(client->recursionquota == NULL);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (client->tcplistener != NULL)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_socket_detach(&client->tcplistener);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (client->udpsocket != NULL)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_socket_detach(&client->udpsocket);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (client->dispatch != NULL)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer dns_dispatch_detach(&client->dispatch);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer client->attributes = 0;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer client->mortal = ISC_FALSE;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer /*
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * Put the client on the inactive list. If we are aiming for
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * the "freed" state, it will be removed from the inactive
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * list shortly, and we need to keep the manager locked until
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * that has been done, lest the manager decide to reactivate
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * the dying client inbetween.
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer */
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (!ns_g_clienttest)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer ISC_QUEUE_PUSH(manager->inactive, client, ilink);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer client->state = NS_CLIENTSTATE_INACTIVE;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer INSIST(client->recursionquota == NULL);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (client->state == client->newstate) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer client->newstate = NS_CLIENTSTATE_MAX;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (client->needshutdown)
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer isc_task_shutdown(client->task);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return (ISC_TRUE);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer }
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer }
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (client->state == NS_CLIENTSTATE_INACTIVE) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer INSIST(client->newstate == NS_CLIENTSTATE_FREED);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer /*
d7a2e0511222811b96f2e8757272ee383ec7b5e6Michael Sawyer * We are trying to free the client.
d7a2e0511222811b96f2e8757272ee383ec7b5e6Michael Sawyer *
d7a2e0511222811b96f2e8757272ee383ec7b5e6Michael Sawyer * When "shuttingdown" is true, either the task has received
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson * its shutdown event or no shutdown event has ever been
d7a2e0511222811b96f2e8757272ee383ec7b5e6Michael Sawyer * set up. Thus, we have no outstanding shutdown
d7a2e0511222811b96f2e8757272ee383ec7b5e6Michael Sawyer * event at this point.
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson INSIST(client->recursionquota == NULL);
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ns_query_free(client);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson isc_event_free((isc_event_t **)&client->sendevent);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_event_free((isc_event_t **)&client->recvevent);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer isc_timer_detach(&client->timer);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->tcpbuf != NULL)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_mem_put(client->mctx, client->tcpbuf,
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson TCP_BUFFER_SIZE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->opt != NULL) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer INSIST(dns_rdataset_isassociated(client->opt));
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson dns_rdataset_disassociate(client->opt);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_message_puttemprdataset(client->message,
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer &client->opt);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer dns_message_destroy(&client->message);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson if (manager != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer LOCK(&manager->listlock);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer ISC_LIST_UNLINK(manager->clients, client, link);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson LOCK(&manager->lock);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (manager->exiting &&
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson ISC_LIST_EMPTY(manager->clients))
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson destroy_manager = ISC_TRUE;
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer UNLOCK(&manager->lock);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer UNLOCK(&manager->listlock);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson }
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer /*
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Detaching the task must be done after unlinking from
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * the manager's lists because the manager accesses
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * client->task.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->task != NULL)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_task_detach(&client->task);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer CTRACE("free");
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer client->magic = 0;
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer /*
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer * Check that there are no other external references to
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * the memory context.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (ns_g_clienttest && isc_mem_references(client->mctx) != 1) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mem_stats(client->mctx, stderr);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(0);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson }
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer if (destroy_manager && manager != NULL)
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer clientmgr_destroy(manager);
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (ISC_TRUE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer/*%
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer * The client's task has received the client's control event
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer * as part of the startup process.
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer */
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyerstatic void
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrewsclient_start(isc_task_t *task, isc_event_t *event) {
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews ns_client_t *client = (ns_client_t *) event->ev_arg;
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews INSIST(task == client->task);
f8fec75ee8f429821137aee090f56ab678404a56Michael Sawyer
6c6a62933dda281cb9193de1d54d4c9e74515f5aMichael Sawyer UNUSED(task);
48674819ebf9176b5d5582ae851e485c324c1159Michael Sawyer
1416cae3f614faf4067e8b5a6b4732e132958bbaAndreas Gustafsson INSIST(client->nctls == 1);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer client->nctls--;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (exit_check(client))
869f65bea49f8d65ab9bb14c4551215529bd7419David Lawrence return;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (TCP_CLIENT(client)) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer client_accept(client);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer } else {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer client_udprecv(client);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer }
5d20773abcdf9ff3afe14a349413174f94594188Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer/*%
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * The client's task has received a shutdown event.
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer */
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyerstatic void
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafssonclient_shutdown(isc_task_t *task, isc_event_t *event) {
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer ns_client_t *client;
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer REQUIRE(event != NULL);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer client = event->ev_arg;
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer REQUIRE(NS_CLIENT_VALID(client));
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson REQUIRE(task == client->task);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer UNUSED(task);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
5d20773abcdf9ff3afe14a349413174f94594188Michael Sawyer CTRACE("shutdown");
5d20773abcdf9ff3afe14a349413174f94594188Michael Sawyer
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer isc_event_free(&event);
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer if (client->shutdown != NULL) {
5d20773abcdf9ff3afe14a349413174f94594188Michael Sawyer (client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer client->shutdown = NULL;
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer client->shutdown_arg = NULL;
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer }
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer client->newstate = NS_CLIENTSTATE_FREED;
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington client->needshutdown = ISC_FALSE;
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington (void)exit_check(client);
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerns_client_endrequest(ns_client_t *client) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer INSIST(client->naccepts == 0);
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington INSIST(client->nreads == 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(client->nsends == 0);
9fe3676b8490319aa65182f2072cbf5086097979Michael Sawyer INSIST(client->nrecvs == 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(client->nupdates == 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(client->state == NS_CLIENTSTATE_WORKING ||
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->state == NS_CLIENTSTATE_RECURSING);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer CTRACE("endrequest");
17747cd5ee98901b435cd2b3019c52937955a981Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (client->next != NULL) {
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer (client->next)(client);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer client->next = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer if (client->view != NULL)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dns_view_detach(&client->view);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (client->opt != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(dns_rdataset_isassociated(client->opt));
cd720113a2fc8a781d4e33350b8a2b62857b31d8David Lawrence dns_rdataset_disassociate(client->opt);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_message_puttemprdataset(client->message, &client->opt);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->signer = NULL;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->udpsize = 512;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->extflags = 0;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->ednsversion = -1;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->recursionquota != NULL)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_quota_detach(&client->recursionquota);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence /*
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Clear all client attributes that are specific to
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * the request; that's all except the TCP flag.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->attributes &= NS_CLIENTATTR_TCP;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyervoid
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafssonns_client_next(ns_client_t *client, isc_result_t result) {
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer int newstate;
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(NS_CLIENT_VALID(client));
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson REQUIRE(client->state == NS_CLIENTSTATE_WORKING ||
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->state == NS_CLIENTSTATE_RECURSING ||
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson client->state == NS_CLIENTSTATE_READING);
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson
6fda1577669dca9e0d8e4832e407bac34cc12de6Mark Andrews CTRACE("next");
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (result != ISC_R_SUCCESS)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
cd720113a2fc8a781d4e33350b8a2b62857b31d8David Lawrence NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews "request failed: %s", isc_result_totext(result));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews /*
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * An error processing a TCP request may have left
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews * the connection out of sync. To be safe, we always
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews * sever the connection when result != ISC_R_SUCCESS.
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
427f933eceba2dd1022fa3b680ca1bd08cbce990Brian Wellington newstate = NS_CLIENTSTATE_READING;
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews else
427f933eceba2dd1022fa3b680ca1bd08cbce990Brian Wellington newstate = NS_CLIENTSTATE_READY;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->newstate > newstate)
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews client->newstate = newstate;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer (void)exit_check(client);
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews}
e412ae947df6de858883564b8676a9650df70d9aMark Andrews
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington
07a926724c0a91d85b85a94441938d0094e88cffMark Andrewsstatic void
e412ae947df6de858883564b8676a9650df70d9aMark Andrewsclient_senddone(isc_task_t *task, isc_event_t *event) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ns_client_t *client;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_socketevent_t *sevent = (isc_socketevent_t *) event;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer REQUIRE(sevent != NULL);
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client = sevent->ev_arg;
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer REQUIRE(NS_CLIENT_VALID(client));
6e3a8e17fdf108f47bbba17474fd3d120d356b2fBrian Wellington REQUIRE(task == client->task);
6e3a8e17fdf108f47bbba17474fd3d120d356b2fBrian Wellington REQUIRE(sevent == client->sendevent);
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington UNUSED(task);
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington CTRACE("senddone");
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews if (sevent->result != ISC_R_SUCCESS)
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington ns_client_log(client, NS_LOGCATEGORY_CLIENT,
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington "error sending response: %s",
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington isc_result_totext(sevent->result));
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington INSIST(client->nsends > 0);
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews client->nsends--;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->tcpbuf != NULL) {
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews INSIST(TCP_CLIENT(client));
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->tcpbuf = NULL;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ns_client_next(client, ISC_R_SUCCESS);
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
66921d6dd259c4486a07aba19eee2b8349698575Brian Wellington/*%
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * We only want to fail with ISC_R_NOSPACE when called from
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * ns_client_sendraw() and not when called from ns_client_send(),
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * tcpbuffer is NULL when called from ns_client_sendraw() and
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * length != 0. tcpbuffer != NULL when called from ns_client_send()
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * and length == 0.
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer */
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic isc_result_t
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyerclient_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer isc_buffer_t *tcpbuffer, isc_uint32_t length,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer unsigned char *sendbuf, unsigned char **datap)
869f65bea49f8d65ab9bb14c4551215529bd7419David Lawrence{
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer unsigned char *data;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_uint32_t bufsize;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_result_t result;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(datap != NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST((tcpbuffer == NULL && length != 0) ||
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer (tcpbuffer != NULL && length == 0));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (TCP_CLIENT(client)) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(client->tcpbuf == NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (length + 2 > TCP_BUFFER_SIZE) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer result = ISC_R_NOSPACE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer goto done;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->tcpbuf == NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer result = ISC_R_NOMEMORY;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer goto done;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer data = client->tcpbuf;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (tcpbuffer != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_init(tcpbuffer, data, TCP_BUFFER_SIZE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_init(buffer, data + 2, TCP_BUFFER_SIZE - 2);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_init(buffer, data, TCP_BUFFER_SIZE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(length <= 0xffff);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_putuint16(buffer, (isc_uint16_t)length);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer data = sendbuf;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (client->udpsize < SEND_BUFFER_SIZE)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer bufsize = client->udpsize;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer else
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer bufsize = SEND_BUFFER_SIZE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (length > bufsize) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer result = ISC_R_NOSPACE;
e715e011788a529446b8013239c33599542ece32Michael Sawyer goto done;
e715e011788a529446b8013239c33599542ece32Michael Sawyer }
e715e011788a529446b8013239c33599542ece32Michael Sawyer isc_buffer_init(buffer, data, bufsize);
e715e011788a529446b8013239c33599542ece32Michael Sawyer }
e715e011788a529446b8013239c33599542ece32Michael Sawyer *datap = data;
e715e011788a529446b8013239c33599542ece32Michael Sawyer result = ISC_R_SUCCESS;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer done:
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (result);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic isc_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerclient_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer struct in6_pktinfo *pktinfo;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_result_t result;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_region_t r;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_sockaddr_t *address;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_socket_t *socket;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_netaddr_t netaddr;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington int match;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington unsigned int sockflags = ISC_SOCKFLAG_IMMEDIATE;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (TCP_CLIENT(client)) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington socket = client->tcpsocket;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer address = NULL;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer } else {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer socket = client->udpsocket;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer address = &client->peeraddr;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (ns_g_server->blackholeacl != NULL &&
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer dns_acl_match(&netaddr, NULL,
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington ns_g_server->blackholeacl,
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington &ns_g_server->aclenv,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer &match, NULL) == ISC_R_SUCCESS &&
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer match > 0)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (DNS_R_BLACKHOLED);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer sockflags |= ISC_SOCKFLAG_NORETRY;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0 &&
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer (client->attributes & NS_CLIENTATTR_MULTICAST) == 0)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington pktinfo = &client->pktinfo;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington else
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington pktinfo = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer isc_buffer_usedregion(buffer, &r);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer CTRACE("sendto");
f8fec75ee8f429821137aee090f56ab678404a56Michael Sawyer
6c6a62933dda281cb9193de1d54d4c9e74515f5aMichael Sawyer result = isc_socket_sendto2(socket, &r, client->task,
88301f84d1391e96ec87a9a308aa18f45553a56bAndreas Gustafsson address, pktinfo,
6c6a62933dda281cb9193de1d54d4c9e74515f5aMichael Sawyer client->sendevent, sockflags);
ad7bb5bff3e796f5648835bf87a203d23d8319e4David Lawrence if (result == ISC_R_SUCCESS || result == ISC_R_INPROGRESS) {
88301f84d1391e96ec87a9a308aa18f45553a56bAndreas Gustafsson client->nsends++;
f8fec75ee8f429821137aee090f56ab678404a56Michael Sawyer if (result == ISC_R_SUCCESS)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer client_senddone(client->task,
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson (isc_event_t *)client->sendevent);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson result = ISC_R_SUCCESS;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington }
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington return (result);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington}
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtonvoid
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtonns_client_sendraw(ns_client_t *client, dns_message_t *message) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_result_t result;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer unsigned char *data;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_buffer_t buffer;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_region_t r;
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson isc_region_t *mr;
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson unsigned char sendbuf[SEND_BUFFER_SIZE];
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington REQUIRE(NS_CLIENT_VALID(client));
bc223aaf8e952f3c88ac7ff82d726b328b990880Michael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer CTRACE("sendraw");
db8b100cae62de849ecf4ba9ad3be811fb375b53Michael Sawyer
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence mr = dns_message_getrawmessage(message);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (mr == NULL) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer result = ISC_R_UNEXPECTEDEND;
goto done;
}
result = client_allocsendbuf(client, &buffer, NULL, mr->length,
sendbuf, &data);
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;
done:
if (client->tcpbuf != NULL) {
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
client->tcpbuf = NULL;
}
ns_client_next(client, result);
}
void
ns_client_send(ns_client_t *client) {
isc_result_t result;
unsigned char *data;
isc_buffer_t buffer;
isc_buffer_t tcpbuffer;
isc_region_t r;
dns_compress_t cctx;
isc_boolean_t cleanup_cctx = ISC_FALSE;
unsigned char sendbuf[SEND_BUFFER_SIZE];
unsigned int render_opts;
unsigned int preferred_glue;
isc_boolean_t opt_included = ISC_FALSE;
REQUIRE(NS_CLIENT_VALID(client));
CTRACE("send");
if ((client->attributes & NS_CLIENTATTR_RA) != 0)
client->message->flags |= DNS_MESSAGEFLAG_RA;
if ((client->attributes & NS_CLIENTATTR_WANTDNSSEC) != 0)
render_opts = 0;
else
render_opts = DNS_MESSAGERENDER_OMITDNSSEC;
#ifdef ALLOW_FILTER_AAAA_ON_V4
/*
* filter-aaaa-on-v4 yes or break-dnssec option to suppress
* AAAA records
* We already know that request came via IPv4,
* that we have both AAAA and A records,
* and that we either have no signatures that the client wants
* or we are supposed to break DNSSEC.
*/
if ((client->attributes & NS_CLIENTATTR_FILTER_AAAA) != 0)
render_opts |= DNS_MESSAGERENDER_FILTER_AAAA;
#endif
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;
}
/*
* XXXRTH The following doesn't deal with TCP buffer resizing.
*/
result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0,
sendbuf, &data);
if (result != ISC_R_SUCCESS)
goto done;
result = dns_compress_init(&cctx, -1, client->mctx);
if (result != ISC_R_SUCCESS)
goto done;
cleanup_cctx = ISC_TRUE;
result = dns_message_renderbegin(client->message, &cctx, &buffer);
if (result != ISC_R_SUCCESS)
goto done;
if (client->opt != NULL) {
result = dns_message_setopt(client->message, client->opt);
opt_included = ISC_TRUE;
client->opt = NULL;
if (result != ISC_R_SUCCESS)
goto done;
}
result = dns_message_rendersection(client->message,
DNS_SECTION_QUESTION, 0);
if (result == ISC_R_NOSPACE) {
client->message->flags |= DNS_MESSAGEFLAG_TC;
goto renderend;
}
if (result != ISC_R_SUCCESS)
goto done;
result = dns_message_rendersection(client->message,
DNS_SECTION_ANSWER,
DNS_MESSAGERENDER_PARTIAL |
render_opts);
if (result == ISC_R_NOSPACE) {
client->message->flags |= DNS_MESSAGEFLAG_TC;
goto renderend;
}
if (result != ISC_R_SUCCESS)
goto done;
result = dns_message_rendersection(client->message,
DNS_SECTION_AUTHORITY,
DNS_MESSAGERENDER_PARTIAL |
render_opts);
if (result == ISC_R_NOSPACE) {
client->message->flags |= DNS_MESSAGEFLAG_TC;
goto renderend;
}
if (result != ISC_R_SUCCESS)
goto done;
result = dns_message_rendersection(client->message,
DNS_SECTION_ADDITIONAL,
preferred_glue | render_opts);
if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
goto done;
renderend:
result = dns_message_renderend(client->message);
if (result != ISC_R_SUCCESS)
goto done;
if (cleanup_cctx) {
dns_compress_invalidate(&cctx);
cleanup_cctx = ISC_FALSE;
}
if (TCP_CLIENT(client)) {
isc_buffer_usedregion(&buffer, &r);
isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
isc_buffer_add(&tcpbuffer, r.length);
result = client_sendpkg(client, &tcpbuffer);
} else
result = client_sendpkg(client, &buffer);
/* update statistics (XXXJT: is it okay to access message->xxxkey?) */
isc_stats_increment(ns_g_server->nsstats, dns_nsstatscounter_response);
if (opt_included) {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_edns0out);
}
if (client->message->tsigkey != NULL) {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_tsigout);
}
if (client->message->sig0key != NULL) {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_sig0out);
}
if ((client->message->flags & DNS_MESSAGEFLAG_TC) != 0)
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_truncatedresp);
if (result == ISC_R_SUCCESS)
return;
done:
if (client->tcpbuf != NULL) {
isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
client->tcpbuf = NULL;
}
if (cleanup_cctx)
dns_compress_invalidate(&cctx);
ns_client_next(client, result);
}
#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
void
ns_client_error(ns_client_t *client, isc_result_t result) {
dns_rcode_t rcode;
dns_message_t *message;
REQUIRE(NS_CLIENT_VALID(client));
CTRACE("error");
message = client->message;
rcode = dns_result_torcode(result);
#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
/*
* Message may be an in-progress reply that we had trouble
* with, in which case QR will be set. We need to clear QR before
* calling dns_message_reply() to avoid triggering an assertion.
*/
message->flags &= ~DNS_MESSAGEFLAG_QR;
/*
* AA and AD shouldn't be set.
*/
message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
result = dns_message_reply(message, ISC_TRUE);
if (result != ISC_R_SUCCESS) {
/*
* It could be that we've got a query with a good header,
* but a bad question section, so we try again with
* want_question_section set to ISC_FALSE.
*/
result = dns_message_reply(message, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
ns_client_next(client, result);
return;
}
}
message->rcode = rcode;
/*
* 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.
*/
if (rcode == dns_rcode_formerr) {
if (isc_sockaddr_equal(&client->peeraddr,
&client->formerrcache.addr) &&
message->id == client->formerrcache.id &&
client->requesttime - client->formerrcache.time < 2) {
/* 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;
client->formerrcache.time = client->requesttime;
client->formerrcache.id = message->id;
}
ns_client_send(client);
}
static inline isc_result_t
client_addopt(ns_client_t *client) {
dns_rdataset_t *rdataset;
dns_rdatalist_t *rdatalist;
dns_rdata_t *rdata;
isc_result_t result;
dns_view_t *view;
dns_resolver_t *resolver;
isc_uint16_t udpsize;
REQUIRE(client->opt == NULL); /* XXXRTH free old. */
rdatalist = NULL;
result = dns_message_gettemprdatalist(client->message, &rdatalist);
if (result != ISC_R_SUCCESS)
return (result);
rdata = NULL;
result = dns_message_gettemprdata(client->message, &rdata);
if (result != ISC_R_SUCCESS)
return (result);
rdataset = NULL;
result = dns_message_gettemprdataset(client->message, &rdataset);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdataset_init(rdataset);
rdatalist->type = dns_rdatatype_opt;
rdatalist->covers = 0;
/*
* Set the maximum UDP buffer size.
*/
view = client->view;
resolver = (view != NULL) ? view->resolver : NULL;
if (resolver != NULL)
udpsize = dns_resolver_getudpsize(resolver);
else
udpsize = ns_g_udpsize;
rdatalist->rdclass = udpsize;
/*
* Set EXTENDED-RCODE, VERSION and Z to 0.
*/
rdatalist->ttl = (client->extflags & DNS_MESSAGEEXTFLAG_REPLYPRESERVE);
/* Set EDNS options if applicable */
if (client->attributes & NS_CLIENTATTR_WANTNSID &&
(ns_g_server->server_id != NULL ||
ns_g_server->server_usehostname)) {
/*
* Space required for NSID data:
* 2 bytes for opt code
* + 2 bytes for NSID length
* + NSID itself
*/
char nsid[BUFSIZ], *nsidp;
isc_buffer_t *buffer = NULL;
if (ns_g_server->server_usehostname) {
isc_result_t result;
result = ns_os_gethostname(nsid, sizeof(nsid));
if (result != ISC_R_SUCCESS) {
goto no_nsid;
}
nsidp = nsid;
} else
nsidp = ns_g_server->server_id;
rdata->length = strlen(nsidp) + 4;
result = isc_buffer_allocate(client->mctx, &buffer,
rdata->length);
if (result != ISC_R_SUCCESS)
goto no_nsid;
isc_buffer_putuint16(buffer, DNS_OPT_NSID);
isc_buffer_putuint16(buffer, strlen(nsidp));
isc_buffer_putstr(buffer, nsidp);
rdata->data = buffer->base;
dns_message_takebuffer(client->message, &buffer);
} else {
no_nsid:
rdata->data = NULL;
rdata->length = 0;
}
rdata->rdclass = rdatalist->rdclass;
rdata->type = rdatalist->type;
rdata->flags = 0;
ISC_LIST_INIT(rdatalist->rdata);
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
== ISC_R_SUCCESS);
client->opt = rdataset;
return (ISC_R_SUCCESS);
}
static inline isc_boolean_t
allowed(isc_netaddr_t *addr, dns_name_t *signer, dns_acl_t *acl) {
int match;
isc_result_t result;
if (acl == NULL)
return (ISC_TRUE);
result = dns_acl_match(addr, signer, acl, &ns_g_server->aclenv,
&match, NULL);
if (result == ISC_R_SUCCESS && match > 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
/*
* Callback to see if a non-recursive query coming from 'srcaddr' to
* 'destaddr', with optional key 'mykey' for class 'rdclass' would be
* delivered to 'myview'.
*
* We run this unlocked as both the view list and the interface list
* are updated when the appropriate task has exclusivity.
*/
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;
dns_tsigkey_t *key = NULL;
dns_name_t *tsig = NULL;
isc_netaddr_t netsrc;
isc_netaddr_t netdst;
UNUSED(arg);
/*
* ns_g_server->interfacemgr is task exclusive locked.
*/
if (ns_g_server->interfacemgr == NULL)
return (ISC_TRUE);
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;
result = dns_view_gettsig(view, &mykey->name, &key);
if (result != ISC_R_SUCCESS)
continue;
match = dst_key_compare(mykey->key, key->key);
dns_tsigkey_detach(&key);
if (!match)
continue;
tsig = dns_tsigkey_identity(mykey);
}
if (allowed(&netsrc, tsig, view->matchclients) &&
allowed(&netdst, tsig, view->matchdestinations))
break;
}
return (ISC_TF(view == myview));
}
/*
* Handle an incoming request event from the socket (UDP case)
* or tcpmsg (TCP case).
*/
static void
client_request(isc_task_t *task, isc_event_t *event) {
ns_client_t *client;
isc_socketevent_t *sevent;
isc_result_t result;
isc_result_t sigresult = ISC_R_SUCCESS;
isc_buffer_t *buffer;
isc_buffer_t tbuffer;
dns_view_t *view;
dns_rdataset_t *opt;
dns_name_t *signame;
isc_boolean_t ra; /* Recursion available. */
isc_netaddr_t netaddr;
int match;
dns_messageid_t id;
unsigned int flags;
isc_boolean_t notimp;
dns_rdata_t rdata;
isc_uint16_t optcode;
REQUIRE(event != NULL);
client = event->ev_arg;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(task == client->task);
INSIST(client->recursionquota == NULL);
INSIST(client->state == TCP_CLIENT(client) ?
NS_CLIENTSTATE_READING :
NS_CLIENTSTATE_READY);
ns_client_requests++;
if (event->ev_type == ISC_SOCKEVENT_RECVDONE) {
INSIST(!TCP_CLIENT(client));
sevent = (isc_socketevent_t *)event;
REQUIRE(sevent == client->recvevent);
isc_buffer_init(&tbuffer, sevent->region.base, sevent->n);
isc_buffer_add(&tbuffer, sevent->n);
buffer = &tbuffer;
result = sevent->result;
if (result == ISC_R_SUCCESS) {
client->peeraddr = sevent->address;
client->peeraddr_valid = ISC_TRUE;
}
if ((sevent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
client->attributes |= NS_CLIENTATTR_PKTINFO;
client->pktinfo = sevent->pktinfo;
}
if ((sevent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
client->attributes |= NS_CLIENTATTR_MULTICAST;
client->nrecvs--;
} else {
INSIST(TCP_CLIENT(client));
REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
REQUIRE(event->ev_sender == &client->tcpmsg);
buffer = &client->tcpmsg.buffer;
result = client->tcpmsg.result;
INSIST(client->nreads == 1);
/*
* client->peeraddr was set when the connection was accepted.
*/
client->nreads--;
}
if (exit_check(client))
goto cleanup;
client->state = client->newstate = NS_CLIENTSTATE_WORKING;
isc_task_getcurrenttime(task, &client->requesttime);
client->now = client->requesttime;
if (result != ISC_R_SUCCESS) {
if (TCP_CLIENT(client)) {
ns_client_next(client, result);
} else {
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));
isc_task_shutdown(client->task);
}
goto cleanup;
}
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
#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
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"%s request",
TCP_CLIENT(client) ? "TCP" : "UDP");
/*
* 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),
"blackholed UDP datagram");
ns_client_next(client, ISC_R_SUCCESS);
goto cleanup;
}
}
/*
* Silently drop multicast requests for the present.
* XXXMPA revisit this as mDNS spec was published.
*/
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);
goto cleanup;
}
result = dns_message_peekheader(buffer, &id, &flags);
if (result != ISC_R_SUCCESS) {
/*
* There isn't enough header to determine whether
* this was a request or a response. Drop it.
*/
ns_client_next(client, result);
goto cleanup;
}
/*
* The client object handles requests, not responses.
* If this is a UDP response, forward it to the dispatcher.
* If it's a TCP response, discard it here.
*/
if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
if (TCP_CLIENT(client)) {
CTRACE("unexpected response");
ns_client_next(client, DNS_R_FORMERR);
goto cleanup;
} else {
dns_dispatch_importrecv(client->dispatch, event);
ns_client_next(client, ISC_R_SUCCESS);
goto cleanup;
}
}
/*
* Update some statistics counters. Don't count responses.
*/
if (isc_sockaddr_pf(&client->peeraddr) == PF_INET) {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_requestv4);
} else {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_requestv6);
}
if (TCP_CLIENT(client))
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_tcp);
/*
* 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).
*/
ns_client_error(client, result);
goto cleanup;
}
dns_opcodestats_increment(ns_g_server->opcodestats,
client->message->opcode);
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;
}
client->message->rcode = dns_rcode_noerror;
/* RFC1123 section 6.1.3.2 */
if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0)
client->message->flags &= ~DNS_MESSAGEFLAG_RD;
/*
* Deal with EDNS.
*/
opt = dns_message_getopt(client->message);
if (opt != NULL) {
/*
* 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;
if (client->ednsversion > 0) {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_badednsver);
result = client_addopt(client);
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);
if (rdata.length >= 2) {
isc_buffer_t nsidbuf;
isc_buffer_init(&nsidbuf,
rdata.data, rdata.length);
isc_buffer_add(&nsidbuf, rdata.length);
optcode = isc_buffer_getuint16(&nsidbuf);
if (optcode == DNS_OPT_NSID)
client->attributes |=
NS_CLIENTATTR_WANTNSID;
}
}
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_edns0in);
/*
* Create an OPT for our reply.
*/
result = client_addopt(client);
if (result != ISC_R_SUCCESS) {
ns_client_error(client, result);
goto cleanup;
}
}
if (client->message->rdclass == 0) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
"message class could not be determined");
ns_client_dumpmessage(client,
"message class could not be determined");
ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR);
goto cleanup;
}
/*
* Determine the destination address. If the receiving interface is
* bound to a specific address, we simply use it regardless of the
* address family. All IPv4 queries should fall into this case.
* Otherwise, if this is a TCP query, get the address from the
* receiving socket (this needs a system call and can be heavy).
* For IPv6 UDP queries, we get this from the pktinfo structure (if
* supported).
* If all the attempts fail (this can happen due to memory shortage,
* etc), we regard this as an error for safety.
*/
if ((client->interface->flags & NS_INTERFACEFLAG_ANYADDR) == 0)
isc_netaddr_fromsockaddr(&client->destaddr,
&client->interface->addr);
else {
isc_sockaddr_t sockaddr;
result = ISC_R_FAILURE;
if (TCP_CLIENT(client))
result = isc_socket_getsockname(client->tcpsocket,
&sockaddr);
if (result == ISC_R_SUCCESS)
isc_netaddr_fromsockaddr(&client->destaddr, &sockaddr);
if (result != ISC_R_SUCCESS &&
client->interface->addr.type.sa.sa_family == AF_INET6 &&
(client->attributes & NS_CLIENTATTR_PKTINFO) != 0) {
/*
* 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.
*/
isc_netaddr_fromin6(&client->destaddr,
&client->pktinfo.ipi6_addr);
if (IN6_IS_ADDR_LINKLOCAL(&client->pktinfo.ipi6_addr))
isc_netaddr_setzone(&client->destaddr,
client->pktinfo.ipi6_ifindex);
result = ISC_R_SUCCESS;
}
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"failed to get request's "
"destination: %s",
isc_result_totext(result));
ns_client_next(client, ISC_R_SUCCESS);
goto cleanup;
}
}
/*
* Find a view that matches the client's source address.
*/
for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link)) {
if (client->message->rdclass == view->rdclass ||
client->message->rdclass == dns_rdataclass_any)
{
dns_name_t *tsig = NULL;
sigresult = dns_message_rechecksig(client->message,
view);
if (sigresult == ISC_R_SUCCESS)
tsig = dns_tsigkey_identity(client->message->tsigkey);
if (allowed(&netaddr, tsig, view->matchclients) &&
allowed(&client->destaddr, tsig,
view->matchdestinations) &&
!((client->message->flags & DNS_MESSAGEFLAG_RD)
== 0 && view->matchrecursiveonly))
{
dns_view_attach(view, &client->view);
break;
}
}
}
if (view == NULL) {
char classname[DNS_RDATACLASS_FORMATSIZE];
/*
* Do a dummy TSIG verification attempt so that the
* response will have a TSIG if the query did, as
* required by RFC2845.
*/
isc_buffer_t b;
isc_region_t *r;
dns_message_resetsig(client->message);
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);
dns_rdataclass_format(client->message->rdclass, classname,
sizeof(classname));
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
"no matching view in class '%s'", classname);
ns_client_dumpmessage(client, "no matching view in class");
ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED);
goto cleanup;
}
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
"using view '%s'", view->name);
/*
* Check for a signature. We log bad signatures regardless of
* whether they ultimately cause the request to be rejected or
* not. We do not log the lack of a signature unless we are
* debugging.
*/
client->signer = NULL;
dns_name_init(&client->signername, NULL);
result = dns_message_signer(client->message, &client->signername);
if (result != ISC_R_NOTFOUND) {
signame = NULL;
if (dns_message_gettsig(client->message, &signame) != NULL) {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_tsigin);
} else {
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_sig0in);
}
}
if (result == ISC_R_SUCCESS) {
char namebuf[DNS_NAME_FORMATSIZE];
dns_name_format(&client->signername, namebuf, sizeof(namebuf));
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request has valid signature: %s", namebuf);
client->signer = &client->signername;
} else if (result == ISC_R_NOTFOUND) {
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request is not signed");
} else if (result == DNS_R_NOIDENTITY) {
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"request is signed by a nonauthoritative key");
} else {
char tsigrcode[64];
isc_buffer_t b;
dns_rcode_t status;
isc_result_t tresult;
/* There is a signature, but it is bad. */
isc_stats_increment(ns_g_server->nsstats,
dns_nsstatscounter_invalidsig);
signame = NULL;
if (dns_message_gettsig(client->message, &signame) != NULL) {
char namebuf[DNS_NAME_FORMATSIZE];
char cnamebuf[DNS_NAME_FORMATSIZE];
dns_name_format(signame, namebuf, sizeof(namebuf));
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';
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);
}
} else {
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';
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
"request has invalid signature: %s (%s)",
isc_result_totext(result), tsigrcode);
}
/*
* 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.
*/
if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
client->message->opcode == dns_opcode_update)) {
ns_client_error(client, sigresult);
goto cleanup;
}
}
/*
* Decide whether recursive service is available to this client.
* We do this here rather than in the query code so that we can
* set the RA bit correctly on all kinds of responses, not just
* responses to ordinary queries. Note if you can't query the
* cache there is no point in setting RA.
*/
ra = ISC_FALSE;
if (client->view->resolver != NULL &&
client->view->recursion == ISC_TRUE &&
ns_client_checkaclsilent(client, NULL,
client->view->recursionacl,
ISC_TRUE) == ISC_R_SUCCESS &&
ns_client_checkaclsilent(client, NULL,
client->view->cacheacl,
ISC_TRUE) == ISC_R_SUCCESS &&
ns_client_checkaclsilent(client, &client->destaddr,
client->view->recursiononacl,
ISC_TRUE) == ISC_R_SUCCESS &&
ns_client_checkaclsilent(client, &client->destaddr,
client->view->cacheonacl,
ISC_TRUE) == ISC_R_SUCCESS)
ra = ISC_TRUE;
if (ra == ISC_TRUE)
client->attributes |= NS_CLIENTATTR_RA;
ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT,
ISC_LOG_DEBUG(3), ra ? "recursion available" :
"recursion not available");
/*
* 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;
}
/*
* Dispatch the request.
*/
switch (client->message->opcode) {
case dns_opcode_query:
CTRACE("query");
ns_query_start(client);
break;
case dns_opcode_update:
CTRACE("update");
ns_client_settimeout(client, 60);
ns_update_start(client, sigresult);
break;
case dns_opcode_notify:
CTRACE("notify");
ns_client_settimeout(client, 60);
ns_notify_start(client);
break;
case dns_opcode_iquery:
CTRACE("iquery");
ns_client_error(client, DNS_R_NOTIMP);
break;
default:
CTRACE("unknown opcode");
ns_client_error(client, DNS_R_NOTIMP);
}
cleanup:
return;
}
static void
client_timeout(isc_task_t *task, isc_event_t *event) {
ns_client_t *client;
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->timer != NULL);
UNUSED(task);
CTRACE("timeout");
isc_event_free(&event);
if (client->shutdown != NULL) {
(client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
client->shutdown = NULL;
client->shutdown_arg = NULL;
}
if (client->newstate > NS_CLIENTSTATE_READY)
client->newstate = NS_CLIENTSTATE_READY;
(void)exit_check(client);
}
static isc_result_t
get_clientmctx(ns_clientmgr_t *manager, isc_mem_t **mctxp) {
isc_mem_t *clientmctx;
isc_result_t result;
#if NMCTXS > 0
unsigned int nextmctx;
#endif
MTRACE("clientmctx");
/*
* Caller must be holding the manager lock.
*/
if (ns_g_clienttest) {
result = isc_mem_create(0, 0, mctxp);
if (result == ISC_R_SUCCESS)
isc_mem_setname(*mctxp, "client", NULL);
return (result);
}
#if NMCTXS > 0
nextmctx = manager->nextmctx++;
if (manager->nextmctx == NMCTXS)
manager->nextmctx = 0;
INSIST(nextmctx < NMCTXS);
clientmctx = manager->mctxpool[nextmctx];
if (clientmctx == NULL) {
result = isc_mem_create(0, 0, &clientmctx);
if (result != ISC_R_SUCCESS)
return (result);
isc_mem_setname(clientmctx, "client", NULL);
manager->mctxpool[nextmctx] = clientmctx;
}
#else
clientmctx = manager->mctx;
#endif
isc_mem_attach(clientmctx, mctxp);
return (ISC_R_SUCCESS);
}
static isc_result_t
client_create(ns_clientmgr_t *manager, ns_client_t **clientp) {
ns_client_t *client;
isc_result_t result;
isc_mem_t *mctx = NULL;
/*
* Caller must be holding the manager lock.
*
* Note: creating a client does not add the client to the
* manager's client list or set the client's manager pointer.
* The caller is responsible for that.
*/
REQUIRE(clientp != NULL && *clientp == NULL);
result = get_clientmctx(manager, &mctx);
if (result != ISC_R_SUCCESS)
return (result);
client = isc_mem_get(mctx, sizeof(*client));
if (client == NULL) {
isc_mem_detach(&mctx);
return (ISC_R_NOMEMORY);
}
client->mctx = mctx;
client->task = NULL;
result = isc_task_create(manager->taskmgr, 0, &client->task);
if (result != ISC_R_SUCCESS)
goto cleanup_client;
isc_task_setname(client->task, "client", client);
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;
client->timerset = ISC_FALSE;
client->message = NULL;
result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE,
&client->message);
if (result != ISC_R_SUCCESS)
goto cleanup_timer;
/* XXXRTH Hardwired constants */
client->sendevent = (isc_socketevent_t *)
isc_event_allocate(client->mctx, client,
ISC_SOCKEVENT_SENDDONE,
client_senddone, client,
sizeof(isc_socketevent_t));
if (client->sendevent == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_message;
}
client->recvbuf = isc_mem_get(client->mctx, RECV_BUFFER_SIZE);
if (client->recvbuf == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_sendevent;
}
client->recvevent = (isc_socketevent_t *)
isc_event_allocate(client->mctx, client,
ISC_SOCKEVENT_RECVDONE,
client_request, client,
sizeof(isc_socketevent_t));
if (client->recvevent == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_recvbuf;
}
client->magic = NS_CLIENT_MAGIC;
client->manager = NULL;
client->state = NS_CLIENTSTATE_INACTIVE;
client->newstate = NS_CLIENTSTATE_MAX;
client->naccepts = 0;
client->nreads = 0;
client->nsends = 0;
client->nrecvs = 0;
client->nupdates = 0;
client->nctls = 0;
client->references = 0;
client->attributes = 0;
client->view = NULL;
client->dispatch = NULL;
client->udpsocket = NULL;
client->tcplistener = NULL;
client->tcpsocket = NULL;
client->tcpmsg_valid = ISC_FALSE;
client->tcpbuf = NULL;
client->opt = NULL;
client->udpsize = 512;
client->extflags = 0;
client->ednsversion = -1;
client->next = NULL;
client->shutdown = NULL;
client->shutdown_arg = NULL;
client->signer = NULL;
dns_name_init(&client->signername, NULL);
client->mortal = ISC_FALSE;
client->tcpquota = NULL;
client->recursionquota = NULL;
client->interface = NULL;
client->peeraddr_valid = ISC_FALSE;
ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
NS_EVENT_CLIENTCONTROL, client_start, client, client,
NULL, NULL);
/*
* 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;
ISC_LINK_INIT(client, link);
ISC_LINK_INIT(client, rlink);
ISC_QLINK_INIT(client, ilink);
/*
* 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);
if (result != ISC_R_SUCCESS)
goto cleanup_recvevent;
result = isc_task_onshutdown(client->task, client_shutdown, client);
if (result != ISC_R_SUCCESS)
goto cleanup_query;
client->needshutdown = ns_g_clienttest;
CTRACE("create");
*clientp = client;
return (ISC_R_SUCCESS);
cleanup_query:
ns_query_free(client);
cleanup_recvevent:
isc_event_free((isc_event_t **)&client->recvevent);
cleanup_recvbuf:
isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
cleanup_sendevent:
isc_event_free((isc_event_t **)&client->sendevent);
client->magic = 0;
cleanup_message:
dns_message_destroy(&client->message);
cleanup_timer:
isc_timer_detach(&client->timer);
cleanup_task:
isc_task_detach(&client->task);
cleanup_client:
isc_mem_putanddetach(&client->mctx, client, sizeof(*client));
return (result);
}
static void
client_read(ns_client_t *client) {
isc_result_t result;
CTRACE("read");
result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
client_request, client);
if (result != ISC_R_SUCCESS)
goto fail;
/*
* Set a timeout to limit the amount of time we will wait
* for a request on this TCP connection.
*/
ns_client_settimeout(client, 30);
client->state = client->newstate = NS_CLIENTSTATE_READING;
INSIST(client->nreads == 0);
INSIST(client->recursionquota == NULL);
client->nreads++;
return;
fail:
ns_client_next(client, result);
}
static void
client_newconn(isc_task_t *task, isc_event_t *event) {
ns_client_t *client = event->ev_arg;
isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
isc_result_t result;
REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(client->task == task);
UNUSED(task);
INSIST(client->state == NS_CLIENTSTATE_READY);
INSIST(client->naccepts == 1);
client->naccepts--;
LOCK(&client->interface->lock);
INSIST(client->interface->ntcpcurrent > 0);
client->interface->ntcpcurrent--;
UNLOCK(&client->interface->lock);
/*
* We must take ownership of the new socket before the exit
* check to make sure it gets destroyed if we decide to exit.
*/
if (nevent->result == ISC_R_SUCCESS) {
client->tcpsocket = nevent->newsocket;
isc_socket_setname(client->tcpsocket, "client-tcp", NULL);
client->state = NS_CLIENTSTATE_READING;
INSIST(client->recursionquota == NULL);
(void)isc_socket_getpeername(client->tcpsocket,
&client->peeraddr);
client->peeraddr_valid = ISC_TRUE;
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"new TCP connection");
} else {
/*
* XXXRTH What should we do? We're trying to accept but
* it didn't work. If we just give up, then TCP
* service may eventually stop.
*
* For now, we just go idle.
*
* Going idle is probably the right thing if the
* I/O was canceled.
*/
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
"accept failed: %s",
isc_result_totext(nevent->result));
}
if (exit_check(client))
goto freeevent;
if (nevent->result == ISC_R_SUCCESS) {
int match;
isc_netaddr_t netaddr;
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
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),
"blackholed connection attempt");
client->newstate = NS_CLIENTSTATE_READY;
(void)exit_check(client);
goto freeevent;
}
INSIST(client->tcpmsg_valid == ISC_FALSE);
dns_tcpmsg_init(client->mctx, client->tcpsocket,
&client->tcpmsg);
client->tcpmsg_valid = ISC_TRUE;
/*
* Let a new client take our place immediately, before
* we wait for a request packet. If we don't,
* telnetting to port 53 (once per CPU) will
* deny service to legitimate TCP clients.
*/
result = isc_quota_attach(&ns_g_server->tcpquota,
&client->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: %s",
isc_result_totext(result));
}
client_read(client);
}
freeevent:
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
* it didn't work. If we just give up, then TCP
* service may eventually stop.
*
* For now, we just go idle.
*/
return;
}
INSIST(client->naccepts == 0);
client->naccepts++;
LOCK(&client->interface->lock);
client->interface->ntcpcurrent++;
UNLOCK(&client->interface->lock);
}
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;
result = isc_socket_recv2(client->udpsocket, &r, 1,
client->task, client->recvevent, 0);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_socket_recv2() failed: %s",
isc_result_totext(result));
/*
* This cannot happen in the current implementation, since
* isc_socket_recv2() cannot fail if flags == 0.
*
* If this does fail, we just go idle.
*/
return;
}
INSIST(client->nrecvs == 0);
client->nrecvs++;
}
void
ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
REQUIRE(NS_CLIENT_VALID(source));
REQUIRE(targetp != NULL && *targetp == NULL);
source->references++;
ns_client_log(source, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
"ns_client_attach: ref = %d", source->references);
*targetp = source;
}
void
ns_client_detach(ns_client_t **clientp) {
ns_client_t *client = *clientp;
client->references--;
INSIST(client->references >= 0);
*clientp = NULL;
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
"ns_client_detach: ref = %d", client->references);
(void)exit_check(client);
}
isc_boolean_t
ns_client_shuttingdown(ns_client_t *client) {
return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
}
isc_result_t
ns_client_replace(ns_client_t *client) {
isc_result_t result;
CTRACE("replace");
result = get_client(client->manager, client->interface,
client->dispatch, TCP_CLIENT(client));
if (result != ISC_R_SUCCESS)
return (result);
/*
* The responsibility for listening for new requests is hereby
* transferred to the new client. Therefore, the old client
* should refrain from listening for any more requests.
*/
client->mortal = ISC_TRUE;
return (ISC_R_SUCCESS);
}
/***
*** Client Manager
***/
static void
clientmgr_destroy(ns_clientmgr_t *manager) {
#if NMCTXS > 0
int i;
#endif
REQUIRE(ISC_LIST_EMPTY(manager->clients));
MTRACE("clientmgr_destroy");
#if NMCTXS > 0
for (i = 0; i < NMCTXS; i++) {
if (manager->mctxpool[i] != NULL)
isc_mem_detach(&manager->mctxpool[i]);
}
#endif
ISC_QUEUE_DESTROY(manager->inactive);
DESTROYLOCK(&manager->lock);
DESTROYLOCK(&manager->listlock);
DESTROYLOCK(&manager->reclock);
manager->magic = 0;
isc_mem_put(manager->mctx, manager, sizeof(*manager));
}
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;
#if NMCTXS > 0
int i;
#endif
manager = isc_mem_get(mctx, sizeof(*manager));
if (manager == NULL)
return (ISC_R_NOMEMORY);
result = isc_mutex_init(&manager->lock);
if (result != ISC_R_SUCCESS)
goto cleanup_manager;
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;
manager->mctx = mctx;
manager->taskmgr = taskmgr;
manager->timermgr = timermgr;
manager->exiting = ISC_FALSE;
ISC_LIST_INIT(manager->clients);
ISC_LIST_INIT(manager->recursing);
ISC_QUEUE_INIT(manager->inactive, ilink);
#if NMCTXS > 0
manager->nextmctx = 0;
for (i = 0; i < NMCTXS; i++)
manager->mctxpool[i] = NULL; /* will be created on-demand */
#endif
manager->magic = MANAGER_MAGIC;
MTRACE("create");
*managerp = manager;
return (ISC_R_SUCCESS);
cleanup_listlock:
isc_mutex_destroy(&manager->listlock);
cleanup_lock:
isc_mutex_destroy(&manager->lock);
cleanup_manager:
isc_mem_put(manager->mctx, manager, sizeof(*manager));
return (result);
}
void
ns_clientmgr_destroy(ns_clientmgr_t **managerp) {
ns_clientmgr_t *manager;
ns_client_t *client;
isc_boolean_t need_destroy = ISC_FALSE;
REQUIRE(managerp != NULL);
manager = *managerp;
REQUIRE(VALID_MANAGER(manager));
MTRACE("destroy");
LOCK(&manager->listlock);
LOCK(&manager->lock);
manager->exiting = ISC_TRUE;
UNLOCK(&manager->lock);
for (client = ISC_LIST_HEAD(manager->clients);
client != NULL;
client = ISC_LIST_NEXT(client, link))
isc_task_shutdown(client->task);
if (ISC_LIST_EMPTY(manager->clients))
need_destroy = ISC_TRUE;
UNLOCK(&manager->listlock);
if (need_destroy)
clientmgr_destroy(manager);
*managerp = NULL;
}
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");
/*
* 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->state = NS_CLIENTSTATE_READY;
INSIST(client->recursionquota == NULL);
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);
}
isc_result_t
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
ns_interface_t *ifp, isc_boolean_t tcp)
{
isc_result_t result = ISC_R_SUCCESS;
unsigned int disp;
REQUIRE(VALID_MANAGER(manager));
REQUIRE(n > 0);
MTRACE("createclients");
for (disp = 0; disp < n; disp++) {
result = get_client(manager, ifp, ifp->udpdispatch[disp], tcp);
if (result != ISC_R_SUCCESS)
break;
}
return (result);
}
isc_sockaddr_t *
ns_client_getsockaddr(ns_client_t *client) {
return (&client->peeraddr);
}
isc_result_t
ns_client_checkaclsilent(ns_client_t *client, isc_netaddr_t *netaddr,
dns_acl_t *acl, isc_boolean_t default_allow)
{
isc_result_t result;
isc_netaddr_t tmpnetaddr;
int match;
if (acl == NULL) {
if (default_allow)
goto allow;
else
goto deny;
}
if (netaddr == NULL) {
isc_netaddr_fromsockaddr(&tmpnetaddr, &client->peeraddr);
netaddr = &tmpnetaddr;
}
result = dns_acl_match(netaddr, client->signer, acl,
&ns_g_server->aclenv, &match, NULL);
if (result != ISC_R_SUCCESS)
goto deny; /* Internal error, already logged. */
if (match > 0)
goto allow;
goto deny; /* Negative match or no match. */
allow:
return (ISC_R_SUCCESS);
deny:
return (DNS_R_REFUSED);
}
isc_result_t
ns_client_checkacl(ns_client_t *client, isc_sockaddr_t *sockaddr,
const char *opname, dns_acl_t *acl,
isc_boolean_t default_allow, int log_level)
{
isc_result_t result;
isc_netaddr_t netaddr;
if (sockaddr != NULL)
isc_netaddr_fromsockaddr(&netaddr, sockaddr);
result = ns_client_checkaclsilent(client, sockaddr ? &netaddr : NULL,
acl, default_allow);
if (result == ISC_R_SUCCESS)
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);
}
static void
ns_client_name(ns_client_t *client, char *peerbuf, size_t len) {
if (client->peeraddr_valid)
isc_sockaddr_format(&client->peeraddr, peerbuf, len);
else
snprintf(peerbuf, len, "@%p", client);
}
void
ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
isc_logmodule_t *module, int level, const char *fmt, va_list ap)
{
char msgbuf[2048];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
char signerbuf[DNS_NAME_FORMATSIZE], qnamebuf[DNS_NAME_FORMATSIZE];
const char *viewname = "";
const char *sep1 = "", *sep2 = "", *sep3 = "", *sep4 = "";
const char *signer = "", *qname = "";
dns_name_t *q = NULL;
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
ns_client_name(client, peerbuf, sizeof(peerbuf));
if (client->signer != NULL) {
dns_name_format(client->signer, signerbuf, sizeof(signerbuf));
sep1 = "/key ";
signer = signerbuf;
}
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;
}
if (client->view != NULL && strcmp(client->view->name, "_bind") != 0 &&
strcmp(client->view->name, "_default") != 0) {
sep4 = ": view ";
viewname = client->view->name;
}
isc_log_write(ns_g_lctx, category, module, level,
"client %s%s%s%s%s%s%s%s: %s",
peerbuf, sep1, signer, sep2, qname, sep3,
sep4, viewname, msgbuf);
}
void
ns_client_log(ns_client_t *client, isc_logcategory_t *category,
isc_logmodule_t *module, int level, const char *fmt, ...)
{
va_list ap;
if (! isc_log_wouldlog(ns_g_lctx, level))
return;
va_start(ap, fmt);
ns_client_logv(client, category, module, level, fmt, ap);
va_end(ap);
}
void
ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdatatype_t type,
dns_rdataclass_t rdclass, char *buf, size_t len)
{
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,
classbuf);
}
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;
/*
* Note that these are multiline debug messages. We want a newline
* to appear in the log after each message.
*/
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)
ns_client_log(client, NS_LOGCATEGORY_UNMATCHED,
NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
"%s\n%.*s", reason,
(int)isc_buffer_usedlength(&buffer),
buf);
} while (result == ISC_R_NOSPACE);
if (buf != NULL)
isc_mem_put(client->mctx, buf, len);
}
void
ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager) {
ns_client_t *client;
char namebuf[DNS_NAME_FORMATSIZE];
char original[DNS_NAME_FORMATSIZE];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
char typebuf[DNS_RDATATYPE_FORMATSIZE];
char classbuf[DNS_RDATACLASS_FORMATSIZE];
const char *name;
const char *sep;
const char *origfor;
dns_rdataset_t *rdataset;
REQUIRE(VALID_MANAGER(manager));
LOCK(&manager->reclock);
client = ISC_LIST_HEAD(manager->recursing);
while (client != NULL) {
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 = "";
}
LOCK(&client->query.fetchlock);
dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
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, "-");
}
UNLOCK(&client->query.fetchlock);
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,
origfor, original, client->requesttime);
client = ISC_LIST_NEXT(client, link);
}
UNLOCK(&manager->reclock);
}
void
ns_client_qnamereplace(ns_client_t *client, dns_name_t *name) {
LOCK(&client->query.fetchlock);
if (client->query.restarts > 0) {
/*
* client->query.qname was dynamically allocated.
*/
dns_message_puttempname(client->message,
&client->query.qname);
}
client->query.qname = name;
UNLOCK(&client->query.fetchlock);
}
isc_result_t
ns_client_sourceip(dns_clientinfo_t *ci, isc_sockaddr_t **addrp) {
ns_client_t *client = (ns_client_t *) ci->data;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(addrp != NULL);
*addrp = &client->peeraddr;
return (ISC_R_SUCCESS);
}