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