| 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 | /**
|
|---|
| 34 | * @file TCP entry points (close to those defined in the RFC)
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <fibril_synch.h>
|
|---|
| 38 | #include <io/log.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include "conn.h"
|
|---|
| 42 | #include "tcp_type.h"
|
|---|
| 43 | #include "tqueue.h"
|
|---|
| 44 | #include "ucall.h"
|
|---|
| 45 |
|
|---|
| 46 | /*
|
|---|
| 47 | * User calls
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | /** OPEN user call
|
|---|
| 51 | *
|
|---|
| 52 | * @param lsock Local socket
|
|---|
| 53 | * @param fsock Foreign socket
|
|---|
| 54 | * @param acpass Active/passive
|
|---|
| 55 | * @param oflags Open flags
|
|---|
| 56 | * @param conn Connection
|
|---|
| 57 | *
|
|---|
| 58 | * Unlike in the spec we allow specifying the local address. This means
|
|---|
| 59 | * the implementation does not need to magically guess it, especially
|
|---|
| 60 | * considering there can be more than one local address.
|
|---|
| 61 | *
|
|---|
| 62 | * XXX We should be able to call active open on an existing listening
|
|---|
| 63 | * connection.
|
|---|
| 64 | * XXX We should be able to get connection structure immediately, before
|
|---|
| 65 | * establishment.
|
|---|
| 66 | */
|
|---|
| 67 | tcp_error_t tcp_uc_open(tcp_sock_t *lsock, tcp_sock_t *fsock, acpass_t acpass,
|
|---|
| 68 | tcp_open_flags_t oflags, tcp_conn_t **conn)
|
|---|
| 69 | {
|
|---|
| 70 | tcp_conn_t *nconn;
|
|---|
| 71 |
|
|---|
| 72 | log_msg(LVL_DEBUG, "tcp_uc_open(%p, %p, %s, %s, %p)",
|
|---|
| 73 | lsock, fsock, acpass == ap_active ? "active" : "passive",
|
|---|
| 74 | oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
|
|---|
| 75 |
|
|---|
| 76 | nconn = tcp_conn_new(lsock, fsock);
|
|---|
| 77 | tcp_conn_add(nconn);
|
|---|
| 78 |
|
|---|
| 79 | if (acpass == ap_active) {
|
|---|
| 80 | /* Synchronize (initiate) connection */
|
|---|
| 81 | tcp_conn_sync(nconn);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (oflags == tcp_open_nonblock) {
|
|---|
| 85 | *conn = nconn;
|
|---|
| 86 | return TCP_EOK;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* Wait for connection to be established or reset */
|
|---|
| 90 | log_msg(LVL_DEBUG, "tcp_uc_open: Wait for connection.");
|
|---|
| 91 | fibril_mutex_lock(&nconn->lock);
|
|---|
| 92 | while (nconn->cstate == st_listen ||
|
|---|
| 93 | nconn->cstate == st_syn_sent ||
|
|---|
| 94 | nconn->cstate == st_syn_received) {
|
|---|
| 95 | fibril_condvar_wait(&nconn->cstate_cv, &nconn->lock);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (nconn->cstate != st_established) {
|
|---|
| 99 | log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset.");
|
|---|
| 100 | assert(nconn->cstate == st_closed);
|
|---|
| 101 | fibril_mutex_unlock(&nconn->lock);
|
|---|
| 102 | return TCP_ERESET;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | fibril_mutex_unlock(&nconn->lock);
|
|---|
| 106 | log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established.");
|
|---|
| 107 |
|
|---|
| 108 | *conn = nconn;
|
|---|
| 109 | log_msg(LVL_DEBUG, "tcp_uc_open -> %p", nconn);
|
|---|
| 110 | return TCP_EOK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** SEND user call */
|
|---|
| 114 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
|---|
| 115 | xflags_t flags)
|
|---|
| 116 | {
|
|---|
| 117 | size_t buf_free;
|
|---|
| 118 | size_t xfer_size;
|
|---|
| 119 |
|
|---|
| 120 | log_msg(LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
|
|---|
| 121 |
|
|---|
| 122 | fibril_mutex_lock(&conn->lock);
|
|---|
| 123 |
|
|---|
| 124 | if (conn->cstate == st_closed) {
|
|---|
| 125 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 126 | return TCP_ENOTEXIST;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | if (conn->cstate == st_listen) {
|
|---|
| 130 | /* Change connection to active */
|
|---|
| 131 | tcp_conn_sync(conn);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | if (conn->snd_buf_fin) {
|
|---|
| 136 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 137 | return TCP_ECLOSING;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | while (size > 0) {
|
|---|
| 141 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
|---|
| 142 | while (buf_free == 0 && !conn->reset) {
|
|---|
| 143 | log_msg(LVL_DEBUG, "%s: buf_free == 0, waiting.",
|
|---|
| 144 | conn->name);
|
|---|
| 145 | fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
|
|---|
| 146 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | if (conn->reset) {
|
|---|
| 150 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 151 | return TCP_ERESET;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | xfer_size = min(size, buf_free);
|
|---|
| 155 |
|
|---|
| 156 | /* Copy data to buffer */
|
|---|
| 157 | memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
|
|---|
| 158 | data += xfer_size;
|
|---|
| 159 | conn->snd_buf_used += xfer_size;
|
|---|
| 160 | size -= xfer_size;
|
|---|
| 161 |
|
|---|
| 162 | tcp_tqueue_new_data(conn);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | tcp_tqueue_new_data(conn);
|
|---|
| 166 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 167 |
|
|---|
| 168 | return TCP_EOK;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** RECEIVE user call */
|
|---|
| 172 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
|---|
| 173 | size_t *rcvd, xflags_t *xflags)
|
|---|
| 174 | {
|
|---|
| 175 | size_t xfer_size;
|
|---|
| 176 |
|
|---|
| 177 | log_msg(LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
|---|
| 178 |
|
|---|
| 179 | fibril_mutex_lock(&conn->lock);
|
|---|
| 180 |
|
|---|
| 181 | if (conn->cstate == st_closed) {
|
|---|
| 182 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 183 | return TCP_ENOTEXIST;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /* Wait for data to become available */
|
|---|
| 187 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
|---|
| 188 | log_msg(LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
|---|
| 189 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | if (conn->rcv_buf_used == 0) {
|
|---|
| 193 | *rcvd = 0;
|
|---|
| 194 | *xflags = 0;
|
|---|
| 195 |
|
|---|
| 196 | if (conn->rcv_buf_fin) {
|
|---|
| 197 | /* End of data, peer closed connection */
|
|---|
| 198 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 199 | return TCP_ECLOSING;
|
|---|
| 200 | } else {
|
|---|
| 201 | /* Connection was reset */
|
|---|
| 202 | assert(conn->reset);
|
|---|
| 203 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 204 | return TCP_ERESET;
|
|---|
| 205 | }
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /* Copy data from receive buffer to user buffer */
|
|---|
| 209 | xfer_size = min(size, conn->rcv_buf_used);
|
|---|
| 210 | memcpy(buf, conn->rcv_buf, xfer_size);
|
|---|
| 211 | *rcvd = xfer_size;
|
|---|
| 212 |
|
|---|
| 213 | /* Remove data from receive buffer */
|
|---|
| 214 | memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
|
|---|
| 215 | xfer_size);
|
|---|
| 216 | conn->rcv_buf_used -= xfer_size;
|
|---|
| 217 | conn->rcv_wnd += xfer_size;
|
|---|
| 218 |
|
|---|
| 219 | /* TODO */
|
|---|
| 220 | *xflags = 0;
|
|---|
| 221 |
|
|---|
| 222 | /* Send new size of receive window */
|
|---|
| 223 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
|---|
| 224 |
|
|---|
| 225 | log_msg(LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
|---|
| 226 | conn->name, xfer_size);
|
|---|
| 227 |
|
|---|
| 228 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 229 |
|
|---|
| 230 | return TCP_EOK;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** CLOSE user call */
|
|---|
| 234 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
|---|
| 235 | {
|
|---|
| 236 | log_msg(LVL_DEBUG, "%s: tcp_uc_close()", conn->name);
|
|---|
| 237 |
|
|---|
| 238 | fibril_mutex_lock(&conn->lock);
|
|---|
| 239 |
|
|---|
| 240 | if (conn->cstate == st_closed) {
|
|---|
| 241 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 242 | return TCP_ENOTEXIST;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (conn->snd_buf_fin) {
|
|---|
| 246 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 247 | return TCP_ECLOSING;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | conn->snd_buf_fin = true;
|
|---|
| 251 | tcp_tqueue_new_data(conn);
|
|---|
| 252 |
|
|---|
| 253 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 254 | return TCP_EOK;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /** ABORT user call */
|
|---|
| 258 | void tcp_uc_abort(tcp_conn_t *conn)
|
|---|
| 259 | {
|
|---|
| 260 | log_msg(LVL_DEBUG, "tcp_uc_abort()");
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /** STATUS user call */
|
|---|
| 264 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
|---|
| 265 | {
|
|---|
| 266 | log_msg(LVL_DEBUG, "tcp_uc_status()");
|
|---|
| 267 | cstatus->cstate = conn->cstate;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /** Delete connection user call.
|
|---|
| 271 | *
|
|---|
| 272 | * (Not in spec.) Inform TCP that the user is done with this connection
|
|---|
| 273 | * and will not make any further calls/references to it. TCP can deallocate
|
|---|
| 274 | * the connection from now on.
|
|---|
| 275 | */
|
|---|
| 276 | void tcp_uc_delete(tcp_conn_t *conn)
|
|---|
| 277 | {
|
|---|
| 278 | log_msg(LVL_DEBUG, "tcp_uc_delete()");
|
|---|
| 279 | tcp_conn_delete(conn);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | void tcp_uc_set_cstate_cb(tcp_conn_t *conn, tcp_cstate_cb_t cb, void *arg)
|
|---|
| 283 | {
|
|---|
| 284 | log_msg(LVL_DEBUG, "tcp_uc_set_ctate_cb(%p, %p, %p)",
|
|---|
| 285 | conn, cb, arg);
|
|---|
| 286 |
|
|---|
| 287 | conn->cstate_cb = cb;
|
|---|
| 288 | conn->cstate_cb_arg = arg;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /*
|
|---|
| 292 | * Arriving segments
|
|---|
| 293 | */
|
|---|
| 294 |
|
|---|
| 295 | /** Segment arrived */
|
|---|
| 296 | void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
|---|
| 297 | {
|
|---|
| 298 | tcp_conn_t *conn;
|
|---|
| 299 |
|
|---|
| 300 | log_msg(LVL_DEBUG, "tcp_as_segment_arrived(f:(%x,%u), l:(%x,%u))",
|
|---|
| 301 | sp->foreign.addr.ipv4, sp->foreign.port,
|
|---|
| 302 | sp->local.addr.ipv4, sp->local.port);
|
|---|
| 303 |
|
|---|
| 304 | conn = tcp_conn_find_ref(sp);
|
|---|
| 305 | if (conn == NULL) {
|
|---|
| 306 | log_msg(LVL_WARN, "No connection found.");
|
|---|
| 307 | tcp_unexpected_segment(sp, seg);
|
|---|
| 308 | return;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | fibril_mutex_lock(&conn->lock);
|
|---|
| 312 |
|
|---|
| 313 | if (conn->cstate == st_closed) {
|
|---|
| 314 | log_msg(LVL_WARN, "Connection is closed.");
|
|---|
| 315 | tcp_unexpected_segment(sp, seg);
|
|---|
| 316 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 317 | tcp_conn_delref(conn);
|
|---|
| 318 | return;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | if (conn->ident.foreign.addr.ipv4 == TCP_IPV4_ANY)
|
|---|
| 322 | conn->ident.foreign.addr.ipv4 = sp->foreign.addr.ipv4;
|
|---|
| 323 | if (conn->ident.foreign.port == TCP_PORT_ANY)
|
|---|
| 324 | conn->ident.foreign.port = sp->foreign.port;
|
|---|
| 325 | if (conn->ident.local.addr.ipv4 == TCP_IPV4_ANY)
|
|---|
| 326 | conn->ident.local.addr.ipv4 = sp->local.addr.ipv4;
|
|---|
| 327 |
|
|---|
| 328 | tcp_conn_segment_arrived(conn, seg);
|
|---|
| 329 |
|
|---|
| 330 | fibril_mutex_unlock(&conn->lock);
|
|---|
| 331 | tcp_conn_delref(conn);
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /*
|
|---|
| 335 | * Timeouts
|
|---|
| 336 | */
|
|---|
| 337 |
|
|---|
| 338 | /** User timeout */
|
|---|
| 339 | void tcp_to_user(void)
|
|---|
| 340 | {
|
|---|
| 341 | log_msg(LVL_DEBUG, "tcp_to_user()");
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | /**
|
|---|
| 345 | * @}
|
|---|
| 346 | */
|
|---|