ipio.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,364 行 · 第 1/2 页

C
1,364
字号
    // The source address is not zero and it's not a unicast IP address, discard it.
    //
    goto CleanUp;
  }

  //
  // Create a netbuffer representing packet
  //
  Pkt = NetbufFromExt (
          (NET_FRAGMENT *) RxData->FragmentTable,
          RxData->FragmentCount,
          0,
          0,
          IpIoExtFree,
          RxData->RecycleSignal
          );
  if (NULL == Pkt) {
    goto CleanUp;
  }

  //
  // Create a net session
  //
  Session.Source = EFI_IP4 (RxData->Header->SourceAddress);
  Session.Dest   = EFI_IP4 (RxData->Header->DestinationAddress);
  Session.IpHdr  = RxData->Header;

  if (EFI_SUCCESS == Status) {

    IpIo->PktRcvdNotify (EFI_SUCCESS, 0, &Session, Pkt, IpIo->RcvdContext);
  } else {
    //
    // Status is EFI_ICMP_ERROR
    //
    Status = IpIoIcmpHandler (IpIo, Pkt, &Session);
    if (EFI_ERROR (Status)) {
      NetbufFree (Pkt);
    }
  }

  goto Resume;

CleanUp:
  gBS->SignalEvent (RxData->RecycleSignal);

Resume:
  Ip->Receive (Ip, &(IpIo->RcvToken));
}

IP_IO *
IpIoCreate (
  IN EFI_HANDLE Image,
  IN EFI_HANDLE Controller
  )
/*++

Routine Description:

  Create a new IP_IO instance.

Arguments:

  Image       - The image handle of an IP_IO consumer protocol.
  Controller  - The controller handle of an IP_IO consumer
                protocol installed on.

Returns:

  Pointer to a newly created IP_IO instance.

--*/
{
  EFI_STATUS  Status;
  IP_IO       *IpIo;

  IpIo = NetAllocateZeroPool (sizeof (IP_IO));
  if (NULL == IpIo) {
    return NULL;
  }

  NetListInit (&(IpIo->PendingSndList));
  NetListInit (&(IpIo->IpList));
  IpIo->Controller  = Controller;
  IpIo->Image       = Image;

  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL,
                  NET_TPL_EVENT,
                  IpIoListenHandler,
                  IpIo,
                  &(IpIo->RcvToken.Event)
                  );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpIo;
  }

  //
  // Create an IP child and open IP protocol
  //
  Status = IpIoCreateIpChildOpenProtocol (
             Controller,
             Image,
             &IpIo->ChildHandle,
             (VOID **)&(IpIo->Ip)
             );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpIo;
  }

  return IpIo;

ReleaseIpIo:

  if (NULL != IpIo->RcvToken.Event) {
    gBS->CloseEvent (IpIo->RcvToken.Event);
  }

  NetFreePool (IpIo);

  return NULL;
}

EFI_STATUS
IpIoOpen (
  IN IP_IO           *IpIo,
  IN IP_IO_OPEN_DATA *OpenData
  )
/*++

Routine Description:

  Open an IP_IO instance for use.

Arguments:

  IpIo      - Pointer to an IP_IO instance that needs to open.
  OpenData  - The configuration data for the IP_IO instance.

Returns:

  EFI_SUCCESS - The IP_IO instance opened with OpenData successfully.
  other       - Error condition occurred.

--*/
{
  EFI_STATUS        Status;
  EFI_IP4_PROTOCOL  *Ip;
  EFI_IPv4_ADDRESS  ZeroIp;

  if (IpIo->IsConfigured) {
    return EFI_ACCESS_DENIED;
  }

  Ip = IpIo->Ip;

  //
  // configure ip
  //
  Status = Ip->Configure (Ip, &OpenData->IpConfigData);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // bugbug: to delete the default route entry in this Ip, if it is:
  // (0.0.0.0, 0.0.0.0, 0.0.0.0). Delete this statement if Ip modified
  // its code
  //
  EFI_IP4 (ZeroIp) = 0;
  Status = Ip->Routes (Ip, TRUE, &ZeroIp, &ZeroIp, &ZeroIp);

  if (EFI_ERROR (Status) && (EFI_NOT_FOUND != Status)) {
    return Status;
  }

  IpIo->PktRcvdNotify = OpenData->PktRcvdNotify;
  IpIo->PktSentNotify = OpenData->PktSentNotify;

  IpIo->RcvdContext   = OpenData->RcvdContext;
  IpIo->SndContext    = OpenData->SndContext;

  IpIo->Protocol      = OpenData->IpConfigData.DefaultProtocol;

  //
  // start to listen incoming packet
  //
  Status = Ip->Receive (Ip, &(IpIo->RcvToken));
  if (EFI_ERROR (Status)) {
    Ip->Configure (Ip, NULL);
    goto ErrorExit;
  }

  IpIo->IsConfigured = TRUE;
  NetListInsertTail (&mActiveIpIoList, &IpIo->Entry);

ErrorExit:

  return Status;
}

