tcpip/link: avoid attempts to deliver packets to nil dispatchers

Fixes #8765
This commit is contained in:
James Tucker
2023-03-30 13:25:02 -07:00
parent fcf93de622
commit a13a283303
4 changed files with 21 additions and 8 deletions
+5 -2
View File
@@ -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
+5 -2
View File
@@ -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.
+5 -2
View File
@@ -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
+6 -2
View File
@@ -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()
}