]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/TcpDxe/TcpInput.c
NetworkPkg: Apply uncrustify changes
[mirror_edk2.git] / NetworkPkg / TcpDxe / TcpInput.c
1 /** @file
2 TCP input process routines.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "TcpMain.h"
11
12 /**
13 Check whether the sequence number of the incoming segment is acceptable.
14
15 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
16 @param[in] Seg Pointer to the incoming segment.
17
18 @retval 1 The sequence number is acceptable.
19 @retval 0 The sequence number is not acceptable.
20
21 **/
22 INTN
23 TcpSeqAcceptable (
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
32 /**
33 NewReno fast recovery defined in RFC3782.
34
35 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
36 @param[in] Seg Segment that triggers the fast recovery.
37
38 **/
39 VOID
40 TcpFastRecover (
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
148 /**
149 NewReno fast loss recovery defined in RFC3792.
150
151 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
152 @param[in] Seg Segment that triggers the fast loss recovery.
153
154 **/
155 VOID
156 TcpFastLossRecover (
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
195 /**
196 Compute the RTT as specified in RFC2988.
197
198 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
199 @param[in] Measure Currently measured RTT in heartbeats.
200
201 **/
202 VOID
203 TcpComputeRtt (
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
253 /**
254 Trim the data; SYN and FIN to fit into the window defined by Left and Right.
255
256 @param[in] Nbuf The buffer that contains a received TCP segment without an IP header.
257 @param[in] Left The sequence number of the window's left edge.
258 @param[in] Right The sequence number of the window's right edge.
259
260 @retval 0 The segment is broken.
261 @retval 1 The segment is in good shape.
262
263 **/
264 INTN
265 TcpTrimSegment (
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
339 /**
340 Trim off the data outside the tcb's receive window.
341
342 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
343 @param[in] Nbuf Pointer to the NET_BUF containing the received tcp segment.
344
345 @retval 0 The segment is broken.
346 @retval 1 The segment is in good shape.
347
348 **/
349 INTN
350 TcpTrimInWnd (
351 IN TCP_CB *Tcb,
352 IN NET_BUF *Nbuf
353 )
354 {
355 return TcpTrimSegment (Nbuf, Tcb->RcvNxt, Tcb->RcvWl2 + Tcb->RcvWnd);
356 }
357
358 /**
359 Process the data and FIN flag, and check whether to deliver
360 data to the socket layer.
361
362 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
363
364 @retval 0 No error occurred to deliver data.
365 @retval -1 An error condition occurred. The proper response is to reset the
366 connection.
367
368 **/
369 INTN
370 TcpDeliverData (
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
537 /**
538 Store the data into the reassemble queue.
539
540 @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
541 @param[in] Nbuf Pointer to the buffer containing the data to be queued.
542
543 @retval 0 An error condition occurred.
544 @retval 1 No error occurred to queue data.
545
546 **/
547 INTN
548 TcpQueueData (
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
645 /**
646 Adjust the send queue or the retransmit queue.
647
648 @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
649 @param[in] Ack The acknowledge sequence number of the received segment.
650
651 @retval 0 An error condition occurred.
652 @retval 1 No error occurred.
653
654 **/
655 INTN
656 TcpAdjustSndQue (
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
694 /**
695 Process the received TCP segments.
696
697 @param[in] Nbuf Buffer that contains received a TCP segment without an IP header.
698 @param[in] Src Source address of the segment, or the peer's IP address.
699 @param[in] Dst Destination address of the segment, or the local end's IP
700 address.
701 @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
702 IP6 stack.
703
704 @retval 0 Segment processed successfully. It is either accepted or
705 discarded. However, no connection is reset by the segment.
706 @retval -1 A connection is reset by the segment.
707
708 **/
709 INTN
710 TcpInput (
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
728 ASSERT ((Version == IP_VERSION_4) || (Version == IP_VERSION_6));
729
730 NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);
731
732 Parent = NULL;
733 Tcb = NULL;
734
735 Head = (TCP_HEAD *)NetbufGetByte (Nbuf, 0, NULL);
736 ASSERT (Head != NULL);
737
738 if (Nbuf->TotalSize < sizeof (TCP_HEAD)) {
739 DEBUG ((DEBUG_NET, "TcpInput: received a malformed packet\n"));
740 goto DISCARD;
741 }
742
743 Len = Nbuf->TotalSize - (Head->HeadLen << 2);
744
745 if ((Head->HeadLen < 5) || (Len < 0)) {
746 DEBUG ((DEBUG_NET, "TcpInput: received a malformed packet\n"));
747
748 goto DISCARD;
749 }
750
751 if (Version == IP_VERSION_4) {
752 Checksum = NetPseudoHeadChecksum (Src->Addr[0], Dst->Addr[0], 6, 0);
753 } else {
754 Checksum = NetIp6PseudoHeadChecksum (&Src->v6, &Dst->v6, 6, 0);
755 }
756
757 Checksum = TcpChecksum (Nbuf, Checksum);
758
759 if (Checksum != 0) {
760 DEBUG ((DEBUG_ERROR, "TcpInput: received a checksum error packet\n"));
761 goto DISCARD;
762 }
763
764 if (TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)) {
765 Len++;
766 }
767
768 if (TCP_FLG_ON (Head->Flag, TCP_FLG_FIN)) {
769 Len++;
770 }
771
772 Tcb = TcpLocateTcb (
773 Head->DstPort,
774 Dst,
775 Head->SrcPort,
776 Src,
777 Version,
778 (BOOLEAN)TCP_FLG_ON (Head->Flag, TCP_FLG_SYN)
779 );
780
781 if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) {
782 DEBUG ((DEBUG_NET, "TcpInput: send reset because no TCB found\n"));
783
784 Tcb = NULL;
785 goto SEND_RESET;
786 }
787
788 Seg = TcpFormatNetbuf (Tcb, Nbuf);
789
790 //
791 // RFC1122 recommended reaction to illegal option
792 // (in fact, an illegal option length) is reset.
793 //
794 if (TcpParseOption (Nbuf->Tcp, &Option) == -1) {
795 DEBUG (
796 (DEBUG_ERROR,
797 "TcpInput: reset the peer because of malformed option for TCB %p\n",
798 Tcb)
799 );
800
801 goto SEND_RESET;
802 }
803
804 //
805 // From now on, the segment is headless
806 //
807 NetbufTrim (Nbuf, (Head->HeadLen << 2), NET_BUF_HEAD);
808 Nbuf->Tcp = NULL;
809
810 //
811 // Process the segment in LISTEN state.
812 //
813 if (Tcb->State == TCP_LISTEN) {
814 //
815 // First step: Check RST
816 //
817 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
818 DEBUG (
819 (DEBUG_WARN,
820 "TcpInput: discard a reset segment for TCB %p in listening\n",
821 Tcb)
822 );
823
824 goto DISCARD;
825 }
826
827 //
828 // Second step: Check ACK.
829 // Any ACK sent to TCP in LISTEN is reseted.
830 //
831 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
832 DEBUG (
833 (DEBUG_WARN,
834 "TcpInput: send reset because of segment with ACK for TCB %p in listening\n",
835 Tcb)
836 );
837
838 goto SEND_RESET;
839 }
840
841 //
842 // Third step: Check SYN
843 //
844 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
845 //
846 // create a child TCB to handle the data
847 //
848 Parent = Tcb;
849
850 Tcb = TcpCloneTcb (Parent);
851 if (Tcb == NULL) {
852 DEBUG (
853 (DEBUG_ERROR,
854 "TcpInput: discard a segment because failed to clone a child for TCB %p\n",
855 Tcb)
856 );
857
858 goto DISCARD;
859 }
860
861 DEBUG (
862 (DEBUG_NET,
863 "TcpInput: create a child for TCB %p in listening\n",
864 Tcb)
865 );
866
867 //
868 // init the TCB structure
869 //
870 IP6_COPY_ADDRESS (&Tcb->LocalEnd.Ip, Dst);
871 IP6_COPY_ADDRESS (&Tcb->RemoteEnd.Ip, Src);
872 Tcb->LocalEnd.Port = Head->DstPort;
873 Tcb->RemoteEnd.Port = Head->SrcPort;
874
875 TcpInitTcbLocal (Tcb);
876 TcpInitTcbPeer (Tcb, Seg, &Option);
877
878 TcpSetState (Tcb, TCP_SYN_RCVD);
879 TcpSetTimer (Tcb, TCP_TIMER_CONNECT, Tcb->ConnectTimeout);
880 if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
881 DEBUG (
882 (DEBUG_ERROR,
883 "TcpInput: discard a broken segment for TCB %p\n",
884 Tcb)
885 );
886
887 goto DISCARD;
888 }
889
890 goto StepSix;
891 }
892
893 goto DISCARD;
894 } else if (Tcb->State == TCP_SYN_SENT) {
895 //
896 // First step: Check ACK bit
897 //
898 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK) && (Seg->Ack != Tcb->Iss + 1)) {
899 DEBUG (
900 (DEBUG_WARN,
901 "TcpInput: send reset because of wrong ACK received for TCB %p in SYN_SENT\n",
902 Tcb)
903 );
904
905 goto SEND_RESET;
906 }
907
908 //
909 // Second step: Check RST bit
910 //
911 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
912 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
913 DEBUG (
914 (DEBUG_WARN,
915 "TcpInput: connection reset by peer for TCB %p in SYN_SENT\n",
916 Tcb)
917 );
918
919 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
920 goto DROP_CONNECTION;
921 } else {
922 DEBUG (
923 (DEBUG_WARN,
924 "TcpInput: discard a reset segment because of no ACK for TCB %p in SYN_SENT\n",
925 Tcb)
926 );
927
928 goto DISCARD;
929 }
930 }
931
932 //
933 // Third step: Check security and precedence. Skipped
934 //
935
936 //
937 // Fourth step: Check SYN. Pay attention to simultaneous open
938 //
939 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
940 TcpInitTcbPeer (Tcb, Seg, &Option);
941
942 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
943 Tcb->SndUna = Seg->Ack;
944 }
945
946 TcpClearTimer (Tcb, TCP_TIMER_REXMIT);
947
948 if (TCP_SEQ_GT (Tcb->SndUna, Tcb->Iss)) {
949 TcpSetState (Tcb, TCP_ESTABLISHED);
950
951 TcpClearTimer (Tcb, TCP_TIMER_CONNECT);
952 TcpDeliverData (Tcb);
953
954 if ((Tcb->CongestState == TCP_CONGEST_OPEN) &&
955 TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON))
956 {
957 TcpComputeRtt (Tcb, Tcb->RttMeasure);
958 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
959 }
960
961 if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
962 DEBUG (
963 (DEBUG_ERROR,
964 "TcpInput: discard a broken segment for TCB %p\n",
965 Tcb)
966 );
967
968 goto DISCARD;
969 }
970
971 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
972
973 DEBUG (
974 (DEBUG_NET,
975 "TcpInput: connection established for TCB %p in SYN_SENT\n",
976 Tcb)
977 );
978
979 goto StepSix;
980 } else {
981 //
982 // Received a SYN segment without ACK, simultaneous open.
983 //
984 TcpSetState (Tcb, TCP_SYN_RCVD);
985
986 ASSERT (Tcb->SndNxt == Tcb->Iss + 1);
987
988 if ((TcpAdjustSndQue (Tcb, Tcb->SndNxt) == 0) || (TcpTrimInWnd (Tcb, Nbuf) == 0)) {
989 DEBUG (
990 (DEBUG_ERROR,
991 "TcpInput: discard a broken segment for TCB %p\n",
992 Tcb)
993 );
994
995 goto DISCARD;
996 }
997
998 DEBUG (
999 (DEBUG_WARN,
1000 "TcpInput: simultaneous open for TCB %p in SYN_SENT\n",
1001 Tcb)
1002 );
1003
1004 goto StepSix;
1005 }
1006 }
1007
1008 goto DISCARD;
1009 }
1010
1011 //
1012 // Process segment in SYN_RCVD or TCP_CONNECTED states
1013 //
1014
1015 //
1016 // Clear probe timer since the RecvWindow is opened.
1017 //
1018 if (Tcb->ProbeTimerOn && (Seg->Wnd != 0)) {
1019 TcpClearTimer (Tcb, TCP_TIMER_PROBE);
1020 Tcb->ProbeTimerOn = FALSE;
1021 }
1022
1023 //
1024 // First step: Check whether SEG.SEQ is acceptable
1025 //
1026 if (TcpSeqAcceptable (Tcb, Seg) == 0) {
1027 DEBUG (
1028 (DEBUG_WARN,
1029 "TcpInput: sequence acceptance test failed for segment of TCB %p\n",
1030 Tcb)
1031 );
1032
1033 if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
1034 TcpSendAck (Tcb);
1035 }
1036
1037 goto DISCARD;
1038 }
1039
1040 if ((TCP_SEQ_LT (Seg->Seq, Tcb->RcvWl2)) &&
1041 (Tcb->RcvWl2 == Seg->End) &&
1042 !TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN | TCP_FLG_FIN))
1043 {
1044 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
1045 }
1046
1047 //
1048 // Second step: Check the RST
1049 //
1050 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_RST)) {
1051 DEBUG ((DEBUG_WARN, "TcpInput: connection reset for TCB %p\n", Tcb));
1052
1053 if (Tcb->State == TCP_SYN_RCVD) {
1054 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_REFUSED);
1055
1056 //
1057 // This TCB comes from either a LISTEN TCB,
1058 // or active open TCB with simultaneous open.
1059 // Do NOT signal user CONNECTION refused
1060 // if it comes from a LISTEN TCB.
1061 //
1062 } else if ((Tcb->State == TCP_ESTABLISHED) ||
1063 (Tcb->State == TCP_FIN_WAIT_1) ||
1064 (Tcb->State == TCP_FIN_WAIT_2) ||
1065 (Tcb->State == TCP_CLOSE_WAIT))
1066 {
1067 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
1068 } else {
1069 }
1070
1071 goto DROP_CONNECTION;
1072 }
1073
1074 //
1075 // Trim the data and flags.
1076 //
1077 if (TcpTrimInWnd (Tcb, Nbuf) == 0) {
1078 DEBUG (
1079 (DEBUG_ERROR,
1080 "TcpInput: discard a broken segment for TCB %p\n",
1081 Tcb)
1082 );
1083
1084 goto DISCARD;
1085 }
1086
1087 //
1088 // Third step: Check security and precedence, Ignored
1089 //
1090
1091 //
1092 // Fourth step: Check the SYN bit.
1093 //
1094 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
1095 DEBUG (
1096 (DEBUG_WARN,
1097 "TcpInput: connection reset because received extra SYN for TCB %p\n",
1098 Tcb)
1099 );
1100
1101 SOCK_ERROR (Tcb->Sk, EFI_CONNECTION_RESET);
1102 goto RESET_THEN_DROP;
1103 }
1104
1105 //
1106 // Fifth step: Check the ACK
1107 //
1108 if (!TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
1109 DEBUG (
1110 (DEBUG_WARN,
1111 "TcpInput: segment discard because of no ACK for connected TCB %p\n",
1112 Tcb)
1113 );
1114
1115 goto DISCARD;
1116 } else {
1117 if ((Tcb->IpInfo->IpVersion == IP_VERSION_6) && (Tcb->Tick == 0)) {
1118 Tcp6RefreshNeighbor (Tcb, Src, TCP6_KEEP_NEIGHBOR_TIME * TICKS_PER_SECOND);
1119 Tcb->Tick = TCP6_REFRESH_NEIGHBOR_TICK;
1120 }
1121 }
1122
1123 if (Tcb->State == TCP_SYN_RCVD) {
1124 if (TCP_SEQ_LT (Tcb->SndUna, Seg->Ack) &&
1125 TCP_SEQ_LEQ (Seg->Ack, Tcb->SndNxt))
1126 {
1127 Tcb->SndWnd = Seg->Wnd;
1128 Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax);
1129 Tcb->SndWl1 = Seg->Seq;
1130 Tcb->SndWl2 = Seg->Ack;
1131 TcpSetState (Tcb, TCP_ESTABLISHED);
1132
1133 TcpClearTimer (Tcb, TCP_TIMER_CONNECT);
1134 TcpDeliverData (Tcb);
1135
1136 DEBUG (
1137 (DEBUG_NET,
1138 "TcpInput: connection established for TCB %p in SYN_RCVD\n",
1139 Tcb)
1140 );
1141
1142 //
1143 // Continue the process as ESTABLISHED state
1144 //
1145 } else {
1146 DEBUG (
1147 (DEBUG_WARN,
1148 "TcpInput: send reset because of wrong ACK for TCB %p in SYN_RCVD\n",
1149 Tcb)
1150 );
1151
1152 goto SEND_RESET;
1153 }
1154 }
1155
1156 if (TCP_SEQ_LT (Seg->Ack, Tcb->SndUna)) {
1157 DEBUG (
1158 (DEBUG_WARN,
1159 "TcpInput: ignore the out-of-data ACK for connected TCB %p\n",
1160 Tcb)
1161 );
1162
1163 goto StepSix;
1164 } else if (TCP_SEQ_GT (Seg->Ack, Tcb->SndNxt)) {
1165 DEBUG (
1166 (DEBUG_WARN,
1167 "TcpInput: discard segment for future ACK for connected TCB %p\n",
1168 Tcb)
1169 );
1170
1171 TcpSendAck (Tcb);
1172 goto DISCARD;
1173 }
1174
1175 //
1176 // From now on: SND.UNA <= SEG.ACK <= SND.NXT.
1177 //
1178 if (TCP_FLG_ON (Option.Flag, TCP_OPTION_RCVD_TS)) {
1179 //
1180 // update TsRecent as specified in page 16 RFC1323.
1181 // RcvWl2 equals to the variable "LastAckSent"
1182 // defined there.
1183 //
1184 if (TCP_SEQ_LEQ (Seg->Seq, Tcb->RcvWl2) &&
1185 TCP_SEQ_LT (Tcb->RcvWl2, Seg->End))
1186 {
1187 Tcb->TsRecent = Option.TSVal;
1188 Tcb->TsRecentAge = mTcpTick;
1189 }
1190
1191 TcpComputeRtt (Tcb, TCP_SUB_TIME (mTcpTick, Option.TSEcr));
1192 } else if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) {
1193 ASSERT (Tcb->CongestState == TCP_CONGEST_OPEN);
1194
1195 TcpComputeRtt (Tcb, Tcb->RttMeasure);
1196 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
1197 }
1198
1199 if (Seg->Ack == Tcb->SndNxt) {
1200 TcpClearTimer (Tcb, TCP_TIMER_REXMIT);
1201 } else {
1202 TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto);
1203 }
1204
1205 //
1206 // Count duplicate acks.
1207 //
1208 if ((Seg->Ack == Tcb->SndUna) &&
1209 (Tcb->SndUna != Tcb->SndNxt) &&
1210 (Seg->Wnd == Tcb->SndWnd) &&
1211 (0 == Len))
1212 {
1213 Tcb->DupAck++;
1214 } else {
1215 Tcb->DupAck = 0;
1216 }
1217
1218 //
1219 // Congestion avoidance, fast recovery and fast retransmission.
1220 //
1221 if (((Tcb->CongestState == TCP_CONGEST_OPEN) && (Tcb->DupAck < 3)) ||
1222 (Tcb->CongestState == TCP_CONGEST_LOSS))
1223 {
1224 if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
1225 if (Tcb->CWnd < Tcb->Ssthresh) {
1226 Tcb->CWnd += Tcb->SndMss;
1227 } else {
1228 Tcb->CWnd += MAX (Tcb->SndMss * Tcb->SndMss / Tcb->CWnd, 1);
1229 }
1230
1231 Tcb->CWnd = MIN (Tcb->CWnd, TCP_MAX_WIN << Tcb->SndWndScale);
1232 }
1233
1234 if (Tcb->CongestState == TCP_CONGEST_LOSS) {
1235 TcpFastLossRecover (Tcb, Seg);
1236 }
1237 } else {
1238 TcpFastRecover (Tcb, Seg);
1239 }
1240
1241 if (TCP_SEQ_GT (Seg->Ack, Tcb->SndUna)) {
1242 if (TcpAdjustSndQue (Tcb, Seg->Ack) == 0) {
1243 DEBUG (
1244 (DEBUG_ERROR,
1245 "TcpInput: discard a broken segment for TCB %p\n",
1246 Tcb)
1247 );
1248
1249 goto DISCARD;
1250 }
1251
1252 Tcb->SndUna = Seg->Ack;
1253
1254 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) &&
1255 TCP_SEQ_LT (Tcb->SndUp, Seg->Ack))
1256 {
1257 TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_URG);
1258 }
1259 }
1260
1261 //
1262 // Update window info
1263 //
1264 if (TCP_SEQ_LT (Tcb->SndWl1, Seg->Seq) ||
1265 ((Tcb->SndWl1 == Seg->Seq) && TCP_SEQ_LEQ (Tcb->SndWl2, Seg->Ack)))
1266 {
1267 Right = Seg->Ack + Seg->Wnd;
1268
1269 if (TCP_SEQ_LT (Right, Tcb->SndWl2 + Tcb->SndWnd)) {
1270 if ((Tcb->SndWl1 == Seg->Seq) &&
1271 (Tcb->SndWl2 == Seg->Ack) &&
1272 (Len == 0))
1273 {
1274 goto NO_UPDATE;
1275 }
1276
1277 DEBUG (
1278 (DEBUG_WARN,
1279 "TcpInput: peer shrinks the window for connected TCB %p\n",
1280 Tcb)
1281 );
1282
1283 if ((Tcb->CongestState == TCP_CONGEST_RECOVER) &&
1284 (TCP_SEQ_LT (Right, Tcb->Recover)))
1285 {
1286 Tcb->Recover = Right;
1287 }
1288
1289 if ((Tcb->CongestState == TCP_CONGEST_LOSS) &&
1290 (TCP_SEQ_LT (Right, Tcb->LossRecover)))
1291 {
1292 Tcb->LossRecover = Right;
1293 }
1294
1295 if (TCP_SEQ_LT (Right, Tcb->SndNxt)) {
1296 //
1297 // Check for Window Retraction in RFC7923 section 2.4.
1298 // The lower n bits of the peer's actual receive window is wiped out if TCP
1299 // window scale is enabled, it will look like the peer is shrinking the window.
1300 // Check whether the SndNxt is out of the advertised receive window by more than
1301 // 2^Rcv.Wind.Shift before moving the SndNxt to the left.
1302 //
1303 DEBUG (
1304 (DEBUG_WARN,
1305 "TcpInput: peer advise negative useable window for connected TCB %p\n",
1306 Tcb)
1307 );
1308 Usable = TCP_SUB_SEQ (Tcb->SndNxt, Right);
1309 if ((Usable >> Tcb->SndWndScale) > 0) {
1310 DEBUG (
1311 (DEBUG_WARN,
1312 "TcpInput: SndNxt is out of window by more than window scale for TCB %p\n",
1313 Tcb)
1314 );
1315 Tcb->SndNxt = Right;
1316 }
1317
1318 if (Right == Tcb->SndUna) {
1319 TcpClearTimer (Tcb, TCP_TIMER_REXMIT);
1320 TcpSetProbeTimer (Tcb);
1321 }
1322 }
1323 }
1324
1325 Tcb->SndWnd = Seg->Wnd;
1326 Tcb->SndWndMax = MAX (Tcb->SndWnd, Tcb->SndWndMax);
1327 Tcb->SndWl1 = Seg->Seq;
1328 Tcb->SndWl2 = Seg->Ack;
1329 }
1330
1331 NO_UPDATE:
1332
1333 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT) &&
1334 (Tcb->SndUna == Tcb->SndNxt))
1335 {
1336 DEBUG (
1337 (DEBUG_NET,
1338 "TcpInput: local FIN is ACKed by peer for connected TCB %p\n",
1339 Tcb)
1340 );
1341
1342 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED);
1343 }
1344
1345 //
1346 // Transit the state if proper.
1347 //
1348 switch (Tcb->State) {
1349 case TCP_FIN_WAIT_1:
1350
1351 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
1352 TcpSetState (Tcb, TCP_FIN_WAIT_2);
1353
1354 TcpClearAllTimer (Tcb);
1355 TcpSetTimer (Tcb, TCP_TIMER_FINWAIT2, Tcb->FinWait2Timeout);
1356 }
1357
1358 case TCP_FIN_WAIT_2:
1359
1360 break;
1361
1362 case TCP_CLOSE_WAIT:
1363 break;
1364
1365 case TCP_CLOSING:
1366
1367 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
1368 TcpSetState (Tcb, TCP_TIME_WAIT);
1369
1370 TcpClearAllTimer (Tcb);
1371
1372 if (Tcb->TimeWaitTimeout != 0) {
1373 TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout);
1374 } else {
1375 DEBUG (
1376 (DEBUG_WARN,
1377 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1378 Tcb)
1379 );
1380
1381 TcpClose (Tcb);
1382 }
1383 }
1384
1385 break;
1386
1387 case TCP_LAST_ACK:
1388
1389 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_ACKED)) {
1390 TcpSetState (Tcb, TCP_CLOSED);
1391 }
1392
1393 break;
1394
1395 case TCP_TIME_WAIT:
1396
1397 TcpSendAck (Tcb);
1398
1399 if (Tcb->TimeWaitTimeout != 0) {
1400 TcpSetTimer (Tcb, TCP_TIMER_2MSL, Tcb->TimeWaitTimeout);
1401 } else {
1402 DEBUG (
1403 (DEBUG_WARN,
1404 "Connection closed immediately because app disables TIME_WAIT timer for %p\n",
1405 Tcb)
1406 );
1407
1408 TcpClose (Tcb);
1409 }
1410
1411 break;
1412
1413 default:
1414 break;
1415 }
1416
1417 //
1418 // Sixth step: Check the URG bit.update the Urg point
1419 // if in TCP_CAN_RECV, otherwise, leave the RcvUp intact.
1420 //
1421 StepSix:
1422
1423 Tcb->Idle = 0;
1424 TcpSetKeepaliveTimer (Tcb);
1425
1426 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_URG) && !TCP_FIN_RCVD (Tcb->State)) {
1427 DEBUG (
1428 (DEBUG_NET,
1429 "TcpInput: received urgent data from peer for connected TCB %p\n",
1430 Tcb)
1431 );
1432
1433 Urg = Seg->Seq + Seg->Urg;
1434
1435 if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG) &&
1436 TCP_SEQ_GT (Urg, Tcb->RcvUp))
1437 {
1438 Tcb->RcvUp = Urg;
1439 } else {
1440 Tcb->RcvUp = Urg;
1441 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_URG);
1442 }
1443 }
1444
1445 //
1446 // Seventh step: Process the segment data
1447 //
1448 if (Seg->End != Seg->Seq) {
1449 if (TCP_FIN_RCVD (Tcb->State)) {
1450 DEBUG (
1451 (DEBUG_WARN,
1452 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1453 Tcb)
1454 );
1455
1456 goto RESET_THEN_DROP;
1457 }
1458
1459 if (TCP_LOCAL_CLOSED (Tcb->State) && (Nbuf->TotalSize != 0)) {
1460 DEBUG (
1461 (DEBUG_WARN,
1462 "TcpInput: connection reset because data is lost for connected TCB %p\n",
1463 Tcb)
1464 );
1465
1466 goto RESET_THEN_DROP;
1467 }
1468
1469 if (TcpQueueData (Tcb, Nbuf) == 0) {
1470 DEBUG (
1471 (DEBUG_ERROR,
1472 "TcpInput: discard a broken segment for TCB %p\n",
1473 Tcb)
1474 );
1475
1476 goto DISCARD;
1477 }
1478
1479 if (TcpDeliverData (Tcb) == -1) {
1480 goto RESET_THEN_DROP;
1481 }
1482
1483 if (!IsListEmpty (&Tcb->RcvQue)) {
1484 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
1485 }
1486 }
1487
1488 //
1489 // Eighth step: check the FIN.
1490 // This step is moved to TcpDeliverData. FIN will be
1491 // processed in sequence there. Check the comments in
1492 // the beginning of the file header for information.
1493 //
1494
1495 //
1496 // Tcb is a new child of the listening Parent,
1497 // commit it.
1498 //
1499 if (Parent != NULL) {
1500 Tcb->Parent = Parent;
1501 TcpInsertTcb (Tcb);
1502 }
1503
1504 if ((Tcb->State != TCP_CLOSED) &&
1505 (TcpToSendData (Tcb, 0) == 0) &&
1506 (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW) || (Nbuf->TotalSize != 0)))
1507 {
1508 TcpToSendAck (Tcb);
1509 }
1510
1511 NetbufFree (Nbuf);
1512 return 0;
1513
1514 RESET_THEN_DROP:
1515 TcpSendReset (Tcb, Head, Len, Dst, Src, Version);
1516
1517 DROP_CONNECTION:
1518 ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL));
1519
1520 NetbufFree (Nbuf);
1521 TcpClose (Tcb);
1522
1523 return -1;
1524
1525 SEND_RESET:
1526
1527 TcpSendReset (Tcb, Head, Len, Dst, Src, Version);
1528
1529 DISCARD:
1530
1531 //
1532 // Tcb is a child of Parent, and it doesn't survive
1533 //
1534 DEBUG ((DEBUG_WARN, "TcpInput: Discard a packet\n"));
1535 NetbufFree (Nbuf);
1536
1537 if ((Parent != NULL) && (Tcb != NULL)) {
1538 ASSERT (Tcb->Sk != NULL);
1539 TcpClose (Tcb);
1540 }
1541
1542 return 0;
1543 }
1544
1545 /**
1546 Process the received ICMP error messages for TCP.
1547
1548 @param[in] Nbuf The buffer that contains part of the TCP segment without an IP header
1549 truncated from the ICMP error packet.
1550 @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet.
1551 @param[in] Src Source address of the ICMP error message.
1552 @param[in] Dst Destination address of the ICMP error message.
1553 @param[in] Version IP_VERSION_4 indicates IP4 stack. IP_VERSION_6 indicates
1554 IP6 stack.
1555
1556 **/
1557 VOID
1558 TcpIcmpInput (
1559 IN NET_BUF *Nbuf,
1560 IN UINT8 IcmpErr,
1561 IN EFI_IP_ADDRESS *Src,
1562 IN EFI_IP_ADDRESS *Dst,
1563 IN UINT8 Version
1564 )
1565 {
1566 TCP_HEAD *Head;
1567 TCP_CB *Tcb;
1568 TCP_SEQNO Seq;
1569 EFI_STATUS IcmpErrStatus;
1570 BOOLEAN IcmpErrIsHard;
1571 BOOLEAN IcmpErrNotify;
1572
1573 if (Nbuf->TotalSize < sizeof (TCP_HEAD)) {
1574 goto CLEAN_EXIT;
1575 }
1576
1577 Head = (TCP_HEAD *)NetbufGetByte (Nbuf, 0, NULL);
1578 ASSERT (Head != NULL);
1579
1580 Tcb = TcpLocateTcb (
1581 Head->DstPort,
1582 Dst,
1583 Head->SrcPort,
1584 Src,
1585 Version,
1586 FALSE
1587 );
1588 if ((Tcb == NULL) || (Tcb->State == TCP_CLOSED)) {
1589 goto CLEAN_EXIT;
1590 }
1591
1592 //
1593 // Validate the sequence number.
1594 //
1595 Seq = NTOHL (Head->Seq);
1596 if (!(TCP_SEQ_LEQ (Tcb->SndUna, Seq) && TCP_SEQ_LT (Seq, Tcb->SndNxt))) {
1597 goto CLEAN_EXIT;
1598 }
1599
1600 IcmpErrStatus = IpIoGetIcmpErrStatus (
1601 IcmpErr,
1602 Tcb->Sk->IpVersion,
1603 &IcmpErrIsHard,
1604 &IcmpErrNotify
1605 );
1606
1607 if (IcmpErrNotify) {
1608 SOCK_ERROR (Tcb->Sk, IcmpErrStatus);
1609 }
1610
1611 if (IcmpErrIsHard) {
1612 TcpClose (Tcb);
1613 }
1614
1615 CLEAN_EXIT:
1616
1617 NetbufFree (Nbuf);
1618 }