summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/Network/Tcp4Dxe/Tcp4Output.c
blob: b44e851b265ac266062a731bc4802dc55380834b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
/** @file
  TCP output process routines.
    
Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php<BR>

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "Tcp4Main.h"

UINT8  mTcpOutFlag[] = {
  0,                          // TCP_CLOSED
  0,                          // TCP_LISTEN
  TCP_FLG_SYN,                // TCP_SYN_SENT
  TCP_FLG_SYN | TCP_FLG_ACK,  // TCP_SYN_RCVD
  TCP_FLG_ACK,                // TCP_ESTABLISHED
  TCP_FLG_FIN | TCP_FLG_ACK,  // TCP_FIN_WAIT_1
  TCP_FLG_ACK,                // TCP_FIN_WAIT_2
  TCP_FLG_ACK | TCP_FLG_FIN,  // TCP_CLOSING
  TCP_FLG_ACK,                // TCP_TIME_WAIT
  TCP_FLG_ACK,                // TCP_CLOSE_WAIT
  TCP_FLG_FIN | TCP_FLG_ACK   // TCP_LAST_ACK
};


/**
  Compute the sequence space left in the old receive window.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.

  @return The sequence space left in the old receive window.

**/
UINT32
TcpRcvWinOld (
  IN TCP_CB *Tcb
  )
{
  UINT32  OldWin;

  OldWin = 0;

  if (TCP_SEQ_GT (Tcb->RcvWl2 + Tcb->RcvWnd, Tcb->RcvNxt)) {

    OldWin = TCP_SUB_SEQ (
              Tcb->RcvWl2 + Tcb->RcvWnd,
              Tcb->RcvNxt
              );
  }

  return OldWin;
}


/**
  Compute the current receive window.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.

  @return The size of the current receive window, in bytes.

**/
UINT32
TcpRcvWinNow (
  IN TCP_CB *Tcb
  )
{
  SOCKET  *Sk;
  UINT32  Win;
  UINT32  Increase;
  UINT32  OldWin;

  Sk = Tcb->Sk;
  ASSERT (Sk != NULL);

  OldWin    = TcpRcvWinOld (Tcb);

  Win       = SockGetFreeSpace (Sk, SOCK_RCV_BUF);

  Increase  = 0;
  if (Win > OldWin) {
    Increase = Win - OldWin;
  }

  //
  // Receiver's SWS: don't advertise a bigger window
  // unless it can be increased by at least one Mss or
  // half of the receive buffer.
  //
  if ((Increase > Tcb->SndMss) ||
      (2 * Increase >= GET_RCV_BUFFSIZE (Sk))) {

    return Win;
  }

  return OldWin;
}


/**
  Compute the value to fill in the window size field of the outgoing segment.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Syn     The flag to indicate whether the outgoing segment is a SYN
                  segment.

  @return The value of the local receive window size used to fill the outing segment.

**/
UINT16
TcpComputeWnd (
  IN OUT TCP_CB  *Tcb,
  IN     BOOLEAN Syn
  )
{
  UINT32  Wnd;

  //
  // RFC requires that initial window not be scaled
  //
  if (Syn) {

    Wnd = GET_RCV_BUFFSIZE (Tcb->Sk);
  } else {

    Wnd         = TcpRcvWinNow (Tcb);

    Tcb->RcvWnd = Wnd;
  }

  Wnd = MIN (Wnd >> Tcb->RcvWndScale, 0xffff);
  return NTOHS ((UINT16) Wnd);
}


/**
  Get the maximum SndNxt.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.

  @return The sequence number of the maximum SndNxt.

**/
TCP_SEQNO
TcpGetMaxSndNxt (
  IN TCP_CB *Tcb
  )
{
  LIST_ENTRY      *Entry;
  NET_BUF         *Nbuf;

  if (IsListEmpty (&Tcb->SndQue)) {
    return Tcb->SndNxt;
  }

  Entry = Tcb->SndQue.BackLink;
  Nbuf  = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);

  ASSERT (TCP_SEQ_GEQ (TCPSEG_NETBUF (Nbuf)->End, Tcb->SndNxt));
  return TCPSEG_NETBUF (Nbuf)->End;
}


