Blame SOURCES/unbound-1.7.3-amplifying-an-incoming-query.patch

8670e0
diff --git a/iterator/iter_delegpt.c b/iterator/iter_delegpt.c
8670e0
index f88b3e1..522e0e9 100644
8670e0
--- a/iterator/iter_delegpt.c
8670e0
+++ b/iterator/iter_delegpt.c
8670e0
@@ -84,7 +84,7 @@ struct delegpt* delegpt_copy(struct delegpt* dp, struct regional* region)
8670e0
 	}
8670e0
 	for(a = dp->target_list; a; a = a->next_target) {
8670e0
 		if(!delegpt_add_addr(copy, region, &a->addr, a->addrlen, 
8670e0
-			a->bogus, a->lame, a->tls_auth_name))
8670e0
+           a->bogus, a->lame, a->tls_auth_name, NULL))
8670e0
 			return NULL;
8670e0
 	}
8670e0
 	return copy;
8670e0
@@ -161,7 +161,7 @@ delegpt_find_addr(struct delegpt* dp, struct sockaddr_storage* addr,
8670e0
 int 
8670e0
 delegpt_add_target(struct delegpt* dp, struct regional* region, 
8670e0
 	uint8_t* name, size_t namelen, struct sockaddr_storage* addr, 
8670e0
-	socklen_t addrlen, uint8_t bogus, uint8_t lame)
8670e0
+   socklen_t addrlen, uint8_t bogus, uint8_t lame, int* additions)
8670e0
 {
8670e0
 	struct delegpt_ns* ns = delegpt_find_ns(dp, name, namelen);
8670e0
 	log_assert(!dp->dp_type_mlc);
8670e0
@@ -176,13 +176,14 @@ delegpt_add_target(struct delegpt* dp, struct regional* region,
8670e0
 		if(ns->got4 && ns->got6)
8670e0
 			ns->resolved = 1;
8670e0
 	}
8670e0
-	return delegpt_add_addr(dp, region, addr, addrlen, bogus, lame, NULL);
8670e0
+	return delegpt_add_addr(dp, region, addr, addrlen, bogus, lame, NULL,
8670e0
+        additions);
8670e0
 }
8670e0
 
8670e0
 int 
8670e0
 delegpt_add_addr(struct delegpt* dp, struct regional* region, 
8670e0
 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus, 
8670e0
-	uint8_t lame, char* tls_auth_name)
8670e0
+	uint8_t lame, char* tls_auth_name, int* additions)
8670e0
 {
8670e0
 	struct delegpt_addr* a;
8670e0
 	log_assert(!dp->dp_type_mlc);
8670e0
@@ -195,6 +196,9 @@ delegpt_add_addr(struct delegpt* dp, struct regional* region,
8670e0
 		return 1;
8670e0
 	}
8670e0
 
8670e0
+    if(additions)
8670e0
+        *additions = 1;
8670e0
+
8670e0
 	a = (struct delegpt_addr*)regional_alloc(region,
8670e0
 		sizeof(struct delegpt_addr));
8670e0
 	if(!a)
8670e0
@@ -382,10 +386,10 @@ delegpt_from_message(struct dns_msg* msg, struct regional* region)
8670e0
 			continue;
8670e0
 
8670e0
 		if(ntohs(s->rk.type) == LDNS_RR_TYPE_A) {
8670e0
-			if(!delegpt_add_rrset_A(dp, region, s, 0))
8670e0
+			if(!delegpt_add_rrset_A(dp, region, s, 0, NULL))
8670e0
 				return NULL;
8670e0
 		} else if(ntohs(s->rk.type) == LDNS_RR_TYPE_AAAA) {
8670e0
-			if(!delegpt_add_rrset_AAAA(dp, region, s, 0))
8670e0
+			if(!delegpt_add_rrset_AAAA(dp, region, s, 0, NULL))
8670e0
 				return NULL;
8670e0
 		}
8670e0
 	}
8670e0
@@ -416,7 +420,7 @@ delegpt_rrset_add_ns(struct delegpt* dp, struct regional* region,
8670e0
 
8670e0
 int 
8670e0
 delegpt_add_rrset_A(struct delegpt* dp, struct regional* region,
8670e0
-	struct ub_packed_rrset_key* ak, uint8_t lame)
8670e0
+	struct ub_packed_rrset_key* ak, uint8_t lame, int* additions)
8670e0
 {
8670e0
         struct packed_rrset_data* d=(struct packed_rrset_data*)ak->entry.data;
8670e0
         size_t i;
8670e0
@@ -432,7 +436,7 @@ delegpt_add_rrset_A(struct delegpt* dp, struct regional* region,
8670e0
                 memmove(&sa.sin_addr, d->rr_data[i]+2, INET_SIZE);
8670e0
                 if(!delegpt_add_target(dp, region, ak->rk.dname,
8670e0
                         ak->rk.dname_len, (struct sockaddr_storage*)&sa,
8670e0
-                        len, (d->security==sec_status_bogus), lame))
8670e0
+                        len, (d->security==sec_status_bogus), lame, additions))
8670e0
                         return 0;
8670e0
         }
8670e0
         return 1;
8670e0
@@ -440,7 +444,7 @@ delegpt_add_rrset_A(struct delegpt* dp, struct regional* region,
8670e0
 
8670e0
 int 
8670e0
 delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* region,
8670e0
-	struct ub_packed_rrset_key* ak, uint8_t lame)
8670e0
+	struct ub_packed_rrset_key* ak, uint8_t lame, int* additions)
8670e0
 {
8670e0
         struct packed_rrset_data* d=(struct packed_rrset_data*)ak->entry.data;
8670e0
         size_t i;
8670e0
@@ -456,7 +460,7 @@ delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* region,
8670e0
                 memmove(&sa.sin6_addr, d->rr_data[i]+2, INET6_SIZE);
8670e0
                 if(!delegpt_add_target(dp, region, ak->rk.dname,
8670e0
                         ak->rk.dname_len, (struct sockaddr_storage*)&sa,
8670e0
-                        len, (d->security==sec_status_bogus), lame))
8670e0
+                        len, (d->security==sec_status_bogus), lame, additions))
8670e0
                         return 0;
8670e0
         }
8670e0
         return 1;
8670e0
@@ -464,20 +468,32 @@ delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* region,
8670e0
 
8670e0
 int 
