]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c
Libraries and utilities for instrumenting regions of code and measuring their perform...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Common.c
CommitLineData
772db4bb 1/** @file\r
2\r
766c7483 3Copyright (c) 2005 - 2009, Intel Corporation.<BR>\r
772db4bb 4All rights reserved. This program and the accompanying materials\r
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
270\r
271\r
272/**\r
273 Set the Ip4 variable data.\r
5405e9a6 274 \r
275 Save the list of all of the IPv4 addresses and subnet masks that are currently\r
276 being used to volatile variable storage.\r
772db4bb 277\r
3e8c18da 278 @param[in] IpSb Ip4 service binding instance\r
772db4bb 279\r
5405e9a6 280 @retval EFI_SUCCESS Successfully set variable.\r
772db4bb 281 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the variable.\r
282 @retval other Set variable failed.\r
283\r
284**/\r
285EFI_STATUS\r
286Ip4SetVariableData (\r
287 IN IP4_SERVICE *IpSb\r
288 )\r
289{\r
290 UINT32 NumConfiguredInstance;\r
e48e37fc 291 LIST_ENTRY *Entry;\r
772db4bb 292 UINTN VariableDataSize;\r
293 EFI_IP4_VARIABLE_DATA *Ip4VariableData;\r
294 EFI_IP4_ADDRESS_PAIR *Ip4AddressPair;\r
295 IP4_PROTOCOL *IpInstance;\r
296 CHAR16 *NewMacString;\r
297 EFI_STATUS Status;\r
298\r
299 NumConfiguredInstance = 0;\r
300\r
301 //\r
302 // Go through the children list to count the configured children.\r
303 //\r
304 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {\r
305 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
306\r
307 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
308 NumConfiguredInstance++;\r
309 }\r
310 }\r
311\r
312 //\r
313 // Calculate the size of the Ip4VariableData. As there may be no IP child,\r
314 // we should add extra buffer for the address paris only if the number of configured\r
315 // children is more than 1.\r
316 //\r
317 VariableDataSize = sizeof (EFI_IP4_VARIABLE_DATA);\r
318\r
319 if (NumConfiguredInstance > 1) {\r
320 VariableDataSize += sizeof (EFI_IP4_ADDRESS_PAIR) * (NumConfiguredInstance - 1);\r
321 }\r
322\r
e48e37fc 323 Ip4VariableData = AllocatePool (VariableDataSize);\r
772db4bb 324 if (Ip4VariableData == NULL) {\r
325 return EFI_OUT_OF_RESOURCES;\r
326 }\r
327\r
328 Ip4VariableData->DriverHandle = IpSb->Image;\r
329 Ip4VariableData->AddressCount = NumConfiguredInstance;\r
330\r
331 Ip4AddressPair = &Ip4VariableData->AddressPairs[0];\r
332\r
333 //\r
334 // Go through the children list to fill the configured children's address pairs.\r
335 //\r
336 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {\r
337 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
338\r
339 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
340 Ip4AddressPair->InstanceHandle = IpInstance->Handle;\r
341 EFI_IP4 (Ip4AddressPair->Ip4Address) = NTOHL (IpInstance->Interface->Ip);\r
342 EFI_IP4 (Ip4AddressPair->SubnetMask) = NTOHL (IpInstance->Interface->SubnetMask);\r
343\r
344 Ip4AddressPair++;\r
345 }\r
346 }\r
347\r
348 //\r
349 // Get the mac string.\r
350 //\r
351 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &NewMacString);\r
352 if (EFI_ERROR (Status)) {\r
353 goto ON_ERROR;\r
354 }\r
355\r
356 if (IpSb->MacString != NULL) {\r
357 //\r
358 // The variable is set already, we're going to update it.\r
359 //\r
360 if (StrCmp (IpSb->MacString, NewMacString) != 0) {\r
361 //\r
362 // The mac address is changed, delete the previous variable first.\r
363 //\r
364 gRT->SetVariable (\r
365 IpSb->MacString,\r
366 &gEfiIp4ServiceBindingProtocolGuid,\r
367 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
368 0,\r
369 NULL\r
370 );\r
371 }\r
372\r
766c7483 373 FreePool (IpSb->MacString);\r
772db4bb 374 }\r
375\r
376 IpSb->MacString = NewMacString;\r
377\r
378 Status = gRT->SetVariable (\r
379 IpSb->MacString,\r
380 &gEfiIp4ServiceBindingProtocolGuid,\r
381 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
382 VariableDataSize,\r
383 (VOID *) Ip4VariableData\r
384 );\r
385\r
386ON_ERROR:\r
387\r
766c7483 388 FreePool (Ip4VariableData);\r
772db4bb 389\r
390 return Status;\r
391}\r
392\r
393\r
394/**\r
395 Clear the variable and free the resource.\r
396\r
3e8c18da 397 @param[in] IpSb Ip4 service binding instance\r
772db4bb 398\r
399**/\r
400VOID\r
401Ip4ClearVariableData (\r
402 IN IP4_SERVICE *IpSb\r
403 )\r
404{\r
405 ASSERT (IpSb->MacString != NULL);\r
406\r
407 gRT->SetVariable (\r
408 IpSb->MacString,\r
409 &gEfiIp4ServiceBindingProtocolGuid,\r
410 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
411 0,\r
412 NULL\r
413 );\r
414\r
766c7483 415 FreePool (IpSb->MacString);\r
772db4bb 416 IpSb->MacString = NULL;\r
417}\r