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 }; 324 325 326 static const int kTimestampFactor = 1000; 327 // conversion factor between usec system time and msec tcp time 328 329 330 static inline bigtime_t 331 absolute_timeout(bigtime_t timeout) 332 { 333 if (timeout == 0 || timeout == B_INFINITE_TIMEOUT) 334 return timeout; 335 336 return timeout + system_time(); 337 } 338 339 340 static inline status_t 341 posix_error(status_t error) 342 { 343 if (error == B_TIMED_OUT) 344 return B_WOULD_BLOCK; 345 346 return error; 347 } 348 349 350 static inline bool 351 in_window(const tcp_sequence& sequence, const tcp_sequence& receiveNext, 352 uint32 receiveWindow) 353 { 354 return sequence >= receiveNext && sequence < (receiveNext + receiveWindow); 355 } 356 357 358 static inline bool 359 segment_in_sequence(const tcp_segment_header& segment, int size, 360 const tcp_sequence& receiveNext, uint32 receiveWindow) 361 { 362 tcp_sequence sequence(segment.sequence); 363 if (size == 0) { 364 if (receiveWindow == 0) 365 return sequence == receiveNext; 366 return in_window(sequence, receiveNext, receiveWindow); 367 } else { 368 if (receiveWindow == 0) 369 return false; 370 return in_window(sequence, receiveNext, receiveWindow) 371 || in_window(sequence + size - 1, receiveNext, receiveWindow); 372 } 373 } 374 375 376 static inline bool 377 is_writable(tcp_state state) 378 { 379 return state == ESTABLISHED || state == FINISH_RECEIVED; 380 } 381 382 383 static inline bool 384 is_establishing(tcp_state state) 385 { 386 return state == SYNCHRONIZE_SENT || state == SYNCHRONIZE_RECEIVED; 387 } 388 389 390 static inline uint32 tcp_now() 391 { 392 return system_time() / kTimestampFactor; 393 } 394 395 396 static inline uint32 tcp_diff_timestamp(uint32 base) 397 { 398 uint32 now = tcp_now(); 399 400 if (now > base) 401 return now - base; 402 403 return now + UINT_MAX - base; 404 } 405 406 407 static inline bool 408 state_needs_finish(int32 state) 409 { 410 return state == WAIT_FOR_FINISH_ACKNOWLEDGE 411 || state == FINISH_SENT || state == CLOSING; 412 } 413 414 415 // #pragma mark - 416 417 418 TCPEndpoint::TCPEndpoint(net_socket* socket) 419 : 420 ProtocolSocket(socket), 421 fManager(NULL), 422 fOptions(0), 423 fSendWindowShift(0), 424 fReceiveWindowShift(0), 425 fSendUnacknowledged(0), 426 fSendNext(0), 427 fSendMax(0), 428 fSendUrgentOffset(0), 429 fSendWindow(0), 430 fSendMaxWindow(0), 431 fSendMaxSegmentSize(TCP_DEFAULT_MAX_SEGMENT_SIZE), 432 fSendMaxSegments(0), 433 fSendQueue(socket->send.buffer_size), 434 fInitialSendSequence(0), 435 fPreviousHighestAcknowledge(0), 436 fDuplicateAcknowledgeCount(0), 437 fPreviousFlightSize(0), 438 fRecover(0), 439 fRoute(NULL), 440 fReceiveNext(0), 441 fReceiveMaxAdvertised(0), 442 fReceiveWindow(socket->receive.buffer_size), 443 fReceiveMaxSegmentSize(TCP_DEFAULT_MAX_SEGMENT_SIZE), 444 fReceiveQueue(socket->receive.buffer_size), 445 fSmoothedRoundTripTime(0), 446 fRoundTripVariation(0), 447 fSendTime(0), 448 fRoundTripStartSequence(0), 449 fRetransmitTimeout(TCP_INITIAL_RTT), 450 fReceivedTimestamp(0), 451 fCongestionWindow(0), 452 fSlowStartThreshold(0), 453 fState(CLOSED), 454 fFlags(FLAG_OPTION_WINDOW_SCALE | FLAG_OPTION_TIMESTAMP) 455 { 456 // TODO: to be replaced with a real read/write locking strategy! 457 mutex_init(&fLock, "tcp lock"); 458 459 fReceiveCondition.Init(this, "tcp receive"); 460 fSendCondition.Init(this, "tcp send"); 461 462 gStackModule->init_timer(&fPersistTimer, TCPEndpoint::_PersistTimer, this); 463 gStackModule->init_timer(&fRetransmitTimer, TCPEndpoint::_RetransmitTimer, 464 this); 465 gStackModule->init_timer(&fDelayedAcknowledgeTimer, 466 TCPEndpoint::_DelayedAcknowledgeTimer, this); 467 gStackModule->init_timer(&fTimeWaitTimer, TCPEndpoint::_TimeWaitTimer, 468 this); 469 470 T(APICall(this, "constructor")); 471 } 472 473 474 TCPEndpoint::~TCPEndpoint() 475 { 476 mutex_lock(&fLock); 477 478 T(APICall(this, "destructor")); 479 480 _CancelConnectionTimers(); 481 gStackModule->cancel_timer(&fTimeWaitTimer); 482 T(TimerSet(this, "time-wait", -1)); 483 484 if (fManager != NULL) { 485 fManager->Unbind(this); 486 put_endpoint_manager(fManager); 487 } 488 489 mutex_destroy(&fLock); 490 491 // we need to wait for all timers to return 492 gStackModule->wait_for_timer(&fRetransmitTimer); 493 gStackModule->wait_for_timer(&fPersistTimer); 494 gStackModule->wait_for_timer(&fDelayedAcknowledgeTimer); 495 gStackModule->wait_for_timer(&fTimeWaitTimer); 496 497 gDatalinkModule->put_route(Domain(), fRoute); 498 } 499 500 501 status_t 502 TCPEndpoint::InitCheck() const 503 { 504 return B_OK; 505 } 506 507 508 // #pragma mark - protocol API 509 510 511 status_t 512 TCPEndpoint::Open() 513 { 514 TRACE("Open()"); 515 T(APICall(this, "open")); 516 517 status_t status = ProtocolSocket::Open(); 518 if (status < B_OK) 519 return status; 520 521 fManager = get_endpoint_manager(Domain()); 522 if (fManager == NULL) 523 return EAFNOSUPPORT; 524 525 return B_OK; 526 } 527 528 529 status_t 530 TCPEndpoint::Close() 531 { 532 MutexLocker locker(fLock); 533 534 TRACE("Close()"); 535 T(APICall(this, "close")); 536 537 if (fState == LISTEN) 538 delete_sem(fAcceptSemaphore); 539 540 if (fState == SYNCHRONIZE_SENT || fState == LISTEN) { 541 // TODO: what about linger in case of SYNCHRONIZE_SENT? 542 fState = CLOSED; 543 T(State(this)); 544 return B_OK; 545 } 546 547 status_t status = _Disconnect(true); 548 if (status != B_OK) 549 return status; 550 551 if (socket->options & SO_LINGER) { 552 TRACE("Close(): Lingering for %i secs", socket->linger); 553 554 bigtime_t maximum = absolute_timeout(socket->linger * 1000000LL); 555 556 while (fSendQueue.Used() > 0) { 557 status = _WaitForCondition(fSendCondition, locker, maximum); 558 if (status == B_TIMED_OUT || status == B_WOULD_BLOCK) 559 break; 560 else if (status < B_OK) 561 return status; 562 } 563 564 TRACE("Close(): after waiting, the SendQ was left with %" B_PRIuSIZE 565 " bytes.", fSendQueue.Used()); 566 } 567 return B_OK; 568 } 569 570 571 void 572 TCPEndpoint::Free() 573 { 574 MutexLocker _(fLock); 575 576 TRACE("Free()"); 577 T(APICall(this, "free")); 578 579 if (fState <= SYNCHRONIZE_SENT) 580 return; 581 582 // we are only interested in the timer, not in changing state 583 _EnterTimeWait(); 584 585 fFlags |= FLAG_CLOSED; 586 if ((fFlags & FLAG_DELETE_ON_CLOSE) == 0) { 587 // we'll be freed later when the 2MSL timer expires 588 gSocketModule->acquire_socket(socket); 589 } 590 } 591 592 593 /*! Creates and sends a synchronize packet to /a address, and then waits 594 until the connection has been established or refused. 595 */ 596 status_t 597 TCPEndpoint::Connect(const sockaddr* address) 598 { 599 if (!AddressModule()->is_same_family(address)) 600 return EAFNOSUPPORT; 601 602 MutexLocker locker(fLock); 603 604 TRACE("Connect() on address %s", PrintAddress(address)); 605 T(APICall(this, "connect")); 606 607 if (gStackModule->is_restarted_syscall()) { 608 bigtime_t timeout = gStackModule->restore_syscall_restart_timeout(); 609 status_t status = _WaitForEstablished(locker, timeout); 610 TRACE(" Connect(): Connection complete: %s (timeout was %" 611 B_PRIdBIGTIME ")", strerror(status), timeout); 612 return posix_error(status); 613 } 614 615 // Can only call connect() from CLOSED or LISTEN states 616 // otherwise endpoint is considered already connected 617 if (fState == LISTEN) { 618 // this socket is about to connect; remove pending connections in the backlog 619 gSocketModule->set_max_backlog(socket, 0); 620 } else if (fState == ESTABLISHED) { 621 return EISCONN; 622 } else if (fState != CLOSED) 623 return EALREADY; 624 625 // consider destination address INADDR_ANY as INADDR_LOOPBACK 626 sockaddr_storage _address; 627 if (AddressModule()->is_empty_address(address, false)) { 628 AddressModule()->get_loopback_address((sockaddr *)&_address); 629 // for IPv4 and IPv6 the port is at the same offset 630 ((sockaddr_in &)_address).sin_port = ((sockaddr_in *)address)->sin_port; 631 address = (sockaddr *)&_address; 632 } 633 634 status_t status = _PrepareSendPath(address); 635 if (status < B_OK) 636 return status; 637 638 TRACE(" Connect(): starting 3-way handshake..."); 639 640 fState = SYNCHRONIZE_SENT; 641 T(State(this)); 642 643 // send SYN 644 status = _SendQueued(); 645 if (status != B_OK) { 646 _Close(); 647 return status; 648 } 649 650 // If we are running over Loopback, after _SendQueued() returns we 651 // may be in ESTABLISHED already. 652 if (fState == ESTABLISHED) { 653 TRACE(" Connect() completed after _SendQueued()"); 654 return B_OK; 655 } 656 657 // wait until 3-way handshake is complete (if needed) 658 bigtime_t timeout = min_c(socket->send.timeout, TCP_CONNECTION_TIMEOUT); 659 if (timeout == 0) { 660 // we're a non-blocking socket 661 TRACE(" Connect() delayed, return EINPROGRESS"); 662 return EINPROGRESS; 663 } 664 665 bigtime_t absoluteTimeout = absolute_timeout(timeout); 666 gStackModule->store_syscall_restart_timeout(absoluteTimeout); 667 668 status = _WaitForEstablished(locker, absoluteTimeout); 669 TRACE(" Connect(): Connection complete: %s (timeout was %" B_PRIdBIGTIME 670 ")", strerror(status), timeout); 671 return posix_error(status); 672 } 673 674 675 status_t 676 TCPEndpoint::Accept(struct net_socket** _acceptedSocket) 677 { 678 MutexLocker locker(fLock); 679 680 TRACE("Accept()"); 681 T(APICall(this, "accept")); 682 683 status_t status; 684 bigtime_t timeout = absolute_timeout(socket->receive.timeout); 685 if (gStackModule->is_restarted_syscall()) 686 timeout = gStackModule->restore_syscall_restart_timeout(); 687 else 688 gStackModule->store_syscall_restart_timeout(timeout); 689 690 do { 691 locker.Unlock(); 692 693 status = acquire_sem_etc(fAcceptSemaphore, 1, B_ABSOLUTE_TIMEOUT 694 | B_CAN_INTERRUPT, timeout); 695 if (status != B_OK) { 696 if (status == B_TIMED_OUT && socket->receive.timeout == 0) 697 return B_WOULD_BLOCK; 698 699 return status; 700 } 701 702 locker.Lock(); 703 status = gSocketModule->dequeue_connected(socket, _acceptedSocket); 704 #ifdef TRACE_TCP 705 if (status == B_OK) 706 TRACE(" Accept() returning %p", (*_acceptedSocket)->first_protocol); 707 #endif 708 } while (status != B_OK); 709 710 return status; 711 } 712 713 714 status_t 715 TCPEndpoint::Bind(const sockaddr *address) 716 { 717 if (address == NULL) 718 return B_BAD_VALUE; 719 720 MutexLocker lock(fLock); 721 722 TRACE("Bind() on address %s", PrintAddress(address)); 723 T(APICall(this, "bind")); 724 725 if (fState != CLOSED) 726 return EISCONN; 727 728 return fManager->Bind(this, address); 729 } 730 731 732 status_t 733 TCPEndpoint::Unbind(struct sockaddr *address) 734 { 735 MutexLocker _(fLock); 736 737 TRACE("Unbind()"); 738 T(APICall(this, "unbind")); 739 740 return fManager->Unbind(this); 741 } 742 743 744 status_t 745 TCPEndpoint::Listen(int count) 746 { 747 MutexLocker _(fLock); 748 749 TRACE("Listen()"); 750 T(APICall(this, "listen")); 751 752 if (fState != CLOSED && fState != LISTEN) 753 return B_BAD_VALUE; 754 755 if (fState == CLOSED) { 756 fAcceptSemaphore = create_sem(0, "tcp accept"); 757 if (fAcceptSemaphore < B_OK) 758 return ENOBUFS; 759 760 status_t status = fManager->SetPassive(this); 761 if (status != B_OK) { 762 delete_sem(fAcceptSemaphore); 763 fAcceptSemaphore = -1; 764 return status; 765 } 766 } 767 768 gSocketModule->set_max_backlog(socket, count); 769 770 fState = LISTEN; 771 T(State(this)); 772 return B_OK; 773 } 774 775 776 status_t 777 TCPEndpoint::Shutdown(int direction) 778 { 779 MutexLocker lock(fLock); 780 781 TRACE("Shutdown(%i)", direction); 782 T(APICall(this, "shutdown")); 783 784 if (direction == SHUT_RD || direction == SHUT_RDWR) 785 fFlags |= FLAG_NO_RECEIVE; 786 787 if (direction == SHUT_WR || direction == SHUT_RDWR) { 788 // TODO: That's not correct. After read/write shutting down the socket 789 // one should still be able to read previously arrived data. 790 _Disconnect(false); 791 } 792 793 return B_OK; 794 } 795 796 797 /*! Puts data contained in \a buffer into send buffer */ 798 status_t 799 TCPEndpoint::SendData(net_buffer *buffer) 800 { 801 MutexLocker lock(fLock); 802 803 TRACE("SendData(buffer %p, size %" B_PRIu32 ", flags %#" B_PRIx32 804 ") [total %" B_PRIuSIZE " bytes, has %" B_PRIuSIZE "]", buffer, 805 buffer->size, buffer->flags, fSendQueue.Size(), fSendQueue.Free()); 806 T(APICall(this, "senddata")); 807 808 uint32 flags = buffer->flags; 809 810 if (fState == CLOSED) 811 return ENOTCONN; 812 if (fState == LISTEN) 813 return EDESTADDRREQ; 814 if (!is_writable(fState) && !is_establishing(fState)) { 815 // we only send signals when called from userland 816 if (gStackModule->is_syscall() && (flags & MSG_NOSIGNAL) == 0) 817 send_signal(find_thread(NULL), SIGPIPE); 818 return EPIPE; 819 } 820 821 size_t left = buffer->size; 822 823 bigtime_t timeout = absolute_timeout(socket->send.timeout); 824 if (gStackModule->is_restarted_syscall()) 825 timeout = gStackModule->restore_syscall_restart_timeout(); 826 else 827 gStackModule->store_syscall_restart_timeout(timeout); 828 829 while (left > 0) { 830 while (fSendQueue.Free() < socket->send.low_water_mark) { 831 // wait until enough space is available 832 status_t status = _WaitForCondition(fSendCondition, lock, timeout); 833 if (status < B_OK) { 834 TRACE(" SendData() returning %s (%d)", 835 strerror(posix_error(status)), (int)posix_error(status)); 836 return posix_error(status); 837 } 838 839 if (!is_writable(fState) && !is_establishing(fState)) { 840 // we only send signals when called from userland 841 if (gStackModule->is_syscall()) 842 send_signal(find_thread(NULL), SIGPIPE); 843 return EPIPE; 844 } 845 } 846 847 size_t size = fSendQueue.Free(); 848 if (size < left) { 849 // we need to split the original buffer 850 net_buffer* clone = gBufferModule->clone(buffer, false); 851 // TODO: add offset/size parameter to net_buffer::clone() or 852 // even a move_data() function, as this is a bit inefficient 853 if (clone == NULL) 854 return ENOBUFS; 855 856 status_t status = gBufferModule->trim(clone, size); 857 if (status != B_OK) { 858 gBufferModule->free(clone); 859 return status; 860 } 861 862 gBufferModule->remove_header(buffer, size); 863 left -= size; 864 fSendQueue.Add(clone); 865 } else { 866 left -= buffer->size; 867 fSendQueue.Add(buffer); 868 } 869 } 870 871 TRACE(" SendData(): %" B_PRIuSIZE " bytes used.", fSendQueue.Used()); 872 873 bool force = false; 874 if ((flags & MSG_OOB) != 0) { 875 fSendUrgentOffset = fSendQueue.LastSequence(); 876 // RFC 961 specifies that the urgent offset points to the last 877 // byte of urgent data. However, this is commonly implemented as 878 // here, ie. it points to the first byte after the urgent data. 879 force = true; 880 } 881 if ((flags & MSG_EOF) != 0) 882 _Disconnect(false); 883 884 if (fState == ESTABLISHED || fState == FINISH_RECEIVED) 885 _SendQueued(force); 886 887 return B_OK; 888 } 889 890 891 ssize_t 892 TCPEndpoint::SendAvailable() 893 { 894 MutexLocker locker(fLock); 895 896 ssize_t available; 897 898 if (is_writable(fState)) 899 available = fSendQueue.Free(); 900 else if (is_establishing(fState)) 901 available = 0; 902 else 903 available = EPIPE; 904 905 TRACE("SendAvailable(): %" B_PRIdSSIZE, available); 906 T(APICall(this, "sendavailable")); 907 return available; 908 } 909 910 911 status_t 912 TCPEndpoint::FillStat(net_stat *stat) 913 { 914 MutexLocker _(fLock); 915 916 strlcpy(stat->state, name_for_state(fState), sizeof(stat->state)); 917 stat->receive_queue_size = fReceiveQueue.Available(); 918 stat->send_queue_size = fSendQueue.Used(); 919 920 return B_OK; 921 } 922 923 924 status_t 925 TCPEndpoint::ReadData(size_t numBytes, uint32 flags, net_buffer** _buffer) 926 { 927 MutexLocker locker(fLock); 928 929 TRACE("ReadData(%" B_PRIuSIZE " bytes, flags %#" B_PRIx32 ")", numBytes, 930 flags); 931 T(APICall(this, "readdata")); 932 933 *_buffer = NULL; 934 935 if (fState == CLOSED) 936 return ENOTCONN; 937 938 bigtime_t timeout = absolute_timeout(socket->receive.timeout); 939 if (gStackModule->is_restarted_syscall()) 940 timeout = gStackModule->restore_syscall_restart_timeout(); 941 else 942 gStackModule->store_syscall_restart_timeout(timeout); 943 944 if (fState == SYNCHRONIZE_SENT || fState == SYNCHRONIZE_RECEIVED) { 945 if (flags & MSG_DONTWAIT) 946 return B_WOULD_BLOCK; 947 948 status_t status = _WaitForEstablished(locker, timeout); 949 if (status < B_OK) 950 return posix_error(status); 951 } 952 953 size_t dataNeeded = socket->receive.low_water_mark; 954 955 // When MSG_WAITALL is set then the function should block 956 // until the full amount of data can be returned. 957 if (flags & MSG_WAITALL) 958 dataNeeded = numBytes; 959 960 // TODO: add support for urgent data (MSG_OOB) 961 962 while (true) { 963 if (fState == CLOSING || fState == WAIT_FOR_FINISH_ACKNOWLEDGE 964 || fState == TIME_WAIT) { 965 // ``Connection closing''. 966 return B_OK; 967 } 968 969 if (fReceiveQueue.Available() > 0) { 970 if (fReceiveQueue.Available() >= dataNeeded 971 || (fReceiveQueue.PushedData() > 0 972 && fReceiveQueue.PushedData() >= fReceiveQueue.Available())) 973 break; 974 } else if (fState == FINISH_RECEIVED) { 975 // ``If no text is awaiting delivery, the RECEIVE will 976 // get a Connection closing''. 977 return B_OK; 978 } 979 980 if ((flags & MSG_DONTWAIT) != 0 || socket->receive.timeout == 0) 981 return B_WOULD_BLOCK; 982 983 if ((fFlags & FLAG_NO_RECEIVE) != 0) 984 return B_OK; 985 986 status_t status = _WaitForCondition(fReceiveCondition, locker, timeout); 987 if (status < B_OK) { 988 // The Open Group base specification mentions that EINTR should be 989 // returned if the recv() is interrupted before _any data_ is 990 // available. So we actually check if there is data, and if so, 991 // push it to the user. 992 if ((status == B_TIMED_OUT || status == B_INTERRUPTED) 993 && fReceiveQueue.Available() > 0) 994 break; 995 996 return posix_error(status); 997 } 998 } 999 1000 TRACE(" ReadData(): %" B_PRIuSIZE " are available.", 1001 fReceiveQueue.Available()); 1002 1003 if (numBytes < fReceiveQueue.Available()) 1004 fReceiveCondition.NotifyAll(); 1005 1006 bool clone = (flags & MSG_PEEK) != 0; 1007 1008 ssize_t receivedBytes = fReceiveQueue.Get(numBytes, !clone, _buffer); 1009 1010 TRACE(" ReadData(): %" B_PRIuSIZE " bytes kept.", 1011 fReceiveQueue.Available()); 1012 1013 // if we are opening the window, check if we should send an ACK 1014 if (!clone) 1015 SendAcknowledge(false); 1016 1017 return receivedBytes; 1018 } 1019 1020 1021 ssize_t 1022 TCPEndpoint::ReadAvailable() 1023 { 1024 MutexLocker locker(fLock); 1025 1026 TRACE("ReadAvailable(): %" B_PRIdSSIZE, _AvailableData()); 1027 T(APICall(this, "readavailable")); 1028 1029 return _AvailableData(); 1030 } 1031 1032 1033 status_t 1034 TCPEndpoint::SetSendBufferSize(size_t length) 1035 { 1036 MutexLocker _(fLock); 1037 fSendQueue.SetMaxBytes(length); 1038 return B_OK; 1039 } 1040 1041 1042 status_t 1043 TCPEndpoint::SetReceiveBufferSize(size_t length) 1044 { 1045 MutexLocker _(fLock); 1046 fReceiveQueue.SetMaxBytes(length); 1047 return B_OK; 1048 } 1049 1050 1051 status_t 1052 TCPEndpoint::GetOption(int option, void* _value, int* _length) 1053 { 1054 if (*_length != sizeof(int)) 1055 return B_BAD_VALUE; 1056 1057 int* value = (int*)_value; 1058 1059 switch (option) { 1060 case TCP_NODELAY: 1061 if ((fOptions & TCP_NODELAY) != 0) 1062 *value = 1; 1063 else 1064 *value = 0; 1065 return B_OK; 1066 1067 case TCP_MAXSEG: 1068 *value = fReceiveMaxSegmentSize; 1069 return B_OK; 1070 1071 default: 1072 return B_BAD_VALUE; 1073 } 1074 } 1075 1076 1077 status_t 1078 TCPEndpoint::SetOption(int option, const void* _value, int length) 1079 { 1080 if (option != TCP_NODELAY) 1081 return B_BAD_VALUE; 1082 1083 if (length != sizeof(int)) 1084 return B_BAD_VALUE; 1085 1086 const int* value = (const int*)_value; 1087 1088 MutexLocker _(fLock); 1089 if (*value) 1090 fOptions |= TCP_NODELAY; 1091 else 1092 fOptions &= ~TCP_NODELAY; 1093 1094 return B_OK; 1095 } 1096 1097 1098 // #pragma mark - misc 1099 1100 1101 bool 1102 TCPEndpoint::IsBound() const 1103 { 1104 return !LocalAddress().IsEmpty(true); 1105 } 1106 1107 1108 bool 1109 TCPEndpoint::IsLocal() const 1110 { 1111 return (fFlags & FLAG_LOCAL) != 0; 1112 } 1113 1114 1115 status_t 1116 TCPEndpoint::DelayedAcknowledge() 1117 { 1118 if (gStackModule->cancel_timer(&fDelayedAcknowledgeTimer)) { 1119 // timer was active, send an ACK now (with the exception above, 1120 // we send every other ACK) 1121 T(TimerSet(this, "delayed ack", -1)); 1122 return SendAcknowledge(true); 1123 } 1124 1125 gStackModule->set_timer(&fDelayedAcknowledgeTimer, 1126 TCP_DELAYED_ACKNOWLEDGE_TIMEOUT); 1127 T(TimerSet(this, "delayed ack", TCP_DELAYED_ACKNOWLEDGE_TIMEOUT)); 1128 return B_OK; 1129 } 1130 1131 1132 status_t 1133 TCPEndpoint::SendAcknowledge(bool force) 1134 { 1135 return _SendQueued(force, 0); 1136 } 1137 1138 1139 void 1140 TCPEndpoint::_StartPersistTimer() 1141 { 1142 gStackModule->set_timer(&fPersistTimer, TCP_PERSIST_TIMEOUT); 1143 T(TimerSet(this, "persist", TCP_PERSIST_TIMEOUT)); 1144 } 1145 1146 1147 void 1148 TCPEndpoint::_EnterTimeWait() 1149 { 1150 TRACE("_EnterTimeWait()"); 1151 1152 if (fState == TIME_WAIT) { 1153 _CancelConnectionTimers(); 1154 1155 if (IsLocal()) { 1156 // we do not use TIME_WAIT state for local connections 1157 fFlags |= FLAG_DELETE_ON_CLOSE; 1158 return; 1159 } 1160 } 1161 1162 _UpdateTimeWait(); 1163 } 1164 1165 1166 void 1167 TCPEndpoint::_UpdateTimeWait() 1168 { 1169 gStackModule->set_timer(&fTimeWaitTimer, TCP_MAX_SEGMENT_LIFETIME << 1); 1170 T(TimerSet(this, "time-wait", TCP_MAX_SEGMENT_LIFETIME << 1)); 1171 } 1172 1173 1174 void 1175 TCPEndpoint::_CancelConnectionTimers() 1176 { 1177 gStackModule->cancel_timer(&fRetransmitTimer); 1178 T(TimerSet(this, "retransmit", -1)); 1179 gStackModule->cancel_timer(&fPersistTimer); 1180 T(TimerSet(this, "persist", -1)); 1181 gStackModule->cancel_timer(&fDelayedAcknowledgeTimer); 1182 T(TimerSet(this, "delayed ack", -1)); 1183 } 1184 1185 1186 /*! Sends the FIN flag to the peer when the connection is still open. 1187 Moves the endpoint to the next state depending on where it was. 1188 */ 1189 status_t 1190 TCPEndpoint::_Disconnect(bool closing) 1191 { 1192 tcp_state previousState = fState; 1193 1194 if (fState == SYNCHRONIZE_RECEIVED || fState == ESTABLISHED) 1195 fState = FINISH_SENT; 1196 else if (fState == FINISH_RECEIVED) 1197 fState = WAIT_FOR_FINISH_ACKNOWLEDGE; 1198 else 1199 return B_OK; 1200 1201 T(State(this)); 1202 1203 status_t status = _SendQueued(); 1204 if (status != B_OK) { 1205 fState = previousState; 1206 T(State(this)); 1207 return status; 1208 } 1209 1210 return B_OK; 1211 } 1212 1213 1214 void 1215 TCPEndpoint::_MarkEstablished() 1216 { 1217 fState = ESTABLISHED; 1218 T(State(this)); 1219 1220 if (gSocketModule->has_parent(socket)) { 1221 gSocketModule->set_connected(socket); 1222 release_sem_etc(fAcceptSemaphore, 1, B_DO_NOT_RESCHEDULE); 1223 } 1224 1225 fSendCondition.NotifyAll(); 1226 gSocketModule->notify(socket, B_SELECT_WRITE, fSendQueue.Free()); 1227 } 1228 1229 1230 status_t 1231 TCPEndpoint::_WaitForEstablished(MutexLocker &locker, bigtime_t timeout) 1232 { 1233 // TODO: Checking for CLOSED seems correct, but breaks several neon tests. 1234 // When investigating this, also have a look at _Close() and _HandleReset(). 1235 while (fState < ESTABLISHED/* && fState != CLOSED*/) { 1236 if (socket->error != B_OK) 1237 return socket->error; 1238 1239 status_t status = _WaitForCondition(fSendCondition, locker, timeout); 1240 if (status < B_OK) 1241 return status; 1242 } 1243 1244 return B_OK; 1245 } 1246 1247 1248 // #pragma mark - receive 1249 1250 1251 void 1252 TCPEndpoint::_Close() 1253 { 1254 _CancelConnectionTimers(); 1255 fState = CLOSED; 1256 T(State(this)); 1257 1258 fFlags |= FLAG_DELETE_ON_CLOSE; 1259 1260 fSendCondition.NotifyAll(); 1261 _NotifyReader(); 1262 1263 if (gSocketModule->has_parent(socket)) { 1264 // We still have a parent - obviously, we haven't been accepted yet, 1265 // so no one could ever close us. 1266 _CancelConnectionTimers(); 1267 gSocketModule->set_aborted(socket); 1268 } 1269 } 1270 1271 1272 void 1273 TCPEndpoint::_HandleReset(status_t error) 1274 { 1275 socket->error = error; 1276 _Close(); 1277 1278 gSocketModule->notify(socket, B_SELECT_WRITE, error); 1279 gSocketModule->notify(socket, B_SELECT_ERROR, error); 1280 } 1281 1282 1283 void 1284 TCPEndpoint::_DuplicateAcknowledge(tcp_segment_header &segment) 1285 { 1286 if (fDuplicateAcknowledgeCount == 0) 1287 fPreviousFlightSize = (fSendMax - fSendUnacknowledged).Number(); 1288 1289 if (++fDuplicateAcknowledgeCount < 3) { 1290 if (fSendQueue.Available(fSendMax) != 0 && fSendWindow != 0) { 1291 fSendNext = fSendMax; 1292 fCongestionWindow += fDuplicateAcknowledgeCount * fSendMaxSegmentSize; 1293 _SendQueued(); 1294 TRACE("_DuplicateAcknowledge(): packet sent under limited transmit on receipt of dup ack"); 1295 fCongestionWindow -= fDuplicateAcknowledgeCount * fSendMaxSegmentSize; 1296 } 1297 } 1298 1299 if (fDuplicateAcknowledgeCount == 3) { 1300 if ((segment.acknowledge - 1) > fRecover || (fCongestionWindow > fSendMaxSegmentSize && 1301 (fSendUnacknowledged - fPreviousHighestAcknowledge) <= 4 * fSendMaxSegmentSize)) { 1302 fFlags |= FLAG_RECOVERY; 1303 fRecover = fSendMax.Number() - 1; 1304 fSlowStartThreshold = max_c(fPreviousFlightSize / 2, 2 * fSendMaxSegmentSize); 1305 fCongestionWindow = fSlowStartThreshold + 3 * fSendMaxSegmentSize; 1306 fSendNext = segment.acknowledge; 1307 _SendQueued(); 1308 TRACE("_DuplicateAcknowledge(): packet sent under fast restransmit on the receipt of 3rd dup ack"); 1309 } 1310 } else if (fDuplicateAcknowledgeCount > 3) { 1311 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 1312 if ((fDuplicateAcknowledgeCount - 3) * fSendMaxSegmentSize <= flightSize) 1313 fCongestionWindow += fSendMaxSegmentSize; 1314 if (fSendQueue.Available(fSendMax) != 0) { 1315 fSendNext = fSendMax; 1316 _SendQueued(); 1317 } 1318 } 1319 } 1320 1321 1322 void 1323 TCPEndpoint::_UpdateTimestamps(tcp_segment_header& segment, 1324 size_t segmentLength) 1325 { 1326 if (fFlags & FLAG_OPTION_TIMESTAMP) { 1327 tcp_sequence sequence(segment.sequence); 1328 1329 if (fLastAcknowledgeSent >= sequence 1330 && fLastAcknowledgeSent < (sequence + segmentLength)) 1331 fReceivedTimestamp = segment.timestamp_value; 1332 } 1333 } 1334 1335 1336 ssize_t 1337 TCPEndpoint::_AvailableData() const 1338 { 1339 // TODO: Refer to the FLAG_NO_RECEIVE comment above regarding 1340 // the application of FLAG_NO_RECEIVE in listen()ing 1341 // sockets. 1342 if (fState == LISTEN) 1343 return gSocketModule->count_connected(socket); 1344 if (fState == SYNCHRONIZE_SENT) 1345 return 0; 1346 1347 ssize_t availableData = fReceiveQueue.Available(); 1348 1349 if (availableData == 0 && !_ShouldReceive()) 1350 return ENOTCONN; 1351 1352 return availableData; 1353 } 1354 1355 1356 void 1357 TCPEndpoint::_NotifyReader() 1358 { 1359 fReceiveCondition.NotifyAll(); 1360 gSocketModule->notify(socket, B_SELECT_READ, _AvailableData()); 1361 } 1362 1363 1364 bool 1365 TCPEndpoint::_AddData(tcp_segment_header& segment, net_buffer* buffer) 1366 { 1367 if ((segment.flags & TCP_FLAG_FINISH) != 0) { 1368 // Remember the position of the finish received flag 1369 fFinishReceived = true; 1370 fFinishReceivedAt = segment.sequence + buffer->size; 1371 } 1372 1373 fReceiveQueue.Add(buffer, segment.sequence); 1374 fReceiveNext = fReceiveQueue.NextSequence(); 1375 1376 if (fFinishReceived) { 1377 // Set or reset the finish flag on the current segment 1378 if (fReceiveNext < fFinishReceivedAt) 1379 segment.flags &= ~TCP_FLAG_FINISH; 1380 else 1381 segment.flags |= TCP_FLAG_FINISH; 1382 } 1383 1384 TRACE(" _AddData(): adding data, receive next = %" B_PRIu32 ". Now have %" 1385 B_PRIuSIZE " bytes.", fReceiveNext.Number(), fReceiveQueue.Available()); 1386 1387 if ((segment.flags & TCP_FLAG_PUSH) != 0) 1388 fReceiveQueue.SetPushPointer(); 1389 1390 return fReceiveQueue.Available() > 0; 1391 } 1392 1393 1394 void 1395 TCPEndpoint::_PrepareReceivePath(tcp_segment_header& segment) 1396 { 1397 fInitialReceiveSequence = segment.sequence; 1398 fFinishReceived = false; 1399 1400 // count the received SYN 1401 segment.sequence++; 1402 1403 fReceiveNext = segment.sequence; 1404 fReceiveQueue.SetInitialSequence(segment.sequence); 1405 1406 if ((fOptions & TCP_NOOPT) == 0) { 1407 if (segment.max_segment_size > 0) 1408 fSendMaxSegmentSize = segment.max_segment_size; 1409 1410 if (segment.options & TCP_HAS_WINDOW_SCALE) { 1411 fFlags |= FLAG_OPTION_WINDOW_SCALE; 1412 fSendWindowShift = segment.window_shift; 1413 } else { 1414 fFlags &= ~FLAG_OPTION_WINDOW_SCALE; 1415 fReceiveWindowShift = 0; 1416 } 1417 1418 if (segment.options & TCP_HAS_TIMESTAMPS) { 1419 fFlags |= FLAG_OPTION_TIMESTAMP; 1420 fReceivedTimestamp = segment.timestamp_value; 1421 } else 1422 fFlags &= ~FLAG_OPTION_TIMESTAMP; 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; 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 _Acknowledged(segment); 1781 } 1782 } 1783 1784 if (segment.flags & TCP_FLAG_URGENT) { 1785 if (fState == ESTABLISHED || fState == FINISH_SENT 1786 || fState == FINISH_ACKNOWLEDGED) { 1787 // TODO: Handle urgent data: 1788 // - RCV.UP <- max(RCV.UP, SEG.UP) 1789 // - signal the user that urgent data is available (SIGURG) 1790 } 1791 } 1792 1793 bool notify = false; 1794 1795 // The buffer may be freed if its data is added to the queue, so cache 1796 // the size as we still need it later. 1797 uint32 bufferSize = buffer->size; 1798 1799 if ((bufferSize > 0 || (segment.flags & TCP_FLAG_FINISH) != 0) 1800 && _ShouldReceive()) 1801 notify = _AddData(segment, buffer); 1802 else { 1803 if ((fFlags & FLAG_NO_RECEIVE) != 0) 1804 fReceiveNext += buffer->size; 1805 1806 action = (action & ~KEEP) | DROP; 1807 } 1808 1809 if ((segment.flags & TCP_FLAG_FINISH) != 0) { 1810 segmentLength++; 1811 if (fState != CLOSED && fState != LISTEN && fState != SYNCHRONIZE_SENT) { 1812 TRACE("Receive(): peer is finishing connection!"); 1813 fReceiveNext++; 1814 notify = true; 1815 1816 // FIN implies PUSH 1817 fReceiveQueue.SetPushPointer(); 1818 1819 // we'll reply immediately to the FIN if we are not 1820 // transitioning to TIME WAIT so we immediatly ACK it. 1821 action |= IMMEDIATE_ACKNOWLEDGE; 1822 1823 // other side is closing connection; change states 1824 switch (fState) { 1825 case ESTABLISHED: 1826 case SYNCHRONIZE_RECEIVED: 1827 fState = FINISH_RECEIVED; 1828 T(State(this)); 1829 break; 1830 case FINISH_SENT: 1831 // simultaneous close 1832 fState = CLOSING; 1833 T(State(this)); 1834 break; 1835 case FINISH_ACKNOWLEDGED: 1836 fState = TIME_WAIT; 1837 T(State(this)); 1838 _EnterTimeWait(); 1839 break; 1840 case TIME_WAIT: 1841 _UpdateTimeWait(); 1842 break; 1843 1844 default: 1845 break; 1846 } 1847 } 1848 } 1849 1850 if (notify) 1851 _NotifyReader(); 1852 1853 if (bufferSize > 0 || (segment.flags & TCP_FLAG_SYNCHRONIZE) != 0) 1854 action |= ACKNOWLEDGE; 1855 1856 _UpdateTimestamps(segment, segmentLength); 1857 1858 TRACE("Receive() Action %" B_PRId32, action); 1859 1860 return action; 1861 } 1862 1863 1864 int32 1865 TCPEndpoint::SegmentReceived(tcp_segment_header& segment, net_buffer* buffer) 1866 { 1867 MutexLocker locker(fLock); 1868 1869 TRACE("SegmentReceived(): buffer %p (%" B_PRIu32 " bytes) address %s " 1870 "to %s flags %#" B_PRIx8 ", seq %" B_PRIu32 ", ack %" B_PRIu32 1871 ", wnd %" B_PRIu32, buffer, buffer->size, PrintAddress(buffer->source), 1872 PrintAddress(buffer->destination), segment.flags, segment.sequence, 1873 segment.acknowledge, 1874 (uint32)segment.advertised_window << fSendWindowShift); 1875 T(Receive(this, segment, 1876 (uint32)segment.advertised_window << fSendWindowShift, buffer)); 1877 int32 segmentAction = DROP; 1878 1879 switch (fState) { 1880 case LISTEN: 1881 segmentAction = _ListenReceive(segment, buffer); 1882 break; 1883 1884 case SYNCHRONIZE_SENT: 1885 segmentAction = _SynchronizeSentReceive(segment, buffer); 1886 break; 1887 1888 case SYNCHRONIZE_RECEIVED: 1889 case ESTABLISHED: 1890 case FINISH_RECEIVED: 1891 case WAIT_FOR_FINISH_ACKNOWLEDGE: 1892 case FINISH_SENT: 1893 case FINISH_ACKNOWLEDGED: 1894 case CLOSING: 1895 case TIME_WAIT: 1896 case CLOSED: 1897 segmentAction = _Receive(segment, buffer); 1898 break; 1899 } 1900 1901 // process acknowledge action as asked for by the *Receive() method 1902 if (segmentAction & IMMEDIATE_ACKNOWLEDGE) 1903 SendAcknowledge(true); 1904 else if (segmentAction & ACKNOWLEDGE) 1905 DelayedAcknowledge(); 1906 1907 if ((fFlags & (FLAG_CLOSED | FLAG_DELETE_ON_CLOSE)) 1908 == (FLAG_CLOSED | FLAG_DELETE_ON_CLOSE)) { 1909 locker.Unlock(); 1910 gSocketModule->release_socket(socket); 1911 } 1912 1913 return segmentAction; 1914 } 1915 1916 1917 // #pragma mark - send 1918 1919 1920 inline uint8 1921 TCPEndpoint::_CurrentFlags() 1922 { 1923 // we don't set FLAG_FINISH here, instead we do it 1924 // conditionally below depending if we are sending 1925 // the last bytes of the send queue. 1926 1927 switch (fState) { 1928 case CLOSED: 1929 return TCP_FLAG_RESET | TCP_FLAG_ACKNOWLEDGE; 1930 1931 case SYNCHRONIZE_SENT: 1932 return TCP_FLAG_SYNCHRONIZE; 1933 case SYNCHRONIZE_RECEIVED: 1934 return TCP_FLAG_SYNCHRONIZE | TCP_FLAG_ACKNOWLEDGE; 1935 1936 case ESTABLISHED: 1937 case FINISH_RECEIVED: 1938 case FINISH_ACKNOWLEDGED: 1939 case TIME_WAIT: 1940 case WAIT_FOR_FINISH_ACKNOWLEDGE: 1941 case FINISH_SENT: 1942 case CLOSING: 1943 return TCP_FLAG_ACKNOWLEDGE; 1944 1945 default: 1946 return 0; 1947 } 1948 } 1949 1950 1951 inline bool 1952 TCPEndpoint::_ShouldSendSegment(tcp_segment_header& segment, uint32 length, 1953 uint32 segmentMaxSize, uint32 flightSize) 1954 { 1955 if (fState == ESTABLISHED && fSendMaxSegments == 0) 1956 return false; 1957 1958 if (length > 0) { 1959 // Avoid the silly window syndrome - we only send a segment in case: 1960 // - we have a full segment to send, or 1961 // - we're at the end of our buffer queue, or 1962 // - the buffer is at least larger than half of the maximum send window, 1963 // or 1964 // - we're retransmitting data 1965 if (length == segmentMaxSize 1966 || (fOptions & TCP_NODELAY) != 0 1967 || tcp_sequence(fSendNext + length) == fSendQueue.LastSequence() 1968 || (fSendMaxWindow > 0 && length >= fSendMaxWindow / 2)) 1969 return true; 1970 } 1971 1972 // check if we need to send a window update to the peer 1973 if (segment.advertised_window > 0) { 1974 // correct the window to take into account what already has been advertised 1975 uint32 window = (segment.advertised_window << fReceiveWindowShift) 1976 - (fReceiveMaxAdvertised - fReceiveNext).Number(); 1977 1978 // if we can advertise a window larger than twice the maximum segment 1979 // size, or half the maximum buffer size we send a window update 1980 if (window >= (fReceiveMaxSegmentSize << 1) 1981 || window >= (socket->receive.buffer_size >> 1)) 1982 return true; 1983 } 1984 1985 if ((segment.flags & (TCP_FLAG_SYNCHRONIZE | TCP_FLAG_FINISH 1986 | TCP_FLAG_RESET)) != 0) 1987 return true; 1988 1989 // We do have urgent data pending 1990 if (fSendUrgentOffset > fSendNext) 1991 return true; 1992 1993 // there is no reason to send a segment just now 1994 return false; 1995 } 1996 1997 1998 status_t 1999 TCPEndpoint::_SendQueued(bool force) 2000 { 2001 return _SendQueued(force, fSendWindow); 2002 } 2003 2004 2005 /*! Sends one or more TCP segments with the data waiting in the queue, or some 2006 specific flags that need to be sent. 2007 */ 2008 status_t 2009 TCPEndpoint::_SendQueued(bool force, uint32 sendWindow) 2010 { 2011 if (fRoute == NULL) 2012 return B_ERROR; 2013 2014 // in passive state? 2015 if (fState == LISTEN) 2016 return B_ERROR; 2017 2018 tcp_segment_header segment(_CurrentFlags()); 2019 2020 if ((fOptions & TCP_NOOPT) == 0) { 2021 if ((fFlags & FLAG_OPTION_TIMESTAMP) != 0) { 2022 segment.options |= TCP_HAS_TIMESTAMPS; 2023 segment.timestamp_reply = fReceivedTimestamp; 2024 segment.timestamp_value = tcp_now(); 2025 } 2026 2027 if ((segment.flags & TCP_FLAG_SYNCHRONIZE) != 0 2028 && fSendNext == fInitialSendSequence) { 2029 // add connection establishment options 2030 segment.max_segment_size = fReceiveMaxSegmentSize; 2031 if (fFlags & FLAG_OPTION_WINDOW_SCALE) { 2032 segment.options |= TCP_HAS_WINDOW_SCALE; 2033 segment.window_shift = fReceiveWindowShift; 2034 } 2035 } 2036 } 2037 2038 size_t availableBytes = fReceiveQueue.Free(); 2039 // window size must remain same for duplicate acknowledgements 2040 if (!fReceiveQueue.IsContiguous()) 2041 availableBytes = (fReceiveMaxAdvertised - fReceiveNext).Number(); 2042 2043 if (fFlags & FLAG_OPTION_WINDOW_SCALE) 2044 segment.advertised_window = availableBytes >> fReceiveWindowShift; 2045 else 2046 segment.advertised_window = min_c(TCP_MAX_WINDOW, availableBytes); 2047 2048 segment.acknowledge = fReceiveNext.Number(); 2049 2050 // Process urgent data 2051 if (fSendUrgentOffset > fSendNext) { 2052 segment.flags |= TCP_FLAG_URGENT; 2053 segment.urgent_offset = (fSendUrgentOffset - fSendNext).Number(); 2054 } else { 2055 fSendUrgentOffset = fSendUnacknowledged.Number(); 2056 // Keep urgent offset updated, so that it doesn't reach into our 2057 // send window on overlap 2058 segment.urgent_offset = 0; 2059 } 2060 2061 if (fCongestionWindow > 0 && fCongestionWindow < sendWindow) 2062 sendWindow = fCongestionWindow; 2063 2064 // fSendUnacknowledged 2065 // | fSendNext fSendMax 2066 // | | | 2067 // v v v 2068 // ----------------------------------- 2069 // | effective window | 2070 // ----------------------------------- 2071 2072 // Flight size represents the window of data which is currently in the 2073 // ether. We should never send data such as the flight size becomes larger 2074 // than the effective window. Note however that the effective window may be 2075 // reduced (by congestion for instance), so at some point in time flight 2076 // size may be larger than the currently calculated window. 2077 2078 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 2079 uint32 consumedWindow = (fSendNext - fSendUnacknowledged).Number(); 2080 2081 if (consumedWindow > sendWindow) { 2082 sendWindow = 0; 2083 // TODO: enter persist state? try to get a window update. 2084 } else 2085 sendWindow -= consumedWindow; 2086 2087 uint32 length = min_c(fSendQueue.Available(fSendNext), sendWindow); 2088 bool shouldStartRetransmitTimer = fSendNext == fSendUnacknowledged; 2089 bool retransmit = fSendNext < fSendMax; 2090 2091 if (fDuplicateAcknowledgeCount != 0) { 2092 // send at most 1 SMSS of data when under limited transmit, fast transmit/recovery 2093 length = min_c(length, fSendMaxSegmentSize); 2094 } 2095 2096 do { 2097 uint32 segmentMaxSize = fSendMaxSegmentSize 2098 - tcp_options_length(segment); 2099 uint32 segmentLength = min_c(length, segmentMaxSize); 2100 2101 if (fSendNext + segmentLength == fSendQueue.LastSequence() && !force) { 2102 if (state_needs_finish(fState)) 2103 segment.flags |= TCP_FLAG_FINISH; 2104 if (length > 0) 2105 segment.flags |= TCP_FLAG_PUSH; 2106 } 2107 2108 // Determine if we should really send this segment 2109 if (!force && !retransmit && !_ShouldSendSegment(segment, segmentLength, 2110 segmentMaxSize, flightSize)) { 2111 if (fSendQueue.Available() 2112 && !gStackModule->is_timer_active(&fPersistTimer) 2113 && !gStackModule->is_timer_active(&fRetransmitTimer)) 2114 _StartPersistTimer(); 2115 break; 2116 } 2117 2118 net_buffer *buffer = gBufferModule->create(256); 2119 if (buffer == NULL) 2120 return B_NO_MEMORY; 2121 2122 status_t status = B_OK; 2123 if (segmentLength > 0) 2124 status = fSendQueue.Get(buffer, fSendNext, segmentLength); 2125 if (status < B_OK) { 2126 gBufferModule->free(buffer); 2127 return status; 2128 } 2129 2130 LocalAddress().CopyTo(buffer->source); 2131 PeerAddress().CopyTo(buffer->destination); 2132 2133 uint32 size = buffer->size; 2134 segment.sequence = fSendNext.Number(); 2135 2136 TRACE("SendQueued(): buffer %p (%" B_PRIu32 " bytes) address %s to " 2137 "%s flags %#" B_PRIx8 ", seq %" B_PRIu32 ", ack %" B_PRIu32 2138 ", rwnd %" B_PRIu16 ", cwnd %" B_PRIu32 ", ssthresh %" B_PRIu32 2139 ", len %" B_PRIu32 ", first %" B_PRIu32 ", last %" B_PRIu32, 2140 buffer, buffer->size, PrintAddress(buffer->source), 2141 PrintAddress(buffer->destination), segment.flags, segment.sequence, 2142 segment.acknowledge, segment.advertised_window, 2143 fCongestionWindow, fSlowStartThreshold, segmentLength, 2144 fSendQueue.FirstSequence().Number(), 2145 fSendQueue.LastSequence().Number()); 2146 T(Send(this, segment, buffer, fSendQueue.FirstSequence(), 2147 fSendQueue.LastSequence())); 2148 2149 PROBE(buffer, sendWindow); 2150 sendWindow -= buffer->size; 2151 2152 status = add_tcp_header(AddressModule(), segment, buffer); 2153 if (status != B_OK) { 2154 gBufferModule->free(buffer); 2155 return status; 2156 } 2157 2158 // Update send status - we need to do this before we send the data 2159 // for local connections as the answer is directly handled 2160 2161 if (segment.flags & TCP_FLAG_SYNCHRONIZE) { 2162 segment.options &= ~TCP_HAS_WINDOW_SCALE; 2163 segment.max_segment_size = 0; 2164 size++; 2165 } 2166 2167 if (segment.flags & TCP_FLAG_FINISH) 2168 size++; 2169 2170 uint32 sendMax = fSendMax.Number(); 2171 fSendNext += size; 2172 if (fSendMax < fSendNext) 2173 fSendMax = fSendNext; 2174 2175 fReceiveMaxAdvertised = fReceiveNext 2176 + ((uint32)segment.advertised_window << fReceiveWindowShift); 2177 2178 if (segmentLength != 0 && fState == ESTABLISHED) 2179 --fSendMaxSegments; 2180 2181 status = next->module->send_routed_data(next, fRoute, buffer); 2182 if (status < B_OK) { 2183 gBufferModule->free(buffer); 2184 2185 fSendNext = segment.sequence; 2186 fSendMax = sendMax; 2187 // restore send status 2188 return status; 2189 } 2190 2191 if (fSendTime == 0 && !retransmit 2192 && (segmentLength != 0 || (segment.flags & TCP_FLAG_SYNCHRONIZE) !=0)) { 2193 fSendTime = tcp_now(); 2194 fRoundTripStartSequence = segment.sequence; 2195 } 2196 2197 if (shouldStartRetransmitTimer && size > 0) { 2198 TRACE("starting initial retransmit timer of: %" B_PRIdBIGTIME, 2199 fRetransmitTimeout); 2200 gStackModule->set_timer(&fRetransmitTimer, fRetransmitTimeout); 2201 T(TimerSet(this, "retransmit", fRetransmitTimeout)); 2202 shouldStartRetransmitTimer = false; 2203 } 2204 2205 if (segment.flags & TCP_FLAG_ACKNOWLEDGE) 2206 fLastAcknowledgeSent = segment.acknowledge; 2207 2208 length -= segmentLength; 2209 segment.flags &= ~(TCP_FLAG_SYNCHRONIZE | TCP_FLAG_RESET 2210 | TCP_FLAG_FINISH); 2211 2212 if (retransmit) 2213 break; 2214 2215 } while (length > 0); 2216 2217 return B_OK; 2218 } 2219 2220 2221 int 2222 TCPEndpoint::_MaxSegmentSize(const sockaddr* address) const 2223 { 2224 return next->module->get_mtu(next, address) - sizeof(tcp_header); 2225 } 2226 2227 2228 status_t 2229 TCPEndpoint::_PrepareSendPath(const sockaddr* peer) 2230 { 2231 if (fRoute == NULL) { 2232 fRoute = gDatalinkModule->get_route(Domain(), peer); 2233 if (fRoute == NULL) 2234 return ENETUNREACH; 2235 2236 if ((fRoute->flags & RTF_LOCAL) != 0) 2237 fFlags |= FLAG_LOCAL; 2238 } 2239 2240 // make sure connection does not already exist 2241 status_t status = fManager->SetConnection(this, *LocalAddress(), peer, 2242 fRoute->interface_address->local); 2243 if (status < B_OK) 2244 return status; 2245 2246 fInitialSendSequence = system_time() >> 4; 2247 fSendNext = fInitialSendSequence; 2248 fSendUnacknowledged = fInitialSendSequence; 2249 fSendMax = fInitialSendSequence; 2250 fSendUrgentOffset = fInitialSendSequence; 2251 fRecover = fInitialSendSequence.Number(); 2252 2253 // we are counting the SYN here 2254 fSendQueue.SetInitialSequence(fSendNext + 1); 2255 2256 fReceiveMaxSegmentSize = _MaxSegmentSize(peer); 2257 2258 // Compute the window shift we advertise to our peer - if it doesn't support 2259 // this option, this will be reset to 0 (when its SYN is received) 2260 fReceiveWindowShift = 0; 2261 while (fReceiveWindowShift < TCP_MAX_WINDOW_SHIFT 2262 && (0xffffUL << fReceiveWindowShift) < socket->receive.buffer_size) { 2263 fReceiveWindowShift++; 2264 } 2265 2266 return B_OK; 2267 } 2268 2269 2270 void 2271 TCPEndpoint::_Acknowledged(tcp_segment_header& segment) 2272 { 2273 TRACE("_Acknowledged(): ack %" B_PRIu32 "; uack %" B_PRIu32 "; next %" 2274 B_PRIu32 "; max %" B_PRIu32, segment.acknowledge, 2275 fSendUnacknowledged.Number(), fSendNext.Number(), fSendMax.Number()); 2276 2277 ASSERT(fSendUnacknowledged <= segment.acknowledge); 2278 2279 if (fSendUnacknowledged < segment.acknowledge) { 2280 fSendQueue.RemoveUntil(segment.acknowledge); 2281 2282 uint32 bytesAcknowledged = segment.acknowledge - fSendUnacknowledged.Number(); 2283 fPreviousHighestAcknowledge = fSendUnacknowledged; 2284 fSendUnacknowledged = segment.acknowledge; 2285 uint32 flightSize = (fSendMax - fSendUnacknowledged).Number(); 2286 int32 expectedSamples = flightSize / (fSendMaxSegmentSize << 1); 2287 2288 if (fPreviousHighestAcknowledge > fSendUnacknowledged) { 2289 // need to update the recover variable upon a sequence wraparound 2290 fRecover = segment.acknowledge - 1; 2291 } 2292 2293 // the acknowledgment of the SYN/ACK MUST NOT increase the size of the congestion window 2294 if (fSendUnacknowledged != fInitialSendSequence) { 2295 if (fCongestionWindow < fSlowStartThreshold) 2296 fCongestionWindow += min_c(bytesAcknowledged, fSendMaxSegmentSize); 2297 else { 2298 uint32 increment = fSendMaxSegmentSize * fSendMaxSegmentSize; 2299 2300 if (increment < fCongestionWindow) 2301 increment = 1; 2302 else 2303 increment /= fCongestionWindow; 2304 2305 fCongestionWindow += increment; 2306 } 2307 2308 fSendMaxSegments = UINT32_MAX; 2309 } 2310 2311 if ((fFlags & FLAG_RECOVERY) != 0) { 2312 fSendNext = fSendUnacknowledged; 2313 _SendQueued(); 2314 fCongestionWindow -= bytesAcknowledged; 2315 2316 if (bytesAcknowledged > fSendMaxSegmentSize) 2317 fCongestionWindow += fSendMaxSegmentSize; 2318 2319 fSendNext = fSendMax; 2320 } else 2321 fDuplicateAcknowledgeCount = 0; 2322 2323 if (fSendNext < fSendUnacknowledged) 2324 fSendNext = fSendUnacknowledged; 2325 2326 if (fFlags & FLAG_OPTION_TIMESTAMP) { 2327 _UpdateRoundTripTime(tcp_diff_timestamp(segment.timestamp_reply), 2328 expectedSamples > 0 ? expectedSamples : 1); 2329 } else if (fSendTime != 0 && fRoundTripStartSequence < segment.acknowledge) { 2330 _UpdateRoundTripTime(tcp_diff_timestamp(fSendTime), 1); 2331 fSendTime = 0; 2332 } 2333 2334 if (fSendUnacknowledged == fSendMax) { 2335 TRACE("all acknowledged, cancelling retransmission timer."); 2336 gStackModule->cancel_timer(&fRetransmitTimer); 2337 T(TimerSet(this, "retransmit", -1)); 2338 } else { 2339 TRACE("data acknowledged, resetting retransmission timer to: %" 2340 B_PRIdBIGTIME, fRetransmitTimeout); 2341 gStackModule->set_timer(&fRetransmitTimer, fRetransmitTimeout); 2342 T(TimerSet(this, "retransmit", fRetransmitTimeout)); 2343 } 2344 2345 if (is_writable(fState)) { 2346 // notify threads waiting on the socket to become writable again 2347 fSendCondition.NotifyAll(); 2348 gSocketModule->notify(socket, B_SELECT_WRITE, fSendQueue.Free()); 2349 } 2350 } 2351 2352 // if there is data left to be sent, send it now 2353 if (fSendQueue.Used() > 0) 2354 _SendQueued(); 2355 } 2356 2357 2358 void 2359 TCPEndpoint::_Retransmit() 2360 { 2361 TRACE("Retransmit()"); 2362 2363 if (fState < ESTABLISHED) { 2364 fRetransmitTimeout = TCP_SYN_RETRANSMIT_TIMEOUT; 2365 fCongestionWindow = fSendMaxSegmentSize; 2366 } else { 2367 _ResetSlowStart(); 2368 fDuplicateAcknowledgeCount = 0; 2369 // Do exponential back off of the retransmit timeout 2370 fRetransmitTimeout *= 2; 2371 if (fRetransmitTimeout > TCP_MAX_RETRANSMIT_TIMEOUT) 2372 fRetransmitTimeout = TCP_MAX_RETRANSMIT_TIMEOUT; 2373 } 2374 2375 fSendNext = fSendUnacknowledged; 2376 _SendQueued(); 2377 2378 fRecover = fSendNext.Number() - 1; 2379 if ((fFlags & FLAG_RECOVERY) != 0) 2380 fFlags &= ~FLAG_RECOVERY; 2381 } 2382 2383 2384 void 2385 TCPEndpoint::_UpdateRoundTripTime(int32 roundTripTime, int32 expectedSamples) 2386 { 2387 if (fSmoothedRoundTripTime == 0) { 2388 fSmoothedRoundTripTime = roundTripTime; 2389 fRoundTripVariation = roundTripTime / 2; 2390 fRetransmitTimeout = (fSmoothedRoundTripTime + max_c(100, fRoundTripVariation * 4)) 2391 * kTimestampFactor; 2392 } else { 2393 int32 delta = fSmoothedRoundTripTime - roundTripTime; 2394 if (delta < 0) 2395 delta = -delta; 2396 fRoundTripVariation += (delta - fRoundTripVariation) / (expectedSamples * 4); 2397 fSmoothedRoundTripTime += (roundTripTime - fSmoothedRoundTripTime) / (expectedSamples * 8); 2398 fRetransmitTimeout = (fSmoothedRoundTripTime + max_c(100, fRoundTripVariation * 4)) 2399 * kTimestampFactor; 2400 } 2401 2402 if (fRetransmitTimeout > TCP_MAX_RETRANSMIT_TIMEOUT) 2403 fRetransmitTimeout = TCP_MAX_RETRANSMIT_TIMEOUT; 2404 2405 if (fRetransmitTimeout < TCP_MIN_RETRANSMIT_TIMEOUT) 2406 fRetransmitTimeout = TCP_MIN_RETRANSMIT_TIMEOUT; 2407 2408 TRACE(" RTO is now %" B_PRIdBIGTIME " (after rtt %" B_PRId32 "ms)", 2409 fRetransmitTimeout, roundTripTime); 2410 } 2411 2412 2413 void 2414 TCPEndpoint::_ResetSlowStart() 2415 { 2416 fSlowStartThreshold = max_c((fSendMax - fSendUnacknowledged).Number() / 2, 2417 2 * fSendMaxSegmentSize); 2418 fCongestionWindow = fSendMaxSegmentSize; 2419 } 2420 2421 2422 // #pragma mark - timer 2423 2424 2425 /*static*/ void 2426 TCPEndpoint::_RetransmitTimer(net_timer* timer, void* _endpoint) 2427 { 2428 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2429 T(TimerTriggered(endpoint, "retransmit")); 2430 2431 MutexLocker locker(endpoint->fLock); 2432 if (!locker.IsLocked() || gStackModule->is_timer_active(timer)) 2433 return; 2434 2435 endpoint->_Retransmit(); 2436 } 2437 2438 2439 /*static*/ void 2440 TCPEndpoint::_PersistTimer(net_timer* timer, void* _endpoint) 2441 { 2442 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2443 T(TimerTriggered(endpoint, "persist")); 2444 2445 MutexLocker locker(endpoint->fLock); 2446 if (!locker.IsLocked()) 2447 return; 2448 2449 // the timer might not have been canceled early enough 2450 if (endpoint->State() == CLOSED) 2451 return; 2452 2453 endpoint->_SendQueued(true); 2454 } 2455 2456 2457 /*static*/ void 2458 TCPEndpoint::_DelayedAcknowledgeTimer(net_timer* timer, void* _endpoint) 2459 { 2460 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2461 T(TimerTriggered(endpoint, "delayed ack")); 2462 2463 MutexLocker locker(endpoint->fLock); 2464 if (!locker.IsLocked()) 2465 return; 2466 2467 // the timer might not have been canceled early enough 2468 if (endpoint->State() == CLOSED) 2469 return; 2470 2471 endpoint->SendAcknowledge(true); 2472 } 2473 2474 2475 /*static*/ void 2476 TCPEndpoint::_TimeWaitTimer(net_timer* timer, void* _endpoint) 2477 { 2478 TCPEndpoint* endpoint = (TCPEndpoint*)_endpoint; 2479 T(TimerTriggered(endpoint, "time-wait")); 2480 2481 MutexLocker locker(endpoint->fLock); 2482 if (!locker.IsLocked()) 2483 return; 2484 2485 if ((endpoint->fFlags & FLAG_CLOSED) == 0) { 2486 endpoint->fFlags |= FLAG_DELETE_ON_CLOSE; 2487 return; 2488 } 2489 2490 locker.Unlock(); 2491 2492 gSocketModule->release_socket(endpoint->socket); 2493 } 2494 2495 2496 /*static*/ status_t 2497 TCPEndpoint::_WaitForCondition(ConditionVariable& condition, 2498 MutexLocker& locker, bigtime_t timeout) 2499 { 2500 ConditionVariableEntry entry; 2501 condition.Add(&entry); 2502 2503 locker.Unlock(); 2504 status_t result = entry.Wait(B_ABSOLUTE_TIMEOUT | B_CAN_INTERRUPT, timeout); 2505 locker.Lock(); 2506 2507 return result; 2508 } 2509 2510 2511 // #pragma mark - 2512 2513 2514 void 2515 TCPEndpoint::Dump() const 2516 { 2517 kprintf("TCP endpoint %p\n", this); 2518 kprintf(" state: %s\n", name_for_state(fState)); 2519 kprintf(" flags: 0x%" B_PRIx32 "\n", fFlags); 2520 #if KDEBUG 2521 kprintf(" lock: { %p, holder: %" B_PRId32 " }\n", &fLock, fLock.holder); 2522 #endif 2523 kprintf(" accept sem: %" B_PRId32 "\n", fAcceptSemaphore); 2524 kprintf(" options: 0x%" B_PRIx32 "\n", (uint32)fOptions); 2525 kprintf(" send\n"); 2526 kprintf(" window shift: %" B_PRIu8 "\n", fSendWindowShift); 2527 kprintf(" unacknowledged: %" B_PRIu32 "\n", 2528 fSendUnacknowledged.Number()); 2529 kprintf(" next: %" B_PRIu32 "\n", fSendNext.Number()); 2530 kprintf(" max: %" B_PRIu32 "\n", fSendMax.Number()); 2531 kprintf(" urgent offset: %" B_PRIu32 "\n", fSendUrgentOffset.Number()); 2532 kprintf(" window: %" B_PRIu32 "\n", fSendWindow); 2533 kprintf(" max window: %" B_PRIu32 "\n", fSendMaxWindow); 2534 kprintf(" max segment size: %" B_PRIu32 "\n", fSendMaxSegmentSize); 2535 kprintf(" queue: %" B_PRIuSIZE " / %" B_PRIuSIZE "\n", fSendQueue.Used(), 2536 fSendQueue.Size()); 2537 #if DEBUG_BUFFER_QUEUE 2538 fSendQueue.Dump(); 2539 #endif 2540 kprintf(" last acknowledge sent: %" B_PRIu32 "\n", 2541 fLastAcknowledgeSent.Number()); 2542 kprintf(" initial sequence: %" B_PRIu32 "\n", 2543 fInitialSendSequence.Number()); 2544 kprintf(" receive\n"); 2545 kprintf(" window shift: %" B_PRIu8 "\n", fReceiveWindowShift); 2546 kprintf(" next: %" B_PRIu32 "\n", fReceiveNext.Number()); 2547 kprintf(" max advertised: %" B_PRIu32 "\n", 2548 fReceiveMaxAdvertised.Number()); 2549 kprintf(" window: %" B_PRIu32 "\n", fReceiveWindow); 2550 kprintf(" max segment size: %" B_PRIu32 "\n", fReceiveMaxSegmentSize); 2551 kprintf(" queue: %" B_PRIuSIZE " / %" B_PRIuSIZE "\n", 2552 fReceiveQueue.Available(), fReceiveQueue.Size()); 2553 #if DEBUG_BUFFER_QUEUE 2554 fReceiveQueue.Dump(); 2555 #endif 2556 kprintf(" initial sequence: %" B_PRIu32 "\n", 2557 fInitialReceiveSequence.Number()); 2558 kprintf(" duplicate acknowledge count: %" B_PRIu32 "\n", 2559 fDuplicateAcknowledgeCount); 2560 kprintf(" smoothed round trip time: %" B_PRId32 " (deviation %" B_PRId32 ")\n", 2561 fSmoothedRoundTripTime, fRoundTripVariation); 2562 kprintf(" retransmit timeout: %" B_PRId64 "\n", fRetransmitTimeout); 2563 kprintf(" congestion window: %" B_PRIu32 "\n", fCongestionWindow); 2564 kprintf(" slow start threshold: %" B_PRIu32 "\n", fSlowStartThreshold); 2565 } 2566 2567