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