source: mainline/uspace/srv/net/tcp/conn.c@ fab2746

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fab2746 was 78192cc7, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Fibril timer locking improvements.

  • Property mode set to 100644
File size: 33.0 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup tcp
30 * @{
31 */
32
33/**
34 * @file TCP connection processing and state machine
35 */
36
37#include <adt/list.h>
38#include <stdbool.h>
39#include <errno.h>
40#include <io/log.h>
41#include <macros.h>
42#include <stdlib.h>
43#include "conn.h"
44#include "iqueue.h"
45#include "segment.h"
46#include "seq_no.h"
47#include "tcp_type.h"
48#include "tqueue.h"
49#include "ucall.h"
50
51#define RCV_BUF_SIZE 4096/*2*/
52#define SND_BUF_SIZE 4096
53
54#define MAX_SEGMENT_LIFETIME (15*1000*1000) //(2*60*1000*1000)
55#define TIME_WAIT_TIMEOUT (2*MAX_SEGMENT_LIFETIME)
56
57LIST_INITIALIZE(conn_list);
58FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
59
60static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg);
61static void tcp_conn_tw_timer_set(tcp_conn_t *conn);
62static void tcp_conn_tw_timer_clear(tcp_conn_t *conn);
63
64/** Create new connection structure.
65 *
66 * @param lsock Local socket (will be deeply copied)
67 * @param fsock Foreign socket (will be deeply copied)
68 * @return New connection or NULL
69 */
70tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
71{
72 tcp_conn_t *conn = NULL;
73 bool tqueue_inited = false;
74
75 /* Allocate connection structure */
76 conn = calloc(1, sizeof(tcp_conn_t));
77 if (conn == NULL)
78 goto error;
79
80 fibril_mutex_initialize(&conn->lock);
81
82 conn->tw_timer = fibril_timer_create(&conn->lock);
83 if (conn->tw_timer == NULL)
84 goto error;
85
86 /* One for the user, one for not being in closed state */
87 atomic_set(&conn->refcnt, 2);
88
89 /* Allocate receive buffer */
90 fibril_condvar_initialize(&conn->rcv_buf_cv);
91 conn->rcv_buf_size = RCV_BUF_SIZE;
92 conn->rcv_buf_used = 0;
93 conn->rcv_buf_fin = false;
94
95 conn->rcv_buf = calloc(1, conn->rcv_buf_size);
96 if (conn->rcv_buf == NULL)
97 goto error;
98
99 /** Allocate send buffer */
100 fibril_condvar_initialize(&conn->snd_buf_cv);
101 conn->snd_buf_size = SND_BUF_SIZE;
102 conn->snd_buf_used = 0;
103 conn->snd_buf_fin = false;
104 conn->snd_buf = calloc(1, conn->snd_buf_size);
105 if (conn->snd_buf == NULL)
106 goto error;
107
108 /* Set up receive window. */
109 conn->rcv_wnd = conn->rcv_buf_size;
110
111 /* Initialize incoming segment queue */
112 tcp_iqueue_init(&conn->incoming, conn);
113
114 /* Initialize retransmission queue */
115 if (tcp_tqueue_init(&conn->retransmit, conn) != EOK)
116 goto error;
117
118 tqueue_inited = true;
119
120 /* Connection state change signalling */
121 fibril_condvar_initialize(&conn->cstate_cv);
122
123 conn->cstate_cb = NULL;
124
125 conn->cstate = st_listen;
126 conn->reset = false;
127 conn->deleted = false;
128 conn->ap = ap_passive;
129 conn->fin_is_acked = false;
130 conn->ident.local = *lsock;
131 if (fsock != NULL)
132 conn->ident.foreign = *fsock;
133
134 return conn;
135
136error:
137 if (tqueue_inited)
138 tcp_tqueue_fini(&conn->retransmit);
139 if (conn != NULL && conn->rcv_buf != NULL)
140 free(conn->rcv_buf);
141 if (conn != NULL && conn->snd_buf != NULL)
142 free(conn->snd_buf);
143 if (conn != NULL && conn->tw_timer != NULL)
144 fibril_timer_destroy(conn->tw_timer);
145 if (conn != NULL)
146 free(conn);
147
148 return NULL;
149}
150
151/** Destroy connection structure.
152 *
153 * Connection structure should be destroyed when the folowing condtitions
154 * are met:
155 * (1) user has deleted the connection
156 * (2) the connection has entered closed state
157 * (3) nobody is holding references to the connection
158 *
159 * This happens when @a conn->refcnt is zero as we count (1) and (2)
160 * as special references.
161 *
162 * @param conn Connection
163 */
164static void tcp_conn_free(tcp_conn_t *conn)
165{
166 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_free(%p)", conn->name, conn);
167 tcp_tqueue_fini(&conn->retransmit);
168
169 if (conn->rcv_buf != NULL)
170 free(conn->rcv_buf);
171 if (conn->snd_buf != NULL)
172 free(conn->snd_buf);
173 if (conn->tw_timer != NULL)
174 fibril_timer_destroy(conn->tw_timer);
175 free(conn);
176}
177
178/** Add reference to connection.
179 *
180 * Increase connection reference count by one.
181 *
182 * @param conn Connection
183 */
184void tcp_conn_addref(tcp_conn_t *conn)
185{
186 log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p)", conn->name, conn);
187 atomic_inc(&conn->refcnt);
188}
189
190/** Remove reference from connection.
191 *
192 * Decrease connection reference count by one.
193 *
194 * @param conn Connection
195 */
196void tcp_conn_delref(tcp_conn_t *conn)
197{
198 log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p)", conn->name, conn);
199
200 if (atomic_predec(&conn->refcnt) == 0)
201 tcp_conn_free(conn);
202}
203
204/** Lock connection.
205 *
206 * Must be called before any other connection-manipulating function,
207 * except tcp_conn_{add|del}ref(). Locks the connection including
208 * its timers. Must not be called inside any of the connection
209 * timer handlers.
210 *
211 * @param conn Connection
212 */
213void tcp_conn_lock(tcp_conn_t *conn)
214{
215 fibril_mutex_lock(&conn->lock);
216}
217
218/** Unlock connection.
219 *
220 * @param conn Connection
221 */
222void tcp_conn_unlock(tcp_conn_t *conn)
223{
224 fibril_mutex_unlock(&conn->lock);
225}
226
227/** Delete connection.
228 *
229 * The caller promises not make no further references to @a conn.
230 * TCP will free @a conn eventually.
231 *
232 * @param conn Connection
233 */
234void tcp_conn_delete(tcp_conn_t *conn)
235{
236 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_delete(%p)", conn->name, conn);
237
238 assert(conn->deleted == false);
239 tcp_conn_delref(conn);
240}
241
242/** Enlist connection.
243 *
244 * Add connection to the connection map.
245 */
246void tcp_conn_add(tcp_conn_t *conn)
247{
248 tcp_conn_addref(conn);
249 fibril_mutex_lock(&conn_list_lock);
250 list_append(&conn->link, &conn_list);
251 fibril_mutex_unlock(&conn_list_lock);
252}
253
254/** Delist connection.
255 *
256 * Remove connection from the connection map.
257 */
258void tcp_conn_remove(tcp_conn_t *conn)
259{
260 fibril_mutex_lock(&conn_list_lock);
261 list_remove(&conn->link);
262 fibril_mutex_unlock(&conn_list_lock);
263 tcp_conn_delref(conn);
264}
265
266static void tcp_conn_state_set(tcp_conn_t *conn, tcp_cstate_t nstate)
267{
268 tcp_cstate_t old_state;
269
270 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set(%p)", conn);
271
272 old_state = conn->cstate;
273 conn->cstate = nstate;
274 fibril_condvar_broadcast(&conn->cstate_cv);
275
276 /* Run user callback function */
277 if (conn->cstate_cb != NULL) {
278 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - run user CB");
279 conn->cstate_cb(conn, conn->cstate_cb_arg);
280 } else {
281 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - no user CB");
282 }
283
284 assert(old_state != st_closed);
285 if (nstate == st_closed) {
286 /* Drop one reference for now being in closed state */
287 tcp_conn_delref(conn);
288 }
289}
290
291/** Synchronize connection.
292 *
293 * This is the first step of an active connection attempt,
294 * sends out SYN and sets up ISS and SND.xxx.
295 */
296void tcp_conn_sync(tcp_conn_t *conn)
297{
298 /* XXX select ISS */
299 conn->iss = 1;
300 conn->snd_nxt = conn->iss;
301 conn->snd_una = conn->iss;
302 conn->ap = ap_active;
303
304 tcp_tqueue_ctrl_seg(conn, CTL_SYN);
305 tcp_conn_state_set(conn, st_syn_sent);
306}
307
308/** FIN has been sent.
309 *
310 * This function should be called when FIN is sent over the connection,
311 * as a result the connection state is changed appropriately.
312 */
313void tcp_conn_fin_sent(tcp_conn_t *conn)
314{
315 switch (conn->cstate) {
316 case st_syn_received:
317 case st_established:
318 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Fin-Wait-1", conn->name);
319 tcp_conn_state_set(conn, st_fin_wait_1);
320 break;
321 case st_close_wait:
322 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Last-Ack", conn->name);
323 tcp_conn_state_set(conn, st_last_ack);
324 break;
325 default:
326 log_msg(LOG_DEFAULT, LVL_ERROR, "%s: Connection state %d", conn->name,
327 conn->cstate);
328 assert(false);
329 }
330
331 conn->fin_is_acked = false;
332}
333
334/** Match socket with pattern. */
335static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt)
336{
337 log_msg(LOG_DEFAULT, LVL_DEBUG2,
338 "tcp_socket_match(sock=(%u), pat=(%u))", sock->port, patt->port);
339
340 if ((!inet_addr_is_any(&patt->addr)) &&
341 (!inet_addr_compare(&patt->addr, &sock->addr)))
342 return false;
343
344 if ((patt->port != TCP_PORT_ANY) &&
345 (patt->port != sock->port))
346 return false;
347
348 log_msg(LOG_DEFAULT, LVL_DEBUG2, " -> match");
349
350 return true;
351}
352
353/** Match socket pair with pattern. */
354static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern)
355{
356 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_sockpair_match(%p, %p)", sp, pattern);
357
358 if (!tcp_socket_match(&sp->local, &pattern->local))
359 return false;
360
361 if (!tcp_socket_match(&sp->foreign, &pattern->foreign))
362 return false;
363
364 return true;
365}
366
367/** Find connection structure for specified socket pair.
368 *
369 * A connection is uniquely identified by a socket pair. Look up our
370 * connection map and return connection structure based on socket pair.
371 * The connection reference count is bumped by one.
372 *
373 * @param sp Socket pair
374 * @return Connection structure or NULL if not found.
375 */
376tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp)
377{
378 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
379
380 log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%u), l:(%u))",
381 sp->foreign.port, sp->local.port);
382
383 fibril_mutex_lock(&conn_list_lock);
384
385 list_foreach(conn_list, link, tcp_conn_t, conn) {
386 tcp_sockpair_t *csp = &conn->ident;
387
388 log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%u), l:(%u))",
389 csp->foreign.port, csp->local.port);
390
391 if (tcp_sockpair_match(sp, csp)) {
392 tcp_conn_addref(conn);
393 fibril_mutex_unlock(&conn_list_lock);
394 return conn;
395 }
396 }
397
398 fibril_mutex_unlock(&conn_list_lock);
399 return NULL;
400}
401
402/** Reset connection.
403 *
404 * @param conn Connection
405 */
406void tcp_conn_reset(tcp_conn_t *conn)
407{
408 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
409 tcp_conn_state_set(conn, st_closed);
410 conn->reset = true;
411
412 tcp_conn_tw_timer_clear(conn);
413 tcp_tqueue_clear(&conn->retransmit);
414
415 fibril_condvar_broadcast(&conn->rcv_buf_cv);
416 fibril_condvar_broadcast(&conn->snd_buf_cv);
417}
418
419/** Signal to the user that connection has been reset.
420 *
421 * Send an out-of-band signal to the user.
422 */
423static void tcp_reset_signal(tcp_conn_t *conn)
424{
425 /* TODO */
426 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_reset_signal()", conn->name);
427}
428
429/** Determine if SYN has been received.
430 *
431 * @param conn Connection
432 * @return @c true if SYN has been received, @c false otherwise.
433 */
434bool tcp_conn_got_syn(tcp_conn_t *conn)
435{
436 switch (conn->cstate) {
437 case st_listen:
438 case st_syn_sent:
439 return false;
440 case st_syn_received:
441 case st_established:
442 case st_fin_wait_1:
443 case st_fin_wait_2:
444 case st_close_wait:
445 case st_closing:
446 case st_last_ack:
447 case st_time_wait:
448 return true;
449 case st_closed:
450 log_msg(LOG_DEFAULT, LVL_WARN, "state=%d", (int) conn->cstate);
451 assert(false);
452 }
453
454 assert(false);
455}
456
457/** Segment arrived in Listen state.
458 *
459 * @param conn Connection
460 * @param seg Segment
461 */
462static void tcp_conn_sa_listen(tcp_conn_t *conn, tcp_segment_t *seg)
463{
464 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_listen(%p, %p)", conn, seg);
465
466 if ((seg->ctrl & CTL_RST) != 0) {
467 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring incoming RST.");
468 return;
469 }
470
471 if ((seg->ctrl & CTL_ACK) != 0) {
472 log_msg(LOG_DEFAULT, LVL_DEBUG, "Incoming ACK, send acceptable RST.");
473 tcp_reply_rst(&conn->ident, seg);
474 return;
475 }
476
477 if ((seg->ctrl & CTL_SYN) == 0) {
478 log_msg(LOG_DEFAULT, LVL_DEBUG, "SYN not present. Ignoring segment.");
479 return;
480 }
481
482 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got SYN, sending SYN, ACK.");
483
484 conn->rcv_nxt = seg->seq + 1;
485 conn->irs = seg->seq;
486
487
488 log_msg(LOG_DEFAULT, LVL_DEBUG, "rcv_nxt=%u", conn->rcv_nxt);
489
490 if (seg->len > 1)
491 log_msg(LOG_DEFAULT, LVL_WARN, "SYN combined with data, ignoring data.");
492
493 /* XXX select ISS */
494 conn->iss = 1;
495 conn->snd_nxt = conn->iss;
496 conn->snd_una = conn->iss;
497
498 /*
499 * Surprisingly the spec does not deal with initial window setting.
500 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
501 * will always be accepted as new window setting.
502 */
503 conn->snd_wnd = seg->wnd;
504 conn->snd_wl1 = seg->seq;
505 conn->snd_wl2 = seg->seq;
506
507 tcp_conn_state_set(conn, st_syn_received);
508
509 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
510
511 tcp_segment_delete(seg);
512}
513
514/** Segment arrived in Syn-Sent state.
515 *
516 * @param conn Connection
517 * @param seg Segment
518 */
519static void tcp_conn_sa_syn_sent(tcp_conn_t *conn, tcp_segment_t *seg)
520{
521 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_syn_sent(%p, %p)", conn, seg);
522
523 if ((seg->ctrl & CTL_ACK) != 0) {
524 log_msg(LOG_DEFAULT, LVL_DEBUG, "snd_una=%u, seg.ack=%u, snd_nxt=%u",
525 conn->snd_una, seg->ack, conn->snd_nxt);
526 if (!seq_no_ack_acceptable(conn, seg->ack)) {
527 if ((seg->ctrl & CTL_RST) == 0) {
528 log_msg(LOG_DEFAULT, LVL_WARN, "ACK not acceptable, send RST");
529 tcp_reply_rst(&conn->ident, seg);
530 } else {
531 log_msg(LOG_DEFAULT, LVL_WARN, "RST,ACK not acceptable, drop");
532 }
533 return;
534 }
535 }
536
537 if ((seg->ctrl & CTL_RST) != 0) {
538 /* If we get here, we have either an acceptable ACK or no ACK */
539 if ((seg->ctrl & CTL_ACK) != 0) {
540 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Connection reset. -> Closed",
541 conn->name);
542 /* Reset connection */
543 tcp_conn_reset(conn);
544 return;
545 } else {
546 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: RST without ACK, drop",
547 conn->name);
548 return;
549 }
550 }
551
552 /* XXX precedence */
553
554 if ((seg->ctrl & CTL_SYN) == 0) {
555 log_msg(LOG_DEFAULT, LVL_DEBUG, "No SYN bit, ignoring segment.");
556 return;
557 }
558
559 conn->rcv_nxt = seg->seq + 1;
560 conn->irs = seg->seq;
561
562 if ((seg->ctrl & CTL_ACK) != 0) {
563 conn->snd_una = seg->ack;
564
565 /*
566 * Prune acked segments from retransmission queue and
567 * possibly transmit more data.
568 */
569 tcp_tqueue_ack_received(conn);
570 }
571
572 log_msg(LOG_DEFAULT, LVL_DEBUG, "Sent SYN, got SYN.");
573
574 /*
575 * Surprisingly the spec does not deal with initial window setting.
576 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
577 * will always be accepted as new window setting.
578 */
579 log_msg(LOG_DEFAULT, LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", "
580 "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq);
581 conn->snd_wnd = seg->wnd;
582 conn->snd_wl1 = seg->seq;
583 conn->snd_wl2 = seg->seq;
584
585 if (seq_no_syn_acked(conn)) {
586 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn acked -> Established", conn->name);
587 tcp_conn_state_set(conn, st_established);
588 tcp_tqueue_ctrl_seg(conn, CTL_ACK /* XXX */);
589 } else {
590 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn not acked -> Syn-Received",
591 conn->name);
592 tcp_conn_state_set(conn, st_syn_received);
593 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
594 }
595
596 tcp_segment_delete(seg);
597}
598
599/** Segment arrived in state where segments are processed in sequence order.
600 *
601 * Queue segment in incoming segments queue for processing.
602 *
603 * @param conn Connection
604 * @param seg Segment
605 */
606static void tcp_conn_sa_queue(tcp_conn_t *conn, tcp_segment_t *seg)
607{
608 tcp_segment_t *pseg;
609
610 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_seq(%p, %p)", conn, seg);
611
612 /* Discard unacceptable segments ("old duplicates") */
613 if (!seq_no_segment_acceptable(conn, seg)) {
614 log_msg(LOG_DEFAULT, LVL_DEBUG, "Replying ACK to unacceptable segment.");
615 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
616 tcp_segment_delete(seg);
617 return;
618 }
619
620 /* Queue for processing */
621 tcp_iqueue_insert_seg(&conn->incoming, seg);
622
623 /*
624 * Process all segments from incoming queue that are ready.
625 * Unacceptable segments are discarded by tcp_iqueue_get_ready_seg().
626 *
627 * XXX Need to return ACK for unacceptable segments
628 */
629 while (tcp_iqueue_get_ready_seg(&conn->incoming, &pseg) == EOK)
630 tcp_conn_seg_process(conn, pseg);
631}
632
633/** Process segment RST field.
634 *
635 * @param conn Connection
636 * @param seg Segment
637 * @return cp_done if we are done with this segment, cp_continue
638 * if not
639 */
640static cproc_t tcp_conn_seg_proc_rst(tcp_conn_t *conn, tcp_segment_t *seg)
641{
642 if ((seg->ctrl & CTL_RST) == 0)
643 return cp_continue;
644
645 switch (conn->cstate) {
646 case st_syn_received:
647 /* XXX In case of passive open, revert to Listen state */
648 if (conn->ap == ap_passive) {
649 tcp_conn_state_set(conn, st_listen);
650 /* XXX Revert conn->ident */
651 tcp_conn_tw_timer_clear(conn);
652 tcp_tqueue_clear(&conn->retransmit);
653 } else {
654 tcp_conn_reset(conn);
655 }
656 break;
657 case st_established:
658 case st_fin_wait_1:
659 case st_fin_wait_2:
660 case st_close_wait:
661 /* General "connection reset" signal */
662 tcp_reset_signal(conn);
663 tcp_conn_reset(conn);
664 break;
665 case st_closing:
666 case st_last_ack:
667 case st_time_wait:
668 tcp_conn_reset(conn);
669 break;
670 case st_listen:
671 case st_syn_sent:
672 case st_closed:
673 assert(false);
674 }
675
676 return cp_done;
677}
678
679/** Process segment security and precedence fields.
680 *
681 * @param conn Connection
682 * @param seg Segment
683 * @return cp_done if we are done with this segment, cp_continue
684 * if not
685 */
686static cproc_t tcp_conn_seg_proc_sp(tcp_conn_t *conn, tcp_segment_t *seg)
687{
688 /* TODO */
689 return cp_continue;
690}
691
692/** Process segment SYN field.
693 *
694 * @param conn Connection
695 * @param seg Segment
696 * @return cp_done if we are done with this segment, cp_continue
697 * if not
698 */
699static cproc_t tcp_conn_seg_proc_syn(tcp_conn_t *conn, tcp_segment_t *seg)
700{
701 if ((seg->ctrl & CTL_SYN) == 0)
702 return cp_continue;
703
704 /*
705 * Assert SYN is in receive window, otherwise this step should not
706 * be reached.
707 */
708 assert(seq_no_in_rcv_wnd(conn, seg->seq));
709
710 log_msg(LOG_DEFAULT, LVL_WARN, "SYN is in receive window, should send reset. XXX");
711
712 /*
713 * TODO
714 *
715 * Send a reset, resond "reset" to all outstanding RECEIVEs and SEND,
716 * flush segment queues. Send unsolicited "connection reset" signal
717 * to user, connection -> closed state, delete TCB, return.
718 */
719 return cp_done;
720}
721
722/** Process segment ACK field in Syn-Received state.
723 *
724 * @param conn Connection
725 * @param seg Segment
726 * @return cp_done if we are done with this segment, cp_continue
727 * if not
728 */
729static cproc_t tcp_conn_seg_proc_ack_sr(tcp_conn_t *conn, tcp_segment_t *seg)
730{
731 if (!seq_no_ack_acceptable(conn, seg->ack)) {
732 /* ACK is not acceptable, send RST. */
733 log_msg(LOG_DEFAULT, LVL_WARN, "Segment ACK not acceptable, sending RST.");
734 tcp_reply_rst(&conn->ident, seg);
735 tcp_segment_delete(seg);
736 return cp_done;
737 }
738
739 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: SYN ACKed -> Established", conn->name);
740
741 tcp_conn_state_set(conn, st_established);
742
743 /* XXX Not mentioned in spec?! */
744 conn->snd_una = seg->ack;
745
746 return cp_continue;
747}
748
749/** Process segment ACK field in Established state.
750 *
751 * @param conn Connection
752 * @param seg Segment
753 * @return cp_done if we are done with this segment, cp_continue
754 * if not
755 */
756static cproc_t tcp_conn_seg_proc_ack_est(tcp_conn_t *conn, tcp_segment_t *seg)
757{
758 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_proc_ack_est(%p, %p)", conn, seg);
759
760 log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.ACK=%u, SND.UNA=%u, SND.NXT=%u",
761 (unsigned)seg->ack, (unsigned)conn->snd_una,
762 (unsigned)conn->snd_nxt);
763
764 if (!seq_no_ack_acceptable(conn, seg->ack)) {
765 log_msg(LOG_DEFAULT, LVL_DEBUG, "ACK not acceptable.");
766 if (!seq_no_ack_duplicate(conn, seg->ack)) {
767 log_msg(LOG_DEFAULT, LVL_WARN, "Not acceptable, not duplicate. "
768 "Send ACK and drop.");
769 /* Not acceptable, not duplicate. Send ACK and drop. */
770 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
771 tcp_segment_delete(seg);
772 return cp_done;
773 } else {
774 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring duplicate ACK.");
775 }
776 } else {
777 /* Update SND.UNA */
778 conn->snd_una = seg->ack;
779 }
780
781 if (seq_no_new_wnd_update(conn, seg)) {
782 conn->snd_wnd = seg->wnd;
783 conn->snd_wl1 = seg->seq;
784 conn->snd_wl2 = seg->ack;
785
786 log_msg(LOG_DEFAULT, LVL_DEBUG, "Updating send window, SND.WND=%" PRIu32
787 ", SND.WL1=%" PRIu32 ", SND.WL2=%" PRIu32,
788 conn->snd_wnd, conn->snd_wl1, conn->snd_wl2);
789 }
790
791 /*
792 * Prune acked segments from retransmission queue and
793 * possibly transmit more data.
794 */
795 tcp_tqueue_ack_received(conn);
796
797 return cp_continue;
798}
799
800/** Process segment ACK field in Fin-Wait-1 state.
801 *
802 * @param conn Connection
803 * @param seg Segment
804 * @return cp_done if we are done with this segment, cp_continue
805 * if not
806 */
807static cproc_t tcp_conn_seg_proc_ack_fw1(tcp_conn_t *conn, tcp_segment_t *seg)
808{
809 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
810 return cp_done;
811
812 if (conn->fin_is_acked) {
813 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Fin-Wait-2", conn->name);
814 tcp_conn_state_set(conn, st_fin_wait_2);
815 }
816
817 return cp_continue;
818}
819
820/** Process segment ACK field in Fin-Wait-2 state.
821 *
822 * @param conn Connection
823 * @param seg Segment
824 * @return cp_done if we are done with this segment, cp_continue
825 * if not
826 */
827static cproc_t tcp_conn_seg_proc_ack_fw2(tcp_conn_t *conn, tcp_segment_t *seg)
828{
829 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
830 return cp_done;
831
832 /* TODO */
833 return cp_continue;
834}
835
836/** Process segment ACK field in Close-Wait state.
837 *
838 * @param conn Connection
839 * @param seg Segment
840 * @return cp_done if we are done with this segment, cp_continue
841 * if not
842 */
843static cproc_t tcp_conn_seg_proc_ack_cw(tcp_conn_t *conn, tcp_segment_t *seg)
844{
845 /* The same processing as in Established state */
846 return tcp_conn_seg_proc_ack_est(conn, seg);
847}
848
849/** Process segment ACK field in Closing state.
850 *
851 * @param conn Connection
852 * @param seg Segment
853 * @return cp_done if we are done with this segment, cp_continue
854 * if not
855 */
856static cproc_t tcp_conn_seg_proc_ack_cls(tcp_conn_t *conn, tcp_segment_t *seg)
857{
858 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
859 return cp_done;
860
861 /* TODO */
862 return cp_continue;
863}
864
865/** Process segment ACK field in Last-Ack state.
866 *
867 * @param conn Connection
868 * @param seg Segment
869 * @return cp_done if we are done with this segment, cp_continue
870 * if not
871 */
872static cproc_t tcp_conn_seg_proc_ack_la(tcp_conn_t *conn, tcp_segment_t *seg)
873{
874 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
875 return cp_done;
876
877 if (conn->fin_is_acked) {
878 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
879 tcp_conn_remove(conn);
880 tcp_conn_state_set(conn, st_closed);
881 return cp_done;
882 }
883
884 return cp_continue;
885}
886
887/** Process segment ACK field in Time-Wait state.
888 *
889 * @param conn Connection
890 * @param seg Segment
891 * @return cp_done if we are done with this segment, cp_continue
892 * if not
893 */
894static cproc_t tcp_conn_seg_proc_ack_tw(tcp_conn_t *conn, tcp_segment_t *seg)
895{
896 /* Nothing to do */
897 return cp_continue;
898}
899
900/** Process segment ACK field.
901 *
902 * @param conn Connection
903 * @param seg Segment
904 * @return cp_done if we are done with this segment, cp_continue
905 * if not
906 */
907static cproc_t tcp_conn_seg_proc_ack(tcp_conn_t *conn, tcp_segment_t *seg)
908{
909 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_ack(%p, %p)",
910 conn->name, conn, seg);
911
912 if ((seg->ctrl & CTL_ACK) == 0) {
913 log_msg(LOG_DEFAULT, LVL_WARN, "Segment has no ACK. Dropping.");
914 tcp_segment_delete(seg);
915 return cp_done;
916 }
917
918 switch (conn->cstate) {
919 case st_syn_received:
920 return tcp_conn_seg_proc_ack_sr(conn, seg);
921 case st_established:
922 return tcp_conn_seg_proc_ack_est(conn, seg);
923 case st_fin_wait_1:
924 return tcp_conn_seg_proc_ack_fw1(conn, seg);
925 case st_fin_wait_2:
926 return tcp_conn_seg_proc_ack_fw2(conn, seg);
927 case st_close_wait:
928 return tcp_conn_seg_proc_ack_cw(conn, seg);
929 case st_closing:
930 return tcp_conn_seg_proc_ack_cls(conn, seg);
931 case st_last_ack:
932 return tcp_conn_seg_proc_ack_la(conn, seg);
933 case st_time_wait:
934 return tcp_conn_seg_proc_ack_tw(conn, seg);
935 case st_listen:
936 case st_syn_sent:
937 case st_closed:
938 assert(false);
939 }
940
941 assert(false);
942}
943
944/** Process segment URG field.
945 *
946 * @param conn Connection
947 * @param seg Segment
948 * @return cp_done if we are done with this segment, cp_continue
949 * if not
950 */
951static cproc_t tcp_conn_seg_proc_urg(tcp_conn_t *conn, tcp_segment_t *seg)
952{
953 return cp_continue;
954}
955
956/** Process segment text.
957 *
958 * @param conn Connection
959 * @param seg Segment
960 * @return cp_done if we are done with this segment, cp_continue
961 * if not
962 */
963static cproc_t tcp_conn_seg_proc_text(tcp_conn_t *conn, tcp_segment_t *seg)
964{
965 size_t text_size;
966 size_t xfer_size;
967
968 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_text(%p, %p)",
969 conn->name, conn, seg);
970
971 switch (conn->cstate) {
972 case st_established:
973 case st_fin_wait_1:
974 case st_fin_wait_2:
975 /* OK */
976 break;
977 case st_close_wait:
978 case st_closing:
979 case st_last_ack:
980 case st_time_wait:
981 /* Invalid since FIN has been received. Ignore text. */
982 return cp_continue;
983 case st_listen:
984 case st_syn_sent:
985 case st_syn_received:
986 case st_closed:
987 assert(false);
988 }
989
990 /*
991 * Process segment text
992 */
993 assert(seq_no_segment_ready(conn, seg));
994
995 /* Trim anything outside our receive window */
996 tcp_conn_trim_seg_to_wnd(conn, seg);
997
998 /* Determine how many bytes to copy */
999 text_size = tcp_segment_text_size(seg);
1000 xfer_size = min(text_size, conn->rcv_buf_size - conn->rcv_buf_used);
1001
1002 /* Copy data to receive buffer */
1003 tcp_segment_text_copy(seg, conn->rcv_buf + conn->rcv_buf_used,
1004 xfer_size);
1005 conn->rcv_buf_used += xfer_size;
1006
1007 /* Signal to the receive function that new data has arrived */
1008 fibril_condvar_broadcast(&conn->rcv_buf_cv);
1009
1010 log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
1011
1012 /* Advance RCV.NXT */
1013 conn->rcv_nxt += xfer_size;
1014
1015 /* Update receive window. XXX Not an efficient strategy. */
1016 conn->rcv_wnd -= xfer_size;
1017
1018 /* Send ACK */
1019 if (xfer_size > 0)
1020 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1021
1022 if (xfer_size < seg->len) {
1023 /* Trim part of segment which we just received */
1024 tcp_conn_trim_seg_to_wnd(conn, seg);
1025 } else {
1026 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Nothing left in segment, dropping "
1027 "(xfer_size=%zu, SEG.LEN=%" PRIu32 ", seg->ctrl=%u)",
1028 conn->name, xfer_size, seg->len, (unsigned int) seg->ctrl);
1029 /* Nothing left in segment */
1030 tcp_segment_delete(seg);
1031 return cp_done;
1032 }
1033
1034 return cp_continue;
1035}
1036
1037/** Process segment FIN field.
1038 *
1039 * @param conn Connection
1040 * @param seg Segment
1041 * @return cp_done if we are done with this segment, cp_continue
1042 * if not
1043 */
1044static cproc_t tcp_conn_seg_proc_fin(tcp_conn_t *conn, tcp_segment_t *seg)
1045{
1046 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_fin(%p, %p)",
1047 conn->name, conn, seg);
1048 log_msg(LOG_DEFAULT, LVL_DEBUG, " seg->len=%zu, seg->ctl=%u", (size_t) seg->len,
1049 (unsigned) seg->ctrl);
1050
1051 /* Only process FIN if no text is left in segment. */
1052 if (tcp_segment_text_size(seg) == 0 && (seg->ctrl & CTL_FIN) != 0) {
1053 log_msg(LOG_DEFAULT, LVL_DEBUG, " - FIN found in segment.");
1054
1055 /* Send ACK */
1056 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1057
1058 conn->rcv_nxt++;
1059 conn->rcv_wnd--;
1060
1061 /* Change connection state */
1062 switch (conn->cstate) {
1063 case st_listen:
1064 case st_syn_sent:
1065 case st_closed:
1066 /* Connection not synchronized */
1067 assert(false);
1068 case st_syn_received:
1069 case st_established:
1070 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Close-Wait",
1071 conn->name);
1072 tcp_conn_state_set(conn, st_close_wait);
1073 break;
1074 case st_fin_wait_1:
1075 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Closing",
1076 conn->name);
1077 tcp_conn_state_set(conn, st_closing);
1078 break;
1079 case st_fin_wait_2:
1080 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Time-Wait",
1081 conn->name);
1082 tcp_conn_state_set(conn, st_time_wait);
1083 /* Start the Time-Wait timer */
1084 tcp_conn_tw_timer_set(conn);
1085 break;
1086 case st_close_wait:
1087 case st_closing:
1088 case st_last_ack:
1089 /* Do nothing */
1090 break;
1091 case st_time_wait:
1092 /* Restart the Time-Wait timer */
1093 tcp_conn_tw_timer_set(conn);
1094 break;
1095 }
1096
1097 /* Add FIN to the receive buffer */
1098 conn->rcv_buf_fin = true;
1099 fibril_condvar_broadcast(&conn->rcv_buf_cv);
1100
1101 tcp_segment_delete(seg);
1102 return cp_done;
1103 }
1104
1105 return cp_continue;
1106}
1107
1108/** Process incoming segment.
1109 *
1110 * We are in connection state where segments are processed in order
1111 * of sequence number. This processes one segment taken from the
1112 * connection incoming segments queue.
1113 *
1114 * @param conn Connection
1115 * @param seg Segment
1116 */
1117static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg)
1118{
1119 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_process(%p, %p)", conn, seg);
1120 tcp_segment_dump(seg);
1121
1122 /* Check whether segment is acceptable */
1123 /* XXX Permit valid ACKs, URGs and RSTs */
1124/* if (!seq_no_segment_acceptable(conn, seg)) {
1125 log_msg(LOG_DEFAULT, LVL_WARN, "Segment not acceptable, dropping.");
1126 if ((seg->ctrl & CTL_RST) == 0) {
1127 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1128 }
1129 return;
1130 }
1131*/
1132
1133 if (tcp_conn_seg_proc_rst(conn, seg) == cp_done)
1134 return;
1135
1136 if (tcp_conn_seg_proc_sp(conn, seg) == cp_done)
1137 return;
1138
1139 if (tcp_conn_seg_proc_syn(conn, seg) == cp_done)
1140 return;
1141
1142 if (tcp_conn_seg_proc_ack(conn, seg) == cp_done)
1143 return;
1144
1145 if (tcp_conn_seg_proc_urg(conn, seg) == cp_done)
1146 return;
1147
1148 if (tcp_conn_seg_proc_text(conn, seg) == cp_done)
1149 return;
1150
1151 if (tcp_conn_seg_proc_fin(conn, seg) == cp_done)
1152 return;
1153
1154 /*
1155 * If anything is left from the segment, insert it back into the
1156 * incoming segments queue.
1157 */
1158 if (seg->len > 0) {
1159 log_msg(LOG_DEFAULT, LVL_DEBUG, "Re-insert segment %p. seg->len=%zu",
1160 seg, (size_t) seg->len);
1161 tcp_iqueue_insert_seg(&conn->incoming, seg);
1162 } else {
1163 tcp_segment_delete(seg);
1164 }
1165}
1166
1167/** Segment arrived on a connection.
1168 *
1169 * @param conn Connection
1170 * @param seg Segment
1171 */
1172void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
1173{
1174 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
1175 conn->name, seg);
1176
1177 switch (conn->cstate) {
1178 case st_listen:
1179 tcp_conn_sa_listen(conn, seg); break;
1180 case st_syn_sent:
1181 tcp_conn_sa_syn_sent(conn, seg); break;
1182 case st_syn_received:
1183 case st_established:
1184 case st_fin_wait_1:
1185 case st_fin_wait_2:
1186 case st_close_wait:
1187 case st_closing:
1188 case st_last_ack:
1189 case st_time_wait:
1190 /* Process segments in order of sequence number */
1191 tcp_conn_sa_queue(conn, seg); break;
1192 case st_closed:
1193 log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
1194 assert(false);
1195 }
1196}
1197
1198/** Time-Wait timeout handler.
1199 *
1200 * @param arg Connection
1201 */
1202static void tw_timeout_func(void *arg)
1203{
1204 tcp_conn_t *conn = (tcp_conn_t *) arg;
1205
1206 log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p)", conn);
1207
1208 tcp_conn_lock(conn);
1209
1210 if (conn->cstate == st_closed) {
1211 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection already closed.");
1212 tcp_conn_unlock(conn);
1213 tcp_conn_delref(conn);
1214 return;
1215 }
1216
1217 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
1218 tcp_conn_remove(conn);
1219 tcp_conn_state_set(conn, st_closed);
1220
1221 tcp_conn_unlock(conn);
1222 tcp_conn_delref(conn);
1223
1224 log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p) end", conn);
1225}
1226
1227/** Start or restart the Time-Wait timeout.
1228 *
1229 * @param conn Connection
1230 */
1231void tcp_conn_tw_timer_set(tcp_conn_t *conn)
1232{
1233 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_set() begin");
1234 tcp_conn_addref(conn);
1235 fibril_timer_set_locked(conn->tw_timer, TIME_WAIT_TIMEOUT,
1236 tw_timeout_func, (void *)conn);
1237 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_set() end");
1238}
1239
1240/** Clear the Time-Wait timeout.
1241 *
1242 * @param conn Connection
1243 */
1244void tcp_conn_tw_timer_clear(tcp_conn_t *conn)
1245{
1246 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_clear() begin");
1247 if (fibril_timer_clear_locked(conn->tw_timer) == fts_active)
1248 tcp_conn_delref(conn);
1249 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_clear() end");
1250}
1251
1252/** Trim segment to the receive window.
1253 *
1254 * @param conn Connection
1255 * @param seg Segment
1256 */
1257void tcp_conn_trim_seg_to_wnd(tcp_conn_t *conn, tcp_segment_t *seg)
1258{
1259 uint32_t left, right;
1260
1261 seq_no_seg_trim_calc(conn, seg, &left, &right);
1262 tcp_segment_trim(seg, left, right);
1263}
1264
1265/** Handle unexpected segment received on a socket pair.
1266 *
1267 * We reply with an RST unless the received segment has RST.
1268 *
1269 * @param sp Socket pair which received the segment
1270 * @param seg Unexpected segment
1271 */
1272void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
1273{
1274 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
1275
1276 if ((seg->ctrl & CTL_RST) == 0)
1277 tcp_reply_rst(sp, seg);
1278}
1279
1280/** Compute flipped socket pair for response.
1281 *
1282 * Flipped socket pair has local and foreign sockets exchanged.
1283 *
1284 * @param sp Socket pair
1285 * @param fsp Place to store flipped socket pair
1286 */
1287void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
1288{
1289 fsp->local = sp->foreign;
1290 fsp->foreign = sp->local;
1291}
1292
1293/** Send RST in response to an incoming segment.
1294 *
1295 * @param sp Socket pair which received the segment
1296 * @param seg Incoming segment
1297 */
1298void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
1299{
1300 tcp_segment_t *rseg;
1301
1302 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
1303
1304 rseg = tcp_segment_make_rst(seg);
1305 tcp_transmit_segment(sp, rseg);
1306}
1307
1308/**
1309 * @}
1310 */
Note: See TracBrowser for help on using the repository browser.