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

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

Close listening TCP connections when socket is closed. Listening connections cannot be closed by sending a FIN.

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