/**
  Compute how much data to send.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Force   Whether to ignore the sender's SWS avoidance algorithm and send
                  out data by force.

  @return The length of the data can be sent, if 0, no data can be sent.

**/
UINT32
TcpDataToSend (
  IN TCP_CB *Tcb,
  IN INTN   Force
  )
{
  SOCKET  *Sk;
  UINT32  Win;
  UINT32  Len;
  UINT32  Left;
  UINT32  Limit;

  Sk = Tcb->Sk;
  ASSERT (Sk != NULL);

  //
  // TCP should NOT send data beyond the send window
  // and congestion window. The right edge of send
  // window is defined as SND.WL2 + SND.WND. The right
  // edge of congestion window is defined as SND.UNA +
  // CWND.
  //
  Win   = 0;
  Limit = Tcb->SndWl2 + Tcb->SndWnd;

  if (TCP_SEQ_GT (Limit, Tcb->SndUna + Tcb->CWnd)) {

    Limit = Tcb->SndUna + Tcb->CWnd;
  }

  if (TCP_SEQ_GT (Limit, Tcb->SndNxt)) {
    Win = TCP_SUB_SEQ (Limit, Tcb->SndNxt);
  }

  //
  // The data to send contains two parts: the data on the
  // socket send queue, and the data on the TCB's send
  // buffer. The later can be non-zero if the peer shrinks
  // its advertised window.
  //
  Left  = GET_SND_DATASIZE (Sk) +
          TCP_SUB_SEQ (TcpGetMaxSndNxt (Tcb), Tcb->SndNxt);

  Len   = MIN (Win, Left);

  if (Len > Tcb->SndMss) {
    Len = Tcb->SndMss;
  }

  if ((Force != 0)|| (Len == 0 && Left == 0)) {
    return Len;
  }

  if (Len == 0 && Left != 0) {
    goto SetPersistTimer;
  }

  //
  // Sender's SWS avoidance: Don't send a small segment unless
  // a)A full-sized segment can be sent,
  // b)at least one-half of the maximum sized windows that
  // the other end has ever advertised.
  // c)It can send everything it has and either it isn't
  // expecting an ACK or the Nagle algorithm is disabled.
  //
  if ((Len == Tcb->SndMss) || (2 * Len >= Tcb->SndWndMax)) {

    return Len;
  }

  if ((Len == Left) &&
      ((Tcb->SndNxt == Tcb->SndUna) ||
      TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_NAGLE))) {

    return Len;
  }

  //
  // RFC1122 suggests to set a timer when SWSA forbids TCP
  // sending more data, and combine it with probe timer.
  //
SetPersistTimer:
  if (!TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_REXMIT)) {

    DEBUG (
      (EFI_D_WARN,
      "TcpDataToSend: enter persistent state for TCB %p\n",
      Tcb)
      );

    if (!Tcb->ProbeTimerOn) {
      TcpSetProbeTimer (Tcb);
    }
  }

  return 0;
}


