2 * util/data/msgparse.c - parse wireformat DNS messages.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Routines for message parsing a packet buffer to a descriptive structure.
40 #include "util/data/msgparse.h"
41 #include "util/data/dname.h"
42 #include "util/data/packed_rrset.h"
43 #include "util/storage/lookup3.h"
44 #include "util/regional.h"
45 #include "ldns/rrdef.h"
46 #include "ldns/sbuffer.h"
47 #include "ldns/parseutil.h"
48 #include "ldns/wire2str.h"
50 /** smart comparison of (compressed, valid) dnames from packet */
52 smart_compare(sldns_buffer
* pkt
, uint8_t* dnow
,
53 uint8_t* dprfirst
, uint8_t* dprlast
)
55 if(LABEL_IS_PTR(*dnow
)) {
56 /* ptr points to a previous dname */
57 uint8_t* p
= sldns_buffer_at(pkt
, PTR_OFFSET(dnow
[0], dnow
[1]));
58 if( p
== dprfirst
|| p
== dprlast
)
60 /* prev dname is also a ptr, both ptrs are the same. */
61 if(LABEL_IS_PTR(*dprlast
) &&
62 dprlast
[0] == dnow
[0] && dprlast
[1] == dnow
[1])
65 return dname_pkt_compare(pkt
, dnow
, dprlast
);
69 * Allocate new rrset in region, fill with data.
71 static struct rrset_parse
*
72 new_rrset(struct msg_parse
* msg
, uint8_t* dname
, size_t dnamelen
,
73 uint16_t type
, uint16_t dclass
, hashvalue_t hash
,
74 uint32_t rrset_flags
, sldns_pkt_section section
,
75 struct regional
* region
)
77 struct rrset_parse
* p
= regional_alloc(region
, sizeof(*p
));
79 p
->rrset_bucket_next
= msg
->hashtable
[hash
& (PARSE_TABLE_SIZE
-1)];
80 msg
->hashtable
[hash
& (PARSE_TABLE_SIZE
-1)] = p
;
81 p
->rrset_all_next
= 0;
83 msg
->rrset_last
->rrset_all_next
= p
;
84 else msg
->rrset_first
= p
;
89 p
->dname_len
= dnamelen
;
91 p
->rrset_class
= dclass
;
92 p
->flags
= rrset_flags
;
103 /** See if next rrset is nsec at zone apex */
105 nsec_at_apex(sldns_buffer
* pkt
)
107 /* we are at ttl position in packet. */
108 size_t pos
= sldns_buffer_position(pkt
);
110 if(sldns_buffer_remaining(pkt
) < 7) /* ttl+len+root */
112 sldns_buffer_skip(pkt
, 4); /* ttl */;
113 rdatalen
= sldns_buffer_read_u16(pkt
);
114 if(sldns_buffer_remaining(pkt
) < rdatalen
) {
115 sldns_buffer_set_position(pkt
, pos
);
116 return 0; /* parse error happens later */
118 /* must validate the nsec next domain name format */
119 if(pkt_dname_len(pkt
) == 0) {
120 sldns_buffer_set_position(pkt
, pos
);
121 return 0; /* parse error */
124 /* see if SOA bit is set. */
125 if(sldns_buffer_position(pkt
) < pos
+4+rdatalen
) {
126 /* nsec type bitmap contains items */
127 uint8_t win
, blen
, bits
;
128 /* need: windownum, bitmap len, firstbyte */
129 if(sldns_buffer_position(pkt
)+3 > pos
+4+rdatalen
) {
130 sldns_buffer_set_position(pkt
, pos
);
131 return 0; /* malformed nsec */
133 win
= sldns_buffer_read_u8(pkt
);
134 blen
= sldns_buffer_read_u8(pkt
);
135 bits
= sldns_buffer_read_u8(pkt
);
136 /* 0window always first window. bitlen >=1 or parse
137 error really. bit 0x2 is SOA. */
138 if(win
== 0 && blen
>= 1 && (bits
& 0x02)) {
139 sldns_buffer_set_position(pkt
, pos
);
144 sldns_buffer_set_position(pkt
, pos
);
148 /** Calculate rrset flags */
150 pkt_rrset_flags(sldns_buffer
* pkt
, uint16_t type
, sldns_pkt_section sec
)
153 if(type
== LDNS_RR_TYPE_NSEC
&& nsec_at_apex(pkt
)) {
154 f
|= PACKED_RRSET_NSEC_AT_APEX
;
155 } else if(type
== LDNS_RR_TYPE_SOA
&& sec
== LDNS_SECTION_AUTHORITY
) {
156 f
|= PACKED_RRSET_SOA_NEG
;
162 pkt_hash_rrset(sldns_buffer
* pkt
, uint8_t* dname
, uint16_t type
,
163 uint16_t dclass
, uint32_t rrset_flags
)
165 /* note this MUST be identical to rrset_key_hash in packed_rrset.c */
166 /* this routine handles compressed names */
167 hashvalue_t h
= 0xab;
168 h
= dname_pkt_hash(pkt
, dname
, h
);
169 h
= hashlittle(&type
, sizeof(type
), h
); /* host order */
170 h
= hashlittle(&dclass
, sizeof(dclass
), h
); /* netw order */
171 h
= hashlittle(&rrset_flags
, sizeof(uint32_t), h
);
175 /** create partial dname hash for rrset hash */
177 pkt_hash_rrset_first(sldns_buffer
* pkt
, uint8_t* dname
)
179 /* works together with pkt_hash_rrset_rest */
180 /* note this MUST be identical to rrset_key_hash in packed_rrset.c */
181 /* this routine handles compressed names */
182 hashvalue_t h
= 0xab;
183 h
= dname_pkt_hash(pkt
, dname
, h
);
187 /** create a rrset hash from a partial dname hash */
189 pkt_hash_rrset_rest(hashvalue_t dname_h
, uint16_t type
, uint16_t dclass
,
190 uint32_t rrset_flags
)
192 /* works together with pkt_hash_rrset_first */
193 /* note this MUST be identical to rrset_key_hash in packed_rrset.c */
195 h
= hashlittle(&type
, sizeof(type
), dname_h
); /* host order */
196 h
= hashlittle(&dclass
, sizeof(dclass
), h
); /* netw order */
197 h
= hashlittle(&rrset_flags
, sizeof(uint32_t), h
);
201 /** compare rrset_parse with data */
203 rrset_parse_equals(struct rrset_parse
* p
, sldns_buffer
* pkt
, hashvalue_t h
,
204 uint32_t rrset_flags
, uint8_t* dname
, size_t dnamelen
,
205 uint16_t type
, uint16_t dclass
)
207 if(p
->hash
== h
&& p
->dname_len
== dnamelen
&& p
->type
== type
&&
208 p
->rrset_class
== dclass
&& p
->flags
== rrset_flags
&&
209 dname_pkt_compare(pkt
, dname
, p
->dname
) == 0)
216 msgparse_hashtable_lookup(struct msg_parse
* msg
, sldns_buffer
* pkt
,
217 hashvalue_t h
, uint32_t rrset_flags
, uint8_t* dname
, size_t dnamelen
,
218 uint16_t type
, uint16_t dclass
)
220 struct rrset_parse
* p
= msg
->hashtable
[h
& (PARSE_TABLE_SIZE
-1)];
222 if(rrset_parse_equals(p
, pkt
, h
, rrset_flags
, dname
, dnamelen
,
225 p
= p
->rrset_bucket_next
;
230 /** return type networkformat that rrsig in packet covers */
232 pkt_rrsig_covered(sldns_buffer
* pkt
, uint8_t* here
, uint16_t* type
)
234 size_t pos
= sldns_buffer_position(pkt
);
235 sldns_buffer_set_position(pkt
, (size_t)(here
-sldns_buffer_begin(pkt
)));
236 /* ttl + len + size of small rrsig(rootlabel, no signature) */
237 if(sldns_buffer_remaining(pkt
) < 4+2+19)
239 sldns_buffer_skip(pkt
, 4); /* ttl */
240 if(sldns_buffer_read_u16(pkt
) < 19) /* too short */ {
241 sldns_buffer_set_position(pkt
, pos
);
244 *type
= sldns_buffer_read_u16(pkt
);
245 sldns_buffer_set_position(pkt
, pos
);
249 /** true if covered type equals prevtype */
251 pkt_rrsig_covered_equals(sldns_buffer
* pkt
, uint8_t* here
, uint16_t type
)
254 if(pkt_rrsig_covered(pkt
, here
, &t
) && t
== type
)
260 msgparse_bucket_remove(struct msg_parse
* msg
, struct rrset_parse
* rrset
)
262 struct rrset_parse
** p
;
263 p
= &msg
->hashtable
[ rrset
->hash
& (PARSE_TABLE_SIZE
-1) ];
266 *p
= rrset
->rrset_bucket_next
;
269 p
= &( (*p
)->rrset_bucket_next
);
273 /** change section of rrset from previous to current section */
275 change_section(struct msg_parse
* msg
, struct rrset_parse
* rrset
,
276 sldns_pkt_section section
)
278 struct rrset_parse
*p
, *prev
;
279 /* remove from list */
280 if(section
== rrset
->section
)
282 p
= msg
->rrset_first
;
286 if(prev
) prev
->rrset_all_next
= p
->rrset_all_next
;
287 else msg
->rrset_first
= p
->rrset_all_next
;
288 if(msg
->rrset_last
== rrset
)
289 msg
->rrset_last
= prev
;
293 p
= p
->rrset_all_next
;
295 /* remove from count */
296 switch(rrset
->section
) {
297 case LDNS_SECTION_ANSWER
: msg
->an_rrsets
--; break;
298 case LDNS_SECTION_AUTHORITY
: msg
->ns_rrsets
--; break;
299 case LDNS_SECTION_ADDITIONAL
: msg
->ar_rrsets
--; break;
300 default: log_assert(0);
302 /* insert at end of list */
303 rrset
->rrset_all_next
= 0;
305 msg
->rrset_last
->rrset_all_next
= rrset
;
306 else msg
->rrset_first
= rrset
;
307 msg
->rrset_last
= rrset
;
308 /* up count of new section */
310 case LDNS_SECTION_AUTHORITY
: msg
->ns_rrsets
++; break;
311 case LDNS_SECTION_ADDITIONAL
: msg
->ar_rrsets
++; break;
312 default: log_assert(0);
314 rrset
->section
= section
;
317 /** see if rrset of type RRSIG contains sig over given type */
319 rrset_has_sigover(sldns_buffer
* pkt
, struct rrset_parse
* rrset
, uint16_t type
,
323 struct rr_parse
* rr
= rrset
->rr_first
;
324 log_assert( rrset
->type
== LDNS_RR_TYPE_RRSIG
);
326 if(pkt_rrsig_covered_equals(pkt
, rr
->ttl_data
, type
))
334 /** move rrsigs from sigset to dataset */
336 moveover_rrsigs(sldns_buffer
* pkt
, struct regional
* region
,
337 struct rrset_parse
* sigset
, struct rrset_parse
* dataset
, int duplicate
)
339 struct rr_parse
* sig
= sigset
->rr_first
;
340 struct rr_parse
* prev
= NULL
;
341 struct rr_parse
* insert
;
342 struct rr_parse
* nextsig
;
345 if(pkt_rrsig_covered_equals(pkt
, sig
->ttl_data
,
349 insert
= (struct rr_parse
*)regional_alloc(
350 region
, sizeof(struct rr_parse
));
351 if(!insert
) return 0;
352 insert
->outside_packet
= 0;
353 insert
->ttl_data
= sig
->ttl_data
;
354 insert
->size
= sig
->size
;
357 /* remove from sigset */
358 if(prev
) prev
->next
= sig
->next
;
359 else sigset
->rr_first
= sig
->next
;
360 if(sigset
->rr_last
== sig
)
361 sigset
->rr_last
= prev
;
363 sigset
->size
-= sig
->size
;
365 /* prev not changed */
368 dataset
->rrsig_count
++;
370 if(dataset
->rrsig_last
)
371 dataset
->rrsig_last
->next
= insert
;
372 else dataset
->rrsig_first
= insert
;
373 dataset
->rrsig_last
= insert
;
374 dataset
->size
+= insert
->size
;
383 /** change an rrsig rrset for use as data rrset */
384 static struct rrset_parse
*
385 change_rrsig_rrset(struct rrset_parse
* sigset
, struct msg_parse
* msg
,
386 sldns_buffer
* pkt
, uint16_t datatype
, uint32_t rrset_flags
,
387 int hasother
, sldns_pkt_section section
, struct regional
* region
)
389 struct rrset_parse
* dataset
= sigset
;
390 hashvalue_t hash
= pkt_hash_rrset(pkt
, sigset
->dname
, datatype
,
391 sigset
->rrset_class
, rrset_flags
);
392 log_assert( sigset
->type
== LDNS_RR_TYPE_RRSIG
);
393 log_assert( datatype
!= LDNS_RR_TYPE_RRSIG
);
395 /* need to make new rrset to hold data type */
396 dataset
= new_rrset(msg
, sigset
->dname
, sigset
->dname_len
,
397 datatype
, sigset
->rrset_class
, hash
, rrset_flags
,
402 case LDNS_SECTION_ANSWER
: msg
->an_rrsets
++; break;
403 case LDNS_SECTION_AUTHORITY
: msg
->ns_rrsets
++; break;
404 case LDNS_SECTION_ADDITIONAL
: msg
->ar_rrsets
++; break;
405 default: log_assert(0);
407 if(!moveover_rrsigs(pkt
, region
, sigset
, dataset
,
408 msg
->qtype
== LDNS_RR_TYPE_RRSIG
||
409 (msg
->qtype
== LDNS_RR_TYPE_ANY
&&
410 section
!= LDNS_SECTION_ANSWER
) ))
414 /* changeover the type of the rrset to data set */
415 msgparse_bucket_remove(msg
, dataset
);
416 /* insert into new hash bucket */
417 dataset
->rrset_bucket_next
= msg
->hashtable
[hash
&(PARSE_TABLE_SIZE
-1)];
418 msg
->hashtable
[hash
&(PARSE_TABLE_SIZE
-1)] = dataset
;
419 dataset
->hash
= hash
;
420 /* use section of data item for result */
421 change_section(msg
, dataset
, section
);
422 dataset
->type
= datatype
;
423 dataset
->flags
= rrset_flags
;
424 dataset
->rrsig_count
+= dataset
->rr_count
;
425 dataset
->rr_count
= 0;
426 /* move sigs to end of siglist */
427 if(dataset
->rrsig_last
)
428 dataset
->rrsig_last
->next
= dataset
->rr_first
;
429 else dataset
->rrsig_first
= dataset
->rr_first
;
430 dataset
->rrsig_last
= dataset
->rr_last
;
431 dataset
->rr_first
= 0;
432 dataset
->rr_last
= 0;
436 /** Find rrset. If equal to previous it is fast. hash if not so.
437 * @param msg: the message with hash table.
438 * @param pkt: the packet in wireformat (needed for compression ptrs).
439 * @param dname: pointer to start of dname (compressed) in packet.
440 * @param dnamelen: uncompressed wirefmt length of dname.
441 * @param type: type of current rr.
442 * @param dclass: class of current rr.
443 * @param hash: hash value is returned if the rrset could not be found.
444 * @param rrset_flags: is returned if the rrset could not be found.
445 * @param prev_dname_first: dname of last seen RR. First seen dname.
446 * @param prev_dname_last: dname of last seen RR. Last seen dname.
447 * @param prev_dnamelen: dname len of last seen RR.
448 * @param prev_type: type of last seen RR.
449 * @param prev_dclass: class of last seen RR.
450 * @param rrset_prev: last seen RRset.
451 * @param section: the current section in the packet.
452 * @param region: used to allocate temporary parsing data.
453 * @return 0 on out of memory.
456 find_rrset(struct msg_parse
* msg
, sldns_buffer
* pkt
, uint8_t* dname
,
457 size_t dnamelen
, uint16_t type
, uint16_t dclass
, hashvalue_t
* hash
,
458 uint32_t* rrset_flags
,
459 uint8_t** prev_dname_first
, uint8_t** prev_dname_last
,
460 size_t* prev_dnamelen
, uint16_t* prev_type
,
461 uint16_t* prev_dclass
, struct rrset_parse
** rrset_prev
,
462 sldns_pkt_section section
, struct regional
* region
)
464 hashvalue_t dname_h
= pkt_hash_rrset_first(pkt
, dname
);
467 /* check if equal to previous item */
468 if(type
== *prev_type
&& dclass
== *prev_dclass
&&
469 dnamelen
== *prev_dnamelen
&&
470 smart_compare(pkt
, dname
, *prev_dname_first
,
471 *prev_dname_last
) == 0 &&
472 type
!= LDNS_RR_TYPE_RRSIG
) {
473 /* same as previous */
474 *prev_dname_last
= dname
;
477 /* check if rrsig over previous item */
478 if(type
== LDNS_RR_TYPE_RRSIG
&& dclass
== *prev_dclass
&&
479 pkt_rrsig_covered_equals(pkt
, sldns_buffer_current(pkt
),
481 smart_compare(pkt
, dname
, *prev_dname_first
,
482 *prev_dname_last
) == 0) {
483 /* covers previous */
484 *prev_dname_last
= dname
;
488 /* find by hashing and lookup in hashtable */
489 *rrset_flags
= pkt_rrset_flags(pkt
, type
, section
);
491 /* if rrsig - try to lookup matching data set first */
492 if(type
== LDNS_RR_TYPE_RRSIG
&& pkt_rrsig_covered(pkt
,
493 sldns_buffer_current(pkt
), &covtype
)) {
494 *hash
= pkt_hash_rrset_rest(dname_h
, covtype
, dclass
,
496 *rrset_prev
= msgparse_hashtable_lookup(msg
, pkt
, *hash
,
497 *rrset_flags
, dname
, dnamelen
, covtype
, dclass
);
498 if(!*rrset_prev
&& covtype
== LDNS_RR_TYPE_NSEC
) {
499 /* if NSEC try with NSEC apex bit twiddled */
500 *rrset_flags
^= PACKED_RRSET_NSEC_AT_APEX
;
501 *hash
= pkt_hash_rrset_rest(dname_h
, covtype
, dclass
,
503 *rrset_prev
= msgparse_hashtable_lookup(msg
, pkt
,
504 *hash
, *rrset_flags
, dname
, dnamelen
, covtype
,
506 if(!*rrset_prev
) /* untwiddle if not found */
507 *rrset_flags
^= PACKED_RRSET_NSEC_AT_APEX
;
509 if(!*rrset_prev
&& covtype
== LDNS_RR_TYPE_SOA
) {
510 /* if SOA try with SOA neg flag twiddled */
511 *rrset_flags
^= PACKED_RRSET_SOA_NEG
;
512 *hash
= pkt_hash_rrset_rest(dname_h
, covtype
, dclass
,
514 *rrset_prev
= msgparse_hashtable_lookup(msg
, pkt
,
515 *hash
, *rrset_flags
, dname
, dnamelen
, covtype
,
517 if(!*rrset_prev
) /* untwiddle if not found */
518 *rrset_flags
^= PACKED_RRSET_SOA_NEG
;
521 *prev_dname_first
= (*rrset_prev
)->dname
;
522 *prev_dname_last
= dname
;
523 *prev_dnamelen
= dnamelen
;
524 *prev_type
= covtype
;
525 *prev_dclass
= dclass
;
529 if(type
!= LDNS_RR_TYPE_RRSIG
) {
531 /* find matching rrsig */
532 *hash
= pkt_hash_rrset_rest(dname_h
, LDNS_RR_TYPE_RRSIG
,
534 *rrset_prev
= msgparse_hashtable_lookup(msg
, pkt
, *hash
,
535 0, dname
, dnamelen
, LDNS_RR_TYPE_RRSIG
,
537 if(*rrset_prev
&& rrset_has_sigover(pkt
, *rrset_prev
, type
,
540 *prev_dname_first
= (*rrset_prev
)->dname
;
541 *prev_dname_last
= dname
;
542 *prev_dnamelen
= dnamelen
;
544 *prev_dclass
= dclass
;
545 *rrset_prev
= change_rrsig_rrset(*rrset_prev
, msg
,
546 pkt
, type
, *rrset_flags
, hasother
, section
,
548 if(!*rrset_prev
) return 0;
553 *hash
= pkt_hash_rrset_rest(dname_h
, type
, dclass
, *rrset_flags
);
554 *rrset_prev
= msgparse_hashtable_lookup(msg
, pkt
, *hash
, *rrset_flags
,
555 dname
, dnamelen
, type
, dclass
);
557 *prev_dname_first
= (*rrset_prev
)->dname
;
558 else *prev_dname_first
= dname
;
559 *prev_dname_last
= dname
;
560 *prev_dnamelen
= dnamelen
;
562 *prev_dclass
= dclass
;
567 * Parse query section.
568 * @param pkt: packet, position at call must be at start of query section.
569 * at end position is after query section.
570 * @param msg: store results here.
571 * @return: 0 if OK, or rcode on error.
574 parse_query_section(sldns_buffer
* pkt
, struct msg_parse
* msg
)
576 if(msg
->qdcount
== 0)
579 return LDNS_RCODE_FORMERR
;
580 log_assert(msg
->qdcount
== 1);
581 if(sldns_buffer_remaining(pkt
) <= 0)
582 return LDNS_RCODE_FORMERR
;
583 msg
->qname
= sldns_buffer_current(pkt
);
584 if((msg
->qname_len
= pkt_dname_len(pkt
)) == 0)
585 return LDNS_RCODE_FORMERR
;
586 if(sldns_buffer_remaining(pkt
) < sizeof(uint16_t)*2)
587 return LDNS_RCODE_FORMERR
;
588 msg
->qtype
= sldns_buffer_read_u16(pkt
);
589 msg
->qclass
= sldns_buffer_read_u16(pkt
);
594 get_rdf_size(sldns_rdf_type rdf
)
597 case LDNS_RDF_TYPE_CLASS
:
598 case LDNS_RDF_TYPE_ALG
:
599 case LDNS_RDF_TYPE_INT8
:
602 case LDNS_RDF_TYPE_INT16
:
603 case LDNS_RDF_TYPE_TYPE
:
604 case LDNS_RDF_TYPE_CERT_ALG
:
607 case LDNS_RDF_TYPE_INT32
:
608 case LDNS_RDF_TYPE_TIME
:
609 case LDNS_RDF_TYPE_A
:
610 case LDNS_RDF_TYPE_PERIOD
:
613 case LDNS_RDF_TYPE_TSIGTIME
:
616 case LDNS_RDF_TYPE_AAAA
:
620 log_assert(0); /* add type above */
621 /* only types that appear before a domain *
622 * name are needed. rest is simply copied. */
627 /** calculate the size of one rr */
629 calc_size(sldns_buffer
* pkt
, uint16_t type
, struct rr_parse
* rr
)
631 const sldns_rr_descriptor
* desc
;
632 uint16_t pkt_len
; /* length of rr inside the packet */
633 rr
->size
= sizeof(uint16_t); /* the rdatalen */
634 sldns_buffer_skip(pkt
, 4); /* skip ttl */
635 pkt_len
= sldns_buffer_read_u16(pkt
);
636 if(sldns_buffer_remaining(pkt
) < pkt_len
)
638 desc
= sldns_rr_descript(type
);
639 if(pkt_len
> 0 && desc
&& desc
->_dname_count
> 0) {
640 int count
= (int)desc
->_dname_count
;
644 /* skip first part. */
645 while(pkt_len
> 0 && count
) {
646 switch(desc
->_wireformat
[rdf
]) {
647 case LDNS_RDF_TYPE_DNAME
:
648 /* decompress every domain name */
649 oldpos
= sldns_buffer_position(pkt
);
650 if((len
= pkt_dname_len(pkt
)) == 0)
651 return 0; /* malformed dname */
652 if(sldns_buffer_position(pkt
)-oldpos
> pkt_len
)
653 return 0; /* dname exceeds rdata */
654 pkt_len
-= sldns_buffer_position(pkt
)-oldpos
;
659 case LDNS_RDF_TYPE_STR
:
661 /* NOTREACHED, due to 'while(>0)' */
662 return 0; /* len byte exceeds rdata */
664 len
= sldns_buffer_current(pkt
)[0] + 1;
667 len
= get_rdf_size(desc
->_wireformat
[rdf
]);
671 return 0; /* exceeds rdata */
673 sldns_buffer_skip(pkt
, (ssize_t
)len
);
679 /* remaining rdata */
681 sldns_buffer_skip(pkt
, (ssize_t
)pkt_len
);
685 /** skip rr ttl and rdata */
687 skip_ttl_rdata(sldns_buffer
* pkt
)
690 if(sldns_buffer_remaining(pkt
) < 6) /* ttl + rdatalen */
692 sldns_buffer_skip(pkt
, 4); /* ttl */
693 rdatalen
= sldns_buffer_read_u16(pkt
);
694 if(sldns_buffer_remaining(pkt
) < rdatalen
)
696 sldns_buffer_skip(pkt
, (ssize_t
)rdatalen
);
700 /** see if RRSIG is a duplicate of another */
702 sig_is_double(sldns_buffer
* pkt
, struct rrset_parse
* rrset
, uint8_t* ttldata
)
704 uint16_t rlen
, siglen
;
705 size_t pos
= sldns_buffer_position(pkt
);
706 struct rr_parse
* sig
;
707 if(sldns_buffer_remaining(pkt
) < 6)
709 sldns_buffer_skip(pkt
, 4); /* ttl */
710 rlen
= sldns_buffer_read_u16(pkt
);
711 if(sldns_buffer_remaining(pkt
) < rlen
) {
712 sldns_buffer_set_position(pkt
, pos
);
715 sldns_buffer_set_position(pkt
, pos
);
717 sig
= rrset
->rrsig_first
;
719 /* check if rdatalen is same */
720 memmove(&siglen
, sig
->ttl_data
+4, sizeof(siglen
));
721 siglen
= ntohs(siglen
);
722 /* checks if data in packet is exactly the same, this means
723 * also dname in rdata is the same, but rrsig is not allowed
724 * to have compressed dnames anyway. If it is compressed anyway
725 * it will lead to duplicate rrs for qtype=RRSIG. (or ANY).
727 * Cannot use sig->size because size of the other one is not
731 if(siglen
>0 && memcmp(sig
->ttl_data
+6, ttldata
+6,
742 /** Add rr (from packet here) to rrset, skips rr */
744 add_rr_to_rrset(struct rrset_parse
* rrset
, sldns_buffer
* pkt
,
745 struct msg_parse
* msg
, struct regional
* region
,
746 sldns_pkt_section section
, uint16_t type
)
749 /* check section of rrset. */
750 if(rrset
->section
!= section
&& type
!= LDNS_RR_TYPE_RRSIG
&&
751 rrset
->type
!= LDNS_RR_TYPE_RRSIG
) {
752 /* silently drop it - we drop the last part, since
753 * trust in rr data depends on the section it is in.
754 * the less trustworthy part is discarded.
755 * also the last part is more likely to be incomplete.
756 * RFC 2181: must put RRset only once in response. */
758 verbose(VERB_QUERY, "Packet contains rrset data in "
759 "multiple sections, dropped last part.");
760 log_buf(VERB_QUERY, "packet was", pkt);
763 if(!skip_ttl_rdata(pkt
))
764 return LDNS_RCODE_FORMERR
;
768 if( (msg
->qtype
== LDNS_RR_TYPE_RRSIG
||
769 msg
->qtype
== LDNS_RR_TYPE_ANY
)
770 && sig_is_double(pkt
, rrset
, sldns_buffer_current(pkt
))) {
771 if(!skip_ttl_rdata(pkt
))
772 return LDNS_RCODE_FORMERR
;
777 if(!(rr
= (struct rr_parse
*)regional_alloc(region
, sizeof(*rr
))))
778 return LDNS_RCODE_SERVFAIL
;
779 rr
->outside_packet
= 0;
780 rr
->ttl_data
= sldns_buffer_current(pkt
);
782 if(type
== LDNS_RR_TYPE_RRSIG
&& rrset
->type
!= LDNS_RR_TYPE_RRSIG
) {
783 if(rrset
->rrsig_last
)
784 rrset
->rrsig_last
->next
= rr
;
785 else rrset
->rrsig_first
= rr
;
786 rrset
->rrsig_last
= rr
;
787 rrset
->rrsig_count
++;
790 rrset
->rr_last
->next
= rr
;
791 else rrset
->rr_first
= rr
;
796 /* calc decompressed size */
797 if(!calc_size(pkt
, type
, rr
))
798 return LDNS_RCODE_FORMERR
;
799 rrset
->size
+= rr
->size
;
805 * Parse packet RR section, for answer, authority and additional sections.
806 * @param pkt: packet, position at call must be at start of section.
807 * at end position is after section.
808 * @param msg: store results here.
809 * @param region: how to alloc results.
810 * @param section: section enum.
811 * @param num_rrs: how many rrs are in the section.
812 * @param num_rrsets: returns number of rrsets in the section.
813 * @return: 0 if OK, or rcode on error.
816 parse_section(sldns_buffer
* pkt
, struct msg_parse
* msg
,
817 struct regional
* region
, sldns_pkt_section section
,
818 uint16_t num_rrs
, size_t* num_rrsets
)
821 uint8_t* dname
, *prev_dname_f
= NULL
, *prev_dname_l
= NULL
;
822 size_t dnamelen
, prev_dnamelen
= 0;
823 uint16_t type
, prev_type
= 0;
824 uint16_t dclass
, prev_dclass
= 0;
825 uint32_t rrset_flags
= 0;
826 hashvalue_t hash
= 0;
827 struct rrset_parse
* rrset
= NULL
;
832 if(sldns_buffer_remaining(pkt
) <= 0)
833 return LDNS_RCODE_FORMERR
;
834 for(i
=0; i
<num_rrs
; i
++) {
836 dname
= sldns_buffer_current(pkt
);
837 if((dnamelen
= pkt_dname_len(pkt
)) == 0)
838 return LDNS_RCODE_FORMERR
;
839 if(sldns_buffer_remaining(pkt
) < 10) /* type, class, ttl, len */
840 return LDNS_RCODE_FORMERR
;
841 type
= sldns_buffer_read_u16(pkt
);
842 sldns_buffer_read(pkt
, &dclass
, sizeof(dclass
));
844 if(0) { /* debug show what is being parsed. */
845 if(type
== LDNS_RR_TYPE_RRSIG
) {
847 if(pkt_rrsig_covered(pkt
,
848 sldns_buffer_current(pkt
), &t
))
849 fprintf(stderr
, "parse of %s(%d) [%s(%d)]",
850 sldns_rr_descript(type
)?
851 sldns_rr_descript(type
)->_name
: "??",
853 sldns_rr_descript(t
)?
854 sldns_rr_descript(t
)->_name
: "??",
857 fprintf(stderr
, "parse of %s(%d)",
858 sldns_rr_descript(type
)?
859 sldns_rr_descript(type
)->_name
: "??",
861 fprintf(stderr
, " %s(%d) ",
862 sldns_lookup_by_id(sldns_rr_classes
,
863 (int)ntohs(dclass
))?sldns_lookup_by_id(
864 sldns_rr_classes
, (int)ntohs(dclass
))->name
:
865 "??", (int)ntohs(dclass
));
866 dname_print(stderr
, pkt
, dname
);
867 fprintf(stderr
, "\n");
870 /* see if it is part of an existing RR set */
871 if(!find_rrset(msg
, pkt
, dname
, dnamelen
, type
, dclass
, &hash
,
872 &rrset_flags
, &prev_dname_f
, &prev_dname_l
,
873 &prev_dnamelen
, &prev_type
, &prev_dclass
, &rrset
,
875 return LDNS_RCODE_SERVFAIL
;
877 /* it is a new RR set. hash&flags already calculated.*/
879 rrset
= new_rrset(msg
, dname
, dnamelen
, type
, dclass
,
880 hash
, rrset_flags
, section
, region
);
882 return LDNS_RCODE_SERVFAIL
;
885 fprintf(stderr
, "is part of existing: ");
886 dname_print(stderr
, pkt
, rrset
->dname
);
887 fprintf(stderr
, " type %s(%d)\n",
888 sldns_rr_descript(rrset
->type
)?
889 sldns_rr_descript(rrset
->type
)->_name
: "??",
893 if((r
=add_rr_to_rrset(rrset
, pkt
, msg
, region
, section
,
901 parse_packet(sldns_buffer
* pkt
, struct msg_parse
* msg
, struct regional
* region
)
904 if(sldns_buffer_remaining(pkt
) < LDNS_HEADER_SIZE
)
905 return LDNS_RCODE_FORMERR
;
906 /* read the header */
907 sldns_buffer_read(pkt
, &msg
->id
, sizeof(uint16_t));
908 msg
->flags
= sldns_buffer_read_u16(pkt
);
909 msg
->qdcount
= sldns_buffer_read_u16(pkt
);
910 msg
->ancount
= sldns_buffer_read_u16(pkt
);
911 msg
->nscount
= sldns_buffer_read_u16(pkt
);
912 msg
->arcount
= sldns_buffer_read_u16(pkt
);
914 return LDNS_RCODE_FORMERR
;
915 if((ret
= parse_query_section(pkt
, msg
)) != 0)
917 if((ret
= parse_section(pkt
, msg
, region
, LDNS_SECTION_ANSWER
,
918 msg
->ancount
, &msg
->an_rrsets
)) != 0)
920 if((ret
= parse_section(pkt
, msg
, region
, LDNS_SECTION_AUTHORITY
,
921 msg
->nscount
, &msg
->ns_rrsets
)) != 0)
923 if(sldns_buffer_remaining(pkt
) == 0 && msg
->arcount
== 1) {
924 /* BIND accepts leniently that an EDNS record is missing.
926 } else if((ret
= parse_section(pkt
, msg
, region
,
927 LDNS_SECTION_ADDITIONAL
, msg
->arcount
, &msg
->ar_rrsets
)) != 0)
929 /* if(sldns_buffer_remaining(pkt) > 0) { */
930 /* there is spurious data at end of packet. ignore */
932 msg
->rrset_count
= msg
->an_rrsets
+ msg
->ns_rrsets
+ msg
->ar_rrsets
;
937 parse_extract_edns(struct msg_parse
* msg
, struct edns_data
* edns
)
939 struct rrset_parse
* rrset
= msg
->rrset_first
;
940 struct rrset_parse
* prev
= 0;
941 struct rrset_parse
* found
= 0;
942 struct rrset_parse
* found_prev
= 0;
943 /* since the class encodes the UDP size, we cannot use hash table to
944 * find the EDNS OPT record. Scan the packet. */
946 if(rrset
->type
== LDNS_RR_TYPE_OPT
) {
947 /* only one OPT RR allowed. */
948 if(found
) return LDNS_RCODE_FORMERR
;
954 rrset
= rrset
->rrset_all_next
;
957 memset(edns
, 0, sizeof(*edns
));
958 edns
->udp_size
= 512;
961 /* check the found RRset */
962 /* most lenient check possible. ignore dname, use last opt */
963 if(found
->section
!= LDNS_SECTION_ADDITIONAL
)
964 return LDNS_RCODE_FORMERR
;
965 if(found
->rr_count
== 0)
966 return LDNS_RCODE_FORMERR
;
967 if(0) { /* strict checking of dname and RRcount */
968 if(found
->dname_len
!= 1 || !found
->dname
969 || found
->dname
[0] != 0) return LDNS_RCODE_FORMERR
;
970 if(found
->rr_count
!= 1) return LDNS_RCODE_FORMERR
;
972 log_assert(found
->rr_first
&& found
->rr_last
);
974 /* remove from packet */
975 if(found_prev
) found_prev
->rrset_all_next
= found
->rrset_all_next
;
976 else msg
->rrset_first
= found
->rrset_all_next
;
977 if(found
== msg
->rrset_last
)
978 msg
->rrset_last
= found_prev
;
983 /* take the data ! */
984 edns
->edns_present
= 1;
985 edns
->ext_rcode
= found
->rr_last
->ttl_data
[0];
986 edns
->edns_version
= found
->rr_last
->ttl_data
[1];
987 edns
->bits
= sldns_read_uint16(&found
->rr_last
->ttl_data
[2]);
988 edns
->udp_size
= ntohs(found
->rrset_class
);
989 /* ignore rdata and rrsigs */
994 parse_edns_from_pkt(sldns_buffer
* pkt
, struct edns_data
* edns
)
996 log_assert(LDNS_QDCOUNT(sldns_buffer_begin(pkt
)) == 1);
997 log_assert(LDNS_ANCOUNT(sldns_buffer_begin(pkt
)) == 0);
998 log_assert(LDNS_NSCOUNT(sldns_buffer_begin(pkt
)) == 0);
999 /* check edns section is present */
1000 if(LDNS_ARCOUNT(sldns_buffer_begin(pkt
)) > 1) {
1001 return LDNS_RCODE_FORMERR
;
1003 if(LDNS_ARCOUNT(sldns_buffer_begin(pkt
)) == 0) {
1004 memset(edns
, 0, sizeof(*edns
));
1005 edns
->udp_size
= 512;
1008 /* domain name must be the root of length 1. */
1009 if(pkt_dname_len(pkt
) != 1)
1010 return LDNS_RCODE_FORMERR
;
1011 if(sldns_buffer_remaining(pkt
) < 10) /* type, class, ttl, rdatalen */
1012 return LDNS_RCODE_FORMERR
;
1013 if(sldns_buffer_read_u16(pkt
) != LDNS_RR_TYPE_OPT
)
1014 return LDNS_RCODE_FORMERR
;
1015 edns
->edns_present
= 1;
1016 edns
->udp_size
= sldns_buffer_read_u16(pkt
); /* class is udp size */
1017 edns
->ext_rcode
= sldns_buffer_read_u8(pkt
); /* ttl used for bits */
1018 edns
->edns_version
= sldns_buffer_read_u8(pkt
);
1019 edns
->bits
= sldns_buffer_read_u16(pkt
);
1020 /* ignore rdata and rrsigs */