client.h 11.5 KB
Newer Older
Bob Halley's avatar
add  
Bob Halley committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004-2009, 2011-2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Bob Halley's avatar
add  
Bob Halley committed
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Bob Halley's avatar
add  
Bob Halley committed
16 17
 */

Automatic Updater's avatar
Automatic Updater committed
18
/* $Id: client.h,v 1.96 2012/01/31 23:47:31 tbox Exp $ */
David Lawrence's avatar
David Lawrence committed
19

20 21
#ifndef NAMED_CLIENT_H
#define NAMED_CLIENT_H 1
Bob Halley's avatar
add  
Bob Halley committed
22

23 24 25 26
/*****
 ***** Module Info
 *****/

Automatic Updater's avatar
Automatic Updater committed
27
/*! \file
28
 * \brief
29 30
 * This module defines two objects, ns_client_t and ns_clientmgr_t.
 *
Andreas Gustafsson's avatar
Andreas Gustafsson committed
31 32
 * An ns_client_t object handles incoming DNS requests from clients
 * on a given network interface.
33 34 35
 *
 * Each ns_client_t object can handle only one TCP connection or UDP
 * request at a time.  Therefore, several ns_client_t objects are
Andreas Gustafsson's avatar
Andreas Gustafsson committed
36
 * typically created to serve each network interface, e.g., one
37
 * for handling TCP requests and a few (one per CPU) for handling
Andreas Gustafsson's avatar
Andreas Gustafsson committed
38
 * UDP requests.
39 40
 *
 * Incoming requests are classified as queries, zone transfer
41
 * requests, update requests, notify requests, etc, and handed off
42
 * to the appropriate request handler.  When the request has been
43 44
 * fully handled (which can be much later), the ns_client_t must be
 * notified of this by calling one of the following functions
45
 * exactly once in the context of its task:
46
 * \code
47
 *   ns_client_send()	(sending a non-error response)
48
 *   ns_client_sendraw() (sending a raw response)
49 50
 *   ns_client_error()	(sending an error response)
 *   ns_client_next()	(sending no response)
51
 *\endcode
52
 * This will release any resources used by the request and
53 54 55 56
 * and allow the ns_client_t to listen for the next request.
 *
 * A ns_clientmgr_t manages a number of ns_client_t objects.
 * New ns_client_t objects are created by calling
57 58
 * ns_clientmgr_createclients(). They are destroyed by
 * destroying their manager.
59 60 61 62 63 64
 */

/***
 *** Imports
 ***/

Bob Halley's avatar
add  
Bob Halley committed
65
#include <isc/buffer.h>
66
#include <isc/magic.h>
67
#include <isc/stdtime.h>
68
#include <isc/quota.h>
69
#include <isc/queue.h>
Bob Halley's avatar
add  
Bob Halley committed
70

71
#include <dns/db.h>
72
#include <dns/fixedname.h>
73
#include <dns/name.h>
74 75
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
Bob Halley's avatar
Bob Halley committed
76
#include <dns/tcpmsg.h>
77 78
#include <dns/types.h>

Bob Halley's avatar
add  
Bob Halley committed
79
#include <named/types.h>
Bob Halley's avatar
Bob Halley committed
80
#include <named/query.h>
Bob Halley's avatar
add  
Bob Halley committed
81

82 83 84
/***
 *** Types
 ***/
Bob Halley's avatar
add  
Bob Halley committed
85

