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

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