mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Implement UDP cheksum verification.
Test: - TestIncrementChecksumErrors Fixes #2943 PiperOrigin-RevId: 317348158
This commit is contained in:
@@ -191,6 +191,7 @@ var Metrics = tcpip.Stats{
|
||||
MalformedPacketsReceived: mustCreateMetric("/netstack/udp/malformed_packets_received", "Number of incoming UDP datagrams dropped due to the UDP header being in a malformed state."),
|
||||
PacketsSent: mustCreateMetric("/netstack/udp/packets_sent", "Number of UDP datagrams sent."),
|
||||
PacketSendErrors: mustCreateMetric("/netstack/udp/packet_send_errors", "Number of UDP datagrams failed to be sent."),
|
||||
ChecksumErrors: mustCreateMetric("/netstack/udp/checksum_errors", "Number of UDP datagrams dropped due to bad checksums."),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -313,7 +313,7 @@ func (s *Stack) Statistics(stat interface{}, arg string) error {
|
||||
udp.PacketsSent.Value(), // OutDatagrams.
|
||||
udp.ReceiveBufferErrors.Value(), // RcvbufErrors.
|
||||
0, // Udp/SndbufErrors.
|
||||
0, // Udp/InCsumErrors.
|
||||
udp.ChecksumErrors.Value(), // Udp/InCsumErrors.
|
||||
0, // Udp/IgnoredMulti.
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -1224,6 +1224,9 @@ type UDPStats struct {
|
||||
|
||||
// PacketSendErrors is the number of datagrams failed to be sent.
|
||||
PacketSendErrors *StatCounter
|
||||
|
||||
// ChecksumErrors is the number of datagrams dropped due to bad checksums.
|
||||
ChecksumErrors *StatCounter
|
||||
}
|
||||
|
||||
// Stats holds statistics about the networking stack.
|
||||
@@ -1267,6 +1270,9 @@ type ReceiveErrors struct {
|
||||
// ClosedReceiver is the number of received packets dropped because
|
||||
// of receiving endpoint state being closed.
|
||||
ClosedReceiver StatCounter
|
||||
|
||||
// ChecksumErrors is the number of packets dropped due to bad checksums.
|
||||
ChecksumErrors StatCounter
|
||||
}
|
||||
|
||||
// SendErrors collects packet send errors within the transport layer for
|
||||
|
||||
@@ -1350,6 +1350,24 @@ func (e *endpoint) HandlePacket(r *stack.Route, id stack.TransportEndpointID, pk
|
||||
return
|
||||
}
|
||||
|
||||
// Verify checksum unless RX checksum offload is enabled.
|
||||
// On IPv4, UDP checksum is optional, and a zero value means
|
||||
// the transmitter omitted the checksum generation (RFC768).
|
||||
// On IPv6, UDP checksum is not optional (RFC2460 Section 8.1).
|
||||
if r.Capabilities()&stack.CapabilityRXChecksumOffload == 0 &&
|
||||
(hdr.Checksum() != 0 || r.NetProto == header.IPv6ProtocolNumber) {
|
||||
xsum := r.PseudoHeaderChecksum(ProtocolNumber, hdr.Length())
|
||||
for _, v := range pkt.Data.Views() {
|
||||
xsum = header.Checksum(v, xsum)
|
||||
}
|
||||
if hdr.CalculateChecksum(xsum) != 0xffff {
|
||||
// Checksum Error.
|
||||
e.stack.Stats().UDP.ChecksumErrors.Increment()
|
||||
e.stats.ReceiveErrors.ChecksumErrors.Increment()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
e.rcvMu.Lock()
|
||||
e.stack.Stats().UDP.PacketsReceived.Increment()
|
||||
e.stats.PacketsReceived.Increment()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user