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_tailroom ( 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,
313 NULL, &tcphdr->csum );
317 * Transmit any outstanding data
319 * @v conn TCP connection
321 * This function allocates space to the transmit buffer and invokes
322 * the senddata() callback function, to allow the application to
325 int tcp_senddata ( struct tcp_application *app ) {
326 struct tcp_connection *conn = app->conn;
328 /* Check connection actually exists */
330 DBG ( "TCP app %p has no connection\n", app );
334 return tcp_senddata_conn ( conn, 0 );
340 * @v app TCP application
341 * @v data Data to be sent
342 * @v len Length of the data
343 * @ret rc Return status code
345 * This function queues data to be sent via the TCP connection. It
346 * can be called only in the context of an application's senddata()
349 int tcp_send ( struct tcp_application *app, const void *data, size_t len ) {
350 struct tcp_connection *conn = app->conn;
353 /* Check connection actually exists */
355 DBG ( "TCP app %p has no connection\n", app );
359 /* Check that we have a packet buffer to fill */
362 DBG ( "TCP app %p tried to send data outside of the "
363 "senddata() method\n", app );
367 /* Truncate length to fit packet buffer */
368 if ( len > pkb_tailroom ( pkb ) )
369 len = pkb_tailroom ( pkb );
372 memmove ( pkb_put ( pkb, len ), data, len );
378 * Retransmission timer expired
380 * @v timer Retry timer
381 * @v over Failure indicator
383 static void tcp_expired ( struct retry_timer *timer, int over ) {
384 struct tcp_connection *conn =
385 container_of ( timer, struct tcp_connection, timer );
386 struct tcp_application *app = conn->app;
387 int graceful_close = TCP_CLOSED_GRACEFULLY ( conn->tcp_state );
389 DBGC ( conn, "TCP %p timer %s in %s\n", conn,
390 ( over ? "expired" : "fired" ), tcp_state ( conn->tcp_state ) );
392 assert ( ( conn->tcp_state == TCP_SYN_SENT ) ||
393 ( conn->tcp_state == TCP_SYN_RCVD ) ||
394 ( conn->tcp_state == TCP_ESTABLISHED ) ||
395 ( conn->tcp_state == TCP_FIN_WAIT_1 ) ||
396 ( conn->tcp_state == TCP_TIME_WAIT ) ||
397 ( conn->tcp_state == TCP_CLOSE_WAIT ) ||
398 ( conn->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
400 /* If we have finally timed out and given up, or if this is
401 * the result of a graceful close, terminate the connection
403 if ( over || graceful_close ) {
405 /* Transition to CLOSED */
406 conn->tcp_state = TCP_CLOSED;
407 tcp_dump_state ( conn );
409 /* If we haven't closed gracefully, send a RST */
410 if ( ! graceful_close )
411 tcp_senddata_conn ( conn, 1 );
413 /* Break association between application and connection */
414 tcp_disassociate ( conn );
416 /* Free the connection */
419 /* Notify application */
420 if ( app && app->tcp_op->closed )
421 app->tcp_op->closed ( app, -ETIMEDOUT );
424 /* Otherwise, retransmit the packet */
425 tcp_senddata_conn ( conn, 0 );
430 * Send RST response to incoming packet
432 * @v in_tcphdr TCP header of incoming packet
433 * @ret rc Return status code
435 static int tcp_send_reset ( struct tcp_connection *conn,
436 struct tcp_header *in_tcphdr ) {
438 struct tcp_header *tcphdr;
440 /* Allocate space for dataless TX buffer */
441 pkb = alloc_pkb ( MAX_HDR_LEN );
443 DBGC ( conn, "TCP %p could not allocate data buffer\n", conn );
446 pkb_reserve ( pkb, MAX_HDR_LEN );
448 /* Construct RST response */
449 tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
450 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
451 tcphdr->src = in_tcphdr->dest;
452 tcphdr->dest = in_tcphdr->src;
453 tcphdr->seq = in_tcphdr->ack;
454 tcphdr->ack = in_tcphdr->seq;
455 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
456 tcphdr->flags = ( TCP_RST | TCP_ACK );
457 tcphdr->win = htons ( TCP_WINDOW_SIZE );
458 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
461 DBGC ( conn, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
462 conn, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
463 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
464 ntohl ( tcphdr->ack ), 0 );
465 tcp_dump_flags ( conn, tcphdr->flags );
468 /* Transmit packet */
469 return tcpip_tx ( pkb, &tcp_protocol, &conn->peer,
470 NULL, &tcphdr->csum );
474 * Identify TCP connection by local port number
476 * @v local_port Local port (in network-endian order)
477 * @ret conn TCP connection, or NULL
479 static struct tcp_connection * tcp_demux ( uint16_t local_port ) {
480 struct tcp_connection *conn;
482 list_for_each_entry ( conn, &tcp_conns, list ) {
483 if ( conn->local_port == local_port )
490 * Handle TCP received SYN
492 * @v conn TCP connection
493 * @v seq SEQ value (in host-endian order)
494 * @ret rc Return status code
496 static int tcp_rx_syn ( struct tcp_connection *conn, uint32_t seq ) {
497 struct tcp_application *app = conn->app;
499 /* Synchronise sequence numbers on first SYN */
500 if ( ! ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) )
503 /* Ignore duplicate SYN */
504 if ( ( conn->rcv_ack - seq ) > 0 )
507 /* Mark SYN as received and start sending ACKs with each packet */
508 conn->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
509 TCP_STATE_RCVD ( TCP_SYN ) );
511 /* Acknowledge SYN */
514 /* Notify application of established connection, if applicable */
515 if ( ( conn->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) &&
516 app && app->tcp_op->connected )
517 app->tcp_op->connected ( app );
523 * Handle TCP received ACK
525 * @v conn TCP connection
526 * @v ack ACK value (in host-endian order)
527 * @v win WIN value (in host-endian order)
528 * @ret rc Return status code
530 static int tcp_rx_ack ( struct tcp_connection *conn, uint32_t ack,
532 struct tcp_application *app = conn->app;
533 size_t ack_len = ( ack - conn->snd_seq );
535 unsigned int acked_flags = 0;
537 /* Ignore duplicate or out-of-range ACK */
538 if ( ack_len > conn->snd_sent ) {
539 DBGC ( conn, "TCP %p received ACK for [%08lx,%08lx), "
540 "sent only [%08lx,%08lx)\n", conn, conn->snd_seq,
541 ( conn->snd_seq + ack_len ), conn->snd_seq,
542 ( conn->snd_seq + conn->snd_sent ) );
546 /* If we are sending flags and this ACK acknowledges all
547 * outstanding sequence points, then it acknowledges the
548 * flags. (This works since both SYN and FIN will always be
549 * the last outstanding sequence point.)
552 if ( ack_len == conn->snd_sent ) {
553 acked_flags = ( TCP_FLAGS_SENDING ( conn->tcp_state ) &
554 ( TCP_SYN | TCP_FIN ) );
559 /* Update SEQ and sent counters, and window size */
564 /* Stop the retransmission timer */
565 stop_timer ( &conn->timer );
567 /* Notify application of acknowledged data, if any */
568 if ( len && app && app->tcp_op->acked )
569 app->tcp_op->acked ( app, len );
571 /* Mark SYN/FIN as acknowledged if applicable. */
573 conn->tcp_state |= TCP_STATE_ACKED ( acked_flags );
575 /* Notify application of established connection, if applicable */
576 if ( ( acked_flags & TCP_SYN ) &&
577 ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) &&
578 app && app->tcp_op->connected )
579 app->tcp_op->connected ( app );
585 * Handle TCP received data
587 * @v conn TCP connection
588 * @v seq SEQ value (in host-endian order)
589 * @v data Data buffer
590 * @v len Length of data buffer
591 * @ret rc Return status code
593 static int tcp_rx_data ( struct tcp_connection *conn, uint32_t seq,
594 void *data, size_t len ) {
595 struct tcp_application *app = conn->app;
598 /* Ignore duplicate data */
599 already_rcvd = ( conn->rcv_ack - seq );
600 if ( already_rcvd >= len )
602 data += already_rcvd;
605 /* Acknowledge new data */
606 conn->rcv_ack += len;
608 /* Notify application */
609 if ( app && app->tcp_op->newdata )
610 app->tcp_op->newdata ( app, data, len );
616 * Handle TCP received FIN
618 * @v conn TCP connection
619 * @v seq SEQ value (in host-endian order)
620 * @ret rc Return status code
622 static int tcp_rx_fin ( struct tcp_connection *conn, uint32_t seq ) {
623 struct tcp_application *app = conn->app;
625 /* Ignore duplicate FIN */
626 if ( ( conn->rcv_ack - seq ) > 0 )
629 /* Mark FIN as received, acknowledge it, and send our own FIN */
630 conn->tcp_state |= ( TCP_STATE_RCVD ( TCP_FIN ) |
631 TCP_STATE_SENT ( TCP_FIN ) );
634 /* Break association with application */
635 tcp_disassociate ( conn );
637 /* Notify application */
638 if ( app && app->tcp_op->closed )
639 app->tcp_op->closed ( app, 0 );
645 * Handle TCP received RST
647 * @v conn TCP connection
648 * @v seq SEQ value (in host-endian order)
649 * @ret rc Return status code
651 static int tcp_rx_rst ( struct tcp_connection *conn, uint32_t seq ) {
652 struct tcp_application *app = conn->app;
654 /* Accept RST only if it falls within the window. If we have
655 * not yet received a SYN, then we have no window to test
656 * against, so fall back to checking that our SYN has been
659 if ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
660 if ( ( conn->rcv_ack - seq ) > 0 )
663 if ( ! ( conn->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
667 /* Transition to CLOSED */
668 conn->tcp_state = TCP_CLOSED;
669 tcp_dump_state ( conn );
671 /* Break association between application and connection */
672 tcp_disassociate ( conn );
674 /* Free the connection */
677 /* Notify application */
678 if ( app && app->tcp_op->closed )
679 app->tcp_op->closed ( app, -ECONNRESET );
685 * Process received packet
687 * @v pkb Packet buffer
688 * @v st_src Partially-filled source address
689 * @v st_dest Partially-filled destination address
690 * @v pshdr_csum Pseudo-header checksum
691 * @ret rc Return status code
693 static int tcp_rx ( struct pk_buff *pkb,
694 struct sockaddr_tcpip *st_src __unused,
695 struct sockaddr_tcpip *st_dest __unused,
696 uint16_t pshdr_csum ) {
697 struct tcp_header *tcphdr = pkb->data;
698 struct tcp_connection *conn;
710 /* Sanity check packet */
711 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
712 DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
713 pkb_len ( pkb ), sizeof ( *tcphdr ) );
717 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
718 if ( hlen < sizeof ( *tcphdr ) ) {
719 DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
720 hlen, sizeof ( *tcphdr ) );
724 if ( hlen > pkb_len ( pkb ) ) {
725 DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
726 hlen, pkb_len ( pkb ) );
730 csum = tcpip_continue_chksum ( pshdr_csum, pkb->data, pkb_len ( pkb ));
732 DBG ( "TCP checksum incorrect (is %04x including checksum "
733 "field, should be 0000)\n", csum );
738 /* Parse parameters from header and strip header */
739 conn = tcp_demux ( tcphdr->dest );
740 start_seq = seq = ntohl ( tcphdr->seq );
741 ack = ntohl ( tcphdr->ack );
742 win = ntohs ( tcphdr->win );
743 flags = tcphdr->flags;
744 data = pkb_pull ( pkb, hlen );
745 len = pkb_len ( pkb );
748 DBGC ( conn, "TCP %p RX %d<-%d %08lx %08lx..%08lx %4zd",
749 conn, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
750 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
751 ( ntohl ( tcphdr->seq ) + len +
752 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
753 tcp_dump_flags ( conn, tcphdr->flags );
756 /* If no connection was found, send RST */
758 tcp_send_reset ( conn, tcphdr );
763 /* Handle ACK, if present */
764 if ( flags & TCP_ACK ) {
765 if ( ( rc = tcp_rx_ack ( conn, ack, win ) ) != 0 ) {
766 tcp_send_reset ( conn, tcphdr );
771 /* Handle SYN, if present */
772 if ( flags & TCP_SYN ) {
773 tcp_rx_syn ( conn, seq );
777 /* Handle RST, if present */
778 if ( flags & TCP_RST ) {
779 if ( ( rc = tcp_rx_rst ( conn, seq ) ) != 0 )
783 /* Handle new data, if any */
784 tcp_rx_data ( conn, seq, data, len );
787 /* Handle FIN, if present */
788 if ( flags & TCP_FIN ) {
789 tcp_rx_fin ( conn, seq );
793 /* Dump out any state change as a result of the received packet */
794 tcp_dump_state ( conn );
796 /* Send out any pending data. If peer is expecting an ACK for
797 * this packet then force sending a reply.
799 tcp_senddata_conn ( conn, ( start_seq != seq ) );
801 /* If this packet was the last we expect to receive, set up
802 * timer to expire and cause the connection to be freed.
804 if ( TCP_CLOSED_GRACEFULLY ( conn->tcp_state ) ) {
805 conn->timer.timeout = ( 2 * TCP_MSL );
806 start_timer ( &conn->timer );
811 /* Free received packet */
817 * Bind TCP connection to local port
819 * @v conn TCP connection
820 * @v local_port Local port (in network byte order), or 0
821 * @ret rc Return status code
823 * This function adds the connection to the list of registered TCP
824 * connections. If the local port is 0, the connection is assigned an
825 * available port between 1024 and 65535.
827 static int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
828 struct tcp_connection *existing;
829 static uint16_t try_port = 1024;
831 /* If no port specified, find the first available port */
832 if ( ! local_port ) {
833 for ( ; try_port ; try_port++ ) {
834 if ( try_port < 1024 )
836 if ( tcp_bind ( conn, htons ( try_port ) ) == 0 )
839 DBGC ( conn, "TCP %p could not bind: no free ports\n", conn );
843 /* Attempt bind to local port */
844 list_for_each_entry ( existing, &tcp_conns, list ) {
845 if ( existing->local_port == local_port ) {
846 DBGC ( conn, "TCP %p could not bind: port %d in use\n",
847 conn, ntohs ( local_port ) );
851 conn->local_port = local_port;
853 DBGC ( conn, "TCP %p bound to port %d\n", conn, ntohs ( local_port ) );
858 * Connect to a remote server
860 * @v app TCP application
861 * @v peer Remote socket address
862 * @v local_port Local port number (in network byte order), or 0
863 * @ret rc Return status code
865 * This function initiates a TCP connection to the socket address specified in
866 * peer. It sends a SYN packet to peer. When the connection is established, the
867 * TCP stack calls the connected() callback function.
869 int tcp_connect ( struct tcp_application *app, struct sockaddr_tcpip *peer,
870 uint16_t local_port ) {
871 struct tcp_connection *conn;
874 /* Application must not already have an open connection */
876 DBG ( "TCP app %p already open on %p\n", app, app->conn );
880 /* Allocate connection state storage and add to connection list */
883 DBG ( "TCP app %p could not allocate connection\n", app );
887 /* Bind to peer and to local port */
888 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
889 if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 ) {
894 /* Associate with application */
895 tcp_associate ( conn, app );
897 /* Transition to TCP_SYN_SENT and send the SYN */
898 conn->tcp_state = TCP_SYN_SENT;
899 tcp_dump_state ( conn );
900 tcp_senddata_conn ( conn, 0 );
906 * Close the connection
908 * @v app TCP application
910 * The association between the application and the TCP connection is
911 * immediately severed, and the TCP application data structure can be
912 * reused or freed immediately. The TCP connection will persist until
913 * the state machine has returned to the TCP_CLOSED state.
915 void tcp_close ( struct tcp_application *app ) {
916 struct tcp_connection *conn = app->conn;
918 /* If no connection exists, do nothing */
922 /* Break association between application and connection */
923 tcp_disassociate ( conn );
925 /* If we have not yet received a SYN (i.e. we are in CLOSED,
926 * LISTEN or SYN_SENT), just delete the connection
928 if ( ! ( conn->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
929 conn->tcp_state = TCP_CLOSED;
930 tcp_dump_state ( conn );
935 /* If we have not had our SYN acknowledged (i.e. we are in
936 * SYN_RCVD), pretend that it has been acknowledged so that we
937 * can send a FIN without breaking things.
939 if ( ! ( conn->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
940 tcp_rx_ack ( conn, ( conn->snd_seq + 1 ), 0 );
942 /* Send a FIN to initiate the close */
943 conn->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
944 tcp_dump_state ( conn );
945 tcp_senddata_conn ( conn, 0 );
949 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
952 .tcpip_proto = IP_TCP,