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-05-02 05:50:55 +00:00
|
|
|
|
2019-02-22 21:33:49 +00:00
|
|
|
package icmp
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
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"
|
2019-08-02 23:25:34 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/iptables"
|
2019-06-13 23:49:09 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
|
|
"gvisor.dev/gvisor/pkg/waiter"
|
2018-05-02 05:50:55 +00:00
|
|
|
)
|
|
|
|
|
2018-08-02 17:41:44 +00:00
|
|
|
// +stateify savable
|
2019-02-22 21:33:49 +00:00
|
|
|
type icmpPacket struct {
|
|
|
|
icmpPacketEntry
|
2018-05-02 05:50:55 +00:00
|
|
|
senderAddress tcpip.FullAddress
|
|
|
|
data buffer.VectorisedView `state:".(buffer.VectorisedView)"`
|
|
|
|
timestamp int64
|
|
|
|
// views is used as buffer for data when its length is large
|
|
|
|
// enough to store a VectorisedView.
|
|
|
|
views [8]buffer.View `state:"nosave"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpointState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
stateInitial endpointState = iota
|
|
|
|
stateBound
|
|
|
|
stateConnected
|
|
|
|
stateClosed
|
|
|
|
)
|
|
|
|
|
2019-02-27 22:30:20 +00:00
|
|
|
// endpoint represents an ICMP endpoint. This struct serves as the interface
|
|
|
|
// between users of the endpoint and the protocol implementation; it is legal to
|
|
|
|
// have concurrent goroutines make calls into the endpoint, they are properly
|
|
|
|
// synchronized.
|
|
|
|
//
|
|
|
|
// +stateify savable
|
2018-05-02 05:50:55 +00:00
|
|
|
type endpoint struct {
|
2019-10-10 00:54:51 +00:00
|
|
|
stack.TransportEndpointInfo
|
|
|
|
|
2019-02-27 22:30:20 +00:00
|
|
|
// The following fields are initialized at creation time and are
|
|
|
|
// immutable.
|
2018-05-02 05:50:55 +00:00
|
|
|
stack *stack.Stack `state:"manual"`
|
|
|
|
waiterQueue *waiter.Queue
|
2019-10-30 22:32:20 +00:00
|
|
|
uniqueID uint64
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
// The following fields are used to manage the receive queue, and are
|
|
|
|
// protected by rcvMu.
|
|
|
|
rcvMu sync.Mutex `state:"nosave"`
|
|
|
|
rcvReady bool
|
2019-02-22 21:33:49 +00:00
|
|
|
rcvList icmpPacketList
|
2018-05-11 23:20:01 +00:00
|
|
|
rcvBufSizeMax int `state:".(int)"`
|
2018-05-02 05:50:55 +00:00
|
|
|
rcvBufSize int
|
|
|
|
rcvClosed bool
|
|
|
|
|
|
|
|
// The following fields are protected by the mu mutex.
|
|
|
|
mu sync.RWMutex `state:"nosave"`
|
|
|
|
sndBufSize int
|
2018-08-07 14:56:08 +00:00
|
|
|
// shutdownFlags represent the current shutdown state of the endpoint.
|
|
|
|
shutdownFlags tcpip.ShutdownFlags
|
|
|
|
state endpointState
|
2019-10-10 00:54:51 +00:00
|
|
|
route stack.Route `state:"manual"`
|
|
|
|
ttl uint8
|
|
|
|
stats tcpip.TransportEndpointStats `state:"nosave"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEndpoint(s *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) {
|
2019-04-02 18:12:29 +00:00
|
|
|
return &endpoint{
|
2019-10-10 00:54:51 +00:00
|
|
|
stack: s,
|
|
|
|
TransportEndpointInfo: stack.TransportEndpointInfo{
|
|
|
|
NetProto: netProto,
|
|
|
|
TransProto: transProto,
|
|
|
|
},
|
2018-05-02 05:50:55 +00:00
|
|
|
waiterQueue: waiterQueue,
|
|
|
|
rcvBufSizeMax: 32 * 1024,
|
|
|
|
sndBufSize: 32 * 1024,
|
2019-10-10 00:54:51 +00:00
|
|
|
state: stateInitial,
|
2019-10-30 22:32:20 +00:00
|
|
|
uniqueID: s.UniqueID(),
|
2019-04-02 18:12:29 +00:00
|
|
|
}, nil
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 22:32:20 +00:00
|
|
|
// UniqueID implements stack.TransportEndpoint.UniqueID.
|
|
|
|
func (e *endpoint) UniqueID() uint64 {
|
|
|
|
return e.uniqueID
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
// Close puts the endpoint in a closed state and frees all resources
|
|
|
|
// associated with it.
|
|
|
|
func (e *endpoint) Close() {
|
|
|
|
e.mu.Lock()
|
2018-08-07 14:56:08 +00:00
|
|
|
e.shutdownFlags = tcpip.ShutdownRead | tcpip.ShutdownWrite
|
2018-05-02 05:50:55 +00:00
|
|
|
switch e.state {
|
|
|
|
case stateBound, stateConnected:
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stack.UnregisterTransportEndpoint(e.RegisterNICID, []tcpip.NetworkProtocolNumber{e.NetProto}, e.TransProto, e.ID, e, 0 /* bindToDevice */)
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the receive list and drain it.
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
e.rcvClosed = true
|
|
|
|
e.rcvBufSize = 0
|
|
|
|
for !e.rcvList.Empty() {
|
|
|
|
p := e.rcvList.Front()
|
|
|
|
e.rcvList.Remove(p)
|
|
|
|
}
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
|
|
|
|
e.route.Release()
|
|
|
|
|
|
|
|
// Update the state.
|
|
|
|
e.state = stateClosed
|
2018-07-10 04:19:58 +00:00
|
|
|
|
|
|
|
e.mu.Unlock()
|
|
|
|
|
|
|
|
e.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.EventIn | waiter.EventOut)
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 05:26:59 +00:00
|
|
|
// ModerateRecvBuf implements tcpip.Endpoint.ModerateRecvBuf.
|
|
|
|
func (e *endpoint) ModerateRecvBuf(copied int) {}
|
|
|
|
|
2019-08-02 23:25:34 +00:00
|
|
|
// IPTables implements tcpip.Endpoint.IPTables.
|
|
|
|
func (e *endpoint) IPTables() (iptables.IPTables, error) {
|
|
|
|
return e.stack.IPTables(), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
// Read reads data from the endpoint. This method does not block if
|
|
|
|
// there is no data pending.
|
|
|
|
func (e *endpoint) Read(addr *tcpip.FullAddress) (buffer.View, tcpip.ControlMessages, *tcpip.Error) {
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
|
|
|
|
if e.rcvList.Empty() {
|
|
|
|
err := tcpip.ErrWouldBlock
|
|
|
|
if e.rcvClosed {
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stats.ReadErrors.ReadClosed.Increment()
|
2018-05-02 05:50:55 +00:00
|
|
|
err = tcpip.ErrClosedForReceive
|
|
|
|
}
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
return buffer.View{}, tcpip.ControlMessages{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := e.rcvList.Front()
|
|
|
|
e.rcvList.Remove(p)
|
|
|
|
e.rcvBufSize -= p.data.Size()
|
|
|
|
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
|
|
|
|
if addr != nil {
|
|
|
|
*addr = p.senderAddress
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:17:51 +00:00
|
|
|
return p.data.ToView(), tcpip.ControlMessages{HasTimestamp: true, Timestamp: p.timestamp}, nil
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareForWrite prepares the endpoint for sending data. In particular, it
|
|
|
|
// binds it if it's still in the initial state. To do so, it must first
|
|
|
|
// reacquire the mutex in exclusive mode.
|
|
|
|
//
|
|
|
|
// Returns true for retry if preparation should be retried.
|
|
|
|
func (e *endpoint) prepareForWrite(to *tcpip.FullAddress) (retry bool, err *tcpip.Error) {
|
|
|
|
switch e.state {
|
|
|
|
case stateInitial:
|
|
|
|
case stateConnected:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
case stateBound:
|
|
|
|
if to == nil {
|
|
|
|
return false, tcpip.ErrDestinationRequired
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
|
|
|
e.mu.RUnlock()
|
|
|
|
defer e.mu.RLock()
|
|
|
|
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
// The state changed when we released the shared locked and re-acquired
|
|
|
|
// it in exclusive mode. Try again.
|
|
|
|
if e.state != stateInitial {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The state is still 'initial', so try to bind the endpoint.
|
2019-03-05 22:52:35 +00:00
|
|
|
if err := e.bindLocked(tcpip.FullAddress{}); err != nil {
|
2018-05-02 05:50:55 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes data to the endpoint's peer. This method does not block
|
|
|
|
// if the data cannot be written.
|
2019-09-13 00:42:14 +00:00
|
|
|
func (e *endpoint) Write(p tcpip.Payloader, opts tcpip.WriteOptions) (int64, <-chan struct{}, *tcpip.Error) {
|
2019-10-10 00:54:51 +00:00
|
|
|
n, ch, err := e.write(p, opts)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
e.stats.PacketsSent.Increment()
|
|
|
|
case tcpip.ErrMessageTooLong, tcpip.ErrInvalidOptionValue:
|
|
|
|
e.stats.WriteErrors.InvalidArgs.Increment()
|
|
|
|
case tcpip.ErrClosedForSend:
|
|
|
|
e.stats.WriteErrors.WriteClosed.Increment()
|
|
|
|
case tcpip.ErrInvalidEndpointState:
|
|
|
|
e.stats.WriteErrors.InvalidEndpointState.Increment()
|
|
|
|
case tcpip.ErrNoLinkAddress:
|
|
|
|
e.stats.SendErrors.NoLinkAddr.Increment()
|
|
|
|
case tcpip.ErrNoRoute, tcpip.ErrBroadcastDisabled, tcpip.ErrNetworkUnreachable:
|
|
|
|
// Errors indicating any problem with IP routing of the packet.
|
|
|
|
e.stats.SendErrors.NoRoute.Increment()
|
|
|
|
default:
|
|
|
|
// For all other errors when writing to the network layer.
|
|
|
|
e.stats.SendErrors.SendToNetworkFailed.Increment()
|
|
|
|
}
|
|
|
|
return n, ch, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *endpoint) write(p tcpip.Payloader, opts tcpip.WriteOptions) (int64, <-chan struct{}, *tcpip.Error) {
|
2018-05-02 05:50:55 +00:00
|
|
|
// MSG_MORE is unimplemented. (This also means that MSG_EOR is a no-op.)
|
|
|
|
if opts.More {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, tcpip.ErrInvalidOptionValue
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
to := opts.To
|
|
|
|
|
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
2018-08-07 14:56:08 +00:00
|
|
|
// If we've shutdown with SHUT_WR we are in an invalid state for sending.
|
|
|
|
if e.shutdownFlags&tcpip.ShutdownWrite != 0 {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, tcpip.ErrClosedForSend
|
2018-08-07 14:56:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
// Prepare for write.
|
|
|
|
for {
|
|
|
|
retry, err := e.prepareForWrite(to)
|
|
|
|
if err != nil {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, err
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var route *stack.Route
|
|
|
|
if to == nil {
|
|
|
|
route = &e.route
|
|
|
|
|
|
|
|
if route.IsResolutionRequired() {
|
2019-04-02 18:12:29 +00:00
|
|
|
// Promote lock to exclusive if using a shared route,
|
|
|
|
// given that it may need to change in Route.Resolve()
|
|
|
|
// call below.
|
2018-05-02 05:50:55 +00:00
|
|
|
e.mu.RUnlock()
|
|
|
|
defer e.mu.RLock()
|
|
|
|
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
// Recheck state after lock was re-acquired.
|
|
|
|
if e.state != stateConnected {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, tcpip.ErrInvalidEndpointState
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Reject destination address if it goes through a different
|
|
|
|
// NIC than the endpoint was bound to.
|
|
|
|
nicid := to.NIC
|
2019-10-10 00:54:51 +00:00
|
|
|
if e.BindNICID != 0 {
|
|
|
|
if nicid != 0 && nicid != e.BindNICID {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, tcpip.ErrNoRoute
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
nicid = e.BindNICID
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toCopy := *to
|
|
|
|
to = &toCopy
|
|
|
|
netProto, err := e.checkV4Mapped(to, true)
|
|
|
|
if err != nil {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, err
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the enpoint.
|
2019-10-10 00:54:51 +00:00
|
|
|
r, err := e.stack.FindRoute(nicid, e.BindAddr, to.Addr, netProto, false /* multicastLoop */)
|
2018-05-02 05:50:55 +00:00
|
|
|
if err != nil {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, err
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
defer r.Release()
|
|
|
|
|
|
|
|
route = &r
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.IsResolutionRequired() {
|
2019-05-24 19:28:15 +00:00
|
|
|
if ch, err := route.Resolve(nil); err != nil {
|
2018-05-02 05:50:55 +00:00
|
|
|
if err == tcpip.ErrWouldBlock {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, ch, tcpip.ErrNoLinkAddress
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, err
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 00:42:14 +00:00
|
|
|
v, err := p.FullPayload()
|
2018-05-02 05:50:55 +00:00
|
|
|
if err != nil {
|
2018-09-28 17:59:21 +00:00
|
|
|
return 0, nil, err
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
switch e.NetProto {
|
2018-05-02 05:50:55 +00:00
|
|
|
case header.IPv4ProtocolNumber:
|
2019-10-10 00:54:51 +00:00
|
|
|
err = send4(route, e.ID.LocalPort, v, e.ttl)
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
case header.IPv6ProtocolNumber:
|
2019-10-10 00:54:51 +00:00
|
|
|
err = send6(route, e.ID.LocalPort, v, e.ttl)
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 19:40:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-14 23:04:41 +00:00
|
|
|
return int64(len(v)), nil, nil
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peek only returns data from a single datagram, so do nothing here.
|
2019-08-14 23:04:41 +00:00
|
|
|
func (e *endpoint) Peek([][]byte) (int64, tcpip.ControlMessages, *tcpip.Error) {
|
2018-05-02 05:50:55 +00:00
|
|
|
return 0, tcpip.ControlMessages{}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-08 02:28:26 +00:00
|
|
|
// SetSockOpt sets a socket option.
|
2018-05-02 05:50:55 +00:00
|
|
|
func (e *endpoint) SetSockOpt(opt interface{}) *tcpip.Error {
|
2019-10-08 02:28:26 +00:00
|
|
|
switch o := opt.(type) {
|
|
|
|
case tcpip.TTLOption:
|
|
|
|
e.mu.Lock()
|
|
|
|
e.ttl = uint8(o)
|
|
|
|
e.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-23 21:37:39 +00:00
|
|
|
// SetSockOptInt sets a socket option. Currently not supported.
|
|
|
|
func (e *endpoint) SetSockOptInt(opt tcpip.SockOpt, v int) *tcpip.Error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 22:39:47 +00:00
|
|
|
// GetSockOptInt implements tcpip.Endpoint.GetSockOptInt.
|
|
|
|
func (e *endpoint) GetSockOptInt(opt tcpip.SockOpt) (int, *tcpip.Error) {
|
|
|
|
switch opt {
|
|
|
|
case tcpip.ReceiveQueueSizeOption:
|
|
|
|
v := 0
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
if !e.rcvList.Empty() {
|
|
|
|
p := e.rcvList.Front()
|
|
|
|
v = p.data.Size()
|
|
|
|
}
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
return v, nil
|
2019-09-23 21:37:39 +00:00
|
|
|
case tcpip.SendBufferSizeOption:
|
|
|
|
e.mu.Lock()
|
|
|
|
v := e.sndBufSize
|
|
|
|
e.mu.Unlock()
|
|
|
|
return v, nil
|
|
|
|
|
|
|
|
case tcpip.ReceiveBufferSizeOption:
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
v := e.rcvBufSizeMax
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
return v, nil
|
|
|
|
|
2019-07-18 22:39:47 +00:00
|
|
|
}
|
|
|
|
return -1, tcpip.ErrUnknownProtocolOption
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
// GetSockOpt implements tcpip.Endpoint.GetSockOpt.
|
|
|
|
func (e *endpoint) GetSockOpt(opt interface{}) *tcpip.Error {
|
|
|
|
switch o := opt.(type) {
|
|
|
|
case tcpip.ErrorOption:
|
|
|
|
return nil
|
|
|
|
|
2018-12-21 21:12:32 +00:00
|
|
|
case *tcpip.KeepaliveEnabledOption:
|
|
|
|
*o = 0
|
|
|
|
return nil
|
|
|
|
|
2019-10-08 02:28:26 +00:00
|
|
|
case *tcpip.TTLOption:
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
*o = tcpip.TTLOption(e.ttl)
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
return nil
|
|
|
|
|
2018-12-21 21:12:32 +00:00
|
|
|
default:
|
|
|
|
return tcpip.ErrUnknownProtocolOption
|
|
|
|
}
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 02:28:26 +00:00
|
|
|
func send4(r *stack.Route, ident uint16, data buffer.View, ttl uint8) *tcpip.Error {
|
2019-07-16 20:02:30 +00:00
|
|
|
if len(data) < header.ICMPv4MinimumSize {
|
2018-05-02 05:50:55 +00:00
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
2019-07-16 20:02:30 +00:00
|
|
|
hdr := buffer.NewPrependable(header.ICMPv4MinimumSize + int(r.MaxHeaderLength()))
|
2018-05-02 05:50:55 +00:00
|
|
|
|
2019-07-16 20:02:30 +00:00
|
|
|
icmpv4 := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize))
|
2018-05-02 05:50:55 +00:00
|
|
|
copy(icmpv4, data)
|
2019-09-03 22:59:58 +00:00
|
|
|
// Set the ident to the user-specified port. Sequence number should
|
|
|
|
// already be set by the user.
|
|
|
|
icmpv4.SetIdent(ident)
|
2019-07-16 20:02:30 +00:00
|
|
|
data = data[header.ICMPv4MinimumSize:]
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
// Linux performs these basic checks.
|
|
|
|
if icmpv4.Type() != header.ICMPv4Echo || icmpv4.Code() != 0 {
|
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
|
|
|
icmpv4.SetChecksum(0)
|
|
|
|
icmpv4.SetChecksum(^header.Checksum(icmpv4, header.Checksum(data, 0)))
|
|
|
|
|
2019-10-15 00:45:29 +00:00
|
|
|
if ttl == 0 {
|
|
|
|
ttl = r.DefaultTTL()
|
|
|
|
}
|
|
|
|
return r.WritePacket(nil /* gso */, hdr, data.ToVectorisedView(), stack.NetworkHeaderParams{Protocol: header.ICMPv4ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS})
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 02:28:26 +00:00
|
|
|
func send6(r *stack.Route, ident uint16, data buffer.View, ttl uint8) *tcpip.Error {
|
2018-09-04 21:30:15 +00:00
|
|
|
if len(data) < header.ICMPv6EchoMinimumSize {
|
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
2019-09-03 22:59:58 +00:00
|
|
|
hdr := buffer.NewPrependable(header.ICMPv6MinimumSize + int(r.MaxHeaderLength()))
|
2018-09-04 21:30:15 +00:00
|
|
|
|
2019-09-03 22:59:58 +00:00
|
|
|
icmpv6 := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize))
|
2018-09-04 21:30:15 +00:00
|
|
|
copy(icmpv6, data)
|
2019-09-03 22:59:58 +00:00
|
|
|
// Set the ident. Sequence number is provided by the user.
|
|
|
|
icmpv6.SetIdent(ident)
|
|
|
|
data = data[header.ICMPv6MinimumSize:]
|
2018-09-04 21:30:15 +00:00
|
|
|
|
|
|
|
if icmpv6.Type() != header.ICMPv6EchoRequest || icmpv6.Code() != 0 {
|
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:05:31 +00:00
|
|
|
dataVV := data.ToVectorisedView()
|
|
|
|
icmpv6.SetChecksum(header.ICMPv6Checksum(icmpv6, r.LocalAddress, r.RemoteAddress, dataVV))
|
2018-09-13 03:38:27 +00:00
|
|
|
|
2019-10-15 00:45:29 +00:00
|
|
|
if ttl == 0 {
|
|
|
|
ttl = r.DefaultTTL()
|
|
|
|
}
|
2019-10-25 23:05:31 +00:00
|
|
|
return r.WritePacket(nil /* gso */, hdr, dataVV, stack.NetworkHeaderParams{Protocol: header.ICMPv6ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS})
|
2018-09-04 21:30:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
func (e *endpoint) checkV4Mapped(addr *tcpip.FullAddress, allowMismatch bool) (tcpip.NetworkProtocolNumber, *tcpip.Error) {
|
2019-10-10 00:54:51 +00:00
|
|
|
netProto := e.NetProto
|
2018-05-02 05:50:55 +00:00
|
|
|
if header.IsV4MappedAddress(addr.Addr) {
|
|
|
|
return 0, tcpip.ErrNoRoute
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if we're bound to an address length different from the one we're
|
|
|
|
// checking.
|
2019-10-10 00:54:51 +00:00
|
|
|
if l := len(e.ID.LocalAddress); !allowMismatch && l != 0 && l != len(addr.Addr) {
|
2018-05-02 05:50:55 +00:00
|
|
|
return 0, tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
|
|
|
return netProto, nil
|
|
|
|
}
|
|
|
|
|
2019-08-17 02:30:59 +00:00
|
|
|
// Disconnect implements tcpip.Endpoint.Disconnect.
|
|
|
|
func (*endpoint) Disconnect() *tcpip.Error {
|
|
|
|
return tcpip.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
// Connect connects the endpoint to its peer. Specifying a NIC is optional.
|
|
|
|
func (e *endpoint) Connect(addr tcpip.FullAddress) *tcpip.Error {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
nicid := addr.NIC
|
|
|
|
localPort := uint16(0)
|
|
|
|
switch e.state {
|
|
|
|
case stateBound, stateConnected:
|
2019-10-10 00:54:51 +00:00
|
|
|
localPort = e.ID.LocalPort
|
|
|
|
if e.BindNICID == 0 {
|
2018-05-02 05:50:55 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
if nicid != 0 && nicid != e.BindNICID {
|
2018-05-02 05:50:55 +00:00
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
nicid = e.BindNICID
|
2018-05-02 05:50:55 +00:00
|
|
|
default:
|
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
|
|
|
netProto, err := e.checkV4Mapped(&addr, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a route to the desired destination.
|
2019-10-10 00:54:51 +00:00
|
|
|
r, err := e.stack.FindRoute(nicid, e.BindAddr, addr.Addr, netProto, false /* multicastLoop */)
|
2018-05-02 05:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.Release()
|
|
|
|
|
|
|
|
id := stack.TransportEndpointID{
|
|
|
|
LocalAddress: r.LocalAddress,
|
|
|
|
LocalPort: localPort,
|
|
|
|
RemoteAddress: r.RemoteAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if we're connected, this endpoint can still be used to send
|
|
|
|
// packets on a different network protocol, so we register both even if
|
|
|
|
// v6only is set to false and this is an ipv6 endpoint.
|
|
|
|
netProtos := []tcpip.NetworkProtocolNumber{netProto}
|
|
|
|
|
|
|
|
id, err = e.registerWithStack(nicid, netProtos, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
e.ID = id
|
2018-05-02 05:50:55 +00:00
|
|
|
e.route = r.Clone()
|
2019-10-10 00:54:51 +00:00
|
|
|
e.RegisterNICID = nicid
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
e.state = stateConnected
|
|
|
|
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
e.rcvReady = true
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectEndpoint is not supported.
|
|
|
|
func (*endpoint) ConnectEndpoint(tcpip.Endpoint) *tcpip.Error {
|
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown closes the read and/or write end of the endpoint connection
|
|
|
|
// to its peer.
|
|
|
|
func (e *endpoint) Shutdown(flags tcpip.ShutdownFlags) *tcpip.Error {
|
2018-08-07 14:56:08 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
e.shutdownFlags |= flags
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
if e.state != stateConnected {
|
|
|
|
return tcpip.ErrNotConnected
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags&tcpip.ShutdownRead != 0 {
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
wasClosed := e.rcvClosed
|
|
|
|
e.rcvClosed = true
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
|
|
|
|
if !wasClosed {
|
|
|
|
e.waiterQueue.Notify(waiter.EventIn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen is not supported by UDP, it just fails.
|
|
|
|
func (*endpoint) Listen(int) *tcpip.Error {
|
|
|
|
return tcpip.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept is not supported by UDP, it just fails.
|
|
|
|
func (*endpoint) Accept() (tcpip.Endpoint, *waiter.Queue, *tcpip.Error) {
|
|
|
|
return nil, nil, tcpip.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *endpoint) registerWithStack(nicid tcpip.NICID, netProtos []tcpip.NetworkProtocolNumber, id stack.TransportEndpointID) (stack.TransportEndpointID, *tcpip.Error) {
|
|
|
|
if id.LocalPort != 0 {
|
|
|
|
// The endpoint already has a local port, just attempt to
|
|
|
|
// register it.
|
2019-10-10 00:54:51 +00:00
|
|
|
err := e.stack.RegisterTransportEndpoint(nicid, netProtos, e.TransProto, id, e, false /* reuse */, 0 /* bindToDevice */)
|
2018-05-02 05:50:55 +00:00
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to find a port for the endpoint.
|
|
|
|
_, err := e.stack.PickEphemeralPort(func(p uint16) (bool, *tcpip.Error) {
|
|
|
|
id.LocalPort = p
|
2019-10-10 00:54:51 +00:00
|
|
|
err := e.stack.RegisterTransportEndpoint(nicid, netProtos, e.TransProto, id, e, false /* reuse */, 0 /* bindtodevice */)
|
2018-05-02 05:50:55 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return true, nil
|
|
|
|
case tcpip.ErrPortInUse:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:52:35 +00:00
|
|
|
func (e *endpoint) bindLocked(addr tcpip.FullAddress) *tcpip.Error {
|
2018-05-02 05:50:55 +00:00
|
|
|
// Don't allow binding once endpoint is not in the initial state
|
|
|
|
// anymore.
|
|
|
|
if e.state != stateInitial {
|
|
|
|
return tcpip.ErrInvalidEndpointState
|
|
|
|
}
|
|
|
|
|
|
|
|
netProto, err := e.checkV4Mapped(&addr, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand netProtos to include v4 and v6 if the caller is binding to a
|
|
|
|
// wildcard (empty) address, and this is an IPv6 endpoint with v6only
|
|
|
|
// set to false.
|
|
|
|
netProtos := []tcpip.NetworkProtocolNumber{netProto}
|
|
|
|
|
|
|
|
if len(addr.Addr) != 0 {
|
|
|
|
// A local address was specified, verify that it's valid.
|
|
|
|
if e.stack.CheckLocalAddress(addr.NIC, netProto, addr.Addr) == 0 {
|
|
|
|
return tcpip.ErrBadLocalAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id := stack.TransportEndpointID{
|
|
|
|
LocalPort: addr.Port,
|
|
|
|
LocalAddress: addr.Addr,
|
|
|
|
}
|
|
|
|
id, err = e.registerWithStack(addr.NIC, netProtos, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
e.ID = id
|
|
|
|
e.RegisterNICID = addr.NIC
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
// Mark endpoint as bound.
|
|
|
|
e.state = stateBound
|
|
|
|
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
e.rcvReady = true
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind binds the endpoint to a specific local address and port.
|
|
|
|
// Specifying a NIC is optional.
|
2019-03-05 22:52:35 +00:00
|
|
|
func (e *endpoint) Bind(addr tcpip.FullAddress) *tcpip.Error {
|
2018-05-02 05:50:55 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
2019-03-05 22:52:35 +00:00
|
|
|
err := e.bindLocked(addr)
|
2018-05-02 05:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:54:51 +00:00
|
|
|
e.BindNICID = addr.NIC
|
|
|
|
e.BindAddr = addr.Addr
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLocalAddress returns the address to which the endpoint is bound.
|
|
|
|
func (e *endpoint) GetLocalAddress() (tcpip.FullAddress, *tcpip.Error) {
|
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
|
|
|
return tcpip.FullAddress{
|
2019-10-10 00:54:51 +00:00
|
|
|
NIC: e.RegisterNICID,
|
|
|
|
Addr: e.ID.LocalAddress,
|
|
|
|
Port: e.ID.LocalPort,
|
2018-05-02 05:50:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteAddress returns the address to which the endpoint is connected.
|
|
|
|
func (e *endpoint) GetRemoteAddress() (tcpip.FullAddress, *tcpip.Error) {
|
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
|
|
|
if e.state != stateConnected {
|
|
|
|
return tcpip.FullAddress{}, tcpip.ErrNotConnected
|
|
|
|
}
|
|
|
|
|
|
|
|
return tcpip.FullAddress{
|
2019-10-10 00:54:51 +00:00
|
|
|
NIC: e.RegisterNICID,
|
|
|
|
Addr: e.ID.RemoteAddress,
|
|
|
|
Port: e.ID.RemotePort,
|
2018-05-02 05:50:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readiness returns the current readiness of the endpoint. For example, if
|
|
|
|
// waiter.EventIn is set, the endpoint is immediately readable.
|
|
|
|
func (e *endpoint) Readiness(mask waiter.EventMask) waiter.EventMask {
|
|
|
|
// The endpoint is always writable.
|
|
|
|
result := waiter.EventOut & mask
|
|
|
|
|
|
|
|
// Determine if the endpoint is readable if requested.
|
|
|
|
if (mask & waiter.EventIn) != 0 {
|
|
|
|
e.rcvMu.Lock()
|
|
|
|
if !e.rcvList.Empty() || e.rcvClosed {
|
|
|
|
result |= waiter.EventIn
|
|
|
|
}
|
|
|
|
e.rcvMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandlePacket is called by the stack when new packets arrive to this transport
|
|
|
|
// endpoint.
|
2019-04-02 18:12:29 +00:00
|
|
|
func (e *endpoint) HandlePacket(r *stack.Route, id stack.TransportEndpointID, vv buffer.VectorisedView) {
|
2019-05-05 23:06:11 +00:00
|
|
|
// Only accept echo replies.
|
2019-10-10 00:54:51 +00:00
|
|
|
switch e.NetProto {
|
2019-05-05 23:06:11 +00:00
|
|
|
case header.IPv4ProtocolNumber:
|
|
|
|
h := header.ICMPv4(vv.First())
|
|
|
|
if h.Type() != header.ICMPv4EchoReply {
|
|
|
|
e.stack.Stats().DroppedPackets.Increment()
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stats.ReceiveErrors.MalformedPacketsReceived.Increment()
|
2019-05-05 23:06:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case header.IPv6ProtocolNumber:
|
|
|
|
h := header.ICMPv6(vv.First())
|
|
|
|
if h.Type() != header.ICMPv6EchoReply {
|
|
|
|
e.stack.Stats().DroppedPackets.Increment()
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stats.ReceiveErrors.MalformedPacketsReceived.Increment()
|
2019-05-05 23:06:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
e.rcvMu.Lock()
|
|
|
|
|
|
|
|
// Drop the packet if our buffer is currently full.
|
2019-10-10 00:54:51 +00:00
|
|
|
if !e.rcvReady || e.rcvClosed {
|
|
|
|
e.rcvMu.Unlock()
|
2019-04-02 18:12:29 +00:00
|
|
|
e.stack.Stats().DroppedPackets.Increment()
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stats.ReceiveErrors.ClosedReceiver.Increment()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.rcvBufSize >= e.rcvBufSizeMax {
|
2018-05-02 05:50:55 +00:00
|
|
|
e.rcvMu.Unlock()
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stack.Stats().DroppedPackets.Increment()
|
|
|
|
e.stats.ReceiveErrors.ReceiveBufferOverflow.Increment()
|
2018-05-02 05:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wasEmpty := e.rcvBufSize == 0
|
|
|
|
|
|
|
|
// Push new packet into receive list and increment the buffer size.
|
2019-02-22 21:33:49 +00:00
|
|
|
pkt := &icmpPacket{
|
2018-05-02 05:50:55 +00:00
|
|
|
senderAddress: tcpip.FullAddress{
|
|
|
|
NIC: r.NICID(),
|
|
|
|
Addr: id.RemoteAddress,
|
|
|
|
},
|
|
|
|
}
|
2019-02-27 22:30:20 +00:00
|
|
|
|
2019-04-02 18:12:29 +00:00
|
|
|
pkt.data = vv.Clone(pkt.views[:])
|
2019-02-27 22:30:20 +00:00
|
|
|
|
2018-05-02 05:50:55 +00:00
|
|
|
e.rcvList.PushBack(pkt)
|
2019-02-27 22:30:20 +00:00
|
|
|
e.rcvBufSize += pkt.data.Size()
|
2018-05-02 05:50:55 +00:00
|
|
|
|
2019-02-15 19:17:51 +00:00
|
|
|
pkt.timestamp = e.stack.NowNanoseconds()
|
2018-05-02 05:50:55 +00:00
|
|
|
|
|
|
|
e.rcvMu.Unlock()
|
2019-10-10 00:54:51 +00:00
|
|
|
e.stats.PacketsReceived.Increment()
|
2018-05-02 05:50:55 +00:00
|
|
|
// Notify any waiters that there's data to be read now.
|
|
|
|
if wasEmpty {
|
|
|
|
e.waiterQueue.Notify(waiter.EventIn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleControlPacket implements stack.TransportEndpoint.HandleControlPacket.
|
2018-09-13 04:57:04 +00:00
|
|
|
func (e *endpoint) HandleControlPacket(id stack.TransportEndpointID, typ stack.ControlType, extra uint32, vv buffer.VectorisedView) {
|
2018-05-02 05:50:55 +00:00
|
|
|
}
|
2019-06-06 22:03:44 +00:00
|
|
|
|
|
|
|
// State implements tcpip.Endpoint.State. The ICMP endpoint currently doesn't
|
|
|
|
// expose internal socket state.
|
|
|
|
func (e *endpoint) State() uint32 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-10-10 00:54:51 +00:00
|
|
|
|
|
|
|
// Info returns a copy of the endpoint info.
|
|
|
|
func (e *endpoint) Info() tcpip.EndpointInfo {
|
|
|
|
e.mu.RLock()
|
|
|
|
// Make a copy of the endpoint info.
|
|
|
|
ret := e.TransportEndpointInfo
|
|
|
|
e.mu.RUnlock()
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stats returns a pointer to the endpoint stats.
|
|
|
|
func (e *endpoint) Stats() tcpip.EndpointStats {
|
|
|
|
return &e.stats
|
|
|
|
}
|
2019-10-29 18:19:04 +00:00
|
|
|
|
|
|
|
// Wait implements stack.TransportEndpoint.Wait.
|
|
|
|
func (*endpoint) Wait() {}
|