ip4igmp.c

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

C
692
字号
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Status = Ip4SendIgmpReport (IpSb, Address);

  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Status = Mnp->Groups (Mnp, TRUE, &Group->Mac);

  if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
    goto ON_ERROR;
  }

  NetListInsertHead (&IgmpCtrl->Groups, &Group->Link);
  return EFI_SUCCESS;

ON_ERROR:
  NetFreePool (Group);
  return Status;
}

EFI_STATUS
Ip4LeaveGroup (
  IN IP4_PROTOCOL           *IpInstance,
  IN IP4_ADDR               Address
  )
/*++

Routine Description:

  Leave the IP4 multicast group on behavior of IpInstance.

Arguments:

  IpInstance  - The IP4 child that wants to leave the group address
  Address     - The group address to leave

Returns:

  EFI_NOT_FOUND - The IP4 service instance isn't in the group
  EFI_SUCCESS   - Successfully leave the multicast group.
  Others        - Failed to leave the multicast group.

--*/
{
  EFI_MANAGED_NETWORK_PROTOCOL  *Mnp;
  IP4_SERVICE                   *IpSb;
  IGMP_SERVICE_DATA             *IgmpCtrl;
  IGMP_GROUP                    *Group;
  EFI_STATUS                    Status;

  IpSb      = IpInstance->Service;
  IgmpCtrl  = &IpSb->IgmpCtrl;
  Mnp       = IpSb->Mnp;

  Group     = Ip4FindGroup (IgmpCtrl, Address);

  if (Group == NULL) {
    return EFI_NOT_FOUND;
  }

  //
  // If more than one instance is in the group, decrease 
  // the RefCnt then return.
  //
  if (--Group->RefCnt > 0) {
    return EFI_SUCCESS;
  }
  
  //
  // If multiple IP4 group addresses are mapped to the same 
  // multicast MAC address, don't configure the MNP to leave
  // the MAC.
  //
  if (Ip4FindMac (IgmpCtrl, &Group->Mac) == 1) {
    Status = Mnp->Groups (Mnp, FALSE, &Group->Mac);

    if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
      return Status;
    }
  }
  
  //
  // Send a leave report if the membership is reported by us
  // and we are talking IGMPv2.
  //
  if (Group->ReportByUs && !IgmpCtrl->Igmpv1QuerySeen) {
    Ip4SendIgmpMessage (IpSb, IP4_ALLROUTER_ADDRESS, IGMP_LEAVE_GROUP, Group->Address);
  }

  NetListRemoveEntry (&Group->Link);
  NetFreePool (Group);

  return EFI_SUCCESS;
}

EFI_STATUS
Ip4IgmpHandle (
  IN IP4_SERVICE            *IpSb,
  IN IP4_HEAD               *Head,
  IN NET_BUF                *Packet
  )
/*++

Routine Description:

  Handle the received IGMP message for the IP4 service instance.

Arguments:

  IpSb    - The IP4 service instance that received the message
  Head    - The IP4 header of the received message
  Packet  - The IGMP message, without IP4 header

Returns:

  EFI_INVALID_PARAMETER - The IGMP message is malformated.
  EFI_SUCCESS           - The IGMP message is successfully processed.

--*/
{
  IGMP_SERVICE_DATA         *IgmpCtrl;
  IGMP_HEAD                 Igmp;
  IGMP_GROUP                *Group;
  IP4_ADDR                  Address;
  NET_LIST_ENTRY            *Entry;

  IgmpCtrl = &IpSb->IgmpCtrl;

  //
  // Must checksum over the whole packet, later IGMP version
  // may employ message longer than 8 bytes. IP's header has
  // already been trimmed off.
  //
  if ((Packet->TotalSize < sizeof (Igmp)) || (NetbufChecksum (Packet) != 0)) {
    NetbufFree (Packet);
    return EFI_INVALID_PARAMETER;
  }
  
  //
  // Copy the packet in case it is fragmented
  //
  NetbufCopy (Packet, 0, sizeof (IGMP_HEAD), (UINT8 *)&Igmp);

  switch (Igmp.Type) {
  case IGMP_MEMBERSHIP_QUERY:
    //
    // If MaxRespTIme is zero, it is most likely that we are
    // talking to a V1 router
    //
    if (Igmp.MaxRespTime == 0) {
      IgmpCtrl->Igmpv1QuerySeen = IGMP_V1ROUTER_PRESENT;
      Igmp.MaxRespTime          = 100;
    }
    
    //
    // Igmp is ticking once per second but MaxRespTime is in
    // the unit of 100ms.
    //
    Igmp.MaxRespTime /= 10;
    Address = NTOHL (Igmp.Group);

    if (Address == IP4_ALLSYSTEM_ADDRESS) {
      break;
    }

    NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) {
      Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link);

      //
      // If address is all zero, all the memberships will be reported.
      // otherwise only one is reported.
      //
      if ((Address == IP4_ALLZERO_ADDRESS) || (Address == Group->Address)) {
        //
        // If the timer is pending, only update it if the time left
        // is longer than the MaxRespTime. TODO: randomize the DelayTime.
        //
        if ((Group->DelayTime == 0) || (Group->DelayTime > Igmp.MaxRespTime)) {
          Group->DelayTime = NET_MAX (1, Igmp.MaxRespTime);
        }
      }
    }

    break;

  case IGMP_V1_MEMBERSHIP_REPORT:
  case IGMP_V2_MEMBERSHIP_REPORT:
    Address = NTOHL (Igmp.Group);
    Group   = Ip4FindGroup (IgmpCtrl, Address);

    if ((Group != NULL) && (Group->DelayTime > 0)) {
      Group->DelayTime  = 0;
      Group->ReportByUs = FALSE;
    }

    break;
  }

  NetbufFree (Packet);
  return EFI_SUCCESS;
}

