summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header/tcp.go
diff options
context:
space:
mode:
authorBhasker Hariharan <bhaskerh@google.com>2019-04-09 11:22:28 -0700
committerShentubot <shentubot@google.com>2019-04-09 11:23:47 -0700
commiteaac2806ffadbb3db6317e58c61b855b1350f0aa (patch)
treec0395952725697a475c3f7500ca64c8d5f9575b7 /pkg/tcpip/header/tcp.go
parentf18a8f958187aa52d028d1e3cc3dc0c03b742fed (diff)
Add TCP checksum verification.
PiperOrigin-RevId: 242704699 Change-Id: I87db368ca343b3b4bf4f969b17d3aa4ce2f8bd4f
Diffstat (limited to 'pkg/tcpip/header/tcp.go')
-rw-r--r--pkg/tcpip/header/tcp.go68
1 files changed, 32 insertions, 36 deletions
diff --git a/pkg/tcpip/header/tcp.go b/pkg/tcpip/header/tcp.go
index 6e3ee2e50..e656ebb15 100644
--- a/pkg/tcpip/header/tcp.go
+++ b/pkg/tcpip/header/tcp.go
@@ -22,16 +22,17 @@ import (
"gvisor.googlesource.com/gvisor/pkg/tcpip/seqnum"
)
+// These constants are the offsets of the respective fields in the TCP header.
const (
- srcPort = 0
- dstPort = 2
- seqNum = 4
- ackNum = 8
- dataOffset = 12
- tcpFlags = 13
- winSize = 14
- tcpChecksum = 16
- urgentPtr = 18
+ TCPSrcPortOffset = 0
+ TCPDstPortOffset = 2
+ TCPSeqNumOffset = 4
+ TCPAckNumOffset = 8
+ TCPDataOffset = 12
+ TCPFlagsOffset = 13
+ TCPWinSizeOffset = 14
+ TCPChecksumOffset = 16
+ TCPUrgentPtrOffset = 18
)
const (
@@ -179,27 +180,27 @@ const (
// SourcePort returns the "source port" field of the tcp header.
func (b TCP) SourcePort() uint16 {
- return binary.BigEndian.Uint16(b[srcPort:])
+ return binary.BigEndian.Uint16(b[TCPSrcPortOffset:])
}
// DestinationPort returns the "destination port" field of the tcp header.
func (b TCP) DestinationPort() uint16 {
- return binary.BigEndian.Uint16(b[dstPort:])
+ return binary.BigEndian.Uint16(b[TCPDstPortOffset:])
}
// SequenceNumber returns the "sequence number" field of the tcp header.
func (b TCP) SequenceNumber() uint32 {
- return binary.BigEndian.Uint32(b[seqNum:])
+ return binary.BigEndian.Uint32(b[TCPSeqNumOffset:])
}
// AckNumber returns the "ack number" field of the tcp header.
func (b TCP) AckNumber() uint32 {
- return binary.BigEndian.Uint32(b[ackNum:])
+ return binary.BigEndian.Uint32(b[TCPAckNumOffset:])
}
// DataOffset returns the "data offset" field of the tcp header.
func (b TCP) DataOffset() uint8 {
- return (b[dataOffset] >> 4) * 4
+ return (b[TCPDataOffset] >> 4) * 4
}
// Payload returns the data in the tcp packet.
@@ -209,32 +210,32 @@ func (b TCP) Payload() []byte {
// Flags returns the flags field of the tcp header.
func (b TCP) Flags() uint8 {
- return b[tcpFlags]
+ return b[TCPFlagsOffset]
}
// WindowSize returns the "window size" field of the tcp header.
func (b TCP) WindowSize() uint16 {
- return binary.BigEndian.Uint16(b[winSize:])
+ return binary.BigEndian.Uint16(b[TCPWinSizeOffset:])
}
// Checksum returns the "checksum" field of the tcp header.
func (b TCP) Checksum() uint16 {
- return binary.BigEndian.Uint16(b[tcpChecksum:])
+ return binary.BigEndian.Uint16(b[TCPChecksumOffset:])
}
// SetSourcePort sets the "source port" field of the tcp header.
func (b TCP) SetSourcePort(port uint16) {
- binary.BigEndian.PutUint16(b[srcPort:], port)
+ binary.BigEndian.PutUint16(b[TCPSrcPortOffset:], port)
}
// SetDestinationPort sets the "destination port" field of the tcp header.
func (b TCP) SetDestinationPort(port uint16) {
- binary.BigEndian.PutUint16(b[dstPort:], port)
+ binary.BigEndian.PutUint16(b[TCPDstPortOffset:], port)
}
// SetChecksum sets the checksum field of the tcp header.
func (b TCP) SetChecksum(checksum uint16) {
- binary.BigEndian.PutUint16(b[tcpChecksum:], checksum)
+ binary.BigEndian.PutUint16(b[TCPChecksumOffset:], checksum)
}
// CalculateChecksum calculates the checksum of the tcp segment.
@@ -258,20 +259,20 @@ func (b TCP) ParsedOptions() TCPOptions {
}
func (b TCP) encodeSubset(seq, ack uint32, flags uint8, rcvwnd uint16) {
- binary.BigEndian.PutUint32(b[seqNum:], seq)
- binary.BigEndian.PutUint32(b[ackNum:], ack)
- b[tcpFlags] = flags
- binary.BigEndian.PutUint16(b[winSize:], rcvwnd)
+ binary.BigEndian.PutUint32(b[TCPSeqNumOffset:], seq)
+ binary.BigEndian.PutUint32(b[TCPAckNumOffset:], ack)
+ b[TCPFlagsOffset] = flags
+ binary.BigEndian.PutUint16(b[TCPWinSizeOffset:], rcvwnd)
}
// Encode encodes all the fields of the tcp header.
func (b TCP) Encode(t *TCPFields) {
b.encodeSubset(t.SeqNum, t.AckNum, t.Flags, t.WindowSize)
- binary.BigEndian.PutUint16(b[srcPort:], t.SrcPort)
- binary.BigEndian.PutUint16(b[dstPort:], t.DstPort)
- b[dataOffset] = (t.DataOffset / 4) << 4
- binary.BigEndian.PutUint16(b[tcpChecksum:], t.Checksum)
- binary.BigEndian.PutUint16(b[urgentPtr:], t.UrgentPointer)
+ binary.BigEndian.PutUint16(b[TCPSrcPortOffset:], t.SrcPort)
+ binary.BigEndian.PutUint16(b[TCPDstPortOffset:], t.DstPort)
+ b[TCPDataOffset] = (t.DataOffset / 4) << 4
+ binary.BigEndian.PutUint16(b[TCPChecksumOffset:], t.Checksum)
+ binary.BigEndian.PutUint16(b[TCPUrgentPtrOffset:], t.UrgentPointer)
}
// EncodePartial updates a subset of the fields of the tcp header. It is useful
@@ -290,18 +291,13 @@ func (b TCP) EncodePartial(partialChecksum, length uint16, seqnum, acknum uint32
b.encodeSubset(seqnum, acknum, flags, rcvwnd)
// Add the contributions of the passed-in fields to the checksum.
- checksum = Checksum(b[seqNum:seqNum+8], checksum)
- checksum = Checksum(b[winSize:winSize+2], checksum)
+ checksum = Checksum(b[TCPSeqNumOffset:TCPSeqNumOffset+8], checksum)
+ checksum = Checksum(b[TCPWinSizeOffset:TCPWinSizeOffset+2], checksum)
// Encode the checksum.
b.SetChecksum(^checksum)
}
-// TCPChecksumOffset returns offset of the checksum field.
-func TCPChecksumOffset() uint16 {
- return tcpChecksum
-}
-
// ParseSynOptions parses the options received in a SYN segment and returns the
// relevant ones. opts should point to the option part of the TCP Header.
func ParseSynOptions(opts []byte, isAck bool) TCPSynOptions {