Coverage Report

Created: 2025-11-11 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unbound/iterator/iter_utils.c
Line
Count
Source
1
/*
2
 * iterator/iter_utils.c - iterative resolver module utility functions.
3
 *
4
 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5
 *
6
 * This software is open source.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * Redistributions of source code must retain the above copyright notice,
13
 * this list of conditions and the following disclaimer.
14
 *
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.
18
 *
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.
22
 *
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.
34
 */
35
36
/**
37
 * \file
38
 *
39
 * This file contains functions to assist the iterator module.
40
 * Configuration options. Forward zones.
41
 */
42
#include "config.h"
43
#include "iterator/iter_utils.h"
44
#include "iterator/iterator.h"
45
#include "iterator/iter_hints.h"
46
#include "iterator/iter_fwd.h"
47
#include "iterator/iter_donotq.h"
48
#include "iterator/iter_delegpt.h"
49
#include "iterator/iter_priv.h"
50
#include "services/cache/infra.h"
51
#include "services/cache/dns.h"
52
#include "services/cache/rrset.h"
53
#include "services/outside_network.h"
54
#include "util/net_help.h"
55
#include "util/module.h"
56
#include "util/log.h"
57
#include "util/config_file.h"
58
#include "util/regional.h"
59
#include "util/data/msgparse.h"
60
#include "util/data/dname.h"
61
#include "util/random.h"
62
#include "util/fptr_wlist.h"
63
#include "validator/val_anchor.h"
64
#include "validator/val_kcache.h"
65
#include "validator/val_kentry.h"
66
#include "validator/val_utils.h"
67
#include "validator/val_sigcrypt.h"
68
#include "sldns/sbuffer.h"
69
#include "sldns/str2wire.h"
70
71
/** time when nameserver glue is said to be 'recent' */
72
#define SUSPICION_RECENT_EXPIRY 86400
73
74
/** if NAT64 is enabled and no NAT64 prefix is configured, first fall back to
75
 * DNS64 prefix.  If that is not configured, fall back to this default value.
76
 */
77
static const char DEFAULT_NAT64_PREFIX[] = "64:ff9b::/96";
78
79
/** fillup fetch policy array */
80
static int
81
fetch_fill(int* target_fetch_policy, int max_dependency_depth, const char* str)
82
0
{
83
0
  char* s = (char*)str, *e;
84
0
  int i;
85
0
  for(i=0; i<max_dependency_depth+1; i++) {
86
0
    target_fetch_policy[i] = strtol(s, &e, 10);
87
0
    if(s == e) {
88
0
      log_err("cannot parse fetch policy number %s", s);
89
0
      return 0;
90
0
    }
91
0
    s = e;
92
0
  }
93
0
  return 1;
94
0
}
95
96
/** Read config string that represents the target fetch policy */
97
int
98
read_fetch_policy(int** target_fetch_policy, int* max_dependency_depth,
99
  const char* str)
100
0
{
101
0
  int count = cfg_count_numbers(str);
102
0
  if(count < 1) {
103
0
    log_err("Cannot parse target fetch policy: \"%s\"", str);
104
0
    return 0;
105
0
  }
106
0
  *max_dependency_depth = count - 1;
107
0
  *target_fetch_policy = (int*)calloc(
108
0
    (size_t)(*max_dependency_depth)+1, sizeof(int));
109
0
  if(!*target_fetch_policy) {
110
0
    log_err("alloc fetch policy: out of memory");
111
0
    return 0;
112
0
  }
113
0
  if(!fetch_fill(*target_fetch_policy, *max_dependency_depth, str))
114
0
    return 0;
115
0
  return 1;
116
0
}
117
118
struct rbtree_type*
119
caps_white_create(void)
120
0
{
121
0
  struct rbtree_type* caps_white = rbtree_create(name_tree_compare);
122
0
  if(!caps_white)
123
0
    log_err("out of memory");
124
0
  return caps_white;
125
0
}
126
127
/** delete caps_whitelist element */
128
static void
129
caps_free(struct rbnode_type* n, void* ATTR_UNUSED(d))
130
0
{
131
0
  if(n) {
132
0
    free(((struct name_tree_node*)n)->name);
133
0
    free(n);
134
0
  }
135
0
}
136
137
void
138
caps_white_delete(struct rbtree_type* caps_white)
139
0
{
140
0
  if(!caps_white)
141
0
    return;
142
0
  traverse_postorder(caps_white, caps_free, NULL);
143
0
  free(caps_white);
144
0
}
145
146
int
147
caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg)
148
0
{
149
0
  struct config_strlist* p;
150
0
  for(p=cfg->caps_whitelist; p; p=p->next) {
151
0
    struct name_tree_node* n;
152
0
    size_t len;
153
0
    uint8_t* nm = sldns_str2wire_dname(p->str, &len);
154
0
    if(!nm) {
155
0
      log_err("could not parse %s", p->str);
156
0
      return 0;
157
0
    }
158
0
    n = (struct name_tree_node*)calloc(1, sizeof(*n));
159
0
    if(!n) {
160
0
      log_err("out of memory");
161
0
      free(nm);
162
0
      return 0;
163
0
    }
164
0
    n->node.key = n;
165
0
    n->name = nm;
166
0
    n->len = len;
167
0
    n->labs = dname_count_labels(nm);
168
0
    n->dclass = LDNS_RR_CLASS_IN;
169
0
    if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) {
170
      /* duplicate element ignored, idempotent */
171
0
      free(n->name);
172
0
      free(n);
173
0
    }
174
0
  }
175
0
  name_tree_init_parents(ntree);
176
0
  return 1;
177
0
}
178
179
int
180
nat64_apply_cfg(struct iter_nat64* nat64, struct config_file* cfg)
181
0
{
182
0
  const char *nat64_prefix;
183
184
0
  nat64_prefix = cfg->nat64_prefix;
185
0
  if(!nat64_prefix)
186
0
    nat64_prefix = cfg->dns64_prefix;
187
0
  if(!nat64_prefix)
188
0
    nat64_prefix = DEFAULT_NAT64_PREFIX;
189
0
  if(!netblockstrtoaddr(nat64_prefix, 0, &nat64->nat64_prefix_addr,
190
0
    &nat64->nat64_prefix_addrlen, &nat64->nat64_prefix_net)) {
191
0
    log_err("cannot parse nat64-prefix netblock: %s", nat64_prefix);
192
0
    return 0;
193
0
  }
194
0
  if(!addr_is_ip6(&nat64->nat64_prefix_addr,
195
0
    nat64->nat64_prefix_addrlen)) {
196
0
    log_err("nat64-prefix is not IPv6: %s", cfg->nat64_prefix);
197
0
    return 0;
198
0
  }
199
0
  if(!prefixnet_is_nat64(nat64->nat64_prefix_net)) {
200
0
    log_err("nat64-prefix length it not 32, 40, 48, 56, 64 or 96: %s",
201
0
      nat64_prefix);
202
0
    return 0;
203
0
  }
204
0
  nat64->use_nat64 = cfg->do_nat64;
205
0
  return 1;
206
0
}
207
208
int
209
iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
210
0
{
211
0
  int i;
212
  /* target fetch policy */
213
0
  if(!read_fetch_policy(&iter_env->target_fetch_policy,
214
0
    &iter_env->max_dependency_depth, cfg->target_fetch_policy))
215
0
    return 0;
216
0
  for(i=0; i<iter_env->max_dependency_depth+1; i++)
217
0
    verbose(VERB_QUERY, "target fetch policy for level %d is %d",
218
0
      i, iter_env->target_fetch_policy[i]);
219
220
0
  if(!iter_env->donotq)
221
0
    iter_env->donotq = donotq_create();
222
0
  if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
223
0
    log_err("Could not set donotqueryaddresses");
224
0
    return 0;
225
0
  }
226
0
  if(!iter_env->priv)
227
0
    iter_env->priv = priv_create();
228
0
  if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
229
0
    log_err("Could not set private addresses");
230
0
    return 0;
231
0
  }
232
0
  if(cfg->caps_whitelist) {
233
0
    if(!iter_env->caps_white)
234
0
      iter_env->caps_white = caps_white_create();
235
0
    if(!iter_env->caps_white || !caps_white_apply_cfg(
236
0
      iter_env->caps_white, cfg)) {
237
0
      log_err("Could not set capsforid whitelist");
238
0
      return 0;
239
0
    }
240
241
0
  }