8670e0
 delegpt_add_rrset(struct delegpt* dp, struct regional* region,
8670e0
-        struct ub_packed_rrset_key* rrset, uint8_t lame)
8670e0
+        struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions)
8670e0
 {
8670e0
 	if(!rrset)
8670e0
 		return 1;
8670e0
 	if(ntohs(rrset->rk.type) == LDNS_RR_TYPE_NS)
8670e0
 		return delegpt_rrset_add_ns(dp, region, rrset, lame);
8670e0
 	else if(ntohs(rrset->rk.type) == LDNS_RR_TYPE_A)
8670e0
-		return delegpt_add_rrset_A(dp, region, rrset, lame);
8670e0
+		return delegpt_add_rrset_A(dp, region, rrset, lame, additions);
8670e0
 	else if(ntohs(rrset->rk.type) == LDNS_RR_TYPE_AAAA)
8670e0
-		return delegpt_add_rrset_AAAA(dp, region, rrset, lame);
8670e0
+		return delegpt_add_rrset_AAAA(dp, region, rrset, lame, additions);
8670e0
 	log_warn("Unknown rrset type added to delegpt");
8670e0
 	return 1;
8670e0
 }
8670e0
 
8670e0
+void delegpt_mark_neg(struct delegpt_ns* ns, uint16_t qtype)
8670e0
+{
8670e0
+    if(ns) {
8670e0
+        if(qtype == LDNS_RR_TYPE_A)
8670e0
+            ns->got4 = 2;
8670e0
+        else if(qtype == LDNS_RR_TYPE_AAAA)
8670e0
+            ns->got6 = 2;
8670e0
+        if(ns->got4 && ns->got6)
8670e0
+            ns->resolved = 1;
8670e0
+    }
8670e0
+}
8670e0
+
8670e0
 void delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg)
8670e0
 {
8670e0
 	struct reply_info* rep = (struct reply_info*)msg->entry.data;
8670e0
@@ -487,14 +503,7 @@ void delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg)
8670e0
 	if(FLAGS_GET_RCODE(rep->flags) != 0 || rep->an_numrrsets == 0) {
8670e0
 		struct delegpt_ns* ns = delegpt_find_ns(dp, msg->key.qname, 
8670e0
 			msg->key.qname_len);
8670e0
-		if(ns) {
8670e0
-			if(msg->key.qtype == LDNS_RR_TYPE_A)
8670e0
-				ns->got4 = 1;
8670e0
-			else if(msg->key.qtype == LDNS_RR_TYPE_AAAA)
8670e0
-				ns->got6 = 1;
8670e0
-			if(ns->got4 && ns->got6)
8670e0
-				ns->resolved = 1;
8670e0
-		}
8670e0
+        delegpt_mark_neg(ns, msg->key.qtype);
8670e0
 	}
8670e0
 }
8670e0
 
8670e0
diff --git a/iterator/iter_delegpt.h b/iterator/iter_delegpt.h
8670e0
index 354bd61..3aded22 100644
8670e0
--- a/iterator/iter_delegpt.h
8670e0
+++ b/iterator/iter_delegpt.h
8670e0
@@ -104,9 +104,10 @@ struct delegpt_ns {
8670e0
 	 * and marked true if got4 and got6 are both true.
8670e0
 	 */
8670e0
 	int resolved;
8670e0
-	/** if the ipv4 address is in the delegpt */
8670e0
+	/** if the ipv4 address is in the delegpt, 0=not, 1=yes 2=negative,
8670e0
+     * negative means it was done, but no content. */
8670e0
 	uint8_t got4;
8670e0
-	/** if the ipv6 address is in the delegpt */
8670e0
+	/** if the ipv6 address is in the delegpt, 0=not, 1=yes 2=negative */
8670e0
 	uint8_t got6;
8670e0
 	/**
8670e0
 	 * If the name is parent-side only and thus dispreferred.
8670e0
@@ -213,11 +214,12 @@ int delegpt_rrset_add_ns(struct delegpt* dp, struct regional* regional,
8670e0
  * @param addrlen: the length of addr.
8670e0
  * @param bogus: security status for the address, pass true if bogus.
8670e0
  * @param lame: address is lame.
8670e0
+ * @param additions: will be set to 1 if a new address is added
8670e0
  * @return false on error.
8670e0
  */
8670e0
 int delegpt_add_target(struct delegpt* dp, struct regional* regional, 
8670e0
 	uint8_t* name, size_t namelen, struct sockaddr_storage* addr, 
8670e0
-	socklen_t addrlen, uint8_t bogus, uint8_t lame);
8670e0
+	socklen_t addrlen, uint8_t bogus, uint8_t lame, int* additions);
8670e0
 
8670e0
 /**
8670e0
  * Add A RRset to delegpt.
8670e0
@@ -225,10 +227,11 @@ int delegpt_add_target(struct delegpt* dp, struct regional* regional,
8670e0
  * @param regional: where to allocate the info.
8670e0
  * @param rrset: RRset A to add.
8670e0
  * @param lame: rrset is lame, disprefer it.
8670e0
+ * @param additions: will be set to 1 if a new address is added
8670e0
  * @return 0 on alloc error.
8670e0
  */
8670e0
 int delegpt_add_rrset_A(struct delegpt* dp, struct regional* regional, 
8670e0
-	struct ub_packed_rrset_key* rrset, uint8_t lame);
8670e0
+	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
8670e0
 
8670e0
 /**
8670e0
  * Add AAAA RRset to delegpt.
8670e0
@@ -236,10 +239,11 @@ int delegpt_add_rrset_A(struct delegpt* dp, struct regional* regional,
8670e0
  * @param regional: where to allocate the info.
8670e0
  * @param rrset: RRset AAAA to add.
8670e0
  * @param lame: rrset is lame, disprefer it.
8670e0
+ * @param additions: will be set to 1 if a new address is added
8670e0
  * @return 0 on alloc error.
8670e0
  */
8670e0
 int delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* regional, 
8670e0
-	struct ub_packed_rrset_key* rrset, uint8_t lame);
8670e0
+	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
8670e0
 
8670e0
 /**
8670e0
  * Add any RRset to delegpt.
8670e0
@@ -248,10 +252,11 @@ int delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* regional,
8670e0
  * @param regional: where to allocate the info.
8670e0
  * @param rrset: RRset to add, NS, A, AAAA.
8670e0
  * @param lame: rrset is lame, disprefer it.
8670e0
+ * @param additions: will be set to 1 if a new address is added
8670e0
  * @return 0 on alloc error.
8670e0
  */
8670e0
 int delegpt_add_rrset(struct delegpt* dp, struct regional* regional, 
8670e0
-	struct ub_packed_rrset_key* rrset, uint8_t lame);
8670e0
+	struct ub_packed_rrset_key* rrset, uint8_t lame, int* additions);
8670e0
 
