From b26b4610bfb7bf37821ab33ad51ace3c6ece81dd Mon Sep 17 00:00:00 2001 From: Ghanan Gowripalan Date: Mon, 20 Dec 2021 10:45:51 -0800 Subject: [PATCH] Use available helpers to set fields ...and introduce a helper to set the Checksum. PiperOrigin-RevId: 417437300 --- pkg/tcpip/header/checksum.go | 5 +++++ pkg/tcpip/header/icmpv4.go | 2 +- pkg/tcpip/header/icmpv6.go | 2 +- pkg/tcpip/header/ipv4.go | 2 +- pkg/tcpip/header/tcp.go | 16 ++++++++-------- pkg/tcpip/header/udp.go | 10 +++++----- test/packetimpact/testbench/layers.go | 2 +- 7 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pkg/tcpip/header/checksum.go b/pkg/tcpip/header/checksum.go index e2c85e220..ff283160d 100644 --- a/pkg/tcpip/header/checksum.go +++ b/pkg/tcpip/header/checksum.go @@ -24,6 +24,11 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/buffer" ) +// PutChecksum puts the checksum in the provided byte slice. +func PutChecksum(b []byte, xsum uint16) { + binary.BigEndian.PutUint16(b, xsum) +} + func calculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bool) { v := initial diff --git a/pkg/tcpip/header/icmpv4.go b/pkg/tcpip/header/icmpv4.go index a7715aefe..892fe8b91 100644 --- a/pkg/tcpip/header/icmpv4.go +++ b/pkg/tcpip/header/icmpv4.go @@ -139,7 +139,7 @@ func (b ICMPv4) Checksum() uint16 { // SetChecksum sets the ICMP checksum field. func (b ICMPv4) SetChecksum(checksum uint16) { - binary.BigEndian.PutUint16(b[icmpv4ChecksumOffset:], checksum) + PutChecksum(b[icmpv4ChecksumOffset:], checksum) } // SourcePort implements Transport.SourcePort. diff --git a/pkg/tcpip/header/icmpv6.go b/pkg/tcpip/header/icmpv6.go index 954b77948..5beaf8cf5 100644 --- a/pkg/tcpip/header/icmpv6.go +++ b/pkg/tcpip/header/icmpv6.go @@ -199,7 +199,7 @@ func (b ICMPv6) Checksum() uint16 { // SetChecksum sets the ICMP checksum field. func (b ICMPv6) SetChecksum(checksum uint16) { - binary.BigEndian.PutUint16(b[icmpv6ChecksumOffset:], checksum) + PutChecksum(b[icmpv6ChecksumOffset:], checksum) } // SourcePort implements Transport.SourcePort. diff --git a/pkg/tcpip/header/ipv4.go b/pkg/tcpip/header/ipv4.go index 7baaf0d17..87f6e5b59 100644 --- a/pkg/tcpip/header/ipv4.go +++ b/pkg/tcpip/header/ipv4.go @@ -380,7 +380,7 @@ func (b IPv4) SetTotalLength(totalLength uint16) { // SetChecksum sets the checksum field of the IPv4 header. func (b IPv4) SetChecksum(v uint16) { - binary.BigEndian.PutUint16(b[checksum:], v) + PutChecksum(b[checksum:], v) } // SetFlagsFragmentOffset sets the "flags" and "fragment offset" fields of the diff --git a/pkg/tcpip/header/tcp.go b/pkg/tcpip/header/tcp.go index a75e51a28..e82e9d9c1 100644 --- a/pkg/tcpip/header/tcp.go +++ b/pkg/tcpip/header/tcp.go @@ -289,7 +289,7 @@ func (b TCP) SetDestinationPort(port uint16) { // SetChecksum sets the checksum field of the TCP header. func (b TCP) SetChecksum(checksum uint16) { - binary.BigEndian.PutUint16(b[TCPChecksumOffset:], checksum) + PutChecksum(b[TCPChecksumOffset:], checksum) } // SetDataOffset sets the data offset field of the TCP header. headerLen should @@ -318,8 +318,8 @@ func (b TCP) SetWindowSize(rcvwnd uint16) { binary.BigEndian.PutUint16(b[TCPWinSizeOffset:], rcvwnd) } -// SetUrgentPoiner sets the window size field of the TCP header. -func (b TCP) SetUrgentPoiner(urgentPointer uint16) { +// SetUrgentPointer sets the window size field of the TCP header. +func (b TCP) SetUrgentPointer(urgentPointer uint16) { binary.BigEndian.PutUint16(b[TCPUrgentPtrOffset:], urgentPointer) } @@ -360,11 +360,11 @@ func (b TCP) encodeSubset(seq, ack uint32, flags TCPFlags, rcvwnd uint16) { // 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[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) + b.SetSourcePort(t.SrcPort) + b.SetDestinationPort(t.DstPort) + b.SetDataOffset(t.DataOffset) + b.SetChecksum(t.Checksum) + b.SetUrgentPointer(t.UrgentPointer) } // EncodePartial updates a subset of the fields of the TCP header. It is useful diff --git a/pkg/tcpip/header/udp.go b/pkg/tcpip/header/udp.go index f69d53314..e2cad1e4a 100644 --- a/pkg/tcpip/header/udp.go +++ b/pkg/tcpip/header/udp.go @@ -101,7 +101,7 @@ func (b UDP) SetDestinationPort(port uint16) { // SetChecksum sets the "checksum" field of the UDP header. func (b UDP) SetChecksum(checksum uint16) { - binary.BigEndian.PutUint16(b[udpChecksum:], checksum) + PutChecksum(b[udpChecksum:], checksum) } // SetLength sets the "length" field of the UDP header. @@ -125,10 +125,10 @@ func (b UDP) IsChecksumValid(src, dst tcpip.Address, payloadChecksum uint16) boo // Encode encodes all the fields of the UDP header. func (b UDP) Encode(u *UDPFields) { - binary.BigEndian.PutUint16(b[udpSrcPort:], u.SrcPort) - binary.BigEndian.PutUint16(b[udpDstPort:], u.DstPort) - binary.BigEndian.PutUint16(b[udpLength:], u.Length) - binary.BigEndian.PutUint16(b[udpChecksum:], u.Checksum) + b.SetSourcePort(u.SrcPort) + b.SetDestinationPort(u.DstPort) + b.SetLength(u.Length) + b.SetChecksum(u.Checksum) } // SetSourcePortWithChecksumUpdate implements ChecksummableTransport. diff --git a/test/packetimpact/testbench/layers.go b/test/packetimpact/testbench/layers.go index 174ee07d1..d6c03343b 100644 --- a/test/packetimpact/testbench/layers.go +++ b/test/packetimpact/testbench/layers.go @@ -1089,7 +1089,7 @@ func (l *TCP) ToBytes() ([]byte, error) { h.SetWindowSize(32768) } if l.UrgentPointer != nil { - h.SetUrgentPoiner(*l.UrgentPointer) + h.SetUrgentPointer(*l.UrgentPointer) } copy(b[header.TCPMinimumSize:], l.Options) header.AddTCPOptionPadding(b[header.TCPMinimumSize:], len(l.Options))