xref: /openbsd/usr.sbin/nsd/xfrd.h (revision 898184e3)
1 /*
2  * xfrd.h - XFR (transfer) Daemon header file. Coordinates SOA updates.
3  *
4  * Copyright (c) 2001-2011, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  */
9 
10 #ifndef XFRD_H
11 #define XFRD_H
12 
13 #include "config.h"
14 #include "netio.h"
15 #include "rbtree.h"
16 #include "namedb.h"
17 #include "options.h"
18 #include "dns.h"
19 #include "tsig.h"
20 
21 struct nsd;
22 struct region;
23 struct buffer;
24 struct xfrd_tcp;
25 struct xfrd_tcp_set;
26 struct notify_zone_t;
27 typedef struct xfrd_state xfrd_state_t;
28 typedef struct xfrd_zone xfrd_zone_t;
29 typedef struct xfrd_soa xfrd_soa_t;
30 /*
31  * The global state for the xfrd daemon process.
32  * The time_t times are epochs in secs since 1970, absolute times.
33  */
34 struct xfrd_state {
35 	/* time when daemon was last started */
36 	time_t xfrd_start_time;
37 	struct region* region;
38 	netio_type* netio;
39 	struct nsd* nsd;
40 
41 	struct xfrd_tcp_set* tcp_set;
42 	/* packet buffer for udp packets */
43 	struct buffer* packet;
44 	/* udp waiting list */
45 	struct xfrd_zone *udp_waiting_first, *udp_waiting_last;
46 	/* number of udp sockets (for sending queries) in use */
47 	size_t udp_use_num;
48 
49 	/* current time is cached */
50 	uint8_t got_time;
51 	time_t current_time;
52 
53 	/* timer for NSD reload */
54 	struct timespec reload_timeout;
55 	netio_handler_type reload_handler;
56 	/* last reload must have caught all zone updates before this time */
57 	time_t reload_cmd_last_sent;
58 	uint8_t can_send_reload;
59 
60 	/* communication channel with server_main */
61 	netio_handler_type ipc_handler;
62 	uint8_t ipc_is_soa;
63 	uint8_t parent_soa_info_pass;
64 	struct xfrd_tcp *ipc_conn;
65 	struct buffer* ipc_pass;
66 	/* sending ipc to server_main */
67 	struct xfrd_tcp *ipc_conn_write;
68 	uint8_t need_to_send_reload;
69 	uint8_t need_to_send_quit;
70 	uint8_t sending_zone_state;
71 	uint8_t	ipc_send_blocked;
72 	stack_type* dirty_zones; /* stack of xfrd_zone* */
73 
74 	/* xfrd shutdown flag */
75 	uint8_t shutdown;
76 
77 	/* tree of zones, by apex name, contains xfrd_zone_t*. Only secondary zones. */
78 	rbtree_t *zones;
79 
80 	/* tree of zones, by apex name, contains notify_zone_t*. All zones. */
81 	rbtree_t *notify_zones;
82 	/* number of notify_zone_t active using UDP socket */
83 	int notify_udp_num;
84 	/* first and last notify_zone_t* entries waiting for a UDP socket */
85 	struct notify_zone_t *notify_waiting_first, *notify_waiting_last;
86 };
87 
88 /*
89  * XFR daemon SOA information kept in network format.
90  * This is in packet order.
91  */
92 struct xfrd_soa {
93 	/* name of RR is zone apex dname */
94 	uint16_t type; /* = TYPE_SOA */
95 	uint16_t klass; /* = CLASS_IN */
96 	uint32_t ttl;
97 	uint16_t rdata_count; /* = 7 */
98 	/* format is 1 octet length, + wireformat dname.
99 	   one more octet since parse_dname_wire_from_packet needs it.
100 	   maximum size is allocated to avoid memory alloc/free. */
101 	uint8_t prim_ns[MAXDOMAINLEN + 2];
102 	uint8_t email[MAXDOMAINLEN + 2];
103 	uint32_t serial;
104 	uint32_t refresh;
105 	uint32_t retry;
106 	uint32_t expire;
107 	uint32_t minimum;
108 };
109 
110 
111 /*
112  * XFRD state for a single zone
113  */
114 struct xfrd_zone {
115 	rbnode_t node;
116 
117 	/* name of the zone */
118 	const dname_type* apex;
119 	const char* apex_str;
120 
121 	/* Three types of soas:
122 	 * NSD: in use by running server
123 	 * disk: stored on disk in db/diff file
124 	 * notified: from notification, could be available on a master.
125 	 * And the time the soa was acquired (start time for timeouts).
126 	 * If the time==0, no SOA is available.
127 	 */
128 	xfrd_soa_t soa_nsd;
129 	time_t soa_nsd_acquired;
130 	xfrd_soa_t soa_disk;
131 	time_t soa_disk_acquired;
132 	xfrd_soa_t soa_notified;
133 	time_t soa_notified_acquired;
134 
135 	enum xfrd_zone_state {
136 		xfrd_zone_ok,
137 		xfrd_zone_refreshing,
138 		xfrd_zone_expired
139 	} state;
140 
141 	/* if state is dirty it needs to be sent to server_main.
142 	 * it is also on the dirty_stack. Not saved on disk. */
143 	uint8_t dirty;
144 
145 	/* master to try to transfer from, number for persistence */
146 	acl_options_t* master;
147 	int master_num;
148 	int next_master; /* -1 or set by notify where to try next */
149 	/* round of xfrattempts, -1 is waiting for timeout */
150 	int round_num;
151 	zone_options_t* zone_options;
152 	int fresh_xfr_timeout;
153 
154 	/* handler for timeouts */
155 	struct timespec timeout;
156 	netio_handler_type zone_handler;
157 
158 	/* tcp connection zone is using, or -1 */
159 	int tcp_conn;
160 	/* zone is waiting for a tcp connection */
161 	uint8_t tcp_waiting;
162 	/* next zone in waiting list */
163 	xfrd_zone_t* tcp_waiting_next;
164 	/* zone is waiting for a udp connection (tcp is preferred) */
165 	uint8_t udp_waiting;
166 	/* next zone in waiting list for UDP */
167 	xfrd_zone_t* udp_waiting_next;
168 
169 	/* xfr message handling data */
170 	/* query id */
171 	uint16_t query_id;
172 	uint32_t msg_seq_nr; /* number of messages already handled */
173 	uint32_t msg_old_serial, msg_new_serial; /* host byte order */
174 	size_t msg_rr_count;
175 	uint8_t msg_is_ixfr; /* 1:IXFR detected. 2:middle IXFR SOA seen. */
176 	tsig_record_type tsig; /* tsig state for IXFR/AXFR */
177 };
178 
179 enum xfrd_packet_result {
180 	xfrd_packet_bad, /* drop the packet/connection */
181 	xfrd_packet_more, /* more packets to follow on tcp */
182 	xfrd_packet_notimpl, /* server responded with NOTIMPL or FORMATERR */
183 	xfrd_packet_tcp, /* try tcp connection */
184 	xfrd_packet_transfer, /* server responded with transfer*/
185 	xfrd_packet_newlease /* no changes, soa OK */
186 };
187 
188 /*
189    Division of the (portably: 1024) max number of sockets that can be open.
190    The sum of the below numbers should be below the user limit for sockets
191    open, or you see errors in your logfile.
192    And it should be below FD_SETSIZE, to be able to select() on replies.
193    Note that also some sockets are used for writing the ixfr.db, xfrd.state
194    files and for the pipes to the main parent process.
195 */
196 #define XFRD_MAX_TCP 50 /* max number of TCP AXFR/IXFR concurrent connections.*/
197 			/* Each entry has 64Kb buffer preallocated.*/
198 #define XFRD_MAX_UDP 100 /* max number of UDP sockets at a time for IXFR */
199 #define XFRD_MAX_UDP_NOTIFY 50 /* max concurrent UDP sockets for NOTIFY */
200 
201 extern xfrd_state_t* xfrd;
202 
203 /* start xfrd, new start. Pass socket to server_main. */
204 void xfrd_init(int socket, struct nsd* nsd);
205 
206 /* get the current time epoch. Cached for speed. */
207 time_t xfrd_time();
208 
209 /*
210  * Handle final received packet from network.
211  * returns enum of packet discovery results
212  */
213 enum xfrd_packet_result xfrd_handle_received_xfr_packet(
214 	xfrd_zone_t* zone, buffer_type* packet);
215 
216 /* set timer to specific value */
217 void xfrd_set_timer(xfrd_zone_t* zone, time_t t);
218 /* set refresh timer of zone to refresh at time now */
219 void xfrd_set_refresh_now(xfrd_zone_t* zone);
220 /* unset the timer - no more timeouts, for when zone is queued */
221 void xfrd_unset_timer(xfrd_zone_t* zone);
222 
223 /*
224  * Make a new request to next master server.
225  * uses next_master if set (and a fresh set of rounds).
226  * otherwised, starts new round of requests if none started already.
227  * starts next round of requests if at last master.
228  * if too many rounds of requests, sets timer for next retry.
229  */
230 void xfrd_make_request(xfrd_zone_t* zone);
231 
232 /*
233  * send packet via udp (returns UDP fd source socket) to acl addr.
234  * returns -1 on failure.
235  */
236 int xfrd_send_udp(acl_options_t* acl, buffer_type* packet, acl_options_t* ifc);
237 
238 /*
239  * read from udp port packet into buffer, returns 0 on failure
240  */
241 int xfrd_udp_read_packet(buffer_type* packet, int fd);
242 
243 /*
244  * Release udp socket that a zone is using
245  */
246 void xfrd_udp_release(xfrd_zone_t* zone);
247 
248 /*
249  * Get a static buffer for temporary use (to build a packet).
250  */
251 struct buffer* xfrd_get_temp_buffer();
252 
253 /*
254  * TSIG sign outgoing request. Call if acl has a key.
255  */
256 void xfrd_tsig_sign_request(buffer_type* packet, struct tsig_record* tsig,
257         acl_options_t* acl);
258 
259 /* handle incoming soa information (NSD is running it, time acquired=guess).
260    Pass soa=NULL,acquired=now if NSD has nothing loaded for the zone
261    (i.e. zonefile was deleted). */
262 void xfrd_handle_incoming_soa(xfrd_zone_t* zone, xfrd_soa_t* soa,
263 	time_t acquired);
264 /* handle a packet passed along ipc route. acl is the one that accepted
265    the packet. The packet is the network blob received. acl_xfr is
266    provide-xfr acl matching notify sender or -1 */
267 void xfrd_handle_passed_packet(buffer_type* packet,
268 	int acl_num, int acl_xfr);
269 
270 /* send expiry notify for all zones to nsd (sets all dirty). */
271 void xfrd_send_expy_all_zones();
272 
273 /* try to reopen the logfile. */
274 void xfrd_reopen_logfile();
275 
276 /* copy SOA info from rr to soa struct. */
277 void xfrd_copy_soa(xfrd_soa_t* soa, rr_type* rr);
278 
279 /* check for failed updates - it is assumed that now the reload has
280    finished, and all zone SOAs have been sent. */
281 void xfrd_check_failed_updates();
282 
283 /*
284  * Prepare zones for a reload, this sets the times on the zones to be
285  * before the current time, so the reload happens after.
286  */
287 void xfrd_prepare_zones_for_reload();
288 
289 /* Bind a local interface to a socket descriptor, return 1 on success */
290 int xfrd_bind_local_interface(int sockd, acl_options_t* ifc,
291 	acl_options_t* acl, int tcp);
292 
293 #endif /* XFRD_H */
294