7 #include <gpxe/timer.h>
8 #include <gpxe/iobuf.h>
9 #include <gpxe/malloc.h>
10 #include <gpxe/retry.h>
11 #include <gpxe/refcnt.h>
12 #include <gpxe/xfer.h>
13 #include <gpxe/open.h>
15 #include <gpxe/tcpip.h>
24 /** A TCP connection */
25 struct tcp_connection {
26 /** Reference counter */
28 /** List of TCP connections */
29 struct list_head list;
31 /** Data transfer interface */
32 struct xfer_interface xfer;
33 /** Data transfer interface closed flag */
36 /** Remote socket address */
37 struct sockaddr_tcpip peer;
38 /** Local port, in network byte order */
39 unsigned int local_port;
41 /** Current TCP state */
42 unsigned int tcp_state;
43 /** Previous TCP state
45 * Maintained only for debug messages
47 unsigned int prev_tcp_state;
48 /** Current sequence number
50 * Equivalent to SND.UNA in RFC 793 terminology.
53 /** Unacknowledged sequence count
55 * Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
60 * Equivalent to SND.WND in RFC 793 terminology
63 /** Current acknowledgement number
65 * Equivalent to RCV.NXT in RFC 793 terminology.
70 * Equivalent to RCV.WND in RFC 793 terminology.
73 /** Most recent received timestamp
75 * Equivalent to TS.Recent in RFC 1323 terminology.
78 /** Timestamps enabled */
82 struct list_head queue;
83 /** Retransmission timer */
84 struct retry_timer timer;
88 * List of registered TCP connections
90 static LIST_HEAD ( tcp_conns );
92 /* Forward declarations */
93 static struct xfer_interface_operations tcp_xfer_operations;
94 static void tcp_expired ( struct retry_timer *timer, int over );
95 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
102 * @ret name Name of TCP state
104 static inline __attribute__ (( always_inline )) const char *
105 tcp_state ( int state ) {
107 case TCP_CLOSED: return "CLOSED";
108 case TCP_LISTEN: return "LISTEN";
109 case TCP_SYN_SENT: return "SYN_SENT";
110 case TCP_SYN_RCVD: return "SYN_RCVD";
111 case TCP_ESTABLISHED: return "ESTABLISHED";
112 case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
113 case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
114 case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
115 case TCP_TIME_WAIT: return "TIME_WAIT";
116 case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
117 default: return "INVALID";
122 * Dump TCP state transition
124 * @v tcp TCP connection
126 static inline __attribute__ (( always_inline )) void
127 tcp_dump_state ( struct tcp_connection *tcp ) {
129 if ( tcp->tcp_state != tcp->prev_tcp_state ) {
130 DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
131 tcp_state ( tcp->prev_tcp_state ),
132 tcp_state ( tcp->tcp_state ) );
134 tcp->prev_tcp_state = tcp->tcp_state;
142 static inline __attribute__ (( always_inline )) void
143 tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
144 if ( flags & TCP_RST )
145 DBGC ( tcp, " RST" );
146 if ( flags & TCP_SYN )
147 DBGC ( tcp, " SYN" );
148 if ( flags & TCP_PSH )
149 DBGC ( tcp, " PSH" );
150 if ( flags & TCP_FIN )
151 DBGC ( tcp, " FIN" );
152 if ( flags & TCP_ACK )
153 DBGC ( tcp, " ACK" );
156 /***************************************************************************
160 ***************************************************************************
164 * Bind TCP connection to local port
166 * @v tcp TCP connection
167 * @v port Local port number, in network-endian order
168 * @ret rc Return status code
170 * If the port is 0, the connection is assigned an available port
171 * between 1024 and 65535.
173 static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
174 struct tcp_connection *existing;
175 static uint16_t try_port = 1023;
177 /* If no port specified, find the first available port */
181 if ( try_port < 1024 )
183 if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 )
186 DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
190 /* Attempt bind to local port */
191 list_for_each_entry ( existing, &tcp_conns, list ) {
192 if ( existing->local_port == port ) {
193 DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
194 tcp, ntohs ( port ) );
198 tcp->local_port = port;
200 DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( port ) );
205 * Open a TCP connection
207 * @v xfer Data transfer interface
208 * @v peer Peer socket address
209 * @v local Local socket address, or NULL
210 * @ret rc Return status code
212 static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
213 struct sockaddr *local ) {
214 struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
215 struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
216 struct tcp_connection *tcp;
217 unsigned int bind_port;
220 /* Allocate and initialise structure */
221 tcp = zalloc ( sizeof ( *tcp ) );
224 DBGC ( tcp, "TCP %p allocated\n", tcp );
225 xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
226 tcp->prev_tcp_state = TCP_CLOSED;
227 tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
228 tcp_dump_state ( tcp );
229 tcp->snd_seq = random();
230 INIT_LIST_HEAD ( &tcp->queue );
231 tcp->timer.expired = tcp_expired;
232 memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
234 /* Bind to local port */
235 bind_port = ( st_local ? st_local->st_port : 0 );
236 if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
239 /* Start timer to initiate SYN */
240 start_timer_nodelay ( &tcp->timer );
242 /* Attach parent interface, transfer reference to connection
245 xfer_plug_plug ( &tcp->xfer, xfer );
246 list_add ( &tcp->list, &tcp_conns );
250 ref_put ( &tcp->refcnt );
255 * Close TCP connection
257 * @v tcp TCP connection
258 * @v rc Reason for close
260 * Closes the data transfer interface. If the TCP state machine is in
261 * a suitable state, the connection will be deleted.
263 static void tcp_close ( struct tcp_connection *tcp, int rc ) {
264 struct io_buffer *iobuf;
265 struct io_buffer *tmp;
267 /* Close data transfer interface */
268 xfer_nullify ( &tcp->xfer );
269 xfer_close ( &tcp->xfer, rc );
270 tcp->xfer_closed = 1;
272 /* If we are in CLOSED, or have otherwise not yet received a
273 * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
276 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
278 /* Transition to CLOSED for the sake of debugging messages */
279 tcp->tcp_state = TCP_CLOSED;
280 tcp_dump_state ( tcp );
282 /* Free any unsent I/O buffers */
283 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
284 list_del ( &iobuf->list );
288 /* Remove from list and drop reference */
289 stop_timer ( &tcp->timer );
290 list_del ( &tcp->list );
291 ref_put ( &tcp->refcnt );
292 DBGC ( tcp, "TCP %p connection deleted\n", tcp );
296 /* If we have not had our SYN acknowledged (i.e. we are in
297 * SYN_RCVD), pretend that it has been acknowledged so that we
298 * can send a FIN without breaking things.
300 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
301 tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
303 /* If we have no data remaining to send, start sending FIN */
304 if ( list_empty ( &tcp->queue ) ) {
305 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
306 tcp_dump_state ( tcp );
310 /***************************************************************************
314 ***************************************************************************
318 * Calculate transmission window
320 * @v tcp TCP connection
321 * @ret len Maximum length that can be sent in a single packet
323 static size_t tcp_xmit_win ( struct tcp_connection *tcp ) {
326 /* Not ready if we're not in a suitable connection state */
327 if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
330 /* Length is the minimum of the receiver's window and the path MTU */
332 if ( len > TCP_PATH_MTU )
339 * Process TCP transmit queue
341 * @v tcp TCP connection
342 * @v max_len Maximum length to process
343 * @v dest I/O buffer to fill with data, or NULL
344 * @v remove Remove data from queue
345 * @ret len Length of data processed
347 * This processes at most @c max_len bytes from the TCP connection's
348 * transmit queue. Data will be copied into the @c dest I/O buffer
349 * (if provided) and, if @c remove is true, removed from the transmit
352 static size_t tcp_process_queue ( struct tcp_connection *tcp, size_t max_len,
353 struct io_buffer *dest, int remove ) {
354 struct io_buffer *iobuf;
355 struct io_buffer *tmp;
359 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
360 frag_len = iob_len ( iobuf );
361 if ( frag_len > max_len )
364 memcpy ( iob_put ( dest, frag_len ), iobuf->data,
368 iob_pull ( iobuf, frag_len );
369 if ( ! iob_len ( iobuf ) ) {
370 list_del ( &iobuf->list );
381 * Transmit any outstanding data
383 * @v tcp TCP connection
384 * @v force_send Force sending of packet
386 * Transmits any outstanding data on the connection.
388 * Note that even if an error is returned, the retransmission timer
389 * will have been started if necessary, and so the stack will
390 * eventually attempt to retransmit the failed packet.
392 static int tcp_xmit ( struct tcp_connection *tcp, int force_send ) {
393 struct io_buffer *iobuf;
394 struct tcp_header *tcphdr;
395 struct tcp_mss_option *mssopt;
396 struct tcp_timestamp_padded_option *tsopt;
404 /* If retransmission timer is already running, do nothing */
405 if ( timer_running ( &tcp->timer ) )
408 /* Calculate both the actual (payload) and sequence space
409 * lengths that we wish to transmit.
411 if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
412 len = tcp_process_queue ( tcp, tcp_xmit_win ( tcp ),
416 flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
417 if ( flags & ( TCP_SYN | TCP_FIN ) ) {
418 /* SYN or FIN consume one byte, and we can never send both */
419 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
422 tcp->snd_sent = seq_len;
424 /* If we have nothing to transmit, stop now */
425 if ( ( seq_len == 0 ) && ! force_send )
428 /* If we are transmitting anything that requires
429 * acknowledgement (i.e. consumes sequence space), start the
430 * retransmission timer. Do this before attempting to
431 * allocate the I/O buffer, in case allocation itself fails.
434 start_timer ( &tcp->timer );
436 /* Allocate I/O buffer */
437 iobuf = alloc_iob ( len + MAX_HDR_LEN );
439 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
442 iob_reserve ( iobuf, MAX_HDR_LEN );
444 /* Fill data payload from transmit queue */
445 tcp_process_queue ( tcp, len, iobuf, 0 );
447 /* Expand receive window if possible */
448 max_rcv_win = ( ( freemem * 3 ) / 4 );
449 if ( max_rcv_win > TCP_MAX_WINDOW_SIZE )
450 max_rcv_win = TCP_MAX_WINDOW_SIZE;
451 app_win = xfer_window ( &tcp->xfer );
452 if ( max_rcv_win > app_win )
453 max_rcv_win = app_win;
454 max_rcv_win &= ~0x03; /* Keep everything dword-aligned */
455 if ( tcp->rcv_win < max_rcv_win )
456 tcp->rcv_win = max_rcv_win;
458 /* Fill up the TCP header */
459 payload = iobuf->data;
460 if ( flags & TCP_SYN ) {
461 mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
462 mssopt->kind = TCP_OPTION_MSS;
463 mssopt->length = sizeof ( *mssopt );
464 mssopt->mss = htons ( TCP_MSS );
466 if ( ( flags & TCP_SYN ) || tcp->timestamps ) {
467 tsopt = iob_push ( iobuf, sizeof ( *tsopt ) );
468 memset ( tsopt->nop, TCP_OPTION_NOP, sizeof ( tsopt->nop ) );
469 tsopt->tsopt.kind = TCP_OPTION_TS;
470 tsopt->tsopt.length = sizeof ( tsopt->tsopt );
471 tsopt->tsopt.tsval = ntohl ( currticks() );
472 tsopt->tsopt.tsecr = ntohl ( tcp->ts_recent );
474 if ( ! ( flags & TCP_SYN ) )
476 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
477 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
478 tcphdr->src = tcp->local_port;
479 tcphdr->dest = tcp->peer.st_port;
480 tcphdr->seq = htonl ( tcp->snd_seq );
481 tcphdr->ack = htonl ( tcp->rcv_ack );
482 tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
483 tcphdr->flags = flags;
484 tcphdr->win = htons ( tcp->rcv_win );
485 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
488 DBGC ( tcp, "TCP %p TX %d->%d %08x..%08zx %08x %4zd",
489 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
490 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
491 ntohl ( tcphdr->ack ), len );
492 tcp_dump_flags ( tcp, tcphdr->flags );
495 /* Transmit packet */
496 return tcpip_tx ( iobuf, &tcp_protocol, NULL, &tcp->peer, NULL,
501 * Retransmission timer expired
503 * @v timer Retry timer
504 * @v over Failure indicator
506 static void tcp_expired ( struct retry_timer *timer, int over ) {
507 struct tcp_connection *tcp =
508 container_of ( timer, struct tcp_connection, timer );
509 int graceful_close = TCP_CLOSED_GRACEFULLY ( tcp->tcp_state );
511 DBGC ( tcp, "TCP %p timer %s in %s\n", tcp,
512 ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ) );
514 assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
515 ( tcp->tcp_state == TCP_SYN_RCVD ) ||
516 ( tcp->tcp_state == TCP_ESTABLISHED ) ||
517 ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
518 ( tcp->tcp_state == TCP_TIME_WAIT ) ||
519 ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
520 ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
522 if ( over || graceful_close ) {
523 /* If we have finally timed out and given up, or if
524 * this is the result of a graceful close, terminate
527 tcp->tcp_state = TCP_CLOSED;
528 tcp_dump_state ( tcp );
529 tcp_close ( tcp, -ETIMEDOUT );
531 /* Otherwise, retransmit the packet */
537 * Send RST response to incoming packet
539 * @v in_tcphdr TCP header of incoming packet
540 * @ret rc Return status code
542 static int tcp_xmit_reset ( struct tcp_connection *tcp,
543 struct sockaddr_tcpip *st_dest,
544 struct tcp_header *in_tcphdr ) {
545 struct io_buffer *iobuf;
546 struct tcp_header *tcphdr;
548 /* Allocate space for dataless TX buffer */
549 iobuf = alloc_iob ( MAX_HDR_LEN );
551 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
554 iob_reserve ( iobuf, MAX_HDR_LEN );
556 /* Construct RST response */
557 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
558 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
559 tcphdr->src = in_tcphdr->dest;
560 tcphdr->dest = in_tcphdr->src;
561 tcphdr->seq = in_tcphdr->ack;
562 tcphdr->ack = in_tcphdr->seq;
563 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
564 tcphdr->flags = ( TCP_RST | TCP_ACK );
565 tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
566 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
569 DBGC ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4d",
570 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
571 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
572 ntohl ( tcphdr->ack ), 0 );
573 tcp_dump_flags ( tcp, tcphdr->flags );
576 /* Transmit packet */
577 return tcpip_tx ( iobuf, &tcp_protocol, NULL, st_dest,
578 NULL, &tcphdr->csum );
581 /***************************************************************************
585 ***************************************************************************
589 * Identify TCP connection by local port number
591 * @v local_port Local port (in network-endian order)
592 * @ret tcp TCP connection, or NULL
594 static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
595 struct tcp_connection *tcp;
597 list_for_each_entry ( tcp, &tcp_conns, list ) {
598 if ( tcp->local_port == local_port )
605 * Parse TCP received options
607 * @v tcp TCP connection
608 * @v data Raw options data
609 * @v len Raw options length
610 * @v options Options structure to fill in
612 static void tcp_rx_opts ( struct tcp_connection *tcp, const void *data,
613 size_t len, struct tcp_options *options ) {
614 const void *end = ( data + len );
615 const struct tcp_option *option;
618 memset ( options, 0, sizeof ( *options ) );
619 while ( data < end ) {
622 if ( kind == TCP_OPTION_END )
624 if ( kind == TCP_OPTION_NOP ) {
630 options->mssopt = data;
633 options->tsopt = data;
636 DBGC ( tcp, "TCP %p received unknown option %d\n",
640 data += option->length;
645 * Consume received sequence space
647 * @v tcp TCP connection
648 * @v seq_len Sequence space length to consume
650 static void tcp_rx_seq ( struct tcp_connection *tcp, size_t seq_len ) {
651 tcp->rcv_ack += seq_len;
652 if ( tcp->rcv_win > seq_len ) {
653 tcp->rcv_win -= seq_len;
660 * Handle TCP received SYN
662 * @v tcp TCP connection
663 * @v seq SEQ value (in host-endian order)
664 * @v options TCP options
665 * @ret rc Return status code
667 static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq,
668 struct tcp_options *options ) {
670 /* Synchronise sequence numbers on first SYN */
671 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
673 if ( options->tsopt )
677 /* Ignore duplicate SYN */
678 if ( ( tcp->rcv_ack - seq ) > 0 )
681 /* Mark SYN as received and start sending ACKs with each packet */
682 tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
683 TCP_STATE_RCVD ( TCP_SYN ) );
685 /* Acknowledge SYN */
686 tcp_rx_seq ( tcp, 1 );
692 * Handle TCP received ACK
694 * @v tcp TCP connection
695 * @v ack ACK value (in host-endian order)
696 * @v win WIN value (in host-endian order)
697 * @ret rc Return status code
699 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
701 size_t ack_len = ( ack - tcp->snd_seq );
703 unsigned int acked_flags;
705 /* Ignore duplicate or out-of-range ACK */
706 if ( ack_len > tcp->snd_sent ) {
707 DBGC ( tcp, "TCP %p received ACK for [%08x,%08zx), "
708 "sent only [%08x,%08x)\n", tcp, tcp->snd_seq,
709 ( tcp->snd_seq + ack_len ), tcp->snd_seq,
710 ( tcp->snd_seq + tcp->snd_sent ) );
714 /* Acknowledge any flags being sent */
716 acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
717 ( TCP_SYN | TCP_FIN ) );
721 /* Update SEQ and sent counters, and window size */
726 /* Stop the retransmission timer */
727 stop_timer ( &tcp->timer );
729 /* Remove any acknowledged data from transmit queue */
730 tcp_process_queue ( tcp, len, NULL, 1 );
732 /* Mark SYN/FIN as acknowledged if applicable. */
734 tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
736 /* Start sending FIN if we've had all possible data ACKed */
737 if ( list_empty ( &tcp->queue ) && tcp->xfer_closed )
738 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
744 * Handle TCP received data
746 * @v tcp TCP connection
747 * @v seq SEQ value (in host-endian order)
748 * @v iobuf I/O buffer
749 * @ret rc Return status code
751 * This function takes ownership of the I/O buffer.
753 static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
754 struct io_buffer *iobuf ) {
759 /* Ignore duplicate or out-of-order data */
760 already_rcvd = ( tcp->rcv_ack - seq );
761 len = iob_len ( iobuf );
762 if ( already_rcvd >= len ) {
766 iob_pull ( iobuf, already_rcvd );
769 /* Deliver data to application */
770 if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 )
773 /* Acknowledge new data */
774 tcp_rx_seq ( tcp, len );
780 * Handle TCP received FIN
782 * @v tcp TCP connection
783 * @v seq SEQ value (in host-endian order)
784 * @ret rc Return status code
786 static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
788 /* Ignore duplicate or out-of-order FIN */
789 if ( ( tcp->rcv_ack - seq ) > 0 )
792 /* Mark FIN as received and acknowledge it */
793 tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
794 tcp_rx_seq ( tcp, 1 );
796 /* Close connection */
797 tcp_close ( tcp, 0 );
803 * Handle TCP received RST
805 * @v tcp TCP connection
806 * @v seq SEQ value (in host-endian order)
807 * @ret rc Return status code
809 static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
811 /* Accept RST only if it falls within the window. If we have
812 * not yet received a SYN, then we have no window to test
813 * against, so fall back to checking that our SYN has been
816 if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
817 if ( ( seq - tcp->rcv_ack ) >= tcp->rcv_win )
820 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
824 /* Abort connection */
825 tcp->tcp_state = TCP_CLOSED;
826 tcp_dump_state ( tcp );
827 tcp_close ( tcp, -ECONNRESET );
833 * Process received packet
835 * @v iobuf I/O buffer
836 * @v st_src Partially-filled source address
837 * @v st_dest Partially-filled destination address
838 * @v pshdr_csum Pseudo-header checksum
839 * @ret rc Return status code
841 static int tcp_rx ( struct io_buffer *iobuf,
842 struct sockaddr_tcpip *st_src,
843 struct sockaddr_tcpip *st_dest __unused,
844 uint16_t pshdr_csum ) {
845 struct tcp_header *tcphdr = iobuf->data;
846 struct tcp_connection *tcp;
847 struct tcp_options options;
858 /* Sanity check packet */
859 if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
860 DBG ( "TCP packet too short at %zd bytes (min %zd bytes)\n",
861 iob_len ( iobuf ), sizeof ( *tcphdr ) );
865 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
866 if ( hlen < sizeof ( *tcphdr ) ) {
867 DBG ( "TCP header too short at %zd bytes (min %zd bytes)\n",
868 hlen, sizeof ( *tcphdr ) );
872 if ( hlen > iob_len ( iobuf ) ) {
873 DBG ( "TCP header too long at %zd bytes (max %zd bytes)\n",
874 hlen, iob_len ( iobuf ) );
878 csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data,
881 DBG ( "TCP checksum incorrect (is %04x including checksum "
882 "field, should be 0000)\n", csum );
887 /* Parse parameters from header and strip header */
888 tcp = tcp_demux ( tcphdr->dest );
889 start_seq = seq = ntohl ( tcphdr->seq );
890 ack = ntohl ( tcphdr->ack );
891 win = ntohs ( tcphdr->win );
892 flags = tcphdr->flags;
893 tcp_rx_opts ( tcp, ( ( ( void * ) tcphdr ) + sizeof ( *tcphdr ) ),
894 ( hlen - sizeof ( *tcphdr ) ), &options );
895 iob_pull ( iobuf, hlen );
896 len = iob_len ( iobuf );
899 DBGC ( tcp, "TCP %p RX %d<-%d %08x %08x..%08zx %4zd",
900 tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
901 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
902 ( ntohl ( tcphdr->seq ) + len +
903 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
904 tcp_dump_flags ( tcp, tcphdr->flags );
907 /* If no connection was found, send RST */
909 tcp_xmit_reset ( tcp, st_src, tcphdr );
914 /* Handle ACK, if present */
915 if ( flags & TCP_ACK ) {
916 if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
917 tcp_xmit_reset ( tcp, st_src, tcphdr );
922 /* Handle SYN, if present */
923 if ( flags & TCP_SYN ) {
924 tcp_rx_syn ( tcp, seq, &options );
928 /* Handle RST, if present */
929 if ( flags & TCP_RST ) {
930 if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
934 /* Handle new data, if any */
935 tcp_rx_data ( tcp, seq, iobuf );
938 /* Handle FIN, if present */
939 if ( flags & TCP_FIN ) {
940 tcp_rx_fin ( tcp, seq );
944 /* Update timestamp, if present and applicable */
945 if ( ( seq == tcp->rcv_ack ) && options.tsopt )
946 tcp->ts_recent = ntohl ( options.tsopt->tsval );
948 /* Dump out any state change as a result of the received packet */
949 tcp_dump_state ( tcp );
951 /* Send out any pending data. We force sending a reply if either
953 * a) the peer is expecting an ACK (i.e. consumed sequence space), or
954 * b) either end of the packet was outside the receive window
956 * Case (b) enables us to support TCP keepalives using
957 * zero-length packets, which we would otherwise ignore. Note
958 * that for case (b), we need *only* consider zero-length
959 * packets, since non-zero-length packets will already be
960 * caught by case (a).
962 tcp_xmit ( tcp, ( ( start_seq != seq ) ||
963 ( ( seq - tcp->rcv_ack ) > tcp->rcv_win ) ) );
965 /* If this packet was the last we expect to receive, set up
966 * timer to expire and cause the connection to be freed.
968 if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
969 tcp->timer.timeout = ( 2 * TCP_MSL );
970 start_timer ( &tcp->timer );
976 /* Free received packet */
982 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
985 .tcpip_proto = IP_TCP,
988 /***************************************************************************
990 * Data transfer interface
992 ***************************************************************************
998 * @v xfer Data transfer interface
999 * @v rc Reason for close
1001 static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
1002 struct tcp_connection *tcp =
1003 container_of ( xfer, struct tcp_connection, xfer );
1005 /* Close data transfer interface */
1006 tcp_close ( tcp, rc );
1008 /* Transmit FIN, if possible */
1009 tcp_xmit ( tcp, 0 );
1013 * Check flow control window
1015 * @v xfer Data transfer interface
1016 * @ret len Length of window
1018 static size_t tcp_xfer_window ( struct xfer_interface *xfer ) {
1019 struct tcp_connection *tcp =
1020 container_of ( xfer, struct tcp_connection, xfer );
1022 /* Not ready if data queue is non-empty. This imposes a limit
1023 * of only one unACKed packet in the TX queue at any time; we
1024 * do this to conserve memory usage.
1026 if ( ! list_empty ( &tcp->queue ) )
1029 /* Return TCP window length */
1030 return tcp_xmit_win ( tcp );
1034 * Deliver datagram as I/O buffer
1036 * @v xfer Data transfer interface
1037 * @v iobuf Datagram I/O buffer
1038 * @v meta Data transfer metadata
1039 * @ret rc Return status code
1041 static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
1042 struct io_buffer *iobuf,
1043 struct xfer_metadata *meta __unused ) {
1044 struct tcp_connection *tcp =
1045 container_of ( xfer, struct tcp_connection, xfer );
1047 /* Enqueue packet */
1048 list_add_tail ( &iobuf->list, &tcp->queue );
1050 /* Transmit data, if possible */
1051 tcp_xmit ( tcp, 0 );
1056 /** TCP data transfer interface operations */
1057 static struct xfer_interface_operations tcp_xfer_operations = {
1058 .close = tcp_xfer_close,
1059 .vredirect = ignore_xfer_vredirect,
1060 .window = tcp_xfer_window,
1061 .alloc_iob = default_xfer_alloc_iob,
1062 .deliver_iob = tcp_xfer_deliver_iob,
1063 .deliver_raw = xfer_deliver_as_iob,
1066 /***************************************************************************
1070 ***************************************************************************
1073 /** TCP socket opener */
1074 struct socket_opener tcp_socket_opener __socket_opener = {
1075 .semantics = TCP_SOCK_STREAM,
1081 int tcp_sock_stream = TCP_SOCK_STREAM;
1086 * @v xfer Data transfer interface
1088 * @ret rc Return status code
1090 static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
1091 struct sockaddr_tcpip peer;
1097 memset ( &peer, 0, sizeof ( peer ) );
1098 peer.st_port = htons ( uri_port ( uri, 0 ) );
1099 return xfer_open_named_socket ( xfer, SOCK_STREAM,
1100 ( struct sockaddr * ) &peer,
1104 /** TCP URI opener */
1105 struct uri_opener tcp_uri_opener __uri_opener = {
1107 .open = tcp_open_uri,