Skip to content

Commit 936ad90

Browse files
committed
Forward queued TCP segments after reopening TIME_WAIT
A forwarder can accept a reopening SYN before its application registers the new endpoint. Ordinary endpoint teardown can reset a duplicate SYN from that connection before the forwarder can finish accepting it. Offer the saved SYN first and retain the handler that accepted it as the fallback for queued segments without a registered destination. A listener or a handler that declines the SYN keeps the usual drain behavior. Other close paths never call the handler under their locks.
1 parent ed8a800 commit 936ad90

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎pkg/tcpip/transport/tcp/connect.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,9 @@ func (e *Endpoint) transitionToStateCloseLocked() {
11621162
// tryDeliverSegmentFromClosedEndpoint attempts to deliver the parsed
11631163
// segment to any other endpoint other than the current one. This is called
11641164
// only when the endpoint is in StateClose and we want to deliver the segment
1165-
// to any other listening endpoint. We reply with RST if we cannot find one.
1166-
func (e *Endpoint) tryDeliverSegmentFromClosedEndpoint(s *segment) {
1165+
// to any other listening endpoint. If none exists, offer it to fallback before
1166+
// sending a reset. Callers holding endpoint locks must pass nil.
1167+
func (e *Endpoint) tryDeliverSegmentFromClosedEndpoint(s *segment, fallback func(stack.TransportEndpointID, *stack.PacketBuffer) bool) {
11671168
ep := e.stack.FindTransportEndpoint(e.NetProto, e.TransProto, e.TransportEndpointInfo.ID, s.pkt.NICID)
11681169
if ep == nil && e.NetProto == header.IPv6ProtocolNumber && e.TransportEndpointInfo.ID.LocalAddress.To4() != (tcpip.Address{}) {
11691170
// Dual-stack socket, try IPv4.
@@ -1175,6 +1176,9 @@ func (e *Endpoint) tryDeliverSegmentFromClosedEndpoint(s *segment) {
11751176
)
11761177
}
11771178
if ep == nil {
1179+
if fallback != nil && fallback(s.id, s.pkt) {
1180+
return
1181+
}
11781182
if !s.flags.Contains(header.TCPFlagRst) {
11791183
replyWithReset(e.stack, s, stack.DefaultTOS, tcpip.UseDefaultIPv4TTL, tcpip.UseDefaultIPv6HopLimit)
11801184
}
@@ -1196,13 +1200,21 @@ func (e *Endpoint) tryDeliverSegmentFromClosedEndpoint(s *segment) {
11961200
//
11971201
// +checklocksexclude:e.segmentQueue.mu
11981202
func (e *Endpoint) drainClosingSegmentQueue() {
1203+
e.drainClosingSegmentQueueWithHandler(nil)
1204+
}
1205+
1206+
// Only the unlocked TIME_WAIT handoff supplies a fallback: it has already
1207+
// accepted the opening SYN, but may not have registered an endpoint yet.
1208+
//
1209+
// +checklocksexclude:e.segmentQueue.mu
1210+
func (e *Endpoint) drainClosingSegmentQueueWithHandler(fallback func(stack.TransportEndpointID, *stack.PacketBuffer) bool) {
11991211
for {
12001212
s := e.segmentQueue.dequeue()
12011213
if s == nil {
12021214
break
12031215
}
12041216

1205-
e.tryDeliverSegmentFromClosedEndpoint(s)
1217+
e.tryDeliverSegmentFromClosedEndpoint(s, fallback)
12061218
s.DecRef()
12071219
}
12081220
}
@@ -1528,6 +1540,8 @@ func (e *Endpoint) handleTimeWaitSegments() (extendTimeWait bool, reuseTW func()
15281540
tcpEP := listenEP.(*Endpoint)
15291541
if EndpointState(tcpEP.State()) == StateListen {
15301542
reuseTW = func() {
1543+
e.drainClosingSegmentQueue()
1544+
e.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.ReadableEvents | waiter.WritableEvents)
15311545
if !tcpEP.enqueueSegment(s) {
15321546
return
15331547
}
@@ -1549,6 +1563,7 @@ func (e *Endpoint) handleTimeWaitSegments() (extendTimeWait bool, reuseTW func()
15491563
// for the rest of the TIME-WAIT period.
15501564
if handler := e.stack.TransportProtocolDefaultHandler(info.TransProto); handler != nil {
15511565
reuseTW = func() {
1566+
e.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.ReadableEvents | waiter.WritableEvents)
15521567
if !handler(s.id, s.pkt) {
15531568
// Mirror the normal (non-TIME-WAIT) delivery path in
15541569
// nic.DeliverTransportPacket: a default handler that
@@ -1557,8 +1572,10 @@ func (e *Endpoint) handleTimeWaitSegments() (extendTimeWait bool, reuseTW func()
15571572
if !s.flags.Contains(header.TCPFlagRst) {
15581573
replyWithReset(e.stack, s, stack.DefaultTOS, tcpip.UseDefaultIPv4TTL, tcpip.UseDefaultIPv6HopLimit)
15591574
}
1575+
handler = nil
15601576
}
15611577
s.DecRef()
1578+
e.drainClosingSegmentQueueWithHandler(handler)
15621579
}
15631580
// We explicitly do not DecRef the segment as it's still valid and
15641581
// being reflected to the default handler.

‎pkg/tcpip/transport/tcp/dispatcher.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ func handleTimeWait(ep *Endpoint) {
266266
if reuseTW != nil {
267267
ep.transitionToStateCloseLocked()
268268
ep.mu.Unlock()
269-
ep.drainClosingSegmentQueue()
270-
ep.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.ReadableEvents | waiter.WritableEvents)
271269
reuseTW()
272270
return
273271
}

0 commit comments

Comments
 (0)