udp4main.c

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

C
886
字号
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. 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                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 

Module Name:

  Udp4Main.c

Abstract:

--*/

#include "Udp4Impl.h"

EFI_UDP4_PROTOCOL  mUdp4Protocol = {
  Udp4GetModeData,
  Udp4Configure,
  Udp4Groups,
  Udp4Routes,
  Udp4Transmit,
  Udp4Receive,
  Udp4Cancel,
  Udp4Poll
};

EFI_STATUS
EFIAPI
Udp4GetModeData (
  IN  EFI_UDP4_PROTOCOL                *This,
  OUT EFI_UDP4_CONFIG_DATA             *Udp4ConfigData OPTIONAL,
  OUT EFI_IP4_MODE_DATA                *Ip4ModeData    OPTIONAL,
  OUT EFI_MANAGED_NETWORK_CONFIG_DATA  *MnpConfigData  OPTIONAL,
  OUT EFI_SIMPLE_NETWORK_MODE          *SnpModeData    OPTIONAL
  )
/*++

Routine Description:

  This function copies the current operational settings of this EFI UDPv4 Protocol
  instance into user-supplied buffers. This function is used optionally to retrieve
  the operational mode data of underlying networks or drivers.

Arguments:

  This           - Pointer to the EFI_UDP4_PROTOCOL instance.
  Udp4ConfigData - Pointer to the buffer to receive the current configuration data.
  Ip4ModeData    - Pointer to the EFI IPv4 Protocol mode data structure.
  MnpConfigData  - Pointer to the managed network configuration data structure.
  SnpModeData    - Pointer to the simple network mode data structure.

Returns:

  EFI_SUCCESS           - The mode data was read.
  EFI_NOT_STARTED       - When Udp4ConfigData is queried, no configuration data is 
                          available because this instance has not been started.
  EFI_INVALID_PARAMETER - This is NULL.

--*/
{
  UDP4_INSTANCE_DATA  *Instance;
  EFI_IP4_PROTOCOL    *Ip;

  if (This == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);

  if (!Instance->Configured && (Udp4ConfigData != NULL)) {
    return EFI_NOT_STARTED;
  }

  if (Udp4ConfigData != NULL) {
    //
    // Set the Udp4ConfigData.
    //
    *Udp4ConfigData = Instance->ConfigData;
  }

  Ip = Instance->IpInfo->Ip;

  //
  // Get the underlying Ip4ModeData, MnpConfigData and SnpModeData.
  //
  return Ip->GetModeData (Ip, Ip4ModeData, MnpConfigData, SnpModeData);
}

EFI_STATUS
EFIAPI
Udp4Configure ( 
  IN EFI_UDP4_PROTOCOL     *This,
  IN EFI_UDP4_CONFIG_DATA  *UdpConfigData OPTIONAL
  )
