summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header
diff options
context:
space:
mode:
authorAndrei Vagin <avagin@google.com>2019-03-26 17:14:04 -0700
committerShentubot <shentubot@google.com>2019-03-26 17:15:13 -0700
commit654e878abba174d8f7d588f38ecfd5a020bf581e (patch)
tree0119b118423522981de149a475b64829c3bed882 /pkg/tcpip/header
parent06ec97a3f823f1f5d928fc9c2beb3a11c2c88487 (diff)
netstack: Don't exclude length when a pseudo-header checksum is calculated
This is a preparation for GSO changes (cl/234508902). RELNOTES[gofers]: Refactor checksum code to include length, which it already did, but in a convoluted way. Should be a no-op. PiperOrigin-RevId: 240460794 Change-Id: I537381bc670b5a9f5d70a87aa3eb7252e8f5ace2
Diffstat (limited to 'pkg/tcpip/header')
-rw-r--r--pkg/tcpip/header/checksum.go17
-rw-r--r--pkg/tcpip/header/tcp.go15
-rw-r--r--pkg/tcpip/header/udp.go14
3 files changed, 20 insertions, 26 deletions
diff --git a/pkg/tcpip/header/checksum.go b/pkg/tcpip/header/checksum.go
index 2c2fcbd9b..2e8c65fac 100644
--- a/pkg/tcpip/header/checksum.go
+++ b/pkg/tcpip/header/checksum.go
@@ -17,6 +17,8 @@
package header
import (
+ "encoding/binary"
+
"gvisor.googlesource.com/gvisor/pkg/tcpip"
"gvisor.googlesource.com/gvisor/pkg/tcpip/buffer"
)
@@ -76,12 +78,17 @@ func ChecksumCombine(a, b uint16) uint16 {
return uint16(v + v>>16)
}
-// PseudoHeaderChecksum calculates the pseudo-header checksum for the
-// given destination protocol and network address, ignoring the length
-// field. Pseudo-headers are needed by transport layers when calculating
-// their own checksum.
-func PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, srcAddr tcpip.Address, dstAddr tcpip.Address) uint16 {
+// PseudoHeaderChecksum calculates the pseudo-header checksum for the given
+// destination protocol and network address. Pseudo-headers are needed by
+// transport layers when calculating their own checksum.
+func PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, srcAddr tcpip.Address, dstAddr tcpip.Address, totalLen uint16) uint16 {
xsum := Checksum([]byte(srcAddr), 0)
xsum = Checksum([]byte(dstAddr), xsum)
+
+ // Add the length portion of the checksum to the pseudo-checksum.
+ tmp := make([]byte, 2)
+ binary.BigEndian.PutUint16(tmp, totalLen)
+ xsum = Checksum(tmp, xsum)
+
return Checksum([]byte{0, uint8(protocol)}, xsum)
}
diff --git a/pkg/tcpip/header/tcp.go b/pkg/tcpip/header/tcp.go
index 207046b36..d37d624fe 100644
--- a/pkg/tcpip/header/tcp.go
+++ b/pkg/tcpip/header/tcp.go
@@ -231,19 +231,12 @@ func (b TCP) SetChecksum(checksum uint16) {
binary.BigEndian.PutUint16(b[tcpChecksum:], checksum)
}
-// CalculateChecksum calculates the checksum of the tcp segment given
-// the totalLen and partialChecksum(descriptions below)
-// totalLen is the total length of the segment
+// CalculateChecksum calculates the checksum of the tcp segment.
// partialChecksum is the checksum of the network-layer pseudo-header
-// (excluding the total length) and the checksum of the segment data.
-func (b TCP) CalculateChecksum(partialChecksum uint16, totalLen uint16) uint16 {
- // Add the length portion of the checksum to the pseudo-checksum.
- tmp := make([]byte, 2)
- binary.BigEndian.PutUint16(tmp, totalLen)
- checksum := Checksum(tmp, partialChecksum)
-
+// and the checksum of the segment data.
+func (b TCP) CalculateChecksum(partialChecksum uint16) uint16 {
// Calculate the rest of the checksum.
- return Checksum(b[:b.DataOffset()], checksum)
+ return Checksum(b[:b.DataOffset()], partialChecksum)
}
// Options returns a slice that holds the unparsed TCP options in the segment.
diff --git a/pkg/tcpip/header/udp.go b/pkg/tcpip/header/udp.go
index 31c8ef456..e8c860436 100644
--- a/pkg/tcpip/header/udp.go
+++ b/pkg/tcpip/header/udp.go
@@ -94,17 +94,11 @@ func (b UDP) SetChecksum(checksum uint16) {
binary.BigEndian.PutUint16(b[udpChecksum:], checksum)
}
-// CalculateChecksum calculates the checksum of the udp packet, given the total
-// length of the packet and the checksum of the network-layer pseudo-header
-// (excluding the total length) and the checksum of the payload.
-func (b UDP) CalculateChecksum(partialChecksum uint16, totalLen uint16) uint16 {
- // Add the length portion of the checksum to the pseudo-checksum.
- tmp := make([]byte, 2)
- binary.BigEndian.PutUint16(tmp, totalLen)
- checksum := Checksum(tmp, partialChecksum)
-
+// CalculateChecksum calculates the checksum of the udp packet, given the
+// checksum of the network-layer pseudo-header and the checksum of the payload.
+func (b UDP) CalculateChecksum(partialChecksum uint16) uint16 {
// Calculate the rest of the checksum.
- return Checksum(b[:UDPMinimumSize], checksum)
+ return Checksum(b[:UDPMinimumSize], partialChecksum)
}
// Encode encodes all the fields of the udp header.