86
/*% nameserver client structure */
Bob Halley's avatar
add  
Bob Halley committed
87
struct ns_client {
88 89 90 91 92 93 94 95
	unsigned int		magic;
	isc_mem_t *		mctx;
	ns_clientmgr_t *	manager;
	int			state;
	int			newstate;
	int			naccepts;
	int			nreads;
	int			nsends;
96
	int			nrecvs;
97
	int			nupdates;
98
	int			nctls;
99
	int			references;
100 101 102 103 104 105 106
	isc_boolean_t		needshutdown; 	/*
						 * Used by clienttest to get
						 * the client to go from
						 * inactive to free state
						 * by shutting down the
						 * client's task.
						 */
107 108 109 110
	unsigned int		attributes;
	isc_task_t *		task;
	dns_view_t *		view;
	dns_dispatch_t *	dispatch;
111
	isc_socket_t *		udpsocket;
112 113
	isc_socket_t *		tcplistener;
	isc_socket_t *		tcpsocket;
114
	unsigned char *		tcpbuf;
115 116 117
	dns_tcpmsg_t		tcpmsg;
	isc_boolean_t		tcpmsg_valid;
	isc_timer_t *		timer;
118
	isc_timer_t *		delaytimer;
119
	isc_boolean_t 		timerset;
120
	dns_message_t *		message;
121 122
	isc_socketevent_t *	sendevent;
	isc_socketevent_t *	recvevent;
123
	unsigned char *		recvbuf;
124 125
	dns_rdataset_t *	opt;
	isc_uint16_t		udpsize;
126
	isc_uint16_t		extflags;
127
	isc_int16_t		ednsversion;	/* -1 noedns */
128 129 130 131 132 133
	void			(*next)(ns_client_t *);
	void			(*shutdown)(void *arg, isc_result_t result);
	void 			*shutdown_arg;
	ns_query_t		query;
	isc_stdtime_t		requesttime;
	isc_stdtime_t		now;
134 135 136
	dns_name_t		signername;   /*%< [T]SIG key name */
	dns_name_t *		signer;	      /*%< NULL if not valid sig */
	isc_boolean_t		mortal;	      /*%< Die after handling request */
137 138 139 140 141
	isc_quota_t		*tcpquota;
	isc_quota_t		*recursionquota;
	ns_interface_t		*interface;
	isc_sockaddr_t		peeraddr;
	isc_boolean_t		peeraddr_valid;
142
	isc_netaddr_t		destaddr;
143
	struct in6_pktinfo	pktinfo;
Evan Hunt's avatar
Evan Hunt committed
144
	isc_dscp_t		dscp;
145
	isc_event_t		ctlevent;
Evan Hunt's avatar
Evan Hunt committed
146 147
#ifdef ALLOW_FILTER_AAAA
	dns_aaaa_t		filter_aaaa;
148
#endif
149
	/*%
150 151 152 153 154 155 156 157 158 159
	 * Information about recent FORMERR response(s), for
	 * FORMERR loop avoidance.  This is separate for each
	 * client object rather than global only to avoid
	 * the need for locking.
	 */
	struct {
		isc_sockaddr_t		addr;
		isc_stdtime_t		time;
		dns_messageid_t		id;
	} formerrcache;
160

161
	ISC_LINK(ns_client_t)	link;
162 163
	ISC_LINK(ns_client_t)	rlink;
	ISC_QLINK(ns_client_t)	ilink;
164
	unsigned char		cookie[8];
165
	isc_uint32_t		expire;
Bob Halley's avatar
add  
Bob Halley committed
166 167
};

168 169 170
typedef ISC_QUEUE(ns_client_t) client_queue_t;
typedef ISC_LIST(ns_client_t) client_list_t;

171
#define NS_CLIENT_MAGIC			ISC_MAGIC('N','S','C','c')
172
#define NS_CLIENT_VALID(c)		ISC_MAGIC_VALID(c, NS_CLIENT_MAGIC)
Bob Halley's avatar
add  
Bob Halley committed
173

174 175 176 177 178 179
#define NS_CLIENTATTR_TCP		0x0001
#define NS_CLIENTATTR_RA		0x0002 /*%< Client gets recursive service */
#define NS_CLIENTATTR_PKTINFO		0x0004 /*%< pktinfo is valid */
#define NS_CLIENTATTR_MULTICAST		0x0008 /*%< recv'd from multicast */
#define NS_CLIENTATTR_WANTDNSSEC	0x0010 /*%< include dnssec records */
#define NS_CLIENTATTR_WANTNSID          0x0020 /*%< include nameserver ID */
Evan Hunt's avatar
Evan Hunt committed
180
#ifdef ALLOW_FILTER_AAAA
181 182
#define NS_CLIENTATTR_FILTER_AAAA	0x0040 /*%< suppress AAAAs */
#define NS_CLIENTATTR_FILTER_AAAA_RC	0x0080 /*%< recursing for A against AAAA */
183
#endif
184 185 186 187 188 189
#define NS_CLIENTATTR_WANTAD		0x0100 /*%< want AD in response if possible */
#define NS_CLIENTATTR_WANTSIT		0x0200 /*%< include SIT */
#define NS_CLIENTATTR_HAVESIT		0x0400 /*%< has a valid SIT */
#define NS_CLIENTATTR_WANTEXPIRE	0x0800 /*%< return seconds to expire */
#define NS_CLIENTATTR_HAVEEXPIRE	0x1000 /*%< return seconds to expire */
#define NS_CLIENTATTR_WANTOPT		0x2000 /*%< add opt to reply */
190