/**
  Build the TCP header of the TCP segment and transmit the segment by IP.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Nbuf    Pointer to the buffer containing the segment to be sent out.

  @retval 0       The segment is sent out successfully.
  @retval other   Error condition occurred.

**/
INTN
TcpTransmitSegment (
  IN OUT TCP_CB  *Tcb,
  IN     NET_BUF *Nbuf
  )
{
  UINT16    Len;
  TCP_HEAD  *Head;
  TCP_SEG   *Seg;
  BOOLEAN   Syn;
  UINT32    DataLen;

  ASSERT ((Nbuf != NULL) && (Nbuf->Tcp == NULL) && (TcpVerifySegment (Nbuf) != 0));

  DataLen = Nbuf->TotalSize;

  Seg     = TCPSEG_NETBUF (Nbuf);
  Syn     = TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN);

  if (Syn) {

    Len = TcpSynBuildOption (Tcb, Nbuf);
  } else {

    Len = TcpBuildOption (Tcb, Nbuf);
  }

  ASSERT ((Len % 4 == 0) && (Len <= 40));

  Len += sizeof (TCP_HEAD);

  Head = (TCP_HEAD *) NetbufAllocSpace (
                        Nbuf,
                        sizeof (TCP_HEAD),
                        NET_BUF_HEAD
                        );

  ASSERT (Head != NULL);

  Nbuf->Tcp       = Head;

  Head->SrcPort   = Tcb->LocalEnd.Port;
  Head->DstPort   = Tcb->RemoteEnd.Port;
  Head->Seq       = NTOHL (Seg->Seq);
  Head->Ack       = NTOHL (Tcb->RcvNxt);
  Head->HeadLen   = (UINT8) (Len >> 2);
  Head->Res       = 0;
  Head->Wnd       = TcpComputeWnd (Tcb, Syn);
  Head->Checksum  = 0;

  //
  // Check whether to set the PSH flag.
  //
  TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_PSH);

  if (DataLen != 0) {
    if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_PSH) &&
        TCP_SEQ_BETWEEN (Seg->Seq, Tcb->SndPsh, Seg->End)) {

      TCP_SET_FLG (Seg->Flag, TCP_FLG_PSH);
      TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_PSH);

    } else if ((Seg->End == Tcb->SndNxt) &&
               (GET_SND_DATASIZE (Tcb->Sk) == 0)) {

      TCP_SET_FLG (Seg->Flag, TCP_FLG_PSH);
    }
  }

  //
  // Check whether to set the URG flag and the urgent pointer.
  //
  TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_URG);

  if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) &&
      TCP_SEQ_LEQ (Seg->Seq, Tcb->SndUp)) {

    TCP_SET_FLG (Seg->Flag, TCP_FLG_URG);

    if (TCP_SEQ_LT (Tcb->SndUp, Seg->End)) {

      Seg->Urg = (UINT16) TCP_SUB_SEQ (Tcb->SndUp, Seg->Seq);
    } else {

      Seg->Urg = (UINT16) MIN (
                            TCP_SUB_SEQ (Tcb->SndUp,
                            Seg->Seq),
                            0xffff
                            );
    }
  }

  Head->Flag      = Seg->Flag;
  Head->Urg       = NTOHS (Seg->Urg);
  Head->Checksum  = TcpChecksum (Nbuf, Tcb->HeadSum);

  //
  // update the TCP session's control information
  //
  Tcb->RcvWl2 = Tcb->RcvNxt;
  if (Syn) {
    Tcb->RcvWnd = NTOHS (Head->Wnd);
  }

  //
  // clear delayedack flag
  //
  Tcb->DelayedAck = 0;

  return TcpSendIpPacket (Tcb, Nbuf, Tcb->LocalEnd.Ip, Tcb->RemoteEnd.Ip);
}