EFI_STATUS
IpIoStop (
  IN IP_IO *IpIo
  )
/*++

Routine Description:

  Stop an IP_IO instance.

Arguments:

  IpIo  - Pointer to the IP_IO instance that needs to stop.

Returns:

  EFI_SUCCESS - The IP_IO instance stopped successfully.
  other       - Error condition occurred.

--*/
{
  EFI_STATUS        Status;
  EFI_IP4_PROTOCOL  *Ip;
  IP_IO_IP_INFO     *IpInfo;

  if (!IpIo->IsConfigured) {
    return EFI_SUCCESS;
  }

  //
  // Remove the IpIo from the active IpIo list.
  //
  NetListRemoveEntry (&IpIo->Entry);

  Ip = IpIo->Ip;

  //
  // Configure NULL Ip
  //
  Status = Ip->Configure (Ip, NULL);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  IpIo->IsConfigured = FALSE;

  //
  // Detroy the Ip List used by IpIo
  //
  while (!NetListIsEmpty (&(IpIo->IpList))) {
    IpInfo = NET_LIST_HEAD (&(IpIo->IpList), IP_IO_IP_INFO, Entry);

    IpIoRemoveIp (IpIo, IpInfo);
  }

  //
  // All pending snd tokens should be flushed by reseting the IP instances.
  //
  ASSERT (NetListIsEmpty (&IpIo->PendingSndList));

  //
  // Close the receive event.
  //
  gBS->CloseEvent (IpIo->RcvToken.Event);

  return EFI_SUCCESS;
}

EFI_STATUS
IpIoDestroy (
  IN IP_IO *IpIo
  )
/*++

Routine Description:

  Destroy an IP_IO instance.

Arguments:

  IpIo  - Pointer to the IP_IO instance that needs to destroy.

Returns:

  EFI_SUCCESS - The IP_IO instance destroyed successfully.
  other       - Error condition occurred.

--*/
{
  //
  // Stop the IpIo.
  //
  IpIoStop (IpIo);

  //
  // Close the IP protocol and destroy the child.
  //
  IpIoCloseProtocolDestroyIpChild (IpIo->Controller, IpIo->Image, IpIo->ChildHandle);

  NetFreePool (IpIo);

  return EFI_SUCCESS;
}

EFI_STATUS
IpIoSend (
  IN IP_IO           *IpIo,
  IN NET_BUF         *Pkt,
  IN IP_IO_IP_INFO   *Sender,
  IN VOID            *Context    OPTIONAL,
  IN VOID            *NotifyData OPTIONAL,
  IN IP4_ADDR        Dest,
  IN IP_IO_OVERRIDE  *OverrideData
  )
/*++

Routine Description:

  Send out an IP packet.

Arguments:

  IpIo          - Pointer to an IP_IO instance used for sending IP packet.
  Pkt           - Pointer to the IP packet to be sent.
  Sender        - The IP protocol instance used for sending.
  NotifyData    - 
  Dest          - The destination IP address to send this packet to.
  OverrideData  - The data to override some configuration of the IP
                  instance used for sending.

Returns:

  EFI_SUCCESS          - The operation is completed successfully.
  EFI_NOT_STARTED      - The IpIo is not configured.
  EFI_OUT_OF_RESOURCES - Failed due to resource limit.

--*/
{
  EFI_STATUS        Status;
  EFI_IP4_PROTOCOL  *Ip;
  IP_IO_SEND_ENTRY  *SndEntry;

  if (!IpIo->IsConfigured) {
    return EFI_NOT_STARTED;
  }

  Ip = (NULL == Sender) ? IpIo->Ip : Sender->Ip;

  //
  // create a new SndEntry
  //
  SndEntry = IpIoCreateSndEntry (IpIo, Pkt, Ip, Context, NotifyData, Dest, OverrideData);
  if (NULL == SndEntry) {
    return EFI_OUT_OF_RESOURCES;
  }

  //
  // Send this Packet
  //
  Status = Ip->Transmit (Ip, SndEntry->SndToken);
  if (EFI_ERROR (Status)) {
    IpIoDestroySndEntry (SndEntry);
  }

  return Status;
}