242
243
0
  if(!nat64_apply_cfg(&iter_env->nat64, cfg)) {
244
0
    log_err("Could not setup nat64");
245
0
    return 0;
246
0
  }
247
248
0
  iter_env->supports_ipv6 = cfg->do_ip6;
249
0
  iter_env->supports_ipv4 = cfg->do_ip4;
250
0
  iter_env->outbound_msg_retry = cfg->outbound_msg_retry;
251
0
  iter_env->max_sent_count = cfg->max_sent_count;
252
0
  iter_env->max_query_restarts = cfg->max_query_restarts;
253
0
  return 1;
254
0
}
255
256
/** filter out unsuitable targets.
257
 * Applies NAT64 if needed as well by replacing the IPv4 with the synthesized
258
 * IPv6 address.
259
 * @param iter_env: iterator environment with ipv6-support flag.
260
 * @param env: module environment with infra cache.
261
 * @param name: zone name
262
 * @param namelen: length of name
263
 * @param qtype: query type (host order).
264
 * @param now: current time
265
 * @param a: address in delegation point we are examining.
266
 * @return an integer that signals the target suitability.
267
 *  as follows:
268
 *  -1: The address should be omitted from the list.
269
 *      Because:
270
 *    o The address is bogus (DNSSEC validation failure).
271
 *    o Listed as donotquery
272
 *    o is ipv6 but no ipv6 support (in operating system).
273
 *    o is ipv4 but no ipv4 support (in operating system).
274
 *    o is lame
275
 *  Otherwise, an rtt in milliseconds.
276
 *  0 .. USEFUL_SERVER_TOP_TIMEOUT-1
277
 *    The roundtrip time timeout estimate. less than 2 minutes.
278
 *    Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
279
 *    values 0 .. 49 are not used, unless that is changed.
280
 *  USEFUL_SERVER_TOP_TIMEOUT
281
 *    This value exactly is given for unresponsive blacklisted.
282
 *  USEFUL_SERVER_TOP_TIMEOUT+1
283
 *    For non-blacklisted servers: huge timeout, but has traffic.
284
 *  USEFUL_SERVER_TOP_TIMEOUT*1 ..
285
 *    parent-side lame servers get this penalty. A dispreferential
286
 *    server. (lame in delegpt).
287
 *  USEFUL_SERVER_TOP_TIMEOUT*2 ..
288
 *    dnsseclame servers get penalty
289
 *  USEFUL_SERVER_TOP_TIMEOUT*3 ..
290
 *    recursion lame servers get penalty
291
 *  UNKNOWN_SERVER_NICENESS
292
 *    If no information is known about the server, this is
293
 *    returned. 376 msec or so.
294
 *  +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
295
 *
296
 * When a final value is chosen that is dnsseclame ; dnsseclameness checking
297
 * is turned off (so we do not discard the reply).
298
 * When a final value is chosen that is recursionlame; RD bit is set on query.
299
 * Because of the numbers this means recursionlame also have dnssec lameness
300
 * checking turned off.
301
 */
302
static int
303
iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
304
  uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
305
  struct delegpt_addr* a)
306
0
{
307
0
  int rtt, lame, reclame, dnsseclame;
308
0
  if(a->bogus)
309
0
    return -1; /* address of server is bogus */
310
0
  if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
311
0
    log_addr(VERB_ALGO, "skip addr on the donotquery list",
312
0
      &a->addr, a->addrlen);
313
0
    return -1; /* server is on the donotquery list */
314
0
  }
315
0
  if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
316
0
    return -1; /* there is no ip6 available */
317
0
  }
318
0
  if(!iter_env->supports_ipv4 && !iter_env->nat64.use_nat64 &&
319
0
     !addr_is_ip6(&a->addr, a->addrlen)) {
320
0
    return -1; /* there is no ip4 available */
321
0
  }
322
0
  if(iter_env->nat64.use_nat64 && !addr_is_ip6(&a->addr, a->addrlen)) {
323
0
    struct sockaddr_storage real_addr;
324
0
    socklen_t real_addrlen;
325
0
    addr_to_nat64(&a->addr, &iter_env->nat64.nat64_prefix_addr,
326
0
      iter_env->nat64.nat64_prefix_addrlen,
327
0
      iter_env->nat64.nat64_prefix_net,
328
0
      &real_addr, &real_addrlen);
329
0
    log_name_addr(VERB_QUERY, "NAT64 apply: from: ",
330
0
      name, &a->addr, a->addrlen);
331
0
    log_name_addr(VERB_QUERY, "NAT64 apply:   to: ",
332
0
      name, &real_addr, real_addrlen);
333
0
    a->addr = real_addr;
334
0
    a->addrlen = real_addrlen;
335
0
  }
336
  /* check lameness - need zone , class info */
337
0
  if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
338
0
    name, namelen, qtype, &lame, &dnsseclame, &reclame,
339
0
    &rtt, now)) {
340
0
    log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
341
0
    verbose(VERB_ALGO, "   rtt=%d%s%s%s%s%s", rtt,
342
0
      lame?" LAME":"",
343
0
      dnsseclame?" DNSSEC_LAME":"",
344
0
      a->dnsseclame?" ADDR_DNSSEC_LAME":"",
345
0
      reclame?" REC_LAME":"",
346
0
      a->lame?" ADDR_LAME":"");
347
0
    if(lame)
348
0
      return -1; /* server is lame */
349
0
    else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
350
      /* server is unresponsive,
351
       * we used to return TOP_TIMEOUT, but fairly useless,
352
       * because if == TOP_TIMEOUT is dropped because
353
       * blacklisted later, instead, remove it here, so
354
       * other choices (that are not blacklisted) can be
355
       * tried */
356
0
      return -1;
357
    /* select remainder from worst to best */
358
0
    else if(reclame)
359
0
      return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
360
0
    else if(dnsseclame || a->dnsseclame)
361
0
      return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
362
0
    else if(a->lame)
363
0
      return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
364
0
    else  return rtt;
365
0
  }
366
  /* no server information present */
367
0
  if(a->dnsseclame)
368
0
    return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
369
0
  else if(a->lame)
370
0
    return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
371
0
  return UNKNOWN_SERVER_NICENESS;
372
0
}
373
374
/** lookup RTT information, and also store fastest rtt (if any) */
375
static int
376
iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
377
  uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
378
  struct delegpt* dp, int* best_rtt, struct sock_list* blacklist,
379
  size_t* num_suitable_results)
380
0
{
381
0
  int got_it = 0;
382
0
  struct delegpt_addr* a;
383
0
  *num_suitable_results = 0;
384
385
0
  if(dp->bogus)
386
0
    return 0; /* NS bogus, all bogus, nothing found */
387
0
  for(a=dp->result_list; a; a = a->next_result) {
388
0
    a->sel_rtt = iter_filter_unsuitable(iter_env, env,
389
0
      name, namelen, qtype, now, a);
390
0
    if(a->sel_rtt != -1) {
391
0
      if(sock_list_find(blacklist, &a->addr, a->addrlen))
392
0
        a->sel_rtt += BLACKLIST_PENALTY;
393
394
0
      if(!got_it) {
395
0
        *best_rtt = a->sel_rtt;
396
0
        got_it = 1;
397
0
      } else if(a->sel_rtt < *best_rtt) {
398
0
        *best_rtt = a->sel_rtt;
399
0
      }
400
0
      (*num_suitable_results)++;
401
0
    }
402
0
  }
403
0
  return got_it;
404
0
}
405
406
/** compare two rtts, return -1, 0 or 1 */
407
static int
408
rtt_compare(const void* x, const void* y)
409
0
{
410
0
  if(*(int*)x == *(int*)y)
411
0
    return 0;
412
0
  if(*(int*)x > *(int*)y)
413
0
    return 1;
414
0
  return -1;
415
0
}
416
417
/** get RTT for the Nth fastest server */
418
static int
419
nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n)
420
0
{
421
0
  int rtt_band;
422
0
  size_t i;
423
0
  int* rtt_list, *rtt_index;
424
425
0
  if(num_results < 1 || n >= num_results) {
426
0
    return -1;
427
0
  }
428
429
0
  rtt_list = calloc(num_results, sizeof(int));
430
0
  if(!rtt_list) {
431
0
    log_err("malloc failure: allocating rtt_list");
432
0
    return -1;
433
0
  }
434
0
  rtt_index = rtt_list;
435
436
0
  for(i=0; i<num_results && result_list; i++) {
437
0
    if(result_list->sel_rtt != -1) {
438
0
      *rtt_index = result_list->sel_rtt;
439
0
      rtt_index++;
440
0
    }
441
0
    result_list=result_list->next_result;
442
0
  }
443
0
  qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare);
