1 /* 2 * Copyright 2006-2009, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler, axeld@pinc-software.de 7 * Andrew Galante, haiku.galante@gmail.com 8 * Hugo Santos, hugosantos@gmail.com 9 */ 10 11 12 #include "EndpointManager.h" 13 #include "TCPEndpoint.h" 14 #include "tcp.h" 15 16 #include <net_protocol.h> 17 #include <net_stat.h> 18 19 #include <KernelExport.h> 20 #include <util/list.h> 21 22 #include <netinet/in.h> 23 #include <netinet/ip.h> 24 #include <new> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include <lock.h> 29 #include <util/AutoLock.h> 30 31 #include <NetBufferUtilities.h> 32 #include <NetUtilities.h> 33 34 35 //#define TRACE_TCP 36 #ifdef TRACE_TCP 37 # define TRACE(x) dprintf x 38 #else 39 # define TRACE(x) 40 #endif 41 42 43 typedef NetBufferField<uint16, offsetof(tcp_header, checksum)> TCPChecksumField; 44 45 46 net_buffer_module_info *gBufferModule; 47 net_datalink_module_info *gDatalinkModule; 48 net_socket_module_info *gSocketModule; 49 net_stack_module_info *gStackModule; 50 51 52 static EndpointManager* sEndpointManagers[AF_MAX]; 53 static rw_lock sEndpointManagersLock; 54 55 56 // The TCP header length is at most 60 bytes (0xf * 4). 57 static const int kMaxOptionSize = 60 - sizeof(tcp_header); 58 59 60 /*! Returns an endpoint manager for the specified domain, if any. 61 You need to hold the sEndpointManagersLock when calling this function. 62 */ 63 static inline EndpointManager* 64 endpoint_manager_for_locked(int family) 65 { 66 if (family >= AF_MAX || family < 0) 67 return NULL; 68 69 return sEndpointManagers[family]; 70 } 71 72 73 /*! Returns an endpoint manager for the specified domain, if any */ 74 static inline EndpointManager* 75 endpoint_manager_for(net_domain* domain) 76 { 77 ReadLocker _(sEndpointManagersLock); 78 79 return endpoint_manager_for_locked(domain->family); 80 } 81 82 83 static inline void 84 bump_option(tcp_option *&option, size_t &length) 85 { 86 if (option->kind <= TCP_OPTION_NOP) { 87 length++; 88 option = (tcp_option *)((uint8 *)option + 1); 89 } else { 90 length += option->length; 91 option = (tcp_option *)((uint8 *)option + option->length); 92 } 93 } 94 95 96 static inline size_t 97 add_options(tcp_segment_header &segment, uint8 *buffer, size_t bufferSize) 98 { 99 // Some network devices can be very sensitive to the ordering of TCP options 100 // https://github.com/torvalds/linux/blob/9e9fb7655ed585da8f468e29221f0ba194a5f613/net/ipv4/tcp_output.c#L598 101 102 tcp_option *option = (tcp_option *)buffer; 103 size_t length = 0; 104 105 if (segment.max_segment_size > 0 && length + 8 <= bufferSize) { 106 option->kind = TCP_OPTION_MAX_SEGMENT_SIZE; 107 option->length = 4; 108 option->max_segment_size = htons(segment.max_segment_size); 109 bump_option(option, length); 110 } 111 112 if ((segment.options & TCP_HAS_TIMESTAMPS) != 0 113 && length + 12 <= bufferSize) { 114 if ((segment.options & TCP_SACK_PERMITTED) != 0) { 115 // combine with timestamp 116 option->kind = TCP_OPTION_SACK_PERMITTED; 117 option->length = 2; 118 bump_option(option, length); 119 } else { 120 // two NOPs so the timestamps get aligned to a 4 byte boundary 121 option->kind = TCP_OPTION_NOP; 122 bump_option(option, length); 123 option->kind = TCP_OPTION_NOP; 124 bump_option(option, length); 125 } 126 option->kind = TCP_OPTION_TIMESTAMP; 127 option->length = 10; 128 option->timestamp.value = htonl(segment.timestamp_value); 129 option->timestamp.reply = htonl(segment.timestamp_reply); 130 bump_option(option, length); 131 } else if ((segment.options & TCP_SACK_PERMITTED) != 0 132 && length + 4 <= bufferSize) { 133 // two NOPs so that the subsequent data is aligned on a 4 byte boundary 134 option->kind = TCP_OPTION_NOP; 135 bump_option(option, length); 136 option->kind = TCP_OPTION_NOP; 137 bump_option(option, length); 138 option->kind = TCP_OPTION_SACK_PERMITTED; 139 option->length = 2; 140 bump_option(option, length); 141 } 142 143 if ((segment.options & TCP_HAS_WINDOW_SCALE) != 0 144 && length + 4 <= bufferSize) { 145 // one NOP so that the subsequent data is aligned on a 4 byte boundary 146 option->kind = TCP_OPTION_NOP; 147 bump_option(option, length); 148 149 option->kind = TCP_OPTION_WINDOW_SHIFT; 150 option->length = 3; 151 option->window_shift = segment.window_shift; 152 bump_option(option, length); 153 } 154 155 if (segment.sackCount > 0) { 156 int sackCount = ((int)(bufferSize - length) - 4) / sizeof(tcp_sack); 157 if (sackCount > segment.sackCount) 158 sackCount = segment.sackCount; 159 160 if (sackCount > 0) { 161 option->kind = TCP_OPTION_NOP; 162 bump_option(option, length); 163 option->kind = TCP_OPTION_NOP; 164 bump_option(option, length); 165 option->kind = TCP_OPTION_SACK; 166 option->length = 2 + sackCount * sizeof(tcp_sack); 167 memcpy(option->sack, segment.sacks, sackCount * sizeof(tcp_sack)); 168 bump_option(option, length); 169 } 170 } 171 172 if ((length & 3) == 0) { 173 // options completely fill out the option space 174 return length; 175 } 176 177 option->kind = TCP_OPTION_END; 178 return (length + 3) & ~3; 179 // bump to a multiple of 4 length 180 } 181 182 183 static void 184 process_options(tcp_segment_header &segment, net_buffer *buffer, size_t size) 185 { 186 if (size == 0) 187 return; 188 189 tcp_option *option; 190 191 uint8 optionsBuffer[kMaxOptionSize]; 192 if (gBufferModule->direct_access(buffer, sizeof(tcp_header), size, 193 (void **)&option) != B_OK) { 194 if ((size_t)size > sizeof(optionsBuffer)) { 195 dprintf("Ignoring TCP options larger than expected.\n"); 196 return; 197 } 198 199 gBufferModule->read(buffer, sizeof(tcp_header), optionsBuffer, size); 200 option = (tcp_option *)optionsBuffer; 201 } 202 203 while (size > 0) { 204 int32 length = -1; 205 206 switch (option->kind) { 207 case TCP_OPTION_END: 208 case TCP_OPTION_NOP: 209 length = 1; 210 break; 211 case TCP_OPTION_MAX_SEGMENT_SIZE: 212 if (option->length == 4 && size >= 4) 213 segment.max_segment_size = ntohs(option->max_segment_size); 214 break; 215 case TCP_OPTION_WINDOW_SHIFT: 216 if (option->length == 3 && size >= 3) { 217 segment.options |= TCP_HAS_WINDOW_SCALE; 218 segment.window_shift = option->window_shift; 219 } 220 break; 221 case TCP_OPTION_TIMESTAMP: 222 if (option->length == 10 && size >= 10) { 223 segment.options |= TCP_HAS_TIMESTAMPS; 224 segment.timestamp_value = ntohl(option->timestamp.value); 225 segment.timestamp_reply = 226 ntohl(option->timestamp.reply); 227 } 228 break; 229 case TCP_OPTION_SACK_PERMITTED: 230 if (option->length == 2 && size >= 2) 231 segment.options |= TCP_SACK_PERMITTED; 232 break; 233 case TCP_OPTION_SACK: 234 if (size >= option->length) { 235 segment.options |= TCP_HAS_SACK; 236 segment.sackCount = min_c((option->length - 2) 237 / sizeof(tcp_sack), MAX_SACK_BLKS); 238 for(int i = 0; i < segment.sackCount; ++i) { 239 segment.sacks[i].left_edge = ntohl( 240 option->sack[i].left_edge); 241 segment.sacks[i].right_edge = ntohl( 242 option->sack[i].right_edge); 243 } 244 } 245 break; 246 } 247 248 if (length < 0) { 249 length = option->length; 250 if (length == 0 || length > (ssize_t)size) 251 break; 252 } 253 254 option = (tcp_option *)((uint8 *)option + length); 255 size -= length; 256 } 257 } 258 259 260 #if 0 261 static void 262 dump_tcp_header(tcp_header &header) 263 { 264 dprintf(" source port: %u\n", ntohs(header.source_port)); 265 dprintf(" dest port: %u\n", ntohs(header.destination_port)); 266 dprintf(" sequence: %lu\n", header.Sequence()); 267 dprintf(" ack: %lu\n", header.Acknowledge()); 268 dprintf(" flags: %s%s%s%s%s%s\n", (header.flags & TCP_FLAG_FINISH) ? "FIN " : "", 269 (header.flags & TCP_FLAG_SYNCHRONIZE) ? "SYN " : "", 270 (header.flags & TCP_FLAG_RESET) ? "RST " : "", 271 (header.flags & TCP_FLAG_PUSH) ? "PUSH " : "", 272 (header.flags & TCP_FLAG_ACKNOWLEDGE) ? "ACK " : "", 273 (header.flags & TCP_FLAG_URGENT) ? "URG " : ""); 274 dprintf(" window: %u\n", header.AdvertisedWindow()); 275 dprintf(" urgent offset: %u\n", header.UrgentOffset()); 276 } 277 #endif 278 279 280 static int 281 dump_endpoints(int argc, char** argv) 282 { 283 for (int i = 0; i < AF_MAX; i++) { 284 EndpointManager* manager = sEndpointManagers[i]; 285 if (manager != NULL) 286 manager->Dump(); 287 } 288 289 return 0; 290 } 291 292 293 static int 294 dump_endpoint(int argc, char** argv) 295 { 296 if (argc < 2) { 297 kprintf("usage: tcp_endpoint [address]\n"); 298 return 0; 299 } 300 301 TCPEndpoint* endpoint = (TCPEndpoint*)parse_expression(argv[1]); 302 endpoint->Dump(); 303 304 return 0; 305 } 306 307 308 // #pragma mark - internal API 309 310 311 /*! Creates a new endpoint manager for the specified domain, or returns 312 an existing one for this domain. 313 */ 314 EndpointManager* 315 get_endpoint_manager(net_domain* domain) 316 { 317 // See if there is one already 318 EndpointManager* endpointManager = endpoint_manager_for(domain); 319 if (endpointManager != NULL) 320 return endpointManager; 321 322 WriteLocker _(sEndpointManagersLock); 323 324 endpointManager = endpoint_manager_for_locked(domain->family); 325 if (endpointManager != NULL) 326 return endpointManager; 327 328 // There is no endpoint manager for this domain yet, so we need 329 // to create one. 330 331 endpointManager = new(std::nothrow) EndpointManager(domain); 332 if (endpointManager == NULL) 333 return NULL; 334 335 if (endpointManager->Init() != B_OK) { 336 delete endpointManager; 337 return NULL; 338 } 339 340 sEndpointManagers[domain->family] = endpointManager; 341 return endpointManager; 342 } 343 344 345 void 346 put_endpoint_manager(EndpointManager* endpointManager) 347 { 348 // TODO: we may want to use reference counting instead of only discarding 349 // them on unload. But since there is likely only IPv4/v6 there is not much 350 // point to it. 351 } 352 353 354 const char* 355 name_for_state(tcp_state state) 356 { 357 switch (state) { 358 case CLOSED: 359 return "closed"; 360 case LISTEN: 361 return "listen"; 362 case SYNCHRONIZE_SENT: 363 return "syn-sent"; 364 case SYNCHRONIZE_RECEIVED: 365 return "syn-received"; 366 case ESTABLISHED: 367 return "established"; 368 369 // peer closes the connection 370 case FINISH_RECEIVED: 371 return "close-wait"; 372 case WAIT_FOR_FINISH_ACKNOWLEDGE: 373 return "last-ack"; 374 375 // we close the connection 376 case FINISH_SENT: 377 return "fin-wait1"; 378 case FINISH_ACKNOWLEDGED: 379 return "fin-wait2"; 380 case CLOSING: 381 return "closing"; 382 383 case TIME_WAIT: 384 return "time-wait"; 385 } 386 387 return "-"; 388 } 389 390 391 /*! Constructs a TCP header on \a buffer with the specified values 392 for \a flags, \a seq \a ack and \a advertisedWindow. 393 */ 394 status_t 395 add_tcp_header(net_address_module_info* addressModule, 396 tcp_segment_header& segment, net_buffer* buffer) 397 { 398 buffer->protocol = IPPROTO_TCP; 399 400 uint8 optionsBuffer[kMaxOptionSize]; 401 uint32 optionsLength = add_options(segment, optionsBuffer, 402 sizeof(optionsBuffer)); 403 404 NetBufferPrepend<tcp_header> bufferHeader(buffer, 405 sizeof(tcp_header) + optionsLength); 406 if (bufferHeader.Status() != B_OK) 407 return bufferHeader.Status(); 408 409 tcp_header& header = bufferHeader.Data(); 410 411 header.source_port = addressModule->get_port(buffer->source); 412 header.destination_port = addressModule->get_port(buffer->destination); 413 header.sequence = htonl(segment.sequence); 414 header.acknowledge = (segment.flags & TCP_FLAG_ACKNOWLEDGE) 415 ? htonl(segment.acknowledge) : 0; 416 header.reserved = 0; 417 header.header_length = (sizeof(tcp_header) + optionsLength) >> 2; 418 header.flags = segment.flags; 419 header.advertised_window = htons(segment.advertised_window); 420 header.checksum = 0; 421 header.urgent_offset = htons(segment.urgent_offset); 422 423 // we must detach before calculating the checksum as we may 424 // not have a contiguous buffer. 425 bufferHeader.Sync(); 426 427 if (optionsLength > 0) { 428 gBufferModule->write(buffer, sizeof(tcp_header), optionsBuffer, 429 optionsLength); 430 } 431 432 TRACE(("add_tcp_header(): buffer %p, flags 0x%x, seq %" B_PRIu32 ", ack %" B_PRIu32 ", up %u, " 433 "win %u\n", buffer, segment.flags, segment.sequence, 434 segment.acknowledge, segment.urgent_offset, segment.advertised_window)); 435 436 *TCPChecksumField(buffer) = Checksum::PseudoHeader(addressModule, 437 gBufferModule, buffer, IPPROTO_TCP); 438 439 return B_OK; 440 } 441 442 443 size_t 444 tcp_options_length(tcp_segment_header& segment) 445 { 446 size_t length = 0; 447 448 if (segment.max_segment_size > 0) 449 length += 4; 450 451 if ((segment.options & TCP_HAS_TIMESTAMPS) != 0) 452 length += 12; 453 else if ((segment.options & TCP_SACK_PERMITTED) != 0) 454 length += 4; 455 456 if ((segment.options & TCP_HAS_WINDOW_SCALE) != 0) 457 length += 4; 458 459 if (segment.sackCount > 0) { 460 int sackCount = min_c((int)((kMaxOptionSize - length - 4) 461 / sizeof(tcp_sack)), segment.sackCount); 462 if (sackCount > 0) 463 length += 4 + sackCount * sizeof(tcp_sack); 464 } 465 466 if ((length & 3) == 0) 467 return length; 468 469 return (length + 3) & ~3; 470 } 471 472 473 // #pragma mark - protocol API 474 475 476 net_protocol* 477 tcp_init_protocol(net_socket* socket) 478 { 479 socket->send.buffer_size = 32768; 480 // override net_socket default 481 482 TCPEndpoint* protocol = new (std::nothrow) TCPEndpoint(socket); 483 if (protocol == NULL) 484 return NULL; 485 486 if (protocol->InitCheck() != B_OK) { 487 delete protocol; 488 return NULL; 489 } 490 491 TRACE(("Creating new TCPEndpoint: %p\n", protocol)); 492 socket->protocol = IPPROTO_TCP; 493 return protocol; 494 } 495 496 497 status_t 498 tcp_uninit_protocol(net_protocol* protocol) 499 { 500 TRACE(("Deleting TCPEndpoint: %p\n", protocol)); 501 delete (TCPEndpoint*)protocol; 502 return B_OK; 503 } 504 505 506 status_t 507 tcp_open(net_protocol* protocol) 508 { 509 return ((TCPEndpoint*)protocol)->Open(); 510 } 511 512 513 status_t 514 tcp_close(net_protocol* protocol) 515 { 516 return ((TCPEndpoint*)protocol)->Close(); 517 } 518 519 520 status_t 521 tcp_free(net_protocol* protocol) 522 { 523 ((TCPEndpoint*)protocol)->Free(); 524 return B_OK; 525 } 526 527 528 status_t 529 tcp_connect(net_protocol* protocol, const struct sockaddr* address) 530 { 531 return ((TCPEndpoint*)protocol)->Connect(address); 532 } 533 534 535 status_t 536 tcp_accept(net_protocol* protocol, struct net_socket** _acceptedSocket) 537 { 538 return ((TCPEndpoint*)protocol)->Accept(_acceptedSocket); 539 } 540 541 542 status_t 543 tcp_control(net_protocol* _protocol, int level, int option, void* value, 544 size_t* _length) 545 { 546 TCPEndpoint* protocol = (TCPEndpoint*)_protocol; 547 548 if ((level & LEVEL_MASK) == IPPROTO_TCP) { 549 if (option == NET_STAT_SOCKET) 550 return protocol->FillStat((net_stat*)value); 551 } 552 553 return protocol->next->module->control(protocol->next, level, option, 554 value, _length); 555 } 556 557 558 status_t 559 tcp_getsockopt(net_protocol* _protocol, int level, int option, void* value, 560 int* _length) 561 { 562 TCPEndpoint* protocol = (TCPEndpoint*)_protocol; 563 564 if (level == IPPROTO_TCP) 565 return protocol->GetOption(option, value, _length); 566 567 return protocol->next->module->getsockopt(protocol->next, level, option, 568 value, _length); 569 } 570 571 572 status_t 573 tcp_setsockopt(net_protocol* _protocol, int level, int option, 574 const void* _value, int length) 575 { 576 TCPEndpoint* protocol = (TCPEndpoint*)_protocol; 577 578 if (level == SOL_SOCKET) { 579 if (option == SO_SNDBUF || option == SO_RCVBUF) { 580 if (length != sizeof(int)) 581 return B_BAD_VALUE; 582 583 status_t status; 584 const int* value = (const int*)_value; 585 586 if (option == SO_SNDBUF) 587 status = protocol->SetSendBufferSize(*value); 588 else 589 status = protocol->SetReceiveBufferSize(*value); 590 591 if (status < B_OK) 592 return status; 593 } 594 } else if (level == IPPROTO_TCP) 595 return protocol->SetOption(option, _value, length); 596 597 return protocol->next->module->setsockopt(protocol->next, level, option, 598 _value, length); 599 } 600 601 602 status_t 603 tcp_bind(net_protocol* protocol, const struct sockaddr* address) 604 { 605 return ((TCPEndpoint*)protocol)->Bind(address); 606 } 607 608 609 status_t 610 tcp_unbind(net_protocol* protocol, struct sockaddr* address) 611 { 612 return ((TCPEndpoint*)protocol)->Unbind(address); 613 } 614 615 616 status_t 617 tcp_listen(net_protocol* protocol, int count) 618 { 619 return ((TCPEndpoint*)protocol)->Listen(count); 620 } 621 622 623 status_t 624 tcp_shutdown(net_protocol* protocol, int direction) 625 { 626 return ((TCPEndpoint*)protocol)->Shutdown(direction); 627 } 628 629 630 status_t 631 tcp_send_data(net_protocol* protocol, net_buffer* buffer) 632 { 633 return ((TCPEndpoint*)protocol)->SendData(buffer); 634 } 635 636 637 status_t 638 tcp_send_routed_data(net_protocol* protocol, struct net_route* route, 639 net_buffer* buffer) 640 { 641 // TCP never sends routed data 642 return B_ERROR; 643 } 644 645 646 ssize_t 647 tcp_send_avail(net_protocol* protocol) 648 { 649 return ((TCPEndpoint*)protocol)->SendAvailable(); 650 } 651 652 653 status_t 654 tcp_read_data(net_protocol* protocol, size_t numBytes, uint32 flags, 655 net_buffer** _buffer) 656 { 657 return ((TCPEndpoint*)protocol)->ReadData(numBytes, flags, _buffer); 658 } 659 660 661 ssize_t 662 tcp_read_avail(net_protocol* protocol) 663 { 664 return ((TCPEndpoint*)protocol)->ReadAvailable(); 665 } 666 667 668 struct net_domain* 669 tcp_get_domain(net_protocol* protocol) 670 { 671 return protocol->next->module->get_domain(protocol->next); 672 } 673 674 675 size_t 676 tcp_get_mtu(net_protocol* protocol, const struct sockaddr* address) 677 { 678 return protocol->next->module->get_mtu(protocol->next, address); 679 } 680 681 682 status_t 683 tcp_receive_data(net_buffer* buffer) 684 { 685 TRACE(("TCP: Received buffer %p\n", buffer)); 686 687 if (buffer->interface_address == NULL 688 || buffer->interface_address->domain == NULL) 689 return B_ERROR; 690 691 net_domain* domain = buffer->interface_address->domain; 692 net_address_module_info* addressModule = domain->address_module; 693 694 NetBufferHeaderReader<tcp_header> bufferHeader(buffer); 695 if (bufferHeader.Status() < B_OK) 696 return bufferHeader.Status(); 697 698 tcp_header& header = bufferHeader.Data(); 699 700 uint16 headerLength = header.HeaderLength(); 701 if (headerLength < sizeof(tcp_header)) 702 return B_BAD_DATA; 703 704 if (Checksum::PseudoHeader(addressModule, gBufferModule, buffer, 705 IPPROTO_TCP) != 0) 706 return B_BAD_DATA; 707 708 addressModule->set_port(buffer->source, header.source_port); 709 addressModule->set_port(buffer->destination, header.destination_port); 710 711 TRACE((" Looking for: peer %s, local %s\n", 712 AddressString(domain, buffer->source, true).Data(), 713 AddressString(domain, buffer->destination, true).Data())); 714 //dump_tcp_header(header); 715 //gBufferModule->dump(buffer); 716 717 tcp_segment_header segment(header.flags); 718 segment.sequence = header.Sequence(); 719 segment.acknowledge = header.Acknowledge(); 720 segment.advertised_window = header.AdvertisedWindow(); 721 segment.urgent_offset = header.UrgentOffset(); 722 process_options(segment, buffer, headerLength - sizeof(tcp_header)); 723 724 bufferHeader.Remove(headerLength); 725 // we no longer need to keep the header around 726 727 EndpointManager* endpointManager = endpoint_manager_for(domain); 728 if (endpointManager == NULL) { 729 TRACE((" No endpoint manager!\n")); 730 return B_ERROR; 731 } 732 733 int32 segmentAction = DROP; 734 735 TCPEndpoint* endpoint = endpointManager->FindConnection( 736 buffer->destination, buffer->source); 737 if (endpoint != NULL) { 738 segmentAction = endpoint->SegmentReceived(segment, buffer); 739 740 // There are some states in which the socket could have been deleted 741 // while handling a segment. If this flag is set in segmentAction 742 // then we know the socket has been freed and can skip releasing 743 // the reference acquired in EndpointManager::FindConnection() 744 // above. 745 if ((segmentAction & DELETED_ENDPOINT) == 0) 746 gSocketModule->release_socket(endpoint->socket); 747 } else if ((segment.flags & TCP_FLAG_RESET) == 0) 748 segmentAction = DROP | RESET; 749 750 if ((segmentAction & RESET) != 0) { 751 // send reset 752 endpointManager->ReplyWithReset(segment, buffer); 753 } 754 if ((segmentAction & DROP) != 0) 755 gBufferModule->free(buffer); 756 757 return B_OK; 758 } 759 760 761 status_t 762 tcp_error_received(net_error error, net_buffer* data) 763 { 764 return B_ERROR; 765 } 766 767 768 status_t 769 tcp_error_reply(net_protocol* protocol, net_buffer* cause, net_error error, 770 net_error_data* errorData) 771 { 772 return B_ERROR; 773 } 774 775 776 // #pragma mark - 777 778 779 static status_t 780 tcp_init() 781 { 782 rw_lock_init(&sEndpointManagersLock, "endpoint managers"); 783 784 status_t status = gStackModule->register_domain_protocols(AF_INET, 785 SOCK_STREAM, 0, 786 "network/protocols/tcp/v1", 787 "network/protocols/ipv4/v1", 788 NULL); 789 if (status < B_OK) 790 return status; 791 status = gStackModule->register_domain_protocols(AF_INET6, 792 SOCK_STREAM, 0, 793 "network/protocols/tcp/v1", 794 "network/protocols/ipv6/v1", 795 NULL); 796 if (status < B_OK) 797 return status; 798 799 status = gStackModule->register_domain_protocols(AF_INET, SOCK_STREAM, 800 IPPROTO_TCP, 801 "network/protocols/tcp/v1", 802 "network/protocols/ipv4/v1", 803 NULL); 804 if (status < B_OK) 805 return status; 806 status = gStackModule->register_domain_protocols(AF_INET6, SOCK_STREAM, 807 IPPROTO_TCP, 808 "network/protocols/tcp/v1", 809 "network/protocols/ipv6/v1", 810 NULL); 811 if (status < B_OK) 812 return status; 813 814 status = gStackModule->register_domain_receiving_protocol(AF_INET, 815 IPPROTO_TCP, "network/protocols/tcp/v1"); 816 if (status < B_OK) 817 return status; 818 status = gStackModule->register_domain_receiving_protocol(AF_INET6, 819 IPPROTO_TCP, "network/protocols/tcp/v1"); 820 if (status < B_OK) 821 return status; 822 823 add_debugger_command("tcp_endpoints", dump_endpoints, 824 "lists all open TCP endpoints"); 825 add_debugger_command("tcp_endpoint", dump_endpoint, 826 "dumps a TCP endpoint internal state"); 827 828 return B_OK; 829 } 830 831 832 static status_t 833 tcp_uninit() 834 { 835 remove_debugger_command("tcp_endpoint", dump_endpoint); 836 remove_debugger_command("tcp_endpoints", dump_endpoints); 837 838 rw_lock_destroy(&sEndpointManagersLock); 839 840 for (int i = 0; i < AF_MAX; i++) { 841 delete sEndpointManagers[i]; 842 } 843 844 return B_OK; 845 } 846 847 848 static status_t 849 tcp_std_ops(int32 op, ...) 850 { 851 switch (op) { 852 case B_MODULE_INIT: 853 return tcp_init(); 854 855 case B_MODULE_UNINIT: 856 return tcp_uninit(); 857 858 default: 859 return B_ERROR; 860 } 861 } 862 863 864 net_protocol_module_info sTCPModule = { 865 { 866 "network/protocols/tcp/v1", 867 0, 868 tcp_std_ops 869 }, 870 0, 871 872 tcp_init_protocol, 873 tcp_uninit_protocol, 874 tcp_open, 875 tcp_close, 876 tcp_free, 877 tcp_connect, 878 tcp_accept, 879 tcp_control, 880 tcp_getsockopt, 881 tcp_setsockopt, 882 tcp_bind, 883 tcp_unbind, 884 tcp_listen, 885 tcp_shutdown, 886 tcp_send_data, 887 tcp_send_routed_data, 888 tcp_send_avail, 889 tcp_read_data, 890 tcp_read_avail, 891 tcp_get_domain, 892 tcp_get_mtu, 893 tcp_receive_data, 894 NULL, // deliver_data() 895 tcp_error_received, 896 tcp_error_reply, 897 NULL, // add_ancillary_data() 898 NULL, // process_ancillary_data() 899 NULL, // process_ancillary_data_no_container() 900 NULL, // send_data_no_buffer() 901 NULL // read_data_no_buffer() 902 }; 903 904 module_dependency module_dependencies[] = { 905 {NET_STACK_MODULE_NAME, (module_info **)&gStackModule}, 906 {NET_BUFFER_MODULE_NAME, (module_info **)&gBufferModule}, 907 {NET_DATALINK_MODULE_NAME, (module_info **)&gDatalinkModule}, 908 {NET_SOCKET_MODULE_NAME, (module_info **)&gSocketModule}, 909 {} 910 }; 911 912 module_info *modules[] = { 913 (module_info *)&sTCPModule, 914 NULL 915 }; 916