3 Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Return the cast type (Unicast/Boradcast) specific to an
19 interface. All the addresses are host byte ordered.
21 @param[in] IpAddr The IP address to classify in host byte order
22 @param[in] IpIf The interface that IpAddr received from
24 @return The cast type of this IP address specific to the interface.
25 @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address
26 @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet boradcast to the
28 @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface
35 IN IP4_INTERFACE
*IpIf
38 if (IpAddr
== IpIf
->Ip
) {
39 return IP4_LOCAL_HOST
;
41 } else if (IpAddr
== IpIf
->SubnetBrdcast
) {
42 return IP4_SUBNET_BROADCAST
;
44 } else if (IpAddr
== IpIf
->NetBrdcast
) {
45 return IP4_NET_BROADCAST
;
54 Find the cast type of the packet related to the local host.
55 This isn't the same as link layer cast type. For example, DHCP
56 server may send local broadcast to the local unicast MAC.
58 @param[in] IpSb The IP4 service binding instance that received the
60 @param[in] Dst The destination address in the packet (host byte
62 @param[in] Src The source address in the packet (host byte order)
64 @return The cast type for the Dst, it will return on the first non-promiscuous
65 cast type to a configured interface. If the packet doesn't match any of
66 the interface, multicast address and local broadcast address are checked.
83 if (IpSb
->MnpConfigData
.EnablePromiscuousReceive
) {
84 Type
= IP4_PROMISCUOUS
;
88 // Go through the interface list of the IP service, most likely.
90 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
91 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
94 // Skip the unconfigured interface and invalid source address:
95 // source address can't be broadcast.
97 if (!IpIf
->Configured
|| IP4_IS_BROADCAST (Ip4GetNetCast (Src
, IpIf
))) {
101 if ((Class
= Ip4GetNetCast (Dst
, IpIf
)) > Type
) {
107 // If it is local broadcast address. The source address must
108 // be a unicast address on one of the direct connected network.
109 // If it is a multicast address, accept it only if we are in
112 if (Dst
== IP4_ALLONE_ADDRESS
) {
113 IpIf
= Ip4FindNet (IpSb
, Src
);
115 if (IpIf
!= NULL
&& !IP4_IS_BROADCAST (Ip4GetNetCast (Src
, IpIf
))) {
116 return IP4_LOCAL_BROADCAST
;
119 } else if (IP4_IS_MULTICAST (Dst
) && Ip4FindGroup (&IpSb
->IgmpCtrl
, Dst
) != NULL
) {
120 return IP4_MULTICAST
;
128 Find an interface whose configured IP address is Ip.
130 @param[in] IpSb The IP4 service binding instance
131 @param[in] Ip The Ip address (host byte order) to find
133 @return The IP4_INTERFACE point if found, otherwise NULL
138 IN IP4_SERVICE
*IpSb
,
145 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
146 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
148 if (IpIf
->Configured
&& (IpIf
->Ip
== Ip
)) {
158 Find an interface that Ip is on that connected network.
160 @param[in] IpSb The IP4 service binding instance
161 @param[in] Ip The Ip address (host byte order) to find
163 @return The IP4_INTERFACE point if found, otherwise NULL
168 IN IP4_SERVICE
*IpSb
,
175 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
176 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
178 if (IpIf
->Configured
&& IP4_NET_EQUAL (Ip
, IpIf
->Ip
, IpIf
->SubnetMask
)) {
188 Find an interface of the service with the same Ip/Netmask pair.
190 @param[in] IpSb Ip4 service binding instance
191 @param[in] Ip The Ip adress to find (host byte order)
192 @param[in] Netmask The network to find (host byte order)
194 @return The IP4_INTERFACE point if found, otherwise NULL
198 Ip4FindStationAddress (
199 IN IP4_SERVICE
*IpSb
,
207 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
208 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
210 if (IpIf
->Configured
&& (IpIf
->Ip
== Ip
) && (IpIf
->SubnetMask
== Netmask
)) {
220 Get the MAC address for a multicast IP address. Call
221 Mnp's McastIpToMac to find the MAC address in stead of
222 hard code the NIC to be Ethernet.
224 @param[in] Mnp The Mnp instance to get the MAC address.
225 @param[in] Multicast The multicast IP address to translate.
226 @param[out] Mac The buffer to hold the translated address.
228 @retval EFI_SUCCESS if the multicast IP is successfully translated to a
229 multicast MAC address.
230 @retval other Otherwise some error.
235 IN EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
,
236 IN IP4_ADDR Multicast
,
237 OUT EFI_MAC_ADDRESS
*Mac
240 EFI_IP_ADDRESS EfiIp
;
242 EFI_IP4 (EfiIp
.v4
) = HTONL (Multicast
);
243 return Mnp
->McastIpToMac (Mnp
, FALSE
, &EfiIp
, Mac
);
248 Convert the multibyte field in IP header's byter order.
249 In spite of its name, it can also be used to convert from
250 host to network byte order.
252 @param[in] Head The IP head to convert
254 @return Point to the converted IP head
262 Head
->TotalLen
= NTOHS (Head
->TotalLen
);
263 Head
->Id
= NTOHS (Head
->Id
);
264 Head
->Fragment
= NTOHS (Head
->Fragment
);
265 Head
->Src
= NTOHL (Head
->Src
);
266 Head
->Dst
= NTOHL (Head
->Dst
);
273 Validate that Ip/Netmask pair is OK to be used as station
274 address. Only continuous netmasks are supported. and check
275 that StationAddress is a unicast address on the newtwork.
277 @param[in] Ip The IP address to validate.
278 @param[in] Netmask The netmaks of the IP.
280 @retval TRUE The Ip/Netmask pair is valid.
281 @retval FALSE The Ip/Netmask pair is invalid.
285 Ip4StationAddressValid (
291 // Only support the station address with 0.0.0.0/0 to enable DHCP client.
293 if (Netmask
== IP4_ALLZERO_ADDRESS
) {
294 return (BOOLEAN
) (Ip
== IP4_ALLZERO_ADDRESS
);
298 // Only support the continuous net masks
300 if (NetGetMaskLength (Netmask
) == (IP4_MASK_MAX
+ 1)) {
305 // Station address can't be class D or class E address
307 if (NetGetIpClass (Ip
) > IP4_ADDR_CLASSC
) {
311 return NetIp4IsUnicast (Ip
, Netmask
);