444
445
0
  log_assert(n > 0);
446
0
  rtt_band = rtt_list[n-1];
447
0
  free(rtt_list);
448
449
0
  return rtt_band;
450
0
}
451
452
/** filter the address list, putting best targets at front,
453
 * returns number of best targets (or 0, no suitable targets) */
454
static int
455
iter_filter_order(struct iter_env* iter_env, struct module_env* env,
456
  uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
457
  struct delegpt* dp, int* selected_rtt, int open_target,
458
  struct sock_list* blacklist, time_t prefetch)
459
0
{
460
0
  int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth;
461
0
  int alllame = 0;
462
0
  size_t num_results;
463
0
  struct delegpt_addr* a, *n, *prev=NULL;
464
465
  /* fillup sel_rtt and find best rtt in the bunch */
466
0
  got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
467
0
    &low_rtt, blacklist, &num_results);
468
0
  if(got_num == 0)
469
0
    return 0;
470
0
  if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
471
    /* If all missing (or not fully resolved) targets are lame,
472
     * then use the remaining lame address. */
473
0
    ((delegpt_count_missing_targets(dp, &alllame) > 0 && !alllame) ||
474
0
    open_target > 0)) {
475
0
    verbose(VERB_ALGO, "Bad choices, trying to get more choice");
476
0
    return 0; /* we want more choice. The best choice is a bad one.
477
           return 0 to force the caller to fetch more */
478
0
  }
479
480
0
  if(env->cfg->fast_server_permil != 0 && prefetch == 0 &&
481
0
    num_results > env->cfg->fast_server_num &&
482
0
    ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) {
483
    /* the query is not prefetch, but for a downstream client,
484
     * there are more servers available then the fastest N we want
485
     * to choose from. Limit our choice to the fastest servers. */
486
0
    nth = nth_rtt(dp->result_list, num_results,
487
0
      env->cfg->fast_server_num);
488
0
    if(nth > 0) {
489
0
      rtt_band = nth - low_rtt;
490
0
      if(rtt_band > RTT_BAND)
491
0
        rtt_band = RTT_BAND;
492
0
    }
493
0
  }
494
495
0
  got_num = 0;
496
0
  a = dp->result_list;
497
0
  while(a) {
498
    /* skip unsuitable targets */
499
0
    if(a->sel_rtt == -1) {
500
0
      prev = a;
501
0
      a = a->next_result;
502
0
      continue;
503
0
    }
504
    /* classify the server address and determine what to do */
505
0
    swap_to_front = 0;
506
0
    if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) {
507
0
      got_num++;
508
0
      swap_to_front = 1;
509
0
    } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) {
510
0
      got_num++;
511
0
      swap_to_front = 1;
512
0
    }
513
    /* swap to front if necessary, or move to next result */
514
0
    if(swap_to_front && prev) {
515
0
      n = a->next_result;
516
0
      prev->next_result = n;
517
0
      a->next_result = dp->result_list;
518
0
      dp->result_list = a;
519
0
      a = n;
520
0
    } else {
521
0
      prev = a;
522
0
      a = a->next_result;
523
0
    }
524
0
  }
525
0
  *selected_rtt = low_rtt;
526
527
0
  if (env->cfg->prefer_ip6) {
528
0
    int got_num6 = 0;
529
0
    int low_rtt6 = 0;
530
0
    int i;
531
0
    int attempt = -1; /* filter to make sure addresses have
532
      less attempts on them than the first, to force round
533
      robin when all the IPv6 addresses fail */
534
0
    int num4ok = 0; /* number ip4 at low attempt count */
535
0
    int num4_lowrtt = 0;
536
0
    prev = NULL;
537
0
    a = dp->result_list;
538
0
    for(i = 0; i < got_num; i++) {
539
0
      if(!a) break; /* robustness */
540
0
      swap_to_front = 0;
541
0
      if(a->addr.ss_family != AF_INET6 && attempt == -1) {
542
        /* if we only have ip4 at low attempt count,
543
         * then ip6 is failing, and we need to
544
         * select one of the remaining IPv4 addrs */
545
0
        attempt = a->attempts;
546
0
        num4ok++;
547
0
        num4_lowrtt = a->sel_rtt;
548
0
      } else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) {
549
0
        num4ok++;
550
0
        if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) {
551
0
          num4_lowrtt = a->sel_rtt;
552
0
        }
553
0
      }
554
0
      if(a->addr.ss_family == AF_INET6) {
555
0
        if(attempt == -1) {
556
0
          attempt = a->attempts;
557
0
        } else if(a->attempts > attempt) {
558
0
          break;
559
0
        }
560
0
        got_num6++;
561
0
        swap_to_front = 1;
562
0
        if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) {
563
0
          low_rtt6 = a->sel_rtt;
564
0
        }
565
0
      }
566
      /* swap to front if IPv6, or move to next result */
567
0
      if(swap_to_front && prev) {
568
0
        n = a->next_result;
569
0
        prev->next_result = n;
570
0
        a->next_result = dp->result_list;
571
0
        dp->result_list = a;
572
0
        a = n;
573
0
      } else {
574
0
        prev = a;
575
0
        a = a->next_result;
576
0
      }
577
0
    }
578
0
    if(got_num6 > 0) {
579
0
      got_num = got_num6;
580
0
      *selected_rtt = low_rtt6;
581
0
    } else if(num4ok > 0) {
582
0
      got_num = num4ok;
583
0
      *selected_rtt = num4_lowrtt;
584
0
    }
585
0
  } else if (env->cfg->prefer_ip4) {
586
0
    int got_num4 = 0;
587
0
    int low_rtt4 = 0;
588
0
    int i;
589
0
    int attempt = -1; /* filter to make sure addresses have
590
      less attempts on them than the first, to force round
591
      robin when all the IPv4 addresses fail */
592
0
    int num6ok = 0; /* number ip6 at low attempt count */
593
0
    int num6_lowrtt = 0;
594
0
    prev = NULL;
595
0
    a = dp->result_list;
596
0
    for(i = 0; i < got_num; i++) {
597
0
      if(!a) break; /* robustness */
598
0
      swap_to_front = 0;
599
0
      if(a->addr.ss_family != AF_INET && attempt == -1) {
600
        /* if we only have ip6 at low attempt count,
601
         * then ip4 is failing, and we need to
602
         * select one of the remaining IPv6 addrs */
603
0
        attempt = a->attempts;
604
0
        num6ok++;
605
0
        num6_lowrtt = a->sel_rtt;
606
0
      } else if(a->addr.ss_family != AF_INET && attempt == a->attempts) {
607
0
        num6ok++;
608
0
        if(num6_lowrtt == 0 || a->sel_rtt < num6_lowrtt) {
609
0
          num6_lowrtt = a->sel_rtt;
610
0
        }
611
0
      }
612
0
      if(a->addr.ss_family == AF_INET) {
613
0
        if(attempt == -1) {
614
0
          attempt = a->attempts;
615
0
        } else if(a->attempts > attempt) {
616
0
          break;
617
0
        }
618
0
        got_num4++;
619
0
        swap_to_front = 1;
620
0
        if(low_rtt4 == 0 || a->sel_rtt < low_rtt4) {
621
0
          low_rtt4 = a->sel_rtt;
622
0
        }
623
0
      }
624
      /* swap to front if IPv4, or move to next result */
625
0
      if(swap_to_front && prev) {
626
0
        n = a->next_result;
627
0
        prev->next_result = n;
628
0
        a->next_result = dp->result_list;
629
0
        dp->result_list = a;
630
0
        a = n;
631
0
      } else {
632
0
        prev = a;
633
0
        a = a->next_result;
634
0
      }
635
0
    }
636
0
    if(got_num4 > 0) {
637
0
      got_num = got_num4;
638
0
      *selected_rtt = low_rtt4;
639
0
    } else if(num6ok > 0) {
640
0
      got_num = num6ok;
641
0
      *selected_rtt = num6_lowrtt;
642
0
    }
643
0
  }