/*++

Routine Description:

  This function is used to do the following:
    Initialize and start this instance of the EFI UDPv4 Protocol.
    Change the filtering rules and operational parameters.
    Reset this instance of the EFI UDPv4 Protocol.

Arguments:

  This          - Pointer to the EFI_UDP4_PROTOCOL instance.
  UdpConfigData - Pointer to the buffer to receive the current mode data.

Returns:

  EFI_SUCCESS           - The configuration settings were set, changed, or reset
                          successfully.
  EFI_NO_MAPPING        - When using a default address, configuration (DHCP, BOOTP,
                          RARP, etc.) is not finished yet.
  EFI_INVALID_PARAMETER - One or more following conditions are TRUE:
                            This is NULL.
                            UdpConfigData.StationAddress is not a valid unicast IPv4
                            address.
                            UdpConfigData.SubnetMask is not a valid IPv4 address mask. 
                            UdpConfigData.RemoteAddress is not a valid unicast IPv4 
                            address if it is not zero.
  EFI_ALREADY_STARTED   - The EFI UDPv4 Protocol instance is already started/configured
                          and must be stopped/reset before it can be reconfigured. Only
                          TypeOfService, TimeToLive, DoNotFragment, ReceiveTimeout, and
                          TransmitTimeout can be reconfigured without stopping the
                          current instance of the EFI UDPv4 Protocol.
  EFI_ACCESS_DENIED     - UdpConfigData.AllowDuplicatePort is FALSE and
                          UdpConfigData.StationPort is already used by other instance.
  EFI_OUT_OF_RESOURCES  - The EFI UDPv4 Protocol driver cannot allocate memory for this
                          EFI UDPv4 Protocol instance.
  EFI_DEVICE_ERROR      - An unexpected network or system error occurred and this
                          instance was not opened.

--*/
{
  EFI_STATUS           Status;
  UDP4_INSTANCE_DATA   *Instance;
  UDP4_SERVICE_DATA    *Udp4Service;
  EFI_TPL              OldTpl;
  IP4_ADDR             StationAddress;
  IP4_ADDR             SubnetMask;
  IP4_ADDR             RemoteAddress;
  EFI_IP4_CONFIG_DATA  Ip4ConfigData;

  if (This == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);

  if (!Instance->Configured && (UdpConfigData == NULL)) {
    return EFI_SUCCESS;
  }

  Udp4Service = Instance->Udp4Service;
  Status      = EFI_SUCCESS;

  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);

  if (UdpConfigData != NULL) {

    StationAddress = EFI_NTOHL (UdpConfigData->StationAddress);
    SubnetMask     = EFI_NTOHL (UdpConfigData->SubnetMask);
    RemoteAddress  = EFI_NTOHL (UdpConfigData->RemoteAddress);

    if (!UdpConfigData->UseDefaultAddress &&
      (!IP4_IS_VALID_NETMASK (SubnetMask) ||
      !((StationAddress == 0) || Ip4IsUnicast (StationAddress, SubnetMask)) ||
      !((RemoteAddress  == 0) || Ip4IsUnicast (RemoteAddress, 0)))) {
      //
      // Don't use default address, and subnet mask is invalid or StationAddress is not
      // a valid unicast IPv4 address or RemoteAddress is not a valid unicast IPv4 address
      // if it is not 0.
      //
      Status = EFI_INVALID_PARAMETER;
      goto ON_EXIT;
    }

    if (Instance->Configured) {
      //
      // The instance is already configured, try to do the re-configuration.
      //
      if (!Udp4IsReconfigurable (&Instance->ConfigData, UdpConfigData)) {
        //
        // If the new configuration data wants to change some unreconfigurable
        // settings, return EFI_ALREADY_STARTED.
        //
        Status = EFI_ALREADY_STARTED;
        goto ON_EXIT;
      }

      //
      // Save the reconfigurable parameters.
      //
      Instance->ConfigData.TypeOfService   = UdpConfigData->TypeOfService;
      Instance->ConfigData.TimeToLive      = UdpConfigData->TimeToLive;
      Instance->ConfigData.DoNotFragment   = UdpConfigData->DoNotFragment;
      Instance->ConfigData.ReceiveTimeout  = UdpConfigData->ReceiveTimeout;
      Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout;
    } else {
      //
      // Construct the Ip configuration data from the UdpConfigData.
      //
      Udp4BuildIp4ConfigData (UdpConfigData, &Ip4ConfigData);

      //
      // Configure the Ip instance wrapped in the IpInfo.
      //
      Status = IpIoConfigIp (Instance->IpInfo, &Ip4ConfigData);
      if (EFI_ERROR (Status)) {
        if (Status == EFI_NO_MAPPING) {
          Instance->IsNoMapping = TRUE;
        }

        goto ON_EXIT;
      }

      Instance->IsNoMapping = FALSE;

      //
      // Save the configuration data.
      //
      Instance->ConfigData                = *UdpConfigData;
      Instance->ConfigData.StationAddress = Ip4ConfigData.StationAddress;
      Instance->ConfigData.SubnetMask     = Ip4ConfigData.SubnetMask;

      //
      // Try to allocate the required port resource.
      //
      Status = Udp4Bind (&Udp4Service->ChildrenList, &Instance->ConfigData);
      if (EFI_ERROR (Status)) {
        //
        // Reset the ip instance if bind fails.
        //
        IpIoConfigIp (Instance->IpInfo, NULL);
        goto ON_EXIT;
      }

      //
      // Pre calculate the checksum for the pseudo head, ignore the UDP length first.
      //
      Instance->HeadSum = NetPseudoHeadChecksum (
                            EFI_IP4 (Instance->ConfigData.StationAddress),
                            EFI_IP4 (Instance->ConfigData.RemoteAddress),
                            EFI_IP_PROTO_UDP,
                            0
                            );

      Instance->Configured = TRUE;
    }
  } else {
    //
    // UdpConfigData is NULL, reset the instance.
    //
    Instance->Configured  = FALSE;
    Instance->IsNoMapping = FALSE;

    //
    // Reset the Ip instance wrapped in the IpInfo.
    //
    IpIoConfigIp (Instance->IpInfo, NULL);

    //
    // Cancel all the user tokens.
    //
    Udp4InstanceCancelToken (Instance, NULL);

    //
    // Remove the buffered RxData for this instance.
    //
    Udp4FlushRxData (&Instance->RcvdDgramQue);
  }

  Udp4SetVariableData (Instance->Udp4Service);

ON_EXIT:

  NET_RESTORE_TPL (OldTpl);

  return Status;
}

EFI_STATUS
EFIAPI
Udp4Groups (
  IN EFI_UDP4_PROTOCOL  *This,
  IN BOOLEAN            JoinFlag,
  IN EFI_IPv4_ADDRESS   *MulticastAddress OPTIONAL
  )
