[457] | 1 | /**
|
---|
| 2 | * @file
|
---|
| 3 | * Transmission Control Protocol for IP
|
---|
| 4 | * See also @ref tcp_raw
|
---|
| 5 | *
|
---|
| 6 | * @defgroup tcp_raw TCP
|
---|
| 7 | * @ingroup callbackstyle_api
|
---|
| 8 | * Transmission Control Protocol for IP\n
|
---|
| 9 | * @see @ref api
|
---|
| 10 | *
|
---|
| 11 | * Common functions for the TCP implementation, such as functions
|
---|
| 12 | * for manipulating the data structures and the TCP timer functions. TCP functions
|
---|
| 13 | * related to input and output is found in tcp_in.c and tcp_out.c respectively.\n
|
---|
| 14 | *
|
---|
| 15 | * TCP connection setup
|
---|
| 16 | * --------------------
|
---|
| 17 | * The functions used for setting up connections is similar to that of
|
---|
| 18 | * the sequential API and of the BSD socket API. A new TCP connection
|
---|
| 19 | * identifier (i.e., a protocol control block - PCB) is created with the
|
---|
| 20 | * tcp_new() function. This PCB can then be either set to listen for new
|
---|
| 21 | * incoming connections or be explicitly connected to another host.
|
---|
| 22 | * - tcp_new()
|
---|
| 23 | * - tcp_bind()
|
---|
| 24 | * - tcp_listen() and tcp_listen_with_backlog()
|
---|
| 25 | * - tcp_accept()
|
---|
| 26 | * - tcp_connect()
|
---|
| 27 | *
|
---|
| 28 | * Sending TCP data
|
---|
| 29 | * ----------------
|
---|
| 30 | * TCP data is sent by enqueueing the data with a call to tcp_write() and
|
---|
| 31 | * triggering to send by calling tcp_output(). When the data is successfully
|
---|
| 32 | * transmitted to the remote host, the application will be notified with a
|
---|
| 33 | * call to a specified callback function.
|
---|
| 34 | * - tcp_write()
|
---|
| 35 | * - tcp_output()
|
---|
| 36 | * - tcp_sent()
|
---|
| 37 | *
|
---|
| 38 | * Receiving TCP data
|
---|
| 39 | * ------------------
|
---|
| 40 | * TCP data reception is callback based - an application specified
|
---|
| 41 | * callback function is called when new data arrives. When the
|
---|
| 42 | * application has taken the data, it has to call the tcp_recved()
|
---|
| 43 | * function to indicate that TCP can advertise increase the receive
|
---|
| 44 | * window.
|
---|
| 45 | * - tcp_recv()
|
---|
| 46 | * - tcp_recved()
|
---|
| 47 | *
|
---|
| 48 | * Application polling
|
---|
| 49 | * -------------------
|
---|
| 50 | * When a connection is idle (i.e., no data is either transmitted or
|
---|
| 51 | * received), lwIP will repeatedly poll the application by calling a
|
---|
| 52 | * specified callback function. This can be used either as a watchdog
|
---|
| 53 | * timer for killing connections that have stayed idle for too long, or
|
---|
| 54 | * as a method of waiting for memory to become available. For instance,
|
---|
| 55 | * if a call to tcp_write() has failed because memory wasn't available,
|
---|
| 56 | * the application may use the polling functionality to call tcp_write()
|
---|
| 57 | * again when the connection has been idle for a while.
|
---|
| 58 | * - tcp_poll()
|
---|
| 59 | *
|
---|
| 60 | * Closing and aborting connections
|
---|
| 61 | * --------------------------------
|
---|
| 62 | * - tcp_close()
|
---|
| 63 | * - tcp_abort()
|
---|
| 64 | * - tcp_err()
|
---|
| 65 | *
|
---|
| 66 | */
|
---|
| 67 |
|
---|
| 68 | /*
|
---|
| 69 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
---|
| 70 | * All rights reserved.
|
---|
| 71 | *
|
---|
| 72 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
| 73 | * are permitted provided that the following conditions are met:
|
---|
| 74 | *
|
---|
| 75 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
| 76 | * this list of conditions and the following disclaimer.
|
---|
| 77 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 78 | * this list of conditions and the following disclaimer in the documentation
|
---|
| 79 | * and/or other materials provided with the distribution.
|
---|
| 80 | * 3. The name of the author may not be used to endorse or promote products
|
---|
| 81 | * derived from this software without specific prior written permission.
|
---|
| 82 | *
|
---|
| 83 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
---|
| 84 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
| 85 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
---|
| 86 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 87 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
---|
| 88 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
| 89 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
| 90 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
---|
| 91 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
---|
| 92 | * OF SUCH DAMAGE.
|
---|
| 93 | *
|
---|
| 94 | * This file is part of the lwIP TCP/IP stack.
|
---|
| 95 | *
|
---|
| 96 | * Author: Adam Dunkels <adam@sics.se>
|
---|
| 97 | *
|
---|
| 98 | */
|
---|
| 99 |
|
---|
| 100 | #include "lwip/opt.h"
|
---|
| 101 |
|
---|
| 102 | #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
|
---|
| 103 |
|
---|
| 104 | #include "lwip/def.h"
|
---|
| 105 | #include "lwip/mem.h"
|
---|
| 106 | #include "lwip/memp.h"
|
---|
| 107 | #include "lwip/tcp.h"
|
---|
| 108 | #include "lwip/priv/tcp_priv.h"
|
---|
| 109 | #include "lwip/debug.h"
|
---|
| 110 | #include "lwip/stats.h"
|
---|
| 111 | #include "lwip/ip6.h"
|
---|
| 112 | #include "lwip/ip6_addr.h"
|
---|
| 113 | #include "lwip/nd6.h"
|
---|
| 114 |
|
---|
| 115 | #include <string.h>
|
---|
| 116 |
|
---|
| 117 | #ifdef LWIP_HOOK_FILENAME
|
---|
| 118 | #include LWIP_HOOK_FILENAME
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
| 121 | #ifndef TCP_LOCAL_PORT_RANGE_START
|
---|
| 122 | /* From http://www.iana.org/assignments/port-numbers:
|
---|
| 123 | "The Dynamic and/or Private Ports are those from 49152 through 65535" */
|
---|
| 124 | #define TCP_LOCAL_PORT_RANGE_START 0xc000
|
---|
| 125 | #define TCP_LOCAL_PORT_RANGE_END 0xffff
|
---|
| 126 | #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & (u16_t)~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START))
|
---|
| 127 | #endif
|
---|
| 128 |
|
---|
| 129 | #if LWIP_TCP_KEEPALIVE
|
---|
| 130 | #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl)
|
---|
| 131 | #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
|
---|
| 132 | #else /* LWIP_TCP_KEEPALIVE */
|
---|
| 133 | #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE
|
---|
| 134 | #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
|
---|
| 135 | #endif /* LWIP_TCP_KEEPALIVE */
|
---|
| 136 |
|
---|
| 137 | /* As initial send MSS, we use TCP_MSS but limit it to 536. */
|
---|
| 138 | #if TCP_MSS > 536
|
---|
| 139 | #define INITIAL_MSS 536
|
---|
| 140 | #else
|
---|
| 141 | #define INITIAL_MSS TCP_MSS
|
---|
| 142 | #endif
|
---|
| 143 |
|
---|
| 144 | static const char *const tcp_state_str[] = {
|
---|
| 145 | "CLOSED",
|
---|
| 146 | "LISTEN",
|
---|
| 147 | "SYN_SENT",
|
---|
| 148 | "SYN_RCVD",
|
---|
| 149 | "ESTABLISHED",
|
---|
| 150 | "FIN_WAIT_1",
|
---|
| 151 | "FIN_WAIT_2",
|
---|
| 152 | "CLOSE_WAIT",
|
---|
| 153 | "CLOSING",
|
---|
| 154 | "LAST_ACK",
|
---|
| 155 | "TIME_WAIT"
|
---|
| 156 | };
|
---|
| 157 |
|
---|
| 158 | /* last local TCP port */
|
---|
| 159 | static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
|
---|
| 160 |
|
---|
| 161 | /* Incremented every coarse grained timer shot (typically every 500 ms). */
|
---|
| 162 | u32_t tcp_ticks;
|
---|
| 163 | static const u8_t tcp_backoff[13] =
|
---|
| 164 | { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
|
---|
| 165 | /* Times per slowtmr hits */
|
---|
| 166 | static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
|
---|
| 167 |
|
---|
| 168 | /* The TCP PCB lists. */
|
---|
| 169 |
|
---|
| 170 | /** List of all TCP PCBs bound but not yet (connected || listening) */
|
---|
| 171 | struct tcp_pcb *tcp_bound_pcbs;
|
---|
| 172 | /** List of all TCP PCBs in LISTEN state */
|
---|
| 173 | union tcp_listen_pcbs_t tcp_listen_pcbs;
|
---|
| 174 | /** List of all TCP PCBs that are in a state in which
|
---|
| 175 | * they accept or send data. */
|
---|
| 176 | struct tcp_pcb *tcp_active_pcbs;
|
---|
| 177 | /** List of all TCP PCBs in TIME-WAIT state */
|
---|
| 178 | struct tcp_pcb *tcp_tw_pcbs;
|
---|
| 179 |
|
---|
| 180 | /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
|
---|
| 181 | struct tcp_pcb **const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
|
---|
| 182 | &tcp_active_pcbs, &tcp_tw_pcbs
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | u8_t tcp_active_pcbs_changed;
|
---|
| 186 |
|
---|
| 187 | /** Timer counter to handle calling slow-timer from tcp_tmr() */
|
---|
| 188 | static u8_t tcp_timer;
|
---|
| 189 | static u8_t tcp_timer_ctr;
|
---|
| 190 | static u16_t tcp_new_port(void);
|
---|
| 191 |
|
---|
| 192 | static err_t tcp_close_shutdown_fin(struct tcp_pcb *pcb);
|
---|
| 193 | #if LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
| 194 | static void tcp_ext_arg_invoke_callbacks_destroyed(struct tcp_pcb_ext_args *ext_args);
|
---|
| 195 | #endif
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Initialize this module.
|
---|
| 199 | */
|
---|
| 200 | void
|
---|
| 201 | tcp_init(void)
|
---|
| 202 | {
|
---|
| 203 | #ifdef LWIP_RAND
|
---|
| 204 | tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
|
---|
| 205 | #endif /* LWIP_RAND */
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /** Free a tcp pcb */
|
---|
| 209 | void
|
---|
| 210 | tcp_free(struct tcp_pcb *pcb)
|
---|
| 211 | {
|
---|
| 212 | LWIP_ASSERT("tcp_free: LISTEN", pcb->state != LISTEN);
|
---|
| 213 | #if LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
| 214 | tcp_ext_arg_invoke_callbacks_destroyed(pcb->ext_args);
|
---|
| 215 | #endif
|
---|
| 216 | memp_free(MEMP_TCP_PCB, pcb);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Free a tcp listen pcb */
|
---|
| 220 | static void
|
---|
| 221 | tcp_free_listen(struct tcp_pcb *pcb)
|
---|
| 222 | {
|
---|
| 223 | LWIP_ASSERT("tcp_free_listen: !LISTEN", pcb->state != LISTEN);
|
---|
| 224 | #if LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
| 225 | tcp_ext_arg_invoke_callbacks_destroyed(pcb->ext_args);
|
---|
| 226 | #endif
|
---|
| 227 | memp_free(MEMP_TCP_PCB_LISTEN, pcb);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /**
|
---|
| 231 | * Called periodically to dispatch TCP timers.
|
---|
| 232 | */
|
---|
| 233 | void
|
---|
| 234 | tcp_tmr(void)
|
---|
| 235 | {
|
---|
| 236 | /* Call tcp_fasttmr() every 250 ms */
|
---|
| 237 | tcp_fasttmr();
|
---|
| 238 |
|
---|
| 239 | if (++tcp_timer & 1) {
|
---|
| 240 | /* Call tcp_slowtmr() every 500 ms, i.e., every other timer
|
---|
| 241 | tcp_tmr() is called. */
|
---|
| 242 | tcp_slowtmr();
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
|
---|
| 247 | /** Called when a listen pcb is closed. Iterates one pcb list and removes the
|
---|
| 248 | * closed listener pcb from pcb->listener if matching.
|
---|
| 249 | */
|
---|
| 250 | static void
|
---|
| 251 | tcp_remove_listener(struct tcp_pcb *list, struct tcp_pcb_listen *lpcb)
|
---|
| 252 | {
|
---|
| 253 | struct tcp_pcb *pcb;
|
---|
| 254 |
|
---|
| 255 | LWIP_ASSERT("tcp_remove_listener: invalid listener", lpcb != NULL);
|
---|
| 256 |
|
---|
| 257 | for (pcb = list; pcb != NULL; pcb = pcb->next) {
|
---|
| 258 | if (pcb->listener == lpcb) {
|
---|
| 259 | pcb->listener = NULL;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 | #endif
|
---|
| 264 |
|
---|
| 265 | /** Called when a listen pcb is closed. Iterates all pcb lists and removes the
|
---|
| 266 | * closed listener pcb from pcb->listener if matching.
|
---|
| 267 | */
|
---|
| 268 | static void
|
---|
| 269 | tcp_listen_closed(struct tcp_pcb *pcb)
|
---|
| 270 | {
|
---|
| 271 | #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
|
---|
| 272 | size_t i;
|
---|
| 273 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 274 | LWIP_ASSERT("pcb->state == LISTEN", pcb->state == LISTEN);
|
---|
| 275 | for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) {
|
---|
| 276 | tcp_remove_listener(*tcp_pcb_lists[i], (struct tcp_pcb_listen *)pcb);
|
---|
| 277 | }
|
---|
| 278 | #endif
|
---|
| 279 | LWIP_UNUSED_ARG(pcb);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | #if TCP_LISTEN_BACKLOG
|
---|
| 283 | /** @ingroup tcp_raw
|
---|
| 284 | * Delay accepting a connection in respect to the listen backlog:
|
---|
| 285 | * the number of outstanding connections is increased until
|
---|
| 286 | * tcp_backlog_accepted() is called.
|
---|
| 287 | *
|
---|
| 288 | * ATTENTION: the caller is responsible for calling tcp_backlog_accepted()
|
---|
| 289 | * or else the backlog feature will get out of sync!
|
---|
| 290 | *
|
---|
| 291 | * @param pcb the connection pcb which is not fully accepted yet
|
---|
| 292 | */
|
---|
| 293 | void
|
---|
| 294 | tcp_backlog_delayed(struct tcp_pcb *pcb)
|
---|
| 295 | {
|
---|
| 296 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 297 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 298 | if ((pcb->flags & TF_BACKLOGPEND) == 0) {
|
---|
| 299 | if (pcb->listener != NULL) {
|
---|
| 300 | pcb->listener->accepts_pending++;
|
---|
| 301 | LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
|
---|
| 302 | tcp_set_flags(pcb, TF_BACKLOGPEND);
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /** @ingroup tcp_raw
|
---|
| 308 | * A delayed-accept a connection is accepted (or closed/aborted): decreases
|
---|
| 309 | * the number of outstanding connections after calling tcp_backlog_delayed().
|
---|
| 310 | *
|
---|
| 311 | * ATTENTION: the caller is responsible for calling tcp_backlog_accepted()
|
---|
| 312 | * or else the backlog feature will get out of sync!
|
---|
| 313 | *
|
---|
| 314 | * @param pcb the connection pcb which is now fully accepted (or closed/aborted)
|
---|
| 315 | */
|
---|
| 316 | void
|
---|
| 317 | tcp_backlog_accepted(struct tcp_pcb *pcb)
|
---|
| 318 | {
|
---|
| 319 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 320 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 321 | if ((pcb->flags & TF_BACKLOGPEND) != 0) {
|
---|
| 322 | if (pcb->listener != NULL) {
|
---|
| 323 | LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
|
---|
| 324 | pcb->listener->accepts_pending--;
|
---|
| 325 | tcp_clear_flags(pcb, TF_BACKLOGPEND);
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | #endif /* TCP_LISTEN_BACKLOG */
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | * Closes the TX side of a connection held by the PCB.
|
---|
| 333 | * For tcp_close(), a RST is sent if the application didn't receive all data
|
---|
| 334 | * (tcp_recved() not called for all data passed to recv callback).
|
---|
| 335 | *
|
---|
| 336 | * Listening pcbs are freed and may not be referenced any more.
|
---|
| 337 | * Connection pcbs are freed if not yet connected and may not be referenced
|
---|
| 338 | * any more. If a connection is established (at least SYN received or in
|
---|
| 339 | * a closing state), the connection is closed, and put in a closing state.
|
---|
| 340 | * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
|
---|
| 341 | * unsafe to reference it.
|
---|
| 342 | *
|
---|
| 343 | * @param pcb the tcp_pcb to close
|
---|
| 344 | * @return ERR_OK if connection has been closed
|
---|
| 345 | * another err_t if closing failed and pcb is not freed
|
---|
| 346 | */
|
---|
| 347 | static err_t
|
---|
| 348 | tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
|
---|
| 349 | {
|
---|
| 350 | LWIP_ASSERT("tcp_close_shutdown: invalid pcb", pcb != NULL);
|
---|
| 351 |
|
---|
| 352 | if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
|
---|
| 353 | if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
|
---|
| 354 | /* Not all data received by application, send RST to tell the remote
|
---|
| 355 | side about this. */
|
---|
| 356 | LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
|
---|
| 357 |
|
---|
| 358 | /* don't call tcp_abort here: we must not deallocate the pcb since
|
---|
| 359 | that might not be expected when calling tcp_close */
|
---|
| 360 | tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
|
---|
| 361 | pcb->local_port, pcb->remote_port);
|
---|
| 362 |
|
---|
| 363 | tcp_pcb_purge(pcb);
|
---|
| 364 | TCP_RMV_ACTIVE(pcb);
|
---|
| 365 | /* Deallocate the pcb since we already sent a RST for it */
|
---|
| 366 | if (tcp_input_pcb == pcb) {
|
---|
| 367 | /* prevent using a deallocated pcb: free it from tcp_input later */
|
---|
| 368 | tcp_trigger_input_pcb_close();
|
---|
| 369 | } else {
|
---|
| 370 | tcp_free(pcb);
|
---|
| 371 | }
|
---|
| 372 | return ERR_OK;
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /* - states which free the pcb are handled here,
|
---|
| 377 | - states which send FIN and change state are handled in tcp_close_shutdown_fin() */
|
---|
| 378 | switch (pcb->state) {
|
---|
| 379 | case CLOSED:
|
---|
| 380 | /* Closing a pcb in the CLOSED state might seem erroneous,
|
---|
| 381 | * however, it is in this state once allocated and as yet unused
|
---|
| 382 | * and the user needs some way to free it should the need arise.
|
---|
| 383 | * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
|
---|
| 384 | * or for a pcb that has been used and then entered the CLOSED state
|
---|
| 385 | * is erroneous, but this should never happen as the pcb has in those cases
|
---|
| 386 | * been freed, and so any remaining handles are bogus. */
|
---|
| 387 | if (pcb->local_port != 0) {
|
---|
| 388 | TCP_RMV(&tcp_bound_pcbs, pcb);
|
---|
| 389 | }
|
---|
| 390 | tcp_free(pcb);
|
---|
| 391 | break;
|
---|
| 392 | case LISTEN:
|
---|
| 393 | tcp_listen_closed(pcb);
|
---|
| 394 | tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
|
---|
| 395 | tcp_free_listen(pcb);
|
---|
| 396 | break;
|
---|
| 397 | case SYN_SENT:
|
---|
| 398 | TCP_PCB_REMOVE_ACTIVE(pcb);
|
---|
| 399 | tcp_free(pcb);
|
---|
| 400 | MIB2_STATS_INC(mib2.tcpattemptfails);
|
---|
| 401 | break;
|
---|
| 402 | default:
|
---|
| 403 | return tcp_close_shutdown_fin(pcb);
|
---|
| 404 | }
|
---|
| 405 | return ERR_OK;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | static err_t
|
---|
| 409 | tcp_close_shutdown_fin(struct tcp_pcb *pcb)
|
---|
| 410 | {
|
---|
| 411 | err_t err;
|
---|
| 412 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 413 |
|
---|
| 414 | switch (pcb->state) {
|
---|
| 415 | case SYN_RCVD:
|
---|
| 416 | err = tcp_send_fin(pcb);
|
---|
| 417 | if (err == ERR_OK) {
|
---|
| 418 | tcp_backlog_accepted(pcb);
|
---|
| 419 | MIB2_STATS_INC(mib2.tcpattemptfails);
|
---|
| 420 | pcb->state = FIN_WAIT_1;
|
---|
| 421 | }
|
---|
| 422 | break;
|
---|
| 423 | case ESTABLISHED:
|
---|
| 424 | err = tcp_send_fin(pcb);
|
---|
| 425 | if (err == ERR_OK) {
|
---|
| 426 | MIB2_STATS_INC(mib2.tcpestabresets);
|
---|
| 427 | pcb->state = FIN_WAIT_1;
|
---|
| 428 | }
|
---|
| 429 | break;
|
---|
| 430 | case CLOSE_WAIT:
|
---|
| 431 | err = tcp_send_fin(pcb);
|
---|
| 432 | if (err == ERR_OK) {
|
---|
| 433 | MIB2_STATS_INC(mib2.tcpestabresets);
|
---|
| 434 | pcb->state = LAST_ACK;
|
---|
| 435 | }
|
---|
| 436 | break;
|
---|
| 437 | default:
|
---|
| 438 | /* Has already been closed, do nothing. */
|
---|
| 439 | return ERR_OK;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | if (err == ERR_OK) {
|
---|
| 443 | /* To ensure all data has been sent when tcp_close returns, we have
|
---|
| 444 | to make sure tcp_output doesn't fail.
|
---|
| 445 | Since we don't really have to ensure all data has been sent when tcp_close
|
---|
| 446 | returns (unsent data is sent from tcp timer functions, also), we don't care
|
---|
| 447 | for the return value of tcp_output for now. */
|
---|
| 448 | tcp_output(pcb);
|
---|
| 449 | } else if (err == ERR_MEM) {
|
---|
| 450 | /* Mark this pcb for closing. Closing is retried from tcp_tmr. */
|
---|
| 451 | tcp_set_flags(pcb, TF_CLOSEPEND);
|
---|
| 452 | /* We have to return ERR_OK from here to indicate to the callers that this
|
---|
| 453 | pcb should not be used any more as it will be freed soon via tcp_tmr.
|
---|
| 454 | This is OK here since sending FIN does not guarantee a time frime for
|
---|
| 455 | actually freeing the pcb, either (it is left in closure states for
|
---|
| 456 | remote ACK or timeout) */
|
---|
| 457 | return ERR_OK;
|
---|
| 458 | }
|
---|
| 459 | return err;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | /**
|
---|
| 463 | * @ingroup tcp_raw
|
---|
| 464 | * Closes the connection held by the PCB.
|
---|
| 465 | *
|
---|
| 466 | * Listening pcbs are freed and may not be referenced any more.
|
---|
| 467 | * Connection pcbs are freed if not yet connected and may not be referenced
|
---|
| 468 | * any more. If a connection is established (at least SYN received or in
|
---|
| 469 | * a closing state), the connection is closed, and put in a closing state.
|
---|
| 470 | * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
|
---|
| 471 | * unsafe to reference it (unless an error is returned).
|
---|
| 472 | *
|
---|
| 473 | * The function may return ERR_MEM if no memory
|
---|
| 474 | * was available for closing the connection. If so, the application
|
---|
| 475 | * should wait and try again either by using the acknowledgment
|
---|
| 476 | * callback or the polling functionality. If the close succeeds, the
|
---|
| 477 | * function returns ERR_OK.
|
---|
| 478 | *
|
---|
| 479 | * @param pcb the tcp_pcb to close
|
---|
| 480 | * @return ERR_OK if connection has been closed
|
---|
| 481 | * another err_t if closing failed and pcb is not freed
|
---|
| 482 | */
|
---|
| 483 | err_t
|
---|
| 484 | tcp_close(struct tcp_pcb *pcb)
|
---|
| 485 | {
|
---|
| 486 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 487 |
|
---|
| 488 | LWIP_ERROR("tcp_close: invalid pcb", pcb != NULL, return ERR_ARG);
|
---|
| 489 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
|
---|
| 490 |
|
---|
| 491 | tcp_debug_print_state(pcb->state);
|
---|
| 492 |
|
---|
| 493 | if (pcb->state != LISTEN) {
|
---|
| 494 | /* Set a flag not to receive any more data... */
|
---|
| 495 | tcp_set_flags(pcb, TF_RXCLOSED);
|
---|
| 496 | }
|
---|
| 497 | /* ... and close */
|
---|
| 498 | return tcp_close_shutdown(pcb, 1);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | /**
|
---|
| 502 | * @ingroup tcp_raw
|
---|
| 503 | * Causes all or part of a full-duplex connection of this PCB to be shut down.
|
---|
| 504 | * This doesn't deallocate the PCB unless shutting down both sides!
|
---|
| 505 | * Shutting down both sides is the same as calling tcp_close, so if it succeds
|
---|
| 506 | * (i.e. returns ER_OK), the PCB must not be referenced any more!
|
---|
| 507 | *
|
---|
| 508 | * @param pcb PCB to shutdown
|
---|
| 509 | * @param shut_rx shut down receive side if this is != 0
|
---|
| 510 | * @param shut_tx shut down send side if this is != 0
|
---|
| 511 | * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
|
---|
| 512 | * another err_t on error.
|
---|
| 513 | */
|
---|
| 514 | err_t
|
---|
| 515 | tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
|
---|
| 516 | {
|
---|
| 517 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 518 |
|
---|
| 519 | LWIP_ERROR("tcp_shutdown: invalid pcb", pcb != NULL, return ERR_ARG);
|
---|
| 520 |
|
---|
| 521 | if (pcb->state == LISTEN) {
|
---|
| 522 | return ERR_CONN;
|
---|
| 523 | }
|
---|
| 524 | if (shut_rx) {
|
---|
| 525 | /* shut down the receive side: set a flag not to receive any more data... */
|
---|
| 526 | tcp_set_flags(pcb, TF_RXCLOSED);
|
---|
| 527 | if (shut_tx) {
|
---|
| 528 | /* shutting down the tx AND rx side is the same as closing for the raw API */
|
---|
| 529 | return tcp_close_shutdown(pcb, 1);
|
---|
| 530 | }
|
---|
| 531 | /* ... and free buffered data */
|
---|
| 532 | if (pcb->refused_data != NULL) {
|
---|
| 533 | pbuf_free(pcb->refused_data);
|
---|
| 534 | pcb->refused_data = NULL;
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | if (shut_tx) {
|
---|
| 538 | /* This can't happen twice since if it succeeds, the pcb's state is changed.
|
---|
| 539 | Only close in these states as the others directly deallocate the PCB */
|
---|
| 540 | switch (pcb->state) {
|
---|
| 541 | case SYN_RCVD:
|
---|
| 542 | case ESTABLISHED:
|
---|
| 543 | case CLOSE_WAIT:
|
---|
| 544 | return tcp_close_shutdown(pcb, (u8_t)shut_rx);
|
---|
| 545 | default:
|
---|
| 546 | /* Not (yet?) connected, cannot shutdown the TX side as that would bring us
|
---|
| 547 | into CLOSED state, where the PCB is deallocated. */
|
---|
| 548 | return ERR_CONN;
|
---|
| 549 | }
|
---|
| 550 | }
|
---|
| 551 | return ERR_OK;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | /**
|
---|
| 555 | * Abandons a connection and optionally sends a RST to the remote
|
---|
| 556 | * host. Deletes the local protocol control block. This is done when
|
---|
| 557 | * a connection is killed because of shortage of memory.
|
---|
| 558 | *
|
---|
| 559 | * @param pcb the tcp_pcb to abort
|
---|
| 560 | * @param reset boolean to indicate whether a reset should be sent
|
---|
| 561 | */
|
---|
| 562 | void
|
---|
| 563 | tcp_abandon(struct tcp_pcb *pcb, int reset)
|
---|
| 564 | {
|
---|
| 565 | u32_t seqno, ackno;
|
---|
| 566 | #if LWIP_CALLBACK_API
|
---|
| 567 | tcp_err_fn errf;
|
---|
| 568 | #endif /* LWIP_CALLBACK_API */
|
---|
| 569 | void *errf_arg;
|
---|
| 570 |
|
---|
| 571 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 572 |
|
---|
| 573 | LWIP_ERROR("tcp_abandon: invalid pcb", pcb != NULL, return);
|
---|
| 574 |
|
---|
| 575 | /* pcb->state LISTEN not allowed here */
|
---|
| 576 | LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
|
---|
| 577 | pcb->state != LISTEN);
|
---|
| 578 | /* Figure out on which TCP PCB list we are, and remove us. If we
|
---|
| 579 | are in an active state, call the receive function associated with
|
---|
| 580 | the PCB with a NULL argument, and send an RST to the remote end. */
|
---|
| 581 | if (pcb->state == TIME_WAIT) {
|
---|
| 582 | tcp_pcb_remove(&tcp_tw_pcbs, pcb);
|
---|
| 583 | tcp_free(pcb);
|
---|
| 584 | } else {
|
---|
| 585 | int send_rst = 0;
|
---|
| 586 | u16_t local_port = 0;
|
---|
| 587 | enum tcp_state last_state;
|
---|
| 588 | seqno = pcb->snd_nxt;
|
---|
| 589 | ackno = pcb->rcv_nxt;
|
---|
| 590 | #if LWIP_CALLBACK_API
|
---|
| 591 | errf = pcb->errf;
|
---|
| 592 | #endif /* LWIP_CALLBACK_API */
|
---|
| 593 | errf_arg = pcb->callback_arg;
|
---|
| 594 | if (pcb->state == CLOSED) {
|
---|
| 595 | if (pcb->local_port != 0) {
|
---|
| 596 | /* bound, not yet opened */
|
---|
| 597 | TCP_RMV(&tcp_bound_pcbs, pcb);
|
---|
| 598 | }
|
---|
| 599 | } else {
|
---|
| 600 | send_rst = reset;
|
---|
| 601 | local_port = pcb->local_port;
|
---|
| 602 | TCP_PCB_REMOVE_ACTIVE(pcb);
|
---|
| 603 | }
|
---|
| 604 | if (pcb->unacked != NULL) {
|
---|
| 605 | tcp_segs_free(pcb->unacked);
|
---|
| 606 | }
|
---|
| 607 | if (pcb->unsent != NULL) {
|
---|
| 608 | tcp_segs_free(pcb->unsent);
|
---|
| 609 | }
|
---|
| 610 | #if TCP_QUEUE_OOSEQ
|
---|
| 611 | if (pcb->ooseq != NULL) {
|
---|
| 612 | tcp_segs_free(pcb->ooseq);
|
---|
| 613 | }
|
---|
| 614 | #endif /* TCP_QUEUE_OOSEQ */
|
---|
| 615 | tcp_backlog_accepted(pcb);
|
---|
| 616 | if (send_rst) {
|
---|
| 617 | LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
|
---|
| 618 | tcp_rst(pcb, seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
|
---|
| 619 | }
|
---|
| 620 | last_state = pcb->state;
|
---|
| 621 | tcp_free(pcb);
|
---|
| 622 | TCP_EVENT_ERR(last_state, errf, errf_arg, ERR_ABRT);
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | /**
|
---|
| 627 | * @ingroup tcp_raw
|
---|
| 628 | * Aborts the connection by sending a RST (reset) segment to the remote
|
---|
| 629 | * host. The pcb is deallocated. This function never fails.
|
---|
| 630 | *
|
---|
| 631 | * ATTENTION: When calling this from one of the TCP callbacks, make
|
---|
| 632 | * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
|
---|
| 633 | * or you will risk accessing deallocated memory or memory leaks!
|
---|
| 634 | *
|
---|
| 635 | * @param pcb the tcp pcb to abort
|
---|
| 636 | */
|
---|
| 637 | void
|
---|
| 638 | tcp_abort(struct tcp_pcb *pcb)
|
---|
| 639 | {
|
---|
| 640 | tcp_abandon(pcb, 1);
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /**
|
---|
| 644 | * @ingroup tcp_raw
|
---|
| 645 | * Binds the connection to a local port number and IP address. If the
|
---|
| 646 | * IP address is not given (i.e., ipaddr == IP_ANY_TYPE), the connection is
|
---|
| 647 | * bound to all local IP addresses.
|
---|
| 648 | * If another connection is bound to the same port, the function will
|
---|
| 649 | * return ERR_USE, otherwise ERR_OK is returned.
|
---|
| 650 | *
|
---|
| 651 | * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
|
---|
| 652 | * already bound!)
|
---|
| 653 | * @param ipaddr the local ip address to bind to (use IPx_ADDR_ANY to bind
|
---|
| 654 | * to any local address
|
---|
| 655 | * @param port the local port to bind to
|
---|
| 656 | * @return ERR_USE if the port is already in use
|
---|
| 657 | * ERR_VAL if bind failed because the PCB is not in a valid state
|
---|
| 658 | * ERR_OK if bound
|
---|
| 659 | */
|
---|
| 660 | err_t
|
---|
| 661 | tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
|
---|
| 662 | {
|
---|
| 663 | int i;
|
---|
| 664 | int max_pcb_list = NUM_TCP_PCB_LISTS;
|
---|
| 665 | struct tcp_pcb *cpcb;
|
---|
| 666 | #if LWIP_IPV6 && LWIP_IPV6_SCOPES
|
---|
| 667 | ip_addr_t zoned_ipaddr;
|
---|
| 668 | #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
|
---|
| 669 |
|
---|
| 670 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 671 |
|
---|
| 672 | #if LWIP_IPV4
|
---|
| 673 | /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
|
---|
| 674 | if (ipaddr == NULL) {
|
---|
| 675 | ipaddr = IP4_ADDR_ANY;
|
---|
| 676 | }
|
---|
| 677 | #else /* LWIP_IPV4 */
|
---|
| 678 | LWIP_ERROR("tcp_bind: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
|
---|
| 679 | #endif /* LWIP_IPV4 */
|
---|
| 680 |
|
---|
| 681 | LWIP_ERROR("tcp_bind: invalid pcb", pcb != NULL, return ERR_ARG);
|
---|
| 682 |
|
---|
| 683 | LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
|
---|
| 684 |
|
---|
| 685 | #if SO_REUSE
|
---|
| 686 | /* Unless the REUSEADDR flag is set,
|
---|
| 687 | we have to check the pcbs in TIME-WAIT state, also.
|
---|
| 688 | We do not dump TIME_WAIT pcb's; they can still be matched by incoming
|
---|
| 689 | packets using both local and remote IP addresses and ports to distinguish.
|
---|
| 690 | */
|
---|
| 691 | if (ip_get_option(pcb, SOF_REUSEADDR)) {
|
---|
| 692 | max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
|
---|
| 693 | }
|
---|
| 694 | #endif /* SO_REUSE */
|
---|
| 695 |
|
---|
| 696 | #if LWIP_IPV6 && LWIP_IPV6_SCOPES
|
---|
| 697 | /* If the given IP address should have a zone but doesn't, assign one now.
|
---|
| 698 | * This is legacy support: scope-aware callers should always provide properly
|
---|
| 699 | * zoned source addresses. Do the zone selection before the address-in-use
|
---|
| 700 | * check below; as such we have to make a temporary copy of the address. */
|
---|
| 701 | if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNICAST)) {
|
---|
| 702 | ip_addr_copy(zoned_ipaddr, *ipaddr);
|
---|
| 703 | ip6_addr_select_zone(ip_2_ip6(&zoned_ipaddr), ip_2_ip6(&zoned_ipaddr));
|
---|
| 704 | ipaddr = &zoned_ipaddr;
|
---|
| 705 | }
|
---|
| 706 | #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
|
---|
| 707 |
|
---|
| 708 | if (port == 0) {
|
---|
| 709 | port = tcp_new_port();
|
---|
| 710 | if (port == 0) {
|
---|
| 711 | return ERR_BUF;
|
---|
| 712 | }
|
---|
| 713 | } else {
|
---|
| 714 | /* Check if the address already is in use (on all lists) */
|
---|
| 715 | for (i = 0; i < max_pcb_list; i++) {
|
---|
| 716 | for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
|
---|
| 717 | if (cpcb->local_port == port) {
|
---|
| 718 | #if SO_REUSE
|
---|
| 719 | /* Omit checking for the same port if both pcbs have REUSEADDR set.
|
---|
| 720 | For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
|
---|
| 721 | tcp_connect. */
|
---|
| 722 | if (!ip_get_option(pcb, SOF_REUSEADDR) ||
|
---|
| 723 | !ip_get_option(cpcb, SOF_REUSEADDR))
|
---|
| 724 | #endif /* SO_REUSE */
|
---|
| 725 | {
|
---|
| 726 | /* @todo: check accept_any_ip_version */
|
---|
| 727 | if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) &&
|
---|
| 728 | (ip_addr_isany(&cpcb->local_ip) ||
|
---|
| 729 | ip_addr_isany(ipaddr) ||
|
---|
| 730 | ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
|
---|
| 731 | return ERR_USE;
|
---|
| 732 | }
|
---|
| 733 | }
|
---|
| 734 | }
|
---|
| 735 | }
|
---|
| 736 | }
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | if (!ip_addr_isany(ipaddr)
|
---|
| 740 | #if LWIP_IPV4 && LWIP_IPV6
|
---|
| 741 | || (IP_GET_TYPE(ipaddr) != IP_GET_TYPE(&pcb->local_ip))
|
---|
| 742 | #endif /* LWIP_IPV4 && LWIP_IPV6 */
|
---|
| 743 | ) {
|
---|
| 744 | ip_addr_set(&pcb->local_ip, ipaddr);
|
---|
| 745 | }
|
---|
| 746 | pcb->local_port = port;
|
---|
| 747 | TCP_REG(&tcp_bound_pcbs, pcb);
|
---|
| 748 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
|
---|
| 749 | return ERR_OK;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | /**
|
---|
| 753 | * @ingroup tcp_raw
|
---|
| 754 | * Binds the connection to a netif and IP address.
|
---|
| 755 | * After calling this function, all packets received via this PCB
|
---|
| 756 | * are guaranteed to have come in via the specified netif, and all
|
---|
| 757 | * outgoing packets will go out via the specified netif.
|
---|
| 758 | *
|
---|
| 759 | * @param pcb the tcp_pcb to bind.
|
---|
| 760 | * @param netif the netif to bind to. Can be NULL.
|
---|
| 761 | */
|
---|
| 762 | void
|
---|
| 763 | tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif)
|
---|
| 764 | {
|
---|
| 765 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 766 | if (netif != NULL) {
|
---|
| 767 | pcb->netif_idx = netif_get_index(netif);
|
---|
| 768 | } else {
|
---|
| 769 | pcb->netif_idx = NETIF_NO_INDEX;
|
---|
| 770 | }
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | #if LWIP_CALLBACK_API
|
---|
| 774 | /**
|
---|
| 775 | * Default accept callback if no accept callback is specified by the user.
|
---|
| 776 | */
|
---|
| 777 | static err_t
|
---|
| 778 | tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
|
---|
| 779 | {
|
---|
| 780 | LWIP_UNUSED_ARG(arg);
|
---|
| 781 | LWIP_UNUSED_ARG(err);
|
---|
| 782 |
|
---|
| 783 | LWIP_ASSERT("tcp_accept_null: invalid pcb", pcb != NULL);
|
---|
| 784 |
|
---|
| 785 | tcp_abort(pcb);
|
---|
| 786 |
|
---|
| 787 | return ERR_ABRT;
|
---|
| 788 | }
|
---|
| 789 | #endif /* LWIP_CALLBACK_API */
|
---|
| 790 |
|
---|
| 791 | /**
|
---|
| 792 | * @ingroup tcp_raw
|
---|
| 793 | * Set the state of the connection to be LISTEN, which means that it
|
---|
| 794 | * is able to accept incoming connections. The protocol control block
|
---|
| 795 | * is reallocated in order to consume less memory. Setting the
|
---|
| 796 | * connection to LISTEN is an irreversible process.
|
---|
| 797 | * When an incoming connection is accepted, the function specified with
|
---|
| 798 | * the tcp_accept() function will be called. The pcb has to be bound
|
---|
| 799 | * to a local port with the tcp_bind() function.
|
---|
| 800 | *
|
---|
| 801 | * The tcp_listen() function returns a new connection identifier, and
|
---|
| 802 | * the one passed as an argument to the function will be
|
---|
| 803 | * deallocated. The reason for this behavior is that less memory is
|
---|
| 804 | * needed for a connection that is listening, so tcp_listen() will
|
---|
| 805 | * reclaim the memory needed for the original connection and allocate a
|
---|
| 806 | * new smaller memory block for the listening connection.
|
---|
| 807 | *
|
---|
| 808 | * tcp_listen() may return NULL if no memory was available for the
|
---|
| 809 | * listening connection. If so, the memory associated with the pcb
|
---|
| 810 | * passed as an argument to tcp_listen() will not be deallocated.
|
---|
| 811 | *
|
---|
| 812 | * The backlog limits the number of outstanding connections
|
---|
| 813 | * in the listen queue to the value specified by the backlog argument.
|
---|
| 814 | * To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
|
---|
| 815 | *
|
---|
| 816 | * @param pcb the original tcp_pcb
|
---|
| 817 | * @param backlog the incoming connections queue limit
|
---|
| 818 | * @return tcp_pcb used for listening, consumes less memory.
|
---|
| 819 | *
|
---|
| 820 | * @note The original tcp_pcb is freed. This function therefore has to be
|
---|
| 821 | * called like this:
|
---|
| 822 | * tpcb = tcp_listen_with_backlog(tpcb, backlog);
|
---|
| 823 | */
|
---|
| 824 | struct tcp_pcb *
|
---|
| 825 | tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
|
---|
| 826 | {
|
---|
| 827 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 828 | return tcp_listen_with_backlog_and_err(pcb, backlog, NULL);
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 | /**
|
---|
| 832 | * @ingroup tcp_raw
|
---|
| 833 | * Set the state of the connection to be LISTEN, which means that it
|
---|
| 834 | * is able to accept incoming connections. The protocol control block
|
---|
| 835 | * is reallocated in order to consume less memory. Setting the
|
---|
| 836 | * connection to LISTEN is an irreversible process.
|
---|
| 837 | *
|
---|
| 838 | * @param pcb the original tcp_pcb
|
---|
| 839 | * @param backlog the incoming connections queue limit
|
---|
| 840 | * @param err when NULL is returned, this contains the error reason
|
---|
| 841 | * @return tcp_pcb used for listening, consumes less memory.
|
---|
| 842 | *
|
---|
| 843 | * @note The original tcp_pcb is freed. This function therefore has to be
|
---|
| 844 | * called like this:
|
---|
| 845 | * tpcb = tcp_listen_with_backlog_and_err(tpcb, backlog, &err);
|
---|
| 846 | */
|
---|
| 847 | struct tcp_pcb *
|
---|
| 848 | tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err)
|
---|
| 849 | {
|
---|
| 850 | struct tcp_pcb_listen *lpcb = NULL;
|
---|
| 851 | err_t res;
|
---|
| 852 |
|
---|
| 853 | LWIP_UNUSED_ARG(backlog);
|
---|
| 854 |
|
---|
| 855 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 856 |
|
---|
| 857 | LWIP_ERROR("tcp_listen_with_backlog_and_err: invalid pcb", pcb != NULL, res = ERR_ARG; goto done);
|
---|
| 858 | LWIP_ERROR("tcp_listen_with_backlog_and_err: pcb already connected", pcb->state == CLOSED, res = ERR_CLSD; goto done);
|
---|
| 859 |
|
---|
| 860 | /* already listening? */
|
---|
| 861 | if (pcb->state == LISTEN) {
|
---|
| 862 | lpcb = (struct tcp_pcb_listen *)pcb;
|
---|
| 863 | res = ERR_ALREADY;
|
---|
| 864 | goto done;
|
---|
| 865 | }
|
---|
| 866 | #if SO_REUSE
|
---|
| 867 | if (ip_get_option(pcb, SOF_REUSEADDR)) {
|
---|
| 868 | /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
|
---|
| 869 | is declared (listen-/connection-pcb), we have to make sure now that
|
---|
| 870 | this port is only used once for every local IP. */
|
---|
| 871 | for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
|
---|
| 872 | if ((lpcb->local_port == pcb->local_port) &&
|
---|
| 873 | ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
|
---|
| 874 | /* this address/port is already used */
|
---|
| 875 | lpcb = NULL;
|
---|
| 876 | res = ERR_USE;
|
---|
| 877 | goto done;
|
---|
| 878 | }
|
---|
| 879 | }
|
---|
| 880 | }
|
---|
| 881 | #endif /* SO_REUSE */
|
---|
| 882 | lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
|
---|
| 883 | if (lpcb == NULL) {
|
---|
| 884 | res = ERR_MEM;
|
---|
| 885 | goto done;
|
---|
| 886 | }
|
---|
| 887 | lpcb->callback_arg = pcb->callback_arg;
|
---|
| 888 | lpcb->local_port = pcb->local_port;
|
---|
| 889 | lpcb->state = LISTEN;
|
---|
| 890 | lpcb->prio = pcb->prio;
|
---|
| 891 | lpcb->so_options = pcb->so_options;
|
---|
| 892 | lpcb->netif_idx = NETIF_NO_INDEX;
|
---|
| 893 | lpcb->ttl = pcb->ttl;
|
---|
| 894 | lpcb->tos = pcb->tos;
|
---|
| 895 | #if LWIP_IPV4 && LWIP_IPV6
|
---|
| 896 | IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type);
|
---|
| 897 | #endif /* LWIP_IPV4 && LWIP_IPV6 */
|
---|
| 898 | ip_addr_copy(lpcb->local_ip, pcb->local_ip);
|
---|
| 899 | if (pcb->local_port != 0) {
|
---|
| 900 | TCP_RMV(&tcp_bound_pcbs, pcb);
|
---|
| 901 | }
|
---|
| 902 | #if LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
| 903 | /* copy over ext_args to listening pcb */
|
---|
| 904 | memcpy(&lpcb->ext_args, &pcb->ext_args, sizeof(pcb->ext_args));
|
---|
| 905 | #endif
|
---|
| 906 | tcp_free(pcb);
|
---|
| 907 | #if LWIP_CALLBACK_API
|
---|
| 908 | lpcb->accept = tcp_accept_null;
|
---|
| 909 | #endif /* LWIP_CALLBACK_API */
|
---|
| 910 | #if TCP_LISTEN_BACKLOG
|
---|
| 911 | lpcb->accepts_pending = 0;
|
---|
| 912 | tcp_backlog_set(lpcb, backlog);
|
---|
| 913 | #endif /* TCP_LISTEN_BACKLOG */
|
---|
| 914 | TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
|
---|
| 915 | res = ERR_OK;
|
---|
| 916 | done:
|
---|
| 917 | if (err != NULL) {
|
---|
| 918 | *err = res;
|
---|
| 919 | }
|
---|
| 920 | return (struct tcp_pcb *)lpcb;
|
---|
| 921 | }
|
---|
| 922 |
|
---|
| 923 | /**
|
---|
| 924 | * Update the state that tracks the available window space to advertise.
|
---|
| 925 | *
|
---|
| 926 | * Returns how much extra window would be advertised if we sent an
|
---|
| 927 | * update now.
|
---|
| 928 | */
|
---|
| 929 | u32_t
|
---|
| 930 | tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
|
---|
| 931 | {
|
---|
| 932 | u32_t new_right_edge;
|
---|
| 933 |
|
---|
| 934 | LWIP_ASSERT("tcp_update_rcv_ann_wnd: invalid pcb", pcb != NULL);
|
---|
| 935 | new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
|
---|
| 936 |
|
---|
| 937 | if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
|
---|
| 938 | /* we can advertise more window */
|
---|
| 939 | pcb->rcv_ann_wnd = pcb->rcv_wnd;
|
---|
| 940 | return new_right_edge - pcb->rcv_ann_right_edge;
|
---|
| 941 | } else {
|
---|
| 942 | if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
|
---|
| 943 | /* Can happen due to other end sending out of advertised window,
|
---|
| 944 | * but within actual available (but not yet advertised) window */
|
---|
| 945 | pcb->rcv_ann_wnd = 0;
|
---|
| 946 | } else {
|
---|
| 947 | /* keep the right edge of window constant */
|
---|
| 948 | u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
|
---|
| 949 | #if !LWIP_WND_SCALE
|
---|
| 950 | LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
|
---|
| 951 | #endif
|
---|
| 952 | pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
|
---|
| 953 | }
|
---|
| 954 | return 0;
|
---|
| 955 | }
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 | /**
|
---|
| 959 | * @ingroup tcp_raw
|
---|
| 960 | * This function should be called by the application when it has
|
---|
| 961 | * processed the data. The purpose is to advertise a larger window
|
---|
| 962 | * when the data has been processed.
|
---|
| 963 | *
|
---|
| 964 | * @param pcb the tcp_pcb for which data is read
|
---|
| 965 | * @param len the amount of bytes that have been read by the application
|
---|
| 966 | */
|
---|
| 967 | void
|
---|
| 968 | tcp_recved(struct tcp_pcb *pcb, u16_t len)
|
---|
| 969 | {
|
---|
| 970 | u32_t wnd_inflation;
|
---|
| 971 | tcpwnd_size_t rcv_wnd;
|
---|
| 972 |
|
---|
| 973 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 974 |
|
---|
| 975 | LWIP_ERROR("tcp_recved: invalid pcb", pcb != NULL, return);
|
---|
| 976 |
|
---|
| 977 | /* pcb->state LISTEN not allowed here */
|
---|
| 978 | LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
|
---|
| 979 | pcb->state != LISTEN);
|
---|
| 980 |
|
---|
| 981 | rcv_wnd = (tcpwnd_size_t)(pcb->rcv_wnd + len);
|
---|
| 982 | if ((rcv_wnd > TCP_WND_MAX(pcb)) || (rcv_wnd < pcb->rcv_wnd)) {
|
---|
| 983 | /* window got too big or tcpwnd_size_t overflow */
|
---|
| 984 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: window got too big or tcpwnd_size_t overflow\n"));
|
---|
| 985 | pcb->rcv_wnd = TCP_WND_MAX(pcb);
|
---|
| 986 | } else {
|
---|
| 987 | pcb->rcv_wnd = rcv_wnd;
|
---|
| 988 | }
|
---|
| 989 |
|
---|
| 990 | wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
|
---|
| 991 |
|
---|
| 992 | /* If the change in the right edge of window is significant (default
|
---|
| 993 | * watermark is TCP_WND/4), then send an explicit update now.
|
---|
| 994 | * Otherwise wait for a packet to be sent in the normal course of
|
---|
| 995 | * events (or more window to be available later) */
|
---|
| 996 | if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
|
---|
| 997 | tcp_ack_now(pcb);
|
---|
| 998 | tcp_output(pcb);
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n",
|
---|
| 1002 | len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd)));
|
---|
| 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | /**
|
---|
| 1006 | * Allocate a new local TCP port.
|
---|
| 1007 | *
|
---|
| 1008 | * @return a new (free) local TCP port number
|
---|
| 1009 | */
|
---|
| 1010 | static u16_t
|
---|
| 1011 | tcp_new_port(void)
|
---|
| 1012 | {
|
---|
| 1013 | u8_t i;
|
---|
| 1014 | u16_t n = 0;
|
---|
| 1015 | struct tcp_pcb *pcb;
|
---|
| 1016 |
|
---|
| 1017 | again:
|
---|
| 1018 | tcp_port++;
|
---|
| 1019 | if (tcp_port == TCP_LOCAL_PORT_RANGE_END) {
|
---|
| 1020 | tcp_port = TCP_LOCAL_PORT_RANGE_START;
|
---|
| 1021 | }
|
---|
| 1022 | /* Check all PCB lists. */
|
---|
| 1023 | for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
|
---|
| 1024 | for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
|
---|
| 1025 | if (pcb->local_port == tcp_port) {
|
---|
| 1026 | n++;
|
---|
| 1027 | if (n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
|
---|
| 1028 | return 0;
|
---|
| 1029 | }
|
---|
| 1030 | goto again;
|
---|
| 1031 | }
|
---|
| 1032 | }
|
---|
| 1033 | }
|
---|
| 1034 | return tcp_port;
|
---|
| 1035 | }
|
---|
| 1036 |
|
---|
| 1037 | /**
|
---|
| 1038 | * @ingroup tcp_raw
|
---|
| 1039 | * Connects to another host. The function given as the "connected"
|
---|
| 1040 | * argument will be called when the connection has been established.
|
---|
| 1041 | * Sets up the pcb to connect to the remote host and sends the
|
---|
| 1042 | * initial SYN segment which opens the connection.
|
---|
| 1043 | *
|
---|
| 1044 | * The tcp_connect() function returns immediately; it does not wait for
|
---|
| 1045 | * the connection to be properly setup. Instead, it will call the
|
---|
| 1046 | * function specified as the fourth argument (the "connected" argument)
|
---|
| 1047 | * when the connection is established. If the connection could not be
|
---|
| 1048 | * properly established, either because the other host refused the
|
---|
| 1049 | * connection or because the other host didn't answer, the "err"
|
---|
| 1050 | * callback function of this pcb (registered with tcp_err, see below)
|
---|
| 1051 | * will be called.
|
---|
| 1052 | *
|
---|
| 1053 | * The tcp_connect() function can return ERR_MEM if no memory is
|
---|
| 1054 | * available for enqueueing the SYN segment. If the SYN indeed was
|
---|
| 1055 | * enqueued successfully, the tcp_connect() function returns ERR_OK.
|
---|
| 1056 | *
|
---|
| 1057 | * @param pcb the tcp_pcb used to establish the connection
|
---|
| 1058 | * @param ipaddr the remote ip address to connect to
|
---|
| 1059 | * @param port the remote tcp port to connect to
|
---|
| 1060 | * @param connected callback function to call when connected (on error,
|
---|
| 1061 | the err calback will be called)
|
---|
| 1062 | * @return ERR_VAL if invalid arguments are given
|
---|
| 1063 | * ERR_OK if connect request has been sent
|
---|
| 1064 | * other err_t values if connect request couldn't be sent
|
---|
| 1065 | */
|
---|
| 1066 | err_t
|
---|
| 1067 | tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
|
---|
| 1068 | tcp_connected_fn connected)
|
---|
| 1069 | {
|
---|
| 1070 | struct netif *netif = NULL;
|
---|
| 1071 | err_t ret;
|
---|
| 1072 | u32_t iss;
|
---|
| 1073 | u16_t old_local_port;
|
---|
| 1074 |
|
---|
| 1075 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 1076 |
|
---|
| 1077 | LWIP_ERROR("tcp_connect: invalid pcb", pcb != NULL, return ERR_ARG);
|
---|
| 1078 | LWIP_ERROR("tcp_connect: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
|
---|
| 1079 |
|
---|
| 1080 | LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
|
---|
| 1081 |
|
---|
| 1082 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
|
---|
| 1083 | ip_addr_set(&pcb->remote_ip, ipaddr);
|
---|
| 1084 | pcb->remote_port = port;
|
---|
| 1085 |
|
---|
| 1086 | if (pcb->netif_idx != NETIF_NO_INDEX) {
|
---|
| 1087 | netif = netif_get_by_index(pcb->netif_idx);
|
---|
| 1088 | } else {
|
---|
| 1089 | /* check if we have a route to the remote host */
|
---|
| 1090 | netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
|
---|
| 1091 | }
|
---|
| 1092 | if (netif == NULL) {
|
---|
| 1093 | /* Don't even try to send a SYN packet if we have no route since that will fail. */
|
---|
| 1094 | return ERR_RTE;
|
---|
| 1095 | }
|
---|
| 1096 |
|
---|
| 1097 | /* check if local IP has been assigned to pcb, if not, get one */
|
---|
| 1098 | if (ip_addr_isany(&pcb->local_ip)) {
|
---|
| 1099 | const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, ipaddr);
|
---|
| 1100 | if (local_ip == NULL) {
|
---|
| 1101 | return ERR_RTE;
|
---|
| 1102 | }
|
---|
| 1103 | ip_addr_copy(pcb->local_ip, *local_ip);
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
| 1106 | #if LWIP_IPV6 && LWIP_IPV6_SCOPES
|
---|
| 1107 | /* If the given IP address should have a zone but doesn't, assign one now.
|
---|
| 1108 | * Given that we already have the target netif, this is easy and cheap. */
|
---|
| 1109 | if (IP_IS_V6(&pcb->remote_ip) &&
|
---|
| 1110 | ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST)) {
|
---|
| 1111 | ip6_addr_assign_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST, netif);
|
---|
| 1112 | }
|
---|
| 1113 | #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
|
---|
| 1114 |
|
---|
| 1115 | old_local_port = pcb->local_port;
|
---|
| 1116 | if (pcb->local_port == 0) {
|
---|
| 1117 | pcb->local_port = tcp_new_port();
|
---|
| 1118 | if (pcb->local_port == 0) {
|
---|
| 1119 | return ERR_BUF;
|
---|
| 1120 | }
|
---|
| 1121 | } else {
|
---|
| 1122 | #if SO_REUSE
|
---|
| 1123 | if (ip_get_option(pcb, SOF_REUSEADDR)) {
|
---|
| 1124 | /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
|
---|
| 1125 | now that the 5-tuple is unique. */
|
---|
| 1126 | struct tcp_pcb *cpcb;
|
---|
| 1127 | int i;
|
---|
| 1128 | /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
|
---|
| 1129 | for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
|
---|
| 1130 | for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
|
---|
| 1131 | if ((cpcb->local_port == pcb->local_port) &&
|
---|
| 1132 | (cpcb->remote_port == port) &&
|
---|
| 1133 | ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
|
---|
| 1134 | ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
|
---|
| 1135 | /* linux returns EISCONN here, but ERR_USE should be OK for us */
|
---|
| 1136 | return ERR_USE;
|
---|
| 1137 | }
|
---|
| 1138 | }
|
---|
| 1139 | }
|
---|
| 1140 | }
|
---|
| 1141 | #endif /* SO_REUSE */
|
---|
| 1142 | }
|
---|
| 1143 |
|
---|
| 1144 | iss = tcp_next_iss(pcb);
|
---|
| 1145 | pcb->rcv_nxt = 0;
|
---|
| 1146 | pcb->snd_nxt = iss;
|
---|
| 1147 | pcb->lastack = iss - 1;
|
---|
| 1148 | pcb->snd_wl2 = iss - 1;
|
---|
| 1149 | pcb->snd_lbb = iss - 1;
|
---|
| 1150 | /* Start with a window that does not need scaling. When window scaling is
|
---|
| 1151 | enabled and used, the window is enlarged when both sides agree on scaling. */
|
---|
| 1152 | pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
|
---|
| 1153 | pcb->rcv_ann_right_edge = pcb->rcv_nxt;
|
---|
| 1154 | pcb->snd_wnd = TCP_WND;
|
---|
| 1155 | /* As initial send MSS, we use TCP_MSS but limit it to 536.
|
---|
| 1156 | The send MSS is updated when an MSS option is received. */
|
---|
| 1157 | pcb->mss = INITIAL_MSS;
|
---|
| 1158 | #if TCP_CALCULATE_EFF_SEND_MSS
|
---|
| 1159 | pcb->mss = tcp_eff_send_mss_netif(pcb->mss, netif, &pcb->remote_ip);
|
---|
| 1160 | #endif /* TCP_CALCULATE_EFF_SEND_MSS */
|
---|
| 1161 | pcb->cwnd = 1;
|
---|
| 1162 | #if LWIP_CALLBACK_API
|
---|
| 1163 | pcb->connected = connected;
|
---|
| 1164 | #else /* LWIP_CALLBACK_API */
|
---|
| 1165 | LWIP_UNUSED_ARG(connected);
|
---|
| 1166 | #endif /* LWIP_CALLBACK_API */
|
---|
| 1167 |
|
---|
| 1168 | /* Send a SYN together with the MSS option. */
|
---|
| 1169 | ret = tcp_enqueue_flags(pcb, TCP_SYN);
|
---|
| 1170 | if (ret == ERR_OK) {
|
---|
| 1171 | /* SYN segment was enqueued, changed the pcbs state now */
|
---|
| 1172 | pcb->state = SYN_SENT;
|
---|
| 1173 | if (old_local_port != 0) {
|
---|
| 1174 | TCP_RMV(&tcp_bound_pcbs, pcb);
|
---|
| 1175 | }
|
---|
| 1176 | TCP_REG_ACTIVE(pcb);
|
---|
| 1177 | MIB2_STATS_INC(mib2.tcpactiveopens);
|
---|
| 1178 |
|
---|
| 1179 | tcp_output(pcb);
|
---|
| 1180 | }
|
---|
| 1181 | return ret;
|
---|
| 1182 | }
|
---|
| 1183 |
|
---|
| 1184 | /**
|
---|
| 1185 | * Called every 500 ms and implements the retransmission timer and the timer that
|
---|
| 1186 | * removes PCBs that have been in TIME-WAIT for enough time. It also increments
|
---|
| 1187 | * various timers such as the inactivity timer in each PCB.
|
---|
| 1188 | *
|
---|
| 1189 | * Automatically called from tcp_tmr().
|
---|
| 1190 | */
|
---|
| 1191 | void
|
---|
| 1192 | tcp_slowtmr(void)
|
---|
| 1193 | {
|
---|
| 1194 | struct tcp_pcb *pcb, *prev;
|
---|
| 1195 | tcpwnd_size_t eff_wnd;
|
---|
| 1196 | u8_t pcb_remove; /* flag if a PCB should be removed */
|
---|
| 1197 | u8_t pcb_reset; /* flag if a RST should be sent when removing */
|
---|
| 1198 | err_t err;
|
---|
| 1199 |
|
---|
| 1200 | err = ERR_OK;
|
---|
| 1201 |
|
---|
| 1202 | ++tcp_ticks;
|
---|
| 1203 | ++tcp_timer_ctr;
|
---|
| 1204 |
|
---|
| 1205 | tcp_slowtmr_start:
|
---|
| 1206 | /* Steps through all of the active PCBs. */
|
---|
| 1207 | prev = NULL;
|
---|
| 1208 | pcb = tcp_active_pcbs;
|
---|
| 1209 | if (pcb == NULL) {
|
---|
| 1210 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
|
---|
| 1211 | }
|
---|
| 1212 | while (pcb != NULL) {
|
---|
| 1213 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
|
---|
| 1214 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
|
---|
| 1215 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
|
---|
| 1216 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
|
---|
| 1217 | if (pcb->last_timer == tcp_timer_ctr) {
|
---|
| 1218 | /* skip this pcb, we have already processed it */
|
---|
| 1219 | prev = pcb;
|
---|
| 1220 | pcb = pcb->next;
|
---|
| 1221 | continue;
|
---|
| 1222 | }
|
---|
| 1223 | pcb->last_timer = tcp_timer_ctr;
|
---|
| 1224 |
|
---|
| 1225 | pcb_remove = 0;
|
---|
| 1226 | pcb_reset = 0;
|
---|
| 1227 |
|
---|
| 1228 | if (pcb->state == SYN_SENT && pcb->nrtx >= TCP_SYNMAXRTX) {
|
---|
| 1229 | ++pcb_remove;
|
---|
| 1230 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
|
---|
| 1231 | } else if (pcb->nrtx >= TCP_MAXRTX) {
|
---|
| 1232 | ++pcb_remove;
|
---|
| 1233 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
|
---|
| 1234 | } else {
|
---|
| 1235 | if (pcb->persist_backoff > 0) {
|
---|
| 1236 | LWIP_ASSERT("tcp_slowtimr: persist ticking with in-flight data", pcb->unacked == NULL);
|
---|
| 1237 | LWIP_ASSERT("tcp_slowtimr: persist ticking with empty send buffer", pcb->unsent != NULL);
|
---|
| 1238 | if (pcb->persist_probe >= TCP_MAXRTX) {
|
---|
| 1239 | ++pcb_remove; /* max probes reached */
|
---|
| 1240 | } else {
|
---|
| 1241 | u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff - 1];
|
---|
| 1242 | if (pcb->persist_cnt < backoff_cnt) {
|
---|
| 1243 | pcb->persist_cnt++;
|
---|
| 1244 | }
|
---|
| 1245 | if (pcb->persist_cnt >= backoff_cnt) {
|
---|
| 1246 | int next_slot = 1; /* increment timer to next slot */
|
---|
| 1247 | /* If snd_wnd is zero, send 1 byte probes */
|
---|
| 1248 | if (pcb->snd_wnd == 0) {
|
---|
| 1249 | if (tcp_zero_window_probe(pcb) != ERR_OK) {
|
---|
| 1250 | next_slot = 0; /* try probe again with current slot */
|
---|
| 1251 | }
|
---|
| 1252 | /* snd_wnd not fully closed, split unsent head and fill window */
|
---|
| 1253 | } else {
|
---|
| 1254 | if (tcp_split_unsent_seg(pcb, (u16_t)pcb->snd_wnd) == ERR_OK) {
|
---|
| 1255 | if (tcp_output(pcb) == ERR_OK) {
|
---|
| 1256 | /* sending will cancel persist timer, else retry with current slot */
|
---|
| 1257 | next_slot = 0;
|
---|
| 1258 | }
|
---|
| 1259 | }
|
---|
| 1260 | }
|
---|
| 1261 | if (next_slot) {
|
---|
| 1262 | pcb->persist_cnt = 0;
|
---|
| 1263 | if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
|
---|
| 1264 | pcb->persist_backoff++;
|
---|
| 1265 | }
|
---|
| 1266 | }
|
---|
| 1267 | }
|
---|
| 1268 | }
|
---|
| 1269 | } else {
|
---|
| 1270 | /* Increase the retransmission timer if it is running */
|
---|
| 1271 | if ((pcb->rtime >= 0) && (pcb->rtime < 0x7FFF)) {
|
---|
| 1272 | ++pcb->rtime;
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
| 1275 | if (pcb->rtime >= pcb->rto) {
|
---|
| 1276 | /* Time for a retransmission. */
|
---|
| 1277 | LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
|
---|
| 1278 | " pcb->rto %"S16_F"\n",
|
---|
| 1279 | pcb->rtime, pcb->rto));
|
---|
| 1280 | /* If prepare phase fails but we have unsent data but no unacked data,
|
---|
| 1281 | still execute the backoff calculations below, as this means we somehow
|
---|
| 1282 | failed to send segment. */
|
---|
| 1283 | if ((tcp_rexmit_rto_prepare(pcb) == ERR_OK) || ((pcb->unacked == NULL) && (pcb->unsent != NULL))) {
|
---|
| 1284 | /* Double retransmission time-out unless we are trying to
|
---|
| 1285 | * connect to somebody (i.e., we are in SYN_SENT). */
|
---|
| 1286 | if (pcb->state != SYN_SENT) {
|
---|
| 1287 | u8_t backoff_idx = LWIP_MIN(pcb->nrtx, sizeof(tcp_backoff) - 1);
|
---|
| 1288 | int calc_rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[backoff_idx];
|
---|
| 1289 | pcb->rto = (s16_t)LWIP_MIN(calc_rto, 0x7FFF);
|
---|
| 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | /* Reset the retransmission timer. */
|
---|
| 1293 | pcb->rtime = 0;
|
---|
| 1294 |
|
---|
| 1295 | /* Reduce congestion window and ssthresh. */
|
---|
| 1296 | eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
|
---|
| 1297 | pcb->ssthresh = eff_wnd >> 1;
|
---|
| 1298 | if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) {
|
---|
| 1299 | pcb->ssthresh = (tcpwnd_size_t)(pcb->mss << 1);
|
---|
| 1300 | }
|
---|
| 1301 | pcb->cwnd = pcb->mss;
|
---|
| 1302 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"TCPWNDSIZE_F
|
---|
| 1303 | " ssthresh %"TCPWNDSIZE_F"\n",
|
---|
| 1304 | pcb->cwnd, pcb->ssthresh));
|
---|
| 1305 | pcb->bytes_acked = 0;
|
---|
| 1306 |
|
---|
| 1307 | /* The following needs to be called AFTER cwnd is set to one
|
---|
| 1308 | mss - STJ */
|
---|
| 1309 | tcp_rexmit_rto_commit(pcb);
|
---|
| 1310 | }
|
---|
| 1311 | }
|
---|
| 1312 | }
|
---|
| 1313 | }
|
---|
| 1314 | /* Check if this PCB has stayed too long in FIN-WAIT-2 */
|
---|
| 1315 | if (pcb->state == FIN_WAIT_2) {
|
---|
| 1316 | /* If this PCB is in FIN_WAIT_2 because of SHUT_WR don't let it time out. */
|
---|
| 1317 | if (pcb->flags & TF_RXCLOSED) {
|
---|
| 1318 | /* PCB was fully closed (either through close() or SHUT_RDWR):
|
---|
| 1319 | normal FIN-WAIT timeout handling. */
|
---|
| 1320 | if ((u32_t)(tcp_ticks - pcb->tmr) >
|
---|
| 1321 | TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
|
---|
| 1322 | ++pcb_remove;
|
---|
| 1323 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
|
---|
| 1324 | }
|
---|
| 1325 | }
|
---|
| 1326 | }
|
---|
| 1327 |
|
---|
| 1328 | /* Check if KEEPALIVE should be sent */
|
---|
| 1329 | if (ip_get_option(pcb, SOF_KEEPALIVE) &&
|
---|
| 1330 | ((pcb->state == ESTABLISHED) ||
|
---|
| 1331 | (pcb->state == CLOSE_WAIT))) {
|
---|
| 1332 | if ((u32_t)(tcp_ticks - pcb->tmr) >
|
---|
| 1333 | (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL) {
|
---|
| 1334 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to "));
|
---|
| 1335 | ip_addr_debug_print_val(TCP_DEBUG, pcb->remote_ip);
|
---|
| 1336 | LWIP_DEBUGF(TCP_DEBUG, ("\n"));
|
---|
| 1337 |
|
---|
| 1338 | ++pcb_remove;
|
---|
| 1339 | ++pcb_reset;
|
---|
| 1340 | } else if ((u32_t)(tcp_ticks - pcb->tmr) >
|
---|
| 1341 | (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
|
---|
| 1342 | / TCP_SLOW_INTERVAL) {
|
---|
| 1343 | err = tcp_keepalive(pcb);
|
---|
| 1344 | if (err == ERR_OK) {
|
---|
| 1345 | pcb->keep_cnt_sent++;
|
---|
| 1346 | }
|
---|
| 1347 | }
|
---|
| 1348 | }
|
---|
| 1349 |
|
---|
| 1350 | /* If this PCB has queued out of sequence data, but has been
|
---|
| 1351 | inactive for too long, will drop the data (it will eventually
|
---|
| 1352 | be retransmitted). */
|
---|
| 1353 | #if TCP_QUEUE_OOSEQ
|
---|
| 1354 | if (pcb->ooseq != NULL &&
|
---|
| 1355 | (tcp_ticks - pcb->tmr >= (u32_t)pcb->rto * TCP_OOSEQ_TIMEOUT)) {
|
---|
| 1356 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
|
---|
| 1357 | tcp_free_ooseq(pcb);
|
---|
| 1358 | }
|
---|
| 1359 | #endif /* TCP_QUEUE_OOSEQ */
|
---|
| 1360 |
|
---|
| 1361 | /* Check if this PCB has stayed too long in SYN-RCVD */
|
---|
| 1362 | if (pcb->state == SYN_RCVD) {
|
---|
| 1363 | if ((u32_t)(tcp_ticks - pcb->tmr) >
|
---|
| 1364 | TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
|
---|
| 1365 | ++pcb_remove;
|
---|
| 1366 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
|
---|
| 1367 | }
|
---|
| 1368 | }
|
---|
| 1369 |
|
---|
| 1370 | /* Check if this PCB has stayed too long in LAST-ACK */
|
---|
| 1371 | if (pcb->state == LAST_ACK) {
|
---|
| 1372 | if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
|
---|
| 1373 | ++pcb_remove;
|
---|
| 1374 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
|
---|
| 1375 | }
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
| 1378 | /* If the PCB should be removed, do it. */
|
---|
| 1379 | if (pcb_remove) {
|
---|
| 1380 | struct tcp_pcb *pcb2;
|
---|
| 1381 | #if LWIP_CALLBACK_API
|
---|
| 1382 | tcp_err_fn err_fn = pcb->errf;
|
---|
| 1383 | #endif /* LWIP_CALLBACK_API */
|
---|
| 1384 | void *err_arg;
|
---|
| 1385 | enum tcp_state last_state;
|
---|
| 1386 | tcp_pcb_purge(pcb);
|
---|
| 1387 | /* Remove PCB from tcp_active_pcbs list. */
|
---|
| 1388 | if (prev != NULL) {
|
---|
| 1389 | LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
|
---|
| 1390 | prev->next = pcb->next;
|
---|
| 1391 | } else {
|
---|
| 1392 | /* This PCB was the first. */
|
---|
| 1393 | LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
|
---|
| 1394 | tcp_active_pcbs = pcb->next;
|
---|
| 1395 | }
|
---|
| 1396 |
|
---|
| 1397 | if (pcb_reset) {
|
---|
| 1398 | tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
|
---|
| 1399 | pcb->local_port, pcb->remote_port);
|
---|
| 1400 | }
|
---|
| 1401 |
|
---|
| 1402 | err_arg = pcb->callback_arg;
|
---|
| 1403 | last_state = pcb->state;
|
---|
| 1404 | pcb2 = pcb;
|
---|
| 1405 | pcb = pcb->next;
|
---|
| 1406 | tcp_free(pcb2);
|
---|
| 1407 |
|
---|
| 1408 | tcp_active_pcbs_changed = 0;
|
---|
| 1409 | TCP_EVENT_ERR(last_state, err_fn, err_arg, ERR_ABRT);
|
---|
| 1410 | if (tcp_active_pcbs_changed) {
|
---|
| 1411 | goto tcp_slowtmr_start;
|
---|
| 1412 | }
|
---|
| 1413 | } else {
|
---|
| 1414 | /* get the 'next' element now and work with 'prev' below (in case of abort) */
|
---|
| 1415 | prev = pcb;
|
---|
| 1416 | pcb = pcb->next;
|
---|
| 1417 |
|
---|
| 1418 | /* We check if we should poll the connection. */
|
---|
| 1419 | ++prev->polltmr;
|
---|
| 1420 | if (prev->polltmr >= prev->pollinterval) {
|
---|
| 1421 | prev->polltmr = 0;
|
---|
| 1422 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
|
---|
| 1423 | tcp_active_pcbs_changed = 0;
|
---|
| 1424 | TCP_EVENT_POLL(prev, err);
|
---|
| 1425 | if (tcp_active_pcbs_changed) {
|
---|
| 1426 | goto tcp_slowtmr_start;
|
---|
| 1427 | }
|
---|
| 1428 | /* if err == ERR_ABRT, 'prev' is already deallocated */
|
---|
| 1429 | if (err == ERR_OK) {
|
---|
| 1430 | tcp_output(prev);
|
---|
| 1431 | }
|
---|
| 1432 | }
|
---|
| 1433 | }
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
| 1436 |
|
---|
| 1437 | /* Steps through all of the TIME-WAIT PCBs. */
|
---|
| 1438 | prev = NULL;
|
---|
| 1439 | pcb = tcp_tw_pcbs;
|
---|
| 1440 | while (pcb != NULL) {
|
---|
| 1441 | LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
|
---|
| 1442 | pcb_remove = 0;
|
---|
| 1443 |
|
---|
| 1444 | /* Check if this PCB has stayed long enough in TIME-WAIT */
|
---|
| 1445 | if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
|
---|
| 1446 | ++pcb_remove;
|
---|
| 1447 | }
|
---|
| 1448 |
|
---|
| 1449 | /* If the PCB should be removed, do it. */
|
---|
| 1450 | if (pcb_remove) {
|
---|
| 1451 | struct tcp_pcb *pcb2;
|
---|
| 1452 | tcp_pcb_purge(pcb);
|
---|
| 1453 | /* Remove PCB from tcp_tw_pcbs list. */
|
---|
| 1454 | if (prev != NULL) {
|
---|
| 1455 | LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
|
---|
| 1456 | prev->next = pcb->next;
|
---|
| 1457 | } else {
|
---|
| 1458 | /* This PCB was the first. */
|
---|
| 1459 | LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
|
---|
| 1460 | tcp_tw_pcbs = pcb->next;
|
---|
| 1461 | }
|
---|
| 1462 | pcb2 = pcb;
|
---|
| 1463 | pcb = pcb->next;
|
---|
| 1464 | tcp_free(pcb2);
|
---|
| 1465 | } else {
|
---|
| 1466 | prev = pcb;
|
---|
| 1467 | pcb = pcb->next;
|
---|
| 1468 | }
|
---|
| 1469 | }
|
---|
| 1470 | }
|
---|
| 1471 |
|
---|
| 1472 | /**
|
---|
| 1473 | * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
|
---|
| 1474 | * "refused" by upper layer (application) and sends delayed ACKs or pending FINs.
|
---|
| 1475 | *
|
---|
| 1476 | * Automatically called from tcp_tmr().
|
---|
| 1477 | */
|
---|
| 1478 | void
|
---|
| 1479 | tcp_fasttmr(void)
|
---|
| 1480 | {
|
---|
| 1481 | struct tcp_pcb *pcb;
|
---|
| 1482 |
|
---|
| 1483 | ++tcp_timer_ctr;
|
---|
| 1484 |
|
---|
| 1485 | tcp_fasttmr_start:
|
---|
| 1486 | pcb = tcp_active_pcbs;
|
---|
| 1487 |
|
---|
| 1488 | while (pcb != NULL) {
|
---|
| 1489 | if (pcb->last_timer != tcp_timer_ctr) {
|
---|
| 1490 | struct tcp_pcb *next;
|
---|
| 1491 | pcb->last_timer = tcp_timer_ctr;
|
---|
| 1492 | /* send delayed ACKs */
|
---|
| 1493 | if (pcb->flags & TF_ACK_DELAY) {
|
---|
| 1494 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
|
---|
| 1495 | tcp_ack_now(pcb);
|
---|
| 1496 | tcp_output(pcb);
|
---|
| 1497 | tcp_clear_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
|
---|
| 1498 | }
|
---|
| 1499 | /* send pending FIN */
|
---|
| 1500 | if (pcb->flags & TF_CLOSEPEND) {
|
---|
| 1501 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: pending FIN\n"));
|
---|
| 1502 | tcp_clear_flags(pcb, TF_CLOSEPEND);
|
---|
| 1503 | tcp_close_shutdown_fin(pcb);
|
---|
| 1504 | }
|
---|
| 1505 |
|
---|
| 1506 | next = pcb->next;
|
---|
| 1507 |
|
---|
| 1508 | /* If there is data which was previously "refused" by upper layer */
|
---|
| 1509 | if (pcb->refused_data != NULL) {
|
---|
| 1510 | tcp_active_pcbs_changed = 0;
|
---|
| 1511 | tcp_process_refused_data(pcb);
|
---|
| 1512 | if (tcp_active_pcbs_changed) {
|
---|
| 1513 | /* application callback has changed the pcb list: restart the loop */
|
---|
| 1514 | goto tcp_fasttmr_start;
|
---|
| 1515 | }
|
---|
| 1516 | }
|
---|
| 1517 | pcb = next;
|
---|
| 1518 | } else {
|
---|
| 1519 | pcb = pcb->next;
|
---|
| 1520 | }
|
---|
| 1521 | }
|
---|
| 1522 | }
|
---|
| 1523 |
|
---|
| 1524 | /** Call tcp_output for all active pcbs that have TF_NAGLEMEMERR set */
|
---|
| 1525 | void
|
---|
| 1526 | tcp_txnow(void)
|
---|
| 1527 | {
|
---|
| 1528 | struct tcp_pcb *pcb;
|
---|
| 1529 |
|
---|
| 1530 | for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 1531 | if (pcb->flags & TF_NAGLEMEMERR) {
|
---|
| 1532 | tcp_output(pcb);
|
---|
| 1533 | }
|
---|
| 1534 | }
|
---|
| 1535 | }
|
---|
| 1536 |
|
---|
| 1537 | /** Pass pcb->refused_data to the recv callback */
|
---|
| 1538 | err_t
|
---|
| 1539 | tcp_process_refused_data(struct tcp_pcb *pcb)
|
---|
| 1540 | {
|
---|
| 1541 | #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
|
---|
| 1542 | struct pbuf *rest;
|
---|
| 1543 | #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
|
---|
| 1544 |
|
---|
| 1545 | LWIP_ERROR("tcp_process_refused_data: invalid pcb", pcb != NULL, return ERR_ARG);
|
---|
| 1546 |
|
---|
| 1547 | #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
|
---|
| 1548 | while (pcb->refused_data != NULL)
|
---|
| 1549 | #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
|
---|
| 1550 | {
|
---|
| 1551 | err_t err;
|
---|
| 1552 | u8_t refused_flags = pcb->refused_data->flags;
|
---|
| 1553 | /* set pcb->refused_data to NULL in case the callback frees it and then
|
---|
| 1554 | closes the pcb */
|
---|
| 1555 | struct pbuf *refused_data = pcb->refused_data;
|
---|
| 1556 | #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
|
---|
| 1557 | pbuf_split_64k(refused_data, &rest);
|
---|
| 1558 | pcb->refused_data = rest;
|
---|
| 1559 | #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
|
---|
| 1560 | pcb->refused_data = NULL;
|
---|
| 1561 | #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
|
---|
| 1562 | /* Notify again application with data previously received. */
|
---|
| 1563 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
|
---|
| 1564 | TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
|
---|
| 1565 | if (err == ERR_OK) {
|
---|
| 1566 | /* did refused_data include a FIN? */
|
---|
| 1567 | if ((refused_flags & PBUF_FLAG_TCP_FIN)
|
---|
| 1568 | #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
|
---|
| 1569 | && (rest == NULL)
|
---|
| 1570 | #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
|
---|
| 1571 | ) {
|
---|
| 1572 | /* correct rcv_wnd as the application won't call tcp_recved()
|
---|
| 1573 | for the FIN's seqno */
|
---|
| 1574 | if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
|
---|
| 1575 | pcb->rcv_wnd++;
|
---|
| 1576 | }
|
---|
| 1577 | TCP_EVENT_CLOSED(pcb, err);
|
---|
| 1578 | if (err == ERR_ABRT) {
|
---|
| 1579 | return ERR_ABRT;
|
---|
| 1580 | }
|
---|
| 1581 | }
|
---|
| 1582 | } else if (err == ERR_ABRT) {
|
---|
| 1583 | /* if err == ERR_ABRT, 'pcb' is already deallocated */
|
---|
| 1584 | /* Drop incoming packets because pcb is "full" (only if the incoming
|
---|
| 1585 | segment contains data). */
|
---|
| 1586 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
|
---|
| 1587 | return ERR_ABRT;
|
---|
| 1588 | } else {
|
---|
| 1589 | /* data is still refused, pbuf is still valid (go on for ACK-only packets) */
|
---|
| 1590 | #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
|
---|
| 1591 | if (rest != NULL) {
|
---|
| 1592 | pbuf_cat(refused_data, rest);
|
---|
| 1593 | }
|
---|
| 1594 | #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
|
---|
| 1595 | pcb->refused_data = refused_data;
|
---|
| 1596 | return ERR_INPROGRESS;
|
---|
| 1597 | }
|
---|
| 1598 | }
|
---|
| 1599 | return ERR_OK;
|
---|
| 1600 | }
|
---|
| 1601 |
|
---|
| 1602 | /**
|
---|
| 1603 | * Deallocates a list of TCP segments (tcp_seg structures).
|
---|
| 1604 | *
|
---|
| 1605 | * @param seg tcp_seg list of TCP segments to free
|
---|
| 1606 | */
|
---|
| 1607 | void
|
---|
| 1608 | tcp_segs_free(struct tcp_seg *seg)
|
---|
| 1609 | {
|
---|
| 1610 | while (seg != NULL) {
|
---|
| 1611 | struct tcp_seg *next = seg->next;
|
---|
| 1612 | tcp_seg_free(seg);
|
---|
| 1613 | seg = next;
|
---|
| 1614 | }
|
---|
| 1615 | }
|
---|
| 1616 |
|
---|
| 1617 | /**
|
---|
| 1618 | * Frees a TCP segment (tcp_seg structure).
|
---|
| 1619 | *
|
---|
| 1620 | * @param seg single tcp_seg to free
|
---|
| 1621 | */
|
---|
| 1622 | void
|
---|
| 1623 | tcp_seg_free(struct tcp_seg *seg)
|
---|
| 1624 | {
|
---|
| 1625 | if (seg != NULL) {
|
---|
| 1626 | if (seg->p != NULL) {
|
---|
| 1627 | pbuf_free(seg->p);
|
---|
| 1628 | #if TCP_DEBUG
|
---|
| 1629 | seg->p = NULL;
|
---|
| 1630 | #endif /* TCP_DEBUG */
|
---|
| 1631 | }
|
---|
| 1632 | memp_free(MEMP_TCP_SEG, seg);
|
---|
| 1633 | }
|
---|
| 1634 | }
|
---|
| 1635 |
|
---|
| 1636 | /**
|
---|
| 1637 | * @ingroup tcp
|
---|
| 1638 | * Sets the priority of a connection.
|
---|
| 1639 | *
|
---|
| 1640 | * @param pcb the tcp_pcb to manipulate
|
---|
| 1641 | * @param prio new priority
|
---|
| 1642 | */
|
---|
| 1643 | void
|
---|
| 1644 | tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
|
---|
| 1645 | {
|
---|
| 1646 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 1647 |
|
---|
| 1648 | LWIP_ERROR("tcp_setprio: invalid pcb", pcb != NULL, return);
|
---|
| 1649 |
|
---|
| 1650 | pcb->prio = prio;
|
---|
| 1651 | }
|
---|
| 1652 |
|
---|
| 1653 | #if TCP_QUEUE_OOSEQ
|
---|
| 1654 | /**
|
---|
| 1655 | * Returns a copy of the given TCP segment.
|
---|
| 1656 | * The pbuf and data are not copied, only the pointers
|
---|
| 1657 | *
|
---|
| 1658 | * @param seg the old tcp_seg
|
---|
| 1659 | * @return a copy of seg
|
---|
| 1660 | */
|
---|
| 1661 | struct tcp_seg *
|
---|
| 1662 | tcp_seg_copy(struct tcp_seg *seg)
|
---|
| 1663 | {
|
---|
| 1664 | struct tcp_seg *cseg;
|
---|
| 1665 |
|
---|
| 1666 | LWIP_ASSERT("tcp_seg_copy: invalid seg", seg != NULL);
|
---|
| 1667 |
|
---|
| 1668 | cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
|
---|
| 1669 | if (cseg == NULL) {
|
---|
| 1670 | return NULL;
|
---|
| 1671 | }
|
---|
| 1672 | SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
|
---|
| 1673 | pbuf_ref(cseg->p);
|
---|
| 1674 | return cseg;
|
---|
| 1675 | }
|
---|
| 1676 | #endif /* TCP_QUEUE_OOSEQ */
|
---|
| 1677 |
|
---|
| 1678 | #if LWIP_CALLBACK_API
|
---|
| 1679 | /**
|
---|
| 1680 | * Default receive callback that is called if the user didn't register
|
---|
| 1681 | * a recv callback for the pcb.
|
---|
| 1682 | */
|
---|
| 1683 | err_t
|
---|
| 1684 | tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
---|
| 1685 | {
|
---|
| 1686 | LWIP_UNUSED_ARG(arg);
|
---|
| 1687 |
|
---|
| 1688 | LWIP_ERROR("tcp_recv_null: invalid pcb", pcb != NULL, return ERR_ARG);
|
---|
| 1689 |
|
---|
| 1690 | if (p != NULL) {
|
---|
| 1691 | tcp_recved(pcb, p->tot_len);
|
---|
| 1692 | pbuf_free(p);
|
---|
| 1693 | } else if (err == ERR_OK) {
|
---|
| 1694 | return tcp_close(pcb);
|
---|
| 1695 | }
|
---|
| 1696 | return ERR_OK;
|
---|
| 1697 | }
|
---|
| 1698 | #endif /* LWIP_CALLBACK_API */
|
---|
| 1699 |
|
---|
| 1700 | /**
|
---|
| 1701 | * Kills the oldest active connection that has a lower priority than 'prio'.
|
---|
| 1702 | *
|
---|
| 1703 | * @param prio minimum priority
|
---|
| 1704 | */
|
---|
| 1705 | static void
|
---|
| 1706 | tcp_kill_prio(u8_t prio)
|
---|
| 1707 | {
|
---|
| 1708 | struct tcp_pcb *pcb, *inactive;
|
---|
| 1709 | u32_t inactivity;
|
---|
| 1710 | u8_t mprio;
|
---|
| 1711 |
|
---|
| 1712 | mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
|
---|
| 1713 |
|
---|
| 1714 | /* We want to kill connections with a lower prio, so bail out if
|
---|
| 1715 | * supplied prio is 0 - there can never be a lower prio
|
---|
| 1716 | */
|
---|
| 1717 | if (mprio == 0) {
|
---|
| 1718 | return;
|
---|
| 1719 | }
|
---|
| 1720 |
|
---|
| 1721 | /* We only want kill connections with a lower prio, so decrement prio by one
|
---|
| 1722 | * and start searching for oldest connection with same or lower priority than mprio.
|
---|
| 1723 | * We want to find the connections with the lowest possible prio, and among
|
---|
| 1724 | * these the one with the longest inactivity time.
|
---|
| 1725 | */
|
---|
| 1726 | mprio--;
|
---|
| 1727 |
|
---|
| 1728 | inactivity = 0;
|
---|
| 1729 | inactive = NULL;
|
---|
| 1730 | for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 1731 | /* lower prio is always a kill candidate */
|
---|
| 1732 | if ((pcb->prio < mprio) ||
|
---|
| 1733 | /* longer inactivity is also a kill candidate */
|
---|
| 1734 | ((pcb->prio == mprio) && ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity))) {
|
---|
| 1735 | inactivity = tcp_ticks - pcb->tmr;
|
---|
| 1736 | inactive = pcb;
|
---|
| 1737 | mprio = pcb->prio;
|
---|
| 1738 | }
|
---|
| 1739 | }
|
---|
| 1740 | if (inactive != NULL) {
|
---|
| 1741 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
|
---|
| 1742 | (void *)inactive, inactivity));
|
---|
| 1743 | tcp_abort(inactive);
|
---|
| 1744 | }
|
---|
| 1745 | }
|
---|
| 1746 |
|
---|
| 1747 | /**
|
---|
| 1748 | * Kills the oldest connection that is in specific state.
|
---|
| 1749 | * Called from tcp_alloc() for LAST_ACK and CLOSING if no more connections are available.
|
---|
| 1750 | */
|
---|
| 1751 | static void
|
---|
| 1752 | tcp_kill_state(enum tcp_state state)
|
---|
| 1753 | {
|
---|
| 1754 | struct tcp_pcb *pcb, *inactive;
|
---|
| 1755 | u32_t inactivity;
|
---|
| 1756 |
|
---|
| 1757 | LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
|
---|
| 1758 |
|
---|
| 1759 | inactivity = 0;
|
---|
| 1760 | inactive = NULL;
|
---|
| 1761 | /* Go through the list of active pcbs and get the oldest pcb that is in state
|
---|
| 1762 | CLOSING/LAST_ACK. */
|
---|
| 1763 | for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 1764 | if (pcb->state == state) {
|
---|
| 1765 | if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
|
---|
| 1766 | inactivity = tcp_ticks - pcb->tmr;
|
---|
| 1767 | inactive = pcb;
|
---|
| 1768 | }
|
---|
| 1769 | }
|
---|
| 1770 | }
|
---|
| 1771 | if (inactive != NULL) {
|
---|
| 1772 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
|
---|
| 1773 | tcp_state_str[state], (void *)inactive, inactivity));
|
---|
| 1774 | /* Don't send a RST, since no data is lost. */
|
---|
| 1775 | tcp_abandon(inactive, 0);
|
---|
| 1776 | }
|
---|
| 1777 | }
|
---|
| 1778 |
|
---|
| 1779 | /**
|
---|
| 1780 | * Kills the oldest connection that is in TIME_WAIT state.
|
---|
| 1781 | * Called from tcp_alloc() if no more connections are available.
|
---|
| 1782 | */
|
---|
| 1783 | static void
|
---|
| 1784 | tcp_kill_timewait(void)
|
---|
| 1785 | {
|
---|
| 1786 | struct tcp_pcb *pcb, *inactive;
|
---|
| 1787 | u32_t inactivity;
|
---|
| 1788 |
|
---|
| 1789 | inactivity = 0;
|
---|
| 1790 | inactive = NULL;
|
---|
| 1791 | /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
|
---|
| 1792 | for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 1793 | if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
|
---|
| 1794 | inactivity = tcp_ticks - pcb->tmr;
|
---|
| 1795 | inactive = pcb;
|
---|
| 1796 | }
|
---|
| 1797 | }
|
---|
| 1798 | if (inactive != NULL) {
|
---|
| 1799 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
|
---|
| 1800 | (void *)inactive, inactivity));
|
---|
| 1801 | tcp_abort(inactive);
|
---|
| 1802 | }
|
---|
| 1803 | }
|
---|
| 1804 |
|
---|
| 1805 | /* Called when allocating a pcb fails.
|
---|
| 1806 | * In this case, we want to handle all pcbs that want to close first: if we can
|
---|
| 1807 | * now send the FIN (which failed before), the pcb might be in a state that is
|
---|
| 1808 | * OK for us to now free it.
|
---|
| 1809 | */
|
---|
| 1810 | static void
|
---|
| 1811 | tcp_handle_closepend(void)
|
---|
| 1812 | {
|
---|
| 1813 | struct tcp_pcb *pcb = tcp_active_pcbs;
|
---|
| 1814 |
|
---|
| 1815 | while (pcb != NULL) {
|
---|
| 1816 | struct tcp_pcb *next = pcb->next;
|
---|
| 1817 | /* send pending FIN */
|
---|
| 1818 | if (pcb->flags & TF_CLOSEPEND) {
|
---|
| 1819 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_handle_closepend: pending FIN\n"));
|
---|
| 1820 | tcp_clear_flags(pcb, TF_CLOSEPEND);
|
---|
| 1821 | tcp_close_shutdown_fin(pcb);
|
---|
| 1822 | }
|
---|
| 1823 | pcb = next;
|
---|
| 1824 | }
|
---|
| 1825 | }
|
---|
| 1826 |
|
---|
| 1827 | /**
|
---|
| 1828 | * Allocate a new tcp_pcb structure.
|
---|
| 1829 | *
|
---|
| 1830 | * @param prio priority for the new pcb
|
---|
| 1831 | * @return a new tcp_pcb that initially is in state CLOSED
|
---|
| 1832 | */
|
---|
| 1833 | struct tcp_pcb *
|
---|
| 1834 | tcp_alloc(u8_t prio)
|
---|
| 1835 | {
|
---|
| 1836 | struct tcp_pcb *pcb;
|
---|
| 1837 |
|
---|
| 1838 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 1839 |
|
---|
| 1840 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
|
---|
| 1841 | if (pcb == NULL) {
|
---|
| 1842 | /* Try to send FIN for all pcbs stuck in TF_CLOSEPEND first */
|
---|
| 1843 | tcp_handle_closepend();
|
---|
| 1844 |
|
---|
| 1845 | /* Try killing oldest connection in TIME-WAIT. */
|
---|
| 1846 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
|
---|
| 1847 | tcp_kill_timewait();
|
---|
| 1848 | /* Try to allocate a tcp_pcb again. */
|
---|
| 1849 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
|
---|
| 1850 | if (pcb == NULL) {
|
---|
| 1851 | /* Try killing oldest connection in LAST-ACK (these wouldn't go to TIME-WAIT). */
|
---|
| 1852 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n"));
|
---|
| 1853 | tcp_kill_state(LAST_ACK);
|
---|
| 1854 | /* Try to allocate a tcp_pcb again. */
|
---|
| 1855 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
|
---|
| 1856 | if (pcb == NULL) {
|
---|
| 1857 | /* Try killing oldest connection in CLOSING. */
|
---|
| 1858 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n"));
|
---|
| 1859 | tcp_kill_state(CLOSING);
|
---|
| 1860 | /* Try to allocate a tcp_pcb again. */
|
---|
| 1861 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
|
---|
| 1862 | if (pcb == NULL) {
|
---|
| 1863 | /* Try killing oldest active connection with lower priority than the new one. */
|
---|
| 1864 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio));
|
---|
| 1865 | tcp_kill_prio(prio);
|
---|
| 1866 | /* Try to allocate a tcp_pcb again. */
|
---|
| 1867 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
|
---|
| 1868 | if (pcb != NULL) {
|
---|
| 1869 | /* adjust err stats: memp_malloc failed multiple times before */
|
---|
| 1870 | MEMP_STATS_DEC(err, MEMP_TCP_PCB);
|
---|
| 1871 | }
|
---|
| 1872 | }
|
---|
| 1873 | if (pcb != NULL) {
|
---|
| 1874 | /* adjust err stats: memp_malloc failed multiple times before */
|
---|
| 1875 | MEMP_STATS_DEC(err, MEMP_TCP_PCB);
|
---|
| 1876 | }
|
---|
| 1877 | }
|
---|
| 1878 | if (pcb != NULL) {
|
---|
| 1879 | /* adjust err stats: memp_malloc failed multiple times before */
|
---|
| 1880 | MEMP_STATS_DEC(err, MEMP_TCP_PCB);
|
---|
| 1881 | }
|
---|
| 1882 | }
|
---|
| 1883 | if (pcb != NULL) {
|
---|
| 1884 | /* adjust err stats: memp_malloc failed above */
|
---|
| 1885 | MEMP_STATS_DEC(err, MEMP_TCP_PCB);
|
---|
| 1886 | }
|
---|
| 1887 | }
|
---|
| 1888 | if (pcb != NULL) {
|
---|
| 1889 | /* zero out the whole pcb, so there is no need to initialize members to zero */
|
---|
| 1890 | memset(pcb, 0, sizeof(struct tcp_pcb));
|
---|
| 1891 | pcb->prio = prio;
|
---|
| 1892 | pcb->snd_buf = TCP_SND_BUF;
|
---|
| 1893 | /* Start with a window that does not need scaling. When window scaling is
|
---|
| 1894 | enabled and used, the window is enlarged when both sides agree on scaling. */
|
---|
| 1895 | pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
|
---|
| 1896 | pcb->ttl = TCP_TTL;
|
---|
| 1897 | /* As initial send MSS, we use TCP_MSS but limit it to 536.
|
---|
| 1898 | The send MSS is updated when an MSS option is received. */
|
---|
| 1899 | pcb->mss = INITIAL_MSS;
|
---|
| 1900 | pcb->rto = 3000 / TCP_SLOW_INTERVAL;
|
---|
| 1901 | pcb->sv = 3000 / TCP_SLOW_INTERVAL;
|
---|
| 1902 | pcb->rtime = -1;
|
---|
| 1903 | pcb->cwnd = 1;
|
---|
| 1904 | pcb->tmr = tcp_ticks;
|
---|
| 1905 | pcb->last_timer = tcp_timer_ctr;
|
---|
| 1906 |
|
---|
| 1907 | /* RFC 5681 recommends setting ssthresh abritrarily high and gives an example
|
---|
| 1908 | of using the largest advertised receive window. We've seen complications with
|
---|
| 1909 | receiving TCPs that use window scaling and/or window auto-tuning where the
|
---|
| 1910 | initial advertised window is very small and then grows rapidly once the
|
---|
| 1911 | connection is established. To avoid these complications, we set ssthresh to the
|
---|
| 1912 | largest effective cwnd (amount of in-flight data) that the sender can have. */
|
---|
| 1913 | pcb->ssthresh = TCP_SND_BUF;
|
---|
| 1914 |
|
---|
| 1915 | #if LWIP_CALLBACK_API
|
---|
| 1916 | pcb->recv = tcp_recv_null;
|
---|
| 1917 | #endif /* LWIP_CALLBACK_API */
|
---|
| 1918 |
|
---|
| 1919 | /* Init KEEPALIVE timer */
|
---|
| 1920 | pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
|
---|
| 1921 |
|
---|
| 1922 | #if LWIP_TCP_KEEPALIVE
|
---|
| 1923 | pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
|
---|
| 1924 | pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
|
---|
| 1925 | #endif /* LWIP_TCP_KEEPALIVE */
|
---|
| 1926 | }
|
---|
| 1927 | return pcb;
|
---|
| 1928 | }
|
---|
| 1929 |
|
---|
| 1930 | /**
|
---|
| 1931 | * @ingroup tcp_raw
|
---|
| 1932 | * Creates a new TCP protocol control block but doesn't place it on
|
---|
| 1933 | * any of the TCP PCB lists.
|
---|
| 1934 | * The pcb is not put on any list until binding using tcp_bind().
|
---|
| 1935 | * If memory is not available for creating the new pcb, NULL is returned.
|
---|
| 1936 | *
|
---|
| 1937 | * @internal: Maybe there should be a idle TCP PCB list where these
|
---|
| 1938 | * PCBs are put on. Port reservation using tcp_bind() is implemented but
|
---|
| 1939 | * allocated pcbs that are not bound can't be killed automatically if wanting
|
---|
| 1940 | * to allocate a pcb with higher prio (@see tcp_kill_prio())
|
---|
| 1941 | *
|
---|
| 1942 | * @return a new tcp_pcb that initially is in state CLOSED
|
---|
| 1943 | */
|
---|
| 1944 | struct tcp_pcb *
|
---|
| 1945 | tcp_new(void)
|
---|
| 1946 | {
|
---|
| 1947 | return tcp_alloc(TCP_PRIO_NORMAL);
|
---|
| 1948 | }
|
---|
| 1949 |
|
---|
| 1950 | /**
|
---|
| 1951 | * @ingroup tcp_raw
|
---|
| 1952 | * Creates a new TCP protocol control block but doesn't
|
---|
| 1953 | * place it on any of the TCP PCB lists.
|
---|
| 1954 | * The pcb is not put on any list until binding using tcp_bind().
|
---|
| 1955 | *
|
---|
| 1956 | * @param type IP address type, see @ref lwip_ip_addr_type definitions.
|
---|
| 1957 | * If you want to listen to IPv4 and IPv6 (dual-stack) connections,
|
---|
| 1958 | * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
|
---|
| 1959 | * @return a new tcp_pcb that initially is in state CLOSED
|
---|
| 1960 | */
|
---|
| 1961 | struct tcp_pcb *
|
---|
| 1962 | tcp_new_ip_type(u8_t type)
|
---|
| 1963 | {
|
---|
| 1964 | struct tcp_pcb *pcb;
|
---|
| 1965 | pcb = tcp_alloc(TCP_PRIO_NORMAL);
|
---|
| 1966 | #if LWIP_IPV4 && LWIP_IPV6
|
---|
| 1967 | if (pcb != NULL) {
|
---|
| 1968 | IP_SET_TYPE_VAL(pcb->local_ip, type);
|
---|
| 1969 | IP_SET_TYPE_VAL(pcb->remote_ip, type);
|
---|
| 1970 | }
|
---|
| 1971 | #else
|
---|
| 1972 | LWIP_UNUSED_ARG(type);
|
---|
| 1973 | #endif /* LWIP_IPV4 && LWIP_IPV6 */
|
---|
| 1974 | return pcb;
|
---|
| 1975 | }
|
---|
| 1976 |
|
---|
| 1977 | /**
|
---|
| 1978 | * @ingroup tcp_raw
|
---|
| 1979 | * Specifies the program specific state that should be passed to all
|
---|
| 1980 | * other callback functions. The "pcb" argument is the current TCP
|
---|
| 1981 | * connection control block, and the "arg" argument is the argument
|
---|
| 1982 | * that will be passed to the callbacks.
|
---|
| 1983 | *
|
---|
| 1984 | * @param pcb tcp_pcb to set the callback argument
|
---|
| 1985 | * @param arg void pointer argument to pass to callback functions
|
---|
| 1986 | */
|
---|
| 1987 | void
|
---|
| 1988 | tcp_arg(struct tcp_pcb *pcb, void *arg)
|
---|
| 1989 | {
|
---|
| 1990 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 1991 | /* This function is allowed to be called for both listen pcbs and
|
---|
| 1992 | connection pcbs. */
|
---|
| 1993 | if (pcb != NULL) {
|
---|
| 1994 | pcb->callback_arg = arg;
|
---|
| 1995 | }
|
---|
| 1996 | }
|
---|
| 1997 | #if LWIP_CALLBACK_API
|
---|
| 1998 |
|
---|
| 1999 | /**
|
---|
| 2000 | * @ingroup tcp_raw
|
---|
| 2001 | * Sets the callback function that will be called when new data
|
---|
| 2002 | * arrives. The callback function will be passed a NULL pbuf to
|
---|
| 2003 | * indicate that the remote host has closed the connection. If the
|
---|
| 2004 | * callback function returns ERR_OK or ERR_ABRT it must have
|
---|
| 2005 | * freed the pbuf, otherwise it must not have freed it.
|
---|
| 2006 | *
|
---|
| 2007 | * @param pcb tcp_pcb to set the recv callback
|
---|
| 2008 | * @param recv callback function to call for this pcb when data is received
|
---|
| 2009 | */
|
---|
| 2010 | void
|
---|
| 2011 | tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
|
---|
| 2012 | {
|
---|
| 2013 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2014 | if (pcb != NULL) {
|
---|
| 2015 | LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
|
---|
| 2016 | pcb->recv = recv;
|
---|
| 2017 | }
|
---|
| 2018 | }
|
---|
| 2019 |
|
---|
| 2020 | /**
|
---|
| 2021 | * @ingroup tcp_raw
|
---|
| 2022 | * Specifies the callback function that should be called when data has
|
---|
| 2023 | * successfully been received (i.e., acknowledged) by the remote
|
---|
| 2024 | * host. The len argument passed to the callback function gives the
|
---|
| 2025 | * amount bytes that was acknowledged by the last acknowledgment.
|
---|
| 2026 | *
|
---|
| 2027 | * @param pcb tcp_pcb to set the sent callback
|
---|
| 2028 | * @param sent callback function to call for this pcb when data is successfully sent
|
---|
| 2029 | */
|
---|
| 2030 | void
|
---|
| 2031 | tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
|
---|
| 2032 | {
|
---|
| 2033 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2034 | if (pcb != NULL) {
|
---|
| 2035 | LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
|
---|
| 2036 | pcb->sent = sent;
|
---|
| 2037 | }
|
---|
| 2038 | }
|
---|
| 2039 |
|
---|
| 2040 | /**
|
---|
| 2041 | * @ingroup tcp_raw
|
---|
| 2042 | * Used to specify the function that should be called when a fatal error
|
---|
| 2043 | * has occurred on the connection.
|
---|
| 2044 | *
|
---|
| 2045 | * If a connection is aborted because of an error, the application is
|
---|
| 2046 | * alerted of this event by the err callback. Errors that might abort a
|
---|
| 2047 | * connection are when there is a shortage of memory. The callback
|
---|
| 2048 | * function to be called is set using the tcp_err() function.
|
---|
| 2049 | *
|
---|
| 2050 | * @note The corresponding pcb is already freed when this callback is called!
|
---|
| 2051 | *
|
---|
| 2052 | * @param pcb tcp_pcb to set the err callback
|
---|
| 2053 | * @param err callback function to call for this pcb when a fatal error
|
---|
| 2054 | * has occurred on the connection
|
---|
| 2055 | */
|
---|
| 2056 | void
|
---|
| 2057 | tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
|
---|
| 2058 | {
|
---|
| 2059 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2060 | if (pcb != NULL) {
|
---|
| 2061 | LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
|
---|
| 2062 | pcb->errf = err;
|
---|
| 2063 | }
|
---|
| 2064 | }
|
---|
| 2065 |
|
---|
| 2066 | /**
|
---|
| 2067 | * @ingroup tcp_raw
|
---|
| 2068 | * Used for specifying the function that should be called when a
|
---|
| 2069 | * LISTENing connection has been connected to another host.
|
---|
| 2070 | *
|
---|
| 2071 | * @param pcb tcp_pcb to set the accept callback
|
---|
| 2072 | * @param accept callback function to call for this pcb when LISTENing
|
---|
| 2073 | * connection has been connected to another host
|
---|
| 2074 | */
|
---|
| 2075 | void
|
---|
| 2076 | tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
|
---|
| 2077 | {
|
---|
| 2078 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2079 | if ((pcb != NULL) && (pcb->state == LISTEN)) {
|
---|
| 2080 | struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen *)pcb;
|
---|
| 2081 | lpcb->accept = accept;
|
---|
| 2082 | }
|
---|
| 2083 | }
|
---|
| 2084 | #endif /* LWIP_CALLBACK_API */
|
---|
| 2085 |
|
---|
| 2086 |
|
---|
| 2087 | /**
|
---|
| 2088 | * @ingroup tcp_raw
|
---|
| 2089 | * Specifies the polling interval and the callback function that should
|
---|
| 2090 | * be called to poll the application. The interval is specified in
|
---|
| 2091 | * number of TCP coarse grained timer shots, which typically occurs
|
---|
| 2092 | * twice a second. An interval of 10 means that the application would
|
---|
| 2093 | * be polled every 5 seconds.
|
---|
| 2094 | *
|
---|
| 2095 | * When a connection is idle (i.e., no data is either transmitted or
|
---|
| 2096 | * received), lwIP will repeatedly poll the application by calling a
|
---|
| 2097 | * specified callback function. This can be used either as a watchdog
|
---|
| 2098 | * timer for killing connections that have stayed idle for too long, or
|
---|
| 2099 | * as a method of waiting for memory to become available. For instance,
|
---|
| 2100 | * if a call to tcp_write() has failed because memory wasn't available,
|
---|
| 2101 | * the application may use the polling functionality to call tcp_write()
|
---|
| 2102 | * again when the connection has been idle for a while.
|
---|
| 2103 | */
|
---|
| 2104 | void
|
---|
| 2105 | tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
|
---|
| 2106 | {
|
---|
| 2107 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2108 |
|
---|
| 2109 | LWIP_ERROR("tcp_poll: invalid pcb", pcb != NULL, return);
|
---|
| 2110 | LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
|
---|
| 2111 |
|
---|
| 2112 | #if LWIP_CALLBACK_API
|
---|
| 2113 | pcb->poll = poll;
|
---|
| 2114 | #else /* LWIP_CALLBACK_API */
|
---|
| 2115 | LWIP_UNUSED_ARG(poll);
|
---|
| 2116 | #endif /* LWIP_CALLBACK_API */
|
---|
| 2117 | pcb->pollinterval = interval;
|
---|
| 2118 | }
|
---|
| 2119 |
|
---|
| 2120 | /**
|
---|
| 2121 | * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
|
---|
| 2122 | * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
|
---|
| 2123 | *
|
---|
| 2124 | * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
|
---|
| 2125 | */
|
---|
| 2126 | void
|
---|
| 2127 | tcp_pcb_purge(struct tcp_pcb *pcb)
|
---|
| 2128 | {
|
---|
| 2129 | LWIP_ERROR("tcp_pcb_purge: invalid pcb", pcb != NULL, return);
|
---|
| 2130 |
|
---|
| 2131 | if (pcb->state != CLOSED &&
|
---|
| 2132 | pcb->state != TIME_WAIT &&
|
---|
| 2133 | pcb->state != LISTEN) {
|
---|
| 2134 |
|
---|
| 2135 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
|
---|
| 2136 |
|
---|
| 2137 | tcp_backlog_accepted(pcb);
|
---|
| 2138 |
|
---|
| 2139 | if (pcb->refused_data != NULL) {
|
---|
| 2140 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
|
---|
| 2141 | pbuf_free(pcb->refused_data);
|
---|
| 2142 | pcb->refused_data = NULL;
|
---|
| 2143 | }
|
---|
| 2144 | if (pcb->unsent != NULL) {
|
---|
| 2145 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
|
---|
| 2146 | }
|
---|
| 2147 | if (pcb->unacked != NULL) {
|
---|
| 2148 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
|
---|
| 2149 | }
|
---|
| 2150 | #if TCP_QUEUE_OOSEQ
|
---|
| 2151 | if (pcb->ooseq != NULL) {
|
---|
| 2152 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
|
---|
| 2153 | tcp_free_ooseq(pcb);
|
---|
| 2154 | }
|
---|
| 2155 | #endif /* TCP_QUEUE_OOSEQ */
|
---|
| 2156 |
|
---|
| 2157 | /* Stop the retransmission timer as it will expect data on unacked
|
---|
| 2158 | queue if it fires */
|
---|
| 2159 | pcb->rtime = -1;
|
---|
| 2160 |
|
---|
| 2161 | tcp_segs_free(pcb->unsent);
|
---|
| 2162 | tcp_segs_free(pcb->unacked);
|
---|
| 2163 | pcb->unacked = pcb->unsent = NULL;
|
---|
| 2164 | #if TCP_OVERSIZE
|
---|
| 2165 | pcb->unsent_oversize = 0;
|
---|
| 2166 | #endif /* TCP_OVERSIZE */
|
---|
| 2167 | }
|
---|
| 2168 | }
|
---|
| 2169 |
|
---|
| 2170 | /**
|
---|
| 2171 | * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
|
---|
| 2172 | *
|
---|
| 2173 | * @param pcblist PCB list to purge.
|
---|
| 2174 | * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
|
---|
| 2175 | */
|
---|
| 2176 | void
|
---|
| 2177 | tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
|
---|
| 2178 | {
|
---|
| 2179 | LWIP_ASSERT("tcp_pcb_remove: invalid pcb", pcb != NULL);
|
---|
| 2180 | LWIP_ASSERT("tcp_pcb_remove: invalid pcblist", pcblist != NULL);
|
---|
| 2181 |
|
---|
| 2182 | TCP_RMV(pcblist, pcb);
|
---|
| 2183 |
|
---|
| 2184 | tcp_pcb_purge(pcb);
|
---|
| 2185 |
|
---|
| 2186 | /* if there is an outstanding delayed ACKs, send it */
|
---|
| 2187 | if ((pcb->state != TIME_WAIT) &&
|
---|
| 2188 | (pcb->state != LISTEN) &&
|
---|
| 2189 | (pcb->flags & TF_ACK_DELAY)) {
|
---|
| 2190 | tcp_ack_now(pcb);
|
---|
| 2191 | tcp_output(pcb);
|
---|
| 2192 | }
|
---|
| 2193 |
|
---|
| 2194 | if (pcb->state != LISTEN) {
|
---|
| 2195 | LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
|
---|
| 2196 | LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
|
---|
| 2197 | #if TCP_QUEUE_OOSEQ
|
---|
| 2198 | LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
|
---|
| 2199 | #endif /* TCP_QUEUE_OOSEQ */
|
---|
| 2200 | }
|
---|
| 2201 |
|
---|
| 2202 | pcb->state = CLOSED;
|
---|
| 2203 | /* reset the local port to prevent the pcb from being 'bound' */
|
---|
| 2204 | pcb->local_port = 0;
|
---|
| 2205 |
|
---|
| 2206 | LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
|
---|
| 2207 | }
|
---|
| 2208 |
|
---|
| 2209 | /**
|
---|
| 2210 | * Calculates a new initial sequence number for new connections.
|
---|
| 2211 | *
|
---|
| 2212 | * @return u32_t pseudo random sequence number
|
---|
| 2213 | */
|
---|
| 2214 | u32_t
|
---|
| 2215 | tcp_next_iss(struct tcp_pcb *pcb)
|
---|
| 2216 | {
|
---|
| 2217 | #ifdef LWIP_HOOK_TCP_ISN
|
---|
| 2218 | LWIP_ASSERT("tcp_next_iss: invalid pcb", pcb != NULL);
|
---|
| 2219 | return LWIP_HOOK_TCP_ISN(&pcb->local_ip, pcb->local_port, &pcb->remote_ip, pcb->remote_port);
|
---|
| 2220 | #else /* LWIP_HOOK_TCP_ISN */
|
---|
| 2221 | static u32_t iss = 6510;
|
---|
| 2222 |
|
---|
| 2223 | LWIP_ASSERT("tcp_next_iss: invalid pcb", pcb != NULL);
|
---|
| 2224 | LWIP_UNUSED_ARG(pcb);
|
---|
| 2225 |
|
---|
| 2226 | iss += tcp_ticks; /* XXX */
|
---|
| 2227 | return iss;
|
---|
| 2228 | #endif /* LWIP_HOOK_TCP_ISN */
|
---|
| 2229 | }
|
---|
| 2230 |
|
---|
| 2231 | #if TCP_CALCULATE_EFF_SEND_MSS
|
---|
| 2232 | /**
|
---|
| 2233 | * Calculates the effective send mss that can be used for a specific IP address
|
---|
| 2234 | * by calculating the minimum of TCP_MSS and the mtu (if set) of the target
|
---|
| 2235 | * netif (if not NULL).
|
---|
| 2236 | */
|
---|
| 2237 | u16_t
|
---|
| 2238 | tcp_eff_send_mss_netif(u16_t sendmss, struct netif *outif, const ip_addr_t *dest)
|
---|
| 2239 | {
|
---|
| 2240 | u16_t mss_s;
|
---|
| 2241 | u16_t mtu;
|
---|
| 2242 |
|
---|
| 2243 | LWIP_UNUSED_ARG(dest); /* in case IPv6 is disabled */
|
---|
| 2244 |
|
---|
| 2245 | LWIP_ASSERT("tcp_eff_send_mss_netif: invalid dst_ip", dest != NULL);
|
---|
| 2246 |
|
---|
| 2247 | #if LWIP_IPV6
|
---|
| 2248 | #if LWIP_IPV4
|
---|
| 2249 | if (IP_IS_V6(dest))
|
---|
| 2250 | #endif /* LWIP_IPV4 */
|
---|
| 2251 | {
|
---|
| 2252 | /* First look in destination cache, to see if there is a Path MTU. */
|
---|
| 2253 | mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
|
---|
| 2254 | }
|
---|
| 2255 | #if LWIP_IPV4
|
---|
| 2256 | else
|
---|
| 2257 | #endif /* LWIP_IPV4 */
|
---|
| 2258 | #endif /* LWIP_IPV6 */
|
---|
| 2259 | #if LWIP_IPV4
|
---|
| 2260 | {
|
---|
| 2261 | if (outif == NULL) {
|
---|
| 2262 | return sendmss;
|
---|
| 2263 | }
|
---|
| 2264 | mtu = outif->mtu;
|
---|
| 2265 | }
|
---|
| 2266 | #endif /* LWIP_IPV4 */
|
---|
| 2267 |
|
---|
| 2268 | if (mtu != 0) {
|
---|
| 2269 | u16_t offset;
|
---|
| 2270 | #if LWIP_IPV6
|
---|
| 2271 | #if LWIP_IPV4
|
---|
| 2272 | if (IP_IS_V6(dest))
|
---|
| 2273 | #endif /* LWIP_IPV4 */
|
---|
| 2274 | {
|
---|
| 2275 | offset = IP6_HLEN + TCP_HLEN;
|
---|
| 2276 | }
|
---|
| 2277 | #if LWIP_IPV4
|
---|
| 2278 | else
|
---|
| 2279 | #endif /* LWIP_IPV4 */
|
---|
| 2280 | #endif /* LWIP_IPV6 */
|
---|
| 2281 | #if LWIP_IPV4
|
---|
| 2282 | {
|
---|
| 2283 | offset = IP_HLEN + TCP_HLEN;
|
---|
| 2284 | }
|
---|
| 2285 | #endif /* LWIP_IPV4 */
|
---|
| 2286 | mss_s = (mtu > offset) ? (u16_t)(mtu - offset) : 0;
|
---|
| 2287 | /* RFC 1122, chap 4.2.2.6:
|
---|
| 2288 | * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
|
---|
| 2289 | * We correct for TCP options in tcp_write(), and don't support IP options.
|
---|
| 2290 | */
|
---|
| 2291 | sendmss = LWIP_MIN(sendmss, mss_s);
|
---|
| 2292 | }
|
---|
| 2293 | return sendmss;
|
---|
| 2294 | }
|
---|
| 2295 | #endif /* TCP_CALCULATE_EFF_SEND_MSS */
|
---|
| 2296 |
|
---|
| 2297 | /** Helper function for tcp_netif_ip_addr_changed() that iterates a pcb list */
|
---|
| 2298 | static void
|
---|
| 2299 | tcp_netif_ip_addr_changed_pcblist(const ip_addr_t *old_addr, struct tcp_pcb *pcb_list)
|
---|
| 2300 | {
|
---|
| 2301 | struct tcp_pcb *pcb;
|
---|
| 2302 | pcb = pcb_list;
|
---|
| 2303 |
|
---|
| 2304 | LWIP_ASSERT("tcp_netif_ip_addr_changed_pcblist: invalid old_addr", old_addr != NULL);
|
---|
| 2305 |
|
---|
| 2306 | while (pcb != NULL) {
|
---|
| 2307 | /* PCB bound to current local interface address? */
|
---|
| 2308 | if (ip_addr_cmp(&pcb->local_ip, old_addr)
|
---|
| 2309 | #if LWIP_AUTOIP
|
---|
| 2310 | /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
|
---|
| 2311 | && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)))
|
---|
| 2312 | #endif /* LWIP_AUTOIP */
|
---|
| 2313 | ) {
|
---|
| 2314 | /* this connection must be aborted */
|
---|
| 2315 | struct tcp_pcb *next = pcb->next;
|
---|
| 2316 | LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
|
---|
| 2317 | tcp_abort(pcb);
|
---|
| 2318 | pcb = next;
|
---|
| 2319 | } else {
|
---|
| 2320 | pcb = pcb->next;
|
---|
| 2321 | }
|
---|
| 2322 | }
|
---|
| 2323 | }
|
---|
| 2324 |
|
---|
| 2325 | /** This function is called from netif.c when address is changed or netif is removed
|
---|
| 2326 | *
|
---|
| 2327 | * @param old_addr IP address of the netif before change
|
---|
| 2328 | * @param new_addr IP address of the netif after change or NULL if netif has been removed
|
---|
| 2329 | */
|
---|
| 2330 | void
|
---|
| 2331 | tcp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
|
---|
| 2332 | {
|
---|
| 2333 | struct tcp_pcb_listen *lpcb;
|
---|
| 2334 |
|
---|
| 2335 | if (!ip_addr_isany(old_addr)) {
|
---|
| 2336 | tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs);
|
---|
| 2337 | tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
|
---|
| 2338 |
|
---|
| 2339 | if (!ip_addr_isany(new_addr)) {
|
---|
| 2340 | /* PCB bound to current local interface address? */
|
---|
| 2341 | for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
|
---|
| 2342 | /* PCB bound to current local interface address? */
|
---|
| 2343 | if (ip_addr_cmp(&lpcb->local_ip, old_addr)) {
|
---|
| 2344 | /* The PCB is listening to the old ipaddr and
|
---|
| 2345 | * is set to listen to the new one instead */
|
---|
| 2346 | ip_addr_copy(lpcb->local_ip, *new_addr);
|
---|
| 2347 | }
|
---|
| 2348 | }
|
---|
| 2349 | }
|
---|
| 2350 | }
|
---|
| 2351 | }
|
---|
| 2352 |
|
---|
| 2353 | const char *
|
---|
| 2354 | tcp_debug_state_str(enum tcp_state s)
|
---|
| 2355 | {
|
---|
| 2356 | return tcp_state_str[s];
|
---|
| 2357 | }
|
---|
| 2358 |
|
---|
| 2359 | err_t
|
---|
| 2360 | tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_addr_t *addr, u16_t *port)
|
---|
| 2361 | {
|
---|
| 2362 | if (pcb) {
|
---|
| 2363 | if (local) {
|
---|
| 2364 | if (addr) {
|
---|
| 2365 | *addr = pcb->local_ip;
|
---|
| 2366 | }
|
---|
| 2367 | if (port) {
|
---|
| 2368 | *port = pcb->local_port;
|
---|
| 2369 | }
|
---|
| 2370 | } else {
|
---|
| 2371 | if (addr) {
|
---|
| 2372 | *addr = pcb->remote_ip;
|
---|
| 2373 | }
|
---|
| 2374 | if (port) {
|
---|
| 2375 | *port = pcb->remote_port;
|
---|
| 2376 | }
|
---|
| 2377 | }
|
---|
| 2378 | return ERR_OK;
|
---|
| 2379 | }
|
---|
| 2380 | return ERR_VAL;
|
---|
| 2381 | }
|
---|
| 2382 |
|
---|
| 2383 | #if TCP_QUEUE_OOSEQ
|
---|
| 2384 | /* Free all ooseq pbufs (and possibly reset SACK state) */
|
---|
| 2385 | void
|
---|
| 2386 | tcp_free_ooseq(struct tcp_pcb *pcb)
|
---|
| 2387 | {
|
---|
| 2388 | if (pcb->ooseq) {
|
---|
| 2389 | tcp_segs_free(pcb->ooseq);
|
---|
| 2390 | pcb->ooseq = NULL;
|
---|
| 2391 | #if LWIP_TCP_SACK_OUT
|
---|
| 2392 | memset(pcb->rcv_sacks, 0, sizeof(pcb->rcv_sacks));
|
---|
| 2393 | #endif /* LWIP_TCP_SACK_OUT */
|
---|
| 2394 | }
|
---|
| 2395 | }
|
---|
| 2396 | #endif /* TCP_QUEUE_OOSEQ */
|
---|
| 2397 |
|
---|
| 2398 | #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
|
---|
| 2399 | /**
|
---|
| 2400 | * Print a tcp header for debugging purposes.
|
---|
| 2401 | *
|
---|
| 2402 | * @param tcphdr pointer to a struct tcp_hdr
|
---|
| 2403 | */
|
---|
| 2404 | void
|
---|
| 2405 | tcp_debug_print(struct tcp_hdr *tcphdr)
|
---|
| 2406 | {
|
---|
| 2407 | LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
|
---|
| 2408 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
|
---|
| 2409 | LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
|
---|
| 2410 | lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest)));
|
---|
| 2411 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
|
---|
| 2412 | LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
|
---|
| 2413 | lwip_ntohl(tcphdr->seqno)));
|
---|
| 2414 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
|
---|
| 2415 | LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
|
---|
| 2416 | lwip_ntohl(tcphdr->ackno)));
|
---|
| 2417 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
|
---|
| 2418 | LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
|
---|
| 2419 | TCPH_HDRLEN(tcphdr),
|
---|
| 2420 | (u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1),
|
---|
| 2421 | (u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1),
|
---|
| 2422 | (u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1),
|
---|
| 2423 | (u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1),
|
---|
| 2424 | (u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1),
|
---|
| 2425 | (u16_t)(TCPH_FLAGS(tcphdr) & 1),
|
---|
| 2426 | lwip_ntohs(tcphdr->wnd)));
|
---|
| 2427 | tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
|
---|
| 2428 | LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
|
---|
| 2429 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
|
---|
| 2430 | LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
|
---|
| 2431 | lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp)));
|
---|
| 2432 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
|
---|
| 2433 | }
|
---|
| 2434 |
|
---|
| 2435 | /**
|
---|
| 2436 | * Print a tcp state for debugging purposes.
|
---|
| 2437 | *
|
---|
| 2438 | * @param s enum tcp_state to print
|
---|
| 2439 | */
|
---|
| 2440 | void
|
---|
| 2441 | tcp_debug_print_state(enum tcp_state s)
|
---|
| 2442 | {
|
---|
| 2443 | LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
|
---|
| 2444 | }
|
---|
| 2445 |
|
---|
| 2446 | /**
|
---|
| 2447 | * Print tcp flags for debugging purposes.
|
---|
| 2448 | *
|
---|
| 2449 | * @param flags tcp flags, all active flags are printed
|
---|
| 2450 | */
|
---|
| 2451 | void
|
---|
| 2452 | tcp_debug_print_flags(u8_t flags)
|
---|
| 2453 | {
|
---|
| 2454 | if (flags & TCP_FIN) {
|
---|
| 2455 | LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
|
---|
| 2456 | }
|
---|
| 2457 | if (flags & TCP_SYN) {
|
---|
| 2458 | LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
|
---|
| 2459 | }
|
---|
| 2460 | if (flags & TCP_RST) {
|
---|
| 2461 | LWIP_DEBUGF(TCP_DEBUG, ("RST "));
|
---|
| 2462 | }
|
---|
| 2463 | if (flags & TCP_PSH) {
|
---|
| 2464 | LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
|
---|
| 2465 | }
|
---|
| 2466 | if (flags & TCP_ACK) {
|
---|
| 2467 | LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
|
---|
| 2468 | }
|
---|
| 2469 | if (flags & TCP_URG) {
|
---|
| 2470 | LWIP_DEBUGF(TCP_DEBUG, ("URG "));
|
---|
| 2471 | }
|
---|
| 2472 | if (flags & TCP_ECE) {
|
---|
| 2473 | LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
|
---|
| 2474 | }
|
---|
| 2475 | if (flags & TCP_CWR) {
|
---|
| 2476 | LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
|
---|
| 2477 | }
|
---|
| 2478 | LWIP_DEBUGF(TCP_DEBUG, ("\n"));
|
---|
| 2479 | }
|
---|
| 2480 |
|
---|
| 2481 | /**
|
---|
| 2482 | * Print all tcp_pcbs in every list for debugging purposes.
|
---|
| 2483 | */
|
---|
| 2484 | void
|
---|
| 2485 | tcp_debug_print_pcbs(void)
|
---|
| 2486 | {
|
---|
| 2487 | struct tcp_pcb *pcb;
|
---|
| 2488 | struct tcp_pcb_listen *pcbl;
|
---|
| 2489 |
|
---|
| 2490 | LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
|
---|
| 2491 | for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 2492 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
|
---|
| 2493 | pcb->local_port, pcb->remote_port,
|
---|
| 2494 | pcb->snd_nxt, pcb->rcv_nxt));
|
---|
| 2495 | tcp_debug_print_state(pcb->state);
|
---|
| 2496 | }
|
---|
| 2497 |
|
---|
| 2498 | LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
|
---|
| 2499 | for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) {
|
---|
| 2500 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F" ", pcbl->local_port));
|
---|
| 2501 | tcp_debug_print_state(pcbl->state);
|
---|
| 2502 | }
|
---|
| 2503 |
|
---|
| 2504 | LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
|
---|
| 2505 | for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 2506 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
|
---|
| 2507 | pcb->local_port, pcb->remote_port,
|
---|
| 2508 | pcb->snd_nxt, pcb->rcv_nxt));
|
---|
| 2509 | tcp_debug_print_state(pcb->state);
|
---|
| 2510 | }
|
---|
| 2511 | }
|
---|
| 2512 |
|
---|
| 2513 | /**
|
---|
| 2514 | * Check state consistency of the tcp_pcb lists.
|
---|
| 2515 | */
|
---|
| 2516 | s16_t
|
---|
| 2517 | tcp_pcbs_sane(void)
|
---|
| 2518 | {
|
---|
| 2519 | struct tcp_pcb *pcb;
|
---|
| 2520 | for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 2521 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
|
---|
| 2522 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
|
---|
| 2523 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
|
---|
| 2524 | }
|
---|
| 2525 | for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
|
---|
| 2526 | LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
|
---|
| 2527 | }
|
---|
| 2528 | return 1;
|
---|
| 2529 | }
|
---|
| 2530 | #endif /* TCP_DEBUG */
|
---|
| 2531 |
|
---|
| 2532 | #if LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
| 2533 | /**
|
---|
| 2534 | * @defgroup tcp_raw_extargs ext arguments
|
---|
| 2535 | * @ingroup tcp_raw
|
---|
| 2536 | * Additional data storage per tcp pcb\n
|
---|
| 2537 | * @see @ref tcp_raw
|
---|
| 2538 | *
|
---|
| 2539 | * When LWIP_TCP_PCB_NUM_EXT_ARGS is > 0, every tcp pcb (including listen pcb)
|
---|
| 2540 | * includes a number of additional argument entries in an array.
|
---|
| 2541 | *
|
---|
| 2542 | * To support memory management, in addition to a 'void *', callbacks can be
|
---|
| 2543 | * provided to manage transition from listening pcbs to connections and to
|
---|
| 2544 | * deallocate memory when a pcb is deallocated (see struct @ref tcp_ext_arg_callbacks).
|
---|
| 2545 | *
|
---|
| 2546 | * After allocating this index, use @ref tcp_ext_arg_set and @ref tcp_ext_arg_get
|
---|
| 2547 | * to store and load arguments from this index for a given pcb.
|
---|
| 2548 | */
|
---|
| 2549 |
|
---|
| 2550 | static u8_t tcp_ext_arg_id;
|
---|
| 2551 |
|
---|
| 2552 | /**
|
---|
| 2553 | * @ingroup tcp_raw_extargs
|
---|
| 2554 | * Allocate an index to store data in ext_args member of struct tcp_pcb.
|
---|
| 2555 | * Returned value is an index in mentioned array.
|
---|
| 2556 | * The index is *global* over all pcbs!
|
---|
| 2557 | *
|
---|
| 2558 | * When @ref LWIP_TCP_PCB_NUM_EXT_ARGS is > 0, every tcp pcb (including listen pcb)
|
---|
| 2559 | * includes a number of additional argument entries in an array.
|
---|
| 2560 | *
|
---|
| 2561 | * To support memory management, in addition to a 'void *', callbacks can be
|
---|
| 2562 | * provided to manage transition from listening pcbs to connections and to
|
---|
| 2563 | * deallocate memory when a pcb is deallocated (see struct @ref tcp_ext_arg_callbacks).
|
---|
| 2564 | *
|
---|
| 2565 | * After allocating this index, use @ref tcp_ext_arg_set and @ref tcp_ext_arg_get
|
---|
| 2566 | * to store and load arguments from this index for a given pcb.
|
---|
| 2567 | *
|
---|
| 2568 | * @return a unique index into struct tcp_pcb.ext_args
|
---|
| 2569 | */
|
---|
| 2570 | u8_t
|
---|
| 2571 | tcp_ext_arg_alloc_id(void)
|
---|
| 2572 | {
|
---|
| 2573 | u8_t result = tcp_ext_arg_id;
|
---|
| 2574 | tcp_ext_arg_id++;
|
---|
| 2575 |
|
---|
| 2576 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2577 |
|
---|
| 2578 | #if LWIP_TCP_PCB_NUM_EXT_ARGS >= 255
|
---|
| 2579 | #error LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
| 2580 | #endif
|
---|
| 2581 | LWIP_ASSERT("Increase LWIP_TCP_PCB_NUM_EXT_ARGS in lwipopts.h", result < LWIP_TCP_PCB_NUM_EXT_ARGS);
|
---|
| 2582 | return result;
|
---|
| 2583 | }
|
---|
| 2584 |
|
---|
| 2585 | /**
|
---|
| 2586 | * @ingroup tcp_raw_extargs
|
---|
| 2587 | * Set callbacks for a given index of ext_args on the specified pcb.
|
---|
| 2588 | *
|
---|
| 2589 | * @param pcb tcp_pcb for which to set the callback
|
---|
| 2590 | * @param id ext_args index to set (allocated via @ref tcp_ext_arg_alloc_id)
|
---|
| 2591 | * @param callbacks callback table (const since it is referenced, not copied!)
|
---|
| 2592 | */
|
---|
| 2593 | void
|
---|
| 2594 | tcp_ext_arg_set_callbacks(struct tcp_pcb *pcb, uint8_t id, const struct tcp_ext_arg_callbacks * const callbacks)
|
---|
| 2595 | {
|
---|
| 2596 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 2597 | LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
|
---|
| 2598 | LWIP_ASSERT("callbacks != NULL", callbacks != NULL);
|
---|
| 2599 |
|
---|
| 2600 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2601 |
|
---|
| 2602 | pcb->ext_args[id].callbacks = callbacks;
|
---|
| 2603 | }
|
---|
| 2604 |
|
---|
| 2605 | /**
|
---|
| 2606 | * @ingroup tcp_raw_extargs
|
---|
| 2607 | * Set data for a given index of ext_args on the specified pcb.
|
---|
| 2608 | *
|
---|
| 2609 | * @param pcb tcp_pcb for which to set the data
|
---|
| 2610 | * @param id ext_args index to set (allocated via @ref tcp_ext_arg_alloc_id)
|
---|
| 2611 | * @param arg data pointer to set
|
---|
| 2612 | */
|
---|
| 2613 | void tcp_ext_arg_set(struct tcp_pcb *pcb, uint8_t id, void *arg)
|
---|
| 2614 | {
|
---|
| 2615 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 2616 | LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
|
---|
| 2617 |
|
---|
| 2618 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2619 |
|
---|
| 2620 | pcb->ext_args[id].data = arg;
|
---|
| 2621 | }
|
---|
| 2622 |
|
---|
| 2623 | /**
|
---|
| 2624 | * @ingroup tcp_raw_extargs
|
---|
| 2625 | * Set data for a given index of ext_args on the specified pcb.
|
---|
| 2626 | *
|
---|
| 2627 | * @param pcb tcp_pcb for which to set the data
|
---|
| 2628 | * @param id ext_args index to set (allocated via @ref tcp_ext_arg_alloc_id)
|
---|
| 2629 | * @return data pointer at the given index
|
---|
| 2630 | */
|
---|
| 2631 | void *tcp_ext_arg_get(const struct tcp_pcb *pcb, uint8_t id)
|
---|
| 2632 | {
|
---|
| 2633 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
| 2634 | LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
|
---|
| 2635 |
|
---|
| 2636 | LWIP_ASSERT_CORE_LOCKED();
|
---|
| 2637 |
|
---|
| 2638 | return pcb->ext_args[id].data;
|
---|
| 2639 | }
|
---|
| 2640 |
|
---|
| 2641 | /** This function calls the "destroy" callback for all ext_args once a pcb is
|
---|
| 2642 | * freed.
|
---|
| 2643 | */
|
---|
| 2644 | static void
|
---|
| 2645 | tcp_ext_arg_invoke_callbacks_destroyed(struct tcp_pcb_ext_args *ext_args)
|
---|
| 2646 | {
|
---|
| 2647 | int i;
|
---|
| 2648 | LWIP_ASSERT("ext_args != NULL", ext_args != NULL);
|
---|
| 2649 |
|
---|
| 2650 | for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) {
|
---|
| 2651 | if (ext_args[i].callbacks != NULL) {
|
---|
| 2652 | if (ext_args[i].callbacks->destroy != NULL) {
|
---|
| 2653 | ext_args[i].callbacks->destroy((u8_t)i, ext_args[i].data);
|
---|
| 2654 | }
|
---|
| 2655 | }
|
---|
| 2656 | }
|
---|
| 2657 | }
|
---|
| 2658 |
|
---|
| 2659 | /** This function calls the "passive_open" callback for all ext_args if a connection
|
---|
| 2660 | * is in the process of being accepted. This is called just after the SYN is
|
---|
| 2661 | * received and before a SYN/ACK is sent, to allow to modify the very first
|
---|
| 2662 | * segment sent even on passive open. Naturally, the "accepted" callback of the
|
---|
| 2663 | * pcb has not been called yet!
|
---|
| 2664 | */
|
---|
| 2665 | err_t
|
---|
| 2666 | tcp_ext_arg_invoke_callbacks_passive_open(struct tcp_pcb_listen *lpcb, struct tcp_pcb *cpcb)
|
---|
| 2667 | {
|
---|
| 2668 | int i;
|
---|
| 2669 | LWIP_ASSERT("lpcb != NULL", lpcb != NULL);
|
---|
| 2670 | LWIP_ASSERT("cpcb != NULL", cpcb != NULL);
|
---|
| 2671 |
|
---|
| 2672 | for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) {
|
---|
| 2673 | if (lpcb->ext_args[i].callbacks != NULL) {
|
---|
| 2674 | if (lpcb->ext_args[i].callbacks->passive_open != NULL) {
|
---|
| 2675 | err_t err = lpcb->ext_args[i].callbacks->passive_open((u8_t)i, lpcb, cpcb);
|
---|
| 2676 | if (err != ERR_OK) {
|
---|
| 2677 | return err;
|
---|
| 2678 | }
|
---|
| 2679 | }
|
---|
| 2680 | }
|
---|
| 2681 | }
|
---|
| 2682 | return ERR_OK;
|
---|
| 2683 | }
|
---|
| 2684 | #endif /* LWIP_TCP_PCB_NUM_EXT_ARGS */
|
---|
| 2685 |
|
---|
| 2686 | #endif /* LWIP_TCP */
|
---|