summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2020-01-22 10:27:54 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2020-03-17 19:32:49 +0100
commitf10dc5ab203ecf9e56f6e69be284855ef0793fad (patch)
treebfa7e32934dfcd0f34657e81ec801b122a662698 /pkg/tcpip/header
parentcb87cbb7e46568c0bab786205369d11a7fc5b268 (diff)
Update the TTL field in forwarded packets
Closes: #1085
Diffstat (limited to 'pkg/tcpip/header')
-rw-r--r--pkg/tcpip/header/ipv4.go12
-rw-r--r--pkg/tcpip/header/ipv6.go5
2 files changed, 17 insertions, 0 deletions
diff --git a/pkg/tcpip/header/ipv4.go b/pkg/tcpip/header/ipv4.go
index 76839eb92..d8111a439 100644
--- a/pkg/tcpip/header/ipv4.go
+++ b/pkg/tcpip/header/ipv4.go
@@ -248,6 +248,18 @@ func (b IPv4) SetDestinationAddress(addr tcpip.Address) {
copy(b[dstAddr:dstAddr+IPv4AddressSize], addr)
}
+func (b IPv4) UpdateTTL(n uint8) {
+ var sum uint32
+ var old uint16
+
+ old = binary.BigEndian.Uint16(b[ttl:])
+ b[ttl] -= n
+ sum = uint32(old) + uint32(^binary.BigEndian.Uint16(b[ttl:]) & 0xffff)
+ sum = sum + uint32(b.Checksum())
+ sum = (sum & 0xffff) + (sum>>16)
+ b.SetChecksum(uint16(sum + (sum>>16)))
+}
+
// CalculateChecksum calculates the checksum of the ipv4 header.
func (b IPv4) CalculateChecksum() uint16 {
return Checksum(b[:b.HeaderLength()], 0)
diff --git a/pkg/tcpip/header/ipv6.go b/pkg/tcpip/header/ipv6.go
index 76e88e9b3..ff1aa937e 100644
--- a/pkg/tcpip/header/ipv6.go
+++ b/pkg/tcpip/header/ipv6.go
@@ -226,6 +226,11 @@ func (b IPv6) SetNextHeader(v uint8) {
b[nextHdr] = v
}
+// SetHopLimit sets the value of the "hop limit" field of the ipv6 header.
+func (b IPv6) SetHopLimit(v uint8) {
+ b[hopLimit] = v
+}
+
// SetChecksum implements Network.SetChecksum. Given that IPv6 doesn't have a
// checksum, it is empty.
func (IPv6) SetChecksum(uint16) {