[tcp] Fix a 64bit compile time error
[gpxe.git] / src / net / tcp.c
blob637bfce3096469ad92ce2b0db7180458c42d72ed
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <byteswap.h>
7 #include <gpxe/timer.h>
8 #include <gpxe/iobuf.h>
9 #include <gpxe/malloc.h>
10 #include <gpxe/retry.h>
11 #include <gpxe/refcnt.h>
12 #include <gpxe/xfer.h>
13 #include <gpxe/open.h>
14 #include <gpxe/uri.h>
15 #include <gpxe/tcpip.h>
16 #include <gpxe/tcp.h>
18 /** @file
20 * TCP protocol
24 FILE_LICENCE ( GPL2_OR_LATER );
26 /** A TCP connection */
27 struct tcp_connection {
28 /** Reference counter */
29 struct refcnt refcnt;
30 /** List of TCP connections */
31 struct list_head list;
33 /** Flags */
34 unsigned int flags;
36 /** Data transfer interface */
37 struct xfer_interface xfer;
39 /** Remote socket address */
40 struct sockaddr_tcpip peer;
41 /** Local port */
42 unsigned int local_port;
44 /** Current TCP state */
45 unsigned int tcp_state;
46 /** Previous TCP state
48 * Maintained only for debug messages
50 unsigned int prev_tcp_state;
51 /** Current sequence number
53 * Equivalent to SND.UNA in RFC 793 terminology.
55 uint32_t snd_seq;
56 /** Unacknowledged sequence count
58 * Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
60 uint32_t snd_sent;
61 /** Send window
63 * Equivalent to SND.WND in RFC 793 terminology
65 uint32_t snd_win;
66 /** Current acknowledgement number
68 * Equivalent to RCV.NXT in RFC 793 terminology.
70 uint32_t rcv_ack;
71 /** Receive window
73 * Equivalent to RCV.WND in RFC 793 terminology.
75 uint32_t rcv_win;
76 /** Most recent received timestamp
78 * Equivalent to TS.Recent in RFC 1323 terminology.
80 uint32_t ts_recent;
82 /** Transmit queue */
83 struct list_head tx_queue;
84 /** Receive queue */
85 struct list_head rx_queue;
86 /** Retransmission timer */
87 struct retry_timer timer;
88 /** Shutdown (TIME_WAIT) timer */
89 struct retry_timer wait;
92 /** TCP flags */
93 enum tcp_flags {
94 /** TCP data transfer interface has been closed */
95 TCP_XFER_CLOSED = 0x0001,
96 /** TCP timestamps are enabled */
97 TCP_TS_ENABLED = 0x0002,
98 /** TCP acknowledgement is pending */
99 TCP_ACK_PENDING = 0x0004,
102 /** TCP internal header
104 * This is the header that replaces the TCP header for packets
105 * enqueued on the receive queue.
107 struct tcp_rx_queued_header {
108 /** SEQ value, in host-endian order
110 * This represents the SEQ value at the time the packet is
111 * enqueued, and so excludes the SYN, if present.
113 uint32_t seq;
114 /** Flags
116 * Only FIN is valid within this flags byte; all other flags
117 * have already been processed by the time the packet is
118 * enqueued.
120 uint8_t flags;
121 /** Reserved */
122 uint8_t reserved[3];
126 * List of registered TCP connections
128 static LIST_HEAD ( tcp_conns );
130 /* Forward declarations */
131 static struct xfer_interface_operations tcp_xfer_operations;
132 static void tcp_expired ( struct retry_timer *timer, int over );
133 static void tcp_wait_expired ( struct retry_timer *timer, int over );
134 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
135 uint32_t win );
138 * Name TCP state
140 * @v state TCP state
141 * @ret name Name of TCP state
143 static inline __attribute__ (( always_inline )) const char *
144 tcp_state ( int state ) {
145 switch ( state ) {
146 case TCP_CLOSED: return "CLOSED";
147 case TCP_LISTEN: return "LISTEN";
148 case TCP_SYN_SENT: return "SYN_SENT";
149 case TCP_SYN_RCVD: return "SYN_RCVD";
150 case TCP_ESTABLISHED: return "ESTABLISHED";
151 case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
152 case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
153 case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
154 case TCP_TIME_WAIT: return "TIME_WAIT";
155 case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
156 default: return "INVALID";
161 * Dump TCP state transition
163 * @v tcp TCP connection
165 static inline __attribute__ (( always_inline )) void
166 tcp_dump_state ( struct tcp_connection *tcp ) {
168 if ( tcp->tcp_state != tcp->prev_tcp_state ) {
169 DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
170 tcp_state ( tcp->prev_tcp_state ),
171 tcp_state ( tcp->tcp_state ) );
173 tcp->prev_tcp_state = tcp->tcp_state;
177 * Dump TCP flags
179 * @v flags TCP flags
181 static inline __attribute__ (( always_inline )) void
182 tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
183 if ( flags & TCP_RST )
184 DBGC2 ( tcp, " RST" );
185 if ( flags & TCP_SYN )
186 DBGC2 ( tcp, " SYN" );
187 if ( flags & TCP_PSH )
188 DBGC2 ( tcp, " PSH" );
189 if ( flags & TCP_FIN )
190 DBGC2 ( tcp, " FIN" );
191 if ( flags & TCP_ACK )
192 DBGC2 ( tcp, " ACK" );
195 /***************************************************************************
197 * Open and close
199 ***************************************************************************
203 * Bind TCP connection to local port
205 * @v tcp TCP connection
206 * @v port Local port number
207 * @ret rc Return status code
209 * If the port is 0, the connection is assigned an available port
210 * between 1024 and 65535.
212 static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
213 struct tcp_connection *existing;
214 uint16_t try_port;
215 int i;
217 /* If no port specified, find an available port */
218 if ( ! port ) {
219 try_port = ( random() % 64512 ) + 1023;
220 for ( i = 0 ; i < 65536 ; ++i ) {
221 if ( ++try_port < 1024 )
222 continue;
223 if ( tcp_bind ( tcp, try_port ) == 0 )
224 return 0;
226 DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
227 return -EADDRINUSE;
230 /* Attempt bind to local port */
231 list_for_each_entry ( existing, &tcp_conns, list ) {
232 if ( existing->local_port == port ) {
233 DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
234 tcp, port );
235 return -EADDRINUSE;
238 tcp->local_port = port;
240 DBGC ( tcp, "TCP %p bound to port %d\n", tcp, port );
241 return 0;
245 * Open a TCP connection
247 * @v xfer Data transfer interface
248 * @v peer Peer socket address
249 * @v local Local socket address, or NULL
250 * @ret rc Return status code
252 static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
253 struct sockaddr *local ) {
254 struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
255 struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
256 struct tcp_connection *tcp;
257 unsigned int bind_port;
258 int rc;
260 /* Allocate and initialise structure */
261 tcp = zalloc ( sizeof ( *tcp ) );
262 if ( ! tcp )
263 return -ENOMEM;
264 DBGC ( tcp, "TCP %p allocated\n", tcp );
265 ref_init ( &tcp->refcnt, NULL );
266 xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
267 timer_init ( &tcp->timer, tcp_expired );
268 timer_init ( &tcp->wait, tcp_wait_expired );
269 tcp->prev_tcp_state = TCP_CLOSED;
270 tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
271 tcp_dump_state ( tcp );
272 tcp->snd_seq = random();
273 INIT_LIST_HEAD ( &tcp->tx_queue );
274 INIT_LIST_HEAD ( &tcp->rx_queue );
275 memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
277 /* Bind to local port */
278 bind_port = ( st_local ? ntohs ( st_local->st_port ) : 0 );
279 if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
280 goto err;
282 /* Start timer to initiate SYN */
283 start_timer_nodelay ( &tcp->timer );
285 /* Attach parent interface, transfer reference to connection
286 * list and return
288 xfer_plug_plug ( &tcp->xfer, xfer );
289 list_add ( &tcp->list, &tcp_conns );
290 return 0;
292 err:
293 ref_put ( &tcp->refcnt );
294 return rc;
298 * Close TCP connection
300 * @v tcp TCP connection
301 * @v rc Reason for close
303 * Closes the data transfer interface. If the TCP state machine is in
304 * a suitable state, the connection will be deleted.
306 static void tcp_close ( struct tcp_connection *tcp, int rc ) {
307 struct io_buffer *iobuf;
308 struct io_buffer *tmp;
310 /* Close data transfer interface */
311 xfer_nullify ( &tcp->xfer );
312 xfer_close ( &tcp->xfer, rc );
313 tcp->flags |= TCP_XFER_CLOSED;
315 /* If we are in CLOSED, or have otherwise not yet received a
316 * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
317 * connection.
319 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
321 /* Transition to CLOSED for the sake of debugging messages */
322 tcp->tcp_state = TCP_CLOSED;
323 tcp_dump_state ( tcp );
325 /* Free any unprocessed I/O buffers */
326 list_for_each_entry_safe ( iobuf, tmp, &tcp->rx_queue, list ) {
327 list_del ( &iobuf->list );
328 free_iob ( iobuf );
331 /* Free any unsent I/O buffers */
332 list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
333 list_del ( &iobuf->list );
334 free_iob ( iobuf );
337 /* Remove from list and drop reference */
338 stop_timer ( &tcp->timer );
339 list_del ( &tcp->list );
340 ref_put ( &tcp->refcnt );
341 DBGC ( tcp, "TCP %p connection deleted\n", tcp );
342 return;
345 /* If we have not had our SYN acknowledged (i.e. we are in
346 * SYN_RCVD), pretend that it has been acknowledged so that we
347 * can send a FIN without breaking things.
349 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
350 tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
352 /* If we have no data remaining to send, start sending FIN */
353 if ( list_empty ( &tcp->tx_queue ) ) {
354 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
355 tcp_dump_state ( tcp );
359 /***************************************************************************
361 * Transmit data path
363 ***************************************************************************
367 * Calculate transmission window
369 * @v tcp TCP connection
370 * @ret len Maximum length that can be sent in a single packet
372 static size_t tcp_xmit_win ( struct tcp_connection *tcp ) {
373 size_t len;
375 /* Not ready if we're not in a suitable connection state */
376 if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
377 return 0;
379 /* Length is the minimum of the receiver's window and the path MTU */
380 len = tcp->snd_win;
381 if ( len > TCP_PATH_MTU )
382 len = TCP_PATH_MTU;
384 return len;
388 * Process TCP transmit queue
390 * @v tcp TCP connection
391 * @v max_len Maximum length to process
392 * @v dest I/O buffer to fill with data, or NULL
393 * @v remove Remove data from queue
394 * @ret len Length of data processed
396 * This processes at most @c max_len bytes from the TCP connection's
397 * transmit queue. Data will be copied into the @c dest I/O buffer
398 * (if provided) and, if @c remove is true, removed from the transmit
399 * queue.
401 static size_t tcp_process_tx_queue ( struct tcp_connection *tcp, size_t max_len,
402 struct io_buffer *dest, int remove ) {
403 struct io_buffer *iobuf;
404 struct io_buffer *tmp;
405 size_t frag_len;
406 size_t len = 0;
408 list_for_each_entry_safe ( iobuf, tmp, &tcp->tx_queue, list ) {
409 frag_len = iob_len ( iobuf );
410 if ( frag_len > max_len )
411 frag_len = max_len;
412 if ( dest ) {
413 memcpy ( iob_put ( dest, frag_len ), iobuf->data,
414 frag_len );
416 if ( remove ) {
417 iob_pull ( iobuf, frag_len );
418 if ( ! iob_len ( iobuf ) ) {
419 list_del ( &iobuf->list );
420 free_iob ( iobuf );
423 len += frag_len;
424 max_len -= frag_len;
426 return len;
430 * Transmit any outstanding data
432 * @v tcp TCP connection
434 * Transmits any outstanding data on the connection.
436 * Note that even if an error is returned, the retransmission timer
437 * will have been started if necessary, and so the stack will
438 * eventually attempt to retransmit the failed packet.
440 static int tcp_xmit ( struct tcp_connection *tcp ) {
441 struct io_buffer *iobuf;
442 struct tcp_header *tcphdr;
443 struct tcp_mss_option *mssopt;
444 struct tcp_timestamp_padded_option *tsopt;
445 void *payload;
446 unsigned int flags;
447 size_t len = 0;
448 uint32_t seq_len;
449 uint32_t app_win;
450 uint32_t max_rcv_win;
451 int rc;
453 /* If retransmission timer is already running, do nothing */
454 if ( timer_running ( &tcp->timer ) )
455 return 0;
457 /* Calculate both the actual (payload) and sequence space
458 * lengths that we wish to transmit.
460 if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
461 len = tcp_process_tx_queue ( tcp, tcp_xmit_win ( tcp ),
462 NULL, 0 );
464 seq_len = len;
465 flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
466 if ( flags & ( TCP_SYN | TCP_FIN ) ) {
467 /* SYN or FIN consume one byte, and we can never send both */
468 assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
469 seq_len++;
471 tcp->snd_sent = seq_len;
473 /* If we have nothing to transmit, stop now */
474 if ( ( seq_len == 0 ) && ! ( tcp->flags & TCP_ACK_PENDING ) )
475 return 0;
477 /* If we are transmitting anything that requires
478 * acknowledgement (i.e. consumes sequence space), start the
479 * retransmission timer. Do this before attempting to
480 * allocate the I/O buffer, in case allocation itself fails.
482 if ( seq_len )
483 start_timer ( &tcp->timer );
485 /* Allocate I/O buffer */
486 iobuf = alloc_iob ( len + MAX_HDR_LEN );
487 if ( ! iobuf ) {
488 DBGC ( tcp, "TCP %p could not allocate iobuf for %08x..%08x "
489 "%08x\n", tcp, tcp->snd_seq, ( tcp->snd_seq + seq_len ),
490 tcp->rcv_ack );
491 return -ENOMEM;
493 iob_reserve ( iobuf, MAX_HDR_LEN );
495 /* Fill data payload from transmit queue */
496 tcp_process_tx_queue ( tcp, len, iobuf, 0 );
498 /* Expand receive window if possible */
499 max_rcv_win = ( ( freemem * 3 ) / 4 );
500 if ( max_rcv_win > TCP_MAX_WINDOW_SIZE )
501 max_rcv_win = TCP_MAX_WINDOW_SIZE;
502 app_win = xfer_window ( &tcp->xfer );
503 if ( max_rcv_win > app_win )
504 max_rcv_win = app_win;
505 max_rcv_win &= ~0x03; /* Keep everything dword-aligned */
506 if ( tcp->rcv_win < max_rcv_win )
507 tcp->rcv_win = max_rcv_win;
509 /* Fill up the TCP header */
510 payload = iobuf->data;
511 if ( flags & TCP_SYN ) {
512 mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
513 mssopt->kind = TCP_OPTION_MSS;
514 mssopt->length = sizeof ( *mssopt );
515 mssopt->mss = htons ( TCP_MSS );
517 if ( ( flags & TCP_SYN ) || ( tcp->flags & TCP_TS_ENABLED ) ) {
518 tsopt = iob_push ( iobuf, sizeof ( *tsopt ) );
519 memset ( tsopt->nop, TCP_OPTION_NOP, sizeof ( tsopt->nop ) );
520 tsopt->tsopt.kind = TCP_OPTION_TS;
521 tsopt->tsopt.length = sizeof ( tsopt->tsopt );
522 tsopt->tsopt.tsval = htonl ( currticks() );
523 tsopt->tsopt.tsecr = htonl ( tcp->ts_recent );
525 if ( ! ( flags & TCP_SYN ) )
526 flags |= TCP_PSH;
527 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
528 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
529 tcphdr->src = htons ( tcp->local_port );
530 tcphdr->dest = tcp->peer.st_port;
531 tcphdr->seq = htonl ( tcp->snd_seq );
532 tcphdr->ack = htonl ( tcp->rcv_ack );
533 tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
534 tcphdr->flags = flags;
535 tcphdr->win = htons ( tcp->rcv_win );
536 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
538 /* Dump header */
539 DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4zd",
540 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
541 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
542 ntohl ( tcphdr->ack ), len );
543 tcp_dump_flags ( tcp, tcphdr->flags );
544 DBGC2 ( tcp, "\n" );
546 /* Transmit packet */
547 if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, &tcp->peer, NULL,
548 &tcphdr->csum ) ) != 0 ) {
549 DBGC ( tcp, "TCP %p could not transmit %08x..%08x %08x: %s\n",
550 tcp, tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ),
551 tcp->rcv_ack, strerror ( rc ) );
552 return rc;
555 /* Clear ACK-pending flag */
556 tcp->flags &= ~TCP_ACK_PENDING;
558 return 0;
562 * Retransmission timer expired
564 * @v timer Retransmission timer
565 * @v over Failure indicator
567 static void tcp_expired ( struct retry_timer *timer, int over ) {
568 struct tcp_connection *tcp =
569 container_of ( timer, struct tcp_connection, timer );
571 DBGC ( tcp, "TCP %p timer %s in %s for %08x..%08x %08x\n", tcp,
572 ( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ),
573 tcp->snd_seq, ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
575 assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
576 ( tcp->tcp_state == TCP_SYN_RCVD ) ||
577 ( tcp->tcp_state == TCP_ESTABLISHED ) ||
578 ( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
579 ( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
580 ( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
582 if ( over ) {
583 /* If we have finally timed out and given up,
584 * terminate the connection
586 tcp->tcp_state = TCP_CLOSED;
587 tcp_dump_state ( tcp );
588 tcp_close ( tcp, -ETIMEDOUT );
589 } else {
590 /* Otherwise, retransmit the packet */
591 tcp_xmit ( tcp );
596 * Shutdown timer expired
598 * @v timer Shutdown timer
599 * @v over Failure indicator
601 static void tcp_wait_expired ( struct retry_timer *timer, int over __unused ) {
602 struct tcp_connection *tcp =
603 container_of ( timer, struct tcp_connection, wait );
605 assert ( tcp->tcp_state == TCP_TIME_WAIT );
607 DBGC ( tcp, "TCP %p wait complete in %s for %08x..%08x %08x\n", tcp,
608 tcp_state ( tcp->tcp_state ), tcp->snd_seq,
609 ( tcp->snd_seq + tcp->snd_sent ), tcp->rcv_ack );
611 tcp->tcp_state = TCP_CLOSED;
612 tcp_dump_state ( tcp );
613 tcp_close ( tcp, 0 );
617 * Send RST response to incoming packet
619 * @v in_tcphdr TCP header of incoming packet
620 * @ret rc Return status code
622 static int tcp_xmit_reset ( struct tcp_connection *tcp,
623 struct sockaddr_tcpip *st_dest,
624 struct tcp_header *in_tcphdr ) {
625 struct io_buffer *iobuf;
626 struct tcp_header *tcphdr;
627 int rc;
629 /* Allocate space for dataless TX buffer */
630 iobuf = alloc_iob ( MAX_HDR_LEN );
631 if ( ! iobuf ) {
632 DBGC ( tcp, "TCP %p could not allocate iobuf for RST "
633 "%08x..%08x %08x\n", tcp, ntohl ( in_tcphdr->ack ),
634 ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ) );
635 return -ENOMEM;
637 iob_reserve ( iobuf, MAX_HDR_LEN );
639 /* Construct RST response */
640 tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
641 memset ( tcphdr, 0, sizeof ( *tcphdr ) );
642 tcphdr->src = in_tcphdr->dest;
643 tcphdr->dest = in_tcphdr->src;
644 tcphdr->seq = in_tcphdr->ack;
645 tcphdr->ack = in_tcphdr->seq;
646 tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
647 tcphdr->flags = ( TCP_RST | TCP_ACK );
648 tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
649 tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
651 /* Dump header */
652 DBGC2 ( tcp, "TCP %p TX %d->%d %08x..%08x %08x %4d",
653 tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
654 ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
655 ntohl ( tcphdr->ack ), 0 );
656 tcp_dump_flags ( tcp, tcphdr->flags );
657 DBGC2 ( tcp, "\n" );
659 /* Transmit packet */
660 if ( ( rc = tcpip_tx ( iobuf, &tcp_protocol, NULL, st_dest,
661 NULL, &tcphdr->csum ) ) != 0 ) {
662 DBGC ( tcp, "TCP %p could not transmit RST %08x..%08x %08x: "
663 "%s\n", tcp, ntohl ( in_tcphdr->ack ),
664 ntohl ( in_tcphdr->ack ), ntohl ( in_tcphdr->seq ),
665 strerror ( rc ) );
666 return rc;
669 return 0;
672 /***************************************************************************
674 * Receive data path
676 ***************************************************************************
680 * Identify TCP connection by local port number
682 * @v local_port Local port
683 * @ret tcp TCP connection, or NULL
685 static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
686 struct tcp_connection *tcp;
688 list_for_each_entry ( tcp, &tcp_conns, list ) {
689 if ( tcp->local_port == local_port )
690 return tcp;
692 return NULL;
696 * Parse TCP received options
698 * @v tcp TCP connection
699 * @v data Raw options data
700 * @v len Raw options length
701 * @v options Options structure to fill in
703 static void tcp_rx_opts ( struct tcp_connection *tcp, const void *data,
704 size_t len, struct tcp_options *options ) {
705 const void *end = ( data + len );
706 const struct tcp_option *option;
707 unsigned int kind;
709 memset ( options, 0, sizeof ( *options ) );
710 while ( data < end ) {
711 option = data;
712 kind = option->kind;
713 if ( kind == TCP_OPTION_END )
714 return;
715 if ( kind == TCP_OPTION_NOP ) {
716 data++;
717 continue;
719 switch ( kind ) {
720 case TCP_OPTION_MSS:
721 options->mssopt = data;
722 break;
723 case TCP_OPTION_TS:
724 options->tsopt = data;
725 break;
726 default:
727 DBGC ( tcp, "TCP %p received unknown option %d\n",
728 tcp, kind );
729 break;
731 data += option->length;
736 * Consume received sequence space
738 * @v tcp TCP connection
739 * @v seq_len Sequence space length to consume
741 static void tcp_rx_seq ( struct tcp_connection *tcp, uint32_t seq_len ) {
742 tcp->rcv_ack += seq_len;
743 if ( tcp->rcv_win > seq_len ) {
744 tcp->rcv_win -= seq_len;
745 } else {
746 tcp->rcv_win = 0;
748 tcp->flags |= TCP_ACK_PENDING;
752 * Handle TCP received SYN
754 * @v tcp TCP connection
755 * @v seq SEQ value (in host-endian order)
756 * @v options TCP options
757 * @ret rc Return status code
759 static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq,
760 struct tcp_options *options ) {
762 /* Synchronise sequence numbers on first SYN */
763 if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
764 tcp->rcv_ack = seq;
765 if ( options->tsopt )
766 tcp->flags |= TCP_TS_ENABLED;
769 /* Ignore duplicate SYN */
770 if ( seq != tcp->rcv_ack )
771 return 0;
773 /* Acknowledge SYN */
774 tcp_rx_seq ( tcp, 1 );
776 /* Mark SYN as received and start sending ACKs with each packet */
777 tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
778 TCP_STATE_RCVD ( TCP_SYN ) );
780 return 0;
784 * Handle TCP received ACK
786 * @v tcp TCP connection
787 * @v ack ACK value (in host-endian order)
788 * @v win WIN value (in host-endian order)
789 * @ret rc Return status code
791 static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
792 uint32_t win ) {
793 uint32_t ack_len = ( ack - tcp->snd_seq );
794 size_t len;
795 unsigned int acked_flags;
797 /* Check for out-of-range or old duplicate ACKs */
798 if ( ack_len > tcp->snd_sent ) {
799 DBGC ( tcp, "TCP %p received ACK for %08x..%08x, "
800 "sent only %08x..%08x\n", tcp, tcp->snd_seq,
801 ( tcp->snd_seq + ack_len ), tcp->snd_seq,
802 ( tcp->snd_seq + tcp->snd_sent ) );
804 if ( TCP_HAS_BEEN_ESTABLISHED ( tcp->tcp_state ) ) {
805 /* Just ignore what might be old duplicate ACKs */
806 return 0;
807 } else {
808 /* Send RST if an out-of-range ACK is received
809 * on a not-yet-established connection, as per
810 * RFC 793.
812 return -EINVAL;
816 /* Ignore ACKs that don't actually acknowledge any new data.
817 * (In particular, do not stop the retransmission timer; this
818 * avoids creating a sorceror's apprentice syndrome when a
819 * duplicate ACK is received and we still have data in our
820 * transmit queue.)
822 if ( ack_len == 0 )
823 return 0;
825 /* Stop the retransmission timer */
826 stop_timer ( &tcp->timer );
828 /* Determine acknowledged flags and data length */
829 len = ack_len;
830 acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
831 ( TCP_SYN | TCP_FIN ) );
832 if ( acked_flags )
833 len--;
835 /* Update SEQ and sent counters, and window size */
836 tcp->snd_seq = ack;
837 tcp->snd_sent = 0;
838 tcp->snd_win = win;
840 /* Remove any acknowledged data from transmit queue */
841 tcp_process_tx_queue ( tcp, len, NULL, 1 );
843 /* Mark SYN/FIN as acknowledged if applicable. */
844 if ( acked_flags )
845 tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
847 /* Start sending FIN if we've had all possible data ACKed */
848 if ( list_empty ( &tcp->tx_queue ) && ( tcp->flags & TCP_XFER_CLOSED ) )
849 tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
851 return 0;
855 * Handle TCP received data
857 * @v tcp TCP connection
858 * @v seq SEQ value (in host-endian order)
859 * @v iobuf I/O buffer
860 * @ret rc Return status code
862 * This function takes ownership of the I/O buffer.
864 static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
865 struct io_buffer *iobuf ) {
866 uint32_t already_rcvd;
867 uint32_t len;
868 int rc;
870 /* Ignore duplicate or out-of-order data */
871 already_rcvd = ( tcp->rcv_ack - seq );
872 len = iob_len ( iobuf );
873 if ( already_rcvd >= len ) {
874 free_iob ( iobuf );
875 return 0;
877 iob_pull ( iobuf, already_rcvd );
878 len -= already_rcvd;
880 /* Acknowledge new data */
881 tcp_rx_seq ( tcp, len );
883 /* Deliver data to application */
884 if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 ) {
885 DBGC ( tcp, "TCP %p could not deliver %08x..%08x: %s\n",
886 tcp, seq, ( seq + len ), strerror ( rc ) );
887 return rc;
890 return 0;
894 * Handle TCP received FIN
896 * @v tcp TCP connection
897 * @v seq SEQ value (in host-endian order)
898 * @ret rc Return status code
900 static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
902 /* Ignore duplicate or out-of-order FIN */
903 if ( seq != tcp->rcv_ack )
904 return 0;
906 /* Acknowledge FIN */
907 tcp_rx_seq ( tcp, 1 );
909 /* Mark FIN as received */
910 tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
912 /* Close connection */
913 tcp_close ( tcp, 0 );
915 return 0;
919 * Handle TCP received RST
921 * @v tcp TCP connection
922 * @v seq SEQ value (in host-endian order)
923 * @ret rc Return status code
925 static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
927 /* Accept RST only if it falls within the window. If we have
928 * not yet received a SYN, then we have no window to test
929 * against, so fall back to checking that our SYN has been
930 * ACKed.
932 if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
933 if ( ! tcp_in_window ( seq, tcp->rcv_ack, tcp->rcv_win ) )
934 return 0;
935 } else {
936 if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
937 return 0;
940 /* Abort connection */
941 tcp->tcp_state = TCP_CLOSED;
942 tcp_dump_state ( tcp );
943 tcp_close ( tcp, -ECONNRESET );
945 DBGC ( tcp, "TCP %p connection reset by peer\n", tcp );
946 return -ECONNRESET;
950 * Enqueue received TCP packet
952 * @v tcp TCP connection
953 * @v seq SEQ value (in host-endian order)
954 * @v flags TCP flags
955 * @v iobuf I/O buffer
957 static void tcp_rx_enqueue ( struct tcp_connection *tcp, uint32_t seq,
958 uint8_t flags, struct io_buffer *iobuf ) {
959 struct tcp_rx_queued_header *tcpqhdr;
960 struct io_buffer *queued;
961 size_t len;
962 uint32_t seq_len;
964 /* Calculate remaining flags and sequence length. Note that
965 * SYN, if present, has already been processed by this point.
967 flags &= TCP_FIN;
968 len = iob_len ( iobuf );
969 seq_len = ( len + ( flags ? 1 : 0 ) );
971 /* Discard immediately (to save memory) if:
973 * a) we have not yet received a SYN (and so have no defined
974 * receive window), or
975 * b) the packet lies entirely outside the receive window, or
976 * c) there is no further content to process.
978 if ( ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) ||
979 ( tcp_cmp ( seq, tcp->rcv_ack + tcp->rcv_win ) >= 0 ) ||
980 ( tcp_cmp ( seq + seq_len, tcp->rcv_ack ) < 0 ) ||
981 ( seq_len == 0 ) ) {
982 free_iob ( iobuf );
983 return;
986 /* Add internal header */
987 tcpqhdr = iob_push ( iobuf, sizeof ( *tcpqhdr ) );
988 tcpqhdr->seq = seq;
989 tcpqhdr->flags = flags;
991 /* Add to RX queue */
992 list_for_each_entry ( queued, &tcp->rx_queue, list ) {
993 tcpqhdr = queued->data;
994 if ( tcp_cmp ( seq, tcpqhdr->seq ) < 0 )
995 break;
997 list_add_tail ( &iobuf->list, &queued->list );
1001 * Process receive queue
1003 * @v tcp TCP connection
1005 static void tcp_process_rx_queue ( struct tcp_connection *tcp ) {
1006 struct io_buffer *iobuf;
1007 struct io_buffer *tmp;
1008 struct tcp_rx_queued_header *tcpqhdr;
1009 uint32_t seq;
1010 unsigned int flags;
1011 size_t len;
1013 /* Process all applicable received buffers */
1014 list_for_each_entry_safe ( iobuf, tmp, &tcp->rx_queue, list ) {
1015 tcpqhdr = iobuf->data;
1016 if ( tcp_cmp ( tcpqhdr->seq, tcp->rcv_ack ) > 0 )
1017 break;
1019 /* Strip internal header and remove from RX queue */
1020 list_del ( &iobuf->list );
1021 seq = tcpqhdr->seq;
1022 flags = tcpqhdr->flags;
1023 iob_pull ( iobuf, sizeof ( *tcpqhdr ) );
1024 len = iob_len ( iobuf );
1026 /* Handle new data, if any */
1027 tcp_rx_data ( tcp, seq, iob_disown ( iobuf ) );
1028 seq += len;
1030 /* Handle FIN, if present */
1031 if ( flags & TCP_FIN ) {
1032 tcp_rx_fin ( tcp, seq );
1033 seq++;
1039 * Process received packet
1041 * @v iobuf I/O buffer
1042 * @v st_src Partially-filled source address
1043 * @v st_dest Partially-filled destination address
1044 * @v pshdr_csum Pseudo-header checksum
1045 * @ret rc Return status code
1047 static int tcp_rx ( struct io_buffer *iobuf,
1048 struct sockaddr_tcpip *st_src,
1049 struct sockaddr_tcpip *st_dest __unused,
1050 uint16_t pshdr_csum ) {
1051 struct tcp_header *tcphdr = iobuf->data;
1052 struct tcp_connection *tcp;
1053 struct tcp_options options;
1054 size_t hlen;
1055 uint16_t csum;
1056 uint32_t seq;
1057 uint32_t ack;
1058 uint32_t win;
1059 unsigned int flags;
1060 size_t len;
1061 uint32_t seq_len;
1062 int rc;
1064 /* Sanity check packet */
1065 if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
1066 DBG ( "TCP packet too short at %zd bytes (min %zd bytes)\n",
1067 iob_len ( iobuf ), sizeof ( *tcphdr ) );
1068 rc = -EINVAL;
1069 goto discard;
1071 hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
1072 if ( hlen < sizeof ( *tcphdr ) ) {
1073 DBG ( "TCP header too short at %zd bytes (min %zd bytes)\n",
1074 hlen, sizeof ( *tcphdr ) );
1075 rc = -EINVAL;
1076 goto discard;
1078 if ( hlen > iob_len ( iobuf ) ) {
1079 DBG ( "TCP header too long at %zd bytes (max %zd bytes)\n",
1080 hlen, iob_len ( iobuf ) );
1081 rc = -EINVAL;
1082 goto discard;
1084 csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data,
1085 iob_len ( iobuf ) );
1086 if ( csum != 0 ) {
1087 DBG ( "TCP checksum incorrect (is %04x including checksum "
1088 "field, should be 0000)\n", csum );
1089 rc = -EINVAL;
1090 goto discard;
1093 /* Parse parameters from header and strip header */
1094 tcp = tcp_demux ( ntohs ( tcphdr->dest ) );
1095 seq = ntohl ( tcphdr->seq );
1096 ack = ntohl ( tcphdr->ack );
1097 win = ntohs ( tcphdr->win );
1098 flags = tcphdr->flags;
1099 tcp_rx_opts ( tcp, ( ( ( void * ) tcphdr ) + sizeof ( *tcphdr ) ),
1100 ( hlen - sizeof ( *tcphdr ) ), &options );
1101 iob_pull ( iobuf, hlen );
1102 len = iob_len ( iobuf );
1103 seq_len = ( len + ( ( flags & TCP_SYN ) ? 1 : 0 ) +
1104 ( ( flags & TCP_FIN ) ? 1 : 0 ) );
1106 /* Dump header */
1107 DBGC2 ( tcp, "TCP %p RX %d<-%d %08x %08x..%08x %4zd",
1108 tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
1109 ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
1110 ( ntohl ( tcphdr->seq ) + seq_len ), len );
1111 tcp_dump_flags ( tcp, tcphdr->flags );
1112 DBGC2 ( tcp, "\n" );
1114 /* If no connection was found, send RST */
1115 if ( ! tcp ) {
1116 tcp_xmit_reset ( tcp, st_src, tcphdr );
1117 rc = -ENOTCONN;
1118 goto discard;
1121 /* Update timestamp, if applicable */
1122 if ( options.tsopt && tcp_in_window ( tcp->rcv_ack, seq, seq_len ) )
1123 tcp->ts_recent = ntohl ( options.tsopt->tsval );
1125 /* Handle ACK, if present */
1126 if ( flags & TCP_ACK ) {
1127 if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
1128 tcp_xmit_reset ( tcp, st_src, tcphdr );
1129 goto discard;
1133 /* Force an ACK if this packet is out of order */
1134 if ( ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) &&
1135 ( seq != tcp->rcv_ack ) ) {
1136 tcp->flags |= TCP_ACK_PENDING;
1139 /* Handle SYN, if present */
1140 if ( flags & TCP_SYN ) {
1141 tcp_rx_syn ( tcp, seq, &options );
1142 seq++;
1145 /* Handle RST, if present */
1146 if ( flags & TCP_RST ) {
1147 if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
1148 goto discard;
1151 /* Enqueue received data */
1152 tcp_rx_enqueue ( tcp, seq, flags, iob_disown ( iobuf ) );
1154 /* Process receive queue */
1155 tcp_process_rx_queue ( tcp );
1157 /* Dump out any state change as a result of the received packet */
1158 tcp_dump_state ( tcp );
1160 /* Send out any pending data */
1161 tcp_xmit ( tcp );
1163 /* If this packet was the last we expect to receive, set up
1164 * timer to expire and cause the connection to be freed.
1166 if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
1167 stop_timer ( &tcp->wait );
1168 start_timer_fixed ( &tcp->wait, ( 2 * TCP_MSL ) );
1171 return 0;
1173 discard:
1174 /* Free received packet */
1175 free_iob ( iobuf );
1176 return rc;
1179 /** TCP protocol */
1180 struct tcpip_protocol tcp_protocol __tcpip_protocol = {
1181 .name = "TCP",
1182 .rx = tcp_rx,
1183 .tcpip_proto = IP_TCP,
1186 /***************************************************************************
1188 * Data transfer interface
1190 ***************************************************************************
1194 * Close interface
1196 * @v xfer Data transfer interface
1197 * @v rc Reason for close
1199 static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
1200 struct tcp_connection *tcp =
1201 container_of ( xfer, struct tcp_connection, xfer );
1203 /* Close data transfer interface */
1204 tcp_close ( tcp, rc );
1206 /* Transmit FIN, if possible */
1207 tcp_xmit ( tcp );
1211 * Check flow control window
1213 * @v xfer Data transfer interface
1214 * @ret len Length of window
1216 static size_t tcp_xfer_window ( struct xfer_interface *xfer ) {
1217 struct tcp_connection *tcp =
1218 container_of ( xfer, struct tcp_connection, xfer );
1220 /* Not ready if data queue is non-empty. This imposes a limit
1221 * of only one unACKed packet in the TX queue at any time; we
1222 * do this to conserve memory usage.
1224 if ( ! list_empty ( &tcp->tx_queue ) )
1225 return 0;
1227 /* Return TCP window length */
1228 return tcp_xmit_win ( tcp );
1232 * Deliver datagram as I/O buffer
1234 * @v xfer Data transfer interface
1235 * @v iobuf Datagram I/O buffer
1236 * @v meta Data transfer metadata
1237 * @ret rc Return status code
1239 static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
1240 struct io_buffer *iobuf,
1241 struct xfer_metadata *meta __unused ) {
1242 struct tcp_connection *tcp =
1243 container_of ( xfer, struct tcp_connection, xfer );
1245 /* Enqueue packet */
1246 list_add_tail ( &iobuf->list, &tcp->tx_queue );
1248 /* Transmit data, if possible */
1249 tcp_xmit ( tcp );
1251 return 0;
1254 /** TCP data transfer interface operations */
1255 static struct xfer_interface_operations tcp_xfer_operations = {
1256 .close = tcp_xfer_close,
1257 .vredirect = ignore_xfer_vredirect,
1258 .window = tcp_xfer_window,
1259 .alloc_iob = default_xfer_alloc_iob,
1260 .deliver_iob = tcp_xfer_deliver_iob,
1261 .deliver_raw = xfer_deliver_as_iob,
1264 /***************************************************************************
1266 * Openers
1268 ***************************************************************************
1271 /** TCP socket opener */
1272 struct socket_opener tcp_socket_opener __socket_opener = {
1273 .semantics = TCP_SOCK_STREAM,
1274 .family = AF_INET,
1275 .open = tcp_open,
1278 /** Linkage hack */
1279 int tcp_sock_stream = TCP_SOCK_STREAM;
1282 * Open TCP URI
1284 * @v xfer Data transfer interface
1285 * @v uri URI
1286 * @ret rc Return status code
1288 static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
1289 struct sockaddr_tcpip peer;
1291 /* Sanity check */
1292 if ( ! uri->host )
1293 return -EINVAL;
1295 memset ( &peer, 0, sizeof ( peer ) );
1296 peer.st_port = htons ( uri_port ( uri, 0 ) );
1297 return xfer_open_named_socket ( xfer, SOCK_STREAM,
1298 ( struct sockaddr * ) &peer,
1299 uri->host, NULL );
1302 /** TCP URI opener */
1303 struct uri_opener tcp_uri_opener __uri_opener = {
1304 .scheme = "tcp",
1305 .open = tcp_open_uri,