goto handle_it;
}
- if((cookie = edns_list_get_option(edns.opt_list_in, 10 /*@TODO change to cookie with downstream merge*/))) {
- /* verify this is a 'complete cookie' (RFC9018) with the length */
- if (cookie->opt_len == 24) {
- /* store the complete cookie in the infra_cache */
+ /* handle the upstream response cookie */
+ if((cookie = edns_list_get_option(edns.opt_list_in, LDNS_EDNS_COOKIE))) {
+ /* verify this is a 'complete cookie' (RFC9018) with the length and
+ * store the complete cookie in the infra_cache */
+ if (cookie->opt_len == 24 &&
infra_set_server_cookie(qstate->env->infra_cache,
&qstate->reply->addr, qstate->reply->addrlen,
- iq->dp->name, iq->dp->namelen, cookie);
-
+ iq->dp->name, iq->dp->namelen, cookie)) {
+ /* log_hex() uses the verbosity levels of verbose() */
log_hex("complete cookie: ", cookie->opt_data,
cookie->opt_len);
} else {
// @TODO just log error? set state to COOKIE_NOT_SUPPORTED?
+ log_info("upstream response server cookie is not added to cache;"
+ "dropping response");
+ goto handle_it;
}
}
-
/* Copy the edns options we may got from the back end */
if(edns.opt_list_in) {
qstate->edns_opts_back_in = edns_opt_copy_region(edns.opt_list_in,
for (i = 0; i < 8; i++) {
cookie[i] = ub_random_max(infra->random_state,
- 255); // @TODO is this correct? macro-ify?
+ 255); // @TODO is 255 correct? macro-ify it?
}
return &data->cookie;
}
-void
+int
infra_set_server_cookie(struct infra_cache* infra, struct sockaddr_storage* addr,
socklen_t addrlen, uint8_t* name, size_t namelen, struct edns_option* cookie)
{
data = (struct infra_data*) e->data;
- /* check if we already know the complete cookie */
- if (data->cookie.state == SERVER_COOKIE_UNKNOWN) {
+ /* check if we already know the complete cookie and that the client cookie
+ * section matches the client cookie we sent */
+ if (data->cookie.state == SERVER_COOKIE_UNKNOWN &&
+ memcmp(data->cookie.data.components.client,
+ cookie->opt_data+4, 8)) {
memcpy(data->cookie.data.complete, cookie->opt_data, 24);
data->cookie.state = SERVER_COOKIE_LEARNED;
- }
- else {
+ return 1;
+ } else {
lock_rw_unlock(&e->lock);
+ return 0;
}
}
/**
* Find the cookie entry in the cache and update it with to make a complete
- * cookie (RFC9018). This function assumes that the cookie param contains a
- * complete cookie with a length of 24 bytes. It also assumes that a previous
+ * cookie (RFC9018). This function asserts that the cookie param contains a
+ * complete cookie with a length of 24 bytes. It also asserts that a previous
* entry in the cache already exists, as it will update the entry.
* @param infra: infrastructure cache.
* @param addr: host address.
* @param namelen: length of name
* @param timenow: what time it is now.
* @param cookie: the EDNS cookie option we want to store.
+ * @return true if the complete cookie is stored, and false if it is not
*/
-void infra_set_server_cookie(struct infra_cache* infra, struct sockaddr_storage* addr,
+int infra_set_server_cookie(struct infra_cache* infra, struct sockaddr_storage* addr,
socklen_t addrlen, uint8_t* name, size_t namelen, struct edns_option* cookie);
if (cookie->state == SERVER_COOKIE_LEARNED) {
/* We known the complete cookie, so we attach it */
- edns_opt_list_append(&per_upstream_opt_list, 10, /* @TODO 10 == COOKIE */
+ edns_opt_list_append(&per_upstream_opt_list, LDNS_EDNS_COOKIE,
24, cookie->data.complete, region);
} else if (cookie->state == SERVER_COOKIE_UNKNOWN) {
/* We know just client cookie, so we attach it */
- edns_opt_list_append(&per_upstream_opt_list, 10, /* @TODO 10 == COOKIE */
+ edns_opt_list_append(&per_upstream_opt_list, LDNS_EDNS_COOKIE,
8, cookie->data.components.client, region);
} /* We ignore COOKIE_NOT_SUPPORTED */
LDNS_EDNS_DHU = 6, /* RFC6975 */
LDNS_EDNS_N3U = 7, /* RFC6975 */
LDNS_EDNS_CLIENT_SUBNET = 8, /* RFC7871 */
+ LDNS_EDNS_COOKIE = 10, /* RFC7873 */
LDNS_EDNS_KEEPALIVE = 11, /* draft-ietf-dnsop-edns-tcp-keepalive*/
LDNS_EDNS_PADDING = 12, /* RFC7830 */
LDNS_EDNS_EDE = 15, /* RFC8914 */
{ 6, "DHU" },
{ 7, "N3U" },
{ 8, "edns-client-subnet" },
+ { 10, "Cookie" },
{ 11, "edns-tcp-keepalive"},
{ 12, "Padding" },
{ 15, "EDE"},