MdeModulePkg/DxeCapsuleLibFmp: Fix build failure issues
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Common.c
CommitLineData
772db4bb 1/** @file\r
2\r
d551cc64 3Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 4This program and the accompanying materials\r
772db4bb 5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
772db4bb 12**/\r
13\r
14#include "Ip4Impl.h"\r
15\r
16\r
17/**\r
5405e9a6 18 Return the cast type (Unicast/Boradcast) specific to an\r
772db4bb 19 interface. All the addresses are host byte ordered.\r
20\r
3e8c18da 21 @param[in] IpAddr The IP address to classify in host byte order\r
22 @param[in] IpIf The interface that IpAddr received from\r
772db4bb 23\r
24 @return The cast type of this IP address specific to the interface.\r
25 @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address\r
26 @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet boradcast to the\r
27 interface\r
28 @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface\r
5405e9a6 29 @retval 0 Otherwise.\r
772db4bb 30\r
31**/\r
32INTN\r
33Ip4GetNetCast (\r
34 IN IP4_ADDR IpAddr,\r
35 IN IP4_INTERFACE *IpIf\r
36 )\r
37{\r
38 if (IpAddr == IpIf->Ip) {\r
39 return IP4_LOCAL_HOST;\r
40\r
41 } else if (IpAddr == IpIf->SubnetBrdcast) {\r
42 return IP4_SUBNET_BROADCAST;\r
43\r
44 } else if (IpAddr == IpIf->NetBrdcast) {\r
45 return IP4_NET_BROADCAST;\r
46\r
47 }\r
48\r
49 return 0;\r
50}\r
51\r
52\r
53/**\r
54 Find the cast type of the packet related to the local host.\r
55 This isn't the same as link layer cast type. For example, DHCP\r
56 server may send local broadcast to the local unicast MAC.\r
57\r
3e8c18da 58 @param[in] IpSb The IP4 service binding instance that received the\r
59 packet\r
60 @param[in] Dst The destination address in the packet (host byte\r
61 order)\r
62 @param[in] Src The source address in the packet (host byte order)\r
772db4bb 63\r
64 @return The cast type for the Dst, it will return on the first non-promiscuous\r
5405e9a6 65 cast type to a configured interface. If the packet doesn't match any of\r
66 the interface, multicast address and local broadcast address are checked.\r
772db4bb 67\r
68**/\r
69INTN\r
70Ip4GetHostCast (\r
71 IN IP4_SERVICE *IpSb,\r
72 IN IP4_ADDR Dst,\r
73 IN IP4_ADDR Src\r
74 )\r
75{\r
e48e37fc 76 LIST_ENTRY *Entry;\r
772db4bb 77 IP4_INTERFACE *IpIf;\r
78 INTN Type;\r
79 INTN Class;\r
80\r
81 Type = 0;\r
82\r
83 if (IpSb->MnpConfigData.EnablePromiscuousReceive) {\r
84 Type = IP4_PROMISCUOUS;\r
85 }\r
86\r
87 //\r
88 // Go through the interface list of the IP service, most likely.\r
89 //\r
90 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
91 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
92\r
93 //\r
94 // Skip the unconfigured interface and invalid source address:\r
95 // source address can't be broadcast.\r
96 //\r
97 if (!IpIf->Configured || IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {\r
98 continue;\r
99 }\r
100\r
101 if ((Class = Ip4GetNetCast (Dst, IpIf)) > Type) {\r
102 return Class;\r
103 }\r
104 }\r
105\r
106 //\r
107 // If it is local broadcast address. The source address must\r
108 // be a unicast address on one of the direct connected network.\r
109 // If it is a multicast address, accept it only if we are in\r
110 // the group.\r
111 //\r
112 if (Dst == IP4_ALLONE_ADDRESS) {\r
113 IpIf = Ip4FindNet (IpSb, Src);\r
114\r
5405e9a6 115 if (IpIf != NULL && !IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {\r
772db4bb 116 return IP4_LOCAL_BROADCAST;\r
117 }\r
118\r
5405e9a6 119 } else if (IP4_IS_MULTICAST (Dst) && Ip4FindGroup (&IpSb->IgmpCtrl, Dst) != NULL) {\r
772db4bb 120 return IP4_MULTICAST;\r
121 }\r
122\r
123 return Type;\r
124}\r
125\r
126\r
127/**\r
5405e9a6 128 Find an interface whose configured IP address is Ip.\r
772db4bb 129\r
3e8c18da 130 @param[in] IpSb The IP4 service binding instance\r
131 @param[in] Ip The Ip address (host byte order) to find\r
772db4bb 132\r
133 @return The IP4_INTERFACE point if found, otherwise NULL\r
134\r
135**/\r
136IP4_INTERFACE *\r
137Ip4FindInterface (\r
138 IN IP4_SERVICE *IpSb,\r
139 IN IP4_ADDR Ip\r
140 )\r
141{\r
e48e37fc 142 LIST_ENTRY *Entry;\r
772db4bb 143 IP4_INTERFACE *IpIf;\r
144\r
145 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
146 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
147\r
148 if (IpIf->Configured && (IpIf->Ip == Ip)) {\r
149 return IpIf;\r
150 }\r
151 }\r
152\r
153 return NULL;\r
154}\r
155\r
156\r
157/**\r
158 Find an interface that Ip is on that connected network.\r
159\r
3e8c18da 160 @param[in] IpSb The IP4 service binding instance\r
161 @param[in] Ip The Ip address (host byte order) to find\r
772db4bb 162\r
163 @return The IP4_INTERFACE point if found, otherwise NULL\r
164\r
165**/\r
166IP4_INTERFACE *\r
167Ip4FindNet (\r
168 IN IP4_SERVICE *IpSb,\r
169 IN IP4_ADDR Ip\r
170 )\r
171{\r
e48e37fc 172 LIST_ENTRY *Entry;\r
772db4bb 173 IP4_INTERFACE *IpIf;\r
174\r
175 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
176 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
177\r
178 if (IpIf->Configured && IP4_NET_EQUAL (Ip, IpIf->Ip, IpIf->SubnetMask)) {\r
179 return IpIf;\r
180 }\r
181 }\r
182\r
183 return NULL;\r
184}\r
185\r
186\r
187/**\r
188 Find an interface of the service with the same Ip/Netmask pair.\r
189\r
3e8c18da 190 @param[in] IpSb Ip4 service binding instance\r
191 @param[in] Ip The Ip adress to find (host byte order)\r
192 @param[in] Netmask The network to find (host byte order)\r
772db4bb 193\r
194 @return The IP4_INTERFACE point if found, otherwise NULL\r
195\r
196**/\r
197IP4_INTERFACE *\r
198Ip4FindStationAddress (\r
199 IN IP4_SERVICE *IpSb,\r
200 IN IP4_ADDR Ip,\r
201 IN IP4_ADDR Netmask\r
202 )\r
203{\r
e48e37fc 204 LIST_ENTRY *Entry;\r
772db4bb 205 IP4_INTERFACE *IpIf;\r
206\r
207 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
208 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
209\r
210 if (IpIf->Configured && (IpIf->Ip == Ip) && (IpIf->SubnetMask == Netmask)) {\r
211 return IpIf;\r
212 }\r
213 }\r
214\r
215 return NULL;\r
216}\r
217\r
218\r
219/**\r
220 Get the MAC address for a multicast IP address. Call\r
221 Mnp's McastIpToMac to find the MAC address in stead of\r
222 hard code the NIC to be Ethernet.\r
223\r
3e8c18da 224 @param[in] Mnp The Mnp instance to get the MAC address.\r
225 @param[in] Multicast The multicast IP address to translate.\r
226 @param[out] Mac The buffer to hold the translated address.\r
772db4bb 227\r
5405e9a6 228 @retval EFI_SUCCESS if the multicast IP is successfully translated to a\r
229 multicast MAC address.\r
230 @retval other Otherwise some error.\r
772db4bb 231\r
232**/\r
233EFI_STATUS\r
234Ip4GetMulticastMac (\r
235 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,\r
236 IN IP4_ADDR Multicast,\r
237 OUT EFI_MAC_ADDRESS *Mac\r
238 )\r
239{\r
240 EFI_IP_ADDRESS EfiIp;\r
241\r
242 EFI_IP4 (EfiIp.v4) = HTONL (Multicast);\r
243 return Mnp->McastIpToMac (Mnp, FALSE, &EfiIp, Mac);\r
244}\r
245\r
246\r
247/**\r
248 Convert the multibyte field in IP header's byter order.\r
249 In spite of its name, it can also be used to convert from\r
250 host to network byte order.\r
251\r
3e8c18da 252 @param[in] Head The IP head to convert\r
772db4bb 253\r
254 @return Point to the converted IP head\r
255\r
256**/\r
257IP4_HEAD *\r
258Ip4NtohHead (\r
259 IN IP4_HEAD *Head\r
260 )\r
261{\r
262 Head->TotalLen = NTOHS (Head->TotalLen);\r
263 Head->Id = NTOHS (Head->Id);\r
264 Head->Fragment = NTOHS (Head->Fragment);\r
265 Head->Src = NTOHL (Head->Src);\r
266 Head->Dst = NTOHL (Head->Dst);\r
267\r
268 return Head;\r
269}\r