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

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

Must update amap when connection ident changes.

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