1933707f3Ssthen /* 2933707f3Ssthen * util/data/msgparse.c - parse wireformat DNS messages. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen /** 36933707f3Ssthen * \file 37933707f3Ssthen * Routines for message parsing a packet buffer to a descriptive structure. 38933707f3Ssthen */ 39933707f3Ssthen #include "config.h" 40e21c60efSsthen #include "util/config_file.h" 41933707f3Ssthen #include "util/data/msgparse.h" 422ee382b6Ssthen #include "util/data/msgreply.h" 43933707f3Ssthen #include "util/data/dname.h" 44933707f3Ssthen #include "util/data/packed_rrset.h" 45e21c60efSsthen #include "util/netevent.h" 46933707f3Ssthen #include "util/storage/lookup3.h" 47933707f3Ssthen #include "util/regional.h" 488b7325afSsthen #include "util/rfc_1982.h" 498b7325afSsthen #include "util/edns.h" 50d896b962Ssthen #include "util/net_help.h" 51fdfb4ba6Ssthen #include "sldns/rrdef.h" 52fdfb4ba6Ssthen #include "sldns/sbuffer.h" 53fdfb4ba6Ssthen #include "sldns/parseutil.h" 54fdfb4ba6Ssthen #include "sldns/wire2str.h" 55933707f3Ssthen 56933707f3Ssthen /** smart comparison of (compressed, valid) dnames from packet */ 57933707f3Ssthen static int 585d76a658Ssthen smart_compare(sldns_buffer* pkt, uint8_t* dnow, 59933707f3Ssthen uint8_t* dprfirst, uint8_t* dprlast) 60933707f3Ssthen { 61933707f3Ssthen if(LABEL_IS_PTR(*dnow)) { 62933707f3Ssthen /* ptr points to a previous dname */ 6306a13c09Ssthen uint8_t* p; 6406a13c09Ssthen if((size_t)PTR_OFFSET(dnow[0], dnow[1]) 6506a13c09Ssthen >= sldns_buffer_limit(pkt)) 6606a13c09Ssthen return -1; 6706a13c09Ssthen p = sldns_buffer_at(pkt, PTR_OFFSET(dnow[0], dnow[1])); 68933707f3Ssthen if( p == dprfirst || p == dprlast ) 69933707f3Ssthen return 0; 70933707f3Ssthen /* prev dname is also a ptr, both ptrs are the same. */ 71933707f3Ssthen if(LABEL_IS_PTR(*dprlast) && 72933707f3Ssthen dprlast[0] == dnow[0] && dprlast[1] == dnow[1]) 73933707f3Ssthen return 0; 74933707f3Ssthen } 75933707f3Ssthen return dname_pkt_compare(pkt, dnow, dprlast); 76933707f3Ssthen } 77933707f3Ssthen 78933707f3Ssthen /** 79933707f3Ssthen * Allocate new rrset in region, fill with data. 80933707f3Ssthen */ 81933707f3Ssthen static struct rrset_parse* 82933707f3Ssthen new_rrset(struct msg_parse* msg, uint8_t* dname, size_t dnamelen, 8377079be7Ssthen uint16_t type, uint16_t dclass, hashvalue_type hash, 845d76a658Ssthen uint32_t rrset_flags, sldns_pkt_section section, 85933707f3Ssthen struct regional* region) 86933707f3Ssthen { 87933707f3Ssthen struct rrset_parse* p = regional_alloc(region, sizeof(*p)); 88933707f3Ssthen if(!p) return NULL; 89933707f3Ssthen p->rrset_bucket_next = msg->hashtable[hash & (PARSE_TABLE_SIZE-1)]; 90933707f3Ssthen msg->hashtable[hash & (PARSE_TABLE_SIZE-1)] = p; 91933707f3Ssthen p->rrset_all_next = 0; 92933707f3Ssthen if(msg->rrset_last) 93933707f3Ssthen msg->rrset_last->rrset_all_next = p; 94933707f3Ssthen else msg->rrset_first = p; 95933707f3Ssthen msg->rrset_last = p; 96933707f3Ssthen p->hash = hash; 97933707f3Ssthen p->section = section; 98933707f3Ssthen p->dname = dname; 99933707f3Ssthen p->dname_len = dnamelen; 100933707f3Ssthen p->type = type; 101933707f3Ssthen p->rrset_class = dclass; 102933707f3Ssthen p->flags = rrset_flags; 103933707f3Ssthen p->rr_count = 0; 104933707f3Ssthen p->size = 0; 105933707f3Ssthen p->rr_first = 0; 106933707f3Ssthen p->rr_last = 0; 107933707f3Ssthen p->rrsig_count = 0; 108933707f3Ssthen p->rrsig_first = 0; 109933707f3Ssthen p->rrsig_last = 0; 110933707f3Ssthen return p; 111933707f3Ssthen } 112933707f3Ssthen 113933707f3Ssthen /** See if next rrset is nsec at zone apex */ 114933707f3Ssthen static int 1155d76a658Ssthen nsec_at_apex(sldns_buffer* pkt) 116933707f3Ssthen { 117933707f3Ssthen /* we are at ttl position in packet. */ 1185d76a658Ssthen size_t pos = sldns_buffer_position(pkt); 119933707f3Ssthen uint16_t rdatalen; 1205d76a658Ssthen if(sldns_buffer_remaining(pkt) < 7) /* ttl+len+root */ 121933707f3Ssthen return 0; /* eek! */ 1225d76a658Ssthen sldns_buffer_skip(pkt, 4); /* ttl */; 1235d76a658Ssthen rdatalen = sldns_buffer_read_u16(pkt); 1245d76a658Ssthen if(sldns_buffer_remaining(pkt) < rdatalen) { 1255d76a658Ssthen sldns_buffer_set_position(pkt, pos); 126933707f3Ssthen return 0; /* parse error happens later */ 127933707f3Ssthen } 128933707f3Ssthen /* must validate the nsec next domain name format */ 129933707f3Ssthen if(pkt_dname_len(pkt) == 0) { 1305d76a658Ssthen sldns_buffer_set_position(pkt, pos); 131933707f3Ssthen return 0; /* parse error */ 132933707f3Ssthen } 133933707f3Ssthen 134933707f3Ssthen /* see if SOA bit is set. */ 1355d76a658Ssthen if(sldns_buffer_position(pkt) < pos+4+rdatalen) { 136933707f3Ssthen /* nsec type bitmap contains items */ 137933707f3Ssthen uint8_t win, blen, bits; 138933707f3Ssthen /* need: windownum, bitmap len, firstbyte */ 1395d76a658Ssthen if(sldns_buffer_position(pkt)+3 > pos+4+rdatalen) { 1405d76a658Ssthen sldns_buffer_set_position(pkt, pos); 141933707f3Ssthen return 0; /* malformed nsec */ 142933707f3Ssthen } 1435d76a658Ssthen win = sldns_buffer_read_u8(pkt); 1445d76a658Ssthen blen = sldns_buffer_read_u8(pkt); 1455d76a658Ssthen bits = sldns_buffer_read_u8(pkt); 146933707f3Ssthen /* 0window always first window. bitlen >=1 or parse 147933707f3Ssthen error really. bit 0x2 is SOA. */ 148933707f3Ssthen if(win == 0 && blen >= 1 && (bits & 0x02)) { 1495d76a658Ssthen sldns_buffer_set_position(pkt, pos); 150933707f3Ssthen return 1; 151933707f3Ssthen } 152933707f3Ssthen } 153933707f3Ssthen 1545d76a658Ssthen sldns_buffer_set_position(pkt, pos); 155933707f3Ssthen return 0; 156933707f3Ssthen } 157933707f3Ssthen 158933707f3Ssthen /** Calculate rrset flags */ 159933707f3Ssthen static uint32_t 1605d76a658Ssthen pkt_rrset_flags(sldns_buffer* pkt, uint16_t type, sldns_pkt_section sec) 161933707f3Ssthen { 162933707f3Ssthen uint32_t f = 0; 163933707f3Ssthen if(type == LDNS_RR_TYPE_NSEC && nsec_at_apex(pkt)) { 164933707f3Ssthen f |= PACKED_RRSET_NSEC_AT_APEX; 165933707f3Ssthen } else if(type == LDNS_RR_TYPE_SOA && sec == LDNS_SECTION_AUTHORITY) { 166933707f3Ssthen f |= PACKED_RRSET_SOA_NEG; 167933707f3Ssthen } 168933707f3Ssthen return f; 169933707f3Ssthen } 170933707f3Ssthen 17177079be7Ssthen hashvalue_type 1725d76a658Ssthen pkt_hash_rrset(sldns_buffer* pkt, uint8_t* dname, uint16_t type, 173933707f3Ssthen uint16_t dclass, uint32_t rrset_flags) 174933707f3Ssthen { 175933707f3Ssthen /* note this MUST be identical to rrset_key_hash in packed_rrset.c */ 176933707f3Ssthen /* this routine handles compressed names */ 17777079be7Ssthen hashvalue_type h = 0xab; 178933707f3Ssthen h = dname_pkt_hash(pkt, dname, h); 179933707f3Ssthen h = hashlittle(&type, sizeof(type), h); /* host order */ 180933707f3Ssthen h = hashlittle(&dclass, sizeof(dclass), h); /* netw order */ 181933707f3Ssthen h = hashlittle(&rrset_flags, sizeof(uint32_t), h); 182933707f3Ssthen return h; 183933707f3Ssthen } 184933707f3Ssthen 185933707f3Ssthen /** create partial dname hash for rrset hash */ 18677079be7Ssthen static hashvalue_type 1875d76a658Ssthen pkt_hash_rrset_first(sldns_buffer* pkt, uint8_t* dname) 188933707f3Ssthen { 189933707f3Ssthen /* works together with pkt_hash_rrset_rest */ 190933707f3Ssthen /* note this MUST be identical to rrset_key_hash in packed_rrset.c */ 191933707f3Ssthen /* this routine handles compressed names */ 19277079be7Ssthen hashvalue_type h = 0xab; 193933707f3Ssthen h = dname_pkt_hash(pkt, dname, h); 194933707f3Ssthen return h; 195933707f3Ssthen } 196933707f3Ssthen 197933707f3Ssthen /** create a rrset hash from a partial dname hash */ 19877079be7Ssthen static hashvalue_type 19977079be7Ssthen pkt_hash_rrset_rest(hashvalue_type dname_h, uint16_t type, uint16_t dclass, 200933707f3Ssthen uint32_t rrset_flags) 201933707f3Ssthen { 202933707f3Ssthen /* works together with pkt_hash_rrset_first */ 203933707f3Ssthen /* note this MUST be identical to rrset_key_hash in packed_rrset.c */ 20477079be7Ssthen hashvalue_type h; 205933707f3Ssthen h = hashlittle(&type, sizeof(type), dname_h); /* host order */ 206933707f3Ssthen h = hashlittle(&dclass, sizeof(dclass), h); /* netw order */ 207933707f3Ssthen h = hashlittle(&rrset_flags, sizeof(uint32_t), h); 208933707f3Ssthen return h; 209933707f3Ssthen } 210933707f3Ssthen 211933707f3Ssthen /** compare rrset_parse with data */ 212933707f3Ssthen static int 21377079be7Ssthen rrset_parse_equals(struct rrset_parse* p, sldns_buffer* pkt, hashvalue_type h, 214933707f3Ssthen uint32_t rrset_flags, uint8_t* dname, size_t dnamelen, 215933707f3Ssthen uint16_t type, uint16_t dclass) 216933707f3Ssthen { 217933707f3Ssthen if(p->hash == h && p->dname_len == dnamelen && p->type == type && 218933707f3Ssthen p->rrset_class == dclass && p->flags == rrset_flags && 219933707f3Ssthen dname_pkt_compare(pkt, dname, p->dname) == 0) 220933707f3Ssthen return 1; 221933707f3Ssthen return 0; 222933707f3Ssthen } 223933707f3Ssthen 224933707f3Ssthen 225933707f3Ssthen struct rrset_parse* 2265d76a658Ssthen msgparse_hashtable_lookup(struct msg_parse* msg, sldns_buffer* pkt, 22777079be7Ssthen hashvalue_type h, uint32_t rrset_flags, uint8_t* dname, 22877079be7Ssthen size_t dnamelen, uint16_t type, uint16_t dclass) 229933707f3Ssthen { 230933707f3Ssthen struct rrset_parse* p = msg->hashtable[h & (PARSE_TABLE_SIZE-1)]; 231933707f3Ssthen while(p) { 232933707f3Ssthen if(rrset_parse_equals(p, pkt, h, rrset_flags, dname, dnamelen, 233933707f3Ssthen type, dclass)) 234933707f3Ssthen return p; 235933707f3Ssthen p = p->rrset_bucket_next; 236933707f3Ssthen } 237933707f3Ssthen return NULL; 238933707f3Ssthen } 239933707f3Ssthen 240933707f3Ssthen /** return type networkformat that rrsig in packet covers */ 241933707f3Ssthen static int 2425d76a658Ssthen pkt_rrsig_covered(sldns_buffer* pkt, uint8_t* here, uint16_t* type) 243933707f3Ssthen { 2445d76a658Ssthen size_t pos = sldns_buffer_position(pkt); 2455d76a658Ssthen sldns_buffer_set_position(pkt, (size_t)(here-sldns_buffer_begin(pkt))); 246933707f3Ssthen /* ttl + len + size of small rrsig(rootlabel, no signature) */ 2475d76a658Ssthen if(sldns_buffer_remaining(pkt) < 4+2+19) 248933707f3Ssthen return 0; 2495d76a658Ssthen sldns_buffer_skip(pkt, 4); /* ttl */ 2505d76a658Ssthen if(sldns_buffer_read_u16(pkt) < 19) /* too short */ { 2515d76a658Ssthen sldns_buffer_set_position(pkt, pos); 252933707f3Ssthen return 0; 253933707f3Ssthen } 2545d76a658Ssthen *type = sldns_buffer_read_u16(pkt); 2555d76a658Ssthen sldns_buffer_set_position(pkt, pos); 256933707f3Ssthen return 1; 257933707f3Ssthen } 258933707f3Ssthen 259933707f3Ssthen /** true if covered type equals prevtype */ 260933707f3Ssthen static int 2615d76a658Ssthen pkt_rrsig_covered_equals(sldns_buffer* pkt, uint8_t* here, uint16_t type) 262933707f3Ssthen { 263933707f3Ssthen uint16_t t; 264933707f3Ssthen if(pkt_rrsig_covered(pkt, here, &t) && t == type) 265933707f3Ssthen return 1; 266933707f3Ssthen return 0; 267933707f3Ssthen } 268933707f3Ssthen 269933707f3Ssthen void 270933707f3Ssthen msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset) 271933707f3Ssthen { 272933707f3Ssthen struct rrset_parse** p; 273933707f3Ssthen p = &msg->hashtable[ rrset->hash & (PARSE_TABLE_SIZE-1) ]; 274933707f3Ssthen while(*p) { 275933707f3Ssthen if(*p == rrset) { 276933707f3Ssthen *p = rrset->rrset_bucket_next; 277933707f3Ssthen return; 278933707f3Ssthen } 279933707f3Ssthen p = &( (*p)->rrset_bucket_next ); 280933707f3Ssthen } 281933707f3Ssthen } 282933707f3Ssthen 283933707f3Ssthen /** change section of rrset from previous to current section */ 284933707f3Ssthen static void 285933707f3Ssthen change_section(struct msg_parse* msg, struct rrset_parse* rrset, 2865d76a658Ssthen sldns_pkt_section section) 287933707f3Ssthen { 288933707f3Ssthen struct rrset_parse *p, *prev; 289933707f3Ssthen /* remove from list */ 290933707f3Ssthen if(section == rrset->section) 291933707f3Ssthen return; 292933707f3Ssthen p = msg->rrset_first; 293933707f3Ssthen prev = 0; 294933707f3Ssthen while(p) { 295933707f3Ssthen if(p == rrset) { 296933707f3Ssthen if(prev) prev->rrset_all_next = p->rrset_all_next; 297933707f3Ssthen else msg->rrset_first = p->rrset_all_next; 298933707f3Ssthen if(msg->rrset_last == rrset) 299933707f3Ssthen msg->rrset_last = prev; 300933707f3Ssthen break; 301933707f3Ssthen } 302933707f3Ssthen prev = p; 303933707f3Ssthen p = p->rrset_all_next; 304933707f3Ssthen } 305933707f3Ssthen /* remove from count */ 306933707f3Ssthen switch(rrset->section) { 307933707f3Ssthen case LDNS_SECTION_ANSWER: msg->an_rrsets--; break; 308933707f3Ssthen case LDNS_SECTION_AUTHORITY: msg->ns_rrsets--; break; 309933707f3Ssthen case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets--; break; 310933707f3Ssthen default: log_assert(0); 311933707f3Ssthen } 312933707f3Ssthen /* insert at end of list */ 313933707f3Ssthen rrset->rrset_all_next = 0; 314933707f3Ssthen if(msg->rrset_last) 315933707f3Ssthen msg->rrset_last->rrset_all_next = rrset; 316933707f3Ssthen else msg->rrset_first = rrset; 317933707f3Ssthen msg->rrset_last = rrset; 318933707f3Ssthen /* up count of new section */ 319933707f3Ssthen switch(section) { 320933707f3Ssthen case LDNS_SECTION_AUTHORITY: msg->ns_rrsets++; break; 321933707f3Ssthen case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets++; break; 322933707f3Ssthen default: log_assert(0); 323933707f3Ssthen } 324933707f3Ssthen rrset->section = section; 325933707f3Ssthen } 326933707f3Ssthen 327933707f3Ssthen /** see if rrset of type RRSIG contains sig over given type */ 328933707f3Ssthen static int 3295d76a658Ssthen rrset_has_sigover(sldns_buffer* pkt, struct rrset_parse* rrset, uint16_t type, 330933707f3Ssthen int* hasother) 331933707f3Ssthen { 332933707f3Ssthen int res = 0; 333933707f3Ssthen struct rr_parse* rr = rrset->rr_first; 334933707f3Ssthen log_assert( rrset->type == LDNS_RR_TYPE_RRSIG ); 335933707f3Ssthen while(rr) { 336933707f3Ssthen if(pkt_rrsig_covered_equals(pkt, rr->ttl_data, type)) 337933707f3Ssthen res = 1; 338933707f3Ssthen else *hasother = 1; 339933707f3Ssthen rr = rr->next; 340933707f3Ssthen } 341933707f3Ssthen return res; 342933707f3Ssthen } 343933707f3Ssthen 344933707f3Ssthen /** move rrsigs from sigset to dataset */ 345933707f3Ssthen static int 3465d76a658Ssthen moveover_rrsigs(sldns_buffer* pkt, struct regional* region, 347933707f3Ssthen struct rrset_parse* sigset, struct rrset_parse* dataset, int duplicate) 348933707f3Ssthen { 349933707f3Ssthen struct rr_parse* sig = sigset->rr_first; 350933707f3Ssthen struct rr_parse* prev = NULL; 351933707f3Ssthen struct rr_parse* insert; 352933707f3Ssthen struct rr_parse* nextsig; 353933707f3Ssthen while(sig) { 354933707f3Ssthen nextsig = sig->next; 355933707f3Ssthen if(pkt_rrsig_covered_equals(pkt, sig->ttl_data, 356933707f3Ssthen dataset->type)) { 357933707f3Ssthen if(duplicate) { 358933707f3Ssthen /* new */ 359933707f3Ssthen insert = (struct rr_parse*)regional_alloc( 360933707f3Ssthen region, sizeof(struct rr_parse)); 361933707f3Ssthen if(!insert) return 0; 362933707f3Ssthen insert->outside_packet = 0; 363933707f3Ssthen insert->ttl_data = sig->ttl_data; 364933707f3Ssthen insert->size = sig->size; 365933707f3Ssthen /* prev not used */ 366933707f3Ssthen } else { 367933707f3Ssthen /* remove from sigset */ 368933707f3Ssthen if(prev) prev->next = sig->next; 369933707f3Ssthen else sigset->rr_first = sig->next; 370933707f3Ssthen if(sigset->rr_last == sig) 371933707f3Ssthen sigset->rr_last = prev; 372933707f3Ssthen sigset->rr_count--; 373933707f3Ssthen sigset->size -= sig->size; 374933707f3Ssthen insert = sig; 375933707f3Ssthen /* prev not changed */ 376933707f3Ssthen } 377933707f3Ssthen /* add to dataset */ 378933707f3Ssthen dataset->rrsig_count++; 379933707f3Ssthen insert->next = 0; 380933707f3Ssthen if(dataset->rrsig_last) 381933707f3Ssthen dataset->rrsig_last->next = insert; 382933707f3Ssthen else dataset->rrsig_first = insert; 383933707f3Ssthen dataset->rrsig_last = insert; 384933707f3Ssthen dataset->size += insert->size; 385933707f3Ssthen } else { 386933707f3Ssthen prev = sig; 387933707f3Ssthen } 388933707f3Ssthen sig = nextsig; 389933707f3Ssthen } 390933707f3Ssthen return 1; 391933707f3Ssthen } 392933707f3Ssthen 393933707f3Ssthen /** change an rrsig rrset for use as data rrset */ 394933707f3Ssthen static struct rrset_parse* 395933707f3Ssthen change_rrsig_rrset(struct rrset_parse* sigset, struct msg_parse* msg, 3965d76a658Ssthen sldns_buffer* pkt, uint16_t datatype, uint32_t rrset_flags, 3975d76a658Ssthen int hasother, sldns_pkt_section section, struct regional* region) 398933707f3Ssthen { 399933707f3Ssthen struct rrset_parse* dataset = sigset; 40077079be7Ssthen hashvalue_type hash = pkt_hash_rrset(pkt, sigset->dname, datatype, 401933707f3Ssthen sigset->rrset_class, rrset_flags); 402933707f3Ssthen log_assert( sigset->type == LDNS_RR_TYPE_RRSIG ); 403933707f3Ssthen log_assert( datatype != LDNS_RR_TYPE_RRSIG ); 404933707f3Ssthen if(hasother) { 405933707f3Ssthen /* need to make new rrset to hold data type */ 406933707f3Ssthen dataset = new_rrset(msg, sigset->dname, sigset->dname_len, 407933707f3Ssthen datatype, sigset->rrset_class, hash, rrset_flags, 408933707f3Ssthen section, region); 409933707f3Ssthen if(!dataset) 410933707f3Ssthen return NULL; 411933707f3Ssthen switch(section) { 412933707f3Ssthen case LDNS_SECTION_ANSWER: msg->an_rrsets++; break; 413933707f3Ssthen case LDNS_SECTION_AUTHORITY: msg->ns_rrsets++; break; 414933707f3Ssthen case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets++; break; 415933707f3Ssthen default: log_assert(0); 416933707f3Ssthen } 417933707f3Ssthen if(!moveover_rrsigs(pkt, region, sigset, dataset, 418933707f3Ssthen msg->qtype == LDNS_RR_TYPE_RRSIG || 419933707f3Ssthen (msg->qtype == LDNS_RR_TYPE_ANY && 420933707f3Ssthen section != LDNS_SECTION_ANSWER) )) 421933707f3Ssthen return NULL; 422933707f3Ssthen return dataset; 423933707f3Ssthen } 424933707f3Ssthen /* changeover the type of the rrset to data set */ 425933707f3Ssthen msgparse_bucket_remove(msg, dataset); 426933707f3Ssthen /* insert into new hash bucket */ 427933707f3Ssthen dataset->rrset_bucket_next = msg->hashtable[hash&(PARSE_TABLE_SIZE-1)]; 428933707f3Ssthen msg->hashtable[hash&(PARSE_TABLE_SIZE-1)] = dataset; 429933707f3Ssthen dataset->hash = hash; 430933707f3Ssthen /* use section of data item for result */ 431933707f3Ssthen change_section(msg, dataset, section); 432933707f3Ssthen dataset->type = datatype; 433933707f3Ssthen dataset->flags = rrset_flags; 434933707f3Ssthen dataset->rrsig_count += dataset->rr_count; 435933707f3Ssthen dataset->rr_count = 0; 436933707f3Ssthen /* move sigs to end of siglist */ 437933707f3Ssthen if(dataset->rrsig_last) 438933707f3Ssthen dataset->rrsig_last->next = dataset->rr_first; 439933707f3Ssthen else dataset->rrsig_first = dataset->rr_first; 440933707f3Ssthen dataset->rrsig_last = dataset->rr_last; 441933707f3Ssthen dataset->rr_first = 0; 442933707f3Ssthen dataset->rr_last = 0; 443933707f3Ssthen return dataset; 444933707f3Ssthen } 445933707f3Ssthen 446933707f3Ssthen /** Find rrset. If equal to previous it is fast. hash if not so. 447933707f3Ssthen * @param msg: the message with hash table. 448933707f3Ssthen * @param pkt: the packet in wireformat (needed for compression ptrs). 449933707f3Ssthen * @param dname: pointer to start of dname (compressed) in packet. 450933707f3Ssthen * @param dnamelen: uncompressed wirefmt length of dname. 451933707f3Ssthen * @param type: type of current rr. 452933707f3Ssthen * @param dclass: class of current rr. 453933707f3Ssthen * @param hash: hash value is returned if the rrset could not be found. 454933707f3Ssthen * @param rrset_flags: is returned if the rrset could not be found. 455933707f3Ssthen * @param prev_dname_first: dname of last seen RR. First seen dname. 456933707f3Ssthen * @param prev_dname_last: dname of last seen RR. Last seen dname. 457933707f3Ssthen * @param prev_dnamelen: dname len of last seen RR. 458933707f3Ssthen * @param prev_type: type of last seen RR. 459933707f3Ssthen * @param prev_dclass: class of last seen RR. 460933707f3Ssthen * @param rrset_prev: last seen RRset. 461933707f3Ssthen * @param section: the current section in the packet. 462933707f3Ssthen * @param region: used to allocate temporary parsing data. 463933707f3Ssthen * @return 0 on out of memory. 464933707f3Ssthen */ 465933707f3Ssthen static int 4665d76a658Ssthen find_rrset(struct msg_parse* msg, sldns_buffer* pkt, uint8_t* dname, 46777079be7Ssthen size_t dnamelen, uint16_t type, uint16_t dclass, hashvalue_type* hash, 468933707f3Ssthen uint32_t* rrset_flags, 469933707f3Ssthen uint8_t** prev_dname_first, uint8_t** prev_dname_last, 470933707f3Ssthen size_t* prev_dnamelen, uint16_t* prev_type, 471933707f3Ssthen uint16_t* prev_dclass, struct rrset_parse** rrset_prev, 4725d76a658Ssthen sldns_pkt_section section, struct regional* region) 473933707f3Ssthen { 47477079be7Ssthen hashvalue_type dname_h = pkt_hash_rrset_first(pkt, dname); 475933707f3Ssthen uint16_t covtype; 476933707f3Ssthen if(*rrset_prev) { 477933707f3Ssthen /* check if equal to previous item */ 478933707f3Ssthen if(type == *prev_type && dclass == *prev_dclass && 479933707f3Ssthen dnamelen == *prev_dnamelen && 480933707f3Ssthen smart_compare(pkt, dname, *prev_dname_first, 481933707f3Ssthen *prev_dname_last) == 0 && 482933707f3Ssthen type != LDNS_RR_TYPE_RRSIG) { 483933707f3Ssthen /* same as previous */ 484933707f3Ssthen *prev_dname_last = dname; 485933707f3Ssthen return 1; 486933707f3Ssthen } 487933707f3Ssthen /* check if rrsig over previous item */ 488933707f3Ssthen if(type == LDNS_RR_TYPE_RRSIG && dclass == *prev_dclass && 4895d76a658Ssthen pkt_rrsig_covered_equals(pkt, sldns_buffer_current(pkt), 490933707f3Ssthen *prev_type) && 491933707f3Ssthen smart_compare(pkt, dname, *prev_dname_first, 492933707f3Ssthen *prev_dname_last) == 0) { 493933707f3Ssthen /* covers previous */ 494933707f3Ssthen *prev_dname_last = dname; 495933707f3Ssthen return 1; 496933707f3Ssthen } 497933707f3Ssthen } 498933707f3Ssthen /* find by hashing and lookup in hashtable */ 499933707f3Ssthen *rrset_flags = pkt_rrset_flags(pkt, type, section); 500933707f3Ssthen 501933707f3Ssthen /* if rrsig - try to lookup matching data set first */ 502933707f3Ssthen if(type == LDNS_RR_TYPE_RRSIG && pkt_rrsig_covered(pkt, 5035d76a658Ssthen sldns_buffer_current(pkt), &covtype)) { 504933707f3Ssthen *hash = pkt_hash_rrset_rest(dname_h, covtype, dclass, 505933707f3Ssthen *rrset_flags); 506933707f3Ssthen *rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, 507933707f3Ssthen *rrset_flags, dname, dnamelen, covtype, dclass); 508933707f3Ssthen if(!*rrset_prev && covtype == LDNS_RR_TYPE_NSEC) { 509933707f3Ssthen /* if NSEC try with NSEC apex bit twiddled */ 510933707f3Ssthen *rrset_flags ^= PACKED_RRSET_NSEC_AT_APEX; 511933707f3Ssthen *hash = pkt_hash_rrset_rest(dname_h, covtype, dclass, 512933707f3Ssthen *rrset_flags); 513933707f3Ssthen *rrset_prev = msgparse_hashtable_lookup(msg, pkt, 514933707f3Ssthen *hash, *rrset_flags, dname, dnamelen, covtype, 515933707f3Ssthen dclass); 516933707f3Ssthen if(!*rrset_prev) /* untwiddle if not found */ 517933707f3Ssthen *rrset_flags ^= PACKED_RRSET_NSEC_AT_APEX; 518933707f3Ssthen } 519933707f3Ssthen if(!*rrset_prev && covtype == LDNS_RR_TYPE_SOA) { 520933707f3Ssthen /* if SOA try with SOA neg flag twiddled */ 521933707f3Ssthen *rrset_flags ^= PACKED_RRSET_SOA_NEG; 522933707f3Ssthen *hash = pkt_hash_rrset_rest(dname_h, covtype, dclass, 523933707f3Ssthen *rrset_flags); 524933707f3Ssthen *rrset_prev = msgparse_hashtable_lookup(msg, pkt, 525933707f3Ssthen *hash, *rrset_flags, dname, dnamelen, covtype, 526933707f3Ssthen dclass); 527933707f3Ssthen if(!*rrset_prev) /* untwiddle if not found */ 528933707f3Ssthen *rrset_flags ^= PACKED_RRSET_SOA_NEG; 529933707f3Ssthen } 530933707f3Ssthen if(*rrset_prev) { 531933707f3Ssthen *prev_dname_first = (*rrset_prev)->dname; 532933707f3Ssthen *prev_dname_last = dname; 533933707f3Ssthen *prev_dnamelen = dnamelen; 534933707f3Ssthen *prev_type = covtype; 535933707f3Ssthen *prev_dclass = dclass; 536933707f3Ssthen return 1; 537933707f3Ssthen } 538933707f3Ssthen } 539933707f3Ssthen if(type != LDNS_RR_TYPE_RRSIG) { 540933707f3Ssthen int hasother = 0; 541933707f3Ssthen /* find matching rrsig */ 542933707f3Ssthen *hash = pkt_hash_rrset_rest(dname_h, LDNS_RR_TYPE_RRSIG, 543933707f3Ssthen dclass, 0); 544933707f3Ssthen *rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, 545933707f3Ssthen 0, dname, dnamelen, LDNS_RR_TYPE_RRSIG, 546933707f3Ssthen dclass); 547933707f3Ssthen if(*rrset_prev && rrset_has_sigover(pkt, *rrset_prev, type, 548933707f3Ssthen &hasother)) { 549933707f3Ssthen /* yes! */ 550933707f3Ssthen *prev_dname_first = (*rrset_prev)->dname; 551933707f3Ssthen *prev_dname_last = dname; 552933707f3Ssthen *prev_dnamelen = dnamelen; 553933707f3Ssthen *prev_type = type; 554933707f3Ssthen *prev_dclass = dclass; 555933707f3Ssthen *rrset_prev = change_rrsig_rrset(*rrset_prev, msg, 556933707f3Ssthen pkt, type, *rrset_flags, hasother, section, 557933707f3Ssthen region); 558933707f3Ssthen if(!*rrset_prev) return 0; 559933707f3Ssthen return 1; 560933707f3Ssthen } 561933707f3Ssthen } 562933707f3Ssthen 563933707f3Ssthen *hash = pkt_hash_rrset_rest(dname_h, type, dclass, *rrset_flags); 564933707f3Ssthen *rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, *rrset_flags, 565933707f3Ssthen dname, dnamelen, type, dclass); 566933707f3Ssthen if(*rrset_prev) 567933707f3Ssthen *prev_dname_first = (*rrset_prev)->dname; 568933707f3Ssthen else *prev_dname_first = dname; 569933707f3Ssthen *prev_dname_last = dname; 570933707f3Ssthen *prev_dnamelen = dnamelen; 571933707f3Ssthen *prev_type = type; 572933707f3Ssthen *prev_dclass = dclass; 573933707f3Ssthen return 1; 574933707f3Ssthen } 575933707f3Ssthen 576933707f3Ssthen /** 577933707f3Ssthen * Parse query section. 578933707f3Ssthen * @param pkt: packet, position at call must be at start of query section. 579933707f3Ssthen * at end position is after query section. 580933707f3Ssthen * @param msg: store results here. 581933707f3Ssthen * @return: 0 if OK, or rcode on error. 582933707f3Ssthen */ 583933707f3Ssthen static int 5845d76a658Ssthen parse_query_section(sldns_buffer* pkt, struct msg_parse* msg) 585933707f3Ssthen { 586933707f3Ssthen if(msg->qdcount == 0) 587933707f3Ssthen return 0; 588933707f3Ssthen if(msg->qdcount > 1) 589933707f3Ssthen return LDNS_RCODE_FORMERR; 590933707f3Ssthen log_assert(msg->qdcount == 1); 5915d76a658Ssthen if(sldns_buffer_remaining(pkt) <= 0) 592933707f3Ssthen return LDNS_RCODE_FORMERR; 5935d76a658Ssthen msg->qname = sldns_buffer_current(pkt); 594933707f3Ssthen if((msg->qname_len = pkt_dname_len(pkt)) == 0) 595933707f3Ssthen return LDNS_RCODE_FORMERR; 5965d76a658Ssthen if(sldns_buffer_remaining(pkt) < sizeof(uint16_t)*2) 597933707f3Ssthen return LDNS_RCODE_FORMERR; 5985d76a658Ssthen msg->qtype = sldns_buffer_read_u16(pkt); 5995d76a658Ssthen msg->qclass = sldns_buffer_read_u16(pkt); 600933707f3Ssthen return 0; 601933707f3Ssthen } 602933707f3Ssthen 603933707f3Ssthen size_t 6045d76a658Ssthen get_rdf_size(sldns_rdf_type rdf) 605933707f3Ssthen { 606933707f3Ssthen switch(rdf) { 607933707f3Ssthen case LDNS_RDF_TYPE_CLASS: 608933707f3Ssthen case LDNS_RDF_TYPE_ALG: 609933707f3Ssthen case LDNS_RDF_TYPE_INT8: 610933707f3Ssthen return 1; 611933707f3Ssthen break; 612933707f3Ssthen case LDNS_RDF_TYPE_INT16: 613933707f3Ssthen case LDNS_RDF_TYPE_TYPE: 614933707f3Ssthen case LDNS_RDF_TYPE_CERT_ALG: 615933707f3Ssthen return 2; 616933707f3Ssthen break; 617933707f3Ssthen case LDNS_RDF_TYPE_INT32: 618933707f3Ssthen case LDNS_RDF_TYPE_TIME: 619933707f3Ssthen case LDNS_RDF_TYPE_A: 620933707f3Ssthen case LDNS_RDF_TYPE_PERIOD: 621933707f3Ssthen return 4; 622933707f3Ssthen break; 623933707f3Ssthen case LDNS_RDF_TYPE_TSIGTIME: 624933707f3Ssthen return 6; 625933707f3Ssthen break; 626933707f3Ssthen case LDNS_RDF_TYPE_AAAA: 627933707f3Ssthen return 16; 628933707f3Ssthen break; 629933707f3Ssthen default: 6305d76a658Ssthen log_assert(0); /* add type above */ 631933707f3Ssthen /* only types that appear before a domain * 632933707f3Ssthen * name are needed. rest is simply copied. */ 633933707f3Ssthen } 634933707f3Ssthen return 0; 635933707f3Ssthen } 636933707f3Ssthen 637933707f3Ssthen /** calculate the size of one rr */ 638933707f3Ssthen static int 6395d76a658Ssthen calc_size(sldns_buffer* pkt, uint16_t type, struct rr_parse* rr) 640933707f3Ssthen { 6415d76a658Ssthen const sldns_rr_descriptor* desc; 642933707f3Ssthen uint16_t pkt_len; /* length of rr inside the packet */ 643933707f3Ssthen rr->size = sizeof(uint16_t); /* the rdatalen */ 6445d76a658Ssthen sldns_buffer_skip(pkt, 4); /* skip ttl */ 6455d76a658Ssthen pkt_len = sldns_buffer_read_u16(pkt); 6465d76a658Ssthen if(sldns_buffer_remaining(pkt) < pkt_len) 647933707f3Ssthen return 0; 6485d76a658Ssthen desc = sldns_rr_descript(type); 649933707f3Ssthen if(pkt_len > 0 && desc && desc->_dname_count > 0) { 650933707f3Ssthen int count = (int)desc->_dname_count; 651933707f3Ssthen int rdf = 0; 652933707f3Ssthen size_t len; 653933707f3Ssthen size_t oldpos; 654933707f3Ssthen /* skip first part. */ 655933707f3Ssthen while(pkt_len > 0 && count) { 656933707f3Ssthen switch(desc->_wireformat[rdf]) { 657933707f3Ssthen case LDNS_RDF_TYPE_DNAME: 658933707f3Ssthen /* decompress every domain name */ 6595d76a658Ssthen oldpos = sldns_buffer_position(pkt); 660933707f3Ssthen if((len = pkt_dname_len(pkt)) == 0) 661933707f3Ssthen return 0; /* malformed dname */ 6625d76a658Ssthen if(sldns_buffer_position(pkt)-oldpos > pkt_len) 663933707f3Ssthen return 0; /* dname exceeds rdata */ 6645d76a658Ssthen pkt_len -= sldns_buffer_position(pkt)-oldpos; 665933707f3Ssthen rr->size += len; 666933707f3Ssthen count--; 667933707f3Ssthen len = 0; 668933707f3Ssthen break; 669933707f3Ssthen case LDNS_RDF_TYPE_STR: 6703dcb24b8Ssthen if(pkt_len < 1) { 6713dcb24b8Ssthen /* NOTREACHED, due to 'while(>0)' */ 672933707f3Ssthen return 0; /* len byte exceeds rdata */ 6733dcb24b8Ssthen } 6745d76a658Ssthen len = sldns_buffer_current(pkt)[0] + 1; 675933707f3Ssthen break; 676933707f3Ssthen default: 677933707f3Ssthen len = get_rdf_size(desc->_wireformat[rdf]); 678933707f3Ssthen } 679933707f3Ssthen if(len) { 680933707f3Ssthen if(pkt_len < len) 681933707f3Ssthen return 0; /* exceeds rdata */ 682933707f3Ssthen pkt_len -= len; 6835d76a658Ssthen sldns_buffer_skip(pkt, (ssize_t)len); 684933707f3Ssthen rr->size += len; 685933707f3Ssthen } 686933707f3Ssthen rdf++; 687933707f3Ssthen } 688933707f3Ssthen } 689933707f3Ssthen /* remaining rdata */ 690933707f3Ssthen rr->size += pkt_len; 6915d76a658Ssthen sldns_buffer_skip(pkt, (ssize_t)pkt_len); 692933707f3Ssthen return 1; 693933707f3Ssthen } 694933707f3Ssthen 695933707f3Ssthen /** skip rr ttl and rdata */ 696933707f3Ssthen static int 6975d76a658Ssthen skip_ttl_rdata(sldns_buffer* pkt) 698933707f3Ssthen { 699933707f3Ssthen uint16_t rdatalen; 7005d76a658Ssthen if(sldns_buffer_remaining(pkt) < 6) /* ttl + rdatalen */ 701933707f3Ssthen return 0; 7025d76a658Ssthen sldns_buffer_skip(pkt, 4); /* ttl */ 7035d76a658Ssthen rdatalen = sldns_buffer_read_u16(pkt); 7045d76a658Ssthen if(sldns_buffer_remaining(pkt) < rdatalen) 705933707f3Ssthen return 0; 7065d76a658Ssthen sldns_buffer_skip(pkt, (ssize_t)rdatalen); 707933707f3Ssthen return 1; 708933707f3Ssthen } 709933707f3Ssthen 710933707f3Ssthen /** see if RRSIG is a duplicate of another */ 711933707f3Ssthen static int 7125d76a658Ssthen sig_is_double(sldns_buffer* pkt, struct rrset_parse* rrset, uint8_t* ttldata) 713933707f3Ssthen { 714933707f3Ssthen uint16_t rlen, siglen; 7155d76a658Ssthen size_t pos = sldns_buffer_position(pkt); 716933707f3Ssthen struct rr_parse* sig; 7175d76a658Ssthen if(sldns_buffer_remaining(pkt) < 6) 718933707f3Ssthen return 0; 7195d76a658Ssthen sldns_buffer_skip(pkt, 4); /* ttl */ 7205d76a658Ssthen rlen = sldns_buffer_read_u16(pkt); 7215d76a658Ssthen if(sldns_buffer_remaining(pkt) < rlen) { 7225d76a658Ssthen sldns_buffer_set_position(pkt, pos); 723933707f3Ssthen return 0; 724933707f3Ssthen } 7255d76a658Ssthen sldns_buffer_set_position(pkt, pos); 726933707f3Ssthen 727933707f3Ssthen sig = rrset->rrsig_first; 728933707f3Ssthen while(sig) { 729933707f3Ssthen /* check if rdatalen is same */ 730933707f3Ssthen memmove(&siglen, sig->ttl_data+4, sizeof(siglen)); 731933707f3Ssthen siglen = ntohs(siglen); 732933707f3Ssthen /* checks if data in packet is exactly the same, this means 733933707f3Ssthen * also dname in rdata is the same, but rrsig is not allowed 734933707f3Ssthen * to have compressed dnames anyway. If it is compressed anyway 735933707f3Ssthen * it will lead to duplicate rrs for qtype=RRSIG. (or ANY). 736933707f3Ssthen * 737933707f3Ssthen * Cannot use sig->size because size of the other one is not 738933707f3Ssthen * calculated yet. 739933707f3Ssthen */ 740933707f3Ssthen if(siglen == rlen) { 741933707f3Ssthen if(siglen>0 && memcmp(sig->ttl_data+6, ttldata+6, 742933707f3Ssthen siglen) == 0) { 743933707f3Ssthen /* same! */ 744933707f3Ssthen return 1; 745933707f3Ssthen } 746933707f3Ssthen } 747933707f3Ssthen sig = sig->next; 748933707f3Ssthen } 749933707f3Ssthen return 0; 750933707f3Ssthen } 751933707f3Ssthen 752933707f3Ssthen /** Add rr (from packet here) to rrset, skips rr */ 753933707f3Ssthen static int 7545d76a658Ssthen add_rr_to_rrset(struct rrset_parse* rrset, sldns_buffer* pkt, 755933707f3Ssthen struct msg_parse* msg, struct regional* region, 7565d76a658Ssthen sldns_pkt_section section, uint16_t type) 757933707f3Ssthen { 758933707f3Ssthen struct rr_parse* rr; 759933707f3Ssthen /* check section of rrset. */ 760933707f3Ssthen if(rrset->section != section && type != LDNS_RR_TYPE_RRSIG && 761933707f3Ssthen rrset->type != LDNS_RR_TYPE_RRSIG) { 762933707f3Ssthen /* silently drop it - we drop the last part, since 763933707f3Ssthen * trust in rr data depends on the section it is in. 764933707f3Ssthen * the less trustworthy part is discarded. 765933707f3Ssthen * also the last part is more likely to be incomplete. 766933707f3Ssthen * RFC 2181: must put RRset only once in response. */ 767933707f3Ssthen /* 768933707f3Ssthen verbose(VERB_QUERY, "Packet contains rrset data in " 769933707f3Ssthen "multiple sections, dropped last part."); 770933707f3Ssthen log_buf(VERB_QUERY, "packet was", pkt); 771933707f3Ssthen */ 772933707f3Ssthen /* forwards */ 773933707f3Ssthen if(!skip_ttl_rdata(pkt)) 774933707f3Ssthen return LDNS_RCODE_FORMERR; 775933707f3Ssthen return 0; 776933707f3Ssthen } 777933707f3Ssthen 778933707f3Ssthen if( (msg->qtype == LDNS_RR_TYPE_RRSIG || 779933707f3Ssthen msg->qtype == LDNS_RR_TYPE_ANY) 7805d76a658Ssthen && sig_is_double(pkt, rrset, sldns_buffer_current(pkt))) { 781933707f3Ssthen if(!skip_ttl_rdata(pkt)) 782933707f3Ssthen return LDNS_RCODE_FORMERR; 783933707f3Ssthen return 0; 784933707f3Ssthen } 785933707f3Ssthen 786933707f3Ssthen /* create rr */ 787933707f3Ssthen if(!(rr = (struct rr_parse*)regional_alloc(region, sizeof(*rr)))) 788933707f3Ssthen return LDNS_RCODE_SERVFAIL; 789933707f3Ssthen rr->outside_packet = 0; 7905d76a658Ssthen rr->ttl_data = sldns_buffer_current(pkt); 791933707f3Ssthen rr->next = 0; 792933707f3Ssthen if(type == LDNS_RR_TYPE_RRSIG && rrset->type != LDNS_RR_TYPE_RRSIG) { 793933707f3Ssthen if(rrset->rrsig_last) 794933707f3Ssthen rrset->rrsig_last->next = rr; 795933707f3Ssthen else rrset->rrsig_first = rr; 796933707f3Ssthen rrset->rrsig_last = rr; 797933707f3Ssthen rrset->rrsig_count++; 798933707f3Ssthen } else { 799933707f3Ssthen if(rrset->rr_last) 800933707f3Ssthen rrset->rr_last->next = rr; 801933707f3Ssthen else rrset->rr_first = rr; 802933707f3Ssthen rrset->rr_last = rr; 803933707f3Ssthen rrset->rr_count++; 804933707f3Ssthen } 805933707f3Ssthen 806933707f3Ssthen /* calc decompressed size */ 807933707f3Ssthen if(!calc_size(pkt, type, rr)) 808933707f3Ssthen return LDNS_RCODE_FORMERR; 809933707f3Ssthen rrset->size += rr->size; 810933707f3Ssthen 811933707f3Ssthen return 0; 812933707f3Ssthen } 813933707f3Ssthen 814933707f3Ssthen /** 815933707f3Ssthen * Parse packet RR section, for answer, authority and additional sections. 816933707f3Ssthen * @param pkt: packet, position at call must be at start of section. 817933707f3Ssthen * at end position is after section. 818933707f3Ssthen * @param msg: store results here. 819933707f3Ssthen * @param region: how to alloc results. 820933707f3Ssthen * @param section: section enum. 821933707f3Ssthen * @param num_rrs: how many rrs are in the section. 822933707f3Ssthen * @param num_rrsets: returns number of rrsets in the section. 823933707f3Ssthen * @return: 0 if OK, or rcode on error. 824933707f3Ssthen */ 825933707f3Ssthen static int 8265d76a658Ssthen parse_section(sldns_buffer* pkt, struct msg_parse* msg, 8275d76a658Ssthen struct regional* region, sldns_pkt_section section, 828933707f3Ssthen uint16_t num_rrs, size_t* num_rrsets) 829933707f3Ssthen { 830933707f3Ssthen uint16_t i; 831933707f3Ssthen uint8_t* dname, *prev_dname_f = NULL, *prev_dname_l = NULL; 832933707f3Ssthen size_t dnamelen, prev_dnamelen = 0; 833933707f3Ssthen uint16_t type, prev_type = 0; 834933707f3Ssthen uint16_t dclass, prev_dclass = 0; 835933707f3Ssthen uint32_t rrset_flags = 0; 83677079be7Ssthen hashvalue_type hash = 0; 837933707f3Ssthen struct rrset_parse* rrset = NULL; 838933707f3Ssthen int r; 839933707f3Ssthen 840933707f3Ssthen if(num_rrs == 0) 841933707f3Ssthen return 0; 8425d76a658Ssthen if(sldns_buffer_remaining(pkt) <= 0) 843933707f3Ssthen return LDNS_RCODE_FORMERR; 844933707f3Ssthen for(i=0; i<num_rrs; i++) { 845933707f3Ssthen /* parse this RR. */ 8465d76a658Ssthen dname = sldns_buffer_current(pkt); 847933707f3Ssthen if((dnamelen = pkt_dname_len(pkt)) == 0) 848933707f3Ssthen return LDNS_RCODE_FORMERR; 8495d76a658Ssthen if(sldns_buffer_remaining(pkt) < 10) /* type, class, ttl, len */ 850933707f3Ssthen return LDNS_RCODE_FORMERR; 8515d76a658Ssthen type = sldns_buffer_read_u16(pkt); 8525d76a658Ssthen sldns_buffer_read(pkt, &dclass, sizeof(dclass)); 853933707f3Ssthen 854933707f3Ssthen if(0) { /* debug show what is being parsed. */ 855933707f3Ssthen if(type == LDNS_RR_TYPE_RRSIG) { 856933707f3Ssthen uint16_t t; 857933707f3Ssthen if(pkt_rrsig_covered(pkt, 8585d76a658Ssthen sldns_buffer_current(pkt), &t)) 859933707f3Ssthen fprintf(stderr, "parse of %s(%d) [%s(%d)]", 8605d76a658Ssthen sldns_rr_descript(type)? 8615d76a658Ssthen sldns_rr_descript(type)->_name: "??", 862933707f3Ssthen (int)type, 8635d76a658Ssthen sldns_rr_descript(t)? 8645d76a658Ssthen sldns_rr_descript(t)->_name: "??", 865933707f3Ssthen (int)t); 866933707f3Ssthen } else 867933707f3Ssthen fprintf(stderr, "parse of %s(%d)", 8685d76a658Ssthen sldns_rr_descript(type)? 8695d76a658Ssthen sldns_rr_descript(type)->_name: "??", 870933707f3Ssthen (int)type); 871933707f3Ssthen fprintf(stderr, " %s(%d) ", 8725d76a658Ssthen sldns_lookup_by_id(sldns_rr_classes, 8735d76a658Ssthen (int)ntohs(dclass))?sldns_lookup_by_id( 8745d76a658Ssthen sldns_rr_classes, (int)ntohs(dclass))->name: 875933707f3Ssthen "??", (int)ntohs(dclass)); 876933707f3Ssthen dname_print(stderr, pkt, dname); 877933707f3Ssthen fprintf(stderr, "\n"); 878933707f3Ssthen } 879933707f3Ssthen 880933707f3Ssthen /* see if it is part of an existing RR set */ 881933707f3Ssthen if(!find_rrset(msg, pkt, dname, dnamelen, type, dclass, &hash, 882933707f3Ssthen &rrset_flags, &prev_dname_f, &prev_dname_l, 883933707f3Ssthen &prev_dnamelen, &prev_type, &prev_dclass, &rrset, 884933707f3Ssthen section, region)) 885933707f3Ssthen return LDNS_RCODE_SERVFAIL; 886933707f3Ssthen if(!rrset) { 887933707f3Ssthen /* it is a new RR set. hash&flags already calculated.*/ 888933707f3Ssthen (*num_rrsets)++; 889933707f3Ssthen rrset = new_rrset(msg, dname, dnamelen, type, dclass, 890933707f3Ssthen hash, rrset_flags, section, region); 891933707f3Ssthen if(!rrset) 892933707f3Ssthen return LDNS_RCODE_SERVFAIL; 893933707f3Ssthen } 894933707f3Ssthen else if(0) { 895933707f3Ssthen fprintf(stderr, "is part of existing: "); 896933707f3Ssthen dname_print(stderr, pkt, rrset->dname); 897933707f3Ssthen fprintf(stderr, " type %s(%d)\n", 8985d76a658Ssthen sldns_rr_descript(rrset->type)? 8995d76a658Ssthen sldns_rr_descript(rrset->type)->_name: "??", 900933707f3Ssthen (int)rrset->type); 901933707f3Ssthen } 902933707f3Ssthen /* add to rrset. */ 903933707f3Ssthen if((r=add_rr_to_rrset(rrset, pkt, msg, region, section, 904933707f3Ssthen type)) != 0) 905933707f3Ssthen return r; 906933707f3Ssthen } 907933707f3Ssthen return 0; 908933707f3Ssthen } 909933707f3Ssthen 910933707f3Ssthen int 9115d76a658Ssthen parse_packet(sldns_buffer* pkt, struct msg_parse* msg, struct regional* region) 912933707f3Ssthen { 913933707f3Ssthen int ret; 9145d76a658Ssthen if(sldns_buffer_remaining(pkt) < LDNS_HEADER_SIZE) 915933707f3Ssthen return LDNS_RCODE_FORMERR; 916933707f3Ssthen /* read the header */ 9175d76a658Ssthen sldns_buffer_read(pkt, &msg->id, sizeof(uint16_t)); 9185d76a658Ssthen msg->flags = sldns_buffer_read_u16(pkt); 9195d76a658Ssthen msg->qdcount = sldns_buffer_read_u16(pkt); 9205d76a658Ssthen msg->ancount = sldns_buffer_read_u16(pkt); 9215d76a658Ssthen msg->nscount = sldns_buffer_read_u16(pkt); 9225d76a658Ssthen msg->arcount = sldns_buffer_read_u16(pkt); 923933707f3Ssthen if(msg->qdcount > 1) 924933707f3Ssthen return LDNS_RCODE_FORMERR; 925933707f3Ssthen if((ret = parse_query_section(pkt, msg)) != 0) 926933707f3Ssthen return ret; 927933707f3Ssthen if((ret = parse_section(pkt, msg, region, LDNS_SECTION_ANSWER, 928933707f3Ssthen msg->ancount, &msg->an_rrsets)) != 0) 929933707f3Ssthen return ret; 930933707f3Ssthen if((ret = parse_section(pkt, msg, region, LDNS_SECTION_AUTHORITY, 931933707f3Ssthen msg->nscount, &msg->ns_rrsets)) != 0) 932933707f3Ssthen return ret; 9335d76a658Ssthen if(sldns_buffer_remaining(pkt) == 0 && msg->arcount == 1) { 934933707f3Ssthen /* BIND accepts leniently that an EDNS record is missing. 935933707f3Ssthen * so, we do too. */ 936933707f3Ssthen } else if((ret = parse_section(pkt, msg, region, 937933707f3Ssthen LDNS_SECTION_ADDITIONAL, msg->arcount, &msg->ar_rrsets)) != 0) 938933707f3Ssthen return ret; 9395d76a658Ssthen /* if(sldns_buffer_remaining(pkt) > 0) { */ 940933707f3Ssthen /* there is spurious data at end of packet. ignore */ 941933707f3Ssthen /* } */ 942933707f3Ssthen msg->rrset_count = msg->an_rrsets + msg->ns_rrsets + msg->ar_rrsets; 943933707f3Ssthen return 0; 944933707f3Ssthen } 945933707f3Ssthen 9462ee382b6Ssthen /** parse EDNS options from EDNS wireformat rdata */ 9472ee382b6Ssthen static int 948e21c60efSsthen parse_edns_options_from_query(uint8_t* rdata_ptr, size_t rdata_len, 949e21c60efSsthen struct edns_data* edns, struct config_file* cfg, struct comm_point* c, 950*98bc733bSsthen struct comm_reply* repinfo, uint32_t now, struct regional* region, 951*98bc733bSsthen struct cookie_secrets* cookie_secrets) 9522ee382b6Ssthen { 953e21c60efSsthen /* To respond with a Keepalive option, the client connection must have 954e21c60efSsthen * received one message with a TCP Keepalive EDNS option, and that 955e21c60efSsthen * option must have 0 length data. Subsequent messages sent on that 956e21c60efSsthen * connection will have a TCP Keepalive option. 957e21c60efSsthen * 958e21c60efSsthen * In the if-statement below, the option is added unsolicited. This 959e21c60efSsthen * means that the client has sent an KEEPALIVE option earlier. We know 960e21c60efSsthen * here this is true, because c->tcp_keepalive is set. 961e21c60efSsthen */ 962e21c60efSsthen if (cfg && cfg->do_tcp_keepalive && c && c->type != comm_udp && c->tcp_keepalive) { 963e21c60efSsthen if(!edns_opt_list_append_keepalive(&edns->opt_list_out, 964e21c60efSsthen c->tcp_timeout_msec / 100, region)) { 965e21c60efSsthen log_err("out of memory"); 966e21c60efSsthen return LDNS_RCODE_SERVFAIL; 967e21c60efSsthen } 968e21c60efSsthen } 969e21c60efSsthen 9702ee382b6Ssthen /* while still more options, and have code+len to read */ 9712ee382b6Ssthen /* ignores partial content (i.e. rdata len 3) */ 9722ee382b6Ssthen while(rdata_len >= 4) { 9732ee382b6Ssthen uint16_t opt_code = sldns_read_uint16(rdata_ptr); 9742ee382b6Ssthen uint16_t opt_len = sldns_read_uint16(rdata_ptr+2); 9758b7325afSsthen uint8_t server_cookie[40]; 9768b7325afSsthen enum edns_cookie_val_status cookie_val_status; 9778b7325afSsthen int cookie_is_v4 = 1; 9788b7325afSsthen 9792ee382b6Ssthen rdata_ptr += 4; 9802ee382b6Ssthen rdata_len -= 4; 9812ee382b6Ssthen if(opt_len > rdata_len) 9822ee382b6Ssthen break; /* option code partial */ 983e21c60efSsthen 984e21c60efSsthen /* handle parse time edns options here */ 985e21c60efSsthen switch(opt_code) { 986e21c60efSsthen case LDNS_EDNS_NSID: 987e21c60efSsthen if (!cfg || !cfg->nsid) 988e21c60efSsthen break; 989e21c60efSsthen if(!edns_opt_list_append(&edns->opt_list_out, 990e21c60efSsthen LDNS_EDNS_NSID, cfg->nsid_len, 991e21c60efSsthen cfg->nsid, region)) { 9922ee382b6Ssthen log_err("out of memory"); 993e21c60efSsthen return LDNS_RCODE_SERVFAIL; 994e21c60efSsthen } 995e21c60efSsthen break; 996e21c60efSsthen 997e21c60efSsthen case LDNS_EDNS_KEEPALIVE: 998e21c60efSsthen /* To respond with a Keepalive option, the client 999e21c60efSsthen * connection must have received one message with a TCP 1000e21c60efSsthen * Keepalive EDNS option, and that option must have 0 1001e21c60efSsthen * length data. Subsequent messages sent on that 1002e21c60efSsthen * connection will have a TCP Keepalive option. 1003e21c60efSsthen * 1004e21c60efSsthen * This should be the first time the client sends this 1005e21c60efSsthen * option, so c->tcp_keepalive is not set. 1006e21c60efSsthen * Besides adding the reply KEEPALIVE option, 1007e21c60efSsthen * c->tcp_keepalive will be set so that the 1008e21c60efSsthen * option will be added unsolicited in subsequent 1009e21c60efSsthen * responses (see the comment above the if-statement 1010e21c60efSsthen * at the start of this function). 1011e21c60efSsthen */ 1012e21c60efSsthen if (!cfg || !cfg->do_tcp_keepalive || !c || 1013e21c60efSsthen c->type == comm_udp || c->tcp_keepalive) 1014e21c60efSsthen break; 1015e21c60efSsthen if(opt_len) { 1016e21c60efSsthen verbose(VERB_ALGO, "query with bad edns keepalive."); 1017e21c60efSsthen return LDNS_RCODE_FORMERR; 1018e21c60efSsthen } 1019e21c60efSsthen if(!edns_opt_list_append_keepalive(&edns->opt_list_out, 1020e21c60efSsthen c->tcp_timeout_msec / 100, 1021e21c60efSsthen region)) { 1022e21c60efSsthen log_err("out of memory"); 1023e21c60efSsthen return LDNS_RCODE_SERVFAIL; 1024e21c60efSsthen } 1025e21c60efSsthen c->tcp_keepalive = 1; 1026e21c60efSsthen break; 1027e21c60efSsthen 1028e21c60efSsthen case LDNS_EDNS_PADDING: 1029e21c60efSsthen if(!cfg || !cfg->pad_responses || 1030e21c60efSsthen !c || c->type != comm_tcp ||!c->ssl) 1031e21c60efSsthen break; 1032e21c60efSsthen if(!edns_opt_list_append(&edns->opt_list_out, 1033e21c60efSsthen LDNS_EDNS_PADDING, 1034e21c60efSsthen 0, NULL, region)) { 1035e21c60efSsthen log_err("out of memory"); 1036e21c60efSsthen return LDNS_RCODE_SERVFAIL; 1037e21c60efSsthen } 1038e21c60efSsthen edns->padding_block_size = cfg->pad_responses_block_size; 1039e21c60efSsthen break; 1040e21c60efSsthen 10418b7325afSsthen case LDNS_EDNS_COOKIE: 10428b7325afSsthen if(!cfg || !cfg->do_answer_cookie || !repinfo) 10438b7325afSsthen break; 10448b7325afSsthen if(opt_len != 8 && (opt_len < 16 || opt_len > 40)) { 10458b7325afSsthen verbose(VERB_ALGO, "worker request: " 10468b7325afSsthen "badly formatted cookie"); 10478b7325afSsthen return LDNS_RCODE_FORMERR; 10488b7325afSsthen } 10498b7325afSsthen edns->cookie_present = 1; 10508b7325afSsthen 10518b7325afSsthen /* Copy client cookie, version and timestamp for 10528b7325afSsthen * validation and creation purposes. 10538b7325afSsthen */ 10548b7325afSsthen if(opt_len >= 16) { 10558b7325afSsthen memmove(server_cookie, rdata_ptr, 16); 10568b7325afSsthen } else { 10578b7325afSsthen memset(server_cookie, 0, 16); 10588b7325afSsthen memmove(server_cookie, rdata_ptr, opt_len); 10598b7325afSsthen } 10608b7325afSsthen 10618b7325afSsthen /* Copy client ip for validation and creation 10628b7325afSsthen * purposes. It will be overwritten if (re)creation 10638b7325afSsthen * is needed. 10648b7325afSsthen */ 10658b7325afSsthen if(repinfo->remote_addr.ss_family == AF_INET) { 10668b7325afSsthen memcpy(server_cookie + 16, 10678b7325afSsthen &((struct sockaddr_in*)&repinfo->remote_addr)->sin_addr, 4); 10688b7325afSsthen } else { 10698b7325afSsthen cookie_is_v4 = 0; 10708b7325afSsthen memcpy(server_cookie + 16, 10718b7325afSsthen &((struct sockaddr_in6*)&repinfo->remote_addr)->sin6_addr, 16); 10728b7325afSsthen } 10738b7325afSsthen 1074*98bc733bSsthen if(cfg->cookie_secret_file && 1075*98bc733bSsthen cfg->cookie_secret_file[0]) { 1076*98bc733bSsthen /* Loop over the active and staging cookies. */ 1077*98bc733bSsthen cookie_val_status = 1078*98bc733bSsthen cookie_secrets_server_validate( 1079*98bc733bSsthen rdata_ptr, opt_len, cookie_secrets, 1080*98bc733bSsthen cookie_is_v4, server_cookie, now); 1081*98bc733bSsthen } else { 1082*98bc733bSsthen /* Use the cookie option value to validate. */ 10838b7325afSsthen cookie_val_status = edns_cookie_server_validate( 10848b7325afSsthen rdata_ptr, opt_len, cfg->cookie_secret, 10858b7325afSsthen cfg->cookie_secret_len, cookie_is_v4, 10868b7325afSsthen server_cookie, now); 1087*98bc733bSsthen } 1088*98bc733bSsthen if(cookie_val_status == COOKIE_STATUS_VALID_RENEW) 1089*98bc733bSsthen edns->cookie_valid = 1; 10908b7325afSsthen switch(cookie_val_status) { 10918b7325afSsthen case COOKIE_STATUS_VALID: 10928b7325afSsthen edns->cookie_valid = 1; 10938b7325afSsthen /* Reuse cookie */ 10948b7325afSsthen if(!edns_opt_list_append( 10958b7325afSsthen &edns->opt_list_out, LDNS_EDNS_COOKIE, 10968b7325afSsthen opt_len, rdata_ptr, region)) { 10978b7325afSsthen log_err("out of memory"); 10988b7325afSsthen return LDNS_RCODE_SERVFAIL; 10998b7325afSsthen } 11008b7325afSsthen /* Cookie to be reused added to outgoing 11018b7325afSsthen * options. Done! 11028b7325afSsthen */ 11038b7325afSsthen break; 11048b7325afSsthen case COOKIE_STATUS_CLIENT_ONLY: 11058b7325afSsthen edns->cookie_client = 1; 1106*98bc733bSsthen ATTR_FALLTHROUGH 11078b7325afSsthen /* fallthrough */ 1108*98bc733bSsthen case COOKIE_STATUS_VALID_RENEW: 11098b7325afSsthen case COOKIE_STATUS_FUTURE: 11108b7325afSsthen case COOKIE_STATUS_EXPIRED: 11118b7325afSsthen case COOKIE_STATUS_INVALID: 11128b7325afSsthen default: 1113*98bc733bSsthen if(cfg->cookie_secret_file && 1114*98bc733bSsthen cfg->cookie_secret_file[0]) { 1115*98bc733bSsthen if(!cookie_secrets) 1116*98bc733bSsthen break; 1117*98bc733bSsthen lock_basic_lock(&cookie_secrets->lock); 1118*98bc733bSsthen if(cookie_secrets->cookie_count < 1) { 1119*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 1120*98bc733bSsthen break; 1121*98bc733bSsthen } 1122*98bc733bSsthen edns_cookie_server_write(server_cookie, 1123*98bc733bSsthen cookie_secrets->cookie_secrets[0].cookie_secret, 1124*98bc733bSsthen cookie_is_v4, now); 1125*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 1126*98bc733bSsthen } else { 11278b7325afSsthen edns_cookie_server_write(server_cookie, 11288b7325afSsthen cfg->cookie_secret, cookie_is_v4, now); 1129*98bc733bSsthen } 11308b7325afSsthen if(!edns_opt_list_append(&edns->opt_list_out, 11318b7325afSsthen LDNS_EDNS_COOKIE, 24, server_cookie, 11328b7325afSsthen region)) { 11338b7325afSsthen log_err("out of memory"); 11348b7325afSsthen return LDNS_RCODE_SERVFAIL; 11358b7325afSsthen } 11368b7325afSsthen break; 11378b7325afSsthen } 11388b7325afSsthen break; 1139e21c60efSsthen default: 1140e21c60efSsthen break; 1141e21c60efSsthen } 1142e21c60efSsthen if(!edns_opt_list_append(&edns->opt_list_in, 1143e21c60efSsthen opt_code, opt_len, rdata_ptr, region)) { 1144e21c60efSsthen log_err("out of memory"); 1145e21c60efSsthen return LDNS_RCODE_SERVFAIL; 11462ee382b6Ssthen } 11472ee382b6Ssthen rdata_ptr += opt_len; 11482ee382b6Ssthen rdata_len -= opt_len; 11492ee382b6Ssthen } 1150e21c60efSsthen return LDNS_RCODE_NOERROR; 11512ee382b6Ssthen } 11522ee382b6Ssthen 1153933707f3Ssthen int 1154e21c60efSsthen parse_extract_edns_from_response_msg(struct msg_parse* msg, 1155e21c60efSsthen struct edns_data* edns, struct regional* region) 1156933707f3Ssthen { 1157933707f3Ssthen struct rrset_parse* rrset = msg->rrset_first; 1158933707f3Ssthen struct rrset_parse* prev = 0; 1159933707f3Ssthen struct rrset_parse* found = 0; 1160933707f3Ssthen struct rrset_parse* found_prev = 0; 11612ee382b6Ssthen size_t rdata_len; 11622ee382b6Ssthen uint8_t* rdata_ptr; 1163933707f3Ssthen /* since the class encodes the UDP size, we cannot use hash table to 1164933707f3Ssthen * find the EDNS OPT record. Scan the packet. */ 1165933707f3Ssthen while(rrset) { 1166933707f3Ssthen if(rrset->type == LDNS_RR_TYPE_OPT) { 1167933707f3Ssthen /* only one OPT RR allowed. */ 1168933707f3Ssthen if(found) return LDNS_RCODE_FORMERR; 1169933707f3Ssthen /* found it! */ 1170933707f3Ssthen found_prev = prev; 1171933707f3Ssthen found = rrset; 1172933707f3Ssthen } 1173933707f3Ssthen prev = rrset; 1174933707f3Ssthen rrset = rrset->rrset_all_next; 1175933707f3Ssthen } 1176933707f3Ssthen if(!found) { 1177933707f3Ssthen memset(edns, 0, sizeof(*edns)); 1178933707f3Ssthen edns->udp_size = 512; 1179933707f3Ssthen return 0; 1180933707f3Ssthen } 1181933707f3Ssthen /* check the found RRset */ 1182933707f3Ssthen /* most lenient check possible. ignore dname, use last opt */ 1183933707f3Ssthen if(found->section != LDNS_SECTION_ADDITIONAL) 1184933707f3Ssthen return LDNS_RCODE_FORMERR; 1185933707f3Ssthen if(found->rr_count == 0) 1186933707f3Ssthen return LDNS_RCODE_FORMERR; 1187933707f3Ssthen if(0) { /* strict checking of dname and RRcount */ 1188933707f3Ssthen if(found->dname_len != 1 || !found->dname 1189933707f3Ssthen || found->dname[0] != 0) return LDNS_RCODE_FORMERR; 1190933707f3Ssthen if(found->rr_count != 1) return LDNS_RCODE_FORMERR; 1191933707f3Ssthen } 1192933707f3Ssthen log_assert(found->rr_first && found->rr_last); 1193933707f3Ssthen 1194933707f3Ssthen /* remove from packet */ 1195933707f3Ssthen if(found_prev) found_prev->rrset_all_next = found->rrset_all_next; 1196933707f3Ssthen else msg->rrset_first = found->rrset_all_next; 1197933707f3Ssthen if(found == msg->rrset_last) 1198933707f3Ssthen msg->rrset_last = found_prev; 1199933707f3Ssthen msg->arcount --; 1200933707f3Ssthen msg->ar_rrsets --; 1201933707f3Ssthen msg->rrset_count --; 1202933707f3Ssthen 1203933707f3Ssthen /* take the data ! */ 1204933707f3Ssthen edns->edns_present = 1; 1205933707f3Ssthen edns->ext_rcode = found->rr_last->ttl_data[0]; 1206933707f3Ssthen edns->edns_version = found->rr_last->ttl_data[1]; 12075d76a658Ssthen edns->bits = sldns_read_uint16(&found->rr_last->ttl_data[2]); 1208933707f3Ssthen edns->udp_size = ntohs(found->rrset_class); 1209e21c60efSsthen edns->opt_list_in = NULL; 1210e21c60efSsthen edns->opt_list_out = NULL; 1211e21c60efSsthen edns->opt_list_inplace_cb_out = NULL; 12129982a05dSsthen edns->padding_block_size = 0; 12138b7325afSsthen edns->cookie_present = 0; 12148b7325afSsthen edns->cookie_valid = 0; 12152ee382b6Ssthen 12162ee382b6Ssthen /* take the options */ 12172be9e038Ssthen rdata_len = found->rr_first->size-2; 12182ee382b6Ssthen rdata_ptr = found->rr_first->ttl_data+6; 12192ee382b6Ssthen 1220e21c60efSsthen /* while still more options, and have code+len to read */ 1221e21c60efSsthen /* ignores partial content (i.e. rdata len 3) */ 1222e21c60efSsthen while(rdata_len >= 4) { 1223e21c60efSsthen uint16_t opt_code = sldns_read_uint16(rdata_ptr); 1224e21c60efSsthen uint16_t opt_len = sldns_read_uint16(rdata_ptr+2); 1225e21c60efSsthen rdata_ptr += 4; 1226e21c60efSsthen rdata_len -= 4; 1227e21c60efSsthen if(opt_len > rdata_len) 1228e21c60efSsthen break; /* option code partial */ 1229e21c60efSsthen 1230e21c60efSsthen if(!edns_opt_list_append(&edns->opt_list_in, 1231e21c60efSsthen opt_code, opt_len, rdata_ptr, region)) { 1232e21c60efSsthen log_err("out of memory"); 1233e21c60efSsthen break; 1234e21c60efSsthen } 1235e21c60efSsthen rdata_ptr += opt_len; 1236e21c60efSsthen rdata_len -= opt_len; 1237e21c60efSsthen } 12382ee382b6Ssthen /* ignore rrsigs */ 1239e21c60efSsthen return LDNS_RCODE_NOERROR; 1240933707f3Ssthen } 1241933707f3Ssthen 124220237c55Ssthen /** skip RR in packet */ 124320237c55Ssthen static int 124420237c55Ssthen skip_pkt_rr(sldns_buffer* pkt) 124520237c55Ssthen { 124620237c55Ssthen if(sldns_buffer_remaining(pkt) < 1) return 0; 124720237c55Ssthen if(!pkt_dname_len(pkt)) 124820237c55Ssthen return 0; 124920237c55Ssthen if(sldns_buffer_remaining(pkt) < 4) return 0; 125020237c55Ssthen sldns_buffer_skip(pkt, 4); /* type and class */ 125120237c55Ssthen if(!skip_ttl_rdata(pkt)) 125220237c55Ssthen return 0; 125320237c55Ssthen return 1; 125420237c55Ssthen } 125520237c55Ssthen 125620237c55Ssthen /** skip RRs from packet */ 12570bdb4f62Ssthen int 125820237c55Ssthen skip_pkt_rrs(sldns_buffer* pkt, int num) 125920237c55Ssthen { 126020237c55Ssthen int i; 126120237c55Ssthen for(i=0; i<num; i++) { 126220237c55Ssthen if(!skip_pkt_rr(pkt)) 126320237c55Ssthen return 0; 126420237c55Ssthen } 126520237c55Ssthen return 1; 126620237c55Ssthen } 126720237c55Ssthen 1268933707f3Ssthen int 1269e21c60efSsthen parse_edns_from_query_pkt(sldns_buffer* pkt, struct edns_data* edns, 12708b7325afSsthen struct config_file* cfg, struct comm_point* c, 1271*98bc733bSsthen struct comm_reply* repinfo, time_t now, struct regional* region, 1272*98bc733bSsthen struct cookie_secrets* cookie_secrets) 1273933707f3Ssthen { 12742ee382b6Ssthen size_t rdata_len; 12752ee382b6Ssthen uint8_t* rdata_ptr; 12765d76a658Ssthen log_assert(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) == 1); 1277fadbf8b1Sflorian memset(edns, 0, sizeof(*edns)); 127820237c55Ssthen if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0 || 127920237c55Ssthen LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) { 128020237c55Ssthen if(!skip_pkt_rrs(pkt, ((int)LDNS_ANCOUNT(sldns_buffer_begin(pkt)))+ 128120237c55Ssthen ((int)LDNS_NSCOUNT(sldns_buffer_begin(pkt))))) 1282fadbf8b1Sflorian return LDNS_RCODE_FORMERR; 128320237c55Ssthen } 1284933707f3Ssthen /* check edns section is present */ 12855d76a658Ssthen if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) { 1286933707f3Ssthen return LDNS_RCODE_FORMERR; 1287933707f3Ssthen } 12885d76a658Ssthen if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) == 0) { 1289933707f3Ssthen edns->udp_size = 512; 1290933707f3Ssthen return 0; 1291933707f3Ssthen } 1292933707f3Ssthen /* domain name must be the root of length 1. */ 1293933707f3Ssthen if(pkt_dname_len(pkt) != 1) 1294933707f3Ssthen return LDNS_RCODE_FORMERR; 12955d76a658Ssthen if(sldns_buffer_remaining(pkt) < 10) /* type, class, ttl, rdatalen */ 1296933707f3Ssthen return LDNS_RCODE_FORMERR; 12975d76a658Ssthen if(sldns_buffer_read_u16(pkt) != LDNS_RR_TYPE_OPT) 1298933707f3Ssthen return LDNS_RCODE_FORMERR; 1299933707f3Ssthen edns->edns_present = 1; 13005d76a658Ssthen edns->udp_size = sldns_buffer_read_u16(pkt); /* class is udp size */ 13015d76a658Ssthen edns->ext_rcode = sldns_buffer_read_u8(pkt); /* ttl used for bits */ 13025d76a658Ssthen edns->edns_version = sldns_buffer_read_u8(pkt); 13035d76a658Ssthen edns->bits = sldns_buffer_read_u16(pkt); 1304e21c60efSsthen edns->opt_list_in = NULL; 1305e21c60efSsthen edns->opt_list_out = NULL; 1306e21c60efSsthen edns->opt_list_inplace_cb_out = NULL; 13079982a05dSsthen edns->padding_block_size = 0; 13088b7325afSsthen edns->cookie_present = 0; 13098b7325afSsthen edns->cookie_valid = 0; 13102ee382b6Ssthen 13112ee382b6Ssthen /* take the options */ 13122ee382b6Ssthen rdata_len = sldns_buffer_read_u16(pkt); 13132ee382b6Ssthen if(sldns_buffer_remaining(pkt) < rdata_len) 13142ee382b6Ssthen return LDNS_RCODE_FORMERR; 13152ee382b6Ssthen rdata_ptr = sldns_buffer_current(pkt); 13162ee382b6Ssthen /* ignore rrsigs */ 1317e21c60efSsthen return parse_edns_options_from_query(rdata_ptr, rdata_len, edns, cfg, 1318*98bc733bSsthen c, repinfo, now, region, cookie_secrets); 1319933707f3Ssthen } 132077079be7Ssthen 132177079be7Ssthen void 132277079be7Ssthen log_edns_opt_list(enum verbosity_value level, const char* info_str, 132377079be7Ssthen struct edns_option* list) 132477079be7Ssthen { 132577079be7Ssthen if(verbosity >= level && list) { 132677079be7Ssthen char str[128], *s; 132777079be7Ssthen size_t slen; 132877079be7Ssthen verbose(level, "%s", info_str); 132977079be7Ssthen while(list) { 133077079be7Ssthen s = str; 133177079be7Ssthen slen = sizeof(str); 133277079be7Ssthen (void)sldns_wire2str_edns_option_print(&s, &slen, list->opt_code, 133377079be7Ssthen list->opt_data, list->opt_len); 133477079be7Ssthen verbose(level, " %s", str); 133577079be7Ssthen list = list->next; 133677079be7Ssthen } 133777079be7Ssthen } 133877079be7Ssthen } 13390bdb4f62Ssthen 1340d896b962Ssthen /** remove RR from msgparse RRset, return true if rrset is entirely bad */ 1341d896b962Ssthen int 1342d896b962Ssthen msgparse_rrset_remove_rr(const char* str, sldns_buffer* pkt, struct rrset_parse* rrset, 1343d896b962Ssthen struct rr_parse* prev, struct rr_parse* rr, struct sockaddr_storage* addr, socklen_t addrlen) 1344d896b962Ssthen { 1345d896b962Ssthen if(verbosity >= VERB_QUERY && rrset->dname_len <= LDNS_MAX_DOMAINLEN && str) { 1346d896b962Ssthen uint8_t buf[LDNS_MAX_DOMAINLEN+1]; 1347d896b962Ssthen dname_pkt_copy(pkt, buf, rrset->dname); 1348d896b962Ssthen if(addr) 1349d896b962Ssthen log_name_addr(VERB_QUERY, str, buf, addr, addrlen); 1350d896b962Ssthen else log_nametypeclass(VERB_QUERY, str, buf, 1351d896b962Ssthen rrset->type, ntohs(rrset->rrset_class)); 1352d896b962Ssthen } 1353d896b962Ssthen if(prev) 1354d896b962Ssthen prev->next = rr->next; 1355d896b962Ssthen else rrset->rr_first = rr->next; 1356d896b962Ssthen if(rrset->rr_last == rr) 1357d896b962Ssthen rrset->rr_last = prev; 1358d896b962Ssthen rrset->rr_count --; 1359d896b962Ssthen rrset->size -= rr->size; 1360d896b962Ssthen /* rr struct still exists, but is unlinked, so that in the for loop 1361d896b962Ssthen * the rr->next works fine to continue. */ 1362d896b962Ssthen return rrset->rr_count == 0; 1363d896b962Ssthen } 1364