forked from NLnetLabs/nsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ixfr.c
2726 lines (2523 loc) · 77.2 KB
/
ixfr.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ixfr.c -- generating IXFR responses.
*
* Copyright (c) 2021, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include <unistd.h>
#include "ixfr.h"
#include "packet.h"
#include "rdata.h"
#include "axfr.h"
#include "options.h"
#include "zonec.h"
#include "zone.h"
/*
* For optimal compression IXFR response packets are limited in size
* to MAX_COMPRESSION_OFFSET.
*/
#define IXFR_MAX_MESSAGE_LEN MAX_COMPRESSION_OFFSET
/* draft-ietf-dnsop-rfc2845bis-06, section 5.3.1 says to sign every packet */
#define IXFR_TSIG_SIGN_EVERY_NTH 0 /* tsig sign every N packets. */
/* initial space in rrs data for storing records */
#define IXFR_STORE_INITIAL_SIZE 4096
/* store compression for one name */
struct rrcompress_entry {
/* rbtree node, key is this struct */
struct rbnode node;
/* the uncompressed domain name */
const uint8_t* dname;
/* the length of the dname, includes terminating 0 label */
uint16_t len;
/* the offset of the dname in the packet */
uint16_t offset;
};
/* structure to store compression data for the packet */
struct pktcompression {
/* rbtree of rrcompress_entry. sorted by dname */
struct rbtree tree;
/* allocation information, how many bytes allocated now */
size_t alloc_now;
/* allocation information, total size in block */
size_t alloc_max;
/* region to use if block full, this is NULL if unused */
struct region* region;
/* block of temp data for allocation */
uint8_t block[sizeof(struct rrcompress_entry)*1024];
};
/* compare two elements in the compression tree. Returns -1, 0, or 1. */
static int compression_cmp(const void* a, const void* b)
{
struct rrcompress_entry* rra = (struct rrcompress_entry*)a;
struct rrcompress_entry* rrb = (struct rrcompress_entry*)b;
if(rra->len != rrb->len) {
if(rra->len < rrb->len)
return -1;
return 1;
}
return memcmp(rra->dname, rrb->dname, rra->len);
}
/* init the pktcompression to a new packet */
static void pktcompression_init(struct pktcompression* pcomp)
{
pcomp->alloc_now = 0;
pcomp->alloc_max = sizeof(pcomp->block);
pcomp->region = NULL;
pcomp->tree.root = RBTREE_NULL;
pcomp->tree.count = 0;
pcomp->tree.region = NULL;
pcomp->tree.cmp = &compression_cmp;
}
/* freeup the pktcompression data */
static void pktcompression_freeup(struct pktcompression* pcomp)
{
if(pcomp->region) {
region_destroy(pcomp->region);
pcomp->region = NULL;
}
pcomp->alloc_now = 0;
pcomp->tree.root = RBTREE_NULL;
pcomp->tree.count = 0;
}
/* alloc data in pktcompression */
static void* pktcompression_alloc(struct pktcompression* pcomp, size_t s)
{
/* first attempt to allocate in the fixed block,
* that is very fast and on the stack in the pcomp struct */
if(pcomp->alloc_now + s <= pcomp->alloc_max) {
void* ret = pcomp->block + pcomp->alloc_now;
pcomp->alloc_now += s;
return ret;
}
/* if that fails, create a region to allocate in,
* it is freed in the freeup */
if(!pcomp->region) {
pcomp->region = region_create(xalloc, free);
if(!pcomp->region)
return NULL;
}
return region_alloc(pcomp->region, s);
}
/* find a pktcompression name, return offset if found */
static uint16_t pktcompression_find(struct pktcompression* pcomp,
const uint8_t* dname, size_t len)
{
struct rrcompress_entry key, *found;
key.node.key = &key;
key.dname = dname;
key.len = len;
found = (struct rrcompress_entry*)rbtree_search(&pcomp->tree, &key);
if(found) return found->offset;
return 0;
}
/* insert a new domain name into the compression tree.
* it fails silently, no need to compress then. */
static void pktcompression_insert(struct pktcompression* pcomp,
const uint8_t* dname, size_t len, uint16_t offset)
{
struct rrcompress_entry* entry;
if(len > 65535)
return;
if(offset > MAX_COMPRESSION_OFFSET)
return; /* too far for a compression pointer */
entry = pktcompression_alloc(pcomp, sizeof(*entry));
if(!entry)
return;
memset(&entry->node, 0, sizeof(entry->node));
entry->node.key = entry;
entry->dname = dname;
entry->len = len;
entry->offset = offset;
(void)rbtree_insert(&pcomp->tree, &entry->node);
}
/* insert all the labels of a domain name */
static void pktcompression_insert_with_labels(struct pktcompression* pcomp,
uint8_t* dname, size_t len, uint16_t offset)
{
if(!dname)
return;
if(offset > MAX_COMPRESSION_OFFSET)
return;
/* while we have not seen the end root label */
while(len > 0 && dname[0] != 0) {
size_t lablen;
pktcompression_insert(pcomp, dname, len, offset);
lablen = (size_t)(dname[0]);
if( (lablen&0xc0) )
return; /* the dname should be uncompressed */
if(lablen+1 > len)
return; /* len should be uncompressed wireformat len */
if(offset > MAX_COMPRESSION_OFFSET - lablen - 1)
return; /* offset moves too far for compression */
/* skip label */
len -= lablen+1;
dname += lablen+1;
offset += lablen+1;
}
}
/* calculate length of dname in uncompressed wireformat in buffer */
static size_t dname_length(const uint8_t* buf, size_t len)
{
size_t l = 0;
if(!buf || len == 0)
return l;
while(len > 0 && buf[0] != 0) {
size_t lablen = (size_t)(buf[0]);
if( (lablen&0xc0) )
return 0; /* the name should be uncompressed */
if(lablen+1 > len)
return 0; /* should fit in the buffer */
l += lablen+1;
len -= lablen+1;
buf += lablen+1;
}
if(len == 0)
return 0; /* end label should fit in buffer */
if(buf[0] != 0)
return 0; /* must end in root label */
l += 1; /* for the end root label */
return l;
}
/* write a compressed domain name into the packet,
* returns uncompressed wireformat length,
* 0 if it does not fit and -1 on failure, bad dname. */
static int pktcompression_write_dname(struct buffer* packet,
struct pktcompression* pcomp, const uint8_t* rr, size_t rrlen)
{
size_t wirelen = 0;
size_t dname_len = dname_length(rr, rrlen);
if(!rr || rrlen == 0 || dname_len == 0)
return 0;
while(rrlen > 0 && rr[0] != 0) {
size_t lablen = (size_t)(rr[0]);
uint16_t offset;
if( (lablen&0xc0) )
return -1; /* name should be uncompressed */
if(lablen+1 > rrlen)
return -1; /* name should fit */
/* see if the domain name has a compression pointer */
if((offset=pktcompression_find(pcomp, rr, dname_len))!=0) {
if(!buffer_available(packet, 2))
return 0;
buffer_write_u16(packet, (uint16_t)(0xc000 | offset));
wirelen += dname_len;
return wirelen;
} else {
if(!buffer_available(packet, lablen+1))
return 0;
/* insert the domain name at this position */
pktcompression_insert(pcomp, rr, dname_len,
buffer_position(packet));
/* write it */
buffer_write(packet, rr, lablen+1);
}
wirelen += lablen+1;
rr += lablen+1;
rrlen -= lablen+1;
dname_len -= lablen+1;
}
if(rrlen > 0 && rr[0] == 0) {
/* write end root label */
if(!buffer_available(packet, 1))
return 0;
buffer_write_u8(packet, 0);
wirelen += 1;
}
return wirelen;
}
/* write an RR into the packet with compression for domain names,
* return 0 and resets position if it does not fit in the packet. */
static int ixfr_write_rr_pkt(struct query* query, struct buffer* packet,
struct pktcompression* pcomp, const uint8_t* rr, size_t rrlen,
uint16_t total_added)
{
size_t oldpos = buffer_position(packet);
size_t rdpos;
uint16_t tp;
int dname_len;
size_t rdlen;
size_t i;
rrtype_descriptor_type* descriptor;
if(total_added == 0) {
size_t oldmaxlen = query->maxlen;
/* RR > 16K can be first RR */
query->maxlen = (query->tcp?TCP_MAX_MESSAGE_LEN:UDP_MAX_MESSAGE_LEN);
if(query_overflow(query)) {
query->maxlen = oldmaxlen;
return 0;
}
query->maxlen = oldmaxlen;
} else {
if(buffer_position(packet) > MAX_COMPRESSION_OFFSET
|| query_overflow(query)) {
/* we are past the maximum length */
return 0;
}
}
/* write owner */
dname_len = pktcompression_write_dname(packet, pcomp, rr, rrlen);
if(dname_len == -1)
return 1; /* attempt to skip this malformed rr, could assert */
if(dname_len == 0) {
buffer_set_position(packet, oldpos);
return 0;
}
rr += dname_len;
rrlen -= dname_len;
/* type, class, ttl, rdatalen */
if(!buffer_available(packet, 10)) {
buffer_set_position(packet, oldpos);
return 0;
}
if(10 > rrlen)
return 1; /* attempt to skip this malformed rr, could assert */
tp = read_uint16(rr);
buffer_write(packet, rr, 8);
rr += 8;
rrlen -= 8;
rdlen = read_uint16(rr);
rr += 2;
rrlen -= 2;
rdpos = buffer_position(packet);
buffer_write_u16(packet, 0);
if(rdlen > rrlen)
return 1; /* attempt to skip this malformed rr, could assert */
/* rdata */
descriptor = rrtype_descriptor_by_type(tp);
for(i=0; i<descriptor->maximum; i++) {
size_t copy_len = 0;
if(rdlen == 0)
break;
switch(rdata_atom_wireformat_type(tp, i)) {
case RDATA_WF_COMPRESSED_DNAME:
dname_len = pktcompression_write_dname(packet, pcomp,
rr, rdlen);
if(dname_len == -1)
return 1; /* attempt to skip malformed rr */
if(dname_len == 0) {
buffer_set_position(packet, oldpos);
return 0;
}
rr += dname_len;
rdlen -= dname_len;
break;
case RDATA_WF_UNCOMPRESSED_DNAME:
case RDATA_WF_LITERAL_DNAME:
copy_len = rdlen;
break;
case RDATA_WF_BYTE:
copy_len = 1;
break;
case RDATA_WF_SHORT:
copy_len = 2;
break;
case RDATA_WF_LONG:
copy_len = 4;
break;
case RDATA_WF_TEXTS:
case RDATA_WF_LONG_TEXT:
copy_len = rdlen;
break;
case RDATA_WF_TEXT:
case RDATA_WF_BINARYWITHLENGTH:
copy_len = 1;
if(rdlen > copy_len)
copy_len += rr[0];
break;
case RDATA_WF_A:
copy_len = 4;
break;
case RDATA_WF_AAAA:
copy_len = 16;
break;
case RDATA_WF_ILNP64:
copy_len = 8;
break;
case RDATA_WF_EUI48:
copy_len = EUI48ADDRLEN;
break;
case RDATA_WF_EUI64:
copy_len = EUI64ADDRLEN;
break;
case RDATA_WF_BINARY:
copy_len = rdlen;
break;
case RDATA_WF_APL:
copy_len = (sizeof(uint16_t) /* address family */
+ sizeof(uint8_t) /* prefix */
+ sizeof(uint8_t)); /* length */
if(copy_len <= rdlen)
copy_len += (rr[copy_len-1]&APL_LENGTH_MASK);
break;
case RDATA_WF_IPSECGATEWAY:
copy_len = rdlen;
break;
case RDATA_WF_SVCPARAM:
copy_len = 4;
if(copy_len <= rdlen)
copy_len += read_uint16(rr+2);
break;
default:
copy_len = rdlen;
break;
}
if(copy_len) {
if(!buffer_available(packet, copy_len)) {
buffer_set_position(packet, oldpos);
return 0;
}
if(copy_len > rdlen)
return 1; /* assert of skip malformed */
buffer_write(packet, rr, copy_len);
rr += copy_len;
rdlen -= copy_len;
}
}
/* write compressed rdata length */
buffer_write_u16_at(packet, rdpos, buffer_position(packet)-rdpos-2);
if(total_added == 0) {
size_t oldmaxlen = query->maxlen;
query->maxlen = (query->tcp?TCP_MAX_MESSAGE_LEN:UDP_MAX_MESSAGE_LEN);
if(query_overflow(query)) {
query->maxlen = oldmaxlen;
buffer_set_position(packet, oldpos);
return 0;
}
query->maxlen = oldmaxlen;
} else {
if(query_overflow(query)) {
/* we are past the maximum length */
buffer_set_position(packet, oldpos);
return 0;
}
}
return 1;
}
/* parse the serial number from the IXFR query */
static int parse_qserial(struct buffer* packet, uint32_t* qserial,
size_t* snip_pos)
{
unsigned int i;
uint16_t type, rdlen;
/* we must have a SOA in the authority section */
if(NSCOUNT(packet) == 0)
return 0;
/* skip over the question section, we want only one */
buffer_set_position(packet, QHEADERSZ);
if(QDCOUNT(packet) != 1)
return 0;
if(!packet_skip_rr(packet, 1))
return 0;
/* set position to snip off the authority section */
*snip_pos = buffer_position(packet);
/* skip over the authority section RRs until we find the SOA */
for(i=0; i<NSCOUNT(packet); i++) {
/* is this the SOA record? */
if(!packet_skip_dname(packet))
return 0; /* malformed name */
if(!buffer_available(packet, 10))
return 0; /* no type,class,ttl,rdatalen */
type = buffer_read_u16(packet);
buffer_skip(packet, 6);
rdlen = buffer_read_u16(packet);
if(!buffer_available(packet, rdlen))
return 0;
if(type == TYPE_SOA) {
/* read serial from rdata, skip two dnames, then
* read the 32bit value */
if(!packet_skip_dname(packet))
return 0; /* malformed nsname */
if(!packet_skip_dname(packet))
return 0; /* malformed rname */
if(!buffer_available(packet, 4))
return 0;
*qserial = buffer_read_u32(packet);
return 1;
}
buffer_skip(packet, rdlen);
}
return 0;
}
/* get serial from SOA RR */
static uint32_t soa_rr_get_serial(struct rr* rr)
{
if(rr->rdata_count < 3)
return 0;
if(rr->rdatas[2].data[0] < 4)
return 0;
return read_uint32(&rr->rdatas[2].data[1]);
}
/* get the current serial from the zone */
uint32_t zone_get_current_serial(struct zone* zone)
{
if(!zone || !zone->soa_rrset)
return 0;
if(zone->soa_rrset->rr_count == 0)
return 0;
if(zone->soa_rrset->rrs[0].rdata_count < 3)
return 0;
if(zone->soa_rrset->rrs[0].rdatas[2].data[0] < 4)
return 0;
return read_uint32(&zone->soa_rrset->rrs[0].rdatas[2].data[1]);
}
/* iterator over ixfr data. find first element, eg. oldest zone version
* change.
* The iterator can be started with the ixfr_data_first, but also with
* ixfr_data_last, or with an existing ixfr_data element to start from.
* Continue by using ixfr_data_next or ixfr_data_prev to ask for more elements
* until that returns NULL. NULL because end of list or loop was detected.
* The ixfr_data_prev uses a counter, start it at 0, it returns NULL when
* a loop is detected.
*/
static struct ixfr_data* ixfr_data_first(struct zone_ixfr* ixfr)
{
struct ixfr_data* n;
if(!ixfr || !ixfr->data || ixfr->data->count==0)
return NULL;
n = (struct ixfr_data*)rbtree_search(ixfr->data, &ixfr->oldest_serial);
if(!n || n == (struct ixfr_data*)RBTREE_NULL)
return NULL;
return n;
}
/* iterator over ixfr data. find last element, eg. newest zone version
* change. */
static struct ixfr_data* ixfr_data_last(struct zone_ixfr* ixfr)
{
struct ixfr_data* n;
if(!ixfr || !ixfr->data || ixfr->data->count==0)
return NULL;
n = (struct ixfr_data*)rbtree_search(ixfr->data, &ixfr->newest_serial);
if(!n || n == (struct ixfr_data*)RBTREE_NULL)
return NULL;
return n;
}
/* iterator over ixfr data. fetch next item. If loop or nothing, NULL */
static struct ixfr_data* ixfr_data_next(struct zone_ixfr* ixfr,
struct ixfr_data* cur)
{
struct ixfr_data* n;
if(!cur || cur == (struct ixfr_data*)RBTREE_NULL)
return NULL;
if(cur->oldserial == ixfr->newest_serial)
return NULL; /* that was the last element */
n = (struct ixfr_data*)rbtree_next(&cur->node);
if(n && n != (struct ixfr_data*)RBTREE_NULL &&
cur->newserial == n->oldserial) {
/* the next rbtree item is the next ixfr data item */
return n;
}
/* If the next item is last of tree, and we have to loop around,
* the search performs the lookup for the next item we need.
* If the next item exists, but also is not connected, the search
* finds the correct connected ixfr in the sorted tree. */
/* try searching for the correct ixfr data item */
n = (struct ixfr_data*)rbtree_search(ixfr->data, &cur->newserial);
if(!n || n == (struct ixfr_data*)RBTREE_NULL)
return NULL;
return n;
}
/* iterator over ixfr data. fetch the previous item. If loop or nothing NULL.*/
static struct ixfr_data* ixfr_data_prev(struct zone_ixfr* ixfr,
struct ixfr_data* cur, size_t* prevcount)
{
struct ixfr_data* prev;
if(!cur || cur == (struct ixfr_data*)RBTREE_NULL)
return NULL;
if(cur->oldserial == ixfr->oldest_serial)
return NULL; /* this was the first element */
prev = (struct ixfr_data*)rbtree_previous(&cur->node);
if(!prev || prev == (struct ixfr_data*)RBTREE_NULL) {
/* We hit the first element in the tree, go again
* at the last one. Wrap around. */
prev = (struct ixfr_data*)rbtree_last(ixfr->data);
}
while(prev && prev != (struct ixfr_data*)RBTREE_NULL) {
if(prev->newserial == cur->oldserial) {
/* This is the correct matching previous ixfr data */
/* Increase the prevcounter every time the routine
* returns an item, and if that becomes too large, we
* are in a loop. in that case, stop. */
if(prevcount) {
(*prevcount)++;
if(*prevcount > ixfr->data->count + 12) {
/* Larger than the max number of items
* plus a small margin. The longest
* chain is all the ixfr elements in
* the tree. It loops. */
return NULL;
}
}
return prev;
}
prev = (struct ixfr_data*)rbtree_previous(&prev->node);
if(!prev || prev == (struct ixfr_data*)RBTREE_NULL) {
/* We hit the first element in the tree, go again
* at the last one. Wrap around. */
prev = (struct ixfr_data*)rbtree_last(ixfr->data);
}
}
/* no elements in list */
return NULL;
}
/* connect IXFRs, return true if connected, false if not. Return last serial */
static int connect_ixfrs(struct zone_ixfr* ixfr, struct ixfr_data* data,
uint32_t* end_serial)
{
struct ixfr_data* p = data;
while(p != NULL) {
struct ixfr_data* next = ixfr_data_next(ixfr, p);
if(next) {
if(p->newserial != next->oldserial) {
/* These ixfrs are not connected,
* during IXFR processing that could already
* have been deleted, but we check here
* in any case */
return 0;
}
} else {
/* the chain of IXFRs ends in this serial number */
*end_serial = p->newserial;
}
p = next;
}
return 1;
}
/* Count length of next record in data */
static size_t count_rr_length(const uint8_t* data, size_t data_len,
size_t current)
{
uint8_t label_size;
uint16_t rdlen;
size_t i = current;
if(current >= data_len)
return 0;
/* pass the owner dname */
while(1) {
if(i+1 > data_len)
return 0;
label_size = data[i++];
if(label_size == 0) {
break;
} else if((label_size &0xc0) != 0) {
return 0; /* uncompressed dnames in IXFR store */
} else if(i+label_size > data_len) {
return 0;
} else {
i += label_size;
}
}
/* after dname, we pass type, class, ttl, rdatalen */
if(i+10 > data_len)
return 0;
i += 8;
rdlen = read_uint16(data+i);
i += 2;
/* pass over the rdata */
if(i+((size_t)rdlen) > data_len)
return 0;
i += ((size_t)rdlen);
return i-current;
}
/* Copy RRs into packet until packet full, return number RRs added */
static uint16_t ixfr_copy_rrs_into_packet(struct query* query,
struct pktcompression* pcomp)
{
uint16_t total_added = 0;
/* Copy RRs into the packet until the answer is full,
* when an RR does not fit, we return and add no more. */
/* Add first SOA */
if(query->ixfr_count_newsoa < query->ixfr_end_data->newsoa_len) {
/* the new SOA is added from the end_data segment, it is
* the final SOA of the result of the IXFR */
if(ixfr_write_rr_pkt(query, query->packet, pcomp,
query->ixfr_end_data->newsoa,
query->ixfr_end_data->newsoa_len, total_added)) {
query->ixfr_count_newsoa = query->ixfr_end_data->newsoa_len;
total_added++;
query->ixfr_pos_of_newsoa = buffer_position(query->packet);
} else {
/* cannot add another RR, so return */
return total_added;
}
}
/* Add second SOA */
if(query->ixfr_count_oldsoa < query->ixfr_data->oldsoa_len) {
if(ixfr_write_rr_pkt(query, query->packet, pcomp,
query->ixfr_data->oldsoa,
query->ixfr_data->oldsoa_len, total_added)) {
query->ixfr_count_oldsoa = query->ixfr_data->oldsoa_len;
total_added++;
} else {
/* cannot add another RR, so return */
return total_added;
}
}
/* Add del data, with deleted RRs and a SOA */
while(query->ixfr_count_del < query->ixfr_data->del_len) {
size_t rrlen = count_rr_length(query->ixfr_data->del,
query->ixfr_data->del_len, query->ixfr_count_del);
if(rrlen && ixfr_write_rr_pkt(query, query->packet, pcomp,
query->ixfr_data->del + query->ixfr_count_del,
rrlen, total_added)) {
query->ixfr_count_del += rrlen;
total_added++;
} else {
/* the next record does not fit in the remaining
* space of the packet */
return total_added;
}
}
/* Add add data, with added RRs and a SOA */
while(query->ixfr_count_add < query->ixfr_data->add_len) {
size_t rrlen = count_rr_length(query->ixfr_data->add,
query->ixfr_data->add_len, query->ixfr_count_add);
if(rrlen && ixfr_write_rr_pkt(query, query->packet, pcomp,
query->ixfr_data->add + query->ixfr_count_add,
rrlen, total_added)) {
query->ixfr_count_add += rrlen;
total_added++;
} else {
/* the next record does not fit in the remaining
* space of the packet */
return total_added;
}
}
return total_added;
}
query_state_type query_ixfr(struct nsd *nsd, struct query *query)
{
uint16_t total_added = 0;
struct pktcompression pcomp;
if (query->ixfr_is_done)
return QUERY_PROCESSED;
pktcompression_init(&pcomp);
if (query->maxlen > IXFR_MAX_MESSAGE_LEN)
query->maxlen = IXFR_MAX_MESSAGE_LEN;
assert(!query_overflow(query));
/* only keep running values for most packets */
query->tsig_prepare_it = 0;
query->tsig_update_it = 1;
if(query->tsig_sign_it) {
/* prepare for next updates */
query->tsig_prepare_it = 1;
query->tsig_sign_it = 0;
}
if (query->ixfr_data == NULL) {
/* This is the first packet, process the query further */
uint32_t qserial = 0, current_serial = 0, end_serial = 0;
struct zone* zone;
struct ixfr_data* ixfr_data;
size_t oldpos;
STATUP(nsd, rixfr);
/* parse the serial number from the IXFR request */
oldpos = QHEADERSZ;
if(!parse_qserial(query->packet, &qserial, &oldpos)) {
NSCOUNT_SET(query->packet, 0);
ARCOUNT_SET(query->packet, 0);
buffer_set_position(query->packet, oldpos);
RCODE_SET(query->packet, RCODE_FORMAT);
return QUERY_PROCESSED;
}
NSCOUNT_SET(query->packet, 0);
ARCOUNT_SET(query->packet, 0);
buffer_set_position(query->packet, oldpos);
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "ixfr query routine, %s IXFR=%u",
dname_to_string(query->qname, NULL), (unsigned)qserial));
/* do we have an IXFR with this serial number? If not, serve AXFR */
zone = namedb_find_zone(nsd->db, query->qname);
if(!zone) {
/* no zone is present */
RCODE_SET(query->packet, RCODE_NOTAUTH);
return QUERY_PROCESSED;
}
ZTATUP(nsd, zone, rixfr);
/* if the query is for same or newer serial than our current
* serial, then serve a single SOA with our current serial */
current_serial = zone_get_current_serial(zone);
if(compare_serial(qserial, current_serial) >= 0) {
if(!zone->soa_rrset || zone->soa_rrset->rr_count != 1){
RCODE_SET(query->packet, RCODE_SERVFAIL);
return QUERY_PROCESSED;
}
query_add_compression_domain(query, zone->apex,
QHEADERSZ);
if(packet_encode_rr(query, zone->apex,
&zone->soa_rrset->rrs[0],
zone->soa_rrset->rrs[0].ttl)) {
ANCOUNT_SET(query->packet, 1);
} else {
RCODE_SET(query->packet, RCODE_SERVFAIL);
}
AA_SET(query->packet);
query_clear_compression_tables(query);
if(query->tsig.status == TSIG_OK)
query->tsig_sign_it = 1;
return QUERY_PROCESSED;
}
if(!zone->ixfr) {
/* we have no ixfr information for the zone, make an AXFR */
if(query->tsig_prepare_it)
query->tsig_sign_it = 1;
VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, no ixfr info for zone: %s",
dname_to_string(query->qname, NULL)));
return query_axfr(nsd, query, 0);
}
ixfr_data = zone_ixfr_find_serial(zone->ixfr, qserial);
if(!ixfr_data) {
/* the specific version is not available, make an AXFR */
if(query->tsig_prepare_it)
query->tsig_sign_it = 1;
VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, no history for serial for zone: %s",
dname_to_string(query->qname, NULL)));
return query_axfr(nsd, query, 0);
}
/* see if the IXFRs connect to the next IXFR, and if it ends
* at the current served zone, if not, AXFR */
if(!connect_ixfrs(zone->ixfr, ixfr_data, &end_serial) ||
end_serial != current_serial) {
if(query->tsig_prepare_it)
query->tsig_sign_it = 1;
VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, incomplete history from this serial for zone: %s",
dname_to_string(query->qname, NULL)));
return query_axfr(nsd, query, 0);
}
query->zone = zone;
query->ixfr_data = ixfr_data;
query->ixfr_is_done = 0;
/* set up to copy the last version's SOA as first SOA */
query->ixfr_end_data = ixfr_data_last(zone->ixfr);
query->ixfr_count_newsoa = 0;
query->ixfr_count_oldsoa = 0;
query->ixfr_count_del = 0;
query->ixfr_count_add = 0;
query->ixfr_pos_of_newsoa = 0;
/* the query name can be compressed to */
pktcompression_insert_with_labels(&pcomp,
buffer_at(query->packet, QHEADERSZ),
query->qname->name_size, QHEADERSZ);
if(query->tsig.status == TSIG_OK) {
query->tsig_sign_it = 1; /* sign first packet in stream */
}
} else {
/*
* Query name need not be repeated after the
* first response packet.
*/
buffer_set_limit(query->packet, QHEADERSZ);
QDCOUNT_SET(query->packet, 0);
query_prepare_response(query);
}
total_added = ixfr_copy_rrs_into_packet(query, &pcomp);
while(query->ixfr_count_add >= query->ixfr_data->add_len) {
struct ixfr_data* next = ixfr_data_next(query->zone->ixfr,
query->ixfr_data);
/* finished the ixfr_data */
if(next) {
/* move to the next IXFR */
query->ixfr_data = next;
/* we need to skip the SOA records, set len to done*/
/* the newsoa count is already done, at end_data len */
query->ixfr_count_oldsoa = next->oldsoa_len;
/* and then set up to copy the del and add sections */
query->ixfr_count_del = 0;
query->ixfr_count_add = 0;
total_added += ixfr_copy_rrs_into_packet(query, &pcomp);
} else {
/* we finished the IXFR */
/* sign the last packet */
query->tsig_sign_it = 1;
query->ixfr_is_done = 1;
break;
}
}
/* return the answer */
AA_SET(query->packet);
ANCOUNT_SET(query->packet, total_added);
NSCOUNT_SET(query->packet, 0);
ARCOUNT_SET(query->packet, 0);
if(!query->tcp && !query->ixfr_is_done) {
TC_SET(query->packet);
if(query->ixfr_pos_of_newsoa) {
/* if we recorded the newsoa in the result, snip off
* the rest of the response, the RFC1995 response for
* when it does not fit is only the latest SOA */
buffer_set_position(query->packet, query->ixfr_pos_of_newsoa);
ANCOUNT_SET(query->packet, 1);
}
query->ixfr_is_done = 1;
}
/* check if it needs tsig signatures */
if(query->tsig.status == TSIG_OK) {
#if IXFR_TSIG_SIGN_EVERY_NTH > 0
if(query->tsig.updates_since_last_prepare >= IXFR_TSIG_SIGN_EVERY_NTH) {
#endif
query->tsig_sign_it = 1;
#if IXFR_TSIG_SIGN_EVERY_NTH > 0
}
#endif
}
pktcompression_freeup(&pcomp);
return QUERY_IN_IXFR;
}
/* free ixfr_data structure */
static void ixfr_data_free(struct ixfr_data* data)
{
if(!data)
return;
free(data->newsoa);
free(data->oldsoa);
free(data->del);
free(data->add);
free(data->log_str);
free(data);
}
size_t ixfr_data_size(struct ixfr_data* data)
{
return sizeof(struct ixfr_data) + data->newsoa_len + data->oldsoa_len
+ data->del_len + data->add_len;
}
struct ixfr_store* ixfr_store_start(struct zone* zone,
struct ixfr_store* ixfr_store_mem)
{
struct ixfr_store* ixfr_store = ixfr_store_mem;
memset(ixfr_store, 0, sizeof(*ixfr_store));
ixfr_store->zone = zone;
ixfr_store->data = xalloc_zero(sizeof(*ixfr_store->data));
return ixfr_store;
}
void ixfr_store_cancel(struct ixfr_store* ixfr_store)
{
ixfr_store->cancelled = 1;
ixfr_data_free(ixfr_store->data);
ixfr_store->data = NULL;
}
void ixfr_store_free(struct ixfr_store* ixfr_store)
{
if(!ixfr_store)
return;
ixfr_data_free(ixfr_store->data);
}
/* make space in record data for the new size, grows the allocation */
static void ixfr_rrs_make_space(uint8_t** rrs, size_t* len, size_t* capacity,
size_t added)
{
size_t newsize = 0;
if(*rrs == NULL) {
newsize = IXFR_STORE_INITIAL_SIZE;
} else {
if(*len + added <= *capacity)
return; /* already enough space */
newsize = (*capacity)*2;
}
if(*len + added > newsize)
newsize = *len + added;
if(*rrs == NULL) {
*rrs = xalloc(newsize);
} else {
*rrs = xrealloc(*rrs, newsize);
}
*capacity = newsize;
}
/* put new SOA record after delrrs and addrrs */
static void ixfr_put_newsoa(struct ixfr_store* ixfr_store, uint8_t** rrs,
size_t* len, size_t* capacity)
{