OpenDNSSEC-signer 2.1.12
xfrd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
32#include "config.h"
33#include "daemon/engine.h"
34#include "daemon/xfrhandler.h"
35#include "duration.h"
36#include "file.h"
37#include "log.h"
38#include "status.h"
39#include "util.h"
40#include "signer/domain.h"
41#include "signer/zone.h"
42#include "wire/tcpset.h"
43#include "wire/xfrd.h"
44#include "signer/backup.h"
45
46#include <unistd.h>
47#include <fcntl.h>
48
49#define XFRD_TSIG_MAX_UNSIGNED 100
50
51static const char* xfrd_str = "xfrd";
52
53static void xfrd_handle_zone(netio_type* netio,
54 netio_handler_type* handler, netio_events_type event_types);
55static void xfrd_make_request(xfrd_type* xfrd);
56
57static socklen_t xfrd_acl_sockaddr(acl_type* acl, unsigned int port,
58 struct sockaddr_storage *sck);
59
60static void xfrd_write_soa(xfrd_type* xfrd, buffer_type* buffer);
61static int xfrd_parse_soa(xfrd_type* xfrd, buffer_type* buffer,
62 unsigned rdata_only, unsigned update, uint32_t t,
63 uint32_t* serial);
64static ods_status xfrd_parse_rrs(xfrd_type* xfrd, buffer_type* buffer,
65 uint16_t count, int* done);
66static xfrd_pkt_status xfrd_parse_packet(xfrd_type* xfrd,
67 buffer_type* buffer);
68static xfrd_pkt_status xfrd_handle_packet(xfrd_type* xfrd,
69 buffer_type* buffer);
70
71static void xfrd_tcp_obtain(xfrd_type* xfrd, tcp_set_type* set);
72static void xfrd_tcp_read(xfrd_type* xfrd, tcp_set_type* set);
73static void xfrd_tcp_release(xfrd_type* xfrd, tcp_set_type* set, int open_waiting);
74static void xfrd_tcp_write(xfrd_type* xfrd, tcp_set_type* set);
75static void xfrd_tcp_xfr(xfrd_type* xfrd, tcp_set_type* set);
76static int xfrd_tcp_open(xfrd_type* xfrd, tcp_set_type* set);
77
78static void xfrd_udp_obtain(xfrd_type* xfrd);
79static void xfrd_udp_read(xfrd_type* xfrd);
80static void xfrd_udp_release(xfrd_type* xfrd);
81static int xfrd_udp_read_packet(xfrd_type* xfrd);
82static int xfrd_udp_send(xfrd_type* xfrd, buffer_type* buffer);
83static int xfrd_udp_send_request_ixfr(xfrd_type* xfrd);
84
85static time_t xfrd_time(xfrd_type* xfrd);
86static void xfrd_set_timer(xfrd_type* xfrd, time_t t);
87static void xfrd_set_timer_time(xfrd_type* xfrd, time_t t);
88static void xfrd_unset_timer(xfrd_type* xfrd);
89
90
95static uint8_t
96xfrd_recover_dname(uint8_t* dname, const char* name)
97{
98 const uint8_t *s = (const uint8_t *) name;
99 uint8_t *h;
100 uint8_t *p;
101 uint8_t *d = dname;
102 size_t label_length;
103
104 if (strcmp(name, ".") == 0) {
105 /* Root domain. */
106 dname[0] = 0;
107 return 1;
108 }
109 for (h = d, p = h + 1; *s; ++s, ++p) {
110 if (p - dname >= MAXDOMAINLEN) {
111 return 0;
112 }
113 switch (*s) {
114 case '.':
115 if (p == h + 1) {
116 /* Empty label. */
117 return 0;
118 } else {
119 label_length = p - h - 1;
120 if (label_length > MAXLABELLEN) {
121 return 0;
122 }
123 *h = label_length;
124 h = p;
125 }
126 break;
127 case '\\':
128 /* Handle escaped characters (RFC1035 5.1) */
129 if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
130 int val = (ldns_hexdigit_to_int(s[1]) * 100 +
131 ldns_hexdigit_to_int(s[2]) * 10 +
132 ldns_hexdigit_to_int(s[3]));
133 if (0 <= val && val <= 255) {
134 s += 3;
135 *p = val;
136 } else {
137 *p = *++s;
138 }
139 } else if (s[1] != '\0') {
140 *p = *++s;
141 }
142 break;
143 default:
144 *p = *s;
145 break;
146 }
147 }
148 if (p != h + 1) {
149 /* Terminate last label. */
150 label_length = p - h - 1;
151 if (label_length > MAXLABELLEN) {
152 return 0;
153 }
154 *h = label_length;
155 h = p;
156 }
157 /* Add root label. */
158 *h = 0;
159 return p-dname;
160}
161
162
167static void
168xfrd_recover(xfrd_type* xfrd)
169{
170 zone_type* zone = (zone_type*) xfrd->zone;
171 char* file = NULL;
172 FILE* fd = NULL;
173 int round_num = 0;
174 int master_num = 0;
175 int next_master = 0;
176 uint32_t timeout = 0;
177 uint32_t serial_xfr = 0;
178 uint32_t serial_notify = 0;
179 uint32_t serial_disk = 0;
180 time_t serial_xfr_acquired = 0;
181 time_t serial_notify_acquired = 0;
182 time_t serial_disk_acquired = 0;
183 uint32_t soa_ttl = 0;
184 uint32_t soa_serial = 0;
185 uint32_t soa_refresh = 0;
186 uint32_t soa_retry = 0;
187 uint32_t soa_expire = 0;
188 uint32_t soa_minimum = 0;
189 const char* soa_mname = NULL;
190 const char* soa_rname = NULL;
191
192 if (zone && zone->name && zone->db &&
193 zone->db->is_initialized && zone->db->have_serial) {
194 file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
195 if (file) {
196 ods_log_verbose("[%s] recover xfrd.state file %s zone %s", xfrd_str,
197 file, zone->name);
198 fd = ods_fopen(file, NULL, "r");
199 if (fd) {
200 if (!backup_read_check_str(fd, ODS_SE_FILE_MAGIC_V3)) {
201 ods_log_error("[%s] corrupted state file zone %s: read "
202 "magic (start) error", xfrd_str, zone->name);
203 goto xfrd_recover_error;
204 }
205 if (!backup_read_check_str(fd, ";;Zone:") |
206 !backup_read_check_str(fd, "name") |
207 !backup_read_check_str(fd, zone->name) |
208 !backup_read_check_str(fd, "ttl") |
209 !backup_read_uint32_t(fd, &soa_ttl) |
210 !backup_read_check_str(fd, "mname") |
211 !backup_read_str(fd, &soa_mname) |
212 !backup_read_check_str(fd, "rname") |
213 !backup_read_str(fd, &soa_rname) |
214 !backup_read_check_str(fd, "serial") |
215 !backup_read_uint32_t(fd, &soa_serial) |
216 !backup_read_check_str(fd, "refresh") |
217 !backup_read_uint32_t(fd, &soa_refresh) |
218 !backup_read_check_str(fd, "retry") |
219 !backup_read_uint32_t(fd, &soa_retry) |
220 !backup_read_check_str(fd, "expire") |
221 !backup_read_uint32_t(fd, &soa_expire) |
222 !backup_read_check_str(fd, "minimum") |
223 !backup_read_uint32_t(fd, &soa_minimum)) {
224 ods_log_error("[%s] corrupted state file zone %s: read "
225 ";;Zone error", xfrd_str, zone->name);
226 goto xfrd_recover_error;
227 }
228 if (!backup_read_check_str(fd, ";;Master:") |
229 !backup_read_check_str(fd, "num") |
230 !backup_read_int(fd, &master_num) |
231 !backup_read_check_str(fd, "next") |
232 !backup_read_int(fd, &next_master) |
233 !backup_read_check_str(fd, "round") |
234 !backup_read_int(fd, &round_num) |
235 !backup_read_check_str(fd, "timeout") |
236 !backup_read_uint32_t(fd, &timeout)) {
237 ods_log_error("[%s] corrupt state file zone %s: read "
238 ";;Master error", xfrd_str, zone->name);
239 goto xfrd_recover_error;
240 }
241 if (!backup_read_check_str(fd, ";;Serial:") |
242 !backup_read_check_str(fd, "xfr") |
243 !backup_read_uint32_t(fd, &serial_xfr) |
244 !backup_read_time_t(fd, &serial_xfr_acquired) |
245 !backup_read_check_str(fd, "notify") |
246 !backup_read_uint32_t(fd, &serial_notify) |
247 !backup_read_time_t(fd, &serial_notify_acquired) |
248 !backup_read_check_str(fd, "disk") |
249 !backup_read_uint32_t(fd, &serial_disk) |
250 !backup_read_time_t(fd, &serial_disk_acquired)) {
251 ods_log_error("[%s] corrupt state file zone %s: read "
252 ";;Serial error", xfrd_str, zone->name);
253 goto xfrd_recover_error;
254 }
255 if (!backup_read_check_str(fd, ODS_SE_FILE_MAGIC_V3)) {
256 ods_log_error("[%s] corrupt state file zone %s: read "
257 "magic (end) error", xfrd_str, zone->name);
258 goto xfrd_recover_error;
259 }
260
261 /* all ok */
262 xfrd->master_num = master_num;
263 xfrd->next_master = next_master;
264 xfrd->round_num = round_num;
265 xfrd->timeout.tv_sec = timeout;
266 xfrd->timeout.tv_nsec = 0;
267 xfrd->master = NULL; /* acl_find_num(...) */
268 xfrd->soa.ttl = soa_ttl;
269 xfrd->soa.serial = soa_serial;
270 xfrd->soa.refresh = soa_refresh;
271 xfrd->soa.retry = soa_retry;
272 xfrd->soa.expire = soa_expire;
273 xfrd->soa.minimum = soa_minimum;
274 xfrd->soa.mname[0] = xfrd_recover_dname(xfrd->soa.mname+1,
275 soa_mname);
276 xfrd->soa.rname[0] = xfrd_recover_dname(xfrd->soa.rname+1,
277 soa_rname);
278 xfrd->serial_xfr = serial_xfr;
279 xfrd->serial_xfr_acquired = serial_xfr_acquired;
280 xfrd->serial_notify = serial_notify;
281 xfrd->serial_notify_acquired = serial_notify_acquired;
282 xfrd->serial_disk = serial_disk;
283 xfrd->serial_disk_acquired = serial_disk_acquired;
284 if (!timeout || serial_notify_acquired ||
285 (serial_disk_acquired &&
286 (uint32_t)xfrd_time(xfrd) - serial_disk_acquired >
287 soa_refresh)) {
289 }
290 if (serial_disk_acquired &&
291 ((uint32_t)xfrd_time(xfrd) - serial_disk_acquired >
292 soa_expire)) {
294 }
295
296xfrd_recover_error:
297 free((void*)soa_mname);
298 free((void*)soa_rname);
299 ods_fclose(fd);
300 }
301 free(file);
302 }
303 } else {
304 ods_log_verbose("[%s] did not recover xfrd.state file zone %s", xfrd_str,
305 (zone && zone->name)?zone->name:"(null)");
306 }
307}
308
309
316{
317 xfrd_type* xfrd = NULL;
318 if (!xfrhandler || !zone) {
319 return NULL;
320 }
321 CHECKALLOC(xfrd = (xfrd_type*) malloc(sizeof(xfrd_type)));
322 pthread_mutex_init(&xfrd->serial_lock, NULL);
323 pthread_mutex_init(&xfrd->rw_lock, NULL);
324
325 xfrd->xfrhandler = xfrhandler;
326 xfrd->zone = zone;
327 xfrd->tcp_conn = -1;
328 xfrd->round_num = -1;
329 xfrd->master_num = 0;
330 xfrd->next_master = -1;
331 xfrd->master = NULL;
332 pthread_mutex_lock(&xfrd->serial_lock);
333 xfrd->serial_xfr = 0;
334 xfrd->serial_disk = 0;
335 xfrd->serial_notify = 0;
336 xfrd->serial_xfr_acquired = 0;
337 xfrd->serial_disk_acquired = 0;
338 xfrd->serial_notify_acquired = 0;
339 xfrd->serial_retransfer = 0;
340 pthread_mutex_unlock(&xfrd->serial_lock);
341 xfrd->query_id = 0;
342 xfrd->msg_seq_nr = 0;
343 xfrd->msg_rr_count = 0;
344 xfrd->msg_old_serial = 0;
345 xfrd->msg_new_serial = 0;
346 xfrd->msg_is_ixfr = 0;
347 xfrd->msg_do_retransfer = 0;
348 xfrd->udp_waiting = 0;
349 xfrd->udp_waiting_next = NULL;
350 xfrd->tcp_waiting = 0;
351 xfrd->tcp_waiting_next = NULL;
352 xfrd->tsig_rr = tsig_rr_create();
353 if (!xfrd->tsig_rr) {
354 xfrd_cleanup(xfrd, 0);
355 return NULL;
356 }
357 memset(&xfrd->soa, 0, sizeof(xfrd->soa));
358 xfrd->soa.ttl = 0;
359 xfrd->soa.mname[0] = 1;
360 xfrd->soa.rname[0] = 1;
361 xfrd->soa.serial = 0;
362 xfrd->soa.refresh = 3600;
363 xfrd->soa.retry = 300;
364 xfrd->soa.expire = 604800;
365 xfrd->soa.minimum = 3600;
366 xfrd->handler.fd = -1;
367 xfrd->handler.user_data = (void*) xfrd;
368 xfrd->handler.timeout = 0;
369 xfrd->handler.event_types =
371 xfrd->handler.event_handler = xfrd_handle_zone;
372 xfrd_set_timer_time(xfrd, 0);
373 xfrd_recover(xfrd);
374 return xfrd;
375}
376
377
382static time_t
383xfrd_time(xfrd_type* xfrd)
384{
385 ods_log_assert(xfrd);
386 ods_log_assert(xfrd->xfrhandler);
388}
389
390
395static void
396xfrd_set_timer(xfrd_type* xfrd, time_t t)
397{
398 if (!xfrd || !xfrd->xfrhandler) {
399 return;
400 }
405 if(t > xfrd_time(xfrd) + 10) {
406 time_t extra = t - xfrd_time(xfrd);
407 time_t base = extra*9/10;
408#ifdef HAVE_ARC4RANDOM_UNIFORM
409 t = xfrd_time(xfrd) + base +
410 arc4random_uniform(extra-base);
411#elif HAVE_ARC4RANDOM
412 t = xfrd_time(xfrd) + base +
413 arc4random()%(extra-base);
414#else
415 t = xfrd_time(xfrd) + base +
416 random()%(extra-base);
417#endif
418 }
419 xfrd->handler.timeout = &xfrd->timeout;
420 xfrd->timeout.tv_sec = t;
421 xfrd->timeout.tv_nsec = 0;
422}
423
424
429static void
430xfrd_unset_timer(xfrd_type* xfrd)
431{
432 ods_log_assert(xfrd);
433 xfrd->handler.timeout = NULL;
434}
435
436
441static void
442xfrd_set_timer_time(xfrd_type* xfrd, time_t t)
443{
444 ods_log_assert(xfrd);
445 xfrd_set_timer(xfrd, xfrd_time(xfrd) + t);
446}
447
448
453void
455{
456 zone_type* zone = NULL;
457 if (!xfrd || !xfrd->zone || !xfrd->xfrhandler) {
458 return;
459 }
460 zone = (zone_type*) xfrd->zone;
461 ods_log_debug("[%s] zone %s sets timer timeout now", xfrd_str,
462 zone->name);
463 xfrd_set_timer_time(xfrd, 0);
464}
465
466
471void
473{
474 zone_type* zone = NULL;
475 if (!xfrd || !xfrd->zone || !xfrd->xfrhandler) {
476 return;
477 }
478 zone = (zone_type*) xfrd->zone;
479 ods_log_debug("[%s] zone %s sets timer timeout retry %u", xfrd_str,
480 zone->name, (unsigned) xfrd->soa.retry);
481 xfrd_set_timer_time(xfrd, xfrd->soa.retry);
482}
483
484
489void
491{
492 zone_type* zone = NULL;
493 if (!xfrd || !xfrd->zone || !xfrd->xfrhandler) {
494 return;
495 }
496 zone = (zone_type*) xfrd->zone;
497 ods_log_debug("[%s] zone %s sets timer timeout refresh %u", xfrd_str,
498 zone->name, (unsigned) xfrd->soa.refresh);
499 xfrd_set_timer_time(xfrd, xfrd->soa.refresh);
500}
501
502
507static socklen_t
508xfrd_acl_sockaddr(acl_type* acl, unsigned int port,
509 struct sockaddr_storage *sck)
510{
511 ods_log_assert(acl);
512 ods_log_assert(sck);
513 ods_log_assert(port);
514 memset(sck, 0, sizeof(struct sockaddr_storage));
515 if (acl->family == AF_INET6) {
516 struct sockaddr_in6* sa = (struct sockaddr_in6*)sck;
517 sa->sin6_family = AF_INET6;
518 sa->sin6_port = htons(port);
519 sa->sin6_addr = acl->addr.addr6;
520 return sizeof(struct sockaddr_in6);
521 } else {
522 struct sockaddr_in* sa = (struct sockaddr_in*)sck;
523 sa->sin_family = AF_INET;
524 sa->sin_port = htons(port);
525 sa->sin_addr = acl->addr.addr;
526 return sizeof(struct sockaddr_in);
527 }
528 return 0;
529}
530
531
536socklen_t
537xfrd_acl_sockaddr_to(acl_type* acl, struct sockaddr_storage *to)
538{
539 unsigned int port = 0;
540 if (!acl || !to) {
541 return 0;
542 }
543 port = acl->port ? acl->port : (unsigned) atoi(DNS_PORT_STRING);
544 return xfrd_acl_sockaddr(acl, port, to);
545}
546
547
552static void
553xfrd_tsig_sign(xfrd_type* xfrd, buffer_type* buffer)
554{
555 tsig_algo_type* algo = NULL;
556 if (!xfrd || !xfrd->tsig_rr || !xfrd->master || !xfrd->master->tsig ||
557 !xfrd->master->tsig->key || !buffer) {
558 return; /* no tsig configured */
559 }
560 algo = tsig_lookup_algo(xfrd->master->tsig->algorithm);
561 if (!algo) {
562 ods_log_error("[%s] unable to sign request: tsig unknown algorithm "
563 "%s", xfrd_str, xfrd->master->tsig->algorithm);
564 return;
565 }
566 ods_log_assert(algo);
567 tsig_rr_reset(xfrd->tsig_rr, algo, xfrd->master->tsig->key);
568 xfrd->tsig_rr->original_query_id = buffer_pkt_id(buffer);
569 xfrd->tsig_rr->algo_name = ldns_rdf_clone(xfrd->tsig_rr->algo->wf_name);
570 xfrd->tsig_rr->key_name = ldns_rdf_clone(xfrd->tsig_rr->key->dname);
571 log_dname(xfrd->tsig_rr->key_name, "tsig sign query with key", LOG_DEBUG);
572 log_dname(xfrd->tsig_rr->algo_name, "tsig sign query with algorithm",
573 LOG_DEBUG);
575 tsig_rr_update(xfrd->tsig_rr, buffer, buffer_position(buffer));
576 tsig_rr_sign(xfrd->tsig_rr);
577 ods_log_debug("[%s] tsig append rr to request id=%u", xfrd_str,
578 buffer_pkt_id(buffer));
579 tsig_rr_append(xfrd->tsig_rr, buffer);
582}
583
584
589static int
590xfrd_tsig_process(xfrd_type* xfrd, buffer_type* buffer)
591{
592 zone_type* zone = NULL;
593 int have_tsig = 0;
594 if (!xfrd || !xfrd->tsig_rr || !xfrd->master || !xfrd->master->tsig ||
595 !xfrd->master->tsig->key || !buffer) {
596 return 1; /* no tsig configured */
597 }
598 zone = (zone_type*) xfrd->zone;
599 ods_log_assert(zone);
600 ods_log_assert(zone->name);
601 ods_log_assert(xfrd->master->address);
602 if (!tsig_rr_find(xfrd->tsig_rr, buffer)) {
603 ods_log_error("[%s] unable to process tsig: xfr zone %s from %s "
604 "has malformed tsig rr", xfrd_str, zone->name,
605 xfrd->master->address);
606 return 0;
607 }
608 if (xfrd->tsig_rr->status == TSIG_OK) {
609 have_tsig = 1;
610 if (xfrd->tsig_rr->error_code != LDNS_RCODE_NOERROR) {
611 ods_log_error("[%s] zone %s, from %s has tsig error (%s)",
612 xfrd_str, zone->name, xfrd->master->address,
614 }
615 /* strip the TSIG resource record off... */
616 buffer_set_limit(buffer, xfrd->tsig_rr->position);
618 }
619 /* keep running the TSIG hash */
620 tsig_rr_update(xfrd->tsig_rr, buffer, buffer_limit(buffer));
621 if (have_tsig) {
622 if (!tsig_rr_verify(xfrd->tsig_rr)) {
623 ods_log_error("[%s] unable to process tsig: xfr zone %s from %s "
624 "has bad tsig signature", xfrd_str, zone->name,
625 xfrd->master->address);
626 return 0;
627 }
628 /* prepare for next tsigs */
630 } else if (xfrd->tsig_rr->update_since_last_prepare >
632 /* we allow a number of non-tsig signed packets */
633 ods_log_error("[%s] unable to process tsig: xfr zone %s, from %s "
634 "has too many consecutive packets without tsig", xfrd_str,
635 zone->name, xfrd->master->address);
636 return 0;
637 }
638 if (!have_tsig && xfrd->msg_seq_nr == 0) {
639 ods_log_error("[%s] unable to process tsig: xfr zone %s from %s "
640 "has no tsig in first packet of reply", xfrd_str,
641 zone->name, xfrd->master->address);
642 return 0;
643 }
644 /* process TSIG ok */
645 return 1;
646}
647
648
653static void
654xfrd_commit_packet(xfrd_type* xfrd)
655{
656 zone_type* zone = NULL;
657 char* xfrfile = NULL;
658 FILE* fd = NULL;
659 time_t serial_disk_acq = 0;
660 ods_log_assert(xfrd);
661 zone = (zone_type*) xfrd->zone;
662 xfrfile = ods_build_path(zone->name, ".xfrd", 0, 1);
663 if (!xfrfile) {
664 ods_log_crit("[%s] unable to commit xfr zone %s: build path failed",
665 xfrd_str, zone->name);
666 return;
667 }
668 ods_log_assert(zone);
669 ods_log_assert(zone->name);
670 pthread_mutex_lock(&zone->zone_lock);
671 pthread_mutex_lock(&xfrd->rw_lock);
672 pthread_mutex_lock(&xfrd->serial_lock);
673 /* mark end packet */
674 fd = ods_fopen(xfrfile, NULL, "a");
675 free((void*)xfrfile);
676 if (fd) {
677 fprintf(fd, ";;ENDPACKET\n");
678 ods_fclose(fd);
679 } else {
680 pthread_mutex_unlock(&xfrd->rw_lock);
681 pthread_mutex_unlock(&zone->zone_lock);
682 pthread_mutex_unlock(&xfrd->serial_lock);
683 ods_log_crit("[%s] unable to commit xfr zone %s: ods_fopen() failed "
684 "(%s)", xfrd_str, zone->name, strerror(errno));
685 return;
686 }
687 /* update soa serial management */
688 xfrd->serial_disk = xfrd->msg_new_serial;
689 serial_disk_acq = xfrd->serial_disk_acquired;
690 xfrd->serial_disk_acquired = xfrd_time(xfrd);
691 /* ensure newer time */
692 if (xfrd->serial_disk_acquired == serial_disk_acq) {
693 xfrd->serial_disk_acquired++;
694 }
695 xfrd->soa.serial = xfrd->serial_disk;
696 if (xfrd->msg_do_retransfer ||
697 (util_serial_gt(xfrd->serial_disk, xfrd->serial_xfr) &&
699 /* reschedule task */
700 int ret = 0;
701 xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
702 engine_type* engine = (engine_type*) xfrhandler->engine;
703 ods_log_assert(xfrhandler);
704 ods_log_assert(engine);
705 ods_log_debug("[%s] reschedule task for zone %s: disk serial=%u "
706 "acquired=%lu, memory serial=%u acquired=%lu", xfrd_str,
707 zone->name, xfrd->serial_disk,
708 (unsigned long)xfrd->serial_disk_acquired, xfrd->serial_xfr,
709 (unsigned long)xfrd->serial_xfr_acquired);
710 schedule_scheduletask(engine->taskq, TASK_FORCEREAD, zone->name, zone, &zone->zone_lock, schedule_IMMEDIATELY);
711 engine_wakeup_workers(engine);
712 }
713 /* reset retransfer */
714 xfrd->msg_do_retransfer = 0;
715
716 pthread_mutex_unlock(&xfrd->serial_lock);
717 pthread_mutex_unlock(&xfrd->rw_lock);
718 pthread_mutex_unlock(&zone->zone_lock);
719}
720
721
726static void
727xfrd_dump_packet(xfrd_type* xfrd, buffer_type* buffer)
728{
729 zone_type* zone = NULL;
730 char* xfrfile = NULL;
731 FILE* fd = NULL;
732 ldns_pkt* pkt = NULL;
733 ldns_status status = LDNS_STATUS_OK;
734 ods_log_assert(buffer);
735 ods_log_assert(xfrd);
736 zone = (zone_type*) xfrd->zone;
737 ods_log_assert(zone);
738 ods_log_assert(zone->name);
739 status = ldns_wire2pkt(&pkt, buffer_begin(buffer), buffer_limit(buffer));
740 if (status != LDNS_STATUS_OK) {
741 ods_log_crit("[%s] unable to dump packet zone %s: ldns_wire2pkt() "
742 "failed (%s)", xfrd_str, zone->name,
743 ldns_get_errorstr_by_id(status));
744 return;
745 }
746 ods_log_assert(pkt);
747 xfrfile = ods_build_path(zone->name, ".xfrd", 0, 1);
748 if (!xfrfile) {
749 ods_log_crit("[%s] unable to dump packet zone %s: build path failed",
750 xfrd_str, zone->name);
751 return;
752 }
753 pthread_mutex_lock(&xfrd->rw_lock);
754 if (xfrd->msg_do_retransfer && !xfrd->msg_seq_nr && !xfrd->msg_is_ixfr) {
755 fd = ods_fopen(xfrfile, NULL, "w");
756 } else {
757 fd = ods_fopen(xfrfile, NULL, "a");
758 }
759 free((void*) xfrfile);
760 if (!fd) {
761 ods_log_crit("[%s] unable to dump packet zone %s: ods_fopen() failed "
762 "(%s)", xfrd_str, zone->name, strerror(errno));
763 pthread_mutex_unlock(&xfrd->rw_lock);
764 return;
765 }
766 ods_log_assert(fd);
767 if (xfrd->msg_seq_nr == 0) {
768 fprintf(fd, ";;BEGINPACKET\n");
769 }
770 ldns_rr_list_print(fd, ldns_pkt_answer(pkt));
771 ods_fclose(fd);
772 pthread_mutex_unlock(&xfrd->rw_lock);
773 ldns_pkt_free(pkt);
774}
775
776
781static void
782xfrd_write_soa(xfrd_type* xfrd, buffer_type* buffer)
783{
784 zone_type* zone = NULL;
785 size_t rdlength_pos = 0;
786 uint16_t rdlength = 0;
787 ods_log_assert(xfrd);
788 ods_log_assert(buffer);
789 zone = (zone_type*) xfrd->zone;
790 ods_log_assert(zone);
791 ods_log_assert(zone->apex);
792 buffer_write_rdf(buffer, zone->apex);
793 buffer_write_u16(buffer, (uint16_t) LDNS_RR_TYPE_SOA);
794 buffer_write_u16(buffer, (uint16_t) zone->klass);
795 buffer_write_u32(buffer, xfrd->soa.ttl);
796 rdlength_pos = buffer_position(buffer);
797 buffer_skip(buffer, sizeof(rdlength));
798 buffer_write(buffer, xfrd->soa.mname+1, xfrd->soa.mname[0]);
799 buffer_write(buffer, xfrd->soa.rname+1, xfrd->soa.rname[0]);
800 buffer_write_u32(buffer, xfrd->soa.serial);
801 buffer_write_u32(buffer, xfrd->soa.refresh);
802 buffer_write_u32(buffer, xfrd->soa.retry);
803 buffer_write_u32(buffer, xfrd->soa.expire);
804 buffer_write_u32(buffer, xfrd->soa.minimum);
805 rdlength = buffer_position(buffer) - rdlength_pos - sizeof(rdlength);
806 buffer_write_u16_at(buffer, rdlength_pos, rdlength);
807}
808
809
814static void
815xfrd_update_soa(xfrd_type* xfrd, buffer_type* buffer, uint32_t ttl,
816 uint16_t mname_pos, uint16_t rname_pos,
817 uint32_t refresh, uint32_t retry, uint32_t expire, uint32_t minimum)
818{
819 zone_type* zone = NULL;
820 ods_log_assert(xfrd);
821 ods_log_assert(buffer);
822 zone = (zone_type*) xfrd->zone;
823 ods_log_assert(zone);
824 ods_log_assert(zone->apex);
825 xfrd->soa.ttl = ttl;
826 xfrd->soa.refresh = refresh;
827 xfrd->soa.retry = retry;
828 xfrd->soa.expire = expire;
829 xfrd->soa.minimum = minimum;
830 buffer_set_position(buffer, mname_pos);
831 if (!(xfrd->soa.mname[0] =
832 buffer_read_dname(buffer, xfrd->soa.mname+1, 1))) {
833 xfrd->soa.mname[0] = 1;
834 xfrd->soa.mname[1] = 0;
835 }
836 buffer_set_position(buffer, rname_pos);
837 if (!(xfrd->soa.rname[0] =
838 buffer_read_dname(buffer, xfrd->soa.rname+1, 1))) {
839 xfrd->soa.rname[0] = 1;
840 xfrd->soa.rname[1] = 0;
841 }
842}
843
844
849static int
850xfrd_parse_soa(xfrd_type* xfrd, buffer_type* buffer, unsigned rdata_only,
851 unsigned update, uint32_t t, uint32_t* soa_serial)
852{
853 ldns_rr_type type = LDNS_RR_TYPE_SOA;
854 uint16_t mname_pos = 0;
855 uint16_t rname_pos = 0;
856 uint16_t pos = 0;
857 uint32_t serial = 0;
858 uint32_t refresh = 0;
859 uint32_t retry = 0;
860 uint32_t expire = 0;
861 uint32_t minimum = 0;
862 uint32_t ttl = t;
863 ods_log_assert(xfrd);
864 ods_log_assert(buffer);
865
866 /* type class ttl */
867 if (!rdata_only) {
868 if (!buffer_available(buffer, 10)) {
869 ods_log_debug("[%s] unable to parse soa: rr too short",
870 xfrd_str);
871 return 0;
872 }
873 type = (ldns_rr_type) buffer_read_u16(buffer);
874 if (type != LDNS_RR_TYPE_SOA) {
875 ods_log_debug("[%s] unable to parse soa: rrtype %u != soa",
876 xfrd_str, (unsigned) type);
877 return 0;
878 }
879 (void)buffer_read_u16(buffer); /* class */
880 ttl = buffer_read_u32(buffer);
881 /* rdata length */
882 if (!buffer_available(buffer, buffer_read_u16(buffer))) {
883 ods_log_debug("[%s] unable to parse soa: rdata too short",
884 xfrd_str);
885 return 0;
886 }
887 }
888 /* MNAME */
889 mname_pos = buffer_position(buffer);
890 if (!buffer_skip_dname(buffer)) {
891 ods_log_debug("[%s] unable to parse soa: bad mname",
892 xfrd_str);
893 return 0;
894 }
895 /* RNAME */
896 rname_pos = buffer_position(buffer);
897 if (!buffer_skip_dname(buffer)) {
898 ods_log_debug("[%s] unable to parse soa: bad rname",
899 xfrd_str);
900 return 0;
901 }
902 serial = buffer_read_u32(buffer);
903 refresh = buffer_read_u32(buffer);
904 retry = buffer_read_u32(buffer);
905 expire = buffer_read_u32(buffer);
906 minimum = buffer_read_u32(buffer);
907 pos = buffer_position(buffer);
908 if (soa_serial) {
909 *soa_serial = serial;
910 }
911 if (update) {
912 xfrd_update_soa(xfrd, buffer, ttl, mname_pos, rname_pos,
913 refresh, retry, expire, minimum);
914 }
915 buffer_set_position(buffer, pos);
916 return 1;
917}
918
919
924static ods_status
925xfrd_parse_rrs(xfrd_type* xfrd, buffer_type* buffer, uint16_t count,
926 int* done)
927{
928 ldns_rr_type type = 0;
929 uint16_t rrlen = 0;
930 uint32_t ttl = 0;
931 uint32_t serial = 0;
932 uint32_t tmp_serial = 0;
933 size_t i = 0;
934 ods_log_assert(xfrd);
935 ods_log_assert(buffer);
936 ods_log_assert(done);
937 for (i=0; i < count; ++i, ++xfrd->msg_rr_count) {
938 if (*done) {
939 return ODS_STATUS_OK;
940 }
941 if (!buffer_skip_dname(buffer)) {
942 return ODS_STATUS_SKIPDNAME;
943 }
944 if (!buffer_available(buffer, 10)) {
945 return ODS_STATUS_BUFAVAIL;
946 }
947 (void)buffer_position(buffer);
948 type = (ldns_rr_type) buffer_read_u16(buffer);
949 (void)buffer_read_u16(buffer); /* class */
950 ttl = buffer_read_u32(buffer);
951 rrlen = buffer_read_u16(buffer);
952 if (!buffer_available(buffer, rrlen)) {
953 return ODS_STATUS_BUFAVAIL;
954 }
955 if (type == LDNS_RR_TYPE_SOA) {
956 if (!xfrd_parse_soa(xfrd, buffer, 1, 0, ttl, &serial)) {
957 return ODS_STATUS_PARSESOA;
958 }
959 if (xfrd->msg_rr_count == 1 && serial != xfrd->msg_new_serial) {
960 /* 2nd RR is SOA with different serial, this is an IXFR */
961 xfrd->msg_is_ixfr = 1;
962 pthread_mutex_lock(&xfrd->serial_lock);
963 if (!xfrd->serial_disk_acquired) {
964 pthread_mutex_unlock(&xfrd->serial_lock);
965 /* got IXFR but need AXFR */
966 return ODS_STATUS_REQAXFR;
967 }
968 if (!xfrd->msg_do_retransfer && serial != xfrd->serial_disk) {
969 pthread_mutex_unlock(&xfrd->serial_lock);
970 /* bad start serial in IXFR */
971 return ODS_STATUS_INSERIAL;
972 }
973 pthread_mutex_unlock(&xfrd->serial_lock);
974 xfrd->msg_old_serial = serial;
975 tmp_serial = serial;
976 } else if (serial == xfrd->msg_new_serial) {
977 /* saw another SOA of new serial. */
978 if (xfrd->msg_is_ixfr == 1) {
979 xfrd->msg_is_ixfr = 2; /* seen middle SOA in ixfr */
980 } else {
981 *done = 1; /* final axfr/ixfr soa */
982 }
983 } else if (xfrd->msg_is_ixfr) {
984 /* some additional checks */
985 if (util_serial_gt(serial, xfrd->msg_new_serial)) {
986 /* bad middle serial in IXFR (too high) */
987 return ODS_STATUS_INSERIAL;
988 }
989 if (util_serial_gt(tmp_serial, serial)) {
990 /* middle serial decreases in IXFR */
991 return ODS_STATUS_INSERIAL;
992 }
993 /* serial ok, update tmp serial */
994 tmp_serial = serial;
995 }
996 } else {
997 buffer_skip(buffer, rrlen);
998 }
999 }
1000 return ODS_STATUS_OK;
1001}
1002
1003
1008static xfrd_pkt_status
1009xfrd_parse_packet(xfrd_type* xfrd, buffer_type* buffer)
1010{
1011 zone_type* zone = NULL;
1012 uint16_t qdcount = 0;
1013 uint16_t ancount = 0;
1014 uint16_t ancount_todo = 0;
1015 uint16_t rrcount = 0;
1016 uint32_t serial = 0;
1017 int done = 0;
1018 ods_status status = ODS_STATUS_OK;
1019 ods_log_assert(buffer);
1020 ods_log_assert(xfrd);
1021 ods_log_assert(xfrd->master);
1022 ods_log_assert(xfrd->master->address);
1023 zone = (zone_type*) xfrd->zone;
1024 ods_log_assert(zone);
1025 ods_log_assert(zone->name);
1026 /* check packet size */
1028 ods_log_error("[%s] unable to parse packet: zone %s received bad "
1029 "packet from %s (too small)", xfrd_str, zone->name,
1030 xfrd->master->address);
1031 return XFRD_PKT_BAD;
1032 }
1033 /* check query id */
1034 if (buffer_pkt_id(buffer) != xfrd->query_id) {
1035 ods_log_error("[%s] bad packet: zone %s received bad query id "
1036 "%u from %s (expected %u)", xfrd_str, zone->name,
1037 buffer_pkt_id(buffer), xfrd->master->address, xfrd->query_id);
1038 return XFRD_PKT_BAD;
1039 }
1040 /* check rcode */
1041 if (buffer_pkt_rcode(buffer) != LDNS_RCODE_NOERROR) {
1042 ods_log_error("[%s] bad packet: zone %s received error code %s from %s",
1043 xfrd_str, zone->name, ldns_pkt_rcode2str(buffer_pkt_rcode(buffer)),
1044 xfrd->master->address);
1045 if (buffer_pkt_rcode(buffer) == LDNS_RCODE_NOTIMPL) {
1046 return XFRD_PKT_NOTIMPL;
1047 } else if (buffer_pkt_rcode(buffer) != LDNS_RCODE_NOTAUTH) {
1048 return XFRD_PKT_BAD;
1049 }
1050 }
1051 /* check tsig */
1052 if (!xfrd_tsig_process(xfrd, buffer)) {
1053 ods_log_error("[%s] bad packet: zone %s received bad tsig "
1054 "from %s", xfrd_str, zone->name, xfrd->master->address);
1055 return XFRD_PKT_BAD;
1056 }
1057 /* skip header and question section */
1059 qdcount = buffer_pkt_qdcount(buffer);
1060 for (rrcount = 0; rrcount < qdcount; rrcount++) {
1061 if (!buffer_skip_rr(buffer, 1)) {
1062 ods_log_error("[%s] bad packet: zone %s received bad "
1063 "question section from %s (bad rr)", xfrd_str, zone->name,
1064 xfrd->master->address);
1065 return XFRD_PKT_BAD;
1066 }
1067 }
1068 /* answer section */
1069 ancount = buffer_pkt_ancount(buffer);
1070 if (xfrd->msg_rr_count == 0 && ancount == 0) {
1071 if (xfrd->tcp_conn == -1 && buffer_pkt_tc(buffer)) {
1072 ods_log_info("[%s] zone %s received tc from %s, retry tcp",
1073 xfrd_str, zone->name, xfrd->master->address);
1074 return XFRD_PKT_TC;
1075 }
1076 ods_log_error("[%s] bad packet: zone %s received bad xfr packet "
1077 "from %s (nodata)", xfrd_str, zone->name, xfrd->master->address);
1078 return XFRD_PKT_BAD;
1079 }
1080
1081 ancount_todo = ancount;
1082 if (xfrd->msg_rr_count == 0) {
1083 /* parse the first RR, see if it is a SOA */
1084 if (!buffer_skip_dname(buffer) ||
1085 !xfrd_parse_soa(xfrd, buffer, 0, 1, 0, &serial)) {
1086 ods_log_error("[%s] bad packet: zone %s received bad xfr "
1087 "packet from %s (bad soa)", xfrd_str, zone->name,
1088 xfrd->master->address);
1089 return XFRD_PKT_BAD;
1090 }
1091 /* check serial */
1092 pthread_mutex_lock(&xfrd->serial_lock);
1093 if (!xfrd->msg_do_retransfer &&
1094 xfrd->serial_disk_acquired && xfrd->serial_disk == serial) {
1095 ods_log_info("[%s] zone %s got update indicating current "
1096 "serial %u from %s", xfrd_str, zone->name, serial,
1097 xfrd->master->address);
1098 xfrd->serial_disk_acquired = xfrd_time(xfrd);
1099 if (xfrd->serial_xfr == serial) {
1100 xfrd->serial_xfr_acquired = time_now();
1101 if (!xfrd->serial_notify_acquired) {
1102 /* not notified or anything, so stop asking around */
1103 xfrd->round_num = -1; /* next try start a new round */
1105 ods_log_debug("[%s] zone %s wait refresh time", xfrd_str,
1106 zone->name);
1107 pthread_mutex_unlock(&xfrd->serial_lock);
1108 return XFRD_PKT_NEWLEASE;
1109 }
1110 /* try next master */
1111 ods_log_debug("[%s] zone %s try next master", xfrd_str,
1112 zone->name);
1113 pthread_mutex_unlock(&xfrd->serial_lock);
1114 return XFRD_PKT_BAD;
1115 }
1116 }
1117 if (!xfrd->msg_do_retransfer && xfrd->serial_disk_acquired &&
1118 !util_serial_gt(serial, xfrd->serial_disk)) {
1119 ods_log_info("[%s] zone %s ignoring old serial %u from %s "
1120 "(have %u)", xfrd_str, zone->name, serial,
1121 xfrd->master->address, xfrd->serial_disk);
1122 pthread_mutex_unlock(&xfrd->serial_lock);
1123 return XFRD_PKT_BAD;
1124 }
1125
1126 xfrd->msg_new_serial = serial;
1127 if (!xfrd->msg_do_retransfer && xfrd->serial_disk_acquired) {
1128 xfrd->msg_old_serial = xfrd->serial_disk;
1129 } else {
1130 xfrd->msg_old_serial = 0;
1131 }
1132 /* update notify serial if this xfr is newer */
1133 if (ancount > 1 && xfrd->serial_notify_acquired &&
1134 util_serial_gt(serial, xfrd->serial_notify)) {
1135 xfrd->serial_notify = serial;
1136 }
1137 pthread_mutex_unlock(&xfrd->serial_lock);
1138 xfrd->msg_rr_count = 1;
1139 xfrd->msg_is_ixfr = 0;
1140 ancount_todo = ancount - 1;
1141 }
1142 /* check tc bit */
1143 if (xfrd->tcp_conn == -1 && buffer_pkt_tc(buffer)) {
1144 ods_log_info("[%s] zone %s received tc from %s, retry tcp",
1145 xfrd_str, zone->name, xfrd->master->address);
1146 return XFRD_PKT_TC;
1147 }
1148 if (xfrd->tcp_conn == -1 && ancount < 2) {
1149 /* too short to be a real ixfr/axfr data transfer */
1150 ods_log_info("[%s] zone %s received too short udp reply from %s, "
1151 "retry tcp", xfrd_str, zone->name, xfrd->master->address);
1152 return XFRD_PKT_TC;
1153 }
1154 status = xfrd_parse_rrs(xfrd, buffer, ancount_todo, &done);
1155 if (status != ODS_STATUS_OK) {
1156 ods_log_error("[%s] bad packet: zone %s received bad xfr packet "
1157 "from %s (%s)", xfrd_str, zone->name, xfrd->master->address,
1158 ods_status2str(status));
1159 return XFRD_PKT_BAD;
1160 }
1161 if (xfrd->tcp_conn == -1 && !done) {
1162 ods_log_error("[%s] bad packet: zone %s received bad xfr packet "
1163 "(xfr over udp incomplete)", xfrd_str, zone->name);
1164 return XFRD_PKT_BAD;
1165 }
1166 if (!done) {
1167 return XFRD_PKT_MORE;
1168 }
1169 return XFRD_PKT_XFR;
1170}
1171
1172
1177static xfrd_pkt_status
1178xfrd_handle_packet(xfrd_type* xfrd, buffer_type* buffer)
1179{
1181 zone_type* zone = NULL;
1182 ods_log_assert(xfrd);
1183 ods_log_assert(xfrd->master);
1184 ods_log_assert(xfrd->master->address);
1185 zone = (zone_type*) xfrd->zone;
1186 ods_log_assert(zone);
1187 ods_log_assert(zone->name);
1188 res = xfrd_parse_packet(xfrd, buffer);
1189 ods_log_debug("[%s] zone %s xfr packet parsed (res %d)", xfrd_str,
1190 zone->name, res);
1191
1192 switch (res) {
1193 case XFRD_PKT_MORE:
1194 case XFRD_PKT_XFR:
1195 /* continue with commit */
1196 break;
1197 case XFRD_PKT_NEWLEASE:
1198 xfrd->serial_notify_acquired = 0;
1199 case XFRD_PKT_TC:
1200 return res;
1201 break;
1202 case XFRD_PKT_NOTIMPL:
1203 case XFRD_PKT_BAD:
1204 default:
1205 /* rollback */
1206 if (xfrd->msg_seq_nr > 0) {
1207 buffer_clear(buffer);
1208 ods_log_info("[%s] zone %s xfr rollback", xfrd_str,
1209 zone->name);
1210 buffer_flip(buffer);
1211 }
1212 return res;
1213 break;
1214 }
1215 /* dump reply on disk to diff file */
1216 xfrd_dump_packet(xfrd, buffer);
1217 /* more? */
1218 xfrd->msg_seq_nr++;
1219 if (res == XFRD_PKT_MORE) {
1220 /* wait for more */
1221 return XFRD_PKT_MORE;
1222 }
1223 /* done */
1224 buffer_clear(buffer);
1225 buffer_flip(buffer);
1226 /* commit packet */
1227 xfrd_commit_packet(xfrd);
1228 /* next time */
1229 pthread_mutex_lock(&xfrd->serial_lock);
1230
1231 ods_log_info("[%s] zone %s transfer done [notify acquired %lu, serial on "
1232 "disk %u, notify serial %u]", xfrd_str, zone->name,
1233 (unsigned long)xfrd->serial_notify_acquired, xfrd->serial_disk,
1234 xfrd->serial_notify);
1235
1236 if (xfrd->serial_notify_acquired &&
1237 !util_serial_gt(xfrd->serial_notify, xfrd->serial_disk)) {
1238 ods_log_verbose("[%s] zone %s reset notify acquired", xfrd_str,
1239 zone->name);
1240 xfrd->serial_notify_acquired = 0;
1241 }
1242 if (!xfrd->serial_notify_acquired) {
1243 ods_log_debug("[%s] zone %s xfr done", xfrd_str, zone->name);
1244 xfrd->round_num = -1; /* next try start anew */
1246 pthread_mutex_unlock(&xfrd->serial_lock);
1247 return XFRD_PKT_XFR;
1248 }
1249 pthread_mutex_unlock(&xfrd->serial_lock);
1250 /* try to get an even newer serial */
1251 ods_log_info("[%s] zone %s try get newer serial", xfrd_str, zone->name);
1252 return XFRD_PKT_BAD;
1253}
1254
1255
1263static void
1264xfrd_tcp_write(xfrd_type* xfrd, tcp_set_type* set)
1265{
1266 zone_type* zone = NULL;
1267 tcp_conn_type* tcp = NULL;
1268 int ret = 0;
1269 int error = 0;
1270 socklen_t len = 0;
1271
1272 ods_log_assert(set);
1273 ods_log_assert(xfrd);
1274 ods_log_assert(xfrd->tcp_conn != -1);
1275 zone = (zone_type*) xfrd->zone;
1276 ods_log_assert(zone);
1277 ods_log_assert(zone->name);
1278 tcp = set->tcp_conn[xfrd->tcp_conn];
1279 if (tcp->total_bytes == 0) {
1280 /* check for pending error from nonblocking connect */
1281 /* from Stevens, unix network programming, vol1, 3rd ed, p450 */
1282 len = sizeof(error);
1283 if (getsockopt(tcp->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1284 error = errno; /* on solaris errno is error */
1285 }
1286 if (error == EINPROGRESS || error == EWOULDBLOCK) {
1287 ods_log_debug("[%s] zone %s zero write, write again later (%s)",
1288 xfrd_str, zone->name, strerror(error));
1289 return; /* try again later */
1290 }
1291 if (error != 0) {
1292 ods_log_error("[%s] zone %s cannot tcp connect to %s: %s",
1293 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1294 xfrd_set_timer_now(xfrd);
1295 xfrd_tcp_release(xfrd, set, 1);
1296 return;
1297 }
1298 }
1299 ret = tcp_conn_write(tcp);
1300 if(ret == -1) {
1301 ods_log_error("[%s] zone %s cannot tcp write to %s: %s",
1302 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1303 xfrd_set_timer_now(xfrd);
1304 xfrd_tcp_release(xfrd, set, 1);
1305 return;
1306 }
1307 if (ret == 0) {
1308 ods_log_debug("[%s] zone %s zero write, write again later",
1309 xfrd_str, zone->name);
1310 return; /* write again later */
1311 }
1312 /* done writing, get ready for reading */
1313 ods_log_debug("[%s] zone %s done writing, get ready for reading",
1314 xfrd_str, zone->name);
1315 tcp->is_reading = 1;
1316 tcp_conn_ready(tcp);
1318 xfrd_tcp_read(xfrd, set);
1319}
1320
1321
1326static int
1327xfrd_tcp_open(xfrd_type* xfrd, tcp_set_type* set)
1328{
1329 int fd, family, conn;
1330 struct sockaddr_storage to;
1331 socklen_t to_len;
1332 zone_type* zone = NULL;
1333
1334 ods_log_assert(set);
1335 ods_log_assert(xfrd);
1336 ods_log_assert(xfrd->tcp_conn != -1);
1337 ods_log_assert(xfrd->master);
1338 ods_log_assert(xfrd->master->address);
1339 zone = (zone_type*) xfrd->zone;
1340 ods_log_assert(zone);
1341 ods_log_assert(zone->name);
1342 ods_log_debug("[%s] zone %s open tcp connection to %s", xfrd_str,
1343 zone->name, xfrd->master->address);
1344 set->tcp_conn[xfrd->tcp_conn]->is_reading = 0;
1345 set->tcp_conn[xfrd->tcp_conn]->total_bytes = 0;
1346 set->tcp_conn[xfrd->tcp_conn]->msglen = 0;
1347 if (xfrd->master->family == AF_INET6) {
1348 family = PF_INET6;
1349 } else {
1350 family = PF_INET;
1351 }
1352 fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
1353 set->tcp_conn[xfrd->tcp_conn]->fd = fd;
1354 if (fd == -1) {
1355 ods_log_error("[%s] zone %s cannot create tcp socket to %s: %s",
1356 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1357 xfrd_set_timer_now(xfrd);
1358 xfrd_tcp_release(xfrd, set, 0);
1359 return 0;
1360 }
1361 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
1362 ods_log_error("[%s] zone %s cannot fcntl tcp socket: %s",
1363 xfrd_str, zone->name, strerror(errno));
1364 xfrd_set_timer_now(xfrd);
1365 xfrd_tcp_release(xfrd, set, 0);
1366 return 0;
1367 }
1368 to_len = xfrd_acl_sockaddr_to(xfrd->master, &to);
1369 /* bind it */
1370 interface_type interface = xfrd->xfrhandler->engine->dnshandler->interfaces->interfaces[0];
1371 if (!interface.address) {
1372 ods_log_error("[%s] unable to get the address of interface", xfrd_str);
1373 return -1;
1374 }
1375 if (acl_parse_family(interface.address) == AF_INET) {
1376 struct sockaddr_in addr;
1377 addr.sin_family = acl_parse_family(interface.address);
1378 addr.sin_addr = interface.addr.addr;
1379 addr.sin_port = 0;
1380 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
1381 ods_log_error("[%s] unable to bind address %s: bind failed %s", xfrd_str, interface.address, strerror(errno));
1382 return -1;
1383 }
1384 }
1385 else {
1386 struct sockaddr_in6 addr6;
1387 addr6.sin6_family = acl_parse_family(interface.address);
1388 addr6.sin6_addr = interface.addr.addr6;
1389 addr6.sin6_port = 0;
1390 if (bind(fd, (struct sockaddr *) &addr6, sizeof(addr6)) != 0) {
1391 ods_log_error("[%s] unable to bind address %s: bind failed %s", xfrd_str, interface.address, strerror(errno));
1392 return -1;
1393 }
1394 }
1395
1396 conn = connect(fd, (struct sockaddr*)&to, to_len);
1397 if (conn == -1 && errno != EINPROGRESS) {
1398 ods_log_error("[%s] zone %s cannot connect tcp socket to %s: %s",
1399 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1400 xfrd_set_timer_now(xfrd);
1401 xfrd_tcp_release(xfrd, set, 0);
1402 return 0;
1403 }
1404 xfrd->handler.fd = fd;
1406 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1407 return 1;
1408}
1409
1410
1415static void
1416xfrd_tcp_obtain(xfrd_type* xfrd, tcp_set_type* set)
1417{
1418 xfrhandler_type* xfrhandler;
1419 int i = 0;
1420
1421 ods_log_assert(set);
1422 ods_log_assert(xfrd);
1423 ods_log_assert(xfrd->tcp_conn == -1);
1424 ods_log_assert(xfrd->tcp_waiting == 0);
1425 if (set->tcp_count < TCPSET_MAX) {
1426 ods_log_assert(!set->tcp_waiting_first);
1427 set->tcp_count ++;
1428 /* find a free tcp_buffer */
1429 for (i=0; i < TCPSET_MAX; i++) {
1430 if (set->tcp_conn[i]->fd == -1) {
1431 xfrd->tcp_conn = i;
1432 break;
1433 }
1434 }
1435 ods_log_assert(xfrd->tcp_conn != -1);
1436 xfrd->tcp_waiting = 0;
1437 /* stop udp use (if any) */
1438 if (xfrd->handler.fd != -1) {
1439 xfrd_udp_release(xfrd);
1440 }
1441 if (!xfrd_tcp_open(xfrd, set)) {
1442 return;
1443 }
1444 xfrd_tcp_xfr(xfrd, set);
1445 return;
1446 }
1447 /* wait, at end of line */
1448 ods_log_verbose("[%s] max number of tcp connections (%d) reached",
1449 xfrd_str, TCPSET_MAX);
1450 xfrd->tcp_waiting = 1;
1451 xfrd_unset_timer(xfrd);
1452
1453 /* add it to the waiting queue */
1454 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1455 xfrd->tcp_waiting_next = xfrhandler->tcp_waiting_first;
1456 xfrhandler->tcp_waiting_first = xfrd;
1457}
1458
1459
1464static void
1465xfrd_tcp_xfr(xfrd_type* xfrd, tcp_set_type* set)
1466{
1467 tcp_conn_type* tcp = NULL;
1468 zone_type* zone = NULL;
1469
1470 ods_log_assert(set);
1471 ods_log_assert(xfrd);
1472 zone = (zone_type*) xfrd->zone;
1473 ods_log_assert(zone);
1474 ods_log_assert(zone->name);
1475 ods_log_assert(xfrd->tcp_conn != -1);
1476 ods_log_assert(xfrd->tcp_waiting == 0);
1477 ods_log_assert(xfrd->master);
1478 ods_log_assert(xfrd->master->address);
1479 /* start AXFR or IXFR for the zone */
1480 tcp = set->tcp_conn[xfrd->tcp_conn];
1481
1482 if (xfrd->msg_do_retransfer || xfrd->serial_xfr_acquired <= 0 ||
1483 xfrd->master->ixfr_disabled) {
1484 ods_log_info("[%s] zone %s request axfr to %s", xfrd_str,
1485 zone->name, xfrd->master->address);
1486 buffer_pkt_query(tcp->packet, zone->apex, LDNS_RR_TYPE_AXFR,
1487 zone->klass);
1488 } else {
1489 ods_log_info("[%s] zone %s request tcp/ixfr=%u to %s", xfrd_str,
1490 zone->name, xfrd->soa.serial, xfrd->master->address);
1491 buffer_pkt_query(tcp->packet, zone->apex, LDNS_RR_TYPE_IXFR,
1492 zone->klass);
1494 xfrd_write_soa(xfrd, tcp->packet);
1495 }
1496 /* make packet */
1497 xfrd->query_id = buffer_pkt_id(tcp->packet);
1498 xfrd->msg_seq_nr = 0;
1499 xfrd->msg_rr_count = 0;
1500 xfrd->msg_old_serial = 0;
1501 xfrd->msg_new_serial = 0;
1502 xfrd->msg_is_ixfr = 0;
1503 xfrd_tsig_sign(xfrd, tcp->packet);
1504 buffer_flip(tcp->packet);
1505 tcp->msglen = buffer_limit(tcp->packet);
1506 ods_log_verbose("[%s] zone %s sending tcp query id=%d", xfrd_str,
1507 zone->name, xfrd->query_id);
1508 /* wait for select to complete connect before write */
1509}
1510
1511
1516static void
1517xfrd_tcp_read(xfrd_type* xfrd, tcp_set_type* set)
1518{
1519 tcp_conn_type* tcp = NULL;
1520 int ret = 0;
1521
1522 ods_log_assert(set);
1523 ods_log_assert(xfrd);
1524 ods_log_assert(xfrd->tcp_conn != -1);
1525 tcp = set->tcp_conn[xfrd->tcp_conn];
1526 ret = tcp_conn_read(tcp);
1527 if (ret == -1) {
1528 xfrd_set_timer_now(xfrd);
1529 xfrd_tcp_release(xfrd, set, 1);
1530 return;
1531 }
1532 if (ret == 0) {
1533 return;
1534 }
1535 /* completed msg */
1536 buffer_flip(tcp->packet);
1537 ret = xfrd_handle_packet(xfrd, tcp->packet);
1538 switch (ret) {
1539 case XFRD_PKT_MORE:
1540 tcp_conn_ready(tcp);
1541 break;
1542 case XFRD_PKT_XFR:
1543 case XFRD_PKT_NEWLEASE:
1544 ods_log_verbose("[%s] tcp read %s: release connection", xfrd_str,
1545 XFRD_PKT_XFR?"xfr":"newlease");
1546 xfrd_tcp_release(xfrd, set, 1);
1547 ods_log_assert(xfrd->round_num == -1);
1548 break;
1549 case XFRD_PKT_NOTIMPL:
1550 xfrd->master->ixfr_disabled = time_now();
1551 ods_log_verbose("[%s] disable ixfr requests for %s from now (%lu)",
1552 xfrd_str, xfrd->master->address, (unsigned long)xfrd->master->ixfr_disabled);
1553 /* break; */
1554 case XFRD_PKT_BAD:
1555 default:
1556 ods_log_debug("[%s] tcp read %s: release connection", xfrd_str,
1557 ret==XFRD_PKT_BAD?"bad":"notimpl");
1558 xfrd_tcp_release(xfrd, set, 1);
1559 xfrd_make_request(xfrd);
1560 break;
1561 }
1562}
1563
1564
1570static void
1571xfrd_tcp_release(xfrd_type* xfrd, tcp_set_type* set, int open_waiting)
1572{
1573 xfrhandler_type* xfrhandler;
1574 int conn = 0;
1575 zone_type* zone = NULL;
1576
1577 ods_log_assert(set);
1578 ods_log_assert(xfrd);
1579 ods_log_assert(xfrd->master);
1580 ods_log_assert(xfrd->master->address);
1581 ods_log_assert(xfrd->tcp_conn != -1);
1582 ods_log_assert(xfrd->tcp_waiting == 0);
1583 zone = (zone_type*) xfrd->zone;
1584 ods_log_debug("[%s] zone %s release tcp connection to %s", xfrd_str,
1585 zone->name, xfrd->master->address);
1586 conn = xfrd->tcp_conn;
1587 xfrd->tcp_conn = -1;
1588 xfrd->tcp_waiting = 0;
1589 xfrd->handler.fd = -1;
1591
1592 if (set->tcp_conn[conn]->fd != -1) {
1593 close(set->tcp_conn[conn]->fd);
1594 }
1595 set->tcp_conn[conn]->fd = -1;
1596 set->tcp_count --;
1597
1598 /* see if there are any connections waiting for a slot. Or return. */
1599 if (!open_waiting) return;
1600 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1601 while (xfrhandler->tcp_waiting_first && set->tcp_count < TCPSET_MAX) {
1602 int i;
1603 xfrd_type* waiting_xfrd = xfrhandler->tcp_waiting_first;
1604 xfrhandler->tcp_waiting_first = waiting_xfrd->tcp_waiting_next;
1605 waiting_xfrd->tcp_waiting_next = NULL;
1606
1607 /* find a free tcp_buffer */
1608 for (i=0; i < TCPSET_MAX; i++) {
1609 if (set->tcp_conn[i]->fd == -1) {
1610 waiting_xfrd->tcp_conn = i;
1611 set->tcp_count++;
1612 break;
1613 }
1614 }
1615 waiting_xfrd->tcp_waiting = 0;
1616 /* stop udp use (if any) */
1617 if (waiting_xfrd->handler.fd != -1) {
1618 xfrd_udp_release(waiting_xfrd);
1619 }
1620 /* if xfrd_tcp_open() fails its slot in set->tcp_conn[]
1621 * is released. Continue to next. We don't put it back in the
1622 * waiting queue, it would keep the signer busy retrying, making
1623 * things only worse. */
1624 if (xfrd_tcp_open(waiting_xfrd, set)) {
1625 xfrd_tcp_xfr(waiting_xfrd, set);
1626 }
1627 }
1628}
1629
1630
1638static int
1639xfrd_udp_send(xfrd_type* xfrd, buffer_type* buffer)
1640{
1641 struct sockaddr_storage to;
1642 socklen_t to_len = 0;
1643 int fd = -1;
1644 int family = PF_INET;
1645 ssize_t nb = -1;
1646 ods_log_assert(buffer);
1647 ods_log_assert(xfrd);
1648 ods_log_assert(xfrd->master);
1649 ods_log_assert(xfrd->master->address);
1650 /* this will set the remote port to acl->port or TCP_PORT */
1651 to_len = xfrd_acl_sockaddr_to(xfrd->master, &to);
1652 /* get the address family of the remote host */
1653 if (xfrd->master->family == AF_INET6) {
1654 family = PF_INET6;
1655 }
1656 /* create socket */
1657 fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1658 if (fd == -1) {
1659 ods_log_error("[%s] unable to send data over udp to %s: "
1660 "socket() failed (%s)", xfrd_str, xfrd->master->address,
1661 strerror(errno));
1662 return -1;
1663 }
1664 /* bind it? */
1665
1666 /* send it (udp) */
1667 ods_log_deeebug("[%s] send %lu bytes over udp to %s", xfrd_str,
1668 (unsigned long)buffer_remaining(buffer), xfrd->master->address);
1669 nb = sendto(fd, buffer_current(buffer), buffer_remaining(buffer), 0,
1670 (struct sockaddr*)&to, to_len);
1671 if (nb == -1) {
1672 ods_log_error("[%s] unable to send data over udp to %s: "
1673 "sendto() failed (%s)", xfrd_str, xfrd->master->address,
1674 strerror(errno));
1675 close(fd);
1676 return -1;
1677 }
1678 return fd;
1679}
1680
1681
1686static int
1687xfrd_udp_send_request_ixfr(xfrd_type* xfrd)
1688{
1689 int fd;
1690 xfrhandler_type* xfrhandler = NULL;
1691 zone_type* zone = NULL;
1692 ods_log_assert(xfrd);
1693 ods_log_assert(xfrd->master);
1694 ods_log_assert(xfrd->master->address);
1695 zone = (zone_type*) xfrd->zone;
1696 ods_log_assert(zone);
1697 ods_log_assert(zone->name);
1698 if (xfrd->tcp_conn != -1) {
1699 /* tcp is using the handler.fd */
1700 ods_log_error("[%s] unable to transfer zone %s: tried to send "
1701 "udp while tcp obtained", xfrd_str, zone->name);
1702 return -1;
1703 }
1704 /* make packet */
1705 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1706 ods_log_assert(xfrhandler);
1707 buffer_pkt_query(xfrhandler->packet, zone->apex, LDNS_RR_TYPE_IXFR,
1708 zone->klass);
1709 xfrd->query_id = buffer_pkt_id(xfrhandler->packet);
1710 xfrd->msg_seq_nr = 0;
1711 xfrd->msg_rr_count = 0;
1712 xfrd->msg_old_serial = 0;
1713 xfrd->msg_new_serial = 0;
1714 xfrd->msg_is_ixfr = 0;
1715 buffer_pkt_set_nscount(xfrhandler->packet, 1);
1716 xfrd_write_soa(xfrd, xfrhandler->packet);
1717 xfrd_tsig_sign(xfrd, xfrhandler->packet);
1718 buffer_flip(xfrhandler->packet);
1719 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_UDP_TIMEOUT);
1720 ods_log_info("[%s] zone %s request udp/ixfr=%u to %s", xfrd_str,
1721 zone->name, xfrd->soa.serial, xfrd->master->address);
1722 if((fd = xfrd_udp_send(xfrd, xfrhandler->packet)) == -1) {
1723 return -1;
1724 }
1725 return fd;
1726}
1727
1732static void
1733xfrd_udp_obtain(xfrd_type* xfrd)
1734{
1735 xfrhandler_type* xfrhandler = NULL;
1736 ods_log_assert(xfrd);
1737 ods_log_assert(xfrd->xfrhandler);
1738 ods_log_assert(xfrd->udp_waiting == 0);
1739 xfrhandler = (void*) xfrd->xfrhandler;
1740 if (xfrd->tcp_conn != -1) {
1741 /* no tcp and udp at the same time */
1742 xfrd_tcp_release(xfrd, xfrhandler->tcp_set, 1);
1743 }
1744 if (xfrhandler->udp_use_num < XFRD_MAX_UDP) {
1745 xfrhandler->udp_use_num++;
1746 xfrd->handler.fd = xfrd_udp_send_request_ixfr(xfrd);
1747 if (xfrd->handler.fd == -1) {
1748 xfrhandler->udp_use_num--;
1749 }
1750 return;
1751 }
1752 /* queue the zone as last */
1753 xfrd->udp_waiting = 1;
1754 xfrd->udp_waiting_next = NULL;
1755 if (!xfrhandler->udp_waiting_first) {
1756 xfrhandler->udp_waiting_first = xfrd;
1757 }
1758 if (xfrhandler->udp_waiting_last) {
1759 xfrhandler->udp_waiting_last->udp_waiting_next = xfrd;
1760 }
1761 xfrhandler->udp_waiting_last = xfrd;
1762 xfrd_unset_timer(xfrd);
1763}
1764
1765
1770static int
1771xfrd_udp_read_packet(xfrd_type* xfrd)
1772{
1773 xfrhandler_type* xfrhandler = NULL;
1774 ssize_t received = 0;
1775 ods_log_assert(xfrd);
1776 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1777 ods_log_assert(xfrhandler);
1778 /* read the data */
1779 buffer_clear(xfrhandler->packet);
1780 received = recvfrom(xfrd->handler.fd, buffer_begin(xfrhandler->packet),
1781 buffer_remaining(xfrhandler->packet), 0, NULL, NULL);
1782 if (received == -1) {
1783 ods_log_error("[%s] unable to read packet: recvfrom() failed fd %d "
1784 "(%s)", xfrd_str, xfrd->handler.fd, strerror(errno));
1785 return 0;
1786 }
1787 buffer_set_limit(xfrhandler->packet, received);
1788 return 1;
1789}
1790
1791
1796static void
1797xfrd_udp_read(xfrd_type* xfrd)
1798{
1799 xfrhandler_type* xfrhandler = NULL;
1800 zone_type* zone = NULL;
1802 ods_log_assert(xfrd);
1803 zone = (zone_type*) xfrd->zone;
1804 ods_log_assert(zone);
1805 ods_log_assert(zone->name);
1806 ods_log_debug("[%s] zone %s read data from udp", xfrd_str,
1807 zone->name);
1808 if (!xfrd_udp_read_packet(xfrd)) {
1809 ods_log_error("[%s] unable to read data from udp zone %s: "
1810 "xfrd_udp_read_packet() failed", xfrd_str, zone->name);
1811 xfrd_udp_release(xfrd);
1812 return;
1813 }
1814 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1815 ods_log_assert(xfrhandler);
1816 res = xfrd_handle_packet(xfrd, xfrhandler->packet);
1817 switch (res) {
1818 case XFRD_PKT_TC:
1819 ods_log_verbose("[%s] truncation from %s",
1820 xfrd_str, xfrd->master->address);
1821 xfrd_udp_release(xfrd);
1822 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1823 xfrd_tcp_obtain(xfrd, xfrhandler->tcp_set);
1824 break;
1825 case XFRD_PKT_XFR:
1826 case XFRD_PKT_NEWLEASE:
1827 ods_log_verbose("[%s] xfr/newlease from %s",
1828 xfrd_str, xfrd->master->address);
1829 /* nothing more to do */
1830 ods_log_assert(xfrd->round_num == -1);
1831 xfrd_udp_release(xfrd);
1832 break;
1833 case XFRD_PKT_NOTIMPL:
1834 xfrd->master->ixfr_disabled = time_now();
1835 ods_log_verbose("[%s] disable ixfr requests for %s from now (%lu)",
1836 xfrd_str, xfrd->master->address, (unsigned long)xfrd->master->ixfr_disabled);
1837 /* break; */
1838 case XFRD_PKT_BAD:
1839 default:
1840 ods_log_debug("[%s] bad ixfr packet from %s",
1841 xfrd_str, xfrd->master->address);
1842 xfrd_udp_release(xfrd);
1843 xfrd_make_request(xfrd);
1844 break;
1845 }
1846}
1847
1848
1853static void
1854xfrd_udp_release(xfrd_type* xfrd)
1855{
1856 xfrhandler_type* xfrhandler = NULL;
1857
1858 ods_log_assert(xfrd);
1859 ods_log_assert(xfrd->udp_waiting == 0);
1860 if(xfrd->handler.fd != -1)
1861 close(xfrd->handler.fd);
1862 xfrd->handler.fd = -1;
1863 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1864 ods_log_assert(xfrhandler);
1865 /* see if there are waiting zones */
1866 if (xfrhandler->udp_use_num == XFRD_MAX_UDP) {
1867 while (xfrhandler->udp_waiting_first) {
1868 /* snip off waiting list */
1869 xfrd_type* wf = xfrhandler->udp_waiting_first;
1870 ods_log_assert(wf->udp_waiting);
1871 wf->udp_waiting = 0;
1872 xfrhandler->udp_waiting_first = wf->udp_waiting_next;
1873 if (xfrhandler->udp_waiting_last == wf) {
1874 xfrhandler->udp_waiting_last = NULL;
1875 }
1876 /* see if this zone needs udp connection */
1877 if (wf->tcp_conn == -1) {
1878 wf->handler.fd = xfrd_udp_send_request_ixfr(wf);
1879 if (wf->handler.fd != -1) {
1880 return;
1881 }
1882 }
1883 }
1884 }
1885 /* no waiting zones */
1886 if (xfrhandler->udp_use_num > 0) {
1887 xfrhandler->udp_use_num --;
1888 }
1889}
1890
1891
1896static void
1897xfrd_make_request(xfrd_type* xfrd)
1898{
1899 zone_type* zone = NULL;
1900 dnsin_type* dnsin = NULL;
1901 if (!xfrd || !xfrd->xfrhandler) {
1902 return;
1903 }
1904 zone = (zone_type*) xfrd->zone;
1905 ods_log_assert(zone);
1906 ods_log_assert(zone->name);
1907 ods_log_assert(zone->adinbound);
1908 ods_log_assert(zone->adinbound->type == ADAPTER_DNS);
1909 ods_log_assert(zone->adinbound->config);
1910
1911 dnsin = (dnsin_type*) zone->adinbound->config;
1912 if (xfrd->next_master != -1) {
1913 /* we are told to use this next master */
1914 xfrd->master_num = xfrd->next_master;
1915 xfrd->master = NULL; /* acl_find_num(...) */
1916 /* if there is no next master, fallback to use the first one */
1917 if (!xfrd->master) {
1918 xfrd->master = dnsin->request_xfr;
1919 xfrd->master_num = 0;
1920 }
1921 /* fallback to cycle master */
1922 xfrd->next_master = -1;
1923 xfrd->round_num = 0; /* fresh set of retries after notify */
1924 } else {
1925 /* cycle master */
1926 if (xfrd->round_num != -1 && xfrd->master &&
1927 xfrd->master->next) {
1928 /* try the next master */
1929 xfrd->master = xfrd->master->next;
1930 xfrd->master_num++;
1931 } else {
1932 /* start a new round */
1933 xfrd->master = dnsin->request_xfr;
1934 xfrd->master_num = 0;
1935 xfrd->round_num++;
1936 }
1937 if (xfrd->round_num >= XFRD_MAX_ROUNDS) {
1938 /* tried all servers that many times, wait */
1939 xfrd->round_num = -1;
1941 ods_log_verbose("[%s] zone %s make request wait retry",
1942 xfrd_str, zone->name);
1943 return;
1944 }
1945 }
1946 if (!xfrd->master) {
1947 ods_log_debug("[%s] unable to make request for zone %s: no master",
1948 xfrd_str, zone->name);
1949 xfrd->round_num = -1;
1951 return;
1952 }
1953 /* cache ixfr_disabled only for XFRD_NO_IXFR_CACHE time */
1954 if (xfrd->master->ixfr_disabled &&
1956 xfrd_time(xfrd)) {
1957 ods_log_verbose("[%s] clear negative caching ixfr disabled for "
1958 "master %s", xfrd_str, xfrd->master->address);
1959 ods_log_debug("[%s] clear negative caching calc: %lu + %lu <= %lu",
1960 xfrd_str, (unsigned long) xfrd->master->ixfr_disabled, (unsigned long)XFRD_NO_IXFR_CACHE,
1961 (unsigned long) xfrd_time(xfrd));
1962 xfrd->master->ixfr_disabled = 0;
1963 }
1964 /* perform xfr request */
1965 if (xfrd->serial_xfr_acquired && !xfrd->master->ixfr_disabled &&
1966 !xfrd->serial_retransfer) {
1967 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_UDP_TIMEOUT);
1968
1969 ods_log_verbose("[%s] zone %s make request [udp round %d master %s:%u]",
1970 xfrd_str, zone->name, xfrd->round_num, xfrd->master->address,
1971 xfrd->master->port);
1972 xfrd_udp_obtain(xfrd);
1973 } else if (!xfrd->serial_xfr_acquired || xfrd->master->ixfr_disabled ||
1974 xfrd->serial_retransfer) {
1975 xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1976 ods_log_assert(xfrhandler);
1977 if (xfrd->serial_retransfer) {
1978 xfrd->msg_do_retransfer = 1;
1979 xfrd->serial_retransfer = 0;
1980 }
1981 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1982
1983 ods_log_verbose("[%s] zone %s make request [tcp round %d master %s:%u]",
1984 xfrd_str, zone->name, xfrd->round_num, xfrd->master->address,
1985 xfrd->master->port);
1986 xfrd_tcp_obtain(xfrd, xfrhandler->tcp_set);
1987 }
1988}
1989
1990
1995static void
1996xfrd_handle_zone(netio_type* ATTR_UNUSED(netio),
1997 netio_handler_type* handler, netio_events_type event_types)
1998{
1999 xfrd_type* xfrd = NULL;
2000 zone_type* zone = NULL;
2001
2002 if (!handler) {
2003 return;
2004 }
2005 xfrd = (xfrd_type*) handler->user_data;
2006 ods_log_assert(xfrd);
2007 zone = (zone_type*) xfrd->zone;
2008 ods_log_assert(zone);
2009 ods_log_assert(zone->name);
2010
2011 if (xfrd->tcp_conn != -1) {
2012 /* busy in tcp transaction */
2013 xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
2014 ods_log_assert(xfrhandler);
2015 if (event_types & NETIO_EVENT_READ) {
2016 ods_log_deeebug("[%s] zone %s event tcp read", xfrd_str, zone->name);
2017 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
2018 xfrd_tcp_read(xfrd, xfrhandler->tcp_set);
2019 return;
2020 } else if (event_types & NETIO_EVENT_WRITE) {
2021 ods_log_deeebug("[%s] zone %s event tcp write", xfrd_str,
2022 zone->name);
2023 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
2024 xfrd_tcp_write(xfrd, xfrhandler->tcp_set);
2025 return;
2026 } else if (event_types & NETIO_EVENT_TIMEOUT) {
2027 /* tcp connection timed out. Stop it. */
2028 ods_log_deeebug("[%s] zone %s event tcp timeout", xfrd_str,
2029 zone->name);
2030 xfrd_tcp_release(xfrd, xfrhandler->tcp_set, 1);
2031 /* continue to retry; as if a timeout happened */
2032 event_types = NETIO_EVENT_TIMEOUT;
2033 }
2034 }
2035
2036 if (event_types & NETIO_EVENT_READ) {
2037 /* busy in udp transaction */
2038 ods_log_deeebug("[%s] zone %s event udp read", xfrd_str,
2039 zone->name);
2040 xfrd_set_timer_now(xfrd);
2041 xfrd_udp_read(xfrd);
2042 return;
2043 }
2044
2045 /* timeout */
2046 ods_log_deeebug("[%s] zone %s timeout", xfrd_str, zone->name);
2047 if (handler->fd != -1) {
2048 ods_log_assert(xfrd->tcp_conn == -1);
2049 xfrd_udp_release(xfrd);
2050 }
2051 if (xfrd->tcp_waiting) {
2052 ods_log_deeebug("[%s] zone %s skips retry: tcp connections full",
2053 xfrd_str, zone->name);
2054 xfrd_unset_timer(xfrd);
2055 return;
2056 }
2057 if (xfrd->udp_waiting) {
2058 ods_log_deeebug("[%s] zone %s skips retry: udp connections full",
2059 xfrd_str, zone->name);
2060 xfrd_unset_timer(xfrd);
2061 return;
2062 }
2063 /* make a new request */
2064 xfrd_make_request(xfrd);
2065}
2066
2067
2072static void
2073xfrd_backup_dname(FILE* out, uint8_t* dname)
2074{
2075 uint8_t* d= dname+1;
2076 uint8_t len = *d++;
2077 uint8_t i;
2078 if (dname[0]<=1) {
2079 fprintf(out, ".");
2080 return;
2081 }
2082 while (len) {
2083 ods_log_assert(d - (dname+1) <= dname[0]);
2084 for (i=0; i<len; i++) {
2085 uint8_t ch = *d++;
2086 if (isalnum(ch) || ch == '-' || ch == '_') {
2087 fprintf(out, "%c", ch);
2088 } else if (ch == '.' || ch == '\\') {
2089 fprintf(out, "\\%c", ch);
2090 } else {
2091 fprintf(out, "\\%03u", (unsigned int)ch);
2092 }
2093 }
2094 fprintf(out, ".");
2095 len = *d++;
2096 }
2097 return;
2098}
2099
2100
2105static void
2106xfrd_backup(xfrd_type* xfrd)
2107{
2108 zone_type* zone = (zone_type*) xfrd->zone;
2109 char* file = NULL;
2110 int timeout = 0;
2111 FILE* fd = NULL;
2112 if (zone && zone->name) {
2113 file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
2114 if (file) {
2115 fd = ods_fopen(file, NULL, "w");
2116 if (fd) {
2117 if (xfrd->handler.timeout) {
2118 timeout = xfrd->timeout.tv_sec;
2119 }
2120 fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
2121 fprintf(fd, ";;Zone: name %s ttl %u mname ",
2122 zone->name,
2123 (unsigned) xfrd->soa.ttl);
2124 xfrd_backup_dname(fd, xfrd->soa.mname),
2125 fprintf(fd, " rname ");
2126 xfrd_backup_dname(fd, xfrd->soa.rname),
2127 fprintf(fd, " serial %u refresh %u retry %u expire %u "
2128 "minimum %u\n",
2129 (unsigned) xfrd->soa.serial,
2130 (unsigned) xfrd->soa.refresh,
2131 (unsigned) xfrd->soa.retry,
2132 (unsigned) xfrd->soa.expire,
2133 (unsigned) xfrd->soa.minimum);
2134 fprintf(fd, ";;Master: num %d next %d round %d timeout %d\n",
2135 xfrd->master_num,
2136 xfrd->next_master,
2137 xfrd->round_num,
2138 timeout);
2139 fprintf(fd, ";;Serial: xfr %u %u notify %u %u disk %u %u\n",
2140 (unsigned) xfrd->serial_xfr,
2141 (unsigned) xfrd->serial_xfr_acquired,
2142 (unsigned) xfrd->serial_notify,
2143 (unsigned) xfrd->serial_notify_acquired,
2144 (unsigned) xfrd->serial_disk,
2145 (unsigned) xfrd->serial_disk_acquired);
2146 fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
2147 ods_fclose(fd);
2148 }
2149 free(file);
2150 }
2151 }
2152}
2153
2154
2159static void
2160xfrd_unlink(xfrd_type* xfrd)
2161{
2162 zone_type* zone = (zone_type*) xfrd->zone;
2163 char* file = NULL;
2164 if (zone && zone->name) {
2165 ods_log_info("[%s] unlink zone %s xfrd state", xfrd_str, zone->name);
2166 file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
2167 if (file) {
2168 (void)unlink(file);
2169 free(file);
2170 }
2171 }
2172}
2173
2174
2179void
2180xfrd_cleanup(xfrd_type* xfrd, int backup)
2181{
2182 if (!xfrd) {
2183 return;
2184 }
2185 /* backup */
2186 if (backup) {
2187 xfrd_backup(xfrd);
2188 } else {
2189 xfrd_unlink(xfrd);
2190 }
2191
2192 tsig_rr_cleanup(xfrd->tsig_rr);
2193 pthread_mutex_destroy(&xfrd->serial_lock);
2194 pthread_mutex_destroy(&xfrd->rw_lock);
2195 free(xfrd);
2196}
int acl_parse_family(const char *a)
Definition: acl.c:104
@ ADAPTER_DNS
Definition: adapter.h:42
int backup_read_uint32_t(FILE *in, uint32_t *v)
Definition: backup.c:209
int backup_read_int(FILE *in, int *v)
Definition: backup.c:175
int backup_read_time_t(FILE *in, time_t *v)
Definition: backup.c:121
int backup_read_check_str(FILE *in, const char *str)
Definition: backup.c:77
int backup_read_str(FILE *in, const char **str)
Definition: backup.c:104
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:487
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:994
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:99
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition: buffer.c:342
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:736
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:721
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:438
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:385
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:137
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:426
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:954
void buffer_flip(buffer_type *buffer)
Definition: buffer.c:112
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:125
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1066
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1054
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition: buffer.c:538
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:591
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:512
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:150
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:894
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:310
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:463
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:578
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition: buffer.c:1120
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:565
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:246
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:373
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1018
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1078
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition: buffer.c:751
#define MAXDOMAINLEN
Definition: buffer.h:44
#define BUFFER_PKT_HEADER_SIZE
Definition: buffer.h:43
#define MAXLABELLEN
Definition: buffer.h:45
void log_dname(ldns_rdf *rdf, const char *pre, int level)
Definition: domain.c:48
void engine_wakeup_workers(engine_type *engine)
Definition: engine.c:291
#define DNS_PORT_STRING
Definition: listener.h:51
#define PF_INET6
Definition: netio.h:61
enum netio_events_enum netio_events_type
Definition: netio.h:76
#define PF_INET
Definition: netio.h:58
@ NETIO_EVENT_WRITE
Definition: netio.h:72
@ NETIO_EVENT_TIMEOUT
Definition: netio.h:74
@ NETIO_EVENT_READ
Definition: netio.h:71
Definition: acl.h:58
int family
Definition: acl.h:63
time_t ixfr_disabled
Definition: acl.h:71
acl_type * next
Definition: acl.h:59
char * address
Definition: acl.h:61
tsig_type * tsig
Definition: acl.h:69
union acl_addr_storage addr
Definition: acl.h:64
unsigned int port
Definition: acl.h:62
void * config
Definition: adapter.h:61
adapter_mode type
Definition: adapter.h:58
acl_type * request_xfr
Definition: addns.h:50
unsigned have_serial
Definition: namedb.h:60
unsigned is_initialized
Definition: namedb.h:57
struct timespec * timeout
Definition: netio.h:115
netio_events_type event_types
Definition: netio.h:124
netio_event_handler_type event_handler
Definition: netio.h:131
void * user_data
Definition: netio.h:119
uint32_t serial
Definition: xfrd.h:82
uint8_t rname[MAXDOMAINLEN+2]
Definition: xfrd.h:81
uint32_t retry
Definition: xfrd.h:84
uint32_t minimum
Definition: xfrd.h:86
uint32_t refresh
Definition: xfrd.h:83
uint32_t ttl
Definition: xfrd.h:78
uint8_t mname[MAXDOMAINLEN+2]
Definition: xfrd.h:80
uint32_t expire
Definition: xfrd.h:85
unsigned is_reading
Definition: tcpset.h:60
uint16_t msglen
Definition: tcpset.h:56
buffer_type * packet
Definition: tcpset.h:58
uint32_t total_bytes
Definition: tcpset.h:54
xfrd_type * tcp_waiting_first
Definition: tcpset.h:69
tcp_conn_type * tcp_conn[TCPSET_MAX]
Definition: tcpset.h:68
size_t tcp_count
Definition: tcpset.h:71
ldns_rdf * wf_name
Definition: tsig.h:91
ldns_rdf * dname
Definition: tsig.h:79
tsig_algo_type * algo
Definition: tsig.h:129
size_t position
Definition: tsig.h:125
tsig_key_type * key
Definition: tsig.h:130
uint16_t error_code
Definition: tsig.h:142
size_t update_since_last_prepare
Definition: tsig.h:127
tsig_status status
Definition: tsig.h:124
uint16_t original_query_id
Definition: tsig.h:141
ldns_rdf * algo_name
Definition: tsig.h:135
ldns_rdf * key_name
Definition: tsig.h:134
tsig_key_type * key
Definition: tsig.h:115
const char * algorithm
Definition: tsig.h:113
time_t serial_disk_acquired
Definition: xfrd.h:118
xfrhandler_type * xfrhandler
Definition: xfrd.h:95
uint32_t msg_old_serial
Definition: xfrd.h:129
netio_handler_type handler
Definition: xfrd.h:124
pthread_mutex_t serial_lock
Definition: xfrd.h:97
int tcp_conn
Definition: xfrd.h:101
pthread_mutex_t rw_lock
Definition: xfrd.h:98
int next_master
Definition: xfrd.h:104
uint16_t query_id
Definition: xfrd.h:127
uint32_t serial_xfr
Definition: xfrd.h:108
uint8_t msg_do_retransfer
Definition: xfrd.h:133
uint32_t serial_disk
Definition: xfrd.h:113
time_t serial_notify_acquired
Definition: xfrd.h:117
unsigned tcp_waiting
Definition: xfrd.h:138
size_t msg_rr_count
Definition: xfrd.h:131
acl_type * master
Definition: xfrd.h:105
xfrd_type * udp_waiting_next
Definition: xfrd.h:137
xfrd_type * tcp_waiting_next
Definition: xfrd.h:136
uint32_t msg_seq_nr
Definition: xfrd.h:128
uint8_t serial_retransfer
Definition: xfrd.h:119
int round_num
Definition: xfrd.h:102
time_t serial_xfr_acquired
Definition: xfrd.h:114
zone_type * zone
Definition: xfrd.h:96
tsig_rr_type * tsig_rr
Definition: xfrd.h:134
struct timespec timeout
Definition: xfrd.h:123
unsigned udp_waiting
Definition: xfrd.h:139
uint32_t msg_new_serial
Definition: xfrd.h:130
soa_type soa
Definition: xfrd.h:120
uint32_t serial_notify
Definition: xfrd.h:111
int master_num
Definition: xfrd.h:103
uint8_t msg_is_ixfr
Definition: xfrd.h:132
buffer_type * packet
Definition: xfrhandler.h:62
tcp_set_type * tcp_set
Definition: xfrhandler.h:61
engine_type * engine
Definition: xfrhandler.h:55
xfrd_type * udp_waiting_first
Definition: xfrhandler.h:64
size_t udp_use_num
Definition: xfrhandler.h:66
xfrd_type * udp_waiting_last
Definition: xfrhandler.h:65
xfrd_type * tcp_waiting_first
Definition: xfrhandler.h:63
namedb_type * db
Definition: zone.h:79
ldns_rr_class klass
Definition: zone.h:62
adapter_type * adinbound
Definition: zone.h:74
ldns_rdf * apex
Definition: zone.h:61
const char * name
Definition: zone.h:69
pthread_mutex_t zone_lock
Definition: zone.h:86
void tcp_conn_ready(tcp_conn_type *tcp)
Definition: tcpset.c:89
int tcp_conn_read(tcp_conn_type *tcp)
Definition: tcpset.c:103
int tcp_conn_write(tcp_conn_type *tcp)
Definition: tcpset.c:177
#define TCPSET_MAX
Definition: tcpset.h:45
void tsig_rr_cleanup(tsig_rr_type *trr)
Definition: tsig.c:832
const char * tsig_strerror(uint16_t error)
Definition: tsig.c:778
int tsig_rr_verify(tsig_rr_type *trr)
Definition: tsig.c:650
void tsig_rr_append(tsig_rr_type *trr, buffer_type *buffer)
Definition: tsig.c:672
void tsig_rr_sign(tsig_rr_type *trr)
Definition: tsig.c:629
void tsig_rr_reset(tsig_rr_type *trr, tsig_algo_type *algo, tsig_key_type *key)
Definition: tsig.c:292
void tsig_rr_update(tsig_rr_type *trr, buffer_type *buffer, size_t length)
Definition: tsig.c:559
void tsig_rr_prepare(tsig_rr_type *trr)
Definition: tsig.c:537
tsig_rr_type * tsig_rr_create()
Definition: tsig.c:274
int tsig_rr_find(tsig_rr_type *trr, buffer_type *buffer)
Definition: tsig.c:435
tsig_algo_type * tsig_lookup_algo(const char *name)
Definition: tsig.c:257
@ TSIG_OK
Definition: tsig.h:57
struct in_addr addr
Definition: listener.h:60
struct in6_addr addr6
Definition: listener.h:61
void xfrd_set_timer_retry(xfrd_type *xfrd)
Definition: xfrd.c:472
void xfrd_set_timer_refresh(xfrd_type *xfrd)
Definition: xfrd.c:490
void xfrd_set_timer_now(xfrd_type *xfrd)
Definition: xfrd.c:454
socklen_t xfrd_acl_sockaddr_to(acl_type *acl, struct sockaddr_storage *to)
Definition: xfrd.c:537
xfrd_type * xfrd_create(xfrhandler_type *xfrhandler, zone_type *zone)
Definition: xfrd.c:315
void xfrd_cleanup(xfrd_type *xfrd, int backup)
Definition: xfrd.c:2180
#define XFRD_TSIG_MAX_UNSIGNED
Definition: xfrd.c:49
#define XFRD_MAX_UDP
Definition: xfrd.h:66
#define XFRD_NO_IXFR_CACHE
Definition: xfrd.h:67
#define XFRD_UDP_TIMEOUT
Definition: xfrd.h:69
#define XFRD_MAX_ROUNDS
Definition: xfrd.h:65
enum xfrd_pkt_enum xfrd_pkt_status
Definition: xfrd.h:51
#define XFRD_TCP_TIMEOUT
Definition: xfrd.h:68
@ XFRD_PKT_NOTIMPL
Definition: xfrd.h:46
@ XFRD_PKT_BAD
Definition: xfrd.h:44
@ XFRD_PKT_TC
Definition: xfrd.h:47
@ XFRD_PKT_MORE
Definition: xfrd.h:45
@ XFRD_PKT_NEWLEASE
Definition: xfrd.h:49
@ XFRD_PKT_XFR
Definition: xfrd.h:48
time_t xfrhandler_time(xfrhandler_type *xfrhandler)
Definition: xfrhandler.c:141