[c5808b41] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup tcp
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[032bbe7] | 34 | * @file TCP connection processing and state machine
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <adt/list.h>
|
---|
[3e6a98c5] | 38 | #include <stdbool.h>
|
---|
[c5808b41] | 39 | #include <errno.h>
|
---|
| 40 | #include <io/log.h>
|
---|
[0093ab6] | 41 | #include <macros.h>
|
---|
[c5808b41] | 42 | #include <stdlib.h>
|
---|
| 43 | #include "conn.h"
|
---|
| 44 | #include "iqueue.h"
|
---|
| 45 | #include "segment.h"
|
---|
| 46 | #include "seq_no.h"
|
---|
| 47 | #include "tcp_type.h"
|
---|
| 48 | #include "tqueue.h"
|
---|
[762b48a] | 49 | #include "ucall.h"
|
---|
[c5808b41] | 50 |
|
---|
[a52de0e] | 51 | #define RCV_BUF_SIZE 4096/*2*/
|
---|
[32105348] | 52 | #define SND_BUF_SIZE 4096
|
---|
[0093ab6] | 53 |
|
---|
[2a3214e] | 54 | #define MAX_SEGMENT_LIFETIME (15*1000*1000) //(2*60*1000*1000)
|
---|
| 55 | #define TIME_WAIT_TIMEOUT (2*MAX_SEGMENT_LIFETIME)
|
---|
| 56 |
|
---|
[c5808b41] | 57 | LIST_INITIALIZE(conn_list);
|
---|
[0edaf0f6] | 58 | FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
|
---|
[c5808b41] | 59 |
|
---|
| 60 | static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg);
|
---|
[2a3214e] | 61 | static void tcp_conn_tw_timer_set(tcp_conn_t *conn);
|
---|
[704586fb] | 62 | static void tcp_conn_tw_timer_clear(tcp_conn_t *conn);
|
---|
[c5808b41] | 63 |
|
---|
[7a8c1c4e] | 64 | /** Create new connection structure.
|
---|
[032bbe7] | 65 | *
|
---|
| 66 | * @param lsock Local socket (will be deeply copied)
|
---|
| 67 | * @param fsock Foreign socket (will be deeply copied)
|
---|
[7a8c1c4e] | 68 | * @return New connection or NULL
|
---|
[032bbe7] | 69 | */
|
---|
[c5808b41] | 70 | tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
|
---|
| 71 | {
|
---|
[2a3214e] | 72 | tcp_conn_t *conn = NULL;
|
---|
[7cf7ded] | 73 | bool tqueue_inited = false;
|
---|
[c5808b41] | 74 |
|
---|
[0093ab6] | 75 | /* Allocate connection structure */
|
---|
[c5808b41] | 76 | conn = calloc(1, sizeof(tcp_conn_t));
|
---|
| 77 | if (conn == NULL)
|
---|
[2a3214e] | 78 | goto error;
|
---|
| 79 |
|
---|
| 80 | conn->tw_timer = fibril_timer_create();
|
---|
| 81 | if (conn->tw_timer == NULL)
|
---|
| 82 | goto error;
|
---|
[c5808b41] | 83 |
|
---|
[0edaf0f6] | 84 | fibril_mutex_initialize(&conn->lock);
|
---|
| 85 |
|
---|
| 86 | /* One for the user, one for not being in closed state */
|
---|
| 87 | atomic_set(&conn->refcnt, 2);
|
---|
| 88 |
|
---|
[0093ab6] | 89 | /* Allocate receive buffer */
|
---|
[d9ce049] | 90 | fibril_condvar_initialize(&conn->rcv_buf_cv);
|
---|
[0093ab6] | 91 | conn->rcv_buf_size = RCV_BUF_SIZE;
|
---|
| 92 | conn->rcv_buf_used = 0;
|
---|
[8c7a054] | 93 | conn->rcv_buf_fin = false;
|
---|
[d9ce049] | 94 |
|
---|
[0093ab6] | 95 | conn->rcv_buf = calloc(1, conn->rcv_buf_size);
|
---|
[2a3214e] | 96 | if (conn->rcv_buf == NULL)
|
---|
| 97 | goto error;
|
---|
[0093ab6] | 98 |
|
---|
[32105348] | 99 | /** Allocate send buffer */
|
---|
[bbf159a] | 100 | fibril_condvar_initialize(&conn->snd_buf_cv);
|
---|
[32105348] | 101 | conn->snd_buf_size = SND_BUF_SIZE;
|
---|
| 102 | conn->snd_buf_used = 0;
|
---|
[8c7a054] | 103 | conn->snd_buf_fin = false;
|
---|
[32105348] | 104 | conn->snd_buf = calloc(1, conn->snd_buf_size);
|
---|
[2a3214e] | 105 | if (conn->snd_buf == NULL)
|
---|
| 106 | goto error;
|
---|
[32105348] | 107 |
|
---|
[0093ab6] | 108 | /* Set up receive window. */
|
---|
| 109 | conn->rcv_wnd = conn->rcv_buf_size;
|
---|
| 110 |
|
---|
| 111 | /* Initialize incoming segment queue */
|
---|
[c5808b41] | 112 | tcp_iqueue_init(&conn->incoming, conn);
|
---|
| 113 |
|
---|
[6df418c4] | 114 | /* Initialize retransmission queue */
|
---|
[7cf7ded] | 115 | if (tcp_tqueue_init(&conn->retransmit, conn) != EOK)
|
---|
| 116 | goto error;
|
---|
| 117 |
|
---|
| 118 | tqueue_inited = true;
|
---|
[6df418c4] | 119 |
|
---|
[004a5fe] | 120 | /* Connection state change signalling */
|
---|
| 121 | fibril_condvar_initialize(&conn->cstate_cv);
|
---|
| 122 |
|
---|
[ae481e0] | 123 | conn->cstate_cb = NULL;
|
---|
| 124 |
|
---|
[c5808b41] | 125 | conn->cstate = st_listen;
|
---|
[d9e14fa4] | 126 | conn->reset = false;
|
---|
[7a8c1c4e] | 127 | conn->deleted = false;
|
---|
[d9e14fa4] | 128 | conn->ap = ap_passive;
|
---|
[6df418c4] | 129 | conn->fin_is_acked = false;
|
---|
[c5808b41] | 130 | conn->ident.local = *lsock;
|
---|
| 131 | if (fsock != NULL)
|
---|
| 132 | conn->ident.foreign = *fsock;
|
---|
| 133 |
|
---|
| 134 | return conn;
|
---|
[2a3214e] | 135 |
|
---|
| 136 | error:
|
---|
[7cf7ded] | 137 | if (tqueue_inited)
|
---|
| 138 | tcp_tqueue_fini(&conn->retransmit);
|
---|
[2a3214e] | 139 | if (conn != NULL && conn->rcv_buf != NULL)
|
---|
| 140 | free(conn->rcv_buf);
|
---|
| 141 | if (conn != NULL && conn->snd_buf != NULL)
|
---|
| 142 | free(conn->snd_buf);
|
---|
| 143 | if (conn != NULL && conn->tw_timer != NULL)
|
---|
| 144 | fibril_timer_destroy(conn->tw_timer);
|
---|
| 145 | if (conn != NULL)
|
---|
| 146 | free(conn);
|
---|
| 147 |
|
---|
| 148 | return NULL;
|
---|
[c5808b41] | 149 | }
|
---|
| 150 |
|
---|
[7a8c1c4e] | 151 | /** Destroy connection structure.
|
---|
| 152 | *
|
---|
[0edaf0f6] | 153 | * Connection structure should be destroyed when the folowing condtitions
|
---|
| 154 | * are met:
|
---|
| 155 | * (1) user has deleted the connection
|
---|
| 156 | * (2) the connection has entered closed state
|
---|
| 157 | * (3) nobody is holding references to the connection
|
---|
| 158 | *
|
---|
| 159 | * This happens when @a conn->refcnt is zero as we count (1) and (2)
|
---|
| 160 | * as special references.
|
---|
[7a8c1c4e] | 161 | *
|
---|
| 162 | * @param conn Connection
|
---|
| 163 | */
|
---|
| 164 | static void tcp_conn_free(tcp_conn_t *conn)
|
---|
| 165 | {
|
---|
[a1a101d] | 166 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_free(%p)", conn->name, conn);
|
---|
[7a8c1c4e] | 167 | tcp_tqueue_fini(&conn->retransmit);
|
---|
| 168 |
|
---|
| 169 | if (conn->rcv_buf != NULL)
|
---|
| 170 | free(conn->rcv_buf);
|
---|
| 171 | if (conn->snd_buf != NULL)
|
---|
| 172 | free(conn->snd_buf);
|
---|
| 173 | if (conn->tw_timer != NULL)
|
---|
| 174 | fibril_timer_destroy(conn->tw_timer);
|
---|
| 175 | free(conn);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[0edaf0f6] | 178 | /** Add reference to connection.
|
---|
| 179 | *
|
---|
| 180 | * Increase connection reference count by one.
|
---|
| 181 | *
|
---|
| 182 | * @param conn Connection
|
---|
| 183 | */
|
---|
| 184 | void tcp_conn_addref(tcp_conn_t *conn)
|
---|
| 185 | {
|
---|
[a1a101d] | 186 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p)", conn->name, conn);
|
---|
[0edaf0f6] | 187 | atomic_inc(&conn->refcnt);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /** Remove reference from connection.
|
---|
| 191 | *
|
---|
| 192 | * Decrease connection reference count by one.
|
---|
| 193 | *
|
---|
| 194 | * @param conn Connection
|
---|
| 195 | */
|
---|
| 196 | void tcp_conn_delref(tcp_conn_t *conn)
|
---|
| 197 | {
|
---|
[a1a101d] | 198 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p)", conn->name, conn);
|
---|
[0edaf0f6] | 199 |
|
---|
| 200 | if (atomic_predec(&conn->refcnt) == 0)
|
---|
| 201 | tcp_conn_free(conn);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[7a8c1c4e] | 204 | /** Delete connection.
|
---|
| 205 | *
|
---|
| 206 | * The caller promises not make no further references to @a conn.
|
---|
| 207 | * TCP will free @a conn eventually.
|
---|
| 208 | *
|
---|
| 209 | * @param conn Connection
|
---|
| 210 | */
|
---|
| 211 | void tcp_conn_delete(tcp_conn_t *conn)
|
---|
| 212 | {
|
---|
[a1a101d] | 213 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_delete(%p)", conn->name, conn);
|
---|
[7a8c1c4e] | 214 |
|
---|
[0edaf0f6] | 215 | assert(conn->deleted == false);
|
---|
| 216 | tcp_conn_delref(conn);
|
---|
[7a8c1c4e] | 217 | }
|
---|
| 218 |
|
---|
[032bbe7] | 219 | /** Enlist connection.
|
---|
| 220 | *
|
---|
| 221 | * Add connection to the connection map.
|
---|
| 222 | */
|
---|
[c5808b41] | 223 | void tcp_conn_add(tcp_conn_t *conn)
|
---|
| 224 | {
|
---|
[e1c6dde9] | 225 | tcp_conn_addref(conn);
|
---|
[0edaf0f6] | 226 | fibril_mutex_lock(&conn_list_lock);
|
---|
[c5808b41] | 227 | list_append(&conn->link, &conn_list);
|
---|
[0edaf0f6] | 228 | fibril_mutex_unlock(&conn_list_lock);
|
---|
[c5808b41] | 229 | }
|
---|
| 230 |
|
---|
[6e88fea] | 231 | /** Delist connection.
|
---|
| 232 | *
|
---|
| 233 | * Remove connection from the connection map.
|
---|
| 234 | */
|
---|
| 235 | void tcp_conn_remove(tcp_conn_t *conn)
|
---|
| 236 | {
|
---|
[0edaf0f6] | 237 | fibril_mutex_lock(&conn_list_lock);
|
---|
[6e88fea] | 238 | list_remove(&conn->link);
|
---|
[0edaf0f6] | 239 | fibril_mutex_unlock(&conn_list_lock);
|
---|
[e1c6dde9] | 240 | tcp_conn_delref(conn);
|
---|
[6e88fea] | 241 | }
|
---|
| 242 |
|
---|
[004a5fe] | 243 | static void tcp_conn_state_set(tcp_conn_t *conn, tcp_cstate_t nstate)
|
---|
| 244 | {
|
---|
[0edaf0f6] | 245 | tcp_cstate_t old_state;
|
---|
| 246 |
|
---|
[a1a101d] | 247 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set(%p)", conn);
|
---|
[ae481e0] | 248 |
|
---|
[0edaf0f6] | 249 | old_state = conn->cstate;
|
---|
[004a5fe] | 250 | conn->cstate = nstate;
|
---|
| 251 | fibril_condvar_broadcast(&conn->cstate_cv);
|
---|
[7a8c1c4e] | 252 |
|
---|
[ae481e0] | 253 | /* Run user callback function */
|
---|
| 254 | if (conn->cstate_cb != NULL) {
|
---|
[a1a101d] | 255 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - run user CB");
|
---|
[ae481e0] | 256 | conn->cstate_cb(conn, conn->cstate_cb_arg);
|
---|
| 257 | } else {
|
---|
[a1a101d] | 258 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - no user CB");
|
---|
[ae481e0] | 259 | }
|
---|
| 260 |
|
---|
[0edaf0f6] | 261 | assert(old_state != st_closed);
|
---|
| 262 | if (nstate == st_closed) {
|
---|
| 263 | /* Drop one reference for now being in closed state */
|
---|
| 264 | tcp_conn_delref(conn);
|
---|
[7a8c1c4e] | 265 | }
|
---|
[004a5fe] | 266 | }
|
---|
| 267 |
|
---|
[032bbe7] | 268 | /** Synchronize connection.
|
---|
| 269 | *
|
---|
| 270 | * This is the first step of an active connection attempt,
|
---|
| 271 | * sends out SYN and sets up ISS and SND.xxx.
|
---|
| 272 | */
|
---|
[c5808b41] | 273 | void tcp_conn_sync(tcp_conn_t *conn)
|
---|
| 274 | {
|
---|
| 275 | /* XXX select ISS */
|
---|
| 276 | conn->iss = 1;
|
---|
| 277 | conn->snd_nxt = conn->iss;
|
---|
| 278 | conn->snd_una = conn->iss;
|
---|
[d9e14fa4] | 279 | conn->ap = ap_active;
|
---|
[c5808b41] | 280 |
|
---|
| 281 | tcp_tqueue_ctrl_seg(conn, CTL_SYN);
|
---|
[004a5fe] | 282 | tcp_conn_state_set(conn, st_syn_sent);
|
---|
[c5808b41] | 283 | }
|
---|
| 284 |
|
---|
[f343a16] | 285 | /** FIN has been sent.
|
---|
| 286 | *
|
---|
| 287 | * This function should be called when FIN is sent over the connection,
|
---|
| 288 | * as a result the connection state is changed appropriately.
|
---|
| 289 | */
|
---|
| 290 | void tcp_conn_fin_sent(tcp_conn_t *conn)
|
---|
| 291 | {
|
---|
| 292 | switch (conn->cstate) {
|
---|
| 293 | case st_syn_received:
|
---|
| 294 | case st_established:
|
---|
[a1a101d] | 295 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Fin-Wait-1", conn->name);
|
---|
[004a5fe] | 296 | tcp_conn_state_set(conn, st_fin_wait_1);
|
---|
[f343a16] | 297 | break;
|
---|
| 298 | case st_close_wait:
|
---|
[a1a101d] | 299 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Last-Ack", conn->name);
|
---|
[004a5fe] | 300 | tcp_conn_state_set(conn, st_last_ack);
|
---|
[f343a16] | 301 | break;
|
---|
| 302 | default:
|
---|
[a1a101d] | 303 | log_msg(LOG_DEFAULT, LVL_ERROR, "%s: Connection state %d", conn->name,
|
---|
[6896409c] | 304 | conn->cstate);
|
---|
[f343a16] | 305 | assert(false);
|
---|
| 306 | }
|
---|
[6df418c4] | 307 |
|
---|
| 308 | conn->fin_is_acked = false;
|
---|
[f343a16] | 309 | }
|
---|
| 310 |
|
---|
[92b42442] | 311 | /** Match socket with pattern. */
|
---|
[3aa2642a] | 312 | static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt)
|
---|
[c5808b41] | 313 | {
|
---|
[a2e3ee6] | 314 | if ((!inet_addr_is_any(&patt->addr)) &&
|
---|
| 315 | (!inet_addr_compare(&patt->addr, &sock->addr)))
|
---|
[c5808b41] | 316 | return false;
|
---|
| 317 |
|
---|
[a2e3ee6] | 318 | if ((patt->port != TCP_PORT_ANY) &&
|
---|
| 319 | (patt->port != sock->port))
|
---|
[c5808b41] | 320 | return false;
|
---|
| 321 |
|
---|
[a1a101d] | 322 | log_msg(LOG_DEFAULT, LVL_DEBUG2, " -> match");
|
---|
[c5808b41] | 323 |
|
---|
| 324 | return true;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[92b42442] | 327 | /** Match socket pair with pattern. */
|
---|
[c5808b41] | 328 | static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern)
|
---|
| 329 | {
|
---|
[a1a101d] | 330 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_sockpair_match(%p, %p)", sp, pattern);
|
---|
[c5808b41] | 331 |
|
---|
[3aa2642a] | 332 | if (!tcp_socket_match(&sp->local, &pattern->local))
|
---|
[c5808b41] | 333 | return false;
|
---|
| 334 |
|
---|
[3aa2642a] | 335 | if (!tcp_socket_match(&sp->foreign, &pattern->foreign))
|
---|
[c5808b41] | 336 | return false;
|
---|
| 337 |
|
---|
| 338 | return true;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[032bbe7] | 341 | /** Find connection structure for specified socket pair.
|
---|
| 342 | *
|
---|
| 343 | * A connection is uniquely identified by a socket pair. Look up our
|
---|
| 344 | * connection map and return connection structure based on socket pair.
|
---|
[0edaf0f6] | 345 | * The connection reference count is bumped by one.
|
---|
[032bbe7] | 346 | *
|
---|
| 347 | * @param sp Socket pair
|
---|
| 348 | * @return Connection structure or NULL if not found.
|
---|
| 349 | */
|
---|
[0edaf0f6] | 350 | tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp)
|
---|
[c5808b41] | 351 | {
|
---|
[a1a101d] | 352 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
|
---|
[f597bc4] | 353 |
|
---|
[0edaf0f6] | 354 | fibril_mutex_lock(&conn_list_lock);
|
---|
[f597bc4] | 355 |
|
---|
[c5808b41] | 356 | list_foreach(conn_list, link) {
|
---|
| 357 | tcp_conn_t *conn = list_get_instance(link, tcp_conn_t, link);
|
---|
[704586fb] | 358 | tcp_sockpair_t *csp = &conn->ident;
|
---|
[f597bc4] | 359 |
|
---|
[704586fb] | 360 | if (tcp_sockpair_match(sp, csp)) {
|
---|
[0edaf0f6] | 361 | tcp_conn_addref(conn);
|
---|
| 362 | fibril_mutex_unlock(&conn_list_lock);
|
---|
[c5808b41] | 363 | return conn;
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
[f597bc4] | 366 |
|
---|
[0edaf0f6] | 367 | fibril_mutex_unlock(&conn_list_lock);
|
---|
[c5808b41] | 368 | return NULL;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[704586fb] | 371 | /** Reset connection.
|
---|
| 372 | *
|
---|
| 373 | * @param conn Connection
|
---|
| 374 | */
|
---|
| 375 | static void tcp_conn_reset(tcp_conn_t *conn)
|
---|
| 376 | {
|
---|
[a1a101d] | 377 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
|
---|
[704586fb] | 378 | tcp_conn_state_set(conn, st_closed);
|
---|
[d9e14fa4] | 379 | conn->reset = true;
|
---|
| 380 |
|
---|
[704586fb] | 381 | tcp_conn_tw_timer_clear(conn);
|
---|
| 382 | tcp_tqueue_clear(&conn->retransmit);
|
---|
[d9e14fa4] | 383 |
|
---|
| 384 | fibril_condvar_broadcast(&conn->rcv_buf_cv);
|
---|
[bbf159a] | 385 | fibril_condvar_broadcast(&conn->snd_buf_cv);
|
---|
[d9e14fa4] | 386 | }
|
---|
| 387 |
|
---|
| 388 | /** Signal to the user that connection has been reset.
|
---|
| 389 | *
|
---|
| 390 | * Send an out-of-band signal to the user.
|
---|
| 391 | */
|
---|
| 392 | static void tcp_reset_signal(tcp_conn_t *conn)
|
---|
| 393 | {
|
---|
| 394 | /* TODO */
|
---|
[a1a101d] | 395 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_reset_signal()", conn->name);
|
---|
[704586fb] | 396 | }
|
---|
| 397 |
|
---|
[32105348] | 398 | /** Determine if SYN has been received.
|
---|
| 399 | *
|
---|
| 400 | * @param conn Connection
|
---|
| 401 | * @return @c true if SYN has been received, @c false otherwise.
|
---|
| 402 | */
|
---|
| 403 | bool tcp_conn_got_syn(tcp_conn_t *conn)
|
---|
| 404 | {
|
---|
| 405 | switch (conn->cstate) {
|
---|
| 406 | case st_listen:
|
---|
| 407 | case st_syn_sent:
|
---|
| 408 | return false;
|
---|
| 409 | case st_syn_received:
|
---|
| 410 | case st_established:
|
---|
| 411 | case st_fin_wait_1:
|
---|
| 412 | case st_fin_wait_2:
|
---|
| 413 | case st_close_wait:
|
---|
| 414 | case st_closing:
|
---|
| 415 | case st_last_ack:
|
---|
| 416 | case st_time_wait:
|
---|
| 417 | return true;
|
---|
| 418 | case st_closed:
|
---|
[a1a101d] | 419 | log_msg(LOG_DEFAULT, LVL_WARN, "state=%d", (int) conn->cstate);
|
---|
[32105348] | 420 | assert(false);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | assert(false);
|
---|
| 424 | }
|
---|
| 425 |
|
---|
[032bbe7] | 426 | /** Segment arrived in Listen state.
|
---|
| 427 | *
|
---|
| 428 | * @param conn Connection
|
---|
| 429 | * @param seg Segment
|
---|
| 430 | */
|
---|
[c5808b41] | 431 | static void tcp_conn_sa_listen(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 432 | {
|
---|
[a1a101d] | 433 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_listen(%p, %p)", conn, seg);
|
---|
[c5808b41] | 434 |
|
---|
| 435 | if ((seg->ctrl & CTL_RST) != 0) {
|
---|
[a1a101d] | 436 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring incoming RST.");
|
---|
[c5808b41] | 437 | return;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
[a1a101d] | 441 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Incoming ACK, send acceptable RST.");
|
---|
[c5808b41] | 442 | tcp_reply_rst(&conn->ident, seg);
|
---|
| 443 | return;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | if ((seg->ctrl & CTL_SYN) == 0) {
|
---|
[a1a101d] | 447 | log_msg(LOG_DEFAULT, LVL_DEBUG, "SYN not present. Ignoring segment.");
|
---|
[c5808b41] | 448 | return;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[a1a101d] | 451 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Got SYN, sending SYN, ACK.");
|
---|
[c5808b41] | 452 |
|
---|
| 453 | conn->rcv_nxt = seg->seq + 1;
|
---|
| 454 | conn->irs = seg->seq;
|
---|
| 455 |
|
---|
[4c55a64] | 456 |
|
---|
[a1a101d] | 457 | log_msg(LOG_DEFAULT, LVL_DEBUG, "rcv_nxt=%u", conn->rcv_nxt);
|
---|
[c5808b41] | 458 |
|
---|
| 459 | if (seg->len > 1)
|
---|
[a1a101d] | 460 | log_msg(LOG_DEFAULT, LVL_WARN, "SYN combined with data, ignoring data.");
|
---|
[c5808b41] | 461 |
|
---|
| 462 | /* XXX select ISS */
|
---|
| 463 | conn->iss = 1;
|
---|
| 464 | conn->snd_nxt = conn->iss;
|
---|
| 465 | conn->snd_una = conn->iss;
|
---|
| 466 |
|
---|
[4c55a64] | 467 | /*
|
---|
| 468 | * Surprisingly the spec does not deal with initial window setting.
|
---|
| 469 | * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
|
---|
| 470 | * will always be accepted as new window setting.
|
---|
| 471 | */
|
---|
| 472 | conn->snd_wnd = seg->wnd;
|
---|
| 473 | conn->snd_wl1 = seg->seq;
|
---|
| 474 | conn->snd_wl2 = seg->seq;
|
---|
| 475 |
|
---|
[004a5fe] | 476 | tcp_conn_state_set(conn, st_syn_received);
|
---|
[c5808b41] | 477 |
|
---|
| 478 | tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
|
---|
[0093ab6] | 479 |
|
---|
| 480 | tcp_segment_delete(seg);
|
---|
[c5808b41] | 481 | }
|
---|
| 482 |
|
---|
[032bbe7] | 483 | /** Segment arrived in Syn-Sent state.
|
---|
| 484 | *
|
---|
| 485 | * @param conn Connection
|
---|
| 486 | * @param seg Segment
|
---|
| 487 | */
|
---|
[c5808b41] | 488 | static void tcp_conn_sa_syn_sent(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 489 | {
|
---|
[a1a101d] | 490 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_syn_sent(%p, %p)", conn, seg);
|
---|
[c5808b41] | 491 |
|
---|
| 492 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
[a1a101d] | 493 | log_msg(LOG_DEFAULT, LVL_DEBUG, "snd_una=%u, seg.ack=%u, snd_nxt=%u",
|
---|
[c5808b41] | 494 | conn->snd_una, seg->ack, conn->snd_nxt);
|
---|
| 495 | if (!seq_no_ack_acceptable(conn, seg->ack)) {
|
---|
[2f0dd2a] | 496 | if ((seg->ctrl & CTL_RST) == 0) {
|
---|
[a1a101d] | 497 | log_msg(LOG_DEFAULT, LVL_WARN, "ACK not acceptable, send RST");
|
---|
[2f0dd2a] | 498 | tcp_reply_rst(&conn->ident, seg);
|
---|
| 499 | } else {
|
---|
[a1a101d] | 500 | log_msg(LOG_DEFAULT, LVL_WARN, "RST,ACK not acceptable, drop");
|
---|
[2f0dd2a] | 501 | }
|
---|
[c5808b41] | 502 | return;
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | if ((seg->ctrl & CTL_RST) != 0) {
|
---|
[2f0dd2a] | 507 | /* If we get here, we have either an acceptable ACK or no ACK */
|
---|
| 508 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
[a1a101d] | 509 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Connection reset. -> Closed",
|
---|
[2f0dd2a] | 510 | conn->name);
|
---|
| 511 | /* Reset connection */
|
---|
| 512 | tcp_conn_reset(conn);
|
---|
| 513 | return;
|
---|
| 514 | } else {
|
---|
[a1a101d] | 515 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: RST without ACK, drop",
|
---|
[2f0dd2a] | 516 | conn->name);
|
---|
| 517 | return;
|
---|
| 518 | }
|
---|
[c5808b41] | 519 | }
|
---|
| 520 |
|
---|
| 521 | /* XXX precedence */
|
---|
| 522 |
|
---|
| 523 | if ((seg->ctrl & CTL_SYN) == 0) {
|
---|
[a1a101d] | 524 | log_msg(LOG_DEFAULT, LVL_DEBUG, "No SYN bit, ignoring segment.");
|
---|
[c5808b41] | 525 | return;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | conn->rcv_nxt = seg->seq + 1;
|
---|
| 529 | conn->irs = seg->seq;
|
---|
| 530 |
|
---|
| 531 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
| 532 | conn->snd_una = seg->ack;
|
---|
[d9ce049] | 533 |
|
---|
| 534 | /*
|
---|
| 535 | * Prune acked segments from retransmission queue and
|
---|
| 536 | * possibly transmit more data.
|
---|
| 537 | */
|
---|
| 538 | tcp_tqueue_ack_received(conn);
|
---|
[c5808b41] | 539 | }
|
---|
| 540 |
|
---|
[a1a101d] | 541 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Sent SYN, got SYN.");
|
---|
[c5808b41] | 542 |
|
---|
[32105348] | 543 | /*
|
---|
| 544 | * Surprisingly the spec does not deal with initial window setting.
|
---|
| 545 | * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
|
---|
| 546 | * will always be accepted as new window setting.
|
---|
| 547 | */
|
---|
[a1a101d] | 548 | log_msg(LOG_DEFAULT, LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", "
|
---|
[32105348] | 549 | "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq);
|
---|
| 550 | conn->snd_wnd = seg->wnd;
|
---|
| 551 | conn->snd_wl1 = seg->seq;
|
---|
| 552 | conn->snd_wl2 = seg->seq;
|
---|
| 553 |
|
---|
[c5808b41] | 554 | if (seq_no_syn_acked(conn)) {
|
---|
[a1a101d] | 555 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn acked -> Established", conn->name);
|
---|
[004a5fe] | 556 | tcp_conn_state_set(conn, st_established);
|
---|
[c5808b41] | 557 | tcp_tqueue_ctrl_seg(conn, CTL_ACK /* XXX */);
|
---|
| 558 | } else {
|
---|
[a1a101d] | 559 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn not acked -> Syn-Received",
|
---|
[6896409c] | 560 | conn->name);
|
---|
[004a5fe] | 561 | tcp_conn_state_set(conn, st_syn_received);
|
---|
[c5808b41] | 562 | tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
|
---|
| 563 | }
|
---|
[0093ab6] | 564 |
|
---|
| 565 | tcp_segment_delete(seg);
|
---|
[c5808b41] | 566 | }
|
---|
| 567 |
|
---|
[032bbe7] | 568 | /** Segment arrived in state where segments are processed in sequence order.
|
---|
| 569 | *
|
---|
| 570 | * Queue segment in incoming segments queue for processing.
|
---|
| 571 | *
|
---|
| 572 | * @param conn Connection
|
---|
| 573 | * @param seg Segment
|
---|
| 574 | */
|
---|
[c5808b41] | 575 | static void tcp_conn_sa_queue(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 576 | {
|
---|
| 577 | tcp_segment_t *pseg;
|
---|
| 578 |
|
---|
[a1a101d] | 579 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_seq(%p, %p)", conn, seg);
|
---|
[c5808b41] | 580 |
|
---|
[7cf7ded] | 581 | /* Discard unacceptable segments ("old duplicates") */
|
---|
| 582 | if (!seq_no_segment_acceptable(conn, seg)) {
|
---|
[a1a101d] | 583 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Replying ACK to unacceptable segment.");
|
---|
[7cf7ded] | 584 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 585 | tcp_segment_delete(seg);
|
---|
| 586 | return;
|
---|
| 587 | }
|
---|
[c5808b41] | 588 |
|
---|
| 589 | /* Queue for processing */
|
---|
| 590 | tcp_iqueue_insert_seg(&conn->incoming, seg);
|
---|
| 591 |
|
---|
| 592 | /*
|
---|
| 593 | * Process all segments from incoming queue that are ready.
|
---|
| 594 | * Unacceptable segments are discarded by tcp_iqueue_get_ready_seg().
|
---|
| 595 | *
|
---|
| 596 | * XXX Need to return ACK for unacceptable segments
|
---|
| 597 | */
|
---|
| 598 | while (tcp_iqueue_get_ready_seg(&conn->incoming, &pseg) == EOK)
|
---|
| 599 | tcp_conn_seg_process(conn, pseg);
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[032bbe7] | 602 | /** Process segment RST field.
|
---|
| 603 | *
|
---|
| 604 | * @param conn Connection
|
---|
| 605 | * @param seg Segment
|
---|
| 606 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 607 | * if not
|
---|
| 608 | */
|
---|
[c5808b41] | 609 | static cproc_t tcp_conn_seg_proc_rst(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 610 | {
|
---|
[03be171] | 611 | if ((seg->ctrl & CTL_RST) == 0)
|
---|
| 612 | return cp_continue;
|
---|
| 613 |
|
---|
[d9e14fa4] | 614 | switch (conn->cstate) {
|
---|
| 615 | case st_syn_received:
|
---|
| 616 | /* XXX In case of passive open, revert to Listen state */
|
---|
| 617 | if (conn->ap == ap_passive) {
|
---|
| 618 | tcp_conn_state_set(conn, st_listen);
|
---|
| 619 | /* XXX Revert conn->ident */
|
---|
| 620 | tcp_conn_tw_timer_clear(conn);
|
---|
| 621 | tcp_tqueue_clear(&conn->retransmit);
|
---|
| 622 | } else {
|
---|
| 623 | tcp_conn_reset(conn);
|
---|
| 624 | }
|
---|
| 625 | break;
|
---|
| 626 | case st_established:
|
---|
| 627 | case st_fin_wait_1:
|
---|
| 628 | case st_fin_wait_2:
|
---|
| 629 | case st_close_wait:
|
---|
| 630 | /* General "connection reset" signal */
|
---|
| 631 | tcp_reset_signal(conn);
|
---|
| 632 | tcp_conn_reset(conn);
|
---|
| 633 | break;
|
---|
| 634 | case st_closing:
|
---|
| 635 | case st_last_ack:
|
---|
| 636 | case st_time_wait:
|
---|
| 637 | tcp_conn_reset(conn);
|
---|
| 638 | break;
|
---|
| 639 | case st_listen:
|
---|
| 640 | case st_syn_sent:
|
---|
| 641 | case st_closed:
|
---|
| 642 | assert(false);
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | return cp_done;
|
---|
[c5808b41] | 646 | }
|
---|
| 647 |
|
---|
[032bbe7] | 648 | /** Process segment security and precedence fields.
|
---|
| 649 | *
|
---|
| 650 | * @param conn Connection
|
---|
| 651 | * @param seg Segment
|
---|
| 652 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 653 | * if not
|
---|
| 654 | */
|
---|
[c5808b41] | 655 | static cproc_t tcp_conn_seg_proc_sp(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 656 | {
|
---|
[4c55a64] | 657 | /* TODO */
|
---|
[c5808b41] | 658 | return cp_continue;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
[032bbe7] | 661 | /** Process segment SYN field.
|
---|
| 662 | *
|
---|
| 663 | * @param conn Connection
|
---|
| 664 | * @param seg Segment
|
---|
| 665 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 666 | * if not
|
---|
| 667 | */
|
---|
[c5808b41] | 668 | static cproc_t tcp_conn_seg_proc_syn(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 669 | {
|
---|
[7cf7ded] | 670 | if ((seg->ctrl & CTL_SYN) == 0)
|
---|
| 671 | return cp_continue;
|
---|
| 672 |
|
---|
| 673 | /*
|
---|
| 674 | * Assert SYN is in receive window, otherwise this step should not
|
---|
| 675 | * be reached.
|
---|
| 676 | */
|
---|
| 677 | assert(seq_no_in_rcv_wnd(conn, seg->seq));
|
---|
| 678 |
|
---|
[a1a101d] | 679 | log_msg(LOG_DEFAULT, LVL_WARN, "SYN is in receive window, should send reset. XXX");
|
---|
[7cf7ded] | 680 |
|
---|
| 681 | /*
|
---|
| 682 | * TODO
|
---|
| 683 | *
|
---|
| 684 | * Send a reset, resond "reset" to all outstanding RECEIVEs and SEND,
|
---|
| 685 | * flush segment queues. Send unsolicited "connection reset" signal
|
---|
| 686 | * to user, connection -> closed state, delete TCB, return.
|
---|
| 687 | */
|
---|
| 688 | return cp_done;
|
---|
[c5808b41] | 689 | }
|
---|
| 690 |
|
---|
[032bbe7] | 691 | /** Process segment ACK field in Syn-Received state.
|
---|
| 692 | *
|
---|
| 693 | * @param conn Connection
|
---|
| 694 | * @param seg Segment
|
---|
| 695 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 696 | * if not
|
---|
| 697 | */
|
---|
[c5808b41] | 698 | static cproc_t tcp_conn_seg_proc_ack_sr(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 699 | {
|
---|
| 700 | if (!seq_no_ack_acceptable(conn, seg->ack)) {
|
---|
| 701 | /* ACK is not acceptable, send RST. */
|
---|
[a1a101d] | 702 | log_msg(LOG_DEFAULT, LVL_WARN, "Segment ACK not acceptable, sending RST.");
|
---|
[c5808b41] | 703 | tcp_reply_rst(&conn->ident, seg);
|
---|
[4c55a64] | 704 | tcp_segment_delete(seg);
|
---|
| 705 | return cp_done;
|
---|
[c5808b41] | 706 | }
|
---|
| 707 |
|
---|
[a1a101d] | 708 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: SYN ACKed -> Established", conn->name);
|
---|
[c5808b41] | 709 |
|
---|
[004a5fe] | 710 | tcp_conn_state_set(conn, st_established);
|
---|
[c5808b41] | 711 |
|
---|
| 712 | /* XXX Not mentioned in spec?! */
|
---|
| 713 | conn->snd_una = seg->ack;
|
---|
| 714 |
|
---|
| 715 | return cp_continue;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[032bbe7] | 718 | /** Process segment ACK field in Established state.
|
---|
| 719 | *
|
---|
| 720 | * @param conn Connection
|
---|
| 721 | * @param seg Segment
|
---|
| 722 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 723 | * if not
|
---|
| 724 | */
|
---|
[c5808b41] | 725 | static cproc_t tcp_conn_seg_proc_ack_est(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 726 | {
|
---|
[a1a101d] | 727 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_proc_ack_est(%p, %p)", conn, seg);
|
---|
[0093ab6] | 728 |
|
---|
[a1a101d] | 729 | log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.ACK=%u, SND.UNA=%u, SND.NXT=%u",
|
---|
[0093ab6] | 730 | (unsigned)seg->ack, (unsigned)conn->snd_una,
|
---|
| 731 | (unsigned)conn->snd_nxt);
|
---|
| 732 |
|
---|
[4c55a64] | 733 | if (!seq_no_ack_acceptable(conn, seg->ack)) {
|
---|
[a1a101d] | 734 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ACK not acceptable.");
|
---|
[4c55a64] | 735 | if (!seq_no_ack_duplicate(conn, seg->ack)) {
|
---|
[a1a101d] | 736 | log_msg(LOG_DEFAULT, LVL_WARN, "Not acceptable, not duplicate. "
|
---|
[0093ab6] | 737 | "Send ACK and drop.");
|
---|
[4c55a64] | 738 | /* Not acceptable, not duplicate. Send ACK and drop. */
|
---|
| 739 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 740 | tcp_segment_delete(seg);
|
---|
| 741 | return cp_done;
|
---|
[0093ab6] | 742 | } else {
|
---|
[a1a101d] | 743 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring duplicate ACK.");
|
---|
[4c55a64] | 744 | }
|
---|
| 745 | } else {
|
---|
| 746 | /* Update SND.UNA */
|
---|
| 747 | conn->snd_una = seg->ack;
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | if (seq_no_new_wnd_update(conn, seg)) {
|
---|
| 751 | conn->snd_wnd = seg->wnd;
|
---|
| 752 | conn->snd_wl1 = seg->seq;
|
---|
| 753 | conn->snd_wl2 = seg->ack;
|
---|
[d9ce049] | 754 |
|
---|
[a1a101d] | 755 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Updating send window, SND.WND=%" PRIu32
|
---|
[d9ce049] | 756 | ", SND.WL1=%" PRIu32 ", SND.WL2=%" PRIu32,
|
---|
| 757 | conn->snd_wnd, conn->snd_wl1, conn->snd_wl2);
|
---|
[4c55a64] | 758 | }
|
---|
| 759 |
|
---|
[d9ce049] | 760 | /*
|
---|
| 761 | * Prune acked segments from retransmission queue and
|
---|
| 762 | * possibly transmit more data.
|
---|
| 763 | */
|
---|
| 764 | tcp_tqueue_ack_received(conn);
|
---|
| 765 |
|
---|
[c5808b41] | 766 | return cp_continue;
|
---|
| 767 | }
|
---|
| 768 |
|
---|
[032bbe7] | 769 | /** Process segment ACK field in Fin-Wait-1 state.
|
---|
| 770 | *
|
---|
| 771 | * @param conn Connection
|
---|
| 772 | * @param seg Segment
|
---|
| 773 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 774 | * if not
|
---|
| 775 | */
|
---|
[c5808b41] | 776 | static cproc_t tcp_conn_seg_proc_ack_fw1(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 777 | {
|
---|
[4c55a64] | 778 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 779 | return cp_done;
|
---|
| 780 |
|
---|
[6df418c4] | 781 | if (conn->fin_is_acked) {
|
---|
[a1a101d] | 782 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Fin-Wait-2", conn->name);
|
---|
[004a5fe] | 783 | tcp_conn_state_set(conn, st_fin_wait_2);
|
---|
[6df418c4] | 784 | }
|
---|
| 785 |
|
---|
[c5808b41] | 786 | return cp_continue;
|
---|
| 787 | }
|
---|
| 788 |
|
---|
[032bbe7] | 789 | /** Process segment ACK field in Fin-Wait-2 state.
|
---|
| 790 | *
|
---|
| 791 | * @param conn Connection
|
---|
| 792 | * @param seg Segment
|
---|
| 793 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 794 | * if not
|
---|
| 795 | */
|
---|
[c5808b41] | 796 | static cproc_t tcp_conn_seg_proc_ack_fw2(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 797 | {
|
---|
[4c55a64] | 798 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 799 | return cp_done;
|
---|
| 800 |
|
---|
| 801 | /* TODO */
|
---|
[c5808b41] | 802 | return cp_continue;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[032bbe7] | 805 | /** Process segment ACK field in Close-Wait 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 | */
|
---|
[c5808b41] | 812 | static cproc_t tcp_conn_seg_proc_ack_cw(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 813 | {
|
---|
[4c55a64] | 814 | /* The same processing as in Established state */
|
---|
| 815 | return tcp_conn_seg_proc_ack_est(conn, seg);
|
---|
[c5808b41] | 816 | }
|
---|
| 817 |
|
---|
[032bbe7] | 818 | /** Process segment ACK field in Closing 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 | */
|
---|
[c5808b41] | 825 | static cproc_t tcp_conn_seg_proc_ack_cls(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 826 | {
|
---|
[4c55a64] | 827 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 828 | return cp_done;
|
---|
| 829 |
|
---|
| 830 | /* TODO */
|
---|
[c5808b41] | 831 | return cp_continue;
|
---|
| 832 | }
|
---|
| 833 |
|
---|
[032bbe7] | 834 | /** Process segment ACK field in Last-Ack state.
|
---|
| 835 | *
|
---|
| 836 | * @param conn Connection
|
---|
| 837 | * @param seg Segment
|
---|
| 838 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 839 | * if not
|
---|
| 840 | */
|
---|
[c5808b41] | 841 | static cproc_t tcp_conn_seg_proc_ack_la(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 842 | {
|
---|
[4c55a64] | 843 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 844 | return cp_done;
|
---|
| 845 |
|
---|
[6e88fea] | 846 | if (conn->fin_is_acked) {
|
---|
[a1a101d] | 847 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
|
---|
[6e88fea] | 848 | tcp_conn_remove(conn);
|
---|
[004a5fe] | 849 | tcp_conn_state_set(conn, st_closed);
|
---|
[6e88fea] | 850 | return cp_done;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
[c5808b41] | 853 | return cp_continue;
|
---|
| 854 | }
|
---|
| 855 |
|
---|
[032bbe7] | 856 | /** Process segment ACK field in Time-Wait 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 | */
|
---|
[c5808b41] | 863 | static cproc_t tcp_conn_seg_proc_ack_tw(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 864 | {
|
---|
[4c55a64] | 865 | /* Nothing to do */
|
---|
[c5808b41] | 866 | return cp_continue;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
[032bbe7] | 869 | /** Process segment ACK field.
|
---|
| 870 | *
|
---|
| 871 | * @param conn Connection
|
---|
| 872 | * @param seg Segment
|
---|
| 873 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 874 | * if not
|
---|
| 875 | */
|
---|
[c5808b41] | 876 | static cproc_t tcp_conn_seg_proc_ack(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 877 | {
|
---|
[a1a101d] | 878 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_ack(%p, %p)",
|
---|
[23fe06c] | 879 | conn->name, conn, seg);
|
---|
[c5808b41] | 880 |
|
---|
| 881 | if ((seg->ctrl & CTL_ACK) == 0) {
|
---|
[a1a101d] | 882 | log_msg(LOG_DEFAULT, LVL_WARN, "Segment has no ACK. Dropping.");
|
---|
[4c55a64] | 883 | tcp_segment_delete(seg);
|
---|
[c5808b41] | 884 | return cp_done;
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | switch (conn->cstate) {
|
---|
| 888 | case st_syn_received:
|
---|
| 889 | return tcp_conn_seg_proc_ack_sr(conn, seg);
|
---|
| 890 | case st_established:
|
---|
| 891 | return tcp_conn_seg_proc_ack_est(conn, seg);
|
---|
| 892 | case st_fin_wait_1:
|
---|
| 893 | return tcp_conn_seg_proc_ack_fw1(conn, seg);
|
---|
| 894 | case st_fin_wait_2:
|
---|
| 895 | return tcp_conn_seg_proc_ack_fw2(conn, seg);
|
---|
| 896 | case st_close_wait:
|
---|
| 897 | return tcp_conn_seg_proc_ack_cw(conn, seg);
|
---|
| 898 | case st_closing:
|
---|
| 899 | return tcp_conn_seg_proc_ack_cls(conn, seg);
|
---|
| 900 | case st_last_ack:
|
---|
| 901 | return tcp_conn_seg_proc_ack_la(conn, seg);
|
---|
| 902 | case st_time_wait:
|
---|
| 903 | return tcp_conn_seg_proc_ack_tw(conn, seg);
|
---|
| 904 | case st_listen:
|
---|
| 905 | case st_syn_sent:
|
---|
| 906 | case st_closed:
|
---|
| 907 | assert(false);
|
---|
| 908 | }
|
---|
| 909 |
|
---|
| 910 | assert(false);
|
---|
| 911 | }
|
---|
| 912 |
|
---|
[032bbe7] | 913 | /** Process segment URG field.
|
---|
| 914 | *
|
---|
| 915 | * @param conn Connection
|
---|
| 916 | * @param seg Segment
|
---|
| 917 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 918 | * if not
|
---|
| 919 | */
|
---|
[c5808b41] | 920 | static cproc_t tcp_conn_seg_proc_urg(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 921 | {
|
---|
| 922 | return cp_continue;
|
---|
| 923 | }
|
---|
| 924 |
|
---|
[032bbe7] | 925 | /** Process segment text.
|
---|
| 926 | *
|
---|
| 927 | * @param conn Connection
|
---|
| 928 | * @param seg Segment
|
---|
| 929 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 930 | * if not
|
---|
| 931 | */
|
---|
[c5808b41] | 932 | static cproc_t tcp_conn_seg_proc_text(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 933 | {
|
---|
[0093ab6] | 934 | size_t text_size;
|
---|
| 935 | size_t xfer_size;
|
---|
| 936 |
|
---|
[a1a101d] | 937 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_text(%p, %p)",
|
---|
[23fe06c] | 938 | conn->name, conn, seg);
|
---|
[0093ab6] | 939 |
|
---|
[4c55a64] | 940 | switch (conn->cstate) {
|
---|
| 941 | case st_established:
|
---|
| 942 | case st_fin_wait_1:
|
---|
| 943 | case st_fin_wait_2:
|
---|
| 944 | /* OK */
|
---|
| 945 | break;
|
---|
| 946 | case st_close_wait:
|
---|
| 947 | case st_closing:
|
---|
| 948 | case st_last_ack:
|
---|
| 949 | case st_time_wait:
|
---|
| 950 | /* Invalid since FIN has been received. Ignore text. */
|
---|
| 951 | return cp_continue;
|
---|
| 952 | case st_listen:
|
---|
| 953 | case st_syn_sent:
|
---|
| 954 | case st_syn_received:
|
---|
| 955 | case st_closed:
|
---|
| 956 | assert(false);
|
---|
| 957 | }
|
---|
| 958 |
|
---|
[0093ab6] | 959 | /*
|
---|
| 960 | * Process segment text
|
---|
| 961 | */
|
---|
| 962 | assert(seq_no_segment_ready(conn, seg));
|
---|
| 963 |
|
---|
| 964 | /* Trim anything outside our receive window */
|
---|
| 965 | tcp_conn_trim_seg_to_wnd(conn, seg);
|
---|
| 966 |
|
---|
| 967 | /* Determine how many bytes to copy */
|
---|
| 968 | text_size = tcp_segment_text_size(seg);
|
---|
| 969 | xfer_size = min(text_size, conn->rcv_buf_size - conn->rcv_buf_used);
|
---|
| 970 |
|
---|
| 971 | /* Copy data to receive buffer */
|
---|
[d9ce049] | 972 | tcp_segment_text_copy(seg, conn->rcv_buf + conn->rcv_buf_used,
|
---|
| 973 | xfer_size);
|
---|
| 974 | conn->rcv_buf_used += xfer_size;
|
---|
| 975 |
|
---|
| 976 | /* Signal to the receive function that new data has arrived */
|
---|
| 977 | fibril_condvar_broadcast(&conn->rcv_buf_cv);
|
---|
[0093ab6] | 978 |
|
---|
[a1a101d] | 979 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
|
---|
[32105348] | 980 |
|
---|
[0093ab6] | 981 | /* Advance RCV.NXT */
|
---|
| 982 | conn->rcv_nxt += xfer_size;
|
---|
| 983 |
|
---|
| 984 | /* Update receive window. XXX Not an efficient strategy. */
|
---|
| 985 | conn->rcv_wnd -= xfer_size;
|
---|
| 986 |
|
---|
| 987 | /* Send ACK */
|
---|
| 988 | if (xfer_size > 0)
|
---|
| 989 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 990 |
|
---|
| 991 | if (xfer_size < seg->len) {
|
---|
[8c7a054] | 992 | /* Trim part of segment which we just received */
|
---|
[0093ab6] | 993 | tcp_conn_trim_seg_to_wnd(conn, seg);
|
---|
[8c7a054] | 994 | } else {
|
---|
[a1a101d] | 995 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Nothing left in segment, dropping "
|
---|
[f0a2720] | 996 | "(xfer_size=%zu, SEG.LEN=%" PRIu32 ", seg->ctrl=%u)",
|
---|
| 997 | conn->name, xfer_size, seg->len, (unsigned int) seg->ctrl);
|
---|
[8c7a054] | 998 | /* Nothing left in segment */
|
---|
| 999 | tcp_segment_delete(seg);
|
---|
[0093ab6] | 1000 | return cp_done;
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
[c5808b41] | 1003 | return cp_continue;
|
---|
| 1004 | }
|
---|
| 1005 |
|
---|
[032bbe7] | 1006 | /** Process segment FIN field.
|
---|
| 1007 | *
|
---|
| 1008 | * @param conn Connection
|
---|
| 1009 | * @param seg Segment
|
---|
| 1010 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 1011 | * if not
|
---|
| 1012 | */
|
---|
[c5808b41] | 1013 | static cproc_t tcp_conn_seg_proc_fin(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 1014 | {
|
---|
[a1a101d] | 1015 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_fin(%p, %p)",
|
---|
[23fe06c] | 1016 | conn->name, conn, seg);
|
---|
[a1a101d] | 1017 | log_msg(LOG_DEFAULT, LVL_DEBUG, " seg->len=%zu, seg->ctl=%u", (size_t) seg->len,
|
---|
[7cf7ded] | 1018 | (unsigned) seg->ctrl);
|
---|
[8c7a054] | 1019 |
|
---|
| 1020 | /* Only process FIN if no text is left in segment. */
|
---|
| 1021 | if (tcp_segment_text_size(seg) == 0 && (seg->ctrl & CTL_FIN) != 0) {
|
---|
[a1a101d] | 1022 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - FIN found in segment.");
|
---|
[8c7a054] | 1023 |
|
---|
[6e88fea] | 1024 | /* Send ACK */
|
---|
| 1025 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 1026 |
|
---|
[8c7a054] | 1027 | conn->rcv_nxt++;
|
---|
| 1028 | conn->rcv_wnd--;
|
---|
| 1029 |
|
---|
[f343a16] | 1030 | /* Change connection state */
|
---|
| 1031 | switch (conn->cstate) {
|
---|
| 1032 | case st_listen:
|
---|
| 1033 | case st_syn_sent:
|
---|
| 1034 | case st_closed:
|
---|
| 1035 | /* Connection not synchronized */
|
---|
| 1036 | assert(false);
|
---|
| 1037 | case st_syn_received:
|
---|
| 1038 | case st_established:
|
---|
[a1a101d] | 1039 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Close-Wait",
|
---|
[6896409c] | 1040 | conn->name);
|
---|
[004a5fe] | 1041 | tcp_conn_state_set(conn, st_close_wait);
|
---|
[f343a16] | 1042 | break;
|
---|
| 1043 | case st_fin_wait_1:
|
---|
[a1a101d] | 1044 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Closing",
|
---|
[6896409c] | 1045 | conn->name);
|
---|
[004a5fe] | 1046 | tcp_conn_state_set(conn, st_closing);
|
---|
[f343a16] | 1047 | break;
|
---|
| 1048 | case st_fin_wait_2:
|
---|
[a1a101d] | 1049 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Time-Wait",
|
---|
[6896409c] | 1050 | conn->name);
|
---|
[004a5fe] | 1051 | tcp_conn_state_set(conn, st_time_wait);
|
---|
[2a3214e] | 1052 | /* Start the Time-Wait timer */
|
---|
| 1053 | tcp_conn_tw_timer_set(conn);
|
---|
[f343a16] | 1054 | break;
|
---|
| 1055 | case st_close_wait:
|
---|
| 1056 | case st_closing:
|
---|
| 1057 | case st_last_ack:
|
---|
| 1058 | /* Do nothing */
|
---|
| 1059 | break;
|
---|
| 1060 | case st_time_wait:
|
---|
[2a3214e] | 1061 | /* Restart the Time-Wait timer */
|
---|
| 1062 | tcp_conn_tw_timer_set(conn);
|
---|
[f343a16] | 1063 | break;
|
---|
| 1064 | }
|
---|
[8c7a054] | 1065 |
|
---|
| 1066 | /* Add FIN to the receive buffer */
|
---|
| 1067 | conn->rcv_buf_fin = true;
|
---|
| 1068 | fibril_condvar_broadcast(&conn->rcv_buf_cv);
|
---|
| 1069 |
|
---|
| 1070 | tcp_segment_delete(seg);
|
---|
| 1071 | return cp_done;
|
---|
| 1072 | }
|
---|
| 1073 |
|
---|
[c5808b41] | 1074 | return cp_continue;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | /** Process incoming segment.
|
---|
| 1078 | *
|
---|
| 1079 | * We are in connection state where segments are processed in order
|
---|
| 1080 | * of sequence number. This processes one segment taken from the
|
---|
| 1081 | * connection incoming segments queue.
|
---|
[032bbe7] | 1082 | *
|
---|
| 1083 | * @param conn Connection
|
---|
| 1084 | * @param seg Segment
|
---|
[c5808b41] | 1085 | */
|
---|
| 1086 | static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 1087 | {
|
---|
[a1a101d] | 1088 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_process(%p, %p)", conn, seg);
|
---|
[6896409c] | 1089 | tcp_segment_dump(seg);
|
---|
[c5808b41] | 1090 |
|
---|
| 1091 | /* Check whether segment is acceptable */
|
---|
| 1092 | /* XXX Permit valid ACKs, URGs and RSTs */
|
---|
| 1093 | /* if (!seq_no_segment_acceptable(conn, seg)) {
|
---|
[a1a101d] | 1094 | log_msg(LOG_DEFAULT, LVL_WARN, "Segment not acceptable, dropping.");
|
---|
[c5808b41] | 1095 | if ((seg->ctrl & CTL_RST) == 0) {
|
---|
| 1096 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 1097 | }
|
---|
| 1098 | return;
|
---|
| 1099 | }
|
---|
| 1100 | */
|
---|
| 1101 |
|
---|
| 1102 | if (tcp_conn_seg_proc_rst(conn, seg) == cp_done)
|
---|
| 1103 | return;
|
---|
| 1104 |
|
---|
| 1105 | if (tcp_conn_seg_proc_sp(conn, seg) == cp_done)
|
---|
| 1106 | return;
|
---|
| 1107 |
|
---|
| 1108 | if (tcp_conn_seg_proc_syn(conn, seg) == cp_done)
|
---|
| 1109 | return;
|
---|
| 1110 |
|
---|
| 1111 | if (tcp_conn_seg_proc_ack(conn, seg) == cp_done)
|
---|
| 1112 | return;
|
---|
| 1113 |
|
---|
| 1114 | if (tcp_conn_seg_proc_urg(conn, seg) == cp_done)
|
---|
| 1115 | return;
|
---|
| 1116 |
|
---|
| 1117 | if (tcp_conn_seg_proc_text(conn, seg) == cp_done)
|
---|
| 1118 | return;
|
---|
| 1119 |
|
---|
| 1120 | if (tcp_conn_seg_proc_fin(conn, seg) == cp_done)
|
---|
| 1121 | return;
|
---|
[0093ab6] | 1122 |
|
---|
[8c7a054] | 1123 | /*
|
---|
| 1124 | * If anything is left from the segment, insert it back into the
|
---|
| 1125 | * incoming segments queue.
|
---|
| 1126 | */
|
---|
[7cf7ded] | 1127 | if (seg->len > 0) {
|
---|
[a1a101d] | 1128 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Re-insert segment %p. seg->len=%zu",
|
---|
[7cf7ded] | 1129 | seg, (size_t) seg->len);
|
---|
[8c7a054] | 1130 | tcp_iqueue_insert_seg(&conn->incoming, seg);
|
---|
[7cf7ded] | 1131 | } else {
|
---|
[8c7a054] | 1132 | tcp_segment_delete(seg);
|
---|
[7cf7ded] | 1133 | }
|
---|
[c5808b41] | 1134 | }
|
---|
| 1135 |
|
---|
[032bbe7] | 1136 | /** Segment arrived on a connection.
|
---|
| 1137 | *
|
---|
| 1138 | * @param conn Connection
|
---|
| 1139 | * @param seg Segment
|
---|
| 1140 | */
|
---|
[c5808b41] | 1141 | void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 1142 | {
|
---|
[70253688] | 1143 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
|
---|
[23fe06c] | 1144 | conn->name, seg);
|
---|
[c5808b41] | 1145 |
|
---|
| 1146 | switch (conn->cstate) {
|
---|
| 1147 | case st_listen:
|
---|
| 1148 | tcp_conn_sa_listen(conn, seg); break;
|
---|
| 1149 | case st_syn_sent:
|
---|
| 1150 | tcp_conn_sa_syn_sent(conn, seg); break;
|
---|
| 1151 | case st_syn_received:
|
---|
| 1152 | case st_established:
|
---|
| 1153 | case st_fin_wait_1:
|
---|
| 1154 | case st_fin_wait_2:
|
---|
| 1155 | case st_close_wait:
|
---|
| 1156 | case st_closing:
|
---|
| 1157 | case st_last_ack:
|
---|
| 1158 | case st_time_wait:
|
---|
| 1159 | /* Process segments in order of sequence number */
|
---|
| 1160 | tcp_conn_sa_queue(conn, seg); break;
|
---|
| 1161 | case st_closed:
|
---|
[a1a101d] | 1162 | log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
|
---|
[c5808b41] | 1163 | assert(false);
|
---|
| 1164 | }
|
---|
| 1165 | }
|
---|
| 1166 |
|
---|
[2a3214e] | 1167 | /** Time-Wait timeout handler.
|
---|
| 1168 | *
|
---|
| 1169 | * @param arg Connection
|
---|
| 1170 | */
|
---|
| 1171 | static void tw_timeout_func(void *arg)
|
---|
| 1172 | {
|
---|
| 1173 | tcp_conn_t *conn = (tcp_conn_t *) arg;
|
---|
| 1174 |
|
---|
[a1a101d] | 1175 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p)", conn);
|
---|
[2a3214e] | 1176 |
|
---|
[0edaf0f6] | 1177 | fibril_mutex_lock(&conn->lock);
|
---|
| 1178 |
|
---|
[704586fb] | 1179 | if (conn->cstate == st_closed) {
|
---|
[a1a101d] | 1180 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection already closed.");
|
---|
[0edaf0f6] | 1181 | fibril_mutex_unlock(&conn->lock);
|
---|
| 1182 | tcp_conn_delref(conn);
|
---|
[704586fb] | 1183 | return;
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
[a1a101d] | 1186 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
|
---|
[2a3214e] | 1187 | tcp_conn_remove(conn);
|
---|
[004a5fe] | 1188 | tcp_conn_state_set(conn, st_closed);
|
---|
[0edaf0f6] | 1189 |
|
---|
| 1190 | fibril_mutex_unlock(&conn->lock);
|
---|
| 1191 | tcp_conn_delref(conn);
|
---|
[2a3214e] | 1192 | }
|
---|
| 1193 |
|
---|
| 1194 | /** Start or restart the Time-Wait timeout.
|
---|
| 1195 | *
|
---|
| 1196 | * @param conn Connection
|
---|
| 1197 | */
|
---|
| 1198 | void tcp_conn_tw_timer_set(tcp_conn_t *conn)
|
---|
| 1199 | {
|
---|
[0edaf0f6] | 1200 | tcp_conn_addref(conn);
|
---|
[2a3214e] | 1201 | fibril_timer_set(conn->tw_timer, TIME_WAIT_TIMEOUT, tw_timeout_func,
|
---|
| 1202 | (void *)conn);
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
[704586fb] | 1205 | /** Clear the Time-Wait timeout.
|
---|
| 1206 | *
|
---|
| 1207 | * @param conn Connection
|
---|
| 1208 | */
|
---|
| 1209 | void tcp_conn_tw_timer_clear(tcp_conn_t *conn)
|
---|
| 1210 | {
|
---|
[0edaf0f6] | 1211 | if (fibril_timer_clear(conn->tw_timer) == fts_active)
|
---|
| 1212 | tcp_conn_delref(conn);
|
---|
[704586fb] | 1213 | }
|
---|
| 1214 |
|
---|
[032bbe7] | 1215 | /** Trim segment to the receive window.
|
---|
| 1216 | *
|
---|
| 1217 | * @param conn Connection
|
---|
| 1218 | * @param seg Segment
|
---|
| 1219 | */
|
---|
[0093ab6] | 1220 | void tcp_conn_trim_seg_to_wnd(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 1221 | {
|
---|
| 1222 | uint32_t left, right;
|
---|
| 1223 |
|
---|
| 1224 | seq_no_seg_trim_calc(conn, seg, &left, &right);
|
---|
| 1225 | tcp_segment_trim(seg, left, right);
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
[032bbe7] | 1228 | /** Handle unexpected segment received on a socket pair.
|
---|
| 1229 | *
|
---|
| 1230 | * We reply with an RST unless the received segment has RST.
|
---|
| 1231 | *
|
---|
| 1232 | * @param sp Socket pair which received the segment
|
---|
| 1233 | * @param seg Unexpected segment
|
---|
| 1234 | */
|
---|
[c5808b41] | 1235 | void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 1236 | {
|
---|
[a1a101d] | 1237 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
|
---|
[c5808b41] | 1238 |
|
---|
| 1239 | if ((seg->ctrl & CTL_RST) == 0)
|
---|
| 1240 | tcp_reply_rst(sp, seg);
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
[032bbe7] | 1243 | /** Compute flipped socket pair for response.
|
---|
| 1244 | *
|
---|
[32105348] | 1245 | * Flipped socket pair has local and foreign sockets exchanged.
|
---|
[032bbe7] | 1246 | *
|
---|
| 1247 | * @param sp Socket pair
|
---|
| 1248 | * @param fsp Place to store flipped socket pair
|
---|
| 1249 | */
|
---|
[c5808b41] | 1250 | void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
|
---|
| 1251 | {
|
---|
| 1252 | fsp->local = sp->foreign;
|
---|
| 1253 | fsp->foreign = sp->local;
|
---|
| 1254 | }
|
---|
| 1255 |
|
---|
[032bbe7] | 1256 | /** Send RST in response to an incoming segment.
|
---|
| 1257 | *
|
---|
| 1258 | * @param sp Socket pair which received the segment
|
---|
| 1259 | * @param seg Incoming segment
|
---|
| 1260 | */
|
---|
[c5808b41] | 1261 | void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 1262 | {
|
---|
| 1263 | tcp_segment_t *rseg;
|
---|
| 1264 |
|
---|
[a1a101d] | 1265 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
|
---|
[c5808b41] | 1266 |
|
---|
| 1267 | rseg = tcp_segment_make_rst(seg);
|
---|
[704586fb] | 1268 | tcp_transmit_segment(sp, rseg);
|
---|
[c5808b41] | 1269 | }
|
---|
| 1270 |
|
---|
| 1271 | /**
|
---|
| 1272 | * @}
|
---|
| 1273 | */
|
---|