644
0
  return got_num;
645
0
}
646
647
struct delegpt_addr*
648
iter_server_selection(struct iter_env* iter_env,
649
  struct module_env* env, struct delegpt* dp,
650
  uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
651
  int* chase_to_rd, int open_target, struct sock_list* blacklist,
652
  time_t prefetch)
653
0
{
654
0
  int sel;
655
0
  int selrtt;
656
0
  struct delegpt_addr* a, *prev;
657
0
  int num = iter_filter_order(iter_env, env, name, namelen, qtype,
658
0
    *env->now, dp, &selrtt, open_target, blacklist, prefetch);
659
660
0
  if(num == 0)
661
0
    return NULL;
662
0
  verbose(VERB_ALGO, "selrtt %d", selrtt);
663
0
  if(selrtt > BLACKLIST_PENALTY) {
664
0
    if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
665
0
      verbose(VERB_ALGO, "chase to "
666
0
        "blacklisted recursion lame server");
667
0
      *chase_to_rd = 1;
668
0
    }
669
0
    if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
670
0
      verbose(VERB_ALGO, "chase to "
671
0
        "blacklisted dnssec lame server");
672
0
      *dnssec_lame = 1;
673
0
    }
674
0
  } else {
675
0
    if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
676
0
      verbose(VERB_ALGO, "chase to recursion lame server");
677
0
      *chase_to_rd = 1;
678
0
    }
679
0
    if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
680
0
      verbose(VERB_ALGO, "chase to dnssec lame server");
681
0
      *dnssec_lame = 1;
682
0
    }
683
0
    if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
684
0
      verbose(VERB_ALGO, "chase to blacklisted lame server");
685
0
      return NULL;
686
0
    }
687
0
  }
688
689
0
  if(num == 1) {
690
0
    a = dp->result_list;
691
0
    if(++a->attempts < iter_env->outbound_msg_retry)
692
0
      return a;
693
0
    dp->result_list = a->next_result;
694
0
    return a;
695
0
  }
696
697
  /* randomly select a target from the list */
698
0
  log_assert(num > 1);
699
  /* grab secure random number, to pick unexpected server.
700
   * also we need it to be threadsafe. */
701
0
  sel = ub_random_max(env->rnd, num);
702
0
  a = dp->result_list;
703
0
  prev = NULL;
704
0
  while(sel > 0 && a) {
705
0
    prev = a;
706
0
    a = a->next_result;
707
0
    sel--;
708
0
  }
709
0
  if(!a)  /* robustness */
710
0
    return NULL;
711
0
  if(++a->attempts < iter_env->outbound_msg_retry)
712
0
    return a;
713
  /* remove it from the delegation point result list */
714
0
  if(prev)
715
0
    prev->next_result = a->next_result;
716
0
  else  dp->result_list = a->next_result;
717
0
  return a;
718
0
}
719
720
struct dns_msg*
721
dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg,
722
  struct regional* region)
723
0
{
724
0
  struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
725
0
    sizeof(struct dns_msg));
726
0
  if(!m)
727
0
    return NULL;
728
0
  memset(m, 0, sizeof(*m));
729
0
  if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
730
0
    log_err("malloc failure: allocating incoming dns_msg");
731
0
    return NULL;
732
0
  }
733
0
  return m;
734
0
}
735
736
struct dns_msg*
737
dns_copy_msg(struct dns_msg* from, struct regional* region)
738
0
{
739
0
  struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
740
0
    sizeof(struct dns_msg));
741
0
  if(!m)
742
0
    return NULL;
743
0
  m->qinfo = from->qinfo;
744
0
  if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
745
0
    from->qinfo.qname_len)))
746
0
    return NULL;
747
0
  if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
748
0
    return NULL;
749
0
  return m;
750
0
}
751
752
void
753
iter_dns_store(struct module_env* env, struct query_info* msgqinf,
754
  struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
755
  struct regional* region, uint16_t flags, time_t qstarttime,
756
  int is_valrec)
757
0
{
758
0
  if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
759
0
    pside, region, flags, qstarttime, is_valrec))
760
0
    log_err("out of memory: cannot store data in cache");
761
0
}
762
763
int
764
iter_ns_probability(struct ub_randstate* rnd, int n, int m)
765
0
{
766
0
  int sel;
767
0
  if(n == m) /* 100% chance */
768
0
    return 1;
769
  /* we do not need secure random numbers here, but
770
   * we do need it to be threadsafe, so we use this */
771
0
  sel = ub_random_max(rnd, m);
772
0
  return (sel < n);
773
0
}
774
775
/** detect dependency cycle for query and target */
776
static int
777
causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
778
  uint16_t t, uint16_t c)
779
0
{
780
0
  struct query_info qinf;
781
0
  qinf.qname = name;
782
0
  qinf.qname_len = namelen;
783
0
  qinf.qtype = t;
784
0
  qinf.qclass = c;
785
0
  qinf.local_alias = NULL;
786
0
  fptr_ok(fptr_whitelist_modenv_detect_cycle(
787
0
    qstate->env->detect_cycle));
788
0
  return (*qstate->env->detect_cycle)(qstate, &qinf,
789
0
    (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming,
790
0
    qstate->is_valrec);
791
0
}
792
793
void
794
iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
795
0
{
796
0
  struct delegpt_ns* ns;
797
0
  for(ns = dp->nslist; ns; ns = ns->next) {
798
0
    if(ns->resolved)
799
0
      continue;
800
    /* see if this ns as target causes dependency cycle */
801
0
    if(causes_cycle(qstate, ns->name, ns->namelen,
802
0
      LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
803
0
       causes_cycle(qstate, ns->name, ns->namelen,
804
0
      LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
805
0
      log_nametypeclass(VERB_QUERY, "skipping target due "
806
0
        "to dependency cycle (harden-glue: no may "
807
0
        "fix some of the cycles)",
808
0
        ns->name, LDNS_RR_TYPE_A,
809
0
        qstate->qinfo.qclass);
810
0
      ns->resolved = 1;
811
0
    }
812
0
  }
813
0
}
814
815
void
816
iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
817
0
{
818
0
  struct delegpt_ns* ns;
819
0
  for(ns = dp->nslist; ns; ns = ns->next) {
820
0
    if(ns->done_pside4 && ns->done_pside6)
821
0
      continue;
822
    /* see if this ns as target causes dependency cycle */
823
0
    if(causes_cycle(qstate, ns->name, ns->namelen,
824
0
      LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
825
0
      log_nametypeclass(VERB_QUERY, "skipping target due "
826
0
        "to dependency cycle", ns->name,
827
0
        LDNS_RR_TYPE_A, qstate->qinfo.qclass);
828
0
      ns->done_pside4 = 1;
829
0
    }
830
0
    if(causes_cycle(qstate, ns->name, ns->namelen,
831
0
      LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
832
0
      log_nametypeclass(VERB_QUERY, "skipping target due "
833
0
        "to dependency cycle", ns->name,
834
0
        LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
835
0
      ns->done_pside6 = 1;
836
0
    }
837
0
  }
838
0
}
839
840
int
841
iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
842
  struct delegpt* dp, int supports_ipv4, int supports_ipv6,
843
  int use_nat64)
844
0
{
845
0
  struct delegpt_ns* ns;
846
0
  struct delegpt_addr* a;
847
848
0
  if(supports_ipv6 && use_nat64)
849
0
    supports_ipv4 = 1;
850
851
  /* check:
852
   *      o RD qflag is on.
853
   *      o no addresses are provided.
854
   *      o all NS items are required glue.
855
   * OR
856
   *      o RD qflag is on.
857
   *      o no addresses are provided.
858
   *      o the query is for one of the nameservers in dp,
859
   *        and that nameserver is a glue-name for this dp.
860
   */
861
0
  if(!(qflags&BIT_RD))
862
0
    return 0;
863
  /* either available or unused targets,
864
   * if they exist, the dp is not useless. */
865
0
  for(a = dp->usable_list; a; a = a->next_usable) {
866
0
    if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
867
0
      return 0;
868
0
    else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
869
0
      return 0;
870
0
  }
871
0
  for(a = dp->result_list; a; a = a->next_result) {
872
0
    if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
873
0
      return 0;
874
0
    else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
875
0
      return 0;
876
0
  }
877
878
  /* see if query is for one of the nameservers, which is glue */
879
0
  if( ((qinfo->qtype == LDNS_RR_TYPE_A && supports_ipv4) ||
880
0
    (qinfo->qtype == LDNS_RR_TYPE_AAAA && supports_ipv6)) &&
881
0
    dname_subdomain_c(qinfo->qname, dp->name) &&
882
0
    delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
883
0
    return 1;
884
885
0
  for(ns = dp->nslist; ns; ns = ns->next) {
886
0
    if(ns->resolved) /* skip failed targets */
887
0
      continue;
888
0
    if(!dname_subdomain_c(ns->name, dp->name))
889
0
      return 0; /* one address is not required glue */
890
0
  }
891
0
  return 1;
892
0
}
893
894
int
895
iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo)
896
0
{
897
0
  struct trust_anchor* a;
898
0
  if(!env || !env->anchors || !qinfo || !qinfo->qname)
899
0
    return 0;
900
  /* a trust anchor exists above the name? */
901
0
  if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len,
902
0
    qinfo->qclass))) {
903
0
    if(a->numDS == 0 && a->numDNSKEY == 0) {
904
      /* insecure trust point */
905
0
      lock_basic_unlock(&a->lock);
906
0
      return 0;
907
0
    }
908
0
    lock_basic_unlock(&a->lock);
909
0
    return 1;
910
0
  }
911
  /* no trust anchor above it. */
912
0
  return 0;
913
0
}
914
915
int
916
iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
917
        struct dns_msg* msg, uint16_t dclass)
918
0
{
919
0
  struct trust_anchor* a;
920
  /* information not available, !env->anchors can be common */
921
0
  if(!env || !env->anchors || !dp || !dp->name)
922
0
    return 0;
923
  /* a trust anchor exists with this name, RRSIGs expected */
924
0
  if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
925
0
    dclass))) {
926
0
    if(a->numDS == 0 && a->numDNSKEY == 0) {
927
      /* insecure trust point */
928
0
      lock_basic_unlock(&a->lock);
929
0
      return 0;
930
0
    }
931
0
    lock_basic_unlock(&a->lock);
932
0
    return 1;
933
0
  }
934
  /* see if DS rrset was given, in AUTH section */
935
0
  if(msg && msg->rep &&
936
0
    reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
937
0
    LDNS_RR_TYPE_DS, dclass))
938
0
    return 1;
939
  /* look in key cache */
940
0
  if(env->key_cache) {
941
0
    struct key_entry_key* kk = key_cache_obtain(env->key_cache,
942
0
      dp->name, dp->namelen, dclass, env->scratch, *env->now);
943
0
    if(kk) {
944
0
      if(query_dname_compare(kk->name, dp->name) == 0) {
945
0
        if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
946
0
        regional_free_all(env->scratch);
947
0
        return 1;
948
0
        } else if(key_entry_isnull(kk)) {
949
0
        regional_free_all(env->scratch);
950
0
        return 0;
951
0
        }
952
0
      }
953
0
      regional_free_all(env->scratch);
954
0
    }
955
0
  }
956
0
  return 0;
957
0
}
958
959
int
960
iter_msg_has_dnssec(struct dns_msg* msg)
961
0
{
962
0
  size_t i;
963
0
  if(!msg || !msg->rep)
964
0
    return 0;
965
0
  for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
966
0
    if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
967
0
      entry.data)->rrsig_count > 0)
968
0
      return 1;
969
0
  }
970
  /* empty message has no DNSSEC info, with DNSSEC the reply is
971
   * not empty (NSEC) */
972
0
  return 0;
973
0
}
974
975
int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
976
        enum response_type type, uint16_t dclass)
977
0
{
978
0
  if(!msg || !dp || !msg->rep || !dp->name)
979
0
    return 0;
980
  /* SOA RRset - always from reply zone */
981
0
  if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
982
0
    LDNS_RR_TYPE_SOA, dclass) ||
983
0
     reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
984
0
    LDNS_RR_TYPE_SOA, dclass))
985
0
    return 1;
986
0
  if(type == RESPONSE_TYPE_REFERRAL) {
987
0
    size_t i;
988
    /* if it adds a single label, i.e. we expect .com,
989
     * and referral to example.com. NS ... , then origin zone
990
     * is .com. For a referral to sub.example.com. NS ... then
991
     * we do not know, since example.com. may be in between. */
992
0
    for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
993
0
      i++) {
994
0
      struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
995
0
      if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
996
0
        ntohs(s->rk.rrset_class) == dclass) {
997
0
        int l = dname_count_labels(s->rk.dname);
998
0
        if(l == dp->namelabs + 1 &&
999
0
          dname_strict_subdomain(s->rk.dname,
1000
0
          l, dp->name, dp->namelabs))
1001
0
          return 1;
1002
0
      }
1003
0
    }
1004
0
    return 0;
1005
0
  }
1006
0
  log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
1007
  /* not a referral, and not lame delegation (upwards), so,
1008
   * any NS rrset must be from the zone itself */
1009
0
  if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
1010
0
    LDNS_RR_TYPE_NS, dclass) ||
1011
0
     reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
1012
0
    LDNS_RR_TYPE_NS, dclass))
1013
0
    return 1;
1014
  /* a DNSKEY set is expected at the zone apex as well */
1015
  /* this is for 'minimal responses' for DNSKEYs */
1016
0
  if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
1017
0
    LDNS_RR_TYPE_DNSKEY, dclass))
1018
0
    return 1;
1019
0
  return 0;
1020
0
}
1021
1022
/**
1023
 * check equality of two rrsets
1024
 * @param k1: rrset
1025
 * @param k2: rrset
1026
 * @return true if equal
1027
 */
1028
static int
1029
rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
1030
0
{
1031
0
  struct packed_rrset_data* d1 = (struct packed_rrset_data*)
1032
0
    k1->entry.data;
1033
0
  struct packed_rrset_data* d2 = (struct packed_rrset_data*)
1034
0
    k2->entry.data;
1035
0
  size_t i, t;
1036
0
  if(k1->rk.dname_len != k2->rk.dname_len ||
1037
0
    k1->rk.flags != k2->rk.flags ||
1038
0
    k1->rk.type != k2->rk.type ||
1039
0
    k1->rk.rrset_class != k2->rk.rrset_class ||
1040
0
    query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
1041
0
    return 0;
1042
0
  if( /* do not check ttl: d1->ttl != d2->ttl || */
1043
0
    d1->count != d2->count ||
1044
0
    d1->rrsig_count != d2->rrsig_count ||
1045
0
    d1->trust != d2->trust ||
1046
0
    d1->security != d2->security)
1047
0
    return 0;
1048
0
  t = d1->count + d1->rrsig_count;
1049
0
  for(i=0; i<t; i++) {
1050
0
    if(d1->rr_len[i] != d2->rr_len[i] ||
1051
      /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/
1052
0
      memcmp(d1->rr_data[i], d2->rr_data[i],
1053
0
        d1->rr_len[i]) != 0)
1054
0
      return 0;
1055
0
  }
1056
0
  return 1;
1057
0
}
1058
1059
/** compare rrsets and sort canonically.  Compares rrset name, type, class.
1060
 * return 0 if equal, +1 if x > y, and -1 if x < y.
1061
 */
1062
static int
1063
rrset_canonical_sort_cmp(const void* x, const void* y)
1064
0
{
1065
0
  struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x;
1066
0
  struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y;
1067
0
  int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname);
1068
0
  if(r != 0)
1069
0
    return r;
1070
0
  if(rrx->rk.type != rry->rk.type) {
1071
0
    if(ntohs(rrx->rk.type) > ntohs(rry->rk.type))
1072
0
      return 1;
1073
0
    else  return -1;
1074
0
  }
1075
0
  if(rrx->rk.rrset_class != rry->rk.rrset_class) {
1076
0
    if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class))
1077
0
      return 1;
1078
0
    else  return -1;
1079
0
  }
1080
0
  return 0;
