13b6c3722Schristos /*
23b6c3722Schristos * iterator/iter_utils.c - iterative resolver module utility functions.
33b6c3722Schristos *
43b6c3722Schristos * Copyright (c) 2007, NLnet Labs. All rights reserved.
53b6c3722Schristos *
63b6c3722Schristos * This software is open source.
73b6c3722Schristos *
83b6c3722Schristos * Redistribution and use in source and binary forms, with or without
93b6c3722Schristos * modification, are permitted provided that the following conditions
103b6c3722Schristos * are met:
113b6c3722Schristos *
123b6c3722Schristos * Redistributions of source code must retain the above copyright notice,
133b6c3722Schristos * this list of conditions and the following disclaimer.
143b6c3722Schristos *
153b6c3722Schristos * Redistributions in binary form must reproduce the above copyright notice,
163b6c3722Schristos * this list of conditions and the following disclaimer in the documentation
173b6c3722Schristos * and/or other materials provided with the distribution.
183b6c3722Schristos *
193b6c3722Schristos * Neither the name of the NLNET LABS nor the names of its contributors may
203b6c3722Schristos * be used to endorse or promote products derived from this software without
213b6c3722Schristos * specific prior written permission.
223b6c3722Schristos *
233b6c3722Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
243b6c3722Schristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
253b6c3722Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
263b6c3722Schristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
273b6c3722Schristos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283b6c3722Schristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
293b6c3722Schristos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303b6c3722Schristos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
313b6c3722Schristos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
323b6c3722Schristos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333b6c3722Schristos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
343b6c3722Schristos */
353b6c3722Schristos
363b6c3722Schristos /**
373b6c3722Schristos * \file
383b6c3722Schristos *
393b6c3722Schristos * This file contains functions to assist the iterator module.
403b6c3722Schristos * Configuration options. Forward zones.
413b6c3722Schristos */
423b6c3722Schristos #include "config.h"
433b6c3722Schristos #include "iterator/iter_utils.h"
443b6c3722Schristos #include "iterator/iterator.h"
453b6c3722Schristos #include "iterator/iter_hints.h"
463b6c3722Schristos #include "iterator/iter_fwd.h"
473b6c3722Schristos #include "iterator/iter_donotq.h"
483b6c3722Schristos #include "iterator/iter_delegpt.h"
493b6c3722Schristos #include "iterator/iter_priv.h"
503b6c3722Schristos #include "services/cache/infra.h"
513b6c3722Schristos #include "services/cache/dns.h"
523b6c3722Schristos #include "services/cache/rrset.h"
537a540f2bSchristos #include "services/outside_network.h"
543b6c3722Schristos #include "util/net_help.h"
553b6c3722Schristos #include "util/module.h"
563b6c3722Schristos #include "util/log.h"
573b6c3722Schristos #include "util/config_file.h"
583b6c3722Schristos #include "util/regional.h"
593b6c3722Schristos #include "util/data/msgparse.h"
603b6c3722Schristos #include "util/data/dname.h"
613b6c3722Schristos #include "util/random.h"
623b6c3722Schristos #include "util/fptr_wlist.h"
633b6c3722Schristos #include "validator/val_anchor.h"
643b6c3722Schristos #include "validator/val_kcache.h"
653b6c3722Schristos #include "validator/val_kentry.h"
663b6c3722Schristos #include "validator/val_utils.h"
673b6c3722Schristos #include "validator/val_sigcrypt.h"
683b6c3722Schristos #include "sldns/sbuffer.h"
693b6c3722Schristos #include "sldns/str2wire.h"
703b6c3722Schristos
713b6c3722Schristos /** time when nameserver glue is said to be 'recent' */
723b6c3722Schristos #define SUSPICION_RECENT_EXPIRY 86400
733b6c3722Schristos
74*91f7d55fSchristos /** if NAT64 is enabled and no NAT64 prefix is configured, first fall back to
75*91f7d55fSchristos * DNS64 prefix. If that is not configured, fall back to this default value.
76*91f7d55fSchristos */
77*91f7d55fSchristos static const char DEFAULT_NAT64_PREFIX[] = "64:ff9b::/96";
78*91f7d55fSchristos
793b6c3722Schristos /** fillup fetch policy array */
803b6c3722Schristos static void
fetch_fill(struct iter_env * ie,const char * str)813b6c3722Schristos fetch_fill(struct iter_env* ie, const char* str)
823b6c3722Schristos {
833b6c3722Schristos char* s = (char*)str, *e;
843b6c3722Schristos int i;
853b6c3722Schristos for(i=0; i<ie->max_dependency_depth+1; i++) {
863b6c3722Schristos ie->target_fetch_policy[i] = strtol(s, &e, 10);
873b6c3722Schristos if(s == e)
883b6c3722Schristos fatal_exit("cannot parse fetch policy number %s", s);
893b6c3722Schristos s = e;
903b6c3722Schristos }
913b6c3722Schristos }
923b6c3722Schristos
933b6c3722Schristos /** Read config string that represents the target fetch policy */
943b6c3722Schristos static int
read_fetch_policy(struct iter_env * ie,const char * str)953b6c3722Schristos read_fetch_policy(struct iter_env* ie, const char* str)
963b6c3722Schristos {
973b6c3722Schristos int count = cfg_count_numbers(str);
983b6c3722Schristos if(count < 1) {
993b6c3722Schristos log_err("Cannot parse target fetch policy: \"%s\"", str);
1003b6c3722Schristos return 0;
1013b6c3722Schristos }
1023b6c3722Schristos ie->max_dependency_depth = count - 1;
1033b6c3722Schristos ie->target_fetch_policy = (int*)calloc(
1043b6c3722Schristos (size_t)ie->max_dependency_depth+1, sizeof(int));
1053b6c3722Schristos if(!ie->target_fetch_policy) {
1063b6c3722Schristos log_err("alloc fetch policy: out of memory");
1073b6c3722Schristos return 0;
1083b6c3722Schristos }
1093b6c3722Schristos fetch_fill(ie, str);
1103b6c3722Schristos return 1;
1113b6c3722Schristos }
1123b6c3722Schristos
1133b6c3722Schristos /** apply config caps whitelist items to name tree */
1143b6c3722Schristos static int
caps_white_apply_cfg(rbtree_type * ntree,struct config_file * cfg)1150cd9f4ecSchristos caps_white_apply_cfg(rbtree_type* ntree, struct config_file* cfg)
1163b6c3722Schristos {
1173b6c3722Schristos struct config_strlist* p;
1183b6c3722Schristos for(p=cfg->caps_whitelist; p; p=p->next) {
1193b6c3722Schristos struct name_tree_node* n;
1203b6c3722Schristos size_t len;
1213b6c3722Schristos uint8_t* nm = sldns_str2wire_dname(p->str, &len);
1223b6c3722Schristos if(!nm) {
1233b6c3722Schristos log_err("could not parse %s", p->str);
1243b6c3722Schristos return 0;
1253b6c3722Schristos }
1263b6c3722Schristos n = (struct name_tree_node*)calloc(1, sizeof(*n));
1273b6c3722Schristos if(!n) {
1283b6c3722Schristos log_err("out of memory");
1293b6c3722Schristos free(nm);
1303b6c3722Schristos return 0;
1313b6c3722Schristos }
1323b6c3722Schristos n->node.key = n;
1333b6c3722Schristos n->name = nm;
1343b6c3722Schristos n->len = len;
1353b6c3722Schristos n->labs = dname_count_labels(nm);
1363b6c3722Schristos n->dclass = LDNS_RR_CLASS_IN;
1373b6c3722Schristos if(!name_tree_insert(ntree, n, nm, len, n->labs, n->dclass)) {
1383b6c3722Schristos /* duplicate element ignored, idempotent */
1393b6c3722Schristos free(n->name);
1403b6c3722Schristos free(n);
1413b6c3722Schristos }
1423b6c3722Schristos }
1433b6c3722Schristos name_tree_init_parents(ntree);
1443b6c3722Schristos return 1;
1453b6c3722Schristos }
1463b6c3722Schristos
1473b6c3722Schristos int
iter_apply_cfg(struct iter_env * iter_env,struct config_file * cfg)1483b6c3722Schristos iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
1493b6c3722Schristos {
150*91f7d55fSchristos const char *nat64_prefix;
1513b6c3722Schristos int i;
1523b6c3722Schristos /* target fetch policy */
1533b6c3722Schristos if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
1543b6c3722Schristos return 0;
1553b6c3722Schristos for(i=0; i<iter_env->max_dependency_depth+1; i++)
1563b6c3722Schristos verbose(VERB_QUERY, "target fetch policy for level %d is %d",
1573b6c3722Schristos i, iter_env->target_fetch_policy[i]);
1583b6c3722Schristos
1593b6c3722Schristos if(!iter_env->donotq)
1603b6c3722Schristos iter_env->donotq = donotq_create();
1613b6c3722Schristos if(!iter_env->donotq || !donotq_apply_cfg(iter_env->donotq, cfg)) {
1623b6c3722Schristos log_err("Could not set donotqueryaddresses");
1633b6c3722Schristos return 0;
1643b6c3722Schristos }
1653b6c3722Schristos if(!iter_env->priv)
1663b6c3722Schristos iter_env->priv = priv_create();
1673b6c3722Schristos if(!iter_env->priv || !priv_apply_cfg(iter_env->priv, cfg)) {
1683b6c3722Schristos log_err("Could not set private addresses");
1693b6c3722Schristos return 0;
1703b6c3722Schristos }
1713b6c3722Schristos if(cfg->caps_whitelist) {
1723b6c3722Schristos if(!iter_env->caps_white)
1733b6c3722Schristos iter_env->caps_white = rbtree_create(name_tree_compare);
1743b6c3722Schristos if(!iter_env->caps_white || !caps_white_apply_cfg(
1753b6c3722Schristos iter_env->caps_white, cfg)) {
1763b6c3722Schristos log_err("Could not set capsforid whitelist");
1773b6c3722Schristos return 0;
1783b6c3722Schristos }
1793b6c3722Schristos
1803b6c3722Schristos }
181*91f7d55fSchristos
182*91f7d55fSchristos nat64_prefix = cfg->nat64_prefix;
183*91f7d55fSchristos if(!nat64_prefix)
184*91f7d55fSchristos nat64_prefix = cfg->dns64_prefix;
185*91f7d55fSchristos if(!nat64_prefix)
186*91f7d55fSchristos nat64_prefix = DEFAULT_NAT64_PREFIX;
187*91f7d55fSchristos if(!netblockstrtoaddr(nat64_prefix, 0, &iter_env->nat64_prefix_addr,
188*91f7d55fSchristos &iter_env->nat64_prefix_addrlen,
189*91f7d55fSchristos &iter_env->nat64_prefix_net)) {
190*91f7d55fSchristos log_err("cannot parse nat64-prefix netblock: %s", nat64_prefix);
191*91f7d55fSchristos return 0;
192*91f7d55fSchristos }
193*91f7d55fSchristos if(!addr_is_ip6(&iter_env->nat64_prefix_addr,
194*91f7d55fSchristos iter_env->nat64_prefix_addrlen)) {
195*91f7d55fSchristos log_err("nat64-prefix is not IPv6: %s", cfg->nat64_prefix);
196*91f7d55fSchristos return 0;
197*91f7d55fSchristos }
198*91f7d55fSchristos if(!prefixnet_is_nat64(iter_env->nat64_prefix_net)) {
199*91f7d55fSchristos log_err("nat64-prefix length it not 32, 40, 48, 56, 64 or 96: %s",
200*91f7d55fSchristos nat64_prefix);
201*91f7d55fSchristos return 0;
202*91f7d55fSchristos }
203*91f7d55fSchristos
2043b6c3722Schristos iter_env->supports_ipv6 = cfg->do_ip6;
2053b6c3722Schristos iter_env->supports_ipv4 = cfg->do_ip4;
206*91f7d55fSchristos iter_env->use_nat64 = cfg->do_nat64;
2077a540f2bSchristos iter_env->outbound_msg_retry = cfg->outbound_msg_retry;
208*91f7d55fSchristos iter_env->max_sent_count = cfg->max_sent_count;
209*91f7d55fSchristos iter_env->max_query_restarts = cfg->max_query_restarts;
2103b6c3722Schristos return 1;
2113b6c3722Schristos }
2123b6c3722Schristos
2133b6c3722Schristos /** filter out unsuitable targets
2143b6c3722Schristos * @param iter_env: iterator environment with ipv6-support flag.
2153b6c3722Schristos * @param env: module environment with infra cache.
2163b6c3722Schristos * @param name: zone name
2173b6c3722Schristos * @param namelen: length of name
2183b6c3722Schristos * @param qtype: query type (host order).
2193b6c3722Schristos * @param now: current time
2203b6c3722Schristos * @param a: address in delegation point we are examining.
2213b6c3722Schristos * @return an integer that signals the target suitability.
2223b6c3722Schristos * as follows:
2233b6c3722Schristos * -1: The address should be omitted from the list.
2243b6c3722Schristos * Because:
2253b6c3722Schristos * o The address is bogus (DNSSEC validation failure).
2263b6c3722Schristos * o Listed as donotquery
2273b6c3722Schristos * o is ipv6 but no ipv6 support (in operating system).
2283b6c3722Schristos * o is ipv4 but no ipv4 support (in operating system).
2293b6c3722Schristos * o is lame
2303b6c3722Schristos * Otherwise, an rtt in milliseconds.
2313b6c3722Schristos * 0 .. USEFUL_SERVER_TOP_TIMEOUT-1
2323b6c3722Schristos * The roundtrip time timeout estimate. less than 2 minutes.
2333b6c3722Schristos * Note that util/rtt.c has a MIN_TIMEOUT of 50 msec, thus
2343b6c3722Schristos * values 0 .. 49 are not used, unless that is changed.
2353b6c3722Schristos * USEFUL_SERVER_TOP_TIMEOUT
2363b6c3722Schristos * This value exactly is given for unresponsive blacklisted.
2373b6c3722Schristos * USEFUL_SERVER_TOP_TIMEOUT+1
2383b6c3722Schristos * For non-blacklisted servers: huge timeout, but has traffic.
2393b6c3722Schristos * USEFUL_SERVER_TOP_TIMEOUT*1 ..
2403b6c3722Schristos * parent-side lame servers get this penalty. A dispreferential
2413b6c3722Schristos * server. (lame in delegpt).
2423b6c3722Schristos * USEFUL_SERVER_TOP_TIMEOUT*2 ..
2433b6c3722Schristos * dnsseclame servers get penalty
2443b6c3722Schristos * USEFUL_SERVER_TOP_TIMEOUT*3 ..
2453b6c3722Schristos * recursion lame servers get penalty
2463b6c3722Schristos * UNKNOWN_SERVER_NICENESS
2473b6c3722Schristos * If no information is known about the server, this is
2483b6c3722Schristos * returned. 376 msec or so.
2493b6c3722Schristos * +BLACKLIST_PENALTY (of USEFUL_TOP_TIMEOUT*4) for dnssec failed IPs.
2503b6c3722Schristos *
2513b6c3722Schristos * When a final value is chosen that is dnsseclame ; dnsseclameness checking
2523b6c3722Schristos * is turned off (so we do not discard the reply).
2533b6c3722Schristos * When a final value is chosen that is recursionlame; RD bit is set on query.
2543b6c3722Schristos * Because of the numbers this means recursionlame also have dnssec lameness
2553b6c3722Schristos * checking turned off.
2563b6c3722Schristos */
2573b6c3722Schristos static int
iter_filter_unsuitable(struct iter_env * iter_env,struct module_env * env,uint8_t * name,size_t namelen,uint16_t qtype,time_t now,struct delegpt_addr * a)2583b6c3722Schristos iter_filter_unsuitable(struct iter_env* iter_env, struct module_env* env,
2593b6c3722Schristos uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
2603b6c3722Schristos struct delegpt_addr* a)
2613b6c3722Schristos {
2623b6c3722Schristos int rtt, lame, reclame, dnsseclame;
2633b6c3722Schristos if(a->bogus)
2643b6c3722Schristos return -1; /* address of server is bogus */
2653b6c3722Schristos if(donotq_lookup(iter_env->donotq, &a->addr, a->addrlen)) {
2663b6c3722Schristos log_addr(VERB_ALGO, "skip addr on the donotquery list",
2673b6c3722Schristos &a->addr, a->addrlen);
2683b6c3722Schristos return -1; /* server is on the donotquery list */
2693b6c3722Schristos }
2703b6c3722Schristos if(!iter_env->supports_ipv6 && addr_is_ip6(&a->addr, a->addrlen)) {
2713b6c3722Schristos return -1; /* there is no ip6 available */
2723b6c3722Schristos }
273*91f7d55fSchristos if(!iter_env->supports_ipv4 && !iter_env->use_nat64 &&
274*91f7d55fSchristos !addr_is_ip6(&a->addr, a->addrlen)) {
2753b6c3722Schristos return -1; /* there is no ip4 available */
2763b6c3722Schristos }
2773b6c3722Schristos /* check lameness - need zone , class info */
2783b6c3722Schristos if(infra_get_lame_rtt(env->infra_cache, &a->addr, a->addrlen,
2793b6c3722Schristos name, namelen, qtype, &lame, &dnsseclame, &reclame,
2803b6c3722Schristos &rtt, now)) {
2813b6c3722Schristos log_addr(VERB_ALGO, "servselect", &a->addr, a->addrlen);
2823b6c3722Schristos verbose(VERB_ALGO, " rtt=%d%s%s%s%s", rtt,
2833b6c3722Schristos lame?" LAME":"",
2843b6c3722Schristos dnsseclame?" DNSSEC_LAME":"",
2853b6c3722Schristos reclame?" REC_LAME":"",
2863b6c3722Schristos a->lame?" ADDR_LAME":"");
2873b6c3722Schristos if(lame)
2883b6c3722Schristos return -1; /* server is lame */
2893b6c3722Schristos else if(rtt >= USEFUL_SERVER_TOP_TIMEOUT)
2903b6c3722Schristos /* server is unresponsive,
2913b6c3722Schristos * we used to return TOP_TIMEOUT, but fairly useless,
2923b6c3722Schristos * because if == TOP_TIMEOUT is dropped because
2933b6c3722Schristos * blacklisted later, instead, remove it here, so
2943b6c3722Schristos * other choices (that are not blacklisted) can be
2953b6c3722Schristos * tried */
2963b6c3722Schristos return -1;
2973b6c3722Schristos /* select remainder from worst to best */
2983b6c3722Schristos else if(reclame)
2993b6c3722Schristos return rtt+USEFUL_SERVER_TOP_TIMEOUT*3; /* nonpref */
3003b6c3722Schristos else if(dnsseclame || a->dnsseclame)
3013b6c3722Schristos return rtt+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
3023b6c3722Schristos else if(a->lame)
3033b6c3722Schristos return rtt+USEFUL_SERVER_TOP_TIMEOUT+1; /* nonpref */
3043b6c3722Schristos else return rtt;
3053b6c3722Schristos }
3063b6c3722Schristos /* no server information present */
3073b6c3722Schristos if(a->dnsseclame)
3083b6c3722Schristos return UNKNOWN_SERVER_NICENESS+USEFUL_SERVER_TOP_TIMEOUT*2; /* nonpref */
3093b6c3722Schristos else if(a->lame)
3103b6c3722Schristos return USEFUL_SERVER_TOP_TIMEOUT+1+UNKNOWN_SERVER_NICENESS; /* nonpref */
3113b6c3722Schristos return UNKNOWN_SERVER_NICENESS;
3123b6c3722Schristos }
3133b6c3722Schristos
3143b6c3722Schristos /** lookup RTT information, and also store fastest rtt (if any) */
3153b6c3722Schristos static int
iter_fill_rtt(struct iter_env * iter_env,struct module_env * env,uint8_t * name,size_t namelen,uint16_t qtype,time_t now,struct delegpt * dp,int * best_rtt,struct sock_list * blacklist,size_t * num_suitable_results)3163b6c3722Schristos iter_fill_rtt(struct iter_env* iter_env, struct module_env* env,
3173b6c3722Schristos uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
318f42d8de7Schristos struct delegpt* dp, int* best_rtt, struct sock_list* blacklist,
319f42d8de7Schristos size_t* num_suitable_results)
3203b6c3722Schristos {
3213b6c3722Schristos int got_it = 0;
3223b6c3722Schristos struct delegpt_addr* a;
323f42d8de7Schristos *num_suitable_results = 0;
324f42d8de7Schristos
3253b6c3722Schristos if(dp->bogus)
3263b6c3722Schristos return 0; /* NS bogus, all bogus, nothing found */
3273b6c3722Schristos for(a=dp->result_list; a; a = a->next_result) {
3283b6c3722Schristos a->sel_rtt = iter_filter_unsuitable(iter_env, env,
3293b6c3722Schristos name, namelen, qtype, now, a);
3303b6c3722Schristos if(a->sel_rtt != -1) {
3313b6c3722Schristos if(sock_list_find(blacklist, &a->addr, a->addrlen))
3323b6c3722Schristos a->sel_rtt += BLACKLIST_PENALTY;
3333b6c3722Schristos
3343b6c3722Schristos if(!got_it) {
3353b6c3722Schristos *best_rtt = a->sel_rtt;
3363b6c3722Schristos got_it = 1;
3373b6c3722Schristos } else if(a->sel_rtt < *best_rtt) {
3383b6c3722Schristos *best_rtt = a->sel_rtt;
3393b6c3722Schristos }
340f42d8de7Schristos (*num_suitable_results)++;
3413b6c3722Schristos }
3423b6c3722Schristos }
3433b6c3722Schristos return got_it;
3443b6c3722Schristos }
3453b6c3722Schristos
346f42d8de7Schristos /** compare two rtts, return -1, 0 or 1 */
347f42d8de7Schristos static int
rtt_compare(const void * x,const void * y)348f42d8de7Schristos rtt_compare(const void* x, const void* y)
349f42d8de7Schristos {
350f42d8de7Schristos if(*(int*)x == *(int*)y)
351f42d8de7Schristos return 0;
352f42d8de7Schristos if(*(int*)x > *(int*)y)
353f42d8de7Schristos return 1;
354f42d8de7Schristos return -1;
355f42d8de7Schristos }
356f42d8de7Schristos
357f42d8de7Schristos /** get RTT for the Nth fastest server */
358f42d8de7Schristos static int
nth_rtt(struct delegpt_addr * result_list,size_t num_results,size_t n)359f42d8de7Schristos nth_rtt(struct delegpt_addr* result_list, size_t num_results, size_t n)
360f42d8de7Schristos {
361f42d8de7Schristos int rtt_band;
362f42d8de7Schristos size_t i;
363f42d8de7Schristos int* rtt_list, *rtt_index;
364f42d8de7Schristos
365f42d8de7Schristos if(num_results < 1 || n >= num_results) {
366f42d8de7Schristos return -1;
367f42d8de7Schristos }
368f42d8de7Schristos
369f42d8de7Schristos rtt_list = calloc(num_results, sizeof(int));
370f42d8de7Schristos if(!rtt_list) {
371f42d8de7Schristos log_err("malloc failure: allocating rtt_list");
372f42d8de7Schristos return -1;
373f42d8de7Schristos }
374f42d8de7Schristos rtt_index = rtt_list;
375f42d8de7Schristos
376f42d8de7Schristos for(i=0; i<num_results && result_list; i++) {
377f42d8de7Schristos if(result_list->sel_rtt != -1) {
378f42d8de7Schristos *rtt_index = result_list->sel_rtt;
379f42d8de7Schristos rtt_index++;
380f42d8de7Schristos }
381f42d8de7Schristos result_list=result_list->next_result;
382f42d8de7Schristos }
383f42d8de7Schristos qsort(rtt_list, num_results, sizeof(*rtt_list), rtt_compare);
384f42d8de7Schristos
385f42d8de7Schristos log_assert(n > 0);
386f42d8de7Schristos rtt_band = rtt_list[n-1];
387f42d8de7Schristos free(rtt_list);
388f42d8de7Schristos
389f42d8de7Schristos return rtt_band;
390f42d8de7Schristos }
391f42d8de7Schristos
3923b6c3722Schristos /** filter the address list, putting best targets at front,
3933b6c3722Schristos * returns number of best targets (or 0, no suitable targets) */
3943b6c3722Schristos static int
iter_filter_order(struct iter_env * iter_env,struct module_env * env,uint8_t * name,size_t namelen,uint16_t qtype,time_t now,struct delegpt * dp,int * selected_rtt,int open_target,struct sock_list * blacklist,time_t prefetch)3953b6c3722Schristos iter_filter_order(struct iter_env* iter_env, struct module_env* env,
3963b6c3722Schristos uint8_t* name, size_t namelen, uint16_t qtype, time_t now,
3973b6c3722Schristos struct delegpt* dp, int* selected_rtt, int open_target,
3987cd94d69Schristos struct sock_list* blacklist, time_t prefetch)
3993b6c3722Schristos {
400f42d8de7Schristos int got_num = 0, low_rtt = 0, swap_to_front, rtt_band = RTT_BAND, nth;
4017a540f2bSchristos int alllame = 0;
402f42d8de7Schristos size_t num_results;
4033b6c3722Schristos struct delegpt_addr* a, *n, *prev=NULL;
4043b6c3722Schristos
4053b6c3722Schristos /* fillup sel_rtt and find best rtt in the bunch */
4063b6c3722Schristos got_num = iter_fill_rtt(iter_env, env, name, namelen, qtype, now, dp,
407f42d8de7Schristos &low_rtt, blacklist, &num_results);
4083b6c3722Schristos if(got_num == 0)
4093b6c3722Schristos return 0;
4103b6c3722Schristos if(low_rtt >= USEFUL_SERVER_TOP_TIMEOUT &&
4117a540f2bSchristos /* If all missing (or not fully resolved) targets are lame,
4127a540f2bSchristos * then use the remaining lame address. */
4137a540f2bSchristos ((delegpt_count_missing_targets(dp, &alllame) > 0 && !alllame) ||
4147a540f2bSchristos open_target > 0)) {
4153b6c3722Schristos verbose(VERB_ALGO, "Bad choices, trying to get more choice");
4163b6c3722Schristos return 0; /* we want more choice. The best choice is a bad one.
4173b6c3722Schristos return 0 to force the caller to fetch more */
4183b6c3722Schristos }
4193b6c3722Schristos
420f42d8de7Schristos if(env->cfg->fast_server_permil != 0 && prefetch == 0 &&
421f42d8de7Schristos num_results > env->cfg->fast_server_num &&
422f42d8de7Schristos ub_random_max(env->rnd, 1000) < env->cfg->fast_server_permil) {
4237cd94d69Schristos /* the query is not prefetch, but for a downstream client,
424f42d8de7Schristos * there are more servers available then the fastest N we want
425f42d8de7Schristos * to choose from. Limit our choice to the fastest servers. */
426f42d8de7Schristos nth = nth_rtt(dp->result_list, num_results,
427f42d8de7Schristos env->cfg->fast_server_num);
428f42d8de7Schristos if(nth > 0) {
429f42d8de7Schristos rtt_band = nth - low_rtt;
430f42d8de7Schristos if(rtt_band > RTT_BAND)
431f42d8de7Schristos rtt_band = RTT_BAND;
432f42d8de7Schristos }
4337cd94d69Schristos }
4347cd94d69Schristos
4353b6c3722Schristos got_num = 0;
4363b6c3722Schristos a = dp->result_list;
4373b6c3722Schristos while(a) {
4383b6c3722Schristos /* skip unsuitable targets */
4393b6c3722Schristos if(a->sel_rtt == -1) {
4403b6c3722Schristos prev = a;
4413b6c3722Schristos a = a->next_result;
4423b6c3722Schristos continue;
4433b6c3722Schristos }
4443b6c3722Schristos /* classify the server address and determine what to do */
4453b6c3722Schristos swap_to_front = 0;
4467cd94d69Schristos if(a->sel_rtt >= low_rtt && a->sel_rtt - low_rtt <= rtt_band) {
4473b6c3722Schristos got_num++;
4483b6c3722Schristos swap_to_front = 1;
4497cd94d69Schristos } else if(a->sel_rtt<low_rtt && low_rtt-a->sel_rtt<=rtt_band) {
4503b6c3722Schristos got_num++;
4513b6c3722Schristos swap_to_front = 1;
4523b6c3722Schristos }
4533b6c3722Schristos /* swap to front if necessary, or move to next result */
4543b6c3722Schristos if(swap_to_front && prev) {
4553b6c3722Schristos n = a->next_result;
4563b6c3722Schristos prev->next_result = n;
4573b6c3722Schristos a->next_result = dp->result_list;
4583b6c3722Schristos dp->result_list = a;
4593b6c3722Schristos a = n;
4603b6c3722Schristos } else {
4613b6c3722Schristos prev = a;
4623b6c3722Schristos a = a->next_result;
4633b6c3722Schristos }
4643b6c3722Schristos }
4653b6c3722Schristos *selected_rtt = low_rtt;
4660cd9f4ecSchristos
4670cd9f4ecSchristos if (env->cfg->prefer_ip6) {
4680cd9f4ecSchristos int got_num6 = 0;
4690cd9f4ecSchristos int low_rtt6 = 0;
4700cd9f4ecSchristos int i;
471f42d8de7Schristos int attempt = -1; /* filter to make sure addresses have
472f42d8de7Schristos less attempts on them than the first, to force round
473f42d8de7Schristos robin when all the IPv6 addresses fail */
474f42d8de7Schristos int num4ok = 0; /* number ip4 at low attempt count */
475f42d8de7Schristos int num4_lowrtt = 0;
4760cd9f4ecSchristos prev = NULL;
4770cd9f4ecSchristos a = dp->result_list;
4780cd9f4ecSchristos for(i = 0; i < got_num; i++) {
4797a540f2bSchristos if(!a) break; /* robustness */
4800cd9f4ecSchristos swap_to_front = 0;
481f42d8de7Schristos if(a->addr.ss_family != AF_INET6 && attempt == -1) {
482f42d8de7Schristos /* if we only have ip4 at low attempt count,
483f42d8de7Schristos * then ip6 is failing, and we need to
484f42d8de7Schristos * select one of the remaining IPv4 addrs */
485f42d8de7Schristos attempt = a->attempts;
486f42d8de7Schristos num4ok++;
487f42d8de7Schristos num4_lowrtt = a->sel_rtt;
488f42d8de7Schristos } else if(a->addr.ss_family != AF_INET6 && attempt == a->attempts) {
489f42d8de7Schristos num4ok++;
490f42d8de7Schristos if(num4_lowrtt == 0 || a->sel_rtt < num4_lowrtt) {
491f42d8de7Schristos num4_lowrtt = a->sel_rtt;
492f42d8de7Schristos }
493f42d8de7Schristos }
4940cd9f4ecSchristos if(a->addr.ss_family == AF_INET6) {
495f42d8de7Schristos if(attempt == -1) {
496f42d8de7Schristos attempt = a->attempts;
497f42d8de7Schristos } else if(a->attempts > attempt) {
498f42d8de7Schristos break;
499f42d8de7Schristos }
5000cd9f4ecSchristos got_num6++;
5010cd9f4ecSchristos swap_to_front = 1;
5020cd9f4ecSchristos if(low_rtt6 == 0 || a->sel_rtt < low_rtt6) {
5030cd9f4ecSchristos low_rtt6 = a->sel_rtt;
5040cd9f4ecSchristos }
5050cd9f4ecSchristos }
5060cd9f4ecSchristos /* swap to front if IPv6, or move to next result */
5070cd9f4ecSchristos if(swap_to_front && prev) {
5080cd9f4ecSchristos n = a->next_result;
5090cd9f4ecSchristos prev->next_result = n;
5100cd9f4ecSchristos a->next_result = dp->result_list;
5110cd9f4ecSchristos dp->result_list = a;
5120cd9f4ecSchristos a = n;
5130cd9f4ecSchristos } else {
5140cd9f4ecSchristos prev = a;
5150cd9f4ecSchristos a = a->next_result;
5160cd9f4ecSchristos }
5170cd9f4ecSchristos }
5180cd9f4ecSchristos if(got_num6 > 0) {
5190cd9f4ecSchristos got_num = got_num6;
5200cd9f4ecSchristos *selected_rtt = low_rtt6;
521f42d8de7Schristos } else if(num4ok > 0) {
522f42d8de7Schristos got_num = num4ok;
523f42d8de7Schristos *selected_rtt = num4_lowrtt;
5240cd9f4ecSchristos }
525d0eba39bSchristos } else if (env->cfg->prefer_ip4) {
526d0eba39bSchristos int got_num4 = 0;
527d0eba39bSchristos int low_rtt4 = 0;
528d0eba39bSchristos int i;
529d0eba39bSchristos int attempt = -1; /* filter to make sure addresses have
530d0eba39bSchristos less attempts on them than the first, to force round
531d0eba39bSchristos robin when all the IPv4 addresses fail */
532d0eba39bSchristos int num6ok = 0; /* number ip6 at low attempt count */
533d0eba39bSchristos int num6_lowrtt = 0;
534d0eba39bSchristos prev = NULL;
535d0eba39bSchristos a = dp->result_list;
536d0eba39bSchristos for(i = 0; i < got_num; i++) {
5377a540f2bSchristos if(!a) break; /* robustness */
538d0eba39bSchristos swap_to_front = 0;
539d0eba39bSchristos if(a->addr.ss_family != AF_INET && attempt == -1) {
540d0eba39bSchristos /* if we only have ip6 at low attempt count,
541d0eba39bSchristos * then ip4 is failing, and we need to
542d0eba39bSchristos * select one of the remaining IPv6 addrs */
543d0eba39bSchristos attempt = a->attempts;
544d0eba39bSchristos num6ok++;
545d0eba39bSchristos num6_lowrtt = a->sel_rtt;
546d0eba39bSchristos } else if(a->addr.ss_family != AF_INET && attempt == a->attempts) {
547d0eba39bSchristos num6ok++;
548d0eba39bSchristos if(num6_lowrtt == 0 || a->sel_rtt < num6_lowrtt) {
549d0eba39bSchristos num6_lowrtt = a->sel_rtt;
550d0eba39bSchristos }
551d0eba39bSchristos }
552d0eba39bSchristos if(a->addr.ss_family == AF_INET) {
553d0eba39bSchristos if(attempt == -1) {
554d0eba39bSchristos attempt = a->attempts;
555d0eba39bSchristos } else if(a->attempts > attempt) {
556d0eba39bSchristos break;
557d0eba39bSchristos }
558d0eba39bSchristos got_num4++;
559d0eba39bSchristos swap_to_front = 1;
560d0eba39bSchristos if(low_rtt4 == 0 || a->sel_rtt < low_rtt4) {
561d0eba39bSchristos low_rtt4 = a->sel_rtt;
562d0eba39bSchristos }
563d0eba39bSchristos }
564d0eba39bSchristos /* swap to front if IPv4, or move to next result */
565d0eba39bSchristos if(swap_to_front && prev) {
566d0eba39bSchristos n = a->next_result;
567d0eba39bSchristos prev->next_result = n;
568d0eba39bSchristos a->next_result = dp->result_list;
569d0eba39bSchristos dp->result_list = a;
570d0eba39bSchristos a = n;
571d0eba39bSchristos } else {
572d0eba39bSchristos prev = a;
573d0eba39bSchristos a = a->next_result;
574d0eba39bSchristos }
575d0eba39bSchristos }
576d0eba39bSchristos if(got_num4 > 0) {
577d0eba39bSchristos got_num = got_num4;
578d0eba39bSchristos *selected_rtt = low_rtt4;
579d0eba39bSchristos } else if(num6ok > 0) {
580d0eba39bSchristos got_num = num6ok;
581d0eba39bSchristos *selected_rtt = num6_lowrtt;
582d0eba39bSchristos }
5830cd9f4ecSchristos }
5843b6c3722Schristos return got_num;
5853b6c3722Schristos }
5863b6c3722Schristos
5873b6c3722Schristos struct delegpt_addr*
iter_server_selection(struct iter_env * iter_env,struct module_env * env,struct delegpt * dp,uint8_t * name,size_t namelen,uint16_t qtype,int * dnssec_lame,int * chase_to_rd,int open_target,struct sock_list * blacklist,time_t prefetch)5883b6c3722Schristos iter_server_selection(struct iter_env* iter_env,
5893b6c3722Schristos struct module_env* env, struct delegpt* dp,
5903b6c3722Schristos uint8_t* name, size_t namelen, uint16_t qtype, int* dnssec_lame,
5917cd94d69Schristos int* chase_to_rd, int open_target, struct sock_list* blacklist,
5927cd94d69Schristos time_t prefetch)
5933b6c3722Schristos {
5943b6c3722Schristos int sel;
5953b6c3722Schristos int selrtt;
5963b6c3722Schristos struct delegpt_addr* a, *prev;
5973b6c3722Schristos int num = iter_filter_order(iter_env, env, name, namelen, qtype,
5987cd94d69Schristos *env->now, dp, &selrtt, open_target, blacklist, prefetch);
5993b6c3722Schristos
6003b6c3722Schristos if(num == 0)
6013b6c3722Schristos return NULL;
6023b6c3722Schristos verbose(VERB_ALGO, "selrtt %d", selrtt);
6033b6c3722Schristos if(selrtt > BLACKLIST_PENALTY) {
6043b6c3722Schristos if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*3) {
6053b6c3722Schristos verbose(VERB_ALGO, "chase to "
6063b6c3722Schristos "blacklisted recursion lame server");
6073b6c3722Schristos *chase_to_rd = 1;
6083b6c3722Schristos }
6093b6c3722Schristos if(selrtt-BLACKLIST_PENALTY > USEFUL_SERVER_TOP_TIMEOUT*2) {
6103b6c3722Schristos verbose(VERB_ALGO, "chase to "
6113b6c3722Schristos "blacklisted dnssec lame server");
6123b6c3722Schristos *dnssec_lame = 1;
6133b6c3722Schristos }
6143b6c3722Schristos } else {
6153b6c3722Schristos if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*3) {
6163b6c3722Schristos verbose(VERB_ALGO, "chase to recursion lame server");
6173b6c3722Schristos *chase_to_rd = 1;
6183b6c3722Schristos }
6193b6c3722Schristos if(selrtt > USEFUL_SERVER_TOP_TIMEOUT*2) {
6203b6c3722Schristos verbose(VERB_ALGO, "chase to dnssec lame server");
6213b6c3722Schristos *dnssec_lame = 1;
6223b6c3722Schristos }
6233b6c3722Schristos if(selrtt == USEFUL_SERVER_TOP_TIMEOUT) {
6243b6c3722Schristos verbose(VERB_ALGO, "chase to blacklisted lame server");
6253b6c3722Schristos return NULL;
6263b6c3722Schristos }
6273b6c3722Schristos }
6283b6c3722Schristos
6293b6c3722Schristos if(num == 1) {
6303b6c3722Schristos a = dp->result_list;
6317a540f2bSchristos if(++a->attempts < iter_env->outbound_msg_retry)
6323b6c3722Schristos return a;
6333b6c3722Schristos dp->result_list = a->next_result;
6343b6c3722Schristos return a;
6353b6c3722Schristos }
6363b6c3722Schristos
6373b6c3722Schristos /* randomly select a target from the list */
6383b6c3722Schristos log_assert(num > 1);
6393b6c3722Schristos /* grab secure random number, to pick unexpected server.
6403b6c3722Schristos * also we need it to be threadsafe. */
6413b6c3722Schristos sel = ub_random_max(env->rnd, num);
6423b6c3722Schristos a = dp->result_list;
6433b6c3722Schristos prev = NULL;
6443b6c3722Schristos while(sel > 0 && a) {
6453b6c3722Schristos prev = a;
6463b6c3722Schristos a = a->next_result;
6473b6c3722Schristos sel--;
6483b6c3722Schristos }
6493b6c3722Schristos if(!a) /* robustness */
6503b6c3722Schristos return NULL;
6517a540f2bSchristos if(++a->attempts < iter_env->outbound_msg_retry)
6523b6c3722Schristos return a;
6533b6c3722Schristos /* remove it from the delegation point result list */
6543b6c3722Schristos if(prev)
6553b6c3722Schristos prev->next_result = a->next_result;
6563b6c3722Schristos else dp->result_list = a->next_result;
6573b6c3722Schristos return a;
6583b6c3722Schristos }
6593b6c3722Schristos
6603b6c3722Schristos struct dns_msg*
dns_alloc_msg(sldns_buffer * pkt,struct msg_parse * msg,struct regional * region)6613b6c3722Schristos dns_alloc_msg(sldns_buffer* pkt, struct msg_parse* msg,
6623b6c3722Schristos struct regional* region)
6633b6c3722Schristos {
6643b6c3722Schristos struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
6653b6c3722Schristos sizeof(struct dns_msg));
6663b6c3722Schristos if(!m)
6673b6c3722Schristos return NULL;
6683b6c3722Schristos memset(m, 0, sizeof(*m));
6693b6c3722Schristos if(!parse_create_msg(pkt, msg, NULL, &m->qinfo, &m->rep, region)) {
6703b6c3722Schristos log_err("malloc failure: allocating incoming dns_msg");
6713b6c3722Schristos return NULL;
6723b6c3722Schristos }
6733b6c3722Schristos return m;
6743b6c3722Schristos }
6753b6c3722Schristos
6763b6c3722Schristos struct dns_msg*
dns_copy_msg(struct dns_msg * from,struct regional * region)6773b6c3722Schristos dns_copy_msg(struct dns_msg* from, struct regional* region)
6783b6c3722Schristos {
6793b6c3722Schristos struct dns_msg* m = (struct dns_msg*)regional_alloc(region,
6803b6c3722Schristos sizeof(struct dns_msg));
6813b6c3722Schristos if(!m)
6823b6c3722Schristos return NULL;
6833b6c3722Schristos m->qinfo = from->qinfo;
6843b6c3722Schristos if(!(m->qinfo.qname = regional_alloc_init(region, from->qinfo.qname,
6853b6c3722Schristos from->qinfo.qname_len)))
6863b6c3722Schristos return NULL;
6873b6c3722Schristos if(!(m->rep = reply_info_copy(from->rep, NULL, region)))
6883b6c3722Schristos return NULL;
6893b6c3722Schristos return m;
6903b6c3722Schristos }
6913b6c3722Schristos
6923b6c3722Schristos void
iter_dns_store(struct module_env * env,struct query_info * msgqinf,struct reply_info * msgrep,int is_referral,time_t leeway,int pside,struct regional * region,uint16_t flags,time_t qstarttime)6933b6c3722Schristos iter_dns_store(struct module_env* env, struct query_info* msgqinf,
6943b6c3722Schristos struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
6957a540f2bSchristos struct regional* region, uint16_t flags, time_t qstarttime)
6963b6c3722Schristos {
6973b6c3722Schristos if(!dns_cache_store(env, msgqinf, msgrep, is_referral, leeway,
6987a540f2bSchristos pside, region, flags, qstarttime))
6993b6c3722Schristos log_err("out of memory: cannot store data in cache");
7003b6c3722Schristos }
7013b6c3722Schristos
7023b6c3722Schristos int
iter_ns_probability(struct ub_randstate * rnd,int n,int m)7033b6c3722Schristos iter_ns_probability(struct ub_randstate* rnd, int n, int m)
7043b6c3722Schristos {
7053b6c3722Schristos int sel;
7063b6c3722Schristos if(n == m) /* 100% chance */
7073b6c3722Schristos return 1;
7083b6c3722Schristos /* we do not need secure random numbers here, but
7093b6c3722Schristos * we do need it to be threadsafe, so we use this */
7103b6c3722Schristos sel = ub_random_max(rnd, m);
7113b6c3722Schristos return (sel < n);
7123b6c3722Schristos }
7133b6c3722Schristos
7143b6c3722Schristos /** detect dependency cycle for query and target */
7153b6c3722Schristos static int
causes_cycle(struct module_qstate * qstate,uint8_t * name,size_t namelen,uint16_t t,uint16_t c)7163b6c3722Schristos causes_cycle(struct module_qstate* qstate, uint8_t* name, size_t namelen,
7173b6c3722Schristos uint16_t t, uint16_t c)
7183b6c3722Schristos {
7193b6c3722Schristos struct query_info qinf;
7203b6c3722Schristos qinf.qname = name;
7213b6c3722Schristos qinf.qname_len = namelen;
7223b6c3722Schristos qinf.qtype = t;
7233b6c3722Schristos qinf.qclass = c;
7240cd9f4ecSchristos qinf.local_alias = NULL;
7253b6c3722Schristos fptr_ok(fptr_whitelist_modenv_detect_cycle(
7263b6c3722Schristos qstate->env->detect_cycle));
7273b6c3722Schristos return (*qstate->env->detect_cycle)(qstate, &qinf,
7283b6c3722Schristos (uint16_t)(BIT_RD|BIT_CD), qstate->is_priming,
7293b6c3722Schristos qstate->is_valrec);
7303b6c3722Schristos }
7313b6c3722Schristos
7323b6c3722Schristos void
iter_mark_cycle_targets(struct module_qstate * qstate,struct delegpt * dp)7333b6c3722Schristos iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
7343b6c3722Schristos {
7353b6c3722Schristos struct delegpt_ns* ns;
7363b6c3722Schristos for(ns = dp->nslist; ns; ns = ns->next) {
7373b6c3722Schristos if(ns->resolved)
7383b6c3722Schristos continue;
7393b6c3722Schristos /* see if this ns as target causes dependency cycle */
7403b6c3722Schristos if(causes_cycle(qstate, ns->name, ns->namelen,
7413b6c3722Schristos LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass) ||
7423b6c3722Schristos causes_cycle(qstate, ns->name, ns->namelen,
7433b6c3722Schristos LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
7443b6c3722Schristos log_nametypeclass(VERB_QUERY, "skipping target due "
7453b6c3722Schristos "to dependency cycle (harden-glue: no may "
7463b6c3722Schristos "fix some of the cycles)",
7473b6c3722Schristos ns->name, LDNS_RR_TYPE_A,
7483b6c3722Schristos qstate->qinfo.qclass);
7493b6c3722Schristos ns->resolved = 1;
7503b6c3722Schristos }
7513b6c3722Schristos }
7523b6c3722Schristos }
7533b6c3722Schristos
7543b6c3722Schristos void
iter_mark_pside_cycle_targets(struct module_qstate * qstate,struct delegpt * dp)7553b6c3722Schristos iter_mark_pside_cycle_targets(struct module_qstate* qstate, struct delegpt* dp)
7563b6c3722Schristos {
7573b6c3722Schristos struct delegpt_ns* ns;
7583b6c3722Schristos for(ns = dp->nslist; ns; ns = ns->next) {
7593b6c3722Schristos if(ns->done_pside4 && ns->done_pside6)
7603b6c3722Schristos continue;
7613b6c3722Schristos /* see if this ns as target causes dependency cycle */
7623b6c3722Schristos if(causes_cycle(qstate, ns->name, ns->namelen,
7633b6c3722Schristos LDNS_RR_TYPE_A, qstate->qinfo.qclass)) {
7643b6c3722Schristos log_nametypeclass(VERB_QUERY, "skipping target due "
7653b6c3722Schristos "to dependency cycle", ns->name,
7663b6c3722Schristos LDNS_RR_TYPE_A, qstate->qinfo.qclass);
7673b6c3722Schristos ns->done_pside4 = 1;
7683b6c3722Schristos }
7693b6c3722Schristos if(causes_cycle(qstate, ns->name, ns->namelen,
7703b6c3722Schristos LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass)) {
7713b6c3722Schristos log_nametypeclass(VERB_QUERY, "skipping target due "
7723b6c3722Schristos "to dependency cycle", ns->name,
7733b6c3722Schristos LDNS_RR_TYPE_AAAA, qstate->qinfo.qclass);
7743b6c3722Schristos ns->done_pside6 = 1;
7753b6c3722Schristos }
7763b6c3722Schristos }
7773b6c3722Schristos }
7783b6c3722Schristos
7793b6c3722Schristos int
iter_dp_is_useless(struct query_info * qinfo,uint16_t qflags,struct delegpt * dp,int supports_ipv4,int supports_ipv6,int use_nat64)7803b6c3722Schristos iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
781*91f7d55fSchristos struct delegpt* dp, int supports_ipv4, int supports_ipv6,
782*91f7d55fSchristos int use_nat64)
7833b6c3722Schristos {
7843b6c3722Schristos struct delegpt_ns* ns;
7857a540f2bSchristos struct delegpt_addr* a;
786*91f7d55fSchristos
787*91f7d55fSchristos if(supports_ipv6 && use_nat64)
788*91f7d55fSchristos supports_ipv4 = 1;
789*91f7d55fSchristos
7903b6c3722Schristos /* check:
7913b6c3722Schristos * o RD qflag is on.
7923b6c3722Schristos * o no addresses are provided.
7933b6c3722Schristos * o all NS items are required glue.
7943b6c3722Schristos * OR
7953b6c3722Schristos * o RD qflag is on.
7963b6c3722Schristos * o no addresses are provided.
7973b6c3722Schristos * o the query is for one of the nameservers in dp,
7983b6c3722Schristos * and that nameserver is a glue-name for this dp.
7993b6c3722Schristos */
8003b6c3722Schristos if(!(qflags&BIT_RD))
8013b6c3722Schristos return 0;
8027a540f2bSchristos /* either available or unused targets,
8037a540f2bSchristos * if they exist, the dp is not useless. */
8047a540f2bSchristos for(a = dp->usable_list; a; a = a->next_usable) {
8057a540f2bSchristos if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
8063b6c3722Schristos return 0;
8077a540f2bSchristos else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
8087a540f2bSchristos return 0;
8097a540f2bSchristos }
8107a540f2bSchristos for(a = dp->result_list; a; a = a->next_result) {
8117a540f2bSchristos if(!addr_is_ip6(&a->addr, a->addrlen) && supports_ipv4)
8127a540f2bSchristos return 0;
8137a540f2bSchristos else if(addr_is_ip6(&a->addr, a->addrlen) && supports_ipv6)
8147a540f2bSchristos return 0;
8157a540f2bSchristos }
8163b6c3722Schristos
8173b6c3722Schristos /* see if query is for one of the nameservers, which is glue */
8187a540f2bSchristos if( ((qinfo->qtype == LDNS_RR_TYPE_A && supports_ipv4) ||
8197a540f2bSchristos (qinfo->qtype == LDNS_RR_TYPE_AAAA && supports_ipv6)) &&
8203b6c3722Schristos dname_subdomain_c(qinfo->qname, dp->name) &&
8213b6c3722Schristos delegpt_find_ns(dp, qinfo->qname, qinfo->qname_len))
8223b6c3722Schristos return 1;
8233b6c3722Schristos
8243b6c3722Schristos for(ns = dp->nslist; ns; ns = ns->next) {
8253b6c3722Schristos if(ns->resolved) /* skip failed targets */
8263b6c3722Schristos continue;
8273b6c3722Schristos if(!dname_subdomain_c(ns->name, dp->name))
8283b6c3722Schristos return 0; /* one address is not required glue */
8293b6c3722Schristos }
8303b6c3722Schristos return 1;
8313b6c3722Schristos }
8323b6c3722Schristos
8333b6c3722Schristos int
iter_qname_indicates_dnssec(struct module_env * env,struct query_info * qinfo)8347cd94d69Schristos iter_qname_indicates_dnssec(struct module_env* env, struct query_info *qinfo)
8353b6c3722Schristos {
8363b6c3722Schristos struct trust_anchor* a;
8373b6c3722Schristos if(!env || !env->anchors || !qinfo || !qinfo->qname)
8383b6c3722Schristos return 0;
8393b6c3722Schristos /* a trust anchor exists above the name? */
8403b6c3722Schristos if((a=anchors_lookup(env->anchors, qinfo->qname, qinfo->qname_len,
8413b6c3722Schristos qinfo->qclass))) {
8423b6c3722Schristos if(a->numDS == 0 && a->numDNSKEY == 0) {
8433b6c3722Schristos /* insecure trust point */
8443b6c3722Schristos lock_basic_unlock(&a->lock);
8453b6c3722Schristos return 0;
8463b6c3722Schristos }
8473b6c3722Schristos lock_basic_unlock(&a->lock);
8483b6c3722Schristos return 1;
8493b6c3722Schristos }
8503b6c3722Schristos /* no trust anchor above it. */
8513b6c3722Schristos return 0;
8523b6c3722Schristos }
8533b6c3722Schristos
8543b6c3722Schristos int
iter_indicates_dnssec(struct module_env * env,struct delegpt * dp,struct dns_msg * msg,uint16_t dclass)8553b6c3722Schristos iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
8563b6c3722Schristos struct dns_msg* msg, uint16_t dclass)
8573b6c3722Schristos {
8583b6c3722Schristos struct trust_anchor* a;
8593b6c3722Schristos /* information not available, !env->anchors can be common */
8603b6c3722Schristos if(!env || !env->anchors || !dp || !dp->name)
8613b6c3722Schristos return 0;
8623b6c3722Schristos /* a trust anchor exists with this name, RRSIGs expected */
8633b6c3722Schristos if((a=anchor_find(env->anchors, dp->name, dp->namelabs, dp->namelen,
8643b6c3722Schristos dclass))) {
8657cd94d69Schristos if(a->numDS == 0 && a->numDNSKEY == 0) {
8667cd94d69Schristos /* insecure trust point */
8677cd94d69Schristos lock_basic_unlock(&a->lock);
8687cd94d69Schristos return 0;
8697cd94d69Schristos }
8703b6c3722Schristos lock_basic_unlock(&a->lock);
8713b6c3722Schristos return 1;
8723b6c3722Schristos }
8733b6c3722Schristos /* see if DS rrset was given, in AUTH section */
8743b6c3722Schristos if(msg && msg->rep &&
8753b6c3722Schristos reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
8763b6c3722Schristos LDNS_RR_TYPE_DS, dclass))
8773b6c3722Schristos return 1;
8783b6c3722Schristos /* look in key cache */
8793b6c3722Schristos if(env->key_cache) {
8803b6c3722Schristos struct key_entry_key* kk = key_cache_obtain(env->key_cache,
8813b6c3722Schristos dp->name, dp->namelen, dclass, env->scratch, *env->now);
8823b6c3722Schristos if(kk) {
8833b6c3722Schristos if(query_dname_compare(kk->name, dp->name) == 0) {
8843b6c3722Schristos if(key_entry_isgood(kk) || key_entry_isbad(kk)) {
8853b6c3722Schristos regional_free_all(env->scratch);
8863b6c3722Schristos return 1;
8873b6c3722Schristos } else if(key_entry_isnull(kk)) {
8883b6c3722Schristos regional_free_all(env->scratch);
8893b6c3722Schristos return 0;
8903b6c3722Schristos }
8913b6c3722Schristos }
8923b6c3722Schristos regional_free_all(env->scratch);
8933b6c3722Schristos }
8943b6c3722Schristos }
8953b6c3722Schristos return 0;
8963b6c3722Schristos }
8973b6c3722Schristos
8983b6c3722Schristos int
iter_msg_has_dnssec(struct dns_msg * msg)8993b6c3722Schristos iter_msg_has_dnssec(struct dns_msg* msg)
9003b6c3722Schristos {
9013b6c3722Schristos size_t i;
9023b6c3722Schristos if(!msg || !msg->rep)
9033b6c3722Schristos return 0;
9043b6c3722Schristos for(i=0; i<msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
9053b6c3722Schristos if(((struct packed_rrset_data*)msg->rep->rrsets[i]->
9063b6c3722Schristos entry.data)->rrsig_count > 0)
9073b6c3722Schristos return 1;
9083b6c3722Schristos }
9093b6c3722Schristos /* empty message has no DNSSEC info, with DNSSEC the reply is
9103b6c3722Schristos * not empty (NSEC) */
9113b6c3722Schristos return 0;
9123b6c3722Schristos }
9133b6c3722Schristos
iter_msg_from_zone(struct dns_msg * msg,struct delegpt * dp,enum response_type type,uint16_t dclass)9143b6c3722Schristos int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
9153b6c3722Schristos enum response_type type, uint16_t dclass)
9163b6c3722Schristos {
9173b6c3722Schristos if(!msg || !dp || !msg->rep || !dp->name)
9183b6c3722Schristos return 0;
9193b6c3722Schristos /* SOA RRset - always from reply zone */
9203b6c3722Schristos if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
9213b6c3722Schristos LDNS_RR_TYPE_SOA, dclass) ||
9223b6c3722Schristos reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
9233b6c3722Schristos LDNS_RR_TYPE_SOA, dclass))
9243b6c3722Schristos return 1;
9253b6c3722Schristos if(type == RESPONSE_TYPE_REFERRAL) {
9263b6c3722Schristos size_t i;
9273b6c3722Schristos /* if it adds a single label, i.e. we expect .com,
9283b6c3722Schristos * and referral to example.com. NS ... , then origin zone
9293b6c3722Schristos * is .com. For a referral to sub.example.com. NS ... then
9303b6c3722Schristos * we do not know, since example.com. may be in between. */
9313b6c3722Schristos for(i=0; i<msg->rep->an_numrrsets+msg->rep->ns_numrrsets;
9323b6c3722Schristos i++) {
9333b6c3722Schristos struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
9343b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS &&
9353b6c3722Schristos ntohs(s->rk.rrset_class) == dclass) {
9363b6c3722Schristos int l = dname_count_labels(s->rk.dname);
9373b6c3722Schristos if(l == dp->namelabs + 1 &&
9383b6c3722Schristos dname_strict_subdomain(s->rk.dname,
9393b6c3722Schristos l, dp->name, dp->namelabs))
9403b6c3722Schristos return 1;
9413b6c3722Schristos }
9423b6c3722Schristos }
9433b6c3722Schristos return 0;
9443b6c3722Schristos }
9453b6c3722Schristos log_assert(type==RESPONSE_TYPE_ANSWER || type==RESPONSE_TYPE_CNAME);
9463b6c3722Schristos /* not a referral, and not lame delegation (upwards), so,
9473b6c3722Schristos * any NS rrset must be from the zone itself */
9483b6c3722Schristos if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
9493b6c3722Schristos LDNS_RR_TYPE_NS, dclass) ||
9503b6c3722Schristos reply_find_rrset_section_ns(msg->rep, dp->name, dp->namelen,
9513b6c3722Schristos LDNS_RR_TYPE_NS, dclass))
9523b6c3722Schristos return 1;
9533b6c3722Schristos /* a DNSKEY set is expected at the zone apex as well */
9543b6c3722Schristos /* this is for 'minimal responses' for DNSKEYs */
9553b6c3722Schristos if(reply_find_rrset_section_an(msg->rep, dp->name, dp->namelen,
9563b6c3722Schristos LDNS_RR_TYPE_DNSKEY, dclass))
9573b6c3722Schristos return 1;
9583b6c3722Schristos return 0;
9593b6c3722Schristos }
9603b6c3722Schristos
9613b6c3722Schristos /**
9623b6c3722Schristos * check equality of two rrsets
9633b6c3722Schristos * @param k1: rrset
9643b6c3722Schristos * @param k2: rrset
9653b6c3722Schristos * @return true if equal
9663b6c3722Schristos */
9673b6c3722Schristos static int
rrset_equal(struct ub_packed_rrset_key * k1,struct ub_packed_rrset_key * k2)9683b6c3722Schristos rrset_equal(struct ub_packed_rrset_key* k1, struct ub_packed_rrset_key* k2)
9693b6c3722Schristos {
9703b6c3722Schristos struct packed_rrset_data* d1 = (struct packed_rrset_data*)
9713b6c3722Schristos k1->entry.data;
9723b6c3722Schristos struct packed_rrset_data* d2 = (struct packed_rrset_data*)
9733b6c3722Schristos k2->entry.data;
9743b6c3722Schristos size_t i, t;
9753b6c3722Schristos if(k1->rk.dname_len != k2->rk.dname_len ||
9763b6c3722Schristos k1->rk.flags != k2->rk.flags ||
9773b6c3722Schristos k1->rk.type != k2->rk.type ||
9783b6c3722Schristos k1->rk.rrset_class != k2->rk.rrset_class ||
9793b6c3722Schristos query_dname_compare(k1->rk.dname, k2->rk.dname) != 0)
9803b6c3722Schristos return 0;
9813b6c3722Schristos if( /* do not check ttl: d1->ttl != d2->ttl || */
9823b6c3722Schristos d1->count != d2->count ||
9833b6c3722Schristos d1->rrsig_count != d2->rrsig_count ||
9843b6c3722Schristos d1->trust != d2->trust ||
9853b6c3722Schristos d1->security != d2->security)
9863b6c3722Schristos return 0;
9873b6c3722Schristos t = d1->count + d1->rrsig_count;
9883b6c3722Schristos for(i=0; i<t; i++) {
9893b6c3722Schristos if(d1->rr_len[i] != d2->rr_len[i] ||
9903b6c3722Schristos /* no ttl check: d1->rr_ttl[i] != d2->rr_ttl[i] ||*/
9913b6c3722Schristos memcmp(d1->rr_data[i], d2->rr_data[i],
9923b6c3722Schristos d1->rr_len[i]) != 0)
9933b6c3722Schristos return 0;
9943b6c3722Schristos }
9953b6c3722Schristos return 1;
9963b6c3722Schristos }
9973b6c3722Schristos
998f42d8de7Schristos /** compare rrsets and sort canonically. Compares rrset name, type, class.
999f42d8de7Schristos * return 0 if equal, +1 if x > y, and -1 if x < y.
1000f42d8de7Schristos */
1001f42d8de7Schristos static int
rrset_canonical_sort_cmp(const void * x,const void * y)1002f42d8de7Schristos rrset_canonical_sort_cmp(const void* x, const void* y)
1003f42d8de7Schristos {
1004f42d8de7Schristos struct ub_packed_rrset_key* rrx = *(struct ub_packed_rrset_key**)x;
1005f42d8de7Schristos struct ub_packed_rrset_key* rry = *(struct ub_packed_rrset_key**)y;
1006f42d8de7Schristos int r = dname_canonical_compare(rrx->rk.dname, rry->rk.dname);
1007f42d8de7Schristos if(r != 0)
1008f42d8de7Schristos return r;
1009f42d8de7Schristos if(rrx->rk.type != rry->rk.type) {
1010f42d8de7Schristos if(ntohs(rrx->rk.type) > ntohs(rry->rk.type))
1011f42d8de7Schristos return 1;
1012f42d8de7Schristos else return -1;
1013f42d8de7Schristos }
1014f42d8de7Schristos if(rrx->rk.rrset_class != rry->rk.rrset_class) {
1015f42d8de7Schristos if(ntohs(rrx->rk.rrset_class) > ntohs(rry->rk.rrset_class))
1016f42d8de7Schristos return 1;
1017f42d8de7Schristos else return -1;
1018f42d8de7Schristos }
1019f42d8de7Schristos return 0;
1020f42d8de7Schristos }
1021f42d8de7Schristos
10223b6c3722Schristos int
reply_equal(struct reply_info * p,struct reply_info * q,struct regional * region)10233b6c3722Schristos reply_equal(struct reply_info* p, struct reply_info* q, struct regional* region)
10243b6c3722Schristos {
10253b6c3722Schristos size_t i;
1026f42d8de7Schristos struct ub_packed_rrset_key** sorted_p, **sorted_q;
10273b6c3722Schristos if(p->flags != q->flags ||
10283b6c3722Schristos p->qdcount != q->qdcount ||
10293b6c3722Schristos /* do not check TTL, this may differ */
10303b6c3722Schristos /*
10313b6c3722Schristos p->ttl != q->ttl ||
10323b6c3722Schristos p->prefetch_ttl != q->prefetch_ttl ||
10333b6c3722Schristos */
10343b6c3722Schristos p->security != q->security ||
10353b6c3722Schristos p->an_numrrsets != q->an_numrrsets ||
10363b6c3722Schristos p->ns_numrrsets != q->ns_numrrsets ||
10373b6c3722Schristos p->ar_numrrsets != q->ar_numrrsets ||
10383b6c3722Schristos p->rrset_count != q->rrset_count)
10393b6c3722Schristos return 0;
1040f42d8de7Schristos /* sort the rrsets in the authority and additional sections before
1041f42d8de7Schristos * compare, the query and answer sections are ordered in the sequence
1042f42d8de7Schristos * they should have (eg. one after the other for aliases). */
1043f42d8de7Schristos sorted_p = (struct ub_packed_rrset_key**)regional_alloc_init(
1044f42d8de7Schristos region, p->rrsets, sizeof(*sorted_p)*p->rrset_count);
1045f42d8de7Schristos if(!sorted_p) return 0;
1046f42d8de7Schristos log_assert(p->an_numrrsets + p->ns_numrrsets + p->ar_numrrsets <=
1047f42d8de7Schristos p->rrset_count);
1048f42d8de7Schristos qsort(sorted_p + p->an_numrrsets, p->ns_numrrsets,
1049f42d8de7Schristos sizeof(*sorted_p), rrset_canonical_sort_cmp);
1050f42d8de7Schristos qsort(sorted_p + p->an_numrrsets + p->ns_numrrsets, p->ar_numrrsets,
1051f42d8de7Schristos sizeof(*sorted_p), rrset_canonical_sort_cmp);
1052f42d8de7Schristos
1053f42d8de7Schristos sorted_q = (struct ub_packed_rrset_key**)regional_alloc_init(
1054f42d8de7Schristos region, q->rrsets, sizeof(*sorted_q)*q->rrset_count);
1055f42d8de7Schristos if(!sorted_q) {
10563b6c3722Schristos regional_free_all(region);
10573b6c3722Schristos return 0;
10583b6c3722Schristos }
1059f42d8de7Schristos log_assert(q->an_numrrsets + q->ns_numrrsets + q->ar_numrrsets <=
1060f42d8de7Schristos q->rrset_count);
1061f42d8de7Schristos qsort(sorted_q + q->an_numrrsets, q->ns_numrrsets,
1062f42d8de7Schristos sizeof(*sorted_q), rrset_canonical_sort_cmp);
1063f42d8de7Schristos qsort(sorted_q + q->an_numrrsets + q->ns_numrrsets, q->ar_numrrsets,
1064f42d8de7Schristos sizeof(*sorted_q), rrset_canonical_sort_cmp);
1065f42d8de7Schristos
1066f42d8de7Schristos /* compare the rrsets */
1067f42d8de7Schristos for(i=0; i<p->rrset_count; i++) {
1068f42d8de7Schristos if(!rrset_equal(sorted_p[i], sorted_q[i])) {
1069f42d8de7Schristos if(!rrset_canonical_equal(region, sorted_p[i],
1070f42d8de7Schristos sorted_q[i])) {
10713b6c3722Schristos regional_free_all(region);
1072f42d8de7Schristos return 0;
10733b6c3722Schristos }
10743b6c3722Schristos }
1075f42d8de7Schristos }
1076f42d8de7Schristos regional_free_all(region);
10773b6c3722Schristos return 1;
10783b6c3722Schristos }
10793b6c3722Schristos
10803b6c3722Schristos void
caps_strip_reply(struct reply_info * rep)10813b6c3722Schristos caps_strip_reply(struct reply_info* rep)
10823b6c3722Schristos {
10833b6c3722Schristos size_t i;
10843b6c3722Schristos if(!rep) return;
10853b6c3722Schristos /* see if message is a referral, in which case the additional and
10863b6c3722Schristos * NS record cannot be removed */
10873b6c3722Schristos /* referrals have the AA flag unset (strict check, not elsewhere in
10883b6c3722Schristos * unbound, but for 0x20 this is very convenient). */
10893b6c3722Schristos if(!(rep->flags&BIT_AA))
10903b6c3722Schristos return;
10913b6c3722Schristos /* remove the additional section from the reply */
10923b6c3722Schristos if(rep->ar_numrrsets != 0) {
10933b6c3722Schristos verbose(VERB_ALGO, "caps fallback: removing additional section");
10943b6c3722Schristos rep->rrset_count -= rep->ar_numrrsets;
10953b6c3722Schristos rep->ar_numrrsets = 0;
10963b6c3722Schristos }
10973b6c3722Schristos /* is there an NS set in the authority section to remove? */
10983b6c3722Schristos /* the failure case (Cisco firewalls) only has one rrset in authsec */
10993b6c3722Schristos for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
11003b6c3722Schristos struct ub_packed_rrset_key* s = rep->rrsets[i];
11013b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_NS) {
11023b6c3722Schristos /* remove NS rrset and break from loop (loop limits
11033b6c3722Schristos * have changed) */
11043b6c3722Schristos /* move last rrset into this position (there is no
11053b6c3722Schristos * additional section any more) */
11063b6c3722Schristos verbose(VERB_ALGO, "caps fallback: removing NS rrset");
11073b6c3722Schristos if(i < rep->rrset_count-1)
11083b6c3722Schristos rep->rrsets[i]=rep->rrsets[rep->rrset_count-1];
11093b6c3722Schristos rep->rrset_count --;
11103b6c3722Schristos rep->ns_numrrsets --;
11113b6c3722Schristos break;
11123b6c3722Schristos }
11133b6c3722Schristos }
11143b6c3722Schristos }
11153b6c3722Schristos
caps_failed_rcode(struct reply_info * rep)11163b6c3722Schristos int caps_failed_rcode(struct reply_info* rep)
11173b6c3722Schristos {
11183b6c3722Schristos return !(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
11193b6c3722Schristos FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN);
11203b6c3722Schristos }
11213b6c3722Schristos
11223b6c3722Schristos void
iter_store_parentside_rrset(struct module_env * env,struct ub_packed_rrset_key * rrset)11233b6c3722Schristos iter_store_parentside_rrset(struct module_env* env,
11243b6c3722Schristos struct ub_packed_rrset_key* rrset)
11253b6c3722Schristos {
11263b6c3722Schristos struct rrset_ref ref;
11273b6c3722Schristos rrset = packed_rrset_copy_alloc(rrset, env->alloc, *env->now);
11283b6c3722Schristos if(!rrset) {
11293b6c3722Schristos log_err("malloc failure in store_parentside_rrset");
11303b6c3722Schristos return;
11313b6c3722Schristos }
11323b6c3722Schristos rrset->rk.flags |= PACKED_RRSET_PARENT_SIDE;
11333b6c3722Schristos rrset->entry.hash = rrset_key_hash(&rrset->rk);
11343b6c3722Schristos ref.key = rrset;
11353b6c3722Schristos ref.id = rrset->id;
11363b6c3722Schristos /* ignore ret: if it was in the cache, ref updated */
11373b6c3722Schristos (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, *env->now);
11383b6c3722Schristos }
11393b6c3722Schristos
11403b6c3722Schristos /** fetch NS record from reply, if any */
11413b6c3722Schristos static struct ub_packed_rrset_key*
reply_get_NS_rrset(struct reply_info * rep)11423b6c3722Schristos reply_get_NS_rrset(struct reply_info* rep)
11433b6c3722Schristos {
11443b6c3722Schristos size_t i;
11453b6c3722Schristos for(i=0; i<rep->rrset_count; i++) {
11463b6c3722Schristos if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NS)) {
11473b6c3722Schristos return rep->rrsets[i];
11483b6c3722Schristos }
11493b6c3722Schristos }
11503b6c3722Schristos return NULL;
11513b6c3722Schristos }
11523b6c3722Schristos
11533b6c3722Schristos void
iter_store_parentside_NS(struct module_env * env,struct reply_info * rep)11543b6c3722Schristos iter_store_parentside_NS(struct module_env* env, struct reply_info* rep)
11553b6c3722Schristos {
11563b6c3722Schristos struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
11573b6c3722Schristos if(rrset) {
11583b6c3722Schristos log_rrset_key(VERB_ALGO, "store parent-side NS", rrset);
11593b6c3722Schristos iter_store_parentside_rrset(env, rrset);
11603b6c3722Schristos }
11613b6c3722Schristos }
11623b6c3722Schristos
iter_store_parentside_neg(struct module_env * env,struct query_info * qinfo,struct reply_info * rep)11633b6c3722Schristos void iter_store_parentside_neg(struct module_env* env,
11643b6c3722Schristos struct query_info* qinfo, struct reply_info* rep)
11653b6c3722Schristos {
11663b6c3722Schristos /* TTL: NS from referral in iq->deleg_msg,
11673b6c3722Schristos * or first RR from iq->response,
11683b6c3722Schristos * or servfail5secs if !iq->response */
11693b6c3722Schristos time_t ttl = NORR_TTL;
11703b6c3722Schristos struct ub_packed_rrset_key* neg;
11713b6c3722Schristos struct packed_rrset_data* newd;
11723b6c3722Schristos if(rep) {
11733b6c3722Schristos struct ub_packed_rrset_key* rrset = reply_get_NS_rrset(rep);
11743b6c3722Schristos if(!rrset && rep->rrset_count != 0) rrset = rep->rrsets[0];
11753b6c3722Schristos if(rrset) ttl = ub_packed_rrset_ttl(rrset);
11763b6c3722Schristos }
11773b6c3722Schristos /* create empty rrset to store */
11783b6c3722Schristos neg = (struct ub_packed_rrset_key*)regional_alloc(env->scratch,
11793b6c3722Schristos sizeof(struct ub_packed_rrset_key));
11803b6c3722Schristos if(!neg) {
11813b6c3722Schristos log_err("out of memory in store_parentside_neg");
11823b6c3722Schristos return;
11833b6c3722Schristos }
11843b6c3722Schristos memset(&neg->entry, 0, sizeof(neg->entry));
11853b6c3722Schristos neg->entry.key = neg;
11863b6c3722Schristos neg->rk.type = htons(qinfo->qtype);
11873b6c3722Schristos neg->rk.rrset_class = htons(qinfo->qclass);
11883b6c3722Schristos neg->rk.flags = 0;
11893b6c3722Schristos neg->rk.dname = regional_alloc_init(env->scratch, qinfo->qname,
11903b6c3722Schristos qinfo->qname_len);
11913b6c3722Schristos if(!neg->rk.dname) {
11923b6c3722Schristos log_err("out of memory in store_parentside_neg");
11933b6c3722Schristos return;
11943b6c3722Schristos }
11953b6c3722Schristos neg->rk.dname_len = qinfo->qname_len;
11963b6c3722Schristos neg->entry.hash = rrset_key_hash(&neg->rk);
11973b6c3722Schristos newd = (struct packed_rrset_data*)regional_alloc_zero(env->scratch,
11983b6c3722Schristos sizeof(struct packed_rrset_data) + sizeof(size_t) +
11993b6c3722Schristos sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t));
12003b6c3722Schristos if(!newd) {
12013b6c3722Schristos log_err("out of memory in store_parentside_neg");
12023b6c3722Schristos return;
12033b6c3722Schristos }
12043b6c3722Schristos neg->entry.data = newd;
12053b6c3722Schristos newd->ttl = ttl;
12063b6c3722Schristos /* entry must have one RR, otherwise not valid in cache.
12073b6c3722Schristos * put in one RR with empty rdata: those are ignored as nameserver */
12083b6c3722Schristos newd->count = 1;
12093b6c3722Schristos newd->rrsig_count = 0;
12103b6c3722Schristos newd->trust = rrset_trust_ans_noAA;
12113b6c3722Schristos newd->rr_len = (size_t*)((uint8_t*)newd +
12123b6c3722Schristos sizeof(struct packed_rrset_data));
12133b6c3722Schristos newd->rr_len[0] = 0 /* zero len rdata */ + sizeof(uint16_t);
12143b6c3722Schristos packed_rrset_ptr_fixup(newd);
12153b6c3722Schristos newd->rr_ttl[0] = newd->ttl;
12163b6c3722Schristos sldns_write_uint16(newd->rr_data[0], 0 /* zero len rdata */);
12173b6c3722Schristos /* store it */
12183b6c3722Schristos log_rrset_key(VERB_ALGO, "store parent-side negative", neg);
12193b6c3722Schristos iter_store_parentside_rrset(env, neg);
12203b6c3722Schristos }
12213b6c3722Schristos
12223b6c3722Schristos int
iter_lookup_parent_NS_from_cache(struct module_env * env,struct delegpt * dp,struct regional * region,struct query_info * qinfo)12233b6c3722Schristos iter_lookup_parent_NS_from_cache(struct module_env* env, struct delegpt* dp,
12243b6c3722Schristos struct regional* region, struct query_info* qinfo)
12253b6c3722Schristos {
12263b6c3722Schristos struct ub_packed_rrset_key* akey;
12273b6c3722Schristos akey = rrset_cache_lookup(env->rrset_cache, dp->name,
12283b6c3722Schristos dp->namelen, LDNS_RR_TYPE_NS, qinfo->qclass,
12293b6c3722Schristos PACKED_RRSET_PARENT_SIDE, *env->now, 0);
12303b6c3722Schristos if(akey) {
12313b6c3722Schristos log_rrset_key(VERB_ALGO, "found parent-side NS in cache", akey);
12323b6c3722Schristos dp->has_parent_side_NS = 1;
12333b6c3722Schristos /* and mark the new names as lame */
12343b6c3722Schristos if(!delegpt_rrset_add_ns(dp, region, akey, 1)) {
12353b6c3722Schristos lock_rw_unlock(&akey->entry.lock);
12363b6c3722Schristos return 0;
12373b6c3722Schristos }
12383b6c3722Schristos lock_rw_unlock(&akey->entry.lock);
12393b6c3722Schristos }
12403b6c3722Schristos return 1;
12413b6c3722Schristos }
12423b6c3722Schristos
iter_lookup_parent_glue_from_cache(struct module_env * env,struct delegpt * dp,struct regional * region,struct query_info * qinfo)12433b6c3722Schristos int iter_lookup_parent_glue_from_cache(struct module_env* env,
12443b6c3722Schristos struct delegpt* dp, struct regional* region, struct query_info* qinfo)
12453b6c3722Schristos {
12463b6c3722Schristos struct ub_packed_rrset_key* akey;
12473b6c3722Schristos struct delegpt_ns* ns;
12483b6c3722Schristos size_t num = delegpt_count_targets(dp);
12493b6c3722Schristos for(ns = dp->nslist; ns; ns = ns->next) {
12507a540f2bSchristos if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE)
12517a540f2bSchristos continue;
12527a540f2bSchristos ns->cache_lookup_count++;
12533b6c3722Schristos /* get cached parentside A */
12543b6c3722Schristos akey = rrset_cache_lookup(env->rrset_cache, ns->name,
12553b6c3722Schristos ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
12563b6c3722Schristos PACKED_RRSET_PARENT_SIDE, *env->now, 0);
12573b6c3722Schristos if(akey) {
12583b6c3722Schristos log_rrset_key(VERB_ALGO, "found parent-side", akey);
12593b6c3722Schristos ns->done_pside4 = 1;
12603b6c3722Schristos /* a negative-cache-element has no addresses it adds */
1261d0eba39bSchristos if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL))
12623b6c3722Schristos log_err("malloc failure in lookup_parent_glue");
12633b6c3722Schristos lock_rw_unlock(&akey->entry.lock);
12643b6c3722Schristos }
12653b6c3722Schristos /* get cached parentside AAAA */
12663b6c3722Schristos akey = rrset_cache_lookup(env->rrset_cache, ns->name,
12673b6c3722Schristos ns->namelen, LDNS_RR_TYPE_AAAA, qinfo->qclass,
12683b6c3722Schristos PACKED_RRSET_PARENT_SIDE, *env->now, 0);
12693b6c3722Schristos if(akey) {
12703b6c3722Schristos log_rrset_key(VERB_ALGO, "found parent-side", akey);
12713b6c3722Schristos ns->done_pside6 = 1;
12723b6c3722Schristos /* a negative-cache-element has no addresses it adds */
1273d0eba39bSchristos if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL))
12743b6c3722Schristos log_err("malloc failure in lookup_parent_glue");
12753b6c3722Schristos lock_rw_unlock(&akey->entry.lock);
12763b6c3722Schristos }
12773b6c3722Schristos }
12783b6c3722Schristos /* see if new (but lame) addresses have become available */
12793b6c3722Schristos return delegpt_count_targets(dp) != num;
12803b6c3722Schristos }
12813b6c3722Schristos
12823b6c3722Schristos int
iter_get_next_root(struct iter_hints * hints,struct iter_forwards * fwd,uint16_t * c)12833b6c3722Schristos iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
12843b6c3722Schristos uint16_t* c)
12853b6c3722Schristos {
12863b6c3722Schristos uint16_t c1 = *c, c2 = *c;
12873b6c3722Schristos int r1 = hints_next_root(hints, &c1);
12883b6c3722Schristos int r2 = forwards_next_root(fwd, &c2);
12893b6c3722Schristos if(!r1 && !r2) /* got none, end of list */
12903b6c3722Schristos return 0;
12913b6c3722Schristos else if(!r1) /* got one, return that */
12923b6c3722Schristos *c = c2;
12933b6c3722Schristos else if(!r2)
12943b6c3722Schristos *c = c1;
12953b6c3722Schristos else if(c1 < c2) /* got both take smallest */
12963b6c3722Schristos *c = c1;
12973b6c3722Schristos else *c = c2;
12983b6c3722Schristos return 1;
12993b6c3722Schristos }
13003b6c3722Schristos
13013b6c3722Schristos void
iter_scrub_ds(struct dns_msg * msg,struct ub_packed_rrset_key * ns,uint8_t * z)13023b6c3722Schristos iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns, uint8_t* z)
13033b6c3722Schristos {
13043b6c3722Schristos /* Only the DS record for the delegation itself is expected.
13053b6c3722Schristos * We allow DS for everything between the bailiwick and the
13063b6c3722Schristos * zonecut, thus DS records must be at or above the zonecut.
13073b6c3722Schristos * And the DS records must be below the server authority zone.
13083b6c3722Schristos * The answer section is already scrubbed. */
13093b6c3722Schristos size_t i = msg->rep->an_numrrsets;
13103b6c3722Schristos while(i < (msg->rep->an_numrrsets + msg->rep->ns_numrrsets)) {
13113b6c3722Schristos struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
13123b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS &&
13133b6c3722Schristos (!ns || !dname_subdomain_c(ns->rk.dname, s->rk.dname)
13143b6c3722Schristos || query_dname_compare(z, s->rk.dname) == 0)) {
13153b6c3722Schristos log_nametypeclass(VERB_ALGO, "removing irrelevant DS",
13163b6c3722Schristos s->rk.dname, ntohs(s->rk.type),
13173b6c3722Schristos ntohs(s->rk.rrset_class));
13183b6c3722Schristos memmove(msg->rep->rrsets+i, msg->rep->rrsets+i+1,
13193b6c3722Schristos sizeof(struct ub_packed_rrset_key*) *
13203b6c3722Schristos (msg->rep->rrset_count-i-1));
13213b6c3722Schristos msg->rep->ns_numrrsets--;
13223b6c3722Schristos msg->rep->rrset_count--;
13233b6c3722Schristos /* stay at same i, but new record */
13243b6c3722Schristos continue;
13253b6c3722Schristos }
13263b6c3722Schristos i++;
13273b6c3722Schristos }
13283b6c3722Schristos }
13293b6c3722Schristos
133001049ae6Schristos void
iter_scrub_nxdomain(struct dns_msg * msg)133101049ae6Schristos iter_scrub_nxdomain(struct dns_msg* msg)
133201049ae6Schristos {
133301049ae6Schristos if(msg->rep->an_numrrsets == 0)
133401049ae6Schristos return;
133501049ae6Schristos
133601049ae6Schristos memmove(msg->rep->rrsets, msg->rep->rrsets+msg->rep->an_numrrsets,
133701049ae6Schristos sizeof(struct ub_packed_rrset_key*) *
133801049ae6Schristos (msg->rep->rrset_count-msg->rep->an_numrrsets));
133901049ae6Schristos msg->rep->rrset_count -= msg->rep->an_numrrsets;
134001049ae6Schristos msg->rep->an_numrrsets = 0;
134101049ae6Schristos }
134201049ae6Schristos
iter_dec_attempts(struct delegpt * dp,int d,int outbound_msg_retry)13437a540f2bSchristos void iter_dec_attempts(struct delegpt* dp, int d, int outbound_msg_retry)
13443b6c3722Schristos {
13453b6c3722Schristos struct delegpt_addr* a;
13463b6c3722Schristos for(a=dp->target_list; a; a = a->next_target) {
13477a540f2bSchristos if(a->attempts >= outbound_msg_retry) {
13483b6c3722Schristos /* add back to result list */
1349*91f7d55fSchristos delegpt_add_to_result_list(dp, a);
13503b6c3722Schristos }
13513b6c3722Schristos if(a->attempts > d)
13523b6c3722Schristos a->attempts -= d;
13533b6c3722Schristos else a->attempts = 0;
13543b6c3722Schristos }
13553b6c3722Schristos }
13563b6c3722Schristos
iter_merge_retry_counts(struct delegpt * dp,struct delegpt * old,int outbound_msg_retry)13577a540f2bSchristos void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old,
13587a540f2bSchristos int outbound_msg_retry)
13593b6c3722Schristos {
13603b6c3722Schristos struct delegpt_addr* a, *o, *prev;
13613b6c3722Schristos for(a=dp->target_list; a; a = a->next_target) {
13623b6c3722Schristos o = delegpt_find_addr(old, &a->addr, a->addrlen);
13633b6c3722Schristos if(o) {
13643b6c3722Schristos log_addr(VERB_ALGO, "copy attempt count previous dp",
13653b6c3722Schristos &a->addr, a->addrlen);
13663b6c3722Schristos a->attempts = o->attempts;
13673b6c3722Schristos }
13683b6c3722Schristos }
13693b6c3722Schristos prev = NULL;
13703b6c3722Schristos a = dp->usable_list;
13713b6c3722Schristos while(a) {
13727a540f2bSchristos if(a->attempts >= outbound_msg_retry) {
13733b6c3722Schristos log_addr(VERB_ALGO, "remove from usable list dp",
13743b6c3722Schristos &a->addr, a->addrlen);
13753b6c3722Schristos /* remove from result list */
13763b6c3722Schristos if(prev)
13773b6c3722Schristos prev->next_usable = a->next_usable;
13783b6c3722Schristos else dp->usable_list = a->next_usable;
13793b6c3722Schristos /* prev stays the same */
13803b6c3722Schristos a = a->next_usable;
13813b6c3722Schristos continue;
13823b6c3722Schristos }
13833b6c3722Schristos prev = a;
13843b6c3722Schristos a = a->next_usable;
13853b6c3722Schristos }
13863b6c3722Schristos }
13873b6c3722Schristos
13883b6c3722Schristos int
iter_ds_toolow(struct dns_msg * msg,struct delegpt * dp)13893b6c3722Schristos iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp)
13903b6c3722Schristos {
13913b6c3722Schristos /* if for query example.com, there is example.com SOA or a subdomain
13923b6c3722Schristos * of example.com, then we are too low and need to fetch NS. */
13933b6c3722Schristos size_t i;
13943b6c3722Schristos /* if we have a DNAME or CNAME we are probably wrong */
13953b6c3722Schristos /* if we have a qtype DS in the answer section, its fine */
13963b6c3722Schristos for(i=0; i < msg->rep->an_numrrsets; i++) {
13973b6c3722Schristos struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
13983b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME ||
13993b6c3722Schristos ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
14003b6c3722Schristos /* not the right answer, maybe too low, check the
14013b6c3722Schristos * RRSIG signer name (if there is any) for a hint
14023b6c3722Schristos * that it is from the dp zone anyway */
14033b6c3722Schristos uint8_t* sname;
14043b6c3722Schristos size_t slen;
14053b6c3722Schristos val_find_rrset_signer(s, &sname, &slen);
14063b6c3722Schristos if(sname && query_dname_compare(dp->name, sname)==0)
14073b6c3722Schristos return 0; /* it is fine, from the right dp */
14083b6c3722Schristos return 1;
14093b6c3722Schristos }
14103b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_DS)
14113b6c3722Schristos return 0; /* fine, we have a DS record */
14123b6c3722Schristos }
14133b6c3722Schristos for(i=msg->rep->an_numrrsets;
14143b6c3722Schristos i < msg->rep->an_numrrsets + msg->rep->ns_numrrsets; i++) {
14153b6c3722Schristos struct ub_packed_rrset_key* s = msg->rep->rrsets[i];
14163b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_SOA) {
14173b6c3722Schristos if(dname_subdomain_c(s->rk.dname, msg->qinfo.qname))
14183b6c3722Schristos return 1; /* point is too low */
14193b6c3722Schristos if(query_dname_compare(s->rk.dname, dp->name)==0)
14203b6c3722Schristos return 0; /* right dp */
14213b6c3722Schristos }
14223b6c3722Schristos if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC ||
14233b6c3722Schristos ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
14243b6c3722Schristos uint8_t* sname;
14253b6c3722Schristos size_t slen;
14263b6c3722Schristos val_find_rrset_signer(s, &sname, &slen);
14273b6c3722Schristos if(sname && query_dname_compare(dp->name, sname)==0)
14283b6c3722Schristos return 0; /* it is fine, from the right dp */
14293b6c3722Schristos return 1;
14303b6c3722Schristos }
14313b6c3722Schristos }
14323b6c3722Schristos /* we do not know */
14333b6c3722Schristos return 1;
14343b6c3722Schristos }
14353b6c3722Schristos
iter_dp_cangodown(struct query_info * qinfo,struct delegpt * dp)14363b6c3722Schristos int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp)
14373b6c3722Schristos {
14383b6c3722Schristos /* no delegation point, do not see how we can go down,
14393b6c3722Schristos * robust check, it should really exist */
14403b6c3722Schristos if(!dp) return 0;
14413b6c3722Schristos
14423b6c3722Schristos /* see if dp equals the qname, then we cannot go down further */
14433b6c3722Schristos if(query_dname_compare(qinfo->qname, dp->name) == 0)
14443b6c3722Schristos return 0;
14453b6c3722Schristos /* if dp is one label above the name we also cannot go down further */
14463b6c3722Schristos if(dname_count_labels(qinfo->qname) == dp->namelabs+1)
14473b6c3722Schristos return 0;
14483b6c3722Schristos return 1;
14493b6c3722Schristos }
1450f42d8de7Schristos
1451f42d8de7Schristos int
iter_stub_fwd_no_cache(struct module_qstate * qstate,struct query_info * qinf,uint8_t ** retdpname,size_t * retdpnamelen)14527a540f2bSchristos iter_stub_fwd_no_cache(struct module_qstate *qstate, struct query_info *qinf,
14537a540f2bSchristos uint8_t** retdpname, size_t* retdpnamelen)
1454f42d8de7Schristos {
1455f42d8de7Schristos struct iter_hints_stub *stub;
1456f42d8de7Schristos struct delegpt *dp;
1457f42d8de7Schristos
1458f42d8de7Schristos /* Check for stub. */
1459f42d8de7Schristos stub = hints_lookup_stub(qstate->env->hints, qinf->qname,
1460f42d8de7Schristos qinf->qclass, NULL);
1461f42d8de7Schristos dp = forwards_lookup(qstate->env->fwds, qinf->qname, qinf->qclass);
1462f42d8de7Schristos
1463f42d8de7Schristos /* see if forward or stub is more pertinent */
1464f42d8de7Schristos if(stub && stub->dp && dp) {
1465f42d8de7Schristos if(dname_strict_subdomain(dp->name, dp->namelabs,
1466f42d8de7Schristos stub->dp->name, stub->dp->namelabs)) {
1467f42d8de7Schristos stub = NULL; /* ignore stub, forward is lower */
1468f42d8de7Schristos } else {
1469f42d8de7Schristos dp = NULL; /* ignore forward, stub is lower */
1470f42d8de7Schristos }
1471f42d8de7Schristos }
1472f42d8de7Schristos
1473f42d8de7Schristos /* check stub */
1474f42d8de7Schristos if (stub != NULL && stub->dp != NULL) {
1475f42d8de7Schristos if(stub->dp->no_cache) {
1476f42d8de7Schristos char qname[255+1];
1477f42d8de7Schristos char dpname[255+1];
1478f42d8de7Schristos dname_str(qinf->qname, qname);
1479f42d8de7Schristos dname_str(stub->dp->name, dpname);
1480f42d8de7Schristos verbose(VERB_ALGO, "stub for %s %s has no_cache", qname, dpname);
1481f42d8de7Schristos }
14827a540f2bSchristos if(retdpname) {
14837a540f2bSchristos *retdpname = stub->dp->name;
14847a540f2bSchristos *retdpnamelen = stub->dp->namelen;
14857a540f2bSchristos }
1486f42d8de7Schristos return (stub->dp->no_cache);
1487f42d8de7Schristos }
1488f42d8de7Schristos
1489f42d8de7Schristos /* Check for forward. */
1490f42d8de7Schristos if (dp) {
1491f42d8de7Schristos if(dp->no_cache) {
1492f42d8de7Schristos char qname[255+1];
1493f42d8de7Schristos char dpname[255+1];
1494f42d8de7Schristos dname_str(qinf->qname, qname);
1495f42d8de7Schristos dname_str(dp->name, dpname);
1496f42d8de7Schristos verbose(VERB_ALGO, "forward for %s %s has no_cache", qname, dpname);
1497f42d8de7Schristos }
14987a540f2bSchristos if(retdpname) {
14997a540f2bSchristos *retdpname = dp->name;
15007a540f2bSchristos *retdpnamelen = dp->namelen;
15017a540f2bSchristos }
1502f42d8de7Schristos return (dp->no_cache);
1503f42d8de7Schristos }
15047a540f2bSchristos if(retdpname) {
15057a540f2bSchristos *retdpname = NULL;
15067a540f2bSchristos *retdpnamelen = 0;
15077a540f2bSchristos }
1508f42d8de7Schristos return 0;
1509f42d8de7Schristos }
15107a540f2bSchristos
iterator_set_ip46_support(struct module_stack * mods,struct module_env * env,struct outside_network * outnet)15117a540f2bSchristos void iterator_set_ip46_support(struct module_stack* mods,
15127a540f2bSchristos struct module_env* env, struct outside_network* outnet)
15137a540f2bSchristos {
15147a540f2bSchristos int m = modstack_find(mods, "iterator");
15157a540f2bSchristos struct iter_env* ie = NULL;
15167a540f2bSchristos if(m == -1)
15177a540f2bSchristos return;
15187a540f2bSchristos ie = (struct iter_env*)env->modinfo[m];
15197a540f2bSchristos if(outnet->pending == NULL)
15207a540f2bSchristos return; /* we are in testbound, no rbtree for UDP */
15217a540f2bSchristos if(outnet->num_ip4 == 0)
15227a540f2bSchristos ie->supports_ipv4 = 0;
15237a540f2bSchristos if(outnet->num_ip6 == 0)
15247a540f2bSchristos ie->supports_ipv6 = 0;
15257a540f2bSchristos }
1526