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 struct list_head queue;
71 /** Retransmission timer */
72 struct retry_timer timer;
76 * List of registered TCP connections
78 static LIST_HEAD ( tcp_conns );
80 /* Forward declarations */
81 static struct xfer_interface_operations tcp_xfer_operations;
82 static void tcp_expired ( struct retry_timer *timer, int over );
83 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
90 * @ret name Name of TCP state
92 static inline __attribute__ (( always_inline )) const char *
93 tcp_state ( int state ) {
95 case TCP_CLOSED: return "CLOSED";
96 case TCP_LISTEN: return "LISTEN";
97 case TCP_SYN_SENT: return "SYN_SENT";
98 case TCP_SYN_RCVD: return "SYN_RCVD";
99 case TCP_ESTABLISHED: return "ESTABLISHED";
100 case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
101 case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
102 case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
103 case TCP_TIME_WAIT: return "TIME_WAIT";
104 case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
105 default: return "INVALID";
110 * Dump TCP state transition
112 * @v tcp TCP connection
114 static inline __attribute__ (( always_inline )) void
115 tcp_dump_state ( struct tcp_connection *tcp ) {
117 if ( tcp->tcp_state != tcp->prev_tcp_state ) {
118 DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
119 tcp_state ( tcp->prev_tcp_state ),
120 tcp_state ( tcp->tcp_state ) );
122 tcp->prev_tcp_state = tcp->tcp_state;
130 static inline __attribute__ (( always_inline )) void
131 tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
132 if ( flags & TCP_RST )
133 DBGC ( tcp, " RST" );
134 if ( flags & TCP_SYN )
135 DBGC ( tcp, " SYN" );
136 if ( flags & TCP_PSH )
137 DBGC ( tcp, " PSH" );
138 if ( flags & TCP_FIN )
139 DBGC ( tcp, " FIN" );
140 if ( flags & TCP_ACK )
141 DBGC ( tcp, " ACK" );
144 /***************************************************************************
148 ***************************************************************************
152 * Bind TCP connection to local port
154 * @v tcp TCP connection
155 * @v port Local port number, in network-endian order
156 * @ret rc Return status code
158 * If the port is 0, the connection is assigned an available port
159 * between 1024 and 65535.
161 static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
162 struct tcp_connection *existing;
163 static uint16_t try_port = 1024;
165 /* If no port specified, find the first available port */
167 for ( ; try_port ; try_port++ ) {
168 if ( try_port < 1024 )
170 if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 )
173 DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
177 /* Attempt bind to local port */
178 list_for_each_entry ( existing, &tcp_conns, list ) {
179 if ( existing->local_port == port ) {
180 DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
181 tcp, ntohs ( port ) );
185 tcp->local_port = port;
187 DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( port ) );
192 * Open a TCP connection
194 * @v xfer Data transfer interface
195 * @v peer Peer socket address
196 * @v local Local socket address, or NULL
197 * @ret rc Return status code
199 static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
200 struct sockaddr *local ) {
201 struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
202 struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
203 struct tcp_connection *tcp;
204 unsigned int bind_port;
207 /* Allocate and initialise structure */
208 tcp = malloc ( sizeof ( *tcp ) );
211 DBGC ( tcp, "TCP %p allocated\n", tcp );
212 memset ( tcp, 0, sizeof ( *tcp ) );
213 xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
214 tcp->prev_tcp_state = TCP_CLOSED;
215 tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
216 tcp_dump_state ( tcp );
217 tcp->snd_seq = random();
218 INIT_LIST_HEAD ( &tcp->queue );
219 tcp->timer.expired = tcp_expired;
220 memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
222 /* Bind to local port */
223 bind_port = ( st_local ? st_local->st_port : 0 );
224 if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
227 /* Start timer to initiate SYN */
228 start_timer ( &tcp->timer );
230 /* Attach parent interface, transfer reference to connection
233 xfer_plug_plug ( &tcp->xfer, xfer );
234 list_add ( &tcp->list, &tcp_conns );
238 ref_put ( &tcp->refcnt );
243 * Close TCP connection
245 * @v tcp TCP connection
246 * @v rc Reason for close
248 * Closes the data transfer interface. If the TCP state machine is in
249 * a suitable state, the connection will be deleted.
251 static void tcp_close ( struct tcp_connection *tcp, int rc ) {
252 struct io_buffer *iobuf;
253 struct io_buffer *tmp;
255 /* Close data transfer interface */
256 xfer_nullify ( &tcp->xfer );
257 xfer_close ( &tcp->xfer, rc );
258 tcp->xfer_closed = 1;
260 /* If we are in CLOSED, or have otherwise not yet received a
261 * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
264 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
266 /* Transition to CLOSED for the sake of debugging messages */
267 tcp->tcp_state = TCP_CLOSED;
268 tcp_dump_state ( tcp );
270 /* Free any unsent I/O buffers */
271 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
272 list_del ( &iobuf->list );
276 /* Remove from list and drop reference */
277 stop_timer ( &tcp->timer );
278 list_del ( &tcp->list );
279 ref_put ( &tcp->refcnt );
280 DBGC ( tcp, "TCP %p connection deleted\n", tcp );
284 /* If we have not had our SYN acknowledged (i.e. we are in
285 * SYN_RCVD), pretend that it has been acknowledged so that we
286 * can send a FIN without breaking things.
288 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
289 tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
291 /* If we have no data remaining to send, start sending FIN */
292 if ( list_empty ( &tcp->queue ) ) {
293 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
294 tcp_dump_state ( tcp );
298 /***************************************************************************
302 ***************************************************************************
306 * Process TCP transmit queue
308 * @v tcp TCP connection
309 * @v max_len Maximum length to process
310 * @v dest I/O buffer to fill with data, or NULL
311 * @v remove Remove data from queue
312 * @ret len Length of data processed
314 * This processes at most @c max_len bytes from the TCP connection's
315 * transmit queue. Data will be copied into the @c dest I/O buffer
316 * (if provided) and, if @c remove is true, removed from the transmit
319 static size_t tcp_process_queue ( struct tcp_connection *tcp, size_t max_len,
320 struct io_buffer *dest, int remove ) {
321 struct io_buffer *iobuf;
322 struct io_buffer *tmp;
326 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
327 frag_len = iob_len ( iobuf );
328 if ( frag_len > max_len )
331 memcpy ( iob_put ( dest, frag_len ), iobuf->data,
335 iob_pull ( iobuf, frag_len );
336 if ( ! iob_len ( iobuf ) ) {
337 list_del ( &iobuf->list );
348 * Transmit any outstanding data
350 * @v tcp TCP connection
351 * @v force_send Force sending of packet
353 * Transmits any outstanding data on the connection.
355 * Note that even if an error is returned, the retransmission timer
356 * will have been started if necessary, and so the stack will
357 * eventually attempt to retransmit the failed packet.
359 static int tcp_xmit ( struct tcp_connection *tcp, int force_send ) {
360 struct io_buffer *iobuf;
361 struct tcp_header *tcphdr;
362 struct tcp_mss_option *mssopt;
370 /* If retransmission timer is already running, do nothing */
371 if ( timer_running ( &tcp->timer ) )
374 /* Calculate both the actual (payload) and sequence space
375 * lengths that we wish to transmit.
377 if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
378 len = tcp_process_queue ( tcp, tcp->snd_win, NULL, 0 );
381 flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
382 if ( flags & ( TCP_SYN | TCP_FIN ) ) {
383 /* SYN or FIN consume one byte, and we can never send both */
384 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
387 tcp->snd_sent = seq_len;
389 /* If we have nothing to transmit, stop now */
390 if ( ( seq_len == 0 ) && ! force_send )
393 /* If we are transmitting anything that requires
394 * acknowledgement (i.e. consumes sequence space), start the
395 * retransmission timer. Do this before attempting to
396 * allocate the I/O buffer, in case allocation itself fails.
399 start_timer ( &tcp->timer );
401 /* Allocate I/O buffer */
402 iobuf = alloc_iob ( len + MAX_HDR_LEN );
404 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
407 iob_reserve ( iobuf, MAX_HDR_LEN );
409 /* Fill data payload from transmit queue */
410 tcp_process_queue ( tcp, len, iobuf, 0 );
412 /* Estimate window size */
413 window = ( ( freemem * 3 ) / 4 );
414 if ( window > TCP_MAX_WINDOW_SIZE )
415 window = TCP_MAX_WINDOW_SIZE;
416 window &= ~0x03; /* Keep everything dword-aligned */
418 /* Fill up the TCP header */
419 payload = iobuf->data;
420 if ( flags & TCP_SYN ) {
421 mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
422 mssopt->kind = TCP_OPTION_MSS;
423 mssopt->length = sizeof ( *mssopt );
424 mssopt->mss = htons ( TCP_MSS );
426 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
427 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
428 tcphdr->src = tcp->local_port;
429 tcphdr->dest = tcp->peer.st_port;
430 tcphdr->seq = htonl ( tcp->snd_seq );
431 tcphdr->ack = htonl ( tcp->rcv_ack );
432 tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
433 tcphdr->flags = flags;
434 tcphdr->win = htons ( window );
435 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
438 DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
439 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
440 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
441 ntohl ( tcphdr->ack ), len );
442 tcp_dump_flags ( tcp, tcphdr->flags );
445 /* Transmit packet */
446 rc = tcpip_tx ( iobuf, &tcp_protocol, &tcp->peer, NULL, &tcphdr->csum );
448 /* If we got -ENETUNREACH, kill the connection immediately
449 * because there is no point retrying. This isn't strictly
450 * necessary (since we will eventually time out anyway), but
451 * it avoids irritating needless delays. Don't do this for
452 * RST packets transmitted on connection abort, to avoid a
453 * potential infinite loop.
455 if ( ( ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_RST ) ) ) &&
456 ( rc == -ENETUNREACH ) ) {
457 DBGC ( tcp, "TCP %p aborting after TX failed: %s\n",
458 tcp, strerror ( rc ) );
459 tcp->tcp_state = TCP_CLOSED;
460 tcp_dump_state ( tcp );
461 tcp_close ( tcp, rc );
468 * Retransmission timer expired
470 * @v timer Retry timer
471 * @v over Failure indicator
473 static void tcp_expired ( struct retry_timer *timer, int over ) {
474 struct tcp_connection *tcp =
475 container_of ( timer, struct tcp_connection, timer );
476 int graceful_close = TCP_CLOSED_GRACEFULLY ( tcp->tcp_state );
478 DBGC ( tcp, "TCP %p timer %s in %s\n", tcp,
479 ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ) );
481 assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
482 ( tcp->tcp_state == TCP_SYN_RCVD ) ||
483 ( tcp->tcp_state == TCP_ESTABLISHED ) ||
484 ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
485 ( tcp->tcp_state == TCP_TIME_WAIT ) ||
486 ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
487 ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
489 if ( over || graceful_close ) {
490 /* If we have finally timed out and given up, or if
491 * this is the result of a graceful close, terminate
494 tcp->tcp_state = TCP_CLOSED;
495 tcp_dump_state ( tcp );
496 tcp_close ( tcp, -ETIMEDOUT );
498 /* Otherwise, retransmit the packet */
504 * Send RST response to incoming packet
506 * @v in_tcphdr TCP header of incoming packet
507 * @ret rc Return status code
509 static int tcp_xmit_reset ( struct tcp_connection *tcp,
510 struct sockaddr_tcpip *st_dest,
511 struct tcp_header *in_tcphdr ) {
512 struct io_buffer *iobuf;
513 struct tcp_header *tcphdr;
515 /* Allocate space for dataless TX buffer */
516 iobuf = alloc_iob ( MAX_HDR_LEN );
518 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
521 iob_reserve ( iobuf, MAX_HDR_LEN );
523 /* Construct RST response */
524 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
525 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
526 tcphdr->src = in_tcphdr->dest;
527 tcphdr->dest = in_tcphdr->src;
528 tcphdr->seq = in_tcphdr->ack;
529 tcphdr->ack = in_tcphdr->seq;
530 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
531 tcphdr->flags = ( TCP_RST | TCP_ACK );
532 tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
533 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
536 DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
537 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
538 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
539 ntohl ( tcphdr->ack ), 0 );
540 tcp_dump_flags ( tcp, tcphdr->flags );
543 /* Transmit packet */
544 return tcpip_tx ( iobuf, &tcp_protocol, st_dest,
545 NULL, &tcphdr->csum );
548 /***************************************************************************
552 ***************************************************************************
556 * Identify TCP connection by local port number
558 * @v local_port Local port (in network-endian order)
559 * @ret tcp TCP connection, or NULL
561 static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
562 struct tcp_connection *tcp;
564 list_for_each_entry ( tcp, &tcp_conns, list ) {
565 if ( tcp->local_port == local_port )
572 * Handle TCP received SYN
574 * @v tcp TCP connection
575 * @v seq SEQ value (in host-endian order)
576 * @ret rc Return status code
578 static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq ) {
580 /* Synchronise sequence numbers on first SYN */
581 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) )
584 /* Ignore duplicate SYN */
585 if ( ( tcp->rcv_ack - seq ) > 0 )
588 /* Mark SYN as received and start sending ACKs with each packet */
589 tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
590 TCP_STATE_RCVD ( TCP_SYN ) );
592 /* Acknowledge SYN */
599 * Handle TCP received ACK
601 * @v tcp TCP connection
602 * @v ack ACK value (in host-endian order)
603 * @v win WIN value (in host-endian order)
604 * @ret rc Return status code
606 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
608 size_t ack_len = ( ack - tcp->snd_seq );
610 unsigned int acked_flags;
612 /* Ignore duplicate or out-of-range ACK */
613 if ( ack_len > tcp->snd_sent ) {
614 DBGC ( tcp, "TCP %p received ACK for [%08lx,%08lx), "
615 "sent only [%08lx,%08lx)\n", tcp, tcp->snd_seq,
616 ( tcp->snd_seq + ack_len ), tcp->snd_seq,
617 ( tcp->snd_seq + tcp->snd_sent ) );
621 /* Acknowledge any flags being sent */
623 acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
624 ( TCP_SYN | TCP_FIN ) );
628 /* Update SEQ and sent counters, and window size */
633 /* Stop the retransmission timer */
634 stop_timer ( &tcp->timer );
636 /* Remove any acknowledged data from transmit queue */
637 tcp_process_queue ( tcp, len, NULL, 1 );
639 /* Mark SYN/FIN as acknowledged if applicable. */
641 tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
643 /* Start sending FIN if we've had all possible data ACKed */
644 if ( list_empty ( &tcp->queue ) && tcp->xfer_closed )
645 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
651 * Handle TCP received data
653 * @v tcp TCP connection
654 * @v seq SEQ value (in host-endian order)
655 * @v iobuf I/O buffer
656 * @ret rc Return status code
658 * This function takes ownership of the I/O buffer.
660 static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
661 struct io_buffer *iobuf ) {
666 /* Ignore duplicate data */
667 already_rcvd = ( tcp->rcv_ack - seq );
668 len = iob_len ( iobuf );
669 if ( already_rcvd >= len ) {
673 iob_pull ( iobuf, already_rcvd );
675 /* Deliver data to application */
676 if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 )
679 /* Acknowledge new data */
685 * Handle TCP received FIN
687 * @v tcp TCP connection
688 * @v seq SEQ value (in host-endian order)
689 * @ret rc Return status code
691 static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
693 /* Ignore duplicate FIN */
694 if ( ( tcp->rcv_ack - seq ) > 0 )
697 /* Mark FIN as received and acknowledge it */
698 tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
701 /* Close connection */
702 tcp_close ( tcp, 0 );
708 * Handle TCP received RST
710 * @v tcp TCP connection
711 * @v seq SEQ value (in host-endian order)
712 * @ret rc Return status code
714 static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
716 /* Accept RST only if it falls within the window. If we have
717 * not yet received a SYN, then we have no window to test
718 * against, so fall back to checking that our SYN has been
721 if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
722 if ( ( tcp->rcv_ack - seq ) > 0 )
725 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
729 /* Abort connection */
730 tcp->tcp_state = TCP_CLOSED;
731 tcp_dump_state ( tcp );
732 tcp_close ( tcp, -ECONNRESET );
738 * Process received packet
740 * @v iobuf I/O buffer
741 * @v st_src Partially-filled source address
742 * @v st_dest Partially-filled destination address
743 * @v pshdr_csum Pseudo-header checksum
744 * @ret rc Return status code
746 static int tcp_rx ( struct io_buffer *iobuf,
747 struct sockaddr_tcpip *st_src,
748 struct sockaddr_tcpip *st_dest __unused,
749 uint16_t pshdr_csum ) {
750 struct tcp_header *tcphdr = iobuf->data;
751 struct tcp_connection *tcp;
762 /* Sanity check packet */
763 if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
764 DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
765 iob_len ( iobuf ), sizeof ( *tcphdr ) );
769 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
770 if ( hlen < sizeof ( *tcphdr ) ) {
771 DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
772 hlen, sizeof ( *tcphdr ) );
776 if ( hlen > iob_len ( iobuf ) ) {
777 DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
778 hlen, iob_len ( iobuf ) );
782 csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, iob_len ( iobuf ));
784 DBG ( "TCP checksum incorrect (is %04x including checksum "
785 "field, should be 0000)\n", csum );
790 /* Parse parameters from header and strip header */
791 tcp = tcp_demux ( tcphdr->dest );
792 start_seq = seq = ntohl ( tcphdr->seq );
793 ack = ntohl ( tcphdr->ack );
794 win = ntohs ( tcphdr->win );
795 flags = tcphdr->flags;
796 iob_pull ( iobuf, hlen );
797 len = iob_len ( iobuf );
800 DBGC ( tcp, "TCP %p RX %d<-%d %08lx %08lx..%08lx %4zd",
801 tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
802 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
803 ( ntohl ( tcphdr->seq ) + len +
804 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
805 tcp_dump_flags ( tcp, tcphdr->flags );
808 /* If no connection was found, send RST */
810 tcp_xmit_reset ( tcp, st_src, tcphdr );
815 /* Handle ACK, if present */
816 if ( flags & TCP_ACK ) {
817 if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
818 tcp_xmit_reset ( tcp, st_src, tcphdr );
823 /* Handle SYN, if present */
824 if ( flags & TCP_SYN ) {
825 tcp_rx_syn ( tcp, seq );
829 /* Handle RST, if present */
830 if ( flags & TCP_RST ) {
831 if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
835 /* Handle new data, if any */
836 tcp_rx_data ( tcp, seq, iobuf );
839 /* Handle FIN, if present */
840 if ( flags & TCP_FIN ) {
841 tcp_rx_fin ( tcp, seq );
845 /* Dump out any state change as a result of the received packet */
846 tcp_dump_state ( tcp );
848 /* Send out any pending data. If peer is expecting an ACK for
849 * this packet then force sending a reply.
851 tcp_xmit ( tcp, ( start_seq != seq ) );
853 /* If this packet was the last we expect to receive, set up
854 * timer to expire and cause the connection to be freed.
856 if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
857 tcp->timer.timeout = ( 2 * TCP_MSL );
858 start_timer ( &tcp->timer );
864 /* Free received packet */
870 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
873 .tcpip_proto = IP_TCP,
876 /***************************************************************************
878 * Data transfer interface
880 ***************************************************************************
886 * @v xfer Data transfer interface
887 * @v rc Reason for close
889 static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
890 struct tcp_connection *tcp =
891 container_of ( xfer, struct tcp_connection, xfer );
893 /* Close data transfer interface */
894 tcp_close ( tcp, rc );
896 /* Transmit FIN, if possible */
903 * @v xfer Data transfer interface
904 * @v offset Offset to new position
905 * @v whence Basis for new position
906 * @ret rc Return status code
908 static int tcp_xfer_seek ( struct xfer_interface *xfer, off_t offset,
910 struct tcp_connection *tcp =
911 container_of ( xfer, struct tcp_connection, xfer );
913 /* TCP doesn't support seeking to arbitrary positions */
914 if ( ( whence != SEEK_CUR ) || ( offset != 0 ) )
917 /* Not ready if we're not in a suitable connection state */
918 if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
921 /* Not ready if data queue is non-empty */
922 if ( ! list_empty ( &tcp->queue ) )
929 * Deliver datagram as I/O buffer
931 * @v xfer Data transfer interface
932 * @v iobuf Datagram I/O buffer
933 * @v meta Data transfer metadata, or NULL
934 * @ret rc Return status code
936 static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
937 struct io_buffer *iobuf,
938 struct xfer_metadata *meta __unused ) {
939 struct tcp_connection *tcp =
940 container_of ( xfer, struct tcp_connection, xfer );
943 list_add_tail ( &iobuf->list, &tcp->queue );
945 /* Transmit data, if possible */
951 /** TCP data transfer interface operations */
952 static struct xfer_interface_operations tcp_xfer_operations = {
953 .close = tcp_xfer_close,
954 .vredirect = ignore_xfer_vredirect,
955 .request = ignore_xfer_request,
956 .seek = tcp_xfer_seek,
957 .alloc_iob = default_xfer_alloc_iob,
958 .deliver_iob = tcp_xfer_deliver_iob,
959 .deliver_raw = xfer_deliver_as_iob,
962 /***************************************************************************
966 ***************************************************************************
969 /** TCP socket opener */
970 struct socket_opener tcp_socket_opener __socket_opener = {
971 .semantics = SOCK_STREAM,
979 * @v xfer Data transfer interface
981 * @ret rc Return status code
983 static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
984 struct sockaddr_tcpip peer;
990 memset ( &peer, 0, sizeof ( peer ) );
991 peer.st_port = htons ( uri_port ( uri, 0 ) );
992 return xfer_open_named_socket ( xfer, SOCK_STREAM,
993 ( struct sockaddr * ) &peer,
997 /** TCP URI opener */
998 struct uri_opener tcp_uri_opener __uri_opener = {
1000 .open = tcp_open_uri,