1081
0
}
1082
1083
int
1084
reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region)
1085
0
{
1086
0
  size_t i;
1087
0
  struct ub_packed_rrset_key** sorted_p, **sorted_q;
1088
0
  if(p->flags != q->flags ||
1089
0
    p->qdcount != q->qdcount ||
1090
    /* do not check TTL, this may differ */
1091
    /*
1092
    p->ttl != q->ttl ||
1093
    p->prefetch_ttl != q->prefetch_ttl ||
1094
    */
1095
0
    p->security != q->security ||
1096
0
    p->an_numrrsets != q->an_numrrsets ||
1097
0
    p->ns_numrrsets != q->ns_numrrsets ||
1098
0
    p->ar_numrrsets != q->ar_numrrsets ||
1099
0
    p->rrset_count != q->rrset_count)
1100
0
    return 0;
1101
  /* sort the rrsets in the authority and additional sections before
1102
   * compare, the query and answer sections are ordered in the sequence
1103
   * they should have (eg. one after the other for aliases). */
1104
0
  sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init(
1105
0
    region, p->rrsets, sizeof(*sorted_p)*p->rrset_count);
1106
0
  if(!sorted_p) return 0;
1107
0
  log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <=
1108
0
    p->rrset_count);
1109
0
  qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets,
1110
0
    sizeof(*sorted_p), rrset_canonical_sort_cmp);
1111
0
  qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets,
1112
0
    sizeof(*sorted_p), rrset_canonical_sort_cmp);
1113
1114
0
  sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init(
1115
0
    region, q->rrsets, sizeof(*sorted_q)*q->rrset_count);
1116
0
  if(!sorted_q) {
1117
0
    regional_free_all(region);
1118
0
    return 0;
1119
0
  }
1120
0
  log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <=
1121
0
    q->rrset_count);
1122
0
  qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets,
1123
0
    sizeof(*sorted_q), rrset_canonical_sort_cmp);
1124
0
  qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets,
1125
0
    sizeof(*sorted_q), rrset_canonical_sort_cmp);
1126
1127
  /* compare the rrsets */
1128
0
  for(i=0; i<p->rrset_count; i++) {
1129
0
    if(!rrset_equal(sorted_p[i], sorted_q[i])) {
1130
0
      if(!rrset_canonical_equal(region, sorted_p[i],
1131
0
        sorted_q[i])) {
1132
0
        regional_free_all(region);
1133
0
        return 0;
1134
0
      }
1135
0
    }
1136
0
  }
1137
0
  regional_free_all(region);
1138
0
  return 1;
1139
0
}
1140
1141
void
1142
caps_strip_reply(struct reply_info* rep)
1143
0
{
1144
0
  size_t i;
1145
0
  if(!rep) return;
1146
  /* see if message is a referral, in which case the additional and
1147
   * NS record cannot be removed */
1148
  /* referrals have the AA flag unset (strict check, not elsewhere in
1149
   * unbound, but for 0x20 this is very convenient). */
1150
0
  if(!(rep->flags&BIT_AA))
1151
0
    return;
1152
  /* remove the additional section from the reply */
1153
0
  if(rep->ar_numrrsets != 0) {
1154
0
    verbose(VERB_ALGO, "caps fallback: removing additional section");
1155
0
    rep->rrset_count -= rep->ar_numrrsets;
1156
0
    rep->ar_numrrsets = 0;
1157
0
  }
1158
  /* is there an NS set in the authority section to remove? */
1159
  /* the failure case (Cisco firewalls) only has one rrset in authsec */
1160
0
  for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
1161
0
    struct ub_packed_rrset_key* s = rep->rrsets[i];
1162
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) {
1163
      /* remove NS rrset and break from loop (loop limits
1164
       * have changed) */
1165
      /* move last rrset into this position (there is no
1166
       * additional section any more) */
1167
0
      verbose(VERB_ALGO, "caps fallback: removing NS rrset");
1168
0
      if(i < rep->rrset_count-1)
1169
0
        rep->rrsets[i]=rep->rrsets[rep->rrset_count-1];
1170
0
      rep->rrset_count --;
1171
0
      rep->ns_numrrsets --;
1172
0
      break;
1173
0
    }
1174
0
  }
1175
0
}
1176
1177
int caps_failed_rcode(struct reply_info* rep)
1178
0
{
1179
0
  return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
1180
0
    FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN);
1181
0
}
1182
1183
void
1184
iter_store_parentside_rrset(struct module_env* env,
1185
  struct ub_packed_rrset_key* rrset)
1186
0
{
1187
0
  struct rrset_ref ref;
1188
0
  rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
1189
0
  if(!rrset) {
1190
0
    log_err("malloc failure in store_parentside_rrset");
1191
0
    return;
1192
0
  }
1193
0
  rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
1194
0
  rrset->entry.hash = rrset_key_hash(&rrset->rk);
1195
0
  ref.key = rrset;
1196
0
  ref.id = rrset->id;
1197
  /* ignore ret: if it was in the cache, ref updated */
1198
0
  (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
1199
0
}
1200
1201
/** fetch NS record from reply, if any */
1202
static struct ub_packed_rrset_key*
1203
reply_get_NS_rrset(struct reply_info* rep)
1204
0
{
1205
0
  size_t i;
1206
0
  for(i=0; i<rep->rrset_count; i++) {
1207
0
    if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
1208
0
      return rep->rrsets[i];
1209
0
    }
1210
0
  }
1211
0
  return NULL;
1212
0
}
1213
1214
void
1215
iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
1216
0
{
1217
0
  struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1218
0
  if(rrset) {
1219
0
    log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
1220
0
    iter_store_parentside_rrset(env, rrset);
1221
0
  }
1222
0
}
1223
1224
void iter_store_parentside_neg(struct module_env* env,
1225
        struct query_info* qinfo, struct reply_info* rep)
1226
0
{
1227
  /* TTL: NS from referral in iq->deleg_msg,
1228
   *      or first RR from iq->response,
1229
   *      or servfail5secs if !iq->response */
1230
0
  time_t ttl = NORR_TTL;
1231
0
  struct ub_packed_rrset_key* neg;
1232
0
  struct packed_rrset_data* newd;
1233
0
  if(rep) {
1234
0
    struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
1235
0
    if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
1236
0
    if(rrset) ttl = ub_packed_rrset_ttl(rrset);
1237
0
  }
1238
  /* create empty rrset to store */
1239
0
  neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
1240
0
                  sizeof(struct ub_packed_rrset_key));
1241
0
  if(!neg) {
1242
0
    log_err("out of memory in store_parentside_neg");
1243
0
    return;
1244
0
  }
1245
0
  memset(&neg->entry, 0, sizeof(neg->entry));
1246
0
  neg->entry.key = neg;
1247
0
  neg->rk.type = htons(qinfo->qtype);
1248
0
  neg->rk.rrset_class = htons(qinfo->qclass);
1249
0
  neg->rk.flags = 0;
1250
0
  neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
1251
0
    qinfo->qname_len);
1252
0
  if(!neg->rk.dname) {
1253
0
    log_err("out of memory in store_parentside_neg");
1254
0
    return;
1255
0
  }
1256
0
  neg->rk.dname_len = qinfo->qname_len;
1257
0
  neg->entry.hash = rrset_key_hash(&neg->rk);
1258
0
  newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
1259
0
    sizeof(struct packed_rrset_data) + sizeof(size_t) +
1260
0
    sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t));
1261
0
  if(!newd) {
1262
0
    log_err("out of memory in store_parentside_neg");
1263
0
    return;
1264
0
  }
1265
0
  neg->entry.data = newd;
1266
0
  newd->ttl = ttl;
1267
  /* entry must have one RR, otherwise not valid in cache.
1268
   * put in one RR with empty rdata: those are ignored as nameserver */
1269
0
  newd->count = 1;
1270
0
  newd->rrsig_count = 0;
1271
0
  newd->trust = rrset_trust_ans_noAA;
1272
0
  newd->rr_len = (size_t*)((uint8_t*)newd +
1273
0
    sizeof(struct packed_rrset_data));
1274
0
  newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
1275
0
  packed_rrset_ptr_fixup(newd);
1276
0
  newd->rr_ttl[0] = newd->ttl;
1277
0
  sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
1278
  /* store it */
1279
0
  log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
1280
0
  iter_store_parentside_rrset(env, neg);
1281
0
}
1282
1283
int
1284
iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
1285
  struct regional* region, struct query_info* qinfo)
