Skip to content

fix(gateway): reply with cookie when rate limit is hit - #9657

Merged
thomaseizinger merged 2 commits into
mainfrom
fix/reply-with-cookie-under-load
Jun 24, 2025
Merged

fix(gateway): reply with cookie when rate limit is hit#9657
thomaseizinger merged 2 commits into
mainfrom
fix/reply-with-cookie-under-load

Conversation

@thomaseizinger

Copy link
Copy Markdown
Member

WireGuard implements a rate-limit mechanism when the number of handshake initiations increases a certain limit. This is important because handshakes involve asymmetric cryptography and are cryptographically expensive. To prevent DoS attacks where other peers repeatedly ask for new handshakes, the rate limiter implements a cookie mechanism where - when under load - the remote peer needs to include a given cookie in new handshakes. This cookie is tied to the peer's IP address to prevent it from being reused by other peers.

Up until now, we have not been passing the sender's IP address to boringtun and therefore, the only option when the rate limit was hit was to error with UnderLoad.

By passing the source IP of the packet, boringtun can engage in the cookie-reply mechanism and therefore avoid the UnderLoad error.

Resolves: #9643

@vercel

vercel Bot commented Jun 24, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
firezone ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2025 9:37am

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request modifies the rate-limiting mechanism in the handshake process by incorporating the sender’s IP address, enabling the cookie reply mechanism during heavy load instead of an UnderLoad error.

  • Import std::net::IpAddr to use IP address types.
  • Update decapsulate to accept a new IP parameter and pass it to decapsulate_at.
Comments suppressed due to low confidence (3)

rust/connlib/snownet/src/node.rs:2108

  • Consider adding a doc comment for the new 'src' parameter to explain that it represents the sender's IP used for the cookie reply mechanism.
        src: IpAddr,

rust/connlib/snownet/src/node.rs:2108

  • [nitpick] Consider renaming the parameter 'src' to 'source_ip' for improved clarity regarding its purpose.
        src: IpAddr,

rust/connlib/snownet/src/node.rs:2119

  • [nitpick] A brief comment explaining the reason for passing Some(src) (i.e., to enable the cookie reply mechanism instead of erroring on UnderLoad) could improve code clarity.
            .decapsulate_at(Some(src), packet, ip_packet.buf(), now)

@thomaseizinger
thomaseizinger enabled auto-merge June 24, 2025 09:34

@jamilbk jamilbk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done. Does this still work for multiple clients behind a NAT that share the same source IP?

@thomaseizinger
thomaseizinger added this pull request to the merge queue Jun 24, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 24, 2025
@thomaseizinger

Copy link
Copy Markdown
Member Author

Nicely done. Does this still work for multiple clients behind a NAT that share the same source IP?

We are following the WireGuard paper here, I'd assume they considered that case in the design.

I think it should work because each tunnel has its own state.

@thomaseizinger
thomaseizinger added this pull request to the merge queue Jun 24, 2025
Merged via the queue into main with commit 4be73da Jun 24, 2025
@thomaseizinger
thomaseizinger deleted the fix/reply-with-cookie-under-load branch June 24, 2025 11:47
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.

Tunnel error: Failed to decapsulate: Failed to decapsulate: UnderLoad

3 participants