Repository navigation
Expand file tree
/
Copy pathprocess_cachehit.go
More file actions
258 lines (222 loc) · 7.78 KB
/
Copy pathprocess_cachehit.go
File metadata and controls
258 lines (222 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
File: process_cachehit.go
Version: 1.24.0 (Split)
Last Updated: 15-Nov-2026 14:45 CET
Description:
Cache-hit serving stage for the sdproxy resolution pipeline.
Owns everything between "the cache had this answer" and "the client has it":
cache-key derivation, the lookup itself, per-client answer ordering, the
post-cache policy re-checks, and the response write.
Changes:
1.24.0 - [FEAT] Added `qc.enforceRFCComplianceResponseCheck(poolMsg, true)` and
`qc.enforceRFCComplianceCheck()` on non-synthetic cache hits so cached
responses are validated for RFC compliance and CNAME indirection.
1.23.0 - [SECURITY/FIX] Removed `CacheUpdateOrder` from `serveFromCache` so
`CacheGet`'s decayed per-request TTLs are never written back into the
shared cache entry (`item.packed`), which previously caused quadratic
TTL decay on repeated cache hits when `answer_sort: "ip-sort"` was
enabled.
1.22.0 - [SECURITY/FIX] Restricted `CacheUpdateOrder` in `serveFromCache` to
`cfg.Cache.AnswerSort == "ip-sort"`.
1.21.0 - [PERF/FIX] Persisted sorted answer ordering via `CacheUpdateOrder` in
`serveFromCache`.
1.20.0 - [FEAT] Passed the incoming listener address (`localAddrIP(qc.w)`) into
`applyAnswerSort` so `answer_sort: "proximity"` includes the active
listener socket IP when computing address-space distance.
1.19.0 - [SECURITY/FIX] Cleared `poolMsg.AuthenticatedData = false` in `serveFromCache`
when the querying client specified neither `DO=1` (`!qc.doBit`) nor
`AD=1` (`!qc.r.AuthenticatedData`), per RFC 6840 §5.8.
1.18.0 - [SECURITY/FIX] Aligned `deriveCacheKey` with `group.RequireDNSSECAD`.
1.17.0 - [FEAT] Added Simple Logging Mode (`logSimple` / `logQuerySimple`)
support on cache hits.
1.16.0 - [SECURITY/FIX] Passed `qc.clientAddr` and `qc.group` to `enforceUDPDefenses`.
1.15.0 - [FEAT/FIX] Added `qc.enforceForSaleResponseCheck(poolMsg)` on non-synthetic
cache hits.
*/
package main
import (
"fmt"
"log"
"net/netip"
"github.com/miekg/dns"
)
func (qc *queryCtx) deriveCacheKey() {
qc.group = effectiveUpstreamGroup(qc.route.routeName)
clientNameForCache := ""
if qc.group != nil && qc.group.HasClientName {
clientNameForCache = qc.clientName
}
ecsForCache := ""
var clientSentECS bool
var clientECSStr string
if qc.ecsAddr.IsValid() {
clientSentECS = true
if qc.ecsPrefix.IsValid() {
clientECSStr = qc.ecsPrefix.String()
} else {
clientECSStr = qc.ecsAddr.String()
}
}
if qc.group != nil {
if qc.group.ECSAction == "add" && qc.clientAddr.IsValid() {
var m int
if qc.clientAddr.Is4() {
m = qc.group.ECSV4Mask
} else {
m = qc.group.ECSV6Mask
}
prefix, _ := qc.clientAddr.Prefix(m)
ecsForCache = prefix.Masked().String()
} else if qc.group.ECSAction == "pass" && clientSentECS {
if clientECSStr != "" {
ecsForCache = clientECSStr
} else {
ecsForCache = "passed-ecs"
}
}
}
effectiveDo := qc.doBit || (qc.group != nil && qc.group.RequireDNSSECAD)
qc.cacheKey = DNSCacheKey{
Name: qc.qNameTrimmed,
ClientName: clientNameForCache,
ECS: ecsForCache,
Qtype: qc.q.Qtype,
Qclass: qc.q.Qclass,
RouteIdx: qc.route.routeIdx,
DoBit: effectiveDo,
CdBit: qc.r.CheckingDisabled,
BypassGlobal: qc.bypassGlobal,
BypassPolicies: qc.bypassPolicies,
BypassLocal: qc.route.bypassLocal,
RecursionDesired: qc.r.RecursionDesired,
}
}
func (qc *queryCtx) serveFromCache() bool {
poolMsg := msgPool.Get().(*dns.Msg)
*poolMsg = dns.Msg{}
isStale, isPrefetch, cacheOK, _ := CacheGet(qc.cacheKey, qc.q.Name, poolMsg)
if !cacheOK {
msgPool.Put(poolMsg)
return false
}
defer msgPool.Put(poolMsg)
if !poolMsg.Authoritative {
if qc.enforceForSaleResponseCheck(poolMsg) {
return true
}
if qc.enforceForSaleCheck() {
return true
}
if qc.enforceParkedResponseCheck(poolMsg) {
return true
}
if qc.enforceParkedCheck() {
return true
}
if qc.enforceRFCComplianceResponseCheck(poolMsg, true) {
return true
}
if qc.enforceRFCComplianceCheck() {
return true
}
}
IncrCacheHit()
if !qc.doBit && qc.q.Qtype != dns.TypeRRSIG && qc.q.Qtype != dns.TypeANY {
poolMsg.Answer = stripRRSIGRecords(poolMsg.Answer)
poolMsg.Ns = stripRRSIGRecords(poolMsg.Ns)
}
if !qc.doBit && !qc.r.AuthenticatedData {
poolMsg.AuthenticatedData = false
}
if cfg.Cache.AnswerSort != "none" && !cacheRotateAnswers {
var lAddr netip.Addr
if cfg.Cache.AnswerSort == "proximity" {
if ip := localAddrIP(qc.w); ip != nil {
if a, ok := netip.AddrFromSlice(ip); ok {
lAddr = a.Unmap()
}
}
}
applyAnswerSort(poolMsg, cfg.Cache.AnswerSort, lAddr)
}
if qc.parentalForcedTTL > 0 {
CapResponseTTL(poolMsg, qc.parentalForcedTTL)
}
poolMsg.Id = qc.originalID
// ── Post-cache policy re-evaluation ───────────────────────────────────
if checkTargetNames(qc.w, qc.r, poolMsg, qc.sk, qc.clientGroup, qc.clientMAC, qc.ednsMAC, qc.clientIP, qc.clientAddr, qc.ecsAddr,
qc.clientName, qc.clientNameLower, qc.clientID, qc.protocol, qc.protoEndpoint, qc.sni, qc.sniLower, qc.path, qc.pathLower, qc.localPort,
qc.bypassPolicies, qc.bypassGlobal, qc.originalQName, true) {
return true
}
resp := transformResponse(poolMsg, qc.q.Qtype, qc.doBit, true)
if filterResponseIPs(qc.w, qc.r, resp, qc.sk, qc.clientGroup, qc.clientMAC, qc.ednsMAC, qc.clientIP, qc.clientAddr, qc.ecsAddr,
qc.clientName, qc.clientNameLower, qc.clientID, qc.protocol, qc.protoEndpoint, qc.sni, qc.sniLower, qc.path, qc.pathLower, qc.localPort,
qc.bypassPolicies, qc.bypassGlobal, qc.originalQName, qc.originalQNameTrimmed, true) {
return true
}
if filterRebinding(qc.w, qc.r, resp, qc.clientIP, qc.clientID,
qc.protocol, qc.protoEndpoint, qc.originalQName, qc.originalQNameTrimmed, true) {
return true
}
// ── Telemetry ─────────────────────────────────────────────────────────
isNullIP := false
for _, rr := range resp.Answer {
switch r := rr.(type) {
case *dns.A:
if r.A.IsUnspecified() {
isNullIP = true
break
}
case *dns.AAAA:
if r.AAAA.IsUnspecified() {
isNullIP = true
break
}
}
if isNullIP {
break
}
}
if isNullIP {
RecordBlockEvent(qc.clientIP, qc.originalQNameTrimmed, "Cached NULL-IP ("+qc.route.routeName+")")
}
IncrReturnCode(resp.Rcode, isNullIP)
if resp.Rcode == dns.RcodeNameError {
IncrNXDomain(qc.originalQNameTrimmed)
}
upstreamSize, clientAdvertised := enforceUDPDefenses(qc.r, resp, qc.protocol, qc.clientAddr, qc.group)
qc.w.WriteMsg(resp)
if logQueries {
rcodeStr := RcodeStr(resp.Rcode)
if isNullIP {
rcodeStr += " (NULL-IP)"
}
if logSimple {
if qc.parentalReason == "ACTIVATING_UNTRIGGER" {
qc.scheduleUntriggerExpiry()
}
logQuerySimple(qc.protoEndpoint, qc.clientIP, qc.originalQName, qc.qtypeStr(), rcodeStr, qc.effectivePolicyLabel(), true)
return true
}
var status string
switch {
case isStale:
status = "STALE (revalidating) | " + rcodeStr
case isPrefetch:
status = "CACHE HIT (prefetching) | " + rcodeStr
case resp.Truncated:
status = fmt.Sprintf("CACHE HIT (Truncated TC=1, Upstream:%dB, Client:%dB) | %s",
upstreamSize, clientAdvertised, rcodeStr)
default:
status = "CACHE HIT | " + rcodeStr
}
status = qc.annotateParental(status)
status = qc.annotateAlias(status)
status = qc.annotateECS(status)
log.Printf("[DNS] [%s] %s -> %s %s | ROUTE: %s (%s) | %s",
qc.protocol, qc.clientID, qc.originalQName, qc.qtypeStr(),
qc.route.routeName, qc.route.routeOriginType, status)
}
return true
}