1286
0
{
1287
0
  struct ub_packed_rrset_key* akey;
1288
0
  akey = rrset_cache_lookup(env->rrset_cache, dp->name,
1289
0
    dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
1290
0
    PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1291
0
  if(akey) {
1292
0
    log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
1293
0
    dp->has_parent_side_NS = 1;
1294
    /* and mark the new names as lame */
1295
0
    if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
1296
0
      lock_rw_unlock(&akey->entry.lock);
1297
0
      return 0;
1298
0
    }
1299
0
    lock_rw_unlock(&akey->entry.lock);
1300
0
  }
1301
0
  return 1;
1302
0
}
1303
1304
int iter_lookup_parent_glue_from_cache(struct module_env* env,
1305
        struct delegpt* dp, struct regional* region, struct query_info* qinfo)
1306
0
{
1307
0
  struct ub_packed_rrset_key* akey;
1308
0
  struct delegpt_ns* ns;
1309
0
  size_t num = delegpt_count_targets(dp);
1310
0
  for(ns = dp->nslist; ns; ns = ns->next) {
1311
0
    if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE)
1312
0
      continue;
1313
0
    ns->cache_lookup_count++;
1314
    /* get cached parentside A */
1315
0
    akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1316
0
      ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
1317
0
      PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1318
0
    if(akey) {
1319
0
      log_rrset_key(VERB_ALGO, "found parent-side", akey);
1320
0
      ns->done_pside4 = 1;
1321
      /* a negative-cache-element has no addresses it adds */
1322
0
      if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL))
1323
0
        log_err("malloc failure in lookup_parent_glue");
1324
0
      lock_rw_unlock(&akey->entry.lock);
1325
0
    }
1326
    /* get cached parentside AAAA */
1327
0
    akey = rrset_cache_lookup(env->rrset_cache, ns->name,
1328
0
      ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
1329
0
      PACKED_RRSET_PARENT_SIDE, *env->now, 0);
1330
0
    if(akey) {
1331
0
      log_rrset_key(VERB_ALGO, "found parent-side", akey);
1332
0
      ns->done_pside6 = 1;
1333
      /* a negative-cache-element has no addresses it adds */
1334
0
      if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL))
1335
0
        log_err("malloc failure in lookup_parent_glue");
1336
0
      lock_rw_unlock(&akey->entry.lock);
1337
0
    }
1338
0
  }
1339
  /* see if new (but lame) addresses have become available */
1340
0
  return delegpt_count_targets(dp) != num;
1341
0
}
1342
1343
int
1344
iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
1345
  uint16_t* c)
1346
0
{
1347
0
  uint16_t c1 = *c, c2 = *c;
1348
0
  int r1, r2;
1349
0
  int nolock = 1;
1350
1351
  /* prelock both forwards and hints for atomic read. */
1352
0
  lock_rw_rdlock(&fwd->lock);
1353
0
  lock_rw_rdlock(&hints->lock);
1354
0
  r1 = hints_next_root(hints, &c1, nolock);
1355
0
  r2 = forwards_next_root(fwd, &c2, nolock);
1356
0
  lock_rw_unlock(&fwd->lock);
1357
0
  lock_rw_unlock(&hints->lock);
1358
1359
0
  if(!r1 && !r2) /* got none, end of list */
1360
0
    return 0;
1361
0
  else if(!r1) /* got one, return that */
1362
0
    *c = c2;
1363
0
  else if(!r2)
1364
0
    *c = c1;
1365
0
  else if(c1 < c2) /* got both take smallest */
1366
0
    *c = c1;
1367
0
  else  *c = c2;
1368
0
  return 1;
1369
0
}
1370
1371
void
1372
iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
1373
0
{
1374
  /* Only the DS record for the delegation itself is expected.
1375
   * We allow DS for everything between the bailiwick and the
1376
   * zonecut, thus DS records must be at or above the zonecut.
1377
   * And the DS records must be below the server authority zone.
1378
   * The answer section is already scrubbed. */
1379
0
  size_t i = msg->rep->an_numrrsets;
1380
0
  while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
1381
0
    struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1382
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
1383
0
      (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
1384
0
      || query_dname_compare(z, s->rk.dname) == 0)) {
1385
0
      log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
1386
0
        s->rk.dname, ntohs(s->rk.type),
1387
0
        ntohs(s->rk.rrset_class));
1388
0
      memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
1389
0
        sizeof(struct ub_packed_rrset_key*) *
1390
0
        (msg->rep->rrset_count-i-1));
1391
0
      msg->rep->ns_numrrsets--;
1392
0
      msg->rep->rrset_count--;
1393
      /* stay at same i, but new record */
1394
0
      continue;
1395
0
    }
1396
0
    i++;
1397
0
  }
1398
0
}
1399
1400
void
1401
iter_scrub_nxdomain(struct dns_msg* msg)
1402
0
{
1403
0
  if(msg->rep->an_numrrsets == 0)
1404
0
    return;
1405
1406
0
  memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets,
1407
0
    sizeof(struct ub_packed_rrset_key*) *
1408
0
    (msg->rep->rrset_count-msg->rep->an_numrrsets));
1409
0
  msg->rep->rrset_count -= msg->rep->an_numrrsets;
1410
0
  msg->rep->an_numrrsets = 0;
1411
0
}
1412
1413
void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry)
1414
0
{
1415
0
  struct delegpt_addr* a;
1416
0
  for(a=dp->target_list; a; a = a->next_target) {
1417
0
    if(a->attempts >= outbound_msg_retry) {
1418
      /* add back to result list */
1419
0
      delegpt_add_to_result_list(dp, a);
1420
0
    }
1421
0
    if(a->attempts > d)
1422
0
      a->attempts -= d;
1423
0
    else a->attempts = 0;
1424
0
  }
1425
0
}
1426
1427
void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old,
1428
  int outbound_msg_retry)
1429
0
{
1430
0
  struct delegpt_addr* a, *o, *prev;
1431
0
  for(a=dp->target_list; a; a = a->next_target) {
1432
0
    o = delegpt_find_addr(old, &a->addr, a->addrlen);
1433
0
    if(o) {
1434
0
      log_addr(VERB_ALGO, "copy attempt count previous dp",
1435
0
        &a->addr, a->addrlen);
1436
0
      a->attempts = o->attempts;
1437
0
    }
1438
0
  }
1439
0
  prev = NULL;
1440
0
  a = dp->usable_list;
1441
0
  while(a) {
1442
0
    if(a->attempts >= outbound_msg_retry) {
1443
0
      log_addr(VERB_ALGO, "remove from usable list dp",
1444
0
        &a->addr, a->addrlen);
1445
      /* remove from result list */
1446
0
      if(prev)
1447
0
        prev->next_usable = a->next_usable;
1448
0
      else  dp->usable_list = a->next_usable;
1449
      /* prev stays the same */
1450
0
      a = a->next_usable;
1451
0
      continue;
1452
0
    }
1453
0
    prev = a;
1454
0
    a = a->next_usable;
1455
0
  }
1456
0
}
1457
1458
int
1459
iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
1460
0
{
1461
  /* if for query example.com, there is example.com SOA or a subdomain
1462
   * of example.com, then we are too low and need to fetch NS. */
1463
0
  size_t i;
1464
  /* if we have a DNAME or CNAME we are probably wrong */
1465
  /* if we have a qtype DS in the answer section, its fine */
1466
0
  for(i=0; i < msg->rep->an_numrrsets; i++) {
1467
0
    struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1468
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
1469
0
      ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
1470
      /* not the right answer, maybe too low, check the
1471
       * RRSIG signer name (if there is any) for a hint
1472
       * that it is from the dp zone anyway */
1473
0
      uint8_t* sname;
1474
0
      size_t slen;
1475
0
      val_find_rrset_signer(s, &sname, &slen);
1476
0
      if(sname && query_dname_compare(dp->name, sname)==0)
1477
0
        return 0; /* it is fine, from the right dp */
1478
0
      return 1;
1479
0
    }
1480
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
1481
0
      return 0; /* fine, we have a DS record */
1482
0
  }
1483
0
  for(i=msg->rep->an_numrrsets;
1484
0
    i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
1485
0
    struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1486
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1487
0
      if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
1488
0
        return 1; /* point is too low */
1489
0
      if(query_dname_compare(s->rk.dname, dp->name)==0)
1490
0
        return 0; /* right dp */
1491
0
    }
1492
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1493
0
      ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1494
0
      uint8_t* sname;
1495
0
      size_t slen;
1496
0
      val_find_rrset_signer(s, &sname, &slen);
1497
0
      if(sname && query_dname_compare(dp->name, sname)==0)
