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 = zalloc ( sizeof ( *tcp ) );
211 DBGC ( tcp, "TCP %p allocated\n", tcp );
212 xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
213 tcp->prev_tcp_state = TCP_CLOSED;
214 tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
215 tcp_dump_state ( tcp );
216 tcp->snd_seq = random();
217 INIT_LIST_HEAD ( &tcp->queue );
218 tcp->timer.expired = tcp_expired;
219 memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
221 /* Bind to local port */
222 bind_port = ( st_local ? st_local->st_port : 0 );
223 if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
226 /* Start timer to initiate SYN */
227 start_timer ( &tcp->timer );
229 /* Attach parent interface, transfer reference to connection
232 xfer_plug_plug ( &tcp->xfer, xfer );
233 list_add ( &tcp->list, &tcp_conns );
237 ref_put ( &tcp->refcnt );
242 * Close TCP connection
244 * @v tcp TCP connection
245 * @v rc Reason for close
247 * Closes the data transfer interface. If the TCP state machine is in
248 * a suitable state, the connection will be deleted.
250 static void tcp_close ( struct tcp_connection *tcp, int rc ) {
251 struct io_buffer *iobuf;
252 struct io_buffer *tmp;
254 /* Close data transfer interface */
255 xfer_nullify ( &tcp->xfer );
256 xfer_close ( &tcp->xfer, rc );
257 tcp->xfer_closed = 1;
259 /* If we are in CLOSED, or have otherwise not yet received a
260 * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
263 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
265 /* Transition to CLOSED for the sake of debugging messages */
266 tcp->tcp_state = TCP_CLOSED;
267 tcp_dump_state ( tcp );
269 /* Free any unsent I/O buffers */
270 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
271 list_del ( &iobuf->list );
275 /* Remove from list and drop reference */
276 stop_timer ( &tcp->timer );
277 list_del ( &tcp->list );
278 ref_put ( &tcp->refcnt );
279 DBGC ( tcp, "TCP %p connection deleted\n", tcp );
283 /* If we have not had our SYN acknowledged (i.e. we are in
284 * SYN_RCVD), pretend that it has been acknowledged so that we
285 * can send a FIN without breaking things.
287 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
288 tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
290 /* If we have no data remaining to send, start sending FIN */
291 if ( list_empty ( &tcp->queue ) ) {
292 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
293 tcp_dump_state ( tcp );
297 /***************************************************************************
301 ***************************************************************************
305 * Process TCP transmit queue
307 * @v tcp TCP connection
308 * @v max_len Maximum length to process
309 * @v dest I/O buffer to fill with data, or NULL
310 * @v remove Remove data from queue
311 * @ret len Length of data processed
313 * This processes at most @c max_len bytes from the TCP connection's
314 * transmit queue. Data will be copied into the @c dest I/O buffer
315 * (if provided) and, if @c remove is true, removed from the transmit
318 static size_t tcp_process_queue ( struct tcp_connection *tcp, size_t max_len,
319 struct io_buffer *dest, int remove ) {
320 struct io_buffer *iobuf;
321 struct io_buffer *tmp;
325 list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
326 frag_len = iob_len ( iobuf );
327 if ( frag_len > max_len )
330 memcpy ( iob_put ( dest, frag_len ), iobuf->data,
334 iob_pull ( iobuf, frag_len );
335 if ( ! iob_len ( iobuf ) ) {
336 list_del ( &iobuf->list );
347 * Transmit any outstanding data
349 * @v tcp TCP connection
350 * @v force_send Force sending of packet
352 * Transmits any outstanding data on the connection.
354 * Note that even if an error is returned, the retransmission timer
355 * will have been started if necessary, and so the stack will
356 * eventually attempt to retransmit the failed packet.
358 static int tcp_xmit ( struct tcp_connection *tcp, int force_send ) {
359 struct io_buffer *iobuf;
360 struct tcp_header *tcphdr;
361 struct tcp_mss_option *mssopt;
369 /* If retransmission timer is already running, do nothing */
370 if ( timer_running ( &tcp->timer ) )
373 /* Calculate both the actual (payload) and sequence space
374 * lengths that we wish to transmit.
376 if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
377 len = tcp_process_queue ( tcp, tcp->snd_win, NULL, 0 );
380 flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
381 if ( flags & ( TCP_SYN | TCP_FIN ) ) {
382 /* SYN or FIN consume one byte, and we can never send both */
383 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
386 tcp->snd_sent = seq_len;
388 /* If we have nothing to transmit, stop now */
389 if ( ( seq_len == 0 ) && ! force_send )
392 /* If we are transmitting anything that requires
393 * acknowledgement (i.e. consumes sequence space), start the
394 * retransmission timer. Do this before attempting to
395 * allocate the I/O buffer, in case allocation itself fails.
398 start_timer ( &tcp->timer );
400 /* Allocate I/O buffer */
401 iobuf = alloc_iob ( len + MAX_HDR_LEN );
403 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
406 iob_reserve ( iobuf, MAX_HDR_LEN );
408 /* Fill data payload from transmit queue */
409 tcp_process_queue ( tcp, len, iobuf, 0 );
411 /* Estimate window size */
412 window = ( ( freemem * 3 ) / 4 );
413 if ( window > TCP_MAX_WINDOW_SIZE )
414 window = TCP_MAX_WINDOW_SIZE;
415 window &= ~0x03; /* Keep everything dword-aligned */
417 /* Fill up the TCP header */
418 payload = iobuf->data;
419 if ( flags & TCP_SYN ) {
420 mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
421 mssopt->kind = TCP_OPTION_MSS;
422 mssopt->length = sizeof ( *mssopt );
423 mssopt->mss = htons ( TCP_MSS );
425 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
426 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
427 tcphdr->src = tcp->local_port;
428 tcphdr->dest = tcp->peer.st_port;
429 tcphdr->seq = htonl ( tcp->snd_seq );
430 tcphdr->ack = htonl ( tcp->rcv_ack );
431 tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
432 tcphdr->flags = flags;
433 tcphdr->win = htons ( window );
434 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
437 DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
438 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
439 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
440 ntohl ( tcphdr->ack ), len );
441 tcp_dump_flags ( tcp, tcphdr->flags );
444 /* Transmit packet */
445 rc = tcpip_tx ( iobuf, &tcp_protocol, &tcp->peer, NULL, &tcphdr->csum );
447 /* If we got -ENETUNREACH, kill the connection immediately
448 * because there is no point retrying. This isn't strictly
449 * necessary (since we will eventually time out anyway), but
450 * it avoids irritating needless delays. Don't do this for
451 * RST packets transmitted on connection abort, to avoid a
452 * potential infinite loop.
454 if ( ( ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_RST ) ) ) &&
455 ( rc == -ENETUNREACH ) ) {
456 DBGC ( tcp, "TCP %p aborting after TX failed: %s\n",
457 tcp, strerror ( rc ) );
458 tcp->tcp_state = TCP_CLOSED;
459 tcp_dump_state ( tcp );
460 tcp_close ( tcp, rc );
467 * Retransmission timer expired
469 * @v timer Retry timer
470 * @v over Failure indicator
472 static void tcp_expired ( struct retry_timer *timer, int over ) {
473 struct tcp_connection *tcp =
474 container_of ( timer, struct tcp_connection, timer );
475 int graceful_close = TCP_CLOSED_GRACEFULLY ( tcp->tcp_state );
477 DBGC ( tcp, "TCP %p timer %s in %s\n", tcp,
478 ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ) );
480 assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
481 ( tcp->tcp_state == TCP_SYN_RCVD ) ||
482 ( tcp->tcp_state == TCP_ESTABLISHED ) ||
483 ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
484 ( tcp->tcp_state == TCP_TIME_WAIT ) ||
485 ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
486 ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
488 if ( over || graceful_close ) {
489 /* If we have finally timed out and given up, or if
490 * this is the result of a graceful close, terminate
493 tcp->tcp_state = TCP_CLOSED;
494 tcp_dump_state ( tcp );
495 tcp_close ( tcp, -ETIMEDOUT );
497 /* Otherwise, retransmit the packet */
503 * Send RST response to incoming packet
505 * @v in_tcphdr TCP header of incoming packet
506 * @ret rc Return status code
508 static int tcp_xmit_reset ( struct tcp_connection *tcp,
509 struct sockaddr_tcpip *st_dest,
510 struct tcp_header *in_tcphdr ) {
511 struct io_buffer *iobuf;
512 struct tcp_header *tcphdr;
514 /* Allocate space for dataless TX buffer */
515 iobuf = alloc_iob ( MAX_HDR_LEN );
517 DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
520 iob_reserve ( iobuf, MAX_HDR_LEN );
522 /* Construct RST response */
523 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
524 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
525 tcphdr->src = in_tcphdr->dest;
526 tcphdr->dest = in_tcphdr->src;
527 tcphdr->seq = in_tcphdr->ack;
528 tcphdr->ack = in_tcphdr->seq;
529 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
530 tcphdr->flags = ( TCP_RST | TCP_ACK );
531 tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
532 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
535 DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
536 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
537 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
538 ntohl ( tcphdr->ack ), 0 );
539 tcp_dump_flags ( tcp, tcphdr->flags );
542 /* Transmit packet */
543 return tcpip_tx ( iobuf, &tcp_protocol, st_dest,
544 NULL, &tcphdr->csum );
547 /***************************************************************************
551 ***************************************************************************
555 * Identify TCP connection by local port number
557 * @v local_port Local port (in network-endian order)
558 * @ret tcp TCP connection, or NULL
560 static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
561 struct tcp_connection *tcp;
563 list_for_each_entry ( tcp, &tcp_conns, list ) {
564 if ( tcp->local_port == local_port )
571 * Handle TCP received SYN
573 * @v tcp TCP connection
574 * @v seq SEQ value (in host-endian order)
575 * @ret rc Return status code
577 static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq ) {
579 /* Synchronise sequence numbers on first SYN */
580 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) )
583 /* Ignore duplicate SYN */
584 if ( ( tcp->rcv_ack - seq ) > 0 )
587 /* Mark SYN as received and start sending ACKs with each packet */
588 tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
589 TCP_STATE_RCVD ( TCP_SYN ) );
591 /* Acknowledge SYN */
598 * Handle TCP received ACK
600 * @v tcp TCP connection
601 * @v ack ACK value (in host-endian order)
602 * @v win WIN value (in host-endian order)
603 * @ret rc Return status code
605 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
607 size_t ack_len = ( ack - tcp->snd_seq );
609 unsigned int acked_flags;
611 /* Ignore duplicate or out-of-range ACK */
612 if ( ack_len > tcp->snd_sent ) {
613 DBGC ( tcp, "TCP %p received ACK for [%08lx,%08lx), "
614 "sent only [%08lx,%08lx)\n", tcp, tcp->snd_seq,
615 ( tcp->snd_seq + ack_len ), tcp->snd_seq,
616 ( tcp->snd_seq + tcp->snd_sent ) );
620 /* Acknowledge any flags being sent */
622 acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
623 ( TCP_SYN | TCP_FIN ) );
627 /* Update SEQ and sent counters, and window size */
632 /* Stop the retransmission timer */
633 stop_timer ( &tcp->timer );
635 /* Remove any acknowledged data from transmit queue */
636 tcp_process_queue ( tcp, len, NULL, 1 );
638 /* Mark SYN/FIN as acknowledged if applicable. */
640 tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
642 /* Start sending FIN if we've had all possible data ACKed */
643 if ( list_empty ( &tcp->queue ) && tcp->xfer_closed )
644 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
650 * Handle TCP received data
652 * @v tcp TCP connection
653 * @v seq SEQ value (in host-endian order)
654 * @v iobuf I/O buffer
655 * @ret rc Return status code
657 * This function takes ownership of the I/O buffer.
659 static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
660 struct io_buffer *iobuf ) {
665 /* Ignore duplicate data */
666 already_rcvd = ( tcp->rcv_ack - seq );
667 len = iob_len ( iobuf );
668 if ( already_rcvd >= len ) {
672 iob_pull ( iobuf, already_rcvd );
674 /* Deliver data to application */
675 if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 )
678 /* Acknowledge new data */
684 * Handle TCP received FIN
686 * @v tcp TCP connection
687 * @v seq SEQ value (in host-endian order)
688 * @ret rc Return status code
690 static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
692 /* Ignore duplicate FIN */
693 if ( ( tcp->rcv_ack - seq ) > 0 )
696 /* Mark FIN as received and acknowledge it */
697 tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
700 /* Close connection */
701 tcp_close ( tcp, 0 );
707 * Handle TCP received RST
709 * @v tcp TCP connection
710 * @v seq SEQ value (in host-endian order)
711 * @ret rc Return status code
713 static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
715 /* Accept RST only if it falls within the window. If we have
716 * not yet received a SYN, then we have no window to test
717 * against, so fall back to checking that our SYN has been
720 if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
721 if ( ( tcp->rcv_ack - seq ) > 0 )
724 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
728 /* Abort connection */
729 tcp->tcp_state = TCP_CLOSED;
730 tcp_dump_state ( tcp );
731 tcp_close ( tcp, -ECONNRESET );
737 * Process received packet
739 * @v iobuf I/O buffer
740 * @v st_src Partially-filled source address
741 * @v st_dest Partially-filled destination address
742 * @v pshdr_csum Pseudo-header checksum
743 * @ret rc Return status code
745 static int tcp_rx ( struct io_buffer *iobuf,
746 struct sockaddr_tcpip *st_src,
747 struct sockaddr_tcpip *st_dest __unused,
748 uint16_t pshdr_csum ) {
749 struct tcp_header *tcphdr = iobuf->data;
750 struct tcp_connection *tcp;
761 /* Sanity check packet */
762 if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
763 DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
764 iob_len ( iobuf ), sizeof ( *tcphdr ) );
768 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
769 if ( hlen < sizeof ( *tcphdr ) ) {
770 DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
771 hlen, sizeof ( *tcphdr ) );
775 if ( hlen > iob_len ( iobuf ) ) {
776 DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
777 hlen, iob_len ( iobuf ) );
781 csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, iob_len ( iobuf ));
783 DBG ( "TCP checksum incorrect (is %04x including checksum "
784 "field, should be 0000)\n", csum );
789 /* Parse parameters from header and strip header */
790 tcp = tcp_demux ( tcphdr->dest );
791 start_seq = seq = ntohl ( tcphdr->seq );
792 ack = ntohl ( tcphdr->ack );
793 win = ntohs ( tcphdr->win );
794 flags = tcphdr->flags;
795 iob_pull ( iobuf, hlen );
796 len = iob_len ( iobuf );
799 DBGC ( tcp, "TCP %p RX %d<-%d %08lx %08lx..%08lx %4zd",
800 tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
801 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
802 ( ntohl ( tcphdr->seq ) + len +
803 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
804 tcp_dump_flags ( tcp, tcphdr->flags );
807 /* If no connection was found, send RST */
809 tcp_xmit_reset ( tcp, st_src, tcphdr );
814 /* Handle ACK, if present */
815 if ( flags & TCP_ACK ) {
816 if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
817 tcp_xmit_reset ( tcp, st_src, tcphdr );
822 /* Handle SYN, if present */
823 if ( flags & TCP_SYN ) {
824 tcp_rx_syn ( tcp, seq );
828 /* Handle RST, if present */
829 if ( flags & TCP_RST ) {
830 if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
834 /* Handle new data, if any */
835 tcp_rx_data ( tcp, seq, iobuf );
838 /* Handle FIN, if present */
839 if ( flags & TCP_FIN ) {
840 tcp_rx_fin ( tcp, seq );
844 /* Dump out any state change as a result of the received packet */
845 tcp_dump_state ( tcp );
847 /* Send out any pending data. If peer is expecting an ACK for
848 * this packet then force sending a reply.
850 tcp_xmit ( tcp, ( start_seq != seq ) );
852 /* If this packet was the last we expect to receive, set up
853 * timer to expire and cause the connection to be freed.
855 if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
856 tcp->timer.timeout = ( 2 * TCP_MSL );
857 start_timer ( &tcp->timer );
863 /* Free received packet */
869 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
872 .tcpip_proto = IP_TCP,
875 /***************************************************************************
877 * Data transfer interface
879 ***************************************************************************
885 * @v xfer Data transfer interface
886 * @v rc Reason for close
888 static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
889 struct tcp_connection *tcp =
890 container_of ( xfer, struct tcp_connection, xfer );
892 /* Close data transfer interface */
893 tcp_close ( tcp, rc );
895 /* Transmit FIN, if possible */
902 * @v xfer Data transfer interface
903 * @v offset Offset to new position
904 * @v whence Basis for new position
905 * @ret rc Return status code
907 static int tcp_xfer_seek ( struct xfer_interface *xfer, off_t offset,
909 struct tcp_connection *tcp =
910 container_of ( xfer, struct tcp_connection, xfer );
912 /* TCP doesn't support seeking to arbitrary positions */
913 if ( ( whence != SEEK_CUR ) || ( offset != 0 ) )
916 /* Not ready if we're not in a suitable connection state */
917 if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
920 /* Not ready if data queue is non-empty */
921 if ( ! list_empty ( &tcp->queue ) )
928 * Deliver datagram as I/O buffer
930 * @v xfer Data transfer interface
931 * @v iobuf Datagram I/O buffer
932 * @v meta Data transfer metadata, or NULL
933 * @ret rc Return status code
935 static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
936 struct io_buffer *iobuf,
937 struct xfer_metadata *meta __unused ) {
938 struct tcp_connection *tcp =
939 container_of ( xfer, struct tcp_connection, xfer );
942 list_add_tail ( &iobuf->list, &tcp->queue );
944 /* Transmit data, if possible */
950 /** TCP data transfer interface operations */
951 static struct xfer_interface_operations tcp_xfer_operations = {
952 .close = tcp_xfer_close,
953 .vredirect = ignore_xfer_vredirect,
954 .request = ignore_xfer_request,
955 .seek = tcp_xfer_seek,
956 .alloc_iob = default_xfer_alloc_iob,
957 .deliver_iob = tcp_xfer_deliver_iob,
958 .deliver_raw = xfer_deliver_as_iob,
961 /***************************************************************************
965 ***************************************************************************
968 /** TCP socket opener */
969 struct socket_opener tcp_socket_opener __socket_opener = {
970 .semantics = SOCK_STREAM,
975 char TCP_SOCK_STREAM[1];
980 * @v xfer Data transfer interface
982 * @ret rc Return status code
984 static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
985 struct sockaddr_tcpip peer;
991 memset ( &peer, 0, sizeof ( peer ) );
992 peer.st_port = htons ( uri_port ( uri, 0 ) );
993 return xfer_open_named_socket ( xfer, SOCK_STREAM,
994 ( struct sockaddr * ) &peer,
998 /** TCP URI opener */
999 struct uri_opener tcp_uri_opener __uri_opener = {
1001 .open = tcp_open_uri,