OpenDNSSEC-signer 2.1.10
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 case XFRD_PKT_TC:
1199 return res;
1200 break;
1201 case XFRD_PKT_NOTIMPL:
1202 case XFRD_PKT_BAD:
1203 default:
1204 /* rollback */
1205 if (xfrd->msg_seq_nr > 0) {
1206 buffer_clear(buffer);
1207 ods_log_info("[%s] zone %s xfr rollback", xfrd_str,
1208 zone->name);
1209 buffer_flip(buffer);
1210 }
1211 return res;
1212 break;
1213 }
1214 /* dump reply on disk to diff file */
1215 xfrd_dump_packet(xfrd, buffer);
1216 /* more? */
1217 xfrd->msg_seq_nr++;
1218 if (res == XFRD_PKT_MORE) {
1219 /* wait for more */
1220 return XFRD_PKT_MORE;
1221 }
1222 /* done */
1223 buffer_clear(buffer);
1224 buffer_flip(buffer);
1225 /* commit packet */
1226 xfrd_commit_packet(xfrd);
1227 /* next time */
1228 pthread_mutex_lock(&xfrd->serial_lock);
1229
1230 ods_log_info("[%s] zone %s transfer done [notify acquired %lu, serial on "
1231 "disk %u, notify serial %u]", xfrd_str, zone->name,
1232 (unsigned long)xfrd->serial_notify_acquired, xfrd->serial_disk,
1233 xfrd->serial_notify);
1234
1235 if (xfrd->serial_notify_acquired &&
1236 !util_serial_gt(xfrd->serial_notify, xfrd->serial_disk)) {
1237 ods_log_verbose("[%s] zone %s reset notify acquired", xfrd_str,
1238 zone->name);
1239 xfrd->serial_notify_acquired = 0;
1240 }
1241 if (!xfrd->serial_notify_acquired) {
1242 ods_log_debug("[%s] zone %s xfr done", xfrd_str, zone->name);
1243 xfrd->round_num = -1; /* next try start anew */
1245 pthread_mutex_unlock(&xfrd->serial_lock);
1246 return XFRD_PKT_XFR;
1247 }
1248 pthread_mutex_unlock(&xfrd->serial_lock);
1249 /* try to get an even newer serial */
1250 ods_log_info("[%s] zone %s try get newer serial", xfrd_str, zone->name);
1251 return XFRD_PKT_BAD;
1252}
1253
1254
1262static void
1263xfrd_tcp_write(xfrd_type* xfrd, tcp_set_type* set)
1264{
1265 zone_type* zone = NULL;
1266 tcp_conn_type* tcp = NULL;
1267 int ret = 0;
1268 int error = 0;
1269 socklen_t len = 0;
1270
1271 ods_log_assert(set);
1272 ods_log_assert(xfrd);
1273 ods_log_assert(xfrd->tcp_conn != -1);
1274 zone = (zone_type*) xfrd->zone;
1275 ods_log_assert(zone);
1276 ods_log_assert(zone->name);
1277 tcp = set->tcp_conn[xfrd->tcp_conn];
1278 if (tcp->total_bytes == 0) {
1279 /* check for pending error from nonblocking connect */
1280 /* from Stevens, unix network programming, vol1, 3rd ed, p450 */
1281 len = sizeof(error);
1282 if (getsockopt(tcp->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1283 error = errno; /* on solaris errno is error */
1284 }
1285 if (error == EINPROGRESS || error == EWOULDBLOCK) {
1286 ods_log_debug("[%s] zone %s zero write, write again later (%s)",
1287 xfrd_str, zone->name, strerror(error));
1288 return; /* try again later */
1289 }
1290 if (error != 0) {
1291 ods_log_error("[%s] zone %s cannot tcp connect to %s: %s",
1292 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1293 xfrd_set_timer_now(xfrd);
1294 xfrd_tcp_release(xfrd, set, 1);
1295 return;
1296 }
1297 }
1298 ret = tcp_conn_write(tcp);
1299 if(ret == -1) {
1300 ods_log_error("[%s] zone %s cannot tcp write to %s: %s",
1301 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1302 xfrd_set_timer_now(xfrd);
1303 xfrd_tcp_release(xfrd, set, 1);
1304 return;
1305 }
1306 if (ret == 0) {
1307 ods_log_debug("[%s] zone %s zero write, write again later",
1308 xfrd_str, zone->name);
1309 return; /* write again later */
1310 }
1311 /* done writing, get ready for reading */
1312 ods_log_debug("[%s] zone %s done writing, get ready for reading",
1313 xfrd_str, zone->name);
1314 tcp->is_reading = 1;
1315 tcp_conn_ready(tcp);
1317 xfrd_tcp_read(xfrd, set);
1318}
1319
1320
1325static int
1326xfrd_tcp_open(xfrd_type* xfrd, tcp_set_type* set)
1327{
1328 int fd, family, conn;
1329 struct sockaddr_storage to;
1330 socklen_t to_len;
1331 zone_type* zone = NULL;
1332
1333 ods_log_assert(set);
1334 ods_log_assert(xfrd);
1335 ods_log_assert(xfrd->tcp_conn != -1);
1336 ods_log_assert(xfrd->master);
1337 ods_log_assert(xfrd->master->address);
1338 zone = (zone_type*) xfrd->zone;
1339 ods_log_assert(zone);
1340 ods_log_assert(zone->name);
1341 ods_log_debug("[%s] zone %s open tcp connection to %s", xfrd_str,
1342 zone->name, xfrd->master->address);
1343 set->tcp_conn[xfrd->tcp_conn]->is_reading = 0;
1344 set->tcp_conn[xfrd->tcp_conn]->total_bytes = 0;
1345 set->tcp_conn[xfrd->tcp_conn]->msglen = 0;
1346 if (xfrd->master->family == AF_INET6) {
1347 family = PF_INET6;
1348 } else {
1349 family = PF_INET;
1350 }
1351 fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
1352 set->tcp_conn[xfrd->tcp_conn]->fd = fd;
1353 if (fd == -1) {
1354 ods_log_error("[%s] zone %s cannot create tcp socket to %s: %s",
1355 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1356 xfrd_set_timer_now(xfrd);
1357 xfrd_tcp_release(xfrd, set, 0);
1358 return 0;
1359 }
1360 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
1361 ods_log_error("[%s] zone %s cannot fcntl tcp socket: %s",
1362 xfrd_str, zone->name, strerror(errno));
1363 xfrd_set_timer_now(xfrd);
1364 xfrd_tcp_release(xfrd, set, 0);
1365 return 0;
1366 }
1367 to_len = xfrd_acl_sockaddr_to(xfrd->master, &to);
1368 /* bind it */
1369 interface_type interface = xfrd->xfrhandler->engine->dnshandler->interfaces->interfaces[0];
1370 if (!interface.address) {
1371 ods_log_error("[%s] unable to get the address of interface", xfrd_str);
1372 return -1;
1373 }
1374 if (acl_parse_family(interface.address) == AF_INET) {
1375 struct sockaddr_in addr;
1376 addr.sin_family = acl_parse_family(interface.address);
1377 addr.sin_addr = interface.addr.addr;
1378 addr.sin_port = 0;
1379 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
1380 ods_log_error("[%s] unable to bind address %s: bind failed %s", xfrd_str, interface.address, strerror(errno));
1381 return -1;
1382 }
1383 }
1384 else {
1385 struct sockaddr_in6 addr6;
1386 addr6.sin6_family = acl_parse_family(interface.address);
1387 addr6.sin6_addr = interface.addr.addr6;
1388 addr6.sin6_port = 0;
1389 if (bind(fd, (struct sockaddr *) &addr6, sizeof(addr6)) != 0) {
1390 ods_log_error("[%s] unable to bind address %s: bind failed %s", xfrd_str, interface.address, strerror(errno));
1391 return -1;
1392 }
1393 }
1394
1395 conn = connect(fd, (struct sockaddr*)&to, to_len);
1396 if (conn == -1 && errno != EINPROGRESS) {
1397 ods_log_error("[%s] zone %s cannot connect tcp socket to %s: %s",
1398 xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1399 xfrd_set_timer_now(xfrd);
1400 xfrd_tcp_release(xfrd, set, 0);
1401 return 0;
1402 }
1403 xfrd->handler.fd = fd;
1405 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1406 return 1;
1407}
1408
1409
1414static void
1415xfrd_tcp_obtain(xfrd_type* xfrd, tcp_set_type* set)
1416{
1417 xfrhandler_type* xfrhandler;
1418 int i = 0;
1419
1420 ods_log_assert(set);
1421 ods_log_assert(xfrd);
1422 ods_log_assert(xfrd->tcp_conn == -1);
1423 ods_log_assert(xfrd->tcp_waiting == 0);
1424 if (set->tcp_count < TCPSET_MAX) {
1425 ods_log_assert(!set->tcp_waiting_first);
1426 set->tcp_count ++;
1427 /* find a free tcp_buffer */
1428 for (i=0; i < TCPSET_MAX; i++) {
1429 if (set->tcp_conn[i]->fd == -1) {
1430 xfrd->tcp_conn = i;
1431 break;
1432 }
1433 }
1434 ods_log_assert(xfrd->tcp_conn != -1);
1435 xfrd->tcp_waiting = 0;
1436 /* stop udp use (if any) */
1437 if (xfrd->handler.fd != -1) {
1438 xfrd_udp_release(xfrd);
1439 }
1440 if (!xfrd_tcp_open(xfrd, set)) {
1441 return;
1442 }
1443 xfrd_tcp_xfr(xfrd, set);
1444 return;
1445 }
1446 /* wait, at end of line */
1447 ods_log_verbose("[%s] max number of tcp connections (%d) reached",
1448 xfrd_str, TCPSET_MAX);
1449 xfrd->tcp_waiting = 1;
1450 xfrd_unset_timer(xfrd);
1451
1452 /* add it to the waiting queue */
1453 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1454 xfrd->tcp_waiting_next = xfrhandler->tcp_waiting_first;
1455 xfrhandler->tcp_waiting_first = xfrd;
1456}
1457
1458
1463static void
1464xfrd_tcp_xfr(xfrd_type* xfrd, tcp_set_type* set)
1465{
1466 tcp_conn_type* tcp = NULL;
1467 zone_type* zone = NULL;
1468
1469 ods_log_assert(set);
1470 ods_log_assert(xfrd);
1471 zone = (zone_type*) xfrd->zone;
1472 ods_log_assert(zone);
1473 ods_log_assert(zone->name);
1474 ods_log_assert(xfrd->tcp_conn != -1);
1475 ods_log_assert(xfrd->tcp_waiting == 0);
1476 ods_log_assert(xfrd->master);
1477 ods_log_assert(xfrd->master->address);
1478 /* start AXFR or IXFR for the zone */
1479 tcp = set->tcp_conn[xfrd->tcp_conn];
1480
1481 if (xfrd->msg_do_retransfer || xfrd->serial_xfr_acquired <= 0 ||
1482 xfrd->master->ixfr_disabled) {
1483 ods_log_info("[%s] zone %s request axfr to %s", xfrd_str,
1484 zone->name, xfrd->master->address);
1485 buffer_pkt_query(tcp->packet, zone->apex, LDNS_RR_TYPE_AXFR,
1486 zone->klass);
1487 } else {
1488 ods_log_info("[%s] zone %s request tcp/ixfr=%u to %s", xfrd_str,
1489 zone->name, xfrd->soa.serial, xfrd->master->address);
1490 buffer_pkt_query(tcp->packet, zone->apex, LDNS_RR_TYPE_IXFR,
1491 zone->klass);
1493 xfrd_write_soa(xfrd, tcp->packet);
1494 }
1495 /* make packet */
1496 xfrd->query_id = buffer_pkt_id(tcp->packet);
1497 xfrd->msg_seq_nr = 0;
1498 xfrd->msg_rr_count = 0;
1499 xfrd->msg_old_serial = 0;
1500 xfrd->msg_new_serial = 0;
1501 xfrd->msg_is_ixfr = 0;
1502 xfrd_tsig_sign(xfrd, tcp->packet);
1503 buffer_flip(tcp->packet);
1504 tcp->msglen = buffer_limit(tcp->packet);
1505 ods_log_verbose("[%s] zone %s sending tcp query id=%d", xfrd_str,
1506 zone->name, xfrd->query_id);
1507 /* wait for select to complete connect before write */
1508}
1509
1510
1515static void
1516xfrd_tcp_read(xfrd_type* xfrd, tcp_set_type* set)
1517{
1518 tcp_conn_type* tcp = NULL;
1519 int ret = 0;
1520
1521 ods_log_assert(set);
1522 ods_log_assert(xfrd);
1523 ods_log_assert(xfrd->tcp_conn != -1);
1524 tcp = set->tcp_conn[xfrd->tcp_conn];
1525 ret = tcp_conn_read(tcp);
1526 if (ret == -1) {
1527 xfrd_set_timer_now(xfrd);
1528 xfrd_tcp_release(xfrd, set, 1);
1529 return;
1530 }
1531 if (ret == 0) {
1532 return;
1533 }
1534 /* completed msg */
1535 buffer_flip(tcp->packet);
1536 ret = xfrd_handle_packet(xfrd, tcp->packet);
1537 switch (ret) {
1538 case XFRD_PKT_MORE:
1539 tcp_conn_ready(tcp);
1540 break;
1541 case XFRD_PKT_XFR:
1542 case XFRD_PKT_NEWLEASE:
1543 ods_log_verbose("[%s] tcp read %s: release connection", xfrd_str,
1544 XFRD_PKT_XFR?"xfr":"newlease");
1545 xfrd_tcp_release(xfrd, set, 1);
1546 ods_log_assert(xfrd->round_num == -1);
1547 break;
1548 case XFRD_PKT_NOTIMPL:
1549 xfrd->master->ixfr_disabled = time_now();
1550 ods_log_verbose("[%s] disable ixfr requests for %s from now (%lu)",
1551 xfrd_str, xfrd->master->address, (unsigned long)xfrd->master->ixfr_disabled);
1552 /* break; */
1553 case XFRD_PKT_BAD:
1554 default:
1555 ods_log_debug("[%s] tcp read %s: release connection", xfrd_str,
1556 ret==XFRD_PKT_BAD?"bad":"notimpl");
1557 xfrd_tcp_release(xfrd, set, 1);
1558 xfrd_make_request(xfrd);
1559 break;
1560 }
1561}
1562
1563
1569static void
1570xfrd_tcp_release(xfrd_type* xfrd, tcp_set_type* set, int open_waiting)
1571{
1572 xfrhandler_type* xfrhandler;
1573 int conn = 0;
1574 zone_type* zone = NULL;
1575
1576 ods_log_assert(set);
1577 ods_log_assert(xfrd);
1578 ods_log_assert(xfrd->master);
1579 ods_log_assert(xfrd->master->address);
1580 ods_log_assert(xfrd->tcp_conn != -1);
1581 ods_log_assert(xfrd->tcp_waiting == 0);
1582 zone = (zone_type*) xfrd->zone;
1583 ods_log_debug("[%s] zone %s release tcp connection to %s", xfrd_str,
1584 zone->name, xfrd->master->address);
1585 conn = xfrd->tcp_conn;
1586 xfrd->tcp_conn = -1;
1587 xfrd->tcp_waiting = 0;
1588 xfrd->handler.fd = -1;
1590
1591 if (set->tcp_conn[conn]->fd != -1) {
1592 close(set->tcp_conn[conn]->fd);
1593 }
1594 set->tcp_conn[conn]->fd = -1;
1595 set->tcp_count --;
1596
1597 /* see if there are any connections waiting for a slot. Or return. */
1598 if (!open_waiting) return;
1599 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1600 while (xfrhandler->tcp_waiting_first && set->tcp_count < TCPSET_MAX) {
1601 int i;
1602 xfrd_type* waiting_xfrd = xfrhandler->tcp_waiting_first;
1603 xfrhandler->tcp_waiting_first = waiting_xfrd->tcp_waiting_next;
1604 waiting_xfrd->tcp_waiting_next = NULL;
1605
1606 /* find a free tcp_buffer */
1607 for (i=0; i < TCPSET_MAX; i++) {
1608 if (set->tcp_conn[i]->fd == -1) {
1609 waiting_xfrd->tcp_conn = i;
1610 set->tcp_count++;
1611 break;
1612 }
1613 }
1614 waiting_xfrd->tcp_waiting = 0;
1615 /* stop udp use (if any) */
1616 if (waiting_xfrd->handler.fd != -1) {
1617 xfrd_udp_release(waiting_xfrd);
1618 }
1619 /* if xfrd_tcp_open() fails its slot in set->tcp_conn[]
1620 * is released. Continue to next. We don't put it back in the
1621 * waiting queue, it would keep the signer busy retrying, making
1622 * things only worse. */
1623 if (xfrd_tcp_open(waiting_xfrd, set)) {
1624 xfrd_tcp_xfr(waiting_xfrd, set);
1625 }
1626 }
1627}
1628
1629
1637static int
1638xfrd_udp_send(xfrd_type* xfrd, buffer_type* buffer)
1639{
1640 struct sockaddr_storage to;
1641 socklen_t to_len = 0;
1642 int fd = -1;
1643 int family = PF_INET;
1644 ssize_t nb = -1;
1645 ods_log_assert(buffer);
1646 ods_log_assert(xfrd);
1647 ods_log_assert(xfrd->master);
1648 ods_log_assert(xfrd->master->address);
1649 /* this will set the remote port to acl->port or TCP_PORT */
1650 to_len = xfrd_acl_sockaddr_to(xfrd->master, &to);
1651 /* get the address family of the remote host */
1652 if (xfrd->master->family == AF_INET6) {
1653 family = PF_INET6;
1654 }
1655 /* create socket */
1656 fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1657 if (fd == -1) {
1658 ods_log_error("[%s] unable to send data over udp to %s: "
1659 "socket() failed (%s)", xfrd_str, xfrd->master->address,
1660 strerror(errno));
1661 return -1;
1662 }
1663 /* bind it? */
1664
1665 /* send it (udp) */
1666 ods_log_deeebug("[%s] send %lu bytes over udp to %s", xfrd_str,
1667 (unsigned long)buffer_remaining(buffer), xfrd->master->address);
1668 nb = sendto(fd, buffer_current(buffer), buffer_remaining(buffer), 0,
1669 (struct sockaddr*)&to, to_len);
1670 if (nb == -1) {
1671 ods_log_error("[%s] unable to send data over udp to %s: "
1672 "sendto() failed (%s)", xfrd_str, xfrd->master->address,
1673 strerror(errno));
1674 close(fd);
1675 return -1;
1676 }
1677 return fd;
1678}
1679
1680
1685static int
1686xfrd_udp_send_request_ixfr(xfrd_type* xfrd)
1687{
1688 int fd;
1689 xfrhandler_type* xfrhandler = NULL;
1690 zone_type* zone = NULL;
1691 ods_log_assert(xfrd);
1692 ods_log_assert(xfrd->master);
1693 ods_log_assert(xfrd->master->address);
1694 zone = (zone_type*) xfrd->zone;
1695 ods_log_assert(zone);
1696 ods_log_assert(zone->name);
1697 if (xfrd->tcp_conn != -1) {
1698 /* tcp is using the handler.fd */
1699 ods_log_error("[%s] unable to transfer zone %s: tried to send "
1700 "udp while tcp obtained", xfrd_str, zone->name);
1701 return -1;
1702 }
1703 /* make packet */
1704 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1705 ods_log_assert(xfrhandler);
1706 buffer_pkt_query(xfrhandler->packet, zone->apex, LDNS_RR_TYPE_IXFR,
1707 zone->klass);
1708 xfrd->query_id = buffer_pkt_id(xfrhandler->packet);
1709 xfrd->msg_seq_nr = 0;
1710 xfrd->msg_rr_count = 0;
1711 xfrd->msg_old_serial = 0;
1712 xfrd->msg_new_serial = 0;
1713 xfrd->msg_is_ixfr = 0;
1714 buffer_pkt_set_nscount(xfrhandler->packet, 1);
1715 xfrd_write_soa(xfrd, xfrhandler->packet);
1716 xfrd_tsig_sign(xfrd, xfrhandler->packet);
1717 buffer_flip(xfrhandler->packet);
1718 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_UDP_TIMEOUT);
1719 ods_log_info("[%s] zone %s request udp/ixfr=%u to %s", xfrd_str,
1720 zone->name, xfrd->soa.serial, xfrd->master->address);
1721 if((fd = xfrd_udp_send(xfrd, xfrhandler->packet)) == -1) {
1722 return -1;
1723 }
1724 return fd;
1725}
1726
1731static void
1732xfrd_udp_obtain(xfrd_type* xfrd)
1733{
1734 xfrhandler_type* xfrhandler = NULL;
1735 ods_log_assert(xfrd);
1736 ods_log_assert(xfrd->xfrhandler);
1737 ods_log_assert(xfrd->udp_waiting == 0);
1738 xfrhandler = (void*) xfrd->xfrhandler;
1739 if (xfrd->tcp_conn != -1) {
1740 /* no tcp and udp at the same time */
1741 xfrd_tcp_release(xfrd, xfrhandler->tcp_set, 1);
1742 }
1743 if (xfrhandler->udp_use_num < XFRD_MAX_UDP) {
1744 xfrhandler->udp_use_num++;
1745 xfrd->handler.fd = xfrd_udp_send_request_ixfr(xfrd);
1746 if (xfrd->handler.fd == -1) {
1747 xfrhandler->udp_use_num--;
1748 }
1749 return;
1750 }
1751 /* queue the zone as last */
1752 xfrd->udp_waiting = 1;
1753 xfrd->udp_waiting_next = NULL;
1754 if (!xfrhandler->udp_waiting_first) {
1755 xfrhandler->udp_waiting_first = xfrd;
1756 }
1757 if (xfrhandler->udp_waiting_last) {
1758 xfrhandler->udp_waiting_last->udp_waiting_next = xfrd;
1759 }
1760 xfrhandler->udp_waiting_last = xfrd;
1761 xfrd_unset_timer(xfrd);
1762}
1763
1764
1769static int
1770xfrd_udp_read_packet(xfrd_type* xfrd)
1771{
1772 xfrhandler_type* xfrhandler = NULL;
1773 ssize_t received = 0;
1774 ods_log_assert(xfrd);
1775 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1776 ods_log_assert(xfrhandler);
1777 /* read the data */
1778 buffer_clear(xfrhandler->packet);
1779 received = recvfrom(xfrd->handler.fd, buffer_begin(xfrhandler->packet),
1780 buffer_remaining(xfrhandler->packet), 0, NULL, NULL);
1781 if (received == -1) {
1782 ods_log_error("[%s] unable to read packet: recvfrom() failed fd %d "
1783 "(%s)", xfrd_str, xfrd->handler.fd, strerror(errno));
1784 return 0;
1785 }
1786 buffer_set_limit(xfrhandler->packet, received);
1787 return 1;
1788}
1789
1790
1795static void
1796xfrd_udp_read(xfrd_type* xfrd)
1797{
1798 xfrhandler_type* xfrhandler = NULL;
1799 zone_type* zone = NULL;
1801 ods_log_assert(xfrd);
1802 zone = (zone_type*) xfrd->zone;
1803 ods_log_assert(zone);
1804 ods_log_assert(zone->name);
1805 ods_log_debug("[%s] zone %s read data from udp", xfrd_str,
1806 zone->name);
1807 if (!xfrd_udp_read_packet(xfrd)) {
1808 ods_log_error("[%s] unable to read data from udp zone %s: "
1809 "xfrd_udp_read_packet() failed", xfrd_str, zone->name);
1810 xfrd_udp_release(xfrd);
1811 return;
1812 }
1813 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1814 ods_log_assert(xfrhandler);
1815 res = xfrd_handle_packet(xfrd, xfrhandler->packet);
1816 switch (res) {
1817 case XFRD_PKT_TC:
1818 ods_log_verbose("[%s] truncation from %s",
1819 xfrd_str, xfrd->master->address);
1820 xfrd_udp_release(xfrd);
1821 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1822 xfrd_tcp_obtain(xfrd, xfrhandler->tcp_set);
1823 break;
1824 case XFRD_PKT_XFR:
1825 case XFRD_PKT_NEWLEASE:
1826 ods_log_verbose("[%s] xfr/newlease from %s",
1827 xfrd_str, xfrd->master->address);
1828 /* nothing more to do */
1829 ods_log_assert(xfrd->round_num == -1);
1830 xfrd_udp_release(xfrd);
1831 break;
1832 case XFRD_PKT_NOTIMPL:
1833 xfrd->master->ixfr_disabled = time_now();
1834 ods_log_verbose("[%s] disable ixfr requests for %s from now (%lu)",
1835 xfrd_str, xfrd->master->address, (unsigned long)xfrd->master->ixfr_disabled);
1836 /* break; */
1837 case XFRD_PKT_BAD:
1838 default:
1839 ods_log_debug("[%s] bad ixfr packet from %s",
1840 xfrd_str, xfrd->master->address);
1841 xfrd_udp_release(xfrd);
1842 xfrd_make_request(xfrd);
1843 break;
1844 }
1845}
1846
1847
1852static void
1853xfrd_udp_release(xfrd_type* xfrd)
1854{
1855 xfrhandler_type* xfrhandler = NULL;
1856
1857 ods_log_assert(xfrd);
1858 ods_log_assert(xfrd->udp_waiting == 0);
1859 if(xfrd->handler.fd != -1)
1860 close(xfrd->handler.fd);
1861 xfrd->handler.fd = -1;
1862 xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1863 ods_log_assert(xfrhandler);
1864 /* see if there are waiting zones */
1865 if (xfrhandler->udp_use_num == XFRD_MAX_UDP) {
1866 while (xfrhandler->udp_waiting_first) {
1867 /* snip off waiting list */
1868 xfrd_type* wf = xfrhandler->udp_waiting_first;
1869 ods_log_assert(wf->udp_waiting);
1870 wf->udp_waiting = 0;
1871 xfrhandler->udp_waiting_first = wf->udp_waiting_next;
1872 if (xfrhandler->udp_waiting_last == wf) {
1873 xfrhandler->udp_waiting_last = NULL;
1874 }
1875 /* see if this zone needs udp connection */
1876 if (wf->tcp_conn == -1) {
1877 wf->handler.fd = xfrd_udp_send_request_ixfr(wf);
1878 if (wf->handler.fd != -1) {
1879 return;
1880 }
1881 }
1882 }
1883 }
1884 /* no waiting zones */
1885 if (xfrhandler->udp_use_num > 0) {
1886 xfrhandler->udp_use_num --;
1887 }
1888}
1889
1890
1895static void
1896xfrd_make_request(xfrd_type* xfrd)
1897{
1898 zone_type* zone = NULL;
1899 dnsin_type* dnsin = NULL;
1900 if (!xfrd || !xfrd->xfrhandler) {
1901 return;
1902 }
1903 zone = (zone_type*) xfrd->zone;
1904 ods_log_assert(zone);
1905 ods_log_assert(zone->name);
1906 ods_log_assert(zone->adinbound);
1907 ods_log_assert(zone->adinbound->type == ADAPTER_DNS);
1908 ods_log_assert(zone->adinbound->config);
1909
1910 dnsin = (dnsin_type*) zone->adinbound->config;
1911 if (xfrd->next_master != -1) {
1912 /* we are told to use this next master */
1913 xfrd->master_num = xfrd->next_master;
1914 xfrd->master = NULL; /* acl_find_num(...) */
1915 /* if there is no next master, fallback to use the first one */
1916 if (!xfrd->master) {
1917 xfrd->master = dnsin->request_xfr;
1918 xfrd->master_num = 0;
1919 }
1920 /* fallback to cycle master */
1921 xfrd->next_master = -1;
1922 xfrd->round_num = 0; /* fresh set of retries after notify */
1923 } else {
1924 /* cycle master */
1925 if (xfrd->round_num != -1 && xfrd->master &&
1926 xfrd->master->next) {
1927 /* try the next master */
1928 xfrd->master = xfrd->master->next;
1929 xfrd->master_num++;
1930 } else {
1931 /* start a new round */
1932 xfrd->master = dnsin->request_xfr;
1933 xfrd->master_num = 0;
1934 xfrd->round_num++;
1935 }
1936 if (xfrd->round_num >= XFRD_MAX_ROUNDS) {
1937 /* tried all servers that many times, wait */
1938 xfrd->round_num = -1;
1940 ods_log_verbose("[%s] zone %s make request wait retry",
1941 xfrd_str, zone->name);
1942 return;
1943 }
1944 }
1945 if (!xfrd->master) {
1946 ods_log_debug("[%s] unable to make request for zone %s: no master",
1947 xfrd_str, zone->name);
1948 xfrd->round_num = -1;
1950 return;
1951 }
1952 /* cache ixfr_disabled only for XFRD_NO_IXFR_CACHE time */
1953 if (xfrd->master->ixfr_disabled &&
1955 xfrd_time(xfrd)) {
1956 ods_log_verbose("[%s] clear negative caching ixfr disabled for "
1957 "master %s", xfrd_str, xfrd->master->address);
1958 ods_log_debug("[%s] clear negative caching calc: %lu + %lu <= %lu",
1959 xfrd_str, (unsigned long) xfrd->master->ixfr_disabled, (unsigned long)XFRD_NO_IXFR_CACHE,
1960 (unsigned long) xfrd_time(xfrd));
1961 xfrd->master->ixfr_disabled = 0;
1962 }
1963 /* perform xfr request */
1964 if (xfrd->serial_xfr_acquired && !xfrd->master->ixfr_disabled &&
1965 !xfrd->serial_retransfer) {
1966 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_UDP_TIMEOUT);
1967
1968 ods_log_verbose("[%s] zone %s make request [udp round %d master %s:%u]",
1969 xfrd_str, zone->name, xfrd->round_num, xfrd->master->address,
1970 xfrd->master->port);
1971 xfrd_udp_obtain(xfrd);
1972 } else if (!xfrd->serial_xfr_acquired || xfrd->master->ixfr_disabled ||
1973 xfrd->serial_retransfer) {
1974 xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1975 ods_log_assert(xfrhandler);
1976 if (xfrd->serial_retransfer) {
1977 xfrd->msg_do_retransfer = 1;
1978 xfrd->serial_retransfer = 0;
1979 }
1980 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1981
1982 ods_log_verbose("[%s] zone %s make request [tcp round %d master %s:%u]",
1983 xfrd_str, zone->name, xfrd->round_num, xfrd->master->address,
1984 xfrd->master->port);
1985 xfrd_tcp_obtain(xfrd, xfrhandler->tcp_set);
1986 }
1987}
1988
1989
1994static void
1995xfrd_handle_zone(netio_type* ATTR_UNUSED(netio),
1996 netio_handler_type* handler, netio_events_type event_types)
1997{
1998 xfrd_type* xfrd = NULL;
1999 zone_type* zone = NULL;
2000
2001 if (!handler) {
2002 return;
2003 }
2004 xfrd = (xfrd_type*) handler->user_data;
2005 ods_log_assert(xfrd);
2006 zone = (zone_type*) xfrd->zone;
2007 ods_log_assert(zone);
2008 ods_log_assert(zone->name);
2009
2010 if (xfrd->tcp_conn != -1) {
2011 /* busy in tcp transaction */
2012 xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
2013 ods_log_assert(xfrhandler);
2014 if (event_types & NETIO_EVENT_READ) {
2015 ods_log_deeebug("[%s] zone %s event tcp read", xfrd_str, zone->name);
2016 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
2017 xfrd_tcp_read(xfrd, xfrhandler->tcp_set);
2018 return;
2019 } else if (event_types & NETIO_EVENT_WRITE) {
2020 ods_log_deeebug("[%s] zone %s event tcp write", xfrd_str,
2021 zone->name);
2022 xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
2023 xfrd_tcp_write(xfrd, xfrhandler->tcp_set);
2024 return;
2025 } else if (event_types & NETIO_EVENT_TIMEOUT) {
2026 /* tcp connection timed out. Stop it. */
2027 ods_log_deeebug("[%s] zone %s event tcp timeout", xfrd_str,
2028 zone->name);
2029 xfrd_tcp_release(xfrd, xfrhandler->tcp_set, 1);
2030 /* continue to retry; as if a timeout happened */
2031 event_types = NETIO_EVENT_TIMEOUT;
2032 }
2033 }
2034
2035 if (event_types & NETIO_EVENT_READ) {
2036 /* busy in udp transaction */
2037 ods_log_deeebug("[%s] zone %s event udp read", xfrd_str,
2038 zone->name);
2039 xfrd_set_timer_now(xfrd);
2040 xfrd_udp_read(xfrd);
2041 return;
2042 }
2043
2044 /* timeout */
2045 ods_log_deeebug("[%s] zone %s timeout", xfrd_str, zone->name);
2046 if (handler->fd != -1) {
2047 ods_log_assert(xfrd->tcp_conn == -1);
2048 xfrd_udp_release(xfrd);
2049 }
2050 if (xfrd->tcp_waiting) {
2051 ods_log_deeebug("[%s] zone %s skips retry: tcp connections full",
2052 xfrd_str, zone->name);
2053 xfrd_unset_timer(xfrd);
2054 return;
2055 }
2056 if (xfrd->udp_waiting) {
2057 ods_log_deeebug("[%s] zone %s skips retry: udp connections full",
2058 xfrd_str, zone->name);
2059 xfrd_unset_timer(xfrd);
2060 return;
2061 }
2062 /* make a new request */
2063 xfrd_make_request(xfrd);
2064}
2065
2066
2071static void
2072xfrd_backup_dname(FILE* out, uint8_t* dname)
2073{
2074 uint8_t* d= dname+1;
2075 uint8_t len = *d++;
2076 uint8_t i;
2077 if (dname[0]<=1) {
2078 fprintf(out, ".");
2079 return;
2080 }
2081 while (len) {
2082 ods_log_assert(d - (dname+1) <= dname[0]);
2083 for (i=0; i<len; i++) {
2084 uint8_t ch = *d++;
2085 if (isalnum(ch) || ch == '-' || ch == '_') {
2086 fprintf(out, "%c", ch);
2087 } else if (ch == '.' || ch == '\\') {
2088 fprintf(out, "\\%c", ch);
2089 } else {
2090 fprintf(out, "\\%03u", (unsigned int)ch);
2091 }
2092 }
2093 fprintf(out, ".");
2094 len = *d++;
2095 }
2096 return;
2097}
2098
2099
2104static void
2105xfrd_backup(xfrd_type* xfrd)
2106{
2107 zone_type* zone = (zone_type*) xfrd->zone;
2108 char* file = NULL;
2109 int timeout = 0;
2110 FILE* fd = NULL;
2111 if (zone && zone->name) {
2112 file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
2113 if (file) {
2114 fd = ods_fopen(file, NULL, "w");
2115 if (fd) {
2116 if (xfrd->handler.timeout) {
2117 timeout = xfrd->timeout.tv_sec;
2118 }
2119 fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
2120 fprintf(fd, ";;Zone: name %s ttl %u mname ",
2121 zone->name,
2122 (unsigned) xfrd->soa.ttl);
2123 xfrd_backup_dname(fd, xfrd->soa.mname),
2124 fprintf(fd, " rname ");
2125 xfrd_backup_dname(fd, xfrd->soa.rname),
2126 fprintf(fd, " serial %u refresh %u retry %u expire %u "
2127 "minimum %u\n",
2128 (unsigned) xfrd->soa.serial,
2129 (unsigned) xfrd->soa.refresh,
2130 (unsigned) xfrd->soa.retry,
2131 (unsigned) xfrd->soa.expire,
2132 (unsigned) xfrd->soa.minimum);
2133 fprintf(fd, ";;Master: num %d next %d round %d timeout %d\n",
2134 xfrd->master_num,
2135 xfrd->next_master,
2136 xfrd->round_num,
2137 timeout);
2138 fprintf(fd, ";;Serial: xfr %u %u notify %u %u disk %u %u\n",
2139 (unsigned) xfrd->serial_xfr,
2140 (unsigned) xfrd->serial_xfr_acquired,
2141 (unsigned) xfrd->serial_notify,
2142 (unsigned) xfrd->serial_notify_acquired,
2143 (unsigned) xfrd->serial_disk,
2144 (unsigned) xfrd->serial_disk_acquired);
2145 fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
2146 ods_fclose(fd);
2147 }
2148 free(file);
2149 }
2150 }
2151}
2152
2153
2158static void
2159xfrd_unlink(xfrd_type* xfrd)
2160{
2161 zone_type* zone = (zone_type*) xfrd->zone;
2162 char* file = NULL;
2163 if (zone && zone->name) {
2164 ods_log_info("[%s] unlink zone %s xfrd state", xfrd_str, zone->name);
2165 file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
2166 if (file) {
2167 (void)unlink(file);
2168 free(file);
2169 }
2170 }
2171}
2172
2173
2178void
2179xfrd_cleanup(xfrd_type* xfrd, int backup)
2180{
2181 if (!xfrd) {
2182 return;
2183 }
2184 /* backup */
2185 if (backup) {
2186 xfrd_backup(xfrd);
2187 } else {
2188 xfrd_unlink(xfrd);
2189 }
2190
2191 tsig_rr_cleanup(xfrd->tsig_rr);
2192 pthread_mutex_destroy(&xfrd->serial_lock);
2193 pthread_mutex_destroy(&xfrd->rw_lock);
2194 free(xfrd);
2195}
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:290
#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:2179
#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