forked from openbsd/ports
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xfrd-notify.c
570 lines (525 loc) · 16.2 KB
/
xfrd-notify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
* xfrd-notify.c - notify sending routines
*
* Copyright (c) 2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "xfrd-notify.h"
#include "xfrd.h"
#include "xfrd-tcp.h"
#include "packet.h"
#define XFRD_NOTIFY_RETRY_TIMOUT 3 /* seconds between retries sending NOTIFY */
/* start sending notifies */
static void notify_enable(struct notify_zone* zone,
struct xfrd_soa* new_soa);
/* setup the notify active state */
static void setup_notify_active(struct notify_zone* zone);
/* handle zone notify send */
static void xfrd_handle_notify_send(int fd, short event, void* arg);
static int xfrd_notify_send_udp(struct notify_zone* zone, int index);
static void
notify_send_disable(struct notify_zone* zone)
{
zone->notify_send_enable = 0;
event_del(&zone->notify_send_handler);
if(zone->notify_send_handler.ev_fd != -1) {
close(zone->notify_send_handler.ev_fd);
zone->notify_send_handler.ev_fd = -1;
}
}
static void
notify_send6_disable(struct notify_zone* zone)
{
zone->notify_send6_enable = 0;
event_del(&zone->notify_send6_handler);
if(zone->notify_send6_handler.ev_fd != -1) {
close(zone->notify_send6_handler.ev_fd);
zone->notify_send6_handler.ev_fd = -1;
}
}
void
notify_disable(struct notify_zone* zone)
{
zone->notify_current = 0;
/* if added, then remove */
if(zone->notify_send_enable) {
notify_send_disable(zone);
}
if(zone->notify_send6_enable) {
notify_send6_disable(zone);
}
if(xfrd->notify_udp_num == XFRD_MAX_UDP_NOTIFY) {
/* find next waiting and needy zone */
while(xfrd->notify_waiting_first) {
/* snip off */
struct notify_zone* wz = xfrd->notify_waiting_first;
assert(wz->is_waiting);
wz->is_waiting = 0;
xfrd->notify_waiting_first = wz->waiting_next;
if(wz->waiting_next)
wz->waiting_next->waiting_prev = NULL;
if(xfrd->notify_waiting_last == wz)
xfrd->notify_waiting_last = NULL;
/* see if this zone needs notify sending */
if(wz->notify_current) {
DEBUG(DEBUG_XFRD,1, (LOG_INFO,
"xfrd: zone %s: notify off waiting list.",
zone->apex_str) );
setup_notify_active(wz);
return;
}
}
}
xfrd->notify_udp_num--;
}
void
init_notify_send(rbtree_type* tree, region_type* region,
struct zone_options* options)
{
struct notify_zone* not = (struct notify_zone*)
region_alloc(region, sizeof(struct notify_zone));
memset(not, 0, sizeof(struct notify_zone));
not->apex = options->node.key;
not->apex_str = options->name;
not->node.key = not->apex;
not->options = options;
/* if master zone and have a SOA */
not->current_soa = (struct xfrd_soa*)region_alloc(region,
sizeof(struct xfrd_soa));
memset(not->current_soa, 0, sizeof(struct xfrd_soa));
not->notify_send_handler.ev_fd = -1;
not->notify_send6_handler.ev_fd = -1;
not->is_waiting = 0;
not->notify_send_enable = 0;
not->notify_send6_enable = 0;
tsig_create_record_custom(¬->notify_tsig, NULL, 0, 0, 4);
not->notify_current = 0;
rbtree_insert(tree, (rbnode_type*)not);
}
void
xfrd_del_notify(xfrd_state_type* xfrd, const dname_type* dname)
{
/* find it */
struct notify_zone* not = (struct notify_zone*)rbtree_delete(
xfrd->notify_zones, dname);
if(!not)
return;
/* waiting list */
if(not->is_waiting) {
if(not->waiting_prev)
not->waiting_prev->waiting_next = not->waiting_next;
else xfrd->notify_waiting_first = not->waiting_next;
if(not->waiting_next)
not->waiting_next->waiting_prev = not->waiting_prev;
else xfrd->notify_waiting_last = not->waiting_prev;
not->is_waiting = 0;
}
/* event */
if(not->notify_send_enable || not->notify_send6_enable) {
notify_disable(not);
}
/* del tsig */
tsig_delete_record(¬->notify_tsig, NULL);
/* free it */
region_recycle(xfrd->region, not->current_soa, sizeof(xfrd_soa_type));
/* the apex is recycled when the zone_options.node.key is removed */
region_recycle(xfrd->region, not, sizeof(*not));
}
static int
reply_pkt_is_ack(struct notify_zone* zone, buffer_type* packet, int index)
{
if((OPCODE(packet) != OPCODE_NOTIFY) ||
(QR(packet) == 0)) {
log_msg(LOG_ERR, "xfrd: zone %s: received bad notify reply opcode/flags from %s",
zone->apex_str, zone->pkts[index].dest->ip_address_spec);
return 0;
}
/* we know it is OPCODE NOTIFY, QUERY_REPLY and for this zone */
if(ID(packet) != zone->pkts[index].notify_query_id) {
log_msg(LOG_ERR, "xfrd: zone %s: received notify-ack with bad ID from %s",
zone->apex_str, zone->pkts[index].dest->ip_address_spec);
return 0;
}
/* could check tsig, but why. The reply does not cause processing. */
if(RCODE(packet) != RCODE_OK) {
log_msg(LOG_ERR, "xfrd: zone %s: received notify response error %s from %s",
zone->apex_str, rcode2str(RCODE(packet)),
zone->pkts[index].dest->ip_address_spec);
if(RCODE(packet) == RCODE_IMPL)
return 1; /* rfc1996: notimpl notify reply: consider retries done */
return 0;
}
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s: host %s acknowledges notify",
zone->apex_str, zone->pkts[index].dest->ip_address_spec));
return 1;
}
/* compare sockaddr and acl_option addr and port numbers */
static int
cmp_addr_equal(struct sockaddr* a, socklen_t a_len, struct acl_options* dest)
{
if(dest) {
unsigned int destport = ((dest->port == 0)?
(unsigned)atoi(TCP_PORT):dest->port);
#ifdef INET6
struct sockaddr_storage* a1 = (struct sockaddr_storage*)a;
if(a1->ss_family == AF_INET6 && dest->is_ipv6) {
struct sockaddr_in6* a2 = (struct sockaddr_in6*)a;
if(a_len < sizeof(struct sockaddr_in6))
return 0; /* too small */
if(ntohs(a2->sin6_port) != destport)
return 0; /* different port number */
if(memcmp(&a2->sin6_addr, &dest->addr.addr6,
sizeof(struct in6_addr)) != 0)
return 0; /* different address */
return 1;
}
if(a1->ss_family == AF_INET6 || dest->is_ipv6)
return 0; /* different address family */
else {
#endif /* INET6 */
struct sockaddr_in* a3 = (struct sockaddr_in*)a;
if(a_len < sizeof(struct sockaddr_in))
return 0; /* too small */
if(ntohs(a3->sin_port) != destport)
return 0; /* different port number */
if(memcmp(&a3->sin_addr, &dest->addr.addr,
sizeof(struct in_addr)) != 0)
return 0; /* different address */
return 1;
#ifdef INET6
}
#endif
}
return 0;
}
static void
notify_pkt_done(struct notify_zone* zone, int index)
{
zone->pkts[index].dest = NULL;
zone->pkts[index].notify_retry = 0;
zone->pkts[index].send_time = 0;
zone->pkts[index].notify_query_id = 0;
zone->notify_pkt_count--;
}
static void
notify_pkt_retry(struct notify_zone* zone, int index)
{
if(++zone->pkts[index].notify_retry >=
zone->options->pattern->notify_retry) {
log_msg(LOG_ERR, "xfrd: zone %s: max notify send count reached, %s unreachable",
zone->apex_str,
zone->pkts[index].dest->ip_address_spec);
notify_pkt_done(zone, index);
return;
}
if(!xfrd_notify_send_udp(zone, index)) {
notify_pkt_retry(zone, index);
}
}
static void
xfrd_handle_notify_reply(struct notify_zone* zone, buffer_type* packet,
struct sockaddr* src, socklen_t srclen)
{
int i;
for(i=0; i<NOTIFY_CONCURRENT_MAX; i++) {
/* is this entry in use */
if(!zone->pkts[i].dest)
continue;
/* based on destination */
if(!cmp_addr_equal(src, srclen, zone->pkts[i].dest))
continue;
if(reply_pkt_is_ack(zone, packet, i)) {
/* is done */
notify_pkt_done(zone, i);
return;
} else {
/* retry */
notify_pkt_retry(zone, i);
return;
}
}
}
static int
xfrd_notify_send_udp(struct notify_zone* zone, int index)
{
buffer_type* packet = xfrd_get_temp_buffer();
if(!zone->pkts[index].dest) return 0;
/* send NOTIFY to secondary. */
xfrd_setup_packet(packet, TYPE_SOA, CLASS_IN, zone->apex,
qid_generate());
zone->pkts[index].notify_query_id = ID(packet);
OPCODE_SET(packet, OPCODE_NOTIFY);
AA_SET(packet);
if(zone->current_soa->serial != 0) {
/* add current SOA to answer section */
ANCOUNT_SET(packet, 1);
xfrd_write_soa_buffer(packet, zone->apex, zone->current_soa);
}
if(zone->pkts[index].dest->key_options) {
xfrd_tsig_sign_request(packet, &zone->notify_tsig, zone->pkts[index].dest);
}
buffer_flip(packet);
if((zone->pkts[index].dest->is_ipv6
&& zone->notify_send6_handler.ev_fd == -1) ||
(!zone->pkts[index].dest->is_ipv6
&& zone->notify_send_handler.ev_fd == -1)) {
/* open fd */
int fd = xfrd_send_udp(zone->pkts[index].dest, packet,
zone->options->pattern->outgoing_interface);
if(fd == -1) {
log_msg(LOG_ERR, "xfrd: zone %s: could not send notify #%d to %s",
zone->apex_str, zone->pkts[index].notify_retry,
zone->pkts[index].dest->ip_address_spec);
return 0;
}
if(zone->pkts[index].dest->is_ipv6)
zone->notify_send6_handler.ev_fd = fd;
else zone->notify_send_handler.ev_fd = fd;
} else {
/* send on existing fd */
#ifdef INET6
struct sockaddr_storage to;
#else
struct sockaddr_in to;
#endif /* INET6 */
int fd;
socklen_t to_len = xfrd_acl_sockaddr_to(
zone->pkts[index].dest, &to);
if(zone->pkts[index].dest->is_ipv6)
fd = zone->notify_send6_handler.ev_fd;
else fd = zone->notify_send_handler.ev_fd;
if(sendto(fd,
buffer_current(packet), buffer_remaining(packet), 0,
(struct sockaddr*)&to, to_len) == -1) {
log_msg(LOG_ERR, "xfrd notify: sendto %s failed %s",
zone->pkts[index].dest->ip_address_spec,
strerror(errno));
return 0;
}
}
zone->pkts[index].send_time = time(NULL);
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s: sent notify #%d to %s",
zone->apex_str, zone->pkts[index].notify_retry,
zone->pkts[index].dest->ip_address_spec));
return 1;
}
static void
notify_timeout_check(struct notify_zone* zone)
{
time_t now = time(NULL);
int i;
for(i=0; i<NOTIFY_CONCURRENT_MAX; i++) {
if(!zone->pkts[i].dest)
continue;
if(now >= zone->pkts[i].send_time + XFRD_NOTIFY_RETRY_TIMOUT) {
notify_pkt_retry(zone, i);
}
}
}
static void
notify_start_pkts(struct notify_zone* zone)
{
int i;
if(!zone->notify_current) return; /* no more acl to send to */
for(i=0; i<NOTIFY_CONCURRENT_MAX; i++) {
/* while loop, in case the retries all fail, and we can
* start another on this slot, or run out of notify acls */
while(zone->pkts[i].dest==NULL && zone->notify_current) {
zone->pkts[i].dest = zone->notify_current;
zone->notify_current = zone->notify_current->next;
zone->pkts[i].notify_retry = 0;
zone->pkts[i].notify_query_id = 0;
zone->pkts[i].send_time = 0;
zone->notify_pkt_count++;
if(!xfrd_notify_send_udp(zone, i)) {
notify_pkt_retry(zone, i);
}
}
}
}
static void
notify_setup_event(struct notify_zone* zone)
{
if(zone->notify_send_handler.ev_fd != -1) {
int fd = zone->notify_send_handler.ev_fd;
if(zone->notify_send_enable) {
event_del(&zone->notify_send_handler);
}
zone->notify_timeout.tv_sec = XFRD_NOTIFY_RETRY_TIMOUT;
memset(&zone->notify_send_handler, 0,
sizeof(zone->notify_send_handler));
event_set(&zone->notify_send_handler, fd, EV_READ | EV_TIMEOUT,
xfrd_handle_notify_send, zone);
if(event_base_set(xfrd->event_base, &zone->notify_send_handler) != 0)
log_msg(LOG_ERR, "notify_send: event_base_set failed");
if(event_add(&zone->notify_send_handler, &zone->notify_timeout) != 0)
log_msg(LOG_ERR, "notify_send: event_add failed");
zone->notify_send_enable = 1;
}
if(zone->notify_send6_handler.ev_fd != -1) {
int fd = zone->notify_send6_handler.ev_fd;
if(zone->notify_send6_enable) {
event_del(&zone->notify_send6_handler);
}
zone->notify_timeout.tv_sec = XFRD_NOTIFY_RETRY_TIMOUT;
memset(&zone->notify_send6_handler, 0,
sizeof(zone->notify_send6_handler));
event_set(&zone->notify_send6_handler, fd, EV_READ | EV_TIMEOUT,
xfrd_handle_notify_send, zone);
if(event_base_set(xfrd->event_base, &zone->notify_send6_handler) != 0)
log_msg(LOG_ERR, "notify_send: event_base_set failed");
if(event_add(&zone->notify_send6_handler, &zone->notify_timeout) != 0)
log_msg(LOG_ERR, "notify_send: event_add failed");
zone->notify_send6_enable = 1;
}
}
static void
xfrd_handle_notify_send(int fd, short event, void* arg)
{
struct notify_zone* zone = (struct notify_zone*)arg;
buffer_type* packet = xfrd_get_temp_buffer();
if(zone->is_waiting) {
DEBUG(DEBUG_XFRD,1, (LOG_INFO,
"xfrd: notify waiting, skipped, %s", zone->apex_str));
return;
}
if((event & EV_READ)) {
struct sockaddr_storage src;
socklen_t srclen = (socklen_t)sizeof(src);
DEBUG(DEBUG_XFRD,1, (LOG_INFO,
"xfrd: zone %s: read notify ACK", zone->apex_str));
assert(fd != -1);
if(xfrd_udp_read_packet(packet, fd, (struct sockaddr*)&src,
&srclen)) {
/* find entry, send retry or make entry NULL */
xfrd_handle_notify_reply(zone, packet,
(struct sockaddr*)&src, srclen);
}
}
if((event & EV_TIMEOUT)) {
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s: notify timeout",
zone->apex_str));
/* timeout, try again */
}
/* see which pkts have timeouted, retry or NULL them */
notify_timeout_check(zone);
/* start new packets if we have empty space */
notify_start_pkts(zone);
/* see if we are done */
if(!zone->notify_current && !zone->notify_pkt_count) {
/* we are done */
DEBUG(DEBUG_XFRD,1, (LOG_INFO,
"xfrd: zone %s: no more notify-send acls. stop notify.",
zone->apex_str));
notify_disable(zone);
return;
}
notify_setup_event(zone);
}
static void
setup_notify_active(struct notify_zone* zone)
{
zone->notify_pkt_count = 0;
memset(zone->pkts, 0, sizeof(zone->pkts));
zone->notify_current = zone->options->pattern->notify;
zone->notify_timeout.tv_sec = 0;
zone->notify_timeout.tv_usec = 0;
if(zone->notify_send_enable)
notify_send_disable(zone);
memset(&zone->notify_send_handler, 0,
sizeof(zone->notify_send_handler));
event_set(&zone->notify_send_handler, -1, EV_TIMEOUT,
xfrd_handle_notify_send, zone);
if(event_base_set(xfrd->event_base, &zone->notify_send_handler) != 0)
log_msg(LOG_ERR, "notifysend: event_base_set failed");
if(evtimer_add(&zone->notify_send_handler, &zone->notify_timeout) != 0)
log_msg(LOG_ERR, "notifysend: evtimer_add failed");
zone->notify_send_enable = 1;
}
static void
notify_enable(struct notify_zone* zone, struct xfrd_soa* new_soa)
{
if(!zone->options->pattern->notify) {
return; /* no notify acl, nothing to do */
}
if(new_soa == NULL)
memset(zone->current_soa, 0, sizeof(xfrd_soa_type));
else
memcpy(zone->current_soa, new_soa, sizeof(xfrd_soa_type));
if(zone->is_waiting)
return;
if(xfrd->notify_udp_num < XFRD_MAX_UDP_NOTIFY) {
setup_notify_active(zone);
xfrd->notify_udp_num++;
return;
}
/* put it in waiting list */
zone->notify_current = zone->options->pattern->notify;
zone->is_waiting = 1;
zone->waiting_next = NULL;
zone->waiting_prev = xfrd->notify_waiting_last;
if(xfrd->notify_waiting_last) {
xfrd->notify_waiting_last->waiting_next = zone;
} else {
xfrd->notify_waiting_first = zone;
}
xfrd->notify_waiting_last = zone;
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s: notify on waiting list.",
zone->apex_str));
}
void
xfrd_notify_start(struct notify_zone* zone, struct xfrd_state* xfrd)
{
xfrd_zone_type* xz;
if(zone->is_waiting || zone->notify_send_enable ||
zone->notify_send6_enable)
return;
xz = (xfrd_zone_type*)rbtree_search(xfrd->zones, zone->apex);
if(xz && xz->soa_nsd_acquired)
notify_enable(zone, &xz->soa_nsd);
else notify_enable(zone, NULL);
}
void
xfrd_send_notify(rbtree_type* tree, const dname_type* apex, struct xfrd_soa* new_soa)
{
/* lookup the zone */
struct notify_zone* zone = (struct notify_zone*)
rbtree_search(tree, apex);
assert(zone);
if(zone->notify_send_enable || zone->notify_send6_enable)
notify_disable(zone);
notify_enable(zone, new_soa);
}
void
notify_handle_master_zone_soainfo(rbtree_type* tree,
const dname_type* apex, struct xfrd_soa* new_soa)
{
/* lookup the zone */
struct notify_zone* zone = (struct notify_zone*)
rbtree_search(tree, apex);
if(!zone) return; /* got SOAINFO but zone was deleted meanwhile */
/* check if SOA changed */
if( (new_soa == NULL && zone->current_soa->serial == 0) ||
(new_soa && new_soa->serial == zone->current_soa->serial))
return;
if(zone->notify_send_enable || zone->notify_send6_enable)
notify_disable(zone);
notify_enable(zone, new_soa);
}
void
close_notify_fds(rbtree_type* tree)
{
struct notify_zone* zone;
RBTREE_FOR(zone, struct notify_zone*, tree)
{
if(zone->notify_send_enable || zone->notify_send6_enable)
notify_send_disable(zone);
}
}