caddy-l4: UDP gets blocked after any unsuccessful matching
I’m currently debugging an OpenVPN matcher, and I’ve noticed a problem with how UDP is handled by caddy-l4.
Symptoms:
- A UDP connection gets blocked / hangs, i.e. no packets are passed for matching, after the first unsuccessful matching.
- If an invalid packet is sent to Caddy after a valid packet, then any valid packets that follow are completely ignored.
- The problem always persists until Caddy is restarted.
Sample config:
{
layer4 {
udp/:53 {
@test dns {
allow apple.com. * *
default_deny
}
route @test {
proxy udp/1.1.1.1:53
}
}
}
}
Replication:
- The problem is related to any UDP packets, but I’ve found DNS to be the easiest way to replicate. Send any non-apple.com DNS request to Caddy running the sample config above, then send apple.com DNS request - it won’t respond. Restart Caddy and send apple.com DNS request - it will respond, then send any non-apple.com DNS request, then send apple.com DNS request again - you will get no response.
- If
fmt.Printf("%x, %d\n", buf[:10], n)is inserted before https://github.com/mholt/caddy-l4/blob/master/layer4/server.go#L109, it never prints anything after unsuccessful matching.
Side problem:
- When I do UDP debugging, Caddy panics on https://github.com/mholt/caddy-l4/blob/master/layer4/server.go#L156 from time to time. I think it depends on how much time has passed between the app stops on a breakpoint and I press the resume button.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (9 by maintainers)
I have never used udp in layer4, but looking at the code a bit, I currently would go with @WeidiDeng’s fix. Since it is closer to the udp code. Properly supporting the matching timeout for udp would likely require a bigger rewrite.
I am for example not sure why the ignoring of timeout errors here even exists. I can not find anything that sets a timeout for udp (the idle timeout is implemented via select & time.After), so before the addition of the matching timeout this branch was probably never executed?
And while @vnxme is right after
compiledRoute.Handlereturns it gives back control of the connection to the caller and thus should probably not leave a deadline set on it. Sadly there does not seam to be an API to restore a previously set deadline. If we always reset it we may also reset a deadline that was set by a future udp rewrite.My solution still needs some works, but it should suffice in this case. The matching timeout is used to control how long the matching goroutine run in this case, by default it’s 3 seconds. It is a whopping 30 seconds for udp though. I thought for a while how to fix it, but one step at a time.
Actually it turns out to be the matchingTimeout set by https://github.com/mholt/caddy-l4/blob/master/layer4/routes.go#L123 that breaks UDP.
As a workaround (and a proof that the timeout is at least related to the problem described above), we can put
_ = conn.SetReadDeadline(time.Time{})as a simple neutralisation command after https://github.com/mholt/caddy-l4/blob/master/layer4/server.go#L147, and it seems to effectively prevent UDP from dying after unsuccessful matching.This read deadline was originally introduced by @ydylla (in https://github.com/mholt/caddy-l4/commit/f04916554cbe94a116aaef90368fd0ac99bce513), and @WeidiDeng was the last one who reworked RouteList.Compile (https://github.com/mholt/caddy-l4/commit/afa78d72257b949486b24fa6f0351381c786a4b3 and https://github.com/mholt/caddy-l4/commit/22a03931ae46d0decc068e8d1ffe83e6187ed812).
I believe there is a straight logic behind this timeout, but I don’t think it was supposed to break UDP. So could you guys, please, take a look as well? Is it possible we can solve the problem by adjusting RouteList.Compile rather than Server.servePacket?
I think the issue described above can be traced to https://github.com/mholt/caddy-l4/commit/6a8be7c4b8acb0c531b6151c94a9cd80894acce1.
@jtackaberry Is there any chance you could look at it?