client.c revision 135446
1/*
2 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: client.c,v 1.176.2.13.4.22 2004/07/23 02:56:51 marka Exp $ */
19
20#include <config.h>
21
22#include <isc/formatcheck.h>
23#include <isc/mutex.h>
24#include <isc/once.h>
25#include <isc/print.h>
26#include <isc/stdio.h>
27#include <isc/string.h>
28#include <isc/task.h>
29#include <isc/timer.h>
30#include <isc/util.h>
31
32#include <dns/db.h>
33#include <dns/dispatch.h>
34#include <dns/events.h>
35#include <dns/message.h>
36#include <dns/rcode.h>
37#include <dns/resolver.h>
38#include <dns/rdata.h>
39#include <dns/rdataclass.h>
40#include <dns/rdatalist.h>
41#include <dns/rdataset.h>
42#include <dns/tsig.h>
43#include <dns/view.h>
44#include <dns/zone.h>
45
46#include <named/interfacemgr.h>
47#include <named/log.h>
48#include <named/notify.h>
49#include <named/server.h>
50#include <named/update.h>
51
52/***
53 *** Client
54 ***/
55
56/*
57 * Important note!
58 *
59 * All client state changes, other than that from idle to listening, occur
60 * as a result of events.  This guarantees serialization and avoids the
61 * need for locking.
62 *
63 * If a routine is ever created that allows someone other than the client's
64 * task to change the client, then the client will have to be locked.
65 */
66
67#define NS_CLIENT_TRACE
68#ifdef NS_CLIENT_TRACE
69#define CTRACE(m)	ns_client_log(client, \
70				      NS_LOGCATEGORY_CLIENT, \
71				      NS_LOGMODULE_CLIENT, \
72				      ISC_LOG_DEBUG(3), \
73				      "%s", (m))
74#define MTRACE(m)	isc_log_write(ns_g_lctx, \
75				      NS_LOGCATEGORY_GENERAL, \
76				      NS_LOGMODULE_CLIENT, \
77				      ISC_LOG_DEBUG(3), \
78				      "clientmgr @%p: %s", manager, (m))
79#else
80#define CTRACE(m)	((void)(m))
81#define MTRACE(m)	((void)(m))
82#endif
83
84#define TCP_CLIENT(c)	(((c)->attributes & NS_CLIENTATTR_TCP) != 0)
85
86#define TCP_BUFFER_SIZE			(65535 + 2)
87#define SEND_BUFFER_SIZE		4096
88#define RECV_BUFFER_SIZE		4096
89
90struct ns_clientmgr {
91	/* Unlocked. */
92	unsigned int			magic;
93	isc_mem_t *			mctx;
94	isc_taskmgr_t *			taskmgr;
95	isc_timermgr_t *		timermgr;
96	isc_mutex_t			lock;
97	/* Locked by lock. */
98	isc_boolean_t			exiting;
99	client_list_t			active; 	/* Active clients */
100	client_list_t			recursing; 	/* Recursing clients */
101	client_list_t 			inactive;	/* To be recycled */
102};
103
104#define MANAGER_MAGIC			ISC_MAGIC('N', 'S', 'C', 'm')
105#define VALID_MANAGER(m)		ISC_MAGIC_VALID(m, MANAGER_MAGIC)
106
107/*
108 * Client object states.  Ordering is significant: higher-numbered
109 * states are generally "more active", meaning that the client can
110 * have more dynamically allocated data, outstanding events, etc.
111 * In the list below, any such properties listed for state N
112 * also apply to any state > N.
113 *
114 * To force the client into a less active state, set client->newstate
115 * to that state and call exit_check().  This will cause any
116 * activities defined for higher-numbered states to be aborted.
117 */
118
119#define NS_CLIENTSTATE_FREED    0
120/*
121 * The client object no longer exists.
122 */
123
124#define NS_CLIENTSTATE_INACTIVE 1
125/*
126 * The client object exists and has a task and timer.
127 * Its "query" struct and sendbuf are initialized.
128 * It is on the client manager's list of inactive clients.
129 * It has a message and OPT, both in the reset state.
130 */
131
132#define NS_CLIENTSTATE_READY    2
133/*
134 * The client object is either a TCP or a UDP one, and
135 * it is associated with a network interface.  It is on the
136 * client manager's list of active clients.
137 *
138 * If it is a TCP client object, it has a TCP listener socket
139 * and an outstanding TCP listen request.
140 *
141 * If it is a UDP client object, it has a UDP listener socket
142 * and an outstanding UDP receive request.
143 */
144
145#define NS_CLIENTSTATE_READING  3
146/*
147 * The client object is a TCP client object that has received
148 * a connection.  It has a tcpsocket, tcpmsg, TCP quota, and an
149 * outstanding TCP read request.  This state is not used for
150 * UDP client objects.
151 */
152
153#define NS_CLIENTSTATE_WORKING  4
154/*
155 * The client object has received a request and is working
156 * on it.  It has a view, and it may have any of a non-reset OPT,
157 * recursion quota, and an outstanding write request.
158 */
159
160#define NS_CLIENTSTATE_MAX      9
161/*
162 * Sentinel value used to indicate "no state".  When client->newstate
163 * has this value, we are not attempting to exit the current state.
164 * Must be greater than any valid state.
165 */
166
167
168static void client_read(ns_client_t *client);
169static void client_accept(ns_client_t *client);
170static void client_udprecv(ns_client_t *client);
171static void clientmgr_destroy(ns_clientmgr_t *manager);
172static isc_boolean_t exit_check(ns_client_t *client);
173static void ns_client_endrequest(ns_client_t *client);
174static void ns_client_checkactive(ns_client_t *client);
175static void client_start(isc_task_t *task, isc_event_t *event);
176static void client_request(isc_task_t *task, isc_event_t *event);
177static void ns_client_dumpmessage(ns_client_t *client, const char *reason);
178
179void
180ns_client_recursing(ns_client_t *client, isc_boolean_t killoldest) {
181	ns_client_t *oldest;
182	REQUIRE(NS_CLIENT_VALID(client));
183
184	LOCK(&client->manager->lock);
185	if (killoldest) {
186		oldest = ISC_LIST_HEAD(client->manager->recursing);
187		if (oldest != NULL) {
188			ns_query_cancel(oldest);
189			ISC_LIST_UNLINK(*oldest->list, oldest, link);
190			ISC_LIST_APPEND(client->manager->active, oldest, link);
191			oldest->list = &client->manager->active;
192		}
193	}
194	ISC_LIST_UNLINK(*client->list, client, link);
195	ISC_LIST_APPEND(client->manager->recursing, client, link);
196	client->list = &client->manager->recursing;
197	UNLOCK(&client->manager->lock);
198}
199
200void
201ns_client_settimeout(ns_client_t *client, unsigned int seconds) {
202	isc_result_t result;
203	isc_interval_t interval;
204
205	isc_interval_set(&interval, seconds, 0);
206	result = isc_timer_reset(client->timer, isc_timertype_once, NULL,
207				 &interval, ISC_FALSE);
208	client->timerset = ISC_TRUE;
209	if (result != ISC_R_SUCCESS) {
210		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
211			      NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
212			      "setting timeout: %s",
213			      isc_result_totext(result));
214		/* Continue anyway. */
215	}
216}
217
218/*
219 * Check for a deactivation or shutdown request and take appropriate
220 * action.  Returns ISC_TRUE if either is in progress; in this case
221 * the caller must no longer use the client object as it may have been
222 * freed.
223 */
224static isc_boolean_t
225exit_check(ns_client_t *client) {
226	ns_clientmgr_t *locked_manager = NULL;
227	ns_clientmgr_t *destroy_manager = NULL;
228
229	REQUIRE(NS_CLIENT_VALID(client));
230
231	if (client->state <= client->newstate)
232		return (ISC_FALSE); /* Business as usual. */
233
234	INSIST(client->newstate < NS_CLIENTSTATE_WORKING);
235
236	/*
237	 * We need to detach from the view early when shutting down
238	 * the server to break the following vicious circle:
239	 *
240	 *  - The resolver will not shut down until the view refcount is zero
241	 *  - The view refcount does not go to zero until all clients detach
242	 *  - The client does not detach from the view until references is zero
243	 *  - references does not go to zero until the resolver has shut down
244	 *
245	 * Keep the view attached until any outstanding updates complete.
246	 */
247	if (client->nupdates == 0 &&
248	    client->newstate == NS_CLIENTSTATE_FREED && client->view != NULL)
249		dns_view_detach(&client->view);
250
251	if (client->state == NS_CLIENTSTATE_WORKING) {
252		INSIST(client->newstate <= NS_CLIENTSTATE_READING);
253		/*
254		 * Let the update processing complete.
255		 */
256		if (client->nupdates > 0)
257			return (ISC_TRUE);
258		/*
259		 * We are trying to abort request processing.
260		 */
261		if (client->nsends > 0) {
262			isc_socket_t *socket;
263			if (TCP_CLIENT(client))
264				socket = client->tcpsocket;
265			else
266				socket = client->udpsocket;
267			isc_socket_cancel(socket, client->task,
268					  ISC_SOCKCANCEL_SEND);
269		}
270
271		if (! (client->nsends == 0 && client->nrecvs == 0 &&
272		       client->references == 0))
273		{
274			/*
275			 * Still waiting for I/O cancel completion.
276			 * or lingering references.
277			 */
278			return (ISC_TRUE);
279		}
280		/*
281		 * I/O cancel is complete.  Burn down all state
282		 * related to the current request.
283		 */
284		ns_client_endrequest(client);
285
286		client->state = NS_CLIENTSTATE_READING;
287		INSIST(client->recursionquota == NULL);
288		if (NS_CLIENTSTATE_READING == client->newstate) {
289			client_read(client);
290			client->newstate = NS_CLIENTSTATE_MAX;
291			return (ISC_TRUE); /* We're done. */
292		}
293	}
294
295	if (client->state == NS_CLIENTSTATE_READING) {
296		/*
297		 * We are trying to abort the current TCP connection,
298		 * if any.
299		 */
300		INSIST(client->recursionquota == NULL);
301		INSIST(client->newstate <= NS_CLIENTSTATE_READY);
302		if (client->nreads > 0)
303			dns_tcpmsg_cancelread(&client->tcpmsg);
304		if (! client->nreads == 0) {
305			/* Still waiting for read cancel completion. */
306			return (ISC_TRUE);
307		}
308
309		if (client->tcpmsg_valid) {
310			dns_tcpmsg_invalidate(&client->tcpmsg);
311			client->tcpmsg_valid = ISC_FALSE;
312		}
313		if (client->tcpsocket != NULL) {
314			CTRACE("closetcp");
315			isc_socket_detach(&client->tcpsocket);
316		}
317
318		if (client->tcpquota != NULL)
319			isc_quota_detach(&client->tcpquota);
320
321		if (client->timerset) {
322			(void)isc_timer_reset(client->timer,
323					      isc_timertype_inactive,
324					      NULL, NULL, ISC_TRUE);
325			client->timerset = ISC_FALSE;
326		}
327
328		client->peeraddr_valid = ISC_FALSE;
329
330		client->state = NS_CLIENTSTATE_READY;
331		INSIST(client->recursionquota == NULL);
332
333		/*
334		 * Now the client is ready to accept a new TCP connection
335		 * or UDP request, but we may have enough clients doing
336		 * that already.  Check whether this client needs to remain
337		 * active and force it to go inactive if not.
338		 */
339		ns_client_checkactive(client);
340
341		if (NS_CLIENTSTATE_READY == client->newstate) {
342			if (TCP_CLIENT(client)) {
343				client_accept(client);
344			} else
345				client_udprecv(client);
346			client->newstate = NS_CLIENTSTATE_MAX;
347			return (ISC_TRUE);
348		}
349	}
350
351	if (client->state == NS_CLIENTSTATE_READY) {
352		INSIST(client->newstate <= NS_CLIENTSTATE_INACTIVE);
353		/*
354		 * We are trying to enter the inactive state.
355		 */
356		if (client->naccepts > 0)
357			isc_socket_cancel(client->tcplistener, client->task,
358					  ISC_SOCKCANCEL_ACCEPT);
359
360		if (! (client->naccepts == 0)) {
361			/* Still waiting for accept cancel completion. */
362			return (ISC_TRUE);
363		}
364		/* Accept cancel is complete. */
365
366		if (client->nrecvs > 0)
367			isc_socket_cancel(client->udpsocket, client->task,
368					  ISC_SOCKCANCEL_RECV);
369		if (! (client->nrecvs == 0)) {
370			/* Still waiting for recv cancel completion. */
371			return (ISC_TRUE);
372		}
373		/* Recv cancel is complete. */
374
375		if (client->nctls > 0) {
376			/* Still waiting for control event to be delivered */
377			return (ISC_TRUE);
378		}
379
380		/* Deactivate the client. */
381		if (client->interface)
382			ns_interface_detach(&client->interface);
383
384		INSIST(client->naccepts == 0);
385		INSIST(client->recursionquota == NULL);
386		if (client->tcplistener != NULL)
387			isc_socket_detach(&client->tcplistener);
388
389		if (client->udpsocket != NULL)
390			isc_socket_detach(&client->udpsocket);
391
392		if (client->dispatch != NULL)
393			dns_dispatch_detach(&client->dispatch);
394
395		client->attributes = 0;
396		client->mortal = ISC_FALSE;
397
398		LOCK(&client->manager->lock);
399		/*
400		 * Put the client on the inactive list.  If we are aiming for
401		 * the "freed" state, it will be removed from the inactive
402		 * list shortly, and we need to keep the manager locked until
403		 * that has been done, lest the manager decide to reactivate
404		 * the dying client inbetween.
405		 */
406		locked_manager = client->manager;
407		ISC_LIST_UNLINK(*client->list, client, link);
408		ISC_LIST_APPEND(client->manager->inactive, client, link);
409		client->list = &client->manager->inactive;
410		client->state = NS_CLIENTSTATE_INACTIVE;
411		INSIST(client->recursionquota == NULL);
412
413		if (client->state == client->newstate) {
414			client->newstate = NS_CLIENTSTATE_MAX;
415			goto unlock;
416		}
417	}
418
419	if (client->state == NS_CLIENTSTATE_INACTIVE) {
420		INSIST(client->newstate == NS_CLIENTSTATE_FREED);
421		/*
422		 * We are trying to free the client.
423		 *
424		 * When "shuttingdown" is true, either the task has received
425		 * its shutdown event or no shutdown event has ever been
426		 * set up.  Thus, we have no outstanding shutdown
427		 * event at this point.
428		 */
429		REQUIRE(client->state == NS_CLIENTSTATE_INACTIVE);
430
431		INSIST(client->recursionquota == NULL);
432
433		ns_query_free(client);
434		isc_mem_put(client->mctx, client->recvbuf, RECV_BUFFER_SIZE);
435		isc_event_free((isc_event_t **)&client->sendevent);
436		isc_event_free((isc_event_t **)&client->recvevent);
437		isc_timer_detach(&client->timer);
438
439		if (client->tcpbuf != NULL)
440			isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
441		if (client->opt != NULL) {
442			INSIST(dns_rdataset_isassociated(client->opt));
443			dns_rdataset_disassociate(client->opt);
444			dns_message_puttemprdataset(client->message, &client->opt);
445		}
446		dns_message_destroy(&client->message);
447		if (client->manager != NULL) {
448			ns_clientmgr_t *manager = client->manager;
449			if (locked_manager == NULL) {
450				LOCK(&manager->lock);
451				locked_manager = manager;
452			}
453			ISC_LIST_UNLINK(*client->list, client, link);
454			client->list = NULL;
455			if (manager->exiting &&
456			    ISC_LIST_EMPTY(manager->active) &&
457			    ISC_LIST_EMPTY(manager->inactive) &&
458			    ISC_LIST_EMPTY(manager->recursing))
459				destroy_manager = manager;
460		}
461		/*
462		 * Detaching the task must be done after unlinking from
463		 * the manager's lists because the manager accesses
464		 * client->task.
465		 */
466		if (client->task != NULL)
467			isc_task_detach(&client->task);
468
469		CTRACE("free");
470		client->magic = 0;
471		isc_mem_put(client->mctx, client, sizeof(*client));
472
473		goto unlock;
474	}
475
476 unlock:
477	if (locked_manager != NULL) {
478		UNLOCK(&locked_manager->lock);
479		locked_manager = NULL;
480	}
481
482	/*
483	 * Only now is it safe to destroy the client manager (if needed),
484	 * because we have accessed its lock for the last time.
485	 */
486	if (destroy_manager != NULL)
487		clientmgr_destroy(destroy_manager);
488
489	return (ISC_TRUE);
490}
491
492/*
493 * The client's task has received the client's control event
494 * as part of the startup process.
495 */
496static void
497client_start(isc_task_t *task, isc_event_t *event) {
498	ns_client_t *client = (ns_client_t *) event->ev_arg;
499
500	INSIST(task == client->task);
501
502	UNUSED(task);
503
504	INSIST(client->nctls == 1);
505	client->nctls--;
506
507	if (exit_check(client))
508		return;
509
510	if (TCP_CLIENT(client)) {
511		client_accept(client);
512	} else {
513		client_udprecv(client);
514	}
515}
516
517
518/*
519 * The client's task has received a shutdown event.
520 */
521static void
522client_shutdown(isc_task_t *task, isc_event_t *event) {
523	ns_client_t *client;
524
525	REQUIRE(event != NULL);
526	REQUIRE(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
527	client = event->ev_arg;
528	REQUIRE(NS_CLIENT_VALID(client));
529	REQUIRE(task == client->task);
530
531	UNUSED(task);
532
533	CTRACE("shutdown");
534
535	isc_event_free(&event);
536
537	if (client->shutdown != NULL) {
538		(client->shutdown)(client->shutdown_arg, ISC_R_SHUTTINGDOWN);
539		client->shutdown = NULL;
540		client->shutdown_arg = NULL;
541	}
542
543	client->newstate = NS_CLIENTSTATE_FREED;
544	(void)exit_check(client);
545}
546
547static void
548ns_client_endrequest(ns_client_t *client) {
549	INSIST(client->naccepts == 0);
550	INSIST(client->nreads == 0);
551	INSIST(client->nsends == 0);
552	INSIST(client->nrecvs == 0);
553	INSIST(client->nupdates == 0);
554	INSIST(client->state == NS_CLIENTSTATE_WORKING);
555
556	CTRACE("endrequest");
557
558	if (client->next != NULL) {
559		(client->next)(client);
560		client->next = NULL;
561	}
562
563	if (client->view != NULL)
564		dns_view_detach(&client->view);
565	if (client->opt != NULL) {
566		INSIST(dns_rdataset_isassociated(client->opt));
567		dns_rdataset_disassociate(client->opt);
568		dns_message_puttemprdataset(client->message, &client->opt);
569	}
570
571	client->udpsize = 512;
572	client->extflags = 0;
573	dns_message_reset(client->message, DNS_MESSAGE_INTENTPARSE);
574
575	if (client->recursionquota != NULL)
576		isc_quota_detach(&client->recursionquota);
577
578	/*
579	 * Clear all client attributes that are specific to
580	 * the request; that's all except the TCP flag.
581	 */
582	client->attributes &= NS_CLIENTATTR_TCP;
583}
584
585static void
586ns_client_checkactive(ns_client_t *client) {
587	if (client->mortal) {
588		/*
589		 * This client object should normally go inactive
590		 * at this point, but if we have fewer active client
591		 * objects than  desired due to earlier quota exhaustion,
592		 * keep it active to make up for the shortage.
593		 */
594		isc_boolean_t need_another_client = ISC_FALSE;
595		if (TCP_CLIENT(client)) {
596			LOCK(&client->interface->lock);
597			if (client->interface->ntcpcurrent <
598			    client->interface->ntcptarget)
599				need_another_client = ISC_TRUE;
600			UNLOCK(&client->interface->lock);
601		} else {
602			/*
603			 * The UDP client quota is enforced by making
604			 * requests fail rather than by not listening
605			 * for new ones.  Therefore, there is always a
606			 * full set of UDP clients listening.
607			 */
608		}
609		if (! need_another_client) {
610			/*
611			 * We don't need this client object.  Recycle it.
612			 */
613			if (client->newstate >= NS_CLIENTSTATE_INACTIVE)
614				client->newstate = NS_CLIENTSTATE_INACTIVE;
615		}
616	}
617}
618
619void
620ns_client_next(ns_client_t *client, isc_result_t result) {
621	int newstate;
622
623	REQUIRE(NS_CLIENT_VALID(client));
624	REQUIRE(client->state == NS_CLIENTSTATE_WORKING ||
625		client->state == NS_CLIENTSTATE_READING);
626
627	CTRACE("next");
628
629	if (result != ISC_R_SUCCESS)
630		ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
631			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
632			      "request failed: %s", isc_result_totext(result));
633
634	/*
635	 * An error processing a TCP request may have left
636	 * the connection out of sync.  To be safe, we always
637	 * sever the connection when result != ISC_R_SUCCESS.
638	 */
639	if (result == ISC_R_SUCCESS && TCP_CLIENT(client))
640		newstate = NS_CLIENTSTATE_READING;
641	else
642		newstate = NS_CLIENTSTATE_READY;
643
644	if (client->newstate > newstate)
645		client->newstate = newstate;
646	(void)exit_check(client);
647}
648
649
650static void
651client_senddone(isc_task_t *task, isc_event_t *event) {
652	ns_client_t *client;
653	isc_socketevent_t *sevent = (isc_socketevent_t *) event;
654
655	REQUIRE(sevent != NULL);
656	REQUIRE(sevent->ev_type == ISC_SOCKEVENT_SENDDONE);
657	client = sevent->ev_arg;
658	REQUIRE(NS_CLIENT_VALID(client));
659	REQUIRE(task == client->task);
660	REQUIRE(sevent == client->sendevent);
661
662	UNUSED(task);
663
664	CTRACE("senddone");
665
666	if (sevent->result != ISC_R_SUCCESS)
667		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
668			      NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
669			      "error sending response: %s",
670			      isc_result_totext(sevent->result));
671
672	INSIST(client->nsends > 0);
673	client->nsends--;
674
675	if (client->tcpbuf != NULL) {
676		INSIST(TCP_CLIENT(client));
677		isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
678		client->tcpbuf = NULL;
679	}
680
681	if (exit_check(client))
682		return;
683
684	ns_client_next(client, ISC_R_SUCCESS);
685}
686
687/*
688 * We only want to fail with ISC_R_NOSPACE when called from
689 * ns_client_sendraw() and not when called from ns_client_send(),
690 * tcpbuffer is NULL when called from ns_client_sendraw() and
691 * length != 0.  tcpbuffer != NULL when called from ns_client_send()
692 * and length == 0.
693 */
694
695static isc_result_t
696client_allocsendbuf(ns_client_t *client, isc_buffer_t *buffer,
697		    isc_buffer_t *tcpbuffer, isc_uint32_t length,
698		    unsigned char *sendbuf, unsigned char **datap)
699{
700	unsigned char *data;
701	isc_uint32_t bufsize;
702	isc_result_t result;
703
704	INSIST(datap != NULL);
705	INSIST((tcpbuffer == NULL && length != 0) ||
706	       (tcpbuffer != NULL && length == 0));
707
708	if (TCP_CLIENT(client)) {
709		INSIST(client->tcpbuf == NULL);
710		if (length + 2 > TCP_BUFFER_SIZE) {
711			result = ISC_R_NOSPACE;
712			goto done;
713		}
714		client->tcpbuf = isc_mem_get(client->mctx, TCP_BUFFER_SIZE);
715		if (client->tcpbuf == NULL) {
716			result = ISC_R_NOMEMORY;
717			goto done;
718		}
719		data = client->tcpbuf;
720		if (tcpbuffer != NULL) {
721			isc_buffer_init(tcpbuffer, data, TCP_BUFFER_SIZE);
722			isc_buffer_init(buffer, data + 2, TCP_BUFFER_SIZE - 2);
723		} else {
724			isc_buffer_init(buffer, data, TCP_BUFFER_SIZE);
725			INSIST(length <= 0xffff);
726			isc_buffer_putuint16(buffer, (isc_uint16_t)length);
727		}
728	} else {
729		data = sendbuf;
730		if (client->udpsize < SEND_BUFFER_SIZE)
731			bufsize = client->udpsize;
732		else
733			bufsize = SEND_BUFFER_SIZE;
734		if (length > bufsize) {
735			result = ISC_R_NOSPACE;
736			goto done;
737		}
738		isc_buffer_init(buffer, data, bufsize);
739	}
740	*datap = data;
741	result = ISC_R_SUCCESS;
742
743 done:
744	return (result);
745}
746
747static isc_result_t
748client_sendpkg(ns_client_t *client, isc_buffer_t *buffer) {
749	struct in6_pktinfo *pktinfo;
750	isc_result_t result;
751	isc_region_t r;
752	isc_sockaddr_t *address;
753	isc_socket_t *socket;
754	isc_netaddr_t netaddr;
755	int match;
756	unsigned int sockflags = ISC_SOCKFLAG_IMMEDIATE;
757
758	if (TCP_CLIENT(client)) {
759		socket = client->tcpsocket;
760		address = NULL;
761	} else {
762		socket = client->udpsocket;
763		address = &client->peeraddr;
764
765		isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
766		if (ns_g_server->blackholeacl != NULL &&
767		    dns_acl_match(&netaddr, NULL,
768			    	  ns_g_server->blackholeacl,
769				  &ns_g_server->aclenv,
770				  &match, NULL) == ISC_R_SUCCESS &&
771		    match > 0)
772			return (DNS_R_BLACKHOLED);
773		sockflags |= ISC_SOCKFLAG_NORETRY;
774	}
775
776	if ((client->attributes & NS_CLIENTATTR_PKTINFO) != 0 &&
777	    (client->attributes & NS_CLIENTATTR_MULTICAST) == 0)
778		pktinfo = &client->pktinfo;
779	else
780		pktinfo = NULL;
781
782	isc_buffer_usedregion(buffer, &r);
783
784	CTRACE("sendto");
785
786	result = isc_socket_sendto2(socket, &r, client->task,
787				    address, pktinfo,
788				    client->sendevent, sockflags);
789	if (result == ISC_R_SUCCESS || result == ISC_R_INPROGRESS) {
790		client->nsends++;
791		if (result == ISC_R_SUCCESS)
792			client_senddone(client->task,
793					(isc_event_t *)client->sendevent);
794		result = ISC_R_SUCCESS;
795	}
796	return (result);
797}
798
799void
800ns_client_sendraw(ns_client_t *client, dns_message_t *message) {
801	isc_result_t result;
802	unsigned char *data;
803	isc_buffer_t buffer;
804	isc_region_t r;
805	isc_region_t *mr;
806	unsigned char sendbuf[SEND_BUFFER_SIZE];
807
808	REQUIRE(NS_CLIENT_VALID(client));
809
810	CTRACE("sendraw");
811
812	mr = dns_message_getrawmessage(message);
813	if (mr == NULL) {
814		result = ISC_R_UNEXPECTEDEND;
815		goto done;
816	}
817
818	result = client_allocsendbuf(client, &buffer, NULL, mr->length,
819				     sendbuf, &data);
820	if (result != ISC_R_SUCCESS)
821		goto done;
822
823	/*
824	 * Copy message to buffer and fixup id.
825	 */
826	isc_buffer_availableregion(&buffer, &r);
827	result = isc_buffer_copyregion(&buffer, mr);
828	if (result != ISC_R_SUCCESS)
829		goto done;
830	r.base[0] = (client->message->id >> 8) & 0xff;
831	r.base[1] = client->message->id & 0xff;
832
833	result = client_sendpkg(client, &buffer);
834	if (result == ISC_R_SUCCESS)
835		return;
836
837 done:
838	if (client->tcpbuf != NULL) {
839		isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
840		client->tcpbuf = NULL;
841	}
842	ns_client_next(client, result);
843}
844
845void
846ns_client_send(ns_client_t *client) {
847	isc_result_t result;
848	unsigned char *data;
849	isc_buffer_t buffer;
850	isc_buffer_t tcpbuffer;
851	isc_region_t r;
852	dns_compress_t cctx;
853	isc_boolean_t cleanup_cctx = ISC_FALSE;
854	unsigned char sendbuf[SEND_BUFFER_SIZE];
855	unsigned int dnssec_opts;
856	unsigned int preferred_glue;
857
858	REQUIRE(NS_CLIENT_VALID(client));
859
860	CTRACE("send");
861
862	if ((client->attributes & NS_CLIENTATTR_RA) != 0)
863		client->message->flags |= DNS_MESSAGEFLAG_RA;
864
865	if ((client->attributes & NS_CLIENTATTR_WANTDNSSEC) != 0)
866		dnssec_opts = 0;
867	else
868		dnssec_opts = DNS_MESSAGERENDER_OMITDNSSEC;
869
870	preferred_glue = 0;
871	if (client->view != NULL) {
872		if (client->view->preferred_glue == dns_rdatatype_a)
873			preferred_glue = DNS_MESSAGERENDER_PREFER_A;
874		else if (client->view->preferred_glue == dns_rdatatype_aaaa)
875			preferred_glue = DNS_MESSAGERENDER_PREFER_AAAA;
876	}
877
878	/*
879	 * XXXRTH  The following doesn't deal with TCP buffer resizing.
880	 */
881	result = client_allocsendbuf(client, &buffer, &tcpbuffer, 0,
882				     sendbuf, &data);
883	if (result != ISC_R_SUCCESS)
884		goto done;
885
886	result = dns_compress_init(&cctx, -1, client->mctx);
887	if (result != ISC_R_SUCCESS)
888		goto done;
889	cleanup_cctx = ISC_TRUE;
890
891	result = dns_message_renderbegin(client->message, &cctx, &buffer);
892	if (result != ISC_R_SUCCESS)
893		goto done;
894	if (client->opt != NULL) {
895		result = dns_message_setopt(client->message, client->opt);
896		/*
897		 * XXXRTH dns_message_setopt() should probably do this...
898		 */
899		client->opt = NULL;
900		if (result != ISC_R_SUCCESS)
901			goto done;
902	}
903	result = dns_message_rendersection(client->message,
904					   DNS_SECTION_QUESTION, 0);
905	if (result == ISC_R_NOSPACE) {
906		client->message->flags |= DNS_MESSAGEFLAG_TC;
907		goto renderend;
908	}
909	if (result != ISC_R_SUCCESS)
910		goto done;
911	result = dns_message_rendersection(client->message,
912					   DNS_SECTION_ANSWER,
913					   DNS_MESSAGERENDER_PARTIAL |
914					   dnssec_opts);
915	if (result == ISC_R_NOSPACE) {
916		client->message->flags |= DNS_MESSAGEFLAG_TC;
917		goto renderend;
918	}
919	if (result != ISC_R_SUCCESS)
920		goto done;
921	result = dns_message_rendersection(client->message,
922					   DNS_SECTION_AUTHORITY,
923					   DNS_MESSAGERENDER_PARTIAL |
924					   dnssec_opts);
925	if (result == ISC_R_NOSPACE) {
926		client->message->flags |= DNS_MESSAGEFLAG_TC;
927		goto renderend;
928	}
929	if (result != ISC_R_SUCCESS)
930		goto done;
931	result = dns_message_rendersection(client->message,
932					   DNS_SECTION_ADDITIONAL,
933					   preferred_glue | dnssec_opts);
934	if (result != ISC_R_SUCCESS && result != ISC_R_NOSPACE)
935		goto done;
936 renderend:
937	result = dns_message_renderend(client->message);
938
939	if (result != ISC_R_SUCCESS)
940		goto done;
941
942	if (cleanup_cctx) {
943		dns_compress_invalidate(&cctx);
944		cleanup_cctx = ISC_FALSE;
945	}
946
947	if (TCP_CLIENT(client)) {
948		isc_buffer_usedregion(&buffer, &r);
949		isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t) r.length);
950		isc_buffer_add(&tcpbuffer, r.length);
951		result = client_sendpkg(client, &tcpbuffer);
952	} else
953		result = client_sendpkg(client, &buffer);
954	if (result == ISC_R_SUCCESS)
955		return;
956
957 done:
958	if (client->tcpbuf != NULL) {
959		isc_mem_put(client->mctx, client->tcpbuf, TCP_BUFFER_SIZE);
960		client->tcpbuf = NULL;
961	}
962
963	if (cleanup_cctx)
964		dns_compress_invalidate(&cctx);
965
966	ns_client_next(client, result);
967}
968
969void
970ns_client_error(ns_client_t *client, isc_result_t result) {
971	dns_rcode_t rcode;
972	dns_message_t *message;
973
974	REQUIRE(NS_CLIENT_VALID(client));
975
976	CTRACE("error");
977
978	message = client->message;
979	rcode = dns_result_torcode(result);
980
981	/*
982	 * Message may be an in-progress reply that we had trouble
983	 * with, in which case QR will be set.  We need to clear QR before
984	 * calling dns_message_reply() to avoid triggering an assertion.
985	 */
986	message->flags &= ~DNS_MESSAGEFLAG_QR;
987	/*
988	 * AA and AD shouldn't be set.
989	 */
990	message->flags &= ~(DNS_MESSAGEFLAG_AA | DNS_MESSAGEFLAG_AD);
991	result = dns_message_reply(message, ISC_TRUE);
992	if (result != ISC_R_SUCCESS) {
993		/*
994		 * It could be that we've got a query with a good header,
995		 * but a bad question section, so we try again with
996		 * want_question_section set to ISC_FALSE.
997		 */
998		result = dns_message_reply(message, ISC_FALSE);
999		if (result != ISC_R_SUCCESS) {
1000			ns_client_next(client, result);
1001			return;
1002		}
1003	}
1004	message->rcode = rcode;
1005
1006	/*
1007	 * FORMERR loop avoidance:  If we sent a FORMERR message
1008	 * with the same ID to the same client less than two
1009	 * seconds ago, assume that we are in an infinite error
1010	 * packet dialog with a server for some protocol whose
1011	 * error responses look enough like DNS queries to
1012	 * elicit a FORMERR response.  Drop a packet to break
1013	 * the loop.
1014	 */
1015	if (rcode == dns_rcode_formerr) {
1016		if (isc_sockaddr_equal(&client->peeraddr,
1017				       &client->formerrcache.addr) &&
1018		    message->id == client->formerrcache.id &&
1019		    client->requesttime - client->formerrcache.time < 2) {
1020			/* Drop packet. */
1021			ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1022				      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
1023				      "possible error packet loop, "
1024				      "FORMERR dropped");
1025			ns_client_next(client, result);
1026			return;
1027		}
1028		client->formerrcache.addr = client->peeraddr;
1029		client->formerrcache.time = client->requesttime;
1030		client->formerrcache.id = message->id;
1031	}
1032	ns_client_send(client);
1033}
1034
1035static inline isc_result_t
1036client_addopt(ns_client_t *client) {
1037	dns_rdataset_t *rdataset;
1038	dns_rdatalist_t *rdatalist;
1039	dns_rdata_t *rdata;
1040	isc_result_t result;
1041	dns_view_t *view;
1042	dns_resolver_t *resolver;
1043	isc_uint16_t udpsize;
1044
1045	REQUIRE(client->opt == NULL);	/* XXXRTH free old. */
1046
1047	rdatalist = NULL;
1048	result = dns_message_gettemprdatalist(client->message, &rdatalist);
1049	if (result != ISC_R_SUCCESS)
1050		return (result);
1051	rdata = NULL;
1052	result = dns_message_gettemprdata(client->message, &rdata);
1053	if (result != ISC_R_SUCCESS)
1054		return (result);
1055	rdataset = NULL;
1056	result = dns_message_gettemprdataset(client->message, &rdataset);
1057	if (result != ISC_R_SUCCESS)
1058		return (result);
1059	dns_rdataset_init(rdataset);
1060
1061	rdatalist->type = dns_rdatatype_opt;
1062	rdatalist->covers = 0;
1063
1064	/*
1065	 * Set the maximum UDP buffer size.
1066	 */
1067	view = client->view;
1068	resolver = (view != NULL) ? view->resolver : NULL;
1069	if (resolver != NULL)
1070		udpsize = dns_resolver_getudpsize(resolver);
1071	else
1072		udpsize = ns_g_udpsize;
1073	rdatalist->rdclass = udpsize;
1074
1075	/*
1076	 * Set EXTENDED-RCODE, VERSION and Z to 0.
1077	 */
1078	rdatalist->ttl = (client->extflags & DNS_MESSAGEEXTFLAG_REPLYPRESERVE);
1079
1080	/*
1081	 * No ENDS options in the default case.
1082	 */
1083	rdata->data = NULL;
1084	rdata->length = 0;
1085	rdata->rdclass = rdatalist->rdclass;
1086	rdata->type = rdatalist->type;
1087	rdata->flags = 0;
1088
1089	ISC_LIST_INIT(rdatalist->rdata);
1090	ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1091	RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset)
1092		      == ISC_R_SUCCESS);
1093
1094	client->opt = rdataset;
1095
1096	return (ISC_R_SUCCESS);
1097}
1098
1099static inline isc_boolean_t
1100allowed(isc_netaddr_t *addr, dns_name_t *signer, dns_acl_t *acl) {
1101	int match;
1102	isc_result_t result;
1103
1104	if (acl == NULL)
1105		return (ISC_TRUE);
1106	result = dns_acl_match(addr, signer, acl, &ns_g_server->aclenv,
1107			       &match, NULL);
1108	if (result == ISC_R_SUCCESS && match > 0)
1109		return (ISC_TRUE);
1110	return (ISC_FALSE);
1111}
1112
1113/*
1114 * Handle an incoming request event from the socket (UDP case)
1115 * or tcpmsg (TCP case).
1116 */
1117static void
1118client_request(isc_task_t *task, isc_event_t *event) {
1119	ns_client_t *client;
1120	isc_socketevent_t *sevent;
1121	isc_result_t result;
1122	isc_result_t sigresult = ISC_R_SUCCESS;
1123	isc_buffer_t *buffer;
1124	isc_buffer_t tbuffer;
1125	dns_view_t *view;
1126	dns_rdataset_t *opt;
1127	isc_boolean_t ra; 	/* Recursion available. */
1128	isc_netaddr_t netaddr;
1129	isc_netaddr_t destaddr;
1130	int match;
1131	dns_messageid_t id;
1132	unsigned int flags;
1133	isc_boolean_t notimp;
1134
1135	REQUIRE(event != NULL);
1136	client = event->ev_arg;
1137	REQUIRE(NS_CLIENT_VALID(client));
1138	REQUIRE(task == client->task);
1139
1140	INSIST(client->recursionquota == NULL);
1141
1142	INSIST(client->state ==
1143	       TCP_CLIENT(client) ?
1144	       NS_CLIENTSTATE_READING :
1145	       NS_CLIENTSTATE_READY);
1146
1147	if (event->ev_type == ISC_SOCKEVENT_RECVDONE) {
1148		INSIST(!TCP_CLIENT(client));
1149		sevent = (isc_socketevent_t *)event;
1150		REQUIRE(sevent == client->recvevent);
1151		isc_buffer_init(&tbuffer, sevent->region.base, sevent->n);
1152		isc_buffer_add(&tbuffer, sevent->n);
1153		buffer = &tbuffer;
1154		result = sevent->result;
1155		if (result == ISC_R_SUCCESS) {
1156			client->peeraddr = sevent->address;
1157			client->peeraddr_valid = ISC_TRUE;
1158		}
1159		if ((sevent->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0) {
1160			client->attributes |= NS_CLIENTATTR_PKTINFO;
1161			client->pktinfo = sevent->pktinfo;
1162		}
1163		if ((sevent->attributes & ISC_SOCKEVENTATTR_MULTICAST) != 0)
1164			client->attributes |= NS_CLIENTATTR_MULTICAST;
1165		client->nrecvs--;
1166	} else {
1167		INSIST(TCP_CLIENT(client));
1168		REQUIRE(event->ev_type == DNS_EVENT_TCPMSG);
1169		REQUIRE(event->ev_sender == &client->tcpmsg);
1170		buffer = &client->tcpmsg.buffer;
1171		result = client->tcpmsg.result;
1172		INSIST(client->nreads == 1);
1173		/*
1174		 * client->peeraddr was set when the connection was accepted.
1175		 */
1176		client->nreads--;
1177	}
1178
1179	if (exit_check(client))
1180		goto cleanup;
1181	client->state = client->newstate = NS_CLIENTSTATE_WORKING;
1182
1183	isc_task_getcurrenttime(task, &client->requesttime);
1184	client->now = client->requesttime;
1185
1186	if (result != ISC_R_SUCCESS) {
1187		if (TCP_CLIENT(client)) {
1188			ns_client_next(client, result);
1189		} else {
1190			if  (result != ISC_R_CANCELED)
1191				isc_log_write(ns_g_lctx, NS_LOGCATEGORY_CLIENT,
1192					      NS_LOGMODULE_CLIENT,
1193					      ISC_LOG_ERROR,
1194					      "UDP client handler shutting "
1195					      "down due to fatal receive "
1196					      "error: %s",
1197					      isc_result_totext(result));
1198			isc_task_shutdown(client->task);
1199		}
1200		goto cleanup;
1201	}
1202
1203	isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
1204
1205	ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1206		      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1207		      "%s request",
1208		      TCP_CLIENT(client) ? "TCP" : "UDP");
1209
1210	/*
1211	 * Check the blackhole ACL for UDP only, since TCP is done in
1212	 * client_newconn.
1213	 */
1214	if (!TCP_CLIENT(client)) {
1215
1216		if (ns_g_server->blackholeacl != NULL &&
1217		    dns_acl_match(&netaddr, NULL, ns_g_server->blackholeacl,
1218				  &ns_g_server->aclenv,
1219				  &match, NULL) == ISC_R_SUCCESS &&
1220		    match > 0)
1221		{
1222			ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1223				      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1224				      "blackholed UDP datagram");
1225			ns_client_next(client, ISC_R_SUCCESS);
1226			goto cleanup;
1227		}
1228	}
1229
1230	/*
1231	 * Silently drop multicast requests for the present.
1232	 * XXXMPA look at when/if mDNS spec stabilizes.
1233	 */
1234	if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) {
1235		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1236			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(2),
1237			      "dropping multicast request");
1238		ns_client_next(client, DNS_R_REFUSED);
1239	}
1240
1241	result = dns_message_peekheader(buffer, &id, &flags);
1242	if (result != ISC_R_SUCCESS) {
1243		/*
1244		 * There isn't enough header to determine whether
1245		 * this was a request or a response.  Drop it.
1246		 */
1247		ns_client_next(client, result);
1248		goto cleanup;
1249	}
1250
1251	/*
1252	 * The client object handles requests, not responses.
1253	 * If this is a UDP response, forward it to the dispatcher.
1254	 * If it's a TCP response, discard it here.
1255	 */
1256	if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
1257		if (TCP_CLIENT(client)) {
1258			CTRACE("unexpected response");
1259			ns_client_next(client, DNS_R_FORMERR);
1260			goto cleanup;
1261		} else {
1262			dns_dispatch_importrecv(client->dispatch, event);
1263			ns_client_next(client, ISC_R_SUCCESS);
1264			goto cleanup;
1265		}
1266	}
1267
1268	/*
1269	 * It's a request.  Parse it.
1270	 */
1271	result = dns_message_parse(client->message, buffer, 0);
1272	if (result != ISC_R_SUCCESS) {
1273		/*
1274		 * Parsing the request failed.  Send a response
1275		 * (typically FORMERR or SERVFAIL).
1276		 */
1277		ns_client_error(client, result);
1278		goto cleanup;
1279	}
1280
1281	switch (client->message->opcode) {
1282	case dns_opcode_query:
1283	case dns_opcode_update:
1284	case dns_opcode_notify:
1285		notimp = ISC_FALSE;
1286		break;
1287	case dns_opcode_iquery:
1288	default:
1289		notimp = ISC_TRUE;
1290		break;
1291	}
1292
1293	client->message->rcode = dns_rcode_noerror;
1294
1295	/* RFC1123 section 6.1.3.2 */
1296	if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0)
1297		client->message->flags &= ~DNS_MESSAGEFLAG_RD;
1298
1299	/*
1300	 * Deal with EDNS.
1301	 */
1302	opt = dns_message_getopt(client->message);
1303	if (opt != NULL) {
1304		unsigned int version;
1305
1306		/*
1307		 * Set the client's UDP buffer size.
1308		 */
1309		client->udpsize = opt->rdclass;
1310
1311		/*
1312		 * If the requested UDP buffer size is less than 512,
1313		 * ignore it and use 512.
1314		 */
1315		if (client->udpsize < 512)
1316			client->udpsize = 512;
1317
1318		/*
1319		 * Get the flags out of the OPT record.
1320		 */
1321		client->extflags = (isc_uint16_t)(opt->ttl & 0xFFFF);
1322
1323		/*
1324		 * Create an OPT for our reply.
1325		 */
1326		result = client_addopt(client);
1327		if (result != ISC_R_SUCCESS) {
1328			ns_client_error(client, result);
1329			goto cleanup;
1330		}
1331
1332		/*
1333		 * Do we understand this version of ENDS?
1334		 *
1335		 * XXXRTH need library support for this!
1336		 */
1337		version = (opt->ttl & 0x00FF0000) >> 16;
1338		if (version != 0) {
1339			ns_client_error(client, DNS_R_BADVERS);
1340			goto cleanup;
1341		}
1342	}
1343
1344	if (client->message->rdclass == 0) {
1345		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1346			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
1347			      "message class could not be determined");
1348		ns_client_dumpmessage(client,
1349				      "message class could not be determined");
1350		ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR);
1351		goto cleanup;
1352	}
1353
1354	/*
1355	 * Determine the destination address.  If the receiving interface is
1356	 * bound to a specific address, we simply use it regardless of the
1357	 * address family.  All IPv4 queries should fall into this case.
1358	 * Otherwise, if this is a TCP query, get the address from the
1359	 * receiving socket (this needs a system call and can be heavy).
1360	 * For IPv6 UDP queries, we get this from the pktinfo structure (if
1361	 * supported).
1362	 * If all the attempts fail (this can happen due to memory shortage,
1363	 * etc), we regard this as an error for safety.
1364	 */
1365	if ((client->interface->flags & NS_INTERFACEFLAG_ANYADDR) == 0)
1366		isc_netaddr_fromsockaddr(&destaddr, &client->interface->addr);
1367	else {
1368		result = ISC_R_FAILURE;
1369
1370		if (TCP_CLIENT(client)) {
1371			isc_sockaddr_t destsockaddr;
1372
1373			result = isc_socket_getsockname(client->tcpsocket,
1374							&destsockaddr);
1375			if (result == ISC_R_SUCCESS)
1376				isc_netaddr_fromsockaddr(&destaddr,
1377							 &destsockaddr);
1378		}
1379		if (result != ISC_R_SUCCESS &&
1380		    client->interface->addr.type.sa.sa_family == AF_INET6 &&
1381		    (client->attributes & NS_CLIENTATTR_PKTINFO) != 0) {
1382			isc_uint32_t zone = 0;
1383
1384			/*
1385			 * XXXJT technically, we should convert the receiving
1386			 * interface ID to a proper scope zone ID.  However,
1387			 * due to the fact there is no standard API for this,
1388			 * we only handle link-local addresses and use the
1389			 * interface index as link ID.  Despite the assumption,
1390			 * it should cover most typical cases.
1391			 */
1392			if (IN6_IS_ADDR_LINKLOCAL(&client->pktinfo.ipi6_addr))
1393				zone = (isc_uint32_t)client->pktinfo.ipi6_ifindex;
1394
1395			isc_netaddr_fromin6(&destaddr,
1396					    &client->pktinfo.ipi6_addr);
1397			isc_netaddr_setzone(&destaddr, zone);
1398			result = ISC_R_SUCCESS;
1399		}
1400		if (result != ISC_R_SUCCESS) {
1401			UNEXPECTED_ERROR(__FILE__, __LINE__,
1402					 "failed to get request's "
1403					 "destination: %s",
1404					 isc_result_totext(result));
1405			goto cleanup;
1406		}
1407	}
1408
1409	/*
1410	 * Find a view that matches the client's source address.
1411	 */
1412	for (view = ISC_LIST_HEAD(ns_g_server->viewlist);
1413	     view != NULL;
1414	     view = ISC_LIST_NEXT(view, link)) {
1415		if (client->message->rdclass == view->rdclass ||
1416		    client->message->rdclass == dns_rdataclass_any)
1417		{
1418			dns_name_t *tsig = NULL;
1419			sigresult = dns_message_rechecksig(client->message,
1420							   view);
1421			if (sigresult == ISC_R_SUCCESS)
1422				tsig = client->message->tsigname;
1423
1424			if (allowed(&netaddr, tsig, view->matchclients) &&
1425			    allowed(&destaddr, tsig, view->matchdestinations) &&
1426			    !((client->message->flags & DNS_MESSAGEFLAG_RD)
1427			      == 0 && view->matchrecursiveonly))
1428			{
1429				dns_view_attach(view, &client->view);
1430				break;
1431			}
1432		}
1433	}
1434
1435	if (view == NULL) {
1436		char classname[DNS_RDATACLASS_FORMATSIZE];
1437
1438		/*
1439		 * Do a dummy TSIG verification attempt so that the
1440		 * response will have a TSIG if the query did, as
1441		 * required by RFC2845.
1442		 */
1443		isc_buffer_t b;
1444		isc_region_t *r;
1445
1446		dns_message_resetsig(client->message);
1447
1448		r = dns_message_getrawmessage(client->message);
1449		isc_buffer_init(&b, r->base, r->length);
1450		isc_buffer_add(&b, r->length);
1451		(void)dns_tsig_verify(&b, client->message, NULL, NULL);
1452
1453		dns_rdataclass_format(client->message->rdclass, classname,
1454				      sizeof(classname));
1455		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1456			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
1457			      "no matching view in class '%s'", classname);
1458		ns_client_dumpmessage(client, "no matching view in class");
1459		ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED);
1460		goto cleanup;
1461	}
1462
1463	ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1464		      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5),
1465		      "using view '%s'", view->name);
1466
1467	/*
1468	 * Check for a signature.  We log bad signatures regardless of
1469	 * whether they ultimately cause the request to be rejected or
1470	 * not.  We do not log the lack of a signature unless we are
1471	 * debugging.
1472	 */
1473	client->signer = NULL;
1474	dns_name_init(&client->signername, NULL);
1475	result = dns_message_signer(client->message, &client->signername);
1476	if (result == ISC_R_SUCCESS) {
1477		ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1478			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1479			      "request has valid signature");
1480		client->signer = &client->signername;
1481	} else if (result == ISC_R_NOTFOUND) {
1482		ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1483			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1484			      "request is not signed");
1485	} else if (result == DNS_R_NOIDENTITY) {
1486		ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1487			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1488			      "request is signed by a nonauthoritative key");
1489	} else {
1490		char tsigrcode[64];
1491		isc_buffer_t b;
1492		dns_name_t *name = NULL;
1493
1494		isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1);
1495		RUNTIME_CHECK(dns_tsigrcode_totext(client->message->tsigstatus,
1496						   &b) == ISC_R_SUCCESS);
1497		tsigrcode[isc_buffer_usedlength(&b)] = '\0';
1498		/* There is a signature, but it is bad. */
1499		if (dns_message_gettsig(client->message, &name) != NULL) {
1500			char namebuf[DNS_NAME_FORMATSIZE];
1501			dns_name_format(name, namebuf, sizeof(namebuf));
1502			ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1503				      NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
1504				      "request has invalid signature: "
1505				      "TSIG %s: %s (%s)", namebuf,
1506				      isc_result_totext(result), tsigrcode);
1507		} else {
1508			ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1509				      NS_LOGMODULE_CLIENT, ISC_LOG_ERROR,
1510				      "request has invalid signature: %s (%s)",
1511				      isc_result_totext(result), tsigrcode);
1512		}
1513		/*
1514		 * Accept update messages signed by unknown keys so that
1515		 * update forwarding works transparently through slaves
1516		 * that don't have all the same keys as the master.
1517		 */
1518		if (!(client->message->tsigstatus == dns_tsigerror_badkey &&
1519		      client->message->opcode == dns_opcode_update)) {
1520			ns_client_error(client, sigresult);
1521			goto cleanup;
1522		}
1523	}
1524
1525	/*
1526	 * Decide whether recursive service is available to this client.
1527	 * We do this here rather than in the query code so that we can
1528	 * set the RA bit correctly on all kinds of responses, not just
1529	 * responses to ordinary queries.
1530	 */
1531	ra = ISC_FALSE;
1532	if (client->view->resolver != NULL &&
1533	    client->view->recursion == ISC_TRUE &&
1534	    ns_client_checkaclsilent(client, client->view->recursionacl,
1535				     ISC_TRUE) == ISC_R_SUCCESS)
1536		ra = ISC_TRUE;
1537
1538	if (ra == ISC_TRUE)
1539		client->attributes |= NS_CLIENTATTR_RA;
1540
1541	ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT,
1542		      ISC_LOG_DEBUG(3), ra ? "recursion available" :
1543		      			     "recursion not available");
1544
1545	/*
1546	 * Dispatch the request.
1547	 */
1548	switch (client->message->opcode) {
1549	case dns_opcode_query:
1550		CTRACE("query");
1551		ns_query_start(client);
1552		break;
1553	case dns_opcode_update:
1554		CTRACE("update");
1555		ns_client_settimeout(client, 60);
1556		ns_update_start(client, sigresult);
1557		break;
1558	case dns_opcode_notify:
1559		CTRACE("notify");
1560		ns_client_settimeout(client, 60);
1561		ns_notify_start(client);
1562		break;
1563	case dns_opcode_iquery:
1564		CTRACE("iquery");
1565		ns_client_error(client, DNS_R_NOTIMP);
1566		break;
1567	default:
1568		CTRACE("unknown opcode");
1569		ns_client_error(client, DNS_R_NOTIMP);
1570	}
1571
1572 cleanup:
1573	return;
1574}
1575
1576static void
1577client_timeout(isc_task_t *task, isc_event_t *event) {
1578	ns_client_t *client;
1579
1580	REQUIRE(event != NULL);
1581	REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE ||
1582		event->ev_type == ISC_TIMEREVENT_IDLE);
1583	client = event->ev_arg;
1584	REQUIRE(NS_CLIENT_VALID(client));
1585	REQUIRE(task == client->task);
1586	REQUIRE(client->timer != NULL);
1587
1588	UNUSED(task);
1589
1590	CTRACE("timeout");
1591
1592	isc_event_free(&event);
1593
1594	if (client->shutdown != NULL) {
1595		(client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT);
1596		client->shutdown = NULL;
1597		client->shutdown_arg = NULL;
1598	}
1599
1600	if (client->newstate > NS_CLIENTSTATE_READY)
1601		client->newstate = NS_CLIENTSTATE_READY;
1602	(void)exit_check(client);
1603}
1604
1605static isc_result_t
1606client_create(ns_clientmgr_t *manager, ns_client_t **clientp)
1607{
1608	ns_client_t *client;
1609	isc_result_t result;
1610
1611	/*
1612	 * Caller must be holding the manager lock.
1613	 *
1614	 * Note: creating a client does not add the client to the
1615	 * manager's client list or set the client's manager pointer.
1616	 * The caller is responsible for that.
1617	 */
1618
1619	REQUIRE(clientp != NULL && *clientp == NULL);
1620
1621	client = isc_mem_get(manager->mctx, sizeof(*client));
1622	if (client == NULL)
1623		return (ISC_R_NOMEMORY);
1624
1625	client->task = NULL;
1626	result = isc_task_create(manager->taskmgr, 0, &client->task);
1627	if (result != ISC_R_SUCCESS)
1628		goto cleanup_client;
1629	isc_task_setname(client->task, "client", client);
1630
1631	client->timer = NULL;
1632	result = isc_timer_create(manager->timermgr, isc_timertype_inactive,
1633				  NULL, NULL, client->task, client_timeout,
1634				  client, &client->timer);
1635	if (result != ISC_R_SUCCESS)
1636		goto cleanup_task;
1637	client->timerset = ISC_FALSE;
1638
1639	client->message = NULL;
1640	result = dns_message_create(manager->mctx, DNS_MESSAGE_INTENTPARSE,
1641				    &client->message);
1642	if (result != ISC_R_SUCCESS)
1643		goto cleanup_timer;
1644
1645	/* XXXRTH  Hardwired constants */
1646
1647	client->sendevent = (isc_socketevent_t *)
1648			    isc_event_allocate(manager->mctx, client,
1649					       ISC_SOCKEVENT_SENDDONE,
1650					       client_senddone, client,
1651					       sizeof(isc_socketevent_t));
1652	if (client->sendevent == NULL) {
1653		result = ISC_R_NOMEMORY;
1654		goto cleanup_message;
1655	}
1656
1657	client->recvbuf = isc_mem_get(manager->mctx, RECV_BUFFER_SIZE);
1658	if  (client->recvbuf == NULL) {
1659		result = ISC_R_NOMEMORY;
1660		goto cleanup_sendevent;
1661	}
1662
1663	client->recvevent = (isc_socketevent_t *)
1664			    isc_event_allocate(manager->mctx, client,
1665					       ISC_SOCKEVENT_RECVDONE,
1666					       client_request, client,
1667					       sizeof(isc_socketevent_t));
1668	if (client->recvevent == NULL) {
1669		result = ISC_R_NOMEMORY;
1670		goto cleanup_recvbuf;
1671	}
1672
1673	client->magic = NS_CLIENT_MAGIC;
1674	client->mctx = manager->mctx;
1675	client->manager = NULL;
1676	client->state = NS_CLIENTSTATE_INACTIVE;
1677	client->newstate = NS_CLIENTSTATE_MAX;
1678	client->naccepts = 0;
1679	client->nreads = 0;
1680	client->nsends = 0;
1681	client->nrecvs = 0;
1682	client->nupdates = 0;
1683	client->nctls = 0;
1684	client->references = 0;
1685	client->attributes = 0;
1686	client->view = NULL;
1687	client->dispatch = NULL;
1688	client->udpsocket = NULL;
1689	client->tcplistener = NULL;
1690	client->tcpsocket = NULL;
1691	client->tcpmsg_valid = ISC_FALSE;
1692	client->tcpbuf = NULL;
1693	client->opt = NULL;
1694	client->udpsize = 512;
1695	client->extflags = 0;
1696	client->next = NULL;
1697	client->shutdown = NULL;
1698	client->shutdown_arg = NULL;
1699	dns_name_init(&client->signername, NULL);
1700	client->mortal = ISC_FALSE;
1701	client->tcpquota = NULL;
1702	client->recursionquota = NULL;
1703	client->interface = NULL;
1704	client->peeraddr_valid = ISC_FALSE;
1705	ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL,
1706		       NS_EVENT_CLIENTCONTROL, client_start, client, client,
1707		       NULL, NULL);
1708	/*
1709	 * Initialize FORMERR cache to sentinel value that will not match
1710	 * any actual FORMERR response.
1711	 */
1712	isc_sockaddr_any(&client->formerrcache.addr);
1713	client->formerrcache.time = 0;
1714	client->formerrcache.id = 0;
1715	ISC_LINK_INIT(client, link);
1716	client->list = NULL;
1717
1718	/*
1719	 * We call the init routines for the various kinds of client here,
1720	 * after we have created an otherwise valid client, because some
1721	 * of them call routines that REQUIRE(NS_CLIENT_VALID(client)).
1722	 */
1723	result = ns_query_init(client);
1724	if (result != ISC_R_SUCCESS)
1725		goto cleanup_recvevent;
1726
1727	result = isc_task_onshutdown(client->task, client_shutdown, client);
1728	if (result != ISC_R_SUCCESS)
1729		goto cleanup_query;
1730
1731	CTRACE("create");
1732
1733	*clientp = client;
1734
1735	return (ISC_R_SUCCESS);
1736
1737 cleanup_query:
1738	ns_query_free(client);
1739
1740 cleanup_recvevent:
1741	isc_event_free((isc_event_t **)&client->recvevent);
1742
1743 cleanup_recvbuf:
1744	isc_mem_put(manager->mctx, client->recvbuf, RECV_BUFFER_SIZE);
1745
1746 cleanup_sendevent:
1747	isc_event_free((isc_event_t **)&client->sendevent);
1748
1749	client->magic = 0;
1750
1751 cleanup_message:
1752	dns_message_destroy(&client->message);
1753
1754 cleanup_timer:
1755	isc_timer_detach(&client->timer);
1756
1757 cleanup_task:
1758	isc_task_detach(&client->task);
1759
1760 cleanup_client:
1761	isc_mem_put(manager->mctx, client, sizeof(*client));
1762
1763	return (result);
1764}
1765
1766static void
1767client_read(ns_client_t *client) {
1768	isc_result_t result;
1769
1770	CTRACE("read");
1771
1772	result = dns_tcpmsg_readmessage(&client->tcpmsg, client->task,
1773					client_request, client);
1774	if (result != ISC_R_SUCCESS)
1775		goto fail;
1776
1777	/*
1778	 * Set a timeout to limit the amount of time we will wait
1779	 * for a request on this TCP connection.
1780	 */
1781	ns_client_settimeout(client, 30);
1782
1783	client->state = client->newstate = NS_CLIENTSTATE_READING;
1784	INSIST(client->nreads == 0);
1785	INSIST(client->recursionquota == NULL);
1786	client->nreads++;
1787
1788	return;
1789 fail:
1790	ns_client_next(client, result);
1791}
1792
1793static void
1794client_newconn(isc_task_t *task, isc_event_t *event) {
1795	ns_client_t *client = event->ev_arg;
1796	isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
1797	isc_result_t result;
1798
1799	REQUIRE(event->ev_type == ISC_SOCKEVENT_NEWCONN);
1800	REQUIRE(NS_CLIENT_VALID(client));
1801	REQUIRE(client->task == task);
1802
1803	UNUSED(task);
1804
1805	INSIST(client->state == NS_CLIENTSTATE_READY);
1806
1807	INSIST(client->naccepts == 1);
1808	client->naccepts--;
1809
1810	LOCK(&client->interface->lock);
1811	INSIST(client->interface->ntcpcurrent > 0);
1812	client->interface->ntcpcurrent--;
1813	UNLOCK(&client->interface->lock);
1814
1815	/*
1816	 * We must take ownership of the new socket before the exit
1817	 * check to make sure it gets destroyed if we decide to exit.
1818	 */
1819	if (nevent->result == ISC_R_SUCCESS) {
1820		client->tcpsocket = nevent->newsocket;
1821		client->state = NS_CLIENTSTATE_READING;
1822		INSIST(client->recursionquota == NULL);
1823
1824		(void)isc_socket_getpeername(client->tcpsocket,
1825					     &client->peeraddr);
1826		client->peeraddr_valid = ISC_TRUE;
1827		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1828			   NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1829			   "new TCP connection");
1830	} else {
1831		/*
1832		 * XXXRTH  What should we do?  We're trying to accept but
1833		 *         it didn't work.  If we just give up, then TCP
1834		 *	   service may eventually stop.
1835		 *
1836		 *	   For now, we just go idle.
1837		 *
1838		 *	   Going idle is probably the right thing if the
1839		 *	   I/O was canceled.
1840		 */
1841		ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1842			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
1843			      "accept failed: %s",
1844			      isc_result_totext(nevent->result));
1845	}
1846
1847	if (exit_check(client))
1848		goto freeevent;
1849
1850	if (nevent->result == ISC_R_SUCCESS) {
1851		int match;
1852		isc_netaddr_t netaddr;
1853
1854		isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
1855
1856		if (ns_g_server->blackholeacl != NULL &&
1857		    dns_acl_match(&netaddr, NULL,
1858			    	  ns_g_server->blackholeacl,
1859				  &ns_g_server->aclenv,
1860				  &match, NULL) == ISC_R_SUCCESS &&
1861		    match > 0)
1862		{
1863			ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1864				      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1865				      "blackholed connection attempt");
1866			client->newstate = NS_CLIENTSTATE_READY;
1867			(void)exit_check(client);
1868			goto freeevent;
1869		}
1870
1871		INSIST(client->tcpmsg_valid == ISC_FALSE);
1872		dns_tcpmsg_init(client->mctx, client->tcpsocket,
1873				&client->tcpmsg);
1874		client->tcpmsg_valid = ISC_TRUE;
1875
1876		/*
1877		 * Let a new client take our place immediately, before
1878		 * we wait for a request packet.  If we don't,
1879		 * telnetting to port 53 (once per CPU) will
1880		 * deny service to legititmate TCP clients.
1881		 */
1882		result = isc_quota_attach(&ns_g_server->tcpquota,
1883					  &client->tcpquota);
1884		if (result == ISC_R_SUCCESS)
1885			result = ns_client_replace(client);
1886		if (result != ISC_R_SUCCESS) {
1887			ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1888				      NS_LOGMODULE_CLIENT, ISC_LOG_WARNING,
1889				      "no more TCP clients: %s",
1890				      isc_result_totext(result));
1891		}
1892
1893		client_read(client);
1894	}
1895
1896 freeevent:
1897	isc_event_free(&event);
1898}
1899
1900static void
1901client_accept(ns_client_t *client) {
1902	isc_result_t result;
1903
1904	CTRACE("accept");
1905
1906	result = isc_socket_accept(client->tcplistener, client->task,
1907				   client_newconn, client);
1908	if (result != ISC_R_SUCCESS) {
1909		UNEXPECTED_ERROR(__FILE__, __LINE__,
1910				 "isc_socket_accept() failed: %s",
1911				 isc_result_totext(result));
1912		/*
1913		 * XXXRTH  What should we do?  We're trying to accept but
1914		 *         it didn't work.  If we just give up, then TCP
1915		 *	   service may eventually stop.
1916		 *
1917		 *	   For now, we just go idle.
1918		 */
1919		return;
1920	}
1921	INSIST(client->naccepts == 0);
1922	client->naccepts++;
1923	LOCK(&client->interface->lock);
1924	client->interface->ntcpcurrent++;
1925	UNLOCK(&client->interface->lock);
1926}
1927
1928static void
1929client_udprecv(ns_client_t *client) {
1930	isc_result_t result;
1931	isc_region_t r;
1932
1933	CTRACE("udprecv");
1934
1935	r.base = client->recvbuf;
1936	r.length = RECV_BUFFER_SIZE;
1937	result = isc_socket_recv2(client->udpsocket, &r, 1,
1938				  client->task, client->recvevent, 0);
1939	if (result != ISC_R_SUCCESS) {
1940		UNEXPECTED_ERROR(__FILE__, __LINE__,
1941				 "isc_socket_recv() failed: %s",
1942				 isc_result_totext(result));
1943		/*
1944		 * This cannot happen in the current implementation, since
1945		 * isc_socket_recv2() cannot fail if flags == 0.
1946		 *
1947		 * If this does fail, we just go idle.
1948		 */
1949		return;
1950	}
1951	INSIST(client->nrecvs == 0);
1952	client->nrecvs++;
1953}
1954
1955void
1956ns_client_attach(ns_client_t *source, ns_client_t **targetp) {
1957	REQUIRE(NS_CLIENT_VALID(source));
1958	REQUIRE(targetp != NULL && *targetp == NULL);
1959
1960	source->references++;
1961	ns_client_log(source, NS_LOGCATEGORY_CLIENT,
1962		      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1963		      "ns_client_attach: ref = %d", source->references);
1964	*targetp = source;
1965}
1966
1967void
1968ns_client_detach(ns_client_t **clientp) {
1969	ns_client_t *client = *clientp;
1970
1971	client->references--;
1972	INSIST(client->references >= 0);
1973	*clientp = NULL;
1974	ns_client_log(client, NS_LOGCATEGORY_CLIENT,
1975		      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(10),
1976		      "ns_client_detach: ref = %d", client->references);
1977	(void)exit_check(client);
1978}
1979
1980isc_boolean_t
1981ns_client_shuttingdown(ns_client_t *client) {
1982	return (ISC_TF(client->newstate == NS_CLIENTSTATE_FREED));
1983}
1984
1985isc_result_t
1986ns_client_replace(ns_client_t *client) {
1987	isc_result_t result;
1988
1989	CTRACE("replace");
1990
1991	result = ns_clientmgr_createclients(client->manager,
1992					    1, client->interface,
1993					    (TCP_CLIENT(client) ?
1994					     ISC_TRUE : ISC_FALSE));
1995	if (result != ISC_R_SUCCESS)
1996		return (result);
1997
1998	/*
1999	 * The responsibility for listening for new requests is hereby
2000	 * transferred to the new client.  Therefore, the old client
2001	 * should refrain from listening for any more requests.
2002	 */
2003	client->mortal = ISC_TRUE;
2004
2005	return (ISC_R_SUCCESS);
2006}
2007
2008/***
2009 *** Client Manager
2010 ***/
2011
2012static void
2013clientmgr_destroy(ns_clientmgr_t *manager) {
2014	REQUIRE(ISC_LIST_EMPTY(manager->active));
2015	REQUIRE(ISC_LIST_EMPTY(manager->inactive));
2016	REQUIRE(ISC_LIST_EMPTY(manager->recursing));
2017
2018	MTRACE("clientmgr_destroy");
2019
2020	DESTROYLOCK(&manager->lock);
2021	manager->magic = 0;
2022	isc_mem_put(manager->mctx, manager, sizeof(*manager));
2023}
2024
2025isc_result_t
2026ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
2027		    isc_timermgr_t *timermgr, ns_clientmgr_t **managerp)
2028{
2029	ns_clientmgr_t *manager;
2030	isc_result_t result;
2031
2032	manager = isc_mem_get(mctx, sizeof(*manager));
2033	if (manager == NULL)
2034		return (ISC_R_NOMEMORY);
2035
2036	result = isc_mutex_init(&manager->lock);
2037	if (result != ISC_R_SUCCESS)
2038		goto cleanup_manager;
2039
2040	manager->mctx = mctx;
2041	manager->taskmgr = taskmgr;
2042	manager->timermgr = timermgr;
2043	manager->exiting = ISC_FALSE;
2044	ISC_LIST_INIT(manager->active);
2045	ISC_LIST_INIT(manager->inactive);
2046	ISC_LIST_INIT(manager->recursing);
2047	manager->magic = MANAGER_MAGIC;
2048
2049	MTRACE("create");
2050
2051	*managerp = manager;
2052
2053	return (ISC_R_SUCCESS);
2054
2055 cleanup_manager:
2056	isc_mem_put(manager->mctx, manager, sizeof(*manager));
2057
2058	return (result);
2059}
2060
2061void
2062ns_clientmgr_destroy(ns_clientmgr_t **managerp) {
2063	ns_clientmgr_t *manager;
2064	ns_client_t *client;
2065	isc_boolean_t need_destroy = ISC_FALSE;
2066
2067	REQUIRE(managerp != NULL);
2068	manager = *managerp;
2069	REQUIRE(VALID_MANAGER(manager));
2070
2071	MTRACE("destroy");
2072
2073	LOCK(&manager->lock);
2074
2075	manager->exiting = ISC_TRUE;
2076
2077	for (client = ISC_LIST_HEAD(manager->recursing);
2078	     client != NULL;
2079	     client = ISC_LIST_NEXT(client, link))
2080		isc_task_shutdown(client->task);
2081
2082	for (client = ISC_LIST_HEAD(manager->active);
2083	     client != NULL;
2084	     client = ISC_LIST_NEXT(client, link))
2085		isc_task_shutdown(client->task);
2086
2087	for (client = ISC_LIST_HEAD(manager->inactive);
2088	     client != NULL;
2089	     client = ISC_LIST_NEXT(client, link))
2090		isc_task_shutdown(client->task);
2091
2092	if (ISC_LIST_EMPTY(manager->active) &&
2093	    ISC_LIST_EMPTY(manager->inactive) &&
2094	    ISC_LIST_EMPTY(manager->recursing))
2095		need_destroy = ISC_TRUE;
2096
2097	UNLOCK(&manager->lock);
2098
2099	if (need_destroy)
2100		clientmgr_destroy(manager);
2101
2102	*managerp = NULL;
2103}
2104
2105isc_result_t
2106ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
2107			   ns_interface_t *ifp, isc_boolean_t tcp)
2108{
2109	isc_result_t result = ISC_R_SUCCESS;
2110	unsigned int i;
2111	ns_client_t *client;
2112
2113	REQUIRE(VALID_MANAGER(manager));
2114	REQUIRE(n > 0);
2115
2116	MTRACE("createclients");
2117
2118	/*
2119	 * We MUST lock the manager lock for the entire client creation
2120	 * process.  If we didn't do this, then a client could get a
2121	 * shutdown event and disappear out from under us.
2122	 */
2123
2124	LOCK(&manager->lock);
2125
2126	for (i = 0; i < n; i++) {
2127		isc_event_t *ev;
2128		/*
2129		 * Allocate a client.  First try to get a recycled one;
2130		 * if that fails, make a new one.
2131		 */
2132		client = ISC_LIST_HEAD(manager->inactive);
2133		if (client != NULL) {
2134			MTRACE("recycle");
2135			ISC_LIST_UNLINK(manager->inactive, client, link);
2136			client->list = NULL;
2137		} else {
2138			MTRACE("create new");
2139			result = client_create(manager, &client);
2140			if (result != ISC_R_SUCCESS)
2141				break;
2142		}
2143
2144		ns_interface_attach(ifp, &client->interface);
2145		client->state = NS_CLIENTSTATE_READY;
2146		INSIST(client->recursionquota == NULL);
2147
2148		if (tcp) {
2149			client->attributes |= NS_CLIENTATTR_TCP;
2150			isc_socket_attach(ifp->tcpsocket,
2151					  &client->tcplistener);
2152		} else {
2153			isc_socket_t *sock;
2154
2155			dns_dispatch_attach(ifp->udpdispatch,
2156					    &client->dispatch);
2157			sock = dns_dispatch_getsocket(client->dispatch);
2158			isc_socket_attach(sock, &client->udpsocket);
2159		}
2160		client->manager = manager;
2161		ISC_LIST_APPEND(manager->active, client, link);
2162		client->list = &manager->active;
2163
2164		INSIST(client->nctls == 0);
2165		client->nctls++;
2166		ev = &client->ctlevent;
2167		isc_task_send(client->task, &ev);
2168	}
2169	if (i != 0) {
2170		/*
2171		 * We managed to create at least one client, so we
2172		 * declare victory.
2173		 */
2174		result = ISC_R_SUCCESS;
2175	}
2176
2177	UNLOCK(&manager->lock);
2178
2179	return (result);
2180}
2181
2182isc_sockaddr_t *
2183ns_client_getsockaddr(ns_client_t *client) {
2184	return (&client->peeraddr);
2185}
2186
2187isc_result_t
2188ns_client_checkaclsilent(ns_client_t *client, dns_acl_t *acl,
2189			 isc_boolean_t default_allow)
2190{
2191	isc_result_t result;
2192	int match;
2193	isc_netaddr_t netaddr;
2194
2195	if (acl == NULL) {
2196		if (default_allow)
2197			goto allow;
2198		else
2199			goto deny;
2200	}
2201
2202	isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
2203
2204	result = dns_acl_match(&netaddr, client->signer, acl,
2205			       &ns_g_server->aclenv,
2206			       &match, NULL);
2207	if (result != ISC_R_SUCCESS)
2208		goto deny; /* Internal error, already logged. */
2209	if (match > 0)
2210		goto allow;
2211	goto deny; /* Negative match or no match. */
2212
2213 allow:
2214	return (ISC_R_SUCCESS);
2215
2216 deny:
2217	return (DNS_R_REFUSED);
2218}
2219
2220isc_result_t
2221ns_client_checkacl(ns_client_t *client,
2222		   const char *opname, dns_acl_t *acl,
2223		   isc_boolean_t default_allow, int log_level)
2224{
2225	isc_result_t result =
2226		ns_client_checkaclsilent(client, acl, default_allow);
2227
2228	if (result == ISC_R_SUCCESS)
2229		ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2230			      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3),
2231			      "%s approved", opname);
2232	else
2233		ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
2234			      NS_LOGMODULE_CLIENT,
2235			      log_level, "%s denied", opname);
2236	return (result);
2237}
2238
2239static void
2240ns_client_name(ns_client_t *client, char *peerbuf, size_t len) {
2241	if (client->peeraddr_valid)
2242		isc_sockaddr_format(&client->peeraddr, peerbuf, len);
2243	else
2244		snprintf(peerbuf, len, "@%p", client);
2245}
2246
2247void
2248ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
2249	   isc_logmodule_t *module, int level, const char *fmt, va_list ap)
2250{
2251	char msgbuf[2048];
2252	char peerbuf[ISC_SOCKADDR_FORMATSIZE];
2253	const char *name = "";
2254	const char *sep = "";
2255
2256	vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
2257	ns_client_name(client, peerbuf, sizeof(peerbuf));
2258	if (client->view != NULL && strcmp(client->view->name, "_bind") != 0 &&
2259	    strcmp(client->view->name, "_default") != 0) {
2260		name = client->view->name;
2261		sep = ": view ";
2262	}
2263
2264	isc_log_write(ns_g_lctx, category, module, level,
2265		      "client %s%s%s: %s", peerbuf, sep, name, msgbuf);
2266}
2267
2268void
2269ns_client_log(ns_client_t *client, isc_logcategory_t *category,
2270	   isc_logmodule_t *module, int level, const char *fmt, ...)
2271{
2272	va_list ap;
2273
2274	if (! isc_log_wouldlog(ns_g_lctx, level))
2275		return;
2276
2277	va_start(ap, fmt);
2278	ns_client_logv(client, category, module, level, fmt, ap);
2279	va_end(ap);
2280}
2281
2282void
2283ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdatatype_t type,
2284		 dns_rdataclass_t rdclass, char *buf, size_t len)
2285{
2286        char namebuf[DNS_NAME_FORMATSIZE];
2287        char typebuf[DNS_RDATATYPE_FORMATSIZE];
2288        char classbuf[DNS_RDATACLASS_FORMATSIZE];
2289
2290        dns_name_format(name, namebuf, sizeof(namebuf));
2291        dns_rdatatype_format(type, typebuf, sizeof(typebuf));
2292        dns_rdataclass_format(rdclass, classbuf, sizeof(classbuf));
2293        (void)snprintf(buf, len, "%s '%s/%s/%s'", msg, namebuf, typebuf,
2294		       classbuf);
2295}
2296
2297static void
2298ns_client_dumpmessage(ns_client_t *client, const char *reason) {
2299	isc_buffer_t buffer;
2300	char *buf = NULL;
2301	int len = 1024;
2302	isc_result_t result;
2303
2304	/*
2305	 * Note that these are multiline debug messages.  We want a newline
2306	 * to appear in the log after each message.
2307	 */
2308
2309	do {
2310		buf = isc_mem_get(client->mctx, len);
2311		if (buf == NULL)
2312			break;
2313		isc_buffer_init(&buffer, buf, len);
2314		result = dns_message_totext(client->message,
2315					    &dns_master_style_debug,
2316					    0, &buffer);
2317		if (result == ISC_R_NOSPACE) {
2318			isc_mem_put(client->mctx, buf, len);
2319			len += 1024;
2320		} else if (result == ISC_R_SUCCESS)
2321		        ns_client_log(client, NS_LOGCATEGORY_UNMATCHED,
2322				      NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1),
2323				      "%s\n%.*s", reason,
2324				       (int)isc_buffer_usedlength(&buffer),
2325				       buf);
2326	} while (result == ISC_R_NOSPACE);
2327
2328	if (buf != NULL)
2329		isc_mem_put(client->mctx, buf, len);
2330}
2331
2332void
2333ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager) {
2334	ns_client_t *client;
2335	char namebuf[DNS_NAME_FORMATSIZE];
2336	char peerbuf[ISC_SOCKADDR_FORMATSIZE];
2337	const char *name;
2338	const char *sep;
2339
2340	REQUIRE(VALID_MANAGER(manager));
2341
2342	LOCK(&manager->lock);
2343	client = ISC_LIST_HEAD(manager->recursing);
2344	while (client != NULL) {
2345		ns_client_name(client, peerbuf, sizeof(peerbuf));
2346		if (client->view != NULL &&
2347		    strcmp(client->view->name, "_bind") != 0 &&
2348		    strcmp(client->view->name, "_default") != 0) {
2349			name = client->view->name;
2350			sep = ": view ";
2351		} else {
2352			name = "";
2353			sep = "";
2354		}
2355		dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
2356		fprintf(f, "; client %s%s%s: '%s' requesttime %d\n",
2357			peerbuf, sep, name, namebuf, client->requesttime);
2358		client = ISC_LIST_NEXT(client, link);
2359	}
2360	UNLOCK(&manager->lock);
2361}
2362