Skip to content

Send a CONNECTION_CLOSE when conn_new() refuses a DoQ connection - #3373

Merged
DL6ER merged 1 commit into
pi-hole:developmentfrom
010011110:doq-refuse-close
Nov 22, 2026
Merged

DL6ER merged 1 commit into
pi-hole:developmentfrom
010011110:doq-refuse-close

Conversation

@010011110

Copy link
Copy Markdown
Contributor

What does this implement/fix?

when conn_new() refused a DoQ connection (connection caps, source not allowed) it only called SSL_free(). OpenSSL hands the connection out after the server's first flight, so the client finished its handshake, got no CONNECTION_CLOSE and sent its queries into nothing until its own idle timeout instead of failing over

sending the close right away is not enough: before the handshake completes OpenSSL can only send it in an Initial packet, which a client that already sent its Handshake packet cannot decrypt any more (RFC 9001 4.9.1), and freeing the connection lets the client's next Initial create a second connection that gets refused again. under load that lost 4 of 160 refusals at the per-source cap and 12 of 100 at the global cap. a refused connection is now kept until its handshake is complete and then closed with the DoQ code in a 1-RTT packet. at most 16 are kept, each for about 2 s at most, beyond that it falls back to the immediate close

independent of #3372, both touch doq_server.c but merge cleanly. #3234 adds a line at the same place in the thread teardown, whichever lands second keeps both

How to test the change during review

run the two refused-connection tests in dotdoh_server.bats (17th connection from one address, 65th overall), both expect the kept connections open and close code 4

Additional information

Related issue or feature (if applicable): N/A

Pull request in docs with documentation (if applicable): N/A


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)
  6. My change does not modify src/dnsmasq/. That tree is a verbatim copy of upstream dnsmasq and we do not carry anything in it that deviates from upstream. Fixes have to go through the dnsmasq-discuss mailing list first, we merge them once they are in dnsmasq master.

Checklist:

  • The code change is tested and works locally.
  • I based my code and PRs against the repository's development branch.
  • I signed off all commits. Pi-hole enforces the DCO for all contributions
  • I signed all my commits. Pi-hole requires signatures to verify authorship
  • I have read the above and my PR is ready for review.

OpenSSL hands a connection to `SSL_accept_connection()` on the client's
first validated Initial, after it has sent the server's first flight,
so the client completes its handshake and believes the connection is
up. When `conn_new()` then refused it, at the connection caps or for a
source that is not allowed, it only called `SSL_free()`, which discards
the connection without a CONNECTION_CLOSE. The client sent its queries
into it until its idle timer ran out, 30 s later, instead of failing
over.

Sending the close right away is not enough either. Until the handshake
is complete OpenSSL can send it only in an Initial packet, and a client
that has already sent its Handshake packet has dropped its Initial keys
(RFC 9001 Sec. 4.9.1) and cannot read it. Freeing the connection also
drops its connection IDs, so the client's next Initial, which still
carries the Retry token, makes OpenSSL accept a second connection that
is refused the same way. Under load 4 of 160 refused clients at the
per-source cap and 12 of 100 at the global cap never learned about the
refusal, and most of the others got a transport APPLICATION_ERROR
instead of the DoQ code.

`conn_refuse()` therefore keeps a refused connection until its
handshake is complete and then closes it with `DOQ_EXCESSIVE_LOAD` at
the caps, `DOQ_INTERNAL_ERROR` otherwise, which goes out in a 1-RTT
packet with that code. `refused_drive()` advances these connections
from the event loop. At most `DOQ_MAX_REFUSED` (16) connections are
kept, each for at most about 2 s (`DOQ_REFUSE_TIMEOUT_MS` for the
handshake, then `DOQ_CLOSE_TIMEOUT_MS` for the close). A refusal
beyond them closes the connection at once, where the close can be lost
as before; a client opening connections beyond the caps can fill the
table to get there. `doq_shutdown()` takes the connection object and
the code for this.

Signed-off-by: 010011110 <duckenheim@posteo.de>
@010011110
010011110 requested a review from a team as a code owner November 17, 2026 07:59
@DL6ER
DL6ER merged commit d4ea893 into pi-hole:development Nov 22, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants