summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header
diff options
context:
space:
mode:
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.