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

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

Disable TCP debugging features.

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