/**
  Get a segment from the Tcb's SndQue.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Seq     The sequence number of the segment.
  @param  Len     The maximum length of the segment.

  @return Pointer to the segment, if NULL some error occurred.

**/
NET_BUF *
TcpGetSegmentSndQue (
  IN TCP_CB    *Tcb,
  IN TCP_SEQNO Seq,
  IN UINT32    Len
  )
{
  LIST_ENTRY      *Head;
  LIST_ENTRY      *Cur;
  NET_BUF         *Node;
  TCP_SEG         *Seg;
  NET_BUF         *Nbuf;
  TCP_SEQNO       End;
  UINT8           *Data;
  UINT8           Flag;
  INT32           Offset;
  INT32           CopyLen;

  ASSERT ((Tcb != NULL) && TCP_SEQ_LEQ (Seq, Tcb->SndNxt) && (Len > 0));

  //
  // Find the segment that contains the Seq.
  //
  Head  = &Tcb->SndQue;

  Node  = NULL;
  Seg   = NULL;

  NET_LIST_FOR_EACH (Cur, Head) {
    Node  = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
    Seg   = TCPSEG_NETBUF (Node);

    if (TCP_SEQ_LT (Seq, Seg->End) && TCP_SEQ_LEQ (Seg->Seq, Seq)) {

      break;
    }
  }

  ASSERT (Cur  != Head);
  ASSERT (Node != NULL);
  ASSERT (Seg  != NULL);

  //
  // Return the buffer if it can be returned without
  // adjustment:
  //
  if ((Seg->Seq == Seq) &&
      TCP_SEQ_LEQ (Seg->End, Seg->Seq + Len) &&
      !NET_BUF_SHARED (Node)) {

    NET_GET_REF (Node);
    return Node;
  }

  //
  // Create a new buffer and copy data there.
  //
  Nbuf = NetbufAlloc (Len + TCP_MAX_HEAD);

  if (Nbuf == NULL) {
    return NULL;
  }

  NetbufReserve (Nbuf, TCP_MAX_HEAD);

  Flag  = Seg->Flag;
  End   = Seg->End;

  if (TCP_SEQ_LT (Seq + Len, Seg->End)) {
    End = Seq + Len;
  }

  CopyLen = TCP_SUB_SEQ (End, Seq);
  Offset  = TCP_SUB_SEQ (Seq, Seg->Seq);

  //
  // If SYN is set and out of the range, clear the flag.
  // Becuase the sequence of the first byte is SEG.SEQ+1,
  // adjust Offset by -1. If SYN is in the range, copy
  // one byte less.
  //
  if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {

    if (TCP_SEQ_LT (Seg->Seq, Seq)) {

      TCP_CLEAR_FLG (Flag, TCP_FLG_SYN);
      Offset--;
    } else {

      CopyLen--;
    }
  }

  //
  // If FIN is set and in the range, copy one byte less,
  // and if it is out of the range, clear the flag.
  //
  if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {

    if (Seg->End == End) {

      CopyLen--;
    } else {

      TCP_CLEAR_FLG (Flag, TCP_FLG_FIN);
    }
  }

  ASSERT (CopyLen >= 0);

  //
  // copy data to the segment
  //
  if (CopyLen != 0) {
    Data = NetbufAllocSpace (Nbuf, CopyLen, NET_BUF_TAIL);
    ASSERT (Data != NULL);

    if ((INT32) NetbufCopy (Node, Offset, CopyLen, Data) != CopyLen) {
      goto OnError;
    }
  }

  CopyMem (TCPSEG_NETBUF (Nbuf), Seg, sizeof (TCP_SEG));

  TCPSEG_NETBUF (Nbuf)->Seq   = Seq;
  TCPSEG_NETBUF (Nbuf)->End   = End;
  TCPSEG_NETBUF (Nbuf)->Flag  = Flag;

  return Nbuf;

OnError:
  NetbufFree (Nbuf);
  return NULL;
}


/**
  Get a segment from the Tcb's socket buffer.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Seq     The sequence number of the segment.
  @param  Len     The maximum length of the segment.

  @return Pointer to the segment, if NULL some error occurred.

**/
NET_BUF *
TcpGetSegmentSock (
  IN TCP_CB    *Tcb,
  IN TCP_SEQNO Seq,
  IN UINT32    Len
  )
{
  NET_BUF *Nbuf;
  UINT8   *Data;
  UINT32  DataGet;

  ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL));

  Nbuf = NetbufAlloc (Len + TCP_MAX_HEAD);

  if (Nbuf == NULL) {
    DEBUG ((EFI_D_ERROR, "TcpGetSegmentSock: failed to allocate "
      "a netbuf for TCB %p\n",Tcb));

    return NULL;
  }

  NetbufReserve (Nbuf, TCP_MAX_HEAD);

  DataGet = 0;

  if (Len != 0) {
    //
    // copy data to the segment.
    //
    Data = NetbufAllocSpace (Nbuf, Len, NET_BUF_TAIL);
    ASSERT (Data != NULL);

    DataGet = SockGetDataToSend (Tcb->Sk, 0, Len, Data);
  }

  NET_GET_REF (Nbuf);

  TCPSEG_NETBUF (Nbuf)->Seq = Seq;
  TCPSEG_NETBUF (Nbuf)->End = Seq + Len;

  InsertTailList (&(Tcb->SndQue), &(Nbuf->List));

  if (DataGet != 0) {

    SockDataSent (Tcb->Sk, DataGet);
  }

  return Nbuf;
}