VOID
Ip4IgmpTicking (
  IN IP4_SERVICE            *IpSb
  )
/*++

Routine Description:

  The periodical timer function for IGMP. It does the following
  things:
  1. Decrease the Igmpv1QuerySeen to make it possible to refresh
     the IGMP server type.
  2. Decrease the report timer for each IGMP group in "delaying 
     member" state.

Arguments:

  IpSb  - The IP4 service instance that is ticking

Returns:

  None

--*/
{
  IGMP_SERVICE_DATA         *IgmpCtrl;
  NET_LIST_ENTRY            *Entry;
  IGMP_GROUP                *Group;

  IgmpCtrl = &IpSb->IgmpCtrl;

  if (IgmpCtrl->Igmpv1QuerySeen > 0) {
    IgmpCtrl->Igmpv1QuerySeen--;
  }

  //
  // Decrease the report timer for each IGMP group in "delaying member"
  //
  NET_LIST_FOR_EACH (Entry, &IgmpCtrl->Groups) {
    Group = NET_LIST_USER_STRUCT (Entry, IGMP_GROUP, Link);
    ASSERT (Group->DelayTime >= 0);

    if (Group->DelayTime > 0) {
      Group->DelayTime--;
      
      if (Group->DelayTime == 0) {
        Ip4SendIgmpReport (IpSb, Group->Address);
        Group->ReportByUs = TRUE;
      }
    }
  }
}

IP4_ADDR *
Ip4CombineGroups (
  IN  IP4_ADDR              *Source,
  IN  UINT32                Count,
  IN  IP4_ADDR              Addr
  )
/*++

Routine Description:

  Add a group address to the array of group addresses. 
  The caller should make sure that no duplicated address
  existed in the array. Although the function doesn't 
  assume the byte order of the both Source and Addr, the
  network byte order is used by the caller.
  
Arguments:

  Source - The array of group addresses to add to
  Count  - The number of group addresses in the Source
  Addr   - The IP4 multicast address to add

Returns:

  NULL if failed to allocate memory for the new groups, 
  otherwise the new combined group addresses.
  
--*/
{
  IP4_ADDR                  *Groups;

  Groups = NetAllocatePool (sizeof (IP4_ADDR) * (Count + 1));

  if (Groups == NULL) {
    return NULL;
  }

  NetCopyMem (Groups, Source, Count * sizeof (IP4_ADDR));
  Groups[Count] = Addr;

  return Groups;
}

INTN
Ip4RemoveGroupAddr (
  IN IP4_ADDR               *Groups,
  IN UINT32                 Count,
  IN IP4_ADDR               Addr
  )
/*++

Routine Description:

  Remove a group address frome the array of group addresses. 
  Although the function doesn't assume the byte order of the
  both Groups and Addr, the network byte order is used by 
  the caller.

Arguments:

  Groups - The array of group addresses to remove from
  Count  - The number of group addresses in the Groups
  Addr   - The IP4 multicast address to remove

Returns:

  The nubmer of group addresses in the Groups after remove.
  It is Count if the Addr isn't in the Groups.

--*/
{
  UINT32                    Index;

  for (Index = 0; Index < Count; Index++) {
    if (Groups[Index] == Addr) {
      break;
    }
  }

  while (Index < Count - 1) {
    Groups[Index] = Groups[Index + 1];
    Index++;
  }

  return Index;
}

⌨️ 快捷键说明

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