/*++

Routine Description:

  This function is used to enable and disable the multicast group filtering.

Arguments:

  This             - Pointer to the EFI_UDP4_PROTOCOL instance.
  JoinFlag         - Set to TRUE to join a multicast group. Set to FALSE to leave one
                     or all multicast groups.
  MulticastAddress - Pointer to multicast group address to join or leave.

Returns:

  EFI_SUCCESS           - The operation completed successfully.
  EFI_NOT_STARTED       - The EFI UDPv4 Protocol instance has not been started.
  EFI_NO_MAPPING        - When using a default address, configuration (DHCP, BOOTP,
                          RARP, etc.) is not finished yet.
  EFI_OUT_OF_RESOURCES  - Could not allocate resources to join the group.
  EFI_INVALID_PARAMETER - One or more of the following conditions is TRUE:
                            This is NULL.
                            JoinFlag is TRUE and MulticastAddress is NULL.
                            JoinFlag is TRUE and *MulticastAddress is not a valid 
                            multicast address.
  EFI_ALREADY_STARTED   - The group address is already in the group table (when JoinFlag
                          is TRUE).
  EFI_NOT_FOUND         - The group address is not in the group table (when JoinFlag is
                          FALSE).
  EFI_DEVICE_ERROR      - An unexpected system or network error occurred.

--*/
{
  EFI_STATUS          Status;
  UDP4_INSTANCE_DATA  *Instance;
  EFI_IP4_PROTOCOL    *Ip;
  EFI_TPL             OldTpl;

  if ((This == NULL) ||
    (JoinFlag && (MulticastAddress == NULL)) ||
    (JoinFlag && !IP4_IS_MULTICAST (EFI_NTOHL (*MulticastAddress)))) {
    return EFI_INVALID_PARAMETER;
  }

  Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);

  if (Instance->IsNoMapping) {
    return EFI_NO_MAPPING;
  }

  if (!Instance->Configured) {
    return EFI_NOT_STARTED;
  }

  Ip = Instance->IpInfo->Ip;

  //
  // Invoke the Ip instance the Udp4 instance consumes to do the group operation.
  //
  Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);

  //
  // Keep a local copy of the configured multicast IPs because IpIo receives
  // datagrams from the 0 station address IP instance and then UDP delivers to
  // the matched instance. This copy of multicast IPs is used to avoid receive
  // the mutlicast datagrams destinated to multicast IPs the other instances configured.
  //
  if (JoinFlag) {

    NetMapInsertTail (
      &Instance->McastIps,
      (VOID *) (UINTN) EFI_IP4 (*MulticastAddress),
      NULL
      );
  } else {

    NetMapIterate (&Instance->McastIps, Udp4LeaveGroup, MulticastAddress);
  }

  NET_RESTORE_TPL (OldTpl);

  return Status;
}

EFI_STATUS
EFIAPI
Udp4Routes (
  IN EFI_UDP4_PROTOCOL  *This,
  IN BOOLEAN            DeleteRoute,
  IN EFI_IPv4_ADDRESS   *SubnetAddress,
  IN EFI_IPv4_ADDRESS   *SubnetMask,
  IN EFI_IPv4_ADDRESS   *GatewayAddress
  )
/*++

Routine Description:

  This function adds a route to or deletes a route from the routing table.

Arguments:

  This           - Pointer to the EFI_UDP4_PROTOCOL instance.
  DeleteRoute    - Set to TRUE to delete this route from the routing table. Set to
                   FALSE to add this route to the routing table.
  SubnetAddress  - The destination network address that needs to be routed.
  SubnetMask     - The subnet mask of SubnetAddress.
  GatewayAddress - The gateway IP address for this route.

Returns:

  EFI_SUCCESS           - The operation completed successfully.
  EFI_NOT_STARTED       - The EFI UDPv4 Protocol instance has not been started.
  EFI_NO_MAPPING        - When using a default address, configuration (DHCP, BOOTP,
                          RARP, etc.) is not finished yet.
  EFI_INVALID_PARAMETER - One or more of the following conditions is TRUE:
                            This is NULL.
                            SubnetAddress is NULL.
                            SubnetMask is NULL.
                            GatewayAddress is NULL.
                            SubnetAddress is not a valid subnet address.
                            SubnetMask is not a valid subnet mask.
                            GatewayAddress is not a valid unicast IP address.
  EFI_OUT_OF_RESOURCES  - Could not add the entry to the routing table.
  EFI_NOT_FOUND         - This route is not in the routing table.
  EFI_ACCESS_DENIED     - The route is already defined in the routing table.

--*/
{
  UDP4_INSTANCE_DATA  *Instance;
  EFI_IP4_PROTOCOL    *Ip;

  if (This == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);

  if (Instance->IsNoMapping) {
    return EFI_NO_MAPPING;
  }

  if (!Instance->Configured) {

⌨️ 快捷键说明

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