/**
  Get a segment starting from sequence Seq of a maximum
  length of Len.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Seq     The sequence number of the segment.
  @param  Len     The maximum length of the segment.

  @return Pointer to the segment, if NULL some error occurred.

**/
NET_BUF *
TcpGetSegment (
  IN TCP_CB    *Tcb,
  IN TCP_SEQNO Seq,
  IN UINT32    Len
  )
{
  NET_BUF *Nbuf;

  ASSERT (Tcb != NULL);

  //
  // Compare the SndNxt with the max sequence number sent.
  //
  if ((Len != 0) && TCP_SEQ_LT (Seq, TcpGetMaxSndNxt (Tcb))) {

    Nbuf = TcpGetSegmentSndQue (Tcb, Seq, Len);
  } else {

    Nbuf = TcpGetSegmentSock (Tcb, Seq, Len);
  }

  ASSERT (TcpVerifySegment (Nbuf) != 0);
  return Nbuf;
}


/**
  Retransmit the segment from sequence Seq.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Seq     The sequence number of the segment to be retransmitted.

  @retval 0       Retransmission succeeded.
  @retval -1      Error condition occurred.

**/
INTN
TcpRetransmit (
  IN TCP_CB    *Tcb,
  IN TCP_SEQNO Seq
  )
{
  NET_BUF *Nbuf;
  UINT32  Len;

  //
  // Compute the maxium length of retransmission. It is
  // limited by three factors:
  // 1. Less than SndMss
  // 2. must in the current send window
  // 3. will not change the boundaries of queued segments.
  //
  if (TCP_SEQ_LT (Tcb->SndWl2 + Tcb->SndWnd, Seq)) {
    DEBUG ((EFI_D_WARN, "TcpRetransmit: retransmission cancelled "
      "because send window too small for TCB %p\n", Tcb));

    return 0;
  }

  Len   = TCP_SUB_SEQ (Tcb->SndWl2 + Tcb->SndWnd, Seq);
  Len   = MIN (Len, Tcb->SndMss);

  Nbuf  = TcpGetSegmentSndQue (Tcb, Seq, Len);
  if (Nbuf == NULL) {
    return -1;
  }

  ASSERT (TcpVerifySegment (Nbuf) != 0);

  if (TcpTransmitSegment (Tcb, Nbuf) != 0) {
    goto OnError;
  }

  //
  // The retransmitted buffer may be on the SndQue,
  // trim TCP head because all the buffer on SndQue
  // are headless.
  //
  ASSERT (Nbuf->Tcp != NULL);
  NetbufTrim (Nbuf, (Nbuf->Tcp->HeadLen << 2), NET_BUF_HEAD);
  Nbuf->Tcp = NULL;

  NetbufFree (Nbuf);
  return 0;

OnError:
  if (Nbuf != NULL) {
    NetbufFree (Nbuf);
  }

  return -1;
}


/**
  Check whether to send data/SYN/FIN and piggy back an ACK.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.
  @param  Force   Whether to ignore the sender's SWS avoidance algorithm and send
                  out data by force.

  @return The number of bytes sent.

**/
INTN
TcpToSendData (
  IN OUT TCP_CB *Tcb,
  IN     INTN Force
  )
{
  UINT32    Len;
  INTN      Sent;
  UINT8     Flag;
  NET_BUF   *Nbuf;
  TCP_SEG   *Seg;
  TCP_SEQNO Seq;
  TCP_SEQNO End;

  ASSERT ((Tcb != NULL) && (Tcb->Sk != NULL) && (Tcb->State != TCP_LISTEN));

  Sent = 0;

  if ((Tcb->State == TCP_CLOSED) ||
      TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT)) {

    return 0;
  }

