1 1.1 christos /* 2 1.1 christos * util/data/msgreply.c - store message and reply data. 3 1.1 christos * 4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 1.1 christos * 6 1.1 christos * This software is open source. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 12 1.1 christos * Redistributions of source code must retain the above copyright notice, 13 1.1 christos * this list of conditions and the following disclaimer. 14 1.1 christos * 15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice, 16 1.1 christos * this list of conditions and the following disclaimer in the documentation 17 1.1 christos * and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may 20 1.1 christos * be used to endorse or promote products derived from this software without 21 1.1 christos * specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /** 37 1.1 christos * \file 38 1.1 christos * 39 1.1 christos * This file contains a data structure to store a message and its reply. 40 1.1 christos */ 41 1.1 christos 42 1.1 christos #include "config.h" 43 1.1 christos #include "util/data/msgreply.h" 44 1.1 christos #include "util/storage/lookup3.h" 45 1.1 christos #include "util/log.h" 46 1.1 christos #include "util/alloc.h" 47 1.1 christos #include "util/netevent.h" 48 1.1 christos #include "util/net_help.h" 49 1.1 christos #include "util/data/dname.h" 50 1.1 christos #include "util/regional.h" 51 1.1 christos #include "util/data/msgparse.h" 52 1.1 christos #include "util/data/msgencode.h" 53 1.1 christos #include "sldns/sbuffer.h" 54 1.1 christos #include "sldns/wire2str.h" 55 1.1.1.2 christos #include "util/module.h" 56 1.1.1.2 christos #include "util/fptr_wlist.h" 57 1.1 christos 58 1.1 christos /** MAX TTL default for messages and rrsets */ 59 1.1 christos time_t MAX_TTL = 3600 * 24 * 10; /* ten days */ 60 1.1 christos /** MIN TTL default for messages and rrsets */ 61 1.1 christos time_t MIN_TTL = 0; 62 1.1 christos /** MAX Negative TTL, for SOA records in authority section */ 63 1.1 christos time_t MAX_NEG_TTL = 3600; /* one hour */ 64 1.1.1.9 christos /** MIN Negative TTL, for SOA records in authority section */ 65 1.1.1.9 christos time_t MIN_NEG_TTL = 0; 66 1.1.1.6 christos /** If we serve expired entries and prefetch them */ 67 1.1.1.6 christos int SERVE_EXPIRED = 0; 68 1.1.1.4 christos /** Time to serve records after expiration */ 69 1.1.1.9 christos time_t SERVE_EXPIRED_TTL = 86400; 70 1.1.1.9 christos /** Reset serve expired TTL after failed update attempt */ 71 1.1.1.9 christos time_t SERVE_EXPIRED_TTL_RESET = 0; 72 1.1.1.6 christos /** TTL to use for expired records */ 73 1.1.1.6 christos time_t SERVE_EXPIRED_REPLY_TTL = 30; 74 1.1.1.6 christos /** If we serve the original TTL or decrementing TTLs */ 75 1.1.1.6 christos int SERVE_ORIGINAL_TTL = 0; 76 1.1 christos 77 1.1 christos /** allocate qinfo, return 0 on error */ 78 1.1 christos static int 79 1.1 christos parse_create_qinfo(sldns_buffer* pkt, struct msg_parse* msg, 80 1.1 christos struct query_info* qinf, struct regional* region) 81 1.1 christos { 82 1.1 christos if(msg->qname) { 83 1.1 christos if(region) 84 1.1 christos qinf->qname = (uint8_t*)regional_alloc(region, 85 1.1 christos msg->qname_len); 86 1.1 christos else qinf->qname = (uint8_t*)malloc(msg->qname_len); 87 1.1 christos if(!qinf->qname) return 0; 88 1.1 christos dname_pkt_copy(pkt, qinf->qname, msg->qname); 89 1.1 christos } else qinf->qname = 0; 90 1.1 christos qinf->qname_len = msg->qname_len; 91 1.1 christos qinf->qtype = msg->qtype; 92 1.1 christos qinf->qclass = msg->qclass; 93 1.1.1.2 christos qinf->local_alias = NULL; 94 1.1 christos return 1; 95 1.1 christos } 96 1.1 christos 97 1.1 christos /** constructor for replyinfo */ 98 1.1 christos struct reply_info* 99 1.1 christos construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd, 100 1.1.1.9 christos time_t ttl, time_t prettl, time_t expttl, time_t norecttl, size_t an, 101 1.1.1.9 christos size_t ns, size_t ar, size_t total, enum sec_status sec, 102 1.1.1.9 christos sldns_ede_code reason_bogus) 103 1.1 christos { 104 1.1 christos struct reply_info* rep; 105 1.1 christos /* rrset_count-1 because the first ref is part of the struct. */ 106 1.1 christos size_t s = sizeof(struct reply_info) - sizeof(struct rrset_ref) + 107 1.1 christos sizeof(struct ub_packed_rrset_key*) * total; 108 1.1 christos if(total >= RR_COUNT_MAX) return NULL; /* sanity check on numRRS*/ 109 1.1 christos if(region) 110 1.1 christos rep = (struct reply_info*)regional_alloc(region, s); 111 1.1 christos else rep = (struct reply_info*)malloc(s + 112 1.1 christos sizeof(struct rrset_ref) * (total)); 113 1.1 christos if(!rep) 114 1.1 christos return NULL; 115 1.1 christos rep->flags = flags; 116 1.1 christos rep->qdcount = qd; 117 1.1 christos rep->ttl = ttl; 118 1.1 christos rep->prefetch_ttl = prettl; 119 1.1.1.4 christos rep->serve_expired_ttl = expttl; 120 1.1.1.9 christos rep->serve_expired_norec_ttl = norecttl; 121 1.1 christos rep->an_numrrsets = an; 122 1.1 christos rep->ns_numrrsets = ns; 123 1.1 christos rep->ar_numrrsets = ar; 124 1.1 christos rep->rrset_count = total; 125 1.1 christos rep->security = sec; 126 1.1.1.8 christos rep->reason_bogus = reason_bogus; 127 1.1.1.8 christos /* this is only allocated and used for caching on copy */ 128 1.1.1.8 christos rep->reason_bogus_str = NULL; 129 1.1 christos rep->authoritative = 0; 130 1.1 christos /* array starts after the refs */ 131 1.1 christos if(region) 132 1.1 christos rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[0]); 133 1.1 christos else rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[total]); 134 1.1 christos /* zero the arrays to assist cleanup in case of malloc failure */ 135 1.1 christos memset( rep->rrsets, 0, sizeof(struct ub_packed_rrset_key*) * total); 136 1.1 christos if(!region) 137 1.1 christos memset( &rep->ref[0], 0, sizeof(struct rrset_ref) * total); 138 1.1 christos return rep; 139 1.1 christos } 140 1.1 christos 141 1.1 christos /** allocate replyinfo, return 0 on error */ 142 1.1 christos static int 143 1.1 christos parse_create_repinfo(struct msg_parse* msg, struct reply_info** rep, 144 1.1 christos struct regional* region) 145 1.1 christos { 146 1.1.1.9 christos *rep = construct_reply_info_base(region, msg->flags, msg->qdcount, 0, 147 1.1.1.9 christos 0, 0, 0, msg->an_rrsets, msg->ns_rrsets, msg->ar_rrsets, 148 1.1.1.8 christos msg->rrset_count, sec_status_unchecked, LDNS_EDE_NONE); 149 1.1 christos if(!*rep) 150 1.1 christos return 0; 151 1.1 christos return 1; 152 1.1 christos } 153 1.1 christos 154 1.1.1.2 christos int 155 1.1.1.2 christos reply_info_alloc_rrset_keys(struct reply_info* rep, struct alloc_cache* alloc, 156 1.1 christos struct regional* region) 157 1.1 christos { 158 1.1 christos size_t i; 159 1.1 christos for(i=0; i<rep->rrset_count; i++) { 160 1.1 christos if(region) { 161 1.1 christos rep->rrsets[i] = (struct ub_packed_rrset_key*) 162 1.1 christos regional_alloc(region, 163 1.1 christos sizeof(struct ub_packed_rrset_key)); 164 1.1 christos if(rep->rrsets[i]) { 165 1.1 christos memset(rep->rrsets[i], 0, 166 1.1 christos sizeof(struct ub_packed_rrset_key)); 167 1.1 christos rep->rrsets[i]->entry.key = rep->rrsets[i]; 168 1.1 christos } 169 1.1 christos } 170 1.1 christos else rep->rrsets[i] = alloc_special_obtain(alloc); 171 1.1 christos if(!rep->rrsets[i]) 172 1.1 christos return 0; 173 1.1 christos rep->rrsets[i]->entry.data = NULL; 174 1.1 christos } 175 1.1 christos return 1; 176 1.1 christos } 177 1.1 christos 178 1.1.1.9 christos int 179 1.1.1.9 christos reply_info_can_answer_expired(struct reply_info* rep, time_t timenow) 180 1.1.1.9 christos { 181 1.1.1.9 christos log_assert(rep->ttl < timenow); 182 1.1.1.9 christos /* Really expired */ 183 1.1.1.9 christos if(SERVE_EXPIRED_TTL && rep->serve_expired_ttl < timenow) return 0; 184 1.1.1.9 christos /* Ignore expired failure answers */ 185 1.1.1.9 christos if(FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_NOERROR && 186 1.1.1.9 christos FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_NXDOMAIN && 187 1.1.1.9 christos FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_YXDOMAIN) return 0; 188 1.1.1.9 christos return 1; 189 1.1.1.9 christos } 190 1.1.1.9 christos 191 1.1.1.9 christos int reply_info_could_use_expired(struct reply_info* rep, time_t timenow) 192 1.1.1.9 christos { 193 1.1.1.9 christos log_assert(rep->ttl < timenow); 194 1.1.1.9 christos /* Really expired */ 195 1.1.1.9 christos if(SERVE_EXPIRED_TTL && rep->serve_expired_ttl < timenow && 196 1.1.1.9 christos !SERVE_EXPIRED_TTL_RESET) return 0; 197 1.1.1.9 christos /* Ignore expired failure answers */ 198 1.1.1.9 christos if(FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_NOERROR && 199 1.1.1.9 christos FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_NXDOMAIN && 200 1.1.1.9 christos FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_YXDOMAIN) return 0; 201 1.1.1.9 christos return 1; 202 1.1.1.9 christos } 203 1.1.1.9 christos 204 1.1.1.7 christos struct reply_info * 205 1.1.1.7 christos make_new_reply_info(const struct reply_info* rep, struct regional* region, 206 1.1.1.7 christos size_t an_numrrsets, size_t copy_rrsets) 207 1.1.1.7 christos { 208 1.1.1.7 christos struct reply_info* new_rep; 209 1.1.1.7 christos size_t i; 210 1.1.1.7 christos 211 1.1.1.7 christos /* create a base struct. we specify 'insecure' security status as 212 1.1.1.7 christos * the modified response won't be DNSSEC-valid. In our faked response 213 1.1.1.7 christos * the authority and additional sections will be empty (except possible 214 1.1.1.7 christos * EDNS0 OPT RR in the additional section appended on sending it out), 215 1.1.1.7 christos * so the total number of RRsets is an_numrrsets. */ 216 1.1.1.7 christos new_rep = construct_reply_info_base(region, rep->flags, 217 1.1.1.7 christos rep->qdcount, rep->ttl, rep->prefetch_ttl, 218 1.1.1.9 christos rep->serve_expired_ttl, rep->serve_expired_norec_ttl, 219 1.1.1.9 christos an_numrrsets, 0, 0, an_numrrsets, 220 1.1.1.8 christos sec_status_insecure, LDNS_EDE_NONE); 221 1.1.1.7 christos if(!new_rep) 222 1.1.1.7 christos return NULL; 223 1.1.1.7 christos if(!reply_info_alloc_rrset_keys(new_rep, NULL, region)) 224 1.1.1.7 christos return NULL; 225 1.1.1.7 christos for(i=0; i<copy_rrsets; i++) 226 1.1.1.7 christos new_rep->rrsets[i] = rep->rrsets[i]; 227 1.1.1.7 christos 228 1.1.1.7 christos return new_rep; 229 1.1.1.7 christos } 230 1.1.1.7 christos 231 1.1 christos /** find the minimumttl in the rdata of SOA record */ 232 1.1 christos static time_t 233 1.1 christos soa_find_minttl(struct rr_parse* rr) 234 1.1 christos { 235 1.1 christos uint16_t rlen = sldns_read_uint16(rr->ttl_data+4); 236 1.1 christos if(rlen < 20) 237 1.1 christos return 0; /* rdata too small for SOA (dname, dname, 5*32bit) */ 238 1.1 christos /* minimum TTL is the last 32bit value in the rdata of the record */ 239 1.1 christos /* at position ttl_data + 4(ttl) + 2(rdatalen) + rdatalen - 4(timeval)*/ 240 1.1 christos return (time_t)sldns_read_uint32(rr->ttl_data+6+rlen-4); 241 1.1 christos } 242 1.1 christos 243 1.1 christos /** do the rdata copy */ 244 1.1 christos static int 245 1.1 christos rdata_copy(sldns_buffer* pkt, struct packed_rrset_data* data, uint8_t* to, 246 1.1 christos struct rr_parse* rr, time_t* rr_ttl, uint16_t type, 247 1.1 christos sldns_pkt_section section) 248 1.1 christos { 249 1.1 christos uint16_t pkt_len; 250 1.1 christos const sldns_rr_descriptor* desc; 251 1.1 christos 252 1.1 christos *rr_ttl = sldns_read_uint32(rr->ttl_data); 253 1.1 christos /* RFC 2181 Section 8. if msb of ttl is set treat as if zero. */ 254 1.1.1.10 christos if((*rr_ttl & 0x80000000U)) 255 1.1 christos *rr_ttl = 0; 256 1.1 christos if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) { 257 1.1 christos /* negative response. see if TTL of SOA record larger than the 258 1.1 christos * minimum-ttl in the rdata of the SOA record */ 259 1.1.1.9 christos if(*rr_ttl > soa_find_minttl(rr)) *rr_ttl = soa_find_minttl(rr); 260 1.1.1.9 christos if(!SERVE_ORIGINAL_TTL) { 261 1.1.1.9 christos /* If MIN_NEG_TTL is configured skip setting MIN_TTL */ 262 1.1.1.9 christos if(MIN_NEG_TTL <= 0 && *rr_ttl < MIN_TTL) { 263 1.1.1.9 christos *rr_ttl = MIN_TTL; 264 1.1.1.9 christos } 265 1.1.1.9 christos if(*rr_ttl > MAX_TTL) *rr_ttl = MAX_TTL; 266 1.1.1.9 christos } 267 1.1.1.9 christos /* MAX_NEG_TTL overrides the min and max ttl of everything 268 1.1.1.9 christos * else; it is for a more specific record */ 269 1.1.1.9 christos if(*rr_ttl > MAX_NEG_TTL) *rr_ttl = MAX_NEG_TTL; 270 1.1.1.9 christos /* MIN_NEG_TTL overrides the min and max ttl of everything 271 1.1.1.9 christos * else if configured; it is for a more specific record */ 272 1.1.1.9 christos if(MIN_NEG_TTL > 0 && *rr_ttl < MIN_NEG_TTL) { 273 1.1.1.9 christos *rr_ttl = MIN_NEG_TTL; 274 1.1.1.9 christos } 275 1.1.1.9 christos } else if(!SERVE_ORIGINAL_TTL) { 276 1.1.1.9 christos if(*rr_ttl < MIN_TTL) *rr_ttl = MIN_TTL; 277 1.1.1.9 christos if(*rr_ttl > MAX_TTL) *rr_ttl = MAX_TTL; 278 1.1.1.7 christos } 279 1.1 christos if(*rr_ttl < data->ttl) 280 1.1 christos data->ttl = *rr_ttl; 281 1.1 christos 282 1.1 christos if(rr->outside_packet) { 283 1.1 christos /* uncompressed already, only needs copy */ 284 1.1 christos memmove(to, rr->ttl_data+sizeof(uint32_t), rr->size); 285 1.1 christos return 1; 286 1.1 christos } 287 1.1 christos 288 1.1 christos sldns_buffer_set_position(pkt, (size_t) 289 1.1 christos (rr->ttl_data - sldns_buffer_begin(pkt) + sizeof(uint32_t))); 290 1.1 christos /* insert decompressed size into rdata len stored in memory */ 291 1.1 christos /* -2 because rdatalen bytes are not included. */ 292 1.1 christos pkt_len = htons(rr->size - 2); 293 1.1 christos memmove(to, &pkt_len, sizeof(uint16_t)); 294 1.1 christos to += 2; 295 1.1 christos /* read packet rdata len */ 296 1.1 christos pkt_len = sldns_buffer_read_u16(pkt); 297 1.1 christos if(sldns_buffer_remaining(pkt) < pkt_len) 298 1.1 christos return 0; 299 1.1 christos desc = sldns_rr_descript(type); 300 1.1 christos if(pkt_len > 0 && desc && desc->_dname_count > 0) { 301 1.1 christos int count = (int)desc->_dname_count; 302 1.1 christos int rdf = 0; 303 1.1 christos size_t len; 304 1.1 christos size_t oldpos; 305 1.1 christos /* decompress dnames. */ 306 1.1 christos while(pkt_len > 0 && count) { 307 1.1 christos switch(desc->_wireformat[rdf]) { 308 1.1 christos case LDNS_RDF_TYPE_DNAME: 309 1.1 christos oldpos = sldns_buffer_position(pkt); 310 1.1 christos dname_pkt_copy(pkt, to, 311 1.1 christos sldns_buffer_current(pkt)); 312 1.1 christos to += pkt_dname_len(pkt); 313 1.1 christos pkt_len -= sldns_buffer_position(pkt)-oldpos; 314 1.1 christos count--; 315 1.1 christos len = 0; 316 1.1 christos break; 317 1.1 christos case LDNS_RDF_TYPE_STR: 318 1.1 christos len = sldns_buffer_current(pkt)[0] + 1; 319 1.1 christos break; 320 1.1 christos default: 321 1.1 christos len = get_rdf_size(desc->_wireformat[rdf]); 322 1.1 christos break; 323 1.1 christos } 324 1.1 christos if(len) { 325 1.1.1.5 christos log_assert(len <= pkt_len); 326 1.1 christos memmove(to, sldns_buffer_current(pkt), len); 327 1.1 christos to += len; 328 1.1 christos sldns_buffer_skip(pkt, (ssize_t)len); 329 1.1 christos pkt_len -= len; 330 1.1 christos } 331 1.1 christos rdf++; 332 1.1 christos } 333 1.1 christos } 334 1.1 christos /* copy remaining rdata */ 335 1.1 christos if(pkt_len > 0) 336 1.1 christos memmove(to, sldns_buffer_current(pkt), pkt_len); 337 1.1 christos 338 1.1 christos return 1; 339 1.1 christos } 340 1.1 christos 341 1.1 christos /** copy over the data into packed rrset */ 342 1.1 christos static int 343 1.1 christos parse_rr_copy(sldns_buffer* pkt, struct rrset_parse* pset, 344 1.1 christos struct packed_rrset_data* data) 345 1.1 christos { 346 1.1 christos size_t i; 347 1.1 christos struct rr_parse* rr = pset->rr_first; 348 1.1 christos uint8_t* nextrdata; 349 1.1 christos size_t total = pset->rr_count + pset->rrsig_count; 350 1.1 christos data->ttl = MAX_TTL; 351 1.1 christos data->count = pset->rr_count; 352 1.1 christos data->rrsig_count = pset->rrsig_count; 353 1.1 christos data->trust = rrset_trust_none; 354 1.1 christos data->security = sec_status_unchecked; 355 1.1 christos /* layout: struct - rr_len - rr_data - rr_ttl - rdata - rrsig */ 356 1.1 christos data->rr_len = (size_t*)((uint8_t*)data + 357 1.1 christos sizeof(struct packed_rrset_data)); 358 1.1 christos data->rr_data = (uint8_t**)&(data->rr_len[total]); 359 1.1 christos data->rr_ttl = (time_t*)&(data->rr_data[total]); 360 1.1 christos nextrdata = (uint8_t*)&(data->rr_ttl[total]); 361 1.1 christos for(i=0; i<data->count; i++) { 362 1.1 christos data->rr_len[i] = rr->size; 363 1.1 christos data->rr_data[i] = nextrdata; 364 1.1 christos nextrdata += rr->size; 365 1.1 christos if(!rdata_copy(pkt, data, data->rr_data[i], rr, 366 1.1 christos &data->rr_ttl[i], pset->type, pset->section)) 367 1.1 christos return 0; 368 1.1 christos rr = rr->next; 369 1.1 christos } 370 1.1 christos /* if rrsig, its rdata is at nextrdata */ 371 1.1 christos rr = pset->rrsig_first; 372 1.1 christos for(i=data->count; i<total; i++) { 373 1.1 christos data->rr_len[i] = rr->size; 374 1.1 christos data->rr_data[i] = nextrdata; 375 1.1 christos nextrdata += rr->size; 376 1.1 christos if(!rdata_copy(pkt, data, data->rr_data[i], rr, 377 1.1 christos &data->rr_ttl[i], LDNS_RR_TYPE_RRSIG, pset->section)) 378 1.1 christos return 0; 379 1.1 christos rr = rr->next; 380 1.1 christos } 381 1.1 christos return 1; 382 1.1 christos } 383 1.1 christos 384 1.1 christos /** create rrset return 0 on failure */ 385 1.1 christos static int 386 1.1 christos parse_create_rrset(sldns_buffer* pkt, struct rrset_parse* pset, 387 1.1 christos struct packed_rrset_data** data, struct regional* region) 388 1.1 christos { 389 1.1 christos /* allocate */ 390 1.1 christos size_t s; 391 1.1 christos if(pset->rr_count > RR_COUNT_MAX || pset->rrsig_count > RR_COUNT_MAX || 392 1.1 christos pset->size > RR_COUNT_MAX) 393 1.1 christos return 0; /* protect against integer overflow */ 394 1.1 christos s = sizeof(struct packed_rrset_data) + 395 1.1 christos (pset->rr_count + pset->rrsig_count) * 396 1.1 christos (sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t)) + 397 1.1 christos pset->size; 398 1.1 christos if(region) 399 1.1.1.6 christos *data = regional_alloc_zero(region, s); 400 1.1.1.6 christos else *data = calloc(1, s); 401 1.1 christos if(!*data) 402 1.1 christos return 0; 403 1.1 christos /* copy & decompress */ 404 1.1 christos if(!parse_rr_copy(pkt, pset, *data)) { 405 1.1.1.7 christos if(!region) { 406 1.1.1.7 christos free(*data); 407 1.1.1.7 christos *data = NULL; 408 1.1.1.7 christos } 409 1.1 christos return 0; 410 1.1 christos } 411 1.1 christos return 1; 412 1.1 christos } 413 1.1 christos 414 1.1 christos /** get trust value for rrset */ 415 1.1 christos static enum rrset_trust 416 1.1 christos get_rrset_trust(struct msg_parse* msg, struct rrset_parse* rrset) 417 1.1 christos { 418 1.1 christos uint16_t AA = msg->flags & BIT_AA; 419 1.1 christos if(rrset->section == LDNS_SECTION_ANSWER) { 420 1.1 christos if(AA) { 421 1.1 christos /* RFC2181 says remainder of CNAME chain is nonauth*/ 422 1.1 christos if(msg->rrset_first && 423 1.1 christos msg->rrset_first->section==LDNS_SECTION_ANSWER 424 1.1 christos && msg->rrset_first->type==LDNS_RR_TYPE_CNAME){ 425 1.1 christos if(rrset == msg->rrset_first) 426 1.1 christos return rrset_trust_ans_AA; 427 1.1 christos else return rrset_trust_ans_noAA; 428 1.1 christos } 429 1.1 christos if(msg->rrset_first && 430 1.1 christos msg->rrset_first->section==LDNS_SECTION_ANSWER 431 1.1 christos && msg->rrset_first->type==LDNS_RR_TYPE_DNAME){ 432 1.1 christos if(rrset == msg->rrset_first || 433 1.1 christos rrset == msg->rrset_first->rrset_all_next) 434 1.1 christos return rrset_trust_ans_AA; 435 1.1 christos else return rrset_trust_ans_noAA; 436 1.1 christos } 437 1.1 christos return rrset_trust_ans_AA; 438 1.1 christos } 439 1.1 christos else return rrset_trust_ans_noAA; 440 1.1 christos } else if(rrset->section == LDNS_SECTION_AUTHORITY) { 441 1.1 christos if(AA) return rrset_trust_auth_AA; 442 1.1 christos else return rrset_trust_auth_noAA; 443 1.1 christos } else { 444 1.1 christos /* addit section */ 445 1.1 christos if(AA) return rrset_trust_add_AA; 446 1.1 christos else return rrset_trust_add_noAA; 447 1.1 christos } 448 1.1 christos /* NOTREACHED */ 449 1.1 christos return rrset_trust_none; 450 1.1 christos } 451 1.1 christos 452 1.1 christos int 453 1.1 christos parse_copy_decompress_rrset(sldns_buffer* pkt, struct msg_parse* msg, 454 1.1 christos struct rrset_parse *pset, struct regional* region, 455 1.1 christos struct ub_packed_rrset_key* pk) 456 1.1 christos { 457 1.1 christos struct packed_rrset_data* data; 458 1.1 christos pk->rk.flags = pset->flags; 459 1.1 christos pk->rk.dname_len = pset->dname_len; 460 1.1 christos if(region) 461 1.1 christos pk->rk.dname = (uint8_t*)regional_alloc( 462 1.1 christos region, pset->dname_len); 463 1.1 christos else pk->rk.dname = 464 1.1 christos (uint8_t*)malloc(pset->dname_len); 465 1.1 christos if(!pk->rk.dname) 466 1.1 christos return 0; 467 1.1 christos /** copy & decompress dname */ 468 1.1 christos dname_pkt_copy(pkt, pk->rk.dname, pset->dname); 469 1.1 christos /** copy over type and class */ 470 1.1 christos pk->rk.type = htons(pset->type); 471 1.1 christos pk->rk.rrset_class = pset->rrset_class; 472 1.1 christos /** read data part. */ 473 1.1.1.7 christos if(!parse_create_rrset(pkt, pset, &data, region)) { 474 1.1.1.7 christos if(!region) { 475 1.1.1.7 christos free(pk->rk.dname); 476 1.1.1.7 christos pk->rk.dname = NULL; 477 1.1.1.7 christos } 478 1.1 christos return 0; 479 1.1.1.7 christos } 480 1.1 christos pk->entry.data = (void*)data; 481 1.1 christos pk->entry.key = (void*)pk; 482 1.1 christos pk->entry.hash = pset->hash; 483 1.1 christos data->trust = get_rrset_trust(msg, pset); 484 1.1 christos return 1; 485 1.1 christos } 486 1.1 christos 487 1.1 christos /** 488 1.1 christos * Copy and decompress rrs 489 1.1 christos * @param pkt: the packet for compression pointer resolution. 490 1.1 christos * @param msg: the parsed message 491 1.1 christos * @param rep: reply info to put rrs into. 492 1.1 christos * @param region: if not NULL, used for allocation. 493 1.1 christos * @return 0 on failure. 494 1.1 christos */ 495 1.1 christos static int 496 1.1 christos parse_copy_decompress(sldns_buffer* pkt, struct msg_parse* msg, 497 1.1 christos struct reply_info* rep, struct regional* region) 498 1.1 christos { 499 1.1 christos size_t i; 500 1.1 christos struct rrset_parse *pset = msg->rrset_first; 501 1.1 christos struct packed_rrset_data* data; 502 1.1 christos log_assert(rep); 503 1.1 christos rep->ttl = MAX_TTL; 504 1.1 christos rep->security = sec_status_unchecked; 505 1.1 christos if(rep->rrset_count == 0) 506 1.1 christos rep->ttl = NORR_TTL; 507 1.1 christos 508 1.1 christos for(i=0; i<rep->rrset_count; i++) { 509 1.1 christos if(!parse_copy_decompress_rrset(pkt, msg, pset, region, 510 1.1 christos rep->rrsets[i])) 511 1.1 christos return 0; 512 1.1 christos data = (struct packed_rrset_data*)rep->rrsets[i]->entry.data; 513 1.1 christos if(data->ttl < rep->ttl) 514 1.1 christos rep->ttl = data->ttl; 515 1.1 christos 516 1.1 christos pset = pset->rrset_all_next; 517 1.1 christos } 518 1.1 christos rep->prefetch_ttl = PREFETCH_TTL_CALC(rep->ttl); 519 1.1.1.4 christos rep->serve_expired_ttl = rep->ttl + SERVE_EXPIRED_TTL; 520 1.1.1.9 christos /* rep->serve_expired_norec_ttl should stay at 0 */ 521 1.1.1.9 christos log_assert(rep->serve_expired_norec_ttl == 0); 522 1.1 christos return 1; 523 1.1 christos } 524 1.1 christos 525 1.1 christos int 526 1.1 christos parse_create_msg(sldns_buffer* pkt, struct msg_parse* msg, 527 1.1 christos struct alloc_cache* alloc, struct query_info* qinf, 528 1.1 christos struct reply_info** rep, struct regional* region) 529 1.1 christos { 530 1.1 christos log_assert(pkt && msg); 531 1.1 christos if(!parse_create_qinfo(pkt, msg, qinf, region)) 532 1.1 christos return 0; 533 1.1 christos if(!parse_create_repinfo(msg, rep, region)) 534 1.1 christos return 0; 535 1.1.1.4 christos if(!reply_info_alloc_rrset_keys(*rep, alloc, region)) { 536 1.1.1.4 christos if(!region) reply_info_parsedelete(*rep, alloc); 537 1.1 christos return 0; 538 1.1.1.4 christos } 539 1.1.1.4 christos if(!parse_copy_decompress(pkt, msg, *rep, region)) { 540 1.1.1.4 christos if(!region) reply_info_parsedelete(*rep, alloc); 541 1.1 christos return 0; 542 1.1.1.4 christos } 543 1.1 christos return 1; 544 1.1 christos } 545 1.1 christos 546 1.1 christos int reply_info_parse(sldns_buffer* pkt, struct alloc_cache* alloc, 547 1.1 christos struct query_info* qinf, struct reply_info** rep, 548 1.1 christos struct regional* region, struct edns_data* edns) 549 1.1 christos { 550 1.1 christos /* use scratch pad region-allocator during parsing. */ 551 1.1 christos struct msg_parse* msg; 552 1.1 christos int ret; 553 1.1 christos 554 1.1 christos qinf->qname = NULL; 555 1.1.1.2 christos qinf->local_alias = NULL; 556 1.1 christos *rep = NULL; 557 1.1 christos if(!(msg = regional_alloc(region, sizeof(*msg)))) { 558 1.1 christos return LDNS_RCODE_SERVFAIL; 559 1.1 christos } 560 1.1 christos memset(msg, 0, sizeof(*msg)); 561 1.1 christos 562 1.1 christos sldns_buffer_set_position(pkt, 0); 563 1.1 christos if((ret = parse_packet(pkt, msg, region)) != 0) { 564 1.1 christos return ret; 565 1.1 christos } 566 1.1.1.7 christos if((ret = parse_extract_edns_from_response_msg(msg, edns, region)) != 0) 567 1.1 christos return ret; 568 1.1 christos 569 1.1 christos /* parse OK, allocate return structures */ 570 1.1 christos /* this also performs dname decompression */ 571 1.1 christos if(!parse_create_msg(pkt, msg, alloc, qinf, rep, NULL)) { 572 1.1 christos query_info_clear(qinf); 573 1.1 christos *rep = NULL; 574 1.1 christos return LDNS_RCODE_SERVFAIL; 575 1.1 christos } 576 1.1 christos return 0; 577 1.1 christos } 578 1.1 christos 579 1.1 christos /** helper compare function to sort in lock order */ 580 1.1 christos static int 581 1.1 christos reply_info_sortref_cmp(const void* a, const void* b) 582 1.1 christos { 583 1.1 christos struct rrset_ref* x = (struct rrset_ref*)a; 584 1.1 christos struct rrset_ref* y = (struct rrset_ref*)b; 585 1.1 christos if(x->key < y->key) return -1; 586 1.1 christos if(x->key > y->key) return 1; 587 1.1 christos return 0; 588 1.1 christos } 589 1.1 christos 590 1.1 christos void 591 1.1 christos reply_info_sortref(struct reply_info* rep) 592 1.1 christos { 593 1.1 christos qsort(&rep->ref[0], rep->rrset_count, sizeof(struct rrset_ref), 594 1.1 christos reply_info_sortref_cmp); 595 1.1 christos } 596 1.1 christos 597 1.1 christos void 598 1.1 christos reply_info_set_ttls(struct reply_info* rep, time_t timenow) 599 1.1 christos { 600 1.1 christos size_t i, j; 601 1.1 christos rep->ttl += timenow; 602 1.1 christos rep->prefetch_ttl += timenow; 603 1.1.1.4 christos rep->serve_expired_ttl += timenow; 604 1.1.1.9 christos /* Don't set rep->serve_expired_norec_ttl; this should only be set 605 1.1.1.9 christos * on cached records when encountering an error */ 606 1.1.1.9 christos log_assert(rep->serve_expired_norec_ttl == 0); 607 1.1 christos for(i=0; i<rep->rrset_count; i++) { 608 1.1 christos struct packed_rrset_data* data = (struct packed_rrset_data*) 609 1.1 christos rep->ref[i].key->entry.data; 610 1.1 christos if(i>0 && rep->ref[i].key == rep->ref[i-1].key) 611 1.1 christos continue; 612 1.1 christos data->ttl += timenow; 613 1.1 christos for(j=0; j<data->count + data->rrsig_count; j++) { 614 1.1 christos data->rr_ttl[j] += timenow; 615 1.1 christos } 616 1.1.1.6 christos data->ttl_add = timenow; 617 1.1 christos } 618 1.1 christos } 619 1.1 christos 620 1.1 christos void 621 1.1 christos reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc) 622 1.1 christos { 623 1.1 christos size_t i; 624 1.1 christos if(!rep) 625 1.1 christos return; 626 1.1 christos /* no need to lock, since not shared in hashtables. */ 627 1.1 christos for(i=0; i<rep->rrset_count; i++) { 628 1.1 christos ub_packed_rrset_parsedelete(rep->rrsets[i], alloc); 629 1.1 christos } 630 1.1.1.8 christos if(rep->reason_bogus_str) { 631 1.1.1.8 christos free(rep->reason_bogus_str); 632 1.1.1.8 christos rep->reason_bogus_str = NULL; 633 1.1.1.8 christos } 634 1.1 christos free(rep); 635 1.1 christos } 636 1.1 christos 637 1.1 christos int 638 1.1 christos query_info_parse(struct query_info* m, sldns_buffer* query) 639 1.1 christos { 640 1.1 christos uint8_t* q = sldns_buffer_begin(query); 641 1.1 christos /* minimum size: header + \0 + qtype + qclass */ 642 1.1 christos if(sldns_buffer_limit(query) < LDNS_HEADER_SIZE + 5) 643 1.1 christos return 0; 644 1.1.1.3 christos if((LDNS_OPCODE_WIRE(q) != LDNS_PACKET_QUERY && LDNS_OPCODE_WIRE(q) != 645 1.1.1.3 christos LDNS_PACKET_NOTIFY) || LDNS_QDCOUNT(q) != 1 || 646 1.1.1.3 christos sldns_buffer_position(query) != 0) 647 1.1 christos return 0; 648 1.1 christos sldns_buffer_skip(query, LDNS_HEADER_SIZE); 649 1.1 christos m->qname = sldns_buffer_current(query); 650 1.1 christos if((m->qname_len = query_dname_len(query)) == 0) 651 1.1 christos return 0; /* parse error */ 652 1.1 christos if(sldns_buffer_remaining(query) < 4) 653 1.1 christos return 0; /* need qtype, qclass */ 654 1.1 christos m->qtype = sldns_buffer_read_u16(query); 655 1.1 christos m->qclass = sldns_buffer_read_u16(query); 656 1.1.1.2 christos m->local_alias = NULL; 657 1.1 christos return 1; 658 1.1 christos } 659 1.1 christos 660 1.1 christos /** tiny subroutine for msgreply_compare */ 661 1.1 christos #define COMPARE_IT(x, y) \ 662 1.1 christos if( (x) < (y) ) return -1; \ 663 1.1 christos else if( (x) > (y) ) return +1; \ 664 1.1 christos log_assert( (x) == (y) ); 665 1.1 christos 666 1.1 christos int 667 1.1 christos query_info_compare(void* m1, void* m2) 668 1.1 christos { 669 1.1 christos struct query_info* msg1 = (struct query_info*)m1; 670 1.1 christos struct query_info* msg2 = (struct query_info*)m2; 671 1.1 christos int mc; 672 1.1 christos /* from most different to least different for speed */ 673 1.1 christos COMPARE_IT(msg1->qtype, msg2->qtype); 674 1.1 christos if((mc = query_dname_compare(msg1->qname, msg2->qname)) != 0) 675 1.1 christos return mc; 676 1.1 christos log_assert(msg1->qname_len == msg2->qname_len); 677 1.1 christos COMPARE_IT(msg1->qclass, msg2->qclass); 678 1.1 christos return 0; 679 1.1 christos #undef COMPARE_IT 680 1.1 christos } 681 1.1 christos 682 1.1 christos void 683 1.1 christos query_info_clear(struct query_info* m) 684 1.1 christos { 685 1.1 christos free(m->qname); 686 1.1 christos m->qname = NULL; 687 1.1 christos } 688 1.1 christos 689 1.1 christos size_t 690 1.1 christos msgreply_sizefunc(void* k, void* d) 691 1.1 christos { 692 1.1 christos struct msgreply_entry* q = (struct msgreply_entry*)k; 693 1.1 christos struct reply_info* r = (struct reply_info*)d; 694 1.1 christos size_t s = sizeof(struct msgreply_entry) + sizeof(struct reply_info) 695 1.1 christos + q->key.qname_len + lock_get_mem(&q->entry.lock) 696 1.1 christos - sizeof(struct rrset_ref); 697 1.1 christos s += r->rrset_count * sizeof(struct rrset_ref); 698 1.1 christos s += r->rrset_count * sizeof(struct ub_packed_rrset_key*); 699 1.1 christos return s; 700 1.1 christos } 701 1.1 christos 702 1.1 christos void 703 1.1 christos query_entry_delete(void *k, void* ATTR_UNUSED(arg)) 704 1.1 christos { 705 1.1 christos struct msgreply_entry* q = (struct msgreply_entry*)k; 706 1.1 christos lock_rw_destroy(&q->entry.lock); 707 1.1 christos query_info_clear(&q->key); 708 1.1 christos free(q); 709 1.1 christos } 710 1.1 christos 711 1.1 christos void 712 1.1 christos reply_info_delete(void* d, void* ATTR_UNUSED(arg)) 713 1.1 christos { 714 1.1 christos struct reply_info* r = (struct reply_info*)d; 715 1.1.1.8 christos if(r->reason_bogus_str) { 716 1.1.1.8 christos free(r->reason_bogus_str); 717 1.1.1.8 christos r->reason_bogus_str = NULL; 718 1.1.1.8 christos } 719 1.1 christos free(r); 720 1.1 christos } 721 1.1 christos 722 1.1.1.2 christos hashvalue_type 723 1.1 christos query_info_hash(struct query_info *q, uint16_t flags) 724 1.1 christos { 725 1.1.1.2 christos hashvalue_type h = 0xab; 726 1.1 christos h = hashlittle(&q->qtype, sizeof(q->qtype), h); 727 1.1 christos if(q->qtype == LDNS_RR_TYPE_AAAA && (flags&BIT_CD)) 728 1.1 christos h++; 729 1.1 christos h = hashlittle(&q->qclass, sizeof(q->qclass), h); 730 1.1 christos h = dname_query_hash(q->qname, h); 731 1.1 christos return h; 732 1.1 christos } 733 1.1 christos 734 1.1 christos struct msgreply_entry* 735 1.1 christos query_info_entrysetup(struct query_info* q, struct reply_info* r, 736 1.1.1.2 christos hashvalue_type h) 737 1.1 christos { 738 1.1 christos struct msgreply_entry* e = (struct msgreply_entry*)malloc( 739 1.1 christos sizeof(struct msgreply_entry)); 740 1.1 christos if(!e) return NULL; 741 1.1 christos memcpy(&e->key, q, sizeof(*q)); 742 1.1 christos e->entry.hash = h; 743 1.1 christos e->entry.key = e; 744 1.1 christos e->entry.data = r; 745 1.1 christos lock_rw_init(&e->entry.lock); 746 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qname, sizeof(e->key.qname)); 747 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qname_len, sizeof(e->key.qname_len)); 748 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qtype, sizeof(e->key.qtype)); 749 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qclass, sizeof(e->key.qclass)); 750 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.local_alias, sizeof(e->key.local_alias)); 751 1.1.1.3 christos lock_protect(&e->entry.lock, &e->entry.hash, sizeof(e->entry.hash)); 752 1.1.1.3 christos lock_protect(&e->entry.lock, &e->entry.key, sizeof(e->entry.key)); 753 1.1.1.3 christos lock_protect(&e->entry.lock, &e->entry.data, sizeof(e->entry.data)); 754 1.1 christos lock_protect(&e->entry.lock, e->key.qname, e->key.qname_len); 755 1.1 christos q->qname = NULL; 756 1.1 christos return e; 757 1.1 christos } 758 1.1 christos 759 1.1 christos /** copy rrsets from replyinfo to dest replyinfo */ 760 1.1 christos static int 761 1.1 christos repinfo_copy_rrsets(struct reply_info* dest, struct reply_info* from, 762 1.1 christos struct regional* region) 763 1.1 christos { 764 1.1 christos size_t i, s; 765 1.1 christos struct packed_rrset_data* fd, *dd; 766 1.1 christos struct ub_packed_rrset_key* fk, *dk; 767 1.1 christos for(i=0; i<dest->rrset_count; i++) { 768 1.1 christos fk = from->rrsets[i]; 769 1.1 christos dk = dest->rrsets[i]; 770 1.1 christos fd = (struct packed_rrset_data*)fk->entry.data; 771 1.1 christos dk->entry.hash = fk->entry.hash; 772 1.1 christos dk->rk = fk->rk; 773 1.1 christos if(region) { 774 1.1 christos dk->id = fk->id; 775 1.1 christos dk->rk.dname = (uint8_t*)regional_alloc_init(region, 776 1.1 christos fk->rk.dname, fk->rk.dname_len); 777 1.1 christos } else 778 1.1 christos dk->rk.dname = (uint8_t*)memdup(fk->rk.dname, 779 1.1 christos fk->rk.dname_len); 780 1.1 christos if(!dk->rk.dname) 781 1.1 christos return 0; 782 1.1 christos s = packed_rrset_sizeof(fd); 783 1.1 christos if(region) 784 1.1 christos dd = (struct packed_rrset_data*)regional_alloc_init( 785 1.1 christos region, fd, s); 786 1.1 christos else dd = (struct packed_rrset_data*)memdup(fd, s); 787 1.1 christos if(!dd) 788 1.1 christos return 0; 789 1.1 christos packed_rrset_ptr_fixup(dd); 790 1.1 christos dk->entry.data = (void*)dd; 791 1.1 christos } 792 1.1 christos return 1; 793 1.1 christos } 794 1.1 christos 795 1.1.1.8 christos struct reply_info* 796 1.1.1.8 christos reply_info_copy(struct reply_info* rep, struct alloc_cache* alloc, 797 1.1 christos struct regional* region) 798 1.1 christos { 799 1.1 christos struct reply_info* cp; 800 1.1.1.8 christos cp = construct_reply_info_base(region, rep->flags, rep->qdcount, 801 1.1.1.8 christos rep->ttl, rep->prefetch_ttl, rep->serve_expired_ttl, 802 1.1.1.9 christos rep->serve_expired_norec_ttl, 803 1.1.1.4 christos rep->an_numrrsets, rep->ns_numrrsets, rep->ar_numrrsets, 804 1.1.1.8 christos rep->rrset_count, rep->security, rep->reason_bogus); 805 1.1 christos if(!cp) 806 1.1 christos return NULL; 807 1.1.1.8 christos 808 1.1.1.8 christos if(rep->reason_bogus_str && *rep->reason_bogus_str != 0) { 809 1.1.1.8 christos if(region) { 810 1.1.1.8 christos cp->reason_bogus_str = (char*)regional_alloc(region, 811 1.1.1.8 christos sizeof(char) 812 1.1.1.8 christos * (strlen(rep->reason_bogus_str)+1)); 813 1.1.1.8 christos } else { 814 1.1.1.8 christos cp->reason_bogus_str = malloc(sizeof(char) 815 1.1.1.8 christos * (strlen(rep->reason_bogus_str)+1)); 816 1.1.1.8 christos } 817 1.1.1.8 christos if(!cp->reason_bogus_str) { 818 1.1.1.8 christos if(!region) 819 1.1.1.8 christos reply_info_parsedelete(cp, alloc); 820 1.1.1.8 christos return NULL; 821 1.1.1.8 christos } 822 1.1.1.8 christos memcpy(cp->reason_bogus_str, rep->reason_bogus_str, 823 1.1.1.8 christos strlen(rep->reason_bogus_str)+1); 824 1.1.1.8 christos } 825 1.1.1.8 christos 826 1.1 christos /* allocate ub_key structures special or not */ 827 1.1.1.2 christos if(!reply_info_alloc_rrset_keys(cp, alloc, region)) { 828 1.1 christos if(!region) 829 1.1 christos reply_info_parsedelete(cp, alloc); 830 1.1 christos return NULL; 831 1.1 christos } 832 1.1 christos if(!repinfo_copy_rrsets(cp, rep, region)) { 833 1.1 christos if(!region) 834 1.1 christos reply_info_parsedelete(cp, alloc); 835 1.1 christos return NULL; 836 1.1 christos } 837 1.1 christos return cp; 838 1.1 christos } 839 1.1 christos 840 1.1 christos uint8_t* 841 1.1 christos reply_find_final_cname_target(struct query_info* qinfo, struct reply_info* rep) 842 1.1 christos { 843 1.1 christos uint8_t* sname = qinfo->qname; 844 1.1 christos size_t snamelen = qinfo->qname_len; 845 1.1 christos size_t i; 846 1.1 christos for(i=0; i<rep->an_numrrsets; i++) { 847 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i]; 848 1.1 christos /* follow CNAME chain (if any) */ 849 1.1 christos if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME && 850 1.1 christos ntohs(s->rk.rrset_class) == qinfo->qclass && 851 1.1 christos snamelen == s->rk.dname_len && 852 1.1 christos query_dname_compare(sname, s->rk.dname) == 0) { 853 1.1 christos get_cname_target(s, &sname, &snamelen); 854 1.1 christos } 855 1.1 christos } 856 1.1 christos if(sname != qinfo->qname) 857 1.1 christos return sname; 858 1.1 christos return NULL; 859 1.1 christos } 860 1.1 christos 861 1.1 christos struct ub_packed_rrset_key* 862 1.1 christos reply_find_answer_rrset(struct query_info* qinfo, struct reply_info* rep) 863 1.1 christos { 864 1.1 christos uint8_t* sname = qinfo->qname; 865 1.1 christos size_t snamelen = qinfo->qname_len; 866 1.1 christos size_t i; 867 1.1 christos for(i=0; i<rep->an_numrrsets; i++) { 868 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i]; 869 1.1 christos /* first match type, for query of qtype cname */ 870 1.1 christos if(ntohs(s->rk.type) == qinfo->qtype && 871 1.1 christos ntohs(s->rk.rrset_class) == qinfo->qclass && 872 1.1 christos snamelen == s->rk.dname_len && 873 1.1 christos query_dname_compare(sname, s->rk.dname) == 0) { 874 1.1 christos return s; 875 1.1 christos } 876 1.1 christos /* follow CNAME chain (if any) */ 877 1.1 christos if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME && 878 1.1 christos ntohs(s->rk.rrset_class) == qinfo->qclass && 879 1.1 christos snamelen == s->rk.dname_len && 880 1.1 christos query_dname_compare(sname, s->rk.dname) == 0) { 881 1.1 christos get_cname_target(s, &sname, &snamelen); 882 1.1 christos } 883 1.1 christos } 884 1.1 christos return NULL; 885 1.1 christos } 886 1.1 christos 887 1.1 christos struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep, 888 1.1 christos uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass) 889 1.1 christos { 890 1.1 christos size_t i; 891 1.1 christos for(i=0; i<rep->an_numrrsets; i++) { 892 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i]; 893 1.1 christos if(ntohs(s->rk.type) == type && 894 1.1 christos ntohs(s->rk.rrset_class) == dclass && 895 1.1 christos namelen == s->rk.dname_len && 896 1.1 christos query_dname_compare(name, s->rk.dname) == 0) { 897 1.1 christos return s; 898 1.1 christos } 899 1.1 christos } 900 1.1 christos return NULL; 901 1.1 christos } 902 1.1 christos 903 1.1 christos struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep, 904 1.1 christos uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass) 905 1.1 christos { 906 1.1 christos size_t i; 907 1.1 christos for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 908 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i]; 909 1.1 christos if(ntohs(s->rk.type) == type && 910 1.1 christos ntohs(s->rk.rrset_class) == dclass && 911 1.1 christos namelen == s->rk.dname_len && 912 1.1 christos query_dname_compare(name, s->rk.dname) == 0) { 913 1.1 christos return s; 914 1.1 christos } 915 1.1 christos } 916 1.1 christos return NULL; 917 1.1 christos } 918 1.1 christos 919 1.1 christos struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep, 920 1.1 christos uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass) 921 1.1 christos { 922 1.1 christos size_t i; 923 1.1 christos for(i=0; i<rep->rrset_count; i++) { 924 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i]; 925 1.1 christos if(ntohs(s->rk.type) == type && 926 1.1 christos ntohs(s->rk.rrset_class) == dclass && 927 1.1 christos namelen == s->rk.dname_len && 928 1.1 christos query_dname_compare(name, s->rk.dname) == 0) { 929 1.1 christos return s; 930 1.1 christos } 931 1.1 christos } 932 1.1 christos return NULL; 933 1.1 christos } 934 1.1 christos 935 1.1 christos void 936 1.1 christos log_dns_msg(const char* str, struct query_info* qinfo, struct reply_info* rep) 937 1.1 christos { 938 1.1 christos /* not particularly fast but flexible, make wireformat and print */ 939 1.1 christos sldns_buffer* buf = sldns_buffer_new(65535); 940 1.1 christos struct regional* region = regional_create(); 941 1.1.1.7 christos if(!(buf && region)) { 942 1.1.1.7 christos log_err("%s: log_dns_msg: out of memory", str); 943 1.1.1.7 christos sldns_buffer_free(buf); 944 1.1.1.7 christos regional_destroy(region); 945 1.1.1.7 christos return; 946 1.1.1.7 christos } 947 1.1.1.7 christos if(!reply_info_encode(qinfo, rep, 0, rep->flags, buf, 0, 948 1.1.1.5 christos region, 65535, 1, 0)) { 949 1.1.1.7 christos log_err("%s: log_dns_msg: out of memory", str); 950 1.1 christos } else { 951 1.1 christos char* s = sldns_wire2str_pkt(sldns_buffer_begin(buf), 952 1.1 christos sldns_buffer_limit(buf)); 953 1.1 christos if(!s) { 954 1.1 christos log_info("%s: log_dns_msg: ldns tostr failed", str); 955 1.1 christos } else { 956 1.1 christos log_info("%s %s", str, s); 957 1.1 christos } 958 1.1 christos free(s); 959 1.1 christos } 960 1.1 christos sldns_buffer_free(buf); 961 1.1 christos regional_destroy(region); 962 1.1 christos } 963 1.1 christos 964 1.1.1.2 christos void 965 1.1.1.2 christos log_reply_info(enum verbosity_value v, struct query_info *qinf, 966 1.1.1.2 christos struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur, 967 1.1.1.9 christos int cached, struct sldns_buffer *rmsg, struct sockaddr_storage* daddr, 968 1.1.1.9 christos enum comm_point_type tp, void* ssl) 969 1.1.1.2 christos { 970 1.1.1.2 christos char clientip_buf[128]; 971 1.1.1.2 christos char rcode_buf[16]; 972 1.1.1.9 christos char dest_buf[160]; 973 1.1.1.2 christos uint16_t rcode = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(rmsg, 2)); 974 1.1.1.2 christos 975 1.1.1.2 christos if(verbosity < v) 976 1.1.1.2 christos return; 977 1.1.1.2 christos 978 1.1.1.2 christos sldns_wire2str_rcode_buf((int)rcode, rcode_buf, sizeof(rcode_buf)); 979 1.1.1.2 christos addr_to_str(addr, addrlen, clientip_buf, sizeof(clientip_buf)); 980 1.1.1.9 christos if(daddr) { 981 1.1.1.9 christos char da[128]; 982 1.1.1.9 christos int port = 0; 983 1.1.1.9 christos char* comm; 984 1.1.1.9 christos if(daddr->ss_family == AF_INET6) { 985 1.1.1.9 christos struct sockaddr_in6 *d = (struct sockaddr_in6 *)daddr; 986 1.1.1.9 christos if(inet_ntop(d->sin6_family, &d->sin6_addr, da, 987 1.1.1.10 christos sizeof(da)) == 0) 988 1.1.1.9 christos snprintf(dest_buf, sizeof(dest_buf), 989 1.1.1.9 christos "(inet_ntop_error)"); 990 1.1.1.9 christos port = ntohs(d->sin6_port); 991 1.1.1.9 christos } else if(daddr->ss_family == AF_INET) { 992 1.1.1.9 christos struct sockaddr_in *d = (struct sockaddr_in *)daddr; 993 1.1.1.9 christos if(inet_ntop(d->sin_family, &d->sin_addr, da, 994 1.1.1.10 christos sizeof(da)) == 0) 995 1.1.1.9 christos snprintf(dest_buf, sizeof(dest_buf), 996 1.1.1.9 christos "(inet_ntop_error)"); 997 1.1.1.9 christos port = ntohs(d->sin_port); 998 1.1.1.9 christos } else { 999 1.1.1.9 christos snprintf(da, sizeof(da), "socket%d", 1000 1.1.1.9 christos (int)daddr->ss_family); 1001 1.1.1.9 christos } 1002 1.1.1.9 christos comm = "udp"; 1003 1.1.1.9 christos if(tp == comm_tcp) comm = (ssl?"dot":"tcp"); 1004 1.1.1.9 christos else if(tp == comm_tcp_accept) comm = (ssl?"dot":"tcp"); 1005 1.1.1.9 christos else if(tp == comm_http) comm = "doh"; 1006 1.1.1.9 christos else if(tp == comm_local) comm = "unix"; 1007 1.1.1.9 christos else if(tp == comm_raw) comm = "raw"; 1008 1.1.1.9 christos snprintf(dest_buf, sizeof(dest_buf), " on %s %s %d", 1009 1.1.1.9 christos comm, da, port); 1010 1.1.1.9 christos } else { 1011 1.1.1.9 christos dest_buf[0]=0; 1012 1.1.1.9 christos } 1013 1.1.1.2 christos if(rcode == LDNS_RCODE_FORMERR) 1014 1.1.1.2 christos { 1015 1.1.1.4 christos if(LOG_TAG_QUERYREPLY) 1016 1.1.1.9 christos log_reply("%s - - - %s - - -%s", clientip_buf, 1017 1.1.1.9 christos rcode_buf, dest_buf); 1018 1.1.1.9 christos else log_info("%s - - - %s - - -%s", clientip_buf, 1019 1.1.1.9 christos rcode_buf, dest_buf); 1020 1.1.1.2 christos } else { 1021 1.1.1.9 christos char qname_buf[LDNS_MAX_DOMAINLEN]; 1022 1.1.1.9 christos char type_buf[16]; 1023 1.1.1.9 christos char class_buf[16]; 1024 1.1.1.9 christos size_t pktlen; 1025 1.1.1.2 christos if(qinf->qname) 1026 1.1.1.2 christos dname_str(qinf->qname, qname_buf); 1027 1.1.1.2 christos else snprintf(qname_buf, sizeof(qname_buf), "null"); 1028 1.1.1.2 christos pktlen = sldns_buffer_limit(rmsg); 1029 1.1.1.2 christos sldns_wire2str_type_buf(qinf->qtype, type_buf, sizeof(type_buf)); 1030 1.1.1.2 christos sldns_wire2str_class_buf(qinf->qclass, class_buf, sizeof(class_buf)); 1031 1.1.1.4 christos if(LOG_TAG_QUERYREPLY) 1032 1.1.1.9 christos log_reply("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d%s", 1033 1.1.1.4 christos clientip_buf, qname_buf, type_buf, class_buf, 1034 1.1.1.9 christos rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, 1035 1.1.1.9 christos cached, (int)pktlen, dest_buf); 1036 1.1.1.9 christos else log_info("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d%s", 1037 1.1.1.2 christos clientip_buf, qname_buf, type_buf, class_buf, 1038 1.1.1.9 christos rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, 1039 1.1.1.9 christos cached, (int)pktlen, dest_buf); 1040 1.1.1.2 christos } 1041 1.1.1.2 christos } 1042 1.1.1.2 christos 1043 1.1.1.2 christos void 1044 1.1 christos log_query_info(enum verbosity_value v, const char* str, 1045 1.1 christos struct query_info* qinf) 1046 1.1 christos { 1047 1.1 christos log_nametypeclass(v, str, qinf->qname, qinf->qtype, qinf->qclass); 1048 1.1 christos } 1049 1.1 christos 1050 1.1 christos int 1051 1.1 christos reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep) 1052 1.1 christos { 1053 1.1 christos /* check only answer section rrs for matching cname chain. 1054 1.1 christos * the cache may return changed rdata, but owner names are untouched.*/ 1055 1.1 christos size_t i; 1056 1.1 christos uint8_t* sname = qinfo->qname; 1057 1.1 christos size_t snamelen = qinfo->qname_len; 1058 1.1 christos for(i=0; i<rep->an_numrrsets; i++) { 1059 1.1 christos uint16_t t = ntohs(rep->rrsets[i]->rk.type); 1060 1.1 christos if(t == LDNS_RR_TYPE_DNAME) 1061 1.1 christos continue; /* skip dnames; note TTL 0 not cached */ 1062 1.1 christos /* verify that owner matches current sname */ 1063 1.1 christos if(query_dname_compare(sname, rep->rrsets[i]->rk.dname) != 0){ 1064 1.1 christos /* cname chain broken */ 1065 1.1 christos return 0; 1066 1.1 christos } 1067 1.1 christos /* if this is a cname; move on */ 1068 1.1 christos if(t == LDNS_RR_TYPE_CNAME) { 1069 1.1 christos get_cname_target(rep->rrsets[i], &sname, &snamelen); 1070 1.1 christos } 1071 1.1 christos } 1072 1.1 christos return 1; 1073 1.1 christos } 1074 1.1 christos 1075 1.1 christos int 1076 1.1 christos reply_all_rrsets_secure(struct reply_info* rep) 1077 1.1 christos { 1078 1.1 christos size_t i; 1079 1.1 christos for(i=0; i<rep->rrset_count; i++) { 1080 1.1 christos if( ((struct packed_rrset_data*)rep->rrsets[i]->entry.data) 1081 1.1 christos ->security != sec_status_secure ) 1082 1.1 christos return 0; 1083 1.1 christos } 1084 1.1 christos return 1; 1085 1.1 christos } 1086 1.1 christos 1087 1.1.1.3 christos struct reply_info* 1088 1.1.1.3 christos parse_reply_in_temp_region(sldns_buffer* pkt, struct regional* region, 1089 1.1.1.3 christos struct query_info* qi) 1090 1.1.1.3 christos { 1091 1.1.1.3 christos struct reply_info* rep; 1092 1.1.1.3 christos struct msg_parse* msg; 1093 1.1.1.3 christos if(!(msg = regional_alloc(region, sizeof(*msg)))) { 1094 1.1.1.3 christos return NULL; 1095 1.1.1.3 christos } 1096 1.1.1.3 christos memset(msg, 0, sizeof(*msg)); 1097 1.1.1.3 christos sldns_buffer_set_position(pkt, 0); 1098 1.1.1.4 christos if(parse_packet(pkt, msg, region) != 0){ 1099 1.1.1.3 christos return 0; 1100 1.1.1.4 christos } 1101 1.1.1.3 christos if(!parse_create_msg(pkt, msg, NULL, qi, &rep, region)) { 1102 1.1.1.3 christos return 0; 1103 1.1.1.3 christos } 1104 1.1.1.3 christos return rep; 1105 1.1.1.3 christos } 1106 1.1.1.3 christos 1107 1.1.1.7 christos int edns_opt_list_append_ede(struct edns_option** list, struct regional* region, 1108 1.1.1.7 christos sldns_ede_code code, const char *txt) 1109 1.1 christos { 1110 1.1 christos struct edns_option** prevp; 1111 1.1 christos struct edns_option* opt; 1112 1.1.1.7 christos size_t txt_len = txt ? strlen(txt) : 0; 1113 1.1 christos 1114 1.1 christos /* allocate new element */ 1115 1.1 christos opt = (struct edns_option*)regional_alloc(region, sizeof(*opt)); 1116 1.1 christos if(!opt) 1117 1.1 christos return 0; 1118 1.1 christos opt->next = NULL; 1119 1.1.1.7 christos opt->opt_code = LDNS_EDNS_EDE; 1120 1.1.1.7 christos opt->opt_len = txt_len + sizeof(uint16_t); 1121 1.1.1.7 christos opt->opt_data = regional_alloc(region, txt_len + sizeof(uint16_t)); 1122 1.1.1.7 christos if(!opt->opt_data) 1123 1.1.1.7 christos return 0; 1124 1.1.1.7 christos sldns_write_uint16(opt->opt_data, (uint16_t)code); 1125 1.1.1.7 christos if (txt_len) 1126 1.1.1.7 christos memmove(opt->opt_data + 2, txt, txt_len); 1127 1.1.1.7 christos 1128 1.1 christos /* append at end of list */ 1129 1.1.1.7 christos prevp = list; 1130 1.1 christos while(*prevp != NULL) 1131 1.1 christos prevp = &((*prevp)->next); 1132 1.1.1.10 christos verbose(VERB_ALGO, "attached EDE code: %d with message: '%s'", code, (txt?txt:"")); 1133 1.1 christos *prevp = opt; 1134 1.1 christos return 1; 1135 1.1 christos } 1136 1.1 christos 1137 1.1.1.8 christos int edns_opt_list_append_keepalive(struct edns_option** list, int msec, 1138 1.1.1.8 christos struct regional* region) 1139 1.1.1.8 christos { 1140 1.1.1.8 christos uint8_t data[2]; /* For keepalive value */ 1141 1.1.1.8 christos data[0] = (uint8_t)((msec >> 8) & 0xff); 1142 1.1.1.8 christos data[1] = (uint8_t)(msec & 0xff); 1143 1.1.1.8 christos return edns_opt_list_append(list, LDNS_EDNS_KEEPALIVE, sizeof(data), 1144 1.1.1.8 christos data, region); 1145 1.1.1.8 christos } 1146 1.1.1.8 christos 1147 1.1.1.2 christos int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len, 1148 1.1.1.2 christos uint8_t* data, struct regional* region) 1149 1.1 christos { 1150 1.1.1.2 christos struct edns_option** prevp; 1151 1.1.1.2 christos struct edns_option* opt; 1152 1.1.1.2 christos 1153 1.1.1.2 christos /* allocate new element */ 1154 1.1.1.2 christos opt = (struct edns_option*)regional_alloc(region, sizeof(*opt)); 1155 1.1.1.2 christos if(!opt) 1156 1.1.1.2 christos return 0; 1157 1.1.1.2 christos opt->next = NULL; 1158 1.1.1.2 christos opt->opt_code = code; 1159 1.1.1.2 christos opt->opt_len = len; 1160 1.1.1.2 christos opt->opt_data = NULL; 1161 1.1.1.2 christos if(len > 0) { 1162 1.1.1.2 christos opt->opt_data = regional_alloc_init(region, data, len); 1163 1.1.1.2 christos if(!opt->opt_data) 1164 1.1.1.2 christos return 0; 1165 1.1.1.2 christos } 1166 1.1.1.2 christos 1167 1.1.1.2 christos /* append at end of list */ 1168 1.1.1.2 christos prevp = list; 1169 1.1.1.2 christos while(*prevp != NULL) { 1170 1.1.1.2 christos prevp = &((*prevp)->next); 1171 1.1.1.2 christos } 1172 1.1.1.2 christos *prevp = opt; 1173 1.1.1.2 christos return 1; 1174 1.1.1.2 christos } 1175 1.1.1.2 christos 1176 1.1.1.2 christos int edns_opt_list_remove(struct edns_option** list, uint16_t code) 1177 1.1.1.2 christos { 1178 1.1.1.2 christos /* The list should already be allocated in a region. Freeing the 1179 1.1.1.2 christos * allocated space in a region is not possible. We just unlink the 1180 1.1.1.2 christos * required elements and they will be freed together with the region. */ 1181 1.1.1.2 christos 1182 1.1.1.2 christos struct edns_option* prev; 1183 1.1.1.2 christos struct edns_option* curr; 1184 1.1.1.2 christos if(!list || !(*list)) return 0; 1185 1.1.1.2 christos 1186 1.1.1.2 christos /* Unlink and repoint if the element(s) are first in list */ 1187 1.1.1.2 christos while(list && *list && (*list)->opt_code == code) { 1188 1.1.1.2 christos *list = (*list)->next; 1189 1.1.1.2 christos } 1190 1.1.1.2 christos 1191 1.1.1.2 christos if(!list || !(*list)) return 1; 1192 1.1.1.2 christos /* Unlink elements and reattach the list */ 1193 1.1.1.2 christos prev = *list; 1194 1.1.1.2 christos curr = (*list)->next; 1195 1.1.1.2 christos while(curr != NULL) { 1196 1.1.1.2 christos if(curr->opt_code == code) { 1197 1.1.1.2 christos prev->next = curr->next; 1198 1.1.1.2 christos curr = curr->next; 1199 1.1.1.2 christos } else { 1200 1.1.1.2 christos prev = curr; 1201 1.1.1.2 christos curr = curr->next; 1202 1.1.1.2 christos } 1203 1.1.1.2 christos } 1204 1.1.1.2 christos return 1; 1205 1.1.1.2 christos } 1206 1.1.1.2 christos 1207 1.1.1.2 christos static int inplace_cb_reply_call_generic( 1208 1.1.1.2 christos struct inplace_cb* callback_list, enum inplace_cb_list_type type, 1209 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate, 1210 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns, 1211 1.1.1.6 christos struct comm_reply* repinfo, struct regional* region, 1212 1.1.1.6 christos struct timeval* start_time) 1213 1.1.1.2 christos { 1214 1.1.1.2 christos struct inplace_cb* cb; 1215 1.1.1.2 christos struct edns_option* opt_list_out = NULL; 1216 1.1.1.2 christos #if defined(EXPORT_ALL_SYMBOLS) 1217 1.1.1.2 christos (void)type; /* param not used when fptr_ok disabled */ 1218 1.1.1.2 christos #endif 1219 1.1.1.2 christos if(qstate) 1220 1.1.1.2 christos opt_list_out = qstate->edns_opts_front_out; 1221 1.1.1.2 christos for(cb=callback_list; cb; cb=cb->next) { 1222 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_reply_generic( 1223 1.1.1.2 christos (inplace_cb_reply_func_type*)cb->cb, type)); 1224 1.1.1.2 christos (void)(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep, 1225 1.1.1.6 christos rcode, edns, &opt_list_out, repinfo, region, start_time, cb->id, cb->cb_arg); 1226 1.1.1.2 christos } 1227 1.1.1.7 christos edns->opt_list_inplace_cb_out = opt_list_out; 1228 1.1.1.2 christos return 1; 1229 1.1.1.2 christos } 1230 1.1.1.2 christos 1231 1.1.1.2 christos int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo, 1232 1.1.1.2 christos struct module_qstate* qstate, struct reply_info* rep, int rcode, 1233 1.1.1.6 christos struct edns_data* edns, struct comm_reply* repinfo, struct regional* region, 1234 1.1.1.6 christos struct timeval* start_time) 1235 1.1.1.2 christos { 1236 1.1.1.2 christos return inplace_cb_reply_call_generic( 1237 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply], inplace_cb_reply, qinfo, 1238 1.1.1.6 christos qstate, rep, rcode, edns, repinfo, region, start_time); 1239 1.1.1.2 christos } 1240 1.1.1.2 christos 1241 1.1.1.2 christos int inplace_cb_reply_cache_call(struct module_env* env, 1242 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate, 1243 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns, 1244 1.1.1.6 christos struct comm_reply* repinfo, struct regional* region, 1245 1.1.1.6 christos struct timeval* start_time) 1246 1.1.1.2 christos { 1247 1.1.1.2 christos return inplace_cb_reply_call_generic( 1248 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply_cache], inplace_cb_reply_cache, 1249 1.1.1.6 christos qinfo, qstate, rep, rcode, edns, repinfo, region, start_time); 1250 1.1.1.2 christos } 1251 1.1.1.2 christos 1252 1.1.1.2 christos int inplace_cb_reply_local_call(struct module_env* env, 1253 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate, 1254 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns, 1255 1.1.1.6 christos struct comm_reply* repinfo, struct regional* region, 1256 1.1.1.6 christos struct timeval* start_time) 1257 1.1.1.2 christos { 1258 1.1.1.2 christos return inplace_cb_reply_call_generic( 1259 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply_local], inplace_cb_reply_local, 1260 1.1.1.6 christos qinfo, qstate, rep, rcode, edns, repinfo, region, start_time); 1261 1.1.1.2 christos } 1262 1.1.1.2 christos 1263 1.1.1.2 christos int inplace_cb_reply_servfail_call(struct module_env* env, 1264 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate, 1265 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns, 1266 1.1.1.6 christos struct comm_reply* repinfo, struct regional* region, 1267 1.1.1.6 christos struct timeval* start_time) 1268 1.1.1.2 christos { 1269 1.1.1.2 christos /* We are going to servfail. Remove any potential edns options. */ 1270 1.1.1.2 christos if(qstate) 1271 1.1.1.2 christos qstate->edns_opts_front_out = NULL; 1272 1.1.1.2 christos return inplace_cb_reply_call_generic( 1273 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply_servfail], 1274 1.1.1.4 christos inplace_cb_reply_servfail, qinfo, qstate, rep, rcode, edns, repinfo, 1275 1.1.1.6 christos region, start_time); 1276 1.1.1.2 christos } 1277 1.1.1.2 christos 1278 1.1.1.2 christos int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo, 1279 1.1.1.2 christos uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen, 1280 1.1.1.2 christos uint8_t* zone, size_t zonelen, struct module_qstate* qstate, 1281 1.1.1.2 christos struct regional* region) 1282 1.1.1.2 christos { 1283 1.1.1.2 christos struct inplace_cb* cb = env->inplace_cb_lists[inplace_cb_query]; 1284 1.1.1.2 christos for(; cb; cb=cb->next) { 1285 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_query( 1286 1.1.1.2 christos (inplace_cb_query_func_type*)cb->cb)); 1287 1.1.1.2 christos (void)(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags, 1288 1.1.1.2 christos qstate, addr, addrlen, zone, zonelen, region, 1289 1.1.1.2 christos cb->id, cb->cb_arg); 1290 1.1.1.2 christos } 1291 1.1.1.2 christos return 1; 1292 1.1.1.2 christos } 1293 1.1.1.2 christos 1294 1.1.1.2 christos int inplace_cb_edns_back_parsed_call(struct module_env* env, 1295 1.1.1.2 christos struct module_qstate* qstate) 1296 1.1.1.2 christos { 1297 1.1.1.2 christos struct inplace_cb* cb = 1298 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_edns_back_parsed]; 1299 1.1.1.2 christos for(; cb; cb=cb->next) { 1300 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_edns_back_parsed( 1301 1.1.1.2 christos (inplace_cb_edns_back_parsed_func_type*)cb->cb)); 1302 1.1.1.2 christos (void)(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate, 1303 1.1.1.2 christos cb->id, cb->cb_arg); 1304 1.1.1.2 christos } 1305 1.1.1.2 christos return 1; 1306 1.1.1.2 christos } 1307 1.1.1.2 christos 1308 1.1.1.2 christos int inplace_cb_query_response_call(struct module_env* env, 1309 1.1.1.2 christos struct module_qstate* qstate, struct dns_msg* response) { 1310 1.1.1.2 christos struct inplace_cb* cb = 1311 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_query_response]; 1312 1.1.1.2 christos for(; cb; cb=cb->next) { 1313 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_query_response( 1314 1.1.1.2 christos (inplace_cb_query_response_func_type*)cb->cb)); 1315 1.1.1.2 christos (void)(*(inplace_cb_query_response_func_type*)cb->cb)(qstate, 1316 1.1.1.2 christos response, cb->id, cb->cb_arg); 1317 1.1.1.2 christos } 1318 1.1 christos return 1; 1319 1.1 christos } 1320 1.1 christos 1321 1.1 christos struct edns_option* edns_opt_copy_region(struct edns_option* list, 1322 1.1.1.8 christos struct regional* region) 1323 1.1 christos { 1324 1.1 christos struct edns_option* result = NULL, *cur = NULL, *s; 1325 1.1 christos while(list) { 1326 1.1 christos /* copy edns option structure */ 1327 1.1 christos s = regional_alloc_init(region, list, sizeof(*list)); 1328 1.1 christos if(!s) return NULL; 1329 1.1 christos s->next = NULL; 1330 1.1 christos 1331 1.1 christos /* copy option data */ 1332 1.1 christos if(s->opt_data) { 1333 1.1 christos s->opt_data = regional_alloc_init(region, s->opt_data, 1334 1.1 christos s->opt_len); 1335 1.1 christos if(!s->opt_data) 1336 1.1 christos return NULL; 1337 1.1 christos } 1338 1.1 christos 1339 1.1 christos /* link into list */ 1340 1.1 christos if(cur) 1341 1.1 christos cur->next = s; 1342 1.1 christos else result = s; 1343 1.1 christos cur = s; 1344 1.1 christos 1345 1.1 christos /* examine next element */ 1346 1.1 christos list = list->next; 1347 1.1 christos } 1348 1.1 christos return result; 1349 1.1 christos } 1350 1.1 christos 1351 1.1.1.8 christos struct edns_option* edns_opt_copy_filter_region(struct edns_option* list, 1352 1.1.1.8 christos uint16_t* filter_list, size_t filter_list_len, struct regional* region) 1353 1.1.1.8 christos { 1354 1.1.1.8 christos struct edns_option* result = NULL, *cur = NULL, *s; 1355 1.1.1.8 christos size_t i; 1356 1.1.1.8 christos while(list) { 1357 1.1.1.8 christos for(i=0; i<filter_list_len; i++) 1358 1.1.1.8 christos if(filter_list[i] == list->opt_code) goto found; 1359 1.1.1.8 christos if(i == filter_list_len) goto next; 1360 1.1.1.8 christos found: 1361 1.1.1.8 christos /* copy edns option structure */ 1362 1.1.1.8 christos s = regional_alloc_init(region, list, sizeof(*list)); 1363 1.1.1.8 christos if(!s) return NULL; 1364 1.1.1.8 christos s->next = NULL; 1365 1.1.1.8 christos 1366 1.1.1.8 christos /* copy option data */ 1367 1.1.1.8 christos if(s->opt_data) { 1368 1.1.1.8 christos s->opt_data = regional_alloc_init(region, s->opt_data, 1369 1.1.1.8 christos s->opt_len); 1370 1.1.1.8 christos if(!s->opt_data) 1371 1.1.1.8 christos return NULL; 1372 1.1.1.8 christos } 1373 1.1.1.8 christos 1374 1.1.1.8 christos /* link into list */ 1375 1.1.1.8 christos if(cur) 1376 1.1.1.8 christos cur->next = s; 1377 1.1.1.8 christos else result = s; 1378 1.1.1.8 christos cur = s; 1379 1.1.1.8 christos 1380 1.1.1.8 christos next: 1381 1.1.1.8 christos /* examine next element */ 1382 1.1.1.8 christos list = list->next; 1383 1.1.1.8 christos } 1384 1.1.1.8 christos return result; 1385 1.1.1.8 christos } 1386 1.1.1.8 christos 1387 1.1 christos int edns_opt_compare(struct edns_option* p, struct edns_option* q) 1388 1.1 christos { 1389 1.1 christos if(!p && !q) return 0; 1390 1.1 christos if(!p) return -1; 1391 1.1 christos if(!q) return 1; 1392 1.1 christos log_assert(p && q); 1393 1.1 christos if(p->opt_code != q->opt_code) 1394 1.1 christos return (int)q->opt_code - (int)p->opt_code; 1395 1.1 christos if(p->opt_len != q->opt_len) 1396 1.1 christos return (int)q->opt_len - (int)p->opt_len; 1397 1.1 christos if(p->opt_len != 0) 1398 1.1 christos return memcmp(p->opt_data, q->opt_data, p->opt_len); 1399 1.1 christos return 0; 1400 1.1 christos } 1401 1.1 christos 1402 1.1 christos int edns_opt_list_compare(struct edns_option* p, struct edns_option* q) 1403 1.1 christos { 1404 1.1 christos int r; 1405 1.1 christos while(p && q) { 1406 1.1 christos r = edns_opt_compare(p, q); 1407 1.1 christos if(r != 0) 1408 1.1 christos return r; 1409 1.1 christos p = p->next; 1410 1.1 christos q = q->next; 1411 1.1 christos } 1412 1.1 christos if(p || q) { 1413 1.1 christos /* uneven length lists */ 1414 1.1 christos if(p) return 1; 1415 1.1 christos if(q) return -1; 1416 1.1 christos } 1417 1.1 christos return 0; 1418 1.1 christos } 1419 1.1 christos 1420 1.1 christos void edns_opt_list_free(struct edns_option* list) 1421 1.1 christos { 1422 1.1 christos struct edns_option* n; 1423 1.1 christos while(list) { 1424 1.1 christos free(list->opt_data); 1425 1.1 christos n = list->next; 1426 1.1 christos free(list); 1427 1.1 christos list = n; 1428 1.1 christos } 1429 1.1 christos } 1430 1.1 christos 1431 1.1 christos struct edns_option* edns_opt_copy_alloc(struct edns_option* list) 1432 1.1 christos { 1433 1.1 christos struct edns_option* result = NULL, *cur = NULL, *s; 1434 1.1 christos while(list) { 1435 1.1 christos /* copy edns option structure */ 1436 1.1 christos s = memdup(list, sizeof(*list)); 1437 1.1 christos if(!s) { 1438 1.1 christos edns_opt_list_free(result); 1439 1.1 christos return NULL; 1440 1.1 christos } 1441 1.1 christos s->next = NULL; 1442 1.1 christos 1443 1.1 christos /* copy option data */ 1444 1.1 christos if(s->opt_data) { 1445 1.1 christos s->opt_data = memdup(s->opt_data, s->opt_len); 1446 1.1 christos if(!s->opt_data) { 1447 1.1.1.2 christos free(s); 1448 1.1 christos edns_opt_list_free(result); 1449 1.1 christos return NULL; 1450 1.1 christos } 1451 1.1 christos } 1452 1.1 christos 1453 1.1 christos /* link into list */ 1454 1.1 christos if(cur) 1455 1.1 christos cur->next = s; 1456 1.1 christos else result = s; 1457 1.1 christos cur = s; 1458 1.1 christos 1459 1.1 christos /* examine next element */ 1460 1.1 christos list = list->next; 1461 1.1 christos } 1462 1.1 christos return result; 1463 1.1 christos } 1464 1.1 christos 1465 1.1.1.2 christos struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code) 1466 1.1 christos { 1467 1.1 christos struct edns_option* p; 1468 1.1 christos for(p=list; p; p=p->next) { 1469 1.1 christos if(p->opt_code == code) 1470 1.1 christos return p; 1471 1.1 christos } 1472 1.1 christos return NULL; 1473 1.1 christos } 1474 1.1.1.10 christos 1475 1.1.1.10 christos int local_alias_shallow_copy_qname(struct local_rrset* local_alias, uint8_t** qname, 1476 1.1.1.10 christos size_t* qname_len) 1477 1.1.1.10 christos { 1478 1.1.1.10 christos struct ub_packed_rrset_key* rrset = local_alias->rrset; 1479 1.1.1.10 christos struct packed_rrset_data* d = rrset->entry.data; 1480 1.1.1.10 christos 1481 1.1.1.10 christos /* Sanity check: our current implementation only supports 1482 1.1.1.10 christos * a single CNAME RRset as a local alias. */ 1483 1.1.1.10 christos if(local_alias->next || 1484 1.1.1.10 christos rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) || 1485 1.1.1.10 christos d->count != 1) { 1486 1.1.1.10 christos log_err("assumption failure: unexpected local alias"); 1487 1.1.1.10 christos return 0; 1488 1.1.1.10 christos } 1489 1.1.1.10 christos *qname = d->rr_data[0] + 2; 1490 1.1.1.10 christos *qname_len = d->rr_len[0] - 2; 1491 1.1.1.10 christos return 1; 1492 1.1.1.10 christos } 1493