7 #include <gpxe/process.h>
9 #include <gpxe/netdevice.h>
10 #include <gpxe/pkbuff.h>
13 #include <gpxe/tcpip.h>
14 #include <gpxe/retry.h>
21 * The gPXE TCP stack is currently implemented on top of the uIP
22 * protocol stack. This file provides wrappers around uIP so that
23 * higher-level protocol implementations do not need to talk directly
24 * to uIP (which has a somewhat baroque API).
26 * Basic operation is to create a #tcp_connection structure, call
27 * tcp_connect() and then call run_tcpip() in a loop until the
28 * operation has completed. The TCP stack will call the various
29 * methods defined in the #tcp_operations structure in order to send
32 * See hello.c for a trivial example of a TCP protocol using this
42 * When a tcp_operations::senddata() method is called, it is
43 * guaranteed to be able to use this buffer as temporary space for
44 * constructing the data to be sent. For example, code such as
48 * static void my_senddata ( struct tcp_connection *conn, void *buf,
50 * len = snprintf ( buf, len, "FETCH %s\r\n", filename );
51 * tcp_send ( conn, buf + already_sent, len - already_sent );
56 * is allowed, and is probably the best way to deal with
57 * variably-sized data.
59 * Note that you cannot use this simple mechanism if you want to be
60 * able to construct single data blocks of more than #len bytes.
62 static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
64 /** Size of #tcp_buffer */
65 static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
68 * Open a TCP connection
70 * @v conn TCP connection
72 * This sets up a new TCP connection to the remote host specified in
73 * tcp_connection::sin.
75 void tcp_connect ( struct tcp_connection *conn ) {
76 struct uip_conn *uip_conn;
79 assert ( conn->sin.sin_addr.s_addr != 0 );
80 assert ( conn->sin.sin_port != 0 );
81 assert ( conn->tcp_op != NULL );
82 assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
84 * ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
85 uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
86 #warning "Use linked lists so that uip_connect() cannot fail"
87 assert ( uip_conn != NULL );
88 *( ( void ** ) uip_conn->appstate ) = conn;
92 * Send data via a TCP connection
94 * @v conn TCP connection
95 * @v data Data to send
96 * @v len Length of data
98 * Data will be automatically limited to the current TCP window size.
100 * If retransmission is required, the connection's
101 * tcp_operations::senddata() method will be called again in order to
102 * regenerate the data.
104 void tcp_send ( struct tcp_connection *conn __unused,
105 const void *data, size_t len ) {
107 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
109 if ( len > tcp_buflen )
111 memmove ( tcp_buffer, data, len );
113 uip_send ( tcp_buffer, len );
117 * Close a TCP connection
119 * @v conn TCP connection
121 void tcp_close ( struct tcp_connection *conn __unused ) {
122 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
127 * uIP TCP application call interface
129 * This is the entry point of gPXE from the point of view of the uIP
130 * protocol stack. This function calls the appropriate methods from
131 * the connection's @tcp_operations table in order to process received
132 * data, transmit new data etc.
134 void uip_tcp_appcall ( void ) {
135 struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
136 struct tcp_operations *op = conn->tcp_op;
140 op->closed ( conn, -ECONNABORTED );
141 if ( uip_timedout() )
142 op->closed ( conn, -ETIMEDOUT );
144 op->closed ( conn, 0 );
146 if ( uip_connected() && op->connected )
147 op->connected ( conn );
148 if ( uip_acked() && op->acked )
149 op->acked ( conn, uip_conn->len );
150 if ( uip_newdata() && op->newdata )
151 op->newdata ( conn, ( void * ) uip_appdata, uip_len );
152 if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
153 uip_connected() || uip_poll() ) && op->senddata )
154 op->senddata ( conn, tcp_buffer, tcp_buflen );
157 /* Present here to allow everything to link. Will go into separate
160 void uip_udp_appcall ( void ) {
164 * Perform periodic processing of all TCP connections
166 * This allows TCP connections to retransmit data if necessary.
168 static void tcp_periodic ( void ) {
172 for ( i = 0 ; i < UIP_CONNS ; i++ ) {
175 pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
179 pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
180 pkb_put ( pkb, uip_len );
181 memcpy ( pkb->data, uip_buf, uip_len );
189 * Kick a connection into life
191 * @v conn TCP connection
193 * Call this function when you have new data to send and are not
194 * already being called as part of TCP processing.
196 void tcp_kick ( struct tcp_connection *conn __unused ) {
197 /* Just kick all the connections; this will work for now */
202 * Single-step the TCP stack
204 * @v process TCP process
206 * This calls tcp_periodic() at regular intervals.
208 static void tcp_step ( struct process *process ) {
209 static unsigned long timeout = 0;
211 if ( currticks() > timeout ) {
212 timeout = currticks() + ( TICKS_PER_SEC / 10 );
216 schedule ( process );
219 /** TCP stack process */
220 static struct process tcp_process = {
224 /** Initialise the TCP stack */
225 static void init_tcp ( void ) {
226 schedule ( &tcp_process );
229 INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );
234 * List of registered TCP connections
236 static LIST_HEAD ( tcp_conns );
241 static const char *tcp_states[] = {
256 * TCP state transition function
258 * @v conn TCP connection
259 * @v nxt_state Next TCP state
261 void tcp_set_flags ( struct tcp_connection *conn ) {
263 /* Set the TCP flags */
264 switch ( conn->tcp_state ) {
266 if ( conn->tcp_lstate == TCP_SYN_RCVD ) {
267 conn->tcp_flags |= TCP_RST;
273 if ( conn->tcp_lstate == TCP_LISTEN ||
274 conn->tcp_lstate == TCP_CLOSED ) {
275 conn->tcp_flags |= TCP_SYN;
279 if ( conn->tcp_lstate == TCP_LISTEN ||
280 conn->tcp_lstate == TCP_SYN_SENT ) {
281 conn->tcp_flags |= ( TCP_SYN | TCP_ACK );
284 case TCP_ESTABLISHED:
285 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
286 conn->tcp_flags |= TCP_ACK;
290 if ( conn->tcp_lstate == TCP_SYN_RCVD ||
291 conn->tcp_lstate == TCP_ESTABLISHED ) {
292 conn->tcp_flags |= TCP_FIN;
298 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ) {
299 conn->tcp_flags |= TCP_ACK;
303 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ||
304 conn->tcp_lstate == TCP_FIN_WAIT_2 ) {
305 conn->tcp_flags |= TCP_ACK;
309 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
310 conn->tcp_flags |= TCP_ACK;
314 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
315 conn->tcp_flags |= TCP_FIN;
319 DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
324 void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
325 /* Remember the last state */
326 conn->tcp_lstate = conn->tcp_state;
327 conn->tcp_state = nxt_state;
329 /* TODO: Check if this check is required */
330 if ( conn->tcp_lstate == conn->tcp_state ||
331 conn->tcp_state == TCP_INVALID ) {
335 tcp_set_flags ( conn );
341 * @v tcphdr TCP header
343 void tcp_dump ( struct tcp_header *tcphdr ) {
344 DBG ( "TCP %p src:%d dest:%d seq:%lld ack:%lld hlen:%hd flags:%#hx\n",
345 tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
346 ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
350 * Initialize a TCP connection
352 * @v conn TCP connection
354 * This function assigns initial values to some fields in the connection
355 * structure. The application should call tcp_init_conn after creating a new
356 * connection before calling any other "tcp_*" function.
358 * struct tcp_connection my_conn;
359 * tcp_init_conn ( &my_conn );
362 void tcp_init_conn ( struct tcp_connection *conn ) {
363 conn->local_port = 0;
364 conn->tcp_state = TCP_CLOSED;
365 conn->tcp_lstate = TCP_INVALID;
372 * @v timer Retry timer
373 * @v over Failure indicator
375 void tcp_expired ( struct retry_timer *timer, int over ) {
376 struct tcp_connection *conn;
377 conn = ( struct tcp_connection * ) container_of ( timer,
378 struct tcp_connection, timer );
379 DBG ( "Timer expired in %s\n", tcp_states[conn->tcp_state] );
380 switch ( conn->tcp_state ) {
383 tcp_trans ( conn, TCP_CLOSED );
384 stop_timer ( &conn->timer );
385 DBG ( "Timeout! Connection closed\n" );
391 tcp_trans ( conn, TCP_CLOSED );
392 stop_timer ( &conn->timer );
396 case TCP_ESTABLISHED:
397 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
402 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
411 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
416 tcp_trans ( conn, TCP_CLOSED );
417 stop_timer ( &conn->timer );
420 /* Retransmit the data */
421 tcp_set_flags ( conn );
422 tcp_senddata ( conn );
426 free_pkb ( conn->tx_pkb );
427 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
428 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
429 tcp_set_flags ( conn );
431 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
432 DBG ( "Error sending TCP message (rc = %d)\n", rc );
438 * Connect to a remote server
440 * @v conn TCP connection
441 * @v peer Remote socket address
443 * This function initiates a TCP connection to the socket address specified in
444 * peer. It sends a SYN packet to peer. When the connection is established, the
445 * TCP stack calls the connected() callback function.
447 int tcp_connectto ( struct tcp_connection *conn,
448 struct sockaddr_tcpip *peer ) {
451 /* A connection can only be established from the CLOSED state */
452 if ( conn->tcp_state != TCP_CLOSED ) {
453 DBG ( "Error opening connection: Invalid state %s\n",
454 tcp_states[conn->tcp_state] );
458 /* Add the connection to the set of listening connections */
459 if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
462 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
464 /* Initialize the TCP timer */
465 conn->timer.expired = tcp_expired;
467 /* Send a SYN packet and transition to TCP_SYN_SENT */
468 conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
469 tcp_trans ( conn, TCP_SYN_SENT );
470 /* Allocate space for the packet */
471 free_pkb ( conn->tx_pkb );
472 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
473 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
474 conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
475 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
478 int tcp_connect ( struct tcp_connection *conn ) {
479 return tcp_connectto ( conn, &conn->peer );
483 * Close the connection
487 * This function sends a FIN packet to the remote end of the connection. When
488 * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
489 * snd stream), the stack invokes the closed() callback function.
491 int tcp_close ( struct tcp_connection *conn ) {
492 /* A connection can only be closed if it is a connected state */
493 switch ( conn->tcp_state ) {
495 case TCP_ESTABLISHED:
496 tcp_trans ( conn, TCP_FIN_WAIT_1 );
497 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
498 /* FIN consumes one byte on the snd stream */
504 * Since the connection does not expect any packets from the
505 * remote end, it can be removed from the set of listening
508 list_del ( &conn->list );
509 tcp_trans ( conn, TCP_CLOSED );
510 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
513 tcp_trans ( conn, TCP_LAST_ACK );
514 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
515 /* FIN consumes one byte on the snd stream */
519 DBG ( "tcp_close(): Invalid state %s\n",
520 tcp_states[conn->tcp_state] );
525 free_pkb ( conn->tx_pkb );
526 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
527 conn->tcp_flags = TCP_FIN;
528 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
529 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
534 * Listen for a packet
536 * @v conn TCP connection
537 * @v port Local port, in network byte order
539 * This function adds the connection to a list of registered tcp connections. If
540 * the local port is 0, the connection is assigned the lowest available port
541 * between MIN_TCP_PORT and 65535.
543 int tcp_listen ( struct tcp_connection *conn, uint16_t port ) {
544 struct tcp_connection *cconn;
546 list_for_each_entry ( cconn, &tcp_conns, list ) {
547 if ( cconn->local_port == port ) {
548 DBG ( "Error listening to %d\n",
553 /* Add the connection to the list of registered connections */
554 conn->local_port = port;
555 list_add ( &conn->list, &tcp_conns );
558 /* Assigning lowest port not supported */
559 DBG ( "Assigning lowest port not implemented\n");
566 * @v conn TCP connection
568 * This function allocates space to the transmit buffer and invokes the
569 * senddata() callback function. It passes the allocated buffer to senddata().
570 * The applicaion may use this space to write it's data.
572 int tcp_senddata ( struct tcp_connection *conn ) {
573 /* The connection must be in a state in which the user can send data */
574 switch ( conn->tcp_state ) {
576 tcp_trans ( conn, TCP_SYN_SENT );
577 conn->snd_una = ( ( ( uint32_t ) random() ) << 16 ) & random();
579 case TCP_ESTABLISHED:
583 DBG ( "tcp_senddata: Invalid state %s\n",
584 tcp_states[conn->tcp_state] );
588 /* Allocate space to the TX buffer */
589 free_pkb ( conn->tx_pkb );
590 conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
591 if ( !conn->tx_pkb ) {
592 DBG ( "Insufficient memory\n" );
595 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
596 /* Set the advertised window */
597 conn->rcv_win = pkb_available ( conn->tx_pkb );
598 /* Call the senddata() call back function */
599 conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
600 pkb_available ( conn->tx_pkb ) );
607 * @v conn TCP connection
608 * @v data Data to be sent
609 * @v len Length of the data
611 * This function sends data to the peer socket address
613 int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
614 struct sockaddr_tcpip *peer = &conn->peer;
615 struct pk_buff *pkb = conn->tx_pkb;
618 /* Determine the amount of data to be sent */
619 slen = len < conn->snd_win ? len : conn->snd_win;
621 memmove ( pkb_put ( pkb, slen ), data, slen );
623 /* Fill up the TCP header */
624 struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
626 /* Source port, assumed to be in network byte order in conn */
627 tcphdr->src = conn->local_port;
628 /* Destination port, assumed to be in network byte order in peer */
629 tcphdr->dest = peer->st_port;
630 tcphdr->seq = htonl ( conn->snd_una );
631 tcphdr->ack = htonl ( conn->rcv_nxt );
632 /* Header length, = 0x50 (without TCP options) */
633 tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
634 /* Copy TCP flags, and then reset the variable */
635 tcphdr->flags = conn->tcp_flags;
637 /* Advertised window, in network byte order */
638 tcphdr->win = htons ( conn->rcv_win );
639 /* Set urgent pointer to 0 */
641 /* Calculate and store partial checksum, in host byte order */
643 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
645 /* Dump the TCP header */
647 /* Start the timer */
648 if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
649 ( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
650 ( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ) {
651 // Don't start the timer
653 start_timer ( &conn->timer );
656 /* Transmit packet */
657 return tcpip_tx ( pkb, &tcp_protocol, peer );
661 * Process received packet
663 * @v pkb Packet buffer
664 * @v partial Partial checksum
666 static int tcp_rx ( struct pk_buff *pkb,
667 struct sockaddr_tcpip *st_src __unused,
668 struct sockaddr_tcpip *st_dest __unused ) {
669 struct tcp_connection *conn;
670 struct tcp_header *tcphdr;
671 uint32_t acked, toack;
676 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
677 DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
682 /* Process TCP header */
686 /* Verify header length */
687 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
688 if ( hlen != sizeof ( *tcphdr ) ) {
689 if ( hlen == sizeof ( *tcphdr ) + 4 ) {
690 DBG ( "TCP options sent\n" );
693 DBG ( "Bad header length (%d bytes)\n", hlen );
699 /* TODO: Verify checksum */
701 /* Demux TCP connection */
702 list_for_each_entry ( conn, &tcp_conns, list ) {
703 if ( tcphdr->dest == conn->local_port ) {
708 DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
713 stop_timer ( &conn->timer );
715 /* Set the advertised window */
716 conn->snd_win = tcphdr->win;
718 /* TCP State Machine */
719 uint8_t out_flags = 0;
720 conn->tcp_lstate = conn->tcp_state;
721 switch ( conn->tcp_state ) {
723 DBG ( "tcp_rx(): Invalid state %s\n",
724 tcp_states[conn->tcp_state] );
727 if ( tcphdr->flags & TCP_SYN ) {
728 tcp_trans ( conn, TCP_SYN_RCVD );
729 /* Synchronize the sequence numbers */
730 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
731 out_flags |= TCP_ACK;
733 /* Set the sequence number for the snd stream */
734 conn->snd_una = ( ( ( uint32_t ) random() ) << 16 );
735 conn->snd_una &= random();
736 out_flags |= TCP_SYN;
738 /* Send a SYN,ACK packet */
741 /* Unexpected packet */
744 if ( tcphdr->flags & TCP_SYN ) {
745 /* Synchronize the sequence number in rcv stream */
746 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
747 out_flags |= TCP_ACK;
749 if ( tcphdr->flags & TCP_ACK ) {
750 tcp_trans ( conn, TCP_ESTABLISHED );
752 * Process ACK of SYN. This does not invoke the
753 * acked() callback function.
755 conn->snd_una = ntohl ( tcphdr->ack );
756 conn->tcp_op->connected ( conn );
757 out_flags |= TCP_ACK;
758 tcp_senddata ( conn );
761 tcp_trans ( conn, TCP_SYN_RCVD );
762 out_flags |= TCP_SYN;
766 /* Unexpected packet */
769 if ( tcphdr->flags & TCP_RST ) {
770 tcp_trans ( conn, TCP_LISTEN );
771 conn->tcp_op->closed ( conn, CONN_RESTART );
774 if ( tcphdr->flags & TCP_ACK ) {
775 tcp_trans ( conn, TCP_ESTABLISHED );
777 * Process ACK of SYN. It neither invokes the callback
778 * function nor does it send an ACK.
780 conn->snd_una = tcphdr->ack - 1;
781 conn->tcp_op->connected ( conn );
784 /* Unexpected packet */
786 case TCP_ESTABLISHED:
788 if ( tcphdr->flags & TCP_FIN ) {
789 tcp_trans ( conn, TCP_CLOSE_WAIT );
790 /* FIN consumes one byte */
792 out_flags |= TCP_ACK;
793 /* Send an acknowledgement */
797 /* Packet might contain data */
800 if ( tcphdr->flags & TCP_FIN ) {
802 out_flags |= TCP_ACK;
803 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
805 if ( tcphdr->flags & TCP_ACK ) {
806 tcp_trans ( conn, TCP_TIME_WAIT );
808 tcp_trans ( conn, TCP_CLOSING );
810 /* Send an acknowledgement */
813 if ( tcphdr->flags & TCP_ACK ) {
814 tcp_trans ( conn, TCP_FIN_WAIT_2 );
816 /* Packet might contain data */
819 if ( tcphdr->flags & TCP_FIN ) {
820 tcp_trans ( conn, TCP_TIME_WAIT );
821 /* FIN consumes one byte */
823 out_flags |= TCP_ACK;
826 /* Packet might contain data */
829 if ( tcphdr->flags & TCP_ACK ) {
830 tcp_trans ( conn, TCP_TIME_WAIT );
831 start_timer ( &conn->timer );
834 /* Unexpected packet */
837 /* Unexpected packet */
840 /* Packet could acknowledge data */
843 if ( tcphdr->flags & TCP_ACK ) {
844 tcp_trans ( conn, TCP_CLOSED );
847 /* Unexpected packet */
852 * Any packet reaching this point either contains new data or
853 * acknowledges previously transmitted data.
855 assert ( ( tcphdr->flags & TCP_ACK ) ||
856 pkb_len ( pkb ) > sizeof ( *tcphdr ) );
858 /* Check for new data */
859 toack = pkb_len ( pkb ) - hlen;
861 /* Check if expected sequence number */
862 if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
863 conn->rcv_nxt += toack;
864 conn->tcp_op->newdata ( conn, pkb->data + sizeof ( *tcphdr ) + add, toack );
866 printf ( "Unexpected sequence number %ld\n",
867 ntohl ( tcphdr->seq ) );
870 /* Acknowledge new data */
871 out_flags |= TCP_ACK;
872 if ( !( tcphdr->flags & TCP_ACK ) ) {
878 if ( tcphdr->flags & TCP_ACK ) {
879 acked = ntohl ( tcphdr->ack ) - conn->snd_una;
880 if ( acked < 0 ) { /* TODO: Replace all uint32_t arith */
881 DBG ( "Previously ACKed (%d)\n", tcphdr->ack );
884 /* Advance snd stream */
885 conn->snd_una += acked;
886 /* Invoke the acked() callback function */
887 conn->tcp_op->acked ( conn, acked );
888 /* Invoke the senddata() callback function */
889 tcp_senddata ( conn );
892 /* If the connection is in ESTABLISHED and it receives a FIN */
893 if ( conn->tcp_state == ESTABLISHED && ( tcphdr->flags & TCP_FIN ) ) {
894 tcp_trans ( conn, TCP_CLOSE_WAIT );
895 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
897 if ( ! ( tcphdr->flags & TCP_ACK ) ) {
898 out_flags |= TCP_ACK;
899 /* Send an acknowledgement */
902 /* Otherwise, the packet has been ACKed already */
907 free_pkb ( conn->tx_pkb );
908 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
909 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
911 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
912 DBG ( "Error sending TCP message (rc = %d)\n", rc );
917 DBG ( "Unexpected packet received in %s with flags = %#hx\n",
918 tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
920 free_pkb ( conn->tx_pkb );
925 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
928 .tcpip_proto = IP_TCP,