1498
0
        return 0; /* it is fine, from the right dp */
1499
0
      return 1;
1500
0
    }
1501
0
  }
1502
  /* we do not know */
1503
0
  return 1;
1504
0
}
1505
1506
int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
1507
0
{
1508
  /* no delegation point, do not see how we can go down,
1509
   * robust check, it should really exist */
1510
0
  if(!dp) return 0;
1511
1512
  /* see if dp equals the qname, then we cannot go down further */
1513
0
  if(query_dname_compare(qinfo->qname, dp->name) == 0)
1514
0
    return 0;
1515
  /* if dp is one label above the name we also cannot go down further */
1516
0
  if(dname_count_labels(qinfo->qname) == dp->namelabs+1)
1517
0
    return 0;
1518
0
  return 1;
1519
0
}
1520
1521
int
1522
iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf,
1523
  uint8_t** retdpname, size_t* retdpnamelen, uint8_t* dpname_storage,
1524
  size_t dpname_storage_len)
1525
0
{
1526
0
  struct iter_hints_stub *stub;
1527
0
  struct delegpt *dp;
1528
0
  int nolock = 1;
1529
1530
  /* Check for stub. */
1531
  /* Lock both forwards and hints for atomic read. */
1532
0
  lock_rw_rdlock(&qstate->env->fwds->lock);
1533
0
  lock_rw_rdlock(&qstate->env->hints->lock);
1534
0
  stub = hints_lookup_stub(qstate->env->hints, qinf->qname,
1535
0
      qinf->qclass, NULL, nolock);
1536
0
  dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass,
1537
0
    nolock);
1538
1539
  /* see if forward or stub is more pertinent */
1540
0
  if(stub && stub->dp && dp) {
1541
0
    if(dname_strict_subdomain(dp->name, dp->namelabs,
1542
0
      stub->dp->name, stub->dp->namelabs)) {
1543
0
      stub = NULL; /* ignore stub, forward is lower */
1544
0
    } else {
1545
0
      dp = NULL; /* ignore forward, stub is lower */
1546
0
    }
1547
0
  }
1548
1549
  /* check stub */
1550
0
  if (stub != NULL && stub->dp != NULL) {
1551
0
    enum verbosity_value level = VERB_ALGO;
1552
0
    int stub_no_cache = stub->dp->no_cache;
1553
0
    lock_rw_unlock(&qstate->env->fwds->lock);
1554
0
    if(verbosity >= level && stub_no_cache) {
1555
0
      char qname[LDNS_MAX_DOMAINLEN];
1556
0
      char dpname[LDNS_MAX_DOMAINLEN];
1557
0
      dname_str(qinf->qname, qname);
1558
0
      dname_str(stub->dp->name, dpname);
1559
0
      verbose(level, "stub for %s %s has no_cache", qname, dpname);
1560
0
    }
1561
0
    if(retdpname) {
1562
0
      if(stub->dp->namelen > dpname_storage_len) {
1563
0
        verbose(VERB_ALGO, "no cache stub dpname too long");
1564
0
        lock_rw_unlock(&qstate->env->hints->lock);
1565
0
        *retdpname = NULL;
1566
0
        *retdpnamelen = 0;
1567
0
        return stub_no_cache;
1568
0
      }
1569
0
      memmove(dpname_storage, stub->dp->name,
1570
0
        stub->dp->namelen);
1571
0
      *retdpname = dpname_storage;
1572
0
      *retdpnamelen = stub->dp->namelen;
1573
0
    }
1574
0
    lock_rw_unlock(&qstate->env->hints->lock);
1575
0
    return stub_no_cache;
1576
0
  }
1577
1578
  /* Check for forward. */
1579
0
  if (dp) {
1580
0
    enum verbosity_value level = VERB_ALGO;
1581
0
    int dp_no_cache = dp->no_cache;
1582
0
    lock_rw_unlock(&qstate->env->hints->lock);
1583
0
    if(verbosity >= level && dp_no_cache) {
1584
0
      char qname[LDNS_MAX_DOMAINLEN];
1585
0
      char dpname[LDNS_MAX_DOMAINLEN];
1586
0
      dname_str(qinf->qname, qname);
1587
0
      dname_str(dp->name, dpname);
1588
0
      verbose(level, "forward for %s %s has no_cache", qname, dpname);
1589
0
    }
1590
0
    if(retdpname) {
1591
0
      if(dp->namelen > dpname_storage_len) {
1592
0
        verbose(VERB_ALGO, "no cache dpname too long");
1593
0
        lock_rw_unlock(&qstate->env->fwds->lock);
1594
0
        *retdpname = NULL;
1595
0
        *retdpnamelen = 0;
1596
0
        return dp_no_cache;
1597
0
      }
1598
0
      memmove(dpname_storage, dp->name, dp->namelen);
1599
0
      *retdpname = dpname_storage;
1600
0
      *retdpnamelen = dp->namelen;
1601
0
    }
1602
0
    lock_rw_unlock(&qstate->env->fwds->lock);
1603
0
    return dp_no_cache;
1604
0
  }
1605
0
  lock_rw_unlock(&qstate->env->fwds->lock);
1606
0
  lock_rw_unlock(&qstate->env->hints->lock);
1607
0
  if(retdpname) {
1608
0
    *retdpname = NULL;
1609
0
    *retdpnamelen = 0;
1610
0
  }
1611
0
  return 0;
1612
0
}
1613
1614
void iterator_set_ip46_support(struct module_stack* mods,
1615
  struct module_env* env, struct outside_network* outnet)
1616
0
{
1617
0
  int m = modstack_find(mods, "iterator");
1618
0
  struct iter_env* ie = NULL;
1619
0
  if(m == -1)
1620
0
    return;
1621
0
  ie = (struct iter_env*)env->modinfo[m];
1622
0
  if(outnet->pending == NULL)
1623
0
    return; /* we are in testbound, no rbtree for UDP */
1624
0
  if(outnet->num_ip4 == 0)
1625
0
    ie->supports_ipv4 = 0;
1626
0
  if(outnet->num_ip6 == 0)
1627
0
    ie->supports_ipv6 = 0;
1628
0
}
1629
1630
void
1631
limit_nsec_ttl(struct dns_msg* msg)
1632
0
{
1633
  /* Limit NSEC and NSEC3 TTL in response, RFC9077 */
1634
0
  size_t i;
1635
0
  int found = 0;
1636
0
  time_t soa_ttl = 0;
1637
  /* Limit the NSEC and NSEC3 TTL values to the SOA TTL and SOA minimum
1638
   * TTL. That has already been applied to the SOA record ttl. */
1639
0
  for(i=0; i<msg->rep->rrset_count; i++) {
1640
0
    struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1641
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
1642
0
      struct packed_rrset_data* soadata = (struct packed_rrset_data*)s->entry.data;
1643
0
      found = 1;
1644
0
      soa_ttl = soadata->ttl;
1645
0
      break;
1646
0
    }
1647
0
  }
1648
0
  if(!found)
1649
0
    return;
1650
0
  for(i=0; i<msg->rep->rrset_count; i++) {
1651
0
    struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
1652
0
    if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
1653
0
      ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1654
0
      struct packed_rrset_data* data = (struct packed_rrset_data*)s->entry.data;
1655
      /* Limit the negative TTL. */
1656
0
      if(data->ttl > soa_ttl) {
1657
0
        if(verbosity >= VERB_ALGO) {
1658
0
          char buf[256];
1659
0
          snprintf(buf, sizeof(buf),
1660
0
            "limiting TTL %d of %s record to the SOA TTL of %d for",
1661
0
            (int)data->ttl, ((ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC)?"NSEC":"NSEC3"), (int)soa_ttl);
1662
0
          log_nametypeclass(VERB_ALGO, buf,
1663
0
            s->rk.dname, ntohs(s->rk.type),
1664
0
            ntohs(s->rk.rrset_class));
1665
0
        }
1666
0
        data->ttl = soa_ttl;
1667
0
      }
1668
0
    }
1669
0
  }
1670
0
}
1671
1672
void
1673
iter_make_minimal(struct reply_info* rep)
1674
0
{
1675
0
  size_t rem = rep->ns_numrrsets + rep->ar_numrrsets;
1676
0
  rep->ns_numrrsets = 0;
1677
0
  rep->ar_numrrsets = 0;
1678
0
  rep->rrset_count -= rem;
1679
0
}