1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * SPDX-License-Identifier: MPL-2.0
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #ifndef NS_SERVER_H
15 #define NS_SERVER_H 1
16 
17 /*! \file */
18 
19 #include <inttypes.h>
20 #include <stdbool.h>
21 
22 #include <isc/fuzz.h>
23 #include <isc/log.h>
24 #include <isc/magic.h>
25 #include <isc/quota.h>
26 #include <isc/random.h>
27 #include <isc/sockaddr.h>
28 #include <isc/types.h>
29 
30 #include <dns/acl.h>
31 #include <dns/types.h>
32 
33 #include <ns/types.h>
34 
35 #define NS_EVENT_CLIENTCONTROL (ISC_EVENTCLASS_NS + 0)
36 
37 #define NS_SERVER_LOGQUERIES   0x00000001U /*%< log queries */
38 #define NS_SERVER_NOAA	       0x00000002U /*%< -T noaa */
39 #define NS_SERVER_NOSOA	       0x00000004U /*%< -T nosoa */
40 #define NS_SERVER_NONEAREST    0x00000008U /*%< -T nonearest */
41 #define NS_SERVER_NOEDNS       0x00000020U /*%< -T noedns */
42 #define NS_SERVER_DROPEDNS     0x00000040U /*%< -T dropedns */
43 #define NS_SERVER_NOTCP	       0x00000080U /*%< -T notcp */
44 #define NS_SERVER_DISABLE4     0x00000100U /*%< -6 */
45 #define NS_SERVER_DISABLE6     0x00000200U /*%< -4 */
46 #define NS_SERVER_FIXEDLOCAL   0x00000400U /*%< -T fixedlocal */
47 #define NS_SERVER_SIGVALINSECS 0x00000800U /*%< -T sigvalinsecs */
48 #define NS_SERVER_EDNSFORMERR  0x00001000U /*%< -T ednsformerr (STD13) */
49 #define NS_SERVER_EDNSNOTIMP   0x00002000U /*%< -T ednsnotimp */
50 #define NS_SERVER_EDNSREFUSED  0x00004000U /*%< -T ednsrefused */
51 
52 /*%
53  * Type for callback function to get hostname.
54  */
55 typedef isc_result_t (*ns_hostnamecb_t)(char *buf, size_t len);
56 
57 /*%
58  * Type for callback function to signal the fuzzer thread
59  * when built with AFL.
60  */
61 typedef void (*ns_fuzzcb_t)(void);
62 
63 /*%
64  * Type for callback function to get the view that can answer a query.
65  */
66 typedef isc_result_t (*ns_matchview_t)(
67 	isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr, dns_message_t *message,
68 	dns_aclenv_t *env, isc_result_t *sigresultp, dns_view_t **viewp);
69 
70 /*%
71  * Server context.
72  */
73 struct ns_server {
74 	unsigned int magic;
75 	isc_mem_t	  *mctx;
76 
77 	isc_refcount_t references;
78 
79 	/*% Server cookie secret and algorithm */
80 	unsigned char	   secret[32];
81 	ns_cookiealg_t	   cookiealg;
82 	ns_altsecretlist_t altsecrets;
83 	bool		   answercookie;
84 
85 	/*% Quotas */
86 	isc_quota_t recursionquota;
87 	isc_quota_t tcpquota;
88 	isc_quota_t xfroutquota;
89 
90 	/*% Test options and other configurables */
91 	uint32_t options;
92 
93 	dns_acl_t	  *blackholeacl;
94 	dns_acl_t	  *keepresporder;
95 	uint16_t       udpsize;
96 	uint16_t       transfer_tcp_message_size;
97 	bool	       interface_auto;
98 	dns_tkeyctx_t *tkeyctx;
99 
100 	/*% Server id for NSID */
101 	char	     *server_id;
102 	ns_hostnamecb_t gethostname;
103 
104 	/*% Fuzzer callback */
105 	isc_fuzztype_t fuzztype;
106 	ns_fuzzcb_t    fuzznotify;
107 
108 	/*% Callback to find a matching view for a query */
109 	ns_matchview_t matchingview;
110 
111 	/*% Stats counters */
112 	ns_stats_t  *nsstats;
113 	dns_stats_t *rcvquerystats;
114 	dns_stats_t *opcodestats;
115 	dns_stats_t *rcodestats;
116 
117 	isc_stats_t *udpinstats4;
118 	isc_stats_t *udpoutstats4;
119 	isc_stats_t *udpinstats6;
120 	isc_stats_t *udpoutstats6;
121 
122 	isc_stats_t *tcpinstats4;
123 	isc_stats_t *tcpoutstats4;
124 	isc_stats_t *tcpinstats6;
125 	isc_stats_t *tcpoutstats6;
126 };
127 
128 struct ns_altsecret {
129 	ISC_LINK(ns_altsecret_t) link;
130 	unsigned char secret[32];
131 };
132 
133 isc_result_t
134 ns_server_create(isc_mem_t *mctx, ns_matchview_t matchingview,
135 		 ns_server_t **sctxp);
136 /*%<
137  * Create a server context object with default settings.
138  */
139 
140 void
141 ns_server_attach(ns_server_t *src, ns_server_t **dest);
142 /*%<
143  * Attach a server context.
144  *
145  * Requires:
146  *\li	'src' is valid.
147  */
148 
149 void
150 ns_server_detach(ns_server_t **sctxp);
151 /*%<
152  * Detach from a server context.  If its reference count drops to zero, destroy
153  * it, freeing its memory.
154  *
155  * Requires:
156  *\li	'*sctxp' is valid.
157  * Ensures:
158  *\li	'*sctxp' is NULL on return.
159  */
160 
161 isc_result_t
162 ns_server_setserverid(ns_server_t *sctx, const char *serverid);
163 /*%<
164  * Set sctx->server_id to 'serverid'. If it was set previously, free the memory.
165  *
166  * Requires:
167  *\li	'sctx' is valid.
168  */
169 
170 void
171 ns_server_setoption(ns_server_t *sctx, unsigned int option, bool value);
172 /*%<
173  *	Set the given options on (if 'value' == #true)
174  *	or off (if 'value' == #false).
175  *
176  * Requires:
177  *\li	'sctx' is valid
178  */
179 
180 bool
181 ns_server_getoption(ns_server_t *sctx, unsigned int option);
182 /*%<
183  *	Returns the current value of the specified server option.
184  *
185  * Requires:
186  *\li	'sctx' is valid.
187  */
188 #endif /* NS_SERVER_H */
189