[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>
|
---|
[8c7a054] | 38 | #include <bool.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 |
|
---|
[8218b6b] | 51 | #define RCV_BUF_SIZE 2/*4096*/
|
---|
[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);
|
---|
| 58 |
|
---|
| 59 | static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg);
|
---|
[2a3214e] | 60 | static void tcp_conn_tw_timer_set(tcp_conn_t *conn);
|
---|
[704586fb] | 61 | static void tcp_conn_tw_timer_clear(tcp_conn_t *conn);
|
---|
[c5808b41] | 62 |
|
---|
[032bbe7] | 63 | /** Create new segment structure.
|
---|
| 64 | *
|
---|
| 65 | * @param lsock Local socket (will be deeply copied)
|
---|
| 66 | * @param fsock Foreign socket (will be deeply copied)
|
---|
| 67 | * @return New segment or NULL
|
---|
| 68 | */
|
---|
[c5808b41] | 69 | tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
|
---|
| 70 | {
|
---|
[2a3214e] | 71 | tcp_conn_t *conn = NULL;
|
---|
[7cf7ded] | 72 | bool tqueue_inited = false;
|
---|
[c5808b41] | 73 |
|
---|
[0093ab6] | 74 | /* Allocate connection structure */
|
---|
[c5808b41] | 75 | conn = calloc(1, sizeof(tcp_conn_t));
|
---|
| 76 | if (conn == NULL)
|
---|
[2a3214e] | 77 | goto error;
|
---|
| 78 |
|
---|
| 79 | conn->tw_timer = fibril_timer_create();
|
---|
| 80 | if (conn->tw_timer == NULL)
|
---|
| 81 | goto error;
|
---|
[c5808b41] | 82 |
|
---|
[0093ab6] | 83 | /* Allocate receive buffer */
|
---|
[d9ce049] | 84 | fibril_mutex_initialize(&conn->rcv_buf_lock);
|
---|
| 85 | fibril_condvar_initialize(&conn->rcv_buf_cv);
|
---|
[0093ab6] | 86 | conn->rcv_buf_size = RCV_BUF_SIZE;
|
---|
| 87 | conn->rcv_buf_used = 0;
|
---|
[8c7a054] | 88 | conn->rcv_buf_fin = false;
|
---|
[d9ce049] | 89 |
|
---|
[0093ab6] | 90 | conn->rcv_buf = calloc(1, conn->rcv_buf_size);
|
---|
[2a3214e] | 91 | if (conn->rcv_buf == NULL)
|
---|
| 92 | goto error;
|
---|
[0093ab6] | 93 |
|
---|
[32105348] | 94 | /** Allocate send buffer */
|
---|
| 95 | conn->snd_buf_size = SND_BUF_SIZE;
|
---|
| 96 | conn->snd_buf_used = 0;
|
---|
[8c7a054] | 97 | conn->snd_buf_fin = false;
|
---|
[32105348] | 98 | conn->snd_buf = calloc(1, conn->snd_buf_size);
|
---|
[2a3214e] | 99 | if (conn->snd_buf == NULL)
|
---|
| 100 | goto error;
|
---|
[32105348] | 101 |
|
---|
[0093ab6] | 102 | /* Set up receive window. */
|
---|
| 103 | conn->rcv_wnd = conn->rcv_buf_size;
|
---|
| 104 |
|
---|
| 105 | /* Initialize incoming segment queue */
|
---|
[c5808b41] | 106 | tcp_iqueue_init(&conn->incoming, conn);
|
---|
| 107 |
|
---|
[6df418c4] | 108 | /* Initialize retransmission queue */
|
---|
[7cf7ded] | 109 | if (tcp_tqueue_init(&conn->retransmit, conn) != EOK)
|
---|
| 110 | goto error;
|
---|
| 111 |
|
---|
| 112 | tqueue_inited = true;
|
---|
[6df418c4] | 113 |
|
---|
[004a5fe] | 114 | /* Connection state change signalling */
|
---|
| 115 | fibril_mutex_initialize(&conn->cstate_lock);
|
---|
| 116 | fibril_condvar_initialize(&conn->cstate_cv);
|
---|
| 117 |
|
---|
[c5808b41] | 118 | conn->cstate = st_listen;
|
---|
[d9e14fa4] | 119 | conn->reset = false;
|
---|
| 120 | conn->ap = ap_passive;
|
---|
[6df418c4] | 121 | conn->fin_is_acked = false;
|
---|
[c5808b41] | 122 | conn->ident.local = *lsock;
|
---|
| 123 | if (fsock != NULL)
|
---|
| 124 | conn->ident.foreign = *fsock;
|
---|
| 125 |
|
---|
| 126 | return conn;
|
---|
[2a3214e] | 127 |
|
---|
| 128 | error:
|
---|
[7cf7ded] | 129 | if (tqueue_inited)
|
---|
| 130 | tcp_tqueue_fini(&conn->retransmit);
|
---|
[2a3214e] | 131 | if (conn != NULL && conn->rcv_buf != NULL)
|
---|
| 132 | free(conn->rcv_buf);
|
---|
| 133 | if (conn != NULL && conn->snd_buf != NULL)
|
---|
| 134 | free(conn->snd_buf);
|
---|
| 135 | if (conn != NULL && conn->tw_timer != NULL)
|
---|
| 136 | fibril_timer_destroy(conn->tw_timer);
|
---|
| 137 | if (conn != NULL)
|
---|
| 138 | free(conn);
|
---|
| 139 |
|
---|
| 140 | return NULL;
|
---|
[c5808b41] | 141 | }
|
---|
| 142 |
|
---|
[032bbe7] | 143 | /** Enlist connection.
|
---|
| 144 | *
|
---|
| 145 | * Add connection to the connection map.
|
---|
| 146 | */
|
---|
[c5808b41] | 147 | void tcp_conn_add(tcp_conn_t *conn)
|
---|
| 148 | {
|
---|
| 149 | list_append(&conn->link, &conn_list);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[6e88fea] | 152 | /** Delist connection.
|
---|
| 153 | *
|
---|
| 154 | * Remove connection from the connection map.
|
---|
| 155 | */
|
---|
| 156 | void tcp_conn_remove(tcp_conn_t *conn)
|
---|
| 157 | {
|
---|
| 158 | list_remove(&conn->link);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[004a5fe] | 161 | static void tcp_conn_state_set(tcp_conn_t *conn, tcp_cstate_t nstate)
|
---|
| 162 | {
|
---|
| 163 | fibril_mutex_lock(&conn->cstate_lock);
|
---|
| 164 | conn->cstate = nstate;
|
---|
| 165 | fibril_condvar_broadcast(&conn->cstate_cv);
|
---|
| 166 | fibril_mutex_unlock(&conn->cstate_lock);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[032bbe7] | 169 | /** Synchronize connection.
|
---|
| 170 | *
|
---|
| 171 | * This is the first step of an active connection attempt,
|
---|
| 172 | * sends out SYN and sets up ISS and SND.xxx.
|
---|
| 173 | */
|
---|
[c5808b41] | 174 | void tcp_conn_sync(tcp_conn_t *conn)
|
---|
| 175 | {
|
---|
| 176 | /* XXX select ISS */
|
---|
| 177 | conn->iss = 1;
|
---|
| 178 | conn->snd_nxt = conn->iss;
|
---|
| 179 | conn->snd_una = conn->iss;
|
---|
[d9e14fa4] | 180 | conn->ap = ap_active;
|
---|
[c5808b41] | 181 |
|
---|
| 182 | tcp_tqueue_ctrl_seg(conn, CTL_SYN);
|
---|
[004a5fe] | 183 | tcp_conn_state_set(conn, st_syn_sent);
|
---|
[c5808b41] | 184 | }
|
---|
| 185 |
|
---|
[f343a16] | 186 | /** FIN has been sent.
|
---|
| 187 | *
|
---|
| 188 | * This function should be called when FIN is sent over the connection,
|
---|
| 189 | * as a result the connection state is changed appropriately.
|
---|
| 190 | */
|
---|
| 191 | void tcp_conn_fin_sent(tcp_conn_t *conn)
|
---|
| 192 | {
|
---|
| 193 | switch (conn->cstate) {
|
---|
| 194 | case st_syn_received:
|
---|
| 195 | case st_established:
|
---|
[6896409c] | 196 | log_msg(LVL_DEBUG, "%s: FIN sent -> Fin-Wait-1", conn->name);
|
---|
[004a5fe] | 197 | tcp_conn_state_set(conn, st_fin_wait_1);
|
---|
[f343a16] | 198 | break;
|
---|
| 199 | case st_close_wait:
|
---|
[6896409c] | 200 | log_msg(LVL_DEBUG, "%s: FIN sent -> Last-Ack", conn->name);
|
---|
[004a5fe] | 201 | tcp_conn_state_set(conn, st_last_ack);
|
---|
[f343a16] | 202 | break;
|
---|
| 203 | default:
|
---|
[6896409c] | 204 | log_msg(LVL_ERROR, "%s: Connection state %d", conn->name,
|
---|
| 205 | conn->cstate);
|
---|
[f343a16] | 206 | assert(false);
|
---|
| 207 | }
|
---|
[6df418c4] | 208 |
|
---|
| 209 | conn->fin_is_acked = false;
|
---|
[f343a16] | 210 | }
|
---|
| 211 |
|
---|
[032bbe7] | 212 | /** Compare two sockets.
|
---|
| 213 | *
|
---|
| 214 | * Two sockets are equal if the address is equal and the port number
|
---|
| 215 | * is equal.
|
---|
| 216 | */
|
---|
[3aa2642a] | 217 | static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt)
|
---|
[c5808b41] | 218 | {
|
---|
[3aa2642a] | 219 | log_msg(LVL_DEBUG, "tcp_socket_match(sock=(%x,%u), pat=(%x,%u))",
|
---|
| 220 | sock->addr.ipv4, sock->port, patt->addr.ipv4, patt->port);
|
---|
[c5808b41] | 221 |
|
---|
[3aa2642a] | 222 | if (patt->addr.ipv4 != TCP_IPV4_ANY &&
|
---|
| 223 | patt->addr.ipv4 != sock->addr.ipv4)
|
---|
[c5808b41] | 224 | return false;
|
---|
| 225 |
|
---|
[3aa2642a] | 226 | if (patt->port != TCP_PORT_ANY &&
|
---|
| 227 | patt->port != sock->port)
|
---|
[c5808b41] | 228 | return false;
|
---|
| 229 |
|
---|
| 230 | log_msg(LVL_DEBUG, " -> match");
|
---|
| 231 |
|
---|
| 232 | return true;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[032bbe7] | 235 | /** Match socket with pattern. */
|
---|
[c5808b41] | 236 | static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern)
|
---|
| 237 | {
|
---|
| 238 | log_msg(LVL_DEBUG, "tcp_sockpair_match(%p, %p)", sp, pattern);
|
---|
| 239 |
|
---|
[3aa2642a] | 240 | if (!tcp_socket_match(&sp->local, &pattern->local))
|
---|
[c5808b41] | 241 | return false;
|
---|
| 242 |
|
---|
[3aa2642a] | 243 | if (!tcp_socket_match(&sp->foreign, &pattern->foreign))
|
---|
[c5808b41] | 244 | return false;
|
---|
| 245 |
|
---|
| 246 | return true;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[032bbe7] | 249 | /** Find connection structure for specified socket pair.
|
---|
| 250 | *
|
---|
| 251 | * A connection is uniquely identified by a socket pair. Look up our
|
---|
| 252 | * connection map and return connection structure based on socket pair.
|
---|
| 253 | *
|
---|
| 254 | * @param sp Socket pair
|
---|
| 255 | * @return Connection structure or NULL if not found.
|
---|
| 256 | */
|
---|
[c5808b41] | 257 | tcp_conn_t *tcp_conn_find(tcp_sockpair_t *sp)
|
---|
| 258 | {
|
---|
| 259 | log_msg(LVL_DEBUG, "tcp_conn_find(%p)", sp);
|
---|
| 260 |
|
---|
| 261 | list_foreach(conn_list, link) {
|
---|
| 262 | tcp_conn_t *conn = list_get_instance(link, tcp_conn_t, link);
|
---|
[704586fb] | 263 | tcp_sockpair_t *csp = &conn->ident;
|
---|
| 264 | log_msg(LVL_DEBUG, "compare with conn (f:(%x,%u), l:(%x,%u))",
|
---|
| 265 | csp->foreign.addr.ipv4, csp->foreign.port,
|
---|
| 266 | csp->local.addr.ipv4, csp->local.port);
|
---|
| 267 | if (tcp_sockpair_match(sp, csp)) {
|
---|
[c5808b41] | 268 | return conn;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | return NULL;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[704586fb] | 275 | /** Reset connection.
|
---|
| 276 | *
|
---|
| 277 | * @param conn Connection
|
---|
| 278 | */
|
---|
| 279 | static void tcp_conn_reset(tcp_conn_t *conn)
|
---|
| 280 | {
|
---|
| 281 | log_msg(LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
|
---|
| 282 | tcp_conn_state_set(conn, st_closed);
|
---|
[d9e14fa4] | 283 | conn->reset = true;
|
---|
| 284 |
|
---|
[704586fb] | 285 | tcp_conn_tw_timer_clear(conn);
|
---|
| 286 | tcp_tqueue_clear(&conn->retransmit);
|
---|
[d9e14fa4] | 287 |
|
---|
| 288 | fibril_condvar_broadcast(&conn->rcv_buf_cv);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /** Signal to the user that connection has been reset.
|
---|
| 292 | *
|
---|
| 293 | * Send an out-of-band signal to the user.
|
---|
| 294 | */
|
---|
| 295 | static void tcp_reset_signal(tcp_conn_t *conn)
|
---|
| 296 | {
|
---|
| 297 | /* TODO */
|
---|
| 298 | log_msg(LVL_DEBUG, "%s: tcp_reset_signal()", conn->name);
|
---|
[704586fb] | 299 | }
|
---|
| 300 |
|
---|
[32105348] | 301 | /** Determine if SYN has been received.
|
---|
| 302 | *
|
---|
| 303 | * @param conn Connection
|
---|
| 304 | * @return @c true if SYN has been received, @c false otherwise.
|
---|
| 305 | */
|
---|
| 306 | bool tcp_conn_got_syn(tcp_conn_t *conn)
|
---|
| 307 | {
|
---|
| 308 | switch (conn->cstate) {
|
---|
| 309 | case st_listen:
|
---|
| 310 | case st_syn_sent:
|
---|
| 311 | return false;
|
---|
| 312 | case st_syn_received:
|
---|
| 313 | case st_established:
|
---|
| 314 | case st_fin_wait_1:
|
---|
| 315 | case st_fin_wait_2:
|
---|
| 316 | case st_close_wait:
|
---|
| 317 | case st_closing:
|
---|
| 318 | case st_last_ack:
|
---|
| 319 | case st_time_wait:
|
---|
| 320 | return true;
|
---|
| 321 | case st_closed:
|
---|
[7cf7ded] | 322 | log_msg(LVL_WARN, "state=%d", (int) conn->cstate);
|
---|
[32105348] | 323 | assert(false);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | assert(false);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[032bbe7] | 329 | /** Segment arrived in Listen state.
|
---|
| 330 | *
|
---|
| 331 | * @param conn Connection
|
---|
| 332 | * @param seg Segment
|
---|
| 333 | */
|
---|
[c5808b41] | 334 | static void tcp_conn_sa_listen(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 335 | {
|
---|
| 336 | log_msg(LVL_DEBUG, "tcp_conn_sa_listen(%p, %p)", conn, seg);
|
---|
| 337 |
|
---|
| 338 | if ((seg->ctrl & CTL_RST) != 0) {
|
---|
| 339 | log_msg(LVL_DEBUG, "Ignoring incoming RST.");
|
---|
| 340 | return;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
| 344 | log_msg(LVL_DEBUG, "Incoming ACK, send acceptable RST.");
|
---|
| 345 | tcp_reply_rst(&conn->ident, seg);
|
---|
| 346 | return;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | if ((seg->ctrl & CTL_SYN) == 0) {
|
---|
| 350 | log_msg(LVL_DEBUG, "SYN not present. Ignoring segment.");
|
---|
| 351 | return;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | log_msg(LVL_DEBUG, "Got SYN, sending SYN, ACK.");
|
---|
| 355 |
|
---|
| 356 | conn->rcv_nxt = seg->seq + 1;
|
---|
| 357 | conn->irs = seg->seq;
|
---|
| 358 |
|
---|
[4c55a64] | 359 |
|
---|
[c5808b41] | 360 | log_msg(LVL_DEBUG, "rcv_nxt=%u", conn->rcv_nxt);
|
---|
| 361 |
|
---|
| 362 | if (seg->len > 1)
|
---|
| 363 | log_msg(LVL_WARN, "SYN combined with data, ignoring data.");
|
---|
| 364 |
|
---|
| 365 | /* XXX select ISS */
|
---|
| 366 | conn->iss = 1;
|
---|
| 367 | conn->snd_nxt = conn->iss;
|
---|
| 368 | conn->snd_una = conn->iss;
|
---|
| 369 |
|
---|
[4c55a64] | 370 | /*
|
---|
| 371 | * Surprisingly the spec does not deal with initial window setting.
|
---|
| 372 | * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
|
---|
| 373 | * will always be accepted as new window setting.
|
---|
| 374 | */
|
---|
| 375 | conn->snd_wnd = seg->wnd;
|
---|
| 376 | conn->snd_wl1 = seg->seq;
|
---|
| 377 | conn->snd_wl2 = seg->seq;
|
---|
| 378 |
|
---|
[004a5fe] | 379 | tcp_conn_state_set(conn, st_syn_received);
|
---|
[c5808b41] | 380 |
|
---|
| 381 | tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
|
---|
[0093ab6] | 382 |
|
---|
| 383 | tcp_segment_delete(seg);
|
---|
[c5808b41] | 384 | }
|
---|
| 385 |
|
---|
[032bbe7] | 386 | /** Segment arrived in Syn-Sent state.
|
---|
| 387 | *
|
---|
| 388 | * @param conn Connection
|
---|
| 389 | * @param seg Segment
|
---|
| 390 | */
|
---|
[c5808b41] | 391 | static void tcp_conn_sa_syn_sent(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 392 | {
|
---|
| 393 | log_msg(LVL_DEBUG, "tcp_conn_sa_syn_sent(%p, %p)", conn, seg);
|
---|
| 394 |
|
---|
| 395 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
| 396 | log_msg(LVL_DEBUG, "snd_una=%u, seg.ack=%u, snd_nxt=%u",
|
---|
| 397 | conn->snd_una, seg->ack, conn->snd_nxt);
|
---|
| 398 | if (!seq_no_ack_acceptable(conn, seg->ack)) {
|
---|
| 399 | log_msg(LVL_WARN, "ACK not acceptable, send RST.");
|
---|
| 400 | tcp_reply_rst(&conn->ident, seg);
|
---|
| 401 | return;
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | if ((seg->ctrl & CTL_RST) != 0) {
|
---|
[6896409c] | 406 | log_msg(LVL_DEBUG, "%s: Connection reset. -> Closed",
|
---|
| 407 | conn->name);
|
---|
[704586fb] | 408 | /* Reset connection */
|
---|
| 409 | tcp_conn_reset(conn);
|
---|
[c5808b41] | 410 | /* XXX delete connection */
|
---|
| 411 | return;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | /* XXX precedence */
|
---|
| 415 |
|
---|
| 416 | if ((seg->ctrl & CTL_SYN) == 0) {
|
---|
| 417 | log_msg(LVL_DEBUG, "No SYN bit, ignoring segment.");
|
---|
| 418 | return;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | conn->rcv_nxt = seg->seq + 1;
|
---|
| 422 | conn->irs = seg->seq;
|
---|
| 423 |
|
---|
| 424 | if ((seg->ctrl & CTL_ACK) != 0) {
|
---|
| 425 | conn->snd_una = seg->ack;
|
---|
[d9ce049] | 426 |
|
---|
| 427 | /*
|
---|
| 428 | * Prune acked segments from retransmission queue and
|
---|
| 429 | * possibly transmit more data.
|
---|
| 430 | */
|
---|
| 431 | tcp_tqueue_ack_received(conn);
|
---|
[c5808b41] | 432 | }
|
---|
| 433 |
|
---|
| 434 | log_msg(LVL_DEBUG, "Sent SYN, got SYN.");
|
---|
| 435 |
|
---|
[32105348] | 436 | /*
|
---|
| 437 | * Surprisingly the spec does not deal with initial window setting.
|
---|
| 438 | * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
|
---|
| 439 | * will always be accepted as new window setting.
|
---|
| 440 | */
|
---|
| 441 | log_msg(LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", "
|
---|
| 442 | "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq);
|
---|
| 443 | conn->snd_wnd = seg->wnd;
|
---|
| 444 | conn->snd_wl1 = seg->seq;
|
---|
| 445 | conn->snd_wl2 = seg->seq;
|
---|
| 446 |
|
---|
[c5808b41] | 447 | if (seq_no_syn_acked(conn)) {
|
---|
[6896409c] | 448 | log_msg(LVL_DEBUG, "%s: syn acked -> Established", conn->name);
|
---|
[004a5fe] | 449 | tcp_conn_state_set(conn, st_established);
|
---|
[c5808b41] | 450 | tcp_tqueue_ctrl_seg(conn, CTL_ACK /* XXX */);
|
---|
| 451 | } else {
|
---|
[6896409c] | 452 | log_msg(LVL_DEBUG, "%s: syn not acked -> Syn-Received",
|
---|
| 453 | conn->name);
|
---|
[004a5fe] | 454 | tcp_conn_state_set(conn, st_syn_received);
|
---|
[c5808b41] | 455 | tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
|
---|
| 456 | }
|
---|
[0093ab6] | 457 |
|
---|
| 458 | tcp_segment_delete(seg);
|
---|
[c5808b41] | 459 | }
|
---|
| 460 |
|
---|
[032bbe7] | 461 | /** Segment arrived in state where segments are processed in sequence order.
|
---|
| 462 | *
|
---|
| 463 | * Queue segment in incoming segments queue for processing.
|
---|
| 464 | *
|
---|
| 465 | * @param conn Connection
|
---|
| 466 | * @param seg Segment
|
---|
| 467 | */
|
---|
[c5808b41] | 468 | static void tcp_conn_sa_queue(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 469 | {
|
---|
| 470 | tcp_segment_t *pseg;
|
---|
| 471 |
|
---|
| 472 | log_msg(LVL_DEBUG, "tcp_conn_sa_seq(%p, %p)", conn, seg);
|
---|
| 473 |
|
---|
[7cf7ded] | 474 | /* Discard unacceptable segments ("old duplicates") */
|
---|
| 475 | if (!seq_no_segment_acceptable(conn, seg)) {
|
---|
| 476 | log_msg(LVL_DEBUG, "Replying ACK to unacceptable segment.");
|
---|
| 477 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 478 | tcp_segment_delete(seg);
|
---|
| 479 | return;
|
---|
| 480 | }
|
---|
[c5808b41] | 481 |
|
---|
| 482 | /* Queue for processing */
|
---|
| 483 | tcp_iqueue_insert_seg(&conn->incoming, seg);
|
---|
| 484 |
|
---|
| 485 | /*
|
---|
| 486 | * Process all segments from incoming queue that are ready.
|
---|
| 487 | * Unacceptable segments are discarded by tcp_iqueue_get_ready_seg().
|
---|
| 488 | *
|
---|
| 489 | * XXX Need to return ACK for unacceptable segments
|
---|
| 490 | */
|
---|
| 491 | while (tcp_iqueue_get_ready_seg(&conn->incoming, &pseg) == EOK)
|
---|
| 492 | tcp_conn_seg_process(conn, pseg);
|
---|
| 493 | }
|
---|
| 494 |
|
---|
[032bbe7] | 495 | /** Process segment RST field.
|
---|
| 496 | *
|
---|
| 497 | * @param conn Connection
|
---|
| 498 | * @param seg Segment
|
---|
| 499 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 500 | * if not
|
---|
| 501 | */
|
---|
[c5808b41] | 502 | static cproc_t tcp_conn_seg_proc_rst(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 503 | {
|
---|
[d9e14fa4] | 504 | switch (conn->cstate) {
|
---|
| 505 | case st_syn_received:
|
---|
| 506 | /* XXX In case of passive open, revert to Listen state */
|
---|
| 507 | if (conn->ap == ap_passive) {
|
---|
| 508 | tcp_conn_state_set(conn, st_listen);
|
---|
| 509 | /* XXX Revert conn->ident */
|
---|
| 510 | tcp_conn_tw_timer_clear(conn);
|
---|
| 511 | tcp_tqueue_clear(&conn->retransmit);
|
---|
| 512 | } else {
|
---|
| 513 | tcp_conn_reset(conn);
|
---|
| 514 | }
|
---|
| 515 | break;
|
---|
| 516 | case st_established:
|
---|
| 517 | case st_fin_wait_1:
|
---|
| 518 | case st_fin_wait_2:
|
---|
| 519 | case st_close_wait:
|
---|
| 520 | /* General "connection reset" signal */
|
---|
| 521 | tcp_reset_signal(conn);
|
---|
| 522 | tcp_conn_reset(conn);
|
---|
| 523 | break;
|
---|
| 524 | case st_closing:
|
---|
| 525 | case st_last_ack:
|
---|
| 526 | case st_time_wait:
|
---|
| 527 | tcp_conn_reset(conn);
|
---|
| 528 | break;
|
---|
| 529 | case st_listen:
|
---|
| 530 | case st_syn_sent:
|
---|
| 531 | case st_closed:
|
---|
| 532 | assert(false);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | return cp_done;
|
---|
[c5808b41] | 536 | }
|
---|
| 537 |
|
---|
[032bbe7] | 538 | /** Process segment security and precedence fields.
|
---|
| 539 | *
|
---|
| 540 | * @param conn Connection
|
---|
| 541 | * @param seg Segment
|
---|
| 542 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 543 | * if not
|
---|
| 544 | */
|
---|
[c5808b41] | 545 | static cproc_t tcp_conn_seg_proc_sp(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 546 | {
|
---|
[4c55a64] | 547 | /* TODO */
|
---|
[c5808b41] | 548 | return cp_continue;
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[032bbe7] | 551 | /** Process segment SYN field.
|
---|
| 552 | *
|
---|
| 553 | * @param conn Connection
|
---|
| 554 | * @param seg Segment
|
---|
| 555 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 556 | * if not
|
---|
| 557 | */
|
---|
[c5808b41] | 558 | static cproc_t tcp_conn_seg_proc_syn(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 559 | {
|
---|
[7cf7ded] | 560 | if ((seg->ctrl & CTL_SYN) == 0)
|
---|
| 561 | return cp_continue;
|
---|
| 562 |
|
---|
| 563 | /*
|
---|
| 564 | * Assert SYN is in receive window, otherwise this step should not
|
---|
| 565 | * be reached.
|
---|
| 566 | */
|
---|
| 567 | assert(seq_no_in_rcv_wnd(conn, seg->seq));
|
---|
| 568 |
|
---|
| 569 | log_msg(LVL_WARN, "SYN is in receive window, should send reset. XXX");
|
---|
| 570 |
|
---|
| 571 | /*
|
---|
| 572 | * TODO
|
---|
| 573 | *
|
---|
| 574 | * Send a reset, resond "reset" to all outstanding RECEIVEs and SEND,
|
---|
| 575 | * flush segment queues. Send unsolicited "connection reset" signal
|
---|
| 576 | * to user, connection -> closed state, delete TCB, return.
|
---|
| 577 | */
|
---|
| 578 | return cp_done;
|
---|
[c5808b41] | 579 | }
|
---|
| 580 |
|
---|
[032bbe7] | 581 | /** Process segment ACK field in Syn-Received state.
|
---|
| 582 | *
|
---|
| 583 | * @param conn Connection
|
---|
| 584 | * @param seg Segment
|
---|
| 585 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 586 | * if not
|
---|
| 587 | */
|
---|
[c5808b41] | 588 | static cproc_t tcp_conn_seg_proc_ack_sr(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 589 | {
|
---|
| 590 | if (!seq_no_ack_acceptable(conn, seg->ack)) {
|
---|
| 591 | /* ACK is not acceptable, send RST. */
|
---|
| 592 | log_msg(LVL_WARN, "Segment ACK not acceptable, sending RST.");
|
---|
| 593 | tcp_reply_rst(&conn->ident, seg);
|
---|
[4c55a64] | 594 | tcp_segment_delete(seg);
|
---|
| 595 | return cp_done;
|
---|
[c5808b41] | 596 | }
|
---|
| 597 |
|
---|
[6896409c] | 598 | log_msg(LVL_DEBUG, "%s: SYN ACKed -> Established", conn->name);
|
---|
[c5808b41] | 599 |
|
---|
[004a5fe] | 600 | tcp_conn_state_set(conn, st_established);
|
---|
[c5808b41] | 601 |
|
---|
| 602 | /* XXX Not mentioned in spec?! */
|
---|
| 603 | conn->snd_una = seg->ack;
|
---|
| 604 |
|
---|
| 605 | return cp_continue;
|
---|
| 606 | }
|
---|
| 607 |
|
---|
[032bbe7] | 608 | /** Process segment ACK field in Established state.
|
---|
| 609 | *
|
---|
| 610 | * @param conn Connection
|
---|
| 611 | * @param seg Segment
|
---|
| 612 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 613 | * if not
|
---|
| 614 | */
|
---|
[c5808b41] | 615 | static cproc_t tcp_conn_seg_proc_ack_est(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 616 | {
|
---|
[0093ab6] | 617 | log_msg(LVL_DEBUG, "tcp_conn_seg_proc_ack_est(%p, %p)", conn, seg);
|
---|
| 618 |
|
---|
| 619 | log_msg(LVL_DEBUG, "SEG.ACK=%u, SND.UNA=%u, SND.NXT=%u",
|
---|
| 620 | (unsigned)seg->ack, (unsigned)conn->snd_una,
|
---|
| 621 | (unsigned)conn->snd_nxt);
|
---|
| 622 |
|
---|
[4c55a64] | 623 | if (!seq_no_ack_acceptable(conn, seg->ack)) {
|
---|
[0093ab6] | 624 | log_msg(LVL_DEBUG, "ACK not acceptable.");
|
---|
[4c55a64] | 625 | if (!seq_no_ack_duplicate(conn, seg->ack)) {
|
---|
[0093ab6] | 626 | log_msg(LVL_WARN, "Not acceptable, not duplicate. "
|
---|
| 627 | "Send ACK and drop.");
|
---|
[4c55a64] | 628 | /* Not acceptable, not duplicate. Send ACK and drop. */
|
---|
| 629 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 630 | tcp_segment_delete(seg);
|
---|
| 631 | return cp_done;
|
---|
[0093ab6] | 632 | } else {
|
---|
| 633 | log_msg(LVL_DEBUG, "Ignoring duplicate ACK.");
|
---|
[4c55a64] | 634 | }
|
---|
| 635 | } else {
|
---|
| 636 | /* Update SND.UNA */
|
---|
| 637 | conn->snd_una = seg->ack;
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | if (seq_no_new_wnd_update(conn, seg)) {
|
---|
| 641 | conn->snd_wnd = seg->wnd;
|
---|
| 642 | conn->snd_wl1 = seg->seq;
|
---|
| 643 | conn->snd_wl2 = seg->ack;
|
---|
[d9ce049] | 644 |
|
---|
| 645 | log_msg(LVL_DEBUG, "Updating send window, SND.WND=%" PRIu32
|
---|
| 646 | ", SND.WL1=%" PRIu32 ", SND.WL2=%" PRIu32,
|
---|
| 647 | conn->snd_wnd, conn->snd_wl1, conn->snd_wl2);
|
---|
[4c55a64] | 648 | }
|
---|
| 649 |
|
---|
[d9ce049] | 650 | /*
|
---|
| 651 | * Prune acked segments from retransmission queue and
|
---|
| 652 | * possibly transmit more data.
|
---|
| 653 | */
|
---|
| 654 | tcp_tqueue_ack_received(conn);
|
---|
| 655 |
|
---|
[c5808b41] | 656 | return cp_continue;
|
---|
| 657 | }
|
---|
| 658 |
|
---|
[032bbe7] | 659 | /** Process segment ACK field in Fin-Wait-1 state.
|
---|
| 660 | *
|
---|
| 661 | * @param conn Connection
|
---|
| 662 | * @param seg Segment
|
---|
| 663 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 664 | * if not
|
---|
| 665 | */
|
---|
[c5808b41] | 666 | static cproc_t tcp_conn_seg_proc_ack_fw1(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 667 | {
|
---|
[4c55a64] | 668 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 669 | return cp_done;
|
---|
| 670 |
|
---|
[6df418c4] | 671 | if (conn->fin_is_acked) {
|
---|
[6896409c] | 672 | log_msg(LVL_DEBUG, "%s: FIN acked -> Fin-Wait-2", conn->name);
|
---|
[004a5fe] | 673 | tcp_conn_state_set(conn, st_fin_wait_2);
|
---|
[6df418c4] | 674 | }
|
---|
| 675 |
|
---|
[c5808b41] | 676 | return cp_continue;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
[032bbe7] | 679 | /** Process segment ACK field in Fin-Wait-2 state.
|
---|
| 680 | *
|
---|
| 681 | * @param conn Connection
|
---|
| 682 | * @param seg Segment
|
---|
| 683 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 684 | * if not
|
---|
| 685 | */
|
---|
[c5808b41] | 686 | static cproc_t tcp_conn_seg_proc_ack_fw2(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 687 | {
|
---|
[4c55a64] | 688 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 689 | return cp_done;
|
---|
| 690 |
|
---|
| 691 | /* TODO */
|
---|
[c5808b41] | 692 | return cp_continue;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
[032bbe7] | 695 | /** Process segment ACK field in Close-Wait state.
|
---|
| 696 | *
|
---|
| 697 | * @param conn Connection
|
---|
| 698 | * @param seg Segment
|
---|
| 699 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 700 | * if not
|
---|
| 701 | */
|
---|
[c5808b41] | 702 | static cproc_t tcp_conn_seg_proc_ack_cw(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 703 | {
|
---|
[4c55a64] | 704 | /* The same processing as in Established state */
|
---|
| 705 | return tcp_conn_seg_proc_ack_est(conn, seg);
|
---|
[c5808b41] | 706 | }
|
---|
| 707 |
|
---|
[032bbe7] | 708 | /** Process segment ACK field in Closing state.
|
---|
| 709 | *
|
---|
| 710 | * @param conn Connection
|
---|
| 711 | * @param seg Segment
|
---|
| 712 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 713 | * if not
|
---|
| 714 | */
|
---|
[c5808b41] | 715 | static cproc_t tcp_conn_seg_proc_ack_cls(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 716 | {
|
---|
[4c55a64] | 717 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 718 | return cp_done;
|
---|
| 719 |
|
---|
| 720 | /* TODO */
|
---|
[c5808b41] | 721 | return cp_continue;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[032bbe7] | 724 | /** Process segment ACK field in Last-Ack state.
|
---|
| 725 | *
|
---|
| 726 | * @param conn Connection
|
---|
| 727 | * @param seg Segment
|
---|
| 728 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 729 | * if not
|
---|
| 730 | */
|
---|
[c5808b41] | 731 | static cproc_t tcp_conn_seg_proc_ack_la(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 732 | {
|
---|
[4c55a64] | 733 | if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
|
---|
| 734 | return cp_done;
|
---|
| 735 |
|
---|
[6e88fea] | 736 | if (conn->fin_is_acked) {
|
---|
[6896409c] | 737 | log_msg(LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
|
---|
[6e88fea] | 738 | tcp_conn_remove(conn);
|
---|
[004a5fe] | 739 | tcp_conn_state_set(conn, st_closed);
|
---|
[6e88fea] | 740 | return cp_done;
|
---|
| 741 | }
|
---|
| 742 |
|
---|
[c5808b41] | 743 | return cp_continue;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
[032bbe7] | 746 | /** Process segment ACK field in Time-Wait state.
|
---|
| 747 | *
|
---|
| 748 | * @param conn Connection
|
---|
| 749 | * @param seg Segment
|
---|
| 750 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 751 | * if not
|
---|
| 752 | */
|
---|
[c5808b41] | 753 | static cproc_t tcp_conn_seg_proc_ack_tw(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 754 | {
|
---|
[4c55a64] | 755 | /* Nothing to do */
|
---|
[c5808b41] | 756 | return cp_continue;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
[032bbe7] | 759 | /** Process segment ACK field.
|
---|
| 760 | *
|
---|
| 761 | * @param conn Connection
|
---|
| 762 | * @param seg Segment
|
---|
| 763 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 764 | * if not
|
---|
| 765 | */
|
---|
[c5808b41] | 766 | static cproc_t tcp_conn_seg_proc_ack(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 767 | {
|
---|
[23fe06c] | 768 | log_msg(LVL_DEBUG, "%s: tcp_conn_seg_proc_ack(%p, %p)",
|
---|
| 769 | conn->name, conn, seg);
|
---|
[c5808b41] | 770 |
|
---|
| 771 | if ((seg->ctrl & CTL_ACK) == 0) {
|
---|
| 772 | log_msg(LVL_WARN, "Segment has no ACK. Dropping.");
|
---|
[4c55a64] | 773 | tcp_segment_delete(seg);
|
---|
[c5808b41] | 774 | return cp_done;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | switch (conn->cstate) {
|
---|
| 778 | case st_syn_received:
|
---|
| 779 | return tcp_conn_seg_proc_ack_sr(conn, seg);
|
---|
| 780 | case st_established:
|
---|
| 781 | return tcp_conn_seg_proc_ack_est(conn, seg);
|
---|
| 782 | case st_fin_wait_1:
|
---|
| 783 | return tcp_conn_seg_proc_ack_fw1(conn, seg);
|
---|
| 784 | case st_fin_wait_2:
|
---|
| 785 | return tcp_conn_seg_proc_ack_fw2(conn, seg);
|
---|
| 786 | case st_close_wait:
|
---|
| 787 | return tcp_conn_seg_proc_ack_cw(conn, seg);
|
---|
| 788 | case st_closing:
|
---|
| 789 | return tcp_conn_seg_proc_ack_cls(conn, seg);
|
---|
| 790 | case st_last_ack:
|
---|
| 791 | return tcp_conn_seg_proc_ack_la(conn, seg);
|
---|
| 792 | case st_time_wait:
|
---|
| 793 | return tcp_conn_seg_proc_ack_tw(conn, seg);
|
---|
| 794 | case st_listen:
|
---|
| 795 | case st_syn_sent:
|
---|
| 796 | case st_closed:
|
---|
| 797 | assert(false);
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | assert(false);
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[032bbe7] | 803 | /** Process segment URG field.
|
---|
| 804 | *
|
---|
| 805 | * @param conn Connection
|
---|
| 806 | * @param seg Segment
|
---|
| 807 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 808 | * if not
|
---|
| 809 | */
|
---|
[c5808b41] | 810 | static cproc_t tcp_conn_seg_proc_urg(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 811 | {
|
---|
| 812 | return cp_continue;
|
---|
| 813 | }
|
---|
| 814 |
|
---|
[032bbe7] | 815 | /** Process segment text.
|
---|
| 816 | *
|
---|
| 817 | * @param conn Connection
|
---|
| 818 | * @param seg Segment
|
---|
| 819 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 820 | * if not
|
---|
| 821 | */
|
---|
[c5808b41] | 822 | static cproc_t tcp_conn_seg_proc_text(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 823 | {
|
---|
[0093ab6] | 824 | size_t text_size;
|
---|
| 825 | size_t xfer_size;
|
---|
| 826 |
|
---|
[23fe06c] | 827 | log_msg(LVL_DEBUG, "%s: tcp_conn_seg_proc_text(%p, %p)",
|
---|
| 828 | conn->name, conn, seg);
|
---|
[0093ab6] | 829 |
|
---|
[4c55a64] | 830 | switch (conn->cstate) {
|
---|
| 831 | case st_established:
|
---|
| 832 | case st_fin_wait_1:
|
---|
| 833 | case st_fin_wait_2:
|
---|
| 834 | /* OK */
|
---|
| 835 | break;
|
---|
| 836 | case st_close_wait:
|
---|
| 837 | case st_closing:
|
---|
| 838 | case st_last_ack:
|
---|
| 839 | case st_time_wait:
|
---|
| 840 | /* Invalid since FIN has been received. Ignore text. */
|
---|
| 841 | return cp_continue;
|
---|
| 842 | case st_listen:
|
---|
| 843 | case st_syn_sent:
|
---|
| 844 | case st_syn_received:
|
---|
| 845 | case st_closed:
|
---|
| 846 | assert(false);
|
---|
| 847 | }
|
---|
| 848 |
|
---|
[0093ab6] | 849 | /*
|
---|
| 850 | * Process segment text
|
---|
| 851 | */
|
---|
| 852 | assert(seq_no_segment_ready(conn, seg));
|
---|
| 853 |
|
---|
| 854 | /* Trim anything outside our receive window */
|
---|
| 855 | tcp_conn_trim_seg_to_wnd(conn, seg);
|
---|
| 856 |
|
---|
[d9ce049] | 857 | fibril_mutex_lock(&conn->rcv_buf_lock);
|
---|
| 858 |
|
---|
[0093ab6] | 859 | /* Determine how many bytes to copy */
|
---|
| 860 | text_size = tcp_segment_text_size(seg);
|
---|
| 861 | xfer_size = min(text_size, conn->rcv_buf_size - conn->rcv_buf_used);
|
---|
| 862 |
|
---|
| 863 | /* Copy data to receive buffer */
|
---|
[d9ce049] | 864 | tcp_segment_text_copy(seg, conn->rcv_buf + conn->rcv_buf_used,
|
---|
| 865 | xfer_size);
|
---|
| 866 | conn->rcv_buf_used += xfer_size;
|
---|
| 867 |
|
---|
| 868 | /* Signal to the receive function that new data has arrived */
|
---|
| 869 | fibril_condvar_broadcast(&conn->rcv_buf_cv);
|
---|
| 870 | fibril_mutex_unlock(&conn->rcv_buf_lock);
|
---|
[0093ab6] | 871 |
|
---|
[32105348] | 872 | log_msg(LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
|
---|
| 873 |
|
---|
[0093ab6] | 874 | /* Advance RCV.NXT */
|
---|
| 875 | conn->rcv_nxt += xfer_size;
|
---|
| 876 |
|
---|
| 877 | /* Update receive window. XXX Not an efficient strategy. */
|
---|
| 878 | conn->rcv_wnd -= xfer_size;
|
---|
| 879 |
|
---|
| 880 | /* Send ACK */
|
---|
| 881 | if (xfer_size > 0)
|
---|
| 882 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 883 |
|
---|
| 884 | if (xfer_size < seg->len) {
|
---|
[8c7a054] | 885 | /* Trim part of segment which we just received */
|
---|
[0093ab6] | 886 | tcp_conn_trim_seg_to_wnd(conn, seg);
|
---|
[8c7a054] | 887 | } else {
|
---|
[6896409c] | 888 | log_msg(LVL_DEBUG, "%s: Nothing left in segment, dropping "
|
---|
| 889 | "(xfer_size=%zu, SEG.LEN=%zu, seg->ctrl=%u)",
|
---|
| 890 | conn->name, xfer_size, seg->len, (unsigned)seg->ctrl);
|
---|
[8c7a054] | 891 | /* Nothing left in segment */
|
---|
| 892 | tcp_segment_delete(seg);
|
---|
[0093ab6] | 893 | return cp_done;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
[c5808b41] | 896 | return cp_continue;
|
---|
| 897 | }
|
---|
| 898 |
|
---|
[032bbe7] | 899 | /** Process segment FIN field.
|
---|
| 900 | *
|
---|
| 901 | * @param conn Connection
|
---|
| 902 | * @param seg Segment
|
---|
| 903 | * @return cp_done if we are done with this segment, cp_continue
|
---|
| 904 | * if not
|
---|
| 905 | */
|
---|
[c5808b41] | 906 | static cproc_t tcp_conn_seg_proc_fin(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 907 | {
|
---|
[23fe06c] | 908 | log_msg(LVL_DEBUG, "%s: tcp_conn_seg_proc_fin(%p, %p)",
|
---|
| 909 | conn->name, conn, seg);
|
---|
[7cf7ded] | 910 | log_msg(LVL_DEBUG, " seg->len=%zu, seg->ctl=%u", (size_t) seg->len,
|
---|
| 911 | (unsigned) seg->ctrl);
|
---|
[8c7a054] | 912 |
|
---|
| 913 | /* Only process FIN if no text is left in segment. */
|
---|
| 914 | if (tcp_segment_text_size(seg) == 0 && (seg->ctrl & CTL_FIN) != 0) {
|
---|
| 915 | log_msg(LVL_DEBUG, " - FIN found in segment.");
|
---|
| 916 |
|
---|
[6e88fea] | 917 | /* Send ACK */
|
---|
| 918 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 919 |
|
---|
[8c7a054] | 920 | conn->rcv_nxt++;
|
---|
| 921 | conn->rcv_wnd--;
|
---|
| 922 |
|
---|
[f343a16] | 923 | /* Change connection state */
|
---|
| 924 | switch (conn->cstate) {
|
---|
| 925 | case st_listen:
|
---|
| 926 | case st_syn_sent:
|
---|
| 927 | case st_closed:
|
---|
| 928 | /* Connection not synchronized */
|
---|
| 929 | assert(false);
|
---|
| 930 | case st_syn_received:
|
---|
| 931 | case st_established:
|
---|
[6896409c] | 932 | log_msg(LVL_DEBUG, "%s: FIN received -> Close-Wait",
|
---|
| 933 | conn->name);
|
---|
[004a5fe] | 934 | tcp_conn_state_set(conn, st_close_wait);
|
---|
[f343a16] | 935 | break;
|
---|
| 936 | case st_fin_wait_1:
|
---|
[6896409c] | 937 | log_msg(LVL_DEBUG, "%s: FIN received -> Closing",
|
---|
| 938 | conn->name);
|
---|
[004a5fe] | 939 | tcp_conn_state_set(conn, st_closing);
|
---|
[f343a16] | 940 | break;
|
---|
| 941 | case st_fin_wait_2:
|
---|
[6896409c] | 942 | log_msg(LVL_DEBUG, "%s: FIN received -> Time-Wait",
|
---|
| 943 | conn->name);
|
---|
[004a5fe] | 944 | tcp_conn_state_set(conn, st_time_wait);
|
---|
[2a3214e] | 945 | /* Start the Time-Wait timer */
|
---|
| 946 | tcp_conn_tw_timer_set(conn);
|
---|
[f343a16] | 947 | break;
|
---|
| 948 | case st_close_wait:
|
---|
| 949 | case st_closing:
|
---|
| 950 | case st_last_ack:
|
---|
| 951 | /* Do nothing */
|
---|
| 952 | break;
|
---|
| 953 | case st_time_wait:
|
---|
[2a3214e] | 954 | /* Restart the Time-Wait timer */
|
---|
| 955 | tcp_conn_tw_timer_set(conn);
|
---|
[f343a16] | 956 | break;
|
---|
| 957 | }
|
---|
[8c7a054] | 958 |
|
---|
| 959 | /* Add FIN to the receive buffer */
|
---|
| 960 | fibril_mutex_lock(&conn->rcv_buf_lock);
|
---|
| 961 | conn->rcv_buf_fin = true;
|
---|
| 962 | fibril_condvar_broadcast(&conn->rcv_buf_cv);
|
---|
| 963 | fibril_mutex_unlock(&conn->rcv_buf_lock);
|
---|
| 964 |
|
---|
| 965 | tcp_segment_delete(seg);
|
---|
| 966 | return cp_done;
|
---|
| 967 | }
|
---|
| 968 |
|
---|
[c5808b41] | 969 | return cp_continue;
|
---|
| 970 | }
|
---|
| 971 |
|
---|
| 972 | /** Process incoming segment.
|
---|
| 973 | *
|
---|
| 974 | * We are in connection state where segments are processed in order
|
---|
| 975 | * of sequence number. This processes one segment taken from the
|
---|
| 976 | * connection incoming segments queue.
|
---|
[032bbe7] | 977 | *
|
---|
| 978 | * @param conn Connection
|
---|
| 979 | * @param seg Segment
|
---|
[c5808b41] | 980 | */
|
---|
| 981 | static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 982 | {
|
---|
| 983 | log_msg(LVL_DEBUG, "tcp_conn_seg_process(%p, %p)", conn, seg);
|
---|
[6896409c] | 984 | tcp_segment_dump(seg);
|
---|
[c5808b41] | 985 |
|
---|
| 986 | /* Check whether segment is acceptable */
|
---|
| 987 | /* XXX Permit valid ACKs, URGs and RSTs */
|
---|
| 988 | /* if (!seq_no_segment_acceptable(conn, seg)) {
|
---|
| 989 | log_msg(LVL_WARN, "Segment not acceptable, dropping.");
|
---|
| 990 | if ((seg->ctrl & CTL_RST) == 0) {
|
---|
| 991 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 992 | }
|
---|
| 993 | return;
|
---|
| 994 | }
|
---|
| 995 | */
|
---|
| 996 |
|
---|
| 997 | if (tcp_conn_seg_proc_rst(conn, seg) == cp_done)
|
---|
| 998 | return;
|
---|
| 999 |
|
---|
| 1000 | if (tcp_conn_seg_proc_sp(conn, seg) == cp_done)
|
---|
| 1001 | return;
|
---|
| 1002 |
|
---|
| 1003 | if (tcp_conn_seg_proc_syn(conn, seg) == cp_done)
|
---|
| 1004 | return;
|
---|
| 1005 |
|
---|
| 1006 | if (tcp_conn_seg_proc_ack(conn, seg) == cp_done)
|
---|
| 1007 | return;
|
---|
| 1008 |
|
---|
| 1009 | if (tcp_conn_seg_proc_urg(conn, seg) == cp_done)
|
---|
| 1010 | return;
|
---|
| 1011 |
|
---|
| 1012 | if (tcp_conn_seg_proc_text(conn, seg) == cp_done)
|
---|
| 1013 | return;
|
---|
| 1014 |
|
---|
| 1015 | if (tcp_conn_seg_proc_fin(conn, seg) == cp_done)
|
---|
| 1016 | return;
|
---|
[0093ab6] | 1017 |
|
---|
[8c7a054] | 1018 | /*
|
---|
| 1019 | * If anything is left from the segment, insert it back into the
|
---|
| 1020 | * incoming segments queue.
|
---|
| 1021 | */
|
---|
[7cf7ded] | 1022 | if (seg->len > 0) {
|
---|
| 1023 | log_msg(LVL_DEBUG, "Re-insert segment %p. seg->len=%zu",
|
---|
| 1024 | seg, (size_t) seg->len);
|
---|
[8c7a054] | 1025 | tcp_iqueue_insert_seg(&conn->incoming, seg);
|
---|
[7cf7ded] | 1026 | } else {
|
---|
[8c7a054] | 1027 | tcp_segment_delete(seg);
|
---|
[7cf7ded] | 1028 | }
|
---|
[c5808b41] | 1029 | }
|
---|
| 1030 |
|
---|
[032bbe7] | 1031 | /** Segment arrived on a connection.
|
---|
| 1032 | *
|
---|
| 1033 | * @param conn Connection
|
---|
| 1034 | * @param seg Segment
|
---|
| 1035 | */
|
---|
[c5808b41] | 1036 | void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 1037 | {
|
---|
[23fe06c] | 1038 | log_msg(LVL_DEBUG, "%c: tcp_conn_segment_arrived(%p)",
|
---|
| 1039 | conn->name, seg);
|
---|
[c5808b41] | 1040 |
|
---|
| 1041 | switch (conn->cstate) {
|
---|
| 1042 | case st_listen:
|
---|
| 1043 | tcp_conn_sa_listen(conn, seg); break;
|
---|
| 1044 | case st_syn_sent:
|
---|
| 1045 | tcp_conn_sa_syn_sent(conn, seg); break;
|
---|
| 1046 | case st_syn_received:
|
---|
| 1047 | case st_established:
|
---|
| 1048 | case st_fin_wait_1:
|
---|
| 1049 | case st_fin_wait_2:
|
---|
| 1050 | case st_close_wait:
|
---|
| 1051 | case st_closing:
|
---|
| 1052 | case st_last_ack:
|
---|
| 1053 | case st_time_wait:
|
---|
| 1054 | /* Process segments in order of sequence number */
|
---|
| 1055 | tcp_conn_sa_queue(conn, seg); break;
|
---|
| 1056 | case st_closed:
|
---|
[7cf7ded] | 1057 | log_msg(LVL_DEBUG, "state=%d", (int) conn->cstate);
|
---|
[c5808b41] | 1058 | assert(false);
|
---|
| 1059 | }
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
[2a3214e] | 1062 | /** Time-Wait timeout handler.
|
---|
| 1063 | *
|
---|
| 1064 | * @param arg Connection
|
---|
| 1065 | */
|
---|
| 1066 | static void tw_timeout_func(void *arg)
|
---|
| 1067 | {
|
---|
| 1068 | tcp_conn_t *conn = (tcp_conn_t *) arg;
|
---|
| 1069 |
|
---|
| 1070 | log_msg(LVL_DEBUG, "tw_timeout_func(%p)", conn);
|
---|
| 1071 |
|
---|
[704586fb] | 1072 | if (conn->cstate == st_closed) {
|
---|
| 1073 | log_msg(LVL_DEBUG, "Connection already closed.");
|
---|
| 1074 | return;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | log_msg(LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
|
---|
[2a3214e] | 1078 | tcp_conn_remove(conn);
|
---|
[004a5fe] | 1079 | tcp_conn_state_set(conn, st_closed);
|
---|
[2a3214e] | 1080 | }
|
---|
| 1081 |
|
---|
| 1082 | /** Start or restart the Time-Wait timeout.
|
---|
| 1083 | *
|
---|
| 1084 | * @param conn Connection
|
---|
| 1085 | */
|
---|
| 1086 | void tcp_conn_tw_timer_set(tcp_conn_t *conn)
|
---|
| 1087 | {
|
---|
| 1088 | fibril_timer_set(conn->tw_timer, TIME_WAIT_TIMEOUT, tw_timeout_func,
|
---|
| 1089 | (void *)conn);
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
[704586fb] | 1092 | /** Clear the Time-Wait timeout.
|
---|
| 1093 | *
|
---|
| 1094 | * @param conn Connection
|
---|
| 1095 | */
|
---|
| 1096 | void tcp_conn_tw_timer_clear(tcp_conn_t *conn)
|
---|
| 1097 | {
|
---|
| 1098 | fibril_timer_clear(conn->tw_timer);
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
[032bbe7] | 1101 | /** Trim segment to the receive window.
|
---|
| 1102 | *
|
---|
| 1103 | * @param conn Connection
|
---|
| 1104 | * @param seg Segment
|
---|
| 1105 | */
|
---|
[0093ab6] | 1106 | void tcp_conn_trim_seg_to_wnd(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 1107 | {
|
---|
| 1108 | uint32_t left, right;
|
---|
| 1109 |
|
---|
| 1110 | seq_no_seg_trim_calc(conn, seg, &left, &right);
|
---|
| 1111 | tcp_segment_trim(seg, left, right);
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
[032bbe7] | 1114 | /** Handle unexpected segment received on a socket pair.
|
---|
| 1115 | *
|
---|
| 1116 | * We reply with an RST unless the received segment has RST.
|
---|
| 1117 | *
|
---|
| 1118 | * @param sp Socket pair which received the segment
|
---|
| 1119 | * @param seg Unexpected segment
|
---|
| 1120 | */
|
---|
[c5808b41] | 1121 | void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 1122 | {
|
---|
| 1123 | log_msg(LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
|
---|
| 1124 |
|
---|
| 1125 | if ((seg->ctrl & CTL_RST) == 0)
|
---|
| 1126 | tcp_reply_rst(sp, seg);
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
[032bbe7] | 1129 | /** Compute flipped socket pair for response.
|
---|
| 1130 | *
|
---|
[32105348] | 1131 | * Flipped socket pair has local and foreign sockets exchanged.
|
---|
[032bbe7] | 1132 | *
|
---|
| 1133 | * @param sp Socket pair
|
---|
| 1134 | * @param fsp Place to store flipped socket pair
|
---|
| 1135 | */
|
---|
[c5808b41] | 1136 | void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
|
---|
| 1137 | {
|
---|
| 1138 | fsp->local = sp->foreign;
|
---|
| 1139 | fsp->foreign = sp->local;
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
[032bbe7] | 1142 | /** Send RST in response to an incoming segment.
|
---|
| 1143 | *
|
---|
| 1144 | * @param sp Socket pair which received the segment
|
---|
| 1145 | * @param seg Incoming segment
|
---|
| 1146 | */
|
---|
[c5808b41] | 1147 | void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 1148 | {
|
---|
| 1149 | tcp_segment_t *rseg;
|
---|
| 1150 |
|
---|
| 1151 | log_msg(LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
|
---|
| 1152 |
|
---|
| 1153 | rseg = tcp_segment_make_rst(seg);
|
---|
[704586fb] | 1154 | tcp_transmit_segment(sp, rseg);
|
---|
[c5808b41] | 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | /**
|
---|
| 1158 | * @}
|
---|
| 1159 | */
|
---|