8670e0
 /**
8670e0
  * Add address to the delegation point. No servername is associated or checked.
8670e0
@@ -262,11 +267,13 @@ int delegpt_add_rrset(struct delegpt* dp, struct regional* regional,
8670e0
  * @param bogus: if address is bogus.
8670e0
  * @param lame: if address is lame.
8670e0
  * @param tls_auth_name: TLS authentication name (or NULL).
8670e0
+ * @param additions: will be set to 1 if a new address is added
8670e0
+ * @return 0 on alloc error.
8670e0
  * @return false on error.
8670e0
  */
8670e0
 int delegpt_add_addr(struct delegpt* dp, struct regional* regional, 
8670e0
 	struct sockaddr_storage* addr, socklen_t addrlen,
8670e0
-	uint8_t bogus, uint8_t lame, char* tls_auth_name);
8670e0
+	uint8_t bogus, uint8_t lame, char* tls_auth_name, int* additions);
8670e0
 
8670e0
 /** 
8670e0
  * Find NS record in name list of delegation point.
8670e0
@@ -339,6 +346,14 @@ size_t delegpt_count_targets(struct delegpt* dp);
8670e0
 struct delegpt* delegpt_from_message(struct dns_msg* msg, 
8670e0
 	struct regional* regional);
8670e0
 
8670e0
+/**
8670e0
+* Mark negative return in delegation point for specific nameserver.
8670e0
+* sets the got4 or got6 to negative, updates the ns->resolved.
8670e0
+* @param ns: the nameserver in the delegpt.
8670e0
+* @param qtype: A or AAAA (host order).
8670e0
+*/
8670e0
+void delegpt_mark_neg(struct delegpt_ns* ns, uint16_t qtype);
8670e0
+
8670e0
 /**
8670e0
  * Add negative message to delegation point.
8670e0
  * @param dp: delegation point.
8670e0
diff --git a/iterator/iter_scrub.c b/iterator/iter_scrub.c
8670e0
index 12580dc..8230d17 100644
8670e0
--- a/iterator/iter_scrub.c
8670e0
+++ b/iterator/iter_scrub.c
8670e0
@@ -185,8 +185,9 @@ mark_additional_rrset(sldns_buffer* pkt, struct msg_parse* msg,
8670e0
 /** Get target name of a CNAME */
8670e0
 static int
