Skip to content

net: dns: Forward all DNS packets if callback is installed still not functional #107589

Description

@Cristib05

Describe the bug

Issue is already described in #106783, an attempt to fix this was done in #106810, but this was incomplete.

While debugging, I have found out that when dns_header_rcode(dns_msg->msg) == DNS_HEADER_REFUSED or dns_header_ancount(dns_msg->msg) < 1 and *dns_id > 0 (DNS packet), function will jump to quit, and dns_id and query_hash are not even computed.

Regression

  • This is a regression.

Steps to reproduce

No response

Relevant log output

Impact

Annoyance – Minor irritation; no significant impact on usability or functionality.

Environment

No response

Additional Context

No response

Activity

  1. added
    bugThe issue is a bug, or the PR is fixing a bug
    on Apr 20, 2026
  2. rlubos commented on Apr 20, 2026

    @rlubos
    Contributor

    For sure additional check for query_idx is needed before passing the packet to pkt_fw_cb.

    Perhaps some reordering could be applied to dns_validate_msg() (like checking the record count after calculating the hash/query index) to mitigate the issue, however I don't think this can be fully avoided - the hash/index cannot be calculated for malformed packets.

  3. Cristib05 commented on Apr 21, 2026

    @Cristib05
    ContributorAuthor

    I was thinking to do those operations if packet forwarding is enabled, a DNS packet is received (dns_id > 0), outside of dns_validate_msg() function.

  4. Cristib05 commented on Apr 21, 2026

    @Cristib05
    ContributorAuthor

    something like this, in dns_read() function:

    	ret = dns_validate_msg(ctx, &dns_msg, dns_id, &query_idx,
    			       dns_cname, query_hash);
    
    #if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING)
    	if (ctx->pkt_fw_cb != NULL) {
    		if (ret != 0) {
    			if (*dns_id > 0 && dns_unpack_response_query(&dns_msg) == 0 ) {
    				update_query_idx(ctx, &dns_msg, dns_id, &query_idx, query_hash);
    			}
    		}
    		if (query_idx != -1) {
    			ctx->pkt_fw_cb(dns_data, data_len, ctx->queries[query_idx].user_data);
    		}
    	}
    #endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
    
  5. rlubos commented on Apr 21, 2026

    @rlubos
    Contributor

    dns_unpack_response_query

    something like this, in dns_read() function:

    	ret = dns_validate_msg(ctx, &dns_msg, dns_id, &query_idx,
    			       dns_cname, query_hash);
    
    #if defined(CONFIG_DNS_RESOLVER_PACKET_FORWARDING)
    	if (ctx->pkt_fw_cb != NULL) {
    		if (ret != 0) {
    			if (*dns_id > 0 && dns_unpack_response_query(&dns_msg) == 0 ) {
    				update_query_idx(ctx, &dns_msg, dns_id, &query_idx, query_hash);
    			}
    		}
    		if (query_idx != -1) {
    			ctx->pkt_fw_cb(dns_data, data_len, ctx->queries[query_idx].user_data);
    		}
    	}
    #endif /* CONFIG_DNS_RESOLVER_PACKET_FORWARDING */
    

    It seems it could work that way, one caveat though. When dns_validate_msg() fails we don't know whether message size has been validated yet (happens in dns_unpack_response_header()), and dns_unpack_response_query() relies on that, so additional check for msg->msg_size > DNS_MSG_HEADER_SIZE before calling that would be needed I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: NetworkingbugThe issue is a bug, or the PR is fixing a bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions