diff --git a/pkg/tcpip/link/channel/channel.go b/pkg/tcpip/link/channel/channel.go index e8c9d4374..104a35964 100644 --- a/pkg/tcpip/link/channel/channel.go +++ b/pkg/tcpip/link/channel/channel.go @@ -192,12 +192,15 @@ func (e *Endpoint) NumQueued() int { return e.q.Num() } -// InjectInbound injects an inbound packet. +// InjectInbound injects an inbound packet. If the endpoint is not attached, the +// packet is not delivered. func (e *Endpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) { e.mu.RLock() d := e.dispatcher e.mu.RUnlock() - d.DeliverNetworkPacket(protocol, pkt) + if d != nil { + d.DeliverNetworkPacket(protocol, pkt) + } } // Attach saves the stack network-layer dispatcher for use later when packets diff --git a/pkg/tcpip/link/fdbased/endpoint.go b/pkg/tcpip/link/fdbased/endpoint.go index adc97926c..2491d6cd6 100644 --- a/pkg/tcpip/link/fdbased/endpoint.go +++ b/pkg/tcpip/link/fdbased/endpoint.go @@ -785,12 +785,15 @@ func (e *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) { e.dispatcher = dispatcher } -// InjectInbound injects an inbound packet. +// InjectInbound injects an inbound packet. If the endpoint is not attached, the +// packet is not delivered. func (e *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) { e.mu.RLock() d := e.dispatcher e.mu.RUnlock() - d.DeliverNetworkPacket(protocol, pkt) + if d != nil { + d.DeliverNetworkPacket(protocol, pkt) + } } // NewInjectable creates a new fd-based InjectableEndpoint. diff --git a/pkg/tcpip/link/loopback/loopback.go b/pkg/tcpip/link/loopback/loopback.go index d3fb48dfc..85089e4e6 100644 --- a/pkg/tcpip/link/loopback/loopback.go +++ b/pkg/tcpip/link/loopback/loopback.go @@ -81,7 +81,8 @@ func (*endpoint) LinkAddress() tcpip.LinkAddress { // Wait implements stack.LinkEndpoint.Wait. func (*endpoint) Wait() {} -// WritePackets implements stack.LinkEndpoint.WritePackets. +// WritePackets implements stack.LinkEndpoint.WritePackets. If the endpoint is +// not attached, the packets are not delivered. func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) { e.mu.RLock() d := e.dispatcher @@ -93,7 +94,9 @@ func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ Payload: pkt.ToBuffer(), }) - d.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt) + if d != nil { + d.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt) + } newPkt.DecRef() } return pkts.Len(), nil diff --git a/pkg/tcpip/link/waitable/waitable.go b/pkg/tcpip/link/waitable/waitable.go index ec08bf805..4000c06f9 100644 --- a/pkg/tcpip/link/waitable/waitable.go +++ b/pkg/tcpip/link/waitable/waitable.go @@ -63,7 +63,9 @@ func (e *Endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pk e.mu.RLock() d := e.dispatcher e.mu.RUnlock() - d.DeliverNetworkPacket(protocol, pkt) + if d != nil { + d.DeliverNetworkPacket(protocol, pkt) + } e.dispatchGate.Leave() } @@ -75,7 +77,9 @@ func (e *Endpoint) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt s e.mu.RLock() d := e.dispatcher e.mu.RUnlock() - d.DeliverLinkPacket(protocol, pkt) + if d != nil { + d.DeliverLinkPacket(protocol, pkt) + } e.dispatchGate.Leave() }