1 /** @file
2 
3   Implementation of the SNP.McastIpToMac() function and its private helpers if
4   any.
5 
6   Copyright (C) 2013, Red Hat, Inc.
7   Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
8 
9   This program and the accompanying materials are licensed and made available
10   under the terms and conditions of the BSD License which accompanies this
11   distribution. The full text of the license may be found at
12   http://opensource.org/licenses/bsd-license.php
13 
14   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
15   WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 
17 **/
18 
19 #include <Library/UefiBootServicesTableLib.h>
20 
21 #include "VirtioNet.h"
22 
23 /**
24   Converts a multicast IP address to a multicast HW MAC address.
25 
26   @param  This The protocol instance pointer.
27   @param  IPv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
28                to FALSE if the multicast IP address is IPv4 [RFC 791].
29   @param  IP   The multicast IP address that is to be converted to a multicast
30                HW MAC address.
31   @param  MAC  The multicast HW MAC address that is to be generated from IP.
32 
33   @retval EFI_SUCCESS           The multicast IP address was mapped to the
34                                 multicast HW MAC address.
35   @retval EFI_NOT_STARTED       The network interface has not been started.
36   @retval EFI_BUFFER_TOO_SMALL  The Statistics buffer was too small. The
37                                 current buffer size needed to hold the
38                                 statistics is returned in StatisticsSize.
39   @retval EFI_INVALID_PARAMETER One or more of the parameters has an
40                                 unsupported value.
41   @retval EFI_DEVICE_ERROR      The command could not be sent to the network
42                                 interface.
43   @retval EFI_UNSUPPORTED       This function is not supported by the network
44                                 interface.
45 
46 **/
47 
48 EFI_STATUS
49 EFIAPI
VirtioNetMcastIpToMac(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN BOOLEAN IPv6,IN EFI_IP_ADDRESS * Ip,OUT EFI_MAC_ADDRESS * Mac)50 VirtioNetMcastIpToMac (
51   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
52   IN BOOLEAN                     IPv6,
53   IN EFI_IP_ADDRESS              *Ip,
54   OUT EFI_MAC_ADDRESS            *Mac
55   )
56 {
57   VNET_DEV   *Dev;
58   EFI_TPL    OldTpl;
59   EFI_STATUS Status;
60 
61   //
62   // http://en.wikipedia.org/wiki/Multicast_address
63   //
64   if (This == NULL || Ip == NULL || Mac == NULL ||
65       ( IPv6 && (Ip->v6.Addr[0]       ) != 0xFF) || // invalid IPv6 mcast addr
66       (!IPv6 && (Ip->v4.Addr[0] & 0xF0) != 0xE0)    // invalid IPv4 mcast addr
67       ) {
68     return EFI_INVALID_PARAMETER;
69   }
70 
71   Dev = VIRTIO_NET_FROM_SNP (This);
72   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
73   switch (Dev->Snm.State) {
74   case EfiSimpleNetworkStopped:
75     Status = EFI_NOT_STARTED;
76     goto Exit;
77   case EfiSimpleNetworkStarted:
78     Status = EFI_DEVICE_ERROR;
79     goto Exit;
80   default:
81     break;
82   }
83 
84   //
85   // http://en.wikipedia.org/wiki/IP_multicast#Layer_2_delivery
86   //
87   if (IPv6) {
88     Mac->Addr[0] = 0x33;
89     Mac->Addr[1] = 0x33;
90     Mac->Addr[2] = Ip->v6.Addr[12];
91     Mac->Addr[3] = Ip->v6.Addr[13];
92     Mac->Addr[4] = Ip->v6.Addr[14];
93     Mac->Addr[5] = Ip->v6.Addr[15];
94   }
95   else {
96     Mac->Addr[0] = 0x01;
97     Mac->Addr[1] = 0x00;
98     Mac->Addr[2] = 0x5E;
99     Mac->Addr[3] = Ip->v4.Addr[1] & 0x7F;
100     Mac->Addr[4] = Ip->v4.Addr[2];
101     Mac->Addr[5] = Ip->v4.Addr[3];
102   }
103   Status = EFI_SUCCESS;
104 
105 Exit:
106   gBS->RestoreTPL (OldTpl);
107   return Status;
108 }
109