Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
lwip_tcp.c
00001 /** 00002 * @file 00003 * Transmission Control Protocol for IP 00004 * See also @ref tcp_raw 00005 * 00006 * @defgroup tcp_raw TCP 00007 * @ingroup callbackstyle_api 00008 * Transmission Control Protocol for IP\n 00009 * @see @ref raw_api and @ref netconn 00010 * 00011 * Common functions for the TCP implementation, such as functinos 00012 * for manipulating the data structures and the TCP timer functions. TCP functions 00013 * related to input and output is found in tcp_in.c and tcp_out.c respectively.\n 00014 */ 00015 00016 /* 00017 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00018 * All rights reserved. 00019 * 00020 * Redistribution and use in source and binary forms, with or without modification, 00021 * are permitted provided that the following conditions are met: 00022 * 00023 * 1. Redistributions of source code must retain the above copyright notice, 00024 * this list of conditions and the following disclaimer. 00025 * 2. Redistributions in binary form must reproduce the above copyright notice, 00026 * this list of conditions and the following disclaimer in the documentation 00027 * and/or other materials provided with the distribution. 00028 * 3. The name of the author may not be used to endorse or promote products 00029 * derived from this software without specific prior written permission. 00030 * 00031 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00032 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00033 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00034 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00035 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00036 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00037 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00038 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00039 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00040 * OF SUCH DAMAGE. 00041 * 00042 * This file is part of the lwIP TCP/IP stack. 00043 * 00044 * Author: Adam Dunkels <adam@sics.se> 00045 * 00046 */ 00047 00048 #include "lwip/opt.h" 00049 00050 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */ 00051 00052 #include "lwip/def.h" 00053 #include "lwip/mem.h" 00054 #include "lwip/memp.h" 00055 #include "lwip/tcp.h" 00056 #include "lwip/priv/tcp_priv.h" 00057 #include "lwip/debug.h" 00058 #include "lwip/stats.h" 00059 #include "lwip/ip6.h" 00060 #include "lwip/ip6_addr.h" 00061 #include "lwip/nd6.h" 00062 00063 #include <string.h> 00064 00065 #ifndef TCP_LOCAL_PORT_RANGE_START 00066 /* From http://www.iana.org/assignments/port-numbers: 00067 "The Dynamic and/or Private Ports are those from 49152 through 65535" */ 00068 #define TCP_LOCAL_PORT_RANGE_START 0xc000 00069 #define TCP_LOCAL_PORT_RANGE_END 0xffff 00070 #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START)) 00071 #endif 00072 00073 #if LWIP_TCP_KEEPALIVE 00074 #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl) 00075 #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl) 00076 #else /* LWIP_TCP_KEEPALIVE */ 00077 #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE 00078 #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT 00079 #endif /* LWIP_TCP_KEEPALIVE */ 00080 00081 /* As initial send MSS, we use TCP_MSS but limit it to 536. */ 00082 #if TCP_MSS > 536 00083 #define INITIAL_MSS 536 00084 #else 00085 #define INITIAL_MSS TCP_MSS 00086 #endif 00087 00088 static const char * const tcp_state_str[] = { 00089 "CLOSED", 00090 "LISTEN", 00091 "SYN_SENT", 00092 "SYN_RCVD", 00093 "ESTABLISHED", 00094 "FIN_WAIT_1", 00095 "FIN_WAIT_2", 00096 "CLOSE_WAIT", 00097 "CLOSING", 00098 "LAST_ACK", 00099 "TIME_WAIT" 00100 }; 00101 00102 /* last local TCP port */ 00103 static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START; 00104 00105 /* Incremented every coarse grained timer shot (typically every 500 ms). */ 00106 u32_t tcp_ticks; 00107 static const u8_t tcp_backoff[13] = 00108 { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7}; 00109 /* Times per slowtmr hits */ 00110 static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 }; 00111 00112 /* The TCP PCB lists. */ 00113 00114 /** List of all TCP PCBs bound but not yet (connected || listening) */ 00115 struct tcp_pcb *tcp_bound_pcbs; 00116 /** List of all TCP PCBs in LISTEN state */ 00117 union tcp_listen_pcbs_t tcp_listen_pcbs; 00118 /** List of all TCP PCBs that are in a state in which 00119 * they accept or send data. */ 00120 struct tcp_pcb *tcp_active_pcbs; 00121 /** List of all TCP PCBs in TIME-WAIT state */ 00122 struct tcp_pcb *tcp_tw_pcbs; 00123 00124 /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */ 00125 struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs, 00126 &tcp_active_pcbs, &tcp_tw_pcbs}; 00127 00128 u8_t tcp_active_pcbs_changed; 00129 00130 /** Timer counter to handle calling slow-timer from tcp_tmr() */ 00131 static u8_t tcp_timer; 00132 static u8_t tcp_timer_ctr; 00133 static u16_t tcp_new_port(void); 00134 00135 /** 00136 * Initialize this module. 00137 */ 00138 void 00139 tcp_init(void) 00140 { 00141 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) 00142 tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND()); 00143 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */ 00144 } 00145 00146 /** 00147 * Called periodically to dispatch TCP timers. 00148 */ 00149 void 00150 tcp_tmr(void) 00151 { 00152 /* Call tcp_fasttmr() every 250 ms */ 00153 tcp_fasttmr(); 00154 00155 if (++tcp_timer & 1) { 00156 /* Call tcp_slowtmr() every 500 ms, i.e., every other timer 00157 tcp_tmr() is called. */ 00158 tcp_slowtmr(); 00159 } 00160 } 00161 00162 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG 00163 /** Called when a listen pcb is closed. Iterates one pcb list and removes the 00164 * closed listener pcb from pcb->listener if matching. 00165 */ 00166 static void 00167 tcp_remove_listener(struct tcp_pcb *list, struct tcp_pcb_listen *lpcb) 00168 { 00169 struct tcp_pcb *pcb; 00170 for (pcb = list; pcb != NULL; pcb = pcb->next) { 00171 if (pcb->listener == lpcb) { 00172 pcb->listener = NULL; 00173 } 00174 } 00175 } 00176 #endif 00177 00178 /** Called when a listen pcb is closed. Iterates all pcb lists and removes the 00179 * closed listener pcb from pcb->listener if matching. 00180 */ 00181 static void 00182 tcp_listen_closed(struct tcp_pcb *pcb) 00183 { 00184 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG 00185 size_t i; 00186 LWIP_ASSERT("pcb != NULL", pcb != NULL); 00187 LWIP_ASSERT("pcb->state == LISTEN", pcb->state == LISTEN); 00188 for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) { 00189 tcp_remove_listener(*tcp_pcb_lists[i], (struct tcp_pcb_listen*)pcb); 00190 } 00191 #endif 00192 LWIP_UNUSED_ARG(pcb); 00193 } 00194 00195 #if TCP_LISTEN_BACKLOG 00196 /** @ingroup tcp_raw 00197 * Delay accepting a connection in respect to the listen backlog: 00198 * the number of outstanding connections is increased until 00199 * tcp_backlog_accepted() is called. 00200 * 00201 * ATTENTION: the caller is responsible for calling tcp_backlog_accepted() 00202 * or else the backlog feature will get out of sync! 00203 * 00204 * @param pcb the connection pcb which is not fully accepted yet 00205 */ 00206 void 00207 tcp_backlog_delayed(struct tcp_pcb* pcb) 00208 { 00209 LWIP_ASSERT("pcb != NULL", pcb != NULL); 00210 if ((pcb->flags & TF_BACKLOGPEND) == 0) { 00211 if (pcb->listener != NULL) { 00212 pcb->listener->accepts_pending++; 00213 LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0); 00214 pcb->flags |= TF_BACKLOGPEND; 00215 } 00216 } 00217 } 00218 00219 /** @ingroup tcp_raw 00220 * A delayed-accept a connection is accepted (or closed/aborted): decreases 00221 * the number of outstanding connections after calling tcp_backlog_delayed(). 00222 * 00223 * ATTENTION: the caller is responsible for calling tcp_backlog_accepted() 00224 * or else the backlog feature will get out of sync! 00225 * 00226 * @param pcb the connection pcb which is now fully accepted (or closed/aborted) 00227 */ 00228 void 00229 tcp_backlog_accepted(struct tcp_pcb* pcb) 00230 { 00231 LWIP_ASSERT("pcb != NULL", pcb != NULL); 00232 if ((pcb->flags & TF_BACKLOGPEND) != 0) { 00233 if (pcb->listener != NULL) { 00234 LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0); 00235 pcb->listener->accepts_pending--; 00236 pcb->flags &= ~TF_BACKLOGPEND; 00237 } 00238 } 00239 } 00240 #endif /* TCP_LISTEN_BACKLOG */ 00241 00242 /** 00243 * Closes the TX side of a connection held by the PCB. 00244 * For tcp_close(), a RST is sent if the application didn't receive all data 00245 * (tcp_recved() not called for all data passed to recv callback). 00246 * 00247 * Listening pcbs are freed and may not be referenced any more. 00248 * Connection pcbs are freed if not yet connected and may not be referenced 00249 * any more. If a connection is established (at least SYN received or in 00250 * a closing state), the connection is closed, and put in a closing state. 00251 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore 00252 * unsafe to reference it. 00253 * 00254 * @param pcb the tcp_pcb to close 00255 * @return ERR_OK if connection has been closed 00256 * another err_t if closing failed and pcb is not freed 00257 */ 00258 static err_t 00259 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data) 00260 { 00261 err_t err; 00262 00263 if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) { 00264 if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) { 00265 /* Not all data received by application, send RST to tell the remote 00266 side about this. */ 00267 LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED); 00268 00269 /* don't call tcp_abort here: we must not deallocate the pcb since 00270 that might not be expected when calling tcp_close */ 00271 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip, 00272 pcb->local_port, pcb->remote_port); 00273 00274 tcp_pcb_purge(pcb); 00275 TCP_RMV_ACTIVE(pcb); 00276 if (pcb->state == ESTABLISHED) { 00277 /* move to TIME_WAIT since we close actively */ 00278 pcb->state = TIME_WAIT; 00279 TCP_REG(&tcp_tw_pcbs, pcb); 00280 } else { 00281 /* CLOSE_WAIT: deallocate the pcb since we already sent a RST for it */ 00282 if (tcp_input_pcb == pcb) { 00283 /* prevent using a deallocated pcb: free it from tcp_input later */ 00284 tcp_trigger_input_pcb_close(); 00285 } else { 00286 memp_free(MEMP_TCP_PCB, pcb); 00287 } 00288 } 00289 return ERR_OK; 00290 } 00291 } 00292 00293 switch (pcb->state) { 00294 case CLOSED: 00295 /* Closing a pcb in the CLOSED state might seem erroneous, 00296 * however, it is in this state once allocated and as yet unused 00297 * and the user needs some way to free it should the need arise. 00298 * Calling tcp_close() with a pcb that has already been closed, (i.e. twice) 00299 * or for a pcb that has been used and then entered the CLOSED state 00300 * is erroneous, but this should never happen as the pcb has in those cases 00301 * been freed, and so any remaining handles are bogus. */ 00302 err = ERR_OK; 00303 if (pcb->local_port != 0) { 00304 TCP_RMV(&tcp_bound_pcbs, pcb); 00305 } 00306 memp_free(MEMP_TCP_PCB, pcb); 00307 pcb = NULL; 00308 break; 00309 case LISTEN: 00310 err = ERR_OK; 00311 tcp_listen_closed(pcb); 00312 tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb); 00313 memp_free(MEMP_TCP_PCB_LISTEN, pcb); 00314 pcb = NULL; 00315 break; 00316 case SYN_SENT: 00317 err = ERR_OK; 00318 TCP_PCB_REMOVE_ACTIVE(pcb); 00319 memp_free(MEMP_TCP_PCB, pcb); 00320 pcb = NULL; 00321 MIB2_STATS_INC(mib2.tcpattemptfails); 00322 break; 00323 case SYN_RCVD: 00324 err = tcp_send_fin(pcb); 00325 if (err == ERR_OK) { 00326 tcp_backlog_accepted(pcb); 00327 MIB2_STATS_INC(mib2.tcpattemptfails); 00328 pcb->state = FIN_WAIT_1; 00329 } 00330 break; 00331 case ESTABLISHED: 00332 err = tcp_send_fin(pcb); 00333 if (err == ERR_OK) { 00334 MIB2_STATS_INC(mib2.tcpestabresets); 00335 pcb->state = FIN_WAIT_1; 00336 } 00337 break; 00338 case CLOSE_WAIT: 00339 err = tcp_send_fin(pcb); 00340 if (err == ERR_OK) { 00341 MIB2_STATS_INC(mib2.tcpestabresets); 00342 pcb->state = LAST_ACK; 00343 } 00344 break; 00345 default: 00346 /* Has already been closed, do nothing. */ 00347 err = ERR_OK; 00348 pcb = NULL; 00349 break; 00350 } 00351 00352 if (pcb != NULL && err == ERR_OK) { 00353 /* To ensure all data has been sent when tcp_close returns, we have 00354 to make sure tcp_output doesn't fail. 00355 Since we don't really have to ensure all data has been sent when tcp_close 00356 returns (unsent data is sent from tcp timer functions, also), we don't care 00357 for the return value of tcp_output for now. */ 00358 tcp_output(pcb); 00359 } 00360 return err; 00361 } 00362 00363 /** 00364 * @ingroup tcp_raw 00365 * Closes the connection held by the PCB. 00366 * 00367 * Listening pcbs are freed and may not be referenced any more. 00368 * Connection pcbs are freed if not yet connected and may not be referenced 00369 * any more. If a connection is established (at least SYN received or in 00370 * a closing state), the connection is closed, and put in a closing state. 00371 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore 00372 * unsafe to reference it (unless an error is returned). 00373 * 00374 * @param pcb the tcp_pcb to close 00375 * @return ERR_OK if connection has been closed 00376 * another err_t if closing failed and pcb is not freed 00377 */ 00378 err_t 00379 tcp_close(struct tcp_pcb *pcb) 00380 { 00381 LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in ")); 00382 tcp_debug_print_state(pcb->state); 00383 00384 if (pcb->state != LISTEN) { 00385 /* Set a flag not to receive any more data... */ 00386 pcb->flags |= TF_RXCLOSED; 00387 } 00388 /* ... and close */ 00389 return tcp_close_shutdown(pcb, 1); 00390 } 00391 00392 /** 00393 * @ingroup tcp_raw 00394 * Causes all or part of a full-duplex connection of this PCB to be shut down. 00395 * This doesn't deallocate the PCB unless shutting down both sides! 00396 * Shutting down both sides is the same as calling tcp_close, so if it succeds, 00397 * the PCB should not be referenced any more. 00398 * 00399 * @param pcb PCB to shutdown 00400 * @param shut_rx shut down receive side if this is != 0 00401 * @param shut_tx shut down send side if this is != 0 00402 * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down) 00403 * another err_t on error. 00404 */ 00405 err_t 00406 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx) 00407 { 00408 if (pcb->state == LISTEN) { 00409 return ERR_CONN; 00410 } 00411 if (shut_rx) { 00412 /* shut down the receive side: set a flag not to receive any more data... */ 00413 pcb->flags |= TF_RXCLOSED; 00414 if (shut_tx) { 00415 /* shutting down the tx AND rx side is the same as closing for the raw API */ 00416 return tcp_close_shutdown(pcb, 1); 00417 } 00418 /* ... and free buffered data */ 00419 if (pcb->refused_data != NULL) { 00420 pbuf_free(pcb->refused_data); 00421 pcb->refused_data = NULL; 00422 } 00423 } 00424 if (shut_tx) { 00425 /* This can't happen twice since if it succeeds, the pcb's state is changed. 00426 Only close in these states as the others directly deallocate the PCB */ 00427 switch (pcb->state) { 00428 case SYN_RCVD: 00429 case ESTABLISHED: 00430 case CLOSE_WAIT: 00431 return tcp_close_shutdown(pcb, (u8_t)shut_rx); 00432 default: 00433 /* Not (yet?) connected, cannot shutdown the TX side as that would bring us 00434 into CLOSED state, where the PCB is deallocated. */ 00435 return ERR_CONN; 00436 } 00437 } 00438 return ERR_OK; 00439 } 00440 00441 /** 00442 * Abandons a connection and optionally sends a RST to the remote 00443 * host. Deletes the local protocol control block. This is done when 00444 * a connection is killed because of shortage of memory. 00445 * 00446 * @param pcb the tcp_pcb to abort 00447 * @param reset boolean to indicate whether a reset should be sent 00448 */ 00449 void 00450 tcp_abandon(struct tcp_pcb *pcb, int reset) 00451 { 00452 u32_t seqno, ackno; 00453 #if LWIP_CALLBACK_API 00454 tcp_err_fn errf; 00455 #endif /* LWIP_CALLBACK_API */ 00456 void *errf_arg; 00457 00458 /* pcb->state LISTEN not allowed here */ 00459 LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs", 00460 pcb->state != LISTEN); 00461 /* Figure out on which TCP PCB list we are, and remove us. If we 00462 are in an active state, call the receive function associated with 00463 the PCB with a NULL argument, and send an RST to the remote end. */ 00464 if (pcb->state == TIME_WAIT) { 00465 tcp_pcb_remove(&tcp_tw_pcbs, pcb); 00466 memp_free(MEMP_TCP_PCB, pcb); 00467 } else { 00468 int send_rst = 0; 00469 u16_t local_port = 0; 00470 seqno = pcb->snd_nxt; 00471 ackno = pcb->rcv_nxt; 00472 #if LWIP_CALLBACK_API 00473 errf = pcb->errf; 00474 #endif /* LWIP_CALLBACK_API */ 00475 errf_arg = pcb->callback_arg; 00476 if (pcb->state == CLOSED) { 00477 if (pcb->local_port != 0) { 00478 /* bound, not yet opened */ 00479 TCP_RMV(&tcp_bound_pcbs, pcb); 00480 } 00481 } else { 00482 send_rst = reset; 00483 local_port = pcb->local_port; 00484 TCP_PCB_REMOVE_ACTIVE(pcb); 00485 } 00486 if (pcb->unacked != NULL) { 00487 tcp_segs_free(pcb->unacked); 00488 } 00489 if (pcb->unsent != NULL) { 00490 tcp_segs_free(pcb->unsent); 00491 } 00492 #if TCP_QUEUE_OOSEQ 00493 if (pcb->ooseq != NULL) { 00494 tcp_segs_free(pcb->ooseq); 00495 } 00496 #endif /* TCP_QUEUE_OOSEQ */ 00497 tcp_backlog_accepted(pcb); 00498 if (send_rst) { 00499 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n")); 00500 tcp_rst(seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port); 00501 } 00502 memp_free(MEMP_TCP_PCB, pcb); 00503 TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT); 00504 } 00505 } 00506 00507 /** 00508 * @ingroup tcp_raw 00509 * Aborts the connection by sending a RST (reset) segment to the remote 00510 * host. The pcb is deallocated. This function never fails. 00511 * 00512 * ATTENTION: When calling this from one of the TCP callbacks, make 00513 * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise 00514 * or you will risk accessing deallocated memory or memory leaks! 00515 * 00516 * @param pcb the tcp pcb to abort 00517 */ 00518 void 00519 tcp_abort(struct tcp_pcb *pcb) 00520 { 00521 tcp_abandon(pcb, 1); 00522 } 00523 00524 /** 00525 * @ingroup tcp_raw 00526 * Binds the connection to a local port number and IP address. If the 00527 * IP address is not given (i.e., ipaddr == NULL), the IP address of 00528 * the outgoing network interface is used instead. 00529 * 00530 * @param pcb the tcp_pcb to bind (no check is done whether this pcb is 00531 * already bound!) 00532 * @param ipaddr the local ip address to bind to (use IP4_ADDR_ANY to bind 00533 * to any local address 00534 * @param port the local port to bind to 00535 * @return ERR_USE if the port is already in use 00536 * ERR_VAL if bind failed because the PCB is not in a valid state 00537 * ERR_OK if bound 00538 */ 00539 err_t 00540 tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) 00541 { 00542 int i; 00543 int max_pcb_list = NUM_TCP_PCB_LISTS; 00544 struct tcp_pcb *cpcb; 00545 00546 #if LWIP_IPV4 00547 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */ 00548 if (ipaddr == NULL) { 00549 ipaddr = IP4_ADDR_ANY; 00550 } 00551 #endif /* LWIP_IPV4 */ 00552 00553 /* still need to check for ipaddr == NULL in IPv6 only case */ 00554 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { 00555 return ERR_VAL; 00556 } 00557 00558 LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL); 00559 00560 #if SO_REUSE 00561 /* Unless the REUSEADDR flag is set, 00562 we have to check the pcbs in TIME-WAIT state, also. 00563 We do not dump TIME_WAIT pcb's; they can still be matched by incoming 00564 packets using both local and remote IP addresses and ports to distinguish. 00565 */ 00566 if (ip_get_option(pcb, SOF_REUSEADDR)) { 00567 max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT; 00568 } 00569 #endif /* SO_REUSE */ 00570 00571 if (port == 0) { 00572 port = tcp_new_port(); 00573 if (port == 0) { 00574 return ERR_BUF; 00575 } 00576 } else { 00577 /* Check if the address already is in use (on all lists) */ 00578 for (i = 0; i < max_pcb_list; i++) { 00579 for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { 00580 if (cpcb->local_port == port) { 00581 #if SO_REUSE 00582 /* Omit checking for the same port if both pcbs have REUSEADDR set. 00583 For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in 00584 tcp_connect. */ 00585 if (!ip_get_option(pcb, SOF_REUSEADDR) || 00586 !ip_get_option(cpcb, SOF_REUSEADDR)) 00587 #endif /* SO_REUSE */ 00588 { 00589 /* @todo: check accept_any_ip_version */ 00590 if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) && 00591 (ip_addr_isany(&cpcb->local_ip) || 00592 ip_addr_isany(ipaddr) || 00593 ip_addr_cmp(&cpcb->local_ip, ipaddr))) { 00594 return ERR_USE; 00595 } 00596 } 00597 } 00598 } 00599 } 00600 } 00601 00602 if (!ip_addr_isany(ipaddr)) { 00603 ip_addr_set(&pcb->local_ip, ipaddr); 00604 } 00605 pcb->local_port = port; 00606 TCP_REG(&tcp_bound_pcbs, pcb); 00607 LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port)); 00608 return ERR_OK; 00609 } 00610 #if LWIP_CALLBACK_API 00611 /** 00612 * Default accept callback if no accept callback is specified by the user. 00613 */ 00614 static err_t 00615 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err) 00616 { 00617 LWIP_UNUSED_ARG(arg); 00618 LWIP_UNUSED_ARG(err); 00619 00620 tcp_abort(pcb); 00621 00622 return ERR_ABRT; 00623 } 00624 #endif /* LWIP_CALLBACK_API */ 00625 00626 /** 00627 * @ingroup tcp_raw 00628 * Set the state of the connection to be LISTEN, which means that it 00629 * is able to accept incoming connections. The protocol control block 00630 * is reallocated in order to consume less memory. Setting the 00631 * connection to LISTEN is an irreversible process. 00632 * 00633 * @param pcb the original tcp_pcb 00634 * @param backlog the incoming connections queue limit 00635 * @return tcp_pcb used for listening, consumes less memory. 00636 * 00637 * @note The original tcp_pcb is freed. This function therefore has to be 00638 * called like this: 00639 * tpcb = tcp_listen_with_backlog(tpcb, backlog); 00640 */ 00641 struct tcp_pcb * 00642 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog) 00643 { 00644 return tcp_listen_with_backlog_and_err(pcb, backlog, NULL); 00645 } 00646 00647 /** 00648 * @ingroup tcp_raw 00649 * Set the state of the connection to be LISTEN, which means that it 00650 * is able to accept incoming connections. The protocol control block 00651 * is reallocated in order to consume less memory. Setting the 00652 * connection to LISTEN is an irreversible process. 00653 * 00654 * @param pcb the original tcp_pcb 00655 * @param backlog the incoming connections queue limit 00656 * @param err when NULL is returned, this contains the error reason 00657 * @return tcp_pcb used for listening, consumes less memory. 00658 * 00659 * @note The original tcp_pcb is freed. This function therefore has to be 00660 * called like this: 00661 * tpcb = tcp_listen_with_backlog_and_err(tpcb, backlog, &err); 00662 */ 00663 struct tcp_pcb * 00664 tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err) 00665 { 00666 struct tcp_pcb_listen *lpcb = NULL; 00667 err_t res; 00668 00669 LWIP_UNUSED_ARG(backlog); 00670 LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, res = ERR_CLSD; goto done); 00671 00672 /* already listening? */ 00673 if (pcb->state == LISTEN) { 00674 lpcb = (struct tcp_pcb_listen*)pcb; 00675 res = ERR_ALREADY; 00676 goto done; 00677 } 00678 #if SO_REUSE 00679 if (ip_get_option(pcb, SOF_REUSEADDR)) { 00680 /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage 00681 is declared (listen-/connection-pcb), we have to make sure now that 00682 this port is only used once for every local IP. */ 00683 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { 00684 if ((lpcb->local_port == pcb->local_port) && 00685 ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) { 00686 /* this address/port is already used */ 00687 lpcb = NULL; 00688 res = ERR_USE; 00689 goto done; 00690 } 00691 } 00692 } 00693 #endif /* SO_REUSE */ 00694 lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN); 00695 if (lpcb == NULL) { 00696 res = ERR_MEM; 00697 goto done; 00698 } 00699 lpcb->callback_arg = pcb->callback_arg; 00700 lpcb->local_port = pcb->local_port; 00701 lpcb->state = LISTEN; 00702 lpcb->prio = pcb->prio; 00703 lpcb->so_options = pcb->so_options; 00704 lpcb->ttl = pcb->ttl; 00705 lpcb->tos = pcb->tos; 00706 #if LWIP_IPV4 && LWIP_IPV6 00707 IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type); 00708 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00709 ip_addr_copy(lpcb->local_ip, pcb->local_ip); 00710 if (pcb->local_port != 0) { 00711 TCP_RMV(&tcp_bound_pcbs, pcb); 00712 } 00713 memp_free(MEMP_TCP_PCB, pcb); 00714 #if LWIP_CALLBACK_API 00715 lpcb->accept = tcp_accept_null; 00716 #endif /* LWIP_CALLBACK_API */ 00717 #if TCP_LISTEN_BACKLOG 00718 lpcb->accepts_pending = 0; 00719 tcp_backlog_set(lpcb, backlog); 00720 #endif /* TCP_LISTEN_BACKLOG */ 00721 TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb); 00722 res = ERR_OK; 00723 done: 00724 if (err != NULL) { 00725 *err = res; 00726 } 00727 return (struct tcp_pcb *)lpcb; 00728 } 00729 00730 /** 00731 * Update the state that tracks the available window space to advertise. 00732 * 00733 * Returns how much extra window would be advertised if we sent an 00734 * update now. 00735 */ 00736 u32_t 00737 tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb) 00738 { 00739 u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd; 00740 00741 if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) { 00742 /* we can advertise more window */ 00743 pcb->rcv_ann_wnd = pcb->rcv_wnd; 00744 return new_right_edge - pcb->rcv_ann_right_edge; 00745 } else { 00746 if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) { 00747 /* Can happen due to other end sending out of advertised window, 00748 * but within actual available (but not yet advertised) window */ 00749 pcb->rcv_ann_wnd = 0; 00750 } else { 00751 /* keep the right edge of window constant */ 00752 u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt; 00753 #if !LWIP_WND_SCALE 00754 LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff); 00755 #endif 00756 pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd; 00757 } 00758 return 0; 00759 } 00760 } 00761 00762 /** 00763 * @ingroup tcp_raw 00764 * This function should be called by the application when it has 00765 * processed the data. The purpose is to advertise a larger window 00766 * when the data has been processed. 00767 * 00768 * @param pcb the tcp_pcb for which data is read 00769 * @param len the amount of bytes that have been read by the application 00770 */ 00771 void 00772 tcp_recved(struct tcp_pcb *pcb, u16_t len) 00773 { 00774 int wnd_inflation; 00775 00776 /* pcb->state LISTEN not allowed here */ 00777 LWIP_ASSERT("don't call tcp_recved for listen-pcbs", 00778 pcb->state != LISTEN); 00779 00780 pcb->rcv_wnd += len; 00781 if (pcb->rcv_wnd > TCP_WND_MAX(pcb)) { 00782 pcb->rcv_wnd = TCP_WND_MAX(pcb); 00783 } else if (pcb->rcv_wnd == 0) { 00784 /* rcv_wnd overflowed */ 00785 if ((pcb->state == CLOSE_WAIT) || (pcb->state == LAST_ACK)) { 00786 /* In passive close, we allow this, since the FIN bit is added to rcv_wnd 00787 by the stack itself, since it is not mandatory for an application 00788 to call tcp_recved() for the FIN bit, but e.g. the netconn API does so. */ 00789 pcb->rcv_wnd = TCP_WND_MAX(pcb); 00790 } else { 00791 LWIP_ASSERT("tcp_recved: len wrapped rcv_wnd\n", 0); 00792 } 00793 } 00794 00795 wnd_inflation = tcp_update_rcv_ann_wnd(pcb); 00796 00797 /* If the change in the right edge of window is significant (default 00798 * watermark is TCP_WND/4), then send an explicit update now. 00799 * Otherwise wait for a packet to be sent in the normal course of 00800 * events (or more window to be available later) */ 00801 if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) { 00802 tcp_ack_now(pcb); 00803 tcp_output(pcb); 00804 } 00805 00806 LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n", 00807 len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd))); 00808 } 00809 00810 /** 00811 * Allocate a new local TCP port. 00812 * 00813 * @return a new (free) local TCP port number 00814 */ 00815 static u16_t 00816 tcp_new_port(void) 00817 { 00818 u8_t i; 00819 u16_t n = 0; 00820 struct tcp_pcb *pcb; 00821 00822 again: 00823 if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) { 00824 tcp_port = TCP_LOCAL_PORT_RANGE_START; 00825 } 00826 /* Check all PCB lists. */ 00827 for (i = 0; i < NUM_TCP_PCB_LISTS; i++) { 00828 for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) { 00829 if (pcb->local_port == tcp_port) { 00830 if (++n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) { 00831 return 0; 00832 } 00833 goto again; 00834 } 00835 } 00836 } 00837 return tcp_port; 00838 } 00839 00840 /** 00841 * @ingroup tcp_raw 00842 * Connects to another host. The function given as the "connected" 00843 * argument will be called when the connection has been established. 00844 * 00845 * @param pcb the tcp_pcb used to establish the connection 00846 * @param ipaddr the remote ip address to connect to 00847 * @param port the remote tcp port to connect to 00848 * @param connected callback function to call when connected (on error, 00849 the err calback will be called) 00850 * @return ERR_VAL if invalid arguments are given 00851 * ERR_OK if connect request has been sent 00852 * other err_t values if connect request couldn't be sent 00853 */ 00854 err_t 00855 tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port, 00856 tcp_connected_fn connected) 00857 { 00858 err_t ret; 00859 u32_t iss; 00860 u16_t old_local_port; 00861 00862 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { 00863 return ERR_VAL; 00864 } 00865 00866 LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN); 00867 00868 LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port)); 00869 ip_addr_set(&pcb->remote_ip, ipaddr); 00870 pcb->remote_port = port; 00871 00872 /* check if we have a route to the remote host */ 00873 if (ip_addr_isany(&pcb->local_ip)) { 00874 /* no local IP address set, yet. */ 00875 struct netif *netif; 00876 const ip_addr_t *local_ip; 00877 ip_route_get_local_ip(&pcb->local_ip, &pcb->remote_ip, netif, local_ip); 00878 if ((netif == NULL) || (local_ip == NULL)) { 00879 /* Don't even try to send a SYN packet if we have no route 00880 since that will fail. */ 00881 return ERR_RTE; 00882 } 00883 /* Use the address as local address of the pcb. */ 00884 ip_addr_copy(pcb->local_ip, *local_ip); 00885 } 00886 00887 old_local_port = pcb->local_port; 00888 if (pcb->local_port == 0) { 00889 pcb->local_port = tcp_new_port(); 00890 if (pcb->local_port == 0) { 00891 return ERR_BUF; 00892 } 00893 } else { 00894 #if SO_REUSE 00895 if (ip_get_option(pcb, SOF_REUSEADDR)) { 00896 /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure 00897 now that the 5-tuple is unique. */ 00898 struct tcp_pcb *cpcb; 00899 int i; 00900 /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */ 00901 for (i = 2; i < NUM_TCP_PCB_LISTS; i++) { 00902 for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { 00903 if ((cpcb->local_port == pcb->local_port) && 00904 (cpcb->remote_port == port) && 00905 ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) && 00906 ip_addr_cmp(&cpcb->remote_ip, ipaddr)) { 00907 /* linux returns EISCONN here, but ERR_USE should be OK for us */ 00908 return ERR_USE; 00909 } 00910 } 00911 } 00912 } 00913 #endif /* SO_REUSE */ 00914 } 00915 00916 iss = tcp_next_iss(pcb); 00917 pcb->rcv_nxt = 0; 00918 pcb->snd_nxt = iss; 00919 pcb->lastack = iss - 1; 00920 pcb->snd_wl2 = iss - 1; 00921 pcb->snd_lbb = iss - 1; 00922 /* Start with a window that does not need scaling. When window scaling is 00923 enabled and used, the window is enlarged when both sides agree on scaling. */ 00924 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND); 00925 pcb->rcv_ann_right_edge = pcb->rcv_nxt; 00926 pcb->snd_wnd = TCP_WND; 00927 /* As initial send MSS, we use TCP_MSS but limit it to 536. 00928 The send MSS is updated when an MSS option is received. */ 00929 pcb->mss = INITIAL_MSS; 00930 #if TCP_CALCULATE_EFF_SEND_MSS 00931 pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip); 00932 #endif /* TCP_CALCULATE_EFF_SEND_MSS */ 00933 pcb->cwnd = 1; 00934 pcb->ssthresh = TCP_WND; 00935 #if LWIP_CALLBACK_API 00936 pcb->connected = connected; 00937 #else /* LWIP_CALLBACK_API */ 00938 LWIP_UNUSED_ARG(connected); 00939 #endif /* LWIP_CALLBACK_API */ 00940 00941 /* Send a SYN together with the MSS option. */ 00942 ret = tcp_enqueue_flags(pcb, TCP_SYN); 00943 if (ret == ERR_OK) { 00944 /* SYN segment was enqueued, changed the pcbs state now */ 00945 pcb->state = SYN_SENT; 00946 if (old_local_port != 0) { 00947 TCP_RMV(&tcp_bound_pcbs, pcb); 00948 } 00949 TCP_REG_ACTIVE(pcb); 00950 MIB2_STATS_INC(mib2.tcpactiveopens); 00951 00952 tcp_output(pcb); 00953 } 00954 return ret; 00955 } 00956 00957 /** 00958 * Called every 500 ms and implements the retransmission timer and the timer that 00959 * removes PCBs that have been in TIME-WAIT for enough time. It also increments 00960 * various timers such as the inactivity timer in each PCB. 00961 * 00962 * Automatically called from tcp_tmr(). 00963 */ 00964 void 00965 tcp_slowtmr(void) 00966 { 00967 struct tcp_pcb *pcb, *prev; 00968 tcpwnd_size_t eff_wnd; 00969 u8_t pcb_remove; /* flag if a PCB should be removed */ 00970 u8_t pcb_reset; /* flag if a RST should be sent when removing */ 00971 err_t err; 00972 00973 err = ERR_OK; 00974 00975 ++tcp_ticks; 00976 ++tcp_timer_ctr; 00977 00978 tcp_slowtmr_start: 00979 /* Steps through all of the active PCBs. */ 00980 prev = NULL; 00981 pcb = tcp_active_pcbs; 00982 if (pcb == NULL) { 00983 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n")); 00984 } 00985 while (pcb != NULL) { 00986 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n")); 00987 LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED); 00988 LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN); 00989 LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT); 00990 if (pcb->last_timer == tcp_timer_ctr) { 00991 /* skip this pcb, we have already processed it */ 00992 pcb = pcb->next; 00993 continue; 00994 } 00995 pcb->last_timer = tcp_timer_ctr; 00996 00997 pcb_remove = 0; 00998 pcb_reset = 0; 00999 01000 if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) { 01001 ++pcb_remove; 01002 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n")); 01003 } 01004 else if (pcb->nrtx == TCP_MAXRTX) { 01005 ++pcb_remove; 01006 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n")); 01007 } else { 01008 if (pcb->persist_backoff > 0) { 01009 /* If snd_wnd is zero, use persist timer to send 1 byte probes 01010 * instead of using the standard retransmission mechanism. */ 01011 u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff-1]; 01012 if (pcb->persist_cnt < backoff_cnt) { 01013 pcb->persist_cnt++; 01014 } 01015 if (pcb->persist_cnt >= backoff_cnt) { 01016 if (tcp_zero_window_probe(pcb) == ERR_OK) { 01017 pcb->persist_cnt = 0; 01018 if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) { 01019 pcb->persist_backoff++; 01020 } 01021 } 01022 } 01023 } else { 01024 /* Increase the retransmission timer if it is running */ 01025 if (pcb->rtime >= 0) { 01026 ++pcb->rtime; 01027 } 01028 01029 if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) { 01030 /* Time for a retransmission. */ 01031 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F 01032 " pcb->rto %"S16_F"\n", 01033 pcb->rtime, pcb->rto)); 01034 01035 /* Double retransmission time-out unless we are trying to 01036 * connect to somebody (i.e., we are in SYN_SENT). */ 01037 if (pcb->state != SYN_SENT) { 01038 pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx]; 01039 } 01040 01041 /* Reset the retransmission timer. */ 01042 pcb->rtime = 0; 01043 01044 /* Reduce congestion window and ssthresh. */ 01045 eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd); 01046 pcb->ssthresh = eff_wnd >> 1; 01047 if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) { 01048 pcb->ssthresh = (pcb->mss << 1); 01049 } 01050 pcb->cwnd = pcb->mss; 01051 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"TCPWNDSIZE_F 01052 " ssthresh %"TCPWNDSIZE_F"\n", 01053 pcb->cwnd, pcb->ssthresh)); 01054 01055 /* The following needs to be called AFTER cwnd is set to one 01056 mss - STJ */ 01057 tcp_rexmit_rto(pcb); 01058 } 01059 } 01060 } 01061 /* Check if this PCB has stayed too long in FIN-WAIT-2 */ 01062 if (pcb->state == FIN_WAIT_2) { 01063 /* If this PCB is in FIN_WAIT_2 because of SHUT_WR don't let it time out. */ 01064 if (pcb->flags & TF_RXCLOSED) { 01065 /* PCB was fully closed (either through close() or SHUT_RDWR): 01066 normal FIN-WAIT timeout handling. */ 01067 if ((u32_t)(tcp_ticks - pcb->tmr) > 01068 TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) { 01069 ++pcb_remove; 01070 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n")); 01071 } 01072 } 01073 } 01074 01075 /* Check if KEEPALIVE should be sent */ 01076 if (ip_get_option(pcb, SOF_KEEPALIVE) && 01077 ((pcb->state == ESTABLISHED) || 01078 (pcb->state == CLOSE_WAIT))) { 01079 if ((u32_t)(tcp_ticks - pcb->tmr) > 01080 (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL) 01081 { 01082 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to ")); 01083 ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip); 01084 LWIP_DEBUGF(TCP_DEBUG, ("\n")); 01085 01086 ++pcb_remove; 01087 ++pcb_reset; 01088 } else if ((u32_t)(tcp_ticks - pcb->tmr) > 01089 (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb)) 01090 / TCP_SLOW_INTERVAL) 01091 { 01092 err = tcp_keepalive(pcb); 01093 if (err == ERR_OK) { 01094 pcb->keep_cnt_sent++; 01095 } 01096 } 01097 } 01098 01099 /* If this PCB has queued out of sequence data, but has been 01100 inactive for too long, will drop the data (it will eventually 01101 be retransmitted). */ 01102 #if TCP_QUEUE_OOSEQ 01103 if (pcb->ooseq != NULL && 01104 (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) { 01105 tcp_segs_free(pcb->ooseq); 01106 pcb->ooseq = NULL; 01107 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n")); 01108 } 01109 #endif /* TCP_QUEUE_OOSEQ */ 01110 01111 /* Check if this PCB has stayed too long in SYN-RCVD */ 01112 if (pcb->state == SYN_RCVD) { 01113 if ((u32_t)(tcp_ticks - pcb->tmr) > 01114 TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) { 01115 ++pcb_remove; 01116 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n")); 01117 } 01118 } 01119 01120 /* Check if this PCB has stayed too long in LAST-ACK */ 01121 if (pcb->state == LAST_ACK) { 01122 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { 01123 ++pcb_remove; 01124 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n")); 01125 } 01126 } 01127 01128 /* If the PCB should be removed, do it. */ 01129 if (pcb_remove) { 01130 struct tcp_pcb *pcb2; 01131 #if LWIP_CALLBACK_API 01132 tcp_err_fn err_fn = pcb->errf; 01133 #endif /* LWIP_CALLBACK_API */ 01134 void *err_arg; 01135 tcp_pcb_purge(pcb); 01136 /* Remove PCB from tcp_active_pcbs list. */ 01137 if (prev != NULL) { 01138 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs); 01139 prev->next = pcb->next; 01140 } else { 01141 /* This PCB was the first. */ 01142 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb); 01143 tcp_active_pcbs = pcb->next; 01144 } 01145 01146 if (pcb_reset) { 01147 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip, 01148 pcb->local_port, pcb->remote_port); 01149 } 01150 01151 err_arg = pcb->callback_arg; 01152 pcb2 = pcb; 01153 pcb = pcb->next; 01154 memp_free(MEMP_TCP_PCB, pcb2); 01155 01156 tcp_active_pcbs_changed = 0; 01157 TCP_EVENT_ERR(err_fn, err_arg, ERR_ABRT); 01158 if (tcp_active_pcbs_changed) { 01159 goto tcp_slowtmr_start; 01160 } 01161 } else { 01162 /* get the 'next' element now and work with 'prev' below (in case of abort) */ 01163 prev = pcb; 01164 pcb = pcb->next; 01165 01166 /* We check if we should poll the connection. */ 01167 ++prev->polltmr; 01168 if (prev->polltmr >= prev->pollinterval) { 01169 prev->polltmr = 0; 01170 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n")); 01171 tcp_active_pcbs_changed = 0; 01172 TCP_EVENT_POLL(prev, err); 01173 if (tcp_active_pcbs_changed) { 01174 goto tcp_slowtmr_start; 01175 } 01176 /* if err == ERR_ABRT, 'prev' is already deallocated */ 01177 if (err == ERR_OK) { 01178 tcp_output(prev); 01179 } 01180 } 01181 } 01182 } 01183 01184 01185 /* Steps through all of the TIME-WAIT PCBs. */ 01186 prev = NULL; 01187 pcb = tcp_tw_pcbs; 01188 while (pcb != NULL) { 01189 LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); 01190 pcb_remove = 0; 01191 01192 /* Check if this PCB has stayed long enough in TIME-WAIT */ 01193 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { 01194 ++pcb_remove; 01195 } 01196 01197 /* If the PCB should be removed, do it. */ 01198 if (pcb_remove) { 01199 struct tcp_pcb *pcb2; 01200 tcp_pcb_purge(pcb); 01201 /* Remove PCB from tcp_tw_pcbs list. */ 01202 if (prev != NULL) { 01203 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs); 01204 prev->next = pcb->next; 01205 } else { 01206 /* This PCB was the first. */ 01207 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb); 01208 tcp_tw_pcbs = pcb->next; 01209 } 01210 pcb2 = pcb; 01211 pcb = pcb->next; 01212 memp_free(MEMP_TCP_PCB, pcb2); 01213 } else { 01214 prev = pcb; 01215 pcb = pcb->next; 01216 } 01217 } 01218 } 01219 01220 /** 01221 * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously 01222 * "refused" by upper layer (application) and sends delayed ACKs. 01223 * 01224 * Automatically called from tcp_tmr(). 01225 */ 01226 void 01227 tcp_fasttmr(void) 01228 { 01229 struct tcp_pcb *pcb; 01230 01231 ++tcp_timer_ctr; 01232 01233 tcp_fasttmr_start: 01234 pcb = tcp_active_pcbs; 01235 01236 while (pcb != NULL) { 01237 if (pcb->last_timer != tcp_timer_ctr) { 01238 struct tcp_pcb *next; 01239 pcb->last_timer = tcp_timer_ctr; 01240 /* send delayed ACKs */ 01241 if (pcb->flags & TF_ACK_DELAY) { 01242 LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n")); 01243 tcp_ack_now(pcb); 01244 tcp_output(pcb); 01245 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW); 01246 } 01247 01248 next = pcb->next; 01249 01250 /* If there is data which was previously "refused" by upper layer */ 01251 if (pcb->refused_data != NULL) { 01252 tcp_active_pcbs_changed = 0; 01253 tcp_process_refused_data(pcb); 01254 if (tcp_active_pcbs_changed) { 01255 /* application callback has changed the pcb list: restart the loop */ 01256 goto tcp_fasttmr_start; 01257 } 01258 } 01259 pcb = next; 01260 } else { 01261 pcb = pcb->next; 01262 } 01263 } 01264 } 01265 01266 /** Call tcp_output for all active pcbs that have TF_NAGLEMEMERR set */ 01267 void 01268 tcp_txnow(void) 01269 { 01270 struct tcp_pcb *pcb; 01271 01272 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 01273 if (pcb->flags & TF_NAGLEMEMERR) { 01274 tcp_output(pcb); 01275 } 01276 } 01277 } 01278 01279 /** Pass pcb->refused_data to the recv callback */ 01280 err_t 01281 tcp_process_refused_data(struct tcp_pcb *pcb) 01282 { 01283 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 01284 struct pbuf *rest; 01285 while (pcb->refused_data != NULL) 01286 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ 01287 { 01288 err_t err; 01289 u8_t refused_flags = pcb->refused_data->flags; 01290 /* set pcb->refused_data to NULL in case the callback frees it and then 01291 closes the pcb */ 01292 struct pbuf *refused_data = pcb->refused_data; 01293 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 01294 pbuf_split_64k(refused_data, &rest); 01295 pcb->refused_data = rest; 01296 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ 01297 pcb->refused_data = NULL; 01298 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ 01299 /* Notify again application with data previously received. */ 01300 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n")); 01301 TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err); 01302 if (err == ERR_OK) { 01303 /* did refused_data include a FIN? */ 01304 if (refused_flags & PBUF_FLAG_TCP_FIN 01305 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 01306 && (rest == NULL) 01307 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ 01308 ) { 01309 /* correct rcv_wnd as the application won't call tcp_recved() 01310 for the FIN's seqno */ 01311 if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) { 01312 pcb->rcv_wnd++; 01313 } 01314 TCP_EVENT_CLOSED(pcb, err); 01315 if (err == ERR_ABRT) { 01316 return ERR_ABRT; 01317 } 01318 } 01319 } else if (err == ERR_ABRT) { 01320 /* if err == ERR_ABRT, 'pcb' is already deallocated */ 01321 /* Drop incoming packets because pcb is "full" (only if the incoming 01322 segment contains data). */ 01323 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n")); 01324 return ERR_ABRT; 01325 } else { 01326 /* data is still refused, pbuf is still valid (go on for ACK-only packets) */ 01327 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 01328 if (rest != NULL) { 01329 pbuf_cat(refused_data, rest); 01330 } 01331 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */ 01332 pcb->refused_data = refused_data; 01333 return ERR_INPROGRESS; 01334 } 01335 } 01336 return ERR_OK; 01337 } 01338 01339 /** 01340 * Deallocates a list of TCP segments (tcp_seg structures). 01341 * 01342 * @param seg tcp_seg list of TCP segments to free 01343 */ 01344 void 01345 tcp_segs_free(struct tcp_seg *seg) 01346 { 01347 while (seg != NULL) { 01348 struct tcp_seg *next = seg->next; 01349 tcp_seg_free(seg); 01350 seg = next; 01351 } 01352 } 01353 01354 /** 01355 * Frees a TCP segment (tcp_seg structure). 01356 * 01357 * @param seg single tcp_seg to free 01358 */ 01359 void 01360 tcp_seg_free(struct tcp_seg *seg) 01361 { 01362 if (seg != NULL) { 01363 if (seg->p != NULL) { 01364 pbuf_free(seg->p); 01365 #if TCP_DEBUG 01366 seg->p = NULL; 01367 #endif /* TCP_DEBUG */ 01368 } 01369 memp_free(MEMP_TCP_SEG, seg); 01370 } 01371 } 01372 01373 /** 01374 * Sets the priority of a connection. 01375 * 01376 * @param pcb the tcp_pcb to manipulate 01377 * @param prio new priority 01378 */ 01379 void 01380 tcp_setprio(struct tcp_pcb *pcb, u8_t prio) 01381 { 01382 pcb->prio = prio; 01383 } 01384 01385 #if TCP_QUEUE_OOSEQ 01386 /** 01387 * Returns a copy of the given TCP segment. 01388 * The pbuf and data are not copied, only the pointers 01389 * 01390 * @param seg the old tcp_seg 01391 * @return a copy of seg 01392 */ 01393 struct tcp_seg * 01394 tcp_seg_copy(struct tcp_seg *seg) 01395 { 01396 struct tcp_seg *cseg; 01397 01398 cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG); 01399 if (cseg == NULL) { 01400 return NULL; 01401 } 01402 SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); 01403 pbuf_ref(cseg->p); 01404 return cseg; 01405 } 01406 #endif /* TCP_QUEUE_OOSEQ */ 01407 01408 #if LWIP_CALLBACK_API 01409 /** 01410 * Default receive callback that is called if the user didn't register 01411 * a recv callback for the pcb. 01412 */ 01413 err_t 01414 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) 01415 { 01416 LWIP_UNUSED_ARG(arg); 01417 if (p != NULL) { 01418 tcp_recved(pcb, p->tot_len); 01419 pbuf_free(p); 01420 } else if (err == ERR_OK) { 01421 return tcp_close(pcb); 01422 } 01423 return ERR_OK; 01424 } 01425 #endif /* LWIP_CALLBACK_API */ 01426 01427 /** 01428 * Kills the oldest active connection that has the same or lower priority than 01429 * 'prio'. 01430 * 01431 * @param prio minimum priority 01432 */ 01433 static void 01434 tcp_kill_prio(u8_t prio) 01435 { 01436 struct tcp_pcb *pcb, *inactive; 01437 u32_t inactivity; 01438 u8_t mprio; 01439 01440 mprio = LWIP_MIN(TCP_PRIO_MAX, prio); 01441 01442 /* We kill the oldest active connection that has lower priority than prio. */ 01443 inactivity = 0; 01444 inactive = NULL; 01445 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 01446 if (pcb->prio <= mprio && 01447 (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { 01448 inactivity = tcp_ticks - pcb->tmr; 01449 inactive = pcb; 01450 mprio = pcb->prio; 01451 } 01452 } 01453 if (inactive != NULL) { 01454 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n", 01455 (void *)inactive, inactivity)); 01456 tcp_abort(inactive); 01457 } 01458 } 01459 01460 /** 01461 * Kills the oldest connection that is in specific state. 01462 * Called from tcp_alloc() for LAST_ACK and CLOSING if no more connections are available. 01463 */ 01464 static void 01465 tcp_kill_state(enum tcp_state state) 01466 { 01467 struct tcp_pcb *pcb, *inactive; 01468 u32_t inactivity; 01469 01470 LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK)); 01471 01472 inactivity = 0; 01473 inactive = NULL; 01474 /* Go through the list of active pcbs and get the oldest pcb that is in state 01475 CLOSING/LAST_ACK. */ 01476 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 01477 if (pcb->state == state) { 01478 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { 01479 inactivity = tcp_ticks - pcb->tmr; 01480 inactive = pcb; 01481 } 01482 } 01483 } 01484 if (inactive != NULL) { 01485 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n", 01486 tcp_state_str[state], (void *)inactive, inactivity)); 01487 /* Don't send a RST, since no data is lost. */ 01488 tcp_abandon(inactive, 0); 01489 } 01490 } 01491 01492 /** 01493 * Kills the oldest connection that is in TIME_WAIT state. 01494 * Called from tcp_alloc() if no more connections are available. 01495 */ 01496 static void 01497 tcp_kill_timewait(void) 01498 { 01499 struct tcp_pcb *pcb, *inactive; 01500 u32_t inactivity; 01501 01502 inactivity = 0; 01503 inactive = NULL; 01504 /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */ 01505 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { 01506 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { 01507 inactivity = tcp_ticks - pcb->tmr; 01508 inactive = pcb; 01509 } 01510 } 01511 if (inactive != NULL) { 01512 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n", 01513 (void *)inactive, inactivity)); 01514 tcp_abort(inactive); 01515 } 01516 } 01517 01518 /** 01519 * Allocate a new tcp_pcb structure. 01520 * 01521 * @param prio priority for the new pcb 01522 * @return a new tcp_pcb that initially is in state CLOSED 01523 */ 01524 struct tcp_pcb * 01525 tcp_alloc(u8_t prio) 01526 { 01527 struct tcp_pcb *pcb; 01528 01529 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01530 if (pcb == NULL) { 01531 /* Try killing oldest connection in TIME-WAIT. */ 01532 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n")); 01533 tcp_kill_timewait(); 01534 /* Try to allocate a tcp_pcb again. */ 01535 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01536 if (pcb == NULL) { 01537 /* Try killing oldest connection in LAST-ACK (these wouldn't go to TIME-WAIT). */ 01538 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n")); 01539 tcp_kill_state(LAST_ACK); 01540 /* Try to allocate a tcp_pcb again. */ 01541 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01542 if (pcb == NULL) { 01543 /* Try killing oldest connection in CLOSING. */ 01544 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n")); 01545 tcp_kill_state(CLOSING); 01546 /* Try to allocate a tcp_pcb again. */ 01547 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01548 if (pcb == NULL) { 01549 /* Try killing active connections with lower priority than the new one. */ 01550 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio)); 01551 tcp_kill_prio(prio); 01552 /* Try to allocate a tcp_pcb again. */ 01553 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01554 if (pcb != NULL) { 01555 /* adjust err stats: memp_malloc failed multiple times before */ 01556 MEMP_STATS_DEC(err, MEMP_TCP_PCB); 01557 } 01558 } 01559 if (pcb != NULL) { 01560 /* adjust err stats: memp_malloc failed multiple times before */ 01561 MEMP_STATS_DEC(err, MEMP_TCP_PCB); 01562 } 01563 } 01564 if (pcb != NULL) { 01565 /* adjust err stats: memp_malloc failed multiple times before */ 01566 MEMP_STATS_DEC(err, MEMP_TCP_PCB); 01567 } 01568 } 01569 if (pcb != NULL) { 01570 /* adjust err stats: memp_malloc failed above */ 01571 MEMP_STATS_DEC(err, MEMP_TCP_PCB); 01572 } 01573 } 01574 if (pcb != NULL) { 01575 /* zero out the whole pcb, so there is no need to initialize members to zero */ 01576 memset(pcb, 0, sizeof(struct tcp_pcb)); 01577 pcb->prio = prio; 01578 pcb->snd_buf = TCP_SND_BUF; 01579 /* Start with a window that does not need scaling. When window scaling is 01580 enabled and used, the window is enlarged when both sides agree on scaling. */ 01581 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND); 01582 pcb->ttl = TCP_TTL; 01583 /* As initial send MSS, we use TCP_MSS but limit it to 536. 01584 The send MSS is updated when an MSS option is received. */ 01585 pcb->mss = INITIAL_MSS; 01586 pcb->rto = 3000 / TCP_SLOW_INTERVAL; 01587 pcb->sv = 3000 / TCP_SLOW_INTERVAL; 01588 pcb->rtime = -1; 01589 pcb->cwnd = 1; 01590 pcb->tmr = tcp_ticks; 01591 pcb->last_timer = tcp_timer_ctr; 01592 01593 #if LWIP_CALLBACK_API 01594 pcb->recv = tcp_recv_null; 01595 #endif /* LWIP_CALLBACK_API */ 01596 01597 /* Init KEEPALIVE timer */ 01598 pcb->keep_idle = TCP_KEEPIDLE_DEFAULT; 01599 01600 #if LWIP_TCP_KEEPALIVE 01601 pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT; 01602 pcb->keep_cnt = TCP_KEEPCNT_DEFAULT; 01603 #endif /* LWIP_TCP_KEEPALIVE */ 01604 } 01605 return pcb; 01606 } 01607 01608 /** 01609 * @ingroup tcp_raw 01610 * Creates a new TCP protocol control block but doesn't place it on 01611 * any of the TCP PCB lists. 01612 * The pcb is not put on any list until binding using tcp_bind(). 01613 * 01614 * @internal: Maybe there should be a idle TCP PCB list where these 01615 * PCBs are put on. Port reservation using tcp_bind() is implemented but 01616 * allocated pcbs that are not bound can't be killed automatically if wanting 01617 * to allocate a pcb with higher prio (@see tcp_kill_prio()) 01618 * 01619 * @return a new tcp_pcb that initially is in state CLOSED 01620 */ 01621 struct tcp_pcb * 01622 tcp_new(void) 01623 { 01624 return tcp_alloc(TCP_PRIO_NORMAL); 01625 } 01626 01627 /** 01628 * @ingroup tcp_raw 01629 * Creates a new TCP protocol control block but doesn't 01630 * place it on any of the TCP PCB lists. 01631 * The pcb is not put on any list until binding using tcp_bind(). 01632 * 01633 * @param type IP address type, see @ref lwip_ip_addr_type definitions. 01634 * If you want to listen to IPv4 and IPv6 (dual-stack) connections, 01635 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE. 01636 * @return a new tcp_pcb that initially is in state CLOSED 01637 */ 01638 struct tcp_pcb * 01639 tcp_new_ip_type(u8_t type) 01640 { 01641 struct tcp_pcb * pcb; 01642 pcb = tcp_alloc(TCP_PRIO_NORMAL); 01643 #if LWIP_IPV4 && LWIP_IPV6 01644 if (pcb != NULL) { 01645 IP_SET_TYPE_VAL(pcb->local_ip, type); 01646 IP_SET_TYPE_VAL(pcb->remote_ip, type); 01647 } 01648 #else 01649 LWIP_UNUSED_ARG(type); 01650 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01651 return pcb; 01652 } 01653 01654 /** 01655 * @ingroup tcp_raw 01656 * Used to specify the argument that should be passed callback 01657 * functions. 01658 * 01659 * @param pcb tcp_pcb to set the callback argument 01660 * @param arg void pointer argument to pass to callback functions 01661 */ 01662 void 01663 tcp_arg(struct tcp_pcb *pcb, void *arg) 01664 { 01665 /* This function is allowed to be called for both listen pcbs and 01666 connection pcbs. */ 01667 if (pcb != NULL) { 01668 pcb->callback_arg = arg; 01669 } 01670 } 01671 #if LWIP_CALLBACK_API 01672 01673 /** 01674 * @ingroup tcp_raw 01675 * Used to specify the function that should be called when a TCP 01676 * connection receives data. 01677 * 01678 * @param pcb tcp_pcb to set the recv callback 01679 * @param recv callback function to call for this pcb when data is received 01680 */ 01681 void 01682 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv) 01683 { 01684 if (pcb != NULL) { 01685 LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN); 01686 pcb->recv = recv; 01687 } 01688 } 01689 01690 /** 01691 * @ingroup tcp_raw 01692 * Used to specify the function that should be called when TCP data 01693 * has been successfully delivered to the remote host. 01694 * 01695 * @param pcb tcp_pcb to set the sent callback 01696 * @param sent callback function to call for this pcb when data is successfully sent 01697 */ 01698 void 01699 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent) 01700 { 01701 if (pcb != NULL) { 01702 LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN); 01703 pcb->sent = sent; 01704 } 01705 } 01706 01707 /** 01708 * @ingroup tcp_raw 01709 * Used to specify the function that should be called when a fatal error 01710 * has occurred on the connection. 01711 * 01712 * @note The corresponding pcb is already freed when this callback is called! 01713 * 01714 * @param pcb tcp_pcb to set the err callback 01715 * @param err callback function to call for this pcb when a fatal error 01716 * has occurred on the connection 01717 */ 01718 void 01719 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err) 01720 { 01721 if (pcb != NULL) { 01722 LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN); 01723 pcb->errf = err; 01724 } 01725 } 01726 01727 /** 01728 * @ingroup tcp_raw 01729 * Used for specifying the function that should be called when a 01730 * LISTENing connection has been connected to another host. 01731 * 01732 * @param pcb tcp_pcb to set the accept callback 01733 * @param accept callback function to call for this pcb when LISTENing 01734 * connection has been connected to another host 01735 */ 01736 void 01737 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept) 01738 { 01739 if ((pcb != NULL) && (pcb->state == LISTEN)) { 01740 struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)pcb; 01741 lpcb->accept = accept; 01742 } 01743 } 01744 #endif /* LWIP_CALLBACK_API */ 01745 01746 01747 /** 01748 * @ingroup tcp_raw 01749 * Used to specify the function that should be called periodically 01750 * from TCP. The interval is specified in terms of the TCP coarse 01751 * timer interval, which is called twice a second. 01752 * 01753 */ 01754 void 01755 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval) 01756 { 01757 LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN); 01758 #if LWIP_CALLBACK_API 01759 pcb->poll = poll; 01760 #else /* LWIP_CALLBACK_API */ 01761 LWIP_UNUSED_ARG(poll); 01762 #endif /* LWIP_CALLBACK_API */ 01763 pcb->pollinterval = interval; 01764 } 01765 01766 /** 01767 * Purges a TCP PCB. Removes any buffered data and frees the buffer memory 01768 * (pcb->ooseq, pcb->unsent and pcb->unacked are freed). 01769 * 01770 * @param pcb tcp_pcb to purge. The pcb itself is not deallocated! 01771 */ 01772 void 01773 tcp_pcb_purge(struct tcp_pcb *pcb) 01774 { 01775 if (pcb->state != CLOSED && 01776 pcb->state != TIME_WAIT && 01777 pcb->state != LISTEN) { 01778 01779 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n")); 01780 01781 tcp_backlog_accepted(pcb); 01782 01783 if (pcb->refused_data != NULL) { 01784 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n")); 01785 pbuf_free(pcb->refused_data); 01786 pcb->refused_data = NULL; 01787 } 01788 if (pcb->unsent != NULL) { 01789 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n")); 01790 } 01791 if (pcb->unacked != NULL) { 01792 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n")); 01793 } 01794 #if TCP_QUEUE_OOSEQ 01795 if (pcb->ooseq != NULL) { 01796 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n")); 01797 } 01798 tcp_segs_free(pcb->ooseq); 01799 pcb->ooseq = NULL; 01800 #endif /* TCP_QUEUE_OOSEQ */ 01801 01802 /* Stop the retransmission timer as it will expect data on unacked 01803 queue if it fires */ 01804 pcb->rtime = -1; 01805 01806 tcp_segs_free(pcb->unsent); 01807 tcp_segs_free(pcb->unacked); 01808 pcb->unacked = pcb->unsent = NULL; 01809 #if TCP_OVERSIZE 01810 pcb->unsent_oversize = 0; 01811 #endif /* TCP_OVERSIZE */ 01812 } 01813 } 01814 01815 /** 01816 * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first. 01817 * 01818 * @param pcblist PCB list to purge. 01819 * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated! 01820 */ 01821 void 01822 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb) 01823 { 01824 TCP_RMV(pcblist, pcb); 01825 01826 tcp_pcb_purge(pcb); 01827 01828 /* if there is an outstanding delayed ACKs, send it */ 01829 if (pcb->state != TIME_WAIT && 01830 pcb->state != LISTEN && 01831 pcb->flags & TF_ACK_DELAY) { 01832 pcb->flags |= TF_ACK_NOW; 01833 tcp_output(pcb); 01834 } 01835 01836 if (pcb->state != LISTEN) { 01837 LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL); 01838 LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL); 01839 #if TCP_QUEUE_OOSEQ 01840 LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL); 01841 #endif /* TCP_QUEUE_OOSEQ */ 01842 } 01843 01844 pcb->state = CLOSED; 01845 /* reset the local port to prevent the pcb from being 'bound' */ 01846 pcb->local_port = 0; 01847 01848 LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane()); 01849 } 01850 01851 /** 01852 * Calculates a new initial sequence number for new connections. 01853 * 01854 * @return u32_t pseudo random sequence number 01855 */ 01856 u32_t 01857 tcp_next_iss(struct tcp_pcb *pcb) 01858 { 01859 #ifdef LWIP_HOOK_TCP_ISN 01860 return LWIP_HOOK_TCP_ISN(&pcb->local_ip, pcb->local_port, &pcb->remote_ip, pcb->remote_port); 01861 #else /* LWIP_HOOK_TCP_ISN */ 01862 static u32_t iss = 6510; 01863 01864 LWIP_UNUSED_ARG(pcb); 01865 01866 iss += tcp_ticks; /* XXX */ 01867 return iss; 01868 #endif /* LWIP_HOOK_TCP_ISN */ 01869 } 01870 01871 #if TCP_CALCULATE_EFF_SEND_MSS 01872 /** 01873 * Calculates the effective send mss that can be used for a specific IP address 01874 * by using ip_route to determine the netif used to send to the address and 01875 * calculating the minimum of TCP_MSS and that netif's mtu (if set). 01876 */ 01877 u16_t 01878 tcp_eff_send_mss_impl(u16_t sendmss, const ip_addr_t *dest 01879 #if LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING 01880 , const ip_addr_t *src 01881 #endif /* LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING */ 01882 ) 01883 { 01884 u16_t mss_s; 01885 struct netif *outif; 01886 s16_t mtu; 01887 01888 outif = ip_route(src, dest); 01889 #if LWIP_IPV6 01890 #if LWIP_IPV4 01891 if (IP_IS_V6(dest)) 01892 #endif /* LWIP_IPV4 */ 01893 { 01894 /* First look in destination cache, to see if there is a Path MTU. */ 01895 mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif); 01896 } 01897 #if LWIP_IPV4 01898 else 01899 #endif /* LWIP_IPV4 */ 01900 #endif /* LWIP_IPV6 */ 01901 #if LWIP_IPV4 01902 { 01903 if (outif == NULL) { 01904 return sendmss; 01905 } 01906 mtu = outif->mtu; 01907 } 01908 #endif /* LWIP_IPV4 */ 01909 01910 if (mtu != 0) { 01911 #if LWIP_IPV6 01912 #if LWIP_IPV4 01913 if (IP_IS_V6(dest)) 01914 #endif /* LWIP_IPV4 */ 01915 { 01916 mss_s = mtu - IP6_HLEN - TCP_HLEN; 01917 } 01918 #if LWIP_IPV4 01919 else 01920 #endif /* LWIP_IPV4 */ 01921 #endif /* LWIP_IPV6 */ 01922 #if LWIP_IPV4 01923 { 01924 mss_s = mtu - IP_HLEN - TCP_HLEN; 01925 } 01926 #endif /* LWIP_IPV4 */ 01927 /* RFC 1122, chap 4.2.2.6: 01928 * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize 01929 * We correct for TCP options in tcp_write(), and don't support IP options. 01930 */ 01931 sendmss = LWIP_MIN(sendmss, mss_s); 01932 } 01933 return sendmss; 01934 } 01935 #endif /* TCP_CALCULATE_EFF_SEND_MSS */ 01936 01937 /** Helper function for tcp_netif_ip_addr_changed() that iterates a pcb list */ 01938 static void 01939 tcp_netif_ip_addr_changed_pcblist(const ip_addr_t* old_addr, struct tcp_pcb* pcb_list) 01940 { 01941 struct tcp_pcb *pcb; 01942 pcb = pcb_list; 01943 while (pcb != NULL) { 01944 /* PCB bound to current local interface address? */ 01945 if (ip_addr_cmp(&pcb->local_ip, old_addr) 01946 #if LWIP_AUTOIP 01947 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */ 01948 && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip))) 01949 #endif /* LWIP_AUTOIP */ 01950 ) { 01951 /* this connection must be aborted */ 01952 struct tcp_pcb *next = pcb->next; 01953 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb)); 01954 tcp_abort(pcb); 01955 pcb = next; 01956 } else { 01957 pcb = pcb->next; 01958 } 01959 } 01960 } 01961 01962 /** This function is called from netif.c when address is changed or netif is removed 01963 * 01964 * @param old_addr IP address of the netif before change 01965 * @param new_addr IP address of the netif after change or NULL if netif has been removed 01966 */ 01967 void 01968 tcp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr) 01969 { 01970 struct tcp_pcb_listen *lpcb, *next; 01971 01972 if (!ip_addr_isany(old_addr)) { 01973 tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs); 01974 tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs); 01975 01976 if (!ip_addr_isany(new_addr)) { 01977 /* PCB bound to current local interface address? */ 01978 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = next) { 01979 next = lpcb->next; 01980 /* PCB bound to current local interface address? */ 01981 if (ip_addr_cmp(&lpcb->local_ip, old_addr)) { 01982 /* The PCB is listening to the old ipaddr and 01983 * is set to listen to the new one instead */ 01984 ip_addr_copy(lpcb->local_ip, *new_addr); 01985 } 01986 } 01987 } 01988 } 01989 } 01990 01991 const char* 01992 tcp_debug_state_str(enum tcp_state s) 01993 { 01994 return tcp_state_str[s]; 01995 } 01996 01997 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG 01998 /** 01999 * Print a tcp header for debugging purposes. 02000 * 02001 * @param tcphdr pointer to a struct tcp_hdr 02002 */ 02003 void 02004 tcp_debug_print(struct tcp_hdr *tcphdr) 02005 { 02006 LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n")); 02007 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 02008 LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", 02009 lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest))); 02010 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 02011 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n", 02012 lwip_ntohl(tcphdr->seqno))); 02013 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 02014 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n", 02015 lwip_ntohl(tcphdr->ackno))); 02016 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 02017 LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (", 02018 TCPH_HDRLEN(tcphdr), 02019 (u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1), 02020 (u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1), 02021 (u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1), 02022 (u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1), 02023 (u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1), 02024 (u16_t)(TCPH_FLAGS(tcphdr) & 1), 02025 lwip_ntohs(tcphdr->wnd))); 02026 tcp_debug_print_flags(TCPH_FLAGS(tcphdr)); 02027 LWIP_DEBUGF(TCP_DEBUG, ("), win)\n")); 02028 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 02029 LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n", 02030 lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp))); 02031 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 02032 } 02033 02034 /** 02035 * Print a tcp state for debugging purposes. 02036 * 02037 * @param s enum tcp_state to print 02038 */ 02039 void 02040 tcp_debug_print_state(enum tcp_state s) 02041 { 02042 LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s])); 02043 } 02044 02045 /** 02046 * Print tcp flags for debugging purposes. 02047 * 02048 * @param flags tcp flags, all active flags are printed 02049 */ 02050 void 02051 tcp_debug_print_flags(u8_t flags) 02052 { 02053 if (flags & TCP_FIN) { 02054 LWIP_DEBUGF(TCP_DEBUG, ("FIN ")); 02055 } 02056 if (flags & TCP_SYN) { 02057 LWIP_DEBUGF(TCP_DEBUG, ("SYN ")); 02058 } 02059 if (flags & TCP_RST) { 02060 LWIP_DEBUGF(TCP_DEBUG, ("RST ")); 02061 } 02062 if (flags & TCP_PSH) { 02063 LWIP_DEBUGF(TCP_DEBUG, ("PSH ")); 02064 } 02065 if (flags & TCP_ACK) { 02066 LWIP_DEBUGF(TCP_DEBUG, ("ACK ")); 02067 } 02068 if (flags & TCP_URG) { 02069 LWIP_DEBUGF(TCP_DEBUG, ("URG ")); 02070 } 02071 if (flags & TCP_ECE) { 02072 LWIP_DEBUGF(TCP_DEBUG, ("ECE ")); 02073 } 02074 if (flags & TCP_CWR) { 02075 LWIP_DEBUGF(TCP_DEBUG, ("CWR ")); 02076 } 02077 LWIP_DEBUGF(TCP_DEBUG, ("\n")); 02078 } 02079 02080 /** 02081 * Print all tcp_pcbs in every list for debugging purposes. 02082 */ 02083 void 02084 tcp_debug_print_pcbs(void) 02085 { 02086 struct tcp_pcb *pcb; 02087 struct tcp_pcb_listen *pcbl; 02088 02089 LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n")); 02090 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 02091 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", 02092 pcb->local_port, pcb->remote_port, 02093 pcb->snd_nxt, pcb->rcv_nxt)); 02094 tcp_debug_print_state(pcb->state); 02095 } 02096 02097 LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n")); 02098 for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) { 02099 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F" ", pcbl->local_port)); 02100 tcp_debug_print_state(pcbl->state); 02101 } 02102 02103 LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n")); 02104 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { 02105 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", 02106 pcb->local_port, pcb->remote_port, 02107 pcb->snd_nxt, pcb->rcv_nxt)); 02108 tcp_debug_print_state(pcb->state); 02109 } 02110 } 02111 02112 /** 02113 * Check state consistency of the tcp_pcb lists. 02114 */ 02115 s16_t 02116 tcp_pcbs_sane(void) 02117 { 02118 struct tcp_pcb *pcb; 02119 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 02120 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED); 02121 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN); 02122 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT); 02123 } 02124 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { 02125 LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); 02126 } 02127 return 1; 02128 } 02129 #endif /* TCP_DEBUG */ 02130 02131 #endif /* LWIP_TCP */
Generated on Tue Jul 12 2022 11:02:28 by
1.7.2