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;
317 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
318 conn->tcp_flags |= ( TCP_FIN | TCP_ACK );
322 DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
327 void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
328 /* Remember the last state */
329 conn->tcp_lstate = conn->tcp_state;
330 conn->tcp_state = nxt_state;
332 DBG ( "Transition from %s to %s\n", tcp_states[conn->tcp_lstate], tcp_states[conn->tcp_state] );
334 /* TODO: Check if this check is required */
335 if ( conn->tcp_lstate == conn->tcp_state ||
336 conn->tcp_state == TCP_INVALID ) {
340 tcp_set_flags ( conn );
346 * @v tcphdr TCP header
348 void tcp_dump ( struct tcp_header *tcphdr ) {
349 DBG ( "TCP %p src:%d dest:%d seq:%lx ack:%lx hlen:%hd flags:%#hx\n",
350 tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
351 ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
355 * Initialize a TCP connection
357 * @v conn TCP connection
359 * This function assigns initial values to some fields in the connection
360 * structure. The application should call tcp_init_conn after creating a new
361 * connection before calling any other "tcp_*" function.
363 * struct tcp_connection my_conn;
364 * tcp_init_conn ( &my_conn );
367 void tcp_init_conn ( struct tcp_connection *conn ) {
368 conn->local_port = 0;
369 conn->tcp_state = TCP_CLOSED;
370 conn->tcp_lstate = TCP_INVALID;
377 * @v timer Retry timer
378 * @v over Failure indicator
380 void tcp_expired ( struct retry_timer *timer, int over ) {
381 struct tcp_connection *conn;
382 conn = ( struct tcp_connection * ) container_of ( timer,
383 struct tcp_connection, timer );
384 DBG ( "Timer expired in %s\n", tcp_states[conn->tcp_state] );
385 switch ( conn->tcp_state ) {
388 tcp_trans ( conn, TCP_CLOSED );
389 DBG ( "Timeout! Connection closed\n" );
395 tcp_trans ( conn, TCP_CLOSED );
399 case TCP_ESTABLISHED:
400 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
405 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
414 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
419 tcp_trans ( conn, TCP_CLOSED );
422 /* Retransmit the data */
423 tcp_set_flags ( conn );
424 tcp_senddata ( conn );
428 free_pkb ( conn->tx_pkb );
429 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
430 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
431 tcp_set_flags ( conn );
433 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
434 DBG ( "Error sending TCP message (rc = %d)\n", rc );
440 * Connect to a remote server
442 * @v conn TCP connection
443 * @v peer Remote socket address
445 * This function initiates a TCP connection to the socket address specified in
446 * peer. It sends a SYN packet to peer. When the connection is established, the
447 * TCP stack calls the connected() callback function.
449 int tcp_connectto ( struct tcp_connection *conn,
450 struct sockaddr_tcpip *peer ) {
453 /* A connection can only be established from the CLOSED state */
454 if ( conn->tcp_state != TCP_CLOSED ) {
455 DBG ( "Error opening connection: Invalid state %s\n",
456 tcp_states[conn->tcp_state] );
460 /* Add the connection to the set of listening connections */
461 if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
464 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
466 /* Initialize the TCP timer */
467 conn->timer.expired = tcp_expired;
469 /* Send a SYN packet and transition to TCP_SYN_SENT */
470 conn->snd_una = random();
471 tcp_trans ( conn, TCP_SYN_SENT );
472 /* Allocate space for the packet */
473 free_pkb ( conn->tx_pkb );
474 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
475 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
476 conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
477 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
480 int tcp_connect ( struct tcp_connection *conn ) {
481 return tcp_connectto ( conn, &conn->peer );
485 * Close the connection
489 * This function sends a FIN packet to the remote end of the connection. When
490 * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
491 * snd stream), the stack invokes the closed() callback function.
493 int tcp_close ( struct tcp_connection *conn ) {
494 /* A connection can only be closed if it is a connected state */
495 switch ( conn->tcp_state ) {
497 case TCP_ESTABLISHED:
498 tcp_trans ( conn, TCP_FIN_WAIT_1 );
499 if ( conn->tcp_op->closed )
500 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
501 /* FIN consumes one byte on the snd stream */
507 * Since the connection does not expect any packets from the
508 * remote end, it can be removed from the set of listening
511 list_del ( &conn->list );
512 tcp_trans ( conn, TCP_CLOSED );
513 if ( conn->tcp_op->closed )
514 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
517 tcp_trans ( conn, TCP_LAST_ACK );
518 if ( conn->tcp_op->closed )
519 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
520 /* FIN consumes one byte on the snd stream */
524 DBG ( "tcp_close(): Invalid state %s\n",
525 tcp_states[conn->tcp_state] );
530 free_pkb ( conn->tx_pkb );
531 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
532 conn->tcp_flags = TCP_FIN;
533 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
534 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
538 * Bind TCP connection to local port
540 * @v conn TCP connection
541 * @v local_port Local port, in network byte order
542 * @ret rc Return status code
544 int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
545 struct tcp_connection *existing;
547 list_for_each_entry ( existing, &tcp_conns, list ) {
548 if ( existing->local_port == local_port )
551 conn->local_port = local_port;
557 * Listen for a packet
559 * @v conn TCP connection
560 * @v local_port Local port, in network byte order
562 * This function adds the connection to a list of registered tcp
563 * connections. If the local port is 0, the connection is assigned an
564 * available port between MIN_TCP_PORT and 65535.
566 int tcp_listen ( struct tcp_connection *conn, uint16_t local_port ) {
567 static uint16_t try_port = 1024;
570 #warning "Fix the port re-use bug"
571 /* If we re-use the same port, the connection should be reset
572 * and a new connection set up. This doesn't happen yet, so
573 * randomise the port to avoid hitting the problem.
577 /* If no port specified, find the first available port */
578 if ( ! local_port ) {
579 for ( ; try_port ; try_port++ ) {
580 if ( try_port < 1024 )
582 if ( tcp_listen ( conn, htons ( try_port ) ) == 0 )
588 /* Attempt bind to local port */
589 if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 )
592 /* Add to TCP connection list */
593 list_add ( &conn->list, &tcp_conns );
594 DBG ( "TCP opened %p on port %d\n", conn, ntohs ( local_port ) );
602 * @v conn TCP connection
604 * This function allocates space to the transmit buffer and invokes the
605 * senddata() callback function. It passes the allocated buffer to senddata().
606 * The applicaion may use this space to write it's data.
608 int tcp_senddata ( struct tcp_connection *conn ) {
609 /* The connection must be in a state in which the user can send data */
610 switch ( conn->tcp_state ) {
612 tcp_trans ( conn, TCP_SYN_SENT );
613 conn->snd_una = random();
615 case TCP_ESTABLISHED:
619 DBG ( "tcp_senddata: Invalid state %s\n",
620 tcp_states[conn->tcp_state] );
624 /* Allocate space to the TX buffer */
625 free_pkb ( conn->tx_pkb );
626 conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
627 if ( !conn->tx_pkb ) {
628 DBG ( "Insufficient memory\n" );
631 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
632 /* Set the advertised window */
633 conn->rcv_win = pkb_available ( conn->tx_pkb );
634 /* Call the senddata() call back function */
635 if ( conn->tcp_op->senddata )
636 conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
637 pkb_available ( conn->tx_pkb ) );
638 /* Send pure ACK if senddata() didn't call tcp_send() */
639 if ( conn->tx_pkb ) {
640 tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
648 * @v conn TCP connection
649 * @v data Data to be sent
650 * @v len Length of the data
652 * This function sends data to the peer socket address
654 int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
655 struct sockaddr_tcpip *peer = &conn->peer;
659 /* Take ownership of the TX buffer from the connection */
663 /* Determine the amount of data to be sent */
664 slen = len < conn->snd_win ? len : conn->snd_win;
666 memmove ( pkb_put ( pkb, slen ), data, slen );
668 /* Fill up the TCP header */
669 struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
671 /* Source port, assumed to be in network byte order in conn */
672 tcphdr->src = conn->local_port;
673 /* Destination port, assumed to be in network byte order in peer */
674 tcphdr->dest = peer->st_port;
675 tcphdr->seq = htonl ( conn->snd_una );
676 tcphdr->ack = htonl ( conn->rcv_nxt );
677 /* Header length, = 0x50 (without TCP options) */
678 tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
679 /* Copy TCP flags, and then reset the variable */
680 tcphdr->flags = conn->tcp_flags;
682 /* Advertised window, in network byte order */
683 tcphdr->win = htons ( conn->rcv_win );
684 /* Set urgent pointer to 0 */
686 /* Calculate and store partial checksum, in host byte order */
688 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
690 /* Dump the TCP header */
693 /* Start the timer */
694 if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
695 ( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
696 ( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ||
697 ( conn->tcp_state == TCP_ESTABLISHED && ( len == 0 ) ) ) {
698 // Don't start the timer
700 start_timer ( &conn->timer );
703 /* Transmit packet */
704 return tcpip_tx ( pkb, &tcp_protocol, peer );
708 * Process received packet
710 * @v pkb Packet buffer
711 * @v partial Partial checksum
713 static int tcp_rx ( struct pk_buff *pkb,
714 struct sockaddr_tcpip *st_src __unused,
715 struct sockaddr_tcpip *st_dest __unused ) {
716 struct tcp_connection *conn;
717 struct tcp_header *tcphdr;
718 uint32_t acked, toack;
723 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
724 DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
729 /* Process TCP header */
733 /* Verify header length */
734 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
735 if ( hlen < sizeof ( *tcphdr ) ) {
736 DBG ( "Bad header length (%d bytes)\n", hlen );
740 /* TODO: Parse TCP options */
741 if ( hlen != sizeof ( *tcphdr ) ) {
742 DBG ( "Ignoring TCP options\n" );
745 /* TODO: Verify checksum */
747 /* Demux TCP connection */
748 list_for_each_entry ( conn, &tcp_conns, list ) {
749 if ( tcphdr->dest == conn->local_port ) {
754 DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
760 stop_timer ( &conn->timer );
762 /* Set the advertised window */
763 conn->snd_win = tcphdr->win;
765 /* TCP State Machine */
766 conn->tcp_lstate = conn->tcp_state;
767 switch ( conn->tcp_state ) {
769 DBG ( "tcp_rx(): Invalid state %s\n",
770 tcp_states[conn->tcp_state] );
774 if ( tcphdr->flags & TCP_SYN ) {
775 tcp_trans ( conn, TCP_SYN_RCVD );
776 /* Synchronize the sequence numbers */
777 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
778 conn->tcp_flags |= TCP_ACK;
780 /* Set the sequence number for the snd stream */
781 conn->snd_una = random();
782 conn->tcp_flags |= TCP_SYN;
784 /* Send a SYN,ACK packet */
787 /* Unexpected packet */
790 if ( tcphdr->flags & TCP_SYN ) {
791 /* Synchronize the sequence number in rcv stream */
792 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
793 conn->tcp_flags |= TCP_ACK;
795 if ( tcphdr->flags & TCP_ACK ) {
796 tcp_trans ( conn, TCP_ESTABLISHED );
798 * Process ACK of SYN. This does not invoke the
799 * acked() callback function.
801 conn->snd_una = ntohl ( tcphdr->ack );
802 if ( conn->tcp_op->connected )
803 conn->tcp_op->connected ( conn );
804 conn->tcp_flags |= TCP_ACK;
805 tcp_senddata ( conn );
809 tcp_trans ( conn, TCP_SYN_RCVD );
810 conn->tcp_flags |= TCP_SYN;
814 /* Unexpected packet */
817 if ( tcphdr->flags & TCP_RST ) {
818 tcp_trans ( conn, TCP_LISTEN );
819 if ( conn->tcp_op->closed )
820 conn->tcp_op->closed ( conn, CONN_RESTART );
824 if ( tcphdr->flags & TCP_ACK ) {
825 tcp_trans ( conn, TCP_ESTABLISHED );
827 * Process ACK of SYN. It neither invokes the callback
828 * function nor does it send an ACK.
830 conn->snd_una = tcphdr->ack - 1;
831 if ( conn->tcp_op->connected )
832 conn->tcp_op->connected ( conn );
836 /* Unexpected packet */
838 case TCP_ESTABLISHED:
839 if ( tcphdr->flags & TCP_FIN ) {
840 if ( tcphdr->flags & TCP_ACK ) {
841 tcp_trans ( conn, TCP_LAST_ACK );
842 conn->tcp_flags |= TCP_FIN;
844 tcp_trans ( conn, TCP_CLOSE_WAIT );
846 /* FIN consumes one byte */
848 conn->tcp_flags |= TCP_ACK;
849 /* Send the packet */
852 /* Packet might contain data */
855 if ( tcphdr->flags & TCP_FIN ) {
857 conn->tcp_flags |= TCP_ACK;
858 if ( conn->tcp_op->closed )
859 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
861 if ( tcphdr->flags & TCP_ACK ) {
862 tcp_trans ( conn, TCP_TIME_WAIT );
864 tcp_trans ( conn, TCP_CLOSING );
866 /* Send an acknowledgement */
869 if ( tcphdr->flags & TCP_ACK ) {
870 tcp_trans ( conn, TCP_FIN_WAIT_2 );
872 /* Packet might contain data */
875 if ( tcphdr->flags & TCP_FIN ) {
876 tcp_trans ( conn, TCP_TIME_WAIT );
877 /* FIN consumes one byte */
879 conn->tcp_flags |= TCP_ACK;
882 /* Packet might contain data */
885 if ( tcphdr->flags & TCP_ACK ) {
886 tcp_trans ( conn, TCP_TIME_WAIT );
887 start_timer ( &conn->timer );
891 /* Unexpected packet */
894 /* Unexpected packet */
897 /* Packet could acknowledge data */
900 if ( tcphdr->flags & TCP_ACK ) {
901 tcp_trans ( conn, TCP_CLOSED );
905 /* Unexpected packet */
910 * Any packet reaching this point either contains new data or
911 * acknowledges previously transmitted data.
913 assert ( ( tcphdr->flags & TCP_ACK ) ||
914 pkb_len ( pkb ) > sizeof ( *tcphdr ) );
917 * Check if the received packet ACKs sent data
919 if ( tcphdr->flags & TCP_ACK ) {
920 acked = ntohl ( tcphdr->ack ) - conn->snd_una;
922 /* Packet ACKs previously ACKed data */
923 DBG ( "Previously ACKed data %lx\n",
924 ntohl ( tcphdr->ack ) );
928 /* Invoke the acked() callback */
929 conn->snd_una += acked;
930 if ( conn->tcp_op->acked )
931 conn->tcp_op->acked ( conn, acked );
935 * Check if packet contains new data
937 toack = pkb_len ( pkb ) - hlen;
939 /* Check the sequence number */
940 if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
941 conn->rcv_nxt += toack;
942 if ( conn->tcp_op->newdata )
943 conn->tcp_op->newdata ( conn, pkb->data + hlen,
946 DBG ( "Unexpected sequence number %lx (wanted %lx)\n",
947 ntohl ( tcphdr->ack ), conn->rcv_nxt );
949 conn->tcp_flags |= TCP_ACK;
955 tcp_senddata ( conn );
959 free_pkb ( conn->tx_pkb );
960 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
961 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
962 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
963 DBG ( "Error sending TCP message (rc = %d)\n", rc );
968 DBG ( "Unexpected packet received in %s with flags = %#hx\n",
969 tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
971 free_pkb ( conn->tx_pkb );
982 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
985 .tcpip_proto = IP_TCP,