8 #include <gpxe/pkbuff.h>
9 #include <gpxe/retry.h>
10 #include <gpxe/tcpip.h>
19 static void tcp_expired ( struct retry_timer *timer, int over );
24 * This data structure represents the internal state of a TCP
25 * connection. It is kept separate from @c struct @c tcp_application
26 * because the internal state is still required for some time after
27 * the application closes the connection.
29 struct tcp_connection {
30 /** List of TCP connections */
31 struct list_head list;
32 /** The associated TCP application, if any */
33 struct tcp_application *app;
35 /** Remote socket address */
36 struct sockaddr_tcpip peer;
37 /** Local port, in network byte order */
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.
68 /** Transmit packet buffer
70 * This buffer is allocated prior to calling the application's
71 * senddata() method, to provide temporary storage space.
73 struct pk_buff *tx_pkb;
74 /** Retransmission timer */
75 struct retry_timer timer;
79 * List of registered TCP connections
81 static LIST_HEAD ( tcp_conns );
87 * @ret name Name of TCP state
89 static inline __attribute__ (( always_inline )) const char *
90 tcp_state ( int state ) {
92 case TCP_CLOSED: return "CLOSED";
93 case TCP_LISTEN: return "LISTEN";
94 case TCP_SYN_SENT: return "SYN_SENT";
95 case TCP_SYN_RCVD: return "SYN_RCVD";
96 case TCP_ESTABLISHED: return "ESTABLISHED";
97 case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
98 case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
99 case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
100 case TCP_TIME_WAIT: return "TIME_WAIT";
101 case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
102 default: return "INVALID";
107 * Dump TCP state transition
109 * @v conn TCP connection
111 static inline __attribute__ (( always_inline )) void
112 tcp_dump_state ( struct tcp_connection *conn ) {
114 if ( conn->tcp_state != conn->prev_tcp_state ) {
115 DBGC ( conn, "TCP %p transitioned from %s to %s\n", conn,
116 tcp_state ( conn->prev_tcp_state ),
117 tcp_state ( conn->tcp_state ) );
119 conn->prev_tcp_state = conn->tcp_state;
127 static inline __attribute__ (( always_inline )) void
128 tcp_dump_flags ( struct tcp_connection *conn, unsigned int flags ) {
129 if ( flags & TCP_RST )
130 DBGC ( conn, " RST" );
131 if ( flags & TCP_SYN )
132 DBGC ( conn, " SYN" );
133 if ( flags & TCP_PSH )
134 DBGC ( conn, " PSH" );
135 if ( flags & TCP_FIN )
136 DBGC ( conn, " FIN" );
137 if ( flags & TCP_ACK )
138 DBGC ( conn, " ACK" );
142 * Allocate TCP connection
144 * @ret conn TCP connection, or NULL
146 * Allocates TCP connection and adds it to the TCP connection list.
148 static struct tcp_connection * alloc_tcp ( void ) {
149 struct tcp_connection *conn;
151 conn = calloc ( 1, sizeof ( *conn ) );
153 DBGC ( conn, "TCP %p allocated\n", conn );
154 conn->tcp_state = conn->prev_tcp_state = TCP_CLOSED;
155 conn->snd_seq = random();
156 conn->timer.expired = tcp_expired;
157 list_add ( &conn->list, &tcp_conns );
163 * Free TCP connection
165 * @v conn TCP connection
167 * Removes connection from TCP connection list and frees the data
170 static void free_tcp ( struct tcp_connection *conn ) {
173 assert ( conn->tcp_state == TCP_CLOSED );
174 assert ( conn->app == NULL );
176 stop_timer ( &conn->timer );
177 list_del ( &conn->list );
179 DBGC ( conn, "TCP %p freed\n", conn );
183 * Associate TCP connection with application
185 * @v conn TCP connection
186 * @v app TCP application
188 static void tcp_associate ( struct tcp_connection *conn,
189 struct tcp_application *app ) {
190 assert ( conn->app == NULL );
191 assert ( app->conn == NULL );
194 DBGC ( conn, "TCP %p associated with application %p\n", conn, app );
198 * Disassociate TCP connection from application
200 * @v conn TCP connection
202 static void tcp_disassociate ( struct tcp_connection *conn ) {
203 struct tcp_application *app = conn->app;
206 assert ( app->conn == conn );
209 DBGC ( conn, "TCP %p disassociated from application %p\n",
215 * Transmit any outstanding data
217 * @v conn TCP connection
218 * @v force_send Force sending of packet
220 * Transmits any outstanding data on the connection. If the
221 * connection is in a connected state, the application's senddata()
222 * method will be called to generate the data payload, if any.
224 * Note that even if an error is returned, the retransmission timer
225 * will have been started if necessary, and so the stack will
226 * eventually attempt to retransmit the failed packet.
228 static int tcp_senddata_conn ( struct tcp_connection *conn, int force_send ) {
229 struct tcp_application *app = conn->app;
231 struct tcp_header *tcphdr;
236 /* Allocate space to the TX buffer */
237 pkb = alloc_pkb ( MAX_PKB_LEN );
239 DBGC ( conn, "TCP %p could not allocate data buffer\n", conn );
240 /* Start the retry timer so that we attempt to
241 * retransmit this packet later. (Start it
242 * unconditionally, since without a packet buffer we
243 * can't call the senddata() callback, and so may not
244 * be able to tell whether or not we have something
245 * that actually needs to be retransmitted).
247 start_timer ( &conn->timer );
250 pkb_reserve ( pkb, MAX_HDR_LEN );
252 /* If we are connected, call the senddata() method, which may
253 * call tcp_send() to queue up a data payload.
255 if ( TCP_CAN_SEND_DATA ( conn->tcp_state ) &&
256 app && app->tcp_op->senddata ) {
258 app->tcp_op->senddata ( app, pkb->data, pkb_available ( pkb ));
262 /* Truncate payload length to fit transmit window */
263 len = pkb_len ( pkb );
264 if ( len > conn->snd_win )
267 /* Calculate amount of sequence space that this transmission
268 * consumes. (SYN or FIN consume one byte, and we can never
269 * send both at once).
272 flags = TCP_FLAGS_SENDING ( conn->tcp_state );
273 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
274 if ( flags & ( TCP_SYN | TCP_FIN ) )
276 conn->snd_sent = seq_len;
278 /* If we have nothing to transmit, drop the packet */
279 if ( ( seq_len == 0 ) && ! force_send ) {
284 /* If we are transmitting anything that requires
285 * acknowledgement (i.e. consumes sequence space), start the
286 * retransmission timer.
289 start_timer ( &conn->timer );
291 /* Fill up the TCP header */
292 tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
293 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
294 tcphdr->src = conn->local_port;
295 tcphdr->dest = conn->peer.st_port;
296 tcphdr->seq = htonl ( conn->snd_seq );
297 tcphdr->ack = htonl ( conn->rcv_ack );
298 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
299 tcphdr->flags = flags;
300 tcphdr->win = htons ( TCP_WINDOW_SIZE );
301 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
304 DBGC ( conn, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
305 conn, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
306 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
307 ntohl ( tcphdr->ack ), len );
308 tcp_dump_flags ( conn, tcphdr->flags );
311 /* Transmit packet */
312 return tcpip_tx ( pkb, &tcp_protocol, &conn->peer, &tcphdr->csum );
316 * Transmit any outstanding data
318 * @v conn TCP connection
320 * This function allocates space to the transmit buffer and invokes
321 * the senddata() callback function, to allow the application to
324 int tcp_senddata ( struct tcp_application *app ) {
325 struct tcp_connection *conn = app->conn;
327 /* Check connection actually exists */
329 DBG ( "TCP app %p has no connection\n", app );
333 return tcp_senddata_conn ( conn, 0 );
339 * @v app TCP application
340 * @v data Data to be sent
341 * @v len Length of the data
342 * @ret rc Return status code
344 * This function queues data to be sent via the TCP connection. It
345 * can be called only in the context of an application's senddata()
348 int tcp_send ( struct tcp_application *app, const void *data, size_t len ) {
349 struct tcp_connection *conn = app->conn;
352 /* Check connection actually exists */
354 DBG ( "TCP app %p has no connection\n", app );
358 /* Check that we have a packet buffer to fill */
361 DBG ( "TCP app %p tried to send data outside of the "
362 "senddata() method\n", app );
366 /* Truncate length to fit packet buffer */
367 if ( len > pkb_available ( pkb ) )
368 len = pkb_available ( pkb );
371 memmove ( pkb_put ( pkb, len ), data, len );
377 * Retransmission timer expired
379 * @v timer Retry timer
380 * @v over Failure indicator
382 static void tcp_expired ( struct retry_timer *timer, int over ) {
383 struct tcp_connection *conn =
384 container_of ( timer, struct tcp_connection, timer );
385 struct tcp_application *app = conn->app;
386 int graceful_close = TCP_CLOSED_GRACEFULLY ( conn->tcp_state );
388 DBGC ( conn, "TCP %p timer %s in %s\n", conn,
389 ( over ? "expired" : "fired" ), tcp_state ( conn->tcp_state ) );
391 assert ( ( conn->tcp_state == TCP_SYN_SENT ) ||
392 ( conn->tcp_state == TCP_SYN_RCVD ) ||
393 ( conn->tcp_state == TCP_ESTABLISHED ) ||
394 ( conn->tcp_state == TCP_FIN_WAIT_1 ) ||
395 ( conn->tcp_state == TCP_TIME_WAIT ) ||
396 ( conn->tcp_state == TCP_CLOSE_WAIT ) ||
397 ( conn->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
399 /* If we have finally timed out and given up, or if this is
400 * the result of a graceful close, terminate the connection
402 if ( over || graceful_close ) {
404 /* Transition to CLOSED */
405 conn->tcp_state = TCP_CLOSED;
406 tcp_dump_state ( conn );
408 /* If we haven't closed gracefully, send a RST */
409 if ( ! graceful_close )
410 tcp_senddata_conn ( conn, 1 );
412 /* Break association between application and connection */
413 tcp_disassociate ( conn );
415 /* Free the connection */
418 /* Notify application */
419 if ( app && app->tcp_op->closed )
420 app->tcp_op->closed ( app, -ETIMEDOUT );
423 /* Otherwise, retransmit the packet */
424 tcp_senddata_conn ( conn, 0 );
429 * Send RST response to incoming packet
431 * @v in_tcphdr TCP header of incoming packet
432 * @ret rc Return status code
434 static int tcp_send_reset ( struct tcp_connection *conn,
435 struct tcp_header *in_tcphdr ) {
437 struct tcp_header *tcphdr;
439 /* Allocate space for dataless TX buffer */
440 pkb = alloc_pkb ( MAX_HDR_LEN );
442 DBGC ( conn, "TCP %p could not allocate data buffer\n", conn );
445 pkb_reserve ( pkb, MAX_HDR_LEN );
447 /* Construct RST response */
448 tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
449 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
450 tcphdr->src = in_tcphdr->dest;
451 tcphdr->dest = in_tcphdr->src;
452 tcphdr->seq = in_tcphdr->ack;
453 tcphdr->ack = in_tcphdr->seq;
454 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
455 tcphdr->flags = ( TCP_RST | TCP_ACK );
456 tcphdr->win = htons ( TCP_WINDOW_SIZE );
457 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
460 DBGC ( conn, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
461 conn, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
462 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
463 ntohl ( tcphdr->ack ), 0 );
464 tcp_dump_flags ( conn, tcphdr->flags );
467 /* Transmit packet */
468 return tcpip_tx ( pkb, &tcp_protocol, &conn->peer, &tcphdr->csum );
472 * Identify TCP connection by local port number
474 * @v local_port Local port (in network-endian order)
475 * @ret conn TCP connection, or NULL
477 static struct tcp_connection * tcp_demux ( uint16_t local_port ) {
478 struct tcp_connection *conn;
480 list_for_each_entry ( conn, &tcp_conns, list ) {
481 if ( conn->local_port == local_port )
488 * Handle TCP received SYN
490 * @v conn TCP connection
491 * @v seq SEQ value (in host-endian order)
492 * @ret rc Return status code
494 static int tcp_rx_syn ( struct tcp_connection *conn, uint32_t seq ) {
496 /* Synchronise sequence numbers on first SYN */
497 if ( ! ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) )
500 /* Ignore duplicate SYN */
501 if ( ( conn->rcv_ack - seq ) > 0 )
504 /* Mark SYN as received and start sending ACKs with each packet */
505 conn->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
506 TCP_STATE_RCVD ( TCP_SYN ) );
508 /* Acknowledge SYN */
515 * Handle TCP received ACK
517 * @v conn TCP connection
518 * @v ack ACK value (in host-endian order)
519 * @v win WIN value (in host-endian order)
520 * @ret rc Return status code
522 static int tcp_rx_ack ( struct tcp_connection *conn, uint32_t ack,
524 struct tcp_application *app = conn->app;
525 size_t ack_len = ( ack - conn->snd_seq );
527 unsigned int acked_flags = 0;
529 /* Ignore duplicate or out-of-range ACK */
530 if ( ack_len > conn->snd_sent ) {
531 DBGC ( conn, "TCP %p received ACK for [%08lx,%08lx), "
532 "sent only [%08lx,%08lx)\n", conn, conn->snd_seq,
533 ( conn->snd_seq + ack_len ), conn->snd_seq,
534 ( conn->snd_seq + conn->snd_sent ) );
538 /* If we are sending flags and this ACK acknowledges all
539 * outstanding sequence points, then it acknowledges the
540 * flags. (This works since both SYN and FIN will always be
541 * the last outstanding sequence point.)
544 if ( ack_len == conn->snd_sent ) {
545 acked_flags = ( TCP_FLAGS_SENDING ( conn->tcp_state ) &
546 ( TCP_SYN | TCP_FIN ) );
551 /* Update SEQ and sent counters, and window size */
556 /* Stop the retransmission timer */
557 stop_timer ( &conn->timer );
559 /* Notify application of acknowledged data, if any */
560 if ( len && app && app->tcp_op->acked )
561 app->tcp_op->acked ( app, len );
563 /* Mark SYN/FIN as acknowledged if applicable. */
565 conn->tcp_state |= TCP_STATE_ACKED ( acked_flags );
567 /* Notify application of established connection, if applicable */
568 if ( ( acked_flags & TCP_SYN ) && app && app->tcp_op->connected )
569 app->tcp_op->connected ( app );
575 * Handle TCP received data
577 * @v conn TCP connection
578 * @v seq SEQ value (in host-endian order)
579 * @v data Data buffer
580 * @v len Length of data buffer
581 * @ret rc Return status code
583 static int tcp_rx_data ( struct tcp_connection *conn, uint32_t seq,
584 void *data, size_t len ) {
585 struct tcp_application *app = conn->app;
588 /* Ignore duplicate data */
589 already_rcvd = ( conn->rcv_ack - seq );
590 if ( already_rcvd >= len )
592 data += already_rcvd;
595 /* Acknowledge new data */
596 conn->rcv_ack += len;
598 /* Notify application */
599 if ( app && app->tcp_op->newdata )
600 app->tcp_op->newdata ( app, data, len );
606 * Handle TCP received FIN
608 * @v conn TCP connection
609 * @v seq SEQ value (in host-endian order)
610 * @ret rc Return status code
612 static int tcp_rx_fin ( struct tcp_connection *conn, uint32_t seq ) {
613 struct tcp_application *app = conn->app;
615 /* Ignore duplicate FIN */
616 if ( ( conn->rcv_ack - seq ) > 0 )
619 /* Mark FIN as received, acknowledge it, and send our own FIN */
620 conn->tcp_state |= ( TCP_STATE_RCVD ( TCP_FIN ) |
621 TCP_STATE_SENT ( TCP_FIN ) );
624 /* Break association with application */
625 tcp_disassociate ( conn );
627 /* Notify application */
628 if ( app && app->tcp_op->closed )
629 app->tcp_op->closed ( app, 0 );
635 * Handle TCP received RST
637 * @v conn TCP connection
638 * @v seq SEQ value (in host-endian order)
639 * @ret rc Return status code
641 static int tcp_rx_rst ( struct tcp_connection *conn, uint32_t seq ) {
642 struct tcp_application *app = conn->app;
644 /* Accept RST only if it falls within the window. If we have
645 * not yet received a SYN, then we have no window to test
646 * against, so fall back to checking that our SYN has been
649 if ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
650 if ( ( conn->rcv_ack - seq ) > 0 )
653 if ( ! ( conn->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
657 /* Transition to CLOSED */
658 conn->tcp_state = TCP_CLOSED;
659 tcp_dump_state ( conn );
661 /* Break association between application and connection */
662 tcp_disassociate ( conn );
664 /* Free the connection */
667 /* Notify application */
668 if ( app && app->tcp_op->closed )
669 app->tcp_op->closed ( app, -ECONNRESET );
675 * Process received packet
677 * @v pkb Packet buffer
678 * @v st_src Partially-filled source address
679 * @v st_dest Partially-filled destination address
680 * @v pshdr_csum Pseudo-header checksum
681 * @ret rc Return status code
683 static int tcp_rx ( struct pk_buff *pkb,
684 struct sockaddr_tcpip *st_src __unused,
685 struct sockaddr_tcpip *st_dest __unused,
686 uint16_t pshdr_csum ) {
687 struct tcp_header *tcphdr = pkb->data;
688 struct tcp_connection *conn;
700 /* Sanity check packet */
701 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
702 DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
703 pkb_len ( pkb ), sizeof ( *tcphdr ) );
707 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
708 if ( hlen < sizeof ( *tcphdr ) ) {
709 DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
710 hlen, sizeof ( *tcphdr ) );
714 if ( hlen > pkb_len ( pkb ) ) {
715 DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
716 hlen, pkb_len ( pkb ) );
720 csum = tcpip_continue_chksum ( pshdr_csum, pkb->data, pkb_len ( pkb ));
722 DBG ( "TCP checksum incorrect (is %04x including checksum "
723 "field, should be 0000)\n", csum );
728 /* Parse parameters from header and strip header */
729 conn = tcp_demux ( tcphdr->dest );
730 start_seq = seq = ntohl ( tcphdr->seq );
731 ack = ntohl ( tcphdr->ack );
732 win = ntohs ( tcphdr->win );
733 flags = tcphdr->flags;
734 data = pkb_pull ( pkb, hlen );
735 len = pkb_len ( pkb );
738 DBGC ( conn, "TCP %p RX %d<-%d %08lx %08lx..%08lx %4zd",
739 conn, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
740 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
741 ( ntohl ( tcphdr->seq ) + len +
742 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
743 tcp_dump_flags ( conn, tcphdr->flags );
746 /* If no connection was found, send RST */
748 tcp_send_reset ( conn, tcphdr );
753 /* Handle ACK, if present */
754 if ( flags & TCP_ACK ) {
755 if ( ( rc = tcp_rx_ack ( conn, ack, win ) ) != 0 ) {
756 tcp_send_reset ( conn, tcphdr );
761 /* Handle SYN, if present */
762 if ( flags & TCP_SYN ) {
763 tcp_rx_syn ( conn, seq );
767 /* Handle RST, if present */
768 if ( flags & TCP_RST ) {
769 if ( ( rc = tcp_rx_rst ( conn, seq ) ) != 0 )
773 /* Handle new data, if any */
774 tcp_rx_data ( conn, seq, data, len );
777 /* Handle FIN, if present */
778 if ( flags & TCP_FIN ) {
779 tcp_rx_fin ( conn, seq );
783 /* Dump out any state change as a result of the received packet */
784 tcp_dump_state ( conn );
786 /* Send out any pending data. If peer is expecting an ACK for
787 * this packet then force sending a reply.
789 tcp_senddata_conn ( conn, ( start_seq != seq ) );
791 /* If this packet was the last we expect to receive, set up
792 * timer to expire and cause the connection to be freed.
794 if ( TCP_CLOSED_GRACEFULLY ( conn->tcp_state ) ) {
795 conn->timer.timeout = ( 2 * TCP_MSL );
796 start_timer ( &conn->timer );
801 /* Free received packet */
807 * Bind TCP connection to local port
809 * @v conn TCP connection
810 * @v local_port Local port (in network byte order), or 0
811 * @ret rc Return status code
813 * This function adds the connection to the list of registered TCP
814 * connections. If the local port is 0, the connection is assigned an
815 * available port between 1024 and 65535.
817 static int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
818 struct tcp_connection *existing;
819 static uint16_t try_port = 1024;
821 /* If no port specified, find the first available port */
822 if ( ! local_port ) {
823 for ( ; try_port ; try_port++ ) {
824 if ( try_port < 1024 )
826 if ( tcp_bind ( conn, htons ( try_port ) ) == 0 )
829 DBGC ( conn, "TCP %p could not bind: no free ports\n", conn );
833 /* Attempt bind to local port */
834 list_for_each_entry ( existing, &tcp_conns, list ) {
835 if ( existing->local_port == local_port ) {
836 DBGC ( conn, "TCP %p could not bind: port %d in use\n",
837 conn, ntohs ( local_port ) );
841 conn->local_port = local_port;
843 DBGC ( conn, "TCP %p bound to port %d\n", conn, ntohs ( local_port ) );
848 * Connect to a remote server
850 * @v app TCP application
851 * @v peer Remote socket address
852 * @v local_port Local port number (in network byte order), or 0
853 * @ret rc Return status code
855 * This function initiates a TCP connection to the socket address specified in
856 * peer. It sends a SYN packet to peer. When the connection is established, the
857 * TCP stack calls the connected() callback function.
859 int tcp_connect ( struct tcp_application *app, struct sockaddr_tcpip *peer,
860 uint16_t local_port ) {
861 struct tcp_connection *conn;
864 /* Application must not already have an open connection */
866 DBG ( "TCP app %p already open on %p\n", app, app->conn );
870 /* Allocate connection state storage and add to connection list */
873 DBG ( "TCP app %p could not allocate connection\n", app );
877 /* Bind to peer and to local port */
878 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
879 if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 ) {
884 /* Associate with application */
885 tcp_associate ( conn, app );
887 /* Transition to TCP_SYN_SENT and send the SYN */
888 conn->tcp_state = TCP_SYN_SENT;
889 tcp_dump_state ( conn );
890 tcp_senddata_conn ( conn, 0 );
896 * Close the connection
898 * @v app TCP application
900 * The association between the application and the TCP connection is
901 * immediately severed, and the TCP application data structure can be
902 * reused or freed immediately. The TCP connection will persist until
903 * the state machine has returned to the TCP_CLOSED state.
905 void tcp_close ( struct tcp_application *app ) {
906 struct tcp_connection *conn = app->conn;
908 /* If no connection exists, do nothing */
912 /* Break association between application and connection */
913 tcp_disassociate ( conn );
915 /* If we have not yet received a SYN (i.e. we are in CLOSED,
916 * LISTEN or SYN_SENT), just delete the connection
918 if ( ! ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
919 conn->tcp_state = TCP_CLOSED;
920 tcp_dump_state ( conn );
925 /* If we have not had our SYN acknowledged (i.e. we are in
926 * SYN_RCVD), pretend that it has been acknowledged so that we
927 * can send a FIN without breaking things.
929 if ( ! ( conn->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
930 tcp_rx_ack ( conn, ( conn->snd_seq + 1 ), 0 );
932 /* Send a FIN to initiate the close */
933 conn->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
934 tcp_dump_state ( conn );
935 tcp_senddata_conn ( conn, 0 );
939 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
942 .tcpip_proto = IP_TCP,