7 #include <ipxe/timer.h>
8 #include <ipxe/iobuf.h>
9 #include <ipxe/malloc.h>
10 #include <ipxe/init.h>
11 #include <ipxe/retry.h>
12 #include <ipxe/refcnt.h>
13 #include <ipxe/pending.h>
14 #include <ipxe/xfer.h>
15 #include <ipxe/open.h>
17 #include <ipxe/netdevice.h>
18 #include <ipxe/profile.h>
19 #include <ipxe/process.h>
20 #include <ipxe/tcpip.h>
29 FILE_LICENCE ( GPL2_OR_LATER );
31 /** A TCP connection */
32 struct tcp_connection {
33 /** Reference counter */
35 /** List of TCP connections */
36 struct list_head list;
41 /** Data transfer interface */
42 struct interface xfer;
44 /** Remote socket address */
45 struct sockaddr_tcpip peer;
47 unsigned int local_port;
48 /** Maximum segment size */
51 /** Current TCP state */
52 unsigned int tcp_state;
53 /** Previous TCP state
55 * Maintained only for debug messages
57 unsigned int prev_tcp_state;
58 /** Current sequence number
60 * Equivalent to SND.UNA in RFC 793 terminology.
63 /** Unacknowledged sequence count
65 * Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
70 * Equivalent to SND.WND in RFC 793 terminology
73 /** Current acknowledgement number
75 * Equivalent to RCV.NXT in RFC 793 terminology.
80 * Equivalent to RCV.WND in RFC 793 terminology.
83 /** Received timestamp value
85 * Updated when a packet is received; copied to ts_recent when
86 * the window is advanced.
89 /** Most recent received timestamp that advanced the window
91 * Equivalent to TS.Recent in RFC 1323 terminology.
96 * Equivalent to Snd.Wind.Scale in RFC 1323 terminology
98 uint8_t snd_win_scale;
99 /** Receive window scale
101 * Equivalent to Rcv.Wind.Scale in RFC 1323 terminology
103 uint8_t rcv_win_scale;
104 /** Maximum receive window */
105 uint32_t max_rcv_win;
107 /** Transmit queue */
108 struct list_head tx_queue;
110 struct list_head rx_queue;
111 /** Transmission process */
112 struct process process;
113 /** Retransmission timer */
114 struct retry_timer timer;
115 /** Shutdown (TIME_WAIT) timer */
116 struct retry_timer wait;
118 /** Pending operations for SYN and FIN */
119 struct pending_operation pending_flags;
120 /** Pending operations for transmit queue */
121 struct pending_operation pending_data;
126 /** TCP data transfer interface has been closed */
127 TCP_XFER_CLOSED = 0x0001,
128 /** TCP timestamps are enabled */
129 TCP_TS_ENABLED = 0x0002,
130 /** TCP acknowledgement is pending */
131 TCP_ACK_PENDING = 0x0004,
134 /** TCP internal header
136 * This is the header that replaces the TCP header for packets
137 * enqueued on the receive queue.
139 struct tcp_rx_queued_header {
140 /** SEQ value, in host-endian order
142 * This represents the SEQ value at the time the packet is
143 * enqueued, and so excludes the SYN, if present.
148 * Only FIN is valid within this flags byte; all other flags
149 * have already been processed by the time the packet is
158 * List of registered TCP connections
160 static LIST_HEAD ( tcp_conns );
162 /** Transmit profiler */
163 static struct profiler tcp_tx_profiler __profiler = { .name = "tcp.tx" };
165 /** Receive profiler */
166 static struct profiler tcp_rx_profiler __profiler = { .name = "tcp.rx" };
168 /** Data transfer profiler */
169 static struct profiler tcp_xfer_profiler __profiler = { .name = "tcp.xfer" };
171 /* Forward declarations */
172 static struct process_descriptor tcp_process_desc;
173 static struct interface_descriptor tcp_xfer_desc;
174 static void tcp_expired ( struct retry_timer *timer, int over );
175 static void tcp_wait_expired ( struct retry_timer *timer, int over );
176 static struct tcp_connection * tcp_demux ( unsigned int local_port );
177 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
184 * @ret name Name of TCP state
186 static inline __attribute__ (( always_inline )) const char *
187 tcp_state ( int state ) {
189 case TCP_CLOSED: return "CLOSED";
190 case TCP_LISTEN: return "LISTEN";
191 case TCP_SYN_SENT: return "SYN_SENT";
192 case TCP_SYN_RCVD: return "SYN_RCVD";
193 case TCP_ESTABLISHED: return "ESTABLISHED";
194 case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
195 case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
196 case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
197 case TCP_TIME_WAIT: return "TIME_WAIT";
198 case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
199 default: return "INVALID";
204 * Dump TCP state transition
206 * @v tcp TCP connection
208 static inline __attribute__ (( always_inline )) void
209 tcp_dump_state ( struct tcp_connection *tcp ) {
211 if ( tcp->tcp_state != tcp->prev_tcp_state ) {
212 DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
213 tcp_state ( tcp->prev_tcp_state ),
214 tcp_state ( tcp->tcp_state ) );
216 tcp->prev_tcp_state = tcp->tcp_state;
224 static inline __attribute__ (( always_inline )) void
225 tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
226 if ( flags & TCP_RST )
227 DBGC2 ( tcp, " RST" );
228 if ( flags & TCP_SYN )
229 DBGC2 ( tcp, " SYN" );
230 if ( flags & TCP_PSH )
231 DBGC2 ( tcp, " PSH" );
232 if ( flags & TCP_FIN )
233 DBGC2 ( tcp, " FIN" );
234 if ( flags & TCP_ACK )
235 DBGC2 ( tcp, " ACK" );
238 /***************************************************************************
242 ***************************************************************************
246 * Check if local TCP port is available
248 * @v port Local port number
249 * @ret port Local port number, or negative error
251 static int tcp_port_available ( int port ) {
253 return ( tcp_demux ( port ) ? -EADDRINUSE : port );
257 * Open a TCP connection
259 * @v xfer Data transfer interface
260 * @v peer Peer socket address
261 * @v local Local socket address, or NULL
262 * @ret rc Return status code
264 static int tcp_open ( struct interface *xfer, struct sockaddr *peer,
265 struct sockaddr *local ) {
266 struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
267 struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
268 struct tcp_connection *tcp;
273 /* Allocate and initialise structure */
274 tcp = zalloc ( sizeof ( *tcp ) );
277 DBGC ( tcp, "TCP %p allocated\n", tcp );
278 ref_init ( &tcp->refcnt, NULL );
279 intf_init ( &tcp->xfer, &tcp_xfer_desc, &tcp->refcnt );
280 process_init_stopped ( &tcp->process, &tcp_process_desc, &tcp->refcnt );
281 timer_init ( &tcp->timer, tcp_expired, &tcp->refcnt );
282 timer_init ( &tcp->wait, tcp_wait_expired, &tcp->refcnt );
283 tcp->prev_tcp_state = TCP_CLOSED;
284 tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
285 tcp_dump_state ( tcp );
286 tcp->snd_seq = random();
287 tcp->max_rcv_win = TCP_MAX_WINDOW_SIZE;
288 INIT_LIST_HEAD ( &tcp->tx_queue );
289 INIT_LIST_HEAD ( &tcp->rx_queue );
290 memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
293 mtu = tcpip_mtu ( &tcp->peer );
295 DBGC ( tcp, "TCP %p has no route to %s\n",
296 tcp, sock_ntoa ( peer ) );
300 tcp->mss = ( mtu - sizeof ( struct tcp_header ) );
302 /* Bind to local port */
303 port = tcpip_bind ( st_local, tcp_port_available );
306 DBGC ( tcp, "TCP %p could not bind: %s\n",
307 tcp, strerror ( rc ) );
310 tcp->local_port = port;
311 DBGC ( tcp, "TCP %p bound to port %d\n", tcp, tcp->local_port );
313 /* Start timer to initiate SYN */
314 start_timer_nodelay ( &tcp->timer );
316 /* Add a pending operation for the SYN */
317 pending_get ( &tcp->pending_flags );
319 /* Attach parent interface, transfer reference to connection
322 intf_plug_plug ( &tcp->xfer, xfer );
323 list_add ( &tcp->list, &tcp_conns );
327 ref_put ( &tcp->refcnt );
332 * Close TCP connection
334 * @v tcp TCP connection
335 * @v rc Reason for close
337 * Closes the data transfer interface. If the TCP state machine is in
338 * a suitable state, the connection will be deleted.
340 static void tcp_close ( struct tcp_connection *tcp, int rc ) {
341 struct io_buffer *iobuf;
342 struct io_buffer *tmp;
344 /* Close data transfer interface */
345 intf_shutdown ( &tcp->xfer, rc );
346 tcp->flags |= TCP_XFER_CLOSED;
348 /* If we are in CLOSED, or have otherwise not yet received a
349 * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
352 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
354 /* Transition to CLOSED for the sake of debugging messages */
355 tcp->tcp_state = TCP_CLOSED;
356 tcp_dump_state ( tcp );
358 /* Free any unprocessed I/O buffers */
359 list_for_each_entry_safe ( iobuf, tmp, &tcp->rx_queue, list ) {
360 list_del ( &iobuf->list );
364 /* Free any unsent I/O buffers */
365 list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
366 list_del ( &iobuf->list );
368 pending_put ( &tcp->pending_data );
370 assert ( ! is_pending ( &tcp->pending_data ) );
372 /* Remove pending operations for SYN and FIN, if applicable */
373 pending_put ( &tcp->pending_flags );
374 pending_put ( &tcp->pending_flags );
376 /* Remove from list and drop reference */
377 process_del ( &tcp->process );
378 stop_timer ( &tcp->timer );
379 stop_timer ( &tcp->wait );
380 list_del ( &tcp->list );
381 ref_put ( &tcp->refcnt );
382 DBGC ( tcp, "TCP %p connection deleted\n", tcp );
386 /* If we have not had our SYN acknowledged (i.e. we are in
387 * SYN_RCVD), pretend that it has been acknowledged so that we
388 * can send a FIN without breaking things.
390 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
391 tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
393 /* If we have no data remaining to send, start sending FIN */
394 if ( list_empty ( &tcp->tx_queue ) &&
395 ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_FIN ) ) ) {
397 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
398 tcp_dump_state ( tcp );
400 /* Add a pending operation for the FIN */
401 pending_get ( &tcp->pending_flags );
405 /***************************************************************************
409 ***************************************************************************
413 * Calculate transmission window
415 * @v tcp TCP connection
416 * @ret len Maximum length that can be sent in a single packet
418 static size_t tcp_xmit_win ( struct tcp_connection *tcp ) {
421 /* Not ready if we're not in a suitable connection state */
422 if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
425 /* Length is the minimum of the receiver's window and the path MTU */
427 if ( len > TCP_PATH_MTU )
434 * Check data-transfer flow control window
436 * @v tcp TCP connection
437 * @ret len Length of window
439 static size_t tcp_xfer_window ( struct tcp_connection *tcp ) {
441 /* Not ready if data queue is non-empty. This imposes a limit
442 * of only one unACKed packet in the TX queue at any time; we
443 * do this to conserve memory usage.
445 if ( ! list_empty ( &tcp->tx_queue ) )
448 /* Return TCP window length */
449 return tcp_xmit_win ( tcp );
453 * Process TCP transmit queue
455 * @v tcp TCP connection
456 * @v max_len Maximum length to process
457 * @v dest I/O buffer to fill with data, or NULL
458 * @v remove Remove data from queue
459 * @ret len Length of data processed
461 * This processes at most @c max_len bytes from the TCP connection's
462 * transmit queue. Data will be copied into the @c dest I/O buffer
463 * (if provided) and, if @c remove is true, removed from the transmit
466 static size_t tcp_process_tx_queue ( struct tcp_connection *tcp, size_t max_len,
467 struct io_buffer *dest, int remove ) {
468 struct io_buffer *iobuf;
469 struct io_buffer *tmp;
473 list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
474 frag_len = iob_len ( iobuf );
475 if ( frag_len > max_len )
478 memcpy ( iob_put ( dest, frag_len ), iobuf->data,
482 iob_pull ( iobuf, frag_len );
483 if ( ! iob_len ( iobuf ) ) {
484 list_del ( &iobuf->list );
486 pending_put ( &tcp->pending_data );
496 * Transmit any outstanding data
498 * @v tcp TCP connection
500 * Transmits any outstanding data on the connection.
502 * Note that even if an error is returned, the retransmission timer
503 * will have been started if necessary, and so the stack will
504 * eventually attempt to retransmit the failed packet.
506 static void tcp_xmit ( struct tcp_connection *tcp ) {
507 struct io_buffer *iobuf;
508 struct tcp_header *tcphdr;
509 struct tcp_mss_option *mssopt;
510 struct tcp_window_scale_padded_option *wsopt;
511 struct tcp_timestamp_padded_option *tsopt;
517 uint32_t max_rcv_win;
518 uint32_t max_representable_win;
521 /* Start profiling */
522 profile_start ( &tcp_tx_profiler );
524 /* If retransmission timer is already running, do nothing */
525 if ( timer_running ( &tcp->timer ) )
528 /* Calculate both the actual (payload) and sequence space
529 * lengths that we wish to transmit.
531 if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
532 len = tcp_process_tx_queue ( tcp, tcp_xmit_win ( tcp ),
536 flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
537 if ( flags & ( TCP_SYN | TCP_FIN ) ) {
538 /* SYN or FIN consume one byte, and we can never send both */
539 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
542 tcp->snd_sent = seq_len;
544 /* If we have nothing to transmit, stop now */
545 if ( ( seq_len == 0 ) && ! ( tcp->flags & TCP_ACK_PENDING ) )
548 /* If we are transmitting anything that requires
549 * acknowledgement (i.e. consumes sequence space), start the
550 * retransmission timer. Do this before attempting to
551 * allocate the I/O buffer, in case allocation itself fails.
554 start_timer ( &tcp->timer );
556 /* Allocate I/O buffer */
557 iobuf = alloc_iob ( len + TCP_MAX_HEADER_LEN );
559 DBGC ( tcp, "TCP %p could not allocate iobuf for %08x..%08x "
560 "%08x\n", tcp, tcp->snd_seq, ( tcp->snd_seq + seq_len ),
564 iob_reserve ( iobuf, TCP_MAX_HEADER_LEN );
566 /* Fill data payload from transmit queue */
567 tcp_process_tx_queue ( tcp, len, iobuf, 0 );
569 /* Expand receive window if possible */
570 max_rcv_win = tcp->max_rcv_win;
571 app_win = xfer_window ( &tcp->xfer );
572 if ( max_rcv_win > app_win )
573 max_rcv_win = app_win;
574 max_representable_win = ( 0xffff << tcp->rcv_win_scale );
575 if ( max_rcv_win > max_representable_win )
576 max_rcv_win = max_representable_win;
577 max_rcv_win &= ~0x03; /* Keep everything dword-aligned */
578 if ( tcp->rcv_win < max_rcv_win )
579 tcp->rcv_win = max_rcv_win;
581 /* Fill up the TCP header */
582 payload = iobuf->data;
583 if ( flags & TCP_SYN ) {
584 mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
585 mssopt->kind = TCP_OPTION_MSS;
586 mssopt->length = sizeof ( *mssopt );
587 mssopt->mss = htons ( tcp->mss );
588 wsopt = iob_push ( iobuf, sizeof ( *wsopt ) );
589 wsopt->nop = TCP_OPTION_NOP;
590 wsopt->wsopt.kind = TCP_OPTION_WS;
591 wsopt->wsopt.length = sizeof ( wsopt->wsopt );
592 wsopt->wsopt.scale = TCP_RX_WINDOW_SCALE;
594 if ( ( flags & TCP_SYN ) || ( tcp->flags & TCP_TS_ENABLED ) ) {
595 tsopt = iob_push ( iobuf, sizeof ( *tsopt ) );
596 memset ( tsopt->nop, TCP_OPTION_NOP, sizeof ( tsopt->nop ) );
597 tsopt->tsopt.kind = TCP_OPTION_TS;
598 tsopt->tsopt.length = sizeof ( tsopt->tsopt );
599 tsopt->tsopt.tsval = htonl ( currticks() );
600 tsopt->tsopt.tsecr = htonl ( tcp->ts_recent );
604 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
605 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
606 tcphdr->src = htons ( tcp->local_port );
607 tcphdr->dest = tcp->peer.st_port;
608 tcphdr->seq = htonl ( tcp->snd_seq );
609 tcphdr->ack = htonl ( tcp->rcv_ack );
610 tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
611 tcphdr->flags = flags;
612 tcphdr->win = htons ( tcp->rcv_win >> tcp->rcv_win_scale );
613 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
616 DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4zd",
617 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
618 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
619 ntohl ( tcphdr->ack ), len );
620 tcp_dump_flags ( tcp, tcphdr->flags );
623 /* Transmit packet */
624 if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, &tcp->peer, NULL,
625 &tcphdr->csum ) ) != 0 ) {
626 DBGC ( tcp, "TCP %p could not transmit %08x..%08x %08x: %s\n",
627 tcp, tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ),
628 tcp->rcv_ack, strerror ( rc ) );
632 /* Clear ACK-pending flag */
633 tcp->flags &= ~TCP_ACK_PENDING;
635 profile_stop ( &tcp_tx_profiler );
638 /** TCP process descriptor */
639 static struct process_descriptor tcp_process_desc =
640 PROC_DESC_ONCE ( struct tcp_connection, process, tcp_xmit );
643 * Retransmission timer expired
645 * @v timer Retransmission timer
646 * @v over Failure indicator
648 static void tcp_expired ( struct retry_timer *timer, int over ) {
649 struct tcp_connection *tcp =
650 container_of ( timer, struct tcp_connection, timer );
652 DBGC ( tcp, "TCP %p timer %s in %s for %08x..%08x %08x\n", tcp,
653 ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ),
654 tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
656 assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
657 ( tcp->tcp_state == TCP_SYN_RCVD ) ||
658 ( tcp->tcp_state == TCP_ESTABLISHED ) ||
659 ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
660 ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
661 ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
664 /* If we have finally timed out and given up,
665 * terminate the connection
667 tcp->tcp_state = TCP_CLOSED;
668 tcp_dump_state ( tcp );
669 tcp_close ( tcp, -ETIMEDOUT );
671 /* Otherwise, retransmit the packet */
677 * Shutdown timer expired
679 * @v timer Shutdown timer
680 * @v over Failure indicator
682 static void tcp_wait_expired ( struct retry_timer *timer, int over __unused ) {
683 struct tcp_connection *tcp =
684 container_of ( timer, struct tcp_connection, wait );
686 assert ( tcp->tcp_state == TCP_TIME_WAIT );
688 DBGC ( tcp, "TCP %p wait complete in %s for %08x..%08x %08x\n", tcp,
689 tcp_state ( tcp->tcp_state ), tcp->snd_seq,
690 ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
692 tcp->tcp_state = TCP_CLOSED;
693 tcp_dump_state ( tcp );
694 tcp_close ( tcp, 0 );
698 * Send RST response to incoming packet
700 * @v in_tcphdr TCP header of incoming packet
701 * @ret rc Return status code
703 static int tcp_xmit_reset ( struct tcp_connection *tcp,
704 struct sockaddr_tcpip *st_dest,
705 struct tcp_header *in_tcphdr ) {
706 struct io_buffer *iobuf;
707 struct tcp_header *tcphdr;
710 /* Allocate space for dataless TX buffer */
711 iobuf = alloc_iob ( TCP_MAX_HEADER_LEN );
713 DBGC ( tcp, "TCP %p could not allocate iobuf for RST "
714 "%08x..%08x %08x\n", tcp, ntohl ( in_tcphdr->ack ),
715 ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ) );
718 iob_reserve ( iobuf, TCP_MAX_HEADER_LEN );
720 /* Construct RST response */
721 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
722 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
723 tcphdr->src = in_tcphdr->dest;
724 tcphdr->dest = in_tcphdr->src;
725 tcphdr->seq = in_tcphdr->ack;
726 tcphdr->ack = in_tcphdr->seq;
727 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
728 tcphdr->flags = ( TCP_RST | TCP_ACK );
729 tcphdr->win = htons ( 0 );
730 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
733 DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4d",
734 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
735 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
736 ntohl ( tcphdr->ack ), 0 );
737 tcp_dump_flags ( tcp, tcphdr->flags );
740 /* Transmit packet */
741 if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, st_dest,
742 NULL, &tcphdr->csum ) ) != 0 ) {
743 DBGC ( tcp, "TCP %p could not transmit RST %08x..%08x %08x: "
744 "%s\n", tcp, ntohl ( in_tcphdr->ack ),
745 ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ),
753 /***************************************************************************
757 ***************************************************************************
761 * Identify TCP connection by local port number
763 * @v local_port Local port
764 * @ret tcp TCP connection, or NULL
766 static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
767 struct tcp_connection *tcp;
769 list_for_each_entry ( tcp, &tcp_conns, list ) {
770 if ( tcp->local_port == local_port )
777 * Parse TCP received options
779 * @v tcp TCP connection
780 * @v data Raw options data
781 * @v len Raw options length
782 * @v options Options structure to fill in
784 static void tcp_rx_opts ( struct tcp_connection *tcp, const void *data,
785 size_t len, struct tcp_options *options ) {
786 const void *end = ( data + len );
787 const struct tcp_option *option;
790 memset ( options, 0, sizeof ( *options ) );
791 while ( data < end ) {
794 if ( kind == TCP_OPTION_END )
796 if ( kind == TCP_OPTION_NOP ) {
802 options->mssopt = data;
805 options->wsopt = data;
808 options->tsopt = data;
811 DBGC ( tcp, "TCP %p received unknown option %d\n",
815 data += option->length;
820 * Consume received sequence space
822 * @v tcp TCP connection
823 * @v seq_len Sequence space length to consume
825 static void tcp_rx_seq ( struct tcp_connection *tcp, uint32_t seq_len ) {
828 assert ( seq_len > 0 );
830 /* Update acknowledgement number */
831 tcp->rcv_ack += seq_len;
834 if ( tcp->rcv_win > seq_len ) {
835 tcp->rcv_win -= seq_len;
840 /* Update timestamp */
841 tcp->ts_recent = tcp->ts_val;
843 /* Mark ACK as pending */
844 tcp->flags |= TCP_ACK_PENDING;
848 * Handle TCP received SYN
850 * @v tcp TCP connection
851 * @v seq SEQ value (in host-endian order)
852 * @v options TCP options
853 * @ret rc Return status code
855 static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq,
856 struct tcp_options *options ) {
858 /* Synchronise sequence numbers on first SYN */
859 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
861 if ( options->tsopt )
862 tcp->flags |= TCP_TS_ENABLED;
863 if ( options->wsopt ) {
864 tcp->snd_win_scale = options->wsopt->scale;
865 tcp->rcv_win_scale = TCP_RX_WINDOW_SCALE;
869 /* Ignore duplicate SYN */
870 if ( seq != tcp->rcv_ack )
873 /* Acknowledge SYN */
874 tcp_rx_seq ( tcp, 1 );
876 /* Mark SYN as received and start sending ACKs with each packet */
877 tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
878 TCP_STATE_RCVD ( TCP_SYN ) );
884 * Handle TCP received ACK
886 * @v tcp TCP connection
887 * @v ack ACK value (in host-endian order)
888 * @v win WIN value (in host-endian order)
889 * @ret rc Return status code
891 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
893 uint32_t ack_len = ( ack - tcp->snd_seq );
895 unsigned int acked_flags;
897 /* Check for out-of-range or old duplicate ACKs */
898 if ( ack_len > tcp->snd_sent ) {
899 DBGC ( tcp, "TCP %p received ACK for %08x..%08x, "
900 "sent only %08x..%08x\n", tcp, tcp->snd_seq,
901 ( tcp->snd_seq + ack_len ), tcp->snd_seq,
902 ( tcp->snd_seq + tcp->snd_sent ) );
904 if ( TCP_HAS_BEEN_ESTABLISHED ( tcp->tcp_state ) ) {
905 /* Just ignore what might be old duplicate ACKs */
908 /* Send RST if an out-of-range ACK is received
909 * on a not-yet-established connection, as per
916 /* Update window size */
919 /* Ignore ACKs that don't actually acknowledge any new data.
920 * (In particular, do not stop the retransmission timer; this
921 * avoids creating a sorceror's apprentice syndrome when a
922 * duplicate ACK is received and we still have data in our
928 /* Stop the retransmission timer */
929 stop_timer ( &tcp->timer );
931 /* Determine acknowledged flags and data length */
933 acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
934 ( TCP_SYN | TCP_FIN ) );
937 pending_put ( &tcp->pending_flags );
940 /* Update SEQ and sent counters */
944 /* Remove any acknowledged data from transmit queue */
945 tcp_process_tx_queue ( tcp, len, NULL, 1 );
947 /* Mark SYN/FIN as acknowledged if applicable. */
949 tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
951 /* Start sending FIN if we've had all possible data ACKed */
952 if ( list_empty ( &tcp->tx_queue ) &&
953 ( tcp->flags & TCP_XFER_CLOSED ) &&
954 ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_FIN ) ) ) {
955 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
956 pending_get ( &tcp->pending_flags );
963 * Handle TCP received data
965 * @v tcp TCP connection
966 * @v seq SEQ value (in host-endian order)
967 * @v iobuf I/O buffer
968 * @ret rc Return status code
970 * This function takes ownership of the I/O buffer.
972 static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
973 struct io_buffer *iobuf ) {
974 uint32_t already_rcvd;
978 /* Ignore duplicate or out-of-order data */
979 already_rcvd = ( tcp->rcv_ack - seq );
980 len = iob_len ( iobuf );
981 if ( already_rcvd >= len ) {
985 iob_pull ( iobuf, already_rcvd );
988 /* Acknowledge new data */
989 tcp_rx_seq ( tcp, len );
991 /* Deliver data to application */
992 profile_start ( &tcp_xfer_profiler );
993 if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 ) {
994 DBGC ( tcp, "TCP %p could not deliver %08x..%08x: %s\n",
995 tcp, seq, ( seq + len ), strerror ( rc ) );
998 profile_stop ( &tcp_xfer_profiler );
1004 * Handle TCP received FIN
1006 * @v tcp TCP connection
1007 * @v seq SEQ value (in host-endian order)
1008 * @ret rc Return status code
1010 static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
1012 /* Ignore duplicate or out-of-order FIN */
1013 if ( seq != tcp->rcv_ack )
1016 /* Acknowledge FIN */
1017 tcp_rx_seq ( tcp, 1 );
1019 /* Mark FIN as received */
1020 tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
1022 /* Close connection */
1023 tcp_close ( tcp, 0 );
1029 * Handle TCP received RST
1031 * @v tcp TCP connection
1032 * @v seq SEQ value (in host-endian order)
1033 * @ret rc Return status code
1035 static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
1037 /* Accept RST only if it falls within the window. If we have
1038 * not yet received a SYN, then we have no window to test
1039 * against, so fall back to checking that our SYN has been
1042 if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
1043 if ( ! tcp_in_window ( seq, tcp->rcv_ack, tcp->rcv_win ) )
1046 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
1050 /* Abort connection */
1051 tcp->tcp_state = TCP_CLOSED;
1052 tcp_dump_state ( tcp );
1053 tcp_close ( tcp, -ECONNRESET );
1055 DBGC ( tcp, "TCP %p connection reset by peer\n", tcp );
1060 * Enqueue received TCP packet
1062 * @v tcp TCP connection
1063 * @v seq SEQ value (in host-endian order)
1064 * @v flags TCP flags
1065 * @v iobuf I/O buffer
1067 static void tcp_rx_enqueue ( struct tcp_connection *tcp, uint32_t seq,
1068 uint8_t flags, struct io_buffer *iobuf ) {
1069 struct tcp_rx_queued_header *tcpqhdr;
1070 struct io_buffer *queued;
1074 /* Calculate remaining flags and sequence length. Note that
1075 * SYN, if present, has already been processed by this point.
1078 len = iob_len ( iobuf );
1079 seq_len = ( len + ( flags ? 1 : 0 ) );
1081 /* Discard immediately (to save memory) if:
1083 * a) we have not yet received a SYN (and so have no defined
1084 * receive window), or
1085 * b) the packet lies entirely outside the receive window, or
1086 * c) there is no further content to process.
1088 if ( ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) ||
1089 ( tcp_cmp ( seq, tcp->rcv_ack + tcp->rcv_win ) >= 0 ) ||
1090 ( tcp_cmp ( seq + seq_len, tcp->rcv_ack ) < 0 ) ||
1091 ( seq_len == 0 ) ) {
1096 /* Add internal header */
1097 tcpqhdr = iob_push ( iobuf, sizeof ( *tcpqhdr ) );
1099 tcpqhdr->flags = flags;
1101 /* Add to RX queue */
1102 list_for_each_entry ( queued, &tcp->rx_queue, list ) {
1103 tcpqhdr = queued->data;
1104 if ( tcp_cmp ( seq, tcpqhdr->seq ) < 0 )
1107 list_add_tail ( &iobuf->list, &queued->list );
1111 * Process receive queue
1113 * @v tcp TCP connection
1115 static void tcp_process_rx_queue ( struct tcp_connection *tcp ) {
1116 struct io_buffer *iobuf;
1117 struct tcp_rx_queued_header *tcpqhdr;
1122 /* Process all applicable received buffers. Note that we
1123 * cannot use list_for_each_entry() to iterate over the RX
1124 * queue, since tcp_discard() may remove packets from the RX
1125 * queue while we are processing.
1127 while ( ( iobuf = list_first_entry ( &tcp->rx_queue, struct io_buffer,
1130 /* Stop processing when we hit the first gap */
1131 tcpqhdr = iobuf->data;
1132 if ( tcp_cmp ( tcpqhdr->seq, tcp->rcv_ack ) > 0 )
1135 /* Strip internal header and remove from RX queue */
1136 list_del ( &iobuf->list );
1138 flags = tcpqhdr->flags;
1139 iob_pull ( iobuf, sizeof ( *tcpqhdr ) );
1140 len = iob_len ( iobuf );
1142 /* Handle new data, if any */
1143 tcp_rx_data ( tcp, seq, iob_disown ( iobuf ) );
1146 /* Handle FIN, if present */
1147 if ( flags & TCP_FIN ) {
1148 tcp_rx_fin ( tcp, seq );
1155 * Process received packet
1157 * @v iobuf I/O buffer
1158 * @v netdev Network device
1159 * @v st_src Partially-filled source address
1160 * @v st_dest Partially-filled destination address
1161 * @v pshdr_csum Pseudo-header checksum
1162 * @ret rc Return status code
1164 static int tcp_rx ( struct io_buffer *iobuf,
1165 struct net_device *netdev __unused,
1166 struct sockaddr_tcpip *st_src,
1167 struct sockaddr_tcpip *st_dest __unused,
1168 uint16_t pshdr_csum ) {
1169 struct tcp_header *tcphdr = iobuf->data;
1170 struct tcp_connection *tcp;
1171 struct tcp_options options;
1181 size_t old_xfer_window;
1184 /* Start profiling */
1185 profile_start ( &tcp_rx_profiler );
1187 /* Sanity check packet */
1188 if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
1189 DBG ( "TCP packet too short at %zd bytes (min %zd bytes)\n",
1190 iob_len ( iobuf ), sizeof ( *tcphdr ) );
1194 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
1195 if ( hlen < sizeof ( *tcphdr ) ) {
1196 DBG ( "TCP header too short at %zd bytes (min %zd bytes)\n",
1197 hlen, sizeof ( *tcphdr ) );
1201 if ( hlen > iob_len ( iobuf ) ) {
1202 DBG ( "TCP header too long at %zd bytes (max %zd bytes)\n",
1203 hlen, iob_len ( iobuf ) );
1207 csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data,
1208 iob_len ( iobuf ) );
1210 DBG ( "TCP checksum incorrect (is %04x including checksum "
1211 "field, should be 0000)\n", csum );
1216 /* Parse parameters from header and strip header */
1217 tcp = tcp_demux ( ntohs ( tcphdr->dest ) );
1218 seq = ntohl ( tcphdr->seq );
1219 ack = ntohl ( tcphdr->ack );
1220 raw_win = ntohs ( tcphdr->win );
1221 flags = tcphdr->flags;
1222 tcp_rx_opts ( tcp, ( ( ( void * ) tcphdr ) + sizeof ( *tcphdr ) ),
1223 ( hlen - sizeof ( *tcphdr ) ), &options );
1224 if ( tcp && options.tsopt )
1225 tcp->ts_val = ntohl ( options.tsopt->tsval );
1226 iob_pull ( iobuf, hlen );
1227 len = iob_len ( iobuf );
1228 seq_len = ( len + ( ( flags & TCP_SYN ) ? 1 : 0 ) +
1229 ( ( flags & TCP_FIN ) ? 1 : 0 ) );
1232 DBGC2 ( tcp, "TCP %p RX %d<-%d %08x %08x..%08x %4zd",
1233 tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
1234 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
1235 ( ntohl ( tcphdr->seq ) + seq_len ), len );
1236 tcp_dump_flags ( tcp, tcphdr->flags );
1237 DBGC2 ( tcp, "\n" );
1239 /* If no connection was found, silently drop packet */
1245 /* Record old data-transfer window */
1246 old_xfer_window = tcp_xfer_window ( tcp );
1248 /* Handle ACK, if present */
1249 if ( flags & TCP_ACK ) {
1250 win = ( raw_win << tcp->snd_win_scale );
1251 if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
1252 tcp_xmit_reset ( tcp, st_src, tcphdr );
1257 /* Force an ACK if this packet is out of order */
1258 if ( ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) &&
1259 ( seq != tcp->rcv_ack ) ) {
1260 tcp->flags |= TCP_ACK_PENDING;
1263 /* Handle SYN, if present */
1264 if ( flags & TCP_SYN ) {
1265 tcp_rx_syn ( tcp, seq, &options );
1269 /* Handle RST, if present */
1270 if ( flags & TCP_RST ) {
1271 if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
1275 /* Enqueue received data */
1276 tcp_rx_enqueue ( tcp, seq, flags, iob_disown ( iobuf ) );
1278 /* Process receive queue */
1279 tcp_process_rx_queue ( tcp );
1281 /* Dump out any state change as a result of the received packet */
1282 tcp_dump_state ( tcp );
1284 /* Schedule transmission of ACK (and any pending data). If we
1285 * have received any out-of-order packets (i.e. if the receive
1286 * queue remains non-empty after processing) then send the ACK
1287 * immediately in order to trigger Fast Retransmission.
1289 if ( list_empty ( &tcp->rx_queue ) ) {
1290 process_add ( &tcp->process );
1295 /* If this packet was the last we expect to receive, set up
1296 * timer to expire and cause the connection to be freed.
1298 if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
1299 stop_timer ( &tcp->wait );
1300 start_timer_fixed ( &tcp->wait, ( 2 * TCP_MSL ) );
1303 /* Notify application if window has changed */
1304 if ( tcp_xfer_window ( tcp ) != old_xfer_window )
1305 xfer_window_changed ( &tcp->xfer );
1307 profile_stop ( &tcp_rx_profiler );
1311 /* Free received packet */
1317 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
1320 .tcpip_proto = IP_TCP,
1324 * Discard some cached TCP data
1326 * @ret discarded Number of cached items discarded
1328 static unsigned int tcp_discard ( void ) {
1329 struct tcp_connection *tcp;
1330 struct io_buffer *iobuf;
1331 struct tcp_rx_queued_header *tcpqhdr;
1333 unsigned int discarded = 0;
1335 /* Try to drop one queued RX packet from each connection */
1336 list_for_each_entry ( tcp, &tcp_conns, list ) {
1337 list_for_each_entry_reverse ( iobuf, &tcp->rx_queue, list ) {
1339 /* Limit window to prevent future discards */
1340 tcpqhdr = iobuf->data;
1341 max_win = ( tcpqhdr->seq - tcp->rcv_ack );
1342 if ( max_win < tcp->max_rcv_win ) {
1343 DBGC ( tcp, "TCP %p reducing maximum window "
1345 tcp, tcp->max_rcv_win, max_win );
1346 tcp->max_rcv_win = max_win;
1349 /* Remove packet from queue */
1350 list_del ( &iobuf->list );
1353 /* Report discard */
1362 /** TCP cache discarder */
1363 struct cache_discarder tcp_discarder __cache_discarder ( CACHE_NORMAL ) = {
1364 .discard = tcp_discard,
1368 * Shut down all TCP connections
1371 static void tcp_shutdown ( int booting __unused ) {
1372 struct tcp_connection *tcp;
1374 while ( ( tcp = list_first_entry ( &tcp_conns, struct tcp_connection,
1375 list ) ) != NULL ) {
1376 tcp->tcp_state = TCP_CLOSED;
1377 tcp_dump_state ( tcp );
1378 tcp_close ( tcp, -ECANCELED );
1382 /** TCP shutdown function */
1383 struct startup_fn tcp_startup_fn __startup_fn ( STARTUP_EARLY ) = {
1384 .shutdown = tcp_shutdown,
1387 /***************************************************************************
1389 * Data transfer interface
1391 ***************************************************************************
1397 * @v tcp TCP connection
1398 * @v rc Reason for close
1400 static void tcp_xfer_close ( struct tcp_connection *tcp, int rc ) {
1402 /* Close data transfer interface */
1403 tcp_close ( tcp, rc );
1405 /* Transmit FIN, if possible */
1410 * Deliver datagram as I/O buffer
1412 * @v tcp TCP connection
1413 * @v iobuf Datagram I/O buffer
1414 * @v meta Data transfer metadata
1415 * @ret rc Return status code
1417 static int tcp_xfer_deliver ( struct tcp_connection *tcp,
1418 struct io_buffer *iobuf,
1419 struct xfer_metadata *meta __unused ) {
1421 /* Enqueue packet */
1422 list_add_tail ( &iobuf->list, &tcp->tx_queue );
1424 /* Each enqueued packet is a pending operation */
1425 pending_get ( &tcp->pending_data );
1427 /* Transmit data, if possible */
1433 /** TCP data transfer interface operations */
1434 static struct interface_operation tcp_xfer_operations[] = {
1435 INTF_OP ( xfer_deliver, struct tcp_connection *, tcp_xfer_deliver ),
1436 INTF_OP ( xfer_window, struct tcp_connection *, tcp_xfer_window ),
1437 INTF_OP ( intf_close, struct tcp_connection *, tcp_xfer_close ),
1440 /** TCP data transfer interface descriptor */
1441 static struct interface_descriptor tcp_xfer_desc =
1442 INTF_DESC ( struct tcp_connection, xfer, tcp_xfer_operations );
1444 /***************************************************************************
1448 ***************************************************************************
1451 /** TCP IPv4 socket opener */
1452 struct socket_opener tcp_ipv4_socket_opener __socket_opener = {
1453 .semantics = TCP_SOCK_STREAM,
1458 /** TCP IPv6 socket opener */
1459 struct socket_opener tcp_ipv6_socket_opener __socket_opener = {
1460 .semantics = TCP_SOCK_STREAM,
1466 int tcp_sock_stream = TCP_SOCK_STREAM;
1471 * @v xfer Data transfer interface
1473 * @ret rc Return status code
1475 static int tcp_open_uri ( struct interface *xfer, struct uri *uri ) {
1476 struct sockaddr_tcpip peer;
1482 memset ( &peer, 0, sizeof ( peer ) );
1483 peer.st_port = htons ( uri_port ( uri, 0 ) );
1484 return xfer_open_named_socket ( xfer, SOCK_STREAM,
1485 ( struct sockaddr * ) &peer,
1489 /** TCP URI opener */
1490 struct uri_opener tcp_uri_opener __uri_opener = {
1492 .open = tcp_open_uri,