77 #if LWIP_ND6_TCP_REACHABILITY_HINTS 82 #define LWIP_TCP_CALC_INITIAL_CWND(mss) LWIP_MIN((4U * (mss)), LWIP_MAX((2U * (mss)), 4380U)); 87 static struct tcp_seg inseg;
89 static u16_t tcphdr_optlen;
90 static u16_t tcphdr_opt1len;
91 static u8_t* tcphdr_opt2;
92 static u16_t tcp_optidx;
93 static u32_t seqno, ackno;
94 static tcpwnd_size_t recv_acked;
98 static u8_t recv_flags;
99 static struct pbuf *recv_data;
101 struct tcp_pcb *tcp_input_pcb;
104 static err_t tcp_process(
struct tcp_pcb *pcb);
105 static void tcp_receive(
struct tcp_pcb *pcb);
106 static void tcp_parseopt(
struct tcp_pcb *pcb);
108 static void tcp_listen_input(
struct tcp_pcb_listen *pcb);
109 static void tcp_timewait_input(
struct tcp_pcb *pcb);
121 tcp_input(
struct pbuf *p,
struct netif *inp)
123 struct tcp_pcb *pcb, *prev;
124 struct tcp_pcb_listen *lpcb;
126 struct tcp_pcb *lpcb_prev = NULL;
127 struct tcp_pcb_listen *lpcb_any = NULL;
136 TCP_STATS_INC(tcp.recv);
137 MIB2_STATS_INC(mib2.tcpinsegs);
142 tcp_debug_print(tcphdr);
146 if (p->
len < TCP_HLEN) {
149 TCP_STATS_INC(tcp.lenerr);
156 TCP_STATS_INC(tcp.proterr);
160 #if CHECKSUM_CHECK_TCP 161 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_TCP) {
163 u16_t chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->
tot_len,
168 tcp_debug_print(tcphdr);
169 TCP_STATS_INC(tcp.chkerr);
176 hdrlen_bytes = TCPH_HDRLEN(tcphdr) * 4;
177 if ((hdrlen_bytes < TCP_HLEN) || (hdrlen_bytes > p->
tot_len)) {
179 TCP_STATS_INC(tcp.lenerr);
185 tcphdr_optlen = hdrlen_bytes - TCP_HLEN;
187 if (p->
len >= hdrlen_bytes) {
189 tcphdr_opt1len = tcphdr_optlen;
195 LWIP_ASSERT(
"p->next != NULL", p->
next != NULL);
201 tcphdr_opt1len = p->
len;
202 opt2len = tcphdr_optlen - tcphdr_opt1len;
212 TCP_STATS_INC(tcp.lenerr);
224 LWIP_ASSERT(
"p->len == 0", p->
len == 0);
229 tcphdr->src = lwip_ntohs(tcphdr->src);
230 tcphdr->dest = lwip_ntohs(tcphdr->dest);
231 seqno = tcphdr->seqno = lwip_ntohl(tcphdr->seqno);
232 ackno = tcphdr->ackno = lwip_ntohl(tcphdr->ackno);
233 tcphdr->wnd = lwip_ntohs(tcphdr->wnd);
235 flags = TCPH_FLAGS(tcphdr);
236 tcplen = p->
tot_len + ((flags & (TCP_FIN | TCP_SYN)) ? 1 : 0);
242 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
243 LWIP_ASSERT(
"tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
244 LWIP_ASSERT(
"tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
245 LWIP_ASSERT(
"tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
246 if (pcb->remote_port == tcphdr->src &&
247 pcb->local_port == tcphdr->dest &&
253 LWIP_ASSERT(
"tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
255 prev->next = pcb->next;
256 pcb->next = tcp_active_pcbs;
257 tcp_active_pcbs = pcb;
259 TCP_STATS_INC(tcp.cachehit);
261 LWIP_ASSERT(
"tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
270 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
271 LWIP_ASSERT(
"tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
272 if (pcb->remote_port == tcphdr->src &&
273 pcb->local_port == tcphdr->dest &&
280 tcp_timewait_input(pcb);
289 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
290 if (lpcb->local_port == tcphdr->dest) {
291 if (IP_IS_ANY_TYPE_VAL(lpcb->local_ip)) {
303 }
else if (ip_addr_isany(&lpcb->local_ip)) {
314 prev = (
struct tcp_pcb *)lpcb;
329 ((
struct tcp_pcb_listen *)prev)->next = lpcb->next;
331 lpcb->next = tcp_listen_pcbs.listen_pcbs;
333 tcp_listen_pcbs.listen_pcbs = lpcb;
335 TCP_STATS_INC(tcp.cachehit);
339 tcp_listen_input(lpcb);
347 tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
355 tcp_debug_print_state(pcb->state);
362 inseg.tcphdr = tcphdr;
368 if (flags & TCP_PSH) {
373 if (pcb->refused_data != NULL) {
374 if ((tcp_process_refused_data(pcb) ==
ERR_ABRT) ||
375 ((pcb->refused_data != NULL) && (tcplen > 0))) {
378 if (pcb->rcv_ann_wnd == 0) {
381 tcp_send_empty_ack(pcb);
383 TCP_STATS_INC(tcp.drop);
384 MIB2_STATS_INC(mib2.tcpinerrs);
389 err = tcp_process(pcb);
393 if (recv_flags & TF_RESET) {
398 TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg,
ERR_RST);
399 tcp_pcb_remove(&tcp_active_pcbs, pcb);
406 if (recv_acked > 0) {
411 u32_t acked = recv_acked;
413 acked16 = (u16_t)LWIP_MIN(acked, 0xffffu);
417 acked16 = recv_acked;
419 TCP_EVENT_SENT(pcb, (u16_t)acked16, err);
426 if (recv_flags & TF_CLOSED) {
429 if (!(pcb->flags & TF_RXCLOSED)) {
433 TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg,
ERR_CLSD);
435 tcp_pcb_remove(&tcp_active_pcbs, pcb);
439 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 440 while (recv_data != NULL) {
441 struct pbuf *rest = NULL;
442 pbuf_split_64k(recv_data, &rest);
444 if (recv_data != NULL) {
447 LWIP_ASSERT(
"pcb->refused_data == NULL", pcb->refused_data == NULL);
448 if (pcb->flags & TF_RXCLOSED) {
452 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 462 TCP_EVENT_RECV(pcb, recv_data,
ERR_OK, err);
464 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 474 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 479 pcb->refused_data = recv_data;
481 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE 492 if (recv_flags & TF_GOT_FIN) {
493 if (pcb->refused_data != NULL) {
499 if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
502 TCP_EVENT_CLOSED(pcb, err);
509 tcp_input_pcb = NULL;
514 tcp_debug_print_state(pcb->state);
522 tcp_input_pcb = NULL;
536 if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
537 TCP_STATS_INC(tcp.proterr);
538 TCP_STATS_INC(tcp.drop);
545 LWIP_ASSERT(
"tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
546 PERF_STOP(
"tcp_input");
549 TCP_STATS_INC(tcp.drop);
550 MIB2_STATS_INC(mib2.tcpinerrs);
564 tcp_listen_input(
struct tcp_pcb_listen *pcb)
566 struct tcp_pcb *npcb;
570 if (flags & TCP_RST) {
577 if (flags & TCP_ACK) {
583 }
else if (flags & TCP_SYN) {
584 LWIP_DEBUGF(
TCP_DEBUG, (
"TCP connection request %"U16_F
" -> %"U16_F
".\n", tcphdr->src, tcphdr->dest));
585 #if TCP_LISTEN_BACKLOG 586 if (pcb->accepts_pending >= pcb->backlog) {
587 LWIP_DEBUGF(
TCP_DEBUG, (
"tcp_listen_input: listen backlog exceeded for port %"U16_F
"\n", tcphdr->dest));
591 npcb = tcp_alloc(pcb->prio);
598 TCP_STATS_INC(tcp.memerr);
599 TCP_EVENT_ACCEPT(pcb, NULL, pcb->callback_arg,
ERR_MEM, err);
603 #if TCP_LISTEN_BACKLOG 604 pcb->accepts_pending++;
605 npcb->flags |= TF_BACKLOGPEND;
610 npcb->local_port = pcb->local_port;
611 npcb->remote_port = tcphdr->src;
612 npcb->state = SYN_RCVD;
613 npcb->rcv_nxt = seqno + 1;
614 npcb->rcv_ann_right_edge = npcb->rcv_nxt;
615 iss = tcp_next_iss(npcb);
620 npcb->snd_wl1 = seqno - 1;
621 npcb->callback_arg = pcb->callback_arg;
622 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG 623 npcb->listener = pcb;
626 npcb->so_options = pcb->so_options & SOF_INHERITED;
629 TCP_REG_ACTIVE(npcb);
633 npcb->snd_wnd = tcphdr->wnd;
634 npcb->snd_wnd_max = npcb->snd_wnd;
636 #if TCP_CALCULATE_EFF_SEND_MSS 637 npcb->mss = tcp_eff_send_mss(npcb->mss, &npcb->local_ip, &npcb->remote_ip);
640 MIB2_STATS_INC(mib2.tcppassiveopens);
643 rc = tcp_enqueue_flags(npcb, TCP_SYN | TCP_ACK);
645 tcp_abandon(npcb, 0);
663 tcp_timewait_input(
struct tcp_pcb *pcb)
670 if (flags & TCP_RST) {
674 if (flags & TCP_SYN) {
677 if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) {
683 }
else if (flags & TCP_FIN) {
686 pcb->tmr = tcp_ticks;
691 pcb->flags |= TF_ACK_NOW;
709 tcp_process(
struct tcp_pcb *pcb)
711 struct tcp_seg *rseg;
718 if (flags & TCP_RST) {
720 if (pcb->state == SYN_SENT) {
723 if (ackno == pcb->snd_nxt) {
729 if (seqno == pcb->rcv_nxt) {
731 }
else if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
732 pcb->rcv_nxt + pcb->rcv_wnd)) {
743 LWIP_ASSERT(
"tcp_input: pcb->state != CLOSED", pcb->state != CLOSED);
744 recv_flags |= TF_RESET;
745 pcb->flags &= ~TF_ACK_DELAY;
749 seqno, pcb->rcv_nxt));
751 seqno, pcb->rcv_nxt));
756 if ((flags & TCP_SYN) && (pcb->state != SYN_SENT && pcb->state != SYN_RCVD)) {
762 if ((pcb->flags & TF_RXCLOSED) == 0) {
764 pcb->tmr = tcp_ticks;
766 pcb->keep_cnt_sent = 0;
771 switch (pcb->state) {
774 pcb->snd_nxt, lwip_ntohl(pcb->unacked->tcphdr->seqno)));
776 if ((flags & TCP_ACK) && (flags & TCP_SYN)
777 && (ackno == pcb->lastack + 1)) {
778 pcb->rcv_nxt = seqno + 1;
779 pcb->rcv_ann_right_edge = pcb->rcv_nxt;
780 pcb->lastack = ackno;
781 pcb->snd_wnd = tcphdr->wnd;
782 pcb->snd_wnd_max = pcb->snd_wnd;
783 pcb->snd_wl1 = seqno - 1;
784 pcb->state = ESTABLISHED;
786 #if TCP_CALCULATE_EFF_SEND_MSS 787 pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
790 pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);
792 " ssthresh %"TCPWNDSIZE_F
"\n",
793 pcb->cwnd, pcb->ssthresh));
794 LWIP_ASSERT(
"pcb->snd_queuelen > 0", (pcb->snd_queuelen > 0));
802 LWIP_ASSERT(
"no segment to free", rseg != NULL);
803 pcb->unsent = rseg->next;
805 pcb->unacked = rseg->next;
811 if (pcb->unacked == NULL) {
820 TCP_EVENT_CONNECTED(pcb,
ERR_OK, err);
827 else if (flags & TCP_ACK) {
841 if (flags & TCP_ACK) {
843 if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)) {
844 pcb->state = ESTABLISHED;
845 LWIP_DEBUGF(
TCP_DEBUG, (
"TCP connection established %"U16_F
" -> %"U16_F
".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
846 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG 847 #if LWIP_CALLBACK_API 848 LWIP_ASSERT(
"pcb->listener->accept != NULL",
849 (pcb->listener == NULL) || (pcb->listener->accept != NULL));
851 if (pcb->listener == NULL) {
857 tcp_backlog_accepted(pcb);
859 TCP_EVENT_ACCEPT(pcb->listener, pcb, pcb->callback_arg,
ERR_OK, err);
875 if (recv_acked != 0) {
879 pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);
881 " ssthresh %"TCPWNDSIZE_F
"\n",
882 pcb->cwnd, pcb->ssthresh));
884 if (recv_flags & TF_GOT_FIN) {
886 pcb->state = CLOSE_WAIT;
893 }
else if ((flags & TCP_SYN) && (seqno == pcb->rcv_nxt - 1)) {
902 if (recv_flags & TF_GOT_FIN) {
904 pcb->state = CLOSE_WAIT;
909 if (recv_flags & TF_GOT_FIN) {
910 if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) &&
911 pcb->unsent == NULL) {
913 (
"TCP connection closed: FIN_WAIT_1 %"U16_F
" -> %"U16_F
".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
917 pcb->state = TIME_WAIT;
918 TCP_REG(&tcp_tw_pcbs, pcb);
921 pcb->state = CLOSING;
923 }
else if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) &&
924 pcb->unsent == NULL) {
925 pcb->state = FIN_WAIT_2;
930 if (recv_flags & TF_GOT_FIN) {
931 LWIP_DEBUGF(
TCP_DEBUG, (
"TCP connection closed: FIN_WAIT_2 %"U16_F
" -> %"U16_F
".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
935 pcb->state = TIME_WAIT;
936 TCP_REG(&tcp_tw_pcbs, pcb);
941 if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) {
942 LWIP_DEBUGF(
TCP_DEBUG, (
"TCP connection closed: CLOSING %"U16_F
" -> %"U16_F
".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
945 pcb->state = TIME_WAIT;
946 TCP_REG(&tcp_tw_pcbs, pcb);
951 if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) {
952 LWIP_DEBUGF(
TCP_DEBUG, (
"TCP connection closed: LAST_ACK %"U16_F
" -> %"U16_F
".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
954 recv_flags |= TF_CLOSED;
970 tcp_oos_insert_segment(
struct tcp_seg *cseg,
struct tcp_seg *next)
972 struct tcp_seg *old_seg;
974 if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
982 TCP_SEQ_GEQ((seqno + cseg->len),
983 (next->tcphdr->seqno + next->len))) {
985 if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
986 TCPH_SET_FLAG(cseg->tcphdr, TCP_FIN);
990 tcp_seg_free(old_seg);
993 TCP_SEQ_GT(seqno + cseg->len, next->tcphdr->seqno)) {
995 cseg->len = (u16_t)(next->tcphdr->seqno - seqno);
1016 tcp_receive(
struct tcp_pcb *pcb)
1018 struct tcp_seg *next;
1020 struct tcp_seg *prev, *cseg;
1024 u32_t right_wnd_edge;
1026 int found_dupack = 0;
1027 #if TCP_OOSEQ_MAX_BYTES || TCP_OOSEQ_MAX_PBUFS 1032 LWIP_ASSERT(
"tcp_receive: wrong state", pcb->state >= ESTABLISHED);
1034 if (flags & TCP_ACK) {
1035 right_wnd_edge = pcb->snd_wnd + pcb->snd_wl2;
1038 if (TCP_SEQ_LT(pcb->snd_wl1, seqno) ||
1039 (pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) ||
1040 (pcb->snd_wl2 == ackno && (u32_t)SND_WND_SCALE(pcb, tcphdr->wnd) > pcb->snd_wnd)) {
1041 pcb->snd_wnd = SND_WND_SCALE(pcb, tcphdr->wnd);
1044 if (pcb->snd_wnd_max < pcb->snd_wnd) {
1045 pcb->snd_wnd_max = pcb->snd_wnd;
1047 pcb->snd_wl1 = seqno;
1048 pcb->snd_wl2 = ackno;
1049 if (pcb->snd_wnd == 0) {
1050 if (pcb->persist_backoff == 0) {
1052 pcb->persist_cnt = 0;
1053 pcb->persist_backoff = 1;
1055 }
else if (pcb->persist_backoff > 0) {
1057 pcb->persist_backoff = 0;
1062 if (pcb->snd_wnd != (tcpwnd_size_t)SND_WND_SCALE(pcb, tcphdr->wnd)) {
1064 (
"tcp_receive: no window update lastack %"U32_F
" ackno %" 1065 U32_F
" wl1 %"U32_F
" seqno %"U32_F
" wl2 %"U32_F
"\n",
1066 pcb->lastack, ackno, pcb->snd_wl1, seqno, pcb->snd_wl2));
1092 if (TCP_SEQ_LEQ(ackno, pcb->lastack)) {
1096 if (pcb->snd_wl2 + pcb->snd_wnd == right_wnd_edge) {
1098 if (pcb->rtime >= 0) {
1100 if (pcb->lastack == ackno) {
1102 if ((u8_t)(pcb->dupacks + 1) > pcb->dupacks) {
1105 if (pcb->dupacks > 3) {
1108 if ((tcpwnd_size_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
1109 pcb->cwnd += pcb->mss;
1111 }
else if (pcb->dupacks == 3) {
1113 tcp_rexmit_fast(pcb);
1121 if (!found_dupack) {
1124 }
else if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)) {
1130 if (pcb->flags & TF_INFR) {
1131 pcb->flags &= ~TF_INFR;
1132 pcb->cwnd = pcb->ssthresh;
1139 pcb->rto = (pcb->sa >> 3) + pcb->sv;
1143 pcb->lastack = ackno;
1147 if (pcb->state >= ESTABLISHED) {
1148 if (pcb->cwnd < pcb->ssthresh) {
1149 if ((tcpwnd_size_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
1150 pcb->cwnd += pcb->mss;
1154 tcpwnd_size_t new_cwnd = (pcb->cwnd + pcb->mss * pcb->mss / pcb->cwnd);
1155 if (new_cwnd > pcb->cwnd) {
1156 pcb->cwnd = new_cwnd;
1163 pcb->unacked != NULL?
1164 lwip_ntohl(pcb->unacked->tcphdr->seqno): 0,
1165 pcb->unacked != NULL?
1166 lwip_ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked): 0));
1170 while (pcb->unacked != NULL &&
1171 TCP_SEQ_LEQ(lwip_ntohl(pcb->unacked->tcphdr->seqno) +
1172 TCP_TCPLEN(pcb->unacked), ackno)) {
1174 lwip_ntohl(pcb->unacked->tcphdr->seqno),
1175 lwip_ntohl(pcb->unacked->tcphdr->seqno) +
1176 TCP_TCPLEN(pcb->unacked)));
1178 next = pcb->unacked;
1179 pcb->unacked = pcb->unacked->next;
1182 LWIP_ASSERT(
"pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >=
pbuf_clen(next->p)));
1184 pcb->snd_queuelen -=
pbuf_clen(next->p);
1185 recv_acked += next->len;
1189 if (pcb->snd_queuelen != 0) {
1190 LWIP_ASSERT(
"tcp_receive: valid queue length", pcb->unacked != NULL ||
1191 pcb->unsent != NULL);
1197 if (pcb->unacked == NULL) {
1205 #if LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS 1206 if (ip_current_is_v6()) {
1208 nd6_reachability_hint(ip6_current_src_addr());
1213 tcp_send_empty_ack(pcb);
1222 while (pcb->unsent != NULL &&
1223 TCP_SEQ_BETWEEN(ackno, lwip_ntohl(pcb->unsent->tcphdr->seqno) +
1224 TCP_TCPLEN(pcb->unsent), pcb->snd_nxt)) {
1226 lwip_ntohl(pcb->unsent->tcphdr->seqno), lwip_ntohl(pcb->unsent->tcphdr->seqno) +
1227 TCP_TCPLEN(pcb->unsent)));
1230 pcb->unsent = pcb->unsent->next;
1232 if (pcb->unsent == NULL) {
1233 pcb->unsent_oversize = 0;
1237 LWIP_ASSERT(
"pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >=
pbuf_clen(next->p)));
1239 pcb->snd_queuelen -=
pbuf_clen(next->p);
1240 recv_acked += next->len;
1243 if (pcb->snd_queuelen != 0) {
1244 LWIP_ASSERT(
"tcp_receive: valid queue length",
1245 pcb->unacked != NULL || pcb->unsent != NULL);
1248 pcb->snd_buf += recv_acked;
1252 pcb->rttest, pcb->rtseq, ackno));
1257 if (pcb->rttest && TCP_SEQ_LT(pcb->rtseq, ackno)) {
1260 m = (s16_t)(tcp_ticks - pcb->rttest);
1263 m, (u16_t)(m * TCP_SLOW_INTERVAL)));
1266 m = m - (pcb->sa >> 3);
1271 m = m - (pcb->sv >> 2);
1273 pcb->rto = (pcb->sa >> 3) + pcb->sv;
1276 pcb->rto, (u16_t)(pcb->rto * TCP_SLOW_INTERVAL)));
1286 if ((tcplen > 0) && (pcb->state < CLOSE_WAIT)) {
1317 if (TCP_SEQ_BETWEEN(pcb->rcv_nxt, seqno + 1, seqno + tcplen - 1)) {
1338 struct pbuf *p = inseg.p;
1339 off = pcb->rcv_nxt - seqno;
1340 LWIP_ASSERT(
"inseg.p != NULL", inseg.p);
1341 LWIP_ASSERT(
"insane offset!", (off < 0x7fff));
1342 if (inseg.p->len < off) {
1343 LWIP_ASSERT(
"pbuf too short!", (((s32_t)inseg.p->tot_len) >= off));
1344 new_tot_len = (u16_t)(inseg.p->tot_len - off);
1345 while (p->
len < off) {
1356 LWIP_ASSERT(
"pbuf_header failed", 0);
1361 LWIP_ASSERT(
"pbuf_header failed", 0);
1364 inseg.len -= (u16_t)(pcb->rcv_nxt - seqno);
1365 inseg.tcphdr->seqno = seqno = pcb->rcv_nxt;
1368 if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)) {
1380 if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
1381 pcb->rcv_nxt + pcb->rcv_wnd - 1)) {
1382 if (pcb->rcv_nxt == seqno) {
1386 tcplen = TCP_TCPLEN(&inseg);
1388 if (tcplen > pcb->rcv_wnd) {
1390 (
"tcp_receive: other end overran receive window" 1391 "seqno %"U32_F
" len %"U16_F
" right edge %"U32_F
"\n",
1392 seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
1393 if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
1396 TCPH_FLAGS_SET(inseg.tcphdr, TCPH_FLAGS(inseg.tcphdr) & ~(
unsigned int)TCP_FIN);
1399 TCPWND_CHECK16(pcb->rcv_wnd);
1400 inseg.len = (u16_t)pcb->rcv_wnd;
1401 if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
1405 tcplen = TCP_TCPLEN(&inseg);
1406 LWIP_ASSERT(
"tcp_receive: segment not trimmed correctly to rcv_wnd\n",
1407 (seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
1413 if (pcb->ooseq != NULL) {
1414 if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
1416 (
"tcp_receive: received in-order FIN, binning ooseq queue\n"));
1420 while (pcb->ooseq != NULL) {
1421 struct tcp_seg *old_ooseq = pcb->ooseq;
1422 pcb->ooseq = pcb->ooseq->next;
1423 tcp_seg_free(old_ooseq);
1430 TCP_SEQ_GEQ(seqno + tcplen,
1431 next->tcphdr->seqno + next->len)) {
1433 if ((TCPH_FLAGS(next->tcphdr) & TCP_FIN) != 0 &&
1434 (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) == 0) {
1435 TCPH_SET_FLAG(inseg.tcphdr, TCP_FIN);
1436 tcplen = TCP_TCPLEN(&inseg);
1445 TCP_SEQ_GT(seqno + tcplen,
1446 next->tcphdr->seqno)) {
1448 inseg.len = (u16_t)(next->tcphdr->seqno - seqno);
1449 if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
1453 tcplen = TCP_TCPLEN(&inseg);
1454 LWIP_ASSERT(
"tcp_receive: segment not trimmed correctly to ooseq queue\n",
1455 (seqno + tcplen) == next->tcphdr->seqno);
1462 pcb->rcv_nxt = seqno + tcplen;
1465 LWIP_ASSERT(
"tcp_receive: tcplen > rcv_wnd\n", pcb->rcv_wnd >= tcplen);
1466 pcb->rcv_wnd -= tcplen;
1468 tcp_update_rcv_ann_wnd(pcb);
1479 if (inseg.p->tot_len > 0) {
1480 recv_data = inseg.p;
1486 if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
1488 recv_flags |= TF_GOT_FIN;
1494 while (pcb->ooseq != NULL &&
1495 pcb->ooseq->tcphdr->seqno == pcb->rcv_nxt) {
1498 seqno = pcb->ooseq->tcphdr->seqno;
1500 pcb->rcv_nxt += TCP_TCPLEN(cseg);
1501 LWIP_ASSERT(
"tcp_receive: ooseq tcplen > rcv_wnd\n",
1502 pcb->rcv_wnd >= TCP_TCPLEN(cseg));
1503 pcb->rcv_wnd -= TCP_TCPLEN(cseg);
1505 tcp_update_rcv_ann_wnd(pcb);
1507 if (cseg->p->tot_len > 0) {
1516 recv_data = cseg->p;
1520 if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
1522 recv_flags |= TF_GOT_FIN;
1523 if (pcb->state == ESTABLISHED) {
1524 pcb->state = CLOSE_WAIT;
1528 pcb->ooseq = cseg->next;
1537 #if LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS 1538 if (ip_current_is_v6()) {
1540 nd6_reachability_hint(ip6_current_src_addr());
1546 tcp_send_empty_ack(pcb);
1549 if (pcb->ooseq == NULL) {
1550 pcb->ooseq = tcp_seg_copy(&inseg);
1565 for (next = pcb->ooseq; next != NULL; next = next->next) {
1566 if (seqno == next->tcphdr->seqno) {
1571 if (inseg.len > next->len) {
1575 cseg = tcp_seg_copy(&inseg);
1582 tcp_oos_insert_segment(cseg, next);
1593 if (TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {
1598 cseg = tcp_seg_copy(&inseg);
1601 tcp_oos_insert_segment(cseg, next);
1608 if (TCP_SEQ_BETWEEN(seqno, prev->tcphdr->seqno+1, next->tcphdr->seqno-1)) {
1614 cseg = tcp_seg_copy(&inseg);
1616 if (TCP_SEQ_GT(prev->tcphdr->seqno + prev->len, seqno)) {
1618 prev->len = (u16_t)(seqno - prev->tcphdr->seqno);
1622 tcp_oos_insert_segment(cseg, next);
1630 if (next->next == NULL &&
1631 TCP_SEQ_GT(seqno, next->tcphdr->seqno)) {
1632 if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
1636 next->next = tcp_seg_copy(&inseg);
1637 if (next->next != NULL) {
1638 if (TCP_SEQ_GT(next->tcphdr->seqno + next->len, seqno)) {
1640 next->len = (u16_t)(seqno - next->tcphdr->seqno);
1644 if (TCP_SEQ_GT((u32_t)tcplen + seqno, pcb->rcv_nxt + (u32_t)pcb->rcv_wnd)) {
1646 (
"tcp_receive: other end overran receive window" 1647 "seqno %"U32_F
" len %"U16_F
" right edge %"U32_F
"\n",
1648 seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
1649 if (TCPH_FLAGS(next->next->tcphdr) & TCP_FIN) {
1652 TCPH_FLAGS_SET(next->next->tcphdr, TCPH_FLAGS(next->next->tcphdr) & ~TCP_FIN);
1655 next->next->len = (u16_t)(pcb->rcv_nxt + pcb->rcv_wnd - seqno);
1657 tcplen = TCP_TCPLEN(next->next);
1658 LWIP_ASSERT(
"tcp_receive: segment not trimmed correctly to rcv_wnd\n",
1659 (seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
1668 #if TCP_OOSEQ_MAX_BYTES || TCP_OOSEQ_MAX_PBUFS 1674 for (next = pcb->ooseq; next != NULL; prev = next, next = next->next) {
1675 struct pbuf *p = next->p;
1681 tcp_segs_free(next);
1697 tcp_send_empty_ack(pcb);
1702 if (!TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd - 1)) {
1709 tcp_getoptbyte(
void)
1711 if ((tcphdr_opt2 == NULL) || (tcp_optidx < tcphdr_opt1len)) {
1712 u8_t* opts = (u8_t *)tcphdr + TCP_HLEN;
1713 return opts[tcp_optidx++];
1715 u8_t idx = (u8_t)(tcp_optidx++ - tcphdr_opt1len);
1716 return tcphdr_opt2[idx];
1729 tcp_parseopt(
struct tcp_pcb *pcb)
1733 #if LWIP_TCP_TIMESTAMPS 1738 if (tcphdr_optlen != 0) {
1739 for (tcp_optidx = 0; tcp_optidx < tcphdr_optlen; ) {
1740 u8_t opt = tcp_getoptbyte();
1742 case LWIP_TCP_OPT_EOL:
1746 case LWIP_TCP_OPT_NOP:
1750 case LWIP_TCP_OPT_MSS:
1752 if (tcp_getoptbyte() != LWIP_TCP_OPT_LEN_MSS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_MSS) > tcphdr_optlen) {
1758 mss = (tcp_getoptbyte() << 8);
1759 mss |= tcp_getoptbyte();
1761 pcb->mss = ((mss > TCP_MSS) || (mss == 0)) ? TCP_MSS : mss;
1764 case LWIP_TCP_OPT_WS:
1766 if (tcp_getoptbyte() != LWIP_TCP_OPT_LEN_WS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_WS) > tcphdr_optlen) {
1773 if ((
flags & TCP_SYN) && !(pcb->flags & TF_WND_SCALE)) {
1775 data = tcp_getoptbyte();
1776 pcb->snd_scale = data;
1777 if (pcb->snd_scale > 14U) {
1778 pcb->snd_scale = 14U;
1780 pcb->rcv_scale = TCP_RCV_SCALE;
1781 pcb->flags |= TF_WND_SCALE;
1783 LWIP_ASSERT(
"window not at default value", pcb->rcv_wnd == TCPWND_MIN16(TCP_WND));
1784 LWIP_ASSERT(
"window not at default value", pcb->rcv_ann_wnd == TCPWND_MIN16(TCP_WND));
1785 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCP_WND;
1789 #if LWIP_TCP_TIMESTAMPS 1790 case LWIP_TCP_OPT_TS:
1792 if (tcp_getoptbyte() != LWIP_TCP_OPT_LEN_TS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_TS) > tcphdr_optlen) {
1798 tsval = tcp_getoptbyte();
1799 tsval |= (tcp_getoptbyte() << 8);
1800 tsval |= (tcp_getoptbyte() << 16);
1801 tsval |= (tcp_getoptbyte() << 24);
1802 if (
flags & TCP_SYN) {
1803 pcb->ts_recent = lwip_ntohl(tsval);
1806 pcb->flags |= TF_TIMESTAMP;
1807 }
else if (TCP_SEQ_BETWEEN(pcb->ts_lastacksent, seqno, seqno+tcplen)) {
1808 pcb->ts_recent = lwip_ntohl(tsval);
1811 tcp_optidx += LWIP_TCP_OPT_LEN_TS - 6;
1816 data = tcp_getoptbyte();
1825 tcp_optidx += data - 2;
1832 tcp_trigger_input_pcb_close(
void)
1834 recv_flags |= TF_CLOSED;
void pbuf_realloc(struct pbuf *p, u16_t new_len)
u16_t pbuf_clen(const struct pbuf *p)
#define ip_current_src_addr()
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define ip_current_netif()
void memp_free(memp_t type, void *mem)
#define ip_current_dest_addr()
#define TCP_OOSEQ_MAX_BYTES
void pbuf_cat(struct pbuf *h, struct pbuf *t)
#define LWIP_DEBUGF(debug, message)
#define PBUF_FLAG_TCP_FIN
#define LWIP_UNUSED_ARG(x)
#define TCP_OOSEQ_MAX_PBUFS
u8_t pbuf_free(struct pbuf *p)