source: mainline/uspace/srv/net/tl/tcp/conn.c@ 415578ef

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

Add one reference for being in connection map.

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