1 /* 2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Andrew Galante, haiku.galante@gmail.com 7 * Axel Dörfler, axeld@pinc-software.de 8 * Hugo Santos, hugosantos@gmail.com 9 */ 10 11 12 #include "TCPEndpoint.h" 13 14 #include <netinet/in.h> 15 #include <netinet/ip.h> 16 #include <netinet/tcp.h> 17 #include <new> 18 #include <signal.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <stdint.h> 22 23 #include <KernelExport.h> 24 #include <Select.h> 25 26 #include <net_buffer.h> 27 #include <net_datalink.h> 28 #include <net_stat.h> 29 #include <NetBufferUtilities.h> 30 #include <NetUtilities.h> 31 32 #include <lock.h> 33 #include <tracing.h> 34 #include <util/AutoLock.h> 35 #include <util/list.h> 36 37 #include "EndpointManager.h" 38 39 40 // References: 41 // - RFC 793 - Transmission Control Protocol 42 // - RFC 813 - Window and Acknowledgement Strategy in TCP 43 // - RFC 1337 - TIME_WAIT Assassination Hazards in TCP 44 // 45 // Things this implementation currently doesn't implement: 46 // - TCP Slow Start, Congestion Avoidance, Fast Retransmit, and Fast Recovery, 47 // RFC 2001, RFC 2581, RFC 3042 48 // - NewReno Modification to TCP's Fast Recovery, RFC 2582 49 // - Explicit Congestion Notification (ECN), RFC 3168 50 // - SYN-Cache 51 // - SACK, Selective Acknowledgment - RFC 2018, RFC 2883, RFC 3517 52 // - Forward RTO-Recovery, RFC 4138 53 // - Time-Wait hash instead of keeping sockets alive 54 // 55 // Things incomplete in this implementation: 56 // - TCP Extensions for High Performance, RFC 1323 - RTTM, PAWS 57 58 #define PrintAddress(address) \ 59 AddressString(Domain(), address, true).Data() 60 61 //#define TRACE_TCP 62 //#define PROBE_TCP 63 64 #ifdef TRACE_TCP 65 // the space before ', ##args' is important in order for this to work with cpp 2.95 66 # define TRACE(format, args...) dprintf("%" B_PRId32 ": TCP [%" \ 67 B_PRIdBIGTIME "] %p (%12s) " format "\n", find_thread(NULL), \ 68 system_time(), this, name_for_state(fState) , ##args) 69 #else 70 # define TRACE(args...) do { } while (0) 71 #endif 72 73 #ifdef PROBE_TCP 74 # define PROBE(buffer, window) \ 75 dprintf("TCP PROBE %" B_PRIdBIGTIME " %s %s %" B_PRIu32 " snxt %" B_PRIu32 \ 76 " suna %" B_PRIu32 " cw %" B_PRIu32 " sst %" B_PRIu32 " win %" \ 77 B_PRIu32 " swin %" B_PRIu32 " smax-suna %" B_PRIu32 " savail %" \ 78 B_PRIuSIZE " sqused %" B_PRIuSIZE " rto %" B_PRIdBIGTIME "\n", \ 79 system_time(), PrintAddress(buffer->source), \ 80 PrintAddress(buffer->destination), buffer->size, fSendNext.Number(), \ 81 fSendUnacknowledged.Number(), fCongestionWindow, fSlowStartThreshold, \ 82 window, fSendWindow, (fSendMax - fSendUnacknowledged).Number(), \ 83 fSendQueue.Available(fSendNext), fSendQueue.Used(), fRetransmitTimeout) 84 #else 85 # define PROBE(buffer, window) do { } while (0) 86 #endif 87 88 #if TCP_TRACING 89 namespace TCPTracing { 90 91 class Receive : public AbstractTraceEntry { 92 public: 93 Receive(TCPEndpoint* endpoint, tcp_segment_header& segment, uint32 window, 94 net_buffer* buffer) 95 : 96 fEndpoint(endpoint), 97 fBuffer(buffer), 98 fBufferSize(buffer->size), 99 fSequence(segment.sequence), 100 fAcknowledge(segment.acknowledge), 101 fWindow(window), 102 fState(endpoint->State()), 103 fFlags(segment.flags) 104 { 105 Initialized(); 106 } 107 108 virtual void AddDump(TraceOutput& out) 109 { 110 out.Print("tcp:%p (%12s) receive buffer %p (%" B_PRIu32 " bytes), " 111 "flags %#" B_PRIx8 ", seq %" B_PRIu32 ", ack %" B_PRIu32 112 ", wnd %" B_PRIu32, fEndpoint, name_for_state(fState), fBuffer, 113 fBufferSize, fFlags, fSequence, fAcknowledge, fWindow); 114 } 115 116 protected: 117 TCPEndpoint* fEndpoint; 118 net_buffer* fBuffer; 119 uint32 fBufferSize; 120 uint32 fSequence; 121 uint32 fAcknowledge; 122 uint32 fWindow; 123 tcp_state fState; 124 uint8 fFlags; 125 }; 126 127 class Send : public AbstractTraceEntry { 128 public: 129 Send(TCPEndpoint* endpoint, tcp_segment_header& segment, net_buffer* buffer, 130 tcp_sequence firstSequence, tcp_sequence lastSequence) 131 : 132 fEndpoint(endpoint), 133 fBuffer(buffer), 134 fBufferSize(buffer->size), 135 fSequence(segment.sequence), 136 fAcknowledge(segment.acknowledge), 137 fFirstSequence(firstSequence.Number()), 138 fLastSequence(lastSequence.Number()), 139 fState(endpoint->State()), 140 fFlags(segment.flags) 141 { 142 Initialized(); 143 } 144 145 virtual void AddDump(TraceOutput& out) 146 { 147 out.Print("tcp:%p (%12s) send buffer %p (%" B_PRIu32 " bytes), " 148 "flags %#" B_PRIx8 ", seq %" B_PRIu32 ", ack %" B_PRIu32 149 ", first %" B_PRIu32 ", last %" B_PRIu32, fEndpoint, 150 name_for_state(fState), fBuffer, fBufferSize, fFlags, fSequence, 151 fAcknowledge, fFirstSequence, fLastSequence); 152 } 153 154 protected: 155 TCPEndpoint* fEndpoint; 156 net_buffer* fBuffer; 157 uint32 fBufferSize; 158 uint32 fSequence; 159 uint32 fAcknowledge; 160 uint32 fFirstSequence; 161 uint32 fLastSequence; 162 tcp_state fState; 163 uint8 fFlags; 164 }; 165 166 class State : public AbstractTraceEntry { 167 public: 168 State(TCPEndpoint* endpoint) 169 : 170 fEndpoint(endpoint), 171 fState(endpoint->State()) 172 { 173 Initialized(); 174 } 175 176 virtual void AddDump(TraceOutput& out) 177 { 178 out.Print("tcp:%p (%12s) state change", fEndpoint, 179 name_for_state(fState)); 180 } 181 182 protected: 183 TCPEndpoint* fEndpoint; 184 tcp_state fState; 185 }; 186 187 class Spawn : public AbstractTraceEntry { 188 public: 189 Spawn(TCPEndpoint* listeningEndpoint, TCPEndpoint* spawnedEndpoint) 190 : 191 fListeningEndpoint(listeningEndpoint), 192 fSpawnedEndpoint(spawnedEndpoint) 193 { 194 Initialized(); 195 } 196 197 virtual void AddDump(TraceOutput& out) 198 { 199 out.Print("tcp:%p spawns %p", fListeningEndpoint, fSpawnedEndpoint); 200 } 201 202 protected: 203 TCPEndpoint* fListeningEndpoint; 204 TCPEndpoint* fSpawnedEndpoint; 205 }; 206 207 class Error : public AbstractTraceEntry { 208 public: 209 Error(TCPEndpoint* endpoint, const char* error, int32 line) 210 : 211 fEndpoint(endpoint), 212 fLine(line), 213 fError(error), 214 fState(endpoint->State()) 215 { 216 Initialized(); 217 } 218 219 virtual void AddDump(TraceOutput& out) 220 { 221 out.Print("tcp:%p (%12s) error at line %" B_PRId32 ": %s", fEndpoint, 222 name_for_state(fState), fLine, fError); 223 } 224 225 protected: 226 TCPEndpoint* fEndpoint; 227 int32 fLine; 228 const char* fError; 229 tcp_state fState; 230 }; 231 232 class TimerSet : public AbstractTraceEntry { 233 public: 234 TimerSet(TCPEndpoint* endpoint, const char* which, bigtime_t timeout) 235 : 236 fEndpoint(endpoint), 237 fWhich(which), 238 fTimeout(timeout), 239 fState(endpoint->State()) 240 { 241 Initialized(); 242 } 243 244 virtual void AddDump(TraceOutput& out) 245 { 246 out.Print("tcp:%p (%12s) %s timer set to %" B_PRIdBIGTIME, fEndpoint, 247 name_for_state(fState), fWhich, fTimeout); 248 } 249 250 protected: 251 TCPEndpoint* fEndpoint; 252 const char* fWhich; 253 bigtime_t fTimeout; 254 tcp_state fState; 255 }; 256 257 class TimerTriggered : public AbstractTraceEntry { 258 public: 259 TimerTriggered(TCPEndpoint* endpoint, const char* which) 260 : 261 fEndpoint(endpoint), 262 fWhich(which), 263 fState(endpoint->State()) 264 { 265 Initialized(); 266 } 267 268 virtual void AddDump(TraceOutput& out) 269 { 270 out.Print("tcp:%p (%12s) %s timer triggered", fEndpoint, 271 name_for_state(fState), fWhich); 272 } 273 274 protected: 275 TCPEndpoint* fEndpoint; 276 const char* fWhich; 277 tcp_state fState; 278 }; 279 280 class APICall : public AbstractTraceEntry { 281 public: 282 APICall(TCPEndpoint* endpoint, const char* which) 283 : 284 fEndpoint(endpoint), 285 fWhich(which), 286 fState(endpoint->State()) 287 { 288 Initialized(); 289 } 290 291 virtual void AddDump(TraceOutput& out) 292 { 293 out.Print("tcp:%p (%12s) api call: %s", fEndpoint, 294 name_for_state(fState), fWhich); 295 } 296 297 protected: 298 TCPEndpoint* fEndpoint; 299 const char* fWhich; 300 tcp_state fState; 301 }; 302 303 } // namespace TCPTracing 304 305 # define T(x) new(std::nothrow) TCPTracing::x 306 #else 307 # define T(x) 308 #endif // TCP_TRACING 309 310 311 // constants for the fFlags field 312 enum { 313 FLAG_OPTION_WINDOW_SCALE = 0x01, 314 FLAG_OPTION_TIMESTAMP = 0x02, 315 // TODO: Should FLAG_NO_RECEIVE apply as well to received connections? 316 // That is, what is expected from accept() after a shutdown() 317 // is performed on a listen()ing socket. 318 FLAG_NO_RECEIVE = 0x04, 319 FLAG_CLOSED = 0x08, 320 FLAG_DELETE_ON_CLOSE = 0x10, 321 FLAG_LOCAL = 0x20, 322 FLAG_RECOVERY = 0x40, 323 FLAG_OPTION_SACK_PERMITTED = 0x80, 324 }; 325 326 327 static const int kTimestampFactor = 1000; 328 // conversion factor between usec system time and msec tcp time 329 330 331 static inline bigtime_t 332 absolute_timeout(bigtime_t timeout) 333 { 334 if (timeout == 0 || timeout == B_INFINITE_TIMEOUT) 335 return timeout; 336 337 return timeout + system_time(); 338 } 339 340 341 static inline status_t 342 posix_error(status_t error) 343 { 344 if (error == B_TIMED_OUT) 345 return B_WOULD_BLOCK; 346 347 return error; 348 } 349 350 351 static inline bool 352 in_window(const tcp_sequence& sequence, const tcp_sequence& receiveNext, 353 uint32 receiveWindow) 354 { 355 return sequence >= receiveNext && sequence < (receiveNext + receiveWindow); 356 } 357 358 359 static inline bool 360 segment_in_sequence(const tcp_segment_header& segment, int size, 361 const tcp_sequence& receiveNext, uint32 receiveWindow) 362 { 363 tcp_sequence sequence(segment.sequence); 364 if (size == 0) { 365 if (receiveWindow == 0) 366 return sequence == receiveNext; 367 return in_window(sequence, receiveNext, receiveWindow); 368 } else { 369 if (receiveWindow == 0) 370 return false; 371 return in_window(sequence, receiveNext, receiveWindow) 372 || in_window(sequence + size - 1, receiveNext, receiveWindow); 373 } 374 } 375 376 377 static inline bool 378 is_writable(tcp_state state) 379 { 380 return state == ESTABLISHED || state == FINISH_RECEIVED; 381 } 382 383 384 static inline bool 385 is_establishing(tcp_state state) 386 { 387 return state == SYNCHRONIZE_SENT || state == SYNCHRONIZE_RECEIVED; 388 } 389 390 391 static inline uint32 tcp_now() 392 { 393 return system_time() / kTimestampFactor; 394 } 395 396 397 static inline uint32 tcp_diff_timestamp(uint32 base) 398 { 399 uint32 now = tcp_now(); 400 401 if (now > base) 402 return now - base; 403 404 return now + UINT_MAX - base; 405 } 406 407 408 static inline bool 409 state_needs_finish(int32 state) 410 { 411 return state == WAIT_FOR_FINISH_ACKNOWLEDGE 412 || state == FINISH_SENT || state == CLOSING; 413 } 414 415 416 // #pragma mark - 417 418 419 TCPEndpoint::TCPEndpoint(net_socket* socket) 420 : 421 ProtocolSocket(socket), 422 fManager(NULL), 423 fOptions(0), 424 fSendWindowShift(0), 425 fReceiveWindowShift(0), 426 fSendUnacknowledged(0), 427 fSendNext(0), 428 fSendMax(0), 429 fSendUrgentOffset(0), 430 fSendWindow(0), 431 fSendMaxWindow(0), 432 fSendMaxSegmentSize(TCP_DEFAULT_MAX_SEGMENT_SIZE), 433 fSendMaxSegments(0), 434 fSendQueue(socket->send.buffer_size), 435 fInitialSendSequence(0), 436 fPreviousHighestAcknowledge(0), 437 fDuplicateAcknowledgeCount(0), 438 fPreviousFlightSize(0), 439 fRecover(0), 440 fRoute(NULL), 441 fReceiveNext(0), 442 fReceiveMaxAdvertised(0), 443 fReceiveWindow(socket->receive.buffer_size), 444 fReceiveMaxSegmentSize(TCP_DEFAULT_MAX_SEGMENT_SIZE), 445 fReceiveQueue(socket->receive.buffer_size), 446 fSmoothedRoundTripTime(0), 447 fRoundTripVariation(0), 448 fSendTime(0), 449 fRoundTripStartSequence(0), 450 fRetransmitTimeout(TCP_INITIAL_RTT), 451 fReceivedTimestamp(0), 452 fCongestionWindow(0), 453 fSlowStartThreshold(0), 454 fState(CLOSED), 455 fFlags(FLAG_OPTION_WINDOW_SCALE | FLAG_OPTION_TIMESTAMP | FLAG_OPTION_SACK_PERMITTED) 456 { 457 // TODO: to be replaced with a real read/write locking strategy! 458 mutex_init(&fLock, "tcp lock"); 459 460 fReceiveCondition.Init(this, "tcp receive"); 461 fSendCondition.Init(this, "tcp send"); 462 463 gStackModule->init_timer(&fPersistTimer, TCPEndpoint::_PersistTimer, this); 464 gStackModule->init_timer(&fRetransmitTimer, TCPEndpoint::_RetransmitTimer, 465 this); 466 gStackModule->init_timer(&fDelayedAcknowledgeTimer, 467 TCPEndpoint::_DelayedAcknowledgeTimer, this); 468 gStackModule->init_timer(&fTimeWaitTimer, TCPEndpoint::_TimeWaitTimer, 469 this); 470 471 T(APICall(this, "constructor")); 472 } 473 474 475 TCPEndpoint::~TCPEndpoint() 476 { 477 mutex_lock(&fLock); 478 479 T(APICall(this, "destructor")); 480 481 _CancelConnectionTimers(); 482 gStackModule->cancel_timer(&fTimeWaitTimer); 483 T(TimerSet(this, "time-wait", -1)); 484 485 if (fManager != NULL) { 486 fManager->Unbind(this); 487 put_endpoint_manager(fManager); 488 } 489 490 mutex_destroy(&fLock); 491 492 // we need to wait for all timers to return 493 gStackModule->wait_for_timer(&fRetransmitTimer); 494 gStackModule->wait_for_timer(&fPersistTimer); 495 gStackModule->wait_for_timer(&fDelayedAcknowledgeTimer); 496 gStackModule->wait_for_timer(&fTimeWaitTimer); 497 498 gDatalinkModule->put_route(Domain(), fRoute); 499 } 500 501 502 status_t 503 TCPEndpoint::InitCheck() const 504 { 505 return B_OK; 506 } 507 508 509 // #pragma mark - protocol API 510 511 512 status_t 513 TCPEndpoint::Open() 514 { 515 TRACE("Open()"); 516 T(APICall(this, "open")); 517 518 status_t status = ProtocolSocket::Open(); 519 if (status < B_OK) 520 return status; 521 522 fManager = get_endpoint_manager(Domain()); 523 if (fManager == NULL) 524 return EAFNOSUPPORT; 525 526 return B_OK; 527 } 528 529 530 status_t 531 TCPEndpoint::Close() 532 { 533 MutexLocker locker(fLock); 534 535 TRACE("Close()"); 536 T(APICall(this, "close")); 537 538 if (fState == LISTEN) 539 delete_sem(fAcceptSemaphore); 540 541 if (fState == SYNCHRONIZE_SENT || fState == LISTEN) { 542 // TODO: what about linger in case of SYNCHRONIZE_SENT? 543 fState = CLOSED; 544 T(State(this)); 545 return B_OK; 546 } 547 548 status_t status = _Disconnect(true); 549 if (status != B_OK) 550 return status; 551 552 if (socket->options & SO_LINGER) { 553 TRACE("Close(): Lingering for %i secs", socket->linger); 554 555 bigtime_t maximum = absolute_timeout(socket->linger * 1000000LL); 556 557 while (fSendQueue.Used() > 0) { 558 status = _WaitForCondition(fSendCondition, locker, maximum); 559 if (status == B_TIMED_OUT || status == B_WOULD_BLOCK) 560 break; 561 else if (status < B_OK) 562 return status; 563 } 564 565 TRACE("Close(): after waiting, the SendQ was left with %" B_PRIuSIZE 566 " bytes.", fSendQueue.Used()); 567 } 568 return B_OK; 569 } 570 571 572 void 573 TCPEndpoint::Free() 574 { 575 MutexLocker _(fLock); 576 577 TRACE("Free()"); 578 T(APICall(this, "free")); 579 580 if (fState <= SYNCHRONIZE_SENT) 581 return; 582 583 // we are only interested in the timer, not in changing state 584 _EnterTimeWait(); 585 586 fFlags |= FLAG_CLOSED; 587 if ((fFlags & FLAG_DELETE_ON_CLOSE) == 0) { 588 // we'll be freed later when the 2MSL timer expires 589 gSocketModule->acquire_socket(socket); 590 } 591 } 592 593 594 /*! Creates and sends a synchronize packet to /a address, and then waits 595 until the connection has been established or refused. 596 */ 597 status_t 598 TCPEndpoint::Connect(const sockaddr* address) 599 { 600 if (!AddressModule()->is_same_family(address)) 601 return EAFNOSUPPORT; 602 603 MutexLocker locker(fLock); 604 605 TRACE("Connect() on address %s", PrintAddress(address)); 606 T(APICall(this, "connect")); 607 608 if (gStackModule->is_restarted_syscall()) { 609 bigtime_t timeout = gStackModule->restore_syscall_restart_timeout(); 610 status_t status = _WaitForEstablished(locker, timeout); 611 TRACE(" Connect(): Connection complete: %s (timeout was %" 612 B_PRIdBIGTIME ")", strerror(status), timeout); 613 return posix_error(status); 614 } 615 616 // Can only call connect() from CLOSED or LISTEN states 617 // otherwise endpoint is considered already connected 618 if (fState == LISTEN) { 619 // this socket is about to connect; remove pending connections in the backlog 620 gSocketModule->set_max_backlog(socket, 0); 621 } else if (fState == ESTABLISHED) { 622 return EISCONN; 623 } else if (fState != CLOSED) 624 return EALREADY; 625 626 // consider destination address INADDR_ANY as INADDR_LOOPBACK 627 sockaddr_storage _address; 628 if (AddressModule()->is_empty_address(address, false)) { 629 AddressModule()->get_loopback_address((sockaddr *)&_address); 630 // for IPv4 and IPv6 the port is at the same offset 631 ((sockaddr_in &)_address).sin_port = ((sockaddr_in *)address)->sin_port; 632 address = (sockaddr *)&_address; 633 } 634 635 status_t status = _PrepareSendPath(address); 636 if (status < B_OK) 637 return status; 638 639 TRACE(" Connect(): starting 3-way handshake..."); 640 641 fState = SYNCHRONIZE_SENT; 642 T(State(this)); 643 644 // send SYN 645 status = _SendQueued(); 646 if (status != B_OK) { 647 _Close(); 648 return status; 649 } 650 651 // If we are running over Loopback, after _SendQueued() returns we 652 // may be in ESTABLISHED already. 653 if (fState == ESTABLISHED) { 654 TRACE(" Connect() completed after _SendQueued()"); 655 return B_OK; 656 } 657 658 // wait until 3-way handshake is complete (if needed) 659 bigtime_t timeout = min_c(socket->send.timeout, TCP_CONNECTION_TIMEOUT); 660 if (timeout == 0) { 661 // we're a non-blocking socket 662 TRACE(" Connect() delayed, return EINPROGRESS"); 663 return EINPROGRESS; 664 } 665 666 bigtime_t absoluteTimeout = absolute_timeout(timeout); 667 gStackModule->store_syscall_restart_timeout(absoluteTimeout); 668 669 status = _WaitForEstablished(locker, absoluteTimeout); 670 TRACE(" Connect(): Connection complete: %s (timeout was %" B_PRIdBIGTIME 671 ")", strerror(status), timeout); 672 return posix_error(status); 673 } 674 675 676 status_t 677 TCPEndpoint::Accept(struct net_socket** _acceptedSocket) 678 { 679 MutexLocker locker(fLock); 680 681 TRACE("Accept()"); 682 T(APICall(this, "accept")); 683 684 status_t status; 685 bigtime_t timeout = absolute_timeout(socket->receive.timeout); 686 if (gStackModule->is_restarted_syscall()) 687 timeout = gStackModule->restore_syscall_restart_timeout(); 688 else 689 gStackModule->store_syscall_restart_timeout(timeout); 690 691 do { 692 locker.Unlock(); 693 694 status = acquire_sem_etc(fAcceptSemaphore, 1, B_ABSOLUTE_TIMEOUT 695 | B_CAN_INTERRUPT, timeout); 696 if (status != B_OK) { 697 if (status == B_TIMED_OUT && socket->receive.timeout == 0) 698 return B_WOULD_BLOCK; 699 700 return status; 701 } 702 703 locker.Lock(); 704 status = gSocketModule->dequeue_connected(socket, _acceptedSocket); 705 #ifdef TRACE_TCP 706 if (status == B_OK) 707 TRACE(" Accept() returning %p", (*_acceptedSocket)->first_protocol); 708 #endif 709 } while (status != B_OK); 710 711 return status; 712 } 713 714 715 status_t 716 TCPEndpoint::Bind(const sockaddr *address) 717 { 718 if (address == NULL) 719 return B_BAD_VALUE; 720 721 MutexLocker lock(fLock); 722 723 TRACE("Bind() on address %s", PrintAddress(address)); 724 T(APICall(this, "bind")); 725 726 if (fState != CLOSED) 727 return EISCONN; 728 729 return fManager->Bind(this, address); 730 } 731 732 733 status_t 734 TCPEndpoint::Unbind(struct sockaddr *address) 735 { 736 MutexLocker _(fLock); 737 738 TRACE("Unbind()"); 739 T(APICall(this, "unbind")); 740 741 return fManager->Unbind(this); 742 } 743 744 745 status_t 746 TCPEndpoint::Listen(int count) 747 { 748 MutexLocker _(fLock); 749 750 TRACE("Listen()"); 751 T(APICall(this, "listen")); 752 753 if (fState != CLOSED && fState != LISTEN) 754 return B_BAD_VALUE; 755 756 if (fState == CLOSED) { 757 fAcceptSemaphore = create_sem(0, "tcp accept"); 758 if (fAcceptSemaphore < B_OK) 759 return ENOBUFS; 760 761 status_t status = fManager->SetPassive(this); 762 if (status != B_OK) { 763 delete_sem(fAcceptSemaphore); 764 fAcceptSemaphore = -1; 765 return status; 766 } 767 } 768 769 gSocketModule->set_max_backlog(socket, count); 770 771 fState = LISTEN; 772 T(State(this)); 773 return B_OK; 774 } 775 776 777 status_t 778 TCPEndpoint::Shutdown(int direction) 779 { 780 MutexLocker lock(fLock); 781 782 TRACE("Shutdown(%i)", direction); 783 T(APICall(this, "shutdown")); 784 785 if (direction == SHUT_RD || direction == SHUT_RDWR) 786 fFlags |= FLAG_NO_RECEIVE; 787 788 if (direction == SHUT_WR || direction == SHUT_RDWR) { 789 // TODO: That's not correct. After read/write shutting down the socket 790 // one should still be able to read previously arrived data. 791 _Disconnect(false); 792 } 793 794 return B_OK; 795 } 796 797 798 /*! Puts data contained in \a buffer into send buffer */ 799 status_t 800 TCPEndpoint::SendData(net_buffer *buffer) 801 { 802 MutexLocker lock(fLock); 803 804 TRACE("SendData(buffer %p, size %" B_PRIu32 ", flags %#" B_PRIx32 805 ") [total %" B_PRIuSIZE " bytes, has %" B_PRIuSIZE "]", buffer, 806 buffer->size, buffer->flags, fSendQueue.Size(), fSendQueue.Free()); 807 T(APICall(this, "senddata")); 808 809 uint32 flags = buffer->flags; 810 811 if (fState == CLOSED) 812 return ENOTCONN; 813 if (fState == LISTEN) 814 return EDESTADDRREQ; 815 if (!is_writable(fState) && !is_establishing(fState)) { 816 // we only send signals when called from userland 817 if (gStackModule->is_syscall() && (flags & MSG_NOSIGNAL) == 0) 818 send_signal(find_thread(NULL), SIGPIPE); 819 return EPIPE; 820 } 821 822 size_t left = buffer->size; 823 824 bigtime_t timeout = absolute_timeout(socket->send.timeout); 825 if (gStackModule->is_restarted_syscall()) 826 timeout = gStackModule->restore_syscall_restart_timeout(); 827 else 828 gStackModule->store_syscall_restart_timeout(timeout); 829 830 while (left > 0) { 831 while (fSendQueue.Free() < socket->send.low_water_mark) { 832 // wait until enough space is available 833 status_t status = _WaitForCondition(fSendCondition, lock, timeout); 834 if (status < B_OK) { 835 TRACE(" SendData() returning %s (%d)", 836 strerror(posix_error(status)), (int)posix_error(status)); 837 return posix_error(status); 838 } 839 840 if (!is_writable(fState) && !is_establishing(fState)) { 841 // we only send signals when called from userland 842 if (gStackModule->is_syscall()) 843 send_signal(find_thread(NULL), SIGPIPE); 844 return EPIPE; 845 } 846 } 847 848 size_t size = fSendQueue.Free(); 849 if (size < left) { 850 // we need to split the original buffer 851 net_buffer* clone = gBufferModule->clone(buffer, false); 852 // TODO: add offset/size parameter to net_buffer::clone() or 853 // even a move_data() function, as this is a bit inefficient 854 if (clone == NULL) 855 return ENOBUFS; 856 857 status_t status = gBufferModule->trim(clone, size); 858 if (status != B_OK) { 859 gBufferModule->free(clone); 860 return status; 861 } 862 863 gBufferModule->remove_header(buffer, size); 864 left -= size; 865 fSendQueue.Add(clone); 866 } else { 867 left -= buffer->size; 868 fSendQueue.Add(buffer); 869 } 870 } 871 872 TRACE(" SendData(): %" B_PRIuSIZE " bytes used.", fSendQueue.Used()); 873 874 bool force = false; 875 if ((flags & MSG_OOB) != 0) { 876 fSendUrgentOffset = fSendQueue.LastSequence(); 877 // RFC 961 specifies that the urgent offset points to the last 878 // byte of urgent data. However, this is commonly implemented as 879 // here, ie. it points to the first byte after the urgent data. 880 force = true; 881 } 882 if ((flags & MSG_EOF) != 0) 883 _Disconnect(false); 884 885 if (fState == ESTABLISHED || fState == FINISH_RECEIVED) 886 _SendQueued(force); 887 888 return B_OK; 889 } 890 891 892 ssize_t 893 TCPEndpoint::SendAvailable() 894 { 895 MutexLocker locker(fLock); 896 897 ssize_t available; 898 899 if (is_writable(fState)) 900 available = fSendQueue.Free(); 901 else if (is_establishing(fState)) 902 available = 0; 903 else 904 available = EPIPE; 905 906 TRACE("SendAvailable(): %" B_PRIdSSIZE, available); 907 T(APICall(this, "sendavailable")); 908 return available; 909 } 910 911 912 status_t 913 TCPEndpoint::FillStat(net_stat *stat) 914 { 915 MutexLocker _(fLock); 916 917 strlcpy(stat->state, name_for_state(fState), sizeof(stat->state)); 918 stat->receive_queue_size = fReceiveQueue.Available(); 919 stat->send_queue_size = fSendQueue.Used(); 920 921 return B_OK; 922 } 923 924 925 status_t 926 TCPEndpoint::ReadData(size_t numBytes, uint32 flags, net_buffer** _buffer) 927 { 928 MutexLocker locker(fLock); 929 930 TRACE("ReadData(%" B_PRIuSIZE " bytes, flags %#" B_PRIx32 ")", numBytes, 931 flags); 932 T(APICall(this, "readdata")); 933 934 *_buffer = NULL; 935 936 if (fState == CLOSED) 937 return ENOTCONN; 938 939 bigtime_t timeout = absolute_timeout(socket->receive.timeout); 940 if (gStackModule->is_restarted_syscall()) 941 timeout = gStackModule->restore_syscall_restart_timeout(); 942 else 943 gStackModule->store_syscall_restart_timeout(timeout); 944 945 if (fState == SYNCHRONIZE_SENT || fState == SYNCHRONIZE_RECEIVED) { 946 if (flags & MSG_DONTWAIT) 947 return B_WOULD_BLOCK; 948 949 status_t status = _WaitForEstablished(locker, timeout); 950 if (status < B_OK) 951 return posix_error(status); 952 } 953 954 size_t dataNeeded = socket->receive.low_water_mark; 955 956 // When MSG_WAITALL is set then the function should block 957 // until the full amount of data can be returned. 958 if (flags & MSG_WAITALL) 959 dataNeeded = numBytes; 960 961 // TODO: add support for urgent data (MSG_OOB) 962 963 while (true) { 964 if (fState == CLOSING || fState == WAIT_FOR_FINISH_ACKNOWLEDGE 965 || fState == TIME_WAIT) { 966 // ``Connection closing''. 967 return B_OK; 968 } 969 970 if (fReceiveQueue.Available() > 0) { 971 if (fReceiveQueue.Available() >= dataNeeded 972 || (fReceiveQueue.PushedData() > 0 973 && fReceiveQueue.PushedData() >= fReceiveQueue.Available())) 974 break; 975 } else if (fState == FINISH_RECEIVED) { 976 // ``If no text is awaiting delivery, the RECEIVE will 977 // get a Connection closing''. 978 return B_OK; 979 } 980 981 if ((flags & MSG_DONTWAIT) != 0 || socket->receive.timeout == 0) 982 return B_WOULD_BLOCK; 983 984 if ((fFlags & FLAG_NO_RECEIVE) != 0) 985 return B_OK; 986 987 status_t status = _WaitForCondition(fReceiveCondition, locker, timeout); 988 if (status < B_OK) { 989 // The Open Group base specification mentions that EINTR should be 990 // returned if the recv() is interrupted before _any data_ is 991 // available. So we actually check if there is data, and if so, 992 // push it to the user. 993 if ((status == B_TIMED_OUT || status == B_INTERRUPTED) 994 && fReceiveQueue.Available() > 0) 995 break; 996 997 return posix_error(status); 998 } 999 } 1000 1001 TRACE(" ReadData(): %" B_PRIuSIZE " are available.", 1002 fReceiveQueue.Available()); 1003 1004 if (numBytes < fReceiveQueue.Available()) 1005 fReceiveCondition.NotifyAll(); 1006 1007 bool clone = (flags & MSG_PEEK) != 0; 1008 1009 ssize_t receivedBytes = fReceiveQueue.Get(numBytes, !clone, _buffer); 1010 1011 TRACE(" ReadData(): %" B_PRIuSIZE " bytes kept.", 1012 fReceiveQueue.Available()); 1013 1014 if (fReceiveQueue.Available() == 0 && fState == FINISH_RECEIVED) 1015 socket->receive.low_water_mark = 0; 1016 1017 // if we are opening the window, check if we should send an ACK 1018 if (!clone) 1019 SendAcknowledge(false); 1020 1021 return receivedBytes; 1022 } 1023 1024 1025 ssize_t 1026 TCPEndpoint::ReadAvailable() 1027 { 1028 MutexLocker locker(fLock); 1029 1030 TRACE("ReadAvailable(): %" B_PRIdSSIZE, _AvailableData()); 1031 T(APICall(this, "readavailable")); 1032 1033 return _AvailableData(); 1034 } 1035 1036 1037 status_t 1038 TCPEndpoint::SetSendBufferSize(size_t length) 1039 { 1040 MutexLocker _(fLock); 1041 fSendQueue.SetMaxBytes(length); 1042 return B_OK; 1043 } 1044 1045 1046 status_t 1047 TCPEndpoint::SetReceiveBufferSize(size_t length) 1048 { 1049 MutexLocker _(fLock); 1050 fReceiveQueue.SetMaxBytes(length); 1051 return B_OK; 1052 } 1053 1054 1055 status_t 1056 TCPEndpoint::GetOption(int option, void* _value, int* _length) 1057 { 1058 if (*_length != sizeof(int)) 1059 return B_BAD_VALUE; 1060 1061 int* value = (int*)_value; 1062 1063 switch (option) { 1064 case TCP_NODELAY: 1065 if ((fOptions & TCP_NODELAY) != 0) 1066 *value = 1; 1067 else 1068 *value = 0; 1069 return B_OK; 1070 1071 case TCP_MAXSEG: 1072 *value = fReceiveMaxSegmentSize; 1073 return B_OK; 1074 1075 default: 1076 return B_BAD_VALUE; 1077 } 1078 } 1079 1080 1081 status_t 1082 TCPEndpoint::SetOption(int option, const void* _value, int length) 1083 { 1084 if (option != TCP_NODELAY) 1085 return B_BAD_VALUE; 1086 1087 if (length != sizeof(int)) 1088 return B_BAD_VALUE; 1089 1090 const int* value = (const int*)_value; 1091 1092 MutexLocker _(fLock); 1093 if (*value) 1094 fOptions |= TCP_NODELAY; 1095 else 1096 fOptions &= ~TCP_NODELAY; 1097 1098 return B_OK; 1099 } 1100 1101 1102 // #pragma mark - misc 1103 1104 1105 bool 1106 TCPEndpoint::IsBound() const 1107 { 1108 return !LocalAddress().IsEmpty(true); 1109 } 1110 1111 1112 bool 1113 TCPEndpoint::IsLocal() const 1114 { 1115 return (fFlags & FLAG_LOCAL) != 0; 1116 } 1117 1118 1119 status_t 1120 TCPEndpoint::DelayedAcknowledge() 1121 { 1122 if (gStackModule->cancel_timer(&fDelayedAcknowledgeTimer)) { 1123 // timer was active, send an ACK now (with the exception above, 1124 // we send every other ACK) 1125 T(TimerSet(this, "delayed ack", -1)); 1126 return SendAcknowledge(true); 1127 } 1128 1129 gStackModule->set_timer(&fDelayedAcknowledgeTimer, 1130 TCP_DELAYED_ACKNOWLEDGE_TIMEOUT); 1131 T(TimerSet(this, "delayed ack", TCP_DELAYED_ACKNOWLEDGE_TIMEOUT)); 1132 return B_OK; 1133 } 1134 1135 1136 status_t 1137 TCPEndpoint::SendAcknowledge(bool force) 1138 { 1139 return _SendQueued(force, 0); 1140 } 1141 1142 1143 void 1144 TCPEndpoint::_StartPersistTimer() 1145 { 1146 gStackModule->set_timer(&fPersistTimer, TCP_PERSIST_TIMEOUT); 1147 T(TimerSet(this, "persist", TCP_PERSIST_TIMEOUT)); 1148 } 1149 1150 1151 void 1152 TCPEndpoint::_EnterTimeWait() 1153 { 1154 TRACE("_EnterTimeWait()"); 1155 1156 if (fState == TIME_WAIT) { 1157 _CancelConnectionTimers(); 1158 } 1159 1160 _UpdateTimeWait(); 1161 } 1162 1163 1164 void 1165 TCPEndpoint::_UpdateTimeWait() 1166 { 1167 gStackModule->set_timer(&fTimeWaitTimer, TCP_MAX_SEGMENT_LIFETIME << 1); 1168 T(TimerSet(this, "time-wait", TCP_MAX_SEGMENT_LIFETIME << 1)); 1169 } 1170 1171 1172 void 1173 TCPEndpoint::_CancelConnectionTimers() 1174 { 1175 gStackModule->cancel_timer(&fRetransmitTimer); 1176 T(TimerSet(this, "retransmit", -1)); 1177 gStackModule->cancel_timer(&fPersistTimer); 1178 T(TimerSet(this, "persist", -1)); 1179 gStackModule->cancel_timer(&fDelayedAcknowledgeTimer); 1180 T(TimerSet(this, "delayed ack", -1)); 1181 } 1182 1183 1184 /*! Sends the FIN flag to the peer when the connection is still open. 1185 Moves the endpoint to the next state depending on where it was. 1186 */ 1187 status_t 1188 TCPEndpoint::_Disconnect(bool closing) 1189 { 1190 tcp_state previousState = fState; 1191 1192 if (fState == SYNCHRONIZE_RECEIVED || fState == ESTABLISHED) 1193 fState = FINISH_SENT; 1194 else if (fState == FINISH_RECEIVED) 1195 fState = WAIT_FOR_FINISH_ACKNOWLEDGE; 1196 else 1197 return B_OK; 1198 1199 T(State(this)); 1200 1201 status_t status = _SendQueued(); 1202 if (status != B_OK) { 1203 fState = previousState; 1204 T(State(this)); 1205 return status; 1206 } 1207 1208 return B_OK; 1209 } 1210 1211 1212 void 1213 TCPEndpoint::_MarkEstablished() 1214 { 1215 fState = ESTABLISHED; 1216 T(State(this)); 1217 1218 gSocketModule->set_connected(socket); 1219 if (gSocketModule->has_parent(socket)) 1220 release_sem_etc(fAcceptSemaphore, 1, B_DO_NOT_RESCHEDULE); 1221 1222 fSendCondition.NotifyAll(); 1223 gSocketModule->notify(socket, B_SELECT_WRITE, fSendQueue.Free()); 1224 } 1225 1226 1227 status_t 1228 TCPEndpoint::_WaitForEstablished(MutexLocker &locker, bigtime_t timeout) 1229 { 1230 // TODO: Checking for CLOSED seems correct, but breaks several neon tests. 1231 // When investigating this, also have a look at _Close() and _HandleReset(). 1232 while (fState < ESTABLISHED/* && fState != CLOSED*/) { 1233 if (socket->error != B_OK) 1234 return socket->error; 1235 1236 status_t status = _WaitForCondition(fSendCondition, locker, timeout); 1237 if (status < B_OK) 1238 return status; 1239 } 1240 1241 return B_OK; 1242 } 1243 1244 1245 // #pragma mark - receive 1246 1247 1248 void 1249 TCPEndpoint::_Close() 1250 { 1251 _CancelConnectionTimers(); 1252 fState = CLOSED; 1253 T(State(this)); 1254 1255 fFlags |= FLAG_DELETE_ON_CLOSE; 1256 1257 fSendCondition.NotifyAll(); 1258 _NotifyReader(); 1259 1260 if (gSocketModule->has_parent(socket)) { 1261 // We still have a parent - obviously, we haven't been accepted yet, 1262 // so no one could ever close us. 1263 _CancelConnectionTimers(); 1264 gSocketModule->set_aborted(socket); 1265 } 1266 } 1267 1268 1269 void 1270 TCPEndpoint::_HandleReset(status_t error) 1271 { 1272 socket->error = error; 1273 _Close(); 1274 1275 gSocketModule->notify(socket, B_SELECT_WRITE, error); 1276 gSocketModule->notify(socket, B_SELECT_ERROR, error); 1277 } 1278 1279 1280 void 1281 TCPEndpoint::_DuplicateAcknowledge(tcp_segment_header &segment) 1282 { 1283 if (fDuplicateAcknowledgeCount == 0) 1284 fPreviousFlightSize = (fSendMax - fSendUnacknowledged).Number(); 1285 1286 if (++fDuplicateAcknowledgeCount < 3) { 1287 if (fSendQueue.Available(fSendMax) != 0 && fSendWindow != 0) { 1288 fSendNext = fSendMax; 1289 fCongestionWindow += fDuplicateAcknowledgeCount * fSendMaxSegmentSize; 1290 _SendQueued(); 1291 TRACE("_DuplicateAcknowledge(): packet sent under limited transmit on receipt of dup ack"); 1292 fCongestionWindow -= fDuplicateAcknowledgeCount * fSendMaxSegmentSize; 1293 } 1294 } 1295 1296 if (fDuplicateAcknowledgeCount == 3) { 1297 if ((segment.acknowledge - 1) > fRecover || (fCongestionWindow > fSendMaxSegmentSize && 1298 (fSendUnacknowledged - fPreviousHighestAcknowledge) <= 4 * fSendMaxSegmentSize)) { 1299 fFlags |= FLAG_RECOVERY; 1300 fRecover = fSendMax.Number() - 1; 1301 fSlowStartThreshold = max_c(fPreviousFlightSize / 2, 2 * fSendMaxSegmentSize); 1302 fCongestionWindow = fSlowStartThreshold + 3 * fSendMaxSegmentSize; 1303 fSendNext = segment.acknowledge; 1304 _SendQueued(); 1305 TRACE("_DuplicateAcknowledge(): packet sent under fast restransmit on the receipt of 3rd dup ack"); 1306 } 1307 } else if (fDuplicateAcknowledgeCount > 3) { 1308 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 1309 if ((fDuplicateAcknowledgeCount - 3) * fSendMaxSegmentSize <= flightSize) 1310 fCongestionWindow += fSendMaxSegmentSize; 1311 if (fSendQueue.Available(fSendMax) != 0) { 1312 fSendNext = fSendMax; 1313 _SendQueued(); 1314 } 1315 } 1316 } 1317 1318 1319 void 1320 TCPEndpoint::_UpdateTimestamps(tcp_segment_header& segment, 1321 size_t segmentLength) 1322 { 1323 if (fFlags & FLAG_OPTION_TIMESTAMP) { 1324 tcp_sequence sequence(segment.sequence); 1325 1326 if (fLastAcknowledgeSent >= sequence 1327 && fLastAcknowledgeSent < (sequence + segmentLength)) 1328 fReceivedTimestamp = segment.timestamp_value; 1329 } 1330 } 1331 1332 1333 ssize_t 1334 TCPEndpoint::_AvailableData() const 1335 { 1336 // TODO: Refer to the FLAG_NO_RECEIVE comment above regarding 1337 // the application of FLAG_NO_RECEIVE in listen()ing 1338 // sockets. 1339 if (fState == LISTEN) 1340 return gSocketModule->count_connected(socket); 1341 if (fState == SYNCHRONIZE_SENT) 1342 return 0; 1343 1344 ssize_t availableData = fReceiveQueue.Available(); 1345 1346 if (availableData == 0 && !_ShouldReceive()) 1347 return ENOTCONN; 1348 1349 return availableData; 1350 } 1351 1352 1353 void 1354 TCPEndpoint::_NotifyReader() 1355 { 1356 fReceiveCondition.NotifyAll(); 1357 gSocketModule->notify(socket, B_SELECT_READ, _AvailableData()); 1358 } 1359 1360 1361 bool 1362 TCPEndpoint::_AddData(tcp_segment_header& segment, net_buffer* buffer) 1363 { 1364 if ((segment.flags & TCP_FLAG_FINISH) != 0) { 1365 // Remember the position of the finish received flag 1366 fFinishReceived = true; 1367 fFinishReceivedAt = segment.sequence + buffer->size; 1368 } 1369 1370 fReceiveQueue.Add(buffer, segment.sequence); 1371 fReceiveNext = fReceiveQueue.NextSequence(); 1372 1373 if (fFinishReceived) { 1374 // Set or reset the finish flag on the current segment 1375 if (fReceiveNext < fFinishReceivedAt) 1376 segment.flags &= ~TCP_FLAG_FINISH; 1377 else 1378 segment.flags |= TCP_FLAG_FINISH; 1379 } 1380 1381 TRACE(" _AddData(): adding data, receive next = %" B_PRIu32 ". Now have %" 1382 B_PRIuSIZE " bytes.", fReceiveNext.Number(), fReceiveQueue.Available()); 1383 1384 if ((segment.flags & TCP_FLAG_PUSH) != 0) 1385 fReceiveQueue.SetPushPointer(); 1386 1387 return fReceiveQueue.Available() > 0; 1388 } 1389 1390 1391 void 1392 TCPEndpoint::_PrepareReceivePath(tcp_segment_header& segment) 1393 { 1394 fInitialReceiveSequence = segment.sequence; 1395 fFinishReceived = false; 1396 1397 // count the received SYN 1398 segment.sequence++; 1399 1400 fReceiveNext = segment.sequence; 1401 fReceiveQueue.SetInitialSequence(segment.sequence); 1402 1403 if ((fOptions & TCP_NOOPT) == 0) { 1404 if (segment.max_segment_size > 0) 1405 fSendMaxSegmentSize = segment.max_segment_size; 1406 1407 if (segment.options & TCP_HAS_WINDOW_SCALE) { 1408 fFlags |= FLAG_OPTION_WINDOW_SCALE; 1409 fSendWindowShift = segment.window_shift; 1410 } else { 1411 fFlags &= ~FLAG_OPTION_WINDOW_SCALE; 1412 fReceiveWindowShift = 0; 1413 } 1414 1415 if (segment.options & TCP_HAS_TIMESTAMPS) { 1416 fFlags |= FLAG_OPTION_TIMESTAMP; 1417 fReceivedTimestamp = segment.timestamp_value; 1418 } else 1419 fFlags &= ~FLAG_OPTION_TIMESTAMP; 1420 1421 if ((segment.options & TCP_SACK_PERMITTED) == 0) 1422 fFlags &= ~FLAG_OPTION_SACK_PERMITTED; 1423 } 1424 1425 if (fSendMaxSegmentSize > 2190) 1426 fCongestionWindow = 2 * fSendMaxSegmentSize; 1427 else if (fSendMaxSegmentSize > 1095) 1428 fCongestionWindow = 3 * fSendMaxSegmentSize; 1429 else 1430 fCongestionWindow = 4 * fSendMaxSegmentSize; 1431 1432 fSendMaxSegments = fCongestionWindow / fSendMaxSegmentSize; 1433 fSlowStartThreshold = (uint32)segment.advertised_window << fSendWindowShift; 1434 } 1435 1436 1437 bool 1438 TCPEndpoint::_ShouldReceive() const 1439 { 1440 if ((fFlags & FLAG_NO_RECEIVE) != 0) 1441 return false; 1442 1443 return fState == ESTABLISHED || fState == FINISH_SENT 1444 || fState == FINISH_ACKNOWLEDGED || fState == FINISH_RECEIVED; 1445 } 1446 1447 1448 int32 1449 TCPEndpoint::_Spawn(TCPEndpoint* parent, tcp_segment_header& segment, 1450 net_buffer* buffer) 1451 { 1452 MutexLocker _(fLock); 1453 1454 // TODO error checking 1455 ProtocolSocket::Open(); 1456 1457 fState = SYNCHRONIZE_RECEIVED; 1458 T(Spawn(parent, this)); 1459 1460 fManager = parent->fManager; 1461 1462 LocalAddress().SetTo(buffer->destination); 1463 PeerAddress().SetTo(buffer->source); 1464 1465 TRACE("Spawn()"); 1466 1467 // TODO: proper error handling! 1468 if (fManager->BindChild(this) != B_OK) { 1469 T(Error(this, "binding failed", __LINE__)); 1470 return DROP; 1471 } 1472 if (_PrepareSendPath(*PeerAddress()) != B_OK) { 1473 T(Error(this, "prepare send faild", __LINE__)); 1474 return DROP; 1475 } 1476 1477 fOptions = parent->fOptions; 1478 fAcceptSemaphore = parent->fAcceptSemaphore; 1479 1480 _PrepareReceivePath(segment); 1481 1482 // send SYN+ACK 1483 if (_SendQueued() != B_OK) { 1484 T(Error(this, "sending failed", __LINE__)); 1485 return DROP; 1486 } 1487 1488 segment.flags &= ~TCP_FLAG_SYNCHRONIZE; 1489 // we handled this flag now, it must not be set for further processing 1490 1491 return _Receive(segment, buffer); 1492 } 1493 1494 1495 int32 1496 TCPEndpoint::_ListenReceive(tcp_segment_header& segment, net_buffer* buffer) 1497 { 1498 TRACE("ListenReceive()"); 1499 1500 // Essentially, we accept only TCP_FLAG_SYNCHRONIZE in this state, 1501 // but the error behaviour differs 1502 if (segment.flags & TCP_FLAG_RESET) 1503 return DROP; 1504 if (segment.flags & TCP_FLAG_ACKNOWLEDGE) 1505 return DROP | RESET; 1506 if ((segment.flags & TCP_FLAG_SYNCHRONIZE) == 0) 1507 return DROP; 1508 1509 // TODO: drop broadcast/multicast 1510 1511 // spawn new endpoint for accept() 1512 net_socket* newSocket; 1513 if (gSocketModule->spawn_pending_socket(socket, &newSocket) < B_OK) { 1514 T(Error(this, "spawning failed", __LINE__)); 1515 return DROP; 1516 } 1517 1518 return ((TCPEndpoint *)newSocket->first_protocol)->_Spawn(this, 1519 segment, buffer); 1520 } 1521 1522 1523 int32 1524 TCPEndpoint::_SynchronizeSentReceive(tcp_segment_header &segment, 1525 net_buffer *buffer) 1526 { 1527 TRACE("_SynchronizeSentReceive()"); 1528 1529 if ((segment.flags & TCP_FLAG_ACKNOWLEDGE) != 0 1530 && (fInitialSendSequence >= segment.acknowledge 1531 || fSendMax < segment.acknowledge)) 1532 return DROP | RESET; 1533 1534 if (segment.flags & TCP_FLAG_RESET) { 1535 _HandleReset(ECONNREFUSED); 1536 return DROP; 1537 } 1538 1539 if ((segment.flags & TCP_FLAG_SYNCHRONIZE) == 0) 1540 return DROP; 1541 1542 fSendUnacknowledged = segment.acknowledge; 1543 _PrepareReceivePath(segment); 1544 1545 if (segment.flags & TCP_FLAG_ACKNOWLEDGE) { 1546 _MarkEstablished(); 1547 } else { 1548 // simultaneous open 1549 fState = SYNCHRONIZE_RECEIVED; 1550 T(State(this)); 1551 } 1552 1553 segment.flags &= ~TCP_FLAG_SYNCHRONIZE; 1554 // we handled this flag now, it must not be set for further processing 1555 1556 return _Receive(segment, buffer) | IMMEDIATE_ACKNOWLEDGE; 1557 } 1558 1559 1560 int32 1561 TCPEndpoint::_Receive(tcp_segment_header& segment, net_buffer* buffer) 1562 { 1563 // PAWS processing takes precedence over regular TCP acceptability check 1564 if ((fFlags & FLAG_OPTION_TIMESTAMP) != 0 && (segment.flags & TCP_FLAG_RESET) == 0) { 1565 if ((segment.options & TCP_HAS_TIMESTAMPS) == 0) 1566 return DROP; 1567 if ((int32)(fReceivedTimestamp - segment.timestamp_value) > 0 1568 && (fReceivedTimestamp - segment.timestamp_value) <= INT32_MAX) 1569 return DROP | IMMEDIATE_ACKNOWLEDGE; 1570 } 1571 1572 uint32 advertisedWindow = (uint32)segment.advertised_window 1573 << fSendWindowShift; 1574 size_t segmentLength = buffer->size; 1575 1576 // First, handle the most common case for uni-directional data transfer 1577 // (known as header prediction - the segment must not change the window, 1578 // and must be the expected sequence, and contain no control flags) 1579 1580 if (fState == ESTABLISHED 1581 && segment.AcknowledgeOnly() 1582 && fReceiveNext == segment.sequence 1583 && advertisedWindow > 0 && advertisedWindow == fSendWindow 1584 && fSendNext == fSendMax) { 1585 _UpdateTimestamps(segment, segmentLength); 1586 1587 if (segmentLength == 0) { 1588 // this is a pure acknowledge segment - we're on the sending end 1589 if (fSendUnacknowledged < segment.acknowledge 1590 && fSendMax >= segment.acknowledge) { 1591 _Acknowledged(segment); 1592 return DROP; 1593 } 1594 } else if (segment.acknowledge == fSendUnacknowledged 1595 && fReceiveQueue.IsContiguous() 1596 && fReceiveQueue.Free() >= segmentLength 1597 && (fFlags & FLAG_NO_RECEIVE) == 0) { 1598 if (_AddData(segment, buffer)) 1599 _NotifyReader(); 1600 1601 return KEEP | ((segment.flags & TCP_FLAG_PUSH) != 0 1602 ? IMMEDIATE_ACKNOWLEDGE : ACKNOWLEDGE); 1603 } 1604 } 1605 1606 // The fast path was not applicable, so we continue with the standard 1607 // processing of the incoming segment 1608 1609 ASSERT(fState != SYNCHRONIZE_SENT && fState != LISTEN); 1610 1611 if (fState != CLOSED && fState != TIME_WAIT) { 1612 // Check sequence number 1613 if (!segment_in_sequence(segment, segmentLength, fReceiveNext, 1614 fReceiveWindow)) { 1615 TRACE(" Receive(): segment out of window, next: %" B_PRIu32 1616 " wnd: %" B_PRIu32, fReceiveNext.Number(), fReceiveWindow); 1617 if ((segment.flags & TCP_FLAG_RESET) != 0) { 1618 // TODO: this doesn't look right - review! 1619 return DROP; 1620 } 1621 return DROP | IMMEDIATE_ACKNOWLEDGE; 1622 } 1623 } 1624 1625 if ((segment.flags & TCP_FLAG_RESET) != 0) { 1626 // Is this a valid reset? 1627 // We generally ignore resets in time wait state (see RFC 1337) 1628 if (fLastAcknowledgeSent <= segment.sequence 1629 && tcp_sequence(segment.sequence) < (fLastAcknowledgeSent 1630 + fReceiveWindow) 1631 && fState != TIME_WAIT) { 1632 status_t error; 1633 if (fState == SYNCHRONIZE_RECEIVED) 1634 error = ECONNREFUSED; 1635 else if (fState == CLOSING || fState == WAIT_FOR_FINISH_ACKNOWLEDGE) 1636 error = ENOTCONN; 1637 else 1638 error = ECONNRESET; 1639 1640 _HandleReset(error); 1641 } 1642 1643 return DROP; 1644 } 1645 1646 if ((segment.flags & TCP_FLAG_SYNCHRONIZE) != 0 1647 || (fState == SYNCHRONIZE_RECEIVED 1648 && (fInitialReceiveSequence > segment.sequence 1649 || ((segment.flags & TCP_FLAG_ACKNOWLEDGE) != 0 1650 && (fSendUnacknowledged > segment.acknowledge 1651 || fSendMax < segment.acknowledge))))) { 1652 // reset the connection - either the initial SYN was faulty, or we 1653 // received a SYN within the data stream 1654 return DROP | RESET; 1655 } 1656 1657 // TODO: Check this! Why do we advertize a window outside of what we should 1658 // buffer? 1659 fReceiveWindow = max_c(fReceiveQueue.Free(), fReceiveWindow); 1660 // the window must not shrink 1661 1662 // trim buffer to be within the receive window 1663 int32 drop = (int32)(fReceiveNext - segment.sequence).Number(); 1664 if (drop > 0) { 1665 if ((uint32)drop > buffer->size 1666 || ((uint32)drop == buffer->size 1667 && (segment.flags & TCP_FLAG_FINISH) == 0)) { 1668 // don't accidently remove a FIN we shouldn't remove 1669 segment.flags &= ~TCP_FLAG_FINISH; 1670 drop = buffer->size; 1671 } 1672 1673 // remove duplicate data at the start 1674 TRACE("* remove %" B_PRId32 " bytes from the start", drop); 1675 gBufferModule->remove_header(buffer, drop); 1676 segment.sequence += drop; 1677 } 1678 1679 int32 action = KEEP; 1680 1681 // immediately acknowledge out-of-order segment to trigger fast-retransmit at the sender 1682 if (drop != 0) 1683 action |= IMMEDIATE_ACKNOWLEDGE; 1684 1685 drop = (int32)(segment.sequence + buffer->size 1686 - (fReceiveNext + fReceiveWindow)).Number(); 1687 if (drop > 0) { 1688 // remove data exceeding our window 1689 if ((uint32)drop >= buffer->size) { 1690 // if we can accept data, or the segment is not what we'd expect, 1691 // drop the segment (an immediate acknowledge is always triggered) 1692 if (fReceiveWindow != 0 || segment.sequence != fReceiveNext) 1693 return DROP | IMMEDIATE_ACKNOWLEDGE; 1694 1695 action |= IMMEDIATE_ACKNOWLEDGE; 1696 } 1697 1698 if ((segment.flags & TCP_FLAG_FINISH) != 0) { 1699 // we need to remove the finish, too, as part of the data 1700 drop--; 1701 } 1702 1703 segment.flags &= ~(TCP_FLAG_FINISH | TCP_FLAG_PUSH); 1704 TRACE("* remove %" B_PRId32 " bytes from the end", drop); 1705 gBufferModule->remove_trailer(buffer, drop); 1706 } 1707 1708 #ifdef TRACE_TCP 1709 if (advertisedWindow > fSendWindow) { 1710 TRACE(" Receive(): Window update %" B_PRIu32 " -> %" B_PRIu32, 1711 fSendWindow, advertisedWindow); 1712 } 1713 #endif 1714 1715 if (advertisedWindow > fSendWindow) 1716 action |= IMMEDIATE_ACKNOWLEDGE; 1717 1718 fSendWindow = advertisedWindow; 1719 if (advertisedWindow > fSendMaxWindow) 1720 fSendMaxWindow = advertisedWindow; 1721 1722 // Then look at the acknowledgement for any updates 1723 1724 if ((segment.flags & TCP_FLAG_ACKNOWLEDGE) != 0) { 1725 // process acknowledged data 1726 if (fState == SYNCHRONIZE_RECEIVED) 1727 _MarkEstablished(); 1728 1729 if (fSendMax < segment.acknowledge) 1730 return DROP | IMMEDIATE_ACKNOWLEDGE; 1731 1732 if (segment.acknowledge == fSendUnacknowledged) { 1733 if (buffer->size == 0 && advertisedWindow == fSendWindow 1734 && (segment.flags & TCP_FLAG_FINISH) == 0 && fSendUnacknowledged != fSendMax) { 1735 TRACE("Receive(): duplicate ack!"); 1736 _DuplicateAcknowledge(segment); 1737 } 1738 } else if (segment.acknowledge < fSendUnacknowledged) { 1739 return DROP; 1740 } else { 1741 // this segment acknowledges in flight data 1742 1743 if (fDuplicateAcknowledgeCount >= 3) { 1744 // deflate the window. 1745 if (segment.acknowledge > fRecover) { 1746 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 1747 fCongestionWindow = min_c(fSlowStartThreshold, 1748 max_c(flightSize, fSendMaxSegmentSize) + fSendMaxSegmentSize); 1749 fFlags &= ~FLAG_RECOVERY; 1750 } 1751 } 1752 1753 if (fSendMax == segment.acknowledge) 1754 TRACE("Receive(): all inflight data ack'd!"); 1755 1756 if (segment.acknowledge > fSendQueue.LastSequence() 1757 && fState > ESTABLISHED) { 1758 TRACE("Receive(): FIN has been acknowledged!"); 1759 1760 switch (fState) { 1761 case FINISH_SENT: 1762 fState = FINISH_ACKNOWLEDGED; 1763 T(State(this)); 1764 break; 1765 case CLOSING: 1766 fState = TIME_WAIT; 1767 T(State(this)); 1768 _EnterTimeWait(); 1769 return DROP; 1770 case WAIT_FOR_FINISH_ACKNOWLEDGE: 1771 _Close(); 1772 break; 1773 1774 default: 1775 break; 1776 } 1777 } 1778 1779 if (fState != CLOSED) { 1780 tcp_sequence last = fLastAcknowledgeSent; 1781 _Acknowledged(segment); 1782 // we just sent an acknowledge, remove from action 1783 if (last < fLastAcknowledgeSent) 1784 action &= ~IMMEDIATE_ACKNOWLEDGE; 1785 } 1786 } 1787 } 1788 1789 if (segment.flags & TCP_FLAG_URGENT) { 1790 if (fState == ESTABLISHED || fState == FINISH_SENT 1791 || fState == FINISH_ACKNOWLEDGED) { 1792 // TODO: Handle urgent data: 1793 // - RCV.UP <- max(RCV.UP, SEG.UP) 1794 // - signal the user that urgent data is available (SIGURG) 1795 } 1796 } 1797 1798 bool notify = false; 1799 1800 // The buffer may be freed if its data is added to the queue, so cache 1801 // the size as we still need it later. 1802 uint32 bufferSize = buffer->size; 1803 1804 if ((bufferSize > 0 || (segment.flags & TCP_FLAG_FINISH) != 0) 1805 && _ShouldReceive()) 1806 notify = _AddData(segment, buffer); 1807 else { 1808 if ((fFlags & FLAG_NO_RECEIVE) != 0) 1809 fReceiveNext += buffer->size; 1810 1811 action = (action & ~KEEP) | DROP; 1812 } 1813 1814 if ((segment.flags & TCP_FLAG_FINISH) != 0) { 1815 segmentLength++; 1816 if (fState != CLOSED && fState != LISTEN && fState != SYNCHRONIZE_SENT) { 1817 TRACE("Receive(): peer is finishing connection!"); 1818 fReceiveNext++; 1819 notify = true; 1820 1821 // FIN implies PUSH 1822 fReceiveQueue.SetPushPointer(); 1823 1824 // we'll reply immediately to the FIN if we are not 1825 // transitioning to TIME WAIT so we immediatly ACK it. 1826 action |= IMMEDIATE_ACKNOWLEDGE; 1827 1828 // other side is closing connection; change states 1829 switch (fState) { 1830 case ESTABLISHED: 1831 case SYNCHRONIZE_RECEIVED: 1832 fState = FINISH_RECEIVED; 1833 T(State(this)); 1834 break; 1835 case FINISH_SENT: 1836 // simultaneous close 1837 fState = CLOSING; 1838 T(State(this)); 1839 break; 1840 case FINISH_ACKNOWLEDGED: 1841 fState = TIME_WAIT; 1842 T(State(this)); 1843 _EnterTimeWait(); 1844 break; 1845 case TIME_WAIT: 1846 _UpdateTimeWait(); 1847 break; 1848 1849 default: 1850 break; 1851 } 1852 } 1853 } 1854 1855 if (notify) 1856 _NotifyReader(); 1857 1858 if (bufferSize > 0 || (segment.flags & TCP_FLAG_SYNCHRONIZE) != 0) 1859 action |= ACKNOWLEDGE; 1860 1861 _UpdateTimestamps(segment, segmentLength); 1862 1863 TRACE("Receive() Action %" B_PRId32, action); 1864 1865 return action; 1866 } 1867 1868 1869 int32 1870 TCPEndpoint::SegmentReceived(tcp_segment_header& segment, net_buffer* buffer) 1871 { 1872 MutexLocker locker(fLock); 1873 1874 TRACE("SegmentReceived(): buffer %p (%" B_PRIu32 " bytes) address %s " 1875 "to %s flags %#" B_PRIx8 ", seq %" B_PRIu32 ", ack %" B_PRIu32 1876 ", wnd %" B_PRIu32, buffer, buffer->size, PrintAddress(buffer->source), 1877 PrintAddress(buffer->destination), segment.flags, segment.sequence, 1878 segment.acknowledge, 1879 (uint32)segment.advertised_window << fSendWindowShift); 1880 T(Receive(this, segment, 1881 (uint32)segment.advertised_window << fSendWindowShift, buffer)); 1882 int32 segmentAction = DROP; 1883 1884 switch (fState) { 1885 case LISTEN: 1886 segmentAction = _ListenReceive(segment, buffer); 1887 break; 1888 1889 case SYNCHRONIZE_SENT: 1890 segmentAction = _SynchronizeSentReceive(segment, buffer); 1891 break; 1892 1893 case SYNCHRONIZE_RECEIVED: 1894 case ESTABLISHED: 1895 case FINISH_RECEIVED: 1896 case WAIT_FOR_FINISH_ACKNOWLEDGE: 1897 case FINISH_SENT: 1898 case FINISH_ACKNOWLEDGED: 1899 case CLOSING: 1900 case TIME_WAIT: 1901 case CLOSED: 1902 segmentAction = _Receive(segment, buffer); 1903 break; 1904 } 1905 1906 // process acknowledge action as asked for by the *Receive() method 1907 if (segmentAction & IMMEDIATE_ACKNOWLEDGE) 1908 SendAcknowledge(true); 1909 else if (segmentAction & ACKNOWLEDGE) 1910 DelayedAcknowledge(); 1911 1912 if ((fFlags & (FLAG_CLOSED | FLAG_DELETE_ON_CLOSE)) 1913 == (FLAG_CLOSED | FLAG_DELETE_ON_CLOSE)) { 1914 1915 locker.Unlock(); 1916 if (gSocketModule->release_socket(socket)) 1917 segmentAction |= DELETED_ENDPOINT; 1918 } 1919 1920 return segmentAction; 1921 } 1922 1923 1924 // #pragma mark - send 1925 1926 1927 inline uint8 1928 TCPEndpoint::_CurrentFlags() 1929 { 1930 // we don't set FLAG_FINISH here, instead we do it 1931 // conditionally below depending if we are sending 1932 // the last bytes of the send queue. 1933 1934 switch (fState) { 1935 case CLOSED: 1936 return TCP_FLAG_RESET | TCP_FLAG_ACKNOWLEDGE; 1937 1938 case SYNCHRONIZE_SENT: 1939 return TCP_FLAG_SYNCHRONIZE; 1940 case SYNCHRONIZE_RECEIVED: 1941 return TCP_FLAG_SYNCHRONIZE | TCP_FLAG_ACKNOWLEDGE; 1942 1943 case ESTABLISHED: 1944 case FINISH_RECEIVED: 1945 case FINISH_ACKNOWLEDGED: 1946 case TIME_WAIT: 1947 case WAIT_FOR_FINISH_ACKNOWLEDGE: 1948 case FINISH_SENT: 1949 case CLOSING: 1950 return TCP_FLAG_ACKNOWLEDGE; 1951 1952 default: 1953 return 0; 1954 } 1955 } 1956 1957 1958 inline bool 1959 TCPEndpoint::_ShouldSendSegment(tcp_segment_header& segment, uint32 length, 1960 uint32 segmentMaxSize, uint32 flightSize) 1961 { 1962 if (fState == ESTABLISHED && fSendMaxSegments == 0) 1963 return false; 1964 1965 if (length > 0) { 1966 // Avoid the silly window syndrome - we only send a segment in case: 1967 // - we have a full segment to send, or 1968 // - we're at the end of our buffer queue, or 1969 // - the buffer is at least larger than half of the maximum send window, 1970 // or 1971 // - we're retransmitting data 1972 if (length == segmentMaxSize 1973 || (fOptions & TCP_NODELAY) != 0 1974 || tcp_sequence(fSendNext + length) == fSendQueue.LastSequence() 1975 || (fSendMaxWindow > 0 && length >= fSendMaxWindow / 2)) 1976 return true; 1977 } 1978 1979 // check if we need to send a window update to the peer 1980 if (segment.advertised_window > 0) { 1981 // correct the window to take into account what already has been advertised 1982 uint32 window = (segment.advertised_window << fReceiveWindowShift) 1983 - (fReceiveMaxAdvertised - fReceiveNext).Number(); 1984 1985 // if we can advertise a window larger than twice the maximum segment 1986 // size, or half the maximum buffer size we send a window update 1987 if (window >= (fReceiveMaxSegmentSize << 1) 1988 || window >= (socket->receive.buffer_size >> 1)) 1989 return true; 1990 } 1991 1992 if ((segment.flags & (TCP_FLAG_SYNCHRONIZE | TCP_FLAG_FINISH 1993 | TCP_FLAG_RESET)) != 0) 1994 return true; 1995 1996 // We do have urgent data pending 1997 if (fSendUrgentOffset > fSendNext) 1998 return true; 1999 2000 // there is no reason to send a segment just now 2001 return false; 2002 } 2003 2004 2005 status_t 2006 TCPEndpoint::_SendQueued(bool force) 2007 { 2008 return _SendQueued(force, fSendWindow); 2009 } 2010 2011 2012 /*! Sends one or more TCP segments with the data waiting in the queue, or some 2013 specific flags that need to be sent. 2014 */ 2015 status_t 2016 TCPEndpoint::_SendQueued(bool force, uint32 sendWindow) 2017 { 2018 if (fRoute == NULL) 2019 return B_ERROR; 2020 2021 // in passive state? 2022 if (fState == LISTEN) 2023 return B_ERROR; 2024 2025 tcp_segment_header segment(_CurrentFlags()); 2026 2027 if ((fOptions & TCP_NOOPT) == 0) { 2028 if ((fFlags & FLAG_OPTION_TIMESTAMP) != 0) { 2029 segment.options |= TCP_HAS_TIMESTAMPS; 2030 segment.timestamp_reply = fReceivedTimestamp; 2031 segment.timestamp_value = tcp_now(); 2032 } 2033 2034 // SACK information is embedded with duplicate acknowledgements 2035 if (!fReceiveQueue.IsContiguous() 2036 && fLastAcknowledgeSent <= fReceiveNext 2037 && (fFlags & FLAG_OPTION_SACK_PERMITTED) != 0) { 2038 segment.options |= TCP_HAS_SACK; 2039 int maxSackCount = MAX_SACK_BLKS 2040 - ((fFlags & FLAG_OPTION_TIMESTAMP) != 0); 2041 memset(segment.sacks, 0, sizeof(segment.sacks)); 2042 segment.sackCount = fReceiveQueue.PopulateSackInfo(fReceiveNext, 2043 maxSackCount, segment.sacks); 2044 } 2045 2046 if ((segment.flags & TCP_FLAG_SYNCHRONIZE) != 0 2047 && fSendNext == fInitialSendSequence) { 2048 // add connection establishment options 2049 segment.max_segment_size = fReceiveMaxSegmentSize; 2050 if (fFlags & FLAG_OPTION_WINDOW_SCALE) { 2051 segment.options |= TCP_HAS_WINDOW_SCALE; 2052 segment.window_shift = fReceiveWindowShift; 2053 } 2054 if ((fFlags & FLAG_OPTION_SACK_PERMITTED) != 0) 2055 segment.options |= TCP_SACK_PERMITTED; 2056 } 2057 } 2058 2059 size_t availableBytes = fReceiveQueue.Free(); 2060 // window size must remain same for duplicate acknowledgements 2061 if (!fReceiveQueue.IsContiguous()) 2062 availableBytes = (fReceiveMaxAdvertised - fReceiveNext).Number(); 2063 2064 if (fFlags & FLAG_OPTION_WINDOW_SCALE) 2065 segment.advertised_window = availableBytes >> fReceiveWindowShift; 2066 else 2067 segment.advertised_window = min_c(TCP_MAX_WINDOW, availableBytes); 2068 2069 segment.acknowledge = fReceiveNext.Number(); 2070 2071 // Process urgent data 2072 if (fSendUrgentOffset > fSendNext) { 2073 segment.flags |= TCP_FLAG_URGENT; 2074 segment.urgent_offset = (fSendUrgentOffset - fSendNext).Number(); 2075 } else { 2076 fSendUrgentOffset = fSendUnacknowledged.Number(); 2077 // Keep urgent offset updated, so that it doesn't reach into our 2078 // send window on overlap 2079 segment.urgent_offset = 0; 2080 } 2081 2082 if (fCongestionWindow > 0 && fCongestionWindow < sendWindow) 2083 sendWindow = fCongestionWindow; 2084 2085 // fSendUnacknowledged 2086 // | fSendNext fSendMax 2087 // | | | 2088 // v v v 2089 // ----------------------------------- 2090 // | effective window | 2091 // ----------------------------------- 2092 2093 // Flight size represents the window of data which is currently in the 2094 // ether. We should never send data such as the flight size becomes larger 2095 // than the effective window. Note however that the effective window may be 2096 // reduced (by congestion for instance), so at some point in time flight 2097 // size may be larger than the currently calculated window. 2098 2099 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 2100 uint32 consumedWindow = (fSendNext - fSendUnacknowledged).Number(); 2101 2102 if (consumedWindow > sendWindow) { 2103 sendWindow = 0; 2104 // TODO: enter persist state? try to get a window update. 2105 } else 2106 sendWindow -= consumedWindow; 2107 2108 uint32 length = min_c(fSendQueue.Available(fSendNext), sendWindow); 2109 bool shouldStartRetransmitTimer = fSendNext == fSendUnacknowledged; 2110 bool retransmit = fSendNext < fSendMax; 2111 2112 if (fDuplicateAcknowledgeCount != 0) { 2113 // send at most 1 SMSS of data when under limited transmit, fast transmit/recovery 2114 length = min_c(length, fSendMaxSegmentSize); 2115 } 2116 2117 do { 2118 uint32 segmentMaxSize = fSendMaxSegmentSize 2119 - tcp_options_length(segment); 2120 uint32 segmentLength = min_c(length, segmentMaxSize); 2121 2122 if (fSendNext + segmentLength == fSendQueue.LastSequence() && !force) { 2123 if (state_needs_finish(fState)) 2124 segment.flags |= TCP_FLAG_FINISH; 2125 if (length > 0) 2126 segment.flags |= TCP_FLAG_PUSH; 2127 } 2128 2129 // Determine if we should really send this segment 2130 if (!force && !retransmit && !_ShouldSendSegment(segment, segmentLength, 2131 segmentMaxSize, flightSize)) { 2132 if (fSendQueue.Available() 2133 && !gStackModule->is_timer_active(&fPersistTimer) 2134 && !gStackModule->is_timer_active(&fRetransmitTimer)) 2135 _StartPersistTimer(); 2136 break; 2137 } 2138 2139 net_buffer *buffer = gBufferModule->create(256); 2140 if (buffer == NULL) 2141 return B_NO_MEMORY; 2142 2143 status_t status = B_OK; 2144 if (segmentLength > 0) 2145 status = fSendQueue.Get(buffer, fSendNext, segmentLength); 2146 if (status < B_OK) { 2147 gBufferModule->free(buffer); 2148 return status; 2149 } 2150 2151 LocalAddress().CopyTo(buffer->source); 2152 PeerAddress().CopyTo(buffer->destination); 2153 2154 uint32 size = buffer->size; 2155 segment.sequence = fSendNext.Number(); 2156 2157 TRACE("SendQueued(): buffer %p (%" B_PRIu32 " bytes) address %s to " 2158 "%s flags %#" B_PRIx8 ", seq %" B_PRIu32 ", ack %" B_PRIu32 2159 ", rwnd %" B_PRIu16 ", cwnd %" B_PRIu32 ", ssthresh %" B_PRIu32 2160 ", len %" B_PRIu32 ", first %" B_PRIu32 ", last %" B_PRIu32, 2161 buffer, buffer->size, PrintAddress(buffer->source), 2162 PrintAddress(buffer->destination), segment.flags, segment.sequence, 2163 segment.acknowledge, segment.advertised_window, 2164 fCongestionWindow, fSlowStartThreshold, segmentLength, 2165 fSendQueue.FirstSequence().Number(), 2166 fSendQueue.LastSequence().Number()); 2167 T(Send(this, segment, buffer, fSendQueue.FirstSequence(), 2168 fSendQueue.LastSequence())); 2169 2170 PROBE(buffer, sendWindow); 2171 sendWindow -= buffer->size; 2172 2173 status = add_tcp_header(AddressModule(), segment, buffer); 2174 if (status != B_OK) { 2175 gBufferModule->free(buffer); 2176 return status; 2177 } 2178 2179 // Update send status - we need to do this before we send the data 2180 // for local connections as the answer is directly handled 2181 2182 if (segment.flags & TCP_FLAG_SYNCHRONIZE) { 2183 segment.options &= ~TCP_HAS_WINDOW_SCALE; 2184 segment.max_segment_size = 0; 2185 size++; 2186 } 2187 2188 if (segment.flags & TCP_FLAG_FINISH) 2189 size++; 2190 2191 uint32 sendMax = fSendMax.Number(); 2192 fSendNext += size; 2193 if (fSendMax < fSendNext) 2194 fSendMax = fSendNext; 2195 2196 fReceiveMaxAdvertised = fReceiveNext 2197 + ((uint32)segment.advertised_window << fReceiveWindowShift); 2198 2199 if (segmentLength != 0 && fState == ESTABLISHED) 2200 --fSendMaxSegments; 2201 2202 status = next->module->send_routed_data(next, fRoute, buffer); 2203 if (status < B_OK) { 2204 gBufferModule->free(buffer); 2205 2206 fSendNext = segment.sequence; 2207 fSendMax = sendMax; 2208 // restore send status 2209 return status; 2210 } 2211 2212 if (fSendTime == 0 && !retransmit 2213 && (segmentLength != 0 || (segment.flags & TCP_FLAG_SYNCHRONIZE) !=0)) { 2214 fSendTime = tcp_now(); 2215 fRoundTripStartSequence = segment.sequence; 2216 } 2217 2218 if (shouldStartRetransmitTimer && size > 0) { 2219 TRACE("starting initial retransmit timer of: %" B_PRIdBIGTIME, 2220 fRetransmitTimeout); 2221 gStackModule->set_timer(&fRetransmitTimer, fRetransmitTimeout); 2222 T(TimerSet(this, "retransmit", fRetransmitTimeout)); 2223 shouldStartRetransmitTimer = false; 2224 } 2225 2226 if (segment.flags & TCP_FLAG_ACKNOWLEDGE) { 2227 fLastAcknowledgeSent = segment.acknowledge; 2228 gStackModule->cancel_timer(&fDelayedAcknowledgeTimer); 2229 } 2230 2231 length -= segmentLength; 2232 segment.flags &= ~(TCP_FLAG_SYNCHRONIZE | TCP_FLAG_RESET 2233 | TCP_FLAG_FINISH); 2234 2235 if (retransmit) 2236 break; 2237 2238 } while (length > 0); 2239 2240 return B_OK; 2241 } 2242 2243 2244 int 2245 TCPEndpoint::_MaxSegmentSize(const sockaddr* address) const 2246 { 2247 return next->module->get_mtu(next, address) - sizeof(tcp_header); 2248 } 2249 2250 2251 status_t 2252 TCPEndpoint::_PrepareSendPath(const sockaddr* peer) 2253 { 2254 if (fRoute == NULL) { 2255 fRoute = gDatalinkModule->get_route(Domain(), peer); 2256 if (fRoute == NULL) 2257 return ENETUNREACH; 2258 2259 if ((fRoute->flags & RTF_LOCAL) != 0) 2260 fFlags |= FLAG_LOCAL; 2261 } 2262 2263 // make sure connection does not already exist 2264 status_t status = fManager->SetConnection(this, *LocalAddress(), peer, 2265 fRoute->interface_address->local); 2266 if (status < B_OK) 2267 return status; 2268 2269 fInitialSendSequence = system_time() >> 4; 2270 fSendNext = fInitialSendSequence; 2271 fSendUnacknowledged = fInitialSendSequence; 2272 fSendMax = fInitialSendSequence; 2273 fSendUrgentOffset = fInitialSendSequence; 2274 fRecover = fInitialSendSequence.Number(); 2275 2276 // we are counting the SYN here 2277 fSendQueue.SetInitialSequence(fSendNext + 1); 2278 2279 fReceiveMaxSegmentSize = _MaxSegmentSize(peer); 2280 2281 // Compute the window shift we advertise to our peer - if it doesn't support 2282 // this option, this will be reset to 0 (when its SYN is received) 2283 fReceiveWindowShift = 0; 2284 while (fReceiveWindowShift < TCP_MAX_WINDOW_SHIFT 2285 && (0xffffUL << fReceiveWindowShift) < socket->receive.buffer_size) { 2286 fReceiveWindowShift++; 2287 } 2288 2289 return B_OK; 2290 } 2291 2292 2293 void 2294 TCPEndpoint::_Acknowledged(tcp_segment_header& segment) 2295 { 2296 TRACE("_Acknowledged(): ack %" B_PRIu32 "; uack %" B_PRIu32 "; next %" 2297 B_PRIu32 "; max %" B_PRIu32, segment.acknowledge, 2298 fSendUnacknowledged.Number(), fSendNext.Number(), fSendMax.Number()); 2299 2300 ASSERT(fSendUnacknowledged <= segment.acknowledge); 2301 2302 if (fSendUnacknowledged < segment.acknowledge) { 2303 fSendQueue.RemoveUntil(segment.acknowledge); 2304 2305 uint32 bytesAcknowledged = segment.acknowledge - fSendUnacknowledged.Number(); 2306 fPreviousHighestAcknowledge = fSendUnacknowledged; 2307 fSendUnacknowledged = segment.acknowledge; 2308 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 2309 int32 expectedSamples = flightSize / (fSendMaxSegmentSize << 1); 2310 2311 if (fPreviousHighestAcknowledge > fSendUnacknowledged) { 2312 // need to update the recover variable upon a sequence wraparound 2313 fRecover = segment.acknowledge - 1; 2314 } 2315 2316 // the acknowledgment of the SYN/ACK MUST NOT increase the size of the congestion window 2317 if (fSendUnacknowledged != fInitialSendSequence) { 2318 if (fCongestionWindow < fSlowStartThreshold) 2319 fCongestionWindow += min_c(bytesAcknowledged, fSendMaxSegmentSize); 2320 else { 2321 uint32 increment = fSendMaxSegmentSize * fSendMaxSegmentSize; 2322 2323 if (increment < fCongestionWindow) 2324 increment = 1; 2325 else 2326 increment /= fCongestionWindow; 2327 2328 fCongestionWindow += increment; 2329 } 2330 2331 fSendMaxSegments = UINT32_MAX; 2332 } 2333 2334 if ((fFlags & FLAG_RECOVERY) != 0) { 2335 fSendNext = fSendUnacknowledged; 2336 _SendQueued(); 2337 fCongestionWindow -= bytesAcknowledged; 2338 2339 if (bytesAcknowledged > fSendMaxSegmentSize) 2340 fCongestionWindow += fSendMaxSegmentSize; 2341 2342 fSendNext = fSendMax; 2343 } else 2344 fDuplicateAcknowledgeCount = 0; 2345 2346 if (fSendNext < fSendUnacknowledged) 2347 fSendNext = fSendUnacknowledged; 2348 2349 if (fFlags & FLAG_OPTION_TIMESTAMP) { 2350 _UpdateRoundTripTime(tcp_diff_timestamp(segment.timestamp_reply), 2351 expectedSamples > 0 ? expectedSamples : 1); 2352 } else if (fSendTime != 0 && fRoundTripStartSequence < segment.acknowledge) { 2353 _UpdateRoundTripTime(tcp_diff_timestamp(fSendTime), 1); 2354 fSendTime = 0; 2355 } 2356 2357 if (fSendUnacknowledged == fSendMax) { 2358 TRACE("all acknowledged, cancelling retransmission timer."); 2359 gStackModule->cancel_timer(&fRetransmitTimer); 2360 T(TimerSet(this, "retransmit", -1)); 2361 } else { 2362 TRACE("data acknowledged, resetting retransmission timer to: %" 2363 B_PRIdBIGTIME, fRetransmitTimeout); 2364 gStackModule->set_timer(&fRetransmitTimer, fRetransmitTimeout); 2365 T(TimerSet(this, "retransmit", fRetransmitTimeout)); 2366 } 2367 2368 if (is_writable(fState)) { 2369 // notify threads waiting on the socket to become writable again 2370 fSendCondition.NotifyAll(); 2371 gSocketModule->notify(socket, B_SELECT_WRITE, fSendQueue.Free()); 2372 } 2373 } 2374 2375 // if there is data left to be sent, send it now 2376 if (fSendQueue.Used() > 0) 2377 _SendQueued(); 2378 } 2379 2380 2381 void 2382 TCPEndpoint::_Retransmit() 2383 { 2384 TRACE("Retransmit()"); 2385 2386 if (fState < ESTABLISHED) { 2387 fRetransmitTimeout = TCP_SYN_RETRANSMIT_TIMEOUT; 2388 fCongestionWindow = fSendMaxSegmentSize; 2389 } else { 2390 _ResetSlowStart(); 2391 fDuplicateAcknowledgeCount = 0; 2392 // Do exponential back off of the retransmit timeout 2393 fRetransmitTimeout *= 2; 2394 if (fRetransmitTimeout > TCP_MAX_RETRANSMIT_TIMEOUT) 2395 fRetransmitTimeout = TCP_MAX_RETRANSMIT_TIMEOUT; 2396 } 2397 2398 fSendNext = fSendUnacknowledged; 2399 _SendQueued(); 2400 2401 fRecover = fSendNext.Number() - 1; 2402 if ((fFlags & FLAG_RECOVERY) != 0) 2403 fFlags &= ~FLAG_RECOVERY; 2404 } 2405 2406 2407 void 2408 TCPEndpoint::_UpdateRoundTripTime(int32 roundTripTime, int32 expectedSamples) 2409 { 2410 if (fSmoothedRoundTripTime == 0) { 2411 fSmoothedRoundTripTime = roundTripTime; 2412 fRoundTripVariation = roundTripTime / 2; 2413 fRetransmitTimeout = (fSmoothedRoundTripTime + max_c(100, fRoundTripVariation * 4)) 2414 * kTimestampFactor; 2415 } else { 2416 int32 delta = fSmoothedRoundTripTime - roundTripTime; 2417 if (delta < 0) 2418 delta = -delta; 2419 fRoundTripVariation += (delta - fRoundTripVariation) / (expectedSamples * 4); 2420 fSmoothedRoundTripTime += (roundTripTime - fSmoothedRoundTripTime) / (expectedSamples * 8); 2421 fRetransmitTimeout = (fSmoothedRoundTripTime + max_c(100, fRoundTripVariation * 4)) 2422 * kTimestampFactor; 2423 } 2424 2425 if (fRetransmitTimeout > TCP_MAX_RETRANSMIT_TIMEOUT) 2426 fRetransmitTimeout = TCP_MAX_RETRANSMIT_TIMEOUT; 2427 2428 if (fRetransmitTimeout < TCP_MIN_RETRANSMIT_TIMEOUT) 2429 fRetransmitTimeout = TCP_MIN_RETRANSMIT_TIMEOUT; 2430 2431 TRACE(" RTO is now %" B_PRIdBIGTIME " (after rtt %" B_PRId32 "ms)", 2432 fRetransmitTimeout, roundTripTime); 2433 } 2434 2435 2436 void 2437 TCPEndpoint::_ResetSlowStart() 2438 { 2439 fSlowStartThreshold = max_c((fSendMax - fSendUnacknowledged).Number() / 2, 2440 2 * fSendMaxSegmentSize); 2441 fCongestionWindow = fSendMaxSegmentSize; 2442 } 2443 2444 2445 // #pragma mark - timer 2446 2447 2448 /*static*/ void 2449 TCPEndpoint::_RetransmitTimer(net_timer* timer, void* _endpoint) 2450 { 2451 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2452 T(TimerTriggered(endpoint, "retransmit")); 2453 2454 MutexLocker locker(endpoint->fLock); 2455 if (!locker.IsLocked() || gStackModule->is_timer_active(timer)) 2456 return; 2457 2458 endpoint->_Retransmit(); 2459 } 2460 2461 2462 /*static*/ void 2463 TCPEndpoint::_PersistTimer(net_timer* timer, void* _endpoint) 2464 { 2465 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2466 T(TimerTriggered(endpoint, "persist")); 2467 2468 MutexLocker locker(endpoint->fLock); 2469 if (!locker.IsLocked()) 2470 return; 2471 2472 // the timer might not have been canceled early enough 2473 if (endpoint->State() == CLOSED) 2474 return; 2475 2476 endpoint->_SendQueued(true); 2477 } 2478 2479 2480 /*static*/ void 2481 TCPEndpoint::_DelayedAcknowledgeTimer(net_timer* timer, void* _endpoint) 2482 { 2483 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2484 T(TimerTriggered(endpoint, "delayed ack")); 2485 2486 MutexLocker locker(endpoint->fLock); 2487 if (!locker.IsLocked()) 2488 return; 2489 2490 // the timer might not have been canceled early enough 2491 if (endpoint->State() == CLOSED) 2492 return; 2493 2494 endpoint->SendAcknowledge(true); 2495 } 2496 2497 2498 /*static*/ void 2499 TCPEndpoint::_TimeWaitTimer(net_timer* timer, void* _endpoint) 2500 { 2501 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2502 T(TimerTriggered(endpoint, "time-wait")); 2503 2504 MutexLocker locker(endpoint->fLock); 2505 if (!locker.IsLocked()) 2506 return; 2507 2508 if ((endpoint->fFlags & FLAG_CLOSED) == 0) { 2509 endpoint->fFlags |= FLAG_DELETE_ON_CLOSE; 2510 return; 2511 } 2512 2513 locker.Unlock(); 2514 2515 gSocketModule->release_socket(endpoint->socket); 2516 } 2517 2518 2519 /*static*/ status_t 2520 TCPEndpoint::_WaitForCondition(ConditionVariable& condition, 2521 MutexLocker& locker, bigtime_t timeout) 2522 { 2523 ConditionVariableEntry entry; 2524 condition.Add(&entry); 2525 2526 locker.Unlock(); 2527 status_t result = entry.Wait(B_ABSOLUTE_TIMEOUT | B_CAN_INTERRUPT, timeout); 2528 locker.Lock(); 2529 2530 return result; 2531 } 2532 2533 2534 // #pragma mark - 2535 2536 2537 void 2538 TCPEndpoint::Dump() const 2539 { 2540 kprintf("TCP endpoint %p\n", this); 2541 kprintf(" state: %s\n", name_for_state(fState)); 2542 kprintf(" flags: 0x%" B_PRIx32 "\n", fFlags); 2543 #if KDEBUG 2544 kprintf(" lock: { %p, holder: %" B_PRId32 " }\n", &fLock, fLock.holder); 2545 #endif 2546 kprintf(" accept sem: %" B_PRId32 "\n", fAcceptSemaphore); 2547 kprintf(" options: 0x%" B_PRIx32 "\n", (uint32)fOptions); 2548 kprintf(" send\n"); 2549 kprintf(" window shift: %" B_PRIu8 "\n", fSendWindowShift); 2550 kprintf(" unacknowledged: %" B_PRIu32 "\n", 2551 fSendUnacknowledged.Number()); 2552 kprintf(" next: %" B_PRIu32 "\n", fSendNext.Number()); 2553 kprintf(" max: %" B_PRIu32 "\n", fSendMax.Number()); 2554 kprintf(" urgent offset: %" B_PRIu32 "\n", fSendUrgentOffset.Number()); 2555 kprintf(" window: %" B_PRIu32 "\n", fSendWindow); 2556 kprintf(" max window: %" B_PRIu32 "\n", fSendMaxWindow); 2557 kprintf(" max segment size: %" B_PRIu32 "\n", fSendMaxSegmentSize); 2558 kprintf(" queue: %" B_PRIuSIZE " / %" B_PRIuSIZE "\n", fSendQueue.Used(), 2559 fSendQueue.Size()); 2560 #if DEBUG_TCP_BUFFER_QUEUE 2561 fSendQueue.Dump(); 2562 #endif 2563 kprintf(" last acknowledge sent: %" B_PRIu32 "\n", 2564 fLastAcknowledgeSent.Number()); 2565 kprintf(" initial sequence: %" B_PRIu32 "\n", 2566 fInitialSendSequence.Number()); 2567 kprintf(" receive\n"); 2568 kprintf(" window shift: %" B_PRIu8 "\n", fReceiveWindowShift); 2569 kprintf(" next: %" B_PRIu32 "\n", fReceiveNext.Number()); 2570 kprintf(" max advertised: %" B_PRIu32 "\n", 2571 fReceiveMaxAdvertised.Number()); 2572 kprintf(" window: %" B_PRIu32 "\n", fReceiveWindow); 2573 kprintf(" max segment size: %" B_PRIu32 "\n", fReceiveMaxSegmentSize); 2574 kprintf(" queue: %" B_PRIuSIZE " / %" B_PRIuSIZE "\n", 2575 fReceiveQueue.Available(), fReceiveQueue.Size()); 2576 #if DEBUG_TCP_BUFFER_QUEUE 2577 fReceiveQueue.Dump(); 2578 #endif 2579 kprintf(" initial sequence: %" B_PRIu32 "\n", 2580 fInitialReceiveSequence.Number()); 2581 kprintf(" duplicate acknowledge count: %" B_PRIu32 "\n", 2582 fDuplicateAcknowledgeCount); 2583 kprintf(" smoothed round trip time: %" B_PRId32 " (deviation %" B_PRId32 ")\n", 2584 fSmoothedRoundTripTime, fRoundTripVariation); 2585 kprintf(" retransmit timeout: %" B_PRId64 "\n", fRetransmitTimeout); 2586 kprintf(" congestion window: %" B_PRIu32 "\n", fCongestionWindow); 2587 kprintf(" slow start threshold: %" B_PRIu32 "\n", fSlowStartThreshold); 2588 } 2589 2590