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 ipv6
|
|
|
|
|
|
|
|
import (
|
2019-06-13 23:49:09 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/buffer"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
2018-04-27 17:37:02 +00:00
|
|
|
)
|
|
|
|
|
2019-09-04 01:41:40 +00:00
|
|
|
const (
|
|
|
|
// ndpHopLimit is the expected IP hop limit value of 255 for received
|
|
|
|
// NDP packets, as per RFC 4861 sections 4.1 - 4.5, 6.1.1, 6.1.2, 7.1.1,
|
|
|
|
// 7.1.2 and 8.1. If the hop limit value is not 255, nodes MUST silently
|
|
|
|
// drop the NDP packet. All outgoing NDP packets must use this value for
|
|
|
|
// its IP hop limit field.
|
|
|
|
ndpHopLimit = 255
|
|
|
|
)
|
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
// handleControl handles the case when an ICMP packet contains the headers of
|
|
|
|
// the original packet that caused the ICMP one to be sent. This information is
|
|
|
|
// used to find out which transport endpoint must be notified about the ICMP
|
|
|
|
// packet.
|
2018-09-13 04:57:04 +00:00
|
|
|
func (e *endpoint) handleControl(typ stack.ControlType, extra uint32, vv buffer.VectorisedView) {
|
2018-04-27 17:37:02 +00:00
|
|
|
h := header.IPv6(vv.First())
|
|
|
|
|
|
|
|
// We don't use IsValid() here because ICMP only requires that up to
|
|
|
|
// 1280 bytes of the original packet be included. So it's likely that it
|
|
|
|
// is truncated, which would cause IsValid to return false.
|
|
|
|
//
|
|
|
|
// Drop packet if it doesn't have the basic IPv6 header or if the
|
|
|
|
// original source address doesn't match the endpoint's address.
|
|
|
|
if len(h) < header.IPv6MinimumSize || h.SourceAddress() != e.id.LocalAddress {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the IP header, then handle the fragmentation header if there
|
|
|
|
// is one.
|
|
|
|
vv.TrimFront(header.IPv6MinimumSize)
|
|
|
|
p := h.TransportProtocol()
|
|
|
|
if p == header.IPv6FragmentHeader {
|
|
|
|
f := header.IPv6Fragment(vv.First())
|
|
|
|
if !f.IsValid() || f.FragmentOffset() != 0 {
|
|
|
|
// We can't handle fragments that aren't at offset 0
|
|
|
|
// because they don't have the transport headers.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip fragmentation header and find out the actual protocol
|
|
|
|
// number.
|
|
|
|
vv.TrimFront(header.IPv6FragmentHeaderSize)
|
|
|
|
p = f.TransportProtocol()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deliver the control packet to the transport endpoint.
|
|
|
|
e.dispatcher.DeliverTransportControlPacket(e.id.LocalAddress, h.DestinationAddress(), ProtocolNumber, p, typ, extra, vv)
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:30:20 +00:00
|
|
|
func (e *endpoint) handleICMP(r *stack.Route, netHeader buffer.View, vv buffer.VectorisedView) {
|
2019-03-28 21:08:11 +00:00
|
|
|
stats := r.Stats().ICMP
|
|
|
|
sent := stats.V6PacketsSent
|
|
|
|
received := stats.V6PacketsReceived
|
2018-04-27 17:37:02 +00:00
|
|
|
v := vv.First()
|
|
|
|
if len(v) < header.ICMPv6MinimumSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-04-27 17:37:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h := header.ICMPv6(v)
|
|
|
|
|
2019-09-04 01:41:40 +00:00
|
|
|
// As per RFC 4861 sections 4.1 - 4.5, 6.1.1, 6.1.2, 7.1.1, 7.1.2 and
|
|
|
|
// 8.1, nodes MUST silently drop NDP packets where the Hop Limit field
|
|
|
|
// in the IPv6 header is not set to 255.
|
|
|
|
switch h.Type() {
|
|
|
|
case header.ICMPv6NeighborSolicit,
|
|
|
|
header.ICMPv6NeighborAdvert,
|
|
|
|
header.ICMPv6RouterSolicit,
|
|
|
|
header.ICMPv6RouterAdvert,
|
|
|
|
header.ICMPv6RedirectMsg:
|
|
|
|
if header.IPv6(netHeader).HopLimit() != ndpHopLimit {
|
|
|
|
received.Invalid.Increment()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 21:03:04 +00:00
|
|
|
// TODO(b/112892170): Meaningfully handle all ICMP types.
|
2018-04-27 17:37:02 +00:00
|
|
|
switch h.Type() {
|
|
|
|
case header.ICMPv6PacketTooBig:
|
2019-03-28 21:08:11 +00:00
|
|
|
received.PacketTooBig.Increment()
|
2018-04-27 17:37:02 +00:00
|
|
|
if len(v) < header.ICMPv6PacketTooBigMinimumSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-04-27 17:37:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
vv.TrimFront(header.ICMPv6PacketTooBigMinimumSize)
|
2019-09-03 22:59:58 +00:00
|
|
|
mtu := h.MTU()
|
2018-04-27 17:37:02 +00:00
|
|
|
e.handleControl(stack.ControlPacketTooBig, calculateMTU(mtu), vv)
|
|
|
|
|
|
|
|
case header.ICMPv6DstUnreachable:
|
2019-03-28 21:08:11 +00:00
|
|
|
received.DstUnreachable.Increment()
|
2018-04-27 17:37:02 +00:00
|
|
|
if len(v) < header.ICMPv6DstUnreachableMinimumSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-04-27 17:37:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
vv.TrimFront(header.ICMPv6DstUnreachableMinimumSize)
|
|
|
|
switch h.Code() {
|
|
|
|
case header.ICMPv6PortUnreachable:
|
|
|
|
e.handleControl(stack.ControlPortUnreachable, 0, vv)
|
|
|
|
}
|
2018-09-04 21:30:15 +00:00
|
|
|
|
|
|
|
case header.ICMPv6NeighborSolicit:
|
2019-03-28 21:08:11 +00:00
|
|
|
received.NeighborSolicit.Increment()
|
|
|
|
|
2018-09-04 21:30:15 +00:00
|
|
|
if len(v) < header.ICMPv6NeighborSolicitMinimumSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-28 01:53:34 +00:00
|
|
|
targetAddr := tcpip.Address(v[8:][:header.IPv6AddressSize])
|
2018-09-04 21:30:15 +00:00
|
|
|
if e.linkAddrCache.CheckLocalAddress(e.nicid, ProtocolNumber, targetAddr) == 0 {
|
|
|
|
// We don't have a useful answer; the best we can do is ignore the request.
|
|
|
|
return
|
|
|
|
}
|
2019-03-28 21:08:11 +00:00
|
|
|
|
|
|
|
hdr := buffer.NewPrependable(int(r.MaxHeaderLength()) + header.ICMPv6NeighborAdvertSize)
|
2018-09-04 21:30:15 +00:00
|
|
|
pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6NeighborAdvertSize))
|
|
|
|
pkt.SetType(header.ICMPv6NeighborAdvert)
|
|
|
|
pkt[icmpV6FlagOffset] = ndpSolicitedFlag | ndpOverrideFlag
|
|
|
|
copy(pkt[icmpV6OptOffset-len(targetAddr):], targetAddr)
|
|
|
|
pkt[icmpV6OptOffset] = ndpOptDstLinkAddr
|
|
|
|
pkt[icmpV6LengthOffset] = 1
|
|
|
|
copy(pkt[icmpV6LengthOffset+1:], r.LocalLinkAddress[:])
|
2018-09-26 19:39:32 +00:00
|
|
|
|
|
|
|
// ICMPv6 Neighbor Solicit messages are always sent to
|
|
|
|
// specially crafted IPv6 multicast addresses. As a result, the
|
|
|
|
// route we end up with here has as its LocalAddress such a
|
|
|
|
// multicast address. It would be nonsense to claim that our
|
|
|
|
// source address is a multicast address, so we manually set
|
|
|
|
// the source address to the target address requested in the
|
|
|
|
// solicit message. Since that requires mutating the route, we
|
|
|
|
// must first clone it.
|
|
|
|
r := r.Clone()
|
|
|
|
defer r.Release()
|
|
|
|
r.LocalAddress = targetAddr
|
2019-09-03 22:59:58 +00:00
|
|
|
pkt.SetChecksum(header.ICMPv6Checksum(pkt, r.LocalAddress, r.RemoteAddress, buffer.VectorisedView{}))
|
2018-09-04 21:30:15 +00:00
|
|
|
|
2019-10-08 02:28:26 +00:00
|
|
|
if err := r.WritePacket(nil /* gso */, hdr, buffer.VectorisedView{}, header.ICMPv6ProtocolNumber, 0, true /* useDefaultTTL */); err != nil {
|
2019-03-28 21:08:11 +00:00
|
|
|
sent.Dropped.Increment()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sent.NeighborAdvert.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
|
|
|
|
case header.ICMPv6NeighborAdvert:
|
2019-03-28 21:08:11 +00:00
|
|
|
received.NeighborAdvert.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
if len(v) < header.ICMPv6NeighborAdvertSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-28 01:53:34 +00:00
|
|
|
targetAddr := tcpip.Address(v[8:][:header.IPv6AddressSize])
|
2018-09-04 21:30:15 +00:00
|
|
|
e.linkAddrCache.AddLinkAddress(e.nicid, targetAddr, r.RemoteLinkAddress)
|
|
|
|
if targetAddr != r.RemoteAddress {
|
|
|
|
e.linkAddrCache.AddLinkAddress(e.nicid, r.RemoteAddress, r.RemoteLinkAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
case header.ICMPv6EchoRequest:
|
2019-03-28 21:08:11 +00:00
|
|
|
received.EchoRequest.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
if len(v) < header.ICMPv6EchoMinimumSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-26 18:43:55 +00:00
|
|
|
|
2018-09-04 21:30:15 +00:00
|
|
|
vv.TrimFront(header.ICMPv6EchoMinimumSize)
|
2019-03-26 18:43:55 +00:00
|
|
|
hdr := buffer.NewPrependable(int(r.MaxHeaderLength()) + header.ICMPv6EchoMinimumSize)
|
2018-09-04 21:30:15 +00:00
|
|
|
pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6EchoMinimumSize))
|
|
|
|
copy(pkt, h)
|
|
|
|
pkt.SetType(header.ICMPv6EchoReply)
|
2019-09-03 22:59:58 +00:00
|
|
|
pkt.SetChecksum(header.ICMPv6Checksum(pkt, r.LocalAddress, r.RemoteAddress, vv))
|
2019-10-08 02:28:26 +00:00
|
|
|
if err := r.WritePacket(nil /* gso */, hdr, vv, header.ICMPv6ProtocolNumber, 0, true /* useDefaultTTL */); err != nil {
|
2019-03-28 21:08:11 +00:00
|
|
|
sent.Dropped.Increment()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sent.EchoReply.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
|
|
|
|
case header.ICMPv6EchoReply:
|
2019-03-28 21:08:11 +00:00
|
|
|
received.EchoReply.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
if len(v) < header.ICMPv6EchoMinimumSize {
|
2019-03-28 21:08:11 +00:00
|
|
|
received.Invalid.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-27 22:30:20 +00:00
|
|
|
e.dispatcher.DeliverTransportPacket(r, header.ICMPv6ProtocolNumber, netHeader, vv)
|
2018-09-04 21:30:15 +00:00
|
|
|
|
2019-03-28 21:08:11 +00:00
|
|
|
case header.ICMPv6TimeExceeded:
|
|
|
|
received.TimeExceeded.Increment()
|
|
|
|
|
|
|
|
case header.ICMPv6ParamProblem:
|
|
|
|
received.ParamProblem.Increment()
|
|
|
|
|
|
|
|
case header.ICMPv6RouterSolicit:
|
|
|
|
received.RouterSolicit.Increment()
|
|
|
|
|
|
|
|
case header.ICMPv6RouterAdvert:
|
|
|
|
received.RouterAdvert.Increment()
|
|
|
|
|
|
|
|
case header.ICMPv6RedirectMsg:
|
|
|
|
received.RedirectMsg.Increment()
|
|
|
|
|
|
|
|
default:
|
|
|
|
received.Invalid.Increment()
|
2018-09-04 21:30:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ndpSolicitedFlag = 1 << 6
|
|
|
|
ndpOverrideFlag = 1 << 5
|
|
|
|
|
|
|
|
ndpOptSrcLinkAddr = 1
|
|
|
|
ndpOptDstLinkAddr = 2
|
|
|
|
|
|
|
|
icmpV6FlagOffset = 4
|
|
|
|
icmpV6OptOffset = 24
|
|
|
|
icmpV6LengthOffset = 25
|
|
|
|
)
|
|
|
|
|
|
|
|
var broadcastMAC = tcpip.LinkAddress([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
|
|
|
|
|
|
|
|
var _ stack.LinkAddressResolver = (*protocol)(nil)
|
|
|
|
|
|
|
|
// LinkAddressProtocol implements stack.LinkAddressResolver.
|
|
|
|
func (*protocol) LinkAddressProtocol() tcpip.NetworkProtocolNumber {
|
|
|
|
return header.IPv6ProtocolNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkAddressRequest implements stack.LinkAddressResolver.
|
|
|
|
func (*protocol) LinkAddressRequest(addr, localAddr tcpip.Address, linkEP stack.LinkEndpoint) *tcpip.Error {
|
2018-09-26 16:48:29 +00:00
|
|
|
snaddr := header.SolicitedNodeAddr(addr)
|
2018-09-04 21:30:15 +00:00
|
|
|
r := &stack.Route{
|
|
|
|
LocalAddress: localAddr,
|
|
|
|
RemoteAddress: snaddr,
|
|
|
|
RemoteLinkAddress: broadcastMAC,
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|
2018-09-04 21:30:15 +00:00
|
|
|
hdr := buffer.NewPrependable(int(linkEP.MaxHeaderLength()) + header.IPv6MinimumSize + header.ICMPv6NeighborAdvertSize)
|
|
|
|
pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6NeighborAdvertSize))
|
|
|
|
pkt.SetType(header.ICMPv6NeighborSolicit)
|
|
|
|
copy(pkt[icmpV6OptOffset-len(addr):], addr)
|
|
|
|
pkt[icmpV6OptOffset] = ndpOptSrcLinkAddr
|
|
|
|
pkt[icmpV6LengthOffset] = 1
|
|
|
|
copy(pkt[icmpV6LengthOffset+1:], linkEP.LinkAddress())
|
2019-09-03 22:59:58 +00:00
|
|
|
pkt.SetChecksum(header.ICMPv6Checksum(pkt, r.LocalAddress, r.RemoteAddress, buffer.VectorisedView{}))
|
2018-09-04 21:30:15 +00:00
|
|
|
|
|
|
|
length := uint16(hdr.UsedLength())
|
|
|
|
ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize))
|
|
|
|
ip.Encode(&header.IPv6Fields{
|
|
|
|
PayloadLength: length,
|
|
|
|
NextHeader: uint8(header.ICMPv6ProtocolNumber),
|
2019-10-08 02:28:26 +00:00
|
|
|
HopLimit: ndpHopLimit,
|
2018-09-04 21:30:15 +00:00
|
|
|
SrcAddr: r.LocalAddress,
|
|
|
|
DstAddr: r.RemoteAddress,
|
|
|
|
})
|
|
|
|
|
2019-04-29 21:03:04 +00:00
|
|
|
// TODO(stijlist): count this in ICMP stats.
|
2019-03-28 18:02:23 +00:00
|
|
|
return linkEP.WritePacket(r, nil /* gso */, hdr, buffer.VectorisedView{}, ProtocolNumber)
|
2018-09-04 21:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveStaticAddress implements stack.LinkAddressResolver.
|
|
|
|
func (*protocol) ResolveStaticAddress(addr tcpip.Address) (tcpip.LinkAddress, bool) {
|
2019-02-28 22:37:42 +00:00
|
|
|
if header.IsV6MulticastAddress(addr) {
|
|
|
|
// RFC 2464 Transmission of IPv6 Packets over Ethernet Networks
|
|
|
|
//
|
|
|
|
// 7. Address Mapping -- Multicast
|
|
|
|
//
|
|
|
|
// An IPv6 packet with a multicast destination address DST,
|
|
|
|
// consisting of the sixteen octets DST[1] through DST[16], is
|
|
|
|
// transmitted to the Ethernet multicast address whose first
|
|
|
|
// two octets are the value 3333 hexadecimal and whose last
|
|
|
|
// four octets are the last four octets of DST.
|
|
|
|
return tcpip.LinkAddress([]byte{
|
|
|
|
0x33,
|
|
|
|
0x33,
|
|
|
|
addr[header.IPv6AddressSize-4],
|
|
|
|
addr[header.IPv6AddressSize-3],
|
|
|
|
addr[header.IPv6AddressSize-2],
|
|
|
|
addr[header.IPv6AddressSize-1],
|
|
|
|
}), true
|
|
|
|
}
|
2018-09-04 21:30:15 +00:00
|
|
|
return "", false
|
|
|
|
}
|