| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package tcp |
| 16 |
|
| 17 |
import ( |
| 18 |
"container/list" |
| 19 |
"crypto/sha1" |
| 20 |
"encoding/binary" |
| 21 |
"fmt" |
| 22 |
"hash" |
| 23 |
"io" |
| 24 |
"time" |
| 25 |
|
| 26 |
"inet.af/netstack/sleep" |
| 27 |
"inet.af/netstack/sync" |
| 28 |
"inet.af/netstack/tcpip" |
| 29 |
"inet.af/netstack/tcpip/header" |
| 30 |
"inet.af/netstack/tcpip/ports" |
| 31 |
"inet.af/netstack/tcpip/seqnum" |
| 32 |
"inet.af/netstack/tcpip/stack" |
| 33 |
"inet.af/netstack/waiter" |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
|
| 38 |
tsLen = 8 |
| 39 |
|
| 40 |
|
| 41 |
tsMask = (1 << tsLen) - 1 |
| 42 |
|
| 43 |
|
| 44 |
tsOffset = 24 |
| 45 |
|
| 46 |
|
| 47 |
hashMask = (1 << tsOffset) - 1 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
maxTSDiff = 2 |
| 53 |
) |
| 54 |
|
| 55 |
var ( |
| 56 |
|
| 57 |
|
| 58 |
mssTable = []uint16{536, 1300, 1440, 1460} |
| 59 |
) |
| 60 |
|
| 61 |
func encodeMSS(mss uint16) uint32 { |
| 62 |
for i := len(mssTable) - 1; i > 0; i-- { |
| 63 |
if mss >= mssTable[i] { |
| 64 |
return uint32(i) |
| 65 |
} |
| 66 |
} |
| 67 |
return 0 |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
type listenContext struct { |
| 75 |
stack *stack.Stack |
| 76 |
protocol *protocol |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
rcvWnd seqnum.Size |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
nonce [2][sha1.BlockSize]byte |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
listenEP *endpoint |
| 90 |
|
| 91 |
|
| 92 |
hasherMu sync.Mutex |
| 93 |
|
| 94 |
hasher hash.Hash |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
v6Only bool |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
netProto tcpip.NetworkProtocolNumber |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func timeStamp(clock tcpip.Clock) uint32 { |
| 107 |
return uint32(clock.NowMonotonic().Sub(tcpip.MonotonicTime{}).Seconds()) >> 6 & tsMask |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func newListenContext(stk *stack.Stack, protocol *protocol, listenEP *endpoint, rcvWnd seqnum.Size, v6Only bool, netProto tcpip.NetworkProtocolNumber) *listenContext { |
| 112 |
l := &listenContext{ |
| 113 |
stack: stk, |
| 114 |
protocol: protocol, |
| 115 |
rcvWnd: rcvWnd, |
| 116 |
hasher: sha1.New(), |
| 117 |
v6Only: v6Only, |
| 118 |
netProto: netProto, |
| 119 |
listenEP: listenEP, |
| 120 |
} |
| 121 |
|
| 122 |
for i := range l.nonce { |
| 123 |
if _, err := io.ReadFull(stk.SecureRNG(), l.nonce[i][:]); err != nil { |
| 124 |
panic(err) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return l |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func (l *listenContext) cookieHash(id stack.TransportEndpointID, ts uint32, nonceIndex int) uint32 { |
| 134 |
|
| 135 |
|
| 136 |
var payload [8]byte |
| 137 |
binary.BigEndian.PutUint16(payload[0:], id.LocalPort) |
| 138 |
binary.BigEndian.PutUint16(payload[2:], id.RemotePort) |
| 139 |
binary.BigEndian.PutUint32(payload[4:], ts) |
| 140 |
|
| 141 |
|
| 142 |
l.hasherMu.Lock() |
| 143 |
l.hasher.Reset() |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
l.hasher.Write(payload[:]) |
| 149 |
l.hasher.Write(l.nonce[nonceIndex][:]) |
| 150 |
l.hasher.Write([]byte(id.LocalAddress)) |
| 151 |
l.hasher.Write([]byte(id.RemoteAddress)) |
| 152 |
|
| 153 |
|
| 154 |
h := l.hasher.Sum(nil) |
| 155 |
l.hasherMu.Unlock() |
| 156 |
|
| 157 |
return binary.BigEndian.Uint32(h[:]) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
func (l *listenContext) createCookie(id stack.TransportEndpointID, seq seqnum.Value, data uint32) seqnum.Value { |
| 163 |
ts := timeStamp(l.stack.Clock()) |
| 164 |
v := l.cookieHash(id, 0, 0) + uint32(seq) + (ts << tsOffset) |
| 165 |
v += (l.cookieHash(id, ts, 1) + data) & hashMask |
| 166 |
return seqnum.Value(v) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
func (l *listenContext) isCookieValid(id stack.TransportEndpointID, cookie seqnum.Value, seq seqnum.Value) (uint32, bool) { |
| 173 |
ts := timeStamp(l.stack.Clock()) |
| 174 |
v := uint32(cookie) - l.cookieHash(id, 0, 0) - uint32(seq) |
| 175 |
cookieTS := v >> tsOffset |
| 176 |
if ((ts - cookieTS) & tsMask) > maxTSDiff { |
| 177 |
return 0, false |
| 178 |
} |
| 179 |
|
| 180 |
return (v - l.cookieHash(id, cookieTS, 1)) & hashMask, true |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
func (l *listenContext) createConnectingEndpoint(s *segment, rcvdSynOpts header.TCPSynOptions, queue *waiter.Queue) (*endpoint, tcpip.Error) { |
| 186 |
|
| 187 |
netProto := l.netProto |
| 188 |
if netProto == 0 { |
| 189 |
netProto = s.netProto |
| 190 |
} |
| 191 |
|
| 192 |
route, err := l.stack.FindRoute(s.nicID, s.dstAddr, s.srcAddr, s.netProto, false ) |
| 193 |
if err != nil { |
| 194 |
return nil, err |
| 195 |
} |
| 196 |
|
| 197 |
n := newEndpoint(l.stack, l.protocol, netProto, queue) |
| 198 |
n.ops.SetV6Only(l.v6Only) |
| 199 |
n.TransportEndpointInfo.ID = s.id |
| 200 |
n.boundNICID = s.nicID |
| 201 |
n.route = route |
| 202 |
n.effectiveNetProtos = []tcpip.NetworkProtocolNumber{s.netProto} |
| 203 |
n.ops.SetReceiveBufferSize(int64(l.rcvWnd), false ) |
| 204 |
n.amss = calculateAdvertisedMSS(n.userMSS, n.route) |
| 205 |
n.setEndpointState(StateConnecting) |
| 206 |
|
| 207 |
n.maybeEnableTimestamp(rcvdSynOpts) |
| 208 |
n.maybeEnableSACKPermitted(rcvdSynOpts) |
| 209 |
|
| 210 |
n.initGSO() |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
n.rcvQueueInfo.RcvAutoParams.PrevCopiedBytes = n.initialReceiveWindow() |
| 216 |
|
| 217 |
return n, nil |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
func (l *listenContext) startHandshake(s *segment, opts header.TCPSynOptions, queue *waiter.Queue, owner tcpip.PacketOwner) (*handshake, tcpip.Error) { |
| 229 |
|
| 230 |
irs := s.sequenceNumber |
| 231 |
isn := generateSecureISN(s.id, l.stack.Clock(), l.protocol.seqnumSecret) |
| 232 |
ep, err := l.createConnectingEndpoint(s, opts, queue) |
| 233 |
if err != nil { |
| 234 |
return nil, err |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
ep.mu.Lock() |
| 241 |
ep.owner = owner |
| 242 |
|
| 243 |
|
| 244 |
deferAccept := time.Duration(0) |
| 245 |
if l.listenEP != nil { |
| 246 |
if l.listenEP.EndpointState() != StateListen { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
ep.mu.Unlock() |
| 251 |
ep.Close() |
| 252 |
|
| 253 |
return nil, &tcpip.ErrConnectionAborted{} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
l.listenEP.propagateInheritableOptionsLocked(ep) |
| 259 |
|
| 260 |
if !ep.reserveTupleLocked() { |
| 261 |
ep.mu.Unlock() |
| 262 |
ep.Close() |
| 263 |
|
| 264 |
return nil, &tcpip.ErrConnectionAborted{} |
| 265 |
} |
| 266 |
|
| 267 |
deferAccept = l.listenEP.deferAccept |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
if err := ep.stack.RegisterTransportEndpoint( |
| 272 |
ep.effectiveNetProtos, |
| 273 |
ProtocolNumber, |
| 274 |
ep.TransportEndpointInfo.ID, |
| 275 |
ep, |
| 276 |
ep.boundPortFlags, |
| 277 |
ep.boundBindToDevice, |
| 278 |
); err != nil { |
| 279 |
ep.mu.Unlock() |
| 280 |
ep.Close() |
| 281 |
|
| 282 |
ep.drainClosingSegmentQueue() |
| 283 |
|
| 284 |
return nil, err |
| 285 |
} |
| 286 |
|
| 287 |
ep.isRegistered = true |
| 288 |
|
| 289 |
|
| 290 |
h := ep.newPassiveHandshake(isn, irs, opts, deferAccept) |
| 291 |
h.listenEP = l.listenEP |
| 292 |
h.start() |
| 293 |
return h, nil |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
func (l *listenContext) performHandshake(s *segment, opts header.TCPSynOptions, queue *waiter.Queue, owner tcpip.PacketOwner) (*endpoint, tcpip.Error) { |
| 301 |
h, err := l.startHandshake(s, opts, queue, owner) |
| 302 |
if err != nil { |
| 303 |
return nil, err |
| 304 |
} |
| 305 |
ep := h.ep |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
if err := h.complete(); err != nil { |
| 310 |
ep.stack.Stats().TCP.FailedConnectionAttempts.Increment() |
| 311 |
ep.stats.FailedConnectionAttempts.Increment() |
| 312 |
l.cleanupFailedHandshake(h) |
| 313 |
return nil, err |
| 314 |
} |
| 315 |
l.cleanupCompletedHandshake(h) |
| 316 |
return ep, nil |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
func (l *listenContext) cleanupFailedHandshake(h *handshake) { |
| 321 |
e := h.ep |
| 322 |
e.mu.Unlock() |
| 323 |
e.Close() |
| 324 |
e.notifyAborted() |
| 325 |
e.drainClosingSegmentQueue() |
| 326 |
e.h = nil |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
func (l *listenContext) cleanupCompletedHandshake(h *handshake) { |
| 334 |
e := h.ep |
| 335 |
e.isConnectNotified = true |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
e.rcv.RcvWndScale = e.h.effectiveRcvWndScale() |
| 341 |
|
| 342 |
|
| 343 |
e.h = nil |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
func (e *endpoint) propagateInheritableOptionsLocked(n *endpoint) { |
| 352 |
n.userTimeout = e.userTimeout |
| 353 |
n.portFlags = e.portFlags |
| 354 |
n.boundBindToDevice = e.boundBindToDevice |
| 355 |
n.boundPortFlags = e.boundPortFlags |
| 356 |
n.userMSS = e.userMSS |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
func (e *endpoint) reserveTupleLocked() bool { |
| 365 |
dest := tcpip.FullAddress{ |
| 366 |
Addr: e.TransportEndpointInfo.ID.RemoteAddress, |
| 367 |
Port: e.TransportEndpointInfo.ID.RemotePort, |
| 368 |
} |
| 369 |
portRes := ports.Reservation{ |
| 370 |
Networks: e.effectiveNetProtos, |
| 371 |
Transport: ProtocolNumber, |
| 372 |
Addr: e.TransportEndpointInfo.ID.LocalAddress, |
| 373 |
Port: e.TransportEndpointInfo.ID.LocalPort, |
| 374 |
Flags: e.boundPortFlags, |
| 375 |
BindToDevice: e.boundBindToDevice, |
| 376 |
Dest: dest, |
| 377 |
} |
| 378 |
if !e.stack.ReserveTuple(portRes) { |
| 379 |
e.stack.Stats().TCP.FailedPortReservations.Increment() |
| 380 |
return false |
| 381 |
} |
| 382 |
|
| 383 |
e.isPortReserved = true |
| 384 |
e.boundDest = dest |
| 385 |
return true |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
func (e *endpoint) notifyAborted() { |
| 395 |
e.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.ReadableEvents | waiter.WritableEvents) |
| 396 |
} |
| 397 |
|
| 398 |
func (e *endpoint) acceptQueueIsFull() bool { |
| 399 |
e.acceptMu.Lock() |
| 400 |
full := e.acceptQueue.isFull() |
| 401 |
e.acceptMu.Unlock() |
| 402 |
return full |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
type acceptQueue struct { |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
endpoints list.List `state:".([]*endpoint)"` |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
pendingEndpoints map[*endpoint]struct{} |
| 415 |
|
| 416 |
|
| 417 |
capacity int |
| 418 |
} |
| 419 |
|
| 420 |
func (a *acceptQueue) isFull() bool { |
| 421 |
return a.endpoints.Len() == a.capacity |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
func (e *endpoint) handleListenSegment(ctx *listenContext, s *segment) tcpip.Error { |
| 429 |
e.rcvQueueInfo.rcvQueueMu.Lock() |
| 430 |
rcvClosed := e.rcvQueueInfo.RcvClosed |
| 431 |
e.rcvQueueInfo.rcvQueueMu.Unlock() |
| 432 |
if rcvClosed || s.flags.Contains(header.TCPFlagSyn|header.TCPFlagAck) { |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
return replyWithReset(e.stack, s, e.sendTOS, e.ttl) |
| 439 |
} |
| 440 |
|
| 441 |
switch { |
| 442 |
case s.flags.Contains(header.TCPFlagRst): |
| 443 |
e.stack.Stats().DroppedPackets.Increment() |
| 444 |
return nil |
| 445 |
|
| 446 |
case s.flags == header.TCPFlagSyn: |
| 447 |
if e.acceptQueueIsFull() { |
| 448 |
e.stack.Stats().TCP.ListenOverflowSynDrop.Increment() |
| 449 |
e.stats.ReceiveErrors.ListenOverflowSynDrop.Increment() |
| 450 |
e.stack.Stats().DroppedPackets.Increment() |
| 451 |
return nil |
| 452 |
} |
| 453 |
|
| 454 |
opts := parseSynSegmentOptions(s) |
| 455 |
|
| 456 |
useSynCookies, err := func() (bool, tcpip.Error) { |
| 457 |
var alwaysUseSynCookies tcpip.TCPAlwaysUseSynCookies |
| 458 |
if err := e.stack.TransportProtocolOption(header.TCPProtocolNumber, &alwaysUseSynCookies); err != nil { |
| 459 |
panic(fmt.Sprintf("TransportProtocolOption(%d, %T) = %s", header.TCPProtocolNumber, alwaysUseSynCookies, err)) |
| 460 |
} |
| 461 |
if alwaysUseSynCookies { |
| 462 |
return true, nil |
| 463 |
} |
| 464 |
e.acceptMu.Lock() |
| 465 |
defer e.acceptMu.Unlock() |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
if len(e.acceptQueue.pendingEndpoints) == e.acceptQueue.capacity-1 { |
| 472 |
return true, nil |
| 473 |
} |
| 474 |
|
| 475 |
h, err := ctx.startHandshake(s, opts, &waiter.Queue{}, e.owner) |
| 476 |
if err != nil { |
| 477 |
e.stack.Stats().TCP.FailedConnectionAttempts.Increment() |
| 478 |
e.stats.FailedConnectionAttempts.Increment() |
| 479 |
return false, err |
| 480 |
} |
| 481 |
|
| 482 |
e.acceptQueue.pendingEndpoints[h.ep] = struct{}{} |
| 483 |
e.pendingAccepted.Add(1) |
| 484 |
|
| 485 |
go func() { |
| 486 |
defer func() { |
| 487 |
e.pendingAccepted.Done() |
| 488 |
|
| 489 |
e.acceptMu.Lock() |
| 490 |
defer e.acceptMu.Unlock() |
| 491 |
delete(e.acceptQueue.pendingEndpoints, h.ep) |
| 492 |
}() |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
if err := h.complete(); err != nil { |
| 497 |
e.stack.Stats().TCP.FailedConnectionAttempts.Increment() |
| 498 |
e.stats.FailedConnectionAttempts.Increment() |
| 499 |
ctx.cleanupFailedHandshake(h) |
| 500 |
return |
| 501 |
} |
| 502 |
ctx.cleanupCompletedHandshake(h) |
| 503 |
h.ep.startAcceptedLoop() |
| 504 |
e.stack.Stats().TCP.PassiveConnectionOpenings.Increment() |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
delivered := func() bool { |
| 511 |
e.acceptMu.Lock() |
| 512 |
defer e.acceptMu.Unlock() |
| 513 |
for { |
| 514 |
|
| 515 |
if e.acceptQueue.capacity == 0 { |
| 516 |
return false |
| 517 |
} |
| 518 |
if e.acceptQueue.isFull() { |
| 519 |
e.acceptCond.Wait() |
| 520 |
continue |
| 521 |
} |
| 522 |
|
| 523 |
e.acceptQueue.endpoints.PushBack(h.ep) |
| 524 |
return true |
| 525 |
} |
| 526 |
}() |
| 527 |
|
| 528 |
if delivered { |
| 529 |
e.waiterQueue.Notify(waiter.ReadableEvents) |
| 530 |
} else { |
| 531 |
h.ep.notifyProtocolGoroutine(notifyReset) |
| 532 |
} |
| 533 |
}() |
| 534 |
|
| 535 |
return false, nil |
| 536 |
}() |
| 537 |
if err != nil { |
| 538 |
return err |
| 539 |
} |
| 540 |
if !useSynCookies { |
| 541 |
return nil |
| 542 |
} |
| 543 |
|
| 544 |
route, err := e.stack.FindRoute(s.nicID, s.dstAddr, s.srcAddr, s.netProto, false ) |
| 545 |
if err != nil { |
| 546 |
return err |
| 547 |
} |
| 548 |
defer route.Release() |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
synOpts := header.TCPSynOptions{ |
| 559 |
WS: -1, |
| 560 |
TS: opts.TS, |
| 561 |
TSEcr: opts.TSVal, |
| 562 |
MSS: calculateAdvertisedMSS(e.userMSS, route), |
| 563 |
} |
| 564 |
if opts.TS { |
| 565 |
offset := e.protocol.tsOffset(s.dstAddr, s.srcAddr) |
| 566 |
now := e.stack.Clock().NowMonotonic() |
| 567 |
synOpts.TSVal = offset.TSVal(now) |
| 568 |
} |
| 569 |
cookie := ctx.createCookie(s.id, s.sequenceNumber, encodeMSS(opts.MSS)) |
| 570 |
fields := tcpFields{ |
| 571 |
id: s.id, |
| 572 |
ttl: e.ttl, |
| 573 |
tos: e.sendTOS, |
| 574 |
flags: header.TCPFlagSyn | header.TCPFlagAck, |
| 575 |
seq: cookie, |
| 576 |
ack: s.sequenceNumber + 1, |
| 577 |
rcvWnd: ctx.rcvWnd, |
| 578 |
} |
| 579 |
if err := e.sendSynTCP(route, fields, synOpts); err != nil { |
| 580 |
return err |
| 581 |
} |
| 582 |
e.stack.Stats().TCP.ListenOverflowSynCookieSent.Increment() |
| 583 |
return nil |
| 584 |
|
| 585 |
case s.flags.Contains(header.TCPFlagAck): |
| 586 |
iss := s.ackNumber - 1 |
| 587 |
irs := s.sequenceNumber - 1 |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
data, ok := ctx.isCookieValid(s.id, iss, irs) |
| 600 |
if !ok || int(data) >= len(mssTable) { |
| 601 |
e.stack.Stats().TCP.ListenOverflowInvalidSynCookieRcvd.Increment() |
| 602 |
e.stack.Stats().DroppedPackets.Increment() |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
return replyWithReset(e.stack, s, e.sendTOS, e.ttl) |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
e.acceptMu.Lock() |
| 626 |
if e.acceptQueue.isFull() { |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
e.acceptMu.Unlock() |
| 633 |
e.stack.Stats().TCP.ListenOverflowAckDrop.Increment() |
| 634 |
e.stats.ReceiveErrors.ListenOverflowAckDrop.Increment() |
| 635 |
e.stack.Stats().DroppedPackets.Increment() |
| 636 |
return nil |
| 637 |
} |
| 638 |
|
| 639 |
e.stack.Stats().TCP.ListenOverflowSynCookieRcvd.Increment() |
| 640 |
|
| 641 |
rcvdSynOptions := header.TCPSynOptions{ |
| 642 |
MSS: mssTable[data], |
| 643 |
|
| 644 |
|
| 645 |
WS: -1, |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if s.parsedOptions.TS { |
| 653 |
rcvdSynOptions.TS = true |
| 654 |
rcvdSynOptions.TSVal = s.parsedOptions.TSVal |
| 655 |
rcvdSynOptions.TSEcr = s.parsedOptions.TSEcr |
| 656 |
} |
| 657 |
|
| 658 |
n, err := ctx.createConnectingEndpoint(s, rcvdSynOptions, &waiter.Queue{}) |
| 659 |
if err != nil { |
| 660 |
e.acceptMu.Unlock() |
| 661 |
return err |
| 662 |
} |
| 663 |
|
| 664 |
n.mu.Lock() |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
e.propagateInheritableOptionsLocked(n) |
| 669 |
|
| 670 |
if !n.reserveTupleLocked() { |
| 671 |
n.mu.Unlock() |
| 672 |
e.acceptMu.Unlock() |
| 673 |
n.Close() |
| 674 |
|
| 675 |
e.stack.Stats().TCP.FailedConnectionAttempts.Increment() |
| 676 |
e.stats.FailedConnectionAttempts.Increment() |
| 677 |
return nil |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
if err := n.stack.RegisterTransportEndpoint( |
| 682 |
n.effectiveNetProtos, |
| 683 |
ProtocolNumber, |
| 684 |
n.TransportEndpointInfo.ID, |
| 685 |
n, |
| 686 |
n.boundPortFlags, |
| 687 |
n.boundBindToDevice, |
| 688 |
); err != nil { |
| 689 |
n.mu.Unlock() |
| 690 |
e.acceptMu.Unlock() |
| 691 |
n.Close() |
| 692 |
|
| 693 |
e.stack.Stats().TCP.FailedConnectionAttempts.Increment() |
| 694 |
e.stats.FailedConnectionAttempts.Increment() |
| 695 |
return err |
| 696 |
} |
| 697 |
|
| 698 |
n.isRegistered = true |
| 699 |
n.TSOffset = n.protocol.tsOffset(s.dstAddr, s.srcAddr) |
| 700 |
|
| 701 |
|
| 702 |
n.isConnectNotified = true |
| 703 |
h := handshake{ |
| 704 |
ep: n, |
| 705 |
iss: iss, |
| 706 |
ackNum: irs + 1, |
| 707 |
rcvWnd: seqnum.Size(n.initialReceiveWindow()), |
| 708 |
sndWnd: s.window, |
| 709 |
rcvWndScale: e.rcvWndScaleForHandshake(), |
| 710 |
sndWndScale: rcvdSynOptions.WS, |
| 711 |
mss: rcvdSynOptions.MSS, |
| 712 |
sampleRTTWithTSOnly: true, |
| 713 |
} |
| 714 |
h.transitionToStateEstablishedLocked(s) |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
if (s.flags.Contains(header.TCPFlagFin) || s.data.Size() > 0) && n.enqueueSegment(s) { |
| 719 |
s.incRef() |
| 720 |
n.newSegmentWaker.Assert() |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
n.startAcceptedLoop() |
| 725 |
e.stack.Stats().TCP.PassiveConnectionOpenings.Increment() |
| 726 |
|
| 727 |
|
| 728 |
e.acceptQueue.endpoints.PushBack(n) |
| 729 |
e.acceptMu.Unlock() |
| 730 |
|
| 731 |
e.waiterQueue.Notify(waiter.ReadableEvents) |
| 732 |
return nil |
| 733 |
|
| 734 |
default: |
| 735 |
e.stack.Stats().DroppedPackets.Increment() |
| 736 |
return nil |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
func (e *endpoint) protocolListenLoop(rcvWnd seqnum.Size) { |
| 743 |
e.mu.Lock() |
| 744 |
v6Only := e.ops.GetV6Only() |
| 745 |
ctx := newListenContext(e.stack, e.protocol, e, rcvWnd, v6Only, e.NetProto) |
| 746 |
|
| 747 |
defer func() { |
| 748 |
e.setEndpointState(StateClose) |
| 749 |
|
| 750 |
|
| 751 |
e.completeWorkerLocked() |
| 752 |
|
| 753 |
if e.drainDone != nil { |
| 754 |
close(e.drainDone) |
| 755 |
} |
| 756 |
e.mu.Unlock() |
| 757 |
|
| 758 |
e.drainClosingSegmentQueue() |
| 759 |
|
| 760 |
|
| 761 |
e.waiterQueue.Notify(waiter.ReadableEvents | waiter.WritableEvents | waiter.EventHUp | waiter.EventErr) |
| 762 |
}() |
| 763 |
|
| 764 |
var s sleep.Sleeper |
| 765 |
s.AddWaker(&e.notificationWaker) |
| 766 |
s.AddWaker(&e.newSegmentWaker) |
| 767 |
defer s.Done() |
| 768 |
for { |
| 769 |
e.mu.Unlock() |
| 770 |
w := s.Fetch(true) |
| 771 |
e.mu.Lock() |
| 772 |
switch w { |
| 773 |
case &e.notificationWaker: |
| 774 |
n := e.fetchNotifications() |
| 775 |
if n¬ifyClose != 0 { |
| 776 |
return |
| 777 |
} |
| 778 |
if n¬ifyDrain != 0 { |
| 779 |
for !e.segmentQueue.empty() { |
| 780 |
s := e.segmentQueue.dequeue() |
| 781 |
|
| 782 |
|
| 783 |
_ = e.handleListenSegment(ctx, s) |
| 784 |
s.decRef() |
| 785 |
} |
| 786 |
close(e.drainDone) |
| 787 |
e.mu.Unlock() |
| 788 |
<-e.undrain |
| 789 |
e.mu.Lock() |
| 790 |
} |
| 791 |
|
| 792 |
case &e.newSegmentWaker: |
| 793 |
|
| 794 |
mayRequeue := true |
| 795 |
for i := 0; i < maxSegmentsPerWake; i++ { |
| 796 |
s := e.segmentQueue.dequeue() |
| 797 |
if s == nil { |
| 798 |
mayRequeue = false |
| 799 |
break |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
_ = e.handleListenSegment(ctx, s) |
| 805 |
s.decRef() |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
if mayRequeue && !e.segmentQueue.empty() { |
| 811 |
e.newSegmentWaker.Assert() |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |