]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Common.c
added newline after brief summary
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Common.c
... / ...
CommitLineData
1/** @file\r
2\r
3Copyright (c) 2005 - 2006, Intel Corporation\r
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
12\r
13Module Name:\r
14\r
15 Ip4Common.c\r
16\r
17Abstract:\r
18\r
19\r
20**/\r
21\r
22#include "Ip4Impl.h"\r
23\r
24\r
25/**\r
26 Return the cast type (Unicast/Boradcast) specific to an\r
27 interface. All the addresses are host byte ordered.\r
28\r
29 @param IpAddr The IP address to classify in host byte order\r
30 @param IpIf The interface that IpAddr received from\r
31\r
32 @return The cast type of this IP address specific to the interface.\r
33 @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address\r
34 @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet boradcast to the\r
35 interface\r
36 @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface\r
37 @retval 0 Otherwise.\r
38\r
39**/\r
40INTN\r
41Ip4GetNetCast (\r
42 IN IP4_ADDR IpAddr,\r
43 IN IP4_INTERFACE *IpIf\r
44 )\r
45{\r
46 if (IpAddr == IpIf->Ip) {\r
47 return IP4_LOCAL_HOST;\r
48\r
49 } else if (IpAddr == IpIf->SubnetBrdcast) {\r
50 return IP4_SUBNET_BROADCAST;\r
51\r
52 } else if (IpAddr == IpIf->NetBrdcast) {\r
53 return IP4_NET_BROADCAST;\r
54\r
55 }\r
56\r
57 return 0;\r
58}\r
59\r
60\r
61/**\r
62 Find the cast type of the packet related to the local host.\r
63 This isn't the same as link layer cast type. For example, DHCP\r
64 server may send local broadcast to the local unicast MAC.\r
65\r
66 @param IpSb The IP4 service binding instance that received the\r
67 packet\r
68 @param Dst The destination address in the packet (host byte\r
69 order)\r
70 @param Src The source address in the packet (host byte order)\r
71\r
72 @return The cast type for the Dst, it will return on the first non-promiscuous\r
73 cast type to a configured interface. If the packet doesn't match any of\r
74 the interface, multicast address and local broadcast address are checked.\r
75\r
76**/\r
77INTN\r
78Ip4GetHostCast (\r
79 IN IP4_SERVICE *IpSb,\r
80 IN IP4_ADDR Dst,\r
81 IN IP4_ADDR Src\r
82 )\r
83{\r
84 LIST_ENTRY *Entry;\r
85 IP4_INTERFACE *IpIf;\r
86 INTN Type;\r
87 INTN Class;\r
88\r
89 Type = 0;\r
90\r
91 if (IpSb->MnpConfigData.EnablePromiscuousReceive) {\r
92 Type = IP4_PROMISCUOUS;\r
93 }\r
94\r
95 //\r
96 // Go through the interface list of the IP service, most likely.\r
97 //\r
98 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
99 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
100\r
101 //\r
102 // Skip the unconfigured interface and invalid source address:\r
103 // source address can't be broadcast.\r
104 //\r
105 if (!IpIf->Configured || IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {\r
106 continue;\r
107 }\r
108\r
109 if ((Class = Ip4GetNetCast (Dst, IpIf)) > Type) {\r
110 return Class;\r
111 }\r
112 }\r
113\r
114 //\r
115 // If it is local broadcast address. The source address must\r
116 // be a unicast address on one of the direct connected network.\r
117 // If it is a multicast address, accept it only if we are in\r
118 // the group.\r
119 //\r
120 if (Dst == IP4_ALLONE_ADDRESS) {\r
121 IpIf = Ip4FindNet (IpSb, Src);\r
122\r
123 if (IpIf != NULL && !IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {\r
124 return IP4_LOCAL_BROADCAST;\r
125 }\r
126\r
127 } else if (IP4_IS_MULTICAST (Dst) && Ip4FindGroup (&IpSb->IgmpCtrl, Dst) != NULL) {\r
128 return IP4_MULTICAST;\r
129 }\r
130\r
131 return Type;\r
132}\r
133\r
134\r
135/**\r
136 Find an interface whose configured IP address is Ip.\r
137\r
138 @param IpSb The IP4 service binding instance\r
139 @param Ip The Ip address (host byte order) to find\r
140\r
141 @return The IP4_INTERFACE point if found, otherwise NULL\r
142\r
143**/\r
144IP4_INTERFACE *\r
145Ip4FindInterface (\r
146 IN IP4_SERVICE *IpSb,\r
147 IN IP4_ADDR Ip\r
148 )\r
149{\r
150 LIST_ENTRY *Entry;\r
151 IP4_INTERFACE *IpIf;\r
152\r
153 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
154 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
155\r
156 if (IpIf->Configured && (IpIf->Ip == Ip)) {\r
157 return IpIf;\r
158 }\r
159 }\r
160\r
161 return NULL;\r
162}\r
163\r
164\r
165/**\r
166 Find an interface that Ip is on that connected network.\r
167\r
168 @param IpSb The IP4 service binding instance\r
169 @param Ip The Ip address (host byte order) to find\r
170\r
171 @return The IP4_INTERFACE point if found, otherwise NULL\r
172\r
173**/\r
174IP4_INTERFACE *\r
175Ip4FindNet (\r
176 IN IP4_SERVICE *IpSb,\r
177 IN IP4_ADDR Ip\r
178 )\r
179{\r
180 LIST_ENTRY *Entry;\r
181 IP4_INTERFACE *IpIf;\r
182\r
183 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
184 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
185\r
186 if (IpIf->Configured && IP4_NET_EQUAL (Ip, IpIf->Ip, IpIf->SubnetMask)) {\r
187 return IpIf;\r
188 }\r
189 }\r
190\r
191 return NULL;\r
192}\r
193\r
194\r
195/**\r
196 Find an interface of the service with the same Ip/Netmask pair.\r
197\r
198 @param IpSb Ip4 service binding instance\r
199 @param Ip The Ip adress to find (host byte order)\r
200 @param Netmask The network to find (host byte order)\r
201\r
202 @return The IP4_INTERFACE point if found, otherwise NULL\r
203\r
204**/\r
205IP4_INTERFACE *\r
206Ip4FindStationAddress (\r
207 IN IP4_SERVICE *IpSb,\r
208 IN IP4_ADDR Ip,\r
209 IN IP4_ADDR Netmask\r
210 )\r
211{\r
212 LIST_ENTRY *Entry;\r
213 IP4_INTERFACE *IpIf;\r
214\r
215 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
216 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
217\r
218 if (IpIf->Configured && (IpIf->Ip == Ip) && (IpIf->SubnetMask == Netmask)) {\r
219 return IpIf;\r
220 }\r
221 }\r
222\r
223 return NULL;\r
224}\r
225\r
226\r
227/**\r
228 Get the MAC address for a multicast IP address. Call\r
229 Mnp's McastIpToMac to find the MAC address in stead of\r
230 hard code the NIC to be Ethernet.\r
231\r
232 @param Mnp The Mnp instance to get the MAC address.\r
233 @param Multicast The multicast IP address to translate.\r
234 @param Mac The buffer to hold the translated address.\r
235\r
236 @retval EFI_SUCCESS if the multicast IP is successfully translated to a\r
237 multicast MAC address.\r
238 @retval other Otherwise some error.\r
239\r
240**/\r
241EFI_STATUS\r
242Ip4GetMulticastMac (\r
243 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,\r
244 IN IP4_ADDR Multicast,\r
245 OUT EFI_MAC_ADDRESS *Mac\r
246 )\r
247{\r
248 EFI_IP_ADDRESS EfiIp;\r
249\r
250 EFI_IP4 (EfiIp.v4) = HTONL (Multicast);\r
251 return Mnp->McastIpToMac (Mnp, FALSE, &EfiIp, Mac);\r
252}\r
253\r
254\r
255/**\r
256 Convert the multibyte field in IP header's byter order.\r
257 In spite of its name, it can also be used to convert from\r
258 host to network byte order.\r
259\r
260 @param Head The IP head to convert\r
261\r
262 @return Point to the converted IP head\r
263\r
264**/\r
265IP4_HEAD *\r
266Ip4NtohHead (\r
267 IN IP4_HEAD *Head\r
268 )\r
269{\r
270 Head->TotalLen = NTOHS (Head->TotalLen);\r
271 Head->Id = NTOHS (Head->Id);\r
272 Head->Fragment = NTOHS (Head->Fragment);\r
273 Head->Src = NTOHL (Head->Src);\r
274 Head->Dst = NTOHL (Head->Dst);\r
275\r
276 return Head;\r
277}\r
278\r
279\r
280/**\r
281 Set the Ip4 variable data.\r
282 \r
283 Save the list of all of the IPv4 addresses and subnet masks that are currently\r
284 being used to volatile variable storage.\r
285\r
286 @param IpSb Ip4 service binding instance\r
287\r
288 @retval EFI_SUCCESS Successfully set variable.\r
289 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the variable.\r
290 @retval other Set variable failed.\r
291\r
292**/\r
293EFI_STATUS\r
294Ip4SetVariableData (\r
295 IN IP4_SERVICE *IpSb\r
296 )\r
297{\r
298 UINT32 NumConfiguredInstance;\r
299 LIST_ENTRY *Entry;\r
300 UINTN VariableDataSize;\r
301 EFI_IP4_VARIABLE_DATA *Ip4VariableData;\r
302 EFI_IP4_ADDRESS_PAIR *Ip4AddressPair;\r
303 IP4_PROTOCOL *IpInstance;\r
304 CHAR16 *NewMacString;\r
305 EFI_STATUS Status;\r
306\r
307 NumConfiguredInstance = 0;\r
308\r
309 //\r
310 // Go through the children list to count the configured children.\r
311 //\r
312 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {\r
313 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
314\r
315 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
316 NumConfiguredInstance++;\r
317 }\r
318 }\r
319\r
320 //\r
321 // Calculate the size of the Ip4VariableData. As there may be no IP child,\r
322 // we should add extra buffer for the address paris only if the number of configured\r
323 // children is more than 1.\r
324 //\r
325 VariableDataSize = sizeof (EFI_IP4_VARIABLE_DATA);\r
326\r
327 if (NumConfiguredInstance > 1) {\r
328 VariableDataSize += sizeof (EFI_IP4_ADDRESS_PAIR) * (NumConfiguredInstance - 1);\r
329 }\r
330\r
331 Ip4VariableData = AllocatePool (VariableDataSize);\r
332 if (Ip4VariableData == NULL) {\r
333 return EFI_OUT_OF_RESOURCES;\r
334 }\r
335\r
336 Ip4VariableData->DriverHandle = IpSb->Image;\r
337 Ip4VariableData->AddressCount = NumConfiguredInstance;\r
338\r
339 Ip4AddressPair = &Ip4VariableData->AddressPairs[0];\r
340\r
341 //\r
342 // Go through the children list to fill the configured children's address pairs.\r
343 //\r
344 NET_LIST_FOR_EACH (Entry, &IpSb->Children) {\r
345 IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP4_PROTOCOL, Link, IP4_PROTOCOL_SIGNATURE);\r
346\r
347 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
348 Ip4AddressPair->InstanceHandle = IpInstance->Handle;\r
349 EFI_IP4 (Ip4AddressPair->Ip4Address) = NTOHL (IpInstance->Interface->Ip);\r
350 EFI_IP4 (Ip4AddressPair->SubnetMask) = NTOHL (IpInstance->Interface->SubnetMask);\r
351\r
352 Ip4AddressPair++;\r
353 }\r
354 }\r
355\r
356 //\r
357 // Get the mac string.\r
358 //\r
359 Status = NetLibGetMacString (IpSb->Controller, IpSb->Image, &NewMacString);\r
360 if (EFI_ERROR (Status)) {\r
361 goto ON_ERROR;\r
362 }\r
363\r
364 if (IpSb->MacString != NULL) {\r
365 //\r
366 // The variable is set already, we're going to update it.\r
367 //\r
368 if (StrCmp (IpSb->MacString, NewMacString) != 0) {\r
369 //\r
370 // The mac address is changed, delete the previous variable first.\r
371 //\r
372 gRT->SetVariable (\r
373 IpSb->MacString,\r
374 &gEfiIp4ServiceBindingProtocolGuid,\r
375 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
376 0,\r
377 NULL\r
378 );\r
379 }\r
380\r
381 gBS->FreePool (IpSb->MacString);\r
382 }\r
383\r
384 IpSb->MacString = NewMacString;\r
385\r
386 Status = gRT->SetVariable (\r
387 IpSb->MacString,\r
388 &gEfiIp4ServiceBindingProtocolGuid,\r
389 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
390 VariableDataSize,\r
391 (VOID *) Ip4VariableData\r
392 );\r
393\r
394ON_ERROR:\r
395\r
396 gBS->FreePool (Ip4VariableData);\r
397\r
398 return Status;\r
399}\r
400\r
401\r
402/**\r
403 Clear the variable and free the resource.\r
404\r
405 @param IpSb Ip4 service binding instance\r
406\r
407 @return None.\r
408\r
409**/\r
410VOID\r
411Ip4ClearVariableData (\r
412 IN IP4_SERVICE *IpSb\r
413 )\r
414{\r
415 ASSERT (IpSb->MacString != NULL);\r
416\r
417 gRT->SetVariable (\r
418 IpSb->MacString,\r
419 &gEfiIp4ServiceBindingProtocolGuid,\r
420 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
421 0,\r
422 NULL\r
423 );\r
424\r
425 gBS->FreePool (IpSb->MacString);\r
426 IpSb->MacString = NULL;\r
427}\r