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