8670e0
 parse_get_cname_target(struct rrset_parse* rrset, uint8_t** sname, 
8670e0
-	size_t* snamelen)
8670e0
+	size_t* snamelen, sldns_buffer* pkt)
8670e0
 {
8670e0
+    size_t oldpos, dlen;
8670e0
 	if(rrset->rr_count != 1) {
8670e0
 		struct rr_parse* sig;
8670e0
 		verbose(VERB_ALGO, "Found CNAME rrset with "
8670e0
@@ -204,6 +205,19 @@ parse_get_cname_target(struct rrset_parse* rrset, uint8_t** sname,
8670e0
 	*sname = rrset->rr_first->ttl_data + sizeof(uint32_t)
8670e0
 		+ sizeof(uint16_t); /* skip ttl, rdatalen */
8670e0
 	*snamelen = rrset->rr_first->size - sizeof(uint16_t);
8670e0
+
8670e0
+    if(rrset->rr_first->outside_packet) {
8670e0
+        if(!dname_valid(*sname, *snamelen))
8670e0
+            return 0;
8670e0
+        return 1;
8670e0
+    }
8670e0
+    oldpos = sldns_buffer_position(pkt);
8670e0
+    sldns_buffer_set_position(pkt, (size_t)(*sname - sldns_buffer_begin(pkt)));
8670e0
+    dlen = pkt_dname_len(pkt);
8670e0
+    sldns_buffer_set_position(pkt, oldpos);
8670e0
+    if(dlen == 0)
8670e0
+        return 0; /* parse fail on the rdata name */
8670e0
+    *snamelen = dlen;
8670e0
 	return 1;
8670e0
 }
8670e0
 
8670e0
@@ -215,7 +229,7 @@ synth_cname(uint8_t* qname, size_t qnamelen, struct rrset_parse* dname_rrset,
8670e0
 	/* we already know that sname is a strict subdomain of DNAME owner */
8670e0
 	uint8_t* dtarg = NULL;
8670e0
 	size_t dtarglen;
8670e0
-	if(!parse_get_cname_target(dname_rrset, &dtarg, &dtarglen))
8670e0
+	if(!parse_get_cname_target(dname_rrset, &dtarg, &dtarglen, pkt))
8670e0
 		return 0; 
8670e0
 	log_assert(qnamelen > dname_rrset->dname_len);
8670e0
 	/* DNAME from com. to net. with qname example.com. -> example.net. */
8670e0
@@ -372,7 +386,7 @@ scrub_normalize(sldns_buffer* pkt, struct msg_parse* msg,
8670e0
 				/* check next cname */
8670e0
 				uint8_t* t = NULL;
8670e0
 				size_t tlen = 0;
8670e0
-				if(!parse_get_cname_target(nx, &t, &tlen))
8670e0
+				if(!parse_get_cname_target(nx, &t, &tlen, pkt))
8670e0
 					return 0;
8670e0
 				if(dname_pkt_compare(pkt, alias, t) == 0) {
8670e0
 					/* it's OK and better capitalized */
8670e0
@@ -423,7 +437,7 @@ scrub_normalize(sldns_buffer* pkt, struct msg_parse* msg,
8670e0
 				size_t tlen = 0;
8670e0
 				if(synth_cname(sname, snamelen, nx, alias,
8670e0
 					&aliaslen, pkt) &&
8670e0
-					parse_get_cname_target(rrset, &t, &tlen) &&
8670e0
+					parse_get_cname_target(rrset, &t, &tlen, pkt) &&
8670e0
 			   		dname_pkt_compare(pkt, alias, t) == 0) {
8670e0
 					/* the synthesized CNAME equals the
8670e0
 					 * current CNAME.  This CNAME is the
8670e0
@@ -442,7 +456,7 @@ scrub_normalize(sldns_buffer* pkt, struct msg_parse* msg,
8670e0
 			}
8670e0
 
8670e0
 			/* move to next name in CNAME chain */
8670e0
-			if(!parse_get_cname_target(rrset, &sname, &snamelen))
8670e0
+			if(!parse_get_cname_target(rrset, &sname, &snamelen, pkt))
8670e0
 				return 0;
8670e0
 			prev = rrset;
8670e0
 			rrset = rrset->rrset_all_next;
8670e0
diff --git a/iterator/iter_utils.c b/iterator/iter_utils.c
8670e0
index 0a8f770..a107bee 100644
8670e0
--- a/iterator/iter_utils.c
8670e0
+++ b/iterator/iter_utils.c
8670e0
@@ -1008,7 +1008,7 @@ int iter_lookup_parent_glue_from_cache(struct module_env* env,
8670e0
 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
8670e0
 			ns->done_pside4 = 1;
8670e0
 			/* a negative-cache-element has no addresses it adds */
8670e0
-			if(!delegpt_add_rrset_A(dp, region, akey, 1))
8670e0
+			if(!delegpt_add_rrset_A(dp, region, akey, 1, NULL))
8670e0
 				log_err("malloc failure in lookup_parent_glue");
8670e0
 			lock_rw_unlock(&akey->entry.lock);
8670e0
 		}
8670e0
@@ -1020,7 +1020,7 @@ int iter_lookup_parent_glue_from_cache(struct module_env* env,
8670e0
 			log_rrset_key(VERB_ALGO, "found parent-side", akey);
8670e0
 			ns->done_pside6 = 1;
8670e0
 			/* a negative-cache-element has no addresses it adds */
8670e0
-			if(!delegpt_add_rrset_AAAA(dp, region, akey, 1))
8670e0
+			if(!delegpt_add_rrset_AAAA(dp, region, akey, 1, NULL))
8670e0
 				log_err("malloc failure in lookup_parent_glue");
8670e0
 			lock_rw_unlock(&akey->entry.lock);
8670e0
 		}
8670e0
diff --git a/iterator/iterator.c b/iterator/iterator.c
8670e0
index 58a9bff..a4ad319 100644
8670e0
--- a/iterator/iterator.c
8670e0
+++ b/iterator/iterator.c
8670e0
@@ -69,6 +69,8 @@
8670e0
 #include "sldns/parseutil.h"
8670e0
 #include "sldns/sbuffer.h"
8670e0
 
8670e0
+static void target_count_increase_nx(struct iter_qstate* iq, int num);
8670e0
+
8670e0
 int 
8670e0
 iter_init(struct module_env* env, int id)
8670e0
 {
8670e0
@@ -147,6 +149,7 @@ iter_new(struct module_qstate* qstate, int id)
8670e0
 	iq->sent_count = 0;
8670e0
 	iq->ratelimit_ok = 0;
8670e0
 	iq->target_count = NULL;
8670e0
+    iq->dp_target_count = 0;
8670e0
 	iq->wait_priming_stub = 0;
8670e0
 	iq->refetch_glue = 0;
8670e0
 	iq->dnssec_expected = 0;
8670e0
@@ -218,6 +221,7 @@ final_state(struct iter_qstate* iq)
8670e0
 static void
8670e0
 error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
8670e0
 {
8670e0
+    struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
8670e0
 	struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
8670e0
 
8670e0
 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
8670e0
@@ -242,7 +246,11 @@ error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
8670e0
 				super->region, super_iq->dp))
8670e0
 				log_err("out of memory adding missing");
8670e0
 		}
8670e0
+        delegpt_mark_neg(dpns, qstate->qinfo.qtype);
8670e0
 		dpns->resolved = 1; /* mark as failed */
8670e0
+        if((dpns->got4 == 2 || !ie->supports_ipv4) &&
8670e0
+            (dpns->got6 == 2 || !ie->supports_ipv6))
8670e0
+            target_count_increase_nx(super_iq, 1);
8670e0
 	}
8670e0
 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
8670e0
 		/* prime failed to get delegation */
8670e0
@@ -577,7 +585,7 @@ static void
8670e0
 target_count_create(struct iter_qstate* iq)
8670e0
 {
8670e0
 	if(!iq->target_count) {
8670e0
-		iq->target_count = (int*)calloc(2, sizeof(int));
8670e0
+		iq->target_count = (int*)calloc(3, sizeof(int));
8670e0
 		/* if calloc fails we simply do not track this number */
8670e0
 		if(iq->target_count)
8670e0
 			iq->target_count[0] = 1;
8670e0
@@ -590,6 +598,15 @@ target_count_increase(struct iter_qstate* iq, int num)
8670e0
 	target_count_create(iq);
8670e0
 	if(iq->target_count)
8670e0
 		iq->target_count[1] += num;
8670e0
+    iq->dp_target_count++;
8670e0
+}
8670e0
+
8670e0
+static void
8670e0
+target_count_increase_nx(struct iter_qstate* iq, int num)
8670e0
+{
8670e0
+   target_count_create(iq);
8670e0
+   if(iq->target_count)
8670e0
+       iq->target_count[2] += num;
8670e0
 }
8670e0
 
8670e0
 /**
8670e0
@@ -612,13 +629,15 @@ target_count_increase(struct iter_qstate* iq, int num)
8670e0
  * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
8670e0
  * 	not need initialisation.
8670e0
  * @param v: if true, validation is done on the subquery.
8670e0
+ * @param detached: true if this qstate should not attach to the subquery
8670e0
  * @return false on error (malloc).
8670e0
  */
8670e0
 static int
8670e0
 generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype, 
8670e0
 	uint16_t qclass, struct module_qstate* qstate, int id,
8670e0
 	struct iter_qstate* iq, enum iter_state initial_state, 
8670e0
-	enum iter_state finalstate, struct module_qstate** subq_ret, int v)
8670e0
+	enum iter_state finalstate, struct module_qstate** subq_ret, int v,
8670e0
+    int detached)
8670e0
 {
8670e0
 	struct module_qstate* subq = NULL;
8670e0
 	struct iter_qstate* subiq = NULL;
8670e0
@@ -645,12 +664,24 @@ generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
8670e0
 		valrec = 1;
8670e0
 	}
8670e0
 	
8670e0
-	/* attach subquery, lookup existing or make a new one */
8670e0
-	fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
8670e0
-	if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime, valrec,
8670e0
-		&subq)) {
8670e0
-		return 0;
8670e0
-	}
8670e0
+    if(detached) {
8670e0
+        struct mesh_state* sub = NULL;
8670e0
+        fptr_ok(fptr_whitelist_modenv_add_sub(
8670e0
+            qstate->env->add_sub));
8670e0
+        if(!(*qstate->env->add_sub)(qstate, &qinf,
8670e0
+            qflags, prime, valrec, &subq, &sub)){
8670e0
+            return 0;
8670e0
+        }
8670e0
+    }
8670e0
+    else {
8670e0
+        /* attach subquery, lookup existing or make a new one */
8670e0
+        fptr_ok(fptr_whitelist_modenv_attach_sub(
8670e0
+            qstate->env->attach_sub));
8670e0
+        if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime,
8670e0
+            valrec, &subq)) {
8670e0
+            return 0;
8670e0
+        }
8670e0
+    }
8670e0
 	*subq_ret = subq;
8670e0
 	if(subq) {
8670e0
 		/* initialise the new subquery */
8670e0
@@ -672,6 +703,7 @@ generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
8670e0
 		subiq->target_count = iq->target_count;
8670e0
 		if(iq->target_count)
8670e0
 			iq->target_count[0] ++; /* extra reference */
8670e0
+        subiq->dp_target_count = 0;
8670e0
 		subiq->num_current_queries = 0;
8670e0
 		subiq->depth = iq->depth+1;
8670e0
 		outbound_list_init(&subiq->outlist);
8670e0
@@ -715,7 +747,7 @@ prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
8670e0
 	 * the normal INIT state logic (which would cause an infloop). */
8670e0
 	if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS, 
8670e0
 		qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
8670e0
-		&subq, 0)) {
8670e0
+		&subq, 0, 0)) {
8670e0
 		verbose(VERB_ALGO, "could not prime root");
8670e0
 		return 0;
8670e0
 	}
