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>
14 #include <gpxe/tcpip.h>
23 /** A TCP connection */
24 struct tcp_connection {
25 /** Reference counter */
27 /** List of TCP connections */
28 struct list_head list;
30 /** Data transfer interface */
31 struct xfer_interface xfer;
32 /** Data transfer interface closed flag */
35 /** Remote socket address */
36 struct sockaddr_tcpip peer;
37 /** Local port, in network byte order */
38 unsigned int local_port;
40 /** Current TCP state */
41 unsigned int tcp_state;
42 /** Previous TCP state
44 * Maintained only for debug messages
46 unsigned int prev_tcp_state;
47 /** Current sequence number
49 * Equivalent to SND.UNA in RFC 793 terminology.
52 /** Unacknowledged sequence count
54 * Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
59 * Equivalent to SND.WND in RFC 793 terminology
62 /** Current acknowledgement number
64 * Equivalent to RCV.NXT in RFC 793 terminology.
69 struct list_head queue;
70 /** Retransmission timer */
71 struct retry_timer timer;
75 * List of registered TCP connections
77 static LIST_HEAD ( tcp_conns );
79 /* Forward declarations */
80 static struct xfer_interface_operations tcp_xfer_operations;
81 static void tcp_expired ( struct retry_timer *timer, int over );
82 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
89 * @ret name Name of TCP state
91 static inline __attribute__ (( always_inline )) const char *
92 tcp_state ( int state ) {
94 case TCP_CLOSED: return "CLOSED";
95 case TCP_LISTEN: return "LISTEN";
96 case TCP_SYN_SENT: return "SYN_SENT";
97 case TCP_SYN_RCVD: return "SYN_RCVD";
98 case TCP_ESTABLISHED: return "ESTABLISHED";
99 case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
100 case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
101 case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
102 case TCP_TIME_WAIT: return "TIME_WAIT";
103 case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
104 default: return "INVALID";
109 * Dump TCP state transition
111 * @v tcp TCP connection
113 static inline __attribute__ (( always_inline )) void
114 tcp_dump_state ( struct tcp_connection *tcp ) {
116 if ( tcp->tcp_state != tcp->prev_tcp_state ) {
117 DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
118 tcp_state ( tcp->prev_tcp_state ),
119 tcp_state ( tcp->tcp_state ) );
121 tcp->prev_tcp_state = tcp->tcp_state;
129 static inline __attribute__ (( always_inline )) void
130 tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
131 if ( flags & TCP_RST )
132 DBGC ( tcp, " RST" );
133 if ( flags & TCP_SYN )
134 DBGC ( tcp, " SYN" );
135 if ( flags & TCP_PSH )
136 DBGC ( tcp, " PSH" );
137 if ( flags & TCP_FIN )
138 DBGC ( tcp, " FIN" );
139 if ( flags & TCP_ACK )
140 DBGC ( tcp, " ACK" );
143 /***************************************************************************
147 ***************************************************************************
151 * Bind TCP connection to local port
153 * @v tcp TCP connection
154 * @v port Local port number, in network-endian order
155 * @ret rc Return status code
157 * If the port is 0, the connection is assigned an available port
158 * between 1024 and 65535.
160 static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
161 struct tcp_connection *existing;
162 static uint16_t try_port = 1024;
164 /* If no port specified, find the first available port */
166 for ( ; try_port ; try_port++ ) {
167 if ( try_port < 1024 )
169 if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 )
172 DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
176 /* Attempt bind to local port */
177 list_for_each_entry ( existing, &tcp_conns, list ) {
178 if ( existing->local_port == port ) {
179 DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
180 tcp, ntohs ( port ) );
184 tcp->local_port = port;
186 DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( port ) );
191 * Open a TCP connection
193 * @v xfer Data transfer interface
194 * @v peer Peer socket address
195 * @v local Local socket address, or NULL
196 * @ret rc Return status code
198 static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
199 struct sockaddr *local ) {
200 struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
201 struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
202 struct tcp_connection *tcp;
203 unsigned int bind_port;
206 /* Allocate and initialise structure */
207 tcp = malloc ( sizeof ( *tcp ) );
210 DBGC ( tcp, "TCP %p allocated\n", tcp );
211 memset ( tcp, 0, sizeof ( *tcp ) );
212 xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
213 tcp->tcp_state = tcp->prev_tcp_state = TCP_CLOSED;
214 tcp->snd_seq = random();
215 INIT_LIST_HEAD ( &tcp->queue );
216 tcp->timer.expired = tcp_expired;
217 memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
219 /* Bind to local port */
220 bind_port = ( st_local ? st_local->st_port : 0 );
221 if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
224 /* Attach parent interface, transfer reference to connection
227 xfer_plug_plug ( &tcp->xfer, xfer );
228 list_add ( &tcp->list, &tcp_conns );
232 ref_put ( &tcp->refcnt );
237 * Close TCP connection
239 * @v tcp TCP connection
240 * @v rc Reason for close
242 * Closes the data transfer interface. If the TCP state machine is in
243 * a suitable state, the connection will be deleted.
245 static void tcp_close ( struct tcp_connection *tcp, int rc ) {
246 struct io_buffer *iobuf;
247 struct io_buffer *tmp;
249 /* Close data transfer interface */
250 xfer_nullify ( &tcp->xfer );
251 xfer_close ( &tcp->xfer, rc );
252 tcp->xfer_closed = 1;
254 /* If we are in CLOSED, or have otherwise not yet received a
255 * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
258 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
260 /* Transition to CLOSED for the sake of debugging messages */
261 tcp->tcp_state = TCP_CLOSED;
262 tcp_dump_state ( tcp );
264 /* Free any unsent I/O buffers */
265 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
266 list_del ( &iobuf->list );
270 /* Remove from list and drop reference */
271 stop_timer ( &tcp->timer );
272 list_del ( &tcp->list );
273 ref_put ( &tcp->refcnt );
277 /* If we have not had our SYN acknowledged (i.e. we are in
278 * SYN_RCVD), pretend that it has been acknowledged so that we
279 * can send a FIN without breaking things.
281 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
282 tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
284 /* If we have no data remaining to send, start sending FIN */
285 if ( list_empty ( &tcp->queue ) ) {
286 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
287 tcp_dump_state ( tcp );
291 /***************************************************************************
295 ***************************************************************************
299 * Process TCP transmit queue
301 * @v tcp TCP connection
302 * @v max_len Maximum length to process
303 * @v dest I/O buffer to fill with data, or NULL
304 * @v remove Remove data from queue
305 * @ret len Length of data processed
307 * This processes at most @c max_len bytes from the TCP connection's
308 * transmit queue. Data will be copied into the @c dest I/O buffer
309 * (if provided) and, if @c remove is true, removed from the transmit
312 static size_t tcp_process_queue ( struct tcp_connection *tcp, size_t max_len,
313 struct io_buffer *dest, int remove ) {
314 struct io_buffer *iobuf;
315 struct io_buffer *tmp;
319 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
320 frag_len = iob_len ( iobuf );
321 if ( frag_len > max_len )
324 memcpy ( iob_put ( dest, frag_len ), iobuf->data,
328 iob_pull ( iobuf, frag_len );
329 if ( ! iob_len ( iobuf ) ) {
330 list_del ( &iobuf->list );
341 * Transmit any outstanding data
343 * @v tcp TCP connection
344 * @v force_send Force sending of packet
346 * Transmits any outstanding data on the connection.
348 * Note that even if an error is returned, the retransmission timer
349 * will have been started if necessary, and so the stack will
350 * eventually attempt to retransmit the failed packet.
352 static int tcp_xmit ( struct tcp_connection *tcp, int force_send ) {
353 struct io_buffer *iobuf;
354 struct tcp_header *tcphdr;
355 struct tcp_mss_option *mssopt;
363 /* Calculate both the actual (payload) and sequence space
364 * lengths that we wish to transmit.
366 if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
367 len = tcp_process_queue ( tcp, tcp->snd_win, NULL, 0 );
370 flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
371 if ( flags & ( TCP_SYN | TCP_FIN ) ) {
372 /* SYN or FIN consume one byte, and we can never send both */
373 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
376 tcp->snd_sent = seq_len;
378 /* If we have nothing to transmit, stop now */
379 if ( ( seq_len == 0 ) && ! force_send )
382 /* If we are transmitting anything that requires
383 * acknowledgement (i.e. consumes sequence space), start the
384 * retransmission timer. Do this before attempting to
385 * allocate the I/O buffer, in case allocation itself fails.
388 start_timer ( &tcp->timer );
390 /* Allocate I/O buffer */
391 iobuf = alloc_iob ( len + MAX_HDR_LEN );
393 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
396 iob_reserve ( iobuf, MAX_HDR_LEN );
398 /* Fill data payload from transmit queue */
399 tcp_process_queue ( tcp, len, iobuf, 0 );
401 /* Estimate window size */
402 window = ( ( freemem * 3 ) / 4 );
403 if ( window > TCP_MAX_WINDOW_SIZE )
404 window = TCP_MAX_WINDOW_SIZE;
405 window &= ~0x03; /* Keep everything dword-aligned */
407 /* Fill up the TCP header */
408 payload = iobuf->data;
409 if ( flags & TCP_SYN ) {
410 mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
411 mssopt->kind = TCP_OPTION_MSS;
412 mssopt->length = sizeof ( *mssopt );
413 mssopt->mss = htons ( TCP_MSS );
415 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
416 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
417 tcphdr->src = tcp->local_port;
418 tcphdr->dest = tcp->peer.st_port;
419 tcphdr->seq = htonl ( tcp->snd_seq );
420 tcphdr->ack = htonl ( tcp->rcv_ack );
421 tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
422 tcphdr->flags = flags;
423 tcphdr->win = htons ( window );
424 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
427 DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
428 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
429 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
430 ntohl ( tcphdr->ack ), len );
431 tcp_dump_flags ( tcp, tcphdr->flags );
434 /* Transmit packet */
435 rc = tcpip_tx ( iobuf, &tcp_protocol, &tcp->peer, NULL, &tcphdr->csum );
437 /* If we got -ENETUNREACH, kill the connection immediately
438 * because there is no point retrying. This isn't strictly
439 * necessary (since we will eventually time out anyway), but
440 * it avoids irritating needless delays. Don't do this for
441 * RST packets transmitted on connection abort, to avoid a
442 * potential infinite loop.
444 if ( ( ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_RST ) ) ) &&
445 ( rc == -ENETUNREACH ) ) {
446 DBGC ( tcp, "TCP %p aborting after TX failed: %s\n",
447 tcp, strerror ( rc ) );
448 tcp->tcp_state = TCP_CLOSED;
449 tcp_dump_state ( tcp );
450 tcp_close ( tcp, rc );
457 * Retransmission timer expired
459 * @v timer Retry timer
460 * @v over Failure indicator
462 static void tcp_expired ( struct retry_timer *timer, int over ) {
463 struct tcp_connection *tcp =
464 container_of ( timer, struct tcp_connection, timer );
465 int graceful_close = TCP_CLOSED_GRACEFULLY ( tcp->tcp_state );
467 DBGC ( tcp, "TCP %p timer %s in %s\n", tcp,
468 ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ) );
470 assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
471 ( tcp->tcp_state == TCP_SYN_RCVD ) ||
472 ( tcp->tcp_state == TCP_ESTABLISHED ) ||
473 ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
474 ( tcp->tcp_state == TCP_TIME_WAIT ) ||
475 ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
476 ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
478 if ( over || graceful_close ) {
479 /* If we have finally timed out and given up, or if
480 * this is the result of a graceful close, terminate
483 tcp->tcp_state = TCP_CLOSED;
484 tcp_dump_state ( tcp );
485 tcp_close ( tcp, -ETIMEDOUT );
487 /* Otherwise, retransmit the packet */
493 * Send RST response to incoming packet
495 * @v in_tcphdr TCP header of incoming packet
496 * @ret rc Return status code
498 static int tcp_xmit_reset ( struct tcp_connection *tcp,
499 struct sockaddr_tcpip *st_dest,
500 struct tcp_header *in_tcphdr ) {
501 struct io_buffer *iobuf;
502 struct tcp_header *tcphdr;
504 /* Allocate space for dataless TX buffer */
505 iobuf = alloc_iob ( MAX_HDR_LEN );
507 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
510 iob_reserve ( iobuf, MAX_HDR_LEN );
512 /* Construct RST response */
513 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
514 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
515 tcphdr->src = in_tcphdr->dest;
516 tcphdr->dest = in_tcphdr->src;
517 tcphdr->seq = in_tcphdr->ack;
518 tcphdr->ack = in_tcphdr->seq;
519 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
520 tcphdr->flags = ( TCP_RST | TCP_ACK );
521 tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
522 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
525 DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
526 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
527 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
528 ntohl ( tcphdr->ack ), 0 );
529 tcp_dump_flags ( tcp, tcphdr->flags );
532 /* Transmit packet */
533 return tcpip_tx ( iobuf, &tcp_protocol, st_dest,
534 NULL, &tcphdr->csum );
537 /***************************************************************************
541 ***************************************************************************
545 * Identify TCP connection by local port number
547 * @v local_port Local port (in network-endian order)
548 * @ret tcp TCP connection, or NULL
550 static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
551 struct tcp_connection *tcp;
553 list_for_each_entry ( tcp, &tcp_conns, list ) {
554 if ( tcp->local_port == local_port )
561 * Handle TCP received SYN
563 * @v tcp TCP connection
564 * @v seq SEQ value (in host-endian order)
565 * @ret rc Return status code
567 static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq ) {
569 /* Synchronise sequence numbers on first SYN */
570 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) )
573 /* Ignore duplicate SYN */
574 if ( ( tcp->rcv_ack - seq ) > 0 )
577 /* Mark SYN as received and start sending ACKs with each packet */
578 tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
579 TCP_STATE_RCVD ( TCP_SYN ) );
581 /* Acknowledge SYN */
588 * Handle TCP received ACK
590 * @v tcp TCP connection
591 * @v ack ACK value (in host-endian order)
592 * @v win WIN value (in host-endian order)
593 * @ret rc Return status code
595 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
597 size_t ack_len = ( ack - tcp->snd_seq );
599 unsigned int acked_flags;
601 /* Ignore duplicate or out-of-range ACK */
602 if ( ack_len > tcp->snd_sent ) {
603 DBGC ( tcp, "TCP %p received ACK for [%08lx,%08lx), "
604 "sent only [%08lx,%08lx)\n", tcp, tcp->snd_seq,
605 ( tcp->snd_seq + ack_len ), tcp->snd_seq,
606 ( tcp->snd_seq + tcp->snd_sent ) );
610 /* Acknowledge any flags being sent */
612 acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
613 ( TCP_SYN | TCP_FIN ) );
617 /* Update SEQ and sent counters, and window size */
622 /* Stop the retransmission timer */
623 stop_timer ( &tcp->timer );
625 /* Remove any acknowledged data from transmit queue */
626 tcp_process_queue ( tcp, len, NULL, 1 );
628 /* Mark SYN/FIN as acknowledged if applicable. */
630 tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
632 /* Start sending FIN if we've had all possible data ACKed */
633 if ( list_empty ( &tcp->queue ) && tcp->xfer_closed )
634 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
640 * Handle TCP received data
642 * @v tcp TCP connection
643 * @v seq SEQ value (in host-endian order)
644 * @v iobuf I/O buffer
645 * @ret rc Return status code
647 * This function takes ownership of the I/O buffer.
649 static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
650 struct io_buffer *iobuf ) {
655 /* Ignore duplicate data */
656 already_rcvd = ( tcp->rcv_ack - seq );
657 len = iob_len ( iobuf );
658 if ( already_rcvd >= len ) {
662 iob_pull ( iobuf, already_rcvd );
664 /* Deliver data to application */
665 if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 )
668 /* Acknowledge new data */
674 * Handle TCP received FIN
676 * @v tcp TCP connection
677 * @v seq SEQ value (in host-endian order)
678 * @ret rc Return status code
680 static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
682 /* Ignore duplicate FIN */
683 if ( ( tcp->rcv_ack - seq ) > 0 )
686 /* Mark FIN as received and acknowledge it */
687 tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
690 /* Close connection */
691 tcp_close ( tcp, 0 );
697 * Handle TCP received RST
699 * @v tcp TCP connection
700 * @v seq SEQ value (in host-endian order)
701 * @ret rc Return status code
703 static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
705 /* Accept RST only if it falls within the window. If we have
706 * not yet received a SYN, then we have no window to test
707 * against, so fall back to checking that our SYN has been
710 if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
711 if ( ( tcp->rcv_ack - seq ) > 0 )
714 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
718 /* Abort connection */
719 tcp->tcp_state = TCP_CLOSED;
720 tcp_dump_state ( tcp );
721 tcp_close ( tcp, -ECONNRESET );
727 * Process received packet
729 * @v iobuf I/O buffer
730 * @v st_src Partially-filled source address
731 * @v st_dest Partially-filled destination address
732 * @v pshdr_csum Pseudo-header checksum
733 * @ret rc Return status code
735 static int tcp_rx ( struct io_buffer *iobuf,
736 struct sockaddr_tcpip *st_src,
737 struct sockaddr_tcpip *st_dest __unused,
738 uint16_t pshdr_csum ) {
739 struct tcp_header *tcphdr = iobuf->data;
740 struct tcp_connection *tcp;
751 /* Sanity check packet */
752 if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
753 DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
754 iob_len ( iobuf ), sizeof ( *tcphdr ) );
758 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
759 if ( hlen < sizeof ( *tcphdr ) ) {
760 DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
761 hlen, sizeof ( *tcphdr ) );
765 if ( hlen > iob_len ( iobuf ) ) {
766 DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
767 hlen, iob_len ( iobuf ) );
771 csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, iob_len ( iobuf ));
773 DBG ( "TCP checksum incorrect (is %04x including checksum "
774 "field, should be 0000)\n", csum );
779 /* Parse parameters from header and strip header */
780 tcp = tcp_demux ( tcphdr->dest );
781 start_seq = seq = ntohl ( tcphdr->seq );
782 ack = ntohl ( tcphdr->ack );
783 win = ntohs ( tcphdr->win );
784 flags = tcphdr->flags;
785 iob_pull ( iobuf, hlen );
786 len = iob_len ( iobuf );
789 DBGC ( tcp, "TCP %p RX %d<-%d %08lx %08lx..%08lx %4zd",
790 tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
791 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
792 ( ntohl ( tcphdr->seq ) + len +
793 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
794 tcp_dump_flags ( tcp, tcphdr->flags );
797 /* If no connection was found, send RST */
799 tcp_xmit_reset ( tcp, st_src, tcphdr );
804 /* Handle ACK, if present */
805 if ( flags & TCP_ACK ) {
806 if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
807 tcp_xmit_reset ( tcp, st_src, tcphdr );
812 /* Handle SYN, if present */
813 if ( flags & TCP_SYN ) {
814 tcp_rx_syn ( tcp, seq );
818 /* Handle RST, if present */
819 if ( flags & TCP_RST ) {
820 if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
824 /* Handle new data, if any */
825 tcp_rx_data ( tcp, seq, iobuf );
828 /* Handle FIN, if present */
829 if ( flags & TCP_FIN ) {
830 tcp_rx_fin ( tcp, seq );
834 /* Dump out any state change as a result of the received packet */
835 tcp_dump_state ( tcp );
837 /* Send out any pending data. If peer is expecting an ACK for
838 * this packet then force sending a reply.
840 tcp_xmit ( tcp, ( start_seq != seq ) );
842 /* If this packet was the last we expect to receive, set up
843 * timer to expire and cause the connection to be freed.
845 if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
846 tcp->timer.timeout = ( 2 * TCP_MSL );
847 start_timer ( &tcp->timer );
853 /* Free received packet */
859 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
862 .tcpip_proto = IP_TCP,
865 /***************************************************************************
867 * Data transfer interface
869 ***************************************************************************
873 * Ensure TCP connection has started
875 * @v tcp TCP connection
877 static void tcp_ensure_started ( struct tcp_connection *tcp ) {
878 if ( ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_SYN ) ) ) {
879 tcp->tcp_state = TCP_SYN_SENT;
880 tcp_dump_state ( tcp );
888 * @v xfer Data transfer interface
889 * @v rc Reason for close
891 static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
892 struct tcp_connection *tcp =
893 container_of ( xfer, struct tcp_connection, xfer );
895 /* Close data transfer interface */
896 tcp_close ( tcp, rc );
898 /* Transmit FIN, if possible */
905 * @v xfer Data transfer interface
906 * @v offset Offset to new position
907 * @v whence Basis for new position
908 * @v len Length of requested data
909 * @ret rc Return status code
911 static int tcp_xfer_request ( struct xfer_interface *xfer,
912 off_t offset __unused, int whence __unused,
913 size_t len __unused ) {
914 struct tcp_connection *tcp =
915 container_of ( xfer, struct tcp_connection, xfer );
917 /* Ensure connection has been started */
918 tcp_ensure_started ( tcp );
926 * @v xfer Data transfer interface
927 * @v offset Offset to new position
928 * @v whence Basis for new position
929 * @ret rc Return status code
931 static int tcp_xfer_seek ( struct xfer_interface *xfer, off_t offset,
933 struct tcp_connection *tcp =
934 container_of ( xfer, struct tcp_connection, xfer );
936 /* Ensure connection has been started */
937 tcp_ensure_started ( tcp );
939 /* TCP doesn't support seeking to arbitrary positions */
940 if ( ( whence != SEEK_CUR ) || ( offset != 0 ) )
943 /* Not ready if we're not in a suitable connection state */
944 if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
947 /* Not ready if data queue is non-empty */
948 if ( ! list_empty ( &tcp->queue ) )
955 * Deliver datagram as I/O buffer
957 * @v xfer Data transfer interface
958 * @v iobuf Datagram I/O buffer
959 * @ret rc Return status code
961 static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
962 struct io_buffer *iobuf ) {
963 struct tcp_connection *tcp =
964 container_of ( xfer, struct tcp_connection, xfer );
966 /* Ensure connection has been started */
967 tcp_ensure_started ( tcp );
970 list_add_tail ( &iobuf->list, &tcp->queue );
972 /* Transmit data, if possible */
978 /** TCP data transfer interface operations */
979 static struct xfer_interface_operations tcp_xfer_operations = {
980 .close = tcp_xfer_close,
981 .vredirect = ignore_xfer_vredirect,
982 .request = tcp_xfer_request,
983 .seek = tcp_xfer_seek,
984 .alloc_iob = default_xfer_alloc_iob,
985 .deliver_iob = tcp_xfer_deliver_iob,
986 .deliver_raw = xfer_deliver_as_iob,
989 /***************************************************************************
993 ***************************************************************************
996 #warning "Placeholder URI opener"
997 static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
998 struct sockaddr_in peer;
1000 memset ( &peer, 0, sizeof ( peer ) );
1001 peer.sin_family = AF_INET;
1002 peer.sin_addr.s_addr = htonl ( 0x0afefe02 );
1003 peer.sin_port = htons ( 12345 );
1004 return tcp_open ( xfer, &peer, NULL );
1007 /** TCP URI opener */
1008 struct uri_opener tcp_uri_opener __uri_opener = {
1010 .open = tcp_open_uri,
1013 /** TCP socket opener */
1014 struct socket_opener tcp_socket_opener __socket_opener = {
1016 .type = SOCK_STREAM,