2 * util/data/msgreply.c - store message and reply data.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains a data structure to store a message and its reply.
43 #include "util/data/msgreply.h"
44 #include "util/storage/lookup3.h"
46 #include "util/alloc.h"
47 #include "util/netevent.h"
48 #include "util/net_help.h"
49 #include "util/data/dname.h"
50 #include "util/regional.h"
51 #include "util/data/msgparse.h"
52 #include "util/data/msgencode.h"
53 #include "sldns/sbuffer.h"
54 #include "sldns/wire2str.h"
55 #include "util/module.h"
56 #include "util/fptr_wlist.h"
58 /** MAX TTL default for messages and rrsets */
59 time_t MAX_TTL = 3600 * 24 * 10; /* ten days */
60 /** MIN TTL default for messages and rrsets */
62 /** MAX Negative TTL, for SOA records in authority section */
63 time_t MAX_NEG_TTL = 3600; /* one hour */
64 /** If we serve expired entries and prefetch them */
65 int SERVE_EXPIRED = 0;
66 /** Time to serve records after expiration */
67 time_t SERVE_EXPIRED_TTL = 0;
68 /** TTL to use for expired records */
69 time_t SERVE_EXPIRED_REPLY_TTL = 30;
70 /** If we serve the original TTL or decrementing TTLs */
71 int SERVE_ORIGINAL_TTL = 0;
73 /** allocate qinfo, return 0 on error */
75 parse_create_qinfo(sldns_buffer* pkt, struct msg_parse* msg,
76 struct query_info* qinf, struct regional* region)
80 qinf->qname = (uint8_t*)regional_alloc(region,
82 else qinf->qname = (uint8_t*)malloc(msg->qname_len);
83 if(!qinf->qname) return 0;
84 dname_pkt_copy(pkt, qinf->qname, msg->qname);
85 } else qinf->qname = 0;
86 qinf->qname_len = msg->qname_len;
87 qinf->qtype = msg->qtype;
88 qinf->qclass = msg->qclass;
89 qinf->local_alias = NULL;
93 /** constructor for replyinfo */
95 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd,
96 time_t ttl, time_t prettl, time_t expttl, size_t an, size_t ns,
97 size_t ar, size_t total, enum sec_status sec, sldns_ede_code reason_bogus)
99 struct reply_info* rep;
100 /* rrset_count-1 because the first ref is part of the struct. */
101 size_t s = sizeof(struct reply_info) - sizeof(struct rrset_ref) +
102 sizeof(struct ub_packed_rrset_key*) * total;
103 if(total >= RR_COUNT_MAX) return NULL; /* sanity check on numRRS*/
105 rep = (struct reply_info*)regional_alloc(region, s);
106 else rep = (struct reply_info*)malloc(s +
107 sizeof(struct rrset_ref) * (total));
113 rep->prefetch_ttl = prettl;
114 rep->serve_expired_ttl = expttl;
115 rep->an_numrrsets = an;
116 rep->ns_numrrsets = ns;
117 rep->ar_numrrsets = ar;
118 rep->rrset_count = total;
120 rep->reason_bogus = reason_bogus;
121 /* this is only allocated and used for caching on copy */
122 rep->reason_bogus_str = NULL;
123 rep->authoritative = 0;
124 /* array starts after the refs */
126 rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[0]);
127 else rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[total]);
128 /* zero the arrays to assist cleanup in case of malloc failure */
129 memset( rep->rrsets, 0, sizeof(struct ub_packed_rrset_key*) * total);
131 memset( &rep->ref[0], 0, sizeof(struct rrset_ref) * total);
135 /** allocate replyinfo, return 0 on error */
137 parse_create_repinfo(struct msg_parse* msg, struct reply_info** rep,
138 struct regional* region)
140 *rep = construct_reply_info_base(region, msg->flags, msg->qdcount, 0,
141 0, 0, msg->an_rrsets, msg->ns_rrsets, msg->ar_rrsets,
142 msg->rrset_count, sec_status_unchecked, LDNS_EDE_NONE);
149 reply_info_alloc_rrset_keys(struct reply_info* rep, struct alloc_cache* alloc,
150 struct regional* region)
153 for(i=0; i<rep->rrset_count; i++) {
155 rep->rrsets[i] = (struct ub_packed_rrset_key*)
156 regional_alloc(region,
157 sizeof(struct ub_packed_rrset_key));
159 memset(rep->rrsets[i], 0,
160 sizeof(struct ub_packed_rrset_key));
161 rep->rrsets[i]->entry.key = rep->rrsets[i];
164 else rep->rrsets[i] = alloc_special_obtain(alloc);
167 rep->rrsets[i]->entry.data = NULL;
173 make_new_reply_info(const struct reply_info* rep, struct regional* region,
174 size_t an_numrrsets, size_t copy_rrsets)
176 struct reply_info* new_rep;
179 /* create a base struct. we specify 'insecure' security status as
180 * the modified response won't be DNSSEC-valid. In our faked response
181 * the authority and additional sections will be empty (except possible
182 * EDNS0 OPT RR in the additional section appended on sending it out),
183 * so the total number of RRsets is an_numrrsets. */
184 new_rep = construct_reply_info_base(region, rep->flags,
185 rep->qdcount, rep->ttl, rep->prefetch_ttl,
186 rep->serve_expired_ttl, an_numrrsets, 0, 0, an_numrrsets,
187 sec_status_insecure, LDNS_EDE_NONE);
190 if(!reply_info_alloc_rrset_keys(new_rep, NULL, region))
192 for(i=0; i<copy_rrsets; i++)
193 new_rep->rrsets[i] = rep->rrsets[i];
198 /** find the minimumttl in the rdata of SOA record */
200 soa_find_minttl(struct rr_parse* rr)
202 uint16_t rlen = sldns_read_uint16(rr->ttl_data+4);
204 return 0; /* rdata too small for SOA (dname, dname, 5*32bit) */
205 /* minimum TTL is the last 32bit value in the rdata of the record */
206 /* at position ttl_data + 4(ttl) + 2(rdatalen) + rdatalen - 4(timeval)*/
207 return (time_t)sldns_read_uint32(rr->ttl_data+6+rlen-4);
210 /** do the rdata copy */
212 rdata_copy(sldns_buffer* pkt, struct packed_rrset_data* data, uint8_t* to,
213 struct rr_parse* rr, time_t* rr_ttl, uint16_t type,
214 sldns_pkt_section section)
217 const sldns_rr_descriptor* desc;
219 *rr_ttl = sldns_read_uint32(rr->ttl_data);
220 /* RFC 2181 Section 8. if msb of ttl is set treat as if zero. */
221 if(*rr_ttl & 0x80000000U)
223 if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) {
224 /* negative response. see if TTL of SOA record larger than the
225 * minimum-ttl in the rdata of the SOA record */
226 if(*rr_ttl > soa_find_minttl(rr))
227 *rr_ttl = soa_find_minttl(rr);
229 if(!SERVE_ORIGINAL_TTL && (*rr_ttl < MIN_TTL))
231 if(!SERVE_ORIGINAL_TTL && (*rr_ttl > MAX_TTL))
233 if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) {
234 /* max neg ttl overrides the min and max ttl of everything
235 * else, it is for a more specific record */
236 if(*rr_ttl > MAX_NEG_TTL)
237 *rr_ttl = MAX_NEG_TTL;
239 if(*rr_ttl < data->ttl)
242 if(rr->outside_packet) {
243 /* uncompressed already, only needs copy */
244 memmove(to, rr->ttl_data+sizeof(uint32_t), rr->size);
248 sldns_buffer_set_position(pkt, (size_t)
249 (rr->ttl_data - sldns_buffer_begin(pkt) + sizeof(uint32_t)));
250 /* insert decompressed size into rdata len stored in memory */
251 /* -2 because rdatalen bytes are not included. */
252 pkt_len = htons(rr->size - 2);
253 memmove(to, &pkt_len, sizeof(uint16_t));
255 /* read packet rdata len */
256 pkt_len = sldns_buffer_read_u16(pkt);
257 if(sldns_buffer_remaining(pkt) < pkt_len)
259 desc = sldns_rr_descript(type);
260 if(pkt_len > 0 && desc && desc->_dname_count > 0) {
261 int count = (int)desc->_dname_count;
265 /* decompress dnames. */
266 while(pkt_len > 0 && count) {
267 switch(desc->_wireformat[rdf]) {
268 case LDNS_RDF_TYPE_DNAME:
269 oldpos = sldns_buffer_position(pkt);
270 dname_pkt_copy(pkt, to,
271 sldns_buffer_current(pkt));
272 to += pkt_dname_len(pkt);
273 pkt_len -= sldns_buffer_position(pkt)-oldpos;
277 case LDNS_RDF_TYPE_STR:
278 len = sldns_buffer_current(pkt)[0] + 1;
281 len = get_rdf_size(desc->_wireformat[rdf]);
285 log_assert(len <= pkt_len);
286 memmove(to, sldns_buffer_current(pkt), len);
288 sldns_buffer_skip(pkt, (ssize_t)len);
294 /* copy remaining rdata */
296 memmove(to, sldns_buffer_current(pkt), pkt_len);
301 /** copy over the data into packed rrset */
303 parse_rr_copy(sldns_buffer* pkt, struct rrset_parse* pset,
304 struct packed_rrset_data* data)
307 struct rr_parse* rr = pset->rr_first;
309 size_t total = pset->rr_count + pset->rrsig_count;
311 data->count = pset->rr_count;
312 data->rrsig_count = pset->rrsig_count;
313 data->trust = rrset_trust_none;
314 data->security = sec_status_unchecked;
315 /* layout: struct - rr_len - rr_data - rr_ttl - rdata - rrsig */
316 data->rr_len = (size_t*)((uint8_t*)data +
317 sizeof(struct packed_rrset_data));
318 data->rr_data = (uint8_t**)&(data->rr_len[total]);
319 data->rr_ttl = (time_t*)&(data->rr_data[total]);
320 nextrdata = (uint8_t*)&(data->rr_ttl[total]);
321 for(i=0; i<data->count; i++) {
322 data->rr_len[i] = rr->size;
323 data->rr_data[i] = nextrdata;
324 nextrdata += rr->size;
325 if(!rdata_copy(pkt, data, data->rr_data[i], rr,
326 &data->rr_ttl[i], pset->type, pset->section))
330 /* if rrsig, its rdata is at nextrdata */
331 rr = pset->rrsig_first;
332 for(i=data->count; i<total; i++) {
333 data->rr_len[i] = rr->size;
334 data->rr_data[i] = nextrdata;
335 nextrdata += rr->size;
336 if(!rdata_copy(pkt, data, data->rr_data[i], rr,
337 &data->rr_ttl[i], LDNS_RR_TYPE_RRSIG, pset->section))
344 /** create rrset return 0 on failure */
346 parse_create_rrset(sldns_buffer* pkt, struct rrset_parse* pset,
347 struct packed_rrset_data** data, struct regional* region)
351 if(pset->rr_count > RR_COUNT_MAX || pset->rrsig_count > RR_COUNT_MAX ||
352 pset->size > RR_COUNT_MAX)
353 return 0; /* protect against integer overflow */
354 s = sizeof(struct packed_rrset_data) +
355 (pset->rr_count + pset->rrsig_count) *
356 (sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t)) +
359 *data = regional_alloc_zero(region, s);
360 else *data = calloc(1, s);
363 /* copy & decompress */
364 if(!parse_rr_copy(pkt, pset, *data)) {
374 /** get trust value for rrset */
375 static enum rrset_trust
376 get_rrset_trust(struct msg_parse* msg, struct rrset_parse* rrset)
378 uint16_t AA = msg->flags & BIT_AA;
379 if(rrset->section == LDNS_SECTION_ANSWER) {
381 /* RFC2181 says remainder of CNAME chain is nonauth*/
382 if(msg->rrset_first &&
383 msg->rrset_first->section==LDNS_SECTION_ANSWER
384 && msg->rrset_first->type==LDNS_RR_TYPE_CNAME){
385 if(rrset == msg->rrset_first)
386 return rrset_trust_ans_AA;
387 else return rrset_trust_ans_noAA;
389 if(msg->rrset_first &&
390 msg->rrset_first->section==LDNS_SECTION_ANSWER
391 && msg->rrset_first->type==LDNS_RR_TYPE_DNAME){
392 if(rrset == msg->rrset_first ||
393 rrset == msg->rrset_first->rrset_all_next)
394 return rrset_trust_ans_AA;
395 else return rrset_trust_ans_noAA;
397 return rrset_trust_ans_AA;
399 else return rrset_trust_ans_noAA;
400 } else if(rrset->section == LDNS_SECTION_AUTHORITY) {
401 if(AA) return rrset_trust_auth_AA;
402 else return rrset_trust_auth_noAA;
405 if(AA) return rrset_trust_add_AA;
406 else return rrset_trust_add_noAA;
409 return rrset_trust_none;
413 parse_copy_decompress_rrset(sldns_buffer* pkt, struct msg_parse* msg,
414 struct rrset_parse *pset, struct regional* region,
415 struct ub_packed_rrset_key* pk)
417 struct packed_rrset_data* data;
418 pk->rk.flags = pset->flags;
419 pk->rk.dname_len = pset->dname_len;
421 pk->rk.dname = (uint8_t*)regional_alloc(
422 region, pset->dname_len);
424 (uint8_t*)malloc(pset->dname_len);
427 /** copy & decompress dname */
428 dname_pkt_copy(pkt, pk->rk.dname, pset->dname);
429 /** copy over type and class */
430 pk->rk.type = htons(pset->type);
431 pk->rk.rrset_class = pset->rrset_class;
432 /** read data part. */
433 if(!parse_create_rrset(pkt, pset, &data, region)) {
440 pk->entry.data = (void*)data;
441 pk->entry.key = (void*)pk;
442 pk->entry.hash = pset->hash;
443 data->trust = get_rrset_trust(msg, pset);
448 * Copy and decompress rrs
449 * @param pkt: the packet for compression pointer resolution.
450 * @param msg: the parsed message
451 * @param rep: reply info to put rrs into.
452 * @param region: if not NULL, used for allocation.
453 * @return 0 on failure.
456 parse_copy_decompress(sldns_buffer* pkt, struct msg_parse* msg,
457 struct reply_info* rep, struct regional* region)
460 struct rrset_parse *pset = msg->rrset_first;
461 struct packed_rrset_data* data;
464 rep->security = sec_status_unchecked;
465 if(rep->rrset_count == 0)
468 for(i=0; i<rep->rrset_count; i++) {
469 if(!parse_copy_decompress_rrset(pkt, msg, pset, region,
472 data = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
473 if(data->ttl < rep->ttl)
474 rep->ttl = data->ttl;
476 pset = pset->rrset_all_next;
478 rep->prefetch_ttl = PREFETCH_TTL_CALC(rep->ttl);
479 rep->serve_expired_ttl = rep->ttl + SERVE_EXPIRED_TTL;
484 parse_create_msg(sldns_buffer* pkt, struct msg_parse* msg,
485 struct alloc_cache* alloc, struct query_info* qinf,
486 struct reply_info** rep, struct regional* region)
488 log_assert(pkt && msg);
489 if(!parse_create_qinfo(pkt, msg, qinf, region))
491 if(!parse_create_repinfo(msg, rep, region))
493 if(!reply_info_alloc_rrset_keys(*rep, alloc, region)) {
494 if(!region) reply_info_parsedelete(*rep, alloc);
497 if(!parse_copy_decompress(pkt, msg, *rep, region)) {
498 if(!region) reply_info_parsedelete(*rep, alloc);
504 int reply_info_parse(sldns_buffer* pkt, struct alloc_cache* alloc,
505 struct query_info* qinf, struct reply_info** rep,
506 struct regional* region, struct edns_data* edns)
508 /* use scratch pad region-allocator during parsing. */
509 struct msg_parse* msg;
513 qinf->local_alias = NULL;
515 if(!(msg = regional_alloc(region, sizeof(*msg)))) {
516 return LDNS_RCODE_SERVFAIL;
518 memset(msg, 0, sizeof(*msg));
520 sldns_buffer_set_position(pkt, 0);
521 if((ret = parse_packet(pkt, msg, region)) != 0) {
524 if((ret = parse_extract_edns_from_response_msg(msg, edns, region)) != 0)
527 /* parse OK, allocate return structures */
528 /* this also performs dname decompression */
529 if(!parse_create_msg(pkt, msg, alloc, qinf, rep, NULL)) {
530 query_info_clear(qinf);
532 return LDNS_RCODE_SERVFAIL;
537 /** helper compare function to sort in lock order */
539 reply_info_sortref_cmp(const void* a, const void* b)
541 struct rrset_ref* x = (struct rrset_ref*)a;
542 struct rrset_ref* y = (struct rrset_ref*)b;
543 if(x->key < y->key) return -1;
544 if(x->key > y->key) return 1;
549 reply_info_sortref(struct reply_info* rep)
551 qsort(&rep->ref[0], rep->rrset_count, sizeof(struct rrset_ref),
552 reply_info_sortref_cmp);
556 reply_info_set_ttls(struct reply_info* rep, time_t timenow)
560 rep->prefetch_ttl += timenow;
561 rep->serve_expired_ttl += timenow;
562 for(i=0; i<rep->rrset_count; i++) {
563 struct packed_rrset_data* data = (struct packed_rrset_data*)
564 rep->ref[i].key->entry.data;
565 if(i>0 && rep->ref[i].key == rep->ref[i-1].key)
567 data->ttl += timenow;
568 for(j=0; j<data->count + data->rrsig_count; j++) {
569 data->rr_ttl[j] += timenow;
571 data->ttl_add = timenow;
576 reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc)
581 /* no need to lock, since not shared in hashtables. */
582 for(i=0; i<rep->rrset_count; i++) {
583 ub_packed_rrset_parsedelete(rep->rrsets[i], alloc);
585 if(rep->reason_bogus_str) {
586 free(rep->reason_bogus_str);
587 rep->reason_bogus_str = NULL;
593 query_info_parse(struct query_info* m, sldns_buffer* query)
595 uint8_t* q = sldns_buffer_begin(query);
596 /* minimum size: header + \0 + qtype + qclass */
597 if(sldns_buffer_limit(query) < LDNS_HEADER_SIZE + 5)
599 if((LDNS_OPCODE_WIRE(q) != LDNS_PACKET_QUERY && LDNS_OPCODE_WIRE(q) !=
600 LDNS_PACKET_NOTIFY) || LDNS_QDCOUNT(q) != 1 ||
601 sldns_buffer_position(query) != 0)
603 sldns_buffer_skip(query, LDNS_HEADER_SIZE);
604 m->qname = sldns_buffer_current(query);
605 if((m->qname_len = query_dname_len(query)) == 0)
606 return 0; /* parse error */
607 if(sldns_buffer_remaining(query) < 4)
608 return 0; /* need qtype, qclass */
609 m->qtype = sldns_buffer_read_u16(query);
610 m->qclass = sldns_buffer_read_u16(query);
611 m->local_alias = NULL;
615 /** tiny subroutine for msgreply_compare */
616 #define COMPARE_IT(x, y) \
617 if( (x) < (y) ) return -1; \
618 else if( (x) > (y) ) return +1; \
619 log_assert( (x) == (y) );
622 query_info_compare(void* m1, void* m2)
624 struct query_info* msg1 = (struct query_info*)m1;
625 struct query_info* msg2 = (struct query_info*)m2;
627 /* from most different to least different for speed */
628 COMPARE_IT(msg1->qtype, msg2->qtype);
629 if((mc = query_dname_compare(msg1->qname, msg2->qname)) != 0)
631 log_assert(msg1->qname_len == msg2->qname_len);
632 COMPARE_IT(msg1->qclass, msg2->qclass);
638 query_info_clear(struct query_info* m)
645 msgreply_sizefunc(void* k, void* d)
647 struct msgreply_entry* q = (struct msgreply_entry*)k;
648 struct reply_info* r = (struct reply_info*)d;
649 size_t s = sizeof(struct msgreply_entry) + sizeof(struct reply_info)
650 + q->key.qname_len + lock_get_mem(&q->entry.lock)
651 - sizeof(struct rrset_ref);
652 s += r->rrset_count * sizeof(struct rrset_ref);
653 s += r->rrset_count * sizeof(struct ub_packed_rrset_key*);
658 query_entry_delete(void *k, void* ATTR_UNUSED(arg))
660 struct msgreply_entry* q = (struct msgreply_entry*)k;
661 lock_rw_destroy(&q->entry.lock);
662 query_info_clear(&q->key);
667 reply_info_delete(void* d, void* ATTR_UNUSED(arg))
669 struct reply_info* r = (struct reply_info*)d;
670 if(r->reason_bogus_str) {
671 free(r->reason_bogus_str);
672 r->reason_bogus_str = NULL;
678 query_info_hash(struct query_info *q, uint16_t flags)
680 hashvalue_type h = 0xab;
681 h = hashlittle(&q->qtype, sizeof(q->qtype), h);
682 if(q->qtype == LDNS_RR_TYPE_AAAA && (flags&BIT_CD))
684 h = hashlittle(&q->qclass, sizeof(q->qclass), h);
685 h = dname_query_hash(q->qname, h);
689 struct msgreply_entry*
690 query_info_entrysetup(struct query_info* q, struct reply_info* r,
693 struct msgreply_entry* e = (struct msgreply_entry*)malloc(
694 sizeof(struct msgreply_entry));
696 memcpy(&e->key, q, sizeof(*q));
700 lock_rw_init(&e->entry.lock);
701 lock_protect(&e->entry.lock, &e->key.qname, sizeof(e->key.qname));
702 lock_protect(&e->entry.lock, &e->key.qname_len, sizeof(e->key.qname_len));
703 lock_protect(&e->entry.lock, &e->key.qtype, sizeof(e->key.qtype));
704 lock_protect(&e->entry.lock, &e->key.qclass, sizeof(e->key.qclass));
705 lock_protect(&e->entry.lock, &e->key.local_alias, sizeof(e->key.local_alias));
706 lock_protect(&e->entry.lock, &e->entry.hash, sizeof(e->entry.hash));
707 lock_protect(&e->entry.lock, &e->entry.key, sizeof(e->entry.key));
708 lock_protect(&e->entry.lock, &e->entry.data, sizeof(e->entry.data));
709 lock_protect(&e->entry.lock, e->key.qname, e->key.qname_len);
714 /** copy rrsets from replyinfo to dest replyinfo */
716 repinfo_copy_rrsets(struct reply_info* dest, struct reply_info* from,
717 struct regional* region)
720 struct packed_rrset_data* fd, *dd;
721 struct ub_packed_rrset_key* fk, *dk;
722 for(i=0; i<dest->rrset_count; i++) {
723 fk = from->rrsets[i];
724 dk = dest->rrsets[i];
725 fd = (struct packed_rrset_data*)fk->entry.data;
726 dk->entry.hash = fk->entry.hash;
730 dk->rk.dname = (uint8_t*)regional_alloc_init(region,
731 fk->rk.dname, fk->rk.dname_len);
733 dk->rk.dname = (uint8_t*)memdup(fk->rk.dname,
737 s = packed_rrset_sizeof(fd);
739 dd = (struct packed_rrset_data*)regional_alloc_init(
741 else dd = (struct packed_rrset_data*)memdup(fd, s);
744 packed_rrset_ptr_fixup(dd);
745 dk->entry.data = (void*)dd;
751 reply_info_copy(struct reply_info* rep, struct alloc_cache* alloc,
752 struct regional* region)
754 struct reply_info* cp;
755 cp = construct_reply_info_base(region, rep->flags, rep->qdcount,
756 rep->ttl, rep->prefetch_ttl, rep->serve_expired_ttl,
757 rep->an_numrrsets, rep->ns_numrrsets, rep->ar_numrrsets,
758 rep->rrset_count, rep->security, rep->reason_bogus);
762 if(rep->reason_bogus_str && *rep->reason_bogus_str != 0) {
764 cp->reason_bogus_str = (char*)regional_alloc(region,
766 * (strlen(rep->reason_bogus_str)+1));
768 cp->reason_bogus_str = malloc(sizeof(char)
769 * (strlen(rep->reason_bogus_str)+1));
771 if(!cp->reason_bogus_str) {
773 reply_info_parsedelete(cp, alloc);
776 memcpy(cp->reason_bogus_str, rep->reason_bogus_str,
777 strlen(rep->reason_bogus_str)+1);
780 /* allocate ub_key structures special or not */
781 if(!reply_info_alloc_rrset_keys(cp, alloc, region)) {
783 reply_info_parsedelete(cp, alloc);
786 if(!repinfo_copy_rrsets(cp, rep, region)) {
788 reply_info_parsedelete(cp, alloc);
795 reply_find_final_cname_target(struct query_info* qinfo, struct reply_info* rep)
797 uint8_t* sname = qinfo->qname;
798 size_t snamelen = qinfo->qname_len;
800 for(i=0; i<rep->an_numrrsets; i++) {
801 struct ub_packed_rrset_key* s = rep->rrsets[i];
802 /* follow CNAME chain (if any) */
803 if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
804 ntohs(s->rk.rrset_class) == qinfo->qclass &&
805 snamelen == s->rk.dname_len &&
806 query_dname_compare(sname, s->rk.dname) == 0) {
807 get_cname_target(s, &sname, &snamelen);
810 if(sname != qinfo->qname)
815 struct ub_packed_rrset_key*
816 reply_find_answer_rrset(struct query_info* qinfo, struct reply_info* rep)
818 uint8_t* sname = qinfo->qname;
819 size_t snamelen = qinfo->qname_len;
821 for(i=0; i<rep->an_numrrsets; i++) {
822 struct ub_packed_rrset_key* s = rep->rrsets[i];
823 /* first match type, for query of qtype cname */
824 if(ntohs(s->rk.type) == qinfo->qtype &&
825 ntohs(s->rk.rrset_class) == qinfo->qclass &&
826 snamelen == s->rk.dname_len &&
827 query_dname_compare(sname, s->rk.dname) == 0) {
830 /* follow CNAME chain (if any) */
831 if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
832 ntohs(s->rk.rrset_class) == qinfo->qclass &&
833 snamelen == s->rk.dname_len &&
834 query_dname_compare(sname, s->rk.dname) == 0) {
835 get_cname_target(s, &sname, &snamelen);
841 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
842 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
845 for(i=0; i<rep->an_numrrsets; i++) {
846 struct ub_packed_rrset_key* s = rep->rrsets[i];
847 if(ntohs(s->rk.type) == type &&
848 ntohs(s->rk.rrset_class) == dclass &&
849 namelen == s->rk.dname_len &&
850 query_dname_compare(name, s->rk.dname) == 0) {
857 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
858 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
861 for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
862 struct ub_packed_rrset_key* s = rep->rrsets[i];
863 if(ntohs(s->rk.type) == type &&
864 ntohs(s->rk.rrset_class) == dclass &&
865 namelen == s->rk.dname_len &&
866 query_dname_compare(name, s->rk.dname) == 0) {
873 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
874 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
877 for(i=0; i<rep->rrset_count; i++) {
878 struct ub_packed_rrset_key* s = rep->rrsets[i];
879 if(ntohs(s->rk.type) == type &&
880 ntohs(s->rk.rrset_class) == dclass &&
881 namelen == s->rk.dname_len &&
882 query_dname_compare(name, s->rk.dname) == 0) {
890 log_dns_msg(const char* str, struct query_info* qinfo, struct reply_info* rep)
892 /* not particularly fast but flexible, make wireformat and print */
893 sldns_buffer* buf = sldns_buffer_new(65535);
894 struct regional* region = regional_create();
895 if(!(buf && region)) {
896 log_err("%s: log_dns_msg: out of memory", str);
897 sldns_buffer_free(buf);
898 regional_destroy(region);
901 if(!reply_info_encode(qinfo, rep, 0, rep->flags, buf, 0,
902 region, 65535, 1, 0)) {
903 log_err("%s: log_dns_msg: out of memory", str);
905 char* s = sldns_wire2str_pkt(sldns_buffer_begin(buf),
906 sldns_buffer_limit(buf));
908 log_info("%s: log_dns_msg: ldns tostr failed", str);
910 log_info("%s %s", str, s);
914 sldns_buffer_free(buf);
915 regional_destroy(region);
919 log_reply_info(enum verbosity_value v, struct query_info *qinf,
920 struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
921 int cached, struct sldns_buffer *rmsg)
923 char qname_buf[LDNS_MAX_DOMAINLEN+1];
924 char clientip_buf[128];
929 uint16_t rcode = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(rmsg, 2));
934 sldns_wire2str_rcode_buf((int)rcode, rcode_buf, sizeof(rcode_buf));
935 addr_to_str(addr, addrlen, clientip_buf, sizeof(clientip_buf));
936 if(rcode == LDNS_RCODE_FORMERR)
938 if(LOG_TAG_QUERYREPLY)
939 log_reply("%s - - - %s - - - ", clientip_buf, rcode_buf);
940 else log_info("%s - - - %s - - - ", clientip_buf, rcode_buf);
943 dname_str(qinf->qname, qname_buf);
944 else snprintf(qname_buf, sizeof(qname_buf), "null");
945 pktlen = sldns_buffer_limit(rmsg);
946 sldns_wire2str_type_buf(qinf->qtype, type_buf, sizeof(type_buf));
947 sldns_wire2str_class_buf(qinf->qclass, class_buf, sizeof(class_buf));
948 if(LOG_TAG_QUERYREPLY)
949 log_reply("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d",
950 clientip_buf, qname_buf, type_buf, class_buf,
951 rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen);
952 else log_info("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d",
953 clientip_buf, qname_buf, type_buf, class_buf,
954 rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen);
959 log_query_info(enum verbosity_value v, const char* str,
960 struct query_info* qinf)
962 log_nametypeclass(v, str, qinf->qname, qinf->qtype, qinf->qclass);
966 reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep)
968 /* check only answer section rrs for matching cname chain.
969 * the cache may return changed rdata, but owner names are untouched.*/
971 uint8_t* sname = qinfo->qname;
972 size_t snamelen = qinfo->qname_len;
973 for(i=0; i<rep->an_numrrsets; i++) {
974 uint16_t t = ntohs(rep->rrsets[i]->rk.type);
975 if(t == LDNS_RR_TYPE_DNAME)
976 continue; /* skip dnames; note TTL 0 not cached */
977 /* verify that owner matches current sname */
978 if(query_dname_compare(sname, rep->rrsets[i]->rk.dname) != 0){
979 /* cname chain broken */
982 /* if this is a cname; move on */
983 if(t == LDNS_RR_TYPE_CNAME) {
984 get_cname_target(rep->rrsets[i], &sname, &snamelen);
991 reply_all_rrsets_secure(struct reply_info* rep)
994 for(i=0; i<rep->rrset_count; i++) {
995 if( ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
996 ->security != sec_status_secure )
1003 parse_reply_in_temp_region(sldns_buffer* pkt, struct regional* region,
1004 struct query_info* qi)
1006 struct reply_info* rep;
1007 struct msg_parse* msg;
1008 if(!(msg = regional_alloc(region, sizeof(*msg)))) {
1011 memset(msg, 0, sizeof(*msg));
1012 sldns_buffer_set_position(pkt, 0);
1013 if(parse_packet(pkt, msg, region) != 0){
1016 if(!parse_create_msg(pkt, msg, NULL, qi, &rep, region)) {
1022 int edns_opt_list_append_ede(struct edns_option** list, struct regional* region,
1023 sldns_ede_code code, const char *txt)
1025 struct edns_option** prevp;
1026 struct edns_option* opt;
1027 size_t txt_len = txt ? strlen(txt) : 0;
1029 /* allocate new element */
1030 opt = (struct edns_option*)regional_alloc(region, sizeof(*opt));
1034 opt->opt_code = LDNS_EDNS_EDE;
1035 opt->opt_len = txt_len + sizeof(uint16_t);
1036 opt->opt_data = regional_alloc(region, txt_len + sizeof(uint16_t));
1039 sldns_write_uint16(opt->opt_data, (uint16_t)code);
1041 memmove(opt->opt_data + 2, txt, txt_len);
1043 /* append at end of list */
1045 while(*prevp != NULL)
1046 prevp = &((*prevp)->next);
1047 verbose(VERB_ALGO, "attached EDE code: %d with message: %s", code, (txt?txt:"\"\""));
1052 int edns_opt_list_append_keepalive(struct edns_option** list, int msec,
1053 struct regional* region)
1055 uint8_t data[2]; /* For keepalive value */
1056 data[0] = (uint8_t)((msec >> 8) & 0xff);
1057 data[1] = (uint8_t)(msec & 0xff);
1058 return edns_opt_list_append(list, LDNS_EDNS_KEEPALIVE, sizeof(data),
1062 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
1063 uint8_t* data, struct regional* region)
1065 struct edns_option** prevp;
1066 struct edns_option* opt;
1068 /* allocate new element */
1069 opt = (struct edns_option*)regional_alloc(region, sizeof(*opt));
1073 opt->opt_code = code;
1075 opt->opt_data = NULL;
1077 opt->opt_data = regional_alloc_init(region, data, len);
1082 /* append at end of list */
1084 while(*prevp != NULL) {
1085 prevp = &((*prevp)->next);
1091 int edns_opt_list_remove(struct edns_option** list, uint16_t code)
1093 /* The list should already be allocated in a region. Freeing the
1094 * allocated space in a region is not possible. We just unlink the
1095 * required elements and they will be freed together with the region. */
1097 struct edns_option* prev;
1098 struct edns_option* curr;
1099 if(!list || !(*list)) return 0;
1101 /* Unlink and repoint if the element(s) are first in list */
1102 while(list && *list && (*list)->opt_code == code) {
1103 *list = (*list)->next;
1106 if(!list || !(*list)) return 1;
1107 /* Unlink elements and reattach the list */
1109 curr = (*list)->next;
1110 while(curr != NULL) {
1111 if(curr->opt_code == code) {
1112 prev->next = curr->next;
1122 static int inplace_cb_reply_call_generic(
1123 struct inplace_cb* callback_list, enum inplace_cb_list_type type,
1124 struct query_info* qinfo, struct module_qstate* qstate,
1125 struct reply_info* rep, int rcode, struct edns_data* edns,
1126 struct comm_reply* repinfo, struct regional* region,
1127 struct timeval* start_time)
1129 struct inplace_cb* cb;
1130 struct edns_option* opt_list_out = NULL;
1131 #if defined(EXPORT_ALL_SYMBOLS)
1132 (void)type; /* param not used when fptr_ok disabled */
1135 opt_list_out = qstate->edns_opts_front_out;
1136 for(cb=callback_list; cb; cb=cb->next) {
1137 fptr_ok(fptr_whitelist_inplace_cb_reply_generic(
1138 (inplace_cb_reply_func_type*)cb->cb, type));
1139 (void)(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep,
1140 rcode, edns, &opt_list_out, repinfo, region, start_time, cb->id, cb->cb_arg);
1142 edns->opt_list_inplace_cb_out = opt_list_out;
1146 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
1147 struct module_qstate* qstate, struct reply_info* rep, int rcode,
1148 struct edns_data* edns, struct comm_reply* repinfo, struct regional* region,
1149 struct timeval* start_time)
1151 return inplace_cb_reply_call_generic(
1152 env->inplace_cb_lists[inplace_cb_reply], inplace_cb_reply, qinfo,
1153 qstate, rep, rcode, edns, repinfo, region, start_time);
1156 int inplace_cb_reply_cache_call(struct module_env* env,
1157 struct query_info* qinfo, struct module_qstate* qstate,
1158 struct reply_info* rep, int rcode, struct edns_data* edns,
1159 struct comm_reply* repinfo, struct regional* region,
1160 struct timeval* start_time)
1162 return inplace_cb_reply_call_generic(
1163 env->inplace_cb_lists[inplace_cb_reply_cache], inplace_cb_reply_cache,
1164 qinfo, qstate, rep, rcode, edns, repinfo, region, start_time);
1167 int inplace_cb_reply_local_call(struct module_env* env,
1168 struct query_info* qinfo, struct module_qstate* qstate,
1169 struct reply_info* rep, int rcode, struct edns_data* edns,
1170 struct comm_reply* repinfo, struct regional* region,
1171 struct timeval* start_time)
1173 return inplace_cb_reply_call_generic(
1174 env->inplace_cb_lists[inplace_cb_reply_local], inplace_cb_reply_local,
1175 qinfo, qstate, rep, rcode, edns, repinfo, region, start_time);
1178 int inplace_cb_reply_servfail_call(struct module_env* env,
1179 struct query_info* qinfo, struct module_qstate* qstate,
1180 struct reply_info* rep, int rcode, struct edns_data* edns,
1181 struct comm_reply* repinfo, struct regional* region,
1182 struct timeval* start_time)
1184 /* We are going to servfail. Remove any potential edns options. */
1186 qstate->edns_opts_front_out = NULL;
1187 return inplace_cb_reply_call_generic(
1188 env->inplace_cb_lists[inplace_cb_reply_servfail],
1189 inplace_cb_reply_servfail, qinfo, qstate, rep, rcode, edns, repinfo,
1190 region, start_time);
1193 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
1194 uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
1195 uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
1196 struct regional* region)
1198 struct inplace_cb* cb = env->inplace_cb_lists[inplace_cb_query];
1199 for(; cb; cb=cb->next) {
1200 fptr_ok(fptr_whitelist_inplace_cb_query(
1201 (inplace_cb_query_func_type*)cb->cb));
1202 (void)(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags,
1203 qstate, addr, addrlen, zone, zonelen, region,
1204 cb->id, cb->cb_arg);
1209 int inplace_cb_edns_back_parsed_call(struct module_env* env,
1210 struct module_qstate* qstate)
1212 struct inplace_cb* cb =
1213 env->inplace_cb_lists[inplace_cb_edns_back_parsed];
1214 for(; cb; cb=cb->next) {
1215 fptr_ok(fptr_whitelist_inplace_cb_edns_back_parsed(
1216 (inplace_cb_edns_back_parsed_func_type*)cb->cb));
1217 (void)(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate,
1218 cb->id, cb->cb_arg);
1223 int inplace_cb_query_response_call(struct module_env* env,
1224 struct module_qstate* qstate, struct dns_msg* response) {
1225 struct inplace_cb* cb =
1226 env->inplace_cb_lists[inplace_cb_query_response];
1227 for(; cb; cb=cb->next) {
1228 fptr_ok(fptr_whitelist_inplace_cb_query_response(
1229 (inplace_cb_query_response_func_type*)cb->cb));
1230 (void)(*(inplace_cb_query_response_func_type*)cb->cb)(qstate,
1231 response, cb->id, cb->cb_arg);
1236 struct edns_option* edns_opt_copy_region(struct edns_option* list,
1237 struct regional* region)
1239 struct edns_option* result = NULL, *cur = NULL, *s;
1241 /* copy edns option structure */
1242 s = regional_alloc_init(region, list, sizeof(*list));
1246 /* copy option data */
1248 s->opt_data = regional_alloc_init(region, s->opt_data,
1254 /* link into list */
1260 /* examine next element */
1266 struct edns_option* edns_opt_copy_filter_region(struct edns_option* list,
1267 uint16_t* filter_list, size_t filter_list_len, struct regional* region)
1269 struct edns_option* result = NULL, *cur = NULL, *s;
1272 for(i=0; i<filter_list_len; i++)
1273 if(filter_list[i] == list->opt_code) goto found;
1274 if(i == filter_list_len) goto next;
1276 /* copy edns option structure */
1277 s = regional_alloc_init(region, list, sizeof(*list));
1281 /* copy option data */
1283 s->opt_data = regional_alloc_init(region, s->opt_data,
1289 /* link into list */
1296 /* examine next element */
1302 int edns_opt_compare(struct edns_option* p, struct edns_option* q)
1304 if(!p && !q) return 0;
1308 if(p->opt_code != q->opt_code)
1309 return (int)q->opt_code - (int)p->opt_code;
1310 if(p->opt_len != q->opt_len)
1311 return (int)q->opt_len - (int)p->opt_len;
1313 return memcmp(p->opt_data, q->opt_data, p->opt_len);
1317 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q)
1321 r = edns_opt_compare(p, q);
1328 /* uneven length lists */
1335 void edns_opt_list_free(struct edns_option* list)
1337 struct edns_option* n;
1339 free(list->opt_data);
1346 struct edns_option* edns_opt_copy_alloc(struct edns_option* list)
1348 struct edns_option* result = NULL, *cur = NULL, *s;
1350 /* copy edns option structure */
1351 s = memdup(list, sizeof(*list));
1353 edns_opt_list_free(result);
1358 /* copy option data */
1360 s->opt_data = memdup(s->opt_data, s->opt_len);
1363 edns_opt_list_free(result);
1368 /* link into list */
1374 /* examine next element */
1380 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code)
1382 struct edns_option* p;
1383 for(p=list; p; p=p->next) {
1384 if(p->opt_code == code)