SEND_AGAIN:
  //
  // compute how much data can be sent
  //
  Len   = TcpDataToSend (Tcb, Force);
  Seq   = Tcb->SndNxt;

  ASSERT ((Tcb->State) < (sizeof (mTcpOutFlag) / sizeof (mTcpOutFlag[0])));
  Flag  = mTcpOutFlag[Tcb->State];

  if ((Flag & TCP_FLG_SYN) != 0) {

    Seq = Tcb->Iss;
    Len = 0;
  }

  //
  // only send a segment without data if SYN or
  // FIN is set.
  //
  if ((Len == 0) && 
      ((Flag & (TCP_FLG_SYN | TCP_FLG_FIN)) == 0)) {
    return Sent;
  }

  Nbuf = TcpGetSegment (Tcb, Seq, Len);

  if (Nbuf == NULL) {
    DEBUG (
      (EFI_D_ERROR,
      "TcpToSendData: failed to get a segment for TCB %p\n",
      Tcb)
      );

    goto OnError;
  }

  Seg = TCPSEG_NETBUF (Nbuf);

  //
  // Set the TcpSeg in Nbuf.
  //
  Len = Nbuf->TotalSize;
  End = Seq + Len;
  if (TCP_FLG_ON (Flag, TCP_FLG_SYN)) {
    End++;
  }

  if ((Flag & TCP_FLG_FIN) != 0) {
    //
    // Send FIN if all data is sent, and FIN is
    // in the window
    //
    if ((TcpGetMaxSndNxt (Tcb) == Tcb->SndNxt) &&
        (GET_SND_DATASIZE (Tcb->Sk) == 0) &&
        TCP_SEQ_LT (End + 1, Tcb->SndWnd + Tcb->SndWl2)) {

      DEBUG (
	  	(EFI_D_INFO, 
	  	"TcpToSendData: send FIN "
        "to peer for TCB %p in state %s\n", 
        Tcb, 
        mTcpStateName[Tcb->State])
      );

      End++;
    } else {
      TCP_CLEAR_FLG (Flag, TCP_FLG_FIN);
    }
  }

  Seg->Seq  = Seq;
  Seg->End  = End;
  Seg->Flag = Flag;

  ASSERT (TcpVerifySegment (Nbuf) != 0);
  ASSERT (TcpCheckSndQue (&Tcb->SndQue) != 0);

  //
  // don't send an empty segment here.
  //
  if (Seg->End == Seg->Seq) {
    DEBUG ((EFI_D_WARN, "TcpToSendData: created a empty"
      " segment for TCB %p, free it now\n", Tcb));

    NetbufFree (Nbuf);
    return Sent;
  }

  if (TcpTransmitSegment (Tcb, Nbuf) != 0) {
    NetbufTrim (Nbuf, (Nbuf->Tcp->HeadLen << 2), NET_BUF_HEAD);
    Nbuf->Tcp = NULL;

    if ((Flag & TCP_FLG_FIN) != 0)  {
      TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT);
    }

    goto OnError;
  }

  Sent += TCP_SUB_SEQ (End, Seq);

  //
  // All the buffer in the SndQue is headless
  //
  ASSERT (Nbuf->Tcp != NULL);

  NetbufTrim (Nbuf, (Nbuf->Tcp->HeadLen << 2), NET_BUF_HEAD);
  Nbuf->Tcp = NULL;

  NetbufFree (Nbuf);

  //
  // update status in TCB
  //
  Tcb->DelayedAck = 0;

  if ((Flag & TCP_FLG_FIN) != 0) {
    TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_FIN_SENT);
  }

  if (TCP_SEQ_GT (End, Tcb->SndNxt)) {
    Tcb->SndNxt = End;
  }

  if (!TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_REXMIT)) {
    TcpSetTimer (Tcb, TCP_TIMER_REXMIT, Tcb->Rto);
  }

  //
  // Enable RTT measurement only if not in retransmit.
  // Karn's algorithm reqires not to update RTT when in loss.
  //
  if ((Tcb->CongestState == TCP_CONGEST_OPEN) &&
      !TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_RTT_ON)) {

    DEBUG ((EFI_D_INFO, "TcpToSendData: set RTT measure "
      "sequence %d for TCB %p\n", Seq, Tcb));

    TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RTT_ON);
    Tcb->RttSeq     = Seq;
    Tcb->RttMeasure = 0;
  }

  if (Len == Tcb->SndMss) {
    goto SEND_AGAIN;
  }

  return Sent;