191
extern unsigned int ns_client_requests;
Bob Halley's avatar
Bob Halley committed
192

193 194 195 196
/***
 *** Functions
 ***/

197
/*%
Bob Halley's avatar
add  
Bob Halley committed
198 199 200
 * Note!  These ns_client_ routines MUST be called ONLY from the client's
 * task in order to ensure synchronization.
 */
Andreas Gustafsson's avatar
Andreas Gustafsson committed
201

Bob Halley's avatar
add  
Bob Halley committed
202
void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
203
ns_client_send(ns_client_t *client);
204
/*%
Andreas Gustafsson's avatar
Andreas Gustafsson committed
205 206
 * Finish processing the current client request and
 * send client->message as a response.
207 208 209
 * \brief
 * Note!  These ns_client_ routines MUST be called ONLY from the client's
 * task in order to ensure synchronization.
Andreas Gustafsson's avatar
Andreas Gustafsson committed
210
 */
Bob Halley's avatar
add  
Bob Halley committed
211

212 213
void
ns_client_sendraw(ns_client_t *client, dns_message_t *msg);
214
/*%
215
 * Finish processing the current client request and
216
 * send msg as a response using client->message->id for the id.
217 218
 */

Bob Halley's avatar
add  
Bob Halley committed
219
void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
220
ns_client_error(ns_client_t *client, isc_result_t result);
221
/*%
Andreas Gustafsson's avatar
Andreas Gustafsson committed
222 223 224 225
 * Finish processing the current client request and return
 * an error response to the client.  The error response
 * will have an RCODE determined by 'result'.
 */
Bob Halley's avatar
add  
Bob Halley committed
226

Bob Halley's avatar
Bob Halley committed
227
void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
228
ns_client_next(ns_client_t *client, isc_result_t result);
229
/*%
230
 * Finish processing the current client request,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
231 232
 * return no response to the client.
 */
Bob Halley's avatar
Bob Halley committed
233

234 235
isc_boolean_t
ns_client_shuttingdown(ns_client_t *client);
236
/*%
237 238 239
 * Return ISC_TRUE iff the client is currently shutting down.
 */

240
void
241
ns_client_attach(ns_client_t *source, ns_client_t **target);
242
/*%
243
 * Attach '*targetp' to 'source'.
244
 */
245

246
void
247
ns_client_detach(ns_client_t **clientp);
248
/*%
249
 * Detach '*clientp' from its client.
250 251 252
 */

isc_result_t
253
ns_client_replace(ns_client_t *client);
254
/*%
255 256 257 258
 * Try to replace the current client with a new one, so that the
 * current one can go off and do some lengthy work without
 * leaving the dispatch/socket without service.
 */
259

260 261
void
ns_client_settimeout(ns_client_t *client, unsigned int seconds);
262
/*%
263 264 265
 * Set a timer in the client to go off in the specified amount of time.
 */

Bob Halley's avatar
add  
Bob Halley committed
266 267 268
isc_result_t
ns_clientmgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
		    isc_timermgr_t *timermgr, ns_clientmgr_t **managerp);
269
/*%
Andreas Gustafsson's avatar
Andreas Gustafsson committed
270 271
 * Create a client manager.
 */
Bob Halley's avatar
add  
Bob Halley committed
272 273 274

void
ns_clientmgr_destroy(ns_clientmgr_t **managerp);
275
/*%
Andreas Gustafsson's avatar
Andreas Gustafsson committed
276 277 278
 * Destroy a client manager and all ns_client_t objects
 * managed by it.
 */
Bob Halley's avatar
add  
Bob Halley committed
279 280

isc_result_t
281 282
ns_clientmgr_createclients(ns_clientmgr_t *manager, unsigned int n,
			   ns_interface_t *ifp, isc_boolean_t tcp);
283
/*%
284 285 286
 * Create up to 'n' clients listening on interface 'ifp'.
 * If 'tcp' is ISC_TRUE, the clients will listen for TCP connections,
 * otherwise for UDP requests.
287
 */
Bob Halley's avatar
Bob Halley committed
288

