TcpIo.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /** @file
  2. Implementation of I/O interfaces between TCP and IpIoLib.
  3. Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "TcpMain.h"
  7. /**
  8. Packet receive callback function provided to IP_IO, used to call
  9. the proper function to handle the packet received by IP.
  10. @param[in] Status Result of the receive request.
  11. @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR.
  12. @param[in] NetSession The IP session for the received packet.
  13. @param[in] Pkt Packet received.
  14. @param[in] Context The data provided by the user for the received packet when
  15. the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
  16. This is an optional parameter that may be NULL.
  17. **/
  18. VOID
  19. EFIAPI
  20. TcpRxCallback (
  21. IN EFI_STATUS Status,
  22. IN UINT8 IcmpErr,
  23. IN EFI_NET_SESSION_DATA *NetSession,
  24. IN NET_BUF *Pkt,
  25. IN VOID *Context OPTIONAL
  26. )
  27. {
  28. if (EFI_SUCCESS == Status) {
  29. TcpInput (Pkt, &NetSession->Source, &NetSession->Dest, NetSession->IpVersion);
  30. } else {
  31. TcpIcmpInput (
  32. Pkt,
  33. IcmpErr,
  34. &NetSession->Source,
  35. &NetSession->Dest,
  36. NetSession->IpVersion
  37. );
  38. }
  39. }
  40. /**
  41. Send the segment to IP via IpIo function.
  42. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  43. @param[in] Nbuf Pointer to the TCP segment to be sent.
  44. @param[in] Src Source address of the TCP segment.
  45. @param[in] Dest Destination address of the TCP segment.
  46. @param[in] Version IP_VERSION_4 or IP_VERSION_6
  47. @retval 0 The segment was sent out successfully.
  48. @retval -1 The segment failed to send.
  49. **/
  50. INTN
  51. TcpSendIpPacket (
  52. IN TCP_CB *Tcb,
  53. IN NET_BUF *Nbuf,
  54. IN EFI_IP_ADDRESS *Src,
  55. IN EFI_IP_ADDRESS *Dest,
  56. IN UINT8 Version
  57. )
  58. {
  59. EFI_STATUS Status;
  60. IP_IO *IpIo;
  61. IP_IO_OVERRIDE Override;
  62. SOCKET *Sock;
  63. VOID *IpSender;
  64. TCP_PROTO_DATA *TcpProto;
  65. if (NULL == Tcb) {
  66. IpIo = NULL;
  67. IpSender = IpIoFindSender (&IpIo, Version, Src);
  68. if (IpSender == NULL) {
  69. DEBUG ((DEBUG_WARN, "TcpSendIpPacket: No appropriate IpSender.\n"));
  70. return -1;
  71. }
  72. if (Version == IP_VERSION_6) {
  73. //
  74. // It's tricky here. EFI IPv6 Spec don't allow an instance overriding the
  75. // destination address if the dest is already specified through the
  76. // configuration data. Here we get the IpIo we need and use the default IP
  77. // instance in this IpIo to send the packet. The dest address is configured
  78. // to be the unspecified address for the default IP instance.
  79. //
  80. IpSender = NULL;
  81. }
  82. } else {
  83. Sock = Tcb->Sk;
  84. TcpProto = (TCP_PROTO_DATA *)Sock->ProtoReserved;
  85. IpIo = TcpProto->TcpService->IpIo;
  86. IpSender = Tcb->IpInfo;
  87. if (Version == IP_VERSION_6) {
  88. //
  89. // It's IPv6 and this TCP segment belongs to a solid TCB, in such case
  90. // the destination address can't be overridden, so reset the Dest to NULL.
  91. //
  92. if (!Tcb->RemoteIpZero) {
  93. Dest = NULL;
  94. }
  95. }
  96. }
  97. ASSERT (Version == IpIo->IpVersion);
  98. if (Version == IP_VERSION_4) {
  99. Override.Ip4OverrideData.TypeOfService = 0;
  100. Override.Ip4OverrideData.TimeToLive = 255;
  101. Override.Ip4OverrideData.DoNotFragment = FALSE;
  102. Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_TCP;
  103. ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));
  104. CopyMem (&Override.Ip4OverrideData.SourceAddress, Src, sizeof (EFI_IPv4_ADDRESS));
  105. } else {
  106. Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_TCP;
  107. Override.Ip6OverrideData.HopLimit = 255;
  108. Override.Ip6OverrideData.FlowLabel = 0;
  109. }
  110. Status = IpIoSend (IpIo, Nbuf, IpSender, NULL, NULL, Dest, &Override);
  111. if (EFI_ERROR (Status)) {
  112. DEBUG ((DEBUG_ERROR, "TcpSendIpPacket: return %r error\n", Status));
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. /**
  118. Refresh the remote peer's Neighbor Cache State if already exists.
  119. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  120. @param[in] Neighbor Source address of the TCP segment.
  121. @param[in] Timeout Time in 100-ns units that this entry will remain
  122. in the neighbor cache. A value of zero means that
  123. the entry is permanent. A value of non-zero means
  124. that the entry is dynamic and will be deleted
  125. after Timeout.
  126. @retval EFI_SUCCESS Successfully updated the neighbor relationship.
  127. @retval EFI_NOT_STARTED The IpIo is not configured.
  128. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  129. @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
  130. @retval EFI_NOT_FOUND This entry is not in the neighbor table.
  131. **/
  132. EFI_STATUS
  133. Tcp6RefreshNeighbor (
  134. IN TCP_CB *Tcb,
  135. IN EFI_IP_ADDRESS *Neighbor,
  136. IN UINT32 Timeout
  137. )
  138. {
  139. IP_IO *IpIo;
  140. SOCKET *Sock;
  141. TCP_PROTO_DATA *TcpProto;
  142. if (NULL == Tcb) {
  143. IpIo = NULL;
  144. IpIoFindSender (&IpIo, IP_VERSION_6, Neighbor);
  145. if (IpIo == NULL) {
  146. DEBUG ((DEBUG_WARN, "Tcp6AddNeighbor: No appropriate IpIo.\n"));
  147. return EFI_NOT_STARTED;
  148. }
  149. } else {
  150. Sock = Tcb->Sk;
  151. TcpProto = (TCP_PROTO_DATA *)Sock->ProtoReserved;
  152. IpIo = TcpProto->TcpService->IpIo;
  153. }
  154. return IpIoRefreshNeighbor (IpIo, Neighbor, Timeout);
  155. }