VOID
IpIoCancelTxToken (
  IN IP_IO  *IpIo,
  IN VOID   *Packet
  )
/*++

Routine Description:

  Cancel the IP transmit token which wraps this Packet.

Arguments:

  IpIo   - Pointer to the IP_IO instance.
  Packet - Pointer to the packet to cancel.

Returns:

  N/A.

--*/

{
  NET_LIST_ENTRY    *Node;
  IP_IO_SEND_ENTRY  *SndEntry;
  EFI_IP4_PROTOCOL  *Ip;

  ASSERT (IpIo && Packet);

  NET_LIST_FOR_EACH (Node, &IpIo->PendingSndList) {

    SndEntry = NET_LIST_USER_STRUCT (Node, IP_IO_SEND_ENTRY, Entry);

    if (SndEntry->Pkt == Packet) {

      Ip = SndEntry->Ip;
      Ip->Cancel (Ip, SndEntry->SndToken);

      //
      // Abort the user token.
      //
      SndEntry->SndToken->Status = EFI_ABORTED;
      IpIoTransmitHandler (NULL, SndEntry);

      break;
    }
  }

}

IP_IO_IP_INFO *
IpIoAddIp (
  IN IP_IO  *IpIo
  )
/*++

Routine Description:

  Add a new IP instance for sending data.

Arguments:

  IpIo - Pointer to a IP_IO instance to add a new IP instance for sending purpose.

Returns:

  Pointer to the created IP_IO_IP_INFO structure, NULL is failed.

--*/
{
  EFI_STATUS     Status;
  IP_IO_IP_INFO  *IpInfo;

  ASSERT (IpIo);

  IpInfo = NetAllocatePool (sizeof (IP_IO_IP_INFO));
  if (IpInfo == NULL) {
    return IpInfo;
  }

  //
  // Init this IpInfo, set the Addr and SubnetMask to 0 before we configure the IP
  // instance.
  //
  NetListInit (&IpInfo->Entry);
  IpInfo->ChildHandle = NULL;
  IpInfo->Addr        = 0;
  IpInfo->SubnetMask  = 0;
  IpInfo->RefCnt      = 1;

  //
  // Create the IP instance and open the Ip4 protocol.
  //
  Status = IpIoCreateIpChildOpenProtocol (
             IpIo->Controller,
             IpIo->Image,
             &IpInfo->ChildHandle,
             &IpInfo->Ip
             );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpInfo;
  }

  //
  // Create the event for the DummyRcvToken.
  //
  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL,
                  NET_TPL_EVENT,
                  IpIoDummyHandler,
                  IpInfo,
                  &IpInfo->DummyRcvToken.Event
                  );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpChild;
  }

  //
  // Link this IpInfo into the IpIo.
  //
  NetListInsertTail (&IpIo->IpList, &IpInfo->Entry);

  return IpInfo;

ReleaseIpChild:

  IpIoCloseProtocolDestroyIpChild (
    IpIo->Controller,
    IpIo->Image,
    IpInfo->ChildHandle
    );

ReleaseIpInfo:

  NetFreePool (IpInfo);

  return NULL;
}

EFI_STATUS
IpIoConfigIp (
  IN     IP_IO_IP_INFO        *IpInfo,
  IN OUT EFI_IP4_CONFIG_DATA  *Ip4ConfigData OPTIONAL
  )
