2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-07-09 21:03:03 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// Package header provides the implementation of the encoding and decoding of
|
|
|
|
// network protocol headers.
|
|
|
|
package header
|
|
|
|
|
|
|
|
import (
|
2019-03-27 00:14:04 +00:00
|
|
|
"encoding/binary"
|
|
|
|
|
2019-06-13 23:49:09 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/buffer"
|
2018-04-27 17:37:02 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 18:54:14 +00:00
|
|
|
func calculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bool) {
|
2018-11-05 23:38:32 +00:00
|
|
|
v := initial
|
2018-04-27 17:37:02 +00:00
|
|
|
|
2019-10-22 18:54:14 +00:00
|
|
|
if odd {
|
|
|
|
v += uint32(buf[0])
|
|
|
|
buf = buf[1:]
|
|
|
|
}
|
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
l := len(buf)
|
2019-10-22 18:54:14 +00:00
|
|
|
odd = l&1 != 0
|
|
|
|
if odd {
|
2018-04-27 17:37:02 +00:00
|
|
|
l--
|
|
|
|
v += uint32(buf[l]) << 8
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i += 2 {
|
|
|
|
v += (uint32(buf[i]) << 8) + uint32(buf[i+1])
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:54:14 +00:00
|
|
|
return ChecksumCombine(uint16(v), uint16(v>>16)), odd
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 23:38:32 +00:00
|
|
|
// Checksum calculates the checksum (as defined in RFC 1071) of the bytes in the
|
|
|
|
// given byte array.
|
|
|
|
//
|
|
|
|
// The initial checksum must have been computed on an even number of bytes.
|
|
|
|
func Checksum(buf []byte, initial uint16) uint16 {
|
2019-10-22 18:54:14 +00:00
|
|
|
s, _ := calculateChecksum(buf, false, uint32(initial))
|
|
|
|
return s
|
2018-11-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChecksumVV calculates the checksum (as defined in RFC 1071) of the bytes in
|
|
|
|
// the given VectorizedView.
|
|
|
|
//
|
|
|
|
// The initial checksum must have been computed on an even number of bytes.
|
|
|
|
func ChecksumVV(vv buffer.VectorisedView, initial uint16) uint16 {
|
2019-10-22 18:54:14 +00:00
|
|
|
return ChecksumVVWithOffset(vv, initial, 0, vv.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChecksumVVWithOffset calculates the checksum (as defined in RFC 1071) of the
|
|
|
|
// bytes in the given VectorizedView.
|
|
|
|
//
|
|
|
|
// The initial checksum must have been computed on an even number of bytes.
|
|
|
|
func ChecksumVVWithOffset(vv buffer.VectorisedView, initial uint16, off int, size int) uint16 {
|
|
|
|
odd := false
|
2018-11-05 23:38:32 +00:00
|
|
|
sum := initial
|
|
|
|
for _, v := range vv.Views() {
|
|
|
|
if len(v) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-22 18:54:14 +00:00
|
|
|
|
|
|
|
if off >= len(v) {
|
|
|
|
off -= len(v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v = v[off:]
|
|
|
|
|
|
|
|
l := len(v)
|
|
|
|
if l > size {
|
|
|
|
l = size
|
|
|
|
}
|
|
|
|
v = v[:l]
|
|
|
|
|
|
|
|
sum, odd = calculateChecksum(v, odd, uint32(sum))
|
|
|
|
|
|
|
|
size -= len(v)
|
|
|
|
if size == 0 {
|
|
|
|
break
|
2018-11-05 23:38:32 +00:00
|
|
|
}
|
2019-10-22 18:54:14 +00:00
|
|
|
off = 0
|
2018-11-05 23:38:32 +00:00
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
// ChecksumCombine combines the two uint16 to form their checksum. This is done
|
|
|
|
// by adding them and the carry.
|
2018-11-05 23:38:32 +00:00
|
|
|
//
|
|
|
|
// Note that checksum a must have been computed on an even number of bytes.
|
2018-04-27 17:37:02 +00:00
|
|
|
func ChecksumCombine(a, b uint16) uint16 {
|
|
|
|
v := uint32(a) + uint32(b)
|
|
|
|
return uint16(v + v>>16)
|
|
|
|
}
|
|
|
|
|
2019-03-27 00:14:04 +00:00
|
|
|
// 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 {
|
2018-04-27 17:37:02 +00:00
|
|
|
xsum := Checksum([]byte(srcAddr), 0)
|
|
|
|
xsum = Checksum([]byte(dstAddr), xsum)
|
2019-03-27 00:14:04 +00:00
|
|
|
|
|
|
|
// Add the length portion of the checksum to the pseudo-checksum.
|
|
|
|
tmp := make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(tmp, totalLen)
|
|
|
|
xsum = Checksum(tmp, xsum)
|
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
return Checksum([]byte{0, uint8(protocol)}, xsum)
|
|
|
|
}
|