8670e0
@@ -805,7 +837,7 @@ prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
8670e0
 	 * redundant INIT state processing. */
8670e0
 	if(!generate_sub_request(stub_dp->name, stub_dp->namelen, 
8670e0
 		LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
8670e0
-		QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0)) {
8670e0
+		QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0, 0)) {
8670e0
 		verbose(VERB_ALGO, "could not prime stub");
8670e0
 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
8670e0
 		return 1; /* return 1 to make module stop, with error */
8670e0
@@ -976,7 +1008,7 @@ generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 		if(!generate_sub_request(s->rk.dname, s->rk.dname_len, 
8670e0
 			ntohs(s->rk.type), ntohs(s->rk.rrset_class),
8670e0
 			qstate, id, iq,
8670e0
-			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
8670e0
+			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1, 0)) {
8670e0
 			verbose(VERB_ALGO, "could not generate addr check");
8670e0
 			return;
8670e0
 		}
8670e0
@@ -1020,7 +1052,7 @@ generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
8670e0
 		iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
8670e0
 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen, 
8670e0
 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
8670e0
-		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
8670e0
+		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1, 0)) {
8670e0
 		verbose(VERB_ALGO, "could not generate ns check");
8670e0
 		return;
8670e0
 	}
8670e0
@@ -1077,7 +1109,7 @@ generate_dnskey_prefetch(struct module_qstate* qstate,
8670e0
 		iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
8670e0
 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen, 
8670e0
 		LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
8670e0
-		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
8670e0
+		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0)) {
8670e0
 		/* we'll be slower, but it'll work */
8670e0
 		verbose(VERB_ALGO, "could not generate dnskey prefetch");
8670e0
 		return;
8670e0
@@ -1251,6 +1283,7 @@ processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 			iq->refetch_glue = 0;
8670e0
 			iq->query_restart_count++;
8670e0
 			iq->sent_count = 0;
8670e0
+            iq->dp_target_count = 0;
8670e0
 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
8670e0
 			if(qstate->env->cfg->qname_minimisation)
8670e0
 				iq->minimisation_state = INIT_MINIMISE_STATE;
8670e0
@@ -1613,7 +1646,7 @@ generate_parentside_target_query(struct module_qstate* qstate,
8670e0
 {
8670e0
 	struct module_qstate* subq;
8670e0
 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate, 
8670e0
-		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
8670e0
+		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0))
8670e0
 		return 0;
8670e0
 	if(subq) {
8670e0
 		struct iter_qstate* subiq = 
8670e0
@@ -1664,7 +1697,7 @@ generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 {
8670e0
 	struct module_qstate* subq;
8670e0
 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate, 
8670e0
-		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
8670e0
+		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0))
8670e0
 		return 0;
8670e0
 	log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
8670e0
 	return 1;
8670e0
@@ -1703,6 +1736,14 @@ query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 			"number of glue fetches %d", s, iq->target_count[1]);
8670e0
 		return 0;
8670e0
 	}
8670e0
+    if(iq->dp_target_count > MAX_DP_TARGET_COUNT) {
8670e0
+        char s[LDNS_MAX_DOMAINLEN+1];
8670e0
+        dname_str(qstate->qinfo.qname, s);
8670e0
+        verbose(VERB_QUERY, "request %s has exceeded the maximum "
8670e0
+            "number of glue fetches %d to a single delegation point",
8670e0
+            s, iq->dp_target_count);
8670e0
+        return 0;
8670e0
+    }
8670e0
 
8670e0
 	iter_mark_cycle_targets(qstate, iq->dp);
8670e0
 	missing = (int)delegpt_count_missing_targets(iq->dp);
8670e0
@@ -1815,7 +1856,7 @@ processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 			for(a = p->target_list; a; a=a->next_target) {
8670e0
 				(void)delegpt_add_addr(iq->dp, qstate->region,
8670e0
 					&a->addr, a->addrlen, a->bogus,
8670e0
-					a->lame, a->tls_auth_name);
8670e0
+					a->lame, a->tls_auth_name, NULL);
8670e0
 			}
8670e0
 		}
8670e0
 		iq->dp->has_parent_side_NS = 1;
8670e0
@@ -1832,6 +1873,7 @@ processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 			iq->refetch_glue = 1;
8670e0
 			iq->query_restart_count++;
8670e0
 			iq->sent_count = 0;
8670e0
+            iq->dp_target_count = 0;
8670e0
 			if(qstate->env->cfg->qname_minimisation)
8670e0
 				iq->minimisation_state = INIT_MINIMISE_STATE;
8670e0
 			return next_state(iq, INIT_REQUEST_STATE);
8670e0
@@ -1986,7 +2028,7 @@ processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
8670e0
 		iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
8670e0
 	if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len, 
8670e0
 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
8670e0
-		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
8670e0
+		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0, 0)) {
8670e0
 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
8670e0
 	}
8670e0
 
8670e0
@@ -2039,7 +2081,14 @@ processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 			"number of sends with %d", iq->sent_count);
8670e0
 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
8670e0
 	}
8670e0
-	
8670e0
+    if(iq->target_count && iq->target_count[2] > MAX_TARGET_NX) {
8670e0
+        verbose(VERB_QUERY, "request has exceeded the maximum "
8670e0
+            " number of nxdomain nameserver lookups with %d",
8670e0
+            iq->target_count[2]);
8670e0
+        errinf(qstate, "exceeded the maximum nameserver nxdomains");
8670e0
+        return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
8670e0
+    }
8670e0
+
8670e0
 	/* Make sure we have a delegation point, otherwise priming failed
8670e0
 	 * or another failure occurred */
8670e0
 	if(!iq->dp) {
8670e0
@@ -2139,12 +2188,41 @@ processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 				iq->qinfo_out.qtype, iq->qinfo_out.qclass, 
8670e0
 				qstate->query_flags, qstate->region, 
8670e0
 				qstate->env->scratch, 0);
