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