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