/*++

Routine Description:

  Configure the IP instance of this IpInfo and start the receiving if Ip4ConfigData
  is not NULL.

Arguments:

  IpInfo         - Pointer to the IP_IO_IP_INFO instance.
  Ip4ConfigData  - The IP4 configure data used to configure the ip instance, if NULL
                   the ip instance is reseted. If UseDefaultAddress is set to TRUE, and
                   the configure operation succeeds, the default address information is
                   written back in this Ip4ConfigData.

Returns:

  EFI_STATUS     - The status returned by IP4->Configure or IP4->Receive.


--*/
{
  EFI_STATUS         Status;
  EFI_IP4_PROTOCOL   *Ip;
  EFI_IP4_MODE_DATA  Ip4ModeData;

  ASSERT (IpInfo);

  if (IpInfo->RefCnt > 1) {
    //
    // This IP instance is shared, don't reconfigure it until it has only one
    // consumer. Currently, only the tcp children cloned from their passive parent
    // will share the same IP. So this cases only happens while Ip4ConfigData is NULL,
    // let the last consumer clean the IP instance.
    //
    return EFI_SUCCESS;
  }

  Ip = IpInfo->Ip;

  Status = Ip->Configure (Ip, Ip4ConfigData);
  if (EFI_ERROR (Status)) {
    goto OnExit;
  }

  if (Ip4ConfigData != NULL) {

    if (Ip4ConfigData->UseDefaultAddress) {
      Ip->GetModeData (Ip, &Ip4ModeData, NULL, NULL);

      Ip4ConfigData->StationAddress = Ip4ModeData.ConfigData.StationAddress;
      Ip4ConfigData->SubnetMask     = Ip4ModeData.ConfigData.SubnetMask;
    }

    IpInfo->Addr       = EFI_IP4 (Ip4ConfigData->StationAddress);
    IpInfo->SubnetMask = EFI_IP4 (Ip4ConfigData->SubnetMask);

    Status = Ip->Receive (Ip, &IpInfo->DummyRcvToken);
    if (EFI_ERROR (Status)) {
      Ip->Configure (Ip, NULL);
    }
  } else {

    //
    // The IP instance is reseted, set the stored Addr and SubnetMask to zero.
    //
    IpInfo->Addr       = 0;
    IpInfo->SubnetMask =0;
  }

OnExit:

  return Status;
}

VOID
IpIoRemoveIp (
  IN IP_IO          *IpIo,
  IN IP_IO_IP_INFO  *IpInfo
  )
/*++

Routine Description:

  Destroy an IP instance maintained in IpIo->IpList for
  sending purpose.

Arguments:

  IpIo   - Pointer to the IP_IO instance.
  IpInfo - Pointer to the IpInfo to be removed.

Returns:

  None.

--*/
{
  ASSERT (IpInfo->RefCnt > 0);

  NET_PUT_REF (IpInfo);

  if (IpInfo->RefCnt > 0) {

    return;
  }

  NetListRemoveEntry (&IpInfo->Entry);

  IpInfo->Ip->Configure (IpInfo->Ip, NULL);

  IpIoCloseProtocolDestroyIpChild (IpIo->Controller, IpIo->Image, IpInfo->ChildHandle);

  gBS->CloseEvent (IpInfo->DummyRcvToken.Event);

  NetFreePool (IpInfo);
}

IP_IO_IP_INFO *
IpIoFindSender (
  IN OUT IP_IO     **IpIo,
  IN     IP4_ADDR  Src
  )
/*++

Routine Description:

  Find the first IP protocol maintained in IpIo whose local
  address is the same with Src.

Arguments:

  IpIo  - Pointer to the pointer of the IP_IO instance.
  Src   - The local IP address.

Returns:

  Pointer to the IP protocol can be used for sending purpose and its local
  address is the same with Src.

--*/
{
  NET_LIST_ENTRY  *IpIoEntry;
  IP_IO           *IpIoPtr;
  NET_LIST_ENTRY  *IpInfoEntry;
  IP_IO_IP_INFO   *IpInfo;

  NET_LIST_FOR_EACH (IpIoEntry, &mActiveIpIoList) {
    IpIoPtr = NET_LIST_USER_STRUCT (IpIoEntry, IP_IO, Entry);

    if ((*IpIo != NULL) && (*IpIo != IpIoPtr)) {
      continue;
    }

    NET_LIST_FOR_EACH (IpInfoEntry, &IpIoPtr->IpList) {
      IpInfo = NET_LIST_USER_STRUCT (IpInfoEntry, IP_IO_IP_INFO, Entry);

      if (IpInfo->Addr == Src) {
        *IpIo = IpIoPtr;
        return IpInfo;
      }
    }
  }

  //
  // No match.
  //
  return NULL;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?