xref: /openbsd/usr.sbin/nsd/nsd.h (revision 9b7c3dbb)
1 /*
2  * nsd.h -- nsd(8) definitions and prototypes
3  *
4  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  */
9 
10 #ifndef	_NSD_H_
11 #define	_NSD_H_
12 
13 #include <signal.h>
14 
15 #include "dns.h"
16 #include "edns.h"
17 struct netio_handler;
18 struct nsd_options;
19 struct udb_base;
20 struct daemon_remote;
21 
22 /* The NSD runtime states and NSD ipc command values */
23 #define	NSD_RUN	0
24 #define	NSD_RELOAD 1
25 #define	NSD_SHUTDOWN 2
26 #define	NSD_STATS 3
27 #define	NSD_REAP_CHILDREN 4
28 #define	NSD_QUIT 5
29 /*
30  * PASS_TO_XFRD is followed by the u16(len in network order) and
31  * then network packet contents.  packet is a notify(acl checked), or
32  * xfr reply from a master(acl checked).
33  * followed by u32(acl number that matched from notify/xfr acl).
34  */
35 #define NSD_PASS_TO_XFRD 6
36 /*
37  * RELOAD_REQ is sent when parent receives a SIGHUP and tells
38  * xfrd that it wants to initiate a reload (and thus task swap).
39  */
40 #define NSD_RELOAD_REQ 7
41 /*
42  * RELOAD_DONE is sent at the end of a reload pass.
43  * xfrd then knows that reload phase is over.
44  */
45 #define NSD_RELOAD_DONE 8
46 /*
47  * QUIT_SYNC is sent to signify a synchronisation of ipc
48  * channel content during reload
49  */
50 #define NSD_QUIT_SYNC 9
51 /*
52  * QUIT_WITH_STATS is sent during a reload when BIND8_STATS is defined,
53  * from parent to children.  The stats are transferred too from child to
54  * parent with this commandvalue, when the child is exiting.
55  */
56 #define NSD_QUIT_WITH_STATS 10
57 /*
58  * QUIT_CHILD is sent at exit, to make sure the child has exited so that
59  * port53 is free when all of nsd's processes have exited at shutdown time
60  */
61 #define NSD_QUIT_CHILD 11
62 
63 #define NSD_SERVER_MAIN 0x0U
64 #define NSD_SERVER_UDP  0x1U
65 #define NSD_SERVER_TCP  0x2U
66 #define NSD_SERVER_BOTH (NSD_SERVER_UDP | NSD_SERVER_TCP)
67 
68 #ifdef INET6
69 #define DEFAULT_AI_FAMILY AF_UNSPEC
70 #else
71 #define DEFAULT_AI_FAMILY AF_INET
72 #endif
73 
74 #ifdef BIND8_STATS
75 /* Counter for statistics */
76 typedef	unsigned long stc_t;
77 
78 #define	LASTELEM(arr)	(sizeof(arr) / sizeof(arr[0]) - 1)
79 
80 #define	STATUP(nsd, stc) nsd->st.stc++
81 /* #define	STATUP2(nsd, stc, i)  ((i) <= (LASTELEM(nsd->st.stc) - 1)) ? nsd->st.stc[(i)]++ : \
82 				nsd->st.stc[LASTELEM(nsd->st.stc)]++ */
83 
84 #define	STATUP2(nsd, stc, i) nsd->st.stc[(i) <= (LASTELEM(nsd->st.stc) - 1) ? i : LASTELEM(nsd->st.stc)]++
85 #else	/* BIND8_STATS */
86 
87 #define	STATUP(nsd, stc) /* Nothing */
88 #define	STATUP2(nsd, stc, i) /* Nothing */
89 
90 #endif /* BIND8_STATS */
91 
92 #ifdef USE_ZONE_STATS
93 /* increment zone statistic, checks if zone-nonNULL and zone array bounds */
94 #define ZTATUP(nsd, zone, stc) ( \
95 	(zone && zone->zonestatid < nsd->zonestatsizenow) ? \
96 		nsd->zonestatnow[zone->zonestatid].stc++ \
97 		: 0)
98 #define	ZTATUP2(nsd, zone, stc, i) ( \
99 	(zone && zone->zonestatid < nsd->zonestatsizenow) ? \
100 		(nsd->zonestatnow[zone->zonestatid].stc[(i) <= (LASTELEM(nsd->zonestatnow[zone->zonestatid].stc) - 1) ? i : LASTELEM(nsd->zonestatnow[zone->zonestatid].stc)]++ ) \
101 		: 0)
102 #else /* USE_ZONE_STATS */
103 #define	ZTATUP(nsd, zone, stc) /* Nothing */
104 #define	ZTATUP2(nsd, zone, stc, i) /* Nothing */
105 #endif /* USE_ZONE_STATS */
106 
107 struct nsd_socket
108 {
109 	struct addrinfo	*	addr;
110 	int			s;
111 	int			fam;
112 };
113 
114 struct nsd_child
115 {
116 	 /* The type of child process (UDP or TCP handler). */
117 	int   kind;
118 
119 	/* The child's process id.  */
120 	pid_t pid;
121 
122 	/* child number in child array */
123 	int child_num;
124 
125 	/*
126 	 * Socket used by the parent process to send commands and
127 	 * receive responses to/from this child process.
128 	 */
129 	int child_fd;
130 
131 	/*
132 	 * Socket used by the child process to receive commands and
133 	 * send responses from/to the parent process.
134 	 */
135 	int parent_fd;
136 
137 	/*
138 	 * IPC info, buffered for nonblocking writes to the child
139 	 */
140 	uint8_t need_to_send_STATS, need_to_send_QUIT;
141 	uint8_t need_to_exit, has_exited;
142 
143 	/*
144 	 * The handler for handling the commands from the child.
145 	 */
146 	struct netio_handler* handler;
147 
148 #ifdef	BIND8_STATS
149 	stc_t query_count;
150 #endif
151 };
152 
153 /* NSD configuration and run-time variables */
154 typedef struct nsd nsd_type;
155 struct	nsd
156 {
157 	/*
158 	 * Global region that is not deallocated until NSD shuts down.
159 	 */
160 	region_type    *region;
161 
162 	/* Run-time variables */
163 	pid_t		pid;
164 	volatile sig_atomic_t mode;
165 	volatile sig_atomic_t signal_hint_reload_hup;
166 	volatile sig_atomic_t signal_hint_reload;
167 	volatile sig_atomic_t signal_hint_child;
168 	volatile sig_atomic_t signal_hint_quit;
169 	volatile sig_atomic_t signal_hint_shutdown;
170 	volatile sig_atomic_t signal_hint_stats;
171 	volatile sig_atomic_t signal_hint_statsusr;
172 	volatile sig_atomic_t quit_sync_done;
173 	unsigned		server_kind;
174 	struct namedb	*db;
175 	int				debug;
176 
177 	size_t            child_count;
178 	struct nsd_child *children;
179 	int	restart_children;
180 	int	reload_failed;
181 
182 	/* NULL if this is the parent process. */
183 	struct nsd_child *this_child;
184 
185 	/* mmaps with data exchange from xfrd and reload */
186 	struct udb_base* task[2];
187 	int mytask; /* the base used by this process */
188 	struct netio_handler* xfrd_listener;
189 	struct daemon_remote* rc;
190 
191 	/* Configuration */
192 	const char		*dbfile;
193 	const char		*pidfile;
194 	const char		*log_filename;
195 	const char		*username;
196 	uid_t			uid;
197 	gid_t			gid;
198 	const char		*chrootdir;
199 	const char		*version;
200 	const char		*identity;
201 	uint16_t		nsid_len;
202 	unsigned char   *nsid;
203 	uint8_t 		file_rotation_ok;
204 
205 	/* number of interfaces */
206 	size_t	ifs;
207 	uint8_t grab_ip6_optional;
208 	/* non0 if so_reuseport is in use, if so, tcp, udp array increased */
209 	int reuseport;
210 
211 	/* TCP specific configuration (array size ifs) */
212 	struct nsd_socket* tcp;
213 
214 	/* UDP specific configuration (array size ifs) */
215 	struct nsd_socket* udp;
216 
217 	edns_data_type edns_ipv4;
218 #if defined(INET6)
219 	edns_data_type edns_ipv6;
220 #endif
221 
222 	int maximum_tcp_count;
223 	int current_tcp_count;
224 	int tcp_query_count;
225 	int tcp_timeout;
226 	int tcp_mss;
227 	int outgoing_tcp_mss;
228 	size_t ipv4_edns_size;
229 	size_t ipv6_edns_size;
230 
231 #ifdef	BIND8_STATS
232 
233 	struct nsdst {
234 		time_t	boot;
235 		int	period;		/* Produce statistics dump every st_period seconds */
236 		stc_t	qtype[257];	/* Counters per qtype */
237 		stc_t	qclass[4];	/* Class IN or Class CH or other */
238 		stc_t	qudp, qudp6;	/* Number of queries udp and udp6 */
239 		stc_t	ctcp, ctcp6;	/* Number of tcp and tcp6 connections */
240 		stc_t	rcode[17], opcode[6]; /* Rcodes & opcodes */
241 		/* Dropped, truncated, queries for nonconfigured zone, tx errors */
242 		stc_t	dropped, truncated, wrongzone, txerr, rxerr;
243 		stc_t 	edns, ednserr, raxfr, nona;
244 		uint64_t db_disk, db_mem;
245 	} st;
246 	/* per zone stats, each an array per zone-stat-idx, stats per zone is
247 	 * add of [0][zoneidx] and [1][zoneidx]. */
248 	struct nsdst* zonestat[2];
249 	/* fd for zonestat mapping (otherwise mmaps cannot be shared between
250 	 * processes and resized) */
251 	int zonestatfd[2];
252 	/* filenames */
253 	char* zonestatfname[2];
254 	/* size of the mmapped zone stat array (number of array entries) */
255 	size_t zonestatsize[2], zonestatdesired, zonestatsizenow;
256 	/* current zonestat array to use */
257 	struct nsdst* zonestatnow;
258 #endif /* BIND8_STATS */
259 	/* ratelimit for errors, time value */
260 	time_t err_limit_time;
261 	/* ratelimit for errors, packet count */
262 	unsigned int err_limit_count;
263 
264 	struct nsd_options* options;
265 };
266 
267 extern struct nsd nsd;
268 
269 /* nsd.c */
270 pid_t readpid(const char *file);
271 int writepid(struct nsd *nsd);
272 void unlinkpid(const char* file);
273 void sig_handler(int sig);
274 void bind8_stats(struct nsd *nsd);
275 
276 /* server.c */
277 int server_init(struct nsd *nsd);
278 int server_prepare(struct nsd *nsd);
279 void server_main(struct nsd *nsd);
280 void server_child(struct nsd *nsd);
281 void server_shutdown(struct nsd *nsd);
282 void server_close_all_sockets(struct nsd_socket sockets[], size_t n);
283 struct event_base* nsd_child_event_base(void);
284 /* extra domain numbers for temporary domains */
285 #define EXTRA_DOMAIN_NUMBERS 1024
286 #define SLOW_ACCEPT_TIMEOUT 2 /* in seconds */
287 /* ratelimit for error responses */
288 #define ERROR_RATELIMIT 100 /* qps */
289 /* allocate zonestat structures */
290 void server_zonestat_alloc(struct nsd* nsd);
291 /* remap the mmaps for zonestat isx, to bytesize sz.  Caller has to set
292  * the zonestatsize */
293 void zonestat_remap(struct nsd* nsd, int idx, size_t sz);
294 /* allocate and init xfrd variables */
295 void server_prepare_xfrd(struct nsd *nsd);
296 /* start xfrdaemon (again) */
297 void server_start_xfrd(struct nsd *nsd, int del_db, int reload_active);
298 /* send SOA serial numbers to xfrd */
299 void server_send_soa_xfrd(struct nsd *nsd, int shortsoa);
300 ssize_t block_read(struct nsd* nsd, int s, void* p, ssize_t sz, int timeout);
301 
302 #endif	/* _NSD_H_ */
303