8670e0
-			if(msg && msg->rep->an_numrrsets == 0
8670e0
-				&& FLAGS_GET_RCODE(msg->rep->flags) == 
8670e0
+            if(msg && FLAGS_GET_RCODE(msg->rep->flags) ==
8670e0
 				LDNS_RCODE_NOERROR)
8670e0
 				/* no need to send query if it is already 
8670e0
-				 * cached as NOERROR/NODATA */
8670e0
+				 * cached as NOERROR */
8670e0
 				return 1;
8670e0
+            if(msg && FLAGS_GET_RCODE(msg->rep->flags) ==
8670e0
+                LDNS_RCODE_NXDOMAIN &&
8670e0
+                qstate->env->need_to_validate &&
8670e0
+                qstate->env->cfg->harden_below_nxdomain) {
8670e0
+                if(msg->rep->security == sec_status_secure) {
8670e0
+                    iq->response = msg;
8670e0
+                    return final_state(iq);
8670e0
+                }
8670e0
+                if(msg->rep->security == sec_status_unchecked) {
8670e0
+                    struct module_qstate* subq = NULL;
8670e0
+                    if(!generate_sub_request(
8670e0
+                        iq->qinfo_out.qname,
8670e0
+                        iq->qinfo_out.qname_len,
8670e0
+                        iq->qinfo_out.qtype,
8670e0
+                        iq->qinfo_out.qclass,
8670e0
+                        qstate, id, iq,
8670e0
+                        INIT_REQUEST_STATE,
8670e0
+                        FINISHED_STATE, &subq, 1, 1))
8670e0
+                        verbose(VERB_ALGO,
8670e0
+                        "could not validate NXDOMAIN "
8670e0
+                        "response");
8670e0
+                }
8670e0
+            }
8670e0
+            if(msg && FLAGS_GET_RCODE(msg->rep->flags) ==
8670e0
+                LDNS_RCODE_NXDOMAIN) {
8670e0
+                /* return and add a label in the next
8670e0
+                 * minimisation iteration.
8670e0
+                 */
8670e0
+                return 1;
8670e0
+            }
8670e0
 		}
8670e0
 	}
8670e0
 	if(iq->minimisation_state == SKIP_MINIMISE_STATE) {
8670e0
@@ -2219,6 +2297,8 @@ processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 	 * generated query will immediately be discarded due to depth and
8670e0
 	 * that servfail is cached, which is not good as opportunism goes. */
8670e0
 	if(iq->depth < ie->max_dependency_depth
8670e0
+        && iq->num_target_queries == 0
8670e0
+        && (!iq->target_count || iq->target_count[2]==0)
8670e0
 		&& iq->sent_count < TARGET_FETCH_STOP) {
8670e0
 		tf_policy = ie->target_fetch_policy[iq->depth];
8670e0
 	}
8670e0
@@ -2256,6 +2336,7 @@ processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 			iq->num_current_queries++; /* RespState decrements it*/
8670e0
 			iq->referral_count++; /* make sure we don't loop */
8670e0
 			iq->sent_count = 0;
8670e0
+            iq->dp_target_count = 0;
8670e0
 			iq->state = QUERY_RESP_STATE;
8670e0
 			return 1;
8670e0
 		}
8670e0
@@ -2341,6 +2422,7 @@ processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 					iq->num_current_queries++; /* RespState decrements it*/
8670e0
 					iq->referral_count++; /* make sure we don't loop */
8670e0
 					iq->sent_count = 0;
8670e0
+                    iq->dp_target_count = 0;
8670e0
 					iq->state = QUERY_RESP_STATE;
8670e0
 					return 1;
8670e0
 				}
8670e0
@@ -2607,7 +2689,8 @@ processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 				/* Make subrequest to validate intermediate
8670e0
 				 * NXDOMAIN if harden-below-nxdomain is
8670e0
 				 * enabled. */
8670e0
-				if(qstate->env->cfg->harden_below_nxdomain) {
8670e0
+				if(qstate->env->cfg->harden_below_nxdomain &&
8670e0
+                    qstate->env->need_to_validate) {
8670e0
 					struct module_qstate* subq = NULL;
8670e0
 					log_query_info(VERB_QUERY,
8670e0
 						"schedule NXDOMAIN validation:",
8670e0
@@ -2619,7 +2702,7 @@ processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 						iq->response->qinfo.qclass,
8670e0
 						qstate, id, iq,
8670e0
 						INIT_REQUEST_STATE,
8670e0
-						FINISHED_STATE, &subq, 1))
8670e0
+						FINISHED_STATE, &subq, 1, 1))
8670e0
 						verbose(VERB_ALGO,
8670e0
 						"could not validate NXDOMAIN "
8670e0
 						"response");
8670e0
@@ -2702,6 +2785,7 @@ processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 		/* Count this as a referral. */
8670e0
 		iq->referral_count++;
8670e0
 		iq->sent_count = 0;
8670e0
+        iq->dp_target_count = 0;
8670e0
 		/* see if the next dp is a trust anchor, or a DS was sent
8670e0
 		 * along, indicating dnssec is expected for next zone */
8670e0
 		iq->dnssec_expected = iter_indicates_dnssec(qstate->env, 
8670e0
@@ -2776,6 +2860,7 @@ processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
8670e0
 		iq->dsns_point = NULL;
8670e0
 		iq->auth_zone_response = 0;
8670e0
 		iq->sent_count = 0;
8670e0
+        iq->dp_target_count = 0;
8670e0
 		if(iq->minimisation_state != MINIMISE_STATE)
8670e0
 			/* Only count as query restart when it is not an extra
8670e0
 			 * query as result of qname minimisation. */
8670e0
@@ -2964,7 +3049,7 @@ processPrimeResponse(struct module_qstate* qstate, int id)
8670e0
 		if(!generate_sub_request(qstate->qinfo.qname, 
8670e0
 			qstate->qinfo.qname_len, qstate->qinfo.qtype,
8670e0
 			qstate->qinfo.qclass, qstate, id, iq,
8670e0
-			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
8670e0
+			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1, 0)) {
8670e0
 			verbose(VERB_ALGO, "could not generate prime check");
8670e0
 		}
8670e0
 		generate_a_aaaa_check(qstate, iq, id);
8670e0
@@ -2992,6 +3077,7 @@ static void
8670e0
 processTargetResponse(struct module_qstate* qstate, int id,
8670e0
 	struct module_qstate* forq)
8670e0
 {
8670e0
+    struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
8670e0
 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
8670e0
 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
8670e0
 	struct ub_packed_rrset_key* rrset;
8670e0
@@ -3029,7 +3115,7 @@ processTargetResponse(struct module_qstate* qstate, int id,
8670e0
 		log_rrset_key(VERB_ALGO, "add parentside glue to dp", 
8670e0
 			iq->pside_glue);
8670e0
 		if(!delegpt_add_rrset(foriq->dp, forq->region, 
8670e0
-			iq->pside_glue, 1))
8670e0
+			iq->pside_glue, 1, NULL))
8670e0
 			log_err("out of memory adding pside glue");
8670e0
 	}
