]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Tcp4Dxe/Tcp4Misc.c
Use Mde library and definition instead of some native definitions in NetLib, to simpl...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Tcp4Dxe / Tcp4Misc.c
1 /** @file
2
3 Copyright (c) 2005 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Tcp4Misc.c
15
16 Abstract:
17
18 Misc support routines for tcp.
19
20
21 **/
22
23
24 #include "Tcp4Main.h"
25
26 #include <Library/DevicePathLib.h>
27
28 LIST_ENTRY mTcpRunQue = {
29 &mTcpRunQue,
30 &mTcpRunQue
31 };
32
33 LIST_ENTRY mTcpListenQue = {
34 &mTcpListenQue,
35 &mTcpListenQue
36 };
37
38 TCP_SEQNO mTcpGlobalIss = 0x4d7e980b;
39
40 CHAR16 *mTcpStateName[] = {
41 L"TCP_CLOSED",
42 L"TCP_LISTEN",
43 L"TCP_SYN_SENT",
44 L"TCP_SYN_RCVD",
45 L"TCP_ESTABLISHED",
46 L"TCP_FIN_WAIT_1",
47 L"TCP_FIN_WAIT_2",
48 L"TCP_CLOSING",
49 L"TCP_TIME_WAIT",
50 L"TCP_CLOSE_WAIT",
51 L"TCP_LAST_ACK"
52 };
53
54
55 /**
56 Initialize the Tcb local related members.
57
58 @param Tcb Pointer to the TCP_CB of this TCP instance.
59
60 @return None
61
62 **/
63 VOID
64 TcpInitTcbLocal (
65 IN TCP_CB *Tcb
66 )
67 {
68 //
69 // Compute the checksum of the fixed parts of pseudo header
70 //
71 Tcb->HeadSum = NetPseudoHeadChecksum (
72 Tcb->LocalEnd.Ip,
73 Tcb->RemoteEnd.Ip,
74 0x06,
75 0
76 );
77
78 Tcb->Iss = TcpGetIss ();
79 Tcb->SndUna = Tcb->Iss;
80 Tcb->SndNxt = Tcb->Iss;
81
82 Tcb->SndWl2 = Tcb->Iss;
83 Tcb->SndWnd = 536;
84
85 Tcb->RcvWnd = GET_RCV_BUFFSIZE (Tcb->Sk);
86
87 //
88 // Fisrt window size is never scaled
89 //
90 Tcb->RcvWndScale = 0;
91 }
92
93
94 /**
95 Initialize the peer related members.
96
97 @param Tcb Pointer to the TCP_CB of this TCP instance.
98 @param Seg Pointer to the segment that contains the peer's
99 intial info.
100 @param Opt Pointer to the options announced by the peer.
101
102 @return None
103
104 **/
105 VOID
106 TcpInitTcbPeer (
107 IN TCP_CB *Tcb,
108 IN TCP_SEG *Seg,
109 IN TCP_OPTION *Opt
110 )
111 {
112 UINT16 RcvMss;
113
114 ASSERT (Tcb && Seg && Opt);
115 ASSERT (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN));
116
117 Tcb->SndWnd = Seg->Wnd;
118 Tcb->SndWndMax = Tcb->SndWnd;
119 Tcb->SndWl1 = Seg->Seq;
120
121 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_ACK)) {
122 Tcb->SndWl2 = Seg->Ack;
123 } else {
124 Tcb->SndWl2 = Tcb->Iss + 1;
125 }
126
127 if (TCP_FLG_ON (Opt->Flag, TCP_OPTION_RCVD_MSS)) {
128 Tcb->SndMss = (UINT16) MAX (64, Opt->Mss);
129
130 RcvMss = TcpGetRcvMss (Tcb->Sk);
131 if (Tcb->SndMss > RcvMss) {
132 Tcb->SndMss = RcvMss;
133 }
134
135 } else {
136 //
137 // One end doesn't support MSS option, use default.
138 //
139 Tcb->RcvMss = 536;
140 }
141
142 Tcb->CWnd = Tcb->SndMss;
143
144 Tcb->Irs = Seg->Seq;
145 Tcb->RcvNxt = Tcb->Irs + 1;
146
147 Tcb->RcvWl2 = Tcb->RcvNxt;
148
149 if (TCP_FLG_ON (Opt->Flag, TCP_OPTION_RCVD_WS) &&
150 !TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_WS)) {
151
152 Tcb->SndWndScale = Opt->WndScale;
153
154 Tcb->RcvWndScale = TcpComputeScale (Tcb);
155 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_WS);
156
157 } else {
158 //
159 // One end doesn't support window scale option. use zero.
160 //
161 Tcb->RcvWndScale = 0;
162 }
163
164 if (TCP_FLG_ON (Opt->Flag, TCP_OPTION_RCVD_TS) &&
165 !TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_TS)) {
166
167 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_TS);
168 TCP_SET_FLG (Tcb->CtrlFlag, TCP_CTRL_RCVD_TS);
169
170 //
171 // Compute the effective SndMss per RFC1122
172 // section 4.2.2.6. If timestamp option is
173 // enabled, it will always occupy 12 bytes.
174 //
175 Tcb->SndMss -= TCP_OPTION_TS_ALIGNED_LEN;
176 }
177 }
178
179
180 /**
181 Locate a listen TCB that matchs the Local and Remote.
182
183 @param Local Pointer to the local (IP, Port).
184 @param Remote Pointer to the remote (IP, Port).
185
186 @return Pointer to the TCP_CB with the least number of wildcard, if NULL no match is found.
187
188 **/
189 STATIC
190 TCP_CB *
191 TcpLocateListenTcb (
192 IN TCP_PEER *Local,
193 IN TCP_PEER *Remote
194 )
195 {
196 LIST_ENTRY *Entry;
197 TCP_CB *Node;
198 TCP_CB *Match;
199 INTN Last;
200 INTN Cur;
201
202 Last = 4;
203 Match = NULL;
204
205 NET_LIST_FOR_EACH (Entry, &mTcpListenQue) {
206 Node = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
207
208 if ((Local->Port != Node->LocalEnd.Port) ||
209 !TCP_PEER_MATCH (Remote, &Node->RemoteEnd) ||
210 !TCP_PEER_MATCH (Local, &Node->LocalEnd)
211 ) {
212
213 continue;
214 }
215
216 //
217 // Compute the number of wildcard
218 //
219 Cur = 0;
220 if (Node->RemoteEnd.Ip == 0) {
221 Cur++;
222 }
223
224 if (Node->RemoteEnd.Port == 0) {
225 Cur++;
226 }
227
228 if (Node->LocalEnd.Ip == 0) {
229 Cur++;
230 }
231
232 if (Cur < Last) {
233 if (Cur == 0) {
234 return Node;
235 }
236
237 Last = Cur;
238 Match = Node;
239 }
240 }
241
242 return Match;
243 }
244
245
246 /**
247 Try to find one Tcb whose <Ip, Port> equals to <IN Addr, IN Port>.
248
249 @param Addr Pointer to the IP address needs to match.
250 @param Port The port number needs to match.
251
252 @return The Tcb which matches the <Addr Port> paire exists or not.
253
254 **/
255 BOOLEAN
256 TcpFindTcbByPeer (
257 IN EFI_IPv4_ADDRESS *Addr,
258 IN TCP_PORTNO Port
259 )
260 {
261 TCP_PORTNO LocalPort;
262 LIST_ENTRY *Entry;
263 TCP_CB *Tcb;
264
265 ASSERT ((Addr != NULL) && (Port != 0));
266
267 LocalPort = HTONS (Port);
268
269 NET_LIST_FOR_EACH (Entry, &mTcpListenQue) {
270 Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
271
272 if (EFI_IP4_EQUAL (Addr, &Tcb->LocalEnd.Ip) &&
273 (LocalPort == Tcb->LocalEnd.Port)) {
274
275 return TRUE;
276 }
277 }
278
279 NET_LIST_FOR_EACH (Entry, &mTcpRunQue) {
280 Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
281
282 if (EFI_IP4_EQUAL (Addr, &Tcb->LocalEnd.Ip) &&
283 (LocalPort == Tcb->LocalEnd.Port)) {
284
285 return TRUE;
286 }
287 }
288
289 return FALSE;
290 }
291
292
293 /**
294 Locate the TCP_CB related to the socket pair.
295
296 @param LocalPort The local port number.
297 @param LocalIp The local IP address.
298 @param RemotePort The remote port number.
299 @param RemoteIp The remote IP address.
300 @param Syn Whether to search the listen sockets, if TRUE, the
301 listen sockets are searched.
302
303 @return Pointer to the related TCP_CB, if NULL no match is found.
304
305 **/
306 TCP_CB *
307 TcpLocateTcb (
308 IN TCP_PORTNO LocalPort,
309 IN UINT32 LocalIp,
310 IN TCP_PORTNO RemotePort,
311 IN UINT32 RemoteIp,
312 IN BOOLEAN Syn
313 )
314 {
315 TCP_PEER Local;
316 TCP_PEER Remote;
317 LIST_ENTRY *Entry;
318 TCP_CB *Tcb;
319
320 Local.Port = LocalPort;
321 Local.Ip = LocalIp;
322
323 Remote.Port = RemotePort;
324 Remote.Ip = RemoteIp;
325
326 //
327 // First check for exact match.
328 //
329 NET_LIST_FOR_EACH (Entry, &mTcpRunQue) {
330 Tcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
331
332 if (TCP_PEER_EQUAL (&Remote, &Tcb->RemoteEnd) &&
333 TCP_PEER_EQUAL (&Local, &Tcb->LocalEnd)) {
334
335 RemoveEntryList (&Tcb->List);
336 InsertHeadList (&mTcpRunQue, &Tcb->List);
337
338 return Tcb;
339 }
340 }
341
342 //
343 // Only check listen queue when SYN flag is on
344 //
345 if (Syn) {
346 return TcpLocateListenTcb (&Local, &Remote);
347 }
348
349 return NULL;
350 }
351
352
353 /**
354 Insert a Tcb into the proper queue.
355
356 @param Tcb Pointer to the TCP_CB to be inserted.
357
358 @retval 0 The Tcb is inserted successfully.
359 @retval -1 Error condition occurred.
360
361 **/
362 INTN
363 TcpInsertTcb (
364 IN TCP_CB *Tcb
365 )
366 {
367 LIST_ENTRY *Entry;
368 LIST_ENTRY *Head;
369 TCP_CB *Node;
370 TCP4_PROTO_DATA *TcpProto;
371
372 ASSERT (
373 Tcb &&
374 (
375 (Tcb->State == TCP_LISTEN) ||
376 (Tcb->State == TCP_SYN_SENT) ||
377 (Tcb->State == TCP_SYN_RCVD) ||
378 (Tcb->State == TCP_CLOSED)
379 )
380 );
381
382 if (Tcb->LocalEnd.Port == 0) {
383 return -1;
384 }
385
386 Head = &mTcpRunQue;
387
388 if (Tcb->State == TCP_LISTEN) {
389 Head = &mTcpListenQue;
390 }
391
392 //
393 // Check that Tcb isn't already on the list.
394 //
395 NET_LIST_FOR_EACH (Entry, Head) {
396 Node = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
397
398 if (TCP_PEER_EQUAL (&Tcb->LocalEnd, &Node->LocalEnd) &&
399 TCP_PEER_EQUAL (&Tcb->RemoteEnd, &Node->RemoteEnd)) {
400
401 return -1;
402 }
403 }
404
405 InsertHeadList (Head, &Tcb->List);
406
407 TcpProto = (TCP4_PROTO_DATA *) Tcb->Sk->ProtoReserved;
408 TcpSetVariableData (TcpProto->TcpService);
409
410 return 0;
411 }
412
413
414 /**
415 Clone a TCP_CB from Tcb.
416
417 @param Tcb Pointer to the TCP_CB to be cloned.
418
419 @return Pointer to the new cloned TCP_CB, if NULL error condition occurred.
420
421 **/
422 TCP_CB *
423 TcpCloneTcb (
424 IN TCP_CB *Tcb
425 )
426 {
427 TCP_CB *Clone;
428
429 Clone = AllocatePool (sizeof (TCP_CB));
430
431 if (Clone == NULL) {
432 return NULL;
433
434 }
435
436 CopyMem (Clone, Tcb, sizeof (TCP_CB));
437
438 //
439 // Increate the reference count of the shared IpInfo.
440 //
441 NET_GET_REF (Tcb->IpInfo);
442
443 InitializeListHead (&Clone->List);
444 InitializeListHead (&Clone->SndQue);
445 InitializeListHead (&Clone->RcvQue);
446
447 Clone->Sk = SockClone (Tcb->Sk);
448 if (Clone->Sk == NULL) {
449 DEBUG ((EFI_D_ERROR, "TcpCloneTcb: failed to clone a sock\n"));
450 gBS->FreePool (Clone);
451 return NULL;
452 }
453
454 ((TCP4_PROTO_DATA *) (Clone->Sk->ProtoReserved))->TcpPcb = Clone;
455
456 return Clone;
457 }
458
459
460 /**
461 Compute an ISS to be used by a new connection.
462
463 None
464
465 @return The result ISS.
466
467 **/
468 TCP_SEQNO
469 TcpGetIss (
470 VOID
471 )
472 {
473 mTcpGlobalIss += 2048;
474 return mTcpGlobalIss;
475 }
476
477
478 /**
479 Get the local mss.
480
481 None
482
483 @return The mss size.
484
485 **/
486 UINT16
487 TcpGetRcvMss (
488 IN SOCKET *Sock
489 )
490 {
491 EFI_SIMPLE_NETWORK_MODE SnpMode;
492 TCP4_PROTO_DATA *TcpProto;
493 EFI_IP4_PROTOCOL *Ip;
494
495 ASSERT (Sock);
496
497 TcpProto = (TCP4_PROTO_DATA *) Sock->ProtoReserved;
498 Ip = TcpProto->TcpService->IpIo->Ip;
499 ASSERT (Ip);
500
501 Ip->GetModeData (Ip, NULL, NULL, &SnpMode);
502
503 return (UINT16) (SnpMode.MaxPacketSize - 40);
504 }
505
506
507 /**
508 Set the Tcb's state.
509
510 @param Tcb Pointer to the TCP_CB of this TCP instance.
511 @param State The state to be set.
512
513 @return None
514
515 **/
516 VOID
517 TcpSetState (
518 IN TCP_CB *Tcb,
519 IN UINT8 State
520 )
521 {
522 DEBUG (
523 (EFI_D_INFO,
524 "Tcb (%x) state %s --> %s\n",
525 Tcb,
526 mTcpStateName[Tcb->State],
527 mTcpStateName[State])
528 );
529
530 Tcb->State = State;
531
532 switch (State) {
533 case TCP_ESTABLISHED:
534
535 SockConnEstablished (Tcb->Sk);
536
537 if (Tcb->Parent != NULL) {
538 //
539 // A new connection is accepted by a listening socket, install
540 // the device path.
541 //
542 TcpInstallDevicePath (Tcb->Sk);
543 }
544
545 break;
546
547 case TCP_CLOSED:
548
549 SockConnClosed (Tcb->Sk);
550
551 break;
552 }
553 }
554
555
556 /**
557 Compute the TCP segment's checksum.
558
559 @param Nbuf Pointer to the buffer that contains the TCP
560 segment.
561 @param HeadSum The checksum value of the fixed part of pseudo
562 header.
563
564 @return The checksum value.
565
566 **/
567 UINT16
568 TcpChecksum (
569 IN NET_BUF *Nbuf,
570 IN UINT16 HeadSum
571 )
572 {
573 UINT16 Checksum;
574
575 Checksum = NetbufChecksum (Nbuf);
576 Checksum = NetAddChecksum (Checksum, HeadSum);
577
578 Checksum = NetAddChecksum (
579 Checksum,
580 HTONS ((UINT16) Nbuf->TotalSize)
581 );
582
583 return (UINT16) ~Checksum;
584 }
585
586
587 /**
588 Translate the information from the head of the received TCP
589 segment Nbuf contains and fill it into a TCP_SEG structure.
590
591 @param Tcb Pointer to the TCP_CB of this TCP instance.
592 @param Nbuf Pointer to the buffer contains the TCP segment.
593
594 @return Pointer to the TCP_SEG that contains the translated TCP head information.
595
596 **/
597 TCP_SEG *
598 TcpFormatNetbuf (
599 IN TCP_CB *Tcb,
600 IN NET_BUF *Nbuf
601 )
602 {
603 TCP_SEG *Seg;
604 TCP_HEAD *Head;
605
606 Seg = TCPSEG_NETBUF (Nbuf);
607 Head = (TCP_HEAD *) NetbufGetByte (Nbuf, 0, NULL);
608 Nbuf->Tcp = Head;
609
610 Seg->Seq = NTOHL (Head->Seq);
611 Seg->Ack = NTOHL (Head->Ack);
612 Seg->End = Seg->Seq + (Nbuf->TotalSize - (Head->HeadLen << 2));
613
614 Seg->Urg = NTOHS (Head->Urg);
615 Seg->Wnd = (NTOHS (Head->Wnd) << Tcb->SndWndScale);
616 Seg->Flag = Head->Flag;
617
618 //
619 // SYN and FIN flag occupy one sequence space each.
620 //
621 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
622 //
623 // RFC requires that initial window not be scaled
624 //
625 Seg->Wnd = NTOHS (Head->Wnd);
626 Seg->End++;
627 }
628
629 if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
630 Seg->End++;
631 }
632
633 return Seg;
634 }
635
636
637 /**
638 Reset the connection related with Tcb.
639
640 @param Tcb Pointer to the TCP_CB of the connection to be
641 reset.
642
643 @return None
644
645 **/
646 VOID
647 TcpResetConnection (
648 IN TCP_CB *Tcb
649 )
650 {
651 NET_BUF *Nbuf;
652 TCP_HEAD *Nhead;
653
654 Nbuf = NetbufAlloc (TCP_MAX_HEAD);
655
656 if (Nbuf == NULL) {
657 return ;
658 }
659
660 Nhead = (TCP_HEAD *) NetbufAllocSpace (
661 Nbuf,
662 sizeof (TCP_HEAD),
663 NET_BUF_TAIL
664 );
665
666 ASSERT (Nhead != NULL);
667
668 Nbuf->Tcp = Nhead;
669
670 Nhead->Flag = TCP_FLG_RST;
671 Nhead->Seq = HTONL (Tcb->SndNxt);
672 Nhead->Ack = HTONL (Tcb->RcvNxt);
673 Nhead->SrcPort = Tcb->LocalEnd.Port;
674 Nhead->DstPort = Tcb->RemoteEnd.Port;
675 Nhead->HeadLen = (sizeof (TCP_HEAD) >> 2);
676 Nhead->Res = 0;
677 Nhead->Wnd = HTONS (0xFFFF);
678 Nhead->Checksum = 0;
679 Nhead->Urg = 0;
680 Nhead->Checksum = TcpChecksum (Nbuf, Tcb->HeadSum);
681
682 TcpSendIpPacket (Tcb, Nbuf, Tcb->LocalEnd.Ip, Tcb->RemoteEnd.Ip);
683
684 NetbufFree (Nbuf);
685 }
686
687
688 /**
689 Initialize an active connection,
690
691 @param Tcb Pointer to the TCP_CB that wants to initiate a
692 connection.
693
694 @return None
695
696 **/
697 VOID
698 TcpOnAppConnect (
699 IN TCP_CB *Tcb
700 )
701 {
702 TcpInitTcbLocal (Tcb);
703 TcpSetState (Tcb, TCP_SYN_SENT);
704
705 TcpSetTimer (Tcb, TCP_TIMER_CONNECT, Tcb->ConnectTimeout);
706 TcpToSendData (Tcb, 1);
707 }
708
709
710 /**
711 Initiate the connection close procedure, called when
712 applications want to close the connection.
713
714 @param Tcb Pointer to the TCP_CB of this TCP instance.
715
716 @return None.
717
718 **/
719 VOID
720 TcpOnAppClose (
721 IN TCP_CB *Tcb
722 )
723 {
724 ASSERT (Tcb);
725
726 if (!IsListEmpty (&Tcb->RcvQue) || GET_RCV_DATASIZE (Tcb->Sk)) {
727
728 DEBUG ((EFI_D_WARN, "TcpOnAppClose: connection reset "
729 "because data is lost for TCB %x\n", Tcb));
730
731 TcpResetConnection (Tcb);
732 TcpClose (Tcb);
733 return;
734 }
735
736 switch (Tcb->State) {
737 case TCP_CLOSED:
738 case TCP_LISTEN:
739 case TCP_SYN_SENT:
740 TcpSetState (Tcb, TCP_CLOSED);
741 break;
742
743 case TCP_SYN_RCVD:
744 case TCP_ESTABLISHED:
745 TcpSetState (Tcb, TCP_FIN_WAIT_1);
746 break;
747
748 case TCP_CLOSE_WAIT:
749 TcpSetState (Tcb, TCP_LAST_ACK);
750 break;
751 }
752
753 TcpToSendData (Tcb, 1);
754 }
755
756
757 /**
758 Check whether the application's newly delivered data
759 can be sent out.
760
761 @param Tcb Pointer to the TCP_CB of this TCP instance.
762
763 @retval 0 Whether the data is sent out or is buffered for
764 further sending.
765 @retval -1 The Tcb is not in a state that data is permitted to
766 be sent out.
767
768 **/
769 INTN
770 TcpOnAppSend (
771 IN TCP_CB *Tcb
772 )
773 {
774
775 switch (Tcb->State) {
776 case TCP_CLOSED:
777 return -1;
778 break;
779
780 case TCP_LISTEN:
781 return -1;
782 break;
783
784 case TCP_SYN_SENT:
785 case TCP_SYN_RCVD:
786 return 0;
787 break;
788
789 case TCP_ESTABLISHED:
790 case TCP_CLOSE_WAIT:
791 TcpToSendData (Tcb, 0);
792 return 0;
793 break;
794
795 case TCP_FIN_WAIT_1:
796 case TCP_FIN_WAIT_2:
797 case TCP_CLOSING:
798 case TCP_LAST_ACK:
799 case TCP_TIME_WAIT:
800 return -1;
801 break;
802 }
803
804 return 0;
805 }
806
807
808 /**
809 Application has consumed some data, check whether
810 to send a window updata ack or a delayed ack.
811
812 @param Tcb Pointer to the TCP_CB of this TCP instance.
813
814
815 **/
816 INTN
817 TcpOnAppConsume (
818 IN TCP_CB *Tcb
819 )
820 {
821 UINT32 TcpOld;
822
823 switch (Tcb->State) {
824 case TCP_CLOSED:
825 return -1;
826 break;
827
828 case TCP_LISTEN:
829 return -1;
830 break;
831
832 case TCP_SYN_SENT:
833 case TCP_SYN_RCVD:
834 return 0;
835 break;
836
837 case TCP_ESTABLISHED:
838 TcpOld = TcpRcvWinOld (Tcb);
839 if (TcpRcvWinNow (Tcb) > TcpOld) {
840
841 if (TcpOld < Tcb->RcvMss) {
842
843 DEBUG ((EFI_D_INFO, "TcpOnAppConsume: send a window"
844 " update for a window closed Tcb(%x)\n", Tcb));
845
846 TcpSendAck (Tcb);
847 } else if (Tcb->DelayedAck == 0) {
848
849 DEBUG ((EFI_D_INFO, "TcpOnAppConsume: scheduled a delayed"
850 " ACK to update window for Tcb(%x)\n", Tcb));
851
852 Tcb->DelayedAck = 1;
853 }
854 }
855
856 break;
857
858 case TCP_CLOSE_WAIT:
859 return 0;
860 break;
861
862 case TCP_FIN_WAIT_1:
863 case TCP_FIN_WAIT_2:
864 case TCP_CLOSING:
865 case TCP_LAST_ACK:
866 case TCP_TIME_WAIT:
867 return -1;
868 break;
869 }
870
871 return -1;
872 }
873
874
875 /**
876 Abort the connection by sending a reset segment, called
877 when the application wants to abort the connection.
878
879 @param Tcb Pointer to the TCP_CB of the TCP instance.
880
881 @return None.
882
883 **/
884 VOID
885 TcpOnAppAbort (
886 IN TCP_CB *Tcb
887 )
888 {
889 DEBUG ((EFI_D_WARN, "TcpOnAppAbort: connection reset "
890 "issued by application for TCB %x\n", Tcb));
891
892 switch (Tcb->State) {
893 case TCP_SYN_RCVD:
894 case TCP_ESTABLISHED:
895 case TCP_FIN_WAIT_1:
896 case TCP_FIN_WAIT_2:
897 case TCP_CLOSE_WAIT:
898 TcpResetConnection (Tcb);
899 break;
900 }
901
902 TcpSetState (Tcb, TCP_CLOSED);
903 }
904
905
906 /**
907 Set the Tdp4 variable data.
908
909 @param Tcp4Service Tcp4 service data.
910
911 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the variable.
912 @retval other Set variable failed.
913
914 **/
915 EFI_STATUS
916 TcpSetVariableData (
917 IN TCP4_SERVICE_DATA *Tcp4Service
918 )
919 {
920 UINT32 NumConfiguredInstance;
921 LIST_ENTRY *Entry;
922 TCP_CB *TcpPcb;
923 TCP4_PROTO_DATA *TcpProto;
924 UINTN VariableDataSize;
925 EFI_TCP4_VARIABLE_DATA *Tcp4VariableData;
926 EFI_TCP4_SERVICE_POINT *Tcp4ServicePoint;
927 CHAR16 *NewMacString;
928 EFI_STATUS Status;
929
930 NumConfiguredInstance = 0;
931
932 //
933 // Go through the running queue to count the instances.
934 //
935 NET_LIST_FOR_EACH (Entry, &mTcpRunQue) {
936 TcpPcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
937
938 TcpProto = (TCP4_PROTO_DATA *) TcpPcb->Sk->ProtoReserved;
939
940 if (TcpProto->TcpService == Tcp4Service) {
941 //
942 // This tcp instance belongs to the Tcp4Service.
943 //
944 NumConfiguredInstance++;
945 }
946 }
947
948 //
949 // Go through the listening queue to count the instances.
950 //
951 NET_LIST_FOR_EACH (Entry, &mTcpListenQue) {
952 TcpPcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
953
954 TcpProto = (TCP4_PROTO_DATA *) TcpPcb->Sk->ProtoReserved;
955
956 if (TcpProto->TcpService == Tcp4Service) {
957 //
958 // This tcp instance belongs to the Tcp4Service.
959 //
960 NumConfiguredInstance++;
961 }
962 }
963
964 //
965 // Calculate the size of the Tcp4VariableData. As there may be no Tcp4 child,
966 // we should add extra buffer for the service points only if the number of configured
967 // children is more than 1.
968 //
969 VariableDataSize = sizeof (EFI_TCP4_VARIABLE_DATA);
970
971 if (NumConfiguredInstance > 1) {
972 VariableDataSize += sizeof (EFI_TCP4_SERVICE_POINT) * (NumConfiguredInstance - 1);
973 }
974
975 Tcp4VariableData = AllocatePool (VariableDataSize);
976 if (Tcp4VariableData == NULL) {
977 return EFI_OUT_OF_RESOURCES;
978 }
979
980 Tcp4VariableData->DriverHandle = Tcp4Service->DriverBindingHandle;
981 Tcp4VariableData->ServiceCount = NumConfiguredInstance;
982
983 Tcp4ServicePoint = &Tcp4VariableData->Services[0];
984
985 //
986 // Go through the running queue to fill the service points.
987 //
988 NET_LIST_FOR_EACH (Entry, &mTcpRunQue) {
989 TcpPcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
990
991 TcpProto = (TCP4_PROTO_DATA *) TcpPcb->Sk->ProtoReserved;
992
993 if (TcpProto->TcpService == Tcp4Service) {
994 //
995 // This tcp instance belongs to the Tcp4Service.
996 //
997 Tcp4ServicePoint->InstanceHandle = TcpPcb->Sk->SockHandle;
998 CopyMem (&Tcp4ServicePoint->LocalAddress, &TcpPcb->LocalEnd.Ip, sizeof (EFI_IPv4_ADDRESS));
999 Tcp4ServicePoint->LocalPort = NTOHS (TcpPcb->LocalEnd.Port);
1000 CopyMem (&Tcp4ServicePoint->RemoteAddress, &TcpPcb->RemoteEnd.Ip, sizeof (EFI_IPv4_ADDRESS));
1001 Tcp4ServicePoint->RemotePort = NTOHS (TcpPcb->RemoteEnd.Port);
1002
1003 Tcp4ServicePoint++;
1004 }
1005 }
1006
1007 //
1008 // Go through the listening queue to fill the service points.
1009 //
1010 NET_LIST_FOR_EACH (Entry, &mTcpListenQue) {
1011 TcpPcb = NET_LIST_USER_STRUCT (Entry, TCP_CB, List);
1012
1013 TcpProto = (TCP4_PROTO_DATA *) TcpPcb->Sk->ProtoReserved;
1014
1015 if (TcpProto->TcpService == Tcp4Service) {
1016 //
1017 // This tcp instance belongs to the Tcp4Service.
1018 //
1019 Tcp4ServicePoint->InstanceHandle = TcpPcb->Sk->SockHandle;
1020 CopyMem (&Tcp4ServicePoint->LocalAddress, &TcpPcb->LocalEnd.Ip, sizeof (EFI_IPv4_ADDRESS));
1021 Tcp4ServicePoint->LocalPort = NTOHS (TcpPcb->LocalEnd.Port);
1022 CopyMem (&Tcp4ServicePoint->RemoteAddress, &TcpPcb->RemoteEnd.Ip, sizeof (EFI_IPv4_ADDRESS));
1023 Tcp4ServicePoint->RemotePort = NTOHS (TcpPcb->RemoteEnd.Port);
1024
1025 Tcp4ServicePoint++;
1026 }
1027 }
1028
1029 //
1030 // Get the mac string.
1031 //
1032 Status = NetLibGetMacString (
1033 Tcp4Service->ControllerHandle,
1034 Tcp4Service->DriverBindingHandle,
1035 &NewMacString
1036 );
1037 if (EFI_ERROR (Status)) {
1038 goto ON_ERROR;
1039 }
1040
1041 if (Tcp4Service->MacString != NULL) {
1042 //
1043 // The variable is set already, we're going to update it.
1044 //
1045 if (StrCmp (Tcp4Service->MacString, NewMacString) != 0) {
1046 //
1047 // The mac address is changed, delete the previous variable first.
1048 //
1049 gRT->SetVariable (
1050 Tcp4Service->MacString,
1051 &gEfiTcp4ServiceBindingProtocolGuid,
1052 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1053 0,
1054 NULL
1055 );
1056 }
1057
1058 gBS->FreePool (Tcp4Service->MacString);
1059 }
1060
1061 Tcp4Service->MacString = NewMacString;
1062
1063 Status = gRT->SetVariable (
1064 Tcp4Service->MacString,
1065 &gEfiTcp4ServiceBindingProtocolGuid,
1066 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1067 VariableDataSize,
1068 (VOID *) Tcp4VariableData
1069 );
1070
1071 ON_ERROR:
1072
1073 gBS->FreePool (Tcp4VariableData);
1074
1075 return Status;
1076 }
1077
1078
1079 /**
1080 Clear the variable and free the resource.
1081
1082 @param Tcp4Service Tcp4 service data.
1083
1084 @return None.
1085
1086 **/
1087 VOID
1088 TcpClearVariableData (
1089 IN TCP4_SERVICE_DATA *Tcp4Service
1090 )
1091 {
1092 ASSERT (Tcp4Service->MacString != NULL);
1093
1094 gRT->SetVariable (
1095 Tcp4Service->MacString,
1096 &gEfiTcp4ServiceBindingProtocolGuid,
1097 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1098 0,
1099 NULL
1100 );
1101
1102 gBS->FreePool (Tcp4Service->MacString);
1103 Tcp4Service->MacString = NULL;
1104 }
1105
1106 EFI_STATUS
1107 TcpInstallDevicePath (
1108 IN SOCKET *Sock
1109 )
1110 /*++
1111
1112 Routine Description:
1113
1114 Install the device path protocol on the TCP instance.
1115
1116 Arguments:
1117
1118 Sock - Pointer to the socket representing the TCP instance.
1119
1120 Returns:
1121
1122 EFI_SUCCESS - The device path protocol is installed.
1123 other - Failed to install the device path protocol.
1124
1125 --*/
1126 {
1127 TCP4_PROTO_DATA *TcpProto;
1128 TCP4_SERVICE_DATA *TcpService;
1129 TCP_CB *Tcb;
1130 IPv4_DEVICE_PATH Ip4DPathNode;
1131 EFI_STATUS Status;
1132
1133 TcpProto = (TCP4_PROTO_DATA *) Sock->ProtoReserved;
1134 TcpService = TcpProto->TcpService;
1135 Tcb = TcpProto->TcpPcb;
1136
1137 NetLibCreateIPv4DPathNode (
1138 &Ip4DPathNode,
1139 TcpService->ControllerHandle,
1140 Tcb->LocalEnd.Ip,
1141 NTOHS (Tcb->LocalEnd.Port),
1142 Tcb->RemoteEnd.Ip,
1143 NTOHS (Tcb->RemoteEnd.Port),
1144 EFI_IP_PROTO_TCP,
1145 Tcb->UseDefaultAddr
1146 );
1147
1148 Sock->DevicePath = AppendDevicePathNode (
1149 Sock->ParentDevicePath,
1150 (EFI_DEVICE_PATH_PROTOCOL *) &Ip4DPathNode
1151 );
1152 if (Sock->DevicePath == NULL) {
1153 return EFI_OUT_OF_RESOURCES;
1154 }
1155
1156 Status = gBS->InstallProtocolInterface (
1157 &Sock->SockHandle,
1158 &gEfiDevicePathProtocolGuid,
1159 EFI_NATIVE_INTERFACE,
1160 Sock->DevicePath
1161 );
1162 if (EFI_ERROR (Status)) {
1163 gBS->FreePool (Sock->DevicePath);
1164 }
1165
1166 return Status;
1167 }