8 #include <gpxe/process.h>
10 #include <gpxe/netdevice.h>
11 #include <gpxe/pkbuff.h>
14 #include <gpxe/tcpip.h>
15 #include <gpxe/retry.h>
22 * The gPXE TCP stack is currently implemented on top of the uIP
23 * protocol stack. This file provides wrappers around uIP so that
24 * higher-level protocol implementations do not need to talk directly
25 * to uIP (which has a somewhat baroque API).
27 * Basic operation is to create a #tcp_connection structure, call
28 * tcp_connect() and then call run_tcpip() in a loop until the
29 * operation has completed. The TCP stack will call the various
30 * methods defined in the #tcp_operations structure in order to send
33 * See hello.c for a trivial example of a TCP protocol using this
43 * When a tcp_operations::senddata() method is called, it is
44 * guaranteed to be able to use this buffer as temporary space for
45 * constructing the data to be sent. For example, code such as
49 * static void my_senddata ( struct tcp_connection *conn, void *buf,
51 * len = snprintf ( buf, len, "FETCH %s\r\n", filename );
52 * tcp_send ( conn, buf + already_sent, len - already_sent );
57 * is allowed, and is probably the best way to deal with
58 * variably-sized data.
60 * Note that you cannot use this simple mechanism if you want to be
61 * able to construct single data blocks of more than #len bytes.
63 static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
65 /** Size of #tcp_buffer */
66 static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
69 * Open a TCP connection
71 * @v conn TCP connection
73 * This sets up a new TCP connection to the remote host specified in
74 * tcp_connection::sin.
76 void tcp_connect ( struct tcp_connection *conn ) {
77 struct uip_conn *uip_conn;
80 assert ( conn->sin.sin_addr.s_addr != 0 );
81 assert ( conn->sin.sin_port != 0 );
82 assert ( conn->tcp_op != NULL );
83 assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
85 * ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
86 uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
87 #warning "Use linked lists so that uip_connect() cannot fail"
88 assert ( uip_conn != NULL );
89 *( ( void ** ) uip_conn->appstate ) = conn;
93 * Send data via a TCP connection
95 * @v conn TCP connection
96 * @v data Data to send
97 * @v len Length of data
99 * Data will be automatically limited to the current TCP window size.
101 * If retransmission is required, the connection's
102 * tcp_operations::senddata() method will be called again in order to
103 * regenerate the data.
105 void tcp_send ( struct tcp_connection *conn __unused,
106 const void *data, size_t len ) {
108 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
110 if ( len > tcp_buflen )
112 memmove ( tcp_buffer, data, len );
114 uip_send ( tcp_buffer, len );
118 * Close a TCP connection
120 * @v conn TCP connection
122 void tcp_close ( struct tcp_connection *conn __unused ) {
123 assert ( conn = *( ( void ** ) uip_conn->appstate ) );
128 * uIP TCP application call interface
130 * This is the entry point of gPXE from the point of view of the uIP
131 * protocol stack. This function calls the appropriate methods from
132 * the connection's @tcp_operations table in order to process received
133 * data, transmit new data etc.
135 void uip_tcp_appcall ( void ) {
136 struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
137 struct tcp_operations *op = conn->tcp_op;
141 op->closed ( conn, -ECONNABORTED );
142 if ( uip_timedout() )
143 op->closed ( conn, -ETIMEDOUT );
145 op->closed ( conn, 0 );
147 if ( uip_connected() && op->connected )
148 op->connected ( conn );
149 if ( uip_acked() && op->acked )
150 op->acked ( conn, uip_conn->len );
151 if ( uip_newdata() && op->newdata )
152 op->newdata ( conn, ( void * ) uip_appdata, uip_len );
153 if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
154 uip_connected() || uip_poll() ) && op->senddata )
155 op->senddata ( conn, tcp_buffer, tcp_buflen );
158 /* Present here to allow everything to link. Will go into separate
161 void uip_udp_appcall ( void ) {
165 * Perform periodic processing of all TCP connections
167 * This allows TCP connections to retransmit data if necessary.
169 static void tcp_periodic ( void ) {
173 for ( i = 0 ; i < UIP_CONNS ; i++ ) {
176 pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
180 pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
181 pkb_put ( pkb, uip_len );
182 memcpy ( pkb->data, uip_buf, uip_len );
190 * Kick a connection into life
192 * @v conn TCP connection
194 * Call this function when you have new data to send and are not
195 * already being called as part of TCP processing.
197 void tcp_kick ( struct tcp_connection *conn __unused ) {
198 /* Just kick all the connections; this will work for now */
203 * Single-step the TCP stack
205 * @v process TCP process
207 * This calls tcp_periodic() at regular intervals.
209 static void tcp_step ( struct process *process ) {
210 static unsigned long timeout = 0;
212 if ( currticks() > timeout ) {
213 timeout = currticks() + ( TICKS_PER_SEC / 10 );
217 schedule ( process );
220 /** TCP stack process */
221 static struct process tcp_process = {
225 /** Initialise the TCP stack */
226 static void init_tcp ( void ) {
227 schedule ( &tcp_process );
230 INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );
235 * List of registered TCP connections
237 static LIST_HEAD ( tcp_conns );
242 static const char *tcp_states[] = {
257 * TCP state transition function
259 * @v conn TCP connection
260 * @v nxt_state Next TCP state
262 void tcp_set_flags ( struct tcp_connection *conn ) {
264 /* Set the TCP flags */
265 switch ( conn->tcp_state ) {
267 if ( conn->tcp_lstate == TCP_SYN_RCVD ) {
268 conn->tcp_flags |= TCP_RST;
274 if ( conn->tcp_lstate == TCP_LISTEN ||
275 conn->tcp_lstate == TCP_CLOSED ) {
276 conn->tcp_flags |= TCP_SYN;
280 if ( conn->tcp_lstate == TCP_LISTEN ||
281 conn->tcp_lstate == TCP_SYN_SENT ) {
282 conn->tcp_flags |= ( TCP_SYN | TCP_ACK );
285 case TCP_ESTABLISHED:
286 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
287 conn->tcp_flags |= TCP_ACK;
291 if ( conn->tcp_lstate == TCP_SYN_RCVD ||
292 conn->tcp_lstate == TCP_ESTABLISHED ) {
293 conn->tcp_flags |= TCP_FIN;
299 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ) {
300 conn->tcp_flags |= TCP_ACK;
304 if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ||
305 conn->tcp_lstate == TCP_FIN_WAIT_2 ) {
306 conn->tcp_flags |= TCP_ACK;
310 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
311 conn->tcp_flags |= TCP_ACK;
315 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
316 conn->tcp_flags |= TCP_FIN;
318 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
319 conn->tcp_flags |= ( TCP_FIN | TCP_ACK );
323 DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
328 void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
329 /* Remember the last state */
330 conn->tcp_lstate = conn->tcp_state;
331 conn->tcp_state = nxt_state;
333 DBG ( "Transition from %s to %s\n", tcp_states[conn->tcp_lstate], tcp_states[conn->tcp_state] );
335 /* TODO: Check if this check is required */
336 if ( conn->tcp_lstate == conn->tcp_state ||
337 conn->tcp_state == TCP_INVALID ) {
341 tcp_set_flags ( conn );
347 * @v tcphdr TCP header
349 void tcp_dump ( struct tcp_header *tcphdr ) {
350 DBG ( "TCP %p src:%d dest:%d seq:%lx ack:%lx hlen:%hd flags:%#hx\n",
351 tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
352 ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
356 * Initialize a TCP connection
358 * @v conn TCP connection
360 * This function assigns initial values to some fields in the connection
361 * structure. The application should call tcp_init_conn after creating a new
362 * connection before calling any other "tcp_*" function.
364 * struct tcp_connection my_conn;
365 * tcp_init_conn ( &my_conn );
368 void tcp_init_conn ( struct tcp_connection *conn ) {
369 conn->local_port = 0;
370 conn->tcp_state = TCP_CLOSED;
371 conn->tcp_lstate = TCP_INVALID;
378 * @v timer Retry timer
379 * @v over Failure indicator
381 void tcp_expired ( struct retry_timer *timer, int over ) {
382 struct tcp_connection *conn;
383 conn = ( struct tcp_connection * ) container_of ( timer,
384 struct tcp_connection, timer );
385 DBG ( "Timer expired in %s\n", tcp_states[conn->tcp_state] );
386 switch ( conn->tcp_state ) {
389 tcp_trans ( conn, TCP_CLOSED );
390 DBG ( "Timeout! Connection closed\n" );
396 tcp_trans ( conn, TCP_CLOSED );
400 case TCP_ESTABLISHED:
401 if ( conn->tcp_lstate == TCP_SYN_SENT ) {
406 if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
415 if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
420 tcp_trans ( conn, TCP_CLOSED );
423 /* Retransmit the data */
424 tcp_set_flags ( conn );
425 tcp_senddata ( conn );
429 free_pkb ( conn->tx_pkb );
430 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
431 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
432 tcp_set_flags ( conn );
434 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
435 DBG ( "Error sending TCP message (rc = %d)\n", rc );
441 * Connect to a remote server
443 * @v conn TCP connection
444 * @v peer Remote socket address
446 * This function initiates a TCP connection to the socket address specified in
447 * peer. It sends a SYN packet to peer. When the connection is established, the
448 * TCP stack calls the connected() callback function.
450 int tcp_connectto ( struct tcp_connection *conn,
451 struct sockaddr_tcpip *peer ) {
454 /* A connection can only be established from the CLOSED state */
455 if ( conn->tcp_state != TCP_CLOSED ) {
456 DBG ( "Error opening connection: Invalid state %s\n",
457 tcp_states[conn->tcp_state] );
461 /* Add the connection to the set of listening connections */
462 if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
465 memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
467 /* Initialize the TCP timer */
468 conn->timer.expired = tcp_expired;
470 /* Send a SYN packet and transition to TCP_SYN_SENT */
471 conn->snd_una = random();
472 tcp_trans ( conn, TCP_SYN_SENT );
473 /* Allocate space for the packet */
474 free_pkb ( conn->tx_pkb );
475 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
476 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
477 conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
478 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
481 int tcp_connect ( struct tcp_connection *conn ) {
482 return tcp_connectto ( conn, &conn->peer );
486 * Close the connection
490 * This function sends a FIN packet to the remote end of the connection. When
491 * the remote end of the connection ACKs the FIN (FIN consumes one byte on the
492 * snd stream), the stack invokes the closed() callback function.
494 int tcp_close ( struct tcp_connection *conn ) {
495 /* A connection can only be closed if it is a connected state */
496 switch ( conn->tcp_state ) {
498 case TCP_ESTABLISHED:
499 tcp_trans ( conn, TCP_FIN_WAIT_1 );
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 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
516 tcp_trans ( conn, TCP_LAST_ACK );
517 conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
518 /* FIN consumes one byte on the snd stream */
522 DBG ( "tcp_close(): Invalid state %s\n",
523 tcp_states[conn->tcp_state] );
528 free_pkb ( conn->tx_pkb );
529 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
530 conn->tcp_flags = TCP_FIN;
531 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
532 return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
536 * Bind TCP connection to local port
538 * @v conn TCP connection
539 * @v local_port Local port, in network byte order
540 * @ret rc Return status code
542 int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
543 struct tcp_connection *existing;
545 list_for_each_entry ( existing, &tcp_conns, list ) {
546 if ( existing->local_port == local_port )
549 conn->local_port = local_port;
555 * Listen for a packet
557 * @v conn TCP connection
558 * @v local_port Local port, in network byte order
560 * This function adds the connection to a list of registered tcp
561 * connections. If the local port is 0, the connection is assigned an
562 * available port between MIN_TCP_PORT and 65535.
564 int tcp_listen ( struct tcp_connection *conn, uint16_t local_port ) {
565 static uint16_t try_port = 1024;
568 #warning "Fix the port re-use bug"
569 /* If we re-use the same port, the connection should be reset
570 * and a new connection set up. This doesn't happen yet, so
571 * randomise the port to avoid hitting the problem.
575 /* If no port specified, find the first available port */
576 if ( ! local_port ) {
577 for ( ; try_port ; try_port++ ) {
578 if ( try_port < 1024 )
580 if ( tcp_listen ( conn, htons ( try_port ) ) == 0 )
586 /* Attempt bind to local port */
587 if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 )
590 /* Add to TCP connection list */
591 list_add ( &conn->list, &tcp_conns );
592 DBG ( "TCP opened %p on port %d\n", conn, ntohs ( local_port ) );
600 * @v conn TCP connection
602 * This function allocates space to the transmit buffer and invokes the
603 * senddata() callback function. It passes the allocated buffer to senddata().
604 * The applicaion may use this space to write it's data.
606 int tcp_senddata ( struct tcp_connection *conn ) {
607 /* The connection must be in a state in which the user can send data */
608 switch ( conn->tcp_state ) {
610 tcp_trans ( conn, TCP_SYN_SENT );
611 conn->snd_una = random();
613 case TCP_ESTABLISHED:
617 DBG ( "tcp_senddata: Invalid state %s\n",
618 tcp_states[conn->tcp_state] );
622 /* Allocate space to the TX buffer */
623 free_pkb ( conn->tx_pkb );
624 conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
625 if ( !conn->tx_pkb ) {
626 DBG ( "Insufficient memory\n" );
629 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
630 /* Set the advertised window */
631 conn->rcv_win = pkb_available ( conn->tx_pkb );
632 /* Call the senddata() call back function */
633 conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
634 pkb_available ( conn->tx_pkb ) );
635 /* Send pure ACK if senddata() didn't call tcp_send() */
636 if ( conn->tx_pkb ) {
637 tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
645 * @v conn TCP connection
646 * @v data Data to be sent
647 * @v len Length of the data
649 * This function sends data to the peer socket address
651 int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
652 struct sockaddr_tcpip *peer = &conn->peer;
656 /* Take ownership of the TX buffer from the connection */
660 /* Determine the amount of data to be sent */
661 slen = len < conn->snd_win ? len : conn->snd_win;
663 memmove ( pkb_put ( pkb, slen ), data, slen );
665 /* Fill up the TCP header */
666 struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
668 /* Source port, assumed to be in network byte order in conn */
669 tcphdr->src = conn->local_port;
670 /* Destination port, assumed to be in network byte order in peer */
671 tcphdr->dest = peer->st_port;
672 tcphdr->seq = htonl ( conn->snd_una );
673 tcphdr->ack = htonl ( conn->rcv_nxt );
674 /* Header length, = 0x50 (without TCP options) */
675 tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
676 /* Copy TCP flags, and then reset the variable */
677 tcphdr->flags = conn->tcp_flags;
679 /* Advertised window, in network byte order */
680 tcphdr->win = htons ( conn->rcv_win );
681 /* Set urgent pointer to 0 */
683 /* Calculate and store partial checksum, in host byte order */
685 tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
687 /* Dump the TCP header */
690 /* Start the timer */
691 if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
692 ( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
693 ( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ||
694 ( conn->tcp_state == TCP_ESTABLISHED && ( len == 0 ) ) ) {
695 // Don't start the timer
697 start_timer ( &conn->timer );
700 /* Transmit packet */
701 return tcpip_tx ( pkb, &tcp_protocol, peer );
705 * Process received packet
707 * @v pkb Packet buffer
708 * @v partial Partial checksum
710 static int tcp_rx ( struct pk_buff *pkb,
711 struct sockaddr_tcpip *st_src __unused,
712 struct sockaddr_tcpip *st_dest __unused ) {
713 struct tcp_connection *conn;
714 struct tcp_header *tcphdr;
715 uint32_t acked, toack;
720 if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
721 DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
726 /* Process TCP header */
730 /* Verify header length */
731 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
732 if ( hlen < sizeof ( *tcphdr ) ) {
733 DBG ( "Bad header length (%d bytes)\n", hlen );
737 /* TODO: Parse TCP options */
738 if ( hlen != sizeof ( *tcphdr ) ) {
739 DBG ( "Ignoring TCP options\n" );
742 /* TODO: Verify checksum */
744 /* Demux TCP connection */
745 list_for_each_entry ( conn, &tcp_conns, list ) {
746 if ( tcphdr->dest == conn->local_port ) {
751 DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
757 stop_timer ( &conn->timer );
759 /* Set the advertised window */
760 conn->snd_win = tcphdr->win;
762 /* TCP State Machine */
763 conn->tcp_lstate = conn->tcp_state;
764 switch ( conn->tcp_state ) {
766 DBG ( "tcp_rx(): Invalid state %s\n",
767 tcp_states[conn->tcp_state] );
771 if ( tcphdr->flags & TCP_SYN ) {
772 tcp_trans ( conn, TCP_SYN_RCVD );
773 /* Synchronize the sequence numbers */
774 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
775 conn->tcp_flags |= TCP_ACK;
777 /* Set the sequence number for the snd stream */
778 conn->snd_una = random();
779 conn->tcp_flags |= TCP_SYN;
781 /* Send a SYN,ACK packet */
784 /* Unexpected packet */
787 if ( tcphdr->flags & TCP_SYN ) {
788 /* Synchronize the sequence number in rcv stream */
789 conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
790 conn->tcp_flags |= TCP_ACK;
792 if ( tcphdr->flags & TCP_ACK ) {
793 tcp_trans ( conn, TCP_ESTABLISHED );
795 * Process ACK of SYN. This does not invoke the
796 * acked() callback function.
798 conn->snd_una = ntohl ( tcphdr->ack );
799 conn->tcp_op->connected ( conn );
800 conn->tcp_flags |= TCP_ACK;
801 tcp_senddata ( conn );
805 tcp_trans ( conn, TCP_SYN_RCVD );
806 conn->tcp_flags |= TCP_SYN;
810 /* Unexpected packet */
813 if ( tcphdr->flags & TCP_RST ) {
814 tcp_trans ( conn, TCP_LISTEN );
815 conn->tcp_op->closed ( conn, CONN_RESTART );
819 if ( tcphdr->flags & TCP_ACK ) {
820 tcp_trans ( conn, TCP_ESTABLISHED );
822 * Process ACK of SYN. It neither invokes the callback
823 * function nor does it send an ACK.
825 conn->snd_una = tcphdr->ack - 1;
826 conn->tcp_op->connected ( conn );
830 /* Unexpected packet */
832 case TCP_ESTABLISHED:
833 if ( tcphdr->flags & TCP_FIN ) {
834 if ( tcphdr->flags & TCP_ACK ) {
835 tcp_trans ( conn, TCP_LAST_ACK );
836 conn->tcp_flags |= TCP_FIN;
838 tcp_trans ( conn, TCP_CLOSE_WAIT );
840 /* FIN consumes one byte */
842 conn->tcp_flags |= TCP_ACK;
843 /* Send the packet */
846 /* Packet might contain data */
849 if ( tcphdr->flags & TCP_FIN ) {
851 conn->tcp_flags |= TCP_ACK;
852 conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
854 if ( tcphdr->flags & TCP_ACK ) {
855 tcp_trans ( conn, TCP_TIME_WAIT );
857 tcp_trans ( conn, TCP_CLOSING );
859 /* Send an acknowledgement */
862 if ( tcphdr->flags & TCP_ACK ) {
863 tcp_trans ( conn, TCP_FIN_WAIT_2 );
865 /* Packet might contain data */
868 if ( tcphdr->flags & TCP_FIN ) {
869 tcp_trans ( conn, TCP_TIME_WAIT );
870 /* FIN consumes one byte */
872 conn->tcp_flags |= TCP_ACK;
875 /* Packet might contain data */
878 if ( tcphdr->flags & TCP_ACK ) {
879 tcp_trans ( conn, TCP_TIME_WAIT );
880 start_timer ( &conn->timer );
884 /* Unexpected packet */
887 /* Unexpected packet */
890 /* Packet could acknowledge data */
893 if ( tcphdr->flags & TCP_ACK ) {
894 tcp_trans ( conn, TCP_CLOSED );
898 /* Unexpected packet */
903 * Any packet reaching this point either contains new data or
904 * acknowledges previously transmitted data.
906 assert ( ( tcphdr->flags & TCP_ACK ) ||
907 pkb_len ( pkb ) > sizeof ( *tcphdr ) );
910 * Check if the received packet ACKs sent data
912 if ( tcphdr->flags & TCP_ACK ) {
913 acked = ntohl ( tcphdr->ack ) - conn->snd_una;
915 /* Packet ACKs previously ACKed data */
916 DBG ( "Previously ACKed data %lx\n",
917 ntohl ( tcphdr->ack ) );
921 /* Invoke the acked() callback */
922 conn->snd_una += acked;
923 conn->tcp_op->acked ( conn, acked );
927 * Check if packet contains new data
929 toack = pkb_len ( pkb ) - hlen;
931 /* Check the sequence number */
932 if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
933 conn->rcv_nxt += toack;
934 conn->tcp_op->newdata ( conn,
935 pkb->data + hlen, toack );
937 DBG ( "Unexpected sequence number %lx (wanted %lx)\n",
938 ntohl ( tcphdr->ack ), conn->rcv_nxt );
940 conn->tcp_flags |= TCP_ACK;
946 tcp_senddata ( conn );
950 free_pkb ( conn->tx_pkb );
951 conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
952 pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
953 if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
954 DBG ( "Error sending TCP message (rc = %d)\n", rc );
959 DBG ( "Unexpected packet received in %s with flags = %#hx\n",
960 tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
962 free_pkb ( conn->tx_pkb );
973 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
976 .tcpip_proto = IP_TCP,