OnError:
  if (Nbuf != NULL) {
    NetbufFree (Nbuf);
  }

  return Sent;
}


/**
  Send an ACK immediately.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.

**/
VOID
TcpSendAck (
  IN OUT TCP_CB *Tcb
  )
{
  NET_BUF *Nbuf;
  TCP_SEG *Seg;

  Nbuf = NetbufAlloc (TCP_MAX_HEAD);

  if (Nbuf == NULL) {
    return;
  }

  NetbufReserve (Nbuf, TCP_MAX_HEAD);

  Seg       = TCPSEG_NETBUF (Nbuf);
  Seg->Seq  = Tcb->SndNxt;
  Seg->End  = Tcb->SndNxt;
  Seg->Flag = TCP_FLG_ACK;

  if (TcpTransmitSegment (Tcb, Nbuf) == 0) {
    TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW);
    Tcb->DelayedAck = 0;
  }

  NetbufFree (Nbuf);
}


/**
  Send a zero probe segment. It can be used by keepalive and zero window probe.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.

  @retval 0       The zero probe segment was sent out successfully.
  @retval other   Error condition occurred.

**/
INTN
TcpSendZeroProbe (
  IN OUT TCP_CB *Tcb
  )
{
  NET_BUF *Nbuf;
  TCP_SEG *Seg;
  INTN     Result;

  Nbuf = NetbufAlloc (TCP_MAX_HEAD);

  if (Nbuf == NULL) {
    return -1;
  }

  NetbufReserve (Nbuf, TCP_MAX_HEAD);

  //
  // SndNxt-1 is out of window. The peer should respond
  // with an ACK.
  //
  Seg       = TCPSEG_NETBUF (Nbuf);
  Seg->Seq  = Tcb->SndNxt - 1;
  Seg->End  = Tcb->SndNxt - 1;
  Seg->Flag = TCP_FLG_ACK;

  Result    = TcpTransmitSegment (Tcb, Nbuf);
  NetbufFree (Nbuf);

  return Result;
}


/**
  Check whether to send an ACK or delayed ACK.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance.

**/
VOID
TcpToSendAck (
  IN OUT TCP_CB *Tcb
  )
{
  UINT32 TcpNow;

  TcpNow = TcpRcvWinNow (Tcb);
  //
  // Generally, TCP should send a delayed ACK unless:
  //   1. ACK at least every other FULL sized segment received,
  //   2. Packets received out of order
  //   3. Receiving window is open
  //
  if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_ACK_NOW) ||
      (Tcb->DelayedAck >= 1) ||
      (TcpNow > TcpRcvWinOld (Tcb))) {
    TcpSendAck (Tcb);
    return;
  }

  DEBUG ((EFI_D_INFO, "TcpToSendAck: scheduled a delayed"
    " ACK for TCB %p\n", Tcb));

  //
  // schedule a delayed ACK
  //
  Tcb->DelayedAck++;
}


