| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package tcp |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/binary" |
| 19 |
"math" |
| 20 |
"time" |
| 21 |
|
| 22 |
"inet.af/netstack/sleep" |
| 23 |
"inet.af/netstack/sync" |
| 24 |
"inet.af/netstack/tcpip" |
| 25 |
"inet.af/netstack/tcpip/buffer" |
| 26 |
"inet.af/netstack/tcpip/hash/jenkins" |
| 27 |
"inet.af/netstack/tcpip/header" |
| 28 |
"inet.af/netstack/tcpip/seqnum" |
| 29 |
"inet.af/netstack/tcpip/stack" |
| 30 |
"inet.af/netstack/waiter" |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
const InitialRTO = time.Second |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
const maxSegmentsPerWake = 100 |
| 42 |
|
| 43 |
type handshakeState int |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
const ( |
| 49 |
handshakeSynSent handshakeState = iota |
| 50 |
handshakeSynRcvd |
| 51 |
handshakeCompleted |
| 52 |
) |
| 53 |
|
| 54 |
const ( |
| 55 |
|
| 56 |
maxOptionSize = 40 |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
type handshake struct { |
| 64 |
ep *endpoint |
| 65 |
listenEP *endpoint |
| 66 |
state handshakeState |
| 67 |
active bool |
| 68 |
flags header.TCPFlags |
| 69 |
ackNum seqnum.Value |
| 70 |
|
| 71 |
|
| 72 |
iss seqnum.Value |
| 73 |
|
| 74 |
|
| 75 |
rcvWnd seqnum.Size |
| 76 |
|
| 77 |
|
| 78 |
sndWnd seqnum.Size |
| 79 |
|
| 80 |
|
| 81 |
mss uint16 |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
sndWndScale int |
| 86 |
|
| 87 |
|
| 88 |
rcvWndScale int |
| 89 |
|
| 90 |
|
| 91 |
startTime tcpip.MonotonicTime |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
deferAccept time.Duration |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
acked bool |
| 102 |
|
| 103 |
|
| 104 |
sendSYNOpts header.TCPSynOptions |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
sampleRTTWithTSOnly bool |
| 110 |
} |
| 111 |
|
| 112 |
func (e *endpoint) newHandshake() *handshake { |
| 113 |
h := &handshake{ |
| 114 |
ep: e, |
| 115 |
active: true, |
| 116 |
rcvWnd: seqnum.Size(e.initialReceiveWindow()), |
| 117 |
rcvWndScale: e.rcvWndScaleForHandshake(), |
| 118 |
} |
| 119 |
h.resetState() |
| 120 |
|
| 121 |
e.h = h |
| 122 |
|
| 123 |
e.TSOffset = e.protocol.tsOffset(e.ID.LocalAddress, e.ID.RemoteAddress) |
| 124 |
return h |
| 125 |
} |
| 126 |
|
| 127 |
func (e *endpoint) newPassiveHandshake(isn, irs seqnum.Value, opts header.TCPSynOptions, deferAccept time.Duration) *handshake { |
| 128 |
h := e.newHandshake() |
| 129 |
h.resetToSynRcvd(isn, irs, opts, deferAccept) |
| 130 |
return h |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
func FindWndScale(wnd seqnum.Size) int { |
| 136 |
if wnd < 0x10000 { |
| 137 |
return 0 |
| 138 |
} |
| 139 |
|
| 140 |
max := seqnum.Size(math.MaxUint16) |
| 141 |
s := 0 |
| 142 |
for wnd > max && s < header.MaxWndScale { |
| 143 |
s++ |
| 144 |
max <<= 1 |
| 145 |
} |
| 146 |
|
| 147 |
return s |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func (h *handshake) resetState() { |
| 153 |
h.state = handshakeSynSent |
| 154 |
h.flags = header.TCPFlagSyn |
| 155 |
h.ackNum = 0 |
| 156 |
h.mss = 0 |
| 157 |
h.iss = generateSecureISN(h.ep.TransportEndpointInfo.ID, h.ep.stack.Clock(), h.ep.protocol.seqnumSecret) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
func generateSecureISN(id stack.TransportEndpointID, clock tcpip.Clock, seed uint32) seqnum.Value { |
| 163 |
isnHasher := jenkins.Sum32(seed) |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
_, _ = isnHasher.Write([]byte(id.LocalAddress)) |
| 168 |
_, _ = isnHasher.Write([]byte(id.RemoteAddress)) |
| 169 |
portBuf := make([]byte, 2) |
| 170 |
binary.LittleEndian.PutUint16(portBuf, id.LocalPort) |
| 171 |
_, _ = isnHasher.Write(portBuf) |
| 172 |
binary.LittleEndian.PutUint16(portBuf, id.RemotePort) |
| 173 |
_, _ = isnHasher.Write(portBuf) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
isn := isnHasher.Sum32() + uint32(clock.NowMonotonic().Sub(tcpip.MonotonicTime{}).Nanoseconds()>>6) |
| 185 |
return seqnum.Value(isn) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func (h *handshake) effectiveRcvWndScale() uint8 { |
| 192 |
if h.sndWndScale < 0 { |
| 193 |
return 0 |
| 194 |
} |
| 195 |
return uint8(h.rcvWndScale) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func (h *handshake) resetToSynRcvd(iss seqnum.Value, irs seqnum.Value, opts header.TCPSynOptions, deferAccept time.Duration) { |
| 201 |
h.active = false |
| 202 |
h.state = handshakeSynRcvd |
| 203 |
h.flags = header.TCPFlagSyn | header.TCPFlagAck |
| 204 |
h.iss = iss |
| 205 |
h.ackNum = irs + 1 |
| 206 |
h.mss = opts.MSS |
| 207 |
h.sndWndScale = opts.WS |
| 208 |
h.deferAccept = deferAccept |
| 209 |
h.ep.setEndpointState(StateSynRecv) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
func (h *handshake) checkAck(s *segment) bool { |
| 216 |
if s.flags.Contains(header.TCPFlagAck) && s.ackNumber != h.iss+1 { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
ack := s.sequenceNumber.Add(s.logicalLen()) |
| 222 |
h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagRst|header.TCPFlagAck, s.ackNumber, ack, 0) |
| 223 |
return false |
| 224 |
} |
| 225 |
|
| 226 |
return true |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func (h *handshake) synSentState(s *segment) tcpip.Error { |
| 232 |
|
| 233 |
|
| 234 |
if s.flags.Contains(header.TCPFlagRst) { |
| 235 |
if s.flags.Contains(header.TCPFlagAck) && s.ackNumber == h.iss+1 { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
h.ep.workerCleanup = true |
| 240 |
|
| 241 |
|
| 242 |
return &tcpip.ErrConnectionRefused{} |
| 243 |
} |
| 244 |
return nil |
| 245 |
} |
| 246 |
|
| 247 |
if !h.checkAck(s) { |
| 248 |
return nil |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if !s.flags.Contains(header.TCPFlagSyn) { |
| 254 |
return nil |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
rcvSynOpts := parseSynSegmentOptions(s) |
| 259 |
|
| 260 |
|
| 261 |
h.ep.maybeEnableTimestamp(rcvSynOpts) |
| 262 |
|
| 263 |
|
| 264 |
h.ep.maybeEnableSACKPermitted(rcvSynOpts) |
| 265 |
|
| 266 |
|
| 267 |
h.ackNum = s.sequenceNumber + 1 |
| 268 |
h.flags |= header.TCPFlagAck |
| 269 |
h.mss = rcvSynOpts.MSS |
| 270 |
h.sndWndScale = rcvSynOpts.WS |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if s.flags.Contains(header.TCPFlagAck) { |
| 275 |
h.state = handshakeCompleted |
| 276 |
h.transitionToStateEstablishedLocked(s) |
| 277 |
|
| 278 |
h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck, h.iss+1, h.ackNum, h.rcvWnd>>h.effectiveRcvWndScale()) |
| 279 |
return nil |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
h.state = handshakeSynRcvd |
| 286 |
ttl := h.ep.ttl |
| 287 |
amss := h.ep.amss |
| 288 |
h.ep.setEndpointState(StateSynRecv) |
| 289 |
synOpts := header.TCPSynOptions{ |
| 290 |
WS: int(h.effectiveRcvWndScale()), |
| 291 |
TS: rcvSynOpts.TS, |
| 292 |
TSVal: h.ep.tsValNow(), |
| 293 |
TSEcr: h.ep.recentTimestamp(), |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
SACKPermitted: rcvSynOpts.SACKPermitted, |
| 299 |
MSS: amss, |
| 300 |
} |
| 301 |
if ttl == 0 { |
| 302 |
ttl = h.ep.route.DefaultTTL() |
| 303 |
} |
| 304 |
h.ep.sendSynTCP(h.ep.route, tcpFields{ |
| 305 |
id: h.ep.TransportEndpointInfo.ID, |
| 306 |
ttl: ttl, |
| 307 |
tos: h.ep.sendTOS, |
| 308 |
flags: h.flags, |
| 309 |
seq: h.iss, |
| 310 |
ack: h.ackNum, |
| 311 |
rcvWnd: h.rcvWnd, |
| 312 |
}, synOpts) |
| 313 |
return nil |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
func (h *handshake) synRcvdState(s *segment) tcpip.Error { |
| 319 |
if s.flags.Contains(header.TCPFlagRst) { |
| 320 |
|
| 321 |
|
| 322 |
if s.sequenceNumber.InWindow(h.ackNum, h.rcvWnd) { |
| 323 |
return &tcpip.ErrConnectionRefused{} |
| 324 |
} |
| 325 |
return nil |
| 326 |
} |
| 327 |
|
| 328 |
if !h.checkAck(s) { |
| 329 |
return nil |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
if !s.sequenceNumber.InWindow(h.ackNum, h.rcvWnd) { |
| 337 |
if h.ep.allowOutOfWindowAck() { |
| 338 |
h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck, h.iss+1, h.ackNum, h.rcvWnd) |
| 339 |
} |
| 340 |
return nil |
| 341 |
} |
| 342 |
|
| 343 |
if s.flags.Contains(header.TCPFlagSyn) && s.sequenceNumber != h.ackNum-1 { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
ack := s.sequenceNumber.Add(s.logicalLen()) |
| 348 |
seq := seqnum.Value(0) |
| 349 |
if s.flags.Contains(header.TCPFlagAck) { |
| 350 |
seq = s.ackNumber |
| 351 |
} |
| 352 |
h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagRst|header.TCPFlagAck, seq, ack, 0) |
| 353 |
|
| 354 |
if !h.active { |
| 355 |
return &tcpip.ErrInvalidEndpointState{} |
| 356 |
} |
| 357 |
|
| 358 |
h.resetState() |
| 359 |
synOpts := header.TCPSynOptions{ |
| 360 |
WS: h.rcvWndScale, |
| 361 |
TS: h.ep.SendTSOk, |
| 362 |
TSVal: h.ep.tsValNow(), |
| 363 |
TSEcr: h.ep.recentTimestamp(), |
| 364 |
SACKPermitted: h.ep.SACKPermitted, |
| 365 |
MSS: h.ep.amss, |
| 366 |
} |
| 367 |
h.ep.sendSynTCP(h.ep.route, tcpFields{ |
| 368 |
id: h.ep.TransportEndpointInfo.ID, |
| 369 |
ttl: h.ep.ttl, |
| 370 |
tos: h.ep.sendTOS, |
| 371 |
flags: h.flags, |
| 372 |
seq: h.iss, |
| 373 |
ack: h.ackNum, |
| 374 |
rcvWnd: h.rcvWnd, |
| 375 |
}, synOpts) |
| 376 |
return nil |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if s.flags.Contains(header.TCPFlagAck) { |
| 382 |
|
| 383 |
|
| 384 |
if h.deferAccept != 0 && s.data.Size() == 0 && h.ep.stack.Clock().NowMonotonic().Sub(h.startTime) < h.deferAccept { |
| 385 |
h.acked = true |
| 386 |
h.ep.stack.Stats().DroppedPackets.Increment() |
| 387 |
return nil |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
if h.ep.SendTSOk && !s.parsedOptions.TS { |
| 394 |
h.ep.stack.Stats().DroppedPackets.Increment() |
| 395 |
return nil |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
if listenEP := h.listenEP; listenEP != nil && listenEP.acceptQueueIsFull() { |
| 403 |
listenEP.stack.Stats().DroppedPackets.Increment() |
| 404 |
return nil |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
if h.ep.SendTSOk && s.parsedOptions.TS { |
| 409 |
h.ep.updateRecentTimestamp(s.parsedOptions.TSVal, h.ackNum, s.sequenceNumber) |
| 410 |
} |
| 411 |
|
| 412 |
h.state = handshakeCompleted |
| 413 |
|
| 414 |
h.transitionToStateEstablishedLocked(s) |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if (s.flags.Contains(header.TCPFlagFin) || s.data.Size() > 0) && h.ep.enqueueSegment(s) { |
| 419 |
s.incRef() |
| 420 |
h.ep.newSegmentWaker.Assert() |
| 421 |
} |
| 422 |
return nil |
| 423 |
} |
| 424 |
|
| 425 |
return nil |
| 426 |
} |
| 427 |
|
| 428 |
func (h *handshake) handleSegment(s *segment) tcpip.Error { |
| 429 |
h.sndWnd = s.window |
| 430 |
if !s.flags.Contains(header.TCPFlagSyn) && h.sndWndScale > 0 { |
| 431 |
h.sndWnd <<= uint8(h.sndWndScale) |
| 432 |
} |
| 433 |
|
| 434 |
switch h.state { |
| 435 |
case handshakeSynRcvd: |
| 436 |
return h.synRcvdState(s) |
| 437 |
case handshakeSynSent: |
| 438 |
return h.synSentState(s) |
| 439 |
} |
| 440 |
return nil |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
func (h *handshake) processSegments() tcpip.Error { |
| 446 |
for i := 0; i < maxSegmentsPerWake; i++ { |
| 447 |
s := h.ep.segmentQueue.dequeue() |
| 448 |
if s == nil { |
| 449 |
return nil |
| 450 |
} |
| 451 |
|
| 452 |
err := h.handleSegment(s) |
| 453 |
s.decRef() |
| 454 |
if err != nil { |
| 455 |
return err |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if h.state == handshakeCompleted { |
| 462 |
break |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
if !h.ep.segmentQueue.empty() { |
| 469 |
h.ep.newSegmentWaker.Assert() |
| 470 |
} |
| 471 |
|
| 472 |
return nil |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
func (h *handshake) start() { |
| 478 |
h.startTime = h.ep.stack.Clock().NowMonotonic() |
| 479 |
h.ep.amss = calculateAdvertisedMSS(h.ep.userMSS, h.ep.route) |
| 480 |
var sackEnabled tcpip.TCPSACKEnabled |
| 481 |
if err := h.ep.stack.TransportProtocolOption(ProtocolNumber, &sackEnabled); err != nil { |
| 482 |
|
| 483 |
|
| 484 |
sackEnabled = false |
| 485 |
} |
| 486 |
|
| 487 |
synOpts := header.TCPSynOptions{ |
| 488 |
WS: h.rcvWndScale, |
| 489 |
TS: true, |
| 490 |
TSVal: h.ep.tsValNow(), |
| 491 |
TSEcr: h.ep.recentTimestamp(), |
| 492 |
SACKPermitted: bool(sackEnabled), |
| 493 |
MSS: h.ep.amss, |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
if h.state == handshakeSynRcvd { |
| 499 |
synOpts.TS = h.ep.SendTSOk |
| 500 |
synOpts.SACKPermitted = h.ep.SACKPermitted && bool(sackEnabled) |
| 501 |
if h.sndWndScale < 0 { |
| 502 |
|
| 503 |
|
| 504 |
synOpts.WS = -1 |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
h.sendSYNOpts = synOpts |
| 509 |
h.ep.sendSynTCP(h.ep.route, tcpFields{ |
| 510 |
id: h.ep.TransportEndpointInfo.ID, |
| 511 |
ttl: h.ep.ttl, |
| 512 |
tos: h.ep.sendTOS, |
| 513 |
flags: h.flags, |
| 514 |
seq: h.iss, |
| 515 |
ack: h.ackNum, |
| 516 |
rcvWnd: h.rcvWnd, |
| 517 |
}, synOpts) |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
func (h *handshake) complete() tcpip.Error { |
| 523 |
|
| 524 |
var s sleep.Sleeper |
| 525 |
resendWaker := sleep.Waker{} |
| 526 |
s.AddWaker(&resendWaker) |
| 527 |
s.AddWaker(&h.ep.notificationWaker) |
| 528 |
s.AddWaker(&h.ep.newSegmentWaker) |
| 529 |
defer s.Done() |
| 530 |
|
| 531 |
|
| 532 |
timer, err := newBackoffTimer(h.ep.stack.Clock(), InitialRTO, MaxRTO, resendWaker.Assert) |
| 533 |
if err != nil { |
| 534 |
return err |
| 535 |
} |
| 536 |
defer timer.stop() |
| 537 |
for h.state != handshakeCompleted { |
| 538 |
|
| 539 |
|
| 540 |
h.ep.mu.Unlock() |
| 541 |
w := s.Fetch(true ) |
| 542 |
h.ep.mu.Lock() |
| 543 |
switch w { |
| 544 |
case &resendWaker: |
| 545 |
if err := timer.reset(); err != nil { |
| 546 |
return err |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
if h.active || !h.acked || h.deferAccept != 0 && h.ep.stack.Clock().NowMonotonic().Sub(h.startTime) > h.deferAccept { |
| 557 |
h.ep.sendSynTCP(h.ep.route, tcpFields{ |
| 558 |
id: h.ep.TransportEndpointInfo.ID, |
| 559 |
ttl: h.ep.ttl, |
| 560 |
tos: h.ep.sendTOS, |
| 561 |
flags: h.flags, |
| 562 |
seq: h.iss, |
| 563 |
ack: h.ackNum, |
| 564 |
rcvWnd: h.rcvWnd, |
| 565 |
}, h.sendSYNOpts) |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
h.sampleRTTWithTSOnly = true |
| 570 |
} |
| 571 |
|
| 572 |
case &h.ep.notificationWaker: |
| 573 |
n := h.ep.fetchNotifications() |
| 574 |
if (n¬ifyClose)|(n¬ifyAbort) != 0 { |
| 575 |
return &tcpip.ErrAborted{} |
| 576 |
} |
| 577 |
if n¬ifyShutdown != 0 { |
| 578 |
return &tcpip.ErrConnectionReset{} |
| 579 |
} |
| 580 |
if n¬ifyDrain != 0 { |
| 581 |
for !h.ep.segmentQueue.empty() { |
| 582 |
s := h.ep.segmentQueue.dequeue() |
| 583 |
err := h.handleSegment(s) |
| 584 |
s.decRef() |
| 585 |
if err != nil { |
| 586 |
return err |
| 587 |
} |
| 588 |
if h.state == handshakeCompleted { |
| 589 |
return nil |
| 590 |
} |
| 591 |
} |
| 592 |
close(h.ep.drainDone) |
| 593 |
h.ep.mu.Unlock() |
| 594 |
<-h.ep.undrain |
| 595 |
h.ep.mu.Lock() |
| 596 |
} |
| 597 |
|
| 598 |
if n¬ifyError != 0 { |
| 599 |
if err := h.ep.lastErrorLocked(); err != nil { |
| 600 |
return err |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
return &tcpip.ErrConnectionAborted{} |
| 605 |
} |
| 606 |
case &h.ep.newSegmentWaker: |
| 607 |
if err := h.processSegments(); err != nil { |
| 608 |
return err |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
return nil |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
func (h *handshake) transitionToStateEstablishedLocked(s *segment) { |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
h.ep.snd = newSender(h.ep, h.iss, h.ackNum-1, h.sndWnd, h.mss, h.sndWndScale) |
| 624 |
|
| 625 |
now := h.ep.stack.Clock().NowMonotonic() |
| 626 |
|
| 627 |
var rtt time.Duration |
| 628 |
if h.ep.SendTSOk && s.parsedOptions.TSEcr != 0 { |
| 629 |
rtt = h.ep.elapsed(now, s.parsedOptions.TSEcr) |
| 630 |
} |
| 631 |
if !h.sampleRTTWithTSOnly && rtt == 0 { |
| 632 |
rtt = now.Sub(h.startTime) |
| 633 |
} |
| 634 |
|
| 635 |
if rtt > 0 { |
| 636 |
h.ep.snd.updateRTO(rtt) |
| 637 |
} |
| 638 |
|
| 639 |
h.ep.rcvQueueInfo.rcvQueueMu.Lock() |
| 640 |
h.ep.rcv = newReceiver(h.ep, h.ackNum-1, h.rcvWnd, h.effectiveRcvWndScale()) |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
h.ep.rcvQueueInfo.RcvAutoParams.PrevCopiedBytes = int(h.rcvWnd) |
| 645 |
h.ep.rcvQueueInfo.rcvQueueMu.Unlock() |
| 646 |
|
| 647 |
h.ep.setEndpointState(StateEstablished) |
| 648 |
} |
| 649 |
|
| 650 |
type backoffTimer struct { |
| 651 |
timeout time.Duration |
| 652 |
maxTimeout time.Duration |
| 653 |
t tcpip.Timer |
| 654 |
} |
| 655 |
|
| 656 |
func newBackoffTimer(clock tcpip.Clock, timeout, maxTimeout time.Duration, f func()) (*backoffTimer, tcpip.Error) { |
| 657 |
if timeout > maxTimeout { |
| 658 |
return nil, &tcpip.ErrTimeout{} |
| 659 |
} |
| 660 |
bt := &backoffTimer{timeout: timeout, maxTimeout: maxTimeout} |
| 661 |
bt.t = clock.AfterFunc(timeout, f) |
| 662 |
return bt, nil |
| 663 |
} |
| 664 |
|
| 665 |
func (bt *backoffTimer) reset() tcpip.Error { |
| 666 |
bt.timeout *= 2 |
| 667 |
if bt.timeout > bt.maxTimeout { |
| 668 |
return &tcpip.ErrTimeout{} |
| 669 |
} |
| 670 |
bt.t.Reset(bt.timeout) |
| 671 |
return nil |
| 672 |
} |
| 673 |
|
| 674 |
func (bt *backoffTimer) stop() { |
| 675 |
bt.t.Stop() |
| 676 |
} |
| 677 |
|
| 678 |
func parseSynSegmentOptions(s *segment) header.TCPSynOptions { |
| 679 |
synOpts := header.ParseSynOptions(s.options, s.flags.Contains(header.TCPFlagAck)) |
| 680 |
if synOpts.TS { |
| 681 |
s.parsedOptions.TSVal = synOpts.TSVal |
| 682 |
s.parsedOptions.TSEcr = synOpts.TSEcr |
| 683 |
} |
| 684 |
return synOpts |
| 685 |
} |
| 686 |
|
| 687 |
var optionPool = sync.Pool{ |
| 688 |
New: func() interface{} { |
| 689 |
return &[maxOptionSize]byte{} |
| 690 |
}, |
| 691 |
} |
| 692 |
|
| 693 |
func getOptions() []byte { |
| 694 |
return (*optionPool.Get().(*[maxOptionSize]byte))[:] |
| 695 |
} |
| 696 |
|
| 697 |
func putOptions(options []byte) { |
| 698 |
|
| 699 |
optionPool.Put(optionsToArray(options)) |
| 700 |
} |
| 701 |
|
| 702 |
func makeSynOptions(opts header.TCPSynOptions) []byte { |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
options := getOptions() |
| 720 |
|
| 721 |
|
| 722 |
offset := header.EncodeMSSOption(uint32(opts.MSS), options) |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
if opts.TS && opts.SACKPermitted { |
| 728 |
offset += header.EncodeSACKPermittedOption(options[offset:]) |
| 729 |
offset += header.EncodeTSOption(opts.TSVal, opts.TSEcr, options[offset:]) |
| 730 |
} else if opts.TS { |
| 731 |
offset += header.EncodeNOP(options[offset:]) |
| 732 |
offset += header.EncodeNOP(options[offset:]) |
| 733 |
offset += header.EncodeTSOption(opts.TSVal, opts.TSEcr, options[offset:]) |
| 734 |
} else if opts.SACKPermitted { |
| 735 |
offset += header.EncodeNOP(options[offset:]) |
| 736 |
offset += header.EncodeNOP(options[offset:]) |
| 737 |
offset += header.EncodeSACKPermittedOption(options[offset:]) |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
if opts.WS >= 0 { |
| 742 |
offset += header.EncodeNOP(options[offset:]) |
| 743 |
offset += header.EncodeWSOption(opts.WS, options[offset:]) |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
if delta := header.AddTCPOptionPadding(options, offset); delta != 0 { |
| 749 |
panic("unexpected option encoding") |
| 750 |
} |
| 751 |
|
| 752 |
return options[:offset] |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
type tcpFields struct { |
| 758 |
id stack.TransportEndpointID |
| 759 |
ttl uint8 |
| 760 |
tos uint8 |
| 761 |
flags header.TCPFlags |
| 762 |
seq seqnum.Value |
| 763 |
ack seqnum.Value |
| 764 |
rcvWnd seqnum.Size |
| 765 |
opts []byte |
| 766 |
txHash uint32 |
| 767 |
} |
| 768 |
|
| 769 |
func (e *endpoint) sendSynTCP(r *stack.Route, tf tcpFields, opts header.TCPSynOptions) tcpip.Error { |
| 770 |
tf.opts = makeSynOptions(opts) |
| 771 |
|
| 772 |
if err := e.sendTCP(r, tf, buffer.VectorisedView{}, stack.GSO{}); err != nil { |
| 773 |
e.stats.SendErrors.SynSendToNetworkFailed.Increment() |
| 774 |
} |
| 775 |
putOptions(tf.opts) |
| 776 |
return nil |
| 777 |
} |
| 778 |
|
| 779 |
func (e *endpoint) sendTCP(r *stack.Route, tf tcpFields, data buffer.VectorisedView, gso stack.GSO) tcpip.Error { |
| 780 |
tf.txHash = e.txHash |
| 781 |
if err := sendTCP(r, tf, data, gso, e.owner); err != nil { |
| 782 |
e.stats.SendErrors.SegmentSendToNetworkFailed.Increment() |
| 783 |
return err |
| 784 |
} |
| 785 |
e.stats.SegmentsSent.Increment() |
| 786 |
return nil |
| 787 |
} |
| 788 |
|
| 789 |
func buildTCPHdr(r *stack.Route, tf tcpFields, pkt *stack.PacketBuffer, gso stack.GSO) { |
| 790 |
optLen := len(tf.opts) |
| 791 |
tcp := header.TCP(pkt.TransportHeader().Push(header.TCPMinimumSize + optLen)) |
| 792 |
pkt.TransportProtocolNumber = header.TCPProtocolNumber |
| 793 |
tcp.Encode(&header.TCPFields{ |
| 794 |
SrcPort: tf.id.LocalPort, |
| 795 |
DstPort: tf.id.RemotePort, |
| 796 |
SeqNum: uint32(tf.seq), |
| 797 |
AckNum: uint32(tf.ack), |
| 798 |
DataOffset: uint8(header.TCPMinimumSize + optLen), |
| 799 |
Flags: tf.flags, |
| 800 |
WindowSize: uint16(tf.rcvWnd), |
| 801 |
}) |
| 802 |
copy(tcp[header.TCPMinimumSize:], tf.opts) |
| 803 |
|
| 804 |
xsum := r.PseudoHeaderChecksum(ProtocolNumber, uint16(pkt.Size())) |
| 805 |
|
| 806 |
if gso.Type != stack.GSONone && gso.NeedsCsum { |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
tcp.SetChecksum(xsum) |
| 812 |
} else if r.RequiresTXTransportChecksum() { |
| 813 |
xsum = header.ChecksumCombine(xsum, pkt.Data().AsRange().Checksum()) |
| 814 |
tcp.SetChecksum(^tcp.CalculateChecksum(xsum)) |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
func sendTCPBatch(r *stack.Route, tf tcpFields, data buffer.VectorisedView, gso stack.GSO, owner tcpip.PacketOwner) tcpip.Error { |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
data = data.Clone(nil) |
| 824 |
|
| 825 |
optLen := len(tf.opts) |
| 826 |
if tf.rcvWnd > math.MaxUint16 { |
| 827 |
tf.rcvWnd = math.MaxUint16 |
| 828 |
} |
| 829 |
|
| 830 |
mss := int(gso.MSS) |
| 831 |
n := (data.Size() + mss - 1) / mss |
| 832 |
|
| 833 |
size := data.Size() |
| 834 |
hdrSize := header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen |
| 835 |
var pkts stack.PacketBufferList |
| 836 |
for i := 0; i < n; i++ { |
| 837 |
packetSize := mss |
| 838 |
if packetSize > size { |
| 839 |
packetSize = size |
| 840 |
} |
| 841 |
size -= packetSize |
| 842 |
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 843 |
ReserveHeaderBytes: hdrSize, |
| 844 |
}) |
| 845 |
pkt.Hash = tf.txHash |
| 846 |
pkt.Owner = owner |
| 847 |
pkt.Data().ReadFromVV(&data, packetSize) |
| 848 |
buildTCPHdr(r, tf, pkt, gso) |
| 849 |
tf.seq = tf.seq.Add(seqnum.Size(packetSize)) |
| 850 |
pkt.GSOOptions = gso |
| 851 |
pkts.PushBack(pkt) |
| 852 |
} |
| 853 |
defer pkts.DecRef() |
| 854 |
|
| 855 |
if tf.ttl == 0 { |
| 856 |
tf.ttl = r.DefaultTTL() |
| 857 |
} |
| 858 |
sent, err := r.WritePackets(pkts, stack.NetworkHeaderParams{Protocol: ProtocolNumber, TTL: tf.ttl, TOS: tf.tos}) |
| 859 |
if err != nil { |
| 860 |
r.Stats().TCP.SegmentSendErrors.IncrementBy(uint64(n - sent)) |
| 861 |
} |
| 862 |
r.Stats().TCP.SegmentsSent.IncrementBy(uint64(sent)) |
| 863 |
return err |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
func sendTCP(r *stack.Route, tf tcpFields, data buffer.VectorisedView, gso stack.GSO, owner tcpip.PacketOwner) tcpip.Error { |
| 869 |
optLen := len(tf.opts) |
| 870 |
if tf.rcvWnd > math.MaxUint16 { |
| 871 |
tf.rcvWnd = math.MaxUint16 |
| 872 |
} |
| 873 |
|
| 874 |
if r.Loop()&stack.PacketLoop == 0 && gso.Type == stack.GSOSW && int(gso.MSS) < data.Size() { |
| 875 |
return sendTCPBatch(r, tf, data, gso, owner) |
| 876 |
} |
| 877 |
|
| 878 |
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 879 |
ReserveHeaderBytes: header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen, |
| 880 |
Data: data, |
| 881 |
}) |
| 882 |
defer pkt.DecRef() |
| 883 |
pkt.GSOOptions = gso |
| 884 |
pkt.Hash = tf.txHash |
| 885 |
pkt.Owner = owner |
| 886 |
buildTCPHdr(r, tf, pkt, gso) |
| 887 |
|
| 888 |
if tf.ttl == 0 { |
| 889 |
tf.ttl = r.DefaultTTL() |
| 890 |
} |
| 891 |
if err := r.WritePacket(stack.NetworkHeaderParams{Protocol: ProtocolNumber, TTL: tf.ttl, TOS: tf.tos}, pkt); err != nil { |
| 892 |
r.Stats().TCP.SegmentSendErrors.Increment() |
| 893 |
return err |
| 894 |
} |
| 895 |
r.Stats().TCP.SegmentsSent.Increment() |
| 896 |
if (tf.flags & header.TCPFlagRst) != 0 { |
| 897 |
r.Stats().TCP.ResetsSent.Increment() |
| 898 |
} |
| 899 |
return nil |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
func (e *endpoint) makeOptions(sackBlocks []header.SACKBlock) []byte { |
| 904 |
options := getOptions() |
| 905 |
offset := 0 |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
if e.SendTSOk { |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
offset += header.EncodeNOP(options[offset:]) |
| 924 |
offset += header.EncodeNOP(options[offset:]) |
| 925 |
offset += header.EncodeTSOption(e.tsValNow(), e.recentTimestamp(), options[offset:]) |
| 926 |
} |
| 927 |
if e.SACKPermitted && len(sackBlocks) > 0 { |
| 928 |
offset += header.EncodeNOP(options[offset:]) |
| 929 |
offset += header.EncodeNOP(options[offset:]) |
| 930 |
offset += header.EncodeSACKBlocks(sackBlocks, options[offset:]) |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
if delta := header.AddTCPOptionPadding(options, offset); delta != 0 { |
| 935 |
panic("unexpected option encoding") |
| 936 |
} |
| 937 |
|
| 938 |
return options[:offset] |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
func (e *endpoint) sendRaw(data buffer.VectorisedView, flags header.TCPFlags, seq, ack seqnum.Value, rcvWnd seqnum.Size) tcpip.Error { |
| 943 |
var sackBlocks []header.SACKBlock |
| 944 |
if e.EndpointState() == StateEstablished && e.rcv.pendingRcvdSegments.Len() > 0 && (flags&header.TCPFlagAck != 0) { |
| 945 |
sackBlocks = e.sack.Blocks[:e.sack.NumBlocks] |
| 946 |
} |
| 947 |
options := e.makeOptions(sackBlocks) |
| 948 |
err := e.sendTCP(e.route, tcpFields{ |
| 949 |
id: e.TransportEndpointInfo.ID, |
| 950 |
ttl: e.ttl, |
| 951 |
tos: e.sendTOS, |
| 952 |
flags: flags, |
| 953 |
seq: seq, |
| 954 |
ack: ack, |
| 955 |
rcvWnd: rcvWnd, |
| 956 |
opts: options, |
| 957 |
}, data, e.gso) |
| 958 |
putOptions(options) |
| 959 |
return err |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
func (e *endpoint) sendData(next *segment) { |
| 964 |
|
| 965 |
if e.snd.writeNext == nil { |
| 966 |
if next == nil { |
| 967 |
return |
| 968 |
} |
| 969 |
e.snd.writeNext = next |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
e.snd.sendData() |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
func (e *endpoint) resetConnectionLocked(err tcpip.Error) { |
| 981 |
|
| 982 |
|
| 983 |
e.setEndpointState(StateError) |
| 984 |
e.hardError = err |
| 985 |
switch err.(type) { |
| 986 |
case *tcpip.ErrConnectionReset, *tcpip.ErrTimeout: |
| 987 |
default: |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
sndWndEnd := e.snd.SndUna.Add(e.snd.SndWnd) |
| 996 |
resetSeqNum := sndWndEnd |
| 997 |
if !sndWndEnd.LessThan(e.snd.SndNxt) || e.snd.SndNxt.Size(sndWndEnd) < (1<<e.snd.SndWndScale) { |
| 998 |
resetSeqNum = e.snd.SndNxt |
| 999 |
} |
| 1000 |
e.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck|header.TCPFlagRst, resetSeqNum, e.rcv.RcvNxt, 0) |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
func (e *endpoint) completeWorkerLocked() { |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
e.workerRunning = false |
| 1012 |
if e.workerCleanup { |
| 1013 |
e.cleanupLocked() |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
func (e *endpoint) transitionToStateCloseLocked() { |
| 1023 |
s := e.EndpointState() |
| 1024 |
if s == StateClose { |
| 1025 |
return |
| 1026 |
} |
| 1027 |
|
| 1028 |
if s.connected() { |
| 1029 |
e.stack.Stats().TCP.CurrentConnected.Decrement() |
| 1030 |
e.stack.Stats().TCP.EstablishedClosed.Increment() |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
e.cleanupLocked() |
| 1035 |
e.setEndpointState(StateClose) |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
func (e *endpoint) tryDeliverSegmentFromClosedEndpoint(s *segment) { |
| 1043 |
ep := e.stack.FindTransportEndpoint(e.NetProto, e.TransProto, e.TransportEndpointInfo.ID, s.nicID) |
| 1044 |
if ep == nil && e.NetProto == header.IPv6ProtocolNumber && e.TransportEndpointInfo.ID.LocalAddress.To4() != "" { |
| 1045 |
|
| 1046 |
ep = e.stack.FindTransportEndpoint( |
| 1047 |
header.IPv4ProtocolNumber, |
| 1048 |
e.TransProto, |
| 1049 |
e.TransportEndpointInfo.ID, |
| 1050 |
s.nicID, |
| 1051 |
) |
| 1052 |
} |
| 1053 |
if ep == nil { |
| 1054 |
replyWithReset(e.stack, s, stack.DefaultTOS, 0 ) |
| 1055 |
s.decRef() |
| 1056 |
return |
| 1057 |
} |
| 1058 |
|
| 1059 |
if e == ep { |
| 1060 |
panic("current endpoint not removed from demuxer, enqueing segments to itself") |
| 1061 |
} |
| 1062 |
|
| 1063 |
if ep := ep.(*endpoint); ep.enqueueSegment(s) { |
| 1064 |
ep.newSegmentWaker.Assert() |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
func (e *endpoint) drainClosingSegmentQueue() { |
| 1072 |
for { |
| 1073 |
s := e.segmentQueue.dequeue() |
| 1074 |
if s == nil { |
| 1075 |
break |
| 1076 |
} |
| 1077 |
|
| 1078 |
e.tryDeliverSegmentFromClosedEndpoint(s) |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
func (e *endpoint) handleReset(s *segment) (ok bool, err tcpip.Error) { |
| 1083 |
if e.rcv.acceptable(s.sequenceNumber, 0) { |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
switch e.EndpointState() { |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
case StateCloseWait: |
| 1109 |
e.transitionToStateCloseLocked() |
| 1110 |
e.hardError = &tcpip.ErrAborted{} |
| 1111 |
e.notifyProtocolGoroutine(notifyTickleWorker) |
| 1112 |
return false, nil |
| 1113 |
default: |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
e.notifyProtocolGoroutine(notifyResetByPeer) |
| 1123 |
return false, &tcpip.ErrConnectionReset{} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
return true, nil |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
func (e *endpoint) handleSegmentsLocked(fastPath bool) tcpip.Error { |
| 1133 |
checkRequeue := true |
| 1134 |
for i := 0; i < maxSegmentsPerWake; i++ { |
| 1135 |
if state := e.EndpointState(); state.closed() || state == StateTimeWait { |
| 1136 |
return nil |
| 1137 |
} |
| 1138 |
s := e.segmentQueue.dequeue() |
| 1139 |
if s == nil { |
| 1140 |
checkRequeue = false |
| 1141 |
break |
| 1142 |
} |
| 1143 |
|
| 1144 |
cont, err := e.handleSegmentLocked(s) |
| 1145 |
s.decRef() |
| 1146 |
if err != nil { |
| 1147 |
return err |
| 1148 |
} |
| 1149 |
if !cont { |
| 1150 |
return nil |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
if !fastPath && checkRequeue && !e.segmentQueue.empty() { |
| 1158 |
e.newSegmentWaker.Assert() |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
if e.rcv.RcvNxt != e.snd.MaxSentAck { |
| 1163 |
e.snd.sendAck() |
| 1164 |
} |
| 1165 |
|
| 1166 |
e.resetKeepaliveTimer(true ) |
| 1167 |
|
| 1168 |
return nil |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
func (e *endpoint) probeSegmentLocked() { |
| 1173 |
if fn := e.probe; fn != nil { |
| 1174 |
fn(e.completeStateLocked()) |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
func (e *endpoint) handleSegmentLocked(s *segment) (cont bool, err tcpip.Error) { |
| 1183 |
|
| 1184 |
|
| 1185 |
defer e.probeSegmentLocked() |
| 1186 |
|
| 1187 |
if s.flags.Contains(header.TCPFlagRst) { |
| 1188 |
if ok, err := e.handleReset(s); !ok { |
| 1189 |
return false, err |
| 1190 |
} |
| 1191 |
} else if s.flags.Contains(header.TCPFlagSyn) { |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
e.snd.maybeSendOutOfWindowAck(s) |
| 1215 |
} else if s.flags.Contains(header.TCPFlagAck) { |
| 1216 |
|
| 1217 |
|
| 1218 |
s.window <<= e.snd.SndWndScale |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
drop, err := e.rcv.handleRcvdSegment(s) |
| 1224 |
if err != nil { |
| 1225 |
return false, err |
| 1226 |
} |
| 1227 |
if drop { |
| 1228 |
return true, nil |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
state := e.EndpointState() |
| 1235 |
if state == StateClose { |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
return false, nil |
| 1244 |
} |
| 1245 |
|
| 1246 |
e.snd.handleRcvdSegment(s) |
| 1247 |
} |
| 1248 |
|
| 1249 |
return true, nil |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
func (e *endpoint) keepaliveTimerExpired() tcpip.Error { |
| 1256 |
userTimeout := e.userTimeout |
| 1257 |
|
| 1258 |
e.keepalive.Lock() |
| 1259 |
if !e.SocketOptions().GetKeepAlive() || !e.keepalive.timer.checkExpiration() { |
| 1260 |
e.keepalive.Unlock() |
| 1261 |
return nil |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
if userTimeout != 0 && e.stack.Clock().NowMonotonic().Sub(e.rcv.lastRcvdAckTime) >= userTimeout && e.keepalive.unacked > 0 { |
| 1267 |
e.keepalive.Unlock() |
| 1268 |
e.stack.Stats().TCP.EstablishedTimedout.Increment() |
| 1269 |
return &tcpip.ErrTimeout{} |
| 1270 |
} |
| 1271 |
|
| 1272 |
if e.keepalive.unacked >= e.keepalive.count { |
| 1273 |
e.keepalive.Unlock() |
| 1274 |
e.stack.Stats().TCP.EstablishedTimedout.Increment() |
| 1275 |
return &tcpip.ErrTimeout{} |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
e.keepalive.unacked++ |
| 1281 |
e.keepalive.Unlock() |
| 1282 |
e.snd.sendSegmentFromView(buffer.VectorisedView{}, header.TCPFlagAck, e.snd.SndNxt-1) |
| 1283 |
e.resetKeepaliveTimer(false) |
| 1284 |
return nil |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
func (e *endpoint) resetKeepaliveTimer(receivedData bool) { |
| 1290 |
e.keepalive.Lock() |
| 1291 |
if receivedData { |
| 1292 |
e.keepalive.unacked = 0 |
| 1293 |
} |
| 1294 |
|
| 1295 |
|
| 1296 |
if !e.SocketOptions().GetKeepAlive() || e.snd == nil || e.snd.SndUna != e.snd.SndNxt { |
| 1297 |
e.keepalive.timer.disable() |
| 1298 |
e.keepalive.Unlock() |
| 1299 |
return |
| 1300 |
} |
| 1301 |
if e.keepalive.unacked > 0 { |
| 1302 |
e.keepalive.timer.enable(e.keepalive.interval) |
| 1303 |
} else { |
| 1304 |
e.keepalive.timer.enable(e.keepalive.idle) |
| 1305 |
} |
| 1306 |
e.keepalive.Unlock() |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
func (e *endpoint) disableKeepaliveTimer() { |
| 1311 |
e.keepalive.Lock() |
| 1312 |
e.keepalive.timer.disable() |
| 1313 |
e.keepalive.Unlock() |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
func (e *endpoint) protocolMainLoopDone(closeTimer tcpip.Timer) { |
| 1319 |
if e.snd != nil { |
| 1320 |
e.snd.resendTimer.cleanup() |
| 1321 |
e.snd.probeTimer.cleanup() |
| 1322 |
e.snd.reorderTimer.cleanup() |
| 1323 |
} |
| 1324 |
|
| 1325 |
if closeTimer != nil { |
| 1326 |
closeTimer.Stop() |
| 1327 |
} |
| 1328 |
|
| 1329 |
e.completeWorkerLocked() |
| 1330 |
|
| 1331 |
if e.drainDone != nil { |
| 1332 |
close(e.drainDone) |
| 1333 |
} |
| 1334 |
|
| 1335 |
e.mu.Unlock() |
| 1336 |
|
| 1337 |
e.drainClosingSegmentQueue() |
| 1338 |
|
| 1339 |
|
| 1340 |
e.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.ReadableEvents | waiter.WritableEvents) |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
func (e *endpoint) handleWakeup(w, closeWaker *sleep.Waker, closeTimer *tcpip.Timer) tcpip.Error { |
| 1347 |
switch w { |
| 1348 |
case &e.sndQueueInfo.sndWaker: |
| 1349 |
e.sendData(nil ) |
| 1350 |
case &e.newSegmentWaker: |
| 1351 |
return e.handleSegmentsLocked(false ) |
| 1352 |
case &e.snd.resendWaker: |
| 1353 |
if !e.snd.retransmitTimerExpired() { |
| 1354 |
e.stack.Stats().TCP.EstablishedTimedout.Increment() |
| 1355 |
return &tcpip.ErrTimeout{} |
| 1356 |
} |
| 1357 |
case closeWaker: |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
e.transitionToStateCloseLocked() |
| 1362 |
e.workerCleanup = true |
| 1363 |
case &e.snd.probeWaker: |
| 1364 |
return e.snd.probeTimerExpired() |
| 1365 |
case &e.keepalive.waker: |
| 1366 |
return e.keepaliveTimerExpired() |
| 1367 |
case &e.notificationWaker: |
| 1368 |
n := e.fetchNotifications() |
| 1369 |
if n¬ifyNonZeroReceiveWindow != 0 { |
| 1370 |
e.rcv.nonZeroWindow() |
| 1371 |
} |
| 1372 |
|
| 1373 |
if n¬ifyMTUChanged != 0 { |
| 1374 |
e.sndQueueInfo.sndQueueMu.Lock() |
| 1375 |
count := e.sndQueueInfo.PacketTooBigCount |
| 1376 |
e.sndQueueInfo.PacketTooBigCount = 0 |
| 1377 |
mtu := e.sndQueueInfo.SndMTU |
| 1378 |
e.sndQueueInfo.sndQueueMu.Unlock() |
| 1379 |
|
| 1380 |
e.snd.updateMaxPayloadSize(mtu, count) |
| 1381 |
} |
| 1382 |
|
| 1383 |
if n¬ifyReset != 0 || n¬ifyAbort != 0 { |
| 1384 |
return &tcpip.ErrConnectionAborted{} |
| 1385 |
} |
| 1386 |
|
| 1387 |
if n¬ifyResetByPeer != 0 { |
| 1388 |
return &tcpip.ErrConnectionReset{} |
| 1389 |
} |
| 1390 |
|
| 1391 |
if n¬ifyClose != 0 && e.closed { |
| 1392 |
switch e.EndpointState() { |
| 1393 |
case StateEstablished: |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
e.shutdownLocked(tcpip.ShutdownWrite | tcpip.ShutdownRead) |
| 1399 |
case StateFinWait2: |
| 1400 |
|
| 1401 |
|
| 1402 |
if *closeTimer == nil { |
| 1403 |
*closeTimer = e.stack.Clock().AfterFunc(e.tcpLingerTimeout, closeWaker.Assert) |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
if n¬ifyKeepaliveChanged != 0 { |
| 1409 |
|
| 1410 |
|
| 1411 |
e.resetKeepaliveTimer(true) |
| 1412 |
} |
| 1413 |
|
| 1414 |
if n¬ifyDrain != 0 { |
| 1415 |
for !e.segmentQueue.empty() { |
| 1416 |
if err := e.handleSegmentsLocked(false ); err != nil { |
| 1417 |
return err |
| 1418 |
} |
| 1419 |
} |
| 1420 |
if !e.EndpointState().closed() { |
| 1421 |
|
| 1422 |
|
| 1423 |
close(e.drainDone) |
| 1424 |
e.mu.Unlock() |
| 1425 |
<-e.undrain |
| 1426 |
e.mu.Lock() |
| 1427 |
} |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
case &e.snd.reorderWaker: |
| 1433 |
return e.snd.rc.reorderTimerExpired() |
| 1434 |
default: |
| 1435 |
panic("unknown waker") |
| 1436 |
} |
| 1437 |
return nil |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
func (e *endpoint) protocolMainLoop(handshake bool, wakerInitDone chan<- struct{}) { |
| 1444 |
var ( |
| 1445 |
closeTimer tcpip.Timer |
| 1446 |
closeWaker sleep.Waker |
| 1447 |
) |
| 1448 |
|
| 1449 |
e.mu.Lock() |
| 1450 |
if handshake { |
| 1451 |
if err := e.h.complete(); err != nil { |
| 1452 |
e.lastErrorMu.Lock() |
| 1453 |
e.lastError = err |
| 1454 |
e.lastErrorMu.Unlock() |
| 1455 |
|
| 1456 |
e.setEndpointState(StateError) |
| 1457 |
e.hardError = err |
| 1458 |
|
| 1459 |
e.workerCleanup = true |
| 1460 |
e.protocolMainLoopDone(closeTimer) |
| 1461 |
return |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
if s := e.EndpointState(); !s.connected() { |
| 1471 |
switch s { |
| 1472 |
case StateClose, StateError: |
| 1473 |
|
| 1474 |
|
| 1475 |
if e.snd != nil { |
| 1476 |
break |
| 1477 |
} |
| 1478 |
fallthrough |
| 1479 |
default: |
| 1480 |
panic("endpoint was not established, current state " + s.String()) |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
e.route.ConfirmReachable() |
| 1488 |
|
| 1489 |
drained := e.drainDone != nil |
| 1490 |
if drained { |
| 1491 |
close(e.drainDone) |
| 1492 |
e.mu.Unlock() |
| 1493 |
<-e.undrain |
| 1494 |
e.mu.Lock() |
| 1495 |
} |
| 1496 |
|
| 1497 |
|
| 1498 |
var s sleep.Sleeper |
| 1499 |
s.AddWaker(&e.sndQueueInfo.sndWaker) |
| 1500 |
s.AddWaker(&e.newSegmentWaker) |
| 1501 |
s.AddWaker(&e.snd.resendWaker) |
| 1502 |
s.AddWaker(&e.snd.probeWaker) |
| 1503 |
s.AddWaker(&closeWaker) |
| 1504 |
s.AddWaker(&e.keepalive.waker) |
| 1505 |
s.AddWaker(&e.notificationWaker) |
| 1506 |
s.AddWaker(&e.snd.reorderWaker) |
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
if wakerInitDone != nil { |
| 1511 |
close(wakerInitDone) |
| 1512 |
} |
| 1513 |
|
| 1514 |
|
| 1515 |
e.waiterQueue.Notify(waiter.WritableEvents) |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
if !e.segmentQueue.empty() { |
| 1521 |
e.newSegmentWaker.Assert() |
| 1522 |
} |
| 1523 |
|
| 1524 |
e.rcvQueueInfo.rcvQueueMu.Lock() |
| 1525 |
if !e.rcvQueueInfo.rcvQueue.Empty() { |
| 1526 |
e.waiterQueue.Notify(waiter.ReadableEvents) |
| 1527 |
} |
| 1528 |
e.rcvQueueInfo.rcvQueueMu.Unlock() |
| 1529 |
|
| 1530 |
if e.workerCleanup { |
| 1531 |
e.notifyProtocolGoroutine(notifyClose) |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
cleanupOnError := func(err tcpip.Error) { |
| 1537 |
e.stack.Stats().TCP.CurrentConnected.Decrement() |
| 1538 |
e.workerCleanup = true |
| 1539 |
if err != nil { |
| 1540 |
e.resetConnectionLocked(err) |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
loop: |
| 1545 |
for { |
| 1546 |
switch e.EndpointState() { |
| 1547 |
case StateTimeWait, StateClose, StateError: |
| 1548 |
break loop |
| 1549 |
} |
| 1550 |
|
| 1551 |
e.mu.Unlock() |
| 1552 |
w := s.Fetch(true ) |
| 1553 |
e.mu.Lock() |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
switch e.EndpointState() { |
| 1558 |
case StateError: |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
cleanupOnError(nil) |
| 1565 |
e.protocolMainLoopDone(closeTimer) |
| 1566 |
return |
| 1567 |
case StateTimeWait: |
| 1568 |
fallthrough |
| 1569 |
case StateClose: |
| 1570 |
break loop |
| 1571 |
default: |
| 1572 |
if err := e.handleWakeup(w, &closeWaker, &closeTimer); err != nil { |
| 1573 |
cleanupOnError(err) |
| 1574 |
e.protocolMainLoopDone(closeTimer) |
| 1575 |
return |
| 1576 |
} |
| 1577 |
} |
| 1578 |
} |
| 1579 |
|
| 1580 |
var reuseTW func() |
| 1581 |
if e.EndpointState() == StateTimeWait { |
| 1582 |
|
| 1583 |
if closeTimer != nil { |
| 1584 |
closeTimer.Stop() |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
s.Done() |
| 1589 |
|
| 1590 |
e.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.ReadableEvents | waiter.WritableEvents) |
| 1591 |
e.workerCleanup = true |
| 1592 |
reuseTW = e.doTimeWait() |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
if e.EndpointState() == StateError { |
| 1597 |
cleanupOnError(nil) |
| 1598 |
e.protocolMainLoopDone(closeTimer) |
| 1599 |
return |
| 1600 |
} |
| 1601 |
|
| 1602 |
e.transitionToStateCloseLocked() |
| 1603 |
|
| 1604 |
e.protocolMainLoopDone(closeTimer) |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
if reuseTW != nil { |
| 1609 |
reuseTW() |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
func (e *endpoint) handleTimeWaitSegments() (extendTimeWait bool, reuseTW func()) { |
| 1616 |
checkRequeue := true |
| 1617 |
for i := 0; i < maxSegmentsPerWake; i++ { |
| 1618 |
s := e.segmentQueue.dequeue() |
| 1619 |
if s == nil { |
| 1620 |
checkRequeue = false |
| 1621 |
break |
| 1622 |
} |
| 1623 |
extTW, newSyn := e.rcv.handleTimeWaitSegment(s) |
| 1624 |
if newSyn { |
| 1625 |
info := e.TransportEndpointInfo |
| 1626 |
newID := info.ID |
| 1627 |
newID.RemoteAddress = "" |
| 1628 |
newID.RemotePort = 0 |
| 1629 |
netProtos := []tcpip.NetworkProtocolNumber{info.NetProto} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
if newID.LocalAddress.To4() != "" { |
| 1634 |
netProtos = []tcpip.NetworkProtocolNumber{header.IPv4ProtocolNumber, header.IPv6ProtocolNumber} |
| 1635 |
} |
| 1636 |
for _, netProto := range netProtos { |
| 1637 |
if listenEP := e.stack.FindTransportEndpoint(netProto, info.TransProto, newID, s.nicID); listenEP != nil { |
| 1638 |
tcpEP := listenEP.(*endpoint) |
| 1639 |
if EndpointState(tcpEP.State()) == StateListen { |
| 1640 |
reuseTW = func() { |
| 1641 |
if !tcpEP.enqueueSegment(s) { |
| 1642 |
s.decRef() |
| 1643 |
return |
| 1644 |
} |
| 1645 |
tcpEP.newSegmentWaker.Assert() |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
return false, reuseTW |
| 1652 |
} |
| 1653 |
} |
| 1654 |
} |
| 1655 |
} |
| 1656 |
if extTW { |
| 1657 |
extendTimeWait = true |
| 1658 |
} |
| 1659 |
s.decRef() |
| 1660 |
} |
| 1661 |
if checkRequeue && !e.segmentQueue.empty() { |
| 1662 |
e.newSegmentWaker.Assert() |
| 1663 |
} |
| 1664 |
return extendTimeWait, nil |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
func (e *endpoint) doTimeWait() (twReuse func()) { |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
timeWaitDuration := DefaultTCPTimeWaitTimeout |
| 1678 |
|
| 1679 |
|
| 1680 |
var tcpTW tcpip.TCPTimeWaitTimeoutOption |
| 1681 |
if err := e.stack.TransportProtocolOption(ProtocolNumber, &tcpTW); err == nil { |
| 1682 |
timeWaitDuration = time.Duration(tcpTW) |
| 1683 |
} |
| 1684 |
|
| 1685 |
var s sleep.Sleeper |
| 1686 |
defer s.Done() |
| 1687 |
s.AddWaker(&e.newSegmentWaker) |
| 1688 |
s.AddWaker(&e.notificationWaker) |
| 1689 |
|
| 1690 |
var timeWaitWaker sleep.Waker |
| 1691 |
s.AddWaker(&timeWaitWaker) |
| 1692 |
timeWaitTimer := e.stack.Clock().AfterFunc(timeWaitDuration, timeWaitWaker.Assert) |
| 1693 |
defer timeWaitTimer.Stop() |
| 1694 |
|
| 1695 |
for { |
| 1696 |
e.mu.Unlock() |
| 1697 |
w := s.Fetch(true ) |
| 1698 |
e.mu.Lock() |
| 1699 |
switch w { |
| 1700 |
case &e.newSegmentWaker: |
| 1701 |
extendTimeWait, reuseTW := e.handleTimeWaitSegments() |
| 1702 |
if reuseTW != nil { |
| 1703 |
return reuseTW |
| 1704 |
} |
| 1705 |
if extendTimeWait { |
| 1706 |
timeWaitTimer.Reset(timeWaitDuration) |
| 1707 |
} |
| 1708 |
case &e.notificationWaker: |
| 1709 |
n := e.fetchNotifications() |
| 1710 |
if n¬ifyAbort != 0 { |
| 1711 |
return nil |
| 1712 |
} |
| 1713 |
if n¬ifyDrain != 0 { |
| 1714 |
for !e.segmentQueue.empty() { |
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
e.handleTimeWaitSegments() |
| 1719 |
} |
| 1720 |
close(e.drainDone) |
| 1721 |
e.mu.Unlock() |
| 1722 |
<-e.undrain |
| 1723 |
e.mu.Lock() |
| 1724 |
return nil |
| 1725 |
} |
| 1726 |
case &timeWaitWaker: |
| 1727 |
return nil |
| 1728 |
} |
| 1729 |
} |
| 1730 |
} |