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

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

Add unit tests for TCP tqueue. Fix tqueue possibly being finalized without freeing pending segments.

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