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

Last change on this file since ca48672 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 36.5 KB
Line 
1/*
2 * Copyright (c) 2015 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 <errno.h>
39#include <inet/endpoint.h>
40#include <io/log.h>
41#include <macros.h>
42#include <nettl/amap.h>
43#include <stdbool.h>
44#include <stdlib.h>
45#include "conn.h"
46#include "inet.h"
47#include "iqueue.h"
48#include "pdu.h"
49#include "rqueue.h"
50#include "segment.h"
51#include "seq_no.h"
52#include "tcp_type.h"
53#include "tqueue.h"
54#include "ucall.h"
55
56#define RCV_BUF_SIZE 4096/*2*/
57#define SND_BUF_SIZE 4096
58
59#define MAX_SEGMENT_LIFETIME (15*1000*1000) //(2*60*1000*1000)
60#define TIME_WAIT_TIMEOUT (2*MAX_SEGMENT_LIFETIME)
61
62/** List of all allocated connections */
63static LIST_INITIALIZE(conn_list);
64/** Taken after tcp_conn_t lock */
65static FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
66/** Connection association map */
67static amap_t *amap;
68/** Taken after tcp_conn_t lock */
69static FIBRIL_MUTEX_INITIALIZE(amap_lock);
70
71/** Internal loopback configuration */
72tcp_lb_t tcp_conn_lb = tcp_lb_none;
73
74static void tcp_conn_seg_process(tcp_conn_t *, tcp_segment_t *);
75static void tcp_conn_tw_timer_set(tcp_conn_t *);
76static void tcp_conn_tw_timer_clear(tcp_conn_t *);
77static void tcp_transmit_segment(inet_ep2_t *, tcp_segment_t *);
78static void tcp_conn_trim_seg_to_wnd(tcp_conn_t *, tcp_segment_t *);
79static void tcp_reply_rst(inet_ep2_t *, tcp_segment_t *);
80
81static tcp_tqueue_cb_t tcp_conn_tqueue_cb = {
82 .transmit_seg = tcp_transmit_segment
83};
84
85/** Initialize connections. */
86errno_t tcp_conns_init(void)
87{
88 errno_t rc;
89
90 rc = amap_create(&amap);
91 if (rc != EOK) {
92 assert(rc == ENOMEM);
93 return ENOMEM;
94 }
95
96 return EOK;
97}
98
99/** Finalize connections. */
100void tcp_conns_fini(void)
101{
102 assert(list_empty(&conn_list));
103
104 amap_destroy(amap);
105 amap = NULL;
106}
107
108/** Create new connection structure.
109 *
110 * @param epp Endpoint pair (will be deeply copied)
111 * @return New connection or NULL
112 */
113tcp_conn_t *tcp_conn_new(inet_ep2_t *epp)
114{
115 tcp_conn_t *conn = NULL;
116 bool tqueue_inited = false;
117
118 /* Allocate connection structure */
119 conn = calloc(1, sizeof(tcp_conn_t));
120 if (conn == NULL)
121 goto error;
122
123 fibril_mutex_initialize(&conn->lock);
124
125 conn->tw_timer = fibril_timer_create(&conn->lock);
126 if (conn->tw_timer == NULL)
127 goto error;
128
129 /* One for the user, one for not being in closed state */
130 refcount_init(&conn->refcnt);
131 refcount_up(&conn->refcnt);
132
133 /* Allocate receive buffer */
134 fibril_condvar_initialize(&conn->rcv_buf_cv);
135 conn->rcv_buf_size = RCV_BUF_SIZE;
136 conn->rcv_buf_used = 0;
137 conn->rcv_buf_fin = false;
138
139 conn->rcv_buf = calloc(1, conn->rcv_buf_size);
140 if (conn->rcv_buf == NULL)
141 goto error;
142
143 /** Allocate send buffer */
144 fibril_condvar_initialize(&conn->snd_buf_cv);
145 conn->snd_buf_size = SND_BUF_SIZE;
146 conn->snd_buf_used = 0;
147 conn->snd_buf_fin = false;
148 conn->snd_buf = calloc(1, conn->snd_buf_size);
149 if (conn->snd_buf == NULL)
150 goto error;
151
152 /* Set up receive window. */
153 conn->rcv_wnd = conn->rcv_buf_size;
154
155 /* Initialize incoming segment queue */
156 tcp_iqueue_init(&conn->incoming, conn);
157
158 /* Initialize retransmission queue */
159 if (tcp_tqueue_init(&conn->retransmit, conn, &tcp_conn_tqueue_cb) !=
160 EOK) {
161 goto error;
162 }
163
164 tqueue_inited = true;
165
166 /* Connection state change signalling */
167 fibril_condvar_initialize(&conn->cstate_cv);
168
169 conn->cb = NULL;
170
171 conn->cstate = st_listen;
172 conn->reset = false;
173 conn->deleted = false;
174 conn->ap = ap_passive;
175 conn->fin_is_acked = false;
176 if (epp != NULL)
177 conn->ident = *epp;
178
179 fibril_mutex_lock(&conn_list_lock);
180 list_append(&conn->link, &conn_list);
181 fibril_mutex_unlock(&conn_list_lock);
182
183 return conn;
184
185error:
186 if (tqueue_inited)
187 tcp_tqueue_fini(&conn->retransmit);
188 if (conn != NULL && conn->rcv_buf != NULL)
189 free(conn->rcv_buf);
190 if (conn != NULL && conn->snd_buf != NULL)
191 free(conn->snd_buf);
192 if (conn != NULL && conn->tw_timer != NULL)
193 fibril_timer_destroy(conn->tw_timer);
194 if (conn != NULL)
195 free(conn);
196
197 return NULL;
198}
199
200/** Destroy connection structure.
201 *
202 * Connection structure should be destroyed when the folowing condtitions
203 * are met:
204 * (1) user has deleted the connection
205 * (2) the connection has entered closed state
206 * (3) nobody is holding references to the connection
207 *
208 * This happens when @a conn->refcnt is zero as we count (1) and (2)
209 * as special references.
210 *
211 * @param conn Connection
212 */
213static void tcp_conn_free(tcp_conn_t *conn)
214{
215 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_free(%p)", conn->name, conn);
216
217 assert(conn->mapped == false);
218 tcp_tqueue_fini(&conn->retransmit);
219
220 fibril_mutex_lock(&conn_list_lock);
221 list_remove(&conn->link);
222 fibril_mutex_unlock(&conn_list_lock);
223
224 if (conn->rcv_buf != NULL)
225 free(conn->rcv_buf);
226 if (conn->snd_buf != NULL)
227 free(conn->snd_buf);
228 if (conn->tw_timer != NULL)
229 fibril_timer_destroy(conn->tw_timer);
230 free(conn);
231}
232
233/** Add reference to connection.
234 *
235 * Increase connection reference count by one.
236 *
237 * @param conn Connection
238 */
239void tcp_conn_addref(tcp_conn_t *conn)
240{
241 log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p)",
242 conn->name, conn);
243
244 refcount_up(&conn->refcnt);
245}
246
247/** Remove reference from connection.
248 *
249 * Decrease connection reference count by one.
250 *
251 * @param conn Connection
252 */
253void tcp_conn_delref(tcp_conn_t *conn)
254{
255 log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p)",
256 conn->name, conn);
257
258 if (refcount_down(&conn->refcnt))
259 tcp_conn_free(conn);
260}
261
262/** Lock connection.
263 *
264 * Must be called before any other connection-manipulating function,
265 * except tcp_conn_{add|del}ref(). Locks the connection including
266 * its timers. Must not be called inside any of the connection
267 * timer handlers.
268 *
269 * @param conn Connection
270 */
271void tcp_conn_lock(tcp_conn_t *conn)
272{
273 fibril_mutex_lock(&conn->lock);
274}
275
276/** Unlock connection.
277 *
278 * @param conn Connection
279 */
280void tcp_conn_unlock(tcp_conn_t *conn)
281{
282 fibril_mutex_unlock(&conn->lock);
283}
284
285/** Delete connection.
286 *
287 * The caller promises not make no further references to @a conn.
288 * TCP will free @a conn eventually.
289 *
290 * @param conn Connection
291 */
292void tcp_conn_delete(tcp_conn_t *conn)
293{
294 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_delete(%p)", conn->name, conn);
295
296 assert(conn->deleted == false);
297 conn->deleted = true;
298 conn->cb = NULL;
299 conn->cb_arg = NULL;
300 tcp_conn_delref(conn);
301}
302
303/** Enlist connection.
304 *
305 * Add connection to the connection map.
306 */
307errno_t tcp_conn_add(tcp_conn_t *conn)
308{
309 inet_ep2_t aepp;
310 errno_t rc;
311
312 tcp_conn_addref(conn);
313 fibril_mutex_lock(&amap_lock);
314
315 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_add: conn=%p", conn);
316
317 rc = amap_insert(amap, &conn->ident, conn, af_allow_system, &aepp);
318 if (rc != EOK) {
319 tcp_conn_delref(conn);
320 fibril_mutex_unlock(&amap_lock);
321 return rc;
322 }
323
324 conn->ident = aepp;
325 conn->mapped = true;
326 fibril_mutex_unlock(&amap_lock);
327
328 return EOK;
329}
330
331/** Delist connection.
332 *
333 * Remove connection from the connection map.
334 */
335static void tcp_conn_remove(tcp_conn_t *conn)
336{
337 if (!conn->mapped)
338 return;
339
340 fibril_mutex_lock(&amap_lock);
341 amap_remove(amap, &conn->ident);
342 conn->mapped = false;
343 fibril_mutex_unlock(&amap_lock);
344 tcp_conn_delref(conn);
345}
346
347static void tcp_conn_state_set(tcp_conn_t *conn, tcp_cstate_t nstate)
348{
349 tcp_cstate_t old_state;
350
351 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set(%p)", conn);
352
353 old_state = conn->cstate;
354 conn->cstate = nstate;
355 fibril_condvar_broadcast(&conn->cstate_cv);
356
357 /* Run user callback function */
358 if (conn->cb != NULL && conn->cb->cstate_change != NULL) {
359 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - run user CB");
360 conn->cb->cstate_change(conn, conn->cb_arg, old_state);
361 } else {
362 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - no user CB");
363 }
364
365 assert(old_state != st_closed);
366 if (nstate == st_closed) {
367 tcp_conn_remove(conn);
368 /* Drop one reference for now being in closed state */
369 tcp_conn_delref(conn);
370 }
371}
372
373/** Synchronize connection.
374 *
375 * This is the first step of an active connection attempt,
376 * sends out SYN and sets up ISS and SND.xxx.
377 */
378void tcp_conn_sync(tcp_conn_t *conn)
379{
380 assert(fibril_mutex_is_locked(&conn->lock));
381
382 /* XXX select ISS */
383 conn->iss = 1;
384 conn->snd_nxt = conn->iss;
385 conn->snd_una = conn->iss;
386 conn->ap = ap_active;
387
388 tcp_tqueue_ctrl_seg(conn, CTL_SYN);
389 tcp_conn_state_set(conn, st_syn_sent);
390}
391
392/** FIN has been sent.
393 *
394 * This function should be called when FIN is sent over the connection,
395 * as a result the connection state is changed appropriately.
396 */
397void tcp_conn_fin_sent(tcp_conn_t *conn)
398{
399 switch (conn->cstate) {
400 case st_syn_received:
401 case st_established:
402 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Fin-Wait-1", conn->name);
403 tcp_conn_state_set(conn, st_fin_wait_1);
404 break;
405 case st_close_wait:
406 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Last-Ack", conn->name);
407 tcp_conn_state_set(conn, st_last_ack);
408 break;
409 default:
410 log_msg(LOG_DEFAULT, LVL_ERROR, "%s: Connection state %d", conn->name,
411 conn->cstate);
412 assert(false);
413 }
414
415 conn->fin_is_acked = false;
416}
417
418/** Find connection structure for specified endpoint pair.
419 *
420 * A connection is uniquely identified by a endpoint pair. Look up our
421 * connection map and return connection structure based on endpoint pair.
422 * The connection reference count is bumped by one.
423 *
424 * @param epp Endpoint pair
425 * @return Connection structure or NULL if not found.
426 */
427tcp_conn_t *tcp_conn_find_ref(inet_ep2_t *epp)
428{
429 errno_t rc;
430 void *arg;
431 tcp_conn_t *conn;
432
433 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", epp);
434
435 fibril_mutex_lock(&amap_lock);
436
437 rc = amap_find_match(amap, epp, &arg);
438 if (rc != EOK) {
439 assert(rc == ENOENT);
440 fibril_mutex_unlock(&amap_lock);
441 return NULL;
442 }
443
444 conn = (tcp_conn_t *)arg;
445 tcp_conn_addref(conn);
446
447 fibril_mutex_unlock(&amap_lock);
448 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref: got conn=%p",
449 conn);
450 return conn;
451}
452
453/** Reset connection.
454 *
455 * @param conn Connection
456 */
457void tcp_conn_reset(tcp_conn_t *conn)
458{
459 assert(fibril_mutex_is_locked(&conn->lock));
460
461 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
462
463 if (conn->cstate == st_closed)
464 return;
465
466 conn->reset = true;
467 tcp_conn_state_set(conn, st_closed);
468
469 tcp_conn_tw_timer_clear(conn);
470 tcp_tqueue_clear(&conn->retransmit);
471
472 fibril_condvar_broadcast(&conn->rcv_buf_cv);
473 fibril_condvar_broadcast(&conn->snd_buf_cv);
474}
475
476/** Signal to the user that connection has been reset.
477 *
478 * Send an out-of-band signal to the user.
479 */
480static void tcp_reset_signal(tcp_conn_t *conn)
481{
482 /* TODO */
483 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_reset_signal()", conn->name);
484}
485
486/** Determine if SYN has been received.
487 *
488 * @param conn Connection
489 * @return @c true if SYN has been received, @c false otherwise.
490 */
491bool tcp_conn_got_syn(tcp_conn_t *conn)
492{
493 switch (conn->cstate) {
494 case st_listen:
495 case st_syn_sent:
496 return false;
497 case st_syn_received:
498 case st_established:
499 case st_fin_wait_1:
500 case st_fin_wait_2:
501 case st_close_wait:
502 case st_closing:
503 case st_last_ack:
504 case st_time_wait:
505 return true;
506 case st_closed:
507 log_msg(LOG_DEFAULT, LVL_WARN, "state=%d", (int) conn->cstate);
508 assert(false);
509 }
510
511 assert(false);
512}
513
514/** Segment arrived in Listen state.
515 *
516 * @param conn Connection
517 * @param seg Segment
518 */
519static void tcp_conn_sa_listen(tcp_conn_t *conn, tcp_segment_t *seg)
520{
521 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_listen(%p, %p)", conn, seg);
522
523 if ((seg->ctrl & CTL_RST) != 0) {
524 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring incoming RST.");
525 return;
526 }
527
528 if ((seg->ctrl & CTL_ACK) != 0) {
529 log_msg(LOG_DEFAULT, LVL_DEBUG, "Incoming ACK, send acceptable RST.");
530 tcp_reply_rst(&conn->ident, seg);
531 return;
532 }
533
534 if ((seg->ctrl & CTL_SYN) == 0) {
535 log_msg(LOG_DEFAULT, LVL_DEBUG, "SYN not present. Ignoring segment.");
536 return;
537 }
538
539 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got SYN, sending SYN, ACK.");
540
541 conn->rcv_nxt = seg->seq + 1;
542 conn->irs = seg->seq;
543
544 log_msg(LOG_DEFAULT, LVL_DEBUG, "rcv_nxt=%u", conn->rcv_nxt);
545
546 if (seg->len > 1)
547 log_msg(LOG_DEFAULT, LVL_WARN, "SYN combined with data, ignoring data.");
548
549 /* XXX select ISS */
550 conn->iss = 1;
551 conn->snd_nxt = conn->iss;
552 conn->snd_una = conn->iss;
553
554 /*
555 * Surprisingly the spec does not deal with initial window setting.
556 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
557 * will always be accepted as new window setting.
558 */
559 conn->snd_wnd = seg->wnd;
560 conn->snd_wl1 = seg->seq;
561 conn->snd_wl2 = seg->seq;
562
563 tcp_conn_state_set(conn, st_syn_received);
564
565 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
566
567 tcp_segment_delete(seg);
568}
569
570/** Segment arrived in Syn-Sent state.
571 *
572 * @param conn Connection
573 * @param seg Segment
574 */
575static void tcp_conn_sa_syn_sent(tcp_conn_t *conn, tcp_segment_t *seg)
576{
577 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_syn_sent(%p, %p)", conn, seg);
578
579 if ((seg->ctrl & CTL_ACK) != 0) {
580 log_msg(LOG_DEFAULT, LVL_DEBUG, "snd_una=%u, seg.ack=%u, snd_nxt=%u",
581 conn->snd_una, seg->ack, conn->snd_nxt);
582 if (!seq_no_ack_acceptable(conn, seg->ack)) {
583 if ((seg->ctrl & CTL_RST) == 0) {
584 log_msg(LOG_DEFAULT, LVL_WARN, "ACK not acceptable, send RST");
585 tcp_reply_rst(&conn->ident, seg);
586 } else {
587 log_msg(LOG_DEFAULT, LVL_WARN, "RST,ACK not acceptable, drop");
588 }
589 return;
590 }
591 }
592
593 if ((seg->ctrl & CTL_RST) != 0) {
594 /* If we get here, we have either an acceptable ACK or no ACK */
595 if ((seg->ctrl & CTL_ACK) != 0) {
596 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Connection reset. -> Closed",
597 conn->name);
598 /* Reset connection */
599 tcp_conn_reset(conn);
600 return;
601 } else {
602 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: RST without ACK, drop",
603 conn->name);
604 return;
605 }
606 }
607
608 /* XXX precedence */
609
610 if ((seg->ctrl & CTL_SYN) == 0) {
611 log_msg(LOG_DEFAULT, LVL_DEBUG, "No SYN bit, ignoring segment.");
612 return;
613 }
614
615 conn->rcv_nxt = seg->seq + 1;
616 conn->irs = seg->seq;
617
618 if ((seg->ctrl & CTL_ACK) != 0) {
619 conn->snd_una = seg->ack;
620
621 /*
622 * Prune acked segments from retransmission queue and
623 * possibly transmit more data.
624 */
625 tcp_tqueue_ack_received(conn);
626 }
627
628 log_msg(LOG_DEFAULT, LVL_DEBUG, "Sent SYN, got SYN.");
629
630 /*
631 * Surprisingly the spec does not deal with initial window setting.
632 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
633 * will always be accepted as new window setting.
634 */
635 log_msg(LOG_DEFAULT, LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", "
636 "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq);
637 conn->snd_wnd = seg->wnd;
638 conn->snd_wl1 = seg->seq;
639 conn->snd_wl2 = seg->seq;
640
641 if (seq_no_syn_acked(conn)) {
642 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn acked -> Established", conn->name);
643 tcp_conn_state_set(conn, st_established);
644 tcp_tqueue_ctrl_seg(conn, CTL_ACK /* XXX */);
645 } else {
646 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn not acked -> Syn-Received",
647 conn->name);
648 tcp_conn_state_set(conn, st_syn_received);
649 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
650 }
651
652 tcp_segment_delete(seg);
653}
654
655/** Segment arrived in state where segments are processed in sequence order.
656 *
657 * Queue segment in incoming segments queue for processing.
658 *
659 * @param conn Connection
660 * @param seg Segment
661 */
662static void tcp_conn_sa_queue(tcp_conn_t *conn, tcp_segment_t *seg)
663{
664 tcp_segment_t *pseg;
665
666 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_seq(%p, %p)", conn, seg);
667
668 /* Discard unacceptable segments ("old duplicates") */
669 if (!seq_no_segment_acceptable(conn, seg)) {
670 log_msg(LOG_DEFAULT, LVL_DEBUG, "Replying ACK to unacceptable segment.");
671 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
672 tcp_segment_delete(seg);
673 return;
674 }
675
676 /* Queue for processing */
677 tcp_iqueue_insert_seg(&conn->incoming, seg);
678
679 /*
680 * Process all segments from incoming queue that are ready.
681 * Unacceptable segments are discarded by tcp_iqueue_get_ready_seg().
682 *
683 * XXX Need to return ACK for unacceptable segments
684 */
685 while (tcp_iqueue_get_ready_seg(&conn->incoming, &pseg) == EOK)
686 tcp_conn_seg_process(conn, pseg);
687}
688
689/** Process segment RST field.
690 *
691 * @param conn Connection
692 * @param seg Segment
693 * @return cp_done if we are done with this segment, cp_continue
694 * if not
695 */
696static cproc_t tcp_conn_seg_proc_rst(tcp_conn_t *conn, tcp_segment_t *seg)
697{
698 if ((seg->ctrl & CTL_RST) == 0)
699 return cp_continue;
700
701 switch (conn->cstate) {
702 case st_syn_received:
703 /* XXX In case of passive open, revert to Listen state */
704 if (conn->ap == ap_passive) {
705 tcp_conn_state_set(conn, st_listen);
706 /* XXX Revert conn->ident */
707 tcp_conn_tw_timer_clear(conn);
708 tcp_tqueue_clear(&conn->retransmit);
709 } else {
710 tcp_conn_reset(conn);
711 }
712 break;
713 case st_established:
714 case st_fin_wait_1:
715 case st_fin_wait_2:
716 case st_close_wait:
717 /* General "connection reset" signal */
718 tcp_reset_signal(conn);
719 tcp_conn_reset(conn);
720 break;
721 case st_closing:
722 case st_last_ack:
723 case st_time_wait:
724 tcp_conn_reset(conn);
725 break;
726 case st_listen:
727 case st_syn_sent:
728 case st_closed:
729 assert(false);
730 }
731
732 return cp_done;
733}
734
735/** Process segment security and precedence fields.
736 *
737 * @param conn Connection
738 * @param seg Segment
739 * @return cp_done if we are done with this segment, cp_continue
740 * if not
741 */
742static cproc_t tcp_conn_seg_proc_sp(tcp_conn_t *conn, tcp_segment_t *seg)
743{
744 /* TODO */
745 return cp_continue;
746}
747
748/** Process segment SYN field.
749 *
750 * @param conn Connection
751 * @param seg Segment
752 * @return cp_done if we are done with this segment, cp_continue
753 * if not
754 */
755static cproc_t tcp_conn_seg_proc_syn(tcp_conn_t *conn, tcp_segment_t *seg)
756{
757 if ((seg->ctrl & CTL_SYN) == 0)
758 return cp_continue;
759
760 /*
761 * Assert SYN is in receive window, otherwise this step should not
762 * be reached.
763 */
764 assert(seq_no_in_rcv_wnd(conn, seg->seq));
765
766 log_msg(LOG_DEFAULT, LVL_WARN, "SYN is in receive window, should send reset. XXX");
767
768 /*
769 * TODO
770 *
771 * Send a reset, resond "reset" to all outstanding RECEIVEs and SEND,
772 * flush segment queues. Send unsolicited "connection reset" signal
773 * to user, connection -> closed state, delete TCB, return.
774 */
775 return cp_done;
776}
777
778/** Process segment ACK field in Syn-Received state.
779 *
780 * @param conn Connection
781 * @param seg Segment
782 * @return cp_done if we are done with this segment, cp_continue
783 * if not
784 */
785static cproc_t tcp_conn_seg_proc_ack_sr(tcp_conn_t *conn, tcp_segment_t *seg)
786{
787 if (!seq_no_ack_acceptable(conn, seg->ack)) {
788 /* ACK is not acceptable, send RST. */
789 log_msg(LOG_DEFAULT, LVL_WARN, "Segment ACK not acceptable, sending RST.");
790 tcp_reply_rst(&conn->ident, seg);
791 tcp_segment_delete(seg);
792 return cp_done;
793 }
794
795 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: SYN ACKed -> Established", conn->name);
796
797 tcp_conn_state_set(conn, st_established);
798
799 /* XXX Not mentioned in spec?! */
800 conn->snd_una = seg->ack;
801
802 return cp_continue;
803}
804
805/** Process segment ACK field in Established 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_est(tcp_conn_t *conn, tcp_segment_t *seg)
813{
814 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_proc_ack_est(%p, %p)", conn, seg);
815
816 log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.ACK=%u, SND.UNA=%u, SND.NXT=%u",
817 (unsigned)seg->ack, (unsigned)conn->snd_una,
818 (unsigned)conn->snd_nxt);
819
820 if (!seq_no_ack_acceptable(conn, seg->ack)) {
821 log_msg(LOG_DEFAULT, LVL_DEBUG, "ACK not acceptable.");
822 if (!seq_no_ack_duplicate(conn, seg->ack)) {
823 log_msg(LOG_DEFAULT, LVL_WARN, "Not acceptable, not duplicate. "
824 "Send ACK and drop.");
825 /* Not acceptable, not duplicate. Send ACK and drop. */
826 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
827 tcp_segment_delete(seg);
828 return cp_done;
829 } else {
830 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring duplicate ACK.");
831 }
832 } else {
833 /* Update SND.UNA */
834 conn->snd_una = seg->ack;
835 }
836
837 if (seq_no_new_wnd_update(conn, seg)) {
838 conn->snd_wnd = seg->wnd;
839 conn->snd_wl1 = seg->seq;
840 conn->snd_wl2 = seg->ack;
841
842 log_msg(LOG_DEFAULT, LVL_DEBUG, "Updating send window, SND.WND=%" PRIu32
843 ", SND.WL1=%" PRIu32 ", SND.WL2=%" PRIu32,
844 conn->snd_wnd, conn->snd_wl1, conn->snd_wl2);
845 }
846
847 /*
848 * Prune acked segments from retransmission queue and
849 * possibly transmit more data.
850 */
851 tcp_tqueue_ack_received(conn);
852
853 return cp_continue;
854}
855
856/** Process segment ACK field in Fin-Wait-1 state.
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_fw1(tcp_conn_t *conn, tcp_segment_t *seg)
864{
865 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
866 return cp_done;
867
868 if (conn->fin_is_acked) {
869 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Fin-Wait-2", conn->name);
870 tcp_conn_state_set(conn, st_fin_wait_2);
871 }
872
873 return cp_continue;
874}
875
876/** Process segment ACK field in Fin-Wait-2 state.
877 *
878 * @param conn Connection
879 * @param seg Segment
880 * @return cp_done if we are done with this segment, cp_continue
881 * if not
882 */
883static cproc_t tcp_conn_seg_proc_ack_fw2(tcp_conn_t *conn, tcp_segment_t *seg)
884{
885 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
886 return cp_done;
887
888 /* TODO */
889 return cp_continue;
890}
891
892/** Process segment ACK field in Close-Wait state.
893 *
894 * @param conn Connection
895 * @param seg Segment
896 * @return cp_done if we are done with this segment, cp_continue
897 * if not
898 */
899static cproc_t tcp_conn_seg_proc_ack_cw(tcp_conn_t *conn, tcp_segment_t *seg)
900{
901 /* The same processing as in Established state */
902 return tcp_conn_seg_proc_ack_est(conn, seg);
903}
904
905/** Process segment ACK field in Closing state.
906 *
907 * @param conn Connection
908 * @param seg Segment
909 * @return cp_done if we are done with this segment, cp_continue
910 * if not
911 */
912static cproc_t tcp_conn_seg_proc_ack_cls(tcp_conn_t *conn, tcp_segment_t *seg)
913{
914 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
915 return cp_done;
916
917 if (conn->fin_is_acked) {
918 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Time-Wait",
919 conn->name);
920 tcp_conn_state_set(conn, st_time_wait);
921 }
922
923 return cp_continue;
924}
925
926/** Process segment ACK field in Last-Ack state.
927 *
928 * @param conn Connection
929 * @param seg Segment
930 * @return cp_done if we are done with this segment, cp_continue
931 * if not
932 */
933static cproc_t tcp_conn_seg_proc_ack_la(tcp_conn_t *conn, tcp_segment_t *seg)
934{
935 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
936 return cp_done;
937
938 if (conn->fin_is_acked) {
939 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
940 tcp_conn_state_set(conn, st_closed);
941 return cp_done;
942 }
943
944 return cp_continue;
945}
946
947/** Process segment ACK field in Time-Wait state.
948 *
949 * @param conn Connection
950 * @param seg Segment
951 * @return cp_done if we are done with this segment, cp_continue
952 * if not
953 */
954static cproc_t tcp_conn_seg_proc_ack_tw(tcp_conn_t *conn, tcp_segment_t *seg)
955{
956 /* Nothing to do */
957 return cp_continue;
958}
959
960/** Process segment ACK field.
961 *
962 * @param conn Connection
963 * @param seg Segment
964 * @return cp_done if we are done with this segment, cp_continue
965 * if not
966 */
967static cproc_t tcp_conn_seg_proc_ack(tcp_conn_t *conn, tcp_segment_t *seg)
968{
969 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_ack(%p, %p)",
970 conn->name, conn, seg);
971
972 if ((seg->ctrl & CTL_ACK) == 0) {
973 log_msg(LOG_DEFAULT, LVL_WARN, "Segment has no ACK. Dropping.");
974 tcp_segment_delete(seg);
975 return cp_done;
976 }
977
978 switch (conn->cstate) {
979 case st_syn_received:
980 return tcp_conn_seg_proc_ack_sr(conn, seg);
981 case st_established:
982 return tcp_conn_seg_proc_ack_est(conn, seg);
983 case st_fin_wait_1:
984 return tcp_conn_seg_proc_ack_fw1(conn, seg);
985 case st_fin_wait_2:
986 return tcp_conn_seg_proc_ack_fw2(conn, seg);
987 case st_close_wait:
988 return tcp_conn_seg_proc_ack_cw(conn, seg);
989 case st_closing:
990 return tcp_conn_seg_proc_ack_cls(conn, seg);
991 case st_last_ack:
992 return tcp_conn_seg_proc_ack_la(conn, seg);
993 case st_time_wait:
994 return tcp_conn_seg_proc_ack_tw(conn, seg);
995 case st_listen:
996 case st_syn_sent:
997 case st_closed:
998 assert(false);
999 }
1000
1001 assert(false);
1002}
1003
1004/** Process segment URG field.
1005 *
1006 * @param conn Connection
1007 * @param seg Segment
1008 * @return cp_done if we are done with this segment, cp_continue
1009 * if not
1010 */
1011static cproc_t tcp_conn_seg_proc_urg(tcp_conn_t *conn, tcp_segment_t *seg)
1012{
1013 return cp_continue;
1014}
1015
1016/** Process segment text.
1017 *
1018 * @param conn Connection
1019 * @param seg Segment
1020 * @return cp_done if we are done with this segment, cp_continue
1021 * if not
1022 */
1023static cproc_t tcp_conn_seg_proc_text(tcp_conn_t *conn, tcp_segment_t *seg)
1024{
1025 size_t text_size;
1026 size_t xfer_size;
1027
1028 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_text(%p, %p)",
1029 conn->name, conn, seg);
1030
1031 switch (conn->cstate) {
1032 case st_established:
1033 case st_fin_wait_1:
1034 case st_fin_wait_2:
1035 /* OK */
1036 break;
1037 case st_close_wait:
1038 case st_closing:
1039 case st_last_ack:
1040 case st_time_wait:
1041 /* Invalid since FIN has been received. Ignore text. */
1042 return cp_continue;
1043 case st_listen:
1044 case st_syn_sent:
1045 case st_syn_received:
1046 case st_closed:
1047 assert(false);
1048 }
1049
1050 /*
1051 * Process segment text
1052 */
1053 assert(seq_no_segment_ready(conn, seg));
1054
1055 /* Trim anything outside our receive window */
1056 tcp_conn_trim_seg_to_wnd(conn, seg);
1057
1058 /* Determine how many bytes to copy */
1059 text_size = tcp_segment_text_size(seg);
1060 xfer_size = min(text_size, conn->rcv_buf_size - conn->rcv_buf_used);
1061
1062 /* Copy data to receive buffer */
1063 tcp_segment_text_copy(seg, conn->rcv_buf + conn->rcv_buf_used,
1064 xfer_size);
1065 conn->rcv_buf_used += xfer_size;
1066
1067 /* Signal to the receive function that new data has arrived */
1068 if (xfer_size > 0) {
1069 fibril_condvar_broadcast(&conn->rcv_buf_cv);
1070 if (conn->cb != NULL && conn->cb->recv_data != NULL)
1071 conn->cb->recv_data(conn, conn->cb_arg);
1072 }
1073
1074 log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
1075
1076 /* Advance RCV.NXT */
1077 conn->rcv_nxt += xfer_size;
1078
1079 /* Update receive window. XXX Not an efficient strategy. */
1080 conn->rcv_wnd -= xfer_size;
1081
1082 /* Send ACK */
1083 if (xfer_size > 0)
1084 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1085
1086 if (xfer_size < seg->len) {
1087 /* Trim part of segment which we just received */
1088 tcp_conn_trim_seg_to_wnd(conn, seg);
1089 } else {
1090 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Nothing left in segment, dropping "
1091 "(xfer_size=%zu, SEG.LEN=%" PRIu32 ", seg->ctrl=%u)",
1092 conn->name, xfer_size, seg->len, (unsigned int) seg->ctrl);
1093 /* Nothing left in segment */
1094 tcp_segment_delete(seg);
1095 return cp_done;
1096 }
1097
1098 return cp_continue;
1099}
1100
1101/** Process segment FIN field.
1102 *
1103 * @param conn Connection
1104 * @param seg Segment
1105 * @return cp_done if we are done with this segment, cp_continue
1106 * if not
1107 */
1108static cproc_t tcp_conn_seg_proc_fin(tcp_conn_t *conn, tcp_segment_t *seg)
1109{
1110 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_fin(%p, %p)",
1111 conn->name, conn, seg);
1112 log_msg(LOG_DEFAULT, LVL_DEBUG, " seg->len=%zu, seg->ctl=%u", (size_t) seg->len,
1113 (unsigned) seg->ctrl);
1114
1115 /* Only process FIN if no text is left in segment. */
1116 if (tcp_segment_text_size(seg) == 0 && (seg->ctrl & CTL_FIN) != 0) {
1117 log_msg(LOG_DEFAULT, LVL_DEBUG, " - FIN found in segment.");
1118
1119 conn->rcv_nxt++;
1120 conn->rcv_wnd--;
1121
1122 /* Send ACK */
1123 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1124
1125 /* Change connection state */
1126 switch (conn->cstate) {
1127 case st_listen:
1128 case st_syn_sent:
1129 case st_closed:
1130 /* Connection not synchronized */
1131 assert(false);
1132 /* Fallthrough */
1133 case st_syn_received:
1134 case st_established:
1135 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Close-Wait",
1136 conn->name);
1137 tcp_conn_state_set(conn, st_close_wait);
1138 break;
1139 case st_fin_wait_1:
1140 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Closing",
1141 conn->name);
1142 tcp_conn_state_set(conn, st_closing);
1143 break;
1144 case st_fin_wait_2:
1145 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Time-Wait",
1146 conn->name);
1147 tcp_conn_state_set(conn, st_time_wait);
1148 /* Start the Time-Wait timer */
1149 tcp_conn_tw_timer_set(conn);
1150 break;
1151 case st_close_wait:
1152 case st_closing:
1153 case st_last_ack:
1154 /* Do nothing */
1155 break;
1156 case st_time_wait:
1157 /* Restart the Time-Wait timer */
1158 tcp_conn_tw_timer_set(conn);
1159 break;
1160 }
1161
1162 /* Add FIN to the receive buffer */
1163 conn->rcv_buf_fin = true;
1164 fibril_condvar_broadcast(&conn->rcv_buf_cv);
1165 if (conn->cb != NULL && conn->cb->recv_data != NULL)
1166 conn->cb->recv_data(conn, conn->cb_arg);
1167
1168 tcp_segment_delete(seg);
1169 return cp_done;
1170 }
1171
1172 return cp_continue;
1173}
1174
1175/** Process incoming segment.
1176 *
1177 * We are in connection state where segments are processed in order
1178 * of sequence number. This processes one segment taken from the
1179 * connection incoming segments queue.
1180 *
1181 * @param conn Connection
1182 * @param seg Segment
1183 */
1184static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg)
1185{
1186 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_process(%p, %p)", conn, seg);
1187 tcp_segment_dump(seg);
1188
1189 if (tcp_conn_seg_proc_rst(conn, seg) == cp_done)
1190 return;
1191
1192 if (tcp_conn_seg_proc_sp(conn, seg) == cp_done)
1193 return;
1194
1195 if (tcp_conn_seg_proc_syn(conn, seg) == cp_done)
1196 return;
1197
1198 if (tcp_conn_seg_proc_ack(conn, seg) == cp_done)
1199 return;
1200
1201 if (tcp_conn_seg_proc_urg(conn, seg) == cp_done)
1202 return;
1203
1204 if (tcp_conn_seg_proc_text(conn, seg) == cp_done)
1205 return;
1206
1207 if (tcp_conn_seg_proc_fin(conn, seg) == cp_done)
1208 return;
1209
1210 /*
1211 * If anything is left from the segment, insert it back into the
1212 * incoming segments queue.
1213 */
1214 if (seg->len > 0) {
1215 log_msg(LOG_DEFAULT, LVL_DEBUG, "Re-insert segment %p. seg->len=%zu",
1216 seg, (size_t) seg->len);
1217 tcp_iqueue_insert_seg(&conn->incoming, seg);
1218 } else {
1219 tcp_segment_delete(seg);
1220 }
1221}
1222
1223/** Segment arrived on a connection.
1224 *
1225 * @param conn Connection
1226 * @param epp Endpoint pair on which segment was received
1227 * @param seg Segment
1228 */
1229void tcp_conn_segment_arrived(tcp_conn_t *conn, inet_ep2_t *epp,
1230 tcp_segment_t *seg)
1231{
1232 inet_ep2_t aepp;
1233 inet_ep2_t oldepp;
1234 errno_t rc;
1235
1236 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
1237 conn->name, seg);
1238
1239 tcp_conn_lock(conn);
1240
1241 if (conn->cstate == st_closed) {
1242 log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
1243 tcp_unexpected_segment(epp, seg);
1244 tcp_conn_unlock(conn);
1245 return;
1246 }
1247
1248 if (inet_addr_is_any(&conn->ident.remote.addr) ||
1249 conn->ident.remote.port == inet_port_any ||
1250 inet_addr_is_any(&conn->ident.local.addr)) {
1251
1252 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_segment_arrived: "
1253 "Changing connection ID, updating amap.");
1254 oldepp = conn->ident;
1255
1256 /* Need to remove and re-insert connection with new identity */
1257 fibril_mutex_lock(&amap_lock);
1258
1259 if (inet_addr_is_any(&conn->ident.remote.addr))
1260 conn->ident.remote.addr = epp->remote.addr;
1261
1262 if (conn->ident.remote.port == inet_port_any)
1263 conn->ident.remote.port = epp->remote.port;
1264
1265 if (inet_addr_is_any(&conn->ident.local.addr))
1266 conn->ident.local.addr = epp->local.addr;
1267
1268 rc = amap_insert(amap, &conn->ident, conn, af_allow_system, &aepp);
1269 if (rc != EOK) {
1270 assert(rc != EEXIST);
1271 assert(rc == ENOMEM);
1272 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
1273 fibril_mutex_unlock(&amap_lock);
1274 tcp_conn_unlock(conn);
1275 return;
1276 }
1277
1278 amap_remove(amap, &oldepp);
1279 fibril_mutex_unlock(&amap_lock);
1280
1281 conn->name = (char *) "a";
1282 }
1283
1284 switch (conn->cstate) {
1285 case st_listen:
1286 tcp_conn_sa_listen(conn, seg);
1287 break;
1288 case st_syn_sent:
1289 tcp_conn_sa_syn_sent(conn, seg);
1290 break;
1291 case st_syn_received:
1292 case st_established:
1293 case st_fin_wait_1:
1294 case st_fin_wait_2:
1295 case st_close_wait:
1296 case st_closing:
1297 case st_last_ack:
1298 case st_time_wait:
1299 /* Process segments in order of sequence number */
1300 tcp_conn_sa_queue(conn, seg);
1301 break;
1302 case st_closed:
1303 log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
1304 assert(false);
1305 }
1306
1307 tcp_conn_unlock(conn);
1308}
1309
1310/** Time-Wait timeout handler.
1311 *
1312 * @param arg Connection
1313 */
1314static void tw_timeout_func(void *arg)
1315{
1316 tcp_conn_t *conn = (tcp_conn_t *) arg;
1317
1318 log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p)", conn);
1319
1320 tcp_conn_lock(conn);
1321
1322 if (conn->cstate == st_closed) {
1323 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection already closed.");
1324 tcp_conn_unlock(conn);
1325 tcp_conn_delref(conn);
1326 return;
1327 }
1328
1329 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
1330 tcp_conn_state_set(conn, st_closed);
1331
1332 tcp_conn_unlock(conn);
1333 tcp_conn_delref(conn);
1334
1335 log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p) end", conn);
1336}
1337
1338/** Start or restart the Time-Wait timeout.
1339 *
1340 * @param conn Connection
1341 */
1342void tcp_conn_tw_timer_set(tcp_conn_t *conn)
1343{
1344 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_set() begin");
1345 tcp_conn_addref(conn);
1346 fibril_timer_set_locked(conn->tw_timer, TIME_WAIT_TIMEOUT,
1347 tw_timeout_func, (void *)conn);
1348 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_set() end");
1349}
1350
1351/** Clear the Time-Wait timeout.
1352 *
1353 * @param conn Connection
1354 */
1355void tcp_conn_tw_timer_clear(tcp_conn_t *conn)
1356{
1357 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_clear() begin");
1358 if (fibril_timer_clear_locked(conn->tw_timer) == fts_active)
1359 tcp_conn_delref(conn);
1360 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_tw_timer_clear() end");
1361}
1362
1363/** Trim segment to the receive window.
1364 *
1365 * @param conn Connection
1366 * @param seg Segment
1367 */
1368static void tcp_conn_trim_seg_to_wnd(tcp_conn_t *conn, tcp_segment_t *seg)
1369{
1370 uint32_t left, right;
1371
1372 seq_no_seg_trim_calc(conn, seg, &left, &right);
1373 tcp_segment_trim(seg, left, right);
1374}
1375
1376/** Handle unexpected segment received on an endpoint pair.
1377 *
1378 * We reply with an RST unless the received segment has RST.
1379 *
1380 * @param sp Endpoint pair which received the segment
1381 * @param seg Unexpected segment
1382 */
1383void tcp_unexpected_segment(inet_ep2_t *epp, tcp_segment_t *seg)
1384{
1385 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", epp,
1386 seg);
1387
1388 if ((seg->ctrl & CTL_RST) == 0)
1389 tcp_reply_rst(epp, seg);
1390}
1391
1392/** Transmit segment over network.
1393 *
1394 * @param epp Endpoint pair with source and destination information
1395 * @param seg Segment (ownership retained by caller)
1396 */
1397static void tcp_transmit_segment(inet_ep2_t *epp, tcp_segment_t *seg)
1398{
1399 tcp_pdu_t *pdu;
1400 tcp_segment_t *dseg;
1401 inet_ep2_t rident;
1402
1403 log_msg(LOG_DEFAULT, LVL_DEBUG,
1404 "tcp_transmit_segment(l:(%u),f:(%u), %p)",
1405 epp->local.port, epp->remote.port, seg);
1406
1407 log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.SEQ=%" PRIu32 ", SEG.WND=%" PRIu32,
1408 seg->seq, seg->wnd);
1409
1410 tcp_segment_dump(seg);
1411
1412 if (tcp_conn_lb == tcp_lb_segment) {
1413 /* Loop back segment */
1414#if 0
1415 tcp_ncsim_bounce_seg(sp, seg);
1416#endif
1417
1418 /* Reverse the identification */
1419 tcp_ep2_flipped(epp, &rident);
1420
1421 /* Insert segment back into rqueue */
1422 dseg = tcp_segment_dup(seg);
1423 tcp_rqueue_insert_seg(&rident, dseg);
1424 return;
1425 }
1426
1427 if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
1428 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
1429 return;
1430 }
1431
1432 if (tcp_conn_lb == tcp_lb_pdu) {
1433 /* Loop back PDU */
1434 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
1435 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
1436 tcp_pdu_delete(pdu);
1437 return;
1438 }
1439
1440 tcp_pdu_delete(pdu);
1441
1442 /* Insert decoded segment into rqueue */
1443 tcp_rqueue_insert_seg(&rident, dseg);
1444 return;
1445 }
1446
1447 tcp_transmit_pdu(pdu);
1448 tcp_pdu_delete(pdu);
1449}
1450
1451/** Compute flipped endpoint pair for response.
1452 *
1453 * Flipped endpoint pair has local and remote endpoints exchanged.
1454 *
1455 * @param epp Endpoint pair
1456 * @param fepp Place to store flipped endpoint pair
1457 */
1458void tcp_ep2_flipped(inet_ep2_t *epp, inet_ep2_t *fepp)
1459{
1460 fepp->local = epp->remote;
1461 fepp->remote = epp->local;
1462}
1463
1464/** Send RST in response to an incoming segment.
1465 *
1466 * @param epp Endpoint pair which received the segment
1467 * @param seg Incoming segment
1468 */
1469static void tcp_reply_rst(inet_ep2_t *epp, tcp_segment_t *seg)
1470{
1471 tcp_segment_t *rseg;
1472
1473 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", epp, seg);
1474
1475 rseg = tcp_segment_make_rst(seg);
1476 tcp_transmit_segment(epp, rseg);
1477}
1478
1479/**
1480 * @}
1481 */
Note: See TracBrowser for help on using the repository browser.