/**
  Send a RESET segment in response to the segment received.

  @param  Tcb     Pointer to the TCP_CB of this TCP instance, may be NULL.
  @param  Head    TCP header of the segment that triggers the reset.
  @param  Len     Length of the segment that triggers the reset.
  @param  Local   Local IP address.
  @param  Remote  Remote peer's IP address.

  @retval 0       A reset is sent or no need to send it.
  @retval -1      No reset is sent.

**/
INTN
TcpSendReset (
  IN TCP_CB    *Tcb,
  IN TCP_HEAD  *Head,
  IN INT32     Len,
  IN UINT32    Local,
  IN UINT32    Remote
  )
{
  NET_BUF   *Nbuf;
  TCP_HEAD  *Nhead;
  UINT16    HeadSum;

  //
  // Don't respond to a Reset with reset
  //
  if ((Head->Flag & TCP_FLG_RST) != 0) {
    return 0;
  }

  Nbuf = NetbufAlloc (TCP_MAX_HEAD);

  if (Nbuf == NULL) {
    return -1;
  }

  Nhead = (TCP_HEAD *) NetbufAllocSpace (
                        Nbuf,
                        sizeof (TCP_HEAD),
                        NET_BUF_TAIL
                        );

  ASSERT (Nhead != NULL);

  Nbuf->Tcp   = Nhead;
  Nhead->Flag = TCP_FLG_RST;

  //
  // Derive Seq/ACK from the segment if no TCB
  // associated with it, otherwise from the Tcb
  //
  if (Tcb == NULL) {

    if (TCP_FLG_ON (Head->Flag, TCP_FLG_ACK)) {
      Nhead->Seq  = Head->Ack;
      Nhead->Ack  = 0;
    } else {
      Nhead->Seq = 0;
      TCP_SET_FLG (Nhead->Flag, TCP_FLG_ACK);
      Nhead->Ack = HTONL (NTOHL (Head->Seq) + Len);
    }
  } else {

    Nhead->Seq  = HTONL (Tcb->SndNxt);
    Nhead->Ack  = HTONL (Tcb->RcvNxt);
    TCP_SET_FLG (Nhead->Flag, TCP_FLG_ACK);
  }

  Nhead->SrcPort  = Head->DstPort;
  Nhead->DstPort  = Head->SrcPort;
  Nhead->HeadLen  = (UINT8) (sizeof (TCP_HEAD) >> 2);
  Nhead->Res      = 0;
  Nhead->Wnd      = HTONS (0xFFFF);
  Nhead->Checksum = 0;
  Nhead->Urg      = 0;

  HeadSum         = NetPseudoHeadChecksum (Local, Remote, 6, 0);
  Nhead->Checksum = TcpChecksum (Nbuf, HeadSum);

  TcpSendIpPacket (Tcb, Nbuf, Local, Remote);

  NetbufFree (Nbuf);
  return 0;
}


/**
  Verify that the segment is in good shape.

  @param  Nbuf    Buffer that contains the segment to be checked.

  @retval 0       The segment is broken.
  @retval 1       The segment is in good shape.

**/
INTN
TcpVerifySegment (
  IN NET_BUF *Nbuf
  )
{
  TCP_HEAD  *Head;
  TCP_SEG   *Seg;
  UINT32    Len;

  if (Nbuf == NULL) {
    return 1;
  }

  NET_CHECK_SIGNATURE (Nbuf, NET_BUF_SIGNATURE);

  Seg   = TCPSEG_NETBUF (Nbuf);
  Len   = Nbuf->TotalSize;
  Head  = Nbuf->Tcp;

  if (Head != NULL) {
    if (Head->Flag != Seg->Flag) {
      return 0;
    }

    Len -= (Head->HeadLen << 2);
  }

  if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
    Len++;
  }

  if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
    Len++;
  }

  if (Seg->Seq + Len != Seg->End) {
    return 0;
  }

  return 1;
}


/**
  Verify that all the segments in SndQue are in good shape.

  @param  Head    Pointer to the head node of the SndQue.

  @retval 0       At least one segment is broken.
  @retval 1       All segments in the specific queue are in good shape.

**/
INTN
TcpCheckSndQue (
  IN LIST_ENTRY     *Head
  )
{
  LIST_ENTRY      *Entry;
  NET_BUF         *Nbuf;
  TCP_SEQNO       Seq;

  if (IsListEmpty (Head)) {
    return 1;
  }
  //
  // Initialize the Seq
  //
  Entry = Head->ForwardLink;
  Nbuf  = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
  Seq   = TCPSEG_NETBUF (Nbuf)->Seq;

  NET_LIST_FOR_EACH (Entry, Head) {
    Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);

    if (TcpVerifySegment (Nbuf) == 0) {
      return 0;
    }

    //
    // All the node in the SndQue should has:
    // SEG.SEQ = LAST_SEG.END
    //
    if (Seq != TCPSEG_NETBUF (Nbuf)->Seq) {
      return 0;
    }

    Seq = TCPSEG_NETBUF (Nbuf)->End;
  }

  return 1;
}