TianoCore EDK2 master
Loading...
Searching...
No Matches
TcpInput.c
Go to the documentation of this file.
1
10#include "TcpMain.h"
11
22INTN
24 IN TCP_CB *Tcb,
25 IN TCP_SEG *Seg
26 )
27{
28 return (TCP_SEQ_LEQ (Tcb->RcvNxt, Seg->End) &&
29 TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2 + Tcb->RcvWnd));
30}
31
39VOID
41 IN OUT TCP_CB *Tcb,
42 IN TCP_SEG *Seg
43 )
44{
45 UINT32 FlightSize;
46 UINT32 Acked;
47
48 //
49 // Step 1: Three duplicate ACKs and not in fast recovery
50 //
51 if (Tcb->CongestState != TCP_CONGEST_RECOVER) {
52 //
53 // Step 1A: Invoking fast retransmission.
54 //
55 FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna);
56
57 Tcb->Ssthresh = MAX (FlightSize >> 1, (UINT32)(2 * Tcb->SndMss));
58 Tcb->Recover = Tcb->SndNxt;
59
60 Tcb->CongestState = TCP_CONGEST_RECOVER;
61 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
62
63 //
64 // Step 2: Entering fast retransmission
65 //
66 TcpRetransmit (Tcb, Tcb->SndUna);
67 Tcb->CWnd = Tcb->Ssthresh + 3 * Tcb->SndMss;
68
69 DEBUG (
70 (DEBUG_NET,
71 "TcpFastRecover: enter fast retransmission for TCB %p, recover point is %d\n",
72 Tcb,
73 Tcb->Recover)
74 );
75 return;
76 }
77
78 //
79 // During fast recovery, execute Step 3, 4, 5 of RFC3782
80 //
81 if (Seg->Ack == Tcb->SndUna) {
82 //
83 // Step 3: Fast Recovery,
84 // If this is a duplicated ACK, increse Cwnd by SMSS.
85 //
86
87 // Step 4 is skipped here only to be executed later
88 // by TcpToSendData
89 //
90 Tcb->CWnd += Tcb->SndMss;
91 DEBUG (
92 (DEBUG_NET,
93 "TcpFastRecover: received another duplicated ACK (%d) for TCB %p\n",
94 Seg->Ack,
95 Tcb)
96 );
97 } else {
98 //
99 // New data is ACKed, check whether it is a
100 // full ACK or partial ACK
101 //
102 if (TCP_SEQ_GEQ (Seg->Ack, Tcb->Recover)) {
103 //
104 // Step 5 - Full ACK:
105 // deflate the congestion window, and exit fast recovery
106 //
107 FlightSize = TCP_SUB_SEQ (Tcb->SndNxt, Tcb->SndUna);
108
109 Tcb->CWnd = MIN (Tcb->Ssthresh, FlightSize + Tcb->SndMss);
110
111 Tcb->CongestState = TCP_CONGEST_OPEN;
112 DEBUG (
113 (DEBUG_NET,
114 "TcpFastRecover: received a full ACK(%d) for TCB %p, exit fast recovery\n",
115 Seg->Ack,
116 Tcb)
117 );
118 } else {
119 //
120 // Step 5 - Partial ACK:
121 // fast retransmit the first unacknowledge field
122 // , then deflate the CWnd
123 //
124 TcpRetransmit (Tcb, Seg->Ack);
125 Acked = TCP_SUB_SEQ (Seg->Ack, Tcb->SndUna);
126
127 //
128 // Deflate the CWnd by the amount of new data
129 // ACKed by SEG.ACK. If more than one SMSS data
130 // is ACKed, add back SMSS byte to CWnd after
131 //
132 if (Acked >= Tcb->SndMss) {
133 Acked -= Tcb->SndMss;
134 }
135
136 Tcb->CWnd -= Acked;
137
138 DEBUG (
139 (DEBUG_NET,
140 "TcpFastRecover: received a partial ACK(%d) for TCB %p\n",
141 Seg->Ack,
142 Tcb)
143 );
144 }
145 }
146}
147
155VOID
157 IN OUT TCP_CB *Tcb,
158 IN TCP_SEG *Seg
159 )
160{
161 if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
162 //
163 // New data is ACKed, check whether it is a
164 // full ACK or partial ACK
165 //
166 if (TCP_SEQ_GEQ (Seg->Ack, Tcb->LossRecover)) {
167 //
168 // Full ACK: exit the loss recovery.
169 //
170 Tcb->LossTimes = 0;
171 Tcb->CongestState = TCP_CONGEST_OPEN;
172
173 DEBUG (
174 (DEBUG_NET,
175 "TcpFastLossRecover: received a full ACK(%d) for TCB %p\n",
176 Seg->Ack,
177 Tcb)
178 );
179 } else {
180 //
181 // Partial ACK:
182 // fast retransmit the first unacknowledge field.
183 //
184 TcpRetransmit (Tcb, Seg->Ack);
185 DEBUG (
186 (DEBUG_NET,
187 "TcpFastLossRecover: received a partial ACK(%d) for TCB %p\n",
188 Seg->Ack,
189 Tcb)
190 );
191 }
192 }
193}
194
202VOID
204 IN OUT TCP_CB *Tcb,
205 IN UINT32 Measure
206 )
207{
208 INT32 Var;
209
210 //
211 // Step 2.3: Compute the RTO for subsequent RTT measurement.
212 //
213 if (Tcb->SRtt != 0) {
214 Var = Tcb->SRtt - (Measure << TCP_RTT_SHIFT);
215
216 if (Var < 0) {
217 Var = -Var;
218 }
219
220 Tcb->RttVar = (3 * Tcb->RttVar + Var) >> 2;
221 Tcb->SRtt = 7 * (Tcb->SRtt >> 3) + Measure;
222 } else {
223 //
224 // Step 2.2: compute the first RTT measure
225 //
226 Tcb->SRtt = Measure << TCP_RTT_SHIFT;
227 Tcb->RttVar = Measure << (TCP_RTT_SHIFT - 1);
228 }
229
230 Tcb->Rto = (Tcb->SRtt + MAX (8, 4 * Tcb->RttVar)) >> TCP_RTT_SHIFT;
231
232 //
233 // Step 2.4: Limit the RTO to at least 1 second
234 // Step 2.5: Limit the RTO to a maximum value that
235 // is at least 60 second
236 //
237 if (Tcb->Rto < TCP_RTO_MIN) {
238 Tcb->Rto = TCP_RTO_MIN;
239 } else if (Tcb->Rto > TCP_RTO_MAX) {
240 Tcb->Rto = TCP_RTO_MAX;
241 }
242
243 DEBUG (
244 (DEBUG_NET,
245 "TcpComputeRtt: new RTT for TCB %p computed SRTT: %d RTTVAR: %d RTO: %d\n",
246 Tcb,
247 Tcb->SRtt,
248 Tcb->RttVar,
249 Tcb->Rto)
250 );
251}
252
264INTN
266 IN NET_BUF *Nbuf,
267 IN TCP_SEQNO Left,
268 IN TCP_SEQNO Right
269 )
270{
271 TCP_SEG *Seg;
272 TCP_SEQNO Urg;
273 UINT32 Drop;
274
275 Seg = TCPSEG_NETBUF (Nbuf);
276
277 //
278 // If the segment is completely out of window,
279 // truncate every thing, include SYN and FIN.
280 //
281 if (TCP_SEQ_LEQ (Seg->End, Left) || TCP_SEQ_LEQ (Right, Seg->Seq)) {
282 TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_SYN);
283 TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_FIN);
284
285 Seg->Seq = Seg->End;
286 NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_HEAD);
287 return 1;
288 }
289
290 //
291 // Adjust the buffer header
292 //
293 if (TCP_SEQ_LT (Seg->Seq, Left)) {
294 Drop = TCP_SUB_SEQ (Left, Seg->Seq);
295 Urg = Seg->Seq + Seg->Urg;
296 Seg->Seq = Left;
297
298 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
299 TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_SYN);
300 Drop--;
301 }
302
303 //
304 // Adjust the urgent point
305 //
306 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG)) {
307 if (TCP_SEQ_LT (Urg, Seg->Seq)) {
308 TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_URG);
309 } else {
310 Seg->Urg = (UINT16)TCP_SUB_SEQ (Urg, Seg->Seq);
311 }
312 }
313
314 if (Drop != 0) {
315 NetbufTrim (Nbuf, Drop, NET_BUF_HEAD);
316 }
317 }
318
319 //
320 // Adjust the buffer tail
321 //
322 if (TCP_SEQ_GT (Seg->End, Right)) {
323 Drop = TCP_SUB_SEQ (Seg->End, Right);
324 Seg->End = Right;
325
326 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
327 TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_FIN);
328 Drop--;
329 }
330
331 if (Drop != 0) {
332 NetbufTrim (Nbuf, Drop, NET_BUF_TAIL);
333 }
334 }
335
336 return TcpVerifySegment (Nbuf);
337}
338
349INTN
351 IN TCP_CB *Tcb,
352 IN NET_BUF *Nbuf
353 )
354{
355 return TcpTrimSegment (Nbuf, Tcb->RcvNxt, Tcb->RcvWl2 + Tcb->RcvWnd);
356}
357
369INTN
371 IN OUT TCP_CB *Tcb
372 )
373{
374 LIST_ENTRY *Entry;
375 NET_BUF *Nbuf;
376 TCP_SEQNO Seq;
377 TCP_SEG *Seg;
378 UINT32 Urgent;
379
380 ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL));
381
382 //
383 // make sure there is some data queued,
384 // and TCP is in a proper state
385 //
386 if (IsListEmpty (&Tcb->RcvQue) || !TCP_CONNECTED (Tcb->State)) {
387 return 0;
388 }
389
390 //
391 // Deliver data to the socket layer
392 //
393 Entry = Tcb->RcvQue.ForwardLink;
394 Seq = Tcb->RcvNxt;
395
396 while (Entry != &Tcb->RcvQue) {
397 Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
398 Seg = TCPSEG_NETBUF (Nbuf);
399
400 if (TcpVerifySegment (Nbuf) == 0) {
401 DEBUG (
402 (DEBUG_ERROR,
403 "TcpToSendData: discard a broken segment for TCB %p\n",
404 Tcb)
405 );
406 NetbufFree (Nbuf);
407 return -1;
408 }
409
410 ASSERT (Nbuf->Tcp == NULL);
411
412 if (TCP_SEQ_GT (Seg->Seq, Seq)) {
413 break;
414 }
415
416 Entry = Entry->ForwardLink;
417 Seq = Seg->End;
418 Tcb->RcvNxt = Seq;
419
420 RemoveEntryList (&Nbuf->List);
421
422 //
423 // RFC793 Eighth step: process FIN in sequence
424 //
425 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
426 //
427 // The peer sends to us junky data after FIN,
428 // reset the connection.
429 //
430 if (!IsListEmpty (&Tcb->RcvQue)) {
431 DEBUG (
432 (DEBUG_ERROR,
433 "TcpDeliverData: data received after FIN from peer of TCB %p, reset connection\n",
434 Tcb)
435 );
436
437 NetbufFree (Nbuf);
438 return -1;
439 }
440
441 DEBUG (
442 (DEBUG_NET,
443 "TcpDeliverData: processing FIN from peer of TCB %p\n",
444 Tcb)
445 );
446
447 switch (Tcb->State) {
448 case TCP_SYN_RCVD:
449 case TCP_ESTABLISHED:
450
451 TcpSetState (Tcb, TCP_CLOSE_WAIT);
452 break;
453
454 case TCP_FIN_WAIT_1:
455
456 if (!TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
457 TcpSetState (Tcb, TCP_CLOSING);
458 break;
459 }
460
461 //
462 // fall through
463 //
464 case TCP_FIN_WAIT_2:
465
466 TcpSetState (Tcb, TCP_TIME_WAIT);
467 TcpClearAllTimer (Tcb);
468
469 if (Tcb->TimeWaitTimeout != 0) {
470 TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout);
471 } else {
472 DEBUG (
473 (DEBUG_WARN,
474 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
475 Tcb)
476 );
477
478 TcpSendAck (Tcb);
479 TcpClose (Tcb);
480 }
481
482 break;
483
484 case TCP_CLOSE_WAIT:
485 case TCP_CLOSING:
486 case TCP_LAST_ACK:
487 case TCP_TIME_WAIT:
488 //
489 // The peer sends to us junk FIN byte. Discard
490 // the buffer then reset the connection
491 //
492 NetbufFree (Nbuf);
493 return -1;
494 break;
495 default:
496 break;
497 }
498
499 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
500
501 Seg->End--;
502 }
503
504 //
505 // Don't delay the ack if PUSH flag is on.
506 //
507 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_PSH)) {
508 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
509 }
510
511 if (Nbuf->TotalSize != 0) {
512 Urgent = 0;
513
514 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) &&
515 TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvUp))
516 {
517 if (TCP_SEQ_LEQ (Seg->End, Tcb->RcvUp)) {
518 Urgent = Nbuf->TotalSize;
519 } else {
520 Urgent = TCP_SUB_SEQ (Tcb->RcvUp, Seg->Seq) + 1;
521 }
522 }
523
524 SockDataRcvd (Tcb->Sk, Nbuf, Urgent);
525 }
526
527 if (TCP_FIN_RCVD (Tcb->State)) {
528 SockNoMoreData (Tcb->Sk);
529 }
530
531 NetbufFree (Nbuf);
532 }
533
534 return 0;
535}
536
547INTN
549 IN OUT TCP_CB *Tcb,
550 IN NET_BUF *Nbuf
551 )
552{
553 TCP_SEG *Seg;
554 LIST_ENTRY *Head;
555 LIST_ENTRY *Prev;
556 LIST_ENTRY *Cur;
557 NET_BUF *Node;
558
559 ASSERT ((Tcb != NULL) && (Nbuf != NULL) && (Nbuf->Tcp == NULL));
560
561 NET_GET_REF (Nbuf);
562
563 Seg = TCPSEG_NETBUF (Nbuf);
564 Head = &Tcb->RcvQue;
565
566 //
567 // Fast path to process normal case. That is,
568 // no out-of-order segments are received.
569 //
570 if (IsListEmpty (Head)) {
571 InsertTailList (Head, &Nbuf->List);
572 return 1;
573 }
574
575 //
576 // Find the point to insert the buffer
577 //
578 for (Prev = Head, Cur = Head->ForwardLink;
579 Cur != Head;
580 Prev = Cur, Cur = Cur->ForwardLink)
581 {
582 Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
583
584 if (TCP_SEQ_LT (Seg->Seq, TCPSEG_NETBUF (Node)->Seq)) {
585 break;
586 }
587 }
588
589 //
590 // Check whether the current segment overlaps with the
591 // previous segment.
592 //
593 if (Prev != Head) {
594 Node = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);
595
596 if (TCP_SEQ_LT (Seg->Seq, TCPSEG_NETBUF (Node)->End)) {
597 if (TCP_SEQ_LEQ (Seg->End, TCPSEG_NETBUF (Node)->End)) {
598 return 1;
599 }
600
601 if (TcpTrimSegment (Nbuf, TCPSEG_NETBUF (Node)->End, Seg->End) == 0) {
602 return 0;
603 }
604 }
605 }
606
607 InsertHeadList (Prev, &Nbuf->List);
608
609 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
610
611 //
612 // Check the segments after the insert point.
613 //
614 while (Cur != Head) {
615 Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
616
617 if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node)->End, Seg->End)) {
618 Cur = Cur->ForwardLink;
619
620 RemoveEntryList (&Node->List);
621 NetbufFree (Node);
622 continue;
623 }
624
625 if (TCP_SEQ_LT (TCPSEG_NETBUF (Node)->Seq, Seg->End)) {
626 if (TCP_SEQ_LEQ (TCPSEG_NETBUF (Node)->Seq, Seg->Seq)) {
627 RemoveEntryList (&Nbuf->List);
628 return 1;
629 }
630
631 if (TcpTrimSegment (Nbuf, Seg->Seq, TCPSEG_NETBUF (Node)->Seq) == 0) {
632 RemoveEntryList (&Nbuf->List);
633 return 0;
634 }
635
636 break;
637 }
638
639 Cur = Cur->ForwardLink;
640 }
641
642 return 1;
643}
644
655INTN
657 IN TCP_CB *Tcb,
658 IN TCP_SEQNO Ack
659 )
660{
661 LIST_ENTRY *Head;
662 LIST_ENTRY *Cur;
663 NET_BUF *Node;
664 TCP_SEG *Seg;
665
666 Head = &Tcb->SndQue;
667 Cur = Head->ForwardLink;
668
669 while (Cur != Head) {
670 Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
671 Seg = TCPSEG_NETBUF (Node);
672
673 if (TCP_SEQ_GEQ (Seg->Seq, Ack)) {
674 break;
675 }
676
677 //
678 // Remove completely ACKed segments
679 //
680 if (TCP_SEQ_LEQ (Seg->End, Ack)) {
681 Cur = Cur->ForwardLink;
682
683 RemoveEntryList (&Node->List);
684 NetbufFree (Node);
685 continue;
686 }
687
688 return TcpTrimSegment (Node, Ack, Seg->End);
689 }
690
691 return 1;
692}
693
709INTN
711 IN NET_BUF *Nbuf,
712 IN EFI_IP_ADDRESS *Src,
713 IN EFI_IP_ADDRESS *Dst,
714 IN UINT8 Version
715 )
716{
717 TCP_CB *Tcb;
718 TCP_CB *Parent;
719 TCP_OPTION Option;
720 TCP_HEAD *Head;
721 INT32 Len;
722 TCP_SEG *Seg;
723 TCP_SEQNO Right;
724 TCP_SEQNO Urg;
725 UINT16 Checksum;
726 INT32 Usable;
727 EFI_STATUS Status;
728
729 ASSERT ((Version == IP_VERSION_4) || (Version == IP_VERSION_6));
730
731 NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);
732
733 Parent = NULL;
734 Tcb = NULL;
735
736 Head = (TCP_HEAD *)NetbufGetByte (Nbuf, 0, NULL);
737 ASSERT (Head != NULL);
738
739 if (Nbuf->TotalSize < sizeof (TCP_HEAD)) {
740 DEBUG ((DEBUG_NET, "TcpInput: received a malformed packet\n"));
741 goto DISCARD;
742 }
743
744 Len = Nbuf->TotalSize - (Head->HeadLen << 2);
745
746 if ((Head->HeadLen < 5) || (Len < 0)) {
747 DEBUG ((DEBUG_NET, "TcpInput: received a malformed packet\n"));
748
749 goto DISCARD;
750 }
751
752 if (Version == IP_VERSION_4) {
753 Checksum = NetPseudoHeadChecksum (Src->Addr[0], Dst->Addr[0], 6, 0);
754 } else {
755 Checksum = NetIp6PseudoHeadChecksum (&Src->v6, &Dst->v6, 6, 0);
756 }
757
758 Checksum = TcpChecksum (Nbuf, Checksum);
759
760 if (Checksum != 0) {
761 DEBUG ((DEBUG_ERROR, "TcpInput: received a checksum error packet\n"));
762 goto DISCARD;
763 }
764
765 if (TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)) {
766 Len++;
767 }
768
769 if (TCP_FLG_ON (Head->Flag, TCP_FLG_FIN)) {
770 Len++;
771 }
772
773 Tcb = TcpLocateTcb (
774 Head->DstPort,
775 Dst,
776 Head->SrcPort,
777 Src,
778 Version,
779 (BOOLEAN)TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)
780 );
781
782 if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) {
783 DEBUG ((DEBUG_NET, "TcpInput: send reset because no TCB found\n"));
784
785 Tcb = NULL;
786 goto SEND_RESET;
787 }
788
789 Seg = TcpFormatNetbuf (Tcb, Nbuf);
790
791 //
792 // RFC1122 recommended reaction to illegal option
793 // (in fact, an illegal option length) is reset.
794 //
795 if (TcpParseOption (Nbuf->Tcp, &Option) == -1) {
796 DEBUG (
797 (DEBUG_ERROR,
798 "TcpInput: reset the peer because of malformed option for TCB %p\n",
799 Tcb)
800 );
801
802 goto SEND_RESET;
803 }
804
805 //
806 // From now on, the segment is headless
807 //
808 NetbufTrim (Nbuf, (Head->HeadLen << 2), NET_BUF_HEAD);
809 Nbuf->Tcp = NULL;
810
811 //
812 // Process the segment in LISTEN state.
813 //
814 if (Tcb->State == TCP_LISTEN) {
815 //
816 // First step: Check RST
817 //
818 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
819 DEBUG (
820 (DEBUG_WARN,
821 "TcpInput: discard a reset segment for TCB %p in listening\n",
822 Tcb)
823 );
824
825 goto DISCARD;
826 }
827
828 //
829 // Second step: Check ACK.
830 // Any ACK sent to TCP in LISTEN is reseted.
831 //
832 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
833 DEBUG (
834 (DEBUG_WARN,
835 "TcpInput: send reset because of segment with ACK for TCB %p in listening\n",
836 Tcb)
837 );
838
839 goto SEND_RESET;
840 }
841
842 //
843 // Third step: Check SYN
844 //
845 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
846 //
847 // create a child TCB to handle the data
848 //
849 Parent = Tcb;
850
851 Tcb = TcpCloneTcb (Parent);
852 if (Tcb == NULL) {
853 DEBUG (
854 (DEBUG_ERROR,
855 "TcpInput: discard a segment because failed to clone a child for TCB %p\n",
856 Tcb)
857 );
858
859 goto DISCARD;
860 }
861
862 DEBUG (
863 (DEBUG_NET,
864 "TcpInput: create a child for TCB %p in listening\n",
865 Tcb)
866 );
867
868 //
869 // init the TCB structure
870 //
871 IP6_COPY_ADDRESS (&Tcb->LocalEnd.Ip, Dst);
872 IP6_COPY_ADDRESS (&Tcb->RemoteEnd.Ip, Src);
873 Tcb->LocalEnd.Port = Head->DstPort;
874 Tcb->RemoteEnd.Port = Head->SrcPort;
875
876 Status = TcpInitTcbLocal (Tcb);
877 if (EFI_ERROR (Status)) {
878 DEBUG (
879 (DEBUG_ERROR,
880 "TcpInput: discard a segment because failed to init local end for TCB %p\n",
881 Tcb)
882 );
883
884 goto DISCARD;
885 }
886
887 TcpInitTcbPeer (Tcb, Seg, &Option);
888
889 TcpSetState (Tcb, TCP_SYN_RCVD);
891 if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
892 DEBUG (
893 (DEBUG_ERROR,
894 "TcpInput: discard a broken segment for TCB %p\n",
895 Tcb)
896 );
897
898 goto DISCARD;
899 }
900
901 goto StepSix;
902 }
903
904 goto DISCARD;
905 } else if (Tcb->State == TCP_SYN_SENT) {
906 //
907 // First step: Check ACK bit
908 //
909 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK) && (Seg->Ack != Tcb->Iss + 1)) {
910 DEBUG (
911 (DEBUG_WARN,
912 "TcpInput: send reset because of wrong ACK received for TCB %p in SYN_SENT\n",
913 Tcb)
914 );
915
916 goto SEND_RESET;
917 }
918
919 //
920 // Second step: Check RST bit
921 //
922 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
923 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
924 DEBUG (
925 (DEBUG_WARN,
926 "TcpInput: connection reset by peer for TCB %p in SYN_SENT\n",
927 Tcb)
928 );
929
930 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
931 goto DROP_CONNECTION;
932 } else {
933 DEBUG (
934 (DEBUG_WARN,
935 "TcpInput: discard a reset segment because of no ACK for TCB %p in SYN_SENT\n",
936 Tcb)
937 );
938
939 goto DISCARD;
940 }
941 }
942
943 //
944 // Third step: Check security and precedence. Skipped
945 //
946
947 //
948 // Fourth step: Check SYN. Pay attention to simultaneous open
949 //
950 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
951 TcpInitTcbPeer (Tcb, Seg, &Option);
952
953 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
954 Tcb->SndUna = Seg->Ack;
955 }
956
958
959 if (TCP_SEQ_GT (Tcb->SndUna, Tcb->Iss)) {
960 TcpSetState (Tcb, TCP_ESTABLISHED);
961
963 TcpDeliverData (Tcb);
964
965 if ((Tcb->CongestState == TCP_CONGEST_OPEN) &&
966 TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON))
967 {
968 TcpComputeRtt (Tcb, Tcb->RttMeasure);
969 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
970 }
971
972 if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
973 DEBUG (
974 (DEBUG_ERROR,
975 "TcpInput: discard a broken segment for TCB %p\n",
976 Tcb)
977 );
978
979 goto DISCARD;
980 }
981
982 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
983
984 DEBUG (
985 (DEBUG_NET,
986 "TcpInput: connection established for TCB %p in SYN_SENT\n",
987 Tcb)
988 );
989
990 goto StepSix;
991 } else {
992 //
993 // Received a SYN segment without ACK, simultaneous open.
994 //
995 TcpSetState (Tcb, TCP_SYN_RCVD);
996
997 ASSERT (Tcb->SndNxt == Tcb->Iss + 1);
998
999 if ((TcpAdjustSndQue (Tcb, Tcb->SndNxt) == 0) || (TcpTrimInWnd (Tcb, Nbuf) == 0)) {
1000 DEBUG (
1001 (DEBUG_ERROR,
1002 "TcpInput: discard a broken segment for TCB %p\n",
1003 Tcb)
1004 );
1005
1006 goto DISCARD;
1007 }
1008
1009 DEBUG (
1010 (DEBUG_WARN,
1011 "TcpInput: simultaneous open for TCB %p in SYN_SENT\n",
1012 Tcb)
1013 );
1014
1015 goto StepSix;
1016 }
1017 }
1018
1019 goto DISCARD;
1020 }
1021
1022 //
1023 // Process segment in SYN_RCVD or TCP_CONNECTED states
1024 //
1025
1026 //
1027 // Clear probe timer since the RecvWindow is opened.
1028 //
1029 if (Tcb->ProbeTimerOn && (Seg->Wnd != 0)) {
1031 Tcb->ProbeTimerOn = FALSE;
1032 }
1033
1034 //
1035 // First step: Check whether SEG.SEQ is acceptable
1036 //
1037 if (TcpSeqAcceptable (Tcb, Seg) == 0) {
1038 DEBUG (
1039 (DEBUG_WARN,
1040 "TcpInput: sequence acceptance test failed for segment of TCB %p\n",
1041 Tcb)
1042 );
1043
1044 if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
1045 TcpSendAck (Tcb);
1046 }
1047
1048 goto DISCARD;
1049 }
1050
1051 if ((TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2)) &&
1052 (Tcb->RcvWl2 == Seg->End) &&
1053 !TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN | TCP_FLG_FIN))
1054 {
1055 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
1056 }
1057
1058 //
1059 // Second step: Check the RST
1060 //
1061 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
1062 DEBUG ((DEBUG_WARN, "TcpInput: connection reset for TCB %p\n", Tcb));
1063
1064 if (Tcb->State == TCP_SYN_RCVD) {
1065 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_REFUSED);
1066
1067 //
1068 // This TCB comes from either a LISTEN TCB,
1069 // or active open TCB with simultaneous open.
1070 // Do NOT signal user CONNECTION refused
1071 // if it comes from a LISTEN TCB.
1072 //
1073 } else if ((Tcb->State == TCP_ESTABLISHED) ||
1074 (Tcb->State == TCP_FIN_WAIT_1) ||
1075 (Tcb->State == TCP_FIN_WAIT_2) ||
1076 (Tcb->State == TCP_CLOSE_WAIT))
1077 {
1078 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
1079 } else {
1080 }
1081
1082 goto DROP_CONNECTION;
1083 }
1084
1085 //
1086 // Trim the data and flags.
1087 //
1088 if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
1089 DEBUG (
1090 (DEBUG_ERROR,
1091 "TcpInput: discard a broken segment for TCB %p\n",
1092 Tcb)
1093 );
1094
1095 goto DISCARD;
1096 }
1097
1098 //
1099 // Third step: Check security and precedence, Ignored
1100 //
1101
1102 //
1103 // Fourth step: Check the SYN bit.
1104 //
1105 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
1106 DEBUG (
1107 (DEBUG_WARN,
1108 "TcpInput: connection reset because received extra SYN for TCB %p\n",
1109 Tcb)
1110 );
1111
1112 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
1113 goto RESET_THEN_DROP;
1114 }
1115
1116 //
1117 // Fifth step: Check the ACK
1118 //
1119 if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
1120 DEBUG (
1121 (DEBUG_WARN,
1122 "TcpInput: segment discard because of no ACK for connected TCB %p\n",
1123 Tcb)
1124 );
1125
1126 goto DISCARD;
1127 } else {
1128 if ((Tcb->IpInfo->IpVersion == IP_VERSION_6) && (Tcb->Tick == 0)) {
1129 Tcp6RefreshNeighbor (Tcb, Src, TCP6_KEEP_NEIGHBOR_TIME * TICKS_PER_SECOND);
1131 }
1132 }
1133
1134 if (Tcb->State == TCP_SYN_RCVD) {
1135 if (TCP_SEQ_LT (Tcb->SndUna, Seg->Ack) &&
1136 TCP_SEQ_LEQ (Seg->Ack, Tcb->SndNxt))
1137 {
1138 Tcb->SndWnd = Seg->Wnd;
1139 Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax);
1140 Tcb->SndWl1 = Seg->Seq;
1141 Tcb->SndWl2 = Seg->Ack;
1142 TcpSetState (Tcb, TCP_ESTABLISHED);
1143
1145 TcpDeliverData (Tcb);
1146
1147 DEBUG (
1148 (DEBUG_NET,
1149 "TcpInput: connection established for TCB %p in SYN_RCVD\n",
1150 Tcb)
1151 );
1152
1153 //
1154 // Continue the process as ESTABLISHED state
1155 //
1156 } else {
1157 DEBUG (
1158 (DEBUG_WARN,
1159 "TcpInput: send reset because of wrong ACK for TCB %p in SYN_RCVD\n",
1160 Tcb)
1161 );
1162
1163 goto SEND_RESET;
1164 }
1165 }
1166
1167 if (TCP_SEQ_LT (Seg->Ack, Tcb->SndUna)) {
1168 DEBUG (
1169 (DEBUG_WARN,
1170 "TcpInput: ignore the out-of-data ACK for connected TCB %p\n",
1171 Tcb)
1172 );
1173
1174 goto StepSix;
1175 } else if (TCP_SEQ_GT (Seg->Ack, Tcb->SndNxt)) {
1176 DEBUG (
1177 (DEBUG_WARN,
1178 "TcpInput: discard segment for future ACK for connected TCB %p\n",
1179 Tcb)
1180 );
1181
1182 TcpSendAck (Tcb);
1183 goto DISCARD;
1184 }
1185
1186 //
1187 // From now on: SND.UNA <= SEG.ACK <= SND.NXT.
1188 //
1189 if (TCP_FLG_ON (Option.Flag, TCP_OPTION_RCVD_TS)) {
1190 //
1191 // update TsRecent as specified in page 17 RFC7323.
1192 // RcvWl2 equals to the variable "LastAckSent"
1193 // defined there.
1194 //
1195 if (TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvWl2) &&
1196 TCP_SEQ_LT (Tcb->RcvWl2, Seg->End))
1197 {
1198 Tcb->TsRecent = Option.TSVal;
1199 Tcb->TsRecentAge = mTcpTick;
1200 }
1201
1202 TcpComputeRtt (Tcb, TCP_SUB_TIME (mTcpTick, Option.TSEcr));
1203 } else if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) {
1204 ASSERT (Tcb->CongestState == TCP_CONGEST_OPEN);
1205
1206 TcpComputeRtt (Tcb, Tcb->RttMeasure);
1207 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
1208 }
1209
1210 if (Seg->Ack == Tcb->SndNxt) {
1212 } else {
1213 TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto);
1214 }
1215
1216 //
1217 // Count duplicate acks.
1218 //
1219 if ((Seg->Ack == Tcb->SndUna) &&
1220 (Tcb->SndUna != Tcb->SndNxt) &&
1221 (Seg->Wnd == Tcb->SndWnd) &&
1222 (0 == Len))
1223 {
1224 Tcb->DupAck++;
1225 } else {
1226 Tcb->DupAck = 0;
1227 }
1228
1229 //
1230 // Congestion avoidance, fast recovery and fast retransmission.
1231 //
1232 if (((Tcb->CongestState == TCP_CONGEST_OPEN) && (Tcb->DupAck < 3)) ||
1234 {
1235 if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
1236 if (Tcb->CWnd < Tcb->Ssthresh) {
1237 Tcb->CWnd += Tcb->SndMss;
1238 } else {
1239 Tcb->CWnd += MAX (Tcb->SndMss * Tcb->SndMss / Tcb->CWnd, 1);
1240 }
1241
1242 Tcb->CWnd = MIN (Tcb->CWnd, TCP_MAX_WIN << Tcb->SndWndScale);
1243 }
1244
1245 if (Tcb->CongestState == TCP_CONGEST_LOSS) {
1246 TcpFastLossRecover (Tcb, Seg);
1247 }
1248 } else {
1249 TcpFastRecover (Tcb, Seg);
1250 }
1251
1252 if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
1253 if (TcpAdjustSndQue (Tcb, Seg->Ack) == 0) {
1254 DEBUG (
1255 (DEBUG_ERROR,
1256 "TcpInput: discard a broken segment for TCB %p\n",
1257 Tcb)
1258 );
1259
1260 goto DISCARD;
1261 }
1262
1263 Tcb->SndUna = Seg->Ack;
1264
1265 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) &&
1266 TCP_SEQ_LT (Tcb->SndUp, Seg->Ack))
1267 {
1268 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_URG);
1269 }
1270 }
1271
1272 //
1273 // Update window info
1274 //
1275 if (TCP_SEQ_LT (Tcb->SndWl1, Seg->Seq) ||
1276 ((Tcb->SndWl1 == Seg->Seq) && TCP_SEQ_LEQ (Tcb->SndWl2, Seg->Ack)))
1277 {
1278 Right = Seg->Ack + Seg->Wnd;
1279
1280 if (TCP_SEQ_LT (Right, Tcb->SndWl2 + Tcb->SndWnd)) {
1281 if ((Tcb->SndWl1 == Seg->Seq) &&
1282 (Tcb->SndWl2 == Seg->Ack) &&
1283 (Len == 0))
1284 {
1285 goto NO_UPDATE;
1286 }
1287
1288 DEBUG (
1289 (DEBUG_WARN,
1290 "TcpInput: peer shrinks the window for connected TCB %p\n",
1291 Tcb)
1292 );
1293
1294 if ((Tcb->CongestState == TCP_CONGEST_RECOVER) &&
1295 (TCP_SEQ_LT (Right, Tcb->Recover)))
1296 {
1297 Tcb->Recover = Right;
1298 }
1299
1300 if ((Tcb->CongestState == TCP_CONGEST_LOSS) &&
1301 (TCP_SEQ_LT (Right, Tcb->LossRecover)))
1302 {
1303 Tcb->LossRecover = Right;
1304 }
1305
1306 if (TCP_SEQ_LT (Right, Tcb->SndNxt)) {
1307 //
1308 // Check for Window Retraction in RFC7923 section 2.4.
1309 // The lower n bits of the peer's actual receive window is wiped out if TCP
1310 // window scale is enabled, it will look like the peer is shrinking the window.
1311 // Check whether the SndNxt is out of the advertised receive window by more than
1312 // 2^Rcv.Wind.Shift before moving the SndNxt to the left.
1313 //
1314 DEBUG (
1315 (DEBUG_WARN,
1316 "TcpInput: peer advise negative useable window for connected TCB %p\n",
1317 Tcb)
1318 );
1319 Usable = TCP_SUB_SEQ (Tcb->SndNxt, Right);
1320 if ((Usable >> Tcb->SndWndScale) > 0) {
1321 DEBUG (
1322 (DEBUG_WARN,
1323 "TcpInput: SndNxt is out of window by more than window scale for TCB %p\n",
1324 Tcb)
1325 );
1326 Tcb->SndNxt = Right;
1327 }
1328
1329 if (Right == Tcb->SndUna) {
1331 TcpSetProbeTimer (Tcb);
1332 }
1333 }
1334 }
1335
1336 Tcb->SndWnd = Seg->Wnd;
1337 Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax);
1338 Tcb->SndWl1 = Seg->Seq;
1339 Tcb->SndWl2 = Seg->Ack;
1340 }
1341
1342NO_UPDATE:
1343
1344 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT) &&
1345 (Tcb->SndUna == Tcb->SndNxt))
1346 {
1347 DEBUG (
1348 (DEBUG_NET,
1349 "TcpInput: local FIN is ACKed by peer for connected TCB %p\n",
1350 Tcb)
1351 );
1352
1353 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED);
1354 }
1355
1356 //
1357 // Transit the state if proper.
1358 //
1359 switch (Tcb->State) {
1360 case TCP_FIN_WAIT_1:
1361
1362 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
1363 TcpSetState (Tcb, TCP_FIN_WAIT_2);
1364
1365 TcpClearAllTimer (Tcb);
1367 }
1368
1369 case TCP_FIN_WAIT_2:
1370
1371 break;
1372
1373 case TCP_CLOSE_WAIT:
1374 break;
1375
1376 case TCP_CLOSING:
1377
1378 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
1379 TcpSetState (Tcb, TCP_TIME_WAIT);
1380
1381 TcpClearAllTimer (Tcb);
1382
1383 if (Tcb->TimeWaitTimeout != 0) {
1385 } else {
1386 DEBUG (
1387 (DEBUG_WARN,
1388 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1389 Tcb)
1390 );
1391
1392 TcpClose (Tcb);
1393 }
1394 }
1395
1396 break;
1397
1398 case TCP_LAST_ACK:
1399
1400 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
1401 TcpSetState (Tcb, TCP_CLOSED);
1402 }
1403
1404 break;
1405
1406 case TCP_TIME_WAIT:
1407
1408 TcpSendAck (Tcb);
1409
1410 if (Tcb->TimeWaitTimeout != 0) {
1412 } else {
1413 DEBUG (
1414 (DEBUG_WARN,
1415 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1416 Tcb)
1417 );
1418
1419 TcpClose (Tcb);
1420 }
1421
1422 break;
1423
1424 default:
1425 break;
1426 }
1427
1428 //
1429 // Sixth step: Check the URG bit.update the Urg point
1430 // if in TCP_CAN_RECV, otherwise, leave the RcvUp intact.
1431 //
1432StepSix:
1433
1434 Tcb->Idle = 0;
1436
1437 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG) && !TCP_FIN_RCVD (Tcb->State)) {
1438 DEBUG (
1439 (DEBUG_NET,
1440 "TcpInput: received urgent data from peer for connected TCB %p\n",
1441 Tcb)
1442 );
1443
1444 Urg = Seg->Seq + Seg->Urg;
1445
1446 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) &&
1447 TCP_SEQ_GT (Urg, Tcb->RcvUp))
1448 {
1449 Tcb->RcvUp = Urg;
1450 } else {
1451 Tcb->RcvUp = Urg;
1452 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG);
1453 }
1454 }
1455
1456 //
1457 // Seventh step: Process the segment data
1458 //
1459 if (Seg->End != Seg->Seq) {
1460 if (TCP_FIN_RCVD (Tcb->State)) {
1461 DEBUG (
1462 (DEBUG_WARN,
1463 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1464 Tcb)
1465 );
1466
1467 goto RESET_THEN_DROP;
1468 }
1469
1470 if (TCP_LOCAL_CLOSED (Tcb->State) && (Nbuf->TotalSize != 0)) {
1471 DEBUG (
1472 (DEBUG_WARN,
1473 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1474 Tcb)
1475 );
1476
1477 goto RESET_THEN_DROP;
1478 }
1479
1480 if (TcpQueueData (Tcb, Nbuf) == 0) {
1481 DEBUG (
1482 (DEBUG_ERROR,
1483 "TcpInput: discard a broken segment for TCB %p\n",
1484 Tcb)
1485 );
1486
1487 goto DISCARD;
1488 }
1489
1490 if (TcpDeliverData (Tcb) == -1) {
1491 goto RESET_THEN_DROP;
1492 }
1493
1494 if (!IsListEmpty (&Tcb->RcvQue)) {
1495 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
1496 }
1497 }
1498
1499 //
1500 // Eighth step: check the FIN.
1501 // This step is moved to TcpDeliverData. FIN will be
1502 // processed in sequence there. Check the comments in
1503 // the beginning of the file header for information.
1504 //
1505
1506 //
1507 // Tcb is a new child of the listening Parent,
1508 // commit it.
1509 //
1510 if (Parent != NULL) {
1511 Tcb->Parent = Parent;
1512 TcpInsertTcb (Tcb);
1513 }
1514
1515 if ((Tcb->State != TCP_CLOSED) &&
1516 (TcpToSendData (Tcb, 0) == 0) &&
1517 (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW) || (Nbuf->TotalSize != 0)))
1518 {
1519 TcpToSendAck (Tcb);
1520 }
1521
1522 NetbufFree (Nbuf);
1523 return 0;
1524
1525RESET_THEN_DROP:
1526 TcpSendReset (Tcb, Head, Len, Dst, Src, Version);
1527
1528DROP_CONNECTION:
1529 ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL));
1530
1531 NetbufFree (Nbuf);
1532 TcpClose (Tcb);
1533
1534 return -1;
1535
1536SEND_RESET:
1537
1538 TcpSendReset (Tcb, Head, Len, Dst, Src, Version);
1539
1540DISCARD:
1541
1542 //
1543 // Tcb is a child of Parent, and it doesn't survive
1544 //
1545 DEBUG ((DEBUG_WARN, "TcpInput: Discard a packet\n"));
1546 NetbufFree (Nbuf);
1547
1548 if ((Parent != NULL) && (Tcb != NULL)) {
1549 ASSERT (Tcb->Sk != NULL);
1550 TcpClose (Tcb);
1551 }
1552
1553 return 0;
1554}
1555
1568VOID
1570 IN NET_BUF *Nbuf,
1571 IN UINT8 IcmpErr,
1572 IN EFI_IP_ADDRESS *Src,
1573 IN EFI_IP_ADDRESS *Dst,
1574 IN UINT8 Version
1575 )
1576{
1577 TCP_HEAD *Head;
1578 TCP_CB *Tcb;
1579 TCP_SEQNO Seq;
1580 EFI_STATUS IcmpErrStatus;
1581 BOOLEAN IcmpErrIsHard;
1582 BOOLEAN IcmpErrNotify;
1583
1584 IcmpErrIsHard = FALSE;
1585 IcmpErrNotify = FALSE;
1586
1587 if (Nbuf->TotalSize < sizeof (TCP_HEAD)) {
1588 goto CLEAN_EXIT;
1589 }
1590
1591 Head = (TCP_HEAD *)NetbufGetByte (Nbuf, 0, NULL);
1592 ASSERT (Head != NULL);
1593
1594 Tcb = TcpLocateTcb (
1595 Head->DstPort,
1596 Dst,
1597 Head->SrcPort,
1598 Src,
1599 Version,
1600 FALSE
1601 );
1602 if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) {
1603 goto CLEAN_EXIT;
1604 }
1605
1606 //
1607 // Validate the sequence number.
1608 //
1609 Seq = NTOHL (Head->Seq);
1610 if (!(TCP_SEQ_LEQ (Tcb->SndUna, Seq) && TCP_SEQ_LT (Seq, Tcb->SndNxt))) {
1611 goto CLEAN_EXIT;
1612 }
1613
1614 IcmpErrStatus = IpIoGetIcmpErrStatus (
1615 IcmpErr,
1616 Tcb->Sk->IpVersion,
1617 &IcmpErrIsHard,
1618 &IcmpErrNotify
1619 );
1620
1621 if (IcmpErrNotify) {
1622 SOCK_ERROR (Tcb->Sk, IcmpErrStatus);
1623 }
1624
1625 if (IcmpErrIsHard) {
1626 TcpClose (Tcb);
1627 }
1628
1629CLEAN_EXIT:
1630
1631 NetbufFree (Nbuf);
1632}
INT64 INTN
BOOLEAN EFIAPI IsListEmpty(IN CONST LIST_ENTRY *ListHead)
Definition: LinkedList.c:403
LIST_ENTRY *EFIAPI InsertHeadList(IN OUT LIST_ENTRY *ListHead, IN OUT LIST_ENTRY *Entry)
Definition: LinkedList.c:218
LIST_ENTRY *EFIAPI RemoveEntryList(IN CONST LIST_ENTRY *Entry)
Definition: LinkedList.c:590
LIST_ENTRY *EFIAPI InsertTailList(IN OUT LIST_ENTRY *ListHead, IN OUT LIST_ENTRY *Entry)
Definition: LinkedList.c:259
EFI_STATUS EFIAPI IpIoGetIcmpErrStatus(IN UINT8 IcmpError, IN UINT8 IpVersion, OUT BOOLEAN *IsHard OPTIONAL, OUT BOOLEAN *Notify OPTIONAL)
Definition: DxeIpIoLib.c:2109
#define NULL
Definition: Base.h:319
#define MIN(a, b)
Definition: Base.h:1007
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define MAX(a, b)
Definition: Base.h:992
#define DEBUG(Expression)
Definition: DebugLib.h:434
VOID EFIAPI NetbufFree(IN NET_BUF *Nbuf)
Definition: NetBuffer.c:195
UINT16 EFIAPI NetIp6PseudoHeadChecksum(IN EFI_IPv6_ADDRESS *Src, IN EFI_IPv6_ADDRESS *Dst, IN UINT8 NextHeader, IN UINT32 Len)
Definition: NetBuffer.c:1777
UINT32 EFIAPI NetbufTrim(IN OUT NET_BUF *Nbuf, IN UINT32 Len, IN BOOLEAN FromHead)
Definition: NetBuffer.c:1134
UINT16 EFIAPI NetPseudoHeadChecksum(IN IP4_ADDR Src, IN IP4_ADDR Dst, IN UINT8 Proto, IN UINT16 Len)
Definition: NetBuffer.c:1740
UINT8 *EFIAPI NetbufGetByte(IN NET_BUF *Nbuf, IN UINT32 Offset, OUT UINT32 *Index OPTIONAL)
Definition: NetBuffer.c:359
VOID SockDataRcvd(IN OUT SOCKET *Sock, IN OUT NET_BUF *NetBuffer, IN UINT32 UrgLen)
Definition: SockImpl.c:1125
VOID SockNoMoreData(IN OUT SOCKET *Sock)
Definition: SockImpl.c:1192
#define SOCK_ERROR(Sock, Error)
Definition: Socket.h:312
EFI_STATUS Tcp6RefreshNeighbor(IN TCP_CB *Tcb, IN EFI_IP_ADDRESS *Neighbor, IN UINT32 Timeout)
Definition: TcpIo.c:157
VOID TcpClose(IN OUT TCP_CB *Tcb)
Definition: TcpTimer.c:96
EFI_STATUS TcpInitTcbLocal(IN OUT TCP_CB *Tcb)
Definition: TcpMisc.c:76
INTN TcpRetransmit(IN TCP_CB *Tcb, IN TCP_SEQNO Seq)
Definition: TcpOutput.c:632
VOID TcpClearTimer(IN OUT TCP_CB *Tcb, IN UINT16 Timer)
Definition: TcpTimer.c:384
INTN TcpVerifySegment(IN NET_BUF *Nbuf)
Definition: TcpOutput.c:1184
INTN TcpSendReset(IN TCP_CB *Tcb, IN TCP_HEAD *Head, IN INT32 Len, IN EFI_IP_ADDRESS *Local, IN EFI_IP_ADDRESS *Remote, IN UINT8 Version)
Definition: TcpOutput.c:1095
INTN TcpToSendData(IN OUT TCP_CB *Tcb, IN INTN Force)
Definition: TcpOutput.c:776
VOID TcpSetProbeTimer(IN OUT TCP_CB *Tcb)
Definition: TcpTimer.c:415
INTN TcpInsertTcb(IN TCP_CB *Tcb)
Definition: TcpMisc.c:478
VOID TcpSetTimer(IN OUT TCP_CB *Tcb, IN UINT16 Timer, IN UINT32 TimeOut)
Definition: TcpTimer.c:364
VOID TcpSetState(IN TCP_CB *Tcb, IN UINT8 State)
Definition: TcpMisc.c:801
TCP_CB * TcpLocateTcb(IN TCP_PORTNO LocalPort, IN EFI_IP_ADDRESS *LocalIp, IN TCP_PORTNO RemotePort, IN EFI_IP_ADDRESS *RemoteIp, IN UINT8 Version, IN BOOLEAN Syn)
Definition: TcpMisc.c:420
TCP_CB * TcpCloneTcb(IN TCP_CB *Tcb)
Definition: TcpMisc.c:534
TCP_SEG * TcpFormatNetbuf(IN TCP_CB *Tcb, IN OUT NET_BUF *Nbuf)
Definition: TcpMisc.c:883
VOID TcpInitTcbPeer(IN OUT TCP_CB *Tcb, IN TCP_SEG *Seg, IN TCP_OPTION *Opt)
Definition: TcpMisc.c:157
VOID TcpSendAck(IN OUT TCP_CB *Tcb)
Definition: TcpOutput.c:967
VOID TcpClearAllTimer(IN OUT TCP_CB *Tcb)
Definition: TcpTimer.c:400
VOID TcpSetKeepaliveTimer(IN OUT TCP_CB *Tcb)
Definition: TcpTimer.c:442
VOID TcpToSendAck(IN OUT TCP_CB *Tcb)
Definition: TcpOutput.c:1043
UINT16 TcpChecksum(IN NET_BUF *Nbuf, IN UINT16 HeadSum)
Definition: TcpMisc.c:854
INTN TcpDeliverData(IN OUT TCP_CB *Tcb)
Definition: TcpInput.c:370
INTN TcpSeqAcceptable(IN TCP_CB *Tcb, IN TCP_SEG *Seg)
Definition: TcpInput.c:23
VOID TcpComputeRtt(IN OUT TCP_CB *Tcb, IN UINT32 Measure)
Definition: TcpInput.c:203
VOID TcpFastRecover(IN OUT TCP_CB *Tcb, IN TCP_SEG *Seg)
Definition: TcpInput.c:40
INTN TcpQueueData(IN OUT TCP_CB *Tcb, IN NET_BUF *Nbuf)
Definition: TcpInput.c:548
VOID TcpIcmpInput(IN NET_BUF *Nbuf, IN UINT8 IcmpErr, IN EFI_IP_ADDRESS *Src, IN EFI_IP_ADDRESS *Dst, IN UINT8 Version)
Definition: TcpInput.c:1569
INTN TcpTrimSegment(IN NET_BUF *Nbuf, IN TCP_SEQNO Left, IN TCP_SEQNO Right)
Definition: TcpInput.c:265
VOID TcpFastLossRecover(IN OUT TCP_CB *Tcb, IN TCP_SEG *Seg)
Definition: TcpInput.c:156
INTN TcpTrimInWnd(IN TCP_CB *Tcb, IN NET_BUF *Nbuf)
Definition: TcpInput.c:350
INTN TcpAdjustSndQue(IN TCP_CB *Tcb, IN TCP_SEQNO Ack)
Definition: TcpInput.c:656
INTN TcpInput(IN NET_BUF *Nbuf, IN EFI_IP_ADDRESS *Src, IN EFI_IP_ADDRESS *Dst, IN UINT8 Version)
Definition: TcpInput.c:710
#define TCP6_REFRESH_NEIGHBOR_TICK
Definition: TcpMain.h:45
#define TCP6_KEEP_NEIGHBOR_TIME
Definition: TcpMain.h:41
INTN TcpParseOption(IN TCP_HEAD *Tcp, IN OUT TCP_OPTION *Option)
Definition: TcpOption.c:232
#define TCP_TIMER_PROBE
Window probe timer.
Definition: TcpProto.h:79
#define TCP_TIMER_2MSL
TIME_WAIT timer.
Definition: TcpProto.h:82
#define TCP_TIMER_CONNECT
Connection establishment timer.
Definition: TcpProto.h:77
#define TCP_CONNECTED(state)
Definition: TcpProto.h:129
#define TCP_RTO_MIN
The minimum value of RTO.
Definition: TcpProto.h:87
#define TCP_CTRL_FIN_ACKED
FIN is ACKed.
Definition: TcpProto.h:69
#define TCP_RTO_MAX
The maximum value of RTO.
Definition: TcpProto.h:88
#define TCP_TIMER_REXMIT
Retransmit timer.
Definition: TcpProto.h:78
#define TCP_CTRL_FIN_SENT
FIN is sent.
Definition: TcpProto.h:68
#define TCP_CLOSED
Definition: TcpProto.h:17
#define TCP_TIMER_FINWAIT2
FIN_WAIT_2 timer.
Definition: TcpProto.h:81
#define TCP_CTRL_RTT_ON
The RTT measurement is on.
Definition: TcpProto.h:71
#define TCP_CTRL_SND_URG
In urgent send mode.
Definition: TcpProto.h:65
#define TCP_CTRL_ACK_NOW
Send the ACK now, don't delay.
Definition: TcpProto.h:72
#define TCP_CONGEST_RECOVER
During the NewReno fast recovery.
Definition: TcpProto.h:51
#define TCP_FLG_FIN
Definition: TcpProto.h:32
#define TCP_CONGEST_LOSS
Retxmit because of retxmit time out.
Definition: TcpProto.h:52
#define TCP_CONGEST_OPEN
TCP is opening its congestion window.
Definition: TcpProto.h:53
#define TCP_CTRL_RCVD_URG
In urgent receive mode.
Definition: TcpProto.h:66
#define TCP_RTT_SHIFT
SRTT & RTTVAR scaled by 8.
Definition: TcpProto.h:86
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
TCP_SEQNO SndUna
First unacknowledged data.
Definition: TcpProto.h:255
UINT32 Tick
1 tick = 200ms
Definition: TcpProto.h:337
UINT8 SndWndScale
Wndscale received from the peer.
Definition: TcpProto.h:283
TCP_PEER RemoteEnd
Remote endpoint.
Definition: TcpProto.h:238
TCP_SEQNO LossRecover
Recover point for retxmit.
Definition: TcpProto.h:307
TCP_SEQNO RcvWl2
Definition: TcpProto.h:266
UINT32 RttMeasure
Currently measured RTT in heartbeats.
Definition: TcpProto.h:292
UINT32 Ssthresh
Slow start threshold.
Definition: TcpProto.h:302
TCP_CB * Parent
The parent TCP_CB structure.
Definition: TcpProto.h:234
UINT32 Idle
How long the connection is in idle.
Definition: TcpProto.h:275
TCP_SEQNO SndWl2
Ack no of last window update.
Definition: TcpProto.h:262
UINT8 CongestState
The current congestion state(RFC3782).
Definition: TcpProto.h:305
UINT32 TimeWaitTimeout
The TIME_WAIT timeout.
Definition: TcpProto.h:324
TCP_SEQNO SndUp
Send urgent point.
Definition: TcpProto.h:258
BOOLEAN ProbeTimerOn
If TRUE, the probe time is on.
Definition: TcpProto.h:277
TCP_SEQNO Recover
Recover point for NewReno.
Definition: TcpProto.h:303
UINT32 Rto
Current RTO, not scaled.
Definition: TcpProto.h:295
TCP_SEQNO Iss
Initial Sending Sequence.
Definition: TcpProto.h:254
UINT32 SndWndMax
Max send window advertised by the peer.
Definition: TcpProto.h:260
UINT8 State
TCP state, such as SYN_SENT, LISTEN.
Definition: TcpProto.h:248
LIST_ENTRY RcvQue
Reassemble queue.
Definition: TcpProto.h:241
UINT16 SndMss
Max send segment size.
Definition: TcpProto.h:263
TCP_SEQNO SndWl1
Seq number used for last window update.
Definition: TcpProto.h:261
UINT32 FinWait2Timeout
The FIN_WAIT_2 timeout.
Definition: TcpProto.h:323
UINT16 DupAck
Number of duplicate ACKs.
Definition: TcpProto.h:304
UINT32 ConnectTimeout
The connect establishment timeout.
Definition: TcpProto.h:325
SOCKET * Sk
The socket it controlled.
Definition: TcpProto.h:236
TCP_PEER LocalEnd
Local endpoint.
Definition: TcpProto.h:237
IP_IO_IP_INFO * IpInfo
Pointer reference to Ip used to send pkt.
Definition: TcpProto.h:336
TCP_SEQNO RcvUp
Urgent point;.
Definition: TcpProto.h:269
UINT32 TsRecentAge
When this TsRecent is updated.
Definition: TcpProto.h:286
UINT32 CtrlFlag
Control flags, such as NO_NAGLE.
Definition: TcpProto.h:242
TCP_SEQNO SndNxt
Next data sequence to send.
Definition: TcpProto.h:256
UINT32 TsRecent
TsRecent to echo to the remote peer.
Definition: TcpProto.h:285
UINT32 CWnd
Sender's congestion window.
Definition: TcpProto.h:301
UINT32 SndWnd
Window advertised by the remote peer.
Definition: TcpProto.h:259
UINT32 TSEcr
The TSEcr field in a timestamp option.
Definition: TcpOption.h:60
UINT8 Flag
Flag such as TCP_OPTION_RCVD_MSS.
Definition: TcpOption.h:56
UINT32 TSVal
The TSVal field in a timestamp option.
Definition: TcpOption.h:59
EFI_IP_ADDRESS Ip
IP address, in network byte order.
Definition: TcpProto.h:223
TCP_PORTNO Port
Port number, in network byte order.
Definition: TcpProto.h:224
TCP_SEQNO Seq
Starting sequence number.
Definition: TcpProto.h:211
TCP_SEQNO End
The sequence of the last byte + 1, include SYN/FIN. End-Seq = SEG.LEN.
Definition: TcpProto.h:212
UINT8 Flag
TCP header flags.
Definition: TcpProto.h:214
UINT16 Urg
Valid if URG flag is set.
Definition: TcpProto.h:215
TCP_SEQNO Ack
ACK field in the segment.
Definition: TcpProto.h:213
UINT32 Wnd
TCP window size field.
Definition: TcpProto.h:216