pxe_bc_arp.c

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

C
617
字号
/*++

Copyright (c) 2004, 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:
  pxe_bc_arp.c

Abstract:

--*/

#include "bc.h"

//
// Definitions for ARP
// Per RFC 826
//
STATIC ARP_HEADER ArpHeader;

#pragma pack(1)
STATIC struct {
  UINT8       MediaHeader[14];
  ARP_HEADER  ArpHeader;
  UINT8       ArpData[64];
} ArpReplyPacket;
#pragma pack()

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
VOID
InitArpHeader (
  VOID
  )
/*++
Routine description:
  Initialize ARP packet header.

Parameters:
  none

Returns:
  none

--*/
{
  ArpHeader.HwType      = HTONS (ETHERNET_ADD_SPC);
  ArpHeader.ProtType    = HTONS (ETHER_TYPE_IP);
  ArpHeader.HwAddLen    = ENET_HWADDLEN;
  ArpHeader.ProtAddLen  = IPV4_PROTADDLEN;
  ArpHeader.OpCode      = HTONS (ARP_REQUEST);

  EfiCopyMem (&ArpReplyPacket.ArpHeader, &ArpHeader, sizeof (ARP_HEADER));
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
VOID
HandleArpReceive (
  IN PXE_BASECODE_DEVICE  *Private,
  IN ARP_PACKET           *ArpPacketPtr,
  IN VOID                 *MediaHeader
  )
/*++
Routine description:
  Process ARP packet.

Parameters:
  Private := Pointer to PxeBc interface
  ArpPacketPtr := Pointer to ARP packet
  MediaHeader := Pointer to media header.
Returns:
--*/
{
  EFI_PXE_BASE_CODE_MODE  *PxeBcMode;
  EFI_SIMPLE_NETWORK_MODE *SnpMode;
  EFI_MAC_ADDRESS         TmpMacAddr;
  UINTN                   Index;
  UINT8                   *SrcHwAddr;
  UINT8                   *SrcPrAddr;
  UINT8                   *DstHwAddr;
  UINT8                   *DstPrAddr;
  UINT8                   *TmpPtr;

  //
  //
  //
  PxeBcMode = Private->EfiBc.Mode;
  SnpMode   = Private->SimpleNetwork->Mode;

  //
  // For now only ethernet addresses are supported.
  // This will need to be updated when other media
  // layers are supported by PxeBc, Snp and UNDI.
  //
  if (ArpPacketPtr->ArpHeader.HwType != HTONS (ETHERNET_ADD_SPC)) {
    return ;
  }
  //
  // For now only IP protocol addresses are supported.
  // This will need to be updated when other protocol
  // types are supported by PxeBc, Snp and UNDI.
  //
  if (ArpPacketPtr->ArpHeader.ProtType != HTONS (ETHER_TYPE_IP)) {
    return ;
  }
  //
  // For now only SNP hardware address sizes are supported.
  //
  if (ArpPacketPtr->ArpHeader.HwAddLen != SnpMode->HwAddressSize) {
    return ;
  }
  //
  // For now only PxeBc protocol address sizes are supported.
  //
  if (ArpPacketPtr->ArpHeader.ProtAddLen != Private->IpLength) {
    return ;
  }
  //
  // Ignore out of range opcodes
  //
  switch (ArpPacketPtr->ArpHeader.OpCode) {
  case HTONS (ARP_REPLY):
  case HTONS (ARP_REQUEST):
    break;

  default:
    return ;
  }
  //
  // update entry in our ARP cache if we have it
  //
  SrcHwAddr = (UINT8 *) &ArpPacketPtr->SrcHardwareAddr;
  SrcPrAddr = SrcHwAddr + SnpMode->HwAddressSize;

  for (Index = 0; Index < PxeBcMode->ArpCacheEntries; ++Index) {
    if (EfiCompareMem (
          &PxeBcMode->ArpCache[Index].IpAddr,
          SrcPrAddr,
          Private->IpLength
          )) {
      continue;
    }

    EfiCopyMem (
      &PxeBcMode->ArpCache[Index].MacAddr,
      SrcHwAddr,
      SnpMode->HwAddressSize
      );

    break;
  }
  //
  // Done if ARP packet was not for us.
  //
  DstHwAddr = SrcPrAddr + Private->IpLength;
  DstPrAddr = DstHwAddr + SnpMode->HwAddressSize;

  if (EfiCompareMem (DstPrAddr, &PxeBcMode->StationIp, Private->IpLength)) {
    return ;
    //
    // not for us
    //
  }
  //
  // for us - if we did not update entry, add it
  //
  if (Index == PxeBcMode->ArpCacheEntries) {
    //
    // if we have a full table, get rid of oldest
    //
    if (Index == PXE_ARP_CACHE_SIZE) {
      Index = Private->OldestArpEntry;

      if (++Private->OldestArpEntry == PXE_ARP_CACHE_SIZE) {
        Private->OldestArpEntry = 0;
      }
    } else {
      ++PxeBcMode->ArpCacheEntries;
    }

    EfiCopyMem (
      &PxeBcMode->ArpCache[Index].MacAddr,
      SrcHwAddr,
      SnpMode->HwAddressSize
      );

    EfiCopyMem (
      &PxeBcMode->ArpCache[Index].IpAddr,
      SrcPrAddr,
      Private->IpLength
      );
  }
  //
  // if this is not a request or we don't yet have an IP, finished
  //
  if (ArpPacketPtr->ArpHeader.OpCode != HTONS (ARP_REQUEST) || !Private->GoodStationIp) {
    return ;
  }
  //
  // Assemble ARP reply.
  //
  //
  // Create media header.  [ dest mac | src mac | prot ]
  //
  EfiCopyMem (
    &ArpReplyPacket.MediaHeader[0],
    SrcHwAddr,
    SnpMode->HwAddressSize
    );

  EfiCopyMem (
    &ArpReplyPacket.MediaHeader[SnpMode->HwAddressSize],
    &SnpMode->CurrentAddress,
    SnpMode->HwAddressSize
    );

  EfiCopyMem (
    &ArpReplyPacket.MediaHeader[2 * SnpMode->HwAddressSize],
    &((UINT8 *) MediaHeader)[2 * SnpMode->HwAddressSize],
    sizeof (UINT16)
    );

  //
  // ARP reply header is almost filled in,
  // just insert the correct opcode.
  //
  ArpReplyPacket.ArpHeader.OpCode = HTONS (ARP_REPLY);

  //
  // Now fill in ARP data.  [ src mac | src prot | dest mac | dest prot ]
  //
  TmpPtr = ArpReplyPacket.ArpData;
  EfiCopyMem (TmpPtr, &SnpMode->CurrentAddress, SnpMode->HwAddressSize);

  TmpPtr += SnpMode->HwAddressSize;
  EfiCopyMem (TmpPtr, &PxeBcMode->StationIp, Private->IpLength);

  TmpPtr += Private->IpLength;
  EfiCopyMem (TmpPtr, SrcHwAddr, SnpMode->HwAddressSize);

  TmpPtr += SnpMode->HwAddressSize;
  EfiCopyMem (TmpPtr, SrcPrAddr, Private->IpLength);

  //
  // Now send out the ARP reply.
  //
  EfiCopyMem (&TmpMacAddr, SrcHwAddr, sizeof (EFI_MAC_ADDRESS));

  SendPacket (
    Private,
    &ArpReplyPacket.MediaHeader,
    &ArpReplyPacket.ArpHeader,
    sizeof (ARP_HEADER) + 2 * (Private->IpLength + SnpMode->HwAddressSize),
    &TmpMacAddr,
    PXE_PROTOCOL_ETHERNET_ARP,
    EFI_PXE_BASE_CODE_FUNCTION_ARP
    );

  //
  // Give time (100 microseconds) for ARP reply to get onto wire.
  //
  gBS->Stall (1000);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
BOOLEAN
GetHwAddr (
  IN PXE_BASECODE_DEVICE  *Private,
  IN EFI_IP_ADDRESS       *ProtocolAddrPtr,
  OUT EFI_MAC_ADDRESS     *HardwareAddrPtr
  )
/*++
Routine description:
  Locate IP address in ARP cache and return MAC address.

Parameters:
  Private := Pointer to PxeBc interface
  ProtocolAddrPtr := Pointer to IP address
  HardwareAddrPtr := Pointer to MAC address storage

Returns:
  TRUE := If IP address was found and MAC address was stored
  FALSE := If IP address was not found
--*/
{
  EFI_PXE_BASE_CODE_MODE  *PxeBcMode;
  UINTN                   HardwareAddrLength;
  UINTN                   Index;

  PxeBcMode           = Private->EfiBc.Mode;
  HardwareAddrLength  = Private->SimpleNetwork->Mode->HwAddressSize;

  for (Index = 0; Index < PxeBcMode->ArpCacheEntries; ++Index) {
    if (!EfiCompareMem (
          ProtocolAddrPtr,
          &PxeBcMode->ArpCache[Index].IpAddr,
          Private->IpLength
          )) {
      EfiCopyMem (
        HardwareAddrPtr,
        &PxeBcMode->ArpCache[Index].MacAddr,
        HardwareAddrLength
        );

⌨️ 快捷键说明

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