Checksum the header and refuse sequence numbers the sender never sent

This commit is contained in:
flamingcow
2026-08-04 20:41:16 -07:00
parent 703039587b
commit 1d54a1a1f6
9 changed files with 241 additions and 35 deletions
+15 -5
View File
@@ -12,6 +12,7 @@ type rxStats struct {
frames atomic.Uint64
bytes atomic.Uint64
badMagic atomic.Uint64
badHdr atomic.Uint64
badLen atomic.Uint64
crcErr atomic.Uint64
rxErrs atomic.Uint64
@@ -99,9 +100,13 @@ func (w *rxWorker) run(done *atomic.Bool) {
}
for i := 0; i < n; i++ {
buf := bufs[i][:int(hdrs[i].len)]
p, ok := parseHeader(buf)
if !ok {
w.stats.badMagic.Add(1)
p, st := parseHeader(buf)
if st != hdrOK {
if st == hdrForeign {
w.stats.badMagic.Add(1)
} else {
w.stats.badHdr.Add(1)
}
continue
}
w.stats.frames.Add(1)
@@ -110,8 +115,13 @@ func (w *rxWorker) run(done *atomic.Bool) {
w.stats.observe(ts, uint64(len(buf)))
}
if int(p.stream) < len(w.streams) {
w.streams[p.stream].observe(p.seq)
// A sequence number the sender never reached got past the header
// checksum, so the frame is damaged whatever its payload says. Counted
// here rather than left to the payload check, which would report the
// same frame twice or, if only the header was hit, not at all.
if int(p.stream) < len(w.streams) && !w.streams[p.stream].observe(p.seq) {
w.stats.badHdr.Add(1)
continue
}
want, ok := w.spec.expectedCRC(p.patIdx, p.payLen)