8670e0
 
8670e0
@@ -3040,6 +3126,7 @@ processTargetResponse(struct module_qstate* qstate, int id,
8670e0
 	 * response type was ANSWER. */
8670e0
 	rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
8670e0
 	if(rrset) {
8670e0
+        int additions = 0;
8670e0
 		/* if CNAMEs have been followed - add new NS to delegpt. */
8670e0
 		/* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
8670e0
 		if(!delegpt_find_ns(foriq->dp, rrset->rk.dname, 
8670e0
@@ -3051,13 +3138,23 @@ processTargetResponse(struct module_qstate* qstate, int id,
8670e0
 		}
8670e0
 		/* if dpns->lame then set the address(es) lame too */
8670e0
 		if(!delegpt_add_rrset(foriq->dp, forq->region, rrset, 
8670e0
-			dpns->lame))
8670e0
+			dpns->lame, &additions))
8670e0
 			log_err("out of memory adding targets");
8670e0
+        if(!additions) {
8670e0
+            /* no new addresses, increase the nxns counter, like
8670e0
+             * this could be a list of wildcards with no new
8670e0
+             * addresses */
8670e0
+            target_count_increase_nx(foriq, 1);
8670e0
+        }
8670e0
 		verbose(VERB_ALGO, "added target response");
8670e0
 		delegpt_log(VERB_ALGO, foriq->dp);
8670e0
 	} else {
8670e0
 		verbose(VERB_ALGO, "iterator TargetResponse failed");
8670e0
+        delegpt_mark_neg(dpns, qstate->qinfo.qtype);
8670e0
 		dpns->resolved = 1; /* fail the target */
8670e0
+        if((dpns->got4 == 2 || !ie->supports_ipv4) &&
8670e0
+            (dpns->got6 == 2 || !ie->supports_ipv6))
8670e0
+            target_count_increase_nx(foriq, 1);
8670e0
 	}
8670e0
 }
8670e0
 
8670e0
@@ -3228,7 +3325,7 @@ processCollectClass(struct module_qstate* qstate, int id)
8670e0
 				qstate->qinfo.qname_len, qstate->qinfo.qtype,
8670e0
 				c, qstate, id, iq, INIT_REQUEST_STATE,
8670e0
 				FINISHED_STATE, &subq, 
8670e0
-				(int)!(qstate->query_flags&BIT_CD))) {
8670e0
+				(int)!(qstate->query_flags&BIT_CD), 0)) {
8670e0
 				return error_response(qstate, id, 
8670e0
 					LDNS_RCODE_SERVFAIL);
8670e0
 			}
8670e0
diff --git a/iterator/iterator.h b/iterator/iterator.h
8670e0
index 67ffeb1..4b325b5 100644
8670e0
--- a/iterator/iterator.h
8670e0
+++ b/iterator/iterator.h
8670e0
@@ -55,6 +55,11 @@ struct rbtree_type;
8670e0
 
8670e0
 /** max number of targets spawned for a query and its subqueries */
8670e0
 #define MAX_TARGET_COUNT	64
8670e0
+/** max number of target lookups per qstate, per delegation point */
8670e0
+#define MAX_DP_TARGET_COUNT    16
8670e0
+/** max number of nxdomains allowed for target lookups for a query and
8670e0
+ * its subqueries */
8670e0
+#define MAX_TARGET_NX      5
8670e0
 /** max number of query restarts. Determines max number of CNAME chain. */
8670e0
 #define MAX_RESTART_COUNT       8
8670e0
 /** max number of referrals. Makes sure resolver does not run away */