289 290
isc_sockaddr_t *
ns_client_getsockaddr(ns_client_t *client);
291
/*%
Andreas Gustafsson's avatar
Andreas Gustafsson committed
292 293 294
 * Get the socket address of the client whose request is
 * currently being processed.
 */
295

296
isc_result_t
297 298
ns_client_checkaclsilent(ns_client_t *client, isc_netaddr_t *netaddr,
			 dns_acl_t *acl, isc_boolean_t default_allow);
299

300
/*%
301 302 303 304
 * Convenience function for client request ACL checking.
 *
 * Check the current client request against 'acl'.  If 'acl'
 * is NULL, allow the request iff 'default_allow' is ISC_TRUE.
305 306
 * If netaddr is NULL, check the ACL against client->peeraddr;
 * otherwise check it against netaddr.
307 308
 *
 * Notes:
309
 *\li	This is appropriate for checking allow-update,
310 311 312 313 314 315
 * 	allow-query, allow-transfer, etc.  It is not appropriate
 * 	for checking the blackhole list because we treat positive
 * 	matches as "allow" and negative matches as "deny"; in
 *	the case of the blackhole list this would be backwards.
 *
 * Requires:
316
 *\li	'client' points to a valid client.
317
 *\li	'netaddr' points to a valid address, or is NULL.
318
 *\li	'acl' points to a valid ACL, or is NULL.
319 320
 *
 * Returns:
321
 *\li	ISC_R_SUCCESS	if the request should be allowed
Francis Dupont's avatar
Francis Dupont committed
322
 * \li	DNS_R_REFUSED	if the request should be denied
323
 *\li	No other return values are possible.
324 325
 */

326 327
isc_result_t
ns_client_checkacl(ns_client_t  *client,
328
		   isc_sockaddr_t *sockaddr,
329 330 331
		   const char *opname, dns_acl_t *acl,
		   isc_boolean_t default_allow,
		   int log_level);
332
/*%
333 334 335
 * Like ns_client_checkaclsilent, except the outcome of the check is
 * logged at log level 'log_level' if denied, and at debug 3 if approved.
 * Log messages will refer to the request as an 'opname' request.
336 337
 *
 * Requires:
338 339 340
 *\li	'client' points to a valid client.
 *\li	'sockaddr' points to a valid address, or is NULL.
 *\li	'acl' points to a valid ACL, or is NULL.
341
 *\li	'opname' points to a null-terminated string.
342 343
 */

Andreas Gustafsson's avatar
Andreas Gustafsson committed
344 345 346
void
ns_client_log(ns_client_t *client, isc_logcategory_t *category,
	      isc_logmodule_t *module, int level,
347
	      const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
348

349 350 351 352
void
ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
	       isc_logmodule_t *module, int level, const char *fmt, va_list ap) ISC_FORMAT_PRINTF(5, 0);

353
void
354 355 356 357 358 359
ns_client_aclmsg(const char *msg, dns_name_t *name, dns_rdatatype_t type,
		 dns_rdataclass_t rdclass, char *buf, size_t len);

#define NS_CLIENT_ACLMSGSIZE(x) \
	(DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE + \
	 DNS_RDATACLASS_FORMATSIZE + sizeof(x) + sizeof("'/'"))
360

361
void
362
ns_client_recursing(ns_client_t *client);
363
/*%
364 365 366 367 368 369 370
 * Add client to end of th recursing list.
 */

void
ns_client_killoldestquery(ns_client_t *client);
/*%
 * Kill the oldest recursive query (recursing list head).
371 372
 */

373 374
void
ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager);
375
/*%
376 377 378
 * Dump the outstanding recursive queries to 'f'.
 */

379 380 381 382 383 384
void
ns_client_qnamereplace(ns_client_t *client, dns_name_t *name);
/*%
 * Replace the qname.
 */

385 386
isc_boolean_t
ns_client_isself(dns_view_t *myview, dns_tsigkey_t *mykey,
Automatic Updater's avatar
Automatic Updater committed
387 388
		 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
		 dns_rdataclass_t rdclass, void *arg);
Mark Andrews's avatar
Mark Andrews committed
389
/*%
390 391 392
 * Isself callback.
 */

393 394 395
isc_result_t
ns_client_sourceip(dns_clientinfo_t *ci, isc_sockaddr_t **addrp);

396 397 398 399
isc_result_t
ns_client_addopt(ns_client_t *client, dns_message_t *message,
                 dns_rdataset_t **opt);

400
#endif /* NAMED_CLIENT_H */