msgreply.c revision 1.1.1.5 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.4 christos /** Time to serve records after expiration */
65 1.1.1.4 christos time_t SERVE_EXPIRED_TTL = 0;
66 1.1 christos
67 1.1 christos /** allocate qinfo, return 0 on error */
68 1.1 christos static int
69 1.1 christos parse_create_qinfo(sldns_buffer* pkt, struct msg_parse* msg,
70 1.1 christos struct query_info* qinf, struct regional* region)
71 1.1 christos {
72 1.1 christos if(msg->qname) {
73 1.1 christos if(region)
74 1.1 christos qinf->qname = (uint8_t*)regional_alloc(region,
75 1.1 christos msg->qname_len);
76 1.1 christos else qinf->qname = (uint8_t*)malloc(msg->qname_len);
77 1.1 christos if(!qinf->qname) return 0;
78 1.1 christos dname_pkt_copy(pkt, qinf->qname, msg->qname);
79 1.1 christos } else qinf->qname = 0;
80 1.1 christos qinf->qname_len = msg->qname_len;
81 1.1 christos qinf->qtype = msg->qtype;
82 1.1 christos qinf->qclass = msg->qclass;
83 1.1.1.2 christos qinf->local_alias = NULL;
84 1.1 christos return 1;
85 1.1 christos }
86 1.1 christos
87 1.1 christos /** constructor for replyinfo */
88 1.1 christos struct reply_info*
89 1.1 christos construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd,
90 1.1.1.4 christos time_t ttl, time_t prettl, time_t expttl, size_t an, size_t ns,
91 1.1.1.4 christos size_t ar, size_t total, enum sec_status sec)
92 1.1 christos {
93 1.1 christos struct reply_info* rep;
94 1.1 christos /* rrset_count-1 because the first ref is part of the struct. */
95 1.1 christos size_t s = sizeof(struct reply_info) - sizeof(struct rrset_ref) +
96 1.1 christos sizeof(struct ub_packed_rrset_key*) * total;
97 1.1 christos if(total >= RR_COUNT_MAX) return NULL; /* sanity check on numRRS*/
98 1.1 christos if(region)
99 1.1 christos rep = (struct reply_info*)regional_alloc(region, s);
100 1.1 christos else rep = (struct reply_info*)malloc(s +
101 1.1 christos sizeof(struct rrset_ref) * (total));
102 1.1 christos if(!rep)
103 1.1 christos return NULL;
104 1.1 christos rep->flags = flags;
105 1.1 christos rep->qdcount = qd;
106 1.1 christos rep->ttl = ttl;
107 1.1 christos rep->prefetch_ttl = prettl;
108 1.1.1.4 christos rep->serve_expired_ttl = expttl;
109 1.1 christos rep->an_numrrsets = an;
110 1.1 christos rep->ns_numrrsets = ns;
111 1.1 christos rep->ar_numrrsets = ar;
112 1.1 christos rep->rrset_count = total;
113 1.1 christos rep->security = sec;
114 1.1 christos rep->authoritative = 0;
115 1.1 christos /* array starts after the refs */
116 1.1 christos if(region)
117 1.1 christos rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[0]);
118 1.1 christos else rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[total]);
119 1.1 christos /* zero the arrays to assist cleanup in case of malloc failure */
120 1.1 christos memset( rep->rrsets, 0, sizeof(struct ub_packed_rrset_key*) * total);
121 1.1 christos if(!region)
122 1.1 christos memset( &rep->ref[0], 0, sizeof(struct rrset_ref) * total);
123 1.1 christos return rep;
124 1.1 christos }
125 1.1 christos
126 1.1 christos /** allocate replyinfo, return 0 on error */
127 1.1 christos static int
128 1.1 christos parse_create_repinfo(struct msg_parse* msg, struct reply_info** rep,
129 1.1 christos struct regional* region)
130 1.1 christos {
131 1.1 christos *rep = construct_reply_info_base(region, msg->flags, msg->qdcount, 0,
132 1.1.1.4 christos 0, 0, msg->an_rrsets, msg->ns_rrsets, msg->ar_rrsets,
133 1.1 christos msg->rrset_count, sec_status_unchecked);
134 1.1 christos if(!*rep)
135 1.1 christos return 0;
136 1.1 christos return 1;
137 1.1 christos }
138 1.1 christos
139 1.1.1.2 christos int
140 1.1.1.2 christos reply_info_alloc_rrset_keys(struct reply_info* rep, struct alloc_cache* alloc,
141 1.1 christos struct regional* region)
142 1.1 christos {
143 1.1 christos size_t i;
144 1.1 christos for(i=0; i<rep->rrset_count; i++) {
145 1.1 christos if(region) {
146 1.1 christos rep->rrsets[i] = (struct ub_packed_rrset_key*)
147 1.1 christos regional_alloc(region,
148 1.1 christos sizeof(struct ub_packed_rrset_key));
149 1.1 christos if(rep->rrsets[i]) {
150 1.1 christos memset(rep->rrsets[i], 0,
151 1.1 christos sizeof(struct ub_packed_rrset_key));
152 1.1 christos rep->rrsets[i]->entry.key = rep->rrsets[i];
153 1.1 christos }
154 1.1 christos }
155 1.1 christos else rep->rrsets[i] = alloc_special_obtain(alloc);
156 1.1 christos if(!rep->rrsets[i])
157 1.1 christos return 0;
158 1.1 christos rep->rrsets[i]->entry.data = NULL;
159 1.1 christos }
160 1.1 christos return 1;
161 1.1 christos }
162 1.1 christos
163 1.1 christos /** find the minimumttl in the rdata of SOA record */
164 1.1 christos static time_t
165 1.1 christos soa_find_minttl(struct rr_parse* rr)
166 1.1 christos {
167 1.1 christos uint16_t rlen = sldns_read_uint16(rr->ttl_data+4);
168 1.1 christos if(rlen < 20)
169 1.1 christos return 0; /* rdata too small for SOA (dname, dname, 5*32bit) */
170 1.1 christos /* minimum TTL is the last 32bit value in the rdata of the record */
171 1.1 christos /* at position ttl_data + 4(ttl) + 2(rdatalen) + rdatalen - 4(timeval)*/
172 1.1 christos return (time_t)sldns_read_uint32(rr->ttl_data+6+rlen-4);
173 1.1 christos }
174 1.1 christos
175 1.1 christos /** do the rdata copy */
176 1.1 christos static int
177 1.1 christos rdata_copy(sldns_buffer* pkt, struct packed_rrset_data* data, uint8_t* to,
178 1.1 christos struct rr_parse* rr, time_t* rr_ttl, uint16_t type,
179 1.1 christos sldns_pkt_section section)
180 1.1 christos {
181 1.1 christos uint16_t pkt_len;
182 1.1 christos const sldns_rr_descriptor* desc;
183 1.1 christos
184 1.1 christos *rr_ttl = sldns_read_uint32(rr->ttl_data);
185 1.1 christos /* RFC 2181 Section 8. if msb of ttl is set treat as if zero. */
186 1.1 christos if(*rr_ttl & 0x80000000U)
187 1.1 christos *rr_ttl = 0;
188 1.1 christos if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) {
189 1.1 christos /* negative response. see if TTL of SOA record larger than the
190 1.1 christos * minimum-ttl in the rdata of the SOA record */
191 1.1 christos if(*rr_ttl > soa_find_minttl(rr))
192 1.1 christos *rr_ttl = soa_find_minttl(rr);
193 1.1 christos if(*rr_ttl > MAX_NEG_TTL)
194 1.1 christos *rr_ttl = MAX_NEG_TTL;
195 1.1 christos }
196 1.1 christos if(*rr_ttl < MIN_TTL)
197 1.1 christos *rr_ttl = MIN_TTL;
198 1.1.1.4 christos if(*rr_ttl > MAX_TTL)
199 1.1.1.4 christos *rr_ttl = MAX_TTL;
200 1.1 christos if(*rr_ttl < data->ttl)
201 1.1 christos data->ttl = *rr_ttl;
202 1.1 christos
203 1.1 christos if(rr->outside_packet) {
204 1.1 christos /* uncompressed already, only needs copy */
205 1.1 christos memmove(to, rr->ttl_data+sizeof(uint32_t), rr->size);
206 1.1 christos return 1;
207 1.1 christos }
208 1.1 christos
209 1.1 christos sldns_buffer_set_position(pkt, (size_t)
210 1.1 christos (rr->ttl_data - sldns_buffer_begin(pkt) + sizeof(uint32_t)));
211 1.1 christos /* insert decompressed size into rdata len stored in memory */
212 1.1 christos /* -2 because rdatalen bytes are not included. */
213 1.1 christos pkt_len = htons(rr->size - 2);
214 1.1 christos memmove(to, &pkt_len, sizeof(uint16_t));
215 1.1 christos to += 2;
216 1.1 christos /* read packet rdata len */
217 1.1 christos pkt_len = sldns_buffer_read_u16(pkt);
218 1.1 christos if(sldns_buffer_remaining(pkt) < pkt_len)
219 1.1 christos return 0;
220 1.1 christos desc = sldns_rr_descript(type);
221 1.1 christos if(pkt_len > 0 && desc && desc->_dname_count > 0) {
222 1.1 christos int count = (int)desc->_dname_count;
223 1.1 christos int rdf = 0;
224 1.1 christos size_t len;
225 1.1 christos size_t oldpos;
226 1.1 christos /* decompress dnames. */
227 1.1 christos while(pkt_len > 0 && count) {
228 1.1 christos switch(desc->_wireformat[rdf]) {
229 1.1 christos case LDNS_RDF_TYPE_DNAME:
230 1.1 christos oldpos = sldns_buffer_position(pkt);
231 1.1 christos dname_pkt_copy(pkt, to,
232 1.1 christos sldns_buffer_current(pkt));
233 1.1 christos to += pkt_dname_len(pkt);
234 1.1 christos pkt_len -= sldns_buffer_position(pkt)-oldpos;
235 1.1 christos count--;
236 1.1 christos len = 0;
237 1.1 christos break;
238 1.1 christos case LDNS_RDF_TYPE_STR:
239 1.1 christos len = sldns_buffer_current(pkt)[0] + 1;
240 1.1 christos break;
241 1.1 christos default:
242 1.1 christos len = get_rdf_size(desc->_wireformat[rdf]);
243 1.1 christos break;
244 1.1 christos }
245 1.1 christos if(len) {
246 1.1.1.5 christos log_assert(len <= pkt_len);
247 1.1 christos memmove(to, sldns_buffer_current(pkt), len);
248 1.1 christos to += len;
249 1.1 christos sldns_buffer_skip(pkt, (ssize_t)len);
250 1.1 christos pkt_len -= len;
251 1.1 christos }
252 1.1 christos rdf++;
253 1.1 christos }
254 1.1 christos }
255 1.1 christos /* copy remaining rdata */
256 1.1 christos if(pkt_len > 0)
257 1.1 christos memmove(to, sldns_buffer_current(pkt), pkt_len);
258 1.1 christos
259 1.1 christos return 1;
260 1.1 christos }
261 1.1 christos
262 1.1 christos /** copy over the data into packed rrset */
263 1.1 christos static int
264 1.1 christos parse_rr_copy(sldns_buffer* pkt, struct rrset_parse* pset,
265 1.1 christos struct packed_rrset_data* data)
266 1.1 christos {
267 1.1 christos size_t i;
268 1.1 christos struct rr_parse* rr = pset->rr_first;
269 1.1 christos uint8_t* nextrdata;
270 1.1 christos size_t total = pset->rr_count + pset->rrsig_count;
271 1.1 christos data->ttl = MAX_TTL;
272 1.1 christos data->count = pset->rr_count;
273 1.1 christos data->rrsig_count = pset->rrsig_count;
274 1.1 christos data->trust = rrset_trust_none;
275 1.1 christos data->security = sec_status_unchecked;
276 1.1 christos /* layout: struct - rr_len - rr_data - rr_ttl - rdata - rrsig */
277 1.1 christos data->rr_len = (size_t*)((uint8_t*)data +
278 1.1 christos sizeof(struct packed_rrset_data));
279 1.1 christos data->rr_data = (uint8_t**)&(data->rr_len[total]);
280 1.1 christos data->rr_ttl = (time_t*)&(data->rr_data[total]);
281 1.1 christos nextrdata = (uint8_t*)&(data->rr_ttl[total]);
282 1.1 christos for(i=0; i<data->count; i++) {
283 1.1 christos data->rr_len[i] = rr->size;
284 1.1 christos data->rr_data[i] = nextrdata;
285 1.1 christos nextrdata += rr->size;
286 1.1 christos if(!rdata_copy(pkt, data, data->rr_data[i], rr,
287 1.1 christos &data->rr_ttl[i], pset->type, pset->section))
288 1.1 christos return 0;
289 1.1 christos rr = rr->next;
290 1.1 christos }
291 1.1 christos /* if rrsig, its rdata is at nextrdata */
292 1.1 christos rr = pset->rrsig_first;
293 1.1 christos for(i=data->count; i<total; i++) {
294 1.1 christos data->rr_len[i] = rr->size;
295 1.1 christos data->rr_data[i] = nextrdata;
296 1.1 christos nextrdata += rr->size;
297 1.1 christos if(!rdata_copy(pkt, data, data->rr_data[i], rr,
298 1.1 christos &data->rr_ttl[i], LDNS_RR_TYPE_RRSIG, pset->section))
299 1.1 christos return 0;
300 1.1 christos rr = rr->next;
301 1.1 christos }
302 1.1 christos return 1;
303 1.1 christos }
304 1.1 christos
305 1.1 christos /** create rrset return 0 on failure */
306 1.1 christos static int
307 1.1 christos parse_create_rrset(sldns_buffer* pkt, struct rrset_parse* pset,
308 1.1 christos struct packed_rrset_data** data, struct regional* region)
309 1.1 christos {
310 1.1 christos /* allocate */
311 1.1 christos size_t s;
312 1.1 christos if(pset->rr_count > RR_COUNT_MAX || pset->rrsig_count > RR_COUNT_MAX ||
313 1.1 christos pset->size > RR_COUNT_MAX)
314 1.1 christos return 0; /* protect against integer overflow */
315 1.1 christos s = sizeof(struct packed_rrset_data) +
316 1.1 christos (pset->rr_count + pset->rrsig_count) *
317 1.1 christos (sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t)) +
318 1.1 christos pset->size;
319 1.1 christos if(region)
320 1.1 christos *data = regional_alloc(region, s);
321 1.1 christos else *data = malloc(s);
322 1.1 christos if(!*data)
323 1.1 christos return 0;
324 1.1 christos /* copy & decompress */
325 1.1 christos if(!parse_rr_copy(pkt, pset, *data)) {
326 1.1 christos if(!region) free(*data);
327 1.1 christos return 0;
328 1.1 christos }
329 1.1 christos return 1;
330 1.1 christos }
331 1.1 christos
332 1.1 christos /** get trust value for rrset */
333 1.1 christos static enum rrset_trust
334 1.1 christos get_rrset_trust(struct msg_parse* msg, struct rrset_parse* rrset)
335 1.1 christos {
336 1.1 christos uint16_t AA = msg->flags & BIT_AA;
337 1.1 christos if(rrset->section == LDNS_SECTION_ANSWER) {
338 1.1 christos if(AA) {
339 1.1 christos /* RFC2181 says remainder of CNAME chain is nonauth*/
340 1.1 christos if(msg->rrset_first &&
341 1.1 christos msg->rrset_first->section==LDNS_SECTION_ANSWER
342 1.1 christos && msg->rrset_first->type==LDNS_RR_TYPE_CNAME){
343 1.1 christos if(rrset == msg->rrset_first)
344 1.1 christos return rrset_trust_ans_AA;
345 1.1 christos else return rrset_trust_ans_noAA;
346 1.1 christos }
347 1.1 christos if(msg->rrset_first &&
348 1.1 christos msg->rrset_first->section==LDNS_SECTION_ANSWER
349 1.1 christos && msg->rrset_first->type==LDNS_RR_TYPE_DNAME){
350 1.1 christos if(rrset == msg->rrset_first ||
351 1.1 christos rrset == msg->rrset_first->rrset_all_next)
352 1.1 christos return rrset_trust_ans_AA;
353 1.1 christos else return rrset_trust_ans_noAA;
354 1.1 christos }
355 1.1 christos return rrset_trust_ans_AA;
356 1.1 christos }
357 1.1 christos else return rrset_trust_ans_noAA;
358 1.1 christos } else if(rrset->section == LDNS_SECTION_AUTHORITY) {
359 1.1 christos if(AA) return rrset_trust_auth_AA;
360 1.1 christos else return rrset_trust_auth_noAA;
361 1.1 christos } else {
362 1.1 christos /* addit section */
363 1.1 christos if(AA) return rrset_trust_add_AA;
364 1.1 christos else return rrset_trust_add_noAA;
365 1.1 christos }
366 1.1 christos /* NOTREACHED */
367 1.1 christos return rrset_trust_none;
368 1.1 christos }
369 1.1 christos
370 1.1 christos int
371 1.1 christos parse_copy_decompress_rrset(sldns_buffer* pkt, struct msg_parse* msg,
372 1.1 christos struct rrset_parse *pset, struct regional* region,
373 1.1 christos struct ub_packed_rrset_key* pk)
374 1.1 christos {
375 1.1 christos struct packed_rrset_data* data;
376 1.1 christos pk->rk.flags = pset->flags;
377 1.1 christos pk->rk.dname_len = pset->dname_len;
378 1.1 christos if(region)
379 1.1 christos pk->rk.dname = (uint8_t*)regional_alloc(
380 1.1 christos region, pset->dname_len);
381 1.1 christos else pk->rk.dname =
382 1.1 christos (uint8_t*)malloc(pset->dname_len);
383 1.1 christos if(!pk->rk.dname)
384 1.1 christos return 0;
385 1.1 christos /** copy & decompress dname */
386 1.1 christos dname_pkt_copy(pkt, pk->rk.dname, pset->dname);
387 1.1 christos /** copy over type and class */
388 1.1 christos pk->rk.type = htons(pset->type);
389 1.1 christos pk->rk.rrset_class = pset->rrset_class;
390 1.1 christos /** read data part. */
391 1.1 christos if(!parse_create_rrset(pkt, pset, &data, region))
392 1.1 christos return 0;
393 1.1 christos pk->entry.data = (void*)data;
394 1.1 christos pk->entry.key = (void*)pk;
395 1.1 christos pk->entry.hash = pset->hash;
396 1.1 christos data->trust = get_rrset_trust(msg, pset);
397 1.1 christos return 1;
398 1.1 christos }
399 1.1 christos
400 1.1 christos /**
401 1.1 christos * Copy and decompress rrs
402 1.1 christos * @param pkt: the packet for compression pointer resolution.
403 1.1 christos * @param msg: the parsed message
404 1.1 christos * @param rep: reply info to put rrs into.
405 1.1 christos * @param region: if not NULL, used for allocation.
406 1.1 christos * @return 0 on failure.
407 1.1 christos */
408 1.1 christos static int
409 1.1 christos parse_copy_decompress(sldns_buffer* pkt, struct msg_parse* msg,
410 1.1 christos struct reply_info* rep, struct regional* region)
411 1.1 christos {
412 1.1 christos size_t i;
413 1.1 christos struct rrset_parse *pset = msg->rrset_first;
414 1.1 christos struct packed_rrset_data* data;
415 1.1 christos log_assert(rep);
416 1.1 christos rep->ttl = MAX_TTL;
417 1.1 christos rep->security = sec_status_unchecked;
418 1.1 christos if(rep->rrset_count == 0)
419 1.1 christos rep->ttl = NORR_TTL;
420 1.1 christos
421 1.1 christos for(i=0; i<rep->rrset_count; i++) {
422 1.1 christos if(!parse_copy_decompress_rrset(pkt, msg, pset, region,
423 1.1 christos rep->rrsets[i]))
424 1.1 christos return 0;
425 1.1 christos data = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
426 1.1 christos if(data->ttl < rep->ttl)
427 1.1 christos rep->ttl = data->ttl;
428 1.1 christos
429 1.1 christos pset = pset->rrset_all_next;
430 1.1 christos }
431 1.1 christos rep->prefetch_ttl = PREFETCH_TTL_CALC(rep->ttl);
432 1.1.1.4 christos rep->serve_expired_ttl = rep->ttl + SERVE_EXPIRED_TTL;
433 1.1 christos return 1;
434 1.1 christos }
435 1.1 christos
436 1.1 christos int
437 1.1 christos parse_create_msg(sldns_buffer* pkt, struct msg_parse* msg,
438 1.1 christos struct alloc_cache* alloc, struct query_info* qinf,
439 1.1 christos struct reply_info** rep, struct regional* region)
440 1.1 christos {
441 1.1 christos log_assert(pkt && msg);
442 1.1 christos if(!parse_create_qinfo(pkt, msg, qinf, region))
443 1.1 christos return 0;
444 1.1 christos if(!parse_create_repinfo(msg, rep, region))
445 1.1 christos return 0;
446 1.1.1.4 christos if(!reply_info_alloc_rrset_keys(*rep, alloc, region)) {
447 1.1.1.4 christos if(!region) reply_info_parsedelete(*rep, alloc);
448 1.1 christos return 0;
449 1.1.1.4 christos }
450 1.1.1.4 christos if(!parse_copy_decompress(pkt, msg, *rep, region)) {
451 1.1.1.4 christos if(!region) reply_info_parsedelete(*rep, alloc);
452 1.1 christos return 0;
453 1.1.1.4 christos }
454 1.1 christos return 1;
455 1.1 christos }
456 1.1 christos
457 1.1 christos int reply_info_parse(sldns_buffer* pkt, struct alloc_cache* alloc,
458 1.1 christos struct query_info* qinf, struct reply_info** rep,
459 1.1 christos struct regional* region, struct edns_data* edns)
460 1.1 christos {
461 1.1 christos /* use scratch pad region-allocator during parsing. */
462 1.1 christos struct msg_parse* msg;
463 1.1 christos int ret;
464 1.1 christos
465 1.1 christos qinf->qname = NULL;
466 1.1.1.2 christos qinf->local_alias = NULL;
467 1.1 christos *rep = NULL;
468 1.1 christos if(!(msg = regional_alloc(region, sizeof(*msg)))) {
469 1.1 christos return LDNS_RCODE_SERVFAIL;
470 1.1 christos }
471 1.1 christos memset(msg, 0, sizeof(*msg));
472 1.1 christos
473 1.1 christos sldns_buffer_set_position(pkt, 0);
474 1.1 christos if((ret = parse_packet(pkt, msg, region)) != 0) {
475 1.1 christos return ret;
476 1.1 christos }
477 1.1 christos if((ret = parse_extract_edns(msg, edns, region)) != 0)
478 1.1 christos return ret;
479 1.1 christos
480 1.1 christos /* parse OK, allocate return structures */
481 1.1 christos /* this also performs dname decompression */
482 1.1 christos if(!parse_create_msg(pkt, msg, alloc, qinf, rep, NULL)) {
483 1.1 christos query_info_clear(qinf);
484 1.1 christos reply_info_parsedelete(*rep, alloc);
485 1.1 christos *rep = NULL;
486 1.1 christos return LDNS_RCODE_SERVFAIL;
487 1.1 christos }
488 1.1 christos return 0;
489 1.1 christos }
490 1.1 christos
491 1.1 christos /** helper compare function to sort in lock order */
492 1.1 christos static int
493 1.1 christos reply_info_sortref_cmp(const void* a, const void* b)
494 1.1 christos {
495 1.1 christos struct rrset_ref* x = (struct rrset_ref*)a;
496 1.1 christos struct rrset_ref* y = (struct rrset_ref*)b;
497 1.1 christos if(x->key < y->key) return -1;
498 1.1 christos if(x->key > y->key) return 1;
499 1.1 christos return 0;
500 1.1 christos }
501 1.1 christos
502 1.1 christos void
503 1.1 christos reply_info_sortref(struct reply_info* rep)
504 1.1 christos {
505 1.1 christos qsort(&rep->ref[0], rep->rrset_count, sizeof(struct rrset_ref),
506 1.1 christos reply_info_sortref_cmp);
507 1.1 christos }
508 1.1 christos
509 1.1 christos void
510 1.1 christos reply_info_set_ttls(struct reply_info* rep, time_t timenow)
511 1.1 christos {
512 1.1 christos size_t i, j;
513 1.1 christos rep->ttl += timenow;
514 1.1 christos rep->prefetch_ttl += timenow;
515 1.1.1.4 christos rep->serve_expired_ttl += timenow;
516 1.1 christos for(i=0; i<rep->rrset_count; i++) {
517 1.1 christos struct packed_rrset_data* data = (struct packed_rrset_data*)
518 1.1 christos rep->ref[i].key->entry.data;
519 1.1 christos if(i>0 && rep->ref[i].key == rep->ref[i-1].key)
520 1.1 christos continue;
521 1.1 christos data->ttl += timenow;
522 1.1 christos for(j=0; j<data->count + data->rrsig_count; j++) {
523 1.1 christos data->rr_ttl[j] += timenow;
524 1.1 christos }
525 1.1 christos }
526 1.1 christos }
527 1.1 christos
528 1.1 christos void
529 1.1 christos reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc)
530 1.1 christos {
531 1.1 christos size_t i;
532 1.1 christos if(!rep)
533 1.1 christos return;
534 1.1 christos /* no need to lock, since not shared in hashtables. */
535 1.1 christos for(i=0; i<rep->rrset_count; i++) {
536 1.1 christos ub_packed_rrset_parsedelete(rep->rrsets[i], alloc);
537 1.1 christos }
538 1.1 christos free(rep);
539 1.1 christos }
540 1.1 christos
541 1.1 christos int
542 1.1 christos query_info_parse(struct query_info* m, sldns_buffer* query)
543 1.1 christos {
544 1.1 christos uint8_t* q = sldns_buffer_begin(query);
545 1.1 christos /* minimum size: header + \0 + qtype + qclass */
546 1.1 christos if(sldns_buffer_limit(query) < LDNS_HEADER_SIZE + 5)
547 1.1 christos return 0;
548 1.1.1.3 christos if((LDNS_OPCODE_WIRE(q) != LDNS_PACKET_QUERY && LDNS_OPCODE_WIRE(q) !=
549 1.1.1.3 christos LDNS_PACKET_NOTIFY) || LDNS_QDCOUNT(q) != 1 ||
550 1.1.1.3 christos sldns_buffer_position(query) != 0)
551 1.1 christos return 0;
552 1.1 christos sldns_buffer_skip(query, LDNS_HEADER_SIZE);
553 1.1 christos m->qname = sldns_buffer_current(query);
554 1.1 christos if((m->qname_len = query_dname_len(query)) == 0)
555 1.1 christos return 0; /* parse error */
556 1.1 christos if(sldns_buffer_remaining(query) < 4)
557 1.1 christos return 0; /* need qtype, qclass */
558 1.1 christos m->qtype = sldns_buffer_read_u16(query);
559 1.1 christos m->qclass = sldns_buffer_read_u16(query);
560 1.1.1.2 christos m->local_alias = NULL;
561 1.1 christos return 1;
562 1.1 christos }
563 1.1 christos
564 1.1 christos /** tiny subroutine for msgreply_compare */
565 1.1 christos #define COMPARE_IT(x, y) \
566 1.1 christos if( (x) < (y) ) return -1; \
567 1.1 christos else if( (x) > (y) ) return +1; \
568 1.1 christos log_assert( (x) == (y) );
569 1.1 christos
570 1.1 christos int
571 1.1 christos query_info_compare(void* m1, void* m2)
572 1.1 christos {
573 1.1 christos struct query_info* msg1 = (struct query_info*)m1;
574 1.1 christos struct query_info* msg2 = (struct query_info*)m2;
575 1.1 christos int mc;
576 1.1 christos /* from most different to least different for speed */
577 1.1 christos COMPARE_IT(msg1->qtype, msg2->qtype);
578 1.1 christos if((mc = query_dname_compare(msg1->qname, msg2->qname)) != 0)
579 1.1 christos return mc;
580 1.1 christos log_assert(msg1->qname_len == msg2->qname_len);
581 1.1 christos COMPARE_IT(msg1->qclass, msg2->qclass);
582 1.1 christos return 0;
583 1.1 christos #undef COMPARE_IT
584 1.1 christos }
585 1.1 christos
586 1.1 christos void
587 1.1 christos query_info_clear(struct query_info* m)
588 1.1 christos {
589 1.1 christos free(m->qname);
590 1.1 christos m->qname = NULL;
591 1.1 christos }
592 1.1 christos
593 1.1 christos size_t
594 1.1 christos msgreply_sizefunc(void* k, void* d)
595 1.1 christos {
596 1.1 christos struct msgreply_entry* q = (struct msgreply_entry*)k;
597 1.1 christos struct reply_info* r = (struct reply_info*)d;
598 1.1 christos size_t s = sizeof(struct msgreply_entry) + sizeof(struct reply_info)
599 1.1 christos + q->key.qname_len + lock_get_mem(&q->entry.lock)
600 1.1 christos - sizeof(struct rrset_ref);
601 1.1 christos s += r->rrset_count * sizeof(struct rrset_ref);
602 1.1 christos s += r->rrset_count * sizeof(struct ub_packed_rrset_key*);
603 1.1 christos return s;
604 1.1 christos }
605 1.1 christos
606 1.1 christos void
607 1.1 christos query_entry_delete(void *k, void* ATTR_UNUSED(arg))
608 1.1 christos {
609 1.1 christos struct msgreply_entry* q = (struct msgreply_entry*)k;
610 1.1 christos lock_rw_destroy(&q->entry.lock);
611 1.1 christos query_info_clear(&q->key);
612 1.1 christos free(q);
613 1.1 christos }
614 1.1 christos
615 1.1 christos void
616 1.1 christos reply_info_delete(void* d, void* ATTR_UNUSED(arg))
617 1.1 christos {
618 1.1 christos struct reply_info* r = (struct reply_info*)d;
619 1.1 christos free(r);
620 1.1 christos }
621 1.1 christos
622 1.1.1.2 christos hashvalue_type
623 1.1 christos query_info_hash(struct query_info *q, uint16_t flags)
624 1.1 christos {
625 1.1.1.2 christos hashvalue_type h = 0xab;
626 1.1 christos h = hashlittle(&q->qtype, sizeof(q->qtype), h);
627 1.1 christos if(q->qtype == LDNS_RR_TYPE_AAAA && (flags&BIT_CD))
628 1.1 christos h++;
629 1.1 christos h = hashlittle(&q->qclass, sizeof(q->qclass), h);
630 1.1 christos h = dname_query_hash(q->qname, h);
631 1.1 christos return h;
632 1.1 christos }
633 1.1 christos
634 1.1 christos struct msgreply_entry*
635 1.1 christos query_info_entrysetup(struct query_info* q, struct reply_info* r,
636 1.1.1.2 christos hashvalue_type h)
637 1.1 christos {
638 1.1 christos struct msgreply_entry* e = (struct msgreply_entry*)malloc(
639 1.1 christos sizeof(struct msgreply_entry));
640 1.1 christos if(!e) return NULL;
641 1.1 christos memcpy(&e->key, q, sizeof(*q));
642 1.1 christos e->entry.hash = h;
643 1.1 christos e->entry.key = e;
644 1.1 christos e->entry.data = r;
645 1.1 christos lock_rw_init(&e->entry.lock);
646 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qname, sizeof(e->key.qname));
647 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qname_len, sizeof(e->key.qname_len));
648 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qtype, sizeof(e->key.qtype));
649 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.qclass, sizeof(e->key.qclass));
650 1.1.1.3 christos lock_protect(&e->entry.lock, &e->key.local_alias, sizeof(e->key.local_alias));
651 1.1.1.3 christos lock_protect(&e->entry.lock, &e->entry.hash, sizeof(e->entry.hash));
652 1.1.1.3 christos lock_protect(&e->entry.lock, &e->entry.key, sizeof(e->entry.key));
653 1.1.1.3 christos lock_protect(&e->entry.lock, &e->entry.data, sizeof(e->entry.data));
654 1.1 christos lock_protect(&e->entry.lock, e->key.qname, e->key.qname_len);
655 1.1 christos q->qname = NULL;
656 1.1 christos return e;
657 1.1 christos }
658 1.1 christos
659 1.1 christos /** copy rrsets from replyinfo to dest replyinfo */
660 1.1 christos static int
661 1.1 christos repinfo_copy_rrsets(struct reply_info* dest, struct reply_info* from,
662 1.1 christos struct regional* region)
663 1.1 christos {
664 1.1 christos size_t i, s;
665 1.1 christos struct packed_rrset_data* fd, *dd;
666 1.1 christos struct ub_packed_rrset_key* fk, *dk;
667 1.1 christos for(i=0; i<dest->rrset_count; i++) {
668 1.1 christos fk = from->rrsets[i];
669 1.1 christos dk = dest->rrsets[i];
670 1.1 christos fd = (struct packed_rrset_data*)fk->entry.data;
671 1.1 christos dk->entry.hash = fk->entry.hash;
672 1.1 christos dk->rk = fk->rk;
673 1.1 christos if(region) {
674 1.1 christos dk->id = fk->id;
675 1.1 christos dk->rk.dname = (uint8_t*)regional_alloc_init(region,
676 1.1 christos fk->rk.dname, fk->rk.dname_len);
677 1.1 christos } else
678 1.1 christos dk->rk.dname = (uint8_t*)memdup(fk->rk.dname,
679 1.1 christos fk->rk.dname_len);
680 1.1 christos if(!dk->rk.dname)
681 1.1 christos return 0;
682 1.1 christos s = packed_rrset_sizeof(fd);
683 1.1 christos if(region)
684 1.1 christos dd = (struct packed_rrset_data*)regional_alloc_init(
685 1.1 christos region, fd, s);
686 1.1 christos else dd = (struct packed_rrset_data*)memdup(fd, s);
687 1.1 christos if(!dd)
688 1.1 christos return 0;
689 1.1 christos packed_rrset_ptr_fixup(dd);
690 1.1 christos dk->entry.data = (void*)dd;
691 1.1 christos }
692 1.1 christos return 1;
693 1.1 christos }
694 1.1 christos
695 1.1 christos struct reply_info*
696 1.1 christos reply_info_copy(struct reply_info* rep, struct alloc_cache* alloc,
697 1.1 christos struct regional* region)
698 1.1 christos {
699 1.1 christos struct reply_info* cp;
700 1.1 christos cp = construct_reply_info_base(region, rep->flags, rep->qdcount,
701 1.1.1.4 christos rep->ttl, rep->prefetch_ttl, rep->serve_expired_ttl,
702 1.1.1.4 christos rep->an_numrrsets, rep->ns_numrrsets, rep->ar_numrrsets,
703 1.1.1.4 christos rep->rrset_count, rep->security);
704 1.1 christos if(!cp)
705 1.1 christos return NULL;
706 1.1 christos /* allocate ub_key structures special or not */
707 1.1.1.2 christos if(!reply_info_alloc_rrset_keys(cp, alloc, region)) {
708 1.1 christos if(!region)
709 1.1 christos reply_info_parsedelete(cp, alloc);
710 1.1 christos return NULL;
711 1.1 christos }
712 1.1 christos if(!repinfo_copy_rrsets(cp, rep, region)) {
713 1.1 christos if(!region)
714 1.1 christos reply_info_parsedelete(cp, alloc);
715 1.1 christos return NULL;
716 1.1 christos }
717 1.1 christos return cp;
718 1.1 christos }
719 1.1 christos
720 1.1 christos uint8_t*
721 1.1 christos reply_find_final_cname_target(struct query_info* qinfo, struct reply_info* rep)
722 1.1 christos {
723 1.1 christos uint8_t* sname = qinfo->qname;
724 1.1 christos size_t snamelen = qinfo->qname_len;
725 1.1 christos size_t i;
726 1.1 christos for(i=0; i<rep->an_numrrsets; i++) {
727 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i];
728 1.1 christos /* follow CNAME chain (if any) */
729 1.1 christos if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
730 1.1 christos ntohs(s->rk.rrset_class) == qinfo->qclass &&
731 1.1 christos snamelen == s->rk.dname_len &&
732 1.1 christos query_dname_compare(sname, s->rk.dname) == 0) {
733 1.1 christos get_cname_target(s, &sname, &snamelen);
734 1.1 christos }
735 1.1 christos }
736 1.1 christos if(sname != qinfo->qname)
737 1.1 christos return sname;
738 1.1 christos return NULL;
739 1.1 christos }
740 1.1 christos
741 1.1 christos struct ub_packed_rrset_key*
742 1.1 christos reply_find_answer_rrset(struct query_info* qinfo, struct reply_info* rep)
743 1.1 christos {
744 1.1 christos uint8_t* sname = qinfo->qname;
745 1.1 christos size_t snamelen = qinfo->qname_len;
746 1.1 christos size_t i;
747 1.1 christos for(i=0; i<rep->an_numrrsets; i++) {
748 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i];
749 1.1 christos /* first match type, for query of qtype cname */
750 1.1 christos if(ntohs(s->rk.type) == qinfo->qtype &&
751 1.1 christos ntohs(s->rk.rrset_class) == qinfo->qclass &&
752 1.1 christos snamelen == s->rk.dname_len &&
753 1.1 christos query_dname_compare(sname, s->rk.dname) == 0) {
754 1.1 christos return s;
755 1.1 christos }
756 1.1 christos /* follow CNAME chain (if any) */
757 1.1 christos if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME &&
758 1.1 christos ntohs(s->rk.rrset_class) == qinfo->qclass &&
759 1.1 christos snamelen == s->rk.dname_len &&
760 1.1 christos query_dname_compare(sname, s->rk.dname) == 0) {
761 1.1 christos get_cname_target(s, &sname, &snamelen);
762 1.1 christos }
763 1.1 christos }
764 1.1 christos return NULL;
765 1.1 christos }
766 1.1 christos
767 1.1 christos struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
768 1.1 christos uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
769 1.1 christos {
770 1.1 christos size_t i;
771 1.1 christos for(i=0; i<rep->an_numrrsets; i++) {
772 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i];
773 1.1 christos if(ntohs(s->rk.type) == type &&
774 1.1 christos ntohs(s->rk.rrset_class) == dclass &&
775 1.1 christos namelen == s->rk.dname_len &&
776 1.1 christos query_dname_compare(name, s->rk.dname) == 0) {
777 1.1 christos return s;
778 1.1 christos }
779 1.1 christos }
780 1.1 christos return NULL;
781 1.1 christos }
782 1.1 christos
783 1.1 christos struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
784 1.1 christos uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
785 1.1 christos {
786 1.1 christos size_t i;
787 1.1 christos for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
788 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i];
789 1.1 christos if(ntohs(s->rk.type) == type &&
790 1.1 christos ntohs(s->rk.rrset_class) == dclass &&
791 1.1 christos namelen == s->rk.dname_len &&
792 1.1 christos query_dname_compare(name, s->rk.dname) == 0) {
793 1.1 christos return s;
794 1.1 christos }
795 1.1 christos }
796 1.1 christos return NULL;
797 1.1 christos }
798 1.1 christos
799 1.1 christos struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
800 1.1 christos uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass)
801 1.1 christos {
802 1.1 christos size_t i;
803 1.1 christos for(i=0; i<rep->rrset_count; i++) {
804 1.1 christos struct ub_packed_rrset_key* s = rep->rrsets[i];
805 1.1 christos if(ntohs(s->rk.type) == type &&
806 1.1 christos ntohs(s->rk.rrset_class) == dclass &&
807 1.1 christos namelen == s->rk.dname_len &&
808 1.1 christos query_dname_compare(name, s->rk.dname) == 0) {
809 1.1 christos return s;
810 1.1 christos }
811 1.1 christos }
812 1.1 christos return NULL;
813 1.1 christos }
814 1.1 christos
815 1.1 christos void
816 1.1 christos log_dns_msg(const char* str, struct query_info* qinfo, struct reply_info* rep)
817 1.1 christos {
818 1.1 christos /* not particularly fast but flexible, make wireformat and print */
819 1.1 christos sldns_buffer* buf = sldns_buffer_new(65535);
820 1.1 christos struct regional* region = regional_create();
821 1.1 christos if(!reply_info_encode(qinfo, rep, 0, rep->flags, buf, 0,
822 1.1.1.5 christos region, 65535, 1, 0)) {
823 1.1 christos log_info("%s: log_dns_msg: out of memory", str);
824 1.1 christos } else {
825 1.1 christos char* s = sldns_wire2str_pkt(sldns_buffer_begin(buf),
826 1.1 christos sldns_buffer_limit(buf));
827 1.1 christos if(!s) {
828 1.1 christos log_info("%s: log_dns_msg: ldns tostr failed", str);
829 1.1 christos } else {
830 1.1 christos log_info("%s %s", str, s);
831 1.1 christos }
832 1.1 christos free(s);
833 1.1 christos }
834 1.1 christos sldns_buffer_free(buf);
835 1.1 christos regional_destroy(region);
836 1.1 christos }
837 1.1 christos
838 1.1.1.2 christos void
839 1.1.1.2 christos log_reply_info(enum verbosity_value v, struct query_info *qinf,
840 1.1.1.2 christos struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur,
841 1.1.1.2 christos int cached, struct sldns_buffer *rmsg)
842 1.1.1.2 christos {
843 1.1.1.2 christos char qname_buf[LDNS_MAX_DOMAINLEN+1];
844 1.1.1.2 christos char clientip_buf[128];
845 1.1.1.2 christos char rcode_buf[16];
846 1.1.1.2 christos char type_buf[16];
847 1.1.1.2 christos char class_buf[16];
848 1.1.1.2 christos size_t pktlen;
849 1.1.1.2 christos uint16_t rcode = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(rmsg, 2));
850 1.1.1.2 christos
851 1.1.1.2 christos if(verbosity < v)
852 1.1.1.2 christos return;
853 1.1.1.2 christos
854 1.1.1.2 christos sldns_wire2str_rcode_buf((int)rcode, rcode_buf, sizeof(rcode_buf));
855 1.1.1.2 christos addr_to_str(addr, addrlen, clientip_buf, sizeof(clientip_buf));
856 1.1.1.2 christos if(rcode == LDNS_RCODE_FORMERR)
857 1.1.1.2 christos {
858 1.1.1.4 christos if(LOG_TAG_QUERYREPLY)
859 1.1.1.4 christos log_reply("%s - - - %s - - - ", clientip_buf, rcode_buf);
860 1.1.1.4 christos else log_info("%s - - - %s - - - ", clientip_buf, rcode_buf);
861 1.1.1.2 christos } else {
862 1.1.1.2 christos if(qinf->qname)
863 1.1.1.2 christos dname_str(qinf->qname, qname_buf);
864 1.1.1.2 christos else snprintf(qname_buf, sizeof(qname_buf), "null");
865 1.1.1.2 christos pktlen = sldns_buffer_limit(rmsg);
866 1.1.1.2 christos sldns_wire2str_type_buf(qinf->qtype, type_buf, sizeof(type_buf));
867 1.1.1.2 christos sldns_wire2str_class_buf(qinf->qclass, class_buf, sizeof(class_buf));
868 1.1.1.4 christos if(LOG_TAG_QUERYREPLY)
869 1.1.1.4 christos log_reply("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d",
870 1.1.1.4 christos clientip_buf, qname_buf, type_buf, class_buf,
871 1.1.1.4 christos rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen);
872 1.1.1.4 christos else log_info("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d",
873 1.1.1.2 christos clientip_buf, qname_buf, type_buf, class_buf,
874 1.1.1.2 christos rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen);
875 1.1.1.2 christos }
876 1.1.1.2 christos }
877 1.1.1.2 christos
878 1.1.1.2 christos void
879 1.1 christos log_query_info(enum verbosity_value v, const char* str,
880 1.1 christos struct query_info* qinf)
881 1.1 christos {
882 1.1 christos log_nametypeclass(v, str, qinf->qname, qinf->qtype, qinf->qclass);
883 1.1 christos }
884 1.1 christos
885 1.1 christos int
886 1.1 christos reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep)
887 1.1 christos {
888 1.1 christos /* check only answer section rrs for matching cname chain.
889 1.1 christos * the cache may return changed rdata, but owner names are untouched.*/
890 1.1 christos size_t i;
891 1.1 christos uint8_t* sname = qinfo->qname;
892 1.1 christos size_t snamelen = qinfo->qname_len;
893 1.1 christos for(i=0; i<rep->an_numrrsets; i++) {
894 1.1 christos uint16_t t = ntohs(rep->rrsets[i]->rk.type);
895 1.1 christos if(t == LDNS_RR_TYPE_DNAME)
896 1.1 christos continue; /* skip dnames; note TTL 0 not cached */
897 1.1 christos /* verify that owner matches current sname */
898 1.1 christos if(query_dname_compare(sname, rep->rrsets[i]->rk.dname) != 0){
899 1.1 christos /* cname chain broken */
900 1.1 christos return 0;
901 1.1 christos }
902 1.1 christos /* if this is a cname; move on */
903 1.1 christos if(t == LDNS_RR_TYPE_CNAME) {
904 1.1 christos get_cname_target(rep->rrsets[i], &sname, &snamelen);
905 1.1 christos }
906 1.1 christos }
907 1.1 christos return 1;
908 1.1 christos }
909 1.1 christos
910 1.1 christos int
911 1.1 christos reply_all_rrsets_secure(struct reply_info* rep)
912 1.1 christos {
913 1.1 christos size_t i;
914 1.1 christos for(i=0; i<rep->rrset_count; i++) {
915 1.1 christos if( ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
916 1.1 christos ->security != sec_status_secure )
917 1.1 christos return 0;
918 1.1 christos }
919 1.1 christos return 1;
920 1.1 christos }
921 1.1 christos
922 1.1.1.3 christos struct reply_info*
923 1.1.1.3 christos parse_reply_in_temp_region(sldns_buffer* pkt, struct regional* region,
924 1.1.1.3 christos struct query_info* qi)
925 1.1.1.3 christos {
926 1.1.1.3 christos struct reply_info* rep;
927 1.1.1.3 christos struct msg_parse* msg;
928 1.1.1.3 christos if(!(msg = regional_alloc(region, sizeof(*msg)))) {
929 1.1.1.3 christos return NULL;
930 1.1.1.3 christos }
931 1.1.1.3 christos memset(msg, 0, sizeof(*msg));
932 1.1.1.3 christos sldns_buffer_set_position(pkt, 0);
933 1.1.1.4 christos if(parse_packet(pkt, msg, region) != 0){
934 1.1.1.3 christos return 0;
935 1.1.1.4 christos }
936 1.1.1.3 christos if(!parse_create_msg(pkt, msg, NULL, qi, &rep, region)) {
937 1.1.1.3 christos return 0;
938 1.1.1.3 christos }
939 1.1.1.3 christos return rep;
940 1.1.1.3 christos }
941 1.1.1.3 christos
942 1.1 christos int edns_opt_append(struct edns_data* edns, struct regional* region,
943 1.1 christos uint16_t code, size_t len, uint8_t* data)
944 1.1 christos {
945 1.1 christos struct edns_option** prevp;
946 1.1 christos struct edns_option* opt;
947 1.1 christos
948 1.1 christos /* allocate new element */
949 1.1 christos opt = (struct edns_option*)regional_alloc(region, sizeof(*opt));
950 1.1 christos if(!opt)
951 1.1 christos return 0;
952 1.1 christos opt->next = NULL;
953 1.1 christos opt->opt_code = code;
954 1.1 christos opt->opt_len = len;
955 1.1.1.2 christos opt->opt_data = NULL;
956 1.1.1.2 christos if(len > 0) {
957 1.1.1.2 christos opt->opt_data = regional_alloc_init(region, data, len);
958 1.1.1.2 christos if(!opt->opt_data)
959 1.1.1.2 christos return 0;
960 1.1.1.2 christos }
961 1.1 christos
962 1.1 christos /* append at end of list */
963 1.1 christos prevp = &edns->opt_list;
964 1.1 christos while(*prevp != NULL)
965 1.1 christos prevp = &((*prevp)->next);
966 1.1 christos *prevp = opt;
967 1.1 christos return 1;
968 1.1 christos }
969 1.1 christos
970 1.1.1.2 christos int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
971 1.1.1.2 christos uint8_t* data, struct regional* region)
972 1.1 christos {
973 1.1.1.2 christos struct edns_option** prevp;
974 1.1.1.2 christos struct edns_option* opt;
975 1.1.1.2 christos
976 1.1.1.2 christos /* allocate new element */
977 1.1.1.2 christos opt = (struct edns_option*)regional_alloc(region, sizeof(*opt));
978 1.1.1.2 christos if(!opt)
979 1.1.1.2 christos return 0;
980 1.1.1.2 christos opt->next = NULL;
981 1.1.1.2 christos opt->opt_code = code;
982 1.1.1.2 christos opt->opt_len = len;
983 1.1.1.2 christos opt->opt_data = NULL;
984 1.1.1.2 christos if(len > 0) {
985 1.1.1.2 christos opt->opt_data = regional_alloc_init(region, data, len);
986 1.1.1.2 christos if(!opt->opt_data)
987 1.1.1.2 christos return 0;
988 1.1.1.2 christos }
989 1.1.1.2 christos
990 1.1.1.2 christos /* append at end of list */
991 1.1.1.2 christos prevp = list;
992 1.1.1.2 christos while(*prevp != NULL) {
993 1.1.1.2 christos prevp = &((*prevp)->next);
994 1.1.1.2 christos }
995 1.1.1.2 christos *prevp = opt;
996 1.1.1.2 christos return 1;
997 1.1.1.2 christos }
998 1.1.1.2 christos
999 1.1.1.2 christos int edns_opt_list_remove(struct edns_option** list, uint16_t code)
1000 1.1.1.2 christos {
1001 1.1.1.2 christos /* The list should already be allocated in a region. Freeing the
1002 1.1.1.2 christos * allocated space in a region is not possible. We just unlink the
1003 1.1.1.2 christos * required elements and they will be freed together with the region. */
1004 1.1.1.2 christos
1005 1.1.1.2 christos struct edns_option* prev;
1006 1.1.1.2 christos struct edns_option* curr;
1007 1.1.1.2 christos if(!list || !(*list)) return 0;
1008 1.1.1.2 christos
1009 1.1.1.2 christos /* Unlink and repoint if the element(s) are first in list */
1010 1.1.1.2 christos while(list && *list && (*list)->opt_code == code) {
1011 1.1.1.2 christos *list = (*list)->next;
1012 1.1.1.2 christos }
1013 1.1.1.2 christos
1014 1.1.1.2 christos if(!list || !(*list)) return 1;
1015 1.1.1.2 christos /* Unlink elements and reattach the list */
1016 1.1.1.2 christos prev = *list;
1017 1.1.1.2 christos curr = (*list)->next;
1018 1.1.1.2 christos while(curr != NULL) {
1019 1.1.1.2 christos if(curr->opt_code == code) {
1020 1.1.1.2 christos prev->next = curr->next;
1021 1.1.1.2 christos curr = curr->next;
1022 1.1.1.2 christos } else {
1023 1.1.1.2 christos prev = curr;
1024 1.1.1.2 christos curr = curr->next;
1025 1.1.1.2 christos }
1026 1.1.1.2 christos }
1027 1.1.1.2 christos return 1;
1028 1.1.1.2 christos }
1029 1.1.1.2 christos
1030 1.1.1.2 christos static int inplace_cb_reply_call_generic(
1031 1.1.1.2 christos struct inplace_cb* callback_list, enum inplace_cb_list_type type,
1032 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate,
1033 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns,
1034 1.1.1.4 christos struct comm_reply* repinfo, struct regional* region)
1035 1.1.1.2 christos {
1036 1.1.1.2 christos struct inplace_cb* cb;
1037 1.1.1.2 christos struct edns_option* opt_list_out = NULL;
1038 1.1.1.2 christos #if defined(EXPORT_ALL_SYMBOLS)
1039 1.1.1.2 christos (void)type; /* param not used when fptr_ok disabled */
1040 1.1.1.2 christos #endif
1041 1.1.1.2 christos if(qstate)
1042 1.1.1.2 christos opt_list_out = qstate->edns_opts_front_out;
1043 1.1.1.2 christos for(cb=callback_list; cb; cb=cb->next) {
1044 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_reply_generic(
1045 1.1.1.2 christos (inplace_cb_reply_func_type*)cb->cb, type));
1046 1.1.1.2 christos (void)(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep,
1047 1.1.1.4 christos rcode, edns, &opt_list_out, repinfo, region, cb->id, cb->cb_arg);
1048 1.1.1.2 christos }
1049 1.1.1.2 christos edns->opt_list = opt_list_out;
1050 1.1.1.2 christos return 1;
1051 1.1.1.2 christos }
1052 1.1.1.2 christos
1053 1.1.1.2 christos int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
1054 1.1.1.2 christos struct module_qstate* qstate, struct reply_info* rep, int rcode,
1055 1.1.1.4 christos struct edns_data* edns, struct comm_reply* repinfo, struct regional* region)
1056 1.1.1.2 christos {
1057 1.1.1.2 christos return inplace_cb_reply_call_generic(
1058 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply], inplace_cb_reply, qinfo,
1059 1.1.1.4 christos qstate, rep, rcode, edns, repinfo, region);
1060 1.1.1.2 christos }
1061 1.1.1.2 christos
1062 1.1.1.2 christos int inplace_cb_reply_cache_call(struct module_env* env,
1063 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate,
1064 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns,
1065 1.1.1.4 christos struct comm_reply* repinfo, struct regional* region)
1066 1.1.1.2 christos {
1067 1.1.1.2 christos return inplace_cb_reply_call_generic(
1068 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply_cache], inplace_cb_reply_cache,
1069 1.1.1.4 christos qinfo, qstate, rep, rcode, edns, repinfo, region);
1070 1.1.1.2 christos }
1071 1.1.1.2 christos
1072 1.1.1.2 christos int inplace_cb_reply_local_call(struct module_env* env,
1073 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate,
1074 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns,
1075 1.1.1.4 christos struct comm_reply* repinfo, struct regional* region)
1076 1.1.1.2 christos {
1077 1.1.1.2 christos return inplace_cb_reply_call_generic(
1078 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply_local], inplace_cb_reply_local,
1079 1.1.1.4 christos qinfo, qstate, rep, rcode, edns, repinfo, region);
1080 1.1.1.2 christos }
1081 1.1.1.2 christos
1082 1.1.1.2 christos int inplace_cb_reply_servfail_call(struct module_env* env,
1083 1.1.1.2 christos struct query_info* qinfo, struct module_qstate* qstate,
1084 1.1.1.2 christos struct reply_info* rep, int rcode, struct edns_data* edns,
1085 1.1.1.4 christos struct comm_reply* repinfo, struct regional* region)
1086 1.1.1.2 christos {
1087 1.1.1.2 christos /* We are going to servfail. Remove any potential edns options. */
1088 1.1.1.2 christos if(qstate)
1089 1.1.1.2 christos qstate->edns_opts_front_out = NULL;
1090 1.1.1.2 christos return inplace_cb_reply_call_generic(
1091 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_reply_servfail],
1092 1.1.1.4 christos inplace_cb_reply_servfail, qinfo, qstate, rep, rcode, edns, repinfo,
1093 1.1.1.4 christos region);
1094 1.1.1.2 christos }
1095 1.1.1.2 christos
1096 1.1.1.2 christos int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
1097 1.1.1.2 christos uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
1098 1.1.1.2 christos uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
1099 1.1.1.2 christos struct regional* region)
1100 1.1.1.2 christos {
1101 1.1.1.2 christos struct inplace_cb* cb = env->inplace_cb_lists[inplace_cb_query];
1102 1.1.1.2 christos for(; cb; cb=cb->next) {
1103 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_query(
1104 1.1.1.2 christos (inplace_cb_query_func_type*)cb->cb));
1105 1.1.1.2 christos (void)(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags,
1106 1.1.1.2 christos qstate, addr, addrlen, zone, zonelen, region,
1107 1.1.1.2 christos cb->id, cb->cb_arg);
1108 1.1.1.2 christos }
1109 1.1.1.2 christos return 1;
1110 1.1.1.2 christos }
1111 1.1.1.2 christos
1112 1.1.1.2 christos int inplace_cb_edns_back_parsed_call(struct module_env* env,
1113 1.1.1.2 christos struct module_qstate* qstate)
1114 1.1.1.2 christos {
1115 1.1.1.2 christos struct inplace_cb* cb =
1116 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_edns_back_parsed];
1117 1.1.1.2 christos for(; cb; cb=cb->next) {
1118 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_edns_back_parsed(
1119 1.1.1.2 christos (inplace_cb_edns_back_parsed_func_type*)cb->cb));
1120 1.1.1.2 christos (void)(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate,
1121 1.1.1.2 christos cb->id, cb->cb_arg);
1122 1.1.1.2 christos }
1123 1.1.1.2 christos return 1;
1124 1.1.1.2 christos }
1125 1.1.1.2 christos
1126 1.1.1.2 christos int inplace_cb_query_response_call(struct module_env* env,
1127 1.1.1.2 christos struct module_qstate* qstate, struct dns_msg* response) {
1128 1.1.1.2 christos struct inplace_cb* cb =
1129 1.1.1.2 christos env->inplace_cb_lists[inplace_cb_query_response];
1130 1.1.1.2 christos for(; cb; cb=cb->next) {
1131 1.1.1.2 christos fptr_ok(fptr_whitelist_inplace_cb_query_response(
1132 1.1.1.2 christos (inplace_cb_query_response_func_type*)cb->cb));
1133 1.1.1.2 christos (void)(*(inplace_cb_query_response_func_type*)cb->cb)(qstate,
1134 1.1.1.2 christos response, cb->id, cb->cb_arg);
1135 1.1.1.2 christos }
1136 1.1 christos return 1;
1137 1.1 christos }
1138 1.1 christos
1139 1.1 christos struct edns_option* edns_opt_copy_region(struct edns_option* list,
1140 1.1 christos struct regional* region)
1141 1.1 christos {
1142 1.1 christos struct edns_option* result = NULL, *cur = NULL, *s;
1143 1.1 christos while(list) {
1144 1.1 christos /* copy edns option structure */
1145 1.1 christos s = regional_alloc_init(region, list, sizeof(*list));
1146 1.1 christos if(!s) return NULL;
1147 1.1 christos s->next = NULL;
1148 1.1 christos
1149 1.1 christos /* copy option data */
1150 1.1 christos if(s->opt_data) {
1151 1.1 christos s->opt_data = regional_alloc_init(region, s->opt_data,
1152 1.1 christos s->opt_len);
1153 1.1 christos if(!s->opt_data)
1154 1.1 christos return NULL;
1155 1.1 christos }
1156 1.1 christos
1157 1.1 christos /* link into list */
1158 1.1 christos if(cur)
1159 1.1 christos cur->next = s;
1160 1.1 christos else result = s;
1161 1.1 christos cur = s;
1162 1.1 christos
1163 1.1 christos /* examine next element */
1164 1.1 christos list = list->next;
1165 1.1 christos }
1166 1.1 christos return result;
1167 1.1 christos }
1168 1.1 christos
1169 1.1 christos int edns_opt_compare(struct edns_option* p, struct edns_option* q)
1170 1.1 christos {
1171 1.1 christos if(!p && !q) return 0;
1172 1.1 christos if(!p) return -1;
1173 1.1 christos if(!q) return 1;
1174 1.1 christos log_assert(p && q);
1175 1.1 christos if(p->opt_code != q->opt_code)
1176 1.1 christos return (int)q->opt_code - (int)p->opt_code;
1177 1.1 christos if(p->opt_len != q->opt_len)
1178 1.1 christos return (int)q->opt_len - (int)p->opt_len;
1179 1.1 christos if(p->opt_len != 0)
1180 1.1 christos return memcmp(p->opt_data, q->opt_data, p->opt_len);
1181 1.1 christos return 0;
1182 1.1 christos }
1183 1.1 christos
1184 1.1 christos int edns_opt_list_compare(struct edns_option* p, struct edns_option* q)
1185 1.1 christos {
1186 1.1 christos int r;
1187 1.1 christos while(p && q) {
1188 1.1 christos r = edns_opt_compare(p, q);
1189 1.1 christos if(r != 0)
1190 1.1 christos return r;
1191 1.1 christos p = p->next;
1192 1.1 christos q = q->next;
1193 1.1 christos }
1194 1.1 christos if(p || q) {
1195 1.1 christos /* uneven length lists */
1196 1.1 christos if(p) return 1;
1197 1.1 christos if(q) return -1;
1198 1.1 christos }
1199 1.1 christos return 0;
1200 1.1 christos }
1201 1.1 christos
1202 1.1 christos void edns_opt_list_free(struct edns_option* list)
1203 1.1 christos {
1204 1.1 christos struct edns_option* n;
1205 1.1 christos while(list) {
1206 1.1 christos free(list->opt_data);
1207 1.1 christos n = list->next;
1208 1.1 christos free(list);
1209 1.1 christos list = n;
1210 1.1 christos }
1211 1.1 christos }
1212 1.1 christos
1213 1.1 christos struct edns_option* edns_opt_copy_alloc(struct edns_option* list)
1214 1.1 christos {
1215 1.1 christos struct edns_option* result = NULL, *cur = NULL, *s;
1216 1.1 christos while(list) {
1217 1.1 christos /* copy edns option structure */
1218 1.1 christos s = memdup(list, sizeof(*list));
1219 1.1 christos if(!s) {
1220 1.1 christos edns_opt_list_free(result);
1221 1.1 christos return NULL;
1222 1.1 christos }
1223 1.1 christos s->next = NULL;
1224 1.1 christos
1225 1.1 christos /* copy option data */
1226 1.1 christos if(s->opt_data) {
1227 1.1 christos s->opt_data = memdup(s->opt_data, s->opt_len);
1228 1.1 christos if(!s->opt_data) {
1229 1.1.1.2 christos free(s);
1230 1.1 christos edns_opt_list_free(result);
1231 1.1 christos return NULL;
1232 1.1 christos }
1233 1.1 christos }
1234 1.1 christos
1235 1.1 christos /* link into list */
1236 1.1 christos if(cur)
1237 1.1 christos cur->next = s;
1238 1.1 christos else result = s;
1239 1.1 christos cur = s;
1240 1.1 christos
1241 1.1 christos /* examine next element */
1242 1.1 christos list = list->next;
1243 1.1 christos }
1244 1.1 christos return result;
1245 1.1 christos }
1246 1.1 christos
1247 1.1.1.2 christos struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code)
1248 1.1 christos {
1249 1.1 christos struct edns_option* p;
1250 1.1 christos for(p=list; p; p=p->next) {
1251 1.1 christos if(p->opt_code == code)
1252 1.1 christos return p;
1253 1.1 christos }
1254 1.1 christos return NULL;
1255 1.1 christos }
1256