8670e0
@@ -305,9 +310,14 @@ struct iter_qstate {
8670e0
 	int sent_count;
8670e0
 	
8670e0
 	/** number of target queries spawned in [1], for this query and its
8670e0
-	 * subqueries, the malloced-array is shared, [0] refcount. */
8670e0
+	 * subqueries, the malloced-array is shared, [0] refcount.
8670e0
+	 * in [2] the number of nxdomains is counted. */
8670e0
 	int* target_count;
8670e0
 
8670e0
+    /** number of target lookups per delegation point. Reset to 0 after
8670e0
+     * receiving referral answer. Not shared with subqueries. */
8670e0
+    int dp_target_count;
8670e0
+
8670e0
 	/** if true, already tested for ratelimiting and passed the test */
8670e0
 	int ratelimit_ok;
8670e0
 
8670e0
diff --git a/services/cache/dns.c b/services/cache/dns.c
8670e0
index 35adc35..23ec68e 100644
8670e0
--- a/services/cache/dns.c
8670e0
+++ b/services/cache/dns.c
8670e0
@@ -271,7 +271,7 @@ find_add_addrs(struct module_env* env, uint16_t qclass,
8670e0
 		akey = rrset_cache_lookup(env->rrset_cache, ns->name, 
8670e0
 			ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
8670e0
 		if(akey) {
8670e0
-			if(!delegpt_add_rrset_A(dp, region, akey, 0)) {
8670e0
+			if(!delegpt_add_rrset_A(dp, region, akey, 0, NULL)) {
8670e0
 				lock_rw_unlock(&akey->entry.lock);
8670e0
 				return 0;
8670e0
 			}
8670e0
@@ -291,7 +291,7 @@ find_add_addrs(struct module_env* env, uint16_t qclass,
8670e0
 		akey = rrset_cache_lookup(env->rrset_cache, ns->name, 
8670e0
 			ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
8670e0
 		if(akey) {
8670e0
-			if(!delegpt_add_rrset_AAAA(dp, region, akey, 0)) {
8670e0
+			if(!delegpt_add_rrset_AAAA(dp, region, akey, 0, NULL)) {
8670e0
 				lock_rw_unlock(&akey->entry.lock);
8670e0
 				return 0;
8670e0
 			}
8670e0
@@ -325,7 +325,8 @@ cache_fill_missing(struct module_env* env, uint16_t qclass,
8670e0
 		akey = rrset_cache_lookup(env->rrset_cache, ns->name, 
8670e0
 			ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
8670e0
 		if(akey) {
8670e0
-			if(!delegpt_add_rrset_A(dp, region, akey, ns->lame)) {
8670e0
+			if(!delegpt_add_rrset_A(dp, region, akey, ns->lame,
8670e0
+                NULL)) {
8670e0
 				lock_rw_unlock(&akey->entry.lock);
8670e0
 				return 0;
8670e0
 			}
8670e0
@@ -345,7 +346,8 @@ cache_fill_missing(struct module_env* env, uint16_t qclass,
8670e0
 		akey = rrset_cache_lookup(env->rrset_cache, ns->name, 
8670e0
 			ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
8670e0
 		if(akey) {
8670e0
-			if(!delegpt_add_rrset_AAAA(dp, region, akey, ns->lame)) {
8670e0
+			if(!delegpt_add_rrset_AAAA(dp, region, akey, ns->lame,
8670e0
+                NULL)) {
8670e0
 				lock_rw_unlock(&akey->entry.lock);
8670e0
 				return 0;
8670e0
 			}
8670e0
diff --git a/util/data/dname.c b/util/data/dname.c
8670e0
index c7360f7..b744f06 100644
8670e0
--- a/util/data/dname.c
8670e0
+++ b/util/data/dname.c
8670e0
@@ -231,17 +231,28 @@ int
8670e0
 dname_pkt_compare(sldns_buffer* pkt, uint8_t* d1, uint8_t* d2)
8670e0
 {
8670e0
 	uint8_t len1, len2;
8670e0
+    int count1 = 0, count2 = 0;
8670e0
 	log_assert(pkt && d1 && d2);
8670e0
 	len1 = *d1++;
8670e0
 	len2 = *d2++;
8670e0
 	while( len1 != 0 || len2 != 0 ) {
8670e0
 		/* resolve ptrs */
8670e0
 		if(LABEL_IS_PTR(len1)) {
8670e0
+            if((size_t)PTR_OFFSET(len1, *d1)
8670e0
+                >= sldns_buffer_limit(pkt))
8670e0
+                return -1;
8670e0
+            if(count1++ > MAX_COMPRESS_PTRS)
8670e0
+                return -1;
8670e0
 			d1 = sldns_buffer_at(pkt, PTR_OFFSET(len1, *d1));
8670e0
 			len1 = *d1++;
8670e0
 			continue;
8670e0
 		}
8670e0
 		if(LABEL_IS_PTR(len2)) {
8670e0
+            if((size_t)PTR_OFFSET(len2, *d2)
8670e0
+                >= sldns_buffer_limit(pkt))
8670e0
+                return 1;
8670e0
+            if(count2++ > MAX_COMPRESS_PTRS)
8670e0
+                return 1;
8670e0
 			d2 = sldns_buffer_at(pkt, PTR_OFFSET(len2, *d2));
8670e0
 			len2 = *d2++;
8670e0
 			continue;
8670e0
@@ -300,12 +311,19 @@ dname_pkt_hash(sldns_buffer* pkt, uint8_t* dname, hashvalue_type h)
8670e0
 	uint8_t labuf[LDNS_MAX_LABELLEN+1];
8670e0
 	uint8_t lablen;
8670e0
 	int i;
8670e0
+    int count = 0;
8670e0
 
8670e0
 	/* preserve case of query, make hash label by label */
8670e0
 	lablen = *dname++;
8670e0
 	while(lablen) {
8670e0
 		if(LABEL_IS_PTR(lablen)) {
8670e0
 			/* follow pointer */
8670e0
+            if((size_t)PTR_OFFSET(lablen, *dname)
8670e0
+                >= sldns_buffer_limit(pkt))
8670e0
+                return h;
8670e0
+            if(count++ > MAX_COMPRESS_PTRS)
8670e0
+                return h;
8670e0
+
8670e0
 			dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
8670e0
 			lablen = *dname++;
8670e0
 			continue;
8670e0
@@ -333,6 +351,9 @@ void dname_pkt_copy(sldns_buffer* pkt, uint8_t* to, uint8_t* dname)
8670e0
 	while(lablen) {
8670e0
 		if(LABEL_IS_PTR(lablen)) {
8670e0
 			/* follow pointer */
8670e0
+            if((size_t)PTR_OFFSET(lablen, *dname)
8670e0
+                >= sldns_buffer_limit(pkt))
8670e0
+                return;
8670e0
 			dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
8670e0
 			lablen = *dname++;
8670e0
 			continue;
8670e0
@@ -357,6 +378,7 @@ void dname_pkt_copy(sldns_buffer* pkt, uint8_t* to, uint8_t* dname)
8670e0
 void dname_print(FILE* out, struct sldns_buffer* pkt, uint8_t* dname)
8670e0
 {
8670e0
 	uint8_t lablen;
8670e0
+    int count = 0;
8670e0
 	if(!out) out = stdout;
8670e0
 	if(!dname) return;
8670e0
 
8670e0
@@ -370,6 +392,15 @@ void dname_print(FILE* out, struct sldns_buffer* pkt, uint8_t* dname)
8670e0
 				fputs("??compressionptr??", out);
8670e0
 				return;
8670e0
 			}
8670e0
+            if((size_t)PTR_OFFSET(lablen, *dname)
8670e0
+                >= sldns_buffer_limit(pkt)) {
8670e0
+                fputs("??compressionptr??", out);
8670e0
+                return;
8670e0
+            }
8670e0
+            if(count++ > MAX_COMPRESS_PTRS) {
8670e0
+                fputs("??compressionptr??", out);
8670e0
+                return;
8670e0
+            }
8670e0
 			dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
8670e0
 			lablen = *dname++;
8670e0
 			continue;
8670e0
diff --git a/util/data/msgparse.c b/util/data/msgparse.c
8670e0
index 13cad8a..c8a5384 100644
8670e0
--- a/util/data/msgparse.c
8670e0
+++ b/util/data/msgparse.c
8670e0
@@ -55,7 +55,11 @@ smart_compare(sldns_buffer* pkt, uint8_t* dnow,
8670e0
 {
8670e0
 	if(LABEL_IS_PTR(*dnow)) {
8670e0
 		/* ptr points to a previous dname */
8670e0
-		uint8_t* p = sldns_buffer_at(pkt, PTR_OFFSET(dnow[0], dnow[1]));
8670e0
+       uint8_t* p;
8670e0
+       if((size_t)PTR_OFFSET(dnow[0], dnow[1])
8670e0
+           >= sldns_buffer_limit(pkt))
8670e0
+           return -1;
8670e0
+       p = sldns_buffer_at(pkt, PTR_OFFSET(dnow[0], dnow[1]));
8670e0
 		if( p == dprfirst || p == dprlast )
8670e0
 			return 0;
8670e0
 		/* prev dname is also a ptr, both ptrs are the same. */